blob: 8a707cbcde2a052674ff170069f41c7213e71e65 [file] [log] [blame]
Alex Deucher09361392015-04-20 12:04:22 -04001/*
2 * Copyright 2014 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
Emil Velikova30da8e2015-08-07 17:20:51 +010022 */
Alex Deucher09361392015-04-20 12:04:22 -040023
Emil Velikovf4c2bfd2015-08-07 17:17:43 +010024#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
Alex Deucher09361392015-04-20 12:04:22 -040028#include <stdlib.h>
29#include <string.h>
Sabre Shao23fab592015-07-09 13:50:36 +080030#include <errno.h>
Alex Deucher09361392015-04-20 12:04:22 -040031#include "amdgpu.h"
32#include "amdgpu_drm.h"
33#include "amdgpu_internal.h"
34#include "util_math.h"
35
Sabre Shao12802da2015-07-09 13:53:24 +080036int amdgpu_va_range_query(amdgpu_device_handle dev,
37 enum amdgpu_gpu_va_range type, uint64_t *start, uint64_t *end)
38{
39 if (type == amdgpu_gpu_va_range_general) {
40 *start = dev->dev_info.virtual_address_offset;
41 *end = dev->dev_info.virtual_address_max;
42 return 0;
43 }
44 return -EINVAL;
45}
46
Jammy Zhouffa305d2015-08-17 11:09:08 +080047drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
Jammy Zhou102ab6f2015-08-17 11:09:07 +080048 uint64_t max, uint64_t alignment)
Alex Deucher09361392015-04-20 12:04:22 -040049{
Jammy Zhou102ab6f2015-08-17 11:09:07 +080050 mgr->va_offset = start;
51 mgr->va_max = max;
52 mgr->va_alignment = alignment;
Alex Deucher09361392015-04-20 12:04:22 -040053
Ken Wang322d02d2015-05-21 17:21:21 +080054 list_inithead(&mgr->va_holes);
55 pthread_mutex_init(&mgr->bo_va_mutex, NULL);
56}
Alex Deucher09361392015-04-20 12:04:22 -040057
Jammy Zhouffa305d2015-08-17 11:09:08 +080058drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr)
Ken Wang322d02d2015-05-21 17:21:21 +080059{
Tom St Denis1a6a8f32015-10-09 12:07:26 -040060 struct amdgpu_bo_va_hole *hole, *tmp;
61 LIST_FOR_EACH_ENTRY_SAFE(hole, tmp, &mgr->va_holes, list) {
Ken Wang322d02d2015-05-21 17:21:21 +080062 list_del(&hole->list);
63 free(hole);
64 }
65 pthread_mutex_destroy(&mgr->bo_va_mutex);
66}
67
Emil Velikovb4718182015-08-07 16:54:29 +010068drm_private uint64_t
69amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
70 uint64_t alignment, uint64_t base_required)
Alex Deucher09361392015-04-20 12:04:22 -040071{
monk.liu9066acf2015-05-13 13:58:43 +080072 struct amdgpu_bo_va_hole *hole, *n;
73 uint64_t offset = 0, waste = 0;
Alex Deucher09361392015-04-20 12:04:22 -040074
monk.liu9066acf2015-05-13 13:58:43 +080075 alignment = MAX2(alignment, mgr->va_alignment);
76 size = ALIGN(size, mgr->va_alignment);
Alex Deucher09361392015-04-20 12:04:22 -040077
Ken Wang5b019082015-07-09 13:48:25 +080078 if (base_required % alignment)
79 return AMDGPU_INVALID_VA_ADDRESS;
80
monk.liu9066acf2015-05-13 13:58:43 +080081 pthread_mutex_lock(&mgr->bo_va_mutex);
82 /* TODO: using more appropriate way to track the holes */
83 /* first look for a hole */
Jammy Zhou56d8dd62015-08-17 11:09:09 +080084 LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) {
Ken Wang5b019082015-07-09 13:48:25 +080085 if (base_required) {
86 if(hole->offset > base_required ||
87 (hole->offset + hole->size) < (base_required + size))
88 continue;
89 waste = base_required - hole->offset;
90 offset = base_required;
91 } else {
92 offset = hole->offset;
93 waste = offset % alignment;
94 waste = waste ? alignment - waste : 0;
95 offset += waste;
96 if (offset >= (hole->offset + hole->size)) {
97 continue;
98 }
monk.liu9066acf2015-05-13 13:58:43 +080099 }
100 if (!waste && hole->size == size) {
101 offset = hole->offset;
102 list_del(&hole->list);
103 free(hole);
104 pthread_mutex_unlock(&mgr->bo_va_mutex);
105 return offset;
106 }
107 if ((hole->size - waste) > size) {
108 if (waste) {
Ken Wang5b019082015-07-09 13:48:25 +0800109 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
monk.liu9066acf2015-05-13 13:58:43 +0800110 n->size = waste;
111 n->offset = hole->offset;
112 list_add(&n->list, &hole->list);
113 }
114 hole->size -= (size + waste);
115 hole->offset += size + waste;
116 pthread_mutex_unlock(&mgr->bo_va_mutex);
117 return offset;
118 }
119 if ((hole->size - waste) == size) {
120 hole->size = waste;
121 pthread_mutex_unlock(&mgr->bo_va_mutex);
122 return offset;
123 }
124 }
Alex Deucher09361392015-04-20 12:04:22 -0400125
Ken Wang5b019082015-07-09 13:48:25 +0800126 if (base_required) {
Tom St Denis988f31e2015-10-09 10:36:04 -0400127 if (base_required < mgr->va_offset) {
128 pthread_mutex_unlock(&mgr->bo_va_mutex);
Ken Wang5b019082015-07-09 13:48:25 +0800129 return AMDGPU_INVALID_VA_ADDRESS;
Tom St Denis988f31e2015-10-09 10:36:04 -0400130 }
Ken Wang5b019082015-07-09 13:48:25 +0800131 offset = mgr->va_offset;
132 waste = base_required - mgr->va_offset;
133 } else {
134 offset = mgr->va_offset;
135 waste = offset % alignment;
136 waste = waste ? alignment - waste : 0;
137 }
Jammy Zhou241cf6d2015-05-13 01:14:11 +0800138
139 if (offset + waste + size > mgr->va_max) {
140 pthread_mutex_unlock(&mgr->bo_va_mutex);
141 return AMDGPU_INVALID_VA_ADDRESS;
142 }
143
monk.liu9066acf2015-05-13 13:58:43 +0800144 if (waste) {
145 n = calloc(1, sizeof(struct amdgpu_bo_va_hole));
146 n->size = waste;
147 n->offset = offset;
148 list_add(&n->list, &mgr->va_holes);
149 }
Ken Wang5b019082015-07-09 13:48:25 +0800150
monk.liu9066acf2015-05-13 13:58:43 +0800151 offset += waste;
152 mgr->va_offset += size + waste;
153 pthread_mutex_unlock(&mgr->bo_va_mutex);
154 return offset;
Alex Deucher09361392015-04-20 12:04:22 -0400155}
156
Emil Velikovb4718182015-08-07 16:54:29 +0100157drm_private void
158amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size)
Alex Deucher09361392015-04-20 12:04:22 -0400159{
monk.liu9066acf2015-05-13 13:58:43 +0800160 struct amdgpu_bo_va_hole *hole;
Alex Deucher09361392015-04-20 12:04:22 -0400161
monk.liud3e71952015-05-13 14:01:53 +0800162 if (va == AMDGPU_INVALID_VA_ADDRESS)
163 return;
164
monk.liu9066acf2015-05-13 13:58:43 +0800165 size = ALIGN(size, mgr->va_alignment);
Alex Deucher09361392015-04-20 12:04:22 -0400166
monk.liu9066acf2015-05-13 13:58:43 +0800167 pthread_mutex_lock(&mgr->bo_va_mutex);
168 if ((va + size) == mgr->va_offset) {
169 mgr->va_offset = va;
170 /* Delete uppermost hole if it reaches the new top */
171 if (!LIST_IS_EMPTY(&mgr->va_holes)) {
172 hole = container_of(mgr->va_holes.next, hole, list);
173 if ((hole->offset + hole->size) == va) {
174 mgr->va_offset = hole->offset;
175 list_del(&hole->list);
176 free(hole);
177 }
178 }
179 } else {
180 struct amdgpu_bo_va_hole *next;
Alex Deucher09361392015-04-20 12:04:22 -0400181
monk.liu9066acf2015-05-13 13:58:43 +0800182 hole = container_of(&mgr->va_holes, hole, list);
183 LIST_FOR_EACH_ENTRY(next, &mgr->va_holes, list) {
184 if (next->offset < va)
185 break;
186 hole = next;
187 }
Alex Deucher09361392015-04-20 12:04:22 -0400188
monk.liu9066acf2015-05-13 13:58:43 +0800189 if (&hole->list != &mgr->va_holes) {
190 /* Grow upper hole if it's adjacent */
191 if (hole->offset == (va + size)) {
192 hole->offset = va;
193 hole->size += size;
194 /* Merge lower hole if it's adjacent */
195 if (next != hole
196 && &next->list != &mgr->va_holes
197 && (next->offset + next->size) == va) {
198 next->size += hole->size;
199 list_del(&hole->list);
200 free(hole);
201 }
202 goto out;
203 }
204 }
Alex Deucher09361392015-04-20 12:04:22 -0400205
monk.liu9066acf2015-05-13 13:58:43 +0800206 /* Grow lower hole if it's adjacent */
207 if (next != hole && &next->list != &mgr->va_holes &&
208 (next->offset + next->size) == va) {
209 next->size += size;
210 goto out;
211 }
Alex Deucher09361392015-04-20 12:04:22 -0400212
monk.liu9066acf2015-05-13 13:58:43 +0800213 /* FIXME on allocation failure we just lose virtual address space
214 * maybe print a warning
215 */
216 next = calloc(1, sizeof(struct amdgpu_bo_va_hole));
217 if (next) {
218 next->size = size;
219 next->offset = va;
220 list_add(&next->list, &hole->list);
221 }
222 }
Alex Deucher09361392015-04-20 12:04:22 -0400223out:
monk.liu9066acf2015-05-13 13:58:43 +0800224 pthread_mutex_unlock(&mgr->bo_va_mutex);
Alex Deucher09361392015-04-20 12:04:22 -0400225}
Sabre Shao23fab592015-07-09 13:50:36 +0800226
227int amdgpu_va_range_alloc(amdgpu_device_handle dev,
228 enum amdgpu_gpu_va_range va_range_type,
229 uint64_t size,
230 uint64_t va_base_alignment,
231 uint64_t va_base_required,
232 uint64_t *va_base_allocated,
Jammy Zhou95d0f352015-07-16 10:29:58 +0800233 amdgpu_va_handle *va_range_handle,
234 uint64_t flags)
Sabre Shao23fab592015-07-09 13:50:36 +0800235{
Jammy Zhouffa305d2015-08-17 11:09:08 +0800236 struct amdgpu_bo_va_mgr *vamgr;
Sabre Shao23fab592015-07-09 13:50:36 +0800237
Jammy Zhouffa305d2015-08-17 11:09:08 +0800238 if (flags & AMDGPU_VA_RANGE_32_BIT)
239 vamgr = dev->vamgr_32;
240 else
241 vamgr = dev->vamgr;
242
243 va_base_alignment = MAX2(va_base_alignment, vamgr->va_alignment);
244 size = ALIGN(size, vamgr->va_alignment);
245
246 *va_base_allocated = amdgpu_vamgr_find_va(vamgr, size,
Sabre Shao23fab592015-07-09 13:50:36 +0800247 va_base_alignment, va_base_required);
248
Jammy Zhouffa305d2015-08-17 11:09:08 +0800249 if (!(flags & AMDGPU_VA_RANGE_32_BIT) &&
250 (*va_base_allocated == AMDGPU_INVALID_VA_ADDRESS)) {
251 /* fallback to 32bit address */
252 vamgr = dev->vamgr_32;
253 *va_base_allocated = amdgpu_vamgr_find_va(vamgr, size,
254 va_base_alignment, va_base_required);
255 }
256
Sabre Shao23fab592015-07-09 13:50:36 +0800257 if (*va_base_allocated != AMDGPU_INVALID_VA_ADDRESS) {
258 struct amdgpu_va* va;
259 va = calloc(1, sizeof(struct amdgpu_va));
260 if(!va){
Jammy Zhouffa305d2015-08-17 11:09:08 +0800261 amdgpu_vamgr_free_va(vamgr, *va_base_allocated, size);
Sabre Shao23fab592015-07-09 13:50:36 +0800262 return -ENOMEM;
263 }
264 va->dev = dev;
265 va->address = *va_base_allocated;
266 va->size = size;
267 va->range = va_range_type;
Jammy Zhouffa305d2015-08-17 11:09:08 +0800268 va->vamgr = vamgr;
Sabre Shao23fab592015-07-09 13:50:36 +0800269 *va_range_handle = va;
270 } else {
271 return -EINVAL;
272 }
273
274 return 0;
275}
276
277int amdgpu_va_range_free(amdgpu_va_handle va_range_handle)
278{
279 if(!va_range_handle || !va_range_handle->address)
280 return 0;
Jammy Zhouffa305d2015-08-17 11:09:08 +0800281
282 amdgpu_vamgr_free_va(va_range_handle->vamgr,
283 va_range_handle->address,
Sabre Shao23fab592015-07-09 13:50:36 +0800284 va_range_handle->size);
285 free(va_range_handle);
286 return 0;
287}