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 | |
| 502 | /* update statistics */ |
| 503 | atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved); |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 508 | { |
| 509 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 510 | struct amdgpu_device *adev = amdgpu_ttm_adev(bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 511 | |
| 512 | mem->bus.addr = NULL; |
| 513 | mem->bus.offset = 0; |
| 514 | mem->bus.size = mem->num_pages << PAGE_SHIFT; |
| 515 | mem->bus.base = 0; |
| 516 | mem->bus.is_iomem = false; |
| 517 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 518 | return -EINVAL; |
| 519 | switch (mem->mem_type) { |
| 520 | case TTM_PL_SYSTEM: |
| 521 | /* system memory */ |
| 522 | return 0; |
| 523 | case TTM_PL_TT: |
| 524 | break; |
| 525 | case TTM_PL_VRAM: |
| 526 | mem->bus.offset = mem->start << PAGE_SHIFT; |
| 527 | /* check if it's visible */ |
| 528 | if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size) |
| 529 | return -EINVAL; |
| 530 | mem->bus.base = adev->mc.aper_base; |
| 531 | mem->bus.is_iomem = true; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 532 | break; |
| 533 | default: |
| 534 | return -EINVAL; |
| 535 | } |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 540 | { |
| 541 | } |
| 542 | |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 543 | static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo, |
| 544 | unsigned long page_offset) |
| 545 | { |
| 546 | struct drm_mm_node *mm = bo->mem.mm_node; |
| 547 | uint64_t size = mm->size; |
Dave Airlie | 0168778 | 2017-04-07 05:41:42 +1000 | [diff] [blame] | 548 | uint64_t offset = page_offset; |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 549 | |
| 550 | page_offset = do_div(offset, size); |
Christian König | ecdba5d | 2017-04-07 10:40:04 +0200 | [diff] [blame] | 551 | mm += offset; |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 552 | return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset; |
| 553 | } |
| 554 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 555 | /* |
| 556 | * TTM backend functions. |
| 557 | */ |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 558 | struct amdgpu_ttm_gup_task_list { |
| 559 | struct list_head list; |
| 560 | struct task_struct *task; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 561 | }; |
| 562 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 563 | struct amdgpu_ttm_tt { |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 564 | struct ttm_dma_tt ttm; |
| 565 | struct amdgpu_device *adev; |
| 566 | u64 offset; |
| 567 | uint64_t userptr; |
| 568 | struct mm_struct *usermm; |
| 569 | uint32_t userflags; |
| 570 | spinlock_t guptasklock; |
| 571 | struct list_head guptasks; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 572 | atomic_t mmu_invalidations; |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 573 | struct list_head list; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 574 | }; |
| 575 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 576 | 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] | 577 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 578 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
Lorenzo Stoakes | 768ae30 | 2016-10-13 01:20:16 +0100 | [diff] [blame] | 579 | unsigned int flags = 0; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 580 | unsigned pinned = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 581 | int r; |
| 582 | |
Lorenzo Stoakes | 768ae30 | 2016-10-13 01:20:16 +0100 | [diff] [blame] | 583 | if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY)) |
| 584 | flags |= FOLL_WRITE; |
| 585 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 586 | if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) { |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 587 | /* check that we only use anonymous memory |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 588 | to prevent problems with writeback */ |
| 589 | unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE; |
| 590 | struct vm_area_struct *vma; |
| 591 | |
| 592 | vma = find_vma(gtt->usermm, gtt->userptr); |
| 593 | if (!vma || vma->vm_file || vma->vm_end < end) |
| 594 | return -EPERM; |
| 595 | } |
| 596 | |
| 597 | do { |
| 598 | unsigned num_pages = ttm->num_pages - pinned; |
| 599 | uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 600 | struct page **p = pages + pinned; |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 601 | struct amdgpu_ttm_gup_task_list guptask; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 602 | |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 603 | guptask.task = current; |
| 604 | spin_lock(>t->guptasklock); |
| 605 | list_add(&guptask.list, >t->guptasks); |
| 606 | spin_unlock(>t->guptasklock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 607 | |
Lorenzo Stoakes | 768ae30 | 2016-10-13 01:20:16 +0100 | [diff] [blame] | 608 | r = get_user_pages(userptr, num_pages, flags, p, NULL); |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 609 | |
| 610 | spin_lock(>t->guptasklock); |
| 611 | list_del(&guptask.list); |
| 612 | spin_unlock(>t->guptasklock); |
| 613 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 614 | if (r < 0) |
| 615 | goto release_pages; |
| 616 | |
| 617 | pinned += r; |
| 618 | |
| 619 | } while (pinned < ttm->num_pages); |
| 620 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 621 | return 0; |
| 622 | |
| 623 | release_pages: |
| 624 | release_pages(pages, pinned, 0); |
| 625 | return r; |
| 626 | } |
| 627 | |
| 628 | /* prepare the sg table with the user pages */ |
| 629 | static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm) |
| 630 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 631 | struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 632 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 633 | unsigned nents; |
| 634 | int r; |
| 635 | |
| 636 | int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); |
| 637 | enum dma_data_direction direction = write ? |
| 638 | DMA_BIDIRECTIONAL : DMA_TO_DEVICE; |
| 639 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 640 | r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0, |
| 641 | ttm->num_pages << PAGE_SHIFT, |
| 642 | GFP_KERNEL); |
| 643 | if (r) |
| 644 | goto release_sg; |
| 645 | |
| 646 | r = -ENOMEM; |
| 647 | nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction); |
| 648 | if (nents != ttm->sg->nents) |
| 649 | goto release_sg; |
| 650 | |
| 651 | drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, |
| 652 | gtt->ttm.dma_address, ttm->num_pages); |
| 653 | |
| 654 | return 0; |
| 655 | |
| 656 | release_sg: |
| 657 | kfree(ttm->sg); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 658 | return r; |
| 659 | } |
| 660 | |
| 661 | static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm) |
| 662 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 663 | struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 664 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
monk.liu | dd08fae | 2015-05-07 14:19:18 -0400 | [diff] [blame] | 665 | struct sg_page_iter sg_iter; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 666 | |
| 667 | int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); |
| 668 | enum dma_data_direction direction = write ? |
| 669 | DMA_BIDIRECTIONAL : DMA_TO_DEVICE; |
| 670 | |
| 671 | /* double check that we don't free the table twice */ |
| 672 | if (!ttm->sg->sgl) |
| 673 | return; |
| 674 | |
| 675 | /* free the sg table and pages again */ |
| 676 | dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction); |
| 677 | |
monk.liu | dd08fae | 2015-05-07 14:19:18 -0400 | [diff] [blame] | 678 | for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) { |
| 679 | struct page *page = sg_page_iter_page(&sg_iter); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 680 | if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY)) |
| 681 | set_page_dirty(page); |
| 682 | |
| 683 | mark_page_accessed(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 684 | put_page(page); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | sg_free_table(ttm->sg); |
| 688 | } |
| 689 | |
Christian König | 98a7f88 | 2017-06-30 10:41:07 +0200 | [diff] [blame] | 690 | static int amdgpu_ttm_do_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem) |
| 691 | { |
| 692 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 693 | uint64_t flags; |
| 694 | int r; |
| 695 | |
| 696 | spin_lock(>t->adev->gtt_list_lock); |
| 697 | flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, mem); |
| 698 | gtt->offset = (u64)mem->start << PAGE_SHIFT; |
| 699 | r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages, |
| 700 | ttm->pages, gtt->ttm.dma_address, flags); |
| 701 | |
| 702 | if (r) { |
| 703 | DRM_ERROR("failed to bind %lu pages at 0x%08llX\n", |
| 704 | ttm->num_pages, gtt->offset); |
| 705 | goto error_gart_bind; |
| 706 | } |
| 707 | |
| 708 | list_add_tail(>t->list, >t->adev->gtt_list); |
| 709 | error_gart_bind: |
| 710 | spin_unlock(>t->adev->gtt_list_lock); |
| 711 | return r; |
| 712 | |
| 713 | } |
| 714 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 715 | static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm, |
| 716 | struct ttm_mem_reg *bo_mem) |
| 717 | { |
| 718 | struct amdgpu_ttm_tt *gtt = (void*)ttm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 719 | int r; |
| 720 | |
Chunming Zhou | e2f784f | 2015-11-26 16:33:58 +0800 | [diff] [blame] | 721 | if (gtt->userptr) { |
| 722 | r = amdgpu_ttm_tt_pin_userptr(ttm); |
| 723 | if (r) { |
| 724 | DRM_ERROR("failed to pin userptr\n"); |
| 725 | return r; |
| 726 | } |
| 727 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 728 | if (!ttm->num_pages) { |
| 729 | WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n", |
| 730 | ttm->num_pages, bo_mem, ttm); |
| 731 | } |
| 732 | |
| 733 | if (bo_mem->mem_type == AMDGPU_PL_GDS || |
| 734 | bo_mem->mem_type == AMDGPU_PL_GWS || |
| 735 | bo_mem->mem_type == AMDGPU_PL_OA) |
| 736 | return -EINVAL; |
| 737 | |
Christian König | 98a7f88 | 2017-06-30 10:41:07 +0200 | [diff] [blame] | 738 | if (amdgpu_gtt_mgr_is_allocated(bo_mem)) |
| 739 | r = amdgpu_ttm_do_bind(ttm, bo_mem); |
| 740 | |
| 741 | return r; |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | bool amdgpu_ttm_is_bound(struct ttm_tt *ttm) |
| 745 | { |
| 746 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 747 | |
| 748 | return gtt && !list_empty(>t->list); |
| 749 | } |
| 750 | |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 751 | 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] | 752 | { |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 753 | struct ttm_tt *ttm = bo->ttm; |
Christian König | c855e25 | 2016-09-05 17:00:57 +0200 | [diff] [blame] | 754 | int r; |
| 755 | |
| 756 | if (!ttm || amdgpu_ttm_is_bound(ttm)) |
| 757 | return 0; |
| 758 | |
Christian König | bb990bb | 2016-09-09 16:32:33 +0200 | [diff] [blame] | 759 | r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo, |
| 760 | NULL, bo_mem); |
| 761 | if (r) { |
| 762 | DRM_ERROR("Failed to allocate GTT address space (%d)\n", r); |
| 763 | return r; |
| 764 | } |
| 765 | |
Christian König | 98a7f88 | 2017-06-30 10:41:07 +0200 | [diff] [blame] | 766 | return amdgpu_ttm_do_bind(ttm, bo_mem); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Chunming Zhou | 2c0d731 | 2016-08-30 16:36:25 +0800 | [diff] [blame] | 769 | int amdgpu_ttm_recover_gart(struct amdgpu_device *adev) |
| 770 | { |
| 771 | struct amdgpu_ttm_tt *gtt, *tmp; |
| 772 | struct ttm_mem_reg bo_mem; |
Monk Liu | 1d1a2cd | 2017-04-27 17:14:57 +0800 | [diff] [blame] | 773 | uint64_t flags; |
Chunming Zhou | 2c0d731 | 2016-08-30 16:36:25 +0800 | [diff] [blame] | 774 | int r; |
| 775 | |
| 776 | bo_mem.mem_type = TTM_PL_TT; |
| 777 | spin_lock(&adev->gtt_list_lock); |
| 778 | list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) { |
| 779 | flags = amdgpu_ttm_tt_pte_flags(gtt->adev, >t->ttm.ttm, &bo_mem); |
| 780 | r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages, |
| 781 | gtt->ttm.ttm.pages, gtt->ttm.dma_address, |
| 782 | flags); |
| 783 | if (r) { |
| 784 | spin_unlock(&adev->gtt_list_lock); |
Christian König | 71c76a0 | 2016-09-03 16:18:26 +0200 | [diff] [blame] | 785 | DRM_ERROR("failed to bind %lu pages at 0x%08llX\n", |
| 786 | gtt->ttm.ttm.num_pages, gtt->offset); |
Chunming Zhou | 2c0d731 | 2016-08-30 16:36:25 +0800 | [diff] [blame] | 787 | return r; |
| 788 | } |
| 789 | } |
| 790 | spin_unlock(&adev->gtt_list_lock); |
| 791 | return 0; |
| 792 | } |
| 793 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 794 | static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm) |
| 795 | { |
| 796 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 797 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 798 | |
Christian König | 85a4b57 | 2016-09-22 14:19:50 +0200 | [diff] [blame] | 799 | if (gtt->userptr) |
| 800 | amdgpu_ttm_tt_unpin_userptr(ttm); |
| 801 | |
Christian König | 78ab0a3 | 2016-09-09 15:39:08 +0200 | [diff] [blame] | 802 | if (!amdgpu_ttm_is_bound(ttm)) |
| 803 | return 0; |
| 804 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 805 | /* 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] | 806 | spin_lock(>t->adev->gtt_list_lock); |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 807 | r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages); |
| 808 | if (r) { |
| 809 | DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n", |
| 810 | gtt->ttm.ttm.num_pages, gtt->offset); |
| 811 | goto error_unbind; |
| 812 | } |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 813 | list_del_init(>t->list); |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 814 | error_unbind: |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 815 | spin_unlock(>t->adev->gtt_list_lock); |
Roger.He | 738f64c | 2017-05-05 13:27:10 +0800 | [diff] [blame] | 816 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm) |
| 820 | { |
| 821 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 822 | |
| 823 | ttm_dma_tt_fini(>t->ttm); |
| 824 | kfree(gtt); |
| 825 | } |
| 826 | |
| 827 | static struct ttm_backend_func amdgpu_backend_func = { |
| 828 | .bind = &amdgpu_ttm_backend_bind, |
| 829 | .unbind = &amdgpu_ttm_backend_unbind, |
| 830 | .destroy = &amdgpu_ttm_backend_destroy, |
| 831 | }; |
| 832 | |
| 833 | static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev, |
| 834 | unsigned long size, uint32_t page_flags, |
| 835 | struct page *dummy_read_page) |
| 836 | { |
| 837 | struct amdgpu_device *adev; |
| 838 | struct amdgpu_ttm_tt *gtt; |
| 839 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 840 | adev = amdgpu_ttm_adev(bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 841 | |
| 842 | gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL); |
| 843 | if (gtt == NULL) { |
| 844 | return NULL; |
| 845 | } |
| 846 | gtt->ttm.ttm.func = &amdgpu_backend_func; |
| 847 | gtt->adev = adev; |
| 848 | if (ttm_dma_tt_init(>t->ttm, bdev, size, page_flags, dummy_read_page)) { |
| 849 | kfree(gtt); |
| 850 | return NULL; |
| 851 | } |
Chunming Zhou | 5c1354b | 2016-08-30 16:13:10 +0800 | [diff] [blame] | 852 | INIT_LIST_HEAD(>t->list); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 853 | return >t->ttm.ttm; |
| 854 | } |
| 855 | |
| 856 | static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm) |
| 857 | { |
| 858 | struct amdgpu_device *adev; |
| 859 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 860 | unsigned i; |
| 861 | int r; |
| 862 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
| 863 | |
| 864 | if (ttm->state != tt_unpopulated) |
| 865 | return 0; |
| 866 | |
| 867 | if (gtt && gtt->userptr) { |
Maninder Singh | 5f0b34c | 2015-06-26 13:28:50 +0530 | [diff] [blame] | 868 | ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 869 | if (!ttm->sg) |
| 870 | return -ENOMEM; |
| 871 | |
| 872 | ttm->page_flags |= TTM_PAGE_FLAG_SG; |
| 873 | ttm->state = tt_unbound; |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | if (slave && ttm->sg) { |
| 878 | drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, |
| 879 | gtt->ttm.dma_address, ttm->num_pages); |
| 880 | ttm->state = tt_unbound; |
| 881 | return 0; |
| 882 | } |
| 883 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 884 | adev = amdgpu_ttm_adev(ttm->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 885 | |
| 886 | #ifdef CONFIG_SWIOTLB |
| 887 | if (swiotlb_nr_tbl()) { |
| 888 | return ttm_dma_populate(>t->ttm, adev->dev); |
| 889 | } |
| 890 | #endif |
| 891 | |
| 892 | r = ttm_pool_populate(ttm); |
| 893 | if (r) { |
| 894 | return r; |
| 895 | } |
| 896 | |
| 897 | for (i = 0; i < ttm->num_pages; i++) { |
| 898 | gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i], |
| 899 | 0, PAGE_SIZE, |
| 900 | PCI_DMA_BIDIRECTIONAL); |
| 901 | if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) { |
Rasmus Villemoes | 09ccbb7 | 2016-02-15 19:41:45 +0100 | [diff] [blame] | 902 | while (i--) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 903 | pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i], |
| 904 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 905 | gtt->ttm.dma_address[i] = 0; |
| 906 | } |
| 907 | ttm_pool_unpopulate(ttm); |
| 908 | return -EFAULT; |
| 909 | } |
| 910 | } |
| 911 | return 0; |
| 912 | } |
| 913 | |
| 914 | static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm) |
| 915 | { |
| 916 | struct amdgpu_device *adev; |
| 917 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 918 | unsigned i; |
| 919 | bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); |
| 920 | |
| 921 | if (gtt && gtt->userptr) { |
| 922 | kfree(ttm->sg); |
| 923 | ttm->page_flags &= ~TTM_PAGE_FLAG_SG; |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | if (slave) |
| 928 | return; |
| 929 | |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 930 | adev = amdgpu_ttm_adev(ttm->bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 931 | |
| 932 | #ifdef CONFIG_SWIOTLB |
| 933 | if (swiotlb_nr_tbl()) { |
| 934 | ttm_dma_unpopulate(>t->ttm, adev->dev); |
| 935 | return; |
| 936 | } |
| 937 | #endif |
| 938 | |
| 939 | for (i = 0; i < ttm->num_pages; i++) { |
| 940 | if (gtt->ttm.dma_address[i]) { |
| 941 | pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i], |
| 942 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | ttm_pool_unpopulate(ttm); |
| 947 | } |
| 948 | |
| 949 | int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr, |
| 950 | uint32_t flags) |
| 951 | { |
| 952 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 953 | |
| 954 | if (gtt == NULL) |
| 955 | return -EINVAL; |
| 956 | |
| 957 | gtt->userptr = addr; |
| 958 | gtt->usermm = current->mm; |
| 959 | gtt->userflags = flags; |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 960 | spin_lock_init(>t->guptasklock); |
| 961 | INIT_LIST_HEAD(>t->guptasks); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 962 | atomic_set(>t->mmu_invalidations, 0); |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 963 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 967 | struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 968 | { |
| 969 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 970 | |
| 971 | if (gtt == NULL) |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 972 | return NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 973 | |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 974 | return gtt->usermm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 975 | } |
| 976 | |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 977 | bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start, |
| 978 | unsigned long end) |
| 979 | { |
| 980 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 981 | struct amdgpu_ttm_gup_task_list *entry; |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 982 | unsigned long size; |
| 983 | |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 984 | if (gtt == NULL || !gtt->userptr) |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 985 | return false; |
| 986 | |
| 987 | size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE; |
| 988 | if (gtt->userptr > end || gtt->userptr + size <= start) |
| 989 | return false; |
| 990 | |
Christian König | 637dd3b | 2016-03-03 14:24:57 +0100 | [diff] [blame] | 991 | spin_lock(>t->guptasklock); |
| 992 | list_for_each_entry(entry, >t->guptasks, list) { |
| 993 | if (entry->task == current) { |
| 994 | spin_unlock(>t->guptasklock); |
| 995 | return false; |
| 996 | } |
| 997 | } |
| 998 | spin_unlock(>t->guptasklock); |
| 999 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 1000 | atomic_inc(>t->mmu_invalidations); |
| 1001 | |
Christian König | cc1de6e | 2016-02-08 10:57:22 +0100 | [diff] [blame] | 1002 | return true; |
| 1003 | } |
| 1004 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 1005 | bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm, |
| 1006 | int *last_invalidated) |
| 1007 | { |
| 1008 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 1009 | int prev_invalidated = *last_invalidated; |
| 1010 | |
| 1011 | *last_invalidated = atomic_read(>t->mmu_invalidations); |
| 1012 | return prev_invalidated != *last_invalidated; |
| 1013 | } |
| 1014 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1015 | bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm) |
| 1016 | { |
| 1017 | struct amdgpu_ttm_tt *gtt = (void *)ttm; |
| 1018 | |
| 1019 | if (gtt == NULL) |
| 1020 | return false; |
| 1021 | |
| 1022 | return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY); |
| 1023 | } |
| 1024 | |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1025 | 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] | 1026 | struct ttm_mem_reg *mem) |
| 1027 | { |
Chunming Zhou | 6b77760 | 2016-09-21 16:19:19 +0800 | [diff] [blame] | 1028 | uint64_t flags = 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1029 | |
| 1030 | if (mem && mem->mem_type != TTM_PL_SYSTEM) |
| 1031 | flags |= AMDGPU_PTE_VALID; |
| 1032 | |
Christian König | 6d99905 | 2015-12-04 13:32:55 +0100 | [diff] [blame] | 1033 | if (mem && mem->mem_type == TTM_PL_TT) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1034 | flags |= AMDGPU_PTE_SYSTEM; |
| 1035 | |
Christian König | 6d99905 | 2015-12-04 13:32:55 +0100 | [diff] [blame] | 1036 | if (ttm->caching_state == tt_cached) |
| 1037 | flags |= AMDGPU_PTE_SNOOPED; |
| 1038 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1039 | |
Alex Xie | 4b98e0c | 2017-02-14 12:31:36 -0500 | [diff] [blame] | 1040 | flags |= adev->gart.gart_pte_flags; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1041 | flags |= AMDGPU_PTE_READABLE; |
| 1042 | |
| 1043 | if (!amdgpu_ttm_tt_is_readonly(ttm)) |
| 1044 | flags |= AMDGPU_PTE_WRITEABLE; |
| 1045 | |
| 1046 | return flags; |
| 1047 | } |
| 1048 | |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1049 | static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo, |
| 1050 | const struct ttm_place *place) |
| 1051 | { |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1052 | unsigned long num_pages = bo->mem.num_pages; |
| 1053 | struct drm_mm_node *node = bo->mem.mm_node; |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1054 | |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1055 | if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET) |
| 1056 | return ttm_bo_eviction_valuable(bo, place); |
| 1057 | |
| 1058 | switch (bo->mem.mem_type) { |
| 1059 | case TTM_PL_TT: |
| 1060 | return true; |
| 1061 | |
| 1062 | case TTM_PL_VRAM: |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1063 | /* Check each drm MM node individually */ |
| 1064 | while (num_pages) { |
| 1065 | if (place->fpfn < (node->start + node->size) && |
| 1066 | !(place->lpfn && place->lpfn <= node->start)) |
| 1067 | return true; |
| 1068 | |
| 1069 | num_pages -= node->size; |
| 1070 | ++node; |
| 1071 | } |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1072 | break; |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1073 | |
Christian König | 4fcae78 | 2017-04-20 12:11:47 +0200 | [diff] [blame] | 1074 | default: |
| 1075 | break; |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | return ttm_bo_eviction_valuable(bo, place); |
| 1079 | } |
| 1080 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1081 | static struct ttm_bo_driver amdgpu_bo_driver = { |
| 1082 | .ttm_tt_create = &amdgpu_ttm_tt_create, |
| 1083 | .ttm_tt_populate = &amdgpu_ttm_tt_populate, |
| 1084 | .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate, |
| 1085 | .invalidate_caches = &amdgpu_invalidate_caches, |
| 1086 | .init_mem_type = &amdgpu_init_mem_type, |
Christian König | 9982ca6 | 2016-10-19 14:44:22 +0200 | [diff] [blame] | 1087 | .eviction_valuable = amdgpu_ttm_bo_eviction_valuable, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1088 | .evict_flags = &amdgpu_evict_flags, |
| 1089 | .move = &amdgpu_bo_move, |
| 1090 | .verify_access = &amdgpu_verify_access, |
| 1091 | .move_notify = &amdgpu_bo_move_notify, |
| 1092 | .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify, |
| 1093 | .io_mem_reserve = &amdgpu_ttm_io_mem_reserve, |
| 1094 | .io_mem_free = &amdgpu_ttm_io_mem_free, |
Christian König | 9bbdcc0 | 2017-03-29 11:16:05 +0200 | [diff] [blame] | 1095 | .io_mem_pfn = amdgpu_ttm_io_mem_pfn, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1096 | }; |
| 1097 | |
| 1098 | int amdgpu_ttm_init(struct amdgpu_device *adev) |
| 1099 | { |
Christian König | 36d3837 | 2017-07-07 13:17:45 +0200 | [diff] [blame] | 1100 | uint64_t gtt_size; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1101 | int r; |
John Brooks | 218b5dc | 2017-06-27 22:33:17 -0400 | [diff] [blame] | 1102 | u64 vis_vram_limit; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1103 | |
Alex Deucher | 70b5c5a | 2016-11-15 16:55:53 -0500 | [diff] [blame] | 1104 | r = amdgpu_ttm_global_init(adev); |
| 1105 | if (r) { |
| 1106 | return r; |
| 1107 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1108 | /* No others user of address space so set it to 0 */ |
| 1109 | r = ttm_bo_device_init(&adev->mman.bdev, |
| 1110 | adev->mman.bo_global_ref.ref.object, |
| 1111 | &amdgpu_bo_driver, |
| 1112 | adev->ddev->anon_inode->i_mapping, |
| 1113 | DRM_FILE_PAGE_OFFSET, |
| 1114 | adev->need_dma32); |
| 1115 | if (r) { |
| 1116 | DRM_ERROR("failed initializing buffer object driver(%d).\n", r); |
| 1117 | return r; |
| 1118 | } |
| 1119 | adev->mman.initialized = true; |
| 1120 | r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM, |
| 1121 | adev->mc.real_vram_size >> PAGE_SHIFT); |
| 1122 | if (r) { |
| 1123 | DRM_ERROR("Failed initializing VRAM heap.\n"); |
| 1124 | return r; |
| 1125 | } |
John Brooks | 218b5dc | 2017-06-27 22:33:17 -0400 | [diff] [blame] | 1126 | |
| 1127 | /* Reduce size of CPU-visible VRAM if requested */ |
| 1128 | vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024; |
| 1129 | if (amdgpu_vis_vram_limit > 0 && |
| 1130 | vis_vram_limit <= adev->mc.visible_vram_size) |
| 1131 | adev->mc.visible_vram_size = vis_vram_limit; |
| 1132 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1133 | /* Change the size here instead of the init above so only lpfn is affected */ |
| 1134 | amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size); |
| 1135 | |
Huang Rui | 916910a | 2017-05-31 10:35:42 +0800 | [diff] [blame] | 1136 | r = amdgpu_bo_create(adev, adev->mc.stolen_size, PAGE_SIZE, true, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 1137 | AMDGPU_GEM_DOMAIN_VRAM, |
Christian König | 03f48dd | 2016-08-15 17:00:22 +0200 | [diff] [blame] | 1138 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED | |
| 1139 | AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 1140 | NULL, NULL, &adev->stollen_vga_memory); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1141 | if (r) { |
| 1142 | return r; |
| 1143 | } |
| 1144 | r = amdgpu_bo_reserve(adev->stollen_vga_memory, false); |
| 1145 | if (r) |
| 1146 | return r; |
| 1147 | r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL); |
| 1148 | amdgpu_bo_unreserve(adev->stollen_vga_memory); |
| 1149 | if (r) { |
| 1150 | amdgpu_bo_unref(&adev->stollen_vga_memory); |
| 1151 | return r; |
| 1152 | } |
| 1153 | DRM_INFO("amdgpu: %uM of VRAM memory ready\n", |
| 1154 | (unsigned) (adev->mc.real_vram_size / (1024 * 1024))); |
Christian König | 36d3837 | 2017-07-07 13:17:45 +0200 | [diff] [blame] | 1155 | |
| 1156 | if (amdgpu_gtt_size == -1) |
| 1157 | gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20), |
| 1158 | adev->mc.mc_vram_size); |
| 1159 | else |
| 1160 | gtt_size = (uint64_t)amdgpu_gtt_size << 20; |
| 1161 | 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] | 1162 | if (r) { |
| 1163 | DRM_ERROR("Failed initializing GTT heap.\n"); |
| 1164 | return r; |
| 1165 | } |
| 1166 | DRM_INFO("amdgpu: %uM of GTT memory ready.\n", |
Christian König | 36d3837 | 2017-07-07 13:17:45 +0200 | [diff] [blame] | 1167 | (unsigned)(gtt_size / (1024 * 1024))); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1168 | |
| 1169 | adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT; |
| 1170 | adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT; |
| 1171 | adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT; |
| 1172 | adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT; |
| 1173 | adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT; |
| 1174 | adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT; |
| 1175 | adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT; |
| 1176 | adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT; |
| 1177 | adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT; |
| 1178 | /* GDS Memory */ |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1179 | if (adev->gds.mem.total_size) { |
| 1180 | r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS, |
| 1181 | adev->gds.mem.total_size >> PAGE_SHIFT); |
| 1182 | if (r) { |
| 1183 | DRM_ERROR("Failed initializing GDS heap.\n"); |
| 1184 | return r; |
| 1185 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | /* GWS */ |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1189 | if (adev->gds.gws.total_size) { |
| 1190 | r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS, |
| 1191 | adev->gds.gws.total_size >> PAGE_SHIFT); |
| 1192 | if (r) { |
| 1193 | DRM_ERROR("Failed initializing gws heap.\n"); |
| 1194 | return r; |
| 1195 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /* OA */ |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1199 | if (adev->gds.oa.total_size) { |
| 1200 | r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA, |
| 1201 | adev->gds.oa.total_size >> PAGE_SHIFT); |
| 1202 | if (r) { |
| 1203 | DRM_ERROR("Failed initializing oa heap.\n"); |
| 1204 | return r; |
| 1205 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | r = amdgpu_ttm_debugfs_init(adev); |
| 1209 | if (r) { |
| 1210 | DRM_ERROR("Failed to init debugfs\n"); |
| 1211 | return r; |
| 1212 | } |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | void amdgpu_ttm_fini(struct amdgpu_device *adev) |
| 1217 | { |
| 1218 | int r; |
| 1219 | |
| 1220 | if (!adev->mman.initialized) |
| 1221 | return; |
| 1222 | amdgpu_ttm_debugfs_fini(adev); |
| 1223 | if (adev->stollen_vga_memory) { |
Michel Dänzer | c81a1a7 | 2017-04-28 17:28:14 +0900 | [diff] [blame] | 1224 | r = amdgpu_bo_reserve(adev->stollen_vga_memory, true); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1225 | if (r == 0) { |
| 1226 | amdgpu_bo_unpin(adev->stollen_vga_memory); |
| 1227 | amdgpu_bo_unreserve(adev->stollen_vga_memory); |
| 1228 | } |
| 1229 | amdgpu_bo_unref(&adev->stollen_vga_memory); |
| 1230 | } |
| 1231 | ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM); |
| 1232 | ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT); |
Alex Deucher | d2d51d8 | 2017-03-15 09:45:48 -0400 | [diff] [blame] | 1233 | if (adev->gds.mem.total_size) |
| 1234 | ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS); |
| 1235 | if (adev->gds.gws.total_size) |
| 1236 | ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS); |
| 1237 | if (adev->gds.oa.total_size) |
| 1238 | ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1239 | ttm_bo_device_release(&adev->mman.bdev); |
| 1240 | amdgpu_gart_fini(adev); |
| 1241 | amdgpu_ttm_global_fini(adev); |
| 1242 | adev->mman.initialized = false; |
| 1243 | DRM_INFO("amdgpu: ttm finalized\n"); |
| 1244 | } |
| 1245 | |
| 1246 | /* this should only be called at bootup or when userspace |
| 1247 | * isn't running */ |
| 1248 | void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size) |
| 1249 | { |
| 1250 | struct ttm_mem_type_manager *man; |
| 1251 | |
| 1252 | if (!adev->mman.initialized) |
| 1253 | return; |
| 1254 | |
| 1255 | man = &adev->mman.bdev.man[TTM_PL_VRAM]; |
| 1256 | /* this just adjusts TTM size idea, which sets lpfn to the correct value */ |
| 1257 | man->size = size >> PAGE_SHIFT; |
| 1258 | } |
| 1259 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1260 | int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma) |
| 1261 | { |
| 1262 | struct drm_file *file_priv; |
| 1263 | struct amdgpu_device *adev; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1264 | |
Christian König | e176fe17 | 2015-05-27 10:22:47 +0200 | [diff] [blame] | 1265 | if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET)) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1266 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1267 | |
| 1268 | file_priv = filp->private_data; |
| 1269 | adev = file_priv->minor->dev->dev_private; |
Christian König | e176fe17 | 2015-05-27 10:22:47 +0200 | [diff] [blame] | 1270 | if (adev == NULL) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1271 | return -EINVAL; |
Christian König | e176fe17 | 2015-05-27 10:22:47 +0200 | [diff] [blame] | 1272 | |
| 1273 | return ttm_bo_mmap(filp, vma, &adev->mman.bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1274 | } |
| 1275 | |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 1276 | static int amdgpu_map_buffer(struct ttm_buffer_object *bo, |
| 1277 | struct ttm_mem_reg *mem, unsigned num_pages, |
| 1278 | uint64_t offset, unsigned window, |
| 1279 | struct amdgpu_ring *ring, |
| 1280 | uint64_t *addr) |
| 1281 | { |
| 1282 | struct amdgpu_ttm_tt *gtt = (void *)bo->ttm; |
| 1283 | struct amdgpu_device *adev = ring->adev; |
| 1284 | struct ttm_tt *ttm = bo->ttm; |
| 1285 | struct amdgpu_job *job; |
| 1286 | unsigned num_dw, num_bytes; |
| 1287 | dma_addr_t *dma_address; |
| 1288 | struct dma_fence *fence; |
| 1289 | uint64_t src_addr, dst_addr; |
| 1290 | uint64_t flags; |
| 1291 | int r; |
| 1292 | |
| 1293 | BUG_ON(adev->mman.buffer_funcs->copy_max_bytes < |
| 1294 | AMDGPU_GTT_MAX_TRANSFER_SIZE * 8); |
| 1295 | |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 1296 | *addr = adev->mc.gart_start; |
Christian König | abca90f | 2017-06-30 11:05:54 +0200 | [diff] [blame] | 1297 | *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE * |
| 1298 | AMDGPU_GPU_PAGE_SIZE; |
| 1299 | |
| 1300 | num_dw = adev->mman.buffer_funcs->copy_num_dw; |
| 1301 | while (num_dw & 0x7) |
| 1302 | num_dw++; |
| 1303 | |
| 1304 | num_bytes = num_pages * 8; |
| 1305 | |
| 1306 | r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job); |
| 1307 | if (r) |
| 1308 | return r; |
| 1309 | |
| 1310 | src_addr = num_dw * 4; |
| 1311 | src_addr += job->ibs[0].gpu_addr; |
| 1312 | |
| 1313 | dst_addr = adev->gart.table_addr; |
| 1314 | dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8; |
| 1315 | amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, |
| 1316 | dst_addr, num_bytes); |
| 1317 | |
| 1318 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
| 1319 | WARN_ON(job->ibs[0].length_dw > num_dw); |
| 1320 | |
| 1321 | dma_address = >t->ttm.dma_address[offset >> PAGE_SHIFT]; |
| 1322 | flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem); |
| 1323 | r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags, |
| 1324 | &job->ibs[0].ptr[num_dw]); |
| 1325 | if (r) |
| 1326 | goto error_free; |
| 1327 | |
| 1328 | r = amdgpu_job_submit(job, ring, &adev->mman.entity, |
| 1329 | AMDGPU_FENCE_OWNER_UNDEFINED, &fence); |
| 1330 | if (r) |
| 1331 | goto error_free; |
| 1332 | |
| 1333 | dma_fence_put(fence); |
| 1334 | |
| 1335 | return r; |
| 1336 | |
| 1337 | error_free: |
| 1338 | amdgpu_job_free(job); |
| 1339 | return r; |
| 1340 | } |
| 1341 | |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 1342 | int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset, |
| 1343 | uint64_t dst_offset, uint32_t byte_count, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1344 | struct reservation_object *resv, |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 1345 | struct dma_fence **fence, bool direct_submit, |
| 1346 | bool vm_needs_flush) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1347 | { |
| 1348 | struct amdgpu_device *adev = ring->adev; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1349 | struct amdgpu_job *job; |
| 1350 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1351 | uint32_t max_bytes; |
| 1352 | unsigned num_loops, num_dw; |
| 1353 | unsigned i; |
| 1354 | int r; |
| 1355 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1356 | max_bytes = adev->mman.buffer_funcs->copy_max_bytes; |
| 1357 | num_loops = DIV_ROUND_UP(byte_count, max_bytes); |
| 1358 | num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw; |
| 1359 | |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1360 | /* for IB padding */ |
| 1361 | while (num_dw & 0x7) |
| 1362 | num_dw++; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1363 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1364 | r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job); |
| 1365 | if (r) |
Chunming Zhou | 9066b0c | 2015-08-25 15:12:26 +0800 | [diff] [blame] | 1366 | return r; |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1367 | |
Christian König | fc9c8f5 | 2017-06-29 11:46:15 +0200 | [diff] [blame] | 1368 | job->vm_needs_flush = vm_needs_flush; |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1369 | if (resv) { |
Christian König | e86f9ce | 2016-02-08 12:13:05 +0100 | [diff] [blame] | 1370 | r = amdgpu_sync_resv(adev, &job->sync, resv, |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1371 | AMDGPU_FENCE_OWNER_UNDEFINED); |
| 1372 | if (r) { |
| 1373 | DRM_ERROR("sync failed (%d).\n", r); |
| 1374 | goto error_free; |
| 1375 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1376 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1377 | |
| 1378 | for (i = 0; i < num_loops; i++) { |
| 1379 | uint32_t cur_size_in_bytes = min(byte_count, max_bytes); |
| 1380 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1381 | amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset, |
| 1382 | dst_offset, cur_size_in_bytes); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1383 | |
| 1384 | src_offset += cur_size_in_bytes; |
| 1385 | dst_offset += cur_size_in_bytes; |
| 1386 | byte_count -= cur_size_in_bytes; |
| 1387 | } |
| 1388 | |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1389 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
| 1390 | WARN_ON(job->ibs[0].length_dw > num_dw); |
Chunming Zhou | e24db98 | 2016-08-15 10:46:04 +0800 | [diff] [blame] | 1391 | if (direct_submit) { |
| 1392 | r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs, |
Junwei Zhang | 50ddc75 | 2017-01-23 16:30:38 +0800 | [diff] [blame] | 1393 | NULL, fence); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 1394 | job->fence = dma_fence_get(*fence); |
Chunming Zhou | e24db98 | 2016-08-15 10:46:04 +0800 | [diff] [blame] | 1395 | if (r) |
| 1396 | DRM_ERROR("Error scheduling IBs (%d)\n", r); |
| 1397 | amdgpu_job_free(job); |
| 1398 | } else { |
| 1399 | r = amdgpu_job_submit(job, ring, &adev->mman.entity, |
| 1400 | AMDGPU_FENCE_OWNER_UNDEFINED, fence); |
| 1401 | if (r) |
| 1402 | goto error_free; |
| 1403 | } |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1404 | |
Chunming Zhou | e24db98 | 2016-08-15 10:46:04 +0800 | [diff] [blame] | 1405 | return r; |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1406 | |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1407 | error_free: |
Christian König | d71518b | 2016-02-01 12:20:25 +0100 | [diff] [blame] | 1408 | amdgpu_job_free(job); |
Chunming Zhou | c7ae72c | 2015-08-25 17:23:45 +0800 | [diff] [blame] | 1409 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1410 | } |
| 1411 | |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1412 | int amdgpu_fill_buffer(struct amdgpu_bo *bo, |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1413 | uint32_t src_data, |
| 1414 | struct reservation_object *resv, |
| 1415 | struct dma_fence **fence) |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1416 | { |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 1417 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1418 | uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1419 | struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring; |
| 1420 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1421 | struct drm_mm_node *mm_node; |
| 1422 | unsigned long num_pages; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1423 | unsigned int num_loops, num_dw; |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1424 | |
| 1425 | struct amdgpu_job *job; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1426 | int r; |
| 1427 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1428 | if (!ring->ready) { |
| 1429 | DRM_ERROR("Trying to clear memory with ring turned off.\n"); |
| 1430 | return -EINVAL; |
| 1431 | } |
| 1432 | |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 1433 | if (bo->tbo.mem.mem_type == TTM_PL_TT) { |
| 1434 | r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem); |
| 1435 | if (r) |
| 1436 | return r; |
| 1437 | } |
| 1438 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1439 | num_pages = bo->tbo.num_pages; |
| 1440 | mm_node = bo->tbo.mem.mm_node; |
| 1441 | num_loops = 0; |
| 1442 | while (num_pages) { |
| 1443 | uint32_t byte_count = mm_node->size << PAGE_SHIFT; |
| 1444 | |
| 1445 | num_loops += DIV_ROUND_UP(byte_count, max_bytes); |
| 1446 | num_pages -= mm_node->size; |
| 1447 | ++mm_node; |
| 1448 | } |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1449 | num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw; |
| 1450 | |
| 1451 | /* for IB padding */ |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1452 | num_dw += 64; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1453 | |
| 1454 | r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job); |
| 1455 | if (r) |
| 1456 | return r; |
| 1457 | |
| 1458 | if (resv) { |
| 1459 | r = amdgpu_sync_resv(adev, &job->sync, resv, |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1460 | AMDGPU_FENCE_OWNER_UNDEFINED); |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1461 | if (r) { |
| 1462 | DRM_ERROR("sync failed (%d).\n", r); |
| 1463 | goto error_free; |
| 1464 | } |
| 1465 | } |
| 1466 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1467 | num_pages = bo->tbo.num_pages; |
| 1468 | mm_node = bo->tbo.mem.mm_node; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1469 | |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1470 | while (num_pages) { |
| 1471 | uint32_t byte_count = mm_node->size << PAGE_SHIFT; |
| 1472 | uint64_t dst_addr; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1473 | |
Christian König | 92c60d9 | 2017-06-29 10:44:39 +0200 | [diff] [blame] | 1474 | 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] | 1475 | while (byte_count) { |
| 1476 | uint32_t cur_size_in_bytes = min(byte_count, max_bytes); |
| 1477 | |
| 1478 | amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data, |
| 1479 | dst_addr, cur_size_in_bytes); |
| 1480 | |
| 1481 | dst_addr += cur_size_in_bytes; |
| 1482 | byte_count -= cur_size_in_bytes; |
| 1483 | } |
| 1484 | |
| 1485 | num_pages -= mm_node->size; |
| 1486 | ++mm_node; |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | amdgpu_ring_pad_ib(ring, &job->ibs[0]); |
| 1490 | WARN_ON(job->ibs[0].length_dw > num_dw); |
| 1491 | r = amdgpu_job_submit(job, ring, &adev->mman.entity, |
Christian König | f29224a6 | 2016-11-17 12:06:38 +0100 | [diff] [blame] | 1492 | AMDGPU_FENCE_OWNER_UNDEFINED, fence); |
Flora Cui | 59b4a97 | 2016-07-19 16:48:22 +0800 | [diff] [blame] | 1493 | if (r) |
| 1494 | goto error_free; |
| 1495 | |
| 1496 | return 0; |
| 1497 | |
| 1498 | error_free: |
| 1499 | amdgpu_job_free(job); |
| 1500 | return r; |
| 1501 | } |
| 1502 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1503 | #if defined(CONFIG_DEBUG_FS) |
| 1504 | |
Chunming Zhou | 05a72a2 | 2017-04-13 16:16:51 +0800 | [diff] [blame] | 1505 | extern void amdgpu_gtt_mgr_print(struct seq_file *m, struct ttm_mem_type_manager |
| 1506 | *man); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1507 | static int amdgpu_mm_dump_table(struct seq_file *m, void *data) |
| 1508 | { |
| 1509 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
| 1510 | unsigned ttm_pl = *(int *)node->info_ent->data; |
| 1511 | struct drm_device *dev = node->minor->dev; |
| 1512 | struct amdgpu_device *adev = dev->dev_private; |
| 1513 | 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] | 1514 | struct ttm_bo_global *glob = adev->mman.bdev.glob; |
Daniel Vetter | b5c3714 | 2016-12-29 12:09:24 +0100 | [diff] [blame] | 1515 | struct drm_printer p = drm_seq_file_printer(m); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1516 | |
| 1517 | spin_lock(&glob->lru_lock); |
Daniel Vetter | b5c3714 | 2016-12-29 12:09:24 +0100 | [diff] [blame] | 1518 | drm_mm_print(mm, &p); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1519 | spin_unlock(&glob->lru_lock); |
Chunming Zhou | 05a72a2 | 2017-04-13 16:16:51 +0800 | [diff] [blame] | 1520 | switch (ttm_pl) { |
| 1521 | case TTM_PL_VRAM: |
Arnd Bergmann | e1b35f6 | 2015-11-10 13:17:55 +0100 | [diff] [blame] | 1522 | 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] | 1523 | adev->mman.bdev.man[ttm_pl].size, |
Arnd Bergmann | e1b35f6 | 2015-11-10 13:17:55 +0100 | [diff] [blame] | 1524 | (u64)atomic64_read(&adev->vram_usage) >> 20, |
| 1525 | (u64)atomic64_read(&adev->vram_vis_usage) >> 20); |
Chunming Zhou | 05a72a2 | 2017-04-13 16:16:51 +0800 | [diff] [blame] | 1526 | break; |
| 1527 | case TTM_PL_TT: |
| 1528 | amdgpu_gtt_mgr_print(m, &adev->mman.bdev.man[TTM_PL_TT]); |
| 1529 | break; |
| 1530 | } |
Daniel Vetter | b5c3714 | 2016-12-29 12:09:24 +0100 | [diff] [blame] | 1531 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | static int ttm_pl_vram = TTM_PL_VRAM; |
| 1535 | static int ttm_pl_tt = TTM_PL_TT; |
| 1536 | |
Nils Wallménius | 06ab683 | 2016-05-02 12:46:15 -0400 | [diff] [blame] | 1537 | static const struct drm_info_list amdgpu_ttm_debugfs_list[] = { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1538 | {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram}, |
| 1539 | {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt}, |
| 1540 | {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL}, |
| 1541 | #ifdef CONFIG_SWIOTLB |
| 1542 | {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL} |
| 1543 | #endif |
| 1544 | }; |
| 1545 | |
| 1546 | static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf, |
| 1547 | size_t size, loff_t *pos) |
| 1548 | { |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 1549 | struct amdgpu_device *adev = file_inode(f)->i_private; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1550 | ssize_t result = 0; |
| 1551 | int r; |
| 1552 | |
| 1553 | if (size & 0x3 || *pos & 0x3) |
| 1554 | return -EINVAL; |
| 1555 | |
Tom St Denis | 9156e72 | 2017-05-23 11:35:22 -0400 | [diff] [blame] | 1556 | if (*pos >= adev->mc.mc_vram_size) |
| 1557 | return -ENXIO; |
| 1558 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1559 | while (size) { |
| 1560 | unsigned long flags; |
| 1561 | uint32_t value; |
| 1562 | |
| 1563 | if (*pos >= adev->mc.mc_vram_size) |
| 1564 | return result; |
| 1565 | |
| 1566 | spin_lock_irqsave(&adev->mmio_idx_lock, flags); |
| 1567 | WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000); |
| 1568 | WREG32(mmMM_INDEX_HI, *pos >> 31); |
| 1569 | value = RREG32(mmMM_DATA); |
| 1570 | spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); |
| 1571 | |
| 1572 | r = put_user(value, (uint32_t *)buf); |
| 1573 | if (r) |
| 1574 | return r; |
| 1575 | |
| 1576 | result += 4; |
| 1577 | buf += 4; |
| 1578 | *pos += 4; |
| 1579 | size -= 4; |
| 1580 | } |
| 1581 | |
| 1582 | return result; |
| 1583 | } |
| 1584 | |
| 1585 | static const struct file_operations amdgpu_ttm_vram_fops = { |
| 1586 | .owner = THIS_MODULE, |
| 1587 | .read = amdgpu_ttm_vram_read, |
| 1588 | .llseek = default_llseek |
| 1589 | }; |
| 1590 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1591 | #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS |
| 1592 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1593 | static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf, |
| 1594 | size_t size, loff_t *pos) |
| 1595 | { |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 1596 | struct amdgpu_device *adev = file_inode(f)->i_private; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1597 | ssize_t result = 0; |
| 1598 | int r; |
| 1599 | |
| 1600 | while (size) { |
| 1601 | loff_t p = *pos / PAGE_SIZE; |
| 1602 | unsigned off = *pos & ~PAGE_MASK; |
| 1603 | size_t cur_size = min_t(size_t, size, PAGE_SIZE - off); |
| 1604 | struct page *page; |
| 1605 | void *ptr; |
| 1606 | |
| 1607 | if (p >= adev->gart.num_cpu_pages) |
| 1608 | return result; |
| 1609 | |
| 1610 | page = adev->gart.pages[p]; |
| 1611 | if (page) { |
| 1612 | ptr = kmap(page); |
| 1613 | ptr += off; |
| 1614 | |
| 1615 | r = copy_to_user(buf, ptr, cur_size); |
| 1616 | kunmap(adev->gart.pages[p]); |
| 1617 | } else |
| 1618 | r = clear_user(buf, cur_size); |
| 1619 | |
| 1620 | if (r) |
| 1621 | return -EFAULT; |
| 1622 | |
| 1623 | result += cur_size; |
| 1624 | buf += cur_size; |
| 1625 | *pos += cur_size; |
| 1626 | size -= cur_size; |
| 1627 | } |
| 1628 | |
| 1629 | return result; |
| 1630 | } |
| 1631 | |
| 1632 | static const struct file_operations amdgpu_ttm_gtt_fops = { |
| 1633 | .owner = THIS_MODULE, |
| 1634 | .read = amdgpu_ttm_gtt_read, |
| 1635 | .llseek = default_llseek |
| 1636 | }; |
| 1637 | |
| 1638 | #endif |
| 1639 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1640 | #endif |
| 1641 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1642 | static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev) |
| 1643 | { |
| 1644 | #if defined(CONFIG_DEBUG_FS) |
| 1645 | unsigned count; |
| 1646 | |
| 1647 | struct drm_minor *minor = adev->ddev->primary; |
| 1648 | struct dentry *ent, *root = minor->debugfs_root; |
| 1649 | |
| 1650 | ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root, |
| 1651 | adev, &amdgpu_ttm_vram_fops); |
| 1652 | if (IS_ERR(ent)) |
| 1653 | return PTR_ERR(ent); |
| 1654 | i_size_write(ent->d_inode, adev->mc.mc_vram_size); |
| 1655 | adev->mman.vram = ent; |
| 1656 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1657 | #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1658 | ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root, |
| 1659 | adev, &amdgpu_ttm_gtt_fops); |
| 1660 | if (IS_ERR(ent)) |
| 1661 | return PTR_ERR(ent); |
Christian König | 6f02a69 | 2017-07-07 11:56:59 +0200 | [diff] [blame] | 1662 | i_size_write(ent->d_inode, adev->mc.gart_size); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1663 | adev->mman.gtt = ent; |
| 1664 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1665 | #endif |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1666 | count = ARRAY_SIZE(amdgpu_ttm_debugfs_list); |
| 1667 | |
| 1668 | #ifdef CONFIG_SWIOTLB |
| 1669 | if (!swiotlb_nr_tbl()) |
| 1670 | --count; |
| 1671 | #endif |
| 1672 | |
| 1673 | return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count); |
| 1674 | #else |
| 1675 | |
| 1676 | return 0; |
| 1677 | #endif |
| 1678 | } |
| 1679 | |
| 1680 | static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev) |
| 1681 | { |
| 1682 | #if defined(CONFIG_DEBUG_FS) |
| 1683 | |
| 1684 | debugfs_remove(adev->mman.vram); |
| 1685 | adev->mman.vram = NULL; |
| 1686 | |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1687 | #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1688 | debugfs_remove(adev->mman.gtt); |
| 1689 | adev->mman.gtt = NULL; |
| 1690 | #endif |
Christian König | a1d2947 | 2016-03-30 14:42:57 +0200 | [diff] [blame] | 1691 | |
| 1692 | #endif |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1693 | } |