blob: a3692dba64fffeb980ea71ccb111d3faf4993f49 [file] [log] [blame]
Hawking Zhangc6b6a422019-03-04 14:07:37 +08001/*
2 * Copyright 2019 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 */
23#include <linux/firmware.h>
24#include <linux/slab.h>
25#include <linux/module.h>
26#include <drm/drmP.h>
27#include "amdgpu.h"
28#include "amdgpu_atombios.h"
29#include "amdgpu_ih.h"
30#include "amdgpu_uvd.h"
31#include "amdgpu_vce.h"
32#include "amdgpu_ucode.h"
33#include "amdgpu_psp.h"
34#include "atom.h"
35#include "amd_pcie.h"
36
37#include "gc/gc_10_1_0_offset.h"
38#include "gc/gc_10_1_0_sh_mask.h"
39#include "hdp/hdp_5_0_0_offset.h"
40#include "hdp/hdp_5_0_0_sh_mask.h"
41
42#include "soc15.h"
43#include "soc15_common.h"
44#include "gmc_v10_0.h"
45#include "gfxhub_v2_0.h"
46#include "mmhub_v2_0.h"
47#include "nv.h"
48#include "navi10_ih.h"
49#include "gfx_v10_0.h"
50#include "sdma_v5_0.h"
51#include "vcn_v2_0.h"
52#include "dce_virtual.h"
53#include "mes_v10_1.h"
54
55static const struct amd_ip_funcs nv_common_ip_funcs;
56
57/*
58 * Indirect registers accessor
59 */
60static u32 nv_pcie_rreg(struct amdgpu_device *adev, u32 reg)
61{
62 unsigned long flags, address, data;
63 u32 r;
64 address = adev->nbio_funcs->get_pcie_index_offset(adev);
65 data = adev->nbio_funcs->get_pcie_data_offset(adev);
66
67 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
68 WREG32(address, reg);
69 (void)RREG32(address);
70 r = RREG32(data);
71 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
72 return r;
73}
74
75static void nv_pcie_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
76{
77 unsigned long flags, address, data;
78
79 address = adev->nbio_funcs->get_pcie_index_offset(adev);
80 data = adev->nbio_funcs->get_pcie_data_offset(adev);
81
82 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
83 WREG32(address, reg);
84 (void)RREG32(address);
85 WREG32(data, v);
86 (void)RREG32(data);
87 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
88}
89
90static u32 nv_didt_rreg(struct amdgpu_device *adev, u32 reg)
91{
92 unsigned long flags, address, data;
93 u32 r;
94
95 address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
96 data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
97
98 spin_lock_irqsave(&adev->didt_idx_lock, flags);
99 WREG32(address, (reg));
100 r = RREG32(data);
101 spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
102 return r;
103}
104
105static void nv_didt_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
106{
107 unsigned long flags, address, data;
108
109 address = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_INDEX);
110 data = SOC15_REG_OFFSET(GC, 0, mmDIDT_IND_DATA);
111
112 spin_lock_irqsave(&adev->didt_idx_lock, flags);
113 WREG32(address, (reg));
114 WREG32(data, (v));
115 spin_unlock_irqrestore(&adev->didt_idx_lock, flags);
116}
117
118static u32 nv_get_config_memsize(struct amdgpu_device *adev)
119{
120 return adev->nbio_funcs->get_memsize(adev);
121}
122
123static u32 nv_get_xclk(struct amdgpu_device *adev)
124{
Tao Zhou462a70d2019-05-14 11:37:32 +0800125 return adev->clock.spll.reference_freq;
Hawking Zhangc6b6a422019-03-04 14:07:37 +0800126}
127
128
129void nv_grbm_select(struct amdgpu_device *adev,
130 u32 me, u32 pipe, u32 queue, u32 vmid)
131{
132 u32 grbm_gfx_cntl = 0;
133 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, PIPEID, pipe);
134 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, MEID, me);
135 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, VMID, vmid);
136 grbm_gfx_cntl = REG_SET_FIELD(grbm_gfx_cntl, GRBM_GFX_CNTL, QUEUEID, queue);
137
138 WREG32(SOC15_REG_OFFSET(GC, 0, mmGRBM_GFX_CNTL), grbm_gfx_cntl);
139}
140
141static void nv_vga_set_state(struct amdgpu_device *adev, bool state)
142{
143 /* todo */
144}
145
146static bool nv_read_disabled_bios(struct amdgpu_device *adev)
147{
148 /* todo */
149 return false;
150}
151
152static bool nv_read_bios_from_rom(struct amdgpu_device *adev,
153 u8 *bios, u32 length_bytes)
154{
155 /* TODO: will implement it when SMU header is available */
156 return false;
157}
158
159static struct soc15_allowed_register_entry nv_allowed_read_registers[] = {
160 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS)},
161 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS2)},
162 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE0)},
163 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE1)},
164 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE2)},
165 { SOC15_REG_ENTRY(GC, 0, mmGRBM_STATUS_SE3)},
166#if 0 /* TODO: will set it when SDMA header is available */
167 { SOC15_REG_ENTRY(SDMA0, 0, mmSDMA0_STATUS_REG)},
168 { SOC15_REG_ENTRY(SDMA1, 0, mmSDMA1_STATUS_REG)},
169#endif
170 { SOC15_REG_ENTRY(GC, 0, mmCP_STAT)},
171 { SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT1)},
172 { SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT2)},
173 { SOC15_REG_ENTRY(GC, 0, mmCP_STALLED_STAT3)},
174 { SOC15_REG_ENTRY(GC, 0, mmCP_CPF_BUSY_STAT)},
175 { SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STALLED_STAT1)},
176 { SOC15_REG_ENTRY(GC, 0, mmCP_CPF_STATUS)},
177 { SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STALLED_STAT1)},
178 { SOC15_REG_ENTRY(GC, 0, mmCP_CPC_STATUS)},
179 { SOC15_REG_ENTRY(GC, 0, mmGB_ADDR_CONFIG)},
180};
181
182static uint32_t nv_read_indexed_register(struct amdgpu_device *adev, u32 se_num,
183 u32 sh_num, u32 reg_offset)
184{
185 uint32_t val;
186
187 mutex_lock(&adev->grbm_idx_mutex);
188 if (se_num != 0xffffffff || sh_num != 0xffffffff)
189 amdgpu_gfx_select_se_sh(adev, se_num, sh_num, 0xffffffff);
190
191 val = RREG32(reg_offset);
192
193 if (se_num != 0xffffffff || sh_num != 0xffffffff)
194 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
195 mutex_unlock(&adev->grbm_idx_mutex);
196 return val;
197}
198
199static uint32_t nv_get_register_value(struct amdgpu_device *adev,
200 bool indexed, u32 se_num,
201 u32 sh_num, u32 reg_offset)
202{
203 if (indexed) {
204 return nv_read_indexed_register(adev, se_num, sh_num, reg_offset);
205 } else {
206 if (reg_offset == SOC15_REG_OFFSET(GC, 0, mmGB_ADDR_CONFIG))
207 return adev->gfx.config.gb_addr_config;
208 return RREG32(reg_offset);
209 }
210}
211
212static int nv_read_register(struct amdgpu_device *adev, u32 se_num,
213 u32 sh_num, u32 reg_offset, u32 *value)
214{
215 uint32_t i;
216 struct soc15_allowed_register_entry *en;
217
218 *value = 0;
219 for (i = 0; i < ARRAY_SIZE(nv_allowed_read_registers); i++) {
220 en = &nv_allowed_read_registers[i];
221 if (reg_offset !=
222 (adev->reg_offset[en->hwip][en->inst][en->seg] + en->reg_offset))
223 continue;
224
225 *value = nv_get_register_value(adev,
226 nv_allowed_read_registers[i].grbm_indexed,
227 se_num, sh_num, reg_offset);
228 return 0;
229 }
230 return -EINVAL;
231}
232
233#if 0
234static void nv_gpu_pci_config_reset(struct amdgpu_device *adev)
235{
236 u32 i;
237
238 dev_info(adev->dev, "GPU pci config reset\n");
239
240 /* disable BM */
241 pci_clear_master(adev->pdev);
242 /* reset */
243 amdgpu_pci_config_reset(adev);
244
245 udelay(100);
246
247 /* wait for asic to come out of reset */
248 for (i = 0; i < adev->usec_timeout; i++) {
249 u32 memsize = nbio_v2_3_get_memsize(adev);
250 if (memsize != 0xffffffff)
251 break;
252 udelay(1);
253 }
254
255}
256#endif
257
258static int nv_asic_reset(struct amdgpu_device *adev)
259{
260
261 /* FIXME: it doesn't work since vega10 */
262#if 0
263 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
264
265 nv_gpu_pci_config_reset(adev);
266
267 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
268#endif
269
270 return 0;
271}
272
273static int nv_set_uvd_clocks(struct amdgpu_device *adev, u32 vclk, u32 dclk)
274{
275 /* todo */
276 return 0;
277}
278
279static int nv_set_vce_clocks(struct amdgpu_device *adev, u32 evclk, u32 ecclk)
280{
281 /* todo */
282 return 0;
283}
284
285static void nv_pcie_gen3_enable(struct amdgpu_device *adev)
286{
287 if (pci_is_root_bus(adev->pdev->bus))
288 return;
289
290 if (amdgpu_pcie_gen2 == 0)
291 return;
292
293 if (!(adev->pm.pcie_gen_mask & (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
294 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3)))
295 return;
296
297 /* todo */
298}
299
300static void nv_program_aspm(struct amdgpu_device *adev)
301{
302
303 if (amdgpu_aspm == 0)
304 return;
305
306 /* todo */
307}
308
309static void nv_enable_doorbell_aperture(struct amdgpu_device *adev,
310 bool enable)
311{
312 adev->nbio_funcs->enable_doorbell_aperture(adev, enable);
313 adev->nbio_funcs->enable_doorbell_selfring_aperture(adev, enable);
314}
315
316static const struct amdgpu_ip_block_version nv_common_ip_block =
317{
318 .type = AMD_IP_BLOCK_TYPE_COMMON,
319 .major = 1,
320 .minor = 0,
321 .rev = 0,
322 .funcs = &nv_common_ip_funcs,
323};
324
325int nv_set_ip_blocks(struct amdgpu_device *adev)
326{
327 /* Set IP register base before any HW register access */
328 switch (adev->asic_type) {
329 case CHIP_NAVI10:
330 navi10_reg_base_init(adev);
331 break;
332 default:
333 return -EINVAL;
334 }
335
336 adev->nbio_funcs = &nbio_v2_3_funcs;
337
338 adev->nbio_funcs->detect_hw_virt(adev);
339
340 switch (adev->asic_type) {
341 case CHIP_NAVI10:
342 amdgpu_device_ip_block_add(adev, &nv_common_ip_block);
343 amdgpu_device_ip_block_add(adev, &gmc_v10_0_ip_block);
344 amdgpu_device_ip_block_add(adev, &navi10_ih_ip_block);
345 amdgpu_device_ip_block_add(adev, &psp_v11_0_ip_block);
346 if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP &&
347 is_support_sw_smu(adev))
348 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
349 if (adev->enable_virtual_display || amdgpu_sriov_vf(adev))
350 amdgpu_device_ip_block_add(adev, &dce_virtual_ip_block);
351 amdgpu_device_ip_block_add(adev, &gfx_v10_0_ip_block);
352 amdgpu_device_ip_block_add(adev, &sdma_v5_0_ip_block);
353 if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT &&
354 is_support_sw_smu(adev))
355 amdgpu_device_ip_block_add(adev, &smu_v11_0_ip_block);
356 amdgpu_device_ip_block_add(adev, &vcn_v2_0_ip_block);
357 if (adev->enable_mes)
358 amdgpu_device_ip_block_add(adev, &mes_v10_1_ip_block);
359 break;
360 default:
361 return -EINVAL;
362 }
363
364 return 0;
365}
366
367static uint32_t nv_get_rev_id(struct amdgpu_device *adev)
368{
369 return adev->nbio_funcs->get_rev_id(adev);
370}
371
372static void nv_flush_hdp(struct amdgpu_device *adev, struct amdgpu_ring *ring)
373{
374 adev->nbio_funcs->hdp_flush(adev, ring);
375}
376
377static void nv_invalidate_hdp(struct amdgpu_device *adev,
378 struct amdgpu_ring *ring)
379{
380 if (!ring || !ring->funcs->emit_wreg) {
381 WREG32_SOC15_NO_KIQ(NBIO, 0, mmHDP_READ_CACHE_INVALIDATE, 1);
382 } else {
383 amdgpu_ring_emit_wreg(ring, SOC15_REG_OFFSET(
384 HDP, 0, mmHDP_READ_CACHE_INVALIDATE), 1);
385 }
386}
387
388static bool nv_need_full_reset(struct amdgpu_device *adev)
389{
390 return true;
391}
392
393static void nv_get_pcie_usage(struct amdgpu_device *adev,
394 uint64_t *count0,
395 uint64_t *count1)
396{
397 /*TODO*/
398}
399
400static bool nv_need_reset_on_init(struct amdgpu_device *adev)
401{
402#if 0
403 u32 sol_reg;
404
405 if (adev->flags & AMD_IS_APU)
406 return false;
407
408 /* Check sOS sign of life register to confirm sys driver and sOS
409 * are already been loaded.
410 */
411 sol_reg = RREG32_SOC15(MP0, 0, mmMP0_SMN_C2PMSG_81);
412 if (sol_reg)
413 return true;
414#endif
415 /* TODO: re-enable it when mode1 reset is functional */
416 return false;
417}
418
419static void nv_init_doorbell_index(struct amdgpu_device *adev)
420{
421 adev->doorbell_index.kiq = AMDGPU_NAVI10_DOORBELL_KIQ;
422 adev->doorbell_index.mec_ring0 = AMDGPU_NAVI10_DOORBELL_MEC_RING0;
423 adev->doorbell_index.mec_ring1 = AMDGPU_NAVI10_DOORBELL_MEC_RING1;
424 adev->doorbell_index.mec_ring2 = AMDGPU_NAVI10_DOORBELL_MEC_RING2;
425 adev->doorbell_index.mec_ring3 = AMDGPU_NAVI10_DOORBELL_MEC_RING3;
426 adev->doorbell_index.mec_ring4 = AMDGPU_NAVI10_DOORBELL_MEC_RING4;
427 adev->doorbell_index.mec_ring5 = AMDGPU_NAVI10_DOORBELL_MEC_RING5;
428 adev->doorbell_index.mec_ring6 = AMDGPU_NAVI10_DOORBELL_MEC_RING6;
429 adev->doorbell_index.mec_ring7 = AMDGPU_NAVI10_DOORBELL_MEC_RING7;
430 adev->doorbell_index.userqueue_start = AMDGPU_NAVI10_DOORBELL_USERQUEUE_START;
431 adev->doorbell_index.userqueue_end = AMDGPU_NAVI10_DOORBELL_USERQUEUE_END;
432 adev->doorbell_index.gfx_ring0 = AMDGPU_NAVI10_DOORBELL_GFX_RING0;
433 adev->doorbell_index.gfx_ring1 = AMDGPU_NAVI10_DOORBELL_GFX_RING1;
434 adev->doorbell_index.sdma_engine[0] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE0;
435 adev->doorbell_index.sdma_engine[1] = AMDGPU_NAVI10_DOORBELL_sDMA_ENGINE1;
436 adev->doorbell_index.ih = AMDGPU_NAVI10_DOORBELL_IH;
437 adev->doorbell_index.vcn.vcn_ring0_1 = AMDGPU_NAVI10_DOORBELL64_VCN0_1;
438 adev->doorbell_index.vcn.vcn_ring2_3 = AMDGPU_NAVI10_DOORBELL64_VCN2_3;
439 adev->doorbell_index.vcn.vcn_ring4_5 = AMDGPU_NAVI10_DOORBELL64_VCN4_5;
440 adev->doorbell_index.vcn.vcn_ring6_7 = AMDGPU_NAVI10_DOORBELL64_VCN6_7;
441 adev->doorbell_index.first_non_cp = AMDGPU_NAVI10_DOORBELL64_FIRST_NON_CP;
442 adev->doorbell_index.last_non_cp = AMDGPU_NAVI10_DOORBELL64_LAST_NON_CP;
443
444 adev->doorbell_index.max_assignment = AMDGPU_NAVI10_DOORBELL_MAX_ASSIGNMENT << 1;
445 adev->doorbell_index.sdma_doorbell_range = 20;
446}
447
448static const struct amdgpu_asic_funcs nv_asic_funcs =
449{
450 .read_disabled_bios = &nv_read_disabled_bios,
451 .read_bios_from_rom = &nv_read_bios_from_rom,
452 .read_register = &nv_read_register,
453 .reset = &nv_asic_reset,
454 .set_vga_state = &nv_vga_set_state,
455 .get_xclk = &nv_get_xclk,
456 .set_uvd_clocks = &nv_set_uvd_clocks,
457 .set_vce_clocks = &nv_set_vce_clocks,
458 .get_config_memsize = &nv_get_config_memsize,
459 .flush_hdp = &nv_flush_hdp,
460 .invalidate_hdp = &nv_invalidate_hdp,
461 .init_doorbell_index = &nv_init_doorbell_index,
462 .need_full_reset = &nv_need_full_reset,
463 .get_pcie_usage = &nv_get_pcie_usage,
464 .need_reset_on_init = &nv_need_reset_on_init,
465};
466
467static int nv_common_early_init(void *handle)
468{
469 bool psp_enabled = false;
470 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
471
472 adev->smc_rreg = NULL;
473 adev->smc_wreg = NULL;
474 adev->pcie_rreg = &nv_pcie_rreg;
475 adev->pcie_wreg = &nv_pcie_wreg;
476
477 /* TODO: will add them during VCN v2 implementation */
478 adev->uvd_ctx_rreg = NULL;
479 adev->uvd_ctx_wreg = NULL;
480
481 adev->didt_rreg = &nv_didt_rreg;
482 adev->didt_wreg = &nv_didt_wreg;
483
484 adev->asic_funcs = &nv_asic_funcs;
485
486 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_PSP) &&
487 (amdgpu_ip_block_mask & (1 << AMD_IP_BLOCK_TYPE_PSP)))
488 psp_enabled = true;
489
490 adev->rev_id = nv_get_rev_id(adev);
491 adev->external_rev_id = 0xff;
492 switch (adev->asic_type) {
493 case CHIP_NAVI10:
494 adev->cg_flags = AMD_CG_SUPPORT_GFX_MGCG |
Hawking Zhangc6b6a422019-03-04 14:07:37 +0800495 AMD_CG_SUPPORT_GFX_CGCG |
496 AMD_CG_SUPPORT_IH_CG |
497 AMD_CG_SUPPORT_HDP_MGCG |
498 AMD_CG_SUPPORT_HDP_LS |
499 AMD_CG_SUPPORT_SDMA_MGCG |
500 AMD_CG_SUPPORT_SDMA_LS |
501 AMD_CG_SUPPORT_MC_MGCG |
502 AMD_CG_SUPPORT_MC_LS |
503 AMD_CG_SUPPORT_ATHUB_MGCG |
504 AMD_CG_SUPPORT_ATHUB_LS |
505 AMD_CG_SUPPORT_VCN_MGCG |
506 AMD_CG_SUPPORT_BIF_MGCG |
507 AMD_CG_SUPPORT_BIF_LS;
Jack Xiao6ff68732019-04-18 15:55:27 +0800508 adev->pg_flags = AMD_PG_SUPPORT_VCN;
Hawking Zhangc6b6a422019-03-04 14:07:37 +0800509 adev->external_rev_id = adev->rev_id + 0x1;
510 break;
511 default:
512 /* FIXME: not supported yet */
513 return -EINVAL;
514 }
515
516 return 0;
517}
518
519static int nv_common_late_init(void *handle)
520{
521 return 0;
522}
523
524static int nv_common_sw_init(void *handle)
525{
526 return 0;
527}
528
529static int nv_common_sw_fini(void *handle)
530{
531 return 0;
532}
533
534static int nv_common_hw_init(void *handle)
535{
536 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
537
538 /* enable pcie gen2/3 link */
539 nv_pcie_gen3_enable(adev);
540 /* enable aspm */
541 nv_program_aspm(adev);
542 /* setup nbio registers */
543 adev->nbio_funcs->init_registers(adev);
544 /* enable the doorbell aperture */
545 nv_enable_doorbell_aperture(adev, true);
546
547 return 0;
548}
549
550static int nv_common_hw_fini(void *handle)
551{
552 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
553
554 /* disable the doorbell aperture */
555 nv_enable_doorbell_aperture(adev, false);
556
557 return 0;
558}
559
560static int nv_common_suspend(void *handle)
561{
562 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
563
564 return nv_common_hw_fini(adev);
565}
566
567static int nv_common_resume(void *handle)
568{
569 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
570
571 return nv_common_hw_init(adev);
572}
573
574static bool nv_common_is_idle(void *handle)
575{
576 return true;
577}
578
579static int nv_common_wait_for_idle(void *handle)
580{
581 return 0;
582}
583
584static int nv_common_soft_reset(void *handle)
585{
586 return 0;
587}
588
589static void nv_update_hdp_mem_power_gating(struct amdgpu_device *adev,
590 bool enable)
591{
592 uint32_t hdp_clk_cntl, hdp_clk_cntl1;
593 uint32_t hdp_mem_pwr_cntl;
594
595 if (!(adev->cg_flags & (AMD_CG_SUPPORT_HDP_LS |
596 AMD_CG_SUPPORT_HDP_DS |
597 AMD_CG_SUPPORT_HDP_SD)))
598 return;
599
600 hdp_clk_cntl = hdp_clk_cntl1 = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
601 hdp_mem_pwr_cntl = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL);
602
603 /* Before doing clock/power mode switch,
604 * forced on IPH & RC clock */
605 hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
606 IPH_MEM_CLK_SOFT_OVERRIDE, 1);
607 hdp_clk_cntl = REG_SET_FIELD(hdp_clk_cntl, HDP_CLK_CNTL,
608 RC_MEM_CLK_SOFT_OVERRIDE, 1);
609 WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl);
610
611 /* HDP 5.0 doesn't support dynamic power mode switch,
612 * disable clock and power gating before any changing */
613 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
614 IPH_MEM_POWER_CTRL_EN, 0);
615 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
616 IPH_MEM_POWER_LS_EN, 0);
617 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
618 IPH_MEM_POWER_DS_EN, 0);
619 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
620 IPH_MEM_POWER_SD_EN, 0);
621 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
622 RC_MEM_POWER_CTRL_EN, 0);
623 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
624 RC_MEM_POWER_LS_EN, 0);
625 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
626 RC_MEM_POWER_DS_EN, 0);
627 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl, HDP_MEM_POWER_CTRL,
628 RC_MEM_POWER_SD_EN, 0);
629 WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl);
630
631 /* only one clock gating mode (LS/DS/SD) can be enabled */
632 if (adev->cg_flags & AMD_CG_SUPPORT_HDP_LS) {
633 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
634 HDP_MEM_POWER_CTRL,
635 IPH_MEM_POWER_LS_EN, enable);
636 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
637 HDP_MEM_POWER_CTRL,
638 RC_MEM_POWER_LS_EN, enable);
639 } else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_DS) {
640 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
641 HDP_MEM_POWER_CTRL,
642 IPH_MEM_POWER_DS_EN, enable);
643 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
644 HDP_MEM_POWER_CTRL,
645 RC_MEM_POWER_DS_EN, enable);
646 } else if (adev->cg_flags & AMD_CG_SUPPORT_HDP_SD) {
647 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
648 HDP_MEM_POWER_CTRL,
649 IPH_MEM_POWER_SD_EN, enable);
650 /* RC should not use shut down mode, fallback to ds */
651 hdp_mem_pwr_cntl = REG_SET_FIELD(hdp_mem_pwr_cntl,
652 HDP_MEM_POWER_CTRL,
653 RC_MEM_POWER_DS_EN, enable);
654 }
655
656 WREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL, hdp_mem_pwr_cntl);
657
658 /* restore IPH & RC clock override after clock/power mode changing */
659 WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl1);
660}
661
662static void nv_update_hdp_clock_gating(struct amdgpu_device *adev,
663 bool enable)
664{
665 uint32_t hdp_clk_cntl;
666
667 if (!(adev->cg_flags & AMD_CG_SUPPORT_HDP_MGCG))
668 return;
669
670 hdp_clk_cntl = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
671
672 if (enable) {
673 hdp_clk_cntl &=
674 ~(uint32_t)
675 (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
676 HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
677 HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
678 HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
679 HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
680 HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK);
681 } else {
682 hdp_clk_cntl |= HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
683 HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
684 HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
685 HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
686 HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
687 HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK;
688 }
689
690 WREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL, hdp_clk_cntl);
691}
692
693static int nv_common_set_clockgating_state(void *handle,
694 enum amd_clockgating_state state)
695{
696 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
697
698 if (amdgpu_sriov_vf(adev))
699 return 0;
700
701 switch (adev->asic_type) {
702 case CHIP_NAVI10:
703 adev->nbio_funcs->update_medium_grain_clock_gating(adev,
704 state == AMD_CG_STATE_GATE ? true : false);
705 adev->nbio_funcs->update_medium_grain_light_sleep(adev,
706 state == AMD_CG_STATE_GATE ? true : false);
707 nv_update_hdp_mem_power_gating(adev,
708 state == AMD_CG_STATE_GATE ? true : false);
709 nv_update_hdp_clock_gating(adev,
710 state == AMD_CG_STATE_GATE ? true : false);
711 break;
712 default:
713 break;
714 }
715 return 0;
716}
717
718static int nv_common_set_powergating_state(void *handle,
719 enum amd_powergating_state state)
720{
721 /* TODO */
722 return 0;
723}
724
725static void nv_common_get_clockgating_state(void *handle, u32 *flags)
726{
727 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
728 uint32_t tmp;
729
730 if (amdgpu_sriov_vf(adev))
731 *flags = 0;
732
733 adev->nbio_funcs->get_clockgating_state(adev, flags);
734
735 /* AMD_CG_SUPPORT_HDP_MGCG */
736 tmp = RREG32_SOC15(HDP, 0, mmHDP_CLK_CNTL);
737 if (!(tmp & (HDP_CLK_CNTL__IPH_MEM_CLK_SOFT_OVERRIDE_MASK |
738 HDP_CLK_CNTL__RC_MEM_CLK_SOFT_OVERRIDE_MASK |
739 HDP_CLK_CNTL__DBUS_CLK_SOFT_OVERRIDE_MASK |
740 HDP_CLK_CNTL__DYN_CLK_SOFT_OVERRIDE_MASK |
741 HDP_CLK_CNTL__XDP_REG_CLK_SOFT_OVERRIDE_MASK |
742 HDP_CLK_CNTL__HDP_REG_CLK_SOFT_OVERRIDE_MASK)))
743 *flags |= AMD_CG_SUPPORT_HDP_MGCG;
744
745 /* AMD_CG_SUPPORT_HDP_LS/DS/SD */
746 tmp = RREG32_SOC15(HDP, 0, mmHDP_MEM_POWER_CTRL);
747 if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_LS_EN_MASK)
748 *flags |= AMD_CG_SUPPORT_HDP_LS;
749 else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_DS_EN_MASK)
750 *flags |= AMD_CG_SUPPORT_HDP_DS;
751 else if (tmp & HDP_MEM_POWER_CTRL__IPH_MEM_POWER_SD_EN_MASK)
752 *flags |= AMD_CG_SUPPORT_HDP_SD;
753
754 return;
755}
756
757static const struct amd_ip_funcs nv_common_ip_funcs = {
758 .name = "nv_common",
759 .early_init = nv_common_early_init,
760 .late_init = nv_common_late_init,
761 .sw_init = nv_common_sw_init,
762 .sw_fini = nv_common_sw_fini,
763 .hw_init = nv_common_hw_init,
764 .hw_fini = nv_common_hw_fini,
765 .suspend = nv_common_suspend,
766 .resume = nv_common_resume,
767 .is_idle = nv_common_is_idle,
768 .wait_for_idle = nv_common_wait_for_idle,
769 .soft_reset = nv_common_soft_reset,
770 .set_clockgating_state = nv_common_set_clockgating_state,
771 .set_powergating_state = nv_common_set_powergating_state,
772 .get_clockgating_state = nv_common_get_clockgating_state,
773};