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.
27 #include "amdgpu_drm.h"
28 #include "amdgpu_internal.h"
29 #include "util_math.h"
31 static struct amdgpu_bo_va_mgr vamgr = {{0}};
33 static void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, struct amdgpu_device *dev)
35 mgr->va_offset = dev->dev_info.virtual_address_offset;
36 mgr->va_max = dev->dev_info.virtual_address_max;
37 mgr->va_alignment = dev->dev_info.virtual_address_alignment;
39 list_inithead(&mgr->va_holes);
40 pthread_mutex_init(&mgr->bo_va_mutex, NULL);
43 static void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr)
45 struct amdgpu_bo_va_hole *hole;
46 LIST_FOR_EACH_ENTRY(hole, &mgr->va_holes, list) {
47 list_del(&hole->list);
50 pthread_mutex_destroy(&mgr->bo_va_mutex);
53 struct amdgpu_bo_va_mgr * amdgpu_vamgr_get_global(struct amdgpu_device *dev)
56 ref = atomic_inc_return(&vamgr.refcount);
59 amdgpu_vamgr_init(&vamgr, dev);
63 void amdgpu_vamgr_reference(struct amdgpu_bo_va_mgr **dst,
64 struct amdgpu_bo_va_mgr *src)
66 if (update_references(&(*dst)->refcount, NULL))
67 amdgpu_vamgr_deinit(*dst);
71 uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr,
72 uint64_t size, uint64_t alignment)
74 struct amdgpu_bo_va_hole *hole, *n;
75 uint64_t offset = 0, waste = 0;
77 alignment = MAX2(alignment, mgr->va_alignment);
78 size = ALIGN(size, mgr->va_alignment);
80 pthread_mutex_lock(&mgr->bo_va_mutex);
81 /* TODO: using more appropriate way to track the holes */
82 /* first look for a hole */
83 LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) {
84 offset = hole->offset;
85 waste = offset % alignment;
86 waste = waste ? alignment - waste : 0;
88 if (offset >= (hole->offset + hole->size)) {
91 if (!waste && hole->size == size) {
92 offset = hole->offset;
93 list_del(&hole->list);
95 pthread_mutex_unlock(&mgr->bo_va_mutex);
98 if ((hole->size - waste) > size) {
101 sizeof(struct amdgpu_bo_va_hole));
103 n->offset = hole->offset;
104 list_add(&n->list, &hole->list);
106 hole->size -= (size + waste);
107 hole->offset += size + waste;
108 pthread_mutex_unlock(&mgr->bo_va_mutex);
111 if ((hole->size - waste) == size) {
113 pthread_mutex_unlock(&mgr->bo_va_mutex);
118 offset = mgr->va_offset;
119 waste = offset % alignment;
120 waste = waste ? alignment - waste : 0;
122 if (offset + waste + size > mgr->va_max) {
123 pthread_mutex_unlock(&mgr->bo_va_mutex);
124 return AMDGPU_INVALID_VA_ADDRESS;
128 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
131 list_add(&n->list, &mgr->va_holes);
134 mgr->va_offset += size + waste;
135 pthread_mutex_unlock(&mgr->bo_va_mutex);
139 void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr,
140 uint64_t va, uint64_t size)
142 struct amdgpu_bo_va_hole *hole;
144 if (va == AMDGPU_INVALID_VA_ADDRESS)
147 size = ALIGN(size, mgr->va_alignment);
149 pthread_mutex_lock(&mgr->bo_va_mutex);
150 if ((va + size) == mgr->va_offset) {
152 /* Delete uppermost hole if it reaches the new top */
153 if (!LIST_IS_EMPTY(&mgr->va_holes)) {
154 hole = container_of(mgr->va_holes.next, hole, list);
155 if ((hole->offset + hole->size) == va) {
156 mgr->va_offset = hole->offset;
157 list_del(&hole->list);
162 struct amdgpu_bo_va_hole *next;
164 hole = container_of(&mgr->va_holes, hole, list);
165 LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
166 if (next->offset < va)
171 if (&hole->list != &mgr->va_holes) {
172 /* Grow upper hole if it's adjacent */
173 if (hole->offset == (va + size)) {
176 /* Merge lower hole if it's adjacent */
178 && &next->list != &mgr->va_holes
179 && (next->offset + next->size) == va) {
180 next->size += hole->size;
181 list_del(&hole->list);
188 /* Grow lower hole if it's adjacent */
189 if (next != hole && &next->list != &mgr->va_holes &&
190 (next->offset + next->size) == va) {
195 /* FIXME on allocation failure we just lose virtual address space
196 * maybe print a warning
198 next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
202 list_add(&next->list, &hole->list);
206 pthread_mutex_unlock(&mgr->bo_va_mutex);