/*1* Copyright 2019 Collabora, Ltd.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*22* Authors (Collabora):23* Alyssa Rosenzweig <[email protected]>24*/25#include <errno.h>26#include <stdio.h>27#include <fcntl.h>28#include <xf86drm.h>29#include <pthread.h>30#include "drm-uapi/panfrost_drm.h"3132#include "pan_bo.h"33#include "pan_device.h"34#include "pan_util.h"35#include "wrap.h"3637#include "os/os_mman.h"3839#include "util/u_inlines.h"40#include "util/u_math.h"4142/* This file implements a userspace BO cache. Allocating and freeing43* GPU-visible buffers is very expensive, and even the extra kernel roundtrips44* adds more work than we would like at this point. So caching BOs in userspace45* solves both of these problems and does not require kernel updates.46*47* Cached BOs are sorted into a bucket based on rounding their size down to the48* nearest power-of-two. Each bucket contains a linked list of free panfrost_bo49* objects. Putting a BO into the cache is accomplished by adding it to the50* corresponding bucket. Getting a BO from the cache consists of finding the51* appropriate bucket and sorting. A cache eviction is a kernel-level free of a52* BO and removing it from the bucket. We special case evicting all BOs from53* the cache, since that's what helpful in practice and avoids extra logic54* around the linked list.55*/5657static struct panfrost_bo *58panfrost_bo_alloc(struct panfrost_device *dev, size_t size,59uint32_t flags, const char *label)60{61struct drm_panfrost_create_bo create_bo = { .size = size };62struct panfrost_bo *bo;63int ret;6465if (dev->kernel_version->version_major > 1 ||66dev->kernel_version->version_minor >= 1) {67if (flags & PAN_BO_GROWABLE)68create_bo.flags |= PANFROST_BO_HEAP;69if (!(flags & PAN_BO_EXECUTE))70create_bo.flags |= PANFROST_BO_NOEXEC;71}7273ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);74if (ret) {75fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %m\n");76return NULL;77}7879bo = pan_lookup_bo(dev, create_bo.handle);80assert(!memcmp(bo, &((struct panfrost_bo){}), sizeof(*bo)));8182bo->size = create_bo.size;83bo->ptr.gpu = create_bo.offset;84bo->gem_handle = create_bo.handle;85bo->flags = flags;86bo->dev = dev;87bo->label = label;88return bo;89}9091static void92panfrost_bo_free(struct panfrost_bo *bo)93{94struct drm_gem_close gem_close = { .handle = bo->gem_handle };95int ret;9697ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);98if (ret) {99fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %m\n");100assert(0);101}102103/* BO will be freed with the sparse array, but zero to indicate free */104memset(bo, 0, sizeof(*bo));105}106107/* Returns true if the BO is ready, false otherwise.108* access_type is encoding the type of access one wants to ensure is done.109* Waiting is always done for writers, but if wait_readers is set then readers110* are also waited for.111*/112bool113panfrost_bo_wait(struct panfrost_bo *bo, int64_t timeout_ns, bool wait_readers)114{115struct drm_panfrost_wait_bo req = {116.handle = bo->gem_handle,117.timeout_ns = timeout_ns,118};119int ret;120121/* If the BO has been exported or imported we can't rely on the cached122* state, we need to call the WAIT_BO ioctl.123*/124if (!(bo->flags & PAN_BO_SHARED)) {125/* If ->gpu_access is 0, the BO is idle, no need to wait. */126if (!bo->gpu_access)127return true;128129/* If the caller only wants to wait for writers and no130* writes are pending, we don't have to wait.131*/132if (!wait_readers && !(bo->gpu_access & PAN_BO_ACCESS_WRITE))133return true;134}135136/* The ioctl returns >= 0 value when the BO we are waiting for is ready137* -1 otherwise.138*/139ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PANFROST_WAIT_BO, &req);140if (ret != -1) {141/* Set gpu_access to 0 so that the next call to bo_wait()142* doesn't have to call the WAIT_BO ioctl.143*/144bo->gpu_access = 0;145return true;146}147148/* If errno is not ETIMEDOUT or EBUSY that means the handle we passed149* is invalid, which shouldn't happen here.150*/151assert(errno == ETIMEDOUT || errno == EBUSY);152return false;153}154155/* Helper to calculate the bucket index of a BO */156157static unsigned158pan_bucket_index(unsigned size)159{160/* Round down to POT to compute a bucket index */161162unsigned bucket_index = util_logbase2(size);163164/* Clamp the bucket index; all huge allocations will be165* sorted into the largest bucket */166167bucket_index = MIN2(bucket_index, MAX_BO_CACHE_BUCKET);168169/* The minimum bucket size must equal the minimum allocation170* size; the maximum we clamped */171172assert(bucket_index >= MIN_BO_CACHE_BUCKET);173assert(bucket_index <= MAX_BO_CACHE_BUCKET);174175/* Reindex from 0 */176return (bucket_index - MIN_BO_CACHE_BUCKET);177}178179static struct list_head *180pan_bucket(struct panfrost_device *dev, unsigned size)181{182return &dev->bo_cache.buckets[pan_bucket_index(size)];183}184185/* Tries to fetch a BO of sufficient size with the appropriate flags from the186* BO cache. If it succeeds, it returns that BO and removes the BO from the187* cache. If it fails, it returns NULL signaling the caller to allocate a new188* BO. */189190static struct panfrost_bo *191panfrost_bo_cache_fetch(struct panfrost_device *dev,192size_t size, uint32_t flags, const char *label,193bool dontwait)194{195pthread_mutex_lock(&dev->bo_cache.lock);196struct list_head *bucket = pan_bucket(dev, size);197struct panfrost_bo *bo = NULL;198199/* Iterate the bucket looking for something suitable */200list_for_each_entry_safe(struct panfrost_bo, entry, bucket,201bucket_link) {202if (entry->size < size || entry->flags != flags)203continue;204205/* If the oldest BO in the cache is busy, likely so is206* everything newer, so bail. */207if (!panfrost_bo_wait(entry, dontwait ? 0 : INT64_MAX,208PAN_BO_ACCESS_RW))209break;210211struct drm_panfrost_madvise madv = {212.handle = entry->gem_handle,213.madv = PANFROST_MADV_WILLNEED,214};215int ret;216217/* This one works, splice it out of the cache */218list_del(&entry->bucket_link);219list_del(&entry->lru_link);220221ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_MADVISE, &madv);222if (!ret && !madv.retained) {223panfrost_bo_free(entry);224continue;225}226/* Let's go! */227bo = entry;228bo->label = label;229break;230}231pthread_mutex_unlock(&dev->bo_cache.lock);232233return bo;234}235236static void237panfrost_bo_cache_evict_stale_bos(struct panfrost_device *dev)238{239struct timespec time;240241clock_gettime(CLOCK_MONOTONIC, &time);242list_for_each_entry_safe(struct panfrost_bo, entry,243&dev->bo_cache.lru, lru_link) {244/* We want all entries that have been used more than 1 sec245* ago to be dropped, others can be kept.246* Note the <= 2 check and not <= 1. It's here to account for247* the fact that we're only testing ->tv_sec, not ->tv_nsec.248* That means we might keep entries that are between 1 and 2249* seconds old, but we don't really care, as long as unused BOs250* are dropped at some point.251*/252if (time.tv_sec - entry->last_used <= 2)253break;254255list_del(&entry->bucket_link);256list_del(&entry->lru_link);257panfrost_bo_free(entry);258}259}260261/* Tries to add a BO to the cache. Returns if it was262* successful */263264static bool265panfrost_bo_cache_put(struct panfrost_bo *bo)266{267struct panfrost_device *dev = bo->dev;268269if (bo->flags & PAN_BO_SHARED)270return false;271272pthread_mutex_lock(&dev->bo_cache.lock);273struct list_head *bucket = pan_bucket(dev, MAX2(bo->size, 4096));274struct drm_panfrost_madvise madv;275struct timespec time;276277madv.handle = bo->gem_handle;278madv.madv = PANFROST_MADV_DONTNEED;279madv.retained = 0;280281drmIoctl(dev->fd, DRM_IOCTL_PANFROST_MADVISE, &madv);282283/* Add us to the bucket */284list_addtail(&bo->bucket_link, bucket);285286/* Add us to the LRU list and update the last_used field. */287list_addtail(&bo->lru_link, &dev->bo_cache.lru);288clock_gettime(CLOCK_MONOTONIC, &time);289bo->last_used = time.tv_sec;290291/* Let's do some cleanup in the BO cache while we hold the292* lock.293*/294panfrost_bo_cache_evict_stale_bos(dev);295pthread_mutex_unlock(&dev->bo_cache.lock);296297/* Update the label to help debug BO cache memory usage issues */298bo->label = "Unused (BO cache)";299300return true;301}302303/* Evicts all BOs from the cache. Called during context304* destroy or during low-memory situations (to free up305* memory that may be unused by us just sitting in our306* cache, but still reserved from the perspective of the307* OS) */308309void310panfrost_bo_cache_evict_all(311struct panfrost_device *dev)312{313pthread_mutex_lock(&dev->bo_cache.lock);314for (unsigned i = 0; i < ARRAY_SIZE(dev->bo_cache.buckets); ++i) {315struct list_head *bucket = &dev->bo_cache.buckets[i];316317list_for_each_entry_safe(struct panfrost_bo, entry, bucket,318bucket_link) {319list_del(&entry->bucket_link);320list_del(&entry->lru_link);321panfrost_bo_free(entry);322}323}324pthread_mutex_unlock(&dev->bo_cache.lock);325}326327void328panfrost_bo_mmap(struct panfrost_bo *bo)329{330struct drm_panfrost_mmap_bo mmap_bo = { .handle = bo->gem_handle };331int ret;332333if (bo->ptr.cpu)334return;335336ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);337if (ret) {338fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %m\n");339assert(0);340}341342bo->ptr.cpu = os_mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,343bo->dev->fd, mmap_bo.offset);344if (bo->ptr.cpu == MAP_FAILED) {345bo->ptr.cpu = NULL;346fprintf(stderr,347"mmap failed: result=%p size=0x%llx fd=%i offset=0x%llx %m\n",348bo->ptr.cpu, (long long)bo->size, bo->dev->fd,349(long long)mmap_bo.offset);350}351}352353static void354panfrost_bo_munmap(struct panfrost_bo *bo)355{356if (!bo->ptr.cpu)357return;358359if (os_munmap((void *) (uintptr_t)bo->ptr.cpu, bo->size)) {360perror("munmap");361abort();362}363364bo->ptr.cpu = NULL;365}366367struct panfrost_bo *368panfrost_bo_create(struct panfrost_device *dev, size_t size,369uint32_t flags, const char *label)370{371struct panfrost_bo *bo;372373/* Kernel will fail (confusingly) with EPERM otherwise */374assert(size > 0);375376/* To maximize BO cache usage, don't allocate tiny BOs */377size = ALIGN_POT(size, 4096);378379/* GROWABLE BOs cannot be mmapped */380if (flags & PAN_BO_GROWABLE)381assert(flags & PAN_BO_INVISIBLE);382383/* Before creating a BO, we first want to check the cache but without384* waiting for BO readiness (BOs in the cache can still be referenced385* by jobs that are not finished yet).386* If the cached allocation fails we fall back on fresh BO allocation,387* and if that fails too, we try one more time to allocate from the388* cache, but this time we accept to wait.389*/390bo = panfrost_bo_cache_fetch(dev, size, flags, label, true);391if (!bo)392bo = panfrost_bo_alloc(dev, size, flags, label);393if (!bo)394bo = panfrost_bo_cache_fetch(dev, size, flags, label, false);395396if (!bo)397fprintf(stderr, "BO creation failed\n");398399assert(bo);400401/* Only mmap now if we know we need to. For CPU-invisible buffers, we402* never map since we don't care about their contents; they're purely403* for GPU-internal use. But we do trace them anyway. */404405if (!(flags & (PAN_BO_INVISIBLE | PAN_BO_DELAY_MMAP)))406panfrost_bo_mmap(bo);407408p_atomic_set(&bo->refcnt, 1);409410if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {411if (flags & PAN_BO_INVISIBLE)412pandecode_inject_mmap(bo->ptr.gpu, NULL, bo->size, NULL);413else if (!(flags & PAN_BO_DELAY_MMAP))414pandecode_inject_mmap(bo->ptr.gpu, bo->ptr.cpu, bo->size, NULL);415}416417return bo;418}419420void421panfrost_bo_reference(struct panfrost_bo *bo)422{423if (bo) {424ASSERTED int count = p_atomic_inc_return(&bo->refcnt);425assert(count != 1);426}427}428429void430panfrost_bo_unreference(struct panfrost_bo *bo)431{432if (!bo)433return;434435/* Don't return to cache if there are still references */436if (p_atomic_dec_return(&bo->refcnt))437return;438439struct panfrost_device *dev = bo->dev;440441pthread_mutex_lock(&dev->bo_map_lock);442443/* Someone might have imported this BO while we were waiting for the444* lock, let's make sure it's still not referenced before freeing it.445*/446if (p_atomic_read(&bo->refcnt) == 0) {447/* When the reference count goes to zero, we need to cleanup */448panfrost_bo_munmap(bo);449450if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))451pandecode_inject_free(bo->ptr.gpu, bo->size);452453/* Rather than freeing the BO now, we'll cache the BO for later454* allocations if we're allowed to.455*/456if (!panfrost_bo_cache_put(bo))457panfrost_bo_free(bo);458459}460pthread_mutex_unlock(&dev->bo_map_lock);461}462463struct panfrost_bo *464panfrost_bo_import(struct panfrost_device *dev, int fd)465{466struct panfrost_bo *bo;467struct drm_panfrost_get_bo_offset get_bo_offset = {0,};468ASSERTED int ret;469unsigned gem_handle;470471ret = drmPrimeFDToHandle(dev->fd, fd, &gem_handle);472assert(!ret);473474pthread_mutex_lock(&dev->bo_map_lock);475bo = pan_lookup_bo(dev, gem_handle);476477if (!bo->dev) {478get_bo_offset.handle = gem_handle;479ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);480assert(!ret);481482bo->dev = dev;483bo->ptr.gpu = (mali_ptr) get_bo_offset.offset;484bo->size = lseek(fd, 0, SEEK_END);485/* Sometimes this can fail and return -1. size of -1 is not486* a nice thing for mmap to try mmap. Be more robust also487* for zero sized maps and fail nicely too488*/489if ((bo->size == 0) || (bo->size == (size_t)-1)) {490pthread_mutex_unlock(&dev->bo_map_lock);491return NULL;492}493bo->flags = PAN_BO_SHARED;494bo->gem_handle = gem_handle;495p_atomic_set(&bo->refcnt, 1);496// TODO map and unmap on demand?497panfrost_bo_mmap(bo);498} else {499/* bo->refcnt == 0 can happen if the BO500* was being released but panfrost_bo_import() acquired the501* lock before panfrost_bo_unreference(). In that case, refcnt502* is 0 and we can't use panfrost_bo_reference() directly, we503* have to re-initialize the refcnt().504* Note that panfrost_bo_unreference() checks505* refcnt value just after acquiring the lock to506* make sure the object is not freed if panfrost_bo_import()507* acquired it in the meantime.508*/509if (p_atomic_read(&bo->refcnt) == 0)510p_atomic_set(&bo->refcnt, 1);511else512panfrost_bo_reference(bo);513assert(bo->ptr.cpu);514}515pthread_mutex_unlock(&dev->bo_map_lock);516517return bo;518}519520int521panfrost_bo_export(struct panfrost_bo *bo)522{523struct drm_prime_handle args = {524.handle = bo->gem_handle,525.flags = DRM_CLOEXEC,526};527528int ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);529if (ret == -1)530return -1;531532bo->flags |= PAN_BO_SHARED;533return args.fd;534}535536537538