nouveau: install libdrm_nouveau with libdrm

main
Ben Skeggs 2009-01-30 11:25:35 +10:00
parent cb85630c02
commit 225e7e274f
24 changed files with 11307 additions and 1 deletions

View File

@ -131,6 +131,8 @@ AC_OUTPUT([
Makefile
libdrm/Makefile
libdrm/intel/Makefile
libdrm/nouveau/Makefile
libdrm/nouveau/libdrm_nouveau.pc
shared-core/Makefile
tests/Makefile
tests/modeprint/Makefile

View File

@ -18,7 +18,7 @@
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SUBDIRS = . intel
SUBDIRS = . intel nouveau
libdrm_la_LTLIBRARIES = libdrm.la
libdrm_ladir = $(libdir)

View File

@ -0,0 +1,40 @@
AM_CFLAGS = \
$(WARN_CFLAGS) \
-I$(top_srcdir)/libdrm \
-I$(top_srcdir)/libdrm/nouveau \
$(PTHREADSTUBS_CFLAGS) \
-I$(top_srcdir)/shared-core
libdrm_nouveau_la_LTLIBRARIES = libdrm_nouveau.la
libdrm_nouveau_ladir = $(libdir)
libdrm_nouveau_la_LDFLAGS = -version-number 1:0:0 -no-undefined
libdrm_nouveau_la_LIBADD = ../libdrm.la @PTHREADSTUBS_LIBS@
libdrm_nouveau_la_SOURCES = \
nouveau_device.c \
nouveau_channel.c \
nouveau_pushbuf.c \
nouveau_grobj.c \
nouveau_notifier.c \
nouveau_bo.c \
nouveau_resource.c \
nouveau_dma.c \
nouveau_fence.c
libdrm_nouveaucommonincludedir = ${includedir}/nouveau
libdrm_nouveaucommoninclude_HEADERS = \
nouveau_device.h \
nouveau_channel.h \
nouveau_grobj.h \
nouveau_notifier.h \
nouveau_pushbuf.h \
nouveau_bo.h \
nouveau_resource.h \
nouveau_class.h
libdrm_nouveauincludedir = ${includedir}/drm
libdrm_nouveauinclude_HEADERS = \
nouveau_drmif.h
pkgconfigdir = @pkgconfigdir@
pkgconfig_DATA = libdrm_nouveau.pc

View File

@ -0,0 +1,10 @@
prefix=@prefix@
exec_prefix=@exec_prefix@
libdir=@libdir@
includedir=@includedir@
Name: libdrm_nouveau
Description: Userspace interface to nouveau kernel DRM services
Version: 0.5
Libs: -L${libdir} -ldrm_nouveau
Cflags: -I${includedir} -I${includedir}/drm -I${includedir}/nouveau

838
libdrm/nouveau/nouveau_bo.c Normal file
View File

@ -0,0 +1,838 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include "nouveau_private.h"
int
nouveau_bo_init(struct nouveau_device *dev)
{
return 0;
}
void
nouveau_bo_takedown(struct nouveau_device *dev)
{
}
static int
nouveau_bo_allocated(struct nouveau_bo_priv *nvbo)
{
if (nvbo->sysmem || nvbo->handle || (nvbo->flags & NOUVEAU_BO_PIN))
return 1;
return 0;
}
static int
nouveau_bo_ualloc(struct nouveau_bo_priv *nvbo)
{
if (nvbo->user || nvbo->sysmem) {
assert(nvbo->sysmem);
return 0;
}
nvbo->sysmem = malloc(nvbo->size);
if (!nvbo->sysmem)
return -ENOMEM;
return 0;
}
static void
nouveau_bo_ufree(struct nouveau_bo_priv *nvbo)
{
if (nvbo->sysmem) {
if (!nvbo->user)
free(nvbo->sysmem);
nvbo->sysmem = NULL;
}
}
static void
nouveau_bo_kfree_nomm(struct nouveau_bo_priv *nvbo)
{
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
struct drm_nouveau_mem_free req;
if (nvbo->map) {
drmUnmap(nvbo->map, nvbo->size);
nvbo->map = NULL;
}
req.offset = nvbo->offset;
if (nvbo->domain & NOUVEAU_BO_GART)
req.flags = NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI;
else
if (nvbo->domain & NOUVEAU_BO_VRAM)
req.flags = NOUVEAU_MEM_FB;
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_MEM_FREE, &req, sizeof(req));
nvbo->handle = 0;
}
static void
nouveau_bo_kfree(struct nouveau_bo_priv *nvbo)
{
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
struct drm_gem_close req;
if (!nvbo->handle)
return;
if (!nvdev->mm_enabled) {
nouveau_bo_kfree_nomm(nvbo);
return;
}
if (nvbo->map) {
munmap(nvbo->map, nvbo->size);
nvbo->map = NULL;
}
req.handle = nvbo->handle;
nvbo->handle = 0;
ioctl(nvdev->fd, DRM_IOCTL_GEM_CLOSE, &req);
}
static int
nouveau_bo_kalloc_nomm(struct nouveau_bo_priv *nvbo)
{
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
struct drm_nouveau_mem_alloc req;
int ret;
if (nvbo->handle)
return 0;
if (!(nvbo->flags & (NOUVEAU_BO_VRAM|NOUVEAU_BO_GART)))
nvbo->flags |= (NOUVEAU_BO_GART | NOUVEAU_BO_VRAM);
req.size = nvbo->size;
req.alignment = nvbo->align;
req.flags = 0;
if (nvbo->flags & NOUVEAU_BO_VRAM)
req.flags |= NOUVEAU_MEM_FB;
if (nvbo->flags & NOUVEAU_BO_GART)
req.flags |= (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI);
if (nvbo->flags & NOUVEAU_BO_TILED) {
req.flags |= NOUVEAU_MEM_TILE;
if (nvbo->flags & NOUVEAU_BO_ZTILE)
req.flags |= NOUVEAU_MEM_TILE_ZETA;
}
req.flags |= NOUVEAU_MEM_MAPPED;
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_MEM_ALLOC,
&req, sizeof(req));
if (ret)
return ret;
nvbo->handle = req.map_handle;
nvbo->size = req.size;
nvbo->offset = req.offset;
if (req.flags & (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI))
nvbo->domain = NOUVEAU_BO_GART;
else
if (req.flags & NOUVEAU_MEM_FB)
nvbo->domain = NOUVEAU_BO_VRAM;
return 0;
}
static int
nouveau_bo_kalloc(struct nouveau_bo_priv *nvbo, struct nouveau_channel *chan)
{
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
struct drm_nouveau_gem_new req;
int ret;
if (nvbo->handle || (nvbo->flags & NOUVEAU_BO_PIN))
return 0;
if (!nvdev->mm_enabled)
return nouveau_bo_kalloc_nomm(nvbo);
req.channel_hint = chan ? chan->id : 0;
req.size = nvbo->size;
req.align = nvbo->align;
req.domain = 0;
if (nvbo->flags & NOUVEAU_BO_VRAM)
req.domain |= NOUVEAU_GEM_DOMAIN_VRAM;
if (nvbo->flags & NOUVEAU_BO_GART)
req.domain |= NOUVEAU_GEM_DOMAIN_GART;
if (nvbo->flags & NOUVEAU_BO_TILED) {
req.domain |= NOUVEAU_GEM_DOMAIN_TILE;
if (nvbo->flags & NOUVEAU_BO_ZTILE)
req.domain |= NOUVEAU_GEM_DOMAIN_TILE_ZETA;
}
if (!req.domain) {
req.domain |= (NOUVEAU_GEM_DOMAIN_VRAM |
NOUVEAU_GEM_DOMAIN_GART);
}
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_NEW,
&req, sizeof(req));
if (ret)
return ret;
nvbo->handle = nvbo->base.handle = req.handle;
nvbo->size = req.size;
nvbo->domain = req.domain;
nvbo->offset = req.offset;
return 0;
}
static int
nouveau_bo_kmap_nomm(struct nouveau_bo_priv *nvbo)
{
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
int ret;
ret = drmMap(nvdev->fd, nvbo->handle, nvbo->size, &nvbo->map);
if (ret) {
nvbo->map = NULL;
return ret;
}
return 0;
}
static int
nouveau_bo_kmap(struct nouveau_bo_priv *nvbo)
{
struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
struct drm_nouveau_gem_mmap req;
int ret;
if (nvbo->map)
return 0;
if (!nvbo->handle)
return -EINVAL;
if (!nvdev->mm_enabled)
return nouveau_bo_kmap_nomm(nvbo);
req.handle = nvbo->handle;
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_MMAP,
&req, sizeof(req));
if (ret)
return ret;
nvbo->map = (void *)(unsigned long)req.vaddr;
return 0;
}
int
nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
int size, struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo;
int ret;
if (!dev || !bo || *bo)
return -EINVAL;
nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
if (!nvbo)
return -ENOMEM;
nvbo->base.device = dev;
nvbo->base.size = size;
nvbo->refcount = 1;
/* Don't set NOUVEAU_BO_PIN here, or nouveau_bo_allocated() will
* decided the buffer's already allocated when it's not. The
* call to nouveau_bo_pin() later will set this flag.
*/
nvbo->flags = (flags & ~NOUVEAU_BO_PIN);
nvbo->size = size;
nvbo->align = align;
/*XXX: murder me violently */
if (flags & NOUVEAU_BO_TILED) {
nvbo->base.tiled = 1;
if (flags & NOUVEAU_BO_ZTILE)
nvbo->base.tiled |= 2;
}
if (flags & NOUVEAU_BO_PIN) {
ret = nouveau_bo_pin((void *)nvbo, nvbo->flags);
if (ret) {
nouveau_bo_ref(NULL, (void *)nvbo);
return ret;
}
}
*bo = &nvbo->base;
return 0;
}
int
nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo;
int ret;
ret = nouveau_bo_new(dev, 0, 0, size, bo);
if (ret)
return ret;
nvbo = nouveau_bo(*bo);
nvbo->sysmem = ptr;
nvbo->user = 1;
return 0;
}
int
nouveau_bo_fake(struct nouveau_device *dev, uint64_t offset, uint32_t flags,
uint32_t size, void *map, struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo;
int ret;
ret = nouveau_bo_new(dev, flags & ~NOUVEAU_BO_PIN, 0, size, bo);
if (ret)
return ret;
nvbo = nouveau_bo(*bo);
nvbo->flags = flags | NOUVEAU_BO_PIN;
nvbo->domain = (flags & (NOUVEAU_BO_VRAM|NOUVEAU_BO_GART));
nvbo->offset = offset;
nvbo->size = nvbo->base.size = size;
nvbo->map = map;
nvbo->base.flags = nvbo->flags;
nvbo->base.offset = nvbo->offset;
return 0;
}
int
nouveau_bo_handle_get(struct nouveau_bo *bo, uint32_t *handle)
{
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
int ret;
if (!bo || !handle)
return -EINVAL;
if (!nvdev->mm_enabled)
return -ENODEV;
if (!nvbo->global_handle) {
struct drm_gem_flink req;
ret = nouveau_bo_kalloc(nvbo, NULL);
if (ret)
return ret;
req.handle = nvbo->handle;
ret = ioctl(nvdev->fd, DRM_IOCTL_GEM_FLINK, &req);
if (ret) {
nouveau_bo_kfree(nvbo);
return ret;
}
nvbo->global_handle = req.name;
}
*handle = nvbo->global_handle;
return 0;
}
int
nouveau_bo_handle_ref(struct nouveau_device *dev, uint32_t handle,
struct nouveau_bo **bo)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
struct nouveau_bo_priv *nvbo;
struct drm_gem_open req;
int ret;
ret = nouveau_bo_new(dev, 0, 0, 0, bo);
if (ret)
return ret;
nvbo = nouveau_bo(*bo);
if (!nvdev->mm_enabled) {
nvbo->handle = 0;
nvbo->offset = handle;
nvbo->domain = NOUVEAU_BO_VRAM;
nvbo->flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN;
nvbo->base.offset = nvbo->offset;
nvbo->base.flags = nvbo->flags;
} else {
req.name = handle;
ret = ioctl(nvdev->fd, DRM_IOCTL_GEM_OPEN, &req);
if (ret) {
nouveau_bo_ref(NULL, bo);
return ret;
}
nvbo->size = req.size;
nvbo->handle = req.handle;
}
return 0;
}
static void
nouveau_bo_del_cb(void *priv)
{
struct nouveau_bo_priv *nvbo = priv;
nouveau_bo_kfree(nvbo);
free(nvbo);
}
static void
nouveau_bo_del(struct nouveau_bo **bo)
{
struct nouveau_bo_priv *nvbo;
if (!bo || !*bo)
return;
nvbo = nouveau_bo(*bo);
*bo = NULL;
if (--nvbo->refcount)
return;
if (nvbo->pending) {
nvbo->pending = NULL;
nouveau_pushbuf_flush(nvbo->pending_channel, 0);
}
nouveau_bo_ufree(nvbo);
if (!nouveau_device(nvbo->base.device)->mm_enabled && nvbo->fence)
nouveau_fence_signal_cb(nvbo->fence, nouveau_bo_del_cb, nvbo);
else
nouveau_bo_del_cb(nvbo);
}
int
nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pbo)
{
if (!pbo)
return -EINVAL;
if (ref)
nouveau_bo(ref)->refcount++;
if (*pbo)
nouveau_bo_del(pbo);
*pbo = ref;
return 0;
}
static int
nouveau_bo_wait_nomm(struct nouveau_bo *bo, int cpu_write)
{
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
int ret = 0;
if (cpu_write)
ret = nouveau_fence_wait(&nvbo->fence);
else
ret = nouveau_fence_wait(&nvbo->wr_fence);
if (ret)
return ret;
nvbo->write_marker = 0;
return 0;
}
static int
nouveau_bo_wait(struct nouveau_bo *bo, int cpu_write)
{
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
struct drm_nouveau_gem_cpu_prep req;
int ret;
if (!nvbo->global_handle && !nvbo->write_marker && !cpu_write)
return 0;
if (nvbo->pending &&
(nvbo->pending->write_domains || cpu_write)) {
nvbo->pending = NULL;
nouveau_pushbuf_flush(nvbo->pending_channel, 0);
}
if (!nvdev->mm_enabled)
return nouveau_bo_wait_nomm(bo, cpu_write);
req.handle = nvbo->handle;
ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_PREP,
&req, sizeof(req));
if (ret)
return ret;
nvbo->write_marker = 0;
return 0;
}
int
nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
{
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
int ret;
if (!nvbo || bo->map)
return -EINVAL;
if (!nouveau_bo_allocated(nvbo)) {
if (nvbo->flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
ret = nouveau_bo_kalloc(nvbo, NULL);
if (ret)
return ret;
}
if (!nouveau_bo_allocated(nvbo)) {
ret = nouveau_bo_ualloc(nvbo);
if (ret)
return ret;
}
}
if (nvbo->sysmem) {
bo->map = nvbo->sysmem;
} else {
ret = nouveau_bo_kmap(nvbo);
if (ret)
return ret;
ret = nouveau_bo_wait(bo, (flags & NOUVEAU_BO_WR));
if (ret)
return ret;
bo->map = nvbo->map;
}
return 0;
}
void
nouveau_bo_unmap(struct nouveau_bo *bo)
{
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
if (nvdev->mm_enabled && bo->map && !nvbo->sysmem) {
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct drm_nouveau_gem_cpu_fini req;
req.handle = nvbo->handle;
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_FINI,
&req, sizeof(req));
}
bo->map = NULL;
}
int
nouveau_bo_validate_nomm(struct nouveau_bo_priv *nvbo, uint32_t flags)
{
struct nouveau_bo *new = NULL;
uint32_t t_handle, t_domain, t_offset, t_size;
void *t_map;
int ret;
if ((flags & NOUVEAU_BO_VRAM) && nvbo->domain == NOUVEAU_BO_VRAM)
return 0;
if ((flags & NOUVEAU_BO_GART) && nvbo->domain == NOUVEAU_BO_GART)
return 0;
assert(flags & (NOUVEAU_BO_VRAM|NOUVEAU_BO_GART));
/* Keep tiling info */
flags |= (nvbo->flags & (NOUVEAU_BO_TILED|NOUVEAU_BO_ZTILE));
ret = nouveau_bo_new(nvbo->base.device, flags, 0, nvbo->size, &new);
if (ret)
return ret;
ret = nouveau_bo_kalloc(nouveau_bo(new), NULL);
if (ret) {
nouveau_bo_ref(NULL, &new);
return ret;
}
if (nvbo->handle || nvbo->sysmem) {
nouveau_bo_kmap(nouveau_bo(new));
if (!nvbo->base.map) {
nouveau_bo_map(&nvbo->base, NOUVEAU_BO_RD);
memcpy(nouveau_bo(new)->map, nvbo->base.map, nvbo->base.size);
nouveau_bo_unmap(&nvbo->base);
} else {
memcpy(nouveau_bo(new)->map, nvbo->base.map, nvbo->base.size);
}
}
t_handle = nvbo->handle;
t_domain = nvbo->domain;
t_offset = nvbo->offset;
t_size = nvbo->size;
t_map = nvbo->map;
nvbo->handle = nouveau_bo(new)->handle;
nvbo->domain = nouveau_bo(new)->domain;
nvbo->offset = nouveau_bo(new)->offset;
nvbo->size = nouveau_bo(new)->size;
nvbo->map = nouveau_bo(new)->map;
nouveau_bo(new)->handle = t_handle;
nouveau_bo(new)->domain = t_domain;
nouveau_bo(new)->offset = t_offset;
nouveau_bo(new)->size = t_size;
nouveau_bo(new)->map = t_map;
nouveau_bo_ref(NULL, &new);
return 0;
}
static int
nouveau_bo_pin_nomm(struct nouveau_bo *bo, uint32_t flags)
{
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
int ret;
if (!nvbo->handle) {
if (!(flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)))
return -EINVAL;
ret = nouveau_bo_validate_nomm(nvbo, flags & ~NOUVEAU_BO_PIN);
if (ret)
return ret;
}
nvbo->pinned = 1;
/* Fill in public nouveau_bo members */
bo->flags = nvbo->domain;
bo->offset = nvbo->offset;
return 0;
}
int
nouveau_bo_pin(struct nouveau_bo *bo, uint32_t flags)
{
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
struct drm_nouveau_gem_pin req;
int ret;
if (nvbo->pinned)
return 0;
if (!nvdev->mm_enabled)
return nouveau_bo_pin_nomm(bo, flags);
/* Ensure we have a kernel object... */
if (!nvbo->handle) {
if (!(flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)))
return -EINVAL;
nvbo->flags = flags;
ret = nouveau_bo_kalloc(nvbo, NULL);
if (ret)
return ret;
}
/* Now force it to stay put :) */
req.handle = nvbo->handle;
req.domain = 0;
if (nvbo->flags & NOUVEAU_BO_VRAM)
req.domain |= NOUVEAU_GEM_DOMAIN_VRAM;
if (nvbo->flags & NOUVEAU_BO_GART)
req.domain |= NOUVEAU_GEM_DOMAIN_GART;
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_PIN, &req,
sizeof(struct drm_nouveau_gem_pin));
if (ret)
return ret;
nvbo->offset = req.offset;
nvbo->domain = req.domain;
nvbo->pinned = 1;
nvbo->flags |= NOUVEAU_BO_PIN;
/* Fill in public nouveau_bo members */
if (nvbo->domain & NOUVEAU_GEM_DOMAIN_VRAM)
bo->flags = NOUVEAU_BO_VRAM;
if (nvbo->domain & NOUVEAU_GEM_DOMAIN_GART)
bo->flags = NOUVEAU_BO_GART;
bo->offset = nvbo->offset;
return 0;
}
void
nouveau_bo_unpin(struct nouveau_bo *bo)
{
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
struct drm_nouveau_gem_unpin req;
if (!nvbo->pinned)
return;
if (nvdev->mm_enabled) {
req.handle = nvbo->handle;
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_UNPIN,
&req, sizeof(req));
}
nvbo->pinned = bo->offset = bo->flags = 0;
}
int
nouveau_bo_tile(struct nouveau_bo *bo, uint32_t flags, uint32_t delta,
uint32_t size)
{
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
uint32_t kern_flags = 0;
int ret = 0;
if (flags & NOUVEAU_BO_TILED) {
kern_flags |= NOUVEAU_MEM_TILE;
if (flags & NOUVEAU_BO_ZTILE)
kern_flags |= NOUVEAU_MEM_TILE_ZETA;
}
if (nvdev->mm_enabled) {
struct drm_nouveau_gem_tile req;
req.handle = nvbo->handle;
req.delta = delta;
req.size = size;
req.flags = kern_flags;
ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_TILE,
&req, sizeof(req));
} else {
struct drm_nouveau_mem_tile req;
req.offset = nvbo->offset;
req.delta = delta;
req.size = size;
req.flags = kern_flags;
if (flags & NOUVEAU_BO_VRAM)
req.flags |= NOUVEAU_MEM_FB;
if (flags & NOUVEAU_BO_GART)
req.flags |= NOUVEAU_MEM_AGP;
ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_MEM_TILE,
&req, sizeof(req));
}
return 0;
}
int
nouveau_bo_busy(struct nouveau_bo *bo, uint32_t access)
{
struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
if (!nvdev->mm_enabled) {
struct nouveau_fence *fence;
if (nvbo->pending && (nvbo->pending->write_domains ||
(access & NOUVEAU_BO_WR)))
return 1;
if (access & NOUVEAU_BO_WR)
fence = nvbo->fence;
else
fence = nvbo->wr_fence;
return !nouveau_fence(fence)->signalled;
}
return 1;
}
#include <stdio.h>
struct drm_nouveau_gem_pushbuf_bo *
nouveau_bo_emit_buffer(struct nouveau_channel *chan, struct nouveau_bo *bo)
{
struct nouveau_pushbuf_priv *nvpb = nouveau_pushbuf(chan->pushbuf);
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
struct drm_nouveau_gem_pushbuf_bo *pbbo;
struct nouveau_bo *ref = NULL;
int ret;
if (nvbo->pending)
return nvbo->pending;
if (!nvbo->handle) {
ret = nouveau_bo_kalloc(nvbo, chan);
if (ret)
return NULL;
if (nvbo->sysmem) {
void *sysmem_tmp = nvbo->sysmem;
nvbo->sysmem = NULL;
ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
if (ret)
return NULL;
nvbo->sysmem = sysmem_tmp;
memcpy(bo->map, nvbo->sysmem, nvbo->base.size);
nouveau_bo_unmap(bo);
nouveau_bo_ufree(nvbo);
}
}
if (nvpb->nr_buffers >= NOUVEAU_PUSHBUF_MAX_BUFFERS)
return NULL;
pbbo = nvpb->buffers + nvpb->nr_buffers++;
nvbo->pending = pbbo;
nvbo->pending_channel = chan;
nouveau_bo_ref(bo, &ref);
pbbo->user_priv = (uint64_t)(unsigned long)ref;
pbbo->handle = nvbo->handle;
pbbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART;
pbbo->read_domains = 0;
pbbo->write_domains = 0;
pbbo->presumed_domain = nvbo->domain;
pbbo->presumed_offset = nvbo->offset;
pbbo->presumed_ok = 1;
return pbbo;
}

