blob: 81d06d772dde31ad2fa557e71efe79f55bb4caf8 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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 <linux/seq_file.h>
30#include <linux/slab.h>
31#include <drm/drmP.h>
32#include <drm/amdgpu_drm.h>
33#include "amdgpu.h"
34#include "atom.h"
35
36/*
37 * Rings
38 * Most engines on the GPU are fed via ring buffers. Ring
39 * buffers are areas of GPU accessible memory that the host
40 * writes commands into and the GPU reads commands out of.
41 * There is a rptr (read pointer) that determines where the
42 * GPU is currently reading, and a wptr (write pointer)
43 * which determines where the host has written. When the
44 * pointers are equal, the ring is idle. When the host
45 * writes commands to the ring buffer, it increments the
46 * wptr. The GPU then starts fetching commands and executes
47 * them until the pointers are equal again.
48 */
49static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring);
50
51/**
52 * amdgpu_ring_free_size - update the free size
53 *
54 * @adev: amdgpu_device pointer
55 * @ring: amdgpu_ring structure holding ring information
56 *
57 * Update the free dw slots in the ring buffer (all asics).
58 */
59void amdgpu_ring_free_size(struct amdgpu_ring *ring)
60{
61 uint32_t rptr = amdgpu_ring_get_rptr(ring);
62
63 /* This works because ring_size is a power of 2 */
64 ring->ring_free_dw = rptr + (ring->ring_size / 4);
65 ring->ring_free_dw -= ring->wptr;
66 ring->ring_free_dw &= ring->ptr_mask;
67 if (!ring->ring_free_dw) {
68 /* this is an empty ring */
69 ring->ring_free_dw = ring->ring_size / 4;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040070 }
71}
72
73/**
74 * amdgpu_ring_alloc - allocate space on the ring buffer
75 *
76 * @adev: amdgpu_device pointer
77 * @ring: amdgpu_ring structure holding ring information
78 * @ndw: number of dwords to allocate in the ring buffer
79 *
80 * Allocate @ndw dwords in the ring buffer (all asics).
81 * Returns 0 on success, error on failure.
82 */
83int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw)
84{
85 int r;
86
87 /* make sure we aren't trying to allocate more space than there is on the ring */
88 if (ndw > (ring->ring_size / 4))
89 return -ENOMEM;
90 /* Align requested size with padding so unlock_commit can
91 * pad safely */
92 amdgpu_ring_free_size(ring);
93 ndw = (ndw + ring->align_mask) & ~ring->align_mask;
94 while (ndw > (ring->ring_free_dw - 1)) {
95 amdgpu_ring_free_size(ring);
96 if (ndw < ring->ring_free_dw) {
97 break;
98 }
99 r = amdgpu_fence_wait_next(ring);
100 if (r)
101 return r;
102 }
103 ring->count_dw = ndw;
104 ring->wptr_old = ring->wptr;
105 return 0;
106}
107
Jammy Zhouedff0e22015-09-01 13:04:08 +0800108/** amdgpu_ring_insert_nop - insert NOP packets
109 *
110 * @ring: amdgpu_ring structure holding ring information
111 * @count: the number of NOP packets to insert
112 *
113 * This is the generic insert_nop function for rings except SDMA
114 */
115void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
116{
117 int i;
118
119 for (i = 0; i < count; i++)
120 amdgpu_ring_write(ring, ring->nop);
121}
122
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123/**
124 * amdgpu_ring_commit - tell the GPU to execute the new
125 * commands on the ring buffer
126 *
127 * @adev: amdgpu_device pointer
128 * @ring: amdgpu_ring structure holding ring information
129 *
130 * Update the wptr (write pointer) to tell the GPU to
131 * execute new commands on the ring buffer (all asics).
132 */
133void amdgpu_ring_commit(struct amdgpu_ring *ring)
134{
Jammy Zhouedff0e22015-09-01 13:04:08 +0800135 uint32_t count;
136
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 /* We pad to match fetch size */
Jammy Zhouedff0e22015-09-01 13:04:08 +0800138 count = ring->align_mask + 1 - (ring->wptr & ring->align_mask);
139 count %= ring->align_mask + 1;
140 ring->funcs->insert_nop(ring, count);
141
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 mb();
143 amdgpu_ring_set_wptr(ring);
144}
145
146/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400147 * amdgpu_ring_undo - reset the wptr
148 *
149 * @ring: amdgpu_ring structure holding ring information
150 *
151 * Reset the driver's copy of the wptr (all asics).
152 */
153void amdgpu_ring_undo(struct amdgpu_ring *ring)
154{
155 ring->wptr = ring->wptr_old;
156}
157
158/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 * amdgpu_ring_backup - Back up the content of a ring
160 *
161 * @ring: the ring we want to back up
162 *
163 * Saves all unprocessed commits from a ring, returns the number of dwords saved.
164 */
165unsigned amdgpu_ring_backup(struct amdgpu_ring *ring,
166 uint32_t **data)
167{
168 unsigned size, ptr, i;
169
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400170 *data = NULL;
171
Christian Königa27de352016-01-21 11:28:53 +0100172 if (ring->ring_obj == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174
175 /* it doesn't make sense to save anything if all fences are signaled */
Christian Königa27de352016-01-21 11:28:53 +0100176 if (!amdgpu_fence_count_emitted(ring))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400177 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178
179 ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
180
181 size = ring->wptr + (ring->ring_size / 4);
182 size -= ptr;
183 size &= ring->ptr_mask;
Christian Königa27de352016-01-21 11:28:53 +0100184 if (size == 0)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186
187 /* and then save the content of the ring */
188 *data = kmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
Christian Königa27de352016-01-21 11:28:53 +0100189 if (!*data)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400190 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191 for (i = 0; i < size; ++i) {
192 (*data)[i] = ring->ring[ptr++];
193 ptr &= ring->ptr_mask;
194 }
195
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196 return size;
197}
198
199/**
200 * amdgpu_ring_restore - append saved commands to the ring again
201 *
202 * @ring: ring to append commands to
203 * @size: number of dwords we want to write
204 * @data: saved commands
205 *
206 * Allocates space on the ring and restore the previously saved commands.
207 */
208int amdgpu_ring_restore(struct amdgpu_ring *ring,
209 unsigned size, uint32_t *data)
210{
211 int i, r;
212
213 if (!size || !data)
214 return 0;
215
216 /* restore the saved ring content */
Christian Königa27de352016-01-21 11:28:53 +0100217 r = amdgpu_ring_alloc(ring, size);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400218 if (r)
219 return r;
220
221 for (i = 0; i < size; ++i) {
222 amdgpu_ring_write(ring, data[i]);
223 }
224
Christian Königa27de352016-01-21 11:28:53 +0100225 amdgpu_ring_commit(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400226 kfree(data);
227 return 0;
228}
229
230/**
231 * amdgpu_ring_init - init driver ring struct.
232 *
233 * @adev: amdgpu_device pointer
234 * @ring: amdgpu_ring structure holding ring information
235 * @ring_size: size of the ring
236 * @nop: nop packet for this ring
237 *
238 * Initialize the driver information for the selected ring (all asics).
239 * Returns 0 on success, error on failure.
240 */
241int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
242 unsigned ring_size, u32 nop, u32 align_mask,
243 struct amdgpu_irq_src *irq_src, unsigned irq_type,
244 enum amdgpu_ring_type ring_type)
245{
246 u32 rb_bufsz;
247 int r;
248
249 if (ring->adev == NULL) {
250 if (adev->num_rings >= AMDGPU_MAX_RINGS)
251 return -EINVAL;
252
253 ring->adev = adev;
254 ring->idx = adev->num_rings++;
255 adev->rings[ring->idx] = ring;
Christian König4f839a22015-09-08 20:22:31 +0200256 r = amdgpu_fence_driver_init_ring(ring);
257 if (r)
258 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400259 }
260
261 r = amdgpu_wb_get(adev, &ring->rptr_offs);
262 if (r) {
263 dev_err(adev->dev, "(%d) ring rptr_offs wb alloc failed\n", r);
264 return r;
265 }
266
267 r = amdgpu_wb_get(adev, &ring->wptr_offs);
268 if (r) {
269 dev_err(adev->dev, "(%d) ring wptr_offs wb alloc failed\n", r);
270 return r;
271 }
272
273 r = amdgpu_wb_get(adev, &ring->fence_offs);
274 if (r) {
275 dev_err(adev->dev, "(%d) ring fence_offs wb alloc failed\n", r);
276 return r;
277 }
278
279 r = amdgpu_wb_get(adev, &ring->next_rptr_offs);
280 if (r) {
281 dev_err(adev->dev, "(%d) ring next_rptr wb alloc failed\n", r);
282 return r;
283 }
284 ring->next_rptr_gpu_addr = adev->wb.gpu_addr + (ring->next_rptr_offs * 4);
285 ring->next_rptr_cpu_addr = &adev->wb.wb[ring->next_rptr_offs];
Chunming Zhou176e1ab2015-07-24 10:49:47 +0800286 spin_lock_init(&ring->fence_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400287 r = amdgpu_fence_driver_start_ring(ring, irq_src, irq_type);
288 if (r) {
289 dev_err(adev->dev, "failed initializing fences (%d).\n", r);
290 return r;
291 }
292
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400293 /* Align ring size */
294 rb_bufsz = order_base_2(ring_size / 8);
295 ring_size = (1 << (rb_bufsz + 1)) * 4;
296 ring->ring_size = ring_size;
297 ring->align_mask = align_mask;
298 ring->nop = nop;
299 ring->type = ring_type;
300
301 /* Allocate ring buffer */
302 if (ring->ring_obj == NULL) {
303 r = amdgpu_bo_create(adev, ring->ring_size, PAGE_SIZE, true,
304 AMDGPU_GEM_DOMAIN_GTT, 0,
Christian König72d76682015-09-03 17:34:59 +0200305 NULL, NULL, &ring->ring_obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306 if (r) {
307 dev_err(adev->dev, "(%d) ring create failed\n", r);
308 return r;
309 }
310 r = amdgpu_bo_reserve(ring->ring_obj, false);
311 if (unlikely(r != 0))
312 return r;
313 r = amdgpu_bo_pin(ring->ring_obj, AMDGPU_GEM_DOMAIN_GTT,
314 &ring->gpu_addr);
315 if (r) {
316 amdgpu_bo_unreserve(ring->ring_obj);
317 dev_err(adev->dev, "(%d) ring pin failed\n", r);
318 return r;
319 }
320 r = amdgpu_bo_kmap(ring->ring_obj,
321 (void **)&ring->ring);
322 amdgpu_bo_unreserve(ring->ring_obj);
323 if (r) {
324 dev_err(adev->dev, "(%d) ring map failed\n", r);
325 return r;
326 }
327 }
328 ring->ptr_mask = (ring->ring_size / 4) - 1;
329 ring->ring_free_dw = ring->ring_size / 4;
330
331 if (amdgpu_debugfs_ring_init(adev, ring)) {
332 DRM_ERROR("Failed to register debugfs file for rings !\n");
333 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400334 return 0;
335}
336
337/**
338 * amdgpu_ring_fini - tear down the driver ring struct.
339 *
340 * @adev: amdgpu_device pointer
341 * @ring: amdgpu_ring structure holding ring information
342 *
343 * Tear down the driver information for the selected ring (all asics).
344 */
345void amdgpu_ring_fini(struct amdgpu_ring *ring)
346{
347 int r;
348 struct amdgpu_bo *ring_obj;
349
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400350 ring_obj = ring->ring_obj;
351 ring->ready = false;
352 ring->ring = NULL;
353 ring->ring_obj = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400354
355 amdgpu_wb_free(ring->adev, ring->fence_offs);
356 amdgpu_wb_free(ring->adev, ring->rptr_offs);
357 amdgpu_wb_free(ring->adev, ring->wptr_offs);
358 amdgpu_wb_free(ring->adev, ring->next_rptr_offs);
359
360 if (ring_obj) {
361 r = amdgpu_bo_reserve(ring_obj, false);
362 if (likely(r == 0)) {
363 amdgpu_bo_kunmap(ring_obj);
364 amdgpu_bo_unpin(ring_obj);
365 amdgpu_bo_unreserve(ring_obj);
366 }
367 amdgpu_bo_unref(&ring_obj);
368 }
369}
370
Christian König8120b612015-10-22 11:29:33 +0200371/**
372 * amdgpu_ring_from_fence - get ring from fence
373 *
374 * @f: fence structure
375 *
376 * Extract the ring a fence belongs to. Handles both scheduler as
377 * well as hardware fences.
378 */
379struct amdgpu_ring *amdgpu_ring_from_fence(struct fence *f)
380{
381 struct amdgpu_fence *a_fence;
382 struct amd_sched_fence *s_fence;
383
384 s_fence = to_amd_sched_fence(f);
385 if (s_fence)
386 return container_of(s_fence->sched, struct amdgpu_ring, sched);
387
388 a_fence = to_amdgpu_fence(f);
389 if (a_fence)
390 return a_fence->ring;
391
392 return NULL;
393}
394
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400395/*
396 * Debugfs info
397 */
398#if defined(CONFIG_DEBUG_FS)
399
400static int amdgpu_debugfs_ring_info(struct seq_file *m, void *data)
401{
402 struct drm_info_node *node = (struct drm_info_node *) m->private;
403 struct drm_device *dev = node->minor->dev;
404 struct amdgpu_device *adev = dev->dev_private;
405 int roffset = *(int*)node->info_ent->data;
406 struct amdgpu_ring *ring = (void *)(((uint8_t*)adev) + roffset);
407
408 uint32_t rptr, wptr, rptr_next;
409 unsigned count, i, j;
410
411 amdgpu_ring_free_size(ring);
412 count = (ring->ring_size / 4) - ring->ring_free_dw;
413
414 wptr = amdgpu_ring_get_wptr(ring);
415 seq_printf(m, "wptr: 0x%08x [%5d]\n",
416 wptr, wptr);
417
418 rptr = amdgpu_ring_get_rptr(ring);
419 seq_printf(m, "rptr: 0x%08x [%5d]\n",
420 rptr, rptr);
421
Christian König41f2d992016-01-21 12:56:52 +0100422 rptr_next = le32_to_cpu(*ring->next_rptr_cpu_addr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400423
424 seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
425 ring->wptr, ring->wptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400426 seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
427 seq_printf(m, "%u dwords in ring\n", count);
428
429 if (!ring->ready)
430 return 0;
431
432 /* print 8 dw before current rptr as often it's the last executed
433 * packet that is the root issue
434 */
435 i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
436 for (j = 0; j <= (count + 32); j++) {
437 seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
438 if (rptr == i)
439 seq_puts(m, " *");
440 if (rptr_next == i)
441 seq_puts(m, " #");
442 seq_puts(m, "\n");
443 i = (i + 1) & ring->ptr_mask;
444 }
445 return 0;
446}
447
448/* TODO: clean this up !*/
449static int amdgpu_gfx_index = offsetof(struct amdgpu_device, gfx.gfx_ring[0]);
450static int cayman_cp1_index = offsetof(struct amdgpu_device, gfx.compute_ring[0]);
451static int cayman_cp2_index = offsetof(struct amdgpu_device, gfx.compute_ring[1]);
Alex Deucherc113ea12015-10-08 16:30:37 -0400452static int amdgpu_dma1_index = offsetof(struct amdgpu_device, sdma.instance[0].ring);
453static int amdgpu_dma2_index = offsetof(struct amdgpu_device, sdma.instance[1].ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454static int r600_uvd_index = offsetof(struct amdgpu_device, uvd.ring);
455static int si_vce1_index = offsetof(struct amdgpu_device, vce.ring[0]);
456static int si_vce2_index = offsetof(struct amdgpu_device, vce.ring[1]);
457
458static struct drm_info_list amdgpu_debugfs_ring_info_list[] = {
459 {"amdgpu_ring_gfx", amdgpu_debugfs_ring_info, 0, &amdgpu_gfx_index},
460 {"amdgpu_ring_cp1", amdgpu_debugfs_ring_info, 0, &cayman_cp1_index},
461 {"amdgpu_ring_cp2", amdgpu_debugfs_ring_info, 0, &cayman_cp2_index},
462 {"amdgpu_ring_dma1", amdgpu_debugfs_ring_info, 0, &amdgpu_dma1_index},
463 {"amdgpu_ring_dma2", amdgpu_debugfs_ring_info, 0, &amdgpu_dma2_index},
464 {"amdgpu_ring_uvd", amdgpu_debugfs_ring_info, 0, &r600_uvd_index},
465 {"amdgpu_ring_vce1", amdgpu_debugfs_ring_info, 0, &si_vce1_index},
466 {"amdgpu_ring_vce2", amdgpu_debugfs_ring_info, 0, &si_vce2_index},
467};
468
469#endif
470
471static int amdgpu_debugfs_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring)
472{
473#if defined(CONFIG_DEBUG_FS)
474 unsigned i;
475 for (i = 0; i < ARRAY_SIZE(amdgpu_debugfs_ring_info_list); ++i) {
476 struct drm_info_list *info = &amdgpu_debugfs_ring_info_list[i];
477 int roffset = *(int*)amdgpu_debugfs_ring_info_list[i].data;
478 struct amdgpu_ring *other = (void *)(((uint8_t*)adev) + roffset);
479 unsigned r;
480
481 if (other != ring)
482 continue;
483
484 r = amdgpu_debugfs_add_files(adev, info, 1);
485 if (r)
486 return r;
487 }
488#endif
489 return 0;
490}