blob: 41e637a0338fa682e825cba076e56e2bfee852bb [file] [log] [blame]
Alex Deuchera2e73f52015-04-20 17:09:27 -04001/*
2 * Copyright 2013 Advanced Micro Devices, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Alex Deucher
23 */
24#include <linux/firmware.h>
25#include <drm/drmP.h>
26#include "amdgpu.h"
27#include "amdgpu_ucode.h"
28#include "amdgpu_trace.h"
29#include "cikd.h"
30#include "cik.h"
31
32#include "bif/bif_4_1_d.h"
33#include "bif/bif_4_1_sh_mask.h"
34
35#include "gca/gfx_7_2_d.h"
Jack Xiao74a5d162015-05-08 14:46:49 +080036#include "gca/gfx_7_2_enum.h"
37#include "gca/gfx_7_2_sh_mask.h"
Alex Deuchera2e73f52015-04-20 17:09:27 -040038
39#include "gmc/gmc_7_1_d.h"
40#include "gmc/gmc_7_1_sh_mask.h"
41
42#include "oss/oss_2_0_d.h"
43#include "oss/oss_2_0_sh_mask.h"
44
45static const u32 sdma_offsets[SDMA_MAX_INSTANCE] =
46{
47 SDMA0_REGISTER_OFFSET,
48 SDMA1_REGISTER_OFFSET
49};
50
51static void cik_sdma_set_ring_funcs(struct amdgpu_device *adev);
52static void cik_sdma_set_irq_funcs(struct amdgpu_device *adev);
53static void cik_sdma_set_buffer_funcs(struct amdgpu_device *adev);
54static void cik_sdma_set_vm_pte_funcs(struct amdgpu_device *adev);
55
56MODULE_FIRMWARE("radeon/bonaire_sdma.bin");
57MODULE_FIRMWARE("radeon/bonaire_sdma1.bin");
58MODULE_FIRMWARE("radeon/hawaii_sdma.bin");
59MODULE_FIRMWARE("radeon/hawaii_sdma1.bin");
60MODULE_FIRMWARE("radeon/kaveri_sdma.bin");
61MODULE_FIRMWARE("radeon/kaveri_sdma1.bin");
62MODULE_FIRMWARE("radeon/kabini_sdma.bin");
63MODULE_FIRMWARE("radeon/kabini_sdma1.bin");
64MODULE_FIRMWARE("radeon/mullins_sdma.bin");
65MODULE_FIRMWARE("radeon/mullins_sdma1.bin");
66
67u32 amdgpu_cik_gpu_check_soft_reset(struct amdgpu_device *adev);
68
69/*
70 * sDMA - System DMA
71 * Starting with CIK, the GPU has new asynchronous
72 * DMA engines. These engines are used for compute
73 * and gfx. There are two DMA engines (SDMA0, SDMA1)
74 * and each one supports 1 ring buffer used for gfx
75 * and 2 queues used for compute.
76 *
77 * The programming model is very similar to the CP
78 * (ring buffer, IBs, etc.), but sDMA has it's own
79 * packet format that is different from the PM4 format
80 * used by the CP. sDMA supports copying data, writing
81 * embedded data, solid fills, and a number of other
82 * things. It also has support for tiling/detiling of
83 * buffers.
84 */
85
86/**
87 * cik_sdma_init_microcode - load ucode images from disk
88 *
89 * @adev: amdgpu_device pointer
90 *
91 * Use the firmware interface to load the ucode images into
92 * the driver (not loaded into hw).
93 * Returns 0 on success, error on failure.
94 */
95static int cik_sdma_init_microcode(struct amdgpu_device *adev)
96{
97 const char *chip_name;
98 char fw_name[30];
Alex Deucherc113ea12015-10-08 16:30:37 -040099 int err = 0, i;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400100
101 DRM_DEBUG("\n");
102
103 switch (adev->asic_type) {
104 case CHIP_BONAIRE:
105 chip_name = "bonaire";
106 break;
107 case CHIP_HAWAII:
108 chip_name = "hawaii";
109 break;
110 case CHIP_KAVERI:
111 chip_name = "kaveri";
112 break;
113 case CHIP_KABINI:
114 chip_name = "kabini";
115 break;
116 case CHIP_MULLINS:
117 chip_name = "mullins";
118 break;
119 default: BUG();
120 }
121
Alex Deucherc113ea12015-10-08 16:30:37 -0400122 for (i = 0; i < adev->sdma.num_instances; i++) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400123 if (i == 0)
124 snprintf(fw_name, sizeof(fw_name), "radeon/%s_sdma.bin", chip_name);
125 else
126 snprintf(fw_name, sizeof(fw_name), "radeon/%s_sdma1.bin", chip_name);
Alex Deucherc113ea12015-10-08 16:30:37 -0400127 err = request_firmware(&adev->sdma.instance[i].fw, fw_name, adev->dev);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400128 if (err)
129 goto out;
Alex Deucherc113ea12015-10-08 16:30:37 -0400130 err = amdgpu_ucode_validate(adev->sdma.instance[i].fw);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400131 }
132out:
133 if (err) {
134 printk(KERN_ERR
135 "cik_sdma: Failed to load firmware \"%s\"\n",
136 fw_name);
Alex Deucherc113ea12015-10-08 16:30:37 -0400137 for (i = 0; i < adev->sdma.num_instances; i++) {
138 release_firmware(adev->sdma.instance[i].fw);
139 adev->sdma.instance[i].fw = NULL;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400140 }
141 }
142 return err;
143}
144
145/**
146 * cik_sdma_ring_get_rptr - get the current read pointer
147 *
148 * @ring: amdgpu ring pointer
149 *
150 * Get the current rptr from the hardware (CIK+).
151 */
152static uint32_t cik_sdma_ring_get_rptr(struct amdgpu_ring *ring)
153{
154 u32 rptr;
155
156 rptr = ring->adev->wb.wb[ring->rptr_offs];
157
158 return (rptr & 0x3fffc) >> 2;
159}
160
161/**
162 * cik_sdma_ring_get_wptr - get the current write pointer
163 *
164 * @ring: amdgpu ring pointer
165 *
166 * Get the current wptr from the hardware (CIK+).
167 */
168static uint32_t cik_sdma_ring_get_wptr(struct amdgpu_ring *ring)
169{
170 struct amdgpu_device *adev = ring->adev;
Alex Deucherc113ea12015-10-08 16:30:37 -0400171 u32 me = (ring == &adev->sdma.instance[0].ring) ? 0 : 1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400172
173 return (RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[me]) & 0x3fffc) >> 2;
174}
175
176/**
177 * cik_sdma_ring_set_wptr - commit the write pointer
178 *
179 * @ring: amdgpu ring pointer
180 *
181 * Write the wptr back to the hardware (CIK+).
182 */
183static void cik_sdma_ring_set_wptr(struct amdgpu_ring *ring)
184{
185 struct amdgpu_device *adev = ring->adev;
Alex Deucherc113ea12015-10-08 16:30:37 -0400186 u32 me = (ring == &adev->sdma.instance[0].ring) ? 0 : 1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400187
188 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[me], (ring->wptr << 2) & 0x3fffc);
189}
190
Jammy Zhouac01db32015-09-01 13:13:54 +0800191static void cik_sdma_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
192{
Alex Deucherc113ea12015-10-08 16:30:37 -0400193 struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
Jammy Zhouac01db32015-09-01 13:13:54 +0800194 int i;
195
196 for (i = 0; i < count; i++)
197 if (sdma && sdma->burst_nop && (i == 0))
198 amdgpu_ring_write(ring, ring->nop |
199 SDMA_NOP_COUNT(count - 1));
200 else
201 amdgpu_ring_write(ring, ring->nop);
202}
203
Alex Deuchera2e73f52015-04-20 17:09:27 -0400204/**
205 * cik_sdma_ring_emit_ib - Schedule an IB on the DMA engine
206 *
207 * @ring: amdgpu ring pointer
208 * @ib: IB object to schedule
209 *
210 * Schedule an IB in the DMA ring (CIK).
211 */
212static void cik_sdma_ring_emit_ib(struct amdgpu_ring *ring,
213 struct amdgpu_ib *ib)
214{
215 u32 extra_bits = (ib->vm ? ib->vm->ids[ring->idx].id : 0) & 0xf;
216 u32 next_rptr = ring->wptr + 5;
217
Alex Deuchera2e73f52015-04-20 17:09:27 -0400218 while ((next_rptr & 7) != 4)
219 next_rptr++;
220
221 next_rptr += 4;
222 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
223 amdgpu_ring_write(ring, ring->next_rptr_gpu_addr & 0xfffffffc);
224 amdgpu_ring_write(ring, upper_32_bits(ring->next_rptr_gpu_addr) & 0xffffffff);
225 amdgpu_ring_write(ring, 1); /* number of DWs to follow */
226 amdgpu_ring_write(ring, next_rptr);
227
Alex Deuchera2e73f52015-04-20 17:09:27 -0400228 /* IB packet must end on a 8 DW boundary */
Jammy Zhouac01db32015-09-01 13:13:54 +0800229 cik_sdma_ring_insert_nop(ring, (12 - (ring->wptr & 7)) % 8);
230
Alex Deuchera2e73f52015-04-20 17:09:27 -0400231 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_INDIRECT_BUFFER, 0, extra_bits));
232 amdgpu_ring_write(ring, ib->gpu_addr & 0xffffffe0); /* base must be 32 byte aligned */
233 amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr) & 0xffffffff);
234 amdgpu_ring_write(ring, ib->length_dw);
235
236}
237
238/**
Christian Königd2edb072015-05-11 14:10:34 +0200239 * cik_sdma_ring_emit_hdp_flush - emit an hdp flush on the DMA ring
Alex Deuchera2e73f52015-04-20 17:09:27 -0400240 *
241 * @ring: amdgpu ring pointer
242 *
243 * Emit an hdp flush packet on the requested DMA ring.
244 */
Christian Königd2edb072015-05-11 14:10:34 +0200245static void cik_sdma_ring_emit_hdp_flush(struct amdgpu_ring *ring)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400246{
247 u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(1) |
248 SDMA_POLL_REG_MEM_EXTRA_FUNC(3)); /* == */
249 u32 ref_and_mask;
250
Alex Deucherc113ea12015-10-08 16:30:37 -0400251 if (ring == &ring->adev->sdma.instance[0].ring)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400252 ref_and_mask = GPU_HDP_FLUSH_DONE__SDMA0_MASK;
253 else
254 ref_and_mask = GPU_HDP_FLUSH_DONE__SDMA1_MASK;
255
256 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
257 amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_DONE << 2);
258 amdgpu_ring_write(ring, mmGPU_HDP_FLUSH_REQ << 2);
259 amdgpu_ring_write(ring, ref_and_mask); /* reference */
260 amdgpu_ring_write(ring, ref_and_mask); /* mask */
261 amdgpu_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
262}
263
264/**
265 * cik_sdma_ring_emit_fence - emit a fence on the DMA ring
266 *
267 * @ring: amdgpu ring pointer
268 * @fence: amdgpu fence object
269 *
270 * Add a DMA fence packet to the ring to write
271 * the fence seq number and DMA trap packet to generate
272 * an interrupt if needed (CIK).
273 */
274static void cik_sdma_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +0800275 unsigned flags)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400276{
Chunming Zhou890ee232015-06-01 14:35:03 +0800277 bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400278 /* write the fence */
279 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
280 amdgpu_ring_write(ring, lower_32_bits(addr));
281 amdgpu_ring_write(ring, upper_32_bits(addr));
282 amdgpu_ring_write(ring, lower_32_bits(seq));
283
284 /* optionally write high bits as well */
285 if (write64bit) {
286 addr += 4;
287 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
288 amdgpu_ring_write(ring, lower_32_bits(addr));
289 amdgpu_ring_write(ring, upper_32_bits(addr));
290 amdgpu_ring_write(ring, upper_32_bits(seq));
291 }
292
293 /* generate an interrupt */
294 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_TRAP, 0, 0));
295}
296
297/**
Alex Deuchera2e73f52015-04-20 17:09:27 -0400298 * cik_sdma_gfx_stop - stop the gfx async dma engines
299 *
300 * @adev: amdgpu_device pointer
301 *
302 * Stop the gfx async dma ring buffers (CIK).
303 */
304static void cik_sdma_gfx_stop(struct amdgpu_device *adev)
305{
Alex Deucherc113ea12015-10-08 16:30:37 -0400306 struct amdgpu_ring *sdma0 = &adev->sdma.instance[0].ring;
307 struct amdgpu_ring *sdma1 = &adev->sdma.instance[1].ring;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400308 u32 rb_cntl;
309 int i;
310
311 if ((adev->mman.buffer_funcs_ring == sdma0) ||
312 (adev->mman.buffer_funcs_ring == sdma1))
313 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
314
Alex Deucherc113ea12015-10-08 16:30:37 -0400315 for (i = 0; i < adev->sdma.num_instances; i++) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400316 rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
317 rb_cntl &= ~SDMA0_GFX_RB_CNTL__RB_ENABLE_MASK;
318 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
319 WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], 0);
320 }
321 sdma0->ready = false;
322 sdma1->ready = false;
323}
324
325/**
326 * cik_sdma_rlc_stop - stop the compute async dma engines
327 *
328 * @adev: amdgpu_device pointer
329 *
330 * Stop the compute async dma queues (CIK).
331 */
332static void cik_sdma_rlc_stop(struct amdgpu_device *adev)
333{
334 /* XXX todo */
335}
336
337/**
338 * cik_sdma_enable - stop the async dma engines
339 *
340 * @adev: amdgpu_device pointer
341 * @enable: enable/disable the DMA MEs.
342 *
343 * Halt or unhalt the async dma engines (CIK).
344 */
345static void cik_sdma_enable(struct amdgpu_device *adev, bool enable)
346{
347 u32 me_cntl;
348 int i;
349
350 if (enable == false) {
351 cik_sdma_gfx_stop(adev);
352 cik_sdma_rlc_stop(adev);
353 }
354
Alex Deucherc113ea12015-10-08 16:30:37 -0400355 for (i = 0; i < adev->sdma.num_instances; i++) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400356 me_cntl = RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]);
357 if (enable)
358 me_cntl &= ~SDMA0_F32_CNTL__HALT_MASK;
359 else
360 me_cntl |= SDMA0_F32_CNTL__HALT_MASK;
361 WREG32(mmSDMA0_F32_CNTL + sdma_offsets[i], me_cntl);
362 }
363}
364
365/**
366 * cik_sdma_gfx_resume - setup and start the async dma engines
367 *
368 * @adev: amdgpu_device pointer
369 *
370 * Set up the gfx DMA ring buffers and enable them (CIK).
371 * Returns 0 for success, error for failure.
372 */
373static int cik_sdma_gfx_resume(struct amdgpu_device *adev)
374{
375 struct amdgpu_ring *ring;
376 u32 rb_cntl, ib_cntl;
377 u32 rb_bufsz;
378 u32 wb_offset;
379 int i, j, r;
380
Alex Deucherc113ea12015-10-08 16:30:37 -0400381 for (i = 0; i < adev->sdma.num_instances; i++) {
382 ring = &adev->sdma.instance[i].ring;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400383 wb_offset = (ring->rptr_offs * 4);
384
385 mutex_lock(&adev->srbm_mutex);
386 for (j = 0; j < 16; j++) {
387 cik_srbm_select(adev, 0, 0, 0, j);
388 /* SDMA GFX */
389 WREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i], 0);
390 WREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i], 0);
391 /* XXX SDMA RLC - todo */
392 }
393 cik_srbm_select(adev, 0, 0, 0, 0);
394 mutex_unlock(&adev->srbm_mutex);
395
396 WREG32(mmSDMA0_SEM_INCOMPLETE_TIMER_CNTL + sdma_offsets[i], 0);
397 WREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i], 0);
398
399 /* Set ring buffer size in dwords */
400 rb_bufsz = order_base_2(ring->ring_size / 4);
401 rb_cntl = rb_bufsz << 1;
402#ifdef __BIG_ENDIAN
Alex Deucher454fc952015-06-09 09:58:23 -0400403 rb_cntl |= SDMA0_GFX_RB_CNTL__RB_SWAP_ENABLE_MASK |
404 SDMA0_GFX_RB_CNTL__RPTR_WRITEBACK_SWAP_ENABLE_MASK;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400405#endif
406 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
407
408 /* Initialize the ring buffer's read and write pointers */
409 WREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i], 0);
410 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], 0);
411
412 /* set the wb address whether it's enabled or not */
413 WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i],
414 upper_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
415 WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i],
416 ((adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
417
418 rb_cntl |= SDMA0_GFX_RB_CNTL__RPTR_WRITEBACK_ENABLE_MASK;
419
420 WREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i], ring->gpu_addr >> 8);
421 WREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i], ring->gpu_addr >> 40);
422
423 ring->wptr = 0;
424 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], ring->wptr << 2);
425
426 /* enable DMA RB */
427 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i],
428 rb_cntl | SDMA0_GFX_RB_CNTL__RB_ENABLE_MASK);
429
430 ib_cntl = SDMA0_GFX_IB_CNTL__IB_ENABLE_MASK;
431#ifdef __BIG_ENDIAN
432 ib_cntl |= SDMA0_GFX_IB_CNTL__IB_SWAP_ENABLE_MASK;
433#endif
434 /* enable DMA IBs */
435 WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
436
437 ring->ready = true;
438
439 r = amdgpu_ring_test_ring(ring);
440 if (r) {
441 ring->ready = false;
442 return r;
443 }
444
445 if (adev->mman.buffer_funcs_ring == ring)
446 amdgpu_ttm_set_active_vram_size(adev, adev->mc.real_vram_size);
447 }
448
449 return 0;
450}
451
452/**
453 * cik_sdma_rlc_resume - setup and start the async dma engines
454 *
455 * @adev: amdgpu_device pointer
456 *
457 * Set up the compute DMA queues and enable them (CIK).
458 * Returns 0 for success, error for failure.
459 */
460static int cik_sdma_rlc_resume(struct amdgpu_device *adev)
461{
462 /* XXX todo */
463 return 0;
464}
465
466/**
467 * cik_sdma_load_microcode - load the sDMA ME ucode
468 *
469 * @adev: amdgpu_device pointer
470 *
471 * Loads the sDMA0/1 ucode.
472 * Returns 0 for success, -EINVAL if the ucode is not available.
473 */
474static int cik_sdma_load_microcode(struct amdgpu_device *adev)
475{
476 const struct sdma_firmware_header_v1_0 *hdr;
477 const __le32 *fw_data;
478 u32 fw_size;
479 int i, j;
480
Alex Deuchera2e73f52015-04-20 17:09:27 -0400481 /* halt the MEs */
482 cik_sdma_enable(adev, false);
483
Alex Deucherc113ea12015-10-08 16:30:37 -0400484 for (i = 0; i < adev->sdma.num_instances; i++) {
485 if (!adev->sdma.instance[i].fw)
486 return -EINVAL;
487 hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400488 amdgpu_ucode_print_sdma_hdr(&hdr->header);
489 fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
Alex Deucherc113ea12015-10-08 16:30:37 -0400490 adev->sdma.instance[i].fw_version = le32_to_cpu(hdr->header.ucode_version);
491 adev->sdma.instance[i].feature_version = le32_to_cpu(hdr->ucode_feature_version);
492 if (adev->sdma.instance[i].feature_version >= 20)
493 adev->sdma.instance[i].burst_nop = true;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400494 fw_data = (const __le32 *)
Alex Deucherc113ea12015-10-08 16:30:37 -0400495 (adev->sdma.instance[i].fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
Alex Deuchera2e73f52015-04-20 17:09:27 -0400496 WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], 0);
497 for (j = 0; j < fw_size; j++)
498 WREG32(mmSDMA0_UCODE_DATA + sdma_offsets[i], le32_to_cpup(fw_data++));
Alex Deucherc113ea12015-10-08 16:30:37 -0400499 WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], adev->sdma.instance[i].fw_version);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400500 }
501
502 return 0;
503}
504
505/**
506 * cik_sdma_start - setup and start the async dma engines
507 *
508 * @adev: amdgpu_device pointer
509 *
510 * Set up the DMA engines and enable them (CIK).
511 * Returns 0 for success, error for failure.
512 */
513static int cik_sdma_start(struct amdgpu_device *adev)
514{
515 int r;
516
517 r = cik_sdma_load_microcode(adev);
518 if (r)
519 return r;
520
521 /* unhalt the MEs */
522 cik_sdma_enable(adev, true);
523
524 /* start the gfx rings and rlc compute queues */
525 r = cik_sdma_gfx_resume(adev);
526 if (r)
527 return r;
528 r = cik_sdma_rlc_resume(adev);
529 if (r)
530 return r;
531
532 return 0;
533}
534
535/**
536 * cik_sdma_ring_test_ring - simple async dma engine test
537 *
538 * @ring: amdgpu_ring structure holding ring information
539 *
540 * Test the DMA engine by writing using it to write an
541 * value to memory. (CIK).
542 * Returns 0 for success, error for failure.
543 */
544static int cik_sdma_ring_test_ring(struct amdgpu_ring *ring)
545{
546 struct amdgpu_device *adev = ring->adev;
547 unsigned i;
548 unsigned index;
549 int r;
550 u32 tmp;
551 u64 gpu_addr;
552
553 r = amdgpu_wb_get(adev, &index);
554 if (r) {
555 dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
556 return r;
557 }
558
559 gpu_addr = adev->wb.gpu_addr + (index * 4);
560 tmp = 0xCAFEDEAD;
561 adev->wb.wb[index] = cpu_to_le32(tmp);
562
563 r = amdgpu_ring_lock(ring, 5);
564 if (r) {
565 DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
566 amdgpu_wb_free(adev, index);
567 return r;
568 }
569 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
570 amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
571 amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
572 amdgpu_ring_write(ring, 1); /* number of DWs to follow */
573 amdgpu_ring_write(ring, 0xDEADBEEF);
574 amdgpu_ring_unlock_commit(ring);
575
576 for (i = 0; i < adev->usec_timeout; i++) {
577 tmp = le32_to_cpu(adev->wb.wb[index]);
578 if (tmp == 0xDEADBEEF)
579 break;
580 DRM_UDELAY(1);
581 }
582
583 if (i < adev->usec_timeout) {
584 DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
585 } else {
586 DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
587 ring->idx, tmp);
588 r = -EINVAL;
589 }
590 amdgpu_wb_free(adev, index);
591
592 return r;
593}
594
595/**
596 * cik_sdma_ring_test_ib - test an IB on the DMA engine
597 *
598 * @ring: amdgpu_ring structure holding ring information
599 *
600 * Test a simple IB in the DMA ring (CIK).
601 * Returns 0 on success, error on failure.
602 */
603static int cik_sdma_ring_test_ib(struct amdgpu_ring *ring)
604{
605 struct amdgpu_device *adev = ring->adev;
606 struct amdgpu_ib ib;
Chunming Zhou17635522015-08-03 11:43:19 +0800607 struct fence *f = NULL;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400608 unsigned i;
609 unsigned index;
610 int r;
611 u32 tmp = 0;
612 u64 gpu_addr;
613
614 r = amdgpu_wb_get(adev, &index);
615 if (r) {
616 dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
617 return r;
618 }
619
620 gpu_addr = adev->wb.gpu_addr + (index * 4);
621 tmp = 0xCAFEDEAD;
622 adev->wb.wb[index] = cpu_to_le32(tmp);
Christian Königb203dd92015-08-18 18:23:16 +0200623 memset(&ib, 0, sizeof(ib));
Alex Deuchera2e73f52015-04-20 17:09:27 -0400624 r = amdgpu_ib_get(ring, NULL, 256, &ib);
625 if (r) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400626 DRM_ERROR("amdgpu: failed to get ib (%d).\n", r);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800627 goto err0;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400628 }
629
630 ib.ptr[0] = SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
631 ib.ptr[1] = lower_32_bits(gpu_addr);
632 ib.ptr[2] = upper_32_bits(gpu_addr);
633 ib.ptr[3] = 1;
634 ib.ptr[4] = 0xDEADBEEF;
635 ib.length_dw = 5;
Chunming Zhou0011fda2015-06-01 15:33:20 +0800636 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, &ib, 1, NULL,
Chunming Zhou17635522015-08-03 11:43:19 +0800637 AMDGPU_FENCE_OWNER_UNDEFINED,
638 &f);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800639 if (r)
640 goto err1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400641
Chunming Zhou17635522015-08-03 11:43:19 +0800642 r = fence_wait(f, false);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400643 if (r) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400644 DRM_ERROR("amdgpu: fence wait failed (%d).\n", r);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800645 goto err1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400646 }
647 for (i = 0; i < adev->usec_timeout; i++) {
648 tmp = le32_to_cpu(adev->wb.wb[index]);
649 if (tmp == 0xDEADBEEF)
650 break;
651 DRM_UDELAY(1);
652 }
653 if (i < adev->usec_timeout) {
654 DRM_INFO("ib test on ring %d succeeded in %u usecs\n",
Chunming Zhou0011fda2015-06-01 15:33:20 +0800655 ring->idx, i);
656 goto err1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400657 } else {
658 DRM_ERROR("amdgpu: ib test failed (0x%08X)\n", tmp);
659 r = -EINVAL;
660 }
Chunming Zhou0011fda2015-06-01 15:33:20 +0800661
662err1:
Chunming Zhou281b4222015-08-12 12:58:31 +0800663 fence_put(f);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400664 amdgpu_ib_free(adev, &ib);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800665err0:
Alex Deuchera2e73f52015-04-20 17:09:27 -0400666 amdgpu_wb_free(adev, index);
667 return r;
668}
669
670/**
671 * cik_sdma_vm_copy_pages - update PTEs by copying them from the GART
672 *
673 * @ib: indirect buffer to fill with commands
674 * @pe: addr of the page entry
675 * @src: src addr to copy from
676 * @count: number of page entries to update
677 *
678 * Update PTEs by copying them from the GART using sDMA (CIK).
679 */
680static void cik_sdma_vm_copy_pte(struct amdgpu_ib *ib,
681 uint64_t pe, uint64_t src,
682 unsigned count)
683{
684 while (count) {
685 unsigned bytes = count * 8;
686 if (bytes > 0x1FFFF8)
687 bytes = 0x1FFFF8;
688
689 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY,
690 SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
691 ib->ptr[ib->length_dw++] = bytes;
692 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
693 ib->ptr[ib->length_dw++] = lower_32_bits(src);
694 ib->ptr[ib->length_dw++] = upper_32_bits(src);
695 ib->ptr[ib->length_dw++] = lower_32_bits(pe);
696 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
697
698 pe += bytes;
699 src += bytes;
700 count -= bytes / 8;
701 }
702}
703
704/**
705 * cik_sdma_vm_write_pages - update PTEs by writing them manually
706 *
707 * @ib: indirect buffer to fill with commands
708 * @pe: addr of the page entry
709 * @addr: dst addr to write into pe
710 * @count: number of page entries to update
711 * @incr: increase next addr by incr bytes
712 * @flags: access flags
713 *
714 * Update PTEs by writing them manually using sDMA (CIK).
715 */
716static void cik_sdma_vm_write_pte(struct amdgpu_ib *ib,
717 uint64_t pe,
718 uint64_t addr, unsigned count,
719 uint32_t incr, uint32_t flags)
720{
721 uint64_t value;
722 unsigned ndw;
723
724 while (count) {
725 ndw = count * 2;
726 if (ndw > 0xFFFFE)
727 ndw = 0xFFFFE;
728
729 /* for non-physically contiguous pages (system) */
730 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_WRITE,
731 SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
732 ib->ptr[ib->length_dw++] = pe;
733 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
734 ib->ptr[ib->length_dw++] = ndw;
735 for (; ndw > 0; ndw -= 2, --count, pe += 8) {
736 if (flags & AMDGPU_PTE_SYSTEM) {
737 value = amdgpu_vm_map_gart(ib->ring->adev, addr);
738 value &= 0xFFFFFFFFFFFFF000ULL;
739 } else if (flags & AMDGPU_PTE_VALID) {
740 value = addr;
741 } else {
742 value = 0;
743 }
744 addr += incr;
745 value |= flags;
746 ib->ptr[ib->length_dw++] = value;
747 ib->ptr[ib->length_dw++] = upper_32_bits(value);
748 }
749 }
750}
751
752/**
753 * cik_sdma_vm_set_pages - update the page tables using sDMA
754 *
755 * @ib: indirect buffer to fill with commands
756 * @pe: addr of the page entry
757 * @addr: dst addr to write into pe
758 * @count: number of page entries to update
759 * @incr: increase next addr by incr bytes
760 * @flags: access flags
761 *
762 * Update the page tables using sDMA (CIK).
763 */
764static void cik_sdma_vm_set_pte_pde(struct amdgpu_ib *ib,
765 uint64_t pe,
766 uint64_t addr, unsigned count,
767 uint32_t incr, uint32_t flags)
768{
769 uint64_t value;
770 unsigned ndw;
771
772 while (count) {
773 ndw = count;
774 if (ndw > 0x7FFFF)
775 ndw = 0x7FFFF;
776
777 if (flags & AMDGPU_PTE_VALID)
778 value = addr;
779 else
780 value = 0;
781
782 /* for physically contiguous pages (vram) */
783 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_GENERATE_PTE_PDE, 0, 0);
784 ib->ptr[ib->length_dw++] = pe; /* dst addr */
785 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
786 ib->ptr[ib->length_dw++] = flags; /* mask */
787 ib->ptr[ib->length_dw++] = 0;
788 ib->ptr[ib->length_dw++] = value; /* value */
789 ib->ptr[ib->length_dw++] = upper_32_bits(value);
790 ib->ptr[ib->length_dw++] = incr; /* increment size */
791 ib->ptr[ib->length_dw++] = 0;
792 ib->ptr[ib->length_dw++] = ndw; /* number of entries */
793
794 pe += ndw * 8;
795 addr += ndw * incr;
796 count -= ndw;
797 }
798}
799
800/**
801 * cik_sdma_vm_pad_ib - pad the IB to the required number of dw
802 *
803 * @ib: indirect buffer to fill with padding
804 *
805 */
806static void cik_sdma_vm_pad_ib(struct amdgpu_ib *ib)
807{
Alex Deucherc113ea12015-10-08 16:30:37 -0400808 struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ib->ring);
Jammy Zhouac01db32015-09-01 13:13:54 +0800809 u32 pad_count;
810 int i;
811
812 pad_count = (8 - (ib->length_dw & 0x7)) % 8;
813 for (i = 0; i < pad_count; i++)
814 if (sdma && sdma->burst_nop && (i == 0))
815 ib->ptr[ib->length_dw++] =
816 SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0) |
817 SDMA_NOP_COUNT(pad_count - 1);
818 else
819 ib->ptr[ib->length_dw++] =
820 SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400821}
822
823/**
824 * cik_sdma_ring_emit_vm_flush - cik vm flush using sDMA
825 *
826 * @ring: amdgpu_ring pointer
827 * @vm: amdgpu_vm pointer
828 *
829 * Update the page table base and flush the VM TLB
830 * using sDMA (CIK).
831 */
832static void cik_sdma_ring_emit_vm_flush(struct amdgpu_ring *ring,
833 unsigned vm_id, uint64_t pd_addr)
834{
835 u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(0) |
836 SDMA_POLL_REG_MEM_EXTRA_FUNC(0)); /* always */
837
838 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
839 if (vm_id < 8) {
840 amdgpu_ring_write(ring, (mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR + vm_id));
841 } else {
842 amdgpu_ring_write(ring, (mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vm_id - 8));
843 }
844 amdgpu_ring_write(ring, pd_addr >> 12);
845
Alex Deuchera2e73f52015-04-20 17:09:27 -0400846 /* flush TLB */
847 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
848 amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST);
849 amdgpu_ring_write(ring, 1 << vm_id);
850
851 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
852 amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST << 2);
853 amdgpu_ring_write(ring, 0);
854 amdgpu_ring_write(ring, 0); /* reference */
855 amdgpu_ring_write(ring, 0); /* mask */
856 amdgpu_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
857}
858
859static void cik_enable_sdma_mgcg(struct amdgpu_device *adev,
860 bool enable)
861{
862 u32 orig, data;
863
864 if (enable && (adev->cg_flags & AMDGPU_CG_SUPPORT_SDMA_MGCG)) {
865 WREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET, 0x00000100);
866 WREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET, 0x00000100);
867 } else {
868 orig = data = RREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET);
869 data |= 0xff000000;
870 if (data != orig)
871 WREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET, data);
872
873 orig = data = RREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET);
874 data |= 0xff000000;
875 if (data != orig)
876 WREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET, data);
877 }
878}
879
880static void cik_enable_sdma_mgls(struct amdgpu_device *adev,
881 bool enable)
882{
883 u32 orig, data;
884
885 if (enable && (adev->cg_flags & AMDGPU_CG_SUPPORT_SDMA_LS)) {
886 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET);
887 data |= 0x100;
888 if (orig != data)
889 WREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET, data);
890
891 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET);
892 data |= 0x100;
893 if (orig != data)
894 WREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET, data);
895 } else {
896 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET);
897 data &= ~0x100;
898 if (orig != data)
899 WREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET, data);
900
901 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET);
902 data &= ~0x100;
903 if (orig != data)
904 WREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET, data);
905 }
906}
907
yanyang15fc3aee2015-05-22 14:39:35 -0400908static int cik_sdma_early_init(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400909{
yanyang15fc3aee2015-05-22 14:39:35 -0400910 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
911
Alex Deucherc113ea12015-10-08 16:30:37 -0400912 adev->sdma.num_instances = SDMA_MAX_INSTANCE;
913
Alex Deuchera2e73f52015-04-20 17:09:27 -0400914 cik_sdma_set_ring_funcs(adev);
915 cik_sdma_set_irq_funcs(adev);
916 cik_sdma_set_buffer_funcs(adev);
917 cik_sdma_set_vm_pte_funcs(adev);
918
919 return 0;
920}
921
yanyang15fc3aee2015-05-22 14:39:35 -0400922static int cik_sdma_sw_init(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400923{
924 struct amdgpu_ring *ring;
yanyang15fc3aee2015-05-22 14:39:35 -0400925 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucherc113ea12015-10-08 16:30:37 -0400926 int r, i;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400927
928 r = cik_sdma_init_microcode(adev);
929 if (r) {
930 DRM_ERROR("Failed to load sdma firmware!\n");
931 return r;
932 }
933
934 /* SDMA trap event */
Alex Deucherc113ea12015-10-08 16:30:37 -0400935 r = amdgpu_irq_add_id(adev, 224, &adev->sdma.trap_irq);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400936 if (r)
937 return r;
938
939 /* SDMA Privileged inst */
Alex Deucherc113ea12015-10-08 16:30:37 -0400940 r = amdgpu_irq_add_id(adev, 241, &adev->sdma.illegal_inst_irq);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400941 if (r)
942 return r;
943
944 /* SDMA Privileged inst */
Alex Deucherc113ea12015-10-08 16:30:37 -0400945 r = amdgpu_irq_add_id(adev, 247, &adev->sdma.illegal_inst_irq);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400946 if (r)
947 return r;
948
Alex Deucherc113ea12015-10-08 16:30:37 -0400949 for (i = 0; i < adev->sdma.num_instances; i++) {
950 ring = &adev->sdma.instance[i].ring;
951 ring->ring_obj = NULL;
952 sprintf(ring->name, "sdma%d", i);
953 r = amdgpu_ring_init(adev, ring, 256 * 1024,
954 SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0), 0xf,
955 &adev->sdma.trap_irq,
956 (i == 0) ?
957 AMDGPU_SDMA_IRQ_TRAP0 : AMDGPU_SDMA_IRQ_TRAP1,
958 AMDGPU_RING_TYPE_SDMA);
959 if (r)
960 return r;
961 }
Alex Deuchera2e73f52015-04-20 17:09:27 -0400962
963 return r;
964}
965
yanyang15fc3aee2015-05-22 14:39:35 -0400966static int cik_sdma_sw_fini(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400967{
yanyang15fc3aee2015-05-22 14:39:35 -0400968 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucherc113ea12015-10-08 16:30:37 -0400969 int i;
yanyang15fc3aee2015-05-22 14:39:35 -0400970
Alex Deucherc113ea12015-10-08 16:30:37 -0400971 for (i = 0; i < adev->sdma.num_instances; i++)
972 amdgpu_ring_fini(&adev->sdma.instance[i].ring);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400973
974 return 0;
975}
976
yanyang15fc3aee2015-05-22 14:39:35 -0400977static int cik_sdma_hw_init(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400978{
979 int r;
yanyang15fc3aee2015-05-22 14:39:35 -0400980 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400981
982 r = cik_sdma_start(adev);
983 if (r)
984 return r;
985
986 return r;
987}
988
yanyang15fc3aee2015-05-22 14:39:35 -0400989static int cik_sdma_hw_fini(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400990{
yanyang15fc3aee2015-05-22 14:39:35 -0400991 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
992
Alex Deuchera2e73f52015-04-20 17:09:27 -0400993 cik_sdma_enable(adev, false);
994
995 return 0;
996}
997
yanyang15fc3aee2015-05-22 14:39:35 -0400998static int cik_sdma_suspend(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400999{
yanyang15fc3aee2015-05-22 14:39:35 -04001000 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001001
1002 return cik_sdma_hw_fini(adev);
1003}
1004
yanyang15fc3aee2015-05-22 14:39:35 -04001005static int cik_sdma_resume(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001006{
yanyang15fc3aee2015-05-22 14:39:35 -04001007 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001008
1009 return cik_sdma_hw_init(adev);
1010}
1011
yanyang15fc3aee2015-05-22 14:39:35 -04001012static bool cik_sdma_is_idle(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001013{
yanyang15fc3aee2015-05-22 14:39:35 -04001014 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001015 u32 tmp = RREG32(mmSRBM_STATUS2);
1016
1017 if (tmp & (SRBM_STATUS2__SDMA_BUSY_MASK |
1018 SRBM_STATUS2__SDMA1_BUSY_MASK))
1019 return false;
1020
1021 return true;
1022}
1023
yanyang15fc3aee2015-05-22 14:39:35 -04001024static int cik_sdma_wait_for_idle(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001025{
1026 unsigned i;
1027 u32 tmp;
yanyang15fc3aee2015-05-22 14:39:35 -04001028 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001029
1030 for (i = 0; i < adev->usec_timeout; i++) {
1031 tmp = RREG32(mmSRBM_STATUS2) & (SRBM_STATUS2__SDMA_BUSY_MASK |
1032 SRBM_STATUS2__SDMA1_BUSY_MASK);
1033
1034 if (!tmp)
1035 return 0;
1036 udelay(1);
1037 }
1038 return -ETIMEDOUT;
1039}
1040
yanyang15fc3aee2015-05-22 14:39:35 -04001041static void cik_sdma_print_status(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001042{
1043 int i, j;
yanyang15fc3aee2015-05-22 14:39:35 -04001044 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001045
1046 dev_info(adev->dev, "CIK SDMA registers\n");
1047 dev_info(adev->dev, " SRBM_STATUS2=0x%08X\n",
1048 RREG32(mmSRBM_STATUS2));
Alex Deucherc113ea12015-10-08 16:30:37 -04001049 for (i = 0; i < adev->sdma.num_instances; i++) {
Alex Deuchera2e73f52015-04-20 17:09:27 -04001050 dev_info(adev->dev, " SDMA%d_STATUS_REG=0x%08X\n",
1051 i, RREG32(mmSDMA0_STATUS_REG + sdma_offsets[i]));
1052 dev_info(adev->dev, " SDMA%d_ME_CNTL=0x%08X\n",
1053 i, RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]));
1054 dev_info(adev->dev, " SDMA%d_CNTL=0x%08X\n",
1055 i, RREG32(mmSDMA0_CNTL + sdma_offsets[i]));
1056 dev_info(adev->dev, " SDMA%d_SEM_INCOMPLETE_TIMER_CNTL=0x%08X\n",
1057 i, RREG32(mmSDMA0_SEM_INCOMPLETE_TIMER_CNTL + sdma_offsets[i]));
1058 dev_info(adev->dev, " SDMA%d_SEM_WAIT_FAIL_TIMER_CNTL=0x%08X\n",
1059 i, RREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i]));
1060 dev_info(adev->dev, " SDMA%d_GFX_IB_CNTL=0x%08X\n",
1061 i, RREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i]));
1062 dev_info(adev->dev, " SDMA%d_GFX_RB_CNTL=0x%08X\n",
1063 i, RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]));
1064 dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR=0x%08X\n",
1065 i, RREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i]));
1066 dev_info(adev->dev, " SDMA%d_GFX_RB_WPTR=0x%08X\n",
1067 i, RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i]));
1068 dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR_ADDR_HI=0x%08X\n",
1069 i, RREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i]));
1070 dev_info(adev->dev, " SDMA%d_GFX_RB_RPTR_ADDR_LO=0x%08X\n",
1071 i, RREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i]));
1072 dev_info(adev->dev, " SDMA%d_GFX_RB_BASE=0x%08X\n",
1073 i, RREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i]));
1074 dev_info(adev->dev, " SDMA%d_GFX_RB_BASE_HI=0x%08X\n",
1075 i, RREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i]));
1076 mutex_lock(&adev->srbm_mutex);
1077 for (j = 0; j < 16; j++) {
1078 cik_srbm_select(adev, 0, 0, 0, j);
1079 dev_info(adev->dev, " VM %d:\n", j);
1080 dev_info(adev->dev, " SDMA0_GFX_VIRTUAL_ADDR=0x%08X\n",
1081 RREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i]));
1082 dev_info(adev->dev, " SDMA0_GFX_APE1_CNTL=0x%08X\n",
1083 RREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i]));
1084 }
1085 cik_srbm_select(adev, 0, 0, 0, 0);
1086 mutex_unlock(&adev->srbm_mutex);
1087 }
1088}
1089
yanyang15fc3aee2015-05-22 14:39:35 -04001090static int cik_sdma_soft_reset(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001091{
1092 u32 srbm_soft_reset = 0;
yanyang15fc3aee2015-05-22 14:39:35 -04001093 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001094 u32 tmp = RREG32(mmSRBM_STATUS2);
1095
1096 if (tmp & SRBM_STATUS2__SDMA_BUSY_MASK) {
1097 /* sdma0 */
1098 tmp = RREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET);
1099 tmp |= SDMA0_F32_CNTL__HALT_MASK;
1100 WREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET, tmp);
1101 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
1102 }
1103 if (tmp & SRBM_STATUS2__SDMA1_BUSY_MASK) {
1104 /* sdma1 */
1105 tmp = RREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET);
1106 tmp |= SDMA0_F32_CNTL__HALT_MASK;
1107 WREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET, tmp);
1108 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
1109 }
1110
1111 if (srbm_soft_reset) {
yanyang15fc3aee2015-05-22 14:39:35 -04001112 cik_sdma_print_status((void *)adev);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001113
1114 tmp = RREG32(mmSRBM_SOFT_RESET);
1115 tmp |= srbm_soft_reset;
1116 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
1117 WREG32(mmSRBM_SOFT_RESET, tmp);
1118 tmp = RREG32(mmSRBM_SOFT_RESET);
1119
1120 udelay(50);
1121
1122 tmp &= ~srbm_soft_reset;
1123 WREG32(mmSRBM_SOFT_RESET, tmp);
1124 tmp = RREG32(mmSRBM_SOFT_RESET);
1125
1126 /* Wait a little for things to settle down */
1127 udelay(50);
1128
yanyang15fc3aee2015-05-22 14:39:35 -04001129 cik_sdma_print_status((void *)adev);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001130 }
1131
1132 return 0;
1133}
1134
1135static int cik_sdma_set_trap_irq_state(struct amdgpu_device *adev,
1136 struct amdgpu_irq_src *src,
1137 unsigned type,
1138 enum amdgpu_interrupt_state state)
1139{
1140 u32 sdma_cntl;
1141
1142 switch (type) {
1143 case AMDGPU_SDMA_IRQ_TRAP0:
1144 switch (state) {
1145 case AMDGPU_IRQ_STATE_DISABLE:
1146 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1147 sdma_cntl &= ~SDMA0_CNTL__TRAP_ENABLE_MASK;
1148 WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1149 break;
1150 case AMDGPU_IRQ_STATE_ENABLE:
1151 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1152 sdma_cntl |= SDMA0_CNTL__TRAP_ENABLE_MASK;
1153 WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1154 break;
1155 default:
1156 break;
1157 }
1158 break;
1159 case AMDGPU_SDMA_IRQ_TRAP1:
1160 switch (state) {
1161 case AMDGPU_IRQ_STATE_DISABLE:
1162 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1163 sdma_cntl &= ~SDMA0_CNTL__TRAP_ENABLE_MASK;
1164 WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1165 break;
1166 case AMDGPU_IRQ_STATE_ENABLE:
1167 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1168 sdma_cntl |= SDMA0_CNTL__TRAP_ENABLE_MASK;
1169 WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1170 break;
1171 default:
1172 break;
1173 }
1174 break;
1175 default:
1176 break;
1177 }
1178 return 0;
1179}
1180
1181static int cik_sdma_process_trap_irq(struct amdgpu_device *adev,
1182 struct amdgpu_irq_src *source,
1183 struct amdgpu_iv_entry *entry)
1184{
1185 u8 instance_id, queue_id;
1186
1187 instance_id = (entry->ring_id & 0x3) >> 0;
1188 queue_id = (entry->ring_id & 0xc) >> 2;
1189 DRM_DEBUG("IH: SDMA trap\n");
1190 switch (instance_id) {
1191 case 0:
1192 switch (queue_id) {
1193 case 0:
Alex Deucherc113ea12015-10-08 16:30:37 -04001194 amdgpu_fence_process(&adev->sdma.instance[0].ring);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001195 break;
1196 case 1:
1197 /* XXX compute */
1198 break;
1199 case 2:
1200 /* XXX compute */
1201 break;
1202 }
1203 break;
1204 case 1:
1205 switch (queue_id) {
1206 case 0:
Alex Deucherc113ea12015-10-08 16:30:37 -04001207 amdgpu_fence_process(&adev->sdma.instance[1].ring);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001208 break;
1209 case 1:
1210 /* XXX compute */
1211 break;
1212 case 2:
1213 /* XXX compute */
1214 break;
1215 }
1216 break;
1217 }
1218
1219 return 0;
1220}
1221
1222static int cik_sdma_process_illegal_inst_irq(struct amdgpu_device *adev,
1223 struct amdgpu_irq_src *source,
1224 struct amdgpu_iv_entry *entry)
1225{
1226 DRM_ERROR("Illegal instruction in SDMA command stream\n");
1227 schedule_work(&adev->reset_work);
1228 return 0;
1229}
1230
yanyang15fc3aee2015-05-22 14:39:35 -04001231static int cik_sdma_set_clockgating_state(void *handle,
1232 enum amd_clockgating_state state)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001233{
1234 bool gate = false;
yanyang15fc3aee2015-05-22 14:39:35 -04001235 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001236
yanyang15fc3aee2015-05-22 14:39:35 -04001237 if (state == AMD_CG_STATE_GATE)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001238 gate = true;
1239
1240 cik_enable_sdma_mgcg(adev, gate);
1241 cik_enable_sdma_mgls(adev, gate);
1242
1243 return 0;
1244}
1245
yanyang15fc3aee2015-05-22 14:39:35 -04001246static int cik_sdma_set_powergating_state(void *handle,
1247 enum amd_powergating_state state)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001248{
1249 return 0;
1250}
1251
yanyang15fc3aee2015-05-22 14:39:35 -04001252const struct amd_ip_funcs cik_sdma_ip_funcs = {
Alex Deuchera2e73f52015-04-20 17:09:27 -04001253 .early_init = cik_sdma_early_init,
1254 .late_init = NULL,
1255 .sw_init = cik_sdma_sw_init,
1256 .sw_fini = cik_sdma_sw_fini,
1257 .hw_init = cik_sdma_hw_init,
1258 .hw_fini = cik_sdma_hw_fini,
1259 .suspend = cik_sdma_suspend,
1260 .resume = cik_sdma_resume,
1261 .is_idle = cik_sdma_is_idle,
1262 .wait_for_idle = cik_sdma_wait_for_idle,
1263 .soft_reset = cik_sdma_soft_reset,
1264 .print_status = cik_sdma_print_status,
1265 .set_clockgating_state = cik_sdma_set_clockgating_state,
1266 .set_powergating_state = cik_sdma_set_powergating_state,
1267};
1268
Alex Deuchera2e73f52015-04-20 17:09:27 -04001269static const struct amdgpu_ring_funcs cik_sdma_ring_funcs = {
1270 .get_rptr = cik_sdma_ring_get_rptr,
1271 .get_wptr = cik_sdma_ring_get_wptr,
1272 .set_wptr = cik_sdma_ring_set_wptr,
1273 .parse_cs = NULL,
1274 .emit_ib = cik_sdma_ring_emit_ib,
1275 .emit_fence = cik_sdma_ring_emit_fence,
Chunming Zhou2f4b9402016-01-15 11:05:21 +08001276 .emit_semaphore = NULL,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001277 .emit_vm_flush = cik_sdma_ring_emit_vm_flush,
Christian Königd2edb072015-05-11 14:10:34 +02001278 .emit_hdp_flush = cik_sdma_ring_emit_hdp_flush,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001279 .test_ring = cik_sdma_ring_test_ring,
1280 .test_ib = cik_sdma_ring_test_ib,
Jammy Zhouac01db32015-09-01 13:13:54 +08001281 .insert_nop = cik_sdma_ring_insert_nop,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001282};
1283
1284static void cik_sdma_set_ring_funcs(struct amdgpu_device *adev)
1285{
Alex Deucherc113ea12015-10-08 16:30:37 -04001286 int i;
1287
1288 for (i = 0; i < adev->sdma.num_instances; i++)
1289 adev->sdma.instance[i].ring.funcs = &cik_sdma_ring_funcs;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001290}
1291
1292static const struct amdgpu_irq_src_funcs cik_sdma_trap_irq_funcs = {
1293 .set = cik_sdma_set_trap_irq_state,
1294 .process = cik_sdma_process_trap_irq,
1295};
1296
1297static const struct amdgpu_irq_src_funcs cik_sdma_illegal_inst_irq_funcs = {
1298 .process = cik_sdma_process_illegal_inst_irq,
1299};
1300
1301static void cik_sdma_set_irq_funcs(struct amdgpu_device *adev)
1302{
Alex Deucherc113ea12015-10-08 16:30:37 -04001303 adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_LAST;
1304 adev->sdma.trap_irq.funcs = &cik_sdma_trap_irq_funcs;
1305 adev->sdma.illegal_inst_irq.funcs = &cik_sdma_illegal_inst_irq_funcs;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001306}
1307
1308/**
1309 * cik_sdma_emit_copy_buffer - copy buffer using the sDMA engine
1310 *
1311 * @ring: amdgpu_ring structure holding ring information
1312 * @src_offset: src GPU address
1313 * @dst_offset: dst GPU address
1314 * @byte_count: number of bytes to xfer
1315 *
1316 * Copy GPU buffers using the DMA engine (CIK).
1317 * Used by the amdgpu ttm implementation to move pages if
1318 * registered as the asic copy callback.
1319 */
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001320static void cik_sdma_emit_copy_buffer(struct amdgpu_ib *ib,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001321 uint64_t src_offset,
1322 uint64_t dst_offset,
1323 uint32_t byte_count)
1324{
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001325 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_COPY_SUB_OPCODE_LINEAR, 0);
1326 ib->ptr[ib->length_dw++] = byte_count;
1327 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1328 ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
1329 ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
1330 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1331 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001332}
1333
1334/**
1335 * cik_sdma_emit_fill_buffer - fill buffer using the sDMA engine
1336 *
1337 * @ring: amdgpu_ring structure holding ring information
1338 * @src_data: value to write to buffer
1339 * @dst_offset: dst GPU address
1340 * @byte_count: number of bytes to xfer
1341 *
1342 * Fill GPU buffers using the DMA engine (CIK).
1343 */
Chunming Zhou6e7a3842015-08-27 13:46:09 +08001344static void cik_sdma_emit_fill_buffer(struct amdgpu_ib *ib,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001345 uint32_t src_data,
1346 uint64_t dst_offset,
1347 uint32_t byte_count)
1348{
Chunming Zhou6e7a3842015-08-27 13:46:09 +08001349 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_CONSTANT_FILL, 0, 0);
1350 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1351 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1352 ib->ptr[ib->length_dw++] = src_data;
1353 ib->ptr[ib->length_dw++] = byte_count;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001354}
1355
1356static const struct amdgpu_buffer_funcs cik_sdma_buffer_funcs = {
1357 .copy_max_bytes = 0x1fffff,
1358 .copy_num_dw = 7,
1359 .emit_copy_buffer = cik_sdma_emit_copy_buffer,
1360
1361 .fill_max_bytes = 0x1fffff,
1362 .fill_num_dw = 5,
1363 .emit_fill_buffer = cik_sdma_emit_fill_buffer,
1364};
1365
1366static void cik_sdma_set_buffer_funcs(struct amdgpu_device *adev)
1367{
1368 if (adev->mman.buffer_funcs == NULL) {
1369 adev->mman.buffer_funcs = &cik_sdma_buffer_funcs;
Alex Deucherc113ea12015-10-08 16:30:37 -04001370 adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001371 }
1372}
1373
1374static const struct amdgpu_vm_pte_funcs cik_sdma_vm_pte_funcs = {
1375 .copy_pte = cik_sdma_vm_copy_pte,
1376 .write_pte = cik_sdma_vm_write_pte,
1377 .set_pte_pde = cik_sdma_vm_set_pte_pde,
1378 .pad_ib = cik_sdma_vm_pad_ib,
1379};
1380
1381static void cik_sdma_set_vm_pte_funcs(struct amdgpu_device *adev)
1382{
1383 if (adev->vm_manager.vm_pte_funcs == NULL) {
1384 adev->vm_manager.vm_pte_funcs = &cik_sdma_vm_pte_funcs;
Alex Deucherc113ea12015-10-08 16:30:37 -04001385 adev->vm_manager.vm_pte_funcs_ring = &adev->sdma.instance[0].ring;
Chunming Zhou4274f5d2015-07-21 16:04:39 +08001386 adev->vm_manager.vm_pte_funcs_ring->is_pte_ring = true;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001387 }
1388}