xref: /linux/drivers/gpu/drm/vc4/vc4_bo.c (revision c0e1ed41ad86cf5230d64dc5f2c6d2e3aeb59937)
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 #include <linux/overflow.h>
21 
22 #include <drm/drm_fourcc.h>
23 #include <drm/drm_print.h>
24 
25 #include "vc4_drv.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 = kvmalloc_objs(struct list_head, new_size);
209 		if (!new_list)
210 			return NULL;
211 
212 		/* Rebase the old cached BO lists to their new list
213 		 * head locations.
214 		 */
215 		for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
216 			struct list_head *old_list =
217 				&vc4->bo_cache.size_list[i];
218 
219 			if (list_empty(old_list))
220 				INIT_LIST_HEAD(&new_list[i]);
221 			else
222 				list_replace(old_list, &new_list[i]);
223 		}
224 		/* And initialize the brand new BO list heads. */
225 		for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
226 			INIT_LIST_HEAD(&new_list[i]);
227 
228 		kvfree(vc4->bo_cache.size_list);
229 		vc4->bo_cache.size_list = new_list;
230 		vc4->bo_cache.size_list_size = new_size;
231 	}
232 
233 	return &vc4->bo_cache.size_list[page_index];
234 }
235 
236 static void vc4_bo_cache_purge(struct drm_device *dev)
237 {
238 	struct vc4_dev *vc4 = to_vc4_dev(dev);
239 
240 	mutex_lock(&vc4->bo_lock);
241 	while (!list_empty(&vc4->bo_cache.time_list)) {
242 		struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
243 						    struct vc4_bo, unref_head);
244 		vc4_bo_remove_from_cache(bo);
245 		vc4_bo_destroy(bo);
246 	}
247 	mutex_unlock(&vc4->bo_lock);
248 }
249 
250 void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo)
251 {
252 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
253 
254 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
255 		return;
256 
257 	mutex_lock(&vc4->purgeable.lock);
258 	list_add_tail(&bo->size_head, &vc4->purgeable.list);
259 	vc4->purgeable.num++;
260 	vc4->purgeable.size += bo->base.base.size;
261 	mutex_unlock(&vc4->purgeable.lock);
262 }
263 
264 static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo *bo)
265 {
266 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
267 
268 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
269 		return;
270 
271 	/* list_del_init() is used here because the caller might release
272 	 * the purgeable lock in order to acquire the madv one and update the
273 	 * madv status.
274 	 * During this short period of time a user might decide to mark
275 	 * the BO as unpurgeable, and if bo->madv is set to
276 	 * VC4_MADV_DONTNEED it will try to remove the BO from the
277 	 * purgeable list which will fail if the ->next/prev fields
278 	 * are set to LIST_POISON1/LIST_POISON2 (which is what
279 	 * list_del() does).
280 	 * Re-initializing the list element guarantees that list_del()
281 	 * will work correctly even if it's a NOP.
282 	 */
283 	list_del_init(&bo->size_head);
284 	vc4->purgeable.num--;
285 	vc4->purgeable.size -= bo->base.base.size;
286 }
287 
288 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo)
289 {
290 	struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
291 
292 	mutex_lock(&vc4->purgeable.lock);
293 	vc4_bo_remove_from_purgeable_pool_locked(bo);
294 	mutex_unlock(&vc4->purgeable.lock);
295 }
296 
297 static void vc4_bo_purge(struct drm_gem_object *obj)
298 {
299 	struct vc4_bo *bo = to_vc4_bo(obj);
300 	struct drm_device *dev = obj->dev;
301 
302 	WARN_ON(!mutex_is_locked(&bo->madv_lock));
303 	WARN_ON(bo->madv != VC4_MADV_DONTNEED);
304 
305 	drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
306 
307 	dma_free_wc(drm_dev_dma_dev(dev), obj->size, bo->base.vaddr,
308 		    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_obj(*bo);
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 (drm_gem_is_imported(gem_bo)) {
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) {
738 		if (vma->vm_flags & VM_WRITE) {
739 			DRM_DEBUG("mmapping of shader BOs for writing not allowed.\n");
740 			return -EINVAL;
741 		}
742 
743 		vm_flags_clear(vma, VM_MAYWRITE);
744 	}
745 
746 	mutex_lock(&bo->madv_lock);
747 	if (bo->madv != VC4_MADV_WILLNEED) {
748 		DRM_DEBUG("mmapping of %s BO not allowed\n",
749 			  bo->madv == VC4_MADV_DONTNEED ?
750 			  "purgeable" : "purged");
751 		mutex_unlock(&bo->madv_lock);
752 		return -EINVAL;
753 	}
754 	mutex_unlock(&bo->madv_lock);
755 
756 	return drm_gem_dma_mmap(&bo->base, vma);
757 }
758 
759 static const struct vm_operations_struct vc4_vm_ops = {
760 	.fault = vc4_fault,
761 	.open = drm_gem_vm_open,
762 	.close = drm_gem_vm_close,
763 };
764 
765 static const struct drm_gem_object_funcs vc4_gem_object_funcs = {
766 	.free = vc4_free_object,
767 	.export = vc4_prime_export,
768 	.get_sg_table = drm_gem_dma_object_get_sg_table,
769 	.vmap = drm_gem_dma_object_vmap,
770 	.mmap = vc4_gem_object_mmap,
771 	.vm_ops = &vc4_vm_ops,
772 };
773 
774 static int vc4_grab_bin_bo(struct vc4_dev *vc4, struct vc4_file *vc4file)
775 {
776 	if (!vc4->v3d)
777 		return -ENODEV;
778 
779 	if (vc4file->bin_bo_used)
780 		return 0;
781 
782 	return vc4_v3d_bin_bo_get(vc4, &vc4file->bin_bo_used);
783 }
784 
785 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
786 			struct drm_file *file_priv)
787 {
788 	struct drm_vc4_create_bo *args = data;
789 	struct vc4_file *vc4file = file_priv->driver_priv;
790 	struct vc4_dev *vc4 = to_vc4_dev(dev);
791 	struct vc4_bo *bo = NULL;
792 	int ret;
793 
794 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
795 		return -ENODEV;
796 
797 	ret = vc4_grab_bin_bo(vc4, vc4file);
798 	if (ret)
799 		return ret;
800 
801 	/*
802 	 * We can't allocate from the BO cache, because the BOs don't
803 	 * get zeroed, and that might leak data between users.
804 	 */
805 	bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
806 	if (IS_ERR(bo))
807 		return PTR_ERR(bo);
808 
809 	bo->madv = VC4_MADV_WILLNEED;
810 
811 	ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
812 	drm_gem_object_put(&bo->base.base);
813 
814 	return ret;
815 }
816 
817 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
818 		      struct drm_file *file_priv)
819 {
820 	struct vc4_dev *vc4 = to_vc4_dev(dev);
821 	struct drm_vc4_mmap_bo *args = data;
822 	struct drm_gem_object *gem_obj;
823 
824 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
825 		return -ENODEV;
826 
827 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
828 	if (!gem_obj) {
829 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
830 		return -EINVAL;
831 	}
832 
833 	/* The mmap offset was set up at BO allocation time. */
834 	args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
835 
836 	drm_gem_object_put(gem_obj);
837 	return 0;
838 }
839 
840 /*
841  * QPUs sharing a slice's instruction cache can livelock evicting each
842  * other's fills. Considering that shader BOs are page aligned, any code
843  * at offset 0 maps every shader to the same cache sets. This makes a
844  * livelock more likely to happen due to GFXH-16.
845  *
846  * Start each shader at one of four offsets, cycled per client, to spread
847  * them across sets. The use of four different offsets is motivated by the
848  * number of QPUs per slice (4 QPUs).
849  */
850 #define VC4_SHADER_OFFSET_STEP	256
851 #define VC4_SHADER_OFFSET_NUM	4
852 
853 int
854 vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
855 			   struct drm_file *file_priv)
856 {
857 	struct drm_vc4_create_shader_bo *args = data;
858 	struct vc4_file *vc4file = file_priv->driver_priv;
859 	struct vc4_dev *vc4 = to_vc4_dev(dev);
860 	struct vc4_bo *bo = NULL;
861 	u32 offset, size;
862 	int ret;
863 
864 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
865 		return -ENODEV;
866 
867 	if (args->size == 0)
868 		return -EINVAL;
869 
870 	if (args->size % sizeof(u64) != 0)
871 		return -EINVAL;
872 
873 	if (args->flags != 0) {
874 		DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
875 		return -EINVAL;
876 	}
877 
878 	if (args->pad != 0) {
879 		DRM_INFO("Pad set: 0x%08x\n", args->pad);
880 		return -EINVAL;
881 	}
882 
883 	offset = ((u32)atomic_inc_return(&vc4file->shader_offset_idx) %
884 		  VC4_SHADER_OFFSET_NUM) * VC4_SHADER_OFFSET_STEP;
885 
886 	if (check_add_overflow(offset, args->size, &size))
887 		return -EINVAL;
888 
889 	ret = vc4_grab_bin_bo(vc4, vc4file);
890 	if (ret)
891 		return ret;
892 
893 	bo = vc4_bo_create(dev, size, true, VC4_BO_TYPE_V3D_SHADER);
894 	if (IS_ERR(bo))
895 		return PTR_ERR(bo);
896 
897 	bo->madv = VC4_MADV_WILLNEED;
898 	bo->shader_offset = offset;
899 
900 	/* Clear the memory around the code, which may come from the BO
901 	 * cache.
902 	 */
903 	memset(bo->base.vaddr, 0, offset);
904 	if (copy_from_user(bo->base.vaddr + offset,
905 			   (void __user *)(uintptr_t)args->data,
906 			   args->size)) {
907 		ret = -EFAULT;
908 		goto fail;
909 	}
910 	memset(bo->base.vaddr + offset + args->size, 0,
911 	       bo->base.base.size - offset - args->size);
912 
913 	bo->validated_shader = vc4_validate_shader(&bo->base, offset);
914 	if (!bo->validated_shader) {
915 		ret = -EINVAL;
916 		goto fail;
917 	}
918 
919 	/* We have to create the handle after validation, to avoid
920 	 * races for users to do doing things like mmap the shader BO.
921 	 */
922 	ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
923 
924 fail:
925 	drm_gem_object_put(&bo->base.base);
926 
927 	return ret;
928 }
929 
930 /**
931  * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
932  * @dev: DRM device
933  * @data: ioctl argument
934  * @file_priv: DRM file for this fd
935  *
936  * The tiling state of the BO decides the default modifier of an fb if
937  * no specific modifier was set by userspace, and the return value of
938  * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
939  * received from dmabuf as the same tiling format as the producer
940  * used).
941  */
942 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
943 			 struct drm_file *file_priv)
944 {
945 	struct vc4_dev *vc4 = to_vc4_dev(dev);
946 	struct drm_vc4_set_tiling *args = data;
947 	struct drm_gem_object *gem_obj;
948 	struct vc4_bo *bo;
949 	bool t_format;
950 
951 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
952 		return -ENODEV;
953 
954 	if (args->flags != 0)
955 		return -EINVAL;
956 
957 	switch (args->modifier) {
958 	case DRM_FORMAT_MOD_NONE:
959 		t_format = false;
960 		break;
961 	case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
962 		t_format = true;
963 		break;
964 	default:
965 		return -EINVAL;
966 	}
967 
968 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
969 	if (!gem_obj) {
970 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
971 		return -ENOENT;
972 	}
973 	bo = to_vc4_bo(gem_obj);
974 	bo->t_format = t_format;
975 
976 	drm_gem_object_put(gem_obj);
977 
978 	return 0;
979 }
980 
981 /**
982  * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
983  * @dev: DRM device
984  * @data: ioctl argument
985  * @file_priv: DRM file for this fd
986  *
987  * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
988  */
989 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
990 			 struct drm_file *file_priv)
991 {
992 	struct vc4_dev *vc4 = to_vc4_dev(dev);
993 	struct drm_vc4_get_tiling *args = data;
994 	struct drm_gem_object *gem_obj;
995 	struct vc4_bo *bo;
996 
997 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
998 		return -ENODEV;
999 
1000 	if (args->flags != 0 || args->modifier != 0)
1001 		return -EINVAL;
1002 
1003 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1004 	if (!gem_obj) {
1005 		DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
1006 		return -ENOENT;
1007 	}
1008 	bo = to_vc4_bo(gem_obj);
1009 
1010 	if (bo->t_format)
1011 		args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
1012 	else
1013 		args->modifier = DRM_FORMAT_MOD_NONE;
1014 
1015 	drm_gem_object_put(gem_obj);
1016 
1017 	return 0;
1018 }
1019 
1020 int vc4_bo_debugfs_init(struct drm_minor *minor)
1021 {
1022 	struct drm_device *drm = minor->dev;
1023 	struct vc4_dev *vc4 = to_vc4_dev(drm);
1024 
1025 	if (!vc4->v3d)
1026 		return -ENODEV;
1027 
1028 	drm_debugfs_add_file(drm, "bo_stats", vc4_bo_stats_debugfs, NULL);
1029 
1030 	return 0;
1031 }
1032 
1033 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused);
1034 int vc4_bo_cache_init(struct drm_device *dev)
1035 {
1036 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1037 	int ret;
1038 	int i;
1039 
1040 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1041 		return -ENODEV;
1042 
1043 	/* Create the initial set of BO labels that the kernel will
1044 	 * use.  This lets us avoid a bunch of string reallocation in
1045 	 * the kernel's draw and BO allocation paths.
1046 	 */
1047 	vc4->bo_labels = kzalloc_objs(*vc4->bo_labels, VC4_BO_TYPE_COUNT);
1048 	if (!vc4->bo_labels)
1049 		return -ENOMEM;
1050 	vc4->num_labels = VC4_BO_TYPE_COUNT;
1051 
1052 	BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
1053 	for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
1054 		vc4->bo_labels[i].name = bo_type_names[i];
1055 
1056 	ret = drmm_mutex_init(dev, &vc4->bo_lock);
1057 	if (ret) {
1058 		kfree(vc4->bo_labels);
1059 		return ret;
1060 	}
1061 
1062 	INIT_LIST_HEAD(&vc4->bo_cache.time_list);
1063 
1064 	INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
1065 	timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
1066 
1067 	return drmm_add_action_or_reset(dev, vc4_bo_cache_destroy, NULL);
1068 }
1069 
1070 static void vc4_bo_cache_destroy(struct drm_device *dev, void *unused)
1071 {
1072 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1073 	int i;
1074 
1075 	timer_shutdown_sync(&vc4->bo_cache.time_timer);
1076 	cancel_work_sync(&vc4->bo_cache.time_work);
1077 
1078 	vc4_bo_cache_purge(dev);
1079 	kvfree(vc4->bo_cache.size_list);
1080 
1081 	for (i = 0; i < vc4->num_labels; i++) {
1082 		if (vc4->bo_labels[i].num_allocated) {
1083 			drm_err(dev, "Destroying BO cache with %d %s "
1084 				"BOs still allocated\n",
1085 				vc4->bo_labels[i].num_allocated,
1086 				vc4->bo_labels[i].name);
1087 		}
1088 
1089 		if (is_user_label(i))
1090 			kfree(vc4->bo_labels[i].name);
1091 	}
1092 	kfree(vc4->bo_labels);
1093 }
1094 
1095 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
1096 		       struct drm_file *file_priv)
1097 {
1098 	struct vc4_dev *vc4 = to_vc4_dev(dev);
1099 	struct drm_vc4_label_bo *args = data;
1100 	char *name;
1101 	struct drm_gem_object *gem_obj;
1102 	int ret = 0, label;
1103 
1104 	if (WARN_ON_ONCE(vc4->gen > VC4_GEN_4))
1105 		return -ENODEV;
1106 
1107 	if (!args->len)
1108 		return -EINVAL;
1109 
1110 	name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
1111 	if (IS_ERR(name))
1112 		return PTR_ERR(name);
1113 
1114 	gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1115 	if (!gem_obj) {
1116 		drm_err(dev, "Failed to look up GEM BO %d\n", args->handle);
1117 		kfree(name);
1118 		return -ENOENT;
1119 	}
1120 
1121 	mutex_lock(&vc4->bo_lock);
1122 	label = vc4_get_user_label(vc4, name);
1123 	if (label != -1)
1124 		vc4_bo_set_label(gem_obj, label);
1125 	else
1126 		ret = -ENOMEM;
1127 	mutex_unlock(&vc4->bo_lock);
1128 
1129 	drm_gem_object_put(gem_obj);
1130 
1131 	return ret;
1132 }
1133