2 * Copyright 2019 Collabora, Ltd.
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:
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
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
23 * Authors (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
30 #include "drm-uapi/panfrost_drm.h"
33 #include "pan_screen.h"
35 #include "pandecode/decode.h"
37 #include "os/os_mman.h"
39 #include "util/u_inlines.h"
40 #include "util/u_math.h"
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.
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.
57 /* Helper to calculate the bucket index of a BO */
60 pan_bucket_index(unsigned size
)
62 /* Round down to POT to compute a bucket index */
64 unsigned bucket_index
= util_logbase2(size
);
66 /* Clamp the bucket index; all huge allocations will be
67 * sorted into the largest bucket */
69 bucket_index
= MIN2(bucket_index
, MAX_BO_CACHE_BUCKET
);
71 /* The minimum bucket size must equal the minimum allocation
72 * size; the maximum we clamped */
74 assert(bucket_index
>= MIN_BO_CACHE_BUCKET
);
75 assert(bucket_index
<= MAX_BO_CACHE_BUCKET
);
78 return (bucket_index
- MIN_BO_CACHE_BUCKET
);
81 static struct list_head
*
82 pan_bucket(struct panfrost_screen
*screen
, unsigned size
)
84 return &screen
->bo_cache
[pan_bucket_index(size
)];
87 /* Tries to fetch a BO of sufficient size with the appropriate flags from the
88 * BO cache. If it succeeds, it returns that BO and removes the BO from the
89 * cache. If it fails, it returns NULL signaling the caller to allocate a new
92 static struct panfrost_bo
*
93 panfrost_bo_cache_fetch(
94 struct panfrost_screen
*screen
,
95 size_t size
, uint32_t flags
)
97 pthread_mutex_lock(&screen
->bo_cache_lock
);
98 struct list_head
*bucket
= pan_bucket(screen
, size
);
99 struct panfrost_bo
*bo
= NULL
;
101 /* Iterate the bucket looking for something suitable */
102 list_for_each_entry_safe(struct panfrost_bo
, entry
, bucket
, link
) {
103 if (entry
->size
>= size
&&
104 entry
->flags
== flags
) {
106 struct drm_panfrost_madvise madv
;
108 /* This one works, splice it out of the cache */
109 list_del(&entry
->link
);
111 madv
.handle
= entry
->gem_handle
;
112 madv
.madv
= PANFROST_MADV_WILLNEED
;
115 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_MADVISE
, &madv
);
116 if (!ret
&& !madv
.retained
) {
117 panfrost_bo_release(entry
, false);
125 pthread_mutex_unlock(&screen
->bo_cache_lock
);
130 /* Tries to add a BO to the cache. Returns if it was
134 panfrost_bo_cache_put(struct panfrost_bo
*bo
)
136 struct panfrost_screen
*screen
= bo
->screen
;
138 pthread_mutex_lock(&screen
->bo_cache_lock
);
139 struct list_head
*bucket
= pan_bucket(screen
, bo
->size
);
140 struct drm_panfrost_madvise madv
;
142 madv
.handle
= bo
->gem_handle
;
143 madv
.madv
= PANFROST_MADV_DONTNEED
;
146 drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_MADVISE
, &madv
);
148 /* Add us to the bucket */
149 list_addtail(&bo
->link
, bucket
);
150 pthread_mutex_unlock(&screen
->bo_cache_lock
);
155 /* Evicts all BOs from the cache. Called during context
156 * destroy or during low-memory situations (to free up
157 * memory that may be unused by us just sitting in our
158 * cache, but still reserved from the perspective of the
162 panfrost_bo_cache_evict_all(
163 struct panfrost_screen
*screen
)
165 pthread_mutex_lock(&screen
->bo_cache_lock
);
166 for (unsigned i
= 0; i
< ARRAY_SIZE(screen
->bo_cache
); ++i
) {
167 struct list_head
*bucket
= &screen
->bo_cache
[i
];
169 list_for_each_entry_safe(struct panfrost_bo
, entry
, bucket
, link
) {
170 list_del(&entry
->link
);
171 panfrost_bo_release(entry
, false);
174 pthread_mutex_unlock(&screen
->bo_cache_lock
);
178 panfrost_bo_mmap(struct panfrost_bo
*bo
)
180 struct drm_panfrost_mmap_bo mmap_bo
= { .handle
= bo
->gem_handle
};
186 ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_PANFROST_MMAP_BO
, &mmap_bo
);
188 fprintf(stderr
, "DRM_IOCTL_PANFROST_MMAP_BO failed: %m\n");
192 bo
->cpu
= os_mmap(NULL
, bo
->size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
,
193 bo
->screen
->fd
, mmap_bo
.offset
);
194 if (bo
->cpu
== MAP_FAILED
) {
195 fprintf(stderr
, "mmap failed: %p %m\n", bo
->cpu
);
199 /* Record the mmap if we're tracing */
200 if (pan_debug
& PAN_DBG_TRACE
)
201 pandecode_inject_mmap(bo
->gpu
, bo
->cpu
, bo
->size
, NULL
);
205 panfrost_bo_munmap(struct panfrost_bo
*bo
)
210 if (os_munmap((void *) (uintptr_t)bo
->cpu
, bo
->size
)) {
219 panfrost_bo_create(struct panfrost_screen
*screen
, size_t size
,
222 struct panfrost_bo
*bo
;
224 /* Kernel will fail (confusingly) with EPERM otherwise */
227 /* To maximize BO cache usage, don't allocate tiny BOs */
228 size
= MAX2(size
, 4096);
230 /* GROWABLE BOs cannot be mmapped */
231 if (flags
& PAN_BO_GROWABLE
)
232 assert(flags
& PAN_BO_INVISIBLE
);
234 unsigned translated_flags
= 0;
236 if (screen
->kernel_version
->version_major
> 1 ||
237 screen
->kernel_version
->version_minor
>= 1) {
238 if (flags
& PAN_BO_GROWABLE
)
239 translated_flags
|= PANFROST_BO_HEAP
;
240 if (!(flags
& PAN_BO_EXECUTE
))
241 translated_flags
|= PANFROST_BO_NOEXEC
;
244 struct drm_panfrost_create_bo create_bo
= {
246 .flags
= translated_flags
,
249 /* Before creating a BO, we first want to check the cache */
251 bo
= panfrost_bo_cache_fetch(screen
, size
, flags
);
254 /* Otherwise, the cache misses and we need to allocate a BO fresh from
259 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_CREATE_BO
, &create_bo
);
261 fprintf(stderr
, "DRM_IOCTL_PANFROST_CREATE_BO failed: %m\n");
265 /* We have a BO allocated from the kernel; fill in the userspace
268 bo
= rzalloc(screen
, struct panfrost_bo
);
269 bo
->size
= create_bo
.size
;
270 bo
->gpu
= create_bo
.offset
;
271 bo
->gem_handle
= create_bo
.handle
;
275 /* Only mmap now if we know we need to. For CPU-invisible buffers, we
276 * never map since we don't care about their contents; they're purely
277 * for GPU-internal use. But we do trace them anyway. */
279 if (!(flags
& (PAN_BO_INVISIBLE
| PAN_BO_DELAY_MMAP
)))
280 panfrost_bo_mmap(bo
);
281 else if (flags
& PAN_BO_INVISIBLE
) {
282 if (pan_debug
& PAN_DBG_TRACE
)
283 pandecode_inject_mmap(bo
->gpu
, NULL
, bo
->size
, NULL
);
286 pipe_reference_init(&bo
->reference
, 1);
291 panfrost_bo_release(struct panfrost_bo
*bo
, bool cacheable
)
296 struct drm_gem_close gem_close
= { .handle
= bo
->gem_handle
};
299 /* Rather than freeing the BO now, we'll cache the BO for later
300 * allocations if we're allowed to */
302 panfrost_bo_munmap(bo
);
305 bool cached
= panfrost_bo_cache_put(bo
);
311 /* Otherwise, if the BO wasn't cached, we'll legitimately free the BO */
313 ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_GEM_CLOSE
, &gem_close
);
315 fprintf(stderr
, "DRM_IOCTL_GEM_CLOSE failed: %m\n");
323 panfrost_bo_reference(struct panfrost_bo
*bo
)
326 pipe_reference(NULL
, &bo
->reference
);
330 panfrost_bo_unreference(struct panfrost_bo
*bo
)
335 /* When the reference count goes to zero, we need to cleanup */
337 if (pipe_reference(&bo
->reference
, NULL
))
338 panfrost_bo_release(bo
, true);
342 panfrost_bo_import(struct panfrost_screen
*screen
, int fd
)
344 struct panfrost_bo
*bo
= rzalloc(screen
, struct panfrost_bo
);
345 struct drm_panfrost_get_bo_offset get_bo_offset
= {0,};
349 ret
= drmPrimeFDToHandle(screen
->fd
, fd
, &gem_handle
);
352 get_bo_offset
.handle
= gem_handle
;
353 ret
= drmIoctl(screen
->fd
, DRM_IOCTL_PANFROST_GET_BO_OFFSET
, &get_bo_offset
);
357 bo
->gem_handle
= gem_handle
;
358 bo
->gpu
= (mali_ptr
) get_bo_offset
.offset
;
359 bo
->size
= lseek(fd
, 0, SEEK_END
);
360 assert(bo
->size
> 0);
361 pipe_reference_init(&bo
->reference
, 1);
363 // TODO map and unmap on demand?
364 panfrost_bo_mmap(bo
);
369 panfrost_bo_export(struct panfrost_bo
*bo
)
371 struct drm_prime_handle args
= {
372 .handle
= bo
->gem_handle
,
373 .flags
= DRM_CLOEXEC
,
376 int ret
= drmIoctl(bo
->screen
->fd
, DRM_IOCTL_PRIME_HANDLE_TO_FD
, &args
);