Home | History | Annotate | Line # | Download | only in amdgpu
amdgpu_object.c revision 1.5
      1 /*	$NetBSD: amdgpu_object.c,v 1.5 2021/12/18 23:44:58 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright 2009 Jerome Glisse.
      5  * All Rights Reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sub license, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     18  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     21  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     22  *
     23  * The above copyright notice and this permission notice (including the
     24  * next paragraph) shall be included in all copies or substantial portions
     25  * of the Software.
     26  *
     27  */
     28 /*
     29  * Authors:
     30  *    Jerome Glisse <glisse (at) freedesktop.org>
     31  *    Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
     32  *    Dave Airlie
     33  */
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: amdgpu_object.c,v 1.5 2021/12/18 23:44:58 riastradh Exp $");
     36 
     37 #include <linux/list.h>
     38 #include <linux/slab.h>
     39 
     40 #include <drm/amdgpu_drm.h>
     41 #include <drm/drm_cache.h>
     42 #include "amdgpu.h"
     43 #include "amdgpu_trace.h"
     44 #include "amdgpu_amdkfd.h"
     45 #include <linux/nbsd-namespace.h>
     46 
     47 /**
     48  * DOC: amdgpu_object
     49  *
     50  * This defines the interfaces to operate on an &amdgpu_bo buffer object which
     51  * represents memory used by driver (VRAM, system memory, etc.). The driver
     52  * provides DRM/GEM APIs to userspace. DRM/GEM APIs then use these interfaces
     53  * to create/destroy/set buffer object which are then managed by the kernel TTM
     54  * memory manager.
     55  * The interfaces are also used internally by kernel clients, including gfx,
     56  * uvd, etc. for kernel managed allocations used by the GPU.
     57  *
     58  */
     59 
     60 /**
     61  * amdgpu_bo_subtract_pin_size - Remove BO from pin_size accounting
     62  *
     63  * @bo: &amdgpu_bo buffer object
     64  *
     65  * This function is called when a BO stops being pinned, and updates the
     66  * &amdgpu_device pin_size values accordingly.
     67  */
     68 static void amdgpu_bo_subtract_pin_size(struct amdgpu_bo *bo)
     69 {
     70 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
     71 
     72 	if (bo->tbo.mem.mem_type == TTM_PL_VRAM) {
     73 		atomic64_sub(amdgpu_bo_size(bo), &adev->vram_pin_size);
     74 		atomic64_sub(amdgpu_vram_mgr_bo_visible_size(bo),
     75 			     &adev->visible_pin_size);
     76 	} else if (bo->tbo.mem.mem_type == TTM_PL_TT) {
     77 		atomic64_sub(amdgpu_bo_size(bo), &adev->gart_pin_size);
     78 	}
     79 }
     80 
     81 static void amdgpu_bo_destroy(struct ttm_buffer_object *tbo)
     82 {
     83 	struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
     84 	struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo);
     85 
     86 	if (bo->pin_count > 0)
     87 		amdgpu_bo_subtract_pin_size(bo);
     88 
     89 	amdgpu_bo_kunmap(bo);
     90 
     91 	if (bo->tbo.base.import_attach)
     92 		drm_prime_gem_destroy(&bo->tbo.base, bo->tbo.sg);
     93 	drm_gem_object_release(&bo->tbo.base);
     94 	/* in case amdgpu_device_recover_vram got NULL of bo->parent */
     95 	if (!list_empty(&bo->shadow_list)) {
     96 		mutex_lock(&adev->shadow_list_lock);
     97 		list_del_init(&bo->shadow_list);
     98 		mutex_unlock(&adev->shadow_list_lock);
     99 	}
    100 	amdgpu_bo_unref(&bo->parent);
    101 
    102 	kfree(bo->metadata);
    103 	kfree(bo);
    104 }
    105 
    106 /**
    107  * amdgpu_bo_is_amdgpu_bo - check if the buffer object is an &amdgpu_bo
    108  * @bo: buffer object to be checked
    109  *
    110  * Uses destroy function associated with the object to determine if this is
    111  * an &amdgpu_bo.
    112  *
    113  * Returns:
    114  * true if the object belongs to &amdgpu_bo, false if not.
    115  */
    116 bool amdgpu_bo_is_amdgpu_bo(struct ttm_buffer_object *bo)
    117 {
    118 	if (bo->destroy == &amdgpu_bo_destroy)
    119 		return true;
    120 	return false;
    121 }
    122 
    123 /**
    124  * amdgpu_bo_placement_from_domain - set buffer's placement
    125  * @abo: &amdgpu_bo buffer object whose placement is to be set
    126  * @domain: requested domain
    127  *
    128  * Sets buffer's placement according to requested domain and the buffer's
    129  * flags.
    130  */
    131 void amdgpu_bo_placement_from_domain(struct amdgpu_bo *abo, u32 domain)
    132 {
    133 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
    134 	struct ttm_placement *placement = &abo->placement;
    135 	struct ttm_place *places = abo->placements;
    136 	u64 flags = abo->flags;
    137 	u32 c = 0;
    138 
    139 	if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
    140 		unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
    141 
    142 		places[c].fpfn = 0;
    143 		places[c].lpfn = 0;
    144 		places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED |
    145 			TTM_PL_FLAG_VRAM;
    146 
    147 		if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)
    148 			places[c].lpfn = visible_pfn;
    149 		else
    150 			places[c].flags |= TTM_PL_FLAG_TOPDOWN;
    151 
    152 		if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)
    153 			places[c].flags |= TTM_PL_FLAG_CONTIGUOUS;
    154 		c++;
    155 	}
    156 
    157 	if (domain & AMDGPU_GEM_DOMAIN_GTT) {
    158 		places[c].fpfn = 0;
    159 		places[c].lpfn = 0;
    160 		places[c].flags = TTM_PL_FLAG_TT;
    161 		if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
    162 			places[c].flags |= TTM_PL_FLAG_WC |
    163 				TTM_PL_FLAG_UNCACHED;
    164 		else
    165 			places[c].flags |= TTM_PL_FLAG_CACHED;
    166 		c++;
    167 	}
    168 
    169 	if (domain & AMDGPU_GEM_DOMAIN_CPU) {
    170 		places[c].fpfn = 0;
    171 		places[c].lpfn = 0;
    172 		places[c].flags = TTM_PL_FLAG_SYSTEM;
    173 		if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
    174 			places[c].flags |= TTM_PL_FLAG_WC |
    175 				TTM_PL_FLAG_UNCACHED;
    176 		else
    177 			places[c].flags |= TTM_PL_FLAG_CACHED;
    178 		c++;
    179 	}
    180 
    181 	if (domain & AMDGPU_GEM_DOMAIN_GDS) {
    182 		places[c].fpfn = 0;
    183 		places[c].lpfn = 0;
    184 		places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS;
    185 		c++;
    186 	}
    187 
    188 	if (domain & AMDGPU_GEM_DOMAIN_GWS) {
    189 		places[c].fpfn = 0;
    190 		places[c].lpfn = 0;
    191 		places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS;
    192 		c++;
    193 	}
    194 
    195 	if (domain & AMDGPU_GEM_DOMAIN_OA) {
    196 		places[c].fpfn = 0;
    197 		places[c].lpfn = 0;
    198 		places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA;
    199 		c++;
    200 	}
    201 
    202 	if (!c) {
    203 		places[c].fpfn = 0;
    204 		places[c].lpfn = 0;
    205 		places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM;
    206 		c++;
    207 	}
    208 
    209 	BUG_ON(c >= AMDGPU_BO_MAX_PLACEMENTS);
    210 
    211 	placement->num_placement = c;
    212 	placement->placement = places;
    213 
    214 	placement->num_busy_placement = c;
    215 	placement->busy_placement = places;
    216 }
    217 
    218 /**
    219  * amdgpu_bo_create_reserved - create reserved BO for kernel use
    220  *
    221  * @adev: amdgpu device object
    222  * @size: size for the new BO
    223  * @align: alignment for the new BO
    224  * @domain: where to place it
    225  * @bo_ptr: used to initialize BOs in structures
    226  * @gpu_addr: GPU addr of the pinned BO
    227  * @cpu_addr: optional CPU address mapping
    228  *
    229  * Allocates and pins a BO for kernel internal use, and returns it still
    230  * reserved.
    231  *
    232  * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
    233  *
    234  * Returns:
    235  * 0 on success, negative error code otherwise.
    236  */
    237 int amdgpu_bo_create_reserved(struct amdgpu_device *adev,
    238 			      unsigned long size, int align,
    239 			      u32 domain, struct amdgpu_bo **bo_ptr,
    240 			      u64 *gpu_addr, void **cpu_addr)
    241 {
    242 	struct amdgpu_bo_param bp;
    243 	bool free = false;
    244 	int r;
    245 
    246 	if (!size) {
    247 		amdgpu_bo_unref(bo_ptr);
    248 		return 0;
    249 	}
    250 
    251 	memset(&bp, 0, sizeof(bp));
    252 	bp.size = size;
    253 	bp.byte_align = align;
    254 	bp.domain = domain;
    255 	bp.flags = cpu_addr ? AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED
    256 		: AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
    257 	bp.flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
    258 	bp.type = ttm_bo_type_kernel;
    259 	bp.resv = NULL;
    260 
    261 	if (!*bo_ptr) {
    262 		r = amdgpu_bo_create(adev, &bp, bo_ptr);
    263 		if (r) {
    264 			dev_err(adev->dev, "(%d) failed to allocate kernel bo\n",
    265 				r);
    266 			return r;
    267 		}
    268 		free = true;
    269 	}
    270 
    271 	r = amdgpu_bo_reserve(*bo_ptr, false);
    272 	if (r) {
    273 		dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r);
    274 		goto error_free;
    275 	}
    276 
    277 	r = amdgpu_bo_pin(*bo_ptr, domain);
    278 	if (r) {
    279 		dev_err(adev->dev, "(%d) kernel bo pin failed\n", r);
    280 		goto error_unreserve;
    281 	}
    282 
    283 	r = amdgpu_ttm_alloc_gart(&(*bo_ptr)->tbo);
    284 	if (r) {
    285 		dev_err(adev->dev, "%p bind failed\n", *bo_ptr);
    286 		goto error_unpin;
    287 	}
    288 
    289 	if (gpu_addr)
    290 		*gpu_addr = amdgpu_bo_gpu_offset(*bo_ptr);
    291 
    292 	if (cpu_addr) {
    293 		r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
    294 		if (r) {
    295 			dev_err(adev->dev, "(%d) kernel bo map failed\n", r);
    296 			goto error_unpin;
    297 		}
    298 	}
    299 
    300 	return 0;
    301 
    302 error_unpin:
    303 	amdgpu_bo_unpin(*bo_ptr);
    304 error_unreserve:
    305 	amdgpu_bo_unreserve(*bo_ptr);
    306 
    307 error_free:
    308 	if (free)
    309 		amdgpu_bo_unref(bo_ptr);
    310 
    311 	return r;
    312 }
    313 
    314 /**
    315  * amdgpu_bo_create_kernel - create BO for kernel use
    316  *
    317  * @adev: amdgpu device object
    318  * @size: size for the new BO
    319  * @align: alignment for the new BO
    320  * @domain: where to place it
    321  * @bo_ptr:  used to initialize BOs in structures
    322  * @gpu_addr: GPU addr of the pinned BO
    323  * @cpu_addr: optional CPU address mapping
    324  *
    325  * Allocates and pins a BO for kernel internal use.
    326  *
    327  * Note: For bo_ptr new BO is only created if bo_ptr points to NULL.
    328  *
    329  * Returns:
    330  * 0 on success, negative error code otherwise.
    331  */
    332 int amdgpu_bo_create_kernel(struct amdgpu_device *adev,
    333 			    unsigned long size, int align,
    334 			    u32 domain, struct amdgpu_bo **bo_ptr,
    335 			    u64 *gpu_addr, void **cpu_addr)
    336 {
    337 	int r;
    338 
    339 	r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr,
    340 				      gpu_addr, cpu_addr);
    341 
    342 	if (r)
    343 		return r;
    344 
    345 	if (*bo_ptr)
    346 		amdgpu_bo_unreserve(*bo_ptr);
    347 
    348 	return 0;
    349 }
    350 
    351 /**
    352  * amdgpu_bo_create_kernel_at - create BO for kernel use at specific location
    353  *
    354  * @adev: amdgpu device object
    355  * @offset: offset of the BO
    356  * @size: size of the BO
    357  * @domain: where to place it
    358  * @bo_ptr:  used to initialize BOs in structures
    359  * @cpu_addr: optional CPU address mapping
    360  *
    361  * Creates a kernel BO at a specific offset in the address space of the domain.
    362  *
    363  * Returns:
    364  * 0 on success, negative error code otherwise.
    365  */
    366 int amdgpu_bo_create_kernel_at(struct amdgpu_device *adev,
    367 			       uint64_t offset, uint64_t size, uint32_t domain,
    368 			       struct amdgpu_bo **bo_ptr, void **cpu_addr)
    369 {
    370 	struct ttm_operation_ctx ctx = { false, false };
    371 	unsigned int i;
    372 	int r;
    373 
    374 	offset &= PAGE_MASK;
    375 	size = ALIGN(size, PAGE_SIZE);
    376 
    377 	r = amdgpu_bo_create_reserved(adev, size, PAGE_SIZE, domain, bo_ptr,
    378 				      NULL, cpu_addr);
    379 	if (r)
    380 		return r;
    381 
    382 	/*
    383 	 * Remove the original mem node and create a new one at the request
    384 	 * position.
    385 	 */
    386 	if (cpu_addr)
    387 		amdgpu_bo_kunmap(*bo_ptr);
    388 
    389 	ttm_bo_mem_put(&(*bo_ptr)->tbo, &(*bo_ptr)->tbo.mem);
    390 
    391 	for (i = 0; i < (*bo_ptr)->placement.num_placement; ++i) {
    392 		(*bo_ptr)->placements[i].fpfn = offset >> PAGE_SHIFT;
    393 		(*bo_ptr)->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
    394 	}
    395 	r = ttm_bo_mem_space(&(*bo_ptr)->tbo, &(*bo_ptr)->placement,
    396 			     &(*bo_ptr)->tbo.mem, &ctx);
    397 	if (r)
    398 		goto error;
    399 
    400 	if (cpu_addr) {
    401 		r = amdgpu_bo_kmap(*bo_ptr, cpu_addr);
    402 		if (r)
    403 			goto error;
    404 	}
    405 
    406 	amdgpu_bo_unreserve(*bo_ptr);
    407 	return 0;
    408 
    409 error:
    410 	amdgpu_bo_unreserve(*bo_ptr);
    411 	amdgpu_bo_unref(bo_ptr);
    412 	return r;
    413 }
    414 
    415 /**
    416  * amdgpu_bo_free_kernel - free BO for kernel use
    417  *
    418  * @bo: amdgpu BO to free
    419  * @gpu_addr: pointer to where the BO's GPU memory space address was stored
    420  * @cpu_addr: pointer to where the BO's CPU memory space address was stored
    421  *
    422  * unmaps and unpin a BO for kernel internal use.
    423  */
    424 void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr,
    425 			   void **cpu_addr)
    426 {
    427 	if (*bo == NULL)
    428 		return;
    429 
    430 	if (likely(amdgpu_bo_reserve(*bo, true) == 0)) {
    431 		if (cpu_addr)
    432 			amdgpu_bo_kunmap(*bo);
    433 
    434 		amdgpu_bo_unpin(*bo);
    435 		amdgpu_bo_unreserve(*bo);
    436 	}
    437 	amdgpu_bo_unref(bo);
    438 
    439 	if (gpu_addr)
    440 		*gpu_addr = 0;
    441 
    442 	if (cpu_addr)
    443 		*cpu_addr = NULL;
    444 }
    445 
    446 /* Validate bo size is bit bigger then the request domain */
    447 static bool amdgpu_bo_validate_size(struct amdgpu_device *adev,
    448 					  unsigned long size, u32 domain)
    449 {
    450 	struct ttm_mem_type_manager *man = NULL;
    451 
    452 	/*
    453 	 * If GTT is part of requested domains the check must succeed to
    454 	 * allow fall back to GTT
    455 	 */
    456 	if (domain & AMDGPU_GEM_DOMAIN_GTT) {
    457 		man = &adev->mman.bdev.man[TTM_PL_TT];
    458 
    459 		if (size < (man->size << PAGE_SHIFT))
    460 			return true;
    461 		else
    462 			goto fail;
    463 	}
    464 
    465 	if (domain & AMDGPU_GEM_DOMAIN_VRAM) {
    466 		man = &adev->mman.bdev.man[TTM_PL_VRAM];
    467 
    468 		if (size < (man->size << PAGE_SHIFT))
    469 			return true;
    470 		else
    471 			goto fail;
    472 	}
    473 
    474 
    475 	/* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */
    476 	return true;
    477 
    478 fail:
    479 	DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size,
    480 		  man->size << PAGE_SHIFT);
    481 	return false;
    482 }
    483 
    484 bool amdgpu_bo_support_uswc(u64 bo_flags)
    485 {
    486 
    487 #ifdef CONFIG_X86_32
    488 	/* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
    489 	 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
    490 	 */
    491 	return false;
    492 #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
    493 	/* Don't try to enable write-combining when it can't work, or things
    494 	 * may be slow
    495 	 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
    496 	 */
    497 
    498 #ifndef CONFIG_COMPILE_TEST
    499 #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
    500 	 thanks to write-combining
    501 #endif
    502 
    503 	if (bo_flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
    504 		DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
    505 			      "better performance thanks to write-combining\n");
    506 	return false;
    507 #else
    508 	/* For architectures that don't support WC memory,
    509 	 * mask out the WC flag from the BO
    510 	 */
    511 	if (!drm_arch_can_wc_memory())
    512 		return false;
    513 
    514 	return true;
    515 #endif
    516 }
    517 
    518 static int amdgpu_bo_do_create(struct amdgpu_device *adev,
    519 			       struct amdgpu_bo_param *bp,
    520 			       struct amdgpu_bo **bo_ptr)
    521 {
    522 	struct ttm_operation_ctx ctx = {
    523 		.interruptible = (bp->type != ttm_bo_type_kernel),
    524 		.no_wait_gpu = bp->no_wait_gpu,
    525 		.resv = bp->resv,
    526 		.flags = bp->type != ttm_bo_type_kernel ?
    527 			TTM_OPT_FLAG_ALLOW_RES_EVICT : 0
    528 	};
    529 	struct amdgpu_bo *bo;
    530 	unsigned long page_align, size = bp->size;
    531 	size_t acc_size;
    532 	int r;
    533 
    534 	/* Note that GDS/GWS/OA allocates 1 page per byte/resource. */
    535 	if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
    536 		/* GWS and OA don't need any alignment. */
    537 		page_align = bp->byte_align;
    538 		size <<= PAGE_SHIFT;
    539 	} else if (bp->domain & AMDGPU_GEM_DOMAIN_GDS) {
    540 		/* Both size and alignment must be a multiple of 4. */
    541 		page_align = ALIGN(bp->byte_align, 4);
    542 		size = ALIGN(size, 4) << PAGE_SHIFT;
    543 	} else {
    544 		/* Memory should be aligned at least to a page size. */
    545 		page_align = ALIGN(bp->byte_align, PAGE_SIZE) >> PAGE_SHIFT;
    546 		size = ALIGN(size, PAGE_SIZE);
    547 	}
    548 
    549 	if (!amdgpu_bo_validate_size(adev, size, bp->domain))
    550 		return -ENOMEM;
    551 
    552 	*bo_ptr = NULL;
    553 
    554 	acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size,
    555 				       sizeof(struct amdgpu_bo));
    556 
    557 	bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL);
    558 	if (bo == NULL)
    559 		return -ENOMEM;
    560 	drm_gem_private_object_init(adev->ddev, &bo->tbo.base, size);
    561 	INIT_LIST_HEAD(&bo->shadow_list);
    562 	bo->vm_bo = NULL;
    563 	bo->preferred_domains = bp->preferred_domain ? bp->preferred_domain :
    564 		bp->domain;
    565 	bo->allowed_domains = bo->preferred_domains;
    566 	if (bp->type != ttm_bo_type_kernel &&
    567 	    bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
    568 		bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
    569 
    570 	bo->flags = bp->flags;
    571 
    572 	if (!amdgpu_bo_support_uswc(bo->flags))
    573 		bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC;
    574 
    575 	bo->tbo.bdev = &adev->mman.bdev;
    576 	if (bp->domain & (AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA |
    577 			  AMDGPU_GEM_DOMAIN_GDS))
    578 		amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_CPU);
    579 	else
    580 		amdgpu_bo_placement_from_domain(bo, bp->domain);
    581 	if (bp->type == ttm_bo_type_kernel)
    582 		bo->tbo.priority = 1;
    583 
    584 	r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, bp->type,
    585 				 &bo->placement, page_align, &ctx, acc_size,
    586 				 NULL, bp->resv, &amdgpu_bo_destroy);
    587 	if (unlikely(r != 0))
    588 		return r;
    589 
    590 	if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
    591 	    bo->tbo.mem.mem_type == TTM_PL_VRAM &&
    592 	    bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT)
    593 		amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved,
    594 					     ctx.bytes_moved);
    595 	else
    596 		amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0);
    597 
    598 	if (bp->flags & AMDGPU_GEM_CREATE_VRAM_CLEARED &&
    599 	    bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) {
    600 		struct dma_fence *fence;
    601 
    602 		r = amdgpu_fill_buffer(bo, 0, bo->tbo.base.resv, &fence);
    603 		if (unlikely(r))
    604 			goto fail_unreserve;
    605 
    606 		amdgpu_bo_fence(bo, fence, false);
    607 		dma_fence_put(bo->tbo.moving);
    608 		bo->tbo.moving = dma_fence_get(fence);
    609 		dma_fence_put(fence);
    610 	}
    611 	if (!bp->resv)
    612 		amdgpu_bo_unreserve(bo);
    613 	*bo_ptr = bo;
    614 
    615 	trace_amdgpu_bo_create(bo);
    616 
    617 	/* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */
    618 	if (bp->type == ttm_bo_type_device)
    619 		bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
    620 
    621 	return 0;
    622 
    623 fail_unreserve:
    624 	if (!bp->resv)
    625 		dma_resv_unlock(bo->tbo.base.resv);
    626 	amdgpu_bo_unref(&bo);
    627 	return r;
    628 }
    629 
    630 static int amdgpu_bo_create_shadow(struct amdgpu_device *adev,
    631 				   unsigned long size,
    632 				   struct amdgpu_bo *bo)
    633 {
    634 	struct amdgpu_bo_param bp;
    635 	int r;
    636 
    637 	if (bo->shadow)
    638 		return 0;
    639 
    640 	memset(&bp, 0, sizeof(bp));
    641 	bp.size = size;
    642 	bp.domain = AMDGPU_GEM_DOMAIN_GTT;
    643 	bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC |
    644 		AMDGPU_GEM_CREATE_SHADOW;
    645 	bp.type = ttm_bo_type_kernel;
    646 	bp.resv = bo->tbo.base.resv;
    647 
    648 	r = amdgpu_bo_do_create(adev, &bp, &bo->shadow);
    649 	if (!r) {
    650 		bo->shadow->parent = amdgpu_bo_ref(bo);
    651 		mutex_lock(&adev->shadow_list_lock);
    652 		list_add_tail(&bo->shadow->shadow_list, &adev->shadow_list);
    653 		mutex_unlock(&adev->shadow_list_lock);
    654 	}
    655 
    656 	return r;
    657 }
    658 
    659 /**
    660  * amdgpu_bo_create - create an &amdgpu_bo buffer object
    661  * @adev: amdgpu device object
    662  * @bp: parameters to be used for the buffer object
    663  * @bo_ptr: pointer to the buffer object pointer
    664  *
    665  * Creates an &amdgpu_bo buffer object; and if requested, also creates a
    666  * shadow object.
    667  * Shadow object is used to backup the original buffer object, and is always
    668  * in GTT.
    669  *
    670  * Returns:
    671  * 0 for success or a negative error code on failure.
    672  */
    673 int amdgpu_bo_create(struct amdgpu_device *adev,
    674 		     struct amdgpu_bo_param *bp,
    675 		     struct amdgpu_bo **bo_ptr)
    676 {
    677 	u64 flags = bp->flags;
    678 	int r;
    679 
    680 	bp->flags = bp->flags & ~AMDGPU_GEM_CREATE_SHADOW;
    681 	r = amdgpu_bo_do_create(adev, bp, bo_ptr);
    682 	if (r)
    683 		return r;
    684 
    685 	if ((flags & AMDGPU_GEM_CREATE_SHADOW) && !(adev->flags & AMD_IS_APU)) {
    686 		if (!bp->resv)
    687 			WARN_ON(dma_resv_lock((*bo_ptr)->tbo.base.resv,
    688 							NULL));
    689 
    690 		r = amdgpu_bo_create_shadow(adev, bp->size, *bo_ptr);
    691 
    692 		if (!bp->resv)
    693 			dma_resv_unlock((*bo_ptr)->tbo.base.resv);
    694 
    695 		if (r)
    696 			amdgpu_bo_unref(bo_ptr);
    697 	}
    698 
    699 	return r;
    700 }
    701 
    702 /**
    703  * amdgpu_bo_validate - validate an &amdgpu_bo buffer object
    704  * @bo: pointer to the buffer object
    705  *
    706  * Sets placement according to domain; and changes placement and caching
    707  * policy of the buffer object according to the placement.
    708  * This is used for validating shadow bos.  It calls ttm_bo_validate() to
    709  * make sure the buffer is resident where it needs to be.
    710  *
    711  * Returns:
    712  * 0 for success or a negative error code on failure.
    713  */
    714 int amdgpu_bo_validate(struct amdgpu_bo *bo)
    715 {
    716 	struct ttm_operation_ctx ctx = { false, false };
    717 	uint32_t domain;
    718 	int r;
    719 
    720 	if (bo->pin_count)
    721 		return 0;
    722 
    723 	domain = bo->preferred_domains;
    724 
    725 retry:
    726 	amdgpu_bo_placement_from_domain(bo, domain);
    727 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
    728 	if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) {
    729 		domain = bo->allowed_domains;
    730 		goto retry;
    731 	}
    732 
    733 	return r;
    734 }
    735 
    736 /**
    737  * amdgpu_bo_restore_shadow - restore an &amdgpu_bo shadow
    738  *
    739  * @shadow: &amdgpu_bo shadow to be restored
    740  * @fence: dma_fence associated with the operation
    741  *
    742  * Copies a buffer object's shadow content back to the object.
    743  * This is used for recovering a buffer from its shadow in case of a gpu
    744  * reset where vram context may be lost.
    745  *
    746  * Returns:
    747  * 0 for success or a negative error code on failure.
    748  */
    749 int amdgpu_bo_restore_shadow(struct amdgpu_bo *shadow, struct dma_fence **fence)
    750 
    751 {
    752 	struct amdgpu_device *adev = amdgpu_ttm_adev(shadow->tbo.bdev);
    753 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
    754 	uint64_t shadow_addr, parent_addr;
    755 
    756 	shadow_addr = amdgpu_bo_gpu_offset(shadow);
    757 	parent_addr = amdgpu_bo_gpu_offset(shadow->parent);
    758 
    759 	return amdgpu_copy_buffer(ring, shadow_addr, parent_addr,
    760 				  amdgpu_bo_size(shadow), NULL, fence,
    761 				  true, false);
    762 }
    763 
    764 /**
    765  * amdgpu_bo_kmap - map an &amdgpu_bo buffer object
    766  * @bo: &amdgpu_bo buffer object to be mapped
    767  * @ptr: kernel virtual address to be returned
    768  *
    769  * Calls ttm_bo_kmap() to set up the kernel virtual mapping; calls
    770  * amdgpu_bo_kptr() to get the kernel virtual address.
    771  *
    772  * Returns:
    773  * 0 for success or a negative error code on failure.
    774  */
    775 int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr)
    776 {
    777 	void *kptr;
    778 	long r;
    779 
    780 	if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
    781 		return -EPERM;
    782 
    783 	kptr = amdgpu_bo_kptr(bo);
    784 	if (kptr) {
    785 		if (ptr)
    786 			*ptr = kptr;
    787 		return 0;
    788 	}
    789 
    790 	r = dma_resv_wait_timeout_rcu(bo->tbo.base.resv, false, false,
    791 						MAX_SCHEDULE_TIMEOUT);
    792 	if (r < 0)
    793 		return r;
    794 
    795 	r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
    796 	if (r)
    797 		return r;
    798 
    799 	if (ptr)
    800 		*ptr = amdgpu_bo_kptr(bo);
    801 
    802 	return 0;
    803 }
    804 
    805 /**
    806  * amdgpu_bo_kptr - returns a kernel virtual address of the buffer object
    807  * @bo: &amdgpu_bo buffer object
    808  *
    809  * Calls ttm_kmap_obj_virtual() to get the kernel virtual address
    810  *
    811  * Returns:
    812  * the virtual address of a buffer object area.
    813  */
    814 void *amdgpu_bo_kptr(struct amdgpu_bo *bo)
    815 {
    816 	bool is_iomem;
    817 
    818 	return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
    819 }
    820 
    821 /**
    822  * amdgpu_bo_kunmap - unmap an &amdgpu_bo buffer object
    823  * @bo: &amdgpu_bo buffer object to be unmapped
    824  *
    825  * Unmaps a kernel map set up by amdgpu_bo_kmap().
    826  */
    827 void amdgpu_bo_kunmap(struct amdgpu_bo *bo)
    828 {
    829 	if (bo->kmap.bo)
    830 		ttm_bo_kunmap(&bo->kmap);
    831 }
    832 
    833 /**
    834  * amdgpu_bo_ref - reference an &amdgpu_bo buffer object
    835  * @bo: &amdgpu_bo buffer object
    836  *
    837  * References the contained &ttm_buffer_object.
    838  *
    839  * Returns:
    840  * a refcounted pointer to the &amdgpu_bo buffer object.
    841  */
    842 struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo)
    843 {
    844 	if (bo == NULL)
    845 		return NULL;
    846 
    847 	ttm_bo_get(&bo->tbo);
    848 	return bo;
    849 }
    850 
    851 /**
    852  * amdgpu_bo_unref - unreference an &amdgpu_bo buffer object
    853  * @bo: &amdgpu_bo buffer object
    854  *
    855  * Unreferences the contained &ttm_buffer_object and clear the pointer
    856  */
    857 void amdgpu_bo_unref(struct amdgpu_bo **bo)
    858 {
    859 	struct ttm_buffer_object *tbo;
    860 
    861 	if ((*bo) == NULL)
    862 		return;
    863 
    864 	tbo = &((*bo)->tbo);
    865 	ttm_bo_put(tbo);
    866 	*bo = NULL;
    867 }
    868 
    869 /**
    870  * amdgpu_bo_pin_restricted - pin an &amdgpu_bo buffer object
    871  * @bo: &amdgpu_bo buffer object to be pinned
    872  * @domain: domain to be pinned to
    873  * @min_offset: the start of requested address range
    874  * @max_offset: the end of requested address range
    875  *
    876  * Pins the buffer object according to requested domain and address range. If
    877  * the memory is unbound gart memory, binds the pages into gart table. Adjusts
    878  * pin_count and pin_size accordingly.
    879  *
    880  * Pinning means to lock pages in memory along with keeping them at a fixed
    881  * offset. It is required when a buffer can not be moved, for example, when
    882  * a display buffer is being scanned out.
    883  *
    884  * Compared with amdgpu_bo_pin(), this function gives more flexibility on
    885  * where to pin a buffer if there are specific restrictions on where a buffer
    886  * must be located.
    887  *
    888  * Returns:
    889  * 0 for success or a negative error code on failure.
    890  */
    891 int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain,
    892 			     u64 min_offset, u64 max_offset)
    893 {
    894 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
    895 	struct ttm_operation_ctx ctx = { false, false };
    896 	int r, i;
    897 
    898 	if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
    899 		return -EPERM;
    900 
    901 	if (WARN_ON_ONCE(min_offset > max_offset))
    902 		return -EINVAL;
    903 
    904 	/* A shared bo cannot be migrated to VRAM */
    905 	if (bo->prime_shared_count) {
    906 		if (domain & AMDGPU_GEM_DOMAIN_GTT)
    907 			domain = AMDGPU_GEM_DOMAIN_GTT;
    908 		else
    909 			return -EINVAL;
    910 	}
    911 
    912 	/* This assumes only APU display buffers are pinned with (VRAM|GTT).
    913 	 * See function amdgpu_display_supported_domains()
    914 	 */
    915 	domain = amdgpu_bo_get_preferred_pin_domain(adev, domain);
    916 
    917 	if (bo->pin_count) {
    918 		uint32_t mem_type = bo->tbo.mem.mem_type;
    919 
    920 		if (!(domain & amdgpu_mem_type_to_domain(mem_type)))
    921 			return -EINVAL;
    922 
    923 		bo->pin_count++;
    924 
    925 		if (max_offset != 0) {
    926 			u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset;
    927 			WARN_ON_ONCE(max_offset <
    928 				     (amdgpu_bo_gpu_offset(bo) - domain_start));
    929 		}
    930 
    931 		return 0;
    932 	}
    933 
    934 	bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
    935 	/* force to pin into visible video ram */
    936 	if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS))
    937 		bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
    938 	amdgpu_bo_placement_from_domain(bo, domain);
    939 	for (i = 0; i < bo->placement.num_placement; i++) {
    940 		unsigned fpfn, lpfn;
    941 
    942 		fpfn = min_offset >> PAGE_SHIFT;
    943 		lpfn = max_offset >> PAGE_SHIFT;
    944 
    945 		if (fpfn > bo->placements[i].fpfn)
    946 			bo->placements[i].fpfn = fpfn;
    947 		if (!bo->placements[i].lpfn ||
    948 		    (lpfn && lpfn < bo->placements[i].lpfn))
    949 			bo->placements[i].lpfn = lpfn;
    950 		bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
    951 	}
    952 
    953 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
    954 	if (unlikely(r)) {
    955 		dev_err(adev->dev, "%p pin failed\n", bo);
    956 		goto error;
    957 	}
    958 
    959 	bo->pin_count = 1;
    960 
    961 	domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
    962 	if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
    963 		atomic64_add(amdgpu_bo_size(bo), &adev->vram_pin_size);
    964 		atomic64_add(amdgpu_vram_mgr_bo_visible_size(bo),
    965 			     &adev->visible_pin_size);
    966 	} else if (domain == AMDGPU_GEM_DOMAIN_GTT) {
    967 		atomic64_add(amdgpu_bo_size(bo), &adev->gart_pin_size);
    968 	}
    969 
    970 error:
    971 	return r;
    972 }
    973 
    974 /**
    975  * amdgpu_bo_pin - pin an &amdgpu_bo buffer object
    976  * @bo: &amdgpu_bo buffer object to be pinned
    977  * @domain: domain to be pinned to
    978  *
    979  * A simple wrapper to amdgpu_bo_pin_restricted().
    980  * Provides a simpler API for buffers that do not have any strict restrictions
    981  * on where a buffer must be located.
    982  *
    983  * Returns:
    984  * 0 for success or a negative error code on failure.
    985  */
    986 int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain)
    987 {
    988 	return amdgpu_bo_pin_restricted(bo, domain, 0, 0);
    989 }
    990 
    991 /**
    992  * amdgpu_bo_unpin - unpin an &amdgpu_bo buffer object
    993  * @bo: &amdgpu_bo buffer object to be unpinned
    994  *
    995  * Decreases the pin_count, and clears the flags if pin_count reaches 0.
    996  * Changes placement and pin size accordingly.
    997  *
    998  * Returns:
    999  * 0 for success or a negative error code on failure.
   1000  */
   1001 int amdgpu_bo_unpin(struct amdgpu_bo *bo)
   1002 {
   1003 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
   1004 	struct ttm_operation_ctx ctx = { false, false };
   1005 	int r, i;
   1006 
   1007 	if (WARN_ON_ONCE(!bo->pin_count)) {
   1008 		dev_warn(adev->dev, "%p unpin not necessary\n", bo);
   1009 		return 0;
   1010 	}
   1011 	bo->pin_count--;
   1012 	if (bo->pin_count)
   1013 		return 0;
   1014 
   1015 	amdgpu_bo_subtract_pin_size(bo);
   1016 
   1017 	for (i = 0; i < bo->placement.num_placement; i++) {
   1018 		bo->placements[i].lpfn = 0;
   1019 		bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
   1020 	}
   1021 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
   1022 	if (unlikely(r))
   1023 		dev_err(adev->dev, "%p validate failed for unpin\n", bo);
   1024 
   1025 	return r;
   1026 }
   1027 
   1028 /**
   1029  * amdgpu_bo_evict_vram - evict VRAM buffers
   1030  * @adev: amdgpu device object
   1031  *
   1032  * Evicts all VRAM buffers on the lru list of the memory type.
   1033  * Mainly used for evicting vram at suspend time.
   1034  *
   1035  * Returns:
   1036  * 0 for success or a negative error code on failure.
   1037  */
   1038 int amdgpu_bo_evict_vram(struct amdgpu_device *adev)
   1039 {
   1040 	/* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
   1041 #ifndef CONFIG_HIBERNATION
   1042 	if (adev->flags & AMD_IS_APU) {
   1043 		/* Useless to evict on IGP chips */
   1044 		return 0;
   1045 	}
   1046 #endif
   1047 	return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM);
   1048 }
   1049 
   1050 static const char *amdgpu_vram_names[] = {
   1051 	"UNKNOWN",
   1052 	"GDDR1",
   1053 	"DDR2",
   1054 	"GDDR3",
   1055 	"GDDR4",
   1056 	"GDDR5",
   1057 	"HBM",
   1058 	"DDR3",
   1059 	"DDR4",
   1060 	"GDDR6",
   1061 };
   1062 
   1063 /**
   1064  * amdgpu_bo_init - initialize memory manager
   1065  * @adev: amdgpu device object
   1066  *
   1067  * Calls amdgpu_ttm_init() to initialize amdgpu memory manager.
   1068  *
   1069  * Returns:
   1070  * 0 for success or a negative error code on failure.
   1071  */
   1072 int amdgpu_bo_init(struct amdgpu_device *adev)
   1073 {
   1074 	/* reserve PAT memory space to WC for VRAM */
   1075 	arch_io_reserve_memtype_wc(adev->gmc.aper_base,
   1076 				   adev->gmc.aper_size);
   1077 
   1078 	/* Add an MTRR for the VRAM */
   1079 	adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base,
   1080 					      adev->gmc.aper_size);
   1081 	DRM_INFO("Detected VRAM RAM=%"PRIu64"M, BAR=%lluM\n",
   1082 		 adev->gmc.mc_vram_size >> 20,
   1083 		 (unsigned long long)adev->gmc.aper_size >> 20);
   1084 	DRM_INFO("RAM width %dbits %s\n",
   1085 		 adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]);
   1086 	return amdgpu_ttm_init(adev);
   1087 }
   1088 
   1089 /**
   1090  * amdgpu_bo_late_init - late init
   1091  * @adev: amdgpu device object
   1092  *
   1093  * Calls amdgpu_ttm_late_init() to free resources used earlier during
   1094  * initialization.
   1095  *
   1096  * Returns:
   1097  * 0 for success or a negative error code on failure.
   1098  */
   1099 int amdgpu_bo_late_init(struct amdgpu_device *adev)
   1100 {
   1101 	amdgpu_ttm_late_init(adev);
   1102 
   1103 	return 0;
   1104 }
   1105 
   1106 /**
   1107  * amdgpu_bo_fini - tear down memory manager
   1108  * @adev: amdgpu device object
   1109  *
   1110  * Reverses amdgpu_bo_init() to tear down memory manager.
   1111  */
   1112 void amdgpu_bo_fini(struct amdgpu_device *adev)
   1113 {
   1114 	amdgpu_ttm_fini(adev);
   1115 	arch_phys_wc_del(adev->gmc.vram_mtrr);
   1116 	arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
   1117 }
   1118 
   1119 #ifndef __NetBSD__		/* XXX unused? */
   1120 /**
   1121  * amdgpu_bo_fbdev_mmap - mmap fbdev memory
   1122  * @bo: &amdgpu_bo buffer object
   1123  * @vma: vma as input from the fbdev mmap method
   1124  *
   1125  * Calls ttm_fbdev_mmap() to mmap fbdev memory if it is backed by a bo.
   1126  *
   1127  * Returns:
   1128  * 0 for success or a negative error code on failure.
   1129  */
   1130 int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo,
   1131 			     struct vm_area_struct *vma)
   1132 {
   1133 	if (vma->vm_pgoff != 0)
   1134 		return -EACCES;
   1135 
   1136 	return ttm_bo_mmap_obj(vma, &bo->tbo);
   1137 }
   1138 #endif
   1139 
   1140 /**
   1141  * amdgpu_bo_set_tiling_flags - set tiling flags
   1142  * @bo: &amdgpu_bo buffer object
   1143  * @tiling_flags: new flags
   1144  *
   1145  * Sets buffer object's tiling flags with the new one. Used by GEM ioctl or
   1146  * kernel driver to set the tiling flags on a buffer.
   1147  *
   1148  * Returns:
   1149  * 0 for success or a negative error code on failure.
   1150  */
   1151 int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags)
   1152 {
   1153 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
   1154 
   1155 	if (adev->family <= AMDGPU_FAMILY_CZ &&
   1156 	    AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6)
   1157 		return -EINVAL;
   1158 
   1159 	bo->tiling_flags = tiling_flags;
   1160 	return 0;
   1161 }
   1162 
   1163 /**
   1164  * amdgpu_bo_get_tiling_flags - get tiling flags
   1165  * @bo: &amdgpu_bo buffer object
   1166  * @tiling_flags: returned flags
   1167  *
   1168  * Gets buffer object's tiling flags. Used by GEM ioctl or kernel driver to
   1169  * set the tiling flags on a buffer.
   1170  */
   1171 void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags)
   1172 {
   1173 	dma_resv_assert_held(bo->tbo.base.resv);
   1174 
   1175 	if (tiling_flags)
   1176 		*tiling_flags = bo->tiling_flags;
   1177 }
   1178 
   1179 /**
   1180  * amdgpu_bo_set_metadata - set metadata
   1181  * @bo: &amdgpu_bo buffer object
   1182  * @metadata: new metadata
   1183  * @metadata_size: size of the new metadata
   1184  * @flags: flags of the new metadata
   1185  *
   1186  * Sets buffer object's metadata, its size and flags.
   1187  * Used via GEM ioctl.
   1188  *
   1189  * Returns:
   1190  * 0 for success or a negative error code on failure.
   1191  */
   1192 int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata,
   1193 			    uint32_t metadata_size, uint64_t flags)
   1194 {
   1195 	void *buffer;
   1196 
   1197 	if (!metadata_size) {
   1198 		if (bo->metadata_size) {
   1199 			kfree(bo->metadata);
   1200 			bo->metadata = NULL;
   1201 			bo->metadata_size = 0;
   1202 		}
   1203 		return 0;
   1204 	}
   1205 
   1206 	if (metadata == NULL)
   1207 		return -EINVAL;
   1208 
   1209 	buffer = kmemdup(metadata, metadata_size, GFP_KERNEL);
   1210 	if (buffer == NULL)
   1211 		return -ENOMEM;
   1212 
   1213 	kfree(bo->metadata);
   1214 	bo->metadata_flags = flags;
   1215 	bo->metadata = buffer;
   1216 	bo->metadata_size = metadata_size;
   1217 
   1218 	return 0;
   1219 }
   1220 
   1221 /**
   1222  * amdgpu_bo_get_metadata - get metadata
   1223  * @bo: &amdgpu_bo buffer object
   1224  * @buffer: returned metadata
   1225  * @buffer_size: size of the buffer
   1226  * @metadata_size: size of the returned metadata
   1227  * @flags: flags of the returned metadata
   1228  *
   1229  * Gets buffer object's metadata, its size and flags. buffer_size shall not be
   1230  * less than metadata_size.
   1231  * Used via GEM ioctl.
   1232  *
   1233  * Returns:
   1234  * 0 for success or a negative error code on failure.
   1235  */
   1236 int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer,
   1237 			   size_t buffer_size, uint32_t *metadata_size,
   1238 			   uint64_t *flags)
   1239 {
   1240 	if (!buffer && !metadata_size)
   1241 		return -EINVAL;
   1242 
   1243 	if (buffer) {
   1244 		if (buffer_size < bo->metadata_size)
   1245 			return -EINVAL;
   1246 
   1247 		if (bo->metadata_size)
   1248 			memcpy(buffer, bo->metadata, bo->metadata_size);
   1249 	}
   1250 
   1251 	if (metadata_size)
   1252 		*metadata_size = bo->metadata_size;
   1253 	if (flags)
   1254 		*flags = bo->metadata_flags;
   1255 
   1256 	return 0;
   1257 }
   1258 
   1259 /**
   1260  * amdgpu_bo_move_notify - notification about a memory move
   1261  * @bo: pointer to a buffer object
   1262  * @evict: if this move is evicting the buffer from the graphics address space
   1263  * @new_mem: new information of the bufer object
   1264  *
   1265  * Marks the corresponding &amdgpu_bo buffer object as invalid, also performs
   1266  * bookkeeping.
   1267  * TTM driver callback which is called when ttm moves a buffer.
   1268  */
   1269 void amdgpu_bo_move_notify(struct ttm_buffer_object *bo,
   1270 			   bool evict,
   1271 			   struct ttm_mem_reg *new_mem)
   1272 {
   1273 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
   1274 	struct amdgpu_bo *abo;
   1275 	struct ttm_mem_reg *old_mem = &bo->mem;
   1276 
   1277 	if (!amdgpu_bo_is_amdgpu_bo(bo))
   1278 		return;
   1279 
   1280 	abo = ttm_to_amdgpu_bo(bo);
   1281 	amdgpu_vm_bo_invalidate(adev, abo, evict);
   1282 
   1283 	amdgpu_bo_kunmap(abo);
   1284 
   1285 	/* remember the eviction */
   1286 	if (evict)
   1287 		atomic64_inc(&adev->num_evictions);
   1288 
   1289 	/* update statistics */
   1290 	if (!new_mem)
   1291 		return;
   1292 
   1293 	/* move_notify is called before move happens */
   1294 	trace_amdgpu_bo_move(abo, new_mem->mem_type, old_mem->mem_type);
   1295 }
   1296 
   1297 /**
   1298  * amdgpu_bo_move_notify - notification about a BO being released
   1299  * @bo: pointer to a buffer object
   1300  *
   1301  * Wipes VRAM buffers whose contents should not be leaked before the
   1302  * memory is released.
   1303  */
   1304 void amdgpu_bo_release_notify(struct ttm_buffer_object *bo)
   1305 {
   1306 	struct dma_fence *fence = NULL;
   1307 	struct amdgpu_bo *abo;
   1308 	int r;
   1309 
   1310 	if (!amdgpu_bo_is_amdgpu_bo(bo))
   1311 		return;
   1312 
   1313 	abo = ttm_to_amdgpu_bo(bo);
   1314 
   1315 	if (abo->kfd_bo)
   1316 		amdgpu_amdkfd_unreserve_memory_limit(abo);
   1317 
   1318 	if (bo->mem.mem_type != TTM_PL_VRAM || !bo->mem.mm_node ||
   1319 	    !(abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE))
   1320 		return;
   1321 
   1322 	dma_resv_lock(bo->base.resv, NULL);
   1323 
   1324 	r = amdgpu_fill_buffer(abo, AMDGPU_POISON, bo->base.resv, &fence);
   1325 	if (!WARN_ON(r)) {
   1326 		amdgpu_bo_fence(abo, fence, false);
   1327 		dma_fence_put(fence);
   1328 	}
   1329 
   1330 	dma_resv_unlock(bo->base.resv);
   1331 }
   1332 
   1333 /**
   1334  * amdgpu_bo_fault_reserve_notify - notification about a memory fault
   1335  * @bo: pointer to a buffer object
   1336  *
   1337  * Notifies the driver we are taking a fault on this BO and have reserved it,
   1338  * also performs bookkeeping.
   1339  * TTM driver callback for dealing with vm faults.
   1340  *
   1341  * Returns:
   1342  * 0 for success or a negative error code on failure.
   1343  */
   1344 int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
   1345 {
   1346 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
   1347 	struct ttm_operation_ctx ctx = { false, false };
   1348 	struct amdgpu_bo *abo;
   1349 	unsigned long offset, size;
   1350 	int r;
   1351 
   1352 	if (!amdgpu_bo_is_amdgpu_bo(bo))
   1353 		return 0;
   1354 
   1355 	abo = ttm_to_amdgpu_bo(bo);
   1356 
   1357 	/* Remember that this BO was accessed by the CPU */
   1358 	abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
   1359 
   1360 	if (bo->mem.mem_type != TTM_PL_VRAM)
   1361 		return 0;
   1362 
   1363 	size = bo->mem.num_pages << PAGE_SHIFT;
   1364 	offset = bo->mem.start << PAGE_SHIFT;
   1365 	if ((offset + size) <= adev->gmc.visible_vram_size)
   1366 		return 0;
   1367 
   1368 	/* Can't move a pinned BO to visible VRAM */
   1369 	if (abo->pin_count > 0)
   1370 		return -EINVAL;
   1371 
   1372 	/* hurrah the memory is not visible ! */
   1373 	atomic64_inc(&adev->num_vram_cpu_page_faults);
   1374 	amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
   1375 					AMDGPU_GEM_DOMAIN_GTT);
   1376 
   1377 	/* Avoid costly evictions; only set GTT as a busy placement */
   1378 	abo->placement.num_busy_placement = 1;
   1379 	abo->placement.busy_placement = &abo->placements[1];
   1380 
   1381 	r = ttm_bo_validate(bo, &abo->placement, &ctx);
   1382 	if (unlikely(r != 0))
   1383 		return r;
   1384 
   1385 	offset = bo->mem.start << PAGE_SHIFT;
   1386 	/* this should never happen */
   1387 	if (bo->mem.mem_type == TTM_PL_VRAM &&
   1388 	    (offset + size) > adev->gmc.visible_vram_size)
   1389 		return -EINVAL;
   1390 
   1391 	return 0;
   1392 }
   1393 
   1394 /**
   1395  * amdgpu_bo_fence - add fence to buffer object
   1396  *
   1397  * @bo: buffer object in question
   1398  * @fence: fence to add
   1399  * @shared: true if fence should be added shared
   1400  *
   1401  */
   1402 void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence,
   1403 		     bool shared)
   1404 {
   1405 	struct dma_resv *resv = bo->tbo.base.resv;
   1406 
   1407 	if (shared)
   1408 		dma_resv_add_shared_fence(resv, fence);
   1409 	else
   1410 		dma_resv_add_excl_fence(resv, fence);
   1411 }
   1412 
   1413 /**
   1414  * amdgpu_sync_wait_resv - Wait for BO reservation fences
   1415  *
   1416  * @bo: buffer object
   1417  * @owner: fence owner
   1418  * @intr: Whether the wait is interruptible
   1419  *
   1420  * Returns:
   1421  * 0 on success, errno otherwise.
   1422  */
   1423 int amdgpu_bo_sync_wait(struct amdgpu_bo *bo, void *owner, bool intr)
   1424 {
   1425 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
   1426 	struct amdgpu_sync sync;
   1427 	int r;
   1428 
   1429 	amdgpu_sync_create(&sync);
   1430 	amdgpu_sync_resv(adev, &sync, bo->tbo.base.resv, owner, false);
   1431 	r = amdgpu_sync_wait(&sync, intr);
   1432 	amdgpu_sync_free(&sync);
   1433 
   1434 	return r;
   1435 }
   1436 
   1437 /**
   1438  * amdgpu_bo_gpu_offset - return GPU offset of bo
   1439  * @bo:	amdgpu object for which we query the offset
   1440  *
   1441  * Note: object should either be pinned or reserved when calling this
   1442  * function, it might be useful to add check for this for debugging.
   1443  *
   1444  * Returns:
   1445  * current GPU offset of the object.
   1446  */
   1447 u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo)
   1448 {
   1449 	WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM);
   1450 	WARN_ON_ONCE(!dma_resv_is_locked(bo->tbo.base.resv) &&
   1451 		     !bo->pin_count && bo->tbo.type != ttm_bo_type_kernel);
   1452 	WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET);
   1453 	WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM &&
   1454 		     !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS));
   1455 
   1456 	return amdgpu_gmc_sign_extend(bo->tbo.offset);
   1457 }
   1458 
   1459 /**
   1460  * amdgpu_bo_get_preferred_pin_domain - get preferred domain for scanout
   1461  * @adev: amdgpu device object
   1462  * @domain: allowed :ref:`memory domains <amdgpu_memory_domains>`
   1463  *
   1464  * Returns:
   1465  * Which of the allowed domains is preferred for pinning the BO for scanout.
   1466  */
   1467 uint32_t amdgpu_bo_get_preferred_pin_domain(struct amdgpu_device *adev,
   1468 					    uint32_t domain)
   1469 {
   1470 	if (domain == (AMDGPU_GEM_DOMAIN_VRAM | AMDGPU_GEM_DOMAIN_GTT)) {
   1471 		domain = AMDGPU_GEM_DOMAIN_VRAM;
   1472 		if (adev->gmc.real_vram_size <= AMDGPU_SG_THRESHOLD)
   1473 			domain = AMDGPU_GEM_DOMAIN_GTT;
   1474 	}
   1475 	return domain;
   1476 }
   1477