xref: /openbmc/linux/drivers/gpu/drm/radeon/radeon_ib.c (revision 7c1f03e02379f0b14251613779fd06e30419b148)
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_debugfs.h>
31f9183127SSam Ravnborg #include <drm/drm_file.h>
32f9183127SSam Ravnborg 
33f28be810SChristian König #include "radeon.h"
34f28be810SChristian König 
35f28be810SChristian König /*
36f28be810SChristian König  * IB
37f28be810SChristian König  * IBs (Indirect Buffers) and areas of GPU accessible memory where
38f28be810SChristian König  * commands are stored.  You can put a pointer to the IB in the
39f28be810SChristian König  * command ring and the hw will fetch the commands from the IB
40f28be810SChristian König  * and execute them.  Generally userspace acceleration drivers
41f28be810SChristian König  * produce command buffers which are send to the kernel and
42f28be810SChristian König  * put in IBs for execution by the requested ring.
43f28be810SChristian König  */
44f28be810SChristian König static int radeon_debugfs_sa_init(struct radeon_device *rdev);
45f28be810SChristian König 
46f28be810SChristian König /**
47f28be810SChristian König  * radeon_ib_get - request an IB (Indirect Buffer)
48f28be810SChristian König  *
49f28be810SChristian König  * @rdev: radeon_device pointer
50f28be810SChristian König  * @ring: ring index the IB is associated with
51*7c1f03e0SLee Jones  * @vm: requested vm
52f28be810SChristian König  * @ib: IB object returned
53f28be810SChristian König  * @size: requested IB size
54f28be810SChristian König  *
55f28be810SChristian König  * Request an IB (all asics).  IBs are allocated using the
56f28be810SChristian König  * suballocator.
57f28be810SChristian König  * Returns 0 on success, error on failure.
58f28be810SChristian König  */
59f28be810SChristian König int radeon_ib_get(struct radeon_device *rdev, int ring,
60f28be810SChristian König 		  struct radeon_ib *ib, struct radeon_vm *vm,
61f28be810SChristian König 		  unsigned size)
62f28be810SChristian König {
63f28be810SChristian König 	int r;
64f28be810SChristian König 
65f28be810SChristian König 	r = radeon_sa_bo_new(rdev, &rdev->ring_tmp_bo, &ib->sa_bo, size, 256);
66f28be810SChristian König 	if (r) {
67f28be810SChristian König 		dev_err(rdev->dev, "failed to get a new IB (%d)\n", r);
68f28be810SChristian König 		return r;
69f28be810SChristian König 	}
70f28be810SChristian König 
71975700d2SChristian König 	radeon_sync_create(&ib->sync);
72f28be810SChristian König 
73f28be810SChristian König 	ib->ring = ring;
74f28be810SChristian König 	ib->fence = NULL;
75f28be810SChristian König 	ib->ptr = radeon_sa_bo_cpu_addr(ib->sa_bo);
76f28be810SChristian König 	ib->vm = vm;
77f28be810SChristian König 	if (vm) {
78f28be810SChristian König 		/* ib pool is bound at RADEON_VA_IB_OFFSET in virtual address
79f28be810SChristian König 		 * space and soffset is the offset inside the pool bo
80f28be810SChristian König 		 */
81f28be810SChristian König 		ib->gpu_addr = ib->sa_bo->soffset + RADEON_VA_IB_OFFSET;
82f28be810SChristian König 	} else {
83f28be810SChristian König 		ib->gpu_addr = radeon_sa_bo_gpu_addr(ib->sa_bo);
84f28be810SChristian König 	}
85f28be810SChristian König 	ib->is_const_ib = false;
86f28be810SChristian König 
87f28be810SChristian König 	return 0;
88f28be810SChristian König }
89f28be810SChristian König 
90f28be810SChristian König /**
91f28be810SChristian König  * radeon_ib_free - free an IB (Indirect Buffer)
92f28be810SChristian König  *
93f28be810SChristian König  * @rdev: radeon_device pointer
94f28be810SChristian König  * @ib: IB object to free
95f28be810SChristian König  *
96f28be810SChristian König  * Free an IB (all asics).
97f28be810SChristian König  */
98f28be810SChristian König void radeon_ib_free(struct radeon_device *rdev, struct radeon_ib *ib)
99f28be810SChristian König {
100975700d2SChristian König 	radeon_sync_free(rdev, &ib->sync, ib->fence);
101f28be810SChristian König 	radeon_sa_bo_free(rdev, &ib->sa_bo, ib->fence);
102f28be810SChristian König 	radeon_fence_unref(&ib->fence);
103f28be810SChristian König }
104f28be810SChristian König 
105f28be810SChristian König /**
106f28be810SChristian König  * radeon_ib_schedule - schedule an IB (Indirect Buffer) on the ring
107f28be810SChristian König  *
108f28be810SChristian König  * @rdev: radeon_device pointer
109f28be810SChristian König  * @ib: IB object to schedule
110f28be810SChristian König  * @const_ib: Const IB to schedule (SI only)
1111538a9e0SMichel Dänzer  * @hdp_flush: Whether or not to perform an HDP cache flush
112f28be810SChristian König  *
113f28be810SChristian König  * Schedule an IB on the associated ring (all asics).
114f28be810SChristian König  * Returns 0 on success, error on failure.
115f28be810SChristian König  *
116f28be810SChristian König  * On SI, there are two parallel engines fed from the primary ring,
117f28be810SChristian König  * the CE (Constant Engine) and the DE (Drawing Engine).  Since
118f28be810SChristian König  * resource descriptors have moved to memory, the CE allows you to
119f28be810SChristian König  * prime the caches while the DE is updating register state so that
120f28be810SChristian König  * the resource descriptors will be already in cache when the draw is
121f28be810SChristian König  * processed.  To accomplish this, the userspace driver submits two
122f28be810SChristian König  * IBs, one for the CE and one for the DE.  If there is a CE IB (called
123f28be810SChristian König  * a CONST_IB), it will be put on the ring prior to the DE IB.  Prior
124f28be810SChristian König  * to SI there was just a DE IB.
125f28be810SChristian König  */
126f28be810SChristian König int radeon_ib_schedule(struct radeon_device *rdev, struct radeon_ib *ib,
1271538a9e0SMichel Dänzer 		       struct radeon_ib *const_ib, bool hdp_flush)
128f28be810SChristian König {
129f28be810SChristian König 	struct radeon_ring *ring = &rdev->ring[ib->ring];
130f28be810SChristian König 	int r = 0;
131f28be810SChristian König 
132f28be810SChristian König 	if (!ib->length_dw || !ring->ready) {
133f28be810SChristian König 		/* TODO: Nothings in the ib we should report. */
134f28be810SChristian König 		dev_err(rdev->dev, "couldn't schedule ib\n");
135f28be810SChristian König 		return -EINVAL;
136f28be810SChristian König 	}
137f28be810SChristian König 
138f28be810SChristian König 	/* 64 dwords should be enough for fence too */
139f28be810SChristian König 	r = radeon_ring_lock(rdev, ring, 64 + RADEON_NUM_SYNCS * 8);
140f28be810SChristian König 	if (r) {
141f28be810SChristian König 		dev_err(rdev->dev, "scheduling IB failed (%d).\n", r);
142f28be810SChristian König 		return r;
143f28be810SChristian König 	}
144f28be810SChristian König 
145f28be810SChristian König 	/* grab a vm id if necessary */
146f28be810SChristian König 	if (ib->vm) {
147f28be810SChristian König 		struct radeon_fence *vm_id_fence;
148f28be810SChristian König 		vm_id_fence = radeon_vm_grab_id(rdev, ib->vm, ib->ring);
149975700d2SChristian König 		radeon_sync_fence(&ib->sync, vm_id_fence);
150f28be810SChristian König 	}
151f28be810SChristian König 
152f28be810SChristian König 	/* sync with other rings */
153975700d2SChristian König 	r = radeon_sync_rings(rdev, &ib->sync, ib->ring);
154f28be810SChristian König 	if (r) {
155f28be810SChristian König 		dev_err(rdev->dev, "failed to sync rings (%d)\n", r);
156f28be810SChristian König 		radeon_ring_unlock_undo(rdev, ring);
157f28be810SChristian König 		return r;
158f28be810SChristian König 	}
159f28be810SChristian König 
160f28be810SChristian König 	if (ib->vm)
161ad1a58a4SChristian König 		radeon_vm_flush(rdev, ib->vm, ib->ring,
162ad1a58a4SChristian König 				ib->sync.last_vm_update);
163f28be810SChristian König 
164f28be810SChristian König 	if (const_ib) {
165f28be810SChristian König 		radeon_ring_ib_execute(rdev, const_ib->ring, const_ib);
166975700d2SChristian König 		radeon_sync_free(rdev, &const_ib->sync, NULL);
167f28be810SChristian König 	}
168f28be810SChristian König 	radeon_ring_ib_execute(rdev, ib->ring, ib);
169f28be810SChristian König 	r = radeon_fence_emit(rdev, &ib->fence, ib->ring);
170f28be810SChristian König 	if (r) {
171f28be810SChristian König 		dev_err(rdev->dev, "failed to emit fence for new IB (%d)\n", r);
172f28be810SChristian König 		radeon_ring_unlock_undo(rdev, ring);
173f28be810SChristian König 		return r;
174f28be810SChristian König 	}
175f28be810SChristian König 	if (const_ib) {
176f28be810SChristian König 		const_ib->fence = radeon_fence_ref(ib->fence);
177f28be810SChristian König 	}
178f28be810SChristian König 
179f28be810SChristian König 	if (ib->vm)
180f28be810SChristian König 		radeon_vm_fence(rdev, ib->vm, ib->fence);
181f28be810SChristian König 
1821538a9e0SMichel Dänzer 	radeon_ring_unlock_commit(rdev, ring, hdp_flush);
183f28be810SChristian König 	return 0;
184f28be810SChristian König }
185f28be810SChristian König 
186f28be810SChristian König /**
187f28be810SChristian König  * radeon_ib_pool_init - Init the IB (Indirect Buffer) pool
188f28be810SChristian König  *
189f28be810SChristian König  * @rdev: radeon_device pointer
190f28be810SChristian König  *
191f28be810SChristian König  * Initialize the suballocator to manage a pool of memory
192f28be810SChristian König  * for use as IBs (all asics).
193f28be810SChristian König  * Returns 0 on success, error on failure.
194f28be810SChristian König  */
195f28be810SChristian König int radeon_ib_pool_init(struct radeon_device *rdev)
196f28be810SChristian König {
197f28be810SChristian König 	int r;
198f28be810SChristian König 
199f28be810SChristian König 	if (rdev->ib_pool_ready) {
200f28be810SChristian König 		return 0;
201f28be810SChristian König 	}
202f28be810SChristian König 
203f28be810SChristian König 	if (rdev->family >= CHIP_BONAIRE) {
204f28be810SChristian König 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
205f28be810SChristian König 					      RADEON_IB_POOL_SIZE*64*1024,
206f28be810SChristian König 					      RADEON_GPU_PAGE_SIZE,
207f28be810SChristian König 					      RADEON_GEM_DOMAIN_GTT,
208f28be810SChristian König 					      RADEON_GEM_GTT_WC);
209f28be810SChristian König 	} else {
210f28be810SChristian König 		/* Before CIK, it's better to stick to cacheable GTT due
211f28be810SChristian König 		 * to the command stream checking
212f28be810SChristian König 		 */
213f28be810SChristian König 		r = radeon_sa_bo_manager_init(rdev, &rdev->ring_tmp_bo,
214f28be810SChristian König 					      RADEON_IB_POOL_SIZE*64*1024,
215f28be810SChristian König 					      RADEON_GPU_PAGE_SIZE,
216f28be810SChristian König 					      RADEON_GEM_DOMAIN_GTT, 0);
217f28be810SChristian König 	}
218f28be810SChristian König 	if (r) {
219f28be810SChristian König 		return r;
220f28be810SChristian König 	}
221f28be810SChristian König 
222f28be810SChristian König 	r = radeon_sa_bo_manager_start(rdev, &rdev->ring_tmp_bo);
223f28be810SChristian König 	if (r) {
224f28be810SChristian König 		return r;
225f28be810SChristian König 	}
226f28be810SChristian König 
227f28be810SChristian König 	rdev->ib_pool_ready = true;
228f28be810SChristian König 	if (radeon_debugfs_sa_init(rdev)) {
229f28be810SChristian König 		dev_err(rdev->dev, "failed to register debugfs file for SA\n");
230f28be810SChristian König 	}
231f28be810SChristian König 	return 0;
232f28be810SChristian König }
233f28be810SChristian König 
234f28be810SChristian König /**
235f28be810SChristian König  * radeon_ib_pool_fini - Free the IB (Indirect Buffer) pool
236f28be810SChristian König  *
237f28be810SChristian König  * @rdev: radeon_device pointer
238f28be810SChristian König  *
239f28be810SChristian König  * Tear down the suballocator managing the pool of memory
240f28be810SChristian König  * for use as IBs (all asics).
241f28be810SChristian König  */
242f28be810SChristian König void radeon_ib_pool_fini(struct radeon_device *rdev)
243f28be810SChristian König {
244f28be810SChristian König 	if (rdev->ib_pool_ready) {
245f28be810SChristian König 		radeon_sa_bo_manager_suspend(rdev, &rdev->ring_tmp_bo);
246f28be810SChristian König 		radeon_sa_bo_manager_fini(rdev, &rdev->ring_tmp_bo);
247f28be810SChristian König 		rdev->ib_pool_ready = false;
248f28be810SChristian König 	}
249f28be810SChristian König }
250f28be810SChristian König 
251f28be810SChristian König /**
252f28be810SChristian König  * radeon_ib_ring_tests - test IBs on the rings
253f28be810SChristian König  *
254f28be810SChristian König  * @rdev: radeon_device pointer
255f28be810SChristian König  *
256f28be810SChristian König  * Test an IB (Indirect Buffer) on each ring.
257f28be810SChristian König  * If the test fails, disable the ring.
258f28be810SChristian König  * Returns 0 on success, error if the primary GFX ring
259f28be810SChristian König  * IB test fails.
260f28be810SChristian König  */
261f28be810SChristian König int radeon_ib_ring_tests(struct radeon_device *rdev)
262f28be810SChristian König {
263f28be810SChristian König 	unsigned i;
264f28be810SChristian König 	int r;
265f28be810SChristian König 
266f28be810SChristian König 	for (i = 0; i < RADEON_NUM_RINGS; ++i) {
267f28be810SChristian König 		struct radeon_ring *ring = &rdev->ring[i];
268f28be810SChristian König 
269f28be810SChristian König 		if (!ring->ready)
270f28be810SChristian König 			continue;
271f28be810SChristian König 
272f28be810SChristian König 		r = radeon_ib_test(rdev, i, ring);
273f28be810SChristian König 		if (r) {
274eb98c709SChristian König 			radeon_fence_driver_force_completion(rdev, i);
275f28be810SChristian König 			ring->ready = false;
276f28be810SChristian König 			rdev->needs_reset = false;
277f28be810SChristian König 
278f28be810SChristian König 			if (i == RADEON_RING_TYPE_GFX_INDEX) {
279f28be810SChristian König 				/* oh, oh, that's really bad */
280f28be810SChristian König 				DRM_ERROR("radeon: failed testing IB on GFX ring (%d).\n", r);
281f28be810SChristian König 				rdev->accel_working = false;
282f28be810SChristian König 				return r;
283f28be810SChristian König 
284f28be810SChristian König 			} else {
285f28be810SChristian König 				/* still not good, but we can live with it */
286f28be810SChristian König 				DRM_ERROR("radeon: failed testing IB on ring %d (%d).\n", i, r);
287f28be810SChristian König 			}
288f28be810SChristian König 		}
289f28be810SChristian König 	}
290f28be810SChristian König 	return 0;
291f28be810SChristian König }
292f28be810SChristian König 
293f28be810SChristian König /*
294f28be810SChristian König  * Debugfs info
295f28be810SChristian König  */
296f28be810SChristian König #if defined(CONFIG_DEBUG_FS)
297f28be810SChristian König 
298f28be810SChristian König static int radeon_debugfs_sa_info(struct seq_file *m, void *data)
299f28be810SChristian König {
300f28be810SChristian König 	struct drm_info_node *node = (struct drm_info_node *) m->private;
301f28be810SChristian König 	struct drm_device *dev = node->minor->dev;
302f28be810SChristian König 	struct radeon_device *rdev = dev->dev_private;
303f28be810SChristian König 
304f28be810SChristian König 	radeon_sa_bo_dump_debug_info(&rdev->ring_tmp_bo, m);
305f28be810SChristian König 
306f28be810SChristian König 	return 0;
307f28be810SChristian König 
308f28be810SChristian König }
309f28be810SChristian König 
310f28be810SChristian König static struct drm_info_list radeon_debugfs_sa_list[] = {
311f28be810SChristian König 	{"radeon_sa_info", &radeon_debugfs_sa_info, 0, NULL},
312f28be810SChristian König };
313f28be810SChristian König 
314f28be810SChristian König #endif
315f28be810SChristian König 
316f28be810SChristian König static int radeon_debugfs_sa_init(struct radeon_device *rdev)
317f28be810SChristian König {
318f28be810SChristian König #if defined(CONFIG_DEBUG_FS)
319f28be810SChristian König 	return radeon_debugfs_add_files(rdev, radeon_debugfs_sa_list, 1);
320f28be810SChristian König #else
321f28be810SChristian König 	return 0;
322f28be810SChristian König #endif
323f28be810SChristian König }
324