blob: eebfc1d19b29fe7b54c07932e817aefd57c9aea2 [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;
538#ifdef __alpha__
539 /*
540 * Alpha: use bus.addr to hold the ioremap() return,
541 * so we can modify bus.base below.
542 */
543 if (mem->placement & TTM_PL_FLAG_WC)
544 mem->bus.addr =
545 ioremap_wc(mem->bus.base + mem->bus.offset,
546 mem->bus.size);
547 else
548 mem->bus.addr =
549 ioremap_nocache(mem->bus.base + mem->bus.offset,
550 mem->bus.size);
551
552 /*
553 * Alpha: Use just the bus offset plus
554 * the hose/domain memory base for bus.base.
555 * It then can be used to build PTEs for VRAM
556 * access, as done in ttm_bo_vm_fault().
557 */
558 mem->bus.base = (mem->bus.base & 0x0ffffffffUL) +
559 adev->ddev->hose->dense_mem_base;
560#endif
561 break;
562 default:
563 return -EINVAL;
564 }
565 return 0;
566}
567
568static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
569{
570}
571
572/*
573 * TTM backend functions.
574 */
Christian König637dd3b2016-03-03 14:24:57 +0100575struct amdgpu_ttm_gup_task_list {
576 struct list_head list;
577 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578};
579
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100581 struct ttm_dma_tt ttm;
582 struct amdgpu_device *adev;
583 u64 offset;
584 uint64_t userptr;
585 struct mm_struct *usermm;
586 uint32_t userflags;
587 spinlock_t guptasklock;
588 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100589 atomic_t mmu_invalidations;
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800590 struct list_head list;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591};
592
Christian König2f568db2016-02-23 12:36:59 +0100593int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100596 unsigned int flags = 0;
Christian König2f568db2016-02-23 12:36:59 +0100597 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598 int r;
599
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100600 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
601 flags |= FOLL_WRITE;
602
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100604 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400605 to prevent problems with writeback */
606 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
607 struct vm_area_struct *vma;
608
609 vma = find_vma(gtt->usermm, gtt->userptr);
610 if (!vma || vma->vm_file || vma->vm_end < end)
611 return -EPERM;
612 }
613
614 do {
615 unsigned num_pages = ttm->num_pages - pinned;
616 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100617 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100618 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619
Christian König637dd3b2016-03-03 14:24:57 +0100620 guptask.task = current;
621 spin_lock(&gtt->guptasklock);
622 list_add(&guptask.list, &gtt->guptasks);
623 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100625 r = get_user_pages(userptr, num_pages, flags, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100626
627 spin_lock(&gtt->guptasklock);
628 list_del(&guptask.list);
629 spin_unlock(&gtt->guptasklock);
630
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400631 if (r < 0)
632 goto release_pages;
633
634 pinned += r;
635
636 } while (pinned < ttm->num_pages);
637
Christian König2f568db2016-02-23 12:36:59 +0100638 return 0;
639
640release_pages:
641 release_pages(pages, pinned, 0);
642 return r;
643}
644
645/* prepare the sg table with the user pages */
646static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
647{
Christian Königa7d64de2016-09-15 14:58:48 +0200648 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Christian König2f568db2016-02-23 12:36:59 +0100649 struct amdgpu_ttm_tt *gtt = (void *)ttm;
650 unsigned nents;
651 int r;
652
653 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
654 enum dma_data_direction direction = write ?
655 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
656
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400657 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
658 ttm->num_pages << PAGE_SHIFT,
659 GFP_KERNEL);
660 if (r)
661 goto release_sg;
662
663 r = -ENOMEM;
664 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
665 if (nents != ttm->sg->nents)
666 goto release_sg;
667
668 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
669 gtt->ttm.dma_address, ttm->num_pages);
670
671 return 0;
672
673release_sg:
674 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675 return r;
676}
677
678static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
679{
Christian Königa7d64de2016-09-15 14:58:48 +0200680 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 struct amdgpu_ttm_tt *gtt = (void *)ttm;
monk.liudd08fae2015-05-07 14:19:18 -0400682 struct sg_page_iter sg_iter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400683
684 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
685 enum dma_data_direction direction = write ?
686 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
687
688 /* double check that we don't free the table twice */
689 if (!ttm->sg->sgl)
690 return;
691
692 /* free the sg table and pages again */
693 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
694
monk.liudd08fae2015-05-07 14:19:18 -0400695 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
696 struct page *page = sg_page_iter_page(&sg_iter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
698 set_page_dirty(page);
699
700 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300701 put_page(page);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 }
703
704 sg_free_table(ttm->sg);
705}
706
707static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
708 struct ttm_mem_reg *bo_mem)
709{
710 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 int r;
712
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800713 if (gtt->userptr) {
714 r = amdgpu_ttm_tt_pin_userptr(ttm);
715 if (r) {
716 DRM_ERROR("failed to pin userptr\n");
717 return r;
718 }
719 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 if (!ttm->num_pages) {
721 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
722 ttm->num_pages, bo_mem, ttm);
723 }
724
725 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
726 bo_mem->mem_type == AMDGPU_PL_GWS ||
727 bo_mem->mem_type == AMDGPU_PL_OA)
728 return -EINVAL;
729
Christian Königc855e252016-09-05 17:00:57 +0200730 return 0;
731}
732
733bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
734{
735 struct amdgpu_ttm_tt *gtt = (void *)ttm;
736
737 return gtt && !list_empty(&gtt->list);
738}
739
Christian Königbb990bb2016-09-09 16:32:33 +0200740int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
Christian Königc855e252016-09-05 17:00:57 +0200741{
Christian Königbb990bb2016-09-09 16:32:33 +0200742 struct ttm_tt *ttm = bo->ttm;
743 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
Christian Königc855e252016-09-05 17:00:57 +0200744 uint32_t flags;
745 int r;
746
747 if (!ttm || amdgpu_ttm_is_bound(ttm))
748 return 0;
749
Christian Königbb990bb2016-09-09 16:32:33 +0200750 r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo,
751 NULL, bo_mem);
752 if (r) {
753 DRM_ERROR("Failed to allocate GTT address space (%d)\n", r);
754 return r;
755 }
756
Christian Königc855e252016-09-05 17:00:57 +0200757 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, bo_mem);
Christian Königbb990bb2016-09-09 16:32:33 +0200758 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
760 ttm->pages, gtt->ttm.dma_address, flags);
761
762 if (r) {
Christian König71c76a02016-09-03 16:18:26 +0200763 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
764 ttm->num_pages, gtt->offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765 return r;
766 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800767 spin_lock(&gtt->adev->gtt_list_lock);
768 list_add_tail(&gtt->list, &gtt->adev->gtt_list);
769 spin_unlock(&gtt->adev->gtt_list_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770 return 0;
771}
772
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800773int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
774{
775 struct amdgpu_ttm_tt *gtt, *tmp;
776 struct ttm_mem_reg bo_mem;
777 uint32_t flags;
778 int r;
779
780 bo_mem.mem_type = TTM_PL_TT;
781 spin_lock(&adev->gtt_list_lock);
782 list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
783 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, &gtt->ttm.ttm, &bo_mem);
784 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
785 gtt->ttm.ttm.pages, gtt->ttm.dma_address,
786 flags);
787 if (r) {
788 spin_unlock(&adev->gtt_list_lock);
Christian König71c76a02016-09-03 16:18:26 +0200789 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
790 gtt->ttm.ttm.num_pages, gtt->offset);
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800791 return r;
792 }
793 }
794 spin_unlock(&adev->gtt_list_lock);
795 return 0;
796}
797
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
799{
800 struct amdgpu_ttm_tt *gtt = (void *)ttm;
801
Christian König85a4b572016-09-22 14:19:50 +0200802 if (gtt->userptr)
803 amdgpu_ttm_tt_unpin_userptr(ttm);
804
Christian König78ab0a32016-09-09 15:39:08 +0200805 if (!amdgpu_ttm_is_bound(ttm))
806 return 0;
807
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
809 if (gtt->adev->gart.ready)
810 amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
811
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800812 spin_lock(&gtt->adev->gtt_list_lock);
813 list_del_init(&gtt->list);
814 spin_unlock(&gtt->adev->gtt_list_lock);
815
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400816 return 0;
817}
818
819static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
820{
821 struct amdgpu_ttm_tt *gtt = (void *)ttm;
822
823 ttm_dma_tt_fini(&gtt->ttm);
824 kfree(gtt);
825}
826
827static struct ttm_backend_func amdgpu_backend_func = {
828 .bind = &amdgpu_ttm_backend_bind,
829 .unbind = &amdgpu_ttm_backend_unbind,
830 .destroy = &amdgpu_ttm_backend_destroy,
831};
832
833static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
834 unsigned long size, uint32_t page_flags,
835 struct page *dummy_read_page)
836{
837 struct amdgpu_device *adev;
838 struct amdgpu_ttm_tt *gtt;
839
Christian Königa7d64de2016-09-15 14:58:48 +0200840 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841
842 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
843 if (gtt == NULL) {
844 return NULL;
845 }
846 gtt->ttm.ttm.func = &amdgpu_backend_func;
847 gtt->adev = adev;
848 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
849 kfree(gtt);
850 return NULL;
851 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800852 INIT_LIST_HEAD(&gtt->list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400853 return &gtt->ttm.ttm;
854}
855
856static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
857{
858 struct amdgpu_device *adev;
859 struct amdgpu_ttm_tt *gtt = (void *)ttm;
860 unsigned i;
861 int r;
862 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
863
864 if (ttm->state != tt_unpopulated)
865 return 0;
866
867 if (gtt && gtt->userptr) {
Maninder Singh5f0b34c2015-06-26 13:28:50 +0530868 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400869 if (!ttm->sg)
870 return -ENOMEM;
871
872 ttm->page_flags |= TTM_PAGE_FLAG_SG;
873 ttm->state = tt_unbound;
874 return 0;
875 }
876
877 if (slave && ttm->sg) {
878 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
879 gtt->ttm.dma_address, ttm->num_pages);
880 ttm->state = tt_unbound;
881 return 0;
882 }
883
Christian Königa7d64de2016-09-15 14:58:48 +0200884 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400885
886#ifdef CONFIG_SWIOTLB
887 if (swiotlb_nr_tbl()) {
888 return ttm_dma_populate(&gtt->ttm, adev->dev);
889 }
890#endif
891
892 r = ttm_pool_populate(ttm);
893 if (r) {
894 return r;
895 }
896
897 for (i = 0; i < ttm->num_pages; i++) {
898 gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
899 0, PAGE_SIZE,
900 PCI_DMA_BIDIRECTIONAL);
901 if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
Rasmus Villemoes09ccbb72016-02-15 19:41:45 +0100902 while (i--) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400903 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
904 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
905 gtt->ttm.dma_address[i] = 0;
906 }
907 ttm_pool_unpopulate(ttm);
908 return -EFAULT;
909 }
910 }
911 return 0;
912}
913
914static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
915{
916 struct amdgpu_device *adev;
917 struct amdgpu_ttm_tt *gtt = (void *)ttm;
918 unsigned i;
919 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
920
921 if (gtt && gtt->userptr) {
922 kfree(ttm->sg);
923 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
924 return;
925 }
926
927 if (slave)
928 return;
929
Christian Königa7d64de2016-09-15 14:58:48 +0200930 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400931
932#ifdef CONFIG_SWIOTLB
933 if (swiotlb_nr_tbl()) {
934 ttm_dma_unpopulate(&gtt->ttm, adev->dev);
935 return;
936 }
937#endif
938
939 for (i = 0; i < ttm->num_pages; i++) {
940 if (gtt->ttm.dma_address[i]) {
941 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
942 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
943 }
944 }
945
946 ttm_pool_unpopulate(ttm);
947}
948
949int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
950 uint32_t flags)
951{
952 struct amdgpu_ttm_tt *gtt = (void *)ttm;
953
954 if (gtt == NULL)
955 return -EINVAL;
956
957 gtt->userptr = addr;
958 gtt->usermm = current->mm;
959 gtt->userflags = flags;
Christian König637dd3b2016-03-03 14:24:57 +0100960 spin_lock_init(&gtt->guptasklock);
961 INIT_LIST_HEAD(&gtt->guptasks);
Christian König2f568db2016-02-23 12:36:59 +0100962 atomic_set(&gtt->mmu_invalidations, 0);
Christian König637dd3b2016-03-03 14:24:57 +0100963
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400964 return 0;
965}
966
Christian Königcc325d12016-02-08 11:08:35 +0100967struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968{
969 struct amdgpu_ttm_tt *gtt = (void *)ttm;
970
971 if (gtt == NULL)
Christian Königcc325d12016-02-08 11:08:35 +0100972 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973
Christian Königcc325d12016-02-08 11:08:35 +0100974 return gtt->usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975}
976
Christian Königcc1de6e2016-02-08 10:57:22 +0100977bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
978 unsigned long end)
979{
980 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König637dd3b2016-03-03 14:24:57 +0100981 struct amdgpu_ttm_gup_task_list *entry;
Christian Königcc1de6e2016-02-08 10:57:22 +0100982 unsigned long size;
983
Christian König637dd3b2016-03-03 14:24:57 +0100984 if (gtt == NULL || !gtt->userptr)
Christian Königcc1de6e2016-02-08 10:57:22 +0100985 return false;
986
987 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
988 if (gtt->userptr > end || gtt->userptr + size <= start)
989 return false;
990
Christian König637dd3b2016-03-03 14:24:57 +0100991 spin_lock(&gtt->guptasklock);
992 list_for_each_entry(entry, &gtt->guptasks, list) {
993 if (entry->task == current) {
994 spin_unlock(&gtt->guptasklock);
995 return false;
996 }
997 }
998 spin_unlock(&gtt->guptasklock);
999
Christian König2f568db2016-02-23 12:36:59 +01001000 atomic_inc(&gtt->mmu_invalidations);
1001
Christian Königcc1de6e2016-02-08 10:57:22 +01001002 return true;
1003}
1004
Christian König2f568db2016-02-23 12:36:59 +01001005bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1006 int *last_invalidated)
1007{
1008 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1009 int prev_invalidated = *last_invalidated;
1010
1011 *last_invalidated = atomic_read(&gtt->mmu_invalidations);
1012 return prev_invalidated != *last_invalidated;
1013}
1014
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001015bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1016{
1017 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1018
1019 if (gtt == NULL)
1020 return false;
1021
1022 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1023}
1024
1025uint32_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1026 struct ttm_mem_reg *mem)
1027{
1028 uint32_t flags = 0;
1029
1030 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1031 flags |= AMDGPU_PTE_VALID;
1032
Christian König6d999052015-12-04 13:32:55 +01001033 if (mem && mem->mem_type == TTM_PL_TT) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001034 flags |= AMDGPU_PTE_SYSTEM;
1035
Christian König6d999052015-12-04 13:32:55 +01001036 if (ttm->caching_state == tt_cached)
1037 flags |= AMDGPU_PTE_SNOOPED;
1038 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001039
Ken Wang8f3c1622016-02-03 19:17:53 +08001040 if (adev->asic_type >= CHIP_TONGA)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041 flags |= AMDGPU_PTE_EXECUTABLE;
1042
1043 flags |= AMDGPU_PTE_READABLE;
1044
1045 if (!amdgpu_ttm_tt_is_readonly(ttm))
1046 flags |= AMDGPU_PTE_WRITEABLE;
1047
1048 return flags;
1049}
1050
Christian König9982ca62016-10-19 14:44:22 +02001051static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1052 const struct ttm_place *place)
1053{
1054 if (bo->mem.mem_type == TTM_PL_VRAM &&
1055 bo->mem.start == AMDGPU_BO_INVALID_OFFSET) {
1056 unsigned long num_pages = bo->mem.num_pages;
1057 struct drm_mm_node *node = bo->mem.mm_node;
1058
1059 /* Check each drm MM node individually */
1060 while (num_pages) {
1061 if (place->fpfn < (node->start + node->size) &&
1062 !(place->lpfn && place->lpfn <= node->start))
1063 return true;
1064
1065 num_pages -= node->size;
1066 ++node;
1067 }
1068
1069 return false;
1070 }
1071
1072 return ttm_bo_eviction_valuable(bo, place);
1073}
1074
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001075static struct ttm_bo_driver amdgpu_bo_driver = {
1076 .ttm_tt_create = &amdgpu_ttm_tt_create,
1077 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1078 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1079 .invalidate_caches = &amdgpu_invalidate_caches,
1080 .init_mem_type = &amdgpu_init_mem_type,
Christian König9982ca62016-10-19 14:44:22 +02001081 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001082 .evict_flags = &amdgpu_evict_flags,
1083 .move = &amdgpu_bo_move,
1084 .verify_access = &amdgpu_verify_access,
1085 .move_notify = &amdgpu_bo_move_notify,
1086 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1087 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1088 .io_mem_free = &amdgpu_ttm_io_mem_free,
1089};
1090
1091int amdgpu_ttm_init(struct amdgpu_device *adev)
1092{
1093 int r;
1094
Alex Deucher70b5c5a2016-11-15 16:55:53 -05001095 r = amdgpu_ttm_global_init(adev);
1096 if (r) {
1097 return r;
1098 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099 /* No others user of address space so set it to 0 */
1100 r = ttm_bo_device_init(&adev->mman.bdev,
1101 adev->mman.bo_global_ref.ref.object,
1102 &amdgpu_bo_driver,
1103 adev->ddev->anon_inode->i_mapping,
1104 DRM_FILE_PAGE_OFFSET,
1105 adev->need_dma32);
1106 if (r) {
1107 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1108 return r;
1109 }
1110 adev->mman.initialized = true;
1111 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1112 adev->mc.real_vram_size >> PAGE_SHIFT);
1113 if (r) {
1114 DRM_ERROR("Failed initializing VRAM heap.\n");
1115 return r;
1116 }
1117 /* Change the size here instead of the init above so only lpfn is affected */
1118 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1119
1120 r = amdgpu_bo_create(adev, 256 * 1024, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001121 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +02001122 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1123 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +02001124 NULL, NULL, &adev->stollen_vga_memory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 if (r) {
1126 return r;
1127 }
1128 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1129 if (r)
1130 return r;
1131 r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
1132 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1133 if (r) {
1134 amdgpu_bo_unref(&adev->stollen_vga_memory);
1135 return r;
1136 }
1137 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1138 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
1139 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT,
1140 adev->mc.gtt_size >> PAGE_SHIFT);
1141 if (r) {
1142 DRM_ERROR("Failed initializing GTT heap.\n");
1143 return r;
1144 }
1145 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1146 (unsigned)(adev->mc.gtt_size / (1024 * 1024)));
1147
1148 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1149 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1150 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1151 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1152 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1153 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1154 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1155 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1156 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1157 /* GDS Memory */
1158 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1159 adev->gds.mem.total_size >> PAGE_SHIFT);
1160 if (r) {
1161 DRM_ERROR("Failed initializing GDS heap.\n");
1162 return r;
1163 }
1164
1165 /* GWS */
1166 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1167 adev->gds.gws.total_size >> PAGE_SHIFT);
1168 if (r) {
1169 DRM_ERROR("Failed initializing gws heap.\n");
1170 return r;
1171 }
1172
1173 /* OA */
1174 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1175 adev->gds.oa.total_size >> PAGE_SHIFT);
1176 if (r) {
1177 DRM_ERROR("Failed initializing oa heap.\n");
1178 return r;
1179 }
1180
1181 r = amdgpu_ttm_debugfs_init(adev);
1182 if (r) {
1183 DRM_ERROR("Failed to init debugfs\n");
1184 return r;
1185 }
1186 return 0;
1187}
1188
1189void amdgpu_ttm_fini(struct amdgpu_device *adev)
1190{
1191 int r;
1192
1193 if (!adev->mman.initialized)
1194 return;
1195 amdgpu_ttm_debugfs_fini(adev);
1196 if (adev->stollen_vga_memory) {
1197 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1198 if (r == 0) {
1199 amdgpu_bo_unpin(adev->stollen_vga_memory);
1200 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1201 }
1202 amdgpu_bo_unref(&adev->stollen_vga_memory);
1203 }
1204 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1205 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
1206 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1207 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1208 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
1209 ttm_bo_device_release(&adev->mman.bdev);
1210 amdgpu_gart_fini(adev);
1211 amdgpu_ttm_global_fini(adev);
1212 adev->mman.initialized = false;
1213 DRM_INFO("amdgpu: ttm finalized\n");
1214}
1215
1216/* this should only be called at bootup or when userspace
1217 * isn't running */
1218void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1219{
1220 struct ttm_mem_type_manager *man;
1221
1222 if (!adev->mman.initialized)
1223 return;
1224
1225 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1226 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1227 man->size = size >> PAGE_SHIFT;
1228}
1229
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1231{
1232 struct drm_file *file_priv;
1233 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001234
Christian Könige176fe172015-05-27 10:22:47 +02001235 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001236 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001237
1238 file_priv = filp->private_data;
1239 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001240 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001241 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001242
1243 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244}
1245
1246int amdgpu_copy_buffer(struct amdgpu_ring *ring,
1247 uint64_t src_offset,
1248 uint64_t dst_offset,
1249 uint32_t byte_count,
1250 struct reservation_object *resv,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001251 struct dma_fence **fence, bool direct_submit)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252{
1253 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001254 struct amdgpu_job *job;
1255
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256 uint32_t max_bytes;
1257 unsigned num_loops, num_dw;
1258 unsigned i;
1259 int r;
1260
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1262 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1263 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1264
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001265 /* for IB padding */
1266 while (num_dw & 0x7)
1267 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001268
Christian Königd71518b2016-02-01 12:20:25 +01001269 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1270 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001271 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001272
1273 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001274 r = amdgpu_sync_resv(adev, &job->sync, resv,
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001275 AMDGPU_FENCE_OWNER_UNDEFINED);
1276 if (r) {
1277 DRM_ERROR("sync failed (%d).\n", r);
1278 goto error_free;
1279 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281
1282 for (i = 0; i < num_loops; i++) {
1283 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1284
Christian Königd71518b2016-02-01 12:20:25 +01001285 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1286 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001287
1288 src_offset += cur_size_in_bytes;
1289 dst_offset += cur_size_in_bytes;
1290 byte_count -= cur_size_in_bytes;
1291 }
1292
Christian Königd71518b2016-02-01 12:20:25 +01001293 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1294 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001295 if (direct_submit) {
1296 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
1297 NULL, NULL, fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001298 job->fence = dma_fence_get(*fence);
Chunming Zhoue24db982016-08-15 10:46:04 +08001299 if (r)
1300 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1301 amdgpu_job_free(job);
1302 } else {
1303 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1304 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1305 if (r)
1306 goto error_free;
1307 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001308
Chunming Zhoue24db982016-08-15 10:46:04 +08001309 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001310
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001311error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001312 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001313 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314}
1315
Flora Cui59b4a972016-07-19 16:48:22 +08001316int amdgpu_fill_buffer(struct amdgpu_bo *bo,
Christian Königf29224a62016-11-17 12:06:38 +01001317 uint32_t src_data,
1318 struct reservation_object *resv,
1319 struct dma_fence **fence)
Flora Cui59b4a972016-07-19 16:48:22 +08001320{
Christian Königa7d64de2016-09-15 14:58:48 +02001321 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian Königf29224a62016-11-17 12:06:38 +01001322 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
Flora Cui59b4a972016-07-19 16:48:22 +08001323 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1324
Christian Königf29224a62016-11-17 12:06:38 +01001325 struct drm_mm_node *mm_node;
1326 unsigned long num_pages;
Flora Cui59b4a972016-07-19 16:48:22 +08001327 unsigned int num_loops, num_dw;
Christian Königf29224a62016-11-17 12:06:38 +01001328
1329 struct amdgpu_job *job;
Flora Cui59b4a972016-07-19 16:48:22 +08001330 int r;
1331
Christian Königf29224a62016-11-17 12:06:38 +01001332 if (!ring->ready) {
1333 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1334 return -EINVAL;
1335 }
1336
1337 num_pages = bo->tbo.num_pages;
1338 mm_node = bo->tbo.mem.mm_node;
1339 num_loops = 0;
1340 while (num_pages) {
1341 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1342
1343 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1344 num_pages -= mm_node->size;
1345 ++mm_node;
1346 }
Flora Cui59b4a972016-07-19 16:48:22 +08001347 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
1348
1349 /* for IB padding */
Christian Königf29224a62016-11-17 12:06:38 +01001350 num_dw += 64;
Flora Cui59b4a972016-07-19 16:48:22 +08001351
1352 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1353 if (r)
1354 return r;
1355
1356 if (resv) {
1357 r = amdgpu_sync_resv(adev, &job->sync, resv,
Christian Königf29224a62016-11-17 12:06:38 +01001358 AMDGPU_FENCE_OWNER_UNDEFINED);
Flora Cui59b4a972016-07-19 16:48:22 +08001359 if (r) {
1360 DRM_ERROR("sync failed (%d).\n", r);
1361 goto error_free;
1362 }
1363 }
1364
Christian Königf29224a62016-11-17 12:06:38 +01001365 num_pages = bo->tbo.num_pages;
1366 mm_node = bo->tbo.mem.mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001367
Christian Königf29224a62016-11-17 12:06:38 +01001368 while (num_pages) {
1369 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1370 uint64_t dst_addr;
Flora Cui59b4a972016-07-19 16:48:22 +08001371
Christian Königf29224a62016-11-17 12:06:38 +01001372 r = amdgpu_mm_node_addr(&bo->tbo, mm_node,
1373 &bo->tbo.mem, &dst_addr);
1374 if (r)
1375 return r;
1376
1377 while (byte_count) {
1378 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1379
1380 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
1381 dst_addr, cur_size_in_bytes);
1382
1383 dst_addr += cur_size_in_bytes;
1384 byte_count -= cur_size_in_bytes;
1385 }
1386
1387 num_pages -= mm_node->size;
1388 ++mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001389 }
1390
1391 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1392 WARN_ON(job->ibs[0].length_dw > num_dw);
1393 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
Christian Königf29224a62016-11-17 12:06:38 +01001394 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
Flora Cui59b4a972016-07-19 16:48:22 +08001395 if (r)
1396 goto error_free;
1397
1398 return 0;
1399
1400error_free:
1401 amdgpu_job_free(job);
1402 return r;
1403}
1404
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405#if defined(CONFIG_DEBUG_FS)
1406
1407static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1408{
1409 struct drm_info_node *node = (struct drm_info_node *)m->private;
1410 unsigned ttm_pl = *(int *)node->info_ent->data;
1411 struct drm_device *dev = node->minor->dev;
1412 struct amdgpu_device *adev = dev->dev_private;
1413 struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Daniel Vetterb5c37142016-12-29 12:09:24 +01001415 struct drm_printer p = drm_seq_file_printer(m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001416
1417 spin_lock(&glob->lru_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001418 drm_mm_print(mm, &p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419 spin_unlock(&glob->lru_lock);
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001420 if (ttm_pl == TTM_PL_VRAM)
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001421 seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001422 adev->mman.bdev.man[ttm_pl].size,
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001423 (u64)atomic64_read(&adev->vram_usage) >> 20,
1424 (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001425 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001426}
1427
1428static int ttm_pl_vram = TTM_PL_VRAM;
1429static int ttm_pl_tt = TTM_PL_TT;
1430
Nils Wallménius06ab6832016-05-02 12:46:15 -04001431static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1433 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1434 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1435#ifdef CONFIG_SWIOTLB
1436 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1437#endif
1438};
1439
1440static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1441 size_t size, loff_t *pos)
1442{
Al Viro45063092016-12-04 18:24:56 -05001443 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 ssize_t result = 0;
1445 int r;
1446
1447 if (size & 0x3 || *pos & 0x3)
1448 return -EINVAL;
1449
1450 while (size) {
1451 unsigned long flags;
1452 uint32_t value;
1453
1454 if (*pos >= adev->mc.mc_vram_size)
1455 return result;
1456
1457 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1458 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1459 WREG32(mmMM_INDEX_HI, *pos >> 31);
1460 value = RREG32(mmMM_DATA);
1461 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1462
1463 r = put_user(value, (uint32_t *)buf);
1464 if (r)
1465 return r;
1466
1467 result += 4;
1468 buf += 4;
1469 *pos += 4;
1470 size -= 4;
1471 }
1472
1473 return result;
1474}
1475
1476static const struct file_operations amdgpu_ttm_vram_fops = {
1477 .owner = THIS_MODULE,
1478 .read = amdgpu_ttm_vram_read,
1479 .llseek = default_llseek
1480};
1481
Christian Königa1d29472016-03-30 14:42:57 +02001482#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1483
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1485 size_t size, loff_t *pos)
1486{
Al Viro45063092016-12-04 18:24:56 -05001487 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 ssize_t result = 0;
1489 int r;
1490
1491 while (size) {
1492 loff_t p = *pos / PAGE_SIZE;
1493 unsigned off = *pos & ~PAGE_MASK;
1494 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1495 struct page *page;
1496 void *ptr;
1497
1498 if (p >= adev->gart.num_cpu_pages)
1499 return result;
1500
1501 page = adev->gart.pages[p];
1502 if (page) {
1503 ptr = kmap(page);
1504 ptr += off;
1505
1506 r = copy_to_user(buf, ptr, cur_size);
1507 kunmap(adev->gart.pages[p]);
1508 } else
1509 r = clear_user(buf, cur_size);
1510
1511 if (r)
1512 return -EFAULT;
1513
1514 result += cur_size;
1515 buf += cur_size;
1516 *pos += cur_size;
1517 size -= cur_size;
1518 }
1519
1520 return result;
1521}
1522
1523static const struct file_operations amdgpu_ttm_gtt_fops = {
1524 .owner = THIS_MODULE,
1525 .read = amdgpu_ttm_gtt_read,
1526 .llseek = default_llseek
1527};
1528
1529#endif
1530
Christian Königa1d29472016-03-30 14:42:57 +02001531#endif
1532
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001533static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1534{
1535#if defined(CONFIG_DEBUG_FS)
1536 unsigned count;
1537
1538 struct drm_minor *minor = adev->ddev->primary;
1539 struct dentry *ent, *root = minor->debugfs_root;
1540
1541 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1542 adev, &amdgpu_ttm_vram_fops);
1543 if (IS_ERR(ent))
1544 return PTR_ERR(ent);
1545 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1546 adev->mman.vram = ent;
1547
Christian Königa1d29472016-03-30 14:42:57 +02001548#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001549 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1550 adev, &amdgpu_ttm_gtt_fops);
1551 if (IS_ERR(ent))
1552 return PTR_ERR(ent);
1553 i_size_write(ent->d_inode, adev->mc.gtt_size);
1554 adev->mman.gtt = ent;
1555
Christian Königa1d29472016-03-30 14:42:57 +02001556#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1558
1559#ifdef CONFIG_SWIOTLB
1560 if (!swiotlb_nr_tbl())
1561 --count;
1562#endif
1563
1564 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1565#else
1566
1567 return 0;
1568#endif
1569}
1570
1571static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1572{
1573#if defined(CONFIG_DEBUG_FS)
1574
1575 debugfs_remove(adev->mman.vram);
1576 adev->mman.vram = NULL;
1577
Christian Königa1d29472016-03-30 14:42:57 +02001578#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001579 debugfs_remove(adev->mman.gtt);
1580 adev->mman.gtt = NULL;
1581#endif
Christian Königa1d29472016-03-30 14:42:57 +02001582
1583#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001584}