2 * Copyright 2014 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
28 #include "amdgpu_drm.h"
29 #include "amdgpu_internal.h"
30 #include "util_math.h"
32 drm_public int amdgpu_va_range_query(amdgpu_device_handle dev,
33 enum amdgpu_gpu_va_range type,
34 uint64_t *start, uint64_t *end)
36 if (type != amdgpu_gpu_va_range_general)
39 *start = dev->dev_info.virtual_address_offset;
40 *end = dev->dev_info.virtual_address_max;
44 drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
45 uint64_t max, uint64_t alignment)
47 struct amdgpu_bo_va_hole *n;
50 mgr->va_alignment = alignment;
52 list_inithead(&mgr->va_holes);
53 pthread_mutex_init(&mgr->bo_va_mutex, NULL);
54 pthread_mutex_lock(&mgr->bo_va_mutex);
55 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
56 n->size = mgr->va_max - start;
58 list_add(&n->list, &mgr->va_holes);
59 pthread_mutex_unlock(&mgr->bo_va_mutex);
62 drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr)
64 struct amdgpu_bo_va_hole *hole, *tmp;
65 LIST_FOR_EACH_ENTRY_SAFE(hole, tmp, &mgr->va_holes, list) {
66 list_del(&hole->list);
69 pthread_mutex_destroy(&mgr->bo_va_mutex);
72 static drm_private uint64_t
73 amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
74 uint64_t alignment, uint64_t base_required)
76 struct amdgpu_bo_va_hole *hole, *n;
77 uint64_t offset = 0, waste = 0;
80 alignment = MAX2(alignment, mgr->va_alignment);
81 size = ALIGN(size, mgr->va_alignment);
83 if (base_required % alignment)
84 return AMDGPU_INVALID_VA_ADDRESS;
86 pthread_mutex_lock(&mgr->bo_va_mutex);
87 LIST_FOR_EACH_ENTRY_SAFE_REV(hole, n, &mgr->va_holes, list) {
89 if (hole->offset > base_required ||
90 (hole->offset + hole->size) < (base_required + size))
92 waste = base_required - hole->offset;
93 offset = base_required;
95 offset = hole->offset;
96 waste = offset % alignment;
97 waste = waste ? alignment - waste : 0;
99 if (offset >= (hole->offset + hole->size)) {
103 if (!waste && hole->size == size) {
104 offset = hole->offset;
105 list_del(&hole->list);
107 pthread_mutex_unlock(&mgr->bo_va_mutex);
110 if ((hole->size - waste) > size) {
112 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
114 n->offset = hole->offset;
115 list_add(&n->list, &hole->list);
117 hole->size -= (size + waste);
118 hole->offset += size + waste;
119 pthread_mutex_unlock(&mgr->bo_va_mutex);
122 if ((hole->size - waste) == size) {
124 pthread_mutex_unlock(&mgr->bo_va_mutex);
129 pthread_mutex_unlock(&mgr->bo_va_mutex);
130 return AMDGPU_INVALID_VA_ADDRESS;
133 static drm_private void
134 amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size)
136 struct amdgpu_bo_va_hole *hole, *next;
138 if (va == AMDGPU_INVALID_VA_ADDRESS)
141 size = ALIGN(size, mgr->va_alignment);
143 pthread_mutex_lock(&mgr->bo_va_mutex);
144 hole = container_of(&mgr->va_holes, hole, list);
145 LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
146 if (next->offset < va)
151 if (&hole->list != &mgr->va_holes) {
152 /* Grow upper hole if it's adjacent */
153 if (hole->offset == (va + size)) {
156 /* Merge lower hole if it's adjacent */
158 &next->list != &mgr->va_holes &&
159 (next->offset + next->size) == va) {
160 next->size += hole->size;
161 list_del(&hole->list);
168 /* Grow lower hole if it's adjacent */
169 if (next != hole && &next->list != &mgr->va_holes &&
170 (next->offset + next->size) == va) {
175 /* FIXME on allocation failure we just lose virtual address space
176 * maybe print a warning
178 next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
182 list_add(&next->list, &hole->list);
186 pthread_mutex_unlock(&mgr->bo_va_mutex);
189 drm_public int amdgpu_va_range_alloc(amdgpu_device_handle dev,
190 enum amdgpu_gpu_va_range va_range_type,
192 uint64_t va_base_alignment,
193 uint64_t va_base_required,
194 uint64_t *va_base_allocated,
195 amdgpu_va_handle *va_range_handle,
198 struct amdgpu_bo_va_mgr *vamgr;
200 /* Clear the flag when the high VA manager is not initialized */
201 if (flags & AMDGPU_VA_RANGE_HIGH && !dev->vamgr_high_32.va_max)
202 flags &= ~AMDGPU_VA_RANGE_HIGH;
204 if (flags & AMDGPU_VA_RANGE_HIGH) {
205 if (flags & AMDGPU_VA_RANGE_32_BIT)
206 vamgr = &dev->vamgr_high_32;
208 vamgr = &dev->vamgr_high;
210 if (flags & AMDGPU_VA_RANGE_32_BIT)
211 vamgr = &dev->vamgr_32;
216 va_base_alignment = MAX2(va_base_alignment, vamgr->va_alignment);
217 size = ALIGN(size, vamgr->va_alignment);
219 *va_base_allocated = amdgpu_vamgr_find_va(vamgr, size,
220 va_base_alignment, va_base_required);
222 if (!(flags & AMDGPU_VA_RANGE_32_BIT) &&
223 (*va_base_allocated == AMDGPU_INVALID_VA_ADDRESS)) {
224 /* fallback to 32bit address */
225 if (flags & AMDGPU_VA_RANGE_HIGH)
226 vamgr = &dev->vamgr_high_32;
228 vamgr = &dev->vamgr_32;
229 *va_base_allocated = amdgpu_vamgr_find_va(vamgr, size,
230 va_base_alignment, va_base_required);
233 if (*va_base_allocated != AMDGPU_INVALID_VA_ADDRESS) {
234 struct amdgpu_va* va;
235 va = calloc(1, sizeof(struct amdgpu_va));
237 amdgpu_vamgr_free_va(vamgr, *va_base_allocated, size);
241 va->address = *va_base_allocated;
243 va->range = va_range_type;
245 *va_range_handle = va;
253 drm_public int amdgpu_va_range_free(amdgpu_va_handle va_range_handle)
255 if(!va_range_handle || !va_range_handle->address)
258 amdgpu_vamgr_free_va(va_range_handle->vamgr,
259 va_range_handle->address,
260 va_range_handle->size);
261 free(va_range_handle);