xref: /dflybsd-src/sys/dev/drm/radeon/radeon_object.c (revision 9eb960773498463a0345970327518dc99f07e96b)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2009 Jerome Glisse.
3926deccbSFrançois Tigeot  * All Rights Reserved.
4926deccbSFrançois Tigeot  *
5926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
6926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the
7926deccbSFrançois Tigeot  * "Software"), to deal in the Software without restriction, including
8926deccbSFrançois Tigeot  * without limitation the rights to use, copy, modify, merge, publish,
9926deccbSFrançois Tigeot  * distribute, sub license, and/or sell copies of the Software, and to
10926deccbSFrançois Tigeot  * permit persons to whom the Software is furnished to do so, subject to
11926deccbSFrançois Tigeot  * the following conditions:
12926deccbSFrançois Tigeot  *
13926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17926deccbSFrançois Tigeot  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18926deccbSFrançois Tigeot  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19926deccbSFrançois Tigeot  * USE OR OTHER DEALINGS IN THE SOFTWARE.
20926deccbSFrançois Tigeot  *
21926deccbSFrançois Tigeot  * The above copyright notice and this permission notice (including the
22926deccbSFrançois Tigeot  * next paragraph) shall be included in all copies or substantial portions
23926deccbSFrançois Tigeot  * of the Software.
24926deccbSFrançois Tigeot  *
25926deccbSFrançois Tigeot  */
26926deccbSFrançois Tigeot /*
27926deccbSFrançois Tigeot  * Authors:
28926deccbSFrançois Tigeot  *    Jerome Glisse <glisse@freedesktop.org>
29926deccbSFrançois Tigeot  *    Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
30926deccbSFrançois Tigeot  *    Dave Airlie
31926deccbSFrançois Tigeot  */
327dcf36dcSFrançois Tigeot #include <linux/list.h>
33926deccbSFrançois Tigeot #include <drm/drmP.h>
3483b4b9b9SFrançois Tigeot #include <drm/radeon_drm.h>
35c59a5c48SFrançois Tigeot #include <drm/drm_cache.h>
36926deccbSFrançois Tigeot #include "radeon.h"
37926deccbSFrançois Tigeot #include "radeon_trace.h"
38926deccbSFrançois Tigeot 
39926deccbSFrançois Tigeot 
40926deccbSFrançois Tigeot static void radeon_bo_clear_surface_reg(struct radeon_bo *bo);
41926deccbSFrançois Tigeot 
42926deccbSFrançois Tigeot /*
43926deccbSFrançois Tigeot  * To exclude mutual BO access we rely on bo_reserve exclusion, as all
44926deccbSFrançois Tigeot  * function are calling it.
45926deccbSFrançois Tigeot  */
46926deccbSFrançois Tigeot 
radeon_update_memory_usage(struct radeon_bo * bo,unsigned mem_type,int sign)47c6f73aabSFrançois Tigeot static void radeon_update_memory_usage(struct radeon_bo *bo,
48c6f73aabSFrançois Tigeot 				       unsigned mem_type, int sign)
49926deccbSFrançois Tigeot {
50c6f73aabSFrançois Tigeot 	struct radeon_device *rdev = bo->rdev;
51c6f73aabSFrançois Tigeot 	u64 size = (u64)bo->tbo.num_pages << PAGE_SHIFT;
52926deccbSFrançois Tigeot 
53c6f73aabSFrançois Tigeot 	switch (mem_type) {
54c6f73aabSFrançois Tigeot 	case TTM_PL_TT:
55c6f73aabSFrançois Tigeot 		if (sign > 0)
56c6f73aabSFrançois Tigeot 			atomic64_add(size, &rdev->gtt_usage);
57c6f73aabSFrançois Tigeot 		else
58c6f73aabSFrançois Tigeot 			atomic64_sub(size, &rdev->gtt_usage);
59c6f73aabSFrançois Tigeot 		break;
60c6f73aabSFrançois Tigeot 	case TTM_PL_VRAM:
61c6f73aabSFrançois Tigeot 		if (sign > 0)
62c6f73aabSFrançois Tigeot 			atomic64_add(size, &rdev->vram_usage);
63c6f73aabSFrançois Tigeot 		else
64c6f73aabSFrançois Tigeot 			atomic64_sub(size, &rdev->vram_usage);
65c6f73aabSFrançois Tigeot 		break;
66926deccbSFrançois Tigeot 	}
67926deccbSFrançois Tigeot }
68926deccbSFrançois Tigeot 
radeon_ttm_bo_destroy(struct ttm_buffer_object * tbo)69926deccbSFrançois Tigeot static void radeon_ttm_bo_destroy(struct ttm_buffer_object *tbo)
70926deccbSFrançois Tigeot {
71926deccbSFrançois Tigeot 	struct radeon_bo *bo;
72926deccbSFrançois Tigeot 
73926deccbSFrançois Tigeot 	bo = container_of(tbo, struct radeon_bo, tbo);
74c6f73aabSFrançois Tigeot 
75c6f73aabSFrançois Tigeot 	radeon_update_memory_usage(bo, bo->tbo.mem.mem_type, -1);
76c6f73aabSFrançois Tigeot 
77fefad7a7SFrançois Tigeot 	mutex_lock(&bo->rdev->gem.mutex);
78926deccbSFrançois Tigeot 	list_del_init(&bo->list);
79fefad7a7SFrançois Tigeot 	mutex_unlock(&bo->rdev->gem.mutex);
80926deccbSFrançois Tigeot 	radeon_bo_clear_surface_reg(bo);
81a85cb24fSFrançois Tigeot 	WARN_ON_ONCE(!list_empty(&bo->va));
823f2dd94aSFrançois Tigeot 	if (bo->gem_base.import_attach)
833f2dd94aSFrançois Tigeot 		drm_prime_gem_destroy(&bo->gem_base, bo->tbo.sg);
84926deccbSFrançois Tigeot 	drm_gem_object_release(&bo->gem_base);
85c4ef309bSzrj 	kfree(bo);
86926deccbSFrançois Tigeot }
87926deccbSFrançois Tigeot 
radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object * bo)88926deccbSFrançois Tigeot bool radeon_ttm_bo_is_radeon_bo(struct ttm_buffer_object *bo)
89926deccbSFrançois Tigeot {
90926deccbSFrançois Tigeot 	if (bo->destroy == &radeon_ttm_bo_destroy)
91926deccbSFrançois Tigeot 		return true;
92926deccbSFrançois Tigeot 	return false;
93926deccbSFrançois Tigeot }
94926deccbSFrançois Tigeot 
radeon_ttm_placement_from_domain(struct radeon_bo * rbo,u32 domain)95926deccbSFrançois Tigeot void radeon_ttm_placement_from_domain(struct radeon_bo *rbo, u32 domain)
96926deccbSFrançois Tigeot {
97c6f73aabSFrançois Tigeot 	u32 c = 0, i;
98926deccbSFrançois Tigeot 
99926deccbSFrançois Tigeot 	rbo->placement.placement = rbo->placements;
100926deccbSFrançois Tigeot 	rbo->placement.busy_placement = rbo->placements;
1017dcf36dcSFrançois Tigeot 	if (domain & RADEON_GEM_DOMAIN_VRAM) {
1027dcf36dcSFrançois Tigeot 		/* Try placing BOs which don't need CPU access outside of the
1037dcf36dcSFrançois Tigeot 		 * CPU accessible part of VRAM
1047dcf36dcSFrançois Tigeot 		 */
1057dcf36dcSFrançois Tigeot 		if ((rbo->flags & RADEON_GEM_NO_CPU_ACCESS) &&
1067dcf36dcSFrançois Tigeot 		    rbo->rdev->mc.visible_vram_size < rbo->rdev->mc.real_vram_size) {
1077dcf36dcSFrançois Tigeot 			rbo->placements[c].fpfn =
1087dcf36dcSFrançois Tigeot 				rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
109591d5043SFrançois Tigeot 			rbo->placements[c++].flags = TTM_PL_FLAG_WC |
110591d5043SFrançois Tigeot 						     TTM_PL_FLAG_UNCACHED |
111926deccbSFrançois Tigeot 						     TTM_PL_FLAG_VRAM;
1127dcf36dcSFrançois Tigeot 		}
1137dcf36dcSFrançois Tigeot 
1147dcf36dcSFrançois Tigeot 		rbo->placements[c].fpfn = 0;
1157dcf36dcSFrançois Tigeot 		rbo->placements[c++].flags = TTM_PL_FLAG_WC |
1167dcf36dcSFrançois Tigeot 					     TTM_PL_FLAG_UNCACHED |
1177dcf36dcSFrançois Tigeot 					     TTM_PL_FLAG_VRAM;
1187dcf36dcSFrançois Tigeot 	}
119591d5043SFrançois Tigeot 
120926deccbSFrançois Tigeot 	if (domain & RADEON_GEM_DOMAIN_GTT) {
121c6f73aabSFrançois Tigeot 		if (rbo->flags & RADEON_GEM_GTT_UC) {
1227dcf36dcSFrançois Tigeot 			rbo->placements[c].fpfn = 0;
123591d5043SFrançois Tigeot 			rbo->placements[c++].flags = TTM_PL_FLAG_UNCACHED |
124591d5043SFrançois Tigeot 				TTM_PL_FLAG_TT;
125591d5043SFrançois Tigeot 
126c6f73aabSFrançois Tigeot 		} else if ((rbo->flags & RADEON_GEM_GTT_WC) ||
127c6f73aabSFrançois Tigeot 			   (rbo->rdev->flags & RADEON_IS_AGP)) {
1287dcf36dcSFrançois Tigeot 			rbo->placements[c].fpfn = 0;
129591d5043SFrançois Tigeot 			rbo->placements[c++].flags = TTM_PL_FLAG_WC |
130591d5043SFrançois Tigeot 				TTM_PL_FLAG_UNCACHED |
131c6f73aabSFrançois Tigeot 				TTM_PL_FLAG_TT;
132926deccbSFrançois Tigeot 		} else {
1337dcf36dcSFrançois Tigeot 			rbo->placements[c].fpfn = 0;
134591d5043SFrançois Tigeot 			rbo->placements[c++].flags = TTM_PL_FLAG_CACHED |
135591d5043SFrançois Tigeot 						     TTM_PL_FLAG_TT;
136926deccbSFrançois Tigeot 		}
137926deccbSFrançois Tigeot 	}
138591d5043SFrançois Tigeot 
139926deccbSFrançois Tigeot 	if (domain & RADEON_GEM_DOMAIN_CPU) {
140c6f73aabSFrançois Tigeot 		if (rbo->flags & RADEON_GEM_GTT_UC) {
1417dcf36dcSFrançois Tigeot 			rbo->placements[c].fpfn = 0;
142591d5043SFrançois Tigeot 			rbo->placements[c++].flags = TTM_PL_FLAG_UNCACHED |
143591d5043SFrançois Tigeot 				TTM_PL_FLAG_SYSTEM;
144591d5043SFrançois Tigeot 
145c6f73aabSFrançois Tigeot 		} else if ((rbo->flags & RADEON_GEM_GTT_WC) ||
146c6f73aabSFrançois Tigeot 		    rbo->rdev->flags & RADEON_IS_AGP) {
1477dcf36dcSFrançois Tigeot 			rbo->placements[c].fpfn = 0;
148591d5043SFrançois Tigeot 			rbo->placements[c++].flags = TTM_PL_FLAG_WC |
149591d5043SFrançois Tigeot 				TTM_PL_FLAG_UNCACHED |
150c6f73aabSFrançois Tigeot 				TTM_PL_FLAG_SYSTEM;
151926deccbSFrançois Tigeot 		} else {
1527dcf36dcSFrançois Tigeot 			rbo->placements[c].fpfn = 0;
153591d5043SFrançois Tigeot 			rbo->placements[c++].flags = TTM_PL_FLAG_CACHED |
154591d5043SFrançois Tigeot 						     TTM_PL_FLAG_SYSTEM;
155926deccbSFrançois Tigeot 		}
156926deccbSFrançois Tigeot 	}
1577dcf36dcSFrançois Tigeot 	if (!c) {
1587dcf36dcSFrançois Tigeot 		rbo->placements[c].fpfn = 0;
159591d5043SFrançois Tigeot 		rbo->placements[c++].flags = TTM_PL_MASK_CACHING |
160591d5043SFrançois Tigeot 					     TTM_PL_FLAG_SYSTEM;
1617dcf36dcSFrançois Tigeot 	}
162591d5043SFrançois Tigeot 
163926deccbSFrançois Tigeot 	rbo->placement.num_placement = c;
164926deccbSFrançois Tigeot 	rbo->placement.num_busy_placement = c;
165c6f73aabSFrançois Tigeot 
166591d5043SFrançois Tigeot 	for (i = 0; i < c; ++i) {
167591d5043SFrançois Tigeot 		if ((rbo->flags & RADEON_GEM_CPU_ACCESS) &&
1687dcf36dcSFrançois Tigeot 		    (rbo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
1697dcf36dcSFrançois Tigeot 		    !rbo->placements[i].fpfn)
170591d5043SFrançois Tigeot 			rbo->placements[i].lpfn =
171591d5043SFrançois Tigeot 				rbo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
172591d5043SFrançois Tigeot 		else
173591d5043SFrançois Tigeot 			rbo->placements[i].lpfn = 0;
174591d5043SFrançois Tigeot 	}
175926deccbSFrançois Tigeot }
176926deccbSFrançois Tigeot 
radeon_bo_create(struct radeon_device * rdev,unsigned long size,int byte_align,bool kernel,u32 domain,u32 flags,struct sg_table * sg,struct reservation_object * resv,struct radeon_bo ** bo_ptr)177926deccbSFrançois Tigeot int radeon_bo_create(struct radeon_device *rdev,
1787dcf36dcSFrançois Tigeot 		     unsigned long size, int byte_align, bool kernel,
1797dcf36dcSFrançois Tigeot 		     u32 domain, u32 flags, struct sg_table *sg,
1807dcf36dcSFrançois Tigeot 		     struct reservation_object *resv,
1817dcf36dcSFrançois Tigeot 		     struct radeon_bo **bo_ptr)
182926deccbSFrançois Tigeot {
183926deccbSFrançois Tigeot 	struct radeon_bo *bo;
184926deccbSFrançois Tigeot 	enum ttm_bo_type type;
1857dcf36dcSFrançois Tigeot 	unsigned long page_align = roundup(byte_align, PAGE_SIZE) >> PAGE_SHIFT;
186926deccbSFrançois Tigeot 	size_t acc_size;
187926deccbSFrançois Tigeot 	int r;
188926deccbSFrançois Tigeot 
189c4ef309bSzrj 	size = ALIGN(size, PAGE_SIZE);
190926deccbSFrançois Tigeot 
191926deccbSFrançois Tigeot 	if (kernel) {
192926deccbSFrançois Tigeot 		type = ttm_bo_type_kernel;
193926deccbSFrançois Tigeot 	} else if (sg) {
194926deccbSFrançois Tigeot 		type = ttm_bo_type_sg;
195926deccbSFrançois Tigeot 	} else {
196926deccbSFrançois Tigeot 		type = ttm_bo_type_device;
197926deccbSFrançois Tigeot 	}
198926deccbSFrançois Tigeot 	*bo_ptr = NULL;
199926deccbSFrançois Tigeot 
200926deccbSFrançois Tigeot 	acc_size = ttm_bo_dma_acc_size(&rdev->mman.bdev, size,
201926deccbSFrançois Tigeot 				       sizeof(struct radeon_bo));
202926deccbSFrançois Tigeot 
203c4ef309bSzrj 	bo = kzalloc(sizeof(struct radeon_bo), GFP_KERNEL);
204926deccbSFrançois Tigeot 	if (bo == NULL)
205926deccbSFrançois Tigeot 		return -ENOMEM;
206926deccbSFrançois Tigeot 	r = drm_gem_object_init(rdev->ddev, &bo->gem_base, size);
207926deccbSFrançois Tigeot 	if (unlikely(r)) {
208c4ef309bSzrj 		kfree(bo);
209926deccbSFrançois Tigeot 		return r;
210926deccbSFrançois Tigeot 	}
211926deccbSFrançois Tigeot 	bo->rdev = rdev;
212926deccbSFrançois Tigeot 	bo->surface_reg = -1;
213926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&bo->list);
214926deccbSFrançois Tigeot 	INIT_LIST_HEAD(&bo->va);
215c6f73aabSFrançois Tigeot 	bo->initial_domain = domain & (RADEON_GEM_DOMAIN_VRAM |
216c6f73aabSFrançois Tigeot 				       RADEON_GEM_DOMAIN_GTT |
217c6f73aabSFrançois Tigeot 				       RADEON_GEM_DOMAIN_CPU);
218c6f73aabSFrançois Tigeot 
219c6f73aabSFrançois Tigeot 	bo->flags = flags;
220c6f73aabSFrançois Tigeot 	/* PCI GART is always snooped */
221c6f73aabSFrançois Tigeot 	if (!(rdev->flags & RADEON_IS_PCIE))
222c6f73aabSFrançois Tigeot 		bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
223c6f73aabSFrançois Tigeot 
224c59a5c48SFrançois Tigeot 	/* Write-combined CPU mappings of GTT cause GPU hangs with RV6xx
225c59a5c48SFrançois Tigeot 	 * See https://bugs.freedesktop.org/show_bug.cgi?id=91268
226c59a5c48SFrançois Tigeot 	 */
227c59a5c48SFrançois Tigeot 	if (rdev->family >= CHIP_RV610 && rdev->family <= CHIP_RV635)
228c59a5c48SFrançois Tigeot 		bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
229c59a5c48SFrançois Tigeot 
230591d5043SFrançois Tigeot #ifdef CONFIG_X86_32
231591d5043SFrançois Tigeot 	/* XXX: Write-combined CPU mappings of GTT seem broken on 32-bit
232591d5043SFrançois Tigeot 	 * See https://bugs.freedesktop.org/show_bug.cgi?id=84627
233591d5043SFrançois Tigeot 	 */
234c59a5c48SFrançois Tigeot 	bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
2357dcf36dcSFrançois Tigeot #elif defined(CONFIG_X86) && !defined(CONFIG_X86_PAT)
2367dcf36dcSFrançois Tigeot 	/* Don't try to enable write-combining when it can't work, or things
2377dcf36dcSFrançois Tigeot 	 * may be slow
2387dcf36dcSFrançois Tigeot 	 * See https://bugs.freedesktop.org/show_bug.cgi?id=88758
2397dcf36dcSFrançois Tigeot 	 */
240c59a5c48SFrançois Tigeot #ifndef CONFIG_COMPILE_TEST
2417dcf36dcSFrançois Tigeot #warning Please enable CONFIG_MTRR and CONFIG_X86_PAT for better performance \
2427dcf36dcSFrançois Tigeot 	 thanks to write-combining
243c59a5c48SFrançois Tigeot #endif
2447dcf36dcSFrançois Tigeot 
245c59a5c48SFrançois Tigeot 	if (bo->flags & RADEON_GEM_GTT_WC)
2467dcf36dcSFrançois Tigeot 		DRM_INFO_ONCE("Please enable CONFIG_MTRR and CONFIG_X86_PAT for "
2477dcf36dcSFrançois Tigeot 			      "better performance thanks to write-combining\n");
248c59a5c48SFrançois Tigeot 	bo->flags &= ~(RADEON_GEM_GTT_WC | RADEON_GEM_GTT_UC);
249c59a5c48SFrançois Tigeot #else
250c59a5c48SFrançois Tigeot 	/* For architectures that don't support WC memory,
251c59a5c48SFrançois Tigeot 	 * mask out the WC flag from the BO
252c59a5c48SFrançois Tigeot 	 */
253c59a5c48SFrançois Tigeot 	if (!drm_arch_can_wc_memory())
2547dcf36dcSFrançois Tigeot 		bo->flags &= ~RADEON_GEM_GTT_WC;
255591d5043SFrançois Tigeot #endif
256591d5043SFrançois Tigeot 
257926deccbSFrançois Tigeot 	radeon_ttm_placement_from_domain(bo, domain);
258926deccbSFrançois Tigeot 	/* Kernel allocation are uninterruptible */
2597dcf36dcSFrançois Tigeot 	down_read(&rdev->pm.mclk_lock);
260926deccbSFrançois Tigeot 	r = ttm_bo_init(&rdev->mman.bdev, &bo->tbo, size, type,
261*9eb96077SSergey Zigachev 			&bo->placement, page_align, !kernel,
2627dcf36dcSFrançois Tigeot 			acc_size, sg, resv, &radeon_ttm_bo_destroy);
2631cfef1a5SFrançois Tigeot 	up_read(&rdev->pm.mclk_lock);
264926deccbSFrançois Tigeot 	if (unlikely(r != 0)) {
265926deccbSFrançois Tigeot 		return r;
266926deccbSFrançois Tigeot 	}
267926deccbSFrançois Tigeot 	*bo_ptr = bo;
268926deccbSFrançois Tigeot 
269926deccbSFrançois Tigeot 	trace_radeon_bo_create(bo);
270926deccbSFrançois Tigeot 
271926deccbSFrançois Tigeot 	return 0;
272926deccbSFrançois Tigeot }
273926deccbSFrançois Tigeot 
radeon_bo_kmap(struct radeon_bo * bo,void ** ptr)274926deccbSFrançois Tigeot int radeon_bo_kmap(struct radeon_bo *bo, void **ptr)
275926deccbSFrançois Tigeot {
276926deccbSFrançois Tigeot 	bool is_iomem;
277926deccbSFrançois Tigeot 	int r;
278926deccbSFrançois Tigeot 
279926deccbSFrançois Tigeot 	if (bo->kptr) {
280926deccbSFrançois Tigeot 		if (ptr) {
281926deccbSFrançois Tigeot 			*ptr = bo->kptr;
282926deccbSFrançois Tigeot 		}
283926deccbSFrançois Tigeot 		return 0;
284926deccbSFrançois Tigeot 	}
285926deccbSFrançois Tigeot 	r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap);
286926deccbSFrançois Tigeot 	if (r) {
287926deccbSFrançois Tigeot 		return r;
288926deccbSFrançois Tigeot 	}
289926deccbSFrançois Tigeot 	bo->kptr = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem);
290926deccbSFrançois Tigeot 	if (ptr) {
291926deccbSFrançois Tigeot 		*ptr = bo->kptr;
292926deccbSFrançois Tigeot 	}
293926deccbSFrançois Tigeot 	radeon_bo_check_tiling(bo, 0, 0);
294926deccbSFrançois Tigeot 	return 0;
295926deccbSFrançois Tigeot }
296926deccbSFrançois Tigeot 
radeon_bo_kunmap(struct radeon_bo * bo)297926deccbSFrançois Tigeot void radeon_bo_kunmap(struct radeon_bo *bo)
298926deccbSFrançois Tigeot {
299926deccbSFrançois Tigeot 	if (bo->kptr == NULL)
300926deccbSFrançois Tigeot 		return;
301926deccbSFrançois Tigeot 	bo->kptr = NULL;
302926deccbSFrançois Tigeot 	radeon_bo_check_tiling(bo, 0, 0);
303926deccbSFrançois Tigeot 	ttm_bo_kunmap(&bo->kmap);
304926deccbSFrançois Tigeot }
305926deccbSFrançois Tigeot 
radeon_bo_ref(struct radeon_bo * bo)306c6f73aabSFrançois Tigeot struct radeon_bo *radeon_bo_ref(struct radeon_bo *bo)
307c6f73aabSFrançois Tigeot {
308c6f73aabSFrançois Tigeot 	if (bo == NULL)
309c6f73aabSFrançois Tigeot 		return NULL;
310c6f73aabSFrançois Tigeot 
311c6f73aabSFrançois Tigeot 	ttm_bo_reference(&bo->tbo);
312c6f73aabSFrançois Tigeot 	return bo;
313c6f73aabSFrançois Tigeot }
314c6f73aabSFrançois Tigeot 
radeon_bo_unref(struct radeon_bo ** bo)315926deccbSFrançois Tigeot void radeon_bo_unref(struct radeon_bo **bo)
316926deccbSFrançois Tigeot {
317926deccbSFrançois Tigeot 	struct ttm_buffer_object *tbo;
318926deccbSFrançois Tigeot 	struct radeon_device *rdev;
319926deccbSFrançois Tigeot 
3207dcf36dcSFrançois Tigeot 	if ((*bo) == NULL)
321926deccbSFrançois Tigeot 		return;
3227dcf36dcSFrançois Tigeot 	rdev = (*bo)->rdev;
3237dcf36dcSFrançois Tigeot 	tbo = &((*bo)->tbo);
324926deccbSFrançois Tigeot 	ttm_bo_unref(&tbo);
3257dcf36dcSFrançois Tigeot 	if (tbo == NULL)
3267dcf36dcSFrançois Tigeot 		*bo = NULL;
327926deccbSFrançois Tigeot }
328926deccbSFrançois Tigeot 
radeon_bo_pin_restricted(struct radeon_bo * bo,u32 domain,u64 max_offset,u64 * gpu_addr)329926deccbSFrançois Tigeot int radeon_bo_pin_restricted(struct radeon_bo *bo, u32 domain, u64 max_offset,
330926deccbSFrançois Tigeot 			     u64 *gpu_addr)
331926deccbSFrançois Tigeot {
332*9eb96077SSergey Zigachev 	struct ttm_operation_ctx ctx = { false, false };
333926deccbSFrançois Tigeot 	int r, i;
334926deccbSFrançois Tigeot 
335926deccbSFrançois Tigeot 	if (bo->pin_count) {
336926deccbSFrançois Tigeot 		bo->pin_count++;
337926deccbSFrançois Tigeot 		if (gpu_addr)
338926deccbSFrançois Tigeot 			*gpu_addr = radeon_bo_gpu_offset(bo);
339926deccbSFrançois Tigeot 
340926deccbSFrançois Tigeot 		if (max_offset != 0) {
341926deccbSFrançois Tigeot 			u64 domain_start;
342926deccbSFrançois Tigeot 
343926deccbSFrançois Tigeot 			if (domain == RADEON_GEM_DOMAIN_VRAM)
344926deccbSFrançois Tigeot 				domain_start = bo->rdev->mc.vram_start;
345926deccbSFrançois Tigeot 			else
346926deccbSFrançois Tigeot 				domain_start = bo->rdev->mc.gtt_start;
3477dcf36dcSFrançois Tigeot 			WARN_ON_ONCE(max_offset <
3487dcf36dcSFrançois Tigeot 				     (radeon_bo_gpu_offset(bo) - domain_start));
349926deccbSFrançois Tigeot 		}
350926deccbSFrançois Tigeot 
351926deccbSFrançois Tigeot 		return 0;
352926deccbSFrançois Tigeot 	}
353a85cb24fSFrançois Tigeot 	if (bo->prime_shared_count && domain == RADEON_GEM_DOMAIN_VRAM) {
354a85cb24fSFrançois Tigeot 		/* A BO shared as a dma-buf cannot be sensibly migrated to VRAM */
355a85cb24fSFrançois Tigeot 		return -EINVAL;
356a85cb24fSFrançois Tigeot 	}
357a85cb24fSFrançois Tigeot 
358926deccbSFrançois Tigeot 	radeon_ttm_placement_from_domain(bo, domain);
359591d5043SFrançois Tigeot 	for (i = 0; i < bo->placement.num_placement; i++) {
360926deccbSFrançois Tigeot 		/* force to pin into visible video ram */
361591d5043SFrançois Tigeot 		if ((bo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
362591d5043SFrançois Tigeot 		    !(bo->flags & RADEON_GEM_NO_CPU_ACCESS) &&
363591d5043SFrançois Tigeot 		    (!max_offset || max_offset > bo->rdev->mc.visible_vram_size))
364591d5043SFrançois Tigeot 			bo->placements[i].lpfn =
365591d5043SFrançois Tigeot 				bo->rdev->mc.visible_vram_size >> PAGE_SHIFT;
366591d5043SFrançois Tigeot 		else
367591d5043SFrançois Tigeot 			bo->placements[i].lpfn = max_offset >> PAGE_SHIFT;
368926deccbSFrançois Tigeot 
369591d5043SFrançois Tigeot 		bo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
370926deccbSFrançois Tigeot 	}
371591d5043SFrançois Tigeot 
372*9eb96077SSergey Zigachev 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
373926deccbSFrançois Tigeot 	if (likely(r == 0)) {
374926deccbSFrançois Tigeot 		bo->pin_count = 1;
375926deccbSFrançois Tigeot 		if (gpu_addr != NULL)
376926deccbSFrançois Tigeot 			*gpu_addr = radeon_bo_gpu_offset(bo);
377c6f73aabSFrançois Tigeot 		if (domain == RADEON_GEM_DOMAIN_VRAM)
378c6f73aabSFrançois Tigeot 			bo->rdev->vram_pin_size += radeon_bo_size(bo);
379c6f73aabSFrançois Tigeot 		else
380c6f73aabSFrançois Tigeot 			bo->rdev->gart_pin_size += radeon_bo_size(bo);
381c6f73aabSFrançois Tigeot 	} else {
382926deccbSFrançois Tigeot 		dev_err(bo->rdev->dev, "%p pin failed\n", bo);
383c6f73aabSFrançois Tigeot 	}
384926deccbSFrançois Tigeot 	return r;
385926deccbSFrançois Tigeot }
386926deccbSFrançois Tigeot 
radeon_bo_pin(struct radeon_bo * bo,u32 domain,u64 * gpu_addr)387926deccbSFrançois Tigeot int radeon_bo_pin(struct radeon_bo *bo, u32 domain, u64 *gpu_addr)
388926deccbSFrançois Tigeot {
389926deccbSFrançois Tigeot 	return radeon_bo_pin_restricted(bo, domain, 0, gpu_addr);
390926deccbSFrançois Tigeot }
391926deccbSFrançois Tigeot 
radeon_bo_unpin(struct radeon_bo * bo)392926deccbSFrançois Tigeot int radeon_bo_unpin(struct radeon_bo *bo)
393926deccbSFrançois Tigeot {
394*9eb96077SSergey Zigachev 	struct ttm_operation_ctx ctx = { false, false };
395926deccbSFrançois Tigeot 	int r, i;
396926deccbSFrançois Tigeot 
397926deccbSFrançois Tigeot 	if (!bo->pin_count) {
398926deccbSFrançois Tigeot 		dev_warn(bo->rdev->dev, "%p unpin not necessary\n", bo);
399926deccbSFrançois Tigeot 		return 0;
400926deccbSFrançois Tigeot 	}
401926deccbSFrançois Tigeot 	bo->pin_count--;
402926deccbSFrançois Tigeot 	if (bo->pin_count)
403926deccbSFrançois Tigeot 		return 0;
404591d5043SFrançois Tigeot 	for (i = 0; i < bo->placement.num_placement; i++) {
405591d5043SFrançois Tigeot 		bo->placements[i].lpfn = 0;
406591d5043SFrançois Tigeot 		bo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
407591d5043SFrançois Tigeot 	}
408*9eb96077SSergey Zigachev 	r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
409c6f73aabSFrançois Tigeot 	if (likely(r == 0)) {
410c6f73aabSFrançois Tigeot 		if (bo->tbo.mem.mem_type == TTM_PL_VRAM)
411c6f73aabSFrançois Tigeot 			bo->rdev->vram_pin_size -= radeon_bo_size(bo);
412c6f73aabSFrançois Tigeot 		else
413c6f73aabSFrançois Tigeot 			bo->rdev->gart_pin_size -= radeon_bo_size(bo);
414c6f73aabSFrançois Tigeot 	} else {
415926deccbSFrançois Tigeot 		dev_err(bo->rdev->dev, "%p validate failed for unpin\n", bo);
416c6f73aabSFrançois Tigeot 	}
417926deccbSFrançois Tigeot 	return r;
418926deccbSFrançois Tigeot }
419926deccbSFrançois Tigeot 
radeon_bo_evict_vram(struct radeon_device * rdev)420926deccbSFrançois Tigeot int radeon_bo_evict_vram(struct radeon_device *rdev)
421926deccbSFrançois Tigeot {
422926deccbSFrançois Tigeot 	/* late 2.6.33 fix IGP hibernate - we need pm ops to do this correct */
423926deccbSFrançois Tigeot 	if (0 && (rdev->flags & RADEON_IS_IGP)) {
424926deccbSFrançois Tigeot 		if (rdev->mc.igp_sideport_enabled == false)
425926deccbSFrançois Tigeot 			/* Useless to evict on IGP chips */
426926deccbSFrançois Tigeot 			return 0;
427926deccbSFrançois Tigeot 	}
428926deccbSFrançois Tigeot 	return ttm_bo_evict_mm(&rdev->mman.bdev, TTM_PL_VRAM);
429926deccbSFrançois Tigeot }
430926deccbSFrançois Tigeot 
radeon_bo_force_delete(struct radeon_device * rdev)431926deccbSFrançois Tigeot void radeon_bo_force_delete(struct radeon_device *rdev)
432926deccbSFrançois Tigeot {
433926deccbSFrançois Tigeot 	struct radeon_bo *bo, *n;
434926deccbSFrançois Tigeot 
435926deccbSFrançois Tigeot 	if (list_empty(&rdev->gem.objects)) {
436926deccbSFrançois Tigeot 		return;
437926deccbSFrançois Tigeot 	}
438926deccbSFrançois Tigeot 	dev_err(rdev->dev, "Userspace still has active objects !\n");
439926deccbSFrançois Tigeot 	list_for_each_entry_safe(bo, n, &rdev->gem.objects, list) {
440926deccbSFrançois Tigeot 		dev_err(rdev->dev, "%p %p %lu %lu force free\n",
441926deccbSFrançois Tigeot 			&bo->gem_base, bo, (unsigned long)bo->gem_base.size,
442926deccbSFrançois Tigeot 			*((unsigned long *)&bo->gem_base.refcount));
443fefad7a7SFrançois Tigeot 		mutex_lock(&bo->rdev->gem.mutex);
444926deccbSFrançois Tigeot 		list_del_init(&bo->list);
445fefad7a7SFrançois Tigeot 		mutex_unlock(&bo->rdev->gem.mutex);
446926deccbSFrançois Tigeot 		/* this should unref the ttm bo */
4473f2dd94aSFrançois Tigeot 		drm_gem_object_put_unlocked(&bo->gem_base);
448926deccbSFrançois Tigeot 	}
449926deccbSFrançois Tigeot }
450926deccbSFrançois Tigeot 
radeon_bo_init(struct radeon_device * rdev)451926deccbSFrançois Tigeot int radeon_bo_init(struct radeon_device *rdev)
452926deccbSFrançois Tigeot {
4531dedbd3bSFrançois Tigeot 	/* reserve PAT memory space to WC for VRAM */
4541dedbd3bSFrançois Tigeot 	arch_io_reserve_memtype_wc(rdev->mc.aper_base,
4551dedbd3bSFrançois Tigeot 				   rdev->mc.aper_size);
4561dedbd3bSFrançois Tigeot 
457926deccbSFrançois Tigeot 	/* Add an MTRR for the VRAM */
458f43cf1b1SMichael Neumann 	if (!rdev->fastfb_working) {
459c6f73aabSFrançois Tigeot 		rdev->mc.vram_mtrr = arch_phys_wc_add(rdev->mc.aper_base,
460c6f73aabSFrançois Tigeot 						      rdev->mc.aper_size);
461f43cf1b1SMichael Neumann 	}
462f77dbd6cSFrançois Tigeot 	DRM_INFO("Detected VRAM RAM=%lluM, BAR=%lluM\n",
4634cd92098Szrj 		rdev->mc.mc_vram_size >> 20,
464f77dbd6cSFrançois Tigeot 		(unsigned long long)rdev->mc.aper_size >> 20);
465926deccbSFrançois Tigeot 	DRM_INFO("RAM width %dbits %cDR\n",
466926deccbSFrançois Tigeot 			rdev->mc.vram_width, rdev->mc.vram_is_ddr ? 'D' : 'S');
467926deccbSFrançois Tigeot 	return radeon_ttm_init(rdev);
468926deccbSFrançois Tigeot }
469926deccbSFrançois Tigeot 
radeon_bo_fini(struct radeon_device * rdev)470926deccbSFrançois Tigeot void radeon_bo_fini(struct radeon_device *rdev)
471926deccbSFrançois Tigeot {
472926deccbSFrançois Tigeot 	radeon_ttm_fini(rdev);
47357e252bfSMichael Neumann 	arch_phys_wc_del(rdev->mc.vram_mtrr);
4741dedbd3bSFrançois Tigeot #if 0
4751dedbd3bSFrançois Tigeot 	arch_io_free_memtype_wc(rdev->mc.aper_base, rdev->mc.aper_size);
4761dedbd3bSFrançois Tigeot #endif
477926deccbSFrançois Tigeot }
478926deccbSFrançois Tigeot 
479c6f73aabSFrançois Tigeot /* Returns how many bytes TTM can move per IB.
480c6f73aabSFrançois Tigeot  */
radeon_bo_get_threshold_for_moves(struct radeon_device * rdev)481c6f73aabSFrançois Tigeot static u64 radeon_bo_get_threshold_for_moves(struct radeon_device *rdev)
482926deccbSFrançois Tigeot {
483c6f73aabSFrançois Tigeot 	u64 real_vram_size = rdev->mc.real_vram_size;
484c6f73aabSFrançois Tigeot 	u64 vram_usage = atomic64_read(&rdev->vram_usage);
485c6f73aabSFrançois Tigeot 
486c6f73aabSFrançois Tigeot 	/* This function is based on the current VRAM usage.
487c6f73aabSFrançois Tigeot 	 *
488c6f73aabSFrançois Tigeot 	 * - If all of VRAM is free, allow relocating the number of bytes that
489c6f73aabSFrançois Tigeot 	 *   is equal to 1/4 of the size of VRAM for this IB.
490c6f73aabSFrançois Tigeot 
491c6f73aabSFrançois Tigeot 	 * - If more than one half of VRAM is occupied, only allow relocating
492c6f73aabSFrançois Tigeot 	 *   1 MB of data for this IB.
493c6f73aabSFrançois Tigeot 	 *
494c6f73aabSFrançois Tigeot 	 * - From 0 to one half of used VRAM, the threshold decreases
495c6f73aabSFrançois Tigeot 	 *   linearly.
496c6f73aabSFrançois Tigeot 	 *         __________________
497c6f73aabSFrançois Tigeot 	 * 1/4 of -|\               |
498c6f73aabSFrançois Tigeot 	 * VRAM    | \              |
499c6f73aabSFrançois Tigeot 	 *         |  \             |
500c6f73aabSFrançois Tigeot 	 *         |   \            |
501c6f73aabSFrançois Tigeot 	 *         |    \           |
502c6f73aabSFrançois Tigeot 	 *         |     \          |
503c6f73aabSFrançois Tigeot 	 *         |      \         |
504c6f73aabSFrançois Tigeot 	 *         |       \________|1 MB
505c6f73aabSFrançois Tigeot 	 *         |----------------|
506c6f73aabSFrançois Tigeot 	 *    VRAM 0 %             100 %
507c6f73aabSFrançois Tigeot 	 *         used            used
508c6f73aabSFrançois Tigeot 	 *
509c6f73aabSFrançois Tigeot 	 * Note: It's a threshold, not a limit. The threshold must be crossed
510c6f73aabSFrançois Tigeot 	 * for buffer relocations to stop, so any buffer of an arbitrary size
511c6f73aabSFrançois Tigeot 	 * can be moved as long as the threshold isn't crossed before
512c6f73aabSFrançois Tigeot 	 * the relocation takes place. We don't want to disable buffer
513c6f73aabSFrançois Tigeot 	 * relocations completely.
514c6f73aabSFrançois Tigeot 	 *
515c6f73aabSFrançois Tigeot 	 * The idea is that buffers should be placed in VRAM at creation time
516c6f73aabSFrançois Tigeot 	 * and TTM should only do a minimum number of relocations during
517c6f73aabSFrançois Tigeot 	 * command submission. In practice, you need to submit at least
518c6f73aabSFrançois Tigeot 	 * a dozen IBs to move all buffers to VRAM if they are in GTT.
519c6f73aabSFrançois Tigeot 	 *
520c6f73aabSFrançois Tigeot 	 * Also, things can get pretty crazy under memory pressure and actual
521c6f73aabSFrançois Tigeot 	 * VRAM usage can change a lot, so playing safe even at 50% does
522c6f73aabSFrançois Tigeot 	 * consistently increase performance.
523c6f73aabSFrançois Tigeot 	 */
524c6f73aabSFrançois Tigeot 
525c6f73aabSFrançois Tigeot 	u64 half_vram = real_vram_size >> 1;
526c6f73aabSFrançois Tigeot 	u64 half_free_vram = vram_usage >= half_vram ? 0 : half_vram - vram_usage;
527c6f73aabSFrançois Tigeot 	u64 bytes_moved_threshold = half_free_vram >> 1;
528c6f73aabSFrançois Tigeot 	return max(bytes_moved_threshold, 1024*1024ull);
529926deccbSFrançois Tigeot }
530926deccbSFrançois Tigeot 
radeon_bo_list_validate(struct radeon_device * rdev,struct ww_acquire_ctx * ticket,struct list_head * head,int ring)531c6f73aabSFrançois Tigeot int radeon_bo_list_validate(struct radeon_device *rdev,
532c6f73aabSFrançois Tigeot 			    struct ww_acquire_ctx *ticket,
5334cd92098Szrj 			    struct list_head *head, int ring)
534926deccbSFrançois Tigeot {
535*9eb96077SSergey Zigachev 	struct ttm_operation_ctx ctx = { true, false };
5367dcf36dcSFrançois Tigeot 	struct radeon_bo_list *lobj;
5377dcf36dcSFrançois Tigeot 	struct list_head duplicates;
538926deccbSFrançois Tigeot 	int r;
539c6f73aabSFrançois Tigeot 	u64 bytes_moved = 0, initial_bytes_moved;
540c6f73aabSFrançois Tigeot 	u64 bytes_moved_threshold = radeon_bo_get_threshold_for_moves(rdev);
541926deccbSFrançois Tigeot 
5427dcf36dcSFrançois Tigeot 	INIT_LIST_HEAD(&duplicates);
5437dcf36dcSFrançois Tigeot 	r = ttm_eu_reserve_buffers(ticket, head, true, &duplicates);
544926deccbSFrançois Tigeot 	if (unlikely(r != 0)) {
545926deccbSFrançois Tigeot 		return r;
546926deccbSFrançois Tigeot 	}
547c6f73aabSFrançois Tigeot 
548926deccbSFrançois Tigeot 	list_for_each_entry(lobj, head, tv.head) {
5497dcf36dcSFrançois Tigeot 		struct radeon_bo *bo = lobj->robj;
550926deccbSFrançois Tigeot 		if (!bo->pin_count) {
5513f2dd94aSFrançois Tigeot 			u32 domain = lobj->preferred_domains;
552591d5043SFrançois Tigeot 			u32 allowed = lobj->allowed_domains;
553c6f73aabSFrançois Tigeot 			u32 current_domain =
554c6f73aabSFrançois Tigeot 				radeon_mem_type_to_domain(bo->tbo.mem.mem_type);
555c6f73aabSFrançois Tigeot 
556c6f73aabSFrançois Tigeot 			/* Check if this buffer will be moved and don't move it
557c6f73aabSFrançois Tigeot 			 * if we have moved too many buffers for this IB already.
558c6f73aabSFrançois Tigeot 			 *
559c6f73aabSFrançois Tigeot 			 * Note that this allows moving at least one buffer of
560c6f73aabSFrançois Tigeot 			 * any size, because it doesn't take the current "bo"
561c6f73aabSFrançois Tigeot 			 * into account. We don't want to disallow buffer moves
562c6f73aabSFrançois Tigeot 			 * completely.
563c6f73aabSFrançois Tigeot 			 */
564591d5043SFrançois Tigeot 			if ((allowed & current_domain) != 0 &&
565c6f73aabSFrançois Tigeot 			    (domain & current_domain) == 0 && /* will be moved */
566c6f73aabSFrançois Tigeot 			    bytes_moved > bytes_moved_threshold) {
567c6f73aabSFrançois Tigeot 				/* don't move it */
568c6f73aabSFrançois Tigeot 				domain = current_domain;
569c6f73aabSFrançois Tigeot 			}
570926deccbSFrançois Tigeot 
571926deccbSFrançois Tigeot 		retry:
572926deccbSFrançois Tigeot 			radeon_ttm_placement_from_domain(bo, domain);
573f43cf1b1SMichael Neumann 			if (ring == R600_RING_TYPE_UVD_INDEX)
574591d5043SFrançois Tigeot 				radeon_uvd_force_into_uvd_segment(bo, allowed);
575c6f73aabSFrançois Tigeot 
576c6f73aabSFrançois Tigeot 			initial_bytes_moved = atomic64_read(&rdev->num_bytes_moved);
577*9eb96077SSergey Zigachev 			r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
578c6f73aabSFrançois Tigeot 			bytes_moved += atomic64_read(&rdev->num_bytes_moved) -
579c6f73aabSFrançois Tigeot 				       initial_bytes_moved;
580c6f73aabSFrançois Tigeot 
581926deccbSFrançois Tigeot 			if (unlikely(r)) {
582c6f73aabSFrançois Tigeot 				if (r != -ERESTARTSYS &&
583c6f73aabSFrançois Tigeot 				    domain != lobj->allowed_domains) {
584c6f73aabSFrançois Tigeot 					domain = lobj->allowed_domains;
585926deccbSFrançois Tigeot 					goto retry;
586926deccbSFrançois Tigeot 				}
5875fc68e26SFrançois Tigeot 				ttm_eu_backoff_reservation(ticket, head);
588926deccbSFrançois Tigeot 				return r;
589926deccbSFrançois Tigeot 			}
590926deccbSFrançois Tigeot 		}
591926deccbSFrançois Tigeot 		lobj->gpu_offset = radeon_bo_gpu_offset(bo);
592926deccbSFrançois Tigeot 		lobj->tiling_flags = bo->tiling_flags;
593926deccbSFrançois Tigeot 	}
5947dcf36dcSFrançois Tigeot 
5957dcf36dcSFrançois Tigeot 	list_for_each_entry(lobj, &duplicates, tv.head) {
5967dcf36dcSFrançois Tigeot 		lobj->gpu_offset = radeon_bo_gpu_offset(lobj->robj);
5977dcf36dcSFrançois Tigeot 		lobj->tiling_flags = lobj->robj->tiling_flags;
5987dcf36dcSFrançois Tigeot 	}
5997dcf36dcSFrançois Tigeot 
600926deccbSFrançois Tigeot 	return 0;
601926deccbSFrançois Tigeot }
602926deccbSFrançois Tigeot 
radeon_bo_get_surface_reg(struct radeon_bo * bo)603926deccbSFrançois Tigeot int radeon_bo_get_surface_reg(struct radeon_bo *bo)
604926deccbSFrançois Tigeot {
605926deccbSFrançois Tigeot 	struct radeon_device *rdev = bo->rdev;
606926deccbSFrançois Tigeot 	struct radeon_surface_reg *reg;
607926deccbSFrançois Tigeot 	struct radeon_bo *old_object;
608926deccbSFrançois Tigeot 	int steal;
609926deccbSFrançois Tigeot 	int i;
610926deccbSFrançois Tigeot 
6117dcf36dcSFrançois Tigeot 	lockdep_assert_held(&bo->tbo.resv->lock.base);
6127dcf36dcSFrançois Tigeot 
613926deccbSFrançois Tigeot 	if (!bo->tiling_flags)
614926deccbSFrançois Tigeot 		return 0;
615926deccbSFrançois Tigeot 
616926deccbSFrançois Tigeot 	if (bo->surface_reg >= 0) {
617926deccbSFrançois Tigeot 		reg = &rdev->surface_regs[bo->surface_reg];
618926deccbSFrançois Tigeot 		i = bo->surface_reg;
619926deccbSFrançois Tigeot 		goto out;
620926deccbSFrançois Tigeot 	}
621926deccbSFrançois Tigeot 
622926deccbSFrançois Tigeot 	steal = -1;
623926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_GEM_MAX_SURFACES; i++) {
624926deccbSFrançois Tigeot 
625926deccbSFrançois Tigeot 		reg = &rdev->surface_regs[i];
626926deccbSFrançois Tigeot 		if (!reg->bo)
627926deccbSFrançois Tigeot 			break;
628926deccbSFrançois Tigeot 
629926deccbSFrançois Tigeot 		old_object = reg->bo;
630926deccbSFrançois Tigeot 		if (old_object->pin_count == 0)
631926deccbSFrançois Tigeot 			steal = i;
632926deccbSFrançois Tigeot 	}
633926deccbSFrançois Tigeot 
634926deccbSFrançois Tigeot 	/* if we are all out */
635926deccbSFrançois Tigeot 	if (i == RADEON_GEM_MAX_SURFACES) {
636926deccbSFrançois Tigeot 		if (steal == -1)
637926deccbSFrançois Tigeot 			return -ENOMEM;
638926deccbSFrançois Tigeot 		/* find someone with a surface reg and nuke their BO */
639926deccbSFrançois Tigeot 		reg = &rdev->surface_regs[steal];
640926deccbSFrançois Tigeot 		old_object = reg->bo;
641926deccbSFrançois Tigeot 		/* blow away the mapping */
642926deccbSFrançois Tigeot 		DRM_DEBUG("stealing surface reg %d from %p\n", steal, old_object);
643926deccbSFrançois Tigeot 		ttm_bo_unmap_virtual(&old_object->tbo);
644926deccbSFrançois Tigeot 		old_object->surface_reg = -1;
645926deccbSFrançois Tigeot 		i = steal;
646926deccbSFrançois Tigeot 	}
647926deccbSFrançois Tigeot 
648926deccbSFrançois Tigeot 	bo->surface_reg = i;
649926deccbSFrançois Tigeot 	reg->bo = bo;
650926deccbSFrançois Tigeot 
651926deccbSFrançois Tigeot out:
652926deccbSFrançois Tigeot 	radeon_set_surface_reg(rdev, i, bo->tiling_flags, bo->pitch,
653926deccbSFrançois Tigeot 			       bo->tbo.mem.start << PAGE_SHIFT,
654926deccbSFrançois Tigeot 			       bo->tbo.num_pages << PAGE_SHIFT);
655926deccbSFrançois Tigeot 	return 0;
656926deccbSFrançois Tigeot }
657926deccbSFrançois Tigeot 
radeon_bo_clear_surface_reg(struct radeon_bo * bo)658926deccbSFrançois Tigeot static void radeon_bo_clear_surface_reg(struct radeon_bo *bo)
659926deccbSFrançois Tigeot {
660926deccbSFrançois Tigeot 	struct radeon_device *rdev = bo->rdev;
661926deccbSFrançois Tigeot 	struct radeon_surface_reg *reg;
662926deccbSFrançois Tigeot 
663926deccbSFrançois Tigeot 	if (bo->surface_reg == -1)
664926deccbSFrançois Tigeot 		return;
665926deccbSFrançois Tigeot 
666926deccbSFrançois Tigeot 	reg = &rdev->surface_regs[bo->surface_reg];
667926deccbSFrançois Tigeot 	radeon_clear_surface_reg(rdev, bo->surface_reg);
668926deccbSFrançois Tigeot 
669926deccbSFrançois Tigeot 	reg->bo = NULL;
670926deccbSFrançois Tigeot 	bo->surface_reg = -1;
671926deccbSFrançois Tigeot }
672926deccbSFrançois Tigeot 
radeon_bo_set_tiling_flags(struct radeon_bo * bo,uint32_t tiling_flags,uint32_t pitch)673926deccbSFrançois Tigeot int radeon_bo_set_tiling_flags(struct radeon_bo *bo,
674926deccbSFrançois Tigeot 				uint32_t tiling_flags, uint32_t pitch)
675926deccbSFrançois Tigeot {
676926deccbSFrançois Tigeot 	struct radeon_device *rdev = bo->rdev;
677926deccbSFrançois Tigeot 	int r;
678926deccbSFrançois Tigeot 
679926deccbSFrançois Tigeot 	if (rdev->family >= CHIP_CEDAR) {
680926deccbSFrançois Tigeot 		unsigned bankw, bankh, mtaspect, tilesplit, stilesplit;
681926deccbSFrançois Tigeot 
682926deccbSFrançois Tigeot 		bankw = (tiling_flags >> RADEON_TILING_EG_BANKW_SHIFT) & RADEON_TILING_EG_BANKW_MASK;
683926deccbSFrançois Tigeot 		bankh = (tiling_flags >> RADEON_TILING_EG_BANKH_SHIFT) & RADEON_TILING_EG_BANKH_MASK;
684926deccbSFrançois Tigeot 		mtaspect = (tiling_flags >> RADEON_TILING_EG_MACRO_TILE_ASPECT_SHIFT) & RADEON_TILING_EG_MACRO_TILE_ASPECT_MASK;
685926deccbSFrançois Tigeot 		tilesplit = (tiling_flags >> RADEON_TILING_EG_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_TILE_SPLIT_MASK;
686926deccbSFrançois Tigeot 		stilesplit = (tiling_flags >> RADEON_TILING_EG_STENCIL_TILE_SPLIT_SHIFT) & RADEON_TILING_EG_STENCIL_TILE_SPLIT_MASK;
687926deccbSFrançois Tigeot 		switch (bankw) {
688926deccbSFrançois Tigeot 		case 0:
689926deccbSFrançois Tigeot 		case 1:
690926deccbSFrançois Tigeot 		case 2:
691926deccbSFrançois Tigeot 		case 4:
692926deccbSFrançois Tigeot 		case 8:
693926deccbSFrançois Tigeot 			break;
694926deccbSFrançois Tigeot 		default:
695926deccbSFrançois Tigeot 			return -EINVAL;
696926deccbSFrançois Tigeot 		}
697926deccbSFrançois Tigeot 		switch (bankh) {
698926deccbSFrançois Tigeot 		case 0:
699926deccbSFrançois Tigeot 		case 1:
700926deccbSFrançois Tigeot 		case 2:
701926deccbSFrançois Tigeot 		case 4:
702926deccbSFrançois Tigeot 		case 8:
703926deccbSFrançois Tigeot 			break;
704926deccbSFrançois Tigeot 		default:
705926deccbSFrançois Tigeot 			return -EINVAL;
706926deccbSFrançois Tigeot 		}
707926deccbSFrançois Tigeot 		switch (mtaspect) {
708926deccbSFrançois Tigeot 		case 0:
709926deccbSFrançois Tigeot 		case 1:
710926deccbSFrançois Tigeot 		case 2:
711926deccbSFrançois Tigeot 		case 4:
712926deccbSFrançois Tigeot 		case 8:
713926deccbSFrançois Tigeot 			break;
714926deccbSFrançois Tigeot 		default:
715926deccbSFrançois Tigeot 			return -EINVAL;
716926deccbSFrançois Tigeot 		}
717926deccbSFrançois Tigeot 		if (tilesplit > 6) {
718926deccbSFrançois Tigeot 			return -EINVAL;
719926deccbSFrançois Tigeot 		}
720926deccbSFrançois Tigeot 		if (stilesplit > 6) {
721926deccbSFrançois Tigeot 			return -EINVAL;
722926deccbSFrançois Tigeot 		}
723926deccbSFrançois Tigeot 	}
724926deccbSFrançois Tigeot 	r = radeon_bo_reserve(bo, false);
725926deccbSFrançois Tigeot 	if (unlikely(r != 0))
726926deccbSFrançois Tigeot 		return r;
727926deccbSFrançois Tigeot 	bo->tiling_flags = tiling_flags;
728926deccbSFrançois Tigeot 	bo->pitch = pitch;
729926deccbSFrançois Tigeot 	radeon_bo_unreserve(bo);
730926deccbSFrançois Tigeot 	return 0;
731926deccbSFrançois Tigeot }
732926deccbSFrançois Tigeot 
radeon_bo_get_tiling_flags(struct radeon_bo * bo,uint32_t * tiling_flags,uint32_t * pitch)733926deccbSFrançois Tigeot void radeon_bo_get_tiling_flags(struct radeon_bo *bo,
734926deccbSFrançois Tigeot 				uint32_t *tiling_flags,
735926deccbSFrançois Tigeot 				uint32_t *pitch)
736926deccbSFrançois Tigeot {
7377dcf36dcSFrançois Tigeot 	lockdep_assert_held(&bo->tbo.resv->lock.base);
7387dcf36dcSFrançois Tigeot 
739926deccbSFrançois Tigeot 	if (tiling_flags)
740926deccbSFrançois Tigeot 		*tiling_flags = bo->tiling_flags;
741926deccbSFrançois Tigeot 	if (pitch)
742926deccbSFrançois Tigeot 		*pitch = bo->pitch;
743926deccbSFrançois Tigeot }
744926deccbSFrançois Tigeot 
radeon_bo_check_tiling(struct radeon_bo * bo,bool has_moved,bool force_drop)745926deccbSFrançois Tigeot int radeon_bo_check_tiling(struct radeon_bo *bo, bool has_moved,
746926deccbSFrançois Tigeot 				bool force_drop)
747926deccbSFrançois Tigeot {
7487dcf36dcSFrançois Tigeot 	if (!force_drop)
7497dcf36dcSFrançois Tigeot 		lockdep_assert_held(&bo->tbo.resv->lock.base);
750926deccbSFrançois Tigeot 
751926deccbSFrançois Tigeot 	if (!(bo->tiling_flags & RADEON_TILING_SURFACE))
752926deccbSFrançois Tigeot 		return 0;
753926deccbSFrançois Tigeot 
754926deccbSFrançois Tigeot 	if (force_drop) {
755926deccbSFrançois Tigeot 		radeon_bo_clear_surface_reg(bo);
756926deccbSFrançois Tigeot 		return 0;
757926deccbSFrançois Tigeot 	}
758926deccbSFrançois Tigeot 
759926deccbSFrançois Tigeot 	if (bo->tbo.mem.mem_type != TTM_PL_VRAM) {
760926deccbSFrançois Tigeot 		if (!has_moved)
761926deccbSFrançois Tigeot 			return 0;
762926deccbSFrançois Tigeot 
763926deccbSFrançois Tigeot 		if (bo->surface_reg >= 0)
764926deccbSFrançois Tigeot 			radeon_bo_clear_surface_reg(bo);
765926deccbSFrançois Tigeot 		return 0;
766926deccbSFrançois Tigeot 	}
767926deccbSFrançois Tigeot 
768926deccbSFrançois Tigeot 	if ((bo->surface_reg >= 0) && !has_moved)
769926deccbSFrançois Tigeot 		return 0;
770926deccbSFrançois Tigeot 
771926deccbSFrançois Tigeot 	return radeon_bo_get_surface_reg(bo);
772926deccbSFrançois Tigeot }
773926deccbSFrançois Tigeot 
radeon_bo_move_notify(struct ttm_buffer_object * bo,bool evict,struct ttm_mem_reg * new_mem)774926deccbSFrançois Tigeot void radeon_bo_move_notify(struct ttm_buffer_object *bo,
775a85cb24fSFrançois Tigeot 			   bool evict,
776c6f73aabSFrançois Tigeot 			   struct ttm_mem_reg *new_mem)
777926deccbSFrançois Tigeot {
778926deccbSFrançois Tigeot 	struct radeon_bo *rbo;
779c6f73aabSFrançois Tigeot 
780926deccbSFrançois Tigeot 	if (!radeon_ttm_bo_is_radeon_bo(bo))
781926deccbSFrançois Tigeot 		return;
782c6f73aabSFrançois Tigeot 
783926deccbSFrançois Tigeot 	rbo = container_of(bo, struct radeon_bo, tbo);
784926deccbSFrançois Tigeot 	radeon_bo_check_tiling(rbo, 0, 1);
785926deccbSFrançois Tigeot 	radeon_vm_bo_invalidate(rbo->rdev, rbo);
786c6f73aabSFrançois Tigeot 
787c6f73aabSFrançois Tigeot 	/* update statistics */
788c6f73aabSFrançois Tigeot 	if (!new_mem)
789c6f73aabSFrançois Tigeot 		return;
790c6f73aabSFrançois Tigeot 
791c6f73aabSFrançois Tigeot 	radeon_update_memory_usage(rbo, bo->mem.mem_type, -1);
792c6f73aabSFrançois Tigeot 	radeon_update_memory_usage(rbo, new_mem->mem_type, 1);
793926deccbSFrançois Tigeot }
794926deccbSFrançois Tigeot 
radeon_bo_fault_reserve_notify(struct ttm_buffer_object * bo)795926deccbSFrançois Tigeot int radeon_bo_fault_reserve_notify(struct ttm_buffer_object *bo)
796926deccbSFrançois Tigeot {
797*9eb96077SSergey Zigachev 	struct ttm_operation_ctx ctx = { false, false };
798926deccbSFrançois Tigeot 	struct radeon_device *rdev;
799926deccbSFrançois Tigeot 	struct radeon_bo *rbo;
8007dcf36dcSFrançois Tigeot 	unsigned long offset, size, lpfn;
8017dcf36dcSFrançois Tigeot 	int i, r;
802926deccbSFrançois Tigeot 
803926deccbSFrançois Tigeot 	if (!radeon_ttm_bo_is_radeon_bo(bo))
804926deccbSFrançois Tigeot 		return 0;
805926deccbSFrançois Tigeot 	rbo = container_of(bo, struct radeon_bo, tbo);
806926deccbSFrançois Tigeot 	radeon_bo_check_tiling(rbo, 0, 0);
807926deccbSFrançois Tigeot 	rdev = rbo->rdev;
808c6f73aabSFrançois Tigeot 	if (bo->mem.mem_type != TTM_PL_VRAM)
809c6f73aabSFrançois Tigeot 		return 0;
810c6f73aabSFrançois Tigeot 
811926deccbSFrançois Tigeot 	size = bo->mem.num_pages << PAGE_SHIFT;
812926deccbSFrançois Tigeot 	offset = bo->mem.start << PAGE_SHIFT;
813c6f73aabSFrançois Tigeot 	if ((offset + size) <= rdev->mc.visible_vram_size)
814c6f73aabSFrançois Tigeot 		return 0;
815c6f73aabSFrançois Tigeot 
816d78d3a22SFrançois Tigeot 	/* Can't move a pinned BO to visible VRAM */
817d78d3a22SFrançois Tigeot 	if (rbo->pin_count > 0)
818d78d3a22SFrançois Tigeot 		return -EINVAL;
819d78d3a22SFrançois Tigeot 
820926deccbSFrançois Tigeot 	/* hurrah the memory is not visible ! */
821926deccbSFrançois Tigeot 	radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_VRAM);
8227dcf36dcSFrançois Tigeot 	lpfn =	rdev->mc.visible_vram_size >> PAGE_SHIFT;
8237dcf36dcSFrançois Tigeot 	for (i = 0; i < rbo->placement.num_placement; i++) {
8247dcf36dcSFrançois Tigeot 		/* Force into visible VRAM */
8257dcf36dcSFrançois Tigeot 		if ((rbo->placements[i].flags & TTM_PL_FLAG_VRAM) &&
8267dcf36dcSFrançois Tigeot 		    (!rbo->placements[i].lpfn || rbo->placements[i].lpfn > lpfn))
8277dcf36dcSFrançois Tigeot 			rbo->placements[i].lpfn = lpfn;
8287dcf36dcSFrançois Tigeot 	}
829*9eb96077SSergey Zigachev 	r = ttm_bo_validate(bo, &rbo->placement, &ctx);
830c6f73aabSFrançois Tigeot 	if (unlikely(r == -ENOMEM)) {
831c6f73aabSFrançois Tigeot 		radeon_ttm_placement_from_domain(rbo, RADEON_GEM_DOMAIN_GTT);
832*9eb96077SSergey Zigachev 		return ttm_bo_validate(bo, &rbo->placement, &ctx);
833c6f73aabSFrançois Tigeot 	} else if (unlikely(r != 0)) {
834926deccbSFrançois Tigeot 		return r;
835c6f73aabSFrançois Tigeot 	}
836c6f73aabSFrançois Tigeot 
837926deccbSFrançois Tigeot 	offset = bo->mem.start << PAGE_SHIFT;
838c6f73aabSFrançois Tigeot 	/* this should never happen */
839926deccbSFrançois Tigeot 	if ((offset + size) > rdev->mc.visible_vram_size)
840926deccbSFrançois Tigeot 		return -EINVAL;
841c6f73aabSFrançois Tigeot 
842926deccbSFrançois Tigeot 	return 0;
843926deccbSFrançois Tigeot }
844926deccbSFrançois Tigeot 
radeon_bo_wait(struct radeon_bo * bo,u32 * mem_type,bool no_wait)845926deccbSFrançois Tigeot int radeon_bo_wait(struct radeon_bo *bo, u32 *mem_type, bool no_wait)
846926deccbSFrançois Tigeot {
847926deccbSFrançois Tigeot 	int r;
848926deccbSFrançois Tigeot 
849d78d3a22SFrançois Tigeot 	r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL);
850926deccbSFrançois Tigeot 	if (unlikely(r != 0))
851926deccbSFrançois Tigeot 		return r;
852926deccbSFrançois Tigeot 	if (mem_type)
853926deccbSFrançois Tigeot 		*mem_type = bo->tbo.mem.mem_type;
8541cfef1a5SFrançois Tigeot 
855d78d3a22SFrançois Tigeot 	r = ttm_bo_wait(&bo->tbo, true, no_wait);
856926deccbSFrançois Tigeot 	ttm_bo_unreserve(&bo->tbo);
857926deccbSFrançois Tigeot 	return r;
858926deccbSFrançois Tigeot }
859ee479021SImre Vadász 
860ee479021SImre Vadász /**
8617dcf36dcSFrançois Tigeot  * radeon_bo_fence - add fence to buffer object
862ee479021SImre Vadász  *
8637dcf36dcSFrançois Tigeot  * @bo: buffer object in question
8647dcf36dcSFrançois Tigeot  * @fence: fence to add
8657dcf36dcSFrançois Tigeot  * @shared: true if fence should be added shared
8667dcf36dcSFrançois Tigeot  *
867ee479021SImre Vadász  */
radeon_bo_fence(struct radeon_bo * bo,struct radeon_fence * fence,bool shared)8687dcf36dcSFrançois Tigeot void radeon_bo_fence(struct radeon_bo *bo, struct radeon_fence *fence,
8697dcf36dcSFrançois Tigeot 		     bool shared)
870ee479021SImre Vadász {
8717dcf36dcSFrançois Tigeot 	struct reservation_object *resv = bo->tbo.resv;
872ee479021SImre Vadász 
8737dcf36dcSFrançois Tigeot 	if (shared)
8747dcf36dcSFrançois Tigeot 		reservation_object_add_shared_fence(resv, &fence->base);
8757dcf36dcSFrançois Tigeot 	else
8767dcf36dcSFrançois Tigeot 		reservation_object_add_excl_fence(resv, &fence->base);
877ee479021SImre Vadász }
878