blob: 8aa3fe6c270c49ece0c4dcadac21bb62bdd9f36a [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2009 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26/*
27 * Authors:
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
30 * Dave Airlie
31 */
32#include <ttm/ttm_bo_api.h>
33#include <ttm/ttm_bo_driver.h>
34#include <ttm/ttm_placement.h>
35#include <ttm/ttm_module.h>
36#include <ttm/ttm_page_alloc.h>
Ken Wanga693e052016-07-27 19:18:01 +080037#include <ttm/ttm_memory.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040038#include <drm/drmP.h>
39#include <drm/amdgpu_drm.h>
40#include <linux/seq_file.h>
41#include <linux/slab.h>
42#include <linux/swiotlb.h>
43#include <linux/swap.h>
44#include <linux/pagemap.h>
45#include <linux/debugfs.h>
46#include "amdgpu.h"
47#include "bif/bif_4_1_d.h"
48
49#define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
50
51static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
52static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
53
54static struct amdgpu_device *amdgpu_get_adev(struct ttm_bo_device *bdev)
55{
56 struct amdgpu_mman *mman;
57 struct amdgpu_device *adev;
58
59 mman = container_of(bdev, struct amdgpu_mman, bdev);
60 adev = container_of(mman, struct amdgpu_device, mman);
61 return adev;
62}
63
64
65/*
66 * Global memory.
67 */
68static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
69{
70 return ttm_mem_global_init(ref->object);
71}
72
73static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
74{
75 ttm_mem_global_release(ref->object);
76}
77
Ken Wanga693e052016-07-27 19:18:01 +080078int amdgpu_ttm_global_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079{
80 struct drm_global_reference *global_ref;
Christian König703297c2016-02-10 14:20:50 +010081 struct amdgpu_ring *ring;
82 struct amd_sched_rq *rq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 int r;
84
85 adev->mman.mem_global_referenced = false;
86 global_ref = &adev->mman.mem_global_ref;
87 global_ref->global_type = DRM_GLOBAL_TTM_MEM;
88 global_ref->size = sizeof(struct ttm_mem_global);
89 global_ref->init = &amdgpu_ttm_mem_global_init;
90 global_ref->release = &amdgpu_ttm_mem_global_release;
91 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +080092 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093 DRM_ERROR("Failed setting up TTM memory accounting "
94 "subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +080095 goto error_mem;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 }
97
98 adev->mman.bo_global_ref.mem_glob =
99 adev->mman.mem_global_ref.object;
100 global_ref = &adev->mman.bo_global_ref.ref;
101 global_ref->global_type = DRM_GLOBAL_TTM_BO;
102 global_ref->size = sizeof(struct ttm_bo_global);
103 global_ref->init = &ttm_bo_global_init;
104 global_ref->release = &ttm_bo_global_release;
105 r = drm_global_item_ref(global_ref);
Huang Ruie9d035e2016-09-07 20:55:42 +0800106 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107 DRM_ERROR("Failed setting up TTM BO subsystem.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800108 goto error_bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 }
110
Christian König703297c2016-02-10 14:20:50 +0100111 ring = adev->mman.buffer_funcs_ring;
112 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
113 r = amd_sched_entity_init(&ring->sched, &adev->mman.entity,
114 rq, amdgpu_sched_jobs);
Huang Ruie9d035e2016-09-07 20:55:42 +0800115 if (r) {
Christian König703297c2016-02-10 14:20:50 +0100116 DRM_ERROR("Failed setting up TTM BO move run queue.\n");
Huang Ruie9d035e2016-09-07 20:55:42 +0800117 goto error_entity;
Christian König703297c2016-02-10 14:20:50 +0100118 }
119
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 adev->mman.mem_global_referenced = true;
Christian König703297c2016-02-10 14:20:50 +0100121
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 return 0;
Huang Ruie9d035e2016-09-07 20:55:42 +0800123
124error_entity:
125 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
126error_bo:
127 drm_global_item_unref(&adev->mman.mem_global_ref);
128error_mem:
129 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130}
131
132static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
133{
134 if (adev->mman.mem_global_referenced) {
Christian König703297c2016-02-10 14:20:50 +0100135 amd_sched_entity_fini(adev->mman.entity.sched,
136 &adev->mman.entity);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 drm_global_item_unref(&adev->mman.bo_global_ref.ref);
138 drm_global_item_unref(&adev->mman.mem_global_ref);
139 adev->mman.mem_global_referenced = false;
140 }
141}
142
143static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
144{
145 return 0;
146}
147
148static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
149 struct ttm_mem_type_manager *man)
150{
151 struct amdgpu_device *adev;
152
153 adev = amdgpu_get_adev(bdev);
154
155 switch (type) {
156 case TTM_PL_SYSTEM:
157 /* System memory */
158 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
159 man->available_caching = TTM_PL_MASK_CACHING;
160 man->default_caching = TTM_PL_FLAG_CACHED;
161 break;
162 case TTM_PL_TT:
163 man->func = &ttm_bo_manager_func;
164 man->gpu_offset = adev->mc.gtt_start;
165 man->available_caching = TTM_PL_MASK_CACHING;
166 man->default_caching = TTM_PL_FLAG_CACHED;
167 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
168 break;
169 case TTM_PL_VRAM:
170 /* "On-card" video ram */
171 man->func = &ttm_bo_manager_func;
172 man->gpu_offset = adev->mc.vram_start;
173 man->flags = TTM_MEMTYPE_FLAG_FIXED |
174 TTM_MEMTYPE_FLAG_MAPPABLE;
175 man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
176 man->default_caching = TTM_PL_FLAG_WC;
177 break;
178 case AMDGPU_PL_GDS:
179 case AMDGPU_PL_GWS:
180 case AMDGPU_PL_OA:
181 /* On-chip GDS memory*/
182 man->func = &ttm_bo_manager_func;
183 man->gpu_offset = 0;
184 man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
185 man->available_caching = TTM_PL_FLAG_UNCACHED;
186 man->default_caching = TTM_PL_FLAG_UNCACHED;
187 break;
188 default:
189 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
190 return -EINVAL;
191 }
192 return 0;
193}
194
195static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
196 struct ttm_placement *placement)
197{
198 struct amdgpu_bo *rbo;
199 static struct ttm_place placements = {
200 .fpfn = 0,
201 .lpfn = 0,
202 .flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
203 };
204
205 if (!amdgpu_ttm_bo_is_amdgpu_bo(bo)) {
206 placement->placement = &placements;
207 placement->busy_placement = &placements;
208 placement->num_placement = 1;
209 placement->num_busy_placement = 1;
210 return;
211 }
212 rbo = container_of(bo, struct amdgpu_bo, tbo);
213 switch (bo->mem.mem_type) {
214 case TTM_PL_VRAM:
215 if (rbo->adev->mman.buffer_funcs_ring->ready == false)
216 amdgpu_ttm_placement_from_domain(rbo, AMDGPU_GEM_DOMAIN_CPU);
217 else
218 amdgpu_ttm_placement_from_domain(rbo, AMDGPU_GEM_DOMAIN_GTT);
219 break;
220 case TTM_PL_TT:
221 default:
222 amdgpu_ttm_placement_from_domain(rbo, AMDGPU_GEM_DOMAIN_CPU);
223 }
224 *placement = rbo->placement;
225}
226
227static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
228{
229 struct amdgpu_bo *rbo = container_of(bo, struct amdgpu_bo, tbo);
230
Jérôme Glisse054892e2016-04-19 09:07:51 -0400231 if (amdgpu_ttm_tt_get_usermm(bo->ttm))
232 return -EPERM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233 return drm_vma_node_verify_access(&rbo->gem_base.vma_node, filp);
234}
235
236static void amdgpu_move_null(struct ttm_buffer_object *bo,
237 struct ttm_mem_reg *new_mem)
238{
239 struct ttm_mem_reg *old_mem = &bo->mem;
240
241 BUG_ON(old_mem->mm_node != NULL);
242 *old_mem = *new_mem;
243 new_mem->mm_node = NULL;
244}
245
246static int amdgpu_move_blit(struct ttm_buffer_object *bo,
247 bool evict, bool no_wait_gpu,
248 struct ttm_mem_reg *new_mem,
249 struct ttm_mem_reg *old_mem)
250{
251 struct amdgpu_device *adev;
252 struct amdgpu_ring *ring;
253 uint64_t old_start, new_start;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +0800254 struct fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255 int r;
256
257 adev = amdgpu_get_adev(bo->bdev);
258 ring = adev->mman.buffer_funcs_ring;
259 old_start = old_mem->start << PAGE_SHIFT;
260 new_start = new_mem->start << PAGE_SHIFT;
261
262 switch (old_mem->mem_type) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400263 case TTM_PL_TT:
Christian Königc855e252016-09-05 17:00:57 +0200264 r = amdgpu_ttm_bind(bo->ttm, old_mem);
265 if (r)
266 return r;
267
268 case TTM_PL_VRAM:
Flora Cui27798e02016-08-18 13:18:09 +0800269 old_start += bo->bdev->man[old_mem->mem_type].gpu_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270 break;
271 default:
272 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
273 return -EINVAL;
274 }
275 switch (new_mem->mem_type) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400276 case TTM_PL_TT:
Christian Königc855e252016-09-05 17:00:57 +0200277 r = amdgpu_ttm_bind(bo->ttm, new_mem);
278 if (r)
279 return r;
280
281 case TTM_PL_VRAM:
Flora Cui27798e02016-08-18 13:18:09 +0800282 new_start += bo->bdev->man[new_mem->mem_type].gpu_offset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283 break;
284 default:
285 DRM_ERROR("Unknown placement %d\n", old_mem->mem_type);
286 return -EINVAL;
287 }
288 if (!ring->ready) {
289 DRM_ERROR("Trying to move memory with ring turned off.\n");
290 return -EINVAL;
291 }
292
293 BUILD_BUG_ON((PAGE_SIZE % AMDGPU_GPU_PAGE_SIZE) != 0);
294
295 r = amdgpu_copy_buffer(ring, old_start, new_start,
296 new_mem->num_pages * PAGE_SIZE, /* bytes */
Chunming Zhoue24db982016-08-15 10:46:04 +0800297 bo->resv, &fence, false);
Christian Königce64bc22016-06-15 13:44:05 +0200298 if (r)
299 return r;
300
301 r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +0800302 fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 return r;
304}
305
306static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo,
307 bool evict, bool interruptible,
308 bool no_wait_gpu,
309 struct ttm_mem_reg *new_mem)
310{
311 struct amdgpu_device *adev;
312 struct ttm_mem_reg *old_mem = &bo->mem;
313 struct ttm_mem_reg tmp_mem;
314 struct ttm_place placements;
315 struct ttm_placement placement;
316 int r;
317
318 adev = amdgpu_get_adev(bo->bdev);
319 tmp_mem = *new_mem;
320 tmp_mem.mm_node = NULL;
321 placement.num_placement = 1;
322 placement.placement = &placements;
323 placement.num_busy_placement = 1;
324 placement.busy_placement = &placements;
325 placements.fpfn = 0;
326 placements.lpfn = 0;
327 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
328 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
329 interruptible, no_wait_gpu);
330 if (unlikely(r)) {
331 return r;
332 }
333
334 r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
335 if (unlikely(r)) {
336 goto out_cleanup;
337 }
338
339 r = ttm_tt_bind(bo->ttm, &tmp_mem);
340 if (unlikely(r)) {
341 goto out_cleanup;
342 }
343 r = amdgpu_move_blit(bo, true, no_wait_gpu, &tmp_mem, old_mem);
344 if (unlikely(r)) {
345 goto out_cleanup;
346 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900347 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348out_cleanup:
349 ttm_bo_mem_put(bo, &tmp_mem);
350 return r;
351}
352
353static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo,
354 bool evict, bool interruptible,
355 bool no_wait_gpu,
356 struct ttm_mem_reg *new_mem)
357{
358 struct amdgpu_device *adev;
359 struct ttm_mem_reg *old_mem = &bo->mem;
360 struct ttm_mem_reg tmp_mem;
361 struct ttm_placement placement;
362 struct ttm_place placements;
363 int r;
364
365 adev = amdgpu_get_adev(bo->bdev);
366 tmp_mem = *new_mem;
367 tmp_mem.mm_node = NULL;
368 placement.num_placement = 1;
369 placement.placement = &placements;
370 placement.num_busy_placement = 1;
371 placement.busy_placement = &placements;
372 placements.fpfn = 0;
373 placements.lpfn = 0;
374 placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
375 r = ttm_bo_mem_space(bo, &placement, &tmp_mem,
376 interruptible, no_wait_gpu);
377 if (unlikely(r)) {
378 return r;
379 }
Michel Dänzer4e2f0ca2016-08-08 12:28:25 +0900380 r = ttm_bo_move_ttm(bo, interruptible, no_wait_gpu, &tmp_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381 if (unlikely(r)) {
382 goto out_cleanup;
383 }
384 r = amdgpu_move_blit(bo, true, no_wait_gpu, new_mem, old_mem);
385 if (unlikely(r)) {
386 goto out_cleanup;
387 }
388out_cleanup:
389 ttm_bo_mem_put(bo, &tmp_mem);
390 return r;
391}
392
393static int amdgpu_bo_move(struct ttm_buffer_object *bo,
394 bool evict, bool interruptible,
395 bool no_wait_gpu,
396 struct ttm_mem_reg *new_mem)
397{
398 struct amdgpu_device *adev;
Michel Dänzer104ece92016-03-28 12:53:02 +0900399 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400400 struct ttm_mem_reg *old_mem = &bo->mem;
401 int r;
402
Michel Dänzer104ece92016-03-28 12:53:02 +0900403 /* Can't move a pinned BO */
404 abo = container_of(bo, struct amdgpu_bo, tbo);
405 if (WARN_ON_ONCE(abo->pin_count > 0))
406 return -EINVAL;
407
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400408 adev = amdgpu_get_adev(bo->bdev);
Christian Königdbd5ed62016-06-21 16:28:14 +0200409
410 /* remember the eviction */
411 if (evict)
412 atomic64_inc(&adev->num_evictions);
413
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400414 if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
415 amdgpu_move_null(bo, new_mem);
416 return 0;
417 }
418 if ((old_mem->mem_type == TTM_PL_TT &&
419 new_mem->mem_type == TTM_PL_SYSTEM) ||
420 (old_mem->mem_type == TTM_PL_SYSTEM &&
421 new_mem->mem_type == TTM_PL_TT)) {
422 /* bind is enough */
423 amdgpu_move_null(bo, new_mem);
424 return 0;
425 }
426 if (adev->mman.buffer_funcs == NULL ||
427 adev->mman.buffer_funcs_ring == NULL ||
428 !adev->mman.buffer_funcs_ring->ready) {
429 /* use memcpy */
430 goto memcpy;
431 }
432
433 if (old_mem->mem_type == TTM_PL_VRAM &&
434 new_mem->mem_type == TTM_PL_SYSTEM) {
435 r = amdgpu_move_vram_ram(bo, evict, interruptible,
436 no_wait_gpu, new_mem);
437 } else if (old_mem->mem_type == TTM_PL_SYSTEM &&
438 new_mem->mem_type == TTM_PL_VRAM) {
439 r = amdgpu_move_ram_vram(bo, evict, interruptible,
440 no_wait_gpu, new_mem);
441 } else {
442 r = amdgpu_move_blit(bo, evict, no_wait_gpu, new_mem, old_mem);
443 }
444
445 if (r) {
446memcpy:
Michel Dänzer4499f2a2016-08-08 12:28:26 +0900447 r = ttm_bo_move_memcpy(bo, interruptible, no_wait_gpu, new_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400448 if (r) {
449 return r;
450 }
451 }
452
453 /* update statistics */
454 atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
455 return 0;
456}
457
458static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
459{
460 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
461 struct amdgpu_device *adev = amdgpu_get_adev(bdev);
462
463 mem->bus.addr = NULL;
464 mem->bus.offset = 0;
465 mem->bus.size = mem->num_pages << PAGE_SHIFT;
466 mem->bus.base = 0;
467 mem->bus.is_iomem = false;
468 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
469 return -EINVAL;
470 switch (mem->mem_type) {
471 case TTM_PL_SYSTEM:
472 /* system memory */
473 return 0;
474 case TTM_PL_TT:
475 break;
476 case TTM_PL_VRAM:
477 mem->bus.offset = mem->start << PAGE_SHIFT;
478 /* check if it's visible */
479 if ((mem->bus.offset + mem->bus.size) > adev->mc.visible_vram_size)
480 return -EINVAL;
481 mem->bus.base = adev->mc.aper_base;
482 mem->bus.is_iomem = true;
483#ifdef __alpha__
484 /*
485 * Alpha: use bus.addr to hold the ioremap() return,
486 * so we can modify bus.base below.
487 */
488 if (mem->placement & TTM_PL_FLAG_WC)
489 mem->bus.addr =
490 ioremap_wc(mem->bus.base + mem->bus.offset,
491 mem->bus.size);
492 else
493 mem->bus.addr =
494 ioremap_nocache(mem->bus.base + mem->bus.offset,
495 mem->bus.size);
496
497 /*
498 * Alpha: Use just the bus offset plus
499 * the hose/domain memory base for bus.base.
500 * It then can be used to build PTEs for VRAM
501 * access, as done in ttm_bo_vm_fault().
502 */
503 mem->bus.base = (mem->bus.base & 0x0ffffffffUL) +
504 adev->ddev->hose->dense_mem_base;
505#endif
506 break;
507 default:
508 return -EINVAL;
509 }
510 return 0;
511}
512
513static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
514{
515}
516
517/*
518 * TTM backend functions.
519 */
Christian König637dd3b2016-03-03 14:24:57 +0100520struct amdgpu_ttm_gup_task_list {
521 struct list_head list;
522 struct task_struct *task;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523};
524
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525struct amdgpu_ttm_tt {
Christian König637dd3b2016-03-03 14:24:57 +0100526 struct ttm_dma_tt ttm;
527 struct amdgpu_device *adev;
528 u64 offset;
529 uint64_t userptr;
530 struct mm_struct *usermm;
531 uint32_t userflags;
532 spinlock_t guptasklock;
533 struct list_head guptasks;
Christian König2f568db2016-02-23 12:36:59 +0100534 atomic_t mmu_invalidations;
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800535 struct list_head list;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536};
537
Christian König2f568db2016-02-23 12:36:59 +0100538int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400540 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König2f568db2016-02-23 12:36:59 +0100541 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
542 unsigned pinned = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 int r;
544
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400545 if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
Christian König2f568db2016-02-23 12:36:59 +0100546 /* check that we only use anonymous memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 to prevent problems with writeback */
548 unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
549 struct vm_area_struct *vma;
550
551 vma = find_vma(gtt->usermm, gtt->userptr);
552 if (!vma || vma->vm_file || vma->vm_end < end)
553 return -EPERM;
554 }
555
556 do {
557 unsigned num_pages = ttm->num_pages - pinned;
558 uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
Christian König2f568db2016-02-23 12:36:59 +0100559 struct page **p = pages + pinned;
Christian König637dd3b2016-03-03 14:24:57 +0100560 struct amdgpu_ttm_gup_task_list guptask;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561
Christian König637dd3b2016-03-03 14:24:57 +0100562 guptask.task = current;
563 spin_lock(&gtt->guptasklock);
564 list_add(&guptask.list, &gtt->guptasks);
565 spin_unlock(&gtt->guptasklock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400566
Linus Torvalds266c73b2016-03-21 13:48:00 -0700567 r = get_user_pages(userptr, num_pages, write, 0, p, NULL);
Christian König637dd3b2016-03-03 14:24:57 +0100568
569 spin_lock(&gtt->guptasklock);
570 list_del(&guptask.list);
571 spin_unlock(&gtt->guptasklock);
572
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400573 if (r < 0)
574 goto release_pages;
575
576 pinned += r;
577
578 } while (pinned < ttm->num_pages);
579
Christian König2f568db2016-02-23 12:36:59 +0100580 return 0;
581
582release_pages:
583 release_pages(pages, pinned, 0);
584 return r;
585}
586
587/* prepare the sg table with the user pages */
588static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
589{
590 struct amdgpu_device *adev = amdgpu_get_adev(ttm->bdev);
591 struct amdgpu_ttm_tt *gtt = (void *)ttm;
592 unsigned nents;
593 int r;
594
595 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
596 enum dma_data_direction direction = write ?
597 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
598
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
600 ttm->num_pages << PAGE_SHIFT,
601 GFP_KERNEL);
602 if (r)
603 goto release_sg;
604
605 r = -ENOMEM;
606 nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
607 if (nents != ttm->sg->nents)
608 goto release_sg;
609
610 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
611 gtt->ttm.dma_address, ttm->num_pages);
612
613 return 0;
614
615release_sg:
616 kfree(ttm->sg);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400617 return r;
618}
619
620static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
621{
622 struct amdgpu_device *adev = amdgpu_get_adev(ttm->bdev);
623 struct amdgpu_ttm_tt *gtt = (void *)ttm;
monk.liudd08fae2015-05-07 14:19:18 -0400624 struct sg_page_iter sg_iter;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625
626 int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
627 enum dma_data_direction direction = write ?
628 DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
629
630 /* double check that we don't free the table twice */
631 if (!ttm->sg->sgl)
632 return;
633
634 /* free the sg table and pages again */
635 dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
636
monk.liudd08fae2015-05-07 14:19:18 -0400637 for_each_sg_page(ttm->sg->sgl, &sg_iter, ttm->sg->nents, 0) {
638 struct page *page = sg_page_iter_page(&sg_iter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
640 set_page_dirty(page);
641
642 mark_page_accessed(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300643 put_page(page);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 }
645
646 sg_free_table(ttm->sg);
647}
648
649static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
650 struct ttm_mem_reg *bo_mem)
651{
652 struct amdgpu_ttm_tt *gtt = (void*)ttm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653 int r;
654
Chunming Zhoue2f784f2015-11-26 16:33:58 +0800655 if (gtt->userptr) {
656 r = amdgpu_ttm_tt_pin_userptr(ttm);
657 if (r) {
658 DRM_ERROR("failed to pin userptr\n");
659 return r;
660 }
661 }
Christian König71c76a02016-09-03 16:18:26 +0200662 gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400663 if (!ttm->num_pages) {
664 WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
665 ttm->num_pages, bo_mem, ttm);
666 }
667
668 if (bo_mem->mem_type == AMDGPU_PL_GDS ||
669 bo_mem->mem_type == AMDGPU_PL_GWS ||
670 bo_mem->mem_type == AMDGPU_PL_OA)
671 return -EINVAL;
672
Christian Königc855e252016-09-05 17:00:57 +0200673 return 0;
674}
675
676bool amdgpu_ttm_is_bound(struct ttm_tt *ttm)
677{
678 struct amdgpu_ttm_tt *gtt = (void *)ttm;
679
680 return gtt && !list_empty(&gtt->list);
681}
682
683int amdgpu_ttm_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
684{
685 struct amdgpu_ttm_tt *gtt = (void *)ttm;
686 uint32_t flags;
687 int r;
688
689 if (!ttm || amdgpu_ttm_is_bound(ttm))
690 return 0;
691
692 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, ttm, bo_mem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 r = amdgpu_gart_bind(gtt->adev, gtt->offset, ttm->num_pages,
694 ttm->pages, gtt->ttm.dma_address, flags);
695
696 if (r) {
Christian König71c76a02016-09-03 16:18:26 +0200697 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
698 ttm->num_pages, gtt->offset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 return r;
700 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800701 spin_lock(&gtt->adev->gtt_list_lock);
702 list_add_tail(&gtt->list, &gtt->adev->gtt_list);
703 spin_unlock(&gtt->adev->gtt_list_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 return 0;
705}
706
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800707int amdgpu_ttm_recover_gart(struct amdgpu_device *adev)
708{
709 struct amdgpu_ttm_tt *gtt, *tmp;
710 struct ttm_mem_reg bo_mem;
711 uint32_t flags;
712 int r;
713
714 bo_mem.mem_type = TTM_PL_TT;
715 spin_lock(&adev->gtt_list_lock);
716 list_for_each_entry_safe(gtt, tmp, &adev->gtt_list, list) {
717 flags = amdgpu_ttm_tt_pte_flags(gtt->adev, &gtt->ttm.ttm, &bo_mem);
718 r = amdgpu_gart_bind(adev, gtt->offset, gtt->ttm.ttm.num_pages,
719 gtt->ttm.ttm.pages, gtt->ttm.dma_address,
720 flags);
721 if (r) {
722 spin_unlock(&adev->gtt_list_lock);
Christian König71c76a02016-09-03 16:18:26 +0200723 DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
724 gtt->ttm.ttm.num_pages, gtt->offset);
Chunming Zhou2c0d7312016-08-30 16:36:25 +0800725 return r;
726 }
727 }
728 spin_unlock(&adev->gtt_list_lock);
729 return 0;
730}
731
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
733{
734 struct amdgpu_ttm_tt *gtt = (void *)ttm;
735
Christian König78ab0a32016-09-09 15:39:08 +0200736 if (!amdgpu_ttm_is_bound(ttm))
737 return 0;
738
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739 /* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
740 if (gtt->adev->gart.ready)
741 amdgpu_gart_unbind(gtt->adev, gtt->offset, ttm->num_pages);
742
743 if (gtt->userptr)
744 amdgpu_ttm_tt_unpin_userptr(ttm);
745
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800746 spin_lock(&gtt->adev->gtt_list_lock);
747 list_del_init(&gtt->list);
748 spin_unlock(&gtt->adev->gtt_list_lock);
749
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400750 return 0;
751}
752
753static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
754{
755 struct amdgpu_ttm_tt *gtt = (void *)ttm;
756
757 ttm_dma_tt_fini(&gtt->ttm);
758 kfree(gtt);
759}
760
761static struct ttm_backend_func amdgpu_backend_func = {
762 .bind = &amdgpu_ttm_backend_bind,
763 .unbind = &amdgpu_ttm_backend_unbind,
764 .destroy = &amdgpu_ttm_backend_destroy,
765};
766
767static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_bo_device *bdev,
768 unsigned long size, uint32_t page_flags,
769 struct page *dummy_read_page)
770{
771 struct amdgpu_device *adev;
772 struct amdgpu_ttm_tt *gtt;
773
774 adev = amdgpu_get_adev(bdev);
775
776 gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
777 if (gtt == NULL) {
778 return NULL;
779 }
780 gtt->ttm.ttm.func = &amdgpu_backend_func;
781 gtt->adev = adev;
782 if (ttm_dma_tt_init(&gtt->ttm, bdev, size, page_flags, dummy_read_page)) {
783 kfree(gtt);
784 return NULL;
785 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +0800786 INIT_LIST_HEAD(&gtt->list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400787 return &gtt->ttm.ttm;
788}
789
790static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm)
791{
792 struct amdgpu_device *adev;
793 struct amdgpu_ttm_tt *gtt = (void *)ttm;
794 unsigned i;
795 int r;
796 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
797
798 if (ttm->state != tt_unpopulated)
799 return 0;
800
801 if (gtt && gtt->userptr) {
Maninder Singh5f0b34c2015-06-26 13:28:50 +0530802 ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400803 if (!ttm->sg)
804 return -ENOMEM;
805
806 ttm->page_flags |= TTM_PAGE_FLAG_SG;
807 ttm->state = tt_unbound;
808 return 0;
809 }
810
811 if (slave && ttm->sg) {
812 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
813 gtt->ttm.dma_address, ttm->num_pages);
814 ttm->state = tt_unbound;
815 return 0;
816 }
817
818 adev = amdgpu_get_adev(ttm->bdev);
819
820#ifdef CONFIG_SWIOTLB
821 if (swiotlb_nr_tbl()) {
822 return ttm_dma_populate(&gtt->ttm, adev->dev);
823 }
824#endif
825
826 r = ttm_pool_populate(ttm);
827 if (r) {
828 return r;
829 }
830
831 for (i = 0; i < ttm->num_pages; i++) {
832 gtt->ttm.dma_address[i] = pci_map_page(adev->pdev, ttm->pages[i],
833 0, PAGE_SIZE,
834 PCI_DMA_BIDIRECTIONAL);
835 if (pci_dma_mapping_error(adev->pdev, gtt->ttm.dma_address[i])) {
Rasmus Villemoes09ccbb72016-02-15 19:41:45 +0100836 while (i--) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400837 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
838 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
839 gtt->ttm.dma_address[i] = 0;
840 }
841 ttm_pool_unpopulate(ttm);
842 return -EFAULT;
843 }
844 }
845 return 0;
846}
847
848static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
849{
850 struct amdgpu_device *adev;
851 struct amdgpu_ttm_tt *gtt = (void *)ttm;
852 unsigned i;
853 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
854
855 if (gtt && gtt->userptr) {
856 kfree(ttm->sg);
857 ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
858 return;
859 }
860
861 if (slave)
862 return;
863
864 adev = amdgpu_get_adev(ttm->bdev);
865
866#ifdef CONFIG_SWIOTLB
867 if (swiotlb_nr_tbl()) {
868 ttm_dma_unpopulate(&gtt->ttm, adev->dev);
869 return;
870 }
871#endif
872
873 for (i = 0; i < ttm->num_pages; i++) {
874 if (gtt->ttm.dma_address[i]) {
875 pci_unmap_page(adev->pdev, gtt->ttm.dma_address[i],
876 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
877 }
878 }
879
880 ttm_pool_unpopulate(ttm);
881}
882
883int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
884 uint32_t flags)
885{
886 struct amdgpu_ttm_tt *gtt = (void *)ttm;
887
888 if (gtt == NULL)
889 return -EINVAL;
890
891 gtt->userptr = addr;
892 gtt->usermm = current->mm;
893 gtt->userflags = flags;
Christian König637dd3b2016-03-03 14:24:57 +0100894 spin_lock_init(&gtt->guptasklock);
895 INIT_LIST_HEAD(&gtt->guptasks);
Christian König2f568db2016-02-23 12:36:59 +0100896 atomic_set(&gtt->mmu_invalidations, 0);
Christian König637dd3b2016-03-03 14:24:57 +0100897
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400898 return 0;
899}
900
Christian Königcc325d12016-02-08 11:08:35 +0100901struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400902{
903 struct amdgpu_ttm_tt *gtt = (void *)ttm;
904
905 if (gtt == NULL)
Christian Königcc325d12016-02-08 11:08:35 +0100906 return NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400907
Christian Königcc325d12016-02-08 11:08:35 +0100908 return gtt->usermm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400909}
910
Christian Königcc1de6e2016-02-08 10:57:22 +0100911bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
912 unsigned long end)
913{
914 struct amdgpu_ttm_tt *gtt = (void *)ttm;
Christian König637dd3b2016-03-03 14:24:57 +0100915 struct amdgpu_ttm_gup_task_list *entry;
Christian Königcc1de6e2016-02-08 10:57:22 +0100916 unsigned long size;
917
Christian König637dd3b2016-03-03 14:24:57 +0100918 if (gtt == NULL || !gtt->userptr)
Christian Königcc1de6e2016-02-08 10:57:22 +0100919 return false;
920
921 size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
922 if (gtt->userptr > end || gtt->userptr + size <= start)
923 return false;
924
Christian König637dd3b2016-03-03 14:24:57 +0100925 spin_lock(&gtt->guptasklock);
926 list_for_each_entry(entry, &gtt->guptasks, list) {
927 if (entry->task == current) {
928 spin_unlock(&gtt->guptasklock);
929 return false;
930 }
931 }
932 spin_unlock(&gtt->guptasklock);
933
Christian König2f568db2016-02-23 12:36:59 +0100934 atomic_inc(&gtt->mmu_invalidations);
935
Christian Königcc1de6e2016-02-08 10:57:22 +0100936 return true;
937}
938
Christian König2f568db2016-02-23 12:36:59 +0100939bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
940 int *last_invalidated)
941{
942 struct amdgpu_ttm_tt *gtt = (void *)ttm;
943 int prev_invalidated = *last_invalidated;
944
945 *last_invalidated = atomic_read(&gtt->mmu_invalidations);
946 return prev_invalidated != *last_invalidated;
947}
948
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400949bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
950{
951 struct amdgpu_ttm_tt *gtt = (void *)ttm;
952
953 if (gtt == NULL)
954 return false;
955
956 return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
957}
958
959uint32_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
960 struct ttm_mem_reg *mem)
961{
962 uint32_t flags = 0;
963
964 if (mem && mem->mem_type != TTM_PL_SYSTEM)
965 flags |= AMDGPU_PTE_VALID;
966
Christian König6d999052015-12-04 13:32:55 +0100967 if (mem && mem->mem_type == TTM_PL_TT) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968 flags |= AMDGPU_PTE_SYSTEM;
969
Christian König6d999052015-12-04 13:32:55 +0100970 if (ttm->caching_state == tt_cached)
971 flags |= AMDGPU_PTE_SNOOPED;
972 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973
Ken Wang8f3c1622016-02-03 19:17:53 +0800974 if (adev->asic_type >= CHIP_TONGA)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 flags |= AMDGPU_PTE_EXECUTABLE;
976
977 flags |= AMDGPU_PTE_READABLE;
978
979 if (!amdgpu_ttm_tt_is_readonly(ttm))
980 flags |= AMDGPU_PTE_WRITEABLE;
981
982 return flags;
983}
984
Christian König29b32592016-04-15 17:19:16 +0200985static void amdgpu_ttm_lru_removal(struct ttm_buffer_object *tbo)
986{
987 struct amdgpu_device *adev = amdgpu_get_adev(tbo->bdev);
988 unsigned i, j;
989
990 for (i = 0; i < AMDGPU_TTM_LRU_SIZE; ++i) {
991 struct amdgpu_mman_lru *lru = &adev->mman.log2_size[i];
992
993 for (j = 0; j < TTM_NUM_MEM_TYPES; ++j)
994 if (&tbo->lru == lru->lru[j])
995 lru->lru[j] = tbo->lru.prev;
996
997 if (&tbo->swap == lru->swap_lru)
998 lru->swap_lru = tbo->swap.prev;
999 }
1000}
1001
1002static struct amdgpu_mman_lru *amdgpu_ttm_lru(struct ttm_buffer_object *tbo)
1003{
1004 struct amdgpu_device *adev = amdgpu_get_adev(tbo->bdev);
1005 unsigned log2_size = min(ilog2(tbo->num_pages),
1006 AMDGPU_TTM_LRU_SIZE - 1);
1007
1008 return &adev->mman.log2_size[log2_size];
1009}
1010
1011static struct list_head *amdgpu_ttm_lru_tail(struct ttm_buffer_object *tbo)
1012{
1013 struct amdgpu_mman_lru *lru = amdgpu_ttm_lru(tbo);
1014 struct list_head *res = lru->lru[tbo->mem.mem_type];
1015
1016 lru->lru[tbo->mem.mem_type] = &tbo->lru;
Christian König1fdc0b72016-08-17 13:44:20 +02001017 while ((++lru)->lru[tbo->mem.mem_type] == res)
1018 lru->lru[tbo->mem.mem_type] = &tbo->lru;
Christian König29b32592016-04-15 17:19:16 +02001019
1020 return res;
1021}
1022
1023static struct list_head *amdgpu_ttm_swap_lru_tail(struct ttm_buffer_object *tbo)
1024{
1025 struct amdgpu_mman_lru *lru = amdgpu_ttm_lru(tbo);
1026 struct list_head *res = lru->swap_lru;
1027
1028 lru->swap_lru = &tbo->swap;
Christian König1fdc0b72016-08-17 13:44:20 +02001029 while ((++lru)->swap_lru == res)
1030 lru->swap_lru = &tbo->swap;
Christian König29b32592016-04-15 17:19:16 +02001031
1032 return res;
1033}
1034
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001035static struct ttm_bo_driver amdgpu_bo_driver = {
1036 .ttm_tt_create = &amdgpu_ttm_tt_create,
1037 .ttm_tt_populate = &amdgpu_ttm_tt_populate,
1038 .ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1039 .invalidate_caches = &amdgpu_invalidate_caches,
1040 .init_mem_type = &amdgpu_init_mem_type,
1041 .evict_flags = &amdgpu_evict_flags,
1042 .move = &amdgpu_bo_move,
1043 .verify_access = &amdgpu_verify_access,
1044 .move_notify = &amdgpu_bo_move_notify,
1045 .fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1046 .io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1047 .io_mem_free = &amdgpu_ttm_io_mem_free,
Christian König29b32592016-04-15 17:19:16 +02001048 .lru_removal = &amdgpu_ttm_lru_removal,
1049 .lru_tail = &amdgpu_ttm_lru_tail,
1050 .swap_lru_tail = &amdgpu_ttm_swap_lru_tail,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001051};
1052
1053int amdgpu_ttm_init(struct amdgpu_device *adev)
1054{
Christian König29b32592016-04-15 17:19:16 +02001055 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001056 int r;
1057
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001058 /* No others user of address space so set it to 0 */
1059 r = ttm_bo_device_init(&adev->mman.bdev,
1060 adev->mman.bo_global_ref.ref.object,
1061 &amdgpu_bo_driver,
1062 adev->ddev->anon_inode->i_mapping,
1063 DRM_FILE_PAGE_OFFSET,
1064 adev->need_dma32);
1065 if (r) {
1066 DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1067 return r;
1068 }
Christian König29b32592016-04-15 17:19:16 +02001069
1070 for (i = 0; i < AMDGPU_TTM_LRU_SIZE; ++i) {
1071 struct amdgpu_mman_lru *lru = &adev->mman.log2_size[i];
1072
1073 for (j = 0; j < TTM_NUM_MEM_TYPES; ++j)
1074 lru->lru[j] = &adev->mman.bdev.man[j].lru;
1075 lru->swap_lru = &adev->mman.bdev.glob->swap_lru;
1076 }
1077
Christian König1fdc0b72016-08-17 13:44:20 +02001078 for (j = 0; j < TTM_NUM_MEM_TYPES; ++j)
1079 adev->mman.guard.lru[j] = NULL;
1080 adev->mman.guard.swap_lru = NULL;
1081
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001082 adev->mman.initialized = true;
1083 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1084 adev->mc.real_vram_size >> PAGE_SHIFT);
1085 if (r) {
1086 DRM_ERROR("Failed initializing VRAM heap.\n");
1087 return r;
1088 }
1089 /* Change the size here instead of the init above so only lpfn is affected */
1090 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
1091
1092 r = amdgpu_bo_create(adev, 256 * 1024, PAGE_SIZE, true,
Alex Deucher857d9132015-08-27 00:14:16 -04001093 AMDGPU_GEM_DOMAIN_VRAM,
1094 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +02001095 NULL, NULL, &adev->stollen_vga_memory);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001096 if (r) {
1097 return r;
1098 }
1099 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1100 if (r)
1101 return r;
1102 r = amdgpu_bo_pin(adev->stollen_vga_memory, AMDGPU_GEM_DOMAIN_VRAM, NULL);
1103 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1104 if (r) {
1105 amdgpu_bo_unref(&adev->stollen_vga_memory);
1106 return r;
1107 }
1108 DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1109 (unsigned) (adev->mc.real_vram_size / (1024 * 1024)));
1110 r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT,
1111 adev->mc.gtt_size >> PAGE_SHIFT);
1112 if (r) {
1113 DRM_ERROR("Failed initializing GTT heap.\n");
1114 return r;
1115 }
1116 DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1117 (unsigned)(adev->mc.gtt_size / (1024 * 1024)));
1118
1119 adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1120 adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1121 adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1122 adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1123 adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1124 adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1125 adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1126 adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1127 adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1128 /* GDS Memory */
1129 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1130 adev->gds.mem.total_size >> PAGE_SHIFT);
1131 if (r) {
1132 DRM_ERROR("Failed initializing GDS heap.\n");
1133 return r;
1134 }
1135
1136 /* GWS */
1137 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1138 adev->gds.gws.total_size >> PAGE_SHIFT);
1139 if (r) {
1140 DRM_ERROR("Failed initializing gws heap.\n");
1141 return r;
1142 }
1143
1144 /* OA */
1145 r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1146 adev->gds.oa.total_size >> PAGE_SHIFT);
1147 if (r) {
1148 DRM_ERROR("Failed initializing oa heap.\n");
1149 return r;
1150 }
1151
1152 r = amdgpu_ttm_debugfs_init(adev);
1153 if (r) {
1154 DRM_ERROR("Failed to init debugfs\n");
1155 return r;
1156 }
1157 return 0;
1158}
1159
1160void amdgpu_ttm_fini(struct amdgpu_device *adev)
1161{
1162 int r;
1163
1164 if (!adev->mman.initialized)
1165 return;
1166 amdgpu_ttm_debugfs_fini(adev);
1167 if (adev->stollen_vga_memory) {
1168 r = amdgpu_bo_reserve(adev->stollen_vga_memory, false);
1169 if (r == 0) {
1170 amdgpu_bo_unpin(adev->stollen_vga_memory);
1171 amdgpu_bo_unreserve(adev->stollen_vga_memory);
1172 }
1173 amdgpu_bo_unref(&adev->stollen_vga_memory);
1174 }
1175 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1176 ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
1177 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1178 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1179 ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
1180 ttm_bo_device_release(&adev->mman.bdev);
1181 amdgpu_gart_fini(adev);
1182 amdgpu_ttm_global_fini(adev);
1183 adev->mman.initialized = false;
1184 DRM_INFO("amdgpu: ttm finalized\n");
1185}
1186
1187/* this should only be called at bootup or when userspace
1188 * isn't running */
1189void amdgpu_ttm_set_active_vram_size(struct amdgpu_device *adev, u64 size)
1190{
1191 struct ttm_mem_type_manager *man;
1192
1193 if (!adev->mman.initialized)
1194 return;
1195
1196 man = &adev->mman.bdev.man[TTM_PL_VRAM];
1197 /* this just adjusts TTM size idea, which sets lpfn to the correct value */
1198 man->size = size >> PAGE_SHIFT;
1199}
1200
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1202{
1203 struct drm_file *file_priv;
1204 struct amdgpu_device *adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001205
Christian Könige176fe172015-05-27 10:22:47 +02001206 if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208
1209 file_priv = filp->private_data;
1210 adev = file_priv->minor->dev->dev_private;
Christian Könige176fe172015-05-27 10:22:47 +02001211 if (adev == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 return -EINVAL;
Christian Könige176fe172015-05-27 10:22:47 +02001213
1214 return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215}
1216
1217int amdgpu_copy_buffer(struct amdgpu_ring *ring,
1218 uint64_t src_offset,
1219 uint64_t dst_offset,
1220 uint32_t byte_count,
1221 struct reservation_object *resv,
Chunming Zhoue24db982016-08-15 10:46:04 +08001222 struct fence **fence, bool direct_submit)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223{
1224 struct amdgpu_device *adev = ring->adev;
Christian Königd71518b2016-02-01 12:20:25 +01001225 struct amdgpu_job *job;
1226
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001227 uint32_t max_bytes;
1228 unsigned num_loops, num_dw;
1229 unsigned i;
1230 int r;
1231
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
1233 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1234 num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
1235
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001236 /* for IB padding */
1237 while (num_dw & 0x7)
1238 num_dw++;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239
Christian Königd71518b2016-02-01 12:20:25 +01001240 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1241 if (r)
Chunming Zhou9066b0c2015-08-25 15:12:26 +08001242 return r;
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001243
1244 if (resv) {
Christian Könige86f9ce2016-02-08 12:13:05 +01001245 r = amdgpu_sync_resv(adev, &job->sync, resv,
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001246 AMDGPU_FENCE_OWNER_UNDEFINED);
1247 if (r) {
1248 DRM_ERROR("sync failed (%d).\n", r);
1249 goto error_free;
1250 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001251 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252
1253 for (i = 0; i < num_loops; i++) {
1254 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1255
Christian Königd71518b2016-02-01 12:20:25 +01001256 amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
1257 dst_offset, cur_size_in_bytes);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001258
1259 src_offset += cur_size_in_bytes;
1260 dst_offset += cur_size_in_bytes;
1261 byte_count -= cur_size_in_bytes;
1262 }
1263
Christian Königd71518b2016-02-01 12:20:25 +01001264 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1265 WARN_ON(job->ibs[0].length_dw > num_dw);
Chunming Zhoue24db982016-08-15 10:46:04 +08001266 if (direct_submit) {
1267 r = amdgpu_ib_schedule(ring, job->num_ibs, job->ibs,
1268 NULL, NULL, fence);
1269 job->fence = fence_get(*fence);
1270 if (r)
1271 DRM_ERROR("Error scheduling IBs (%d)\n", r);
1272 amdgpu_job_free(job);
1273 } else {
1274 r = amdgpu_job_submit(job, ring, &adev->mman.entity,
1275 AMDGPU_FENCE_OWNER_UNDEFINED, fence);
1276 if (r)
1277 goto error_free;
1278 }
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001279
Chunming Zhoue24db982016-08-15 10:46:04 +08001280 return r;
Christian Königd71518b2016-02-01 12:20:25 +01001281
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001282error_free:
Christian Königd71518b2016-02-01 12:20:25 +01001283 amdgpu_job_free(job);
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001284 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001285}
1286
Flora Cui59b4a972016-07-19 16:48:22 +08001287int amdgpu_fill_buffer(struct amdgpu_bo *bo,
1288 uint32_t src_data,
1289 struct reservation_object *resv,
1290 struct fence **fence)
1291{
1292 struct amdgpu_device *adev = bo->adev;
1293 struct amdgpu_job *job;
1294 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
1295
1296 uint32_t max_bytes, byte_count;
1297 uint64_t dst_offset;
1298 unsigned int num_loops, num_dw;
1299 unsigned int i;
1300 int r;
1301
1302 byte_count = bo->tbo.num_pages << PAGE_SHIFT;
1303 max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
1304 num_loops = DIV_ROUND_UP(byte_count, max_bytes);
1305 num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
1306
1307 /* for IB padding */
1308 while (num_dw & 0x7)
1309 num_dw++;
1310
1311 r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
1312 if (r)
1313 return r;
1314
1315 if (resv) {
1316 r = amdgpu_sync_resv(adev, &job->sync, resv,
1317 AMDGPU_FENCE_OWNER_UNDEFINED);
1318 if (r) {
1319 DRM_ERROR("sync failed (%d).\n", r);
1320 goto error_free;
1321 }
1322 }
1323
1324 dst_offset = bo->tbo.mem.start << PAGE_SHIFT;
1325 for (i = 0; i < num_loops; i++) {
1326 uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
1327
1328 amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
1329 dst_offset, cur_size_in_bytes);
1330
1331 dst_offset += cur_size_in_bytes;
1332 byte_count -= cur_size_in_bytes;
1333 }
1334
1335 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
1336 WARN_ON(job->ibs[0].length_dw > num_dw);
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 return 0;
1343
1344error_free:
1345 amdgpu_job_free(job);
1346 return r;
1347}
1348
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349#if defined(CONFIG_DEBUG_FS)
1350
1351static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
1352{
1353 struct drm_info_node *node = (struct drm_info_node *)m->private;
1354 unsigned ttm_pl = *(int *)node->info_ent->data;
1355 struct drm_device *dev = node->minor->dev;
1356 struct amdgpu_device *adev = dev->dev_private;
1357 struct drm_mm *mm = (struct drm_mm *)adev->mman.bdev.man[ttm_pl].priv;
1358 int ret;
1359 struct ttm_bo_global *glob = adev->mman.bdev.glob;
1360
1361 spin_lock(&glob->lru_lock);
1362 ret = drm_mm_dump_table(m, mm);
1363 spin_unlock(&glob->lru_lock);
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001364 if (ttm_pl == TTM_PL_VRAM)
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001365 seq_printf(m, "man size:%llu pages, ram usage:%lluMB, vis usage:%lluMB\n",
Chunming Zhoua2ef8a92015-09-22 18:20:50 +08001366 adev->mman.bdev.man[ttm_pl].size,
Arnd Bergmanne1b35f62015-11-10 13:17:55 +01001367 (u64)atomic64_read(&adev->vram_usage) >> 20,
1368 (u64)atomic64_read(&adev->vram_vis_usage) >> 20);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001369 return ret;
1370}
1371
1372static int ttm_pl_vram = TTM_PL_VRAM;
1373static int ttm_pl_tt = TTM_PL_TT;
1374
Nils Wallménius06ab6832016-05-02 12:46:15 -04001375static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001376 {"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
1377 {"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
1378 {"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
1379#ifdef CONFIG_SWIOTLB
1380 {"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
1381#endif
1382};
1383
1384static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
1385 size_t size, loff_t *pos)
1386{
1387 struct amdgpu_device *adev = f->f_inode->i_private;
1388 ssize_t result = 0;
1389 int r;
1390
1391 if (size & 0x3 || *pos & 0x3)
1392 return -EINVAL;
1393
1394 while (size) {
1395 unsigned long flags;
1396 uint32_t value;
1397
1398 if (*pos >= adev->mc.mc_vram_size)
1399 return result;
1400
1401 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1402 WREG32(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
1403 WREG32(mmMM_INDEX_HI, *pos >> 31);
1404 value = RREG32(mmMM_DATA);
1405 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1406
1407 r = put_user(value, (uint32_t *)buf);
1408 if (r)
1409 return r;
1410
1411 result += 4;
1412 buf += 4;
1413 *pos += 4;
1414 size -= 4;
1415 }
1416
1417 return result;
1418}
1419
1420static const struct file_operations amdgpu_ttm_vram_fops = {
1421 .owner = THIS_MODULE,
1422 .read = amdgpu_ttm_vram_read,
1423 .llseek = default_llseek
1424};
1425
Christian Königa1d29472016-03-30 14:42:57 +02001426#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
1427
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
1429 size_t size, loff_t *pos)
1430{
1431 struct amdgpu_device *adev = f->f_inode->i_private;
1432 ssize_t result = 0;
1433 int r;
1434
1435 while (size) {
1436 loff_t p = *pos / PAGE_SIZE;
1437 unsigned off = *pos & ~PAGE_MASK;
1438 size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
1439 struct page *page;
1440 void *ptr;
1441
1442 if (p >= adev->gart.num_cpu_pages)
1443 return result;
1444
1445 page = adev->gart.pages[p];
1446 if (page) {
1447 ptr = kmap(page);
1448 ptr += off;
1449
1450 r = copy_to_user(buf, ptr, cur_size);
1451 kunmap(adev->gart.pages[p]);
1452 } else
1453 r = clear_user(buf, cur_size);
1454
1455 if (r)
1456 return -EFAULT;
1457
1458 result += cur_size;
1459 buf += cur_size;
1460 *pos += cur_size;
1461 size -= cur_size;
1462 }
1463
1464 return result;
1465}
1466
1467static const struct file_operations amdgpu_ttm_gtt_fops = {
1468 .owner = THIS_MODULE,
1469 .read = amdgpu_ttm_gtt_read,
1470 .llseek = default_llseek
1471};
1472
1473#endif
1474
Christian Königa1d29472016-03-30 14:42:57 +02001475#endif
1476
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001477static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
1478{
1479#if defined(CONFIG_DEBUG_FS)
1480 unsigned count;
1481
1482 struct drm_minor *minor = adev->ddev->primary;
1483 struct dentry *ent, *root = minor->debugfs_root;
1484
1485 ent = debugfs_create_file("amdgpu_vram", S_IFREG | S_IRUGO, root,
1486 adev, &amdgpu_ttm_vram_fops);
1487 if (IS_ERR(ent))
1488 return PTR_ERR(ent);
1489 i_size_write(ent->d_inode, adev->mc.mc_vram_size);
1490 adev->mman.vram = ent;
1491
Christian Königa1d29472016-03-30 14:42:57 +02001492#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493 ent = debugfs_create_file("amdgpu_gtt", S_IFREG | S_IRUGO, root,
1494 adev, &amdgpu_ttm_gtt_fops);
1495 if (IS_ERR(ent))
1496 return PTR_ERR(ent);
1497 i_size_write(ent->d_inode, adev->mc.gtt_size);
1498 adev->mman.gtt = ent;
1499
Christian Königa1d29472016-03-30 14:42:57 +02001500#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001501 count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
1502
1503#ifdef CONFIG_SWIOTLB
1504 if (!swiotlb_nr_tbl())
1505 --count;
1506#endif
1507
1508 return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
1509#else
1510
1511 return 0;
1512#endif
1513}
1514
1515static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
1516{
1517#if defined(CONFIG_DEBUG_FS)
1518
1519 debugfs_remove(adev->mman.vram);
1520 adev->mman.vram = NULL;
1521
Christian Königa1d29472016-03-30 14:42:57 +02001522#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 debugfs_remove(adev->mman.gtt);
1524 adev->mman.gtt = NULL;
1525#endif
Christian Königa1d29472016-03-30 14:42:57 +02001526
1527#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001528}
Ken Wanga693e052016-07-27 19:18:01 +08001529
1530u64 amdgpu_ttm_get_gtt_mem_size(struct amdgpu_device *adev)
1531{
1532 return ttm_get_kernel_zone_memory_size(adev->mman.mem_global_ref.object);
1533}