blob: e3008d55ec77ce48bae2172ba5192c2d0d16d170 [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 */
32#include <ttm/ttm_bo_api.h>
33#include <ttm/ttm_bo_driver.h>
34#include <ttm/ttm_placement.h>
35#include <ttm/ttm_module.h>
36#include <ttm/ttm_page_alloc.h>
37#include <drm/drmP.h>
38#include <drm/amdgpu_drm.h>
39#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/swiotlb.h>
42#include <linux/swap.h>
43#include <linux/pagemap.h>
44#include <linux/debugfs.h>
45#include "amdgpu.h"
46#include "bif/bif_4_1_d.h"
47
48#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
49
50static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
51static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
52
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053
54/*
55 * Global memory.
56 */
57static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
58{
59 return ttm_mem_global_init(ref->object);
60}
61
62static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
63{
64 ttm_mem_global_release(ref->object);
65}
66
Alex Deucher70b5c5a2016-11-15 16:55:53 -050067static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068{
69 struct drm_global_reference *global_ref;
Christian König703297c2016-02-10 14:20:50 +010070 struct amdgpu_ring *ring;
71 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 int r;
73
74 adev->mman.mem_global_referenced = false;
75 global_ref = &adev->mman.mem_global_ref;
76 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
77 global_ref->size = sizeof(struct ttm_mem_global);
78 global_ref->init = &amdgpu_ttm_mem_global_init;
79 global_ref->release = &amdgpu_ttm_mem_global_release;
80 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +080081 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040082 DRM_ERROR("Failed setting up TTM memory accounting "
83 "subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +080084 goto error_mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 }
86
87 adev->mman.bo_global_ref.mem_glob =
88 adev->mman.mem_global_ref.object;
89 global_ref = &adev->mman.bo_global_ref.ref;
90 global_ref->global_type = DRM_GLOBAL_TTM_BO;
91 global_ref->size = sizeof(struct ttm_bo_global);
92 global_ref->init = &ttm_bo_global_init;
93 global_ref->release = &ttm_bo_global_release;
94 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +080095 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +080097 goto error_bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 }
99
Christian König703297c2016-02-10 14:20:50 +0100100 ring = adev->mman.buffer_funcs_ring;
101 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
102 r = amd_sched_entity_init(&ring->sched, &adev->mman.entity,
103 rq, amdgpu_sched_jobs);
Huang Ruie9d035e2016-09-07 20:55:42 +0800104 if (r) {
Christian König703297c2016-02-10 14:20:50 +0100105 DRM_ERROR("Failed setting up TTM BO move run queue.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800106 goto error_entity;
Christian König703297c2016-02-10 14:20:50 +0100107 }
108
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 adev->mman.mem_global_referenced = true;
Christian König703297c2016-02-10 14:20:50 +0100110
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111 return 0;
Huang Ruie9d035e2016-09-07 20:55:42 +0800112
113error_entity:
114 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
115error_bo:
116 drm_global_item_unref(&adev->mman.mem_global_ref);
117error_mem:
118 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119}
120
121static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
122{
123 if (adev->mman.mem_global_referenced) {
Christian König703297c2016-02-10 14:20:50 +0100124 amd_sched_entity_fini(adev->mman.entity.sched,
125 &adev->mman.entity);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
127 drm_global_item_unref(&adev->mman.mem_global_ref);
128 adev->mman.mem_global_referenced = false;
129 }
130}
131
132static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
133{
134 return 0;
135}
136
137static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
138 struct ttm_mem_type_manager *man)
139{
140 struct amdgpu_device *adev;
141
Christian Königa7d64de2016-09-15 14:58:48 +0200142 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143
144 switch (type) {
145 case TTM_PL_SYSTEM:
146 /* System memory */
147 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
148 man->available_caching = TTM_PL_MASK_CACHING;
149 man->default_caching = TTM_PL_FLAG_CACHED;
150 break;
151 case TTM_PL_TT:
Christian Königbb990bb2016-09-09 16:32:33 +0200152 man->func = &amdgpu_gtt_mgr_func;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400153 man->gpu_offset = adev->mc.gtt_start;
154 man->available_caching = TTM_PL_MASK_CACHING;
155 man->default_caching = TTM_PL_FLAG_CACHED;
156 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
157 break;
158 case TTM_PL_VRAM:
159 /* "On-card" video ram */
Christian König6a7f76e2016-08-24 15:51:49 +0200160 man->func = &amdgpu_vram_mgr_func;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161 man->gpu_offset = adev->mc.vram_start;
162 man->flags = TTM_MEMTYPE_FLAG_FIXED |
163 TTM_MEMTYPE_FLAG_MAPPABLE;
164 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
165 man->default_caching = TTM_PL_FLAG_WC;
166 break;
167 case AMDGPU_PL_GDS:
168 case AMDGPU_PL_GWS:
169 case AMDGPU_PL_OA:
170 /* On-chip GDS memory*/
171 man->func = &ttm_bo_manager_func;
172 man->gpu_offset = 0;
173 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
174 man->available_caching = TTM_PL_FLAG_UNCACHED;
175 man->default_caching = TTM_PL_FLAG_UNCACHED;
176 break;
177 default:
178 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
179 return -EINVAL;
180 }
181 return 0;
182}
183
184static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
185 struct ttm_placement *placement)
186{
Christian Königa7d64de2016-09-15 14:58:48 +0200187 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian König765e7fb2016-09-15 15:06:50 +0200188 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400189 static struct ttm_place placements = {
190 .fpfn = 0,
191 .lpfn = 0,
192 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
193 };
Christian König08291c52016-09-12 16:06:18 +0200194 unsigned i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195
196 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) {
197 placement->placement = &placements;
198 placement->busy_placement = &placements;
199 placement->num_placement = 1;
200 placement->num_busy_placement = 1;
201 return;
202 }
Christian König765e7fb2016-09-15 15:06:50 +0200203 abo = container_of(bo, struct amdgpu_bo, tbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204 switch (bo->mem.mem_type) {
205 case TTM_PL_VRAM:
Christian Königa7d64de2016-09-15 14:58:48 +0200206 if (adev->mman.buffer_funcs_ring->ready == false) {
Christian König765e7fb2016-09-15 15:06:50 +0200207 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Christian König08291c52016-09-12 16:06:18 +0200208 } else {
Christian König765e7fb2016-09-15 15:06:50 +0200209 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
210 for (i = 0; i < abo->placement.num_placement; ++i) {
211 if (!(abo->placements[i].flags &
Christian König08291c52016-09-12 16:06:18 +0200212 TTM_PL_FLAG_TT))
213 continue;
214
Christian König765e7fb2016-09-15 15:06:50 +0200215 if (abo->placements[i].lpfn)
Christian König08291c52016-09-12 16:06:18 +0200216 continue;
217
218 /* set an upper limit to force directly
219 * allocating address space for the BO.
220 */
Christian König765e7fb2016-09-15 15:06:50 +0200221 abo->placements[i].lpfn =
Christian Königa7d64de2016-09-15 14:58:48 +0200222 adev->mc.gtt_size >> PAGE_SHIFT;
Christian König08291c52016-09-12 16:06:18 +0200223 }
224 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400225 break;
226 case TTM_PL_TT:
227 default:
Christian König765e7fb2016-09-15 15:06:50 +0200228 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400229 }
Christian König765e7fb2016-09-15 15:06:50 +0200230 *placement = abo->placement;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231}
232
233static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
234{
Christian König765e7fb2016-09-15 15:06:50 +0200235 struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236
Jérôme Glisse054892e2016-04-19 09:07:51 -0400237 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
238 return -EPERM;
Dave Airlie28a39652016-09-30 13:18:26 +1000239 return drm_vma_node_verify_access(&abo->gem_base.vma_node,
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200240 filp->private_data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241}
242
243static void amdgpu_move_null(struct ttm_buffer_object *bo,
244 struct ttm_mem_reg *new_mem)
245{
246 struct ttm_mem_reg *old_mem = &bo->mem;
247
248 BUG_ON(old_mem->mm_node != NULL);
249 *old_mem = *new_mem;
250 new_mem->mm_node = NULL;
251}
252
Christian König8892f152016-08-17 10:46:52 +0200253static int amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
254 struct drm_mm_node *mm_node,
255 struct ttm_mem_reg *mem,
256 uint64_t *addr)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400258 int r;
259
Christian König8892f152016-08-17 10:46:52 +0200260 switch (mem->mem_type) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400261 case TTM_PL_TT:
Christian König8892f152016-08-17 10:46:52 +0200262 r = amdgpu_ttm_bind(bo, mem);
Christian Königc855e252016-09-05 17:00:57 +0200263 if (r)
264 return r;
265
266 case TTM_PL_VRAM:
Christian König8892f152016-08-17 10:46:52 +0200267 *addr = mm_node->start << PAGE_SHIFT;
268 *addr += bo->bdev->man[mem->mem_type].gpu_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269 break;
270 default:
Christian König8892f152016-08-17 10:46:52 +0200271 DRM_ERROR("Unknown placement %d\n", mem->mem_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272 return -EINVAL;
273 }
Christian Königc855e252016-09-05 17:00:57 +0200274
Christian König8892f152016-08-17 10:46:52 +0200275 return 0;
276}
277
278static int amdgpu_move_blit(struct ttm_buffer_object *bo,
279 bool evict, bool no_wait_gpu,
280 struct ttm_mem_reg *new_mem,
281 struct ttm_mem_reg *old_mem)
282{
Christian Königa7d64de2016-09-15 14:58:48 +0200283 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian König8892f152016-08-17 10:46:52 +0200284 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
285
286 struct drm_mm_node *old_mm, *new_mm;
287 uint64_t old_start, old_size, new_start, new_size;
288 unsigned long num_pages;
Dave Airlie220196b2016-10-28 11:33:52 +1000289 struct dma_fence *fence = NULL;
Christian König8892f152016-08-17 10:46:52 +0200290 int r;
291
292 BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0);
293
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400294 if (!ring->ready) {
295 DRM_ERROR("Trying to move memory with ring turned off.\n");
296 return -EINVAL;
297 }
298
Christian König8892f152016-08-17 10:46:52 +0200299 old_mm = old_mem->mm_node;
300 r = amdgpu_mm_node_addr(bo, old_mm, old_mem, &old_start);
Christian Königce64bc22016-06-15 13:44:05 +0200301 if (r)
302 return r;
Christian König8892f152016-08-17 10:46:52 +0200303 old_size = old_mm->size;
304
305
306 new_mm = new_mem->mm_node;
307 r = amdgpu_mm_node_addr(bo, new_mm, new_mem, &new_start);
308 if (r)
309 return r;
310 new_size = new_mm->size;
311
312 num_pages = new_mem->num_pages;
313 while (num_pages) {
314 unsigned long cur_pages = min(old_size, new_size);
Dave Airlie220196b2016-10-28 11:33:52 +1000315 struct dma_fence *next;
Christian König8892f152016-08-17 10:46:52 +0200316
317 r = amdgpu_copy_buffer(ring, old_start, new_start,
318 cur_pages * PAGE_SIZE,
319 bo->resv, &next, false);
320 if (r)
321 goto error;
322
Dave Airlie220196b2016-10-28 11:33:52 +1000323 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200324 fence = next;
325
326 num_pages -= cur_pages;
327 if (!num_pages)
328 break;
329
330 old_size -= cur_pages;
331 if (!old_size) {
332 r = amdgpu_mm_node_addr(bo, ++old_mm, old_mem,
333 &old_start);
334 if (r)
335 goto error;
336 old_size = old_mm->size;
337 } else {
338 old_start += cur_pages * PAGE_SIZE;
339 }
340
341 new_size -= cur_pages;
342 if (!new_size) {
343 r = amdgpu_mm_node_addr(bo, ++new_mm, new_mem,
344 &new_start);
345 if (r)
346 goto error;
347
348 new_size = new_mm->size;
349 } else {
350 new_start += cur_pages * PAGE_SIZE;
351 }
352 }
Christian Königce64bc22016-06-15 13:44:05 +0200353
354 r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100355 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356 return r;
Christian König8892f152016-08-17 10:46:52 +0200357
358error:
359 if (fence)
Dave Airlie220196b2016-10-28 11:33:52 +1000360 dma_fence_wait(fence, false);
361 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200362 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400363}
364
365static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo,
366 bool evict, bool interruptible,
367 bool no_wait_gpu,
368 struct ttm_mem_reg *new_mem)
369{
370 struct amdgpu_device *adev;
371 struct ttm_mem_reg *old_mem = &bo->mem;
372 struct ttm_mem_reg tmp_mem;
373 struct ttm_place placements;
374 struct ttm_placement placement;
375 int r;
376
Christian Königa7d64de2016-09-15 14:58:48 +0200377 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378 tmp_mem = *new_mem;
379 tmp_mem.mm_node = NULL;
380 placement.num_placement = 1;
381 placement.placement = &placements;
382 placement.num_busy_placement = 1;
383 placement.busy_placement = &placements;
384 placements.fpfn = 0;
Christian König056472f2016-09-12 16:08:52 +0200385 placements.lpfn = adev->mc.gtt_size >> PAGE_SHIFT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400386 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
387 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
388 interruptible, no_wait_gpu);
389 if (unlikely(r)) {
390 return r;
391 }
392
393 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
394 if (unlikely(r)) {
395 goto out_cleanup;
396 }
397
398 r = ttm_tt_bind(bo->ttm, &tmp_mem);
399 if (unlikely(r)) {
400 goto out_cleanup;
401 }
402 r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
403 if (unlikely(r)) {
404 goto out_cleanup;
405 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900406 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400407out_cleanup:
408 ttm_bo_mem_put(bo, &tmp_mem);
409 return r;
410}
411
412static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo,
413 bool evict, bool interruptible,
414 bool no_wait_gpu,
415 struct ttm_mem_reg *new_mem)
416{
417 struct amdgpu_device *adev;
418 struct ttm_mem_reg *old_mem = &bo->mem;
419 struct ttm_mem_reg tmp_mem;
420 struct ttm_placement placement;
421 struct ttm_place placements;
422 int r;
423
Christian Königa7d64de2016-09-15 14:58:48 +0200424 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425 tmp_mem = *new_mem;
426 tmp_mem.mm_node = NULL;
427 placement.num_placement = 1;
428 placement.placement = &placements;
429 placement.num_busy_placement = 1;
430 placement.busy_placement = &placements;
431 placements.fpfn = 0;
Christian König056472f2016-09-12 16:08:52 +0200432 placements.lpfn = adev->mc.gtt_size >> PAGE_SHIFT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400433 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
434 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
435 interruptible, no_wait_gpu);
436 if (unlikely(r)) {
437 return r;
438 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900439 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400440 if (unlikely(r)) {
441 goto out_cleanup;
442 }
443 r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
444 if (unlikely(r)) {
445 goto out_cleanup;
446 }
447out_cleanup:
448 ttm_bo_mem_put(bo, &tmp_mem);
449 return r;
450}
451
452static int amdgpu_bo_move(struct ttm_buffer_object *bo,
453 bool evict, bool interruptible,
454 bool no_wait_gpu,
455 struct ttm_mem_reg *new_mem)
456{
457 struct amdgpu_device *adev;
Michel Dänzer104ece92016-03-28 12:53:02 +0900458 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459 struct ttm_mem_reg *old_mem = &bo->mem;
460 int r;
461
Michel Dänzer104ece92016-03-28 12:53:02 +0900462 /* Can't move a pinned BO */
463 abo = container_of(bo, struct amdgpu_bo, tbo);
464 if (WARN_ON_ONCE(abo->pin_count > 0))
465 return -EINVAL;
466
Christian Königa7d64de2016-09-15 14:58:48 +0200467 adev = amdgpu_ttm_adev(bo->bdev);
Christian Königdbd5ed62016-06-21 16:28:14 +0200468
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400469 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
470 amdgpu_move_null(bo, new_mem);
471 return 0;
472 }
473 if ((old_mem->mem_type == TTM_PL_TT &&
474 new_mem->mem_type == TTM_PL_SYSTEM) ||
475 (old_mem->mem_type == TTM_PL_SYSTEM &&
476 new_mem->mem_type == TTM_PL_TT)) {
477 /* bind is enough */
478 amdgpu_move_null(bo, new_mem);
479 return 0;
480 }
481 if (adev->mman.buffer_funcs == NULL ||
482 adev->mman.buffer_funcs_ring == NULL ||
483 !adev->mman.buffer_funcs_ring->ready) {
484 /* use memcpy */
485 goto memcpy;
486 }
487
488 if (old_mem->mem_type == TTM_PL_VRAM &&
489 new_mem->mem_type == TTM_PL_SYSTEM) {
490 r = amdgpu_move_vram_ram(bo, evict, interruptible,
491 no_wait_gpu, new_mem);
492 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
493 new_mem->mem_type == TTM_PL_VRAM) {
494 r = amdgpu_move_ram_vram(bo, evict, interruptible,
495 no_wait_gpu, new_mem);
496 } else {
497 r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
498 }
499
500 if (r) {
501memcpy:
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900502 r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 if (r) {
504 return r;
505 }
506 }
507
508 /* update statistics */
509 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
510 return 0;
511}
512
513static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
514{
515 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
Christian Königa7d64de2016-09-15 14:58:48 +0200516 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517
518 mem->bus.addr = NULL;
519 mem->bus.offset = 0;
520 mem->bus.size = mem->num_pages << PAGE_SHIFT;
521 mem->bus.base = 0;
522 mem->bus.is_iomem = false;
523 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
524 return -EINVAL;
525 switch (mem->mem_type) {
526 case TTM_PL_SYSTEM:
527 /* system memory */
528 return 0;
529 case TTM_PL_TT:
530 break;
531 case TTM_PL_VRAM:
532 mem->bus.offset = mem->start << PAGE_SHIFT;
533 /* check if it's visible */
534 if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
535 return -EINVAL;
536 mem->bus.base = adev->mc.aper_base;
537 mem->bus.is_iomem = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400538 break;
539 default:
540 return -EINVAL;
541 }
542 return 0;
543}
544
545static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
546{
547}
548
Christian König9bbdcc02017-03-29 11:16:05 +0200549static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
550 unsigned long page_offset)
551{
552 struct drm_mm_node *mm = bo->mem.mm_node;
553 uint64_t size = mm->size;
554 unsigned long offset = page_offset;
555
556 page_offset = do_div(offset, size);
557 return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset;
558}
559
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560/*
561 * TTM backend functions.
562 */
Christian König637dd3b2016-03-03 14:24:57 +0100563struct amdgpu_ttm_gup_task_list {
564 struct list_head list;
565 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400566};
567
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400568struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100569 struct ttm_dma_tt ttm;
570 struct amdgpu_device *adev;
571 u64 offset;
572 uint64_t userptr;
573 struct mm_struct *usermm;
574 uint32_t userflags;
575 spinlock_t guptasklock;
576 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100577 atomic_t mmu_invalidations;
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800578 struct list_head list;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579};
580
Christian König2f568db2016-02-23 12:36:59 +0100581int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100584 unsigned int flags = 0;
Christian König2f568db2016-02-23 12:36:59 +0100585 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 int r;
587
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100588 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
589 flags |= FOLL_WRITE;
590
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100592 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 to prevent problems with writeback */
594 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
595 struct vm_area_struct *vma;
596
597 vma = find_vma(gtt->usermm, gtt->userptr);
598 if (!vma || vma->vm_file || vma->vm_end < end)
599 return -EPERM;
600 }
601
602 do {
603 unsigned num_pages = ttm->num_pages - pinned;
604 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100605 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100606 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400607
Christian König637dd3b2016-03-03 14:24:57 +0100608 guptask.task = current;
609 spin_lock(&gtt->guptasklock);
610 list_add(&guptask.list, &gtt->guptasks);
611 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400612
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100613 r = get_user_pages(userptr, num_pages, flags, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100614
615 spin_lock(&gtt->guptasklock);
616 list_del(&guptask.list);
617 spin_unlock(&gtt->guptasklock);
618
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 if (r < 0)
620 goto release_pages;
621
622 pinned += r;
623
624 } while (pinned < ttm->num_pages);
625
Christian König2f568db2016-02-23 12:36:59 +0100626 return 0;
627
628release_pages:
629 release_pages(pages, pinned, 0);
630 return r;
631}
632
633/* prepare the sg table with the user pages */
634static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
635{
Christian Königa7d64de2016-09-15 14:58:48 +0200636 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Christian König2f568db2016-02-23 12:36:59 +0100637 struct amdgpu_ttm_tt *gtt = (void *)ttm;
638 unsigned nents;
639 int r;
640
641 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
642 enum dma_data_direction direction = write ?
643 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
644
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
646 ttm->num_pages << PAGE_SHIFT,
647 GFP_KERNEL);
648 if (r)
649 goto release_sg;
650
651 r = -ENOMEM;
652 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
653 if (nents != ttm->sg->nents)
654 goto release_sg;
655
656 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
657 gtt->ttm.dma_address, ttm->num_pages);
658
659 return 0;
660
661release_sg:
662 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400663 return r;
664}
665
666static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
667{
Christian Königa7d64de2016-09-15 14:58:48 +0200668 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400669 struct amdgpu_ttm_tt *gtt = (void *)ttm;
monk.liudd08fae2015-05-07 14:19:18 -0400670 struct sg_page_iter sg_iter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671
672 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
673 enum dma_data_direction direction = write ?
674 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
675
676 /* double check that we don't free the table twice */
677 if (!ttm->sg->sgl)
678 return;
679
680 /* free the sg table and pages again */
681 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
682
monk.liudd08fae2015-05-07 14:19:18 -0400683 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
684 struct page *page = sg_page_iter_page(&sg_iter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
686 set_page_dirty(page);
687
688 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300689 put_page(page);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690 }
691
692 sg_free_table(ttm->sg);
693}
694
695static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
696 struct ttm_mem_reg *bo_mem)
697{
698 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 int r;
700
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800701 if (gtt->userptr) {
702 r = amdgpu_ttm_tt_pin_userptr(ttm);
703 if (r) {
704 DRM_ERROR("failed to pin userptr\n");
705 return r;
706 }
707 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708 if (!ttm->num_pages) {
709 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
710 ttm->num_pages, bo_mem, ttm);
711 }
712
713 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
714 bo_mem->mem_type == AMDGPU_PL_GWS ||
715 bo_mem->mem_type == AMDGPU_PL_OA)
716 return -EINVAL;
717
Christian Königc855e252016-09-05 17:00:57 +0200718 return 0;
719}
720
721bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
722{
723 struct amdgpu_ttm_tt *gtt = (void *)ttm;
724
725 return gtt && !list_empty(&gtt->list);
726}
727
Christian Königbb990bb2016-09-09 16:32:33 +0200728int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
Christian Königc855e252016-09-05 17:00:57 +0200729{
Christian Königbb990bb2016-09-09 16:32:33 +0200730 struct ttm_tt *ttm = bo->ttm;
731 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
Chunming Zhou6b777602016-09-21 16:19:19 +0800732 uint64_t flags;
Christian Königc855e252016-09-05 17:00:57 +0200733 int r;
734
735 if (!ttm || amdgpu_ttm_is_bound(ttm))
736 return 0;
737
Christian Königbb990bb2016-09-09 16:32:33 +0200738 r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo,
739 NULL, bo_mem);
740 if (r) {
741 DRM_ERROR("Failed to allocate GTT address space (%d)\n", r);
742 return r;
743 }
744
Christian Königc855e252016-09-05 17:00:57 +0200745 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, bo_mem);
Christian Königbb990bb2016-09-09 16:32:33 +0200746 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
748 ttm->pages, gtt->ttm.dma_address, flags);
749
750 if (r) {
Christian König71c76a02016-09-03 16:18:26 +0200751 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
752 ttm->num_pages, gtt->offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 return r;
754 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800755 spin_lock(&gtt->adev->gtt_list_lock);
756 list_add_tail(&gtt->list, &gtt->adev->gtt_list);
757 spin_unlock(&gtt->adev->gtt_list_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 return 0;
759}
760
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800761int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
762{
763 struct amdgpu_ttm_tt *gtt, *tmp;
764 struct ttm_mem_reg bo_mem;
765 uint32_t flags;
766 int r;
767
768 bo_mem.mem_type = TTM_PL_TT;
769 spin_lock(&adev->gtt_list_lock);
770 list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
771 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, &gtt->ttm.ttm, &bo_mem);
772 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
773 gtt->ttm.ttm.pages, gtt->ttm.dma_address,
774 flags);
775 if (r) {
776 spin_unlock(&adev->gtt_list_lock);
Christian König71c76a02016-09-03 16:18:26 +0200777 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
778 gtt->ttm.ttm.num_pages, gtt->offset);
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800779 return r;
780 }
781 }
782 spin_unlock(&adev->gtt_list_lock);
783 return 0;
784}
785
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
787{
788 struct amdgpu_ttm_tt *gtt = (void *)ttm;
789
Christian König85a4b572016-09-22 14:19:50 +0200790 if (gtt->userptr)
791 amdgpu_ttm_tt_unpin_userptr(ttm);
792
Christian König78ab0a32016-09-09 15:39:08 +0200793 if (!amdgpu_ttm_is_bound(ttm))
794 return 0;
795
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
797 if (gtt->adev->gart.ready)
798 amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
799
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800800 spin_lock(&gtt->adev->gtt_list_lock);
801 list_del_init(&gtt->list);
802 spin_unlock(&gtt->adev->gtt_list_lock);
803
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 return 0;
805}
806
807static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
808{
809 struct amdgpu_ttm_tt *gtt = (void *)ttm;
810
811 ttm_dma_tt_fini(&gtt->ttm);
812 kfree(gtt);
813}
814
815static struct ttm_backend_func amdgpu_backend_func = {
816 .bind = &amdgpu_ttm_backend_bind,
817 .unbind = &amdgpu_ttm_backend_unbind,
818 .destroy = &amdgpu_ttm_backend_destroy,
819};
820
821static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
822 unsigned long size, uint32_t page_flags,
823 struct page *dummy_read_page)
824{
825 struct amdgpu_device *adev;
826 struct amdgpu_ttm_tt *gtt;
827
Christian Königa7d64de2016-09-15 14:58:48 +0200828 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400829
830 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
831 if (gtt == NULL) {
832 return NULL;
833 }
834 gtt->ttm.ttm.func = &amdgpu_backend_func;
835 gtt->adev = adev;
836 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
837 kfree(gtt);
838 return NULL;
839 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800840 INIT_LIST_HEAD(&gtt->list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841 return &gtt->ttm.ttm;
842}
843
844static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
845{
846 struct amdgpu_device *adev;
847 struct amdgpu_ttm_tt *gtt = (void *)ttm;
848 unsigned i;
849 int r;
850 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
851
852 if (ttm->state != tt_unpopulated)
853 return 0;
854
855 if (gtt && gtt->userptr) {
Maninder Singh5f0b34c2015-06-26 13:28:50 +0530856 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857 if (!ttm->sg)
858 return -ENOMEM;
859
860 ttm->page_flags |= TTM_PAGE_FLAG_SG;
861 ttm->state = tt_unbound;
862 return 0;
863 }
864
865 if (slave && ttm->sg) {
866 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
867 gtt->ttm.dma_address, ttm->num_pages);
868 ttm->state = tt_unbound;
869 return 0;
870 }
871
Christian Königa7d64de2016-09-15 14:58:48 +0200872 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873
874#ifdef CONFIG_SWIOTLB
875 if (swiotlb_nr_tbl()) {
876 return ttm_dma_populate(&gtt->ttm, adev->dev);
877 }
878#endif
879
880 r = ttm_pool_populate(ttm);
881 if (r) {
882 return r;
883 }
884
885 for (i = 0; i < ttm->num_pages; i++) {
886 gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
887 0, PAGE_SIZE,
888 PCI_DMA_BIDIRECTIONAL);
889 if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
Rasmus Villemoes09ccbb72016-02-15 19:41:45 +0100890 while (i--) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400891 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
892 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
893 gtt->ttm.dma_address[i] = 0;
894 }
895 ttm_pool_unpopulate(ttm);
896 return -EFAULT;
897 }
898 }
899 return 0;
900}
901
902static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
903{
904 struct amdgpu_device *adev;
905 struct amdgpu_ttm_tt *gtt = (void *)ttm;
906 unsigned i;
907 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
908
909 if (gtt && gtt->userptr) {
910 kfree(ttm->sg);
911 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
912 return;
913 }
914
915 if (slave)
916 return;
917
Christian Königa7d64de2016-09-15 14:58:48 +0200918 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400919
920#ifdef CONFIG_SWIOTLB
921 if (swiotlb_nr_tbl()) {
922 ttm_dma_unpopulate(&gtt->ttm, adev->dev);
923 return;
924 }
925#endif
926
927 for (i = 0; i < ttm->num_pages; i++) {
928 if (gtt->ttm.dma_address[i]) {
929 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
930 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
931 }
932 }
933
934 ttm_pool_unpopulate(ttm);
935}
936
937int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
938 uint32_t flags)
939{
940 struct amdgpu_ttm_tt *gtt = (void *)ttm;
941
942 if (gtt == NULL)
943 return -EINVAL;
944
945 gtt->userptr = addr;
946 gtt->usermm = current->mm;
947 gtt->userflags = flags;
Christian König637dd3b2016-03-03 14:24:57 +0100948 spin_lock_init(&gtt->guptasklock);
949 INIT_LIST_HEAD(&gtt->guptasks);
Christian König2f568db2016-02-23 12:36:59 +0100950 atomic_set(&gtt->mmu_invalidations, 0);
Christian König637dd3b2016-03-03 14:24:57 +0100951
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952 return 0;
953}
954
Christian Königcc325d12016-02-08 11:08:35 +0100955struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956{
957 struct amdgpu_ttm_tt *gtt = (void *)ttm;
958
959 if (gtt == NULL)
Christian Königcc325d12016-02-08 11:08:35 +0100960 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961
Christian Königcc325d12016-02-08 11:08:35 +0100962 return gtt->usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400963}
964
Christian Königcc1de6e2016-02-08 10:57:22 +0100965bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
966 unsigned long end)
967{
968 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König637dd3b2016-03-03 14:24:57 +0100969 struct amdgpu_ttm_gup_task_list *entry;
Christian Königcc1de6e2016-02-08 10:57:22 +0100970 unsigned long size;
971
Christian König637dd3b2016-03-03 14:24:57 +0100972 if (gtt == NULL || !gtt->userptr)
Christian Königcc1de6e2016-02-08 10:57:22 +0100973 return false;
974
975 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
976 if (gtt->userptr > end || gtt->userptr + size <= start)
977 return false;
978
Christian König637dd3b2016-03-03 14:24:57 +0100979 spin_lock(&gtt->guptasklock);
980 list_for_each_entry(entry, &gtt->guptasks, list) {
981 if (entry->task == current) {
982 spin_unlock(&gtt->guptasklock);
983 return false;
984 }
985 }
986 spin_unlock(&gtt->guptasklock);
987
Christian König2f568db2016-02-23 12:36:59 +0100988 atomic_inc(&gtt->mmu_invalidations);
989
Christian Königcc1de6e2016-02-08 10:57:22 +0100990 return true;
991}
992
Christian König2f568db2016-02-23 12:36:59 +0100993bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
994 int *last_invalidated)
995{
996 struct amdgpu_ttm_tt *gtt = (void *)ttm;
997 int prev_invalidated = *last_invalidated;
998
999 *last_invalidated = atomic_read(&gtt->mmu_invalidations);
1000 return prev_invalidated != *last_invalidated;
1001}
1002
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001003bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1004{
1005 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1006
1007 if (gtt == NULL)
1008 return false;
1009
1010 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1011}
1012
Chunming Zhou6b777602016-09-21 16:19:19 +08001013uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001014 struct ttm_mem_reg *mem)
1015{
Chunming Zhou6b777602016-09-21 16:19:19 +08001016 uint64_t flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001017
1018 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1019 flags |= AMDGPU_PTE_VALID;
1020
Christian König6d999052015-12-04 13:32:55 +01001021 if (mem && mem->mem_type == TTM_PL_TT) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001022 flags |= AMDGPU_PTE_SYSTEM;
1023
Christian König6d999052015-12-04 13:32:55 +01001024 if (ttm->caching_state == tt_cached)
1025 flags |= AMDGPU_PTE_SNOOPED;
1026 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001027
Alex Xie4b98e0c2017-02-14 12:31:36 -05001028 flags |= adev->gart.gart_pte_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029 flags |= AMDGPU_PTE_READABLE;
1030
1031 if (!amdgpu_ttm_tt_is_readonly(ttm))
1032 flags |= AMDGPU_PTE_WRITEABLE;
1033
1034 return flags;
1035}
1036
Christian König9982ca62016-10-19 14:44:22 +02001037static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1038 const struct ttm_place *place)
1039{
1040 if (bo->mem.mem_type == TTM_PL_VRAM &&
1041 bo->mem.start == AMDGPU_BO_INVALID_OFFSET) {
1042 unsigned long num_pages = bo->mem.num_pages;
1043 struct drm_mm_node *node = bo->mem.mm_node;
1044
1045 /* Check each drm MM node individually */
1046 while (num_pages) {
1047 if (place->fpfn < (node->start + node->size) &&
1048 !(place->lpfn && place->lpfn <= node->start))
1049 return true;
1050
1051 num_pages -= node->size;
1052 ++node;
1053 }
1054
1055 return false;
1056 }
1057
1058 return ttm_bo_eviction_valuable(bo, place);
1059}
1060
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061static struct ttm_bo_driver amdgpu_bo_driver = {
1062 .ttm_tt_create = &amdgpu_ttm_tt_create,
1063 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1064 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1065 .invalidate_caches = &amdgpu_invalidate_caches,
1066 .init_mem_type = &amdgpu_init_mem_type,
Christian König9982ca62016-10-19 14:44:22 +02001067 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001068 .evict_flags = &amdgpu_evict_flags,
1069 .move = &amdgpu_bo_move,
1070 .verify_access = &amdgpu_verify_access,
1071 .move_notify = &amdgpu_bo_move_notify,
1072 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1073 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1074 .io_mem_free = &amdgpu_ttm_io_mem_free,
Christian König9bbdcc02017-03-29 11:16:05 +02001075 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076};
1077
1078int amdgpu_ttm_init(struct amdgpu_device *adev)
1079{
1080 int r;
1081
Alex Deucher70b5c5a2016-11-15 16:55:53 -05001082 r = amdgpu_ttm_global_init(adev);
1083 if (r) {
1084 return r;
1085 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001086 /* No others user of address space so set it to 0 */
1087 r = ttm_bo_device_init(&adev->mman.bdev,
1088 adev->mman.bo_global_ref.ref.object,
1089 &amdgpu_bo_driver,
1090 adev->ddev->anon_inode->i_mapping,
1091 DRM_FILE_PAGE_OFFSET,
1092 adev->need_dma32);
1093 if (r) {
1094 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1095 return r;
1096 }
1097 adev->mman.initialized = true;
1098 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1099 adev->mc.real_vram_size >> PAGE_SHIFT);
1100 if (r) {
1101 DRM_ERROR("Failed initializing VRAM heap.\n");
1102 return r;
1103 }
1104 /* Change the size here instead of the init above so only lpfn is affected */
1105 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1106
1107 r = amdgpu_bo_create(adev, 256 * 1024, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001108 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +02001109 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1110 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +02001111 NULL, NULL, &adev->stollen_vga_memory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001112 if (r) {
1113 return r;
1114 }
1115 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1116 if (r)
1117 return r;
1118 r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
1119 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1120 if (r) {
1121 amdgpu_bo_unref(&adev->stollen_vga_memory);
1122 return r;
1123 }
1124 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1125 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
1126 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT,
1127 adev->mc.gtt_size >> PAGE_SHIFT);
1128 if (r) {
1129 DRM_ERROR("Failed initializing GTT heap.\n");
1130 return r;
1131 }
1132 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1133 (unsigned)(adev->mc.gtt_size / (1024 * 1024)));
1134
1135 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1136 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1137 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1138 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1139 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1140 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1141 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1142 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1143 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1144 /* GDS Memory */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001145 if (adev->gds.mem.total_size) {
1146 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1147 adev->gds.mem.total_size >> PAGE_SHIFT);
1148 if (r) {
1149 DRM_ERROR("Failed initializing GDS heap.\n");
1150 return r;
1151 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152 }
1153
1154 /* GWS */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001155 if (adev->gds.gws.total_size) {
1156 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1157 adev->gds.gws.total_size >> PAGE_SHIFT);
1158 if (r) {
1159 DRM_ERROR("Failed initializing gws heap.\n");
1160 return r;
1161 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162 }
1163
1164 /* OA */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001165 if (adev->gds.oa.total_size) {
1166 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1167 adev->gds.oa.total_size >> PAGE_SHIFT);
1168 if (r) {
1169 DRM_ERROR("Failed initializing oa heap.\n");
1170 return r;
1171 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 }
1173
1174 r = amdgpu_ttm_debugfs_init(adev);
1175 if (r) {
1176 DRM_ERROR("Failed to init debugfs\n");
1177 return r;
1178 }
1179 return 0;
1180}
1181
1182void amdgpu_ttm_fini(struct amdgpu_device *adev)
1183{
1184 int r;
1185
1186 if (!adev->mman.initialized)
1187 return;
1188 amdgpu_ttm_debugfs_fini(adev);
1189 if (adev->stollen_vga_memory) {
1190 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1191 if (r == 0) {
1192 amdgpu_bo_unpin(adev->stollen_vga_memory);
1193 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1194 }
1195 amdgpu_bo_unref(&adev->stollen_vga_memory);
1196 }
1197 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1198 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
Alex Deucherd2d51d82017-03-15 09:45:48 -04001199 if (adev->gds.mem.total_size)
1200 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1201 if (adev->gds.gws.total_size)
1202 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1203 if (adev->gds.oa.total_size)
1204 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001205 ttm_bo_device_release(&adev->mman.bdev);
1206 amdgpu_gart_fini(adev);
1207 amdgpu_ttm_global_fini(adev);
1208 adev->mman.initialized = false;
1209 DRM_INFO("amdgpu: ttm finalized\n");
1210}
1211
1212/* this should only be called at bootup or when userspace
1213 * isn't running */
1214void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1215{
1216 struct ttm_mem_type_manager *man;
1217
1218 if (!adev->mman.initialized)
1219 return;
1220
1221 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1222 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1223 man->size = size >> PAGE_SHIFT;
1224}
1225
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1227{
1228 struct drm_file *file_priv;
1229 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230
Christian Könige176fe172015-05-27 10:22:47 +02001231 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233
1234 file_priv = filp->private_data;
1235 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001236 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001237 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001238
1239 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001240}
1241
1242int amdgpu_copy_buffer(struct amdgpu_ring *ring,
1243 uint64_t src_offset,
1244 uint64_t dst_offset,
1245 uint32_t byte_count,
1246 struct reservation_object *resv,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001247 struct dma_fence **fence, bool direct_submit)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001248{
1249 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001250 struct amdgpu_job *job;
1251
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 uint32_t max_bytes;
1253 unsigned num_loops, num_dw;
1254 unsigned i;
1255 int r;
1256
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1258 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1259 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1260
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001261 /* for IB padding */
1262 while (num_dw & 0x7)
1263 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001264
Christian Königd71518b2016-02-01 12:20:25 +01001265 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1266 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001267 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001268
1269 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001270 r = amdgpu_sync_resv(adev, &job->sync, resv,
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001271 AMDGPU_FENCE_OWNER_UNDEFINED);
1272 if (r) {
1273 DRM_ERROR("sync failed (%d).\n", r);
1274 goto error_free;
1275 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001277
1278 for (i = 0; i < num_loops; i++) {
1279 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1280
Christian Königd71518b2016-02-01 12:20:25 +01001281 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1282 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283
1284 src_offset += cur_size_in_bytes;
1285 dst_offset += cur_size_in_bytes;
1286 byte_count -= cur_size_in_bytes;
1287 }
1288
Christian Königd71518b2016-02-01 12:20:25 +01001289 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1290 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001291 if (direct_submit) {
1292 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
Junwei Zhang50ddc752017-01-23 16:30:38 +08001293 NULL, fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001294 job->fence = dma_fence_get(*fence);
Chunming Zhoue24db982016-08-15 10:46:04 +08001295 if (r)
1296 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1297 amdgpu_job_free(job);
1298 } else {
1299 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1300 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1301 if (r)
1302 goto error_free;
1303 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001304
Chunming Zhoue24db982016-08-15 10:46:04 +08001305 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001306
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001307error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001308 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001309 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001310}
1311
Flora Cui59b4a972016-07-19 16:48:22 +08001312int amdgpu_fill_buffer(struct amdgpu_bo *bo,
Christian Königf29224a62016-11-17 12:06:38 +01001313 uint32_t src_data,
1314 struct reservation_object *resv,
1315 struct dma_fence **fence)
Flora Cui59b4a972016-07-19 16:48:22 +08001316{
Christian Königa7d64de2016-09-15 14:58:48 +02001317 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian Königf29224a62016-11-17 12:06:38 +01001318 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
Flora Cui59b4a972016-07-19 16:48:22 +08001319 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1320
Christian Königf29224a62016-11-17 12:06:38 +01001321 struct drm_mm_node *mm_node;
1322 unsigned long num_pages;
Flora Cui59b4a972016-07-19 16:48:22 +08001323 unsigned int num_loops, num_dw;
Christian Königf29224a62016-11-17 12:06:38 +01001324
1325 struct amdgpu_job *job;
Flora Cui59b4a972016-07-19 16:48:22 +08001326 int r;
1327
Christian Königf29224a62016-11-17 12:06:38 +01001328 if (!ring->ready) {
1329 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1330 return -EINVAL;
1331 }
1332
1333 num_pages = bo->tbo.num_pages;
1334 mm_node = bo->tbo.mem.mm_node;
1335 num_loops = 0;
1336 while (num_pages) {
1337 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1338
1339 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1340 num_pages -= mm_node->size;
1341 ++mm_node;
1342 }
Flora Cui59b4a972016-07-19 16:48:22 +08001343 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
1344
1345 /* for IB padding */
Christian Königf29224a62016-11-17 12:06:38 +01001346 num_dw += 64;
Flora Cui59b4a972016-07-19 16:48:22 +08001347
1348 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1349 if (r)
1350 return r;
1351
1352 if (resv) {
1353 r = amdgpu_sync_resv(adev, &job->sync, resv,
Christian Königf29224a62016-11-17 12:06:38 +01001354 AMDGPU_FENCE_OWNER_UNDEFINED);
Flora Cui59b4a972016-07-19 16:48:22 +08001355 if (r) {
1356 DRM_ERROR("sync failed (%d).\n", r);
1357 goto error_free;
1358 }
1359 }
1360
Christian Königf29224a62016-11-17 12:06:38 +01001361 num_pages = bo->tbo.num_pages;
1362 mm_node = bo->tbo.mem.mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001363
Christian Königf29224a62016-11-17 12:06:38 +01001364 while (num_pages) {
1365 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1366 uint64_t dst_addr;
Flora Cui59b4a972016-07-19 16:48:22 +08001367
Christian Königf29224a62016-11-17 12:06:38 +01001368 r = amdgpu_mm_node_addr(&bo->tbo, mm_node,
1369 &bo->tbo.mem, &dst_addr);
1370 if (r)
1371 return r;
1372
1373 while (byte_count) {
1374 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1375
1376 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
1377 dst_addr, cur_size_in_bytes);
1378
1379 dst_addr += cur_size_in_bytes;
1380 byte_count -= cur_size_in_bytes;
1381 }
1382
1383 num_pages -= mm_node->size;
1384 ++mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001385 }
1386
1387 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1388 WARN_ON(job->ibs[0].length_dw > num_dw);
1389 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
Christian Königf29224a62016-11-17 12:06:38 +01001390 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
Flora Cui59b4a972016-07-19 16:48:22 +08001391 if (r)
1392 goto error_free;
1393
1394 return 0;
1395
1396error_free:
1397 amdgpu_job_free(job);
1398 return r;
1399}
1400
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001401#if defined(CONFIG_DEBUG_FS)
1402
1403static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1404{
1405 struct drm_info_node *node = (struct drm_info_node *)m->private;
1406 unsigned ttm_pl = *(int *)node->info_ent->data;
1407 struct drm_device *dev = node->minor->dev;
1408 struct amdgpu_device *adev = dev->dev_private;
1409 struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Daniel Vetterb5c37142016-12-29 12:09:24 +01001411 struct drm_printer p = drm_seq_file_printer(m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412
1413 spin_lock(&glob->lru_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001414 drm_mm_print(mm, &p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 spin_unlock(&glob->lru_lock);
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001416 if (ttm_pl == TTM_PL_VRAM)
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001417 seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001418 adev->mman.bdev.man[ttm_pl].size,
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001419 (u64)atomic64_read(&adev->vram_usage) >> 20,
1420 (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001421 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422}
1423
1424static int ttm_pl_vram = TTM_PL_VRAM;
1425static int ttm_pl_tt = TTM_PL_TT;
1426
Nils Wallménius06ab6832016-05-02 12:46:15 -04001427static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1429 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1430 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1431#ifdef CONFIG_SWIOTLB
1432 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1433#endif
1434};
1435
1436static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1437 size_t size, loff_t *pos)
1438{
Al Viro45063092016-12-04 18:24:56 -05001439 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001440 ssize_t result = 0;
1441 int r;
1442
1443 if (size & 0x3 || *pos & 0x3)
1444 return -EINVAL;
1445
1446 while (size) {
1447 unsigned long flags;
1448 uint32_t value;
1449
1450 if (*pos >= adev->mc.mc_vram_size)
1451 return result;
1452
1453 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1454 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1455 WREG32(mmMM_INDEX_HI, *pos >> 31);
1456 value = RREG32(mmMM_DATA);
1457 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1458
1459 r = put_user(value, (uint32_t *)buf);
1460 if (r)
1461 return r;
1462
1463 result += 4;
1464 buf += 4;
1465 *pos += 4;
1466 size -= 4;
1467 }
1468
1469 return result;
1470}
1471
1472static const struct file_operations amdgpu_ttm_vram_fops = {
1473 .owner = THIS_MODULE,
1474 .read = amdgpu_ttm_vram_read,
1475 .llseek = default_llseek
1476};
1477
Christian Königa1d29472016-03-30 14:42:57 +02001478#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1479
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1481 size_t size, loff_t *pos)
1482{
Al Viro45063092016-12-04 18:24:56 -05001483 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484 ssize_t result = 0;
1485 int r;
1486
1487 while (size) {
1488 loff_t p = *pos / PAGE_SIZE;
1489 unsigned off = *pos & ~PAGE_MASK;
1490 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1491 struct page *page;
1492 void *ptr;
1493
1494 if (p >= adev->gart.num_cpu_pages)
1495 return result;
1496
1497 page = adev->gart.pages[p];
1498 if (page) {
1499 ptr = kmap(page);
1500 ptr += off;
1501
1502 r = copy_to_user(buf, ptr, cur_size);
1503 kunmap(adev->gart.pages[p]);
1504 } else
1505 r = clear_user(buf, cur_size);
1506
1507 if (r)
1508 return -EFAULT;
1509
1510 result += cur_size;
1511 buf += cur_size;
1512 *pos += cur_size;
1513 size -= cur_size;
1514 }
1515
1516 return result;
1517}
1518
1519static const struct file_operations amdgpu_ttm_gtt_fops = {
1520 .owner = THIS_MODULE,
1521 .read = amdgpu_ttm_gtt_read,
1522 .llseek = default_llseek
1523};
1524
1525#endif
1526
Christian Königa1d29472016-03-30 14:42:57 +02001527#endif
1528
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001529static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1530{
1531#if defined(CONFIG_DEBUG_FS)
1532 unsigned count;
1533
1534 struct drm_minor *minor = adev->ddev->primary;
1535 struct dentry *ent, *root = minor->debugfs_root;
1536
1537 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1538 adev, &amdgpu_ttm_vram_fops);
1539 if (IS_ERR(ent))
1540 return PTR_ERR(ent);
1541 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1542 adev->mman.vram = ent;
1543
Christian Königa1d29472016-03-30 14:42:57 +02001544#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001545 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1546 adev, &amdgpu_ttm_gtt_fops);
1547 if (IS_ERR(ent))
1548 return PTR_ERR(ent);
1549 i_size_write(ent->d_inode, adev->mc.gtt_size);
1550 adev->mman.gtt = ent;
1551
Christian Königa1d29472016-03-30 14:42:57 +02001552#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1554
1555#ifdef CONFIG_SWIOTLB
1556 if (!swiotlb_nr_tbl())
1557 --count;
1558#endif
1559
1560 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1561#else
1562
1563 return 0;
1564#endif
1565}
1566
1567static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1568{
1569#if defined(CONFIG_DEBUG_FS)
1570
1571 debugfs_remove(adev->mman.vram);
1572 adev->mman.vram = NULL;
1573
Christian Königa1d29472016-03-30 14:42:57 +02001574#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001575 debugfs_remove(adev->mman.gtt);
1576 adev->mman.gtt = NULL;
1577#endif
Christian Königa1d29472016-03-30 14:42:57 +02001578
1579#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580}