blob: da8b0e15a30c08ffd3d8191f5a9827e0f1ed86e3 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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 Yamada248a1d62017-04-24 13:50:21 +090032#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 Deucherd38ceaf2015-04-20 16:55:21 -040037#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önigabca90f2017-06-30 11:05:54 +020050static 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 Deucherd38ceaf2015-04-20 16:55:21 -040056static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
57static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
58
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059/*
60 * Global memory.
61 */
62static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
63{
64 return ttm_mem_global_init(ref->object);
65}
66
67static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
68{
69 ttm_mem_global_release(ref->object);
70}
71
Alex Deucher70b5c5a2016-11-15 16:55:53 -050072static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073{
74 struct drm_global_reference *global_ref;
Christian König703297c2016-02-10 14:20:50 +010075 struct amdgpu_ring *ring;
76 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 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 Ruie9d035e2016-09-07 20:55:42 +080086 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 DRM_ERROR("Failed setting up TTM memory accounting "
88 "subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +080089 goto error_mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 }
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 Ruie9d035e2016-09-07 20:55:42 +0800100 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800102 goto error_bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103 }
104
Christian Königabca90f2017-06-30 11:05:54 +0200105 mutex_init(&adev->mman.gtt_window_lock);
106
Christian König703297c2016-02-10 14:20:50 +0100107 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 Ruie9d035e2016-09-07 20:55:42 +0800111 if (r) {
Christian König703297c2016-02-10 14:20:50 +0100112 DRM_ERROR("Failed setting up TTM BO move run queue.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800113 goto error_entity;
Christian König703297c2016-02-10 14:20:50 +0100114 }
115
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 adev->mman.mem_global_referenced = true;
Christian König703297c2016-02-10 14:20:50 +0100117
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 return 0;
Huang Ruie9d035e2016-09-07 20:55:42 +0800119
120error_entity:
121 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
122error_bo:
123 drm_global_item_unref(&adev->mman.mem_global_ref);
124error_mem:
125 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126}
127
128static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
129{
130 if (adev->mman.mem_global_referenced) {
Christian König703297c2016-02-10 14:20:50 +0100131 amd_sched_entity_fini(adev->mman.entity.sched,
132 &adev->mman.entity);
Christian Königabca90f2017-06-30 11:05:54 +0200133 mutex_destroy(&adev->mman.gtt_window_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 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
140static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
141{
142 return 0;
143}
144
145static 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öniga7d64de2016-09-15 14:58:48 +0200150 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151
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önigbb990bb2016-09-09 16:32:33 +0200160 man->func = &amdgpu_gtt_mgr_func;
Christian König6f02a692017-07-07 11:56:59 +0200161 man->gpu_offset = adev->mc.gart_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162 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önig6a7f76e2016-08-24 15:51:49 +0200168 man->func = &amdgpu_vram_mgr_func;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 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
192static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
193 struct ttm_placement *placement)
194{
Christian Königa7d64de2016-09-15 14:58:48 +0200195 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian König765e7fb2016-09-15 15:06:50 +0200196 struct amdgpu_bo *abo;
Arvind Yadav1aaa5602017-07-02 14:43:58 +0530197 static const struct ttm_place placements = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 .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önig765e7fb2016-09-15 15:06:50 +0200210 abo = container_of(bo, struct amdgpu_bo, tbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211 switch (bo->mem.mem_type) {
212 case TTM_PL_VRAM:
Huang Ruicbcbea92017-04-11 09:24:56 +0800213 if (adev->mman.buffer_funcs &&
214 adev->mman.buffer_funcs_ring &&
215 adev->mman.buffer_funcs_ring->ready == false) {
Christian König765e7fb2016-09-15 15:06:50 +0200216 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Michel Dänzercb2dd1a2017-07-04 17:16:42 +0900217 } else if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
218 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
219 unsigned fpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
220 struct drm_mm_node *node = bo->mem.mm_node;
221 unsigned long pages_left;
222
223 for (pages_left = bo->mem.num_pages;
224 pages_left;
225 pages_left -= node->size, node++) {
226 if (node->start < fpfn)
227 break;
228 }
229
230 if (!pages_left)
231 goto gtt;
232
233 /* Try evicting to the CPU inaccessible part of VRAM
234 * first, but only set GTT as busy placement, so this
235 * BO will be evicted to GTT rather than causing other
236 * BOs to be evicted from VRAM
237 */
238 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
239 AMDGPU_GEM_DOMAIN_GTT);
240 abo->placements[0].fpfn = fpfn;
241 abo->placements[0].lpfn = 0;
242 abo->placement.busy_placement = &abo->placements[1];
243 abo->placement.num_busy_placement = 1;
Christian König08291c52016-09-12 16:06:18 +0200244 } else {
Michel Dänzercb2dd1a2017-07-04 17:16:42 +0900245gtt:
Christian König765e7fb2016-09-15 15:06:50 +0200246 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
Christian König08291c52016-09-12 16:06:18 +0200247 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400248 break;
249 case TTM_PL_TT:
250 default:
Christian König765e7fb2016-09-15 15:06:50 +0200251 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400252 }
Christian König765e7fb2016-09-15 15:06:50 +0200253 *placement = abo->placement;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254}
255
256static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
257{
Christian König765e7fb2016-09-15 15:06:50 +0200258 struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400259
Jérôme Glisse054892e2016-04-19 09:07:51 -0400260 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
261 return -EPERM;
Dave Airlie28a39652016-09-30 13:18:26 +1000262 return drm_vma_node_verify_access(&abo->gem_base.vma_node,
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200263 filp->private_data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400264}
265
266static void amdgpu_move_null(struct ttm_buffer_object *bo,
267 struct ttm_mem_reg *new_mem)
268{
269 struct ttm_mem_reg *old_mem = &bo->mem;
270
271 BUG_ON(old_mem->mm_node != NULL);
272 *old_mem = *new_mem;
273 new_mem->mm_node = NULL;
274}
275
Christian König92c60d92017-06-29 10:44:39 +0200276static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
277 struct drm_mm_node *mm_node,
278 struct ttm_mem_reg *mem)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400279{
Christian Königabca90f2017-06-30 11:05:54 +0200280 uint64_t addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281
Christian Königabca90f2017-06-30 11:05:54 +0200282 if (mem->mem_type != TTM_PL_TT ||
283 amdgpu_gtt_mgr_is_allocated(mem)) {
284 addr = mm_node->start << PAGE_SHIFT;
285 addr += bo->bdev->man[mem->mem_type].gpu_offset;
286 }
Christian König92c60d92017-06-29 10:44:39 +0200287 return addr;
Christian König8892f152016-08-17 10:46:52 +0200288}
289
290static int amdgpu_move_blit(struct ttm_buffer_object *bo,
291 bool evict, bool no_wait_gpu,
292 struct ttm_mem_reg *new_mem,
293 struct ttm_mem_reg *old_mem)
294{
Christian Königa7d64de2016-09-15 14:58:48 +0200295 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian König8892f152016-08-17 10:46:52 +0200296 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
297
298 struct drm_mm_node *old_mm, *new_mm;
299 uint64_t old_start, old_size, new_start, new_size;
300 unsigned long num_pages;
Dave Airlie220196b2016-10-28 11:33:52 +1000301 struct dma_fence *fence = NULL;
Christian König8892f152016-08-17 10:46:52 +0200302 int r;
303
304 BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0);
305
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306 if (!ring->ready) {
307 DRM_ERROR("Trying to move memory with ring turned off.\n");
308 return -EINVAL;
309 }
310
Christian König92c60d92017-06-29 10:44:39 +0200311 old_mm = old_mem->mm_node;
312 old_size = old_mm->size;
313 old_start = amdgpu_mm_node_addr(bo, old_mm, old_mem);
314
Christian König8892f152016-08-17 10:46:52 +0200315 new_mm = new_mem->mm_node;
Christian König8892f152016-08-17 10:46:52 +0200316 new_size = new_mm->size;
Christian König92c60d92017-06-29 10:44:39 +0200317 new_start = amdgpu_mm_node_addr(bo, new_mm, new_mem);
Christian König8892f152016-08-17 10:46:52 +0200318
319 num_pages = new_mem->num_pages;
Christian Königabca90f2017-06-30 11:05:54 +0200320 mutex_lock(&adev->mman.gtt_window_lock);
Christian König8892f152016-08-17 10:46:52 +0200321 while (num_pages) {
Christian Königabca90f2017-06-30 11:05:54 +0200322 unsigned long cur_pages = min(min(old_size, new_size),
323 (u64)AMDGPU_GTT_MAX_TRANSFER_SIZE);
324 uint64_t from = old_start, to = new_start;
Dave Airlie220196b2016-10-28 11:33:52 +1000325 struct dma_fence *next;
Christian König8892f152016-08-17 10:46:52 +0200326
Christian Königabca90f2017-06-30 11:05:54 +0200327 if (old_mem->mem_type == TTM_PL_TT &&
328 !amdgpu_gtt_mgr_is_allocated(old_mem)) {
329 r = amdgpu_map_buffer(bo, old_mem, cur_pages,
330 old_start, 0, ring, &from);
331 if (r)
332 goto error;
333 }
334
335 if (new_mem->mem_type == TTM_PL_TT &&
336 !amdgpu_gtt_mgr_is_allocated(new_mem)) {
337 r = amdgpu_map_buffer(bo, new_mem, cur_pages,
338 new_start, 1, ring, &to);
339 if (r)
340 goto error;
341 }
342
343 r = amdgpu_copy_buffer(ring, from, to,
Christian König8892f152016-08-17 10:46:52 +0200344 cur_pages * PAGE_SIZE,
Christian Königabca90f2017-06-30 11:05:54 +0200345 bo->resv, &next, false, true);
Christian König8892f152016-08-17 10:46:52 +0200346 if (r)
347 goto error;
348
Dave Airlie220196b2016-10-28 11:33:52 +1000349 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200350 fence = next;
351
352 num_pages -= cur_pages;
353 if (!num_pages)
354 break;
355
356 old_size -= cur_pages;
357 if (!old_size) {
Christian König92c60d92017-06-29 10:44:39 +0200358 old_start = amdgpu_mm_node_addr(bo, ++old_mm, old_mem);
Christian König8892f152016-08-17 10:46:52 +0200359 old_size = old_mm->size;
360 } else {
361 old_start += cur_pages * PAGE_SIZE;
362 }
363
364 new_size -= cur_pages;
365 if (!new_size) {
Christian König92c60d92017-06-29 10:44:39 +0200366 new_start = amdgpu_mm_node_addr(bo, ++new_mm, new_mem);
Christian König8892f152016-08-17 10:46:52 +0200367 new_size = new_mm->size;
368 } else {
369 new_start += cur_pages * PAGE_SIZE;
370 }
371 }
Christian Königabca90f2017-06-30 11:05:54 +0200372 mutex_unlock(&adev->mman.gtt_window_lock);
Christian Königce64bc22016-06-15 13:44:05 +0200373
374 r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100375 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376 return r;
Christian König8892f152016-08-17 10:46:52 +0200377
378error:
Christian Königabca90f2017-06-30 11:05:54 +0200379 mutex_unlock(&adev->mman.gtt_window_lock);
380
Christian König8892f152016-08-17 10:46:52 +0200381 if (fence)
Dave Airlie220196b2016-10-28 11:33:52 +1000382 dma_fence_wait(fence, false);
383 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200384 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400385}
386
387static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo,
388 bool evict, bool interruptible,
389 bool no_wait_gpu,
390 struct ttm_mem_reg *new_mem)
391{
392 struct amdgpu_device *adev;
393 struct ttm_mem_reg *old_mem = &bo->mem;
394 struct ttm_mem_reg tmp_mem;
395 struct ttm_place placements;
396 struct ttm_placement placement;
397 int r;
398
Christian Königa7d64de2016-09-15 14:58:48 +0200399 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400400 tmp_mem = *new_mem;
401 tmp_mem.mm_node = NULL;
402 placement.num_placement = 1;
403 placement.placement = &placements;
404 placement.num_busy_placement = 1;
405 placement.busy_placement = &placements;
406 placements.fpfn = 0;
Christian König5e7e8392017-06-30 12:19:42 +0200407 placements.lpfn = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400408 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
409 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
410 interruptible, no_wait_gpu);
411 if (unlikely(r)) {
412 return r;
413 }
414
415 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
416 if (unlikely(r)) {
417 goto out_cleanup;
418 }
419
420 r = ttm_tt_bind(bo->ttm, &tmp_mem);
421 if (unlikely(r)) {
422 goto out_cleanup;
423 }
424 r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
425 if (unlikely(r)) {
426 goto out_cleanup;
427 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900428 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429out_cleanup:
430 ttm_bo_mem_put(bo, &tmp_mem);
431 return r;
432}
433
434static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo,
435 bool evict, bool interruptible,
436 bool no_wait_gpu,
437 struct ttm_mem_reg *new_mem)
438{
439 struct amdgpu_device *adev;
440 struct ttm_mem_reg *old_mem = &bo->mem;
441 struct ttm_mem_reg tmp_mem;
442 struct ttm_placement placement;
443 struct ttm_place placements;
444 int r;
445
Christian Königa7d64de2016-09-15 14:58:48 +0200446 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400447 tmp_mem = *new_mem;
448 tmp_mem.mm_node = NULL;
449 placement.num_placement = 1;
450 placement.placement = &placements;
451 placement.num_busy_placement = 1;
452 placement.busy_placement = &placements;
453 placements.fpfn = 0;
Christian König5e7e8392017-06-30 12:19:42 +0200454 placements.lpfn = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400455 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
456 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
457 interruptible, no_wait_gpu);
458 if (unlikely(r)) {
459 return r;
460 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900461 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462 if (unlikely(r)) {
463 goto out_cleanup;
464 }
465 r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
466 if (unlikely(r)) {
467 goto out_cleanup;
468 }
469out_cleanup:
470 ttm_bo_mem_put(bo, &tmp_mem);
471 return r;
472}
473
474static int amdgpu_bo_move(struct ttm_buffer_object *bo,
475 bool evict, bool interruptible,
476 bool no_wait_gpu,
477 struct ttm_mem_reg *new_mem)
478{
479 struct amdgpu_device *adev;
Michel Dänzer104ece92016-03-28 12:53:02 +0900480 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481 struct ttm_mem_reg *old_mem = &bo->mem;
482 int r;
483
Michel Dänzer104ece92016-03-28 12:53:02 +0900484 /* Can't move a pinned BO */
485 abo = container_of(bo, struct amdgpu_bo, tbo);
486 if (WARN_ON_ONCE(abo->pin_count > 0))
487 return -EINVAL;
488
Christian Königa7d64de2016-09-15 14:58:48 +0200489 adev = amdgpu_ttm_adev(bo->bdev);
Christian Königdbd5ed62016-06-21 16:28:14 +0200490
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
492 amdgpu_move_null(bo, new_mem);
493 return 0;
494 }
495 if ((old_mem->mem_type == TTM_PL_TT &&
496 new_mem->mem_type == TTM_PL_SYSTEM) ||
497 (old_mem->mem_type == TTM_PL_SYSTEM &&
498 new_mem->mem_type == TTM_PL_TT)) {
499 /* bind is enough */
500 amdgpu_move_null(bo, new_mem);
501 return 0;
502 }
503 if (adev->mman.buffer_funcs == NULL ||
504 adev->mman.buffer_funcs_ring == NULL ||
505 !adev->mman.buffer_funcs_ring->ready) {
506 /* use memcpy */
507 goto memcpy;
508 }
509
510 if (old_mem->mem_type == TTM_PL_VRAM &&
511 new_mem->mem_type == TTM_PL_SYSTEM) {
512 r = amdgpu_move_vram_ram(bo, evict, interruptible,
513 no_wait_gpu, new_mem);
514 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
515 new_mem->mem_type == TTM_PL_VRAM) {
516 r = amdgpu_move_ram_vram(bo, evict, interruptible,
517 no_wait_gpu, new_mem);
518 } else {
519 r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
520 }
521
522 if (r) {
523memcpy:
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900524 r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 if (r) {
526 return r;
527 }
528 }
529
John Brooks96cf8272017-06-30 11:31:08 -0400530 if (bo->type == ttm_bo_type_device &&
531 new_mem->mem_type == TTM_PL_VRAM &&
532 old_mem->mem_type != TTM_PL_VRAM) {
533 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
534 * accesses the BO after it's moved.
535 */
536 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
537 }
538
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 /* update statistics */
540 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
541 return 0;
542}
543
544static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
545{
546 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
Christian Königa7d64de2016-09-15 14:58:48 +0200547 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548
549 mem->bus.addr = NULL;
550 mem->bus.offset = 0;
551 mem->bus.size = mem->num_pages << PAGE_SHIFT;
552 mem->bus.base = 0;
553 mem->bus.is_iomem = false;
554 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
555 return -EINVAL;
556 switch (mem->mem_type) {
557 case TTM_PL_SYSTEM:
558 /* system memory */
559 return 0;
560 case TTM_PL_TT:
561 break;
562 case TTM_PL_VRAM:
563 mem->bus.offset = mem->start << PAGE_SHIFT;
564 /* check if it's visible */
565 if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
566 return -EINVAL;
567 mem->bus.base = adev->mc.aper_base;
568 mem->bus.is_iomem = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569 break;
570 default:
571 return -EINVAL;
572 }
573 return 0;
574}
575
576static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
577{
578}
579
Christian König9bbdcc02017-03-29 11:16:05 +0200580static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
581 unsigned long page_offset)
582{
583 struct drm_mm_node *mm = bo->mem.mm_node;
584 uint64_t size = mm->size;
Dave Airlie01687782017-04-07 05:41:42 +1000585 uint64_t offset = page_offset;
Christian König9bbdcc02017-03-29 11:16:05 +0200586
587 page_offset = do_div(offset, size);
Christian Königecdba5d2017-04-07 10:40:04 +0200588 mm += offset;
Christian König9bbdcc02017-03-29 11:16:05 +0200589 return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset;
590}
591
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592/*
593 * TTM backend functions.
594 */
Christian König637dd3b2016-03-03 14:24:57 +0100595struct amdgpu_ttm_gup_task_list {
596 struct list_head list;
597 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598};
599
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100601 struct ttm_dma_tt ttm;
602 struct amdgpu_device *adev;
603 u64 offset;
604 uint64_t userptr;
605 struct mm_struct *usermm;
606 uint32_t userflags;
607 spinlock_t guptasklock;
608 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100609 atomic_t mmu_invalidations;
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800610 struct list_head list;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611};
612
Christian König2f568db2016-02-23 12:36:59 +0100613int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100616 unsigned int flags = 0;
Christian König2f568db2016-02-23 12:36:59 +0100617 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 int r;
619
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100620 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
621 flags |= FOLL_WRITE;
622
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100624 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 to prevent problems with writeback */
626 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
627 struct vm_area_struct *vma;
628
629 vma = find_vma(gtt->usermm, gtt->userptr);
630 if (!vma || vma->vm_file || vma->vm_end < end)
631 return -EPERM;
632 }
633
634 do {
635 unsigned num_pages = ttm->num_pages - pinned;
636 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100637 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100638 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639
Christian König637dd3b2016-03-03 14:24:57 +0100640 guptask.task = current;
641 spin_lock(&gtt->guptasklock);
642 list_add(&guptask.list, &gtt->guptasks);
643 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100645 r = get_user_pages(userptr, num_pages, flags, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100646
647 spin_lock(&gtt->guptasklock);
648 list_del(&guptask.list);
649 spin_unlock(&gtt->guptasklock);
650
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651 if (r < 0)
652 goto release_pages;
653
654 pinned += r;
655
656 } while (pinned < ttm->num_pages);
657
Christian König2f568db2016-02-23 12:36:59 +0100658 return 0;
659
660release_pages:
661 release_pages(pages, pinned, 0);
662 return r;
663}
664
665/* prepare the sg table with the user pages */
666static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
667{
Christian Königa7d64de2016-09-15 14:58:48 +0200668 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Christian König2f568db2016-02-23 12:36:59 +0100669 struct amdgpu_ttm_tt *gtt = (void *)ttm;
670 unsigned nents;
671 int r;
672
673 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
674 enum dma_data_direction direction = write ?
675 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
676
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
678 ttm->num_pages << PAGE_SHIFT,
679 GFP_KERNEL);
680 if (r)
681 goto release_sg;
682
683 r = -ENOMEM;
684 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
685 if (nents != ttm->sg->nents)
686 goto release_sg;
687
688 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
689 gtt->ttm.dma_address, ttm->num_pages);
690
691 return 0;
692
693release_sg:
694 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 return r;
696}
697
698static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
699{
Christian Königa7d64de2016-09-15 14:58:48 +0200700 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 struct amdgpu_ttm_tt *gtt = (void *)ttm;
monk.liudd08fae2015-05-07 14:19:18 -0400702 struct sg_page_iter sg_iter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703
704 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
705 enum dma_data_direction direction = write ?
706 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
707
708 /* double check that we don't free the table twice */
709 if (!ttm->sg->sgl)
710 return;
711
712 /* free the sg table and pages again */
713 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
714
monk.liudd08fae2015-05-07 14:19:18 -0400715 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
716 struct page *page = sg_page_iter_page(&sg_iter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
718 set_page_dirty(page);
719
720 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300721 put_page(page);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722 }
723
724 sg_free_table(ttm->sg);
725}
726
Christian König98a7f882017-06-30 10:41:07 +0200727static int amdgpu_ttm_do_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
728{
729 struct amdgpu_ttm_tt *gtt = (void *)ttm;
730 uint64_t flags;
731 int r;
732
733 spin_lock(&gtt->adev->gtt_list_lock);
734 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, mem);
735 gtt->offset = (u64)mem->start << PAGE_SHIFT;
736 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
737 ttm->pages, gtt->ttm.dma_address, flags);
738
739 if (r) {
740 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
741 ttm->num_pages, gtt->offset);
742 goto error_gart_bind;
743 }
744
745 list_add_tail(&gtt->list, &gtt->adev->gtt_list);
746error_gart_bind:
747 spin_unlock(&gtt->adev->gtt_list_lock);
748 return r;
749
750}
751
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
753 struct ttm_mem_reg *bo_mem)
754{
755 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756 int r;
757
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800758 if (gtt->userptr) {
759 r = amdgpu_ttm_tt_pin_userptr(ttm);
760 if (r) {
761 DRM_ERROR("failed to pin userptr\n");
762 return r;
763 }
764 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765 if (!ttm->num_pages) {
766 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
767 ttm->num_pages, bo_mem, ttm);
768 }
769
770 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
771 bo_mem->mem_type == AMDGPU_PL_GWS ||
772 bo_mem->mem_type == AMDGPU_PL_OA)
773 return -EINVAL;
774
Christian König98a7f882017-06-30 10:41:07 +0200775 if (amdgpu_gtt_mgr_is_allocated(bo_mem))
776 r = amdgpu_ttm_do_bind(ttm, bo_mem);
777
778 return r;
Christian Königc855e252016-09-05 17:00:57 +0200779}
780
781bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
782{
783 struct amdgpu_ttm_tt *gtt = (void *)ttm;
784
785 return gtt && !list_empty(&gtt->list);
786}
787
Christian Königbb990bb2016-09-09 16:32:33 +0200788int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
Christian Königc855e252016-09-05 17:00:57 +0200789{
Christian Königbb990bb2016-09-09 16:32:33 +0200790 struct ttm_tt *ttm = bo->ttm;
Christian Königc855e252016-09-05 17:00:57 +0200791 int r;
792
793 if (!ttm || amdgpu_ttm_is_bound(ttm))
794 return 0;
795
Christian Königbb990bb2016-09-09 16:32:33 +0200796 r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo,
797 NULL, bo_mem);
798 if (r) {
799 DRM_ERROR("Failed to allocate GTT address space (%d)\n", r);
800 return r;
801 }
802
Christian König98a7f882017-06-30 10:41:07 +0200803 return amdgpu_ttm_do_bind(ttm, bo_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804}
805
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800806int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
807{
808 struct amdgpu_ttm_tt *gtt, *tmp;
809 struct ttm_mem_reg bo_mem;
Monk Liu1d1a2cd2017-04-27 17:14:57 +0800810 uint64_t flags;
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800811 int r;
812
813 bo_mem.mem_type = TTM_PL_TT;
814 spin_lock(&adev->gtt_list_lock);
815 list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
816 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, &gtt->ttm.ttm, &bo_mem);
817 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
818 gtt->ttm.ttm.pages, gtt->ttm.dma_address,
819 flags);
820 if (r) {
821 spin_unlock(&adev->gtt_list_lock);
Christian König71c76a02016-09-03 16:18:26 +0200822 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
823 gtt->ttm.ttm.num_pages, gtt->offset);
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800824 return r;
825 }
826 }
827 spin_unlock(&adev->gtt_list_lock);
828 return 0;
829}
830
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400831static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
832{
833 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Roger.He738f64c2017-05-05 13:27:10 +0800834 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400835
Christian König85a4b572016-09-22 14:19:50 +0200836 if (gtt->userptr)
837 amdgpu_ttm_tt_unpin_userptr(ttm);
838
Christian König78ab0a32016-09-09 15:39:08 +0200839 if (!amdgpu_ttm_is_bound(ttm))
840 return 0;
841
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800843 spin_lock(&gtt->adev->gtt_list_lock);
Roger.He738f64c2017-05-05 13:27:10 +0800844 r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
845 if (r) {
846 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
847 gtt->ttm.ttm.num_pages, gtt->offset);
848 goto error_unbind;
849 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800850 list_del_init(&gtt->list);
Roger.He738f64c2017-05-05 13:27:10 +0800851error_unbind:
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800852 spin_unlock(&gtt->adev->gtt_list_lock);
Roger.He738f64c2017-05-05 13:27:10 +0800853 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854}
855
856static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
857{
858 struct amdgpu_ttm_tt *gtt = (void *)ttm;
859
860 ttm_dma_tt_fini(&gtt->ttm);
861 kfree(gtt);
862}
863
864static struct ttm_backend_func amdgpu_backend_func = {
865 .bind = &amdgpu_ttm_backend_bind,
866 .unbind = &amdgpu_ttm_backend_unbind,
867 .destroy = &amdgpu_ttm_backend_destroy,
868};
869
870static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
871 unsigned long size, uint32_t page_flags,
872 struct page *dummy_read_page)
873{
874 struct amdgpu_device *adev;
875 struct amdgpu_ttm_tt *gtt;
876
Christian Königa7d64de2016-09-15 14:58:48 +0200877 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878
879 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
880 if (gtt == NULL) {
881 return NULL;
882 }
883 gtt->ttm.ttm.func = &amdgpu_backend_func;
884 gtt->adev = adev;
885 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
886 kfree(gtt);
887 return NULL;
888 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800889 INIT_LIST_HEAD(&gtt->list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400890 return &gtt->ttm.ttm;
891}
892
893static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
894{
895 struct amdgpu_device *adev;
896 struct amdgpu_ttm_tt *gtt = (void *)ttm;
897 unsigned i;
898 int r;
899 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
900
901 if (ttm->state != tt_unpopulated)
902 return 0;
903
904 if (gtt && gtt->userptr) {
Maninder Singh5f0b34c2015-06-26 13:28:50 +0530905 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400906 if (!ttm->sg)
907 return -ENOMEM;
908
909 ttm->page_flags |= TTM_PAGE_FLAG_SG;
910 ttm->state = tt_unbound;
911 return 0;
912 }
913
914 if (slave && ttm->sg) {
915 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
916 gtt->ttm.dma_address, ttm->num_pages);
917 ttm->state = tt_unbound;
918 return 0;
919 }
920
Christian Königa7d64de2016-09-15 14:58:48 +0200921 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400922
923#ifdef CONFIG_SWIOTLB
924 if (swiotlb_nr_tbl()) {
925 return ttm_dma_populate(&gtt->ttm, adev->dev);
926 }
927#endif
928
929 r = ttm_pool_populate(ttm);
930 if (r) {
931 return r;
932 }
933
934 for (i = 0; i < ttm->num_pages; i++) {
935 gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
936 0, PAGE_SIZE,
937 PCI_DMA_BIDIRECTIONAL);
938 if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
Rasmus Villemoes09ccbb72016-02-15 19:41:45 +0100939 while (i--) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
941 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
942 gtt->ttm.dma_address[i] = 0;
943 }
944 ttm_pool_unpopulate(ttm);
945 return -EFAULT;
946 }
947 }
948 return 0;
949}
950
951static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
952{
953 struct amdgpu_device *adev;
954 struct amdgpu_ttm_tt *gtt = (void *)ttm;
955 unsigned i;
956 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
957
958 if (gtt && gtt->userptr) {
959 kfree(ttm->sg);
960 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
961 return;
962 }
963
964 if (slave)
965 return;
966
Christian Königa7d64de2016-09-15 14:58:48 +0200967 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968
969#ifdef CONFIG_SWIOTLB
970 if (swiotlb_nr_tbl()) {
971 ttm_dma_unpopulate(&gtt->ttm, adev->dev);
972 return;
973 }
974#endif
975
976 for (i = 0; i < ttm->num_pages; i++) {
977 if (gtt->ttm.dma_address[i]) {
978 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
979 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
980 }
981 }
982
983 ttm_pool_unpopulate(ttm);
984}
985
986int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
987 uint32_t flags)
988{
989 struct amdgpu_ttm_tt *gtt = (void *)ttm;
990
991 if (gtt == NULL)
992 return -EINVAL;
993
994 gtt->userptr = addr;
995 gtt->usermm = current->mm;
996 gtt->userflags = flags;
Christian König637dd3b2016-03-03 14:24:57 +0100997 spin_lock_init(&gtt->guptasklock);
998 INIT_LIST_HEAD(&gtt->guptasks);
Christian König2f568db2016-02-23 12:36:59 +0100999 atomic_set(&gtt->mmu_invalidations, 0);
Christian König637dd3b2016-03-03 14:24:57 +01001000
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001 return 0;
1002}
1003
Christian Königcc325d12016-02-08 11:08:35 +01001004struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005{
1006 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1007
1008 if (gtt == NULL)
Christian Königcc325d12016-02-08 11:08:35 +01001009 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010
Christian Königcc325d12016-02-08 11:08:35 +01001011 return gtt->usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001012}
1013
Christian Königcc1de6e2016-02-08 10:57:22 +01001014bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1015 unsigned long end)
1016{
1017 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König637dd3b2016-03-03 14:24:57 +01001018 struct amdgpu_ttm_gup_task_list *entry;
Christian Königcc1de6e2016-02-08 10:57:22 +01001019 unsigned long size;
1020
Christian König637dd3b2016-03-03 14:24:57 +01001021 if (gtt == NULL || !gtt->userptr)
Christian Königcc1de6e2016-02-08 10:57:22 +01001022 return false;
1023
1024 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
1025 if (gtt->userptr > end || gtt->userptr + size <= start)
1026 return false;
1027
Christian König637dd3b2016-03-03 14:24:57 +01001028 spin_lock(&gtt->guptasklock);
1029 list_for_each_entry(entry, &gtt->guptasks, list) {
1030 if (entry->task == current) {
1031 spin_unlock(&gtt->guptasklock);
1032 return false;
1033 }
1034 }
1035 spin_unlock(&gtt->guptasklock);
1036
Christian König2f568db2016-02-23 12:36:59 +01001037 atomic_inc(&gtt->mmu_invalidations);
1038
Christian Königcc1de6e2016-02-08 10:57:22 +01001039 return true;
1040}
1041
Christian König2f568db2016-02-23 12:36:59 +01001042bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1043 int *last_invalidated)
1044{
1045 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1046 int prev_invalidated = *last_invalidated;
1047
1048 *last_invalidated = atomic_read(&gtt->mmu_invalidations);
1049 return prev_invalidated != *last_invalidated;
1050}
1051
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001052bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1053{
1054 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1055
1056 if (gtt == NULL)
1057 return false;
1058
1059 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1060}
1061
Chunming Zhou6b777602016-09-21 16:19:19 +08001062uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001063 struct ttm_mem_reg *mem)
1064{
Chunming Zhou6b777602016-09-21 16:19:19 +08001065 uint64_t flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001066
1067 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1068 flags |= AMDGPU_PTE_VALID;
1069
Christian König6d999052015-12-04 13:32:55 +01001070 if (mem && mem->mem_type == TTM_PL_TT) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001071 flags |= AMDGPU_PTE_SYSTEM;
1072
Christian König6d999052015-12-04 13:32:55 +01001073 if (ttm->caching_state == tt_cached)
1074 flags |= AMDGPU_PTE_SNOOPED;
1075 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076
Alex Xie4b98e0c2017-02-14 12:31:36 -05001077 flags |= adev->gart.gart_pte_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001078 flags |= AMDGPU_PTE_READABLE;
1079
1080 if (!amdgpu_ttm_tt_is_readonly(ttm))
1081 flags |= AMDGPU_PTE_WRITEABLE;
1082
1083 return flags;
1084}
1085
Christian König9982ca62016-10-19 14:44:22 +02001086static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1087 const struct ttm_place *place)
1088{
Christian König4fcae782017-04-20 12:11:47 +02001089 unsigned long num_pages = bo->mem.num_pages;
1090 struct drm_mm_node *node = bo->mem.mm_node;
Christian König9982ca62016-10-19 14:44:22 +02001091
Christian König4fcae782017-04-20 12:11:47 +02001092 if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
1093 return ttm_bo_eviction_valuable(bo, place);
1094
1095 switch (bo->mem.mem_type) {
1096 case TTM_PL_TT:
1097 return true;
1098
1099 case TTM_PL_VRAM:
Christian König9982ca62016-10-19 14:44:22 +02001100 /* Check each drm MM node individually */
1101 while (num_pages) {
1102 if (place->fpfn < (node->start + node->size) &&
1103 !(place->lpfn && place->lpfn <= node->start))
1104 return true;
1105
1106 num_pages -= node->size;
1107 ++node;
1108 }
Christian König4fcae782017-04-20 12:11:47 +02001109 break;
Christian König9982ca62016-10-19 14:44:22 +02001110
Christian König4fcae782017-04-20 12:11:47 +02001111 default:
1112 break;
Christian König9982ca62016-10-19 14:44:22 +02001113 }
1114
1115 return ttm_bo_eviction_valuable(bo, place);
1116}
1117
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118static struct ttm_bo_driver amdgpu_bo_driver = {
1119 .ttm_tt_create = &amdgpu_ttm_tt_create,
1120 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1121 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1122 .invalidate_caches = &amdgpu_invalidate_caches,
1123 .init_mem_type = &amdgpu_init_mem_type,
Christian König9982ca62016-10-19 14:44:22 +02001124 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 .evict_flags = &amdgpu_evict_flags,
1126 .move = &amdgpu_bo_move,
1127 .verify_access = &amdgpu_verify_access,
1128 .move_notify = &amdgpu_bo_move_notify,
1129 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1130 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1131 .io_mem_free = &amdgpu_ttm_io_mem_free,
Christian König9bbdcc02017-03-29 11:16:05 +02001132 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133};
1134
1135int amdgpu_ttm_init(struct amdgpu_device *adev)
1136{
Christian König36d38372017-07-07 13:17:45 +02001137 uint64_t gtt_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001138 int r;
John Brooks218b5dc2017-06-27 22:33:17 -04001139 u64 vis_vram_limit;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140
Alex Deucher70b5c5a2016-11-15 16:55:53 -05001141 r = amdgpu_ttm_global_init(adev);
1142 if (r) {
1143 return r;
1144 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001145 /* No others user of address space so set it to 0 */
1146 r = ttm_bo_device_init(&adev->mman.bdev,
1147 adev->mman.bo_global_ref.ref.object,
1148 &amdgpu_bo_driver,
1149 adev->ddev->anon_inode->i_mapping,
1150 DRM_FILE_PAGE_OFFSET,
1151 adev->need_dma32);
1152 if (r) {
1153 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1154 return r;
1155 }
1156 adev->mman.initialized = true;
1157 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1158 adev->mc.real_vram_size >> PAGE_SHIFT);
1159 if (r) {
1160 DRM_ERROR("Failed initializing VRAM heap.\n");
1161 return r;
1162 }
John Brooks218b5dc2017-06-27 22:33:17 -04001163
1164 /* Reduce size of CPU-visible VRAM if requested */
1165 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1166 if (amdgpu_vis_vram_limit > 0 &&
1167 vis_vram_limit <= adev->mc.visible_vram_size)
1168 adev->mc.visible_vram_size = vis_vram_limit;
1169
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001170 /* Change the size here instead of the init above so only lpfn is affected */
1171 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1172
Huang Rui916910a2017-05-31 10:35:42 +08001173 r = amdgpu_bo_create(adev, adev->mc.stolen_size, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001174 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +02001175 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1176 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +02001177 NULL, NULL, &adev->stollen_vga_memory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001178 if (r) {
1179 return r;
1180 }
1181 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1182 if (r)
1183 return r;
1184 r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
1185 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1186 if (r) {
1187 amdgpu_bo_unref(&adev->stollen_vga_memory);
1188 return r;
1189 }
1190 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1191 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
Christian König36d38372017-07-07 13:17:45 +02001192
1193 if (amdgpu_gtt_size == -1)
1194 gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1195 adev->mc.mc_vram_size);
1196 else
1197 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1198 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 if (r) {
1200 DRM_ERROR("Failed initializing GTT heap.\n");
1201 return r;
1202 }
1203 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
Christian König36d38372017-07-07 13:17:45 +02001204 (unsigned)(gtt_size / (1024 * 1024)));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001205
1206 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1207 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1208 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1209 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1210 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1211 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1212 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1213 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1214 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1215 /* GDS Memory */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001216 if (adev->gds.mem.total_size) {
1217 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1218 adev->gds.mem.total_size >> PAGE_SHIFT);
1219 if (r) {
1220 DRM_ERROR("Failed initializing GDS heap.\n");
1221 return r;
1222 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 }
1224
1225 /* GWS */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001226 if (adev->gds.gws.total_size) {
1227 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1228 adev->gds.gws.total_size >> PAGE_SHIFT);
1229 if (r) {
1230 DRM_ERROR("Failed initializing gws heap.\n");
1231 return r;
1232 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233 }
1234
1235 /* OA */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001236 if (adev->gds.oa.total_size) {
1237 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1238 adev->gds.oa.total_size >> PAGE_SHIFT);
1239 if (r) {
1240 DRM_ERROR("Failed initializing oa heap.\n");
1241 return r;
1242 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001243 }
1244
1245 r = amdgpu_ttm_debugfs_init(adev);
1246 if (r) {
1247 DRM_ERROR("Failed to init debugfs\n");
1248 return r;
1249 }
1250 return 0;
1251}
1252
1253void amdgpu_ttm_fini(struct amdgpu_device *adev)
1254{
1255 int r;
1256
1257 if (!adev->mman.initialized)
1258 return;
1259 amdgpu_ttm_debugfs_fini(adev);
1260 if (adev->stollen_vga_memory) {
Michel Dänzerc81a1a72017-04-28 17:28:14 +09001261 r = amdgpu_bo_reserve(adev->stollen_vga_memory, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262 if (r == 0) {
1263 amdgpu_bo_unpin(adev->stollen_vga_memory);
1264 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1265 }
1266 amdgpu_bo_unref(&adev->stollen_vga_memory);
1267 }
1268 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1269 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
Alex Deucherd2d51d82017-03-15 09:45:48 -04001270 if (adev->gds.mem.total_size)
1271 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1272 if (adev->gds.gws.total_size)
1273 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1274 if (adev->gds.oa.total_size)
1275 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 ttm_bo_device_release(&adev->mman.bdev);
1277 amdgpu_gart_fini(adev);
1278 amdgpu_ttm_global_fini(adev);
1279 adev->mman.initialized = false;
1280 DRM_INFO("amdgpu: ttm finalized\n");
1281}
1282
1283/* this should only be called at bootup or when userspace
1284 * isn't running */
1285void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1286{
1287 struct ttm_mem_type_manager *man;
1288
1289 if (!adev->mman.initialized)
1290 return;
1291
1292 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1293 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1294 man->size = size >> PAGE_SHIFT;
1295}
1296
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1298{
1299 struct drm_file *file_priv;
1300 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301
Christian Könige176fe172015-05-27 10:22:47 +02001302 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001304
1305 file_priv = filp->private_data;
1306 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001307 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001309
1310 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001311}
1312
Christian Königabca90f2017-06-30 11:05:54 +02001313static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
1314 struct ttm_mem_reg *mem, unsigned num_pages,
1315 uint64_t offset, unsigned window,
1316 struct amdgpu_ring *ring,
1317 uint64_t *addr)
1318{
1319 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
1320 struct amdgpu_device *adev = ring->adev;
1321 struct ttm_tt *ttm = bo->ttm;
1322 struct amdgpu_job *job;
1323 unsigned num_dw, num_bytes;
1324 dma_addr_t *dma_address;
1325 struct dma_fence *fence;
1326 uint64_t src_addr, dst_addr;
1327 uint64_t flags;
1328 int r;
1329
1330 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
1331 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
1332
Christian König6f02a692017-07-07 11:56:59 +02001333 *addr = adev->mc.gart_start;
Christian Königabca90f2017-06-30 11:05:54 +02001334 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
1335 AMDGPU_GPU_PAGE_SIZE;
1336
1337 num_dw = adev->mman.buffer_funcs->copy_num_dw;
1338 while (num_dw & 0x7)
1339 num_dw++;
1340
1341 num_bytes = num_pages * 8;
1342
1343 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job);
1344 if (r)
1345 return r;
1346
1347 src_addr = num_dw * 4;
1348 src_addr += job->ibs[0].gpu_addr;
1349
1350 dst_addr = adev->gart.table_addr;
1351 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
1352 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
1353 dst_addr, num_bytes);
1354
1355 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1356 WARN_ON(job->ibs[0].length_dw > num_dw);
1357
1358 dma_address = &gtt->ttm.dma_address[offset >> PAGE_SHIFT];
1359 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem);
1360 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
1361 &job->ibs[0].ptr[num_dw]);
1362 if (r)
1363 goto error_free;
1364
1365 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1366 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
1367 if (r)
1368 goto error_free;
1369
1370 dma_fence_put(fence);
1371
1372 return r;
1373
1374error_free:
1375 amdgpu_job_free(job);
1376 return r;
1377}
1378
Christian Königfc9c8f52017-06-29 11:46:15 +02001379int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1380 uint64_t dst_offset, uint32_t byte_count,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001381 struct reservation_object *resv,
Christian Königfc9c8f52017-06-29 11:46:15 +02001382 struct dma_fence **fence, bool direct_submit,
1383 bool vm_needs_flush)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384{
1385 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001386 struct amdgpu_job *job;
1387
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001388 uint32_t max_bytes;
1389 unsigned num_loops, num_dw;
1390 unsigned i;
1391 int r;
1392
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001393 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1394 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1395 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1396
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001397 /* for IB padding */
1398 while (num_dw & 0x7)
1399 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001400
Christian Königd71518b2016-02-01 12:20:25 +01001401 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1402 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001403 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001404
Christian Königfc9c8f52017-06-29 11:46:15 +02001405 job->vm_needs_flush = vm_needs_flush;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001406 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001407 r = amdgpu_sync_resv(adev, &job->sync, resv,
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001408 AMDGPU_FENCE_OWNER_UNDEFINED);
1409 if (r) {
1410 DRM_ERROR("sync failed (%d).\n", r);
1411 goto error_free;
1412 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001413 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414
1415 for (i = 0; i < num_loops; i++) {
1416 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1417
Christian Königd71518b2016-02-01 12:20:25 +01001418 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1419 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420
1421 src_offset += cur_size_in_bytes;
1422 dst_offset += cur_size_in_bytes;
1423 byte_count -= cur_size_in_bytes;
1424 }
1425
Christian Königd71518b2016-02-01 12:20:25 +01001426 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1427 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001428 if (direct_submit) {
1429 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
Junwei Zhang50ddc752017-01-23 16:30:38 +08001430 NULL, fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001431 job->fence = dma_fence_get(*fence);
Chunming Zhoue24db982016-08-15 10:46:04 +08001432 if (r)
1433 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1434 amdgpu_job_free(job);
1435 } else {
1436 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1437 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1438 if (r)
1439 goto error_free;
1440 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001441
Chunming Zhoue24db982016-08-15 10:46:04 +08001442 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001443
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001444error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001445 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001446 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447}
1448
Flora Cui59b4a972016-07-19 16:48:22 +08001449int amdgpu_fill_buffer(struct amdgpu_bo *bo,
Christian Königf29224a62016-11-17 12:06:38 +01001450 uint32_t src_data,
1451 struct reservation_object *resv,
1452 struct dma_fence **fence)
Flora Cui59b4a972016-07-19 16:48:22 +08001453{
Christian Königa7d64de2016-09-15 14:58:48 +02001454 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian Königf29224a62016-11-17 12:06:38 +01001455 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
Flora Cui59b4a972016-07-19 16:48:22 +08001456 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1457
Christian Königf29224a62016-11-17 12:06:38 +01001458 struct drm_mm_node *mm_node;
1459 unsigned long num_pages;
Flora Cui59b4a972016-07-19 16:48:22 +08001460 unsigned int num_loops, num_dw;
Christian Königf29224a62016-11-17 12:06:38 +01001461
1462 struct amdgpu_job *job;
Flora Cui59b4a972016-07-19 16:48:22 +08001463 int r;
1464
Christian Königf29224a62016-11-17 12:06:38 +01001465 if (!ring->ready) {
1466 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1467 return -EINVAL;
1468 }
1469
Christian König92c60d92017-06-29 10:44:39 +02001470 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
1471 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
1472 if (r)
1473 return r;
1474 }
1475
Christian Königf29224a62016-11-17 12:06:38 +01001476 num_pages = bo->tbo.num_pages;
1477 mm_node = bo->tbo.mem.mm_node;
1478 num_loops = 0;
1479 while (num_pages) {
1480 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1481
1482 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1483 num_pages -= mm_node->size;
1484 ++mm_node;
1485 }
Flora Cui59b4a972016-07-19 16:48:22 +08001486 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
1487
1488 /* for IB padding */
Christian Königf29224a62016-11-17 12:06:38 +01001489 num_dw += 64;
Flora Cui59b4a972016-07-19 16:48:22 +08001490
1491 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1492 if (r)
1493 return r;
1494
1495 if (resv) {
1496 r = amdgpu_sync_resv(adev, &job->sync, resv,
Christian Königf29224a62016-11-17 12:06:38 +01001497 AMDGPU_FENCE_OWNER_UNDEFINED);
Flora Cui59b4a972016-07-19 16:48:22 +08001498 if (r) {
1499 DRM_ERROR("sync failed (%d).\n", r);
1500 goto error_free;
1501 }
1502 }
1503
Christian Königf29224a62016-11-17 12:06:38 +01001504 num_pages = bo->tbo.num_pages;
1505 mm_node = bo->tbo.mem.mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001506
Christian Königf29224a62016-11-17 12:06:38 +01001507 while (num_pages) {
1508 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1509 uint64_t dst_addr;
Flora Cui59b4a972016-07-19 16:48:22 +08001510
Christian König92c60d92017-06-29 10:44:39 +02001511 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
Christian Königf29224a62016-11-17 12:06:38 +01001512 while (byte_count) {
1513 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1514
1515 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
1516 dst_addr, cur_size_in_bytes);
1517
1518 dst_addr += cur_size_in_bytes;
1519 byte_count -= cur_size_in_bytes;
1520 }
1521
1522 num_pages -= mm_node->size;
1523 ++mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001524 }
1525
1526 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1527 WARN_ON(job->ibs[0].length_dw > num_dw);
1528 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
Christian Königf29224a62016-11-17 12:06:38 +01001529 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
Flora Cui59b4a972016-07-19 16:48:22 +08001530 if (r)
1531 goto error_free;
1532
1533 return 0;
1534
1535error_free:
1536 amdgpu_job_free(job);
1537 return r;
1538}
1539
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001540#if defined(CONFIG_DEBUG_FS)
1541
Chunming Zhou05a72a22017-04-13 16:16:51 +08001542extern void amdgpu_gtt_mgr_print(struct seq_file *m, struct ttm_mem_type_manager
1543 *man);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001544static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1545{
1546 struct drm_info_node *node = (struct drm_info_node *)m->private;
1547 unsigned ttm_pl = *(int *)node->info_ent->data;
1548 struct drm_device *dev = node->minor->dev;
1549 struct amdgpu_device *adev = dev->dev_private;
1550 struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Daniel Vetterb5c37142016-12-29 12:09:24 +01001552 struct drm_printer p = drm_seq_file_printer(m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553
1554 spin_lock(&glob->lru_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001555 drm_mm_print(mm, &p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 spin_unlock(&glob->lru_lock);
Chunming Zhou05a72a22017-04-13 16:16:51 +08001557 switch (ttm_pl) {
1558 case TTM_PL_VRAM:
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001559 seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001560 adev->mman.bdev.man[ttm_pl].size,
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001561 (u64)atomic64_read(&adev->vram_usage) >> 20,
1562 (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
Chunming Zhou05a72a22017-04-13 16:16:51 +08001563 break;
1564 case TTM_PL_TT:
1565 amdgpu_gtt_mgr_print(m, &adev->mman.bdev.man[TTM_PL_TT]);
1566 break;
1567 }
Daniel Vetterb5c37142016-12-29 12:09:24 +01001568 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569}
1570
1571static int ttm_pl_vram = TTM_PL_VRAM;
1572static int ttm_pl_tt = TTM_PL_TT;
1573
Nils Wallménius06ab6832016-05-02 12:46:15 -04001574static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001575 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1576 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1577 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1578#ifdef CONFIG_SWIOTLB
1579 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1580#endif
1581};
1582
1583static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1584 size_t size, loff_t *pos)
1585{
Al Viro45063092016-12-04 18:24:56 -05001586 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001587 ssize_t result = 0;
1588 int r;
1589
1590 if (size & 0x3 || *pos & 0x3)
1591 return -EINVAL;
1592
Tom St Denis9156e722017-05-23 11:35:22 -04001593 if (*pos >= adev->mc.mc_vram_size)
1594 return -ENXIO;
1595
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596 while (size) {
1597 unsigned long flags;
1598 uint32_t value;
1599
1600 if (*pos >= adev->mc.mc_vram_size)
1601 return result;
1602
1603 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1604 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1605 WREG32(mmMM_INDEX_HI, *pos >> 31);
1606 value = RREG32(mmMM_DATA);
1607 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1608
1609 r = put_user(value, (uint32_t *)buf);
1610 if (r)
1611 return r;
1612
1613 result += 4;
1614 buf += 4;
1615 *pos += 4;
1616 size -= 4;
1617 }
1618
1619 return result;
1620}
1621
1622static const struct file_operations amdgpu_ttm_vram_fops = {
1623 .owner = THIS_MODULE,
1624 .read = amdgpu_ttm_vram_read,
1625 .llseek = default_llseek
1626};
1627
Christian Königa1d29472016-03-30 14:42:57 +02001628#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1629
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001630static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1631 size_t size, loff_t *pos)
1632{
Al Viro45063092016-12-04 18:24:56 -05001633 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 ssize_t result = 0;
1635 int r;
1636
1637 while (size) {
1638 loff_t p = *pos / PAGE_SIZE;
1639 unsigned off = *pos & ~PAGE_MASK;
1640 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1641 struct page *page;
1642 void *ptr;
1643
1644 if (p >= adev->gart.num_cpu_pages)
1645 return result;
1646
1647 page = adev->gart.pages[p];
1648 if (page) {
1649 ptr = kmap(page);
1650 ptr += off;
1651
1652 r = copy_to_user(buf, ptr, cur_size);
1653 kunmap(adev->gart.pages[p]);
1654 } else
1655 r = clear_user(buf, cur_size);
1656
1657 if (r)
1658 return -EFAULT;
1659
1660 result += cur_size;
1661 buf += cur_size;
1662 *pos += cur_size;
1663 size -= cur_size;
1664 }
1665
1666 return result;
1667}
1668
1669static const struct file_operations amdgpu_ttm_gtt_fops = {
1670 .owner = THIS_MODULE,
1671 .read = amdgpu_ttm_gtt_read,
1672 .llseek = default_llseek
1673};
1674
1675#endif
1676
Christian Königa1d29472016-03-30 14:42:57 +02001677#endif
1678
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001679static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1680{
1681#if defined(CONFIG_DEBUG_FS)
1682 unsigned count;
1683
1684 struct drm_minor *minor = adev->ddev->primary;
1685 struct dentry *ent, *root = minor->debugfs_root;
1686
1687 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1688 adev, &amdgpu_ttm_vram_fops);
1689 if (IS_ERR(ent))
1690 return PTR_ERR(ent);
1691 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1692 adev->mman.vram = ent;
1693
Christian Königa1d29472016-03-30 14:42:57 +02001694#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001695 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1696 adev, &amdgpu_ttm_gtt_fops);
1697 if (IS_ERR(ent))
1698 return PTR_ERR(ent);
Christian König6f02a692017-07-07 11:56:59 +02001699 i_size_write(ent->d_inode, adev->mc.gart_size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 adev->mman.gtt = ent;
1701
Christian Königa1d29472016-03-30 14:42:57 +02001702#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1704
1705#ifdef CONFIG_SWIOTLB
1706 if (!swiotlb_nr_tbl())
1707 --count;
1708#endif
1709
1710 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1711#else
1712
1713 return 0;
1714#endif
1715}
1716
1717static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1718{
1719#if defined(CONFIG_DEBUG_FS)
1720
1721 debugfs_remove(adev->mman.vram);
1722 adev->mman.vram = NULL;
1723
Christian Königa1d29472016-03-30 14:42:57 +02001724#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001725 debugfs_remove(adev->mman.gtt);
1726 adev->mman.gtt = NULL;
1727#endif
Christian Königa1d29472016-03-30 14:42:57 +02001728
1729#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730}