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 | */ |
Masahiro Yamada | 248a1d6 | 2017-04-24 13:50:21 +0900 | [diff] [blame] | 32 | #include <drm/ttm/ttm_bo_api.h> |
| 33 | #include <drm/ttm/ttm_bo_driver.h> |
| 34 | #include <drm/ttm/ttm_placement.h> |
| 35 | #include <drm/ttm/ttm_module.h> |
| 36 | #include <drm/ttm/ttm_page_alloc.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 37 | #include <drm/drmP.h> |
| 38 | #include <drm/amdgpu_drm.h> |
| 39 | #include <linux/seq_file.h> |
| 40 | #include <linux/slab.h> |
| 41 | #include <linux/swiotlb.h> |
| 42 | #include <linux/swap.h> |
| 43 | #include <linux/pagemap.h> |
| 44 | #include <linux/debugfs.h> |
| 45 | #include "amdgpu.h" |
| 46 | #include "bif/bif_4_1_d.h" |
| 47 | |
| 48 | #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT) |
| 49 | |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 50 | static int amdgpu_map_buffer(struct ttm_buffer_object *bo, |
| 51 | struct ttm_mem_reg *mem, unsigned num_pages, |
| 52 | uint64_t offset, unsigned window, |
| 53 | struct amdgpu_ring *ring, |
| 54 | uint64_t *addr); |
| 55 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 56 | static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev); |
| 57 | static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev); |
| 58 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 59 | /* |
| 60 | * Global memory. |
| 61 | */ |
| 62 | static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref) |
| 63 | { |
| 64 | return ttm_mem_global_init(ref->object); |
| 65 | } |
| 66 | |
| 67 | static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref) |
| 68 | { |
| 69 | ttm_mem_global_release(ref->object); |
| 70 | } |
| 71 | |
Alex Deucher | 70b5c5a | 2016-11-15 16:55:53 -0500 | [diff] [blame] | 72 | static int amdgpu_ttm_global_init(struct amdgpu_device *adev) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 73 | { |
| 74 | struct drm_global_reference *global_ref; |
Christian König | 703297c | 2016-02-10 14:20:50 +0100 | [diff] [blame] | 75 | struct amdgpu_ring *ring; |
| 76 | struct amd_sched_rq *rq; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 77 | int r; |
| 78 | |
| 79 | adev->mman.mem_global_referenced = false; |
| 80 | global_ref = &adev->mman.mem_global_ref; |
| 81 | global_ref->global_type = DRM_GLOBAL_TTM_MEM; |
| 82 | global_ref->size = sizeof(struct ttm_mem_global); |
| 83 | global_ref->init = &amdgpu_ttm_mem_global_init; |
| 84 | global_ref->release = &amdgpu_ttm_mem_global_release; |
| 85 | r = drm_global_item_ref(global_ref); |
Huang Rui | e9d035e | 2016-09-07 20:55:42 +0800 | [diff] [blame] | 86 | if (r) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 87 | DRM_ERROR("Failed setting up TTM memory accounting " |
| 88 | "subsystem.\n"); |
Huang Rui | e9d035e | 2016-09-07 20:55:42 +0800 | [diff] [blame] | 89 | goto error_mem; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | adev->mman.bo_global_ref.mem_glob = |
| 93 | adev->mman.mem_global_ref.object; |
| 94 | global_ref = &adev->mman.bo_global_ref.ref; |
| 95 | global_ref->global_type = DRM_GLOBAL_TTM_BO; |
| 96 | global_ref->size = sizeof(struct ttm_bo_global); |
| 97 | global_ref->init = &ttm_bo_global_init; |
| 98 | global_ref->release = &ttm_bo_global_release; |
| 99 | r = drm_global_item_ref(global_ref); |
Huang Rui | e9d035e | 2016-09-07 20:55:42 +0800 | [diff] [blame] | 100 | if (r) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 101 | DRM_ERROR("Failed setting up TTM BO subsystem.\n"); |
Huang Rui | e9d035e | 2016-09-07 20:55:42 +0800 | [diff] [blame] | 102 | goto error_bo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 105 | mutex_init(&adev->mman.gtt_window_lock); |
| 106 | |
Christian König | 703297c | 2016-02-10 14:20:50 +0100 | [diff] [blame] | 107 | ring = adev->mman.buffer_funcs_ring; |
| 108 | rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL]; |
| 109 | r = amd_sched_entity_init(&ring->sched, &adev->mman.entity, |
| 110 | rq, amdgpu_sched_jobs); |
Huang Rui | e9d035e | 2016-09-07 20:55:42 +0800 | [diff] [blame] | 111 | if (r) { |
Christian König | 703297c | 2016-02-10 14:20:50 +0100 | [diff] [blame] | 112 | DRM_ERROR("Failed setting up TTM BO move run queue.\n"); |
Huang Rui | e9d035e | 2016-09-07 20:55:42 +0800 | [diff] [blame] | 113 | goto error_entity; |
Christian König | 703297c | 2016-02-10 14:20:50 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 116 | adev->mman.mem_global_referenced = true; |
Christian König | 703297c | 2016-02-10 14:20:50 +0100 | [diff] [blame] | 117 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 118 | return 0; |
Huang Rui | e9d035e | 2016-09-07 20:55:42 +0800 | [diff] [blame] | 119 | |
| 120 | error_entity: |
| 121 | drm_global_item_unref(&adev->mman.bo_global_ref.ref); |
| 122 | error_bo: |
| 123 | drm_global_item_unref(&adev->mman.mem_global_ref); |
| 124 | error_mem: |
| 125 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static void amdgpu_ttm_global_fini(struct amdgpu_device *adev) |
| 129 | { |
| 130 | if (adev->mman.mem_global_referenced) { |
Christian König | 703297c | 2016-02-10 14:20:50 +0100 | [diff] [blame] | 131 | amd_sched_entity_fini(adev->mman.entity.sched, |
| 132 | &adev->mman.entity); |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 133 | mutex_destroy(&adev->mman.gtt_window_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 134 | drm_global_item_unref(&adev->mman.bo_global_ref.ref); |
| 135 | drm_global_item_unref(&adev->mman.mem_global_ref); |
| 136 | adev->mman.mem_global_referenced = false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) |
| 141 | { |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type, |
| 146 | struct ttm_mem_type_manager *man) |
| 147 | { |
| 148 | struct amdgpu_device *adev; |
| 149 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 150 | adev = amdgpu_ttm_adev(bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 151 | |
| 152 | switch (type) { |
| 153 | case TTM_PL_SYSTEM: |
| 154 | /* System memory */ |
| 155 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; |
| 156 | man->available_caching = TTM_PL_MASK_CACHING; |
| 157 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 158 | break; |
| 159 | case TTM_PL_TT: |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 160 | man->func = &amdgpu_gtt_mgr_func; |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 161 | man->gpu_offset = adev->mc.gart_start; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 162 | man->available_caching = TTM_PL_MASK_CACHING; |
| 163 | man->default_caching = TTM_PL_FLAG_CACHED; |
| 164 | man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA; |
| 165 | break; |
| 166 | case TTM_PL_VRAM: |
| 167 | /* "On-card" video ram */ |
Christian König | 6a7f76e | 2016-08-24 15:51:49 +0200 | [diff] [blame] | 168 | man->func = &amdgpu_vram_mgr_func; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 169 | man->gpu_offset = adev->mc.vram_start; |
| 170 | man->flags = TTM_MEMTYPE_FLAG_FIXED | |
| 171 | TTM_MEMTYPE_FLAG_MAPPABLE; |
| 172 | man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC; |
| 173 | man->default_caching = TTM_PL_FLAG_WC; |
| 174 | break; |
| 175 | case AMDGPU_PL_GDS: |
| 176 | case AMDGPU_PL_GWS: |
| 177 | case AMDGPU_PL_OA: |
| 178 | /* On-chip GDS memory*/ |
| 179 | man->func = &ttm_bo_manager_func; |
| 180 | man->gpu_offset = 0; |
| 181 | man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA; |
| 182 | man->available_caching = TTM_PL_FLAG_UNCACHED; |
| 183 | man->default_caching = TTM_PL_FLAG_UNCACHED; |
| 184 | break; |
| 185 | default: |
| 186 | DRM_ERROR("Unsupported memory type %u\n", (unsigned)type); |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void amdgpu_evict_flags(struct ttm_buffer_object *bo, |
| 193 | struct ttm_placement *placement) |
| 194 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 195 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 196 | struct amdgpu_bo *abo; |
Arvind Yadav | 1aaa560 | 2017-07-02 14:43:58 +0530 | [diff] [blame] | 197 | static const struct ttm_place placements = { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 198 | .fpfn = 0, |
| 199 | .lpfn = 0, |
| 200 | .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM |
| 201 | }; |
| 202 | |
| 203 | if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) { |
| 204 | placement->placement = &placements; |
| 205 | placement->busy_placement = &placements; |
| 206 | placement->num_placement = 1; |
| 207 | placement->num_busy_placement = 1; |
| 208 | return; |
| 209 | } |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 210 | abo = container_of(bo, struct amdgpu_bo, tbo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 211 | switch (bo->mem.mem_type) { |
| 212 | case TTM_PL_VRAM: |
Huang Rui | cbcbea9 | 2017-04-11 09:24:56 +0800 | [diff] [blame] | 213 | if (adev->mman.buffer_funcs && |
| 214 | adev->mman.buffer_funcs_ring && |
| 215 | adev->mman.buffer_funcs_ring->ready == false) { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 216 | amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU); |
Christian König | 08291c5 | 2016-09-12 16:06:18 +0200 | [diff] [blame] | 217 | } else { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 218 | amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT); |
Christian König | 08291c5 | 2016-09-12 16:06:18 +0200 | [diff] [blame] | 219 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 220 | break; |
| 221 | case TTM_PL_TT: |
| 222 | default: |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 223 | amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 224 | } |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 225 | *placement = abo->placement; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp) |
| 229 | { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 230 | struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 231 | |
Jérôme Glisse | 054892e | 2016-04-19 09:07:51 -0400 | [diff] [blame] | 232 | if (amdgpu_ttm_tt_get_usermm(bo->ttm)) |
| 233 | return -EPERM; |
Dave Airlie | 28a3965 | 2016-09-30 13:18:26 +1000 | [diff] [blame] | 234 | return drm_vma_node_verify_access(&abo->gem_base.vma_node, |
David Herrmann | d9a1f0b | 2016-09-01 14:48:33 +0200 | [diff] [blame] | 235 | filp->private_data); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static void amdgpu_move_null(struct ttm_buffer_object *bo, |
| 239 | struct ttm_mem_reg *new_mem) |
| 240 | { |
| 241 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 242 | |
| 243 | BUG_ON(old_mem->mm_node != NULL); |
| 244 | *old_mem = *new_mem; |
| 245 | new_mem->mm_node = NULL; |
| 246 | } |
| 247 | |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 248 | static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo, |
| 249 | struct drm_mm_node *mm_node, |
| 250 | struct ttm_mem_reg *mem) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 251 | { |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 252 | uint64_t addr = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 253 | |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 254 | if (mem->mem_type != TTM_PL_TT || |
| 255 | amdgpu_gtt_mgr_is_allocated(mem)) { |
| 256 | addr = mm_node->start << PAGE_SHIFT; |
| 257 | addr += bo->bdev->man[mem->mem_type].gpu_offset; |
| 258 | } |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 259 | return addr; |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static int amdgpu_move_blit(struct ttm_buffer_object *bo, |
| 263 | bool evict, bool no_wait_gpu, |
| 264 | struct ttm_mem_reg *new_mem, |
| 265 | struct ttm_mem_reg *old_mem) |
| 266 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 267 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 268 | struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; |
| 269 | |
| 270 | struct drm_mm_node *old_mm, *new_mm; |
| 271 | uint64_t old_start, old_size, new_start, new_size; |
| 272 | unsigned long num_pages; |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 273 | struct dma_fence *fence = NULL; |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 274 | int r; |
| 275 | |
| 276 | BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0); |
| 277 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 278 | if (!ring->ready) { |
| 279 | DRM_ERROR("Trying to move memory with ring turned off.\n"); |
| 280 | return -EINVAL; |
| 281 | } |
| 282 | |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 283 | old_mm = old_mem->mm_node; |
| 284 | old_size = old_mm->size; |
| 285 | old_start = amdgpu_mm_node_addr(bo, old_mm, old_mem); |
| 286 | |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 287 | new_mm = new_mem->mm_node; |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 288 | new_size = new_mm->size; |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 289 | new_start = amdgpu_mm_node_addr(bo, new_mm, new_mem); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 290 | |
| 291 | num_pages = new_mem->num_pages; |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 292 | mutex_lock(&adev->mman.gtt_window_lock); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 293 | while (num_pages) { |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 294 | unsigned long cur_pages = min(min(old_size, new_size), |
| 295 | (u64)AMDGPU_GTT_MAX_TRANSFER_SIZE); |
| 296 | uint64_t from = old_start, to = new_start; |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 297 | struct dma_fence *next; |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 298 | |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 299 | if (old_mem->mem_type == TTM_PL_TT && |
| 300 | !amdgpu_gtt_mgr_is_allocated(old_mem)) { |
| 301 | r = amdgpu_map_buffer(bo, old_mem, cur_pages, |
| 302 | old_start, 0, ring, &from); |
| 303 | if (r) |
| 304 | goto error; |
| 305 | } |
| 306 | |
| 307 | if (new_mem->mem_type == TTM_PL_TT && |
| 308 | !amdgpu_gtt_mgr_is_allocated(new_mem)) { |
| 309 | r = amdgpu_map_buffer(bo, new_mem, cur_pages, |
| 310 | new_start, 1, ring, &to); |
| 311 | if (r) |
| 312 | goto error; |
| 313 | } |
| 314 | |
| 315 | r = amdgpu_copy_buffer(ring, from, to, |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 316 | cur_pages * PAGE_SIZE, |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 317 | bo->resv, &next, false, true); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 318 | if (r) |
| 319 | goto error; |
| 320 | |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 321 | dma_fence_put(fence); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 322 | fence = next; |
| 323 | |
| 324 | num_pages -= cur_pages; |
| 325 | if (!num_pages) |
| 326 | break; |
| 327 | |
| 328 | old_size -= cur_pages; |
| 329 | if (!old_size) { |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 330 | old_start = amdgpu_mm_node_addr(bo, ++old_mm, old_mem); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 331 | old_size = old_mm->size; |
| 332 | } else { |
| 333 | old_start += cur_pages * PAGE_SIZE; |
| 334 | } |
| 335 | |
| 336 | new_size -= cur_pages; |
| 337 | if (!new_size) { |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 338 | new_start = amdgpu_mm_node_addr(bo, ++new_mm, new_mem); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 339 | new_size = new_mm->size; |
| 340 | } else { |
| 341 | new_start += cur_pages * PAGE_SIZE; |
| 342 | } |
| 343 | } |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 344 | mutex_unlock(&adev->mman.gtt_window_lock); |
Christian König | ce64bc2 | 2016-06-15 13:44:05 +0200 | [diff] [blame] | 345 | |
| 346 | r = ttm_bo_pipeline_move(bo, fence, evict, new_mem); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 347 | dma_fence_put(fence); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 348 | return r; |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 349 | |
| 350 | error: |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 351 | mutex_unlock(&adev->mman.gtt_window_lock); |
| 352 | |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 353 | if (fence) |
Dave Airlie | 220196b | 2016-10-28 11:33:52 +1000 | [diff] [blame] | 354 | dma_fence_wait(fence, false); |
| 355 | dma_fence_put(fence); |
Christian König | 8892f15 | 2016-08-17 10:46:52 +0200 | [diff] [blame] | 356 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo, |
| 360 | bool evict, bool interruptible, |
| 361 | bool no_wait_gpu, |
| 362 | struct ttm_mem_reg *new_mem) |
| 363 | { |
| 364 | struct amdgpu_device *adev; |
| 365 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 366 | struct ttm_mem_reg tmp_mem; |
| 367 | struct ttm_place placements; |
| 368 | struct ttm_placement placement; |
| 369 | int r; |
| 370 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 371 | adev = amdgpu_ttm_adev(bo->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 372 | tmp_mem = *new_mem; |
| 373 | tmp_mem.mm_node = NULL; |
| 374 | placement.num_placement = 1; |
| 375 | placement.placement = &placements; |
| 376 | placement.num_busy_placement = 1; |
| 377 | placement.busy_placement = &placements; |
| 378 | placements.fpfn = 0; |
Christian König | 5e7e839 | 2017-06-30 12:19:42 +0200 | [diff] [blame] | 379 | placements.lpfn = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 380 | placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT; |
| 381 | r = ttm_bo_mem_space(bo, &placement, &tmp_mem, |
| 382 | interruptible, no_wait_gpu); |
| 383 | if (unlikely(r)) { |
| 384 | return r; |
| 385 | } |
| 386 | |
| 387 | r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement); |
| 388 | if (unlikely(r)) { |
| 389 | goto out_cleanup; |
| 390 | } |
| 391 | |
| 392 | r = ttm_tt_bind(bo->ttm, &tmp_mem); |
| 393 | if (unlikely(r)) { |
| 394 | goto out_cleanup; |
| 395 | } |
| 396 | r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem); |
| 397 | if (unlikely(r)) { |
| 398 | goto out_cleanup; |
| 399 | } |
Michel Dänzer | 4e2f0ca | 2016-08-08 12:28:25 +0900 | [diff] [blame] | 400 | r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 401 | out_cleanup: |
| 402 | ttm_bo_mem_put(bo, &tmp_mem); |
| 403 | return r; |
| 404 | } |
| 405 | |
| 406 | static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo, |
| 407 | bool evict, bool interruptible, |
| 408 | bool no_wait_gpu, |
| 409 | struct ttm_mem_reg *new_mem) |
| 410 | { |
| 411 | struct amdgpu_device *adev; |
| 412 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 413 | struct ttm_mem_reg tmp_mem; |
| 414 | struct ttm_placement placement; |
| 415 | struct ttm_place placements; |
| 416 | int r; |
| 417 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 418 | adev = amdgpu_ttm_adev(bo->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 419 | tmp_mem = *new_mem; |
| 420 | tmp_mem.mm_node = NULL; |
| 421 | placement.num_placement = 1; |
| 422 | placement.placement = &placements; |
| 423 | placement.num_busy_placement = 1; |
| 424 | placement.busy_placement = &placements; |
| 425 | placements.fpfn = 0; |
Christian König | 5e7e839 | 2017-06-30 12:19:42 +0200 | [diff] [blame] | 426 | placements.lpfn = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 427 | placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT; |
| 428 | r = ttm_bo_mem_space(bo, &placement, &tmp_mem, |
| 429 | interruptible, no_wait_gpu); |
| 430 | if (unlikely(r)) { |
| 431 | return r; |
| 432 | } |
Michel Dänzer | 4e2f0ca | 2016-08-08 12:28:25 +0900 | [diff] [blame] | 433 | r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 434 | if (unlikely(r)) { |
| 435 | goto out_cleanup; |
| 436 | } |
| 437 | r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem); |
| 438 | if (unlikely(r)) { |
| 439 | goto out_cleanup; |
| 440 | } |
| 441 | out_cleanup: |
| 442 | ttm_bo_mem_put(bo, &tmp_mem); |
| 443 | return r; |
| 444 | } |
| 445 | |
| 446 | static int amdgpu_bo_move(struct ttm_buffer_object *bo, |
| 447 | bool evict, bool interruptible, |
| 448 | bool no_wait_gpu, |
| 449 | struct ttm_mem_reg *new_mem) |
| 450 | { |
| 451 | struct amdgpu_device *adev; |
Michel Dänzer | 104ece9 | 2016-03-28 12:53:02 +0900 | [diff] [blame] | 452 | struct amdgpu_bo *abo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 453 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 454 | int r; |
| 455 | |
Michel Dänzer | 104ece9 | 2016-03-28 12:53:02 +0900 | [diff] [blame] | 456 | /* Can't move a pinned BO */ |
| 457 | abo = container_of(bo, struct amdgpu_bo, tbo); |
| 458 | if (WARN_ON_ONCE(abo->pin_count > 0)) |
| 459 | return -EINVAL; |
| 460 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 461 | adev = amdgpu_ttm_adev(bo->bdev); |
Christian König | dbd5ed6 | 2016-06-21 16:28:14 +0200 | [diff] [blame] | 462 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 463 | if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
| 464 | amdgpu_move_null(bo, new_mem); |
| 465 | return 0; |
| 466 | } |
| 467 | if ((old_mem->mem_type == TTM_PL_TT && |
| 468 | new_mem->mem_type == TTM_PL_SYSTEM) || |
| 469 | (old_mem->mem_type == TTM_PL_SYSTEM && |
| 470 | new_mem->mem_type == TTM_PL_TT)) { |
| 471 | /* bind is enough */ |
| 472 | amdgpu_move_null(bo, new_mem); |
| 473 | return 0; |
| 474 | } |
| 475 | if (adev->mman.buffer_funcs == NULL || |
| 476 | adev->mman.buffer_funcs_ring == NULL || |
| 477 | !adev->mman.buffer_funcs_ring->ready) { |
| 478 | /* use memcpy */ |
| 479 | goto memcpy; |
| 480 | } |
| 481 | |
| 482 | if (old_mem->mem_type == TTM_PL_VRAM && |
| 483 | new_mem->mem_type == TTM_PL_SYSTEM) { |
| 484 | r = amdgpu_move_vram_ram(bo, evict, interruptible, |
| 485 | no_wait_gpu, new_mem); |
| 486 | } else if (old_mem->mem_type == TTM_PL_SYSTEM && |
| 487 | new_mem->mem_type == TTM_PL_VRAM) { |
| 488 | r = amdgpu_move_ram_vram(bo, evict, interruptible, |
| 489 | no_wait_gpu, new_mem); |
| 490 | } else { |
| 491 | r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem); |
| 492 | } |
| 493 | |
| 494 | if (r) { |
| 495 | memcpy: |
Michel Dänzer | 4499f2a | 2016-08-08 12:28:26 +0900 | [diff] [blame] | 496 | r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 497 | if (r) { |
| 498 | return r; |
| 499 | } |
| 500 | } |
| 501 | |
John Brooks | 96cf827 | 2017-06-30 11:31:08 -0400 | [diff] [blame^] | 502 | if (bo->type == ttm_bo_type_device && |
| 503 | new_mem->mem_type == TTM_PL_VRAM && |
| 504 | old_mem->mem_type != TTM_PL_VRAM) { |
| 505 | /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU |
| 506 | * accesses the BO after it's moved. |
| 507 | */ |
| 508 | abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; |
| 509 | } |
| 510 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 511 | /* update statistics */ |
| 512 | atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved); |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 517 | { |
| 518 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 519 | struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 520 | |
| 521 | mem->bus.addr = NULL; |
| 522 | mem->bus.offset = 0; |
| 523 | mem->bus.size = mem->num_pages << PAGE_SHIFT; |
| 524 | mem->bus.base = 0; |
| 525 | mem->bus.is_iomem = false; |
| 526 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 527 | return -EINVAL; |
| 528 | switch (mem->mem_type) { |
| 529 | case TTM_PL_SYSTEM: |
| 530 | /* system memory */ |
| 531 | return 0; |
| 532 | case TTM_PL_TT: |
| 533 | break; |
| 534 | case TTM_PL_VRAM: |
| 535 | mem->bus.offset = mem->start << PAGE_SHIFT; |
| 536 | /* check if it's visible */ |
| 537 | if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size) |
| 538 | return -EINVAL; |
| 539 | mem->bus.base = adev->mc.aper_base; |
| 540 | mem->bus.is_iomem = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 541 | break; |
| 542 | default: |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 549 | { |
| 550 | } |
| 551 | |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 552 | static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo, |
| 553 | unsigned long page_offset) |
| 554 | { |
| 555 | struct drm_mm_node *mm = bo->mem.mm_node; |
| 556 | uint64_t size = mm->size; |
Dave Airlie | 0168778 | 2017-04-07 05:41:42 +1000 | [diff] [blame] | 557 | uint64_t offset = page_offset; |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 558 | |
| 559 | page_offset = do_div(offset, size); |
Christian König | ecdba5d | 2017-04-07 10:40:04 +0200 | [diff] [blame] | 560 | mm += offset; |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 561 | return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset; |
| 562 | } |
| 563 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 564 | /* |
| 565 | * TTM backend functions. |
| 566 | */ |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 567 | struct amdgpu_ttm_gup_task_list { |
| 568 | struct list_head list; |
| 569 | struct task_struct *task; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 570 | }; |
| 571 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 572 | struct amdgpu_ttm_tt { |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 573 | struct ttm_dma_tt ttm; |
| 574 | struct amdgpu_device *adev; |
| 575 | u64 offset; |
| 576 | uint64_t userptr; |
| 577 | struct mm_struct *usermm; |
| 578 | uint32_t userflags; |
| 579 | spinlock_t guptasklock; |
| 580 | struct list_head guptasks; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 581 | atomic_t mmu_invalidations; |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 582 | struct list_head list; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 583 | }; |
| 584 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 585 | int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 586 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 587 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
Lorenzo Stoakes | 768ae30 | 2016-10-13 01:20:16 +0100 | [diff] [blame] | 588 | unsigned int flags = 0; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 589 | unsigned pinned = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 590 | int r; |
| 591 | |
Lorenzo Stoakes | 768ae30 | 2016-10-13 01:20:16 +0100 | [diff] [blame] | 592 | if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY)) |
| 593 | flags |= FOLL_WRITE; |
| 594 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 595 | if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) { |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 596 | /* check that we only use anonymous memory |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 597 | to prevent problems with writeback */ |
| 598 | unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE; |
| 599 | struct vm_area_struct *vma; |
| 600 | |
| 601 | vma = find_vma(gtt->usermm, gtt->userptr); |
| 602 | if (!vma || vma->vm_file || vma->vm_end < end) |
| 603 | return -EPERM; |
| 604 | } |
| 605 | |
| 606 | do { |
| 607 | unsigned num_pages = ttm->num_pages - pinned; |
| 608 | uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 609 | struct page **p = pages + pinned; |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 610 | struct amdgpu_ttm_gup_task_list guptask; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 611 | |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 612 | guptask.task = current; |
| 613 | spin_lock(>t->guptasklock); |
| 614 | list_add(&guptask.list, >t->guptasks); |
| 615 | spin_unlock(>t->guptasklock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 616 | |
Lorenzo Stoakes | 768ae30 | 2016-10-13 01:20:16 +0100 | [diff] [blame] | 617 | r = get_user_pages(userptr, num_pages, flags, p, NULL); |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 618 | |
| 619 | spin_lock(>t->guptasklock); |
| 620 | list_del(&guptask.list); |
| 621 | spin_unlock(>t->guptasklock); |
| 622 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 623 | if (r < 0) |
| 624 | goto release_pages; |
| 625 | |
| 626 | pinned += r; |
| 627 | |
| 628 | } while (pinned < ttm->num_pages); |
| 629 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 630 | return 0; |
| 631 | |
| 632 | release_pages: |
| 633 | release_pages(pages, pinned, 0); |
| 634 | return r; |
| 635 | } |
| 636 | |
| 637 | /* prepare the sg table with the user pages */ |
| 638 | static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm) |
| 639 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 640 | struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 641 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 642 | unsigned nents; |
| 643 | int r; |
| 644 | |
| 645 | int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); |
| 646 | enum dma_data_direction direction = write ? |
| 647 | DMA_BIDIRECTIONAL : DMA_TO_DEVICE; |
| 648 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 649 | r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0, |
| 650 | ttm->num_pages << PAGE_SHIFT, |
| 651 | GFP_KERNEL); |
| 652 | if (r) |
| 653 | goto release_sg; |
| 654 | |
| 655 | r = -ENOMEM; |
| 656 | nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction); |
| 657 | if (nents != ttm->sg->nents) |
| 658 | goto release_sg; |
| 659 | |
| 660 | drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, |
| 661 | gtt->ttm.dma_address, ttm->num_pages); |
| 662 | |
| 663 | return 0; |
| 664 | |
| 665 | release_sg: |
| 666 | kfree(ttm->sg); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 667 | return r; |
| 668 | } |
| 669 | |
| 670 | static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm) |
| 671 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 672 | struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 673 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
monk.liu | dd08fae | 2015-05-07 14:19:18 -0400 | [diff] [blame] | 674 | struct sg_page_iter sg_iter; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 675 | |
| 676 | int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); |
| 677 | enum dma_data_direction direction = write ? |
| 678 | DMA_BIDIRECTIONAL : DMA_TO_DEVICE; |
| 679 | |
| 680 | /* double check that we don't free the table twice */ |
| 681 | if (!ttm->sg->sgl) |
| 682 | return; |
| 683 | |
| 684 | /* free the sg table and pages again */ |
| 685 | dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction); |
| 686 | |
monk.liu | dd08fae | 2015-05-07 14:19:18 -0400 | [diff] [blame] | 687 | for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) { |
| 688 | struct page *page = sg_page_iter_page(&sg_iter); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 689 | if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY)) |
| 690 | set_page_dirty(page); |
| 691 | |
| 692 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 693 | put_page(page); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 694 | } |
| 695 | |
| 696 | sg_free_table(ttm->sg); |
| 697 | } |
| 698 | |
Christian König | 98a7f88 | 2017-06-30 10:41:07 +0200 | [diff] [blame] | 699 | static int amdgpu_ttm_do_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem) |
| 700 | { |
| 701 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 702 | uint64_t flags; |
| 703 | int r; |
| 704 | |
| 705 | spin_lock(>t->adev->gtt_list_lock); |
| 706 | flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, mem); |
| 707 | gtt->offset = (u64)mem->start << PAGE_SHIFT; |
| 708 | r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages, |
| 709 | ttm->pages, gtt->ttm.dma_address, flags); |
| 710 | |
| 711 | if (r) { |
| 712 | DRM_ERROR("failed to bind %lu pages at 0x%08llX\n", |
| 713 | ttm->num_pages, gtt->offset); |
| 714 | goto error_gart_bind; |
| 715 | } |
| 716 | |
| 717 | list_add_tail(>t->list, >t->adev->gtt_list); |
| 718 | error_gart_bind: |
| 719 | spin_unlock(>t->adev->gtt_list_lock); |
| 720 | return r; |
| 721 | |
| 722 | } |
| 723 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 724 | static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm, |
| 725 | struct ttm_mem_reg *bo_mem) |
| 726 | { |
| 727 | struct amdgpu_ttm_tt *gtt = (void*)ttm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 728 | int r; |
| 729 | |
Chunming Zhou | e2f784f | 2015-11-26 16:33:58 +0800 | [diff] [blame] | 730 | if (gtt->userptr) { |
| 731 | r = amdgpu_ttm_tt_pin_userptr(ttm); |
| 732 | if (r) { |
| 733 | DRM_ERROR("failed to pin userptr\n"); |
| 734 | return r; |
| 735 | } |
| 736 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 737 | if (!ttm->num_pages) { |
| 738 | WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n", |
| 739 | ttm->num_pages, bo_mem, ttm); |
| 740 | } |
| 741 | |
| 742 | if (bo_mem->mem_type == AMDGPU_PL_GDS || |
| 743 | bo_mem->mem_type == AMDGPU_PL_GWS || |
| 744 | bo_mem->mem_type == AMDGPU_PL_OA) |
| 745 | return -EINVAL; |
| 746 | |
Christian König | 98a7f88 | 2017-06-30 10:41:07 +0200 | [diff] [blame] | 747 | if (amdgpu_gtt_mgr_is_allocated(bo_mem)) |
| 748 | r = amdgpu_ttm_do_bind(ttm, bo_mem); |
| 749 | |
| 750 | return r; |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | bool amdgpu_ttm_is_bound(struct ttm_tt *ttm) |
| 754 | { |
| 755 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 756 | |
| 757 | return gtt && !list_empty(>t->list); |
| 758 | } |
| 759 | |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 760 | int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem) |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 761 | { |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 762 | struct ttm_tt *ttm = bo->ttm; |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 763 | int r; |
| 764 | |
| 765 | if (!ttm || amdgpu_ttm_is_bound(ttm)) |
| 766 | return 0; |
| 767 | |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 768 | r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo, |
| 769 | NULL, bo_mem); |
| 770 | if (r) { |
| 771 | DRM_ERROR("Failed to allocate GTT address space (%d)\n", r); |
| 772 | return r; |
| 773 | } |
| 774 | |
Christian König | 98a7f88 | 2017-06-30 10:41:07 +0200 | [diff] [blame] | 775 | return amdgpu_ttm_do_bind(ttm, bo_mem); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 776 | } |
| 777 | |
Chunming Zhou | 2c0d731 | 2016-08-30 16:36:25 +0800 | [diff] [blame] | 778 | int amdgpu_ttm_recover_gart(struct amdgpu_device *adev) |
| 779 | { |
| 780 | struct amdgpu_ttm_tt *gtt, *tmp; |
| 781 | struct ttm_mem_reg bo_mem; |
Monk Liu | 1d1a2cd | 2017-04-27 17:14:57 +0800 | [diff] [blame] | 782 | uint64_t flags; |
Chunming Zhou | 2c0d731 | 2016-08-30 16:36:25 +0800 | [diff] [blame] | 783 | int r; |
| 784 | |
| 785 | bo_mem.mem_type = TTM_PL_TT; |
| 786 | spin_lock(&adev->gtt_list_lock); |
| 787 | list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) { |
| 788 | flags = amdgpu_ttm_tt_pte_flags(gtt->adev, >t->ttm.ttm, &bo_mem); |
| 789 | r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages, |
| 790 | gtt->ttm.ttm.pages, gtt->ttm.dma_address, |
| 791 | flags); |
| 792 | if (r) { |
| 793 | spin_unlock(&adev->gtt_list_lock); |
Christian König | 71c76a0 | 2016-09-03 16:18:26 +0200 | [diff] [blame] | 794 | DRM_ERROR("failed to bind %lu pages at 0x%08llX\n", |
| 795 | gtt->ttm.ttm.num_pages, gtt->offset); |
Chunming Zhou | 2c0d731 | 2016-08-30 16:36:25 +0800 | [diff] [blame] | 796 | return r; |
| 797 | } |
| 798 | } |
| 799 | spin_unlock(&adev->gtt_list_lock); |
| 800 | return 0; |
| 801 | } |
| 802 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 803 | static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm) |
| 804 | { |
| 805 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 806 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 807 | |
Christian König | 85a4b57 | 2016-09-22 14:19:50 +0200 | [diff] [blame] | 808 | if (gtt->userptr) |
| 809 | amdgpu_ttm_tt_unpin_userptr(ttm); |
| 810 | |
Christian König | 78ab0a3 | 2016-09-09 15:39:08 +0200 | [diff] [blame] | 811 | if (!amdgpu_ttm_is_bound(ttm)) |
| 812 | return 0; |
| 813 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 814 | /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */ |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 815 | spin_lock(>t->adev->gtt_list_lock); |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 816 | r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages); |
| 817 | if (r) { |
| 818 | DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n", |
| 819 | gtt->ttm.ttm.num_pages, gtt->offset); |
| 820 | goto error_unbind; |
| 821 | } |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 822 | list_del_init(>t->list); |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 823 | error_unbind: |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 824 | spin_unlock(>t->adev->gtt_list_lock); |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 825 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 826 | } |
| 827 | |
| 828 | static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm) |
| 829 | { |
| 830 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 831 | |
| 832 | ttm_dma_tt_fini(>t->ttm); |
| 833 | kfree(gtt); |
| 834 | } |
| 835 | |
| 836 | static struct ttm_backend_func amdgpu_backend_func = { |
| 837 | .bind = &amdgpu_ttm_backend_bind, |
| 838 | .unbind = &amdgpu_ttm_backend_unbind, |
| 839 | .destroy = &amdgpu_ttm_backend_destroy, |
| 840 | }; |
| 841 | |
| 842 | static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev, |
| 843 | unsigned long size, uint32_t page_flags, |
| 844 | struct page *dummy_read_page) |
| 845 | { |
| 846 | struct amdgpu_device *adev; |
| 847 | struct amdgpu_ttm_tt *gtt; |
| 848 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 849 | adev = amdgpu_ttm_adev(bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 850 | |
| 851 | gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL); |
| 852 | if (gtt == NULL) { |
| 853 | return NULL; |
| 854 | } |
| 855 | gtt->ttm.ttm.func = &amdgpu_backend_func; |
| 856 | gtt->adev = adev; |
| 857 | if (ttm_dma_tt_init(>t->ttm, bdev, size, page_flags, dummy_read_page)) { |
| 858 | kfree(gtt); |
| 859 | return NULL; |
| 860 | } |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 861 | INIT_LIST_HEAD(>t->list); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 862 | return >t->ttm.ttm; |
| 863 | } |
| 864 | |
| 865 | static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm) |
| 866 | { |
| 867 | struct amdgpu_device *adev; |
| 868 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 869 | unsigned i; |
| 870 | int r; |
| 871 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
| 872 | |
| 873 | if (ttm->state != tt_unpopulated) |
| 874 | return 0; |
| 875 | |
| 876 | if (gtt && gtt->userptr) { |
Maninder Singh | 5f0b34c | 2015-06-26 13:28:50 +0530 | [diff] [blame] | 877 | ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 878 | if (!ttm->sg) |
| 879 | return -ENOMEM; |
| 880 | |
| 881 | ttm->page_flags |= TTM_PAGE_FLAG_SG; |
| 882 | ttm->state = tt_unbound; |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | if (slave && ttm->sg) { |
| 887 | drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, |
| 888 | gtt->ttm.dma_address, ttm->num_pages); |
| 889 | ttm->state = tt_unbound; |
| 890 | return 0; |
| 891 | } |
| 892 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 893 | adev = amdgpu_ttm_adev(ttm->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 894 | |
| 895 | #ifdef CONFIG_SWIOTLB |
| 896 | if (swiotlb_nr_tbl()) { |
| 897 | return ttm_dma_populate(>t->ttm, adev->dev); |
| 898 | } |
| 899 | #endif |
| 900 | |
| 901 | r = ttm_pool_populate(ttm); |
| 902 | if (r) { |
| 903 | return r; |
| 904 | } |
| 905 | |
| 906 | for (i = 0; i < ttm->num_pages; i++) { |
| 907 | gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i], |
| 908 | 0, PAGE_SIZE, |
| 909 | PCI_DMA_BIDIRECTIONAL); |
| 910 | if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) { |
Rasmus Villemoes | 09ccbb7 | 2016-02-15 19:41:45 +0100 | [diff] [blame] | 911 | while (i--) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 912 | pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i], |
| 913 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 914 | gtt->ttm.dma_address[i] = 0; |
| 915 | } |
| 916 | ttm_pool_unpopulate(ttm); |
| 917 | return -EFAULT; |
| 918 | } |
| 919 | } |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm) |
| 924 | { |
| 925 | struct amdgpu_device *adev; |
| 926 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 927 | unsigned i; |
| 928 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
| 929 | |
| 930 | if (gtt && gtt->userptr) { |
| 931 | kfree(ttm->sg); |
| 932 | ttm->page_flags &= ~TTM_PAGE_FLAG_SG; |
| 933 | return; |
| 934 | } |
| 935 | |
| 936 | if (slave) |
| 937 | return; |
| 938 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 939 | adev = amdgpu_ttm_adev(ttm->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 940 | |
| 941 | #ifdef CONFIG_SWIOTLB |
| 942 | if (swiotlb_nr_tbl()) { |
| 943 | ttm_dma_unpopulate(>t->ttm, adev->dev); |
| 944 | return; |
| 945 | } |
| 946 | #endif |
| 947 | |
| 948 | for (i = 0; i < ttm->num_pages; i++) { |
| 949 | if (gtt->ttm.dma_address[i]) { |
| 950 | pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i], |
| 951 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | ttm_pool_unpopulate(ttm); |
| 956 | } |
| 957 | |
| 958 | int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr, |
| 959 | uint32_t flags) |
| 960 | { |
| 961 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 962 | |
| 963 | if (gtt == NULL) |
| 964 | return -EINVAL; |
| 965 | |
| 966 | gtt->userptr = addr; |
| 967 | gtt->usermm = current->mm; |
| 968 | gtt->userflags = flags; |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 969 | spin_lock_init(>t->guptasklock); |
| 970 | INIT_LIST_HEAD(>t->guptasks); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 971 | atomic_set(>t->mmu_invalidations, 0); |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 972 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 976 | struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 977 | { |
| 978 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 979 | |
| 980 | if (gtt == NULL) |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 981 | return NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 982 | |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 983 | return gtt->usermm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 984 | } |
| 985 | |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 986 | bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start, |
| 987 | unsigned long end) |
| 988 | { |
| 989 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 990 | struct amdgpu_ttm_gup_task_list *entry; |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 991 | unsigned long size; |
| 992 | |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 993 | if (gtt == NULL || !gtt->userptr) |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 994 | return false; |
| 995 | |
| 996 | size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE; |
| 997 | if (gtt->userptr > end || gtt->userptr + size <= start) |
| 998 | return false; |
| 999 | |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 1000 | spin_lock(>t->guptasklock); |
| 1001 | list_for_each_entry(entry, >t->guptasks, list) { |
| 1002 | if (entry->task == current) { |
| 1003 | spin_unlock(>t->guptasklock); |
| 1004 | return false; |
| 1005 | } |
| 1006 | } |
| 1007 | spin_unlock(>t->guptasklock); |
| 1008 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 1009 | atomic_inc(>t->mmu_invalidations); |
| 1010 | |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 1011 | return true; |
| 1012 | } |
| 1013 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 1014 | bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm, |
| 1015 | int *last_invalidated) |
| 1016 | { |
| 1017 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 1018 | int prev_invalidated = *last_invalidated; |
| 1019 | |
| 1020 | *last_invalidated = atomic_read(>t->mmu_invalidations); |
| 1021 | return prev_invalidated != *last_invalidated; |
| 1022 | } |
| 1023 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1024 | bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm) |
| 1025 | { |
| 1026 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 1027 | |
| 1028 | if (gtt == NULL) |
| 1029 | return false; |
| 1030 | |
| 1031 | return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); |
| 1032 | } |
| 1033 | |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1034 | uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1035 | struct ttm_mem_reg *mem) |
| 1036 | { |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1037 | uint64_t flags = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1038 | |
| 1039 | if (mem && mem->mem_type != TTM_PL_SYSTEM) |
| 1040 | flags |= AMDGPU_PTE_VALID; |
| 1041 | |
Christian König | 6d99905 | 2015-12-04 13:32:55 +0100 | [diff] [blame] | 1042 | if (mem && mem->mem_type == TTM_PL_TT) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1043 | flags |= AMDGPU_PTE_SYSTEM; |
| 1044 | |
Christian König | 6d99905 | 2015-12-04 13:32:55 +0100 | [diff] [blame] | 1045 | if (ttm->caching_state == tt_cached) |
| 1046 | flags |= AMDGPU_PTE_SNOOPED; |
| 1047 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1048 | |
Alex Xie | 4b98e0c | 2017-02-14 12:31:36 -0500 | [diff] [blame] | 1049 | flags |= adev->gart.gart_pte_flags; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1050 | flags |= AMDGPU_PTE_READABLE; |
| 1051 | |
| 1052 | if (!amdgpu_ttm_tt_is_readonly(ttm)) |
| 1053 | flags |= AMDGPU_PTE_WRITEABLE; |
| 1054 | |
| 1055 | return flags; |
| 1056 | } |
| 1057 | |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1058 | static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, |
| 1059 | const struct ttm_place *place) |
| 1060 | { |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1061 | unsigned long num_pages = bo->mem.num_pages; |
| 1062 | struct drm_mm_node *node = bo->mem.mm_node; |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1063 | |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1064 | if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET) |
| 1065 | return ttm_bo_eviction_valuable(bo, place); |
| 1066 | |
| 1067 | switch (bo->mem.mem_type) { |
| 1068 | case TTM_PL_TT: |
| 1069 | return true; |
| 1070 | |
| 1071 | case TTM_PL_VRAM: |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1072 | /* Check each drm MM node individually */ |
| 1073 | while (num_pages) { |
| 1074 | if (place->fpfn < (node->start + node->size) && |
| 1075 | !(place->lpfn && place->lpfn <= node->start)) |
| 1076 | return true; |
| 1077 | |
| 1078 | num_pages -= node->size; |
| 1079 | ++node; |
| 1080 | } |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1081 | break; |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1082 | |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1083 | default: |
| 1084 | break; |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | return ttm_bo_eviction_valuable(bo, place); |
| 1088 | } |
| 1089 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1090 | static struct ttm_bo_driver amdgpu_bo_driver = { |
| 1091 | .ttm_tt_create = &amdgpu_ttm_tt_create, |
| 1092 | .ttm_tt_populate = &amdgpu_ttm_tt_populate, |
| 1093 | .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate, |
| 1094 | .invalidate_caches = &amdgpu_invalidate_caches, |
| 1095 | .init_mem_type = &amdgpu_init_mem_type, |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1096 | .eviction_valuable = amdgpu_ttm_bo_eviction_valuable, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1097 | .evict_flags = &amdgpu_evict_flags, |
| 1098 | .move = &amdgpu_bo_move, |
| 1099 | .verify_access = &amdgpu_verify_access, |
| 1100 | .move_notify = &amdgpu_bo_move_notify, |
| 1101 | .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify, |
| 1102 | .io_mem_reserve = &amdgpu_ttm_io_mem_reserve, |
| 1103 | .io_mem_free = &amdgpu_ttm_io_mem_free, |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 1104 | .io_mem_pfn = amdgpu_ttm_io_mem_pfn, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1105 | }; |
| 1106 | |
| 1107 | int amdgpu_ttm_init(struct amdgpu_device *adev) |
| 1108 | { |
Christian König | 36d3837 | 2017-07-07 13:17:45 +0200 | [diff] [blame] | 1109 | uint64_t gtt_size; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1110 | int r; |
John Brooks | 218b5dc | 2017-06-27 22:33:17 -0400 | [diff] [blame] | 1111 | u64 vis_vram_limit; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1112 | |
Alex Deucher | 70b5c5a | 2016-11-15 16:55:53 -0500 | [diff] [blame] | 1113 | r = amdgpu_ttm_global_init(adev); |
| 1114 | if (r) { |
| 1115 | return r; |
| 1116 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1117 | /* No others user of address space so set it to 0 */ |
| 1118 | r = ttm_bo_device_init(&adev->mman.bdev, |
| 1119 | adev->mman.bo_global_ref.ref.object, |
| 1120 | &amdgpu_bo_driver, |
| 1121 | adev->ddev->anon_inode->i_mapping, |
| 1122 | DRM_FILE_PAGE_OFFSET, |
| 1123 | adev->need_dma32); |
| 1124 | if (r) { |
| 1125 | DRM_ERROR("failed initializing buffer object driver(%d).\n", r); |
| 1126 | return r; |
| 1127 | } |
| 1128 | adev->mman.initialized = true; |
| 1129 | r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM, |
| 1130 | adev->mc.real_vram_size >> PAGE_SHIFT); |
| 1131 | if (r) { |
| 1132 | DRM_ERROR("Failed initializing VRAM heap.\n"); |
| 1133 | return r; |
| 1134 | } |
John Brooks | 218b5dc | 2017-06-27 22:33:17 -0400 | [diff] [blame] | 1135 | |
| 1136 | /* Reduce size of CPU-visible VRAM if requested */ |
| 1137 | vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024; |
| 1138 | if (amdgpu_vis_vram_limit > 0 && |
| 1139 | vis_vram_limit <= adev->mc.visible_vram_size) |
| 1140 | adev->mc.visible_vram_size = vis_vram_limit; |
| 1141 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1142 | /* Change the size here instead of the init above so only lpfn is affected */ |
| 1143 | amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size); |
| 1144 | |
Huang Rui | 916910a | 2017-05-31 10:35:42 +0800 | [diff] [blame] | 1145 | r = amdgpu_bo_create(adev, adev->mc.stolen_size, PAGE_SIZE, true, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 1146 | AMDGPU_GEM_DOMAIN_VRAM, |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 1147 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED | |
| 1148 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 1149 | NULL, NULL, &adev->stollen_vga_memory); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1150 | if (r) { |
| 1151 | return r; |
| 1152 | } |
| 1153 | r = amdgpu_bo_reserve(adev->stollen_vga_memory, false); |
| 1154 | if (r) |
| 1155 | return r; |
| 1156 | r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL); |
| 1157 | amdgpu_bo_unreserve(adev->stollen_vga_memory); |
| 1158 | if (r) { |
| 1159 | amdgpu_bo_unref(&adev->stollen_vga_memory); |
| 1160 | return r; |
| 1161 | } |
| 1162 | DRM_INFO("amdgpu: %uM of VRAM memory ready\n", |
| 1163 | (unsigned) (adev->mc.real_vram_size / (1024 * 1024))); |
Christian König | 36d3837 | 2017-07-07 13:17:45 +0200 | [diff] [blame] | 1164 | |
| 1165 | if (amdgpu_gtt_size == -1) |
| 1166 | gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20), |
| 1167 | adev->mc.mc_vram_size); |
| 1168 | else |
| 1169 | gtt_size = (uint64_t)amdgpu_gtt_size << 20; |
| 1170 | r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1171 | if (r) { |
| 1172 | DRM_ERROR("Failed initializing GTT heap.\n"); |
| 1173 | return r; |
| 1174 | } |
| 1175 | DRM_INFO("amdgpu: %uM of GTT memory ready.\n", |
Christian König | 36d3837 | 2017-07-07 13:17:45 +0200 | [diff] [blame] | 1176 | (unsigned)(gtt_size / (1024 * 1024))); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1177 | |
| 1178 | adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT; |
| 1179 | adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT; |
| 1180 | adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT; |
| 1181 | adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT; |
| 1182 | adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT; |
| 1183 | adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT; |
| 1184 | adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT; |
| 1185 | adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT; |
| 1186 | adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT; |
| 1187 | /* GDS Memory */ |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1188 | if (adev->gds.mem.total_size) { |
| 1189 | r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS, |
| 1190 | adev->gds.mem.total_size >> PAGE_SHIFT); |
| 1191 | if (r) { |
| 1192 | DRM_ERROR("Failed initializing GDS heap.\n"); |
| 1193 | return r; |
| 1194 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | /* GWS */ |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1198 | if (adev->gds.gws.total_size) { |
| 1199 | r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS, |
| 1200 | adev->gds.gws.total_size >> PAGE_SHIFT); |
| 1201 | if (r) { |
| 1202 | DRM_ERROR("Failed initializing gws heap.\n"); |
| 1203 | return r; |
| 1204 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1205 | } |
| 1206 | |
| 1207 | /* OA */ |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1208 | if (adev->gds.oa.total_size) { |
| 1209 | r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA, |
| 1210 | adev->gds.oa.total_size >> PAGE_SHIFT); |
| 1211 | if (r) { |
| 1212 | DRM_ERROR("Failed initializing oa heap.\n"); |
| 1213 | return r; |
| 1214 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | r = amdgpu_ttm_debugfs_init(adev); |
| 1218 | if (r) { |
| 1219 | DRM_ERROR("Failed to init debugfs\n"); |
| 1220 | return r; |
| 1221 | } |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | void amdgpu_ttm_fini(struct amdgpu_device *adev) |
| 1226 | { |
| 1227 | int r; |
| 1228 | |
| 1229 | if (!adev->mman.initialized) |
| 1230 | return; |
| 1231 | amdgpu_ttm_debugfs_fini(adev); |
| 1232 | if (adev->stollen_vga_memory) { |
Michel Dänzer | c81a1a7 | 2017-04-28 17:28:14 +0900 | [diff] [blame] | 1233 | r = amdgpu_bo_reserve(adev->stollen_vga_memory, true); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1234 | if (r == 0) { |
| 1235 | amdgpu_bo_unpin(adev->stollen_vga_memory); |
| 1236 | amdgpu_bo_unreserve(adev->stollen_vga_memory); |
| 1237 | } |
| 1238 | amdgpu_bo_unref(&adev->stollen_vga_memory); |
| 1239 | } |
| 1240 | ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM); |
| 1241 | ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT); |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1242 | if (adev->gds.mem.total_size) |
| 1243 | ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS); |
| 1244 | if (adev->gds.gws.total_size) |
| 1245 | ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS); |
| 1246 | if (adev->gds.oa.total_size) |
| 1247 | ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1248 | ttm_bo_device_release(&adev->mman.bdev); |
| 1249 | amdgpu_gart_fini(adev); |
| 1250 | amdgpu_ttm_global_fini(adev); |
| 1251 | adev->mman.initialized = false; |
| 1252 | DRM_INFO("amdgpu: ttm finalized\n"); |
| 1253 | } |
| 1254 | |
| 1255 | /* this should only be called at bootup or when userspace |
| 1256 | * isn't running */ |
| 1257 | void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size) |
| 1258 | { |
| 1259 | struct ttm_mem_type_manager *man; |
| 1260 | |
| 1261 | if (!adev->mman.initialized) |
| 1262 | return; |
| 1263 | |
| 1264 | man = &adev->mman.bdev.man[TTM_PL_VRAM]; |
| 1265 | /* this just adjusts TTM size idea, which sets lpfn to the correct value */ |
| 1266 | man->size = size >> PAGE_SHIFT; |
| 1267 | } |
| 1268 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1269 | int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1270 | { |
| 1271 | struct drm_file *file_priv; |
| 1272 | struct amdgpu_device *adev; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1273 | |
Christian König | e176fe17 | 2015-05-27 10:22:47 +0200 | [diff] [blame] | 1274 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1275 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1276 | |
| 1277 | file_priv = filp->private_data; |
| 1278 | adev = file_priv->minor->dev->dev_private; |
Christian König | e176fe17 | 2015-05-27 10:22:47 +0200 | [diff] [blame] | 1279 | if (adev == NULL) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1280 | return -EINVAL; |
Christian König | e176fe17 | 2015-05-27 10:22:47 +0200 | [diff] [blame] | 1281 | |
| 1282 | return ttm_bo_mmap(filp, vma, &adev->mman.bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1283 | } |
| 1284 | |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 1285 | static int amdgpu_map_buffer(struct ttm_buffer_object *bo, |
| 1286 | struct ttm_mem_reg *mem, unsigned num_pages, |
| 1287 | uint64_t offset, unsigned window, |
| 1288 | struct amdgpu_ring *ring, |
| 1289 | uint64_t *addr) |
| 1290 | { |
| 1291 | struct amdgpu_ttm_tt *gtt = (void *)bo->ttm; |
| 1292 | struct amdgpu_device *adev = ring->adev; |
| 1293 | struct ttm_tt *ttm = bo->ttm; |
| 1294 | struct amdgpu_job *job; |
| 1295 | unsigned num_dw, num_bytes; |
| 1296 | dma_addr_t *dma_address; |
| 1297 | struct dma_fence *fence; |
| 1298 | uint64_t src_addr, dst_addr; |
| 1299 | uint64_t flags; |
| 1300 | int r; |
| 1301 | |
| 1302 | BUG_ON(adev->mman.buffer_funcs->copy_max_bytes < |
| 1303 | AMDGPU_GTT_MAX_TRANSFER_SIZE * 8); |
| 1304 | |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 1305 | *addr = adev->mc.gart_start; |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 1306 | *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE * |
| 1307 | AMDGPU_GPU_PAGE_SIZE; |
| 1308 | |
| 1309 | num_dw = adev->mman.buffer_funcs->copy_num_dw; |
| 1310 | while (num_dw & 0x7) |
| 1311 | num_dw++; |
| 1312 | |
| 1313 | num_bytes = num_pages * 8; |
| 1314 | |
| 1315 | r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job); |
| 1316 | if (r) |
| 1317 | return r; |
| 1318 | |
| 1319 | src_addr = num_dw * 4; |
| 1320 | src_addr += job->ibs[0].gpu_addr; |
| 1321 | |
| 1322 | dst_addr = adev->gart.table_addr; |
| 1323 | dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8; |
| 1324 | amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, |
| 1325 | dst_addr, num_bytes); |
| 1326 | |
| 1327 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
| 1328 | WARN_ON(job->ibs[0].length_dw > num_dw); |
| 1329 | |
| 1330 | dma_address = >t->ttm.dma_address[offset >> PAGE_SHIFT]; |
| 1331 | flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem); |
| 1332 | r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags, |
| 1333 | &job->ibs[0].ptr[num_dw]); |
| 1334 | if (r) |
| 1335 | goto error_free; |
| 1336 | |
| 1337 | r = amdgpu_job_submit(job, ring, &adev->mman.entity, |
| 1338 | AMDGPU_FENCE_OWNER_UNDEFINED, &fence); |
| 1339 | if (r) |
| 1340 | goto error_free; |
| 1341 | |
| 1342 | dma_fence_put(fence); |
| 1343 | |
| 1344 | return r; |
| 1345 | |
| 1346 | error_free: |
| 1347 | amdgpu_job_free(job); |
| 1348 | return r; |
| 1349 | } |
| 1350 | |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 1351 | int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset, |
| 1352 | uint64_t dst_offset, uint32_t byte_count, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1353 | struct reservation_object *resv, |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 1354 | struct dma_fence **fence, bool direct_submit, |
| 1355 | bool vm_needs_flush) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1356 | { |
| 1357 | struct amdgpu_device *adev = ring->adev; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1358 | struct amdgpu_job *job; |
| 1359 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1360 | uint32_t max_bytes; |
| 1361 | unsigned num_loops, num_dw; |
| 1362 | unsigned i; |
| 1363 | int r; |
| 1364 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1365 | max_bytes = adev->mman.buffer_funcs->copy_max_bytes; |
| 1366 | num_loops = DIV_ROUND_UP(byte_count, max_bytes); |
| 1367 | num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw; |
| 1368 | |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1369 | /* for IB padding */ |
| 1370 | while (num_dw & 0x7) |
| 1371 | num_dw++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1372 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1373 | r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job); |
| 1374 | if (r) |
Chunming Zhou | 9066b0c | 2015-08-25 15:12:26 +0800 | [diff] [blame] | 1375 | return r; |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1376 | |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 1377 | job->vm_needs_flush = vm_needs_flush; |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1378 | if (resv) { |
Christian König | e86f9ce | 2016-02-08 12:13:05 +0100 | [diff] [blame] | 1379 | r = amdgpu_sync_resv(adev, &job->sync, resv, |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1380 | AMDGPU_FENCE_OWNER_UNDEFINED); |
| 1381 | if (r) { |
| 1382 | DRM_ERROR("sync failed (%d).\n", r); |
| 1383 | goto error_free; |
| 1384 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1385 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1386 | |
| 1387 | for (i = 0; i < num_loops; i++) { |
| 1388 | uint32_t cur_size_in_bytes = min(byte_count, max_bytes); |
| 1389 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1390 | amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset, |
| 1391 | dst_offset, cur_size_in_bytes); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1392 | |
| 1393 | src_offset += cur_size_in_bytes; |
| 1394 | dst_offset += cur_size_in_bytes; |
| 1395 | byte_count -= cur_size_in_bytes; |
| 1396 | } |
| 1397 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1398 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
| 1399 | WARN_ON(job->ibs[0].length_dw > num_dw); |
Chunming Zhou | e24db98 | 2016-08-15 10:46:04 +0800 | [diff] [blame] | 1400 | if (direct_submit) { |
| 1401 | r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, |
Junwei Zhang | 50ddc75 | 2017-01-23 16:30:38 +0800 | [diff] [blame] | 1402 | NULL, fence); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1403 | job->fence = dma_fence_get(*fence); |
Chunming Zhou | e24db98 | 2016-08-15 10:46:04 +0800 | [diff] [blame] | 1404 | if (r) |
| 1405 | DRM_ERROR("Error scheduling IBs (%d)\n", r); |
| 1406 | amdgpu_job_free(job); |
| 1407 | } else { |
| 1408 | r = amdgpu_job_submit(job, ring, &adev->mman.entity, |
| 1409 | AMDGPU_FENCE_OWNER_UNDEFINED, fence); |
| 1410 | if (r) |
| 1411 | goto error_free; |
| 1412 | } |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1413 | |
Chunming Zhou | e24db98 | 2016-08-15 10:46:04 +0800 | [diff] [blame] | 1414 | return r; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1415 | |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1416 | error_free: |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1417 | amdgpu_job_free(job); |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1418 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1419 | } |
| 1420 | |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1421 | int amdgpu_fill_buffer(struct amdgpu_bo *bo, |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1422 | uint32_t src_data, |
| 1423 | struct reservation_object *resv, |
| 1424 | struct dma_fence **fence) |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1425 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 1426 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1427 | uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1428 | struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; |
| 1429 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1430 | struct drm_mm_node *mm_node; |
| 1431 | unsigned long num_pages; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1432 | unsigned int num_loops, num_dw; |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1433 | |
| 1434 | struct amdgpu_job *job; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1435 | int r; |
| 1436 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1437 | if (!ring->ready) { |
| 1438 | DRM_ERROR("Trying to clear memory with ring turned off.\n"); |
| 1439 | return -EINVAL; |
| 1440 | } |
| 1441 | |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 1442 | if (bo->tbo.mem.mem_type == TTM_PL_TT) { |
| 1443 | r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem); |
| 1444 | if (r) |
| 1445 | return r; |
| 1446 | } |
| 1447 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1448 | num_pages = bo->tbo.num_pages; |
| 1449 | mm_node = bo->tbo.mem.mm_node; |
| 1450 | num_loops = 0; |
| 1451 | while (num_pages) { |
| 1452 | uint32_t byte_count = mm_node->size << PAGE_SHIFT; |
| 1453 | |
| 1454 | num_loops += DIV_ROUND_UP(byte_count, max_bytes); |
| 1455 | num_pages -= mm_node->size; |
| 1456 | ++mm_node; |
| 1457 | } |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1458 | num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw; |
| 1459 | |
| 1460 | /* for IB padding */ |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1461 | num_dw += 64; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1462 | |
| 1463 | r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job); |
| 1464 | if (r) |
| 1465 | return r; |
| 1466 | |
| 1467 | if (resv) { |
| 1468 | r = amdgpu_sync_resv(adev, &job->sync, resv, |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1469 | AMDGPU_FENCE_OWNER_UNDEFINED); |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1470 | if (r) { |
| 1471 | DRM_ERROR("sync failed (%d).\n", r); |
| 1472 | goto error_free; |
| 1473 | } |
| 1474 | } |
| 1475 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1476 | num_pages = bo->tbo.num_pages; |
| 1477 | mm_node = bo->tbo.mem.mm_node; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1478 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1479 | while (num_pages) { |
| 1480 | uint32_t byte_count = mm_node->size << PAGE_SHIFT; |
| 1481 | uint64_t dst_addr; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1482 | |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 1483 | dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem); |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1484 | while (byte_count) { |
| 1485 | uint32_t cur_size_in_bytes = min(byte_count, max_bytes); |
| 1486 | |
| 1487 | amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data, |
| 1488 | dst_addr, cur_size_in_bytes); |
| 1489 | |
| 1490 | dst_addr += cur_size_in_bytes; |
| 1491 | byte_count -= cur_size_in_bytes; |
| 1492 | } |
| 1493 | |
| 1494 | num_pages -= mm_node->size; |
| 1495 | ++mm_node; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
| 1499 | WARN_ON(job->ibs[0].length_dw > num_dw); |
| 1500 | r = amdgpu_job_submit(job, ring, &adev->mman.entity, |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1501 | AMDGPU_FENCE_OWNER_UNDEFINED, fence); |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1502 | if (r) |
| 1503 | goto error_free; |
| 1504 | |
| 1505 | return 0; |
| 1506 | |
| 1507 | error_free: |
| 1508 | amdgpu_job_free(job); |
| 1509 | return r; |
| 1510 | } |
| 1511 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1512 | #if defined(CONFIG_DEBUG_FS) |
| 1513 | |
Chunming Zhou | 05a72a2 | 2017-04-13 16:16:51 +0800 | [diff] [blame] | 1514 | extern void amdgpu_gtt_mgr_print(struct seq_file *m, struct ttm_mem_type_manager |
| 1515 | *man); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1516 | static int amdgpu_mm_dump_table(struct seq_file *m, void *data) |
| 1517 | { |
| 1518 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
| 1519 | unsigned ttm_pl = *(int *)node->info_ent->data; |
| 1520 | struct drm_device *dev = node->minor->dev; |
| 1521 | struct amdgpu_device *adev = dev->dev_private; |
| 1522 | struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1523 | struct ttm_bo_global *glob = adev->mman.bdev.glob; |
Daniel Vetter | b5c3714 | 2016-12-29 12:09:24 +0100 | [diff] [blame] | 1524 | struct drm_printer p = drm_seq_file_printer(m); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1525 | |
| 1526 | spin_lock(&glob->lru_lock); |
Daniel Vetter | b5c3714 | 2016-12-29 12:09:24 +0100 | [diff] [blame] | 1527 | drm_mm_print(mm, &p); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1528 | spin_unlock(&glob->lru_lock); |
Chunming Zhou | 05a72a2 | 2017-04-13 16:16:51 +0800 | [diff] [blame] | 1529 | switch (ttm_pl) { |
| 1530 | case TTM_PL_VRAM: |
Arnd Bergmann | e1b35f6 | 2015-11-10 13:17:55 +0100 | [diff] [blame] | 1531 | seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n", |
Chunming Zhou | a2ef8a9 | 2015-09-22 18:20:50 +0800 | [diff] [blame] | 1532 | adev->mman.bdev.man[ttm_pl].size, |
Arnd Bergmann | e1b35f6 | 2015-11-10 13:17:55 +0100 | [diff] [blame] | 1533 | (u64)atomic64_read(&adev->vram_usage) >> 20, |
| 1534 | (u64)atomic64_read(&adev->vram_vis_usage) >> 20); |
Chunming Zhou | 05a72a2 | 2017-04-13 16:16:51 +0800 | [diff] [blame] | 1535 | break; |
| 1536 | case TTM_PL_TT: |
| 1537 | amdgpu_gtt_mgr_print(m, &adev->mman.bdev.man[TTM_PL_TT]); |
| 1538 | break; |
| 1539 | } |
Daniel Vetter | b5c3714 | 2016-12-29 12:09:24 +0100 | [diff] [blame] | 1540 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1541 | } |
| 1542 | |
| 1543 | static int ttm_pl_vram = TTM_PL_VRAM; |
| 1544 | static int ttm_pl_tt = TTM_PL_TT; |
| 1545 | |
Nils Wallménius | 06ab683 | 2016-05-02 12:46:15 -0400 | [diff] [blame] | 1546 | static const struct drm_info_list amdgpu_ttm_debugfs_list[] = { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1547 | {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram}, |
| 1548 | {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt}, |
| 1549 | {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL}, |
| 1550 | #ifdef CONFIG_SWIOTLB |
| 1551 | {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL} |
| 1552 | #endif |
| 1553 | }; |
| 1554 | |
| 1555 | static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf, |
| 1556 | size_t size, loff_t *pos) |
| 1557 | { |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 1558 | struct amdgpu_device *adev = file_inode(f)->i_private; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1559 | ssize_t result = 0; |
| 1560 | int r; |
| 1561 | |
| 1562 | if (size & 0x3 || *pos & 0x3) |
| 1563 | return -EINVAL; |
| 1564 | |
Tom St Denis | 9156e72 | 2017-05-23 11:35:22 -0400 | [diff] [blame] | 1565 | if (*pos >= adev->mc.mc_vram_size) |
| 1566 | return -ENXIO; |
| 1567 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1568 | while (size) { |
| 1569 | unsigned long flags; |
| 1570 | uint32_t value; |
| 1571 | |
| 1572 | if (*pos >= adev->mc.mc_vram_size) |
| 1573 | return result; |
| 1574 | |
| 1575 | spin_lock_irqsave(&adev->mmio_idx_lock, flags); |
| 1576 | WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000); |
| 1577 | WREG32(mmMM_INDEX_HI, *pos >> 31); |
| 1578 | value = RREG32(mmMM_DATA); |
| 1579 | spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); |
| 1580 | |
| 1581 | r = put_user(value, (uint32_t *)buf); |
| 1582 | if (r) |
| 1583 | return r; |
| 1584 | |
| 1585 | result += 4; |
| 1586 | buf += 4; |
| 1587 | *pos += 4; |
| 1588 | size -= 4; |
| 1589 | } |
| 1590 | |
| 1591 | return result; |
| 1592 | } |
| 1593 | |
| 1594 | static const struct file_operations amdgpu_ttm_vram_fops = { |
| 1595 | .owner = THIS_MODULE, |
| 1596 | .read = amdgpu_ttm_vram_read, |
| 1597 | .llseek = default_llseek |
| 1598 | }; |
| 1599 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1600 | #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS |
| 1601 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1602 | static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf, |
| 1603 | size_t size, loff_t *pos) |
| 1604 | { |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 1605 | struct amdgpu_device *adev = file_inode(f)->i_private; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1606 | ssize_t result = 0; |
| 1607 | int r; |
| 1608 | |
| 1609 | while (size) { |
| 1610 | loff_t p = *pos / PAGE_SIZE; |
| 1611 | unsigned off = *pos & ~PAGE_MASK; |
| 1612 | size_t cur_size = min_t(size_t, size, PAGE_SIZE - off); |
| 1613 | struct page *page; |
| 1614 | void *ptr; |
| 1615 | |
| 1616 | if (p >= adev->gart.num_cpu_pages) |
| 1617 | return result; |
| 1618 | |
| 1619 | page = adev->gart.pages[p]; |
| 1620 | if (page) { |
| 1621 | ptr = kmap(page); |
| 1622 | ptr += off; |
| 1623 | |
| 1624 | r = copy_to_user(buf, ptr, cur_size); |
| 1625 | kunmap(adev->gart.pages[p]); |
| 1626 | } else |
| 1627 | r = clear_user(buf, cur_size); |
| 1628 | |
| 1629 | if (r) |
| 1630 | return -EFAULT; |
| 1631 | |
| 1632 | result += cur_size; |
| 1633 | buf += cur_size; |
| 1634 | *pos += cur_size; |
| 1635 | size -= cur_size; |
| 1636 | } |
| 1637 | |
| 1638 | return result; |
| 1639 | } |
| 1640 | |
| 1641 | static const struct file_operations amdgpu_ttm_gtt_fops = { |
| 1642 | .owner = THIS_MODULE, |
| 1643 | .read = amdgpu_ttm_gtt_read, |
| 1644 | .llseek = default_llseek |
| 1645 | }; |
| 1646 | |
| 1647 | #endif |
| 1648 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1649 | #endif |
| 1650 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1651 | static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev) |
| 1652 | { |
| 1653 | #if defined(CONFIG_DEBUG_FS) |
| 1654 | unsigned count; |
| 1655 | |
| 1656 | struct drm_minor *minor = adev->ddev->primary; |
| 1657 | struct dentry *ent, *root = minor->debugfs_root; |
| 1658 | |
| 1659 | ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root, |
| 1660 | adev, &amdgpu_ttm_vram_fops); |
| 1661 | if (IS_ERR(ent)) |
| 1662 | return PTR_ERR(ent); |
| 1663 | i_size_write(ent->d_inode, adev->mc.mc_vram_size); |
| 1664 | adev->mman.vram = ent; |
| 1665 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1666 | #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1667 | ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root, |
| 1668 | adev, &amdgpu_ttm_gtt_fops); |
| 1669 | if (IS_ERR(ent)) |
| 1670 | return PTR_ERR(ent); |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 1671 | i_size_write(ent->d_inode, adev->mc.gart_size); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1672 | adev->mman.gtt = ent; |
| 1673 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1674 | #endif |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1675 | count = ARRAY_SIZE(amdgpu_ttm_debugfs_list); |
| 1676 | |
| 1677 | #ifdef CONFIG_SWIOTLB |
| 1678 | if (!swiotlb_nr_tbl()) |
| 1679 | --count; |
| 1680 | #endif |
| 1681 | |
| 1682 | return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count); |
| 1683 | #else |
| 1684 | |
| 1685 | return 0; |
| 1686 | #endif |
| 1687 | } |
| 1688 | |
| 1689 | static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev) |
| 1690 | { |
| 1691 | #if defined(CONFIG_DEBUG_FS) |
| 1692 | |
| 1693 | debugfs_remove(adev->mman.vram); |
| 1694 | adev->mman.vram = NULL; |
| 1695 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1696 | #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1697 | debugfs_remove(adev->mman.gtt); |
| 1698 | adev->mman.gtt = NULL; |
| 1699 | #endif |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1700 | |
| 1701 | #endif |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1702 | } |