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