View File

@ -0,0 +1,97 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_BO_H__
#define __NOUVEAU_BO_H__
/* Relocation/Buffer type flags */
#define NOUVEAU_BO_VRAM (1 << 0)
#define NOUVEAU_BO_GART (1 << 1)
#define NOUVEAU_BO_RD (1 << 2)
#define NOUVEAU_BO_WR (1 << 3)
#define NOUVEAU_BO_RDWR (NOUVEAU_BO_RD | NOUVEAU_BO_WR)
#define NOUVEAU_BO_MAP (1 << 4)
#define NOUVEAU_BO_PIN (1 << 5)
#define NOUVEAU_BO_LOW (1 << 6)
#define NOUVEAU_BO_HIGH (1 << 7)
#define NOUVEAU_BO_OR (1 << 8)
#define NOUVEAU_BO_LOCAL (1 << 9)
#define NOUVEAU_BO_TILED (1 << 10)
#define NOUVEAU_BO_ZTILE (1 << 11)
#define NOUVEAU_BO_DUMMY (1 << 31)
struct nouveau_bo {
struct nouveau_device *device;
uint32_t handle;
uint64_t size;
void *map;
int tiled;
/* Available when buffer is pinned *only* */
uint32_t flags;
uint64_t offset;
};
int
nouveau_bo_new(struct nouveau_device *, uint32_t flags, int align, int size,
struct nouveau_bo **);
int
nouveau_bo_user(struct nouveau_device *, void *ptr, int size,
struct nouveau_bo **);
int
nouveau_bo_fake(struct nouveau_device *dev, uint64_t offset, uint32_t flags,
uint32_t size, void *map, struct nouveau_bo **);
int
nouveau_bo_handle_get(struct nouveau_bo *, uint32_t *);
int
nouveau_bo_handle_ref(struct nouveau_device *, uint32_t handle,
struct nouveau_bo **);
int
nouveau_bo_ref(struct nouveau_bo *, struct nouveau_bo **);
int
nouveau_bo_map(struct nouveau_bo *, uint32_t flags);
void
nouveau_bo_unmap(struct nouveau_bo *);
int
nouveau_bo_pin(struct nouveau_bo *, uint32_t flags);
void
nouveau_bo_unpin(struct nouveau_bo *);
int
nouveau_bo_tile(struct nouveau_bo *, uint32_t flags, uint32_t delta,
uint32_t size);
int
nouveau_bo_busy(struct nouveau_bo *, uint32_t access);
#endif

