xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_ib.c (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
1f28be810SChristian König /*
2f28be810SChristian König  * Copyright 2008 Advanced Micro Devices, Inc.
3f28be810SChristian König  * Copyright 2008 Red Hat Inc.
4f28be810SChristian König  * Copyright 2009 Jerome Glisse.
5f28be810SChristian König  *
6f28be810SChristian König  * Permission is hereby granted, free of charge, to any person obtaining a
7f28be810SChristian König  * copy of this software and associated documentation files (the "Software"),
8f28be810SChristian König  * to deal in the Software without restriction, including without limitation
9f28be810SChristian König  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10f28be810SChristian König  * and/or sell copies of the Software, and to permit persons to whom the
11f28be810SChristian König  * Software is furnished to do so, subject to the following conditions:
12f28be810SChristian König  *
13f28be810SChristian König  * The above copyright notice and this permission notice shall be included in
14f28be810SChristian König  * all copies or substantial portions of the Software.
15f28be810SChristian König  *
16f28be810SChristian König  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17f28be810SChristian König  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18f28be810SChristian König  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19f28be810SChristian König  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20f28be810SChristian König  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21f28be810SChristian König  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22f28be810SChristian König  * OTHER DEALINGS IN THE SOFTWARE.
23f28be810SChristian König  *
24f28be810SChristian König  * Authors: Dave Airlie
25f28be810SChristian König  *          Alex Deucher
26f28be810SChristian König  *          Jerome Glisse
27f28be810SChristian König  *          Christian König
28f28be810SChristian König  */
29f9183127SSam Ravnborg 
30f9183127SSam Ravnborg #include <drm/drm_file.h>
31f9183127SSam Ravnborg 
32f28be810SChristian König #include "radeon.h"
33f28be810SChristian König 
34f28be810SChristian König /*
35f28be810SChristian König  * IB
36f28be810SChristian König  * IBs (Indirect Buffers) and areas of GPU accessible memory where
37f28be810SChristian König  * commands are stored.  You can put a pointer to the IB in the
38f28be810SChristian König  * command ring and the hw will fetch the commands from the IB
39f28be810SChristian König  * and execute them.  Generally userspace acceleration drivers
40f28be810SChristian König  * produce command buffers which are send to the kernel and
41f28be810SChristian König  * put in IBs for execution by the requested ring.
42f28be810SChristian König  */
43*5b54d679SNirmoy Das static void radeon_debugfs_sa_init(struct radeon_device *rdev);
44f28be810SChristian König 
45f28be810SChristian König /**
46f28be810SChristian König  * radeon_ib_get - request an IB (Indirect Buffer)
47f28be810SChristian König  *
48f28be810SChristian König  * @rdev: radeon_device pointer
49f28be810SChristian König  * @ring: ring index the IB is associated with
507c1f03e0SLee Jones  * @vm: requested vm
51f28be810SChristian König  * @ib: IB object returned
52f28be810SChristian König  * @size: requested IB size
53f28be810SChristian König  *
54f28be810SChristian König  * Request an IB (all asics).  IBs are allocated using the
55f28be810SChristian König  * suballocator.
56f28be810SChristian König  * Returns 0 on success, error on failure.
57f28be810SChristian König  */
radeon_ib_get(struct radeon_device * rdev,int ring,struct radeon_ib * ib,struct radeon_vm * vm,unsigned size)58f28be810SChristian König int radeon_ib_get(struct radeon_device *rdev, int ring,
59f28be810SChristian König 		  struct radeon_ib *ib, struct radeon_vm *vm,
60f28be810SChristian König 		  unsigned size)
61f28be810SChristian König {
62f28be810SChristian König 	int r;
63f28be810SChristian König 
64f28be810SChristian König 	r = radeon_sa_bo_new(&rdev->ring_tmp_bo, &ib->sa_bo, size, 256);
65f28be810SChristian König 	if (r) {
66f28be810SChristian König 		dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
67f28be810SChristian König 		return r;
68f28be810SChristian König 	}
69f28be810SChristian König 
70975700d2SChristian König 	radeon_sync_create(&ib->sync);
71f28be810SChristian König 
72f28be810SChristian König 	ib->ring = ring;
73f28be810SChristian König 	ib->fence = NULL;
74f28be810SChristian König 	ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
75f28be810SChristian König 	ib->vm = vm;
76f28be810SChristian König 	if (vm) {
77f28be810SChristian König 		/* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address
78f28be810SChristian König 		 * space and soffset is the offset inside the pool bo
79f28be810SChristian König 		 */
80f28be810SChristian König 		ib->gpu_addr = drm_suballoc_soffset(ib->sa_bo) + RADEON_VA_IB_OFFSET;
81f28be810SChristian König 	} else {
82f28be810SChristian König 		ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
83f28be810SChristian König 	}
84f28be810SChristian König 	ib->is_const_ib = false;
85f28be810SChristian König 
86f28be810SChristian König 	return 0;
87f28be810SChristian König }
88f28be810SChristian König 
89f28be810SChristian König /**
90f28be810SChristian König  * radeon_ib_free - free an IB (Indirect Buffer)
91f28be810SChristian König  *
92f28be810SChristian König  * @rdev: radeon_device pointer
93f28be810SChristian König  * @ib: IB object to free
94f28be810SChristian König  *
95f28be810SChristian König  * Free an IB (all asics).
96f28be810SChristian König  */
radeon_ib_free(struct radeon_device * rdev,struct radeon_ib * ib)97f28be810SChristian König void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
98f28be810SChristian König {
99975700d2SChristian König 	radeon_sync_free(rdev, &ib->sync, ib->fence);
100f28be810SChristian König 	radeon_sa_bo_free(&ib->sa_bo, ib->fence);
101f28be810SChristian König 	radeon_fence_unref(&ib->fence);
102f28be810SChristian König }
103f28be810SChristian König 
104f28be810SChristian König /**
105f28be810SChristian König  * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring
106f28be810SChristian König  *
107f28be810SChristian König  * @rdev: radeon_device pointer
108f28be810SChristian König  * @ib: IB object to schedule
109f28be810SChristian König  * @const_ib: Const IB to schedule (SI only)
1101538a9e0SMichel Dänzer  * @hdp_flush: Whether or not to perform an HDP cache flush
111f28be810SChristian König  *
112f28be810SChristian König  * Schedule an IB on the associated ring (all asics).
113f28be810SChristian König  * Returns 0 on success, error on failure.
114f28be810SChristian König  *
115f28be810SChristian König  * On SI, there are two parallel engines fed from the primary ring,
116f28be810SChristian König  * the CE (Constant Engine) and the DE (Drawing Engine).  Since
117f28be810SChristian König  * resource descriptors have moved to memory, the CE allows you to
118f28be810SChristian König  * prime the caches while the DE is updating register state so that
119f28be810SChristian König  * the resource descriptors will be already in cache when the draw is
120f28be810SChristian König  * processed.  To accomplish this, the userspace driver submits two
121f28be810SChristian König  * IBs, one for the CE and one for the DE.  If there is a CE IB (called
122f28be810SChristian König  * a CONST_IB), it will be put on the ring prior to the DE IB.  Prior
123f28be810SChristian König  * to SI there was just a DE IB.
124f28be810SChristian König  */
radeon_ib_schedule(struct radeon_device * rdev,struct radeon_ib * ib,struct radeon_ib * const_ib,bool hdp_flush)125f28be810SChristian König int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
1261538a9e0SMichel Dänzer 		       struct radeon_ib *const_ib, bool hdp_flush)
127f28be810SChristian König {
128f28be810SChristian König 	struct radeon_ring *ring = &rdev->ring[ib->ring];
129f28be810SChristian König 	int r = 0;
130f28be810SChristian König 
131f28be810SChristian König 	if (!ib->length_dw || !ring->ready) {
132f28be810SChristian König 		/* TODO: Nothings in the ib we should report. */
133f28be810SChristian König 		dev_err(rdev->dev, "couldn't schedule ib\n");
134f28be810SChristian König 		return -EINVAL;
135f28be810SChristian König 	}
136f28be810SChristian König 
137f28be810SChristian König 	/* 64 dwords should be enough for fence too */
138f28be810SChristian König 	r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8);
139f28be810SChristian König 	if (r) {
140f28be810SChristian König 		dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
141f28be810SChristian König 		return r;
142f28be810SChristian König 	}
143f28be810SChristian König 
144f28be810SChristian König 	/* grab a vm id if necessary */
145f28be810SChristian König 	if (ib->vm) {
146f28be810SChristian König 		struct radeon_fence *vm_id_fence;
147f28be810SChristian König 		vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring);
148975700d2SChristian König 		radeon_sync_fence(&ib->sync, vm_id_fence);
149f28be810SChristian König 	}
150f28be810SChristian König 
151f28be810SChristian König 	/* sync with other rings */
152975700d2SChristian König 	r = radeon_sync_rings(rdev, &ib->sync, ib->ring);
153f28be810SChristian König 	if (r) {
154f28be810SChristian König 		dev_err(rdev->dev, "failed to sync rings (%d)\n", r);
155f28be810SChristian König 		radeon_ring_unlock_undo(rdev, ring);
156f28be810SChristian König 		return r;
157f28be810SChristian König 	}
158f28be810SChristian König 
159f28be810SChristian König 	if (ib->vm)
160ad1a58a4SChristian König 		radeon_vm_flush(rdev, ib->vm, ib->ring,
161ad1a58a4SChristian König 				ib->sync.last_vm_update);
162f28be810SChristian König 
163f28be810SChristian König 	if (const_ib) {
164f28be810SChristian König 		radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
165975700d2SChristian König 		radeon_sync_free(rdev, &const_ib->sync, NULL);
166f28be810SChristian König 	}
167f28be810SChristian König 	radeon_ring_ib_execute(rdev, ib->ring, ib);
168f28be810SChristian König 	r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
169f28be810SChristian König 	if (r) {
170f28be810SChristian König 		dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
171f28be810SChristian König 		radeon_ring_unlock_undo(rdev, ring);
172f28be810SChristian König 		return r;
173f28be810SChristian König 	}
174f28be810SChristian König 	if (const_ib) {
175f28be810SChristian König 		const_ib->fence = radeon_fence_ref(ib->fence);
176f28be810SChristian König 	}
177f28be810SChristian König 
178f28be810SChristian König 	if (ib->vm)
179f28be810SChristian König 		radeon_vm_fence(rdev, ib->vm, ib->fence);
180f28be810SChristian König 
1811538a9e0SMichel Dänzer 	radeon_ring_unlock_commit(rdev, ring, hdp_flush);
182f28be810SChristian König 	return 0;
183f28be810SChristian König }
184f28be810SChristian König 
185f28be810SChristian König /**
186f28be810SChristian König  * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
187f28be810SChristian König  *
188f28be810SChristian König  * @rdev: radeon_device pointer
189f28be810SChristian König  *
190f28be810SChristian König  * Initialize the suballocator to manage a pool of memory
191f28be810SChristian König  * for use as IBs (all asics).
192f28be810SChristian König  * Returns 0 on success, error on failure.
193f28be810SChristian König  */
radeon_ib_pool_init(struct radeon_device * rdev)194f28be810SChristian König int radeon_ib_pool_init(struct radeon_device *rdev)
195f28be810SChristian König {
196f28be810SChristian König 	int r;
197f28be810SChristian König 
198f28be810SChristian König 	if (rdev->ib_pool_ready) {
199f28be810SChristian König 		return 0;
200f28be810SChristian König 	}
201f28be810SChristian König 
202f28be810SChristian König 	if (rdev->family >= CHIP_BONAIRE) {
203f28be810SChristian König 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
204f28be810SChristian König 					      RADEON_IB_POOL_SIZE*64*1024, 256,
205f28be810SChristian König 					      RADEON_GEM_DOMAIN_GTT,
206f28be810SChristian König 					      RADEON_GEM_GTT_WC);
207f28be810SChristian König 	} else {
208f28be810SChristian König 		/* Before CIK, it's better to stick to cacheable GTT due
209f28be810SChristian König 		 * to the command stream checking
210f28be810SChristian König 		 */
211f28be810SChristian König 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
212f28be810SChristian König 					      RADEON_IB_POOL_SIZE*64*1024, 256,
213f28be810SChristian König 					      RADEON_GEM_DOMAIN_GTT, 0);
214f28be810SChristian König 	}
215f28be810SChristian König 	if (r) {
216f28be810SChristian König 		return r;
217f28be810SChristian König 	}
218f28be810SChristian König 
219f28be810SChristian König 	r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
220f28be810SChristian König 	if (r) {
221f28be810SChristian König 		return r;
222f28be810SChristian König 	}
223f28be810SChristian König 
224f28be810SChristian König 	rdev->ib_pool_ready = true;
225f28be810SChristian König 	radeon_debugfs_sa_init(rdev);
226f28be810SChristian König 	return 0;
227*5b54d679SNirmoy Das }
228f28be810SChristian König 
229f28be810SChristian König /**
230f28be810SChristian König  * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
231f28be810SChristian König  *
232f28be810SChristian König  * @rdev: radeon_device pointer
233f28be810SChristian König  *
234f28be810SChristian König  * Tear down the suballocator managing the pool of memory
235f28be810SChristian König  * for use as IBs (all asics).
236f28be810SChristian König  */
radeon_ib_pool_fini(struct radeon_device * rdev)237f28be810SChristian König void radeon_ib_pool_fini(struct radeon_device *rdev)
238f28be810SChristian König {
239f28be810SChristian König 	if (rdev->ib_pool_ready) {
240f28be810SChristian König 		radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
241f28be810SChristian König 		radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
242f28be810SChristian König 		rdev->ib_pool_ready = false;
243f28be810SChristian König 	}
244f28be810SChristian König }
245f28be810SChristian König 
246f28be810SChristian König /**
247f28be810SChristian König  * radeon_ib_ring_tests - test IBs on the rings
248f28be810SChristian König  *
249f28be810SChristian König  * @rdev: radeon_device pointer
250f28be810SChristian König  *
251f28be810SChristian König  * Test an IB (Indirect Buffer) on each ring.
252f28be810SChristian König  * If the test fails, disable the ring.
253f28be810SChristian König  * Returns 0 on success, error if the primary GFX ring
254f28be810SChristian König  * IB test fails.
255f28be810SChristian König  */
radeon_ib_ring_tests(struct radeon_device * rdev)256f28be810SChristian König int radeon_ib_ring_tests(struct radeon_device *rdev)
257f28be810SChristian König {
258f28be810SChristian König 	unsigned i;
259f28be810SChristian König 	int r;
260f28be810SChristian König 
261f28be810SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
262f28be810SChristian König 		struct radeon_ring *ring = &rdev->ring[i];
263f28be810SChristian König 
264f28be810SChristian König 		if (!ring->ready)
265f28be810SChristian König 			continue;
266f28be810SChristian König 
267f28be810SChristian König 		r = radeon_ib_test(rdev, i, ring);
268f28be810SChristian König 		if (r) {
269f28be810SChristian König 			radeon_fence_driver_force_completion(rdev, i);
270f28be810SChristian König 			ring->ready = false;
271eb98c709SChristian König 			rdev->needs_reset = false;
272f28be810SChristian König 
273f28be810SChristian König 			if (i == RADEON_RING_TYPE_GFX_INDEX) {
274f28be810SChristian König 				/* oh, oh, that's really bad */
275f28be810SChristian König 				DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
276f28be810SChristian König 				rdev->accel_working = false;
277f28be810SChristian König 				return r;
278f28be810SChristian König 
279f28be810SChristian König 			} else {
280f28be810SChristian König 				/* still not good, but we can live with it */
281f28be810SChristian König 				DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
282f28be810SChristian König 			}
283f28be810SChristian König 		}
284f28be810SChristian König 	}
285f28be810SChristian König 	return 0;
286f28be810SChristian König }
287f28be810SChristian König 
288f28be810SChristian König /*
289f28be810SChristian König  * Debugfs info
290f28be810SChristian König  */
291f28be810SChristian König #if defined(CONFIG_DEBUG_FS)
292f28be810SChristian König 
radeon_debugfs_sa_info_show(struct seq_file * m,void * unused)293f28be810SChristian König static int radeon_debugfs_sa_info_show(struct seq_file *m, void *unused)
294f28be810SChristian König {
295*5b54d679SNirmoy Das 	struct radeon_device *rdev = m->private;
296f28be810SChristian König 
297*5b54d679SNirmoy Das 	radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
298f28be810SChristian König 
299f28be810SChristian König 	return 0;
300f28be810SChristian König 
301f28be810SChristian König }
302f28be810SChristian König 
303f28be810SChristian König DEFINE_SHOW_ATTRIBUTE(radeon_debugfs_sa_info);
304f28be810SChristian König 
305*5b54d679SNirmoy Das #endif
306f28be810SChristian König 
radeon_debugfs_sa_init(struct radeon_device * rdev)307f28be810SChristian König static void radeon_debugfs_sa_init(struct radeon_device *rdev)
308f28be810SChristian König {
309*5b54d679SNirmoy Das #if defined(CONFIG_DEBUG_FS)
310f28be810SChristian König 	struct dentry *root = rdev_to_drm(rdev)->primary->debugfs_root;
311f28be810SChristian König 
312*5b54d679SNirmoy Das 	debugfs_create_file("radeon_sa_info", 0444, root, rdev,
313*5b54d679SNirmoy Das 			    &radeon_debugfs_sa_info_fops);
314*5b54d679SNirmoy Das #endif
315*5b54d679SNirmoy Das }
316f28be810SChristian König