X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=freedreno%2Ffreedreno_bo.c;h=3a2e4649282e1e5eb015b8ff48befaf251c73de1;hb=479b6cef702f26d886b3f6ccfd48d5e39a623944;hp=f52ce5ea53ec46385837e98b038b0ad2a0ab6a24;hpb=0b89e2730c41466e8d9c04c469679ba23d052ec9;p=platform%2Fupstream%2Flibdrm.git diff --git a/freedreno/freedreno_bo.c b/freedreno/freedreno_bo.c index f52ce5e..3a2e464 100644 --- a/freedreno/freedreno_bo.c +++ b/freedreno/freedreno_bo.c @@ -29,10 +29,10 @@ #include "freedreno_drmif.h" #include "freedreno_priv.h" -#include - static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER; +static void bo_del(struct fd_bo *bo); + /* set buffer name, and add to table, call w/ table_lock held: */ static void set_name(struct fd_bo *bo, uint32_t name) { @@ -56,8 +56,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, @@ -69,132 +70,146 @@ static struct fd_bo * bo_from_handle(struct fd_device *dev, bo->size = size; bo->handle = handle; atomic_set(&bo->refcnt, 1); + list_inithead(&bo->list); /* 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) +/* Frees older cached buffers. Called under table_lock */ +void fd_cleanup_bo_cache(struct fd_device *dev, time_t time) { - struct drm_kgsl_gem_memtype req = { - .handle = bo->handle, - .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK, - }; + int i; + + if (dev->time == time) + return; + + for (i = 0; i < dev->num_buckets; i++) { + struct fd_bo_bucket *bucket = &dev->cache_bucket[i]; + struct fd_bo *bo; - return drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SETMEMTYPE, - &req, sizeof(req)); + while (!LIST_IS_EMPTY(&bucket->list)) { + bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list); + + /* keep things in cache for at least 1 second: */ + if (time && ((time - bo->free_time) <= 1)) + break; + + list_del(&bo->list); + bo_del(bo); + } + } + + dev->time = time; } -static int bo_alloc(struct fd_bo *bo) +static struct fd_bo_bucket * get_bucket(struct fd_device *dev, uint32_t size) { - if (!bo->offset) { - struct drm_kgsl_gem_alloc req = { - .handle = bo->handle, - }; - int ret; + int i; - /* 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; + /* hmm, this is what intel does, but I suppose we could calculate our + * way to the correct bucket size rather than looping.. + */ + for (i = 0; i < dev->num_buckets; i++) { + struct fd_bo_bucket *bucket = &dev->cache_bucket[i]; + if (bucket->size >= size) { + return bucket; } - - bo->offset = req.offset; } - return 0; + + return NULL; } -struct fd_bo * fd_bo_new(struct fd_device *dev, - uint32_t size, uint32_t flags) +static int is_idle(struct fd_bo *bo) { - struct drm_kgsl_gem_create req = { - .size = ALIGN(size, 4096), - }; - struct fd_bo *bo = NULL; + return fd_bo_cpu_prep(bo, NULL, + DRM_FREEDRENO_PREP_READ | + DRM_FREEDRENO_PREP_WRITE | + DRM_FREEDRENO_PREP_NOSYNC) == 0; +} - if (drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE, - &req, sizeof(req))) { - return NULL; - } +static struct fd_bo *find_in_bucket(struct fd_device *dev, + struct fd_bo_bucket *bucket, uint32_t flags) +{ + struct fd_bo *bo = NULL; + /* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could + * skip the busy check.. if it is only going to be a render target + * then we probably don't need to stall.. + * + * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail + * (MRU, since likely to be in GPU cache), rather than head (LRU).. + */ pthread_mutex_lock(&table_lock); - bo = bo_from_handle(dev, size, req.handle); - pthread_mutex_unlock(&table_lock); - if (!bo) { - goto fail; - } - - if (set_memtype(bo, flags)) { - goto fail; + while (!LIST_IS_EMPTY(&bucket->list)) { + bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list); + if (0 /* TODO: if madvise tells us bo is gone... */) { + list_del(&bo->list); + bo_del(bo); + bo = NULL; + continue; + } + /* TODO check for compatible flags? */ + if (is_idle(bo)) { + list_del(&bo->list); + break; + } + bo = NULL; + break; } + pthread_mutex_unlock(&table_lock); 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) + +drm_public struct fd_bo * +fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags) { - struct drm_kgsl_gem_create_fd req = { - .fd = fbfd, - }; - struct fd_bo *bo; + struct fd_bo *bo = NULL; + struct fd_bo_bucket *bucket; + uint32_t handle; + int ret; + + size = ALIGN(size, 4096); + bucket = get_bucket(dev, size); + + /* see if we can be green and recycle: */ + if (bucket) { + size = bucket->size; + bo = find_in_bucket(dev, bucket, flags); + if (bo) { + atomic_set(&bo->refcnt, 1); + fd_device_ref(bo->dev); + return bo; + } + } - if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD, - &req, sizeof(req))) { + ret = dev->funcs->bo_new_handle(dev, size, flags, &handle); + if (ret) return 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); + bo->bo_reuse = 1; pthread_mutex_unlock(&table_lock); return bo; -fail: +} + +drm_public struct fd_bo * +fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size) +{ + struct fd_bo *bo = NULL; + + pthread_mutex_lock(&table_lock); + bo = bo_from_handle(dev, size, handle); pthread_mutex_unlock(&table_lock); - if (bo) - fd_bo_del(bo); - return NULL; + + return bo; } -struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name) +drm_public struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name) { struct drm_gem_open req = { .name = name, @@ -227,35 +242,72 @@ out_unlock: return bo; } -struct fd_bo * fd_bo_ref(struct fd_bo *bo) +drm_public struct fd_bo * fd_bo_ref(struct fd_bo *bo) { atomic_inc(&bo->refcnt); return bo; } -void fd_bo_del(struct fd_bo *bo) +drm_public void fd_bo_del(struct fd_bo *bo) { + struct fd_device *dev = bo->dev; + if (!atomic_dec_and_test(&bo->refcnt)) return; + pthread_mutex_lock(&table_lock); + + if (bo->bo_reuse) { + struct fd_bo_bucket *bucket = get_bucket(dev, bo->size); + + /* see if we can be green and recycle: */ + if (bucket) { + struct timespec time; + + clock_gettime(CLOCK_MONOTONIC, &time); + + bo->free_time = time.tv_sec; + list_addtail(&bo->list, &bucket->list); + fd_cleanup_bo_cache(dev, time.tv_sec); + + /* bo's in the bucket cache don't have a ref and + * don't hold a ref to the dev: + */ + + goto out; + } + } + + bo_del(bo); +out: + fd_device_del_locked(dev); + pthread_mutex_unlock(&table_lock); +} + +/* Called under table_lock */ +static void bo_del(struct fd_bo *bo) +{ if (bo->map) munmap(bo->map, bo->size); + /* TODO probably bo's in bucket list get removed from + * handle table?? + */ + if (bo->handle) { struct drm_gem_close req = { .handle = bo->handle, }; - pthread_mutex_lock(&table_lock); drmHashDelete(bo->dev->handle_table, bo->handle); + if (bo->name) + drmHashDelete(bo->dev->name_table, bo->name); drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req); - pthread_mutex_unlock(&table_lock); } - fd_device_del(bo->dev); - free(bo); + bo->funcs->destroy(bo); } -int fd_bo_get_name(struct fd_bo *bo, uint32_t *name) +drm_public int fd_bo_get_name(struct fd_bo *bo, uint32_t *name) { if (!bo->name) { struct drm_gem_flink req = { @@ -278,28 +330,29 @@ int fd_bo_get_name(struct fd_bo *bo, uint32_t *name) return 0; } -uint32_t fd_bo_handle(struct fd_bo *bo) +drm_public uint32_t fd_bo_handle(struct fd_bo *bo) { return bo->handle; } -uint32_t fd_bo_size(struct fd_bo *bo) +drm_public uint32_t fd_bo_size(struct fd_bo *bo) { return bo->size; } -void * fd_bo_map(struct fd_bo *bo) +drm_public 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; @@ -308,88 +361,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.. */ +drm_public 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) +drm_public 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); }