1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev * Copyright 2008 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev *
4*b843c749SSergey Zigachev * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev *
11*b843c749SSergey Zigachev * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev *
14*b843c749SSergey Zigachev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17*b843c749SSergey Zigachev * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev *
22*b843c749SSergey Zigachev * Author: Stanislaw Skowronek
23*b843c749SSergey Zigachev */
24*b843c749SSergey Zigachev
25*b843c749SSergey Zigachev #include <linux/module.h>
26*b843c749SSergey Zigachev #include <linux/sched.h>
27*b843c749SSergey Zigachev #include <linux/slab.h>
28*b843c749SSergey Zigachev #include <asm/unaligned.h>
29*b843c749SSergey Zigachev
30*b843c749SSergey Zigachev #define ATOM_DEBUG
31*b843c749SSergey Zigachev
32*b843c749SSergey Zigachev #include "atom.h"
33*b843c749SSergey Zigachev #include "atom-names.h"
34*b843c749SSergey Zigachev #include "atom-bits.h"
35*b843c749SSergey Zigachev #include "amdgpu.h"
36*b843c749SSergey Zigachev
37*b843c749SSergey Zigachev #define ATOM_COND_ABOVE 0
38*b843c749SSergey Zigachev #define ATOM_COND_ABOVEOREQUAL 1
39*b843c749SSergey Zigachev #define ATOM_COND_ALWAYS 2
40*b843c749SSergey Zigachev #define ATOM_COND_BELOW 3
41*b843c749SSergey Zigachev #define ATOM_COND_BELOWOREQUAL 4
42*b843c749SSergey Zigachev #define ATOM_COND_EQUAL 5
43*b843c749SSergey Zigachev #define ATOM_COND_NOTEQUAL 6
44*b843c749SSergey Zigachev
45*b843c749SSergey Zigachev #define ATOM_PORT_ATI 0
46*b843c749SSergey Zigachev #define ATOM_PORT_PCI 1
47*b843c749SSergey Zigachev #define ATOM_PORT_SYSIO 2
48*b843c749SSergey Zigachev
49*b843c749SSergey Zigachev #define ATOM_UNIT_MICROSEC 0
50*b843c749SSergey Zigachev #define ATOM_UNIT_MILLISEC 1
51*b843c749SSergey Zigachev
52*b843c749SSergey Zigachev #define PLL_INDEX 2
53*b843c749SSergey Zigachev #define PLL_DATA 3
54*b843c749SSergey Zigachev
55*b843c749SSergey Zigachev typedef struct {
56*b843c749SSergey Zigachev struct atom_context *ctx;
57*b843c749SSergey Zigachev uint32_t *ps, *ws;
58*b843c749SSergey Zigachev int ps_shift;
59*b843c749SSergey Zigachev uint16_t start;
60*b843c749SSergey Zigachev unsigned last_jump;
61*b843c749SSergey Zigachev unsigned long last_jump_jiffies;
62*b843c749SSergey Zigachev bool abort;
63*b843c749SSergey Zigachev } atom_exec_context;
64*b843c749SSergey Zigachev
65*b843c749SSergey Zigachev int amdgpu_atom_debug = 0;
66*b843c749SSergey Zigachev static int amdgpu_atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params);
67*b843c749SSergey Zigachev
68*b843c749SSergey Zigachev static uint32_t atom_arg_mask[8] =
69*b843c749SSergey Zigachev { 0xFFFFFFFF, 0xFFFF, 0xFFFF00, 0xFFFF0000, 0xFF, 0xFF00, 0xFF0000,
70*b843c749SSergey Zigachev 0xFF000000 };
71*b843c749SSergey Zigachev static int atom_arg_shift[8] = { 0, 0, 8, 16, 0, 8, 16, 24 };
72*b843c749SSergey Zigachev
73*b843c749SSergey Zigachev static int atom_dst_to_src[8][4] = {
74*b843c749SSergey Zigachev /* translate destination alignment field to the source alignment encoding */
75*b843c749SSergey Zigachev {0, 0, 0, 0},
76*b843c749SSergey Zigachev {1, 2, 3, 0},
77*b843c749SSergey Zigachev {1, 2, 3, 0},
78*b843c749SSergey Zigachev {1, 2, 3, 0},
79*b843c749SSergey Zigachev {4, 5, 6, 7},
80*b843c749SSergey Zigachev {4, 5, 6, 7},
81*b843c749SSergey Zigachev {4, 5, 6, 7},
82*b843c749SSergey Zigachev {4, 5, 6, 7},
83*b843c749SSergey Zigachev };
84*b843c749SSergey Zigachev static int atom_def_dst[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
85*b843c749SSergey Zigachev
86*b843c749SSergey Zigachev static int debug_depth = 0;
87*b843c749SSergey Zigachev #ifdef ATOM_DEBUG
debug_print_spaces(int n)88*b843c749SSergey Zigachev static void debug_print_spaces(int n)
89*b843c749SSergey Zigachev {
90*b843c749SSergey Zigachev while (n--)
91*b843c749SSergey Zigachev printk(" ");
92*b843c749SSergey Zigachev }
93*b843c749SSergey Zigachev
94*b843c749SSergey Zigachev #define DEBUG(...) do if (amdgpu_atom_debug) { printk(KERN_DEBUG __VA_ARGS__); } while (0)
95*b843c749SSergey Zigachev #define SDEBUG(...) do if (amdgpu_atom_debug) { printk(KERN_DEBUG); debug_print_spaces(debug_depth); printk(__VA_ARGS__); } while (0)
96*b843c749SSergey Zigachev #else
97*b843c749SSergey Zigachev #define DEBUG(...) do { } while (0)
98*b843c749SSergey Zigachev #define SDEBUG(...) do { } while (0)
99*b843c749SSergey Zigachev #endif
100*b843c749SSergey Zigachev
atom_iio_execute(struct atom_context * ctx,int base,uint32_t index,uint32_t data)101*b843c749SSergey Zigachev static uint32_t atom_iio_execute(struct atom_context *ctx, int base,
102*b843c749SSergey Zigachev uint32_t index, uint32_t data)
103*b843c749SSergey Zigachev {
104*b843c749SSergey Zigachev uint32_t temp = 0xCDCDCDCD;
105*b843c749SSergey Zigachev
106*b843c749SSergey Zigachev while (1)
107*b843c749SSergey Zigachev switch (CU8(base)) {
108*b843c749SSergey Zigachev case ATOM_IIO_NOP:
109*b843c749SSergey Zigachev base++;
110*b843c749SSergey Zigachev break;
111*b843c749SSergey Zigachev case ATOM_IIO_READ:
112*b843c749SSergey Zigachev temp = ctx->card->ioreg_read(ctx->card, CU16(base + 1));
113*b843c749SSergey Zigachev base += 3;
114*b843c749SSergey Zigachev break;
115*b843c749SSergey Zigachev case ATOM_IIO_WRITE:
116*b843c749SSergey Zigachev ctx->card->ioreg_write(ctx->card, CU16(base + 1), temp);
117*b843c749SSergey Zigachev base += 3;
118*b843c749SSergey Zigachev break;
119*b843c749SSergey Zigachev case ATOM_IIO_CLEAR:
120*b843c749SSergey Zigachev temp &=
121*b843c749SSergey Zigachev ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
122*b843c749SSergey Zigachev CU8(base + 2));
123*b843c749SSergey Zigachev base += 3;
124*b843c749SSergey Zigachev break;
125*b843c749SSergey Zigachev case ATOM_IIO_SET:
126*b843c749SSergey Zigachev temp |=
127*b843c749SSergey Zigachev (0xFFFFFFFF >> (32 - CU8(base + 1))) << CU8(base +
128*b843c749SSergey Zigachev 2);
129*b843c749SSergey Zigachev base += 3;
130*b843c749SSergey Zigachev break;
131*b843c749SSergey Zigachev case ATOM_IIO_MOVE_INDEX:
132*b843c749SSergey Zigachev temp &=
133*b843c749SSergey Zigachev ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
134*b843c749SSergey Zigachev CU8(base + 3));
135*b843c749SSergey Zigachev temp |=
136*b843c749SSergey Zigachev ((index >> CU8(base + 2)) &
137*b843c749SSergey Zigachev (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
138*b843c749SSergey Zigachev 3);
139*b843c749SSergey Zigachev base += 4;
140*b843c749SSergey Zigachev break;
141*b843c749SSergey Zigachev case ATOM_IIO_MOVE_DATA:
142*b843c749SSergey Zigachev temp &=
143*b843c749SSergey Zigachev ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
144*b843c749SSergey Zigachev CU8(base + 3));
145*b843c749SSergey Zigachev temp |=
146*b843c749SSergey Zigachev ((data >> CU8(base + 2)) &
147*b843c749SSergey Zigachev (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
148*b843c749SSergey Zigachev 3);
149*b843c749SSergey Zigachev base += 4;
150*b843c749SSergey Zigachev break;
151*b843c749SSergey Zigachev case ATOM_IIO_MOVE_ATTR:
152*b843c749SSergey Zigachev temp &=
153*b843c749SSergey Zigachev ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
154*b843c749SSergey Zigachev CU8(base + 3));
155*b843c749SSergey Zigachev temp |=
156*b843c749SSergey Zigachev ((ctx->
157*b843c749SSergey Zigachev io_attr >> CU8(base + 2)) & (0xFFFFFFFF >> (32 -
158*b843c749SSergey Zigachev CU8
159*b843c749SSergey Zigachev (base
160*b843c749SSergey Zigachev +
161*b843c749SSergey Zigachev 1))))
162*b843c749SSergey Zigachev << CU8(base + 3);
163*b843c749SSergey Zigachev base += 4;
164*b843c749SSergey Zigachev break;
165*b843c749SSergey Zigachev case ATOM_IIO_END:
166*b843c749SSergey Zigachev return temp;
167*b843c749SSergey Zigachev default:
168*b843c749SSergey Zigachev pr_info("Unknown IIO opcode\n");
169*b843c749SSergey Zigachev return 0;
170*b843c749SSergey Zigachev }
171*b843c749SSergey Zigachev }
172*b843c749SSergey Zigachev
atom_get_src_int(atom_exec_context * ctx,uint8_t attr,int * ptr,uint32_t * saved,int print)173*b843c749SSergey Zigachev static uint32_t atom_get_src_int(atom_exec_context *ctx, uint8_t attr,
174*b843c749SSergey Zigachev int *ptr, uint32_t *saved, int print)
175*b843c749SSergey Zigachev {
176*b843c749SSergey Zigachev uint32_t idx, val = 0xCDCDCDCD, align, arg;
177*b843c749SSergey Zigachev struct atom_context *gctx = ctx->ctx;
178*b843c749SSergey Zigachev arg = attr & 7;
179*b843c749SSergey Zigachev align = (attr >> 3) & 7;
180*b843c749SSergey Zigachev switch (arg) {
181*b843c749SSergey Zigachev case ATOM_ARG_REG:
182*b843c749SSergey Zigachev idx = U16(*ptr);
183*b843c749SSergey Zigachev (*ptr) += 2;
184*b843c749SSergey Zigachev if (print)
185*b843c749SSergey Zigachev DEBUG("REG[0x%04X]", idx);
186*b843c749SSergey Zigachev idx += gctx->reg_block;
187*b843c749SSergey Zigachev switch (gctx->io_mode) {
188*b843c749SSergey Zigachev case ATOM_IO_MM:
189*b843c749SSergey Zigachev val = gctx->card->reg_read(gctx->card, idx);
190*b843c749SSergey Zigachev break;
191*b843c749SSergey Zigachev case ATOM_IO_PCI:
192*b843c749SSergey Zigachev pr_info("PCI registers are not implemented\n");
193*b843c749SSergey Zigachev return 0;
194*b843c749SSergey Zigachev case ATOM_IO_SYSIO:
195*b843c749SSergey Zigachev pr_info("SYSIO registers are not implemented\n");
196*b843c749SSergey Zigachev return 0;
197*b843c749SSergey Zigachev default:
198*b843c749SSergey Zigachev if (!(gctx->io_mode & 0x80)) {
199*b843c749SSergey Zigachev pr_info("Bad IO mode\n");
200*b843c749SSergey Zigachev return 0;
201*b843c749SSergey Zigachev }
202*b843c749SSergey Zigachev if (!gctx->iio[gctx->io_mode & 0x7F]) {
203*b843c749SSergey Zigachev pr_info("Undefined indirect IO read method %d\n",
204*b843c749SSergey Zigachev gctx->io_mode & 0x7F);
205*b843c749SSergey Zigachev return 0;
206*b843c749SSergey Zigachev }
207*b843c749SSergey Zigachev val =
208*b843c749SSergey Zigachev atom_iio_execute(gctx,
209*b843c749SSergey Zigachev gctx->iio[gctx->io_mode & 0x7F],
210*b843c749SSergey Zigachev idx, 0);
211*b843c749SSergey Zigachev }
212*b843c749SSergey Zigachev break;
213*b843c749SSergey Zigachev case ATOM_ARG_PS:
214*b843c749SSergey Zigachev idx = U8(*ptr);
215*b843c749SSergey Zigachev (*ptr)++;
216*b843c749SSergey Zigachev /* get_unaligned_le32 avoids unaligned accesses from atombios
217*b843c749SSergey Zigachev * tables, noticed on a DEC Alpha. */
218*b843c749SSergey Zigachev val = get_unaligned_le32((u32 *)&ctx->ps[idx]);
219*b843c749SSergey Zigachev if (print)
220*b843c749SSergey Zigachev DEBUG("PS[0x%02X,0x%04X]", idx, val);
221*b843c749SSergey Zigachev break;
222*b843c749SSergey Zigachev case ATOM_ARG_WS:
223*b843c749SSergey Zigachev idx = U8(*ptr);
224*b843c749SSergey Zigachev (*ptr)++;
225*b843c749SSergey Zigachev if (print)
226*b843c749SSergey Zigachev DEBUG("WS[0x%02X]", idx);
227*b843c749SSergey Zigachev switch (idx) {
228*b843c749SSergey Zigachev case ATOM_WS_QUOTIENT:
229*b843c749SSergey Zigachev val = gctx->divmul[0];
230*b843c749SSergey Zigachev break;
231*b843c749SSergey Zigachev case ATOM_WS_REMAINDER:
232*b843c749SSergey Zigachev val = gctx->divmul[1];
233*b843c749SSergey Zigachev break;
234*b843c749SSergey Zigachev case ATOM_WS_DATAPTR:
235*b843c749SSergey Zigachev val = gctx->data_block;
236*b843c749SSergey Zigachev break;
237*b843c749SSergey Zigachev case ATOM_WS_SHIFT:
238*b843c749SSergey Zigachev val = gctx->shift;
239*b843c749SSergey Zigachev break;
240*b843c749SSergey Zigachev case ATOM_WS_OR_MASK:
241*b843c749SSergey Zigachev val = 1 << gctx->shift;
242*b843c749SSergey Zigachev break;
243*b843c749SSergey Zigachev case ATOM_WS_AND_MASK:
244*b843c749SSergey Zigachev val = ~(1 << gctx->shift);
245*b843c749SSergey Zigachev break;
246*b843c749SSergey Zigachev case ATOM_WS_FB_WINDOW:
247*b843c749SSergey Zigachev val = gctx->fb_base;
248*b843c749SSergey Zigachev break;
249*b843c749SSergey Zigachev case ATOM_WS_ATTRIBUTES:
250*b843c749SSergey Zigachev val = gctx->io_attr;
251*b843c749SSergey Zigachev break;
252*b843c749SSergey Zigachev case ATOM_WS_REGPTR:
253*b843c749SSergey Zigachev val = gctx->reg_block;
254*b843c749SSergey Zigachev break;
255*b843c749SSergey Zigachev default:
256*b843c749SSergey Zigachev val = ctx->ws[idx];
257*b843c749SSergey Zigachev }
258*b843c749SSergey Zigachev break;
259*b843c749SSergey Zigachev case ATOM_ARG_ID:
260*b843c749SSergey Zigachev idx = U16(*ptr);
261*b843c749SSergey Zigachev (*ptr) += 2;
262*b843c749SSergey Zigachev if (print) {
263*b843c749SSergey Zigachev if (gctx->data_block)
264*b843c749SSergey Zigachev DEBUG("ID[0x%04X+%04X]", idx, gctx->data_block);
265*b843c749SSergey Zigachev else
266*b843c749SSergey Zigachev DEBUG("ID[0x%04X]", idx);
267*b843c749SSergey Zigachev }
268*b843c749SSergey Zigachev val = U32(idx + gctx->data_block);
269*b843c749SSergey Zigachev break;
270*b843c749SSergey Zigachev case ATOM_ARG_FB:
271*b843c749SSergey Zigachev idx = U8(*ptr);
272*b843c749SSergey Zigachev (*ptr)++;
273*b843c749SSergey Zigachev if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
274*b843c749SSergey Zigachev DRM_ERROR("ATOM: fb read beyond scratch region: %d vs. %d\n",
275*b843c749SSergey Zigachev gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
276*b843c749SSergey Zigachev val = 0;
277*b843c749SSergey Zigachev } else
278*b843c749SSergey Zigachev val = gctx->scratch[(gctx->fb_base / 4) + idx];
279*b843c749SSergey Zigachev if (print)
280*b843c749SSergey Zigachev DEBUG("FB[0x%02X]", idx);
281*b843c749SSergey Zigachev break;
282*b843c749SSergey Zigachev case ATOM_ARG_IMM:
283*b843c749SSergey Zigachev switch (align) {
284*b843c749SSergey Zigachev case ATOM_SRC_DWORD:
285*b843c749SSergey Zigachev val = U32(*ptr);
286*b843c749SSergey Zigachev (*ptr) += 4;
287*b843c749SSergey Zigachev if (print)
288*b843c749SSergey Zigachev DEBUG("IMM 0x%08X\n", val);
289*b843c749SSergey Zigachev return val;
290*b843c749SSergey Zigachev case ATOM_SRC_WORD0:
291*b843c749SSergey Zigachev case ATOM_SRC_WORD8:
292*b843c749SSergey Zigachev case ATOM_SRC_WORD16:
293*b843c749SSergey Zigachev val = U16(*ptr);
294*b843c749SSergey Zigachev (*ptr) += 2;
295*b843c749SSergey Zigachev if (print)
296*b843c749SSergey Zigachev DEBUG("IMM 0x%04X\n", val);
297*b843c749SSergey Zigachev return val;
298*b843c749SSergey Zigachev case ATOM_SRC_BYTE0:
299*b843c749SSergey Zigachev case ATOM_SRC_BYTE8:
300*b843c749SSergey Zigachev case ATOM_SRC_BYTE16:
301*b843c749SSergey Zigachev case ATOM_SRC_BYTE24:
302*b843c749SSergey Zigachev val = U8(*ptr);
303*b843c749SSergey Zigachev (*ptr)++;
304*b843c749SSergey Zigachev if (print)
305*b843c749SSergey Zigachev DEBUG("IMM 0x%02X\n", val);
306*b843c749SSergey Zigachev return val;
307*b843c749SSergey Zigachev }
308*b843c749SSergey Zigachev return 0;
309*b843c749SSergey Zigachev case ATOM_ARG_PLL:
310*b843c749SSergey Zigachev idx = U8(*ptr);
311*b843c749SSergey Zigachev (*ptr)++;
312*b843c749SSergey Zigachev if (print)
313*b843c749SSergey Zigachev DEBUG("PLL[0x%02X]", idx);
314*b843c749SSergey Zigachev val = gctx->card->pll_read(gctx->card, idx);
315*b843c749SSergey Zigachev break;
316*b843c749SSergey Zigachev case ATOM_ARG_MC:
317*b843c749SSergey Zigachev idx = U8(*ptr);
318*b843c749SSergey Zigachev (*ptr)++;
319*b843c749SSergey Zigachev if (print)
320*b843c749SSergey Zigachev DEBUG("MC[0x%02X]", idx);
321*b843c749SSergey Zigachev val = gctx->card->mc_read(gctx->card, idx);
322*b843c749SSergey Zigachev break;
323*b843c749SSergey Zigachev }
324*b843c749SSergey Zigachev if (saved)
325*b843c749SSergey Zigachev *saved = val;
326*b843c749SSergey Zigachev val &= atom_arg_mask[align];
327*b843c749SSergey Zigachev val >>= atom_arg_shift[align];
328*b843c749SSergey Zigachev if (print)
329*b843c749SSergey Zigachev switch (align) {
330*b843c749SSergey Zigachev case ATOM_SRC_DWORD:
331*b843c749SSergey Zigachev DEBUG(".[31:0] -> 0x%08X\n", val);
332*b843c749SSergey Zigachev break;
333*b843c749SSergey Zigachev case ATOM_SRC_WORD0:
334*b843c749SSergey Zigachev DEBUG(".[15:0] -> 0x%04X\n", val);
335*b843c749SSergey Zigachev break;
336*b843c749SSergey Zigachev case ATOM_SRC_WORD8:
337*b843c749SSergey Zigachev DEBUG(".[23:8] -> 0x%04X\n", val);
338*b843c749SSergey Zigachev break;
339*b843c749SSergey Zigachev case ATOM_SRC_WORD16:
340*b843c749SSergey Zigachev DEBUG(".[31:16] -> 0x%04X\n", val);
341*b843c749SSergey Zigachev break;
342*b843c749SSergey Zigachev case ATOM_SRC_BYTE0:
343*b843c749SSergey Zigachev DEBUG(".[7:0] -> 0x%02X\n", val);
344*b843c749SSergey Zigachev break;
345*b843c749SSergey Zigachev case ATOM_SRC_BYTE8:
346*b843c749SSergey Zigachev DEBUG(".[15:8] -> 0x%02X\n", val);
347*b843c749SSergey Zigachev break;
348*b843c749SSergey Zigachev case ATOM_SRC_BYTE16:
349*b843c749SSergey Zigachev DEBUG(".[23:16] -> 0x%02X\n", val);
350*b843c749SSergey Zigachev break;
351*b843c749SSergey Zigachev case ATOM_SRC_BYTE24:
352*b843c749SSergey Zigachev DEBUG(".[31:24] -> 0x%02X\n", val);
353*b843c749SSergey Zigachev break;
354*b843c749SSergey Zigachev }
355*b843c749SSergey Zigachev return val;
356*b843c749SSergey Zigachev }
357*b843c749SSergey Zigachev
atom_skip_src_int(atom_exec_context * ctx,uint8_t attr,int * ptr)358*b843c749SSergey Zigachev static void atom_skip_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr)
359*b843c749SSergey Zigachev {
360*b843c749SSergey Zigachev uint32_t align = (attr >> 3) & 7, arg = attr & 7;
361*b843c749SSergey Zigachev switch (arg) {
362*b843c749SSergey Zigachev case ATOM_ARG_REG:
363*b843c749SSergey Zigachev case ATOM_ARG_ID:
364*b843c749SSergey Zigachev (*ptr) += 2;
365*b843c749SSergey Zigachev break;
366*b843c749SSergey Zigachev case ATOM_ARG_PLL:
367*b843c749SSergey Zigachev case ATOM_ARG_MC:
368*b843c749SSergey Zigachev case ATOM_ARG_PS:
369*b843c749SSergey Zigachev case ATOM_ARG_WS:
370*b843c749SSergey Zigachev case ATOM_ARG_FB:
371*b843c749SSergey Zigachev (*ptr)++;
372*b843c749SSergey Zigachev break;
373*b843c749SSergey Zigachev case ATOM_ARG_IMM:
374*b843c749SSergey Zigachev switch (align) {
375*b843c749SSergey Zigachev case ATOM_SRC_DWORD:
376*b843c749SSergey Zigachev (*ptr) += 4;
377*b843c749SSergey Zigachev return;
378*b843c749SSergey Zigachev case ATOM_SRC_WORD0:
379*b843c749SSergey Zigachev case ATOM_SRC_WORD8:
380*b843c749SSergey Zigachev case ATOM_SRC_WORD16:
381*b843c749SSergey Zigachev (*ptr) += 2;
382*b843c749SSergey Zigachev return;
383*b843c749SSergey Zigachev case ATOM_SRC_BYTE0:
384*b843c749SSergey Zigachev case ATOM_SRC_BYTE8:
385*b843c749SSergey Zigachev case ATOM_SRC_BYTE16:
386*b843c749SSergey Zigachev case ATOM_SRC_BYTE24:
387*b843c749SSergey Zigachev (*ptr)++;
388*b843c749SSergey Zigachev return;
389*b843c749SSergey Zigachev }
390*b843c749SSergey Zigachev return;
391*b843c749SSergey Zigachev }
392*b843c749SSergey Zigachev }
393*b843c749SSergey Zigachev
atom_get_src(atom_exec_context * ctx,uint8_t attr,int * ptr)394*b843c749SSergey Zigachev static uint32_t atom_get_src(atom_exec_context *ctx, uint8_t attr, int *ptr)
395*b843c749SSergey Zigachev {
396*b843c749SSergey Zigachev return atom_get_src_int(ctx, attr, ptr, NULL, 1);
397*b843c749SSergey Zigachev }
398*b843c749SSergey Zigachev
atom_get_src_direct(atom_exec_context * ctx,uint8_t align,int * ptr)399*b843c749SSergey Zigachev static uint32_t atom_get_src_direct(atom_exec_context *ctx, uint8_t align, int *ptr)
400*b843c749SSergey Zigachev {
401*b843c749SSergey Zigachev uint32_t val = 0xCDCDCDCD;
402*b843c749SSergey Zigachev
403*b843c749SSergey Zigachev switch (align) {
404*b843c749SSergey Zigachev case ATOM_SRC_DWORD:
405*b843c749SSergey Zigachev val = U32(*ptr);
406*b843c749SSergey Zigachev (*ptr) += 4;
407*b843c749SSergey Zigachev break;
408*b843c749SSergey Zigachev case ATOM_SRC_WORD0:
409*b843c749SSergey Zigachev case ATOM_SRC_WORD8:
410*b843c749SSergey Zigachev case ATOM_SRC_WORD16:
411*b843c749SSergey Zigachev val = U16(*ptr);
412*b843c749SSergey Zigachev (*ptr) += 2;
413*b843c749SSergey Zigachev break;
414*b843c749SSergey Zigachev case ATOM_SRC_BYTE0:
415*b843c749SSergey Zigachev case ATOM_SRC_BYTE8:
416*b843c749SSergey Zigachev case ATOM_SRC_BYTE16:
417*b843c749SSergey Zigachev case ATOM_SRC_BYTE24:
418*b843c749SSergey Zigachev val = U8(*ptr);
419*b843c749SSergey Zigachev (*ptr)++;
420*b843c749SSergey Zigachev break;
421*b843c749SSergey Zigachev }
422*b843c749SSergey Zigachev return val;
423*b843c749SSergey Zigachev }
424*b843c749SSergey Zigachev
atom_get_dst(atom_exec_context * ctx,int arg,uint8_t attr,int * ptr,uint32_t * saved,int print)425*b843c749SSergey Zigachev static uint32_t atom_get_dst(atom_exec_context *ctx, int arg, uint8_t attr,
426*b843c749SSergey Zigachev int *ptr, uint32_t *saved, int print)
427*b843c749SSergey Zigachev {
428*b843c749SSergey Zigachev return atom_get_src_int(ctx,
429*b843c749SSergey Zigachev arg | atom_dst_to_src[(attr >> 3) &
430*b843c749SSergey Zigachev 7][(attr >> 6) & 3] << 3,
431*b843c749SSergey Zigachev ptr, saved, print);
432*b843c749SSergey Zigachev }
433*b843c749SSergey Zigachev
atom_skip_dst(atom_exec_context * ctx,int arg,uint8_t attr,int * ptr)434*b843c749SSergey Zigachev static void atom_skip_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr)
435*b843c749SSergey Zigachev {
436*b843c749SSergey Zigachev atom_skip_src_int(ctx,
437*b843c749SSergey Zigachev arg | atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) &
438*b843c749SSergey Zigachev 3] << 3, ptr);
439*b843c749SSergey Zigachev }
440*b843c749SSergey Zigachev
atom_put_dst(atom_exec_context * ctx,int arg,uint8_t attr,int * ptr,uint32_t val,uint32_t saved)441*b843c749SSergey Zigachev static void atom_put_dst(atom_exec_context *ctx, int arg, uint8_t attr,
442*b843c749SSergey Zigachev int *ptr, uint32_t val, uint32_t saved)
443*b843c749SSergey Zigachev {
444*b843c749SSergey Zigachev uint32_t align =
445*b843c749SSergey Zigachev atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3], old_val =
446*b843c749SSergey Zigachev val, idx;
447*b843c749SSergey Zigachev struct atom_context *gctx = ctx->ctx;
448*b843c749SSergey Zigachev old_val &= atom_arg_mask[align] >> atom_arg_shift[align];
449*b843c749SSergey Zigachev val <<= atom_arg_shift[align];
450*b843c749SSergey Zigachev val &= atom_arg_mask[align];
451*b843c749SSergey Zigachev saved &= ~atom_arg_mask[align];
452*b843c749SSergey Zigachev val |= saved;
453*b843c749SSergey Zigachev switch (arg) {
454*b843c749SSergey Zigachev case ATOM_ARG_REG:
455*b843c749SSergey Zigachev idx = U16(*ptr);
456*b843c749SSergey Zigachev (*ptr) += 2;
457*b843c749SSergey Zigachev DEBUG("REG[0x%04X]", idx);
458*b843c749SSergey Zigachev idx += gctx->reg_block;
459*b843c749SSergey Zigachev switch (gctx->io_mode) {
460*b843c749SSergey Zigachev case ATOM_IO_MM:
461*b843c749SSergey Zigachev if (idx == 0)
462*b843c749SSergey Zigachev gctx->card->reg_write(gctx->card, idx,
463*b843c749SSergey Zigachev val << 2);
464*b843c749SSergey Zigachev else
465*b843c749SSergey Zigachev gctx->card->reg_write(gctx->card, idx, val);
466*b843c749SSergey Zigachev break;
467*b843c749SSergey Zigachev case ATOM_IO_PCI:
468*b843c749SSergey Zigachev pr_info("PCI registers are not implemented\n");
469*b843c749SSergey Zigachev return;
470*b843c749SSergey Zigachev case ATOM_IO_SYSIO:
471*b843c749SSergey Zigachev pr_info("SYSIO registers are not implemented\n");
472*b843c749SSergey Zigachev return;
473*b843c749SSergey Zigachev default:
474*b843c749SSergey Zigachev if (!(gctx->io_mode & 0x80)) {
475*b843c749SSergey Zigachev pr_info("Bad IO mode\n");
476*b843c749SSergey Zigachev return;
477*b843c749SSergey Zigachev }
478*b843c749SSergey Zigachev if (!gctx->iio[gctx->io_mode & 0xFF]) {
479*b843c749SSergey Zigachev pr_info("Undefined indirect IO write method %d\n",
480*b843c749SSergey Zigachev gctx->io_mode & 0x7F);
481*b843c749SSergey Zigachev return;
482*b843c749SSergey Zigachev }
483*b843c749SSergey Zigachev atom_iio_execute(gctx, gctx->iio[gctx->io_mode & 0xFF],
484*b843c749SSergey Zigachev idx, val);
485*b843c749SSergey Zigachev }
486*b843c749SSergey Zigachev break;
487*b843c749SSergey Zigachev case ATOM_ARG_PS:
488*b843c749SSergey Zigachev idx = U8(*ptr);
489*b843c749SSergey Zigachev (*ptr)++;
490*b843c749SSergey Zigachev DEBUG("PS[0x%02X]", idx);
491*b843c749SSergey Zigachev ctx->ps[idx] = cpu_to_le32(val);
492*b843c749SSergey Zigachev break;
493*b843c749SSergey Zigachev case ATOM_ARG_WS:
494*b843c749SSergey Zigachev idx = U8(*ptr);
495*b843c749SSergey Zigachev (*ptr)++;
496*b843c749SSergey Zigachev DEBUG("WS[0x%02X]", idx);
497*b843c749SSergey Zigachev switch (idx) {
498*b843c749SSergey Zigachev case ATOM_WS_QUOTIENT:
499*b843c749SSergey Zigachev gctx->divmul[0] = val;
500*b843c749SSergey Zigachev break;
501*b843c749SSergey Zigachev case ATOM_WS_REMAINDER:
502*b843c749SSergey Zigachev gctx->divmul[1] = val;
503*b843c749SSergey Zigachev break;
504*b843c749SSergey Zigachev case ATOM_WS_DATAPTR:
505*b843c749SSergey Zigachev gctx->data_block = val;
506*b843c749SSergey Zigachev break;
507*b843c749SSergey Zigachev case ATOM_WS_SHIFT:
508*b843c749SSergey Zigachev gctx->shift = val;
509*b843c749SSergey Zigachev break;
510*b843c749SSergey Zigachev case ATOM_WS_OR_MASK:
511*b843c749SSergey Zigachev case ATOM_WS_AND_MASK:
512*b843c749SSergey Zigachev break;
513*b843c749SSergey Zigachev case ATOM_WS_FB_WINDOW:
514*b843c749SSergey Zigachev gctx->fb_base = val;
515*b843c749SSergey Zigachev break;
516*b843c749SSergey Zigachev case ATOM_WS_ATTRIBUTES:
517*b843c749SSergey Zigachev gctx->io_attr = val;
518*b843c749SSergey Zigachev break;
519*b843c749SSergey Zigachev case ATOM_WS_REGPTR:
520*b843c749SSergey Zigachev gctx->reg_block = val;
521*b843c749SSergey Zigachev break;
522*b843c749SSergey Zigachev default:
523*b843c749SSergey Zigachev ctx->ws[idx] = val;
524*b843c749SSergey Zigachev }
525*b843c749SSergey Zigachev break;
526*b843c749SSergey Zigachev case ATOM_ARG_FB:
527*b843c749SSergey Zigachev idx = U8(*ptr);
528*b843c749SSergey Zigachev (*ptr)++;
529*b843c749SSergey Zigachev if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
530*b843c749SSergey Zigachev DRM_ERROR("ATOM: fb write beyond scratch region: %d vs. %d\n",
531*b843c749SSergey Zigachev gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
532*b843c749SSergey Zigachev } else
533*b843c749SSergey Zigachev gctx->scratch[(gctx->fb_base / 4) + idx] = val;
534*b843c749SSergey Zigachev DEBUG("FB[0x%02X]", idx);
535*b843c749SSergey Zigachev break;
536*b843c749SSergey Zigachev case ATOM_ARG_PLL:
537*b843c749SSergey Zigachev idx = U8(*ptr);
538*b843c749SSergey Zigachev (*ptr)++;
539*b843c749SSergey Zigachev DEBUG("PLL[0x%02X]", idx);
540*b843c749SSergey Zigachev gctx->card->pll_write(gctx->card, idx, val);
541*b843c749SSergey Zigachev break;
542*b843c749SSergey Zigachev case ATOM_ARG_MC:
543*b843c749SSergey Zigachev idx = U8(*ptr);
544*b843c749SSergey Zigachev (*ptr)++;
545*b843c749SSergey Zigachev DEBUG("MC[0x%02X]", idx);
546*b843c749SSergey Zigachev gctx->card->mc_write(gctx->card, idx, val);
547*b843c749SSergey Zigachev return;
548*b843c749SSergey Zigachev }
549*b843c749SSergey Zigachev switch (align) {
550*b843c749SSergey Zigachev case ATOM_SRC_DWORD:
551*b843c749SSergey Zigachev DEBUG(".[31:0] <- 0x%08X\n", old_val);
552*b843c749SSergey Zigachev break;
553*b843c749SSergey Zigachev case ATOM_SRC_WORD0:
554*b843c749SSergey Zigachev DEBUG(".[15:0] <- 0x%04X\n", old_val);
555*b843c749SSergey Zigachev break;
556*b843c749SSergey Zigachev case ATOM_SRC_WORD8:
557*b843c749SSergey Zigachev DEBUG(".[23:8] <- 0x%04X\n", old_val);
558*b843c749SSergey Zigachev break;
559*b843c749SSergey Zigachev case ATOM_SRC_WORD16:
560*b843c749SSergey Zigachev DEBUG(".[31:16] <- 0x%04X\n", old_val);
561*b843c749SSergey Zigachev break;
562*b843c749SSergey Zigachev case ATOM_SRC_BYTE0:
563*b843c749SSergey Zigachev DEBUG(".[7:0] <- 0x%02X\n", old_val);
564*b843c749SSergey Zigachev break;
565*b843c749SSergey Zigachev case ATOM_SRC_BYTE8:
566*b843c749SSergey Zigachev DEBUG(".[15:8] <- 0x%02X\n", old_val);
567*b843c749SSergey Zigachev break;
568*b843c749SSergey Zigachev case ATOM_SRC_BYTE16:
569*b843c749SSergey Zigachev DEBUG(".[23:16] <- 0x%02X\n", old_val);
570*b843c749SSergey Zigachev break;
571*b843c749SSergey Zigachev case ATOM_SRC_BYTE24:
572*b843c749SSergey Zigachev DEBUG(".[31:24] <- 0x%02X\n", old_val);
573*b843c749SSergey Zigachev break;
574*b843c749SSergey Zigachev }
575*b843c749SSergey Zigachev }
576*b843c749SSergey Zigachev
atom_op_add(atom_exec_context * ctx,int * ptr,int arg)577*b843c749SSergey Zigachev static void atom_op_add(atom_exec_context *ctx, int *ptr, int arg)
578*b843c749SSergey Zigachev {
579*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
580*b843c749SSergey Zigachev uint32_t dst, src, saved;
581*b843c749SSergey Zigachev int dptr = *ptr;
582*b843c749SSergey Zigachev SDEBUG(" dst: ");
583*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
584*b843c749SSergey Zigachev SDEBUG(" src: ");
585*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
586*b843c749SSergey Zigachev dst += src;
587*b843c749SSergey Zigachev SDEBUG(" dst: ");
588*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
589*b843c749SSergey Zigachev }
590*b843c749SSergey Zigachev
atom_op_and(atom_exec_context * ctx,int * ptr,int arg)591*b843c749SSergey Zigachev static void atom_op_and(atom_exec_context *ctx, int *ptr, int arg)
592*b843c749SSergey Zigachev {
593*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
594*b843c749SSergey Zigachev uint32_t dst, src, saved;
595*b843c749SSergey Zigachev int dptr = *ptr;
596*b843c749SSergey Zigachev SDEBUG(" dst: ");
597*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
598*b843c749SSergey Zigachev SDEBUG(" src: ");
599*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
600*b843c749SSergey Zigachev dst &= src;
601*b843c749SSergey Zigachev SDEBUG(" dst: ");
602*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
603*b843c749SSergey Zigachev }
604*b843c749SSergey Zigachev
atom_op_beep(atom_exec_context * ctx,int * ptr,int arg)605*b843c749SSergey Zigachev static void atom_op_beep(atom_exec_context *ctx, int *ptr, int arg)
606*b843c749SSergey Zigachev {
607*b843c749SSergey Zigachev printk("ATOM BIOS beeped!\n");
608*b843c749SSergey Zigachev }
609*b843c749SSergey Zigachev
atom_op_calltable(atom_exec_context * ctx,int * ptr,int arg)610*b843c749SSergey Zigachev static void atom_op_calltable(atom_exec_context *ctx, int *ptr, int arg)
611*b843c749SSergey Zigachev {
612*b843c749SSergey Zigachev int idx = U8((*ptr)++);
613*b843c749SSergey Zigachev int r = 0;
614*b843c749SSergey Zigachev
615*b843c749SSergey Zigachev if (idx < ATOM_TABLE_NAMES_CNT)
616*b843c749SSergey Zigachev SDEBUG(" table: %d (%s)\n", idx, atom_table_names[idx]);
617*b843c749SSergey Zigachev else
618*b843c749SSergey Zigachev SDEBUG(" table: %d\n", idx);
619*b843c749SSergey Zigachev if (U16(ctx->ctx->cmd_table + 4 + 2 * idx))
620*b843c749SSergey Zigachev r = amdgpu_atom_execute_table_locked(ctx->ctx, idx, ctx->ps + ctx->ps_shift);
621*b843c749SSergey Zigachev if (r) {
622*b843c749SSergey Zigachev ctx->abort = true;
623*b843c749SSergey Zigachev }
624*b843c749SSergey Zigachev }
625*b843c749SSergey Zigachev
atom_op_clear(atom_exec_context * ctx,int * ptr,int arg)626*b843c749SSergey Zigachev static void atom_op_clear(atom_exec_context *ctx, int *ptr, int arg)
627*b843c749SSergey Zigachev {
628*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
629*b843c749SSergey Zigachev uint32_t saved;
630*b843c749SSergey Zigachev int dptr = *ptr;
631*b843c749SSergey Zigachev attr &= 0x38;
632*b843c749SSergey Zigachev attr |= atom_def_dst[attr >> 3] << 6;
633*b843c749SSergey Zigachev atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
634*b843c749SSergey Zigachev SDEBUG(" dst: ");
635*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, 0, saved);
636*b843c749SSergey Zigachev }
637*b843c749SSergey Zigachev
atom_op_compare(atom_exec_context * ctx,int * ptr,int arg)638*b843c749SSergey Zigachev static void atom_op_compare(atom_exec_context *ctx, int *ptr, int arg)
639*b843c749SSergey Zigachev {
640*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
641*b843c749SSergey Zigachev uint32_t dst, src;
642*b843c749SSergey Zigachev SDEBUG(" src1: ");
643*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
644*b843c749SSergey Zigachev SDEBUG(" src2: ");
645*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
646*b843c749SSergey Zigachev ctx->ctx->cs_equal = (dst == src);
647*b843c749SSergey Zigachev ctx->ctx->cs_above = (dst > src);
648*b843c749SSergey Zigachev SDEBUG(" result: %s %s\n", ctx->ctx->cs_equal ? "EQ" : "NE",
649*b843c749SSergey Zigachev ctx->ctx->cs_above ? "GT" : "LE");
650*b843c749SSergey Zigachev }
651*b843c749SSergey Zigachev
atom_op_delay(atom_exec_context * ctx,int * ptr,int arg)652*b843c749SSergey Zigachev static void atom_op_delay(atom_exec_context *ctx, int *ptr, int arg)
653*b843c749SSergey Zigachev {
654*b843c749SSergey Zigachev unsigned count = U8((*ptr)++);
655*b843c749SSergey Zigachev SDEBUG(" count: %d\n", count);
656*b843c749SSergey Zigachev if (arg == ATOM_UNIT_MICROSEC)
657*b843c749SSergey Zigachev udelay(count);
658*b843c749SSergey Zigachev else if (!drm_can_sleep())
659*b843c749SSergey Zigachev mdelay(count);
660*b843c749SSergey Zigachev else
661*b843c749SSergey Zigachev msleep(count);
662*b843c749SSergey Zigachev }
663*b843c749SSergey Zigachev
atom_op_div(atom_exec_context * ctx,int * ptr,int arg)664*b843c749SSergey Zigachev static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
665*b843c749SSergey Zigachev {
666*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
667*b843c749SSergey Zigachev uint32_t dst, src;
668*b843c749SSergey Zigachev SDEBUG(" src1: ");
669*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
670*b843c749SSergey Zigachev SDEBUG(" src2: ");
671*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
672*b843c749SSergey Zigachev if (src != 0) {
673*b843c749SSergey Zigachev ctx->ctx->divmul[0] = dst / src;
674*b843c749SSergey Zigachev ctx->ctx->divmul[1] = dst % src;
675*b843c749SSergey Zigachev } else {
676*b843c749SSergey Zigachev ctx->ctx->divmul[0] = 0;
677*b843c749SSergey Zigachev ctx->ctx->divmul[1] = 0;
678*b843c749SSergey Zigachev }
679*b843c749SSergey Zigachev }
680*b843c749SSergey Zigachev
atom_op_div32(atom_exec_context * ctx,int * ptr,int arg)681*b843c749SSergey Zigachev static void atom_op_div32(atom_exec_context *ctx, int *ptr, int arg)
682*b843c749SSergey Zigachev {
683*b843c749SSergey Zigachev uint64_t val64;
684*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
685*b843c749SSergey Zigachev uint32_t dst, src;
686*b843c749SSergey Zigachev SDEBUG(" src1: ");
687*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
688*b843c749SSergey Zigachev SDEBUG(" src2: ");
689*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
690*b843c749SSergey Zigachev if (src != 0) {
691*b843c749SSergey Zigachev val64 = dst;
692*b843c749SSergey Zigachev val64 |= ((uint64_t)ctx->ctx->divmul[1]) << 32;
693*b843c749SSergey Zigachev do_div(val64, src);
694*b843c749SSergey Zigachev ctx->ctx->divmul[0] = lower_32_bits(val64);
695*b843c749SSergey Zigachev ctx->ctx->divmul[1] = upper_32_bits(val64);
696*b843c749SSergey Zigachev } else {
697*b843c749SSergey Zigachev ctx->ctx->divmul[0] = 0;
698*b843c749SSergey Zigachev ctx->ctx->divmul[1] = 0;
699*b843c749SSergey Zigachev }
700*b843c749SSergey Zigachev }
701*b843c749SSergey Zigachev
atom_op_eot(atom_exec_context * ctx,int * ptr,int arg)702*b843c749SSergey Zigachev static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
703*b843c749SSergey Zigachev {
704*b843c749SSergey Zigachev /* functionally, a nop */
705*b843c749SSergey Zigachev }
706*b843c749SSergey Zigachev
atom_op_jump(atom_exec_context * ctx,int * ptr,int arg)707*b843c749SSergey Zigachev static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
708*b843c749SSergey Zigachev {
709*b843c749SSergey Zigachev int execute = 0, target = U16(*ptr);
710*b843c749SSergey Zigachev unsigned long cjiffies;
711*b843c749SSergey Zigachev
712*b843c749SSergey Zigachev (*ptr) += 2;
713*b843c749SSergey Zigachev switch (arg) {
714*b843c749SSergey Zigachev case ATOM_COND_ABOVE:
715*b843c749SSergey Zigachev execute = ctx->ctx->cs_above;
716*b843c749SSergey Zigachev break;
717*b843c749SSergey Zigachev case ATOM_COND_ABOVEOREQUAL:
718*b843c749SSergey Zigachev execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
719*b843c749SSergey Zigachev break;
720*b843c749SSergey Zigachev case ATOM_COND_ALWAYS:
721*b843c749SSergey Zigachev execute = 1;
722*b843c749SSergey Zigachev break;
723*b843c749SSergey Zigachev case ATOM_COND_BELOW:
724*b843c749SSergey Zigachev execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
725*b843c749SSergey Zigachev break;
726*b843c749SSergey Zigachev case ATOM_COND_BELOWOREQUAL:
727*b843c749SSergey Zigachev execute = !ctx->ctx->cs_above;
728*b843c749SSergey Zigachev break;
729*b843c749SSergey Zigachev case ATOM_COND_EQUAL:
730*b843c749SSergey Zigachev execute = ctx->ctx->cs_equal;
731*b843c749SSergey Zigachev break;
732*b843c749SSergey Zigachev case ATOM_COND_NOTEQUAL:
733*b843c749SSergey Zigachev execute = !ctx->ctx->cs_equal;
734*b843c749SSergey Zigachev break;
735*b843c749SSergey Zigachev }
736*b843c749SSergey Zigachev if (arg != ATOM_COND_ALWAYS)
737*b843c749SSergey Zigachev SDEBUG(" taken: %s\n", execute ? "yes" : "no");
738*b843c749SSergey Zigachev SDEBUG(" target: 0x%04X\n", target);
739*b843c749SSergey Zigachev if (execute) {
740*b843c749SSergey Zigachev if (ctx->last_jump == (ctx->start + target)) {
741*b843c749SSergey Zigachev cjiffies = jiffies;
742*b843c749SSergey Zigachev if (time_after(cjiffies, ctx->last_jump_jiffies)) {
743*b843c749SSergey Zigachev cjiffies -= ctx->last_jump_jiffies;
744*b843c749SSergey Zigachev if ((jiffies_to_msecs(cjiffies) > 10000)) {
745*b843c749SSergey Zigachev DRM_ERROR("atombios stuck in loop for more than 10secs aborting\n");
746*b843c749SSergey Zigachev ctx->abort = true;
747*b843c749SSergey Zigachev }
748*b843c749SSergey Zigachev } else {
749*b843c749SSergey Zigachev /* jiffies wrap around we will just wait a little longer */
750*b843c749SSergey Zigachev ctx->last_jump_jiffies = jiffies;
751*b843c749SSergey Zigachev }
752*b843c749SSergey Zigachev } else {
753*b843c749SSergey Zigachev ctx->last_jump = ctx->start + target;
754*b843c749SSergey Zigachev ctx->last_jump_jiffies = jiffies;
755*b843c749SSergey Zigachev }
756*b843c749SSergey Zigachev *ptr = ctx->start + target;
757*b843c749SSergey Zigachev }
758*b843c749SSergey Zigachev }
759*b843c749SSergey Zigachev
atom_op_mask(atom_exec_context * ctx,int * ptr,int arg)760*b843c749SSergey Zigachev static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
761*b843c749SSergey Zigachev {
762*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
763*b843c749SSergey Zigachev uint32_t dst, mask, src, saved;
764*b843c749SSergey Zigachev int dptr = *ptr;
765*b843c749SSergey Zigachev SDEBUG(" dst: ");
766*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
767*b843c749SSergey Zigachev mask = atom_get_src_direct(ctx, ((attr >> 3) & 7), ptr);
768*b843c749SSergey Zigachev SDEBUG(" mask: 0x%08x", mask);
769*b843c749SSergey Zigachev SDEBUG(" src: ");
770*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
771*b843c749SSergey Zigachev dst &= mask;
772*b843c749SSergey Zigachev dst |= src;
773*b843c749SSergey Zigachev SDEBUG(" dst: ");
774*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
775*b843c749SSergey Zigachev }
776*b843c749SSergey Zigachev
atom_op_move(atom_exec_context * ctx,int * ptr,int arg)777*b843c749SSergey Zigachev static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
778*b843c749SSergey Zigachev {
779*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
780*b843c749SSergey Zigachev uint32_t src, saved;
781*b843c749SSergey Zigachev int dptr = *ptr;
782*b843c749SSergey Zigachev if (((attr >> 3) & 7) != ATOM_SRC_DWORD)
783*b843c749SSergey Zigachev atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
784*b843c749SSergey Zigachev else {
785*b843c749SSergey Zigachev atom_skip_dst(ctx, arg, attr, ptr);
786*b843c749SSergey Zigachev saved = 0xCDCDCDCD;
787*b843c749SSergey Zigachev }
788*b843c749SSergey Zigachev SDEBUG(" src: ");
789*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
790*b843c749SSergey Zigachev SDEBUG(" dst: ");
791*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, src, saved);
792*b843c749SSergey Zigachev }
793*b843c749SSergey Zigachev
atom_op_mul(atom_exec_context * ctx,int * ptr,int arg)794*b843c749SSergey Zigachev static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
795*b843c749SSergey Zigachev {
796*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
797*b843c749SSergey Zigachev uint32_t dst, src;
798*b843c749SSergey Zigachev SDEBUG(" src1: ");
799*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
800*b843c749SSergey Zigachev SDEBUG(" src2: ");
801*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
802*b843c749SSergey Zigachev ctx->ctx->divmul[0] = dst * src;
803*b843c749SSergey Zigachev }
804*b843c749SSergey Zigachev
atom_op_mul32(atom_exec_context * ctx,int * ptr,int arg)805*b843c749SSergey Zigachev static void atom_op_mul32(atom_exec_context *ctx, int *ptr, int arg)
806*b843c749SSergey Zigachev {
807*b843c749SSergey Zigachev uint64_t val64;
808*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
809*b843c749SSergey Zigachev uint32_t dst, src;
810*b843c749SSergey Zigachev SDEBUG(" src1: ");
811*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
812*b843c749SSergey Zigachev SDEBUG(" src2: ");
813*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
814*b843c749SSergey Zigachev val64 = (uint64_t)dst * (uint64_t)src;
815*b843c749SSergey Zigachev ctx->ctx->divmul[0] = lower_32_bits(val64);
816*b843c749SSergey Zigachev ctx->ctx->divmul[1] = upper_32_bits(val64);
817*b843c749SSergey Zigachev }
818*b843c749SSergey Zigachev
atom_op_nop(atom_exec_context * ctx,int * ptr,int arg)819*b843c749SSergey Zigachev static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
820*b843c749SSergey Zigachev {
821*b843c749SSergey Zigachev /* nothing */
822*b843c749SSergey Zigachev }
823*b843c749SSergey Zigachev
atom_op_or(atom_exec_context * ctx,int * ptr,int arg)824*b843c749SSergey Zigachev static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
825*b843c749SSergey Zigachev {
826*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
827*b843c749SSergey Zigachev uint32_t dst, src, saved;
828*b843c749SSergey Zigachev int dptr = *ptr;
829*b843c749SSergey Zigachev SDEBUG(" dst: ");
830*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
831*b843c749SSergey Zigachev SDEBUG(" src: ");
832*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
833*b843c749SSergey Zigachev dst |= src;
834*b843c749SSergey Zigachev SDEBUG(" dst: ");
835*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
836*b843c749SSergey Zigachev }
837*b843c749SSergey Zigachev
atom_op_postcard(atom_exec_context * ctx,int * ptr,int arg)838*b843c749SSergey Zigachev static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
839*b843c749SSergey Zigachev {
840*b843c749SSergey Zigachev uint8_t val = U8((*ptr)++);
841*b843c749SSergey Zigachev SDEBUG("POST card output: 0x%02X\n", val);
842*b843c749SSergey Zigachev }
843*b843c749SSergey Zigachev
atom_op_repeat(atom_exec_context * ctx,int * ptr,int arg)844*b843c749SSergey Zigachev static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
845*b843c749SSergey Zigachev {
846*b843c749SSergey Zigachev pr_info("unimplemented!\n");
847*b843c749SSergey Zigachev }
848*b843c749SSergey Zigachev
atom_op_restorereg(atom_exec_context * ctx,int * ptr,int arg)849*b843c749SSergey Zigachev static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
850*b843c749SSergey Zigachev {
851*b843c749SSergey Zigachev pr_info("unimplemented!\n");
852*b843c749SSergey Zigachev }
853*b843c749SSergey Zigachev
atom_op_savereg(atom_exec_context * ctx,int * ptr,int arg)854*b843c749SSergey Zigachev static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
855*b843c749SSergey Zigachev {
856*b843c749SSergey Zigachev pr_info("unimplemented!\n");
857*b843c749SSergey Zigachev }
858*b843c749SSergey Zigachev
atom_op_setdatablock(atom_exec_context * ctx,int * ptr,int arg)859*b843c749SSergey Zigachev static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
860*b843c749SSergey Zigachev {
861*b843c749SSergey Zigachev int idx = U8(*ptr);
862*b843c749SSergey Zigachev (*ptr)++;
863*b843c749SSergey Zigachev SDEBUG(" block: %d\n", idx);
864*b843c749SSergey Zigachev if (!idx)
865*b843c749SSergey Zigachev ctx->ctx->data_block = 0;
866*b843c749SSergey Zigachev else if (idx == 255)
867*b843c749SSergey Zigachev ctx->ctx->data_block = ctx->start;
868*b843c749SSergey Zigachev else
869*b843c749SSergey Zigachev ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
870*b843c749SSergey Zigachev SDEBUG(" base: 0x%04X\n", ctx->ctx->data_block);
871*b843c749SSergey Zigachev }
872*b843c749SSergey Zigachev
atom_op_setfbbase(atom_exec_context * ctx,int * ptr,int arg)873*b843c749SSergey Zigachev static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
874*b843c749SSergey Zigachev {
875*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
876*b843c749SSergey Zigachev SDEBUG(" fb_base: ");
877*b843c749SSergey Zigachev ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
878*b843c749SSergey Zigachev }
879*b843c749SSergey Zigachev
atom_op_setport(atom_exec_context * ctx,int * ptr,int arg)880*b843c749SSergey Zigachev static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
881*b843c749SSergey Zigachev {
882*b843c749SSergey Zigachev int port;
883*b843c749SSergey Zigachev switch (arg) {
884*b843c749SSergey Zigachev case ATOM_PORT_ATI:
885*b843c749SSergey Zigachev port = U16(*ptr);
886*b843c749SSergey Zigachev if (port < ATOM_IO_NAMES_CNT)
887*b843c749SSergey Zigachev SDEBUG(" port: %d (%s)\n", port, atom_io_names[port]);
888*b843c749SSergey Zigachev else
889*b843c749SSergey Zigachev SDEBUG(" port: %d\n", port);
890*b843c749SSergey Zigachev if (!port)
891*b843c749SSergey Zigachev ctx->ctx->io_mode = ATOM_IO_MM;
892*b843c749SSergey Zigachev else
893*b843c749SSergey Zigachev ctx->ctx->io_mode = ATOM_IO_IIO | port;
894*b843c749SSergey Zigachev (*ptr) += 2;
895*b843c749SSergey Zigachev break;
896*b843c749SSergey Zigachev case ATOM_PORT_PCI:
897*b843c749SSergey Zigachev ctx->ctx->io_mode = ATOM_IO_PCI;
898*b843c749SSergey Zigachev (*ptr)++;
899*b843c749SSergey Zigachev break;
900*b843c749SSergey Zigachev case ATOM_PORT_SYSIO:
901*b843c749SSergey Zigachev ctx->ctx->io_mode = ATOM_IO_SYSIO;
902*b843c749SSergey Zigachev (*ptr)++;
903*b843c749SSergey Zigachev break;
904*b843c749SSergey Zigachev }
905*b843c749SSergey Zigachev }
906*b843c749SSergey Zigachev
atom_op_setregblock(atom_exec_context * ctx,int * ptr,int arg)907*b843c749SSergey Zigachev static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
908*b843c749SSergey Zigachev {
909*b843c749SSergey Zigachev ctx->ctx->reg_block = U16(*ptr);
910*b843c749SSergey Zigachev (*ptr) += 2;
911*b843c749SSergey Zigachev SDEBUG(" base: 0x%04X\n", ctx->ctx->reg_block);
912*b843c749SSergey Zigachev }
913*b843c749SSergey Zigachev
atom_op_shift_left(atom_exec_context * ctx,int * ptr,int arg)914*b843c749SSergey Zigachev static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
915*b843c749SSergey Zigachev {
916*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++), shift;
917*b843c749SSergey Zigachev uint32_t saved, dst;
918*b843c749SSergey Zigachev int dptr = *ptr;
919*b843c749SSergey Zigachev attr &= 0x38;
920*b843c749SSergey Zigachev attr |= atom_def_dst[attr >> 3] << 6;
921*b843c749SSergey Zigachev SDEBUG(" dst: ");
922*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
923*b843c749SSergey Zigachev shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
924*b843c749SSergey Zigachev SDEBUG(" shift: %d\n", shift);
925*b843c749SSergey Zigachev dst <<= shift;
926*b843c749SSergey Zigachev SDEBUG(" dst: ");
927*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
928*b843c749SSergey Zigachev }
929*b843c749SSergey Zigachev
atom_op_shift_right(atom_exec_context * ctx,int * ptr,int arg)930*b843c749SSergey Zigachev static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
931*b843c749SSergey Zigachev {
932*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++), shift;
933*b843c749SSergey Zigachev uint32_t saved, dst;
934*b843c749SSergey Zigachev int dptr = *ptr;
935*b843c749SSergey Zigachev attr &= 0x38;
936*b843c749SSergey Zigachev attr |= atom_def_dst[attr >> 3] << 6;
937*b843c749SSergey Zigachev SDEBUG(" dst: ");
938*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
939*b843c749SSergey Zigachev shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
940*b843c749SSergey Zigachev SDEBUG(" shift: %d\n", shift);
941*b843c749SSergey Zigachev dst >>= shift;
942*b843c749SSergey Zigachev SDEBUG(" dst: ");
943*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
944*b843c749SSergey Zigachev }
945*b843c749SSergey Zigachev
atom_op_shl(atom_exec_context * ctx,int * ptr,int arg)946*b843c749SSergey Zigachev static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
947*b843c749SSergey Zigachev {
948*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++), shift;
949*b843c749SSergey Zigachev uint32_t saved, dst;
950*b843c749SSergey Zigachev int dptr = *ptr;
951*b843c749SSergey Zigachev uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
952*b843c749SSergey Zigachev SDEBUG(" dst: ");
953*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
954*b843c749SSergey Zigachev /* op needs to full dst value */
955*b843c749SSergey Zigachev dst = saved;
956*b843c749SSergey Zigachev shift = atom_get_src(ctx, attr, ptr);
957*b843c749SSergey Zigachev SDEBUG(" shift: %d\n", shift);
958*b843c749SSergey Zigachev dst <<= shift;
959*b843c749SSergey Zigachev dst &= atom_arg_mask[dst_align];
960*b843c749SSergey Zigachev dst >>= atom_arg_shift[dst_align];
961*b843c749SSergey Zigachev SDEBUG(" dst: ");
962*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
963*b843c749SSergey Zigachev }
964*b843c749SSergey Zigachev
atom_op_shr(atom_exec_context * ctx,int * ptr,int arg)965*b843c749SSergey Zigachev static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
966*b843c749SSergey Zigachev {
967*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++), shift;
968*b843c749SSergey Zigachev uint32_t saved, dst;
969*b843c749SSergey Zigachev int dptr = *ptr;
970*b843c749SSergey Zigachev uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
971*b843c749SSergey Zigachev SDEBUG(" dst: ");
972*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
973*b843c749SSergey Zigachev /* op needs to full dst value */
974*b843c749SSergey Zigachev dst = saved;
975*b843c749SSergey Zigachev shift = atom_get_src(ctx, attr, ptr);
976*b843c749SSergey Zigachev SDEBUG(" shift: %d\n", shift);
977*b843c749SSergey Zigachev dst >>= shift;
978*b843c749SSergey Zigachev dst &= atom_arg_mask[dst_align];
979*b843c749SSergey Zigachev dst >>= atom_arg_shift[dst_align];
980*b843c749SSergey Zigachev SDEBUG(" dst: ");
981*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
982*b843c749SSergey Zigachev }
983*b843c749SSergey Zigachev
atom_op_sub(atom_exec_context * ctx,int * ptr,int arg)984*b843c749SSergey Zigachev static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
985*b843c749SSergey Zigachev {
986*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
987*b843c749SSergey Zigachev uint32_t dst, src, saved;
988*b843c749SSergey Zigachev int dptr = *ptr;
989*b843c749SSergey Zigachev SDEBUG(" dst: ");
990*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
991*b843c749SSergey Zigachev SDEBUG(" src: ");
992*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
993*b843c749SSergey Zigachev dst -= src;
994*b843c749SSergey Zigachev SDEBUG(" dst: ");
995*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
996*b843c749SSergey Zigachev }
997*b843c749SSergey Zigachev
atom_op_switch(atom_exec_context * ctx,int * ptr,int arg)998*b843c749SSergey Zigachev static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
999*b843c749SSergey Zigachev {
1000*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
1001*b843c749SSergey Zigachev uint32_t src, val, target;
1002*b843c749SSergey Zigachev SDEBUG(" switch: ");
1003*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
1004*b843c749SSergey Zigachev while (U16(*ptr) != ATOM_CASE_END)
1005*b843c749SSergey Zigachev if (U8(*ptr) == ATOM_CASE_MAGIC) {
1006*b843c749SSergey Zigachev (*ptr)++;
1007*b843c749SSergey Zigachev SDEBUG(" case: ");
1008*b843c749SSergey Zigachev val =
1009*b843c749SSergey Zigachev atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
1010*b843c749SSergey Zigachev ptr);
1011*b843c749SSergey Zigachev target = U16(*ptr);
1012*b843c749SSergey Zigachev if (val == src) {
1013*b843c749SSergey Zigachev SDEBUG(" target: %04X\n", target);
1014*b843c749SSergey Zigachev *ptr = ctx->start + target;
1015*b843c749SSergey Zigachev return;
1016*b843c749SSergey Zigachev }
1017*b843c749SSergey Zigachev (*ptr) += 2;
1018*b843c749SSergey Zigachev } else {
1019*b843c749SSergey Zigachev pr_info("Bad case\n");
1020*b843c749SSergey Zigachev return;
1021*b843c749SSergey Zigachev }
1022*b843c749SSergey Zigachev (*ptr) += 2;
1023*b843c749SSergey Zigachev }
1024*b843c749SSergey Zigachev
atom_op_test(atom_exec_context * ctx,int * ptr,int arg)1025*b843c749SSergey Zigachev static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
1026*b843c749SSergey Zigachev {
1027*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
1028*b843c749SSergey Zigachev uint32_t dst, src;
1029*b843c749SSergey Zigachev SDEBUG(" src1: ");
1030*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
1031*b843c749SSergey Zigachev SDEBUG(" src2: ");
1032*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
1033*b843c749SSergey Zigachev ctx->ctx->cs_equal = ((dst & src) == 0);
1034*b843c749SSergey Zigachev SDEBUG(" result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
1035*b843c749SSergey Zigachev }
1036*b843c749SSergey Zigachev
atom_op_xor(atom_exec_context * ctx,int * ptr,int arg)1037*b843c749SSergey Zigachev static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
1038*b843c749SSergey Zigachev {
1039*b843c749SSergey Zigachev uint8_t attr = U8((*ptr)++);
1040*b843c749SSergey Zigachev uint32_t dst, src, saved;
1041*b843c749SSergey Zigachev int dptr = *ptr;
1042*b843c749SSergey Zigachev SDEBUG(" dst: ");
1043*b843c749SSergey Zigachev dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
1044*b843c749SSergey Zigachev SDEBUG(" src: ");
1045*b843c749SSergey Zigachev src = atom_get_src(ctx, attr, ptr);
1046*b843c749SSergey Zigachev dst ^= src;
1047*b843c749SSergey Zigachev SDEBUG(" dst: ");
1048*b843c749SSergey Zigachev atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
1049*b843c749SSergey Zigachev }
1050*b843c749SSergey Zigachev
atom_op_debug(atom_exec_context * ctx,int * ptr,int arg)1051*b843c749SSergey Zigachev static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
1052*b843c749SSergey Zigachev {
1053*b843c749SSergey Zigachev uint8_t val = U8((*ptr)++);
1054*b843c749SSergey Zigachev SDEBUG("DEBUG output: 0x%02X\n", val);
1055*b843c749SSergey Zigachev }
1056*b843c749SSergey Zigachev
atom_op_processds(atom_exec_context * ctx,int * ptr,int arg)1057*b843c749SSergey Zigachev static void atom_op_processds(atom_exec_context *ctx, int *ptr, int arg)
1058*b843c749SSergey Zigachev {
1059*b843c749SSergey Zigachev uint16_t val = U16(*ptr);
1060*b843c749SSergey Zigachev (*ptr) += val + 2;
1061*b843c749SSergey Zigachev SDEBUG("PROCESSDS output: 0x%02X\n", val);
1062*b843c749SSergey Zigachev }
1063*b843c749SSergey Zigachev
1064*b843c749SSergey Zigachev static struct {
1065*b843c749SSergey Zigachev void (*func) (atom_exec_context *, int *, int);
1066*b843c749SSergey Zigachev int arg;
1067*b843c749SSergey Zigachev } opcode_table[ATOM_OP_CNT] = {
1068*b843c749SSergey Zigachev {
1069*b843c749SSergey Zigachev NULL, 0}, {
1070*b843c749SSergey Zigachev atom_op_move, ATOM_ARG_REG}, {
1071*b843c749SSergey Zigachev atom_op_move, ATOM_ARG_PS}, {
1072*b843c749SSergey Zigachev atom_op_move, ATOM_ARG_WS}, {
1073*b843c749SSergey Zigachev atom_op_move, ATOM_ARG_FB}, {
1074*b843c749SSergey Zigachev atom_op_move, ATOM_ARG_PLL}, {
1075*b843c749SSergey Zigachev atom_op_move, ATOM_ARG_MC}, {
1076*b843c749SSergey Zigachev atom_op_and, ATOM_ARG_REG}, {
1077*b843c749SSergey Zigachev atom_op_and, ATOM_ARG_PS}, {
1078*b843c749SSergey Zigachev atom_op_and, ATOM_ARG_WS}, {
1079*b843c749SSergey Zigachev atom_op_and, ATOM_ARG_FB}, {
1080*b843c749SSergey Zigachev atom_op_and, ATOM_ARG_PLL}, {
1081*b843c749SSergey Zigachev atom_op_and, ATOM_ARG_MC}, {
1082*b843c749SSergey Zigachev atom_op_or, ATOM_ARG_REG}, {
1083*b843c749SSergey Zigachev atom_op_or, ATOM_ARG_PS}, {
1084*b843c749SSergey Zigachev atom_op_or, ATOM_ARG_WS}, {
1085*b843c749SSergey Zigachev atom_op_or, ATOM_ARG_FB}, {
1086*b843c749SSergey Zigachev atom_op_or, ATOM_ARG_PLL}, {
1087*b843c749SSergey Zigachev atom_op_or, ATOM_ARG_MC}, {
1088*b843c749SSergey Zigachev atom_op_shift_left, ATOM_ARG_REG}, {
1089*b843c749SSergey Zigachev atom_op_shift_left, ATOM_ARG_PS}, {
1090*b843c749SSergey Zigachev atom_op_shift_left, ATOM_ARG_WS}, {
1091*b843c749SSergey Zigachev atom_op_shift_left, ATOM_ARG_FB}, {
1092*b843c749SSergey Zigachev atom_op_shift_left, ATOM_ARG_PLL}, {
1093*b843c749SSergey Zigachev atom_op_shift_left, ATOM_ARG_MC}, {
1094*b843c749SSergey Zigachev atom_op_shift_right, ATOM_ARG_REG}, {
1095*b843c749SSergey Zigachev atom_op_shift_right, ATOM_ARG_PS}, {
1096*b843c749SSergey Zigachev atom_op_shift_right, ATOM_ARG_WS}, {
1097*b843c749SSergey Zigachev atom_op_shift_right, ATOM_ARG_FB}, {
1098*b843c749SSergey Zigachev atom_op_shift_right, ATOM_ARG_PLL}, {
1099*b843c749SSergey Zigachev atom_op_shift_right, ATOM_ARG_MC}, {
1100*b843c749SSergey Zigachev atom_op_mul, ATOM_ARG_REG}, {
1101*b843c749SSergey Zigachev atom_op_mul, ATOM_ARG_PS}, {
1102*b843c749SSergey Zigachev atom_op_mul, ATOM_ARG_WS}, {
1103*b843c749SSergey Zigachev atom_op_mul, ATOM_ARG_FB}, {
1104*b843c749SSergey Zigachev atom_op_mul, ATOM_ARG_PLL}, {
1105*b843c749SSergey Zigachev atom_op_mul, ATOM_ARG_MC}, {
1106*b843c749SSergey Zigachev atom_op_div, ATOM_ARG_REG}, {
1107*b843c749SSergey Zigachev atom_op_div, ATOM_ARG_PS}, {
1108*b843c749SSergey Zigachev atom_op_div, ATOM_ARG_WS}, {
1109*b843c749SSergey Zigachev atom_op_div, ATOM_ARG_FB}, {
1110*b843c749SSergey Zigachev atom_op_div, ATOM_ARG_PLL}, {
1111*b843c749SSergey Zigachev atom_op_div, ATOM_ARG_MC}, {
1112*b843c749SSergey Zigachev atom_op_add, ATOM_ARG_REG}, {
1113*b843c749SSergey Zigachev atom_op_add, ATOM_ARG_PS}, {
1114*b843c749SSergey Zigachev atom_op_add, ATOM_ARG_WS}, {
1115*b843c749SSergey Zigachev atom_op_add, ATOM_ARG_FB}, {
1116*b843c749SSergey Zigachev atom_op_add, ATOM_ARG_PLL}, {
1117*b843c749SSergey Zigachev atom_op_add, ATOM_ARG_MC}, {
1118*b843c749SSergey Zigachev atom_op_sub, ATOM_ARG_REG}, {
1119*b843c749SSergey Zigachev atom_op_sub, ATOM_ARG_PS}, {
1120*b843c749SSergey Zigachev atom_op_sub, ATOM_ARG_WS}, {
1121*b843c749SSergey Zigachev atom_op_sub, ATOM_ARG_FB}, {
1122*b843c749SSergey Zigachev atom_op_sub, ATOM_ARG_PLL}, {
1123*b843c749SSergey Zigachev atom_op_sub, ATOM_ARG_MC}, {
1124*b843c749SSergey Zigachev atom_op_setport, ATOM_PORT_ATI}, {
1125*b843c749SSergey Zigachev atom_op_setport, ATOM_PORT_PCI}, {
1126*b843c749SSergey Zigachev atom_op_setport, ATOM_PORT_SYSIO}, {
1127*b843c749SSergey Zigachev atom_op_setregblock, 0}, {
1128*b843c749SSergey Zigachev atom_op_setfbbase, 0}, {
1129*b843c749SSergey Zigachev atom_op_compare, ATOM_ARG_REG}, {
1130*b843c749SSergey Zigachev atom_op_compare, ATOM_ARG_PS}, {
1131*b843c749SSergey Zigachev atom_op_compare, ATOM_ARG_WS}, {
1132*b843c749SSergey Zigachev atom_op_compare, ATOM_ARG_FB}, {
1133*b843c749SSergey Zigachev atom_op_compare, ATOM_ARG_PLL}, {
1134*b843c749SSergey Zigachev atom_op_compare, ATOM_ARG_MC}, {
1135*b843c749SSergey Zigachev atom_op_switch, 0}, {
1136*b843c749SSergey Zigachev atom_op_jump, ATOM_COND_ALWAYS}, {
1137*b843c749SSergey Zigachev atom_op_jump, ATOM_COND_EQUAL}, {
1138*b843c749SSergey Zigachev atom_op_jump, ATOM_COND_BELOW}, {
1139*b843c749SSergey Zigachev atom_op_jump, ATOM_COND_ABOVE}, {
1140*b843c749SSergey Zigachev atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
1141*b843c749SSergey Zigachev atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
1142*b843c749SSergey Zigachev atom_op_jump, ATOM_COND_NOTEQUAL}, {
1143*b843c749SSergey Zigachev atom_op_test, ATOM_ARG_REG}, {
1144*b843c749SSergey Zigachev atom_op_test, ATOM_ARG_PS}, {
1145*b843c749SSergey Zigachev atom_op_test, ATOM_ARG_WS}, {
1146*b843c749SSergey Zigachev atom_op_test, ATOM_ARG_FB}, {
1147*b843c749SSergey Zigachev atom_op_test, ATOM_ARG_PLL}, {
1148*b843c749SSergey Zigachev atom_op_test, ATOM_ARG_MC}, {
1149*b843c749SSergey Zigachev atom_op_delay, ATOM_UNIT_MILLISEC}, {
1150*b843c749SSergey Zigachev atom_op_delay, ATOM_UNIT_MICROSEC}, {
1151*b843c749SSergey Zigachev atom_op_calltable, 0}, {
1152*b843c749SSergey Zigachev atom_op_repeat, 0}, {
1153*b843c749SSergey Zigachev atom_op_clear, ATOM_ARG_REG}, {
1154*b843c749SSergey Zigachev atom_op_clear, ATOM_ARG_PS}, {
1155*b843c749SSergey Zigachev atom_op_clear, ATOM_ARG_WS}, {
1156*b843c749SSergey Zigachev atom_op_clear, ATOM_ARG_FB}, {
1157*b843c749SSergey Zigachev atom_op_clear, ATOM_ARG_PLL}, {
1158*b843c749SSergey Zigachev atom_op_clear, ATOM_ARG_MC}, {
1159*b843c749SSergey Zigachev atom_op_nop, 0}, {
1160*b843c749SSergey Zigachev atom_op_eot, 0}, {
1161*b843c749SSergey Zigachev atom_op_mask, ATOM_ARG_REG}, {
1162*b843c749SSergey Zigachev atom_op_mask, ATOM_ARG_PS}, {
1163*b843c749SSergey Zigachev atom_op_mask, ATOM_ARG_WS}, {
1164*b843c749SSergey Zigachev atom_op_mask, ATOM_ARG_FB}, {
1165*b843c749SSergey Zigachev atom_op_mask, ATOM_ARG_PLL}, {
1166*b843c749SSergey Zigachev atom_op_mask, ATOM_ARG_MC}, {
1167*b843c749SSergey Zigachev atom_op_postcard, 0}, {
1168*b843c749SSergey Zigachev atom_op_beep, 0}, {
1169*b843c749SSergey Zigachev atom_op_savereg, 0}, {
1170*b843c749SSergey Zigachev atom_op_restorereg, 0}, {
1171*b843c749SSergey Zigachev atom_op_setdatablock, 0}, {
1172*b843c749SSergey Zigachev atom_op_xor, ATOM_ARG_REG}, {
1173*b843c749SSergey Zigachev atom_op_xor, ATOM_ARG_PS}, {
1174*b843c749SSergey Zigachev atom_op_xor, ATOM_ARG_WS}, {
1175*b843c749SSergey Zigachev atom_op_xor, ATOM_ARG_FB}, {
1176*b843c749SSergey Zigachev atom_op_xor, ATOM_ARG_PLL}, {
1177*b843c749SSergey Zigachev atom_op_xor, ATOM_ARG_MC}, {
1178*b843c749SSergey Zigachev atom_op_shl, ATOM_ARG_REG}, {
1179*b843c749SSergey Zigachev atom_op_shl, ATOM_ARG_PS}, {
1180*b843c749SSergey Zigachev atom_op_shl, ATOM_ARG_WS}, {
1181*b843c749SSergey Zigachev atom_op_shl, ATOM_ARG_FB}, {
1182*b843c749SSergey Zigachev atom_op_shl, ATOM_ARG_PLL}, {
1183*b843c749SSergey Zigachev atom_op_shl, ATOM_ARG_MC}, {
1184*b843c749SSergey Zigachev atom_op_shr, ATOM_ARG_REG}, {
1185*b843c749SSergey Zigachev atom_op_shr, ATOM_ARG_PS}, {
1186*b843c749SSergey Zigachev atom_op_shr, ATOM_ARG_WS}, {
1187*b843c749SSergey Zigachev atom_op_shr, ATOM_ARG_FB}, {
1188*b843c749SSergey Zigachev atom_op_shr, ATOM_ARG_PLL}, {
1189*b843c749SSergey Zigachev atom_op_shr, ATOM_ARG_MC}, {
1190*b843c749SSergey Zigachev atom_op_debug, 0}, {
1191*b843c749SSergey Zigachev atom_op_processds, 0}, {
1192*b843c749SSergey Zigachev atom_op_mul32, ATOM_ARG_PS}, {
1193*b843c749SSergey Zigachev atom_op_mul32, ATOM_ARG_WS}, {
1194*b843c749SSergey Zigachev atom_op_div32, ATOM_ARG_PS}, {
1195*b843c749SSergey Zigachev atom_op_div32, ATOM_ARG_WS},
1196*b843c749SSergey Zigachev };
1197*b843c749SSergey Zigachev
amdgpu_atom_execute_table_locked(struct atom_context * ctx,int index,uint32_t * params)1198*b843c749SSergey Zigachev static int amdgpu_atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
1199*b843c749SSergey Zigachev {
1200*b843c749SSergey Zigachev int base = CU16(ctx->cmd_table + 4 + 2 * index);
1201*b843c749SSergey Zigachev int len, ws, ps, ptr;
1202*b843c749SSergey Zigachev unsigned char op;
1203*b843c749SSergey Zigachev atom_exec_context ectx;
1204*b843c749SSergey Zigachev int ret = 0;
1205*b843c749SSergey Zigachev
1206*b843c749SSergey Zigachev if (!base)
1207*b843c749SSergey Zigachev return -EINVAL;
1208*b843c749SSergey Zigachev
1209*b843c749SSergey Zigachev len = CU16(base + ATOM_CT_SIZE_PTR);
1210*b843c749SSergey Zigachev ws = CU8(base + ATOM_CT_WS_PTR);
1211*b843c749SSergey Zigachev ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
1212*b843c749SSergey Zigachev ptr = base + ATOM_CT_CODE_PTR;
1213*b843c749SSergey Zigachev
1214*b843c749SSergey Zigachev SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
1215*b843c749SSergey Zigachev
1216*b843c749SSergey Zigachev ectx.ctx = ctx;
1217*b843c749SSergey Zigachev ectx.ps_shift = ps / 4;
1218*b843c749SSergey Zigachev ectx.start = base;
1219*b843c749SSergey Zigachev ectx.ps = params;
1220*b843c749SSergey Zigachev ectx.abort = false;
1221*b843c749SSergey Zigachev ectx.last_jump = 0;
1222*b843c749SSergey Zigachev if (ws)
1223*b843c749SSergey Zigachev ectx.ws = kcalloc(4, ws, GFP_KERNEL);
1224*b843c749SSergey Zigachev else
1225*b843c749SSergey Zigachev ectx.ws = NULL;
1226*b843c749SSergey Zigachev
1227*b843c749SSergey Zigachev debug_depth++;
1228*b843c749SSergey Zigachev while (1) {
1229*b843c749SSergey Zigachev op = CU8(ptr++);
1230*b843c749SSergey Zigachev if (op < ATOM_OP_NAMES_CNT)
1231*b843c749SSergey Zigachev SDEBUG("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
1232*b843c749SSergey Zigachev else
1233*b843c749SSergey Zigachev SDEBUG("[%d] @ 0x%04X\n", op, ptr - 1);
1234*b843c749SSergey Zigachev if (ectx.abort) {
1235*b843c749SSergey Zigachev DRM_ERROR("atombios stuck executing %04X (len %d, WS %d, PS %d) @ 0x%04X\n",
1236*b843c749SSergey Zigachev base, len, ws, ps, ptr - 1);
1237*b843c749SSergey Zigachev ret = -EINVAL;
1238*b843c749SSergey Zigachev goto free;
1239*b843c749SSergey Zigachev }
1240*b843c749SSergey Zigachev
1241*b843c749SSergey Zigachev if (op < ATOM_OP_CNT && op > 0)
1242*b843c749SSergey Zigachev opcode_table[op].func(&ectx, &ptr,
1243*b843c749SSergey Zigachev opcode_table[op].arg);
1244*b843c749SSergey Zigachev else
1245*b843c749SSergey Zigachev break;
1246*b843c749SSergey Zigachev
1247*b843c749SSergey Zigachev if (op == ATOM_OP_EOT)
1248*b843c749SSergey Zigachev break;
1249*b843c749SSergey Zigachev }
1250*b843c749SSergey Zigachev debug_depth--;
1251*b843c749SSergey Zigachev SDEBUG("<<\n");
1252*b843c749SSergey Zigachev
1253*b843c749SSergey Zigachev free:
1254*b843c749SSergey Zigachev if (ws)
1255*b843c749SSergey Zigachev kfree(ectx.ws);
1256*b843c749SSergey Zigachev return ret;
1257*b843c749SSergey Zigachev }
1258*b843c749SSergey Zigachev
amdgpu_atom_execute_table(struct atom_context * ctx,int index,uint32_t * params)1259*b843c749SSergey Zigachev int amdgpu_atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
1260*b843c749SSergey Zigachev {
1261*b843c749SSergey Zigachev int r;
1262*b843c749SSergey Zigachev
1263*b843c749SSergey Zigachev mutex_lock(&ctx->mutex);
1264*b843c749SSergey Zigachev /* reset data block */
1265*b843c749SSergey Zigachev ctx->data_block = 0;
1266*b843c749SSergey Zigachev /* reset reg block */
1267*b843c749SSergey Zigachev ctx->reg_block = 0;
1268*b843c749SSergey Zigachev /* reset fb window */
1269*b843c749SSergey Zigachev ctx->fb_base = 0;
1270*b843c749SSergey Zigachev /* reset io mode */
1271*b843c749SSergey Zigachev ctx->io_mode = ATOM_IO_MM;
1272*b843c749SSergey Zigachev /* reset divmul */
1273*b843c749SSergey Zigachev ctx->divmul[0] = 0;
1274*b843c749SSergey Zigachev ctx->divmul[1] = 0;
1275*b843c749SSergey Zigachev r = amdgpu_atom_execute_table_locked(ctx, index, params);
1276*b843c749SSergey Zigachev mutex_unlock(&ctx->mutex);
1277*b843c749SSergey Zigachev return r;
1278*b843c749SSergey Zigachev }
1279*b843c749SSergey Zigachev
1280*b843c749SSergey Zigachev static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1281*b843c749SSergey Zigachev
atom_index_iio(struct atom_context * ctx,int base)1282*b843c749SSergey Zigachev static void atom_index_iio(struct atom_context *ctx, int base)
1283*b843c749SSergey Zigachev {
1284*b843c749SSergey Zigachev ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
1285*b843c749SSergey Zigachev if (!ctx->iio)
1286*b843c749SSergey Zigachev return;
1287*b843c749SSergey Zigachev while (CU8(base) == ATOM_IIO_START) {
1288*b843c749SSergey Zigachev ctx->iio[CU8(base + 1)] = base + 2;
1289*b843c749SSergey Zigachev base += 2;
1290*b843c749SSergey Zigachev while (CU8(base) != ATOM_IIO_END)
1291*b843c749SSergey Zigachev base += atom_iio_len[CU8(base)];
1292*b843c749SSergey Zigachev base += 3;
1293*b843c749SSergey Zigachev }
1294*b843c749SSergey Zigachev }
1295*b843c749SSergey Zigachev
amdgpu_atom_parse(struct card_info * card,void * bios)1296*b843c749SSergey Zigachev struct atom_context *amdgpu_atom_parse(struct card_info *card, void *bios)
1297*b843c749SSergey Zigachev {
1298*b843c749SSergey Zigachev int base;
1299*b843c749SSergey Zigachev struct atom_context *ctx =
1300*b843c749SSergey Zigachev kzalloc(sizeof(struct atom_context), GFP_KERNEL);
1301*b843c749SSergey Zigachev char *str;
1302*b843c749SSergey Zigachev u16 idx;
1303*b843c749SSergey Zigachev
1304*b843c749SSergey Zigachev if (!ctx)
1305*b843c749SSergey Zigachev return NULL;
1306*b843c749SSergey Zigachev
1307*b843c749SSergey Zigachev ctx->card = card;
1308*b843c749SSergey Zigachev ctx->bios = bios;
1309*b843c749SSergey Zigachev
1310*b843c749SSergey Zigachev if (CU16(0) != ATOM_BIOS_MAGIC) {
1311*b843c749SSergey Zigachev pr_info("Invalid BIOS magic\n");
1312*b843c749SSergey Zigachev kfree(ctx);
1313*b843c749SSergey Zigachev return NULL;
1314*b843c749SSergey Zigachev }
1315*b843c749SSergey Zigachev if (strncmp
1316*b843c749SSergey Zigachev (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
1317*b843c749SSergey Zigachev strlen(ATOM_ATI_MAGIC))) {
1318*b843c749SSergey Zigachev pr_info("Invalid ATI magic\n");
1319*b843c749SSergey Zigachev kfree(ctx);
1320*b843c749SSergey Zigachev return NULL;
1321*b843c749SSergey Zigachev }
1322*b843c749SSergey Zigachev
1323*b843c749SSergey Zigachev base = CU16(ATOM_ROM_TABLE_PTR);
1324*b843c749SSergey Zigachev if (strncmp
1325*b843c749SSergey Zigachev (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
1326*b843c749SSergey Zigachev strlen(ATOM_ROM_MAGIC))) {
1327*b843c749SSergey Zigachev pr_info("Invalid ATOM magic\n");
1328*b843c749SSergey Zigachev kfree(ctx);
1329*b843c749SSergey Zigachev return NULL;
1330*b843c749SSergey Zigachev }
1331*b843c749SSergey Zigachev
1332*b843c749SSergey Zigachev ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
1333*b843c749SSergey Zigachev ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
1334*b843c749SSergey Zigachev atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
1335*b843c749SSergey Zigachev if (!ctx->iio) {
1336*b843c749SSergey Zigachev amdgpu_atom_destroy(ctx);
1337*b843c749SSergey Zigachev return NULL;
1338*b843c749SSergey Zigachev }
1339*b843c749SSergey Zigachev
1340*b843c749SSergey Zigachev idx = CU16(ATOM_ROM_PART_NUMBER_PTR);
1341*b843c749SSergey Zigachev if (idx == 0)
1342*b843c749SSergey Zigachev idx = 0x80;
1343*b843c749SSergey Zigachev
1344*b843c749SSergey Zigachev str = CSTR(idx);
1345*b843c749SSergey Zigachev if (*str != '\0') {
1346*b843c749SSergey Zigachev pr_info("ATOM BIOS: %s\n", str);
1347*b843c749SSergey Zigachev strlcpy(ctx->vbios_version, str, sizeof(ctx->vbios_version));
1348*b843c749SSergey Zigachev }
1349*b843c749SSergey Zigachev
1350*b843c749SSergey Zigachev
1351*b843c749SSergey Zigachev return ctx;
1352*b843c749SSergey Zigachev }
1353*b843c749SSergey Zigachev
amdgpu_atom_asic_init(struct atom_context * ctx)1354*b843c749SSergey Zigachev int amdgpu_atom_asic_init(struct atom_context *ctx)
1355*b843c749SSergey Zigachev {
1356*b843c749SSergey Zigachev int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
1357*b843c749SSergey Zigachev uint32_t ps[16];
1358*b843c749SSergey Zigachev int ret;
1359*b843c749SSergey Zigachev
1360*b843c749SSergey Zigachev memset(ps, 0, 64);
1361*b843c749SSergey Zigachev
1362*b843c749SSergey Zigachev ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
1363*b843c749SSergey Zigachev ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
1364*b843c749SSergey Zigachev if (!ps[0] || !ps[1])
1365*b843c749SSergey Zigachev return 1;
1366*b843c749SSergey Zigachev
1367*b843c749SSergey Zigachev if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
1368*b843c749SSergey Zigachev return 1;
1369*b843c749SSergey Zigachev ret = amdgpu_atom_execute_table(ctx, ATOM_CMD_INIT, ps);
1370*b843c749SSergey Zigachev if (ret)
1371*b843c749SSergey Zigachev return ret;
1372*b843c749SSergey Zigachev
1373*b843c749SSergey Zigachev memset(ps, 0, 64);
1374*b843c749SSergey Zigachev
1375*b843c749SSergey Zigachev return ret;
1376*b843c749SSergey Zigachev }
1377*b843c749SSergey Zigachev
amdgpu_atom_destroy(struct atom_context * ctx)1378*b843c749SSergey Zigachev void amdgpu_atom_destroy(struct atom_context *ctx)
1379*b843c749SSergey Zigachev {
1380*b843c749SSergey Zigachev kfree(ctx->iio);
1381*b843c749SSergey Zigachev kfree(ctx);
1382*b843c749SSergey Zigachev }
1383*b843c749SSergey Zigachev
amdgpu_atom_parse_data_header(struct atom_context * ctx,int index,uint16_t * size,uint8_t * frev,uint8_t * crev,uint16_t * data_start)1384*b843c749SSergey Zigachev bool amdgpu_atom_parse_data_header(struct atom_context *ctx, int index,
1385*b843c749SSergey Zigachev uint16_t * size, uint8_t * frev, uint8_t * crev,
1386*b843c749SSergey Zigachev uint16_t * data_start)
1387*b843c749SSergey Zigachev {
1388*b843c749SSergey Zigachev int offset = index * 2 + 4;
1389*b843c749SSergey Zigachev int idx = CU16(ctx->data_table + offset);
1390*b843c749SSergey Zigachev u16 *mdt = (u16 *)(ctx->bios + ctx->data_table + 4);
1391*b843c749SSergey Zigachev
1392*b843c749SSergey Zigachev if (!mdt[index])
1393*b843c749SSergey Zigachev return false;
1394*b843c749SSergey Zigachev
1395*b843c749SSergey Zigachev if (size)
1396*b843c749SSergey Zigachev *size = CU16(idx);
1397*b843c749SSergey Zigachev if (frev)
1398*b843c749SSergey Zigachev *frev = CU8(idx + 2);
1399*b843c749SSergey Zigachev if (crev)
1400*b843c749SSergey Zigachev *crev = CU8(idx + 3);
1401*b843c749SSergey Zigachev *data_start = idx;
1402*b843c749SSergey Zigachev return true;
1403*b843c749SSergey Zigachev }
1404*b843c749SSergey Zigachev
amdgpu_atom_parse_cmd_header(struct atom_context * ctx,int index,uint8_t * frev,uint8_t * crev)1405*b843c749SSergey Zigachev bool amdgpu_atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
1406*b843c749SSergey Zigachev uint8_t * crev)
1407*b843c749SSergey Zigachev {
1408*b843c749SSergey Zigachev int offset = index * 2 + 4;
1409*b843c749SSergey Zigachev int idx = CU16(ctx->cmd_table + offset);
1410*b843c749SSergey Zigachev u16 *mct = (u16 *)(ctx->bios + ctx->cmd_table + 4);
1411*b843c749SSergey Zigachev
1412*b843c749SSergey Zigachev if (!mct[index])
1413*b843c749SSergey Zigachev return false;
1414*b843c749SSergey Zigachev
1415*b843c749SSergey Zigachev if (frev)
1416*b843c749SSergey Zigachev *frev = CU8(idx + 2);
1417*b843c749SSergey Zigachev if (crev)
1418*b843c749SSergey Zigachev *crev = CU8(idx + 3);
1419*b843c749SSergey Zigachev return true;
1420*b843c749SSergey Zigachev }
1421*b843c749SSergey Zigachev
1422