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