1 /* $NetBSD: amdgpu_csa.c,v 1.3 2021/12/19 10:59:01 riastradh Exp $ */ 2 3 /* 4 * Copyright 2016 Advanced Micro Devices, Inc. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 24 * * Author: Monk.liu@amd.com 25 */ 26 27 #include <sys/cdefs.h> 28 __KERNEL_RCSID(0, "$NetBSD: amdgpu_csa.c,v 1.3 2021/12/19 10:59:01 riastradh Exp $"); 29 30 #include "amdgpu.h" 31 32 uint64_t amdgpu_csa_vaddr(struct amdgpu_device *adev) 33 { 34 uint64_t addr = adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT; 35 36 addr -= AMDGPU_VA_RESERVED_SIZE; 37 addr = amdgpu_gmc_sign_extend(addr); 38 39 return addr; 40 } 41 42 int amdgpu_allocate_static_csa(struct amdgpu_device *adev, struct amdgpu_bo **bo, 43 u32 domain, uint32_t size) 44 { 45 int r __unused; 46 void *ptr; 47 48 r = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE, 49 domain, bo, 50 NULL, &ptr); 51 if (!*bo) 52 return -ENOMEM; 53 54 memset(ptr, 0, size); 55 adev->virt.csa_cpu_addr = ptr; 56 return 0; 57 } 58 59 void amdgpu_free_static_csa(struct amdgpu_bo **bo) 60 { 61 amdgpu_bo_free_kernel(bo, NULL, NULL); 62 } 63 64 /* 65 * amdgpu_map_static_csa should be called during amdgpu_vm_init 66 * it maps virtual address amdgpu_csa_vaddr() to this VM, and each command 67 * submission of GFX should use this virtual address within META_DATA init 68 * package to support SRIOV gfx preemption. 69 */ 70 int amdgpu_map_static_csa(struct amdgpu_device *adev, struct amdgpu_vm *vm, 71 struct amdgpu_bo *bo, struct amdgpu_bo_va **bo_va, 72 uint64_t csa_addr, uint32_t size) 73 { 74 struct ww_acquire_ctx ticket; 75 struct list_head list; 76 struct amdgpu_bo_list_entry pd; 77 struct ttm_validate_buffer csa_tv; 78 int r; 79 80 INIT_LIST_HEAD(&list); 81 INIT_LIST_HEAD(&csa_tv.head); 82 csa_tv.bo = &bo->tbo; 83 csa_tv.num_shared = 1; 84 85 list_add(&csa_tv.head, &list); 86 amdgpu_vm_get_pd_bo(vm, &list, &pd); 87 88 r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL); 89 if (r) { 90 DRM_ERROR("failed to reserve CSA,PD BOs: err=%d\n", r); 91 return r; 92 } 93 94 *bo_va = amdgpu_vm_bo_add(adev, vm, bo); 95 if (!*bo_va) { 96 ttm_eu_backoff_reservation(&ticket, &list); 97 DRM_ERROR("failed to create bo_va for static CSA\n"); 98 return -ENOMEM; 99 } 100 101 r = amdgpu_vm_bo_map(adev, *bo_va, csa_addr, 0, size, 102 AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE | 103 AMDGPU_PTE_EXECUTABLE); 104 105 if (r) { 106 DRM_ERROR("failed to do bo_map on static CSA, err=%d\n", r); 107 amdgpu_vm_bo_rmv(adev, *bo_va); 108 ttm_eu_backoff_reservation(&ticket, &list); 109 return r; 110 } 111 112 ttm_eu_backoff_reservation(&ticket, &list); 113 return 0; 114 } 115