1fef6e24cSHawking Zhang /*
2fef6e24cSHawking Zhang * Copyright 2019 Advanced Micro Devices, Inc.
3fef6e24cSHawking Zhang *
4fef6e24cSHawking Zhang * Permission is hereby granted, free of charge, to any person obtaining a
5fef6e24cSHawking Zhang * copy of this software and associated documentation files (the "Software"),
6fef6e24cSHawking Zhang * to deal in the Software without restriction, including without limitation
7fef6e24cSHawking Zhang * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8fef6e24cSHawking Zhang * and/or sell copies of the Software, and to permit persons to whom the
9fef6e24cSHawking Zhang * Software is furnished to do so, subject to the following conditions:
10fef6e24cSHawking Zhang *
11fef6e24cSHawking Zhang * The above copyright notice and this permission notice shall be included in
12fef6e24cSHawking Zhang * all copies or substantial portions of the Software.
13fef6e24cSHawking Zhang *
14fef6e24cSHawking Zhang * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15fef6e24cSHawking Zhang * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16fef6e24cSHawking Zhang * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17fef6e24cSHawking Zhang * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18fef6e24cSHawking Zhang * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19fef6e24cSHawking Zhang * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20fef6e24cSHawking Zhang * OTHER DEALINGS IN THE SOFTWARE.
21fef6e24cSHawking Zhang *
22fef6e24cSHawking Zhang */
23fef6e24cSHawking Zhang
2475589f49SAlex Deucher #include <linux/delay.h>
25fef6e24cSHawking Zhang #include <linux/firmware.h>
2675589f49SAlex Deucher #include <linux/module.h>
2775589f49SAlex Deucher #include <linux/pci.h>
2875589f49SAlex Deucher
29fef6e24cSHawking Zhang #include "amdgpu.h"
30fef6e24cSHawking Zhang #include "amdgpu_ucode.h"
31fef6e24cSHawking Zhang #include "amdgpu_trace.h"
32fef6e24cSHawking Zhang
33fef6e24cSHawking Zhang #include "gc/gc_10_1_0_offset.h"
34fef6e24cSHawking Zhang #include "gc/gc_10_1_0_sh_mask.h"
35fef6e24cSHawking Zhang #include "ivsrcid/sdma0/irqsrcs_sdma0_5_0.h"
36fef6e24cSHawking Zhang #include "ivsrcid/sdma1/irqsrcs_sdma1_5_0.h"
37fef6e24cSHawking Zhang
38fef6e24cSHawking Zhang #include "soc15_common.h"
39fef6e24cSHawking Zhang #include "soc15.h"
40fef6e24cSHawking Zhang #include "navi10_sdma_pkt_open.h"
41fef6e24cSHawking Zhang #include "nbio_v2_3.h"
42157e72e8SLikun Gao #include "sdma_common.h"
43fef6e24cSHawking Zhang #include "sdma_v5_0.h"
44fef6e24cSHawking Zhang
45fef6e24cSHawking Zhang MODULE_FIRMWARE("amdgpu/navi10_sdma.bin");
46fef6e24cSHawking Zhang MODULE_FIRMWARE("amdgpu/navi10_sdma1.bin");
47fef6e24cSHawking Zhang
486041f2a2SXiaojie Yuan MODULE_FIRMWARE("amdgpu/navi14_sdma.bin");
496041f2a2SXiaojie Yuan MODULE_FIRMWARE("amdgpu/navi14_sdma1.bin");
506041f2a2SXiaojie Yuan
516f523fd7SXiaojie Yuan MODULE_FIRMWARE("amdgpu/navi12_sdma.bin");
526f523fd7SXiaojie Yuan MODULE_FIRMWARE("amdgpu/navi12_sdma1.bin");
536f523fd7SXiaojie Yuan
54bf4759a8STao Zhou MODULE_FIRMWARE("amdgpu/cyan_skillfish2_sdma.bin");
55bf4759a8STao Zhou MODULE_FIRMWARE("amdgpu/cyan_skillfish2_sdma1.bin");
56bf4759a8STao Zhou
57fef6e24cSHawking Zhang #define SDMA1_REG_OFFSET 0x600
58fef6e24cSHawking Zhang #define SDMA0_HYP_DEC_REG_START 0x5880
59fef6e24cSHawking Zhang #define SDMA0_HYP_DEC_REG_END 0x5893
60fef6e24cSHawking Zhang #define SDMA1_HYP_DEC_REG_OFFSET 0x20
61fef6e24cSHawking Zhang
62fef6e24cSHawking Zhang static void sdma_v5_0_set_ring_funcs(struct amdgpu_device *adev);
63fef6e24cSHawking Zhang static void sdma_v5_0_set_buffer_funcs(struct amdgpu_device *adev);
64fef6e24cSHawking Zhang static void sdma_v5_0_set_vm_pte_funcs(struct amdgpu_device *adev);
65fef6e24cSHawking Zhang static void sdma_v5_0_set_irq_funcs(struct amdgpu_device *adev);
66fef6e24cSHawking Zhang
67fef6e24cSHawking Zhang static const struct soc15_reg_golden golden_settings_sdma_5[] = {
68fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_CHICKEN_BITS, 0xffbf1f0f, 0x03ab0107),
69fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_GFX_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
70fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_PAGE_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
71fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC0_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
72fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC1_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
73fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC2_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
74a994b742Stiancyin SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
75fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC4_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
76fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC5_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
77fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC6_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
78fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC7_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
79fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_UTCL1_PAGE, 0x00ffffff, 0x000c5c00),
80fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_CHICKEN_BITS, 0xffbf1f0f, 0x03ab0107),
81fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_GFX_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
82fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_PAGE_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
83fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC0_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
84fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC1_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
85fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC2_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
86a994b742Stiancyin SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
87fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC4_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
88fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC5_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
89fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC6_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
90fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC7_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
91fef6e24cSHawking Zhang SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_UTCL1_PAGE, 0x00ffffff, 0x000c5c00)
92fef6e24cSHawking Zhang };
93fef6e24cSHawking Zhang
9417e137f2SYintian Tao static const struct soc15_reg_golden golden_settings_sdma_5_sriov[] = {
9517e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_GFX_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
9617e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_PAGE_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
9717e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC0_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
9817e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC1_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
9917e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC2_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10017e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10117e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC4_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10217e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC5_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10317e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC6_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10417e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC7_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10517e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_GFX_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10617e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_PAGE_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10717e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC0_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10817e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC1_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
10917e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC2_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
11017e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
11117e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC4_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
11217e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC5_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
11317e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC6_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
11417e137f2SYintian Tao SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC7_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
11517e137f2SYintian Tao };
11617e137f2SYintian Tao
117fef6e24cSHawking Zhang static const struct soc15_reg_golden golden_settings_sdma_nv10[] = {
118c049af3eSXiaojie Yuan SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC3_RB_WPTR_POLL_CNTL, 0x0000fff0, 0x00403000),
119c049af3eSXiaojie Yuan SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC3_RB_WPTR_POLL_CNTL, 0x0000fff0, 0x00403000),
120fef6e24cSHawking Zhang };
121fef6e24cSHawking Zhang
12206823925SXiaojie Yuan static const struct soc15_reg_golden golden_settings_sdma_nv14[] = {
123c049af3eSXiaojie Yuan SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
124c049af3eSXiaojie Yuan SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
12506823925SXiaojie Yuan };
12606823925SXiaojie Yuan
127f2d6731dSXiaojie Yuan static const struct soc15_reg_golden golden_settings_sdma_nv12[] = {
128c726fbf0SXiaojie Yuan SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
12987476d12SGuchun Chen SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_GB_ADDR_CONFIG, 0x001877ff, 0x00000044),
13087476d12SGuchun Chen SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_GB_ADDR_CONFIG_READ, 0x001877ff, 0x00000044),
13187476d12SGuchun Chen SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_GB_ADDR_CONFIG, 0x001877ff, 0x00000044),
13287476d12SGuchun Chen SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_GB_ADDR_CONFIG_READ, 0x001877ff, 0x00000044),
133c726fbf0SXiaojie Yuan SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
134f2d6731dSXiaojie Yuan };
135f2d6731dSXiaojie Yuan
13686491ff7STao Zhou static const struct soc15_reg_golden golden_settings_sdma_cyan_skillfish[] = {
13786491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_CHICKEN_BITS, 0xffbf1f0f, 0x03ab0107),
13886491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_GB_ADDR_CONFIG, 0x001877ff, 0x00000044),
13986491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_GB_ADDR_CONFIG_READ, 0x001877ff, 0x00000044),
14086491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_GFX_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14186491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_PAGE_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14286491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC0_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14386491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC1_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14486491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC2_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14586491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14686491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC4_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14786491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC5_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14886491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC6_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
14986491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_RLC7_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
15086491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA0_UTCL1_PAGE, 0x007fffff, 0x004c5c00),
15186491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_CHICKEN_BITS, 0xffbf1f0f, 0x03ab0107),
15286491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_GB_ADDR_CONFIG, 0x001877ff, 0x00000044),
15386491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_GB_ADDR_CONFIG_READ, 0x001877ff, 0x00000044),
15486491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_GFX_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
15586491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_PAGE_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
15686491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC0_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
15786491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC1_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
15886491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC2_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
15986491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC3_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
16086491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC4_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
16186491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC5_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
16286491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC6_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
16386491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_RLC7_RB_WPTR_POLL_CNTL, 0xfffffff7, 0x00403000),
16486491ff7STao Zhou SOC15_REG_GOLDEN_VALUE(GC, 0, mmSDMA1_UTCL1_PAGE, 0x007fffff, 0x004c5c00)
16586491ff7STao Zhou };
16686491ff7STao Zhou
sdma_v5_0_get_reg_offset(struct amdgpu_device * adev,u32 instance,u32 internal_offset)167fef6e24cSHawking Zhang static u32 sdma_v5_0_get_reg_offset(struct amdgpu_device *adev, u32 instance, u32 internal_offset)
168fef6e24cSHawking Zhang {
169fef6e24cSHawking Zhang u32 base;
170fef6e24cSHawking Zhang
171fef6e24cSHawking Zhang if (internal_offset >= SDMA0_HYP_DEC_REG_START &&
172fef6e24cSHawking Zhang internal_offset <= SDMA0_HYP_DEC_REG_END) {
173fef6e24cSHawking Zhang base = adev->reg_offset[GC_HWIP][0][1];
174fef6e24cSHawking Zhang if (instance == 1)
175fef6e24cSHawking Zhang internal_offset += SDMA1_HYP_DEC_REG_OFFSET;
176fef6e24cSHawking Zhang } else {
177fef6e24cSHawking Zhang base = adev->reg_offset[GC_HWIP][0][0];
178fef6e24cSHawking Zhang if (instance == 1)
179fef6e24cSHawking Zhang internal_offset += SDMA1_REG_OFFSET;
180fef6e24cSHawking Zhang }
181fef6e24cSHawking Zhang
182fef6e24cSHawking Zhang return base + internal_offset;
183fef6e24cSHawking Zhang }
184fef6e24cSHawking Zhang
sdma_v5_0_init_golden_registers(struct amdgpu_device * adev)185fef6e24cSHawking Zhang static void sdma_v5_0_init_golden_registers(struct amdgpu_device *adev)
186fef6e24cSHawking Zhang {
1871d789535SAlex Deucher switch (adev->ip_versions[SDMA0_HWIP][0]) {
18802200e91SAlex Deucher case IP_VERSION(5, 0, 0):
189fef6e24cSHawking Zhang soc15_program_register_sequence(adev,
190fef6e24cSHawking Zhang golden_settings_sdma_5,
191fef6e24cSHawking Zhang (const u32)ARRAY_SIZE(golden_settings_sdma_5));
192fef6e24cSHawking Zhang soc15_program_register_sequence(adev,
193fef6e24cSHawking Zhang golden_settings_sdma_nv10,
194fef6e24cSHawking Zhang (const u32)ARRAY_SIZE(golden_settings_sdma_nv10));
195fef6e24cSHawking Zhang break;
19602200e91SAlex Deucher case IP_VERSION(5, 0, 2):
19706823925SXiaojie Yuan soc15_program_register_sequence(adev,
19806823925SXiaojie Yuan golden_settings_sdma_5,
19906823925SXiaojie Yuan (const u32)ARRAY_SIZE(golden_settings_sdma_5));
20006823925SXiaojie Yuan soc15_program_register_sequence(adev,
20106823925SXiaojie Yuan golden_settings_sdma_nv14,
20206823925SXiaojie Yuan (const u32)ARRAY_SIZE(golden_settings_sdma_nv14));
20306823925SXiaojie Yuan break;
20402200e91SAlex Deucher case IP_VERSION(5, 0, 5):
20517e137f2SYintian Tao if (amdgpu_sriov_vf(adev))
20617e137f2SYintian Tao soc15_program_register_sequence(adev,
20717e137f2SYintian Tao golden_settings_sdma_5_sriov,
20817e137f2SYintian Tao (const u32)ARRAY_SIZE(golden_settings_sdma_5_sriov));
20917e137f2SYintian Tao else
210f2d6731dSXiaojie Yuan soc15_program_register_sequence(adev,
211f2d6731dSXiaojie Yuan golden_settings_sdma_5,
212f2d6731dSXiaojie Yuan (const u32)ARRAY_SIZE(golden_settings_sdma_5));
213f2d6731dSXiaojie Yuan soc15_program_register_sequence(adev,
214f2d6731dSXiaojie Yuan golden_settings_sdma_nv12,
215f2d6731dSXiaojie Yuan (const u32)ARRAY_SIZE(golden_settings_sdma_nv12));
216f2d6731dSXiaojie Yuan break;
21702200e91SAlex Deucher case IP_VERSION(5, 0, 1):
21886491ff7STao Zhou soc15_program_register_sequence(adev,
21986491ff7STao Zhou golden_settings_sdma_cyan_skillfish,
22086491ff7STao Zhou (const u32)ARRAY_SIZE(golden_settings_sdma_cyan_skillfish));
22186491ff7STao Zhou break;
222fef6e24cSHawking Zhang default:
223fef6e24cSHawking Zhang break;
224fef6e24cSHawking Zhang }
225fef6e24cSHawking Zhang }
226fef6e24cSHawking Zhang
227fef6e24cSHawking Zhang /**
228fef6e24cSHawking Zhang * sdma_v5_0_init_microcode - load ucode images from disk
229fef6e24cSHawking Zhang *
230fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
231fef6e24cSHawking Zhang *
232fef6e24cSHawking Zhang * Use the firmware interface to load the ucode images into
233fef6e24cSHawking Zhang * the driver (not loaded into hw).
234fef6e24cSHawking Zhang * Returns 0 on success, error on failure.
235fef6e24cSHawking Zhang */
236fef6e24cSHawking Zhang
237fef6e24cSHawking Zhang // emulation only, won't work on real chip
238fef6e24cSHawking Zhang // navi10 real chip need to use PSP to load firmware
sdma_v5_0_init_microcode(struct amdgpu_device * adev)239fef6e24cSHawking Zhang static int sdma_v5_0_init_microcode(struct amdgpu_device *adev)
240*30b59910SYuanShang {
241*30b59910SYuanShang int ret, i;
242162b786fSJingwen Chen
243fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
2441336b4e7SMario Limonciello ret = amdgpu_sdma_init_microcode(adev, i, false);
245108db8deSLikun Gao if (ret)
246108db8deSLikun Gao return ret;
247108db8deSLikun Gao }
248fef6e24cSHawking Zhang
249108db8deSLikun Gao return ret;
250fef6e24cSHawking Zhang }
251fef6e24cSHawking Zhang
sdma_v5_0_ring_init_cond_exec(struct amdgpu_ring * ring)252fef6e24cSHawking Zhang static unsigned sdma_v5_0_ring_init_cond_exec(struct amdgpu_ring *ring)
253fef6e24cSHawking Zhang {
254fef6e24cSHawking Zhang unsigned ret;
255fef6e24cSHawking Zhang
256fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_COND_EXE));
257fef6e24cSHawking Zhang amdgpu_ring_write(ring, lower_32_bits(ring->cond_exe_gpu_addr));
258fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(ring->cond_exe_gpu_addr));
259fef6e24cSHawking Zhang amdgpu_ring_write(ring, 1);
260fef6e24cSHawking Zhang ret = ring->wptr & ring->buf_mask;/* this is the offset we need patch later */
261fef6e24cSHawking Zhang amdgpu_ring_write(ring, 0x55aa55aa);/* insert dummy here and patch it later */
262fef6e24cSHawking Zhang
263fef6e24cSHawking Zhang return ret;
264fef6e24cSHawking Zhang }
265fef6e24cSHawking Zhang
sdma_v5_0_ring_patch_cond_exec(struct amdgpu_ring * ring,unsigned offset)266fef6e24cSHawking Zhang static void sdma_v5_0_ring_patch_cond_exec(struct amdgpu_ring *ring,
267fef6e24cSHawking Zhang unsigned offset)
268fef6e24cSHawking Zhang {
269fef6e24cSHawking Zhang unsigned cur;
270fef6e24cSHawking Zhang
271fef6e24cSHawking Zhang BUG_ON(offset > ring->buf_mask);
272fef6e24cSHawking Zhang BUG_ON(ring->ring[offset] != 0x55aa55aa);
273fef6e24cSHawking Zhang
274fef6e24cSHawking Zhang cur = (ring->wptr - 1) & ring->buf_mask;
275fef6e24cSHawking Zhang if (cur > offset)
276fef6e24cSHawking Zhang ring->ring[offset] = cur - offset;
277fef6e24cSHawking Zhang else
278fef6e24cSHawking Zhang ring->ring[offset] = (ring->buf_mask + 1) - offset + cur;
279fef6e24cSHawking Zhang }
280fef6e24cSHawking Zhang
281fef6e24cSHawking Zhang /**
282fef6e24cSHawking Zhang * sdma_v5_0_ring_get_rptr - get the current read pointer
283fef6e24cSHawking Zhang *
284fef6e24cSHawking Zhang * @ring: amdgpu ring pointer
285fef6e24cSHawking Zhang *
286fef6e24cSHawking Zhang * Get the current rptr from the hardware (NAVI10+).
287fef6e24cSHawking Zhang */
sdma_v5_0_ring_get_rptr(struct amdgpu_ring * ring)288fef6e24cSHawking Zhang static uint64_t sdma_v5_0_ring_get_rptr(struct amdgpu_ring *ring)
289fef6e24cSHawking Zhang {
290fef6e24cSHawking Zhang u64 *rptr;
291fef6e24cSHawking Zhang
292fef6e24cSHawking Zhang /* XXX check if swapping is necessary on BE */
2933748424bSJack Xiao rptr = (u64 *)ring->rptr_cpu_addr;
294fef6e24cSHawking Zhang
295fef6e24cSHawking Zhang DRM_DEBUG("rptr before shift == 0x%016llx\n", *rptr);
296fef6e24cSHawking Zhang return ((*rptr) >> 2);
297fef6e24cSHawking Zhang }
298fef6e24cSHawking Zhang
299fef6e24cSHawking Zhang /**
300fef6e24cSHawking Zhang * sdma_v5_0_ring_get_wptr - get the current write pointer
301fef6e24cSHawking Zhang *
302fef6e24cSHawking Zhang * @ring: amdgpu ring pointer
303fef6e24cSHawking Zhang *
304fef6e24cSHawking Zhang * Get the current wptr from the hardware (NAVI10+).
305fef6e24cSHawking Zhang */
sdma_v5_0_ring_get_wptr(struct amdgpu_ring * ring)306fef6e24cSHawking Zhang static uint64_t sdma_v5_0_ring_get_wptr(struct amdgpu_ring *ring)
307fef6e24cSHawking Zhang {
308fef6e24cSHawking Zhang struct amdgpu_device *adev = ring->adev;
30987d6883bSXiaojie Yuan u64 wptr;
310fef6e24cSHawking Zhang
311fef6e24cSHawking Zhang if (ring->use_doorbell) {
312fef6e24cSHawking Zhang /* XXX check if swapping is necessary on BE */
3133748424bSJack Xiao wptr = READ_ONCE(*((u64 *)ring->wptr_cpu_addr));
31487d6883bSXiaojie Yuan DRM_DEBUG("wptr/doorbell before shift == 0x%016llx\n", wptr);
315fef6e24cSHawking Zhang } else {
3167373fc5eSPeng Ju Zhou wptr = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, ring->me, mmSDMA0_GFX_RB_WPTR_HI));
31787d6883bSXiaojie Yuan wptr = wptr << 32;
3187373fc5eSPeng Ju Zhou wptr |= RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, ring->me, mmSDMA0_GFX_RB_WPTR));
31987d6883bSXiaojie Yuan DRM_DEBUG("wptr before shift [%i] wptr == 0x%016llx\n", ring->me, wptr);
320fef6e24cSHawking Zhang }
321fef6e24cSHawking Zhang
32287d6883bSXiaojie Yuan return wptr >> 2;
323fef6e24cSHawking Zhang }
324fef6e24cSHawking Zhang
325fef6e24cSHawking Zhang /**
326fef6e24cSHawking Zhang * sdma_v5_0_ring_set_wptr - commit the write pointer
327fef6e24cSHawking Zhang *
328fef6e24cSHawking Zhang * @ring: amdgpu ring pointer
329fef6e24cSHawking Zhang *
330fef6e24cSHawking Zhang * Write the wptr back to the hardware (NAVI10+).
331fef6e24cSHawking Zhang */
sdma_v5_0_ring_set_wptr(struct amdgpu_ring * ring)332fef6e24cSHawking Zhang static void sdma_v5_0_ring_set_wptr(struct amdgpu_ring *ring)
333fef6e24cSHawking Zhang {
334fef6e24cSHawking Zhang struct amdgpu_device *adev = ring->adev;
3352d7a1f71SLe Ma uint32_t *wptr_saved;
3362d7a1f71SLe Ma uint32_t *is_queue_unmap;
3372d7a1f71SLe Ma uint64_t aggregated_db_index;
3382d7a1f71SLe Ma uint32_t mqd_size = adev->mqds[AMDGPU_HW_IP_DMA].mqd_size;
339fef6e24cSHawking Zhang
340fef6e24cSHawking Zhang DRM_DEBUG("Setting write pointer\n");
3412d7a1f71SLe Ma if (ring->is_mes_queue) {
3422d7a1f71SLe Ma wptr_saved = (uint32_t *)(ring->mqd_ptr + mqd_size);
3432d7a1f71SLe Ma is_queue_unmap = (uint32_t *)(ring->mqd_ptr + mqd_size +
3442d7a1f71SLe Ma sizeof(uint32_t));
3452d7a1f71SLe Ma aggregated_db_index =
3462d7a1f71SLe Ma amdgpu_mes_get_aggregated_doorbell_index(adev,
3472d7a1f71SLe Ma AMDGPU_MES_PRIORITY_LEVEL_NORMAL);
3482d7a1f71SLe Ma
3492d7a1f71SLe Ma atomic64_set((atomic64_t *)ring->wptr_cpu_addr,
3502d7a1f71SLe Ma ring->wptr << 2);
3512d7a1f71SLe Ma *wptr_saved = ring->wptr << 2;
3522d7a1f71SLe Ma if (*is_queue_unmap) {
3532d7a1f71SLe Ma WDOORBELL64(aggregated_db_index, ring->wptr << 2);
3542d7a1f71SLe Ma DRM_DEBUG("calling WDOORBELL64(0x%08x, 0x%016llx)\n",
3552d7a1f71SLe Ma ring->doorbell_index, ring->wptr << 2);
3562d7a1f71SLe Ma WDOORBELL64(ring->doorbell_index, ring->wptr << 2);
3572d7a1f71SLe Ma } else {
3582d7a1f71SLe Ma DRM_DEBUG("calling WDOORBELL64(0x%08x, 0x%016llx)\n",
3592d7a1f71SLe Ma ring->doorbell_index, ring->wptr << 2);
3602d7a1f71SLe Ma WDOORBELL64(ring->doorbell_index, ring->wptr << 2);
3612d7a1f71SLe Ma
3622d7a1f71SLe Ma if (*is_queue_unmap)
3632d7a1f71SLe Ma WDOORBELL64(aggregated_db_index,
3642d7a1f71SLe Ma ring->wptr << 2);
3652d7a1f71SLe Ma }
3662d7a1f71SLe Ma } else {
367fef6e24cSHawking Zhang if (ring->use_doorbell) {
368fef6e24cSHawking Zhang DRM_DEBUG("Using doorbell -- "
369fef6e24cSHawking Zhang "wptr_offs == 0x%08x "
3702d7a1f71SLe Ma "lower_32_bits(ring->wptr) << 2 == 0x%08x "
3712d7a1f71SLe Ma "upper_32_bits(ring->wptr) << 2 == 0x%08x\n",
372fef6e24cSHawking Zhang ring->wptr_offs,
373fef6e24cSHawking Zhang lower_32_bits(ring->wptr << 2),
374fef6e24cSHawking Zhang upper_32_bits(ring->wptr << 2));
375fef6e24cSHawking Zhang /* XXX check if swapping is necessary on BE */
3763748424bSJack Xiao atomic64_set((atomic64_t *)ring->wptr_cpu_addr,
3773748424bSJack Xiao ring->wptr << 2);
378fef6e24cSHawking Zhang DRM_DEBUG("calling WDOORBELL64(0x%08x, 0x%016llx)\n",
379fef6e24cSHawking Zhang ring->doorbell_index, ring->wptr << 2);
380fef6e24cSHawking Zhang WDOORBELL64(ring->doorbell_index, ring->wptr << 2);
381fef6e24cSHawking Zhang } else {
382fef6e24cSHawking Zhang DRM_DEBUG("Not using doorbell -- "
383fef6e24cSHawking Zhang "mmSDMA%i_GFX_RB_WPTR == 0x%08x "
384fef6e24cSHawking Zhang "mmSDMA%i_GFX_RB_WPTR_HI == 0x%08x\n",
385fef6e24cSHawking Zhang ring->me,
386fef6e24cSHawking Zhang lower_32_bits(ring->wptr << 2),
387fef6e24cSHawking Zhang ring->me,
388fef6e24cSHawking Zhang upper_32_bits(ring->wptr << 2));
3892d7a1f71SLe Ma WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev,
3902d7a1f71SLe Ma ring->me, mmSDMA0_GFX_RB_WPTR),
391fef6e24cSHawking Zhang lower_32_bits(ring->wptr << 2));
3922d7a1f71SLe Ma WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev,
3932d7a1f71SLe Ma ring->me, mmSDMA0_GFX_RB_WPTR_HI),
394fef6e24cSHawking Zhang upper_32_bits(ring->wptr << 2));
395fef6e24cSHawking Zhang }
396fef6e24cSHawking Zhang }
3972d7a1f71SLe Ma }
398fef6e24cSHawking Zhang
sdma_v5_0_ring_insert_nop(struct amdgpu_ring * ring,uint32_t count)399fef6e24cSHawking Zhang static void sdma_v5_0_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count)
400fef6e24cSHawking Zhang {
401fef6e24cSHawking Zhang struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
402fef6e24cSHawking Zhang int i;
403fef6e24cSHawking Zhang
404fef6e24cSHawking Zhang for (i = 0; i < count; i++)
405fef6e24cSHawking Zhang if (sdma && sdma->burst_nop && (i == 0))
406fef6e24cSHawking Zhang amdgpu_ring_write(ring, ring->funcs->nop |
407fef6e24cSHawking Zhang SDMA_PKT_NOP_HEADER_COUNT(count - 1));
408fef6e24cSHawking Zhang else
409fef6e24cSHawking Zhang amdgpu_ring_write(ring, ring->funcs->nop);
410fef6e24cSHawking Zhang }
411fef6e24cSHawking Zhang
412fef6e24cSHawking Zhang /**
413fef6e24cSHawking Zhang * sdma_v5_0_ring_emit_ib - Schedule an IB on the DMA engine
414fef6e24cSHawking Zhang *
415fef6e24cSHawking Zhang * @ring: amdgpu ring pointer
416b4234aecSLee Jones * @job: job to retrieve vmid from
417fef6e24cSHawking Zhang * @ib: IB object to schedule
418b4234aecSLee Jones * @flags: unused
419fef6e24cSHawking Zhang *
420fef6e24cSHawking Zhang * Schedule an IB in the DMA ring (NAVI10).
421fef6e24cSHawking Zhang */
sdma_v5_0_ring_emit_ib(struct amdgpu_ring * ring,struct amdgpu_job * job,struct amdgpu_ib * ib,uint32_t flags)422fef6e24cSHawking Zhang static void sdma_v5_0_ring_emit_ib(struct amdgpu_ring *ring,
423fef6e24cSHawking Zhang struct amdgpu_job *job,
424fef6e24cSHawking Zhang struct amdgpu_ib *ib,
425fef6e24cSHawking Zhang uint32_t flags)
426fef6e24cSHawking Zhang {
427fef6e24cSHawking Zhang unsigned vmid = AMDGPU_JOB_GET_VMID(job);
428fef6e24cSHawking Zhang uint64_t csa_mc_addr = amdgpu_sdma_get_csa_mc_addr(ring, vmid);
429fef6e24cSHawking Zhang
430ce73516dSLuben Tuikov /* An IB packet must end on a 8 DW boundary--the next dword
431ce73516dSLuben Tuikov * must be on a 8-dword boundary. Our IB packet below is 6
432ce73516dSLuben Tuikov * dwords long, thus add x number of NOPs, such that, in
433ce73516dSLuben Tuikov * modular arithmetic,
434ce73516dSLuben Tuikov * wptr + 6 + x = 8k, k >= 0, which in C is,
435ce73516dSLuben Tuikov * (wptr + 6 + x) % 8 = 0.
436ce73516dSLuben Tuikov * The expression below, is a solution of x.
437ce73516dSLuben Tuikov */
438ce73516dSLuben Tuikov sdma_v5_0_ring_insert_nop(ring, (2 - lower_32_bits(ring->wptr)) & 7);
439fef6e24cSHawking Zhang
440fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_INDIRECT) |
441fef6e24cSHawking Zhang SDMA_PKT_INDIRECT_HEADER_VMID(vmid & 0xf));
442fef6e24cSHawking Zhang /* base must be 32 byte aligned */
443fef6e24cSHawking Zhang amdgpu_ring_write(ring, lower_32_bits(ib->gpu_addr) & 0xffffffe0);
444fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(ib->gpu_addr));
445fef6e24cSHawking Zhang amdgpu_ring_write(ring, ib->length_dw);
446fef6e24cSHawking Zhang amdgpu_ring_write(ring, lower_32_bits(csa_mc_addr));
447fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(csa_mc_addr));
448fef6e24cSHawking Zhang }
449fef6e24cSHawking Zhang
450fef6e24cSHawking Zhang /**
451e8d7aa68SAlex Deucher * sdma_v5_0_ring_emit_mem_sync - flush the IB by graphics cache rinse
452e8d7aa68SAlex Deucher *
453e8d7aa68SAlex Deucher * @ring: amdgpu ring pointer
454e8d7aa68SAlex Deucher *
455e8d7aa68SAlex Deucher * flush the IB by graphics cache rinse.
456e8d7aa68SAlex Deucher */
sdma_v5_0_ring_emit_mem_sync(struct amdgpu_ring * ring)457e8d7aa68SAlex Deucher static void sdma_v5_0_ring_emit_mem_sync(struct amdgpu_ring *ring)
458e8d7aa68SAlex Deucher {
459f43ae2d1SJiapeng Chong uint32_t gcr_cntl = SDMA_GCR_GL2_INV | SDMA_GCR_GL2_WB | SDMA_GCR_GLM_INV |
460e8d7aa68SAlex Deucher SDMA_GCR_GL1_INV | SDMA_GCR_GLV_INV | SDMA_GCR_GLK_INV |
461e8d7aa68SAlex Deucher SDMA_GCR_GLI_INV(1);
462e8d7aa68SAlex Deucher
463e8d7aa68SAlex Deucher /* flush entire cache L0/L1/L2, this can be optimized by performance requirement */
464e8d7aa68SAlex Deucher amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_GCR_REQ));
465e8d7aa68SAlex Deucher amdgpu_ring_write(ring, SDMA_PKT_GCR_REQ_PAYLOAD1_BASE_VA_31_7(0));
466e8d7aa68SAlex Deucher amdgpu_ring_write(ring, SDMA_PKT_GCR_REQ_PAYLOAD2_GCR_CONTROL_15_0(gcr_cntl) |
467e8d7aa68SAlex Deucher SDMA_PKT_GCR_REQ_PAYLOAD2_BASE_VA_47_32(0));
468e8d7aa68SAlex Deucher amdgpu_ring_write(ring, SDMA_PKT_GCR_REQ_PAYLOAD3_LIMIT_VA_31_7(0) |
469e8d7aa68SAlex Deucher SDMA_PKT_GCR_REQ_PAYLOAD3_GCR_CONTROL_18_16(gcr_cntl >> 16));
470e8d7aa68SAlex Deucher amdgpu_ring_write(ring, SDMA_PKT_GCR_REQ_PAYLOAD4_LIMIT_VA_47_32(0) |
471e8d7aa68SAlex Deucher SDMA_PKT_GCR_REQ_PAYLOAD4_VMID(0));
472e8d7aa68SAlex Deucher }
473e8d7aa68SAlex Deucher
474e8d7aa68SAlex Deucher /**
475fef6e24cSHawking Zhang * sdma_v5_0_ring_emit_hdp_flush - emit an hdp flush on the DMA ring
476fef6e24cSHawking Zhang *
477fef6e24cSHawking Zhang * @ring: amdgpu ring pointer
478fef6e24cSHawking Zhang *
479fef6e24cSHawking Zhang * Emit an hdp flush packet on the requested DMA ring.
480fef6e24cSHawking Zhang */
sdma_v5_0_ring_emit_hdp_flush(struct amdgpu_ring * ring)481fef6e24cSHawking Zhang static void sdma_v5_0_ring_emit_hdp_flush(struct amdgpu_ring *ring)
482fef6e24cSHawking Zhang {
483fef6e24cSHawking Zhang struct amdgpu_device *adev = ring->adev;
484fef6e24cSHawking Zhang u32 ref_and_mask = 0;
485bebc0762SHawking Zhang const struct nbio_hdp_flush_reg *nbio_hf_reg = adev->nbio.hdp_flush_reg;
486fef6e24cSHawking Zhang
487fef6e24cSHawking Zhang if (ring->me == 0)
488fef6e24cSHawking Zhang ref_and_mask = nbio_hf_reg->ref_and_mask_sdma0;
489fef6e24cSHawking Zhang else
490fef6e24cSHawking Zhang ref_and_mask = nbio_hf_reg->ref_and_mask_sdma1;
491fef6e24cSHawking Zhang
492fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
493fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(1) |
494fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3)); /* == */
495bebc0762SHawking Zhang amdgpu_ring_write(ring, (adev->nbio.funcs->get_hdp_flush_done_offset(adev)) << 2);
496bebc0762SHawking Zhang amdgpu_ring_write(ring, (adev->nbio.funcs->get_hdp_flush_req_offset(adev)) << 2);
497fef6e24cSHawking Zhang amdgpu_ring_write(ring, ref_and_mask); /* reference */
498fef6e24cSHawking Zhang amdgpu_ring_write(ring, ref_and_mask); /* mask */
499fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
500fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10)); /* retry count, poll interval */
501fef6e24cSHawking Zhang }
502fef6e24cSHawking Zhang
503fef6e24cSHawking Zhang /**
504fef6e24cSHawking Zhang * sdma_v5_0_ring_emit_fence - emit a fence on the DMA ring
505fef6e24cSHawking Zhang *
506fef6e24cSHawking Zhang * @ring: amdgpu ring pointer
507b4234aecSLee Jones * @addr: address
508b4234aecSLee Jones * @seq: sequence number
509b4234aecSLee Jones * @flags: fence related flags
510fef6e24cSHawking Zhang *
511fef6e24cSHawking Zhang * Add a DMA fence packet to the ring to write
512fef6e24cSHawking Zhang * the fence seq number and DMA trap packet to generate
513fef6e24cSHawking Zhang * an interrupt if needed (NAVI10).
514fef6e24cSHawking Zhang */
sdma_v5_0_ring_emit_fence(struct amdgpu_ring * ring,u64 addr,u64 seq,unsigned flags)515fef6e24cSHawking Zhang static void sdma_v5_0_ring_emit_fence(struct amdgpu_ring *ring, u64 addr, u64 seq,
516fef6e24cSHawking Zhang unsigned flags)
517fef6e24cSHawking Zhang {
518fef6e24cSHawking Zhang bool write64bit = flags & AMDGPU_FENCE_FLAG_64BIT;
519fef6e24cSHawking Zhang /* write the fence */
520fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE) |
521fef6e24cSHawking Zhang SDMA_PKT_FENCE_HEADER_MTYPE(0x3)); /* Ucached(UC) */
522fef6e24cSHawking Zhang /* zero in first two bits */
523fef6e24cSHawking Zhang BUG_ON(addr & 0x3);
524fef6e24cSHawking Zhang amdgpu_ring_write(ring, lower_32_bits(addr));
525fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(addr));
526fef6e24cSHawking Zhang amdgpu_ring_write(ring, lower_32_bits(seq));
527fef6e24cSHawking Zhang
528fef6e24cSHawking Zhang /* optionally write high bits as well */
529fef6e24cSHawking Zhang if (write64bit) {
530fef6e24cSHawking Zhang addr += 4;
531fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_FENCE) |
532fef6e24cSHawking Zhang SDMA_PKT_FENCE_HEADER_MTYPE(0x3));
533fef6e24cSHawking Zhang /* zero in first two bits */
534fef6e24cSHawking Zhang BUG_ON(addr & 0x3);
535fef6e24cSHawking Zhang amdgpu_ring_write(ring, lower_32_bits(addr));
536fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(addr));
537fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(seq));
538fef6e24cSHawking Zhang }
539fef6e24cSHawking Zhang
5401cb63593SLikun Gao if (flags & AMDGPU_FENCE_FLAG_INT) {
541217d29f1SJack Xiao uint32_t ctx = ring->is_mes_queue ?
542217d29f1SJack Xiao (ring->hw_queue_id | AMDGPU_FENCE_MES_QUEUE_FLAG) : 0;
543fef6e24cSHawking Zhang /* generate an interrupt */
544fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_TRAP));
545217d29f1SJack Xiao amdgpu_ring_write(ring, SDMA_PKT_TRAP_INT_CONTEXT_INT_CONTEXT(ctx));
546fef6e24cSHawking Zhang }
547fef6e24cSHawking Zhang }
548fef6e24cSHawking Zhang
549fef6e24cSHawking Zhang
550fef6e24cSHawking Zhang /**
551fef6e24cSHawking Zhang * sdma_v5_0_gfx_stop - stop the gfx async dma engines
552fef6e24cSHawking Zhang *
553fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
554fef6e24cSHawking Zhang *
555fef6e24cSHawking Zhang * Stop the gfx async dma ring buffers (NAVI10).
556fef6e24cSHawking Zhang */
sdma_v5_0_gfx_stop(struct amdgpu_device * adev)557fef6e24cSHawking Zhang static void sdma_v5_0_gfx_stop(struct amdgpu_device *adev)
558fef6e24cSHawking Zhang {
559fef6e24cSHawking Zhang u32 rb_cntl, ib_cntl;
560fef6e24cSHawking Zhang int i;
561fef6e24cSHawking Zhang
562571c0536SAlex Deucher amdgpu_sdma_unset_buffer_funcs_helper(adev);
563fef6e24cSHawking Zhang
564fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
5657373fc5eSPeng Ju Zhou rb_cntl = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL));
566fef6e24cSHawking Zhang rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 0);
5677373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL), rb_cntl);
5687373fc5eSPeng Ju Zhou ib_cntl = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL));
569fef6e24cSHawking Zhang ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 0);
5707373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL), ib_cntl);
571fef6e24cSHawking Zhang }
572fef6e24cSHawking Zhang }
573fef6e24cSHawking Zhang
574fef6e24cSHawking Zhang /**
575fef6e24cSHawking Zhang * sdma_v5_0_rlc_stop - stop the compute async dma engines
576fef6e24cSHawking Zhang *
577fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
578fef6e24cSHawking Zhang *
579fef6e24cSHawking Zhang * Stop the compute async dma queues (NAVI10).
580fef6e24cSHawking Zhang */
sdma_v5_0_rlc_stop(struct amdgpu_device * adev)581fef6e24cSHawking Zhang static void sdma_v5_0_rlc_stop(struct amdgpu_device *adev)
582fef6e24cSHawking Zhang {
583fef6e24cSHawking Zhang /* XXX todo */
584fef6e24cSHawking Zhang }
585fef6e24cSHawking Zhang
586fef6e24cSHawking Zhang /**
5878d55be74SLee Jones * sdma_v5_0_ctx_switch_enable - stop the async dma engines context switch
588fef6e24cSHawking Zhang *
589fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
590fef6e24cSHawking Zhang * @enable: enable/disable the DMA MEs context switch.
591fef6e24cSHawking Zhang *
592fef6e24cSHawking Zhang * Halt or unhalt the async dma engines context switch (NAVI10).
593fef6e24cSHawking Zhang */
sdma_v5_0_ctx_switch_enable(struct amdgpu_device * adev,bool enable)594fef6e24cSHawking Zhang static void sdma_v5_0_ctx_switch_enable(struct amdgpu_device *adev, bool enable)
595fef6e24cSHawking Zhang {
59617e137f2SYintian Tao u32 f32_cntl = 0, phase_quantum = 0;
597fef6e24cSHawking Zhang int i;
598fef6e24cSHawking Zhang
599fef6e24cSHawking Zhang if (amdgpu_sdma_phase_quantum) {
600fef6e24cSHawking Zhang unsigned value = amdgpu_sdma_phase_quantum;
601fef6e24cSHawking Zhang unsigned unit = 0;
602fef6e24cSHawking Zhang
603fef6e24cSHawking Zhang while (value > (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
604fef6e24cSHawking Zhang SDMA0_PHASE0_QUANTUM__VALUE__SHIFT)) {
605fef6e24cSHawking Zhang value = (value + 1) >> 1;
606fef6e24cSHawking Zhang unit++;
607fef6e24cSHawking Zhang }
608fef6e24cSHawking Zhang if (unit > (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
609fef6e24cSHawking Zhang SDMA0_PHASE0_QUANTUM__UNIT__SHIFT)) {
610fef6e24cSHawking Zhang value = (SDMA0_PHASE0_QUANTUM__VALUE_MASK >>
611fef6e24cSHawking Zhang SDMA0_PHASE0_QUANTUM__VALUE__SHIFT);
612fef6e24cSHawking Zhang unit = (SDMA0_PHASE0_QUANTUM__UNIT_MASK >>
613fef6e24cSHawking Zhang SDMA0_PHASE0_QUANTUM__UNIT__SHIFT);
614fef6e24cSHawking Zhang WARN_ONCE(1,
615fef6e24cSHawking Zhang "clamping sdma_phase_quantum to %uK clock cycles\n",
616fef6e24cSHawking Zhang value << unit);
617fef6e24cSHawking Zhang }
618fef6e24cSHawking Zhang phase_quantum =
619fef6e24cSHawking Zhang value << SDMA0_PHASE0_QUANTUM__VALUE__SHIFT |
620fef6e24cSHawking Zhang unit << SDMA0_PHASE0_QUANTUM__UNIT__SHIFT;
621fef6e24cSHawking Zhang }
622fef6e24cSHawking Zhang
623fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
62417e137f2SYintian Tao if (!amdgpu_sriov_vf(adev)) {
625fef6e24cSHawking Zhang f32_cntl = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CNTL));
626fef6e24cSHawking Zhang f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_CNTL,
627fef6e24cSHawking Zhang AUTO_CTXSW_ENABLE, enable ? 1 : 0);
62817e137f2SYintian Tao }
62917e137f2SYintian Tao
630fef6e24cSHawking Zhang if (enable && amdgpu_sdma_phase_quantum) {
6317373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_PHASE0_QUANTUM),
632fef6e24cSHawking Zhang phase_quantum);
6337373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_PHASE1_QUANTUM),
634fef6e24cSHawking Zhang phase_quantum);
6357373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_PHASE2_QUANTUM),
636fef6e24cSHawking Zhang phase_quantum);
637fef6e24cSHawking Zhang }
63817e137f2SYintian Tao if (!amdgpu_sriov_vf(adev))
639fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CNTL), f32_cntl);
640fef6e24cSHawking Zhang }
641fef6e24cSHawking Zhang
642fef6e24cSHawking Zhang }
643fef6e24cSHawking Zhang
644fef6e24cSHawking Zhang /**
645fef6e24cSHawking Zhang * sdma_v5_0_enable - stop the async dma engines
646fef6e24cSHawking Zhang *
647fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
648fef6e24cSHawking Zhang * @enable: enable/disable the DMA MEs.
649fef6e24cSHawking Zhang *
650fef6e24cSHawking Zhang * Halt or unhalt the async dma engines (NAVI10).
651fef6e24cSHawking Zhang */
sdma_v5_0_enable(struct amdgpu_device * adev,bool enable)652fef6e24cSHawking Zhang static void sdma_v5_0_enable(struct amdgpu_device *adev, bool enable)
653fef6e24cSHawking Zhang {
654fef6e24cSHawking Zhang u32 f32_cntl;
655fef6e24cSHawking Zhang int i;
656fef6e24cSHawking Zhang
657960a06ffSZheng Bin if (!enable) {
658fef6e24cSHawking Zhang sdma_v5_0_gfx_stop(adev);
659fef6e24cSHawking Zhang sdma_v5_0_rlc_stop(adev);
660fef6e24cSHawking Zhang }
661fef6e24cSHawking Zhang
66217e137f2SYintian Tao if (amdgpu_sriov_vf(adev))
66317e137f2SYintian Tao return;
66417e137f2SYintian Tao
665fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
666fef6e24cSHawking Zhang f32_cntl = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_F32_CNTL));
667fef6e24cSHawking Zhang f32_cntl = REG_SET_FIELD(f32_cntl, SDMA0_F32_CNTL, HALT, enable ? 0 : 1);
668fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_F32_CNTL), f32_cntl);
669fef6e24cSHawking Zhang }
670fef6e24cSHawking Zhang }
671fef6e24cSHawking Zhang
672fef6e24cSHawking Zhang /**
673fef6e24cSHawking Zhang * sdma_v5_0_gfx_resume - setup and start the async dma engines
674fef6e24cSHawking Zhang *
675fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
676fef6e24cSHawking Zhang *
677fef6e24cSHawking Zhang * Set up the gfx DMA ring buffers and enable them (NAVI10).
678fef6e24cSHawking Zhang * Returns 0 for success, error for failure.
679fef6e24cSHawking Zhang */
sdma_v5_0_gfx_resume(struct amdgpu_device * adev)680fef6e24cSHawking Zhang static int sdma_v5_0_gfx_resume(struct amdgpu_device *adev)
681fef6e24cSHawking Zhang {
682fef6e24cSHawking Zhang struct amdgpu_ring *ring;
683fef6e24cSHawking Zhang u32 rb_cntl, ib_cntl;
684fef6e24cSHawking Zhang u32 rb_bufsz;
685fef6e24cSHawking Zhang u32 doorbell;
686fef6e24cSHawking Zhang u32 doorbell_offset;
687fef6e24cSHawking Zhang u32 temp;
68837809f55SJack Xiao u32 wptr_poll_cntl;
68937809f55SJack Xiao u64 wptr_gpu_addr;
690fef6e24cSHawking Zhang int i, r;
691fef6e24cSHawking Zhang
692fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
693fef6e24cSHawking Zhang ring = &adev->sdma.instance[i].ring;
694fef6e24cSHawking Zhang
69517e137f2SYintian Tao if (!amdgpu_sriov_vf(adev))
696fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_SEM_WAIT_FAIL_TIMER_CNTL), 0);
697fef6e24cSHawking Zhang
698fef6e24cSHawking Zhang /* Set ring buffer size in dwords */
699fef6e24cSHawking Zhang rb_bufsz = order_base_2(ring->ring_size / 4);
7007373fc5eSPeng Ju Zhou rb_cntl = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL));
701fef6e24cSHawking Zhang rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SIZE, rb_bufsz);
702fef6e24cSHawking Zhang #ifdef __BIG_ENDIAN
703fef6e24cSHawking Zhang rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_SWAP_ENABLE, 1);
704fef6e24cSHawking Zhang rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL,
705fef6e24cSHawking Zhang RPTR_WRITEBACK_SWAP_ENABLE, 1);
706fef6e24cSHawking Zhang #endif
7077373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL), rb_cntl);
708fef6e24cSHawking Zhang
709fef6e24cSHawking Zhang /* Initialize the ring buffer's read and write pointers */
7107373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR), 0);
7117373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR_HI), 0);
7127373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR), 0);
7137373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_HI), 0);
714fef6e24cSHawking Zhang
715fef6e24cSHawking Zhang /* setup the wptr shadow polling */
7163748424bSJack Xiao wptr_gpu_addr = ring->wptr_gpu_addr;
7177373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_POLL_ADDR_LO),
718fef6e24cSHawking Zhang lower_32_bits(wptr_gpu_addr));
7197373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_POLL_ADDR_HI),
720fef6e24cSHawking Zhang upper_32_bits(wptr_gpu_addr));
7217373fc5eSPeng Ju Zhou wptr_poll_cntl = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i,
722fef6e24cSHawking Zhang mmSDMA0_GFX_RB_WPTR_POLL_CNTL));
723fef6e24cSHawking Zhang wptr_poll_cntl = REG_SET_FIELD(wptr_poll_cntl,
724fef6e24cSHawking Zhang SDMA0_GFX_RB_WPTR_POLL_CNTL,
725fef6e24cSHawking Zhang F32_POLL_ENABLE, 1);
7267373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_POLL_CNTL),
727fef6e24cSHawking Zhang wptr_poll_cntl);
728fef6e24cSHawking Zhang
729fef6e24cSHawking Zhang /* set the wb address whether it's enabled or not */
7307373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR_ADDR_HI),
7313748424bSJack Xiao upper_32_bits(ring->rptr_gpu_addr) & 0xFFFFFFFF);
7327373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_RPTR_ADDR_LO),
7333748424bSJack Xiao lower_32_bits(ring->rptr_gpu_addr) & 0xFFFFFFFC);
734fef6e24cSHawking Zhang
735fef6e24cSHawking Zhang rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RPTR_WRITEBACK_ENABLE, 1);
736fef6e24cSHawking Zhang
7377373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_BASE),
7387373fc5eSPeng Ju Zhou ring->gpu_addr >> 8);
7397373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_BASE_HI),
7407373fc5eSPeng Ju Zhou ring->gpu_addr >> 40);
741fef6e24cSHawking Zhang
742fef6e24cSHawking Zhang ring->wptr = 0;
743fef6e24cSHawking Zhang
744fef6e24cSHawking Zhang /* before programing wptr to a less value, need set minor_ptr_update first */
7457373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_MINOR_PTR_UPDATE), 1);
746fef6e24cSHawking Zhang
747fef6e24cSHawking Zhang if (!amdgpu_sriov_vf(adev)) { /* only bare-metal use register write for wptr */
7487373fc5eSPeng Ju Zhou WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR),
7497dba6e83SHaohui Mai lower_32_bits(ring->wptr << 2));
7507373fc5eSPeng Ju Zhou WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_WPTR_HI),
7517dba6e83SHaohui Mai upper_32_bits(ring->wptr << 2));
752fef6e24cSHawking Zhang }
753fef6e24cSHawking Zhang
7547373fc5eSPeng Ju Zhou doorbell = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_DOORBELL));
7557373fc5eSPeng Ju Zhou doorbell_offset = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i,
7567373fc5eSPeng Ju Zhou mmSDMA0_GFX_DOORBELL_OFFSET));
757fef6e24cSHawking Zhang
758fef6e24cSHawking Zhang if (ring->use_doorbell) {
759fef6e24cSHawking Zhang doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 1);
760fef6e24cSHawking Zhang doorbell_offset = REG_SET_FIELD(doorbell_offset, SDMA0_GFX_DOORBELL_OFFSET,
761fef6e24cSHawking Zhang OFFSET, ring->doorbell_index);
762fef6e24cSHawking Zhang } else {
763fef6e24cSHawking Zhang doorbell = REG_SET_FIELD(doorbell, SDMA0_GFX_DOORBELL, ENABLE, 0);
764fef6e24cSHawking Zhang }
7657373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_DOORBELL), doorbell);
7667373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_DOORBELL_OFFSET),
7677373fc5eSPeng Ju Zhou doorbell_offset);
768fef6e24cSHawking Zhang
769bebc0762SHawking Zhang adev->nbio.funcs->sdma_doorbell_range(adev, i, ring->use_doorbell,
770fef6e24cSHawking Zhang ring->doorbell_index, 20);
771fef6e24cSHawking Zhang
772fef6e24cSHawking Zhang if (amdgpu_sriov_vf(adev))
773fef6e24cSHawking Zhang sdma_v5_0_ring_set_wptr(ring);
774fef6e24cSHawking Zhang
775fef6e24cSHawking Zhang /* set minor_ptr_update to 0 after wptr programed */
7767373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_MINOR_PTR_UPDATE), 0);
777fef6e24cSHawking Zhang
77817e137f2SYintian Tao if (!amdgpu_sriov_vf(adev)) {
779fef6e24cSHawking Zhang /* set utc l1 enable flag always to 1 */
780fef6e24cSHawking Zhang temp = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CNTL));
781fef6e24cSHawking Zhang temp = REG_SET_FIELD(temp, SDMA0_CNTL, UTC_L1_ENABLE, 1);
782fef6e24cSHawking Zhang
783fef6e24cSHawking Zhang /* enable MCBP */
784fef6e24cSHawking Zhang temp = REG_SET_FIELD(temp, SDMA0_CNTL, MIDCMD_PREEMPT_ENABLE, 1);
785fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CNTL), temp);
786fef6e24cSHawking Zhang
787fef6e24cSHawking Zhang /* Set up RESP_MODE to non-copy addresses */
788fef6e24cSHawking Zhang temp = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_UTCL1_CNTL));
789278b6fbaStiancyin temp = REG_SET_FIELD(temp, SDMA0_UTCL1_CNTL, RESP_MODE, 3);
790278b6fbaStiancyin temp = REG_SET_FIELD(temp, SDMA0_UTCL1_CNTL, REDO_DELAY, 9);
791fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_UTCL1_CNTL), temp);
792fef6e24cSHawking Zhang
793fef6e24cSHawking Zhang /* program default cache read and write policy */
794fef6e24cSHawking Zhang temp = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_UTCL1_PAGE));
795fef6e24cSHawking Zhang /* clean read policy and write policy bits */
796fef6e24cSHawking Zhang temp &= 0xFF0FFF;
797fef6e24cSHawking Zhang temp |= ((CACHE_READ_POLICY_L2__DEFAULT << 12) | (CACHE_WRITE_POLICY_L2__DEFAULT << 14));
798fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_UTCL1_PAGE), temp);
79917e137f2SYintian Tao }
800fef6e24cSHawking Zhang
801fef6e24cSHawking Zhang if (!amdgpu_sriov_vf(adev)) {
802fef6e24cSHawking Zhang /* unhalt engine */
803fef6e24cSHawking Zhang temp = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_F32_CNTL));
804fef6e24cSHawking Zhang temp = REG_SET_FIELD(temp, SDMA0_F32_CNTL, HALT, 0);
805fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_F32_CNTL), temp);
806fef6e24cSHawking Zhang }
807fef6e24cSHawking Zhang
808fef6e24cSHawking Zhang /* enable DMA RB */
809fef6e24cSHawking Zhang rb_cntl = REG_SET_FIELD(rb_cntl, SDMA0_GFX_RB_CNTL, RB_ENABLE, 1);
8107373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_RB_CNTL), rb_cntl);
811fef6e24cSHawking Zhang
8127373fc5eSPeng Ju Zhou ib_cntl = RREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL));
813fef6e24cSHawking Zhang ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_ENABLE, 1);
814fef6e24cSHawking Zhang #ifdef __BIG_ENDIAN
815fef6e24cSHawking Zhang ib_cntl = REG_SET_FIELD(ib_cntl, SDMA0_GFX_IB_CNTL, IB_SWAP_ENABLE, 1);
816fef6e24cSHawking Zhang #endif
817fef6e24cSHawking Zhang /* enable DMA IBs */
8187373fc5eSPeng Ju Zhou WREG32_SOC15_IP(GC, sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_GFX_IB_CNTL), ib_cntl);
819fef6e24cSHawking Zhang
820fef6e24cSHawking Zhang if (amdgpu_sriov_vf(adev)) { /* bare-metal sequence doesn't need below to lines */
821fef6e24cSHawking Zhang sdma_v5_0_ctx_switch_enable(adev, true);
822fef6e24cSHawking Zhang sdma_v5_0_enable(adev, true);
823fef6e24cSHawking Zhang }
824fef6e24cSHawking Zhang
825c6fc97f9SNirmoy Das r = amdgpu_ring_test_helper(ring);
826c6fc97f9SNirmoy Das if (r)
827fef6e24cSHawking Zhang return r;
828fef6e24cSHawking Zhang
829fef6e24cSHawking Zhang if (adev->mman.buffer_funcs_ring == ring)
830fef6e24cSHawking Zhang amdgpu_ttm_set_buffer_funcs_status(adev, true);
831fef6e24cSHawking Zhang }
832fef6e24cSHawking Zhang
833fef6e24cSHawking Zhang return 0;
834fef6e24cSHawking Zhang }
835fef6e24cSHawking Zhang
836fef6e24cSHawking Zhang /**
837fef6e24cSHawking Zhang * sdma_v5_0_rlc_resume - setup and start the async dma engines
838fef6e24cSHawking Zhang *
839fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
840fef6e24cSHawking Zhang *
841fef6e24cSHawking Zhang * Set up the compute DMA queues and enable them (NAVI10).
842fef6e24cSHawking Zhang * Returns 0 for success, error for failure.
843fef6e24cSHawking Zhang */
sdma_v5_0_rlc_resume(struct amdgpu_device * adev)844fef6e24cSHawking Zhang static int sdma_v5_0_rlc_resume(struct amdgpu_device *adev)
845fef6e24cSHawking Zhang {
846fef6e24cSHawking Zhang return 0;
847fef6e24cSHawking Zhang }
848fef6e24cSHawking Zhang
849fef6e24cSHawking Zhang /**
850fef6e24cSHawking Zhang * sdma_v5_0_load_microcode - load the sDMA ME ucode
851fef6e24cSHawking Zhang *
852fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
853fef6e24cSHawking Zhang *
854fef6e24cSHawking Zhang * Loads the sDMA0/1 ucode.
855fef6e24cSHawking Zhang * Returns 0 for success, -EINVAL if the ucode is not available.
856fef6e24cSHawking Zhang */
sdma_v5_0_load_microcode(struct amdgpu_device * adev)857fef6e24cSHawking Zhang static int sdma_v5_0_load_microcode(struct amdgpu_device *adev)
858fef6e24cSHawking Zhang {
859fef6e24cSHawking Zhang const struct sdma_firmware_header_v1_0 *hdr;
860fef6e24cSHawking Zhang const __le32 *fw_data;
861fef6e24cSHawking Zhang u32 fw_size;
862fef6e24cSHawking Zhang int i, j;
863fef6e24cSHawking Zhang
864fef6e24cSHawking Zhang /* halt the MEs */
865fef6e24cSHawking Zhang sdma_v5_0_enable(adev, false);
866fef6e24cSHawking Zhang
867fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
868fef6e24cSHawking Zhang if (!adev->sdma.instance[i].fw)
869fef6e24cSHawking Zhang return -EINVAL;
870fef6e24cSHawking Zhang
871fef6e24cSHawking Zhang hdr = (const struct sdma_firmware_header_v1_0 *)adev->sdma.instance[i].fw->data;
872fef6e24cSHawking Zhang amdgpu_ucode_print_sdma_hdr(&hdr->header);
873fef6e24cSHawking Zhang fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
874fef6e24cSHawking Zhang
875fef6e24cSHawking Zhang fw_data = (const __le32 *)
876fef6e24cSHawking Zhang (adev->sdma.instance[i].fw->data +
877fef6e24cSHawking Zhang le32_to_cpu(hdr->header.ucode_array_offset_bytes));
878fef6e24cSHawking Zhang
879fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_UCODE_ADDR), 0);
880fef6e24cSHawking Zhang
881fef6e24cSHawking Zhang for (j = 0; j < fw_size; j++) {
882fef6e24cSHawking Zhang if (amdgpu_emu_mode == 1 && j % 500 == 0)
883fef6e24cSHawking Zhang msleep(1);
884fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_UCODE_DATA), le32_to_cpup(fw_data++));
885fef6e24cSHawking Zhang }
886fef6e24cSHawking Zhang
887fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_UCODE_ADDR), adev->sdma.instance[i].fw_version);
888fef6e24cSHawking Zhang }
889fef6e24cSHawking Zhang
890fef6e24cSHawking Zhang return 0;
891fef6e24cSHawking Zhang }
892fef6e24cSHawking Zhang
893fef6e24cSHawking Zhang /**
894fef6e24cSHawking Zhang * sdma_v5_0_start - setup and start the async dma engines
895fef6e24cSHawking Zhang *
896fef6e24cSHawking Zhang * @adev: amdgpu_device pointer
897fef6e24cSHawking Zhang *
898fef6e24cSHawking Zhang * Set up the DMA engines and enable them (NAVI10).
899fef6e24cSHawking Zhang * Returns 0 for success, error for failure.
900fef6e24cSHawking Zhang */
sdma_v5_0_start(struct amdgpu_device * adev)901fef6e24cSHawking Zhang static int sdma_v5_0_start(struct amdgpu_device *adev)
902fef6e24cSHawking Zhang {
903fef6e24cSHawking Zhang int r = 0;
904fef6e24cSHawking Zhang
905fef6e24cSHawking Zhang if (amdgpu_sriov_vf(adev)) {
906fef6e24cSHawking Zhang sdma_v5_0_ctx_switch_enable(adev, false);
907fef6e24cSHawking Zhang sdma_v5_0_enable(adev, false);
908fef6e24cSHawking Zhang
909fef6e24cSHawking Zhang /* set RB registers */
910fef6e24cSHawking Zhang r = sdma_v5_0_gfx_resume(adev);
911fef6e24cSHawking Zhang return r;
912fef6e24cSHawking Zhang }
913fef6e24cSHawking Zhang
914fef6e24cSHawking Zhang if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
915fef6e24cSHawking Zhang r = sdma_v5_0_load_microcode(adev);
916fef6e24cSHawking Zhang if (r)
917fef6e24cSHawking Zhang return r;
918fef6e24cSHawking Zhang }
919fef6e24cSHawking Zhang
920fef6e24cSHawking Zhang /* unhalt the MEs */
921fef6e24cSHawking Zhang sdma_v5_0_enable(adev, true);
922fef6e24cSHawking Zhang /* enable sdma ring preemption */
923fef6e24cSHawking Zhang sdma_v5_0_ctx_switch_enable(adev, true);
924fef6e24cSHawking Zhang
925fef6e24cSHawking Zhang /* start the gfx rings and rlc compute queues */
926fef6e24cSHawking Zhang r = sdma_v5_0_gfx_resume(adev);
927fef6e24cSHawking Zhang if (r)
928fef6e24cSHawking Zhang return r;
929fef6e24cSHawking Zhang r = sdma_v5_0_rlc_resume(adev);
930fef6e24cSHawking Zhang
931fef6e24cSHawking Zhang return r;
932fef6e24cSHawking Zhang }
933fef6e24cSHawking Zhang
sdma_v5_0_mqd_init(struct amdgpu_device * adev,void * mqd,struct amdgpu_mqd_prop * prop)934810479baSJack Xiao static int sdma_v5_0_mqd_init(struct amdgpu_device *adev, void *mqd,
935810479baSJack Xiao struct amdgpu_mqd_prop *prop)
936810479baSJack Xiao {
937810479baSJack Xiao struct v10_sdma_mqd *m = mqd;
938810479baSJack Xiao uint64_t wb_gpu_addr;
939810479baSJack Xiao
940810479baSJack Xiao m->sdmax_rlcx_rb_cntl =
941810479baSJack Xiao order_base_2(prop->queue_size / 4) << SDMA0_RLC0_RB_CNTL__RB_SIZE__SHIFT |
942810479baSJack Xiao 1 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT |
943810479baSJack Xiao 6 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT |
944810479baSJack Xiao 1 << SDMA0_RLC0_RB_CNTL__RB_PRIV__SHIFT;
945810479baSJack Xiao
946810479baSJack Xiao m->sdmax_rlcx_rb_base = lower_32_bits(prop->hqd_base_gpu_addr >> 8);
947810479baSJack Xiao m->sdmax_rlcx_rb_base_hi = upper_32_bits(prop->hqd_base_gpu_addr >> 8);
948810479baSJack Xiao
949810479baSJack Xiao m->sdmax_rlcx_rb_wptr_poll_cntl = RREG32(sdma_v5_0_get_reg_offset(adev, 0,
950810479baSJack Xiao mmSDMA0_GFX_RB_WPTR_POLL_CNTL));
951810479baSJack Xiao
952810479baSJack Xiao wb_gpu_addr = prop->wptr_gpu_addr;
953810479baSJack Xiao m->sdmax_rlcx_rb_wptr_poll_addr_lo = lower_32_bits(wb_gpu_addr);
954810479baSJack Xiao m->sdmax_rlcx_rb_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr);
955810479baSJack Xiao
956810479baSJack Xiao wb_gpu_addr = prop->rptr_gpu_addr;
957810479baSJack Xiao m->sdmax_rlcx_rb_rptr_addr_lo = lower_32_bits(wb_gpu_addr);
958810479baSJack Xiao m->sdmax_rlcx_rb_rptr_addr_hi = upper_32_bits(wb_gpu_addr);
959810479baSJack Xiao
960810479baSJack Xiao m->sdmax_rlcx_ib_cntl = RREG32(sdma_v5_0_get_reg_offset(adev, 0,
961810479baSJack Xiao mmSDMA0_GFX_IB_CNTL));
962810479baSJack Xiao
963810479baSJack Xiao m->sdmax_rlcx_doorbell_offset =
964810479baSJack Xiao prop->doorbell_index << SDMA0_RLC0_DOORBELL_OFFSET__OFFSET__SHIFT;
965810479baSJack Xiao
966810479baSJack Xiao m->sdmax_rlcx_doorbell = REG_SET_FIELD(0, SDMA0_RLC0_DOORBELL, ENABLE, 1);
967810479baSJack Xiao
968810479baSJack Xiao return 0;
969810479baSJack Xiao }
970810479baSJack Xiao
sdma_v5_0_set_mqd_funcs(struct amdgpu_device * adev)971810479baSJack Xiao static void sdma_v5_0_set_mqd_funcs(struct amdgpu_device *adev)
972810479baSJack Xiao {
973810479baSJack Xiao adev->mqds[AMDGPU_HW_IP_DMA].mqd_size = sizeof(struct v10_sdma_mqd);
974810479baSJack Xiao adev->mqds[AMDGPU_HW_IP_DMA].init_mqd = sdma_v5_0_mqd_init;
975810479baSJack Xiao }
976810479baSJack Xiao
977fef6e24cSHawking Zhang /**
978fef6e24cSHawking Zhang * sdma_v5_0_ring_test_ring - simple async dma engine test
979fef6e24cSHawking Zhang *
980fef6e24cSHawking Zhang * @ring: amdgpu_ring structure holding ring information
981fef6e24cSHawking Zhang *
982fef6e24cSHawking Zhang * Test the DMA engine by writing using it to write an
983fef6e24cSHawking Zhang * value to memory. (NAVI10).
984fef6e24cSHawking Zhang * Returns 0 for success, error for failure.
985fef6e24cSHawking Zhang */
sdma_v5_0_ring_test_ring(struct amdgpu_ring * ring)986fef6e24cSHawking Zhang static int sdma_v5_0_ring_test_ring(struct amdgpu_ring *ring)
987fef6e24cSHawking Zhang {
988fef6e24cSHawking Zhang struct amdgpu_device *adev = ring->adev;
989fef6e24cSHawking Zhang unsigned i;
990fef6e24cSHawking Zhang unsigned index;
991fef6e24cSHawking Zhang int r;
992fef6e24cSHawking Zhang u32 tmp;
993fef6e24cSHawking Zhang u64 gpu_addr;
994ea93ac2fSJack Xiao volatile uint32_t *cpu_ptr = NULL;
995fef6e24cSHawking Zhang
996ea93ac2fSJack Xiao tmp = 0xCAFEDEAD;
997ea93ac2fSJack Xiao
998ea93ac2fSJack Xiao if (ring->is_mes_queue) {
999ea93ac2fSJack Xiao uint32_t offset = 0;
1000ea93ac2fSJack Xiao offset = amdgpu_mes_ctx_get_offs(ring,
1001ea93ac2fSJack Xiao AMDGPU_MES_CTX_PADDING_OFFS);
1002ea93ac2fSJack Xiao gpu_addr = amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
1003ea93ac2fSJack Xiao cpu_ptr = amdgpu_mes_ctx_get_offs_cpu_addr(ring, offset);
1004ea93ac2fSJack Xiao *cpu_ptr = tmp;
1005ea93ac2fSJack Xiao } else {
1006fef6e24cSHawking Zhang r = amdgpu_device_wb_get(adev, &index);
1007fef6e24cSHawking Zhang if (r) {
1008fef6e24cSHawking Zhang dev_err(adev->dev, "(%d) failed to allocate wb slot\n", r);
1009fef6e24cSHawking Zhang return r;
1010fef6e24cSHawking Zhang }
1011fef6e24cSHawking Zhang
1012fef6e24cSHawking Zhang gpu_addr = adev->wb.gpu_addr + (index * 4);
1013fef6e24cSHawking Zhang adev->wb.wb[index] = cpu_to_le32(tmp);
1014ea93ac2fSJack Xiao }
1015fef6e24cSHawking Zhang
1016ea93ac2fSJack Xiao r = amdgpu_ring_alloc(ring, 20);
1017fef6e24cSHawking Zhang if (r) {
1018fef6e24cSHawking Zhang DRM_ERROR("amdgpu: dma failed to lock ring %d (%d).\n", ring->idx, r);
1019fef6e24cSHawking Zhang amdgpu_device_wb_free(adev, index);
1020fef6e24cSHawking Zhang return r;
1021fef6e24cSHawking Zhang }
1022fef6e24cSHawking Zhang
1023fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
1024fef6e24cSHawking Zhang SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR));
1025fef6e24cSHawking Zhang amdgpu_ring_write(ring, lower_32_bits(gpu_addr));
1026fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(gpu_addr));
1027fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(0));
1028fef6e24cSHawking Zhang amdgpu_ring_write(ring, 0xDEADBEEF);
1029fef6e24cSHawking Zhang amdgpu_ring_commit(ring);
1030fef6e24cSHawking Zhang
1031fef6e24cSHawking Zhang for (i = 0; i < adev->usec_timeout; i++) {
1032ea93ac2fSJack Xiao if (ring->is_mes_queue)
1033ea93ac2fSJack Xiao tmp = le32_to_cpu(*cpu_ptr);
1034ea93ac2fSJack Xiao else
1035fef6e24cSHawking Zhang tmp = le32_to_cpu(adev->wb.wb[index]);
1036fef6e24cSHawking Zhang if (tmp == 0xDEADBEEF)
1037fef6e24cSHawking Zhang break;
1038fef6e24cSHawking Zhang if (amdgpu_emu_mode == 1)
1039fef6e24cSHawking Zhang msleep(1);
1040fef6e24cSHawking Zhang else
104175589f49SAlex Deucher udelay(1);
1042fef6e24cSHawking Zhang }
1043fef6e24cSHawking Zhang
104442a9938eSAlex Deucher if (i >= adev->usec_timeout)
104542a9938eSAlex Deucher r = -ETIMEDOUT;
104642a9938eSAlex Deucher
1047ea93ac2fSJack Xiao if (!ring->is_mes_queue)
1048fef6e24cSHawking Zhang amdgpu_device_wb_free(adev, index);
1049fef6e24cSHawking Zhang
1050fef6e24cSHawking Zhang return r;
1051fef6e24cSHawking Zhang }
1052fef6e24cSHawking Zhang
1053fef6e24cSHawking Zhang /**
1054fef6e24cSHawking Zhang * sdma_v5_0_ring_test_ib - test an IB on the DMA engine
1055fef6e24cSHawking Zhang *
1056fef6e24cSHawking Zhang * @ring: amdgpu_ring structure holding ring information
1057b4234aecSLee Jones * @timeout: timeout value in jiffies, or MAX_SCHEDULE_TIMEOUT
1058fef6e24cSHawking Zhang *
1059fef6e24cSHawking Zhang * Test a simple IB in the DMA ring (NAVI10).
1060fef6e24cSHawking Zhang * Returns 0 on success, error on failure.
1061fef6e24cSHawking Zhang */
sdma_v5_0_ring_test_ib(struct amdgpu_ring * ring,long timeout)1062fef6e24cSHawking Zhang static int sdma_v5_0_ring_test_ib(struct amdgpu_ring *ring, long timeout)
1063fef6e24cSHawking Zhang {
1064fef6e24cSHawking Zhang struct amdgpu_device *adev = ring->adev;
1065fef6e24cSHawking Zhang struct amdgpu_ib ib;
1066fef6e24cSHawking Zhang struct dma_fence *f = NULL;
1067fef6e24cSHawking Zhang unsigned index;
1068fef6e24cSHawking Zhang long r;
1069fef6e24cSHawking Zhang u32 tmp = 0;
1070fef6e24cSHawking Zhang u64 gpu_addr;
10718183d743SJack Xiao volatile uint32_t *cpu_ptr = NULL;
1072fef6e24cSHawking Zhang
10738183d743SJack Xiao tmp = 0xCAFEDEAD;
10748183d743SJack Xiao memset(&ib, 0, sizeof(ib));
10758183d743SJack Xiao
10768183d743SJack Xiao if (ring->is_mes_queue) {
10778183d743SJack Xiao uint32_t offset = 0;
10788183d743SJack Xiao offset = amdgpu_mes_ctx_get_offs(ring, AMDGPU_MES_CTX_IB_OFFS);
10798183d743SJack Xiao ib.gpu_addr = amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
10808183d743SJack Xiao ib.ptr = (void *)amdgpu_mes_ctx_get_offs_cpu_addr(ring, offset);
10818183d743SJack Xiao
10828183d743SJack Xiao offset = amdgpu_mes_ctx_get_offs(ring,
10838183d743SJack Xiao AMDGPU_MES_CTX_PADDING_OFFS);
10848183d743SJack Xiao gpu_addr = amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset);
10858183d743SJack Xiao cpu_ptr = amdgpu_mes_ctx_get_offs_cpu_addr(ring, offset);
10868183d743SJack Xiao *cpu_ptr = tmp;
10878183d743SJack Xiao } else {
1088fef6e24cSHawking Zhang r = amdgpu_device_wb_get(adev, &index);
1089fef6e24cSHawking Zhang if (r) {
1090fef6e24cSHawking Zhang dev_err(adev->dev, "(%ld) failed to allocate wb slot\n", r);
1091fef6e24cSHawking Zhang return r;
1092fef6e24cSHawking Zhang }
1093fef6e24cSHawking Zhang
1094fef6e24cSHawking Zhang gpu_addr = adev->wb.gpu_addr + (index * 4);
1095fef6e24cSHawking Zhang adev->wb.wb[index] = cpu_to_le32(tmp);
10968183d743SJack Xiao
1097c8e42d57Sxinhui pan r = amdgpu_ib_get(adev, NULL, 256,
1098c8e42d57Sxinhui pan AMDGPU_IB_POOL_DIRECT, &ib);
1099fef6e24cSHawking Zhang if (r) {
1100fef6e24cSHawking Zhang DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
1101fef6e24cSHawking Zhang goto err0;
1102fef6e24cSHawking Zhang }
11038183d743SJack Xiao }
1104fef6e24cSHawking Zhang
1105fef6e24cSHawking Zhang ib.ptr[0] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
1106fef6e24cSHawking Zhang SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
1107fef6e24cSHawking Zhang ib.ptr[1] = lower_32_bits(gpu_addr);
1108fef6e24cSHawking Zhang ib.ptr[2] = upper_32_bits(gpu_addr);
1109fef6e24cSHawking Zhang ib.ptr[3] = SDMA_PKT_WRITE_UNTILED_DW_3_COUNT(0);
1110fef6e24cSHawking Zhang ib.ptr[4] = 0xDEADBEEF;
1111fef6e24cSHawking Zhang ib.ptr[5] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
1112fef6e24cSHawking Zhang ib.ptr[6] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
1113fef6e24cSHawking Zhang ib.ptr[7] = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP);
1114fef6e24cSHawking Zhang ib.length_dw = 8;
1115fef6e24cSHawking Zhang
1116fef6e24cSHawking Zhang r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
1117fef6e24cSHawking Zhang if (r)
1118fef6e24cSHawking Zhang goto err1;
1119fef6e24cSHawking Zhang
1120fef6e24cSHawking Zhang r = dma_fence_wait_timeout(f, false, timeout);
1121fef6e24cSHawking Zhang if (r == 0) {
1122fef6e24cSHawking Zhang DRM_ERROR("amdgpu: IB test timed out\n");
1123fef6e24cSHawking Zhang r = -ETIMEDOUT;
1124fef6e24cSHawking Zhang goto err1;
1125fef6e24cSHawking Zhang } else if (r < 0) {
1126fef6e24cSHawking Zhang DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
1127fef6e24cSHawking Zhang goto err1;
1128fef6e24cSHawking Zhang }
11298183d743SJack Xiao
11308183d743SJack Xiao if (ring->is_mes_queue)
11318183d743SJack Xiao tmp = le32_to_cpu(*cpu_ptr);
11328183d743SJack Xiao else
1133fef6e24cSHawking Zhang tmp = le32_to_cpu(adev->wb.wb[index]);
11348183d743SJack Xiao
113542a9938eSAlex Deucher if (tmp == 0xDEADBEEF)
1136fef6e24cSHawking Zhang r = 0;
113742a9938eSAlex Deucher else
1138fef6e24cSHawking Zhang r = -EINVAL;
1139fef6e24cSHawking Zhang
1140fef6e24cSHawking Zhang err1:
1141fef6e24cSHawking Zhang amdgpu_ib_free(adev, &ib, NULL);
1142fef6e24cSHawking Zhang dma_fence_put(f);
1143fef6e24cSHawking Zhang err0:
11448183d743SJack Xiao if (!ring->is_mes_queue)
1145fef6e24cSHawking Zhang amdgpu_device_wb_free(adev, index);
1146fef6e24cSHawking Zhang return r;
1147fef6e24cSHawking Zhang }
1148fef6e24cSHawking Zhang
1149fef6e24cSHawking Zhang
1150fef6e24cSHawking Zhang /**
1151fef6e24cSHawking Zhang * sdma_v5_0_vm_copy_pte - update PTEs by copying them from the GART
1152fef6e24cSHawking Zhang *
1153fef6e24cSHawking Zhang * @ib: indirect buffer to fill with commands
1154fef6e24cSHawking Zhang * @pe: addr of the page entry
1155fef6e24cSHawking Zhang * @src: src addr to copy from
1156fef6e24cSHawking Zhang * @count: number of page entries to update
1157fef6e24cSHawking Zhang *
1158fef6e24cSHawking Zhang * Update PTEs by copying them from the GART using sDMA (NAVI10).
1159fef6e24cSHawking Zhang */
sdma_v5_0_vm_copy_pte(struct amdgpu_ib * ib,uint64_t pe,uint64_t src,unsigned count)1160fef6e24cSHawking Zhang static void sdma_v5_0_vm_copy_pte(struct amdgpu_ib *ib,
1161fef6e24cSHawking Zhang uint64_t pe, uint64_t src,
1162fef6e24cSHawking Zhang unsigned count)
1163fef6e24cSHawking Zhang {
1164fef6e24cSHawking Zhang unsigned bytes = count * 8;
1165fef6e24cSHawking Zhang
1166fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
1167fef6e24cSHawking Zhang SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR);
1168fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = bytes - 1;
1169fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1170fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(src);
1171fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(src);
1172fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(pe);
1173fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1174fef6e24cSHawking Zhang
1175fef6e24cSHawking Zhang }
1176fef6e24cSHawking Zhang
1177fef6e24cSHawking Zhang /**
1178fef6e24cSHawking Zhang * sdma_v5_0_vm_write_pte - update PTEs by writing them manually
1179fef6e24cSHawking Zhang *
1180fef6e24cSHawking Zhang * @ib: indirect buffer to fill with commands
1181fef6e24cSHawking Zhang * @pe: addr of the page entry
1182b4234aecSLee Jones * @value: dst addr to write into pe
1183fef6e24cSHawking Zhang * @count: number of page entries to update
1184fef6e24cSHawking Zhang * @incr: increase next addr by incr bytes
1185fef6e24cSHawking Zhang *
1186fef6e24cSHawking Zhang * Update PTEs by writing them manually using sDMA (NAVI10).
1187fef6e24cSHawking Zhang */
sdma_v5_0_vm_write_pte(struct amdgpu_ib * ib,uint64_t pe,uint64_t value,unsigned count,uint32_t incr)1188fef6e24cSHawking Zhang static void sdma_v5_0_vm_write_pte(struct amdgpu_ib *ib, uint64_t pe,
1189fef6e24cSHawking Zhang uint64_t value, unsigned count,
1190fef6e24cSHawking Zhang uint32_t incr)
1191fef6e24cSHawking Zhang {
1192fef6e24cSHawking Zhang unsigned ndw = count * 2;
1193fef6e24cSHawking Zhang
1194fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_WRITE) |
1195fef6e24cSHawking Zhang SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_WRITE_LINEAR);
1196fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(pe);
1197fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1198fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = ndw - 1;
1199fef6e24cSHawking Zhang for (; ndw > 0; ndw -= 2) {
1200fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(value);
1201fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(value);
1202fef6e24cSHawking Zhang value += incr;
1203fef6e24cSHawking Zhang }
1204fef6e24cSHawking Zhang }
1205fef6e24cSHawking Zhang
1206fef6e24cSHawking Zhang /**
1207fef6e24cSHawking Zhang * sdma_v5_0_vm_set_pte_pde - update the page tables using sDMA
1208fef6e24cSHawking Zhang *
1209fef6e24cSHawking Zhang * @ib: indirect buffer to fill with commands
1210fef6e24cSHawking Zhang * @pe: addr of the page entry
1211fef6e24cSHawking Zhang * @addr: dst addr to write into pe
1212fef6e24cSHawking Zhang * @count: number of page entries to update
1213fef6e24cSHawking Zhang * @incr: increase next addr by incr bytes
1214fef6e24cSHawking Zhang * @flags: access flags
1215fef6e24cSHawking Zhang *
1216fef6e24cSHawking Zhang * Update the page tables using sDMA (NAVI10).
1217fef6e24cSHawking Zhang */
sdma_v5_0_vm_set_pte_pde(struct amdgpu_ib * ib,uint64_t pe,uint64_t addr,unsigned count,uint32_t incr,uint64_t flags)1218fef6e24cSHawking Zhang static void sdma_v5_0_vm_set_pte_pde(struct amdgpu_ib *ib,
1219fef6e24cSHawking Zhang uint64_t pe,
1220fef6e24cSHawking Zhang uint64_t addr, unsigned count,
1221fef6e24cSHawking Zhang uint32_t incr, uint64_t flags)
1222fef6e24cSHawking Zhang {
1223fef6e24cSHawking Zhang /* for physically contiguous pages (vram) */
1224fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_PTEPDE);
1225fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(pe); /* dst addr */
1226fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(pe);
1227fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(flags); /* mask */
1228fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(flags);
1229fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(addr); /* value */
1230fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(addr);
1231fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = incr; /* increment size */
1232fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = 0;
1233fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = count - 1; /* number of entries */
1234fef6e24cSHawking Zhang }
1235fef6e24cSHawking Zhang
1236fef6e24cSHawking Zhang /**
1237ce73516dSLuben Tuikov * sdma_v5_0_ring_pad_ib - pad the IB
1238b4234aecSLee Jones * @ring: amdgpu_ring structure holding ring information
1239fef6e24cSHawking Zhang * @ib: indirect buffer to fill with padding
1240fef6e24cSHawking Zhang *
1241ce73516dSLuben Tuikov * Pad the IB with NOPs to a boundary multiple of 8.
1242fef6e24cSHawking Zhang */
sdma_v5_0_ring_pad_ib(struct amdgpu_ring * ring,struct amdgpu_ib * ib)1243fef6e24cSHawking Zhang static void sdma_v5_0_ring_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib)
1244fef6e24cSHawking Zhang {
1245fef6e24cSHawking Zhang struct amdgpu_sdma_instance *sdma = amdgpu_sdma_get_instance_from_ring(ring);
1246fef6e24cSHawking Zhang u32 pad_count;
1247fef6e24cSHawking Zhang int i;
1248fef6e24cSHawking Zhang
1249ce73516dSLuben Tuikov pad_count = (-ib->length_dw) & 0x7;
1250fef6e24cSHawking Zhang for (i = 0; i < pad_count; i++)
1251fef6e24cSHawking Zhang if (sdma && sdma->burst_nop && (i == 0))
1252fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] =
1253fef6e24cSHawking Zhang SDMA_PKT_HEADER_OP(SDMA_OP_NOP) |
1254fef6e24cSHawking Zhang SDMA_PKT_NOP_HEADER_COUNT(pad_count - 1);
1255fef6e24cSHawking Zhang else
1256fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] =
1257fef6e24cSHawking Zhang SDMA_PKT_HEADER_OP(SDMA_OP_NOP);
1258fef6e24cSHawking Zhang }
1259fef6e24cSHawking Zhang
1260fef6e24cSHawking Zhang
1261fef6e24cSHawking Zhang /**
1262fef6e24cSHawking Zhang * sdma_v5_0_ring_emit_pipeline_sync - sync the pipeline
1263fef6e24cSHawking Zhang *
1264fef6e24cSHawking Zhang * @ring: amdgpu_ring pointer
1265fef6e24cSHawking Zhang *
1266fef6e24cSHawking Zhang * Make sure all previous operations are completed (CIK).
1267fef6e24cSHawking Zhang */
sdma_v5_0_ring_emit_pipeline_sync(struct amdgpu_ring * ring)1268fef6e24cSHawking Zhang static void sdma_v5_0_ring_emit_pipeline_sync(struct amdgpu_ring *ring)
1269fef6e24cSHawking Zhang {
1270fef6e24cSHawking Zhang uint32_t seq = ring->fence_drv.sync_seq;
1271fef6e24cSHawking Zhang uint64_t addr = ring->fence_drv.gpu_addr;
1272fef6e24cSHawking Zhang
1273fef6e24cSHawking Zhang /* wait for idle */
1274fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1275fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1276fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3) | /* equal */
1277fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_HEADER_MEM_POLL(1));
1278fef6e24cSHawking Zhang amdgpu_ring_write(ring, addr & 0xfffffffc);
1279fef6e24cSHawking Zhang amdgpu_ring_write(ring, upper_32_bits(addr) & 0xffffffff);
1280fef6e24cSHawking Zhang amdgpu_ring_write(ring, seq); /* reference */
1281d12c5085SXiaojie Yuan amdgpu_ring_write(ring, 0xffffffff); /* mask */
1282fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1283fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(4)); /* retry count, poll interval */
1284fef6e24cSHawking Zhang }
1285fef6e24cSHawking Zhang
1286fef6e24cSHawking Zhang
1287fef6e24cSHawking Zhang /**
1288fef6e24cSHawking Zhang * sdma_v5_0_ring_emit_vm_flush - vm flush using sDMA
1289fef6e24cSHawking Zhang *
1290fef6e24cSHawking Zhang * @ring: amdgpu_ring pointer
1291b4234aecSLee Jones * @vmid: vmid number to use
1292b4234aecSLee Jones * @pd_addr: address
1293fef6e24cSHawking Zhang *
1294fef6e24cSHawking Zhang * Update the page table base and flush the VM TLB
1295fef6e24cSHawking Zhang * using sDMA (NAVI10).
1296fef6e24cSHawking Zhang */
sdma_v5_0_ring_emit_vm_flush(struct amdgpu_ring * ring,unsigned vmid,uint64_t pd_addr)1297fef6e24cSHawking Zhang static void sdma_v5_0_ring_emit_vm_flush(struct amdgpu_ring *ring,
1298fef6e24cSHawking Zhang unsigned vmid, uint64_t pd_addr)
1299fef6e24cSHawking Zhang {
1300fef6e24cSHawking Zhang amdgpu_gmc_emit_flush_gpu_tlb(ring, vmid, pd_addr);
1301fef6e24cSHawking Zhang }
1302fef6e24cSHawking Zhang
sdma_v5_0_ring_emit_wreg(struct amdgpu_ring * ring,uint32_t reg,uint32_t val)1303fef6e24cSHawking Zhang static void sdma_v5_0_ring_emit_wreg(struct amdgpu_ring *ring,
1304fef6e24cSHawking Zhang uint32_t reg, uint32_t val)
1305fef6e24cSHawking Zhang {
1306fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_SRBM_WRITE) |
1307fef6e24cSHawking Zhang SDMA_PKT_SRBM_WRITE_HEADER_BYTE_EN(0xf));
1308fef6e24cSHawking Zhang amdgpu_ring_write(ring, reg);
1309fef6e24cSHawking Zhang amdgpu_ring_write(ring, val);
1310fef6e24cSHawking Zhang }
1311fef6e24cSHawking Zhang
sdma_v5_0_ring_emit_reg_wait(struct amdgpu_ring * ring,uint32_t reg,uint32_t val,uint32_t mask)1312fef6e24cSHawking Zhang static void sdma_v5_0_ring_emit_reg_wait(struct amdgpu_ring *ring, uint32_t reg,
1313fef6e24cSHawking Zhang uint32_t val, uint32_t mask)
1314fef6e24cSHawking Zhang {
1315fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_HEADER_OP(SDMA_OP_POLL_REGMEM) |
1316fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_HEADER_HDP_FLUSH(0) |
1317fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_HEADER_FUNC(3)); /* equal */
1318fef6e24cSHawking Zhang amdgpu_ring_write(ring, reg << 2);
1319fef6e24cSHawking Zhang amdgpu_ring_write(ring, 0);
1320fef6e24cSHawking Zhang amdgpu_ring_write(ring, val); /* reference */
1321fef6e24cSHawking Zhang amdgpu_ring_write(ring, mask); /* mask */
1322fef6e24cSHawking Zhang amdgpu_ring_write(ring, SDMA_PKT_POLL_REGMEM_DW5_RETRY_COUNT(0xfff) |
1323fef6e24cSHawking Zhang SDMA_PKT_POLL_REGMEM_DW5_INTERVAL(10));
1324fef6e24cSHawking Zhang }
1325fef6e24cSHawking Zhang
sdma_v5_0_ring_emit_reg_write_reg_wait(struct amdgpu_ring * ring,uint32_t reg0,uint32_t reg1,uint32_t ref,uint32_t mask)1326589b64a7Schangzhu static void sdma_v5_0_ring_emit_reg_write_reg_wait(struct amdgpu_ring *ring,
1327589b64a7Schangzhu uint32_t reg0, uint32_t reg1,
1328589b64a7Schangzhu uint32_t ref, uint32_t mask)
1329589b64a7Schangzhu {
1330589b64a7Schangzhu amdgpu_ring_emit_wreg(ring, reg0, ref);
1331589b64a7Schangzhu /* wait for a cycle to reset vm_inv_eng*_ack */
1332589b64a7Schangzhu amdgpu_ring_emit_reg_wait(ring, reg0, 0, 0);
1333589b64a7Schangzhu amdgpu_ring_emit_reg_wait(ring, reg1, mask, mask);
1334589b64a7Schangzhu }
1335589b64a7Schangzhu
sdma_v5_0_early_init(void * handle)1336fef6e24cSHawking Zhang static int sdma_v5_0_early_init(void *handle)
1337fef6e24cSHawking Zhang {
1338fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1339fef6e24cSHawking Zhang
1340fef6e24cSHawking Zhang sdma_v5_0_set_ring_funcs(adev);
1341fef6e24cSHawking Zhang sdma_v5_0_set_buffer_funcs(adev);
1342fef6e24cSHawking Zhang sdma_v5_0_set_vm_pte_funcs(adev);
1343fef6e24cSHawking Zhang sdma_v5_0_set_irq_funcs(adev);
1344810479baSJack Xiao sdma_v5_0_set_mqd_funcs(adev);
1345fef6e24cSHawking Zhang
1346fef6e24cSHawking Zhang return 0;
1347fef6e24cSHawking Zhang }
1348fef6e24cSHawking Zhang
1349fef6e24cSHawking Zhang
sdma_v5_0_sw_init(void * handle)1350fef6e24cSHawking Zhang static int sdma_v5_0_sw_init(void *handle)
1351fef6e24cSHawking Zhang {
1352fef6e24cSHawking Zhang struct amdgpu_ring *ring;
1353fef6e24cSHawking Zhang int r, i;
1354fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1355fef6e24cSHawking Zhang
1356fef6e24cSHawking Zhang /* SDMA trap event */
1357fef6e24cSHawking Zhang r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_SDMA0,
1358fef6e24cSHawking Zhang SDMA0_5_0__SRCID__SDMA_TRAP,
1359fef6e24cSHawking Zhang &adev->sdma.trap_irq);
1360fef6e24cSHawking Zhang if (r)
1361fef6e24cSHawking Zhang return r;
1362fef6e24cSHawking Zhang
1363fef6e24cSHawking Zhang /* SDMA trap event */
1364fef6e24cSHawking Zhang r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_SDMA1,
1365fef6e24cSHawking Zhang SDMA1_5_0__SRCID__SDMA_TRAP,
1366fef6e24cSHawking Zhang &adev->sdma.trap_irq);
1367fef6e24cSHawking Zhang if (r)
1368fef6e24cSHawking Zhang return r;
1369fef6e24cSHawking Zhang
1370fef6e24cSHawking Zhang r = sdma_v5_0_init_microcode(adev);
1371fef6e24cSHawking Zhang if (r) {
1372fef6e24cSHawking Zhang DRM_ERROR("Failed to load sdma firmware!\n");
1373fef6e24cSHawking Zhang return r;
1374fef6e24cSHawking Zhang }
1375fef6e24cSHawking Zhang
1376fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
1377fef6e24cSHawking Zhang ring = &adev->sdma.instance[i].ring;
1378fef6e24cSHawking Zhang ring->ring_obj = NULL;
1379fef6e24cSHawking Zhang ring->use_doorbell = true;
1380fef6e24cSHawking Zhang
1381e1046a1fSYong Zhao DRM_DEBUG("SDMA %d use_doorbell being set to: [%s]\n", i,
1382fef6e24cSHawking Zhang ring->use_doorbell?"true":"false");
1383fef6e24cSHawking Zhang
1384fef6e24cSHawking Zhang ring->doorbell_index = (i == 0) ?
1385fef6e24cSHawking Zhang (adev->doorbell_index.sdma_engine[0] << 1) //get DWORD offset
1386fef6e24cSHawking Zhang : (adev->doorbell_index.sdma_engine[1] << 1); // get DWORD offset
1387fef6e24cSHawking Zhang
1388f4caf584SHawking Zhang ring->vm_hub = AMDGPU_GFXHUB(0);
1389fef6e24cSHawking Zhang sprintf(ring->name, "sdma%d", i);
1390c107171bSChristian König r = amdgpu_ring_init(adev, ring, 1024, &adev->sdma.trap_irq,
1391c107171bSChristian König (i == 0) ? AMDGPU_SDMA_IRQ_INSTANCE0 :
13921c6d567bSNirmoy Das AMDGPU_SDMA_IRQ_INSTANCE1,
1393c107171bSChristian König AMDGPU_RING_PRIO_DEFAULT, NULL);
1394fef6e24cSHawking Zhang if (r)
1395fef6e24cSHawking Zhang return r;
1396fef6e24cSHawking Zhang }
1397fef6e24cSHawking Zhang
1398fef6e24cSHawking Zhang return r;
1399fef6e24cSHawking Zhang }
1400fef6e24cSHawking Zhang
sdma_v5_0_sw_fini(void * handle)1401fef6e24cSHawking Zhang static int sdma_v5_0_sw_fini(void *handle)
1402fef6e24cSHawking Zhang {
1403fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1404fef6e24cSHawking Zhang int i;
1405fef6e24cSHawking Zhang
1406108db8deSLikun Gao for (i = 0; i < adev->sdma.num_instances; i++)
1407fef6e24cSHawking Zhang amdgpu_ring_fini(&adev->sdma.instance[i].ring);
1408108db8deSLikun Gao
1409108db8deSLikun Gao amdgpu_sdma_destroy_inst_ctx(adev, false);
1410fef6e24cSHawking Zhang
1411fef6e24cSHawking Zhang return 0;
1412fef6e24cSHawking Zhang }
1413fef6e24cSHawking Zhang
sdma_v5_0_hw_init(void * handle)1414fef6e24cSHawking Zhang static int sdma_v5_0_hw_init(void *handle)
1415fef6e24cSHawking Zhang {
1416fef6e24cSHawking Zhang int r;
1417fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1418fef6e24cSHawking Zhang
1419fef6e24cSHawking Zhang sdma_v5_0_init_golden_registers(adev);
1420fef6e24cSHawking Zhang
1421fef6e24cSHawking Zhang r = sdma_v5_0_start(adev);
1422fef6e24cSHawking Zhang
1423fef6e24cSHawking Zhang return r;
1424fef6e24cSHawking Zhang }
1425fef6e24cSHawking Zhang
sdma_v5_0_hw_fini(void * handle)1426fef6e24cSHawking Zhang static int sdma_v5_0_hw_fini(void *handle)
1427fef6e24cSHawking Zhang {
1428fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1429fef6e24cSHawking Zhang
1430a98cec22SAlex Deucher if (amdgpu_sriov_vf(adev)) {
1431a98cec22SAlex Deucher /* disable the scheduler for SDMA */
1432a98cec22SAlex Deucher amdgpu_sdma_unset_buffer_funcs_helper(adev);
1433fef6e24cSHawking Zhang return 0;
1434a98cec22SAlex Deucher }
1435fef6e24cSHawking Zhang
1436fef6e24cSHawking Zhang sdma_v5_0_ctx_switch_enable(adev, false);
1437fef6e24cSHawking Zhang sdma_v5_0_enable(adev, false);
1438fef6e24cSHawking Zhang
1439fef6e24cSHawking Zhang return 0;
1440fef6e24cSHawking Zhang }
1441fef6e24cSHawking Zhang
sdma_v5_0_suspend(void * handle)1442fef6e24cSHawking Zhang static int sdma_v5_0_suspend(void *handle)
1443fef6e24cSHawking Zhang {
1444fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1445fef6e24cSHawking Zhang
1446fef6e24cSHawking Zhang return sdma_v5_0_hw_fini(adev);
1447fef6e24cSHawking Zhang }
1448fef6e24cSHawking Zhang
sdma_v5_0_resume(void * handle)1449fef6e24cSHawking Zhang static int sdma_v5_0_resume(void *handle)
1450fef6e24cSHawking Zhang {
1451fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1452fef6e24cSHawking Zhang
1453fef6e24cSHawking Zhang return sdma_v5_0_hw_init(adev);
1454fef6e24cSHawking Zhang }
1455fef6e24cSHawking Zhang
sdma_v5_0_is_idle(void * handle)1456fef6e24cSHawking Zhang static bool sdma_v5_0_is_idle(void *handle)
1457fef6e24cSHawking Zhang {
1458fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1459fef6e24cSHawking Zhang u32 i;
1460fef6e24cSHawking Zhang
1461fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
1462fef6e24cSHawking Zhang u32 tmp = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_STATUS_REG));
1463fef6e24cSHawking Zhang
1464fef6e24cSHawking Zhang if (!(tmp & SDMA0_STATUS_REG__IDLE_MASK))
1465fef6e24cSHawking Zhang return false;
1466fef6e24cSHawking Zhang }
1467fef6e24cSHawking Zhang
1468fef6e24cSHawking Zhang return true;
1469fef6e24cSHawking Zhang }
1470fef6e24cSHawking Zhang
sdma_v5_0_wait_for_idle(void * handle)1471fef6e24cSHawking Zhang static int sdma_v5_0_wait_for_idle(void *handle)
1472fef6e24cSHawking Zhang {
1473fef6e24cSHawking Zhang unsigned i;
1474fef6e24cSHawking Zhang u32 sdma0, sdma1;
1475fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1476fef6e24cSHawking Zhang
1477fef6e24cSHawking Zhang for (i = 0; i < adev->usec_timeout; i++) {
1478fef6e24cSHawking Zhang sdma0 = RREG32(sdma_v5_0_get_reg_offset(adev, 0, mmSDMA0_STATUS_REG));
1479fef6e24cSHawking Zhang sdma1 = RREG32(sdma_v5_0_get_reg_offset(adev, 1, mmSDMA0_STATUS_REG));
1480fef6e24cSHawking Zhang
1481fef6e24cSHawking Zhang if (sdma0 & sdma1 & SDMA0_STATUS_REG__IDLE_MASK)
1482fef6e24cSHawking Zhang return 0;
1483fef6e24cSHawking Zhang udelay(1);
1484fef6e24cSHawking Zhang }
1485fef6e24cSHawking Zhang return -ETIMEDOUT;
1486fef6e24cSHawking Zhang }
1487fef6e24cSHawking Zhang
sdma_v5_0_soft_reset(void * handle)1488fef6e24cSHawking Zhang static int sdma_v5_0_soft_reset(void *handle)
1489fef6e24cSHawking Zhang {
1490fef6e24cSHawking Zhang /* todo */
1491fef6e24cSHawking Zhang
1492fef6e24cSHawking Zhang return 0;
1493fef6e24cSHawking Zhang }
1494fef6e24cSHawking Zhang
sdma_v5_0_ring_preempt_ib(struct amdgpu_ring * ring)1495fef6e24cSHawking Zhang static int sdma_v5_0_ring_preempt_ib(struct amdgpu_ring *ring)
1496fef6e24cSHawking Zhang {
1497fef6e24cSHawking Zhang int i, r = 0;
1498fef6e24cSHawking Zhang struct amdgpu_device *adev = ring->adev;
1499fef6e24cSHawking Zhang u32 index = 0;
1500fef6e24cSHawking Zhang u64 sdma_gfx_preempt;
1501fef6e24cSHawking Zhang
1502fef6e24cSHawking Zhang amdgpu_sdma_get_index_from_ring(ring, &index);
1503fef6e24cSHawking Zhang if (index == 0)
1504fef6e24cSHawking Zhang sdma_gfx_preempt = mmSDMA0_GFX_PREEMPT;
1505fef6e24cSHawking Zhang else
1506fef6e24cSHawking Zhang sdma_gfx_preempt = mmSDMA1_GFX_PREEMPT;
1507fef6e24cSHawking Zhang
1508fef6e24cSHawking Zhang /* assert preemption condition */
1509fef6e24cSHawking Zhang amdgpu_ring_set_preempt_cond_exec(ring, false);
1510fef6e24cSHawking Zhang
1511fef6e24cSHawking Zhang /* emit the trailing fence */
1512fef6e24cSHawking Zhang ring->trail_seq += 1;
1513fef6e24cSHawking Zhang amdgpu_ring_alloc(ring, 10);
1514fef6e24cSHawking Zhang sdma_v5_0_ring_emit_fence(ring, ring->trail_fence_gpu_addr,
1515fef6e24cSHawking Zhang ring->trail_seq, 0);
1516fef6e24cSHawking Zhang amdgpu_ring_commit(ring);
1517fef6e24cSHawking Zhang
1518fef6e24cSHawking Zhang /* assert IB preemption */
1519fef6e24cSHawking Zhang WREG32(sdma_gfx_preempt, 1);
1520fef6e24cSHawking Zhang
1521fef6e24cSHawking Zhang /* poll the trailing fence */
1522fef6e24cSHawking Zhang for (i = 0; i < adev->usec_timeout; i++) {
1523fef6e24cSHawking Zhang if (ring->trail_seq ==
1524fef6e24cSHawking Zhang le32_to_cpu(*(ring->trail_fence_cpu_addr)))
1525fef6e24cSHawking Zhang break;
152675589f49SAlex Deucher udelay(1);
1527fef6e24cSHawking Zhang }
1528fef6e24cSHawking Zhang
1529fef6e24cSHawking Zhang if (i >= adev->usec_timeout) {
1530fef6e24cSHawking Zhang r = -EINVAL;
1531fef6e24cSHawking Zhang DRM_ERROR("ring %d failed to be preempted\n", ring->idx);
1532fef6e24cSHawking Zhang }
1533fef6e24cSHawking Zhang
1534fef6e24cSHawking Zhang /* deassert IB preemption */
1535fef6e24cSHawking Zhang WREG32(sdma_gfx_preempt, 0);
1536fef6e24cSHawking Zhang
1537fef6e24cSHawking Zhang /* deassert the preemption condition */
1538fef6e24cSHawking Zhang amdgpu_ring_set_preempt_cond_exec(ring, true);
1539fef6e24cSHawking Zhang return r;
1540fef6e24cSHawking Zhang }
1541fef6e24cSHawking Zhang
sdma_v5_0_set_trap_irq_state(struct amdgpu_device * adev,struct amdgpu_irq_src * source,unsigned type,enum amdgpu_interrupt_state state)1542fef6e24cSHawking Zhang static int sdma_v5_0_set_trap_irq_state(struct amdgpu_device *adev,
1543fef6e24cSHawking Zhang struct amdgpu_irq_src *source,
1544fef6e24cSHawking Zhang unsigned type,
1545fef6e24cSHawking Zhang enum amdgpu_interrupt_state state)
1546fef6e24cSHawking Zhang {
1547fef6e24cSHawking Zhang u32 sdma_cntl;
1548fef6e24cSHawking Zhang
154917e137f2SYintian Tao if (!amdgpu_sriov_vf(adev)) {
1550fef6e24cSHawking Zhang u32 reg_offset = (type == AMDGPU_SDMA_IRQ_INSTANCE0) ?
1551fef6e24cSHawking Zhang sdma_v5_0_get_reg_offset(adev, 0, mmSDMA0_CNTL) :
1552fef6e24cSHawking Zhang sdma_v5_0_get_reg_offset(adev, 1, mmSDMA0_CNTL);
1553fef6e24cSHawking Zhang
1554fef6e24cSHawking Zhang sdma_cntl = RREG32(reg_offset);
1555fef6e24cSHawking Zhang sdma_cntl = REG_SET_FIELD(sdma_cntl, SDMA0_CNTL, TRAP_ENABLE,
1556fef6e24cSHawking Zhang state == AMDGPU_IRQ_STATE_ENABLE ? 1 : 0);
1557fef6e24cSHawking Zhang WREG32(reg_offset, sdma_cntl);
155817e137f2SYintian Tao }
1559fef6e24cSHawking Zhang
1560fef6e24cSHawking Zhang return 0;
1561fef6e24cSHawking Zhang }
1562fef6e24cSHawking Zhang
sdma_v5_0_process_trap_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)1563fef6e24cSHawking Zhang static int sdma_v5_0_process_trap_irq(struct amdgpu_device *adev,
1564fef6e24cSHawking Zhang struct amdgpu_irq_src *source,
1565fef6e24cSHawking Zhang struct amdgpu_iv_entry *entry)
1566fef6e24cSHawking Zhang {
156776411afdSJack Xiao uint32_t mes_queue_id = entry->src_data[0];
156876411afdSJack Xiao
1569fef6e24cSHawking Zhang DRM_DEBUG("IH: SDMA trap\n");
157076411afdSJack Xiao
157176411afdSJack Xiao if (adev->enable_mes && (mes_queue_id & AMDGPU_FENCE_MES_QUEUE_FLAG)) {
157276411afdSJack Xiao struct amdgpu_mes_queue *queue;
157376411afdSJack Xiao
157476411afdSJack Xiao mes_queue_id &= AMDGPU_FENCE_MES_QUEUE_ID_MASK;
157576411afdSJack Xiao
157676411afdSJack Xiao spin_lock(&adev->mes.queue_id_lock);
157776411afdSJack Xiao queue = idr_find(&adev->mes.queue_id_idr, mes_queue_id);
157876411afdSJack Xiao if (queue) {
157976411afdSJack Xiao DRM_DEBUG("process smda queue id = %d\n", mes_queue_id);
158076411afdSJack Xiao amdgpu_fence_process(queue->ring);
158176411afdSJack Xiao }
158276411afdSJack Xiao spin_unlock(&adev->mes.queue_id_lock);
158376411afdSJack Xiao return 0;
158476411afdSJack Xiao }
158576411afdSJack Xiao
1586fef6e24cSHawking Zhang switch (entry->client_id) {
1587fef6e24cSHawking Zhang case SOC15_IH_CLIENTID_SDMA0:
1588fef6e24cSHawking Zhang switch (entry->ring_id) {
1589fef6e24cSHawking Zhang case 0:
1590fef6e24cSHawking Zhang amdgpu_fence_process(&adev->sdma.instance[0].ring);
1591fef6e24cSHawking Zhang break;
1592fef6e24cSHawking Zhang case 1:
1593fef6e24cSHawking Zhang /* XXX compute */
1594fef6e24cSHawking Zhang break;
1595fef6e24cSHawking Zhang case 2:
1596fef6e24cSHawking Zhang /* XXX compute */
1597fef6e24cSHawking Zhang break;
1598fef6e24cSHawking Zhang case 3:
1599fef6e24cSHawking Zhang /* XXX page queue*/
1600fef6e24cSHawking Zhang break;
1601fef6e24cSHawking Zhang }
1602fef6e24cSHawking Zhang break;
1603fef6e24cSHawking Zhang case SOC15_IH_CLIENTID_SDMA1:
1604fef6e24cSHawking Zhang switch (entry->ring_id) {
1605fef6e24cSHawking Zhang case 0:
1606fef6e24cSHawking Zhang amdgpu_fence_process(&adev->sdma.instance[1].ring);
1607fef6e24cSHawking Zhang break;
1608fef6e24cSHawking Zhang case 1:
1609fef6e24cSHawking Zhang /* XXX compute */
1610fef6e24cSHawking Zhang break;
1611fef6e24cSHawking Zhang case 2:
1612fef6e24cSHawking Zhang /* XXX compute */
1613fef6e24cSHawking Zhang break;
1614fef6e24cSHawking Zhang case 3:
1615fef6e24cSHawking Zhang /* XXX page queue*/
1616fef6e24cSHawking Zhang break;
1617fef6e24cSHawking Zhang }
1618fef6e24cSHawking Zhang break;
1619fef6e24cSHawking Zhang }
1620fef6e24cSHawking Zhang return 0;
1621fef6e24cSHawking Zhang }
1622fef6e24cSHawking Zhang
sdma_v5_0_process_illegal_inst_irq(struct amdgpu_device * adev,struct amdgpu_irq_src * source,struct amdgpu_iv_entry * entry)1623fef6e24cSHawking Zhang static int sdma_v5_0_process_illegal_inst_irq(struct amdgpu_device *adev,
1624fef6e24cSHawking Zhang struct amdgpu_irq_src *source,
1625fef6e24cSHawking Zhang struct amdgpu_iv_entry *entry)
1626fef6e24cSHawking Zhang {
1627fef6e24cSHawking Zhang return 0;
1628fef6e24cSHawking Zhang }
1629fef6e24cSHawking Zhang
sdma_v5_0_update_medium_grain_clock_gating(struct amdgpu_device * adev,bool enable)1630fef6e24cSHawking Zhang static void sdma_v5_0_update_medium_grain_clock_gating(struct amdgpu_device *adev,
1631fef6e24cSHawking Zhang bool enable)
1632fef6e24cSHawking Zhang {
1633fef6e24cSHawking Zhang uint32_t data, def;
1634fef6e24cSHawking Zhang int i;
1635fef6e24cSHawking Zhang
1636fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
1637fef6e24cSHawking Zhang if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_MGCG)) {
1638fef6e24cSHawking Zhang /* Enable sdma clock gating */
1639fef6e24cSHawking Zhang def = data = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL));
1640fef6e24cSHawking Zhang data &= ~(SDMA0_CLK_CTRL__SOFT_OVERRIDE7_MASK |
1641fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE6_MASK |
1642fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE5_MASK |
1643fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1644fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1645fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1646fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1647fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK);
1648fef6e24cSHawking Zhang if (def != data)
1649fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL), data);
1650fef6e24cSHawking Zhang } else {
1651fef6e24cSHawking Zhang /* Disable sdma clock gating */
1652fef6e24cSHawking Zhang def = data = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL));
1653fef6e24cSHawking Zhang data |= (SDMA0_CLK_CTRL__SOFT_OVERRIDE7_MASK |
1654fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE6_MASK |
1655fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE5_MASK |
1656fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE4_MASK |
1657fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE3_MASK |
1658fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE2_MASK |
1659fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE1_MASK |
1660fef6e24cSHawking Zhang SDMA0_CLK_CTRL__SOFT_OVERRIDE0_MASK);
1661fef6e24cSHawking Zhang if (def != data)
1662fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_CLK_CTRL), data);
1663fef6e24cSHawking Zhang }
1664fef6e24cSHawking Zhang }
1665fef6e24cSHawking Zhang }
1666fef6e24cSHawking Zhang
sdma_v5_0_update_medium_grain_light_sleep(struct amdgpu_device * adev,bool enable)1667fef6e24cSHawking Zhang static void sdma_v5_0_update_medium_grain_light_sleep(struct amdgpu_device *adev,
1668fef6e24cSHawking Zhang bool enable)
1669fef6e24cSHawking Zhang {
1670fef6e24cSHawking Zhang uint32_t data, def;
1671fef6e24cSHawking Zhang int i;
1672fef6e24cSHawking Zhang
1673fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
1674fef6e24cSHawking Zhang if (enable && (adev->cg_flags & AMD_CG_SUPPORT_SDMA_LS)) {
1675fef6e24cSHawking Zhang /* Enable sdma mem light sleep */
1676fef6e24cSHawking Zhang def = data = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL));
1677fef6e24cSHawking Zhang data |= SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1678fef6e24cSHawking Zhang if (def != data)
1679fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL), data);
1680fef6e24cSHawking Zhang
1681fef6e24cSHawking Zhang } else {
1682fef6e24cSHawking Zhang /* Disable sdma mem light sleep */
1683fef6e24cSHawking Zhang def = data = RREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL));
1684fef6e24cSHawking Zhang data &= ~SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK;
1685fef6e24cSHawking Zhang if (def != data)
1686fef6e24cSHawking Zhang WREG32(sdma_v5_0_get_reg_offset(adev, i, mmSDMA0_POWER_CNTL), data);
1687fef6e24cSHawking Zhang
1688fef6e24cSHawking Zhang }
1689fef6e24cSHawking Zhang }
1690fef6e24cSHawking Zhang }
1691fef6e24cSHawking Zhang
sdma_v5_0_set_clockgating_state(void * handle,enum amd_clockgating_state state)1692fef6e24cSHawking Zhang static int sdma_v5_0_set_clockgating_state(void *handle,
1693fef6e24cSHawking Zhang enum amd_clockgating_state state)
1694fef6e24cSHawking Zhang {
1695fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1696fef6e24cSHawking Zhang
1697fef6e24cSHawking Zhang if (amdgpu_sriov_vf(adev))
1698fef6e24cSHawking Zhang return 0;
1699fef6e24cSHawking Zhang
17001d789535SAlex Deucher switch (adev->ip_versions[SDMA0_HWIP][0]) {
170102200e91SAlex Deucher case IP_VERSION(5, 0, 0):
170202200e91SAlex Deucher case IP_VERSION(5, 0, 2):
170302200e91SAlex Deucher case IP_VERSION(5, 0, 5):
1704fef6e24cSHawking Zhang sdma_v5_0_update_medium_grain_clock_gating(adev,
1705a9d4fe2fSNirmoy Das state == AMD_CG_STATE_GATE);
1706fef6e24cSHawking Zhang sdma_v5_0_update_medium_grain_light_sleep(adev,
1707a9d4fe2fSNirmoy Das state == AMD_CG_STATE_GATE);
1708fef6e24cSHawking Zhang break;
1709fef6e24cSHawking Zhang default:
1710fef6e24cSHawking Zhang break;
1711fef6e24cSHawking Zhang }
1712fef6e24cSHawking Zhang
1713fef6e24cSHawking Zhang return 0;
1714fef6e24cSHawking Zhang }
1715fef6e24cSHawking Zhang
sdma_v5_0_set_powergating_state(void * handle,enum amd_powergating_state state)1716fef6e24cSHawking Zhang static int sdma_v5_0_set_powergating_state(void *handle,
1717fef6e24cSHawking Zhang enum amd_powergating_state state)
1718fef6e24cSHawking Zhang {
1719fef6e24cSHawking Zhang return 0;
1720fef6e24cSHawking Zhang }
1721fef6e24cSHawking Zhang
sdma_v5_0_get_clockgating_state(void * handle,u64 * flags)172225faeddcSEvan Quan static void sdma_v5_0_get_clockgating_state(void *handle, u64 *flags)
1723fef6e24cSHawking Zhang {
1724fef6e24cSHawking Zhang struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1725fef6e24cSHawking Zhang int data;
1726fef6e24cSHawking Zhang
1727fef6e24cSHawking Zhang if (amdgpu_sriov_vf(adev))
1728fef6e24cSHawking Zhang *flags = 0;
1729fef6e24cSHawking Zhang
1730fef6e24cSHawking Zhang /* AMD_CG_SUPPORT_SDMA_MGCG */
1731fef6e24cSHawking Zhang data = RREG32(sdma_v5_0_get_reg_offset(adev, 0, mmSDMA0_CLK_CTRL));
1732fef6e24cSHawking Zhang if (!(data & SDMA0_CLK_CTRL__SOFT_OVERRIDE7_MASK))
1733fef6e24cSHawking Zhang *flags |= AMD_CG_SUPPORT_SDMA_MGCG;
1734fef6e24cSHawking Zhang
1735fef6e24cSHawking Zhang /* AMD_CG_SUPPORT_SDMA_LS */
1736fef6e24cSHawking Zhang data = RREG32(sdma_v5_0_get_reg_offset(adev, 0, mmSDMA0_POWER_CNTL));
1737fef6e24cSHawking Zhang if (data & SDMA0_POWER_CNTL__MEM_POWER_OVERRIDE_MASK)
1738fef6e24cSHawking Zhang *flags |= AMD_CG_SUPPORT_SDMA_LS;
1739fef6e24cSHawking Zhang }
1740fef6e24cSHawking Zhang
1741fef6e24cSHawking Zhang const struct amd_ip_funcs sdma_v5_0_ip_funcs = {
1742fef6e24cSHawking Zhang .name = "sdma_v5_0",
1743fef6e24cSHawking Zhang .early_init = sdma_v5_0_early_init,
1744fef6e24cSHawking Zhang .late_init = NULL,
1745fef6e24cSHawking Zhang .sw_init = sdma_v5_0_sw_init,
1746fef6e24cSHawking Zhang .sw_fini = sdma_v5_0_sw_fini,
1747fef6e24cSHawking Zhang .hw_init = sdma_v5_0_hw_init,
1748fef6e24cSHawking Zhang .hw_fini = sdma_v5_0_hw_fini,
1749fef6e24cSHawking Zhang .suspend = sdma_v5_0_suspend,
1750fef6e24cSHawking Zhang .resume = sdma_v5_0_resume,
1751fef6e24cSHawking Zhang .is_idle = sdma_v5_0_is_idle,
1752fef6e24cSHawking Zhang .wait_for_idle = sdma_v5_0_wait_for_idle,
1753fef6e24cSHawking Zhang .soft_reset = sdma_v5_0_soft_reset,
1754fef6e24cSHawking Zhang .set_clockgating_state = sdma_v5_0_set_clockgating_state,
1755fef6e24cSHawking Zhang .set_powergating_state = sdma_v5_0_set_powergating_state,
1756fef6e24cSHawking Zhang .get_clockgating_state = sdma_v5_0_get_clockgating_state,
1757fef6e24cSHawking Zhang };
1758fef6e24cSHawking Zhang
1759fef6e24cSHawking Zhang static const struct amdgpu_ring_funcs sdma_v5_0_ring_funcs = {
1760fef6e24cSHawking Zhang .type = AMDGPU_RING_TYPE_SDMA,
1761fef6e24cSHawking Zhang .align_mask = 0xf,
1762fef6e24cSHawking Zhang .nop = SDMA_PKT_NOP_HEADER_OP(SDMA_OP_NOP),
1763fef6e24cSHawking Zhang .support_64bit_ptrs = true,
17648c0f11ffSLang Yu .secure_submission_supported = true,
1765fef6e24cSHawking Zhang .get_rptr = sdma_v5_0_ring_get_rptr,
1766fef6e24cSHawking Zhang .get_wptr = sdma_v5_0_ring_get_wptr,
1767fef6e24cSHawking Zhang .set_wptr = sdma_v5_0_ring_set_wptr,
1768fef6e24cSHawking Zhang .emit_frame_size =
1769fef6e24cSHawking Zhang 5 + /* sdma_v5_0_ring_init_cond_exec */
1770fef6e24cSHawking Zhang 6 + /* sdma_v5_0_ring_emit_hdp_flush */
1771fef6e24cSHawking Zhang 3 + /* hdp_invalidate */
1772fef6e24cSHawking Zhang 6 + /* sdma_v5_0_ring_emit_pipeline_sync */
1773fef6e24cSHawking Zhang /* sdma_v5_0_ring_emit_vm_flush */
1774fef6e24cSHawking Zhang SOC15_FLUSH_GPU_TLB_NUM_WREG * 3 +
1775589b64a7Schangzhu SOC15_FLUSH_GPU_TLB_NUM_REG_WAIT * 6 * 2 +
1776fef6e24cSHawking Zhang 10 + 10 + 10, /* sdma_v5_0_ring_emit_fence x3 for user fence, vm fence */
1777652a6a85SMarek Olšák .emit_ib_size = 5 + 7 + 6, /* sdma_v5_0_ring_emit_ib */
1778fef6e24cSHawking Zhang .emit_ib = sdma_v5_0_ring_emit_ib,
1779e8d7aa68SAlex Deucher .emit_mem_sync = sdma_v5_0_ring_emit_mem_sync,
1780fef6e24cSHawking Zhang .emit_fence = sdma_v5_0_ring_emit_fence,
1781fef6e24cSHawking Zhang .emit_pipeline_sync = sdma_v5_0_ring_emit_pipeline_sync,
1782fef6e24cSHawking Zhang .emit_vm_flush = sdma_v5_0_ring_emit_vm_flush,
1783fef6e24cSHawking Zhang .emit_hdp_flush = sdma_v5_0_ring_emit_hdp_flush,
1784fef6e24cSHawking Zhang .test_ring = sdma_v5_0_ring_test_ring,
1785fef6e24cSHawking Zhang .test_ib = sdma_v5_0_ring_test_ib,
1786fef6e24cSHawking Zhang .insert_nop = sdma_v5_0_ring_insert_nop,
1787fef6e24cSHawking Zhang .pad_ib = sdma_v5_0_ring_pad_ib,
1788fef6e24cSHawking Zhang .emit_wreg = sdma_v5_0_ring_emit_wreg,
1789fef6e24cSHawking Zhang .emit_reg_wait = sdma_v5_0_ring_emit_reg_wait,
1790589b64a7Schangzhu .emit_reg_write_reg_wait = sdma_v5_0_ring_emit_reg_write_reg_wait,
1791fef6e24cSHawking Zhang .init_cond_exec = sdma_v5_0_ring_init_cond_exec,
1792fef6e24cSHawking Zhang .patch_cond_exec = sdma_v5_0_ring_patch_cond_exec,
1793fef6e24cSHawking Zhang .preempt_ib = sdma_v5_0_ring_preempt_ib,
1794fef6e24cSHawking Zhang };
1795fef6e24cSHawking Zhang
sdma_v5_0_set_ring_funcs(struct amdgpu_device * adev)1796fef6e24cSHawking Zhang static void sdma_v5_0_set_ring_funcs(struct amdgpu_device *adev)
1797fef6e24cSHawking Zhang {
1798fef6e24cSHawking Zhang int i;
1799fef6e24cSHawking Zhang
1800fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
1801fef6e24cSHawking Zhang adev->sdma.instance[i].ring.funcs = &sdma_v5_0_ring_funcs;
1802fef6e24cSHawking Zhang adev->sdma.instance[i].ring.me = i;
1803fef6e24cSHawking Zhang }
1804fef6e24cSHawking Zhang }
1805fef6e24cSHawking Zhang
1806fef6e24cSHawking Zhang static const struct amdgpu_irq_src_funcs sdma_v5_0_trap_irq_funcs = {
1807fef6e24cSHawking Zhang .set = sdma_v5_0_set_trap_irq_state,
1808fef6e24cSHawking Zhang .process = sdma_v5_0_process_trap_irq,
1809fef6e24cSHawking Zhang };
1810fef6e24cSHawking Zhang
1811fef6e24cSHawking Zhang static const struct amdgpu_irq_src_funcs sdma_v5_0_illegal_inst_irq_funcs = {
1812fef6e24cSHawking Zhang .process = sdma_v5_0_process_illegal_inst_irq,
1813fef6e24cSHawking Zhang };
1814fef6e24cSHawking Zhang
sdma_v5_0_set_irq_funcs(struct amdgpu_device * adev)1815fef6e24cSHawking Zhang static void sdma_v5_0_set_irq_funcs(struct amdgpu_device *adev)
1816fef6e24cSHawking Zhang {
18179e484950SXiaojie Yuan adev->sdma.trap_irq.num_types = AMDGPU_SDMA_IRQ_INSTANCE0 +
18189e484950SXiaojie Yuan adev->sdma.num_instances;
1819fef6e24cSHawking Zhang adev->sdma.trap_irq.funcs = &sdma_v5_0_trap_irq_funcs;
1820fef6e24cSHawking Zhang adev->sdma.illegal_inst_irq.funcs = &sdma_v5_0_illegal_inst_irq_funcs;
1821fef6e24cSHawking Zhang }
1822fef6e24cSHawking Zhang
1823fef6e24cSHawking Zhang /**
1824fef6e24cSHawking Zhang * sdma_v5_0_emit_copy_buffer - copy buffer using the sDMA engine
1825fef6e24cSHawking Zhang *
1826b4234aecSLee Jones * @ib: indirect buffer to copy to
1827fef6e24cSHawking Zhang * @src_offset: src GPU address
1828fef6e24cSHawking Zhang * @dst_offset: dst GPU address
1829fef6e24cSHawking Zhang * @byte_count: number of bytes to xfer
1830b4234aecSLee Jones * @tmz: if a secure copy should be used
1831fef6e24cSHawking Zhang *
1832fef6e24cSHawking Zhang * Copy GPU buffers using the DMA engine (NAVI10).
1833fef6e24cSHawking Zhang * Used by the amdgpu ttm implementation to move pages if
1834fef6e24cSHawking Zhang * registered as the asic copy callback.
1835fef6e24cSHawking Zhang */
sdma_v5_0_emit_copy_buffer(struct amdgpu_ib * ib,uint64_t src_offset,uint64_t dst_offset,uint32_t byte_count,bool tmz)1836fef6e24cSHawking Zhang static void sdma_v5_0_emit_copy_buffer(struct amdgpu_ib *ib,
1837fef6e24cSHawking Zhang uint64_t src_offset,
1838fef6e24cSHawking Zhang uint64_t dst_offset,
1839be7538ffSAaron Liu uint32_t byte_count,
1840be7538ffSAaron Liu bool tmz)
1841fef6e24cSHawking Zhang {
1842fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_COPY) |
1843b231531cSAaron Liu SDMA_PKT_HEADER_SUB_OP(SDMA_SUBOP_COPY_LINEAR) |
1844b231531cSAaron Liu SDMA_PKT_COPY_LINEAR_HEADER_TMZ(tmz ? 1 : 0);
1845fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = byte_count - 1;
1846fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = 0; /* src/dst endian swap */
1847fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(src_offset);
1848fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(src_offset);
1849fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1850fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1851fef6e24cSHawking Zhang }
1852fef6e24cSHawking Zhang
1853fef6e24cSHawking Zhang /**
1854fef6e24cSHawking Zhang * sdma_v5_0_emit_fill_buffer - fill buffer using the sDMA engine
1855fef6e24cSHawking Zhang *
1856b4234aecSLee Jones * @ib: indirect buffer to fill
1857fef6e24cSHawking Zhang * @src_data: value to write to buffer
1858fef6e24cSHawking Zhang * @dst_offset: dst GPU address
1859fef6e24cSHawking Zhang * @byte_count: number of bytes to xfer
1860fef6e24cSHawking Zhang *
1861fef6e24cSHawking Zhang * Fill GPU buffers using the DMA engine (NAVI10).
1862fef6e24cSHawking Zhang */
sdma_v5_0_emit_fill_buffer(struct amdgpu_ib * ib,uint32_t src_data,uint64_t dst_offset,uint32_t byte_count)1863fef6e24cSHawking Zhang static void sdma_v5_0_emit_fill_buffer(struct amdgpu_ib *ib,
1864fef6e24cSHawking Zhang uint32_t src_data,
1865fef6e24cSHawking Zhang uint64_t dst_offset,
1866fef6e24cSHawking Zhang uint32_t byte_count)
1867fef6e24cSHawking Zhang {
1868fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = SDMA_PKT_HEADER_OP(SDMA_OP_CONST_FILL);
1869fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = lower_32_bits(dst_offset);
1870fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = upper_32_bits(dst_offset);
1871fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = src_data;
1872fef6e24cSHawking Zhang ib->ptr[ib->length_dw++] = byte_count - 1;
1873fef6e24cSHawking Zhang }
1874fef6e24cSHawking Zhang
1875fef6e24cSHawking Zhang static const struct amdgpu_buffer_funcs sdma_v5_0_buffer_funcs = {
1876fef6e24cSHawking Zhang .copy_max_bytes = 0x400000,
1877fef6e24cSHawking Zhang .copy_num_dw = 7,
1878fef6e24cSHawking Zhang .emit_copy_buffer = sdma_v5_0_emit_copy_buffer,
1879fef6e24cSHawking Zhang
1880fef6e24cSHawking Zhang .fill_max_bytes = 0x400000,
1881fef6e24cSHawking Zhang .fill_num_dw = 5,
1882fef6e24cSHawking Zhang .emit_fill_buffer = sdma_v5_0_emit_fill_buffer,
1883fef6e24cSHawking Zhang };
1884fef6e24cSHawking Zhang
sdma_v5_0_set_buffer_funcs(struct amdgpu_device * adev)1885fef6e24cSHawking Zhang static void sdma_v5_0_set_buffer_funcs(struct amdgpu_device *adev)
1886fef6e24cSHawking Zhang {
1887fef6e24cSHawking Zhang if (adev->mman.buffer_funcs == NULL) {
1888fef6e24cSHawking Zhang adev->mman.buffer_funcs = &sdma_v5_0_buffer_funcs;
1889fef6e24cSHawking Zhang adev->mman.buffer_funcs_ring = &adev->sdma.instance[0].ring;
1890fef6e24cSHawking Zhang }
1891fef6e24cSHawking Zhang }
1892fef6e24cSHawking Zhang
1893fef6e24cSHawking Zhang static const struct amdgpu_vm_pte_funcs sdma_v5_0_vm_pte_funcs = {
1894fef6e24cSHawking Zhang .copy_pte_num_dw = 7,
1895fef6e24cSHawking Zhang .copy_pte = sdma_v5_0_vm_copy_pte,
1896fef6e24cSHawking Zhang .write_pte = sdma_v5_0_vm_write_pte,
1897fef6e24cSHawking Zhang .set_pte_pde = sdma_v5_0_vm_set_pte_pde,
1898fef6e24cSHawking Zhang };
1899fef6e24cSHawking Zhang
sdma_v5_0_set_vm_pte_funcs(struct amdgpu_device * adev)1900fef6e24cSHawking Zhang static void sdma_v5_0_set_vm_pte_funcs(struct amdgpu_device *adev)
1901fef6e24cSHawking Zhang {
1902fef6e24cSHawking Zhang unsigned i;
1903fef6e24cSHawking Zhang
1904fef6e24cSHawking Zhang if (adev->vm_manager.vm_pte_funcs == NULL) {
1905fef6e24cSHawking Zhang adev->vm_manager.vm_pte_funcs = &sdma_v5_0_vm_pte_funcs;
1906fef6e24cSHawking Zhang for (i = 0; i < adev->sdma.num_instances; i++) {
19070c88b430SNirmoy Das adev->vm_manager.vm_pte_scheds[i] =
19080c88b430SNirmoy Das &adev->sdma.instance[i].ring.sched;
1909fef6e24cSHawking Zhang }
19100c88b430SNirmoy Das adev->vm_manager.vm_pte_num_scheds = adev->sdma.num_instances;
1911fef6e24cSHawking Zhang }
1912fef6e24cSHawking Zhang }
1913fef6e24cSHawking Zhang
1914fef6e24cSHawking Zhang const struct amdgpu_ip_block_version sdma_v5_0_ip_block = {
1915fef6e24cSHawking Zhang .type = AMD_IP_BLOCK_TYPE_SDMA,
1916fef6e24cSHawking Zhang .major = 5,
1917fef6e24cSHawking Zhang .minor = 0,
1918fef6e24cSHawking Zhang .rev = 0,
1919fef6e24cSHawking Zhang .funcs = &sdma_v5_0_ip_funcs,
1920fef6e24cSHawking Zhang };
1921