43427a3148b74094c6b89c7514b6148b97b86801
[sfrench/cifs-2.6.git] / drivers / gpu / drm / amd / amdgpu / gfx_v10_0.c
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/firmware.h>
27 #include <linux/module.h>
28 #include <linux/pci.h>
29 #include "amdgpu.h"
30 #include "amdgpu_gfx.h"
31 #include "amdgpu_psp.h"
32 #include "amdgpu_smu.h"
33 #include "nv.h"
34 #include "nvd.h"
35
36 #include "gc/gc_10_1_0_offset.h"
37 #include "gc/gc_10_1_0_sh_mask.h"
38 #include "navi10_enum.h"
39 #include "hdp/hdp_5_0_0_offset.h"
40 #include "ivsrcid/gfx/irqsrcs_gfx_10_1.h"
41
42 #include "soc15.h"
43 #include "soc15_common.h"
44 #include "clearstate_gfx10.h"
45 #include "v10_structs.h"
46 #include "gfx_v10_0.h"
47 #include "nbio_v2_3.h"
48
49 /**
50  * Navi10 has two graphic rings to share each graphic pipe.
51  * 1. Primary ring
52  * 2. Async ring
53  *
54  * In bring-up phase, it just used primary ring so set gfx ring number as 1 at
55  * first.
56  */
57 #define GFX10_NUM_GFX_RINGS     2
58 #define GFX10_MEC_HPD_SIZE      2048
59
60 #define F32_CE_PROGRAM_RAM_SIZE         65536
61 #define RLCG_UCODE_LOADING_START_ADDRESS        0x00002000L
62
63 #define mmCGTT_GS_NGG_CLK_CTRL  0x5087
64 #define mmCGTT_GS_NGG_CLK_CTRL_BASE_IDX 1
65
66 MODULE_FIRMWARE("amdgpu/navi10_ce.bin");
67 MODULE_FIRMWARE("amdgpu/navi10_pfp.bin");
68 MODULE_FIRMWARE("amdgpu/navi10_me.bin");
69 MODULE_FIRMWARE("amdgpu/navi10_mec.bin");
70 MODULE_FIRMWARE("amdgpu/navi10_mec2.bin");
71 MODULE_FIRMWARE("amdgpu/navi10_rlc.bin");
72
73 MODULE_FIRMWARE("amdgpu/navi14_ce.bin");
74 MODULE_FIRMWARE("amdgpu/navi14_pfp.bin");
75 MODULE_FIRMWARE("amdgpu/navi14_me.bin");
76 MODULE_FIRMWARE("amdgpu/navi14_mec.bin");
77 MODULE_FIRMWARE("amdgpu/navi14_mec2.bin");
78 MODULE_FIRMWARE("amdgpu/navi14_rlc.bin");
79
80 MODULE_FIRMWARE("amdgpu/navi12_ce.bin");
81 MODULE_FIRMWARE("amdgpu/navi12_pfp.bin");
82 MODULE_FIRMWARE("amdgpu/navi12_me.bin");
83 MODULE_FIRMWARE("amdgpu/navi12_mec.bin");
84 MODULE_FIRMWARE("amdgpu/navi12_mec2.bin");
85 MODULE_FIRMWARE("amdgpu/navi12_rlc.bin");
86
87 static const struct soc15_reg_golden golden_settings_gc_10_1[] =
88 {
89         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_4, 0xffffffff, 0x00400014),
90         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_CPF_CLK_CTRL, 0xfcff8fff, 0xf8000100),
91         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SPI_CLK_CTRL, 0xc0000000, 0xc0000100),
92         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SQ_CLK_CTRL, 0x60000ff0, 0x60000100),
93         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SQG_CLK_CTRL, 0x40000000, 0x40000100),
94         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_VGT_CLK_CTRL, 0xffff8fff, 0xffff8100),
95         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_WD_CLK_CTRL, 0xfeff8fff, 0xfeff8100),
96         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCH_PIPE_STEER, 0xffffffff, 0xe4e4e4e4),
97         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCH_VC5_ENABLE, 0x00000002, 0x00000000),
98         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_SD_CNTL, 0x000007ff, 0x000005ff),
99         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG, 0x20000000, 0x20000000),
100         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xffffffff, 0x00000420),
101         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG3, 0x00000200, 0x00000200),
102         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG4, 0x07900000, 0x04900000),
103         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DFSM_TILES_IN_FLIGHT, 0x0000ffff, 0x0000003f),
104         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_LAST_OF_BURST_CONFIG, 0xffffffff, 0x03860204),
105         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGCR_GENERAL_CNTL, 0x1ff0ffff, 0x00000500),
106         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGE_PRIV_CONTROL, 0x000007ff, 0x000001fe),
107         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL1_PIPE_STEER, 0xffffffff, 0xe4e4e4e4),
108         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2_PIPE_STEER_0, 0x77777777, 0x10321032),
109         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2_PIPE_STEER_1, 0x77777777, 0x02310231),
110         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2A_ADDR_MATCH_MASK, 0xffffffff, 0xffffffcf),
111         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_ADDR_MATCH_MASK, 0xffffffff, 0xffffffcf),
112         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CGTT_SCLK_CTRL, 0x10000000, 0x10000100),
113         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CTRL2, 0xffffffff, 0x1402002f),
114         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CTRL3, 0xffff9fff, 0x00001188),
115         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x08000009),
116         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0x00400000, 0x04440000),
117         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
118         SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_SPARE, 0xffffffff, 0xffff3101),
119         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_ALU_CLK_CTRL, 0xffffffff, 0xffffffff),
120         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_ARB_CONFIG, 0x00000100, 0x00000130),
121         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_LDS_CLK_CTRL, 0xffffffff, 0xffffffff),
122         SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfff7ffff, 0x01030000),
123         SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CNTL, 0x60000010, 0x479c0010),
124         SOC15_REG_GOLDEN_VALUE(GC, 0, mmUTCL1_CGTT_CLK_CTRL, 0xfeff0fff, 0x40000100),
125         SOC15_REG_GOLDEN_VALUE(GC, 0, mmUTCL1_CTRL, 0x00800000, 0x00800000)
126 };
127
128 static const struct soc15_reg_golden golden_settings_gc_10_0_nv10[] =
129 {
130         /* Pending on emulation bring up */
131 };
132
133 static const struct soc15_reg_golden golden_settings_gc_10_1_1[] =
134 {
135         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_4, 0xffffffff, 0x003c0014),
136         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_GS_NGG_CLK_CTRL, 0xffff8fff, 0xffff8100),
137         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_IA_CLK_CTRL, 0xffff0fff, 0xffff0100),
138         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SPI_CLK_CTRL, 0xc0000000, 0xc0000100),
139         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SQ_CLK_CTRL, 0xf8ff0fff, 0x60000100),
140         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SQG_CLK_CTRL, 0x40000ff0, 0x40000100),
141         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_VGT_CLK_CTRL, 0xffff8fff, 0xffff8100),
142         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_WD_CLK_CTRL, 0xffff8fff, 0xffff8100),
143         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCH_PIPE_STEER, 0xffffffff, 0xe4e4e4e4),
144         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCH_VC5_ENABLE, 0x00000002, 0x00000000),
145         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_SD_CNTL, 0x800007ff, 0x000005ff),
146         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG, 0xffffffff, 0x20000000),
147         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xffffffff, 0x00000420),
148         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG3, 0x00000200, 0x00000200),
149         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG4, 0xffffffff, 0x04900000),
150         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DFSM_TILES_IN_FLIGHT, 0x0000ffff, 0x0000003f),
151         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_LAST_OF_BURST_CONFIG, 0xffffffff, 0x03860204),
152         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGCR_GENERAL_CNTL, 0x1ff0ffff, 0x00000500),
153         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGE_PRIV_CONTROL, 0x000007ff, 0x000001fe),
154         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL1_PIPE_STEER, 0xffffffff, 0xe4e4e4e4),
155         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2A_ADDR_MATCH_MASK, 0xffffffff, 0xffffffe7),
156         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_ADDR_MATCH_MASK, 0xffffffff, 0xffffffe7),
157         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CGTT_SCLK_CTRL, 0xffff0fff, 0x10000100),
158         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CTRL2, 0xffffffff, 0x1402002f),
159         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CTRL3, 0xffffbfff, 0x00000188),
160         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x08000009),
161         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0x00400000, 0x04440000),
162         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
163         SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_SPARE, 0xffffffff, 0xffff3101),
164         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_ALU_CLK_CTRL, 0xffffffff, 0xffffffff),
165         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_ARB_CONFIG, 0x00000133, 0x00000130),
166         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_LDS_CLK_CTRL, 0xffffffff, 0xffffffff),
167         SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfff7ffff, 0x01030000),
168         SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CNTL, 0x60000010, 0x479c0010),
169         SOC15_REG_GOLDEN_VALUE(GC, 0, mmUTCL1_CTRL, 0x00800000, 0x00800000),
170 };
171
172 static const struct soc15_reg_golden golden_settings_gc_10_1_2[] =
173 {
174         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCB_HW_CONTROL_4, 0x003e001f, 0x003c0014),
175         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_GS_NGG_CLK_CTRL, 0xffff8fff, 0xffff8100),
176         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_IA_CLK_CTRL, 0xffff0fff, 0xffff0100),
177         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SPI_CLK_CTRL, 0xff7f0fff, 0xc0000100),
178         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SQ_CLK_CTRL, 0xffffcfff, 0x60000100),
179         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_SQG_CLK_CTRL, 0xffff0fff, 0x40000100),
180         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_VGT_CLK_CTRL, 0xffff8fff, 0xffff8100),
181         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCGTT_WD_CLK_CTRL, 0xffff8fff, 0xffff8100),
182         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCH_PIPE_STEER, 0xffffffff, 0xe4e4e4e4),
183         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCH_VC5_ENABLE, 0x00000003, 0x00000000),
184         SOC15_REG_GOLDEN_VALUE(GC, 0, mmCP_SD_CNTL, 0x800007ff, 0x000005ff),
185         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG, 0xffffffff, 0x20000000),
186         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG2, 0xffffffff, 0x00000420),
187         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG3, 0xffffffff, 0x00000200),
188         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DEBUG4, 0xffffffff, 0x04800000),
189         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_DFSM_TILES_IN_FLIGHT, 0x0000ffff, 0x0000003f),
190         SOC15_REG_GOLDEN_VALUE(GC, 0, mmDB_LAST_OF_BURST_CONFIG, 0xffffffff, 0x03860204),
191         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGCR_GENERAL_CNTL, 0x1ff0ffff, 0x00000500),
192         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGE_PRIV_CONTROL, 0x00007fff, 0x000001fe),
193         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL1_PIPE_STEER, 0xffffffff, 0xe4e4e4e4),
194         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2_PIPE_STEER_0, 0x77777777, 0x10321032),
195         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2_PIPE_STEER_1, 0x77777777, 0x02310231),
196         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2A_ADDR_MATCH_MASK, 0xffffffff, 0xffffffcf),
197         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_ADDR_MATCH_MASK, 0xffffffff, 0xffffffcf),
198         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CGTT_SCLK_CTRL, 0xffff0fff, 0x10000100),
199         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CTRL2, 0xffffffff, 0x1402002f),
200         SOC15_REG_GOLDEN_VALUE(GC, 0, mmGL2C_CTRL3, 0xffffbfff, 0x00000188),
201         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_EVENT_CNTL_0, 0xffffffff, 0x842a4c02),
202         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_BINNER_TIMEOUT_COUNTER, 0xffffffff, 0x00000800),
203         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE, 0x3fffffff, 0x08000009),
204         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_1, 0xffffffff, 0x04440000),
205         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_ENHANCE_2, 0x00000820, 0x00000820),
206         SOC15_REG_GOLDEN_VALUE(GC, 0, mmPA_SC_LINE_STIPPLE_STATE, 0x0000ff0f, 0x00000000),
207         SOC15_REG_GOLDEN_VALUE(GC, 0, mmRMI_SPARE, 0xffffffff, 0xffff3101),
208         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_ALU_CLK_CTRL, 0xffffffff, 0xffffffff),
209         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_ARB_CONFIG, 0x00000133, 0x00000130),
210         SOC15_REG_GOLDEN_VALUE(GC, 0, mmSQ_LDS_CLK_CTRL, 0xffffffff, 0xffffffff),
211         SOC15_REG_GOLDEN_VALUE(GC, 0, mmTA_CNTL_AUX, 0xfff7ffff, 0x01030000),
212         SOC15_REG_GOLDEN_VALUE(GC, 0, mmTCP_CNTL, 0xffdf80ff, 0x479c0010),
213         SOC15_REG_GOLDEN_VALUE(GC, 0, mmUTCL1_CTRL, 0xffffffff, 0x00800000)
214 };
215
216 static const struct soc15_reg_golden golden_settings_gc_10_1_nv14[] =
217 {
218         /* Pending on emulation bring up */
219 };
220
221 static const struct soc15_reg_golden golden_settings_gc_10_1_2_nv12[] =
222 {
223         /* Pending on emulation bring up */
224 };
225
226 #define DEFAULT_SH_MEM_CONFIG \
227         ((SH_MEM_ADDRESS_MODE_64 << SH_MEM_CONFIG__ADDRESS_MODE__SHIFT) | \
228          (SH_MEM_ALIGNMENT_MODE_UNALIGNED << SH_MEM_CONFIG__ALIGNMENT_MODE__SHIFT) | \
229          (SH_MEM_RETRY_MODE_ALL << SH_MEM_CONFIG__RETRY_MODE__SHIFT) | \
230          (3 << SH_MEM_CONFIG__INITIAL_INST_PREFETCH__SHIFT))
231
232
233 static void gfx_v10_0_set_ring_funcs(struct amdgpu_device *adev);
234 static void gfx_v10_0_set_irq_funcs(struct amdgpu_device *adev);
235 static void gfx_v10_0_set_gds_init(struct amdgpu_device *adev);
236 static void gfx_v10_0_set_rlc_funcs(struct amdgpu_device *adev);
237 static int gfx_v10_0_get_cu_info(struct amdgpu_device *adev,
238                                  struct amdgpu_cu_info *cu_info);
239 static uint64_t gfx_v10_0_get_gpu_clock_counter(struct amdgpu_device *adev);
240 static void gfx_v10_0_select_se_sh(struct amdgpu_device *adev, u32 se_num,
241                                    u32 sh_num, u32 instance);
242 static u32 gfx_v10_0_get_wgp_active_bitmap_per_sh(struct amdgpu_device *adev);
243
244 static int gfx_v10_0_rlc_backdoor_autoload_buffer_init(struct amdgpu_device *adev);
245 static void gfx_v10_0_rlc_backdoor_autoload_buffer_fini(struct amdgpu_device *adev);
246 static int gfx_v10_0_rlc_backdoor_autoload_enable(struct amdgpu_device *adev);
247 static int gfx_v10_0_wait_for_rlc_autoload_complete(struct amdgpu_device *adev);
248 static void gfx_v10_0_ring_emit_ce_meta(struct amdgpu_ring *ring, bool resume);
249 static void gfx_v10_0_ring_emit_de_meta(struct amdgpu_ring *ring, bool resume);
250 static void gfx_v10_0_ring_emit_tmz(struct amdgpu_ring *ring, bool start);
251
252 static void gfx10_kiq_set_resources(struct amdgpu_ring *kiq_ring, uint64_t queue_mask)
253 {
254         amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_SET_RESOURCES, 6));
255         amdgpu_ring_write(kiq_ring, PACKET3_SET_RESOURCES_VMID_MASK(0) |
256                           PACKET3_SET_RESOURCES_QUEUE_TYPE(0)); /* vmid_mask:0 queue_type:0 (KIQ) */
257         amdgpu_ring_write(kiq_ring, lower_32_bits(queue_mask)); /* queue mask lo */
258         amdgpu_ring_write(kiq_ring, upper_32_bits(queue_mask)); /* queue mask hi */
259         amdgpu_ring_write(kiq_ring, 0); /* gws mask lo */
260         amdgpu_ring_write(kiq_ring, 0); /* gws mask hi */
261         amdgpu_ring_write(kiq_ring, 0); /* oac mask */
262         amdgpu_ring_write(kiq_ring, 0); /* gds heap base:0, gds heap size:0 */
263 }
264
265 static void gfx10_kiq_map_queues(struct amdgpu_ring *kiq_ring,
266                                  struct amdgpu_ring *ring)
267 {
268         struct amdgpu_device *adev = kiq_ring->adev;
269         uint64_t mqd_addr = amdgpu_bo_gpu_offset(ring->mqd_obj);
270         uint64_t wptr_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
271         uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
272
273         amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_MAP_QUEUES, 5));
274         /* Q_sel:0, vmid:0, vidmem: 1, engine:0, num_Q:1*/
275         amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
276                           PACKET3_MAP_QUEUES_QUEUE_SEL(0) | /* Queue_Sel */
277                           PACKET3_MAP_QUEUES_VMID(0) | /* VMID */
278                           PACKET3_MAP_QUEUES_QUEUE(ring->queue) |
279                           PACKET3_MAP_QUEUES_PIPE(ring->pipe) |
280                           PACKET3_MAP_QUEUES_ME((ring->me == 1 ? 0 : 1)) |
281                           PACKET3_MAP_QUEUES_QUEUE_TYPE(0) | /*queue_type: normal compute queue */
282                           PACKET3_MAP_QUEUES_ALLOC_FORMAT(0) | /* alloc format: all_on_one_pipe */
283                           PACKET3_MAP_QUEUES_ENGINE_SEL(eng_sel) |
284                           PACKET3_MAP_QUEUES_NUM_QUEUES(1)); /* num_queues: must be 1 */
285         amdgpu_ring_write(kiq_ring, PACKET3_MAP_QUEUES_DOORBELL_OFFSET(ring->doorbell_index));
286         amdgpu_ring_write(kiq_ring, lower_32_bits(mqd_addr));
287         amdgpu_ring_write(kiq_ring, upper_32_bits(mqd_addr));
288         amdgpu_ring_write(kiq_ring, lower_32_bits(wptr_addr));
289         amdgpu_ring_write(kiq_ring, upper_32_bits(wptr_addr));
290 }
291
292 static void gfx10_kiq_unmap_queues(struct amdgpu_ring *kiq_ring,
293                                    struct amdgpu_ring *ring,
294                                    enum amdgpu_unmap_queues_action action,
295                                    u64 gpu_addr, u64 seq)
296 {
297         uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
298
299         amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_UNMAP_QUEUES, 4));
300         amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
301                           PACKET3_UNMAP_QUEUES_ACTION(action) |
302                           PACKET3_UNMAP_QUEUES_QUEUE_SEL(0) |
303                           PACKET3_UNMAP_QUEUES_ENGINE_SEL(eng_sel) |
304                           PACKET3_UNMAP_QUEUES_NUM_QUEUES(1));
305         amdgpu_ring_write(kiq_ring,
306                   PACKET3_UNMAP_QUEUES_DOORBELL_OFFSET0(ring->doorbell_index));
307
308         if (action == PREEMPT_QUEUES_NO_UNMAP) {
309                 amdgpu_ring_write(kiq_ring, lower_32_bits(gpu_addr));
310                 amdgpu_ring_write(kiq_ring, upper_32_bits(gpu_addr));
311                 amdgpu_ring_write(kiq_ring, seq);
312         } else {
313                 amdgpu_ring_write(kiq_ring, 0);
314                 amdgpu_ring_write(kiq_ring, 0);
315                 amdgpu_ring_write(kiq_ring, 0);
316         }
317 }
318
319 static void gfx10_kiq_query_status(struct amdgpu_ring *kiq_ring,
320                                    struct amdgpu_ring *ring,
321                                    u64 addr,
322                                    u64 seq)
323 {
324         uint32_t eng_sel = ring->funcs->type == AMDGPU_RING_TYPE_GFX ? 4 : 0;
325
326         amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_QUERY_STATUS, 5));
327         amdgpu_ring_write(kiq_ring,
328                           PACKET3_QUERY_STATUS_CONTEXT_ID(0) |
329                           PACKET3_QUERY_STATUS_INTERRUPT_SEL(0) |
330                           PACKET3_QUERY_STATUS_COMMAND(2));
331         amdgpu_ring_write(kiq_ring, /* Q_sel: 0, vmid: 0, engine: 0, num_Q: 1 */
332                           PACKET3_QUERY_STATUS_DOORBELL_OFFSET(ring->doorbell_index) |
333                           PACKET3_QUERY_STATUS_ENG_SEL(eng_sel));
334         amdgpu_ring_write(kiq_ring, lower_32_bits(addr));
335         amdgpu_ring_write(kiq_ring, upper_32_bits(addr));
336         amdgpu_ring_write(kiq_ring, lower_32_bits(seq));
337         amdgpu_ring_write(kiq_ring, upper_32_bits(seq));
338 }
339
340 static const struct kiq_pm4_funcs gfx_v10_0_kiq_pm4_funcs = {
341         .kiq_set_resources = gfx10_kiq_set_resources,
342         .kiq_map_queues = gfx10_kiq_map_queues,
343         .kiq_unmap_queues = gfx10_kiq_unmap_queues,
344         .kiq_query_status = gfx10_kiq_query_status,
345         .set_resources_size = 8,
346         .map_queues_size = 7,
347         .unmap_queues_size = 6,
348         .query_status_size = 7,
349 };
350
351 static void gfx_v10_0_set_kiq_pm4_funcs(struct amdgpu_device *adev)
352 {
353         adev->gfx.kiq.pmf = &gfx_v10_0_kiq_pm4_funcs;
354 }
355
356 static void gfx_v10_0_init_golden_registers(struct amdgpu_device *adev)
357 {
358         switch (adev->asic_type) {
359         case CHIP_NAVI10:
360                 soc15_program_register_sequence(adev,
361                                                 golden_settings_gc_10_1,
362                                                 (const u32)ARRAY_SIZE(golden_settings_gc_10_1));
363                 soc15_program_register_sequence(adev,
364                                                 golden_settings_gc_10_0_nv10,
365                                                 (const u32)ARRAY_SIZE(golden_settings_gc_10_0_nv10));
366                 break;
367         case CHIP_NAVI14:
368                 soc15_program_register_sequence(adev,
369                                                 golden_settings_gc_10_1_1,
370                                                 (const u32)ARRAY_SIZE(golden_settings_gc_10_1_1));
371                 soc15_program_register_sequence(adev,
372                                                 golden_settings_gc_10_1_nv14,
373                                                 (const u32)ARRAY_SIZE(golden_settings_gc_10_1_nv14));
374                 break;
375         case CHIP_NAVI12:
376                 soc15_program_register_sequence(adev,
377                                                 golden_settings_gc_10_1_2,
378                                                 (const u32)ARRAY_SIZE(golden_settings_gc_10_1_2));
379                 soc15_program_register_sequence(adev,
380                                                 golden_settings_gc_10_1_2_nv12,
381                                                 (const u32)ARRAY_SIZE(golden_settings_gc_10_1_2_nv12));
382                 break;
383         default:
384                 break;
385         }
386 }
387
388 static void gfx_v10_0_scratch_init(struct amdgpu_device *adev)
389 {
390         adev->gfx.scratch.num_reg = 8;
391         adev->gfx.scratch.reg_base = SOC15_REG_OFFSET(GC, 0, mmSCRATCH_REG0);
392         adev->gfx.scratch.free_mask = (1u << adev->gfx.scratch.num_reg) - 1;
393 }
394
395 static void gfx_v10_0_write_data_to_reg(struct amdgpu_ring *ring, int eng_sel,
396                                        bool wc, uint32_t reg, uint32_t val)
397 {
398         amdgpu_ring_write(ring, PACKET3(PACKET3_WRITE_DATA, 3));
399         amdgpu_ring_write(ring, WRITE_DATA_ENGINE_SEL(eng_sel) |
400                           WRITE_DATA_DST_SEL(0) | (wc ? WR_CONFIRM : 0));
401         amdgpu_ring_write(ring, reg);
402         amdgpu_ring_write(ring, 0);
403         amdgpu_ring_write(ring, val);
404 }
405
406 static void gfx_v10_0_wait_reg_mem(struct amdgpu_ring *ring, int eng_sel,
407                                   int mem_space, int opt, uint32_t addr0,
408                                   uint32_t addr1, uint32_t ref, uint32_t mask,
409                                   uint32_t inv)
410 {
411         amdgpu_ring_write(ring, PACKET3(PACKET3_WAIT_REG_MEM, 5));
412         amdgpu_ring_write(ring,
413                           /* memory (1) or register (0) */
414                           (WAIT_REG_MEM_MEM_SPACE(mem_space) |
415                            WAIT_REG_MEM_OPERATION(opt) | /* wait */
416                            WAIT_REG_MEM_FUNCTION(3) |  /* equal */
417                            WAIT_REG_MEM_ENGINE(eng_sel)));
418
419         if (mem_space)
420                 BUG_ON(addr0 & 0x3); /* Dword align */
421         amdgpu_ring_write(ring, addr0);
422         amdgpu_ring_write(ring, addr1);
423         amdgpu_ring_write(ring, ref);
424         amdgpu_ring_write(ring, mask);
425         amdgpu_ring_write(ring, inv); /* poll interval */
426 }
427
428 static int gfx_v10_0_ring_test_ring(struct amdgpu_ring *ring)
429 {
430         struct amdgpu_device *adev = ring->adev;
431         uint32_t scratch;
432         uint32_t tmp = 0;
433         unsigned i;
434         int r;
435
436         r = amdgpu_gfx_scratch_get(adev, &scratch);
437         if (r) {
438                 DRM_ERROR("amdgpu: cp failed to get scratch reg (%d).\n", r);
439                 return r;
440         }
441
442         WREG32(scratch, 0xCAFEDEAD);
443
444         r = amdgpu_ring_alloc(ring, 3);
445         if (r) {
446                 DRM_ERROR("amdgpu: cp failed to lock ring %d (%d).\n",
447                           ring->idx, r);
448                 amdgpu_gfx_scratch_free(adev, scratch);
449                 return r;
450         }
451
452         amdgpu_ring_write(ring, PACKET3(PACKET3_SET_UCONFIG_REG, 1));
453         amdgpu_ring_write(ring, (scratch - PACKET3_SET_UCONFIG_REG_START));
454         amdgpu_ring_write(ring, 0xDEADBEEF);
455         amdgpu_ring_commit(ring);
456
457         for (i = 0; i < adev->usec_timeout; i++) {
458                 tmp = RREG32(scratch);
459                 if (tmp == 0xDEADBEEF)
460                         break;
461                 if (amdgpu_emu_mode == 1)
462                         msleep(1);
463                 else
464                         udelay(1);
465         }
466         if (i < adev->usec_timeout) {
467                 if (amdgpu_emu_mode == 1)
468                         DRM_INFO("ring test on %d succeeded in %d msecs\n",
469                                  ring->idx, i);
470                 else
471                         DRM_INFO("ring test on %d succeeded in %d usecs\n",
472                                  ring->idx, i);
473         } else {
474                 DRM_ERROR("amdgpu: ring %d test failed (scratch(0x%04X)=0x%08X)\n",
475                           ring->idx, scratch, tmp);
476                 r = -EINVAL;
477         }
478         amdgpu_gfx_scratch_free(adev, scratch);
479
480         return r;
481 }
482
483 static int gfx_v10_0_ring_test_ib(struct amdgpu_ring *ring, long timeout)
484 {
485         struct amdgpu_device *adev = ring->adev;
486         struct amdgpu_ib ib;
487         struct dma_fence *f = NULL;
488         uint32_t scratch;
489         uint32_t tmp = 0;
490         long r;
491
492         r = amdgpu_gfx_scratch_get(adev, &scratch);
493         if (r) {
494                 DRM_ERROR("amdgpu: failed to get scratch reg (%ld).\n", r);
495                 return r;
496         }
497
498         WREG32(scratch, 0xCAFEDEAD);
499
500         memset(&ib, 0, sizeof(ib));
501         r = amdgpu_ib_get(adev, NULL, 256, &ib);
502         if (r) {
503                 DRM_ERROR("amdgpu: failed to get ib (%ld).\n", r);
504                 goto err1;
505         }
506
507         ib.ptr[0] = PACKET3(PACKET3_SET_UCONFIG_REG, 1);
508         ib.ptr[1] = ((scratch - PACKET3_SET_UCONFIG_REG_START));
509         ib.ptr[2] = 0xDEADBEEF;
510         ib.length_dw = 3;
511
512         r = amdgpu_ib_schedule(ring, 1, &ib, NULL, &f);
513         if (r)
514                 goto err2;
515
516         r = dma_fence_wait_timeout(f, false, timeout);
517         if (r == 0) {
518                 DRM_ERROR("amdgpu: IB test timed out.\n");
519                 r = -ETIMEDOUT;
520                 goto err2;
521         } else if (r < 0) {
522                 DRM_ERROR("amdgpu: fence wait failed (%ld).\n", r);
523                 goto err2;
524         }
525
526         tmp = RREG32(scratch);
527         if (tmp == 0xDEADBEEF) {
528                 DRM_INFO("ib test on ring %d succeeded\n", ring->idx);
529                 r = 0;
530         } else {
531                 DRM_ERROR("amdgpu: ib test failed (scratch(0x%04X)=0x%08X)\n",
532                           scratch, tmp);
533                 r = -EINVAL;
534         }
535 err2:
536         amdgpu_ib_free(adev, &ib, NULL);
537         dma_fence_put(f);
538 err1:
539         amdgpu_gfx_scratch_free(adev, scratch);
540
541         return r;
542 }
543
544 static void gfx_v10_0_free_microcode(struct amdgpu_device *adev)
545 {
546         release_firmware(adev->gfx.pfp_fw);
547         adev->gfx.pfp_fw = NULL;
548         release_firmware(adev->gfx.me_fw);
549         adev->gfx.me_fw = NULL;
550         release_firmware(adev->gfx.ce_fw);
551         adev->gfx.ce_fw = NULL;
552         release_firmware(adev->gfx.rlc_fw);
553         adev->gfx.rlc_fw = NULL;
554         release_firmware(adev->gfx.mec_fw);
555         adev->gfx.mec_fw = NULL;
556         release_firmware(adev->gfx.mec2_fw);
557         adev->gfx.mec2_fw = NULL;
558
559         kfree(adev->gfx.rlc.register_list_format);
560 }
561
562 static void gfx_v10_0_init_rlc_ext_microcode(struct amdgpu_device *adev)
563 {
564         const struct rlc_firmware_header_v2_1 *rlc_hdr;
565
566         rlc_hdr = (const struct rlc_firmware_header_v2_1 *)adev->gfx.rlc_fw->data;
567         adev->gfx.rlc_srlc_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_cntl_ucode_ver);
568         adev->gfx.rlc_srlc_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_cntl_feature_ver);
569         adev->gfx.rlc.save_restore_list_cntl_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_cntl_size_bytes);
570         adev->gfx.rlc.save_restore_list_cntl = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_cntl_offset_bytes);
571         adev->gfx.rlc_srlg_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_gpm_ucode_ver);
572         adev->gfx.rlc_srlg_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_gpm_feature_ver);
573         adev->gfx.rlc.save_restore_list_gpm_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_gpm_size_bytes);
574         adev->gfx.rlc.save_restore_list_gpm = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_gpm_offset_bytes);
575         adev->gfx.rlc_srls_fw_version = le32_to_cpu(rlc_hdr->save_restore_list_srm_ucode_ver);
576         adev->gfx.rlc_srls_feature_version = le32_to_cpu(rlc_hdr->save_restore_list_srm_feature_ver);
577         adev->gfx.rlc.save_restore_list_srm_size_bytes = le32_to_cpu(rlc_hdr->save_restore_list_srm_size_bytes);
578         adev->gfx.rlc.save_restore_list_srm = (u8 *)rlc_hdr + le32_to_cpu(rlc_hdr->save_restore_list_srm_offset_bytes);
579         adev->gfx.rlc.reg_list_format_direct_reg_list_length =
580                         le32_to_cpu(rlc_hdr->reg_list_format_direct_reg_list_length);
581 }
582
583 static void gfx_v10_0_check_gfxoff_flag(struct amdgpu_device *adev)
584 {
585         switch (adev->asic_type) {
586         case CHIP_NAVI10:
587                 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
588                 break;
589         default:
590                 break;
591         }
592 }
593
594 static int gfx_v10_0_init_microcode(struct amdgpu_device *adev)
595 {
596         const char *chip_name;
597         char fw_name[30];
598         int err;
599         struct amdgpu_firmware_info *info = NULL;
600         const struct common_firmware_header *header = NULL;
601         const struct gfx_firmware_header_v1_0 *cp_hdr;
602         const struct rlc_firmware_header_v2_0 *rlc_hdr;
603         unsigned int *tmp = NULL;
604         unsigned int i = 0;
605         uint16_t version_major;
606         uint16_t version_minor;
607
608         DRM_DEBUG("\n");
609
610         switch (adev->asic_type) {
611         case CHIP_NAVI10:
612                 chip_name = "navi10";
613                 break;
614         case CHIP_NAVI14:
615                 chip_name = "navi14";
616                 break;
617         case CHIP_NAVI12:
618                 chip_name = "navi12";
619                 break;
620         default:
621                 BUG();
622         }
623
624         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_pfp.bin", chip_name);
625         err = request_firmware(&adev->gfx.pfp_fw, fw_name, adev->dev);
626         if (err)
627                 goto out;
628         err = amdgpu_ucode_validate(adev->gfx.pfp_fw);
629         if (err)
630                 goto out;
631         cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.pfp_fw->data;
632         adev->gfx.pfp_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
633         adev->gfx.pfp_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
634
635         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_me.bin", chip_name);
636         err = request_firmware(&adev->gfx.me_fw, fw_name, adev->dev);
637         if (err)
638                 goto out;
639         err = amdgpu_ucode_validate(adev->gfx.me_fw);
640         if (err)
641                 goto out;
642         cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.me_fw->data;
643         adev->gfx.me_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
644         adev->gfx.me_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
645
646         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_ce.bin", chip_name);
647         err = request_firmware(&adev->gfx.ce_fw, fw_name, adev->dev);
648         if (err)
649                 goto out;
650         err = amdgpu_ucode_validate(adev->gfx.ce_fw);
651         if (err)
652                 goto out;
653         cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.ce_fw->data;
654         adev->gfx.ce_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
655         adev->gfx.ce_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
656
657         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_rlc.bin", chip_name);
658         err = request_firmware(&adev->gfx.rlc_fw, fw_name, adev->dev);
659         if (err)
660                 goto out;
661         err = amdgpu_ucode_validate(adev->gfx.rlc_fw);
662         rlc_hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
663         version_major = le16_to_cpu(rlc_hdr->header.header_version_major);
664         version_minor = le16_to_cpu(rlc_hdr->header.header_version_minor);
665         if (version_major == 2 && version_minor == 1)
666                 adev->gfx.rlc.is_rlc_v2_1 = true;
667
668         adev->gfx.rlc_fw_version = le32_to_cpu(rlc_hdr->header.ucode_version);
669         adev->gfx.rlc_feature_version = le32_to_cpu(rlc_hdr->ucode_feature_version);
670         adev->gfx.rlc.save_and_restore_offset =
671                         le32_to_cpu(rlc_hdr->save_and_restore_offset);
672         adev->gfx.rlc.clear_state_descriptor_offset =
673                         le32_to_cpu(rlc_hdr->clear_state_descriptor_offset);
674         adev->gfx.rlc.avail_scratch_ram_locations =
675                         le32_to_cpu(rlc_hdr->avail_scratch_ram_locations);
676         adev->gfx.rlc.reg_restore_list_size =
677                         le32_to_cpu(rlc_hdr->reg_restore_list_size);
678         adev->gfx.rlc.reg_list_format_start =
679                         le32_to_cpu(rlc_hdr->reg_list_format_start);
680         adev->gfx.rlc.reg_list_format_separate_start =
681                         le32_to_cpu(rlc_hdr->reg_list_format_separate_start);
682         adev->gfx.rlc.starting_offsets_start =
683                         le32_to_cpu(rlc_hdr->starting_offsets_start);
684         adev->gfx.rlc.reg_list_format_size_bytes =
685                         le32_to_cpu(rlc_hdr->reg_list_format_size_bytes);
686         adev->gfx.rlc.reg_list_size_bytes =
687                         le32_to_cpu(rlc_hdr->reg_list_size_bytes);
688         adev->gfx.rlc.register_list_format =
689                         kmalloc(adev->gfx.rlc.reg_list_format_size_bytes +
690                                 adev->gfx.rlc.reg_list_size_bytes, GFP_KERNEL);
691         if (!adev->gfx.rlc.register_list_format) {
692                 err = -ENOMEM;
693                 goto out;
694         }
695
696         tmp = (unsigned int *)((uintptr_t)rlc_hdr +
697                         le32_to_cpu(rlc_hdr->reg_list_format_array_offset_bytes));
698         for (i = 0 ; i < (rlc_hdr->reg_list_format_size_bytes >> 2); i++)
699                 adev->gfx.rlc.register_list_format[i] = le32_to_cpu(tmp[i]);
700
701         adev->gfx.rlc.register_restore = adev->gfx.rlc.register_list_format + i;
702
703         tmp = (unsigned int *)((uintptr_t)rlc_hdr +
704                         le32_to_cpu(rlc_hdr->reg_list_array_offset_bytes));
705         for (i = 0 ; i < (rlc_hdr->reg_list_size_bytes >> 2); i++)
706                 adev->gfx.rlc.register_restore[i] = le32_to_cpu(tmp[i]);
707
708         if (adev->gfx.rlc.is_rlc_v2_1)
709                 gfx_v10_0_init_rlc_ext_microcode(adev);
710
711         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mec.bin", chip_name);
712         err = request_firmware(&adev->gfx.mec_fw, fw_name, adev->dev);
713         if (err)
714                 goto out;
715         err = amdgpu_ucode_validate(adev->gfx.mec_fw);
716         if (err)
717                 goto out;
718         cp_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
719         adev->gfx.mec_fw_version = le32_to_cpu(cp_hdr->header.ucode_version);
720         adev->gfx.mec_feature_version = le32_to_cpu(cp_hdr->ucode_feature_version);
721
722         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_mec2.bin", chip_name);
723         err = request_firmware(&adev->gfx.mec2_fw, fw_name, adev->dev);
724         if (!err) {
725                 err = amdgpu_ucode_validate(adev->gfx.mec2_fw);
726                 if (err)
727                         goto out;
728                 cp_hdr = (const struct gfx_firmware_header_v1_0 *)
729                 adev->gfx.mec2_fw->data;
730                 adev->gfx.mec2_fw_version =
731                 le32_to_cpu(cp_hdr->header.ucode_version);
732                 adev->gfx.mec2_feature_version =
733                 le32_to_cpu(cp_hdr->ucode_feature_version);
734         } else {
735                 err = 0;
736                 adev->gfx.mec2_fw = NULL;
737         }
738
739         if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
740                 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_PFP];
741                 info->ucode_id = AMDGPU_UCODE_ID_CP_PFP;
742                 info->fw = adev->gfx.pfp_fw;
743                 header = (const struct common_firmware_header *)info->fw->data;
744                 adev->firmware.fw_size +=
745                         ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
746
747                 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_ME];
748                 info->ucode_id = AMDGPU_UCODE_ID_CP_ME;
749                 info->fw = adev->gfx.me_fw;
750                 header = (const struct common_firmware_header *)info->fw->data;
751                 adev->firmware.fw_size +=
752                         ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
753
754                 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_CE];
755                 info->ucode_id = AMDGPU_UCODE_ID_CP_CE;
756                 info->fw = adev->gfx.ce_fw;
757                 header = (const struct common_firmware_header *)info->fw->data;
758                 adev->firmware.fw_size +=
759                         ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
760
761                 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_G];
762                 info->ucode_id = AMDGPU_UCODE_ID_RLC_G;
763                 info->fw = adev->gfx.rlc_fw;
764                 header = (const struct common_firmware_header *)info->fw->data;
765                 adev->firmware.fw_size +=
766                         ALIGN(le32_to_cpu(header->ucode_size_bytes), PAGE_SIZE);
767
768                 if (adev->gfx.rlc.is_rlc_v2_1 &&
769                     adev->gfx.rlc.save_restore_list_cntl_size_bytes &&
770                     adev->gfx.rlc.save_restore_list_gpm_size_bytes &&
771                     adev->gfx.rlc.save_restore_list_srm_size_bytes) {
772                         info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL];
773                         info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL;
774                         info->fw = adev->gfx.rlc_fw;
775                         adev->firmware.fw_size +=
776                                 ALIGN(adev->gfx.rlc.save_restore_list_cntl_size_bytes, PAGE_SIZE);
777
778                         info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM];
779                         info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM;
780                         info->fw = adev->gfx.rlc_fw;
781                         adev->firmware.fw_size +=
782                                 ALIGN(adev->gfx.rlc.save_restore_list_gpm_size_bytes, PAGE_SIZE);
783
784                         info = &adev->firmware.ucode[AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM];
785                         info->ucode_id = AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM;
786                         info->fw = adev->gfx.rlc_fw;
787                         adev->firmware.fw_size +=
788                                 ALIGN(adev->gfx.rlc.save_restore_list_srm_size_bytes, PAGE_SIZE);
789                 }
790
791                 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC1];
792                 info->ucode_id = AMDGPU_UCODE_ID_CP_MEC1;
793                 info->fw = adev->gfx.mec_fw;
794                 header = (const struct common_firmware_header *)info->fw->data;
795                 cp_hdr = (const struct gfx_firmware_header_v1_0 *)info->fw->data;
796                 adev->firmware.fw_size +=
797                         ALIGN(le32_to_cpu(header->ucode_size_bytes) -
798                               le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
799
800                 info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC1_JT];
801                 info->ucode_id = AMDGPU_UCODE_ID_CP_MEC1_JT;
802                 info->fw = adev->gfx.mec_fw;
803                 adev->firmware.fw_size +=
804                         ALIGN(le32_to_cpu(cp_hdr->jt_size) * 4, PAGE_SIZE);
805
806                 if (adev->gfx.mec2_fw) {
807                         info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC2];
808                         info->ucode_id = AMDGPU_UCODE_ID_CP_MEC2;
809                         info->fw = adev->gfx.mec2_fw;
810                         header = (const struct common_firmware_header *)info->fw->data;
811                         cp_hdr = (const struct gfx_firmware_header_v1_0 *)info->fw->data;
812                         adev->firmware.fw_size +=
813                                 ALIGN(le32_to_cpu(header->ucode_size_bytes) -
814                                       le32_to_cpu(cp_hdr->jt_size) * 4,
815                                       PAGE_SIZE);
816                         info = &adev->firmware.ucode[AMDGPU_UCODE_ID_CP_MEC2_JT];
817                         info->ucode_id = AMDGPU_UCODE_ID_CP_MEC2_JT;
818                         info->fw = adev->gfx.mec2_fw;
819                         adev->firmware.fw_size +=
820                                 ALIGN(le32_to_cpu(cp_hdr->jt_size) * 4,
821                                       PAGE_SIZE);
822                 }
823         }
824
825 out:
826         if (err) {
827                 dev_err(adev->dev,
828                         "gfx10: Failed to load firmware \"%s\"\n",
829                         fw_name);
830                 release_firmware(adev->gfx.pfp_fw);
831                 adev->gfx.pfp_fw = NULL;
832                 release_firmware(adev->gfx.me_fw);
833                 adev->gfx.me_fw = NULL;
834                 release_firmware(adev->gfx.ce_fw);
835                 adev->gfx.ce_fw = NULL;
836                 release_firmware(adev->gfx.rlc_fw);
837                 adev->gfx.rlc_fw = NULL;
838                 release_firmware(adev->gfx.mec_fw);
839                 adev->gfx.mec_fw = NULL;
840                 release_firmware(adev->gfx.mec2_fw);
841                 adev->gfx.mec2_fw = NULL;
842         }
843
844         gfx_v10_0_check_gfxoff_flag(adev);
845
846         return err;
847 }
848
849 static u32 gfx_v10_0_get_csb_size(struct amdgpu_device *adev)
850 {
851         u32 count = 0;
852         const struct cs_section_def *sect = NULL;
853         const struct cs_extent_def *ext = NULL;
854
855         /* begin clear state */
856         count += 2;
857         /* context control state */
858         count += 3;
859
860         for (sect = gfx10_cs_data; sect->section != NULL; ++sect) {
861                 for (ext = sect->section; ext->extent != NULL; ++ext) {
862                         if (sect->id == SECT_CONTEXT)
863                                 count += 2 + ext->reg_count;
864                         else
865                                 return 0;
866                 }
867         }
868
869         /* set PA_SC_TILE_STEERING_OVERRIDE */
870         count += 3;
871         /* end clear state */
872         count += 2;
873         /* clear state */
874         count += 2;
875
876         return count;
877 }
878
879 static void gfx_v10_0_get_csb_buffer(struct amdgpu_device *adev,
880                                     volatile u32 *buffer)
881 {
882         u32 count = 0, i;
883         const struct cs_section_def *sect = NULL;
884         const struct cs_extent_def *ext = NULL;
885         int ctx_reg_offset;
886
887         if (adev->gfx.rlc.cs_data == NULL)
888                 return;
889         if (buffer == NULL)
890                 return;
891
892         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
893         buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
894
895         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CONTEXT_CONTROL, 1));
896         buffer[count++] = cpu_to_le32(0x80000000);
897         buffer[count++] = cpu_to_le32(0x80000000);
898
899         for (sect = adev->gfx.rlc.cs_data; sect->section != NULL; ++sect) {
900                 for (ext = sect->section; ext->extent != NULL; ++ext) {
901                         if (sect->id == SECT_CONTEXT) {
902                                 buffer[count++] =
903                                         cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, ext->reg_count));
904                                 buffer[count++] = cpu_to_le32(ext->reg_index -
905                                                 PACKET3_SET_CONTEXT_REG_START);
906                                 for (i = 0; i < ext->reg_count; i++)
907                                         buffer[count++] = cpu_to_le32(ext->extent[i]);
908                         } else {
909                                 return;
910                         }
911                 }
912         }
913
914         ctx_reg_offset =
915                 SOC15_REG_OFFSET(GC, 0, mmPA_SC_TILE_STEERING_OVERRIDE) - PACKET3_SET_CONTEXT_REG_START;
916         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_SET_CONTEXT_REG, 1));
917         buffer[count++] = cpu_to_le32(ctx_reg_offset);
918         buffer[count++] = cpu_to_le32(adev->gfx.config.pa_sc_tile_steering_override);
919
920         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_PREAMBLE_CNTL, 0));
921         buffer[count++] = cpu_to_le32(PACKET3_PREAMBLE_END_CLEAR_STATE);
922
923         buffer[count++] = cpu_to_le32(PACKET3(PACKET3_CLEAR_STATE, 0));
924         buffer[count++] = cpu_to_le32(0);
925 }
926
927 static void gfx_v10_0_rlc_fini(struct amdgpu_device *adev)
928 {
929         /* clear state block */
930         amdgpu_bo_free_kernel(&adev->gfx.rlc.clear_state_obj,
931                         &adev->gfx.rlc.clear_state_gpu_addr,
932                         (void **)&adev->gfx.rlc.cs_ptr);
933
934         /* jump table block */
935         amdgpu_bo_free_kernel(&adev->gfx.rlc.cp_table_obj,
936                         &adev->gfx.rlc.cp_table_gpu_addr,
937                         (void **)&adev->gfx.rlc.cp_table_ptr);
938 }
939
940 static int gfx_v10_0_rlc_init(struct amdgpu_device *adev)
941 {
942         const struct cs_section_def *cs_data;
943         int r;
944
945         adev->gfx.rlc.cs_data = gfx10_cs_data;
946
947         cs_data = adev->gfx.rlc.cs_data;
948
949         if (cs_data) {
950                 /* init clear state block */
951                 r = amdgpu_gfx_rlc_init_csb(adev);
952                 if (r)
953                         return r;
954         }
955
956         return 0;
957 }
958
959 static int gfx_v10_0_csb_vram_pin(struct amdgpu_device *adev)
960 {
961         int r;
962
963         r = amdgpu_bo_reserve(adev->gfx.rlc.clear_state_obj, false);
964         if (unlikely(r != 0))
965                 return r;
966
967         r = amdgpu_bo_pin(adev->gfx.rlc.clear_state_obj,
968                         AMDGPU_GEM_DOMAIN_VRAM);
969         if (!r)
970                 adev->gfx.rlc.clear_state_gpu_addr =
971                         amdgpu_bo_gpu_offset(adev->gfx.rlc.clear_state_obj);
972
973         amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
974
975         return r;
976 }
977
978 static void gfx_v10_0_csb_vram_unpin(struct amdgpu_device *adev)
979 {
980         int r;
981
982         if (!adev->gfx.rlc.clear_state_obj)
983                 return;
984
985         r = amdgpu_bo_reserve(adev->gfx.rlc.clear_state_obj, true);
986         if (likely(r == 0)) {
987                 amdgpu_bo_unpin(adev->gfx.rlc.clear_state_obj);
988                 amdgpu_bo_unreserve(adev->gfx.rlc.clear_state_obj);
989         }
990 }
991
992 static void gfx_v10_0_mec_fini(struct amdgpu_device *adev)
993 {
994         amdgpu_bo_free_kernel(&adev->gfx.mec.hpd_eop_obj, NULL, NULL);
995         amdgpu_bo_free_kernel(&adev->gfx.mec.mec_fw_obj, NULL, NULL);
996 }
997
998 static int gfx_v10_0_me_init(struct amdgpu_device *adev)
999 {
1000         int r;
1001
1002         bitmap_zero(adev->gfx.me.queue_bitmap, AMDGPU_MAX_GFX_QUEUES);
1003
1004         amdgpu_gfx_graphics_queue_acquire(adev);
1005
1006         r = gfx_v10_0_init_microcode(adev);
1007         if (r)
1008                 DRM_ERROR("Failed to load gfx firmware!\n");
1009
1010         return r;
1011 }
1012
1013 static int gfx_v10_0_mec_init(struct amdgpu_device *adev)
1014 {
1015         int r;
1016         u32 *hpd;
1017         const __le32 *fw_data = NULL;
1018         unsigned fw_size;
1019         u32 *fw = NULL;
1020         size_t mec_hpd_size;
1021
1022         const struct gfx_firmware_header_v1_0 *mec_hdr = NULL;
1023
1024         bitmap_zero(adev->gfx.mec.queue_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
1025
1026         /* take ownership of the relevant compute queues */
1027         amdgpu_gfx_compute_queue_acquire(adev);
1028         mec_hpd_size = adev->gfx.num_compute_rings * GFX10_MEC_HPD_SIZE;
1029
1030         r = amdgpu_bo_create_reserved(adev, mec_hpd_size, PAGE_SIZE,
1031                                       AMDGPU_GEM_DOMAIN_GTT,
1032                                       &adev->gfx.mec.hpd_eop_obj,
1033                                       &adev->gfx.mec.hpd_eop_gpu_addr,
1034                                       (void **)&hpd);
1035         if (r) {
1036                 dev_warn(adev->dev, "(%d) create HDP EOP bo failed\n", r);
1037                 gfx_v10_0_mec_fini(adev);
1038                 return r;
1039         }
1040
1041         memset(hpd, 0, adev->gfx.mec.hpd_eop_obj->tbo.mem.size);
1042
1043         amdgpu_bo_kunmap(adev->gfx.mec.hpd_eop_obj);
1044         amdgpu_bo_unreserve(adev->gfx.mec.hpd_eop_obj);
1045
1046         if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
1047                 mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
1048
1049                 fw_data = (const __le32 *) (adev->gfx.mec_fw->data +
1050                          le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
1051                 fw_size = le32_to_cpu(mec_hdr->header.ucode_size_bytes);
1052
1053                 r = amdgpu_bo_create_reserved(adev, mec_hdr->header.ucode_size_bytes,
1054                                               PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1055                                               &adev->gfx.mec.mec_fw_obj,
1056                                               &adev->gfx.mec.mec_fw_gpu_addr,
1057                                               (void **)&fw);
1058                 if (r) {
1059                         dev_err(adev->dev, "(%d) failed to create mec fw bo\n", r);
1060                         gfx_v10_0_mec_fini(adev);
1061                         return r;
1062                 }
1063
1064                 memcpy(fw, fw_data, fw_size);
1065
1066                 amdgpu_bo_kunmap(adev->gfx.mec.mec_fw_obj);
1067                 amdgpu_bo_unreserve(adev->gfx.mec.mec_fw_obj);
1068         }
1069
1070         return 0;
1071 }
1072
1073 static uint32_t wave_read_ind(struct amdgpu_device *adev, uint32_t wave, uint32_t address)
1074 {
1075         WREG32_SOC15(GC, 0, mmSQ_IND_INDEX,
1076                 (wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
1077                 (address << SQ_IND_INDEX__INDEX__SHIFT));
1078         return RREG32_SOC15(GC, 0, mmSQ_IND_DATA);
1079 }
1080
1081 static void wave_read_regs(struct amdgpu_device *adev, uint32_t wave,
1082                            uint32_t thread, uint32_t regno,
1083                            uint32_t num, uint32_t *out)
1084 {
1085         WREG32_SOC15(GC, 0, mmSQ_IND_INDEX,
1086                 (wave << SQ_IND_INDEX__WAVE_ID__SHIFT) |
1087                 (regno << SQ_IND_INDEX__INDEX__SHIFT) |
1088                 (thread << SQ_IND_INDEX__WORKITEM_ID__SHIFT) |
1089                 (SQ_IND_INDEX__AUTO_INCR_MASK));
1090         while (num--)
1091                 *(out++) = RREG32_SOC15(GC, 0, mmSQ_IND_DATA);
1092 }
1093
1094 static void gfx_v10_0_read_wave_data(struct amdgpu_device *adev, uint32_t simd, uint32_t wave, uint32_t *dst, int *no_fields)
1095 {
1096         /* in gfx10 the SIMD_ID is specified as part of the INSTANCE
1097          * field when performing a select_se_sh so it should be
1098          * zero here */
1099         WARN_ON(simd != 0);
1100
1101         /* type 2 wave data */
1102         dst[(*no_fields)++] = 2;
1103         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_STATUS);
1104         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_PC_LO);
1105         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_PC_HI);
1106         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_EXEC_LO);
1107         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_EXEC_HI);
1108         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_HW_ID1);
1109         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_HW_ID2);
1110         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_INST_DW0);
1111         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_GPR_ALLOC);
1112         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_LDS_ALLOC);
1113         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_TRAPSTS);
1114         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_IB_STS);
1115         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_IB_STS2);
1116         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_IB_DBG1);
1117         dst[(*no_fields)++] = wave_read_ind(adev, wave, ixSQ_WAVE_M0);
1118 }
1119
1120 static void gfx_v10_0_read_wave_sgprs(struct amdgpu_device *adev, uint32_t simd,
1121                                      uint32_t wave, uint32_t start,
1122                                      uint32_t size, uint32_t *dst)
1123 {
1124         WARN_ON(simd != 0);
1125
1126         wave_read_regs(
1127                 adev, wave, 0, start + SQIND_WAVE_SGPRS_OFFSET, size,
1128                 dst);
1129 }
1130
1131 static void gfx_v10_0_read_wave_vgprs(struct amdgpu_device *adev, uint32_t simd,
1132                                       uint32_t wave, uint32_t thread,
1133                                       uint32_t start, uint32_t size,
1134                                       uint32_t *dst)
1135 {
1136         wave_read_regs(
1137                 adev, wave, thread,
1138                 start + SQIND_WAVE_VGPRS_OFFSET, size, dst);
1139 }
1140
1141 static void gfx_v10_0_select_me_pipe_q(struct amdgpu_device *adev,
1142                                                                           u32 me, u32 pipe, u32 q, u32 vm)
1143  {
1144        nv_grbm_select(adev, me, pipe, q, vm);
1145  }
1146
1147
1148 static const struct amdgpu_gfx_funcs gfx_v10_0_gfx_funcs = {
1149         .get_gpu_clock_counter = &gfx_v10_0_get_gpu_clock_counter,
1150         .select_se_sh = &gfx_v10_0_select_se_sh,
1151         .read_wave_data = &gfx_v10_0_read_wave_data,
1152         .read_wave_sgprs = &gfx_v10_0_read_wave_sgprs,
1153         .read_wave_vgprs = &gfx_v10_0_read_wave_vgprs,
1154         .select_me_pipe_q = &gfx_v10_0_select_me_pipe_q,
1155 };
1156
1157 static void gfx_v10_0_gpu_early_init(struct amdgpu_device *adev)
1158 {
1159         u32 gb_addr_config;
1160
1161         adev->gfx.funcs = &gfx_v10_0_gfx_funcs;
1162
1163         switch (adev->asic_type) {
1164         case CHIP_NAVI10:
1165         case CHIP_NAVI14:
1166         case CHIP_NAVI12:
1167                 adev->gfx.config.max_hw_contexts = 8;
1168                 adev->gfx.config.sc_prim_fifo_size_frontend = 0x20;
1169                 adev->gfx.config.sc_prim_fifo_size_backend = 0x100;
1170                 adev->gfx.config.sc_hiz_tile_fifo_size = 0;
1171                 adev->gfx.config.sc_earlyz_tile_fifo_size = 0x4C0;
1172                 gb_addr_config = RREG32_SOC15(GC, 0, mmGB_ADDR_CONFIG);
1173                 break;
1174         default:
1175                 BUG();
1176                 break;
1177         }
1178
1179         adev->gfx.config.gb_addr_config = gb_addr_config;
1180
1181         adev->gfx.config.gb_addr_config_fields.num_pipes = 1 <<
1182                         REG_GET_FIELD(adev->gfx.config.gb_addr_config,
1183                                       GB_ADDR_CONFIG, NUM_PIPES);
1184
1185         adev->gfx.config.max_tile_pipes =
1186                 adev->gfx.config.gb_addr_config_fields.num_pipes;
1187
1188         adev->gfx.config.gb_addr_config_fields.max_compress_frags = 1 <<
1189                         REG_GET_FIELD(adev->gfx.config.gb_addr_config,
1190                                       GB_ADDR_CONFIG, MAX_COMPRESSED_FRAGS);
1191         adev->gfx.config.gb_addr_config_fields.num_rb_per_se = 1 <<
1192                         REG_GET_FIELD(adev->gfx.config.gb_addr_config,
1193                                       GB_ADDR_CONFIG, NUM_RB_PER_SE);
1194         adev->gfx.config.gb_addr_config_fields.num_se = 1 <<
1195                         REG_GET_FIELD(adev->gfx.config.gb_addr_config,
1196                                       GB_ADDR_CONFIG, NUM_SHADER_ENGINES);
1197         adev->gfx.config.gb_addr_config_fields.pipe_interleave_size = 1 << (8 +
1198                         REG_GET_FIELD(adev->gfx.config.gb_addr_config,
1199                                       GB_ADDR_CONFIG, PIPE_INTERLEAVE_SIZE));
1200 }
1201
1202 static int gfx_v10_0_gfx_ring_init(struct amdgpu_device *adev, int ring_id,
1203                                    int me, int pipe, int queue)
1204 {
1205         int r;
1206         struct amdgpu_ring *ring;
1207         unsigned int irq_type;
1208
1209         ring = &adev->gfx.gfx_ring[ring_id];
1210
1211         ring->me = me;
1212         ring->pipe = pipe;
1213         ring->queue = queue;
1214
1215         ring->ring_obj = NULL;
1216         ring->use_doorbell = true;
1217
1218         if (!ring_id)
1219                 ring->doorbell_index = adev->doorbell_index.gfx_ring0 << 1;
1220         else
1221                 ring->doorbell_index = adev->doorbell_index.gfx_ring1 << 1;
1222         sprintf(ring->name, "gfx_%d.%d.%d", ring->me, ring->pipe, ring->queue);
1223
1224         irq_type = AMDGPU_CP_IRQ_GFX_ME0_PIPE0_EOP + ring->pipe;
1225         r = amdgpu_ring_init(adev, ring, 1024,
1226                              &adev->gfx.eop_irq, irq_type);
1227         if (r)
1228                 return r;
1229         return 0;
1230 }
1231
1232 static int gfx_v10_0_compute_ring_init(struct amdgpu_device *adev, int ring_id,
1233                                        int mec, int pipe, int queue)
1234 {
1235         int r;
1236         unsigned irq_type;
1237         struct amdgpu_ring *ring = &adev->gfx.compute_ring[ring_id];
1238
1239         ring = &adev->gfx.compute_ring[ring_id];
1240
1241         /* mec0 is me1 */
1242         ring->me = mec + 1;
1243         ring->pipe = pipe;
1244         ring->queue = queue;
1245
1246         ring->ring_obj = NULL;
1247         ring->use_doorbell = true;
1248         ring->doorbell_index = (adev->doorbell_index.mec_ring0 + ring_id) << 1;
1249         ring->eop_gpu_addr = adev->gfx.mec.hpd_eop_gpu_addr
1250                                 + (ring_id * GFX10_MEC_HPD_SIZE);
1251         sprintf(ring->name, "comp_%d.%d.%d", ring->me, ring->pipe, ring->queue);
1252
1253         irq_type = AMDGPU_CP_IRQ_COMPUTE_MEC1_PIPE0_EOP
1254                 + ((ring->me - 1) * adev->gfx.mec.num_pipe_per_mec)
1255                 + ring->pipe;
1256
1257         /* type-2 packets are deprecated on MEC, use type-3 instead */
1258         r = amdgpu_ring_init(adev, ring, 1024,
1259                              &adev->gfx.eop_irq, irq_type);
1260         if (r)
1261                 return r;
1262
1263         return 0;
1264 }
1265
1266 static int gfx_v10_0_sw_init(void *handle)
1267 {
1268         int i, j, k, r, ring_id = 0;
1269         struct amdgpu_kiq *kiq;
1270         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1271
1272         switch (adev->asic_type) {
1273         case CHIP_NAVI10:
1274         case CHIP_NAVI14:
1275         case CHIP_NAVI12:
1276                 adev->gfx.me.num_me = 1;
1277                 adev->gfx.me.num_pipe_per_me = 2;
1278                 adev->gfx.me.num_queue_per_pipe = 1;
1279                 adev->gfx.mec.num_mec = 2;
1280                 adev->gfx.mec.num_pipe_per_mec = 4;
1281                 adev->gfx.mec.num_queue_per_pipe = 8;
1282                 break;
1283         default:
1284                 adev->gfx.me.num_me = 1;
1285                 adev->gfx.me.num_pipe_per_me = 1;
1286                 adev->gfx.me.num_queue_per_pipe = 1;
1287                 adev->gfx.mec.num_mec = 1;
1288                 adev->gfx.mec.num_pipe_per_mec = 4;
1289                 adev->gfx.mec.num_queue_per_pipe = 8;
1290                 break;
1291         }
1292
1293         /* KIQ event */
1294         r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP,
1295                               GFX_10_1__SRCID__CP_IB2_INTERRUPT_PKT,
1296                               &adev->gfx.kiq.irq);
1297         if (r)
1298                 return r;
1299
1300         /* EOP Event */
1301         r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP,
1302                               GFX_10_1__SRCID__CP_EOP_INTERRUPT,
1303                               &adev->gfx.eop_irq);
1304         if (r)
1305                 return r;
1306
1307         /* Privileged reg */
1308         r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_10_1__SRCID__CP_PRIV_REG_FAULT,
1309                               &adev->gfx.priv_reg_irq);
1310         if (r)
1311                 return r;
1312
1313         /* Privileged inst */
1314         r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_GRBM_CP, GFX_10_1__SRCID__CP_PRIV_INSTR_FAULT,
1315                               &adev->gfx.priv_inst_irq);
1316         if (r)
1317                 return r;
1318
1319         adev->gfx.gfx_current_status = AMDGPU_GFX_NORMAL_MODE;
1320
1321         gfx_v10_0_scratch_init(adev);
1322
1323         r = gfx_v10_0_me_init(adev);
1324         if (r)
1325                 return r;
1326
1327         r = gfx_v10_0_rlc_init(adev);
1328         if (r) {
1329                 DRM_ERROR("Failed to init rlc BOs!\n");
1330                 return r;
1331         }
1332
1333         r = gfx_v10_0_mec_init(adev);
1334         if (r) {
1335                 DRM_ERROR("Failed to init MEC BOs!\n");
1336                 return r;
1337         }
1338
1339         /* set up the gfx ring */
1340         for (i = 0; i < adev->gfx.me.num_me; i++) {
1341                 for (j = 0; j < adev->gfx.me.num_queue_per_pipe; j++) {
1342                         for (k = 0; k < adev->gfx.me.num_pipe_per_me; k++) {
1343                                 if (!amdgpu_gfx_is_me_queue_enabled(adev, i, k, j))
1344                                         continue;
1345
1346                                 r = gfx_v10_0_gfx_ring_init(adev, ring_id,
1347                                                             i, k, j);
1348                                 if (r)
1349                                         return r;
1350                                 ring_id++;
1351                         }
1352                 }
1353         }
1354
1355         ring_id = 0;
1356         /* set up the compute queues - allocate horizontally across pipes */
1357         for (i = 0; i < adev->gfx.mec.num_mec; ++i) {
1358                 for (j = 0; j < adev->gfx.mec.num_queue_per_pipe; j++) {
1359                         for (k = 0; k < adev->gfx.mec.num_pipe_per_mec; k++) {
1360                                 if (!amdgpu_gfx_is_mec_queue_enabled(adev, i, k,
1361                                                                      j))
1362                                         continue;
1363
1364                                 r = gfx_v10_0_compute_ring_init(adev, ring_id,
1365                                                                 i, k, j);
1366                                 if (r)
1367                                         return r;
1368
1369                                 ring_id++;
1370                         }
1371                 }
1372         }
1373
1374         r = amdgpu_gfx_kiq_init(adev, GFX10_MEC_HPD_SIZE);
1375         if (r) {
1376                 DRM_ERROR("Failed to init KIQ BOs!\n");
1377                 return r;
1378         }
1379
1380         kiq = &adev->gfx.kiq;
1381         r = amdgpu_gfx_kiq_init_ring(adev, &kiq->ring, &kiq->irq);
1382         if (r)
1383                 return r;
1384
1385         r = amdgpu_gfx_mqd_sw_init(adev, sizeof(struct v10_compute_mqd));
1386         if (r)
1387                 return r;
1388
1389         /* allocate visible FB for rlc auto-loading fw */
1390         if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) {
1391                 r = gfx_v10_0_rlc_backdoor_autoload_buffer_init(adev);
1392                 if (r)
1393                         return r;
1394         }
1395
1396         adev->gfx.ce_ram_size = F32_CE_PROGRAM_RAM_SIZE;
1397
1398         gfx_v10_0_gpu_early_init(adev);
1399
1400         return 0;
1401 }
1402
1403 static void gfx_v10_0_pfp_fini(struct amdgpu_device *adev)
1404 {
1405         amdgpu_bo_free_kernel(&adev->gfx.pfp.pfp_fw_obj,
1406                               &adev->gfx.pfp.pfp_fw_gpu_addr,
1407                               (void **)&adev->gfx.pfp.pfp_fw_ptr);
1408 }
1409
1410 static void gfx_v10_0_ce_fini(struct amdgpu_device *adev)
1411 {
1412         amdgpu_bo_free_kernel(&adev->gfx.ce.ce_fw_obj,
1413                               &adev->gfx.ce.ce_fw_gpu_addr,
1414                               (void **)&adev->gfx.ce.ce_fw_ptr);
1415 }
1416
1417 static void gfx_v10_0_me_fini(struct amdgpu_device *adev)
1418 {
1419         amdgpu_bo_free_kernel(&adev->gfx.me.me_fw_obj,
1420                               &adev->gfx.me.me_fw_gpu_addr,
1421                               (void **)&adev->gfx.me.me_fw_ptr);
1422 }
1423
1424 static int gfx_v10_0_sw_fini(void *handle)
1425 {
1426         int i;
1427         struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1428
1429         for (i = 0; i < adev->gfx.num_gfx_rings; i++)
1430                 amdgpu_ring_fini(&adev->gfx.gfx_ring[i]);
1431         for (i = 0; i < adev->gfx.num_compute_rings; i++)
1432                 amdgpu_ring_fini(&adev->gfx.compute_ring[i]);
1433
1434         amdgpu_gfx_mqd_sw_fini(adev);
1435         amdgpu_gfx_kiq_free_ring(&adev->gfx.kiq.ring, &adev->gfx.kiq.irq);
1436         amdgpu_gfx_kiq_fini(adev);
1437
1438         gfx_v10_0_pfp_fini(adev);
1439         gfx_v10_0_ce_fini(adev);
1440         gfx_v10_0_me_fini(adev);
1441         gfx_v10_0_rlc_fini(adev);
1442         gfx_v10_0_mec_fini(adev);
1443
1444         if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO)
1445                 gfx_v10_0_rlc_backdoor_autoload_buffer_fini(adev);
1446
1447         gfx_v10_0_free_microcode(adev);
1448
1449         return 0;
1450 }
1451
1452
1453 static void gfx_v10_0_tiling_mode_table_init(struct amdgpu_device *adev)
1454 {
1455         /* TODO */
1456 }
1457
1458 static void gfx_v10_0_select_se_sh(struct amdgpu_device *adev, u32 se_num,
1459                                    u32 sh_num, u32 instance)
1460 {
1461         u32 data;
1462
1463         if (instance == 0xffffffff)
1464                 data = REG_SET_FIELD(0, GRBM_GFX_INDEX,
1465                                      INSTANCE_BROADCAST_WRITES, 1);
1466         else
1467                 data = REG_SET_FIELD(0, GRBM_GFX_INDEX, INSTANCE_INDEX,
1468                                      instance);
1469
1470         if (se_num == 0xffffffff)
1471                 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_BROADCAST_WRITES,
1472                                      1);
1473         else
1474                 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SE_INDEX, se_num);
1475
1476         if (sh_num == 0xffffffff)
1477                 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SA_BROADCAST_WRITES,
1478                                      1);
1479         else
1480                 data = REG_SET_FIELD(data, GRBM_GFX_INDEX, SA_INDEX, sh_num);
1481
1482         WREG32_SOC15(GC, 0, mmGRBM_GFX_INDEX, data);
1483 }
1484
1485 static u32 gfx_v10_0_get_rb_active_bitmap(struct amdgpu_device *adev)
1486 {
1487         u32 data, mask;
1488
1489         data = RREG32_SOC15(GC, 0, mmCC_RB_BACKEND_DISABLE);
1490         data |= RREG32_SOC15(GC, 0, mmGC_USER_RB_BACKEND_DISABLE);
1491
1492         data &= CC_RB_BACKEND_DISABLE__BACKEND_DISABLE_MASK;
1493         data >>= GC_USER_RB_BACKEND_DISABLE__BACKEND_DISABLE__SHIFT;
1494
1495         mask = amdgpu_gfx_create_bitmask(adev->gfx.config.max_backends_per_se /
1496                                          adev->gfx.config.max_sh_per_se);
1497
1498         return (~data) & mask;
1499 }
1500
1501 static void gfx_v10_0_setup_rb(struct amdgpu_device *adev)
1502 {
1503         int i, j;
1504         u32 data;
1505         u32 active_rbs = 0;
1506         u32 rb_bitmap_width_per_sh = adev->gfx.config.max_backends_per_se /
1507                                         adev->gfx.config.max_sh_per_se;
1508
1509         mutex_lock(&adev->grbm_idx_mutex);
1510         for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
1511                 for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
1512                         gfx_v10_0_select_se_sh(adev, i, j, 0xffffffff);
1513                         data = gfx_v10_0_get_rb_active_bitmap(adev);
1514                         active_rbs |= data << ((i * adev->gfx.config.max_sh_per_se + j) *
1515                                                rb_bitmap_width_per_sh);
1516                 }
1517         }
1518         gfx_v10_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
1519         mutex_unlock(&adev->grbm_idx_mutex);
1520
1521         adev->gfx.config.backend_enable_mask = active_rbs;
1522         adev->gfx.config.num_rbs = hweight32(active_rbs);
1523 }
1524
1525 static u32 gfx_v10_0_init_pa_sc_tile_steering_override(struct amdgpu_device *adev)
1526 {
1527         uint32_t num_sc;
1528         uint32_t enabled_rb_per_sh;
1529         uint32_t active_rb_bitmap;
1530         uint32_t num_rb_per_sc;
1531         uint32_t num_packer_per_sc;
1532         uint32_t pa_sc_tile_steering_override;
1533
1534         /* init num_sc */
1535         num_sc = adev->gfx.config.max_shader_engines * adev->gfx.config.max_sh_per_se *
1536                         adev->gfx.config.num_sc_per_sh;
1537         /* init num_rb_per_sc */
1538         active_rb_bitmap = gfx_v10_0_get_rb_active_bitmap(adev);
1539         enabled_rb_per_sh = hweight32(active_rb_bitmap);
1540         num_rb_per_sc = enabled_rb_per_sh / adev->gfx.config.num_sc_per_sh;
1541         /* init num_packer_per_sc */
1542         num_packer_per_sc = adev->gfx.config.num_packer_per_sc;
1543
1544         pa_sc_tile_steering_override = 0;
1545         pa_sc_tile_steering_override |=
1546                 (order_base_2(num_sc) << PA_SC_TILE_STEERING_OVERRIDE__NUM_SC__SHIFT) &
1547                 PA_SC_TILE_STEERING_OVERRIDE__NUM_SC_MASK;
1548         pa_sc_tile_steering_override |=
1549                 (order_base_2(num_rb_per_sc) << PA_SC_TILE_STEERING_OVERRIDE__NUM_RB_PER_SC__SHIFT) &
1550                 PA_SC_TILE_STEERING_OVERRIDE__NUM_RB_PER_SC_MASK;
1551         pa_sc_tile_steering_override |=
1552                 (order_base_2(num_packer_per_sc) << PA_SC_TILE_STEERING_OVERRIDE__NUM_PACKER_PER_SC__SHIFT) &
1553                 PA_SC_TILE_STEERING_OVERRIDE__NUM_PACKER_PER_SC_MASK;
1554
1555         return pa_sc_tile_steering_override;
1556 }
1557
1558 #define DEFAULT_SH_MEM_BASES    (0x6000)
1559 #define FIRST_COMPUTE_VMID      (8)
1560 #define LAST_COMPUTE_VMID       (16)
1561
1562 static void gfx_v10_0_init_compute_vmid(struct amdgpu_device *adev)
1563 {
1564         int i;
1565         uint32_t sh_mem_bases;
1566
1567         /*
1568          * Configure apertures:
1569          * LDS:         0x60000000'00000000 - 0x60000001'00000000 (4GB)
1570          * Scratch:     0x60000001'00000000 - 0x60000002'00000000 (4GB)
1571          * GPUVM:       0x60010000'00000000 - 0x60020000'00000000 (1TB)
1572          */
1573         sh_mem_bases = DEFAULT_SH_MEM_BASES | (DEFAULT_SH_MEM_BASES << 16);
1574
1575         mutex_lock(&adev->srbm_mutex);
1576         for (i = FIRST_COMPUTE_VMID; i < LAST_COMPUTE_VMID; i++) {
1577                 nv_grbm_select(adev, 0, 0, 0, i);
1578                 /* CP and shaders */
1579                 WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, DEFAULT_SH_MEM_CONFIG);
1580                 WREG32_SOC15(GC, 0, mmSH_MEM_BASES, sh_mem_bases);
1581         }
1582         nv_grbm_select(adev, 0, 0, 0, 0);
1583         mutex_unlock(&adev->srbm_mutex);
1584
1585         /* Initialize all compute VMIDs to have no GDS, GWS, or OA
1586            acccess. These should be enabled by FW for target VMIDs. */
1587         for (i = FIRST_COMPUTE_VMID; i < LAST_COMPUTE_VMID; i++) {
1588                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_VMID0_BASE, 2 * i, 0);
1589                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_VMID0_SIZE, 2 * i, 0);
1590                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_GWS_VMID0, i, 0);
1591                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_OA_VMID0, i, 0);
1592         }
1593 }
1594
1595 static void gfx_v10_0_init_gds_vmid(struct amdgpu_device *adev)
1596 {
1597         int vmid;
1598
1599         /*
1600          * Initialize all compute and user-gfx VMIDs to have no GDS, GWS, or OA
1601          * access. Compute VMIDs should be enabled by FW for target VMIDs,
1602          * the driver can enable them for graphics. VMID0 should maintain
1603          * access so that HWS firmware can save/restore entries.
1604          */
1605         for (vmid = 1; vmid < 16; vmid++) {
1606                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_VMID0_BASE, 2 * vmid, 0);
1607                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_VMID0_SIZE, 2 * vmid, 0);
1608                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_GWS_VMID0, vmid, 0);
1609                 WREG32_SOC15_OFFSET(GC, 0, mmGDS_OA_VMID0, vmid, 0);
1610         }
1611 }
1612
1613
1614 static void gfx_v10_0_tcp_harvest(struct amdgpu_device *adev)
1615 {
1616         int i, j, k;
1617         int max_wgp_per_sh = adev->gfx.config.max_cu_per_sh >> 1;
1618         u32 tmp, wgp_active_bitmap = 0;
1619         u32 gcrd_targets_disable_tcp = 0;
1620         u32 utcl_invreq_disable = 0;
1621         /*
1622          * GCRD_TARGETS_DISABLE field contains
1623          * for Navi10/Navi12: GL1C=[18:15], SQC=[14:10], TCP=[9:0]
1624          * for Navi14: GL1C=[21:18], SQC=[17:12], TCP=[11:0]
1625          */
1626         u32 gcrd_targets_disable_mask = amdgpu_gfx_create_bitmask(
1627                 2 * max_wgp_per_sh + /* TCP */
1628                 max_wgp_per_sh + /* SQC */
1629                 4); /* GL1C */
1630         /*
1631          * UTCL1_UTCL0_INVREQ_DISABLE field contains
1632          * for Navi10Navi12: SQG=[24], RMI=[23:20], SQC=[19:10], TCP=[9:0]
1633          * for Navi14: SQG=[28], RMI=[27:24], SQC=[23:12], TCP=[11:0]
1634          */
1635         u32 utcl_invreq_disable_mask = amdgpu_gfx_create_bitmask(
1636                 2 * max_wgp_per_sh + /* TCP */
1637                 2 * max_wgp_per_sh + /* SQC */
1638                 4 + /* RMI */
1639                 1); /* SQG */
1640
1641         if (adev->asic_type == CHIP_NAVI10 ||
1642             adev->asic_type == CHIP_NAVI14 ||
1643             adev->asic_type == CHIP_NAVI12) {
1644                 mutex_lock(&adev->grbm_idx_mutex);
1645                 for (i = 0; i < adev->gfx.config.max_shader_engines; i++) {
1646                         for (j = 0; j < adev->gfx.config.max_sh_per_se; j++) {
1647                                 gfx_v10_0_select_se_sh(adev, i, j, 0xffffffff);
1648                                 wgp_active_bitmap = gfx_v10_0_get_wgp_active_bitmap_per_sh(adev);
1649                                 /*
1650                                  * Set corresponding TCP bits for the inactive WGPs in
1651                                  * GCRD_SA_TARGETS_DISABLE
1652                                  */
1653                                 gcrd_targets_disable_tcp = 0;
1654                                 /* Set TCP & SQC bits in UTCL1_UTCL0_INVREQ_DISABLE */
1655                                 utcl_invreq_disable = 0;
1656
1657                                 for (k = 0; k < max_wgp_per_sh; k++) {
1658                                         if (!(wgp_active_bitmap & (1 << k))) {
1659                                                 gcrd_targets_disable_tcp |= 3 << (2 * k);
1660                                                 utcl_invreq_disable |= (3 << (2 * k)) |
1661                                                         (3 << (2 * (max_wgp_per_sh + k)));
1662                                         }
1663                                 }
1664
1665                                 tmp = RREG32_SOC15(GC, 0, mmUTCL1_UTCL0_INVREQ_DISABLE);
1666                                 /* only override TCP & SQC bits */
1667                                 tmp &= 0xffffffff << (4 * max_wgp_per_sh);
1668                                 tmp |= (utcl_invreq_disable & utcl_invreq_disable_mask);
1669                                 WREG32_SOC15(GC, 0, mmUTCL1_UTCL0_INVREQ_DISABLE, tmp);
1670
1671                                 tmp = RREG32_SOC15(GC, 0, mmGCRD_SA_TARGETS_DISABLE);
1672                                 /* only override TCP bits */
1673                                 tmp &= 0xffffffff << (2 * max_wgp_per_sh);
1674                                 tmp |= (gcrd_targets_disable_tcp & gcrd_targets_disable_mask);
1675                                 WREG32_SOC15(GC, 0, mmGCRD_SA_TARGETS_DISABLE, tmp);
1676                         }
1677                 }
1678
1679                 gfx_v10_0_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
1680                 mutex_unlock(&adev->grbm_idx_mutex);
1681         }
1682 }
1683
1684 static void gfx_v10_0_constants_init(struct amdgpu_device *adev)
1685 {
1686         u32 tmp;
1687         int i;
1688
1689         WREG32_FIELD15(GC, 0, GRBM_CNTL, READ_TIMEOUT, 0xff);
1690
1691         gfx_v10_0_tiling_mode_table_init(adev);
1692
1693         gfx_v10_0_setup_rb(adev);
1694         gfx_v10_0_get_cu_info(adev, &adev->gfx.cu_info);
1695         adev->gfx.config.pa_sc_tile_steering_override =
1696                 gfx_v10_0_init_pa_sc_tile_steering_override(adev);
1697
1698         /* XXX SH_MEM regs */
1699         /* where to put LDS, scratch, GPUVM in FSA64 space */
1700         mutex_lock(&adev->srbm_mutex);
1701         for (i = 0; i < adev->vm_manager.id_mgr[AMDGPU_GFXHUB_0].num_ids; i++) {
1702                 nv_grbm_select(adev, 0, 0, 0, i);
1703                 /* CP and shaders */
1704                 WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, DEFAULT_SH_MEM_CONFIG);
1705                 if (i != 0) {
1706                         tmp = REG_SET_FIELD(0, SH_MEM_BASES, PRIVATE_BASE,
1707                                 (adev->gmc.private_aperture_start >> 48));
1708                         tmp = REG_SET_FIELD(tmp, SH_MEM_BASES, SHARED_BASE,
1709                                 (adev->gmc.shared_aperture_start >> 48));
1710                         WREG32_SOC15(GC, 0, mmSH_MEM_BASES, tmp);
1711                 }
1712         }
1713         nv_grbm_select(adev, 0, 0, 0, 0);
1714
1715         mutex_unlock(&adev->srbm_mutex);
1716
1717         gfx_v10_0_init_compute_vmid(adev);
1718         gfx_v10_0_init_gds_vmid(adev);
1719
1720 }
1721
1722 static void gfx_v10_0_enable_gui_idle_interrupt(struct amdgpu_device *adev,
1723                                                bool enable)
1724 {
1725         u32 tmp = RREG32_SOC15(GC, 0, mmCP_INT_CNTL_RING0);
1726
1727         tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_BUSY_INT_ENABLE,
1728                             enable ? 1 : 0);
1729         tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CNTX_EMPTY_INT_ENABLE,
1730                             enable ? 1 : 0);
1731         tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, CMP_BUSY_INT_ENABLE,
1732                             enable ? 1 : 0);
1733         tmp = REG_SET_FIELD(tmp, CP_INT_CNTL_RING0, GFX_IDLE_INT_ENABLE,
1734                             enable ? 1 : 0);
1735
1736         WREG32_SOC15(GC, 0, mmCP_INT_CNTL_RING0, tmp);
1737 }
1738
1739 static void gfx_v10_0_init_csb(struct amdgpu_device *adev)
1740 {
1741         /* csib */
1742         WREG32_SOC15(GC, 0, mmRLC_CSIB_ADDR_HI,
1743                      adev->gfx.rlc.clear_state_gpu_addr >> 32);
1744         WREG32_SOC15(GC, 0, mmRLC_CSIB_ADDR_LO,
1745                      adev->gfx.rlc.clear_state_gpu_addr & 0xfffffffc);
1746         WREG32_SOC15(GC, 0, mmRLC_CSIB_LENGTH, adev->gfx.rlc.clear_state_size);
1747 }
1748
1749 static void gfx_v10_0_init_pg(struct amdgpu_device *adev)
1750 {
1751         gfx_v10_0_init_csb(adev);
1752
1753         amdgpu_gmc_flush_gpu_tlb(adev, 0, 0);
1754
1755         /* TODO: init power gating */
1756         return;
1757 }
1758
1759 void gfx_v10_0_rlc_stop(struct amdgpu_device *adev)
1760 {
1761         u32 tmp = RREG32_SOC15(GC, 0, mmRLC_CNTL);
1762
1763         tmp = REG_SET_FIELD(tmp, RLC_CNTL, RLC_ENABLE_F32, 0);
1764         WREG32_SOC15(GC, 0, mmRLC_CNTL, tmp);
1765 }
1766
1767 static void gfx_v10_0_rlc_reset(struct amdgpu_device *adev)
1768 {
1769         WREG32_FIELD15(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 1);
1770         udelay(50);
1771         WREG32_FIELD15(GC, 0, GRBM_SOFT_RESET, SOFT_RESET_RLC, 0);
1772         udelay(50);
1773 }
1774
1775 static void gfx_v10_0_rlc_smu_handshake_cntl(struct amdgpu_device *adev,
1776                                              bool enable)
1777 {
1778         uint32_t rlc_pg_cntl;
1779
1780         rlc_pg_cntl = RREG32_SOC15(GC, 0, mmRLC_PG_CNTL);
1781
1782         if (!enable) {
1783                 /* RLC_PG_CNTL[23] = 0 (default)
1784                  * RLC will wait for handshake acks with SMU
1785                  * GFXOFF will be enabled
1786                  * RLC_PG_CNTL[23] = 1
1787                  * RLC will not issue any message to SMU
1788                  * hence no handshake between SMU & RLC
1789                  * GFXOFF will be disabled
1790                  */
1791                 rlc_pg_cntl |= 0x800000;
1792         } else
1793                 rlc_pg_cntl &= ~0x800000;
1794         WREG32_SOC15(GC, 0, mmRLC_PG_CNTL, rlc_pg_cntl);
1795 }
1796
1797 static void gfx_v10_0_rlc_start(struct amdgpu_device *adev)
1798 {
1799         /* TODO: enable rlc & smu handshake until smu
1800          * and gfxoff feature works as expected */
1801         if (!(amdgpu_pp_feature_mask & PP_GFXOFF_MASK))
1802                 gfx_v10_0_rlc_smu_handshake_cntl(adev, false);
1803
1804         WREG32_FIELD15(GC, 0, RLC_CNTL, RLC_ENABLE_F32, 1);
1805         udelay(50);
1806 }
1807
1808 static void gfx_v10_0_rlc_enable_srm(struct amdgpu_device *adev)
1809 {
1810         uint32_t tmp;
1811
1812         /* enable Save Restore Machine */
1813         tmp = RREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_CNTL));
1814         tmp |= RLC_SRM_CNTL__AUTO_INCR_ADDR_MASK;
1815         tmp |= RLC_SRM_CNTL__SRM_ENABLE_MASK;
1816         WREG32(SOC15_REG_OFFSET(GC, 0, mmRLC_SRM_CNTL), tmp);
1817 }
1818
1819 static int gfx_v10_0_rlc_load_microcode(struct amdgpu_device *adev)
1820 {
1821         const struct rlc_firmware_header_v2_0 *hdr;
1822         const __le32 *fw_data;
1823         unsigned i, fw_size;
1824
1825         if (!adev->gfx.rlc_fw)
1826                 return -EINVAL;
1827
1828         hdr = (const struct rlc_firmware_header_v2_0 *)adev->gfx.rlc_fw->data;
1829         amdgpu_ucode_print_rlc_hdr(&hdr->header);
1830
1831         fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
1832                            le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1833         fw_size = le32_to_cpu(hdr->header.ucode_size_bytes) / 4;
1834
1835         WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_ADDR,
1836                      RLCG_UCODE_LOADING_START_ADDRESS);
1837
1838         for (i = 0; i < fw_size; i++)
1839                 WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_DATA,
1840                              le32_to_cpup(fw_data++));
1841
1842         WREG32_SOC15(GC, 0, mmRLC_GPM_UCODE_ADDR, adev->gfx.rlc_fw_version);
1843
1844         return 0;
1845 }
1846
1847 static int gfx_v10_0_rlc_resume(struct amdgpu_device *adev)
1848 {
1849         int r;
1850
1851         if (amdgpu_sriov_vf(adev))
1852                 return 0;
1853
1854         if (adev->firmware.load_type == AMDGPU_FW_LOAD_PSP) {
1855                 r = gfx_v10_0_wait_for_rlc_autoload_complete(adev);
1856                 if (r)
1857                         return r;
1858                 gfx_v10_0_init_pg(adev);
1859
1860                 /* enable RLC SRM */
1861                 gfx_v10_0_rlc_enable_srm(adev);
1862
1863         } else {
1864                 adev->gfx.rlc.funcs->stop(adev);
1865
1866                 /* disable CG */
1867                 WREG32_SOC15(GC, 0, mmRLC_CGCG_CGLS_CTRL, 0);
1868
1869                 /* disable PG */
1870                 WREG32_SOC15(GC, 0, mmRLC_PG_CNTL, 0);
1871
1872                 if (adev->firmware.load_type == AMDGPU_FW_LOAD_DIRECT) {
1873                         /* legacy rlc firmware loading */
1874                         r = gfx_v10_0_rlc_load_microcode(adev);
1875                         if (r)
1876                                 return r;
1877                 } else if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) {
1878                         /* rlc backdoor autoload firmware */
1879                         r = gfx_v10_0_rlc_backdoor_autoload_enable(adev);
1880                         if (r)
1881                                 return r;
1882                 }
1883
1884                 gfx_v10_0_init_pg(adev);
1885                 adev->gfx.rlc.funcs->start(adev);
1886
1887                 if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) {
1888                         r = gfx_v10_0_wait_for_rlc_autoload_complete(adev);
1889                         if (r)
1890                                 return r;
1891                 }
1892         }
1893         return 0;
1894 }
1895
1896 static struct {
1897         FIRMWARE_ID     id;
1898         unsigned int    offset;
1899         unsigned int    size;
1900 } rlc_autoload_info[FIRMWARE_ID_MAX];
1901
1902 static int gfx_v10_0_parse_rlc_toc(struct amdgpu_device *adev)
1903 {
1904         int ret;
1905         RLC_TABLE_OF_CONTENT *rlc_toc;
1906
1907         ret = amdgpu_bo_create_reserved(adev, adev->psp.toc_bin_size, PAGE_SIZE,
1908                                         AMDGPU_GEM_DOMAIN_GTT,
1909                                         &adev->gfx.rlc.rlc_toc_bo,
1910                                         &adev->gfx.rlc.rlc_toc_gpu_addr,
1911                                         (void **)&adev->gfx.rlc.rlc_toc_buf);
1912         if (ret) {
1913                 dev_err(adev->dev, "(%d) failed to create rlc toc bo\n", ret);
1914                 return ret;
1915         }
1916
1917         /* Copy toc from psp sos fw to rlc toc buffer */
1918         memcpy(adev->gfx.rlc.rlc_toc_buf, adev->psp.toc_start_addr, adev->psp.toc_bin_size);
1919
1920         rlc_toc = (RLC_TABLE_OF_CONTENT *)adev->gfx.rlc.rlc_toc_buf;
1921         while (rlc_toc && (rlc_toc->id > FIRMWARE_ID_INVALID) &&
1922                 (rlc_toc->id < FIRMWARE_ID_MAX)) {
1923                 if ((rlc_toc->id >= FIRMWARE_ID_CP_CE) &&
1924                     (rlc_toc->id <= FIRMWARE_ID_CP_MES)) {
1925                         /* Offset needs 4KB alignment */
1926                         rlc_toc->offset = ALIGN(rlc_toc->offset * 4, PAGE_SIZE);
1927                 }
1928
1929                 rlc_autoload_info[rlc_toc->id].id = rlc_toc->id;
1930                 rlc_autoload_info[rlc_toc->id].offset = rlc_toc->offset * 4;
1931                 rlc_autoload_info[rlc_toc->id].size = rlc_toc->size * 4;
1932
1933                 rlc_toc++;
1934         };
1935
1936         return 0;
1937 }
1938
1939 static uint32_t gfx_v10_0_calc_toc_total_size(struct amdgpu_device *adev)
1940 {
1941         uint32_t total_size = 0;
1942         FIRMWARE_ID id;
1943         int ret;
1944
1945         ret = gfx_v10_0_parse_rlc_toc(adev);
1946         if (ret) {
1947                 dev_err(adev->dev, "failed to parse rlc toc\n");
1948                 return 0;
1949         }
1950
1951         for (id = FIRMWARE_ID_RLC_G_UCODE; id < FIRMWARE_ID_MAX; id++)
1952                 total_size += rlc_autoload_info[id].size;
1953
1954         /* In case the offset in rlc toc ucode is aligned */
1955         if (total_size < rlc_autoload_info[FIRMWARE_ID_MAX-1].offset)
1956                 total_size = rlc_autoload_info[FIRMWARE_ID_MAX-1].offset +
1957                                 rlc_autoload_info[FIRMWARE_ID_MAX-1].size;
1958
1959         return total_size;
1960 }
1961
1962 static int gfx_v10_0_rlc_backdoor_autoload_buffer_init(struct amdgpu_device *adev)
1963 {
1964         int r;
1965         uint32_t total_size;
1966
1967         total_size = gfx_v10_0_calc_toc_total_size(adev);
1968
1969         r = amdgpu_bo_create_reserved(adev, total_size, PAGE_SIZE,
1970                                       AMDGPU_GEM_DOMAIN_GTT,
1971                                       &adev->gfx.rlc.rlc_autoload_bo,
1972                                       &adev->gfx.rlc.rlc_autoload_gpu_addr,
1973                                       (void **)&adev->gfx.rlc.rlc_autoload_ptr);
1974         if (r) {
1975                 dev_err(adev->dev, "(%d) failed to create fw autoload bo\n", r);
1976                 return r;
1977         }
1978
1979         return 0;
1980 }
1981
1982 static void gfx_v10_0_rlc_backdoor_autoload_buffer_fini(struct amdgpu_device *adev)
1983 {
1984         amdgpu_bo_free_kernel(&adev->gfx.rlc.rlc_toc_bo,
1985                               &adev->gfx.rlc.rlc_toc_gpu_addr,
1986                               (void **)&adev->gfx.rlc.rlc_toc_buf);
1987         amdgpu_bo_free_kernel(&adev->gfx.rlc.rlc_autoload_bo,
1988                               &adev->gfx.rlc.rlc_autoload_gpu_addr,
1989                               (void **)&adev->gfx.rlc.rlc_autoload_ptr);
1990 }
1991
1992 static void gfx_v10_0_rlc_backdoor_autoload_copy_ucode(struct amdgpu_device *adev,
1993                                                        FIRMWARE_ID id,
1994                                                        const void *fw_data,
1995                                                        uint32_t fw_size)
1996 {
1997         uint32_t toc_offset;
1998         uint32_t toc_fw_size;
1999         char *ptr = adev->gfx.rlc.rlc_autoload_ptr;
2000
2001         if (id <= FIRMWARE_ID_INVALID || id >= FIRMWARE_ID_MAX)
2002                 return;
2003
2004         toc_offset = rlc_autoload_info[id].offset;
2005         toc_fw_size = rlc_autoload_info[id].size;
2006
2007         if (fw_size == 0)
2008                 fw_size = toc_fw_size;
2009
2010         if (fw_size > toc_fw_size)
2011                 fw_size = toc_fw_size;
2012
2013         memcpy(ptr + toc_offset, fw_data, fw_size);
2014
2015         if (fw_size < toc_fw_size)
2016                 memset(ptr + toc_offset + fw_size, 0, toc_fw_size - fw_size);
2017 }
2018
2019 static void gfx_v10_0_rlc_backdoor_autoload_copy_toc_ucode(struct amdgpu_device *adev)
2020 {
2021         void *data;
2022         uint32_t size;
2023
2024         data = adev->gfx.rlc.rlc_toc_buf;
2025         size = rlc_autoload_info[FIRMWARE_ID_RLC_TOC].size;
2026
2027         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2028                                                    FIRMWARE_ID_RLC_TOC,
2029                                                    data, size);
2030 }
2031
2032 static void gfx_v10_0_rlc_backdoor_autoload_copy_gfx_ucode(struct amdgpu_device *adev)
2033 {
2034         const __le32 *fw_data;
2035         uint32_t fw_size;
2036         const struct gfx_firmware_header_v1_0 *cp_hdr;
2037         const struct rlc_firmware_header_v2_0 *rlc_hdr;
2038
2039         /* pfp ucode */
2040         cp_hdr = (const struct gfx_firmware_header_v1_0 *)
2041                 adev->gfx.pfp_fw->data;
2042         fw_data = (const __le32 *)(adev->gfx.pfp_fw->data +
2043                 le32_to_cpu(cp_hdr->header.ucode_array_offset_bytes));
2044         fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
2045         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2046                                                    FIRMWARE_ID_CP_PFP,
2047                                                    fw_data, fw_size);
2048
2049         /* ce ucode */
2050         cp_hdr = (const struct gfx_firmware_header_v1_0 *)
2051                 adev->gfx.ce_fw->data;
2052         fw_data = (const __le32 *)(adev->gfx.ce_fw->data +
2053                 le32_to_cpu(cp_hdr->header.ucode_array_offset_bytes));
2054         fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
2055         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2056                                                    FIRMWARE_ID_CP_CE,
2057                                                    fw_data, fw_size);
2058
2059         /* me ucode */
2060         cp_hdr = (const struct gfx_firmware_header_v1_0 *)
2061                 adev->gfx.me_fw->data;
2062         fw_data = (const __le32 *)(adev->gfx.me_fw->data +
2063                 le32_to_cpu(cp_hdr->header.ucode_array_offset_bytes));
2064         fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes);
2065         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2066                                                    FIRMWARE_ID_CP_ME,
2067                                                    fw_data, fw_size);
2068
2069         /* rlc ucode */
2070         rlc_hdr = (const struct rlc_firmware_header_v2_0 *)
2071                 adev->gfx.rlc_fw->data;
2072         fw_data = (const __le32 *)(adev->gfx.rlc_fw->data +
2073                 le32_to_cpu(rlc_hdr->header.ucode_array_offset_bytes));
2074         fw_size = le32_to_cpu(rlc_hdr->header.ucode_size_bytes);
2075         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2076                                                    FIRMWARE_ID_RLC_G_UCODE,
2077                                                    fw_data, fw_size);
2078
2079         /* mec1 ucode */
2080         cp_hdr = (const struct gfx_firmware_header_v1_0 *)
2081                 adev->gfx.mec_fw->data;
2082         fw_data = (const __le32 *) (adev->gfx.mec_fw->data +
2083                 le32_to_cpu(cp_hdr->header.ucode_array_offset_bytes));
2084         fw_size = le32_to_cpu(cp_hdr->header.ucode_size_bytes) -
2085                 cp_hdr->jt_size * 4;
2086         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2087                                                    FIRMWARE_ID_CP_MEC,
2088                                                    fw_data, fw_size);
2089         /* mec2 ucode is not necessary if mec2 ucode is same as mec1 */
2090 }
2091
2092 /* Temporarily put sdma part here */
2093 static void gfx_v10_0_rlc_backdoor_autoload_copy_sdma_ucode(struct amdgpu_device *adev)
2094 {
2095         const __le32 *fw_data;
2096         uint32_t fw_size;
2097         const struct sdma_firmware_header_v1_0 *sdma_hdr;
2098         int i;
2099
2100         for (i = 0; i < adev->sdma.num_instances; i++) {
2101                 sdma_hdr = (const struct sdma_firmware_header_v1_0 *)
2102                         adev->sdma.instance[i].fw->data;
2103                 fw_data = (const __le32 *) (adev->sdma.instance[i].fw->data +
2104                         le32_to_cpu(sdma_hdr->header.ucode_array_offset_bytes));
2105                 fw_size = le32_to_cpu(sdma_hdr->header.ucode_size_bytes);
2106
2107                 if (i == 0) {
2108                         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2109                                 FIRMWARE_ID_SDMA0_UCODE, fw_data, fw_size);
2110                         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2111                                 FIRMWARE_ID_SDMA0_JT,
2112                                 (uint32_t *)fw_data +
2113                                 sdma_hdr->jt_offset,
2114                                 sdma_hdr->jt_size * 4);
2115                 } else if (i == 1) {
2116                         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2117                                 FIRMWARE_ID_SDMA1_UCODE, fw_data, fw_size);
2118                         gfx_v10_0_rlc_backdoor_autoload_copy_ucode(adev,
2119                                 FIRMWARE_ID_SDMA1_JT,
2120                                 (uint32_t *)fw_data +
2121                                 sdma_hdr->jt_offset,
2122                                 sdma_hdr->jt_size * 4);
2123                 }
2124         }
2125 }
2126
2127 static int gfx_v10_0_rlc_backdoor_autoload_enable(struct amdgpu_device *adev)
2128 {
2129         uint32_t rlc_g_offset, rlc_g_size, tmp;
2130         uint64_t gpu_addr;
2131
2132         gfx_v10_0_rlc_backdoor_autoload_copy_toc_ucode(adev);
2133         gfx_v10_0_rlc_backdoor_autoload_copy_sdma_ucode(adev);
2134         gfx_v10_0_rlc_backdoor_autoload_copy_gfx_ucode(adev);
2135
2136         rlc_g_offset = rlc_autoload_info[FIRMWARE_ID_RLC_G_UCODE].offset;
2137         rlc_g_size = rlc_autoload_info[FIRMWARE_ID_RLC_G_UCODE].size;
2138         gpu_addr = adev->gfx.rlc.rlc_autoload_gpu_addr + rlc_g_offset;
2139
2140         WREG32_SOC15(GC, 0, mmRLC_HYP_BOOTLOAD_ADDR_HI, upper_32_bits(gpu_addr));
2141         WREG32_SOC15(GC, 0, mmRLC_HYP_BOOTLOAD_ADDR_LO, lower_32_bits(gpu_addr));
2142         WREG32_SOC15(GC, 0, mmRLC_HYP_BOOTLOAD_SIZE, rlc_g_size);
2143
2144         tmp = RREG32_SOC15(GC, 0, mmRLC_HYP_RESET_VECTOR);
2145         if (!(tmp & (RLC_HYP_RESET_VECTOR__COLD_BOOT_EXIT_MASK |
2146                    RLC_HYP_RESET_VECTOR__VDDGFX_EXIT_MASK))) {
2147                 DRM_ERROR("Neither COLD_BOOT_EXIT nor VDDGFX_EXIT is set\n");
2148                 return -EINVAL;
2149         }
2150
2151         tmp = RREG32_SOC15(GC, 0, mmRLC_CNTL);
2152         if (tmp & RLC_CNTL__RLC_ENABLE_F32_MASK) {
2153                 DRM_ERROR("RLC ROM should halt itself\n");
2154                 return -EINVAL;
2155         }
2156
2157         return 0;
2158 }
2159
2160 static int gfx_v10_0_rlc_backdoor_autoload_config_me_cache(struct amdgpu_device *adev)
2161 {
2162         uint32_t usec_timeout = 50000;  /* wait for 50ms */
2163         uint32_t tmp;
2164         int i;
2165         uint64_t addr;
2166
2167         /* Trigger an invalidation of the L1 instruction caches */
2168         tmp = RREG32_SOC15(GC, 0, mmCP_ME_IC_OP_CNTL);
2169         tmp = REG_SET_FIELD(tmp, CP_ME_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2170         WREG32_SOC15(GC, 0, mmCP_ME_IC_OP_CNTL, tmp);
2171
2172         /* Wait for invalidation complete */
2173         for (i = 0; i < usec_timeout; i++) {
2174                 tmp = RREG32_SOC15(GC, 0, mmCP_ME_IC_OP_CNTL);
2175                 if (1 == REG_GET_FIELD(tmp, CP_ME_IC_OP_CNTL,
2176                         INVALIDATE_CACHE_COMPLETE))
2177                         break;
2178                 udelay(1);
2179         }
2180
2181         if (i >= usec_timeout) {
2182                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2183                 return -EINVAL;
2184         }
2185
2186         /* Program me ucode address into intruction cache address register */
2187         addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2188                 rlc_autoload_info[FIRMWARE_ID_CP_ME].offset;
2189         WREG32_SOC15(GC, 0, mmCP_ME_IC_BASE_LO,
2190                         lower_32_bits(addr) & 0xFFFFF000);
2191         WREG32_SOC15(GC, 0, mmCP_ME_IC_BASE_HI,
2192                         upper_32_bits(addr));
2193
2194         return 0;
2195 }
2196
2197 static int gfx_v10_0_rlc_backdoor_autoload_config_ce_cache(struct amdgpu_device *adev)
2198 {
2199         uint32_t usec_timeout = 50000;  /* wait for 50ms */
2200         uint32_t tmp;
2201         int i;
2202         uint64_t addr;
2203
2204         /* Trigger an invalidation of the L1 instruction caches */
2205         tmp = RREG32_SOC15(GC, 0, mmCP_CE_IC_OP_CNTL);
2206         tmp = REG_SET_FIELD(tmp, CP_CE_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2207         WREG32_SOC15(GC, 0, mmCP_CE_IC_OP_CNTL, tmp);
2208
2209         /* Wait for invalidation complete */
2210         for (i = 0; i < usec_timeout; i++) {
2211                 tmp = RREG32_SOC15(GC, 0, mmCP_CE_IC_OP_CNTL);
2212                 if (1 == REG_GET_FIELD(tmp, CP_CE_IC_OP_CNTL,
2213                         INVALIDATE_CACHE_COMPLETE))
2214                         break;
2215                 udelay(1);
2216         }
2217
2218         if (i >= usec_timeout) {
2219                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2220                 return -EINVAL;
2221         }
2222
2223         /* Program ce ucode address into intruction cache address register */
2224         addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2225                 rlc_autoload_info[FIRMWARE_ID_CP_CE].offset;
2226         WREG32_SOC15(GC, 0, mmCP_CE_IC_BASE_LO,
2227                         lower_32_bits(addr) & 0xFFFFF000);
2228         WREG32_SOC15(GC, 0, mmCP_CE_IC_BASE_HI,
2229                         upper_32_bits(addr));
2230
2231         return 0;
2232 }
2233
2234 static int gfx_v10_0_rlc_backdoor_autoload_config_pfp_cache(struct amdgpu_device *adev)
2235 {
2236         uint32_t usec_timeout = 50000;  /* wait for 50ms */
2237         uint32_t tmp;
2238         int i;
2239         uint64_t addr;
2240
2241         /* Trigger an invalidation of the L1 instruction caches */
2242         tmp = RREG32_SOC15(GC, 0, mmCP_PFP_IC_OP_CNTL);
2243         tmp = REG_SET_FIELD(tmp, CP_PFP_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2244         WREG32_SOC15(GC, 0, mmCP_PFP_IC_OP_CNTL, tmp);
2245
2246         /* Wait for invalidation complete */
2247         for (i = 0; i < usec_timeout; i++) {
2248                 tmp = RREG32_SOC15(GC, 0, mmCP_PFP_IC_OP_CNTL);
2249                 if (1 == REG_GET_FIELD(tmp, CP_PFP_IC_OP_CNTL,
2250                         INVALIDATE_CACHE_COMPLETE))
2251                         break;
2252                 udelay(1);
2253         }
2254
2255         if (i >= usec_timeout) {
2256                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2257                 return -EINVAL;
2258         }
2259
2260         /* Program pfp ucode address into intruction cache address register */
2261         addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2262                 rlc_autoload_info[FIRMWARE_ID_CP_PFP].offset;
2263         WREG32_SOC15(GC, 0, mmCP_PFP_IC_BASE_LO,
2264                         lower_32_bits(addr) & 0xFFFFF000);
2265         WREG32_SOC15(GC, 0, mmCP_PFP_IC_BASE_HI,
2266                         upper_32_bits(addr));
2267
2268         return 0;
2269 }
2270
2271 static int gfx_v10_0_rlc_backdoor_autoload_config_mec_cache(struct amdgpu_device *adev)
2272 {
2273         uint32_t usec_timeout = 50000;  /* wait for 50ms */
2274         uint32_t tmp;
2275         int i;
2276         uint64_t addr;
2277
2278         /* Trigger an invalidation of the L1 instruction caches */
2279         tmp = RREG32_SOC15(GC, 0, mmCP_CPC_IC_OP_CNTL);
2280         tmp = REG_SET_FIELD(tmp, CP_CPC_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2281         WREG32_SOC15(GC, 0, mmCP_CPC_IC_OP_CNTL, tmp);
2282
2283         /* Wait for invalidation complete */
2284         for (i = 0; i < usec_timeout; i++) {
2285                 tmp = RREG32_SOC15(GC, 0, mmCP_CPC_IC_OP_CNTL);
2286                 if (1 == REG_GET_FIELD(tmp, CP_CPC_IC_OP_CNTL,
2287                         INVALIDATE_CACHE_COMPLETE))
2288                         break;
2289                 udelay(1);
2290         }
2291
2292         if (i >= usec_timeout) {
2293                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2294                 return -EINVAL;
2295         }
2296
2297         /* Program mec1 ucode address into intruction cache address register */
2298         addr = adev->gfx.rlc.rlc_autoload_gpu_addr +
2299                 rlc_autoload_info[FIRMWARE_ID_CP_MEC].offset;
2300         WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_LO,
2301                         lower_32_bits(addr) & 0xFFFFF000);
2302         WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_HI,
2303                         upper_32_bits(addr));
2304
2305         return 0;
2306 }
2307
2308 static int gfx_v10_0_wait_for_rlc_autoload_complete(struct amdgpu_device *adev)
2309 {
2310         uint32_t cp_status;
2311         uint32_t bootload_status;
2312         int i, r;
2313
2314         for (i = 0; i < adev->usec_timeout; i++) {
2315                 cp_status = RREG32_SOC15(GC, 0, mmCP_STAT);
2316                 bootload_status = RREG32_SOC15(GC, 0, mmRLC_RLCS_BOOTLOAD_STATUS);
2317                 if ((cp_status == 0) &&
2318                     (REG_GET_FIELD(bootload_status,
2319                         RLC_RLCS_BOOTLOAD_STATUS, BOOTLOAD_COMPLETE) == 1)) {
2320                         break;
2321                 }
2322                 udelay(1);
2323         }
2324
2325         if (i >= adev->usec_timeout) {
2326                 dev_err(adev->dev, "rlc autoload: gc ucode autoload timeout\n");
2327                 return -ETIMEDOUT;
2328         }
2329
2330         if (adev->firmware.load_type == AMDGPU_FW_LOAD_RLC_BACKDOOR_AUTO) {
2331                 r = gfx_v10_0_rlc_backdoor_autoload_config_me_cache(adev);
2332                 if (r)
2333                         return r;
2334
2335                 r = gfx_v10_0_rlc_backdoor_autoload_config_ce_cache(adev);
2336                 if (r)
2337                         return r;
2338
2339                 r = gfx_v10_0_rlc_backdoor_autoload_config_pfp_cache(adev);
2340                 if (r)
2341                         return r;
2342
2343                 r = gfx_v10_0_rlc_backdoor_autoload_config_mec_cache(adev);
2344                 if (r)
2345                         return r;
2346         }
2347
2348         return 0;
2349 }
2350
2351 static void gfx_v10_0_cp_gfx_enable(struct amdgpu_device *adev, bool enable)
2352 {
2353         int i;
2354         u32 tmp = RREG32_SOC15(GC, 0, mmCP_ME_CNTL);
2355
2356         tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, ME_HALT, enable ? 0 : 1);
2357         tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, PFP_HALT, enable ? 0 : 1);
2358         tmp = REG_SET_FIELD(tmp, CP_ME_CNTL, CE_HALT, enable ? 0 : 1);
2359         if (!enable) {
2360                 for (i = 0; i < adev->gfx.num_gfx_rings; i++)
2361                         adev->gfx.gfx_ring[i].sched.ready = false;
2362         }
2363         WREG32_SOC15(GC, 0, mmCP_ME_CNTL, tmp);
2364         udelay(50);
2365 }
2366
2367 static int gfx_v10_0_cp_gfx_load_pfp_microcode(struct amdgpu_device *adev)
2368 {
2369         int r;
2370         const struct gfx_firmware_header_v1_0 *pfp_hdr;
2371         const __le32 *fw_data;
2372         unsigned i, fw_size;
2373         uint32_t tmp;
2374         uint32_t usec_timeout = 50000;  /* wait for 50ms */
2375
2376         pfp_hdr = (const struct gfx_firmware_header_v1_0 *)
2377                 adev->gfx.pfp_fw->data;
2378
2379         amdgpu_ucode_print_gfx_hdr(&pfp_hdr->header);
2380
2381         fw_data = (const __le32 *)(adev->gfx.pfp_fw->data +
2382                 le32_to_cpu(pfp_hdr->header.ucode_array_offset_bytes));
2383         fw_size = le32_to_cpu(pfp_hdr->header.ucode_size_bytes);
2384
2385         r = amdgpu_bo_create_reserved(adev, pfp_hdr->header.ucode_size_bytes,
2386                                       PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
2387                                       &adev->gfx.pfp.pfp_fw_obj,
2388                                       &adev->gfx.pfp.pfp_fw_gpu_addr,
2389                                       (void **)&adev->gfx.pfp.pfp_fw_ptr);
2390         if (r) {
2391                 dev_err(adev->dev, "(%d) failed to create pfp fw bo\n", r);
2392                 gfx_v10_0_pfp_fini(adev);
2393                 return r;
2394         }
2395
2396         memcpy(adev->gfx.pfp.pfp_fw_ptr, fw_data, fw_size);
2397
2398         amdgpu_bo_kunmap(adev->gfx.pfp.pfp_fw_obj);
2399         amdgpu_bo_unreserve(adev->gfx.pfp.pfp_fw_obj);
2400
2401         /* Trigger an invalidation of the L1 instruction caches */
2402         tmp = RREG32_SOC15(GC, 0, mmCP_PFP_IC_OP_CNTL);
2403         tmp = REG_SET_FIELD(tmp, CP_PFP_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2404         WREG32_SOC15(GC, 0, mmCP_PFP_IC_OP_CNTL, tmp);
2405
2406         /* Wait for invalidation complete */
2407         for (i = 0; i < usec_timeout; i++) {
2408                 tmp = RREG32_SOC15(GC, 0, mmCP_PFP_IC_OP_CNTL);
2409                 if (1 == REG_GET_FIELD(tmp, CP_PFP_IC_OP_CNTL,
2410                         INVALIDATE_CACHE_COMPLETE))
2411                         break;
2412                 udelay(1);
2413         }
2414
2415         if (i >= usec_timeout) {
2416                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2417                 return -EINVAL;
2418         }
2419
2420         if (amdgpu_emu_mode == 1)
2421                 adev->nbio_funcs->hdp_flush(adev, NULL);
2422
2423         tmp = RREG32_SOC15(GC, 0, mmCP_PFP_IC_BASE_CNTL);
2424         tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, VMID, 0);
2425         tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, CACHE_POLICY, 0);
2426         tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, EXE_DISABLE, 0);
2427         tmp = REG_SET_FIELD(tmp, CP_PFP_IC_BASE_CNTL, ADDRESS_CLAMP, 1);
2428         WREG32_SOC15(GC, 0, mmCP_PFP_IC_BASE_CNTL, tmp);
2429         WREG32_SOC15(GC, 0, mmCP_PFP_IC_BASE_LO,
2430                 adev->gfx.pfp.pfp_fw_gpu_addr & 0xFFFFF000);
2431         WREG32_SOC15(GC, 0, mmCP_PFP_IC_BASE_HI,
2432                 upper_32_bits(adev->gfx.pfp.pfp_fw_gpu_addr));
2433
2434         return 0;
2435 }
2436
2437 static int gfx_v10_0_cp_gfx_load_ce_microcode(struct amdgpu_device *adev)
2438 {
2439         int r;
2440         const struct gfx_firmware_header_v1_0 *ce_hdr;
2441         const __le32 *fw_data;
2442         unsigned i, fw_size;
2443         uint32_t tmp;
2444         uint32_t usec_timeout = 50000;  /* wait for 50ms */
2445
2446         ce_hdr = (const struct gfx_firmware_header_v1_0 *)
2447                 adev->gfx.ce_fw->data;
2448
2449         amdgpu_ucode_print_gfx_hdr(&ce_hdr->header);
2450
2451         fw_data = (const __le32 *)(adev->gfx.ce_fw->data +
2452                 le32_to_cpu(ce_hdr->header.ucode_array_offset_bytes));
2453         fw_size = le32_to_cpu(ce_hdr->header.ucode_size_bytes);
2454
2455         r = amdgpu_bo_create_reserved(adev, ce_hdr->header.ucode_size_bytes,
2456                                       PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
2457                                       &adev->gfx.ce.ce_fw_obj,
2458                                       &adev->gfx.ce.ce_fw_gpu_addr,
2459                                       (void **)&adev->gfx.ce.ce_fw_ptr);
2460         if (r) {
2461                 dev_err(adev->dev, "(%d) failed to create ce fw bo\n", r);
2462                 gfx_v10_0_ce_fini(adev);
2463                 return r;
2464         }
2465
2466         memcpy(adev->gfx.ce.ce_fw_ptr, fw_data, fw_size);
2467
2468         amdgpu_bo_kunmap(adev->gfx.ce.ce_fw_obj);
2469         amdgpu_bo_unreserve(adev->gfx.ce.ce_fw_obj);
2470
2471         /* Trigger an invalidation of the L1 instruction caches */
2472         tmp = RREG32_SOC15(GC, 0, mmCP_CE_IC_OP_CNTL);
2473         tmp = REG_SET_FIELD(tmp, CP_CE_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2474         WREG32_SOC15(GC, 0, mmCP_CE_IC_OP_CNTL, tmp);
2475
2476         /* Wait for invalidation complete */
2477         for (i = 0; i < usec_timeout; i++) {
2478                 tmp = RREG32_SOC15(GC, 0, mmCP_CE_IC_OP_CNTL);
2479                 if (1 == REG_GET_FIELD(tmp, CP_CE_IC_OP_CNTL,
2480                         INVALIDATE_CACHE_COMPLETE))
2481                         break;
2482                 udelay(1);
2483         }
2484
2485         if (i >= usec_timeout) {
2486                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2487                 return -EINVAL;
2488         }
2489
2490         if (amdgpu_emu_mode == 1)
2491                 adev->nbio_funcs->hdp_flush(adev, NULL);
2492
2493         tmp = RREG32_SOC15(GC, 0, mmCP_CE_IC_BASE_CNTL);
2494         tmp = REG_SET_FIELD(tmp, CP_CE_IC_BASE_CNTL, VMID, 0);
2495         tmp = REG_SET_FIELD(tmp, CP_CE_IC_BASE_CNTL, CACHE_POLICY, 0);
2496         tmp = REG_SET_FIELD(tmp, CP_CE_IC_BASE_CNTL, EXE_DISABLE, 0);
2497         tmp = REG_SET_FIELD(tmp, CP_CE_IC_BASE_CNTL, ADDRESS_CLAMP, 1);
2498         WREG32_SOC15(GC, 0, mmCP_CE_IC_BASE_LO,
2499                 adev->gfx.ce.ce_fw_gpu_addr & 0xFFFFF000);
2500         WREG32_SOC15(GC, 0, mmCP_CE_IC_BASE_HI,
2501                 upper_32_bits(adev->gfx.ce.ce_fw_gpu_addr));
2502
2503         return 0;
2504 }
2505
2506 static int gfx_v10_0_cp_gfx_load_me_microcode(struct amdgpu_device *adev)
2507 {
2508         int r;
2509         const struct gfx_firmware_header_v1_0 *me_hdr;
2510         const __le32 *fw_data;
2511         unsigned i, fw_size;
2512         uint32_t tmp;
2513         uint32_t usec_timeout = 50000;  /* wait for 50ms */
2514
2515         me_hdr = (const struct gfx_firmware_header_v1_0 *)
2516                 adev->gfx.me_fw->data;
2517
2518         amdgpu_ucode_print_gfx_hdr(&me_hdr->header);
2519
2520         fw_data = (const __le32 *)(adev->gfx.me_fw->data +
2521                 le32_to_cpu(me_hdr->header.ucode_array_offset_bytes));
2522         fw_size = le32_to_cpu(me_hdr->header.ucode_size_bytes);
2523
2524         r = amdgpu_bo_create_reserved(adev, me_hdr->header.ucode_size_bytes,
2525                                       PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
2526                                       &adev->gfx.me.me_fw_obj,
2527                                       &adev->gfx.me.me_fw_gpu_addr,
2528                                       (void **)&adev->gfx.me.me_fw_ptr);
2529         if (r) {
2530                 dev_err(adev->dev, "(%d) failed to create me fw bo\n", r);
2531                 gfx_v10_0_me_fini(adev);
2532                 return r;
2533         }
2534
2535         memcpy(adev->gfx.me.me_fw_ptr, fw_data, fw_size);
2536
2537         amdgpu_bo_kunmap(adev->gfx.me.me_fw_obj);
2538         amdgpu_bo_unreserve(adev->gfx.me.me_fw_obj);
2539
2540         /* Trigger an invalidation of the L1 instruction caches */
2541         tmp = RREG32_SOC15(GC, 0, mmCP_ME_IC_OP_CNTL);
2542         tmp = REG_SET_FIELD(tmp, CP_ME_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2543         WREG32_SOC15(GC, 0, mmCP_ME_IC_OP_CNTL, tmp);
2544
2545         /* Wait for invalidation complete */
2546         for (i = 0; i < usec_timeout; i++) {
2547                 tmp = RREG32_SOC15(GC, 0, mmCP_ME_IC_OP_CNTL);
2548                 if (1 == REG_GET_FIELD(tmp, CP_ME_IC_OP_CNTL,
2549                         INVALIDATE_CACHE_COMPLETE))
2550                         break;
2551                 udelay(1);
2552         }
2553
2554         if (i >= usec_timeout) {
2555                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2556                 return -EINVAL;
2557         }
2558
2559         if (amdgpu_emu_mode == 1)
2560                 adev->nbio_funcs->hdp_flush(adev, NULL);
2561
2562         tmp = RREG32_SOC15(GC, 0, mmCP_ME_IC_BASE_CNTL);
2563         tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, VMID, 0);
2564         tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, CACHE_POLICY, 0);
2565         tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, EXE_DISABLE, 0);
2566         tmp = REG_SET_FIELD(tmp, CP_ME_IC_BASE_CNTL, ADDRESS_CLAMP, 1);
2567         WREG32_SOC15(GC, 0, mmCP_ME_IC_BASE_LO,
2568                 adev->gfx.me.me_fw_gpu_addr & 0xFFFFF000);
2569         WREG32_SOC15(GC, 0, mmCP_ME_IC_BASE_HI,
2570                 upper_32_bits(adev->gfx.me.me_fw_gpu_addr));
2571
2572         return 0;
2573 }
2574
2575 static int gfx_v10_0_cp_gfx_load_microcode(struct amdgpu_device *adev)
2576 {
2577         int r;
2578
2579         if (!adev->gfx.me_fw || !adev->gfx.pfp_fw || !adev->gfx.ce_fw)
2580                 return -EINVAL;
2581
2582         gfx_v10_0_cp_gfx_enable(adev, false);
2583
2584         r = gfx_v10_0_cp_gfx_load_pfp_microcode(adev);
2585         if (r) {
2586                 dev_err(adev->dev, "(%d) failed to load pfp fw\n", r);
2587                 return r;
2588         }
2589
2590         r = gfx_v10_0_cp_gfx_load_ce_microcode(adev);
2591         if (r) {
2592                 dev_err(adev->dev, "(%d) failed to load ce fw\n", r);
2593                 return r;
2594         }
2595
2596         r = gfx_v10_0_cp_gfx_load_me_microcode(adev);
2597         if (r) {
2598                 dev_err(adev->dev, "(%d) failed to load me fw\n", r);
2599                 return r;
2600         }
2601
2602         return 0;
2603 }
2604
2605 static int gfx_v10_0_cp_gfx_start(struct amdgpu_device *adev)
2606 {
2607         struct amdgpu_ring *ring;
2608         const struct cs_section_def *sect = NULL;
2609         const struct cs_extent_def *ext = NULL;
2610         int r, i;
2611         int ctx_reg_offset;
2612
2613         /* init the CP */
2614         WREG32_SOC15(GC, 0, mmCP_MAX_CONTEXT,
2615                      adev->gfx.config.max_hw_contexts - 1);
2616         WREG32_SOC15(GC, 0, mmCP_DEVICE_ID, 1);
2617
2618         gfx_v10_0_cp_gfx_enable(adev, true);
2619
2620         ring = &adev->gfx.gfx_ring[0];
2621         r = amdgpu_ring_alloc(ring, gfx_v10_0_get_csb_size(adev) + 4);
2622         if (r) {
2623                 DRM_ERROR("amdgpu: cp failed to lock ring (%d).\n", r);
2624                 return r;
2625         }
2626
2627         amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2628         amdgpu_ring_write(ring, PACKET3_PREAMBLE_BEGIN_CLEAR_STATE);
2629
2630         amdgpu_ring_write(ring, PACKET3(PACKET3_CONTEXT_CONTROL, 1));
2631         amdgpu_ring_write(ring, 0x80000000);
2632         amdgpu_ring_write(ring, 0x80000000);
2633
2634         for (sect = gfx10_cs_data; sect->section != NULL; ++sect) {
2635                 for (ext = sect->section; ext->extent != NULL; ++ext) {
2636                         if (sect->id == SECT_CONTEXT) {
2637                                 amdgpu_ring_write(ring,
2638                                                   PACKET3(PACKET3_SET_CONTEXT_REG,
2639                                                           ext->reg_count));
2640                                 amdgpu_ring_write(ring, ext->reg_index -
2641                                                   PACKET3_SET_CONTEXT_REG_START);
2642                                 for (i = 0; i < ext->reg_count; i++)
2643                                         amdgpu_ring_write(ring, ext->extent[i]);
2644                         }
2645                 }
2646         }
2647
2648         ctx_reg_offset =
2649                 SOC15_REG_OFFSET(GC, 0, mmPA_SC_TILE_STEERING_OVERRIDE) - PACKET3_SET_CONTEXT_REG_START;
2650         amdgpu_ring_write(ring, PACKET3(PACKET3_SET_CONTEXT_REG, 1));
2651         amdgpu_ring_write(ring, ctx_reg_offset);
2652         amdgpu_ring_write(ring, adev->gfx.config.pa_sc_tile_steering_override);
2653
2654         amdgpu_ring_write(ring, PACKET3(PACKET3_PREAMBLE_CNTL, 0));
2655         amdgpu_ring_write(ring, PACKET3_PREAMBLE_END_CLEAR_STATE);
2656
2657         amdgpu_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
2658         amdgpu_ring_write(ring, 0);
2659
2660         amdgpu_ring_write(ring, PACKET3(PACKET3_SET_BASE, 2));
2661         amdgpu_ring_write(ring, PACKET3_BASE_INDEX(CE_PARTITION_BASE));
2662         amdgpu_ring_write(ring, 0x8000);
2663         amdgpu_ring_write(ring, 0x8000);
2664
2665         amdgpu_ring_commit(ring);
2666
2667         /* submit cs packet to copy state 0 to next available state */
2668         ring = &adev->gfx.gfx_ring[1];
2669         r = amdgpu_ring_alloc(ring, 2);
2670         if (r) {
2671                 DRM_ERROR("amdgpu: cp failed to lock ring (%d).\n", r);
2672                 return r;
2673         }
2674
2675         amdgpu_ring_write(ring, PACKET3(PACKET3_CLEAR_STATE, 0));
2676         amdgpu_ring_write(ring, 0);
2677
2678         amdgpu_ring_commit(ring);
2679
2680         return 0;
2681 }
2682
2683 static void gfx_v10_0_cp_gfx_switch_pipe(struct amdgpu_device *adev,
2684                                          CP_PIPE_ID pipe)
2685 {
2686         u32 tmp;
2687
2688         tmp = RREG32_SOC15(GC, 0, mmGRBM_GFX_CNTL);
2689         tmp = REG_SET_FIELD(tmp, GRBM_GFX_CNTL, PIPEID, pipe);
2690
2691         WREG32_SOC15(GC, 0, mmGRBM_GFX_CNTL, tmp);
2692 }
2693
2694 static void gfx_v10_0_cp_gfx_set_doorbell(struct amdgpu_device *adev,
2695                                           struct amdgpu_ring *ring)
2696 {
2697         u32 tmp;
2698
2699         tmp = RREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_CONTROL);
2700         if (ring->use_doorbell) {
2701                 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2702                                     DOORBELL_OFFSET, ring->doorbell_index);
2703                 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2704                                     DOORBELL_EN, 1);
2705         } else {
2706                 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2707                                     DOORBELL_EN, 0);
2708         }
2709         WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_CONTROL, tmp);
2710         tmp = REG_SET_FIELD(0, CP_RB_DOORBELL_RANGE_LOWER,
2711                             DOORBELL_RANGE_LOWER, ring->doorbell_index);
2712         WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_RANGE_LOWER, tmp);
2713
2714         WREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_RANGE_UPPER,
2715                      CP_RB_DOORBELL_RANGE_UPPER__DOORBELL_RANGE_UPPER_MASK);
2716 }
2717
2718 static int gfx_v10_0_cp_gfx_resume(struct amdgpu_device *adev)
2719 {
2720         struct amdgpu_ring *ring;
2721         u32 tmp;
2722         u32 rb_bufsz;
2723         u64 rb_addr, rptr_addr, wptr_gpu_addr;
2724         u32 i;
2725
2726         /* Set the write pointer delay */
2727         WREG32_SOC15(GC, 0, mmCP_RB_WPTR_DELAY, 0);
2728
2729         /* set the RB to use vmid 0 */
2730         WREG32_SOC15(GC, 0, mmCP_RB_VMID, 0);
2731
2732         /* Init gfx ring 0 for pipe 0 */
2733         mutex_lock(&adev->srbm_mutex);
2734         gfx_v10_0_cp_gfx_switch_pipe(adev, PIPE_ID0);
2735         mutex_unlock(&adev->srbm_mutex);
2736         /* Set ring buffer size */
2737         ring = &adev->gfx.gfx_ring[0];
2738         rb_bufsz = order_base_2(ring->ring_size / 8);
2739         tmp = REG_SET_FIELD(0, CP_RB0_CNTL, RB_BUFSZ, rb_bufsz);
2740         tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, RB_BLKSZ, rb_bufsz - 2);
2741 #ifdef __BIG_ENDIAN
2742         tmp = REG_SET_FIELD(tmp, CP_RB0_CNTL, BUF_SWAP, 1);
2743 #endif
2744         WREG32_SOC15(GC, 0, mmCP_RB0_CNTL, tmp);
2745
2746         /* Initialize the ring buffer's write pointers */
2747         ring->wptr = 0;
2748         WREG32_SOC15(GC, 0, mmCP_RB0_WPTR, lower_32_bits(ring->wptr));
2749         WREG32_SOC15(GC, 0, mmCP_RB0_WPTR_HI, upper_32_bits(ring->wptr));
2750
2751         /* set the wb address wether it's enabled or not */
2752         rptr_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
2753         WREG32_SOC15(GC, 0, mmCP_RB0_RPTR_ADDR, lower_32_bits(rptr_addr));
2754         WREG32_SOC15(GC, 0, mmCP_RB0_RPTR_ADDR_HI, upper_32_bits(rptr_addr) &
2755                      CP_RB_RPTR_ADDR_HI__RB_RPTR_ADDR_HI_MASK);
2756
2757         wptr_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2758         WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_LO,
2759                      lower_32_bits(wptr_gpu_addr));
2760         WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_HI,
2761                      upper_32_bits(wptr_gpu_addr));
2762
2763         mdelay(1);
2764         WREG32_SOC15(GC, 0, mmCP_RB0_CNTL, tmp);
2765
2766         rb_addr = ring->gpu_addr >> 8;
2767         WREG32_SOC15(GC, 0, mmCP_RB0_BASE, rb_addr);
2768         WREG32_SOC15(GC, 0, mmCP_RB0_BASE_HI, upper_32_bits(rb_addr));
2769
2770         WREG32_SOC15(GC, 0, mmCP_RB_ACTIVE, 1);
2771
2772         gfx_v10_0_cp_gfx_set_doorbell(adev, ring);
2773
2774         /* Init gfx ring 1 for pipe 1 */
2775         mutex_lock(&adev->srbm_mutex);
2776         gfx_v10_0_cp_gfx_switch_pipe(adev, PIPE_ID1);
2777         mutex_unlock(&adev->srbm_mutex);
2778         ring = &adev->gfx.gfx_ring[1];
2779         rb_bufsz = order_base_2(ring->ring_size / 8);
2780         tmp = REG_SET_FIELD(0, CP_RB1_CNTL, RB_BUFSZ, rb_bufsz);
2781         tmp = REG_SET_FIELD(tmp, CP_RB1_CNTL, RB_BLKSZ, rb_bufsz - 2);
2782         WREG32_SOC15(GC, 0, mmCP_RB1_CNTL, tmp);
2783         /* Initialize the ring buffer's write pointers */
2784         ring->wptr = 0;
2785         WREG32_SOC15(GC, 0, mmCP_RB1_WPTR, lower_32_bits(ring->wptr));
2786         WREG32_SOC15(GC, 0, mmCP_RB1_WPTR_HI, upper_32_bits(ring->wptr));
2787         /* Set the wb address wether it's enabled or not */
2788         rptr_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
2789         WREG32_SOC15(GC, 0, mmCP_RB1_RPTR_ADDR, lower_32_bits(rptr_addr));
2790         WREG32_SOC15(GC, 0, mmCP_RB1_RPTR_ADDR_HI, upper_32_bits(rptr_addr) &
2791                 CP_RB1_RPTR_ADDR_HI__RB_RPTR_ADDR_HI_MASK);
2792         wptr_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2793         WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_LO,
2794                 lower_32_bits(wptr_gpu_addr));
2795         WREG32_SOC15(GC, 0, mmCP_RB_WPTR_POLL_ADDR_HI,
2796                 upper_32_bits(wptr_gpu_addr));
2797
2798         mdelay(1);
2799         WREG32_SOC15(GC, 0, mmCP_RB1_CNTL, tmp);
2800
2801         rb_addr = ring->gpu_addr >> 8;
2802         WREG32_SOC15(GC, 0, mmCP_RB1_BASE, rb_addr);
2803         WREG32_SOC15(GC, 0, mmCP_RB1_BASE_HI, upper_32_bits(rb_addr));
2804         WREG32_SOC15(GC, 0, mmCP_RB1_ACTIVE, 1);
2805
2806         gfx_v10_0_cp_gfx_set_doorbell(adev, ring);
2807
2808         /* Switch to pipe 0 */
2809         mutex_lock(&adev->srbm_mutex);
2810         gfx_v10_0_cp_gfx_switch_pipe(adev, PIPE_ID0);
2811         mutex_unlock(&adev->srbm_mutex);
2812
2813         /* start the ring */
2814         gfx_v10_0_cp_gfx_start(adev);
2815
2816         for (i = 0; i < adev->gfx.num_gfx_rings; i++) {
2817                 ring = &adev->gfx.gfx_ring[i];
2818                 ring->sched.ready = true;
2819         }
2820
2821         return 0;
2822 }
2823
2824 static void gfx_v10_0_cp_compute_enable(struct amdgpu_device *adev, bool enable)
2825 {
2826         int i;
2827
2828         if (enable) {
2829                 WREG32_SOC15(GC, 0, mmCP_MEC_CNTL, 0);
2830         } else {
2831                 WREG32_SOC15(GC, 0, mmCP_MEC_CNTL,
2832                              (CP_MEC_CNTL__MEC_ME1_HALT_MASK |
2833                               CP_MEC_CNTL__MEC_ME2_HALT_MASK));
2834                 for (i = 0; i < adev->gfx.num_compute_rings; i++)
2835                         adev->gfx.compute_ring[i].sched.ready = false;
2836                 adev->gfx.kiq.ring.sched.ready = false;
2837         }
2838         udelay(50);
2839 }
2840
2841 static int gfx_v10_0_cp_compute_load_microcode(struct amdgpu_device *adev)
2842 {
2843         const struct gfx_firmware_header_v1_0 *mec_hdr;
2844         const __le32 *fw_data;
2845         unsigned i;
2846         u32 tmp;
2847         u32 usec_timeout = 50000; /* Wait for 50 ms */
2848
2849         if (!adev->gfx.mec_fw)
2850                 return -EINVAL;
2851
2852         gfx_v10_0_cp_compute_enable(adev, false);
2853
2854         mec_hdr = (const struct gfx_firmware_header_v1_0 *)adev->gfx.mec_fw->data;
2855         amdgpu_ucode_print_gfx_hdr(&mec_hdr->header);
2856
2857         fw_data = (const __le32 *)
2858                 (adev->gfx.mec_fw->data +
2859                  le32_to_cpu(mec_hdr->header.ucode_array_offset_bytes));
2860
2861         /* Trigger an invalidation of the L1 instruction caches */
2862         tmp = RREG32_SOC15(GC, 0, mmCP_CPC_IC_OP_CNTL);
2863         tmp = REG_SET_FIELD(tmp, CP_CPC_IC_OP_CNTL, INVALIDATE_CACHE, 1);
2864         WREG32_SOC15(GC, 0, mmCP_CPC_IC_OP_CNTL, tmp);
2865
2866         /* Wait for invalidation complete */
2867         for (i = 0; i < usec_timeout; i++) {
2868                 tmp = RREG32_SOC15(GC, 0, mmCP_CPC_IC_OP_CNTL);
2869                 if (1 == REG_GET_FIELD(tmp, CP_CPC_IC_OP_CNTL,
2870                                        INVALIDATE_CACHE_COMPLETE))
2871                         break;
2872                 udelay(1);
2873         }
2874
2875         if (i >= usec_timeout) {
2876                 dev_err(adev->dev, "failed to invalidate instruction cache\n");
2877                 return -EINVAL;
2878         }
2879
2880         if (amdgpu_emu_mode == 1)
2881                 adev->nbio_funcs->hdp_flush(adev, NULL);
2882
2883         tmp = RREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_CNTL);
2884         tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, CACHE_POLICY, 0);
2885         tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, EXE_DISABLE, 0);
2886         tmp = REG_SET_FIELD(tmp, CP_CPC_IC_BASE_CNTL, ADDRESS_CLAMP, 1);
2887         WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_CNTL, tmp);
2888
2889         WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_LO, adev->gfx.mec.mec_fw_gpu_addr &
2890                      0xFFFFF000);
2891         WREG32_SOC15(GC, 0, mmCP_CPC_IC_BASE_HI,
2892                      upper_32_bits(adev->gfx.mec.mec_fw_gpu_addr));
2893
2894         /* MEC1 */
2895         WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_ADDR, 0);
2896
2897         for (i = 0; i < mec_hdr->jt_size; i++)
2898                 WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_DATA,
2899                              le32_to_cpup(fw_data + mec_hdr->jt_offset + i));
2900
2901         WREG32_SOC15(GC, 0, mmCP_MEC_ME1_UCODE_ADDR, adev->gfx.mec_fw_version);
2902
2903         /*
2904          * TODO: Loading MEC2 firmware is only necessary if MEC2 should run
2905          * different microcode than MEC1.
2906          */
2907
2908         return 0;
2909 }
2910
2911 static void gfx_v10_0_kiq_setting(struct amdgpu_ring *ring)
2912 {
2913         uint32_t tmp;
2914         struct amdgpu_device *adev = ring->adev;
2915
2916         /* tell RLC which is KIQ queue */
2917         tmp = RREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS);
2918         tmp &= 0xffffff00;
2919         tmp |= (ring->me << 5) | (ring->pipe << 3) | (ring->queue);
2920         WREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS, tmp);
2921         tmp |= 0x80;
2922         WREG32_SOC15(GC, 0, mmRLC_CP_SCHEDULERS, tmp);
2923 }
2924
2925 static int gfx_v10_0_gfx_mqd_init(struct amdgpu_ring *ring)
2926 {
2927         struct amdgpu_device *adev = ring->adev;
2928         struct v10_gfx_mqd *mqd = ring->mqd_ptr;
2929         uint64_t hqd_gpu_addr, wb_gpu_addr;
2930         uint32_t tmp;
2931         uint32_t rb_bufsz;
2932
2933         /* set up gfx hqd wptr */
2934         mqd->cp_gfx_hqd_wptr = 0;
2935         mqd->cp_gfx_hqd_wptr_hi = 0;
2936
2937         /* set the pointer to the MQD */
2938         mqd->cp_mqd_base_addr = ring->mqd_gpu_addr & 0xfffffffc;
2939         mqd->cp_mqd_base_addr_hi = upper_32_bits(ring->mqd_gpu_addr);
2940
2941         /* set up mqd control */
2942         tmp = RREG32_SOC15(GC, 0, mmCP_GFX_MQD_CONTROL);
2943         tmp = REG_SET_FIELD(tmp, CP_GFX_MQD_CONTROL, VMID, 0);
2944         tmp = REG_SET_FIELD(tmp, CP_GFX_MQD_CONTROL, PRIV_STATE, 1);
2945         tmp = REG_SET_FIELD(tmp, CP_GFX_MQD_CONTROL, CACHE_POLICY, 0);
2946         mqd->cp_gfx_mqd_control = tmp;
2947
2948         /* set up gfx_hqd_vimd with 0x0 to indicate the ring buffer's vmid */
2949         tmp = RREG32_SOC15(GC, 0, mmCP_GFX_HQD_VMID);
2950         tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_VMID, VMID, 0);
2951         mqd->cp_gfx_hqd_vmid = 0;
2952
2953         /* set up default queue priority level
2954          * 0x0 = low priority, 0x1 = high priority */
2955         tmp = RREG32_SOC15(GC, 0, mmCP_GFX_HQD_QUEUE_PRIORITY);
2956         tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_QUEUE_PRIORITY, PRIORITY_LEVEL, 0);
2957         mqd->cp_gfx_hqd_queue_priority = tmp;
2958
2959         /* set up time quantum */
2960         tmp = RREG32_SOC15(GC, 0, mmCP_GFX_HQD_QUANTUM);
2961         tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_QUANTUM, QUANTUM_EN, 1);
2962         mqd->cp_gfx_hqd_quantum = tmp;
2963
2964         /* set up gfx hqd base. this is similar as CP_RB_BASE */
2965         hqd_gpu_addr = ring->gpu_addr >> 8;
2966         mqd->cp_gfx_hqd_base = hqd_gpu_addr;
2967         mqd->cp_gfx_hqd_base_hi = upper_32_bits(hqd_gpu_addr);
2968
2969         /* set up hqd_rptr_addr/_hi, similar as CP_RB_RPTR */
2970         wb_gpu_addr = adev->wb.gpu_addr + (ring->rptr_offs * 4);
2971         mqd->cp_gfx_hqd_rptr_addr = wb_gpu_addr & 0xfffffffc;
2972         mqd->cp_gfx_hqd_rptr_addr_hi =
2973                 upper_32_bits(wb_gpu_addr) & 0xffff;
2974
2975         /* set up rb_wptr_poll addr */
2976         wb_gpu_addr = adev->wb.gpu_addr + (ring->wptr_offs * 4);
2977         mqd->cp_rb_wptr_poll_addr_lo = wb_gpu_addr & 0xfffffffc;
2978         mqd->cp_rb_wptr_poll_addr_hi = upper_32_bits(wb_gpu_addr) & 0xffff;
2979
2980         /* set up the gfx_hqd_control, similar as CP_RB0_CNTL */
2981         rb_bufsz = order_base_2(ring->ring_size / 4) - 1;
2982         tmp = RREG32_SOC15(GC, 0, mmCP_GFX_HQD_CNTL);
2983         tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BUFSZ, rb_bufsz);
2984         tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, RB_BLKSZ, rb_bufsz - 2);
2985 #ifdef __BIG_ENDIAN
2986         tmp = REG_SET_FIELD(tmp, CP_GFX_HQD_CNTL, BUF_SWAP, 1);
2987 #endif
2988         mqd->cp_gfx_hqd_cntl = tmp;
2989
2990         /* set up cp_doorbell_control */
2991         tmp = RREG32_SOC15(GC, 0, mmCP_RB_DOORBELL_CONTROL);
2992         if (ring->use_doorbell) {
2993                 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2994                                     DOORBELL_OFFSET, ring->doorbell_index);
2995                 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2996                                     DOORBELL_EN, 1);
2997         } else
2998                 tmp = REG_SET_FIELD(tmp, CP_RB_DOORBELL_CONTROL,
2999                                     DOORBELL_EN, 0);
3000         mqd->cp_rb_doorbell_control = tmp;
3001
3002         /* reset read and write pointers, similar to CP_RB0_WPTR/_RPTR */
3003         ring->wptr = 0;
3004         mqd->cp_gfx_hqd_rptr = RREG32_SOC15(GC, 0, mmCP_GFX_HQD_RPTR);
3005
3006         /* active the queue */
3007         mqd->cp_gfx_hqd_active = 1;
3008
3009         return 0;
3010 }
3011
3012 #ifdef BRING_UP_DEBUG
3013 static int gfx_v10_0_gfx_queue_init_register(struct amdgpu_ring *ring)
3014 {