blob: 4ae45d7dac428984fa63081338ac23f283fc5ac8 [file] [log] [blame]
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001/*
2 * Copyright © 2015 Broadcom
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
Eric Anholt72f793f2017-02-27 12:11:41 -08009/**
10 * DOC: VC4 GEM BO management support
Eric Anholtc8b75bc2015-03-02 13:01:12 -080011 *
12 * The VC4 GPU architecture (both scanout and rendering) has direct
13 * access to system memory with no MMU in between. To support it, we
14 * use the GEM CMA helper functions to allocate contiguous ranges of
15 * physical memory for our BOs.
Eric Anholtc826a6e2015-10-09 20:25:07 -070016 *
17 * Since the CMA allocator is very slow, we keep a cache of recently
18 * freed BOs around so that the kernel's allocation of objects for 3D
19 * rendering can return quickly.
Eric Anholtc8b75bc2015-03-02 13:01:12 -080020 */
21
Eric Anholtcdec4d32017-04-12 12:12:02 -070022#include <linux/dma-buf.h>
23
Eric Anholtc8b75bc2015-03-02 13:01:12 -080024#include "vc4_drv.h"
Eric Anholtd5bc60f2015-01-18 09:33:17 +130025#include "uapi/drm/vc4_drm.h"
Eric Anholtc8b75bc2015-03-02 13:01:12 -080026
Eric Anholtf3099462017-07-25 11:27:17 -070027static const char * const bo_type_names[] = {
28 "kernel",
29 "V3D",
30 "V3D shader",
31 "dumb",
32 "binner",
33 "RCL",
34 "BCL",
35 "kernel BO cache",
36};
37
38static bool is_user_label(int label)
39{
40 return label >= VC4_BO_TYPE_COUNT;
41}
42
Eric Anholtc826a6e2015-10-09 20:25:07 -070043static void vc4_bo_stats_dump(struct vc4_dev *vc4)
Eric Anholtc8b75bc2015-03-02 13:01:12 -080044{
Eric Anholtf3099462017-07-25 11:27:17 -070045 int i;
46
47 for (i = 0; i < vc4->num_labels; i++) {
48 if (!vc4->bo_labels[i].num_allocated)
49 continue;
50
51 DRM_INFO("%30s: %6dkb BOs (%d)\n",
52 vc4->bo_labels[i].name,
53 vc4->bo_labels[i].size_allocated / 1024,
54 vc4->bo_labels[i].num_allocated);
55 }
Boris Brezillonb9f19252017-10-19 14:57:48 +020056
57 mutex_lock(&vc4->purgeable.lock);
58 if (vc4->purgeable.num)
59 DRM_INFO("%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
60 vc4->purgeable.size / 1024, vc4->purgeable.num);
61
62 if (vc4->purgeable.purged_num)
63 DRM_INFO("%30s: %6zdkb BOs (%d)\n", "total purged BO",
64 vc4->purgeable.purged_size / 1024,
65 vc4->purgeable.purged_num);
66 mutex_unlock(&vc4->purgeable.lock);
Eric Anholtc826a6e2015-10-09 20:25:07 -070067}
68
69#ifdef CONFIG_DEBUG_FS
70int 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);
Eric Anholtf3099462017-07-25 11:27:17 -070075 int i;
Eric Anholtc826a6e2015-10-09 20:25:07 -070076
Eric Anholtc826a6e2015-10-09 20:25:07 -070077 mutex_lock(&vc4->bo_lock);
Eric Anholtf3099462017-07-25 11:27:17 -070078 for (i = 0; i < vc4->num_labels; i++) {
79 if (!vc4->bo_labels[i].num_allocated)
80 continue;
Eric Anholtc826a6e2015-10-09 20:25:07 -070081
Eric Anholtf3099462017-07-25 11:27:17 -070082 seq_printf(m, "%30s: %6dkb BOs (%d)\n",
83 vc4->bo_labels[i].name,
84 vc4->bo_labels[i].size_allocated / 1024,
85 vc4->bo_labels[i].num_allocated);
86 }
87 mutex_unlock(&vc4->bo_lock);
Eric Anholtc826a6e2015-10-09 20:25:07 -070088
Boris Brezillonb9f19252017-10-19 14:57:48 +020089 mutex_lock(&vc4->purgeable.lock);
90 if (vc4->purgeable.num)
Boris BREZILLONe073db52017-11-01 10:57:31 +010091 seq_printf(m, "%30s: %6zdkb BOs (%d)\n", "userspace BO cache",
Boris Brezillonb9f19252017-10-19 14:57:48 +020092 vc4->purgeable.size / 1024, vc4->purgeable.num);
93
94 if (vc4->purgeable.purged_num)
Boris BREZILLONe073db52017-11-01 10:57:31 +010095 seq_printf(m, "%30s: %6zdkb BOs (%d)\n", "total purged BO",
Boris Brezillonb9f19252017-10-19 14:57:48 +020096 vc4->purgeable.purged_size / 1024,
97 vc4->purgeable.purged_num);
98 mutex_unlock(&vc4->purgeable.lock);
99
Eric Anholtc826a6e2015-10-09 20:25:07 -0700100 return 0;
101}
102#endif
103
Eric Anholtf3099462017-07-25 11:27:17 -0700104/* Takes ownership of *name and returns the appropriate slot for it in
105 * the bo_labels[] array, extending it as necessary.
106 *
107 * This is inefficient and could use a hash table instead of walking
108 * an array and strcmp()ing. However, the assumption is that user
109 * labeling will be infrequent (scanout buffers and other long-lived
110 * objects, or debug driver builds), so we can live with it for now.
111 */
112static int vc4_get_user_label(struct vc4_dev *vc4, const char *name)
113{
114 int i;
115 int free_slot = -1;
116
117 for (i = 0; i < vc4->num_labels; i++) {
118 if (!vc4->bo_labels[i].name) {
119 free_slot = i;
120 } else if (strcmp(vc4->bo_labels[i].name, name) == 0) {
121 kfree(name);
122 return i;
123 }
124 }
125
126 if (free_slot != -1) {
127 WARN_ON(vc4->bo_labels[free_slot].num_allocated != 0);
128 vc4->bo_labels[free_slot].name = name;
129 return free_slot;
130 } else {
131 u32 new_label_count = vc4->num_labels + 1;
132 struct vc4_label *new_labels =
133 krealloc(vc4->bo_labels,
134 new_label_count * sizeof(*new_labels),
135 GFP_KERNEL);
136
137 if (!new_labels) {
138 kfree(name);
139 return -1;
140 }
141
142 free_slot = vc4->num_labels;
143 vc4->bo_labels = new_labels;
144 vc4->num_labels = new_label_count;
145
146 vc4->bo_labels[free_slot].name = name;
147 vc4->bo_labels[free_slot].num_allocated = 0;
148 vc4->bo_labels[free_slot].size_allocated = 0;
149
150 return free_slot;
151 }
152}
153
154static void vc4_bo_set_label(struct drm_gem_object *gem_obj, int label)
155{
156 struct vc4_bo *bo = to_vc4_bo(gem_obj);
157 struct vc4_dev *vc4 = to_vc4_dev(gem_obj->dev);
158
159 lockdep_assert_held(&vc4->bo_lock);
160
161 if (label != -1) {
162 vc4->bo_labels[label].num_allocated++;
163 vc4->bo_labels[label].size_allocated += gem_obj->size;
164 }
165
166 vc4->bo_labels[bo->label].num_allocated--;
167 vc4->bo_labels[bo->label].size_allocated -= gem_obj->size;
168
169 if (vc4->bo_labels[bo->label].num_allocated == 0 &&
170 is_user_label(bo->label)) {
171 /* Free user BO label slots on last unreference.
172 * Slots are just where we track the stats for a given
173 * name, and once a name is unused we can reuse that
174 * slot.
175 */
176 kfree(vc4->bo_labels[bo->label].name);
177 vc4->bo_labels[bo->label].name = NULL;
178 }
179
180 bo->label = label;
181}
182
Eric Anholtc826a6e2015-10-09 20:25:07 -0700183static uint32_t bo_page_index(size_t size)
184{
185 return (size / PAGE_SIZE) - 1;
186}
187
Eric Anholtc826a6e2015-10-09 20:25:07 -0700188static void vc4_bo_destroy(struct vc4_bo *bo)
189{
190 struct drm_gem_object *obj = &bo->base.base;
Eric Anholt4e6b1e92017-07-25 11:27:18 -0700191 struct vc4_dev *vc4 = to_vc4_dev(obj->dev);
192
193 lockdep_assert_held(&vc4->bo_lock);
Eric Anholtf3099462017-07-25 11:27:17 -0700194
195 vc4_bo_set_label(obj, -1);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700196
Eric Anholt463873d2015-11-30 11:41:40 -0800197 if (bo->validated_shader) {
198 kfree(bo->validated_shader->texture_samples);
199 kfree(bo->validated_shader);
200 bo->validated_shader = NULL;
201 }
202
Hans Verkuil24bb2062017-06-07 21:05:57 +0200203 reservation_object_fini(&bo->_resv);
Eric Anholtcdec4d32017-04-12 12:12:02 -0700204
Eric Anholtc826a6e2015-10-09 20:25:07 -0700205 drm_gem_cma_free_object(obj);
206}
207
Eric Anholtc826a6e2015-10-09 20:25:07 -0700208static void vc4_bo_remove_from_cache(struct vc4_bo *bo)
209{
Eric Anholt4e6b1e92017-07-25 11:27:18 -0700210 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
211
212 lockdep_assert_held(&vc4->bo_lock);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700213 list_del(&bo->unref_head);
214 list_del(&bo->size_head);
215}
216
217static struct list_head *vc4_get_cache_list_for_size(struct drm_device *dev,
218 size_t size)
219{
220 struct vc4_dev *vc4 = to_vc4_dev(dev);
221 uint32_t page_index = bo_page_index(size);
222
223 if (vc4->bo_cache.size_list_size <= page_index) {
224 uint32_t new_size = max(vc4->bo_cache.size_list_size * 2,
225 page_index + 1);
226 struct list_head *new_list;
227 uint32_t i;
228
229 new_list = kmalloc_array(new_size, sizeof(struct list_head),
230 GFP_KERNEL);
231 if (!new_list)
232 return NULL;
233
234 /* Rebase the old cached BO lists to their new list
235 * head locations.
236 */
237 for (i = 0; i < vc4->bo_cache.size_list_size; i++) {
238 struct list_head *old_list =
239 &vc4->bo_cache.size_list[i];
240
241 if (list_empty(old_list))
242 INIT_LIST_HEAD(&new_list[i]);
243 else
244 list_replace(old_list, &new_list[i]);
245 }
246 /* And initialize the brand new BO list heads. */
247 for (i = vc4->bo_cache.size_list_size; i < new_size; i++)
248 INIT_LIST_HEAD(&new_list[i]);
249
250 kfree(vc4->bo_cache.size_list);
251 vc4->bo_cache.size_list = new_list;
252 vc4->bo_cache.size_list_size = new_size;
253 }
254
255 return &vc4->bo_cache.size_list[page_index];
256}
257
Baoyou Xieea903832016-09-08 19:03:20 +0800258static void vc4_bo_cache_purge(struct drm_device *dev)
Eric Anholtc826a6e2015-10-09 20:25:07 -0700259{
260 struct vc4_dev *vc4 = to_vc4_dev(dev);
261
262 mutex_lock(&vc4->bo_lock);
263 while (!list_empty(&vc4->bo_cache.time_list)) {
264 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
265 struct vc4_bo, unref_head);
266 vc4_bo_remove_from_cache(bo);
267 vc4_bo_destroy(bo);
268 }
269 mutex_unlock(&vc4->bo_lock);
270}
271
Boris Brezillonb9f19252017-10-19 14:57:48 +0200272void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo)
273{
274 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
275
276 mutex_lock(&vc4->purgeable.lock);
277 list_add_tail(&bo->size_head, &vc4->purgeable.list);
278 vc4->purgeable.num++;
279 vc4->purgeable.size += bo->base.base.size;
280 mutex_unlock(&vc4->purgeable.lock);
281}
282
283static void vc4_bo_remove_from_purgeable_pool_locked(struct vc4_bo *bo)
284{
285 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
286
287 /* list_del_init() is used here because the caller might release
288 * the purgeable lock in order to acquire the madv one and update the
289 * madv status.
290 * During this short period of time a user might decide to mark
291 * the BO as unpurgeable, and if bo->madv is set to
292 * VC4_MADV_DONTNEED it will try to remove the BO from the
293 * purgeable list which will fail if the ->next/prev fields
294 * are set to LIST_POISON1/LIST_POISON2 (which is what
295 * list_del() does).
296 * Re-initializing the list element guarantees that list_del()
297 * will work correctly even if it's a NOP.
298 */
299 list_del_init(&bo->size_head);
300 vc4->purgeable.num--;
301 vc4->purgeable.size -= bo->base.base.size;
302}
303
304void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo)
305{
306 struct vc4_dev *vc4 = to_vc4_dev(bo->base.base.dev);
307
308 mutex_lock(&vc4->purgeable.lock);
309 vc4_bo_remove_from_purgeable_pool_locked(bo);
310 mutex_unlock(&vc4->purgeable.lock);
311}
312
313static void vc4_bo_purge(struct drm_gem_object *obj)
314{
315 struct vc4_bo *bo = to_vc4_bo(obj);
316 struct drm_device *dev = obj->dev;
317
318 WARN_ON(!mutex_is_locked(&bo->madv_lock));
319 WARN_ON(bo->madv != VC4_MADV_DONTNEED);
320
321 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
322
323 dma_free_wc(dev->dev, obj->size, bo->base.vaddr, bo->base.paddr);
324 bo->base.vaddr = NULL;
325 bo->madv = __VC4_MADV_PURGED;
326}
327
328static void vc4_bo_userspace_cache_purge(struct drm_device *dev)
329{
330 struct vc4_dev *vc4 = to_vc4_dev(dev);
331
332 mutex_lock(&vc4->purgeable.lock);
333 while (!list_empty(&vc4->purgeable.list)) {
334 struct vc4_bo *bo = list_first_entry(&vc4->purgeable.list,
335 struct vc4_bo, size_head);
336 struct drm_gem_object *obj = &bo->base.base;
337 size_t purged_size = 0;
338
339 vc4_bo_remove_from_purgeable_pool_locked(bo);
340
341 /* Release the purgeable lock while we're purging the BO so
342 * that other people can continue inserting things in the
343 * purgeable pool without having to wait for all BOs to be
344 * purged.
345 */
346 mutex_unlock(&vc4->purgeable.lock);
347 mutex_lock(&bo->madv_lock);
348
349 /* Since we released the purgeable pool lock before acquiring
350 * the BO madv one, the user may have marked the BO as WILLNEED
351 * and re-used it in the meantime.
352 * Before purging the BO we need to make sure
353 * - it is still marked as DONTNEED
354 * - it has not been re-inserted in the purgeable list
355 * - it is not used by HW blocks
356 * If one of these conditions is not met, just skip the entry.
357 */
358 if (bo->madv == VC4_MADV_DONTNEED &&
359 list_empty(&bo->size_head) &&
360 !refcount_read(&bo->usecnt)) {
361 purged_size = bo->base.base.size;
362 vc4_bo_purge(obj);
363 }
364 mutex_unlock(&bo->madv_lock);
365 mutex_lock(&vc4->purgeable.lock);
366
367 if (purged_size) {
368 vc4->purgeable.purged_size += purged_size;
369 vc4->purgeable.purged_num++;
370 }
371 }
372 mutex_unlock(&vc4->purgeable.lock);
373}
374
Eric Anholtc826a6e2015-10-09 20:25:07 -0700375static struct vc4_bo *vc4_bo_get_from_cache(struct drm_device *dev,
Eric Anholtf3099462017-07-25 11:27:17 -0700376 uint32_t size,
377 enum vc4_kernel_bo_type type)
Eric Anholtc826a6e2015-10-09 20:25:07 -0700378{
379 struct vc4_dev *vc4 = to_vc4_dev(dev);
380 uint32_t page_index = bo_page_index(size);
381 struct vc4_bo *bo = NULL;
382
383 size = roundup(size, PAGE_SIZE);
384
385 mutex_lock(&vc4->bo_lock);
386 if (page_index >= vc4->bo_cache.size_list_size)
387 goto out;
388
389 if (list_empty(&vc4->bo_cache.size_list[page_index]))
390 goto out;
391
392 bo = list_first_entry(&vc4->bo_cache.size_list[page_index],
393 struct vc4_bo, size_head);
394 vc4_bo_remove_from_cache(bo);
395 kref_init(&bo->base.base.refcount);
396
397out:
Eric Anholtf3099462017-07-25 11:27:17 -0700398 if (bo)
399 vc4_bo_set_label(&bo->base.base, type);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700400 mutex_unlock(&vc4->bo_lock);
401 return bo;
402}
403
404/**
405 * vc4_gem_create_object - Implementation of driver->gem_create_object.
Eric Anholt72f793f2017-02-27 12:11:41 -0800406 * @dev: DRM device
407 * @size: Size in bytes of the memory the object will reference
Eric Anholtc826a6e2015-10-09 20:25:07 -0700408 *
409 * This lets the CMA helpers allocate object structs for us, and keep
410 * our BO stats correct.
411 */
412struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size)
413{
414 struct vc4_dev *vc4 = to_vc4_dev(dev);
415 struct vc4_bo *bo;
416
417 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
418 if (!bo)
419 return ERR_PTR(-ENOMEM);
420
Boris Brezillonb9f19252017-10-19 14:57:48 +0200421 bo->madv = VC4_MADV_WILLNEED;
422 refcount_set(&bo->usecnt, 0);
423 mutex_init(&bo->madv_lock);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700424 mutex_lock(&vc4->bo_lock);
Eric Anholtf3099462017-07-25 11:27:17 -0700425 bo->label = VC4_BO_TYPE_KERNEL;
426 vc4->bo_labels[VC4_BO_TYPE_KERNEL].num_allocated++;
427 vc4->bo_labels[VC4_BO_TYPE_KERNEL].size_allocated += size;
Eric Anholtc826a6e2015-10-09 20:25:07 -0700428 mutex_unlock(&vc4->bo_lock);
Hans Verkuil24bb2062017-06-07 21:05:57 +0200429 bo->resv = &bo->_resv;
430 reservation_object_init(bo->resv);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700431
432 return &bo->base.base;
433}
434
435struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t unaligned_size,
Eric Anholtf3099462017-07-25 11:27:17 -0700436 bool allow_unzeroed, enum vc4_kernel_bo_type type)
Eric Anholtc826a6e2015-10-09 20:25:07 -0700437{
438 size_t size = roundup(unaligned_size, PAGE_SIZE);
439 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800440 struct drm_gem_cma_object *cma_obj;
Eric Anholteb981382017-03-01 10:56:01 -0800441 struct vc4_bo *bo;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800442
Eric Anholtc826a6e2015-10-09 20:25:07 -0700443 if (size == 0)
Eric Anholt2c68f1f2016-01-25 14:13:12 -0800444 return ERR_PTR(-EINVAL);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700445
446 /* First, try to get a vc4_bo from the kernel BO cache. */
Eric Anholtf3099462017-07-25 11:27:17 -0700447 bo = vc4_bo_get_from_cache(dev, size, type);
Eric Anholteb981382017-03-01 10:56:01 -0800448 if (bo) {
449 if (!allow_unzeroed)
450 memset(bo->base.vaddr, 0, bo->base.base.size);
451 return bo;
Eric Anholtc826a6e2015-10-09 20:25:07 -0700452 }
453
454 cma_obj = drm_gem_cma_create(dev, size);
455 if (IS_ERR(cma_obj)) {
456 /*
457 * If we've run out of CMA memory, kill the cache of
458 * CMA allocations we've got laying around and try again.
459 */
460 vc4_bo_cache_purge(dev);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700461 cma_obj = drm_gem_cma_create(dev, size);
Boris Brezillonb9f19252017-10-19 14:57:48 +0200462 }
463
464 if (IS_ERR(cma_obj)) {
465 /*
466 * Still not enough CMA memory, purge the userspace BO
467 * cache and retry.
468 * This is sub-optimal since we purge the whole userspace
469 * BO cache which forces user that want to re-use the BO to
470 * restore its initial content.
471 * Ideally, we should purge entries one by one and retry
472 * after each to see if CMA allocation succeeds. Or even
473 * better, try to find an entry with at least the same
474 * size.
475 */
476 vc4_bo_userspace_cache_purge(dev);
477 cma_obj = drm_gem_cma_create(dev, size);
478 }
479
480 if (IS_ERR(cma_obj)) {
481 DRM_ERROR("Failed to allocate from CMA:\n");
482 vc4_bo_stats_dump(vc4);
483 return ERR_PTR(-ENOMEM);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700484 }
Eric Anholtf3099462017-07-25 11:27:17 -0700485 bo = to_vc4_bo(&cma_obj->base);
486
Boris Brezillonb9f19252017-10-19 14:57:48 +0200487 /* By default, BOs do not support the MADV ioctl. This will be enabled
488 * only on BOs that are exposed to userspace (V3D, V3D_SHADER and DUMB
489 * BOs).
490 */
491 bo->madv = __VC4_MADV_NOTSUPP;
492
Eric Anholtf3099462017-07-25 11:27:17 -0700493 mutex_lock(&vc4->bo_lock);
494 vc4_bo_set_label(&cma_obj->base, type);
495 mutex_unlock(&vc4->bo_lock);
496
497 return bo;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800498}
499
500int vc4_dumb_create(struct drm_file *file_priv,
501 struct drm_device *dev,
502 struct drm_mode_create_dumb *args)
503{
504 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
505 struct vc4_bo *bo = NULL;
506 int ret;
507
508 if (args->pitch < min_pitch)
509 args->pitch = min_pitch;
510
511 if (args->size < args->pitch * args->height)
512 args->size = args->pitch * args->height;
513
Eric Anholtf3099462017-07-25 11:27:17 -0700514 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_DUMB);
Eric Anholt2c68f1f2016-01-25 14:13:12 -0800515 if (IS_ERR(bo))
516 return PTR_ERR(bo);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800517
Boris Brezillonb9f19252017-10-19 14:57:48 +0200518 bo->madv = VC4_MADV_WILLNEED;
519
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800520 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300521 drm_gem_object_put_unlocked(&bo->base.base);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800522
523 return ret;
524}
Eric Anholtc826a6e2015-10-09 20:25:07 -0700525
Eric Anholtc826a6e2015-10-09 20:25:07 -0700526static void vc4_bo_cache_free_old(struct drm_device *dev)
527{
528 struct vc4_dev *vc4 = to_vc4_dev(dev);
529 unsigned long expire_time = jiffies - msecs_to_jiffies(1000);
530
Eric Anholt4e6b1e92017-07-25 11:27:18 -0700531 lockdep_assert_held(&vc4->bo_lock);
532
Eric Anholtc826a6e2015-10-09 20:25:07 -0700533 while (!list_empty(&vc4->bo_cache.time_list)) {
534 struct vc4_bo *bo = list_last_entry(&vc4->bo_cache.time_list,
535 struct vc4_bo, unref_head);
536 if (time_before(expire_time, bo->free_time)) {
537 mod_timer(&vc4->bo_cache.time_timer,
538 round_jiffies_up(jiffies +
539 msecs_to_jiffies(1000)));
540 return;
541 }
542
543 vc4_bo_remove_from_cache(bo);
544 vc4_bo_destroy(bo);
545 }
546}
547
548/* Called on the last userspace/kernel unreference of the BO. Returns
549 * it to the BO cache if possible, otherwise frees it.
Eric Anholtc826a6e2015-10-09 20:25:07 -0700550 */
551void vc4_free_object(struct drm_gem_object *gem_bo)
552{
553 struct drm_device *dev = gem_bo->dev;
554 struct vc4_dev *vc4 = to_vc4_dev(dev);
555 struct vc4_bo *bo = to_vc4_bo(gem_bo);
556 struct list_head *cache_list;
557
Boris Brezillonb9f19252017-10-19 14:57:48 +0200558 /* Remove the BO from the purgeable list. */
559 mutex_lock(&bo->madv_lock);
560 if (bo->madv == VC4_MADV_DONTNEED && !refcount_read(&bo->usecnt))
561 vc4_bo_remove_from_purgeable_pool(bo);
562 mutex_unlock(&bo->madv_lock);
563
Eric Anholtc826a6e2015-10-09 20:25:07 -0700564 mutex_lock(&vc4->bo_lock);
565 /* If the object references someone else's memory, we can't cache it.
566 */
567 if (gem_bo->import_attach) {
568 vc4_bo_destroy(bo);
569 goto out;
570 }
571
572 /* Don't cache if it was publicly named. */
573 if (gem_bo->name) {
574 vc4_bo_destroy(bo);
575 goto out;
576 }
577
Eric Anholtca39b442017-03-01 10:56:02 -0800578 /* If this object was partially constructed but CMA allocation
Boris Brezillonb9f19252017-10-19 14:57:48 +0200579 * had failed, just free it. Can also happen when the BO has been
580 * purged.
Eric Anholtca39b442017-03-01 10:56:02 -0800581 */
582 if (!bo->base.vaddr) {
583 vc4_bo_destroy(bo);
584 goto out;
585 }
586
Eric Anholtc826a6e2015-10-09 20:25:07 -0700587 cache_list = vc4_get_cache_list_for_size(dev, gem_bo->size);
588 if (!cache_list) {
589 vc4_bo_destroy(bo);
590 goto out;
591 }
592
Eric Anholt463873d2015-11-30 11:41:40 -0800593 if (bo->validated_shader) {
594 kfree(bo->validated_shader->texture_samples);
595 kfree(bo->validated_shader);
596 bo->validated_shader = NULL;
597 }
598
Boris Brezillonb9f19252017-10-19 14:57:48 +0200599 /* Reset madv and usecnt before adding the BO to the cache. */
600 bo->madv = __VC4_MADV_NOTSUPP;
601 refcount_set(&bo->usecnt, 0);
602
Eric Anholt83753112017-06-07 17:13:36 -0700603 bo->t_format = false;
Eric Anholtc826a6e2015-10-09 20:25:07 -0700604 bo->free_time = jiffies;
605 list_add(&bo->size_head, cache_list);
606 list_add(&bo->unref_head, &vc4->bo_cache.time_list);
607
Eric Anholtf3099462017-07-25 11:27:17 -0700608 vc4_bo_set_label(&bo->base.base, VC4_BO_TYPE_KERNEL_CACHE);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700609
610 vc4_bo_cache_free_old(dev);
611
612out:
613 mutex_unlock(&vc4->bo_lock);
614}
615
616static void vc4_bo_cache_time_work(struct work_struct *work)
617{
618 struct vc4_dev *vc4 =
619 container_of(work, struct vc4_dev, bo_cache.time_work);
620 struct drm_device *dev = vc4->dev;
621
622 mutex_lock(&vc4->bo_lock);
623 vc4_bo_cache_free_old(dev);
624 mutex_unlock(&vc4->bo_lock);
625}
626
Boris Brezillonb9f19252017-10-19 14:57:48 +0200627int vc4_bo_inc_usecnt(struct vc4_bo *bo)
628{
629 int ret;
630
631 /* Fast path: if the BO is already retained by someone, no need to
632 * check the madv status.
633 */
634 if (refcount_inc_not_zero(&bo->usecnt))
635 return 0;
636
637 mutex_lock(&bo->madv_lock);
638 switch (bo->madv) {
639 case VC4_MADV_WILLNEED:
640 refcount_inc(&bo->usecnt);
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
662void vc4_bo_dec_usecnt(struct vc4_bo *bo)
663{
664 /* Fast path: if the BO is still retained by someone, no need to test
665 * the madv value.
666 */
667 if (refcount_dec_not_one(&bo->usecnt))
668 return;
669
670 mutex_lock(&bo->madv_lock);
671 if (refcount_dec_and_test(&bo->usecnt) &&
672 bo->madv == VC4_MADV_DONTNEED)
673 vc4_bo_add_to_purgeable_pool(bo);
674 mutex_unlock(&bo->madv_lock);
675}
676
Kees Cook00787302017-10-24 08:16:48 -0700677static void vc4_bo_cache_time_timer(struct timer_list *t)
Eric Anholtc826a6e2015-10-09 20:25:07 -0700678{
Kees Cook00787302017-10-24 08:16:48 -0700679 struct vc4_dev *vc4 = from_timer(vc4, t, bo_cache.time_timer);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700680
681 schedule_work(&vc4->bo_cache.time_work);
682}
683
Eric Anholtcdec4d32017-04-12 12:12:02 -0700684struct reservation_object *vc4_prime_res_obj(struct drm_gem_object *obj)
685{
686 struct vc4_bo *bo = to_vc4_bo(obj);
687
688 return bo->resv;
689}
690
Eric Anholt463873d2015-11-30 11:41:40 -0800691struct dma_buf *
692vc4_prime_export(struct drm_device *dev, struct drm_gem_object *obj, int flags)
693{
694 struct vc4_bo *bo = to_vc4_bo(obj);
Boris Brezillonb9f19252017-10-19 14:57:48 +0200695 struct dma_buf *dmabuf;
696 int ret;
Eric Anholt463873d2015-11-30 11:41:40 -0800697
698 if (bo->validated_shader) {
Eric Anholtfb959922017-07-25 09:27:32 -0700699 DRM_DEBUG("Attempting to export shader BO\n");
Eric Anholt463873d2015-11-30 11:41:40 -0800700 return ERR_PTR(-EINVAL);
701 }
702
Boris Brezillonb9f19252017-10-19 14:57:48 +0200703 /* Note: as soon as the BO is exported it becomes unpurgeable, because
704 * noone ever decrements the usecnt even if the reference held by the
705 * exported BO is released. This shouldn't be a problem since we don't
706 * expect exported BOs to be marked as purgeable.
707 */
708 ret = vc4_bo_inc_usecnt(bo);
709 if (ret) {
710 DRM_ERROR("Failed to increment BO usecnt\n");
711 return ERR_PTR(ret);
712 }
713
714 dmabuf = drm_gem_prime_export(dev, obj, flags);
715 if (IS_ERR(dmabuf))
716 vc4_bo_dec_usecnt(bo);
717
718 return dmabuf;
719}
720
721int vc4_fault(struct vm_fault *vmf)
722{
723 struct vm_area_struct *vma = vmf->vma;
724 struct drm_gem_object *obj = vma->vm_private_data;
725 struct vc4_bo *bo = to_vc4_bo(obj);
726
727 /* The only reason we would end up here is when user-space accesses
728 * BO's memory after it's been purged.
729 */
730 mutex_lock(&bo->madv_lock);
731 WARN_ON(bo->madv != __VC4_MADV_PURGED);
732 mutex_unlock(&bo->madv_lock);
733
734 return VM_FAULT_SIGBUS;
Eric Anholt463873d2015-11-30 11:41:40 -0800735}
736
737int vc4_mmap(struct file *filp, struct vm_area_struct *vma)
738{
739 struct drm_gem_object *gem_obj;
Boris Brezillonb9f19252017-10-19 14:57:48 +0200740 unsigned long vm_pgoff;
Eric Anholt463873d2015-11-30 11:41:40 -0800741 struct vc4_bo *bo;
742 int ret;
743
744 ret = drm_gem_mmap(filp, vma);
745 if (ret)
746 return ret;
747
748 gem_obj = vma->vm_private_data;
749 bo = to_vc4_bo(gem_obj);
750
751 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
Eric Anholtfb959922017-07-25 09:27:32 -0700752 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
Eric Anholt463873d2015-11-30 11:41:40 -0800753 return -EINVAL;
754 }
755
Boris Brezillonb9f19252017-10-19 14:57:48 +0200756 if (bo->madv != VC4_MADV_WILLNEED) {
757 DRM_DEBUG("mmaping of %s BO not allowed\n",
758 bo->madv == VC4_MADV_DONTNEED ?
759 "purgeable" : "purged");
760 return -EINVAL;
761 }
762
Eric Anholt463873d2015-11-30 11:41:40 -0800763 /*
764 * Clear the VM_PFNMAP flag that was set by drm_gem_mmap(), and set the
765 * vm_pgoff (used as a fake buffer offset by DRM) to 0 as we want to map
766 * the whole buffer.
767 */
768 vma->vm_flags &= ~VM_PFNMAP;
Eric Anholt463873d2015-11-30 11:41:40 -0800769
Boris Brezillonb9f19252017-10-19 14:57:48 +0200770 /* This ->vm_pgoff dance is needed to make all parties happy:
771 * - dma_mmap_wc() uses ->vm_pgoff as an offset within the allocated
772 * mem-region, hence the need to set it to zero (the value set by
773 * the DRM core is a virtual offset encoding the GEM object-id)
774 * - the mmap() core logic needs ->vm_pgoff to be restored to its
775 * initial value before returning from this function because it
776 * encodes the offset of this GEM in the dev->anon_inode pseudo-file
777 * and this information will be used when we invalidate userspace
778 * mappings with drm_vma_node_unmap() (called from vc4_gem_purge()).
779 */
780 vm_pgoff = vma->vm_pgoff;
781 vma->vm_pgoff = 0;
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800782 ret = dma_mmap_wc(bo->base.base.dev->dev, vma, bo->base.vaddr,
783 bo->base.paddr, vma->vm_end - vma->vm_start);
Boris Brezillonb9f19252017-10-19 14:57:48 +0200784 vma->vm_pgoff = vm_pgoff;
785
Eric Anholt463873d2015-11-30 11:41:40 -0800786 if (ret)
787 drm_gem_vm_close(vma);
788
789 return ret;
790}
791
792int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
793{
794 struct vc4_bo *bo = to_vc4_bo(obj);
795
796 if (bo->validated_shader && (vma->vm_flags & VM_WRITE)) {
Eric Anholtfb959922017-07-25 09:27:32 -0700797 DRM_DEBUG("mmaping of shader BOs for writing not allowed.\n");
Eric Anholt463873d2015-11-30 11:41:40 -0800798 return -EINVAL;
799 }
800
801 return drm_gem_cma_prime_mmap(obj, vma);
802}
803
804void *vc4_prime_vmap(struct drm_gem_object *obj)
805{
806 struct vc4_bo *bo = to_vc4_bo(obj);
807
808 if (bo->validated_shader) {
Eric Anholtfb959922017-07-25 09:27:32 -0700809 DRM_DEBUG("mmaping of shader BOs not allowed.\n");
Eric Anholt463873d2015-11-30 11:41:40 -0800810 return ERR_PTR(-EINVAL);
811 }
812
813 return drm_gem_cma_prime_vmap(obj);
814}
815
Eric Anholtcdec4d32017-04-12 12:12:02 -0700816struct drm_gem_object *
817vc4_prime_import_sg_table(struct drm_device *dev,
818 struct dma_buf_attachment *attach,
819 struct sg_table *sgt)
820{
821 struct drm_gem_object *obj;
822 struct vc4_bo *bo;
823
824 obj = drm_gem_cma_prime_import_sg_table(dev, attach, sgt);
825 if (IS_ERR(obj))
826 return obj;
827
828 bo = to_vc4_bo(obj);
829 bo->resv = attach->dmabuf->resv;
830
831 return obj;
832}
833
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300834int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
835 struct drm_file *file_priv)
836{
837 struct drm_vc4_create_bo *args = data;
838 struct vc4_bo *bo = NULL;
839 int ret;
840
841 /*
842 * We can't allocate from the BO cache, because the BOs don't
843 * get zeroed, and that might leak data between users.
844 */
Eric Anholtf3099462017-07-25 11:27:17 -0700845 bo = vc4_bo_create(dev, args->size, false, VC4_BO_TYPE_V3D);
Eric Anholt2c68f1f2016-01-25 14:13:12 -0800846 if (IS_ERR(bo))
847 return PTR_ERR(bo);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300848
Boris Brezillonb9f19252017-10-19 14:57:48 +0200849 bo->madv = VC4_MADV_WILLNEED;
850
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300851 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300852 drm_gem_object_put_unlocked(&bo->base.base);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300853
854 return ret;
855}
856
857int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
858 struct drm_file *file_priv)
859{
860 struct drm_vc4_mmap_bo *args = data;
861 struct drm_gem_object *gem_obj;
862
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100863 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300864 if (!gem_obj) {
Eric Anholtfb959922017-07-25 09:27:32 -0700865 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300866 return -EINVAL;
867 }
868
869 /* The mmap offset was set up at BO allocation time. */
870 args->offset = drm_vma_node_offset_addr(&gem_obj->vma_node);
871
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300872 drm_gem_object_put_unlocked(gem_obj);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300873 return 0;
874}
875
Eric Anholt463873d2015-11-30 11:41:40 -0800876int
877vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
878 struct drm_file *file_priv)
879{
880 struct drm_vc4_create_shader_bo *args = data;
881 struct vc4_bo *bo = NULL;
882 int ret;
883
884 if (args->size == 0)
885 return -EINVAL;
886
887 if (args->size % sizeof(u64) != 0)
888 return -EINVAL;
889
890 if (args->flags != 0) {
891 DRM_INFO("Unknown flags set: 0x%08x\n", args->flags);
892 return -EINVAL;
893 }
894
895 if (args->pad != 0) {
896 DRM_INFO("Pad set: 0x%08x\n", args->pad);
897 return -EINVAL;
898 }
899
Eric Anholtf3099462017-07-25 11:27:17 -0700900 bo = vc4_bo_create(dev, args->size, true, VC4_BO_TYPE_V3D_SHADER);
Eric Anholt2c68f1f2016-01-25 14:13:12 -0800901 if (IS_ERR(bo))
902 return PTR_ERR(bo);
Eric Anholt463873d2015-11-30 11:41:40 -0800903
Boris Brezillonb9f19252017-10-19 14:57:48 +0200904 bo->madv = VC4_MADV_WILLNEED;
905
Dan Carpenter585cb132016-03-08 15:09:41 +0300906 if (copy_from_user(bo->base.vaddr,
Eric Anholt463873d2015-11-30 11:41:40 -0800907 (void __user *)(uintptr_t)args->data,
Dan Carpenter585cb132016-03-08 15:09:41 +0300908 args->size)) {
909 ret = -EFAULT;
Eric Anholt463873d2015-11-30 11:41:40 -0800910 goto fail;
Dan Carpenter585cb132016-03-08 15:09:41 +0300911 }
Eric Anholt463873d2015-11-30 11:41:40 -0800912 /* Clear the rest of the memory from allocating from the BO
913 * cache.
914 */
915 memset(bo->base.vaddr + args->size, 0,
916 bo->base.base.size - args->size);
917
918 bo->validated_shader = vc4_validate_shader(&bo->base);
919 if (!bo->validated_shader) {
920 ret = -EINVAL;
921 goto fail;
922 }
923
924 /* We have to create the handle after validation, to avoid
925 * races for users to do doing things like mmap the shader BO.
926 */
927 ret = drm_gem_handle_create(file_priv, &bo->base.base, &args->handle);
928
929 fail:
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300930 drm_gem_object_put_unlocked(&bo->base.base);
Eric Anholt463873d2015-11-30 11:41:40 -0800931
932 return ret;
933}
934
Eric Anholt83753112017-06-07 17:13:36 -0700935/**
936 * vc4_set_tiling_ioctl() - Sets the tiling modifier for a BO.
937 * @dev: DRM device
938 * @data: ioctl argument
939 * @file_priv: DRM file for this fd
940 *
941 * The tiling state of the BO decides the default modifier of an fb if
942 * no specific modifier was set by userspace, and the return value of
943 * vc4_get_tiling_ioctl() (so that userspace can treat a BO it
944 * received from dmabuf as the same tiling format as the producer
945 * used).
946 */
947int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
948 struct drm_file *file_priv)
949{
950 struct drm_vc4_set_tiling *args = data;
951 struct drm_gem_object *gem_obj;
952 struct vc4_bo *bo;
953 bool t_format;
954
955 if (args->flags != 0)
956 return -EINVAL;
957
958 switch (args->modifier) {
959 case DRM_FORMAT_MOD_NONE:
960 t_format = false;
961 break;
962 case DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED:
963 t_format = true;
964 break;
965 default:
966 return -EINVAL;
967 }
968
969 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
970 if (!gem_obj) {
Eric Anholtfb959922017-07-25 09:27:32 -0700971 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
Eric Anholt83753112017-06-07 17:13:36 -0700972 return -ENOENT;
973 }
974 bo = to_vc4_bo(gem_obj);
975 bo->t_format = t_format;
976
Cihangir Akturk1d5494e2017-08-03 14:58:40 +0300977 drm_gem_object_put_unlocked(gem_obj);
Eric Anholt83753112017-06-07 17:13:36 -0700978
979 return 0;
980}
981
982/**
983 * vc4_get_tiling_ioctl() - Gets the tiling modifier for a BO.
984 * @dev: DRM device
985 * @data: ioctl argument
986 * @file_priv: DRM file for this fd
987 *
988 * Returns the tiling modifier for a BO as set by vc4_set_tiling_ioctl().
989 */
990int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
991 struct drm_file *file_priv)
992{
993 struct drm_vc4_get_tiling *args = data;
994 struct drm_gem_object *gem_obj;
995 struct vc4_bo *bo;
996
997 if (args->flags != 0 || args->modifier != 0)
998 return -EINVAL;
999
1000 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1001 if (!gem_obj) {
Eric Anholtfb959922017-07-25 09:27:32 -07001002 DRM_DEBUG("Failed to look up GEM BO %d\n", args->handle);
Eric Anholt83753112017-06-07 17:13:36 -07001003 return -ENOENT;
1004 }
1005 bo = to_vc4_bo(gem_obj);
1006
1007 if (bo->t_format)
1008 args->modifier = DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
1009 else
1010 args->modifier = DRM_FORMAT_MOD_NONE;
1011
Cihangir Akturk1d5494e2017-08-03 14:58:40 +03001012 drm_gem_object_put_unlocked(gem_obj);
Eric Anholt83753112017-06-07 17:13:36 -07001013
1014 return 0;
1015}
1016
Eric Anholtf3099462017-07-25 11:27:17 -07001017int vc4_bo_cache_init(struct drm_device *dev)
Eric Anholtc826a6e2015-10-09 20:25:07 -07001018{
1019 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtf3099462017-07-25 11:27:17 -07001020 int i;
1021
1022 /* Create the initial set of BO labels that the kernel will
1023 * use. This lets us avoid a bunch of string reallocation in
1024 * the kernel's draw and BO allocation paths.
1025 */
1026 vc4->bo_labels = kcalloc(VC4_BO_TYPE_COUNT, sizeof(*vc4->bo_labels),
1027 GFP_KERNEL);
1028 if (!vc4->bo_labels)
1029 return -ENOMEM;
1030 vc4->num_labels = VC4_BO_TYPE_COUNT;
1031
1032 BUILD_BUG_ON(ARRAY_SIZE(bo_type_names) != VC4_BO_TYPE_COUNT);
1033 for (i = 0; i < VC4_BO_TYPE_COUNT; i++)
1034 vc4->bo_labels[i].name = bo_type_names[i];
Eric Anholtc826a6e2015-10-09 20:25:07 -07001035
1036 mutex_init(&vc4->bo_lock);
1037
1038 INIT_LIST_HEAD(&vc4->bo_cache.time_list);
1039
1040 INIT_WORK(&vc4->bo_cache.time_work, vc4_bo_cache_time_work);
Kees Cook00787302017-10-24 08:16:48 -07001041 timer_setup(&vc4->bo_cache.time_timer, vc4_bo_cache_time_timer, 0);
Eric Anholtf3099462017-07-25 11:27:17 -07001042
1043 return 0;
Eric Anholtc826a6e2015-10-09 20:25:07 -07001044}
1045
1046void vc4_bo_cache_destroy(struct drm_device *dev)
1047{
1048 struct vc4_dev *vc4 = to_vc4_dev(dev);
Eric Anholtf3099462017-07-25 11:27:17 -07001049 int i;
Eric Anholtc826a6e2015-10-09 20:25:07 -07001050
1051 del_timer(&vc4->bo_cache.time_timer);
1052 cancel_work_sync(&vc4->bo_cache.time_work);
1053
1054 vc4_bo_cache_purge(dev);
1055
Eric Anholtf3099462017-07-25 11:27:17 -07001056 for (i = 0; i < vc4->num_labels; i++) {
1057 if (vc4->bo_labels[i].num_allocated) {
1058 DRM_ERROR("Destroying BO cache with %d %s "
1059 "BOs still allocated\n",
1060 vc4->bo_labels[i].num_allocated,
1061 vc4->bo_labels[i].name);
1062 }
1063
1064 if (is_user_label(i))
1065 kfree(vc4->bo_labels[i].name);
Eric Anholtc826a6e2015-10-09 20:25:07 -07001066 }
Eric Anholtf3099462017-07-25 11:27:17 -07001067 kfree(vc4->bo_labels);
1068}
1069
1070int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
1071 struct drm_file *file_priv)
1072{
1073 struct vc4_dev *vc4 = to_vc4_dev(dev);
1074 struct drm_vc4_label_bo *args = data;
1075 char *name;
1076 struct drm_gem_object *gem_obj;
1077 int ret = 0, label;
1078
1079 if (!args->len)
1080 return -EINVAL;
1081
1082 name = strndup_user(u64_to_user_ptr(args->name), args->len + 1);
1083 if (IS_ERR(name))
1084 return PTR_ERR(name);
1085
1086 gem_obj = drm_gem_object_lookup(file_priv, args->handle);
1087 if (!gem_obj) {
1088 DRM_ERROR("Failed to look up GEM BO %d\n", args->handle);
1089 kfree(name);
1090 return -ENOENT;
1091 }
1092
1093 mutex_lock(&vc4->bo_lock);
1094 label = vc4_get_user_label(vc4, name);
1095 if (label != -1)
1096 vc4_bo_set_label(gem_obj, label);
1097 else
1098 ret = -ENOMEM;
1099 mutex_unlock(&vc4->bo_lock);
1100
Cihangir Akturkb9c55b62017-08-11 15:33:10 +03001101 drm_gem_object_put_unlocked(gem_obj);
Eric Anholtf3099462017-07-25 11:27:17 -07001102
1103 return ret;
Eric Anholtc826a6e2015-10-09 20:25:07 -07001104}