summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Clark <robclark@freedesktop.org>2013-07-10 15:20:04 -0400
committerRob Clark <robclark@freedesktop.org>2013-08-28 15:50:15 -0400
commitb2b1885dfcb3a206623e926704057b448d06781d (patch)
tree6ca7628bb7504364a8f0eeffebbed9b6ce05f4a9
parent3c967e715528ee52195c178c4d09d03b643f0c06 (diff)
freedreno: support either kgsl or msm
Split out common code and backend. Current backend is for 'kgsl' android driver, but a new backend will provide support for the upstream msm drm/kms driver. Signed-off-by: Rob Clark <robclark@freedesktop.org>
-rw-r--r--freedreno/Makefile.am11
-rw-r--r--freedreno/freedreno_bo.c213
-rw-r--r--freedreno/freedreno_device.c45
-rw-r--r--freedreno/freedreno_drmif.h7
-rw-r--r--freedreno/freedreno_pipe.c188
-rw-r--r--freedreno/freedreno_priv.h85
-rw-r--r--freedreno/freedreno_ringbuffer.c170
-rw-r--r--freedreno/freedreno_ringbuffer.h32
-rw-r--r--freedreno/kgsl/kgsl_bo.c291
-rw-r--r--freedreno/kgsl/kgsl_device.c61
-rw-r--r--freedreno/kgsl/kgsl_drm.h (renamed from freedreno/kgsl_drm.h)0
-rw-r--r--freedreno/kgsl/kgsl_pipe.c264
-rw-r--r--freedreno/kgsl/kgsl_priv.h115
-rw-r--r--freedreno/kgsl/kgsl_ringbuffer.c224
-rw-r--r--freedreno/kgsl/msm_kgsl.h (renamed from freedreno/msm_kgsl.h)0
15 files changed, 1103 insertions, 603 deletions
diff --git a/freedreno/Makefile.am b/freedreno/Makefile.am
index ba9bd68b..45074c9b 100644
--- a/freedreno/Makefile.am
+++ b/freedreno/Makefile.am
@@ -16,9 +16,14 @@ libdrm_freedreno_la_SOURCES = \
freedreno_priv.h \
freedreno_ringbuffer.c \
freedreno_bo.c \
- kgsl_drm.h \
- list.h \
- msm_kgsl.h
+ kgsl/kgsl_bo.c \
+ kgsl/kgsl_device.c \
+ kgsl/kgsl_drm.h \
+ kgsl/kgsl_pipe.c \
+ kgsl/kgsl_priv.h \
+ kgsl/kgsl_ringbuffer.c \
+ kgsl/msm_kgsl.h \
+ list.h
libdrm_freedrenocommonincludedir = ${includedir}/freedreno
libdrm_freedrenocommoninclude_HEADERS = freedreno_drmif.h freedreno_ringbuffer.h
diff --git a/freedreno/freedreno_bo.c b/freedreno/freedreno_bo.c
index 8f78432b..92c7dd79 100644
--- a/freedreno/freedreno_bo.c
+++ b/freedreno/freedreno_bo.c
@@ -29,8 +29,6 @@
#include "freedreno_drmif.h"
#include "freedreno_priv.h"
-#include <linux/fb.h>
-
static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
/* set buffer name, and add to table, call w/ table_lock held: */
@@ -56,8 +54,9 @@ static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
static struct fd_bo * bo_from_handle(struct fd_device *dev,
uint32_t size, uint32_t handle)
{
- unsigned i;
- struct fd_bo *bo = calloc(1, sizeof(*bo));
+ struct fd_bo *bo;
+
+ bo = dev->funcs->bo_from_handle(dev, size, handle);
if (!bo) {
struct drm_gem_close req = {
.handle = handle,
@@ -71,127 +70,37 @@ static struct fd_bo * bo_from_handle(struct fd_device *dev,
atomic_set(&bo->refcnt, 1);
/* add ourself into the handle table: */
drmHashInsert(dev->handle_table, handle, bo);
- for (i = 0; i < ARRAY_SIZE(bo->list); i++)
- list_inithead(&bo->list[i]);
return bo;
}
-static int set_memtype(struct fd_bo *bo, uint32_t flags)
-{
- struct drm_kgsl_gem_memtype req = {
- .handle = bo->handle,
- .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK,
- };
-
- return drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SETMEMTYPE,
- &req, sizeof(req));
-}
-
-static int bo_alloc(struct fd_bo *bo)
-{
- if (!bo->offset) {
- struct drm_kgsl_gem_alloc req = {
- .handle = bo->handle,
- };
- int ret;
-
- /* if the buffer is already backed by pages then this
- * doesn't actually do anything (other than giving us
- * the offset)
- */
- ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_ALLOC,
- &req, sizeof(req));
- if (ret) {
- ERROR_MSG("alloc failed: %s", strerror(errno));
- return ret;
- }
-
- bo->offset = req.offset;
- }
- return 0;
-}
-
struct fd_bo * fd_bo_new(struct fd_device *dev,
uint32_t size, uint32_t flags)
{
- struct drm_kgsl_gem_create req = {
- .size = ALIGN(size, 4096),
- };
struct fd_bo *bo = NULL;
+ uint32_t handle;
+ int ret;
- if (drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE,
- &req, sizeof(req))) {
+ ret = dev->funcs->bo_new_handle(dev, ALIGN(size, 4096), flags, &handle);
+ if (ret)
return NULL;
- }
pthread_mutex_lock(&table_lock);
- bo = bo_from_handle(dev, size, req.handle);
+ bo = bo_from_handle(dev, size, handle);
pthread_mutex_unlock(&table_lock);
- if (!bo) {
- goto fail;
- }
-
- if (set_memtype(bo, flags)) {
- goto fail;
- }
return bo;
-fail:
- if (bo)
- fd_bo_del(bo);
- return NULL;
}
-/* don't use this... it is just needed to get a bo from the
- * framebuffer (pre-dmabuf)
- */
-struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe,
- int fbfd, uint32_t size)
+struct fd_bo *fd_bo_from_handle(struct fd_device *dev,
+ uint32_t handle, uint32_t size)
{
- struct drm_kgsl_gem_create_fd req = {
- .fd = fbfd,
- };
- struct fd_bo *bo;
-
- if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD,
- &req, sizeof(req))) {
- return NULL;
- }
+ struct fd_bo *bo = NULL;
pthread_mutex_lock(&table_lock);
- bo = bo_from_handle(pipe->dev, size, req.handle);
-
- /* this is fugly, but works around a bug in the kernel..
- * priv->memdesc.size never gets set, so getbufinfo ioctl
- * thinks the buffer hasn't be allocate and fails
- */
- if (bo && !fd_bo_gpuaddr(bo, 0)) {
- void *fbmem = mmap(NULL, size, PROT_READ | PROT_WRITE,
- MAP_SHARED, fbfd, 0);
- struct kgsl_map_user_mem req = {
- .memtype = KGSL_USER_MEM_TYPE_ADDR,
- .len = size,
- .offset = 0,
- .hostptr = (unsigned long)fbmem,
- };
- int ret;
- ret = ioctl(pipe->fd, IOCTL_KGSL_MAP_USER_MEM, &req);
- if (ret) {
- ERROR_MSG("mapping user mem failed: %s",
- strerror(errno));
- goto fail;
- }
- bo->gpuaddr = req.gpuaddr;
- bo->map = fbmem;
- }
+ bo = bo_from_handle(dev, size, handle);
pthread_mutex_unlock(&table_lock);
return bo;
-fail:
- pthread_mutex_unlock(&table_lock);
- if (bo)
- fd_bo_del(bo);
- return NULL;
}
struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
@@ -235,6 +144,8 @@ struct fd_bo * fd_bo_ref(struct fd_bo *bo)
void fd_bo_del(struct fd_bo *bo)
{
+ struct fd_device *dev;
+
if (!atomic_dec_and_test(&bo->refcnt))
return;
@@ -253,8 +164,10 @@ void fd_bo_del(struct fd_bo *bo)
pthread_mutex_unlock(&table_lock);
}
- fd_device_del(bo->dev);
- free(bo);
+ dev = bo->dev;
+ bo->funcs->destroy(bo);
+
+ fd_device_del(dev);
}
int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
@@ -293,15 +206,16 @@ uint32_t fd_bo_size(struct fd_bo *bo)
void * fd_bo_map(struct fd_bo *bo)
{
if (!bo->map) {
+ uint64_t offset;
int ret;
- ret = bo_alloc(bo);
+ ret = bo->funcs->offset(bo, &offset);
if (ret) {
return NULL;
}
bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
- bo->dev->fd, bo->offset);
+ bo->dev->fd, offset);
if (bo->map == MAP_FAILED) {
ERROR_MSG("mmap failed: %s", strerror(errno));
bo->map = NULL;
@@ -310,88 +224,13 @@ void * fd_bo_map(struct fd_bo *bo)
return bo->map;
}
-uint32_t fd_bo_gpuaddr(struct fd_bo *bo, uint32_t offset)
+/* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
+int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
{
- if (!bo->gpuaddr) {
- struct drm_kgsl_gem_bufinfo req = {
- .handle = bo->handle,
- };
- int ret;
-
- ret = bo_alloc(bo);
- if (ret) {
- return ret;
- }
-
- ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
- &req, sizeof(req));
- if (ret) {
- ERROR_MSG("get bufinfo failed: %s", strerror(errno));
- return 0;
- }
-
- bo->gpuaddr = req.gpuaddr[0];
- }
- return bo->gpuaddr + offset;
+ return bo->funcs->cpu_prep(bo, pipe, op);
}
-/*
- * Super-cheezy way to synchronization between mesa and ddx.. the
- * SET_ACTIVE ioctl gives us a way to stash a 32b # w/ a GEM bo, and
- * GET_BUFINFO gives us a way to retrieve it. We use this to stash
- * the timestamp of the last ISSUEIBCMDS on the buffer.
- *
- * To avoid an obscene amount of syscalls, we:
- * 1) Only set the timestamp for buffers w/ an flink name, ie.
- * only buffers shared across processes. This is enough to
- * catch the DRI2 buffers.
- * 2) Only set the timestamp for buffers submitted to the 3d ring
- * and only check the timestamps on buffers submitted to the
- * 2d ring. This should be enough to handle synchronizing of
- * presentation blit. We could do synchronization in the other
- * direction too, but that would be problematic if we are using
- * the 3d ring from DDX, since client side wouldn't know this.
- *
- * The waiting on timestamp happens before flush, and setting of
- * timestamp happens after flush. It is transparent to the user
- * of libdrm_freedreno as all the tracking of buffers happens via
- * _emit_reloc()..
- */
-
-void fb_bo_set_timestamp(struct fd_bo *bo, uint32_t timestamp)
+void fd_bo_cpu_fini(struct fd_bo *bo)
{
- if (bo->name) {
- struct drm_kgsl_gem_active req = {
- .handle = bo->handle,
- .active = timestamp,
- };
- int ret;
-
- ret = drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SET_ACTIVE,
- &req, sizeof(req));
- if (ret) {
- ERROR_MSG("set active failed: %s", strerror(errno));
- }
- }
-}
-
-uint32_t fd_bo_get_timestamp(struct fd_bo *bo)
-{
- uint32_t timestamp = 0;
- if (bo->name) {
- struct drm_kgsl_gem_bufinfo req = {
- .handle = bo->handle,
- };
- int ret;
-
- ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
- &req, sizeof(req));
- if (ret) {
- ERROR_MSG("get bufinfo failed: %s", strerror(errno));
- return 0;
- }
-
- timestamp = req.active;
- }
- return timestamp;
+ bo->funcs->cpu_fini(bo);
}
diff --git a/freedreno/freedreno_device.c b/freedreno/freedreno_device.c
index 901d0667..03553b9f 100644
--- a/freedreno/freedreno_device.c
+++ b/freedreno/freedreno_device.c
@@ -36,35 +36,43 @@
static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
static void * dev_table;
+struct fd_device * kgsl_device_new(int fd);
+
static struct fd_device * fd_device_new_impl(int fd)
{
- struct fd_device *dev = calloc(1, sizeof(*dev));
+ struct fd_device *dev;
+ drmVersionPtr version;
+
+ /* figure out if we are kgsl or msm drm driver: */
+ version = drmGetVersion(fd);
+ if (!version) {
+ ERROR_MSG("cannot get version: %s", strerror(errno));
+ return NULL;
+ }
+
+ if (!strcmp(version->name, "kgsl")) {
+ DEBUG_MSG("kgsl DRM device");
+ dev = kgsl_device_new(fd);
+ } else {
+ ERROR_MSG("unknown device: %s", version->name);
+ dev = NULL;
+ }
+
if (!dev)
return NULL;
+
atomic_set(&dev->refcnt, 1);
dev->fd = fd;
dev->handle_table = drmHashCreate();
dev->name_table = drmHashCreate();
- return dev;
-}
-/* use inode for key into dev_table, rather than fd, to avoid getting
- * confused by multiple-opens:
- */
-static int devkey(int fd)
-{
- struct stat s;
- if (fstat(fd, &s)) {
- ERROR_MSG("stat failed: %s", strerror(errno));
- return -1;
- }
- return s.st_ino;
+ return dev;
}
struct fd_device * fd_device_new(int fd)
{
struct fd_device *dev = NULL;
- int key = devkey(fd);
+ int key = fd;
pthread_mutex_lock(&table_lock);
@@ -73,7 +81,8 @@ struct fd_device * fd_device_new(int fd)
if (drmHashLookup(dev_table, key, (void **)&dev)) {
dev = fd_device_new_impl(fd);
- drmHashInsert(dev_table, key, dev);
+ if (dev)
+ drmHashInsert(dev_table, key, dev);
} else {
dev = fd_device_ref(dev);
}
@@ -96,7 +105,7 @@ void fd_device_del(struct fd_device *dev)
pthread_mutex_lock(&table_lock);
drmHashDestroy(dev->handle_table);
drmHashDestroy(dev->name_table);
- drmHashDelete(dev_table, devkey(dev->fd));
+ drmHashDelete(dev_table, dev->fd);
pthread_mutex_unlock(&table_lock);
- free(dev);
+ dev->funcs->destroy(dev);
}
diff --git a/freedreno/freedreno_drmif.h b/freedreno/freedreno_drmif.h
index 54b900e7..2086cf1e 100644
--- a/freedreno/freedreno_drmif.h
+++ b/freedreno/freedreno_drmif.h
@@ -63,6 +63,10 @@ enum fd_param_id {
#define DRM_FREEDRENO_GEM_CACHE_MASK 0x00f00000
#define DRM_FREEDRENO_GEM_GPUREADONLY 0x01000000
+/* bo access flags: */
+#define DRM_FREEDRENO_PREP_READ 0x01
+#define DRM_FREEDRENO_PREP_WRITE 0x02
+
/* device functions:
*/
@@ -80,7 +84,6 @@ void fd_pipe_del(struct fd_pipe *pipe);
int fd_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
uint64_t *value);
int fd_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp);
-int fd_pipe_timestamp(struct fd_pipe *pipe, uint32_t *timestamp);
/* buffer-object functions:
@@ -97,5 +100,7 @@ int fd_bo_get_name(struct fd_bo *bo, uint32_t *name);
uint32_t fd_bo_handle(struct fd_bo *bo);
uint32_t fd_bo_size(struct fd_bo *bo);
void * fd_bo_map(struct fd_bo *bo);
+int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
+void fd_bo_cpu_fini(struct fd_bo *bo);
#endif /* FREEDRENO_DRMIF_H_ */
diff --git a/freedreno/freedreno_pipe.c b/freedreno/freedreno_pipe.c
index 9f9f1c6e..805bf008 100644
--- a/freedreno/freedreno_pipe.c
+++ b/freedreno/freedreno_pipe.c
@@ -29,57 +29,16 @@
#include "freedreno_drmif.h"
#include "freedreno_priv.h"
-
-static int getprop(int fd, enum kgsl_property_type type,
- void *value, int sizebytes)
-{
- struct kgsl_device_getproperty req = {
- .type = type,
- .value = value,
- .sizebytes = sizebytes,
- };
- return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req);
-}
-
-#define GETPROP(fd, prop, x) do { \
- if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) { \
- ERROR_MSG("failed to get property: " #prop); \
- goto fail; \
- } } while (0)
-
-
struct fd_pipe * fd_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
{
- static const char *paths[] = {
- [FD_PIPE_3D] = "/dev/kgsl-3d0",
- [FD_PIPE_2D] = "/dev/kgsl-2d0",
- };
- struct kgsl_drawctxt_create req = {
- .flags = 0x2000, /* ??? */
- };
struct fd_pipe *pipe = NULL;
- int ret, fd;
if (id > FD_PIPE_MAX) {
ERROR_MSG("invalid pipe id: %d", id);
goto fail;
}
- fd = open(paths[id], O_RDWR);
- if (fd < 0) {
- ERROR_MSG("could not open %s device: %d (%s)",
- paths[id], fd, strerror(errno));
- goto fail;
- }
-
- ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req);
- if (ret) {
- ERROR_MSG("failed to allocate context: %d (%s)",
- ret, strerror(errno));
- goto fail;
- }
-
- pipe = calloc(1, sizeof(*pipe));
+ pipe = dev->funcs->pipe_new(dev, id);
if (!pipe) {
ERROR_MSG("allocation failed");
goto fail;
@@ -87,31 +46,6 @@ struct fd_pipe * fd_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
pipe->dev = dev;
pipe->id = id;
- pipe->fd = fd;
- pipe->drawctxt_id = req.drawctxt_id;
-
- list_inithead(&pipe->submit_list);
- list_inithead(&pipe->pending_list);
-
- GETPROP(fd, VERSION, pipe->version);
- GETPROP(fd, DEVICE_INFO, pipe->devinfo);
-
- INFO_MSG("Pipe Info:");
- INFO_MSG(" Device: %s", paths[id]);
- INFO_MSG(" Chip-id: %d.%d.%d.%d",
- (pipe->devinfo.chip_id >> 24) & 0xff,
- (pipe->devinfo.chip_id >> 16) & 0xff,
- (pipe->devinfo.chip_id >> 8) & 0xff,
- (pipe->devinfo.chip_id >> 0) & 0xff);
- INFO_MSG(" Device-id: %d", pipe->devinfo.device_id);
- INFO_MSG(" GPU-id: %d", pipe->devinfo.gpu_id);
- INFO_MSG(" MMU enabled: %d", pipe->devinfo.mmu_enabled);
- INFO_MSG(" GMEM Base addr: 0x%08x", pipe->devinfo.gmem_gpubaseaddr);
- INFO_MSG(" GMEM size: 0x%08x", pipe->devinfo.gmem_sizebytes);
- INFO_MSG(" Driver version: %d.%d",
- pipe->version.drv_major, pipe->version.drv_minor);
- INFO_MSG(" Device version: %d.%d",
- pipe->version.dev_major, pipe->version.dev_minor);
return pipe;
fail:
@@ -122,130 +56,16 @@ fail:
void fd_pipe_del(struct fd_pipe *pipe)
{
- struct kgsl_drawctxt_destroy req = {
- .drawctxt_id = pipe->drawctxt_id,
- };
-
- if (pipe->drawctxt_id)
- ioctl(pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req);
-
- if (pipe->fd)
- close(pipe->fd);
-
- free(pipe);
+ pipe->funcs->destroy(pipe);
}
int fd_pipe_get_param(struct fd_pipe *pipe, enum fd_param_id param,
uint64_t *value)
{
- switch (param) {
- case FD_DEVICE_ID:
- *value = pipe->devinfo.device_id;
- return 0;
- case FD_GPU_ID:
- *value = pipe->devinfo.gpu_id;
- return 0;
- case FD_GMEM_SIZE:
- *value = pipe->devinfo.gmem_sizebytes;
- return 0;
- default:
- ERROR_MSG("invalid param id: %d", param);
- return -1;
- }
+ return pipe->funcs->get_param(pipe, param, value);
}
int fd_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp)
{
- struct kgsl_device_waittimestamp req = {
- .timestamp = timestamp,
- .timeout = 5000,
- };
- int ret;
-
- do {
- ret = ioctl(pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req);
- } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
- if (ret)
- ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno));
- else
- fd_pipe_process_pending(pipe, timestamp);
- return ret;
-}
-
-int fd_pipe_timestamp(struct fd_pipe *pipe, uint32_t *timestamp)
-{
- struct kgsl_cmdstream_readtimestamp req = {
- .type = KGSL_TIMESTAMP_RETIRED
- };
- int ret = ioctl(pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req);
- if (ret) {
- ERROR_MSG("readtimestamp failed! %d (%s)",
- ret, strerror(errno));
- return ret;
- }
- *timestamp = req.timestamp;
- return 0;
-}
-
-/* add buffer to submit list when it is referenced in cmdstream: */
-void fd_pipe_add_submit(struct fd_pipe *pipe, struct fd_bo *bo)
-{
- struct list_head *list = &bo->list[pipe->id];
- if (LIST_IS_EMPTY(list)) {
- fd_bo_ref(bo);
- } else {
- list_del(list);
- }
- list_addtail(list, &pipe->submit_list);
-}
-
-/* prepare buffers on submit list before flush: */
-void fd_pipe_pre_submit(struct fd_pipe *pipe)
-{
- struct fd_bo *bo;
-
- if (pipe->id == FD_PIPE_3D)
- return;
-
- if (!pipe->p3d)
- pipe->p3d = fd_pipe_new(pipe->dev, FD_PIPE_3D);
-
- LIST_FOR_EACH_ENTRY(bo, &pipe->submit_list, list[pipe->id]) {
- uint32_t timestamp = fd_bo_get_timestamp(bo);
- if (timestamp)
- fd_pipe_wait(pipe->p3d, timestamp);
- }
-}
-
-/* process buffers on submit list after flush: */
-void fd_pipe_post_submit(struct fd_pipe *pipe, uint32_t timestamp)
-{
- struct fd_bo *bo, *tmp;
-
- LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &pipe->submit_list, list[pipe->id]) {
- struct list_head *list = &bo->list[pipe->id];
- list_del(list);
- bo->timestamp[pipe->id] = timestamp;
- list_addtail(list, &pipe->pending_list);
-
- if (pipe->id == FD_PIPE_3D)
- fb_bo_set_timestamp(bo, timestamp);
- }
-
- if (!fd_pipe_timestamp(pipe, &timestamp))
- fd_pipe_process_pending(pipe, timestamp);
-}
-
-void fd_pipe_process_pending(struct fd_pipe *pipe, uint32_t timestamp)
-{
- struct fd_bo *bo, *tmp;
-
- LIST_FOR_EACH_ENTRY_SAFE(bo, tmp, &pipe->pending_list, list[pipe->id]) {
- struct list_head *list = &bo->list[pipe->id];
- if (bo->timestamp[pipe->id] > timestamp)
- return;
- list_delinit(list);
- bo->timestamp[pipe->id] = 0;
- fd_bo_del(bo);
- }
+ return pipe->funcs->wait(pipe, timestamp);
}
diff --git a/freedreno/freedreno_priv.h b/freedreno/freedreno_priv.h
index 433bd300..cbf41d71 100644
--- a/freedreno/freedreno_priv.h
+++ b/freedreno/freedreno_priv.h
@@ -38,6 +38,8 @@
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <pthread.h>
+#include <stdio.h>
+#include <assert.h>
#include "xf86drm.h"
#include "xf86atomic.h"
@@ -45,8 +47,17 @@
#include "list.h"
#include "freedreno_drmif.h"
-#include "msm_kgsl.h"
-#include "kgsl_drm.h"
+#include "freedreno_ringbuffer.h"
+#include "drm/drm.h"
+
+struct fd_device_funcs {
+ int (*bo_new_handle)(struct fd_device *dev, uint32_t size,
+ uint32_t flags, uint32_t *handle);
+ struct fd_bo * (*bo_from_handle)(struct fd_device *dev,
+ uint32_t size, uint32_t handle);
+ struct fd_pipe * (*pipe_new)(struct fd_device *dev, enum fd_pipe_id id);
+ void (*destroy)(struct fd_device *dev);
+};
struct fd_device {
int fd;
@@ -62,60 +73,57 @@ struct fd_device {
* open in the process first, before calling gem-open.
*/
void *handle_table, *name_table;
+
+ struct fd_device_funcs *funcs;
+};
+
+struct fd_pipe_funcs {
+ struct fd_ringbuffer * (*ringbuffer_new)(struct fd_pipe *pipe, uint32_t size);
+ int (*get_param)(struct fd_pipe *pipe, enum fd_param_id param, uint64_t *value);
+ int (*wait)(struct fd_pipe *pipe, uint32_t timestamp);
+ void (*destroy)(struct fd_pipe *pipe);
};
struct fd_pipe {
struct fd_device *dev;
enum fd_pipe_id id;
- int fd;
- uint32_t drawctxt_id;
-
- /* device properties: */
- struct kgsl_version version;
- struct kgsl_devinfo devinfo;
-
- /* list of bo's that are referenced in ringbuffer but not
- * submitted yet:
- */
- struct list_head submit_list;
+ struct fd_pipe_funcs *funcs;
+};
- /* list of bo's that have been submitted but timestamp has
- * not passed yet (so still ref'd in active cmdstream)
- */
- struct list_head pending_list;
+struct fd_ringmarker {
+ struct fd_ringbuffer *ring;
+ uint32_t *cur;
+};
- /* if we are the 2d pipe, and want to wait on a timestamp
- * from 3d, we need to also internally open the 3d pipe:
- */
- struct fd_pipe *p3d;
+struct fd_ringbuffer_funcs {
+ void * (*hostptr)(struct fd_ringbuffer *ring);
+ int (*flush)(struct fd_ringbuffer *ring, uint32_t *last_start);
+ void (*emit_reloc)(struct fd_ringbuffer *ring,
+ const struct fd_reloc *reloc);
+ void (*emit_reloc_ring)(struct fd_ringbuffer *ring,
+ struct fd_ringmarker *target, struct fd_ringmarker *end);
+ void (*destroy)(struct fd_ringbuffer *ring);
};
-void fd_pipe_add_submit(struct fd_pipe *pipe, struct fd_bo *bo);
-void fd_pipe_pre_submit(struct fd_pipe *pipe);
-void fd_pipe_post_submit(struct fd_pipe *pipe, uint32_t timestamp);
-void fd_pipe_process_pending(struct fd_pipe *pipe, uint32_t timestamp);
+struct fd_bo_funcs {
+ int (*offset)(struct fd_bo *bo, uint64_t *offset);
+ int (*cpu_prep)(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op);
+ void (*cpu_fini)(struct fd_bo *bo);
+ void (*destroy)(struct fd_bo *bo);
+};
struct fd_bo {
struct fd_device *dev;
uint32_t size;
uint32_t handle;
uint32_t name;
- uint32_t gpuaddr;
void *map;
- uint64_t offset;
- /* timestamp (per pipe) for bo's in a pipe's pending_list: */
- uint32_t timestamp[FD_PIPE_MAX];
- /* list-node for pipe's submit_list or pending_list */
- struct list_head list[FD_PIPE_MAX];
atomic_t refcnt;
+ struct fd_bo_funcs *funcs;
};
-/* not exposed publicly, because won't be needed when we have
- * a proper kernel driver
- */
-uint32_t fd_bo_gpuaddr(struct fd_bo *bo, uint32_t offset);
-void fb_bo_set_timestamp(struct fd_bo *bo, uint32_t timestamp);
-uint32_t fd_bo_get_timestamp(struct fd_bo *bo);
+struct fd_bo *fd_bo_from_handle(struct fd_device *dev,
+ uint32_t handle, uint32_t size);
#define ALIGN(v,a) (((v) + (a) - 1) & ~((a) - 1))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
@@ -135,4 +143,7 @@ uint32_t fd_bo_get_timestamp(struct fd_bo *bo);
do { drmMsg("[E] " fmt " (%s:%d)\n", \
##__VA_ARGS__, __FUNCTION__, __LINE__); } while (0)
+#define U642VOID(x) ((void *)(unsigned long)(x))
+#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
+
#endif /* FREEDRENO_PRIV_H_ */
diff --git a/freedreno/freedreno_ringbuffer.c b/freedreno/freedreno_ringbuffer.c
index 2a9f4367..de666865 100644
--- a/freedreno/freedreno_ringbuffer.c
+++ b/freedreno/freedreno_ringbuffer.c
@@ -32,109 +32,28 @@
#include "freedreno_priv.h"
#include "freedreno_ringbuffer.h"
-
-/* because kgsl tries to validate the gpuaddr on kernel side in ISSUEIBCMDS,
- * we can't use normal gem bo's for ringbuffer.. someday the kernel part
- * needs to be reworked into a single sane drm driver :-/
- */
-struct fd_rb_bo {
- struct fd_pipe *pipe;
- void *hostptr;
- uint32_t gpuaddr;
- uint32_t size;
-};
-
-struct fd_ringmarker {
- struct fd_ringbuffer *ring;
- uint32_t *cur;
-};
-
-static void fd_rb_bo_del(struct fd_rb_bo *bo)
-{
- struct kgsl_sharedmem_free req = {
- .gpuaddr = bo->gpuaddr,
- };
- int ret;
-
- munmap(bo->hostptr, bo->size);
-
- ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req);
- if (ret) {
- ERROR_MSG("sharedmem free failed: %s", strerror(errno));
- }
-
- free(bo);
-}
-
-static struct fd_rb_bo * fd_rb_bo_new(struct fd_pipe *pipe, uint32_t size)
-{
- struct fd_rb_bo *bo;
- struct kgsl_gpumem_alloc req = {
- .size = ALIGN(size, 4096),
- .flags = KGSL_MEMFLAGS_GPUREADONLY,
- };
- int ret;
-
- bo = calloc(1, sizeof(*bo));
- if (!bo) {
- ERROR_MSG("allocation failed");
- return NULL;
- }
- ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req);
- if (ret) {
- ERROR_MSG("gpumem allocation failed: %s", strerror(errno));
- goto fail;
- }
-
- bo->pipe = pipe;
- bo->gpuaddr = req.gpuaddr;
- bo->size = size;
- bo->hostptr = mmap(NULL, size, PROT_WRITE|PROT_READ,
- MAP_SHARED, pipe->fd, req.gpuaddr);
-
- return bo;
-fail:
- if (bo)
- fd_rb_bo_del(bo);
- return NULL;
-}
-
struct fd_ringbuffer * fd_ringbuffer_new(struct fd_pipe *pipe,
uint32_t size)
{
- struct fd_ringbuffer *ring = NULL;
-
- ring = calloc(1, sizeof(*ring));
- if (!ring) {
- ERROR_MSG("allocation failed");
- goto fail;
- }
+ struct fd_ringbuffer *ring;
- ring->bo = fd_rb_bo_new(pipe, size);
- if (!ring->bo) {
- ERROR_MSG("ringbuffer allocation failed");
- goto fail;
- }
+ ring = pipe->funcs->ringbuffer_new(pipe, size);
+ if (!ring)
+ return NULL;
ring->size = size;
ring->pipe = pipe;
- ring->start = ring->bo->hostptr;
+ ring->start = ring->funcs->hostptr(ring);
ring->end = &(ring->start[size/4]);
ring->cur = ring->last_start = ring->start;
return ring;
-fail:
- if (ring)
- fd_ringbuffer_del(ring);
- return NULL;
}
void fd_ringbuffer_del(struct fd_ringbuffer *ring)
{
- if (ring->bo)
- fd_rb_bo_del(ring->bo);
- free(ring);
+ ring->funcs->destroy(ring);
}
void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
@@ -145,54 +64,10 @@ void fd_ringbuffer_reset(struct fd_ringbuffer *ring)
ring->cur = ring->last_start = start;
}
-static int flush_impl(struct fd_ringbuffer *ring, uint32_t *last_start)
-{
- uint32_t offset = (uint8_t *)last_start - (uint8_t *)ring->start;
- struct kgsl_ibdesc ibdesc = {
- .gpuaddr = ring->bo->gpuaddr + offset,
- .hostptr = last_start,
- .sizedwords = ring->cur - last_start,
- };
- struct kgsl_ringbuffer_issueibcmds req = {
- .drawctxt_id = ring->pipe->drawctxt_id,
- .ibdesc_addr = (unsigned long)&ibdesc,
- .numibs = 1,
- .flags = KGSL_CONTEXT_SUBMIT_IB_LIST,
- };
- int ret;
-
- fd_pipe_pre_submit(ring->pipe);
-
- /* z180_cmdstream_issueibcmds() is made of fail: */
- if (ring->pipe->id == FD_PIPE_2D) {
- /* fix up size field in last cmd packet */
- uint32_t last_size = (uint32_t)(ring->cur - last_start);
- /* 5 is length of first packet, 2 for the two 7f000000's */
- last_start[2] = last_size - (5 + 2);
- ibdesc.gpuaddr = ring->bo->gpuaddr;
- ibdesc.hostptr = ring->bo->hostptr;
- ibdesc.sizedwords = 0x145;
- req.timestamp = (uint32_t)ring->bo->hostptr;
- }
-
- do {
- ret = ioctl(ring->pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
- } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
- if (ret)
- ERROR_MSG("issueibcmds failed! %d (%s)", ret, strerror(errno));
-
- ring->last_timestamp = req.timestamp;
- ring->last_start = ring->cur;
-
- fd_pipe_post_submit(ring->pipe, req.timestamp);
-
- return ret;
-}
-
/* maybe get rid of this and use fd_ringmarker_flush() from DDX too? */
int fd_ringbuffer_flush(struct fd_ringbuffer *ring)
{
- return flush_impl(ring, ring->last_start);
+ return ring->funcs->flush(ring, ring->last_start);
}
uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
@@ -200,33 +75,17 @@ uint32_t fd_ringbuffer_timestamp(struct fd_ringbuffer *ring)
return ring->last_timestamp;
}
-void fd_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
- struct fd_bo *bo, uint32_t offset, uint32_t or)
-{
- uint32_t addr = fd_bo_gpuaddr(bo, offset);
- assert(addr);
- (*ring->cur++) = addr | or;
- fd_pipe_add_submit(ring->pipe, bo);
-}
-
-void fd_ringbuffer_emit_reloc_shift(struct fd_ringbuffer *ring,
- struct fd_bo *bo, uint32_t offset, uint32_t or, int32_t shift)
+void fd_ringbuffer_reloc(struct fd_ringbuffer *ring,
+ const struct fd_reloc *reloc)
{
- uint32_t addr = fd_bo_gpuaddr(bo, offset);
- assert(addr);
- if (shift < 0)
- addr >>= -shift;
- else
- addr <<= shift;
- (*ring->cur++) = addr | or;
- fd_pipe_add_submit(ring->pipe, bo);
+ ring->funcs->emit_reloc(ring, reloc);
}
void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
- struct fd_ringmarker *target)
+ struct fd_ringmarker *target, struct fd_ringmarker *end)
{
- (*ring->cur++) = target->ring->bo->gpuaddr +
- (uint8_t *)target->cur - (uint8_t *)target->ring->start;
+ assert(target->ring == end->ring);
+ ring->funcs->emit_reloc_ring(ring, target, end);
}
struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring)
@@ -264,5 +123,6 @@ uint32_t fd_ringmarker_dwords(struct fd_ringmarker *start,
int fd_ringmarker_flush(struct fd_ringmarker *marker)
{
- return flush_impl(marker->ring, marker->cur);
+ struct fd_ringbuffer *ring = marker->ring;
+ return ring->funcs->flush(ring, marker->cur);
}
diff --git a/freedreno/freedreno_ringbuffer.h b/freedreno/freedreno_ringbuffer.h
index 051bbca9..4c99ea81 100644
--- a/freedreno/freedreno_ringbuffer.h
+++ b/freedreno/freedreno_ringbuffer.h
@@ -37,28 +37,17 @@
* have a kernel driver that can deal w/ reloc's..
*/
-struct fd_rb_bo;
+struct fd_ringbuffer_funcs;
struct fd_ringmarker;
struct fd_ringbuffer {
int size;
uint32_t *cur, *end, *start, *last_start;
struct fd_pipe *pipe;
- struct fd_rb_bo *bo;
+ struct fd_ringbuffer_funcs *funcs;
uint32_t last_timestamp;
};
-/* ringbuffer flush flags:
- * SAVE_GMEM - GMEM contents not preserved to system memory
- * in cmds flushed so if there is a context switch after
- * this flush and before the next one the kernel must
- * save GMEM contents
- * SUBMIT_IB_LIST - tbd..
- */
-#define DRM_FREEDRENO_CONTEXT_SAVE_GMEM 1
-#define DRM_FREEDRENO_CONTEXT_SUBMIT_IB_LIST 4
-
-
struct fd_ringbuffer * fd_ringbuffer_new(struct fd_pipe *pipe,
uint32_t size);
void fd_ringbuffer_del(struct fd_ringbuffer *ring);
@@ -72,12 +61,19 @@ static inline void fd_ringbuffer_emit(struct fd_ringbuffer *ring,
(*ring->cur++) = data;
}
-void fd_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
- struct fd_bo *bo, uint32_t offset, uint32_t or);
-void fd_ringbuffer_emit_reloc_shift(struct fd_ringbuffer *ring,
- struct fd_bo *bo, uint32_t offset, uint32_t or, int32_t shift);
+struct fd_reloc {
+ struct fd_bo *bo;
+#define FD_RELOC_READ 0x0001
+#define FD_RELOC_WRITE 0x0002
+ uint32_t flags;
+ uint32_t offset;
+ uint32_t or;
+ int32_t shift;
+};
+
+void fd_ringbuffer_reloc(struct fd_ringbuffer *ring, const struct fd_reloc *reloc);
void fd_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
- struct fd_ringmarker *target);
+ struct fd_ringmarker *target, struct fd_ringmarker *end);
struct fd_ringmarker * fd_ringmarker_new(struct fd_ringbuffer *ring);
void fd_ringmarker_del(struct fd_ringmarker *marker);
diff --git a/freedreno/kgsl/kgsl_bo.c b/freedreno/kgsl/kgsl_bo.c
new file mode 100644
index 00000000..0d019cb1
--- /dev/null
+++ b/freedreno/kgsl/kgsl_bo.c
@@ -0,0 +1,291 @@
+/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
+
+/*
+ * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
+ *
+ * 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 (including the next
+ * paragraph) 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 OR COPYRIGHT HOLDERS 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.
+ *
+ * Authors:
+ * Rob Clark <robclark@freedesktop.org>
+ */
+
+#include "kgsl_priv.h"
+
+#include <linux/fb.h>
+
+static int set_memtype(struct fd_device *dev, uint32_t handle, uint32_t flags)
+{
+ struct drm_kgsl_gem_memtype req = {
+ .handle = handle,
+ .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK,
+ };
+
+ return drmCommandWrite(dev->fd, DRM_KGSL_GEM_SETMEMTYPE,
+ &req, sizeof(req));
+}
+
+static int bo_alloc(struct kgsl_bo *kgsl_bo)
+{
+ struct fd_bo *bo = &kgsl_bo->base;
+ if (!kgsl_bo->offset) {
+ struct drm_kgsl_gem_alloc req = {
+ .handle = bo->handle,
+ };
+ int ret;
+
+ /* if the buffer is already backed by pages then this
+ * doesn't actually do anything (other than giving us
+ * the offset)
+ */
+ ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_ALLOC,
+ &req, sizeof(req));
+ if (ret) {
+ ERROR_MSG("alloc failed: %s", strerror(errno));
+ return ret;
+ }
+
+ kgsl_bo->offset = req.offset;
+ }
+
+ return 0;
+}
+
+static int kgsl_bo_offset(struct fd_bo *bo, uint64_t *offset)
+{
+ struct kgsl_bo *kgsl_bo = to_kgsl_bo(bo);
+ int ret = bo_alloc(kgsl_bo);
+ if (ret)
+ return ret;
+ *offset = kgsl_bo->offset;
+ return 0;
+}
+
+static int kgsl_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
+{
+ uint32_t timestamp = kgsl_bo_get_timestamp(to_kgsl_bo(bo));
+ if (timestamp) {
+ fd_pipe_wait(pipe, timestamp);
+ }
+ return 0;
+}
+
+static void kgsl_bo_cpu_fini(struct fd_bo *bo)
+{
+}
+
+static void kgsl_bo_destroy(struct fd_bo *bo)
+{
+ struct kgsl_bo *kgsl_bo = to_kgsl_bo(bo);
+ free(kgsl_bo);
+
+}
+
+static struct fd_bo_funcs funcs = {
+ .offset = kgsl_bo_offset,
+ .cpu_prep = kgsl_bo_cpu_prep,
+ .cpu_fini = kgsl_bo_cpu_fini,
+ .destroy = kgsl_bo_destroy,
+};
+
+/* allocate a buffer handle: */
+int kgsl_bo_new_handle(struct fd_device *dev,
+ uint32_t size, uint32_t flags, uint32_t *handle)
+{
+ struct drm_kgsl_gem_create req = {
+ .size = size,
+ };
+ int ret;
+
+ ret = drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE,
+ &req, sizeof(req));
+ if (ret)
+ return ret;
+
+ // TODO make flags match msm driver, since kgsl is legacy..
+ // translate flags in kgsl..
+
+ set_memtype(dev, req.handle, flags);
+
+ *handle = req.handle;
+
+ return 0;
+}
+
+/* allocate a new buffer object */
+struct fd_bo * kgsl_bo_from_handle(struct fd_device *dev,
+ uint32_t size, uint32_t handle)
+{
+ struct kgsl_bo *kgsl_bo;
+ struct fd_bo *bo;
+ unsigned i;
+
+ kgsl_bo = calloc(1, sizeof(*kgsl_bo));
+ if (!kgsl_bo)
+ return NULL;
+
+ bo = &kgsl_bo->base;
+ bo->funcs = &funcs;
+
+ for (i = 0; i < ARRAY_SIZE(kgsl_bo->list); i++)
+ list_inithead(&kgsl_bo->list[i]);
+
+ return bo;
+}
+
+struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe,
+ int fbfd, uint32_t size)
+{
+ struct drm_kgsl_gem_create_fd req = {
+ .fd = fbfd,
+ };
+ struct fd_bo *bo;
+ struct kgsl_bo *kgsl_bo;
+
+ if (!is_kgsl_pipe(pipe))
+ return NULL;
+
+ if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD,
+ &req, sizeof(req))) {
+ return NULL;
+ }
+
+ bo = fd_bo_from_handle(pipe->dev, req.handle, size);
+ kgsl_bo = to_kgsl_bo(bo);
+
+ /* this is fugly, but works around a bug in the kernel..
+ * priv->memdesc.size never gets set, so getbufinfo ioctl
+ * thinks the buffer hasn't be allocate and fails
+ */
+ if (bo && !kgsl_bo_gpuaddr(kgsl_bo, 0)) {
+ void *fbmem = mmap(NULL, size, PROT_READ | PROT_WRITE,
+ MAP_SHARED, fbfd, 0);
+ struct kgsl_map_user_mem req = {
+ .memtype = KGSL_USER_MEM_TYPE_ADDR,
+ .len = size,
+ .offset = 0,
+ .hostptr = (unsigned long)fbmem,
+ };
+ int ret;
+ ret = ioctl(to_kgsl_pipe(pipe)->fd, IOCTL_KGSL_MAP_USER_MEM, &req);
+ if (ret) {
+ ERROR_MSG("mapping user mem failed: %s",
+ strerror(errno));
+ goto fail;
+ }
+ kgsl_bo->gpuaddr = req.gpuaddr;
+ bo->map = fbmem;
+ }
+
+ return bo;
+fail:
+ if (bo)
+ fd_bo_del(bo);
+ return NULL;
+}
+
+
+uint32_t kgsl_bo_gpuaddr(struct kgsl_bo *kgsl_bo, uint32_t offset)
+{
+ struct fd_bo *bo = &kgsl_bo->base;
+ if (!kgsl_bo->gpuaddr) {
+ struct drm_kgsl_gem_bufinfo req = {
+ .handle = bo->handle,
+ };
+ int ret;
+
+ ret = bo_alloc(kgsl_bo);
+ if (ret) {
+ return ret;
+ }
+
+ ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
+ &req, sizeof(req));
+ if (ret) {
+ ERROR_MSG("get bufinfo failed: %s", strerror(errno));
+ return 0;
+ }
+
+ kgsl_bo->gpuaddr = req.gpuaddr[0];
+ }
+ return kgsl_bo->gpuaddr + offset;
+}
+
+/*
+ * Super-cheezy way to synchronization between mesa and ddx.. the
+ * SET_ACTIVE ioctl gives us a way to stash a 32b # w/ a GEM bo, and
+ * GET_BUFINFO gives us a way to retrieve it. We use this to stash
+ * the timestamp of the last ISSUEIBCMDS on the buffer.
+ *
+ * To avoid an obscene amount of syscalls, we:
+ * 1) Only set the timestamp for buffers w/ an flink name, ie.
+ * only buffers shared across processes. This is enough to
+ * catch the DRI2 buffers.
+ * 2) Only set the timestamp for buffers submitted to the 3d ring
+ * and only check the timestamps on buffers submitted to the
+ * 2d ring. This should be enough to handle synchronizing of
+ * presentation blit. We could do synchronization in the other
+ * direction too, but that would be problematic if we are using
+ * the 3d ring from DDX, since client side wouldn't know this.
+ *
+ * The waiting on timestamp happens before flush, and setting of
+ * timestamp happens after flush. It is transparent to the user
+ * of libdrm_freedreno as all the tracking of buffers happens via
+ * _emit_reloc()..
+ */
+
+void kgsl_bo_set_timestamp(struct kgsl_bo *kgsl_bo, uint32_t timestamp)
+{
+ struct fd_bo *bo = &kgsl_bo->base;
+ if (bo->name) {
+ struct drm_kgsl_gem_active req = {
+ .handle = bo->handle,
+ .active = timestamp,
+ };
+ int ret;
+
+ ret = drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SET_ACTIVE,
+ &req, sizeof(req));
+ if (ret) {
+ ERROR_MSG("set active failed: %s", strerror(errno));
+ }
+ }
+}
+
+uint32_t kgsl_bo_get_timestamp(struct kgsl_bo *kgsl_bo)
+{
+ struct fd_bo *bo = &kgsl_bo->base;
+ uint32_t timestamp = 0;
+ if (bo->name) {
+ struct drm_kgsl_gem_bufinfo req = {
+ .handle = bo->handle,
+ };
+ int ret;
+
+ ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
+ &req, sizeof(req));
+ if (ret) {
+ ERROR_MSG("get bufinfo failed: %s", strerror(errno));
+ return 0;
+ }
+
+ timestamp = req.active;
+ }
+ return timestamp;
+}
diff --git a/freedreno/kgsl/kgsl_device.c b/freedreno/kgsl/kgsl_device.c
new file mode 100644
index 00000000..fb6d6d2d
--- /dev/null
+++ b/freedreno/kgsl/kgsl_device.c
@@ -0,0 +1,61 @@
+/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
+
+/*
+ * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
+ *
+ * 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 (including the next
+ * paragraph) 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 OR COPYRIGHT HOLDERS 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.
+ *
+ * Authors:
+ * Rob Clark <robclark@freedesktop.org>
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "kgsl_priv.h"
+
+static void kgsl_device_destroy(struct fd_device *dev)
+{
+ struct kgsl_device *kgsl_dev = to_kgsl_device(dev);
+ free(kgsl_dev);
+}
+
+static struct fd_device_funcs funcs = {
+ .bo_new_handle = kgsl_bo_new_handle,
+ .bo_from_handle = kgsl_bo_from_handle,
+ .pipe_new = kgsl_pipe_new,
+ .destroy = kgsl_device_destroy,
+};
+
+struct fd_device * kgsl_device_new(int fd)
+{
+ struct kgsl_device *kgsl_dev;
+ struct fd_device *dev;
+
+ kgsl_dev = calloc(1, sizeof(*kgsl_dev));
+ if (!kgsl_dev)
+ return NULL;
+
+ dev = &kgsl_dev->base;
+ dev->funcs = &funcs;
+
+ return dev;
+}
diff --git a/freedreno/kgsl_drm.h b/freedreno/kgsl/kgsl_drm.h
index f1c7f4e2..f1c7f4e2 100644
--- a/freedreno/kgsl_drm.h
+++ b/freedreno/kgsl/kgsl_drm.h
diff --git a/freedreno/kgsl/kgsl_pipe.c b/freedreno/kgsl/kgsl_pipe.c
new file mode 100644
index 00000000..f7ff7fe0
--- /dev/null
+++ b/freedreno/kgsl/kgsl_pipe.c
@@ -0,0 +1,264 @@
+/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
+
+/*
+ * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
+ *
+ * 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 (including the next
+ * paragraph) 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 OR COPYRIGHT HOLDERS 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.
+ *
+ * Authors:
+ * Rob Clark <robclark@freedesktop.org>
+ */
+
+#include "kgsl_priv.h"
+
+
+static int kgsl_pipe_get_param(struct fd_pipe *pipe,
+ enum fd_param_id param, uint64_t *value)
+{
+ struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
+ switch (param) {
+ case FD_DEVICE_ID:
+ *value = kgsl_pipe->devinfo.device_id;
+ return 0;
+ case FD_GPU_ID:
+ *value = kgsl_pipe->devinfo.gpu_id;
+ return 0;
+ case FD_GMEM_SIZE:
+ *value = kgsl_pipe->devinfo.gmem_sizebytes;
+ return 0;
+ default:
+ ERROR_MSG("invalid param id: %d", param);
+ return -1;
+ }
+}
+
+static int kgsl_pipe_wait(struct fd_pipe *pipe, uint32_t timestamp)
+{
+ struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
+ struct kgsl_device_waittimestamp req = {
+ .timestamp = timestamp,
+ .timeout = 5000,
+ };
+ int ret;
+
+ do {
+ ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_DEVICE_WAITTIMESTAMP, &req);
+ } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
+ if (ret)
+ ERROR_MSG("waittimestamp failed! %d (%s)", ret, strerror(errno));
+ else
+ kgsl_pipe_process_pending(kgsl_pipe, timestamp);
+ return ret;
+}
+
+int kgsl_pipe_timestamp(struct kgsl_pipe *kgsl_pipe, uint32_t *timestamp)
+{
+ struct kgsl_cmdstream_readtimestamp req = {
+ .type = KGSL_TIMESTAMP_RETIRED
+ };
+ int ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_CMDSTREAM_READTIMESTAMP, &req);
+ if (ret) {
+ ERROR_MSG("readtimestamp failed! %d (%s)",
+ ret, strerror(errno));
+ return ret;
+ }
+ *timestamp = req.timestamp;
+ return 0;
+}
+
+static void kgsl_pipe_destroy(struct fd_pipe *pipe)
+{
+ struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(pipe);
+ struct kgsl_drawctxt_destroy req = {
+ .drawctxt_id = kgsl_pipe->drawctxt_id,
+ };
+
+ if (kgsl_pipe->drawctxt_id)
+ ioctl(kgsl_pipe->fd, IOCTL_KGSL_DRAWCTXT_DESTROY, &req);
+
+ if (kgsl_pipe->fd)
+ close(kgsl_pipe->fd);
+
+ free(kgsl_pipe);
+}
+
+static struct fd_pipe_funcs funcs = {
+ .ringbuffer_new = kgsl_ringbuffer_new,
+ .get_param = kgsl_pipe_get_param,
+ .wait = kgsl_pipe_wait,
+ .destroy = kgsl_pipe_destroy,
+};
+
+int is_kgsl_pipe(struct fd_pipe *pipe)
+{
+ return pipe->funcs == &funcs;
+}
+
+/* add buffer to submit list when it is referenced in cmdstream: */
+void kgsl_pipe_add_submit(struct kgsl_pipe *kgsl_pipe,
+ struct kgsl_bo *kgsl_bo)
+{
+ struct fd_pipe *pipe = &kgsl_pipe->base;
+ struct fd_bo *bo = &kgsl_bo->base;
+ struct list_head *list = &kgsl_bo->list[pipe->id];
+ if (LIST_IS_EMPTY(list)) {
+ fd_bo_ref(bo);
+ } else {
+ list_del(list);
+ }
+ list_addtail(list, &kgsl_pipe->submit_list);
+}
+
+/* prepare buffers on submit list before flush: */
+void kgsl_pipe_pre_submit(struct kgsl_pipe *kgsl_pipe)
+{
+ struct fd_pipe *pipe = &kgsl_pipe->base;
+ struct kgsl_bo *kgsl_bo = NULL;
+
+ if (!kgsl_pipe->p3d)
+ kgsl_pipe->p3d = fd_pipe_new(pipe->dev, FD_PIPE_3D);
+
+ LIST_FOR_EACH_ENTRY(kgsl_bo, &kgsl_pipe->submit_list, list[pipe->id]) {
+ uint32_t timestamp = kgsl_bo_get_timestamp(kgsl_bo);
+ if (timestamp)
+ fd_pipe_wait(kgsl_pipe->p3d, timestamp);
+ }
+}
+
+/* process buffers on submit list after flush: */
+void kgsl_pipe_post_submit(struct kgsl_pipe *kgsl_pipe, uint32_t timestamp)
+{
+ struct fd_pipe *pipe = &kgsl_pipe->base;
+ struct kgsl_bo *kgsl_bo = NULL, *tmp;
+
+ LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->submit_list, list[pipe->id]) {
+ struct list_head *list = &kgsl_bo->list[pipe->id];
+ list_del(list);
+ kgsl_bo->timestamp[pipe->id] = timestamp;
+ list_addtail(list, &kgsl_pipe->pending_list);
+
+ kgsl_bo_set_timestamp(kgsl_bo, timestamp);
+ }
+
+ if (!kgsl_pipe_timestamp(kgsl_pipe, &timestamp))
+ kgsl_pipe_process_pending(kgsl_pipe, timestamp);
+}
+
+void kgsl_pipe_process_pending(struct kgsl_pipe *kgsl_pipe, uint32_t timestamp)
+{
+ struct fd_pipe *pipe = &kgsl_pipe->base;
+ struct kgsl_bo *kgsl_bo = NULL, *tmp;
+
+ LIST_FOR_EACH_ENTRY_SAFE(kgsl_bo, tmp, &kgsl_pipe->pending_list, list[pipe->id]) {
+ struct list_head *list = &kgsl_bo->list[pipe->id];
+ if (kgsl_bo->timestamp[pipe->id] > timestamp)
+ return;
+ list_delinit(list);
+ kgsl_bo->timestamp[pipe->id] = 0;
+ fd_bo_del(&kgsl_bo->base);
+ }
+}
+
+static int getprop(int fd, enum kgsl_property_type type,
+ void *value, int sizebytes)
+{
+ struct kgsl_device_getproperty req = {
+ .type = type,
+ .value = value,
+ .sizebytes = sizebytes,
+ };
+ return ioctl(fd, IOCTL_KGSL_DEVICE_GETPROPERTY, &req);
+}
+
+#define GETPROP(fd, prop, x) do { \
+ if (getprop((fd), KGSL_PROP_##prop, &(x), sizeof(x))) { \
+ ERROR_MSG("failed to get property: " #prop); \
+ goto fail; \
+ } } while (0)
+
+
+struct fd_pipe * kgsl_pipe_new(struct fd_device *dev, enum fd_pipe_id id)
+{
+ static const char *paths[] = {
+ [FD_PIPE_3D] = "/dev/kgsl-3d0",
+ [FD_PIPE_2D] = "/dev/kgsl-2d0",
+ };
+ struct kgsl_drawctxt_create req = {
+ .flags = 0x2000, /* ??? */
+ };
+ struct kgsl_pipe *kgsl_pipe = NULL;
+ struct fd_pipe *pipe = NULL;
+ int ret, fd;
+
+ fd = open(paths[id], O_RDWR);
+ if (fd < 0) {
+ ERROR_MSG("could not open %s device: %d (%s)",
+ paths[id], fd, strerror(errno));
+ goto fail;
+ }
+
+ ret = ioctl(fd, IOCTL_KGSL_DRAWCTXT_CREATE, &req);
+ if (ret) {
+ ERROR_MSG("failed to allocate context: %d (%s)",
+ ret, strerror(errno));
+ goto fail;
+ }
+
+ kgsl_pipe = calloc(1, sizeof(*kgsl_pipe));
+ if (!kgsl_pipe) {
+ ERROR_MSG("allocation failed");
+ goto fail;
+ }
+
+ pipe = &kgsl_pipe->base;
+ pipe->funcs = &funcs;
+
+ kgsl_pipe->fd = fd;
+ kgsl_pipe->drawctxt_id = req.drawctxt_id;
+
+ list_inithead(&kgsl_pipe->submit_list);
+ list_inithead(&kgsl_pipe->pending_list);
+
+ GETPROP(fd, VERSION, kgsl_pipe->version);
+ GETPROP(fd, DEVICE_INFO, kgsl_pipe->devinfo);
+
+ INFO_MSG("Pipe Info:");
+ INFO_MSG(" Device: %s", paths[id]);
+ INFO_MSG(" Chip-id: %d.%d.%d.%d",
+ (kgsl_pipe->devinfo.chip_id >> 24) & 0xff,
+ (kgsl_pipe->devinfo.chip_id >> 16) & 0xff,
+ (kgsl_pipe->devinfo.chip_id >> 8) & 0xff,
+ (kgsl_pipe->devinfo.chip_id >> 0) & 0xff);
+ INFO_MSG(" Device-id: %d", kgsl_pipe->devinfo.device_id);
+ INFO_MSG(" GPU-id: %d", kgsl_pipe->devinfo.gpu_id);
+ INFO_MSG(" MMU enabled: %d", kgsl_pipe->devinfo.mmu_enabled);
+ INFO_MSG(" GMEM Base addr: 0x%08x", kgsl_pipe->devinfo.gmem_gpubaseaddr);
+ INFO_MSG(" GMEM size: 0x%08x", kgsl_pipe->devinfo.gmem_sizebytes);
+ INFO_MSG(" Driver version: %d.%d",
+ kgsl_pipe->version.drv_major, kgsl_pipe->version.drv_minor);
+ INFO_MSG(" Device version: %d.%d",
+ kgsl_pipe->version.dev_major, kgsl_pipe->version.dev_minor);
+
+ return pipe;
+fail:
+ if (pipe)
+ fd_pipe_del(pipe);
+ return NULL;
+}
diff --git a/freedreno/kgsl/kgsl_priv.h b/freedreno/kgsl/kgsl_priv.h
new file mode 100644
index 00000000..56dc21a5
--- /dev/null
+++ b/freedreno/kgsl/kgsl_priv.h
@@ -0,0 +1,115 @@
+/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
+
+/*
+ * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
+ *
+ * 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 (including the next
+ * paragraph) 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 OR COPYRIGHT HOLDERS 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.
+ *
+ * Authors:
+ * Rob Clark <robclark@freedesktop.org>
+ */
+
+#ifndef KGSL_PRIV_H_
+#define KGSL_PRIV_H_
+
+#include "freedreno_priv.h"
+#include "msm_kgsl.h"
+#include "kgsl_drm.h"
+
+struct kgsl_device {
+ struct fd_device base;
+};
+
+static inline struct kgsl_device * to_kgsl_device(struct fd_device *x)
+{
+ return (struct kgsl_device *)x;
+}
+
+struct kgsl_pipe {
+ struct fd_pipe base;
+
+ int fd;
+ uint32_t drawctxt_id;
+
+ /* device properties: */
+ struct kgsl_version version;
+ struct kgsl_devinfo devinfo;
+
+ /* list of bo's that are referenced in ringbuffer but not
+ * submitted yet:
+ */
+ struct list_head submit_list;
+
+ /* list of bo's that have been submitted but timestamp has
+ * not passed yet (so still ref'd in active cmdstream)
+ */
+ struct list_head pending_list;
+
+ /* if we are the 2d pipe, and want to wait on a timestamp
+ * from 3d, we need to also internally open the 3d pipe:
+ */
+ struct fd_pipe *p3d;
+};
+
+static inline struct kgsl_pipe * to_kgsl_pipe(struct fd_pipe *x)
+{
+ return (struct kgsl_pipe *)x;
+}
+
+int is_kgsl_pipe(struct fd_pipe *pipe);
+
+struct kgsl_bo {
+ struct fd_bo base;
+ uint64_t offset;
+ uint32_t gpuaddr;
+ /* timestamp (per pipe) for bo's in a pipe's pending_list: */
+ uint32_t timestamp[FD_PIPE_MAX];
+ /* list-node for pipe's submit_list or pending_list */
+ struct list_head list[FD_PIPE_MAX];
+};
+
+static inline struct kgsl_bo * to_kgsl_bo(struct fd_bo *x)
+{
+ return (struct kgsl_bo *)x;
+}
+
+
+struct fd_device * kgsl_device_new(int fd);
+
+int kgsl_pipe_timestamp(struct kgsl_pipe *kgsl_pipe, uint32_t *timestamp);
+void kgsl_pipe_add_submit(struct kgsl_pipe *pipe, struct kgsl_bo *bo);
+void kgsl_pipe_pre_submit(struct kgsl_pipe *pipe);
+void kgsl_pipe_post_submit(struct kgsl_pipe *pipe, uint32_t timestamp);
+void kgsl_pipe_process_pending(struct kgsl_pipe *pipe, uint32_t timestamp);
+struct fd_pipe * kgsl_pipe_new(struct fd_device *dev, enum fd_pipe_id id);
+
+struct fd_ringbuffer * kgsl_ringbuffer_new(struct fd_pipe *pipe,
+ uint32_t size);
+
+int kgsl_bo_new_handle(struct fd_device *dev,
+ uint32_t size, uint32_t flags, uint32_t *handle);
+struct fd_bo * kgsl_bo_from_handle(struct fd_device *dev,
+ uint32_t size, uint32_t handle);
+
+uint32_t kgsl_bo_gpuaddr(struct kgsl_bo *bo, uint32_t offset);
+void kgsl_bo_set_timestamp(struct kgsl_bo *bo, uint32_t timestamp);
+uint32_t kgsl_bo_get_timestamp(struct kgsl_bo *bo);
+
+#endif /* KGSL_PRIV_H_ */
diff --git a/freedreno/kgsl/kgsl_ringbuffer.c b/freedreno/kgsl/kgsl_ringbuffer.c
new file mode 100644
index 00000000..dc3c9c25
--- /dev/null
+++ b/freedreno/kgsl/kgsl_ringbuffer.c
@@ -0,0 +1,224 @@
+/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
+
+/*
+ * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
+ *
+ * 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 (including the next
+ * paragraph) 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 OR COPYRIGHT HOLDERS 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.
+ *
+ * Authors:
+ * Rob Clark <robclark@freedesktop.org>
+ */
+
+#include <assert.h>
+
+#include "freedreno_ringbuffer.h"
+#include "kgsl_priv.h"
+
+
+/* because kgsl tries to validate the gpuaddr on kernel side in ISSUEIBCMDS,
+ * we can't use normal gem bo's for ringbuffer.. someday the kernel part
+ * needs to be reworked into a single sane drm driver :-/
+ */
+struct kgsl_rb_bo {
+ struct kgsl_pipe *pipe;
+ void *hostptr;
+ uint32_t gpuaddr;
+ uint32_t size;
+};
+
+struct kgsl_ringbuffer {
+ struct fd_ringbuffer base;
+ struct kgsl_rb_bo *bo;
+};
+
+static inline struct kgsl_ringbuffer * to_kgsl_ringbuffer(struct fd_ringbuffer *x)
+{
+ return (struct kgsl_ringbuffer *)x;
+}
+
+static void kgsl_rb_bo_del(struct kgsl_rb_bo *bo)
+{
+ struct kgsl_sharedmem_free req = {
+ .gpuaddr = bo->gpuaddr,
+ };
+ int ret;
+
+ munmap(bo->hostptr, bo->size);
+
+ ret = ioctl(bo->pipe->fd, IOCTL_KGSL_SHAREDMEM_FREE, &req);
+ if (ret) {
+ ERROR_MSG("sharedmem free failed: %s", strerror(errno));
+ }
+
+ free(bo);
+}
+
+static struct kgsl_rb_bo * kgsl_rb_bo_new(struct kgsl_pipe *pipe, uint32_t size)
+{
+ struct kgsl_rb_bo *bo;
+ struct kgsl_gpumem_alloc req = {
+ .size = ALIGN(size, 4096),
+ .flags = KGSL_MEMFLAGS_GPUREADONLY,
+ };
+ int ret;
+
+ bo = calloc(1, sizeof(*bo));
+ if (!bo) {
+ ERROR_MSG("allocation failed");
+ return NULL;
+ }
+ ret = ioctl(pipe->fd, IOCTL_KGSL_GPUMEM_ALLOC, &req);
+ if (ret) {
+ ERROR_MSG("gpumem allocation failed: %s", strerror(errno));
+ goto fail;
+ }
+
+ bo->pipe = pipe;
+ bo->gpuaddr = req.gpuaddr;
+ bo->size = size;
+ bo->hostptr = mmap(NULL, size, PROT_WRITE|PROT_READ,
+ MAP_SHARED, pipe->fd, req.gpuaddr);
+
+ return bo;
+fail:
+ if (bo)
+ kgsl_rb_bo_del(bo);
+ return NULL;
+}
+
+static void * kgsl_ringbuffer_hostptr(struct fd_ringbuffer *ring)
+{
+ struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
+ return kgsl_ring->bo->hostptr;
+}
+
+static int kgsl_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start)
+{
+ struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
+ struct kgsl_pipe *kgsl_pipe = to_kgsl_pipe(ring->pipe);
+ uint32_t offset = (uint8_t *)last_start - (uint8_t *)ring->start;
+ struct kgsl_ibdesc ibdesc = {
+ .gpuaddr = kgsl_ring->bo->gpuaddr + offset,
+ .hostptr = last_start,
+ .sizedwords = ring->cur - last_start,
+ };
+ struct kgsl_ringbuffer_issueibcmds req = {
+ .drawctxt_id = kgsl_pipe->drawctxt_id,
+ .ibdesc_addr = (unsigned long)&ibdesc,
+ .numibs = 1,
+ .flags = KGSL_CONTEXT_SUBMIT_IB_LIST,
+ };
+ int ret;
+
+ kgsl_pipe_pre_submit(kgsl_pipe);
+
+ /* z180_cmdstream_issueibcmds() is made of fail: */
+ if (ring->pipe->id == FD_PIPE_2D) {
+ /* fix up size field in last cmd packet */
+ uint32_t last_size = (uint32_t)(ring->cur - last_start);
+ /* 5 is length of first packet, 2 for the two 7f000000's */
+ last_start[2] = last_size - (5 + 2);
+ ibdesc.gpuaddr = kgsl_ring->bo->gpuaddr;
+ ibdesc.hostptr = kgsl_ring->bo->hostptr;
+ ibdesc.sizedwords = 0x145;
+ req.timestamp = (uint32_t)kgsl_ring->bo->hostptr;
+ }
+
+ do {
+ ret = ioctl(kgsl_pipe->fd, IOCTL_KGSL_RINGBUFFER_ISSUEIBCMDS, &req);
+ } while ((ret == -1) && ((errno == EINTR) || (errno == EAGAIN)));
+ if (ret)
+ ERROR_MSG("issueibcmds failed! %d (%s)", ret, strerror(errno));
+
+ ring->last_timestamp = req.timestamp;
+ ring->last_start = ring->cur;
+
+ kgsl_pipe_post_submit(kgsl_pipe, req.timestamp);
+
+ return ret;
+}
+
+static void kgsl_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
+ const struct fd_reloc *r)
+{
+ struct kgsl_bo *kgsl_bo = to_kgsl_bo(r->bo);
+ uint32_t addr = kgsl_bo_gpuaddr(kgsl_bo, r->offset);
+ assert(addr);
+ if (r->shift < 0)
+ addr >>= -r->shift;
+ else
+ addr <<= r->shift;
+ (*ring->cur++) = addr | r->or;
+ kgsl_pipe_add_submit(to_kgsl_pipe(ring->pipe), kgsl_bo);
+}
+
+static void kgsl_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
+ struct fd_ringmarker *target, struct fd_ringmarker *end)
+{
+ struct kgsl_ringbuffer *target_ring = to_kgsl_ringbuffer(target->ring);
+ (*ring->cur++) = target_ring->bo->gpuaddr +
+ (uint8_t *)target->cur - (uint8_t *)target->ring->start;
+}
+
+static void kgsl_ringbuffer_destroy(struct fd_ringbuffer *ring)
+{
+ struct kgsl_ringbuffer *kgsl_ring = to_kgsl_ringbuffer(ring);
+ if (ring->last_timestamp)
+ fd_pipe_wait(ring->pipe, ring->last_timestamp);
+ if (kgsl_ring->bo)
+ kgsl_rb_bo_del(kgsl_ring->bo);
+ free(kgsl_ring);
+}
+
+static struct fd_ringbuffer_funcs funcs = {
+ .hostptr = kgsl_ringbuffer_hostptr,
+ .flush = kgsl_ringbuffer_flush,
+ .emit_reloc = kgsl_ringbuffer_emit_reloc,
+ .emit_reloc_ring = kgsl_ringbuffer_emit_reloc_ring,
+ .destroy = kgsl_ringbuffer_destroy,
+};
+
+struct fd_ringbuffer * kgsl_ringbuffer_new(struct fd_pipe *pipe,
+ uint32_t size)
+{
+ struct kgsl_ringbuffer *kgsl_ring;
+ struct fd_ringbuffer *ring = NULL;
+
+ kgsl_ring = calloc(1, sizeof(*kgsl_ring));
+ if (!kgsl_ring) {
+ ERROR_MSG("allocation failed");
+ goto fail;
+ }
+
+ ring = &kgsl_ring->base;
+ ring->funcs = &funcs;
+
+ kgsl_ring->bo = kgsl_rb_bo_new(to_kgsl_pipe(pipe), size);
+ if (!kgsl_ring->bo) {
+ ERROR_MSG("ringbuffer allocation failed");
+ goto fail;
+ }
+
+ return ring;
+fail:
+ if (ring)
+ fd_ringbuffer_del(ring);
+ return NULL;
+}
diff --git a/freedreno/msm_kgsl.h b/freedreno/kgsl/msm_kgsl.h
index e67190f0..e67190f0 100644
--- a/freedreno/msm_kgsl.h
+++ b/freedreno/kgsl/msm_kgsl.h