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