View File

@ -0,0 +1,167 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "nouveau_private.h"
int
nouveau_channel_alloc(struct nouveau_device *dev, uint32_t fb_ctxdma,
uint32_t tt_ctxdma, struct nouveau_channel **chan)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
struct nouveau_channel_priv *nvchan;
unsigned i;
int ret;
if (!nvdev || !chan || *chan)
return -EINVAL;
nvchan = calloc(1, sizeof(struct nouveau_channel_priv));
if (!nvchan)
return -ENOMEM;
nvchan->base.device = dev;
nvchan->drm.fb_ctxdma_handle = fb_ctxdma;
nvchan->drm.tt_ctxdma_handle = tt_ctxdma;
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_CHANNEL_ALLOC,
&nvchan->drm, sizeof(nvchan->drm));
if (ret) {
free(nvchan);
return ret;
}
nvchan->base.id = nvchan->drm.channel;
if (nouveau_grobj_ref(&nvchan->base, nvchan->drm.fb_ctxdma_handle,
&nvchan->base.vram) ||
nouveau_grobj_ref(&nvchan->base, nvchan->drm.tt_ctxdma_handle,
&nvchan->base.gart)) {
nouveau_channel_free((void *)&nvchan);
return -EINVAL;
}
/* Mark all DRM-assigned subchannels as in-use */
for (i = 0; i < nvchan->drm.nr_subchan; i++) {
struct nouveau_grobj_priv *gr = calloc(1, sizeof(*gr));
gr->base.bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
gr->base.subc = i;
gr->base.handle = nvchan->drm.subchan[i].handle;
gr->base.grclass = nvchan->drm.subchan[i].grclass;
gr->base.channel = &nvchan->base;
nvchan->base.subc[i].gr = &gr->base;
}
ret = drmMap(nvdev->fd, nvchan->drm.notifier, nvchan->drm.notifier_size,
(drmAddressPtr)&nvchan->notifier_block);
if (ret) {
nouveau_channel_free((void *)&nvchan);
return ret;
}
ret = nouveau_grobj_alloc(&nvchan->base, 0x00000000, 0x0030,
&nvchan->base.nullobj);
if (ret) {
nouveau_channel_free((void *)&nvchan);
return ret;
}
if (!nvdev->mm_enabled) {
ret = drmMap(nvdev->fd, nvchan->drm.ctrl, nvchan->drm.ctrl_size,
(void*)&nvchan->user);
if (ret) {
nouveau_channel_free((void *)&nvchan);
return ret;
}
nvchan->put = &nvchan->user[0x40/4];
nvchan->get = &nvchan->user[0x44/4];
nvchan->ref_cnt = &nvchan->user[0x48/4];
ret = drmMap(nvdev->fd, nvchan->drm.cmdbuf,
nvchan->drm.cmdbuf_size, (void*)&nvchan->pushbuf);
if (ret) {
nouveau_channel_free((void *)&nvchan);
return ret;
}
nouveau_dma_channel_init(&nvchan->base);
}
nouveau_pushbuf_init(&nvchan->base);
if (!nvdev->mm_enabled && dev->chipset < 0x10) {
ret = nouveau_grobj_alloc(&nvchan->base, 0xbeef3904, 0x5039,
&nvchan->fence_grobj);
if (ret) {
nouveau_channel_free((void *)&nvchan);
return ret;
}
ret = nouveau_notifier_alloc(&nvchan->base, 0xbeef3905, 1,
&nvchan->fence_ntfy);
if (ret) {
nouveau_channel_free((void *)&nvchan);
return ret;
}
BEGIN_RING(&nvchan->base, nvchan->fence_grobj, 0x0180, 1);
OUT_RING (&nvchan->base, nvchan->fence_ntfy->handle);
nvchan->fence_grobj->bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
}
*chan = &nvchan->base;
return 0;
}
void
nouveau_channel_free(struct nouveau_channel **chan)
{
struct nouveau_channel_priv *nvchan;
struct nouveau_device_priv *nvdev;
struct drm_nouveau_channel_free cf;
if (!chan || !*chan)
return;
nvchan = nouveau_channel(*chan);
*chan = NULL;
nvdev = nouveau_device(nvchan->base.device);
FIRE_RING(&nvchan->base);
if (nvchan->notifier_block)
drmUnmap(nvchan->notifier_block, nvchan->drm.notifier_size);
nouveau_grobj_free(&nvchan->base.vram);
nouveau_grobj_free(&nvchan->base.gart);
nouveau_grobj_free(&nvchan->base.nullobj);
nouveau_grobj_free(&nvchan->fence_grobj);
nouveau_notifier_free(&nvchan->fence_ntfy);
cf.channel = nvchan->drm.channel;
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_CHANNEL_FREE, &cf, sizeof(cf));
free(nvchan);
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_CHANNEL_H__
#define __NOUVEAU_CHANNEL_H__
struct nouveau_subchannel {
struct nouveau_grobj *gr;
unsigned sequence;
};
struct nouveau_channel {
struct nouveau_device *device;
int id;
struct nouveau_pushbuf *pushbuf;
struct nouveau_grobj *nullobj;
struct nouveau_grobj *vram;
struct nouveau_grobj *gart;
void *user_private;
void (*hang_notify)(struct nouveau_channel *);
void (*flush_notify)(struct nouveau_channel *);
struct nouveau_subchannel subc[8];
unsigned subc_sequence;
};
int
nouveau_channel_alloc(struct nouveau_device *, uint32_t fb, uint32_t tt,
struct nouveau_channel **);
void
nouveau_channel_free(struct nouveau_channel **);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,185 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include "nouveau_private.h"
#if NOUVEAU_DRM_HEADER_PATCHLEVEL != 12
#error nouveau_drm.h does not match expected patchlevel, update libdrm.
#endif
int
nouveau_device_open_existing(struct nouveau_device **dev, int close,
int fd, drm_context_t ctx)
{
struct nouveau_device_priv *nvdev;
drmVersionPtr ver;
uint64_t value;
int ret;
if (!dev || *dev)
return -EINVAL;
ver = drmGetVersion(fd);
if (!ver || ver->version_patchlevel != NOUVEAU_DRM_HEADER_PATCHLEVEL)
return -EINVAL;
nvdev = calloc(1, sizeof(*nvdev));
if (!nvdev)
return -ENOMEM;
nvdev->fd = fd;
nvdev->ctx = ctx;
nvdev->needs_close = close;
ret = drmCommandNone(nvdev->fd, DRM_NOUVEAU_CARD_INIT);
if (ret) {
nouveau_device_close((void *)&nvdev);
return ret;
}
ret = nouveau_device_get_param(&nvdev->base,
NOUVEAU_GETPARAM_MM_ENABLED, &value);
if (ret) {
nouveau_device_close((void *)&nvdev);
return ret;
}
nvdev->mm_enabled = value;
ret = nouveau_device_get_param(&nvdev->base,
NOUVEAU_GETPARAM_VM_VRAM_BASE, &value);
if (ret) {
nouveau_device_close((void *)&nvdev);
return ret;
}
nvdev->base.vm_vram_base = value;
ret = nouveau_bo_init(&nvdev->base);
if (ret) {
nouveau_device_close((void *)&nvdev);
return ret;
}
ret = nouveau_device_get_param(&nvdev->base,
NOUVEAU_GETPARAM_CHIPSET_ID, &value);
if (ret) {
nouveau_device_close((void *)&nvdev);
return ret;
}
nvdev->base.chipset = value;
*dev = &nvdev->base;
return 0;
}
int
nouveau_device_open(struct nouveau_device **dev, const char *busid)
{
drm_context_t ctx;
int fd, ret;
if (!dev || *dev)
return -EINVAL;
fd = drmOpen("nouveau", busid);
if (fd < 0)
return -EINVAL;
ret = drmCreateContext(fd, &ctx);
if (ret) {
drmClose(fd);
return ret;
}
ret = nouveau_device_open_existing(dev, 1, fd, ctx);
if (ret) {
drmDestroyContext(fd, ctx);
drmClose(fd);
return ret;
}
return 0;
}
void
nouveau_device_close(struct nouveau_device **dev)
{
struct nouveau_device_priv *nvdev;
if (dev || !*dev)
return;
nvdev = nouveau_device(*dev);
*dev = NULL;
nouveau_bo_takedown(&nvdev->base);
if (nvdev->needs_close) {
drmDestroyContext(nvdev->fd, nvdev->ctx);
drmClose(nvdev->fd);
}
free(nvdev);
}
int
nouveau_device_get_param(struct nouveau_device *dev,
uint64_t param, uint64_t *value)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
struct drm_nouveau_getparam g;
int ret;
if (!nvdev || !value)
return -EINVAL;
g.param = param;
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GETPARAM,
&g, sizeof(g));
if (ret)
return ret;
*value = g.value;
return 0;
}
int
nouveau_device_set_param(struct nouveau_device *dev,
uint64_t param, uint64_t value)
{
struct nouveau_device_priv *nvdev = nouveau_device(dev);
struct drm_nouveau_setparam s;
int ret;
if (!nvdev)
return -EINVAL;
s.param = param;
s.value = value;
ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_SETPARAM,
&s, sizeof(s));
if (ret)
return ret;
return 0;
}

