xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_ib.c (revision eb98c709907c7a78b9cd0d18642477d47d348f9f)
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  */
29f28be810SChristian König #include <drm/drmP.h>
30f28be810SChristian König #include "radeon.h"
31f28be810SChristian König 
32f28be810SChristian König /*
33f28be810SChristian König  * IB
34f28be810SChristian König  * IBs (Indirect Buffers) and areas of GPU accessible memory where
35f28be810SChristian König  * commands are stored.  You can put a pointer to the IB in the
36f28be810SChristian König  * command ring and the hw will fetch the commands from the IB
37f28be810SChristian König  * and execute them.  Generally userspace acceleration drivers
38f28be810SChristian König  * produce command buffers which are send to the kernel and
39f28be810SChristian König  * put in IBs for execution by the requested ring.
40f28be810SChristian König  */
41f28be810SChristian König static int radeon_debugfs_sa_init(struct radeon_device *rdev);
42f28be810SChristian König 
43f28be810SChristian König /**
44f28be810SChristian König  * radeon_ib_get - request an IB (Indirect Buffer)
45f28be810SChristian König  *
46f28be810SChristian König  * @rdev: radeon_device pointer
47f28be810SChristian König  * @ring: ring index the IB is associated with
48f28be810SChristian König  * @ib: IB object returned
49f28be810SChristian König  * @size: requested IB size
50f28be810SChristian König  *
51f28be810SChristian König  * Request an IB (all asics).  IBs are allocated using the
52f28be810SChristian König  * suballocator.
53f28be810SChristian König  * Returns 0 on success, error on failure.
54f28be810SChristian König  */
55f28be810SChristian König int radeon_ib_get(struct radeon_device *rdev, int ring,
56f28be810SChristian König 		  struct radeon_ib *ib, struct radeon_vm *vm,
57f28be810SChristian König 		  unsigned size)
58f28be810SChristian König {
59f28be810SChristian König 	int r;
60f28be810SChristian König 
61f28be810SChristian König 	r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256);
62f28be810SChristian König 	if (r) {
63f28be810SChristian König 		dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
64f28be810SChristian König 		return r;
65f28be810SChristian König 	}
66f28be810SChristian König 
67f28be810SChristian König 	r = radeon_semaphore_create(rdev, &ib->semaphore);
68f28be810SChristian König 	if (r) {
69f28be810SChristian König 		return r;
70f28be810SChristian König 	}
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 = ib->sa_bo->soffset + 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  */
97f28be810SChristian König void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
98f28be810SChristian König {
99f28be810SChristian König 	radeon_semaphore_free(rdev, &ib->semaphore, ib->fence);
100f28be810SChristian König 	radeon_sa_bo_free(rdev, &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  */
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);
148f28be810SChristian König         	radeon_semaphore_sync_to(ib->semaphore, vm_id_fence);
149f28be810SChristian König 	}
150f28be810SChristian König 
151f28be810SChristian König 	/* sync with other rings */
152f28be810SChristian König 	r = radeon_semaphore_sync_rings(rdev, ib->semaphore, 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)
160f28be810SChristian König 		radeon_vm_flush(rdev, ib->vm, ib->ring);
161f28be810SChristian König 
162f28be810SChristian König 	if (const_ib) {
163f28be810SChristian König 		radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
164f28be810SChristian König 		radeon_semaphore_free(rdev, &const_ib->semaphore, NULL);
165f28be810SChristian König 	}
166f28be810SChristian König 	radeon_ring_ib_execute(rdev, ib->ring, ib);
167f28be810SChristian König 	r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
168f28be810SChristian König 	if (r) {
169f28be810SChristian König 		dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
170f28be810SChristian König 		radeon_ring_unlock_undo(rdev, ring);
171f28be810SChristian König 		return r;
172f28be810SChristian König 	}
173f28be810SChristian König 	if (const_ib) {
174f28be810SChristian König 		const_ib->fence = radeon_fence_ref(ib->fence);
175f28be810SChristian König 	}
176f28be810SChristian König 
177f28be810SChristian König 	if (ib->vm)
178f28be810SChristian König 		radeon_vm_fence(rdev, ib->vm, ib->fence);
179f28be810SChristian König 
1801538a9e0SMichel Dänzer 	radeon_ring_unlock_commit(rdev, ring, hdp_flush);
181f28be810SChristian König 	return 0;
182f28be810SChristian König }
183f28be810SChristian König 
184f28be810SChristian König /**
185f28be810SChristian König  * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
186f28be810SChristian König  *
187f28be810SChristian König  * @rdev: radeon_device pointer
188f28be810SChristian König  *
189f28be810SChristian König  * Initialize the suballocator to manage a pool of memory
190f28be810SChristian König  * for use as IBs (all asics).
191f28be810SChristian König  * Returns 0 on success, error on failure.
192f28be810SChristian König  */
193f28be810SChristian König int radeon_ib_pool_init(struct radeon_device *rdev)
194f28be810SChristian König {
195f28be810SChristian König 	int r;
196f28be810SChristian König 
197f28be810SChristian König 	if (rdev->ib_pool_ready) {
198f28be810SChristian König 		return 0;
199f28be810SChristian König 	}
200f28be810SChristian König 
201f28be810SChristian König 	if (rdev->family >= CHIP_BONAIRE) {
202f28be810SChristian König 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
203f28be810SChristian König 					      RADEON_IB_POOL_SIZE*64*1024,
204f28be810SChristian König 					      RADEON_GPU_PAGE_SIZE,
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,
213f28be810SChristian König 					      RADEON_GPU_PAGE_SIZE,
214f28be810SChristian König 					      RADEON_GEM_DOMAIN_GTT, 0);
215f28be810SChristian König 	}
216f28be810SChristian König 	if (r) {
217f28be810SChristian König 		return r;
218f28be810SChristian König 	}
219f28be810SChristian König 
220f28be810SChristian König 	r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
221f28be810SChristian König 	if (r) {
222f28be810SChristian König 		return r;
223f28be810SChristian König 	}
224f28be810SChristian König 
225f28be810SChristian König 	rdev->ib_pool_ready = true;
226f28be810SChristian König 	if (radeon_debugfs_sa_init(rdev)) {
227f28be810SChristian König 		dev_err(rdev->dev, "failed to register debugfs file for SA\n");
228f28be810SChristian König 	}
229f28be810SChristian König 	return 0;
230f28be810SChristian König }
231f28be810SChristian König 
232f28be810SChristian König /**
233f28be810SChristian König  * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
234f28be810SChristian König  *
235f28be810SChristian König  * @rdev: radeon_device pointer
236f28be810SChristian König  *
237f28be810SChristian König  * Tear down the suballocator managing the pool of memory
238f28be810SChristian König  * for use as IBs (all asics).
239f28be810SChristian König  */
240f28be810SChristian König void radeon_ib_pool_fini(struct radeon_device *rdev)
241f28be810SChristian König {
242f28be810SChristian König 	if (rdev->ib_pool_ready) {
243f28be810SChristian König 		radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
244f28be810SChristian König 		radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
245f28be810SChristian König 		rdev->ib_pool_ready = false;
246f28be810SChristian König 	}
247f28be810SChristian König }
248f28be810SChristian König 
249f28be810SChristian König /**
250f28be810SChristian König  * radeon_ib_ring_tests - test IBs on the rings
251f28be810SChristian König  *
252f28be810SChristian König  * @rdev: radeon_device pointer
253f28be810SChristian König  *
254f28be810SChristian König  * Test an IB (Indirect Buffer) on each ring.
255f28be810SChristian König  * If the test fails, disable the ring.
256f28be810SChristian König  * Returns 0 on success, error if the primary GFX ring
257f28be810SChristian König  * IB test fails.
258f28be810SChristian König  */
259f28be810SChristian König int radeon_ib_ring_tests(struct radeon_device *rdev)
260f28be810SChristian König {
261f28be810SChristian König 	unsigned i;
262f28be810SChristian König 	int r;
263f28be810SChristian König 
264f28be810SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
265f28be810SChristian König 		struct radeon_ring *ring = &rdev->ring[i];
266f28be810SChristian König 
267f28be810SChristian König 		if (!ring->ready)
268f28be810SChristian König 			continue;
269f28be810SChristian König 
270f28be810SChristian König 		r = radeon_ib_test(rdev, i, ring);
271f28be810SChristian König 		if (r) {
272*eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
273f28be810SChristian König 			ring->ready = false;
274f28be810SChristian König 			rdev->needs_reset = false;
275f28be810SChristian König 
276f28be810SChristian König 			if (i == RADEON_RING_TYPE_GFX_INDEX) {
277f28be810SChristian König 				/* oh, oh, that's really bad */
278f28be810SChristian König 				DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
279f28be810SChristian König 		                rdev->accel_working = false;
280f28be810SChristian König 				return r;
281f28be810SChristian König 
282f28be810SChristian König 			} else {
283f28be810SChristian König 				/* still not good, but we can live with it */
284f28be810SChristian König 				DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
285f28be810SChristian König 			}
286f28be810SChristian König 		}
287f28be810SChristian König 	}
288f28be810SChristian König 	return 0;
289f28be810SChristian König }
290f28be810SChristian König 
291f28be810SChristian König /*
292f28be810SChristian König  * Debugfs info
293f28be810SChristian König  */
294f28be810SChristian König #if defined(CONFIG_DEBUG_FS)
295f28be810SChristian König 
296f28be810SChristian König static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
297f28be810SChristian König {
298f28be810SChristian König 	struct drm_info_node *node = (struct drm_info_node *) m->private;
299f28be810SChristian König 	struct drm_device *dev = node->minor->dev;
300f28be810SChristian König 	struct radeon_device *rdev = dev->dev_private;
301f28be810SChristian König 
302f28be810SChristian König 	radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
303f28be810SChristian König 
304f28be810SChristian König 	return 0;
305f28be810SChristian König 
306f28be810SChristian König }
307f28be810SChristian König 
308f28be810SChristian König static struct drm_info_list radeon_debugfs_sa_list[] = {
309f28be810SChristian König         {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
310f28be810SChristian König };
311f28be810SChristian König 
312f28be810SChristian König #endif
313f28be810SChristian König 
314f28be810SChristian König static int radeon_debugfs_sa_init(struct radeon_device *rdev)
315f28be810SChristian König {
316f28be810SChristian König #if defined(CONFIG_DEBUG_FS)
317f28be810SChristian König 	return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
318f28be810SChristian König #else
319f28be810SChristian König 	return 0;
320f28be810SChristian König #endif
321f28be810SChristian König }
322