xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_ib.c (revision 975700d2cc84408efa9b2360e38b1ab95368556f)
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 
67*975700d2SChristian König 	radeon_sync_create(&ib->sync);
68f28be810SChristian König 
69f28be810SChristian König 	ib->ring = ring;
70f28be810SChristian König 	ib->fence = NULL;
71f28be810SChristian König 	ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
72f28be810SChristian König 	ib->vm = vm;
73f28be810SChristian König 	if (vm) {
74f28be810SChristian König 		/* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address
75f28be810SChristian König 		 * space and soffset is the offset inside the pool bo
76f28be810SChristian König 		 */
77f28be810SChristian König 		ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET;
78f28be810SChristian König 	} else {
79f28be810SChristian König 		ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
80f28be810SChristian König 	}
81f28be810SChristian König 	ib->is_const_ib = false;
82f28be810SChristian König 
83f28be810SChristian König 	return 0;
84f28be810SChristian König }
85f28be810SChristian König 
86f28be810SChristian König /**
87f28be810SChristian König  * radeon_ib_free - free an IB (Indirect Buffer)
88f28be810SChristian König  *
89f28be810SChristian König  * @rdev: radeon_device pointer
90f28be810SChristian König  * @ib: IB object to free
91f28be810SChristian König  *
92f28be810SChristian König  * Free an IB (all asics).
93f28be810SChristian König  */
94f28be810SChristian König void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
95f28be810SChristian König {
96*975700d2SChristian König 	radeon_sync_free(rdev, &ib->sync, ib->fence);
97f28be810SChristian König 	radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
98f28be810SChristian König 	radeon_fence_unref(&ib->fence);
99f28be810SChristian König }
100f28be810SChristian König 
101f28be810SChristian König /**
102f28be810SChristian König  * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring
103f28be810SChristian König  *
104f28be810SChristian König  * @rdev: radeon_device pointer
105f28be810SChristian König  * @ib: IB object to schedule
106f28be810SChristian König  * @const_ib: Const IB to schedule (SI only)
1071538a9e0SMichel Dänzer  * @hdp_flush: Whether or not to perform an HDP cache flush
108f28be810SChristian König  *
109f28be810SChristian König  * Schedule an IB on the associated ring (all asics).
110f28be810SChristian König  * Returns 0 on success, error on failure.
111f28be810SChristian König  *
112f28be810SChristian König  * On SI, there are two parallel engines fed from the primary ring,
113f28be810SChristian König  * the CE (Constant Engine) and the DE (Drawing Engine).  Since
114f28be810SChristian König  * resource descriptors have moved to memory, the CE allows you to
115f28be810SChristian König  * prime the caches while the DE is updating register state so that
116f28be810SChristian König  * the resource descriptors will be already in cache when the draw is
117f28be810SChristian König  * processed.  To accomplish this, the userspace driver submits two
118f28be810SChristian König  * IBs, one for the CE and one for the DE.  If there is a CE IB (called
119f28be810SChristian König  * a CONST_IB), it will be put on the ring prior to the DE IB.  Prior
120f28be810SChristian König  * to SI there was just a DE IB.
121f28be810SChristian König  */
122f28be810SChristian König int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
1231538a9e0SMichel Dänzer 		       struct radeon_ib *const_ib, bool hdp_flush)
124f28be810SChristian König {
125f28be810SChristian König 	struct radeon_ring *ring = &rdev->ring[ib->ring];
126f28be810SChristian König 	int r = 0;
127f28be810SChristian König 
128f28be810SChristian König 	if (!ib->length_dw || !ring->ready) {
129f28be810SChristian König 		/* TODO: Nothings in the ib we should report. */
130f28be810SChristian König 		dev_err(rdev->dev, "couldn't schedule ib\n");
131f28be810SChristian König 		return -EINVAL;
132f28be810SChristian König 	}
133f28be810SChristian König 
134f28be810SChristian König 	/* 64 dwords should be enough for fence too */
135f28be810SChristian König 	r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8);
136f28be810SChristian König 	if (r) {
137f28be810SChristian König 		dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
138f28be810SChristian König 		return r;
139f28be810SChristian König 	}
140f28be810SChristian König 
141f28be810SChristian König 	/* grab a vm id if necessary */
142f28be810SChristian König 	if (ib->vm) {
143f28be810SChristian König 		struct radeon_fence *vm_id_fence;
144f28be810SChristian König 		vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring);
145*975700d2SChristian König 		radeon_sync_fence(&ib->sync, vm_id_fence);
146f28be810SChristian König 	}
147f28be810SChristian König 
148f28be810SChristian König 	/* sync with other rings */
149*975700d2SChristian König 	r = radeon_sync_rings(rdev, &ib->sync, ib->ring);
150f28be810SChristian König 	if (r) {
151f28be810SChristian König 		dev_err(rdev->dev, "failed to sync rings (%d)\n", r);
152f28be810SChristian König 		radeon_ring_unlock_undo(rdev, ring);
153f28be810SChristian König 		return r;
154f28be810SChristian König 	}
155f28be810SChristian König 
156f28be810SChristian König 	if (ib->vm)
157f28be810SChristian König 		radeon_vm_flush(rdev, ib->vm, ib->ring);
158f28be810SChristian König 
159f28be810SChristian König 	if (const_ib) {
160f28be810SChristian König 		radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
161*975700d2SChristian König 		radeon_sync_free(rdev, &const_ib->sync, NULL);
162f28be810SChristian König 	}
163f28be810SChristian König 	radeon_ring_ib_execute(rdev, ib->ring, ib);
164f28be810SChristian König 	r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
165f28be810SChristian König 	if (r) {
166f28be810SChristian König 		dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
167f28be810SChristian König 		radeon_ring_unlock_undo(rdev, ring);
168f28be810SChristian König 		return r;
169f28be810SChristian König 	}
170f28be810SChristian König 	if (const_ib) {
171f28be810SChristian König 		const_ib->fence = radeon_fence_ref(ib->fence);
172f28be810SChristian König 	}
173f28be810SChristian König 
174f28be810SChristian König 	if (ib->vm)
175f28be810SChristian König 		radeon_vm_fence(rdev, ib->vm, ib->fence);
176f28be810SChristian König 
1771538a9e0SMichel Dänzer 	radeon_ring_unlock_commit(rdev, ring, hdp_flush);
178f28be810SChristian König 	return 0;
179f28be810SChristian König }
180f28be810SChristian König 
181f28be810SChristian König /**
182f28be810SChristian König  * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
183f28be810SChristian König  *
184f28be810SChristian König  * @rdev: radeon_device pointer
185f28be810SChristian König  *
186f28be810SChristian König  * Initialize the suballocator to manage a pool of memory
187f28be810SChristian König  * for use as IBs (all asics).
188f28be810SChristian König  * Returns 0 on success, error on failure.
189f28be810SChristian König  */
190f28be810SChristian König int radeon_ib_pool_init(struct radeon_device *rdev)
191f28be810SChristian König {
192f28be810SChristian König 	int r;
193f28be810SChristian König 
194f28be810SChristian König 	if (rdev->ib_pool_ready) {
195f28be810SChristian König 		return 0;
196f28be810SChristian König 	}
197f28be810SChristian König 
198f28be810SChristian König 	if (rdev->family >= CHIP_BONAIRE) {
199f28be810SChristian König 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
200f28be810SChristian König 					      RADEON_IB_POOL_SIZE*64*1024,
201f28be810SChristian König 					      RADEON_GPU_PAGE_SIZE,
202f28be810SChristian König 					      RADEON_GEM_DOMAIN_GTT,
203f28be810SChristian König 					      RADEON_GEM_GTT_WC);
204f28be810SChristian König 	} else {
205f28be810SChristian König 		/* Before CIK, it's better to stick to cacheable GTT due
206f28be810SChristian König 		 * to the command stream checking
207f28be810SChristian König 		 */
208f28be810SChristian König 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
209f28be810SChristian König 					      RADEON_IB_POOL_SIZE*64*1024,
210f28be810SChristian König 					      RADEON_GPU_PAGE_SIZE,
211f28be810SChristian König 					      RADEON_GEM_DOMAIN_GTT, 0);
212f28be810SChristian König 	}
213f28be810SChristian König 	if (r) {
214f28be810SChristian König 		return r;
215f28be810SChristian König 	}
216f28be810SChristian König 
217f28be810SChristian König 	r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
218f28be810SChristian König 	if (r) {
219f28be810SChristian König 		return r;
220f28be810SChristian König 	}
221f28be810SChristian König 
222f28be810SChristian König 	rdev->ib_pool_ready = true;
223f28be810SChristian König 	if (radeon_debugfs_sa_init(rdev)) {
224f28be810SChristian König 		dev_err(rdev->dev, "failed to register debugfs file for SA\n");
225f28be810SChristian König 	}
226f28be810SChristian König 	return 0;
227f28be810SChristian König }
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  */
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  */
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) {
269eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
270f28be810SChristian König 			ring->ready = false;
271f28be810SChristian 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 
293f28be810SChristian König static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
294f28be810SChristian König {
295f28be810SChristian König 	struct drm_info_node *node = (struct drm_info_node *) m->private;
296f28be810SChristian König 	struct drm_device *dev = node->minor->dev;
297f28be810SChristian König 	struct radeon_device *rdev = dev->dev_private;
298f28be810SChristian König 
299f28be810SChristian König 	radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
300f28be810SChristian König 
301f28be810SChristian König 	return 0;
302f28be810SChristian König 
303f28be810SChristian König }
304f28be810SChristian König 
305f28be810SChristian König static struct drm_info_list radeon_debugfs_sa_list[] = {
306f28be810SChristian König         {"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
307f28be810SChristian König };
308f28be810SChristian König 
309f28be810SChristian König #endif
310f28be810SChristian König 
311f28be810SChristian König static int radeon_debugfs_sa_init(struct radeon_device *rdev)
312f28be810SChristian König {
313f28be810SChristian König #if defined(CONFIG_DEBUG_FS)
314f28be810SChristian König 	return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
315f28be810SChristian König #else
316f28be810SChristian König 	return 0;
317f28be810SChristian König #endif
318f28be810SChristian König }
319