xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_ttm.c (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2009 Jerome Glisse.
3b843c749SSergey Zigachev  * All Rights Reserved.
4b843c749SSergey Zigachev  *
5b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
6b843c749SSergey Zigachev  * copy of this software and associated documentation files (the
7b843c749SSergey Zigachev  * "Software"), to deal in the Software without restriction, including
8b843c749SSergey Zigachev  * without limitation the rights to use, copy, modify, merge, publish,
9b843c749SSergey Zigachev  * distribute, sub license, and/or sell copies of the Software, and to
10b843c749SSergey Zigachev  * permit persons to whom the Software is furnished to do so, subject to
11b843c749SSergey Zigachev  * the following conditions:
12b843c749SSergey Zigachev  *
13b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16b843c749SSergey Zigachev  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17b843c749SSergey Zigachev  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18b843c749SSergey Zigachev  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19b843c749SSergey Zigachev  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20b843c749SSergey Zigachev  *
21b843c749SSergey Zigachev  * The above copyright notice and this permission notice (including the
22b843c749SSergey Zigachev  * next paragraph) shall be included in all copies or substantial portions
23b843c749SSergey Zigachev  * of the Software.
24b843c749SSergey Zigachev  *
25b843c749SSergey Zigachev  */
26b843c749SSergey Zigachev /*
27b843c749SSergey Zigachev  * Authors:
28b843c749SSergey Zigachev  *    Jerome Glisse <glisse@freedesktop.org>
29b843c749SSergey Zigachev  *    Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
30b843c749SSergey Zigachev  *    Dave Airlie
31b843c749SSergey Zigachev  */
32b843c749SSergey Zigachev #include <drm/ttm/ttm_bo_api.h>
33b843c749SSergey Zigachev #include <drm/ttm/ttm_bo_driver.h>
34b843c749SSergey Zigachev #include <drm/ttm/ttm_placement.h>
35b843c749SSergey Zigachev #include <drm/ttm/ttm_module.h>
36b843c749SSergey Zigachev #include <drm/ttm/ttm_page_alloc.h>
37b843c749SSergey Zigachev #include <drm/drmP.h>
38b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
39b843c749SSergey Zigachev #include <linux/seq_file.h>
40b843c749SSergey Zigachev #include <linux/slab.h>
41b843c749SSergey Zigachev #include <linux/swiotlb.h>
42b843c749SSergey Zigachev #include <linux/swap.h>
43b843c749SSergey Zigachev #include <linux/pagemap.h>
44b843c749SSergey Zigachev #include <linux/debugfs.h>
45*78973132SSergey Zigachev #if 0
46b843c749SSergey Zigachev #include <linux/iommu.h>
47*78973132SSergey Zigachev #endif
48b843c749SSergey Zigachev #include "amdgpu.h"
49b843c749SSergey Zigachev #include "amdgpu_object.h"
50b843c749SSergey Zigachev #include "amdgpu_trace.h"
51b843c749SSergey Zigachev #include "amdgpu_amdkfd.h"
52b843c749SSergey Zigachev #include "bif/bif_4_1_d.h"
53b843c749SSergey Zigachev 
54*78973132SSergey Zigachev #include <linux/pfn_t.h>
55*78973132SSergey Zigachev 
56b843c749SSergey Zigachev #define DRM_FILE_PAGE_OFFSET (0x100000000ULL >> PAGE_SHIFT)
57b843c749SSergey Zigachev 
58b843c749SSergey Zigachev static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
59b843c749SSergey Zigachev 			     struct ttm_mem_reg *mem, unsigned num_pages,
60b843c749SSergey Zigachev 			     uint64_t offset, unsigned window,
61b843c749SSergey Zigachev 			     struct amdgpu_ring *ring,
62b843c749SSergey Zigachev 			     uint64_t *addr);
63b843c749SSergey Zigachev 
64b843c749SSergey Zigachev static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev);
65b843c749SSergey Zigachev static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev);
66b843c749SSergey Zigachev 
67b843c749SSergey Zigachev /*
68b843c749SSergey Zigachev  * Global memory.
69b843c749SSergey Zigachev  */
70b843c749SSergey Zigachev 
71b843c749SSergey Zigachev /**
72b843c749SSergey Zigachev  * amdgpu_ttm_mem_global_init - Initialize and acquire reference to
73b843c749SSergey Zigachev  * memory object
74b843c749SSergey Zigachev  *
75b843c749SSergey Zigachev  * @ref: Object for initialization.
76b843c749SSergey Zigachev  *
77b843c749SSergey Zigachev  * This is called by drm_global_item_ref() when an object is being
78b843c749SSergey Zigachev  * initialized.
79b843c749SSergey Zigachev  */
amdgpu_ttm_mem_global_init(struct drm_global_reference * ref)80b843c749SSergey Zigachev static int amdgpu_ttm_mem_global_init(struct drm_global_reference *ref)
81b843c749SSergey Zigachev {
82b843c749SSergey Zigachev 	return ttm_mem_global_init(ref->object);
83b843c749SSergey Zigachev }
84b843c749SSergey Zigachev 
85b843c749SSergey Zigachev /**
86b843c749SSergey Zigachev  * amdgpu_ttm_mem_global_release - Drop reference to a memory object
87b843c749SSergey Zigachev  *
88b843c749SSergey Zigachev  * @ref: Object being removed
89b843c749SSergey Zigachev  *
90b843c749SSergey Zigachev  * This is called by drm_global_item_unref() when an object is being
91b843c749SSergey Zigachev  * released.
92b843c749SSergey Zigachev  */
amdgpu_ttm_mem_global_release(struct drm_global_reference * ref)93b843c749SSergey Zigachev static void amdgpu_ttm_mem_global_release(struct drm_global_reference *ref)
94b843c749SSergey Zigachev {
95b843c749SSergey Zigachev 	ttm_mem_global_release(ref->object);
96b843c749SSergey Zigachev }
97b843c749SSergey Zigachev 
98b843c749SSergey Zigachev /**
99b843c749SSergey Zigachev  * amdgpu_ttm_global_init - Initialize global TTM memory reference structures.
100b843c749SSergey Zigachev  *
101b843c749SSergey Zigachev  * @adev: AMDGPU device for which the global structures need to be registered.
102b843c749SSergey Zigachev  *
103b843c749SSergey Zigachev  * This is called as part of the AMDGPU ttm init from amdgpu_ttm_init()
104b843c749SSergey Zigachev  * during bring up.
105b843c749SSergey Zigachev  */
amdgpu_ttm_global_init(struct amdgpu_device * adev)106b843c749SSergey Zigachev static int amdgpu_ttm_global_init(struct amdgpu_device *adev)
107b843c749SSergey Zigachev {
108b843c749SSergey Zigachev 	struct drm_global_reference *global_ref;
109b843c749SSergey Zigachev 	int r;
110b843c749SSergey Zigachev 
111b843c749SSergey Zigachev 	/* ensure reference is false in case init fails */
112b843c749SSergey Zigachev 	adev->mman.mem_global_referenced = false;
113b843c749SSergey Zigachev 
114b843c749SSergey Zigachev 	global_ref = &adev->mman.mem_global_ref;
115b843c749SSergey Zigachev 	global_ref->global_type = DRM_GLOBAL_TTM_MEM;
116b843c749SSergey Zigachev 	global_ref->size = sizeof(struct ttm_mem_global);
117b843c749SSergey Zigachev 	global_ref->init = &amdgpu_ttm_mem_global_init;
118b843c749SSergey Zigachev 	global_ref->release = &amdgpu_ttm_mem_global_release;
119b843c749SSergey Zigachev 	r = drm_global_item_ref(global_ref);
120b843c749SSergey Zigachev 	if (r) {
121b843c749SSergey Zigachev 		DRM_ERROR("Failed setting up TTM memory accounting "
122b843c749SSergey Zigachev 			  "subsystem.\n");
123b843c749SSergey Zigachev 		goto error_mem;
124b843c749SSergey Zigachev 	}
125b843c749SSergey Zigachev 
126b843c749SSergey Zigachev 	adev->mman.bo_global_ref.mem_glob =
127b843c749SSergey Zigachev 		adev->mman.mem_global_ref.object;
128b843c749SSergey Zigachev 	global_ref = &adev->mman.bo_global_ref.ref;
129b843c749SSergey Zigachev 	global_ref->global_type = DRM_GLOBAL_TTM_BO;
130b843c749SSergey Zigachev 	global_ref->size = sizeof(struct ttm_bo_global);
131b843c749SSergey Zigachev 	global_ref->init = &ttm_bo_global_init;
132b843c749SSergey Zigachev 	global_ref->release = &ttm_bo_global_release;
133b843c749SSergey Zigachev 	r = drm_global_item_ref(global_ref);
134b843c749SSergey Zigachev 	if (r) {
135b843c749SSergey Zigachev 		DRM_ERROR("Failed setting up TTM BO subsystem.\n");
136b843c749SSergey Zigachev 		goto error_bo;
137b843c749SSergey Zigachev 	}
138b843c749SSergey Zigachev 
139*78973132SSergey Zigachev 	lockinit(&adev->mman.gtt_window_lock, "agmgtwl", 0, LK_CANRECURSE);
140b843c749SSergey Zigachev 
141b843c749SSergey Zigachev 	adev->mman.mem_global_referenced = true;
142b843c749SSergey Zigachev 
143b843c749SSergey Zigachev 	return 0;
144b843c749SSergey Zigachev 
145b843c749SSergey Zigachev error_bo:
146b843c749SSergey Zigachev 	drm_global_item_unref(&adev->mman.mem_global_ref);
147b843c749SSergey Zigachev error_mem:
148b843c749SSergey Zigachev 	return r;
149b843c749SSergey Zigachev }
150b843c749SSergey Zigachev 
amdgpu_ttm_global_fini(struct amdgpu_device * adev)151b843c749SSergey Zigachev static void amdgpu_ttm_global_fini(struct amdgpu_device *adev)
152b843c749SSergey Zigachev {
153b843c749SSergey Zigachev 	if (adev->mman.mem_global_referenced) {
154b843c749SSergey Zigachev 		mutex_destroy(&adev->mman.gtt_window_lock);
155b843c749SSergey Zigachev 		drm_global_item_unref(&adev->mman.bo_global_ref.ref);
156b843c749SSergey Zigachev 		drm_global_item_unref(&adev->mman.mem_global_ref);
157b843c749SSergey Zigachev 		adev->mman.mem_global_referenced = false;
158b843c749SSergey Zigachev 	}
159b843c749SSergey Zigachev }
160b843c749SSergey Zigachev 
amdgpu_invalidate_caches(struct ttm_bo_device * bdev,uint32_t flags)161b843c749SSergey Zigachev static int amdgpu_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
162b843c749SSergey Zigachev {
163b843c749SSergey Zigachev 	return 0;
164b843c749SSergey Zigachev }
165b843c749SSergey Zigachev 
166b843c749SSergey Zigachev /**
167b843c749SSergey Zigachev  * amdgpu_init_mem_type - Initialize a memory manager for a specific type of
168b843c749SSergey Zigachev  * memory request.
169b843c749SSergey Zigachev  *
170b843c749SSergey Zigachev  * @bdev: The TTM BO device object (contains a reference to amdgpu_device)
171b843c749SSergey Zigachev  * @type: The type of memory requested
172b843c749SSergey Zigachev  * @man: The memory type manager for each domain
173b843c749SSergey Zigachev  *
174b843c749SSergey Zigachev  * This is called by ttm_bo_init_mm() when a buffer object is being
175b843c749SSergey Zigachev  * initialized.
176b843c749SSergey Zigachev  */
amdgpu_init_mem_type(struct ttm_bo_device * bdev,uint32_t type,struct ttm_mem_type_manager * man)177b843c749SSergey Zigachev static int amdgpu_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
178b843c749SSergey Zigachev 				struct ttm_mem_type_manager *man)
179b843c749SSergey Zigachev {
180b843c749SSergey Zigachev 	struct amdgpu_device *adev;
181b843c749SSergey Zigachev 
182b843c749SSergey Zigachev 	adev = amdgpu_ttm_adev(bdev);
183b843c749SSergey Zigachev 
184b843c749SSergey Zigachev 	switch (type) {
185b843c749SSergey Zigachev 	case TTM_PL_SYSTEM:
186b843c749SSergey Zigachev 		/* System memory */
187b843c749SSergey Zigachev 		man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
188b843c749SSergey Zigachev 		man->available_caching = TTM_PL_MASK_CACHING;
189b843c749SSergey Zigachev 		man->default_caching = TTM_PL_FLAG_CACHED;
190b843c749SSergey Zigachev 		break;
191b843c749SSergey Zigachev 	case TTM_PL_TT:
192b843c749SSergey Zigachev 		/* GTT memory  */
193b843c749SSergey Zigachev 		man->func = &amdgpu_gtt_mgr_func;
194b843c749SSergey Zigachev 		man->gpu_offset = adev->gmc.gart_start;
195b843c749SSergey Zigachev 		man->available_caching = TTM_PL_MASK_CACHING;
196b843c749SSergey Zigachev 		man->default_caching = TTM_PL_FLAG_CACHED;
197b843c749SSergey Zigachev 		man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | TTM_MEMTYPE_FLAG_CMA;
198b843c749SSergey Zigachev 		break;
199b843c749SSergey Zigachev 	case TTM_PL_VRAM:
200b843c749SSergey Zigachev 		/* "On-card" video ram */
201b843c749SSergey Zigachev 		man->func = &amdgpu_vram_mgr_func;
202b843c749SSergey Zigachev 		man->gpu_offset = adev->gmc.vram_start;
203b843c749SSergey Zigachev 		man->flags = TTM_MEMTYPE_FLAG_FIXED |
204b843c749SSergey Zigachev 			     TTM_MEMTYPE_FLAG_MAPPABLE;
205b843c749SSergey Zigachev 		man->available_caching = TTM_PL_FLAG_UNCACHED | TTM_PL_FLAG_WC;
206b843c749SSergey Zigachev 		man->default_caching = TTM_PL_FLAG_WC;
207b843c749SSergey Zigachev 		break;
208b843c749SSergey Zigachev 	case AMDGPU_PL_GDS:
209b843c749SSergey Zigachev 	case AMDGPU_PL_GWS:
210b843c749SSergey Zigachev 	case AMDGPU_PL_OA:
211b843c749SSergey Zigachev 		/* On-chip GDS memory*/
212b843c749SSergey Zigachev 		man->func = &ttm_bo_manager_func;
213b843c749SSergey Zigachev 		man->gpu_offset = 0;
214b843c749SSergey Zigachev 		man->flags = TTM_MEMTYPE_FLAG_FIXED | TTM_MEMTYPE_FLAG_CMA;
215b843c749SSergey Zigachev 		man->available_caching = TTM_PL_FLAG_UNCACHED;
216b843c749SSergey Zigachev 		man->default_caching = TTM_PL_FLAG_UNCACHED;
217b843c749SSergey Zigachev 		break;
218b843c749SSergey Zigachev 	default:
219b843c749SSergey Zigachev 		DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
220b843c749SSergey Zigachev 		return -EINVAL;
221b843c749SSergey Zigachev 	}
222b843c749SSergey Zigachev 	return 0;
223b843c749SSergey Zigachev }
224b843c749SSergey Zigachev 
225b843c749SSergey Zigachev /**
226b843c749SSergey Zigachev  * amdgpu_evict_flags - Compute placement flags
227b843c749SSergey Zigachev  *
228b843c749SSergey Zigachev  * @bo: The buffer object to evict
229b843c749SSergey Zigachev  * @placement: Possible destination(s) for evicted BO
230b843c749SSergey Zigachev  *
231b843c749SSergey Zigachev  * Fill in placement data when ttm_bo_evict() is called
232b843c749SSergey Zigachev  */
amdgpu_evict_flags(struct ttm_buffer_object * bo,struct ttm_placement * placement)233b843c749SSergey Zigachev static void amdgpu_evict_flags(struct ttm_buffer_object *bo,
234b843c749SSergey Zigachev 				struct ttm_placement *placement)
235b843c749SSergey Zigachev {
236b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
237b843c749SSergey Zigachev 	struct amdgpu_bo *abo;
238b843c749SSergey Zigachev 	static const struct ttm_place placements = {
239b843c749SSergey Zigachev 		.fpfn = 0,
240b843c749SSergey Zigachev 		.lpfn = 0,
241b843c749SSergey Zigachev 		.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_SYSTEM
242b843c749SSergey Zigachev 	};
243b843c749SSergey Zigachev 
244b843c749SSergey Zigachev 	/* Don't handle scatter gather BOs */
245b843c749SSergey Zigachev 	if (bo->type == ttm_bo_type_sg) {
246b843c749SSergey Zigachev 		placement->num_placement = 0;
247b843c749SSergey Zigachev 		placement->num_busy_placement = 0;
248b843c749SSergey Zigachev 		return;
249b843c749SSergey Zigachev 	}
250b843c749SSergey Zigachev 
251b843c749SSergey Zigachev 	/* Object isn't an AMDGPU object so ignore */
252b843c749SSergey Zigachev 	if (!amdgpu_bo_is_amdgpu_bo(bo)) {
253b843c749SSergey Zigachev 		placement->placement = &placements;
254b843c749SSergey Zigachev 		placement->busy_placement = &placements;
255b843c749SSergey Zigachev 		placement->num_placement = 1;
256b843c749SSergey Zigachev 		placement->num_busy_placement = 1;
257b843c749SSergey Zigachev 		return;
258b843c749SSergey Zigachev 	}
259b843c749SSergey Zigachev 
260b843c749SSergey Zigachev 	abo = ttm_to_amdgpu_bo(bo);
261b843c749SSergey Zigachev 	switch (bo->mem.mem_type) {
262b843c749SSergey Zigachev 	case TTM_PL_VRAM:
263b843c749SSergey Zigachev 		if (!adev->mman.buffer_funcs_enabled) {
264b843c749SSergey Zigachev 			/* Move to system memory */
265b843c749SSergey Zigachev 			amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
266b843c749SSergey Zigachev 		} else if (!amdgpu_gmc_vram_full_visible(&adev->gmc) &&
267b843c749SSergey Zigachev 			   !(abo->flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) &&
268b843c749SSergey Zigachev 			   amdgpu_bo_in_cpu_visible_vram(abo)) {
269b843c749SSergey Zigachev 
270b843c749SSergey Zigachev 			/* Try evicting to the CPU inaccessible part of VRAM
271b843c749SSergey Zigachev 			 * first, but only set GTT as busy placement, so this
272b843c749SSergey Zigachev 			 * BO will be evicted to GTT rather than causing other
273b843c749SSergey Zigachev 			 * BOs to be evicted from VRAM
274b843c749SSergey Zigachev 			 */
275b843c749SSergey Zigachev 			amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_VRAM |
276b843c749SSergey Zigachev 							 AMDGPU_GEM_DOMAIN_GTT);
277b843c749SSergey Zigachev 			abo->placements[0].fpfn = adev->gmc.visible_vram_size >> PAGE_SHIFT;
278b843c749SSergey Zigachev 			abo->placements[0].lpfn = 0;
279b843c749SSergey Zigachev 			abo->placement.busy_placement = &abo->placements[1];
280b843c749SSergey Zigachev 			abo->placement.num_busy_placement = 1;
281b843c749SSergey Zigachev 		} else {
282b843c749SSergey Zigachev 			/* Move to GTT memory */
283b843c749SSergey Zigachev 			amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_GTT);
284b843c749SSergey Zigachev 		}
285b843c749SSergey Zigachev 		break;
286b843c749SSergey Zigachev 	case TTM_PL_TT:
287b843c749SSergey Zigachev 	default:
288b843c749SSergey Zigachev 		amdgpu_bo_placement_from_domain(abo, AMDGPU_GEM_DOMAIN_CPU);
289b843c749SSergey Zigachev 	}
290b843c749SSergey Zigachev 	*placement = abo->placement;
291b843c749SSergey Zigachev }
292b843c749SSergey Zigachev 
293b843c749SSergey Zigachev /**
294b843c749SSergey Zigachev  * amdgpu_verify_access - Verify access for a mmap call
295b843c749SSergey Zigachev  *
296b843c749SSergey Zigachev  * @bo:	The buffer object to map
297b843c749SSergey Zigachev  * @filp: The file pointer from the process performing the mmap
298b843c749SSergey Zigachev  *
299b843c749SSergey Zigachev  * This is called by ttm_bo_mmap() to verify whether a process
300b843c749SSergey Zigachev  * has the right to mmap a BO to their process space.
301b843c749SSergey Zigachev  */
amdgpu_verify_access(struct ttm_buffer_object * bo,struct file * filp)302b843c749SSergey Zigachev static int amdgpu_verify_access(struct ttm_buffer_object *bo, struct file *filp)
303b843c749SSergey Zigachev {
304b843c749SSergey Zigachev 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
305b843c749SSergey Zigachev 
306b843c749SSergey Zigachev 	/*
307b843c749SSergey Zigachev 	 * Don't verify access for KFD BOs. They don't have a GEM
308b843c749SSergey Zigachev 	 * object associated with them.
309b843c749SSergey Zigachev 	 */
310b843c749SSergey Zigachev 	if (abo->kfd_bo)
311b843c749SSergey Zigachev 		return 0;
312b843c749SSergey Zigachev 
313b843c749SSergey Zigachev 	if (amdgpu_ttm_tt_get_usermm(bo->ttm))
314b843c749SSergey Zigachev 		return -EPERM;
315b843c749SSergey Zigachev 	return drm_vma_node_verify_access(&abo->gem_base.vma_node,
316b843c749SSergey Zigachev 					  filp->private_data);
317b843c749SSergey Zigachev }
318b843c749SSergey Zigachev 
319b843c749SSergey Zigachev /**
320b843c749SSergey Zigachev  * amdgpu_move_null - Register memory for a buffer object
321b843c749SSergey Zigachev  *
322b843c749SSergey Zigachev  * @bo: The bo to assign the memory to
323b843c749SSergey Zigachev  * @new_mem: The memory to be assigned.
324b843c749SSergey Zigachev  *
325b843c749SSergey Zigachev  * Assign the memory from new_mem to the memory of the buffer object bo.
326b843c749SSergey Zigachev  */
amdgpu_move_null(struct ttm_buffer_object * bo,struct ttm_mem_reg * new_mem)327b843c749SSergey Zigachev static void amdgpu_move_null(struct ttm_buffer_object *bo,
328b843c749SSergey Zigachev 			     struct ttm_mem_reg *new_mem)
329b843c749SSergey Zigachev {
330b843c749SSergey Zigachev 	struct ttm_mem_reg *old_mem = &bo->mem;
331b843c749SSergey Zigachev 
332b843c749SSergey Zigachev 	BUG_ON(old_mem->mm_node != NULL);
333b843c749SSergey Zigachev 	*old_mem = *new_mem;
334b843c749SSergey Zigachev 	new_mem->mm_node = NULL;
335b843c749SSergey Zigachev }
336b843c749SSergey Zigachev 
337b843c749SSergey Zigachev /**
338b843c749SSergey Zigachev  * amdgpu_mm_node_addr - Compute the GPU relative offset of a GTT buffer.
339b843c749SSergey Zigachev  *
340b843c749SSergey Zigachev  * @bo: The bo to assign the memory to.
341b843c749SSergey Zigachev  * @mm_node: Memory manager node for drm allocator.
342b843c749SSergey Zigachev  * @mem: The region where the bo resides.
343b843c749SSergey Zigachev  *
344b843c749SSergey Zigachev  */
amdgpu_mm_node_addr(struct ttm_buffer_object * bo,struct drm_mm_node * mm_node,struct ttm_mem_reg * mem)345b843c749SSergey Zigachev static uint64_t amdgpu_mm_node_addr(struct ttm_buffer_object *bo,
346b843c749SSergey Zigachev 				    struct drm_mm_node *mm_node,
347b843c749SSergey Zigachev 				    struct ttm_mem_reg *mem)
348b843c749SSergey Zigachev {
349b843c749SSergey Zigachev 	uint64_t addr = 0;
350b843c749SSergey Zigachev 
351b843c749SSergey Zigachev 	if (mem->mem_type != TTM_PL_TT || amdgpu_gtt_mgr_has_gart_addr(mem)) {
352b843c749SSergey Zigachev 		addr = mm_node->start << PAGE_SHIFT;
353b843c749SSergey Zigachev 		addr += bo->bdev->man[mem->mem_type].gpu_offset;
354b843c749SSergey Zigachev 	}
355b843c749SSergey Zigachev 	return addr;
356b843c749SSergey Zigachev }
357b843c749SSergey Zigachev 
358b843c749SSergey Zigachev /**
359b843c749SSergey Zigachev  * amdgpu_find_mm_node - Helper function finds the drm_mm_node corresponding to
360b843c749SSergey Zigachev  * @offset. It also modifies the offset to be within the drm_mm_node returned
361b843c749SSergey Zigachev  *
362b843c749SSergey Zigachev  * @mem: The region where the bo resides.
363b843c749SSergey Zigachev  * @offset: The offset that drm_mm_node is used for finding.
364b843c749SSergey Zigachev  *
365b843c749SSergey Zigachev  */
amdgpu_find_mm_node(struct ttm_mem_reg * mem,unsigned long * offset)366b843c749SSergey Zigachev static struct drm_mm_node *amdgpu_find_mm_node(struct ttm_mem_reg *mem,
367b843c749SSergey Zigachev 					       unsigned long *offset)
368b843c749SSergey Zigachev {
369b843c749SSergey Zigachev 	struct drm_mm_node *mm_node = mem->mm_node;
370b843c749SSergey Zigachev 
371b843c749SSergey Zigachev 	while (*offset >= (mm_node->size << PAGE_SHIFT)) {
372b843c749SSergey Zigachev 		*offset -= (mm_node->size << PAGE_SHIFT);
373b843c749SSergey Zigachev 		++mm_node;
374b843c749SSergey Zigachev 	}
375b843c749SSergey Zigachev 	return mm_node;
376b843c749SSergey Zigachev }
377b843c749SSergey Zigachev 
378b843c749SSergey Zigachev /**
379b843c749SSergey Zigachev  * amdgpu_copy_ttm_mem_to_mem - Helper function for copy
380b843c749SSergey Zigachev  *
381b843c749SSergey Zigachev  * The function copies @size bytes from {src->mem + src->offset} to
382b843c749SSergey Zigachev  * {dst->mem + dst->offset}. src->bo and dst->bo could be same BO for a
383b843c749SSergey Zigachev  * move and different for a BO to BO copy.
384b843c749SSergey Zigachev  *
385b843c749SSergey Zigachev  * @f: Returns the last fence if multiple jobs are submitted.
386b843c749SSergey Zigachev  */
amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device * adev,struct amdgpu_copy_mem * src,struct amdgpu_copy_mem * dst,uint64_t size,struct reservation_object * resv,struct dma_fence ** f)387b843c749SSergey Zigachev int amdgpu_ttm_copy_mem_to_mem(struct amdgpu_device *adev,
388b843c749SSergey Zigachev 			       struct amdgpu_copy_mem *src,
389b843c749SSergey Zigachev 			       struct amdgpu_copy_mem *dst,
390b843c749SSergey Zigachev 			       uint64_t size,
391b843c749SSergey Zigachev 			       struct reservation_object *resv,
392b843c749SSergey Zigachev 			       struct dma_fence **f)
393b843c749SSergey Zigachev {
394b843c749SSergey Zigachev 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
395b843c749SSergey Zigachev 	struct drm_mm_node *src_mm, *dst_mm;
396b843c749SSergey Zigachev 	uint64_t src_node_start, dst_node_start, src_node_size,
397b843c749SSergey Zigachev 		 dst_node_size, src_page_offset, dst_page_offset;
398b843c749SSergey Zigachev 	struct dma_fence *fence = NULL;
399b843c749SSergey Zigachev 	int r = 0;
400b843c749SSergey Zigachev 	const uint64_t GTT_MAX_BYTES = (AMDGPU_GTT_MAX_TRANSFER_SIZE *
401b843c749SSergey Zigachev 					AMDGPU_GPU_PAGE_SIZE);
402b843c749SSergey Zigachev 
403b843c749SSergey Zigachev 	if (!adev->mman.buffer_funcs_enabled) {
404b843c749SSergey Zigachev 		DRM_ERROR("Trying to move memory with ring turned off.\n");
405b843c749SSergey Zigachev 		return -EINVAL;
406b843c749SSergey Zigachev 	}
407b843c749SSergey Zigachev 
408b843c749SSergey Zigachev 	src_mm = amdgpu_find_mm_node(src->mem, &src->offset);
409b843c749SSergey Zigachev 	src_node_start = amdgpu_mm_node_addr(src->bo, src_mm, src->mem) +
410b843c749SSergey Zigachev 					     src->offset;
411b843c749SSergey Zigachev 	src_node_size = (src_mm->size << PAGE_SHIFT) - src->offset;
412b843c749SSergey Zigachev 	src_page_offset = src_node_start & (PAGE_SIZE - 1);
413b843c749SSergey Zigachev 
414b843c749SSergey Zigachev 	dst_mm = amdgpu_find_mm_node(dst->mem, &dst->offset);
415b843c749SSergey Zigachev 	dst_node_start = amdgpu_mm_node_addr(dst->bo, dst_mm, dst->mem) +
416b843c749SSergey Zigachev 					     dst->offset;
417b843c749SSergey Zigachev 	dst_node_size = (dst_mm->size << PAGE_SHIFT) - dst->offset;
418b843c749SSergey Zigachev 	dst_page_offset = dst_node_start & (PAGE_SIZE - 1);
419b843c749SSergey Zigachev 
420b843c749SSergey Zigachev 	mutex_lock(&adev->mman.gtt_window_lock);
421b843c749SSergey Zigachev 
422b843c749SSergey Zigachev 	while (size) {
423b843c749SSergey Zigachev 		unsigned long cur_size;
424b843c749SSergey Zigachev 		uint64_t from = src_node_start, to = dst_node_start;
425b843c749SSergey Zigachev 		struct dma_fence *next;
426b843c749SSergey Zigachev 
427b843c749SSergey Zigachev 		/* Copy size cannot exceed GTT_MAX_BYTES. So if src or dst
428b843c749SSergey Zigachev 		 * begins at an offset, then adjust the size accordingly
429b843c749SSergey Zigachev 		 */
430b843c749SSergey Zigachev 		cur_size = min3(min(src_node_size, dst_node_size), size,
431b843c749SSergey Zigachev 				GTT_MAX_BYTES);
432b843c749SSergey Zigachev 		if (cur_size + src_page_offset > GTT_MAX_BYTES ||
433b843c749SSergey Zigachev 		    cur_size + dst_page_offset > GTT_MAX_BYTES)
434b843c749SSergey Zigachev 			cur_size -= max(src_page_offset, dst_page_offset);
435b843c749SSergey Zigachev 
436b843c749SSergey Zigachev 		/* Map only what needs to be accessed. Map src to window 0 and
437b843c749SSergey Zigachev 		 * dst to window 1
438b843c749SSergey Zigachev 		 */
439b843c749SSergey Zigachev 		if (src->mem->mem_type == TTM_PL_TT &&
440b843c749SSergey Zigachev 		    !amdgpu_gtt_mgr_has_gart_addr(src->mem)) {
441b843c749SSergey Zigachev 			r = amdgpu_map_buffer(src->bo, src->mem,
442b843c749SSergey Zigachev 					PFN_UP(cur_size + src_page_offset),
443b843c749SSergey Zigachev 					src_node_start, 0, ring,
444b843c749SSergey Zigachev 					&from);
445b843c749SSergey Zigachev 			if (r)
446b843c749SSergey Zigachev 				goto error;
447b843c749SSergey Zigachev 			/* Adjust the offset because amdgpu_map_buffer returns
448b843c749SSergey Zigachev 			 * start of mapped page
449b843c749SSergey Zigachev 			 */
450b843c749SSergey Zigachev 			from += src_page_offset;
451b843c749SSergey Zigachev 		}
452b843c749SSergey Zigachev 
453b843c749SSergey Zigachev 		if (dst->mem->mem_type == TTM_PL_TT &&
454b843c749SSergey Zigachev 		    !amdgpu_gtt_mgr_has_gart_addr(dst->mem)) {
455b843c749SSergey Zigachev 			r = amdgpu_map_buffer(dst->bo, dst->mem,
456b843c749SSergey Zigachev 					PFN_UP(cur_size + dst_page_offset),
457b843c749SSergey Zigachev 					dst_node_start, 1, ring,
458b843c749SSergey Zigachev 					&to);
459b843c749SSergey Zigachev 			if (r)
460b843c749SSergey Zigachev 				goto error;
461b843c749SSergey Zigachev 			to += dst_page_offset;
462b843c749SSergey Zigachev 		}
463b843c749SSergey Zigachev 
464b843c749SSergey Zigachev 		r = amdgpu_copy_buffer(ring, from, to, cur_size,
465b843c749SSergey Zigachev 				       resv, &next, false, true);
466b843c749SSergey Zigachev 		if (r)
467b843c749SSergey Zigachev 			goto error;
468b843c749SSergey Zigachev 
469b843c749SSergey Zigachev 		dma_fence_put(fence);
470b843c749SSergey Zigachev 		fence = next;
471b843c749SSergey Zigachev 
472b843c749SSergey Zigachev 		size -= cur_size;
473b843c749SSergey Zigachev 		if (!size)
474b843c749SSergey Zigachev 			break;
475b843c749SSergey Zigachev 
476b843c749SSergey Zigachev 		src_node_size -= cur_size;
477b843c749SSergey Zigachev 		if (!src_node_size) {
478b843c749SSergey Zigachev 			src_node_start = amdgpu_mm_node_addr(src->bo, ++src_mm,
479b843c749SSergey Zigachev 							     src->mem);
480b843c749SSergey Zigachev 			src_node_size = (src_mm->size << PAGE_SHIFT);
481b843c749SSergey Zigachev 		} else {
482b843c749SSergey Zigachev 			src_node_start += cur_size;
483b843c749SSergey Zigachev 			src_page_offset = src_node_start & (PAGE_SIZE - 1);
484b843c749SSergey Zigachev 		}
485b843c749SSergey Zigachev 		dst_node_size -= cur_size;
486b843c749SSergey Zigachev 		if (!dst_node_size) {
487b843c749SSergey Zigachev 			dst_node_start = amdgpu_mm_node_addr(dst->bo, ++dst_mm,
488b843c749SSergey Zigachev 							     dst->mem);
489b843c749SSergey Zigachev 			dst_node_size = (dst_mm->size << PAGE_SHIFT);
490b843c749SSergey Zigachev 		} else {
491b843c749SSergey Zigachev 			dst_node_start += cur_size;
492b843c749SSergey Zigachev 			dst_page_offset = dst_node_start & (PAGE_SIZE - 1);
493b843c749SSergey Zigachev 		}
494b843c749SSergey Zigachev 	}
495b843c749SSergey Zigachev error:
496b843c749SSergey Zigachev 	mutex_unlock(&adev->mman.gtt_window_lock);
497b843c749SSergey Zigachev 	if (f)
498b843c749SSergey Zigachev 		*f = dma_fence_get(fence);
499b843c749SSergey Zigachev 	dma_fence_put(fence);
500b843c749SSergey Zigachev 	return r;
501b843c749SSergey Zigachev }
502b843c749SSergey Zigachev 
503b843c749SSergey Zigachev /**
504b843c749SSergey Zigachev  * amdgpu_move_blit - Copy an entire buffer to another buffer
505b843c749SSergey Zigachev  *
506b843c749SSergey Zigachev  * This is a helper called by amdgpu_bo_move() and amdgpu_move_vram_ram() to
507b843c749SSergey Zigachev  * help move buffers to and from VRAM.
508b843c749SSergey Zigachev  */
amdgpu_move_blit(struct ttm_buffer_object * bo,bool evict,bool no_wait_gpu,struct ttm_mem_reg * new_mem,struct ttm_mem_reg * old_mem)509b843c749SSergey Zigachev static int amdgpu_move_blit(struct ttm_buffer_object *bo,
510b843c749SSergey Zigachev 			    bool evict, bool no_wait_gpu,
511b843c749SSergey Zigachev 			    struct ttm_mem_reg *new_mem,
512b843c749SSergey Zigachev 			    struct ttm_mem_reg *old_mem)
513b843c749SSergey Zigachev {
514b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
515b843c749SSergey Zigachev 	struct amdgpu_copy_mem src, dst;
516b843c749SSergey Zigachev 	struct dma_fence *fence = NULL;
517b843c749SSergey Zigachev 	int r;
518b843c749SSergey Zigachev 
519b843c749SSergey Zigachev 	src.bo = bo;
520b843c749SSergey Zigachev 	dst.bo = bo;
521b843c749SSergey Zigachev 	src.mem = old_mem;
522b843c749SSergey Zigachev 	dst.mem = new_mem;
523b843c749SSergey Zigachev 	src.offset = 0;
524b843c749SSergey Zigachev 	dst.offset = 0;
525b843c749SSergey Zigachev 
526b843c749SSergey Zigachev 	r = amdgpu_ttm_copy_mem_to_mem(adev, &src, &dst,
527b843c749SSergey Zigachev 				       new_mem->num_pages << PAGE_SHIFT,
528b843c749SSergey Zigachev 				       bo->resv, &fence);
529b843c749SSergey Zigachev 	if (r)
530b843c749SSergey Zigachev 		goto error;
531b843c749SSergey Zigachev 
532b843c749SSergey Zigachev 	r = ttm_bo_pipeline_move(bo, fence, evict, new_mem);
533b843c749SSergey Zigachev 	dma_fence_put(fence);
534b843c749SSergey Zigachev 	return r;
535b843c749SSergey Zigachev 
536b843c749SSergey Zigachev error:
537b843c749SSergey Zigachev 	if (fence)
538b843c749SSergey Zigachev 		dma_fence_wait(fence, false);
539b843c749SSergey Zigachev 	dma_fence_put(fence);
540b843c749SSergey Zigachev 	return r;
541b843c749SSergey Zigachev }
542b843c749SSergey Zigachev 
543b843c749SSergey Zigachev /**
544b843c749SSergey Zigachev  * amdgpu_move_vram_ram - Copy VRAM buffer to RAM buffer
545b843c749SSergey Zigachev  *
546b843c749SSergey Zigachev  * Called by amdgpu_bo_move().
547b843c749SSergey Zigachev  */
amdgpu_move_vram_ram(struct ttm_buffer_object * bo,bool evict,struct ttm_operation_ctx * ctx,struct ttm_mem_reg * new_mem)548b843c749SSergey Zigachev static int amdgpu_move_vram_ram(struct ttm_buffer_object *bo, bool evict,
549b843c749SSergey Zigachev 				struct ttm_operation_ctx *ctx,
550b843c749SSergey Zigachev 				struct ttm_mem_reg *new_mem)
551b843c749SSergey Zigachev {
552b843c749SSergey Zigachev 	struct amdgpu_device *adev;
553b843c749SSergey Zigachev 	struct ttm_mem_reg *old_mem = &bo->mem;
554b843c749SSergey Zigachev 	struct ttm_mem_reg tmp_mem;
555b843c749SSergey Zigachev 	struct ttm_place placements;
556b843c749SSergey Zigachev 	struct ttm_placement placement;
557b843c749SSergey Zigachev 	int r;
558b843c749SSergey Zigachev 
559b843c749SSergey Zigachev 	adev = amdgpu_ttm_adev(bo->bdev);
560b843c749SSergey Zigachev 
561b843c749SSergey Zigachev 	/* create space/pages for new_mem in GTT space */
562b843c749SSergey Zigachev 	tmp_mem = *new_mem;
563b843c749SSergey Zigachev 	tmp_mem.mm_node = NULL;
564b843c749SSergey Zigachev 	placement.num_placement = 1;
565b843c749SSergey Zigachev 	placement.placement = &placements;
566b843c749SSergey Zigachev 	placement.num_busy_placement = 1;
567b843c749SSergey Zigachev 	placement.busy_placement = &placements;
568b843c749SSergey Zigachev 	placements.fpfn = 0;
569b843c749SSergey Zigachev 	placements.lpfn = 0;
570b843c749SSergey Zigachev 	placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
571b843c749SSergey Zigachev 	r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
572b843c749SSergey Zigachev 	if (unlikely(r)) {
573b843c749SSergey Zigachev 		return r;
574b843c749SSergey Zigachev 	}
575b843c749SSergey Zigachev 
576b843c749SSergey Zigachev 	/* set caching flags */
577b843c749SSergey Zigachev 	r = ttm_tt_set_placement_caching(bo->ttm, tmp_mem.placement);
578b843c749SSergey Zigachev 	if (unlikely(r)) {
579b843c749SSergey Zigachev 		goto out_cleanup;
580b843c749SSergey Zigachev 	}
581b843c749SSergey Zigachev 
582b843c749SSergey Zigachev 	/* Bind the memory to the GTT space */
583b843c749SSergey Zigachev 	r = ttm_tt_bind(bo->ttm, &tmp_mem, ctx);
584b843c749SSergey Zigachev 	if (unlikely(r)) {
585b843c749SSergey Zigachev 		goto out_cleanup;
586b843c749SSergey Zigachev 	}
587b843c749SSergey Zigachev 
588b843c749SSergey Zigachev 	/* blit VRAM to GTT */
589b843c749SSergey Zigachev 	r = amdgpu_move_blit(bo, evict, ctx->no_wait_gpu, &tmp_mem, old_mem);
590b843c749SSergey Zigachev 	if (unlikely(r)) {
591b843c749SSergey Zigachev 		goto out_cleanup;
592b843c749SSergey Zigachev 	}
593b843c749SSergey Zigachev 
594b843c749SSergey Zigachev 	/* move BO (in tmp_mem) to new_mem */
595b843c749SSergey Zigachev 	r = ttm_bo_move_ttm(bo, ctx, new_mem);
596b843c749SSergey Zigachev out_cleanup:
597b843c749SSergey Zigachev 	ttm_bo_mem_put(bo, &tmp_mem);
598b843c749SSergey Zigachev 	return r;
599b843c749SSergey Zigachev }
600b843c749SSergey Zigachev 
601b843c749SSergey Zigachev /**
602b843c749SSergey Zigachev  * amdgpu_move_ram_vram - Copy buffer from RAM to VRAM
603b843c749SSergey Zigachev  *
604b843c749SSergey Zigachev  * Called by amdgpu_bo_move().
605b843c749SSergey Zigachev  */
amdgpu_move_ram_vram(struct ttm_buffer_object * bo,bool evict,struct ttm_operation_ctx * ctx,struct ttm_mem_reg * new_mem)606b843c749SSergey Zigachev static int amdgpu_move_ram_vram(struct ttm_buffer_object *bo, bool evict,
607b843c749SSergey Zigachev 				struct ttm_operation_ctx *ctx,
608b843c749SSergey Zigachev 				struct ttm_mem_reg *new_mem)
609b843c749SSergey Zigachev {
610b843c749SSergey Zigachev 	struct amdgpu_device *adev;
611b843c749SSergey Zigachev 	struct ttm_mem_reg *old_mem = &bo->mem;
612b843c749SSergey Zigachev 	struct ttm_mem_reg tmp_mem;
613b843c749SSergey Zigachev 	struct ttm_placement placement;
614b843c749SSergey Zigachev 	struct ttm_place placements;
615b843c749SSergey Zigachev 	int r;
616b843c749SSergey Zigachev 
617b843c749SSergey Zigachev 	adev = amdgpu_ttm_adev(bo->bdev);
618b843c749SSergey Zigachev 
619b843c749SSergey Zigachev 	/* make space in GTT for old_mem buffer */
620b843c749SSergey Zigachev 	tmp_mem = *new_mem;
621b843c749SSergey Zigachev 	tmp_mem.mm_node = NULL;
622b843c749SSergey Zigachev 	placement.num_placement = 1;
623b843c749SSergey Zigachev 	placement.placement = &placements;
624b843c749SSergey Zigachev 	placement.num_busy_placement = 1;
625b843c749SSergey Zigachev 	placement.busy_placement = &placements;
626b843c749SSergey Zigachev 	placements.fpfn = 0;
627b843c749SSergey Zigachev 	placements.lpfn = 0;
628b843c749SSergey Zigachev 	placements.flags = TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT;
629b843c749SSergey Zigachev 	r = ttm_bo_mem_space(bo, &placement, &tmp_mem, ctx);
630b843c749SSergey Zigachev 	if (unlikely(r)) {
631b843c749SSergey Zigachev 		return r;
632b843c749SSergey Zigachev 	}
633b843c749SSergey Zigachev 
634b843c749SSergey Zigachev 	/* move/bind old memory to GTT space */
635b843c749SSergey Zigachev 	r = ttm_bo_move_ttm(bo, ctx, &tmp_mem);
636b843c749SSergey Zigachev 	if (unlikely(r)) {
637b843c749SSergey Zigachev 		goto out_cleanup;
638b843c749SSergey Zigachev 	}
639b843c749SSergey Zigachev 
640b843c749SSergey Zigachev 	/* copy to VRAM */
641b843c749SSergey Zigachev 	r = amdgpu_move_blit(bo, evict, ctx->no_wait_gpu, new_mem, old_mem);
642b843c749SSergey Zigachev 	if (unlikely(r)) {
643b843c749SSergey Zigachev 		goto out_cleanup;
644b843c749SSergey Zigachev 	}
645b843c749SSergey Zigachev out_cleanup:
646b843c749SSergey Zigachev 	ttm_bo_mem_put(bo, &tmp_mem);
647b843c749SSergey Zigachev 	return r;
648b843c749SSergey Zigachev }
649b843c749SSergey Zigachev 
650b843c749SSergey Zigachev /**
651b843c749SSergey Zigachev  * amdgpu_bo_move - Move a buffer object to a new memory location
652b843c749SSergey Zigachev  *
653b843c749SSergey Zigachev  * Called by ttm_bo_handle_move_mem()
654b843c749SSergey Zigachev  */
amdgpu_bo_move(struct ttm_buffer_object * bo,bool evict,struct ttm_operation_ctx * ctx,struct ttm_mem_reg * new_mem)655b843c749SSergey Zigachev static int amdgpu_bo_move(struct ttm_buffer_object *bo, bool evict,
656b843c749SSergey Zigachev 			  struct ttm_operation_ctx *ctx,
657b843c749SSergey Zigachev 			  struct ttm_mem_reg *new_mem)
658b843c749SSergey Zigachev {
659b843c749SSergey Zigachev 	struct amdgpu_device *adev;
660b843c749SSergey Zigachev 	struct amdgpu_bo *abo;
661b843c749SSergey Zigachev 	struct ttm_mem_reg *old_mem = &bo->mem;
662b843c749SSergey Zigachev 	int r;
663b843c749SSergey Zigachev 
664b843c749SSergey Zigachev 	/* Can't move a pinned BO */
665b843c749SSergey Zigachev 	abo = ttm_to_amdgpu_bo(bo);
666b843c749SSergey Zigachev 	if (WARN_ON_ONCE(abo->pin_count > 0))
667b843c749SSergey Zigachev 		return -EINVAL;
668b843c749SSergey Zigachev 
669b843c749SSergey Zigachev 	adev = amdgpu_ttm_adev(bo->bdev);
670b843c749SSergey Zigachev 
671b843c749SSergey Zigachev 	if (old_mem->mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) {
672b843c749SSergey Zigachev 		amdgpu_move_null(bo, new_mem);
673b843c749SSergey Zigachev 		return 0;
674b843c749SSergey Zigachev 	}
675b843c749SSergey Zigachev 	if ((old_mem->mem_type == TTM_PL_TT &&
676b843c749SSergey Zigachev 	     new_mem->mem_type == TTM_PL_SYSTEM) ||
677b843c749SSergey Zigachev 	    (old_mem->mem_type == TTM_PL_SYSTEM &&
678b843c749SSergey Zigachev 	     new_mem->mem_type == TTM_PL_TT)) {
679b843c749SSergey Zigachev 		/* bind is enough */
680b843c749SSergey Zigachev 		amdgpu_move_null(bo, new_mem);
681b843c749SSergey Zigachev 		return 0;
682b843c749SSergey Zigachev 	}
683b843c749SSergey Zigachev 
684b843c749SSergey Zigachev 	if (!adev->mman.buffer_funcs_enabled)
685b843c749SSergey Zigachev 		goto memcpy;
686b843c749SSergey Zigachev 
687b843c749SSergey Zigachev 	if (old_mem->mem_type == TTM_PL_VRAM &&
688b843c749SSergey Zigachev 	    new_mem->mem_type == TTM_PL_SYSTEM) {
689b843c749SSergey Zigachev 		r = amdgpu_move_vram_ram(bo, evict, ctx, new_mem);
690b843c749SSergey Zigachev 	} else if (old_mem->mem_type == TTM_PL_SYSTEM &&
691b843c749SSergey Zigachev 		   new_mem->mem_type == TTM_PL_VRAM) {
692b843c749SSergey Zigachev 		r = amdgpu_move_ram_vram(bo, evict, ctx, new_mem);
693b843c749SSergey Zigachev 	} else {
694b843c749SSergey Zigachev 		r = amdgpu_move_blit(bo, evict, ctx->no_wait_gpu,
695b843c749SSergey Zigachev 				     new_mem, old_mem);
696b843c749SSergey Zigachev 	}
697b843c749SSergey Zigachev 
698b843c749SSergey Zigachev 	if (r) {
699b843c749SSergey Zigachev memcpy:
700b843c749SSergey Zigachev 		r = ttm_bo_move_memcpy(bo, ctx, new_mem);
701b843c749SSergey Zigachev 		if (r) {
702b843c749SSergey Zigachev 			return r;
703b843c749SSergey Zigachev 		}
704b843c749SSergey Zigachev 	}
705b843c749SSergey Zigachev 
706b843c749SSergey Zigachev 	if (bo->type == ttm_bo_type_device &&
707b843c749SSergey Zigachev 	    new_mem->mem_type == TTM_PL_VRAM &&
708b843c749SSergey Zigachev 	    old_mem->mem_type != TTM_PL_VRAM) {
709b843c749SSergey Zigachev 		/* amdgpu_bo_fault_reserve_notify will re-set this if the CPU
710b843c749SSergey Zigachev 		 * accesses the BO after it's moved.
711b843c749SSergey Zigachev 		 */
712b843c749SSergey Zigachev 		abo->flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
713b843c749SSergey Zigachev 	}
714b843c749SSergey Zigachev 
715b843c749SSergey Zigachev 	/* update statistics */
716b843c749SSergey Zigachev 	atomic64_add((u64)bo->num_pages << PAGE_SHIFT, &adev->num_bytes_moved);
717b843c749SSergey Zigachev 	return 0;
718b843c749SSergey Zigachev }
719b843c749SSergey Zigachev 
720b843c749SSergey Zigachev /**
721b843c749SSergey Zigachev  * amdgpu_ttm_io_mem_reserve - Reserve a block of memory during a fault
722b843c749SSergey Zigachev  *
723b843c749SSergey Zigachev  * Called by ttm_mem_io_reserve() ultimately via ttm_bo_vm_fault()
724b843c749SSergey Zigachev  */
amdgpu_ttm_io_mem_reserve(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem)725b843c749SSergey Zigachev static int amdgpu_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
726b843c749SSergey Zigachev {
727b843c749SSergey Zigachev 	struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
728b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(bdev);
729b843c749SSergey Zigachev 	struct drm_mm_node *mm_node = mem->mm_node;
730b843c749SSergey Zigachev 
731b843c749SSergey Zigachev 	mem->bus.addr = NULL;
732b843c749SSergey Zigachev 	mem->bus.offset = 0;
733b843c749SSergey Zigachev 	mem->bus.size = mem->num_pages << PAGE_SHIFT;
734b843c749SSergey Zigachev 	mem->bus.base = 0;
735b843c749SSergey Zigachev 	mem->bus.is_iomem = false;
736b843c749SSergey Zigachev 	if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
737b843c749SSergey Zigachev 		return -EINVAL;
738b843c749SSergey Zigachev 	switch (mem->mem_type) {
739b843c749SSergey Zigachev 	case TTM_PL_SYSTEM:
740b843c749SSergey Zigachev 		/* system memory */
741b843c749SSergey Zigachev 		return 0;
742b843c749SSergey Zigachev 	case TTM_PL_TT:
743b843c749SSergey Zigachev 		break;
744b843c749SSergey Zigachev 	case TTM_PL_VRAM:
745b843c749SSergey Zigachev 		mem->bus.offset = mem->start << PAGE_SHIFT;
746b843c749SSergey Zigachev 		/* check if it's visible */
747b843c749SSergey Zigachev 		if ((mem->bus.offset + mem->bus.size) > adev->gmc.visible_vram_size)
748b843c749SSergey Zigachev 			return -EINVAL;
749b843c749SSergey Zigachev 		/* Only physically contiguous buffers apply. In a contiguous
750b843c749SSergey Zigachev 		 * buffer, size of the first mm_node would match the number of
751b843c749SSergey Zigachev 		 * pages in ttm_mem_reg.
752b843c749SSergey Zigachev 		 */
753b843c749SSergey Zigachev 		if (adev->mman.aper_base_kaddr &&
754b843c749SSergey Zigachev 		    (mm_node->size == mem->num_pages))
755b843c749SSergey Zigachev 			mem->bus.addr = (u8 *)adev->mman.aper_base_kaddr +
756b843c749SSergey Zigachev 					mem->bus.offset;
757b843c749SSergey Zigachev 
758b843c749SSergey Zigachev 		mem->bus.base = adev->gmc.aper_base;
759b843c749SSergey Zigachev 		mem->bus.is_iomem = true;
760b843c749SSergey Zigachev 		break;
761b843c749SSergey Zigachev 	default:
762b843c749SSergey Zigachev 		return -EINVAL;
763b843c749SSergey Zigachev 	}
764b843c749SSergey Zigachev 	return 0;
765b843c749SSergey Zigachev }
766b843c749SSergey Zigachev 
amdgpu_ttm_io_mem_free(struct ttm_bo_device * bdev,struct ttm_mem_reg * mem)767b843c749SSergey Zigachev static void amdgpu_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
768b843c749SSergey Zigachev {
769b843c749SSergey Zigachev }
770b843c749SSergey Zigachev 
amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object * bo,unsigned long page_offset)771b843c749SSergey Zigachev static unsigned long amdgpu_ttm_io_mem_pfn(struct ttm_buffer_object *bo,
772b843c749SSergey Zigachev 					   unsigned long page_offset)
773b843c749SSergey Zigachev {
774b843c749SSergey Zigachev 	struct drm_mm_node *mm;
775b843c749SSergey Zigachev 	unsigned long offset = (page_offset << PAGE_SHIFT);
776b843c749SSergey Zigachev 
777b843c749SSergey Zigachev 	mm = amdgpu_find_mm_node(&bo->mem, &offset);
778b843c749SSergey Zigachev 	return (bo->mem.bus.base >> PAGE_SHIFT) + mm->start +
779b843c749SSergey Zigachev 		(offset >> PAGE_SHIFT);
780b843c749SSergey Zigachev }
781b843c749SSergey Zigachev 
782b843c749SSergey Zigachev /*
783b843c749SSergey Zigachev  * TTM backend functions.
784b843c749SSergey Zigachev  */
785b843c749SSergey Zigachev struct amdgpu_ttm_gup_task_list {
786b843c749SSergey Zigachev 	struct list_head	list;
787b843c749SSergey Zigachev 	struct task_struct	*task;
788b843c749SSergey Zigachev };
789b843c749SSergey Zigachev 
790b843c749SSergey Zigachev struct amdgpu_ttm_tt {
791b843c749SSergey Zigachev 	struct ttm_dma_tt	ttm;
792b843c749SSergey Zigachev 	u64			offset;
793b843c749SSergey Zigachev 	uint64_t		userptr;
794b843c749SSergey Zigachev 	struct task_struct	*usertask;
795b843c749SSergey Zigachev 	uint32_t		userflags;
796*78973132SSergey Zigachev 	struct spinlock		guptasklock;
797b843c749SSergey Zigachev 	struct list_head        guptasks;
798b843c749SSergey Zigachev 	atomic_t		mmu_invalidations;
799b843c749SSergey Zigachev 	uint32_t		last_set_pages;
800b843c749SSergey Zigachev };
801b843c749SSergey Zigachev 
802b843c749SSergey Zigachev /**
803b843c749SSergey Zigachev  * amdgpu_ttm_tt_get_user_pages - Pin pages of memory pointed to by a USERPTR
804b843c749SSergey Zigachev  * pointer to memory
805b843c749SSergey Zigachev  *
806b843c749SSergey Zigachev  * Called by amdgpu_gem_userptr_ioctl() and amdgpu_cs_parser_bos().
807b843c749SSergey Zigachev  * This provides a wrapper around the get_user_pages() call to provide
808b843c749SSergey Zigachev  * device accessible pages that back user memory.
809b843c749SSergey Zigachev  */
amdgpu_ttm_tt_get_user_pages(struct ttm_tt * ttm,struct page ** pages)810b843c749SSergey Zigachev int amdgpu_ttm_tt_get_user_pages(struct ttm_tt *ttm, struct page **pages)
811b843c749SSergey Zigachev {
812b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
813b843c749SSergey Zigachev 	struct mm_struct *mm = gtt->usertask->mm;
814b843c749SSergey Zigachev 	unsigned int flags = 0;
815b843c749SSergey Zigachev 	unsigned pinned = 0;
816b843c749SSergey Zigachev 	int r;
817b843c749SSergey Zigachev 
818b843c749SSergey Zigachev 	if (!mm) /* Happens during process shutdown */
819b843c749SSergey Zigachev 		return -ESRCH;
820b843c749SSergey Zigachev 
821b843c749SSergey Zigachev 	if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
822b843c749SSergey Zigachev 		flags |= FOLL_WRITE;
823b843c749SSergey Zigachev 
824b843c749SSergey Zigachev 	down_read(&mm->mmap_sem);
825b843c749SSergey Zigachev 
826*78973132SSergey Zigachev #if 0
827b843c749SSergey Zigachev 	if (gtt->userflags & AMDGPU_GEM_USERPTR_ANONONLY) {
828b843c749SSergey Zigachev 		/*
829b843c749SSergey Zigachev 		 * check that we only use anonymous memory to prevent problems
830b843c749SSergey Zigachev 		 * with writeback
831b843c749SSergey Zigachev 		 */
832b843c749SSergey Zigachev 		unsigned long end = gtt->userptr + ttm->num_pages * PAGE_SIZE;
833b843c749SSergey Zigachev 		struct vm_area_struct *vma;
834b843c749SSergey Zigachev 
835b843c749SSergey Zigachev 		vma = find_vma(mm, gtt->userptr);
836b843c749SSergey Zigachev 		if (!vma || vma->vm_file || vma->vm_end < end) {
837b843c749SSergey Zigachev 			up_read(&mm->mmap_sem);
838b843c749SSergey Zigachev 			return -EPERM;
839b843c749SSergey Zigachev 		}
840b843c749SSergey Zigachev 	}
841*78973132SSergey Zigachev #endif
842b843c749SSergey Zigachev 
843b843c749SSergey Zigachev 	/* loop enough times using contiguous pages of memory */
844b843c749SSergey Zigachev 	do {
845b843c749SSergey Zigachev 		unsigned num_pages = ttm->num_pages - pinned;
846b843c749SSergey Zigachev 		uint64_t userptr = gtt->userptr + pinned * PAGE_SIZE;
847b843c749SSergey Zigachev 		struct page **p = pages + pinned;
848b843c749SSergey Zigachev 		struct amdgpu_ttm_gup_task_list guptask;
849b843c749SSergey Zigachev 
850b843c749SSergey Zigachev 		guptask.task = current;
851b843c749SSergey Zigachev 		spin_lock(&gtt->guptasklock);
852b843c749SSergey Zigachev 		list_add(&guptask.list, &gtt->guptasks);
853b843c749SSergey Zigachev 		spin_unlock(&gtt->guptasklock);
854b843c749SSergey Zigachev 
855*78973132SSergey Zigachev 		r = get_user_pages(userptr, num_pages, flags, p, NULL);
856*78973132SSergey Zigachev 
857*78973132SSergey Zigachev #if 0
858b843c749SSergey Zigachev 		if (mm == current->mm)
859b843c749SSergey Zigachev 			r = get_user_pages(userptr, num_pages, flags, p, NULL);
860b843c749SSergey Zigachev 		else
861b843c749SSergey Zigachev 			r = get_user_pages_remote(gtt->usertask,
862b843c749SSergey Zigachev 					mm, userptr, num_pages,
863b843c749SSergey Zigachev 					flags, p, NULL, NULL);
864*78973132SSergey Zigachev #endif
865b843c749SSergey Zigachev 
866b843c749SSergey Zigachev 		spin_lock(&gtt->guptasklock);
867b843c749SSergey Zigachev 		list_del(&guptask.list);
868b843c749SSergey Zigachev 		spin_unlock(&gtt->guptasklock);
869b843c749SSergey Zigachev 
870b843c749SSergey Zigachev 		if (r < 0)
871b843c749SSergey Zigachev 			goto release_pages;
872b843c749SSergey Zigachev 
873b843c749SSergey Zigachev 		pinned += r;
874b843c749SSergey Zigachev 
875b843c749SSergey Zigachev 	} while (pinned < ttm->num_pages);
876b843c749SSergey Zigachev 
877b843c749SSergey Zigachev 	up_read(&mm->mmap_sem);
878b843c749SSergey Zigachev 	return 0;
879b843c749SSergey Zigachev 
880b843c749SSergey Zigachev release_pages:
881b843c749SSergey Zigachev 	release_pages(pages, pinned);
882b843c749SSergey Zigachev 	up_read(&mm->mmap_sem);
883b843c749SSergey Zigachev 	return r;
884b843c749SSergey Zigachev }
885b843c749SSergey Zigachev 
886b843c749SSergey Zigachev /**
887b843c749SSergey Zigachev  * amdgpu_ttm_tt_set_user_pages - Copy pages in, putting old pages as necessary.
888b843c749SSergey Zigachev  *
889b843c749SSergey Zigachev  * Called by amdgpu_cs_list_validate(). This creates the page list
890b843c749SSergey Zigachev  * that backs user memory and will ultimately be mapped into the device
891b843c749SSergey Zigachev  * address space.
892b843c749SSergey Zigachev  */
amdgpu_ttm_tt_set_user_pages(struct ttm_tt * ttm,struct page ** pages)893b843c749SSergey Zigachev void amdgpu_ttm_tt_set_user_pages(struct ttm_tt *ttm, struct page **pages)
894b843c749SSergey Zigachev {
895b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
896b843c749SSergey Zigachev 	unsigned i;
897b843c749SSergey Zigachev 
898b843c749SSergey Zigachev 	gtt->last_set_pages = atomic_read(&gtt->mmu_invalidations);
899b843c749SSergey Zigachev 	for (i = 0; i < ttm->num_pages; ++i) {
900b843c749SSergey Zigachev 		if (ttm->pages[i])
901b843c749SSergey Zigachev 			put_page(ttm->pages[i]);
902b843c749SSergey Zigachev 
903b843c749SSergey Zigachev 		ttm->pages[i] = pages ? pages[i] : NULL;
904b843c749SSergey Zigachev 	}
905b843c749SSergey Zigachev }
906b843c749SSergey Zigachev 
907b843c749SSergey Zigachev /**
908b843c749SSergey Zigachev  * amdgpu_ttm_tt_mark_user_page - Mark pages as dirty
909b843c749SSergey Zigachev  *
910b843c749SSergey Zigachev  * Called while unpinning userptr pages
911b843c749SSergey Zigachev  */
amdgpu_ttm_tt_mark_user_pages(struct ttm_tt * ttm)912b843c749SSergey Zigachev void amdgpu_ttm_tt_mark_user_pages(struct ttm_tt *ttm)
913b843c749SSergey Zigachev {
914b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
915b843c749SSergey Zigachev 	unsigned i;
916b843c749SSergey Zigachev 
917b843c749SSergey Zigachev 	for (i = 0; i < ttm->num_pages; ++i) {
918b843c749SSergey Zigachev 		struct page *page = ttm->pages[i];
919b843c749SSergey Zigachev 
920b843c749SSergey Zigachev 		if (!page)
921b843c749SSergey Zigachev 			continue;
922b843c749SSergey Zigachev 
923b843c749SSergey Zigachev 		if (!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY))
924b843c749SSergey Zigachev 			set_page_dirty(page);
925b843c749SSergey Zigachev 
926b843c749SSergey Zigachev 		mark_page_accessed(page);
927b843c749SSergey Zigachev 	}
928b843c749SSergey Zigachev }
929b843c749SSergey Zigachev 
930b843c749SSergey Zigachev /**
931b843c749SSergey Zigachev  * amdgpu_ttm_tt_pin_userptr - 	prepare the sg table with the user pages
932b843c749SSergey Zigachev  *
933b843c749SSergey Zigachev  * Called by amdgpu_ttm_backend_bind()
934b843c749SSergey Zigachev  **/
amdgpu_ttm_tt_pin_userptr(struct ttm_tt * ttm)935b843c749SSergey Zigachev static int amdgpu_ttm_tt_pin_userptr(struct ttm_tt *ttm)
936b843c749SSergey Zigachev {
937b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
938b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
939b843c749SSergey Zigachev 	unsigned nents;
940b843c749SSergey Zigachev 	int r;
941b843c749SSergey Zigachev 
942b843c749SSergey Zigachev 	int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
943b843c749SSergey Zigachev 	enum dma_data_direction direction = write ?
944b843c749SSergey Zigachev 		DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
945b843c749SSergey Zigachev 
946b843c749SSergey Zigachev 	/* Allocate an SG array and squash pages into it */
947b843c749SSergey Zigachev 	r = sg_alloc_table_from_pages(ttm->sg, ttm->pages, ttm->num_pages, 0,
948b843c749SSergey Zigachev 				      ttm->num_pages << PAGE_SHIFT,
949b843c749SSergey Zigachev 				      GFP_KERNEL);
950b843c749SSergey Zigachev 	if (r)
951b843c749SSergey Zigachev 		goto release_sg;
952b843c749SSergey Zigachev 
953b843c749SSergey Zigachev 	/* Map SG to device */
954b843c749SSergey Zigachev 	r = -ENOMEM;
955b843c749SSergey Zigachev 	nents = dma_map_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
956b843c749SSergey Zigachev 	if (nents != ttm->sg->nents)
957b843c749SSergey Zigachev 		goto release_sg;
958b843c749SSergey Zigachev 
959b843c749SSergey Zigachev 	/* convert SG to linear array of pages and dma addresses */
960b843c749SSergey Zigachev 	drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
961b843c749SSergey Zigachev 					 gtt->ttm.dma_address, ttm->num_pages);
962b843c749SSergey Zigachev 
963b843c749SSergey Zigachev 	return 0;
964b843c749SSergey Zigachev 
965b843c749SSergey Zigachev release_sg:
966b843c749SSergey Zigachev 	kfree(ttm->sg);
967b843c749SSergey Zigachev 	ttm->sg = NULL;
968b843c749SSergey Zigachev 	return r;
969b843c749SSergey Zigachev }
970b843c749SSergey Zigachev 
971b843c749SSergey Zigachev /**
972b843c749SSergey Zigachev  * amdgpu_ttm_tt_unpin_userptr - Unpin and unmap userptr pages
973b843c749SSergey Zigachev  */
amdgpu_ttm_tt_unpin_userptr(struct ttm_tt * ttm)974b843c749SSergey Zigachev static void amdgpu_ttm_tt_unpin_userptr(struct ttm_tt *ttm)
975b843c749SSergey Zigachev {
976b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
977b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
978b843c749SSergey Zigachev 
979b843c749SSergey Zigachev 	int write = !(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
980b843c749SSergey Zigachev 	enum dma_data_direction direction = write ?
981b843c749SSergey Zigachev 		DMA_BIDIRECTIONAL : DMA_TO_DEVICE;
982b843c749SSergey Zigachev 
983b843c749SSergey Zigachev 	/* double check that we don't free the table twice */
984b843c749SSergey Zigachev 	if (!ttm->sg || !ttm->sg->sgl)
985b843c749SSergey Zigachev 		return;
986b843c749SSergey Zigachev 
987b843c749SSergey Zigachev 	/* unmap the pages mapped to the device */
988b843c749SSergey Zigachev 	dma_unmap_sg(adev->dev, ttm->sg->sgl, ttm->sg->nents, direction);
989b843c749SSergey Zigachev 
990b843c749SSergey Zigachev 	/* mark the pages as dirty */
991b843c749SSergey Zigachev 	amdgpu_ttm_tt_mark_user_pages(ttm);
992b843c749SSergey Zigachev 
993b843c749SSergey Zigachev 	sg_free_table(ttm->sg);
994b843c749SSergey Zigachev }
995b843c749SSergey Zigachev 
996b843c749SSergey Zigachev int amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
997b843c749SSergey Zigachev 				struct ttm_buffer_object *tbo,
998*78973132SSergey Zigachev 				uint64_t flags);
amdgpu_ttm_gart_bind(struct amdgpu_device * adev,struct ttm_buffer_object * tbo,uint64_t flags)999*78973132SSergey Zigachev int amdgpu_ttm_gart_bind(struct amdgpu_device *adev,
1000*78973132SSergey Zigachev 				struct ttm_buffer_object *tbo,
1001b843c749SSergey Zigachev 				uint64_t flags)
1002b843c749SSergey Zigachev {
1003b843c749SSergey Zigachev 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(tbo);
1004b843c749SSergey Zigachev 	struct ttm_tt *ttm = tbo->ttm;
1005b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1006b843c749SSergey Zigachev 	int r;
1007b843c749SSergey Zigachev 
1008b843c749SSergey Zigachev 	if (abo->flags & AMDGPU_GEM_CREATE_MQD_GFX9) {
1009b843c749SSergey Zigachev 		uint64_t page_idx = 1;
1010b843c749SSergey Zigachev 
1011b843c749SSergey Zigachev 		r = amdgpu_gart_bind(adev, gtt->offset, page_idx,
1012b843c749SSergey Zigachev 				ttm->pages, gtt->ttm.dma_address, flags);
1013b843c749SSergey Zigachev 		if (r)
1014b843c749SSergey Zigachev 			goto gart_bind_fail;
1015b843c749SSergey Zigachev 
1016b843c749SSergey Zigachev 		/* Patch mtype of the second part BO */
1017b843c749SSergey Zigachev 		flags &=  ~AMDGPU_PTE_MTYPE_MASK;
1018b843c749SSergey Zigachev 		flags |= AMDGPU_PTE_MTYPE(AMDGPU_MTYPE_NC);
1019b843c749SSergey Zigachev 
1020b843c749SSergey Zigachev 		r = amdgpu_gart_bind(adev,
1021b843c749SSergey Zigachev 				gtt->offset + (page_idx << PAGE_SHIFT),
1022b843c749SSergey Zigachev 				ttm->num_pages - page_idx,
1023b843c749SSergey Zigachev 				&ttm->pages[page_idx],
1024b843c749SSergey Zigachev 				&(gtt->ttm.dma_address[page_idx]), flags);
1025b843c749SSergey Zigachev 	} else {
1026b843c749SSergey Zigachev 		r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1027b843c749SSergey Zigachev 				     ttm->pages, gtt->ttm.dma_address, flags);
1028b843c749SSergey Zigachev 	}
1029b843c749SSergey Zigachev 
1030b843c749SSergey Zigachev gart_bind_fail:
1031b843c749SSergey Zigachev 	if (r)
1032b843c749SSergey Zigachev 		DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1033b843c749SSergey Zigachev 			  ttm->num_pages, gtt->offset);
1034b843c749SSergey Zigachev 
1035b843c749SSergey Zigachev 	return r;
1036b843c749SSergey Zigachev }
1037b843c749SSergey Zigachev 
1038b843c749SSergey Zigachev /**
1039b843c749SSergey Zigachev  * amdgpu_ttm_backend_bind - Bind GTT memory
1040b843c749SSergey Zigachev  *
1041b843c749SSergey Zigachev  * Called by ttm_tt_bind() on behalf of ttm_bo_handle_move_mem().
1042b843c749SSergey Zigachev  * This handles binding GTT memory to the device address space.
1043b843c749SSergey Zigachev  */
amdgpu_ttm_backend_bind(struct ttm_tt * ttm,struct ttm_mem_reg * bo_mem)1044b843c749SSergey Zigachev static int amdgpu_ttm_backend_bind(struct ttm_tt *ttm,
1045b843c749SSergey Zigachev 				   struct ttm_mem_reg *bo_mem)
1046b843c749SSergey Zigachev {
1047b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1048b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void*)ttm;
1049b843c749SSergey Zigachev 	uint64_t flags;
1050b843c749SSergey Zigachev 	int r = 0;
1051b843c749SSergey Zigachev 
1052b843c749SSergey Zigachev 	if (gtt->userptr) {
1053b843c749SSergey Zigachev 		r = amdgpu_ttm_tt_pin_userptr(ttm);
1054b843c749SSergey Zigachev 		if (r) {
1055b843c749SSergey Zigachev 			DRM_ERROR("failed to pin userptr\n");
1056b843c749SSergey Zigachev 			return r;
1057b843c749SSergey Zigachev 		}
1058b843c749SSergey Zigachev 	}
1059b843c749SSergey Zigachev 	if (!ttm->num_pages) {
1060b843c749SSergey Zigachev 		WARN(1, "nothing to bind %lu pages for mreg %p back %p!\n",
1061b843c749SSergey Zigachev 		     ttm->num_pages, bo_mem, ttm);
1062b843c749SSergey Zigachev 	}
1063b843c749SSergey Zigachev 
1064b843c749SSergey Zigachev 	if (bo_mem->mem_type == AMDGPU_PL_GDS ||
1065b843c749SSergey Zigachev 	    bo_mem->mem_type == AMDGPU_PL_GWS ||
1066b843c749SSergey Zigachev 	    bo_mem->mem_type == AMDGPU_PL_OA)
1067b843c749SSergey Zigachev 		return -EINVAL;
1068b843c749SSergey Zigachev 
1069b843c749SSergey Zigachev 	if (!amdgpu_gtt_mgr_has_gart_addr(bo_mem)) {
1070b843c749SSergey Zigachev 		gtt->offset = AMDGPU_BO_INVALID_OFFSET;
1071b843c749SSergey Zigachev 		return 0;
1072b843c749SSergey Zigachev 	}
1073b843c749SSergey Zigachev 
1074b843c749SSergey Zigachev 	/* compute PTE flags relevant to this BO memory */
1075b843c749SSergey Zigachev 	flags = amdgpu_ttm_tt_pte_flags(adev, ttm, bo_mem);
1076b843c749SSergey Zigachev 
1077b843c749SSergey Zigachev 	/* bind pages into GART page tables */
1078b843c749SSergey Zigachev 	gtt->offset = (u64)bo_mem->start << PAGE_SHIFT;
1079b843c749SSergey Zigachev 	r = amdgpu_gart_bind(adev, gtt->offset, ttm->num_pages,
1080b843c749SSergey Zigachev 		ttm->pages, gtt->ttm.dma_address, flags);
1081b843c749SSergey Zigachev 
1082b843c749SSergey Zigachev 	if (r)
1083b843c749SSergey Zigachev 		DRM_ERROR("failed to bind %lu pages at 0x%08llX\n",
1084b843c749SSergey Zigachev 			  ttm->num_pages, gtt->offset);
1085b843c749SSergey Zigachev 	return r;
1086b843c749SSergey Zigachev }
1087b843c749SSergey Zigachev 
1088b843c749SSergey Zigachev /**
1089b843c749SSergey Zigachev  * amdgpu_ttm_alloc_gart - Allocate GART memory for buffer object
1090b843c749SSergey Zigachev  */
amdgpu_ttm_alloc_gart(struct ttm_buffer_object * bo)1091b843c749SSergey Zigachev int amdgpu_ttm_alloc_gart(struct ttm_buffer_object *bo)
1092b843c749SSergey Zigachev {
1093b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->bdev);
1094b843c749SSergey Zigachev 	struct ttm_operation_ctx ctx = { false, false };
1095b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void*)bo->ttm;
1096b843c749SSergey Zigachev 	struct ttm_mem_reg tmp;
1097b843c749SSergey Zigachev 	struct ttm_placement placement;
1098b843c749SSergey Zigachev 	struct ttm_place placements;
1099b843c749SSergey Zigachev 	uint64_t flags;
1100b843c749SSergey Zigachev 	int r;
1101b843c749SSergey Zigachev 
1102b843c749SSergey Zigachev 	if (bo->mem.mem_type != TTM_PL_TT ||
1103b843c749SSergey Zigachev 	    amdgpu_gtt_mgr_has_gart_addr(&bo->mem))
1104b843c749SSergey Zigachev 		return 0;
1105b843c749SSergey Zigachev 
1106b843c749SSergey Zigachev 	/* allocate GTT space */
1107b843c749SSergey Zigachev 	tmp = bo->mem;
1108b843c749SSergey Zigachev 	tmp.mm_node = NULL;
1109b843c749SSergey Zigachev 	placement.num_placement = 1;
1110b843c749SSergey Zigachev 	placement.placement = &placements;
1111b843c749SSergey Zigachev 	placement.num_busy_placement = 1;
1112b843c749SSergey Zigachev 	placement.busy_placement = &placements;
1113b843c749SSergey Zigachev 	placements.fpfn = 0;
1114b843c749SSergey Zigachev 	placements.lpfn = adev->gmc.gart_size >> PAGE_SHIFT;
1115b843c749SSergey Zigachev 	placements.flags = (bo->mem.placement & ~TTM_PL_MASK_MEM) |
1116b843c749SSergey Zigachev 		TTM_PL_FLAG_TT;
1117b843c749SSergey Zigachev 
1118b843c749SSergey Zigachev 	r = ttm_bo_mem_space(bo, &placement, &tmp, &ctx);
1119b843c749SSergey Zigachev 	if (unlikely(r))
1120b843c749SSergey Zigachev 		return r;
1121b843c749SSergey Zigachev 
1122b843c749SSergey Zigachev 	/* compute PTE flags for this buffer object */
1123b843c749SSergey Zigachev 	flags = amdgpu_ttm_tt_pte_flags(adev, bo->ttm, &tmp);
1124b843c749SSergey Zigachev 
1125b843c749SSergey Zigachev 	/* Bind pages */
1126b843c749SSergey Zigachev 	gtt->offset = (u64)tmp.start << PAGE_SHIFT;
1127b843c749SSergey Zigachev 	r = amdgpu_ttm_gart_bind(adev, bo, flags);
1128b843c749SSergey Zigachev 	if (unlikely(r)) {
1129b843c749SSergey Zigachev 		ttm_bo_mem_put(bo, &tmp);
1130b843c749SSergey Zigachev 		return r;
1131b843c749SSergey Zigachev 	}
1132b843c749SSergey Zigachev 
1133b843c749SSergey Zigachev 	ttm_bo_mem_put(bo, &bo->mem);
1134b843c749SSergey Zigachev 	bo->mem = tmp;
1135b843c749SSergey Zigachev 	bo->offset = (bo->mem.start << PAGE_SHIFT) +
1136b843c749SSergey Zigachev 		bo->bdev->man[bo->mem.mem_type].gpu_offset;
1137b843c749SSergey Zigachev 
1138b843c749SSergey Zigachev 	return 0;
1139b843c749SSergey Zigachev }
1140b843c749SSergey Zigachev 
1141b843c749SSergey Zigachev /**
1142b843c749SSergey Zigachev  * amdgpu_ttm_recover_gart - Rebind GTT pages
1143b843c749SSergey Zigachev  *
1144b843c749SSergey Zigachev  * Called by amdgpu_gtt_mgr_recover() from amdgpu_device_reset() to
1145b843c749SSergey Zigachev  * rebind GTT pages during a GPU reset.
1146b843c749SSergey Zigachev  */
amdgpu_ttm_recover_gart(struct ttm_buffer_object * tbo)1147b843c749SSergey Zigachev int amdgpu_ttm_recover_gart(struct ttm_buffer_object *tbo)
1148b843c749SSergey Zigachev {
1149b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(tbo->bdev);
1150b843c749SSergey Zigachev 	uint64_t flags;
1151b843c749SSergey Zigachev 	int r;
1152b843c749SSergey Zigachev 
1153b843c749SSergey Zigachev 	if (!tbo->ttm)
1154b843c749SSergey Zigachev 		return 0;
1155b843c749SSergey Zigachev 
1156b843c749SSergey Zigachev 	flags = amdgpu_ttm_tt_pte_flags(adev, tbo->ttm, &tbo->mem);
1157b843c749SSergey Zigachev 	r = amdgpu_ttm_gart_bind(adev, tbo, flags);
1158b843c749SSergey Zigachev 
1159b843c749SSergey Zigachev 	return r;
1160b843c749SSergey Zigachev }
1161b843c749SSergey Zigachev 
1162b843c749SSergey Zigachev /**
1163b843c749SSergey Zigachev  * amdgpu_ttm_backend_unbind - Unbind GTT mapped pages
1164b843c749SSergey Zigachev  *
1165b843c749SSergey Zigachev  * Called by ttm_tt_unbind() on behalf of ttm_bo_move_ttm() and
1166b843c749SSergey Zigachev  * ttm_tt_destroy().
1167b843c749SSergey Zigachev  */
amdgpu_ttm_backend_unbind(struct ttm_tt * ttm)1168b843c749SSergey Zigachev static int amdgpu_ttm_backend_unbind(struct ttm_tt *ttm)
1169b843c749SSergey Zigachev {
1170b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1171b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1172b843c749SSergey Zigachev 	int r;
1173b843c749SSergey Zigachev 
1174b843c749SSergey Zigachev 	/* if the pages have userptr pinning then clear that first */
1175b843c749SSergey Zigachev 	if (gtt->userptr)
1176b843c749SSergey Zigachev 		amdgpu_ttm_tt_unpin_userptr(ttm);
1177b843c749SSergey Zigachev 
1178b843c749SSergey Zigachev 	if (gtt->offset == AMDGPU_BO_INVALID_OFFSET)
1179b843c749SSergey Zigachev 		return 0;
1180b843c749SSergey Zigachev 
1181b843c749SSergey Zigachev 	/* unbind shouldn't be done for GDS/GWS/OA in ttm_bo_clean_mm */
1182b843c749SSergey Zigachev 	r = amdgpu_gart_unbind(adev, gtt->offset, ttm->num_pages);
1183b843c749SSergey Zigachev 	if (r)
1184b843c749SSergey Zigachev 		DRM_ERROR("failed to unbind %lu pages at 0x%08llX\n",
1185b843c749SSergey Zigachev 			  gtt->ttm.ttm.num_pages, gtt->offset);
1186b843c749SSergey Zigachev 	return r;
1187b843c749SSergey Zigachev }
1188b843c749SSergey Zigachev 
amdgpu_ttm_backend_destroy(struct ttm_tt * ttm)1189b843c749SSergey Zigachev static void amdgpu_ttm_backend_destroy(struct ttm_tt *ttm)
1190b843c749SSergey Zigachev {
1191b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1192b843c749SSergey Zigachev 
1193b843c749SSergey Zigachev 	if (gtt->usertask)
1194b843c749SSergey Zigachev 		put_task_struct(gtt->usertask);
1195b843c749SSergey Zigachev 
1196b843c749SSergey Zigachev 	ttm_dma_tt_fini(&gtt->ttm);
1197b843c749SSergey Zigachev 	kfree(gtt);
1198b843c749SSergey Zigachev }
1199b843c749SSergey Zigachev 
1200b843c749SSergey Zigachev static struct ttm_backend_func amdgpu_backend_func = {
1201b843c749SSergey Zigachev 	.bind = &amdgpu_ttm_backend_bind,
1202b843c749SSergey Zigachev 	.unbind = &amdgpu_ttm_backend_unbind,
1203b843c749SSergey Zigachev 	.destroy = &amdgpu_ttm_backend_destroy,
1204b843c749SSergey Zigachev };
1205b843c749SSergey Zigachev 
1206b843c749SSergey Zigachev /**
1207b843c749SSergey Zigachev  * amdgpu_ttm_tt_create - Create a ttm_tt object for a given BO
1208b843c749SSergey Zigachev  *
1209b843c749SSergey Zigachev  * @bo: The buffer object to create a GTT ttm_tt object around
1210b843c749SSergey Zigachev  *
1211b843c749SSergey Zigachev  * Called by ttm_tt_create().
1212b843c749SSergey Zigachev  */
amdgpu_ttm_tt_create(struct ttm_buffer_object * bo,uint32_t page_flags)1213b843c749SSergey Zigachev static struct ttm_tt *amdgpu_ttm_tt_create(struct ttm_buffer_object *bo,
1214b843c749SSergey Zigachev 					   uint32_t page_flags)
1215b843c749SSergey Zigachev {
1216b843c749SSergey Zigachev 	struct amdgpu_device *adev;
1217b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt;
1218b843c749SSergey Zigachev 
1219b843c749SSergey Zigachev 	adev = amdgpu_ttm_adev(bo->bdev);
1220b843c749SSergey Zigachev 
1221b843c749SSergey Zigachev 	gtt = kzalloc(sizeof(struct amdgpu_ttm_tt), GFP_KERNEL);
1222b843c749SSergey Zigachev 	if (gtt == NULL) {
1223b843c749SSergey Zigachev 		return NULL;
1224b843c749SSergey Zigachev 	}
1225b843c749SSergey Zigachev 	gtt->ttm.ttm.func = &amdgpu_backend_func;
1226b843c749SSergey Zigachev 
1227b843c749SSergey Zigachev 	/* allocate space for the uninitialized page entries */
1228b843c749SSergey Zigachev 	if (ttm_sg_tt_init(&gtt->ttm, bo, page_flags)) {
1229b843c749SSergey Zigachev 		kfree(gtt);
1230b843c749SSergey Zigachev 		return NULL;
1231b843c749SSergey Zigachev 	}
1232b843c749SSergey Zigachev 	return &gtt->ttm.ttm;
1233b843c749SSergey Zigachev }
1234b843c749SSergey Zigachev 
1235b843c749SSergey Zigachev /**
1236b843c749SSergey Zigachev  * amdgpu_ttm_tt_populate - Map GTT pages visible to the device
1237b843c749SSergey Zigachev  *
1238b843c749SSergey Zigachev  * Map the pages of a ttm_tt object to an address space visible
1239b843c749SSergey Zigachev  * to the underlying device.
1240b843c749SSergey Zigachev  */
amdgpu_ttm_tt_populate(struct ttm_tt * ttm,struct ttm_operation_ctx * ctx)1241b843c749SSergey Zigachev static int amdgpu_ttm_tt_populate(struct ttm_tt *ttm,
1242b843c749SSergey Zigachev 			struct ttm_operation_ctx *ctx)
1243b843c749SSergey Zigachev {
1244b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(ttm->bdev);
1245b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1246b843c749SSergey Zigachev 	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1247b843c749SSergey Zigachev 
1248b843c749SSergey Zigachev 	/* user pages are bound by amdgpu_ttm_tt_pin_userptr() */
1249b843c749SSergey Zigachev 	if (gtt && gtt->userptr) {
1250b843c749SSergey Zigachev 		ttm->sg = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
1251b843c749SSergey Zigachev 		if (!ttm->sg)
1252b843c749SSergey Zigachev 			return -ENOMEM;
1253b843c749SSergey Zigachev 
1254b843c749SSergey Zigachev 		ttm->page_flags |= TTM_PAGE_FLAG_SG;
1255b843c749SSergey Zigachev 		ttm->state = tt_unbound;
1256b843c749SSergey Zigachev 		return 0;
1257b843c749SSergey Zigachev 	}
1258b843c749SSergey Zigachev 
1259b843c749SSergey Zigachev 	if (slave && ttm->sg) {
1260b843c749SSergey Zigachev 		drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1261b843c749SSergey Zigachev 						 gtt->ttm.dma_address,
1262b843c749SSergey Zigachev 						 ttm->num_pages);
1263b843c749SSergey Zigachev 		ttm->state = tt_unbound;
1264b843c749SSergey Zigachev 		return 0;
1265b843c749SSergey Zigachev 	}
1266b843c749SSergey Zigachev 
1267b843c749SSergey Zigachev #ifdef CONFIG_SWIOTLB
1268b843c749SSergey Zigachev 	if (adev->need_swiotlb && swiotlb_nr_tbl()) {
1269b843c749SSergey Zigachev 		return ttm_dma_populate(&gtt->ttm, adev->dev, ctx);
1270b843c749SSergey Zigachev 	}
1271b843c749SSergey Zigachev #endif
1272b843c749SSergey Zigachev 
1273b843c749SSergey Zigachev 	/* fall back to generic helper to populate the page array
1274b843c749SSergey Zigachev 	 * and map them to the device */
1275b843c749SSergey Zigachev 	return ttm_populate_and_map_pages(adev->dev, &gtt->ttm, ctx);
1276b843c749SSergey Zigachev }
1277b843c749SSergey Zigachev 
1278b843c749SSergey Zigachev /**
1279b843c749SSergey Zigachev  * amdgpu_ttm_tt_unpopulate - unmap GTT pages and unpopulate page arrays
1280b843c749SSergey Zigachev  *
1281b843c749SSergey Zigachev  * Unmaps pages of a ttm_tt object from the device address space and
1282b843c749SSergey Zigachev  * unpopulates the page array backing it.
1283b843c749SSergey Zigachev  */
amdgpu_ttm_tt_unpopulate(struct ttm_tt * ttm)1284b843c749SSergey Zigachev static void amdgpu_ttm_tt_unpopulate(struct ttm_tt *ttm)
1285b843c749SSergey Zigachev {
1286b843c749SSergey Zigachev 	struct amdgpu_device *adev;
1287b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1288b843c749SSergey Zigachev 	bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1289b843c749SSergey Zigachev 
1290b843c749SSergey Zigachev 	if (gtt && gtt->userptr) {
1291b843c749SSergey Zigachev 		amdgpu_ttm_tt_set_user_pages(ttm, NULL);
1292b843c749SSergey Zigachev 		kfree(ttm->sg);
1293b843c749SSergey Zigachev 		ttm->sg = NULL;
1294b843c749SSergey Zigachev 		ttm->page_flags &= ~TTM_PAGE_FLAG_SG;
1295b843c749SSergey Zigachev 		return;
1296b843c749SSergey Zigachev 	}
1297b843c749SSergey Zigachev 
1298b843c749SSergey Zigachev 	if (slave)
1299b843c749SSergey Zigachev 		return;
1300b843c749SSergey Zigachev 
1301b843c749SSergey Zigachev 	adev = amdgpu_ttm_adev(ttm->bdev);
1302b843c749SSergey Zigachev 
1303b843c749SSergey Zigachev #ifdef CONFIG_SWIOTLB
1304b843c749SSergey Zigachev 	if (adev->need_swiotlb && swiotlb_nr_tbl()) {
1305b843c749SSergey Zigachev 		ttm_dma_unpopulate(&gtt->ttm, adev->dev);
1306b843c749SSergey Zigachev 		return;
1307b843c749SSergey Zigachev 	}
1308b843c749SSergey Zigachev #endif
1309b843c749SSergey Zigachev 
1310b843c749SSergey Zigachev 	/* fall back to generic helper to unmap and unpopulate array */
1311b843c749SSergey Zigachev 	ttm_unmap_and_unpopulate_pages(adev->dev, &gtt->ttm);
1312b843c749SSergey Zigachev }
1313b843c749SSergey Zigachev 
1314b843c749SSergey Zigachev /**
1315b843c749SSergey Zigachev  * amdgpu_ttm_tt_set_userptr - Initialize userptr GTT ttm_tt for the current
1316b843c749SSergey Zigachev  * task
1317b843c749SSergey Zigachev  *
1318b843c749SSergey Zigachev  * @ttm: The ttm_tt object to bind this userptr object to
1319b843c749SSergey Zigachev  * @addr:  The address in the current tasks VM space to use
1320b843c749SSergey Zigachev  * @flags: Requirements of userptr object.
1321b843c749SSergey Zigachev  *
1322b843c749SSergey Zigachev  * Called by amdgpu_gem_userptr_ioctl() to bind userptr pages
1323b843c749SSergey Zigachev  * to current task
1324b843c749SSergey Zigachev  */
amdgpu_ttm_tt_set_userptr(struct ttm_tt * ttm,uint64_t addr,uint32_t flags)1325b843c749SSergey Zigachev int amdgpu_ttm_tt_set_userptr(struct ttm_tt *ttm, uint64_t addr,
1326b843c749SSergey Zigachev 			      uint32_t flags)
1327b843c749SSergey Zigachev {
1328b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1329b843c749SSergey Zigachev 
1330b843c749SSergey Zigachev 	if (gtt == NULL)
1331b843c749SSergey Zigachev 		return -EINVAL;
1332b843c749SSergey Zigachev 
1333b843c749SSergey Zigachev 	gtt->userptr = addr;
1334b843c749SSergey Zigachev 	gtt->userflags = flags;
1335b843c749SSergey Zigachev 
1336b843c749SSergey Zigachev 	if (gtt->usertask)
1337b843c749SSergey Zigachev 		put_task_struct(gtt->usertask);
1338*78973132SSergey Zigachev kprintf("amdgpu_ttm_tt_set_userptr: gtt->usertask will not be set\n");
1339*78973132SSergey Zigachev #if 0
1340b843c749SSergey Zigachev 	gtt->usertask = current->group_leader;
1341b843c749SSergey Zigachev 	get_task_struct(gtt->usertask);
1342*78973132SSergey Zigachev #endif
1343b843c749SSergey Zigachev 
1344*78973132SSergey Zigachev 	spin_init(&gtt->guptasklock, "agttgutl");
1345b843c749SSergey Zigachev 	INIT_LIST_HEAD(&gtt->guptasks);
1346b843c749SSergey Zigachev 	atomic_set(&gtt->mmu_invalidations, 0);
1347b843c749SSergey Zigachev 	gtt->last_set_pages = 0;
1348b843c749SSergey Zigachev 
1349b843c749SSergey Zigachev 	return 0;
1350b843c749SSergey Zigachev }
1351b843c749SSergey Zigachev 
1352b843c749SSergey Zigachev /**
1353b843c749SSergey Zigachev  * amdgpu_ttm_tt_get_usermm - Return memory manager for ttm_tt object
1354b843c749SSergey Zigachev  */
amdgpu_ttm_tt_get_usermm(struct ttm_tt * ttm)1355b843c749SSergey Zigachev struct mm_struct *amdgpu_ttm_tt_get_usermm(struct ttm_tt *ttm)
1356b843c749SSergey Zigachev {
1357b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1358b843c749SSergey Zigachev 
1359b843c749SSergey Zigachev 	if (gtt == NULL)
1360b843c749SSergey Zigachev 		return NULL;
1361b843c749SSergey Zigachev 
1362b843c749SSergey Zigachev 	if (gtt->usertask == NULL)
1363b843c749SSergey Zigachev 		return NULL;
1364b843c749SSergey Zigachev 
1365b843c749SSergey Zigachev 	return gtt->usertask->mm;
1366b843c749SSergey Zigachev }
1367b843c749SSergey Zigachev 
1368b843c749SSergey Zigachev /**
1369b843c749SSergey Zigachev  * amdgpu_ttm_tt_affect_userptr - Determine if a ttm_tt object lays inside an
1370b843c749SSergey Zigachev  * address range for the current task.
1371b843c749SSergey Zigachev  *
1372b843c749SSergey Zigachev  */
amdgpu_ttm_tt_affect_userptr(struct ttm_tt * ttm,unsigned long start,unsigned long end)1373b843c749SSergey Zigachev bool amdgpu_ttm_tt_affect_userptr(struct ttm_tt *ttm, unsigned long start,
1374b843c749SSergey Zigachev 				  unsigned long end)
1375b843c749SSergey Zigachev {
1376b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1377b843c749SSergey Zigachev 	struct amdgpu_ttm_gup_task_list *entry;
1378b843c749SSergey Zigachev 	unsigned long size;
1379b843c749SSergey Zigachev 
1380b843c749SSergey Zigachev 	if (gtt == NULL || !gtt->userptr)
1381b843c749SSergey Zigachev 		return false;
1382b843c749SSergey Zigachev 
1383b843c749SSergey Zigachev 	/* Return false if no part of the ttm_tt object lies within
1384b843c749SSergey Zigachev 	 * the range
1385b843c749SSergey Zigachev 	 */
1386b843c749SSergey Zigachev 	size = (unsigned long)gtt->ttm.ttm.num_pages * PAGE_SIZE;
1387b843c749SSergey Zigachev 	if (gtt->userptr > end || gtt->userptr + size <= start)
1388b843c749SSergey Zigachev 		return false;
1389b843c749SSergey Zigachev 
1390b843c749SSergey Zigachev 	/* Search the lists of tasks that hold this mapping and see
1391b843c749SSergey Zigachev 	 * if current is one of them.  If it is return false.
1392b843c749SSergey Zigachev 	 */
1393b843c749SSergey Zigachev 	spin_lock(&gtt->guptasklock);
1394b843c749SSergey Zigachev 	list_for_each_entry(entry, &gtt->guptasks, list) {
1395b843c749SSergey Zigachev 		if (entry->task == current) {
1396b843c749SSergey Zigachev 			spin_unlock(&gtt->guptasklock);
1397b843c749SSergey Zigachev 			return false;
1398b843c749SSergey Zigachev 		}
1399b843c749SSergey Zigachev 	}
1400b843c749SSergey Zigachev 	spin_unlock(&gtt->guptasklock);
1401b843c749SSergey Zigachev 
1402b843c749SSergey Zigachev 	atomic_inc(&gtt->mmu_invalidations);
1403b843c749SSergey Zigachev 
1404b843c749SSergey Zigachev 	return true;
1405b843c749SSergey Zigachev }
1406b843c749SSergey Zigachev 
1407b843c749SSergey Zigachev /**
1408b843c749SSergey Zigachev  * amdgpu_ttm_tt_userptr_invalidated - Has the ttm_tt object been invalidated?
1409b843c749SSergey Zigachev  */
amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt * ttm,int * last_invalidated)1410b843c749SSergey Zigachev bool amdgpu_ttm_tt_userptr_invalidated(struct ttm_tt *ttm,
1411b843c749SSergey Zigachev 				       int *last_invalidated)
1412b843c749SSergey Zigachev {
1413b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1414b843c749SSergey Zigachev 	int prev_invalidated = *last_invalidated;
1415b843c749SSergey Zigachev 
1416b843c749SSergey Zigachev 	*last_invalidated = atomic_read(&gtt->mmu_invalidations);
1417b843c749SSergey Zigachev 	return prev_invalidated != *last_invalidated;
1418b843c749SSergey Zigachev }
1419b843c749SSergey Zigachev 
1420b843c749SSergey Zigachev /**
1421b843c749SSergey Zigachev  * amdgpu_ttm_tt_userptr_needs_pages - Have the pages backing this ttm_tt object
1422b843c749SSergey Zigachev  * been invalidated since the last time they've been set?
1423b843c749SSergey Zigachev  */
amdgpu_ttm_tt_userptr_needs_pages(struct ttm_tt * ttm)1424b843c749SSergey Zigachev bool amdgpu_ttm_tt_userptr_needs_pages(struct ttm_tt *ttm)
1425b843c749SSergey Zigachev {
1426b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1427b843c749SSergey Zigachev 
1428b843c749SSergey Zigachev 	if (gtt == NULL || !gtt->userptr)
1429b843c749SSergey Zigachev 		return false;
1430b843c749SSergey Zigachev 
1431b843c749SSergey Zigachev 	return atomic_read(&gtt->mmu_invalidations) != gtt->last_set_pages;
1432b843c749SSergey Zigachev }
1433b843c749SSergey Zigachev 
1434b843c749SSergey Zigachev /**
1435b843c749SSergey Zigachev  * amdgpu_ttm_tt_is_readonly - Is the ttm_tt object read only?
1436b843c749SSergey Zigachev  */
amdgpu_ttm_tt_is_readonly(struct ttm_tt * ttm)1437b843c749SSergey Zigachev bool amdgpu_ttm_tt_is_readonly(struct ttm_tt *ttm)
1438b843c749SSergey Zigachev {
1439b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)ttm;
1440b843c749SSergey Zigachev 
1441b843c749SSergey Zigachev 	if (gtt == NULL)
1442b843c749SSergey Zigachev 		return false;
1443b843c749SSergey Zigachev 
1444b843c749SSergey Zigachev 	return !!(gtt->userflags & AMDGPU_GEM_USERPTR_READONLY);
1445b843c749SSergey Zigachev }
1446b843c749SSergey Zigachev 
1447b843c749SSergey Zigachev /**
1448b843c749SSergey Zigachev  * amdgpu_ttm_tt_pte_flags - Compute PTE flags for ttm_tt object
1449b843c749SSergey Zigachev  *
1450b843c749SSergey Zigachev  * @ttm: The ttm_tt object to compute the flags for
1451b843c749SSergey Zigachev  * @mem: The memory registry backing this ttm_tt object
1452b843c749SSergey Zigachev  */
amdgpu_ttm_tt_pte_flags(struct amdgpu_device * adev,struct ttm_tt * ttm,struct ttm_mem_reg * mem)1453b843c749SSergey Zigachev uint64_t amdgpu_ttm_tt_pte_flags(struct amdgpu_device *adev, struct ttm_tt *ttm,
1454b843c749SSergey Zigachev 				 struct ttm_mem_reg *mem)
1455b843c749SSergey Zigachev {
1456b843c749SSergey Zigachev 	uint64_t flags = 0;
1457b843c749SSergey Zigachev 
1458b843c749SSergey Zigachev 	if (mem && mem->mem_type != TTM_PL_SYSTEM)
1459b843c749SSergey Zigachev 		flags |= AMDGPU_PTE_VALID;
1460b843c749SSergey Zigachev 
1461b843c749SSergey Zigachev 	if (mem && mem->mem_type == TTM_PL_TT) {
1462b843c749SSergey Zigachev 		flags |= AMDGPU_PTE_SYSTEM;
1463b843c749SSergey Zigachev 
1464b843c749SSergey Zigachev 		if (ttm->caching_state == tt_cached)
1465b843c749SSergey Zigachev 			flags |= AMDGPU_PTE_SNOOPED;
1466b843c749SSergey Zigachev 	}
1467b843c749SSergey Zigachev 
1468b843c749SSergey Zigachev 	flags |= adev->gart.gart_pte_flags;
1469b843c749SSergey Zigachev 	flags |= AMDGPU_PTE_READABLE;
1470b843c749SSergey Zigachev 
1471b843c749SSergey Zigachev 	if (!amdgpu_ttm_tt_is_readonly(ttm))
1472b843c749SSergey Zigachev 		flags |= AMDGPU_PTE_WRITEABLE;
1473b843c749SSergey Zigachev 
1474b843c749SSergey Zigachev 	return flags;
1475b843c749SSergey Zigachev }
1476b843c749SSergey Zigachev 
1477b843c749SSergey Zigachev /**
1478b843c749SSergey Zigachev  * amdgpu_ttm_bo_eviction_valuable - Check to see if we can evict a buffer
1479b843c749SSergey Zigachev  * object.
1480b843c749SSergey Zigachev  *
1481b843c749SSergey Zigachev  * Return true if eviction is sensible. Called by ttm_mem_evict_first() on
1482b843c749SSergey Zigachev  * behalf of ttm_bo_mem_force_space() which tries to evict buffer objects until
1483b843c749SSergey Zigachev  * it can find space for a new object and by ttm_bo_force_list_clean() which is
1484b843c749SSergey Zigachev  * used to clean out a memory space.
1485b843c749SSergey Zigachev  */
amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object * bo,const struct ttm_place * place)1486b843c749SSergey Zigachev static bool amdgpu_ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
1487b843c749SSergey Zigachev 					    const struct ttm_place *place)
1488b843c749SSergey Zigachev {
1489b843c749SSergey Zigachev 	unsigned long num_pages = bo->mem.num_pages;
1490b843c749SSergey Zigachev 	struct drm_mm_node *node = bo->mem.mm_node;
1491b843c749SSergey Zigachev 	struct reservation_object_list *flist;
1492b843c749SSergey Zigachev 	struct dma_fence *f;
1493b843c749SSergey Zigachev 	int i;
1494b843c749SSergey Zigachev 
1495b843c749SSergey Zigachev 	/* If bo is a KFD BO, check if the bo belongs to the current process.
1496b843c749SSergey Zigachev 	 * If true, then return false as any KFD process needs all its BOs to
1497b843c749SSergey Zigachev 	 * be resident to run successfully
1498b843c749SSergey Zigachev 	 */
1499b843c749SSergey Zigachev 	flist = reservation_object_get_list(bo->resv);
1500b843c749SSergey Zigachev 	if (flist) {
1501b843c749SSergey Zigachev 		for (i = 0; i < flist->shared_count; ++i) {
1502b843c749SSergey Zigachev 			f = rcu_dereference_protected(flist->shared[i],
1503b843c749SSergey Zigachev 				reservation_object_held(bo->resv));
1504b843c749SSergey Zigachev 			if (amdkfd_fence_check_mm(f, current->mm))
1505b843c749SSergey Zigachev 				return false;
1506b843c749SSergey Zigachev 		}
1507b843c749SSergey Zigachev 	}
1508b843c749SSergey Zigachev 
1509b843c749SSergey Zigachev 	switch (bo->mem.mem_type) {
1510b843c749SSergey Zigachev 	case TTM_PL_TT:
1511b843c749SSergey Zigachev 		return true;
1512b843c749SSergey Zigachev 
1513b843c749SSergey Zigachev 	case TTM_PL_VRAM:
1514b843c749SSergey Zigachev 		/* Check each drm MM node individually */
1515b843c749SSergey Zigachev 		while (num_pages) {
1516b843c749SSergey Zigachev 			if (place->fpfn < (node->start + node->size) &&
1517b843c749SSergey Zigachev 			    !(place->lpfn && place->lpfn <= node->start))
1518b843c749SSergey Zigachev 				return true;
1519b843c749SSergey Zigachev 
1520b843c749SSergey Zigachev 			num_pages -= node->size;
1521b843c749SSergey Zigachev 			++node;
1522b843c749SSergey Zigachev 		}
1523b843c749SSergey Zigachev 		return false;
1524b843c749SSergey Zigachev 
1525b843c749SSergey Zigachev 	default:
1526b843c749SSergey Zigachev 		break;
1527b843c749SSergey Zigachev 	}
1528b843c749SSergey Zigachev 
1529b843c749SSergey Zigachev 	return ttm_bo_eviction_valuable(bo, place);
1530b843c749SSergey Zigachev }
1531b843c749SSergey Zigachev 
1532b843c749SSergey Zigachev /**
1533b843c749SSergey Zigachev  * amdgpu_ttm_access_memory - Read or Write memory that backs a buffer object.
1534b843c749SSergey Zigachev  *
1535b843c749SSergey Zigachev  * @bo:  The buffer object to read/write
1536b843c749SSergey Zigachev  * @offset:  Offset into buffer object
1537b843c749SSergey Zigachev  * @buf:  Secondary buffer to write/read from
1538b843c749SSergey Zigachev  * @len: Length in bytes of access
1539b843c749SSergey Zigachev  * @write:  true if writing
1540b843c749SSergey Zigachev  *
1541b843c749SSergey Zigachev  * This is used to access VRAM that backs a buffer object via MMIO
1542b843c749SSergey Zigachev  * access for debugging purposes.
1543b843c749SSergey Zigachev  */
amdgpu_ttm_access_memory(struct ttm_buffer_object * bo,unsigned long offset,void * buf,int len,int write)1544b843c749SSergey Zigachev static int amdgpu_ttm_access_memory(struct ttm_buffer_object *bo,
1545b843c749SSergey Zigachev 				    unsigned long offset,
1546b843c749SSergey Zigachev 				    void *buf, int len, int write)
1547b843c749SSergey Zigachev {
1548b843c749SSergey Zigachev 	struct amdgpu_bo *abo = ttm_to_amdgpu_bo(bo);
1549b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
1550b843c749SSergey Zigachev 	struct drm_mm_node *nodes;
1551b843c749SSergey Zigachev 	uint32_t value = 0;
1552b843c749SSergey Zigachev 	int ret = 0;
1553b843c749SSergey Zigachev 	uint64_t pos;
1554b843c749SSergey Zigachev 	unsigned long flags;
1555b843c749SSergey Zigachev 
1556b843c749SSergey Zigachev 	if (bo->mem.mem_type != TTM_PL_VRAM)
1557b843c749SSergey Zigachev 		return -EIO;
1558b843c749SSergey Zigachev 
1559b843c749SSergey Zigachev 	nodes = amdgpu_find_mm_node(&abo->tbo.mem, &offset);
1560b843c749SSergey Zigachev 	pos = (nodes->start << PAGE_SHIFT) + offset;
1561b843c749SSergey Zigachev 
1562b843c749SSergey Zigachev 	while (len && pos < adev->gmc.mc_vram_size) {
1563b843c749SSergey Zigachev 		uint64_t aligned_pos = pos & ~(uint64_t)3;
1564b843c749SSergey Zigachev 		uint32_t bytes = 4 - (pos & 3);
1565b843c749SSergey Zigachev 		uint32_t shift = (pos & 3) * 8;
1566b843c749SSergey Zigachev 		uint32_t mask = 0xffffffff << shift;
1567b843c749SSergey Zigachev 
1568b843c749SSergey Zigachev 		if (len < bytes) {
1569b843c749SSergey Zigachev 			mask &= 0xffffffff >> (bytes - len) * 8;
1570b843c749SSergey Zigachev 			bytes = len;
1571b843c749SSergey Zigachev 		}
1572b843c749SSergey Zigachev 
1573b843c749SSergey Zigachev 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
1574b843c749SSergey Zigachev 		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)aligned_pos) | 0x80000000);
1575b843c749SSergey Zigachev 		WREG32_NO_KIQ(mmMM_INDEX_HI, aligned_pos >> 31);
1576b843c749SSergey Zigachev 		if (!write || mask != 0xffffffff)
1577b843c749SSergey Zigachev 			value = RREG32_NO_KIQ(mmMM_DATA);
1578b843c749SSergey Zigachev 		if (write) {
1579b843c749SSergey Zigachev 			value &= ~mask;
1580b843c749SSergey Zigachev 			value |= (*(uint32_t *)buf << shift) & mask;
1581b843c749SSergey Zigachev 			WREG32_NO_KIQ(mmMM_DATA, value);
1582b843c749SSergey Zigachev 		}
1583b843c749SSergey Zigachev 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
1584b843c749SSergey Zigachev 		if (!write) {
1585b843c749SSergey Zigachev 			value = (value & mask) >> shift;
1586b843c749SSergey Zigachev 			memcpy(buf, &value, bytes);
1587b843c749SSergey Zigachev 		}
1588b843c749SSergey Zigachev 
1589b843c749SSergey Zigachev 		ret += bytes;
1590b843c749SSergey Zigachev 		buf = (uint8_t *)buf + bytes;
1591b843c749SSergey Zigachev 		pos += bytes;
1592b843c749SSergey Zigachev 		len -= bytes;
1593b843c749SSergey Zigachev 		if (pos >= (nodes->start + nodes->size) << PAGE_SHIFT) {
1594b843c749SSergey Zigachev 			++nodes;
1595b843c749SSergey Zigachev 			pos = (nodes->start << PAGE_SHIFT);
1596b843c749SSergey Zigachev 		}
1597b843c749SSergey Zigachev 	}
1598b843c749SSergey Zigachev 
1599b843c749SSergey Zigachev 	return ret;
1600b843c749SSergey Zigachev }
1601b843c749SSergey Zigachev 
1602b843c749SSergey Zigachev static struct ttm_bo_driver amdgpu_bo_driver = {
1603b843c749SSergey Zigachev 	.ttm_tt_create = &amdgpu_ttm_tt_create,
1604b843c749SSergey Zigachev 	.ttm_tt_populate = &amdgpu_ttm_tt_populate,
1605b843c749SSergey Zigachev 	.ttm_tt_unpopulate = &amdgpu_ttm_tt_unpopulate,
1606b843c749SSergey Zigachev 	.invalidate_caches = &amdgpu_invalidate_caches,
1607b843c749SSergey Zigachev 	.init_mem_type = &amdgpu_init_mem_type,
1608b843c749SSergey Zigachev 	.eviction_valuable = amdgpu_ttm_bo_eviction_valuable,
1609b843c749SSergey Zigachev 	.evict_flags = &amdgpu_evict_flags,
1610b843c749SSergey Zigachev 	.move = &amdgpu_bo_move,
1611b843c749SSergey Zigachev 	.verify_access = &amdgpu_verify_access,
1612b843c749SSergey Zigachev 	.move_notify = &amdgpu_bo_move_notify,
1613b843c749SSergey Zigachev 	.fault_reserve_notify = &amdgpu_bo_fault_reserve_notify,
1614b843c749SSergey Zigachev 	.io_mem_reserve = &amdgpu_ttm_io_mem_reserve,
1615b843c749SSergey Zigachev 	.io_mem_free = &amdgpu_ttm_io_mem_free,
1616b843c749SSergey Zigachev 	.io_mem_pfn = amdgpu_ttm_io_mem_pfn,
1617b843c749SSergey Zigachev 	.access_memory = &amdgpu_ttm_access_memory
1618b843c749SSergey Zigachev };
1619b843c749SSergey Zigachev 
1620b843c749SSergey Zigachev /*
1621b843c749SSergey Zigachev  * Firmware Reservation functions
1622b843c749SSergey Zigachev  */
1623b843c749SSergey Zigachev /**
1624b843c749SSergey Zigachev  * amdgpu_ttm_fw_reserve_vram_fini - free fw reserved vram
1625b843c749SSergey Zigachev  *
1626b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1627b843c749SSergey Zigachev  *
1628b843c749SSergey Zigachev  * free fw reserved vram if it has been reserved.
1629b843c749SSergey Zigachev  */
amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device * adev)1630b843c749SSergey Zigachev static void amdgpu_ttm_fw_reserve_vram_fini(struct amdgpu_device *adev)
1631b843c749SSergey Zigachev {
1632b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
1633b843c749SSergey Zigachev 		NULL, &adev->fw_vram_usage.va);
1634b843c749SSergey Zigachev }
1635b843c749SSergey Zigachev 
1636b843c749SSergey Zigachev /**
1637b843c749SSergey Zigachev  * amdgpu_ttm_fw_reserve_vram_init - create bo vram reservation from fw
1638b843c749SSergey Zigachev  *
1639b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1640b843c749SSergey Zigachev  *
1641b843c749SSergey Zigachev  * create bo vram reservation from fw.
1642b843c749SSergey Zigachev  */
amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device * adev)1643b843c749SSergey Zigachev static int amdgpu_ttm_fw_reserve_vram_init(struct amdgpu_device *adev)
1644b843c749SSergey Zigachev {
1645b843c749SSergey Zigachev 	struct ttm_operation_ctx ctx = { false, false };
1646b843c749SSergey Zigachev 	struct amdgpu_bo_param bp;
1647b843c749SSergey Zigachev 	int r = 0;
1648b843c749SSergey Zigachev 	int i;
1649b843c749SSergey Zigachev 	u64 vram_size = adev->gmc.visible_vram_size;
1650b843c749SSergey Zigachev 	u64 offset = adev->fw_vram_usage.start_offset;
1651b843c749SSergey Zigachev 	u64 size = adev->fw_vram_usage.size;
1652b843c749SSergey Zigachev 	struct amdgpu_bo *bo;
1653b843c749SSergey Zigachev 
1654b843c749SSergey Zigachev 	memset(&bp, 0, sizeof(bp));
1655b843c749SSergey Zigachev 	bp.size = adev->fw_vram_usage.size;
1656b843c749SSergey Zigachev 	bp.byte_align = PAGE_SIZE;
1657b843c749SSergey Zigachev 	bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
1658b843c749SSergey Zigachev 	bp.flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1659b843c749SSergey Zigachev 		AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1660b843c749SSergey Zigachev 	bp.type = ttm_bo_type_kernel;
1661b843c749SSergey Zigachev 	bp.resv = NULL;
1662b843c749SSergey Zigachev 	adev->fw_vram_usage.va = NULL;
1663b843c749SSergey Zigachev 	adev->fw_vram_usage.reserved_bo = NULL;
1664b843c749SSergey Zigachev 
1665b843c749SSergey Zigachev 	if (adev->fw_vram_usage.size > 0 &&
1666b843c749SSergey Zigachev 		adev->fw_vram_usage.size <= vram_size) {
1667b843c749SSergey Zigachev 
1668b843c749SSergey Zigachev 		r = amdgpu_bo_create(adev, &bp,
1669b843c749SSergey Zigachev 				     &adev->fw_vram_usage.reserved_bo);
1670b843c749SSergey Zigachev 		if (r)
1671b843c749SSergey Zigachev 			goto error_create;
1672b843c749SSergey Zigachev 
1673b843c749SSergey Zigachev 		r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
1674b843c749SSergey Zigachev 		if (r)
1675b843c749SSergey Zigachev 			goto error_reserve;
1676b843c749SSergey Zigachev 
1677b843c749SSergey Zigachev 		/* remove the original mem node and create a new one at the
1678b843c749SSergey Zigachev 		 * request position
1679b843c749SSergey Zigachev 		 */
1680b843c749SSergey Zigachev 		bo = adev->fw_vram_usage.reserved_bo;
1681b843c749SSergey Zigachev 		offset = ALIGN(offset, PAGE_SIZE);
1682b843c749SSergey Zigachev 		for (i = 0; i < bo->placement.num_placement; ++i) {
1683b843c749SSergey Zigachev 			bo->placements[i].fpfn = offset >> PAGE_SHIFT;
1684b843c749SSergey Zigachev 			bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
1685b843c749SSergey Zigachev 		}
1686b843c749SSergey Zigachev 
1687b843c749SSergey Zigachev 		ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
1688b843c749SSergey Zigachev 		r = ttm_bo_mem_space(&bo->tbo, &bo->placement,
1689b843c749SSergey Zigachev 				     &bo->tbo.mem, &ctx);
1690b843c749SSergey Zigachev 		if (r)
1691b843c749SSergey Zigachev 			goto error_pin;
1692b843c749SSergey Zigachev 
1693b843c749SSergey Zigachev 		r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
1694b843c749SSergey Zigachev 			AMDGPU_GEM_DOMAIN_VRAM,
1695b843c749SSergey Zigachev 			adev->fw_vram_usage.start_offset,
1696b843c749SSergey Zigachev 			(adev->fw_vram_usage.start_offset +
1697b843c749SSergey Zigachev 			adev->fw_vram_usage.size));
1698b843c749SSergey Zigachev 		if (r)
1699b843c749SSergey Zigachev 			goto error_pin;
1700b843c749SSergey Zigachev 		r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
1701b843c749SSergey Zigachev 			&adev->fw_vram_usage.va);
1702b843c749SSergey Zigachev 		if (r)
1703b843c749SSergey Zigachev 			goto error_kmap;
1704b843c749SSergey Zigachev 
1705b843c749SSergey Zigachev 		amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
1706b843c749SSergey Zigachev 	}
1707b843c749SSergey Zigachev 	return r;
1708b843c749SSergey Zigachev 
1709b843c749SSergey Zigachev error_kmap:
1710b843c749SSergey Zigachev 	amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
1711b843c749SSergey Zigachev error_pin:
1712b843c749SSergey Zigachev 	amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
1713b843c749SSergey Zigachev error_reserve:
1714b843c749SSergey Zigachev 	amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
1715b843c749SSergey Zigachev error_create:
1716b843c749SSergey Zigachev 	adev->fw_vram_usage.va = NULL;
1717b843c749SSergey Zigachev 	adev->fw_vram_usage.reserved_bo = NULL;
1718b843c749SSergey Zigachev 	return r;
1719b843c749SSergey Zigachev }
1720b843c749SSergey Zigachev /**
1721b843c749SSergey Zigachev  * amdgpu_ttm_init - Init the memory management (ttm) as well as various
1722b843c749SSergey Zigachev  * gtt/vram related fields.
1723b843c749SSergey Zigachev  *
1724b843c749SSergey Zigachev  * This initializes all of the memory space pools that the TTM layer
1725b843c749SSergey Zigachev  * will need such as the GTT space (system memory mapped to the device),
1726b843c749SSergey Zigachev  * VRAM (on-board memory), and on-chip memories (GDS, GWS, OA) which
1727b843c749SSergey Zigachev  * can be mapped per VMID.
1728b843c749SSergey Zigachev  */
amdgpu_ttm_init(struct amdgpu_device * adev)1729b843c749SSergey Zigachev int amdgpu_ttm_init(struct amdgpu_device *adev)
1730b843c749SSergey Zigachev {
1731b843c749SSergey Zigachev 	uint64_t gtt_size;
1732b843c749SSergey Zigachev 	int r;
1733b843c749SSergey Zigachev 	u64 vis_vram_limit;
1734b843c749SSergey Zigachev 
1735b843c749SSergey Zigachev 	/* initialize global references for vram/gtt */
1736b843c749SSergey Zigachev 	r = amdgpu_ttm_global_init(adev);
1737b843c749SSergey Zigachev 	if (r) {
1738b843c749SSergey Zigachev 		return r;
1739b843c749SSergey Zigachev 	}
1740b843c749SSergey Zigachev 	/* No others user of address space so set it to 0 */
1741b843c749SSergey Zigachev 	r = ttm_bo_device_init(&adev->mman.bdev,
1742b843c749SSergey Zigachev 			       adev->mman.bo_global_ref.ref.object,
1743b843c749SSergey Zigachev 			       &amdgpu_bo_driver,
1744*78973132SSergey Zigachev #if 0
1745b843c749SSergey Zigachev 			       adev->ddev->anon_inode->i_mapping,
1746*78973132SSergey Zigachev #endif
1747*78973132SSergey Zigachev 			       NULL,
1748b843c749SSergey Zigachev 			       DRM_FILE_PAGE_OFFSET,
1749b843c749SSergey Zigachev 			       adev->need_dma32);
1750b843c749SSergey Zigachev 	if (r) {
1751b843c749SSergey Zigachev 		DRM_ERROR("failed initializing buffer object driver(%d).\n", r);
1752b843c749SSergey Zigachev 		return r;
1753b843c749SSergey Zigachev 	}
1754*78973132SSergey Zigachev 	adev->ddev->drm_ttm_bdev = &adev->mman.bdev;
1755b843c749SSergey Zigachev 	adev->mman.initialized = true;
1756b843c749SSergey Zigachev 
1757b843c749SSergey Zigachev 	/* We opt to avoid OOM on system pages allocations */
1758b843c749SSergey Zigachev 	adev->mman.bdev.no_retry = true;
1759b843c749SSergey Zigachev 
1760b843c749SSergey Zigachev 	/* Initialize VRAM pool with all of VRAM divided into pages */
1761b843c749SSergey Zigachev 	r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_VRAM,
1762b843c749SSergey Zigachev 				adev->gmc.real_vram_size >> PAGE_SHIFT);
1763b843c749SSergey Zigachev 	if (r) {
1764b843c749SSergey Zigachev 		DRM_ERROR("Failed initializing VRAM heap.\n");
1765b843c749SSergey Zigachev 		return r;
1766b843c749SSergey Zigachev 	}
1767b843c749SSergey Zigachev 
1768b843c749SSergey Zigachev 	/* Reduce size of CPU-visible VRAM if requested */
1769b843c749SSergey Zigachev 	vis_vram_limit = (u64)amdgpu_vis_vram_limit * 1024 * 1024;
1770b843c749SSergey Zigachev 	if (amdgpu_vis_vram_limit > 0 &&
1771b843c749SSergey Zigachev 	    vis_vram_limit <= adev->gmc.visible_vram_size)
1772b843c749SSergey Zigachev 		adev->gmc.visible_vram_size = vis_vram_limit;
1773b843c749SSergey Zigachev 
1774b843c749SSergey Zigachev 	/* Change the size here instead of the init above so only lpfn is affected */
1775b843c749SSergey Zigachev 	amdgpu_ttm_set_buffer_funcs_status(adev, false);
1776b843c749SSergey Zigachev #ifdef CONFIG_64BIT
1777b843c749SSergey Zigachev 	adev->mman.aper_base_kaddr = ioremap_wc(adev->gmc.aper_base,
1778b843c749SSergey Zigachev 						adev->gmc.visible_vram_size);
1779b843c749SSergey Zigachev #endif
1780b843c749SSergey Zigachev 
1781b843c749SSergey Zigachev 	/*
1782b843c749SSergey Zigachev 	 *The reserved vram for firmware must be pinned to the specified
1783b843c749SSergey Zigachev 	 *place on the VRAM, so reserve it early.
1784b843c749SSergey Zigachev 	 */
1785b843c749SSergey Zigachev 	r = amdgpu_ttm_fw_reserve_vram_init(adev);
1786b843c749SSergey Zigachev 	if (r) {
1787b843c749SSergey Zigachev 		return r;
1788b843c749SSergey Zigachev 	}
1789b843c749SSergey Zigachev 
1790b843c749SSergey Zigachev 	/* allocate memory as required for VGA
1791b843c749SSergey Zigachev 	 * This is used for VGA emulation and pre-OS scanout buffers to
1792b843c749SSergey Zigachev 	 * avoid display artifacts while transitioning between pre-OS
1793b843c749SSergey Zigachev 	 * and driver.  */
1794b843c749SSergey Zigachev 	if (adev->gmc.stolen_size) {
1795b843c749SSergey Zigachev 		r = amdgpu_bo_create_kernel(adev, adev->gmc.stolen_size, PAGE_SIZE,
1796b843c749SSergey Zigachev 					    AMDGPU_GEM_DOMAIN_VRAM,
1797b843c749SSergey Zigachev 					    &adev->stolen_vga_memory,
1798b843c749SSergey Zigachev 					    NULL, NULL);
1799b843c749SSergey Zigachev 		if (r)
1800b843c749SSergey Zigachev 			return r;
1801b843c749SSergey Zigachev 	}
1802b843c749SSergey Zigachev 	DRM_INFO("amdgpu: %uM of VRAM memory ready\n",
1803b843c749SSergey Zigachev 		 (unsigned) (adev->gmc.real_vram_size / (1024 * 1024)));
1804b843c749SSergey Zigachev 
1805b843c749SSergey Zigachev 	/* Compute GTT size, either bsaed on 3/4th the size of RAM size
1806b843c749SSergey Zigachev 	 * or whatever the user passed on module init */
1807b843c749SSergey Zigachev 	if (amdgpu_gtt_size == -1) {
1808b843c749SSergey Zigachev 		struct sysinfo si;
1809b843c749SSergey Zigachev 
1810b843c749SSergey Zigachev 		si_meminfo(&si);
1811b843c749SSergey Zigachev 		gtt_size = min(max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
1812b843c749SSergey Zigachev 			       adev->gmc.mc_vram_size),
1813b843c749SSergey Zigachev 			       ((uint64_t)si.totalram * si.mem_unit * 3/4));
1814b843c749SSergey Zigachev 	}
1815b843c749SSergey Zigachev 	else
1816b843c749SSergey Zigachev 		gtt_size = (uint64_t)amdgpu_gtt_size << 20;
1817b843c749SSergey Zigachev 
1818b843c749SSergey Zigachev 	/* Initialize GTT memory pool */
1819b843c749SSergey Zigachev 	r = ttm_bo_init_mm(&adev->mman.bdev, TTM_PL_TT, gtt_size >> PAGE_SHIFT);
1820b843c749SSergey Zigachev 	if (r) {
1821b843c749SSergey Zigachev 		DRM_ERROR("Failed initializing GTT heap.\n");
1822b843c749SSergey Zigachev 		return r;
1823b843c749SSergey Zigachev 	}
1824b843c749SSergey Zigachev 	DRM_INFO("amdgpu: %uM of GTT memory ready.\n",
1825b843c749SSergey Zigachev 		 (unsigned)(gtt_size / (1024 * 1024)));
1826b843c749SSergey Zigachev 
1827b843c749SSergey Zigachev 	/* Initialize various on-chip memory pools */
1828b843c749SSergey Zigachev 	adev->gds.mem.total_size = adev->gds.mem.total_size << AMDGPU_GDS_SHIFT;
1829b843c749SSergey Zigachev 	adev->gds.mem.gfx_partition_size = adev->gds.mem.gfx_partition_size << AMDGPU_GDS_SHIFT;
1830b843c749SSergey Zigachev 	adev->gds.mem.cs_partition_size = adev->gds.mem.cs_partition_size << AMDGPU_GDS_SHIFT;
1831b843c749SSergey Zigachev 	adev->gds.gws.total_size = adev->gds.gws.total_size << AMDGPU_GWS_SHIFT;
1832b843c749SSergey Zigachev 	adev->gds.gws.gfx_partition_size = adev->gds.gws.gfx_partition_size << AMDGPU_GWS_SHIFT;
1833b843c749SSergey Zigachev 	adev->gds.gws.cs_partition_size = adev->gds.gws.cs_partition_size << AMDGPU_GWS_SHIFT;
1834b843c749SSergey Zigachev 	adev->gds.oa.total_size = adev->gds.oa.total_size << AMDGPU_OA_SHIFT;
1835b843c749SSergey Zigachev 	adev->gds.oa.gfx_partition_size = adev->gds.oa.gfx_partition_size << AMDGPU_OA_SHIFT;
1836b843c749SSergey Zigachev 	adev->gds.oa.cs_partition_size = adev->gds.oa.cs_partition_size << AMDGPU_OA_SHIFT;
1837b843c749SSergey Zigachev 	/* GDS Memory */
1838b843c749SSergey Zigachev 	if (adev->gds.mem.total_size) {
1839b843c749SSergey Zigachev 		r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GDS,
1840b843c749SSergey Zigachev 				   adev->gds.mem.total_size >> PAGE_SHIFT);
1841b843c749SSergey Zigachev 		if (r) {
1842b843c749SSergey Zigachev 			DRM_ERROR("Failed initializing GDS heap.\n");
1843b843c749SSergey Zigachev 			return r;
1844b843c749SSergey Zigachev 		}
1845b843c749SSergey Zigachev 	}
1846b843c749SSergey Zigachev 
1847b843c749SSergey Zigachev 	/* GWS */
1848b843c749SSergey Zigachev 	if (adev->gds.gws.total_size) {
1849b843c749SSergey Zigachev 		r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_GWS,
1850b843c749SSergey Zigachev 				   adev->gds.gws.total_size >> PAGE_SHIFT);
1851b843c749SSergey Zigachev 		if (r) {
1852b843c749SSergey Zigachev 			DRM_ERROR("Failed initializing gws heap.\n");
1853b843c749SSergey Zigachev 			return r;
1854b843c749SSergey Zigachev 		}
1855b843c749SSergey Zigachev 	}
1856b843c749SSergey Zigachev 
1857b843c749SSergey Zigachev 	/* OA */
1858b843c749SSergey Zigachev 	if (adev->gds.oa.total_size) {
1859b843c749SSergey Zigachev 		r = ttm_bo_init_mm(&adev->mman.bdev, AMDGPU_PL_OA,
1860b843c749SSergey Zigachev 				   adev->gds.oa.total_size >> PAGE_SHIFT);
1861b843c749SSergey Zigachev 		if (r) {
1862b843c749SSergey Zigachev 			DRM_ERROR("Failed initializing oa heap.\n");
1863b843c749SSergey Zigachev 			return r;
1864b843c749SSergey Zigachev 		}
1865b843c749SSergey Zigachev 	}
1866b843c749SSergey Zigachev 
1867b843c749SSergey Zigachev 	/* Register debugfs entries for amdgpu_ttm */
1868b843c749SSergey Zigachev 	r = amdgpu_ttm_debugfs_init(adev);
1869b843c749SSergey Zigachev 	if (r) {
1870b843c749SSergey Zigachev 		DRM_ERROR("Failed to init debugfs\n");
1871b843c749SSergey Zigachev 		return r;
1872b843c749SSergey Zigachev 	}
1873b843c749SSergey Zigachev 	return 0;
1874b843c749SSergey Zigachev }
1875b843c749SSergey Zigachev 
1876b843c749SSergey Zigachev /**
1877b843c749SSergey Zigachev  * amdgpu_ttm_late_init - Handle any late initialization for amdgpu_ttm
1878b843c749SSergey Zigachev  */
amdgpu_ttm_late_init(struct amdgpu_device * adev)1879b843c749SSergey Zigachev void amdgpu_ttm_late_init(struct amdgpu_device *adev)
1880b843c749SSergey Zigachev {
1881b843c749SSergey Zigachev 	/* return the VGA stolen memory (if any) back to VRAM */
1882b843c749SSergey Zigachev 	amdgpu_bo_free_kernel(&adev->stolen_vga_memory, NULL, NULL);
1883b843c749SSergey Zigachev }
1884b843c749SSergey Zigachev 
1885b843c749SSergey Zigachev /**
1886b843c749SSergey Zigachev  * amdgpu_ttm_fini - De-initialize the TTM memory pools
1887b843c749SSergey Zigachev  */
amdgpu_ttm_fini(struct amdgpu_device * adev)1888b843c749SSergey Zigachev void amdgpu_ttm_fini(struct amdgpu_device *adev)
1889b843c749SSergey Zigachev {
1890b843c749SSergey Zigachev 	if (!adev->mman.initialized)
1891b843c749SSergey Zigachev 		return;
1892b843c749SSergey Zigachev 
1893b843c749SSergey Zigachev 	amdgpu_ttm_debugfs_fini(adev);
1894b843c749SSergey Zigachev 	amdgpu_ttm_fw_reserve_vram_fini(adev);
1895b843c749SSergey Zigachev 	if (adev->mman.aper_base_kaddr)
1896b843c749SSergey Zigachev 		iounmap(adev->mman.aper_base_kaddr);
1897b843c749SSergey Zigachev 	adev->mman.aper_base_kaddr = NULL;
1898b843c749SSergey Zigachev 
1899b843c749SSergey Zigachev 	ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_VRAM);
1900b843c749SSergey Zigachev 	ttm_bo_clean_mm(&adev->mman.bdev, TTM_PL_TT);
1901b843c749SSergey Zigachev 	if (adev->gds.mem.total_size)
1902b843c749SSergey Zigachev 		ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GDS);
1903b843c749SSergey Zigachev 	if (adev->gds.gws.total_size)
1904b843c749SSergey Zigachev 		ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_GWS);
1905b843c749SSergey Zigachev 	if (adev->gds.oa.total_size)
1906b843c749SSergey Zigachev 		ttm_bo_clean_mm(&adev->mman.bdev, AMDGPU_PL_OA);
1907b843c749SSergey Zigachev 	ttm_bo_device_release(&adev->mman.bdev);
1908b843c749SSergey Zigachev 	amdgpu_ttm_global_fini(adev);
1909b843c749SSergey Zigachev 	adev->mman.initialized = false;
1910b843c749SSergey Zigachev 	DRM_INFO("amdgpu: ttm finalized\n");
1911b843c749SSergey Zigachev }
1912b843c749SSergey Zigachev 
1913b843c749SSergey Zigachev /**
1914b843c749SSergey Zigachev  * amdgpu_ttm_set_buffer_funcs_status - enable/disable use of buffer functions
1915b843c749SSergey Zigachev  *
1916b843c749SSergey Zigachev  * @adev: amdgpu_device pointer
1917b843c749SSergey Zigachev  * @enable: true when we can use buffer functions.
1918b843c749SSergey Zigachev  *
1919b843c749SSergey Zigachev  * Enable/disable use of buffer functions during suspend/resume. This should
1920b843c749SSergey Zigachev  * only be called at bootup or when userspace isn't running.
1921b843c749SSergey Zigachev  */
amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device * adev,bool enable)1922b843c749SSergey Zigachev void amdgpu_ttm_set_buffer_funcs_status(struct amdgpu_device *adev, bool enable)
1923b843c749SSergey Zigachev {
1924b843c749SSergey Zigachev 	struct ttm_mem_type_manager *man = &adev->mman.bdev.man[TTM_PL_VRAM];
1925b843c749SSergey Zigachev 	uint64_t size;
1926b843c749SSergey Zigachev 	int r;
1927b843c749SSergey Zigachev 
1928b843c749SSergey Zigachev 	if (!adev->mman.initialized || adev->in_gpu_reset ||
1929b843c749SSergey Zigachev 	    adev->mman.buffer_funcs_enabled == enable)
1930b843c749SSergey Zigachev 		return;
1931b843c749SSergey Zigachev 
1932b843c749SSergey Zigachev 	if (enable) {
1933b843c749SSergey Zigachev 		struct amdgpu_ring *ring;
1934b843c749SSergey Zigachev 		struct drm_sched_rq *rq;
1935b843c749SSergey Zigachev 
1936b843c749SSergey Zigachev 		ring = adev->mman.buffer_funcs_ring;
1937b843c749SSergey Zigachev 		rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
1938b843c749SSergey Zigachev 		r = drm_sched_entity_init(&adev->mman.entity, &rq, 1, NULL);
1939b843c749SSergey Zigachev 		if (r) {
1940b843c749SSergey Zigachev 			DRM_ERROR("Failed setting up TTM BO move entity (%d)\n",
1941b843c749SSergey Zigachev 				  r);
1942b843c749SSergey Zigachev 			return;
1943b843c749SSergey Zigachev 		}
1944b843c749SSergey Zigachev 	} else {
1945b843c749SSergey Zigachev 		drm_sched_entity_destroy(&adev->mman.entity);
1946b843c749SSergey Zigachev 		dma_fence_put(man->move);
1947b843c749SSergey Zigachev 		man->move = NULL;
1948b843c749SSergey Zigachev 	}
1949b843c749SSergey Zigachev 
1950b843c749SSergey Zigachev 	/* this just adjusts TTM size idea, which sets lpfn to the correct value */
1951b843c749SSergey Zigachev 	if (enable)
1952b843c749SSergey Zigachev 		size = adev->gmc.real_vram_size;
1953b843c749SSergey Zigachev 	else
1954b843c749SSergey Zigachev 		size = adev->gmc.visible_vram_size;
1955b843c749SSergey Zigachev 	man->size = size >> PAGE_SHIFT;
1956b843c749SSergey Zigachev 	adev->mman.buffer_funcs_enabled = enable;
1957b843c749SSergey Zigachev }
1958b843c749SSergey Zigachev 
amdgpu_mmap(struct file * filp,struct vm_area_struct * vma)1959b843c749SSergey Zigachev int amdgpu_mmap(struct file *filp, struct vm_area_struct *vma)
1960b843c749SSergey Zigachev {
1961b843c749SSergey Zigachev 	struct drm_file *file_priv;
1962b843c749SSergey Zigachev 	struct amdgpu_device *adev;
1963b843c749SSergey Zigachev 
1964b843c749SSergey Zigachev 	if (unlikely(vma->vm_pgoff < DRM_FILE_PAGE_OFFSET))
1965b843c749SSergey Zigachev 		return -EINVAL;
1966b843c749SSergey Zigachev 
1967b843c749SSergey Zigachev 	file_priv = filp->private_data;
1968b843c749SSergey Zigachev 	adev = file_priv->minor->dev->dev_private;
1969b843c749SSergey Zigachev 	if (adev == NULL)
1970b843c749SSergey Zigachev 		return -EINVAL;
1971b843c749SSergey Zigachev 
1972b843c749SSergey Zigachev 	return ttm_bo_mmap(filp, vma, &adev->mman.bdev);
1973b843c749SSergey Zigachev }
1974b843c749SSergey Zigachev 
amdgpu_map_buffer(struct ttm_buffer_object * bo,struct ttm_mem_reg * mem,unsigned num_pages,uint64_t offset,unsigned window,struct amdgpu_ring * ring,uint64_t * addr)1975b843c749SSergey Zigachev static int amdgpu_map_buffer(struct ttm_buffer_object *bo,
1976b843c749SSergey Zigachev 			     struct ttm_mem_reg *mem, unsigned num_pages,
1977b843c749SSergey Zigachev 			     uint64_t offset, unsigned window,
1978b843c749SSergey Zigachev 			     struct amdgpu_ring *ring,
1979b843c749SSergey Zigachev 			     uint64_t *addr)
1980b843c749SSergey Zigachev {
1981b843c749SSergey Zigachev 	struct amdgpu_ttm_tt *gtt = (void *)bo->ttm;
1982b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
1983b843c749SSergey Zigachev 	struct ttm_tt *ttm = bo->ttm;
1984b843c749SSergey Zigachev 	struct amdgpu_job *job;
1985b843c749SSergey Zigachev 	unsigned num_dw, num_bytes;
1986b843c749SSergey Zigachev 	dma_addr_t *dma_address;
1987b843c749SSergey Zigachev 	struct dma_fence *fence;
1988b843c749SSergey Zigachev 	uint64_t src_addr, dst_addr;
1989b843c749SSergey Zigachev 	uint64_t flags;
1990b843c749SSergey Zigachev 	int r;
1991b843c749SSergey Zigachev 
1992b843c749SSergey Zigachev 	BUG_ON(adev->mman.buffer_funcs->copy_max_bytes <
1993b843c749SSergey Zigachev 	       AMDGPU_GTT_MAX_TRANSFER_SIZE * 8);
1994b843c749SSergey Zigachev 
1995b843c749SSergey Zigachev 	*addr = adev->gmc.gart_start;
1996b843c749SSergey Zigachev 	*addr += (u64)window * AMDGPU_GTT_MAX_TRANSFER_SIZE *
1997b843c749SSergey Zigachev 		AMDGPU_GPU_PAGE_SIZE;
1998b843c749SSergey Zigachev 
1999b843c749SSergey Zigachev 	num_dw = adev->mman.buffer_funcs->copy_num_dw;
2000b843c749SSergey Zigachev 	while (num_dw & 0x7)
2001b843c749SSergey Zigachev 		num_dw++;
2002b843c749SSergey Zigachev 
2003b843c749SSergey Zigachev 	num_bytes = num_pages * 8;
2004b843c749SSergey Zigachev 
2005b843c749SSergey Zigachev 	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4 + num_bytes, &job);
2006b843c749SSergey Zigachev 	if (r)
2007b843c749SSergey Zigachev 		return r;
2008b843c749SSergey Zigachev 
2009b843c749SSergey Zigachev 	src_addr = num_dw * 4;
2010b843c749SSergey Zigachev 	src_addr += job->ibs[0].gpu_addr;
2011b843c749SSergey Zigachev 
2012b843c749SSergey Zigachev 	dst_addr = adev->gart.table_addr;
2013b843c749SSergey Zigachev 	dst_addr += window * AMDGPU_GTT_MAX_TRANSFER_SIZE * 8;
2014b843c749SSergey Zigachev 	amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_addr,
2015b843c749SSergey Zigachev 				dst_addr, num_bytes);
2016b843c749SSergey Zigachev 
2017b843c749SSergey Zigachev 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2018b843c749SSergey Zigachev 	WARN_ON(job->ibs[0].length_dw > num_dw);
2019b843c749SSergey Zigachev 
2020b843c749SSergey Zigachev 	dma_address = &gtt->ttm.dma_address[offset >> PAGE_SHIFT];
2021b843c749SSergey Zigachev 	flags = amdgpu_ttm_tt_pte_flags(adev, ttm, mem);
2022b843c749SSergey Zigachev 	r = amdgpu_gart_map(adev, 0, num_pages, dma_address, flags,
2023b843c749SSergey Zigachev 			    &job->ibs[0].ptr[num_dw]);
2024b843c749SSergey Zigachev 	if (r)
2025b843c749SSergey Zigachev 		goto error_free;
2026b843c749SSergey Zigachev 
2027b843c749SSergey Zigachev 	r = amdgpu_job_submit(job, &adev->mman.entity,
2028b843c749SSergey Zigachev 			      AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
2029b843c749SSergey Zigachev 	if (r)
2030b843c749SSergey Zigachev 		goto error_free;
2031b843c749SSergey Zigachev 
2032b843c749SSergey Zigachev 	dma_fence_put(fence);
2033b843c749SSergey Zigachev 
2034b843c749SSergey Zigachev 	return r;
2035b843c749SSergey Zigachev 
2036b843c749SSergey Zigachev error_free:
2037b843c749SSergey Zigachev 	amdgpu_job_free(job);
2038b843c749SSergey Zigachev 	return r;
2039b843c749SSergey Zigachev }
2040b843c749SSergey Zigachev 
amdgpu_copy_buffer(struct amdgpu_ring * ring,uint64_t src_offset,uint64_t dst_offset,uint32_t byte_count,struct reservation_object * resv,struct dma_fence ** fence,bool direct_submit,bool vm_needs_flush)2041b843c749SSergey Zigachev int amdgpu_copy_buffer(struct amdgpu_ring *ring, uint64_t src_offset,
2042b843c749SSergey Zigachev 		       uint64_t dst_offset, uint32_t byte_count,
2043b843c749SSergey Zigachev 		       struct reservation_object *resv,
2044b843c749SSergey Zigachev 		       struct dma_fence **fence, bool direct_submit,
2045b843c749SSergey Zigachev 		       bool vm_needs_flush)
2046b843c749SSergey Zigachev {
2047b843c749SSergey Zigachev 	struct amdgpu_device *adev = ring->adev;
2048b843c749SSergey Zigachev 	struct amdgpu_job *job;
2049b843c749SSergey Zigachev 
2050b843c749SSergey Zigachev 	uint32_t max_bytes;
2051b843c749SSergey Zigachev 	unsigned num_loops, num_dw;
2052b843c749SSergey Zigachev 	unsigned i;
2053b843c749SSergey Zigachev 	int r;
2054b843c749SSergey Zigachev 
2055b843c749SSergey Zigachev 	if (direct_submit && !ring->ready) {
2056b843c749SSergey Zigachev 		DRM_ERROR("Trying to move memory with ring turned off.\n");
2057b843c749SSergey Zigachev 		return -EINVAL;
2058b843c749SSergey Zigachev 	}
2059b843c749SSergey Zigachev 
2060b843c749SSergey Zigachev 	max_bytes = adev->mman.buffer_funcs->copy_max_bytes;
2061b843c749SSergey Zigachev 	num_loops = DIV_ROUND_UP(byte_count, max_bytes);
2062b843c749SSergey Zigachev 	num_dw = num_loops * adev->mman.buffer_funcs->copy_num_dw;
2063b843c749SSergey Zigachev 
2064b843c749SSergey Zigachev 	/* for IB padding */
2065b843c749SSergey Zigachev 	while (num_dw & 0x7)
2066b843c749SSergey Zigachev 		num_dw++;
2067b843c749SSergey Zigachev 
2068b843c749SSergey Zigachev 	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
2069b843c749SSergey Zigachev 	if (r)
2070b843c749SSergey Zigachev 		return r;
2071b843c749SSergey Zigachev 
2072b843c749SSergey Zigachev 	job->vm_needs_flush = vm_needs_flush;
2073b843c749SSergey Zigachev 	if (resv) {
2074b843c749SSergey Zigachev 		r = amdgpu_sync_resv(adev, &job->sync, resv,
2075b843c749SSergey Zigachev 				     AMDGPU_FENCE_OWNER_UNDEFINED,
2076b843c749SSergey Zigachev 				     false);
2077b843c749SSergey Zigachev 		if (r) {
2078b843c749SSergey Zigachev 			DRM_ERROR("sync failed (%d).\n", r);
2079b843c749SSergey Zigachev 			goto error_free;
2080b843c749SSergey Zigachev 		}
2081b843c749SSergey Zigachev 	}
2082b843c749SSergey Zigachev 
2083b843c749SSergey Zigachev 	for (i = 0; i < num_loops; i++) {
2084b843c749SSergey Zigachev 		uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2085b843c749SSergey Zigachev 
2086b843c749SSergey Zigachev 		amdgpu_emit_copy_buffer(adev, &job->ibs[0], src_offset,
2087b843c749SSergey Zigachev 					dst_offset, cur_size_in_bytes);
2088b843c749SSergey Zigachev 
2089b843c749SSergey Zigachev 		src_offset += cur_size_in_bytes;
2090b843c749SSergey Zigachev 		dst_offset += cur_size_in_bytes;
2091b843c749SSergey Zigachev 		byte_count -= cur_size_in_bytes;
2092b843c749SSergey Zigachev 	}
2093b843c749SSergey Zigachev 
2094b843c749SSergey Zigachev 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2095b843c749SSergey Zigachev 	WARN_ON(job->ibs[0].length_dw > num_dw);
2096b843c749SSergey Zigachev 	if (direct_submit)
2097b843c749SSergey Zigachev 		r = amdgpu_job_submit_direct(job, ring, fence);
2098b843c749SSergey Zigachev 	else
2099b843c749SSergey Zigachev 		r = amdgpu_job_submit(job, &adev->mman.entity,
2100b843c749SSergey Zigachev 				      AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2101b843c749SSergey Zigachev 	if (r)
2102b843c749SSergey Zigachev 		goto error_free;
2103b843c749SSergey Zigachev 
2104b843c749SSergey Zigachev 	return r;
2105b843c749SSergey Zigachev 
2106b843c749SSergey Zigachev error_free:
2107b843c749SSergey Zigachev 	amdgpu_job_free(job);
2108b843c749SSergey Zigachev 	DRM_ERROR("Error scheduling IBs (%d)\n", r);
2109b843c749SSergey Zigachev 	return r;
2110b843c749SSergey Zigachev }
2111b843c749SSergey Zigachev 
amdgpu_fill_buffer(struct amdgpu_bo * bo,uint32_t src_data,struct reservation_object * resv,struct dma_fence ** fence)2112b843c749SSergey Zigachev int amdgpu_fill_buffer(struct amdgpu_bo *bo,
2113b843c749SSergey Zigachev 		       uint32_t src_data,
2114b843c749SSergey Zigachev 		       struct reservation_object *resv,
2115b843c749SSergey Zigachev 		       struct dma_fence **fence)
2116b843c749SSergey Zigachev {
2117b843c749SSergey Zigachev 	struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
2118b843c749SSergey Zigachev 	uint32_t max_bytes = adev->mman.buffer_funcs->fill_max_bytes;
2119b843c749SSergey Zigachev 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2120b843c749SSergey Zigachev 
2121b843c749SSergey Zigachev 	struct drm_mm_node *mm_node;
2122b843c749SSergey Zigachev 	unsigned long num_pages;
2123b843c749SSergey Zigachev 	unsigned int num_loops, num_dw;
2124b843c749SSergey Zigachev 
2125b843c749SSergey Zigachev 	struct amdgpu_job *job;
2126b843c749SSergey Zigachev 	int r;
2127b843c749SSergey Zigachev 
2128b843c749SSergey Zigachev 	if (!adev->mman.buffer_funcs_enabled) {
2129b843c749SSergey Zigachev 		DRM_ERROR("Trying to clear memory with ring turned off.\n");
2130b843c749SSergey Zigachev 		return -EINVAL;
2131b843c749SSergey Zigachev 	}
2132b843c749SSergey Zigachev 
2133b843c749SSergey Zigachev 	if (bo->tbo.mem.mem_type == TTM_PL_TT) {
2134b843c749SSergey Zigachev 		r = amdgpu_ttm_alloc_gart(&bo->tbo);
2135b843c749SSergey Zigachev 		if (r)
2136b843c749SSergey Zigachev 			return r;
2137b843c749SSergey Zigachev 	}
2138b843c749SSergey Zigachev 
2139b843c749SSergey Zigachev 	num_pages = bo->tbo.num_pages;
2140b843c749SSergey Zigachev 	mm_node = bo->tbo.mem.mm_node;
2141b843c749SSergey Zigachev 	num_loops = 0;
2142b843c749SSergey Zigachev 	while (num_pages) {
2143b843c749SSergey Zigachev 		uint32_t byte_count = mm_node->size << PAGE_SHIFT;
2144b843c749SSergey Zigachev 
2145b843c749SSergey Zigachev 		num_loops += DIV_ROUND_UP(byte_count, max_bytes);
2146b843c749SSergey Zigachev 		num_pages -= mm_node->size;
2147b843c749SSergey Zigachev 		++mm_node;
2148b843c749SSergey Zigachev 	}
2149b843c749SSergey Zigachev 	num_dw = num_loops * adev->mman.buffer_funcs->fill_num_dw;
2150b843c749SSergey Zigachev 
2151b843c749SSergey Zigachev 	/* for IB padding */
2152b843c749SSergey Zigachev 	num_dw += 64;
2153b843c749SSergey Zigachev 
2154b843c749SSergey Zigachev 	r = amdgpu_job_alloc_with_ib(adev, num_dw * 4, &job);
2155b843c749SSergey Zigachev 	if (r)
2156b843c749SSergey Zigachev 		return r;
2157b843c749SSergey Zigachev 
2158b843c749SSergey Zigachev 	if (resv) {
2159b843c749SSergey Zigachev 		r = amdgpu_sync_resv(adev, &job->sync, resv,
2160b843c749SSergey Zigachev 				     AMDGPU_FENCE_OWNER_UNDEFINED, false);
2161b843c749SSergey Zigachev 		if (r) {
2162b843c749SSergey Zigachev 			DRM_ERROR("sync failed (%d).\n", r);
2163b843c749SSergey Zigachev 			goto error_free;
2164b843c749SSergey Zigachev 		}
2165b843c749SSergey Zigachev 	}
2166b843c749SSergey Zigachev 
2167b843c749SSergey Zigachev 	num_pages = bo->tbo.num_pages;
2168b843c749SSergey Zigachev 	mm_node = bo->tbo.mem.mm_node;
2169b843c749SSergey Zigachev 
2170b843c749SSergey Zigachev 	while (num_pages) {
2171b843c749SSergey Zigachev 		uint32_t byte_count = mm_node->size << PAGE_SHIFT;
2172b843c749SSergey Zigachev 		uint64_t dst_addr;
2173b843c749SSergey Zigachev 
2174b843c749SSergey Zigachev 		dst_addr = amdgpu_mm_node_addr(&bo->tbo, mm_node, &bo->tbo.mem);
2175b843c749SSergey Zigachev 		while (byte_count) {
2176b843c749SSergey Zigachev 			uint32_t cur_size_in_bytes = min(byte_count, max_bytes);
2177b843c749SSergey Zigachev 
2178b843c749SSergey Zigachev 			amdgpu_emit_fill_buffer(adev, &job->ibs[0], src_data,
2179b843c749SSergey Zigachev 						dst_addr, cur_size_in_bytes);
2180b843c749SSergey Zigachev 
2181b843c749SSergey Zigachev 			dst_addr += cur_size_in_bytes;
2182b843c749SSergey Zigachev 			byte_count -= cur_size_in_bytes;
2183b843c749SSergey Zigachev 		}
2184b843c749SSergey Zigachev 
2185b843c749SSergey Zigachev 		num_pages -= mm_node->size;
2186b843c749SSergey Zigachev 		++mm_node;
2187b843c749SSergey Zigachev 	}
2188b843c749SSergey Zigachev 
2189b843c749SSergey Zigachev 	amdgpu_ring_pad_ib(ring, &job->ibs[0]);
2190b843c749SSergey Zigachev 	WARN_ON(job->ibs[0].length_dw > num_dw);
2191b843c749SSergey Zigachev 	r = amdgpu_job_submit(job, &adev->mman.entity,
2192b843c749SSergey Zigachev 			      AMDGPU_FENCE_OWNER_UNDEFINED, fence);
2193b843c749SSergey Zigachev 	if (r)
2194b843c749SSergey Zigachev 		goto error_free;
2195b843c749SSergey Zigachev 
2196b843c749SSergey Zigachev 	return 0;
2197b843c749SSergey Zigachev 
2198b843c749SSergey Zigachev error_free:
2199b843c749SSergey Zigachev 	amdgpu_job_free(job);
2200b843c749SSergey Zigachev 	return r;
2201b843c749SSergey Zigachev }
2202b843c749SSergey Zigachev 
2203b843c749SSergey Zigachev #if defined(CONFIG_DEBUG_FS)
2204b843c749SSergey Zigachev 
amdgpu_mm_dump_table(struct seq_file * m,void * data)2205b843c749SSergey Zigachev static int amdgpu_mm_dump_table(struct seq_file *m, void *data)
2206b843c749SSergey Zigachev {
2207b843c749SSergey Zigachev 	struct drm_info_node *node = (struct drm_info_node *)m->private;
2208b843c749SSergey Zigachev 	unsigned ttm_pl = *(int *)node->info_ent->data;
2209b843c749SSergey Zigachev 	struct drm_device *dev = node->minor->dev;
2210b843c749SSergey Zigachev 	struct amdgpu_device *adev = dev->dev_private;
2211b843c749SSergey Zigachev 	struct ttm_mem_type_manager *man = &adev->mman.bdev.man[ttm_pl];
2212b843c749SSergey Zigachev 	struct drm_printer p = drm_seq_file_printer(m);
2213b843c749SSergey Zigachev 
2214b843c749SSergey Zigachev 	man->func->debug(man, &p);
2215b843c749SSergey Zigachev 	return 0;
2216b843c749SSergey Zigachev }
2217b843c749SSergey Zigachev 
2218b843c749SSergey Zigachev static int ttm_pl_vram = TTM_PL_VRAM;
2219b843c749SSergey Zigachev static int ttm_pl_tt = TTM_PL_TT;
2220b843c749SSergey Zigachev 
2221b843c749SSergey Zigachev static const struct drm_info_list amdgpu_ttm_debugfs_list[] = {
2222b843c749SSergey Zigachev 	{"amdgpu_vram_mm", amdgpu_mm_dump_table, 0, &ttm_pl_vram},
2223b843c749SSergey Zigachev 	{"amdgpu_gtt_mm", amdgpu_mm_dump_table, 0, &ttm_pl_tt},
2224b843c749SSergey Zigachev 	{"ttm_page_pool", ttm_page_alloc_debugfs, 0, NULL},
2225b843c749SSergey Zigachev #ifdef CONFIG_SWIOTLB
2226b843c749SSergey Zigachev 	{"ttm_dma_page_pool", ttm_dma_page_alloc_debugfs, 0, NULL}
2227b843c749SSergey Zigachev #endif
2228b843c749SSergey Zigachev };
2229b843c749SSergey Zigachev 
2230b843c749SSergey Zigachev /**
2231b843c749SSergey Zigachev  * amdgpu_ttm_vram_read - Linear read access to VRAM
2232b843c749SSergey Zigachev  *
2233b843c749SSergey Zigachev  * Accesses VRAM via MMIO for debugging purposes.
2234b843c749SSergey Zigachev  */
amdgpu_ttm_vram_read(struct file * f,char __user * buf,size_t size,loff_t * pos)2235b843c749SSergey Zigachev static ssize_t amdgpu_ttm_vram_read(struct file *f, char __user *buf,
2236b843c749SSergey Zigachev 				    size_t size, loff_t *pos)
2237b843c749SSergey Zigachev {
2238b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
2239b843c749SSergey Zigachev 	ssize_t result = 0;
2240b843c749SSergey Zigachev 	int r;
2241b843c749SSergey Zigachev 
2242b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
2243b843c749SSergey Zigachev 		return -EINVAL;
2244b843c749SSergey Zigachev 
2245b843c749SSergey Zigachev 	if (*pos >= adev->gmc.mc_vram_size)
2246b843c749SSergey Zigachev 		return -ENXIO;
2247b843c749SSergey Zigachev 
2248b843c749SSergey Zigachev 	while (size) {
2249b843c749SSergey Zigachev 		unsigned long flags;
2250b843c749SSergey Zigachev 		uint32_t value;
2251b843c749SSergey Zigachev 
2252b843c749SSergey Zigachev 		if (*pos >= adev->gmc.mc_vram_size)
2253b843c749SSergey Zigachev 			return result;
2254b843c749SSergey Zigachev 
2255b843c749SSergey Zigachev 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
2256b843c749SSergey Zigachev 		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
2257b843c749SSergey Zigachev 		WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
2258b843c749SSergey Zigachev 		value = RREG32_NO_KIQ(mmMM_DATA);
2259b843c749SSergey Zigachev 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
2260b843c749SSergey Zigachev 
2261b843c749SSergey Zigachev 		r = put_user(value, (uint32_t *)buf);
2262b843c749SSergey Zigachev 		if (r)
2263b843c749SSergey Zigachev 			return r;
2264b843c749SSergey Zigachev 
2265b843c749SSergey Zigachev 		result += 4;
2266b843c749SSergey Zigachev 		buf += 4;
2267b843c749SSergey Zigachev 		*pos += 4;
2268b843c749SSergey Zigachev 		size -= 4;
2269b843c749SSergey Zigachev 	}
2270b843c749SSergey Zigachev 
2271b843c749SSergey Zigachev 	return result;
2272b843c749SSergey Zigachev }
2273b843c749SSergey Zigachev 
2274b843c749SSergey Zigachev /**
2275b843c749SSergey Zigachev  * amdgpu_ttm_vram_write - Linear write access to VRAM
2276b843c749SSergey Zigachev  *
2277b843c749SSergey Zigachev  * Accesses VRAM via MMIO for debugging purposes.
2278b843c749SSergey Zigachev  */
amdgpu_ttm_vram_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)2279b843c749SSergey Zigachev static ssize_t amdgpu_ttm_vram_write(struct file *f, const char __user *buf,
2280b843c749SSergey Zigachev 				    size_t size, loff_t *pos)
2281b843c749SSergey Zigachev {
2282b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
2283b843c749SSergey Zigachev 	ssize_t result = 0;
2284b843c749SSergey Zigachev 	int r;
2285b843c749SSergey Zigachev 
2286b843c749SSergey Zigachev 	if (size & 0x3 || *pos & 0x3)
2287b843c749SSergey Zigachev 		return -EINVAL;
2288b843c749SSergey Zigachev 
2289b843c749SSergey Zigachev 	if (*pos >= adev->gmc.mc_vram_size)
2290b843c749SSergey Zigachev 		return -ENXIO;
2291b843c749SSergey Zigachev 
2292b843c749SSergey Zigachev 	while (size) {
2293b843c749SSergey Zigachev 		unsigned long flags;
2294b843c749SSergey Zigachev 		uint32_t value;
2295b843c749SSergey Zigachev 
2296b843c749SSergey Zigachev 		if (*pos >= adev->gmc.mc_vram_size)
2297b843c749SSergey Zigachev 			return result;
2298b843c749SSergey Zigachev 
2299b843c749SSergey Zigachev 		r = get_user(value, (uint32_t *)buf);
2300b843c749SSergey Zigachev 		if (r)
2301b843c749SSergey Zigachev 			return r;
2302b843c749SSergey Zigachev 
2303b843c749SSergey Zigachev 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
2304b843c749SSergey Zigachev 		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)*pos) | 0x80000000);
2305b843c749SSergey Zigachev 		WREG32_NO_KIQ(mmMM_INDEX_HI, *pos >> 31);
2306b843c749SSergey Zigachev 		WREG32_NO_KIQ(mmMM_DATA, value);
2307b843c749SSergey Zigachev 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
2308b843c749SSergey Zigachev 
2309b843c749SSergey Zigachev 		result += 4;
2310b843c749SSergey Zigachev 		buf += 4;
2311b843c749SSergey Zigachev 		*pos += 4;
2312b843c749SSergey Zigachev 		size -= 4;
2313b843c749SSergey Zigachev 	}
2314b843c749SSergey Zigachev 
2315b843c749SSergey Zigachev 	return result;
2316b843c749SSergey Zigachev }
2317b843c749SSergey Zigachev 
2318b843c749SSergey Zigachev static const struct file_operations amdgpu_ttm_vram_fops = {
2319b843c749SSergey Zigachev 	.owner = THIS_MODULE,
2320b843c749SSergey Zigachev 	.read = amdgpu_ttm_vram_read,
2321b843c749SSergey Zigachev 	.write = amdgpu_ttm_vram_write,
2322b843c749SSergey Zigachev 	.llseek = default_llseek,
2323b843c749SSergey Zigachev };
2324b843c749SSergey Zigachev 
2325b843c749SSergey Zigachev #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2326b843c749SSergey Zigachev 
2327b843c749SSergey Zigachev /**
2328b843c749SSergey Zigachev  * amdgpu_ttm_gtt_read - Linear read access to GTT memory
2329b843c749SSergey Zigachev  */
amdgpu_ttm_gtt_read(struct file * f,char __user * buf,size_t size,loff_t * pos)2330b843c749SSergey Zigachev static ssize_t amdgpu_ttm_gtt_read(struct file *f, char __user *buf,
2331b843c749SSergey Zigachev 				   size_t size, loff_t *pos)
2332b843c749SSergey Zigachev {
2333b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
2334b843c749SSergey Zigachev 	ssize_t result = 0;
2335b843c749SSergey Zigachev 	int r;
2336b843c749SSergey Zigachev 
2337b843c749SSergey Zigachev 	while (size) {
2338b843c749SSergey Zigachev 		loff_t p = *pos / PAGE_SIZE;
2339b843c749SSergey Zigachev 		unsigned off = *pos & ~PAGE_MASK;
2340b843c749SSergey Zigachev 		size_t cur_size = min_t(size_t, size, PAGE_SIZE - off);
2341b843c749SSergey Zigachev 		struct page *page;
2342b843c749SSergey Zigachev 		void *ptr;
2343b843c749SSergey Zigachev 
2344b843c749SSergey Zigachev 		if (p >= adev->gart.num_cpu_pages)
2345b843c749SSergey Zigachev 			return result;
2346b843c749SSergey Zigachev 
2347b843c749SSergey Zigachev 		page = adev->gart.pages[p];
2348b843c749SSergey Zigachev 		if (page) {
2349b843c749SSergey Zigachev 			ptr = kmap(page);
2350b843c749SSergey Zigachev 			ptr += off;
2351b843c749SSergey Zigachev 
2352b843c749SSergey Zigachev 			r = copy_to_user(buf, ptr, cur_size);
2353b843c749SSergey Zigachev 			kunmap(adev->gart.pages[p]);
2354b843c749SSergey Zigachev 		} else
2355b843c749SSergey Zigachev 			r = clear_user(buf, cur_size);
2356b843c749SSergey Zigachev 
2357b843c749SSergey Zigachev 		if (r)
2358b843c749SSergey Zigachev 			return -EFAULT;
2359b843c749SSergey Zigachev 
2360b843c749SSergey Zigachev 		result += cur_size;
2361b843c749SSergey Zigachev 		buf += cur_size;
2362b843c749SSergey Zigachev 		*pos += cur_size;
2363b843c749SSergey Zigachev 		size -= cur_size;
2364b843c749SSergey Zigachev 	}
2365b843c749SSergey Zigachev 
2366b843c749SSergey Zigachev 	return result;
2367b843c749SSergey Zigachev }
2368b843c749SSergey Zigachev 
2369b843c749SSergey Zigachev static const struct file_operations amdgpu_ttm_gtt_fops = {
2370b843c749SSergey Zigachev 	.owner = THIS_MODULE,
2371b843c749SSergey Zigachev 	.read = amdgpu_ttm_gtt_read,
2372b843c749SSergey Zigachev 	.llseek = default_llseek
2373b843c749SSergey Zigachev };
2374b843c749SSergey Zigachev 
2375b843c749SSergey Zigachev #endif
2376b843c749SSergey Zigachev 
2377b843c749SSergey Zigachev /**
2378b843c749SSergey Zigachev  * amdgpu_iomem_read - Virtual read access to GPU mapped memory
2379b843c749SSergey Zigachev  *
2380b843c749SSergey Zigachev  * This function is used to read memory that has been mapped to the
2381b843c749SSergey Zigachev  * GPU and the known addresses are not physical addresses but instead
2382b843c749SSergey Zigachev  * bus addresses (e.g., what you'd put in an IB or ring buffer).
2383b843c749SSergey Zigachev  */
amdgpu_iomem_read(struct file * f,char __user * buf,size_t size,loff_t * pos)2384b843c749SSergey Zigachev static ssize_t amdgpu_iomem_read(struct file *f, char __user *buf,
2385b843c749SSergey Zigachev 				 size_t size, loff_t *pos)
2386b843c749SSergey Zigachev {
2387b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
2388b843c749SSergey Zigachev 	struct iommu_domain *dom;
2389b843c749SSergey Zigachev 	ssize_t result = 0;
2390b843c749SSergey Zigachev 	int r;
2391b843c749SSergey Zigachev 
2392b843c749SSergey Zigachev 	/* retrieve the IOMMU domain if any for this device */
2393b843c749SSergey Zigachev 	dom = iommu_get_domain_for_dev(adev->dev);
2394b843c749SSergey Zigachev 
2395b843c749SSergey Zigachev 	while (size) {
2396b843c749SSergey Zigachev 		phys_addr_t addr = *pos & PAGE_MASK;
2397b843c749SSergey Zigachev 		loff_t off = *pos & ~PAGE_MASK;
2398b843c749SSergey Zigachev 		size_t bytes = PAGE_SIZE - off;
2399b843c749SSergey Zigachev 		unsigned long pfn;
2400b843c749SSergey Zigachev 		struct page *p;
2401b843c749SSergey Zigachev 		void *ptr;
2402b843c749SSergey Zigachev 
2403b843c749SSergey Zigachev 		bytes = bytes < size ? bytes : size;
2404b843c749SSergey Zigachev 
2405b843c749SSergey Zigachev 		/* Translate the bus address to a physical address.  If
2406b843c749SSergey Zigachev 		 * the domain is NULL it means there is no IOMMU active
2407b843c749SSergey Zigachev 		 * and the address translation is the identity
2408b843c749SSergey Zigachev 		 */
2409b843c749SSergey Zigachev 		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2410b843c749SSergey Zigachev 
2411b843c749SSergey Zigachev 		pfn = addr >> PAGE_SHIFT;
2412b843c749SSergey Zigachev 		if (!pfn_valid(pfn))
2413b843c749SSergey Zigachev 			return -EPERM;
2414b843c749SSergey Zigachev 
2415b843c749SSergey Zigachev 		p = pfn_to_page(pfn);
2416b843c749SSergey Zigachev 		if (p->mapping != adev->mman.bdev.dev_mapping)
2417b843c749SSergey Zigachev 			return -EPERM;
2418b843c749SSergey Zigachev 
2419b843c749SSergey Zigachev 		ptr = kmap(p);
2420b843c749SSergey Zigachev 		r = copy_to_user(buf, ptr + off, bytes);
2421b843c749SSergey Zigachev 		kunmap(p);
2422b843c749SSergey Zigachev 		if (r)
2423b843c749SSergey Zigachev 			return -EFAULT;
2424b843c749SSergey Zigachev 
2425b843c749SSergey Zigachev 		size -= bytes;
2426b843c749SSergey Zigachev 		*pos += bytes;
2427b843c749SSergey Zigachev 		result += bytes;
2428b843c749SSergey Zigachev 	}
2429b843c749SSergey Zigachev 
2430b843c749SSergey Zigachev 	return result;
2431b843c749SSergey Zigachev }
2432b843c749SSergey Zigachev 
2433b843c749SSergey Zigachev /**
2434b843c749SSergey Zigachev  * amdgpu_iomem_write - Virtual write access to GPU mapped memory
2435b843c749SSergey Zigachev  *
2436b843c749SSergey Zigachev  * This function is used to write memory that has been mapped to the
2437b843c749SSergey Zigachev  * GPU and the known addresses are not physical addresses but instead
2438b843c749SSergey Zigachev  * bus addresses (e.g., what you'd put in an IB or ring buffer).
2439b843c749SSergey Zigachev  */
amdgpu_iomem_write(struct file * f,const char __user * buf,size_t size,loff_t * pos)2440b843c749SSergey Zigachev static ssize_t amdgpu_iomem_write(struct file *f, const char __user *buf,
2441b843c749SSergey Zigachev 				 size_t size, loff_t *pos)
2442b843c749SSergey Zigachev {
2443b843c749SSergey Zigachev 	struct amdgpu_device *adev = file_inode(f)->i_private;
2444b843c749SSergey Zigachev 	struct iommu_domain *dom;
2445b843c749SSergey Zigachev 	ssize_t result = 0;
2446b843c749SSergey Zigachev 	int r;
2447b843c749SSergey Zigachev 
2448b843c749SSergey Zigachev 	dom = iommu_get_domain_for_dev(adev->dev);
2449b843c749SSergey Zigachev 
2450b843c749SSergey Zigachev 	while (size) {
2451b843c749SSergey Zigachev 		phys_addr_t addr = *pos & PAGE_MASK;
2452b843c749SSergey Zigachev 		loff_t off = *pos & ~PAGE_MASK;
2453b843c749SSergey Zigachev 		size_t bytes = PAGE_SIZE - off;
2454b843c749SSergey Zigachev 		unsigned long pfn;
2455b843c749SSergey Zigachev 		struct page *p;
2456b843c749SSergey Zigachev 		void *ptr;
2457b843c749SSergey Zigachev 
2458b843c749SSergey Zigachev 		bytes = bytes < size ? bytes : size;
2459b843c749SSergey Zigachev 
2460b843c749SSergey Zigachev 		addr = dom ? iommu_iova_to_phys(dom, addr) : addr;
2461b843c749SSergey Zigachev 
2462b843c749SSergey Zigachev 		pfn = addr >> PAGE_SHIFT;
2463b843c749SSergey Zigachev 		if (!pfn_valid(pfn))
2464b843c749SSergey Zigachev 			return -EPERM;
2465b843c749SSergey Zigachev 
2466b843c749SSergey Zigachev 		p = pfn_to_page(pfn);
2467b843c749SSergey Zigachev 		if (p->mapping != adev->mman.bdev.dev_mapping)
2468b843c749SSergey Zigachev 			return -EPERM;
2469b843c749SSergey Zigachev 
2470b843c749SSergey Zigachev 		ptr = kmap(p);
2471b843c749SSergey Zigachev 		r = copy_from_user(ptr + off, buf, bytes);
2472b843c749SSergey Zigachev 		kunmap(p);
2473b843c749SSergey Zigachev 		if (r)
2474b843c749SSergey Zigachev 			return -EFAULT;
2475b843c749SSergey Zigachev 
2476b843c749SSergey Zigachev 		size -= bytes;
2477b843c749SSergey Zigachev 		*pos += bytes;
2478b843c749SSergey Zigachev 		result += bytes;
2479b843c749SSergey Zigachev 	}
2480b843c749SSergey Zigachev 
2481b843c749SSergey Zigachev 	return result;
2482b843c749SSergey Zigachev }
2483b843c749SSergey Zigachev 
2484b843c749SSergey Zigachev static const struct file_operations amdgpu_ttm_iomem_fops = {
2485b843c749SSergey Zigachev 	.owner = THIS_MODULE,
2486b843c749SSergey Zigachev 	.read = amdgpu_iomem_read,
2487b843c749SSergey Zigachev 	.write = amdgpu_iomem_write,
2488b843c749SSergey Zigachev 	.llseek = default_llseek
2489b843c749SSergey Zigachev };
2490b843c749SSergey Zigachev 
2491b843c749SSergey Zigachev static const struct {
2492b843c749SSergey Zigachev 	char *name;
2493b843c749SSergey Zigachev 	const struct file_operations *fops;
2494b843c749SSergey Zigachev 	int domain;
2495b843c749SSergey Zigachev } ttm_debugfs_entries[] = {
2496b843c749SSergey Zigachev 	{ "amdgpu_vram", &amdgpu_ttm_vram_fops, TTM_PL_VRAM },
2497b843c749SSergey Zigachev #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
2498b843c749SSergey Zigachev 	{ "amdgpu_gtt", &amdgpu_ttm_gtt_fops, TTM_PL_TT },
2499b843c749SSergey Zigachev #endif
2500b843c749SSergey Zigachev 	{ "amdgpu_iomem", &amdgpu_ttm_iomem_fops, TTM_PL_SYSTEM },
2501b843c749SSergey Zigachev };
2502b843c749SSergey Zigachev 
2503b843c749SSergey Zigachev #endif
2504b843c749SSergey Zigachev 
amdgpu_ttm_debugfs_init(struct amdgpu_device * adev)2505b843c749SSergey Zigachev static int amdgpu_ttm_debugfs_init(struct amdgpu_device *adev)
2506b843c749SSergey Zigachev {
2507b843c749SSergey Zigachev #if defined(CONFIG_DEBUG_FS)
2508b843c749SSergey Zigachev 	unsigned count;
2509b843c749SSergey Zigachev 
2510b843c749SSergey Zigachev 	struct drm_minor *minor = adev->ddev->primary;
2511b843c749SSergey Zigachev 	struct dentry *ent, *root = minor->debugfs_root;
2512b843c749SSergey Zigachev 
2513b843c749SSergey Zigachev 	for (count = 0; count < ARRAY_SIZE(ttm_debugfs_entries); count++) {
2514b843c749SSergey Zigachev 		ent = debugfs_create_file(
2515b843c749SSergey Zigachev 				ttm_debugfs_entries[count].name,
2516b843c749SSergey Zigachev 				S_IFREG | S_IRUGO, root,
2517b843c749SSergey Zigachev 				adev,
2518b843c749SSergey Zigachev 				ttm_debugfs_entries[count].fops);
2519b843c749SSergey Zigachev 		if (IS_ERR(ent))
2520b843c749SSergey Zigachev 			return PTR_ERR(ent);
2521b843c749SSergey Zigachev 		if (ttm_debugfs_entries[count].domain == TTM_PL_VRAM)
2522b843c749SSergey Zigachev 			i_size_write(ent->d_inode, adev->gmc.mc_vram_size);
2523b843c749SSergey Zigachev 		else if (ttm_debugfs_entries[count].domain == TTM_PL_TT)
2524b843c749SSergey Zigachev 			i_size_write(ent->d_inode, adev->gmc.gart_size);
2525b843c749SSergey Zigachev 		adev->mman.debugfs_entries[count] = ent;
2526b843c749SSergey Zigachev 	}
2527b843c749SSergey Zigachev 
2528b843c749SSergey Zigachev 	count = ARRAY_SIZE(amdgpu_ttm_debugfs_list);
2529b843c749SSergey Zigachev 
2530b843c749SSergey Zigachev #ifdef CONFIG_SWIOTLB
2531b843c749SSergey Zigachev 	if (!(adev->need_swiotlb && swiotlb_nr_tbl()))
2532b843c749SSergey Zigachev 		--count;
2533b843c749SSergey Zigachev #endif
2534b843c749SSergey Zigachev 
2535b843c749SSergey Zigachev 	return amdgpu_debugfs_add_files(adev, amdgpu_ttm_debugfs_list, count);
2536b843c749SSergey Zigachev #else
2537b843c749SSergey Zigachev 	return 0;
2538b843c749SSergey Zigachev #endif
2539b843c749SSergey Zigachev }
2540b843c749SSergey Zigachev 
amdgpu_ttm_debugfs_fini(struct amdgpu_device * adev)2541b843c749SSergey Zigachev static void amdgpu_ttm_debugfs_fini(struct amdgpu_device *adev)
2542b843c749SSergey Zigachev {
2543b843c749SSergey Zigachev #if defined(CONFIG_DEBUG_FS)
2544b843c749SSergey Zigachev 	unsigned i;
2545b843c749SSergey Zigachev 
2546b843c749SSergey Zigachev 	for (i = 0; i < ARRAY_SIZE(ttm_debugfs_entries); i++)
2547b843c749SSergey Zigachev 		debugfs_remove(adev->mman.debugfs_entries[i]);
2548b843c749SSergey Zigachev #endif
2549b843c749SSergey Zigachev }
2550