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