blob: e238084b71425ebe04801f8a5b308c553c7960bb [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2009 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26/*
27 * Authors:
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
30 * Dave Airlie
31 */
Masahiro Yamada248a1d62017-04-24 13:50:21 +090032#include <drm/ttm/ttm_bo_api.h>
33#include <drm/ttm/ttm_bo_driver.h>
34#include <drm/ttm/ttm_placement.h>
35#include <drm/ttm/ttm_module.h>
36#include <drm/ttm/ttm_page_alloc.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040037#include <drm/drmP.h>
38#include <drm/amdgpu_drm.h>
39#include <linux/seq_file.h>
40#include <linux/slab.h>
41#include <linux/swiotlb.h>
42#include <linux/swap.h>
43#include <linux/pagemap.h>
44#include <linux/debugfs.h>
45#include "amdgpu.h"
46#include "bif/bif_4_1_d.h"
47
48#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
49
Christian Königabca90f2017-06-30 11:05:54 +020050static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
51 struct ttm_mem_reg *mem, unsigned num_pages,
52 uint64_t offset, unsigned window,
53 struct amdgpu_ring *ring,
54 uint64_t *addr);
55
Alex Deucherd38ceaf2015-04-20 16:55:21 -040056static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
57static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
58
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059/*
60 * Global memory.
61 */
62static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
63{
64 return ttm_mem_global_init(ref->object);
65}
66
67static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
68{
69 ttm_mem_global_release(ref->object);
70}
71
Alex Deucher70b5c5a2016-11-15 16:55:53 -050072static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073{
74 struct drm_global_reference *global_ref;
Christian König703297c2016-02-10 14:20:50 +010075 struct amdgpu_ring *ring;
76 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 int r;
78
79 adev->mman.mem_global_referenced = false;
80 global_ref = &adev->mman.mem_global_ref;
81 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
82 global_ref->size = sizeof(struct ttm_mem_global);
83 global_ref->init = &amdgpu_ttm_mem_global_init;
84 global_ref->release = &amdgpu_ttm_mem_global_release;
85 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +080086 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 DRM_ERROR("Failed setting up TTM memory accounting "
88 "subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +080089 goto error_mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 }
91
92 adev->mman.bo_global_ref.mem_glob =
93 adev->mman.mem_global_ref.object;
94 global_ref = &adev->mman.bo_global_ref.ref;
95 global_ref->global_type = DRM_GLOBAL_TTM_BO;
96 global_ref->size = sizeof(struct ttm_bo_global);
97 global_ref->init = &ttm_bo_global_init;
98 global_ref->release = &ttm_bo_global_release;
99 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +0800100 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800102 goto error_bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103 }
104
Christian Königabca90f2017-06-30 11:05:54 +0200105 mutex_init(&adev->mman.gtt_window_lock);
106
Christian König703297c2016-02-10 14:20:50 +0100107 ring = adev->mman.buffer_funcs_ring;
108 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
109 r = amd_sched_entity_init(&ring->sched, &adev->mman.entity,
110 rq, amdgpu_sched_jobs);
Huang Ruie9d035e2016-09-07 20:55:42 +0800111 if (r) {
Christian König703297c2016-02-10 14:20:50 +0100112 DRM_ERROR("Failed setting up TTM BO move run queue.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800113 goto error_entity;
Christian König703297c2016-02-10 14:20:50 +0100114 }
115
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 adev->mman.mem_global_referenced = true;
Christian König703297c2016-02-10 14:20:50 +0100117
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 return 0;
Huang Ruie9d035e2016-09-07 20:55:42 +0800119
120error_entity:
121 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
122error_bo:
123 drm_global_item_unref(&adev->mman.mem_global_ref);
124error_mem:
125 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126}
127
128static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
129{
130 if (adev->mman.mem_global_referenced) {
Christian König703297c2016-02-10 14:20:50 +0100131 amd_sched_entity_fini(adev->mman.entity.sched,
132 &adev->mman.entity);
Christian Königabca90f2017-06-30 11:05:54 +0200133 mutex_destroy(&adev->mman.gtt_window_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
135 drm_global_item_unref(&adev->mman.mem_global_ref);
136 adev->mman.mem_global_referenced = false;
137 }
138}
139
140static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
141{
142 return 0;
143}
144
145static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
146 struct ttm_mem_type_manager *man)
147{
148 struct amdgpu_device *adev;
149
Christian Königa7d64de2016-09-15 14:58:48 +0200150 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400151
152 switch (type) {
153 case TTM_PL_SYSTEM:
154 /* System memory */
155 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
156 man->available_caching = TTM_PL_MASK_CACHING;
157 man->default_caching = TTM_PL_FLAG_CACHED;
158 break;
159 case TTM_PL_TT:
Christian Königbb990bb2016-09-09 16:32:33 +0200160 man->func = &amdgpu_gtt_mgr_func;
Christian König6f02a692017-07-07 11:56:59 +0200161 man->gpu_offset = adev->mc.gart_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162 man->available_caching = TTM_PL_MASK_CACHING;
163 man->default_caching = TTM_PL_FLAG_CACHED;
164 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
165 break;
166 case TTM_PL_VRAM:
167 /* "On-card" video ram */
Christian König6a7f76e2016-08-24 15:51:49 +0200168 man->func = &amdgpu_vram_mgr_func;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 man->gpu_offset = adev->mc.vram_start;
170 man->flags = TTM_MEMTYPE_FLAG_FIXED |
171 TTM_MEMTYPE_FLAG_MAPPABLE;
172 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
173 man->default_caching = TTM_PL_FLAG_WC;
174 break;
175 case AMDGPU_PL_GDS:
176 case AMDGPU_PL_GWS:
177 case AMDGPU_PL_OA:
178 /* On-chip GDS memory*/
179 man->func = &ttm_bo_manager_func;
180 man->gpu_offset = 0;
181 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
182 man->available_caching = TTM_PL_FLAG_UNCACHED;
183 man->default_caching = TTM_PL_FLAG_UNCACHED;
184 break;
185 default:
186 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
187 return -EINVAL;
188 }
189 return 0;
190}
191
192static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
193 struct ttm_placement *placement)
194{
Christian Königa7d64de2016-09-15 14:58:48 +0200195 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian König765e7fb2016-09-15 15:06:50 +0200196 struct amdgpu_bo *abo;
Arvind Yadav1aaa5602017-07-02 14:43:58 +0530197 static const struct ttm_place placements = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 .fpfn = 0,
199 .lpfn = 0,
200 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
201 };
202
203 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) {
204 placement->placement = &placements;
205 placement->busy_placement = &placements;
206 placement->num_placement = 1;
207 placement->num_busy_placement = 1;
208 return;
209 }
Christian König765e7fb2016-09-15 15:06:50 +0200210 abo = container_of(bo, struct amdgpu_bo, tbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211 switch (bo->mem.mem_type) {
212 case TTM_PL_VRAM:
Huang Ruicbcbea92017-04-11 09:24:56 +0800213 if (adev->mman.buffer_funcs &&
214 adev->mman.buffer_funcs_ring &&
215 adev->mman.buffer_funcs_ring->ready == false) {
Christian König765e7fb2016-09-15 15:06:50 +0200216 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Christian König08291c52016-09-12 16:06:18 +0200217 } else {
Christian König765e7fb2016-09-15 15:06:50 +0200218 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
Christian König08291c52016-09-12 16:06:18 +0200219 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400220 break;
221 case TTM_PL_TT:
222 default:
Christian König765e7fb2016-09-15 15:06:50 +0200223 amdgpu_ttm_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400224 }
Christian König765e7fb2016-09-15 15:06:50 +0200225 *placement = abo->placement;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226}
227
228static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
229{
Christian König765e7fb2016-09-15 15:06:50 +0200230 struct amdgpu_bo *abo = container_of(bo, struct amdgpu_bo, tbo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231
Jérôme Glisse054892e2016-04-19 09:07:51 -0400232 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
233 return -EPERM;
Dave Airlie28a39652016-09-30 13:18:26 +1000234 return drm_vma_node_verify_access(&abo->gem_base.vma_node,
David Herrmannd9a1f0b2016-09-01 14:48:33 +0200235 filp->private_data);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236}
237
238static void amdgpu_move_null(struct ttm_buffer_object *bo,
239 struct ttm_mem_reg *new_mem)
240{
241 struct ttm_mem_reg *old_mem = &bo->mem;
242
243 BUG_ON(old_mem->mm_node != NULL);
244 *old_mem = *new_mem;
245 new_mem->mm_node = NULL;
246}
247
Christian König92c60d92017-06-29 10:44:39 +0200248static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
249 struct drm_mm_node *mm_node,
250 struct ttm_mem_reg *mem)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400251{
Christian Königabca90f2017-06-30 11:05:54 +0200252 uint64_t addr = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400253
Christian Königabca90f2017-06-30 11:05:54 +0200254 if (mem->mem_type != TTM_PL_TT ||
255 amdgpu_gtt_mgr_is_allocated(mem)) {
256 addr = mm_node->start << PAGE_SHIFT;
257 addr += bo->bdev->man[mem->mem_type].gpu_offset;
258 }
Christian König92c60d92017-06-29 10:44:39 +0200259 return addr;
Christian König8892f152016-08-17 10:46:52 +0200260}
261
262static int amdgpu_move_blit(struct ttm_buffer_object *bo,
263 bool evict, bool no_wait_gpu,
264 struct ttm_mem_reg *new_mem,
265 struct ttm_mem_reg *old_mem)
266{
Christian Königa7d64de2016-09-15 14:58:48 +0200267 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
Christian König8892f152016-08-17 10:46:52 +0200268 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
269
270 struct drm_mm_node *old_mm, *new_mm;
271 uint64_t old_start, old_size, new_start, new_size;
272 unsigned long num_pages;
Dave Airlie220196b2016-10-28 11:33:52 +1000273 struct dma_fence *fence = NULL;
Christian König8892f152016-08-17 10:46:52 +0200274 int r;
275
276 BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0);
277
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278 if (!ring->ready) {
279 DRM_ERROR("Trying to move memory with ring turned off.\n");
280 return -EINVAL;
281 }
282
Christian König92c60d92017-06-29 10:44:39 +0200283 old_mm = old_mem->mm_node;
284 old_size = old_mm->size;
285 old_start = amdgpu_mm_node_addr(bo, old_mm, old_mem);
286
Christian König8892f152016-08-17 10:46:52 +0200287 new_mm = new_mem->mm_node;
Christian König8892f152016-08-17 10:46:52 +0200288 new_size = new_mm->size;
Christian König92c60d92017-06-29 10:44:39 +0200289 new_start = amdgpu_mm_node_addr(bo, new_mm, new_mem);
Christian König8892f152016-08-17 10:46:52 +0200290
291 num_pages = new_mem->num_pages;
Christian Königabca90f2017-06-30 11:05:54 +0200292 mutex_lock(&adev->mman.gtt_window_lock);
Christian König8892f152016-08-17 10:46:52 +0200293 while (num_pages) {
Christian Königabca90f2017-06-30 11:05:54 +0200294 unsigned long cur_pages = min(min(old_size, new_size),
295 (u64)AMDGPU_GTT_MAX_TRANSFER_SIZE);
296 uint64_t from = old_start, to = new_start;
Dave Airlie220196b2016-10-28 11:33:52 +1000297 struct dma_fence *next;
Christian König8892f152016-08-17 10:46:52 +0200298
Christian Königabca90f2017-06-30 11:05:54 +0200299 if (old_mem->mem_type == TTM_PL_TT &&
300 !amdgpu_gtt_mgr_is_allocated(old_mem)) {
301 r = amdgpu_map_buffer(bo, old_mem, cur_pages,
302 old_start, 0, ring, &from);
303 if (r)
304 goto error;
305 }
306
307 if (new_mem->mem_type == TTM_PL_TT &&
308 !amdgpu_gtt_mgr_is_allocated(new_mem)) {
309 r = amdgpu_map_buffer(bo, new_mem, cur_pages,
310 new_start, 1, ring, &to);
311 if (r)
312 goto error;
313 }
314
315 r = amdgpu_copy_buffer(ring, from, to,
Christian König8892f152016-08-17 10:46:52 +0200316 cur_pages * PAGE_SIZE,
Christian Königabca90f2017-06-30 11:05:54 +0200317 bo->resv, &next, false, true);
Christian König8892f152016-08-17 10:46:52 +0200318 if (r)
319 goto error;
320
Dave Airlie220196b2016-10-28 11:33:52 +1000321 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200322 fence = next;
323
324 num_pages -= cur_pages;
325 if (!num_pages)
326 break;
327
328 old_size -= cur_pages;
329 if (!old_size) {
Christian König92c60d92017-06-29 10:44:39 +0200330 old_start = amdgpu_mm_node_addr(bo, ++old_mm, old_mem);
Christian König8892f152016-08-17 10:46:52 +0200331 old_size = old_mm->size;
332 } else {
333 old_start += cur_pages * PAGE_SIZE;
334 }
335
336 new_size -= cur_pages;
337 if (!new_size) {
Christian König92c60d92017-06-29 10:44:39 +0200338 new_start = amdgpu_mm_node_addr(bo, ++new_mm, new_mem);
Christian König8892f152016-08-17 10:46:52 +0200339 new_size = new_mm->size;
340 } else {
341 new_start += cur_pages * PAGE_SIZE;
342 }
343 }
Christian Königabca90f2017-06-30 11:05:54 +0200344 mutex_unlock(&adev->mman.gtt_window_lock);
Christian Königce64bc22016-06-15 13:44:05 +0200345
346 r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100347 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 return r;
Christian König8892f152016-08-17 10:46:52 +0200349
350error:
Christian Königabca90f2017-06-30 11:05:54 +0200351 mutex_unlock(&adev->mman.gtt_window_lock);
352
Christian König8892f152016-08-17 10:46:52 +0200353 if (fence)
Dave Airlie220196b2016-10-28 11:33:52 +1000354 dma_fence_wait(fence, false);
355 dma_fence_put(fence);
Christian König8892f152016-08-17 10:46:52 +0200356 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357}
358
359static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo,
360 bool evict, bool interruptible,
361 bool no_wait_gpu,
362 struct ttm_mem_reg *new_mem)
363{
364 struct amdgpu_device *adev;
365 struct ttm_mem_reg *old_mem = &bo->mem;
366 struct ttm_mem_reg tmp_mem;
367 struct ttm_place placements;
368 struct ttm_placement placement;
369 int r;
370
Christian Königa7d64de2016-09-15 14:58:48 +0200371 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400372 tmp_mem = *new_mem;
373 tmp_mem.mm_node = NULL;
374 placement.num_placement = 1;
375 placement.placement = &placements;
376 placement.num_busy_placement = 1;
377 placement.busy_placement = &placements;
378 placements.fpfn = 0;
Christian König5e7e8392017-06-30 12:19:42 +0200379 placements.lpfn = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
381 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
382 interruptible, no_wait_gpu);
383 if (unlikely(r)) {
384 return r;
385 }
386
387 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
388 if (unlikely(r)) {
389 goto out_cleanup;
390 }
391
392 r = ttm_tt_bind(bo->ttm, &tmp_mem);
393 if (unlikely(r)) {
394 goto out_cleanup;
395 }
396 r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
397 if (unlikely(r)) {
398 goto out_cleanup;
399 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900400 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400401out_cleanup:
402 ttm_bo_mem_put(bo, &tmp_mem);
403 return r;
404}
405
406static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo,
407 bool evict, bool interruptible,
408 bool no_wait_gpu,
409 struct ttm_mem_reg *new_mem)
410{
411 struct amdgpu_device *adev;
412 struct ttm_mem_reg *old_mem = &bo->mem;
413 struct ttm_mem_reg tmp_mem;
414 struct ttm_placement placement;
415 struct ttm_place placements;
416 int r;
417
Christian Königa7d64de2016-09-15 14:58:48 +0200418 adev = amdgpu_ttm_adev(bo->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419 tmp_mem = *new_mem;
420 tmp_mem.mm_node = NULL;
421 placement.num_placement = 1;
422 placement.placement = &placements;
423 placement.num_busy_placement = 1;
424 placement.busy_placement = &placements;
425 placements.fpfn = 0;
Christian König5e7e8392017-06-30 12:19:42 +0200426 placements.lpfn = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400427 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
428 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
429 interruptible, no_wait_gpu);
430 if (unlikely(r)) {
431 return r;
432 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900433 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434 if (unlikely(r)) {
435 goto out_cleanup;
436 }
437 r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
438 if (unlikely(r)) {
439 goto out_cleanup;
440 }
441out_cleanup:
442 ttm_bo_mem_put(bo, &tmp_mem);
443 return r;
444}
445
446static int amdgpu_bo_move(struct ttm_buffer_object *bo,
447 bool evict, bool interruptible,
448 bool no_wait_gpu,
449 struct ttm_mem_reg *new_mem)
450{
451 struct amdgpu_device *adev;
Michel Dänzer104ece92016-03-28 12:53:02 +0900452 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 struct ttm_mem_reg *old_mem = &bo->mem;
454 int r;
455
Michel Dänzer104ece92016-03-28 12:53:02 +0900456 /* Can't move a pinned BO */
457 abo = container_of(bo, struct amdgpu_bo, tbo);
458 if (WARN_ON_ONCE(abo->pin_count > 0))
459 return -EINVAL;
460
Christian Königa7d64de2016-09-15 14:58:48 +0200461 adev = amdgpu_ttm_adev(bo->bdev);
Christian Königdbd5ed62016-06-21 16:28:14 +0200462
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
464 amdgpu_move_null(bo, new_mem);
465 return 0;
466 }
467 if ((old_mem->mem_type == TTM_PL_TT &&
468 new_mem->mem_type == TTM_PL_SYSTEM) ||
469 (old_mem->mem_type == TTM_PL_SYSTEM &&
470 new_mem->mem_type == TTM_PL_TT)) {
471 /* bind is enough */
472 amdgpu_move_null(bo, new_mem);
473 return 0;
474 }
475 if (adev->mman.buffer_funcs == NULL ||
476 adev->mman.buffer_funcs_ring == NULL ||
477 !adev->mman.buffer_funcs_ring->ready) {
478 /* use memcpy */
479 goto memcpy;
480 }
481
482 if (old_mem->mem_type == TTM_PL_VRAM &&
483 new_mem->mem_type == TTM_PL_SYSTEM) {
484 r = amdgpu_move_vram_ram(bo, evict, interruptible,
485 no_wait_gpu, new_mem);
486 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
487 new_mem->mem_type == TTM_PL_VRAM) {
488 r = amdgpu_move_ram_vram(bo, evict, interruptible,
489 no_wait_gpu, new_mem);
490 } else {
491 r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
492 }
493
494 if (r) {
495memcpy:
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900496 r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497 if (r) {
498 return r;
499 }
500 }
501
502 /* update statistics */
503 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
504 return 0;
505}
506
507static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
508{
509 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
Christian Königa7d64de2016-09-15 14:58:48 +0200510 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511
512 mem->bus.addr = NULL;
513 mem->bus.offset = 0;
514 mem->bus.size = mem->num_pages << PAGE_SHIFT;
515 mem->bus.base = 0;
516 mem->bus.is_iomem = false;
517 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
518 return -EINVAL;
519 switch (mem->mem_type) {
520 case TTM_PL_SYSTEM:
521 /* system memory */
522 return 0;
523 case TTM_PL_TT:
524 break;
525 case TTM_PL_VRAM:
526 mem->bus.offset = mem->start << PAGE_SHIFT;
527 /* check if it's visible */
528 if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
529 return -EINVAL;
530 mem->bus.base = adev->mc.aper_base;
531 mem->bus.is_iomem = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400532 break;
533 default:
534 return -EINVAL;
535 }
536 return 0;
537}
538
539static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
540{
541}
542
Christian König9bbdcc02017-03-29 11:16:05 +0200543static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
544 unsigned long page_offset)
545{
546 struct drm_mm_node *mm = bo->mem.mm_node;
547 uint64_t size = mm->size;
Dave Airlie01687782017-04-07 05:41:42 +1000548 uint64_t offset = page_offset;
Christian König9bbdcc02017-03-29 11:16:05 +0200549
550 page_offset = do_div(offset, size);
Christian Königecdba5d2017-04-07 10:40:04 +0200551 mm += offset;
Christian König9bbdcc02017-03-29 11:16:05 +0200552 return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset;
553}
554
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555/*
556 * TTM backend functions.
557 */
Christian König637dd3b2016-03-03 14:24:57 +0100558struct amdgpu_ttm_gup_task_list {
559 struct list_head list;
560 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561};
562
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100564 struct ttm_dma_tt ttm;
565 struct amdgpu_device *adev;
566 u64 offset;
567 uint64_t userptr;
568 struct mm_struct *usermm;
569 uint32_t userflags;
570 spinlock_t guptasklock;
571 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100572 atomic_t mmu_invalidations;
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800573 struct list_head list;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574};
575
Christian König2f568db2016-02-23 12:36:59 +0100576int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100579 unsigned int flags = 0;
Christian König2f568db2016-02-23 12:36:59 +0100580 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 int r;
582
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100583 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
584 flags |= FOLL_WRITE;
585
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100587 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 to prevent problems with writeback */
589 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
590 struct vm_area_struct *vma;
591
592 vma = find_vma(gtt->usermm, gtt->userptr);
593 if (!vma || vma->vm_file || vma->vm_end < end)
594 return -EPERM;
595 }
596
597 do {
598 unsigned num_pages = ttm->num_pages - pinned;
599 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100600 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100601 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400602
Christian König637dd3b2016-03-03 14:24:57 +0100603 guptask.task = current;
604 spin_lock(&gtt->guptasklock);
605 list_add(&guptask.list, &gtt->guptasks);
606 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400607
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100608 r = get_user_pages(userptr, num_pages, flags, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100609
610 spin_lock(&gtt->guptasklock);
611 list_del(&guptask.list);
612 spin_unlock(&gtt->guptasklock);
613
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 if (r < 0)
615 goto release_pages;
616
617 pinned += r;
618
619 } while (pinned < ttm->num_pages);
620
Christian König2f568db2016-02-23 12:36:59 +0100621 return 0;
622
623release_pages:
624 release_pages(pages, pinned, 0);
625 return r;
626}
627
628/* prepare the sg table with the user pages */
629static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
630{
Christian Königa7d64de2016-09-15 14:58:48 +0200631 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Christian König2f568db2016-02-23 12:36:59 +0100632 struct amdgpu_ttm_tt *gtt = (void *)ttm;
633 unsigned nents;
634 int r;
635
636 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
637 enum dma_data_direction direction = write ?
638 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
639
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
641 ttm->num_pages << PAGE_SHIFT,
642 GFP_KERNEL);
643 if (r)
644 goto release_sg;
645
646 r = -ENOMEM;
647 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
648 if (nents != ttm->sg->nents)
649 goto release_sg;
650
651 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
652 gtt->ttm.dma_address, ttm->num_pages);
653
654 return 0;
655
656release_sg:
657 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400658 return r;
659}
660
661static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
662{
Christian Königa7d64de2016-09-15 14:58:48 +0200663 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 struct amdgpu_ttm_tt *gtt = (void *)ttm;
monk.liudd08fae2015-05-07 14:19:18 -0400665 struct sg_page_iter sg_iter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666
667 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
668 enum dma_data_direction direction = write ?
669 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
670
671 /* double check that we don't free the table twice */
672 if (!ttm->sg->sgl)
673 return;
674
675 /* free the sg table and pages again */
676 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
677
monk.liudd08fae2015-05-07 14:19:18 -0400678 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
679 struct page *page = sg_page_iter_page(&sg_iter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
681 set_page_dirty(page);
682
683 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300684 put_page(page);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 }
686
687 sg_free_table(ttm->sg);
688}
689
Christian König98a7f882017-06-30 10:41:07 +0200690static int amdgpu_ttm_do_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
691{
692 struct amdgpu_ttm_tt *gtt = (void *)ttm;
693 uint64_t flags;
694 int r;
695
696 spin_lock(&gtt->adev->gtt_list_lock);
697 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, mem);
698 gtt->offset = (u64)mem->start << PAGE_SHIFT;
699 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
700 ttm->pages, gtt->ttm.dma_address, flags);
701
702 if (r) {
703 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
704 ttm->num_pages, gtt->offset);
705 goto error_gart_bind;
706 }
707
708 list_add_tail(&gtt->list, &gtt->adev->gtt_list);
709error_gart_bind:
710 spin_unlock(&gtt->adev->gtt_list_lock);
711 return r;
712
713}
714
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
716 struct ttm_mem_reg *bo_mem)
717{
718 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719 int r;
720
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800721 if (gtt->userptr) {
722 r = amdgpu_ttm_tt_pin_userptr(ttm);
723 if (r) {
724 DRM_ERROR("failed to pin userptr\n");
725 return r;
726 }
727 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728 if (!ttm->num_pages) {
729 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
730 ttm->num_pages, bo_mem, ttm);
731 }
732
733 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
734 bo_mem->mem_type == AMDGPU_PL_GWS ||
735 bo_mem->mem_type == AMDGPU_PL_OA)
736 return -EINVAL;
737
Christian König98a7f882017-06-30 10:41:07 +0200738 if (amdgpu_gtt_mgr_is_allocated(bo_mem))
739 r = amdgpu_ttm_do_bind(ttm, bo_mem);
740
741 return r;
Christian Königc855e252016-09-05 17:00:57 +0200742}
743
744bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
745{
746 struct amdgpu_ttm_tt *gtt = (void *)ttm;
747
748 return gtt && !list_empty(&gtt->list);
749}
750
Christian Königbb990bb2016-09-09 16:32:33 +0200751int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
Christian Königc855e252016-09-05 17:00:57 +0200752{
Christian Königbb990bb2016-09-09 16:32:33 +0200753 struct ttm_tt *ttm = bo->ttm;
Christian Königc855e252016-09-05 17:00:57 +0200754 int r;
755
756 if (!ttm || amdgpu_ttm_is_bound(ttm))
757 return 0;
758
Christian Königbb990bb2016-09-09 16:32:33 +0200759 r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo,
760 NULL, bo_mem);
761 if (r) {
762 DRM_ERROR("Failed to allocate GTT address space (%d)\n", r);
763 return r;
764 }
765
Christian König98a7f882017-06-30 10:41:07 +0200766 return amdgpu_ttm_do_bind(ttm, bo_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400767}
768
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800769int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
770{
771 struct amdgpu_ttm_tt *gtt, *tmp;
772 struct ttm_mem_reg bo_mem;
Monk Liu1d1a2cd2017-04-27 17:14:57 +0800773 uint64_t flags;
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800774 int r;
775
776 bo_mem.mem_type = TTM_PL_TT;
777 spin_lock(&adev->gtt_list_lock);
778 list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
779 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, &gtt->ttm.ttm, &bo_mem);
780 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
781 gtt->ttm.ttm.pages, gtt->ttm.dma_address,
782 flags);
783 if (r) {
784 spin_unlock(&adev->gtt_list_lock);
Christian König71c76a02016-09-03 16:18:26 +0200785 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
786 gtt->ttm.ttm.num_pages, gtt->offset);
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800787 return r;
788 }
789 }
790 spin_unlock(&adev->gtt_list_lock);
791 return 0;
792}
793
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
795{
796 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Roger.He738f64c2017-05-05 13:27:10 +0800797 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798
Christian König85a4b572016-09-22 14:19:50 +0200799 if (gtt->userptr)
800 amdgpu_ttm_tt_unpin_userptr(ttm);
801
Christian König78ab0a32016-09-09 15:39:08 +0200802 if (!amdgpu_ttm_is_bound(ttm))
803 return 0;
804
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800806 spin_lock(&gtt->adev->gtt_list_lock);
Roger.He738f64c2017-05-05 13:27:10 +0800807 r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
808 if (r) {
809 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
810 gtt->ttm.ttm.num_pages, gtt->offset);
811 goto error_unbind;
812 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800813 list_del_init(&gtt->list);
Roger.He738f64c2017-05-05 13:27:10 +0800814error_unbind:
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800815 spin_unlock(&gtt->adev->gtt_list_lock);
Roger.He738f64c2017-05-05 13:27:10 +0800816 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400817}
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
Chunming Zhou6b777602016-09-21 16:19:19 +08001025uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001026 struct ttm_mem_reg *mem)
1027{
Chunming Zhou6b777602016-09-21 16:19:19 +08001028 uint64_t flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001029
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
Alex Xie4b98e0c2017-02-14 12:31:36 -05001040 flags |= adev->gart.gart_pte_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041 flags |= AMDGPU_PTE_READABLE;
1042
1043 if (!amdgpu_ttm_tt_is_readonly(ttm))
1044 flags |= AMDGPU_PTE_WRITEABLE;
1045
1046 return flags;
1047}
1048
Christian König9982ca62016-10-19 14:44:22 +02001049static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1050 const struct ttm_place *place)
1051{
Christian König4fcae782017-04-20 12:11:47 +02001052 unsigned long num_pages = bo->mem.num_pages;
1053 struct drm_mm_node *node = bo->mem.mm_node;
Christian König9982ca62016-10-19 14:44:22 +02001054
Christian König4fcae782017-04-20 12:11:47 +02001055 if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
1056 return ttm_bo_eviction_valuable(bo, place);
1057
1058 switch (bo->mem.mem_type) {
1059 case TTM_PL_TT:
1060 return true;
1061
1062 case TTM_PL_VRAM:
Christian König9982ca62016-10-19 14:44:22 +02001063 /* Check each drm MM node individually */
1064 while (num_pages) {
1065 if (place->fpfn < (node->start + node->size) &&
1066 !(place->lpfn && place->lpfn <= node->start))
1067 return true;
1068
1069 num_pages -= node->size;
1070 ++node;
1071 }
Christian König4fcae782017-04-20 12:11:47 +02001072 break;
Christian König9982ca62016-10-19 14:44:22 +02001073
Christian König4fcae782017-04-20 12:11:47 +02001074 default:
1075 break;
Christian König9982ca62016-10-19 14:44:22 +02001076 }
1077
1078 return ttm_bo_eviction_valuable(bo, place);
1079}
1080
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081static struct ttm_bo_driver amdgpu_bo_driver = {
1082 .ttm_tt_create = &amdgpu_ttm_tt_create,
1083 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1084 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1085 .invalidate_caches = &amdgpu_invalidate_caches,
1086 .init_mem_type = &amdgpu_init_mem_type,
Christian König9982ca62016-10-19 14:44:22 +02001087 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001088 .evict_flags = &amdgpu_evict_flags,
1089 .move = &amdgpu_bo_move,
1090 .verify_access = &amdgpu_verify_access,
1091 .move_notify = &amdgpu_bo_move_notify,
1092 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1093 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1094 .io_mem_free = &amdgpu_ttm_io_mem_free,
Christian König9bbdcc02017-03-29 11:16:05 +02001095 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001096};
1097
1098int amdgpu_ttm_init(struct amdgpu_device *adev)
1099{
Christian König36d38372017-07-07 13:17:45 +02001100 uint64_t gtt_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 int r;
John Brooks218b5dc2017-06-27 22:33:17 -04001102 u64 vis_vram_limit;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001103
Alex Deucher70b5c5a2016-11-15 16:55:53 -05001104 r = amdgpu_ttm_global_init(adev);
1105 if (r) {
1106 return r;
1107 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001108 /* No others user of address space so set it to 0 */
1109 r = ttm_bo_device_init(&adev->mman.bdev,
1110 adev->mman.bo_global_ref.ref.object,
1111 &amdgpu_bo_driver,
1112 adev->ddev->anon_inode->i_mapping,
1113 DRM_FILE_PAGE_OFFSET,
1114 adev->need_dma32);
1115 if (r) {
1116 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1117 return r;
1118 }
1119 adev->mman.initialized = true;
1120 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1121 adev->mc.real_vram_size >> PAGE_SHIFT);
1122 if (r) {
1123 DRM_ERROR("Failed initializing VRAM heap.\n");
1124 return r;
1125 }
John Brooks218b5dc2017-06-27 22:33:17 -04001126
1127 /* Reduce size of CPU-visible VRAM if requested */
1128 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1129 if (amdgpu_vis_vram_limit > 0 &&
1130 vis_vram_limit <= adev->mc.visible_vram_size)
1131 adev->mc.visible_vram_size = vis_vram_limit;
1132
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133 /* Change the size here instead of the init above so only lpfn is affected */
1134 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1135
Huang Rui916910a2017-05-31 10:35:42 +08001136 r = amdgpu_bo_create(adev, adev->mc.stolen_size, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001137 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +02001138 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1139 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +02001140 NULL, NULL, &adev->stollen_vga_memory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001141 if (r) {
1142 return r;
1143 }
1144 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1145 if (r)
1146 return r;
1147 r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
1148 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1149 if (r) {
1150 amdgpu_bo_unref(&adev->stollen_vga_memory);
1151 return r;
1152 }
1153 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1154 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
Christian König36d38372017-07-07 13:17:45 +02001155
1156 if (amdgpu_gtt_size == -1)
1157 gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1158 adev->mc.mc_vram_size);
1159 else
1160 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1161 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162 if (r) {
1163 DRM_ERROR("Failed initializing GTT heap.\n");
1164 return r;
1165 }
1166 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
Christian König36d38372017-07-07 13:17:45 +02001167 (unsigned)(gtt_size / (1024 * 1024)));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168
1169 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1170 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1171 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1172 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1173 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1174 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1175 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1176 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1177 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1178 /* GDS Memory */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001179 if (adev->gds.mem.total_size) {
1180 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1181 adev->gds.mem.total_size >> PAGE_SHIFT);
1182 if (r) {
1183 DRM_ERROR("Failed initializing GDS heap.\n");
1184 return r;
1185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001186 }
1187
1188 /* GWS */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001189 if (adev->gds.gws.total_size) {
1190 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1191 adev->gds.gws.total_size >> PAGE_SHIFT);
1192 if (r) {
1193 DRM_ERROR("Failed initializing gws heap.\n");
1194 return r;
1195 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196 }
1197
1198 /* OA */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001199 if (adev->gds.oa.total_size) {
1200 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1201 adev->gds.oa.total_size >> PAGE_SHIFT);
1202 if (r) {
1203 DRM_ERROR("Failed initializing oa heap.\n");
1204 return r;
1205 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001206 }
1207
1208 r = amdgpu_ttm_debugfs_init(adev);
1209 if (r) {
1210 DRM_ERROR("Failed to init debugfs\n");
1211 return r;
1212 }
1213 return 0;
1214}
1215
1216void amdgpu_ttm_fini(struct amdgpu_device *adev)
1217{
1218 int r;
1219
1220 if (!adev->mman.initialized)
1221 return;
1222 amdgpu_ttm_debugfs_fini(adev);
1223 if (adev->stollen_vga_memory) {
Michel Dänzerc81a1a72017-04-28 17:28:14 +09001224 r = amdgpu_bo_reserve(adev->stollen_vga_memory, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 if (r == 0) {
1226 amdgpu_bo_unpin(adev->stollen_vga_memory);
1227 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1228 }
1229 amdgpu_bo_unref(&adev->stollen_vga_memory);
1230 }
1231 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1232 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
Alex Deucherd2d51d82017-03-15 09:45:48 -04001233 if (adev->gds.mem.total_size)
1234 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1235 if (adev->gds.gws.total_size)
1236 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1237 if (adev->gds.oa.total_size)
1238 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 ttm_bo_device_release(&adev->mman.bdev);
1240 amdgpu_gart_fini(adev);
1241 amdgpu_ttm_global_fini(adev);
1242 adev->mman.initialized = false;
1243 DRM_INFO("amdgpu: ttm finalized\n");
1244}
1245
1246/* this should only be called at bootup or when userspace
1247 * isn't running */
1248void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1249{
1250 struct ttm_mem_type_manager *man;
1251
1252 if (!adev->mman.initialized)
1253 return;
1254
1255 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1256 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1257 man->size = size >> PAGE_SHIFT;
1258}
1259
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1261{
1262 struct drm_file *file_priv;
1263 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001264
Christian Könige176fe172015-05-27 10:22:47 +02001265 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001267
1268 file_priv = filp->private_data;
1269 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001270 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001271 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001272
1273 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001274}
1275
Christian Königabca90f2017-06-30 11:05:54 +02001276static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
1277 struct ttm_mem_reg *mem, unsigned num_pages,
1278 uint64_t offset, unsigned window,
1279 struct amdgpu_ring *ring,
1280 uint64_t *addr)
1281{
1282 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
1283 struct amdgpu_device *adev = ring->adev;
1284 struct ttm_tt *ttm = bo->ttm;
1285 struct amdgpu_job *job;
1286 unsigned num_dw, num_bytes;
1287 dma_addr_t *dma_address;
1288 struct dma_fence *fence;
1289 uint64_t src_addr, dst_addr;
1290 uint64_t flags;
1291 int r;
1292
1293 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
1294 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
1295
Christian König6f02a692017-07-07 11:56:59 +02001296 *addr = adev->mc.gart_start;
Christian Königabca90f2017-06-30 11:05:54 +02001297 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
1298 AMDGPU_GPU_PAGE_SIZE;
1299
1300 num_dw = adev->mman.buffer_funcs->copy_num_dw;
1301 while (num_dw & 0x7)
1302 num_dw++;
1303
1304 num_bytes = num_pages * 8;
1305
1306 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job);
1307 if (r)
1308 return r;
1309
1310 src_addr = num_dw * 4;
1311 src_addr += job->ibs[0].gpu_addr;
1312
1313 dst_addr = adev->gart.table_addr;
1314 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
1315 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
1316 dst_addr, num_bytes);
1317
1318 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1319 WARN_ON(job->ibs[0].length_dw > num_dw);
1320
1321 dma_address = &gtt->ttm.dma_address[offset >> PAGE_SHIFT];
1322 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem);
1323 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
1324 &job->ibs[0].ptr[num_dw]);
1325 if (r)
1326 goto error_free;
1327
1328 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1329 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
1330 if (r)
1331 goto error_free;
1332
1333 dma_fence_put(fence);
1334
1335 return r;
1336
1337error_free:
1338 amdgpu_job_free(job);
1339 return r;
1340}
1341
Christian Königfc9c8f52017-06-29 11:46:15 +02001342int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1343 uint64_t dst_offset, uint32_t byte_count,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001344 struct reservation_object *resv,
Christian Königfc9c8f52017-06-29 11:46:15 +02001345 struct dma_fence **fence, bool direct_submit,
1346 bool vm_needs_flush)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001347{
1348 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001349 struct amdgpu_job *job;
1350
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001351 uint32_t max_bytes;
1352 unsigned num_loops, num_dw;
1353 unsigned i;
1354 int r;
1355
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1357 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1358 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1359
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001360 /* for IB padding */
1361 while (num_dw & 0x7)
1362 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363
Christian Königd71518b2016-02-01 12:20:25 +01001364 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1365 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001366 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001367
Christian Königfc9c8f52017-06-29 11:46:15 +02001368 job->vm_needs_flush = vm_needs_flush;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001369 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001370 r = amdgpu_sync_resv(adev, &job->sync, resv,
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001371 AMDGPU_FENCE_OWNER_UNDEFINED);
1372 if (r) {
1373 DRM_ERROR("sync failed (%d).\n", r);
1374 goto error_free;
1375 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001376 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377
1378 for (i = 0; i < num_loops; i++) {
1379 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1380
Christian Königd71518b2016-02-01 12:20:25 +01001381 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1382 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001383
1384 src_offset += cur_size_in_bytes;
1385 dst_offset += cur_size_in_bytes;
1386 byte_count -= cur_size_in_bytes;
1387 }
1388
Christian Königd71518b2016-02-01 12:20:25 +01001389 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1390 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001391 if (direct_submit) {
1392 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
Junwei Zhang50ddc752017-01-23 16:30:38 +08001393 NULL, fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001394 job->fence = dma_fence_get(*fence);
Chunming Zhoue24db982016-08-15 10:46:04 +08001395 if (r)
1396 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1397 amdgpu_job_free(job);
1398 } else {
1399 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1400 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1401 if (r)
1402 goto error_free;
1403 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001404
Chunming Zhoue24db982016-08-15 10:46:04 +08001405 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001406
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001407error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001408 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001409 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410}
1411
Flora Cui59b4a972016-07-19 16:48:22 +08001412int amdgpu_fill_buffer(struct amdgpu_bo *bo,
Christian Königf29224a62016-11-17 12:06:38 +01001413 uint32_t src_data,
1414 struct reservation_object *resv,
1415 struct dma_fence **fence)
Flora Cui59b4a972016-07-19 16:48:22 +08001416{
Christian Königa7d64de2016-09-15 14:58:48 +02001417 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian Königf29224a62016-11-17 12:06:38 +01001418 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
Flora Cui59b4a972016-07-19 16:48:22 +08001419 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1420
Christian Königf29224a62016-11-17 12:06:38 +01001421 struct drm_mm_node *mm_node;
1422 unsigned long num_pages;
Flora Cui59b4a972016-07-19 16:48:22 +08001423 unsigned int num_loops, num_dw;
Christian Königf29224a62016-11-17 12:06:38 +01001424
1425 struct amdgpu_job *job;
Flora Cui59b4a972016-07-19 16:48:22 +08001426 int r;
1427
Christian Königf29224a62016-11-17 12:06:38 +01001428 if (!ring->ready) {
1429 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1430 return -EINVAL;
1431 }
1432
Christian König92c60d92017-06-29 10:44:39 +02001433 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
1434 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
1435 if (r)
1436 return r;
1437 }
1438
Christian Königf29224a62016-11-17 12:06:38 +01001439 num_pages = bo->tbo.num_pages;
1440 mm_node = bo->tbo.mem.mm_node;
1441 num_loops = 0;
1442 while (num_pages) {
1443 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1444
1445 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1446 num_pages -= mm_node->size;
1447 ++mm_node;
1448 }
Flora Cui59b4a972016-07-19 16:48:22 +08001449 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
1450
1451 /* for IB padding */
Christian Königf29224a62016-11-17 12:06:38 +01001452 num_dw += 64;
Flora Cui59b4a972016-07-19 16:48:22 +08001453
1454 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1455 if (r)
1456 return r;
1457
1458 if (resv) {
1459 r = amdgpu_sync_resv(adev, &job->sync, resv,
Christian Königf29224a62016-11-17 12:06:38 +01001460 AMDGPU_FENCE_OWNER_UNDEFINED);
Flora Cui59b4a972016-07-19 16:48:22 +08001461 if (r) {
1462 DRM_ERROR("sync failed (%d).\n", r);
1463 goto error_free;
1464 }
1465 }
1466
Christian Königf29224a62016-11-17 12:06:38 +01001467 num_pages = bo->tbo.num_pages;
1468 mm_node = bo->tbo.mem.mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001469
Christian Königf29224a62016-11-17 12:06:38 +01001470 while (num_pages) {
1471 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1472 uint64_t dst_addr;
Flora Cui59b4a972016-07-19 16:48:22 +08001473
Christian König92c60d92017-06-29 10:44:39 +02001474 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
Christian Königf29224a62016-11-17 12:06:38 +01001475 while (byte_count) {
1476 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1477
1478 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
1479 dst_addr, cur_size_in_bytes);
1480
1481 dst_addr += cur_size_in_bytes;
1482 byte_count -= cur_size_in_bytes;
1483 }
1484
1485 num_pages -= mm_node->size;
1486 ++mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001487 }
1488
1489 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1490 WARN_ON(job->ibs[0].length_dw > num_dw);
1491 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
Christian Königf29224a62016-11-17 12:06:38 +01001492 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
Flora Cui59b4a972016-07-19 16:48:22 +08001493 if (r)
1494 goto error_free;
1495
1496 return 0;
1497
1498error_free:
1499 amdgpu_job_free(job);
1500 return r;
1501}
1502
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001503#if defined(CONFIG_DEBUG_FS)
1504
Chunming Zhou05a72a22017-04-13 16:16:51 +08001505extern void amdgpu_gtt_mgr_print(struct seq_file *m, struct ttm_mem_type_manager
1506 *man);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001507static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1508{
1509 struct drm_info_node *node = (struct drm_info_node *)m->private;
1510 unsigned ttm_pl = *(int *)node->info_ent->data;
1511 struct drm_device *dev = node->minor->dev;
1512 struct amdgpu_device *adev = dev->dev_private;
1513 struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001514 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Daniel Vetterb5c37142016-12-29 12:09:24 +01001515 struct drm_printer p = drm_seq_file_printer(m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001516
1517 spin_lock(&glob->lru_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001518 drm_mm_print(mm, &p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001519 spin_unlock(&glob->lru_lock);
Chunming Zhou05a72a22017-04-13 16:16:51 +08001520 switch (ttm_pl) {
1521 case TTM_PL_VRAM:
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001522 seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001523 adev->mman.bdev.man[ttm_pl].size,
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001524 (u64)atomic64_read(&adev->vram_usage) >> 20,
1525 (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
Chunming Zhou05a72a22017-04-13 16:16:51 +08001526 break;
1527 case TTM_PL_TT:
1528 amdgpu_gtt_mgr_print(m, &adev->mman.bdev.man[TTM_PL_TT]);
1529 break;
1530 }
Daniel Vetterb5c37142016-12-29 12:09:24 +01001531 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001532}
1533
1534static int ttm_pl_vram = TTM_PL_VRAM;
1535static int ttm_pl_tt = TTM_PL_TT;
1536
Nils Wallménius06ab6832016-05-02 12:46:15 -04001537static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001538 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1539 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1540 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1541#ifdef CONFIG_SWIOTLB
1542 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1543#endif
1544};
1545
1546static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1547 size_t size, loff_t *pos)
1548{
Al Viro45063092016-12-04 18:24:56 -05001549 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001550 ssize_t result = 0;
1551 int r;
1552
1553 if (size & 0x3 || *pos & 0x3)
1554 return -EINVAL;
1555
Tom St Denis9156e722017-05-23 11:35:22 -04001556 if (*pos >= adev->mc.mc_vram_size)
1557 return -ENXIO;
1558
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001559 while (size) {
1560 unsigned long flags;
1561 uint32_t value;
1562
1563 if (*pos >= adev->mc.mc_vram_size)
1564 return result;
1565
1566 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1567 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1568 WREG32(mmMM_INDEX_HI, *pos >> 31);
1569 value = RREG32(mmMM_DATA);
1570 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1571
1572 r = put_user(value, (uint32_t *)buf);
1573 if (r)
1574 return r;
1575
1576 result += 4;
1577 buf += 4;
1578 *pos += 4;
1579 size -= 4;
1580 }
1581
1582 return result;
1583}
1584
1585static const struct file_operations amdgpu_ttm_vram_fops = {
1586 .owner = THIS_MODULE,
1587 .read = amdgpu_ttm_vram_read,
1588 .llseek = default_llseek
1589};
1590
Christian Königa1d29472016-03-30 14:42:57 +02001591#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1592
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1594 size_t size, loff_t *pos)
1595{
Al Viro45063092016-12-04 18:24:56 -05001596 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001597 ssize_t result = 0;
1598 int r;
1599
1600 while (size) {
1601 loff_t p = *pos / PAGE_SIZE;
1602 unsigned off = *pos & ~PAGE_MASK;
1603 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1604 struct page *page;
1605 void *ptr;
1606
1607 if (p >= adev->gart.num_cpu_pages)
1608 return result;
1609
1610 page = adev->gart.pages[p];
1611 if (page) {
1612 ptr = kmap(page);
1613 ptr += off;
1614
1615 r = copy_to_user(buf, ptr, cur_size);
1616 kunmap(adev->gart.pages[p]);
1617 } else
1618 r = clear_user(buf, cur_size);
1619
1620 if (r)
1621 return -EFAULT;
1622
1623 result += cur_size;
1624 buf += cur_size;
1625 *pos += cur_size;
1626 size -= cur_size;
1627 }
1628
1629 return result;
1630}
1631
1632static const struct file_operations amdgpu_ttm_gtt_fops = {
1633 .owner = THIS_MODULE,
1634 .read = amdgpu_ttm_gtt_read,
1635 .llseek = default_llseek
1636};
1637
1638#endif
1639
Christian Königa1d29472016-03-30 14:42:57 +02001640#endif
1641
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1643{
1644#if defined(CONFIG_DEBUG_FS)
1645 unsigned count;
1646
1647 struct drm_minor *minor = adev->ddev->primary;
1648 struct dentry *ent, *root = minor->debugfs_root;
1649
1650 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1651 adev, &amdgpu_ttm_vram_fops);
1652 if (IS_ERR(ent))
1653 return PTR_ERR(ent);
1654 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1655 adev->mman.vram = ent;
1656
Christian Königa1d29472016-03-30 14:42:57 +02001657#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001658 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1659 adev, &amdgpu_ttm_gtt_fops);
1660 if (IS_ERR(ent))
1661 return PTR_ERR(ent);
Christian König6f02a692017-07-07 11:56:59 +02001662 i_size_write(ent->d_inode, adev->mc.gart_size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001663 adev->mman.gtt = ent;
1664
Christian Königa1d29472016-03-30 14:42:57 +02001665#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001666 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1667
1668#ifdef CONFIG_SWIOTLB
1669 if (!swiotlb_nr_tbl())
1670 --count;
1671#endif
1672
1673 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1674#else
1675
1676 return 0;
1677#endif
1678}
1679
1680static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1681{
1682#if defined(CONFIG_DEBUG_FS)
1683
1684 debugfs_remove(adev->mman.vram);
1685 adev->mman.vram = NULL;
1686
Christian Königa1d29472016-03-30 14:42:57 +02001687#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 debugfs_remove(adev->mman.gtt);
1689 adev->mman.gtt = NULL;
1690#endif
Christian Königa1d29472016-03-30 14:42:57 +02001691
1692#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001693}