1 /*
2 * Copyright 2019 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26 #include <errno.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <xf86drm.h>
30 #include <pthread.h>
31 #include "drm-uapi/panfrost_drm.h"
32
33 #include "pan_bo.h"
34 #include "pan_util.h"
35 #include "wrap.h"
36
37 #include "os/os_mman.h"
38
39 #include "util/u_inlines.h"
40 #include "util/u_math.h"
41
42 /* This file implements a userspace BO cache. Allocating and freeing
43 * GPU-visible buffers is very expensive, and even the extra kernel roundtrips
44 * adds more work than we would like at this point. So caching BOs in userspace
45 * 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 the
48 * nearest power-of-two. Each bucket contains a linked list of free panfrost_bo
49 * objects. Putting a BO into the cache is accomplished by adding it to the
50 * corresponding bucket. Getting a BO from the cache consists of finding the
51 * appropriate bucket and sorting. A cache eviction is a kernel-level free of a
52 * BO and removing it from the bucket. We special case evicting all BOs from
53 * the cache, since that's what helpful in practice and avoids extra logic
54 * around the linked list.
55 */
56
57 static struct panfrost_bo *
panfrost_bo_alloc(struct panfrost_device * dev,size_t size,uint32_t flags)58 panfrost_bo_alloc(struct panfrost_device *dev, size_t size,
59 uint32_t flags)
60 {
61 struct drm_panfrost_create_bo create_bo = { .size = size };
62 struct panfrost_bo *bo;
63 int ret;
64
65 if (dev->kernel_version->version_major > 1 ||
66 dev->kernel_version->version_minor >= 1) {
67 if (flags & PAN_BO_GROWABLE)
68 create_bo.flags |= PANFROST_BO_HEAP;
69 if (!(flags & PAN_BO_EXECUTE))
70 create_bo.flags |= PANFROST_BO_NOEXEC;
71 }
72
73 ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_CREATE_BO, &create_bo);
74 if (ret) {
75 fprintf(stderr, "DRM_IOCTL_PANFROST_CREATE_BO failed: %m\n");
76 return NULL;
77 }
78
79 bo = pan_lookup_bo(dev, create_bo.handle);
80 assert(!memcmp(bo, &((struct panfrost_bo){}), sizeof(*bo)));
81
82 bo->size = create_bo.size;
83 bo->ptr.gpu = create_bo.offset;
84 bo->gem_handle = create_bo.handle;
85 bo->flags = flags;
86 bo->dev = dev;
87 return bo;
88 }
89
90 static void
panfrost_bo_free(struct panfrost_bo * bo)91 panfrost_bo_free(struct panfrost_bo *bo)
92 {
93 struct drm_gem_close gem_close = { .handle = bo->gem_handle };
94 int ret;
95
96 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
97 if (ret) {
98 fprintf(stderr, "DRM_IOCTL_GEM_CLOSE failed: %m\n");
99 assert(0);
100 }
101
102 /* BO will be freed with the sparse array, but zero to indicate free */
103 memset(bo, 0, sizeof(*bo));
104 }
105
106 /* Returns true if the BO is ready, false otherwise.
107 * access_type is encoding the type of access one wants to ensure is done.
108 * Waiting is always done for writers, but if wait_readers is set then readers
109 * are also waited for.
110 */
111 bool
panfrost_bo_wait(struct panfrost_bo * bo,int64_t timeout_ns,bool wait_readers)112 panfrost_bo_wait(struct panfrost_bo *bo, int64_t timeout_ns, bool wait_readers)
113 {
114 struct drm_panfrost_wait_bo req = {
115 .handle = bo->gem_handle,
116 .timeout_ns = timeout_ns,
117 };
118 int ret;
119
120 /* If the BO has been exported or imported we can't rely on the cached
121 * state, we need to call the WAIT_BO ioctl.
122 */
123 if (!(bo->flags & PAN_BO_SHARED)) {
124 /* If ->gpu_access is 0, the BO is idle, no need to wait. */
125 if (!bo->gpu_access)
126 return true;
127
128 /* If the caller only wants to wait for writers and no
129 * writes are pending, we don't have to wait.
130 */
131 if (!wait_readers && !(bo->gpu_access & PAN_BO_ACCESS_WRITE))
132 return true;
133 }
134
135 /* The ioctl returns >= 0 value when the BO we are waiting for is ready
136 * -1 otherwise.
137 */
138 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PANFROST_WAIT_BO, &req);
139 if (ret != -1) {
140 /* Set gpu_access to 0 so that the next call to bo_wait()
141 * doesn't have to call the WAIT_BO ioctl.
142 */
143 bo->gpu_access = 0;
144 return true;
145 }
146
147 /* If errno is not ETIMEDOUT or EBUSY that means the handle we passed
148 * is invalid, which shouldn't happen here.
149 */
150 assert(errno == ETIMEDOUT || errno == EBUSY);
151 return false;
152 }
153
154 /* Helper to calculate the bucket index of a BO */
155
156 static unsigned
pan_bucket_index(unsigned size)157 pan_bucket_index(unsigned size)
158 {
159 /* Round down to POT to compute a bucket index */
160
161 unsigned bucket_index = util_logbase2(size);
162
163 /* Clamp the bucket index; all huge allocations will be
164 * sorted into the largest bucket */
165
166 bucket_index = MIN2(bucket_index, MAX_BO_CACHE_BUCKET);
167
168 /* The minimum bucket size must equal the minimum allocation
169 * size; the maximum we clamped */
170
171 assert(bucket_index >= MIN_BO_CACHE_BUCKET);
172 assert(bucket_index <= MAX_BO_CACHE_BUCKET);
173
174 /* Reindex from 0 */
175 return (bucket_index - MIN_BO_CACHE_BUCKET);
176 }
177
178 static struct list_head *
pan_bucket(struct panfrost_device * dev,unsigned size)179 pan_bucket(struct panfrost_device *dev, unsigned size)
180 {
181 return &dev->bo_cache.buckets[pan_bucket_index(size)];
182 }
183
184 /* Tries to fetch a BO of sufficient size with the appropriate flags from the
185 * BO cache. If it succeeds, it returns that BO and removes the BO from the
186 * cache. If it fails, it returns NULL signaling the caller to allocate a new
187 * BO. */
188
189 static struct panfrost_bo *
panfrost_bo_cache_fetch(struct panfrost_device * dev,size_t size,uint32_t flags,bool dontwait)190 panfrost_bo_cache_fetch(struct panfrost_device *dev,
191 size_t size, uint32_t flags, bool dontwait)
192 {
193 pthread_mutex_lock(&dev->bo_cache.lock);
194 struct list_head *bucket = pan_bucket(dev, size);
195 struct panfrost_bo *bo = NULL;
196
197 /* Iterate the bucket looking for something suitable */
198 list_for_each_entry_safe(struct panfrost_bo, entry, bucket,
199 bucket_link) {
200 if (entry->size < size || entry->flags != flags)
201 continue;
202
203 if (!panfrost_bo_wait(entry, dontwait ? 0 : INT64_MAX,
204 PAN_BO_ACCESS_RW))
205 continue;
206
207 struct drm_panfrost_madvise madv = {
208 .handle = entry->gem_handle,
209 .madv = PANFROST_MADV_WILLNEED,
210 };
211 int ret;
212
213 /* This one works, splice it out of the cache */
214 list_del(&entry->bucket_link);
215 list_del(&entry->lru_link);
216
217 ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_MADVISE, &madv);
218 if (!ret && !madv.retained) {
219 panfrost_bo_free(entry);
220 continue;
221 }
222 /* Let's go! */
223 bo = entry;
224 break;
225 }
226 pthread_mutex_unlock(&dev->bo_cache.lock);
227
228 return bo;
229 }
230
231 static void
panfrost_bo_cache_evict_stale_bos(struct panfrost_device * dev)232 panfrost_bo_cache_evict_stale_bos(struct panfrost_device *dev)
233 {
234 struct timespec time;
235
236 clock_gettime(CLOCK_MONOTONIC, &time);
237 list_for_each_entry_safe(struct panfrost_bo, entry,
238 &dev->bo_cache.lru, lru_link) {
239 /* We want all entries that have been used more than 1 sec
240 * ago to be dropped, others can be kept.
241 * Note the <= 2 check and not <= 1. It's here to account for
242 * the fact that we're only testing ->tv_sec, not ->tv_nsec.
243 * That means we might keep entries that are between 1 and 2
244 * seconds old, but we don't really care, as long as unused BOs
245 * are dropped at some point.
246 */
247 if (time.tv_sec - entry->last_used <= 2)
248 break;
249
250 list_del(&entry->bucket_link);
251 list_del(&entry->lru_link);
252 panfrost_bo_free(entry);
253 }
254 }
255
256 /* Tries to add a BO to the cache. Returns if it was
257 * successful */
258
259 static bool
panfrost_bo_cache_put(struct panfrost_bo * bo)260 panfrost_bo_cache_put(struct panfrost_bo *bo)
261 {
262 struct panfrost_device *dev = bo->dev;
263
264 if (bo->flags & PAN_BO_SHARED)
265 return false;
266
267 pthread_mutex_lock(&dev->bo_cache.lock);
268 struct list_head *bucket = pan_bucket(dev, MAX2(bo->size, 4096));
269 struct drm_panfrost_madvise madv;
270 struct timespec time;
271
272 madv.handle = bo->gem_handle;
273 madv.madv = PANFROST_MADV_DONTNEED;
274 madv.retained = 0;
275
276 drmIoctl(dev->fd, DRM_IOCTL_PANFROST_MADVISE, &madv);
277
278 /* Add us to the bucket */
279 list_addtail(&bo->bucket_link, bucket);
280
281 /* Add us to the LRU list and update the last_used field. */
282 list_addtail(&bo->lru_link, &dev->bo_cache.lru);
283 clock_gettime(CLOCK_MONOTONIC, &time);
284 bo->last_used = time.tv_sec;
285
286 /* Let's do some cleanup in the BO cache while we hold the
287 * lock.
288 */
289 panfrost_bo_cache_evict_stale_bos(dev);
290 pthread_mutex_unlock(&dev->bo_cache.lock);
291
292 return true;
293 }
294
295 /* Evicts all BOs from the cache. Called during context
296 * destroy or during low-memory situations (to free up
297 * memory that may be unused by us just sitting in our
298 * cache, but still reserved from the perspective of the
299 * OS) */
300
301 void
panfrost_bo_cache_evict_all(struct panfrost_device * dev)302 panfrost_bo_cache_evict_all(
303 struct panfrost_device *dev)
304 {
305 pthread_mutex_lock(&dev->bo_cache.lock);
306 for (unsigned i = 0; i < ARRAY_SIZE(dev->bo_cache.buckets); ++i) {
307 struct list_head *bucket = &dev->bo_cache.buckets[i];
308
309 list_for_each_entry_safe(struct panfrost_bo, entry, bucket,
310 bucket_link) {
311 list_del(&entry->bucket_link);
312 list_del(&entry->lru_link);
313 panfrost_bo_free(entry);
314 }
315 }
316 pthread_mutex_unlock(&dev->bo_cache.lock);
317 }
318
319 void
panfrost_bo_mmap(struct panfrost_bo * bo)320 panfrost_bo_mmap(struct panfrost_bo *bo)
321 {
322 struct drm_panfrost_mmap_bo mmap_bo = { .handle = bo->gem_handle };
323 int ret;
324
325 if (bo->ptr.cpu)
326 return;
327
328 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PANFROST_MMAP_BO, &mmap_bo);
329 if (ret) {
330 fprintf(stderr, "DRM_IOCTL_PANFROST_MMAP_BO failed: %m\n");
331 assert(0);
332 }
333
334 bo->ptr.cpu = os_mmap(NULL, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
335 bo->dev->fd, mmap_bo.offset);
336 if (bo->ptr.cpu == MAP_FAILED) {
337 fprintf(stderr, "mmap failed: %p %m\n", bo->ptr.cpu);
338 assert(0);
339 }
340 }
341
342 static void
panfrost_bo_munmap(struct panfrost_bo * bo)343 panfrost_bo_munmap(struct panfrost_bo *bo)
344 {
345 if (!bo->ptr.cpu)
346 return;
347
348 if (os_munmap((void *) (uintptr_t)bo->ptr.cpu, bo->size)) {
349 perror("munmap");
350 abort();
351 }
352
353 bo->ptr.cpu = NULL;
354 }
355
356 struct panfrost_bo *
panfrost_bo_create(struct panfrost_device * dev,size_t size,uint32_t flags)357 panfrost_bo_create(struct panfrost_device *dev, size_t size,
358 uint32_t flags)
359 {
360 struct panfrost_bo *bo;
361
362 /* Kernel will fail (confusingly) with EPERM otherwise */
363 assert(size > 0);
364
365 /* To maximize BO cache usage, don't allocate tiny BOs */
366 size = MAX2(size, 4096);
367
368 /* GROWABLE BOs cannot be mmapped */
369 if (flags & PAN_BO_GROWABLE)
370 assert(flags & PAN_BO_INVISIBLE);
371
372 /* Before creating a BO, we first want to check the cache but without
373 * waiting for BO readiness (BOs in the cache can still be referenced
374 * by jobs that are not finished yet).
375 * If the cached allocation fails we fall back on fresh BO allocation,
376 * and if that fails too, we try one more time to allocate from the
377 * cache, but this time we accept to wait.
378 */
379 bo = panfrost_bo_cache_fetch(dev, size, flags, true);
380 if (!bo)
381 bo = panfrost_bo_alloc(dev, size, flags);
382 if (!bo)
383 bo = panfrost_bo_cache_fetch(dev, size, flags, false);
384
385 if (!bo)
386 fprintf(stderr, "BO creation failed\n");
387
388 assert(bo);
389
390 /* Only mmap now if we know we need to. For CPU-invisible buffers, we
391 * never map since we don't care about their contents; they're purely
392 * for GPU-internal use. But we do trace them anyway. */
393
394 if (!(flags & (PAN_BO_INVISIBLE | PAN_BO_DELAY_MMAP)))
395 panfrost_bo_mmap(bo);
396
397 p_atomic_set(&bo->refcnt, 1);
398
399 if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC)) {
400 if (flags & PAN_BO_INVISIBLE)
401 pandecode_inject_mmap(bo->ptr.gpu, NULL, bo->size, NULL);
402 else if (!(flags & PAN_BO_DELAY_MMAP))
403 pandecode_inject_mmap(bo->ptr.gpu, bo->ptr.cpu, bo->size, NULL);
404 }
405
406 return bo;
407 }
408
409 void
panfrost_bo_reference(struct panfrost_bo * bo)410 panfrost_bo_reference(struct panfrost_bo *bo)
411 {
412 if (bo) {
413 ASSERTED int count = p_atomic_inc_return(&bo->refcnt);
414 assert(count != 1);
415 }
416 }
417
418 void
panfrost_bo_unreference(struct panfrost_bo * bo)419 panfrost_bo_unreference(struct panfrost_bo *bo)
420 {
421 if (!bo)
422 return;
423
424 /* Don't return to cache if there are still references */
425 if (p_atomic_dec_return(&bo->refcnt))
426 return;
427
428 struct panfrost_device *dev = bo->dev;
429
430 pthread_mutex_lock(&dev->bo_map_lock);
431
432 /* Someone might have imported this BO while we were waiting for the
433 * lock, let's make sure it's still not referenced before freeing it.
434 */
435 if (p_atomic_read(&bo->refcnt) == 0) {
436 /* When the reference count goes to zero, we need to cleanup */
437 panfrost_bo_munmap(bo);
438
439 if (dev->debug & (PAN_DBG_TRACE | PAN_DBG_SYNC))
440 pandecode_inject_free(bo->ptr.gpu, bo->size);
441
442 /* Rather than freeing the BO now, we'll cache the BO for later
443 * allocations if we're allowed to.
444 */
445 if (!panfrost_bo_cache_put(bo))
446 panfrost_bo_free(bo);
447
448 }
449 pthread_mutex_unlock(&dev->bo_map_lock);
450 }
451
452 struct panfrost_bo *
panfrost_bo_import(struct panfrost_device * dev,int fd)453 panfrost_bo_import(struct panfrost_device *dev, int fd)
454 {
455 struct panfrost_bo *bo;
456 struct drm_panfrost_get_bo_offset get_bo_offset = {0,};
457 ASSERTED int ret;
458 unsigned gem_handle;
459
460 ret = drmPrimeFDToHandle(dev->fd, fd, &gem_handle);
461 assert(!ret);
462
463 pthread_mutex_lock(&dev->bo_map_lock);
464 bo = pan_lookup_bo(dev, gem_handle);
465
466 if (!bo->dev) {
467 get_bo_offset.handle = gem_handle;
468 ret = drmIoctl(dev->fd, DRM_IOCTL_PANFROST_GET_BO_OFFSET, &get_bo_offset);
469 assert(!ret);
470
471 bo->dev = dev;
472 bo->ptr.gpu = (mali_ptr) get_bo_offset.offset;
473 bo->size = lseek(fd, 0, SEEK_END);
474 bo->flags = PAN_BO_SHARED;
475 bo->gem_handle = gem_handle;
476 assert(bo->size > 0);
477 p_atomic_set(&bo->refcnt, 1);
478 // TODO map and unmap on demand?
479 panfrost_bo_mmap(bo);
480 } else {
481 /* bo->refcnt == 0 can happen if the BO
482 * was being released but panfrost_bo_import() acquired the
483 * lock before panfrost_bo_unreference(). In that case, refcnt
484 * is 0 and we can't use panfrost_bo_reference() directly, we
485 * have to re-initialize the refcnt().
486 * Note that panfrost_bo_unreference() checks
487 * refcnt value just after acquiring the lock to
488 * make sure the object is not freed if panfrost_bo_import()
489 * acquired it in the meantime.
490 */
491 if (p_atomic_read(&bo->refcnt) == 0)
492 p_atomic_set(&bo->refcnt, 1);
493 else
494 panfrost_bo_reference(bo);
495 assert(bo->ptr.cpu);
496 }
497 pthread_mutex_unlock(&dev->bo_map_lock);
498
499 return bo;
500 }
501
502 int
panfrost_bo_export(struct panfrost_bo * bo)503 panfrost_bo_export(struct panfrost_bo *bo)
504 {
505 struct drm_prime_handle args = {
506 .handle = bo->gem_handle,
507 .flags = DRM_CLOEXEC,
508 };
509
510 int ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
511 if (ret == -1)
512 return -1;
513
514 bo->flags |= PAN_BO_SHARED;
515 return args.fd;
516 }
517
518