Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 1 | /* |
| 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 |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 27 | * Christian König |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 28 | */ |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/drmP.h> |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 30 | #include "radeon.h" |
Christian König | 7bd560e | 2012-05-02 15:11:12 +0200 | [diff] [blame] | 31 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 32 | /* |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 33 | * Rings |
| 34 | * Most engines on the GPU are fed via ring buffers. Ring |
| 35 | * buffers are areas of GPU accessible memory that the host |
| 36 | * writes commands into and the GPU reads commands out of. |
| 37 | * There is a rptr (read pointer) that determines where the |
| 38 | * GPU is currently reading, and a wptr (write pointer) |
| 39 | * which determines where the host has written. When the |
| 40 | * pointers are equal, the ring is idle. When the host |
| 41 | * writes commands to the ring buffer, it increments the |
| 42 | * wptr. The GPU then starts fetching commands and executes |
| 43 | * them until the pointers are equal again. |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 44 | */ |
Lauri Kasanen | 1109ca0 | 2012-08-31 13:43:50 -0400 | [diff] [blame] | 45 | static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring); |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 46 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 47 | /** |
| 48 | * radeon_ring_write - write a value to the ring |
| 49 | * |
| 50 | * @ring: radeon_ring structure holding ring information |
| 51 | * @v: dword (dw) value to write |
| 52 | * |
| 53 | * Write a value to the requested ring buffer (all asics). |
| 54 | */ |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 55 | void radeon_ring_write(struct radeon_ring *ring, uint32_t v) |
| 56 | { |
| 57 | #if DRM_DEBUG_CODE |
| 58 | if (ring->count_dw <= 0) { |
Thomas Friebel | 8ad33cd | 2012-10-15 13:16:22 -0400 | [diff] [blame] | 59 | DRM_ERROR("radeon: writing more dwords to the ring than expected!\n"); |
Jerome Glisse | c507f7e | 2012-05-09 15:34:58 +0200 | [diff] [blame] | 60 | } |
| 61 | #endif |
| 62 | ring->ring[ring->wptr++] = v; |
| 63 | ring->wptr &= ring->ptr_mask; |
| 64 | ring->count_dw--; |
| 65 | ring->ring_free_dw--; |
| 66 | } |
| 67 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 68 | /** |
| 69 | * radeon_ring_supports_scratch_reg - check if the ring supports |
| 70 | * writing to scratch registers |
| 71 | * |
| 72 | * @rdev: radeon_device pointer |
| 73 | * @ring: radeon_ring structure holding ring information |
| 74 | * |
| 75 | * Check if a specific ring supports writing to scratch registers (all asics). |
| 76 | * Returns true if the ring supports writing to scratch regs, false if not. |
| 77 | */ |
Alex Deucher | 89d3580 | 2012-07-17 14:02:31 -0400 | [diff] [blame] | 78 | bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev, |
| 79 | struct radeon_ring *ring) |
| 80 | { |
| 81 | switch (ring->idx) { |
| 82 | case RADEON_RING_TYPE_GFX_INDEX: |
| 83 | case CAYMAN_RING_TYPE_CP1_INDEX: |
| 84 | case CAYMAN_RING_TYPE_CP2_INDEX: |
| 85 | return true; |
| 86 | default: |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 91 | /** |
| 92 | * radeon_ring_free_size - update the free size |
| 93 | * |
| 94 | * @rdev: radeon_device pointer |
| 95 | * @ring: radeon_ring structure holding ring information |
| 96 | * |
| 97 | * Update the free dw slots in the ring buffer (all asics). |
| 98 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 99 | void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 100 | { |
Christian König | ff212f2 | 2014-02-18 14:52:33 +0100 | [diff] [blame] | 101 | uint32_t rptr = radeon_ring_get_rptr(rdev, ring); |
| 102 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 103 | /* This works because ring_size is a power of 2 */ |
Christian König | ff212f2 | 2014-02-18 14:52:33 +0100 | [diff] [blame] | 104 | ring->ring_free_dw = rptr + (ring->ring_size / 4); |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 105 | ring->ring_free_dw -= ring->wptr; |
| 106 | ring->ring_free_dw &= ring->ptr_mask; |
| 107 | if (!ring->ring_free_dw) { |
Christian König | 82dc62a | 2014-02-18 15:03:22 +0100 | [diff] [blame] | 108 | /* this is an empty ring */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 109 | ring->ring_free_dw = ring->ring_size / 4; |
Christian König | 82dc62a | 2014-02-18 15:03:22 +0100 | [diff] [blame] | 110 | /* update lockup info to avoid false positive */ |
| 111 | radeon_ring_lockup_update(rdev, ring); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 115 | /** |
| 116 | * radeon_ring_alloc - allocate space on the ring buffer |
| 117 | * |
| 118 | * @rdev: radeon_device pointer |
| 119 | * @ring: radeon_ring structure holding ring information |
| 120 | * @ndw: number of dwords to allocate in the ring buffer |
| 121 | * |
| 122 | * Allocate @ndw dwords in the ring buffer (all asics). |
| 123 | * Returns 0 on success, error on failure. |
| 124 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 125 | int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 126 | { |
| 127 | int r; |
| 128 | |
Alex Deucher | fd5d93a | 2013-01-30 14:24:09 -0500 | [diff] [blame] | 129 | /* make sure we aren't trying to allocate more space than there is on the ring */ |
| 130 | if (ndw > (ring->ring_size / 4)) |
| 131 | return -ENOMEM; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 132 | /* Align requested size with padding so unlock_commit can |
| 133 | * pad safely */ |
Jerome Glisse | 8444d5c | 2013-06-19 10:02:28 -0400 | [diff] [blame] | 134 | radeon_ring_free_size(rdev, ring); |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 135 | ndw = (ndw + ring->align_mask) & ~ring->align_mask; |
| 136 | while (ndw > (ring->ring_free_dw - 1)) { |
| 137 | radeon_ring_free_size(rdev, ring); |
| 138 | if (ndw < ring->ring_free_dw) { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 139 | break; |
| 140 | } |
Christian König | 3761552 | 2014-02-18 15:58:31 +0100 | [diff] [blame] | 141 | r = radeon_fence_wait_next(rdev, ring->idx); |
Matthew Garrett | 91700f3 | 2010-04-30 15:24:17 -0400 | [diff] [blame] | 142 | if (r) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 143 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 144 | } |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 145 | ring->count_dw = ndw; |
| 146 | ring->wptr_old = ring->wptr; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 150 | /** |
| 151 | * radeon_ring_lock - lock the ring and allocate space on it |
| 152 | * |
| 153 | * @rdev: radeon_device pointer |
| 154 | * @ring: radeon_ring structure holding ring information |
| 155 | * @ndw: number of dwords to allocate in the ring buffer |
| 156 | * |
| 157 | * Lock the ring and allocate @ndw dwords in the ring buffer |
| 158 | * (all asics). |
| 159 | * Returns 0 on success, error on failure. |
| 160 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 161 | int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw) |
Matthew Garrett | 91700f3 | 2010-04-30 15:24:17 -0400 | [diff] [blame] | 162 | { |
| 163 | int r; |
| 164 | |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 165 | mutex_lock(&rdev->ring_lock); |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 166 | r = radeon_ring_alloc(rdev, ring, ndw); |
Matthew Garrett | 91700f3 | 2010-04-30 15:24:17 -0400 | [diff] [blame] | 167 | if (r) { |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 168 | mutex_unlock(&rdev->ring_lock); |
Matthew Garrett | 91700f3 | 2010-04-30 15:24:17 -0400 | [diff] [blame] | 169 | return r; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 174 | /** |
| 175 | * radeon_ring_commit - tell the GPU to execute the new |
| 176 | * commands on the ring buffer |
| 177 | * |
| 178 | * @rdev: radeon_device pointer |
| 179 | * @ring: radeon_ring structure holding ring information |
| 180 | * |
| 181 | * Update the wptr (write pointer) to tell the GPU to |
| 182 | * execute new commands on the ring buffer (all asics). |
| 183 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 184 | void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 185 | { |
Michel Dänzer | 72a9987 | 2014-07-31 18:43:49 +0900 | [diff] [blame] | 186 | /* If we are emitting the HDP flush via the ring buffer, we need to |
| 187 | * do it before padding. |
| 188 | */ |
| 189 | if (rdev->asic->ring[ring->idx]->hdp_flush) |
| 190 | rdev->asic->ring[ring->idx]->hdp_flush(rdev, ring); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 191 | /* We pad to match fetch size */ |
Christian König | 07a7133 | 2012-07-07 12:11:32 +0200 | [diff] [blame] | 192 | while (ring->wptr & ring->align_mask) { |
Alex Deucher | 78c5560 | 2011-11-17 14:25:56 -0500 | [diff] [blame] | 193 | radeon_ring_write(ring, ring->nop); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 194 | } |
Daniel Vetter | 85b2331 | 2013-12-11 11:34:45 +0100 | [diff] [blame] | 195 | mb(); |
Michel Dänzer | 72a9987 | 2014-07-31 18:43:49 +0900 | [diff] [blame] | 196 | /* If we are emitting the HDP flush via MMIO, we need to do it after |
| 197 | * all CPU writes to VRAM finished. |
| 198 | */ |
| 199 | if (rdev->asic->mmio_hdp_flush) |
| 200 | rdev->asic->mmio_hdp_flush(rdev); |
Alex Deucher | f93bdef | 2013-01-29 14:10:56 -0500 | [diff] [blame] | 201 | radeon_ring_set_wptr(rdev, ring); |
Matthew Garrett | 91700f3 | 2010-04-30 15:24:17 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 204 | /** |
| 205 | * radeon_ring_unlock_commit - tell the GPU to execute the new |
| 206 | * commands on the ring buffer and unlock it |
| 207 | * |
| 208 | * @rdev: radeon_device pointer |
| 209 | * @ring: radeon_ring structure holding ring information |
| 210 | * |
| 211 | * Call radeon_ring_commit() then unlock the ring (all asics). |
| 212 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 213 | void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring) |
Matthew Garrett | 91700f3 | 2010-04-30 15:24:17 -0400 | [diff] [blame] | 214 | { |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 215 | radeon_ring_commit(rdev, ring); |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 216 | mutex_unlock(&rdev->ring_lock); |
| 217 | } |
| 218 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 219 | /** |
| 220 | * radeon_ring_undo - reset the wptr |
| 221 | * |
| 222 | * @ring: radeon_ring structure holding ring information |
| 223 | * |
Paul Bolle | 501f9d4c | 2012-11-20 22:31:06 +0100 | [diff] [blame] | 224 | * Reset the driver's copy of the wptr (all asics). |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 225 | */ |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 226 | void radeon_ring_undo(struct radeon_ring *ring) |
| 227 | { |
| 228 | ring->wptr = ring->wptr_old; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 231 | /** |
| 232 | * radeon_ring_unlock_undo - reset the wptr and unlock the ring |
| 233 | * |
| 234 | * @ring: radeon_ring structure holding ring information |
| 235 | * |
| 236 | * Call radeon_ring_undo() then unlock the ring (all asics). |
| 237 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 238 | void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 239 | { |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 240 | radeon_ring_undo(ring); |
| 241 | mutex_unlock(&rdev->ring_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 244 | /** |
Paul Bolle | 501f9d4c | 2012-11-20 22:31:06 +0100 | [diff] [blame] | 245 | * radeon_ring_lockup_update - update lockup variables |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 246 | * |
| 247 | * @ring: radeon_ring structure holding ring information |
| 248 | * |
| 249 | * Update the last rptr value and timestamp (all asics). |
| 250 | */ |
Christian König | ff212f2 | 2014-02-18 14:52:33 +0100 | [diff] [blame] | 251 | void radeon_ring_lockup_update(struct radeon_device *rdev, |
| 252 | struct radeon_ring *ring) |
Christian König | 069211e | 2012-05-02 15:11:20 +0200 | [diff] [blame] | 253 | { |
Christian König | aee4aa7 | 2014-02-18 15:24:06 +0100 | [diff] [blame] | 254 | atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring)); |
| 255 | atomic64_set(&ring->last_activity, jiffies_64); |
Christian König | 069211e | 2012-05-02 15:11:20 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | /** |
| 259 | * radeon_ring_test_lockup() - check if ring is lockedup by recording information |
| 260 | * @rdev: radeon device structure |
| 261 | * @ring: radeon_ring structure holding ring information |
| 262 | * |
Christian König | 2d2fe3f | 2014-02-18 12:37:50 +0100 | [diff] [blame] | 263 | */ |
Christian König | 069211e | 2012-05-02 15:11:20 +0200 | [diff] [blame] | 264 | bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring) |
| 265 | { |
Christian König | ff212f2 | 2014-02-18 14:52:33 +0100 | [diff] [blame] | 266 | uint32_t rptr = radeon_ring_get_rptr(rdev, ring); |
Christian König | aee4aa7 | 2014-02-18 15:24:06 +0100 | [diff] [blame] | 267 | uint64_t last = atomic64_read(&ring->last_activity); |
| 268 | uint64_t elapsed; |
Christian König | 069211e | 2012-05-02 15:11:20 +0200 | [diff] [blame] | 269 | |
Christian König | aee4aa7 | 2014-02-18 15:24:06 +0100 | [diff] [blame] | 270 | if (rptr != atomic_read(&ring->last_rptr)) { |
| 271 | /* ring is still working, no lockup */ |
Christian König | ff212f2 | 2014-02-18 14:52:33 +0100 | [diff] [blame] | 272 | radeon_ring_lockup_update(rdev, ring); |
Christian König | 069211e | 2012-05-02 15:11:20 +0200 | [diff] [blame] | 273 | return false; |
| 274 | } |
Christian König | aee4aa7 | 2014-02-18 15:24:06 +0100 | [diff] [blame] | 275 | |
| 276 | elapsed = jiffies_to_msecs(jiffies_64 - last); |
Christian König | 3368ff0 | 2012-05-02 15:11:21 +0200 | [diff] [blame] | 277 | if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) { |
Christian König | aee4aa7 | 2014-02-18 15:24:06 +0100 | [diff] [blame] | 278 | dev_err(rdev->dev, "ring %d stalled for more than %llumsec\n", |
| 279 | ring->idx, elapsed); |
Christian König | 069211e | 2012-05-02 15:11:20 +0200 | [diff] [blame] | 280 | return true; |
| 281 | } |
| 282 | /* give a chance to the GPU ... */ |
| 283 | return false; |
| 284 | } |
| 285 | |
Christian König | 55d7c22 | 2012-07-09 11:52:44 +0200 | [diff] [blame] | 286 | /** |
| 287 | * radeon_ring_backup - Back up the content of a ring |
| 288 | * |
| 289 | * @rdev: radeon_device pointer |
| 290 | * @ring: the ring we want to back up |
| 291 | * |
| 292 | * Saves all unprocessed commits from a ring, returns the number of dwords saved. |
| 293 | */ |
| 294 | unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring, |
| 295 | uint32_t **data) |
| 296 | { |
| 297 | unsigned size, ptr, i; |
Christian König | 55d7c22 | 2012-07-09 11:52:44 +0200 | [diff] [blame] | 298 | |
| 299 | /* just in case lock the ring */ |
| 300 | mutex_lock(&rdev->ring_lock); |
| 301 | *data = NULL; |
| 302 | |
Alex Deucher | 89d3580 | 2012-07-17 14:02:31 -0400 | [diff] [blame] | 303 | if (ring->ring_obj == NULL) { |
Christian König | 55d7c22 | 2012-07-09 11:52:44 +0200 | [diff] [blame] | 304 | mutex_unlock(&rdev->ring_lock); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* it doesn't make sense to save anything if all fences are signaled */ |
Alex Deucher | 8b25ed3 | 2012-07-17 14:02:30 -0400 | [diff] [blame] | 309 | if (!radeon_fence_count_emitted(rdev, ring->idx)) { |
Christian König | 55d7c22 | 2012-07-09 11:52:44 +0200 | [diff] [blame] | 310 | mutex_unlock(&rdev->ring_lock); |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | /* calculate the number of dw on the ring */ |
Alex Deucher | 89d3580 | 2012-07-17 14:02:31 -0400 | [diff] [blame] | 315 | if (ring->rptr_save_reg) |
| 316 | ptr = RREG32(ring->rptr_save_reg); |
| 317 | else if (rdev->wb.enabled) |
| 318 | ptr = le32_to_cpu(*ring->next_rptr_cpu_addr); |
| 319 | else { |
| 320 | /* no way to read back the next rptr */ |
| 321 | mutex_unlock(&rdev->ring_lock); |
| 322 | return 0; |
| 323 | } |
| 324 | |
Christian König | 55d7c22 | 2012-07-09 11:52:44 +0200 | [diff] [blame] | 325 | size = ring->wptr + (ring->ring_size / 4); |
| 326 | size -= ptr; |
| 327 | size &= ring->ptr_mask; |
| 328 | if (size == 0) { |
| 329 | mutex_unlock(&rdev->ring_lock); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /* and then save the content of the ring */ |
Dan Carpenter | 1e179d4e | 2012-07-20 14:17:00 +0300 | [diff] [blame] | 334 | *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL); |
| 335 | if (!*data) { |
| 336 | mutex_unlock(&rdev->ring_lock); |
| 337 | return 0; |
| 338 | } |
Christian König | 55d7c22 | 2012-07-09 11:52:44 +0200 | [diff] [blame] | 339 | for (i = 0; i < size; ++i) { |
| 340 | (*data)[i] = ring->ring[ptr++]; |
| 341 | ptr &= ring->ptr_mask; |
| 342 | } |
| 343 | |
| 344 | mutex_unlock(&rdev->ring_lock); |
| 345 | return size; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * radeon_ring_restore - append saved commands to the ring again |
| 350 | * |
| 351 | * @rdev: radeon_device pointer |
| 352 | * @ring: ring to append commands to |
| 353 | * @size: number of dwords we want to write |
| 354 | * @data: saved commands |
| 355 | * |
| 356 | * Allocates space on the ring and restore the previously saved commands. |
| 357 | */ |
| 358 | int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring, |
| 359 | unsigned size, uint32_t *data) |
| 360 | { |
| 361 | int i, r; |
| 362 | |
| 363 | if (!size || !data) |
| 364 | return 0; |
| 365 | |
| 366 | /* restore the saved ring content */ |
| 367 | r = radeon_ring_lock(rdev, ring, size); |
| 368 | if (r) |
| 369 | return r; |
| 370 | |
| 371 | for (i = 0; i < size; ++i) { |
| 372 | radeon_ring_write(ring, data[i]); |
| 373 | } |
| 374 | |
| 375 | radeon_ring_unlock_commit(rdev, ring); |
| 376 | kfree(data); |
| 377 | return 0; |
| 378 | } |
| 379 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 380 | /** |
| 381 | * radeon_ring_init - init driver ring struct. |
| 382 | * |
| 383 | * @rdev: radeon_device pointer |
| 384 | * @ring: radeon_ring structure holding ring information |
| 385 | * @ring_size: size of the ring |
| 386 | * @rptr_offs: offset of the rptr writeback location in the WB buffer |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 387 | * @nop: nop packet for this ring |
| 388 | * |
| 389 | * Initialize the driver information for the selected ring (all asics). |
| 390 | * Returns 0 on success, error on failure. |
| 391 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 392 | int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size, |
Alex Deucher | ea31bf6 | 2013-12-09 19:44:30 -0500 | [diff] [blame] | 393 | unsigned rptr_offs, u32 nop) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 394 | { |
| 395 | int r; |
| 396 | |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 397 | ring->ring_size = ring_size; |
| 398 | ring->rptr_offs = rptr_offs; |
Alex Deucher | 78c5560 | 2011-11-17 14:25:56 -0500 | [diff] [blame] | 399 | ring->nop = nop; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 400 | /* Allocate ring buffer */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 401 | if (ring->ring_obj == NULL) { |
| 402 | r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true, |
Michel Dänzer | 1490434 | 2014-07-29 18:47:20 +0900 | [diff] [blame] | 403 | RADEON_GEM_DOMAIN_GTT, |
| 404 | (rdev->flags & RADEON_IS_PCIE) ? |
| 405 | RADEON_GEM_GTT_WC : 0, |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 406 | NULL, &ring->ring_obj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 407 | if (r) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 408 | dev_err(rdev->dev, "(%d) ring create failed\n", r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 409 | return r; |
| 410 | } |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 411 | r = radeon_bo_reserve(ring->ring_obj, false); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 412 | if (unlikely(r != 0)) |
| 413 | return r; |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 414 | r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT, |
| 415 | &ring->gpu_addr); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 416 | if (r) { |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 417 | radeon_bo_unreserve(ring->ring_obj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 418 | dev_err(rdev->dev, "(%d) ring pin failed\n", r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 419 | return r; |
| 420 | } |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 421 | r = radeon_bo_kmap(ring->ring_obj, |
| 422 | (void **)&ring->ring); |
| 423 | radeon_bo_unreserve(ring->ring_obj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 424 | if (r) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 425 | dev_err(rdev->dev, "(%d) ring map failed\n", r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 426 | return r; |
| 427 | } |
| 428 | } |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 429 | ring->ptr_mask = (ring->ring_size / 4) - 1; |
| 430 | ring->ring_free_dw = ring->ring_size / 4; |
Alex Deucher | 89d3580 | 2012-07-17 14:02:31 -0400 | [diff] [blame] | 431 | if (rdev->wb.enabled) { |
| 432 | u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4); |
| 433 | ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index; |
| 434 | ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4]; |
| 435 | } |
Christian König | ec1a6cc | 2012-05-02 15:11:11 +0200 | [diff] [blame] | 436 | if (radeon_debugfs_ring_init(rdev, ring)) { |
| 437 | DRM_ERROR("Failed to register debugfs file for rings !\n"); |
| 438 | } |
Christian König | ff212f2 | 2014-02-18 14:52:33 +0100 | [diff] [blame] | 439 | radeon_ring_lockup_update(rdev, ring); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | |
Alex Deucher | 7592328 | 2012-07-17 14:02:38 -0400 | [diff] [blame] | 443 | /** |
| 444 | * radeon_ring_fini - tear down the driver ring struct. |
| 445 | * |
| 446 | * @rdev: radeon_device pointer |
| 447 | * @ring: radeon_ring structure holding ring information |
| 448 | * |
| 449 | * Tear down the driver information for the selected ring (all asics). |
| 450 | */ |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 451 | void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 452 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 453 | int r; |
Alex Deucher | ca2af92 | 2010-05-06 11:02:24 -0400 | [diff] [blame] | 454 | struct radeon_bo *ring_obj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 455 | |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 456 | mutex_lock(&rdev->ring_lock); |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 457 | ring_obj = ring->ring_obj; |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 458 | ring->ready = false; |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 459 | ring->ring = NULL; |
| 460 | ring->ring_obj = NULL; |
Christian König | d6999bc | 2012-05-09 15:34:45 +0200 | [diff] [blame] | 461 | mutex_unlock(&rdev->ring_lock); |
Alex Deucher | ca2af92 | 2010-05-06 11:02:24 -0400 | [diff] [blame] | 462 | |
| 463 | if (ring_obj) { |
| 464 | r = radeon_bo_reserve(ring_obj, false); |
| 465 | if (likely(r == 0)) { |
| 466 | radeon_bo_kunmap(ring_obj); |
| 467 | radeon_bo_unpin(ring_obj); |
| 468 | radeon_bo_unreserve(ring_obj); |
| 469 | } |
| 470 | radeon_bo_unref(&ring_obj); |
| 471 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 474 | /* |
| 475 | * Debugfs info |
| 476 | */ |
| 477 | #if defined(CONFIG_DEBUG_FS) |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 478 | |
| 479 | static int radeon_debugfs_ring_info(struct seq_file *m, void *data) |
| 480 | { |
| 481 | struct drm_info_node *node = (struct drm_info_node *) m->private; |
| 482 | struct drm_device *dev = node->minor->dev; |
| 483 | struct radeon_device *rdev = dev->dev_private; |
| 484 | int ridx = *(int*)node->info_ent->data; |
| 485 | struct radeon_ring *ring = &rdev->ring[ridx]; |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 486 | |
| 487 | uint32_t rptr, wptr, rptr_next; |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 488 | unsigned count, i, j; |
| 489 | |
| 490 | radeon_ring_free_size(rdev, ring); |
| 491 | count = (ring->ring_size / 4) - ring->ring_free_dw; |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 492 | |
| 493 | wptr = radeon_ring_get_wptr(rdev, ring); |
Alex Deucher | ea31bf6 | 2013-12-09 19:44:30 -0500 | [diff] [blame] | 494 | seq_printf(m, "wptr: 0x%08x [%5d]\n", |
| 495 | wptr, wptr); |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 496 | |
| 497 | rptr = radeon_ring_get_rptr(rdev, ring); |
Alex Deucher | ea31bf6 | 2013-12-09 19:44:30 -0500 | [diff] [blame] | 498 | seq_printf(m, "rptr: 0x%08x [%5d]\n", |
| 499 | rptr, rptr); |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 500 | |
Christian König | 45df680 | 2012-07-06 16:22:55 +0200 | [diff] [blame] | 501 | if (ring->rptr_save_reg) { |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 502 | rptr_next = RREG32(ring->rptr_save_reg); |
| 503 | seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n", |
| 504 | ring->rptr_save_reg, rptr_next, rptr_next); |
| 505 | } else |
| 506 | rptr_next = ~0; |
| 507 | |
| 508 | seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n", |
| 509 | ring->wptr, ring->wptr); |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 510 | seq_printf(m, "last semaphore signal addr : 0x%016llx\n", |
| 511 | ring->last_semaphore_signal_addr); |
| 512 | seq_printf(m, "last semaphore wait addr : 0x%016llx\n", |
| 513 | ring->last_semaphore_wait_addr); |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 514 | seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw); |
| 515 | seq_printf(m, "%u dwords in ring\n", count); |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 516 | |
| 517 | if (!ring->ready) |
| 518 | return 0; |
| 519 | |
Jerome Glisse | 4d00919 | 2013-01-02 17:30:34 -0500 | [diff] [blame] | 520 | /* print 8 dw before current rptr as often it's the last executed |
| 521 | * packet that is the root issue |
| 522 | */ |
Christian König | df893a2 | 2013-12-12 09:42:37 +0100 | [diff] [blame] | 523 | i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask; |
| 524 | for (j = 0; j <= (count + 32); j++) { |
| 525 | seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]); |
| 526 | if (rptr == i) |
| 527 | seq_puts(m, " *"); |
| 528 | if (rptr_next == i) |
| 529 | seq_puts(m, " #"); |
| 530 | seq_puts(m, "\n"); |
| 531 | i = (i + 1) & ring->ptr_mask; |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
Christian König | f2ba57b | 2013-04-08 12:41:29 +0200 | [diff] [blame] | 536 | static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX; |
| 537 | static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX; |
| 538 | static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX; |
| 539 | static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX; |
| 540 | static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX; |
| 541 | static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX; |
Christian König | d93f793 | 2013-05-23 12:10:04 +0200 | [diff] [blame] | 542 | static int si_vce1_index = TN_RING_TYPE_VCE1_INDEX; |
| 543 | static int si_vce2_index = TN_RING_TYPE_VCE2_INDEX; |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 544 | |
| 545 | static struct drm_info_list radeon_debugfs_ring_info_list[] = { |
Christian König | f2ba57b | 2013-04-08 12:41:29 +0200 | [diff] [blame] | 546 | {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index}, |
| 547 | {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index}, |
| 548 | {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index}, |
| 549 | {"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index}, |
| 550 | {"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index}, |
| 551 | {"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index}, |
Christian König | d93f793 | 2013-05-23 12:10:04 +0200 | [diff] [blame] | 552 | {"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index}, |
| 553 | {"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index}, |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 554 | }; |
| 555 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 556 | #endif |
| 557 | |
Lauri Kasanen | 1109ca0 | 2012-08-31 13:43:50 -0400 | [diff] [blame] | 558 | static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring) |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 559 | { |
| 560 | #if defined(CONFIG_DEBUG_FS) |
Christian König | ec1a6cc | 2012-05-02 15:11:11 +0200 | [diff] [blame] | 561 | unsigned i; |
| 562 | for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) { |
| 563 | struct drm_info_list *info = &radeon_debugfs_ring_info_list[i]; |
| 564 | int ridx = *(int*)radeon_debugfs_ring_info_list[i].data; |
| 565 | unsigned r; |
| 566 | |
| 567 | if (&rdev->ring[ridx] != ring) |
| 568 | continue; |
| 569 | |
| 570 | r = radeon_debugfs_add_files(rdev, info, 1); |
| 571 | if (r) |
| 572 | return r; |
| 573 | } |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 574 | #endif |
Christian König | ec1a6cc | 2012-05-02 15:11:11 +0200 | [diff] [blame] | 575 | return 0; |
Christian König | af9720f | 2011-10-24 17:08:44 +0200 | [diff] [blame] | 576 | } |