1 // SPDX-License-Identifier: MIT
3 * Copyright 2023 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
26 #include "amdgpu_seq64.h"
28 #include <drm/drm_exec.h>
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.
39 * amdgpu_seq64_map - Map the seq64 memory to VM
41 * @adev: amdgpu_device pointer
43 * @bo_va: bo_va pointer
44 * @seq64_addr: seq64 vaddr start address
45 * @size: seq64 pool size
47 * Map the seq64 memory to the given VM.
50 * 0 on success or a negative error code on failure
52 int amdgpu_seq64_map(struct amdgpu_device *adev, struct amdgpu_vm *vm,
53 struct amdgpu_bo_va **bo_va, u64 seq64_addr,
64 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT);
65 drm_exec_until_all_locked(&exec) {
66 r = amdgpu_vm_lock_pd(vm, &exec, 0);
68 r = drm_exec_lock_obj(&exec, &bo->tbo.base);
69 drm_exec_retry_on_contention(&exec);
74 *bo_va = amdgpu_vm_bo_add(adev, vm, bo);
80 r = amdgpu_vm_bo_map(adev, *bo_va, seq64_addr, 0, size,
81 AMDGPU_PTE_READABLE | AMDGPU_PTE_WRITEABLE |
82 AMDGPU_PTE_EXECUTABLE);
84 DRM_ERROR("failed to do bo_map on userq sem, err=%d\n", r);
85 amdgpu_vm_bo_del(adev, *bo_va);
89 r = amdgpu_vm_bo_update(adev, *bo_va, false);
91 DRM_ERROR("failed to do vm_bo_update on userq sem\n");
92 amdgpu_vm_bo_del(adev, *bo_va);
102 * amdgpu_seq64_unmap - Unmap the seq64 memory
104 * @adev: amdgpu_device pointer
105 * @fpriv: DRM file private
107 * Unmap the seq64 memory from the given VM.
109 void amdgpu_seq64_unmap(struct amdgpu_device *adev, struct amdgpu_fpriv *fpriv)
111 struct amdgpu_vm *vm;
112 struct amdgpu_bo *bo;
113 struct drm_exec exec;
116 if (!fpriv->seq64_va)
119 bo = adev->seq64.sbo;
125 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT);
126 drm_exec_until_all_locked(&exec) {
127 r = amdgpu_vm_lock_pd(vm, &exec, 0);
129 r = drm_exec_lock_obj(&exec, &bo->tbo.base);
130 drm_exec_retry_on_contention(&exec);
135 amdgpu_vm_bo_del(adev, fpriv->seq64_va);
137 fpriv->seq64_va = NULL;
140 drm_exec_fini(&exec);
144 * amdgpu_seq64_alloc - Allocate a 64 bit memory
146 * @adev: amdgpu_device pointer
147 * @gpu_addr: allocated gpu VA start address
148 * @cpu_addr: allocated cpu VA start address
150 * Alloc a 64 bit memory from seq64 pool.
153 * 0 on success or a negative error code on failure
155 int amdgpu_seq64_alloc(struct amdgpu_device *adev, u64 *gpu_addr,
158 unsigned long bit_pos;
161 bit_pos = find_first_zero_bit(adev->seq64.used, adev->seq64.num_sem);
163 if (bit_pos < adev->seq64.num_sem) {
164 __set_bit(bit_pos, adev->seq64.used);
165 offset = bit_pos << 6; /* convert to qw offset */
170 *gpu_addr = offset + AMDGPU_SEQ64_VADDR_START;
171 *cpu_addr = offset + adev->seq64.cpu_base_addr;
177 * amdgpu_seq64_free - Free the given 64 bit memory
179 * @adev: amdgpu_device pointer
180 * @gpu_addr: gpu start address to be freed
182 * Free the given 64 bit memory from seq64 pool.
185 void amdgpu_seq64_free(struct amdgpu_device *adev, u64 gpu_addr)
189 offset = gpu_addr - AMDGPU_SEQ64_VADDR_START;
192 if (offset < adev->seq64.num_sem)
193 __clear_bit(offset, adev->seq64.used);
197 * amdgpu_seq64_fini - Cleanup seq64 driver
199 * @adev: amdgpu_device pointer
201 * Free the memory space allocated for seq64.
204 void amdgpu_seq64_fini(struct amdgpu_device *adev)
206 amdgpu_bo_free_kernel(&adev->seq64.sbo,
208 (void **)&adev->seq64.cpu_base_addr);
212 * amdgpu_seq64_init - Initialize seq64 driver
214 * @adev: amdgpu_device pointer
216 * Allocate the required memory space for seq64.
219 * 0 on success or a negative error code on failure
221 int amdgpu_seq64_init(struct amdgpu_device *adev)
229 * AMDGPU_MAX_SEQ64_SLOTS * sizeof(u64) * 8 = AMDGPU_MAX_SEQ64_SLOTS
232 r = amdgpu_bo_create_kernel(adev, AMDGPU_SEQ64_SIZE,
233 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
234 &adev->seq64.sbo, NULL,
235 (void **)&adev->seq64.cpu_base_addr);
237 dev_warn(adev->dev, "(%d) create seq64 failed\n", r);
241 memset(adev->seq64.cpu_base_addr, 0, AMDGPU_SEQ64_SIZE);
243 adev->seq64.num_sem = AMDGPU_MAX_SEQ64_SLOTS;
244 memset(&adev->seq64.used, 0, sizeof(adev->seq64.used));