xref: /linux/drivers/gpu/drm/vc4/vc4_bo.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Copyright © 2015 Broadcom
4  */
5 
6 /**
7  * DOC: VC4 GEM BO management support
8  *
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 DMA helper functions to allocate contiguous ranges of
12  * physical memory for our BOs.
13  *
14  * Since the DMA 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.
17  */
18 
19 #include <linux/dma-buf.h>
20 
21 #include <drm/drm_fourcc.h>
22 #include <drm/drm_print.h>
23 
24 #include "vc4_drv.h"
25 #include "uapi/drm/vc4_drm.h"
26 
27 static const struct drm_gem_object_funcs vc4_gem_object_funcs;
28 
29 static const char * const bo_type_names[] = {
30 	"kernel",
31 	"V3D",
32 	"V3D shader",
33 	"dumb",
34 	"binner",
35 	"RCL",
36 	"BCL",
37 	"kernel BO cache",
38 };
39 
40 static bool is_user_label(int label)
41 {
42 	return label >= VC4_BO_TYPE_COUNT;
43 }
44 
45 static void vc4_bo_stats_print(struct drm_printer *p, struct vc4_dev *vc4)
46 {
47 	int i;
48 
49 	for (i = 0; i < vc4->num_labels; i++) {
50 		if (!vc4->bo_labels[i].num_allocated)
51 			continue;
52 
53 		drm_printf(p, "%30s: %6dkb BOs (%d)\n",
54 			   vc4->bo_labels[i].name,
55 			   vc4->bo_labels[i].size_allocated / 1024,
56 			   vc4->bo_labels[i].num_allocated);
57 	}
58 
59 	mutex_lock(&vc4->purgeable.lock);
60 	if (vc4->purgeable.num)
61 		drm_printf(p, "%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
62 			   vc4->purgeable.size / 1024, vc4->purgeable.num);
63 
64 	if (vc4->purgeable.purged_num)
65 		drm_printf(p, "%30s: %6zdkb BOs (%d)\n", "total purged BO",
66 			   vc4->purgeable.purged_size / 1024,
67 			   vc4->purgeable.purged_num);
68 	mutex_unlock(&vc4->purgeable.lock);
69 }
70 
71 static int vc4_bo_stats_debugfs(struct seq_file *m, void *unused)
72 {
73 	struct drm_debugfs_entry *entry = m->private;
74 	struct drm_device *dev = entry->dev;
75 	struct vc4_dev *vc4 = to_vc4_dev(dev);
76 	struct drm_printer p = drm_seq_file_printer(m);
77 
78 	vc4_bo_stats_print(&p, vc4);
79 
80 	return 0;
81 }
82 
83 /* Takes ownership of *name and returns the appropriate slot for it in
84  * the bo_labels[] array, extending it as necessary.
85  *
86  * This is inefficient and could use a hash table instead of walking
87  * an array and strcmp()ing.  However, the assumption is that user
88  * labeling will be infrequent (scanout buffers and other long-lived
89  * objects, or debug driver builds), so we can live with it for now.
90  */
91 static int vc4_get_user_label(struct vc4_dev *vc4, const char *name)
92 {
93 	int i;
94 	int free_slot = -1;
95 
96 	for (i = 0; i < vc4->num_labels; i++) {
97 		if (!vc4->bo_labels[i].name) {
98 			free_slot = i;
99 		} else if (strcmp(vc4->bo_labels[i].name, name) == 0) {
100 			kfree(name);
101 			return i;
102 		}
103 	}
104 
105 	if (free_slot != -1) {
106 		WARN_ON(vc4->bo_labels[free_slot].num_allocated != 0);
107 		vc4->bo_labels[free_slot].name = name;
108 		return free_slot;
109 	} else {
110 		u32 new_label_count = vc4->num_labels + 1;
111 		struct vc4_label *new_labels =
112 			krealloc(vc4->bo_labels,
113 				 new_label_count * sizeof(*new_labels),
114 				 GFP_KERNEL);
115 
116 		if (!new_labels) {
117 			kfree(name);
118 			return -1;
119 		}
120 
121 		free_slot = vc4->num_labels;
122 		vc4->bo_labels = new_labels;
123 		vc4->num_labels = new_label_count;
124 
125 		vc4->bo_labels[free_slot].name = name;
126 		vc4->bo_labels[free_slot].num_allocated = 0;
127 		vc4->bo_labels[free_slot].size_allocated = 0;
128 
129 		return free_slot;
130 	}
131 }
132 
133 static void vc4_bo_set_label(struct drm_gem_object *gem_obj, int label)
134 {
135 	struct vc4_bo *bo = to_vc4_bo(gem_obj);
136 	struct vc4_dev *vc4 = to_vc4_dev(gem_obj->dev);
137 
138 	lockdep_assert_held(&vc4->bo_lock);
139 
140 	if (label != -1) {
141 		vc4->bo_labels[label].num_allocated++;
142 		vc4->bo_labels[label].size_allocated += gem_obj->size;
143 	}
144 
145 	vc4->bo_labels[bo->label].num_allocated--;
146 	vc4->bo_labels[bo->label].size_allocated -= gem_obj->size;
147 
148 	if (vc4->bo_labels[bo->label].num_allocated == 0 &&
149 	    is_user_label(bo->label)) {
150 		/* Free user BO label slots on last unreference.
151 		 * Slots are just where we track the stats for a given
152 		 * name, and once a name is unused we can reuse that
153 		 * slot.
154 		 */
155 		kfree(vc4->bo_labels[bo->label].name);
156 		vc4->bo_labels[bo->label].name = NULL;
157 	}
158 
159 	bo->label = label;
160 }
161 
162 static uint32_t bo_page_index(size_t size)
163 {
164 	return (size / PAGE_SIZE) - 1;
165 }
166 
167 static void vc4_bo_destroy(struct vc4_bo *bo)
168 {
169 	struct drm_gem_object *obj = &bo->base.base;
170 	struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
171 
172 	lockdep_assert_held(&vc4->bo_lock);
173 
174 	vc4_bo_set_label(obj, -1);
175 
176 	if (bo->validated_shader) {
177 		kfree(bo->validated_shader->uniform_addr_offsets);
178 		kfree(bo->validated_shader->texture_samples);
179 		kfree(bo->validated_shader);
180 		bo->validated_shader = NULL;
181 	}
182 
183 	mutex_destroy(&bo->madv_lock);
184 	drm_gem_dma_free(&bo->base);
185 }
186 
187 static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
188 {
189 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
190 
191 	lockdep_assert_held(&vc4->bo_lock);
192 	list_del(&bo->unref_head);
193 	list_del(&bo->size_head);
194 }
195 
196 static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
197 						     size_t size)
198 {
199 	struct vc4_dev *vc4 = to_vc4_dev(dev);
200 	uint32_t page_index = bo_page_index(size);
201 
202 	if (vc4->bo_cache.size_list_size <= page_index) {
203 		uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
204 					page_index + 1);
205 		struct list_head *new_list;
206 		uint32_t i;
207 
208 		new_list = kmalloc_array(new_size, sizeof(struct list_head),
209 					 GFP_KERNEL);
210 		if (!new_list)
211 			return NULL;
212 
213 		/* Rebase the old cached BO lists to their new list
214 		 * head locations.
215 		 */
216 		for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
217 			struct list_head *old_list =
218 				&vc4->bo_cache.size_list[i];
219 
220 			if (list_empty(old_list))
221 				INIT_LIST_HEAD(&new_list[i]);
222 			else
223 				list_replace(old_list, &new_list[i]);
224 		}
225 		/* And initialize the brand new BO list heads. */
226 		for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
227 			INIT_LIST_HEAD(&new_list[i]);
228 
229 		kfree(vc4->bo_cache.size_list);
230 		vc4->bo_cache.size_list = new_list;
231 		vc4->bo_cache.size_list_size = new_size;
232 	}
233 
234 	return &vc4->bo_cache.size_list[page_index];
235 }
236 
237 static void vc4_bo_cache_purge(struct drm_device *dev)
238 {
239 	struct vc4_dev *vc4 = to_vc4_dev(dev);
240 
241 	mutex_lock(&vc4->bo_lock);
242 	while (!list_empty(&vc4->bo_cache.time_list)) {
243 		struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
244 						    struct vc4_bo, unref_head);
245 		vc4_bo_remove_from_cache(bo);
246 		vc4_bo_destroy(bo);
247 	}
248 	mutex_unlock(&vc4->bo_lock);
249 }
250 
251 void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo)
252 {
253 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
254 
255 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
256 		return;
257 
258 	mutex_lock(&vc4->purgeable.lock);
259 	list_add_tail(&bo->size_head, &vc4->purgeable.list);
260 	vc4->purgeable.num++;
261 	vc4->purgeable.size += bo->base.base.size;
262 	mutex_unlock(&vc4->purgeable.lock);
263 }
264 
265 static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo *bo)
266 {
267 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
268 
269 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
270 		return;
271 
272 	/* list_del_init() is used here because the caller might release
273 	 * the purgeable lock in order to acquire the madv one and update the
274 	 * madv status.
275 	 * During this short period of time a user might decide to mark
276 	 * the BO as unpurgeable, and if bo->madv is set to
277 	 * VC4_MADV_DONTNEED it will try to remove the BO from the
278 	 * purgeable list which will fail if the ->next/prev fields
279 	 * are set to LIST_POISON1/LIST_POISON2 (which is what
280 	 * list_del() does).
281 	 * Re-initializing the list element guarantees that list_del()
282 	 * will work correctly even if it's a NOP.
283 	 */
284 	list_del_init(&bo->size_head);
285 	vc4->purgeable.num--;
286 	vc4->purgeable.size -= bo->base.base.size;
287 }
288 
289 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo)
290 {
291 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
292 
293 	mutex_lock(&vc4->purgeable.lock);
294 	vc4_bo_remove_from_purgeable_pool_locked(bo);
295 	mutex_unlock(&vc4->purgeable.lock);
296 }
297 
298 static void vc4_bo_purge(struct drm_gem_object *obj)
299 {
300 	struct vc4_bo *bo = to_vc4_bo(obj);
301 	struct drm_device *dev = obj->dev;
302 
303 	WARN_ON(!mutex_is_locked(&bo->madv_lock));
304 	WARN_ON(bo->madv != VC4_MADV_DONTNEED);
305 
306 	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
307 
308 	dma_free_wc(dev->dev, obj->size, bo->base.vaddr, bo->base.dma_addr);
309 	bo->base.vaddr = NULL;
310 	bo->madv = __VC4_MADV_PURGED;
311 }
312 
313 static void vc4_bo_userspace_cache_purge(struct drm_device *dev)
314 {
315 	struct vc4_dev *vc4 = to_vc4_dev(dev);
316 
317 	mutex_lock(&vc4->purgeable.lock);
318 	while (!list_empty(&vc4->purgeable.list)) {
319 		struct vc4_bo *bo = list_first_entry(&vc4->purgeable.list,
320 						     struct vc4_bo, size_head);
321 		struct drm_gem_object *obj = &bo->base.base;
322 		size_t purged_size = 0;
323 
324 		vc4_bo_remove_from_purgeable_pool_locked(bo);
325 
326 		/* Release the purgeable lock while we're purging the BO so
327 		 * that other people can continue inserting things in the
328 		 * purgeable pool without having to wait for all BOs to be
329 		 * purged.
330 		 */
331 		mutex_unlock(&vc4->purgeable.lock);
332 		mutex_lock(&bo->madv_lock);
333 
334 		/* Since we released the purgeable pool lock before acquiring
335 		 * the BO madv one, the user may have marked the BO as WILLNEED
336 		 * and re-used it in the meantime.
337 		 * Before purging the BO we need to make sure
338 		 * - it is still marked as DONTNEED
339 		 * - it has not been re-inserted in the purgeable list
340 		 * - it is not used by HW blocks
341 		 * If one of these conditions is not met, just skip the entry.
342 		 */
343 		if (bo->madv == VC4_MADV_DONTNEED &&
344 		    list_empty(&bo->size_head) &&
345 		    !refcount_read(&bo->usecnt)) {
346 			purged_size = bo->base.base.size;
347 			vc4_bo_purge(obj);
348 		}
349 		mutex_unlock(&bo->madv_lock);
350 		mutex_lock(&vc4->purgeable.lock);
351 
352 		if (purged_size) {
353 			vc4->purgeable.purged_size += purged_size;
354 			vc4->purgeable.purged_num++;
355 		}
356 	}
357 	mutex_unlock(&vc4->purgeable.lock);
358 }
359 
360 static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
361 					    uint32_t size,
362 					    enum vc4_kernel_bo_type type)
363 {
364 	struct vc4_dev *vc4 = to_vc4_dev(dev);
365 	uint32_t page_index = bo_page_index(size);
366 	struct vc4_bo *bo = NULL;
367 
368 	mutex_lock(&vc4->bo_lock);
369 	if (page_index >= vc4->bo_cache.size_list_size)
370 		goto out;
371 
372 	if (list_empty(&vc4->bo_cache.size_list[page_index]))
373 		goto out;
374 
375 	bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
376 			      struct vc4_bo, size_head);
377 	vc4_bo_remove_from_cache(bo);
378 	kref_init(&bo->base.base.refcount);
379 
380 out:
381 	if (bo)
382 		vc4_bo_set_label(&bo->base.base, type);
383 	mutex_unlock(&vc4->bo_lock);
384 	return bo;
385 }
386 
387 /**
388  * vc4_create_object - Implementation of driver->gem_create_object.
389  * @dev: DRM device
390  * @size: Size in bytes of the memory the object will reference
391  *
392  * This lets the DMA helpers allocate object structs for us, and keep
393  * our BO stats correct.
394  */
395 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
396 {
397 	struct vc4_dev *vc4 = to_vc4_dev(dev);
398 	struct vc4_bo *bo;
399 
400 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
401 		return ERR_PTR(-ENODEV);
402 
403 	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
404 	if (!bo)
405 		return ERR_PTR(-ENOMEM);
406 
407 	bo->madv = VC4_MADV_WILLNEED;
408 	refcount_set(&bo->usecnt, 0);
409 
410 	mutex_init(&bo->madv_lock);
411 
412 	mutex_lock(&vc4->bo_lock);
413 	bo->label = VC4_BO_TYPE_KERNEL;
414 	vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
415 	vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
416 	mutex_unlock(&vc4->bo_lock);
417 
418 	bo->base.base.funcs = &vc4_gem_object_funcs;
419 
420 	return &bo->base.base;
421 }
422 
423 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
424 			     bool allow_unzeroed, enum vc4_kernel_bo_type type)
425 {
426 	size_t size = roundup(unaligned_size, PAGE_SIZE);
427 	struct vc4_dev *vc4 = to_vc4_dev(dev);
428 	struct drm_gem_dma_object *dma_obj;
429 	struct vc4_bo *bo;
430 
431 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
432 		return ERR_PTR(-ENODEV);
433 
434 	if (size == 0)
435 		return ERR_PTR(-EINVAL);
436 
437 	/* First, try to get a vc4_bo from the kernel BO cache. */
438 	bo = vc4_bo_get_from_cache(dev, size, type);
439 	if (bo) {
440 		if (!allow_unzeroed)
441 			memset(bo->base.vaddr, 0, bo->base.base.size);
442 		return bo;
443 	}
444 
445 	dma_obj = drm_gem_dma_create(dev, size);
446 	if (IS_ERR(dma_obj)) {
447 		/*
448 		 * If we've run out of DMA memory, kill the cache of
449 		 * DMA allocations we've got laying around and try again.
450 		 */
451 		vc4_bo_cache_purge(dev);
452 		dma_obj = drm_gem_dma_create(dev, size);
453 	}
454 
455 	if (IS_ERR(dma_obj)) {
456 		/*
457 		 * Still not enough DMA memory, purge the userspace BO
458 		 * cache and retry.
459 		 * This is sub-optimal since we purge the whole userspace
460 		 * BO cache which forces user that want to re-use the BO to
461 		 * restore its initial content.
462 		 * Ideally, we should purge entries one by one and retry
463 		 * after each to see if DMA allocation succeeds. Or even
464 		 * better, try to find an entry with at least the same
465 		 * size.
466 		 */
467 		vc4_bo_userspace_cache_purge(dev);
468 		dma_obj = drm_gem_dma_create(dev, size);
469 	}
470 
471 	if (IS_ERR(dma_obj)) {
472 		struct drm_printer p = drm_info_printer(vc4->base.dev);
473 		drm_err(dev, "Failed to allocate from GEM DMA helper:\n");
474 		vc4_bo_stats_print(&p, vc4);
475 		return ERR_PTR(-ENOMEM);
476 	}
477 	bo = to_vc4_bo(&dma_obj->base);
478 
479 	/* By default, BOs do not support the MADV ioctl. This will be enabled
480 	 * only on BOs that are exposed to userspace (V3D, V3D_SHADER and DUMB
481 	 * BOs).
482 	 */
483 	bo->madv = __VC4_MADV_NOTSUPP;
484 
485 	mutex_lock(&vc4->bo_lock);
486 	vc4_bo_set_label(&dma_obj->base, type);
487 	mutex_unlock(&vc4->bo_lock);
488 
489 	return bo;
490 }
491 
492 int vc4_bo_dumb_create(struct drm_file *file_priv,
493 		       struct drm_device *dev,
494 		       struct drm_mode_create_dumb *args)
495 {
496 	struct vc4_dev *vc4 = to_vc4_dev(dev);
497 	struct vc4_bo *bo = NULL;
498 	int ret;
499 
500 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
501 		return -ENODEV;
502 
503 	ret = vc4_dumb_fixup_args(args);
504 	if (ret)
505 		return ret;
506 
507 	bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
508 	if (IS_ERR(bo))
509 		return PTR_ERR(bo);
510 
511 	bo->madv = VC4_MADV_WILLNEED;
512 
513 	ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
514 	drm_gem_object_put(&bo->base.base);
515 
516 	return ret;
517 }
518 
519 static void vc4_bo_cache_free_old(struct drm_device *dev)
520 {
521 	struct vc4_dev *vc4 = to_vc4_dev(dev);
522 	unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
523 
524 	lockdep_assert_held(&vc4->bo_lock);
525 
526 	while (!list_empty(&vc4->bo_cache.time_list)) {
527 		struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
528 						    struct vc4_bo, unref_head);
529 		if (time_before(expire_time, bo->free_time)) {
530 			mod_timer(&vc4->bo_cache.time_timer,
531 				  round_jiffies_up(jiffies +
532 						   msecs_to_jiffies(1000)));
533 			return;
534 		}
535 
536 		vc4_bo_remove_from_cache(bo);
537 		vc4_bo_destroy(bo);
538 	}
539 }
540 
541 /* Called on the last userspace/kernel unreference of the BO.  Returns
542  * it to the BO cache if possible, otherwise frees it.
543  */
544 static void vc4_free_object(struct drm_gem_object *gem_bo)
545 {
546 	struct drm_device *dev = gem_bo->dev;
547 	struct vc4_dev *vc4 = to_vc4_dev(dev);
548 	struct vc4_bo *bo = to_vc4_bo(gem_bo);
549 	struct list_head *cache_list;
550 
551 	/* Remove the BO from the purgeable list. */
552 	mutex_lock(&bo->madv_lock);
553 	if (bo->madv == VC4_MADV_DONTNEED && !refcount_read(&bo->usecnt))
554 		vc4_bo_remove_from_purgeable_pool(bo);
555 	mutex_unlock(&bo->madv_lock);
556 
557 	mutex_lock(&vc4->bo_lock);
558 	/* If the object references someone else's memory, we can't cache it.
559 	 */
560 	if (gem_bo->import_attach) {
561 		vc4_bo_destroy(bo);
562 		goto out;
563 	}
564 
565 	/* Don't cache if it was publicly named. */
566 	if (gem_bo->name) {
567 		vc4_bo_destroy(bo);
568 		goto out;
569 	}
570 
571 	/* If this object was partially constructed but DMA allocation
572 	 * had failed, just free it. Can also happen when the BO has been
573 	 * purged.
574 	 */
575 	if (!bo->base.vaddr) {
576 		vc4_bo_destroy(bo);
577 		goto out;
578 	}
579 
580 	cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
581 	if (!cache_list) {
582 		vc4_bo_destroy(bo);
583 		goto out;
584 	}
585 
586 	if (bo->validated_shader) {
587 		kfree(bo->validated_shader->uniform_addr_offsets);
588 		kfree(bo->validated_shader->texture_samples);
589 		kfree(bo->validated_shader);
590 		bo->validated_shader = NULL;
591 	}
592 
593 	/* Reset madv and usecnt before adding the BO to the cache. */
594 	bo->madv = __VC4_MADV_NOTSUPP;
595 	refcount_set(&bo->usecnt, 0);
596 
597 	bo->t_format = false;
598 	bo->free_time = jiffies;
599 	list_add(&bo->size_head, cache_list);
600 	list_add(&bo->unref_head, &vc4->bo_cache.time_list);
601 
602 	vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
603 
604 	vc4_bo_cache_free_old(dev);
605 
606 out:
607 	mutex_unlock(&vc4->bo_lock);
608 }
609 
610 static void vc4_bo_cache_time_work(struct work_struct *work)
611 {
612 	struct vc4_dev *vc4 =
613 		container_of(work, struct vc4_dev, bo_cache.time_work);
614 	struct drm_device *dev = &vc4->base;
615 
616 	mutex_lock(&vc4->bo_lock);
617 	vc4_bo_cache_free_old(dev);
618 	mutex_unlock(&vc4->bo_lock);
619 }
620 
621 int vc4_bo_inc_usecnt(struct vc4_bo *bo)
622 {
623 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
624 	int ret;
625 
626 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
627 		return -ENODEV;
628 
629 	/* Fast path: if the BO is already retained by someone, no need to
630 	 * check the madv status.
631 	 */
632 	if (refcount_inc_not_zero(&bo->usecnt))
633 		return 0;
634 
635 	mutex_lock(&bo->madv_lock);
636 	switch (bo->madv) {
637 	case VC4_MADV_WILLNEED:
638 		if (!refcount_inc_not_zero(&bo->usecnt))
639 			refcount_set(&bo->usecnt, 1);
640 		ret = 0;
641 		break;
642 	case VC4_MADV_DONTNEED:
643 		/* We shouldn't use a BO marked as purgeable if at least
644 		 * someone else retained its content by incrementing usecnt.
645 		 * Luckily the BO hasn't been purged yet, but something wrong
646 		 * is happening here. Just throw an error instead of
647 		 * authorizing this use case.
648 		 */
649 	case __VC4_MADV_PURGED:
650 		/* We can't use a purged BO. */
651 	default:
652 		/* Invalid madv value. */
653 		ret = -EINVAL;
654 		break;
655 	}
656 	mutex_unlock(&bo->madv_lock);
657 
658 	return ret;
659 }
660 
661 void vc4_bo_dec_usecnt(struct vc4_bo *bo)
662 {
663 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
664 
665 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
666 		return;
667 
668 	/* Fast path: if the BO is still retained by someone, no need to test
669 	 * the madv value.
670 	 */
671 	if (refcount_dec_not_one(&bo->usecnt))
672 		return;
673 
674 	mutex_lock(&bo->madv_lock);
675 	if (refcount_dec_and_test(&bo->usecnt) &&
676 	    bo->madv == VC4_MADV_DONTNEED)
677 		vc4_bo_add_to_purgeable_pool(bo);
678 	mutex_unlock(&bo->madv_lock);
679 }
680 
681 static void vc4_bo_cache_time_timer(struct timer_list *t)
682 {
683 	struct vc4_dev *vc4 = timer_container_of(vc4, t, bo_cache.time_timer);
684 
685 	schedule_work(&vc4->bo_cache.time_work);
686 }
687 
688 static struct dma_buf *vc4_prime_export(struct drm_gem_object *obj, int flags)
689 {
690 	struct vc4_bo *bo = to_vc4_bo(obj);
691 	struct dma_buf *dmabuf;
692 	int ret;
693 
694 	if (bo->validated_shader) {
695 		DRM_DEBUG("Attempting to export shader BO\n");
696 		return ERR_PTR(-EINVAL);
697 	}
698 
699 	/* Note: as soon as the BO is exported it becomes unpurgeable, because
700 	 * noone ever decrements the usecnt even if the reference held by the
701 	 * exported BO is released. This shouldn't be a problem since we don't
702 	 * expect exported BOs to be marked as purgeable.
703 	 */
704 	ret = vc4_bo_inc_usecnt(bo);
705 	if (ret) {
706 		drm_err(obj->dev, "Failed to increment BO usecnt\n");
707 		return ERR_PTR(ret);
708 	}
709 
710 	dmabuf = drm_gem_prime_export(obj, flags);
711 	if (IS_ERR(dmabuf))
712 		vc4_bo_dec_usecnt(bo);
713 
714 	return dmabuf;
715 }
716 
717 static vm_fault_t vc4_fault(struct vm_fault *vmf)
718 {
719 	struct vm_area_struct *vma = vmf->vma;
720 	struct drm_gem_object *obj = vma->vm_private_data;
721 	struct vc4_bo *bo = to_vc4_bo(obj);
722 
723 	/* The only reason we would end up here is when user-space accesses
724 	 * BO's memory after it's been purged.
725 	 */
726 	mutex_lock(&bo->madv_lock);
727 	WARN_ON(bo->madv != __VC4_MADV_PURGED);
728 	mutex_unlock(&bo->madv_lock);
729 
730 	return VM_FAULT_SIGBUS;
731 }
732 
733 static int vc4_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
734 {
735 	struct vc4_bo *bo = to_vc4_bo(obj);
736 
737 	if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
738 		DRM_DEBUG("mmapping of shader BOs for writing not allowed.\n");
739 		return -EINVAL;
740 	}
741 
742 	if (bo->madv != VC4_MADV_WILLNEED) {
743 		DRM_DEBUG("mmapping of %s BO not allowed\n",
744 			  bo->madv == VC4_MADV_DONTNEED ?
745 			  "purgeable" : "purged");
746 		return -EINVAL;
747 	}
748 
749 	return drm_gem_dma_mmap(&bo->base, vma);
750 }
751 
752 static const struct vm_operations_struct vc4_vm_ops = {
753 	.fault = vc4_fault,
754 	.open = drm_gem_vm_open,
755 	.close = drm_gem_vm_close,
756 };
757 
758 static const struct drm_gem_object_funcs vc4_gem_object_funcs = {
759 	.free = vc4_free_object,
760 	.export = vc4_prime_export,
761 	.get_sg_table = drm_gem_dma_object_get_sg_table,
762 	.vmap = drm_gem_dma_object_vmap,
763 	.mmap = vc4_gem_object_mmap,
764 	.vm_ops = &vc4_vm_ops,
765 };
766 
767 static int vc4_grab_bin_bo(struct vc4_dev *vc4, struct vc4_file *vc4file)
768 {
769 	if (!vc4->v3d)
770 		return -ENODEV;
771 
772 	if (vc4file->bin_bo_used)
773 		return 0;
774 
775 	return vc4_v3d_bin_bo_get(vc4, &vc4file->bin_bo_used);
776 }
777 
778 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
779 			struct drm_file *file_priv)
780 {
781 	struct drm_vc4_create_bo *args = data;
782 	struct vc4_file *vc4file = file_priv->driver_priv;
783 	struct vc4_dev *vc4 = to_vc4_dev(dev);
784 	struct vc4_bo *bo = NULL;
785 	int ret;
786 
787 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
788 		return -ENODEV;
789 
790 	ret = vc4_grab_bin_bo(vc4, vc4file);
791 	if (ret)
792 		return ret;
793 
794 	/*
795 	 * We can't allocate from the BO cache, because the BOs don't
796 	 * get zeroed, and that might leak data between users.
797 	 */
798 	bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
799 	if (IS_ERR(bo))
800 		return PTR_ERR(bo);
801 
802 	bo->madv = VC4_MADV_WILLNEED;
803 
804 	ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
805 	drm_gem_object_put(&bo->base.base);
806 
807 	return ret;
808 }
809 
810 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
811 		      struct drm_file *file_priv)
812 {
813 	struct vc4_dev *vc4 = to_vc4_dev(dev);
814 	struct drm_vc4_mmap_bo *args = data;
815 	struct drm_gem_object *gem_obj;
816 
817 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
818 		return -ENODEV;
819 
820 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
821 	if (!gem_obj) {
822 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
823 		return -EINVAL;
824 	}
825 
826 	/* The mmap offset was set up at BO allocation time. */
827 	args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
828 
829 	drm_gem_object_put(gem_obj);
830 	return 0;
831 }
832 
833 int
834 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
835 			   struct drm_file *file_priv)
836 {
837 	struct drm_vc4_create_shader_bo *args = data;
838 	struct vc4_file *vc4file = file_priv->driver_priv;
839 	struct vc4_dev *vc4 = to_vc4_dev(dev);
840 	struct vc4_bo *bo = NULL;
841 	int ret;
842 
843 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
844 		return -ENODEV;
845 
846 	if (args->size == 0)
847 		return -EINVAL;
848 
849 	if (args->size % sizeof(u64) != 0)
850 		return -EINVAL;
851 
852 	if (args->flags != 0) {
853 		DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
854 		return -EINVAL;
855 	}
856 
857 	if (args->pad != 0) {
858 		DRM_INFO("Pad set: 0x%08x\n", args->pad);
859 		return -EINVAL;
860 	}
861 
862 	ret = vc4_grab_bin_bo(vc4, vc4file);
863 	if (ret)
864 		return ret;
865 
866 	bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
867 	if (IS_ERR(bo))
868 		return PTR_ERR(bo);
869 
870 	bo->madv = VC4_MADV_WILLNEED;
871 
872 	if (copy_from_user(bo->base.vaddr,
873 			     (void __user *)(uintptr_t)args->data,
874 			     args->size)) {
875 		ret = -EFAULT;
876 		goto fail;
877 	}
878 	/* Clear the rest of the memory from allocating from the BO
879 	 * cache.
880 	 */
881 	memset(bo->base.vaddr + args->size, 0,
882 	       bo->base.base.size - args->size);
883 
884 	bo->validated_shader = vc4_validate_shader(&bo->base);
885 	if (!bo->validated_shader) {
886 		ret = -EINVAL;
887 		goto fail;
888 	}
889 
890 	/* We have to create the handle after validation, to avoid
891 	 * races for users to do doing things like mmap the shader BO.
892 	 */
893 	ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
894 
895 fail:
896 	drm_gem_object_put(&bo->base.base);
897 
898 	return ret;
899 }
900 
901 /**
902  * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
903  * @dev: DRM device
904  * @data: ioctl argument
905  * @file_priv: DRM file for this fd
906  *
907  * The tiling state of the BO decides the default modifier of an fb if
908  * no specific modifier was set by userspace, and the return value of
909  * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
910  * received from dmabuf as the same tiling format as the producer
911  * used).
912  */
913 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
914 			 struct drm_file *file_priv)
915 {
916 	struct vc4_dev *vc4 = to_vc4_dev(dev);
917 	struct drm_vc4_set_tiling *args = data;
918 	struct drm_gem_object *gem_obj;
919 	struct vc4_bo *bo;
920 	bool t_format;
921 
922 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
923 		return -ENODEV;
924 
925 	if (args->flags != 0)
926 		return -EINVAL;
927 
928 	switch (args->modifier) {
929 	case DRM_FORMAT_MOD_NONE:
930 		t_format = false;
931 		break;
932 	case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
933 		t_format = true;
934 		break;
935 	default:
936 		return -EINVAL;
937 	}
938 
939 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
940 	if (!gem_obj) {
941 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
942 		return -ENOENT;
943 	}
944 	bo = to_vc4_bo(gem_obj);
945 	bo->t_format = t_format;
946 
947 	drm_gem_object_put(gem_obj);
948 
949 	return 0;
950 }
951 
952 /**
953  * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
954  * @dev: DRM device
955  * @data: ioctl argument
956  * @file_priv: DRM file for this fd
957  *
958  * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
959  */
960 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
961 			 struct drm_file *file_priv)
962 {
963 	struct vc4_dev *vc4 = to_vc4_dev(dev);
964 	struct drm_vc4_get_tiling *args = data;
965 	struct drm_gem_object *gem_obj;
966 	struct vc4_bo *bo;
967 
968 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
969 		return -ENODEV;
970 
971 	if (args->flags != 0 || args->modifier != 0)
972 		return -EINVAL;
973 
974 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
975 	if (!gem_obj) {
976 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
977 		return -ENOENT;
978 	}
979 	bo = to_vc4_bo(gem_obj);
980 
981 	if (bo->t_format)
982 		args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
983 	else
984 		args->modifier = DRM_FORMAT_MOD_NONE;
985 
986 	drm_gem_object_put(gem_obj);
987 
988 	return 0;
989 }
990 
991 int vc4_bo_debugfs_init(struct drm_minor *minor)
992 {
993 	struct drm_device *drm = minor->dev;
994 	struct vc4_dev *vc4 = to_vc4_dev(drm);
995 
996 	if (!vc4->v3d)
997 		return -ENODEV;
998 
999 	drm_debugfs_add_file(drm, "bo_stats", vc4_bo_stats_debugfs, NULL);
1000 
1001 	return 0;
1002 }
1003 
1004 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused);
1005 int vc4_bo_cache_init(struct drm_device *dev)
1006 {
1007 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1008 	int ret;
1009 	int i;
1010 
1011 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1012 		return -ENODEV;
1013 
1014 	/* Create the initial set of BO labels that the kernel will
1015 	 * use.  This lets us avoid a bunch of string reallocation in
1016 	 * the kernel's draw and BO allocation paths.
1017 	 */
1018 	vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
1019 				 GFP_KERNEL);
1020 	if (!vc4->bo_labels)
1021 		return -ENOMEM;
1022 	vc4->num_labels = VC4_BO_TYPE_COUNT;
1023 
1024 	BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
1025 	for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
1026 		vc4->bo_labels[i].name = bo_type_names[i];
1027 
1028 	ret = drmm_mutex_init(dev, &vc4->bo_lock);
1029 	if (ret) {
1030 		kfree(vc4->bo_labels);
1031 		return ret;
1032 	}
1033 
1034 	INIT_LIST_HEAD(&vc4->bo_cache.time_list);
1035 
1036 	INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
1037 	timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
1038 
1039 	return drmm_add_action_or_reset(dev, vc4_bo_cache_destroy, NULL);
1040 }
1041 
1042 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused)
1043 {
1044 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1045 	int i;
1046 
1047 	timer_delete(&vc4->bo_cache.time_timer);
1048 	cancel_work_sync(&vc4->bo_cache.time_work);
1049 
1050 	vc4_bo_cache_purge(dev);
1051 
1052 	for (i = 0; i < vc4->num_labels; i++) {
1053 		if (vc4->bo_labels[i].num_allocated) {
1054 			drm_err(dev, "Destroying BO cache with %d %s "
1055 				"BOs still allocated\n",
1056 				vc4->bo_labels[i].num_allocated,
1057 				vc4->bo_labels[i].name);
1058 		}
1059 
1060 		if (is_user_label(i))
1061 			kfree(vc4->bo_labels[i].name);
1062 	}
1063 	kfree(vc4->bo_labels);
1064 }
1065 
1066 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
1067 		       struct drm_file *file_priv)
1068 {
1069 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1070 	struct drm_vc4_label_bo *args = data;
1071 	char *name;
1072 	struct drm_gem_object *gem_obj;
1073 	int ret = 0, label;
1074 
1075 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1076 		return -ENODEV;
1077 
1078 	if (!args->len)
1079 		return -EINVAL;
1080 
1081 	name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
1082 	if (IS_ERR(name))
1083 		return PTR_ERR(name);
1084 
1085 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1086 	if (!gem_obj) {
1087 		drm_err(dev, "Failed to look up GEM BO %d\n", args->handle);
1088 		kfree(name);
1089 		return -ENOENT;
1090 	}
1091 
1092 	mutex_lock(&vc4->bo_lock);
1093 	label = vc4_get_user_label(vc4, name);
1094 	if (label != -1)
1095 		vc4_bo_set_label(gem_obj, label);
1096 	else
1097 		ret = -ENOMEM;
1098 	mutex_unlock(&vc4->bo_lock);
1099 
1100 	drm_gem_object_put(gem_obj);
1101 
1102 	return ret;
1103 }
1104