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