| 1 | |
| 2 | |
| 3 | * Copyright 2023 Advanced Micro Devices, Inc.
|
| 4 | *
|
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a
|
| 6 | * copy of this software and associated documentation files (the "Software"),
|
| 7 | * to deal in the Software without restriction, including without limitation
|
| 8 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
| 9 | * and/or sell copies of the Software, and to permit persons to whom the
|
| 10 | * Software is furnished to do so, subject to the following conditions:
|
| 11 | *
|
| 12 | * The above copyright notice and this permission notice shall be included in
|
| 13 | * all copies or substantial portions of the Software.
|
| 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 NONINFRINGEMENT. IN NO EVENT SHALL
|
| 18 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
| 19 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
| 20 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
| 21 | * OTHER DEALINGS IN THE SOFTWARE.
|
| 22 | *
|
| 23 |
|
| 24 |
|
| 25 | #include "amdgpu.h"
|
| 26 | #include "amdgpu_seq64.h"
|
| 27 |
|
| 28 | #include <drm/drm_exec.h>
|
| 29 |
|
| 30 | |
| 31 | * DOC: amdgpu_seq64
|
| 32 | *
|
| 33 | * amdgpu_seq64 allocates a 64bit memory on each request in sequence order.
|
| 34 | * seq64 driver is required for user queue fence memory allocation, TLB
|
| 35 | * counters and VM updates. It has maximum count of 32768 64 bit slots.
|
| 36 |
|
| 37 |
|
| 38 | |
| 39 | * amdgpu_seq64_get_va_base - Get the seq64 va base address
|
| 40 | *
|
| 41 | * @adev: amdgpu_device pointer
|
| 42 | *
|
| 43 | * Returns:
|
| 44 | * va base address on success
|
| 45 |
|
| 46 | static inline u64 amdgpu_seq64_get_va_base(struct amdgpu_device *adev)
|
| 47 | {
|
| 48 | u64 addr = AMDGPU_VA_RESERVED_SEQ64_START(adev);
|
| 49 |
|
| 50 | addr = amdgpu_gmc_sign_extend(addr);
|
| 51 |
|
| 52 | return addr;
|
| 53 | }
|
| 54 |
|
| 55 | |
| 56 | * amdgpu_seq64_map - Map the seq64 memory to VM
|
| 57 | *
|
| 58 | * @adev: amdgpu_device pointer
|
| 59 | * @vm: vm pointer
|
| 60 | * @bo_va: bo_va pointer
|
| 61 | *
|
| 62 | * Map the seq64 memory to the given VM.
|
| 63 | *
|
| 64 | * Returns:
|
| 65 | * 0 on success or a negative error code on failure
|
| 66 |
|
| 67 | int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
|
| 68 | struct amdgpu_bo_va **bo_va)
|
| 69 | {
|
| 70 | struct amdgpu_bo *bo;
|
| 71 | struct drm_exec exec;
|
| 72 | u64 seq64_addr;
|
| 73 | int r;
|
| 74 |
|
| 75 | bo = adev->seq64.sbo;
|
| 76 | if (!bo)
|
| 77 | return -EINVAL;
|
| 78 |
|
| 79 | drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT, 0);
|
| 80 | drm_exec_until_all_locked(&exec) {
|
| 81 | r = amdgpu_vm_lock_pd(vm, &exec, 0);
|
| 82 | if (likely(!r))
|
| 83 | r = drm_exec_lock_obj(&exec, &bo->tbo.base);
|
| 84 | drm_exec_retry_on_contention(&exec);
|
| 85 | if (unlikely(r))
|
| 86 | goto error;
|
| 87 | }
|
| 88 |
|
| 89 | *bo_va = amdgpu_vm_bo_add(adev, vm, bo);
|
| 90 | if (!*bo_va) {
|
| 91 | r = -ENOMEM;
|
| 92 | goto error;
|
| 93 | }
|
| 94 |
|
| 95 | seq64_addr = amdgpu_seq64_get_va_base(adev) & AMDGPU_GMC_HOLE_MASK;
|
| 96 |
|
| 97 | r = amdgpu_vm_bo_map(adev, *bo_va, seq64_addr, 0,
|
| 98 | AMDGPU_VA_RESERVED_SEQ64_SIZE,
|
| 99 | AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_MTYPE_UC);
|
| 100 | if (r) {
|
| 101 | DRM_ERROR("failed to do bo_map on userq sem, err=%d\n", r);
|
| 102 | amdgpu_vm_bo_del(adev, *bo_va);
|
| 103 | goto error;
|
| 104 | }
|
| 105 |
|
| 106 | r = amdgpu_vm_bo_update(adev, *bo_va, false);
|
| 107 | if (r) {
|
| 108 | DRM_ERROR("failed to do vm_bo_update on userq sem\n");
|
| 109 | amdgpu_vm_bo_del(adev, *bo_va);
|
| 110 | goto error;
|
| 111 | }
|
| 112 |
|
| 113 | error:
|
| 114 | drm_exec_fini(&exec);
|
| 115 | return r;
|
| 116 | }
|
| 117 |
|
| 118 | |
| 119 | * amdgpu_seq64_unmap - Unmap the seq64 memory
|
| 120 | *
|
| 121 | * @adev: amdgpu_device pointer
|
| 122 | * @fpriv: DRM file private
|
| 123 | *
|
| 124 | * Unmap the seq64 memory from the given VM.
|
| 125 |
|
| 126 | void amdgpu_seq64_unmap(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv)
|
| 127 | {
|
| 128 | struct amdgpu_vm *vm;
|
| 129 | struct amdgpu_bo *bo;
|
| 130 | struct drm_exec exec;
|
| 131 | int r;
|
| 132 |
|
| 133 | if (!fpriv->seq64_va)
|
| 134 | return;
|
| 135 |
|
| 136 | bo = adev->seq64.sbo;
|
| 137 | if (!bo)
|
| 138 | return;
|
| 139 |
|
| 140 | vm = &fpriv->vm;
|
| 141 |
|
| 142 | drm_exec_init(&exec, 0, 0);
|
| 143 | drm_exec_until_all_locked(&exec) {
|
| 144 | r = amdgpu_vm_lock_pd(vm, &exec, 0);
|
| 145 | if (likely(!r))
|
| 146 | r = drm_exec_lock_obj(&exec, &bo->tbo.base);
|
| 147 | drm_exec_retry_on_contention(&exec);
|
| 148 | if (unlikely(r))
|
| 149 | goto error;
|
| 150 | }
|
| 151 |
|
| 152 | amdgpu_vm_bo_del(adev, fpriv->seq64_va);
|
| 153 |
|
| 154 | fpriv->seq64_va = NULL;
|
| 155 |
|
| 156 | error:
|
| 157 | drm_exec_fini(&exec);
|
| 158 | }
|
| 159 |
|
| 160 | |
| 161 | * amdgpu_seq64_alloc - Allocate a 64 bit memory
|
| 162 | *
|
| 163 | * @adev: amdgpu_device pointer
|
| 164 | * @va: VA to access the seq in process address space
|
| 165 | * @gpu_addr: GPU address to access the seq
|
| 166 | * @cpu_addr: CPU address to access the seq
|
| 167 | *
|
| 168 | * Alloc a 64 bit memory from seq64 pool.
|
| 169 | *
|
| 170 | * Returns:
|
| 171 | * 0 on success or a negative error code on failure
|
| 172 |
|
| 173 | int amdgpu_seq64_alloc(struct amdgpu_device *adev, u64 *va,
|
| 174 | u64 *gpu_addr, u64 **cpu_addr)
|
| 175 | {
|
| 176 | unsigned long bit_pos = 0;
|
| 177 |
|
| 178 | do {
|
| 179 | bit_pos = find_next_zero_bit(adev->seq64.used,
|
| 180 | adev->seq64.num_sem, bit_pos);
|
| 181 | if (bit_pos >= adev->seq64.num_sem)
|
| 182 | return -ENOSPC;
|
| 183 | if (!test_and_set_bit(bit_pos, adev->seq64.used))
|
| 184 | break;
|
| 185 | bit_pos++;
|
| 186 | } while (1);
|
| 187 |
|
| 188 | *va = bit_pos * sizeof(u64) + amdgpu_seq64_get_va_base(adev);
|
| 189 |
|
| 190 | if (gpu_addr)
|
| 191 | *gpu_addr = bit_pos * sizeof(u64) + adev->seq64.gpu_addr;
|
| 192 |
|
| 193 | *cpu_addr = bit_pos + adev->seq64.cpu_base_addr;
|
| 194 |
|
| 195 | return 0;
|
| 196 | }
|
| 197 |
|
| 198 | |
| 199 | * amdgpu_seq64_free - Free the given 64 bit memory
|
| 200 | *
|
| 201 | * @adev: amdgpu_device pointer
|
| 202 | * @va: gpu start address to be freed
|
| 203 | *
|
| 204 | * Free the given 64 bit memory from seq64 pool.
|
| 205 |
|
| 206 | void amdgpu_seq64_free(struct amdgpu_device *adev, u64 va)
|
| 207 | {
|
| 208 | unsigned long bit_pos;
|
| 209 |
|
| 210 | bit_pos = (va - amdgpu_seq64_get_va_base(adev)) / sizeof(u64);
|
| 211 | if (bit_pos < adev->seq64.num_sem)
|
| 212 | clear_bit(bit_pos, adev->seq64.used);
|
| 213 | }
|
| 214 |
|
| 215 | |
| 216 | * amdgpu_seq64_fini - Cleanup seq64 driver
|
| 217 | *
|
| 218 | * @adev: amdgpu_device pointer
|
| 219 | *
|
| 220 | * Free the memory space allocated for seq64.
|
| 221 | *
|
| 222 |
|
| 223 | void amdgpu_seq64_fini(struct amdgpu_device *adev)
|
| 224 | {
|
| 225 | amdgpu_bo_free_kernel(&adev->seq64.sbo,
|
| 226 | NULL,
|
| 227 | (void **)&adev->seq64.cpu_base_addr);
|
| 228 | }
|
| 229 |
|
| 230 | |
| 231 | * amdgpu_seq64_init - Initialize seq64 driver
|
| 232 | *
|
| 233 | * @adev: amdgpu_device pointer
|
| 234 | *
|
| 235 | * Allocate the required memory space for seq64.
|
| 236 | *
|
| 237 | * Returns:
|
| 238 | * 0 on success or a negative error code on failure
|
| 239 |
|
| 240 | int amdgpu_seq64_init(struct amdgpu_device *adev)
|
| 241 | {
|
| 242 | int r;
|
| 243 |
|
| 244 | if (adev->seq64.sbo)
|
| 245 | return 0;
|
| 246 |
|
| 247 | |
| 248 | * AMDGPU_MAX_SEQ64_SLOTS * sizeof(u64) * 8 = AMDGPU_MAX_SEQ64_SLOTS
|
| 249 | * 64bit slots
|
| 250 |
|
| 251 | r = amdgpu_bo_create_kernel(adev, AMDGPU_VA_RESERVED_SEQ64_SIZE,
|
| 252 | PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
|
| 253 | &adev->seq64.sbo, &adev->seq64.gpu_addr,
|
| 254 | (void **)&adev->seq64.cpu_base_addr);
|
| 255 | if (r) {
|
| 256 | dev_warn(adev->dev, "(%d) create seq64 failed\n", r);
|
| 257 | return r;
|
| 258 | }
|
| 259 |
|
| 260 | memset(adev->seq64.cpu_base_addr, 0, AMDGPU_VA_RESERVED_SEQ64_SIZE);
|
| 261 |
|
| 262 | adev->seq64.num_sem = AMDGPU_MAX_SEQ64_SLOTS;
|
| 263 | memset(&adev->seq64.used, 0, sizeof(adev->seq64.used));
|
| 264 |
|
| 265 | return 0;
|
| 266 | }
|