blob: 987f8f02348dbf53b84f124bfc1e98bbefdbcab4 [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:
Nicolai Hähnle46943352017-02-16 23:49:12 +0100532 if (mem->start == AMDGPU_BO_INVALID_OFFSET)
533 return -EINVAL;
534
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535 mem->bus.offset = mem->start << PAGE_SHIFT;
536 /* check if it's visible */
537 if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
538 return -EINVAL;
539 mem->bus.base = adev->mc.aper_base;
540 mem->bus.is_iomem = true;
541#ifdef __alpha__
542 /*
543 * Alpha: use bus.addr to hold the ioremap() return,
544 * so we can modify bus.base below.
545 */
546 if (mem->placement & TTM_PL_FLAG_WC)
547 mem->bus.addr =
548 ioremap_wc(mem->bus.base + mem->bus.offset,
549 mem->bus.size);
550 else
551 mem->bus.addr =
552 ioremap_nocache(mem->bus.base + mem->bus.offset,
553 mem->bus.size);
Arvind Yadav8ba28ac2017-01-24 14:55:33 +0530554 if (!mem->bus.addr)
555 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556
557 /*
558 * Alpha: Use just the bus offset plus
559 * the hose/domain memory base for bus.base.
560 * It then can be used to build PTEs for VRAM
561 * access, as done in ttm_bo_vm_fault().
562 */
563 mem->bus.base = (mem->bus.base & 0x0ffffffffUL) +
564 adev->ddev->hose->dense_mem_base;
565#endif
566 break;
567 default:
568 return -EINVAL;
569 }
570 return 0;
571}
572
573static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
574{
575}
576
577/*
578 * TTM backend functions.
579 */
Christian König637dd3b2016-03-03 14:24:57 +0100580struct amdgpu_ttm_gup_task_list {
581 struct list_head list;
582 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583};
584
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100586 struct ttm_dma_tt ttm;
587 struct amdgpu_device *adev;
588 u64 offset;
589 uint64_t userptr;
590 struct mm_struct *usermm;
591 uint32_t userflags;
592 spinlock_t guptasklock;
593 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100594 atomic_t mmu_invalidations;
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800595 struct list_head list;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400596};
597
Christian König2f568db2016-02-23 12:36:59 +0100598int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100601 unsigned int flags = 0;
Christian König2f568db2016-02-23 12:36:59 +0100602 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603 int r;
604
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100605 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
606 flags |= FOLL_WRITE;
607
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100609 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610 to prevent problems with writeback */
611 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
612 struct vm_area_struct *vma;
613
614 vma = find_vma(gtt->usermm, gtt->userptr);
615 if (!vma || vma->vm_file || vma->vm_end < end)
616 return -EPERM;
617 }
618
619 do {
620 unsigned num_pages = ttm->num_pages - pinned;
621 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100622 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100623 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624
Christian König637dd3b2016-03-03 14:24:57 +0100625 guptask.task = current;
626 spin_lock(&gtt->guptasklock);
627 list_add(&guptask.list, &gtt->guptasks);
628 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100630 r = get_user_pages(userptr, num_pages, flags, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100631
632 spin_lock(&gtt->guptasklock);
633 list_del(&guptask.list);
634 spin_unlock(&gtt->guptasklock);
635
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 if (r < 0)
637 goto release_pages;
638
639 pinned += r;
640
641 } while (pinned < ttm->num_pages);
642
Christian König2f568db2016-02-23 12:36:59 +0100643 return 0;
644
645release_pages:
646 release_pages(pages, pinned, 0);
647 return r;
648}
649
650/* prepare the sg table with the user pages */
651static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
652{
Christian Königa7d64de2016-09-15 14:58:48 +0200653 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Christian König2f568db2016-02-23 12:36:59 +0100654 struct amdgpu_ttm_tt *gtt = (void *)ttm;
655 unsigned nents;
656 int r;
657
658 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
659 enum dma_data_direction direction = write ?
660 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
661
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
663 ttm->num_pages << PAGE_SHIFT,
664 GFP_KERNEL);
665 if (r)
666 goto release_sg;
667
668 r = -ENOMEM;
669 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
670 if (nents != ttm->sg->nents)
671 goto release_sg;
672
673 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
674 gtt->ttm.dma_address, ttm->num_pages);
675
676 return 0;
677
678release_sg:
679 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 return r;
681}
682
683static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
684{
Christian Königa7d64de2016-09-15 14:58:48 +0200685 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 struct amdgpu_ttm_tt *gtt = (void *)ttm;
monk.liudd08fae2015-05-07 14:19:18 -0400687 struct sg_page_iter sg_iter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688
689 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
690 enum dma_data_direction direction = write ?
691 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
692
693 /* double check that we don't free the table twice */
694 if (!ttm->sg->sgl)
695 return;
696
697 /* free the sg table and pages again */
698 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
699
monk.liudd08fae2015-05-07 14:19:18 -0400700 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
701 struct page *page = sg_page_iter_page(&sg_iter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
703 set_page_dirty(page);
704
705 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 put_page(page);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 }
708
709 sg_free_table(ttm->sg);
710}
711
712static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
713 struct ttm_mem_reg *bo_mem)
714{
715 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716 int r;
717
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800718 if (gtt->userptr) {
719 r = amdgpu_ttm_tt_pin_userptr(ttm);
720 if (r) {
721 DRM_ERROR("failed to pin userptr\n");
722 return r;
723 }
724 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400725 if (!ttm->num_pages) {
726 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
727 ttm->num_pages, bo_mem, ttm);
728 }
729
730 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
731 bo_mem->mem_type == AMDGPU_PL_GWS ||
732 bo_mem->mem_type == AMDGPU_PL_OA)
733 return -EINVAL;
734
Christian Königc855e252016-09-05 17:00:57 +0200735 return 0;
736}
737
738bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
739{
740 struct amdgpu_ttm_tt *gtt = (void *)ttm;
741
742 return gtt && !list_empty(&gtt->list);
743}
744
Christian Königbb990bb2016-09-09 16:32:33 +0200745int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
Christian Königc855e252016-09-05 17:00:57 +0200746{
Christian Königbb990bb2016-09-09 16:32:33 +0200747 struct ttm_tt *ttm = bo->ttm;
748 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
Chunming Zhou6b777602016-09-21 16:19:19 +0800749 uint64_t flags;
Christian Königc855e252016-09-05 17:00:57 +0200750 int r;
751
752 if (!ttm || amdgpu_ttm_is_bound(ttm))
753 return 0;
754
Christian Königbb990bb2016-09-09 16:32:33 +0200755 r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo,
756 NULL, bo_mem);
757 if (r) {
758 DRM_ERROR("Failed to allocate GTT address space (%d)\n", r);
759 return r;
760 }
761
Christian Königc855e252016-09-05 17:00:57 +0200762 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, bo_mem);
Christian Königbb990bb2016-09-09 16:32:33 +0200763 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400764 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
765 ttm->pages, gtt->ttm.dma_address, flags);
766
767 if (r) {
Christian König71c76a02016-09-03 16:18:26 +0200768 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
769 ttm->num_pages, gtt->offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400770 return r;
771 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800772 spin_lock(&gtt->adev->gtt_list_lock);
773 list_add_tail(&gtt->list, &gtt->adev->gtt_list);
774 spin_unlock(&gtt->adev->gtt_list_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400775 return 0;
776}
777
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800778int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
779{
780 struct amdgpu_ttm_tt *gtt, *tmp;
781 struct ttm_mem_reg bo_mem;
782 uint32_t flags;
783 int r;
784
785 bo_mem.mem_type = TTM_PL_TT;
786 spin_lock(&adev->gtt_list_lock);
787 list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
788 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, &gtt->ttm.ttm, &bo_mem);
789 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
790 gtt->ttm.ttm.pages, gtt->ttm.dma_address,
791 flags);
792 if (r) {
793 spin_unlock(&adev->gtt_list_lock);
Christian König71c76a02016-09-03 16:18:26 +0200794 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
795 gtt->ttm.ttm.num_pages, gtt->offset);
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800796 return r;
797 }
798 }
799 spin_unlock(&adev->gtt_list_lock);
800 return 0;
801}
802
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400803static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
804{
805 struct amdgpu_ttm_tt *gtt = (void *)ttm;
806
Christian König85a4b572016-09-22 14:19:50 +0200807 if (gtt->userptr)
808 amdgpu_ttm_tt_unpin_userptr(ttm);
809
Christian König78ab0a32016-09-09 15:39:08 +0200810 if (!amdgpu_ttm_is_bound(ttm))
811 return 0;
812
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400813 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
814 if (gtt->adev->gart.ready)
815 amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
816
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800817 spin_lock(&gtt->adev->gtt_list_lock);
818 list_del_init(&gtt->list);
819 spin_unlock(&gtt->adev->gtt_list_lock);
820
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400821 return 0;
822}
823
824static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
825{
826 struct amdgpu_ttm_tt *gtt = (void *)ttm;
827
828 ttm_dma_tt_fini(&gtt->ttm);
829 kfree(gtt);
830}
831
832static struct ttm_backend_func amdgpu_backend_func = {
833 .bind = &amdgpu_ttm_backend_bind,
834 .unbind = &amdgpu_ttm_backend_unbind,
835 .destroy = &amdgpu_ttm_backend_destroy,
836};
837
838static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
839 unsigned long size, uint32_t page_flags,
840 struct page *dummy_read_page)
841{
842 struct amdgpu_device *adev;
843 struct amdgpu_ttm_tt *gtt;
844
Christian Königa7d64de2016-09-15 14:58:48 +0200845 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846
847 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
848 if (gtt == NULL) {
849 return NULL;
850 }
851 gtt->ttm.ttm.func = &amdgpu_backend_func;
852 gtt->adev = adev;
853 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
854 kfree(gtt);
855 return NULL;
856 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800857 INIT_LIST_HEAD(&gtt->list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400858 return &gtt->ttm.ttm;
859}
860
861static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
862{
863 struct amdgpu_device *adev;
864 struct amdgpu_ttm_tt *gtt = (void *)ttm;
865 unsigned i;
866 int r;
867 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
868
869 if (ttm->state != tt_unpopulated)
870 return 0;
871
872 if (gtt && gtt->userptr) {
Maninder Singh5f0b34c2015-06-26 13:28:50 +0530873 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400874 if (!ttm->sg)
875 return -ENOMEM;
876
877 ttm->page_flags |= TTM_PAGE_FLAG_SG;
878 ttm->state = tt_unbound;
879 return 0;
880 }
881
882 if (slave && ttm->sg) {
883 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
884 gtt->ttm.dma_address, ttm->num_pages);
885 ttm->state = tt_unbound;
886 return 0;
887 }
888
Christian Königa7d64de2016-09-15 14:58:48 +0200889 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400890
891#ifdef CONFIG_SWIOTLB
892 if (swiotlb_nr_tbl()) {
893 return ttm_dma_populate(&gtt->ttm, adev->dev);
894 }
895#endif
896
897 r = ttm_pool_populate(ttm);
898 if (r) {
899 return r;
900 }
901
902 for (i = 0; i < ttm->num_pages; i++) {
903 gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
904 0, PAGE_SIZE,
905 PCI_DMA_BIDIRECTIONAL);
906 if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
Rasmus Villemoes09ccbb72016-02-15 19:41:45 +0100907 while (i--) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400908 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
909 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
910 gtt->ttm.dma_address[i] = 0;
911 }
912 ttm_pool_unpopulate(ttm);
913 return -EFAULT;
914 }
915 }
916 return 0;
917}
918
919static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
920{
921 struct amdgpu_device *adev;
922 struct amdgpu_ttm_tt *gtt = (void *)ttm;
923 unsigned i;
924 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
925
926 if (gtt && gtt->userptr) {
927 kfree(ttm->sg);
928 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
929 return;
930 }
931
932 if (slave)
933 return;
934
Christian Königa7d64de2016-09-15 14:58:48 +0200935 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936
937#ifdef CONFIG_SWIOTLB
938 if (swiotlb_nr_tbl()) {
939 ttm_dma_unpopulate(&gtt->ttm, adev->dev);
940 return;
941 }
942#endif
943
944 for (i = 0; i < ttm->num_pages; i++) {
945 if (gtt->ttm.dma_address[i]) {
946 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
947 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
948 }
949 }
950
951 ttm_pool_unpopulate(ttm);
952}
953
954int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
955 uint32_t flags)
956{
957 struct amdgpu_ttm_tt *gtt = (void *)ttm;
958
959 if (gtt == NULL)
960 return -EINVAL;
961
962 gtt->userptr = addr;
963 gtt->usermm = current->mm;
964 gtt->userflags = flags;
Christian König637dd3b2016-03-03 14:24:57 +0100965 spin_lock_init(&gtt->guptasklock);
966 INIT_LIST_HEAD(&gtt->guptasks);
Christian König2f568db2016-02-23 12:36:59 +0100967 atomic_set(&gtt->mmu_invalidations, 0);
Christian König637dd3b2016-03-03 14:24:57 +0100968
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969 return 0;
970}
971
Christian Königcc325d12016-02-08 11:08:35 +0100972struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973{
974 struct amdgpu_ttm_tt *gtt = (void *)ttm;
975
976 if (gtt == NULL)
Christian Königcc325d12016-02-08 11:08:35 +0100977 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400978
Christian Königcc325d12016-02-08 11:08:35 +0100979 return gtt->usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980}
981
Christian Königcc1de6e2016-02-08 10:57:22 +0100982bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
983 unsigned long end)
984{
985 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König637dd3b2016-03-03 14:24:57 +0100986 struct amdgpu_ttm_gup_task_list *entry;
Christian Königcc1de6e2016-02-08 10:57:22 +0100987 unsigned long size;
988
Christian König637dd3b2016-03-03 14:24:57 +0100989 if (gtt == NULL || !gtt->userptr)
Christian Königcc1de6e2016-02-08 10:57:22 +0100990 return false;
991
992 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
993 if (gtt->userptr > end || gtt->userptr + size <= start)
994 return false;
995
Christian König637dd3b2016-03-03 14:24:57 +0100996 spin_lock(&gtt->guptasklock);
997 list_for_each_entry(entry, &gtt->guptasks, list) {
998 if (entry->task == current) {
999 spin_unlock(&gtt->guptasklock);
1000 return false;
1001 }
1002 }
1003 spin_unlock(&gtt->guptasklock);
1004
Christian König2f568db2016-02-23 12:36:59 +01001005 atomic_inc(&gtt->mmu_invalidations);
1006
Christian Königcc1de6e2016-02-08 10:57:22 +01001007 return true;
1008}
1009
Christian König2f568db2016-02-23 12:36:59 +01001010bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1011 int *last_invalidated)
1012{
1013 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1014 int prev_invalidated = *last_invalidated;
1015
1016 *last_invalidated = atomic_read(&gtt->mmu_invalidations);
1017 return prev_invalidated != *last_invalidated;
1018}
1019
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1021{
1022 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1023
1024 if (gtt == NULL)
1025 return false;
1026
1027 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1028}
1029
Chunming Zhou6b777602016-09-21 16:19:19 +08001030uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001031 struct ttm_mem_reg *mem)
1032{
Chunming Zhou6b777602016-09-21 16:19:19 +08001033 uint64_t flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001034
1035 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1036 flags |= AMDGPU_PTE_VALID;
1037
Christian König6d999052015-12-04 13:32:55 +01001038 if (mem && mem->mem_type == TTM_PL_TT) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001039 flags |= AMDGPU_PTE_SYSTEM;
1040
Christian König6d999052015-12-04 13:32:55 +01001041 if (ttm->caching_state == tt_cached)
1042 flags |= AMDGPU_PTE_SNOOPED;
1043 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001044
Alex Xie4b98e0c2017-02-14 12:31:36 -05001045 flags |= adev->gart.gart_pte_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001046 flags |= AMDGPU_PTE_READABLE;
1047
1048 if (!amdgpu_ttm_tt_is_readonly(ttm))
1049 flags |= AMDGPU_PTE_WRITEABLE;
1050
1051 return flags;
1052}
1053
Christian König9982ca62016-10-19 14:44:22 +02001054static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1055 const struct ttm_place *place)
1056{
1057 if (bo->mem.mem_type == TTM_PL_VRAM &&
1058 bo->mem.start == AMDGPU_BO_INVALID_OFFSET) {
1059 unsigned long num_pages = bo->mem.num_pages;
1060 struct drm_mm_node *node = bo->mem.mm_node;
1061
1062 /* Check each drm MM node individually */
1063 while (num_pages) {
1064 if (place->fpfn < (node->start + node->size) &&
1065 !(place->lpfn && place->lpfn <= node->start))
1066 return true;
1067
1068 num_pages -= node->size;
1069 ++node;
1070 }
1071
1072 return false;
1073 }
1074
1075 return ttm_bo_eviction_valuable(bo, place);
1076}
1077
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001078static struct ttm_bo_driver amdgpu_bo_driver = {
1079 .ttm_tt_create = &amdgpu_ttm_tt_create,
1080 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1081 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1082 .invalidate_caches = &amdgpu_invalidate_caches,
1083 .init_mem_type = &amdgpu_init_mem_type,
Christian König9982ca62016-10-19 14:44:22 +02001084 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001085 .evict_flags = &amdgpu_evict_flags,
1086 .move = &amdgpu_bo_move,
1087 .verify_access = &amdgpu_verify_access,
1088 .move_notify = &amdgpu_bo_move_notify,
1089 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1090 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1091 .io_mem_free = &amdgpu_ttm_io_mem_free,
1092};
1093
1094int amdgpu_ttm_init(struct amdgpu_device *adev)
1095{
1096 int r;
1097
Alex Deucher70b5c5a2016-11-15 16:55:53 -05001098 r = amdgpu_ttm_global_init(adev);
1099 if (r) {
1100 return r;
1101 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001102 /* No others user of address space so set it to 0 */
1103 r = ttm_bo_device_init(&adev->mman.bdev,
1104 adev->mman.bo_global_ref.ref.object,
1105 &amdgpu_bo_driver,
1106 adev->ddev->anon_inode->i_mapping,
1107 DRM_FILE_PAGE_OFFSET,
1108 adev->need_dma32);
1109 if (r) {
1110 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1111 return r;
1112 }
1113 adev->mman.initialized = true;
1114 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1115 adev->mc.real_vram_size >> PAGE_SHIFT);
1116 if (r) {
1117 DRM_ERROR("Failed initializing VRAM heap.\n");
1118 return r;
1119 }
1120 /* Change the size here instead of the init above so only lpfn is affected */
1121 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1122
1123 r = amdgpu_bo_create(adev, 256 * 1024, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001124 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +02001125 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1126 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +02001127 NULL, NULL, &adev->stollen_vga_memory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001128 if (r) {
1129 return r;
1130 }
1131 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1132 if (r)
1133 return r;
1134 r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
1135 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1136 if (r) {
1137 amdgpu_bo_unref(&adev->stollen_vga_memory);
1138 return r;
1139 }
1140 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1141 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
1142 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT,
1143 adev->mc.gtt_size >> PAGE_SHIFT);
1144 if (r) {
1145 DRM_ERROR("Failed initializing GTT heap.\n");
1146 return r;
1147 }
1148 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1149 (unsigned)(adev->mc.gtt_size / (1024 * 1024)));
1150
1151 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1152 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1153 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1154 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1155 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1156 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1157 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1158 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1159 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1160 /* GDS Memory */
1161 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1162 adev->gds.mem.total_size >> PAGE_SHIFT);
1163 if (r) {
1164 DRM_ERROR("Failed initializing GDS heap.\n");
1165 return r;
1166 }
1167
1168 /* GWS */
1169 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1170 adev->gds.gws.total_size >> PAGE_SHIFT);
1171 if (r) {
1172 DRM_ERROR("Failed initializing gws heap.\n");
1173 return r;
1174 }
1175
1176 /* OA */
1177 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1178 adev->gds.oa.total_size >> PAGE_SHIFT);
1179 if (r) {
1180 DRM_ERROR("Failed initializing oa heap.\n");
1181 return r;
1182 }
1183
1184 r = amdgpu_ttm_debugfs_init(adev);
1185 if (r) {
1186 DRM_ERROR("Failed to init debugfs\n");
1187 return r;
1188 }
1189 return 0;
1190}
1191
1192void amdgpu_ttm_fini(struct amdgpu_device *adev)
1193{
1194 int r;
1195
1196 if (!adev->mman.initialized)
1197 return;
1198 amdgpu_ttm_debugfs_fini(adev);
1199 if (adev->stollen_vga_memory) {
1200 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1201 if (r == 0) {
1202 amdgpu_bo_unpin(adev->stollen_vga_memory);
1203 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1204 }
1205 amdgpu_bo_unref(&adev->stollen_vga_memory);
1206 }
1207 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1208 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
1209 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1210 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1211 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
1212 ttm_bo_device_release(&adev->mman.bdev);
1213 amdgpu_gart_fini(adev);
1214 amdgpu_ttm_global_fini(adev);
1215 adev->mman.initialized = false;
1216 DRM_INFO("amdgpu: ttm finalized\n");
1217}
1218
1219/* this should only be called at bootup or when userspace
1220 * isn't running */
1221void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1222{
1223 struct ttm_mem_type_manager *man;
1224
1225 if (!adev->mman.initialized)
1226 return;
1227
1228 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1229 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1230 man->size = size >> PAGE_SHIFT;
1231}
1232
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1234{
1235 struct drm_file *file_priv;
1236 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001237
Christian Könige176fe172015-05-27 10:22:47 +02001238 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001240
1241 file_priv = filp->private_data;
1242 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001243 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001245
1246 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247}
1248
1249int amdgpu_copy_buffer(struct amdgpu_ring *ring,
1250 uint64_t src_offset,
1251 uint64_t dst_offset,
1252 uint32_t byte_count,
1253 struct reservation_object *resv,
Chris Wilsonf54d1862016-10-25 13:00:45 +01001254 struct dma_fence **fence, bool direct_submit)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001255{
1256 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001257 struct amdgpu_job *job;
1258
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 uint32_t max_bytes;
1260 unsigned num_loops, num_dw;
1261 unsigned i;
1262 int r;
1263
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001264 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1265 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1266 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1267
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001268 /* for IB padding */
1269 while (num_dw & 0x7)
1270 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001271
Christian Königd71518b2016-02-01 12:20:25 +01001272 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1273 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001274 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001275
1276 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001277 r = amdgpu_sync_resv(adev, &job->sync, resv,
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001278 AMDGPU_FENCE_OWNER_UNDEFINED);
1279 if (r) {
1280 DRM_ERROR("sync failed (%d).\n", r);
1281 goto error_free;
1282 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284
1285 for (i = 0; i < num_loops; i++) {
1286 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1287
Christian Königd71518b2016-02-01 12:20:25 +01001288 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1289 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290
1291 src_offset += cur_size_in_bytes;
1292 dst_offset += cur_size_in_bytes;
1293 byte_count -= cur_size_in_bytes;
1294 }
1295
Christian Königd71518b2016-02-01 12:20:25 +01001296 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1297 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001298 if (direct_submit) {
1299 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
Junwei Zhang50ddc752017-01-23 16:30:38 +08001300 NULL, fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001301 job->fence = dma_fence_get(*fence);
Chunming Zhoue24db982016-08-15 10:46:04 +08001302 if (r)
1303 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1304 amdgpu_job_free(job);
1305 } else {
1306 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1307 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1308 if (r)
1309 goto error_free;
1310 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001311
Chunming Zhoue24db982016-08-15 10:46:04 +08001312 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001313
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001314error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001315 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001316 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001317}
1318
Flora Cui59b4a972016-07-19 16:48:22 +08001319int amdgpu_fill_buffer(struct amdgpu_bo *bo,
Christian Königf29224a62016-11-17 12:06:38 +01001320 uint32_t src_data,
1321 struct reservation_object *resv,
1322 struct dma_fence **fence)
Flora Cui59b4a972016-07-19 16:48:22 +08001323{
Christian Königa7d64de2016-09-15 14:58:48 +02001324 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian Königf29224a62016-11-17 12:06:38 +01001325 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
Flora Cui59b4a972016-07-19 16:48:22 +08001326 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1327
Christian Königf29224a62016-11-17 12:06:38 +01001328 struct drm_mm_node *mm_node;
1329 unsigned long num_pages;
Flora Cui59b4a972016-07-19 16:48:22 +08001330 unsigned int num_loops, num_dw;
Christian Königf29224a62016-11-17 12:06:38 +01001331
1332 struct amdgpu_job *job;
Flora Cui59b4a972016-07-19 16:48:22 +08001333 int r;
1334
Christian Königf29224a62016-11-17 12:06:38 +01001335 if (!ring->ready) {
1336 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1337 return -EINVAL;
1338 }
1339
1340 num_pages = bo->tbo.num_pages;
1341 mm_node = bo->tbo.mem.mm_node;
1342 num_loops = 0;
1343 while (num_pages) {
1344 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1345
1346 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1347 num_pages -= mm_node->size;
1348 ++mm_node;
1349 }
Flora Cui59b4a972016-07-19 16:48:22 +08001350 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
1351
1352 /* for IB padding */
Christian Königf29224a62016-11-17 12:06:38 +01001353 num_dw += 64;
Flora Cui59b4a972016-07-19 16:48:22 +08001354
1355 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1356 if (r)
1357 return r;
1358
1359 if (resv) {
1360 r = amdgpu_sync_resv(adev, &job->sync, resv,
Christian Königf29224a62016-11-17 12:06:38 +01001361 AMDGPU_FENCE_OWNER_UNDEFINED);
Flora Cui59b4a972016-07-19 16:48:22 +08001362 if (r) {
1363 DRM_ERROR("sync failed (%d).\n", r);
1364 goto error_free;
1365 }
1366 }
1367
Christian Königf29224a62016-11-17 12:06:38 +01001368 num_pages = bo->tbo.num_pages;
1369 mm_node = bo->tbo.mem.mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001370
Christian Königf29224a62016-11-17 12:06:38 +01001371 while (num_pages) {
1372 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1373 uint64_t dst_addr;
Flora Cui59b4a972016-07-19 16:48:22 +08001374
Christian Königf29224a62016-11-17 12:06:38 +01001375 r = amdgpu_mm_node_addr(&bo->tbo, mm_node,
1376 &bo->tbo.mem, &dst_addr);
1377 if (r)
1378 return r;
1379
1380 while (byte_count) {
1381 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1382
1383 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
1384 dst_addr, cur_size_in_bytes);
1385
1386 dst_addr += cur_size_in_bytes;
1387 byte_count -= cur_size_in_bytes;
1388 }
1389
1390 num_pages -= mm_node->size;
1391 ++mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001392 }
1393
1394 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1395 WARN_ON(job->ibs[0].length_dw > num_dw);
1396 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
Christian Königf29224a62016-11-17 12:06:38 +01001397 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
Flora Cui59b4a972016-07-19 16:48:22 +08001398 if (r)
1399 goto error_free;
1400
1401 return 0;
1402
1403error_free:
1404 amdgpu_job_free(job);
1405 return r;
1406}
1407
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001408#if defined(CONFIG_DEBUG_FS)
1409
1410static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1411{
1412 struct drm_info_node *node = (struct drm_info_node *)m->private;
1413 unsigned ttm_pl = *(int *)node->info_ent->data;
1414 struct drm_device *dev = node->minor->dev;
1415 struct amdgpu_device *adev = dev->dev_private;
1416 struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Daniel Vetterb5c37142016-12-29 12:09:24 +01001418 struct drm_printer p = drm_seq_file_printer(m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419
1420 spin_lock(&glob->lru_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001421 drm_mm_print(mm, &p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422 spin_unlock(&glob->lru_lock);
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001423 if (ttm_pl == TTM_PL_VRAM)
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001424 seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001425 adev->mman.bdev.man[ttm_pl].size,
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001426 (u64)atomic64_read(&adev->vram_usage) >> 20,
1427 (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001428 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429}
1430
1431static int ttm_pl_vram = TTM_PL_VRAM;
1432static int ttm_pl_tt = TTM_PL_TT;
1433
Nils Wallménius06ab6832016-05-02 12:46:15 -04001434static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001435 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1436 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1437 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1438#ifdef CONFIG_SWIOTLB
1439 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1440#endif
1441};
1442
1443static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1444 size_t size, loff_t *pos)
1445{
Al Viro45063092016-12-04 18:24:56 -05001446 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447 ssize_t result = 0;
1448 int r;
1449
1450 if (size & 0x3 || *pos & 0x3)
1451 return -EINVAL;
1452
1453 while (size) {
1454 unsigned long flags;
1455 uint32_t value;
1456
1457 if (*pos >= adev->mc.mc_vram_size)
1458 return result;
1459
1460 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1461 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1462 WREG32(mmMM_INDEX_HI, *pos >> 31);
1463 value = RREG32(mmMM_DATA);
1464 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1465
1466 r = put_user(value, (uint32_t *)buf);
1467 if (r)
1468 return r;
1469
1470 result += 4;
1471 buf += 4;
1472 *pos += 4;
1473 size -= 4;
1474 }
1475
1476 return result;
1477}
1478
1479static const struct file_operations amdgpu_ttm_vram_fops = {
1480 .owner = THIS_MODULE,
1481 .read = amdgpu_ttm_vram_read,
1482 .llseek = default_llseek
1483};
1484
Christian Königa1d29472016-03-30 14:42:57 +02001485#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1486
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1488 size_t size, loff_t *pos)
1489{
Al Viro45063092016-12-04 18:24:56 -05001490 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491 ssize_t result = 0;
1492 int r;
1493
1494 while (size) {
1495 loff_t p = *pos / PAGE_SIZE;
1496 unsigned off = *pos & ~PAGE_MASK;
1497 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1498 struct page *page;
1499 void *ptr;
1500
1501 if (p >= adev->gart.num_cpu_pages)
1502 return result;
1503
1504 page = adev->gart.pages[p];
1505 if (page) {
1506 ptr = kmap(page);
1507 ptr += off;
1508
1509 r = copy_to_user(buf, ptr, cur_size);
1510 kunmap(adev->gart.pages[p]);
1511 } else
1512 r = clear_user(buf, cur_size);
1513
1514 if (r)
1515 return -EFAULT;
1516
1517 result += cur_size;
1518 buf += cur_size;
1519 *pos += cur_size;
1520 size -= cur_size;
1521 }
1522
1523 return result;
1524}
1525
1526static const struct file_operations amdgpu_ttm_gtt_fops = {
1527 .owner = THIS_MODULE,
1528 .read = amdgpu_ttm_gtt_read,
1529 .llseek = default_llseek
1530};
1531
1532#endif
1533
Christian Königa1d29472016-03-30 14:42:57 +02001534#endif
1535
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1537{
1538#if defined(CONFIG_DEBUG_FS)
1539 unsigned count;
1540
1541 struct drm_minor *minor = adev->ddev->primary;
1542 struct dentry *ent, *root = minor->debugfs_root;
1543
1544 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1545 adev, &amdgpu_ttm_vram_fops);
1546 if (IS_ERR(ent))
1547 return PTR_ERR(ent);
1548 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1549 adev->mman.vram = ent;
1550
Christian Königa1d29472016-03-30 14:42:57 +02001551#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1553 adev, &amdgpu_ttm_gtt_fops);
1554 if (IS_ERR(ent))
1555 return PTR_ERR(ent);
1556 i_size_write(ent->d_inode, adev->mc.gtt_size);
1557 adev->mman.gtt = ent;
1558
Christian Königa1d29472016-03-30 14:42:57 +02001559#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1561
1562#ifdef CONFIG_SWIOTLB
1563 if (!swiotlb_nr_tbl())
1564 --count;
1565#endif
1566
1567 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1568#else
1569
1570 return 0;
1571#endif
1572}
1573
1574static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1575{
1576#if defined(CONFIG_DEBUG_FS)
1577
1578 debugfs_remove(adev->mman.vram);
1579 adev->mman.vram = NULL;
1580
Christian Königa1d29472016-03-30 14:42:57 +02001581#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001582 debugfs_remove(adev->mman.gtt);
1583 adev->mman.gtt = NULL;
1584#endif
Christian Königa1d29472016-03-30 14:42:57 +02001585
1586#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001587}