View File

@ -0,0 +1,31 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_DEVICE_H__
#define __NOUVEAU_DEVICE_H__
struct nouveau_device {
unsigned chipset;
uint64_t vm_vram_base;
};
#endif

View File

@ -0,0 +1,215 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdint.h>
#include <assert.h>
#include <errno.h>
#include "nouveau_drmif.h"
#include "nouveau_dma.h"
static inline uint32_t
READ_GET(struct nouveau_channel_priv *nvchan)
{
return *nvchan->get;
}
static inline void
WRITE_PUT(struct nouveau_channel_priv *nvchan, uint32_t val)
{
uint32_t put = ((val << 2) + nvchan->dma->base);
volatile int dum;
NOUVEAU_DMA_BARRIER;
dum = READ_GET(nvchan);
*nvchan->put = put;
nvchan->dma->put = val;
#ifdef NOUVEAU_DMA_TRACE
printf("WRITE_PUT %d/0x%08x\n", nvchan->drm.channel, put);
#endif
NOUVEAU_DMA_BARRIER;
}
static inline int
LOCAL_GET(struct nouveau_dma_priv *dma, uint32_t *val)
{
uint32_t get = *val;
if (get >= dma->base && get <= (dma->base + (dma->max << 2))) {
*val = (get - dma->base) >> 2;
return 1;
}
return 0;
}
void
nouveau_dma_channel_init(struct nouveau_channel *chan)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
int i;
nvchan->dma = &nvchan->struct_dma;
nvchan->dma->base = nvchan->drm.put_base;
nvchan->dma->cur = nvchan->dma->put = 0;
nvchan->dma->max = (nvchan->drm.cmdbuf_size >> 2) - 2;
nvchan->dma->free = nvchan->dma->max - nvchan->dma->cur;
RING_SPACE_CH(chan, RING_SKIPS);
for (i = 0; i < RING_SKIPS; i++)
OUT_RING_CH(chan, 0);
}
#define CHECK_TIMEOUT() do { \
if ((NOUVEAU_TIME_MSEC() - t_start) > NOUVEAU_DMA_TIMEOUT) \
return - EBUSY; \
} while(0)
int
nouveau_dma_wait(struct nouveau_channel *chan, int size)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_dma_priv *dma = nvchan->dma;
uint32_t get, t_start;
FIRE_RING_CH(chan);
t_start = NOUVEAU_TIME_MSEC();
while (dma->free < size) {
CHECK_TIMEOUT();
get = READ_GET(nvchan);
if (!LOCAL_GET(dma, &get))
continue;
if (dma->put >= get) {
dma->free = dma->max - dma->cur;
if (dma->free < size) {
#ifdef NOUVEAU_DMA_DEBUG
dma->push_free = 1;
#endif
OUT_RING_CH(chan, 0x20000000 | dma->base);
if (get <= RING_SKIPS) {
/*corner case - will be idle*/
if (dma->put <= RING_SKIPS)
WRITE_PUT(nvchan,
RING_SKIPS + 1);
do {
CHECK_TIMEOUT();
get = READ_GET(nvchan);
if (!LOCAL_GET(dma, &get))
get = 0;
} while (get <= RING_SKIPS);
}
WRITE_PUT(nvchan, RING_SKIPS);
dma->cur = dma->put = RING_SKIPS;
dma->free = get - (RING_SKIPS + 1);
}
} else {
dma->free = get - dma->cur - 1;
}
}
return 0;
}
#ifdef NOUVEAU_DMA_DUMP_POSTRELOC_PUSHBUF
static void
nouveau_dma_parse_pushbuf(struct nouveau_channel *chan, int get, int put)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
unsigned mthd_count = 0;
while (get != put) {
uint32_t gpuget = (get << 2) + nvchan->drm.put_base;
uint32_t data;
if (get < 0 || get >= nvchan->drm.cmdbuf_size)
assert(0);
data = nvchan->pushbuf[get++];
if (mthd_count) {
printf("0x%08x 0x%08x\n", gpuget, data);
mthd_count--;
continue;
}
switch (data & 0x60000000) {
case 0x00000000:
mthd_count = (data >> 18) & 0x7ff;
printf("0x%08x 0x%08x MTHD "
"Sc %d Mthd 0x%04x Size %d\n",
gpuget, data, (data>>13) & 7, data & 0x1ffc,
mthd_count);
break;
case 0x20000000:
get = (data & 0x1ffffffc) >> 2;
printf("0x%08x 0x%08x JUMP 0x%08x\n",
gpuget, data, data & 0x1ffffffc);
continue;
case 0x40000000:
mthd_count = (data >> 18) & 0x7ff;
printf("0x%08x 0x%08x NINC "
"Sc %d Mthd 0x%04x Size %d\n",
gpuget, data, (data>>13) & 7, data & 0x1ffc,
mthd_count);
break;
case 0x60000000:
/* DMA_OPCODE_CALL apparently, doesn't seem to work on
* my NV40 at least..
*/
/* fall-through */
default:
printf("DMA_PUSHER 0x%08x 0x%08x\n", gpuget, data);
assert(0);
}
}
}
#endif
void
nouveau_dma_kickoff(struct nouveau_channel *chan)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_dma_priv *dma = nvchan->dma;
if (dma->cur == dma->put)
return;
#ifdef NOUVEAU_DMA_DEBUG
if (dma->push_free) {
printf("Packet incomplete: %d left\n", dma->push_free);
return;
}
#endif
#ifdef NOUVEAU_DMA_DUMP_POSTRELOC_PUSHBUF
nouveau_dma_parse_pushbuf(chan, dma->put, dma->cur);
#endif
WRITE_PUT(nvchan, dma->cur);
}

