Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Jerome Glisse. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sub license, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 16 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 18 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 19 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | * |
| 21 | * The above copyright notice and this permission notice (including the |
| 22 | * next paragraph) shall be included in all copies or substantial portions |
| 23 | * of the Software. |
| 24 | * |
| 25 | */ |
| 26 | /* |
| 27 | * Authors: |
| 28 | * Jerome Glisse <glisse@freedesktop.org> |
| 29 | * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com> |
| 30 | * Dave Airlie |
| 31 | */ |
| 32 | #include <linux/list.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <drm/drmP.h> |
| 35 | #include <drm/amdgpu_drm.h> |
Oded Gabbay | a187f17 | 2016-01-30 07:59:34 +0200 | [diff] [blame] | 36 | #include <drm/drm_cache.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 37 | #include "amdgpu.h" |
| 38 | #include "amdgpu_trace.h" |
| 39 | |
| 40 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 41 | |
| 42 | static u64 amdgpu_get_vis_part_size(struct amdgpu_device *adev, |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 43 | struct ttm_mem_reg *mem) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 44 | { |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 45 | if (mem->start << PAGE_SHIFT >= adev->mc.visible_vram_size) |
| 46 | return 0; |
| 47 | |
| 48 | return ((mem->start << PAGE_SHIFT) + mem->size) > |
| 49 | adev->mc.visible_vram_size ? |
| 50 | adev->mc.visible_vram_size - (mem->start << PAGE_SHIFT) : |
| 51 | mem->size; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | static void amdgpu_update_memory_usage(struct amdgpu_device *adev, |
| 55 | struct ttm_mem_reg *old_mem, |
| 56 | struct ttm_mem_reg *new_mem) |
| 57 | { |
| 58 | u64 vis_size; |
| 59 | if (!adev) |
| 60 | return; |
| 61 | |
| 62 | if (new_mem) { |
| 63 | switch (new_mem->mem_type) { |
| 64 | case TTM_PL_TT: |
| 65 | atomic64_add(new_mem->size, &adev->gtt_usage); |
| 66 | break; |
| 67 | case TTM_PL_VRAM: |
| 68 | atomic64_add(new_mem->size, &adev->vram_usage); |
| 69 | vis_size = amdgpu_get_vis_part_size(adev, new_mem); |
| 70 | atomic64_add(vis_size, &adev->vram_vis_usage); |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (old_mem) { |
| 76 | switch (old_mem->mem_type) { |
| 77 | case TTM_PL_TT: |
| 78 | atomic64_sub(old_mem->size, &adev->gtt_usage); |
| 79 | break; |
| 80 | case TTM_PL_VRAM: |
| 81 | atomic64_sub(old_mem->size, &adev->vram_usage); |
| 82 | vis_size = amdgpu_get_vis_part_size(adev, old_mem); |
| 83 | atomic64_sub(vis_size, &adev->vram_vis_usage); |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo) |
| 90 | { |
| 91 | struct amdgpu_bo *bo; |
| 92 | |
| 93 | bo = container_of(tbo, struct amdgpu_bo, tbo); |
| 94 | |
| 95 | amdgpu_update_memory_usage(bo->adev, &bo->tbo.mem, NULL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 96 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 97 | drm_gem_object_release(&bo->gem_base); |
Christian König | 82b9c55 | 2015-11-27 16:49:00 +0100 | [diff] [blame] | 98 | amdgpu_bo_unref(&bo->parent); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 99 | if (!list_empty(&bo->shadow_list)) { |
| 100 | mutex_lock(&bo->adev->shadow_list_lock); |
| 101 | list_del_init(&bo->shadow_list); |
| 102 | mutex_unlock(&bo->adev->shadow_list_lock); |
| 103 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 104 | kfree(bo->metadata); |
| 105 | kfree(bo); |
| 106 | } |
| 107 | |
| 108 | bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo) |
| 109 | { |
| 110 | if (bo->destroy == &amdgpu_ttm_bo_destroy) |
| 111 | return true; |
| 112 | return false; |
| 113 | } |
| 114 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 115 | static void amdgpu_ttm_placement_init(struct amdgpu_device *adev, |
| 116 | struct ttm_placement *placement, |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 117 | struct ttm_place *places, |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 118 | u32 domain, u64 flags) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 119 | { |
Christian König | 6369f6f | 2016-08-15 14:08:54 +0200 | [diff] [blame] | 120 | u32 c = 0; |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 121 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 122 | if (domain & AMDGPU_GEM_DOMAIN_VRAM) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 123 | unsigned visible_pfn = adev->mc.visible_vram_size >> PAGE_SHIFT; |
Christian König | 56de55a | 2016-08-24 14:30:21 +0200 | [diff] [blame^] | 124 | unsigned lpfn = 0; |
| 125 | |
| 126 | /* This forces a reallocation if the flag wasn't set before */ |
| 127 | if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) |
| 128 | lpfn = adev->mc.real_vram_size >> PAGE_SHIFT; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 129 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 130 | if (flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS && |
Christian König | 6369f6f | 2016-08-15 14:08:54 +0200 | [diff] [blame] | 131 | !(flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) && |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 132 | adev->mc.visible_vram_size < adev->mc.real_vram_size) { |
| 133 | places[c].fpfn = visible_pfn; |
Christian König | 56de55a | 2016-08-24 14:30:21 +0200 | [diff] [blame^] | 134 | places[c].lpfn = lpfn; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 135 | places[c].flags = TTM_PL_FLAG_WC | |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 136 | TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_VRAM | |
| 137 | TTM_PL_FLAG_TOPDOWN; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 138 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 139 | } |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 140 | |
| 141 | places[c].fpfn = 0; |
Christian König | 56de55a | 2016-08-24 14:30:21 +0200 | [diff] [blame^] | 142 | places[c].lpfn = lpfn; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 143 | places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 144 | TTM_PL_FLAG_VRAM; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 145 | if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) |
| 146 | places[c].lpfn = visible_pfn; |
| 147 | else |
| 148 | places[c].flags |= TTM_PL_FLAG_TOPDOWN; |
| 149 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (domain & AMDGPU_GEM_DOMAIN_GTT) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 153 | places[c].fpfn = 0; |
| 154 | places[c].lpfn = 0; |
| 155 | places[c].flags = TTM_PL_FLAG_TT; |
| 156 | if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) |
| 157 | places[c].flags |= TTM_PL_FLAG_WC | |
| 158 | TTM_PL_FLAG_UNCACHED; |
| 159 | else |
| 160 | places[c].flags |= TTM_PL_FLAG_CACHED; |
| 161 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if (domain & AMDGPU_GEM_DOMAIN_CPU) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 165 | places[c].fpfn = 0; |
| 166 | places[c].lpfn = 0; |
| 167 | places[c].flags = TTM_PL_FLAG_SYSTEM; |
| 168 | if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) |
| 169 | places[c].flags |= TTM_PL_FLAG_WC | |
| 170 | TTM_PL_FLAG_UNCACHED; |
| 171 | else |
| 172 | places[c].flags |= TTM_PL_FLAG_CACHED; |
| 173 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | if (domain & AMDGPU_GEM_DOMAIN_GDS) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 177 | places[c].fpfn = 0; |
| 178 | places[c].lpfn = 0; |
| 179 | places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS; |
| 180 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 181 | } |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 182 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 183 | if (domain & AMDGPU_GEM_DOMAIN_GWS) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 184 | places[c].fpfn = 0; |
| 185 | places[c].lpfn = 0; |
| 186 | places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS; |
| 187 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 188 | } |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 189 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 190 | if (domain & AMDGPU_GEM_DOMAIN_OA) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 191 | places[c].fpfn = 0; |
| 192 | places[c].lpfn = 0; |
| 193 | places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA; |
| 194 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if (!c) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 198 | places[c].fpfn = 0; |
| 199 | places[c].lpfn = 0; |
| 200 | places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM; |
| 201 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 202 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 203 | |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 204 | placement->num_placement = c; |
| 205 | placement->placement = places; |
| 206 | |
| 207 | placement->num_busy_placement = c; |
| 208 | placement->busy_placement = places; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 211 | void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain) |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 212 | { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 213 | amdgpu_ttm_placement_init(abo->adev, &abo->placement, |
| 214 | abo->placements, domain, abo->flags); |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | static void amdgpu_fill_placement_to_bo(struct amdgpu_bo *bo, |
| 218 | struct ttm_placement *placement) |
| 219 | { |
| 220 | BUG_ON(placement->num_placement > (AMDGPU_GEM_DOMAIN_MAX + 1)); |
| 221 | |
| 222 | memcpy(bo->placements, placement->placement, |
| 223 | placement->num_placement * sizeof(struct ttm_place)); |
| 224 | bo->placement.num_placement = placement->num_placement; |
| 225 | bo->placement.num_busy_placement = placement->num_busy_placement; |
| 226 | bo->placement.placement = bo->placements; |
| 227 | bo->placement.busy_placement = bo->placements; |
| 228 | } |
| 229 | |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 230 | /** |
| 231 | * amdgpu_bo_create_kernel - create BO for kernel use |
| 232 | * |
| 233 | * @adev: amdgpu device object |
| 234 | * @size: size for the new BO |
| 235 | * @align: alignment for the new BO |
| 236 | * @domain: where to place it |
| 237 | * @bo_ptr: resulting BO |
| 238 | * @gpu_addr: GPU addr of the pinned BO |
| 239 | * @cpu_addr: optional CPU address mapping |
| 240 | * |
| 241 | * Allocates and pins a BO for kernel internal use. |
| 242 | * |
| 243 | * Returns 0 on success, negative error code otherwise. |
| 244 | */ |
| 245 | int amdgpu_bo_create_kernel(struct amdgpu_device *adev, |
| 246 | unsigned long size, int align, |
| 247 | u32 domain, struct amdgpu_bo **bo_ptr, |
| 248 | u64 *gpu_addr, void **cpu_addr) |
| 249 | { |
| 250 | int r; |
| 251 | |
| 252 | r = amdgpu_bo_create(adev, size, align, true, domain, |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 253 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED | |
| 254 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 255 | NULL, NULL, bo_ptr); |
| 256 | if (r) { |
| 257 | dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", r); |
| 258 | return r; |
| 259 | } |
| 260 | |
| 261 | r = amdgpu_bo_reserve(*bo_ptr, false); |
| 262 | if (r) { |
| 263 | dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r); |
| 264 | goto error_free; |
| 265 | } |
| 266 | |
| 267 | r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr); |
| 268 | if (r) { |
| 269 | dev_err(adev->dev, "(%d) kernel bo pin failed\n", r); |
| 270 | goto error_unreserve; |
| 271 | } |
| 272 | |
| 273 | if (cpu_addr) { |
| 274 | r = amdgpu_bo_kmap(*bo_ptr, cpu_addr); |
| 275 | if (r) { |
| 276 | dev_err(adev->dev, "(%d) kernel bo map failed\n", r); |
| 277 | goto error_unreserve; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | amdgpu_bo_unreserve(*bo_ptr); |
| 282 | |
| 283 | return 0; |
| 284 | |
| 285 | error_unreserve: |
| 286 | amdgpu_bo_unreserve(*bo_ptr); |
| 287 | |
| 288 | error_free: |
| 289 | amdgpu_bo_unref(bo_ptr); |
| 290 | |
| 291 | return r; |
| 292 | } |
| 293 | |
Junwei Zhang | aa1d562 | 2016-09-08 10:13:32 +0800 | [diff] [blame] | 294 | /** |
| 295 | * amdgpu_bo_free_kernel - free BO for kernel use |
| 296 | * |
| 297 | * @bo: amdgpu BO to free |
| 298 | * |
| 299 | * unmaps and unpin a BO for kernel internal use. |
| 300 | */ |
| 301 | void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr, |
| 302 | void **cpu_addr) |
| 303 | { |
| 304 | if (*bo == NULL) |
| 305 | return; |
| 306 | |
| 307 | if (likely(amdgpu_bo_reserve(*bo, false) == 0)) { |
| 308 | if (cpu_addr) |
| 309 | amdgpu_bo_kunmap(*bo); |
| 310 | |
| 311 | amdgpu_bo_unpin(*bo); |
| 312 | amdgpu_bo_unreserve(*bo); |
| 313 | } |
| 314 | amdgpu_bo_unref(bo); |
| 315 | |
| 316 | if (gpu_addr) |
| 317 | *gpu_addr = 0; |
| 318 | |
| 319 | if (cpu_addr) |
| 320 | *cpu_addr = NULL; |
| 321 | } |
| 322 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 323 | int amdgpu_bo_create_restricted(struct amdgpu_device *adev, |
| 324 | unsigned long size, int byte_align, |
| 325 | bool kernel, u32 domain, u64 flags, |
| 326 | struct sg_table *sg, |
| 327 | struct ttm_placement *placement, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 328 | struct reservation_object *resv, |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 329 | struct amdgpu_bo **bo_ptr) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 330 | { |
| 331 | struct amdgpu_bo *bo; |
| 332 | enum ttm_bo_type type; |
| 333 | unsigned long page_align; |
| 334 | size_t acc_size; |
| 335 | int r; |
| 336 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 337 | page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT; |
| 338 | size = ALIGN(size, PAGE_SIZE); |
| 339 | |
| 340 | if (kernel) { |
| 341 | type = ttm_bo_type_kernel; |
| 342 | } else if (sg) { |
| 343 | type = ttm_bo_type_sg; |
| 344 | } else { |
| 345 | type = ttm_bo_type_device; |
| 346 | } |
| 347 | *bo_ptr = NULL; |
| 348 | |
| 349 | acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size, |
| 350 | sizeof(struct amdgpu_bo)); |
| 351 | |
| 352 | bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL); |
| 353 | if (bo == NULL) |
| 354 | return -ENOMEM; |
| 355 | r = drm_gem_object_init(adev->ddev, &bo->gem_base, size); |
| 356 | if (unlikely(r)) { |
| 357 | kfree(bo); |
| 358 | return r; |
| 359 | } |
| 360 | bo->adev = adev; |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 361 | INIT_LIST_HEAD(&bo->shadow_list); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 362 | INIT_LIST_HEAD(&bo->va); |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 363 | bo->prefered_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM | |
| 364 | AMDGPU_GEM_DOMAIN_GTT | |
| 365 | AMDGPU_GEM_DOMAIN_CPU | |
| 366 | AMDGPU_GEM_DOMAIN_GDS | |
| 367 | AMDGPU_GEM_DOMAIN_GWS | |
| 368 | AMDGPU_GEM_DOMAIN_OA); |
| 369 | bo->allowed_domains = bo->prefered_domains; |
| 370 | if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM) |
| 371 | bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 372 | |
| 373 | bo->flags = flags; |
Oded Gabbay | a187f17 | 2016-01-30 07:59:34 +0200 | [diff] [blame] | 374 | |
| 375 | /* For architectures that don't support WC memory, |
| 376 | * mask out the WC flag from the BO |
| 377 | */ |
| 378 | if (!drm_arch_can_wc_memory()) |
| 379 | bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC; |
| 380 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 381 | amdgpu_fill_placement_to_bo(bo, placement); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 382 | /* Kernel allocation are uninterruptible */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 383 | r = ttm_bo_init(&adev->mman.bdev, &bo->tbo, size, type, |
| 384 | &bo->placement, page_align, !kernel, NULL, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 385 | acc_size, sg, resv, &amdgpu_ttm_bo_destroy); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 386 | if (unlikely(r != 0)) { |
| 387 | return r; |
| 388 | } |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 389 | |
| 390 | if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED && |
| 391 | bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) { |
| 392 | struct fence *fence; |
| 393 | |
| 394 | if (adev->mman.buffer_funcs_ring == NULL || |
| 395 | !adev->mman.buffer_funcs_ring->ready) { |
| 396 | r = -EBUSY; |
| 397 | goto fail_free; |
| 398 | } |
| 399 | |
| 400 | r = amdgpu_bo_reserve(bo, false); |
| 401 | if (unlikely(r != 0)) |
| 402 | goto fail_free; |
| 403 | |
| 404 | amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_VRAM); |
| 405 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); |
| 406 | if (unlikely(r != 0)) |
| 407 | goto fail_unreserve; |
| 408 | |
| 409 | amdgpu_fill_buffer(bo, 0, bo->tbo.resv, &fence); |
| 410 | amdgpu_bo_fence(bo, fence, false); |
| 411 | amdgpu_bo_unreserve(bo); |
| 412 | fence_put(bo->tbo.moving); |
| 413 | bo->tbo.moving = fence_get(fence); |
| 414 | fence_put(fence); |
| 415 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 416 | *bo_ptr = bo; |
| 417 | |
| 418 | trace_amdgpu_bo_create(bo); |
| 419 | |
| 420 | return 0; |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 421 | |
| 422 | fail_unreserve: |
| 423 | amdgpu_bo_unreserve(bo); |
| 424 | fail_free: |
| 425 | amdgpu_bo_unref(&bo); |
| 426 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 427 | } |
| 428 | |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 429 | static int amdgpu_bo_create_shadow(struct amdgpu_device *adev, |
| 430 | unsigned long size, int byte_align, |
| 431 | struct amdgpu_bo *bo) |
| 432 | { |
| 433 | struct ttm_placement placement = {0}; |
| 434 | struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1]; |
| 435 | int r; |
| 436 | |
| 437 | if (bo->shadow) |
| 438 | return 0; |
| 439 | |
| 440 | bo->flags |= AMDGPU_GEM_CREATE_SHADOW; |
| 441 | memset(&placements, 0, |
| 442 | (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place)); |
| 443 | |
| 444 | amdgpu_ttm_placement_init(adev, &placement, |
| 445 | placements, AMDGPU_GEM_DOMAIN_GTT, |
| 446 | AMDGPU_GEM_CREATE_CPU_GTT_USWC); |
| 447 | |
| 448 | r = amdgpu_bo_create_restricted(adev, size, byte_align, true, |
| 449 | AMDGPU_GEM_DOMAIN_GTT, |
| 450 | AMDGPU_GEM_CREATE_CPU_GTT_USWC, |
| 451 | NULL, &placement, |
| 452 | bo->tbo.resv, |
| 453 | &bo->shadow); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 454 | if (!r) { |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 455 | bo->shadow->parent = amdgpu_bo_ref(bo); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 456 | mutex_lock(&adev->shadow_list_lock); |
| 457 | list_add_tail(&bo->shadow_list, &adev->shadow_list); |
| 458 | mutex_unlock(&adev->shadow_list_lock); |
| 459 | } |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 460 | |
| 461 | return r; |
| 462 | } |
| 463 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 464 | int amdgpu_bo_create(struct amdgpu_device *adev, |
| 465 | unsigned long size, int byte_align, |
| 466 | bool kernel, u32 domain, u64 flags, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 467 | struct sg_table *sg, |
| 468 | struct reservation_object *resv, |
| 469 | struct amdgpu_bo **bo_ptr) |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 470 | { |
| 471 | struct ttm_placement placement = {0}; |
| 472 | struct ttm_place placements[AMDGPU_GEM_DOMAIN_MAX + 1]; |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 473 | int r; |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 474 | |
| 475 | memset(&placements, 0, |
| 476 | (AMDGPU_GEM_DOMAIN_MAX + 1) * sizeof(struct ttm_place)); |
| 477 | |
| 478 | amdgpu_ttm_placement_init(adev, &placement, |
| 479 | placements, domain, flags); |
| 480 | |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 481 | r = amdgpu_bo_create_restricted(adev, size, byte_align, kernel, |
| 482 | domain, flags, sg, &placement, |
| 483 | resv, bo_ptr); |
| 484 | if (r) |
| 485 | return r; |
| 486 | |
Chunming Zhou | 3ad81f1 | 2016-08-05 17:30:17 +0800 | [diff] [blame] | 487 | if (amdgpu_need_backup(adev) && (flags & AMDGPU_GEM_CREATE_SHADOW)) { |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 488 | r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr)); |
| 489 | if (r) |
| 490 | amdgpu_bo_unref(bo_ptr); |
| 491 | } |
| 492 | |
| 493 | return r; |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 494 | } |
| 495 | |
Chunming Zhou | 20f4eff | 2016-08-04 16:51:18 +0800 | [diff] [blame] | 496 | int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev, |
| 497 | struct amdgpu_ring *ring, |
| 498 | struct amdgpu_bo *bo, |
| 499 | struct reservation_object *resv, |
| 500 | struct fence **fence, |
| 501 | bool direct) |
| 502 | |
| 503 | { |
| 504 | struct amdgpu_bo *shadow = bo->shadow; |
| 505 | uint64_t bo_addr, shadow_addr; |
| 506 | int r; |
| 507 | |
| 508 | if (!shadow) |
| 509 | return -EINVAL; |
| 510 | |
| 511 | bo_addr = amdgpu_bo_gpu_offset(bo); |
| 512 | shadow_addr = amdgpu_bo_gpu_offset(bo->shadow); |
| 513 | |
| 514 | r = reservation_object_reserve_shared(bo->tbo.resv); |
| 515 | if (r) |
| 516 | goto err; |
| 517 | |
| 518 | r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr, |
| 519 | amdgpu_bo_size(bo), resv, fence, |
| 520 | direct); |
| 521 | if (!r) |
| 522 | amdgpu_bo_fence(bo, *fence, true); |
| 523 | |
| 524 | err: |
| 525 | return r; |
| 526 | } |
| 527 | |
| 528 | int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev, |
| 529 | struct amdgpu_ring *ring, |
| 530 | struct amdgpu_bo *bo, |
| 531 | struct reservation_object *resv, |
| 532 | struct fence **fence, |
| 533 | bool direct) |
| 534 | |
| 535 | { |
| 536 | struct amdgpu_bo *shadow = bo->shadow; |
| 537 | uint64_t bo_addr, shadow_addr; |
| 538 | int r; |
| 539 | |
| 540 | if (!shadow) |
| 541 | return -EINVAL; |
| 542 | |
| 543 | bo_addr = amdgpu_bo_gpu_offset(bo); |
| 544 | shadow_addr = amdgpu_bo_gpu_offset(bo->shadow); |
| 545 | |
| 546 | r = reservation_object_reserve_shared(bo->tbo.resv); |
| 547 | if (r) |
| 548 | goto err; |
| 549 | |
| 550 | r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr, |
| 551 | amdgpu_bo_size(bo), resv, fence, |
| 552 | direct); |
| 553 | if (!r) |
| 554 | amdgpu_bo_fence(bo, *fence, true); |
| 555 | |
| 556 | err: |
| 557 | return r; |
| 558 | } |
| 559 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 560 | int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr) |
| 561 | { |
| 562 | bool is_iomem; |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 563 | long r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 564 | |
Christian König | 271c812 | 2015-05-13 14:30:53 +0200 | [diff] [blame] | 565 | if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) |
| 566 | return -EPERM; |
| 567 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 568 | if (bo->kptr) { |
| 569 | if (ptr) { |
| 570 | *ptr = bo->kptr; |
| 571 | } |
| 572 | return 0; |
| 573 | } |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 574 | |
| 575 | r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false, |
| 576 | MAX_SCHEDULE_TIMEOUT); |
| 577 | if (r < 0) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 578 | return r; |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 579 | |
| 580 | r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap); |
| 581 | if (r) |
| 582 | return r; |
| 583 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 584 | bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 585 | if (ptr) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 586 | *ptr = bo->kptr; |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 587 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | void amdgpu_bo_kunmap(struct amdgpu_bo *bo) |
| 592 | { |
| 593 | if (bo->kptr == NULL) |
| 594 | return; |
| 595 | bo->kptr = NULL; |
| 596 | ttm_bo_kunmap(&bo->kmap); |
| 597 | } |
| 598 | |
| 599 | struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo) |
| 600 | { |
| 601 | if (bo == NULL) |
| 602 | return NULL; |
| 603 | |
| 604 | ttm_bo_reference(&bo->tbo); |
| 605 | return bo; |
| 606 | } |
| 607 | |
| 608 | void amdgpu_bo_unref(struct amdgpu_bo **bo) |
| 609 | { |
| 610 | struct ttm_buffer_object *tbo; |
| 611 | |
| 612 | if ((*bo) == NULL) |
| 613 | return; |
| 614 | |
| 615 | tbo = &((*bo)->tbo); |
| 616 | ttm_bo_unref(&tbo); |
| 617 | if (tbo == NULL) |
| 618 | *bo = NULL; |
| 619 | } |
| 620 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 621 | int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain, |
| 622 | u64 min_offset, u64 max_offset, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 623 | u64 *gpu_addr) |
| 624 | { |
| 625 | int r, i; |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 626 | unsigned fpfn, lpfn; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 627 | |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 628 | if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 629 | return -EPERM; |
| 630 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 631 | if (WARN_ON_ONCE(min_offset > max_offset)) |
| 632 | return -EINVAL; |
| 633 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 634 | if (bo->pin_count) { |
Flora Cui | 408778e | 2016-08-18 12:55:13 +0800 | [diff] [blame] | 635 | uint32_t mem_type = bo->tbo.mem.mem_type; |
| 636 | |
| 637 | if (domain != amdgpu_mem_type_to_domain(mem_type)) |
| 638 | return -EINVAL; |
| 639 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 640 | bo->pin_count++; |
| 641 | if (gpu_addr) |
| 642 | *gpu_addr = amdgpu_bo_gpu_offset(bo); |
| 643 | |
| 644 | if (max_offset != 0) { |
Flora Cui | 27798e0 | 2016-08-18 13:18:09 +0800 | [diff] [blame] | 645 | u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 646 | WARN_ON_ONCE(max_offset < |
| 647 | (amdgpu_bo_gpu_offset(bo) - domain_start)); |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 652 | |
| 653 | bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 654 | amdgpu_ttm_placement_from_domain(bo, domain); |
| 655 | for (i = 0; i < bo->placement.num_placement; i++) { |
| 656 | /* force to pin into visible video ram */ |
| 657 | if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) && |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 658 | !(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) && |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 659 | (!max_offset || max_offset > |
| 660 | bo->adev->mc.visible_vram_size)) { |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 661 | if (WARN_ON_ONCE(min_offset > |
| 662 | bo->adev->mc.visible_vram_size)) |
| 663 | return -EINVAL; |
| 664 | fpfn = min_offset >> PAGE_SHIFT; |
| 665 | lpfn = bo->adev->mc.visible_vram_size >> PAGE_SHIFT; |
| 666 | } else { |
| 667 | fpfn = min_offset >> PAGE_SHIFT; |
| 668 | lpfn = max_offset >> PAGE_SHIFT; |
| 669 | } |
| 670 | if (fpfn > bo->placements[i].fpfn) |
| 671 | bo->placements[i].fpfn = fpfn; |
Christian König | 78d0e18 | 2016-01-19 12:48:14 +0100 | [diff] [blame] | 672 | if (!bo->placements[i].lpfn || |
| 673 | (lpfn && lpfn < bo->placements[i].lpfn)) |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 674 | bo->placements[i].lpfn = lpfn; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 675 | bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT; |
| 676 | } |
| 677 | |
| 678 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 679 | if (unlikely(r)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 680 | dev_err(bo->adev->dev, "%p pin failed\n", bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 681 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 682 | } |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 683 | r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem); |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 684 | if (unlikely(r)) { |
| 685 | dev_err(bo->adev->dev, "%p bind failed\n", bo); |
| 686 | goto error; |
| 687 | } |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 688 | |
| 689 | bo->pin_count = 1; |
| 690 | if (gpu_addr != NULL) |
| 691 | *gpu_addr = amdgpu_bo_gpu_offset(bo); |
| 692 | if (domain == AMDGPU_GEM_DOMAIN_VRAM) { |
| 693 | bo->adev->vram_pin_size += amdgpu_bo_size(bo); |
| 694 | if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) |
| 695 | bo->adev->invisible_pin_size += amdgpu_bo_size(bo); |
Flora Cui | 32ab75f | 2016-08-18 13:17:07 +0800 | [diff] [blame] | 696 | } else if (domain == AMDGPU_GEM_DOMAIN_GTT) { |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 697 | bo->adev->gart_pin_size += amdgpu_bo_size(bo); |
| 698 | } |
| 699 | |
| 700 | error: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 701 | return r; |
| 702 | } |
| 703 | |
| 704 | int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr) |
| 705 | { |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 706 | return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | int amdgpu_bo_unpin(struct amdgpu_bo *bo) |
| 710 | { |
| 711 | int r, i; |
| 712 | |
| 713 | if (!bo->pin_count) { |
| 714 | dev_warn(bo->adev->dev, "%p unpin not necessary\n", bo); |
| 715 | return 0; |
| 716 | } |
| 717 | bo->pin_count--; |
| 718 | if (bo->pin_count) |
| 719 | return 0; |
| 720 | for (i = 0; i < bo->placement.num_placement; i++) { |
| 721 | bo->placements[i].lpfn = 0; |
| 722 | bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT; |
| 723 | } |
| 724 | r = ttm_bo_validate(&bo->tbo, &bo->placement, false, false); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 725 | if (unlikely(r)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 726 | dev_err(bo->adev->dev, "%p validate failed for unpin\n", bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 727 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 728 | } |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 729 | |
| 730 | if (bo->tbo.mem.mem_type == TTM_PL_VRAM) { |
| 731 | bo->adev->vram_pin_size -= amdgpu_bo_size(bo); |
| 732 | if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) |
| 733 | bo->adev->invisible_pin_size -= amdgpu_bo_size(bo); |
Flora Cui | 441f90e | 2016-09-09 14:15:30 +0800 | [diff] [blame] | 734 | } else if (bo->tbo.mem.mem_type == TTM_PL_TT) { |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 735 | bo->adev->gart_pin_size -= amdgpu_bo_size(bo); |
| 736 | } |
| 737 | |
| 738 | error: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 739 | return r; |
| 740 | } |
| 741 | |
| 742 | int amdgpu_bo_evict_vram(struct amdgpu_device *adev) |
| 743 | { |
| 744 | /* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */ |
Jammy Zhou | 2f7d10b | 2015-07-22 11:29:01 +0800 | [diff] [blame] | 745 | if (0 && (adev->flags & AMD_IS_APU)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 746 | /* Useless to evict on IGP chips */ |
| 747 | return 0; |
| 748 | } |
| 749 | return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM); |
| 750 | } |
| 751 | |
Alex Deucher | 1f8628c | 2016-03-31 16:56:22 -0400 | [diff] [blame] | 752 | static const char *amdgpu_vram_names[] = { |
| 753 | "UNKNOWN", |
| 754 | "GDDR1", |
| 755 | "DDR2", |
| 756 | "GDDR3", |
| 757 | "GDDR4", |
| 758 | "GDDR5", |
| 759 | "HBM", |
| 760 | "DDR3" |
| 761 | }; |
| 762 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 763 | int amdgpu_bo_init(struct amdgpu_device *adev) |
| 764 | { |
| 765 | /* Add an MTRR for the VRAM */ |
| 766 | adev->mc.vram_mtrr = arch_phys_wc_add(adev->mc.aper_base, |
| 767 | adev->mc.aper_size); |
| 768 | DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n", |
| 769 | adev->mc.mc_vram_size >> 20, |
| 770 | (unsigned long long)adev->mc.aper_size >> 20); |
Alex Deucher | 1f8628c | 2016-03-31 16:56:22 -0400 | [diff] [blame] | 771 | DRM_INFO("RAM width %dbits %s\n", |
| 772 | adev->mc.vram_width, amdgpu_vram_names[adev->mc.vram_type]); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 773 | return amdgpu_ttm_init(adev); |
| 774 | } |
| 775 | |
| 776 | void amdgpu_bo_fini(struct amdgpu_device *adev) |
| 777 | { |
| 778 | amdgpu_ttm_fini(adev); |
| 779 | arch_phys_wc_del(adev->mc.vram_mtrr); |
| 780 | } |
| 781 | |
| 782 | int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo, |
| 783 | struct vm_area_struct *vma) |
| 784 | { |
| 785 | return ttm_fbdev_mmap(vma, &bo->tbo); |
| 786 | } |
| 787 | |
| 788 | int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags) |
| 789 | { |
Marek Olšák | fbd76d5 | 2015-05-14 23:48:26 +0200 | [diff] [blame] | 790 | if (AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 791 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 792 | |
| 793 | bo->tiling_flags = tiling_flags; |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags) |
| 798 | { |
| 799 | lockdep_assert_held(&bo->tbo.resv->lock.base); |
| 800 | |
| 801 | if (tiling_flags) |
| 802 | *tiling_flags = bo->tiling_flags; |
| 803 | } |
| 804 | |
| 805 | int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata, |
| 806 | uint32_t metadata_size, uint64_t flags) |
| 807 | { |
| 808 | void *buffer; |
| 809 | |
| 810 | if (!metadata_size) { |
| 811 | if (bo->metadata_size) { |
| 812 | kfree(bo->metadata); |
Dave Airlie | 0092d3e | 2016-05-03 12:44:29 +1000 | [diff] [blame] | 813 | bo->metadata = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 814 | bo->metadata_size = 0; |
| 815 | } |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | if (metadata == NULL) |
| 820 | return -EINVAL; |
| 821 | |
Andrzej Hajda | 71affda | 2015-09-21 17:34:39 -0400 | [diff] [blame] | 822 | buffer = kmemdup(metadata, metadata_size, GFP_KERNEL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 823 | if (buffer == NULL) |
| 824 | return -ENOMEM; |
| 825 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 826 | kfree(bo->metadata); |
| 827 | bo->metadata_flags = flags; |
| 828 | bo->metadata = buffer; |
| 829 | bo->metadata_size = metadata_size; |
| 830 | |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer, |
| 835 | size_t buffer_size, uint32_t *metadata_size, |
| 836 | uint64_t *flags) |
| 837 | { |
| 838 | if (!buffer && !metadata_size) |
| 839 | return -EINVAL; |
| 840 | |
| 841 | if (buffer) { |
| 842 | if (buffer_size < bo->metadata_size) |
| 843 | return -EINVAL; |
| 844 | |
| 845 | if (bo->metadata_size) |
| 846 | memcpy(buffer, bo->metadata, bo->metadata_size); |
| 847 | } |
| 848 | |
| 849 | if (metadata_size) |
| 850 | *metadata_size = bo->metadata_size; |
| 851 | if (flags) |
| 852 | *flags = bo->metadata_flags; |
| 853 | |
| 854 | return 0; |
| 855 | } |
| 856 | |
| 857 | void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, |
| 858 | struct ttm_mem_reg *new_mem) |
| 859 | { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 860 | struct amdgpu_bo *abo; |
David Mao | 15da301 | 2016-06-07 17:48:52 +0800 | [diff] [blame] | 861 | struct ttm_mem_reg *old_mem = &bo->mem; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 862 | |
| 863 | if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) |
| 864 | return; |
| 865 | |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 866 | abo = container_of(bo, struct amdgpu_bo, tbo); |
| 867 | amdgpu_vm_bo_invalidate(abo->adev, abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 868 | |
| 869 | /* update statistics */ |
| 870 | if (!new_mem) |
| 871 | return; |
| 872 | |
| 873 | /* move_notify is called before move happens */ |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 874 | amdgpu_update_memory_usage(abo->adev, &bo->mem, new_mem); |
David Mao | 15da301 | 2016-06-07 17:48:52 +0800 | [diff] [blame] | 875 | |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 876 | trace_amdgpu_ttm_bo_move(abo, new_mem->mem_type, old_mem->mem_type); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo) |
| 880 | { |
| 881 | struct amdgpu_device *adev; |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 882 | struct amdgpu_bo *abo; |
| 883 | unsigned long offset, size, lpfn; |
| 884 | int i, r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 885 | |
| 886 | if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) |
| 887 | return 0; |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 888 | |
| 889 | abo = container_of(bo, struct amdgpu_bo, tbo); |
| 890 | adev = abo->adev; |
| 891 | if (bo->mem.mem_type != TTM_PL_VRAM) |
| 892 | return 0; |
| 893 | |
| 894 | size = bo->mem.num_pages << PAGE_SHIFT; |
| 895 | offset = bo->mem.start << PAGE_SHIFT; |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 896 | /* TODO: figure out how to map scattered VRAM to the CPU */ |
| 897 | if ((offset + size) <= adev->mc.visible_vram_size && |
| 898 | (abo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)) |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 899 | return 0; |
| 900 | |
Michel Dänzer | 104ece9 | 2016-03-28 12:53:02 +0900 | [diff] [blame] | 901 | /* Can't move a pinned BO to visible VRAM */ |
| 902 | if (abo->pin_count > 0) |
| 903 | return -EINVAL; |
| 904 | |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 905 | /* hurrah the memory is not visible ! */ |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 906 | abo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 907 | amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM); |
| 908 | lpfn = adev->mc.visible_vram_size >> PAGE_SHIFT; |
| 909 | for (i = 0; i < abo->placement.num_placement; i++) { |
| 910 | /* Force into visible VRAM */ |
| 911 | if ((abo->placements[i].flags & TTM_PL_FLAG_VRAM) && |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 912 | (!abo->placements[i].lpfn || |
| 913 | abo->placements[i].lpfn > lpfn)) |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 914 | abo->placements[i].lpfn = lpfn; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 915 | } |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 916 | r = ttm_bo_validate(bo, &abo->placement, false, false); |
| 917 | if (unlikely(r == -ENOMEM)) { |
| 918 | amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT); |
| 919 | return ttm_bo_validate(bo, &abo->placement, false, false); |
| 920 | } else if (unlikely(r != 0)) { |
| 921 | return r; |
| 922 | } |
| 923 | |
| 924 | offset = bo->mem.start << PAGE_SHIFT; |
| 925 | /* this should never happen */ |
| 926 | if ((offset + size) > adev->mc.visible_vram_size) |
| 927 | return -EINVAL; |
| 928 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * amdgpu_bo_fence - add fence to buffer object |
| 934 | * |
| 935 | * @bo: buffer object in question |
| 936 | * @fence: fence to add |
| 937 | * @shared: true if fence should be added shared |
| 938 | * |
| 939 | */ |
Chunming Zhou | e40a311 | 2015-08-03 11:38:09 +0800 | [diff] [blame] | 940 | void amdgpu_bo_fence(struct amdgpu_bo *bo, struct fence *fence, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 941 | bool shared) |
| 942 | { |
| 943 | struct reservation_object *resv = bo->tbo.resv; |
| 944 | |
| 945 | if (shared) |
Chunming Zhou | e40a311 | 2015-08-03 11:38:09 +0800 | [diff] [blame] | 946 | reservation_object_add_shared_fence(resv, fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 947 | else |
Chunming Zhou | e40a311 | 2015-08-03 11:38:09 +0800 | [diff] [blame] | 948 | reservation_object_add_excl_fence(resv, fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 949 | } |
Christian König | cdb7e8f | 2016-07-25 17:56:18 +0200 | [diff] [blame] | 950 | |
| 951 | /** |
| 952 | * amdgpu_bo_gpu_offset - return GPU offset of bo |
| 953 | * @bo: amdgpu object for which we query the offset |
| 954 | * |
| 955 | * Returns current GPU offset of the object. |
| 956 | * |
| 957 | * Note: object should either be pinned or reserved when calling this |
| 958 | * function, it might be useful to add check for this for debugging. |
| 959 | */ |
| 960 | u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo) |
| 961 | { |
| 962 | WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM); |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 963 | WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT && |
| 964 | !amdgpu_ttm_is_bound(bo->tbo.ttm)); |
Christian König | cdb7e8f | 2016-07-25 17:56:18 +0200 | [diff] [blame] | 965 | WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) && |
| 966 | !bo->pin_count); |
Christian König | 9702d40 | 2016-09-07 15:10:44 +0200 | [diff] [blame] | 967 | WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET); |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 968 | WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM && |
| 969 | !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)); |
Christian König | cdb7e8f | 2016-07-25 17:56:18 +0200 | [diff] [blame] | 970 | |
| 971 | return bo->tbo.offset; |
| 972 | } |