blob: f1b7d987bd572f56247621144d8667a38a548ca8 [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>
Tom St Denis38290b22017-09-18 07:28:14 -040045#include <linux/iommu.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040046#include "amdgpu.h"
Andres Rodriguezb82485f2017-09-15 21:05:19 -040047#include "amdgpu_object.h"
Tom St Denisaca81712017-07-31 09:35:24 -040048#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040049#include "bif/bif_4_1_d.h"
50
51#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
52
Christian Königabca90f2017-06-30 11:05:54 +020053static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
54 struct ttm_mem_reg *mem, unsigned num_pages,
55 uint64_t offset, unsigned window,
56 struct amdgpu_ring *ring,
57 uint64_t *addr);
58
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
60static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
61
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062/*
63 * Global memory.
64 */
65static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
66{
67 return ttm_mem_global_init(ref->object);
68}
69
70static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
71{
72 ttm_mem_global_release(ref->object);
73}
74
Alex Deucher70b5c5a2016-11-15 16:55:53 -050075static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076{
77 struct drm_global_reference *global_ref;
Christian König703297c2016-02-10 14:20:50 +010078 struct amdgpu_ring *ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +010079 struct drm_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080 int r;
81
82 adev->mman.mem_global_referenced = false;
83 global_ref = &adev->mman.mem_global_ref;
84 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
85 global_ref->size = sizeof(struct ttm_mem_global);
86 global_ref->init = &amdgpu_ttm_mem_global_init;
87 global_ref->release = &amdgpu_ttm_mem_global_release;
88 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +080089 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 DRM_ERROR("Failed setting up TTM memory accounting "
91 "subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +080092 goto error_mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093 }
94
95 adev->mman.bo_global_ref.mem_glob =
96 adev->mman.mem_global_ref.object;
97 global_ref = &adev->mman.bo_global_ref.ref;
98 global_ref->global_type = DRM_GLOBAL_TTM_BO;
99 global_ref->size = sizeof(struct ttm_bo_global);
100 global_ref->init = &ttm_bo_global_init;
101 global_ref->release = &ttm_bo_global_release;
102 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +0800103 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800105 goto error_bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 }
107
Christian Königabca90f2017-06-30 11:05:54 +0200108 mutex_init(&adev->mman.gtt_window_lock);
109
Christian König703297c2016-02-10 14:20:50 +0100110 ring = adev->mman.buffer_funcs_ring;
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100111 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
112 r = drm_sched_entity_init(&ring->sched, &adev->mman.entity,
Monk Liub3eebe32017-10-23 12:23:29 +0800113 rq, amdgpu_sched_jobs, NULL);
Huang Ruie9d035e2016-09-07 20:55:42 +0800114 if (r) {
Christian König703297c2016-02-10 14:20:50 +0100115 DRM_ERROR("Failed setting up TTM BO move run queue.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800116 goto error_entity;
Christian König703297c2016-02-10 14:20:50 +0100117 }
118
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 adev->mman.mem_global_referenced = true;
Christian König703297c2016-02-10 14:20:50 +0100120
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121 return 0;
Huang Ruie9d035e2016-09-07 20:55:42 +0800122
123error_entity:
124 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
125error_bo:
126 drm_global_item_unref(&adev->mman.mem_global_ref);
127error_mem:
128 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129}
130
131static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
132{
133 if (adev->mman.mem_global_referenced) {
Lucas Stach1b1f42d2017-12-06 17:49:39 +0100134 drm_sched_entity_fini(adev->mman.entity.sched,
Christian König703297c2016-02-10 14:20:50 +0100135 &adev->mman.entity);
Christian Königabca90f2017-06-30 11:05:54 +0200136 mutex_destroy(&adev->mman.gtt_window_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
138 drm_global_item_unref(&adev->mman.mem_global_ref);
139 adev->mman.mem_global_referenced = false;
140 }
141}
142
143static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
144{
145 return 0;
146}
147
148static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
149 struct ttm_mem_type_manager *man)
150{
151 struct amdgpu_device *adev;
152
Christian Königa7d64de2016-09-15 14:58:48 +0200153 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400154
155 switch (type) {
156 case TTM_PL_SYSTEM:
157 /* System memory */
158 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
159 man->available_caching = TTM_PL_MASK_CACHING;
160 man->default_caching = TTM_PL_FLAG_CACHED;
161 break;
162 case TTM_PL_TT:
Christian Königbb990bb2016-09-09 16:32:33 +0200163 man->func = &amdgpu_gtt_mgr_func;
Christian König6f02a692017-07-07 11:56:59 +0200164 man->gpu_offset = adev->mc.gart_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 man->available_caching = TTM_PL_MASK_CACHING;
166 man->default_caching = TTM_PL_FLAG_CACHED;
167 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
168 break;
169 case TTM_PL_VRAM:
170 /* "On-card" video ram */
Christian König6a7f76e2016-08-24 15:51:49 +0200171 man->func = &amdgpu_vram_mgr_func;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172 man->gpu_offset = adev->mc.vram_start;
173 man->flags = TTM_MEMTYPE_FLAG_FIXED |
174 TTM_MEMTYPE_FLAG_MAPPABLE;
175 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
176 man->default_caching = TTM_PL_FLAG_WC;
177 break;
178 case AMDGPU_PL_GDS:
179 case AMDGPU_PL_GWS:
180 case AMDGPU_PL_OA:
181 /* On-chip GDS memory*/
182 man->func = &ttm_bo_manager_func;
183 man->gpu_offset = 0;
184 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
185 man->available_caching = TTM_PL_FLAG_UNCACHED;
186 man->default_caching = TTM_PL_FLAG_UNCACHED;
187 break;
188 default:
189 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
190 return -EINVAL;
191 }
192 return 0;
193}
194
195static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
196 struct ttm_placement *placement)
197{
Christian Königa7d64de2016-09-15 14:58:48 +0200198 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian König765e7fb2016-09-15 15:06:50 +0200199 struct amdgpu_bo *abo;
Arvind Yadav1aaa5602017-07-02 14:43:58 +0530200 static const struct ttm_place placements = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400201 .fpfn = 0,
202 .lpfn = 0,
203 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
204 };
205
206 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) {
207 placement->placement = &placements;
208 placement->busy_placement = &placements;
209 placement->num_placement = 1;
210 placement->num_busy_placement = 1;
211 return;
212 }
Andres Rodriguezb82485f2017-09-15 21:05:19 -0400213 abo = ttm_to_amdgpu_bo(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214 switch (bo->mem.mem_type) {
215 case TTM_PL_VRAM:
Huang Ruicbcbea92017-04-11 09:24:56 +0800216 if (adev->mman.buffer_funcs &&
217 adev->mman.buffer_funcs_ring &&
218 adev->mman.buffer_funcs_ring->ready == false) {
Christian König765e7fb2016-09-15 15:06:50 +0200219 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Michel Dänzercb2dd1a2017-07-04 17:16:42 +0900220 } else if (adev->mc.visible_vram_size < adev->mc.real_vram_size &&
221 !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED)) {
222 unsigned fpfn = adev->mc.visible_vram_size >> PAGE_SHIFT;
223 struct drm_mm_node *node = bo->mem.mm_node;
224 unsigned long pages_left;
225
226 for (pages_left = bo->mem.num_pages;
227 pages_left;
228 pages_left -= node->size, node++) {
229 if (node->start < fpfn)
230 break;
231 }
232
233 if (!pages_left)
234 goto gtt;
235
236 /* Try evicting to the CPU inaccessible part of VRAM
237 * first, but only set GTT as busy placement, so this
238 * BO will be evicted to GTT rather than causing other
239 * BOs to be evicted from VRAM
240 */
241 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
242 AMDGPU_GEM_DOMAIN_GTT);
243 abo->placements[0].fpfn = fpfn;
244 abo->placements[0].lpfn = 0;
245 abo->placement.busy_placement = &abo->placements[1];
246 abo->placement.num_busy_placement = 1;
Christian König08291c52016-09-12 16:06:18 +0200247 } else {
Michel Dänzercb2dd1a2017-07-04 17:16:42 +0900248gtt:
Christian König765e7fb2016-09-15 15:06:50 +0200249 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
Christian König08291c52016-09-12 16:06:18 +0200250 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400251 break;
252 case TTM_PL_TT:
253 default:
Christian König765e7fb2016-09-15 15:06:50 +0200254 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255 }
Christian König765e7fb2016-09-15 15:06:50 +0200256 *placement = abo->placement;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257}
258
259static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
260{
Andres Rodriguezb82485f2017-09-15 21:05:19 -0400261 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262
Jérôme Glisse054892e2016-04-19 09:07:51 -0400263 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
264 return -EPERM;
Dave Airlie28a39652016-09-30 13:18:26 +1000265 return drm_vma_node_verify_access(&abo->gem_base.vma_node,
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200266 filp->private_data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267}
268
269static void amdgpu_move_null(struct ttm_buffer_object *bo,
270 struct ttm_mem_reg *new_mem)
271{
272 struct ttm_mem_reg *old_mem = &bo->mem;
273
274 BUG_ON(old_mem->mm_node != NULL);
275 *old_mem = *new_mem;
276 new_mem->mm_node = NULL;
277}
278
Christian König92c60d92017-06-29 10:44:39 +0200279static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
280 struct drm_mm_node *mm_node,
281 struct ttm_mem_reg *mem)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282{
Christian Königabca90f2017-06-30 11:05:54 +0200283 uint64_t addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284
Christian König3da917b2017-10-27 14:17:09 +0200285 if (mem->mem_type != TTM_PL_TT || amdgpu_gtt_mgr_has_gart_addr(mem)) {
Christian Königabca90f2017-06-30 11:05:54 +0200286 addr = mm_node->start << PAGE_SHIFT;
287 addr += bo->bdev->man[mem->mem_type].gpu_offset;
288 }
Christian König92c60d92017-06-29 10:44:39 +0200289 return addr;
Christian König8892f152016-08-17 10:46:52 +0200290}
291
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400292/**
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -0400293 * amdgpu_find_mm_node - Helper function finds the drm_mm_node
294 * corresponding to @offset. It also modifies the offset to be
295 * within the drm_mm_node returned
296 */
297static struct drm_mm_node *amdgpu_find_mm_node(struct ttm_mem_reg *mem,
298 unsigned long *offset)
Christian König8892f152016-08-17 10:46:52 +0200299{
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -0400300 struct drm_mm_node *mm_node = mem->mm_node;
301
302 while (*offset >= (mm_node->size << PAGE_SHIFT)) {
303 *offset -= (mm_node->size << PAGE_SHIFT);
304 ++mm_node;
305 }
306 return mm_node;
307}
308
309/**
310 * amdgpu_copy_ttm_mem_to_mem - Helper function for copy
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400311 *
312 * The function copies @size bytes from {src->mem + src->offset} to
313 * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
314 * move and different for a BO to BO copy.
315 *
316 * @f: Returns the last fence if multiple jobs are submitted.
317 */
318int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
319 struct amdgpu_copy_mem *src,
320 struct amdgpu_copy_mem *dst,
321 uint64_t size,
322 struct reservation_object *resv,
323 struct dma_fence **f)
Christian König8892f152016-08-17 10:46:52 +0200324{
Christian König8892f152016-08-17 10:46:52 +0200325 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400326 struct drm_mm_node *src_mm, *dst_mm;
327 uint64_t src_node_start, dst_node_start, src_node_size,
328 dst_node_size, src_page_offset, dst_page_offset;
Dave Airlie220196b2016-10-28 11:33:52 +1000329 struct dma_fence *fence = NULL;
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400330 int r = 0;
331 const uint64_t GTT_MAX_BYTES = (AMDGPU_GTT_MAX_TRANSFER_SIZE *
332 AMDGPU_GPU_PAGE_SIZE);
Christian König8892f152016-08-17 10:46:52 +0200333
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400334 if (!ring->ready) {
335 DRM_ERROR("Trying to move memory with ring turned off.\n");
336 return -EINVAL;
337 }
338
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -0400339 src_mm = amdgpu_find_mm_node(src->mem, &src->offset);
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400340 src_node_start = amdgpu_mm_node_addr(src->bo, src_mm, src->mem) +
341 src->offset;
342 src_node_size = (src_mm->size << PAGE_SHIFT) - src->offset;
343 src_page_offset = src_node_start & (PAGE_SIZE - 1);
Christian König92c60d92017-06-29 10:44:39 +0200344
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -0400345 dst_mm = amdgpu_find_mm_node(dst->mem, &dst->offset);
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400346 dst_node_start = amdgpu_mm_node_addr(dst->bo, dst_mm, dst->mem) +
347 dst->offset;
348 dst_node_size = (dst_mm->size << PAGE_SHIFT) - dst->offset;
349 dst_page_offset = dst_node_start & (PAGE_SIZE - 1);
Christian König8892f152016-08-17 10:46:52 +0200350
Christian Königabca90f2017-06-30 11:05:54 +0200351 mutex_lock(&adev->mman.gtt_window_lock);
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400352
353 while (size) {
354 unsigned long cur_size;
355 uint64_t from = src_node_start, to = dst_node_start;
Dave Airlie220196b2016-10-28 11:33:52 +1000356 struct dma_fence *next;
Christian König8892f152016-08-17 10:46:52 +0200357
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400358 /* Copy size cannot exceed GTT_MAX_BYTES. So if src or dst
359 * begins at an offset, then adjust the size accordingly
360 */
361 cur_size = min3(min(src_node_size, dst_node_size), size,
362 GTT_MAX_BYTES);
363 if (cur_size + src_page_offset > GTT_MAX_BYTES ||
364 cur_size + dst_page_offset > GTT_MAX_BYTES)
365 cur_size -= max(src_page_offset, dst_page_offset);
366
367 /* Map only what needs to be accessed. Map src to window 0 and
368 * dst to window 1
369 */
370 if (src->mem->mem_type == TTM_PL_TT &&
Christian König3da917b2017-10-27 14:17:09 +0200371 !amdgpu_gtt_mgr_has_gart_addr(src->mem)) {
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400372 r = amdgpu_map_buffer(src->bo, src->mem,
373 PFN_UP(cur_size + src_page_offset),
374 src_node_start, 0, ring,
375 &from);
Christian Königabca90f2017-06-30 11:05:54 +0200376 if (r)
377 goto error;
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400378 /* Adjust the offset because amdgpu_map_buffer returns
379 * start of mapped page
380 */
381 from += src_page_offset;
Christian Königabca90f2017-06-30 11:05:54 +0200382 }
383
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400384 if (dst->mem->mem_type == TTM_PL_TT &&
Christian König3da917b2017-10-27 14:17:09 +0200385 !amdgpu_gtt_mgr_has_gart_addr(dst->mem)) {
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400386 r = amdgpu_map_buffer(dst->bo, dst->mem,
387 PFN_UP(cur_size + dst_page_offset),
388 dst_node_start, 1, ring,
389 &to);
Christian Königabca90f2017-06-30 11:05:54 +0200390 if (r)
391 goto error;
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400392 to += dst_page_offset;
Christian Königabca90f2017-06-30 11:05:54 +0200393 }
394
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400395 r = amdgpu_copy_buffer(ring, from, to, cur_size,
396 resv, &next, false, true);
Christian König8892f152016-08-17 10:46:52 +0200397 if (r)
398 goto error;
399
Dave Airlie220196b2016-10-28 11:33:52 +1000400 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200401 fence = next;
402
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400403 size -= cur_size;
404 if (!size)
Christian König8892f152016-08-17 10:46:52 +0200405 break;
406
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400407 src_node_size -= cur_size;
408 if (!src_node_size) {
409 src_node_start = amdgpu_mm_node_addr(src->bo, ++src_mm,
410 src->mem);
411 src_node_size = (src_mm->size << PAGE_SHIFT);
Christian König8892f152016-08-17 10:46:52 +0200412 } else {
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400413 src_node_start += cur_size;
414 src_page_offset = src_node_start & (PAGE_SIZE - 1);
Christian König8892f152016-08-17 10:46:52 +0200415 }
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400416 dst_node_size -= cur_size;
417 if (!dst_node_size) {
418 dst_node_start = amdgpu_mm_node_addr(dst->bo, ++dst_mm,
419 dst->mem);
420 dst_node_size = (dst_mm->size << PAGE_SHIFT);
Christian König8892f152016-08-17 10:46:52 +0200421 } else {
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400422 dst_node_start += cur_size;
423 dst_page_offset = dst_node_start & (PAGE_SIZE - 1);
Christian König8892f152016-08-17 10:46:52 +0200424 }
425 }
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400426error:
Christian Königabca90f2017-06-30 11:05:54 +0200427 mutex_unlock(&adev->mman.gtt_window_lock);
Harish Kasiviswanathan1eca5a52017-10-03 15:41:56 -0400428 if (f)
429 *f = dma_fence_get(fence);
430 dma_fence_put(fence);
431 return r;
432}
433
434
435static int amdgpu_move_blit(struct ttm_buffer_object *bo,
436 bool evict, bool no_wait_gpu,
437 struct ttm_mem_reg *new_mem,
438 struct ttm_mem_reg *old_mem)
439{
440 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
441 struct amdgpu_copy_mem src, dst;
442 struct dma_fence *fence = NULL;
443 int r;
444
445 src.bo = bo;
446 dst.bo = bo;
447 src.mem = old_mem;
448 dst.mem = new_mem;
449 src.offset = 0;
450 dst.offset = 0;
451
452 r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
453 new_mem->num_pages << PAGE_SHIFT,
454 bo->resv, &fence);
455 if (r)
456 goto error;
Christian Königce64bc22016-06-15 13:44:05 +0200457
458 r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100459 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 return r;
Christian König8892f152016-08-17 10:46:52 +0200461
462error:
463 if (fence)
Dave Airlie220196b2016-10-28 11:33:52 +1000464 dma_fence_wait(fence, false);
465 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200466 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400467}
468
Christian Königdfb8fa92017-04-26 16:44:41 +0200469static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo, bool evict,
470 struct ttm_operation_ctx *ctx,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471 struct ttm_mem_reg *new_mem)
472{
473 struct amdgpu_device *adev;
474 struct ttm_mem_reg *old_mem = &bo->mem;
475 struct ttm_mem_reg tmp_mem;
476 struct ttm_place placements;
477 struct ttm_placement placement;
478 int r;
479
Christian Königa7d64de2016-09-15 14:58:48 +0200480 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481 tmp_mem = *new_mem;
482 tmp_mem.mm_node = NULL;
483 placement.num_placement = 1;
484 placement.placement = &placements;
485 placement.num_busy_placement = 1;
486 placement.busy_placement = &placements;
487 placements.fpfn = 0;
Christian König5e7e8392017-06-30 12:19:42 +0200488 placements.lpfn = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400489 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
Christian Königdfb8fa92017-04-26 16:44:41 +0200490 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491 if (unlikely(r)) {
492 return r;
493 }
494
495 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
496 if (unlikely(r)) {
497 goto out_cleanup;
498 }
499
500 r = ttm_tt_bind(bo->ttm, &tmp_mem);
501 if (unlikely(r)) {
502 goto out_cleanup;
503 }
Christian Königdfb8fa92017-04-26 16:44:41 +0200504 r = amdgpu_move_blit(bo, true, ctx->no_wait_gpu, &tmp_mem, old_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 if (unlikely(r)) {
506 goto out_cleanup;
507 }
Roger He3e98d822017-12-08 20:19:32 +0800508 r = ttm_bo_move_ttm(bo, ctx, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509out_cleanup:
510 ttm_bo_mem_put(bo, &tmp_mem);
511 return r;
512}
513
Christian Königdfb8fa92017-04-26 16:44:41 +0200514static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo, bool evict,
515 struct ttm_operation_ctx *ctx,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400516 struct ttm_mem_reg *new_mem)
517{
518 struct amdgpu_device *adev;
519 struct ttm_mem_reg *old_mem = &bo->mem;
520 struct ttm_mem_reg tmp_mem;
521 struct ttm_placement placement;
522 struct ttm_place placements;
523 int r;
524
Christian Königa7d64de2016-09-15 14:58:48 +0200525 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526 tmp_mem = *new_mem;
527 tmp_mem.mm_node = NULL;
528 placement.num_placement = 1;
529 placement.placement = &placements;
530 placement.num_busy_placement = 1;
531 placement.busy_placement = &placements;
532 placements.fpfn = 0;
Christian König5e7e8392017-06-30 12:19:42 +0200533 placements.lpfn = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
Christian Königdfb8fa92017-04-26 16:44:41 +0200535 r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 if (unlikely(r)) {
537 return r;
538 }
Roger He3e98d822017-12-08 20:19:32 +0800539 r = ttm_bo_move_ttm(bo, ctx, &tmp_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540 if (unlikely(r)) {
541 goto out_cleanup;
542 }
Christian Königdfb8fa92017-04-26 16:44:41 +0200543 r = amdgpu_move_blit(bo, true, ctx->no_wait_gpu, new_mem, old_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 if (unlikely(r)) {
545 goto out_cleanup;
546 }
547out_cleanup:
548 ttm_bo_mem_put(bo, &tmp_mem);
549 return r;
550}
551
Christian König2823f4f2017-04-26 16:31:14 +0200552static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
553 struct ttm_operation_ctx *ctx,
554 struct ttm_mem_reg *new_mem)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555{
556 struct amdgpu_device *adev;
Michel Dänzer104ece92016-03-28 12:53:02 +0900557 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400558 struct ttm_mem_reg *old_mem = &bo->mem;
559 int r;
560
Michel Dänzer104ece92016-03-28 12:53:02 +0900561 /* Can't move a pinned BO */
Andres Rodriguezb82485f2017-09-15 21:05:19 -0400562 abo = ttm_to_amdgpu_bo(bo);
Michel Dänzer104ece92016-03-28 12:53:02 +0900563 if (WARN_ON_ONCE(abo->pin_count > 0))
564 return -EINVAL;
565
Christian Königa7d64de2016-09-15 14:58:48 +0200566 adev = amdgpu_ttm_adev(bo->bdev);
Christian Königdbd5ed62016-06-21 16:28:14 +0200567
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400568 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
569 amdgpu_move_null(bo, new_mem);
570 return 0;
571 }
572 if ((old_mem->mem_type == TTM_PL_TT &&
573 new_mem->mem_type == TTM_PL_SYSTEM) ||
574 (old_mem->mem_type == TTM_PL_SYSTEM &&
575 new_mem->mem_type == TTM_PL_TT)) {
576 /* bind is enough */
577 amdgpu_move_null(bo, new_mem);
578 return 0;
579 }
580 if (adev->mman.buffer_funcs == NULL ||
581 adev->mman.buffer_funcs_ring == NULL ||
582 !adev->mman.buffer_funcs_ring->ready) {
583 /* use memcpy */
584 goto memcpy;
585 }
586
587 if (old_mem->mem_type == TTM_PL_VRAM &&
588 new_mem->mem_type == TTM_PL_SYSTEM) {
Christian Königdfb8fa92017-04-26 16:44:41 +0200589 r = amdgpu_move_vram_ram(bo, evict, ctx, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400590 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
591 new_mem->mem_type == TTM_PL_VRAM) {
Christian Königdfb8fa92017-04-26 16:44:41 +0200592 r = amdgpu_move_ram_vram(bo, evict, ctx, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 } else {
Christian König2823f4f2017-04-26 16:31:14 +0200594 r = amdgpu_move_blit(bo, evict, ctx->no_wait_gpu,
595 new_mem, old_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400596 }
597
598 if (r) {
599memcpy:
Roger He3e98d822017-12-08 20:19:32 +0800600 r = ttm_bo_move_memcpy(bo, ctx, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601 if (r) {
602 return r;
603 }
604 }
605
John Brooks96cf8272017-06-30 11:31:08 -0400606 if (bo->type == ttm_bo_type_device &&
607 new_mem->mem_type == TTM_PL_VRAM &&
608 old_mem->mem_type != TTM_PL_VRAM) {
609 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
610 * accesses the BO after it's moved.
611 */
612 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
613 }
614
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 /* update statistics */
616 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
617 return 0;
618}
619
620static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
621{
622 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
Christian Königa7d64de2016-09-15 14:58:48 +0200623 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624
625 mem->bus.addr = NULL;
626 mem->bus.offset = 0;
627 mem->bus.size = mem->num_pages << PAGE_SHIFT;
628 mem->bus.base = 0;
629 mem->bus.is_iomem = false;
630 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
631 return -EINVAL;
632 switch (mem->mem_type) {
633 case TTM_PL_SYSTEM:
634 /* system memory */
635 return 0;
636 case TTM_PL_TT:
637 break;
638 case TTM_PL_VRAM:
639 mem->bus.offset = mem->start << PAGE_SHIFT;
640 /* check if it's visible */
641 if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
642 return -EINVAL;
643 mem->bus.base = adev->mc.aper_base;
644 mem->bus.is_iomem = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 break;
646 default:
647 return -EINVAL;
648 }
649 return 0;
650}
651
652static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
653{
654}
655
Christian König9bbdcc02017-03-29 11:16:05 +0200656static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
657 unsigned long page_offset)
658{
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -0400659 struct drm_mm_node *mm;
660 unsigned long offset = (page_offset << PAGE_SHIFT);
Christian König9bbdcc02017-03-29 11:16:05 +0200661
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -0400662 mm = amdgpu_find_mm_node(&bo->mem, &offset);
663 return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start +
664 (offset >> PAGE_SHIFT);
Christian König9bbdcc02017-03-29 11:16:05 +0200665}
666
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667/*
668 * TTM backend functions.
669 */
Christian König637dd3b2016-03-03 14:24:57 +0100670struct amdgpu_ttm_gup_task_list {
671 struct list_head list;
672 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673};
674
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100676 struct ttm_dma_tt ttm;
677 struct amdgpu_device *adev;
678 u64 offset;
679 uint64_t userptr;
680 struct mm_struct *usermm;
681 uint32_t userflags;
682 spinlock_t guptasklock;
683 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100684 atomic_t mmu_invalidations;
Christian Königca666a32017-09-05 14:30:05 +0200685 uint32_t last_set_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686};
687
Christian König2f568db2016-02-23 12:36:59 +0100688int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100691 unsigned int flags = 0;
Christian König2f568db2016-02-23 12:36:59 +0100692 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 int r;
694
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100695 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
696 flags |= FOLL_WRITE;
697
Christian Königb72cf4f2017-09-03 15:22:06 +0200698 down_read(&current->mm->mmap_sem);
699
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100701 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 to prevent problems with writeback */
703 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
704 struct vm_area_struct *vma;
705
706 vma = find_vma(gtt->usermm, gtt->userptr);
Christian Königb72cf4f2017-09-03 15:22:06 +0200707 if (!vma || vma->vm_file || vma->vm_end < end) {
708 up_read(&current->mm->mmap_sem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 return -EPERM;
Christian Königb72cf4f2017-09-03 15:22:06 +0200710 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 }
712
713 do {
714 unsigned num_pages = ttm->num_pages - pinned;
715 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100716 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100717 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400718
Christian König637dd3b2016-03-03 14:24:57 +0100719 guptask.task = current;
720 spin_lock(&gtt->guptasklock);
721 list_add(&guptask.list, &gtt->guptasks);
722 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100724 r = get_user_pages(userptr, num_pages, flags, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100725
726 spin_lock(&gtt->guptasklock);
727 list_del(&guptask.list);
728 spin_unlock(&gtt->guptasklock);
729
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 if (r < 0)
731 goto release_pages;
732
733 pinned += r;
734
735 } while (pinned < ttm->num_pages);
736
Christian Königb72cf4f2017-09-03 15:22:06 +0200737 up_read(&current->mm->mmap_sem);
Christian König2f568db2016-02-23 12:36:59 +0100738 return 0;
739
740release_pages:
Mel Gormanc6f92f92017-11-15 17:37:55 -0800741 release_pages(pages, pinned);
Christian Königb72cf4f2017-09-03 15:22:06 +0200742 up_read(&current->mm->mmap_sem);
Christian König2f568db2016-02-23 12:36:59 +0100743 return r;
744}
745
Christian Königa216ab02017-09-02 13:21:31 +0200746void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
Tom St Denisaca81712017-07-31 09:35:24 -0400747{
Tom St Denisaca81712017-07-31 09:35:24 -0400748 struct amdgpu_ttm_tt *gtt = (void *)ttm;
749 unsigned i;
750
Christian Königca666a32017-09-05 14:30:05 +0200751 gtt->last_set_pages = atomic_read(&gtt->mmu_invalidations);
Christian Königa216ab02017-09-02 13:21:31 +0200752 for (i = 0; i < ttm->num_pages; ++i) {
753 if (ttm->pages[i])
754 put_page(ttm->pages[i]);
755
756 ttm->pages[i] = pages ? pages[i] : NULL;
Tom St Denisaca81712017-07-31 09:35:24 -0400757 }
758}
759
Christian König1b0c0f92017-09-05 14:36:44 +0200760void amdgpu_ttm_tt_mark_user_pages(struct ttm_tt *ttm)
Tom St Denisaca81712017-07-31 09:35:24 -0400761{
Tom St Denisaca81712017-07-31 09:35:24 -0400762 struct amdgpu_ttm_tt *gtt = (void *)ttm;
763 unsigned i;
764
Christian König1b0c0f92017-09-05 14:36:44 +0200765 for (i = 0; i < ttm->num_pages; ++i) {
766 struct page *page = ttm->pages[i];
767
768 if (!page)
769 continue;
770
771 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
772 set_page_dirty(page);
773
774 mark_page_accessed(page);
Tom St Denisaca81712017-07-31 09:35:24 -0400775 }
776}
777
Christian König2f568db2016-02-23 12:36:59 +0100778/* prepare the sg table with the user pages */
779static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
780{
Christian Königa7d64de2016-09-15 14:58:48 +0200781 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Christian König2f568db2016-02-23 12:36:59 +0100782 struct amdgpu_ttm_tt *gtt = (void *)ttm;
783 unsigned nents;
784 int r;
785
786 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
787 enum dma_data_direction direction = write ?
788 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
789
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400790 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
791 ttm->num_pages << PAGE_SHIFT,
792 GFP_KERNEL);
793 if (r)
794 goto release_sg;
795
796 r = -ENOMEM;
797 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
798 if (nents != ttm->sg->nents)
799 goto release_sg;
800
801 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
802 gtt->ttm.dma_address, ttm->num_pages);
803
804 return 0;
805
806release_sg:
807 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808 return r;
809}
810
811static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
812{
Christian Königa7d64de2016-09-15 14:58:48 +0200813 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400815
816 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
817 enum dma_data_direction direction = write ?
818 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
819
820 /* double check that we don't free the table twice */
821 if (!ttm->sg->sgl)
822 return;
823
824 /* free the sg table and pages again */
825 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
826
Christian König1b0c0f92017-09-05 14:36:44 +0200827 amdgpu_ttm_tt_mark_user_pages(ttm);
Tom St Denisaca81712017-07-31 09:35:24 -0400828
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829 sg_free_table(ttm->sg);
830}
831
832static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
833 struct ttm_mem_reg *bo_mem)
834{
835 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Christian Königac7afe62017-08-22 21:04:47 +0200836 uint64_t flags;
Dan Carpenter2ce3f5dc2017-08-09 13:30:46 +0300837 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400838
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800839 if (gtt->userptr) {
840 r = amdgpu_ttm_tt_pin_userptr(ttm);
841 if (r) {
842 DRM_ERROR("failed to pin userptr\n");
843 return r;
844 }
845 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 if (!ttm->num_pages) {
847 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
848 ttm->num_pages, bo_mem, ttm);
849 }
850
851 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
852 bo_mem->mem_type == AMDGPU_PL_GWS ||
853 bo_mem->mem_type == AMDGPU_PL_OA)
854 return -EINVAL;
855
Christian König3da917b2017-10-27 14:17:09 +0200856 if (!amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
857 gtt->offset = AMDGPU_BO_INVALID_OFFSET;
Christian Königac7afe62017-08-22 21:04:47 +0200858 return 0;
Christian König3da917b2017-10-27 14:17:09 +0200859 }
Christian König98a7f882017-06-30 10:41:07 +0200860
Christian Königac7afe62017-08-22 21:04:47 +0200861 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, bo_mem);
862 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
863 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
864 ttm->pages, gtt->ttm.dma_address, flags);
865
Christian Königc1c7ce82017-10-16 16:50:32 +0200866 if (r)
Christian Königac7afe62017-08-22 21:04:47 +0200867 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
868 ttm->num_pages, gtt->offset);
Christian König98a7f882017-06-30 10:41:07 +0200869 return r;
Christian Königc855e252016-09-05 17:00:57 +0200870}
871
Christian Königc5835bb2017-10-27 15:43:14 +0200872int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
Christian Königc855e252016-09-05 17:00:57 +0200873{
Christian König1d004022017-08-22 16:58:07 +0200874 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian Königc13c55d2017-04-12 15:33:00 +0200875 struct ttm_operation_ctx ctx = { false, false };
Christian König40575732017-10-26 17:54:12 +0200876 struct amdgpu_ttm_tt *gtt = (void*)bo->ttm;
Christian König1d004022017-08-22 16:58:07 +0200877 struct ttm_mem_reg tmp;
Christian König1d004022017-08-22 16:58:07 +0200878 struct ttm_placement placement;
879 struct ttm_place placements;
Christian König40575732017-10-26 17:54:12 +0200880 uint64_t flags;
Christian Königc855e252016-09-05 17:00:57 +0200881 int r;
882
Christian König3da917b2017-10-27 14:17:09 +0200883 if (bo->mem.mem_type != TTM_PL_TT ||
884 amdgpu_gtt_mgr_has_gart_addr(&bo->mem))
Christian Königc855e252016-09-05 17:00:57 +0200885 return 0;
886
Christian König1d004022017-08-22 16:58:07 +0200887 tmp = bo->mem;
888 tmp.mm_node = NULL;
889 placement.num_placement = 1;
890 placement.placement = &placements;
891 placement.num_busy_placement = 1;
892 placement.busy_placement = &placements;
893 placements.fpfn = 0;
894 placements.lpfn = adev->mc.gart_size >> PAGE_SHIFT;
Christian Königec8c9f82017-10-16 13:47:15 +0200895 placements.flags = (bo->mem.placement & ~TTM_PL_MASK_MEM) |
896 TTM_PL_FLAG_TT;
Christian Königbb990bb2016-09-09 16:32:33 +0200897
Christian Königc13c55d2017-04-12 15:33:00 +0200898 r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
Christian König1d004022017-08-22 16:58:07 +0200899 if (unlikely(r))
900 return r;
901
Christian König40575732017-10-26 17:54:12 +0200902 flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
903 gtt->offset = (u64)tmp.start << PAGE_SHIFT;
904 r = amdgpu_gart_bind(adev, gtt->offset, bo->ttm->num_pages,
905 bo->ttm->pages, gtt->ttm.dma_address, flags);
906 if (unlikely(r)) {
Christian König1d004022017-08-22 16:58:07 +0200907 ttm_bo_mem_put(bo, &tmp);
Christian König40575732017-10-26 17:54:12 +0200908 return r;
909 }
Christian König1d004022017-08-22 16:58:07 +0200910
Christian König40575732017-10-26 17:54:12 +0200911 ttm_bo_mem_put(bo, &bo->mem);
912 bo->mem = tmp;
913 bo->offset = (bo->mem.start << PAGE_SHIFT) +
914 bo->bdev->man[bo->mem.mem_type].gpu_offset;
915
916 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400917}
918
Christian Königc1c7ce82017-10-16 16:50:32 +0200919int amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800920{
Christian Königc1c7ce82017-10-16 16:50:32 +0200921 struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
922 struct amdgpu_ttm_tt *gtt = (void *)tbo->ttm;
Monk Liu1d1a2cd2017-04-27 17:14:57 +0800923 uint64_t flags;
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800924 int r;
925
Christian Königc1c7ce82017-10-16 16:50:32 +0200926 if (!gtt)
927 return 0;
928
929 flags = amdgpu_ttm_tt_pte_flags(adev, &gtt->ttm.ttm, &tbo->mem);
930 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
931 gtt->ttm.ttm.pages, gtt->ttm.dma_address, flags);
932 if (r)
933 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
934 gtt->ttm.ttm.num_pages, gtt->offset);
935 return r;
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800936}
937
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
939{
940 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Roger.He738f64c2017-05-05 13:27:10 +0800941 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942
Christian König85a4b572016-09-22 14:19:50 +0200943 if (gtt->userptr)
944 amdgpu_ttm_tt_unpin_userptr(ttm);
945
Christian König3da917b2017-10-27 14:17:09 +0200946 if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
Christian König78ab0a32016-09-09 15:39:08 +0200947 return 0;
948
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400949 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
Roger.He738f64c2017-05-05 13:27:10 +0800950 r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
Christian Königc1c7ce82017-10-16 16:50:32 +0200951 if (r)
Roger.He738f64c2017-05-05 13:27:10 +0800952 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
953 gtt->ttm.ttm.num_pages, gtt->offset);
Roger.He738f64c2017-05-05 13:27:10 +0800954 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955}
956
957static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
958{
959 struct amdgpu_ttm_tt *gtt = (void *)ttm;
960
961 ttm_dma_tt_fini(&gtt->ttm);
962 kfree(gtt);
963}
964
965static struct ttm_backend_func amdgpu_backend_func = {
966 .bind = &amdgpu_ttm_backend_bind,
967 .unbind = &amdgpu_ttm_backend_unbind,
968 .destroy = &amdgpu_ttm_backend_destroy,
969};
970
971static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
972 unsigned long size, uint32_t page_flags,
973 struct page *dummy_read_page)
974{
975 struct amdgpu_device *adev;
976 struct amdgpu_ttm_tt *gtt;
977
Christian Königa7d64de2016-09-15 14:58:48 +0200978 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979
980 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
981 if (gtt == NULL) {
982 return NULL;
983 }
984 gtt->ttm.ttm.func = &amdgpu_backend_func;
985 gtt->adev = adev;
986 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
987 kfree(gtt);
988 return NULL;
989 }
990 return &gtt->ttm.ttm;
991}
992
993static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
994{
Tom St Denisaca81712017-07-31 09:35:24 -0400995 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400997 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
998
999 if (ttm->state != tt_unpopulated)
1000 return 0;
1001
1002 if (gtt && gtt->userptr) {
Maninder Singh5f0b34c2015-06-26 13:28:50 +05301003 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001004 if (!ttm->sg)
1005 return -ENOMEM;
1006
1007 ttm->page_flags |= TTM_PAGE_FLAG_SG;
1008 ttm->state = tt_unbound;
1009 return 0;
1010 }
1011
1012 if (slave && ttm->sg) {
1013 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1014 gtt->ttm.dma_address, ttm->num_pages);
1015 ttm->state = tt_unbound;
Tom St Denis79ba2802017-09-18 08:10:00 -04001016 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001017 }
1018
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001019#ifdef CONFIG_SWIOTLB
1020 if (swiotlb_nr_tbl()) {
Tom St Denis79ba2802017-09-18 08:10:00 -04001021 return ttm_dma_populate(&gtt->ttm, adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001022 }
1023#endif
1024
Tom St Denis79ba2802017-09-18 08:10:00 -04001025 return ttm_populate_and_map_pages(adev->dev, &gtt->ttm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001026}
1027
1028static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
1029{
1030 struct amdgpu_device *adev;
1031 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001032 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1033
1034 if (gtt && gtt->userptr) {
Christian Königa216ab02017-09-02 13:21:31 +02001035 amdgpu_ttm_tt_set_user_pages(ttm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001036 kfree(ttm->sg);
1037 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
1038 return;
1039 }
1040
1041 if (slave)
1042 return;
1043
Christian Königa7d64de2016-09-15 14:58:48 +02001044 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045
1046#ifdef CONFIG_SWIOTLB
1047 if (swiotlb_nr_tbl()) {
1048 ttm_dma_unpopulate(&gtt->ttm, adev->dev);
1049 return;
1050 }
1051#endif
1052
Tom St Denis7405e0d2017-08-18 10:05:48 -04001053 ttm_unmap_and_unpopulate_pages(adev->dev, &gtt->ttm);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001054}
1055
1056int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
1057 uint32_t flags)
1058{
1059 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1060
1061 if (gtt == NULL)
1062 return -EINVAL;
1063
1064 gtt->userptr = addr;
1065 gtt->usermm = current->mm;
1066 gtt->userflags = flags;
Christian König637dd3b2016-03-03 14:24:57 +01001067 spin_lock_init(&gtt->guptasklock);
1068 INIT_LIST_HEAD(&gtt->guptasks);
Christian König2f568db2016-02-23 12:36:59 +01001069 atomic_set(&gtt->mmu_invalidations, 0);
Christian Königca666a32017-09-05 14:30:05 +02001070 gtt->last_set_pages = 0;
Christian König637dd3b2016-03-03 14:24:57 +01001071
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001072 return 0;
1073}
1074
Christian Königcc325d12016-02-08 11:08:35 +01001075struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076{
1077 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1078
1079 if (gtt == NULL)
Christian Königcc325d12016-02-08 11:08:35 +01001080 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081
Christian Königcc325d12016-02-08 11:08:35 +01001082 return gtt->usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083}
1084
Christian Königcc1de6e2016-02-08 10:57:22 +01001085bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1086 unsigned long end)
1087{
1088 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König637dd3b2016-03-03 14:24:57 +01001089 struct amdgpu_ttm_gup_task_list *entry;
Christian Königcc1de6e2016-02-08 10:57:22 +01001090 unsigned long size;
1091
Christian König637dd3b2016-03-03 14:24:57 +01001092 if (gtt == NULL || !gtt->userptr)
Christian Königcc1de6e2016-02-08 10:57:22 +01001093 return false;
1094
1095 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
1096 if (gtt->userptr > end || gtt->userptr + size <= start)
1097 return false;
1098
Christian König637dd3b2016-03-03 14:24:57 +01001099 spin_lock(&gtt->guptasklock);
1100 list_for_each_entry(entry, &gtt->guptasks, list) {
1101 if (entry->task == current) {
1102 spin_unlock(&gtt->guptasklock);
1103 return false;
1104 }
1105 }
1106 spin_unlock(&gtt->guptasklock);
1107
Christian König2f568db2016-02-23 12:36:59 +01001108 atomic_inc(&gtt->mmu_invalidations);
1109
Christian Königcc1de6e2016-02-08 10:57:22 +01001110 return true;
1111}
1112
Christian König2f568db2016-02-23 12:36:59 +01001113bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1114 int *last_invalidated)
1115{
1116 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1117 int prev_invalidated = *last_invalidated;
1118
1119 *last_invalidated = atomic_read(&gtt->mmu_invalidations);
1120 return prev_invalidated != *last_invalidated;
1121}
1122
Christian Königca666a32017-09-05 14:30:05 +02001123bool amdgpu_ttm_tt_userptr_needs_pages(struct ttm_tt *ttm)
1124{
1125 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1126
1127 if (gtt == NULL || !gtt->userptr)
1128 return false;
1129
1130 return atomic_read(&gtt->mmu_invalidations) != gtt->last_set_pages;
1131}
1132
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1134{
1135 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1136
1137 if (gtt == NULL)
1138 return false;
1139
1140 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1141}
1142
Chunming Zhou6b777602016-09-21 16:19:19 +08001143uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144 struct ttm_mem_reg *mem)
1145{
Chunming Zhou6b777602016-09-21 16:19:19 +08001146 uint64_t flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001147
1148 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1149 flags |= AMDGPU_PTE_VALID;
1150
Christian König6d999052015-12-04 13:32:55 +01001151 if (mem && mem->mem_type == TTM_PL_TT) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152 flags |= AMDGPU_PTE_SYSTEM;
1153
Christian König6d999052015-12-04 13:32:55 +01001154 if (ttm->caching_state == tt_cached)
1155 flags |= AMDGPU_PTE_SNOOPED;
1156 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001157
Alex Xie4b98e0c2017-02-14 12:31:36 -05001158 flags |= adev->gart.gart_pte_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159 flags |= AMDGPU_PTE_READABLE;
1160
1161 if (!amdgpu_ttm_tt_is_readonly(ttm))
1162 flags |= AMDGPU_PTE_WRITEABLE;
1163
1164 return flags;
1165}
1166
Christian König9982ca62016-10-19 14:44:22 +02001167static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1168 const struct ttm_place *place)
1169{
Christian König4fcae782017-04-20 12:11:47 +02001170 unsigned long num_pages = bo->mem.num_pages;
1171 struct drm_mm_node *node = bo->mem.mm_node;
Christian König9982ca62016-10-19 14:44:22 +02001172
Christian König4fcae782017-04-20 12:11:47 +02001173 switch (bo->mem.mem_type) {
1174 case TTM_PL_TT:
1175 return true;
1176
1177 case TTM_PL_VRAM:
Christian König9982ca62016-10-19 14:44:22 +02001178 /* Check each drm MM node individually */
1179 while (num_pages) {
1180 if (place->fpfn < (node->start + node->size) &&
1181 !(place->lpfn && place->lpfn <= node->start))
1182 return true;
1183
1184 num_pages -= node->size;
1185 ++node;
1186 }
Roger He7da2e3e2017-11-02 13:14:27 +08001187 return false;
Christian König9982ca62016-10-19 14:44:22 +02001188
Christian König4fcae782017-04-20 12:11:47 +02001189 default:
1190 break;
Christian König9982ca62016-10-19 14:44:22 +02001191 }
1192
1193 return ttm_bo_eviction_valuable(bo, place);
1194}
1195
Felix Kuehlinge3426102017-07-03 14:18:27 -04001196static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1197 unsigned long offset,
1198 void *buf, int len, int write)
1199{
Andres Rodriguezb82485f2017-09-15 21:05:19 -04001200 struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
Felix Kuehlinge3426102017-07-03 14:18:27 -04001201 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -04001202 struct drm_mm_node *nodes;
Felix Kuehlinge3426102017-07-03 14:18:27 -04001203 uint32_t value = 0;
1204 int ret = 0;
1205 uint64_t pos;
1206 unsigned long flags;
1207
1208 if (bo->mem.mem_type != TTM_PL_VRAM)
1209 return -EIO;
1210
Harish Kasiviswanathane1d51502017-10-06 17:36:35 -04001211 nodes = amdgpu_find_mm_node(&abo->tbo.mem, &offset);
Felix Kuehlinge3426102017-07-03 14:18:27 -04001212 pos = (nodes->start << PAGE_SHIFT) + offset;
1213
1214 while (len && pos < adev->mc.mc_vram_size) {
1215 uint64_t aligned_pos = pos & ~(uint64_t)3;
1216 uint32_t bytes = 4 - (pos & 3);
1217 uint32_t shift = (pos & 3) * 8;
1218 uint32_t mask = 0xffffffff << shift;
1219
1220 if (len < bytes) {
1221 mask &= 0xffffffff >> (bytes - len) * 8;
1222 bytes = len;
1223 }
1224
1225 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
Tom St Denis97bae492017-09-14 08:57:26 -04001226 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
1227 WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
Felix Kuehlinge3426102017-07-03 14:18:27 -04001228 if (!write || mask != 0xffffffff)
Tom St Denis97bae492017-09-14 08:57:26 -04001229 value = RREG32_NO_KIQ(mmMM_DATA);
Felix Kuehlinge3426102017-07-03 14:18:27 -04001230 if (write) {
1231 value &= ~mask;
1232 value |= (*(uint32_t *)buf << shift) & mask;
Tom St Denis97bae492017-09-14 08:57:26 -04001233 WREG32_NO_KIQ(mmMM_DATA, value);
Felix Kuehlinge3426102017-07-03 14:18:27 -04001234 }
1235 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1236 if (!write) {
1237 value = (value & mask) >> shift;
1238 memcpy(buf, &value, bytes);
1239 }
1240
1241 ret += bytes;
1242 buf = (uint8_t *)buf + bytes;
1243 pos += bytes;
1244 len -= bytes;
1245 if (pos >= (nodes->start + nodes->size) << PAGE_SHIFT) {
1246 ++nodes;
1247 pos = (nodes->start << PAGE_SHIFT);
1248 }
1249 }
1250
1251 return ret;
1252}
1253
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001254static struct ttm_bo_driver amdgpu_bo_driver = {
1255 .ttm_tt_create = &amdgpu_ttm_tt_create,
1256 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1257 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1258 .invalidate_caches = &amdgpu_invalidate_caches,
1259 .init_mem_type = &amdgpu_init_mem_type,
Christian König9982ca62016-10-19 14:44:22 +02001260 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 .evict_flags = &amdgpu_evict_flags,
1262 .move = &amdgpu_bo_move,
1263 .verify_access = &amdgpu_verify_access,
1264 .move_notify = &amdgpu_bo_move_notify,
1265 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1266 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1267 .io_mem_free = &amdgpu_ttm_io_mem_free,
Christian König9bbdcc02017-03-29 11:16:05 +02001268 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
Felix Kuehlinge3426102017-07-03 14:18:27 -04001269 .access_memory = &amdgpu_ttm_access_memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270};
1271
Alex Deucherf5ec6972017-12-14 16:39:02 -05001272/*
1273 * Firmware Reservation functions
1274 */
1275/**
1276 * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1277 *
1278 * @adev: amdgpu_device pointer
1279 *
1280 * free fw reserved vram if it has been reserved.
1281 */
1282static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1283{
1284 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
1285 NULL, &adev->fw_vram_usage.va);
1286}
1287
1288/**
1289 * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1290 *
1291 * @adev: amdgpu_device pointer
1292 *
1293 * create bo vram reservation from fw.
1294 */
1295static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1296{
1297 struct ttm_operation_ctx ctx = { false, false };
1298 int r = 0;
1299 int i;
1300 u64 vram_size = adev->mc.visible_vram_size;
1301 u64 offset = adev->fw_vram_usage.start_offset;
1302 u64 size = adev->fw_vram_usage.size;
1303 struct amdgpu_bo *bo;
1304
1305 adev->fw_vram_usage.va = NULL;
1306 adev->fw_vram_usage.reserved_bo = NULL;
1307
1308 if (adev->fw_vram_usage.size > 0 &&
1309 adev->fw_vram_usage.size <= vram_size) {
1310
1311 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
1312 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
1313 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1314 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
1315 &adev->fw_vram_usage.reserved_bo);
1316 if (r)
1317 goto error_create;
1318
1319 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
1320 if (r)
1321 goto error_reserve;
1322
1323 /* remove the original mem node and create a new one at the
1324 * request position
1325 */
1326 bo = adev->fw_vram_usage.reserved_bo;
1327 offset = ALIGN(offset, PAGE_SIZE);
1328 for (i = 0; i < bo->placement.num_placement; ++i) {
1329 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1330 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1331 }
1332
1333 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1334 r = ttm_bo_mem_space(&bo->tbo, &bo->placement,
1335 &bo->tbo.mem, &ctx);
1336 if (r)
1337 goto error_pin;
1338
1339 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
1340 AMDGPU_GEM_DOMAIN_VRAM,
1341 adev->fw_vram_usage.start_offset,
1342 (adev->fw_vram_usage.start_offset +
1343 adev->fw_vram_usage.size), NULL);
1344 if (r)
1345 goto error_pin;
1346 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
1347 &adev->fw_vram_usage.va);
1348 if (r)
1349 goto error_kmap;
1350
1351 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
1352 }
1353 return r;
1354
1355error_kmap:
1356 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
1357error_pin:
1358 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
1359error_reserve:
1360 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
1361error_create:
1362 adev->fw_vram_usage.va = NULL;
1363 adev->fw_vram_usage.reserved_bo = NULL;
1364 return r;
1365}
1366
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001367int amdgpu_ttm_init(struct amdgpu_device *adev)
1368{
Christian König36d38372017-07-07 13:17:45 +02001369 uint64_t gtt_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001370 int r;
John Brooks218b5dc2017-06-27 22:33:17 -04001371 u64 vis_vram_limit;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372
Alex Deucher70b5c5a2016-11-15 16:55:53 -05001373 r = amdgpu_ttm_global_init(adev);
1374 if (r) {
1375 return r;
1376 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 /* No others user of address space so set it to 0 */
1378 r = ttm_bo_device_init(&adev->mman.bdev,
1379 adev->mman.bo_global_ref.ref.object,
1380 &amdgpu_bo_driver,
1381 adev->ddev->anon_inode->i_mapping,
1382 DRM_FILE_PAGE_OFFSET,
1383 adev->need_dma32);
1384 if (r) {
1385 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1386 return r;
1387 }
1388 adev->mman.initialized = true;
1389 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1390 adev->mc.real_vram_size >> PAGE_SHIFT);
1391 if (r) {
1392 DRM_ERROR("Failed initializing VRAM heap.\n");
1393 return r;
1394 }
John Brooks218b5dc2017-06-27 22:33:17 -04001395
1396 /* Reduce size of CPU-visible VRAM if requested */
1397 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1398 if (amdgpu_vis_vram_limit > 0 &&
1399 vis_vram_limit <= adev->mc.visible_vram_size)
1400 adev->mc.visible_vram_size = vis_vram_limit;
1401
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001402 /* Change the size here instead of the init above so only lpfn is affected */
1403 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1404
Horace Chena05502e2017-09-29 14:41:57 +08001405 /*
1406 *The reserved vram for firmware must be pinned to the specified
1407 *place on the VRAM, so reserve it early.
1408 */
Alex Deucherf5ec6972017-12-14 16:39:02 -05001409 r = amdgpu_ttm_fw_reserve_vram_init(adev);
Horace Chena05502e2017-09-29 14:41:57 +08001410 if (r) {
1411 return r;
1412 }
1413
Christian Königa4a02772017-07-27 17:24:36 +02001414 r = amdgpu_bo_create_kernel(adev, adev->mc.stolen_size, PAGE_SIZE,
1415 AMDGPU_GEM_DOMAIN_VRAM,
Kent Russell5af2c102017-08-08 07:48:01 -04001416 &adev->stolen_vga_memory,
Christian Königa4a02772017-07-27 17:24:36 +02001417 NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418 if (r)
1419 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1421 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
Christian König36d38372017-07-07 13:17:45 +02001422
Roger He424e2c82017-11-10 19:05:13 +08001423 if (amdgpu_gtt_size == -1) {
1424 struct sysinfo si;
1425
1426 si_meminfo(&si);
Andrey Grodzovsky24562522017-12-15 12:09:16 -05001427 gtt_size = min(max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1428 adev->mc.mc_vram_size),
1429 ((uint64_t)si.totalram * si.mem_unit * 3/4));
1430 }
1431 else
Christian König36d38372017-07-07 13:17:45 +02001432 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1433 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001434 if (r) {
1435 DRM_ERROR("Failed initializing GTT heap.\n");
1436 return r;
1437 }
1438 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
Christian König36d38372017-07-07 13:17:45 +02001439 (unsigned)(gtt_size / (1024 * 1024)));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001440
1441 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1442 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1443 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1444 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1445 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1446 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1447 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1448 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1449 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1450 /* GDS Memory */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001451 if (adev->gds.mem.total_size) {
1452 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1453 adev->gds.mem.total_size >> PAGE_SHIFT);
1454 if (r) {
1455 DRM_ERROR("Failed initializing GDS heap.\n");
1456 return r;
1457 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458 }
1459
1460 /* GWS */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001461 if (adev->gds.gws.total_size) {
1462 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1463 adev->gds.gws.total_size >> PAGE_SHIFT);
1464 if (r) {
1465 DRM_ERROR("Failed initializing gws heap.\n");
1466 return r;
1467 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001468 }
1469
1470 /* OA */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001471 if (adev->gds.oa.total_size) {
1472 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1473 adev->gds.oa.total_size >> PAGE_SHIFT);
1474 if (r) {
1475 DRM_ERROR("Failed initializing oa heap.\n");
1476 return r;
1477 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001478 }
1479
1480 r = amdgpu_ttm_debugfs_init(adev);
1481 if (r) {
1482 DRM_ERROR("Failed to init debugfs\n");
1483 return r;
1484 }
1485 return 0;
1486}
1487
1488void amdgpu_ttm_fini(struct amdgpu_device *adev)
1489{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001490 if (!adev->mman.initialized)
1491 return;
Monk Liu11c6b822017-11-13 20:41:56 +08001492
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493 amdgpu_ttm_debugfs_fini(adev);
Monk Liu11c6b822017-11-13 20:41:56 +08001494 amdgpu_bo_free_kernel(&adev->stolen_vga_memory, NULL, NULL);
Alex Deucherf5ec6972017-12-14 16:39:02 -05001495 amdgpu_ttm_fw_reserve_vram_fini(adev);
Monk Liu11c6b822017-11-13 20:41:56 +08001496
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1498 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
Alex Deucherd2d51d82017-03-15 09:45:48 -04001499 if (adev->gds.mem.total_size)
1500 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1501 if (adev->gds.gws.total_size)
1502 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1503 if (adev->gds.oa.total_size)
1504 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505 ttm_bo_device_release(&adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001506 amdgpu_ttm_global_fini(adev);
1507 adev->mman.initialized = false;
1508 DRM_INFO("amdgpu: ttm finalized\n");
1509}
1510
1511/* this should only be called at bootup or when userspace
1512 * isn't running */
1513void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1514{
1515 struct ttm_mem_type_manager *man;
1516
1517 if (!adev->mman.initialized)
1518 return;
1519
1520 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1521 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1522 man->size = size >> PAGE_SHIFT;
1523}
1524
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1526{
1527 struct drm_file *file_priv;
1528 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001529
Christian Könige176fe172015-05-27 10:22:47 +02001530 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001531 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001532
1533 file_priv = filp->private_data;
1534 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001535 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001537
1538 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539}
1540
Christian Königabca90f2017-06-30 11:05:54 +02001541static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
1542 struct ttm_mem_reg *mem, unsigned num_pages,
1543 uint64_t offset, unsigned window,
1544 struct amdgpu_ring *ring,
1545 uint64_t *addr)
1546{
1547 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
1548 struct amdgpu_device *adev = ring->adev;
1549 struct ttm_tt *ttm = bo->ttm;
1550 struct amdgpu_job *job;
1551 unsigned num_dw, num_bytes;
1552 dma_addr_t *dma_address;
1553 struct dma_fence *fence;
1554 uint64_t src_addr, dst_addr;
1555 uint64_t flags;
1556 int r;
1557
1558 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
1559 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
1560
Christian König6f02a692017-07-07 11:56:59 +02001561 *addr = adev->mc.gart_start;
Christian Königabca90f2017-06-30 11:05:54 +02001562 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
1563 AMDGPU_GPU_PAGE_SIZE;
1564
1565 num_dw = adev->mman.buffer_funcs->copy_num_dw;
1566 while (num_dw & 0x7)
1567 num_dw++;
1568
1569 num_bytes = num_pages * 8;
1570
1571 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job);
1572 if (r)
1573 return r;
1574
1575 src_addr = num_dw * 4;
1576 src_addr += job->ibs[0].gpu_addr;
1577
1578 dst_addr = adev->gart.table_addr;
1579 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
1580 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
1581 dst_addr, num_bytes);
1582
1583 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1584 WARN_ON(job->ibs[0].length_dw > num_dw);
1585
1586 dma_address = &gtt->ttm.dma_address[offset >> PAGE_SHIFT];
1587 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem);
1588 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
1589 &job->ibs[0].ptr[num_dw]);
1590 if (r)
1591 goto error_free;
1592
1593 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1594 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
1595 if (r)
1596 goto error_free;
1597
1598 dma_fence_put(fence);
1599
1600 return r;
1601
1602error_free:
1603 amdgpu_job_free(job);
1604 return r;
1605}
1606
Christian Königfc9c8f52017-06-29 11:46:15 +02001607int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1608 uint64_t dst_offset, uint32_t byte_count,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001609 struct reservation_object *resv,
Christian Königfc9c8f52017-06-29 11:46:15 +02001610 struct dma_fence **fence, bool direct_submit,
1611 bool vm_needs_flush)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612{
1613 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001614 struct amdgpu_job *job;
1615
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616 uint32_t max_bytes;
1617 unsigned num_loops, num_dw;
1618 unsigned i;
1619 int r;
1620
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1622 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1623 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1624
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001625 /* for IB padding */
1626 while (num_dw & 0x7)
1627 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001628
Christian Königd71518b2016-02-01 12:20:25 +01001629 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1630 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001631 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001632
Christian Königfc9c8f52017-06-29 11:46:15 +02001633 job->vm_needs_flush = vm_needs_flush;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001634 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001635 r = amdgpu_sync_resv(adev, &job->sync, resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001636 AMDGPU_FENCE_OWNER_UNDEFINED,
1637 false);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001638 if (r) {
1639 DRM_ERROR("sync failed (%d).\n", r);
1640 goto error_free;
1641 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643
1644 for (i = 0; i < num_loops; i++) {
1645 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1646
Christian Königd71518b2016-02-01 12:20:25 +01001647 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1648 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649
1650 src_offset += cur_size_in_bytes;
1651 dst_offset += cur_size_in_bytes;
1652 byte_count -= cur_size_in_bytes;
1653 }
1654
Christian Königd71518b2016-02-01 12:20:25 +01001655 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1656 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001657 if (direct_submit) {
1658 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
Junwei Zhang50ddc752017-01-23 16:30:38 +08001659 NULL, fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001660 job->fence = dma_fence_get(*fence);
Chunming Zhoue24db982016-08-15 10:46:04 +08001661 if (r)
1662 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1663 amdgpu_job_free(job);
1664 } else {
1665 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1666 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1667 if (r)
1668 goto error_free;
1669 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001670
Chunming Zhoue24db982016-08-15 10:46:04 +08001671 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001672
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001673error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001674 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001675 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001676}
1677
Flora Cui59b4a972016-07-19 16:48:22 +08001678int amdgpu_fill_buffer(struct amdgpu_bo *bo,
Yong Zhao330df032017-07-20 18:44:10 -04001679 uint64_t src_data,
Christian Königf29224a62016-11-17 12:06:38 +01001680 struct reservation_object *resv,
1681 struct dma_fence **fence)
Flora Cui59b4a972016-07-19 16:48:22 +08001682{
Christian Königa7d64de2016-09-15 14:58:48 +02001683 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001684 uint32_t max_bytes = 8 *
1685 adev->vm_manager.vm_pte_funcs->set_max_nums_pte_pde;
Flora Cui59b4a972016-07-19 16:48:22 +08001686 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1687
Christian Königf29224a62016-11-17 12:06:38 +01001688 struct drm_mm_node *mm_node;
1689 unsigned long num_pages;
Flora Cui59b4a972016-07-19 16:48:22 +08001690 unsigned int num_loops, num_dw;
Christian Königf29224a62016-11-17 12:06:38 +01001691
1692 struct amdgpu_job *job;
Flora Cui59b4a972016-07-19 16:48:22 +08001693 int r;
1694
Christian Königf29224a62016-11-17 12:06:38 +01001695 if (!ring->ready) {
1696 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1697 return -EINVAL;
1698 }
1699
Christian König92c60d92017-06-29 10:44:39 +02001700 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
Christian Königc5835bb2017-10-27 15:43:14 +02001701 r = amdgpu_ttm_alloc_gart(&bo->tbo);
Christian König92c60d92017-06-29 10:44:39 +02001702 if (r)
1703 return r;
1704 }
1705
Christian Königf29224a62016-11-17 12:06:38 +01001706 num_pages = bo->tbo.num_pages;
1707 mm_node = bo->tbo.mem.mm_node;
1708 num_loops = 0;
1709 while (num_pages) {
1710 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1711
1712 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1713 num_pages -= mm_node->size;
1714 ++mm_node;
1715 }
Yong Zhao330df032017-07-20 18:44:10 -04001716
Yong Zhao7bdc53f2017-09-15 18:20:37 -04001717 /* num of dwords for each SDMA_OP_PTEPDE cmd */
1718 num_dw = num_loops * adev->vm_manager.vm_pte_funcs->set_pte_pde_num_dw;
Flora Cui59b4a972016-07-19 16:48:22 +08001719
1720 /* for IB padding */
Christian Königf29224a62016-11-17 12:06:38 +01001721 num_dw += 64;
Flora Cui59b4a972016-07-19 16:48:22 +08001722
1723 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1724 if (r)
1725 return r;
1726
1727 if (resv) {
1728 r = amdgpu_sync_resv(adev, &job->sync, resv,
Andres Rodriguez177ae092017-09-15 20:44:06 -04001729 AMDGPU_FENCE_OWNER_UNDEFINED, false);
Flora Cui59b4a972016-07-19 16:48:22 +08001730 if (r) {
1731 DRM_ERROR("sync failed (%d).\n", r);
1732 goto error_free;
1733 }
1734 }
1735
Christian Königf29224a62016-11-17 12:06:38 +01001736 num_pages = bo->tbo.num_pages;
1737 mm_node = bo->tbo.mem.mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001738
Christian Königf29224a62016-11-17 12:06:38 +01001739 while (num_pages) {
1740 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1741 uint64_t dst_addr;
Flora Cui59b4a972016-07-19 16:48:22 +08001742
Yong Zhao330df032017-07-20 18:44:10 -04001743 WARN_ONCE(byte_count & 0x7, "size should be a multiple of 8");
1744
Christian König92c60d92017-06-29 10:44:39 +02001745 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
Christian Königf29224a62016-11-17 12:06:38 +01001746 while (byte_count) {
1747 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1748
Yong Zhao330df032017-07-20 18:44:10 -04001749 amdgpu_vm_set_pte_pde(adev, &job->ibs[0],
1750 dst_addr, 0,
1751 cur_size_in_bytes >> 3, 0,
1752 src_data);
Christian Königf29224a62016-11-17 12:06:38 +01001753
1754 dst_addr += cur_size_in_bytes;
1755 byte_count -= cur_size_in_bytes;
1756 }
1757
1758 num_pages -= mm_node->size;
1759 ++mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001760 }
1761
1762 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1763 WARN_ON(job->ibs[0].length_dw > num_dw);
1764 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
Christian Königf29224a62016-11-17 12:06:38 +01001765 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
Flora Cui59b4a972016-07-19 16:48:22 +08001766 if (r)
1767 goto error_free;
1768
1769 return 0;
1770
1771error_free:
1772 amdgpu_job_free(job);
1773 return r;
1774}
1775
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776#if defined(CONFIG_DEBUG_FS)
1777
1778static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1779{
1780 struct drm_info_node *node = (struct drm_info_node *)m->private;
1781 unsigned ttm_pl = *(int *)node->info_ent->data;
1782 struct drm_device *dev = node->minor->dev;
1783 struct amdgpu_device *adev = dev->dev_private;
Christian König12d4ac52017-08-07 14:07:43 +02001784 struct ttm_mem_type_manager *man = &adev->mman.bdev.man[ttm_pl];
Daniel Vetterb5c37142016-12-29 12:09:24 +01001785 struct drm_printer p = drm_seq_file_printer(m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001786
Christian König12d4ac52017-08-07 14:07:43 +02001787 man->func->debug(man, &p);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001788 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789}
1790
1791static int ttm_pl_vram = TTM_PL_VRAM;
1792static int ttm_pl_tt = TTM_PL_TT;
1793
Nils Wallménius06ab6832016-05-02 12:46:15 -04001794static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1796 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1797 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1798#ifdef CONFIG_SWIOTLB
1799 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1800#endif
1801};
1802
1803static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1804 size_t size, loff_t *pos)
1805{
Al Viro45063092016-12-04 18:24:56 -05001806 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001807 ssize_t result = 0;
1808 int r;
1809
1810 if (size & 0x3 || *pos & 0x3)
1811 return -EINVAL;
1812
Tom St Denis9156e722017-05-23 11:35:22 -04001813 if (*pos >= adev->mc.mc_vram_size)
1814 return -ENXIO;
1815
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001816 while (size) {
1817 unsigned long flags;
1818 uint32_t value;
1819
1820 if (*pos >= adev->mc.mc_vram_size)
1821 return result;
1822
1823 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
Tom St Denisc30572812017-09-13 12:35:15 -04001824 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1825 WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
1826 value = RREG32_NO_KIQ(mmMM_DATA);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001827 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1828
1829 r = put_user(value, (uint32_t *)buf);
1830 if (r)
1831 return r;
1832
1833 result += 4;
1834 buf += 4;
1835 *pos += 4;
1836 size -= 4;
1837 }
1838
1839 return result;
1840}
1841
Tom St Denis08cab982017-08-29 08:36:52 -04001842static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
1843 size_t size, loff_t *pos)
1844{
1845 struct amdgpu_device *adev = file_inode(f)->i_private;
1846 ssize_t result = 0;
1847 int r;
1848
1849 if (size & 0x3 || *pos & 0x3)
1850 return -EINVAL;
1851
1852 if (*pos >= adev->mc.mc_vram_size)
1853 return -ENXIO;
1854
1855 while (size) {
1856 unsigned long flags;
1857 uint32_t value;
1858
1859 if (*pos >= adev->mc.mc_vram_size)
1860 return result;
1861
1862 r = get_user(value, (uint32_t *)buf);
1863 if (r)
1864 return r;
1865
1866 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
Tom St Denisc30572812017-09-13 12:35:15 -04001867 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1868 WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
1869 WREG32_NO_KIQ(mmMM_DATA, value);
Tom St Denis08cab982017-08-29 08:36:52 -04001870 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1871
1872 result += 4;
1873 buf += 4;
1874 *pos += 4;
1875 size -= 4;
1876 }
1877
1878 return result;
1879}
1880
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001881static const struct file_operations amdgpu_ttm_vram_fops = {
1882 .owner = THIS_MODULE,
1883 .read = amdgpu_ttm_vram_read,
Tom St Denis08cab982017-08-29 08:36:52 -04001884 .write = amdgpu_ttm_vram_write,
1885 .llseek = default_llseek,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886};
1887
Christian Königa1d29472016-03-30 14:42:57 +02001888#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1889
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001890static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1891 size_t size, loff_t *pos)
1892{
Al Viro45063092016-12-04 18:24:56 -05001893 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001894 ssize_t result = 0;
1895 int r;
1896
1897 while (size) {
1898 loff_t p = *pos / PAGE_SIZE;
1899 unsigned off = *pos & ~PAGE_MASK;
1900 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1901 struct page *page;
1902 void *ptr;
1903
1904 if (p >= adev->gart.num_cpu_pages)
1905 return result;
1906
1907 page = adev->gart.pages[p];
1908 if (page) {
1909 ptr = kmap(page);
1910 ptr += off;
1911
1912 r = copy_to_user(buf, ptr, cur_size);
1913 kunmap(adev->gart.pages[p]);
1914 } else
1915 r = clear_user(buf, cur_size);
1916
1917 if (r)
1918 return -EFAULT;
1919
1920 result += cur_size;
1921 buf += cur_size;
1922 *pos += cur_size;
1923 size -= cur_size;
1924 }
1925
1926 return result;
1927}
1928
1929static const struct file_operations amdgpu_ttm_gtt_fops = {
1930 .owner = THIS_MODULE,
1931 .read = amdgpu_ttm_gtt_read,
1932 .llseek = default_llseek
1933};
1934
1935#endif
1936
Tom St Denis38290b22017-09-18 07:28:14 -04001937static ssize_t amdgpu_iova_to_phys_read(struct file *f, char __user *buf,
1938 size_t size, loff_t *pos)
1939{
1940 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis38290b22017-09-18 07:28:14 -04001941 int r;
1942 uint64_t phys;
Tom St Denis38290b22017-09-18 07:28:14 -04001943 struct iommu_domain *dom;
Tom St Denisa40cfa02017-09-18 07:14:56 -04001944
Tom St Denis10cfafd2017-09-19 11:29:04 -04001945 // always return 8 bytes
1946 if (size != 8)
1947 return -EINVAL;
Tom St Denis38290b22017-09-18 07:28:14 -04001948
Tom St Denis10cfafd2017-09-19 11:29:04 -04001949 // only accept page addresses
1950 if (*pos & 0xFFF)
1951 return -EINVAL;
Tom St Denis38290b22017-09-18 07:28:14 -04001952
1953 dom = iommu_get_domain_for_dev(adev->dev);
Tom St Denis10cfafd2017-09-19 11:29:04 -04001954 if (dom)
1955 phys = iommu_iova_to_phys(dom, *pos);
1956 else
1957 phys = *pos;
1958
1959 r = copy_to_user(buf, &phys, 8);
1960 if (r)
Tom St Denis38290b22017-09-18 07:28:14 -04001961 return -EFAULT;
1962
Tom St Denis10cfafd2017-09-19 11:29:04 -04001963 return 8;
Tom St Denis38290b22017-09-18 07:28:14 -04001964}
1965
1966static const struct file_operations amdgpu_ttm_iova_fops = {
1967 .owner = THIS_MODULE,
1968 .read = amdgpu_iova_to_phys_read,
Tom St Denis38290b22017-09-18 07:28:14 -04001969 .llseek = default_llseek
1970};
Tom St Denisa40cfa02017-09-18 07:14:56 -04001971
1972static const struct {
1973 char *name;
1974 const struct file_operations *fops;
1975 int domain;
1976} ttm_debugfs_entries[] = {
1977 { "amdgpu_vram", &amdgpu_ttm_vram_fops, TTM_PL_VRAM },
1978#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1979 { "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
1980#endif
Tom St Denis38290b22017-09-18 07:28:14 -04001981 { "amdgpu_iova", &amdgpu_ttm_iova_fops, TTM_PL_SYSTEM },
Tom St Denisa40cfa02017-09-18 07:14:56 -04001982};
1983
Christian Königa1d29472016-03-30 14:42:57 +02001984#endif
1985
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1987{
1988#if defined(CONFIG_DEBUG_FS)
1989 unsigned count;
1990
1991 struct drm_minor *minor = adev->ddev->primary;
1992 struct dentry *ent, *root = minor->debugfs_root;
1993
Tom St Denisa40cfa02017-09-18 07:14:56 -04001994 for (count = 0; count < ARRAY_SIZE(ttm_debugfs_entries); count++) {
1995 ent = debugfs_create_file(
1996 ttm_debugfs_entries[count].name,
1997 S_IFREG | S_IRUGO, root,
1998 adev,
1999 ttm_debugfs_entries[count].fops);
2000 if (IS_ERR(ent))
2001 return PTR_ERR(ent);
2002 if (ttm_debugfs_entries[count].domain == TTM_PL_VRAM)
2003 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
2004 else if (ttm_debugfs_entries[count].domain == TTM_PL_TT)
2005 i_size_write(ent->d_inode, adev->mc.gart_size);
2006 adev->mman.debugfs_entries[count] = ent;
2007 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002008
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
2010
2011#ifdef CONFIG_SWIOTLB
2012 if (!swiotlb_nr_tbl())
2013 --count;
2014#endif
2015
2016 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
2017#else
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002018 return 0;
2019#endif
2020}
2021
2022static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
2023{
2024#if defined(CONFIG_DEBUG_FS)
Tom St Denisa40cfa02017-09-18 07:14:56 -04002025 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002026
Tom St Denisa40cfa02017-09-18 07:14:56 -04002027 for (i = 0; i < ARRAY_SIZE(ttm_debugfs_entries); i++)
2028 debugfs_remove(adev->mman.debugfs_entries[i]);
Christian Königa1d29472016-03-30 14:42:57 +02002029#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030}