View File

@ -0,0 +1,154 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_DMA_H__
#define __NOUVEAU_DMA_H__
#include <string.h>
#include "nouveau_private.h"
//#define NOUVEAU_DMA_DEBUG
//#define NOUVEAU_DMA_TRACE
//#define NOUVEAU_DMA_DUMP_POSTRELOC_PUSHBUF
#if defined(__amd64__)
#define NOUVEAU_DMA_BARRIER asm volatile("lock; addl $0,0(%%rsp)" ::: "memory")
#elif defined(__i386__)
#define NOUVEAU_DMA_BARRIER asm volatile("lock; addl $0,0(%%esp)" ::: "memory")
#else
#define NOUVEAU_DMA_BARRIER
#endif
#define NOUVEAU_DMA_TIMEOUT 2000
#define NOUVEAU_TIME_MSEC() 0
#define RING_SKIPS 8
extern int nouveau_dma_wait(struct nouveau_channel *chan, int size);
extern void nouveau_dma_subc_bind(struct nouveau_grobj *);
extern void nouveau_dma_channel_init(struct nouveau_channel *);
extern void nouveau_dma_kickoff(struct nouveau_channel *);
#ifdef NOUVEAU_DMA_DEBUG
static char faulty[1024];
#endif
static inline void
nouveau_dma_out(struct nouveau_channel *chan, uint32_t data)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_dma_priv *dma = nvchan->dma;
#ifdef NOUVEAU_DMA_DEBUG
if (dma->push_free == 0) {
printf("No space left in packet at %s\n", faulty);
return;
}
dma->push_free--;
#endif
#ifdef NOUVEAU_DMA_TRACE
{
uint32_t offset = (dma->cur << 2) + dma->base;
printf("\tOUT_RING %d/0x%08x -> 0x%08x\n",
nvchan->drm.channel, offset, data);
}
#endif
nvchan->pushbuf[dma->cur + (dma->base - nvchan->drm.put_base)/4] = data;
dma->cur++;
}
static inline void
nouveau_dma_outp(struct nouveau_channel *chan, uint32_t *ptr, int size)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_dma_priv *dma = nvchan->dma;
(void)dma;
#ifdef NOUVEAU_DMA_DEBUG
if (dma->push_free < size) {
printf("Packet too small. Free=%d, Need=%d\n",
dma->push_free, size);
return;
}
#endif
#ifdef NOUVEAU_DMA_TRACE
while (size--) {
nouveau_dma_out(chan, *ptr);
ptr++;
}
#else
memcpy(&nvchan->pushbuf[dma->cur], ptr, size << 2);
#ifdef NOUVEAU_DMA_DEBUG
dma->push_free -= size;
#endif
dma->cur += size;
#endif
}
static inline void
nouveau_dma_space(struct nouveau_channel *chan, unsigned size)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_dma_priv *dma = nvchan->dma;
if (dma->free < size) {
if (nouveau_dma_wait(chan, size) && chan->hang_notify)
chan->hang_notify(chan);
}
dma->free -= size;
#ifdef NOUVEAU_DMA_DEBUG
dma->push_free = size;
#endif
}
static inline void
nouveau_dma_begin(struct nouveau_channel *chan, struct nouveau_grobj *grobj,
int method, int size, const char* file, int line)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_dma_priv *dma = nvchan->dma;
(void)dma;
#ifdef NOUVEAU_DMA_TRACE
printf("BEGIN_RING %d/%08x/%d/0x%04x/%d\n", nvchan->drm.channel,
grobj->handle, grobj->subc, method, size);
#endif
#ifdef NOUVEAU_DMA_DEBUG
if (dma->push_free) {
printf("Previous packet incomplete: %d left at %s\n",
dma->push_free, faulty);
return;
}
sprintf(faulty,"%s:%d",file,line);
#endif
nouveau_dma_space(chan, (size + 1));
nouveau_dma_out(chan, (size << 18) | (grobj->subc << 13) | method);
}
#define RING_SPACE_CH(ch,sz) nouveau_dma_space((ch), (sz))
#define BEGIN_RING_CH(ch,gr,m,sz) nouveau_dma_begin((ch), (gr), (m), (sz), __FUNCTION__, __LINE__ )
#define OUT_RING_CH(ch, data) nouveau_dma_out((ch), (data))
#define OUT_RINGp_CH(ch,ptr,dwords) nouveau_dma_outp((ch), (void*)(ptr), \
(dwords))
#define FIRE_RING_CH(ch) nouveau_dma_kickoff((ch))
#define WAIT_RING_CH(ch,sz) nouveau_dma_wait((ch), (sz))
#endif

View File

@ -0,0 +1,59 @@
/*
* Copyright 2008 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_DRMIF_H__
#define __NOUVEAU_DRMIF_H__
#include <stdint.h>
#include <xf86drm.h>
#include "nouveau_device.h"
struct nouveau_device_priv {
struct nouveau_device base;
int fd;
drm_context_t ctx;
drmLock *lock;
int needs_close;
int mm_enabled;
};
#define nouveau_device(n) ((struct nouveau_device_priv *)(n))
int
nouveau_device_open_existing(struct nouveau_device **, int close,
int fd, drm_context_t ctx);
int
nouveau_device_open(struct nouveau_device **, const char *busid);
void
nouveau_device_close(struct nouveau_device **);
int
nouveau_device_get_param(struct nouveau_device *, uint64_t param, uint64_t *v);
int
nouveau_device_set_param(struct nouveau_device *, uint64_t param, uint64_t val);
#endif

View File

@ -0,0 +1,249 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include "nouveau_private.h"
#include "nouveau_dma.h"
static void
nouveau_fence_del_unsignalled(struct nouveau_fence *fence)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(fence->channel);
struct nouveau_fence *le;
if (nvchan->fence_head == fence) {
nvchan->fence_head = nouveau_fence(fence)->next;
if (nvchan->fence_head == NULL)
nvchan->fence_tail = NULL;
return;
}
le = nvchan->fence_head;
while (le && nouveau_fence(le)->next != fence)
le = nouveau_fence(le)->next;
assert(le && nouveau_fence(le)->next == fence);
nouveau_fence(le)->next = nouveau_fence(fence)->next;
if (nvchan->fence_tail == fence)
nvchan->fence_tail = le;
}
static void
nouveau_fence_del(struct nouveau_fence **fence)
{
struct nouveau_fence_priv *nvfence;
if (!fence || !*fence)
return;
nvfence = nouveau_fence(*fence);
*fence = NULL;
if (--nvfence->refcount)
return;
if (nvfence->emitted && !nvfence->signalled) {
if (nvfence->signal_cb) {
nvfence->refcount++;
nouveau_fence_wait((void *)&nvfence);
return;
}
nouveau_fence_del_unsignalled(&nvfence->base);
}
free(nvfence);
}
int
nouveau_fence_new(struct nouveau_channel *chan, struct nouveau_fence **fence)
{
struct nouveau_fence_priv *nvfence;
if (!chan || !fence || *fence)
return -EINVAL;
nvfence = calloc(1, sizeof(struct nouveau_fence_priv));
if (!nvfence)
return -ENOMEM;
nvfence->base.channel = chan;
nvfence->refcount = 1;
*fence = &nvfence->base;
return 0;
}
int
nouveau_fence_ref(struct nouveau_fence *ref, struct nouveau_fence **fence)
{
struct nouveau_fence_priv *nvfence;
if (!fence)
return -EINVAL;
if (*fence) {
nouveau_fence_del(fence);
*fence = NULL;
}
if (ref) {
nvfence = nouveau_fence(ref);
nvfence->refcount++;
*fence = &nvfence->base;
}
return 0;
}
int
nouveau_fence_signal_cb(struct nouveau_fence *fence, void (*func)(void *),
void *priv)
{
struct nouveau_fence_priv *nvfence = nouveau_fence(fence);
struct nouveau_fence_cb *cb;
if (!nvfence || !func)
return -EINVAL;
cb = malloc(sizeof(struct nouveau_fence_cb));
if (!cb)
return -ENOMEM;
cb->func = func;
cb->priv = priv;
cb->next = nvfence->signal_cb;
nvfence->signal_cb = cb;
return 0;
}
void
nouveau_fence_emit(struct nouveau_fence *fence)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(fence->channel);
struct nouveau_fence_priv *nvfence = nouveau_fence(fence);
nvfence->emitted = 1;
nvfence->sequence = ++nvchan->fence_sequence;
if (nvfence->sequence == 0xffffffff)
printf("AII wrap unhandled\n");
if (!nvchan->fence_ntfy) {
/*XXX: assumes subc 0 is populated */
nouveau_dma_space(fence->channel, 2);
nouveau_dma_out (fence->channel, 0x00040050);
nouveau_dma_out (fence->channel, nvfence->sequence);
}
nouveau_dma_kickoff(fence->channel);
if (nvchan->fence_tail) {
nouveau_fence(nvchan->fence_tail)->next = fence;
} else {
nvchan->fence_head = fence;
}
nvchan->fence_tail = fence;
}
static void
nouveau_fence_flush_seq(struct nouveau_channel *chan, uint32_t sequence)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
while (nvchan->fence_head) {
struct nouveau_fence_priv *nvfence;
nvfence = nouveau_fence(nvchan->fence_head);
if (nvfence->sequence > sequence)
break;
nouveau_fence_del_unsignalled(&nvfence->base);
nvfence->signalled = 1;
if (nvfence->signal_cb) {
struct nouveau_fence *fence = NULL;
nouveau_fence_ref(&nvfence->base, &fence);
while (nvfence->signal_cb) {
struct nouveau_fence_cb *cb;
cb = nvfence->signal_cb;
nvfence->signal_cb = cb->next;
cb->func(cb->priv);
free(cb);
}
nouveau_fence_ref(NULL, &fence);
}
}
}
void
nouveau_fence_flush(struct nouveau_channel *chan)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
if (!nvchan->fence_ntfy)
nouveau_fence_flush_seq(chan, *nvchan->ref_cnt);
}
int
nouveau_fence_wait(struct nouveau_fence **fence)
{
struct nouveau_fence_priv *nvfence;
struct nouveau_channel_priv *nvchan;
if (!fence)
return -EINVAL;
nvfence = nouveau_fence(*fence);
if (!nvfence)
return 0;
nvchan = nouveau_channel(nvfence->base.channel);
if (nvfence->emitted) {
if (!nvfence->signalled && nvchan->fence_ntfy) {
struct nouveau_channel *chan = &nvchan->base;
int ret;
/*XXX: NV04/NV05: Full sync + flush all fences */
nouveau_notifier_reset(nvchan->fence_ntfy, 0);
BEGIN_RING(chan, nvchan->fence_grobj, 0x0104, 1);
OUT_RING (chan, 0);
BEGIN_RING(chan, nvchan->fence_grobj, 0x0100, 1);
OUT_RING (chan, 0);
FIRE_RING (chan);
ret = nouveau_notifier_wait_status(nvchan->fence_ntfy,
0, 0, 2.0);
if (ret)
return ret;
nouveau_fence_flush_seq(chan, nvchan->fence_sequence);
}
while (!nvfence->signalled)
nouveau_fence_flush(nvfence->base.channel);
}
nouveau_fence_ref(NULL, fence);
return 0;
}

