blob: 9bbaffbd5f6aabc6088b857213f84353c5e7813a [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
John Brooks96cf8272017-06-30 11:31:08 -0400502 if (bo->type == ttm_bo_type_device &&
503 new_mem->mem_type == TTM_PL_VRAM &&
504 old_mem->mem_type != TTM_PL_VRAM) {
505 /* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
506 * accesses the BO after it's moved.
507 */
508 abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
509 }
510
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 /* update statistics */
512 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
513 return 0;
514}
515
516static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
517{
518 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
Christian Königa7d64de2016-09-15 14:58:48 +0200519 struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520
521 mem->bus.addr = NULL;
522 mem->bus.offset = 0;
523 mem->bus.size = mem->num_pages << PAGE_SHIFT;
524 mem->bus.base = 0;
525 mem->bus.is_iomem = false;
526 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
527 return -EINVAL;
528 switch (mem->mem_type) {
529 case TTM_PL_SYSTEM:
530 /* system memory */
531 return 0;
532 case TTM_PL_TT:
533 break;
534 case TTM_PL_VRAM:
535 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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 break;
542 default:
543 return -EINVAL;
544 }
545 return 0;
546}
547
548static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
549{
550}
551
Christian König9bbdcc02017-03-29 11:16:05 +0200552static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
553 unsigned long page_offset)
554{
555 struct drm_mm_node *mm = bo->mem.mm_node;
556 uint64_t size = mm->size;
Dave Airlie01687782017-04-07 05:41:42 +1000557 uint64_t offset = page_offset;
Christian König9bbdcc02017-03-29 11:16:05 +0200558
559 page_offset = do_div(offset, size);
Christian Königecdba5d2017-04-07 10:40:04 +0200560 mm += offset;
Christian König9bbdcc02017-03-29 11:16:05 +0200561 return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start + page_offset;
562}
563
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564/*
565 * TTM backend functions.
566 */
Christian König637dd3b2016-03-03 14:24:57 +0100567struct amdgpu_ttm_gup_task_list {
568 struct list_head list;
569 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570};
571
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100573 struct ttm_dma_tt ttm;
574 struct amdgpu_device *adev;
575 u64 offset;
576 uint64_t userptr;
577 struct mm_struct *usermm;
578 uint32_t userflags;
579 spinlock_t guptasklock;
580 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100581 atomic_t mmu_invalidations;
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800582 struct list_head list;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583};
584
Christian König2f568db2016-02-23 12:36:59 +0100585int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100588 unsigned int flags = 0;
Christian König2f568db2016-02-23 12:36:59 +0100589 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400590 int r;
591
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100592 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
593 flags |= FOLL_WRITE;
594
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100596 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597 to prevent problems with writeback */
598 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
599 struct vm_area_struct *vma;
600
601 vma = find_vma(gtt->usermm, gtt->userptr);
602 if (!vma || vma->vm_file || vma->vm_end < end)
603 return -EPERM;
604 }
605
606 do {
607 unsigned num_pages = ttm->num_pages - pinned;
608 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100609 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100610 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400611
Christian König637dd3b2016-03-03 14:24:57 +0100612 guptask.task = current;
613 spin_lock(&gtt->guptasklock);
614 list_add(&guptask.list, &gtt->guptasks);
615 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400616
Lorenzo Stoakes768ae302016-10-13 01:20:16 +0100617 r = get_user_pages(userptr, num_pages, flags, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100618
619 spin_lock(&gtt->guptasklock);
620 list_del(&guptask.list);
621 spin_unlock(&gtt->guptasklock);
622
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 if (r < 0)
624 goto release_pages;
625
626 pinned += r;
627
628 } while (pinned < ttm->num_pages);
629
Christian König2f568db2016-02-23 12:36:59 +0100630 return 0;
631
632release_pages:
633 release_pages(pages, pinned, 0);
634 return r;
635}
636
637/* prepare the sg table with the user pages */
638static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
639{
Christian Königa7d64de2016-09-15 14:58:48 +0200640 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Christian König2f568db2016-02-23 12:36:59 +0100641 struct amdgpu_ttm_tt *gtt = (void *)ttm;
642 unsigned nents;
643 int r;
644
645 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
646 enum dma_data_direction direction = write ?
647 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
648
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
650 ttm->num_pages << PAGE_SHIFT,
651 GFP_KERNEL);
652 if (r)
653 goto release_sg;
654
655 r = -ENOMEM;
656 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
657 if (nents != ttm->sg->nents)
658 goto release_sg;
659
660 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
661 gtt->ttm.dma_address, ttm->num_pages);
662
663 return 0;
664
665release_sg:
666 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 return r;
668}
669
670static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
671{
Christian Königa7d64de2016-09-15 14:58:48 +0200672 struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 struct amdgpu_ttm_tt *gtt = (void *)ttm;
monk.liudd08fae2015-05-07 14:19:18 -0400674 struct sg_page_iter sg_iter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675
676 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
677 enum dma_data_direction direction = write ?
678 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
679
680 /* double check that we don't free the table twice */
681 if (!ttm->sg->sgl)
682 return;
683
684 /* free the sg table and pages again */
685 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
686
monk.liudd08fae2015-05-07 14:19:18 -0400687 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
688 struct page *page = sg_page_iter_page(&sg_iter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
690 set_page_dirty(page);
691
692 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300693 put_page(page);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 }
695
696 sg_free_table(ttm->sg);
697}
698
Christian König98a7f882017-06-30 10:41:07 +0200699static int amdgpu_ttm_do_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem)
700{
701 struct amdgpu_ttm_tt *gtt = (void *)ttm;
702 uint64_t flags;
703 int r;
704
705 spin_lock(&gtt->adev->gtt_list_lock);
706 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, mem);
707 gtt->offset = (u64)mem->start << PAGE_SHIFT;
708 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
709 ttm->pages, gtt->ttm.dma_address, flags);
710
711 if (r) {
712 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
713 ttm->num_pages, gtt->offset);
714 goto error_gart_bind;
715 }
716
717 list_add_tail(&gtt->list, &gtt->adev->gtt_list);
718error_gart_bind:
719 spin_unlock(&gtt->adev->gtt_list_lock);
720 return r;
721
722}
723
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
725 struct ttm_mem_reg *bo_mem)
726{
727 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400728 int r;
729
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800730 if (gtt->userptr) {
731 r = amdgpu_ttm_tt_pin_userptr(ttm);
732 if (r) {
733 DRM_ERROR("failed to pin userptr\n");
734 return r;
735 }
736 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 if (!ttm->num_pages) {
738 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
739 ttm->num_pages, bo_mem, ttm);
740 }
741
742 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
743 bo_mem->mem_type == AMDGPU_PL_GWS ||
744 bo_mem->mem_type == AMDGPU_PL_OA)
745 return -EINVAL;
746
Christian König98a7f882017-06-30 10:41:07 +0200747 if (amdgpu_gtt_mgr_is_allocated(bo_mem))
748 r = amdgpu_ttm_do_bind(ttm, bo_mem);
749
750 return r;
Christian Königc855e252016-09-05 17:00:57 +0200751}
752
753bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
754{
755 struct amdgpu_ttm_tt *gtt = (void *)ttm;
756
757 return gtt && !list_empty(&gtt->list);
758}
759
Christian Königbb990bb2016-09-09 16:32:33 +0200760int amdgpu_ttm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *bo_mem)
Christian Königc855e252016-09-05 17:00:57 +0200761{
Christian Königbb990bb2016-09-09 16:32:33 +0200762 struct ttm_tt *ttm = bo->ttm;
Christian Königc855e252016-09-05 17:00:57 +0200763 int r;
764
765 if (!ttm || amdgpu_ttm_is_bound(ttm))
766 return 0;
767
Christian Königbb990bb2016-09-09 16:32:33 +0200768 r = amdgpu_gtt_mgr_alloc(&bo->bdev->man[TTM_PL_TT], bo,
769 NULL, bo_mem);
770 if (r) {
771 DRM_ERROR("Failed to allocate GTT address space (%d)\n", r);
772 return r;
773 }
774
Christian König98a7f882017-06-30 10:41:07 +0200775 return amdgpu_ttm_do_bind(ttm, bo_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776}
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;
Monk Liu1d1a2cd2017-04-27 17:14:57 +0800782 uint64_t flags;
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800783 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;
Roger.He738f64c2017-05-05 13:27:10 +0800806 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400807
Christian König85a4b572016-09-22 14:19:50 +0200808 if (gtt->userptr)
809 amdgpu_ttm_tt_unpin_userptr(ttm);
810
Christian König78ab0a32016-09-09 15:39:08 +0200811 if (!amdgpu_ttm_is_bound(ttm))
812 return 0;
813
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800815 spin_lock(&gtt->adev->gtt_list_lock);
Roger.He738f64c2017-05-05 13:27:10 +0800816 r = amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
817 if (r) {
818 DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
819 gtt->ttm.ttm.num_pages, gtt->offset);
820 goto error_unbind;
821 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800822 list_del_init(&gtt->list);
Roger.He738f64c2017-05-05 13:27:10 +0800823error_unbind:
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800824 spin_unlock(&gtt->adev->gtt_list_lock);
Roger.He738f64c2017-05-05 13:27:10 +0800825 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400826}
827
828static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
829{
830 struct amdgpu_ttm_tt *gtt = (void *)ttm;
831
832 ttm_dma_tt_fini(&gtt->ttm);
833 kfree(gtt);
834}
835
836static struct ttm_backend_func amdgpu_backend_func = {
837 .bind = &amdgpu_ttm_backend_bind,
838 .unbind = &amdgpu_ttm_backend_unbind,
839 .destroy = &amdgpu_ttm_backend_destroy,
840};
841
842static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
843 unsigned long size, uint32_t page_flags,
844 struct page *dummy_read_page)
845{
846 struct amdgpu_device *adev;
847 struct amdgpu_ttm_tt *gtt;
848
Christian Königa7d64de2016-09-15 14:58:48 +0200849 adev = amdgpu_ttm_adev(bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400850
851 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
852 if (gtt == NULL) {
853 return NULL;
854 }
855 gtt->ttm.ttm.func = &amdgpu_backend_func;
856 gtt->adev = adev;
857 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
858 kfree(gtt);
859 return NULL;
860 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800861 INIT_LIST_HEAD(&gtt->list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400862 return &gtt->ttm.ttm;
863}
864
865static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
866{
867 struct amdgpu_device *adev;
868 struct amdgpu_ttm_tt *gtt = (void *)ttm;
869 unsigned i;
870 int r;
871 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
872
873 if (ttm->state != tt_unpopulated)
874 return 0;
875
876 if (gtt && gtt->userptr) {
Maninder Singh5f0b34c2015-06-26 13:28:50 +0530877 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400878 if (!ttm->sg)
879 return -ENOMEM;
880
881 ttm->page_flags |= TTM_PAGE_FLAG_SG;
882 ttm->state = tt_unbound;
883 return 0;
884 }
885
886 if (slave && ttm->sg) {
887 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
888 gtt->ttm.dma_address, ttm->num_pages);
889 ttm->state = tt_unbound;
890 return 0;
891 }
892
Christian Königa7d64de2016-09-15 14:58:48 +0200893 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400894
895#ifdef CONFIG_SWIOTLB
896 if (swiotlb_nr_tbl()) {
897 return ttm_dma_populate(&gtt->ttm, adev->dev);
898 }
899#endif
900
901 r = ttm_pool_populate(ttm);
902 if (r) {
903 return r;
904 }
905
906 for (i = 0; i < ttm->num_pages; i++) {
907 gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
908 0, PAGE_SIZE,
909 PCI_DMA_BIDIRECTIONAL);
910 if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
Rasmus Villemoes09ccbb72016-02-15 19:41:45 +0100911 while (i--) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400912 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
913 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
914 gtt->ttm.dma_address[i] = 0;
915 }
916 ttm_pool_unpopulate(ttm);
917 return -EFAULT;
918 }
919 }
920 return 0;
921}
922
923static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
924{
925 struct amdgpu_device *adev;
926 struct amdgpu_ttm_tt *gtt = (void *)ttm;
927 unsigned i;
928 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
929
930 if (gtt && gtt->userptr) {
931 kfree(ttm->sg);
932 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
933 return;
934 }
935
936 if (slave)
937 return;
938
Christian Königa7d64de2016-09-15 14:58:48 +0200939 adev = amdgpu_ttm_adev(ttm->bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940
941#ifdef CONFIG_SWIOTLB
942 if (swiotlb_nr_tbl()) {
943 ttm_dma_unpopulate(&gtt->ttm, adev->dev);
944 return;
945 }
946#endif
947
948 for (i = 0; i < ttm->num_pages; i++) {
949 if (gtt->ttm.dma_address[i]) {
950 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
951 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
952 }
953 }
954
955 ttm_pool_unpopulate(ttm);
956}
957
958int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
959 uint32_t flags)
960{
961 struct amdgpu_ttm_tt *gtt = (void *)ttm;
962
963 if (gtt == NULL)
964 return -EINVAL;
965
966 gtt->userptr = addr;
967 gtt->usermm = current->mm;
968 gtt->userflags = flags;
Christian König637dd3b2016-03-03 14:24:57 +0100969 spin_lock_init(&gtt->guptasklock);
970 INIT_LIST_HEAD(&gtt->guptasks);
Christian König2f568db2016-02-23 12:36:59 +0100971 atomic_set(&gtt->mmu_invalidations, 0);
Christian König637dd3b2016-03-03 14:24:57 +0100972
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973 return 0;
974}
975
Christian Königcc325d12016-02-08 11:08:35 +0100976struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400977{
978 struct amdgpu_ttm_tt *gtt = (void *)ttm;
979
980 if (gtt == NULL)
Christian Königcc325d12016-02-08 11:08:35 +0100981 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982
Christian Königcc325d12016-02-08 11:08:35 +0100983 return gtt->usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984}
985
Christian Königcc1de6e2016-02-08 10:57:22 +0100986bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
987 unsigned long end)
988{
989 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König637dd3b2016-03-03 14:24:57 +0100990 struct amdgpu_ttm_gup_task_list *entry;
Christian Königcc1de6e2016-02-08 10:57:22 +0100991 unsigned long size;
992
Christian König637dd3b2016-03-03 14:24:57 +0100993 if (gtt == NULL || !gtt->userptr)
Christian Königcc1de6e2016-02-08 10:57:22 +0100994 return false;
995
996 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
997 if (gtt->userptr > end || gtt->userptr + size <= start)
998 return false;
999
Christian König637dd3b2016-03-03 14:24:57 +01001000 spin_lock(&gtt->guptasklock);
1001 list_for_each_entry(entry, &gtt->guptasks, list) {
1002 if (entry->task == current) {
1003 spin_unlock(&gtt->guptasklock);
1004 return false;
1005 }
1006 }
1007 spin_unlock(&gtt->guptasklock);
1008
Christian König2f568db2016-02-23 12:36:59 +01001009 atomic_inc(&gtt->mmu_invalidations);
1010
Christian Königcc1de6e2016-02-08 10:57:22 +01001011 return true;
1012}
1013
Christian König2f568db2016-02-23 12:36:59 +01001014bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1015 int *last_invalidated)
1016{
1017 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1018 int prev_invalidated = *last_invalidated;
1019
1020 *last_invalidated = atomic_read(&gtt->mmu_invalidations);
1021 return prev_invalidated != *last_invalidated;
1022}
1023
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001024bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1025{
1026 struct amdgpu_ttm_tt *gtt = (void *)ttm;
1027
1028 if (gtt == NULL)
1029 return false;
1030
1031 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1032}
1033
Chunming Zhou6b777602016-09-21 16:19:19 +08001034uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001035 struct ttm_mem_reg *mem)
1036{
Chunming Zhou6b777602016-09-21 16:19:19 +08001037 uint64_t flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001038
1039 if (mem && mem->mem_type != TTM_PL_SYSTEM)
1040 flags |= AMDGPU_PTE_VALID;
1041
Christian König6d999052015-12-04 13:32:55 +01001042 if (mem && mem->mem_type == TTM_PL_TT) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001043 flags |= AMDGPU_PTE_SYSTEM;
1044
Christian König6d999052015-12-04 13:32:55 +01001045 if (ttm->caching_state == tt_cached)
1046 flags |= AMDGPU_PTE_SNOOPED;
1047 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001048
Alex Xie4b98e0c2017-02-14 12:31:36 -05001049 flags |= adev->gart.gart_pte_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050 flags |= AMDGPU_PTE_READABLE;
1051
1052 if (!amdgpu_ttm_tt_is_readonly(ttm))
1053 flags |= AMDGPU_PTE_WRITEABLE;
1054
1055 return flags;
1056}
1057
Christian König9982ca62016-10-19 14:44:22 +02001058static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1059 const struct ttm_place *place)
1060{
Christian König4fcae782017-04-20 12:11:47 +02001061 unsigned long num_pages = bo->mem.num_pages;
1062 struct drm_mm_node *node = bo->mem.mm_node;
Christian König9982ca62016-10-19 14:44:22 +02001063
Christian König4fcae782017-04-20 12:11:47 +02001064 if (bo->mem.start != AMDGPU_BO_INVALID_OFFSET)
1065 return ttm_bo_eviction_valuable(bo, place);
1066
1067 switch (bo->mem.mem_type) {
1068 case TTM_PL_TT:
1069 return true;
1070
1071 case TTM_PL_VRAM:
Christian König9982ca62016-10-19 14:44:22 +02001072 /* Check each drm MM node individually */
1073 while (num_pages) {
1074 if (place->fpfn < (node->start + node->size) &&
1075 !(place->lpfn && place->lpfn <= node->start))
1076 return true;
1077
1078 num_pages -= node->size;
1079 ++node;
1080 }
Christian König4fcae782017-04-20 12:11:47 +02001081 break;
Christian König9982ca62016-10-19 14:44:22 +02001082
Christian König4fcae782017-04-20 12:11:47 +02001083 default:
1084 break;
Christian König9982ca62016-10-19 14:44:22 +02001085 }
1086
1087 return ttm_bo_eviction_valuable(bo, place);
1088}
1089
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090static struct ttm_bo_driver amdgpu_bo_driver = {
1091 .ttm_tt_create = &amdgpu_ttm_tt_create,
1092 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1093 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1094 .invalidate_caches = &amdgpu_invalidate_caches,
1095 .init_mem_type = &amdgpu_init_mem_type,
Christian König9982ca62016-10-19 14:44:22 +02001096 .eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097 .evict_flags = &amdgpu_evict_flags,
1098 .move = &amdgpu_bo_move,
1099 .verify_access = &amdgpu_verify_access,
1100 .move_notify = &amdgpu_bo_move_notify,
1101 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1102 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1103 .io_mem_free = &amdgpu_ttm_io_mem_free,
Christian König9bbdcc02017-03-29 11:16:05 +02001104 .io_mem_pfn = amdgpu_ttm_io_mem_pfn,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001105};
1106
1107int amdgpu_ttm_init(struct amdgpu_device *adev)
1108{
Christian König36d38372017-07-07 13:17:45 +02001109 uint64_t gtt_size;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001110 int r;
John Brooks218b5dc2017-06-27 22:33:17 -04001111 u64 vis_vram_limit;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001112
Alex Deucher70b5c5a2016-11-15 16:55:53 -05001113 r = amdgpu_ttm_global_init(adev);
1114 if (r) {
1115 return r;
1116 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117 /* No others user of address space so set it to 0 */
1118 r = ttm_bo_device_init(&adev->mman.bdev,
1119 adev->mman.bo_global_ref.ref.object,
1120 &amdgpu_bo_driver,
1121 adev->ddev->anon_inode->i_mapping,
1122 DRM_FILE_PAGE_OFFSET,
1123 adev->need_dma32);
1124 if (r) {
1125 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1126 return r;
1127 }
1128 adev->mman.initialized = true;
1129 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1130 adev->mc.real_vram_size >> PAGE_SHIFT);
1131 if (r) {
1132 DRM_ERROR("Failed initializing VRAM heap.\n");
1133 return r;
1134 }
John Brooks218b5dc2017-06-27 22:33:17 -04001135
1136 /* Reduce size of CPU-visible VRAM if requested */
1137 vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1138 if (amdgpu_vis_vram_limit > 0 &&
1139 vis_vram_limit <= adev->mc.visible_vram_size)
1140 adev->mc.visible_vram_size = vis_vram_limit;
1141
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001142 /* Change the size here instead of the init above so only lpfn is affected */
1143 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1144
Huang Rui916910a2017-05-31 10:35:42 +08001145 r = amdgpu_bo_create(adev, adev->mc.stolen_size, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001146 AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +02001147 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1148 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +02001149 NULL, NULL, &adev->stollen_vga_memory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150 if (r) {
1151 return r;
1152 }
1153 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1154 if (r)
1155 return r;
1156 r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
1157 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1158 if (r) {
1159 amdgpu_bo_unref(&adev->stollen_vga_memory);
1160 return r;
1161 }
1162 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1163 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
Christian König36d38372017-07-07 13:17:45 +02001164
1165 if (amdgpu_gtt_size == -1)
1166 gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1167 adev->mc.mc_vram_size);
1168 else
1169 gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1170 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171 if (r) {
1172 DRM_ERROR("Failed initializing GTT heap.\n");
1173 return r;
1174 }
1175 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
Christian König36d38372017-07-07 13:17:45 +02001176 (unsigned)(gtt_size / (1024 * 1024)));
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177
1178 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1179 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1180 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1181 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1182 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1183 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1184 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1185 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1186 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1187 /* GDS Memory */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001188 if (adev->gds.mem.total_size) {
1189 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1190 adev->gds.mem.total_size >> PAGE_SHIFT);
1191 if (r) {
1192 DRM_ERROR("Failed initializing GDS heap.\n");
1193 return r;
1194 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 }
1196
1197 /* GWS */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001198 if (adev->gds.gws.total_size) {
1199 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1200 adev->gds.gws.total_size >> PAGE_SHIFT);
1201 if (r) {
1202 DRM_ERROR("Failed initializing gws heap.\n");
1203 return r;
1204 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001205 }
1206
1207 /* OA */
Alex Deucherd2d51d82017-03-15 09:45:48 -04001208 if (adev->gds.oa.total_size) {
1209 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1210 adev->gds.oa.total_size >> PAGE_SHIFT);
1211 if (r) {
1212 DRM_ERROR("Failed initializing oa heap.\n");
1213 return r;
1214 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215 }
1216
1217 r = amdgpu_ttm_debugfs_init(adev);
1218 if (r) {
1219 DRM_ERROR("Failed to init debugfs\n");
1220 return r;
1221 }
1222 return 0;
1223}
1224
1225void amdgpu_ttm_fini(struct amdgpu_device *adev)
1226{
1227 int r;
1228
1229 if (!adev->mman.initialized)
1230 return;
1231 amdgpu_ttm_debugfs_fini(adev);
1232 if (adev->stollen_vga_memory) {
Michel Dänzerc81a1a72017-04-28 17:28:14 +09001233 r = amdgpu_bo_reserve(adev->stollen_vga_memory, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001234 if (r == 0) {
1235 amdgpu_bo_unpin(adev->stollen_vga_memory);
1236 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1237 }
1238 amdgpu_bo_unref(&adev->stollen_vga_memory);
1239 }
1240 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1241 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
Alex Deucherd2d51d82017-03-15 09:45:48 -04001242 if (adev->gds.mem.total_size)
1243 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1244 if (adev->gds.gws.total_size)
1245 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1246 if (adev->gds.oa.total_size)
1247 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001248 ttm_bo_device_release(&adev->mman.bdev);
1249 amdgpu_gart_fini(adev);
1250 amdgpu_ttm_global_fini(adev);
1251 adev->mman.initialized = false;
1252 DRM_INFO("amdgpu: ttm finalized\n");
1253}
1254
1255/* this should only be called at bootup or when userspace
1256 * isn't running */
1257void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1258{
1259 struct ttm_mem_type_manager *man;
1260
1261 if (!adev->mman.initialized)
1262 return;
1263
1264 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1265 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1266 man->size = size >> PAGE_SHIFT;
1267}
1268
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001269int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1270{
1271 struct drm_file *file_priv;
1272 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001273
Christian Könige176fe172015-05-27 10:22:47 +02001274 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001275 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276
1277 file_priv = filp->private_data;
1278 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001279 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001281
1282 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283}
1284
Christian Königabca90f2017-06-30 11:05:54 +02001285static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
1286 struct ttm_mem_reg *mem, unsigned num_pages,
1287 uint64_t offset, unsigned window,
1288 struct amdgpu_ring *ring,
1289 uint64_t *addr)
1290{
1291 struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
1292 struct amdgpu_device *adev = ring->adev;
1293 struct ttm_tt *ttm = bo->ttm;
1294 struct amdgpu_job *job;
1295 unsigned num_dw, num_bytes;
1296 dma_addr_t *dma_address;
1297 struct dma_fence *fence;
1298 uint64_t src_addr, dst_addr;
1299 uint64_t flags;
1300 int r;
1301
1302 BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
1303 AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
1304
Christian König6f02a692017-07-07 11:56:59 +02001305 *addr = adev->mc.gart_start;
Christian Königabca90f2017-06-30 11:05:54 +02001306 *addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
1307 AMDGPU_GPU_PAGE_SIZE;
1308
1309 num_dw = adev->mman.buffer_funcs->copy_num_dw;
1310 while (num_dw & 0x7)
1311 num_dw++;
1312
1313 num_bytes = num_pages * 8;
1314
1315 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job);
1316 if (r)
1317 return r;
1318
1319 src_addr = num_dw * 4;
1320 src_addr += job->ibs[0].gpu_addr;
1321
1322 dst_addr = adev->gart.table_addr;
1323 dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
1324 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
1325 dst_addr, num_bytes);
1326
1327 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1328 WARN_ON(job->ibs[0].length_dw > num_dw);
1329
1330 dma_address = &gtt->ttm.dma_address[offset >> PAGE_SHIFT];
1331 flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem);
1332 r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
1333 &job->ibs[0].ptr[num_dw]);
1334 if (r)
1335 goto error_free;
1336
1337 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1338 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
1339 if (r)
1340 goto error_free;
1341
1342 dma_fence_put(fence);
1343
1344 return r;
1345
1346error_free:
1347 amdgpu_job_free(job);
1348 return r;
1349}
1350
Christian Königfc9c8f52017-06-29 11:46:15 +02001351int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1352 uint64_t dst_offset, uint32_t byte_count,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001353 struct reservation_object *resv,
Christian Königfc9c8f52017-06-29 11:46:15 +02001354 struct dma_fence **fence, bool direct_submit,
1355 bool vm_needs_flush)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356{
1357 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001358 struct amdgpu_job *job;
1359
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001360 uint32_t max_bytes;
1361 unsigned num_loops, num_dw;
1362 unsigned i;
1363 int r;
1364
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001365 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1366 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1367 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1368
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001369 /* for IB padding */
1370 while (num_dw & 0x7)
1371 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372
Christian Königd71518b2016-02-01 12:20:25 +01001373 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1374 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001375 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001376
Christian Königfc9c8f52017-06-29 11:46:15 +02001377 job->vm_needs_flush = vm_needs_flush;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001378 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001379 r = amdgpu_sync_resv(adev, &job->sync, resv,
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001380 AMDGPU_FENCE_OWNER_UNDEFINED);
1381 if (r) {
1382 DRM_ERROR("sync failed (%d).\n", r);
1383 goto error_free;
1384 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386
1387 for (i = 0; i < num_loops; i++) {
1388 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1389
Christian Königd71518b2016-02-01 12:20:25 +01001390 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1391 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392
1393 src_offset += cur_size_in_bytes;
1394 dst_offset += cur_size_in_bytes;
1395 byte_count -= cur_size_in_bytes;
1396 }
1397
Christian Königd71518b2016-02-01 12:20:25 +01001398 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1399 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001400 if (direct_submit) {
1401 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
Junwei Zhang50ddc752017-01-23 16:30:38 +08001402 NULL, fence);
Chris Wilsonf54d1862016-10-25 13:00:45 +01001403 job->fence = dma_fence_get(*fence);
Chunming Zhoue24db982016-08-15 10:46:04 +08001404 if (r)
1405 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1406 amdgpu_job_free(job);
1407 } else {
1408 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1409 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1410 if (r)
1411 goto error_free;
1412 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001413
Chunming Zhoue24db982016-08-15 10:46:04 +08001414 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001415
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001416error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001417 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001418 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419}
1420
Flora Cui59b4a972016-07-19 16:48:22 +08001421int amdgpu_fill_buffer(struct amdgpu_bo *bo,
Christian Königf29224a62016-11-17 12:06:38 +01001422 uint32_t src_data,
1423 struct reservation_object *resv,
1424 struct dma_fence **fence)
Flora Cui59b4a972016-07-19 16:48:22 +08001425{
Christian Königa7d64de2016-09-15 14:58:48 +02001426 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Christian Königf29224a62016-11-17 12:06:38 +01001427 uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
Flora Cui59b4a972016-07-19 16:48:22 +08001428 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1429
Christian Königf29224a62016-11-17 12:06:38 +01001430 struct drm_mm_node *mm_node;
1431 unsigned long num_pages;
Flora Cui59b4a972016-07-19 16:48:22 +08001432 unsigned int num_loops, num_dw;
Christian Königf29224a62016-11-17 12:06:38 +01001433
1434 struct amdgpu_job *job;
Flora Cui59b4a972016-07-19 16:48:22 +08001435 int r;
1436
Christian Königf29224a62016-11-17 12:06:38 +01001437 if (!ring->ready) {
1438 DRM_ERROR("Trying to clear memory with ring turned off.\n");
1439 return -EINVAL;
1440 }
1441
Christian König92c60d92017-06-29 10:44:39 +02001442 if (bo->tbo.mem.mem_type == TTM_PL_TT) {
1443 r = amdgpu_ttm_bind(&bo->tbo, &bo->tbo.mem);
1444 if (r)
1445 return r;
1446 }
1447
Christian Königf29224a62016-11-17 12:06:38 +01001448 num_pages = bo->tbo.num_pages;
1449 mm_node = bo->tbo.mem.mm_node;
1450 num_loops = 0;
1451 while (num_pages) {
1452 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1453
1454 num_loops += DIV_ROUND_UP(byte_count, max_bytes);
1455 num_pages -= mm_node->size;
1456 ++mm_node;
1457 }
Flora Cui59b4a972016-07-19 16:48:22 +08001458 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
1459
1460 /* for IB padding */
Christian Königf29224a62016-11-17 12:06:38 +01001461 num_dw += 64;
Flora Cui59b4a972016-07-19 16:48:22 +08001462
1463 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1464 if (r)
1465 return r;
1466
1467 if (resv) {
1468 r = amdgpu_sync_resv(adev, &job->sync, resv,
Christian Königf29224a62016-11-17 12:06:38 +01001469 AMDGPU_FENCE_OWNER_UNDEFINED);
Flora Cui59b4a972016-07-19 16:48:22 +08001470 if (r) {
1471 DRM_ERROR("sync failed (%d).\n", r);
1472 goto error_free;
1473 }
1474 }
1475
Christian Königf29224a62016-11-17 12:06:38 +01001476 num_pages = bo->tbo.num_pages;
1477 mm_node = bo->tbo.mem.mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001478
Christian Königf29224a62016-11-17 12:06:38 +01001479 while (num_pages) {
1480 uint32_t byte_count = mm_node->size << PAGE_SHIFT;
1481 uint64_t dst_addr;
Flora Cui59b4a972016-07-19 16:48:22 +08001482
Christian König92c60d92017-06-29 10:44:39 +02001483 dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
Christian Königf29224a62016-11-17 12:06:38 +01001484 while (byte_count) {
1485 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1486
1487 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
1488 dst_addr, cur_size_in_bytes);
1489
1490 dst_addr += cur_size_in_bytes;
1491 byte_count -= cur_size_in_bytes;
1492 }
1493
1494 num_pages -= mm_node->size;
1495 ++mm_node;
Flora Cui59b4a972016-07-19 16:48:22 +08001496 }
1497
1498 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1499 WARN_ON(job->ibs[0].length_dw > num_dw);
1500 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
Christian Königf29224a62016-11-17 12:06:38 +01001501 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
Flora Cui59b4a972016-07-19 16:48:22 +08001502 if (r)
1503 goto error_free;
1504
1505 return 0;
1506
1507error_free:
1508 amdgpu_job_free(job);
1509 return r;
1510}
1511
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001512#if defined(CONFIG_DEBUG_FS)
1513
Chunming Zhou05a72a22017-04-13 16:16:51 +08001514extern void amdgpu_gtt_mgr_print(struct seq_file *m, struct ttm_mem_type_manager
1515 *man);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001516static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1517{
1518 struct drm_info_node *node = (struct drm_info_node *)m->private;
1519 unsigned ttm_pl = *(int *)node->info_ent->data;
1520 struct drm_device *dev = node->minor->dev;
1521 struct amdgpu_device *adev = dev->dev_private;
1522 struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 struct ttm_bo_global *glob = adev->mman.bdev.glob;
Daniel Vetterb5c37142016-12-29 12:09:24 +01001524 struct drm_printer p = drm_seq_file_printer(m);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525
1526 spin_lock(&glob->lru_lock);
Daniel Vetterb5c37142016-12-29 12:09:24 +01001527 drm_mm_print(mm, &p);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001528 spin_unlock(&glob->lru_lock);
Chunming Zhou05a72a22017-04-13 16:16:51 +08001529 switch (ttm_pl) {
1530 case TTM_PL_VRAM:
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001531 seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001532 adev->mman.bdev.man[ttm_pl].size,
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001533 (u64)atomic64_read(&adev->vram_usage) >> 20,
1534 (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
Chunming Zhou05a72a22017-04-13 16:16:51 +08001535 break;
1536 case TTM_PL_TT:
1537 amdgpu_gtt_mgr_print(m, &adev->mman.bdev.man[TTM_PL_TT]);
1538 break;
1539 }
Daniel Vetterb5c37142016-12-29 12:09:24 +01001540 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541}
1542
1543static int ttm_pl_vram = TTM_PL_VRAM;
1544static int ttm_pl_tt = TTM_PL_TT;
1545
Nils Wallménius06ab6832016-05-02 12:46:15 -04001546static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001547 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1548 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1549 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1550#ifdef CONFIG_SWIOTLB
1551 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1552#endif
1553};
1554
1555static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1556 size_t size, loff_t *pos)
1557{
Al Viro45063092016-12-04 18:24:56 -05001558 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001559 ssize_t result = 0;
1560 int r;
1561
1562 if (size & 0x3 || *pos & 0x3)
1563 return -EINVAL;
1564
Tom St Denis9156e722017-05-23 11:35:22 -04001565 if (*pos >= adev->mc.mc_vram_size)
1566 return -ENXIO;
1567
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001568 while (size) {
1569 unsigned long flags;
1570 uint32_t value;
1571
1572 if (*pos >= adev->mc.mc_vram_size)
1573 return result;
1574
1575 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1576 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1577 WREG32(mmMM_INDEX_HI, *pos >> 31);
1578 value = RREG32(mmMM_DATA);
1579 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1580
1581 r = put_user(value, (uint32_t *)buf);
1582 if (r)
1583 return r;
1584
1585 result += 4;
1586 buf += 4;
1587 *pos += 4;
1588 size -= 4;
1589 }
1590
1591 return result;
1592}
1593
1594static const struct file_operations amdgpu_ttm_vram_fops = {
1595 .owner = THIS_MODULE,
1596 .read = amdgpu_ttm_vram_read,
1597 .llseek = default_llseek
1598};
1599
Christian Königa1d29472016-03-30 14:42:57 +02001600#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1601
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1603 size_t size, loff_t *pos)
1604{
Al Viro45063092016-12-04 18:24:56 -05001605 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606 ssize_t result = 0;
1607 int r;
1608
1609 while (size) {
1610 loff_t p = *pos / PAGE_SIZE;
1611 unsigned off = *pos & ~PAGE_MASK;
1612 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1613 struct page *page;
1614 void *ptr;
1615
1616 if (p >= adev->gart.num_cpu_pages)
1617 return result;
1618
1619 page = adev->gart.pages[p];
1620 if (page) {
1621 ptr = kmap(page);
1622 ptr += off;
1623
1624 r = copy_to_user(buf, ptr, cur_size);
1625 kunmap(adev->gart.pages[p]);
1626 } else
1627 r = clear_user(buf, cur_size);
1628
1629 if (r)
1630 return -EFAULT;
1631
1632 result += cur_size;
1633 buf += cur_size;
1634 *pos += cur_size;
1635 size -= cur_size;
1636 }
1637
1638 return result;
1639}
1640
1641static const struct file_operations amdgpu_ttm_gtt_fops = {
1642 .owner = THIS_MODULE,
1643 .read = amdgpu_ttm_gtt_read,
1644 .llseek = default_llseek
1645};
1646
1647#endif
1648
Christian Königa1d29472016-03-30 14:42:57 +02001649#endif
1650
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001651static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1652{
1653#if defined(CONFIG_DEBUG_FS)
1654 unsigned count;
1655
1656 struct drm_minor *minor = adev->ddev->primary;
1657 struct dentry *ent, *root = minor->debugfs_root;
1658
1659 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1660 adev, &amdgpu_ttm_vram_fops);
1661 if (IS_ERR(ent))
1662 return PTR_ERR(ent);
1663 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1664 adev->mman.vram = ent;
1665
Christian Königa1d29472016-03-30 14:42:57 +02001666#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1668 adev, &amdgpu_ttm_gtt_fops);
1669 if (IS_ERR(ent))
1670 return PTR_ERR(ent);
Christian König6f02a692017-07-07 11:56:59 +02001671 i_size_write(ent->d_inode, adev->mc.gart_size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 adev->mman.gtt = ent;
1673
Christian Königa1d29472016-03-30 14:42:57 +02001674#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1676
1677#ifdef CONFIG_SWIOTLB
1678 if (!swiotlb_nr_tbl())
1679 --count;
1680#endif
1681
1682 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1683#else
1684
1685 return 0;
1686#endif
1687}
1688
1689static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1690{
1691#if defined(CONFIG_DEBUG_FS)
1692
1693 debugfs_remove(adev->mman.vram);
1694 adev->mman.vram = NULL;
1695
Christian Königa1d29472016-03-30 14:42:57 +02001696#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001697 debugfs_remove(adev->mman.gtt);
1698 adev->mman.gtt = NULL;
1699#endif
Christian Königa1d29472016-03-30 14:42:57 +02001700
1701#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001702}