blob: 4a9cea03f54f94ee5416b275ad7b68103bb409f8 [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);
jimqu10ea9432016-08-30 08:59:42 +080055static int cik_sdma_soft_reset(void *handle);
Alex Deuchera2e73f52015-04-20 17:09:27 -040056
57MODULE_FIRMWARE("radeon/bonaire_sdma.bin");
58MODULE_FIRMWARE("radeon/bonaire_sdma1.bin");
59MODULE_FIRMWARE("radeon/hawaii_sdma.bin");
60MODULE_FIRMWARE("radeon/hawaii_sdma1.bin");
61MODULE_FIRMWARE("radeon/kaveri_sdma.bin");
62MODULE_FIRMWARE("radeon/kaveri_sdma1.bin");
63MODULE_FIRMWARE("radeon/kabini_sdma.bin");
64MODULE_FIRMWARE("radeon/kabini_sdma1.bin");
65MODULE_FIRMWARE("radeon/mullins_sdma.bin");
66MODULE_FIRMWARE("radeon/mullins_sdma1.bin");
67
68u32 amdgpu_cik_gpu_check_soft_reset(struct amdgpu_device *adev);
69
Monk Liud1ff53b2016-05-30 16:07:40 +080070
71static void cik_sdma_free_microcode(struct amdgpu_device *adev)
72{
73 int i;
74 for (i = 0; i < adev->sdma.num_instances; i++) {
75 release_firmware(adev->sdma.instance[i].fw);
76 adev->sdma.instance[i].fw = NULL;
77 }
78}
79
Alex Deuchera2e73f52015-04-20 17:09:27 -040080/*
81 * sDMA - System DMA
82 * Starting with CIK, the GPU has new asynchronous
83 * DMA engines. These engines are used for compute
84 * and gfx. There are two DMA engines (SDMA0, SDMA1)
85 * and each one supports 1 ring buffer used for gfx
86 * and 2 queues used for compute.
87 *
88 * The programming model is very similar to the CP
89 * (ring buffer, IBs, etc.), but sDMA has it's own
90 * packet format that is different from the PM4 format
91 * used by the CP. sDMA supports copying data, writing
92 * embedded data, solid fills, and a number of other
93 * things. It also has support for tiling/detiling of
94 * buffers.
95 */
96
97/**
98 * cik_sdma_init_microcode - load ucode images from disk
99 *
100 * @adev: amdgpu_device pointer
101 *
102 * Use the firmware interface to load the ucode images into
103 * the driver (not loaded into hw).
104 * Returns 0 on success, error on failure.
105 */
106static int cik_sdma_init_microcode(struct amdgpu_device *adev)
107{
108 const char *chip_name;
109 char fw_name[30];
Alex Deucherc113ea12015-10-08 16:30:37 -0400110 int err = 0, i;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400111
112 DRM_DEBUG("\n");
113
114 switch (adev->asic_type) {
115 case CHIP_BONAIRE:
116 chip_name = "bonaire";
117 break;
118 case CHIP_HAWAII:
119 chip_name = "hawaii";
120 break;
121 case CHIP_KAVERI:
122 chip_name = "kaveri";
123 break;
124 case CHIP_KABINI:
125 chip_name = "kabini";
126 break;
127 case CHIP_MULLINS:
128 chip_name = "mullins";
129 break;
130 default: BUG();
131 }
132
Alex Deucherc113ea12015-10-08 16:30:37 -0400133 for (i = 0; i < adev->sdma.num_instances; i++) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400134 if (i == 0)
135 snprintf(fw_name, sizeof(fw_name), "radeon/%s_sdma.bin", chip_name);
136 else
137 snprintf(fw_name, sizeof(fw_name), "radeon/%s_sdma1.bin", chip_name);
Alex Deucherc113ea12015-10-08 16:30:37 -0400138 err = request_firmware(&adev->sdma.instance[i].fw, fw_name, adev->dev);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400139 if (err)
140 goto out;
Alex Deucherc113ea12015-10-08 16:30:37 -0400141 err = amdgpu_ucode_validate(adev->sdma.instance[i].fw);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400142 }
143out:
144 if (err) {
Joe Perches7ca85292017-02-28 04:55:52 -0800145 pr_err("cik_sdma: Failed to load firmware \"%s\"\n", fw_name);
Alex Deucherc113ea12015-10-08 16:30:37 -0400146 for (i = 0; i < adev->sdma.num_instances; i++) {
147 release_firmware(adev->sdma.instance[i].fw);
148 adev->sdma.instance[i].fw = NULL;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400149 }
150 }
151 return err;
152}
153
154/**
155 * cik_sdma_ring_get_rptr - get the current read pointer
156 *
157 * @ring: amdgpu ring pointer
158 *
159 * Get the current rptr from the hardware (CIK+).
160 */
Ken Wang536fbf92016-03-12 09:32:30 +0800161static uint64_t cik_sdma_ring_get_rptr(struct amdgpu_ring *ring)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400162{
163 u32 rptr;
164
165 rptr = ring->adev->wb.wb[ring->rptr_offs];
166
167 return (rptr & 0x3fffc) >> 2;
168}
169
170/**
171 * cik_sdma_ring_get_wptr - get the current write pointer
172 *
173 * @ring: amdgpu ring pointer
174 *
175 * Get the current wptr from the hardware (CIK+).
176 */
Ken Wang536fbf92016-03-12 09:32:30 +0800177static uint64_t cik_sdma_ring_get_wptr(struct amdgpu_ring *ring)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400178{
179 struct amdgpu_device *adev = ring->adev;
Alex Deucherc113ea12015-10-08 16:30:37 -0400180 u32 me = (ring == &adev->sdma.instance[0].ring) ? 0 : 1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400181
182 return (RREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[me]) & 0x3fffc) >> 2;
183}
184
185/**
186 * cik_sdma_ring_set_wptr - commit the write pointer
187 *
188 * @ring: amdgpu ring pointer
189 *
190 * Write the wptr back to the hardware (CIK+).
191 */
192static void cik_sdma_ring_set_wptr(struct amdgpu_ring *ring)
193{
194 struct amdgpu_device *adev = ring->adev;
Alex Deucherc113ea12015-10-08 16:30:37 -0400195 u32 me = (ring == &adev->sdma.instance[0].ring) ? 0 : 1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400196
Ken Wang536fbf92016-03-12 09:32:30 +0800197 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[me],
198 (lower_32_bits(ring->wptr) << 2) & 0x3fffc);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400199}
200
Jammy Zhouac01db32015-09-01 13:13:54 +0800201static void cik_sdma_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
202{
Alex Deucherc113ea12015-10-08 16:30:37 -0400203 struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
Jammy Zhouac01db32015-09-01 13:13:54 +0800204 int i;
205
206 for (i = 0; i < count; i++)
207 if (sdma && sdma->burst_nop && (i == 0))
Christian König79887142016-10-05 16:09:32 +0200208 amdgpu_ring_write(ring, ring->funcs->nop |
Jammy Zhouac01db32015-09-01 13:13:54 +0800209 SDMA_NOP_COUNT(count - 1));
210 else
Christian König79887142016-10-05 16:09:32 +0200211 amdgpu_ring_write(ring, ring->funcs->nop);
Jammy Zhouac01db32015-09-01 13:13:54 +0800212}
213
Alex Deuchera2e73f52015-04-20 17:09:27 -0400214/**
215 * cik_sdma_ring_emit_ib - Schedule an IB on the DMA engine
216 *
217 * @ring: amdgpu ring pointer
218 * @ib: IB object to schedule
219 *
220 * Schedule an IB in the DMA ring (CIK).
221 */
222static void cik_sdma_ring_emit_ib(struct amdgpu_ring *ring,
Christian Königd88bf582016-05-06 17:50:03 +0200223 struct amdgpu_ib *ib,
224 unsigned vm_id, bool ctx_switch)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400225{
Christian Königd88bf582016-05-06 17:50:03 +0200226 u32 extra_bits = vm_id & 0xf;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400227
Alex Deuchera2e73f52015-04-20 17:09:27 -0400228 /* IB packet must end on a 8 DW boundary */
Ken Wang536fbf92016-03-12 09:32:30 +0800229 cik_sdma_ring_insert_nop(ring, (12 - (lower_32_bits(ring->wptr) & 7)) % 8);
Jammy Zhouac01db32015-09-01 13:13:54 +0800230
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
Chunming Zhou498dd972016-03-03 12:05:44 +0800264static void cik_sdma_ring_emit_hdp_invalidate(struct amdgpu_ring *ring)
265{
266 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
267 amdgpu_ring_write(ring, mmHDP_DEBUG0);
268 amdgpu_ring_write(ring, 1);
269}
270
Alex Deuchera2e73f52015-04-20 17:09:27 -0400271/**
272 * cik_sdma_ring_emit_fence - emit a fence on the DMA ring
273 *
274 * @ring: amdgpu ring pointer
275 * @fence: amdgpu fence object
276 *
277 * Add a DMA fence packet to the ring to write
278 * the fence seq number and DMA trap packet to generate
279 * an interrupt if needed (CIK).
280 */
281static void cik_sdma_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
Chunming Zhou890ee232015-06-01 14:35:03 +0800282 unsigned flags)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400283{
Chunming Zhou890ee232015-06-01 14:35:03 +0800284 bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400285 /* write the fence */
286 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
287 amdgpu_ring_write(ring, lower_32_bits(addr));
288 amdgpu_ring_write(ring, upper_32_bits(addr));
289 amdgpu_ring_write(ring, lower_32_bits(seq));
290
291 /* optionally write high bits as well */
292 if (write64bit) {
293 addr += 4;
294 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_FENCE, 0, 0));
295 amdgpu_ring_write(ring, lower_32_bits(addr));
296 amdgpu_ring_write(ring, upper_32_bits(addr));
297 amdgpu_ring_write(ring, upper_32_bits(seq));
298 }
299
300 /* generate an interrupt */
301 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_TRAP, 0, 0));
302}
303
304/**
Alex Deuchera2e73f52015-04-20 17:09:27 -0400305 * cik_sdma_gfx_stop - stop the gfx async dma engines
306 *
307 * @adev: amdgpu_device pointer
308 *
309 * Stop the gfx async dma ring buffers (CIK).
310 */
311static void cik_sdma_gfx_stop(struct amdgpu_device *adev)
312{
Alex Deucherc113ea12015-10-08 16:30:37 -0400313 struct amdgpu_ring *sdma0 = &adev->sdma.instance[0].ring;
314 struct amdgpu_ring *sdma1 = &adev->sdma.instance[1].ring;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400315 u32 rb_cntl;
316 int i;
317
318 if ((adev->mman.buffer_funcs_ring == sdma0) ||
319 (adev->mman.buffer_funcs_ring == sdma1))
320 amdgpu_ttm_set_active_vram_size(adev, adev->mc.visible_vram_size);
321
Alex Deucherc113ea12015-10-08 16:30:37 -0400322 for (i = 0; i < adev->sdma.num_instances; i++) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400323 rb_cntl = RREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i]);
324 rb_cntl &= ~SDMA0_GFX_RB_CNTL__RB_ENABLE_MASK;
325 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
326 WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], 0);
327 }
328 sdma0->ready = false;
329 sdma1->ready = false;
330}
331
332/**
333 * cik_sdma_rlc_stop - stop the compute async dma engines
334 *
335 * @adev: amdgpu_device pointer
336 *
337 * Stop the compute async dma queues (CIK).
338 */
339static void cik_sdma_rlc_stop(struct amdgpu_device *adev)
340{
341 /* XXX todo */
342}
343
344/**
Felix Kuehling763dbbf2016-06-15 16:33:15 -0400345 * cik_ctx_switch_enable - stop the async dma engines context switch
346 *
347 * @adev: amdgpu_device pointer
348 * @enable: enable/disable the DMA MEs context switch.
349 *
350 * Halt or unhalt the async dma engines context switch (VI).
351 */
352static void cik_ctx_switch_enable(struct amdgpu_device *adev, bool enable)
353{
354 u32 f32_cntl;
355 int i;
356
357 for (i = 0; i < adev->sdma.num_instances; i++) {
358 f32_cntl = RREG32(mmSDMA0_CNTL + sdma_offsets[i]);
359 if (enable) {
360 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
361 AUTO_CTXSW_ENABLE, 1);
362 } else {
363 f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
364 AUTO_CTXSW_ENABLE, 0);
365 }
366
367 WREG32(mmSDMA0_CNTL + sdma_offsets[i], f32_cntl);
368 }
369}
370
371/**
Alex Deuchera2e73f52015-04-20 17:09:27 -0400372 * cik_sdma_enable - stop the async dma engines
373 *
374 * @adev: amdgpu_device pointer
375 * @enable: enable/disable the DMA MEs.
376 *
377 * Halt or unhalt the async dma engines (CIK).
378 */
379static void cik_sdma_enable(struct amdgpu_device *adev, bool enable)
380{
381 u32 me_cntl;
382 int i;
383
Edward O'Callaghan004e29c2016-07-12 10:17:53 +1000384 if (!enable) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400385 cik_sdma_gfx_stop(adev);
386 cik_sdma_rlc_stop(adev);
387 }
388
Alex Deucherc113ea12015-10-08 16:30:37 -0400389 for (i = 0; i < adev->sdma.num_instances; i++) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400390 me_cntl = RREG32(mmSDMA0_F32_CNTL + sdma_offsets[i]);
391 if (enable)
392 me_cntl &= ~SDMA0_F32_CNTL__HALT_MASK;
393 else
394 me_cntl |= SDMA0_F32_CNTL__HALT_MASK;
395 WREG32(mmSDMA0_F32_CNTL + sdma_offsets[i], me_cntl);
396 }
397}
398
399/**
400 * cik_sdma_gfx_resume - setup and start the async dma engines
401 *
402 * @adev: amdgpu_device pointer
403 *
404 * Set up the gfx DMA ring buffers and enable them (CIK).
405 * Returns 0 for success, error for failure.
406 */
407static int cik_sdma_gfx_resume(struct amdgpu_device *adev)
408{
409 struct amdgpu_ring *ring;
410 u32 rb_cntl, ib_cntl;
411 u32 rb_bufsz;
412 u32 wb_offset;
413 int i, j, r;
414
Alex Deucherc113ea12015-10-08 16:30:37 -0400415 for (i = 0; i < adev->sdma.num_instances; i++) {
416 ring = &adev->sdma.instance[i].ring;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400417 wb_offset = (ring->rptr_offs * 4);
418
419 mutex_lock(&adev->srbm_mutex);
420 for (j = 0; j < 16; j++) {
421 cik_srbm_select(adev, 0, 0, 0, j);
422 /* SDMA GFX */
423 WREG32(mmSDMA0_GFX_VIRTUAL_ADDR + sdma_offsets[i], 0);
424 WREG32(mmSDMA0_GFX_APE1_CNTL + sdma_offsets[i], 0);
425 /* XXX SDMA RLC - todo */
426 }
427 cik_srbm_select(adev, 0, 0, 0, 0);
428 mutex_unlock(&adev->srbm_mutex);
429
Alex Deucher2b3a7652016-02-12 03:05:24 -0500430 WREG32(mmSDMA0_TILING_CONFIG + sdma_offsets[i],
431 adev->gfx.config.gb_addr_config & 0x70);
432
Alex Deuchera2e73f52015-04-20 17:09:27 -0400433 WREG32(mmSDMA0_SEM_INCOMPLETE_TIMER_CNTL + sdma_offsets[i], 0);
434 WREG32(mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL + sdma_offsets[i], 0);
435
436 /* Set ring buffer size in dwords */
437 rb_bufsz = order_base_2(ring->ring_size / 4);
438 rb_cntl = rb_bufsz << 1;
439#ifdef __BIG_ENDIAN
Alex Deucher454fc952015-06-09 09:58:23 -0400440 rb_cntl |= SDMA0_GFX_RB_CNTL__RB_SWAP_ENABLE_MASK |
441 SDMA0_GFX_RB_CNTL__RPTR_WRITEBACK_SWAP_ENABLE_MASK;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400442#endif
443 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i], rb_cntl);
444
445 /* Initialize the ring buffer's read and write pointers */
446 WREG32(mmSDMA0_GFX_RB_RPTR + sdma_offsets[i], 0);
447 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], 0);
Monk Liud72f7c02016-05-25 16:55:50 +0800448 WREG32(mmSDMA0_GFX_IB_RPTR + sdma_offsets[i], 0);
449 WREG32(mmSDMA0_GFX_IB_OFFSET + sdma_offsets[i], 0);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400450
451 /* set the wb address whether it's enabled or not */
452 WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_HI + sdma_offsets[i],
453 upper_32_bits(adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFF);
454 WREG32(mmSDMA0_GFX_RB_RPTR_ADDR_LO + sdma_offsets[i],
455 ((adev->wb.gpu_addr + wb_offset) & 0xFFFFFFFC));
456
457 rb_cntl |= SDMA0_GFX_RB_CNTL__RPTR_WRITEBACK_ENABLE_MASK;
458
459 WREG32(mmSDMA0_GFX_RB_BASE + sdma_offsets[i], ring->gpu_addr >> 8);
460 WREG32(mmSDMA0_GFX_RB_BASE_HI + sdma_offsets[i], ring->gpu_addr >> 40);
461
462 ring->wptr = 0;
Ken Wang536fbf92016-03-12 09:32:30 +0800463 WREG32(mmSDMA0_GFX_RB_WPTR + sdma_offsets[i], lower_32_bits(ring->wptr) << 2);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400464
465 /* enable DMA RB */
466 WREG32(mmSDMA0_GFX_RB_CNTL + sdma_offsets[i],
467 rb_cntl | SDMA0_GFX_RB_CNTL__RB_ENABLE_MASK);
468
469 ib_cntl = SDMA0_GFX_IB_CNTL__IB_ENABLE_MASK;
470#ifdef __BIG_ENDIAN
471 ib_cntl |= SDMA0_GFX_IB_CNTL__IB_SWAP_ENABLE_MASK;
472#endif
473 /* enable DMA IBs */
474 WREG32(mmSDMA0_GFX_IB_CNTL + sdma_offsets[i], ib_cntl);
475
476 ring->ready = true;
Monk Liu505dfe72016-05-25 16:57:14 +0800477 }
Alex Deuchera2e73f52015-04-20 17:09:27 -0400478
Monk Liu505dfe72016-05-25 16:57:14 +0800479 cik_sdma_enable(adev, true);
480
481 for (i = 0; i < adev->sdma.num_instances; i++) {
482 ring = &adev->sdma.instance[i].ring;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400483 r = amdgpu_ring_test_ring(ring);
484 if (r) {
485 ring->ready = false;
486 return r;
487 }
488
489 if (adev->mman.buffer_funcs_ring == ring)
490 amdgpu_ttm_set_active_vram_size(adev, adev->mc.real_vram_size);
491 }
492
493 return 0;
494}
495
496/**
497 * cik_sdma_rlc_resume - setup and start the async dma engines
498 *
499 * @adev: amdgpu_device pointer
500 *
501 * Set up the compute DMA queues and enable them (CIK).
502 * Returns 0 for success, error for failure.
503 */
504static int cik_sdma_rlc_resume(struct amdgpu_device *adev)
505{
506 /* XXX todo */
507 return 0;
508}
509
510/**
511 * cik_sdma_load_microcode - load the sDMA ME ucode
512 *
513 * @adev: amdgpu_device pointer
514 *
515 * Loads the sDMA0/1 ucode.
516 * Returns 0 for success, -EINVAL if the ucode is not available.
517 */
518static int cik_sdma_load_microcode(struct amdgpu_device *adev)
519{
520 const struct sdma_firmware_header_v1_0 *hdr;
521 const __le32 *fw_data;
522 u32 fw_size;
523 int i, j;
524
Alex Deuchera2e73f52015-04-20 17:09:27 -0400525 /* halt the MEs */
526 cik_sdma_enable(adev, false);
527
Alex Deucherc113ea12015-10-08 16:30:37 -0400528 for (i = 0; i < adev->sdma.num_instances; i++) {
529 if (!adev->sdma.instance[i].fw)
530 return -EINVAL;
531 hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400532 amdgpu_ucode_print_sdma_hdr(&hdr->header);
533 fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
Alex Deucherc113ea12015-10-08 16:30:37 -0400534 adev->sdma.instance[i].fw_version = le32_to_cpu(hdr->header.ucode_version);
535 adev->sdma.instance[i].feature_version = le32_to_cpu(hdr->ucode_feature_version);
536 if (adev->sdma.instance[i].feature_version >= 20)
537 adev->sdma.instance[i].burst_nop = true;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400538 fw_data = (const __le32 *)
Alex Deucherc113ea12015-10-08 16:30:37 -0400539 (adev->sdma.instance[i].fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
Alex Deuchera2e73f52015-04-20 17:09:27 -0400540 WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], 0);
541 for (j = 0; j < fw_size; j++)
542 WREG32(mmSDMA0_UCODE_DATA + sdma_offsets[i], le32_to_cpup(fw_data++));
Alex Deucherc113ea12015-10-08 16:30:37 -0400543 WREG32(mmSDMA0_UCODE_ADDR + sdma_offsets[i], adev->sdma.instance[i].fw_version);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400544 }
545
546 return 0;
547}
548
549/**
550 * cik_sdma_start - setup and start the async dma engines
551 *
552 * @adev: amdgpu_device pointer
553 *
554 * Set up the DMA engines and enable them (CIK).
555 * Returns 0 for success, error for failure.
556 */
557static int cik_sdma_start(struct amdgpu_device *adev)
558{
559 int r;
560
561 r = cik_sdma_load_microcode(adev);
562 if (r)
563 return r;
564
Monk Liu505dfe72016-05-25 16:57:14 +0800565 /* halt the engine before programing */
566 cik_sdma_enable(adev, false);
Felix Kuehling763dbbf2016-06-15 16:33:15 -0400567 /* enable sdma ring preemption */
568 cik_ctx_switch_enable(adev, true);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400569
570 /* start the gfx rings and rlc compute queues */
571 r = cik_sdma_gfx_resume(adev);
572 if (r)
573 return r;
574 r = cik_sdma_rlc_resume(adev);
575 if (r)
576 return r;
577
578 return 0;
579}
580
581/**
582 * cik_sdma_ring_test_ring - simple async dma engine test
583 *
584 * @ring: amdgpu_ring structure holding ring information
585 *
586 * Test the DMA engine by writing using it to write an
587 * value to memory. (CIK).
588 * Returns 0 for success, error for failure.
589 */
590static int cik_sdma_ring_test_ring(struct amdgpu_ring *ring)
591{
592 struct amdgpu_device *adev = ring->adev;
593 unsigned i;
594 unsigned index;
595 int r;
596 u32 tmp;
597 u64 gpu_addr;
598
599 r = amdgpu_wb_get(adev, &index);
600 if (r) {
601 dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
602 return r;
603 }
604
605 gpu_addr = adev->wb.gpu_addr + (index * 4);
606 tmp = 0xCAFEDEAD;
607 adev->wb.wb[index] = cpu_to_le32(tmp);
608
Christian Königa27de352016-01-21 11:28:53 +0100609 r = amdgpu_ring_alloc(ring, 5);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400610 if (r) {
611 DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
612 amdgpu_wb_free(adev, index);
613 return r;
614 }
615 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_WRITE, SDMA_WRITE_SUB_OPCODE_LINEAR, 0));
616 amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
617 amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
618 amdgpu_ring_write(ring, 1); /* number of DWs to follow */
619 amdgpu_ring_write(ring, 0xDEADBEEF);
Christian Königa27de352016-01-21 11:28:53 +0100620 amdgpu_ring_commit(ring);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400621
622 for (i = 0; i < adev->usec_timeout; i++) {
623 tmp = le32_to_cpu(adev->wb.wb[index]);
624 if (tmp == 0xDEADBEEF)
625 break;
626 DRM_UDELAY(1);
627 }
628
629 if (i < adev->usec_timeout) {
630 DRM_INFO("ring test on %d succeeded in %d usecs\n", ring->idx, i);
631 } else {
632 DRM_ERROR("amdgpu: ring %d test failed (0x%08X)\n",
633 ring->idx, tmp);
634 r = -EINVAL;
635 }
636 amdgpu_wb_free(adev, index);
637
638 return r;
639}
640
641/**
642 * cik_sdma_ring_test_ib - test an IB on the DMA engine
643 *
644 * @ring: amdgpu_ring structure holding ring information
645 *
646 * Test a simple IB in the DMA ring (CIK).
647 * Returns 0 on success, error on failure.
648 */
Christian Königbbec97a2016-07-05 21:07:17 +0200649static int cik_sdma_ring_test_ib(struct amdgpu_ring *ring, long timeout)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400650{
651 struct amdgpu_device *adev = ring->adev;
652 struct amdgpu_ib ib;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100653 struct dma_fence *f = NULL;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400654 unsigned index;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400655 u32 tmp = 0;
656 u64 gpu_addr;
Christian Königbbec97a2016-07-05 21:07:17 +0200657 long r;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400658
659 r = amdgpu_wb_get(adev, &index);
660 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200661 dev_err(adev->dev, "(%ld) failed to allocate wb slot\n", r);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400662 return r;
663 }
664
665 gpu_addr = adev->wb.gpu_addr + (index * 4);
666 tmp = 0xCAFEDEAD;
667 adev->wb.wb[index] = cpu_to_le32(tmp);
Christian Königb203dd92015-08-18 18:23:16 +0200668 memset(&ib, 0, sizeof(ib));
Christian Königb07c60c2016-01-31 12:29:04 +0100669 r = amdgpu_ib_get(adev, NULL, 256, &ib);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400670 if (r) {
Christian Königbbec97a2016-07-05 21:07:17 +0200671 DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800672 goto err0;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400673 }
674
Christian König6d445652016-07-05 15:53:07 +0200675 ib.ptr[0] = SDMA_PACKET(SDMA_OPCODE_WRITE,
676 SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400677 ib.ptr[1] = lower_32_bits(gpu_addr);
678 ib.ptr[2] = upper_32_bits(gpu_addr);
679 ib.ptr[3] = 1;
680 ib.ptr[4] = 0xDEADBEEF;
681 ib.length_dw = 5;
Junwei Zhang50ddc752017-01-23 16:30:38 +0800682 r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800683 if (r)
684 goto err1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400685
Chris Wilsonf54d1862016-10-25 13:00:45 +0100686 r = dma_fence_wait_timeout(f, false, timeout);
Christian Königbbec97a2016-07-05 21:07:17 +0200687 if (r == 0) {
688 DRM_ERROR("amdgpu: IB test timed out\n");
689 r = -ETIMEDOUT;
690 goto err1;
691 } else if (r < 0) {
692 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800693 goto err1;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400694 }
Christian König6d445652016-07-05 15:53:07 +0200695 tmp = le32_to_cpu(adev->wb.wb[index]);
696 if (tmp == 0xDEADBEEF) {
697 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
Christian Königbbec97a2016-07-05 21:07:17 +0200698 r = 0;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400699 } else {
700 DRM_ERROR("amdgpu: ib test failed (0x%08X)\n", tmp);
701 r = -EINVAL;
702 }
Chunming Zhou0011fda2015-06-01 15:33:20 +0800703
704err1:
Monk Liucc55c452016-03-17 10:47:07 +0800705 amdgpu_ib_free(adev, &ib, NULL);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100706 dma_fence_put(f);
Chunming Zhou0011fda2015-06-01 15:33:20 +0800707err0:
Alex Deuchera2e73f52015-04-20 17:09:27 -0400708 amdgpu_wb_free(adev, index);
709 return r;
710}
711
712/**
713 * cik_sdma_vm_copy_pages - update PTEs by copying them from the GART
714 *
715 * @ib: indirect buffer to fill with commands
716 * @pe: addr of the page entry
717 * @src: src addr to copy from
718 * @count: number of page entries to update
719 *
720 * Update PTEs by copying them from the GART using sDMA (CIK).
721 */
722static void cik_sdma_vm_copy_pte(struct amdgpu_ib *ib,
723 uint64_t pe, uint64_t src,
724 unsigned count)
725{
Christian König96105e52016-08-12 12:59:59 +0200726 unsigned bytes = count * 8;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400727
Christian König96105e52016-08-12 12:59:59 +0200728 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY,
729 SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
730 ib->ptr[ib->length_dw++] = bytes;
731 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
732 ib->ptr[ib->length_dw++] = lower_32_bits(src);
733 ib->ptr[ib->length_dw++] = upper_32_bits(src);
734 ib->ptr[ib->length_dw++] = lower_32_bits(pe);
735 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400736}
737
738/**
739 * cik_sdma_vm_write_pages - update PTEs by writing them manually
740 *
741 * @ib: indirect buffer to fill with commands
742 * @pe: addr of the page entry
Christian Königde9ea7b2016-08-12 11:33:30 +0200743 * @value: dst addr to write into pe
Alex Deuchera2e73f52015-04-20 17:09:27 -0400744 * @count: number of page entries to update
745 * @incr: increase next addr by incr bytes
Alex Deuchera2e73f52015-04-20 17:09:27 -0400746 *
747 * Update PTEs by writing them manually using sDMA (CIK).
748 */
Christian Königde9ea7b2016-08-12 11:33:30 +0200749static void cik_sdma_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
750 uint64_t value, unsigned count,
751 uint32_t incr)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400752{
Christian Königde9ea7b2016-08-12 11:33:30 +0200753 unsigned ndw = count * 2;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400754
Christian Königde9ea7b2016-08-12 11:33:30 +0200755 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_WRITE,
756 SDMA_WRITE_SUB_OPCODE_LINEAR, 0);
757 ib->ptr[ib->length_dw++] = lower_32_bits(pe);
758 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
759 ib->ptr[ib->length_dw++] = ndw;
760 for (; ndw > 0; ndw -= 2) {
761 ib->ptr[ib->length_dw++] = lower_32_bits(value);
762 ib->ptr[ib->length_dw++] = upper_32_bits(value);
763 value += incr;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400764 }
765}
766
767/**
768 * cik_sdma_vm_set_pages - update the page tables using sDMA
769 *
770 * @ib: indirect buffer to fill with commands
771 * @pe: addr of the page entry
772 * @addr: dst addr to write into pe
773 * @count: number of page entries to update
774 * @incr: increase next addr by incr bytes
775 * @flags: access flags
776 *
777 * Update the page tables using sDMA (CIK).
778 */
Christian König96105e52016-08-12 12:59:59 +0200779static void cik_sdma_vm_set_pte_pde(struct amdgpu_ib *ib, uint64_t pe,
Alex Deuchera2e73f52015-04-20 17:09:27 -0400780 uint64_t addr, unsigned count,
Chunming Zhou6b777602016-09-21 16:19:19 +0800781 uint32_t incr, uint64_t flags)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400782{
Christian König96105e52016-08-12 12:59:59 +0200783 /* for physically contiguous pages (vram) */
784 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_GENERATE_PTE_PDE, 0, 0);
785 ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
786 ib->ptr[ib->length_dw++] = upper_32_bits(pe);
Junwei Zhangb9be7002017-03-28 16:52:07 +0800787 ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
788 ib->ptr[ib->length_dw++] = upper_32_bits(flags);
Christian König96105e52016-08-12 12:59:59 +0200789 ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
790 ib->ptr[ib->length_dw++] = upper_32_bits(addr);
791 ib->ptr[ib->length_dw++] = incr; /* increment size */
792 ib->ptr[ib->length_dw++] = 0;
793 ib->ptr[ib->length_dw++] = count; /* number of entries */
Alex Deuchera2e73f52015-04-20 17:09:27 -0400794}
795
796/**
797 * cik_sdma_vm_pad_ib - pad the IB to the required number of dw
798 *
799 * @ib: indirect buffer to fill with padding
800 *
801 */
Christian König9e5d53092016-01-31 12:20:55 +0100802static void cik_sdma_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400803{
Christian König9e5d53092016-01-31 12:20:55 +0100804 struct amdgpu_sdma_instance *sdma = amdgpu_get_sdma_instance(ring);
Jammy Zhouac01db32015-09-01 13:13:54 +0800805 u32 pad_count;
806 int i;
807
808 pad_count = (8 - (ib->length_dw & 0x7)) % 8;
809 for (i = 0; i < pad_count; i++)
810 if (sdma && sdma->burst_nop && (i == 0))
811 ib->ptr[ib->length_dw++] =
812 SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0) |
813 SDMA_NOP_COUNT(pad_count - 1);
814 else
815 ib->ptr[ib->length_dw++] =
816 SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400817}
818
819/**
Christian König00b7c4f2016-03-08 14:11:00 +0100820 * cik_sdma_ring_emit_pipeline_sync - sync the pipeline
Alex Deuchera2e73f52015-04-20 17:09:27 -0400821 *
822 * @ring: amdgpu_ring pointer
Alex Deuchera2e73f52015-04-20 17:09:27 -0400823 *
Christian König00b7c4f2016-03-08 14:11:00 +0100824 * Make sure all previous operations are completed (CIK).
Alex Deuchera2e73f52015-04-20 17:09:27 -0400825 */
Christian König00b7c4f2016-03-08 14:11:00 +0100826static void cik_sdma_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400827{
Chunming Zhou5c55db82016-03-02 11:30:31 +0800828 uint32_t seq = ring->fence_drv.sync_seq;
829 uint64_t addr = ring->fence_drv.gpu_addr;
830
831 /* wait for idle */
832 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0,
833 SDMA_POLL_REG_MEM_EXTRA_OP(0) |
834 SDMA_POLL_REG_MEM_EXTRA_FUNC(3) | /* equal */
835 SDMA_POLL_REG_MEM_EXTRA_M));
836 amdgpu_ring_write(ring, addr & 0xfffffffc);
837 amdgpu_ring_write(ring, upper_32_bits(addr) & 0xffffffff);
838 amdgpu_ring_write(ring, seq); /* reference */
839 amdgpu_ring_write(ring, 0xfffffff); /* mask */
840 amdgpu_ring_write(ring, (0xfff << 16) | 4); /* retry count, poll interval */
Christian König00b7c4f2016-03-08 14:11:00 +0100841}
Chunming Zhou5c55db82016-03-02 11:30:31 +0800842
Christian König00b7c4f2016-03-08 14:11:00 +0100843/**
Alex Deuchera2e73f52015-04-20 17:09:27 -0400844 * cik_sdma_ring_emit_vm_flush - cik vm flush using sDMA
845 *
846 * @ring: amdgpu_ring pointer
847 * @vm: amdgpu_vm pointer
848 *
849 * Update the page table base and flush the VM TLB
850 * using sDMA (CIK).
851 */
852static void cik_sdma_ring_emit_vm_flush(struct amdgpu_ring *ring,
853 unsigned vm_id, uint64_t pd_addr)
854{
855 u32 extra_bits = (SDMA_POLL_REG_MEM_EXTRA_OP(0) |
856 SDMA_POLL_REG_MEM_EXTRA_FUNC(0)); /* always */
857
858 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
859 if (vm_id < 8) {
860 amdgpu_ring_write(ring, (mmVM_CONTEXT0_PAGE_TABLE_BASE_ADDR + vm_id));
861 } else {
862 amdgpu_ring_write(ring, (mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vm_id - 8));
863 }
864 amdgpu_ring_write(ring, pd_addr >> 12);
865
Alex Deuchera2e73f52015-04-20 17:09:27 -0400866 /* flush TLB */
867 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_SRBM_WRITE, 0, 0xf000));
868 amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST);
869 amdgpu_ring_write(ring, 1 << vm_id);
870
871 amdgpu_ring_write(ring, SDMA_PACKET(SDMA_OPCODE_POLL_REG_MEM, 0, extra_bits));
872 amdgpu_ring_write(ring, mmVM_INVALIDATE_REQUEST << 2);
873 amdgpu_ring_write(ring, 0);
874 amdgpu_ring_write(ring, 0); /* reference */
875 amdgpu_ring_write(ring, 0); /* mask */
876 amdgpu_ring_write(ring, (0xfff << 16) | 10); /* retry count, poll interval */
877}
878
879static void cik_enable_sdma_mgcg(struct amdgpu_device *adev,
880 bool enable)
881{
882 u32 orig, data;
883
Alex Deuchere3b04bc2016-02-05 10:56:22 -0500884 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_MGCG)) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400885 WREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET, 0x00000100);
886 WREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET, 0x00000100);
887 } else {
888 orig = data = RREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET);
889 data |= 0xff000000;
890 if (data != orig)
891 WREG32(mmSDMA0_CLK_CTRL + SDMA0_REGISTER_OFFSET, data);
892
893 orig = data = RREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET);
894 data |= 0xff000000;
895 if (data != orig)
896 WREG32(mmSDMA0_CLK_CTRL + SDMA1_REGISTER_OFFSET, data);
897 }
898}
899
900static void cik_enable_sdma_mgls(struct amdgpu_device *adev,
901 bool enable)
902{
903 u32 orig, data;
904
Alex Deuchere3b04bc2016-02-05 10:56:22 -0500905 if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_LS)) {
Alex Deuchera2e73f52015-04-20 17:09:27 -0400906 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET);
907 data |= 0x100;
908 if (orig != data)
909 WREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET, data);
910
911 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET);
912 data |= 0x100;
913 if (orig != data)
914 WREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET, data);
915 } else {
916 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET);
917 data &= ~0x100;
918 if (orig != data)
919 WREG32(mmSDMA0_POWER_CNTL + SDMA0_REGISTER_OFFSET, data);
920
921 orig = data = RREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET);
922 data &= ~0x100;
923 if (orig != data)
924 WREG32(mmSDMA0_POWER_CNTL + SDMA1_REGISTER_OFFSET, data);
925 }
926}
927
yanyang15fc3aee2015-05-22 14:39:35 -0400928static int cik_sdma_early_init(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400929{
yanyang15fc3aee2015-05-22 14:39:35 -0400930 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
931
Alex Deucherc113ea12015-10-08 16:30:37 -0400932 adev->sdma.num_instances = SDMA_MAX_INSTANCE;
933
Alex Deuchera2e73f52015-04-20 17:09:27 -0400934 cik_sdma_set_ring_funcs(adev);
935 cik_sdma_set_irq_funcs(adev);
936 cik_sdma_set_buffer_funcs(adev);
937 cik_sdma_set_vm_pte_funcs(adev);
938
939 return 0;
940}
941
yanyang15fc3aee2015-05-22 14:39:35 -0400942static int cik_sdma_sw_init(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400943{
944 struct amdgpu_ring *ring;
yanyang15fc3aee2015-05-22 14:39:35 -0400945 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucherc113ea12015-10-08 16:30:37 -0400946 int r, i;
Alex Deuchera2e73f52015-04-20 17:09:27 -0400947
948 r = cik_sdma_init_microcode(adev);
949 if (r) {
950 DRM_ERROR("Failed to load sdma firmware!\n");
951 return r;
952 }
953
954 /* SDMA trap event */
Alex Deucherd766e6a2016-03-29 18:28:50 -0400955 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 224,
956 &adev->sdma.trap_irq);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400957 if (r)
958 return r;
959
960 /* SDMA Privileged inst */
Alex Deucherd766e6a2016-03-29 18:28:50 -0400961 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 241,
962 &adev->sdma.illegal_inst_irq);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400963 if (r)
964 return r;
965
966 /* SDMA Privileged inst */
Alex Deucherd766e6a2016-03-29 18:28:50 -0400967 r = amdgpu_irq_add_id(adev, AMDGPU_IH_CLIENTID_LEGACY, 247,
968 &adev->sdma.illegal_inst_irq);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400969 if (r)
970 return r;
971
Alex Deucherc113ea12015-10-08 16:30:37 -0400972 for (i = 0; i < adev->sdma.num_instances; i++) {
973 ring = &adev->sdma.instance[i].ring;
974 ring->ring_obj = NULL;
975 sprintf(ring->name, "sdma%d", i);
Christian Königb38d99c2016-04-13 10:30:13 +0200976 r = amdgpu_ring_init(adev, ring, 1024,
Alex Deucherc113ea12015-10-08 16:30:37 -0400977 &adev->sdma.trap_irq,
978 (i == 0) ?
Christian König21cd9422016-10-05 15:36:39 +0200979 AMDGPU_SDMA_IRQ_TRAP0 :
980 AMDGPU_SDMA_IRQ_TRAP1);
Alex Deucherc113ea12015-10-08 16:30:37 -0400981 if (r)
982 return r;
983 }
Alex Deuchera2e73f52015-04-20 17:09:27 -0400984
985 return r;
986}
987
yanyang15fc3aee2015-05-22 14:39:35 -0400988static int cik_sdma_sw_fini(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -0400989{
yanyang15fc3aee2015-05-22 14:39:35 -0400990 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deucherc113ea12015-10-08 16:30:37 -0400991 int i;
yanyang15fc3aee2015-05-22 14:39:35 -0400992
Alex Deucherc113ea12015-10-08 16:30:37 -0400993 for (i = 0; i < adev->sdma.num_instances; i++)
994 amdgpu_ring_fini(&adev->sdma.instance[i].ring);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400995
Monk Liud1ff53b2016-05-30 16:07:40 +0800996 cik_sdma_free_microcode(adev);
Alex Deuchera2e73f52015-04-20 17:09:27 -0400997 return 0;
998}
999
yanyang15fc3aee2015-05-22 14:39:35 -04001000static int cik_sdma_hw_init(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001001{
1002 int r;
yanyang15fc3aee2015-05-22 14:39:35 -04001003 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001004
1005 r = cik_sdma_start(adev);
1006 if (r)
1007 return r;
1008
1009 return r;
1010}
1011
yanyang15fc3aee2015-05-22 14:39:35 -04001012static int cik_sdma_hw_fini(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;
1015
Felix Kuehling763dbbf2016-06-15 16:33:15 -04001016 cik_ctx_switch_enable(adev, false);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001017 cik_sdma_enable(adev, false);
1018
1019 return 0;
1020}
1021
yanyang15fc3aee2015-05-22 14:39:35 -04001022static int cik_sdma_suspend(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001023{
yanyang15fc3aee2015-05-22 14:39:35 -04001024 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001025
1026 return cik_sdma_hw_fini(adev);
1027}
1028
yanyang15fc3aee2015-05-22 14:39:35 -04001029static int cik_sdma_resume(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001030{
yanyang15fc3aee2015-05-22 14:39:35 -04001031 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001032
jimqu10ea9432016-08-30 08:59:42 +08001033 cik_sdma_soft_reset(handle);
1034
Alex Deuchera2e73f52015-04-20 17:09:27 -04001035 return cik_sdma_hw_init(adev);
1036}
1037
yanyang15fc3aee2015-05-22 14:39:35 -04001038static bool cik_sdma_is_idle(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001039{
yanyang15fc3aee2015-05-22 14:39:35 -04001040 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001041 u32 tmp = RREG32(mmSRBM_STATUS2);
1042
1043 if (tmp & (SRBM_STATUS2__SDMA_BUSY_MASK |
1044 SRBM_STATUS2__SDMA1_BUSY_MASK))
1045 return false;
1046
1047 return true;
1048}
1049
yanyang15fc3aee2015-05-22 14:39:35 -04001050static int cik_sdma_wait_for_idle(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001051{
1052 unsigned i;
1053 u32 tmp;
yanyang15fc3aee2015-05-22 14:39:35 -04001054 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001055
1056 for (i = 0; i < adev->usec_timeout; i++) {
1057 tmp = RREG32(mmSRBM_STATUS2) & (SRBM_STATUS2__SDMA_BUSY_MASK |
1058 SRBM_STATUS2__SDMA1_BUSY_MASK);
1059
1060 if (!tmp)
1061 return 0;
1062 udelay(1);
1063 }
1064 return -ETIMEDOUT;
1065}
1066
yanyang15fc3aee2015-05-22 14:39:35 -04001067static int cik_sdma_soft_reset(void *handle)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001068{
1069 u32 srbm_soft_reset = 0;
yanyang15fc3aee2015-05-22 14:39:35 -04001070 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001071 u32 tmp = RREG32(mmSRBM_STATUS2);
1072
1073 if (tmp & SRBM_STATUS2__SDMA_BUSY_MASK) {
1074 /* sdma0 */
1075 tmp = RREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET);
1076 tmp |= SDMA0_F32_CNTL__HALT_MASK;
1077 WREG32(mmSDMA0_F32_CNTL + SDMA0_REGISTER_OFFSET, tmp);
1078 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA_MASK;
1079 }
1080 if (tmp & SRBM_STATUS2__SDMA1_BUSY_MASK) {
1081 /* sdma1 */
1082 tmp = RREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET);
1083 tmp |= SDMA0_F32_CNTL__HALT_MASK;
1084 WREG32(mmSDMA0_F32_CNTL + SDMA1_REGISTER_OFFSET, tmp);
1085 srbm_soft_reset |= SRBM_SOFT_RESET__SOFT_RESET_SDMA1_MASK;
1086 }
1087
1088 if (srbm_soft_reset) {
Alex Deuchera2e73f52015-04-20 17:09:27 -04001089 tmp = RREG32(mmSRBM_SOFT_RESET);
1090 tmp |= srbm_soft_reset;
1091 dev_info(adev->dev, "SRBM_SOFT_RESET=0x%08X\n", tmp);
1092 WREG32(mmSRBM_SOFT_RESET, tmp);
1093 tmp = RREG32(mmSRBM_SOFT_RESET);
1094
1095 udelay(50);
1096
1097 tmp &= ~srbm_soft_reset;
1098 WREG32(mmSRBM_SOFT_RESET, tmp);
1099 tmp = RREG32(mmSRBM_SOFT_RESET);
1100
1101 /* Wait a little for things to settle down */
1102 udelay(50);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001103 }
1104
1105 return 0;
1106}
1107
1108static int cik_sdma_set_trap_irq_state(struct amdgpu_device *adev,
1109 struct amdgpu_irq_src *src,
1110 unsigned type,
1111 enum amdgpu_interrupt_state state)
1112{
1113 u32 sdma_cntl;
1114
1115 switch (type) {
1116 case AMDGPU_SDMA_IRQ_TRAP0:
1117 switch (state) {
1118 case AMDGPU_IRQ_STATE_DISABLE:
1119 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1120 sdma_cntl &= ~SDMA0_CNTL__TRAP_ENABLE_MASK;
1121 WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1122 break;
1123 case AMDGPU_IRQ_STATE_ENABLE:
1124 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET);
1125 sdma_cntl |= SDMA0_CNTL__TRAP_ENABLE_MASK;
1126 WREG32(mmSDMA0_CNTL + SDMA0_REGISTER_OFFSET, sdma_cntl);
1127 break;
1128 default:
1129 break;
1130 }
1131 break;
1132 case AMDGPU_SDMA_IRQ_TRAP1:
1133 switch (state) {
1134 case AMDGPU_IRQ_STATE_DISABLE:
1135 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1136 sdma_cntl &= ~SDMA0_CNTL__TRAP_ENABLE_MASK;
1137 WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1138 break;
1139 case AMDGPU_IRQ_STATE_ENABLE:
1140 sdma_cntl = RREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET);
1141 sdma_cntl |= SDMA0_CNTL__TRAP_ENABLE_MASK;
1142 WREG32(mmSDMA0_CNTL + SDMA1_REGISTER_OFFSET, sdma_cntl);
1143 break;
1144 default:
1145 break;
1146 }
1147 break;
1148 default:
1149 break;
1150 }
1151 return 0;
1152}
1153
1154static int cik_sdma_process_trap_irq(struct amdgpu_device *adev,
1155 struct amdgpu_irq_src *source,
1156 struct amdgpu_iv_entry *entry)
1157{
1158 u8 instance_id, queue_id;
1159
1160 instance_id = (entry->ring_id & 0x3) >> 0;
1161 queue_id = (entry->ring_id & 0xc) >> 2;
1162 DRM_DEBUG("IH: SDMA trap\n");
1163 switch (instance_id) {
1164 case 0:
1165 switch (queue_id) {
1166 case 0:
Alex Deucherc113ea12015-10-08 16:30:37 -04001167 amdgpu_fence_process(&adev->sdma.instance[0].ring);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001168 break;
1169 case 1:
1170 /* XXX compute */
1171 break;
1172 case 2:
1173 /* XXX compute */
1174 break;
1175 }
1176 break;
1177 case 1:
1178 switch (queue_id) {
1179 case 0:
Alex Deucherc113ea12015-10-08 16:30:37 -04001180 amdgpu_fence_process(&adev->sdma.instance[1].ring);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001181 break;
1182 case 1:
1183 /* XXX compute */
1184 break;
1185 case 2:
1186 /* XXX compute */
1187 break;
1188 }
1189 break;
1190 }
1191
1192 return 0;
1193}
1194
1195static int cik_sdma_process_illegal_inst_irq(struct amdgpu_device *adev,
1196 struct amdgpu_irq_src *source,
1197 struct amdgpu_iv_entry *entry)
1198{
1199 DRM_ERROR("Illegal instruction in SDMA command stream\n");
1200 schedule_work(&adev->reset_work);
1201 return 0;
1202}
1203
yanyang15fc3aee2015-05-22 14:39:35 -04001204static int cik_sdma_set_clockgating_state(void *handle,
1205 enum amd_clockgating_state state)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001206{
1207 bool gate = false;
yanyang15fc3aee2015-05-22 14:39:35 -04001208 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001209
yanyang15fc3aee2015-05-22 14:39:35 -04001210 if (state == AMD_CG_STATE_GATE)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001211 gate = true;
1212
1213 cik_enable_sdma_mgcg(adev, gate);
1214 cik_enable_sdma_mgls(adev, gate);
1215
1216 return 0;
1217}
1218
yanyang15fc3aee2015-05-22 14:39:35 -04001219static int cik_sdma_set_powergating_state(void *handle,
1220 enum amd_powergating_state state)
Alex Deuchera2e73f52015-04-20 17:09:27 -04001221{
1222 return 0;
1223}
1224
Alex Deuchera1255102016-10-13 17:41:13 -04001225static const struct amd_ip_funcs cik_sdma_ip_funcs = {
Tom St Denis88a907d2016-05-04 14:28:35 -04001226 .name = "cik_sdma",
Alex Deuchera2e73f52015-04-20 17:09:27 -04001227 .early_init = cik_sdma_early_init,
1228 .late_init = NULL,
1229 .sw_init = cik_sdma_sw_init,
1230 .sw_fini = cik_sdma_sw_fini,
1231 .hw_init = cik_sdma_hw_init,
1232 .hw_fini = cik_sdma_hw_fini,
1233 .suspend = cik_sdma_suspend,
1234 .resume = cik_sdma_resume,
1235 .is_idle = cik_sdma_is_idle,
1236 .wait_for_idle = cik_sdma_wait_for_idle,
1237 .soft_reset = cik_sdma_soft_reset,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001238 .set_clockgating_state = cik_sdma_set_clockgating_state,
1239 .set_powergating_state = cik_sdma_set_powergating_state,
1240};
1241
Alex Deuchera2e73f52015-04-20 17:09:27 -04001242static const struct amdgpu_ring_funcs cik_sdma_ring_funcs = {
Christian König21cd9422016-10-05 15:36:39 +02001243 .type = AMDGPU_RING_TYPE_SDMA,
Christian König79887142016-10-05 16:09:32 +02001244 .align_mask = 0xf,
1245 .nop = SDMA_PACKET(SDMA_OPCODE_NOP, 0, 0),
Ken Wang536fbf92016-03-12 09:32:30 +08001246 .support_64bit_ptrs = false,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001247 .get_rptr = cik_sdma_ring_get_rptr,
1248 .get_wptr = cik_sdma_ring_get_wptr,
1249 .set_wptr = cik_sdma_ring_set_wptr,
Christian Könige12f3d72016-10-05 14:29:38 +02001250 .emit_frame_size =
1251 6 + /* cik_sdma_ring_emit_hdp_flush */
1252 3 + /* cik_sdma_ring_emit_hdp_invalidate */
1253 6 + /* cik_sdma_ring_emit_pipeline_sync */
1254 12 + /* cik_sdma_ring_emit_vm_flush */
1255 9 + 9 + 9, /* cik_sdma_ring_emit_fence x3 for user fence, vm fence */
1256 .emit_ib_size = 7 + 4, /* cik_sdma_ring_emit_ib */
Alex Deuchera2e73f52015-04-20 17:09:27 -04001257 .emit_ib = cik_sdma_ring_emit_ib,
1258 .emit_fence = cik_sdma_ring_emit_fence,
Christian König00b7c4f2016-03-08 14:11:00 +01001259 .emit_pipeline_sync = cik_sdma_ring_emit_pipeline_sync,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001260 .emit_vm_flush = cik_sdma_ring_emit_vm_flush,
Christian Königd2edb072015-05-11 14:10:34 +02001261 .emit_hdp_flush = cik_sdma_ring_emit_hdp_flush,
Chunming Zhou498dd972016-03-03 12:05:44 +08001262 .emit_hdp_invalidate = cik_sdma_ring_emit_hdp_invalidate,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001263 .test_ring = cik_sdma_ring_test_ring,
1264 .test_ib = cik_sdma_ring_test_ib,
Jammy Zhouac01db32015-09-01 13:13:54 +08001265 .insert_nop = cik_sdma_ring_insert_nop,
Christian König9e5d53092016-01-31 12:20:55 +01001266 .pad_ib = cik_sdma_ring_pad_ib,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001267};
1268
1269static void cik_sdma_set_ring_funcs(struct amdgpu_device *adev)
1270{
Alex Deucherc113ea12015-10-08 16:30:37 -04001271 int i;
1272
1273 for (i = 0; i < adev->sdma.num_instances; i++)
1274 adev->sdma.instance[i].ring.funcs = &cik_sdma_ring_funcs;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001275}
1276
1277static const struct amdgpu_irq_src_funcs cik_sdma_trap_irq_funcs = {
1278 .set = cik_sdma_set_trap_irq_state,
1279 .process = cik_sdma_process_trap_irq,
1280};
1281
1282static const struct amdgpu_irq_src_funcs cik_sdma_illegal_inst_irq_funcs = {
1283 .process = cik_sdma_process_illegal_inst_irq,
1284};
1285
1286static void cik_sdma_set_irq_funcs(struct amdgpu_device *adev)
1287{
Alex Deucherc113ea12015-10-08 16:30:37 -04001288 adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_LAST;
1289 adev->sdma.trap_irq.funcs = &cik_sdma_trap_irq_funcs;
1290 adev->sdma.illegal_inst_irq.funcs = &cik_sdma_illegal_inst_irq_funcs;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001291}
1292
1293/**
1294 * cik_sdma_emit_copy_buffer - copy buffer using the sDMA engine
1295 *
1296 * @ring: amdgpu_ring structure holding ring information
1297 * @src_offset: src GPU address
1298 * @dst_offset: dst GPU address
1299 * @byte_count: number of bytes to xfer
1300 *
1301 * Copy GPU buffers using the DMA engine (CIK).
1302 * Used by the amdgpu ttm implementation to move pages if
1303 * registered as the asic copy callback.
1304 */
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001305static void cik_sdma_emit_copy_buffer(struct amdgpu_ib *ib,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001306 uint64_t src_offset,
1307 uint64_t dst_offset,
1308 uint32_t byte_count)
1309{
Chunming Zhouc7ae72c2015-08-25 17:23:45 +08001310 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_COPY, SDMA_COPY_SUB_OPCODE_LINEAR, 0);
1311 ib->ptr[ib->length_dw++] = byte_count;
1312 ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1313 ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
1314 ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
1315 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1316 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
Alex Deuchera2e73f52015-04-20 17:09:27 -04001317}
1318
1319/**
1320 * cik_sdma_emit_fill_buffer - fill buffer using the sDMA engine
1321 *
1322 * @ring: amdgpu_ring structure holding ring information
1323 * @src_data: value to write to buffer
1324 * @dst_offset: dst GPU address
1325 * @byte_count: number of bytes to xfer
1326 *
1327 * Fill GPU buffers using the DMA engine (CIK).
1328 */
Chunming Zhou6e7a3842015-08-27 13:46:09 +08001329static void cik_sdma_emit_fill_buffer(struct amdgpu_ib *ib,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001330 uint32_t src_data,
1331 uint64_t dst_offset,
1332 uint32_t byte_count)
1333{
Chunming Zhou6e7a3842015-08-27 13:46:09 +08001334 ib->ptr[ib->length_dw++] = SDMA_PACKET(SDMA_OPCODE_CONSTANT_FILL, 0, 0);
1335 ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1336 ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1337 ib->ptr[ib->length_dw++] = src_data;
1338 ib->ptr[ib->length_dw++] = byte_count;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001339}
1340
1341static const struct amdgpu_buffer_funcs cik_sdma_buffer_funcs = {
1342 .copy_max_bytes = 0x1fffff,
1343 .copy_num_dw = 7,
1344 .emit_copy_buffer = cik_sdma_emit_copy_buffer,
1345
1346 .fill_max_bytes = 0x1fffff,
1347 .fill_num_dw = 5,
1348 .emit_fill_buffer = cik_sdma_emit_fill_buffer,
1349};
1350
1351static void cik_sdma_set_buffer_funcs(struct amdgpu_device *adev)
1352{
1353 if (adev->mman.buffer_funcs == NULL) {
1354 adev->mman.buffer_funcs = &cik_sdma_buffer_funcs;
Alex Deucherc113ea12015-10-08 16:30:37 -04001355 adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001356 }
1357}
1358
1359static const struct amdgpu_vm_pte_funcs cik_sdma_vm_pte_funcs = {
1360 .copy_pte = cik_sdma_vm_copy_pte,
1361 .write_pte = cik_sdma_vm_write_pte,
1362 .set_pte_pde = cik_sdma_vm_set_pte_pde,
Alex Deuchera2e73f52015-04-20 17:09:27 -04001363};
1364
1365static void cik_sdma_set_vm_pte_funcs(struct amdgpu_device *adev)
1366{
Christian König2d55e452016-02-08 17:37:38 +01001367 unsigned i;
1368
Alex Deuchera2e73f52015-04-20 17:09:27 -04001369 if (adev->vm_manager.vm_pte_funcs == NULL) {
1370 adev->vm_manager.vm_pte_funcs = &cik_sdma_vm_pte_funcs;
Christian König2d55e452016-02-08 17:37:38 +01001371 for (i = 0; i < adev->sdma.num_instances; i++)
1372 adev->vm_manager.vm_pte_rings[i] =
1373 &adev->sdma.instance[i].ring;
1374
1375 adev->vm_manager.vm_pte_num_rings = adev->sdma.num_instances;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001376 }
1377}
Alex Deuchera1255102016-10-13 17:41:13 -04001378
1379const struct amdgpu_ip_block_version cik_sdma_ip_block =
1380{
1381 .type = AMD_IP_BLOCK_TYPE_SDMA,
1382 .major = 2,
1383 .minor = 0,
1384 .rev = 0,
1385 .funcs = &cik_sdma_ip_funcs,
1386};