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 | |
Alex Deucher | 6b8f4ee | 2017-12-15 16:45:02 -0500 | [diff] [blame] | 40 | static bool amdgpu_need_backup(struct amdgpu_device *adev) |
| 41 | { |
| 42 | if (adev->flags & AMD_IS_APU) |
| 43 | return false; |
| 44 | |
Christian König | 4f4b94e | 2017-12-20 14:21:25 +0100 | [diff] [blame] | 45 | if (amdgpu_gpu_recovery == 0 || |
| 46 | (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev))) |
| 47 | return false; |
| 48 | |
| 49 | return true; |
Alex Deucher | 6b8f4ee | 2017-12-15 16:45:02 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 52 | static void amdgpu_ttm_bo_destroy(struct ttm_buffer_object *tbo) |
| 53 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 54 | struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev); |
Andres Rodriguez | b82485f | 2017-09-15 21:05:19 -0400 | [diff] [blame] | 55 | struct amdgpu_bo *bo = ttm_to_amdgpu_bo(tbo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 56 | |
Christian König | 6375bbb | 2017-07-11 17:25:49 +0200 | [diff] [blame] | 57 | amdgpu_bo_kunmap(bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 58 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 59 | drm_gem_object_release(&bo->gem_base); |
Christian König | 82b9c55 | 2015-11-27 16:49:00 +0100 | [diff] [blame] | 60 | amdgpu_bo_unref(&bo->parent); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 61 | if (!list_empty(&bo->shadow_list)) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 62 | mutex_lock(&adev->shadow_list_lock); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 63 | list_del_init(&bo->shadow_list); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 64 | mutex_unlock(&adev->shadow_list_lock); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 65 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 66 | kfree(bo->metadata); |
| 67 | kfree(bo); |
| 68 | } |
| 69 | |
| 70 | bool amdgpu_ttm_bo_is_amdgpu_bo(struct ttm_buffer_object *bo) |
| 71 | { |
| 72 | if (bo->destroy == &amdgpu_ttm_bo_destroy) |
| 73 | return true; |
| 74 | return false; |
| 75 | } |
| 76 | |
Christian König | c09312a | 2017-09-12 10:56:17 +0200 | [diff] [blame] | 77 | void amdgpu_ttm_placement_from_domain(struct amdgpu_bo *abo, u32 domain) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 78 | { |
Christian König | c09312a | 2017-09-12 10:56:17 +0200 | [diff] [blame] | 79 | struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev); |
| 80 | struct ttm_placement *placement = &abo->placement; |
| 81 | struct ttm_place *places = abo->placements; |
| 82 | u64 flags = abo->flags; |
Christian König | 6369f6f | 2016-08-15 14:08:54 +0200 | [diff] [blame] | 83 | u32 c = 0; |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 84 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 85 | if (domain & AMDGPU_GEM_DOMAIN_VRAM) { |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 86 | unsigned visible_pfn = adev->gmc.visible_vram_size >> PAGE_SHIFT; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 87 | |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 88 | places[c].fpfn = 0; |
Christian König | 89bb575 | 2017-03-29 13:41:57 +0200 | [diff] [blame] | 89 | places[c].lpfn = 0; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 90 | places[c].flags = TTM_PL_FLAG_WC | TTM_PL_FLAG_UNCACHED | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 91 | TTM_PL_FLAG_VRAM; |
Christian König | 89bb575 | 2017-03-29 13:41:57 +0200 | [diff] [blame] | 92 | |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 93 | if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) |
| 94 | places[c].lpfn = visible_pfn; |
| 95 | else |
| 96 | places[c].flags |= TTM_PL_FLAG_TOPDOWN; |
Christian König | 89bb575 | 2017-03-29 13:41:57 +0200 | [diff] [blame] | 97 | |
| 98 | if (flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS) |
| 99 | places[c].flags |= TTM_PL_FLAG_CONTIGUOUS; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 100 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | if (domain & AMDGPU_GEM_DOMAIN_GTT) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 104 | places[c].fpfn = 0; |
Christian König | cf273a5 | 2017-08-18 15:50:17 +0200 | [diff] [blame] | 105 | if (flags & AMDGPU_GEM_CREATE_SHADOW) |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 106 | places[c].lpfn = adev->gmc.gart_size >> PAGE_SHIFT; |
Christian König | cf273a5 | 2017-08-18 15:50:17 +0200 | [diff] [blame] | 107 | else |
| 108 | places[c].lpfn = 0; |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 109 | places[c].flags = TTM_PL_FLAG_TT; |
| 110 | if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) |
| 111 | places[c].flags |= TTM_PL_FLAG_WC | |
| 112 | TTM_PL_FLAG_UNCACHED; |
| 113 | else |
| 114 | places[c].flags |= TTM_PL_FLAG_CACHED; |
| 115 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if (domain & AMDGPU_GEM_DOMAIN_CPU) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 119 | places[c].fpfn = 0; |
| 120 | places[c].lpfn = 0; |
| 121 | places[c].flags = TTM_PL_FLAG_SYSTEM; |
| 122 | if (flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) |
| 123 | places[c].flags |= TTM_PL_FLAG_WC | |
| 124 | TTM_PL_FLAG_UNCACHED; |
| 125 | else |
| 126 | places[c].flags |= TTM_PL_FLAG_CACHED; |
| 127 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (domain & AMDGPU_GEM_DOMAIN_GDS) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 131 | places[c].fpfn = 0; |
| 132 | places[c].lpfn = 0; |
| 133 | places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GDS; |
| 134 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 135 | } |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 136 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 137 | if (domain & AMDGPU_GEM_DOMAIN_GWS) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 138 | places[c].fpfn = 0; |
| 139 | places[c].lpfn = 0; |
| 140 | places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_GWS; |
| 141 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 142 | } |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 143 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 144 | if (domain & AMDGPU_GEM_DOMAIN_OA) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 145 | places[c].fpfn = 0; |
| 146 | places[c].lpfn = 0; |
| 147 | places[c].flags = TTM_PL_FLAG_UNCACHED | AMDGPU_PL_FLAG_OA; |
| 148 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (!c) { |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 152 | places[c].fpfn = 0; |
| 153 | places[c].lpfn = 0; |
| 154 | places[c].flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM; |
| 155 | c++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 156 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 157 | |
Christian König | faceaf6 | 2016-08-15 14:06:50 +0200 | [diff] [blame] | 158 | placement->num_placement = c; |
| 159 | placement->placement = places; |
| 160 | |
| 161 | placement->num_busy_placement = c; |
| 162 | placement->busy_placement = places; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 165 | /** |
Christian König | 9d903cb | 2017-07-27 17:08:54 +0200 | [diff] [blame] | 166 | * amdgpu_bo_create_reserved - create reserved BO for kernel use |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 167 | * |
| 168 | * @adev: amdgpu device object |
| 169 | * @size: size for the new BO |
| 170 | * @align: alignment for the new BO |
| 171 | * @domain: where to place it |
| 172 | * @bo_ptr: resulting BO |
| 173 | * @gpu_addr: GPU addr of the pinned BO |
| 174 | * @cpu_addr: optional CPU address mapping |
| 175 | * |
Christian König | 9d903cb | 2017-07-27 17:08:54 +0200 | [diff] [blame] | 176 | * Allocates and pins a BO for kernel internal use, and returns it still |
| 177 | * reserved. |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 178 | * |
| 179 | * Returns 0 on success, negative error code otherwise. |
| 180 | */ |
Christian König | 9d903cb | 2017-07-27 17:08:54 +0200 | [diff] [blame] | 181 | int amdgpu_bo_create_reserved(struct amdgpu_device *adev, |
| 182 | unsigned long size, int align, |
| 183 | u32 domain, struct amdgpu_bo **bo_ptr, |
| 184 | u64 *gpu_addr, void **cpu_addr) |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 185 | { |
Christian König | 53766e5 | 2017-07-27 14:52:53 +0200 | [diff] [blame] | 186 | bool free = false; |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 187 | int r; |
| 188 | |
Christian König | 53766e5 | 2017-07-27 14:52:53 +0200 | [diff] [blame] | 189 | if (!*bo_ptr) { |
| 190 | r = amdgpu_bo_create(adev, size, align, true, domain, |
| 191 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED | |
| 192 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, |
Yong Zhao | 2046d46 | 2017-07-20 18:49:09 -0400 | [diff] [blame] | 193 | NULL, NULL, 0, bo_ptr); |
Christian König | 53766e5 | 2017-07-27 14:52:53 +0200 | [diff] [blame] | 194 | if (r) { |
| 195 | dev_err(adev->dev, "(%d) failed to allocate kernel bo\n", |
| 196 | r); |
| 197 | return r; |
| 198 | } |
| 199 | free = true; |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | r = amdgpu_bo_reserve(*bo_ptr, false); |
| 203 | if (r) { |
| 204 | dev_err(adev->dev, "(%d) failed to reserve kernel bo\n", r); |
| 205 | goto error_free; |
| 206 | } |
| 207 | |
| 208 | r = amdgpu_bo_pin(*bo_ptr, domain, gpu_addr); |
| 209 | if (r) { |
| 210 | dev_err(adev->dev, "(%d) kernel bo pin failed\n", r); |
| 211 | goto error_unreserve; |
| 212 | } |
| 213 | |
| 214 | if (cpu_addr) { |
| 215 | r = amdgpu_bo_kmap(*bo_ptr, cpu_addr); |
| 216 | if (r) { |
| 217 | dev_err(adev->dev, "(%d) kernel bo map failed\n", r); |
| 218 | goto error_unreserve; |
| 219 | } |
| 220 | } |
| 221 | |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 222 | return 0; |
| 223 | |
| 224 | error_unreserve: |
| 225 | amdgpu_bo_unreserve(*bo_ptr); |
| 226 | |
| 227 | error_free: |
Christian König | 53766e5 | 2017-07-27 14:52:53 +0200 | [diff] [blame] | 228 | if (free) |
| 229 | amdgpu_bo_unref(bo_ptr); |
Christian König | 7c20488 | 2015-12-14 13:18:01 +0100 | [diff] [blame] | 230 | |
| 231 | return r; |
| 232 | } |
| 233 | |
Junwei Zhang | aa1d562 | 2016-09-08 10:13:32 +0800 | [diff] [blame] | 234 | /** |
Christian König | 9d903cb | 2017-07-27 17:08:54 +0200 | [diff] [blame] | 235 | * amdgpu_bo_create_kernel - create BO for kernel use |
| 236 | * |
| 237 | * @adev: amdgpu device object |
| 238 | * @size: size for the new BO |
| 239 | * @align: alignment for the new BO |
| 240 | * @domain: where to place it |
| 241 | * @bo_ptr: resulting BO |
| 242 | * @gpu_addr: GPU addr of the pinned BO |
| 243 | * @cpu_addr: optional CPU address mapping |
| 244 | * |
| 245 | * Allocates and pins a BO for kernel internal use. |
| 246 | * |
| 247 | * Returns 0 on success, negative error code otherwise. |
| 248 | */ |
| 249 | int amdgpu_bo_create_kernel(struct amdgpu_device *adev, |
| 250 | unsigned long size, int align, |
| 251 | u32 domain, struct amdgpu_bo **bo_ptr, |
| 252 | u64 *gpu_addr, void **cpu_addr) |
| 253 | { |
| 254 | int r; |
| 255 | |
| 256 | r = amdgpu_bo_create_reserved(adev, size, align, domain, bo_ptr, |
| 257 | gpu_addr, cpu_addr); |
| 258 | |
| 259 | if (r) |
| 260 | return r; |
| 261 | |
| 262 | amdgpu_bo_unreserve(*bo_ptr); |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /** |
Junwei Zhang | aa1d562 | 2016-09-08 10:13:32 +0800 | [diff] [blame] | 268 | * amdgpu_bo_free_kernel - free BO for kernel use |
| 269 | * |
| 270 | * @bo: amdgpu BO to free |
| 271 | * |
| 272 | * unmaps and unpin a BO for kernel internal use. |
| 273 | */ |
| 274 | void amdgpu_bo_free_kernel(struct amdgpu_bo **bo, u64 *gpu_addr, |
| 275 | void **cpu_addr) |
| 276 | { |
| 277 | if (*bo == NULL) |
| 278 | return; |
| 279 | |
Alex Xie | f3aa745 | 2017-04-24 14:27:00 -0400 | [diff] [blame] | 280 | if (likely(amdgpu_bo_reserve(*bo, true) == 0)) { |
Junwei Zhang | aa1d562 | 2016-09-08 10:13:32 +0800 | [diff] [blame] | 281 | if (cpu_addr) |
| 282 | amdgpu_bo_kunmap(*bo); |
| 283 | |
| 284 | amdgpu_bo_unpin(*bo); |
| 285 | amdgpu_bo_unreserve(*bo); |
| 286 | } |
| 287 | amdgpu_bo_unref(bo); |
| 288 | |
| 289 | if (gpu_addr) |
| 290 | *gpu_addr = 0; |
| 291 | |
| 292 | if (cpu_addr) |
| 293 | *cpu_addr = NULL; |
| 294 | } |
| 295 | |
Andrey Grodzovsky | 79c6312 | 2017-11-10 18:35:56 -0500 | [diff] [blame] | 296 | /* Validate bo size is bit bigger then the request domain */ |
| 297 | static bool amdgpu_bo_validate_size(struct amdgpu_device *adev, |
| 298 | unsigned long size, u32 domain) |
| 299 | { |
| 300 | struct ttm_mem_type_manager *man = NULL; |
| 301 | |
| 302 | /* |
| 303 | * If GTT is part of requested domains the check must succeed to |
| 304 | * allow fall back to GTT |
| 305 | */ |
| 306 | if (domain & AMDGPU_GEM_DOMAIN_GTT) { |
| 307 | man = &adev->mman.bdev.man[TTM_PL_TT]; |
| 308 | |
| 309 | if (size < (man->size << PAGE_SHIFT)) |
| 310 | return true; |
| 311 | else |
| 312 | goto fail; |
| 313 | } |
| 314 | |
| 315 | if (domain & AMDGPU_GEM_DOMAIN_VRAM) { |
| 316 | man = &adev->mman.bdev.man[TTM_PL_VRAM]; |
| 317 | |
| 318 | if (size < (man->size << PAGE_SHIFT)) |
| 319 | return true; |
| 320 | else |
| 321 | goto fail; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | /* TODO add more domains checks, such as AMDGPU_GEM_DOMAIN_CPU */ |
| 326 | return true; |
| 327 | |
| 328 | fail: |
Michel Dänzer | 299c776 | 2017-11-15 11:37:23 +0100 | [diff] [blame] | 329 | DRM_DEBUG("BO size %lu > total memory in domain: %llu\n", size, |
| 330 | man->size << PAGE_SHIFT); |
Andrey Grodzovsky | 79c6312 | 2017-11-10 18:35:56 -0500 | [diff] [blame] | 331 | return false; |
| 332 | } |
| 333 | |
Christian König | c09312a | 2017-09-12 10:56:17 +0200 | [diff] [blame] | 334 | static int amdgpu_bo_do_create(struct amdgpu_device *adev, |
| 335 | unsigned long size, int byte_align, |
| 336 | bool kernel, u32 domain, u64 flags, |
| 337 | struct sg_table *sg, |
| 338 | struct reservation_object *resv, |
| 339 | uint64_t init_value, |
| 340 | struct amdgpu_bo **bo_ptr) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 341 | { |
Roger He | 9251859 | 2017-12-08 13:31:52 +0800 | [diff] [blame] | 342 | struct ttm_operation_ctx ctx = { |
| 343 | .interruptible = !kernel, |
| 344 | .no_wait_gpu = false, |
| 345 | .allow_reserved_eviction = true, |
| 346 | .resv = resv |
| 347 | }; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 348 | struct amdgpu_bo *bo; |
| 349 | enum ttm_bo_type type; |
| 350 | unsigned long page_align; |
| 351 | size_t acc_size; |
| 352 | int r; |
| 353 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 354 | page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT; |
| 355 | size = ALIGN(size, PAGE_SIZE); |
| 356 | |
Andrey Grodzovsky | 79c6312 | 2017-11-10 18:35:56 -0500 | [diff] [blame] | 357 | if (!amdgpu_bo_validate_size(adev, size, domain)) |
| 358 | return -ENOMEM; |
| 359 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 360 | if (kernel) { |
| 361 | type = ttm_bo_type_kernel; |
| 362 | } else if (sg) { |
| 363 | type = ttm_bo_type_sg; |
| 364 | } else { |
| 365 | type = ttm_bo_type_device; |
| 366 | } |
| 367 | *bo_ptr = NULL; |
| 368 | |
| 369 | acc_size = ttm_bo_dma_acc_size(&adev->mman.bdev, size, |
| 370 | sizeof(struct amdgpu_bo)); |
| 371 | |
| 372 | bo = kzalloc(sizeof(struct amdgpu_bo), GFP_KERNEL); |
| 373 | if (bo == NULL) |
| 374 | return -ENOMEM; |
| 375 | r = drm_gem_object_init(adev->ddev, &bo->gem_base, size); |
| 376 | if (unlikely(r)) { |
| 377 | kfree(bo); |
| 378 | return r; |
| 379 | } |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 380 | INIT_LIST_HEAD(&bo->shadow_list); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 381 | INIT_LIST_HEAD(&bo->va); |
Kent Russell | 6d7d9c5 | 2017-08-08 07:58:01 -0400 | [diff] [blame] | 382 | bo->preferred_domains = domain & (AMDGPU_GEM_DOMAIN_VRAM | |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 383 | AMDGPU_GEM_DOMAIN_GTT | |
| 384 | AMDGPU_GEM_DOMAIN_CPU | |
| 385 | AMDGPU_GEM_DOMAIN_GDS | |
| 386 | AMDGPU_GEM_DOMAIN_GWS | |
| 387 | AMDGPU_GEM_DOMAIN_OA); |
Kent Russell | 6d7d9c5 | 2017-08-08 07:58:01 -0400 | [diff] [blame] | 388 | bo->allowed_domains = bo->preferred_domains; |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 389 | if (!kernel && bo->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM) |
| 390 | bo->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 391 | |
| 392 | bo->flags = flags; |
Oded Gabbay | a187f17 | 2016-01-30 07:59:34 +0200 | [diff] [blame] | 393 | |
Nils Holland | a2e2f29 | 2017-01-22 20:15:27 +0100 | [diff] [blame] | 394 | #ifdef CONFIG_X86_32 |
| 395 | /* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit |
| 396 | * See https://bugs.freedesktop.org/show_bug.cgi?id=84627 |
| 397 | */ |
| 398 | bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC; |
| 399 | #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT) |
| 400 | /* Don't try to enable write-combining when it can't work, or things |
| 401 | * may be slow |
| 402 | * See https://bugs.freedesktop.org/show_bug.cgi?id=88758 |
| 403 | */ |
| 404 | |
Arnd Bergmann | 31bb90f | 2017-02-01 16:59:21 +0100 | [diff] [blame] | 405 | #ifndef CONFIG_COMPILE_TEST |
Nils Holland | a2e2f29 | 2017-01-22 20:15:27 +0100 | [diff] [blame] | 406 | #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \ |
| 407 | thanks to write-combining |
Arnd Bergmann | 31bb90f | 2017-02-01 16:59:21 +0100 | [diff] [blame] | 408 | #endif |
Nils Holland | a2e2f29 | 2017-01-22 20:15:27 +0100 | [diff] [blame] | 409 | |
| 410 | if (bo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC) |
| 411 | DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for " |
| 412 | "better performance thanks to write-combining\n"); |
| 413 | bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC; |
| 414 | #else |
Oded Gabbay | a187f17 | 2016-01-30 07:59:34 +0200 | [diff] [blame] | 415 | /* For architectures that don't support WC memory, |
| 416 | * mask out the WC flag from the BO |
| 417 | */ |
| 418 | if (!drm_arch_can_wc_memory()) |
| 419 | bo->flags &= ~AMDGPU_GEM_CREATE_CPU_GTT_USWC; |
Nils Holland | a2e2f29 | 2017-01-22 20:15:27 +0100 | [diff] [blame] | 420 | #endif |
Oded Gabbay | a187f17 | 2016-01-30 07:59:34 +0200 | [diff] [blame] | 421 | |
Christian König | c09312a | 2017-09-12 10:56:17 +0200 | [diff] [blame] | 422 | bo->tbo.bdev = &adev->mman.bdev; |
| 423 | amdgpu_ttm_placement_from_domain(bo, domain); |
Christian König | f45dc74 | 2016-11-17 12:24:48 +0100 | [diff] [blame] | 424 | |
Nicolai Hähnle | 59c66c9 | 2017-02-16 11:01:44 +0100 | [diff] [blame] | 425 | r = ttm_bo_init_reserved(&adev->mman.bdev, &bo->tbo, size, type, |
Christian König | 6fead44 | 2017-04-12 14:41:43 +0200 | [diff] [blame] | 426 | &bo->placement, page_align, &ctx, NULL, |
Nicolai Hähnle | 59c66c9 | 2017-02-16 11:01:44 +0100 | [diff] [blame] | 427 | acc_size, sg, resv, &amdgpu_ttm_bo_destroy); |
Christian König | a695e43 | 2017-10-31 09:36:13 +0100 | [diff] [blame] | 428 | if (unlikely(r != 0)) |
| 429 | return r; |
| 430 | |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 431 | if (adev->gmc.visible_vram_size < adev->gmc.real_vram_size && |
John Brooks | 00f06b2 | 2017-06-27 22:33:18 -0400 | [diff] [blame] | 432 | bo->tbo.mem.mem_type == TTM_PL_VRAM && |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 433 | bo->tbo.mem.start < adev->gmc.visible_vram_size >> PAGE_SHIFT) |
Christian König | 6af046d | 2017-04-27 18:20:47 +0200 | [diff] [blame] | 434 | amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, |
| 435 | ctx.bytes_moved); |
John Brooks | 00f06b2 | 2017-06-27 22:33:18 -0400 | [diff] [blame] | 436 | else |
Christian König | 6af046d | 2017-04-27 18:20:47 +0200 | [diff] [blame] | 437 | amdgpu_cs_report_moved_bytes(adev, ctx.bytes_moved, 0); |
Samuel Pitoiset | fad0612 | 2017-02-09 11:33:37 +0100 | [diff] [blame] | 438 | |
Christian König | 373308a5 | 2017-01-23 16:28:06 -0500 | [diff] [blame] | 439 | if (kernel) |
Roger.He | c309cd0 | 2017-03-27 19:38:11 +0800 | [diff] [blame] | 440 | bo->tbo.priority = 1; |
Christian König | e1f055b | 2017-01-10 17:27:49 +0100 | [diff] [blame] | 441 | |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 442 | if (flags & AMDGPU_GEM_CREATE_VRAM_CLEARED && |
| 443 | bo->tbo.mem.placement & TTM_PL_FLAG_VRAM) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 444 | struct dma_fence *fence; |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 445 | |
Yong Zhao | 2046d46 | 2017-07-20 18:49:09 -0400 | [diff] [blame] | 446 | r = amdgpu_fill_buffer(bo, init_value, bo->tbo.resv, &fence); |
Christian König | c3af1258 | 2016-11-17 12:16:34 +0100 | [diff] [blame] | 447 | if (unlikely(r)) |
| 448 | goto fail_unreserve; |
| 449 | |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 450 | amdgpu_bo_fence(bo, fence, false); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 451 | dma_fence_put(bo->tbo.moving); |
| 452 | bo->tbo.moving = dma_fence_get(fence); |
| 453 | dma_fence_put(fence); |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 454 | } |
Christian König | f45dc74 | 2016-11-17 12:24:48 +0100 | [diff] [blame] | 455 | if (!resv) |
Nicolai Hähnle | 59c66c9 | 2017-02-16 11:01:44 +0100 | [diff] [blame] | 456 | amdgpu_bo_unreserve(bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 457 | *bo_ptr = bo; |
| 458 | |
| 459 | trace_amdgpu_bo_create(bo); |
| 460 | |
John Brooks | 96cf827 | 2017-06-30 11:31:08 -0400 | [diff] [blame] | 461 | /* Treat CPU_ACCESS_REQUIRED only as a hint if given by UMD */ |
| 462 | if (type == ttm_bo_type_device) |
| 463 | bo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; |
| 464 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 465 | return 0; |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 466 | |
| 467 | fail_unreserve: |
Nicolai Hähnle | f1543f5 | 2017-01-10 20:36:56 +0100 | [diff] [blame] | 468 | if (!resv) |
| 469 | ww_mutex_unlock(&bo->tbo.resv->lock); |
Flora Cui | 4fea83f | 2016-07-20 14:44:38 +0800 | [diff] [blame] | 470 | amdgpu_bo_unref(&bo); |
| 471 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 472 | } |
| 473 | |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 474 | static int amdgpu_bo_create_shadow(struct amdgpu_device *adev, |
| 475 | unsigned long size, int byte_align, |
| 476 | struct amdgpu_bo *bo) |
| 477 | { |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 478 | int r; |
| 479 | |
| 480 | if (bo->shadow) |
| 481 | return 0; |
| 482 | |
Christian König | c09312a | 2017-09-12 10:56:17 +0200 | [diff] [blame] | 483 | r = amdgpu_bo_do_create(adev, size, byte_align, true, |
| 484 | AMDGPU_GEM_DOMAIN_GTT, |
| 485 | AMDGPU_GEM_CREATE_CPU_GTT_USWC | |
| 486 | AMDGPU_GEM_CREATE_SHADOW, |
| 487 | NULL, bo->tbo.resv, 0, |
| 488 | &bo->shadow); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 489 | if (!r) { |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 490 | bo->shadow->parent = amdgpu_bo_ref(bo); |
Chunming Zhou | 0c4e7fa | 2016-08-17 11:41:30 +0800 | [diff] [blame] | 491 | mutex_lock(&adev->shadow_list_lock); |
| 492 | list_add_tail(&bo->shadow_list, &adev->shadow_list); |
| 493 | mutex_unlock(&adev->shadow_list_lock); |
| 494 | } |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 495 | |
| 496 | return r; |
| 497 | } |
| 498 | |
Yong Zhao | 2046d46 | 2017-07-20 18:49:09 -0400 | [diff] [blame] | 499 | /* init_value will only take effect when flags contains |
| 500 | * AMDGPU_GEM_CREATE_VRAM_CLEARED. |
| 501 | */ |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 502 | int amdgpu_bo_create(struct amdgpu_device *adev, |
| 503 | unsigned long size, int byte_align, |
| 504 | bool kernel, u32 domain, u64 flags, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 505 | struct sg_table *sg, |
| 506 | struct reservation_object *resv, |
Yong Zhao | 2046d46 | 2017-07-20 18:49:09 -0400 | [diff] [blame] | 507 | uint64_t init_value, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 508 | struct amdgpu_bo **bo_ptr) |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 509 | { |
Christian König | cf273a5 | 2017-08-18 15:50:17 +0200 | [diff] [blame] | 510 | uint64_t parent_flags = flags & ~AMDGPU_GEM_CREATE_SHADOW; |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 511 | int r; |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 512 | |
Christian König | c09312a | 2017-09-12 10:56:17 +0200 | [diff] [blame] | 513 | r = amdgpu_bo_do_create(adev, size, byte_align, kernel, domain, |
| 514 | parent_flags, sg, resv, init_value, bo_ptr); |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 515 | if (r) |
| 516 | return r; |
| 517 | |
Christian König | cf273a5 | 2017-08-18 15:50:17 +0200 | [diff] [blame] | 518 | if ((flags & AMDGPU_GEM_CREATE_SHADOW) && amdgpu_need_backup(adev)) { |
| 519 | if (!resv) |
| 520 | WARN_ON(reservation_object_lock((*bo_ptr)->tbo.resv, |
| 521 | NULL)); |
Nicolai Hähnle | 36ea83d | 2017-01-10 19:06:00 +0100 | [diff] [blame] | 522 | |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 523 | r = amdgpu_bo_create_shadow(adev, size, byte_align, (*bo_ptr)); |
Nicolai Hähnle | 36ea83d | 2017-01-10 19:06:00 +0100 | [diff] [blame] | 524 | |
| 525 | if (!resv) |
Christian König | cf273a5 | 2017-08-18 15:50:17 +0200 | [diff] [blame] | 526 | reservation_object_unlock((*bo_ptr)->tbo.resv); |
Nicolai Hähnle | 36ea83d | 2017-01-10 19:06:00 +0100 | [diff] [blame] | 527 | |
Chunming Zhou | e7893c4 | 2016-07-26 14:13:21 +0800 | [diff] [blame] | 528 | if (r) |
| 529 | amdgpu_bo_unref(bo_ptr); |
| 530 | } |
| 531 | |
| 532 | return r; |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 533 | } |
| 534 | |
Chunming Zhou | 20f4eff | 2016-08-04 16:51:18 +0800 | [diff] [blame] | 535 | int amdgpu_bo_backup_to_shadow(struct amdgpu_device *adev, |
| 536 | struct amdgpu_ring *ring, |
| 537 | struct amdgpu_bo *bo, |
| 538 | struct reservation_object *resv, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 539 | struct dma_fence **fence, |
Chunming Zhou | 20f4eff | 2016-08-04 16:51:18 +0800 | [diff] [blame] | 540 | bool direct) |
| 541 | |
| 542 | { |
| 543 | struct amdgpu_bo *shadow = bo->shadow; |
| 544 | uint64_t bo_addr, shadow_addr; |
| 545 | int r; |
| 546 | |
| 547 | if (!shadow) |
| 548 | return -EINVAL; |
| 549 | |
| 550 | bo_addr = amdgpu_bo_gpu_offset(bo); |
| 551 | shadow_addr = amdgpu_bo_gpu_offset(bo->shadow); |
| 552 | |
| 553 | r = reservation_object_reserve_shared(bo->tbo.resv); |
| 554 | if (r) |
| 555 | goto err; |
| 556 | |
| 557 | r = amdgpu_copy_buffer(ring, bo_addr, shadow_addr, |
| 558 | amdgpu_bo_size(bo), resv, fence, |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 559 | direct, false); |
Chunming Zhou | 20f4eff | 2016-08-04 16:51:18 +0800 | [diff] [blame] | 560 | if (!r) |
| 561 | amdgpu_bo_fence(bo, *fence, true); |
| 562 | |
| 563 | err: |
| 564 | return r; |
| 565 | } |
| 566 | |
Roger.He | 8252131 | 2017-04-21 13:08:43 +0800 | [diff] [blame] | 567 | int amdgpu_bo_validate(struct amdgpu_bo *bo) |
| 568 | { |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 569 | struct ttm_operation_ctx ctx = { false, false }; |
Roger.He | 8252131 | 2017-04-21 13:08:43 +0800 | [diff] [blame] | 570 | uint32_t domain; |
| 571 | int r; |
| 572 | |
| 573 | if (bo->pin_count) |
| 574 | return 0; |
| 575 | |
Kent Russell | 6d7d9c5 | 2017-08-08 07:58:01 -0400 | [diff] [blame] | 576 | domain = bo->preferred_domains; |
Roger.He | 8252131 | 2017-04-21 13:08:43 +0800 | [diff] [blame] | 577 | |
| 578 | retry: |
| 579 | amdgpu_ttm_placement_from_domain(bo, domain); |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 580 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); |
Roger.He | 8252131 | 2017-04-21 13:08:43 +0800 | [diff] [blame] | 581 | if (unlikely(r == -ENOMEM) && domain != bo->allowed_domains) { |
| 582 | domain = bo->allowed_domains; |
| 583 | goto retry; |
| 584 | } |
| 585 | |
| 586 | return r; |
| 587 | } |
| 588 | |
Chunming Zhou | 20f4eff | 2016-08-04 16:51:18 +0800 | [diff] [blame] | 589 | int amdgpu_bo_restore_from_shadow(struct amdgpu_device *adev, |
| 590 | struct amdgpu_ring *ring, |
| 591 | struct amdgpu_bo *bo, |
| 592 | struct reservation_object *resv, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 593 | struct dma_fence **fence, |
Chunming Zhou | 20f4eff | 2016-08-04 16:51:18 +0800 | [diff] [blame] | 594 | bool direct) |
| 595 | |
| 596 | { |
| 597 | struct amdgpu_bo *shadow = bo->shadow; |
| 598 | uint64_t bo_addr, shadow_addr; |
| 599 | int r; |
| 600 | |
| 601 | if (!shadow) |
| 602 | return -EINVAL; |
| 603 | |
| 604 | bo_addr = amdgpu_bo_gpu_offset(bo); |
| 605 | shadow_addr = amdgpu_bo_gpu_offset(bo->shadow); |
| 606 | |
| 607 | r = reservation_object_reserve_shared(bo->tbo.resv); |
| 608 | if (r) |
| 609 | goto err; |
| 610 | |
| 611 | r = amdgpu_copy_buffer(ring, shadow_addr, bo_addr, |
| 612 | amdgpu_bo_size(bo), resv, fence, |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 613 | direct, false); |
Chunming Zhou | 20f4eff | 2016-08-04 16:51:18 +0800 | [diff] [blame] | 614 | if (!r) |
| 615 | amdgpu_bo_fence(bo, *fence, true); |
| 616 | |
| 617 | err: |
| 618 | return r; |
| 619 | } |
| 620 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 621 | int amdgpu_bo_kmap(struct amdgpu_bo *bo, void **ptr) |
| 622 | { |
Christian König | f5e1c74 | 2017-07-20 23:45:18 +0200 | [diff] [blame] | 623 | void *kptr; |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 624 | long r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 625 | |
Christian König | 271c812 | 2015-05-13 14:30:53 +0200 | [diff] [blame] | 626 | if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) |
| 627 | return -EPERM; |
| 628 | |
Christian König | f5e1c74 | 2017-07-20 23:45:18 +0200 | [diff] [blame] | 629 | kptr = amdgpu_bo_kptr(bo); |
| 630 | if (kptr) { |
| 631 | if (ptr) |
| 632 | *ptr = kptr; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 633 | return 0; |
| 634 | } |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 635 | |
| 636 | r = reservation_object_wait_timeout_rcu(bo->tbo.resv, false, false, |
| 637 | MAX_SCHEDULE_TIMEOUT); |
| 638 | if (r < 0) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 639 | return r; |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 640 | |
| 641 | r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap); |
| 642 | if (r) |
| 643 | return r; |
| 644 | |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 645 | if (ptr) |
Christian König | f5e1c74 | 2017-07-20 23:45:18 +0200 | [diff] [blame] | 646 | *ptr = amdgpu_bo_kptr(bo); |
Christian König | 587f3c7 | 2016-03-10 16:21:04 +0100 | [diff] [blame] | 647 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Christian König | f5e1c74 | 2017-07-20 23:45:18 +0200 | [diff] [blame] | 651 | void *amdgpu_bo_kptr(struct amdgpu_bo *bo) |
| 652 | { |
| 653 | bool is_iomem; |
| 654 | |
| 655 | return ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); |
| 656 | } |
| 657 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 658 | void amdgpu_bo_kunmap(struct amdgpu_bo *bo) |
| 659 | { |
Christian König | f5e1c74 | 2017-07-20 23:45:18 +0200 | [diff] [blame] | 660 | if (bo->kmap.bo) |
| 661 | ttm_bo_kunmap(&bo->kmap); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | struct amdgpu_bo *amdgpu_bo_ref(struct amdgpu_bo *bo) |
| 665 | { |
| 666 | if (bo == NULL) |
| 667 | return NULL; |
| 668 | |
| 669 | ttm_bo_reference(&bo->tbo); |
| 670 | return bo; |
| 671 | } |
| 672 | |
| 673 | void amdgpu_bo_unref(struct amdgpu_bo **bo) |
| 674 | { |
| 675 | struct ttm_buffer_object *tbo; |
| 676 | |
| 677 | if ((*bo) == NULL) |
| 678 | return; |
| 679 | |
| 680 | tbo = &((*bo)->tbo); |
| 681 | ttm_bo_unref(&tbo); |
| 682 | if (tbo == NULL) |
| 683 | *bo = NULL; |
| 684 | } |
| 685 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 686 | int amdgpu_bo_pin_restricted(struct amdgpu_bo *bo, u32 domain, |
| 687 | u64 min_offset, u64 max_offset, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 688 | u64 *gpu_addr) |
| 689 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 690 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 691 | struct ttm_operation_ctx ctx = { false, false }; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 692 | int r, i; |
| 693 | |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 694 | if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 695 | return -EPERM; |
| 696 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 697 | if (WARN_ON_ONCE(min_offset > max_offset)) |
| 698 | return -EINVAL; |
| 699 | |
Christopher James Halse Rogers | 803d89a | 2017-04-03 13:31:22 +1000 | [diff] [blame] | 700 | /* A shared bo cannot be migrated to VRAM */ |
| 701 | if (bo->prime_shared_count && (domain == AMDGPU_GEM_DOMAIN_VRAM)) |
| 702 | return -EINVAL; |
| 703 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 704 | if (bo->pin_count) { |
Flora Cui | 408778e | 2016-08-18 12:55:13 +0800 | [diff] [blame] | 705 | uint32_t mem_type = bo->tbo.mem.mem_type; |
| 706 | |
Christian König | f531895 | 2017-10-23 17:29:36 +0200 | [diff] [blame] | 707 | if (!(domain & amdgpu_mem_type_to_domain(mem_type))) |
Flora Cui | 408778e | 2016-08-18 12:55:13 +0800 | [diff] [blame] | 708 | return -EINVAL; |
| 709 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 710 | bo->pin_count++; |
| 711 | if (gpu_addr) |
| 712 | *gpu_addr = amdgpu_bo_gpu_offset(bo); |
| 713 | |
| 714 | if (max_offset != 0) { |
Flora Cui | 27798e0 | 2016-08-18 13:18:09 +0800 | [diff] [blame] | 715 | u64 domain_start = bo->tbo.bdev->man[mem_type].gpu_offset; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 716 | WARN_ON_ONCE(max_offset < |
| 717 | (amdgpu_bo_gpu_offset(bo) - domain_start)); |
| 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 722 | |
| 723 | bo->flags |= AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS; |
Christian König | e9c7577 | 2017-09-11 17:29:26 +0200 | [diff] [blame] | 724 | /* force to pin into visible video ram */ |
| 725 | if (!(bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) |
| 726 | bo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 727 | amdgpu_ttm_placement_from_domain(bo, domain); |
| 728 | for (i = 0; i < bo->placement.num_placement; i++) { |
Christian König | e9c7577 | 2017-09-11 17:29:26 +0200 | [diff] [blame] | 729 | unsigned fpfn, lpfn; |
| 730 | |
| 731 | fpfn = min_offset >> PAGE_SHIFT; |
| 732 | lpfn = max_offset >> PAGE_SHIFT; |
| 733 | |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 734 | if (fpfn > bo->placements[i].fpfn) |
| 735 | bo->placements[i].fpfn = fpfn; |
Christian König | 78d0e18 | 2016-01-19 12:48:14 +0100 | [diff] [blame] | 736 | if (!bo->placements[i].lpfn || |
| 737 | (lpfn && lpfn < bo->placements[i].lpfn)) |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 738 | bo->placements[i].lpfn = lpfn; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 739 | bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT; |
| 740 | } |
| 741 | |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 742 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 743 | if (unlikely(r)) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 744 | dev_err(adev->dev, "%p pin failed\n", bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 745 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 746 | } |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 747 | |
Christian König | c5835bb | 2017-10-27 15:43:14 +0200 | [diff] [blame] | 748 | r = amdgpu_ttm_alloc_gart(&bo->tbo); |
Christian König | ead282a | 2017-10-20 13:12:12 +0200 | [diff] [blame] | 749 | if (unlikely(r)) { |
| 750 | dev_err(adev->dev, "%p bind failed\n", bo); |
| 751 | goto error; |
Chunming Zhou | 07306b4 | 2017-07-12 12:36:47 +0800 | [diff] [blame] | 752 | } |
Christian König | 5e91fb5 | 2017-10-20 13:11:00 +0200 | [diff] [blame] | 753 | |
Christian König | ead282a | 2017-10-20 13:12:12 +0200 | [diff] [blame] | 754 | bo->pin_count = 1; |
| 755 | if (gpu_addr != NULL) |
| 756 | *gpu_addr = amdgpu_bo_gpu_offset(bo); |
| 757 | |
Christian König | 5e91fb5 | 2017-10-20 13:11:00 +0200 | [diff] [blame] | 758 | domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 759 | if (domain == AMDGPU_GEM_DOMAIN_VRAM) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 760 | adev->vram_pin_size += amdgpu_bo_size(bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 761 | if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 762 | adev->invisible_pin_size += amdgpu_bo_size(bo); |
Flora Cui | 32ab75f | 2016-08-18 13:17:07 +0800 | [diff] [blame] | 763 | } else if (domain == AMDGPU_GEM_DOMAIN_GTT) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 764 | adev->gart_pin_size += amdgpu_bo_size(bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | error: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 768 | return r; |
| 769 | } |
| 770 | |
| 771 | int amdgpu_bo_pin(struct amdgpu_bo *bo, u32 domain, u64 *gpu_addr) |
| 772 | { |
Chunming Zhou | 7e5a547 | 2015-04-24 17:37:30 +0800 | [diff] [blame] | 773 | return amdgpu_bo_pin_restricted(bo, domain, 0, 0, gpu_addr); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | int amdgpu_bo_unpin(struct amdgpu_bo *bo) |
| 777 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 778 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 779 | struct ttm_operation_ctx ctx = { false, false }; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 780 | int r, i; |
| 781 | |
| 782 | if (!bo->pin_count) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 783 | dev_warn(adev->dev, "%p unpin not necessary\n", bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 784 | return 0; |
| 785 | } |
| 786 | bo->pin_count--; |
| 787 | if (bo->pin_count) |
| 788 | return 0; |
| 789 | for (i = 0; i < bo->placement.num_placement; i++) { |
| 790 | bo->placements[i].lpfn = 0; |
| 791 | bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT; |
| 792 | } |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 793 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 794 | if (unlikely(r)) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 795 | dev_err(adev->dev, "%p validate failed for unpin\n", bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 796 | goto error; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 797 | } |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 798 | |
| 799 | if (bo->tbo.mem.mem_type == TTM_PL_VRAM) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 800 | adev->vram_pin_size -= amdgpu_bo_size(bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 801 | if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS) |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 802 | adev->invisible_pin_size -= amdgpu_bo_size(bo); |
Flora Cui | 441f90e | 2016-09-09 14:15:30 +0800 | [diff] [blame] | 803 | } else if (bo->tbo.mem.mem_type == TTM_PL_TT) { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 804 | adev->gart_pin_size -= amdgpu_bo_size(bo); |
Christian König | 6681c5e | 2016-08-12 16:50:12 +0200 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | error: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 808 | return r; |
| 809 | } |
| 810 | |
| 811 | int amdgpu_bo_evict_vram(struct amdgpu_device *adev) |
| 812 | { |
| 813 | /* 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] | 814 | if (0 && (adev->flags & AMD_IS_APU)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 815 | /* Useless to evict on IGP chips */ |
| 816 | return 0; |
| 817 | } |
| 818 | return ttm_bo_evict_mm(&adev->mman.bdev, TTM_PL_VRAM); |
| 819 | } |
| 820 | |
Alex Deucher | 1f8628c | 2016-03-31 16:56:22 -0400 | [diff] [blame] | 821 | static const char *amdgpu_vram_names[] = { |
| 822 | "UNKNOWN", |
| 823 | "GDDR1", |
| 824 | "DDR2", |
| 825 | "GDDR3", |
| 826 | "GDDR4", |
| 827 | "GDDR5", |
| 828 | "HBM", |
| 829 | "DDR3" |
| 830 | }; |
| 831 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 832 | int amdgpu_bo_init(struct amdgpu_device *adev) |
| 833 | { |
Dave Airlie | 7cf321d | 2016-10-24 15:37:48 +1000 | [diff] [blame] | 834 | /* reserve PAT memory space to WC for VRAM */ |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 835 | arch_io_reserve_memtype_wc(adev->gmc.aper_base, |
| 836 | adev->gmc.aper_size); |
Dave Airlie | 7cf321d | 2016-10-24 15:37:48 +1000 | [diff] [blame] | 837 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 838 | /* Add an MTRR for the VRAM */ |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 839 | adev->gmc.vram_mtrr = arch_phys_wc_add(adev->gmc.aper_base, |
| 840 | adev->gmc.aper_size); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 841 | DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n", |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 842 | adev->gmc.mc_vram_size >> 20, |
| 843 | (unsigned long long)adev->gmc.aper_size >> 20); |
Alex Deucher | 1f8628c | 2016-03-31 16:56:22 -0400 | [diff] [blame] | 844 | DRM_INFO("RAM width %dbits %s\n", |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 845 | adev->gmc.vram_width, amdgpu_vram_names[adev->gmc.vram_type]); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 846 | return amdgpu_ttm_init(adev); |
| 847 | } |
| 848 | |
| 849 | void amdgpu_bo_fini(struct amdgpu_device *adev) |
| 850 | { |
| 851 | amdgpu_ttm_fini(adev); |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 852 | arch_phys_wc_del(adev->gmc.vram_mtrr); |
| 853 | arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | int amdgpu_bo_fbdev_mmap(struct amdgpu_bo *bo, |
| 857 | struct vm_area_struct *vma) |
| 858 | { |
| 859 | return ttm_fbdev_mmap(vma, &bo->tbo); |
| 860 | } |
| 861 | |
| 862 | int amdgpu_bo_set_tiling_flags(struct amdgpu_bo *bo, u64 tiling_flags) |
| 863 | { |
Marek Olšák | 9079ac7 | 2017-03-03 16:03:15 -0500 | [diff] [blame] | 864 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
| 865 | |
| 866 | if (adev->family <= AMDGPU_FAMILY_CZ && |
| 867 | AMDGPU_TILING_GET(tiling_flags, TILE_SPLIT) > 6) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 868 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 869 | |
| 870 | bo->tiling_flags = tiling_flags; |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | void amdgpu_bo_get_tiling_flags(struct amdgpu_bo *bo, u64 *tiling_flags) |
| 875 | { |
| 876 | lockdep_assert_held(&bo->tbo.resv->lock.base); |
| 877 | |
| 878 | if (tiling_flags) |
| 879 | *tiling_flags = bo->tiling_flags; |
| 880 | } |
| 881 | |
| 882 | int amdgpu_bo_set_metadata (struct amdgpu_bo *bo, void *metadata, |
| 883 | uint32_t metadata_size, uint64_t flags) |
| 884 | { |
| 885 | void *buffer; |
| 886 | |
| 887 | if (!metadata_size) { |
| 888 | if (bo->metadata_size) { |
| 889 | kfree(bo->metadata); |
Dave Airlie | 0092d3e | 2016-05-03 12:44:29 +1000 | [diff] [blame] | 890 | bo->metadata = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 891 | bo->metadata_size = 0; |
| 892 | } |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | if (metadata == NULL) |
| 897 | return -EINVAL; |
| 898 | |
Andrzej Hajda | 71affda | 2015-09-21 17:34:39 -0400 | [diff] [blame] | 899 | buffer = kmemdup(metadata, metadata_size, GFP_KERNEL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 900 | if (buffer == NULL) |
| 901 | return -ENOMEM; |
| 902 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 903 | kfree(bo->metadata); |
| 904 | bo->metadata_flags = flags; |
| 905 | bo->metadata = buffer; |
| 906 | bo->metadata_size = metadata_size; |
| 907 | |
| 908 | return 0; |
| 909 | } |
| 910 | |
| 911 | int amdgpu_bo_get_metadata(struct amdgpu_bo *bo, void *buffer, |
| 912 | size_t buffer_size, uint32_t *metadata_size, |
| 913 | uint64_t *flags) |
| 914 | { |
| 915 | if (!buffer && !metadata_size) |
| 916 | return -EINVAL; |
| 917 | |
| 918 | if (buffer) { |
| 919 | if (buffer_size < bo->metadata_size) |
| 920 | return -EINVAL; |
| 921 | |
| 922 | if (bo->metadata_size) |
| 923 | memcpy(buffer, bo->metadata, bo->metadata_size); |
| 924 | } |
| 925 | |
| 926 | if (metadata_size) |
| 927 | *metadata_size = bo->metadata_size; |
| 928 | if (flags) |
| 929 | *flags = bo->metadata_flags; |
| 930 | |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | void amdgpu_bo_move_notify(struct ttm_buffer_object *bo, |
Nicolai Hähnle | 66257db | 2016-12-15 17:23:49 +0100 | [diff] [blame] | 935 | bool evict, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 936 | struct ttm_mem_reg *new_mem) |
| 937 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 938 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 939 | struct amdgpu_bo *abo; |
David Mao | 15da301 | 2016-06-07 17:48:52 +0800 | [diff] [blame] | 940 | struct ttm_mem_reg *old_mem = &bo->mem; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 941 | |
| 942 | if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) |
| 943 | return; |
| 944 | |
Andres Rodriguez | b82485f | 2017-09-15 21:05:19 -0400 | [diff] [blame] | 945 | abo = ttm_to_amdgpu_bo(bo); |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 946 | amdgpu_vm_bo_invalidate(adev, abo, evict); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 947 | |
Christian König | 6375bbb | 2017-07-11 17:25:49 +0200 | [diff] [blame] | 948 | amdgpu_bo_kunmap(abo); |
| 949 | |
Nicolai Hähnle | 661a760 | 2016-12-15 17:26:42 +0100 | [diff] [blame] | 950 | /* remember the eviction */ |
| 951 | if (evict) |
| 952 | atomic64_inc(&adev->num_evictions); |
| 953 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 954 | /* update statistics */ |
| 955 | if (!new_mem) |
| 956 | return; |
| 957 | |
| 958 | /* move_notify is called before move happens */ |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 959 | 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] | 960 | } |
| 961 | |
| 962 | int amdgpu_bo_fault_reserve_notify(struct ttm_buffer_object *bo) |
| 963 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 964 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 965 | struct ttm_operation_ctx ctx = { false, false }; |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 966 | struct amdgpu_bo *abo; |
John Brooks | 96cf827 | 2017-06-30 11:31:08 -0400 | [diff] [blame] | 967 | unsigned long offset, size; |
| 968 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 969 | |
| 970 | if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) |
| 971 | return 0; |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 972 | |
Andres Rodriguez | b82485f | 2017-09-15 21:05:19 -0400 | [diff] [blame] | 973 | abo = ttm_to_amdgpu_bo(bo); |
John Brooks | 96cf827 | 2017-06-30 11:31:08 -0400 | [diff] [blame] | 974 | |
| 975 | /* Remember that this BO was accessed by the CPU */ |
| 976 | abo->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; |
| 977 | |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 978 | if (bo->mem.mem_type != TTM_PL_VRAM) |
| 979 | return 0; |
| 980 | |
| 981 | size = bo->mem.num_pages << PAGE_SHIFT; |
| 982 | offset = bo->mem.start << PAGE_SHIFT; |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 983 | if ((offset + size) <= adev->gmc.visible_vram_size) |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 984 | return 0; |
| 985 | |
Michel Dänzer | 104ece9 | 2016-03-28 12:53:02 +0900 | [diff] [blame] | 986 | /* Can't move a pinned BO to visible VRAM */ |
| 987 | if (abo->pin_count > 0) |
| 988 | return -EINVAL; |
| 989 | |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 990 | /* hurrah the memory is not visible ! */ |
Marek Olšák | 68e2c5f | 2017-05-17 20:05:08 +0200 | [diff] [blame] | 991 | atomic64_inc(&adev->num_vram_cpu_page_faults); |
John Brooks | 41d9a6a | 2017-06-27 22:33:21 -0400 | [diff] [blame] | 992 | amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM | |
| 993 | AMDGPU_GEM_DOMAIN_GTT); |
| 994 | |
| 995 | /* Avoid costly evictions; only set GTT as a busy placement */ |
| 996 | abo->placement.num_busy_placement = 1; |
| 997 | abo->placement.busy_placement = &abo->placements[1]; |
| 998 | |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 999 | r = ttm_bo_validate(bo, &abo->placement, &ctx); |
John Brooks | 41d9a6a | 2017-06-27 22:33:21 -0400 | [diff] [blame] | 1000 | if (unlikely(r != 0)) |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 1001 | return r; |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 1002 | |
| 1003 | offset = bo->mem.start << PAGE_SHIFT; |
| 1004 | /* this should never happen */ |
John Brooks | 41d9a6a | 2017-06-27 22:33:21 -0400 | [diff] [blame] | 1005 | if (bo->mem.mem_type == TTM_PL_VRAM && |
Christian König | 770d13b | 2018-01-12 14:52:22 +0100 | [diff] [blame^] | 1006 | (offset + size) > adev->gmc.visible_vram_size) |
Christian König | 5fb1941 | 2015-05-21 17:03:46 +0200 | [diff] [blame] | 1007 | return -EINVAL; |
| 1008 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * amdgpu_bo_fence - add fence to buffer object |
| 1014 | * |
| 1015 | * @bo: buffer object in question |
| 1016 | * @fence: fence to add |
| 1017 | * @shared: true if fence should be added shared |
| 1018 | * |
| 1019 | */ |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1020 | void amdgpu_bo_fence(struct amdgpu_bo *bo, struct dma_fence *fence, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1021 | bool shared) |
| 1022 | { |
| 1023 | struct reservation_object *resv = bo->tbo.resv; |
| 1024 | |
| 1025 | if (shared) |
Chunming Zhou | e40a311 | 2015-08-03 11:38:09 +0800 | [diff] [blame] | 1026 | reservation_object_add_shared_fence(resv, fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1027 | else |
Chunming Zhou | e40a311 | 2015-08-03 11:38:09 +0800 | [diff] [blame] | 1028 | reservation_object_add_excl_fence(resv, fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1029 | } |
Christian König | cdb7e8f | 2016-07-25 17:56:18 +0200 | [diff] [blame] | 1030 | |
| 1031 | /** |
| 1032 | * amdgpu_bo_gpu_offset - return GPU offset of bo |
| 1033 | * @bo: amdgpu object for which we query the offset |
| 1034 | * |
| 1035 | * Returns current GPU offset of the object. |
| 1036 | * |
| 1037 | * Note: object should either be pinned or reserved when calling this |
| 1038 | * function, it might be useful to add check for this for debugging. |
| 1039 | */ |
| 1040 | u64 amdgpu_bo_gpu_offset(struct amdgpu_bo *bo) |
| 1041 | { |
| 1042 | WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_SYSTEM); |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 1043 | WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_TT && |
Christian König | 3da917b | 2017-10-27 14:17:09 +0200 | [diff] [blame] | 1044 | !amdgpu_gtt_mgr_has_gart_addr(&bo->tbo.mem)); |
Christian König | cdb7e8f | 2016-07-25 17:56:18 +0200 | [diff] [blame] | 1045 | WARN_ON_ONCE(!ww_mutex_is_locked(&bo->tbo.resv->lock) && |
| 1046 | !bo->pin_count); |
Christian König | 9702d40 | 2016-09-07 15:10:44 +0200 | [diff] [blame] | 1047 | WARN_ON_ONCE(bo->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET); |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 1048 | WARN_ON_ONCE(bo->tbo.mem.mem_type == TTM_PL_VRAM && |
| 1049 | !(bo->flags & AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS)); |
Christian König | cdb7e8f | 2016-07-25 17:56:18 +0200 | [diff] [blame] | 1050 | |
| 1051 | return bo->tbo.offset; |
| 1052 | } |