1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot * Copyright 2008 Advanced Micro Devices, Inc.
3926deccbSFrançois Tigeot * Copyright 2008 Red Hat Inc.
4926deccbSFrançois Tigeot * Copyright 2009 Jerome Glisse.
5926deccbSFrançois Tigeot *
6926deccbSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a
7926deccbSFrançois Tigeot * copy of this software and associated documentation files (the "Software"),
8926deccbSFrançois Tigeot * to deal in the Software without restriction, including without limitation
9926deccbSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10926deccbSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the
11926deccbSFrançois Tigeot * Software is furnished to do so, subject to the following conditions:
12926deccbSFrançois Tigeot *
13926deccbSFrançois Tigeot * The above copyright notice and this permission notice shall be included in
14926deccbSFrançois Tigeot * all copies or substantial portions of the Software.
15926deccbSFrançois Tigeot *
16926deccbSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17926deccbSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18926deccbSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19926deccbSFrançois Tigeot * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20926deccbSFrançois Tigeot * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21926deccbSFrançois Tigeot * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22926deccbSFrançois Tigeot * OTHER DEALINGS IN THE SOFTWARE.
23926deccbSFrançois Tigeot *
24926deccbSFrançois Tigeot * Authors: Dave Airlie
25926deccbSFrançois Tigeot * Alex Deucher
26926deccbSFrançois Tigeot * Jerome Glisse
27926deccbSFrançois Tigeot * Christian König
28926deccbSFrançois Tigeot */
29926deccbSFrançois Tigeot #include <drm/drmP.h>
30926deccbSFrançois Tigeot #include "radeon.h"
31926deccbSFrançois Tigeot
32926deccbSFrançois Tigeot /*
33926deccbSFrançois Tigeot * Rings
34926deccbSFrançois Tigeot * Most engines on the GPU are fed via ring buffers. Ring
35926deccbSFrançois Tigeot * buffers are areas of GPU accessible memory that the host
36926deccbSFrançois Tigeot * writes commands into and the GPU reads commands out of.
37926deccbSFrançois Tigeot * There is a rptr (read pointer) that determines where the
38926deccbSFrançois Tigeot * GPU is currently reading, and a wptr (write pointer)
39926deccbSFrançois Tigeot * which determines where the host has written. When the
40926deccbSFrançois Tigeot * pointers are equal, the ring is idle. When the host
41926deccbSFrançois Tigeot * writes commands to the ring buffer, it increments the
42926deccbSFrançois Tigeot * wptr. The GPU then starts fetching commands and executes
43926deccbSFrançois Tigeot * them until the pointers are equal again.
44926deccbSFrançois Tigeot */
45926deccbSFrançois Tigeot static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring);
46926deccbSFrançois Tigeot
47926deccbSFrançois Tigeot /**
48926deccbSFrançois Tigeot * radeon_ring_supports_scratch_reg - check if the ring supports
49926deccbSFrançois Tigeot * writing to scratch registers
50926deccbSFrançois Tigeot *
51926deccbSFrançois Tigeot * @rdev: radeon_device pointer
52926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
53926deccbSFrançois Tigeot *
54926deccbSFrançois Tigeot * Check if a specific ring supports writing to scratch registers (all asics).
55926deccbSFrançois Tigeot * Returns true if the ring supports writing to scratch regs, false if not.
56926deccbSFrançois Tigeot */
radeon_ring_supports_scratch_reg(struct radeon_device * rdev,struct radeon_ring * ring)57926deccbSFrançois Tigeot bool radeon_ring_supports_scratch_reg(struct radeon_device *rdev,
58926deccbSFrançois Tigeot struct radeon_ring *ring)
59926deccbSFrançois Tigeot {
60926deccbSFrançois Tigeot switch (ring->idx) {
61926deccbSFrançois Tigeot case RADEON_RING_TYPE_GFX_INDEX:
62926deccbSFrançois Tigeot case CAYMAN_RING_TYPE_CP1_INDEX:
63926deccbSFrançois Tigeot case CAYMAN_RING_TYPE_CP2_INDEX:
64926deccbSFrançois Tigeot return true;
65926deccbSFrançois Tigeot default:
66926deccbSFrançois Tigeot return false;
67926deccbSFrançois Tigeot }
68926deccbSFrançois Tigeot }
69926deccbSFrançois Tigeot
70926deccbSFrançois Tigeot /**
71926deccbSFrançois Tigeot * radeon_ring_free_size - update the free size
72926deccbSFrançois Tigeot *
73926deccbSFrançois Tigeot * @rdev: radeon_device pointer
74926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
75926deccbSFrançois Tigeot *
76926deccbSFrançois Tigeot * Update the free dw slots in the ring buffer (all asics).
77926deccbSFrançois Tigeot */
radeon_ring_free_size(struct radeon_device * rdev,struct radeon_ring * ring)78926deccbSFrançois Tigeot void radeon_ring_free_size(struct radeon_device *rdev, struct radeon_ring *ring)
79926deccbSFrançois Tigeot {
80c6f73aabSFrançois Tigeot uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
81c6f73aabSFrançois Tigeot
82926deccbSFrançois Tigeot /* This works because ring_size is a power of 2 */
83c6f73aabSFrançois Tigeot ring->ring_free_dw = rptr + (ring->ring_size / 4);
84926deccbSFrançois Tigeot ring->ring_free_dw -= ring->wptr;
85926deccbSFrançois Tigeot ring->ring_free_dw &= ring->ptr_mask;
86926deccbSFrançois Tigeot if (!ring->ring_free_dw) {
87c6f73aabSFrançois Tigeot /* this is an empty ring */
88926deccbSFrançois Tigeot ring->ring_free_dw = ring->ring_size / 4;
89c6f73aabSFrançois Tigeot /* update lockup info to avoid false positive */
90c6f73aabSFrançois Tigeot radeon_ring_lockup_update(rdev, ring);
91926deccbSFrançois Tigeot }
92926deccbSFrançois Tigeot }
93926deccbSFrançois Tigeot
94926deccbSFrançois Tigeot /**
95926deccbSFrançois Tigeot * radeon_ring_alloc - allocate space on the ring buffer
96926deccbSFrançois Tigeot *
97926deccbSFrançois Tigeot * @rdev: radeon_device pointer
98926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
99926deccbSFrançois Tigeot * @ndw: number of dwords to allocate in the ring buffer
100926deccbSFrançois Tigeot *
101926deccbSFrançois Tigeot * Allocate @ndw dwords in the ring buffer (all asics).
102926deccbSFrançois Tigeot * Returns 0 on success, error on failure.
103926deccbSFrançois Tigeot */
radeon_ring_alloc(struct radeon_device * rdev,struct radeon_ring * ring,unsigned ndw)104926deccbSFrançois Tigeot int radeon_ring_alloc(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
105926deccbSFrançois Tigeot {
106926deccbSFrançois Tigeot int r;
107926deccbSFrançois Tigeot
108926deccbSFrançois Tigeot /* make sure we aren't trying to allocate more space than there is on the ring */
109926deccbSFrançois Tigeot if (ndw > (ring->ring_size / 4))
110926deccbSFrançois Tigeot return -ENOMEM;
111926deccbSFrançois Tigeot /* Align requested size with padding so unlock_commit can
112926deccbSFrançois Tigeot * pad safely */
113f43cf1b1SMichael Neumann radeon_ring_free_size(rdev, ring);
114926deccbSFrançois Tigeot ndw = (ndw + ring->align_mask) & ~ring->align_mask;
115926deccbSFrançois Tigeot while (ndw > (ring->ring_free_dw - 1)) {
116926deccbSFrançois Tigeot radeon_ring_free_size(rdev, ring);
117926deccbSFrançois Tigeot if (ndw < ring->ring_free_dw) {
118926deccbSFrançois Tigeot break;
119926deccbSFrançois Tigeot }
120c6f73aabSFrançois Tigeot r = radeon_fence_wait_next(rdev, ring->idx);
121926deccbSFrançois Tigeot if (r)
122926deccbSFrançois Tigeot return r;
123926deccbSFrançois Tigeot }
124926deccbSFrançois Tigeot ring->count_dw = ndw;
125926deccbSFrançois Tigeot ring->wptr_old = ring->wptr;
126926deccbSFrançois Tigeot return 0;
127926deccbSFrançois Tigeot }
128926deccbSFrançois Tigeot
129926deccbSFrançois Tigeot /**
130926deccbSFrançois Tigeot * radeon_ring_lock - lock the ring and allocate space on it
131926deccbSFrançois Tigeot *
132926deccbSFrançois Tigeot * @rdev: radeon_device pointer
133926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
134926deccbSFrançois Tigeot * @ndw: number of dwords to allocate in the ring buffer
135926deccbSFrançois Tigeot *
136926deccbSFrançois Tigeot * Lock the ring and allocate @ndw dwords in the ring buffer
137926deccbSFrançois Tigeot * (all asics).
138926deccbSFrançois Tigeot * Returns 0 on success, error on failure.
139926deccbSFrançois Tigeot */
radeon_ring_lock(struct radeon_device * rdev,struct radeon_ring * ring,unsigned ndw)140926deccbSFrançois Tigeot int radeon_ring_lock(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ndw)
141926deccbSFrançois Tigeot {
142926deccbSFrançois Tigeot int r;
143926deccbSFrançois Tigeot
1447dcf36dcSFrançois Tigeot mutex_lock(&rdev->ring_lock);
145926deccbSFrançois Tigeot r = radeon_ring_alloc(rdev, ring, ndw);
146926deccbSFrançois Tigeot if (r) {
1477dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
148926deccbSFrançois Tigeot return r;
149926deccbSFrançois Tigeot }
150926deccbSFrançois Tigeot return 0;
151926deccbSFrançois Tigeot }
152926deccbSFrançois Tigeot
153926deccbSFrançois Tigeot /**
154926deccbSFrançois Tigeot * radeon_ring_commit - tell the GPU to execute the new
155926deccbSFrançois Tigeot * commands on the ring buffer
156926deccbSFrançois Tigeot *
157926deccbSFrançois Tigeot * @rdev: radeon_device pointer
158926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
159c6f73aabSFrançois Tigeot * @hdp_flush: Whether or not to perform an HDP cache flush
160926deccbSFrançois Tigeot *
161926deccbSFrançois Tigeot * Update the wptr (write pointer) to tell the GPU to
162926deccbSFrançois Tigeot * execute new commands on the ring buffer (all asics).
163926deccbSFrançois Tigeot */
radeon_ring_commit(struct radeon_device * rdev,struct radeon_ring * ring,bool hdp_flush)164c6f73aabSFrançois Tigeot void radeon_ring_commit(struct radeon_device *rdev, struct radeon_ring *ring,
165c6f73aabSFrançois Tigeot bool hdp_flush)
166926deccbSFrançois Tigeot {
167c6f73aabSFrançois Tigeot /* If we are emitting the HDP flush via the ring buffer, we need to
168c6f73aabSFrançois Tigeot * do it before padding.
169c6f73aabSFrançois Tigeot */
170c6f73aabSFrançois Tigeot if (hdp_flush && rdev->asic->ring[ring->idx]->hdp_flush)
171c6f73aabSFrançois Tigeot rdev->asic->ring[ring->idx]->hdp_flush(rdev, ring);
172926deccbSFrançois Tigeot /* We pad to match fetch size */
173926deccbSFrançois Tigeot while (ring->wptr & ring->align_mask) {
174926deccbSFrançois Tigeot radeon_ring_write(ring, ring->nop);
175926deccbSFrançois Tigeot }
176c6f73aabSFrançois Tigeot mb();
177c6f73aabSFrançois Tigeot /* If we are emitting the HDP flush via MMIO, we need to do it after
178c6f73aabSFrançois Tigeot * all CPU writes to VRAM finished.
179c6f73aabSFrançois Tigeot */
180c6f73aabSFrançois Tigeot if (hdp_flush && rdev->asic->mmio_hdp_flush)
181c6f73aabSFrançois Tigeot rdev->asic->mmio_hdp_flush(rdev);
18257e252bfSMichael Neumann radeon_ring_set_wptr(rdev, ring);
183926deccbSFrançois Tigeot }
184926deccbSFrançois Tigeot
185926deccbSFrançois Tigeot /**
186926deccbSFrançois Tigeot * radeon_ring_unlock_commit - tell the GPU to execute the new
187926deccbSFrançois Tigeot * commands on the ring buffer and unlock it
188926deccbSFrançois Tigeot *
189926deccbSFrançois Tigeot * @rdev: radeon_device pointer
190926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
191c6f73aabSFrançois Tigeot * @hdp_flush: Whether or not to perform an HDP cache flush
192926deccbSFrançois Tigeot *
193926deccbSFrançois Tigeot * Call radeon_ring_commit() then unlock the ring (all asics).
194926deccbSFrançois Tigeot */
radeon_ring_unlock_commit(struct radeon_device * rdev,struct radeon_ring * ring,bool hdp_flush)195c6f73aabSFrançois Tigeot void radeon_ring_unlock_commit(struct radeon_device *rdev, struct radeon_ring *ring,
196c6f73aabSFrançois Tigeot bool hdp_flush)
197926deccbSFrançois Tigeot {
198c6f73aabSFrançois Tigeot radeon_ring_commit(rdev, ring, hdp_flush);
1997dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
200926deccbSFrançois Tigeot }
201926deccbSFrançois Tigeot
202926deccbSFrançois Tigeot /**
203926deccbSFrançois Tigeot * radeon_ring_undo - reset the wptr
204926deccbSFrançois Tigeot *
205926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
206926deccbSFrançois Tigeot *
207926deccbSFrançois Tigeot * Reset the driver's copy of the wptr (all asics).
208926deccbSFrançois Tigeot */
radeon_ring_undo(struct radeon_ring * ring)209926deccbSFrançois Tigeot void radeon_ring_undo(struct radeon_ring *ring)
210926deccbSFrançois Tigeot {
211926deccbSFrançois Tigeot ring->wptr = ring->wptr_old;
212926deccbSFrançois Tigeot }
213926deccbSFrançois Tigeot
214926deccbSFrançois Tigeot /**
215926deccbSFrançois Tigeot * radeon_ring_unlock_undo - reset the wptr and unlock the ring
216926deccbSFrançois Tigeot *
217926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
218926deccbSFrançois Tigeot *
219926deccbSFrançois Tigeot * Call radeon_ring_undo() then unlock the ring (all asics).
220926deccbSFrançois Tigeot */
radeon_ring_unlock_undo(struct radeon_device * rdev,struct radeon_ring * ring)221926deccbSFrançois Tigeot void radeon_ring_unlock_undo(struct radeon_device *rdev, struct radeon_ring *ring)
222926deccbSFrançois Tigeot {
223926deccbSFrançois Tigeot radeon_ring_undo(ring);
2247dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
225926deccbSFrançois Tigeot }
226926deccbSFrançois Tigeot
227926deccbSFrançois Tigeot /**
228926deccbSFrançois Tigeot * radeon_ring_lockup_update - update lockup variables
229926deccbSFrançois Tigeot *
230926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
231926deccbSFrançois Tigeot *
232926deccbSFrançois Tigeot * Update the last rptr value and timestamp (all asics).
233926deccbSFrançois Tigeot */
radeon_ring_lockup_update(struct radeon_device * rdev,struct radeon_ring * ring)234c6f73aabSFrançois Tigeot void radeon_ring_lockup_update(struct radeon_device *rdev,
235c6f73aabSFrançois Tigeot struct radeon_ring *ring)
236926deccbSFrançois Tigeot {
237c6f73aabSFrançois Tigeot atomic_set(&ring->last_rptr, radeon_ring_get_rptr(rdev, ring));
238c6f73aabSFrançois Tigeot atomic64_set(&ring->last_activity, jiffies_64);
239926deccbSFrançois Tigeot }
240926deccbSFrançois Tigeot
241926deccbSFrançois Tigeot /**
242926deccbSFrançois Tigeot * radeon_ring_test_lockup() - check if ring is lockedup by recording information
243926deccbSFrançois Tigeot * @rdev: radeon device structure
244926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
245926deccbSFrançois Tigeot *
246c6f73aabSFrançois Tigeot */
radeon_ring_test_lockup(struct radeon_device * rdev,struct radeon_ring * ring)247926deccbSFrançois Tigeot bool radeon_ring_test_lockup(struct radeon_device *rdev, struct radeon_ring *ring)
248926deccbSFrançois Tigeot {
249c6f73aabSFrançois Tigeot uint32_t rptr = radeon_ring_get_rptr(rdev, ring);
250c6f73aabSFrançois Tigeot uint64_t last = atomic64_read(&ring->last_activity);
251c6f73aabSFrançois Tigeot uint64_t elapsed;
252926deccbSFrançois Tigeot
253c6f73aabSFrançois Tigeot if (rptr != atomic_read(&ring->last_rptr)) {
254c6f73aabSFrançois Tigeot /* ring is still working, no lockup */
255c6f73aabSFrançois Tigeot radeon_ring_lockup_update(rdev, ring);
256926deccbSFrançois Tigeot return false;
257926deccbSFrançois Tigeot }
258c6f73aabSFrançois Tigeot
259c6f73aabSFrançois Tigeot elapsed = jiffies_to_msecs(jiffies_64 - last);
260926deccbSFrançois Tigeot if (radeon_lockup_timeout && elapsed >= radeon_lockup_timeout) {
261c6f73aabSFrançois Tigeot dev_err(rdev->dev, "ring %d stalled for more than %lumsec\n",
262c6f73aabSFrançois Tigeot ring->idx, elapsed);
263926deccbSFrançois Tigeot return true;
264926deccbSFrançois Tigeot }
265926deccbSFrançois Tigeot /* give a chance to the GPU ... */
266926deccbSFrançois Tigeot return false;
267926deccbSFrançois Tigeot }
268926deccbSFrançois Tigeot
269926deccbSFrançois Tigeot /**
270926deccbSFrançois Tigeot * radeon_ring_backup - Back up the content of a ring
271926deccbSFrançois Tigeot *
272926deccbSFrançois Tigeot * @rdev: radeon_device pointer
273926deccbSFrançois Tigeot * @ring: the ring we want to back up
274926deccbSFrançois Tigeot *
275926deccbSFrançois Tigeot * Saves all unprocessed commits from a ring, returns the number of dwords saved.
276926deccbSFrançois Tigeot */
radeon_ring_backup(struct radeon_device * rdev,struct radeon_ring * ring,uint32_t ** data)277926deccbSFrançois Tigeot unsigned radeon_ring_backup(struct radeon_device *rdev, struct radeon_ring *ring,
278926deccbSFrançois Tigeot uint32_t **data)
279926deccbSFrançois Tigeot {
280926deccbSFrançois Tigeot unsigned size, ptr, i;
281926deccbSFrançois Tigeot
282926deccbSFrançois Tigeot /* just in case lock the ring */
2837dcf36dcSFrançois Tigeot mutex_lock(&rdev->ring_lock);
284926deccbSFrançois Tigeot *data = NULL;
285926deccbSFrançois Tigeot
286926deccbSFrançois Tigeot if (ring->ring_obj == NULL) {
2877dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
288926deccbSFrançois Tigeot return 0;
289926deccbSFrançois Tigeot }
290926deccbSFrançois Tigeot
291926deccbSFrançois Tigeot /* it doesn't make sense to save anything if all fences are signaled */
292926deccbSFrançois Tigeot if (!radeon_fence_count_emitted(rdev, ring->idx)) {
2937dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
294926deccbSFrançois Tigeot return 0;
295926deccbSFrançois Tigeot }
296926deccbSFrançois Tigeot
297926deccbSFrançois Tigeot /* calculate the number of dw on the ring */
298926deccbSFrançois Tigeot if (ring->rptr_save_reg)
299926deccbSFrançois Tigeot ptr = RREG32(ring->rptr_save_reg);
300926deccbSFrançois Tigeot else if (rdev->wb.enabled)
301926deccbSFrançois Tigeot ptr = le32_to_cpu(*ring->next_rptr_cpu_addr);
302926deccbSFrançois Tigeot else {
303926deccbSFrançois Tigeot /* no way to read back the next rptr */
3047dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
305926deccbSFrançois Tigeot return 0;
306926deccbSFrançois Tigeot }
307926deccbSFrançois Tigeot
308926deccbSFrançois Tigeot size = ring->wptr + (ring->ring_size / 4);
309926deccbSFrançois Tigeot size -= ptr;
310926deccbSFrançois Tigeot size &= ring->ptr_mask;
311926deccbSFrançois Tigeot if (size == 0) {
3127dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
313926deccbSFrançois Tigeot return 0;
314926deccbSFrançois Tigeot }
315926deccbSFrançois Tigeot
316926deccbSFrançois Tigeot /* and then save the content of the ring */
317*3f2dd94aSFrançois Tigeot *data = kvmalloc_array(size, sizeof(uint32_t), GFP_KERNEL);
318926deccbSFrançois Tigeot if (!*data) {
3197dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
320926deccbSFrançois Tigeot return 0;
321926deccbSFrançois Tigeot }
322926deccbSFrançois Tigeot for (i = 0; i < size; ++i) {
323926deccbSFrançois Tigeot (*data)[i] = ring->ring[ptr++];
324926deccbSFrançois Tigeot ptr &= ring->ptr_mask;
325926deccbSFrançois Tigeot }
326926deccbSFrançois Tigeot
3277dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
328926deccbSFrançois Tigeot return size;
329926deccbSFrançois Tigeot }
330926deccbSFrançois Tigeot
331926deccbSFrançois Tigeot /**
332926deccbSFrançois Tigeot * radeon_ring_restore - append saved commands to the ring again
333926deccbSFrançois Tigeot *
334926deccbSFrançois Tigeot * @rdev: radeon_device pointer
335926deccbSFrançois Tigeot * @ring: ring to append commands to
336926deccbSFrançois Tigeot * @size: number of dwords we want to write
337926deccbSFrançois Tigeot * @data: saved commands
338926deccbSFrançois Tigeot *
339926deccbSFrançois Tigeot * Allocates space on the ring and restore the previously saved commands.
340926deccbSFrançois Tigeot */
radeon_ring_restore(struct radeon_device * rdev,struct radeon_ring * ring,unsigned size,uint32_t * data)341926deccbSFrançois Tigeot int radeon_ring_restore(struct radeon_device *rdev, struct radeon_ring *ring,
342926deccbSFrançois Tigeot unsigned size, uint32_t *data)
343926deccbSFrançois Tigeot {
344926deccbSFrançois Tigeot int i, r;
345926deccbSFrançois Tigeot
346926deccbSFrançois Tigeot if (!size || !data)
347926deccbSFrançois Tigeot return 0;
348926deccbSFrançois Tigeot
349926deccbSFrançois Tigeot /* restore the saved ring content */
350926deccbSFrançois Tigeot r = radeon_ring_lock(rdev, ring, size);
351926deccbSFrançois Tigeot if (r)
352926deccbSFrançois Tigeot return r;
353926deccbSFrançois Tigeot
354926deccbSFrançois Tigeot for (i = 0; i < size; ++i) {
355926deccbSFrançois Tigeot radeon_ring_write(ring, data[i]);
356926deccbSFrançois Tigeot }
357926deccbSFrançois Tigeot
358c6f73aabSFrançois Tigeot radeon_ring_unlock_commit(rdev, ring, false);
359*3f2dd94aSFrançois Tigeot kvfree(data);
360926deccbSFrançois Tigeot return 0;
361926deccbSFrançois Tigeot }
362926deccbSFrançois Tigeot
363926deccbSFrançois Tigeot /**
364926deccbSFrançois Tigeot * radeon_ring_init - init driver ring struct.
365926deccbSFrançois Tigeot *
366926deccbSFrançois Tigeot * @rdev: radeon_device pointer
367926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
368926deccbSFrançois Tigeot * @ring_size: size of the ring
369926deccbSFrançois Tigeot * @rptr_offs: offset of the rptr writeback location in the WB buffer
370926deccbSFrançois Tigeot * @nop: nop packet for this ring
371926deccbSFrançois Tigeot *
372926deccbSFrançois Tigeot * Initialize the driver information for the selected ring (all asics).
373926deccbSFrançois Tigeot * Returns 0 on success, error on failure.
374926deccbSFrançois Tigeot */
radeon_ring_init(struct radeon_device * rdev,struct radeon_ring * ring,unsigned ring_size,unsigned rptr_offs,u32 nop)375926deccbSFrançois Tigeot int radeon_ring_init(struct radeon_device *rdev, struct radeon_ring *ring, unsigned ring_size,
376c6f73aabSFrançois Tigeot unsigned rptr_offs, u32 nop)
377926deccbSFrançois Tigeot {
378926deccbSFrançois Tigeot int r;
379926deccbSFrançois Tigeot
380926deccbSFrançois Tigeot ring->ring_size = ring_size;
381926deccbSFrançois Tigeot ring->rptr_offs = rptr_offs;
382926deccbSFrançois Tigeot ring->nop = nop;
383926deccbSFrançois Tigeot /* Allocate ring buffer */
384926deccbSFrançois Tigeot if (ring->ring_obj == NULL) {
385926deccbSFrançois Tigeot r = radeon_bo_create(rdev, ring->ring_size, PAGE_SIZE, true,
3867dcf36dcSFrançois Tigeot RADEON_GEM_DOMAIN_GTT, 0, NULL,
387926deccbSFrançois Tigeot NULL, &ring->ring_obj);
388926deccbSFrançois Tigeot if (r) {
389926deccbSFrançois Tigeot dev_err(rdev->dev, "(%d) ring create failed\n", r);
390926deccbSFrançois Tigeot return r;
391926deccbSFrançois Tigeot }
392926deccbSFrançois Tigeot r = radeon_bo_reserve(ring->ring_obj, false);
3937dcf36dcSFrançois Tigeot if (unlikely(r != 0))
394926deccbSFrançois Tigeot return r;
395926deccbSFrançois Tigeot r = radeon_bo_pin(ring->ring_obj, RADEON_GEM_DOMAIN_GTT,
396f77dbd6cSFrançois Tigeot (u64 *)&ring->gpu_addr);
397926deccbSFrançois Tigeot if (r) {
398926deccbSFrançois Tigeot radeon_bo_unreserve(ring->ring_obj);
399926deccbSFrançois Tigeot dev_err(rdev->dev, "(%d) ring pin failed\n", r);
400926deccbSFrançois Tigeot return r;
401926deccbSFrançois Tigeot }
402926deccbSFrançois Tigeot r = radeon_bo_kmap(ring->ring_obj,
403a85cb24fSFrançois Tigeot (void **)&ring->ring);
404926deccbSFrançois Tigeot radeon_bo_unreserve(ring->ring_obj);
405926deccbSFrançois Tigeot if (r) {
406926deccbSFrançois Tigeot dev_err(rdev->dev, "(%d) ring map failed\n", r);
407926deccbSFrançois Tigeot return r;
408926deccbSFrançois Tigeot }
409926deccbSFrançois Tigeot }
410926deccbSFrançois Tigeot ring->ptr_mask = (ring->ring_size / 4) - 1;
411926deccbSFrançois Tigeot ring->ring_free_dw = ring->ring_size / 4;
412926deccbSFrançois Tigeot if (rdev->wb.enabled) {
413926deccbSFrançois Tigeot u32 index = RADEON_WB_RING0_NEXT_RPTR + (ring->idx * 4);
414926deccbSFrançois Tigeot ring->next_rptr_gpu_addr = rdev->wb.gpu_addr + index;
415926deccbSFrançois Tigeot ring->next_rptr_cpu_addr = &rdev->wb.wb[index/4];
416926deccbSFrançois Tigeot }
417926deccbSFrançois Tigeot if (radeon_debugfs_ring_init(rdev, ring)) {
418926deccbSFrançois Tigeot DRM_ERROR("Failed to register debugfs file for rings !\n");
419926deccbSFrançois Tigeot }
420c6f73aabSFrançois Tigeot radeon_ring_lockup_update(rdev, ring);
421926deccbSFrançois Tigeot return 0;
422926deccbSFrançois Tigeot }
423926deccbSFrançois Tigeot
424926deccbSFrançois Tigeot /**
425926deccbSFrançois Tigeot * radeon_ring_fini - tear down the driver ring struct.
426926deccbSFrançois Tigeot *
427926deccbSFrançois Tigeot * @rdev: radeon_device pointer
428926deccbSFrançois Tigeot * @ring: radeon_ring structure holding ring information
429926deccbSFrançois Tigeot *
430926deccbSFrançois Tigeot * Tear down the driver information for the selected ring (all asics).
431926deccbSFrançois Tigeot */
radeon_ring_fini(struct radeon_device * rdev,struct radeon_ring * ring)432926deccbSFrançois Tigeot void radeon_ring_fini(struct radeon_device *rdev, struct radeon_ring *ring)
433926deccbSFrançois Tigeot {
434926deccbSFrançois Tigeot int r;
435926deccbSFrançois Tigeot struct radeon_bo *ring_obj;
436926deccbSFrançois Tigeot
4377dcf36dcSFrançois Tigeot mutex_lock(&rdev->ring_lock);
438926deccbSFrançois Tigeot ring_obj = ring->ring_obj;
439926deccbSFrançois Tigeot ring->ready = false;
440926deccbSFrançois Tigeot ring->ring = NULL;
441926deccbSFrançois Tigeot ring->ring_obj = NULL;
4427dcf36dcSFrançois Tigeot mutex_unlock(&rdev->ring_lock);
443926deccbSFrançois Tigeot
444926deccbSFrançois Tigeot if (ring_obj) {
445926deccbSFrançois Tigeot r = radeon_bo_reserve(ring_obj, false);
446926deccbSFrançois Tigeot if (likely(r == 0)) {
447926deccbSFrançois Tigeot radeon_bo_kunmap(ring_obj);
448926deccbSFrançois Tigeot radeon_bo_unpin(ring_obj);
449926deccbSFrançois Tigeot radeon_bo_unreserve(ring_obj);
450926deccbSFrançois Tigeot }
451926deccbSFrançois Tigeot radeon_bo_unref(&ring_obj);
452926deccbSFrançois Tigeot }
453926deccbSFrançois Tigeot }
454926deccbSFrançois Tigeot
455926deccbSFrançois Tigeot /*
456926deccbSFrançois Tigeot * Debugfs info
457926deccbSFrançois Tigeot */
458926deccbSFrançois Tigeot #if defined(CONFIG_DEBUG_FS)
459926deccbSFrançois Tigeot
radeon_debugfs_ring_info(struct seq_file * m,void * data)460926deccbSFrançois Tigeot static int radeon_debugfs_ring_info(struct seq_file *m, void *data)
461926deccbSFrançois Tigeot {
462926deccbSFrançois Tigeot struct drm_info_node *node = (struct drm_info_node *) m->private;
463926deccbSFrançois Tigeot struct drm_device *dev = node->minor->dev;
464926deccbSFrançois Tigeot struct radeon_device *rdev = dev->dev_private;
465926deccbSFrançois Tigeot int ridx = *(int*)node->info_ent->data;
466926deccbSFrançois Tigeot struct radeon_ring *ring = &rdev->ring[ridx];
467c6f73aabSFrançois Tigeot
468c6f73aabSFrançois Tigeot uint32_t rptr, wptr, rptr_next;
469926deccbSFrançois Tigeot unsigned count, i, j;
470926deccbSFrançois Tigeot
471926deccbSFrançois Tigeot radeon_ring_free_size(rdev, ring);
472926deccbSFrançois Tigeot count = (ring->ring_size / 4) - ring->ring_free_dw;
473c6f73aabSFrançois Tigeot
474c6f73aabSFrançois Tigeot wptr = radeon_ring_get_wptr(rdev, ring);
475c6f73aabSFrançois Tigeot seq_printf(m, "wptr: 0x%08x [%5d]\n",
476c6f73aabSFrançois Tigeot wptr, wptr);
477c6f73aabSFrançois Tigeot
478c6f73aabSFrançois Tigeot rptr = radeon_ring_get_rptr(rdev, ring);
479c6f73aabSFrançois Tigeot seq_printf(m, "rptr: 0x%08x [%5d]\n",
480c6f73aabSFrançois Tigeot rptr, rptr);
481c6f73aabSFrançois Tigeot
482926deccbSFrançois Tigeot if (ring->rptr_save_reg) {
483c6f73aabSFrançois Tigeot rptr_next = RREG32(ring->rptr_save_reg);
484c6f73aabSFrançois Tigeot seq_printf(m, "rptr next(0x%04x): 0x%08x [%5d]\n",
485c6f73aabSFrançois Tigeot ring->rptr_save_reg, rptr_next, rptr_next);
486c6f73aabSFrançois Tigeot } else
487c6f73aabSFrançois Tigeot rptr_next = ~0;
488c6f73aabSFrançois Tigeot
489c6f73aabSFrançois Tigeot seq_printf(m, "driver's copy of the wptr: 0x%08x [%5d]\n",
490c6f73aabSFrançois Tigeot ring->wptr, ring->wptr);
491c6f73aabSFrançois Tigeot seq_printf(m, "last semaphore signal addr : 0x%016llx\n",
492c6f73aabSFrançois Tigeot ring->last_semaphore_signal_addr);
493c6f73aabSFrançois Tigeot seq_printf(m, "last semaphore wait addr : 0x%016llx\n",
494c6f73aabSFrançois Tigeot ring->last_semaphore_wait_addr);
495926deccbSFrançois Tigeot seq_printf(m, "%u free dwords in ring\n", ring->ring_free_dw);
496926deccbSFrançois Tigeot seq_printf(m, "%u dwords in ring\n", count);
497c6f73aabSFrançois Tigeot
498c59a5c48SFrançois Tigeot if (!ring->ring)
499c6f73aabSFrançois Tigeot return 0;
500c6f73aabSFrançois Tigeot
501926deccbSFrançois Tigeot /* print 8 dw before current rptr as often it's the last executed
502926deccbSFrançois Tigeot * packet that is the root issue
503926deccbSFrançois Tigeot */
504c6f73aabSFrançois Tigeot i = (rptr + ring->ptr_mask + 1 - 32) & ring->ptr_mask;
505926deccbSFrançois Tigeot for (j = 0; j <= (count + 32); j++) {
506c6f73aabSFrançois Tigeot seq_printf(m, "r[%5d]=0x%08x", i, ring->ring[i]);
507c6f73aabSFrançois Tigeot if (rptr == i)
508c6f73aabSFrançois Tigeot seq_puts(m, " *");
509c6f73aabSFrançois Tigeot if (rptr_next == i)
510c6f73aabSFrançois Tigeot seq_puts(m, " #");
511c6f73aabSFrançois Tigeot seq_puts(m, "\n");
512926deccbSFrançois Tigeot i = (i + 1) & ring->ptr_mask;
513926deccbSFrançois Tigeot }
514926deccbSFrançois Tigeot return 0;
515926deccbSFrançois Tigeot }
516926deccbSFrançois Tigeot
517f43cf1b1SMichael Neumann static int radeon_gfx_index = RADEON_RING_TYPE_GFX_INDEX;
518f43cf1b1SMichael Neumann static int cayman_cp1_index = CAYMAN_RING_TYPE_CP1_INDEX;
519f43cf1b1SMichael Neumann static int cayman_cp2_index = CAYMAN_RING_TYPE_CP2_INDEX;
520f43cf1b1SMichael Neumann static int radeon_dma1_index = R600_RING_TYPE_DMA_INDEX;
521f43cf1b1SMichael Neumann static int radeon_dma2_index = CAYMAN_RING_TYPE_DMA1_INDEX;
522f43cf1b1SMichael Neumann static int r600_uvd_index = R600_RING_TYPE_UVD_INDEX;
523c6f73aabSFrançois Tigeot static int si_vce1_index = TN_RING_TYPE_VCE1_INDEX;
524c6f73aabSFrançois Tigeot static int si_vce2_index = TN_RING_TYPE_VCE2_INDEX;
525926deccbSFrançois Tigeot
526926deccbSFrançois Tigeot static struct drm_info_list radeon_debugfs_ring_info_list[] = {
527f43cf1b1SMichael Neumann {"radeon_ring_gfx", radeon_debugfs_ring_info, 0, &radeon_gfx_index},
528f43cf1b1SMichael Neumann {"radeon_ring_cp1", radeon_debugfs_ring_info, 0, &cayman_cp1_index},
529f43cf1b1SMichael Neumann {"radeon_ring_cp2", radeon_debugfs_ring_info, 0, &cayman_cp2_index},
530f43cf1b1SMichael Neumann {"radeon_ring_dma1", radeon_debugfs_ring_info, 0, &radeon_dma1_index},
531f43cf1b1SMichael Neumann {"radeon_ring_dma2", radeon_debugfs_ring_info, 0, &radeon_dma2_index},
532f43cf1b1SMichael Neumann {"radeon_ring_uvd", radeon_debugfs_ring_info, 0, &r600_uvd_index},
533c6f73aabSFrançois Tigeot {"radeon_ring_vce1", radeon_debugfs_ring_info, 0, &si_vce1_index},
534c6f73aabSFrançois Tigeot {"radeon_ring_vce2", radeon_debugfs_ring_info, 0, &si_vce2_index},
535926deccbSFrançois Tigeot };
536926deccbSFrançois Tigeot
537926deccbSFrançois Tigeot #endif
538926deccbSFrançois Tigeot
radeon_debugfs_ring_init(struct radeon_device * rdev,struct radeon_ring * ring)539926deccbSFrançois Tigeot static int radeon_debugfs_ring_init(struct radeon_device *rdev, struct radeon_ring *ring)
540926deccbSFrançois Tigeot {
541926deccbSFrançois Tigeot #if defined(CONFIG_DEBUG_FS)
542926deccbSFrançois Tigeot unsigned i;
543926deccbSFrançois Tigeot for (i = 0; i < ARRAY_SIZE(radeon_debugfs_ring_info_list); ++i) {
544926deccbSFrançois Tigeot struct drm_info_list *info = &radeon_debugfs_ring_info_list[i];
545926deccbSFrançois Tigeot int ridx = *(int*)radeon_debugfs_ring_info_list[i].data;
546926deccbSFrançois Tigeot unsigned r;
547926deccbSFrançois Tigeot
548926deccbSFrançois Tigeot if (&rdev->ring[ridx] != ring)
549926deccbSFrançois Tigeot continue;
550926deccbSFrançois Tigeot
551926deccbSFrançois Tigeot r = radeon_debugfs_add_files(rdev, info, 1);
552926deccbSFrançois Tigeot if (r)
553926deccbSFrançois Tigeot return r;
554926deccbSFrançois Tigeot }
555926deccbSFrançois Tigeot #endif
556926deccbSFrançois Tigeot return 0;
557926deccbSFrançois Tigeot }
558