Linux Audio

Check our new training course

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