1 /*
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 
33 #include <linux/dma-mapping.h>
34 #include <linux/iommu.h>
35 #include <linux/pagemap.h>
36 #include <linux/sched/task.h>
37 #include <linux/sched/mm.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/swap.h>
41 #include <linux/swiotlb.h>
42 #include <linux/dma-buf.h>
43 #include <linux/sizes.h>
44 #include <linux/module.h>
45 
46 #include <drm/drm_drv.h>
47 #include <drm/ttm/ttm_bo_api.h>
48 #include <drm/ttm/ttm_bo_driver.h>
49 #include <drm/ttm/ttm_placement.h>
50 #include <drm/ttm/ttm_range_manager.h>
51 
52 #include <drm/amdgpu_drm.h>
53 #include <drm/drm_drv.h>
54 
55 #include "amdgpu.h"
56 #include "amdgpu_object.h"
57 #include "amdgpu_trace.h"
58 #include "amdgpu_amdkfd.h"
59 #include "amdgpu_sdma.h"
60 #include "amdgpu_ras.h"
61 #include "amdgpu_atomfirmware.h"
62 #include "amdgpu_res_cursor.h"
63 #include "bif/bif_4_1_d.h"
64 
65 MODULE_IMPORT_NS(DMA_BUF);
66 
67 #define AMDGPU_TTM_VRAM_MAX_DW_READ	(size_t)128
68 
69 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
70 				   struct ttm_tt *ttm,
71 				   struct ttm_resource *bo_mem);
72 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
73 				      struct ttm_tt *ttm);
74 
75 static int amdgpu_ttm_init_on_chip(struct amdgpu_device *adev,
76 				    unsigned int type,
77 				    uint64_t size_in_page)
78 {
79 	return ttm_range_man_init(&adev->mman.bdev, type,
80 				  false, size_in_page);
81 }
82 
83 /**
84  * amdgpu_evict_flags - Compute placement flags
85  *
86  * @bo: The buffer object to evict
87  * @placement: Possible destination(s) for evicted BO
88  *
89  * Fill in placement data when ttm_bo_evict() is called
90  */
91 static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
92 				struct ttm_placement *placement)
93 {
94 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
95 	struct amdgpu_bo *abo;
96 	static const struct ttm_place placements = {
97 		.fpfn = 0,
98 		.lpfn = 0,
99 		.mem_type = TTM_PL_SYSTEM,
100 		.flags = 0
101 	};
102 
103 	/* Don't handle scatter gather BOs */
104 	if (bo->type == ttm_bo_type_sg) {
105 		placement->num_placement = 0;
106 		placement->num_busy_placement = 0;
107 		return;
108 	}
109 
110 	/* Object isn't an AMDGPU object so ignore */
111 	if (!amdgpu_bo_is_amdgpu_bo(bo)) {
112 		placement->placement = &placements;
113 		placement->busy_placement = &placements;
114 		placement->num_placement = 1;
115 		placement->num_busy_placement = 1;
116 		return;
117 	}
118 
119 	abo = ttm_to_amdgpu_bo(bo);
120 	if (abo->flags & AMDGPU_GEM_CREATE_DISCARDABLE) {
121 		placement->num_placement = 0;
122 		placement->num_busy_placement = 0;
123 		return;
124 	}
125 
126 	switch (bo->resource->mem_type) {
127 	case AMDGPU_PL_GDS:
128 	case AMDGPU_PL_GWS:
129 	case AMDGPU_PL_OA:
130 		placement->num_placement = 0;
131 		placement->num_busy_placement = 0;
132 		return;
133 
134 	case TTM_PL_VRAM:
135 		if (!adev->mman.buffer_funcs_enabled) {
136 			/* Move to system memory */
137 			amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
138 		} else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
139 			   !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
140 			   amdgpu_bo_in_cpu_visible_vram(abo)) {
141 
142 			/* Try evicting to the CPU inaccessible part of VRAM
143 			 * first, but only set GTT as busy placement, so this
144 			 * BO will be evicted to GTT rather than causing other
145 			 * BOs to be evicted from VRAM
146 			 */
147 			amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
148 							AMDGPU_GEM_DOMAIN_GTT |
149 							AMDGPU_GEM_DOMAIN_CPU);
150 			abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
151 			abo->placements[0].lpfn = 0;
152 			abo->placement.busy_placement = &abo->placements[1];
153 			abo->placement.num_busy_placement = 1;
154 		} else {
155 			/* Move to GTT memory */
156 			amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT |
157 							AMDGPU_GEM_DOMAIN_CPU);
158 		}
159 		break;
160 	case TTM_PL_TT:
161 	case AMDGPU_PL_PREEMPT:
162 	default:
163 		amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
164 		break;
165 	}
166 	*placement = abo->placement;
167 }
168 
169 /**
170  * amdgpu_ttm_map_buffer - Map memory into the GART windows
171  * @bo: buffer object to map
172  * @mem: memory object to map
173  * @mm_cur: range to map
174  * @window: which GART window to use
175  * @ring: DMA ring to use for the copy
176  * @tmz: if we should setup a TMZ enabled mapping
177  * @size: in number of bytes to map, out number of bytes mapped
178  * @addr: resulting address inside the MC address space
179  *
180  * Setup one of the GART windows to access a specific piece of memory or return
181  * the physical address for local memory.
182  */
183 static int amdgpu_ttm_map_buffer(struct ttm_buffer_object *bo,
184 				 struct ttm_resource *mem,
185 				 struct amdgpu_res_cursor *mm_cur,
186 				 unsigned window, struct amdgpu_ring *ring,
187 				 bool tmz, uint64_t *size, uint64_t *addr)
188 {
189 	struct amdgpu_device *adev = ring->adev;
190 	unsigned offset, num_pages, num_dw, num_bytes;
191 	uint64_t src_addr, dst_addr;
192 	struct dma_fence *fence;
193 	struct amdgpu_job *job;
194 	void *cpu_addr;
195 	uint64_t flags;
196 	unsigned int i;
197 	int r;
198 
199 	BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
200 	       AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
201 
202 	if (WARN_ON(mem->mem_type == AMDGPU_PL_PREEMPT))
203 		return -EINVAL;
204 
205 	/* Map only what can't be accessed directly */
206 	if (!tmz && mem->start != AMDGPU_BO_INVALID_OFFSET) {
207 		*addr = amdgpu_ttm_domain_start(adev, mem->mem_type) +
208 			mm_cur->start;
209 		return 0;
210 	}
211 
212 
213 	/*
214 	 * If start begins at an offset inside the page, then adjust the size
215 	 * and addr accordingly
216 	 */
217 	offset = mm_cur->start & ~PAGE_MASK;
218 
219 	num_pages = PFN_UP(*size + offset);
220 	num_pages = min_t(uint32_t, num_pages, AMDGPU_GTT_MAX_TRANSFER_SIZE);
221 
222 	*size = min(*size, (uint64_t)num_pages * PAGE_SIZE - offset);
223 
224 	*addr = adev->gmc.gart_start;
225 	*addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
226 		AMDGPU_GPU_PAGE_SIZE;
227 	*addr += offset;
228 
229 	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
230 	num_bytes = num_pages * 8 * AMDGPU_GPU_PAGES_IN_CPU_PAGE;
231 
232 	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes,
233 				     AMDGPU_IB_POOL_DELAYED, &job);
234 	if (r)
235 		return r;
236 
237 	src_addr = num_dw * 4;
238 	src_addr += job->ibs[0].gpu_addr;
239 
240 	dst_addr = amdgpu_bo_gpu_offset(adev->gart.bo);
241 	dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
242 	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
243 				dst_addr, num_bytes, false);
244 
245 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
246 	WARN_ON(job->ibs[0].length_dw > num_dw);
247 
248 	flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, mem);
249 	if (tmz)
250 		flags |= AMDGPU_PTE_TMZ;
251 
252 	cpu_addr = &job->ibs[0].ptr[num_dw];
253 
254 	if (mem->mem_type == TTM_PL_TT) {
255 		dma_addr_t *dma_addr;
256 
257 		dma_addr = &bo->ttm->dma_address[mm_cur->start >> PAGE_SHIFT];
258 		amdgpu_gart_map(adev, 0, num_pages, dma_addr, flags, cpu_addr);
259 	} else {
260 		dma_addr_t dma_address;
261 
262 		dma_address = mm_cur->start;
263 		dma_address += adev->vm_manager.vram_base_offset;
264 
265 		for (i = 0; i < num_pages; ++i) {
266 			amdgpu_gart_map(adev, i << PAGE_SHIFT, 1, &dma_address,
267 					flags, cpu_addr);
268 			dma_address += PAGE_SIZE;
269 		}
270 	}
271 
272 	r = amdgpu_job_submit(job, &adev->mman.entity,
273 			      AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
274 	if (r)
275 		goto error_free;
276 
277 	dma_fence_put(fence);
278 
279 	return r;
280 
281 error_free:
282 	amdgpu_job_free(job);
283 	return r;
284 }
285 
286 /**
287  * amdgpu_ttm_copy_mem_to_mem - Helper function for copy
288  * @adev: amdgpu device
289  * @src: buffer/address where to read from
290  * @dst: buffer/address where to write to
291  * @size: number of bytes to copy
292  * @tmz: if a secure copy should be used
293  * @resv: resv object to sync to
294  * @f: Returns the last fence if multiple jobs are submitted.
295  *
296  * The function copies @size bytes from {src->mem + src->offset} to
297  * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
298  * move and different for a BO to BO copy.
299  *
300  */
301 int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
302 			       const struct amdgpu_copy_mem *src,
303 			       const struct amdgpu_copy_mem *dst,
304 			       uint64_t size, bool tmz,
305 			       struct dma_resv *resv,
306 			       struct dma_fence **f)
307 {
308 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
309 	struct amdgpu_res_cursor src_mm, dst_mm;
310 	struct dma_fence *fence = NULL;
311 	int r = 0;
312 
313 	if (!adev->mman.buffer_funcs_enabled) {
314 		DRM_ERROR("Trying to move memory with ring turned off.\n");
315 		return -EINVAL;
316 	}
317 
318 	amdgpu_res_first(src->mem, src->offset, size, &src_mm);
319 	amdgpu_res_first(dst->mem, dst->offset, size, &dst_mm);
320 
321 	mutex_lock(&adev->mman.gtt_window_lock);
322 	while (src_mm.remaining) {
323 		uint64_t from, to, cur_size;
324 		struct dma_fence *next;
325 
326 		/* Never copy more than 256MiB at once to avoid a timeout */
327 		cur_size = min3(src_mm.size, dst_mm.size, 256ULL << 20);
328 
329 		/* Map src to window 0 and dst to window 1. */
330 		r = amdgpu_ttm_map_buffer(src->bo, src->mem, &src_mm,
331 					  0, ring, tmz, &cur_size, &from);
332 		if (r)
333 			goto error;
334 
335 		r = amdgpu_ttm_map_buffer(dst->bo, dst->mem, &dst_mm,
336 					  1, ring, tmz, &cur_size, &to);
337 		if (r)
338 			goto error;
339 
340 		r = amdgpu_copy_buffer(ring, from, to, cur_size,
341 				       resv, &next, false, true, tmz);
342 		if (r)
343 			goto error;
344 
345 		dma_fence_put(fence);
346 		fence = next;
347 
348 		amdgpu_res_next(&src_mm, cur_size);
349 		amdgpu_res_next(&dst_mm, cur_size);
350 	}
351 error:
352 	mutex_unlock(&adev->mman.gtt_window_lock);
353 	if (f)
354 		*f = dma_fence_get(fence);
355 	dma_fence_put(fence);
356 	return r;
357 }
358 
359 /*
360  * amdgpu_move_blit - Copy an entire buffer to another buffer
361  *
362  * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
363  * help move buffers to and from VRAM.
364  */
365 static int amdgpu_move_blit(struct ttm_buffer_object *bo,
366 			    bool evict,
367 			    struct ttm_resource *new_mem,
368 			    struct ttm_resource *old_mem)
369 {
370 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
371 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
372 	struct amdgpu_copy_mem src, dst;
373 	struct dma_fence *fence = NULL;
374 	int r;
375 
376 	src.bo = bo;
377 	dst.bo = bo;
378 	src.mem = old_mem;
379 	dst.mem = new_mem;
380 	src.offset = 0;
381 	dst.offset = 0;
382 
383 	r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
384 				       new_mem->num_pages << PAGE_SHIFT,
385 				       amdgpu_bo_encrypted(abo),
386 				       bo->base.resv, &fence);
387 	if (r)
388 		goto error;
389 
390 	/* clear the space being freed */
391 	if (old_mem->mem_type == TTM_PL_VRAM &&
392 	    (abo->flags & AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE)) {
393 		struct dma_fence *wipe_fence = NULL;
394 
395 		r = amdgpu_fill_buffer(abo, AMDGPU_POISON, NULL, &wipe_fence);
396 		if (r) {
397 			goto error;
398 		} else if (wipe_fence) {
399 			dma_fence_put(fence);
400 			fence = wipe_fence;
401 		}
402 	}
403 
404 	/* Always block for VM page tables before committing the new location */
405 	if (bo->type == ttm_bo_type_kernel)
406 		r = ttm_bo_move_accel_cleanup(bo, fence, true, false, new_mem);
407 	else
408 		r = ttm_bo_move_accel_cleanup(bo, fence, evict, true, new_mem);
409 	dma_fence_put(fence);
410 	return r;
411 
412 error:
413 	if (fence)
414 		dma_fence_wait(fence, false);
415 	dma_fence_put(fence);
416 	return r;
417 }
418 
419 /*
420  * amdgpu_mem_visible - Check that memory can be accessed by ttm_bo_move_memcpy
421  *
422  * Called by amdgpu_bo_move()
423  */
424 static bool amdgpu_mem_visible(struct amdgpu_device *adev,
425 			       struct ttm_resource *mem)
426 {
427 	u64 mem_size = (u64)mem->num_pages << PAGE_SHIFT;
428 	struct amdgpu_res_cursor cursor;
429 	u64 end;
430 
431 	if (mem->mem_type == TTM_PL_SYSTEM ||
432 	    mem->mem_type == TTM_PL_TT)
433 		return true;
434 	if (mem->mem_type != TTM_PL_VRAM)
435 		return false;
436 
437 	amdgpu_res_first(mem, 0, mem_size, &cursor);
438 	end = cursor.start + cursor.size;
439 	while (cursor.remaining) {
440 		amdgpu_res_next(&cursor, cursor.size);
441 
442 		/* ttm_resource_ioremap only supports contiguous memory */
443 		if (end != cursor.start)
444 			return false;
445 
446 		end = cursor.start + cursor.size;
447 	}
448 
449 	return end <= adev->gmc.visible_vram_size;
450 }
451 
452 /*
453  * amdgpu_bo_move - Move a buffer object to a new memory location
454  *
455  * Called by ttm_bo_handle_move_mem()
456  */
457 static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
458 			  struct ttm_operation_ctx *ctx,
459 			  struct ttm_resource *new_mem,
460 			  struct ttm_place *hop)
461 {
462 	struct amdgpu_device *adev;
463 	struct amdgpu_bo *abo;
464 	struct ttm_resource *old_mem = bo->resource;
465 	int r;
466 
467 	if (new_mem->mem_type == TTM_PL_TT ||
468 	    new_mem->mem_type == AMDGPU_PL_PREEMPT) {
469 		r = amdgpu_ttm_backend_bind(bo->bdev, bo->ttm, new_mem);
470 		if (r)
471 			return r;
472 	}
473 
474 	/* Can't move a pinned BO */
475 	abo = ttm_to_amdgpu_bo(bo);
476 	if (WARN_ON_ONCE(abo->tbo.pin_count > 0))
477 		return -EINVAL;
478 
479 	adev = amdgpu_ttm_adev(bo->bdev);
480 
481 	if (!old_mem || (old_mem->mem_type == TTM_PL_SYSTEM &&
482 			 bo->ttm == NULL)) {
483 		ttm_bo_move_null(bo, new_mem);
484 		goto out;
485 	}
486 	if (old_mem->mem_type == TTM_PL_SYSTEM &&
487 	    (new_mem->mem_type == TTM_PL_TT ||
488 	     new_mem->mem_type == AMDGPU_PL_PREEMPT)) {
489 		ttm_bo_move_null(bo, new_mem);
490 		goto out;
491 	}
492 	if ((old_mem->mem_type == TTM_PL_TT ||
493 	     old_mem->mem_type == AMDGPU_PL_PREEMPT) &&
494 	    new_mem->mem_type == TTM_PL_SYSTEM) {
495 		r = ttm_bo_wait_ctx(bo, ctx);
496 		if (r)
497 			return r;
498 
499 		amdgpu_ttm_backend_unbind(bo->bdev, bo->ttm);
500 		ttm_resource_free(bo, &bo->resource);
501 		ttm_bo_assign_mem(bo, new_mem);
502 		goto out;
503 	}
504 
505 	if (old_mem->mem_type == AMDGPU_PL_GDS ||
506 	    old_mem->mem_type == AMDGPU_PL_GWS ||
507 	    old_mem->mem_type == AMDGPU_PL_OA ||
508 	    new_mem->mem_type == AMDGPU_PL_GDS ||
509 	    new_mem->mem_type == AMDGPU_PL_GWS ||
510 	    new_mem->mem_type == AMDGPU_PL_OA) {
511 		/* Nothing to save here */
512 		ttm_bo_move_null(bo, new_mem);
513 		goto out;
514 	}
515 
516 	if (bo->type == ttm_bo_type_device &&
517 	    new_mem->mem_type == TTM_PL_VRAM &&
518 	    old_mem->mem_type != TTM_PL_VRAM) {
519 		/* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
520 		 * accesses the BO after it's moved.
521 		 */
522 		abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
523 	}
524 
525 	if (adev->mman.buffer_funcs_enabled) {
526 		if (((old_mem->mem_type == TTM_PL_SYSTEM &&
527 		      new_mem->mem_type == TTM_PL_VRAM) ||
528 		     (old_mem->mem_type == TTM_PL_VRAM &&
529 		      new_mem->mem_type == TTM_PL_SYSTEM))) {
530 			hop->fpfn = 0;
531 			hop->lpfn = 0;
532 			hop->mem_type = TTM_PL_TT;
533 			hop->flags = TTM_PL_FLAG_TEMPORARY;
534 			return -EMULTIHOP;
535 		}
536 
537 		r = amdgpu_move_blit(bo, evict, new_mem, old_mem);
538 	} else {
539 		r = -ENODEV;
540 	}
541 
542 	if (r) {
543 		/* Check that all memory is CPU accessible */
544 		if (!amdgpu_mem_visible(adev, old_mem) ||
545 		    !amdgpu_mem_visible(adev, new_mem)) {
546 			pr_err("Move buffer fallback to memcpy unavailable\n");
547 			return r;
548 		}
549 
550 		r = ttm_bo_move_memcpy(bo, ctx, new_mem);
551 		if (r)
552 			return r;
553 	}
554 
555 out:
556 	/* update statistics */
557 	atomic64_add(bo->base.size, &adev->num_bytes_moved);
558 	amdgpu_bo_move_notify(bo, evict, new_mem);
559 	return 0;
560 }
561 
562 /*
563  * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
564  *
565  * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
566  */
567 static int amdgpu_ttm_io_mem_reserve(struct ttm_device *bdev,
568 				     struct ttm_resource *mem)
569 {
570 	struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
571 	size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT;
572 
573 	switch (mem->mem_type) {
574 	case TTM_PL_SYSTEM:
575 		/* system memory */
576 		return 0;
577 	case TTM_PL_TT:
578 	case AMDGPU_PL_PREEMPT:
579 		break;
580 	case TTM_PL_VRAM:
581 		mem->bus.offset = mem->start << PAGE_SHIFT;
582 		/* check if it's visible */
583 		if ((mem->bus.offset + bus_size) > adev->gmc.visible_vram_size)
584 			return -EINVAL;
585 
586 		if (adev->mman.aper_base_kaddr &&
587 		    mem->placement & TTM_PL_FLAG_CONTIGUOUS)
588 			mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
589 					mem->bus.offset;
590 
591 		mem->bus.offset += adev->gmc.aper_base;
592 		mem->bus.is_iomem = true;
593 		break;
594 	default:
595 		return -EINVAL;
596 	}
597 	return 0;
598 }
599 
600 static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
601 					   unsigned long page_offset)
602 {
603 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
604 	struct amdgpu_res_cursor cursor;
605 
606 	amdgpu_res_first(bo->resource, (u64)page_offset << PAGE_SHIFT, 0,
607 			 &cursor);
608 	return (adev->gmc.aper_base + cursor.start) >> PAGE_SHIFT;
609 }
610 
611 /**
612  * amdgpu_ttm_domain_start - Returns GPU start address
613  * @adev: amdgpu device object
614  * @type: type of the memory
615  *
616  * Returns:
617  * GPU start address of a memory domain
618  */
619 
620 uint64_t amdgpu_ttm_domain_start(struct amdgpu_device *adev, uint32_t type)
621 {
622 	switch (type) {
623 	case TTM_PL_TT:
624 		return adev->gmc.gart_start;
625 	case TTM_PL_VRAM:
626 		return adev->gmc.vram_start;
627 	}
628 
629 	return 0;
630 }
631 
632 /*
633  * TTM backend functions.
634  */
635 struct amdgpu_ttm_tt {
636 	struct ttm_tt	ttm;
637 	struct drm_gem_object	*gobj;
638 	u64			offset;
639 	uint64_t		userptr;
640 	struct task_struct	*usertask;
641 	uint32_t		userflags;
642 	bool			bound;
643 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
644 	struct hmm_range	*range;
645 #endif
646 };
647 
648 #define ttm_to_amdgpu_ttm_tt(ptr)	container_of(ptr, struct amdgpu_ttm_tt, ttm)
649 
650 #ifdef CONFIG_DRM_AMDGPU_USERPTR
651 /*
652  * amdgpu_ttm_tt_get_user_pages - get device accessible pages that back user
653  * memory and start HMM tracking CPU page table update
654  *
655  * Calling function must call amdgpu_ttm_tt_userptr_range_done() once and only
656  * once afterwards to stop HMM tracking
657  */
658 int amdgpu_ttm_tt_get_user_pages(struct amdgpu_bo *bo, struct page **pages)
659 {
660 	struct ttm_tt *ttm = bo->tbo.ttm;
661 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
662 	unsigned long start = gtt->userptr;
663 	struct vm_area_struct *vma;
664 	struct mm_struct *mm;
665 	bool readonly;
666 	int r = 0;
667 
668 	mm = bo->notifier.mm;
669 	if (unlikely(!mm)) {
670 		DRM_DEBUG_DRIVER("BO is not registered?\n");
671 		return -EFAULT;
672 	}
673 
674 	/* Another get_user_pages is running at the same time?? */
675 	if (WARN_ON(gtt->range))
676 		return -EFAULT;
677 
678 	if (!mmget_not_zero(mm)) /* Happens during process shutdown */
679 		return -ESRCH;
680 
681 	mmap_read_lock(mm);
682 	vma = vma_lookup(mm, start);
683 	if (unlikely(!vma)) {
684 		r = -EFAULT;
685 		goto out_unlock;
686 	}
687 	if (unlikely((gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) &&
688 		vma->vm_file)) {
689 		r = -EPERM;
690 		goto out_unlock;
691 	}
692 
693 	readonly = amdgpu_ttm_tt_is_readonly(ttm);
694 	r = amdgpu_hmm_range_get_pages(&bo->notifier, mm, pages, start,
695 				       ttm->num_pages, &gtt->range, readonly,
696 				       true, NULL);
697 out_unlock:
698 	mmap_read_unlock(mm);
699 	if (r)
700 		pr_debug("failed %d to get user pages 0x%lx\n", r, start);
701 
702 	mmput(mm);
703 
704 	return r;
705 }
706 
707 /*
708  * amdgpu_ttm_tt_userptr_range_done - stop HMM track the CPU page table change
709  * Check if the pages backing this ttm range have been invalidated
710  *
711  * Returns: true if pages are still valid
712  */
713 bool amdgpu_ttm_tt_get_user_pages_done(struct ttm_tt *ttm)
714 {
715 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
716 	bool r = false;
717 
718 	if (!gtt || !gtt->userptr)
719 		return false;
720 
721 	DRM_DEBUG_DRIVER("user_pages_done 0x%llx pages 0x%x\n",
722 		gtt->userptr, ttm->num_pages);
723 
724 	WARN_ONCE(!gtt->range || !gtt->range->hmm_pfns,
725 		"No user pages to check\n");
726 
727 	if (gtt->range) {
728 		/*
729 		 * FIXME: Must always hold notifier_lock for this, and must
730 		 * not ignore the return code.
731 		 */
732 		r = amdgpu_hmm_range_get_pages_done(gtt->range);
733 		gtt->range = NULL;
734 	}
735 
736 	return !r;
737 }
738 #endif
739 
740 /*
741  * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
742  *
743  * Called by amdgpu_cs_list_validate(). This creates the page list
744  * that backs user memory and will ultimately be mapped into the device
745  * address space.
746  */
747 void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
748 {
749 	unsigned long i;
750 
751 	for (i = 0; i < ttm->num_pages; ++i)
752 		ttm->pages[i] = pages ? pages[i] : NULL;
753 }
754 
755 /*
756  * amdgpu_ttm_tt_pin_userptr - prepare the sg table with the user pages
757  *
758  * Called by amdgpu_ttm_backend_bind()
759  **/
760 static int amdgpu_ttm_tt_pin_userptr(struct ttm_device *bdev,
761 				     struct ttm_tt *ttm)
762 {
763 	struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
764 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
765 	int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
766 	enum dma_data_direction direction = write ?
767 		DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
768 	int r;
769 
770 	/* Allocate an SG array and squash pages into it */
771 	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
772 				      (u64)ttm->num_pages << PAGE_SHIFT,
773 				      GFP_KERNEL);
774 	if (r)
775 		goto release_sg;
776 
777 	/* Map SG to device */
778 	r = dma_map_sgtable(adev->dev, ttm->sg, direction, 0);
779 	if (r)
780 		goto release_sg;
781 
782 	/* convert SG to linear array of pages and dma addresses */
783 	drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
784 				       ttm->num_pages);
785 
786 	return 0;
787 
788 release_sg:
789 	kfree(ttm->sg);
790 	ttm->sg = NULL;
791 	return r;
792 }
793 
794 /*
795  * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
796  */
797 static void amdgpu_ttm_tt_unpin_userptr(struct ttm_device *bdev,
798 					struct ttm_tt *ttm)
799 {
800 	struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
801 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
802 	int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
803 	enum dma_data_direction direction = write ?
804 		DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
805 
806 	/* double check that we don't free the table twice */
807 	if (!ttm->sg || !ttm->sg->sgl)
808 		return;
809 
810 	/* unmap the pages mapped to the device */
811 	dma_unmap_sgtable(adev->dev, ttm->sg, direction, 0);
812 	sg_free_table(ttm->sg);
813 
814 #if IS_ENABLED(CONFIG_DRM_AMDGPU_USERPTR)
815 	if (gtt->range) {
816 		unsigned long i;
817 
818 		for (i = 0; i < ttm->num_pages; i++) {
819 			if (ttm->pages[i] !=
820 			    hmm_pfn_to_page(gtt->range->hmm_pfns[i]))
821 				break;
822 		}
823 
824 		WARN((i == ttm->num_pages), "Missing get_user_page_done\n");
825 	}
826 #endif
827 }
828 
829 static void amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
830 				 struct ttm_buffer_object *tbo,
831 				 uint64_t flags)
832 {
833 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
834 	struct ttm_tt *ttm = tbo->ttm;
835 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
836 
837 	if (amdgpu_bo_encrypted(abo))
838 		flags |= AMDGPU_PTE_TMZ;
839 
840 	if (abo->flags & AMDGPU_GEM_CREATE_CP_MQD_GFX9) {
841 		uint64_t page_idx = 1;
842 
843 		amdgpu_gart_bind(adev, gtt->offset, page_idx,
844 				 gtt->ttm.dma_address, flags);
845 
846 		/* The memory type of the first page defaults to UC. Now
847 		 * modify the memory type to NC from the second page of
848 		 * the BO onward.
849 		 */
850 		flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
851 		flags |= AMDGPU_PTE_MTYPE_VG10(AMDGPU_MTYPE_NC);
852 
853 		amdgpu_gart_bind(adev, gtt->offset + (page_idx << PAGE_SHIFT),
854 				 ttm->num_pages - page_idx,
855 				 &(gtt->ttm.dma_address[page_idx]), flags);
856 	} else {
857 		amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
858 				 gtt->ttm.dma_address, flags);
859 	}
860 }
861 
862 /*
863  * amdgpu_ttm_backend_bind - Bind GTT memory
864  *
865  * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
866  * This handles binding GTT memory to the device address space.
867  */
868 static int amdgpu_ttm_backend_bind(struct ttm_device *bdev,
869 				   struct ttm_tt *ttm,
870 				   struct ttm_resource *bo_mem)
871 {
872 	struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
873 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
874 	uint64_t flags;
875 	int r;
876 
877 	if (!bo_mem)
878 		return -EINVAL;
879 
880 	if (gtt->bound)
881 		return 0;
882 
883 	if (gtt->userptr) {
884 		r = amdgpu_ttm_tt_pin_userptr(bdev, ttm);
885 		if (r) {
886 			DRM_ERROR("failed to pin userptr\n");
887 			return r;
888 		}
889 	} else if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL) {
890 		if (!ttm->sg) {
891 			struct dma_buf_attachment *attach;
892 			struct sg_table *sgt;
893 
894 			attach = gtt->gobj->import_attach;
895 			sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
896 			if (IS_ERR(sgt))
897 				return PTR_ERR(sgt);
898 
899 			ttm->sg = sgt;
900 		}
901 
902 		drm_prime_sg_to_dma_addr_array(ttm->sg, gtt->ttm.dma_address,
903 					       ttm->num_pages);
904 	}
905 
906 	if (!ttm->num_pages) {
907 		WARN(1, "nothing to bind %u pages for mreg %p back %p!\n",
908 		     ttm->num_pages, bo_mem, ttm);
909 	}
910 
911 	if (bo_mem->mem_type != TTM_PL_TT ||
912 	    !amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
913 		gtt->offset = AMDGPU_BO_INVALID_OFFSET;
914 		return 0;
915 	}
916 
917 	/* compute PTE flags relevant to this BO memory */
918 	flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
919 
920 	/* bind pages into GART page tables */
921 	gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
922 	amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
923 			 gtt->ttm.dma_address, flags);
924 	gtt->bound = true;
925 	return 0;
926 }
927 
928 /*
929  * amdgpu_ttm_alloc_gart - Make sure buffer object is accessible either
930  * through AGP or GART aperture.
931  *
932  * If bo is accessible through AGP aperture, then use AGP aperture
933  * to access bo; otherwise allocate logical space in GART aperture
934  * and map bo to GART aperture.
935  */
936 int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
937 {
938 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
939 	struct ttm_operation_ctx ctx = { false, false };
940 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
941 	struct ttm_placement placement;
942 	struct ttm_place placements;
943 	struct ttm_resource *tmp;
944 	uint64_t addr, flags;
945 	int r;
946 
947 	if (bo->resource->start != AMDGPU_BO_INVALID_OFFSET)
948 		return 0;
949 
950 	addr = amdgpu_gmc_agp_addr(bo);
951 	if (addr != AMDGPU_BO_INVALID_OFFSET) {
952 		bo->resource->start = addr >> PAGE_SHIFT;
953 		return 0;
954 	}
955 
956 	/* allocate GART space */
957 	placement.num_placement = 1;
958 	placement.placement = &placements;
959 	placement.num_busy_placement = 1;
960 	placement.busy_placement = &placements;
961 	placements.fpfn = 0;
962 	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
963 	placements.mem_type = TTM_PL_TT;
964 	placements.flags = bo->resource->placement;
965 
966 	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
967 	if (unlikely(r))
968 		return r;
969 
970 	/* compute PTE flags for this buffer object */
971 	flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, tmp);
972 
973 	/* Bind pages */
974 	gtt->offset = (u64)tmp->start << PAGE_SHIFT;
975 	amdgpu_ttm_gart_bind(adev, bo, flags);
976 	amdgpu_gart_invalidate_tlb(adev);
977 	ttm_resource_free(bo, &bo->resource);
978 	ttm_bo_assign_mem(bo, tmp);
979 
980 	return 0;
981 }
982 
983 /*
984  * amdgpu_ttm_recover_gart - Rebind GTT pages
985  *
986  * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
987  * rebind GTT pages during a GPU reset.
988  */
989 void amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
990 {
991 	struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
992 	uint64_t flags;
993 
994 	if (!tbo->ttm)
995 		return;
996 
997 	flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, tbo->resource);
998 	amdgpu_ttm_gart_bind(adev, tbo, flags);
999 }
1000 
1001 /*
1002  * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
1003  *
1004  * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1005  * ttm_tt_destroy().
1006  */
1007 static void amdgpu_ttm_backend_unbind(struct ttm_device *bdev,
1008 				      struct ttm_tt *ttm)
1009 {
1010 	struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1011 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1012 
1013 	/* if the pages have userptr pinning then clear that first */
1014 	if (gtt->userptr) {
1015 		amdgpu_ttm_tt_unpin_userptr(bdev, ttm);
1016 	} else if (ttm->sg && gtt->gobj->import_attach) {
1017 		struct dma_buf_attachment *attach;
1018 
1019 		attach = gtt->gobj->import_attach;
1020 		dma_buf_unmap_attachment(attach, ttm->sg, DMA_BIDIRECTIONAL);
1021 		ttm->sg = NULL;
1022 	}
1023 
1024 	if (!gtt->bound)
1025 		return;
1026 
1027 	if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1028 		return;
1029 
1030 	/* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1031 	amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1032 	gtt->bound = false;
1033 }
1034 
1035 static void amdgpu_ttm_backend_destroy(struct ttm_device *bdev,
1036 				       struct ttm_tt *ttm)
1037 {
1038 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1039 
1040 	if (gtt->usertask)
1041 		put_task_struct(gtt->usertask);
1042 
1043 	ttm_tt_fini(&gtt->ttm);
1044 	kfree(gtt);
1045 }
1046 
1047 /**
1048  * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1049  *
1050  * @bo: The buffer object to create a GTT ttm_tt object around
1051  * @page_flags: Page flags to be added to the ttm_tt object
1052  *
1053  * Called by ttm_tt_create().
1054  */
1055 static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1056 					   uint32_t page_flags)
1057 {
1058 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1059 	struct amdgpu_ttm_tt *gtt;
1060 	enum ttm_caching caching;
1061 
1062 	gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1063 	if (gtt == NULL) {
1064 		return NULL;
1065 	}
1066 	gtt->gobj = &bo->base;
1067 
1068 	if (abo->flags & AMDGPU_GEM_CREATE_CPU_GTT_USWC)
1069 		caching = ttm_write_combined;
1070 	else
1071 		caching = ttm_cached;
1072 
1073 	/* allocate space for the uninitialized page entries */
1074 	if (ttm_sg_tt_init(&gtt->ttm, bo, page_flags, caching)) {
1075 		kfree(gtt);
1076 		return NULL;
1077 	}
1078 	return &gtt->ttm;
1079 }
1080 
1081 /*
1082  * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1083  *
1084  * Map the pages of a ttm_tt object to an address space visible
1085  * to the underlying device.
1086  */
1087 static int amdgpu_ttm_tt_populate(struct ttm_device *bdev,
1088 				  struct ttm_tt *ttm,
1089 				  struct ttm_operation_ctx *ctx)
1090 {
1091 	struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
1092 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1093 	pgoff_t i;
1094 	int ret;
1095 
1096 	/* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1097 	if (gtt->userptr) {
1098 		ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1099 		if (!ttm->sg)
1100 			return -ENOMEM;
1101 		return 0;
1102 	}
1103 
1104 	if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1105 		return 0;
1106 
1107 	ret = ttm_pool_alloc(&adev->mman.bdev.pool, ttm, ctx);
1108 	if (ret)
1109 		return ret;
1110 
1111 	for (i = 0; i < ttm->num_pages; ++i)
1112 		ttm->pages[i]->mapping = bdev->dev_mapping;
1113 
1114 	return 0;
1115 }
1116 
1117 /*
1118  * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1119  *
1120  * Unmaps pages of a ttm_tt object from the device address space and
1121  * unpopulates the page array backing it.
1122  */
1123 static void amdgpu_ttm_tt_unpopulate(struct ttm_device *bdev,
1124 				     struct ttm_tt *ttm)
1125 {
1126 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1127 	struct amdgpu_device *adev;
1128 	pgoff_t i;
1129 
1130 	amdgpu_ttm_backend_unbind(bdev, ttm);
1131 
1132 	if (gtt->userptr) {
1133 		amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1134 		kfree(ttm->sg);
1135 		ttm->sg = NULL;
1136 		return;
1137 	}
1138 
1139 	if (ttm->page_flags & TTM_TT_FLAG_EXTERNAL)
1140 		return;
1141 
1142 	for (i = 0; i < ttm->num_pages; ++i)
1143 		ttm->pages[i]->mapping = NULL;
1144 
1145 	adev = amdgpu_ttm_adev(bdev);
1146 	return ttm_pool_free(&adev->mman.bdev.pool, ttm);
1147 }
1148 
1149 /**
1150  * amdgpu_ttm_tt_get_userptr - Return the userptr GTT ttm_tt for the current
1151  * task
1152  *
1153  * @tbo: The ttm_buffer_object that contains the userptr
1154  * @user_addr:  The returned value
1155  */
1156 int amdgpu_ttm_tt_get_userptr(const struct ttm_buffer_object *tbo,
1157 			      uint64_t *user_addr)
1158 {
1159 	struct amdgpu_ttm_tt *gtt;
1160 
1161 	if (!tbo->ttm)
1162 		return -EINVAL;
1163 
1164 	gtt = (void *)tbo->ttm;
1165 	*user_addr = gtt->userptr;
1166 	return 0;
1167 }
1168 
1169 /**
1170  * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1171  * task
1172  *
1173  * @bo: The ttm_buffer_object to bind this userptr to
1174  * @addr:  The address in the current tasks VM space to use
1175  * @flags: Requirements of userptr object.
1176  *
1177  * Called by amdgpu_gem_userptr_ioctl() to bind userptr pages
1178  * to current task
1179  */
1180 int amdgpu_ttm_tt_set_userptr(struct ttm_buffer_object *bo,
1181 			      uint64_t addr, uint32_t flags)
1182 {
1183 	struct amdgpu_ttm_tt *gtt;
1184 
1185 	if (!bo->ttm) {
1186 		/* TODO: We want a separate TTM object type for userptrs */
1187 		bo->ttm = amdgpu_ttm_tt_create(bo, 0);
1188 		if (bo->ttm == NULL)
1189 			return -ENOMEM;
1190 	}
1191 
1192 	/* Set TTM_TT_FLAG_EXTERNAL before populate but after create. */
1193 	bo->ttm->page_flags |= TTM_TT_FLAG_EXTERNAL;
1194 
1195 	gtt = ttm_to_amdgpu_ttm_tt(bo->ttm);
1196 	gtt->userptr = addr;
1197 	gtt->userflags = flags;
1198 
1199 	if (gtt->usertask)
1200 		put_task_struct(gtt->usertask);
1201 	gtt->usertask = current->group_leader;
1202 	get_task_struct(gtt->usertask);
1203 
1204 	return 0;
1205 }
1206 
1207 /*
1208  * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1209  */
1210 struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1211 {
1212 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1213 
1214 	if (gtt == NULL)
1215 		return NULL;
1216 
1217 	if (gtt->usertask == NULL)
1218 		return NULL;
1219 
1220 	return gtt->usertask->mm;
1221 }
1222 
1223 /*
1224  * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1225  * address range for the current task.
1226  *
1227  */
1228 bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1229 				  unsigned long end, unsigned long *userptr)
1230 {
1231 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1232 	unsigned long size;
1233 
1234 	if (gtt == NULL || !gtt->userptr)
1235 		return false;
1236 
1237 	/* Return false if no part of the ttm_tt object lies within
1238 	 * the range
1239 	 */
1240 	size = (unsigned long)gtt->ttm.num_pages * PAGE_SIZE;
1241 	if (gtt->userptr > end || gtt->userptr + size <= start)
1242 		return false;
1243 
1244 	if (userptr)
1245 		*userptr = gtt->userptr;
1246 	return true;
1247 }
1248 
1249 /*
1250  * amdgpu_ttm_tt_is_userptr - Have the pages backing by userptr?
1251  */
1252 bool amdgpu_ttm_tt_is_userptr(struct ttm_tt *ttm)
1253 {
1254 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1255 
1256 	if (gtt == NULL || !gtt->userptr)
1257 		return false;
1258 
1259 	return true;
1260 }
1261 
1262 /*
1263  * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1264  */
1265 bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1266 {
1267 	struct amdgpu_ttm_tt *gtt = ttm_to_amdgpu_ttm_tt(ttm);
1268 
1269 	if (gtt == NULL)
1270 		return false;
1271 
1272 	return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1273 }
1274 
1275 /**
1276  * amdgpu_ttm_tt_pde_flags - Compute PDE flags for ttm_tt object
1277  *
1278  * @ttm: The ttm_tt object to compute the flags for
1279  * @mem: The memory registry backing this ttm_tt object
1280  *
1281  * Figure out the flags to use for a VM PDE (Page Directory Entry).
1282  */
1283 uint64_t amdgpu_ttm_tt_pde_flags(struct ttm_tt *ttm, struct ttm_resource *mem)
1284 {
1285 	uint64_t flags = 0;
1286 
1287 	if (mem && mem->mem_type != TTM_PL_SYSTEM)
1288 		flags |= AMDGPU_PTE_VALID;
1289 
1290 	if (mem && (mem->mem_type == TTM_PL_TT ||
1291 		    mem->mem_type == AMDGPU_PL_PREEMPT)) {
1292 		flags |= AMDGPU_PTE_SYSTEM;
1293 
1294 		if (ttm->caching == ttm_cached)
1295 			flags |= AMDGPU_PTE_SNOOPED;
1296 	}
1297 
1298 	if (mem && mem->mem_type == TTM_PL_VRAM &&
1299 			mem->bus.caching == ttm_cached)
1300 		flags |= AMDGPU_PTE_SNOOPED;
1301 
1302 	return flags;
1303 }
1304 
1305 /**
1306  * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1307  *
1308  * @adev: amdgpu_device pointer
1309  * @ttm: The ttm_tt object to compute the flags for
1310  * @mem: The memory registry backing this ttm_tt object
1311  *
1312  * Figure out the flags to use for a VM PTE (Page Table Entry).
1313  */
1314 uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1315 				 struct ttm_resource *mem)
1316 {
1317 	uint64_t flags = amdgpu_ttm_tt_pde_flags(ttm, mem);
1318 
1319 	flags |= adev->gart.gart_pte_flags;
1320 	flags |= AMDGPU_PTE_READABLE;
1321 
1322 	if (!amdgpu_ttm_tt_is_readonly(ttm))
1323 		flags |= AMDGPU_PTE_WRITEABLE;
1324 
1325 	return flags;
1326 }
1327 
1328 /*
1329  * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1330  * object.
1331  *
1332  * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1333  * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1334  * it can find space for a new object and by ttm_bo_force_list_clean() which is
1335  * used to clean out a memory space.
1336  */
1337 static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1338 					    const struct ttm_place *place)
1339 {
1340 	struct dma_resv_iter resv_cursor;
1341 	struct dma_fence *f;
1342 
1343 	if (!amdgpu_bo_is_amdgpu_bo(bo))
1344 		return ttm_bo_eviction_valuable(bo, place);
1345 
1346 	/* Swapout? */
1347 	if (bo->resource->mem_type == TTM_PL_SYSTEM)
1348 		return true;
1349 
1350 	if (bo->type == ttm_bo_type_kernel &&
1351 	    !amdgpu_vm_evictable(ttm_to_amdgpu_bo(bo)))
1352 		return false;
1353 
1354 	/* If bo is a KFD BO, check if the bo belongs to the current process.
1355 	 * If true, then return false as any KFD process needs all its BOs to
1356 	 * be resident to run successfully
1357 	 */
1358 	dma_resv_for_each_fence(&resv_cursor, bo->base.resv,
1359 				DMA_RESV_USAGE_BOOKKEEP, f) {
1360 		if (amdkfd_fence_check_mm(f, current->mm))
1361 			return false;
1362 	}
1363 
1364 	/* Preemptible BOs don't own system resources managed by the
1365 	 * driver (pages, VRAM, GART space). They point to resources
1366 	 * owned by someone else (e.g. pageable memory in user mode
1367 	 * or a DMABuf). They are used in a preemptible context so we
1368 	 * can guarantee no deadlocks and good QoS in case of MMU
1369 	 * notifiers or DMABuf move notifiers from the resource owner.
1370 	 */
1371 	if (bo->resource->mem_type == AMDGPU_PL_PREEMPT)
1372 		return false;
1373 
1374 	if (bo->resource->mem_type == TTM_PL_TT &&
1375 	    amdgpu_bo_encrypted(ttm_to_amdgpu_bo(bo)))
1376 		return false;
1377 
1378 	return ttm_bo_eviction_valuable(bo, place);
1379 }
1380 
1381 static void amdgpu_ttm_vram_mm_access(struct amdgpu_device *adev, loff_t pos,
1382 				      void *buf, size_t size, bool write)
1383 {
1384 	while (size) {
1385 		uint64_t aligned_pos = ALIGN_DOWN(pos, 4);
1386 		uint64_t bytes = 4 - (pos & 0x3);
1387 		uint32_t shift = (pos & 0x3) * 8;
1388 		uint32_t mask = 0xffffffff << shift;
1389 		uint32_t value = 0;
1390 
1391 		if (size < bytes) {
1392 			mask &= 0xffffffff >> (bytes - size) * 8;
1393 			bytes = size;
1394 		}
1395 
1396 		if (mask != 0xffffffff) {
1397 			amdgpu_device_mm_access(adev, aligned_pos, &value, 4, false);
1398 			if (write) {
1399 				value &= ~mask;
1400 				value |= (*(uint32_t *)buf << shift) & mask;
1401 				amdgpu_device_mm_access(adev, aligned_pos, &value, 4, true);
1402 			} else {
1403 				value = (value & mask) >> shift;
1404 				memcpy(buf, &value, bytes);
1405 			}
1406 		} else {
1407 			amdgpu_device_mm_access(adev, aligned_pos, buf, 4, write);
1408 		}
1409 
1410 		pos += bytes;
1411 		buf += bytes;
1412 		size -= bytes;
1413 	}
1414 }
1415 
1416 static int amdgpu_ttm_access_memory_sdma(struct ttm_buffer_object *bo,
1417 					unsigned long offset, void *buf, int len, int write)
1418 {
1419 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1420 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1421 	struct amdgpu_res_cursor src_mm;
1422 	struct amdgpu_job *job;
1423 	struct dma_fence *fence;
1424 	uint64_t src_addr, dst_addr;
1425 	unsigned int num_dw;
1426 	int r, idx;
1427 
1428 	if (len != PAGE_SIZE)
1429 		return -EINVAL;
1430 
1431 	if (!adev->mman.sdma_access_ptr)
1432 		return -EACCES;
1433 
1434 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
1435 		return -ENODEV;
1436 
1437 	if (write)
1438 		memcpy(adev->mman.sdma_access_ptr, buf, len);
1439 
1440 	num_dw = ALIGN(adev->mman.buffer_funcs->copy_num_dw, 8);
1441 	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, AMDGPU_IB_POOL_DELAYED, &job);
1442 	if (r)
1443 		goto out;
1444 
1445 	amdgpu_res_first(abo->tbo.resource, offset, len, &src_mm);
1446 	src_addr = amdgpu_ttm_domain_start(adev, bo->resource->mem_type) + src_mm.start;
1447 	dst_addr = amdgpu_bo_gpu_offset(adev->mman.sdma_access_bo);
1448 	if (write)
1449 		swap(src_addr, dst_addr);
1450 
1451 	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr, dst_addr, PAGE_SIZE, false);
1452 
1453 	amdgpu_ring_pad_ib(adev->mman.buffer_funcs_ring, &job->ibs[0]);
1454 	WARN_ON(job->ibs[0].length_dw > num_dw);
1455 
1456 	r = amdgpu_job_submit(job, &adev->mman.entity, AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
1457 	if (r) {
1458 		amdgpu_job_free(job);
1459 		goto out;
1460 	}
1461 
1462 	if (!dma_fence_wait_timeout(fence, false, adev->sdma_timeout))
1463 		r = -ETIMEDOUT;
1464 	dma_fence_put(fence);
1465 
1466 	if (!(r || write))
1467 		memcpy(buf, adev->mman.sdma_access_ptr, len);
1468 out:
1469 	drm_dev_exit(idx);
1470 	return r;
1471 }
1472 
1473 /**
1474  * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1475  *
1476  * @bo:  The buffer object to read/write
1477  * @offset:  Offset into buffer object
1478  * @buf:  Secondary buffer to write/read from
1479  * @len: Length in bytes of access
1480  * @write:  true if writing
1481  *
1482  * This is used to access VRAM that backs a buffer object via MMIO
1483  * access for debugging purposes.
1484  */
1485 static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1486 				    unsigned long offset, void *buf, int len,
1487 				    int write)
1488 {
1489 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1490 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1491 	struct amdgpu_res_cursor cursor;
1492 	int ret = 0;
1493 
1494 	if (bo->resource->mem_type != TTM_PL_VRAM)
1495 		return -EIO;
1496 
1497 	if (amdgpu_device_has_timeouts_enabled(adev) &&
1498 			!amdgpu_ttm_access_memory_sdma(bo, offset, buf, len, write))
1499 		return len;
1500 
1501 	amdgpu_res_first(bo->resource, offset, len, &cursor);
1502 	while (cursor.remaining) {
1503 		size_t count, size = cursor.size;
1504 		loff_t pos = cursor.start;
1505 
1506 		count = amdgpu_device_aper_access(adev, pos, buf, size, write);
1507 		size -= count;
1508 		if (size) {
1509 			/* using MM to access rest vram and handle un-aligned address */
1510 			pos += count;
1511 			buf += count;
1512 			amdgpu_ttm_vram_mm_access(adev, pos, buf, size, write);
1513 		}
1514 
1515 		ret += cursor.size;
1516 		buf += cursor.size;
1517 		amdgpu_res_next(&cursor, cursor.size);
1518 	}
1519 
1520 	return ret;
1521 }
1522 
1523 static void
1524 amdgpu_bo_delete_mem_notify(struct ttm_buffer_object *bo)
1525 {
1526 	amdgpu_bo_move_notify(bo, false, NULL);
1527 }
1528 
1529 static struct ttm_device_funcs amdgpu_bo_driver = {
1530 	.ttm_tt_create = &amdgpu_ttm_tt_create,
1531 	.ttm_tt_populate = &amdgpu_ttm_tt_populate,
1532 	.ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1533 	.ttm_tt_destroy = &amdgpu_ttm_backend_destroy,
1534 	.eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1535 	.evict_flags = &amdgpu_evict_flags,
1536 	.move = &amdgpu_bo_move,
1537 	.delete_mem_notify = &amdgpu_bo_delete_mem_notify,
1538 	.release_notify = &amdgpu_bo_release_notify,
1539 	.io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1540 	.io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1541 	.access_memory = &amdgpu_ttm_access_memory,
1542 };
1543 
1544 /*
1545  * Firmware Reservation functions
1546  */
1547 /**
1548  * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1549  *
1550  * @adev: amdgpu_device pointer
1551  *
1552  * free fw reserved vram if it has been reserved.
1553  */
1554 static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1555 {
1556 	amdgpu_bo_free_kernel(&adev->mman.fw_vram_usage_reserved_bo,
1557 		NULL, &adev->mman.fw_vram_usage_va);
1558 }
1559 
1560 /**
1561  * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1562  *
1563  * @adev: amdgpu_device pointer
1564  *
1565  * create bo vram reservation from fw.
1566  */
1567 static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1568 {
1569 	uint64_t vram_size = adev->gmc.visible_vram_size;
1570 
1571 	adev->mman.fw_vram_usage_va = NULL;
1572 	adev->mman.fw_vram_usage_reserved_bo = NULL;
1573 
1574 	if (adev->mman.fw_vram_usage_size == 0 ||
1575 	    adev->mman.fw_vram_usage_size > vram_size)
1576 		return 0;
1577 
1578 	return amdgpu_bo_create_kernel_at(adev,
1579 					  adev->mman.fw_vram_usage_start_offset,
1580 					  adev->mman.fw_vram_usage_size,
1581 					  AMDGPU_GEM_DOMAIN_VRAM,
1582 					  &adev->mman.fw_vram_usage_reserved_bo,
1583 					  &adev->mman.fw_vram_usage_va);
1584 }
1585 
1586 /*
1587  * Memoy training reservation functions
1588  */
1589 
1590 /**
1591  * amdgpu_ttm_training_reserve_vram_fini - free memory training reserved vram
1592  *
1593  * @adev: amdgpu_device pointer
1594  *
1595  * free memory training reserved vram if it has been reserved.
1596  */
1597 static int amdgpu_ttm_training_reserve_vram_fini(struct amdgpu_device *adev)
1598 {
1599 	struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1600 
1601 	ctx->init = PSP_MEM_TRAIN_NOT_SUPPORT;
1602 	amdgpu_bo_free_kernel(&ctx->c2p_bo, NULL, NULL);
1603 	ctx->c2p_bo = NULL;
1604 
1605 	return 0;
1606 }
1607 
1608 static void amdgpu_ttm_training_data_block_init(struct amdgpu_device *adev)
1609 {
1610 	struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1611 
1612 	memset(ctx, 0, sizeof(*ctx));
1613 
1614 	ctx->c2p_train_data_offset =
1615 		ALIGN((adev->gmc.mc_vram_size - adev->mman.discovery_tmr_size - SZ_1M), SZ_1M);
1616 	ctx->p2c_train_data_offset =
1617 		(adev->gmc.mc_vram_size - GDDR6_MEM_TRAINING_OFFSET);
1618 	ctx->train_data_size =
1619 		GDDR6_MEM_TRAINING_DATA_SIZE_IN_BYTES;
1620 
1621 	DRM_DEBUG("train_data_size:%llx,p2c_train_data_offset:%llx,c2p_train_data_offset:%llx.\n",
1622 			ctx->train_data_size,
1623 			ctx->p2c_train_data_offset,
1624 			ctx->c2p_train_data_offset);
1625 }
1626 
1627 /*
1628  * reserve TMR memory at the top of VRAM which holds
1629  * IP Discovery data and is protected by PSP.
1630  */
1631 static int amdgpu_ttm_reserve_tmr(struct amdgpu_device *adev)
1632 {
1633 	int ret;
1634 	struct psp_memory_training_context *ctx = &adev->psp.mem_train_ctx;
1635 	bool mem_train_support = false;
1636 
1637 	if (!amdgpu_sriov_vf(adev)) {
1638 		if (amdgpu_atomfirmware_mem_training_supported(adev))
1639 			mem_train_support = true;
1640 		else
1641 			DRM_DEBUG("memory training does not support!\n");
1642 	}
1643 
1644 	/*
1645 	 * Query reserved tmr size through atom firmwareinfo for Sienna_Cichlid and onwards for all
1646 	 * the use cases (IP discovery/G6 memory training/profiling/diagnostic data.etc)
1647 	 *
1648 	 * Otherwise, fallback to legacy approach to check and reserve tmr block for ip
1649 	 * discovery data and G6 memory training data respectively
1650 	 */
1651 	adev->mman.discovery_tmr_size =
1652 		amdgpu_atomfirmware_get_fw_reserved_fb_size(adev);
1653 	if (!adev->mman.discovery_tmr_size)
1654 		adev->mman.discovery_tmr_size = DISCOVERY_TMR_OFFSET;
1655 
1656 	if (mem_train_support) {
1657 		/* reserve vram for mem train according to TMR location */
1658 		amdgpu_ttm_training_data_block_init(adev);
1659 		ret = amdgpu_bo_create_kernel_at(adev,
1660 					 ctx->c2p_train_data_offset,
1661 					 ctx->train_data_size,
1662 					 AMDGPU_GEM_DOMAIN_VRAM,
1663 					 &ctx->c2p_bo,
1664 					 NULL);
1665 		if (ret) {
1666 			DRM_ERROR("alloc c2p_bo failed(%d)!\n", ret);
1667 			amdgpu_ttm_training_reserve_vram_fini(adev);
1668 			return ret;
1669 		}
1670 		ctx->init = PSP_MEM_TRAIN_RESERVE_SUCCESS;
1671 	}
1672 
1673 	ret = amdgpu_bo_create_kernel_at(adev,
1674 				adev->gmc.real_vram_size - adev->mman.discovery_tmr_size,
1675 				adev->mman.discovery_tmr_size,
1676 				AMDGPU_GEM_DOMAIN_VRAM,
1677 				&adev->mman.discovery_memory,
1678 				NULL);
1679 	if (ret) {
1680 		DRM_ERROR("alloc tmr failed(%d)!\n", ret);
1681 		amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1682 		return ret;
1683 	}
1684 
1685 	return 0;
1686 }
1687 
1688 /*
1689  * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1690  * gtt/vram related fields.
1691  *
1692  * This initializes all of the memory space pools that the TTM layer
1693  * will need such as the GTT space (system memory mapped to the device),
1694  * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1695  * can be mapped per VMID.
1696  */
1697 int amdgpu_ttm_init(struct amdgpu_device *adev)
1698 {
1699 	uint64_t gtt_size;
1700 	int r;
1701 	u64 vis_vram_limit;
1702 
1703 	mutex_init(&adev->mman.gtt_window_lock);
1704 
1705 	/* No others user of address space so set it to 0 */
1706 	r = ttm_device_init(&adev->mman.bdev, &amdgpu_bo_driver, adev->dev,
1707 			       adev_to_drm(adev)->anon_inode->i_mapping,
1708 			       adev_to_drm(adev)->vma_offset_manager,
1709 			       adev->need_swiotlb,
1710 			       dma_addressing_limited(adev->dev));
1711 	if (r) {
1712 		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1713 		return r;
1714 	}
1715 	adev->mman.initialized = true;
1716 
1717 	/* Initialize VRAM pool with all of VRAM divided into pages */
1718 	r = amdgpu_vram_mgr_init(adev);
1719 	if (r) {
1720 		DRM_ERROR("Failed initializing VRAM heap.\n");
1721 		return r;
1722 	}
1723 
1724 	/* Reduce size of CPU-visible VRAM if requested */
1725 	vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1726 	if (amdgpu_vis_vram_limit > 0 &&
1727 	    vis_vram_limit <= adev->gmc.visible_vram_size)
1728 		adev->gmc.visible_vram_size = vis_vram_limit;
1729 
1730 	/* Change the size here instead of the init above so only lpfn is affected */
1731 	amdgpu_ttm_set_buffer_funcs_status(adev, false);
1732 #ifdef CONFIG_64BIT
1733 #ifdef CONFIG_X86
1734 	if (adev->gmc.xgmi.connected_to_cpu)
1735 		adev->mman.aper_base_kaddr = ioremap_cache(adev->gmc.aper_base,
1736 				adev->gmc.visible_vram_size);
1737 
1738 	else
1739 #endif
1740 		adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1741 				adev->gmc.visible_vram_size);
1742 #endif
1743 
1744 	/*
1745 	 *The reserved vram for firmware must be pinned to the specified
1746 	 *place on the VRAM, so reserve it early.
1747 	 */
1748 	r = amdgpu_ttm_fw_reserve_vram_init(adev);
1749 	if (r) {
1750 		return r;
1751 	}
1752 
1753 	/*
1754 	 * only NAVI10 and onwards ASIC support for IP discovery.
1755 	 * If IP discovery enabled, a block of memory should be
1756 	 * reserved for IP discovey.
1757 	 */
1758 	if (adev->mman.discovery_bin) {
1759 		r = amdgpu_ttm_reserve_tmr(adev);
1760 		if (r)
1761 			return r;
1762 	}
1763 
1764 	/* allocate memory as required for VGA
1765 	 * This is used for VGA emulation and pre-OS scanout buffers to
1766 	 * avoid display artifacts while transitioning between pre-OS
1767 	 * and driver.  */
1768 	r = amdgpu_bo_create_kernel_at(adev, 0, adev->mman.stolen_vga_size,
1769 				       AMDGPU_GEM_DOMAIN_VRAM,
1770 				       &adev->mman.stolen_vga_memory,
1771 				       NULL);
1772 	if (r)
1773 		return r;
1774 	r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_vga_size,
1775 				       adev->mman.stolen_extended_size,
1776 				       AMDGPU_GEM_DOMAIN_VRAM,
1777 				       &adev->mman.stolen_extended_memory,
1778 				       NULL);
1779 	if (r)
1780 		return r;
1781 	r = amdgpu_bo_create_kernel_at(adev, adev->mman.stolen_reserved_offset,
1782 				       adev->mman.stolen_reserved_size,
1783 				       AMDGPU_GEM_DOMAIN_VRAM,
1784 				       &adev->mman.stolen_reserved_memory,
1785 				       NULL);
1786 	if (r)
1787 		return r;
1788 
1789 	DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1790 		 (unsigned) (adev->gmc.real_vram_size / (1024 * 1024)));
1791 
1792 	/* Compute GTT size, either based on 1/2 the size of RAM size
1793 	 * or whatever the user passed on module init */
1794 	if (amdgpu_gtt_size == -1) {
1795 		struct sysinfo si;
1796 
1797 		si_meminfo(&si);
1798 		/* Certain GL unit tests for large textures can cause problems
1799 		 * with the OOM killer since there is no way to link this memory
1800 		 * to a process.  This was originally mitigated (but not necessarily
1801 		 * eliminated) by limiting the GTT size.  The problem is this limit
1802 		 * is often too low for many modern games so just make the limit 1/2
1803 		 * of system memory which aligns with TTM. The OOM accounting needs
1804 		 * to be addressed, but we shouldn't prevent common 3D applications
1805 		 * from being usable just to potentially mitigate that corner case.
1806 		 */
1807 		gtt_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1808 			       (u64)si.totalram * si.mem_unit / 2);
1809 	} else {
1810 		gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1811 	}
1812 
1813 	/* Initialize GTT memory pool */
1814 	r = amdgpu_gtt_mgr_init(adev, gtt_size);
1815 	if (r) {
1816 		DRM_ERROR("Failed initializing GTT heap.\n");
1817 		return r;
1818 	}
1819 	DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1820 		 (unsigned)(gtt_size / (1024 * 1024)));
1821 
1822 	/* Initialize preemptible memory pool */
1823 	r = amdgpu_preempt_mgr_init(adev);
1824 	if (r) {
1825 		DRM_ERROR("Failed initializing PREEMPT heap.\n");
1826 		return r;
1827 	}
1828 
1829 	/* Initialize various on-chip memory pools */
1830 	r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GDS, adev->gds.gds_size);
1831 	if (r) {
1832 		DRM_ERROR("Failed initializing GDS heap.\n");
1833 		return r;
1834 	}
1835 
1836 	r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_GWS, adev->gds.gws_size);
1837 	if (r) {
1838 		DRM_ERROR("Failed initializing gws heap.\n");
1839 		return r;
1840 	}
1841 
1842 	r = amdgpu_ttm_init_on_chip(adev, AMDGPU_PL_OA, adev->gds.oa_size);
1843 	if (r) {
1844 		DRM_ERROR("Failed initializing oa heap.\n");
1845 		return r;
1846 	}
1847 
1848 	if (amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
1849 				AMDGPU_GEM_DOMAIN_GTT,
1850 				&adev->mman.sdma_access_bo, NULL,
1851 				&adev->mman.sdma_access_ptr))
1852 		DRM_WARN("Debug VRAM access will use slowpath MM access\n");
1853 
1854 	return 0;
1855 }
1856 
1857 /*
1858  * amdgpu_ttm_fini - De-initialize the TTM memory pools
1859  */
1860 void amdgpu_ttm_fini(struct amdgpu_device *adev)
1861 {
1862 	int idx;
1863 	if (!adev->mman.initialized)
1864 		return;
1865 
1866 	amdgpu_ttm_training_reserve_vram_fini(adev);
1867 	/* return the stolen vga memory back to VRAM */
1868 	amdgpu_bo_free_kernel(&adev->mman.stolen_vga_memory, NULL, NULL);
1869 	amdgpu_bo_free_kernel(&adev->mman.stolen_extended_memory, NULL, NULL);
1870 	/* return the IP Discovery TMR memory back to VRAM */
1871 	amdgpu_bo_free_kernel(&adev->mman.discovery_memory, NULL, NULL);
1872 	if (adev->mman.stolen_reserved_size)
1873 		amdgpu_bo_free_kernel(&adev->mman.stolen_reserved_memory,
1874 				      NULL, NULL);
1875 	amdgpu_bo_free_kernel(&adev->mman.sdma_access_bo, NULL,
1876 					&adev->mman.sdma_access_ptr);
1877 	amdgpu_ttm_fw_reserve_vram_fini(adev);
1878 
1879 	if (drm_dev_enter(adev_to_drm(adev), &idx)) {
1880 
1881 		if (adev->mman.aper_base_kaddr)
1882 			iounmap(adev->mman.aper_base_kaddr);
1883 		adev->mman.aper_base_kaddr = NULL;
1884 
1885 		drm_dev_exit(idx);
1886 	}
1887 
1888 	amdgpu_vram_mgr_fini(adev);
1889 	amdgpu_gtt_mgr_fini(adev);
1890 	amdgpu_preempt_mgr_fini(adev);
1891 	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GDS);
1892 	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_GWS);
1893 	ttm_range_man_fini(&adev->mman.bdev, AMDGPU_PL_OA);
1894 	ttm_device_fini(&adev->mman.bdev);
1895 	adev->mman.initialized = false;
1896 	DRM_INFO("amdgpu: ttm finalized\n");
1897 }
1898 
1899 /**
1900  * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
1901  *
1902  * @adev: amdgpu_device pointer
1903  * @enable: true when we can use buffer functions.
1904  *
1905  * Enable/disable use of buffer functions during suspend/resume. This should
1906  * only be called at bootup or when userspace isn't running.
1907  */
1908 void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
1909 {
1910 	struct ttm_resource_manager *man = ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
1911 	uint64_t size;
1912 	int r;
1913 
1914 	if (!adev->mman.initialized || amdgpu_in_reset(adev) ||
1915 	    adev->mman.buffer_funcs_enabled == enable)
1916 		return;
1917 
1918 	if (enable) {
1919 		struct amdgpu_ring *ring;
1920 		struct drm_gpu_scheduler *sched;
1921 
1922 		ring = adev->mman.buffer_funcs_ring;
1923 		sched = &ring->sched;
1924 		r = drm_sched_entity_init(&adev->mman.entity,
1925 					  DRM_SCHED_PRIORITY_KERNEL, &sched,
1926 					  1, NULL);
1927 		if (r) {
1928 			DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
1929 				  r);
1930 			return;
1931 		}
1932 	} else {
1933 		drm_sched_entity_destroy(&adev->mman.entity);
1934 		dma_fence_put(man->move);
1935 		man->move = NULL;
1936 	}
1937 
1938 	/* this just adjusts TTM size idea, which sets lpfn to the correct value */
1939 	if (enable)
1940 		size = adev->gmc.real_vram_size;
1941 	else
1942 		size = adev->gmc.visible_vram_size;
1943 	man->size = size;
1944 	adev->mman.buffer_funcs_enabled = enable;
1945 }
1946 
1947 static int amdgpu_ttm_prepare_job(struct amdgpu_device *adev,
1948 				  bool direct_submit,
1949 				  unsigned int num_dw,
1950 				  struct dma_resv *resv,
1951 				  bool vm_needs_flush,
1952 				  struct amdgpu_job **job)
1953 {
1954 	enum amdgpu_ib_pool_type pool = direct_submit ?
1955 		AMDGPU_IB_POOL_DIRECT :
1956 		AMDGPU_IB_POOL_DELAYED;
1957 	int r;
1958 
1959 	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, pool, job);
1960 	if (r)
1961 		return r;
1962 
1963 	if (vm_needs_flush) {
1964 		(*job)->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gmc.pdb0_bo ?
1965 							adev->gmc.pdb0_bo :
1966 							adev->gart.bo);
1967 		(*job)->vm_needs_flush = true;
1968 	}
1969 	if (resv) {
1970 		r = amdgpu_sync_resv(adev, &(*job)->sync, resv,
1971 				     AMDGPU_SYNC_ALWAYS,
1972 				     AMDGPU_FENCE_OWNER_UNDEFINED);
1973 		if (r) {
1974 			DRM_ERROR("sync failed (%d).\n", r);
1975 			amdgpu_job_free(*job);
1976 			return r;
1977 		}
1978 	}
1979 	return 0;
1980 }
1981 
1982 int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
1983 		       uint64_t dst_offset, uint32_t byte_count,
1984 		       struct dma_resv *resv,
1985 		       struct dma_fence **fence, bool direct_submit,
1986 		       bool vm_needs_flush, bool tmz)
1987 {
1988 	struct amdgpu_device *adev = ring->adev;
1989 	unsigned num_loops, num_dw;
1990 	struct amdgpu_job *job;
1991 	uint32_t max_bytes;
1992 	unsigned i;
1993 	int r;
1994 
1995 	if (!direct_submit && !ring->sched.ready) {
1996 		DRM_ERROR("Trying to move memory with ring turned off.\n");
1997 		return -EINVAL;
1998 	}
1999 
2000 	max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
2001 	num_loops = DIV_ROUND_UP(byte_count, max_bytes);
2002 	num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->copy_num_dw, 8);
2003 	r = amdgpu_ttm_prepare_job(adev, direct_submit, num_dw,
2004 				   resv, vm_needs_flush, &job);
2005 	if (r)
2006 		return r;
2007 
2008 	for (i = 0; i < num_loops; i++) {
2009 		uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2010 
2011 		amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2012 					dst_offset, cur_size_in_bytes, tmz);
2013 
2014 		src_offset += cur_size_in_bytes;
2015 		dst_offset += cur_size_in_bytes;
2016 		byte_count -= cur_size_in_bytes;
2017 	}
2018 
2019 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2020 	WARN_ON(job->ibs[0].length_dw > num_dw);
2021 	if (direct_submit)
2022 		r = amdgpu_job_submit_direct(job, ring, fence);
2023 	else
2024 		r = amdgpu_job_submit(job, &adev->mman.entity,
2025 				      AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2026 	if (r)
2027 		goto error_free;
2028 
2029 	return r;
2030 
2031 error_free:
2032 	amdgpu_job_free(job);
2033 	DRM_ERROR("Error scheduling IBs (%d)\n", r);
2034 	return r;
2035 }
2036 
2037 static int amdgpu_ttm_fill_mem(struct amdgpu_ring *ring, uint32_t src_data,
2038 			       uint64_t dst_addr, uint32_t byte_count,
2039 			       struct dma_resv *resv,
2040 			       struct dma_fence **fence,
2041 			       bool vm_needs_flush)
2042 {
2043 	struct amdgpu_device *adev = ring->adev;
2044 	unsigned int num_loops, num_dw;
2045 	struct amdgpu_job *job;
2046 	uint32_t max_bytes;
2047 	unsigned int i;
2048 	int r;
2049 
2050 	max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2051 	num_loops = DIV_ROUND_UP_ULL(byte_count, max_bytes);
2052 	num_dw = ALIGN(num_loops * adev->mman.buffer_funcs->fill_num_dw, 8);
2053 	r = amdgpu_ttm_prepare_job(adev, false, num_dw, resv, vm_needs_flush,
2054 				   &job);
2055 	if (r)
2056 		return r;
2057 
2058 	for (i = 0; i < num_loops; i++) {
2059 		uint32_t cur_size = min(byte_count, max_bytes);
2060 
2061 		amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data, dst_addr,
2062 					cur_size);
2063 
2064 		dst_addr += cur_size;
2065 		byte_count -= cur_size;
2066 	}
2067 
2068 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2069 	WARN_ON(job->ibs[0].length_dw > num_dw);
2070 	r = amdgpu_job_submit(job, &adev->mman.entity,
2071 			      AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2072 	if (r)
2073 		goto error_free;
2074 
2075 	return 0;
2076 
2077 error_free:
2078 	amdgpu_job_free(job);
2079 	return r;
2080 }
2081 
2082 int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2083 			uint32_t src_data,
2084 			struct dma_resv *resv,
2085 			struct dma_fence **f)
2086 {
2087 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2088 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2089 	struct dma_fence *fence = NULL;
2090 	struct amdgpu_res_cursor dst;
2091 	int r;
2092 
2093 	if (!adev->mman.buffer_funcs_enabled) {
2094 		DRM_ERROR("Trying to clear memory with ring turned off.\n");
2095 		return -EINVAL;
2096 	}
2097 
2098 	amdgpu_res_first(bo->tbo.resource, 0, amdgpu_bo_size(bo), &dst);
2099 
2100 	mutex_lock(&adev->mman.gtt_window_lock);
2101 	while (dst.remaining) {
2102 		struct dma_fence *next;
2103 		uint64_t cur_size, to;
2104 
2105 		/* Never fill more than 256MiB at once to avoid timeouts */
2106 		cur_size = min(dst.size, 256ULL << 20);
2107 
2108 		r = amdgpu_ttm_map_buffer(&bo->tbo, bo->tbo.resource, &dst,
2109 					  1, ring, false, &cur_size, &to);
2110 		if (r)
2111 			goto error;
2112 
2113 		r = amdgpu_ttm_fill_mem(ring, src_data, to, cur_size, resv,
2114 					&next, true);
2115 		if (r)
2116 			goto error;
2117 
2118 		dma_fence_put(fence);
2119 		fence = next;
2120 
2121 		amdgpu_res_next(&dst, cur_size);
2122 	}
2123 error:
2124 	mutex_unlock(&adev->mman.gtt_window_lock);
2125 	if (f)
2126 		*f = dma_fence_get(fence);
2127 	dma_fence_put(fence);
2128 	return r;
2129 }
2130 
2131 /**
2132  * amdgpu_ttm_evict_resources - evict memory buffers
2133  * @adev: amdgpu device object
2134  * @mem_type: evicted BO's memory type
2135  *
2136  * Evicts all @mem_type buffers on the lru list of the memory type.
2137  *
2138  * Returns:
2139  * 0 for success or a negative error code on failure.
2140  */
2141 int amdgpu_ttm_evict_resources(struct amdgpu_device *adev, int mem_type)
2142 {
2143 	struct ttm_resource_manager *man;
2144 
2145 	switch (mem_type) {
2146 	case TTM_PL_VRAM:
2147 	case TTM_PL_TT:
2148 	case AMDGPU_PL_GWS:
2149 	case AMDGPU_PL_GDS:
2150 	case AMDGPU_PL_OA:
2151 		man = ttm_manager_type(&adev->mman.bdev, mem_type);
2152 		break;
2153 	default:
2154 		DRM_ERROR("Trying to evict invalid memory type\n");
2155 		return -EINVAL;
2156 	}
2157 
2158 	return ttm_resource_manager_evict_all(&adev->mman.bdev, man);
2159 }
2160 
2161 #if defined(CONFIG_DEBUG_FS)
2162 
2163 static int amdgpu_ttm_page_pool_show(struct seq_file *m, void *unused)
2164 {
2165 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
2166 
2167 	return ttm_pool_debugfs(&adev->mman.bdev.pool, m);
2168 }
2169 
2170 DEFINE_SHOW_ATTRIBUTE(amdgpu_ttm_page_pool);
2171 
2172 /*
2173  * amdgpu_ttm_vram_read - Linear read access to VRAM
2174  *
2175  * Accesses VRAM via MMIO for debugging purposes.
2176  */
2177 static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2178 				    size_t size, loff_t *pos)
2179 {
2180 	struct amdgpu_device *adev = file_inode(f)->i_private;
2181 	ssize_t result = 0;
2182 
2183 	if (size & 0x3 || *pos & 0x3)
2184 		return -EINVAL;
2185 
2186 	if (*pos >= adev->gmc.mc_vram_size)
2187 		return -ENXIO;
2188 
2189 	size = min(size, (size_t)(adev->gmc.mc_vram_size - *pos));
2190 	while (size) {
2191 		size_t bytes = min(size, AMDGPU_TTM_VRAM_MAX_DW_READ * 4);
2192 		uint32_t value[AMDGPU_TTM_VRAM_MAX_DW_READ];
2193 
2194 		amdgpu_device_vram_access(adev, *pos, value, bytes, false);
2195 		if (copy_to_user(buf, value, bytes))
2196 			return -EFAULT;
2197 
2198 		result += bytes;
2199 		buf += bytes;
2200 		*pos += bytes;
2201 		size -= bytes;
2202 	}
2203 
2204 	return result;
2205 }
2206 
2207 /*
2208  * amdgpu_ttm_vram_write - Linear write access to VRAM
2209  *
2210  * Accesses VRAM via MMIO for debugging purposes.
2211  */
2212 static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2213 				    size_t size, loff_t *pos)
2214 {
2215 	struct amdgpu_device *adev = file_inode(f)->i_private;
2216 	ssize_t result = 0;
2217 	int r;
2218 
2219 	if (size & 0x3 || *pos & 0x3)
2220 		return -EINVAL;
2221 
2222 	if (*pos >= adev->gmc.mc_vram_size)
2223 		return -ENXIO;
2224 
2225 	while (size) {
2226 		uint32_t value;
2227 
2228 		if (*pos >= adev->gmc.mc_vram_size)
2229 			return result;
2230 
2231 		r = get_user(value, (uint32_t *)buf);
2232 		if (r)
2233 			return r;
2234 
2235 		amdgpu_device_mm_access(adev, *pos, &value, 4, true);
2236 
2237 		result += 4;
2238 		buf += 4;
2239 		*pos += 4;
2240 		size -= 4;
2241 	}
2242 
2243 	return result;
2244 }
2245 
2246 static const struct file_operations amdgpu_ttm_vram_fops = {
2247 	.owner = THIS_MODULE,
2248 	.read = amdgpu_ttm_vram_read,
2249 	.write = amdgpu_ttm_vram_write,
2250 	.llseek = default_llseek,
2251 };
2252 
2253 /*
2254  * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2255  *
2256  * This function is used to read memory that has been mapped to the
2257  * GPU and the known addresses are not physical addresses but instead
2258  * bus addresses (e.g., what you'd put in an IB or ring buffer).
2259  */
2260 static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2261 				 size_t size, loff_t *pos)
2262 {
2263 	struct amdgpu_device *adev = file_inode(f)->i_private;
2264 	struct iommu_domain *dom;
2265 	ssize_t result = 0;
2266 	int r;
2267 
2268 	/* retrieve the IOMMU domain if any for this device */
2269 	dom = iommu_get_domain_for_dev(adev->dev);
2270 
2271 	while (size) {
2272 		phys_addr_t addr = *pos & PAGE_MASK;
2273 		loff_t off = *pos & ~PAGE_MASK;
2274 		size_t bytes = PAGE_SIZE - off;
2275 		unsigned long pfn;
2276 		struct page *p;
2277 		void *ptr;
2278 
2279 		bytes = bytes < size ? bytes : size;
2280 
2281 		/* Translate the bus address to a physical address.  If
2282 		 * the domain is NULL it means there is no IOMMU active
2283 		 * and the address translation is the identity
2284 		 */
2285 		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2286 
2287 		pfn = addr >> PAGE_SHIFT;
2288 		if (!pfn_valid(pfn))
2289 			return -EPERM;
2290 
2291 		p = pfn_to_page(pfn);
2292 		if (p->mapping != adev->mman.bdev.dev_mapping)
2293 			return -EPERM;
2294 
2295 		ptr = kmap(p);
2296 		r = copy_to_user(buf, ptr + off, bytes);
2297 		kunmap(p);
2298 		if (r)
2299 			return -EFAULT;
2300 
2301 		size -= bytes;
2302 		*pos += bytes;
2303 		result += bytes;
2304 	}
2305 
2306 	return result;
2307 }
2308 
2309 /*
2310  * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2311  *
2312  * This function is used to write memory that has been mapped to the
2313  * GPU and the known addresses are not physical addresses but instead
2314  * bus addresses (e.g., what you'd put in an IB or ring buffer).
2315  */
2316 static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
2317 				 size_t size, loff_t *pos)
2318 {
2319 	struct amdgpu_device *adev = file_inode(f)->i_private;
2320 	struct iommu_domain *dom;
2321 	ssize_t result = 0;
2322 	int r;
2323 
2324 	dom = iommu_get_domain_for_dev(adev->dev);
2325 
2326 	while (size) {
2327 		phys_addr_t addr = *pos & PAGE_MASK;
2328 		loff_t off = *pos & ~PAGE_MASK;
2329 		size_t bytes = PAGE_SIZE - off;
2330 		unsigned long pfn;
2331 		struct page *p;
2332 		void *ptr;
2333 
2334 		bytes = bytes < size ? bytes : size;
2335 
2336 		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2337 
2338 		pfn = addr >> PAGE_SHIFT;
2339 		if (!pfn_valid(pfn))
2340 			return -EPERM;
2341 
2342 		p = pfn_to_page(pfn);
2343 		if (p->mapping != adev->mman.bdev.dev_mapping)
2344 			return -EPERM;
2345 
2346 		ptr = kmap(p);
2347 		r = copy_from_user(ptr + off, buf, bytes);
2348 		kunmap(p);
2349 		if (r)
2350 			return -EFAULT;
2351 
2352 		size -= bytes;
2353 		*pos += bytes;
2354 		result += bytes;
2355 	}
2356 
2357 	return result;
2358 }
2359 
2360 static const struct file_operations amdgpu_ttm_iomem_fops = {
2361 	.owner = THIS_MODULE,
2362 	.read = amdgpu_iomem_read,
2363 	.write = amdgpu_iomem_write,
2364 	.llseek = default_llseek
2365 };
2366 
2367 #endif
2368 
2369 void amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2370 {
2371 #if defined(CONFIG_DEBUG_FS)
2372 	struct drm_minor *minor = adev_to_drm(adev)->primary;
2373 	struct dentry *root = minor->debugfs_root;
2374 
2375 	debugfs_create_file_size("amdgpu_vram", 0444, root, adev,
2376 				 &amdgpu_ttm_vram_fops, adev->gmc.mc_vram_size);
2377 	debugfs_create_file("amdgpu_iomem", 0444, root, adev,
2378 			    &amdgpu_ttm_iomem_fops);
2379 	debugfs_create_file("ttm_page_pool", 0444, root, adev,
2380 			    &amdgpu_ttm_page_pool_fops);
2381 	ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2382 							     TTM_PL_VRAM),
2383 					    root, "amdgpu_vram_mm");
2384 	ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2385 							     TTM_PL_TT),
2386 					    root, "amdgpu_gtt_mm");
2387 	ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2388 							     AMDGPU_PL_GDS),
2389 					    root, "amdgpu_gds_mm");
2390 	ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2391 							     AMDGPU_PL_GWS),
2392 					    root, "amdgpu_gws_mm");
2393 	ttm_resource_manager_create_debugfs(ttm_manager_type(&adev->mman.bdev,
2394 							     AMDGPU_PL_OA),
2395 					    root, "amdgpu_oa_mm");
2396 
2397 #endif
2398 }
2399