View File

@ -0,0 +1,138 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <errno.h>
#include "nouveau_private.h"
int
nouveau_grobj_alloc(struct nouveau_channel *chan, uint32_t handle,
int class, struct nouveau_grobj **grobj)
{
struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
struct nouveau_grobj_priv *nvgrobj;
struct drm_nouveau_grobj_alloc g;
int ret;
if (!nvdev || !grobj || *grobj)
return -EINVAL;
nvgrobj = calloc(1, sizeof(*nvgrobj));
if (!nvgrobj)
return -ENOMEM;
nvgrobj->base.channel = chan;
nvgrobj->base.handle = handle;
nvgrobj->base.grclass = class;
nvgrobj->base.bound = NOUVEAU_GROBJ_UNBOUND;
nvgrobj->base.subc = -1;
g.channel = chan->id;
g.handle = handle;
g.class = class;
ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GROBJ_ALLOC,
&g, sizeof(g));
if (ret) {
nouveau_grobj_free((void *)&nvgrobj);
return ret;
}
*grobj = &nvgrobj->base;
return 0;
}
int
nouveau_grobj_ref(struct nouveau_channel *chan, uint32_t handle,
struct nouveau_grobj **grobj)
{
struct nouveau_grobj_priv *nvgrobj;
if (!chan || !grobj || *grobj)
return -EINVAL;
nvgrobj = calloc(1, sizeof(struct nouveau_grobj_priv));
if (!nvgrobj)
return -ENOMEM;
nvgrobj->base.channel = chan;
nvgrobj->base.handle = handle;
nvgrobj->base.grclass = 0;
*grobj = &nvgrobj->base;
return 0;
}
void
nouveau_grobj_free(struct nouveau_grobj **grobj)
{
struct nouveau_device_priv *nvdev;
struct nouveau_channel_priv *chan;
struct nouveau_grobj_priv *nvgrobj;
if (!grobj || !*grobj)
return;
nvgrobj = nouveau_grobj(*grobj);
*grobj = NULL;
chan = nouveau_channel(nvgrobj->base.channel);
nvdev = nouveau_device(chan->base.device);
if (nvgrobj->base.grclass) {
struct drm_nouveau_gpuobj_free f;
f.channel = chan->drm.channel;
f.handle = nvgrobj->base.handle;
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GPUOBJ_FREE,
&f, sizeof(f));
}
free(nvgrobj);
}
void
nouveau_grobj_autobind(struct nouveau_grobj *grobj)
{
struct nouveau_subchannel *subc = NULL;
int i;
for (i = 0; i < 8; i++) {
struct nouveau_subchannel *scc = &grobj->channel->subc[i];
if (scc->gr && scc->gr->bound == NOUVEAU_GROBJ_BOUND_EXPLICIT)
continue;
if (!subc || scc->sequence < subc->sequence)
subc = scc;
}
if (subc->gr) {
subc->gr->bound = NOUVEAU_GROBJ_UNBOUND;
subc->gr->subc = -1;
}
subc->gr = grobj;
subc->gr->bound = NOUVEAU_GROBJ_BOUND;
subc->gr->subc = subc - &grobj->channel->subc[0];
BEGIN_RING(grobj->channel, grobj, 0x0000, 1);
OUT_RING (grobj->channel, grobj->handle);
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_GROBJ_H__
#define __NOUVEAU_GROBJ_H__
#include "nouveau_channel.h"
struct nouveau_grobj {
struct nouveau_channel *channel;
int grclass;
uint32_t handle;
enum {
NOUVEAU_GROBJ_UNBOUND = 0,
NOUVEAU_GROBJ_BOUND = 1,
NOUVEAU_GROBJ_BOUND_EXPLICIT = 2
} bound;
int subc;
};
int nouveau_grobj_alloc(struct nouveau_channel *, uint32_t handle,
int class, struct nouveau_grobj **);
int nouveau_grobj_ref(struct nouveau_channel *, uint32_t handle,
struct nouveau_grobj **);
void nouveau_grobj_free(struct nouveau_grobj **);
void nouveau_grobj_autobind(struct nouveau_grobj *);
#endif

View File

@ -0,0 +1,146 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include "nouveau_private.h"
#define NOTIFIER(__v) \
struct nouveau_notifier_priv *nvnotify = nouveau_notifier(notifier); \
volatile uint32_t *__v = (void*)nvnotify->map + (id * 32)
int
nouveau_notifier_alloc(struct nouveau_channel *chan, uint32_t handle,
int count, struct nouveau_notifier **notifier)
{
struct nouveau_notifier_priv *nvnotify;
int ret;
if (!chan || !notifier || *notifier)
return -EINVAL;
nvnotify = calloc(1, sizeof(struct nouveau_notifier_priv));
if (!nvnotify)
return -ENOMEM;
nvnotify->base.channel = chan;
nvnotify->base.handle = handle;
nvnotify->drm.channel = chan->id;
nvnotify->drm.handle = handle;
nvnotify->drm.count = count;
if ((ret = drmCommandWriteRead(nouveau_device(chan->device)->fd,
DRM_NOUVEAU_NOTIFIEROBJ_ALLOC,
&nvnotify->drm,
sizeof(nvnotify->drm)))) {
nouveau_notifier_free((void *)&nvnotify);
return ret;
}
nvnotify->map = (void *)nouveau_channel(chan)->notifier_block +
nvnotify->drm.offset;
*notifier = &nvnotify->base;
return 0;
}
void
nouveau_notifier_free(struct nouveau_notifier **notifier)
{
struct nouveau_notifier_priv *nvnotify;
struct nouveau_channel_priv *nvchan;
struct nouveau_device_priv *nvdev;
struct drm_nouveau_gpuobj_free f;
if (!notifier || !*notifier)
return;
nvnotify = nouveau_notifier(*notifier);
*notifier = NULL;
nvchan = nouveau_channel(nvnotify->base.channel);
nvdev = nouveau_device(nvchan->base.device);
f.channel = nvchan->drm.channel;
f.handle = nvnotify->base.handle;
drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GPUOBJ_FREE, &f, sizeof(f));
free(nvnotify);
}
void
nouveau_notifier_reset(struct nouveau_notifier *notifier, int id)
{
NOTIFIER(n);
n[NV_NOTIFY_TIME_0 /4] = 0x00000000;
n[NV_NOTIFY_TIME_1 /4] = 0x00000000;
n[NV_NOTIFY_RETURN_VALUE/4] = 0x00000000;
n[NV_NOTIFY_STATE /4] = (NV_NOTIFY_STATE_STATUS_IN_PROCESS <<
NV_NOTIFY_STATE_STATUS_SHIFT);
}
uint32_t
nouveau_notifier_status(struct nouveau_notifier *notifier, int id)
{
NOTIFIER(n);
return n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT;
}
uint32_t
nouveau_notifier_return_val(struct nouveau_notifier *notifier, int id)
{
NOTIFIER(n);
return n[NV_NOTIFY_RETURN_VALUE/4];
}
static inline double
gettime(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + tv.tv_usec / 1000000.0;
}
int
nouveau_notifier_wait_status(struct nouveau_notifier *notifier, int id,
int status, double timeout)
{
NOTIFIER(n);
double time = 0, t_start = gettime();
while (time <= timeout) {
uint32_t v;
v = n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT;
if (v == status)
return 0;
if (timeout)
time = gettime() - t_start;
}
return -EBUSY;
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_NOTIFIER_H__
#define __NOUVEAU_NOTIFIER_H__
#define NV_NOTIFIER_SIZE 32
#define NV_NOTIFY_TIME_0 0x00000000
#define NV_NOTIFY_TIME_1 0x00000004
#define NV_NOTIFY_RETURN_VALUE 0x00000008
#define NV_NOTIFY_STATE 0x0000000C
#define NV_NOTIFY_STATE_STATUS_MASK 0xFF000000
#define NV_NOTIFY_STATE_STATUS_SHIFT 24
#define NV_NOTIFY_STATE_STATUS_COMPLETED 0x00
#define NV_NOTIFY_STATE_STATUS_IN_PROCESS 0x01
#define NV_NOTIFY_STATE_ERROR_CODE_MASK 0x0000FFFF
#define NV_NOTIFY_STATE_ERROR_CODE_SHIFT 0
struct nouveau_notifier {
struct nouveau_channel *channel;
uint32_t handle;
};
int
nouveau_notifier_alloc(struct nouveau_channel *, uint32_t handle, int count,
struct nouveau_notifier **);
void
nouveau_notifier_free(struct nouveau_notifier **);
void
nouveau_notifier_reset(struct nouveau_notifier *, int id);
uint32_t
nouveau_notifier_status(struct nouveau_notifier *, int id);
uint32_t
nouveau_notifier_return_val(struct nouveau_notifier *, int id);
int
nouveau_notifier_wait_status(struct nouveau_notifier *, int id, int status,
double timeout);
#endif

View File

@ -0,0 +1,203 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_PRIVATE_H__
#define __NOUVEAU_PRIVATE_H__
#include <stdint.h>
#include <xf86drm.h>
#include <nouveau_drm.h>
#include "nouveau_drmif.h"
#include "nouveau_device.h"
#include "nouveau_channel.h"
#include "nouveau_grobj.h"
#include "nouveau_notifier.h"
#include "nouveau_bo.h"
#include "nouveau_resource.h"
#include "nouveau_pushbuf.h"
#define NOUVEAU_PUSHBUF_MAX_BUFFERS 1024
#define NOUVEAU_PUSHBUF_MAX_RELOCS 1024
struct nouveau_pushbuf_priv {
struct nouveau_pushbuf base;
int use_cal;
struct nouveau_bo *buffer;
unsigned *pushbuf;
unsigned size;
struct drm_nouveau_gem_pushbuf_bo *buffers;
unsigned nr_buffers;
struct drm_nouveau_gem_pushbuf_reloc *relocs;
unsigned nr_relocs;
/*XXX: nomm */
struct nouveau_fence *fence;
};
#define nouveau_pushbuf(n) ((struct nouveau_pushbuf_priv *)(n))
#define pbbo_to_ptr(o) ((uint64_t)(unsigned long)(o))
#define ptr_to_pbbo(h) ((struct nouveau_pushbuf_bo *)(unsigned long)(h))
#define pbrel_to_ptr(o) ((uint64_t)(unsigned long)(o))
#define ptr_to_pbrel(h) ((struct nouveau_pushbuf_reloc *)(unsigned long)(h))
#define bo_to_ptr(o) ((uint64_t)(unsigned long)(o))
#define ptr_to_bo(h) ((struct nouveau_bo_priv *)(unsigned long)(h))
int
nouveau_pushbuf_init(struct nouveau_channel *);
struct nouveau_dma_priv {
uint32_t base;
uint32_t max;
uint32_t cur;
uint32_t put;
uint32_t free;
int push_free;
} dma;
struct nouveau_channel_priv {
struct nouveau_channel base;
struct drm_nouveau_channel_alloc drm;
void *notifier_block;
struct nouveau_pushbuf_priv pb;
/*XXX: nomm */
volatile uint32_t *user, *put, *get, *ref_cnt;
uint32_t *pushbuf;
struct nouveau_dma_priv struct_dma;
struct nouveau_dma_priv *dma;
struct nouveau_fence *fence_head;
struct nouveau_fence *fence_tail;
uint32_t fence_sequence;
struct nouveau_grobj *fence_grobj;
struct nouveau_notifier *fence_ntfy;
};
#define nouveau_channel(n) ((struct nouveau_channel_priv *)(n))
struct nouveau_fence {
struct nouveau_channel *channel;
};
struct nouveau_fence_cb {
struct nouveau_fence_cb *next;
void (*func)(void *);
void *priv;
};
struct nouveau_fence_priv {
struct nouveau_fence base;
int refcount;
struct nouveau_fence *next;
struct nouveau_fence_cb *signal_cb;
uint32_t sequence;
int emitted;
int signalled;
};
#define nouveau_fence(n) ((struct nouveau_fence_priv *)(n))
int
nouveau_fence_new(struct nouveau_channel *, struct nouveau_fence **);
int
nouveau_fence_ref(struct nouveau_fence *, struct nouveau_fence **);
int
nouveau_fence_signal_cb(struct nouveau_fence *, void (*)(void *), void *);
void
nouveau_fence_emit(struct nouveau_fence *);
int
nouveau_fence_wait(struct nouveau_fence **);
void
nouveau_fence_flush(struct nouveau_channel *);
struct nouveau_grobj_priv {
struct nouveau_grobj base;
};
#define nouveau_grobj(n) ((struct nouveau_grobj_priv *)(n))
struct nouveau_notifier_priv {
struct nouveau_notifier base;
struct drm_nouveau_notifierobj_alloc drm;
volatile void *map;
};
#define nouveau_notifier(n) ((struct nouveau_notifier_priv *)(n))
struct nouveau_bo_priv {
struct nouveau_bo base;
int refcount;
/* Buffer configuration + usage hints */
unsigned flags;
unsigned size;
unsigned align;
int user;
/* Tracking */
struct drm_nouveau_gem_pushbuf_bo *pending;
struct nouveau_channel *pending_channel;
int write_marker;
/* Userspace object */
void *sysmem;
/* Kernel object */
uint32_t global_handle;
unsigned handle;
void *map;
/* Last known information from kernel on buffer status */
int pinned;
uint64_t offset;
uint32_t domain;
/*XXX: nomm stuff */
struct nouveau_fence *fence;
struct nouveau_fence *wr_fence;
};
#define nouveau_bo(n) ((struct nouveau_bo_priv *)(n))
int
nouveau_bo_init(struct nouveau_device *);
void
nouveau_bo_takedown(struct nouveau_device *);
struct drm_nouveau_gem_pushbuf_bo *
nouveau_bo_emit_buffer(struct nouveau_channel *, struct nouveau_bo *);
int
nouveau_bo_validate_nomm(struct nouveau_bo_priv *, uint32_t);
#include "nouveau_dma.h"
#endif

View File

@ -0,0 +1,276 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include "nouveau_private.h"
#include "nouveau_dma.h"
#define PB_BUFMGR_DWORDS (4096 / 2)
#define PB_MIN_USER_DWORDS 2048
static uint32_t
nouveau_pushbuf_calc_reloc(struct drm_nouveau_gem_pushbuf_bo *pbbo,
struct drm_nouveau_gem_pushbuf_reloc *r,
int mm_enabled)
{
uint32_t push = 0;
const unsigned is_vram = mm_enabled ? NOUVEAU_GEM_DOMAIN_VRAM :
NOUVEAU_BO_VRAM;
if (r->flags & NOUVEAU_GEM_RELOC_LOW)
push = (pbbo->presumed_offset + r->data);
else
if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
push = (pbbo->presumed_offset + r->data) >> 32;
else
push = r->data;
if (r->flags & NOUVEAU_GEM_RELOC_OR) {
if (pbbo->presumed_domain & is_vram)
push |= r->vor;
else
push |= r->tor;
}
return push;
}
int
nouveau_pushbuf_emit_reloc(struct nouveau_channel *chan, void *ptr,
struct nouveau_bo *bo, uint32_t data, uint32_t flags,
uint32_t vor, uint32_t tor)
{
struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
struct nouveau_pushbuf_priv *nvpb = nouveau_pushbuf(chan->pushbuf);
struct drm_nouveau_gem_pushbuf_reloc *r;
struct drm_nouveau_gem_pushbuf_bo *pbbo;
uint32_t domains = 0;
if (nvpb->nr_relocs >= NOUVEAU_PUSHBUF_MAX_RELOCS)
return -ENOMEM;
if (nouveau_bo(bo)->user && (flags & NOUVEAU_BO_WR)) {
fprintf(stderr, "write to user buffer!!\n");
return -EINVAL;
}
pbbo = nouveau_bo_emit_buffer(chan, bo);
if (!pbbo)
return -ENOMEM;
if (flags & NOUVEAU_BO_VRAM)
domains |= NOUVEAU_GEM_DOMAIN_VRAM;
if (flags & NOUVEAU_BO_GART)
domains |= NOUVEAU_GEM_DOMAIN_GART;
pbbo->valid_domains &= domains;
assert(pbbo->valid_domains);
if (!nvdev->mm_enabled) {
struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
nouveau_fence_ref(nvpb->fence, &nvbo->fence);
if (flags & NOUVEAU_BO_WR)
nouveau_fence_ref(nvpb->fence, &nvbo->wr_fence);
}
assert(flags & NOUVEAU_BO_RDWR);
if (flags & NOUVEAU_BO_RD) {
pbbo->read_domains |= domains;
}
if (flags & NOUVEAU_BO_WR) {
pbbo->write_domains |= domains;
nouveau_bo(bo)->write_marker = 1;
}
r = nvpb->relocs + nvpb->nr_relocs++;
r->bo_index = pbbo - nvpb->buffers;
r->reloc_index = (uint32_t *)ptr - nvpb->pushbuf;
r->flags = 0;
if (flags & NOUVEAU_BO_LOW)
r->flags |= NOUVEAU_GEM_RELOC_LOW;
if (flags & NOUVEAU_BO_HIGH)
r->flags |= NOUVEAU_GEM_RELOC_HIGH;
if (flags & NOUVEAU_BO_OR)
r->flags |= NOUVEAU_GEM_RELOC_OR;
r->data = data;
r->vor = vor;
r->tor = tor;
*(uint32_t *)ptr = (flags & NOUVEAU_BO_DUMMY) ? 0 :
nouveau_pushbuf_calc_reloc(pbbo, r, nvdev->mm_enabled);
return 0;
}
static int
nouveau_pushbuf_space(struct nouveau_channel *chan, unsigned min)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
if (nvpb->pushbuf) {
free(nvpb->pushbuf);
nvpb->pushbuf = NULL;
}
nvpb->size = min < PB_MIN_USER_DWORDS ? PB_MIN_USER_DWORDS : min;
nvpb->pushbuf = malloc(sizeof(uint32_t) * nvpb->size);
nvpb->base.channel = chan;
nvpb->base.remaining = nvpb->size;
nvpb->base.cur = nvpb->pushbuf;
if (!nouveau_device(chan->device)->mm_enabled) {
nouveau_fence_ref(NULL, &nvpb->fence);
nouveau_fence_new(chan, &nvpb->fence);
}
return 0;
}
int
nouveau_pushbuf_init(struct nouveau_channel *chan)
{
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
nouveau_pushbuf_space(chan, 0);
nvpb->buffers = calloc(NOUVEAU_PUSHBUF_MAX_BUFFERS,
sizeof(struct drm_nouveau_gem_pushbuf_bo));
nvpb->relocs = calloc(NOUVEAU_PUSHBUF_MAX_RELOCS,
sizeof(struct drm_nouveau_gem_pushbuf_reloc));
chan->pushbuf = &nvpb->base;
return 0;
}
static int
nouveau_pushbuf_flush_nomm(struct nouveau_channel_priv *nvchan)
{
struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
struct drm_nouveau_gem_pushbuf_bo *bo = nvpb->buffers;
struct drm_nouveau_gem_pushbuf_reloc *reloc = nvpb->relocs;
unsigned b, r;
int ret;
for (b = 0; b < nvpb->nr_buffers; b++) {
struct nouveau_bo_priv *nvbo =
(void *)(unsigned long)bo[b].user_priv;
uint32_t flags = 0;
if (bo[b].valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
flags |= NOUVEAU_BO_VRAM;
if (bo[b].valid_domains & NOUVEAU_GEM_DOMAIN_GART)
flags |= NOUVEAU_BO_GART;
ret = nouveau_bo_validate_nomm(nvbo, flags);
if (ret)
return ret;
if (1 || bo[b].presumed_domain != nvbo->domain ||
bo[b].presumed_offset != nvbo->offset) {
bo[b].presumed_ok = 0;
bo[b].presumed_domain = nvbo->domain;
bo[b].presumed_offset = nvbo->offset;
}
}
for (r = 0; r < nvpb->nr_relocs; r++, reloc++) {
uint32_t push;
if (bo[reloc->bo_index].presumed_ok)
continue;
push = nouveau_pushbuf_calc_reloc(&bo[reloc->bo_index], reloc, 0);
nvpb->pushbuf[reloc->reloc_index] = push;
}
nouveau_dma_space(&nvchan->base, nvpb->size);
nouveau_dma_outp (&nvchan->base, nvpb->pushbuf, nvpb->size);
nouveau_fence_emit(nvpb->fence);
return 0;
}
int
nouveau_pushbuf_flush(struct nouveau_channel *chan, unsigned min)
{
struct nouveau_device_priv *nvdev = nouveau_device(chan->device);
struct nouveau_channel_priv *nvchan = nouveau_channel(chan);
struct nouveau_pushbuf_priv *nvpb = &nvchan->pb;
struct drm_nouveau_gem_pushbuf req;
unsigned i;
int ret;
if (nvpb->base.remaining == nvpb->size)
return 0;
nvpb->size -= nvpb->base.remaining;
if (nvdev->mm_enabled) {
req.channel = chan->id;
req.nr_dwords = nvpb->size;
req.dwords = (uint64_t)(unsigned long)nvpb->pushbuf;
req.nr_buffers = nvpb->nr_buffers;
req.buffers = (uint64_t)(unsigned long)nvpb->buffers;
req.nr_relocs = nvpb->nr_relocs;
req.relocs = (uint64_t)(unsigned long)nvpb->relocs;
ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_PUSHBUF,
&req, sizeof(req));
} else {
nouveau_fence_flush(chan);
ret = nouveau_pushbuf_flush_nomm(nvchan);
}
assert(ret == 0);
/* Update presumed offset/domain for any buffers that moved.
* Dereference all buffers on validate list
*/
for (i = 0; i < nvpb->nr_buffers; i++) {
struct drm_nouveau_gem_pushbuf_bo *pbbo = &nvpb->buffers[i];
struct nouveau_bo *bo = (void *)(unsigned long)pbbo->user_priv;
if (pbbo->presumed_ok == 0) {
nouveau_bo(bo)->domain = pbbo->presumed_domain;
nouveau_bo(bo)->offset = pbbo->presumed_offset;
}
nouveau_bo(bo)->pending = NULL;
nouveau_bo_ref(NULL, &bo);
}
nvpb->nr_buffers = 0;
nvpb->nr_relocs = 0;
/* Allocate space for next push buffer */
ret = nouveau_pushbuf_space(chan, min);
assert(!ret);
if (chan->flush_notify)
chan->flush_notify(chan);
return 0;
}

