Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 1 | /* |
| 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 Velikov | a30da8e | 2015-08-07 17:20:51 +0100 | [diff] [blame] | 22 | */ |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 23 | |
Emil Velikov | f4c2bfd | 2015-08-07 17:17:43 +0100 | [diff] [blame] | 24 | #ifdef HAVE_CONFIG_H |
| 25 | #include "config.h" |
| 26 | #endif |
| 27 | |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
Sabre Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 30 | #include <errno.h> |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 31 | #include "amdgpu.h" |
| 32 | #include "amdgpu_drm.h" |
| 33 | #include "amdgpu_internal.h" |
| 34 | #include "util_math.h" |
| 35 | |
Sabre Shao | 12802da | 2015-07-09 13:53:24 +0800 | [diff] [blame] | 36 | int 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 Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 47 | drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start, |
Jammy Zhou | 102ab6f | 2015-08-17 11:09:07 +0800 | [diff] [blame] | 48 | uint64_t max, uint64_t alignment) |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 49 | { |
Jammy Zhou | 102ab6f | 2015-08-17 11:09:07 +0800 | [diff] [blame] | 50 | mgr->va_offset = start; |
| 51 | mgr->va_max = max; |
| 52 | mgr->va_alignment = alignment; |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 53 | |
Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 54 | list_inithead(&mgr->va_holes); |
| 55 | pthread_mutex_init(&mgr->bo_va_mutex, NULL); |
| 56 | } |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 57 | |
Jammy Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 58 | drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr) |
Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 59 | { |
Tom St Denis | 1a6a8f3 | 2015-10-09 12:07:26 -0400 | [diff] [blame^] | 60 | struct amdgpu_bo_va_hole *hole, *tmp; |
| 61 | LIST_FOR_EACH_ENTRY_SAFE(hole, tmp, &mgr->va_holes, list) { |
Ken Wang | 322d02d | 2015-05-21 17:21:21 +0800 | [diff] [blame] | 62 | list_del(&hole->list); |
| 63 | free(hole); |
| 64 | } |
| 65 | pthread_mutex_destroy(&mgr->bo_va_mutex); |
| 66 | } |
| 67 | |
Emil Velikov | b471818 | 2015-08-07 16:54:29 +0100 | [diff] [blame] | 68 | drm_private uint64_t |
| 69 | amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size, |
| 70 | uint64_t alignment, uint64_t base_required) |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 71 | { |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 72 | struct amdgpu_bo_va_hole *hole, *n; |
| 73 | uint64_t offset = 0, waste = 0; |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 74 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 75 | alignment = MAX2(alignment, mgr->va_alignment); |
| 76 | size = ALIGN(size, mgr->va_alignment); |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 77 | |
Ken Wang | 5b01908 | 2015-07-09 13:48:25 +0800 | [diff] [blame] | 78 | if (base_required % alignment) |
| 79 | return AMDGPU_INVALID_VA_ADDRESS; |
| 80 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 81 | pthread_mutex_lock(&mgr->bo_va_mutex); |
| 82 | /* TODO: using more appropriate way to track the holes */ |
| 83 | /* first look for a hole */ |
Jammy Zhou | 56d8dd6 | 2015-08-17 11:09:09 +0800 | [diff] [blame] | 84 | LIST_FOR_EACH_ENTRY_SAFE(hole, n, &mgr->va_holes, list) { |
Ken Wang | 5b01908 | 2015-07-09 13:48:25 +0800 | [diff] [blame] | 85 | 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.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 99 | } |
| 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 Wang | 5b01908 | 2015-07-09 13:48:25 +0800 | [diff] [blame] | 109 | n = calloc(1, sizeof(struct amdgpu_bo_va_hole)); |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 110 | 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 Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 125 | |
Ken Wang | 5b01908 | 2015-07-09 13:48:25 +0800 | [diff] [blame] | 126 | if (base_required) { |
Tom St Denis | 988f31e | 2015-10-09 10:36:04 -0400 | [diff] [blame] | 127 | if (base_required < mgr->va_offset) { |
| 128 | pthread_mutex_unlock(&mgr->bo_va_mutex); |
Ken Wang | 5b01908 | 2015-07-09 13:48:25 +0800 | [diff] [blame] | 129 | return AMDGPU_INVALID_VA_ADDRESS; |
Tom St Denis | 988f31e | 2015-10-09 10:36:04 -0400 | [diff] [blame] | 130 | } |
Ken Wang | 5b01908 | 2015-07-09 13:48:25 +0800 | [diff] [blame] | 131 | 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 Zhou | 241cf6d | 2015-05-13 01:14:11 +0800 | [diff] [blame] | 138 | |
| 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.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 144 | 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 Wang | 5b01908 | 2015-07-09 13:48:25 +0800 | [diff] [blame] | 150 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 151 | offset += waste; |
| 152 | mgr->va_offset += size + waste; |
| 153 | pthread_mutex_unlock(&mgr->bo_va_mutex); |
| 154 | return offset; |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Emil Velikov | b471818 | 2015-08-07 16:54:29 +0100 | [diff] [blame] | 157 | drm_private void |
| 158 | amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size) |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 159 | { |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 160 | struct amdgpu_bo_va_hole *hole; |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 161 | |
monk.liu | d3e7195 | 2015-05-13 14:01:53 +0800 | [diff] [blame] | 162 | if (va == AMDGPU_INVALID_VA_ADDRESS) |
| 163 | return; |
| 164 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 165 | size = ALIGN(size, mgr->va_alignment); |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 166 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 167 | 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 Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 181 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 182 | 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 Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 188 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 189 | 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 Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 205 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 206 | /* 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 Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 212 | |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 213 | /* 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 Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 223 | out: |
monk.liu | 9066acf | 2015-05-13 13:58:43 +0800 | [diff] [blame] | 224 | pthread_mutex_unlock(&mgr->bo_va_mutex); |
Alex Deucher | 0936139 | 2015-04-20 12:04:22 -0400 | [diff] [blame] | 225 | } |
Sabre Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 226 | |
| 227 | int 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 Zhou | 95d0f35 | 2015-07-16 10:29:58 +0800 | [diff] [blame] | 233 | amdgpu_va_handle *va_range_handle, |
| 234 | uint64_t flags) |
Sabre Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 235 | { |
Jammy Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 236 | struct amdgpu_bo_va_mgr *vamgr; |
Sabre Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 237 | |
Jammy Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 238 | 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 Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 247 | va_base_alignment, va_base_required); |
| 248 | |
Jammy Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 249 | 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 Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 257 | 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 Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 261 | amdgpu_vamgr_free_va(vamgr, *va_base_allocated, size); |
Sabre Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 262 | return -ENOMEM; |
| 263 | } |
| 264 | va->dev = dev; |
| 265 | va->address = *va_base_allocated; |
| 266 | va->size = size; |
| 267 | va->range = va_range_type; |
Jammy Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 268 | va->vamgr = vamgr; |
Sabre Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 269 | *va_range_handle = va; |
| 270 | } else { |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int amdgpu_va_range_free(amdgpu_va_handle va_range_handle) |
| 278 | { |
| 279 | if(!va_range_handle || !va_range_handle->address) |
| 280 | return 0; |
Jammy Zhou | ffa305d | 2015-08-17 11:09:08 +0800 | [diff] [blame] | 281 | |
| 282 | amdgpu_vamgr_free_va(va_range_handle->vamgr, |
| 283 | va_range_handle->address, |
Sabre Shao | 23fab59 | 2015-07-09 13:50:36 +0800 | [diff] [blame] | 284 | va_range_handle->size); |
| 285 | free(va_range_handle); |
| 286 | return 0; |
| 287 | } |