1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright © 2015 Broadcom
7 * DOC: VC4 GEM BO management support
9 * The VC4 GPU architecture (both scanout and rendering) has direct
10 * access to system memory with no MMU in between. To support it, we
11 * use the GEM CMA helper functions to allocate contiguous ranges of
12 * physical memory for our BOs.
14 * Since the CMA allocator is very slow, we keep a cache of recently
15 * freed BOs around so that the kernel's allocation of objects for 3D
16 * rendering can return quickly.
19 #include <linux/dma-buf.h>
22 #include "uapi/drm/vc4_drm.h"
24 static const char * const bo_type_names
[] = {
35 static bool is_user_label(int label
)
37 return label
>= VC4_BO_TYPE_COUNT
;
40 static void vc4_bo_stats_print(struct drm_printer
*p
, struct vc4_dev
*vc4
)
44 for (i
= 0; i
< vc4
->num_labels
; i
++) {
45 if (!vc4
->bo_labels
[i
].num_allocated
)
48 drm_printf(p
, "%30s: %6dkb BOs (%d)\n",
49 vc4
->bo_labels
[i
].name
,
50 vc4
->bo_labels
[i
].size_allocated
/ 1024,
51 vc4
->bo_labels
[i
].num_allocated
);
54 mutex_lock(&vc4
->purgeable
.lock
);
55 if (vc4
->purgeable
.num
)
56 drm_printf(p
, "%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
57 vc4
->purgeable
.size
/ 1024, vc4
->purgeable
.num
);
59 if (vc4
->purgeable
.purged_num
)
60 drm_printf(p
, "%30s: %6zdkb BOs (%d)\n", "total purged BO",
61 vc4
->purgeable
.purged_size
/ 1024,
62 vc4
->purgeable
.purged_num
);
63 mutex_unlock(&vc4
->purgeable
.lock
);
66 static int vc4_bo_stats_debugfs(struct seq_file
*m
, void *unused
)
68 struct drm_info_node
*node
= (struct drm_info_node
*)m
->private;
69 struct drm_device
*dev
= node
->minor
->dev
;
70 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
71 struct drm_printer p
= drm_seq_file_printer(m
);
73 vc4_bo_stats_print(&p
, vc4
);
78 /* Takes ownership of *name and returns the appropriate slot for it in
79 * the bo_labels[] array, extending it as necessary.
81 * This is inefficient and could use a hash table instead of walking
82 * an array and strcmp()ing. However, the assumption is that user
83 * labeling will be infrequent (scanout buffers and other long-lived
84 * objects, or debug driver builds), so we can live with it for now.
86 static int vc4_get_user_label(struct vc4_dev
*vc4
, const char *name
)
91 for (i
= 0; i
< vc4
->num_labels
; i
++) {
92 if (!vc4
->bo_labels
[i
].name
) {
94 } else if (strcmp(vc4
->bo_labels
[i
].name
, name
) == 0) {
100 if (free_slot
!= -1) {
101 WARN_ON(vc4
->bo_labels
[free_slot
].num_allocated
!= 0);
102 vc4
->bo_labels
[free_slot
].name
= name
;
105 u32 new_label_count
= vc4
->num_labels
+ 1;
106 struct vc4_label
*new_labels
=
107 krealloc(vc4
->bo_labels
,
108 new_label_count
* sizeof(*new_labels
),
116 free_slot
= vc4
->num_labels
;
117 vc4
->bo_labels
= new_labels
;
118 vc4
->num_labels
= new_label_count
;
120 vc4
->bo_labels
[free_slot
].name
= name
;
121 vc4
->bo_labels
[free_slot
].num_allocated
= 0;
122 vc4
->bo_labels
[free_slot
].size_allocated
= 0;
128 static void vc4_bo_set_label(struct drm_gem_object
*gem_obj
, int label
)
130 struct vc4_bo
*bo
= to_vc4_bo(gem_obj
);
131 struct vc4_dev
*vc4
= to_vc4_dev(gem_obj
->dev
);
133 lockdep_assert_held(&vc4
->bo_lock
);
136 vc4
->bo_labels
[label
].num_allocated
++;
137 vc4
->bo_labels
[label
].size_allocated
+= gem_obj
->size
;
140 vc4
->bo_labels
[bo
->label
].num_allocated
--;
141 vc4
->bo_labels
[bo
->label
].size_allocated
-= gem_obj
->size
;
143 if (vc4
->bo_labels
[bo
->label
].num_allocated
== 0 &&
144 is_user_label(bo
->label
)) {
145 /* Free user BO label slots on last unreference.
146 * Slots are just where we track the stats for a given
147 * name, and once a name is unused we can reuse that
150 kfree(vc4
->bo_labels
[bo
->label
].name
);
151 vc4
->bo_labels
[bo
->label
].name
= NULL
;
157 static uint32_t bo_page_index(size_t size
)
159 return (size
/ PAGE_SIZE
) - 1;
162 static void vc4_bo_destroy(struct vc4_bo
*bo
)
164 struct drm_gem_object
*obj
= &bo
->base
.base
;
165 struct vc4_dev
*vc4
= to_vc4_dev(obj
->dev
);
167 lockdep_assert_held(&vc4
->bo_lock
);
169 vc4_bo_set_label(obj
, -1);
171 if (bo
->validated_shader
) {
172 kfree(bo
->validated_shader
->uniform_addr_offsets
);
173 kfree(bo
->validated_shader
->texture_samples
);
174 kfree(bo
->validated_shader
);
175 bo
->validated_shader
= NULL
;
178 drm_gem_cma_free_object(obj
);
181 static void vc4_bo_remove_from_cache(struct vc4_bo
*bo
)
183 struct vc4_dev
*vc4
= to_vc4_dev(bo
->base
.base
.dev
);
185 lockdep_assert_held(&vc4
->bo_lock
);
186 list_del(&bo
->unref_head
);
187 list_del(&bo
->size_head
);
190 static struct list_head
*vc4_get_cache_list_for_size(struct drm_device
*dev
,
193 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
194 uint32_t page_index
= bo_page_index(size
);
196 if (vc4
->bo_cache
.size_list_size
<= page_index
) {
197 uint32_t new_size
= max(vc4
->bo_cache
.size_list_size
* 2,
199 struct list_head
*new_list
;
202 new_list
= kmalloc_array(new_size
, sizeof(struct list_head
),
207 /* Rebase the old cached BO lists to their new list
210 for (i
= 0; i
< vc4
->bo_cache
.size_list_size
; i
++) {
211 struct list_head
*old_list
=
212 &vc4
->bo_cache
.size_list
[i
];
214 if (list_empty(old_list
))
215 INIT_LIST_HEAD(&new_list
[i
]);
217 list_replace(old_list
, &new_list
[i
]);
219 /* And initialize the brand new BO list heads. */
220 for (i
= vc4
->bo_cache
.size_list_size
; i
< new_size
; i
++)
221 INIT_LIST_HEAD(&new_list
[i
]);
223 kfree(vc4
->bo_cache
.size_list
);
224 vc4
->bo_cache
.size_list
= new_list
;
225 vc4
->bo_cache
.size_list_size
= new_size
;
228 return &vc4
->bo_cache
.size_list
[page_index
];
231 static void vc4_bo_cache_purge(struct drm_device
*dev
)
233 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
235 mutex_lock(&vc4
->bo_lock
);
236 while (!list_empty(&vc4
->bo_cache
.time_list
)) {
237 struct vc4_bo
*bo
= list_last_entry(&vc4
->bo_cache
.time_list
,
238 struct vc4_bo
, unref_head
);
239 vc4_bo_remove_from_cache(bo
);
242 mutex_unlock(&vc4
->bo_lock
);
245 void vc4_bo_add_to_purgeable_pool(struct vc4_bo
*bo
)
247 struct vc4_dev
*vc4
= to_vc4_dev(bo
->base
.base
.dev
);
249 mutex_lock(&vc4
->purgeable
.lock
);
250 list_add_tail(&bo
->size_head
, &vc4
->purgeable
.list
);
251 vc4
->purgeable
.num
++;
252 vc4
->purgeable
.size
+= bo
->base
.base
.size
;
253 mutex_unlock(&vc4
->purgeable
.lock
);
256 static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo
*bo
)
258 struct vc4_dev
*vc4
= to_vc4_dev(bo
->base
.base
.dev
);
260 /* list_del_init() is used here because the caller might release
261 * the purgeable lock in order to acquire the madv one and update the
263 * During this short period of time a user might decide to mark
264 * the BO as unpurgeable, and if bo->madv is set to
265 * VC4_MADV_DONTNEED it will try to remove the BO from the
266 * purgeable list which will fail if the ->next/prev fields
267 * are set to LIST_POISON1/LIST_POISON2 (which is what
269 * Re-initializing the list element guarantees that list_del()
270 * will work correctly even if it's a NOP.
272 list_del_init(&bo
->size_head
);
273 vc4
->purgeable
.num
--;
274 vc4
->purgeable
.size
-= bo
->base
.base
.size
;
277 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo
*bo
)
279 struct vc4_dev
*vc4
= to_vc4_dev(bo
->base
.base
.dev
);
281 mutex_lock(&vc4
->purgeable
.lock
);
282 vc4_bo_remove_from_purgeable_pool_locked(bo
);
283 mutex_unlock(&vc4
->purgeable
.lock
);
286 static void vc4_bo_purge(struct drm_gem_object
*obj
)
288 struct vc4_bo
*bo
= to_vc4_bo(obj
);
289 struct drm_device
*dev
= obj
->dev
;
291 WARN_ON(!mutex_is_locked(&bo
->madv_lock
));
292 WARN_ON(bo
->madv
!= VC4_MADV_DONTNEED
);
294 drm_vma_node_unmap(&obj
->vma_node
, dev
->anon_inode
->i_mapping
);
296 dma_free_wc(dev
->dev
, obj
->size
, bo
->base
.vaddr
, bo
->base
.paddr
);
297 bo
->base
.vaddr
= NULL
;
298 bo
->madv
= __VC4_MADV_PURGED
;
301 static void vc4_bo_userspace_cache_purge(struct drm_device
*dev
)
303 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
305 mutex_lock(&vc4
->purgeable
.lock
);
306 while (!list_empty(&vc4
->purgeable
.list
)) {
307 struct vc4_bo
*bo
= list_first_entry(&vc4
->purgeable
.list
,
308 struct vc4_bo
, size_head
);
309 struct drm_gem_object
*obj
= &bo
->base
.base
;
310 size_t purged_size
= 0;
312 vc4_bo_remove_from_purgeable_pool_locked(bo
);
314 /* Release the purgeable lock while we're purging the BO so
315 * that other people can continue inserting things in the
316 * purgeable pool without having to wait for all BOs to be
319 mutex_unlock(&vc4
->purgeable
.lock
);
320 mutex_lock(&bo
->madv_lock
);
322 /* Since we released the purgeable pool lock before acquiring
323 * the BO madv one, the user may have marked the BO as WILLNEED
324 * and re-used it in the meantime.
325 * Before purging the BO we need to make sure
326 * - it is still marked as DONTNEED
327 * - it has not been re-inserted in the purgeable list
328 * - it is not used by HW blocks
329 * If one of these conditions is not met, just skip the entry.
331 if (bo
->madv
== VC4_MADV_DONTNEED
&&
332 list_empty(&bo
->size_head
) &&
333 !refcount_read(&bo
->usecnt
)) {
334 purged_size
= bo
->base
.base
.size
;
337 mutex_unlock(&bo
->madv_lock
);
338 mutex_lock(&vc4
->purgeable
.lock
);
341 vc4
->purgeable
.purged_size
+= purged_size
;
342 vc4
->purgeable
.purged_num
++;
345 mutex_unlock(&vc4
->purgeable
.lock
);
348 static struct vc4_bo
*vc4_bo_get_from_cache(struct drm_device
*dev
,
350 enum vc4_kernel_bo_type type
)
352 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
353 uint32_t page_index
= bo_page_index(size
);
354 struct vc4_bo
*bo
= NULL
;
356 size
= roundup(size
, PAGE_SIZE
);
358 mutex_lock(&vc4
->bo_lock
);
359 if (page_index
>= vc4
->bo_cache
.size_list_size
)
362 if (list_empty(&vc4
->bo_cache
.size_list
[page_index
]))
365 bo
= list_first_entry(&vc4
->bo_cache
.size_list
[page_index
],
366 struct vc4_bo
, size_head
);
367 vc4_bo_remove_from_cache(bo
);
368 kref_init(&bo
->base
.base
.refcount
);
372 vc4_bo_set_label(&bo
->base
.base
, type
);
373 mutex_unlock(&vc4
->bo_lock
);
378 * vc4_gem_create_object - Implementation of driver->gem_create_object.
380 * @size: Size in bytes of the memory the object will reference
382 * This lets the CMA helpers allocate object structs for us, and keep
383 * our BO stats correct.
385 struct drm_gem_object
*vc4_create_object(struct drm_device
*dev
, size_t size
)
387 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
390 bo
= kzalloc(sizeof(*bo
), GFP_KERNEL
);
392 return ERR_PTR(-ENOMEM
);
394 bo
->madv
= VC4_MADV_WILLNEED
;
395 refcount_set(&bo
->usecnt
, 0);
396 mutex_init(&bo
->madv_lock
);
397 mutex_lock(&vc4
->bo_lock
);
398 bo
->label
= VC4_BO_TYPE_KERNEL
;
399 vc4
->bo_labels
[VC4_BO_TYPE_KERNEL
].num_allocated
++;
400 vc4
->bo_labels
[VC4_BO_TYPE_KERNEL
].size_allocated
+= size
;
401 mutex_unlock(&vc4
->bo_lock
);
403 return &bo
->base
.base
;
406 struct vc4_bo
*vc4_bo_create(struct drm_device
*dev
, size_t unaligned_size
,
407 bool allow_unzeroed
, enum vc4_kernel_bo_type type
)
409 size_t size
= roundup(unaligned_size
, PAGE_SIZE
);
410 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
411 struct drm_gem_cma_object
*cma_obj
;
415 return ERR_PTR(-EINVAL
);
417 /* First, try to get a vc4_bo from the kernel BO cache. */
418 bo
= vc4_bo_get_from_cache(dev
, size
, type
);
421 memset(bo
->base
.vaddr
, 0, bo
->base
.base
.size
);
425 cma_obj
= drm_gem_cma_create(dev
, size
);
426 if (IS_ERR(cma_obj
)) {
428 * If we've run out of CMA memory, kill the cache of
429 * CMA allocations we've got laying around and try again.
431 vc4_bo_cache_purge(dev
);
432 cma_obj
= drm_gem_cma_create(dev
, size
);
435 if (IS_ERR(cma_obj
)) {
437 * Still not enough CMA memory, purge the userspace BO
439 * This is sub-optimal since we purge the whole userspace
440 * BO cache which forces user that want to re-use the BO to
441 * restore its initial content.
442 * Ideally, we should purge entries one by one and retry
443 * after each to see if CMA allocation succeeds. Or even
444 * better, try to find an entry with at least the same
447 vc4_bo_userspace_cache_purge(dev
);
448 cma_obj
= drm_gem_cma_create(dev
, size
);
451 if (IS_ERR(cma_obj
)) {
452 struct drm_printer p
= drm_info_printer(vc4
->dev
->dev
);
453 DRM_ERROR("Failed to allocate from CMA:\n");
454 vc4_bo_stats_print(&p
, vc4
);
455 return ERR_PTR(-ENOMEM
);
457 bo
= to_vc4_bo(&cma_obj
->base
);
459 /* By default, BOs do not support the MADV ioctl. This will be enabled
460 * only on BOs that are exposed to userspace (V3D, V3D_SHADER and DUMB
463 bo
->madv
= __VC4_MADV_NOTSUPP
;
465 mutex_lock(&vc4
->bo_lock
);
466 vc4_bo_set_label(&cma_obj
->base
, type
);
467 mutex_unlock(&vc4
->bo_lock
);
472 int vc4_dumb_create(struct drm_file
*file_priv
,
473 struct drm_device
*dev
,
474 struct drm_mode_create_dumb
*args
)
476 int min_pitch
= DIV_ROUND_UP(args
->width
* args
->bpp
, 8);
477 struct vc4_bo
*bo
= NULL
;
480 if (args
->pitch
< min_pitch
)
481 args
->pitch
= min_pitch
;
483 if (args
->size
< args
->pitch
* args
->height
)
484 args
->size
= args
->pitch
* args
->height
;
486 bo
= vc4_bo_create(dev
, args
->size
, false, VC4_BO_TYPE_DUMB
);
490 bo
->madv
= VC4_MADV_WILLNEED
;
492 ret
= drm_gem_handle_create(file_priv
, &bo
->base
.base
, &args
->handle
);
493 drm_gem_object_put_unlocked(&bo
->base
.base
);
498 static void vc4_bo_cache_free_old(struct drm_device
*dev
)
500 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
501 unsigned long expire_time
= jiffies
- msecs_to_jiffies(1000);
503 lockdep_assert_held(&vc4
->bo_lock
);
505 while (!list_empty(&vc4
->bo_cache
.time_list
)) {
506 struct vc4_bo
*bo
= list_last_entry(&vc4
->bo_cache
.time_list
,
507 struct vc4_bo
, unref_head
);
508 if (time_before(expire_time
, bo
->free_time
)) {
509 mod_timer(&vc4
->bo_cache
.time_timer
,
510 round_jiffies_up(jiffies
+
511 msecs_to_jiffies(1000)));
515 vc4_bo_remove_from_cache(bo
);
520 /* Called on the last userspace/kernel unreference of the BO. Returns
521 * it to the BO cache if possible, otherwise frees it.
523 void vc4_free_object(struct drm_gem_object
*gem_bo
)
525 struct drm_device
*dev
= gem_bo
->dev
;
526 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
527 struct vc4_bo
*bo
= to_vc4_bo(gem_bo
);
528 struct list_head
*cache_list
;
530 /* Remove the BO from the purgeable list. */
531 mutex_lock(&bo
->madv_lock
);
532 if (bo
->madv
== VC4_MADV_DONTNEED
&& !refcount_read(&bo
->usecnt
))
533 vc4_bo_remove_from_purgeable_pool(bo
);
534 mutex_unlock(&bo
->madv_lock
);
536 mutex_lock(&vc4
->bo_lock
);
537 /* If the object references someone else's memory, we can't cache it.
539 if (gem_bo
->import_attach
) {
544 /* Don't cache if it was publicly named. */
550 /* If this object was partially constructed but CMA allocation
551 * had failed, just free it. Can also happen when the BO has been
554 if (!bo
->base
.vaddr
) {
559 cache_list
= vc4_get_cache_list_for_size(dev
, gem_bo
->size
);
565 if (bo
->validated_shader
) {
566 kfree(bo
->validated_shader
->uniform_addr_offsets
);
567 kfree(bo
->validated_shader
->texture_samples
);
568 kfree(bo
->validated_shader
);
569 bo
->validated_shader
= NULL
;
572 /* Reset madv and usecnt before adding the BO to the cache. */
573 bo
->madv
= __VC4_MADV_NOTSUPP
;
574 refcount_set(&bo
->usecnt
, 0);
576 bo
->t_format
= false;
577 bo
->free_time
= jiffies
;
578 list_add(&bo
->size_head
, cache_list
);
579 list_add(&bo
->unref_head
, &vc4
->bo_cache
.time_list
);
581 vc4_bo_set_label(&bo
->base
.base
, VC4_BO_TYPE_KERNEL_CACHE
);
583 vc4_bo_cache_free_old(dev
);
586 mutex_unlock(&vc4
->bo_lock
);
589 static void vc4_bo_cache_time_work(struct work_struct
*work
)
591 struct vc4_dev
*vc4
=
592 container_of(work
, struct vc4_dev
, bo_cache
.time_work
);
593 struct drm_device
*dev
= vc4
->dev
;
595 mutex_lock(&vc4
->bo_lock
);
596 vc4_bo_cache_free_old(dev
);
597 mutex_unlock(&vc4
->bo_lock
);
600 int vc4_bo_inc_usecnt(struct vc4_bo
*bo
)
604 /* Fast path: if the BO is already retained by someone, no need to
605 * check the madv status.
607 if (refcount_inc_not_zero(&bo
->usecnt
))
610 mutex_lock(&bo
->madv_lock
);
612 case VC4_MADV_WILLNEED
:
613 if (!refcount_inc_not_zero(&bo
->usecnt
))
614 refcount_set(&bo
->usecnt
, 1);
617 case VC4_MADV_DONTNEED
:
618 /* We shouldn't use a BO marked as purgeable if at least
619 * someone else retained its content by incrementing usecnt.
620 * Luckily the BO hasn't been purged yet, but something wrong
621 * is happening here. Just throw an error instead of
622 * authorizing this use case.
624 case __VC4_MADV_PURGED
:
625 /* We can't use a purged BO. */
627 /* Invalid madv value. */
631 mutex_unlock(&bo
->madv_lock
);
636 void vc4_bo_dec_usecnt(struct vc4_bo
*bo
)
638 /* Fast path: if the BO is still retained by someone, no need to test
641 if (refcount_dec_not_one(&bo
->usecnt
))
644 mutex_lock(&bo
->madv_lock
);
645 if (refcount_dec_and_test(&bo
->usecnt
) &&
646 bo
->madv
== VC4_MADV_DONTNEED
)
647 vc4_bo_add_to_purgeable_pool(bo
);
648 mutex_unlock(&bo
->madv_lock
);
651 static void vc4_bo_cache_time_timer(struct timer_list
*t
)
653 struct vc4_dev
*vc4
= from_timer(vc4
, t
, bo_cache
.time_timer
);
655 schedule_work(&vc4
->bo_cache
.time_work
);
658 struct dma_buf
* vc4_prime_export(struct drm_gem_object
*obj
, int flags
)
660 struct vc4_bo
*bo
= to_vc4_bo(obj
);
661 struct dma_buf
*dmabuf
;
664 if (bo
->validated_shader
) {
665 DRM_DEBUG("Attempting to export shader BO\n");
666 return ERR_PTR(-EINVAL
);
669 /* Note: as soon as the BO is exported it becomes unpurgeable, because
670 * noone ever decrements the usecnt even if the reference held by the
671 * exported BO is released. This shouldn't be a problem since we don't
672 * expect exported BOs to be marked as purgeable.
674 ret
= vc4_bo_inc_usecnt(bo
);
676 DRM_ERROR("Failed to increment BO usecnt\n");
680 dmabuf
= drm_gem_prime_export(obj
, flags
);
682 vc4_bo_dec_usecnt(bo
);
687 vm_fault_t
vc4_fault(struct vm_fault
*vmf
)
689 struct vm_area_struct
*vma
= vmf
->vma
;
690 struct drm_gem_object
*obj
= vma
->vm_private_data
;
691 struct vc4_bo
*bo
= to_vc4_bo(obj
);
693 /* The only reason we would end up here is when user-space accesses
694 * BO's memory after it's been purged.
696 mutex_lock(&bo
->madv_lock
);
697 WARN_ON(bo
->madv
!= __VC4_MADV_PURGED
);
698 mutex_unlock(&bo
->madv_lock
);
700 return VM_FAULT_SIGBUS
;
703 int vc4_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
705 struct drm_gem_object
*gem_obj
;
706 unsigned long vm_pgoff
;
710 ret
= drm_gem_mmap(filp
, vma
);
714 gem_obj
= vma
->vm_private_data
;
715 bo
= to_vc4_bo(gem_obj
);
717 if (bo
->validated_shader
&& (vma
->vm_flags
& VM_WRITE
)) {
718 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
722 if (bo
->madv
!= VC4_MADV_WILLNEED
) {
723 DRM_DEBUG("mmaping of %s BO not allowed\n",
724 bo
->madv
== VC4_MADV_DONTNEED
?
725 "purgeable" : "purged");
730 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
731 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
734 vma
->vm_flags
&= ~VM_PFNMAP
;
736 /* This ->vm_pgoff dance is needed to make all parties happy:
737 * - dma_mmap_wc() uses ->vm_pgoff as an offset within the allocated
738 * mem-region, hence the need to set it to zero (the value set by
739 * the DRM core is a virtual offset encoding the GEM object-id)
740 * - the mmap() core logic needs ->vm_pgoff to be restored to its
741 * initial value before returning from this function because it
742 * encodes the offset of this GEM in the dev->anon_inode pseudo-file
743 * and this information will be used when we invalidate userspace
744 * mappings with drm_vma_node_unmap() (called from vc4_gem_purge()).
746 vm_pgoff
= vma
->vm_pgoff
;
748 ret
= dma_mmap_wc(bo
->base
.base
.dev
->dev
, vma
, bo
->base
.vaddr
,
749 bo
->base
.paddr
, vma
->vm_end
- vma
->vm_start
);
750 vma
->vm_pgoff
= vm_pgoff
;
753 drm_gem_vm_close(vma
);
758 int vc4_prime_mmap(struct drm_gem_object
*obj
, struct vm_area_struct
*vma
)
760 struct vc4_bo
*bo
= to_vc4_bo(obj
);
762 if (bo
->validated_shader
&& (vma
->vm_flags
& VM_WRITE
)) {
763 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
767 return drm_gem_cma_prime_mmap(obj
, vma
);
770 void *vc4_prime_vmap(struct drm_gem_object
*obj
)
772 struct vc4_bo
*bo
= to_vc4_bo(obj
);
774 if (bo
->validated_shader
) {
775 DRM_DEBUG("mmaping of shader BOs not allowed.\n");
776 return ERR_PTR(-EINVAL
);
779 return drm_gem_cma_prime_vmap(obj
);
782 struct drm_gem_object
*
783 vc4_prime_import_sg_table(struct drm_device
*dev
,
784 struct dma_buf_attachment
*attach
,
785 struct sg_table
*sgt
)
787 struct drm_gem_object
*obj
;
789 obj
= drm_gem_cma_prime_import_sg_table(dev
, attach
, sgt
);
796 static int vc4_grab_bin_bo(struct vc4_dev
*vc4
, struct vc4_file
*vc4file
)
803 if (vc4file
->bin_bo_used
)
806 ret
= vc4_v3d_bin_bo_get(vc4
, &vc4file
->bin_bo_used
);
813 int vc4_create_bo_ioctl(struct drm_device
*dev
, void *data
,
814 struct drm_file
*file_priv
)
816 struct drm_vc4_create_bo
*args
= data
;
817 struct vc4_file
*vc4file
= file_priv
->driver_priv
;
818 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
819 struct vc4_bo
*bo
= NULL
;
822 ret
= vc4_grab_bin_bo(vc4
, vc4file
);
827 * We can't allocate from the BO cache, because the BOs don't
828 * get zeroed, and that might leak data between users.
830 bo
= vc4_bo_create(dev
, args
->size
, false, VC4_BO_TYPE_V3D
);
834 bo
->madv
= VC4_MADV_WILLNEED
;
836 ret
= drm_gem_handle_create(file_priv
, &bo
->base
.base
, &args
->handle
);
837 drm_gem_object_put_unlocked(&bo
->base
.base
);
842 int vc4_mmap_bo_ioctl(struct drm_device
*dev
, void *data
,
843 struct drm_file
*file_priv
)
845 struct drm_vc4_mmap_bo
*args
= data
;
846 struct drm_gem_object
*gem_obj
;
848 gem_obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
850 DRM_DEBUG("Failed to look up GEM BO %d\n", args
->handle
);
854 /* The mmap offset was set up at BO allocation time. */
855 args
->offset
= drm_vma_node_offset_addr(&gem_obj
->vma_node
);
857 drm_gem_object_put_unlocked(gem_obj
);
862 vc4_create_shader_bo_ioctl(struct drm_device
*dev
, void *data
,
863 struct drm_file
*file_priv
)
865 struct drm_vc4_create_shader_bo
*args
= data
;
866 struct vc4_file
*vc4file
= file_priv
->driver_priv
;
867 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
868 struct vc4_bo
*bo
= NULL
;
874 if (args
->size
% sizeof(u64
) != 0)
877 if (args
->flags
!= 0) {
878 DRM_INFO("Unknown flags set: 0x%08x\n", args
->flags
);
882 if (args
->pad
!= 0) {
883 DRM_INFO("Pad set: 0x%08x\n", args
->pad
);
887 ret
= vc4_grab_bin_bo(vc4
, vc4file
);
891 bo
= vc4_bo_create(dev
, args
->size
, true, VC4_BO_TYPE_V3D_SHADER
);
895 bo
->madv
= VC4_MADV_WILLNEED
;
897 if (copy_from_user(bo
->base
.vaddr
,
898 (void __user
*)(uintptr_t)args
->data
,
903 /* Clear the rest of the memory from allocating from the BO
906 memset(bo
->base
.vaddr
+ args
->size
, 0,
907 bo
->base
.base
.size
- args
->size
);
909 bo
->validated_shader
= vc4_validate_shader(&bo
->base
);
910 if (!bo
->validated_shader
) {
915 /* We have to create the handle after validation, to avoid
916 * races for users to do doing things like mmap the shader BO.
918 ret
= drm_gem_handle_create(file_priv
, &bo
->base
.base
, &args
->handle
);
921 drm_gem_object_put_unlocked(&bo
->base
.base
);
927 * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
929 * @data: ioctl argument
930 * @file_priv: DRM file for this fd
932 * The tiling state of the BO decides the default modifier of an fb if
933 * no specific modifier was set by userspace, and the return value of
934 * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
935 * received from dmabuf as the same tiling format as the producer
938 int vc4_set_tiling_ioctl(struct drm_device
*dev
, void *data
,
939 struct drm_file
*file_priv
)
941 struct drm_vc4_set_tiling
*args
= data
;
942 struct drm_gem_object
*gem_obj
;
946 if (args
->flags
!= 0)
949 switch (args
->modifier
) {
950 case DRM_FORMAT_MOD_NONE
:
953 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED
:
960 gem_obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
962 DRM_DEBUG("Failed to look up GEM BO %d\n", args
->handle
);
965 bo
= to_vc4_bo(gem_obj
);
966 bo
->t_format
= t_format
;
968 drm_gem_object_put_unlocked(gem_obj
);
974 * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
976 * @data: ioctl argument
977 * @file_priv: DRM file for this fd
979 * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
981 int vc4_get_tiling_ioctl(struct drm_device
*dev
, void *data
,
982 struct drm_file
*file_priv
)
984 struct drm_vc4_get_tiling
*args
= data
;
985 struct drm_gem_object
*gem_obj
;
988 if (args
->flags
!= 0 || args
->modifier
!= 0)
991 gem_obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
993 DRM_DEBUG("Failed to look up GEM BO %d\n", args
->handle
);
996 bo
= to_vc4_bo(gem_obj
);
999 args
->modifier
= DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED
;
1001 args
->modifier
= DRM_FORMAT_MOD_NONE
;
1003 drm_gem_object_put_unlocked(gem_obj
);
1008 int vc4_bo_cache_init(struct drm_device
*dev
)
1010 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
1013 /* Create the initial set of BO labels that the kernel will
1014 * use. This lets us avoid a bunch of string reallocation in
1015 * the kernel's draw and BO allocation paths.
1017 vc4
->bo_labels
= kcalloc(VC4_BO_TYPE_COUNT
, sizeof(*vc4
->bo_labels
),
1019 if (!vc4
->bo_labels
)
1021 vc4
->num_labels
= VC4_BO_TYPE_COUNT
;
1023 BUILD_BUG_ON(ARRAY_SIZE(bo_type_names
) != VC4_BO_TYPE_COUNT
);
1024 for (i
= 0; i
< VC4_BO_TYPE_COUNT
; i
++)
1025 vc4
->bo_labels
[i
].name
= bo_type_names
[i
];
1027 mutex_init(&vc4
->bo_lock
);
1029 vc4_debugfs_add_file(dev
, "bo_stats", vc4_bo_stats_debugfs
, NULL
);
1031 INIT_LIST_HEAD(&vc4
->bo_cache
.time_list
);
1033 INIT_WORK(&vc4
->bo_cache
.time_work
, vc4_bo_cache_time_work
);
1034 timer_setup(&vc4
->bo_cache
.time_timer
, vc4_bo_cache_time_timer
, 0);
1039 void vc4_bo_cache_destroy(struct drm_device
*dev
)
1041 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
1044 del_timer(&vc4
->bo_cache
.time_timer
);
1045 cancel_work_sync(&vc4
->bo_cache
.time_work
);
1047 vc4_bo_cache_purge(dev
);
1049 for (i
= 0; i
< vc4
->num_labels
; i
++) {
1050 if (vc4
->bo_labels
[i
].num_allocated
) {
1051 DRM_ERROR("Destroying BO cache with %d %s "
1052 "BOs still allocated\n",
1053 vc4
->bo_labels
[i
].num_allocated
,
1054 vc4
->bo_labels
[i
].name
);
1057 if (is_user_label(i
))
1058 kfree(vc4
->bo_labels
[i
].name
);
1060 kfree(vc4
->bo_labels
);
1063 int vc4_label_bo_ioctl(struct drm_device
*dev
, void *data
,
1064 struct drm_file
*file_priv
)
1066 struct vc4_dev
*vc4
= to_vc4_dev(dev
);
1067 struct drm_vc4_label_bo
*args
= data
;
1069 struct drm_gem_object
*gem_obj
;
1075 name
= strndup_user(u64_to_user_ptr(args
->name
), args
->len
+ 1);
1077 return PTR_ERR(name
);
1079 gem_obj
= drm_gem_object_lookup(file_priv
, args
->handle
);
1081 DRM_ERROR("Failed to look up GEM BO %d\n", args
->handle
);
1086 mutex_lock(&vc4
->bo_lock
);
1087 label
= vc4_get_user_label(vc4
, name
);
1089 vc4_bo_set_label(gem_obj
, label
);
1092 mutex_unlock(&vc4
->bo_lock
);
1094 drm_gem_object_put_unlocked(gem_obj
);