radeon_ring.c revision 1.2.32.1 1 /* $NetBSD: radeon_ring.c,v 1.2.32.1 2019/06/10 22:08:26 christos Exp $ */
2
3 /*
4 * Copyright 2008 Advanced Micro Devices, Inc.
5 * Copyright 2008 Red Hat Inc.
6 * Copyright 2009 Jerome Glisse.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors: Dave Airlie
27 * Alex Deucher
28 * Jerome Glisse
29 * Christian Knig
30 */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: radeon_ring.c,v 1.2.32.1 2019/06/10 22:08:26 christos Exp $");
33
34 #include <linux/jiffies.h>
35 #include <drm/drmP.h>
36 #include "radeon.h"
37
38 /*
39 * Rings
40 * Most engines on the GPU are fed via ring buffers. Ring
41 * buffers are areas of GPU accessible memory that the host
42 * writes commands into and the GPU reads commands out of.
43 * There is a rptr (read pointer) that determines where the
44 * GPU is currently reading, and a wptr (write pointer)
45 * which determines where the host has written. When the
46 * pointers are equal, the ring is idle. When the host
47 * writes commands to the ring buffer, it increments the
48 * wptr. The GPU then starts fetching commands and executes
49 * them until the pointers are equal again.
50 */
51 static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
52
53 /**
54 * radeon_ring_supports_scratch_reg - check if the ring supports
55 * writing to scratch registers
56 *
57 * @rdev: radeon_device pointer
58 * @ring: radeon_ring structure holding ring information
59 *
60 * Check if a specific ring supports writing to scratch registers (all asics).
61 * Returns true if the ring supports writing to scratch regs, false if not.
62 */
63 bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
64 struct radeon_ring *ring)
65 {
66 switch (ring->idx) {
67 case RADEON_RING_TYPE_GFX_INDEX:
68 case CAYMAN_RING_TYPE_CP1_INDEX:
69 case CAYMAN_RING_TYPE_CP2_INDEX:
70 return true;
71 default:
72 return false;
73 }
74 }
75
76 /**
77 * radeon_ring_free_size - update the free size
78 *
79 * @rdev: radeon_device pointer
80 * @ring: radeon_ring structure holding ring information
81 *
82 * Update the free dw slots in the ring buffer (all asics).
83 */
84 void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
85 {
86 uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
87
88 /* This works because ring_size is a power of 2 */
89 ring->ring_free_dw = rptr + (ring->ring_size / 4);
90 ring->ring_free_dw -= ring->wptr;
91 ring->ring_free_dw &= ring->ptr_mask;
92 if (!ring->ring_free_dw) {
93 /* this is an empty ring */
94 ring->ring_free_dw = ring->ring_size / 4;
95 /* update lockup info to avoid false positive */
96 radeon_ring_lockup_update(rdev, ring);
97 }
98 }
99
100 /**
101 * radeon_ring_alloc - allocate space on the ring buffer
102 *
103 * @rdev: radeon_device pointer
104 * @ring: radeon_ring structure holding ring information
105 * @ndw: number of dwords to allocate in the ring buffer
106 *
107 * Allocate @ndw dwords in the ring buffer (all asics).
108 * Returns 0 on success, error on failure.
109 */
110 int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
111 {
112 int r;
113
114 /* make sure we aren't trying to allocate more space than there is on the ring */
115 if (ndw > (ring->ring_size / 4))
116 return -ENOMEM;
117 /* Align requested size with padding so unlock_commit can
118 * pad safely */
119 radeon_ring_free_size(rdev, ring);
120 ndw = (ndw + ring->align_mask) & ~ring->align_mask;
121 while (ndw > (ring->ring_free_dw - 1)) {
122 radeon_ring_free_size(rdev, ring);
123 if (ndw < ring->ring_free_dw) {
124 break;
125 }
126 r = radeon_fence_wait_next(rdev, ring->idx);
127 if (r)
128 return r;
129 }
130 ring->count_dw = ndw;
131 ring->wptr_old = ring->wptr;
132 return 0;
133 }
134
135 /**
136 * radeon_ring_lock - lock the ring and allocate space on it
137 *
138 * @rdev: radeon_device pointer
139 * @ring: radeon_ring structure holding ring information
140 * @ndw: number of dwords to allocate in the ring buffer
141 *
142 * Lock the ring and allocate @ndw dwords in the ring buffer
143 * (all asics).
144 * Returns 0 on success, error on failure.
145 */
146 int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
147 {
148 int r;
149
150 mutex_lock(&rdev->ring_lock);
151 r = radeon_ring_alloc(rdev, ring, ndw);
152 if (r) {
153 mutex_unlock(&rdev->ring_lock);
154 return r;
155 }
156 return 0;
157 }
158
159 /**
160 * radeon_ring_commit - tell the GPU to execute the new
161 * commands on the ring buffer
162 *
163 * @rdev: radeon_device pointer
164 * @ring: radeon_ring structure holding ring information
165 * @hdp_flush: Whether or not to perform an HDP cache flush
166 *
167 * Update the wptr (write pointer) to tell the GPU to
168 * execute new commands on the ring buffer (all asics).
169 */
170 void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring,
171 bool hdp_flush)
172 {
173 /* If we are emitting the HDP flush via the ring buffer, we need to
174 * do it before padding.
175 */
176 if (hdp_flush && rdev->asic->ring[ring->idx]->hdp_flush)
177 rdev->asic->ring[ring->idx]->hdp_flush(rdev, ring);
178 /* We pad to match fetch size */
179 while (ring->wptr & ring->align_mask) {
180 radeon_ring_write(ring, ring->nop);
181 }
182 mb();
183 /* If we are emitting the HDP flush via MMIO, we need to do it after
184 * all CPU writes to VRAM finished.
185 */
186 if (hdp_flush && rdev->asic->mmio_hdp_flush)
187 rdev->asic->mmio_hdp_flush(rdev);
188 radeon_ring_set_wptr(rdev, ring);
189 }
190
191 /**
192 * radeon_ring_unlock_commit - tell the GPU to execute the new
193 * commands on the ring buffer and unlock it
194 *
195 * @rdev: radeon_device pointer
196 * @ring: radeon_ring structure holding ring information
197 * @hdp_flush: Whether or not to perform an HDP cache flush
198 *
199 * Call radeon_ring_commit() then unlock the ring (all asics).
200 */
201 void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring,
202 bool hdp_flush)
203 {
204 radeon_ring_commit(rdev, ring, hdp_flush);
205 mutex_unlock(&rdev->ring_lock);
206 }
207
208 /**
209 * radeon_ring_undo - reset the wptr
210 *
211 * @ring: radeon_ring structure holding ring information
212 *
213 * Reset the driver's copy of the wptr (all asics).
214 */
215 void radeon_ring_undo(struct radeon_ring *ring)
216 {
217 ring->wptr = ring->wptr_old;
218 }
219
220 /**
221 * radeon_ring_unlock_undo - reset the wptr and unlock the ring
222 *
223 * @ring: radeon_ring structure holding ring information
224 *
225 * Call radeon_ring_undo() then unlock the ring (all asics).
226 */
227 void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
228 {
229 radeon_ring_undo(ring);
230 mutex_unlock(&rdev->ring_lock);
231 }
232
233 /**
234 * radeon_ring_lockup_update - update lockup variables
235 *
236 * @ring: radeon_ring structure holding ring information
237 *
238 * Update the last rptr value and timestamp (all asics).
239 */
240 void radeon_ring_lockup_update(struct radeon_device *rdev,
241 struct radeon_ring *ring)
242 {
243 atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring));
244 atomic64_set(&ring->last_activity, jiffies_64);
245 }
246
247 /**
248 * radeon_ring_test_lockup() - check if ring is lockedup by recording information
249 * @rdev: radeon device structure
250 * @ring: radeon_ring structure holding ring information
251 *
252 */
253 bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
254 {
255 uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
256 uint64_t last = atomic64_read(&ring->last_activity);
257 uint64_t elapsed;
258
259 if (rptr != atomic_read(&ring->last_rptr)) {
260 /* ring is still working, no lockup */
261 radeon_ring_lockup_update(rdev, ring);
262 return false;
263 }
264
265 elapsed = jiffies_to_msecs(jiffies_64 - last);
266 if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
267 dev_err(rdev->dev, "ring %d stalled for more than %"PRIu64"msec\n",
268 ring->idx, elapsed);
269 return true;
270 }
271 /* give a chance to the GPU ... */
272 return false;
273 }
274
275 /**
276 * radeon_ring_backup - Back up the content of a ring
277 *
278 * @rdev: radeon_device pointer
279 * @ring: the ring we want to back up
280 *
281 * Saves all unprocessed commits from a ring, returns the number of dwords saved.
282 */
283 unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
284 uint32_t **data)
285 {
286 unsigned size, ptr, i;
287
288 /* just in case lock the ring */
289 mutex_lock(&rdev->ring_lock);
290 *data = NULL;
291
292 if (ring->ring_obj == NULL) {
293 mutex_unlock(&rdev->ring_lock);
294 return 0;
295 }
296
297 /* it doesn't make sense to save anything if all fences are signaled */
298 if (!radeon_fence_count_emitted(rdev, ring->idx)) {
299 mutex_unlock(&rdev->ring_lock);
300 return 0;
301 }
302
303 /* calculate the number of dw on the ring */
304 if (ring->rptr_save_reg)
305 ptr = RREG32(ring->rptr_save_reg);
306 else if (rdev->wb.enabled)
307 ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
308 else {
309 /* no way to read back the next rptr */
310 mutex_unlock(&rdev->ring_lock);
311 return 0;
312 }
313
314 size = ring->wptr + (ring->ring_size / 4);
315 size -= ptr;
316 size &= ring->ptr_mask;
317 if (size == 0) {
318 mutex_unlock(&rdev->ring_lock);
319 return 0;
320 }
321
322 /* and then save the content of the ring */
323 *data = drm_malloc_ab(size, sizeof(uint32_t));
324 if (!*data) {
325 mutex_unlock(&rdev->ring_lock);
326 return 0;
327 }
328 for (i = 0; i < size; ++i) {
329 (*data)[i] = ring->ring[ptr++];
330 ptr &= ring->ptr_mask;
331 }
332
333 mutex_unlock(&rdev->ring_lock);
334 return size;
335 }
336
337 /**
338 * radeon_ring_restore - append saved commands to the ring again
339 *
340 * @rdev: radeon_device pointer
341 * @ring: ring to append commands to
342 * @size: number of dwords we want to write
343 * @data: saved commands
344 *
345 * Allocates space on the ring and restore the previously saved commands.
346 */
347 int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
348 unsigned size, uint32_t *data)
349 {
350 int i, r;
351
352 if (!size || !data)
353 return 0;
354
355 /* restore the saved ring content */
356 r = radeon_ring_lock(rdev, ring, size);
357 if (r)
358 return r;
359
360 for (i = 0; i < size; ++i) {
361 radeon_ring_write(ring, data[i]);
362 }
363
364 radeon_ring_unlock_commit(rdev, ring, false);
365 drm_free_large(data);
366 return 0;
367 }
368
369 /**
370 * radeon_ring_init - init driver ring struct.
371 *
372 * @rdev: radeon_device pointer
373 * @ring: radeon_ring structure holding ring information
374 * @ring_size: size of the ring
375 * @rptr_offs: offset of the rptr writeback location in the WB buffer
376 * @nop: nop packet for this ring
377 *
378 * Initialize the driver information for the selected ring (all asics).
379 * Returns 0 on success, error on failure.
380 */
381 int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
382 unsigned rptr_offs, u32 nop)
383 {
384 int r;
385
386 ring->ring_size = ring_size;
387 ring->rptr_offs = rptr_offs;
388 ring->nop = nop;
389 /* Allocate ring buffer */
390 if (ring->ring_obj == NULL) {
391 r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
392 RADEON_GEM_DOMAIN_GTT, 0, NULL,
393 NULL, &ring->ring_obj);
394 if (r) {
395 dev_err(rdev->dev, "(%d) ring create failed\n", r);
396 return r;
397 }
398 r = radeon_bo_reserve(ring->ring_obj, false);
399 if (unlikely(r != 0))
400 return r;
401 r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
402 &ring->gpu_addr);
403 if (r) {
404 radeon_bo_unreserve(ring->ring_obj);
405 dev_err(rdev->dev, "(%d) ring pin failed\n", r);
406 return r;
407 }
408 r = radeon_bo_kmap(ring->ring_obj,
409 (void **)__UNVOLATILE(&ring->ring));
410 radeon_bo_unreserve(ring->ring_obj);
411 if (r) {
412 dev_err(rdev->dev, "(%d) ring map failed\n", r);
413 return r;
414 }
415 }
416 ring->ptr_mask = (ring->ring_size / 4) - 1;
417 ring->ring_free_dw = ring->ring_size / 4;
418 if (rdev->wb.enabled) {
419 u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
420 ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
421 ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
422 }
423 if (radeon_debugfs_ring_init(rdev, ring)) {
424 DRM_ERROR("Failed to register debugfs file for rings !\n");
425 }
426 radeon_ring_lockup_update(rdev, ring);
427 return 0;
428 }
429
430 /**
431 * radeon_ring_fini - tear down the driver ring struct.
432 *
433 * @rdev: radeon_device pointer
434 * @ring: radeon_ring structure holding ring information
435 *
436 * Tear down the driver information for the selected ring (all asics).
437 */
438 void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
439 {
440 int r;
441 struct radeon_bo *ring_obj;
442
443 mutex_lock(&rdev->ring_lock);
444 ring_obj = ring->ring_obj;
445 ring->ready = false;
446 ring->ring = NULL;
447 ring->ring_obj = NULL;
448 mutex_unlock(&rdev->ring_lock);
449
450 if (ring_obj) {
451 r = radeon_bo_reserve(ring_obj, false);
452 if (likely(r == 0)) {
453 radeon_bo_kunmap(ring_obj);
454 radeon_bo_unpin(ring_obj);
455 radeon_bo_unreserve(ring_obj);
456 }
457 radeon_bo_unref(&ring_obj);
458 }
459 }
460
461 /*
462 * Debugfs info
463 */
464 #if defined(CONFIG_DEBUG_FS)
465
466 static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
467 {
468 struct drm_info_node *node = (struct drm_info_node *) m->private;
469 struct drm_device *dev = node->minor->dev;
470 struct radeon_device *rdev = dev->dev_private;
471 int ridx = *(int*)node->info_ent->data;
472 struct radeon_ring *ring = &rdev->ring[ridx];
473
474 uint32_t rptr, wptr, rptr_next;
475 unsigned count, i, j;
476
477 radeon_ring_free_size(rdev, ring);
478 count = (ring->ring_size / 4) - ring->ring_free_dw;
479
480 wptr = radeon_ring_get_wptr(rdev, ring);
481 seq_printf(m, "wptr: 0x%08x [%5d]\n",
482 wptr, wptr);
483
484 rptr = radeon_ring_get_rptr(rdev, ring);
485 seq_printf(m, "rptr: 0x%08x [%5d]\n",
486 rptr, rptr);
487
488 if (ring->rptr_save_reg) {
489 rptr_next = RREG32(ring->rptr_save_reg);
490 seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n",
491 ring->rptr_save_reg, rptr_next, rptr_next);
492 } else
493 rptr_next = ~0;
494
495 seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
496 ring->wptr, ring->wptr);
497 seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
498 ring->last_semaphore_signal_addr);
499 seq_printf(m, "last semaphore wait addr : 0x%016llx\n",
500 ring->last_semaphore_wait_addr);
501 seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
502 seq_printf(m, "%u dwords in ring\n", count);
503
504 if (!ring->ring)
505 return 0;
506
507 /* print 8 dw before current rptr as often it's the last executed
508 * packet that is the root issue
509 */
510 i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
511 for (j = 0; j <= (count + 32); j++) {
512 seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
513 if (rptr == i)
514 seq_puts(m, " *");
515 if (rptr_next == i)
516 seq_puts(m, " #");
517 seq_puts(m, "\n");
518 i = (i + 1) & ring->ptr_mask;
519 }
520 return 0;
521 }
522
523 static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
524 static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
525 static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
526 static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
527 static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
528 static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
529 static int si_vce1_index = TN_RING_TYPE_VCE1_INDEX;
530 static int si_vce2_index = TN_RING_TYPE_VCE2_INDEX;
531
532 static struct drm_info_list radeon_debugfs_ring_info_list[] = {
533 {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
534 {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
535 {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
536 {"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
537 {"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
538 {"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
539 {"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index},
540 {"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index},
541 };
542
543 #endif
544
545 static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
546 {
547 #if defined(CONFIG_DEBUG_FS)
548 unsigned i;
549 for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
550 struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
551 int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
552 unsigned r;
553
554 if (&rdev->ring[ridx] != ring)
555 continue;
556
557 r = radeon_debugfs_add_files(rdev, info, 1);
558 if (r)
559 return r;
560 }
561 #endif
562 return 0;
563 }
564