View File

@ -0,0 +1,160 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_PUSHBUF_H__
#define __NOUVEAU_PUSHBUF_H__
#include <assert.h>
#include <string.h>
#include "nouveau_bo.h"
#include "nouveau_grobj.h"
struct nouveau_pushbuf {
struct nouveau_channel *channel;
unsigned remaining;
uint32_t *cur;
};
int
nouveau_pushbuf_flush(struct nouveau_channel *, unsigned min);
int
nouveau_pushbuf_emit_reloc(struct nouveau_channel *, void *ptr,
struct nouveau_bo *, uint32_t data, uint32_t flags,
uint32_t vor, uint32_t tor);
/* Push buffer access macros */
static __inline__ void
OUT_RING(struct nouveau_channel *chan, unsigned data)
{
*(chan->pushbuf->cur++) = (data);
}
static __inline__ void
OUT_RINGp(struct nouveau_channel *chan, const void *data, unsigned size)
{
memcpy(chan->pushbuf->cur, data, size * 4);
chan->pushbuf->cur += size;
}
static __inline__ void
OUT_RINGf(struct nouveau_channel *chan, float f)
{
union { uint32_t i; float f; } c;
c.f = f;
OUT_RING(chan, c.i);
}
static __inline__ unsigned
AVAIL_RING(struct nouveau_channel *chan)
{
return chan->pushbuf->remaining;
}
static __inline__ void
WAIT_RING(struct nouveau_channel *chan, unsigned size)
{
if (chan->pushbuf->remaining < size)
nouveau_pushbuf_flush(chan, size);
}
static __inline__ void
BEGIN_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr,
unsigned mthd, unsigned size)
{
if (gr->bound == NOUVEAU_GROBJ_UNBOUND)
nouveau_grobj_autobind(gr);
chan->subc[gr->subc].sequence = chan->subc_sequence++;
WAIT_RING(chan, size + 1);
OUT_RING(chan, (gr->subc << 13) | (size << 18) | mthd);
chan->pushbuf->remaining -= (size + 1);
}
static __inline__ void
FIRE_RING(struct nouveau_channel *chan)
{
nouveau_pushbuf_flush(chan, 0);
}
static __inline__ void
BIND_RING(struct nouveau_channel *chan, struct nouveau_grobj *gr, unsigned sc)
{
struct nouveau_subchannel *subc = &gr->channel->subc[sc];
if (subc->gr) {
if (subc->gr->bound == NOUVEAU_GROBJ_BOUND_EXPLICIT)
assert(0);
subc->gr->bound = NOUVEAU_GROBJ_UNBOUND;
}
subc->gr = gr;
subc->gr->subc = sc;
subc->gr->bound = NOUVEAU_GROBJ_BOUND_EXPLICIT;
BEGIN_RING(chan, gr, 0x0000, 1);
OUT_RING (chan, gr->handle);
}
static __inline__ void
OUT_RELOC(struct nouveau_channel *chan, struct nouveau_bo *bo,
unsigned data, unsigned flags, unsigned vor, unsigned tor)
{
nouveau_pushbuf_emit_reloc(chan, chan->pushbuf->cur++, bo,
data, flags, vor, tor);
}
/* Raw data + flags depending on FB/TT buffer */
static __inline__ void
OUT_RELOCd(struct nouveau_channel *chan, struct nouveau_bo *bo,
unsigned data, unsigned flags, unsigned vor, unsigned tor)
{
OUT_RELOC(chan, bo, data, flags | NOUVEAU_BO_OR, vor, tor);
}
/* FB/TT object handle */
static __inline__ void
OUT_RELOCo(struct nouveau_channel *chan, struct nouveau_bo *bo,
unsigned flags)
{
OUT_RELOC(chan, bo, 0, flags | NOUVEAU_BO_OR,
chan->vram->handle, chan->gart->handle);
}
/* Low 32-bits of offset */
static __inline__ void
OUT_RELOCl(struct nouveau_channel *chan, struct nouveau_bo *bo,
unsigned delta, unsigned flags)
{
OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_LOW, 0, 0);
}
/* High 32-bits of offset */
static __inline__ void
OUT_RELOCh(struct nouveau_channel *chan, struct nouveau_bo *bo,
unsigned delta, unsigned flags)
{
OUT_RELOC(chan, bo, delta, flags | NOUVEAU_BO_HIGH, 0, 0);
}
#endif

