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 void amdgpu_vamgr_init(struct amdgpu_device *dev)
33 struct amdgpu_bo_va_mgr *vamgr = &dev->vamgr;
35 vamgr->va_offset = dev->dev_info.virtual_address_offset;
36 vamgr->va_max = dev->dev_info.virtual_address_max;
37 vamgr->va_alignment = dev->dev_info.virtual_address_alignment;
39 list_inithead(&vamgr->va_holes);
40 pthread_mutex_init(&vamgr->bo_va_mutex, NULL);
43 uint64_t amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr,
44 uint64_t size, uint64_t alignment)
46 struct amdgpu_bo_va_hole *hole, *n;
47 uint64_t offset = 0, waste = 0;
49 alignment = MAX2(alignment, mgr->va_alignment);
50 size = ALIGN(size, mgr->va_alignment);
52 pthread_mutex_lock(&mgr->bo_va_mutex);
53 /* TODO: using more appropriate way to track the holes */
54 /* first look for a hole */
55 LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) {
56 offset = hole->offset;
57 waste = offset % alignment;
58 waste = waste ? alignment - waste : 0;
60 if (offset >= (hole->offset + hole->size)) {
63 if (!waste && hole->size == size) {
64 offset = hole->offset;
65 list_del(&hole->list);
67 pthread_mutex_unlock(&mgr->bo_va_mutex);
70 if ((hole->size - waste) > size) {
73 sizeof(struct amdgpu_bo_va_hole));
75 n->offset = hole->offset;
76 list_add(&n->list, &hole->list);
78 hole->size -= (size + waste);
79 hole->offset += size + waste;
80 pthread_mutex_unlock(&mgr->bo_va_mutex);
83 if ((hole->size - waste) == size) {
85 pthread_mutex_unlock(&mgr->bo_va_mutex);
90 offset = mgr->va_offset;
91 waste = offset % alignment;
92 waste = waste ? alignment - waste : 0;
94 if (offset + waste + size > mgr->va_max) {
95 pthread_mutex_unlock(&mgr->bo_va_mutex);
96 return AMDGPU_INVALID_VA_ADDRESS;
100 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
103 list_add(&n->list, &mgr->va_holes);
106 mgr->va_offset += size + waste;
107 pthread_mutex_unlock(&mgr->bo_va_mutex);
111 void amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va,
114 struct amdgpu_bo_va_hole *hole;
116 size = ALIGN(size, mgr->va_alignment);
118 pthread_mutex_lock(&mgr->bo_va_mutex);
119 if ((va + size) == mgr->va_offset) {
121 /* Delete uppermost hole if it reaches the new top */
122 if (!LIST_IS_EMPTY(&mgr->va_holes)) {
123 hole = container_of(mgr->va_holes.next, hole, list);
124 if ((hole->offset + hole->size) == va) {
125 mgr->va_offset = hole->offset;
126 list_del(&hole->list);
131 struct amdgpu_bo_va_hole *next;
133 hole = container_of(&mgr->va_holes, hole, list);
134 LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
135 if (next->offset < va)
140 if (&hole->list != &mgr->va_holes) {
141 /* Grow upper hole if it's adjacent */
142 if (hole->offset == (va + size)) {
145 /* Merge lower hole if it's adjacent */
147 && &next->list != &mgr->va_holes
148 && (next->offset + next->size) == va) {
149 next->size += hole->size;
150 list_del(&hole->list);
157 /* Grow lower hole if it's adjacent */
158 if (next != hole && &next->list != &mgr->va_holes &&
159 (next->offset + next->size) == va) {
164 /* FIXME on allocation failure we just lose virtual address space
165 * maybe print a warning
167 next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
171 list_add(&next->list, &hole->list);
175 pthread_mutex_unlock(&mgr->bo_va_mutex);