1*837d542aSEvan Quan /*
2*837d542aSEvan Quan * Copyright 2011 Advanced Micro Devices, Inc.
3*837d542aSEvan Quan *
4*837d542aSEvan Quan * Permission is hereby granted, free of charge, to any person obtaining a
5*837d542aSEvan Quan * copy of this software and associated documentation files (the "Software"),
6*837d542aSEvan Quan * to deal in the Software without restriction, including without limitation
7*837d542aSEvan Quan * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*837d542aSEvan Quan * and/or sell copies of the Software, and to permit persons to whom the
9*837d542aSEvan Quan * Software is furnished to do so, subject to the following conditions:
10*837d542aSEvan Quan *
11*837d542aSEvan Quan * The above copyright notice and this permission notice shall be included in
12*837d542aSEvan Quan * all copies or substantial portions of the Software.
13*837d542aSEvan Quan *
14*837d542aSEvan Quan * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*837d542aSEvan Quan * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*837d542aSEvan Quan * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17*837d542aSEvan Quan * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*837d542aSEvan Quan * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*837d542aSEvan Quan * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*837d542aSEvan Quan * OTHER DEALINGS IN THE SOFTWARE.
21*837d542aSEvan Quan *
22*837d542aSEvan Quan * Authors: Alex Deucher
23*837d542aSEvan Quan */
24*837d542aSEvan Quan
25*837d542aSEvan Quan #include <linux/firmware.h>
26*837d542aSEvan Quan
27*837d542aSEvan Quan #include "amdgpu.h"
28*837d542aSEvan Quan #include "sid.h"
29*837d542aSEvan Quan #include "ppsmc.h"
30*837d542aSEvan Quan #include "amdgpu_ucode.h"
31*837d542aSEvan Quan #include "sislands_smc.h"
32*837d542aSEvan Quan
si_set_smc_sram_address(struct amdgpu_device * adev,u32 smc_address,u32 limit)33*837d542aSEvan Quan static int si_set_smc_sram_address(struct amdgpu_device *adev,
34*837d542aSEvan Quan u32 smc_address, u32 limit)
35*837d542aSEvan Quan {
36*837d542aSEvan Quan if (smc_address & 3)
37*837d542aSEvan Quan return -EINVAL;
38*837d542aSEvan Quan if ((smc_address + 3) > limit)
39*837d542aSEvan Quan return -EINVAL;
40*837d542aSEvan Quan
41*837d542aSEvan Quan WREG32(SMC_IND_INDEX_0, smc_address);
42*837d542aSEvan Quan WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
43*837d542aSEvan Quan
44*837d542aSEvan Quan return 0;
45*837d542aSEvan Quan }
46*837d542aSEvan Quan
amdgpu_si_copy_bytes_to_smc(struct amdgpu_device * adev,u32 smc_start_address,const u8 * src,u32 byte_count,u32 limit)47*837d542aSEvan Quan int amdgpu_si_copy_bytes_to_smc(struct amdgpu_device *adev,
48*837d542aSEvan Quan u32 smc_start_address,
49*837d542aSEvan Quan const u8 *src, u32 byte_count, u32 limit)
50*837d542aSEvan Quan {
51*837d542aSEvan Quan unsigned long flags;
52*837d542aSEvan Quan int ret = 0;
53*837d542aSEvan Quan u32 data, original_data, addr, extra_shift;
54*837d542aSEvan Quan
55*837d542aSEvan Quan if (smc_start_address & 3)
56*837d542aSEvan Quan return -EINVAL;
57*837d542aSEvan Quan if ((smc_start_address + byte_count) > limit)
58*837d542aSEvan Quan return -EINVAL;
59*837d542aSEvan Quan
60*837d542aSEvan Quan addr = smc_start_address;
61*837d542aSEvan Quan
62*837d542aSEvan Quan spin_lock_irqsave(&adev->smc_idx_lock, flags);
63*837d542aSEvan Quan while (byte_count >= 4) {
64*837d542aSEvan Quan /* SMC address space is BE */
65*837d542aSEvan Quan data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
66*837d542aSEvan Quan
67*837d542aSEvan Quan ret = si_set_smc_sram_address(adev, addr, limit);
68*837d542aSEvan Quan if (ret)
69*837d542aSEvan Quan goto done;
70*837d542aSEvan Quan
71*837d542aSEvan Quan WREG32(SMC_IND_DATA_0, data);
72*837d542aSEvan Quan
73*837d542aSEvan Quan src += 4;
74*837d542aSEvan Quan byte_count -= 4;
75*837d542aSEvan Quan addr += 4;
76*837d542aSEvan Quan }
77*837d542aSEvan Quan
78*837d542aSEvan Quan /* RMW for the final bytes */
79*837d542aSEvan Quan if (byte_count > 0) {
80*837d542aSEvan Quan data = 0;
81*837d542aSEvan Quan
82*837d542aSEvan Quan ret = si_set_smc_sram_address(adev, addr, limit);
83*837d542aSEvan Quan if (ret)
84*837d542aSEvan Quan goto done;
85*837d542aSEvan Quan
86*837d542aSEvan Quan original_data = RREG32(SMC_IND_DATA_0);
87*837d542aSEvan Quan extra_shift = 8 * (4 - byte_count);
88*837d542aSEvan Quan
89*837d542aSEvan Quan while (byte_count > 0) {
90*837d542aSEvan Quan /* SMC address space is BE */
91*837d542aSEvan Quan data = (data << 8) + *src++;
92*837d542aSEvan Quan byte_count--;
93*837d542aSEvan Quan }
94*837d542aSEvan Quan
95*837d542aSEvan Quan data <<= extra_shift;
96*837d542aSEvan Quan data |= (original_data & ~((~0UL) << extra_shift));
97*837d542aSEvan Quan
98*837d542aSEvan Quan ret = si_set_smc_sram_address(adev, addr, limit);
99*837d542aSEvan Quan if (ret)
100*837d542aSEvan Quan goto done;
101*837d542aSEvan Quan
102*837d542aSEvan Quan WREG32(SMC_IND_DATA_0, data);
103*837d542aSEvan Quan }
104*837d542aSEvan Quan
105*837d542aSEvan Quan done:
106*837d542aSEvan Quan spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
107*837d542aSEvan Quan
108*837d542aSEvan Quan return ret;
109*837d542aSEvan Quan }
110*837d542aSEvan Quan
amdgpu_si_start_smc(struct amdgpu_device * adev)111*837d542aSEvan Quan void amdgpu_si_start_smc(struct amdgpu_device *adev)
112*837d542aSEvan Quan {
113*837d542aSEvan Quan u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
114*837d542aSEvan Quan
115*837d542aSEvan Quan tmp &= ~RST_REG;
116*837d542aSEvan Quan
117*837d542aSEvan Quan WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
118*837d542aSEvan Quan }
119*837d542aSEvan Quan
amdgpu_si_reset_smc(struct amdgpu_device * adev)120*837d542aSEvan Quan void amdgpu_si_reset_smc(struct amdgpu_device *adev)
121*837d542aSEvan Quan {
122*837d542aSEvan Quan u32 tmp;
123*837d542aSEvan Quan
124*837d542aSEvan Quan RREG32(CB_CGTT_SCLK_CTRL);
125*837d542aSEvan Quan RREG32(CB_CGTT_SCLK_CTRL);
126*837d542aSEvan Quan RREG32(CB_CGTT_SCLK_CTRL);
127*837d542aSEvan Quan RREG32(CB_CGTT_SCLK_CTRL);
128*837d542aSEvan Quan
129*837d542aSEvan Quan tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL) |
130*837d542aSEvan Quan RST_REG;
131*837d542aSEvan Quan WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
132*837d542aSEvan Quan }
133*837d542aSEvan Quan
amdgpu_si_program_jump_on_start(struct amdgpu_device * adev)134*837d542aSEvan Quan int amdgpu_si_program_jump_on_start(struct amdgpu_device *adev)
135*837d542aSEvan Quan {
136*837d542aSEvan Quan static const u8 data[] = { 0x0E, 0x00, 0x40, 0x40 };
137*837d542aSEvan Quan
138*837d542aSEvan Quan return amdgpu_si_copy_bytes_to_smc(adev, 0x0, data, 4, sizeof(data)+1);
139*837d542aSEvan Quan }
140*837d542aSEvan Quan
amdgpu_si_smc_clock(struct amdgpu_device * adev,bool enable)141*837d542aSEvan Quan void amdgpu_si_smc_clock(struct amdgpu_device *adev, bool enable)
142*837d542aSEvan Quan {
143*837d542aSEvan Quan u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
144*837d542aSEvan Quan
145*837d542aSEvan Quan if (enable)
146*837d542aSEvan Quan tmp &= ~CK_DISABLE;
147*837d542aSEvan Quan else
148*837d542aSEvan Quan tmp |= CK_DISABLE;
149*837d542aSEvan Quan
150*837d542aSEvan Quan WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
151*837d542aSEvan Quan }
152*837d542aSEvan Quan
amdgpu_si_is_smc_running(struct amdgpu_device * adev)153*837d542aSEvan Quan bool amdgpu_si_is_smc_running(struct amdgpu_device *adev)
154*837d542aSEvan Quan {
155*837d542aSEvan Quan u32 rst = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
156*837d542aSEvan Quan u32 clk = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
157*837d542aSEvan Quan
158*837d542aSEvan Quan if (!(rst & RST_REG) && !(clk & CK_DISABLE))
159*837d542aSEvan Quan return true;
160*837d542aSEvan Quan
161*837d542aSEvan Quan return false;
162*837d542aSEvan Quan }
163*837d542aSEvan Quan
amdgpu_si_send_msg_to_smc(struct amdgpu_device * adev,PPSMC_Msg msg)164*837d542aSEvan Quan PPSMC_Result amdgpu_si_send_msg_to_smc(struct amdgpu_device *adev,
165*837d542aSEvan Quan PPSMC_Msg msg)
166*837d542aSEvan Quan {
167*837d542aSEvan Quan u32 tmp;
168*837d542aSEvan Quan int i;
169*837d542aSEvan Quan
170*837d542aSEvan Quan if (!amdgpu_si_is_smc_running(adev))
171*837d542aSEvan Quan return PPSMC_Result_Failed;
172*837d542aSEvan Quan
173*837d542aSEvan Quan WREG32(SMC_MESSAGE_0, msg);
174*837d542aSEvan Quan
175*837d542aSEvan Quan for (i = 0; i < adev->usec_timeout; i++) {
176*837d542aSEvan Quan tmp = RREG32(SMC_RESP_0);
177*837d542aSEvan Quan if (tmp != 0)
178*837d542aSEvan Quan break;
179*837d542aSEvan Quan udelay(1);
180*837d542aSEvan Quan }
181*837d542aSEvan Quan
182*837d542aSEvan Quan return (PPSMC_Result)RREG32(SMC_RESP_0);
183*837d542aSEvan Quan }
184*837d542aSEvan Quan
amdgpu_si_wait_for_smc_inactive(struct amdgpu_device * adev)185*837d542aSEvan Quan PPSMC_Result amdgpu_si_wait_for_smc_inactive(struct amdgpu_device *adev)
186*837d542aSEvan Quan {
187*837d542aSEvan Quan u32 tmp;
188*837d542aSEvan Quan int i;
189*837d542aSEvan Quan
190*837d542aSEvan Quan if (!amdgpu_si_is_smc_running(adev))
191*837d542aSEvan Quan return PPSMC_Result_OK;
192*837d542aSEvan Quan
193*837d542aSEvan Quan for (i = 0; i < adev->usec_timeout; i++) {
194*837d542aSEvan Quan tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
195*837d542aSEvan Quan if ((tmp & CKEN) == 0)
196*837d542aSEvan Quan break;
197*837d542aSEvan Quan udelay(1);
198*837d542aSEvan Quan }
199*837d542aSEvan Quan
200*837d542aSEvan Quan return PPSMC_Result_OK;
201*837d542aSEvan Quan }
202*837d542aSEvan Quan
amdgpu_si_load_smc_ucode(struct amdgpu_device * adev,u32 limit)203*837d542aSEvan Quan int amdgpu_si_load_smc_ucode(struct amdgpu_device *adev, u32 limit)
204*837d542aSEvan Quan {
205*837d542aSEvan Quan const struct smc_firmware_header_v1_0 *hdr;
206*837d542aSEvan Quan unsigned long flags;
207*837d542aSEvan Quan u32 ucode_start_address;
208*837d542aSEvan Quan u32 ucode_size;
209*837d542aSEvan Quan const u8 *src;
210*837d542aSEvan Quan u32 data;
211*837d542aSEvan Quan
212*837d542aSEvan Quan if (!adev->pm.fw)
213*837d542aSEvan Quan return -EINVAL;
214*837d542aSEvan Quan
215*837d542aSEvan Quan hdr = (const struct smc_firmware_header_v1_0 *)adev->pm.fw->data;
216*837d542aSEvan Quan
217*837d542aSEvan Quan amdgpu_ucode_print_smc_hdr(&hdr->header);
218*837d542aSEvan Quan
219*837d542aSEvan Quan adev->pm.fw_version = le32_to_cpu(hdr->header.ucode_version);
220*837d542aSEvan Quan ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
221*837d542aSEvan Quan ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
222*837d542aSEvan Quan src = (const u8 *)
223*837d542aSEvan Quan (adev->pm.fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
224*837d542aSEvan Quan if (ucode_size & 3)
225*837d542aSEvan Quan return -EINVAL;
226*837d542aSEvan Quan
227*837d542aSEvan Quan spin_lock_irqsave(&adev->smc_idx_lock, flags);
228*837d542aSEvan Quan WREG32(SMC_IND_INDEX_0, ucode_start_address);
229*837d542aSEvan Quan WREG32_P(SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, ~AUTO_INCREMENT_IND_0);
230*837d542aSEvan Quan while (ucode_size >= 4) {
231*837d542aSEvan Quan /* SMC address space is BE */
232*837d542aSEvan Quan data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
233*837d542aSEvan Quan
234*837d542aSEvan Quan WREG32(SMC_IND_DATA_0, data);
235*837d542aSEvan Quan
236*837d542aSEvan Quan src += 4;
237*837d542aSEvan Quan ucode_size -= 4;
238*837d542aSEvan Quan }
239*837d542aSEvan Quan WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
240*837d542aSEvan Quan spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
241*837d542aSEvan Quan
242*837d542aSEvan Quan return 0;
243*837d542aSEvan Quan }
244*837d542aSEvan Quan
amdgpu_si_read_smc_sram_dword(struct amdgpu_device * adev,u32 smc_address,u32 * value,u32 limit)245*837d542aSEvan Quan int amdgpu_si_read_smc_sram_dword(struct amdgpu_device *adev, u32 smc_address,
246*837d542aSEvan Quan u32 *value, u32 limit)
247*837d542aSEvan Quan {
248*837d542aSEvan Quan unsigned long flags;
249*837d542aSEvan Quan int ret;
250*837d542aSEvan Quan
251*837d542aSEvan Quan spin_lock_irqsave(&adev->smc_idx_lock, flags);
252*837d542aSEvan Quan ret = si_set_smc_sram_address(adev, smc_address, limit);
253*837d542aSEvan Quan if (ret == 0)
254*837d542aSEvan Quan *value = RREG32(SMC_IND_DATA_0);
255*837d542aSEvan Quan spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
256*837d542aSEvan Quan
257*837d542aSEvan Quan return ret;
258*837d542aSEvan Quan }
259*837d542aSEvan Quan
amdgpu_si_write_smc_sram_dword(struct amdgpu_device * adev,u32 smc_address,u32 value,u32 limit)260*837d542aSEvan Quan int amdgpu_si_write_smc_sram_dword(struct amdgpu_device *adev, u32 smc_address,
261*837d542aSEvan Quan u32 value, u32 limit)
262*837d542aSEvan Quan {
263*837d542aSEvan Quan unsigned long flags;
264*837d542aSEvan Quan int ret;
265*837d542aSEvan Quan
266*837d542aSEvan Quan spin_lock_irqsave(&adev->smc_idx_lock, flags);
267*837d542aSEvan Quan ret = si_set_smc_sram_address(adev, smc_address, limit);
268*837d542aSEvan Quan if (ret == 0)
269*837d542aSEvan Quan WREG32(SMC_IND_DATA_0, value);
270*837d542aSEvan Quan spin_unlock_irqrestore(&adev->smc_idx_lock, flags);
271*837d542aSEvan Quan
272*837d542aSEvan Quan return ret;
273*837d542aSEvan Quan }
274