View File

@ -0,0 +1,115 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <errno.h>
#include "nouveau_private.h"
int
nouveau_resource_init(struct nouveau_resource **heap,
unsigned start, unsigned size)
{
struct nouveau_resource *r;
r = calloc(1, sizeof(struct nouveau_resource));
if (!r)
return 1;
r->start = start;
r->size = size;
*heap = r;
return 0;
}
int
nouveau_resource_alloc(struct nouveau_resource *heap, int size, void *priv,
struct nouveau_resource **res)
{
struct nouveau_resource *r;
if (!heap || !size || !res || *res)
return 1;
while (heap) {
if (!heap->in_use && heap->size >= size) {
r = calloc(1, sizeof(struct nouveau_resource));
if (!r)
return 1;
r->start = (heap->start + heap->size) - size;
r->size = size;
r->in_use = 1;
r->priv = priv;
heap->size -= size;
r->next = heap->next;
if (heap->next)
heap->next->prev = r;
r->prev = heap;
heap->next = r;
*res = r;
return 0;
}
heap = heap->next;
}
return 1;
}
void
nouveau_resource_free(struct nouveau_resource **res)
{
struct nouveau_resource *r;
if (!res || !*res)
return;
r = *res;
*res = NULL;
r->in_use = 0;
if (r->next && !r->next->in_use) {
struct nouveau_resource *new = r->next;
new->prev = r->prev;
if (r->prev)
r->prev->next = new;
new->size += r->size;
new->start = r->start;
free(r);
r = new;
}
if (r->prev && !r->prev->in_use) {
r->prev->next = r->next;
if (r->next)
r->next->prev = r->prev;
r->prev->size += r->size;
free(r);
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2007 Nouveau Project
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef __NOUVEAU_RESOURCE_H__
#define __NOUVEAU_RESOURCE_H__
struct nouveau_resource {
struct nouveau_resource *prev;
struct nouveau_resource *next;
int in_use;
void *priv;
unsigned int start;
unsigned int size;
};
int
nouveau_resource_init(struct nouveau_resource **heap, unsigned start,
unsigned size);
int
nouveau_resource_alloc(struct nouveau_resource *heap, int size, void *priv,
struct nouveau_resource **);
void
nouveau_resource_free(struct nouveau_resource **);
#endif