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