xref: /dragonfly/sys/dev/drm/radeon/atom.c (revision d78d3a22)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2008 Advanced Micro Devices, Inc.
3926deccbSFrançois Tigeot  *
4926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6926deccbSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7926deccbSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8926deccbSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9926deccbSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10926deccbSFrançois Tigeot  *
11926deccbSFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
12926deccbSFrançois Tigeot  * all copies or substantial portions of the Software.
13926deccbSFrançois Tigeot  *
14926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18926deccbSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19926deccbSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20926deccbSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
21926deccbSFrançois Tigeot  *
22926deccbSFrançois Tigeot  * Author: Stanislaw Skowronek
23926deccbSFrançois Tigeot  */
24926deccbSFrançois Tigeot 
25dbb5897cSFrançois Tigeot #include <linux/module.h>
26dbb5897cSFrançois Tigeot #include <linux/sched.h>
27dbb5897cSFrançois Tigeot #include <asm/unaligned.h>
28dbb5897cSFrançois Tigeot 
29926deccbSFrançois Tigeot #define ATOM_DEBUG
30926deccbSFrançois Tigeot 
31926deccbSFrançois Tigeot #include "atom.h"
32926deccbSFrançois Tigeot #include "atom-names.h"
33926deccbSFrançois Tigeot #include "atom-bits.h"
34926deccbSFrançois Tigeot #include "radeon.h"
353ff03fddSFrançois Tigeot #include <linux/delay.h>
36926deccbSFrançois Tigeot 
37926deccbSFrançois Tigeot #define ATOM_COND_ABOVE		0
38926deccbSFrançois Tigeot #define ATOM_COND_ABOVEOREQUAL	1
39926deccbSFrançois Tigeot #define ATOM_COND_ALWAYS	2
40926deccbSFrançois Tigeot #define ATOM_COND_BELOW		3
41926deccbSFrançois Tigeot #define ATOM_COND_BELOWOREQUAL	4
42926deccbSFrançois Tigeot #define ATOM_COND_EQUAL		5
43926deccbSFrançois Tigeot #define ATOM_COND_NOTEQUAL	6
44926deccbSFrançois Tigeot 
45926deccbSFrançois Tigeot #define ATOM_PORT_ATI	0
46926deccbSFrançois Tigeot #define ATOM_PORT_PCI	1
47926deccbSFrançois Tigeot #define ATOM_PORT_SYSIO	2
48926deccbSFrançois Tigeot 
49926deccbSFrançois Tigeot #define ATOM_UNIT_MICROSEC	0
50926deccbSFrançois Tigeot #define ATOM_UNIT_MILLISEC	1
51926deccbSFrançois Tigeot 
52926deccbSFrançois Tigeot #define PLL_INDEX	2
53926deccbSFrançois Tigeot #define PLL_DATA	3
54926deccbSFrançois Tigeot 
55926deccbSFrançois Tigeot typedef struct {
56926deccbSFrançois Tigeot 	struct atom_context *ctx;
57926deccbSFrançois Tigeot 	uint32_t *ps, *ws;
58926deccbSFrançois Tigeot 	int ps_shift;
59926deccbSFrançois Tigeot 	uint16_t start;
60926deccbSFrançois Tigeot 	unsigned last_jump;
61926deccbSFrançois Tigeot 	unsigned long last_jump_jiffies;
62926deccbSFrançois Tigeot 	bool abort;
63926deccbSFrançois Tigeot } atom_exec_context;
64926deccbSFrançois Tigeot 
65926deccbSFrançois Tigeot int atom_debug = 0;
66926deccbSFrançois Tigeot static int atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params);
67926deccbSFrançois Tigeot 
68*d78d3a22SFrançois Tigeot static uint32_t atom_arg_mask[8] = {
69*d78d3a22SFrançois Tigeot 	0xFFFFFFFF, 0x0000FFFF, 0x00FFFF00, 0xFFFF0000,
70*d78d3a22SFrançois Tigeot 	0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
71*d78d3a22SFrançois Tigeot };
72926deccbSFrançois Tigeot static int atom_arg_shift[8] = { 0, 0, 8, 16, 0, 8, 16, 24 };
73926deccbSFrançois Tigeot 
74926deccbSFrançois Tigeot static int atom_dst_to_src[8][4] = {
75926deccbSFrançois Tigeot 	/* translate destination alignment field to the source alignment encoding */
76926deccbSFrançois Tigeot 	{0, 0, 0, 0},
77926deccbSFrançois Tigeot 	{1, 2, 3, 0},
78926deccbSFrançois Tigeot 	{1, 2, 3, 0},
79926deccbSFrançois Tigeot 	{1, 2, 3, 0},
80926deccbSFrançois Tigeot 	{4, 5, 6, 7},
81926deccbSFrançois Tigeot 	{4, 5, 6, 7},
82926deccbSFrançois Tigeot 	{4, 5, 6, 7},
83926deccbSFrançois Tigeot 	{4, 5, 6, 7},
84926deccbSFrançois Tigeot };
85926deccbSFrançois Tigeot static int atom_def_dst[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
86926deccbSFrançois Tigeot 
87926deccbSFrançois Tigeot static int debug_depth = 0;
88926deccbSFrançois Tigeot #ifdef ATOM_DEBUG
debug_print_spaces(int n)89926deccbSFrançois Tigeot static void debug_print_spaces(int n)
90926deccbSFrançois Tigeot {
91926deccbSFrançois Tigeot 	while (n--)
92926deccbSFrançois Tigeot 		kprintf("   ");
93926deccbSFrançois Tigeot }
94926deccbSFrançois Tigeot 
95ee479021SImre Vadász #define ATOM_DEBUG_PRINT(...) do if (atom_debug) { kprintf(__FILE__ __VA_ARGS__); } while (0)
96ee479021SImre Vadász #define ATOM_SDEBUG_PRINT(...) do if (atom_debug) { kprintf(__FILE__); debug_print_spaces(debug_depth); kprintf(__VA_ARGS__); } while (0)
97ee479021SImre Vadász #else
98ee479021SImre Vadász #define ATOM_DEBUG_PRINT(...) do { } while (0)
99ee479021SImre Vadász #define ATOM_SDEBUG_PRINT(...) do { } while (0)
100926deccbSFrançois Tigeot #endif
101926deccbSFrançois Tigeot 
atom_iio_execute(struct atom_context * ctx,int base,uint32_t index,uint32_t data)102926deccbSFrançois Tigeot static uint32_t atom_iio_execute(struct atom_context *ctx, int base,
103926deccbSFrançois Tigeot 				 uint32_t index, uint32_t data)
104926deccbSFrançois Tigeot {
105926deccbSFrançois Tigeot 	struct radeon_device *rdev = ctx->card->dev->dev_private;
106926deccbSFrançois Tigeot 	uint32_t temp = 0xCDCDCDCD;
107926deccbSFrançois Tigeot 
108926deccbSFrançois Tigeot 	while (1)
109926deccbSFrançois Tigeot 		switch (CU8(base)) {
110926deccbSFrançois Tigeot 		case ATOM_IIO_NOP:
111926deccbSFrançois Tigeot 			base++;
112926deccbSFrançois Tigeot 			break;
113926deccbSFrançois Tigeot 		case ATOM_IIO_READ:
114926deccbSFrançois Tigeot 			temp = ctx->card->ioreg_read(ctx->card, CU16(base + 1));
115926deccbSFrançois Tigeot 			base += 3;
116926deccbSFrançois Tigeot 			break;
117926deccbSFrançois Tigeot 		case ATOM_IIO_WRITE:
118926deccbSFrançois Tigeot 			if (rdev->family == CHIP_RV515)
119926deccbSFrançois Tigeot 				(void)ctx->card->ioreg_read(ctx->card, CU16(base + 1));
120926deccbSFrançois Tigeot 			ctx->card->ioreg_write(ctx->card, CU16(base + 1), temp);
121926deccbSFrançois Tigeot 			base += 3;
122926deccbSFrançois Tigeot 			break;
123926deccbSFrançois Tigeot 		case ATOM_IIO_CLEAR:
124926deccbSFrançois Tigeot 			temp &=
125926deccbSFrançois Tigeot 			    ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
126926deccbSFrançois Tigeot 			      CU8(base + 2));
127926deccbSFrançois Tigeot 			base += 3;
128926deccbSFrançois Tigeot 			break;
129926deccbSFrançois Tigeot 		case ATOM_IIO_SET:
130926deccbSFrançois Tigeot 			temp |=
131926deccbSFrançois Tigeot 			    (0xFFFFFFFF >> (32 - CU8(base + 1))) << CU8(base +
132926deccbSFrançois Tigeot 									2);
133926deccbSFrançois Tigeot 			base += 3;
134926deccbSFrançois Tigeot 			break;
135926deccbSFrançois Tigeot 		case ATOM_IIO_MOVE_INDEX:
136926deccbSFrançois Tigeot 			temp &=
137926deccbSFrançois Tigeot 			    ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
138926deccbSFrançois Tigeot 			      CU8(base + 3));
139926deccbSFrançois Tigeot 			temp |=
140926deccbSFrançois Tigeot 			    ((index >> CU8(base + 2)) &
141926deccbSFrançois Tigeot 			     (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
142926deccbSFrançois Tigeot 									  3);
143926deccbSFrançois Tigeot 			base += 4;
144926deccbSFrançois Tigeot 			break;
145926deccbSFrançois Tigeot 		case ATOM_IIO_MOVE_DATA:
146926deccbSFrançois Tigeot 			temp &=
147926deccbSFrançois Tigeot 			    ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
148926deccbSFrançois Tigeot 			      CU8(base + 3));
149926deccbSFrançois Tigeot 			temp |=
150926deccbSFrançois Tigeot 			    ((data >> CU8(base + 2)) &
151926deccbSFrançois Tigeot 			     (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
152926deccbSFrançois Tigeot 									  3);
153926deccbSFrançois Tigeot 			base += 4;
154926deccbSFrançois Tigeot 			break;
155926deccbSFrançois Tigeot 		case ATOM_IIO_MOVE_ATTR:
156926deccbSFrançois Tigeot 			temp &=
157926deccbSFrançois Tigeot 			    ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
158926deccbSFrançois Tigeot 			      CU8(base + 3));
159926deccbSFrançois Tigeot 			temp |=
160926deccbSFrançois Tigeot 			    ((ctx->
161926deccbSFrançois Tigeot 			      io_attr >> CU8(base + 2)) & (0xFFFFFFFF >> (32 -
162926deccbSFrançois Tigeot 									  CU8
163926deccbSFrançois Tigeot 									  (base
164926deccbSFrançois Tigeot 									   +
165926deccbSFrançois Tigeot 									   1))))
166926deccbSFrançois Tigeot 			    << CU8(base + 3);
167926deccbSFrançois Tigeot 			base += 4;
168926deccbSFrançois Tigeot 			break;
169926deccbSFrançois Tigeot 		case ATOM_IIO_END:
170926deccbSFrançois Tigeot 			return temp;
171926deccbSFrançois Tigeot 		default:
172926deccbSFrançois Tigeot 			DRM_INFO("Unknown IIO opcode.\n");
173926deccbSFrançois Tigeot 			return 0;
174926deccbSFrançois Tigeot 		}
175926deccbSFrançois Tigeot }
176926deccbSFrançois Tigeot 
atom_get_src_int(atom_exec_context * ctx,uint8_t attr,int * ptr,uint32_t * saved,int print)177926deccbSFrançois Tigeot static uint32_t atom_get_src_int(atom_exec_context *ctx, uint8_t attr,
178926deccbSFrançois Tigeot 				 int *ptr, uint32_t *saved, int print)
179926deccbSFrançois Tigeot {
180926deccbSFrançois Tigeot 	uint32_t idx, val = 0xCDCDCDCD, align, arg;
181926deccbSFrançois Tigeot 	struct atom_context *gctx = ctx->ctx;
182926deccbSFrançois Tigeot 	arg = attr & 7;
183926deccbSFrançois Tigeot 	align = (attr >> 3) & 7;
18457e09377SMatthew Dillon 
18557e09377SMatthew Dillon 	if (saved)
18657e09377SMatthew Dillon 		*saved = 0;	/* avoid bogus gcc warning */
18757e09377SMatthew Dillon 
188926deccbSFrançois Tigeot 	switch (arg) {
189926deccbSFrançois Tigeot 	case ATOM_ARG_REG:
190926deccbSFrançois Tigeot 		idx = U16(*ptr);
191926deccbSFrançois Tigeot 		(*ptr) += 2;
192926deccbSFrançois Tigeot 		if (print)
193ee479021SImre Vadász 			ATOM_DEBUG_PRINT("REG[0x%04X]", idx);
194926deccbSFrançois Tigeot 		idx += gctx->reg_block;
195926deccbSFrançois Tigeot 		switch (gctx->io_mode) {
196926deccbSFrançois Tigeot 		case ATOM_IO_MM:
197926deccbSFrançois Tigeot 			val = gctx->card->reg_read(gctx->card, idx);
198926deccbSFrançois Tigeot 			break;
199926deccbSFrançois Tigeot 		case ATOM_IO_PCI:
200926deccbSFrançois Tigeot 			DRM_INFO(
201926deccbSFrançois Tigeot 			       "PCI registers are not implemented.\n");
202926deccbSFrançois Tigeot 			return 0;
203926deccbSFrançois Tigeot 		case ATOM_IO_SYSIO:
204926deccbSFrançois Tigeot 			DRM_INFO(
205926deccbSFrançois Tigeot 			       "SYSIO registers are not implemented.\n");
206926deccbSFrançois Tigeot 			return 0;
207926deccbSFrançois Tigeot 		default:
208926deccbSFrançois Tigeot 			if (!(gctx->io_mode & 0x80)) {
209926deccbSFrançois Tigeot 				DRM_INFO("Bad IO mode.\n");
210926deccbSFrançois Tigeot 				return 0;
211926deccbSFrançois Tigeot 			}
212926deccbSFrançois Tigeot 			if (!gctx->iio[gctx->io_mode & 0x7F]) {
213926deccbSFrançois Tigeot 				DRM_INFO(
214926deccbSFrançois Tigeot 				       "Undefined indirect IO read method %d.\n",
215926deccbSFrançois Tigeot 				       gctx->io_mode & 0x7F);
216926deccbSFrançois Tigeot 				return 0;
217926deccbSFrançois Tigeot 			}
218926deccbSFrançois Tigeot 			val =
219926deccbSFrançois Tigeot 			    atom_iio_execute(gctx,
220926deccbSFrançois Tigeot 					     gctx->iio[gctx->io_mode & 0x7F],
221926deccbSFrançois Tigeot 					     idx, 0);
222926deccbSFrançois Tigeot 		}
223926deccbSFrançois Tigeot 		break;
224926deccbSFrançois Tigeot 	case ATOM_ARG_PS:
225926deccbSFrançois Tigeot 		idx = U8(*ptr);
226926deccbSFrançois Tigeot 		(*ptr)++;
227926deccbSFrançois Tigeot 		/* get_unaligned_le32 avoids unaligned accesses from atombios
228926deccbSFrançois Tigeot 		 * tables, noticed on a DEC Alpha. */
229926deccbSFrançois Tigeot 		val = get_unaligned_le32((u32 *)&ctx->ps[idx]);
230926deccbSFrançois Tigeot 		if (print)
231ee479021SImre Vadász 			ATOM_DEBUG_PRINT("PS[0x%02X,0x%04X]", idx, val);
232926deccbSFrançois Tigeot 		break;
233926deccbSFrançois Tigeot 	case ATOM_ARG_WS:
234926deccbSFrançois Tigeot 		idx = U8(*ptr);
235926deccbSFrançois Tigeot 		(*ptr)++;
236926deccbSFrançois Tigeot 		if (print)
237ee479021SImre Vadász 			ATOM_DEBUG_PRINT("WS[0x%02X]", idx);
238926deccbSFrançois Tigeot 		switch (idx) {
239926deccbSFrançois Tigeot 		case ATOM_WS_QUOTIENT:
240926deccbSFrançois Tigeot 			val = gctx->divmul[0];
241926deccbSFrançois Tigeot 			break;
242926deccbSFrançois Tigeot 		case ATOM_WS_REMAINDER:
243926deccbSFrançois Tigeot 			val = gctx->divmul[1];
244926deccbSFrançois Tigeot 			break;
245926deccbSFrançois Tigeot 		case ATOM_WS_DATAPTR:
246926deccbSFrançois Tigeot 			val = gctx->data_block;
247926deccbSFrançois Tigeot 			break;
248926deccbSFrançois Tigeot 		case ATOM_WS_SHIFT:
249926deccbSFrançois Tigeot 			val = gctx->shift;
250926deccbSFrançois Tigeot 			break;
251926deccbSFrançois Tigeot 		case ATOM_WS_OR_MASK:
252926deccbSFrançois Tigeot 			val = 1 << gctx->shift;
253926deccbSFrançois Tigeot 			break;
254926deccbSFrançois Tigeot 		case ATOM_WS_AND_MASK:
255926deccbSFrançois Tigeot 			val = ~(1 << gctx->shift);
256926deccbSFrançois Tigeot 			break;
257926deccbSFrançois Tigeot 		case ATOM_WS_FB_WINDOW:
258926deccbSFrançois Tigeot 			val = gctx->fb_base;
259926deccbSFrançois Tigeot 			break;
260926deccbSFrançois Tigeot 		case ATOM_WS_ATTRIBUTES:
261926deccbSFrançois Tigeot 			val = gctx->io_attr;
262926deccbSFrançois Tigeot 			break;
263926deccbSFrançois Tigeot 		case ATOM_WS_REGPTR:
264926deccbSFrançois Tigeot 			val = gctx->reg_block;
265926deccbSFrançois Tigeot 			break;
266926deccbSFrançois Tigeot 		default:
267926deccbSFrançois Tigeot 			val = ctx->ws[idx];
268926deccbSFrançois Tigeot 		}
269926deccbSFrançois Tigeot 		break;
270926deccbSFrançois Tigeot 	case ATOM_ARG_ID:
271926deccbSFrançois Tigeot 		idx = U16(*ptr);
272926deccbSFrançois Tigeot 		(*ptr) += 2;
273926deccbSFrançois Tigeot 		if (print) {
274926deccbSFrançois Tigeot 			if (gctx->data_block)
275ee479021SImre Vadász 				ATOM_DEBUG_PRINT("ID[0x%04X+%04X]", idx, gctx->data_block);
276926deccbSFrançois Tigeot 			else
277ee479021SImre Vadász 				ATOM_DEBUG_PRINT("ID[0x%04X]", idx);
278926deccbSFrançois Tigeot 		}
279926deccbSFrançois Tigeot 		val = U32(idx + gctx->data_block);
280926deccbSFrançois Tigeot 		break;
281926deccbSFrançois Tigeot 	case ATOM_ARG_FB:
282926deccbSFrançois Tigeot 		idx = U8(*ptr);
283926deccbSFrançois Tigeot 		(*ptr)++;
284926deccbSFrançois Tigeot 		if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
285926deccbSFrançois Tigeot 			DRM_ERROR("ATOM: fb read beyond scratch region: %d vs. %d\n",
286926deccbSFrançois Tigeot 				  gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
287926deccbSFrançois Tigeot 			val = 0;
288926deccbSFrançois Tigeot 		} else
289926deccbSFrançois Tigeot 			val = gctx->scratch[(gctx->fb_base / 4) + idx];
290926deccbSFrançois Tigeot 		if (print)
291ee479021SImre Vadász 			ATOM_DEBUG_PRINT("FB[0x%02X]", idx);
292926deccbSFrançois Tigeot 		break;
293926deccbSFrançois Tigeot 	case ATOM_ARG_IMM:
294926deccbSFrançois Tigeot 		switch (align) {
295926deccbSFrançois Tigeot 		case ATOM_SRC_DWORD:
296926deccbSFrançois Tigeot 			val = U32(*ptr);
297926deccbSFrançois Tigeot 			(*ptr) += 4;
298926deccbSFrançois Tigeot 			if (print)
299ee479021SImre Vadász 				ATOM_DEBUG_PRINT("IMM 0x%08X\n", val);
300926deccbSFrançois Tigeot 			return val;
301926deccbSFrançois Tigeot 		case ATOM_SRC_WORD0:
302926deccbSFrançois Tigeot 		case ATOM_SRC_WORD8:
303926deccbSFrançois Tigeot 		case ATOM_SRC_WORD16:
304926deccbSFrançois Tigeot 			val = U16(*ptr);
305926deccbSFrançois Tigeot 			(*ptr) += 2;
306926deccbSFrançois Tigeot 			if (print)
307ee479021SImre Vadász 				ATOM_DEBUG_PRINT("IMM 0x%04X\n", val);
308926deccbSFrançois Tigeot 			return val;
309926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE0:
310926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE8:
311926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE16:
312926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE24:
313926deccbSFrançois Tigeot 			val = U8(*ptr);
314926deccbSFrançois Tigeot 			(*ptr)++;
315926deccbSFrançois Tigeot 			if (print)
316ee479021SImre Vadász 				ATOM_DEBUG_PRINT("IMM 0x%02X\n", val);
317926deccbSFrançois Tigeot 			return val;
318926deccbSFrançois Tigeot 		}
319926deccbSFrançois Tigeot 		return 0;
320926deccbSFrançois Tigeot 	case ATOM_ARG_PLL:
321926deccbSFrançois Tigeot 		idx = U8(*ptr);
322926deccbSFrançois Tigeot 		(*ptr)++;
323926deccbSFrançois Tigeot 		if (print)
324ee479021SImre Vadász 			ATOM_DEBUG_PRINT("PLL[0x%02X]", idx);
325926deccbSFrançois Tigeot 		val = gctx->card->pll_read(gctx->card, idx);
326926deccbSFrançois Tigeot 		break;
327926deccbSFrançois Tigeot 	case ATOM_ARG_MC:
328926deccbSFrançois Tigeot 		idx = U8(*ptr);
329926deccbSFrançois Tigeot 		(*ptr)++;
330926deccbSFrançois Tigeot 		if (print)
331ee479021SImre Vadász 			ATOM_DEBUG_PRINT("MC[0x%02X]", idx);
332926deccbSFrançois Tigeot 		val = gctx->card->mc_read(gctx->card, idx);
333926deccbSFrançois Tigeot 		break;
334926deccbSFrançois Tigeot 	}
335926deccbSFrançois Tigeot 	if (saved)
336926deccbSFrançois Tigeot 		*saved = val;
337926deccbSFrançois Tigeot 	val &= atom_arg_mask[align];
338926deccbSFrançois Tigeot 	val >>= atom_arg_shift[align];
339926deccbSFrançois Tigeot 	if (print)
340926deccbSFrançois Tigeot 		switch (align) {
341926deccbSFrançois Tigeot 		case ATOM_SRC_DWORD:
342ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[31:0] -> 0x%08X\n", val);
343926deccbSFrançois Tigeot 			break;
344926deccbSFrançois Tigeot 		case ATOM_SRC_WORD0:
345ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[15:0] -> 0x%04X\n", val);
346926deccbSFrançois Tigeot 			break;
347926deccbSFrançois Tigeot 		case ATOM_SRC_WORD8:
348ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[23:8] -> 0x%04X\n", val);
349926deccbSFrançois Tigeot 			break;
350926deccbSFrançois Tigeot 		case ATOM_SRC_WORD16:
351ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[31:16] -> 0x%04X\n", val);
352926deccbSFrançois Tigeot 			break;
353926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE0:
354ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[7:0] -> 0x%02X\n", val);
355926deccbSFrançois Tigeot 			break;
356926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE8:
357ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[15:8] -> 0x%02X\n", val);
358926deccbSFrançois Tigeot 			break;
359926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE16:
360ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[23:16] -> 0x%02X\n", val);
361926deccbSFrançois Tigeot 			break;
362926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE24:
363ee479021SImre Vadász 			ATOM_DEBUG_PRINT(".[31:24] -> 0x%02X\n", val);
364926deccbSFrançois Tigeot 			break;
365926deccbSFrançois Tigeot 		}
366926deccbSFrançois Tigeot 	return val;
367926deccbSFrançois Tigeot }
368926deccbSFrançois Tigeot 
atom_skip_src_int(atom_exec_context * ctx,uint8_t attr,int * ptr)369926deccbSFrançois Tigeot static void atom_skip_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr)
370926deccbSFrançois Tigeot {
371926deccbSFrançois Tigeot 	uint32_t align = (attr >> 3) & 7, arg = attr & 7;
372926deccbSFrançois Tigeot 	switch (arg) {
373926deccbSFrançois Tigeot 	case ATOM_ARG_REG:
374926deccbSFrançois Tigeot 	case ATOM_ARG_ID:
375926deccbSFrançois Tigeot 		(*ptr) += 2;
376926deccbSFrançois Tigeot 		break;
377926deccbSFrançois Tigeot 	case ATOM_ARG_PLL:
378926deccbSFrançois Tigeot 	case ATOM_ARG_MC:
379926deccbSFrançois Tigeot 	case ATOM_ARG_PS:
380926deccbSFrançois Tigeot 	case ATOM_ARG_WS:
381926deccbSFrançois Tigeot 	case ATOM_ARG_FB:
382926deccbSFrançois Tigeot 		(*ptr)++;
383926deccbSFrançois Tigeot 		break;
384926deccbSFrançois Tigeot 	case ATOM_ARG_IMM:
385926deccbSFrançois Tigeot 		switch (align) {
386926deccbSFrançois Tigeot 		case ATOM_SRC_DWORD:
387926deccbSFrançois Tigeot 			(*ptr) += 4;
388926deccbSFrançois Tigeot 			return;
389926deccbSFrançois Tigeot 		case ATOM_SRC_WORD0:
390926deccbSFrançois Tigeot 		case ATOM_SRC_WORD8:
391926deccbSFrançois Tigeot 		case ATOM_SRC_WORD16:
392926deccbSFrançois Tigeot 			(*ptr) += 2;
393926deccbSFrançois Tigeot 			return;
394926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE0:
395926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE8:
396926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE16:
397926deccbSFrançois Tigeot 		case ATOM_SRC_BYTE24:
398926deccbSFrançois Tigeot 			(*ptr)++;
399926deccbSFrançois Tigeot 			return;
400926deccbSFrançois Tigeot 		}
401926deccbSFrançois Tigeot 		return;
402926deccbSFrançois Tigeot 	}
403926deccbSFrançois Tigeot }
404926deccbSFrançois Tigeot 
atom_get_src(atom_exec_context * ctx,uint8_t attr,int * ptr)405926deccbSFrançois Tigeot static uint32_t atom_get_src(atom_exec_context *ctx, uint8_t attr, int *ptr)
406926deccbSFrançois Tigeot {
407926deccbSFrançois Tigeot 	return atom_get_src_int(ctx, attr, ptr, NULL, 1);
408926deccbSFrançois Tigeot }
409926deccbSFrançois Tigeot 
atom_get_src_direct(atom_exec_context * ctx,uint8_t align,int * ptr)410926deccbSFrançois Tigeot static uint32_t atom_get_src_direct(atom_exec_context *ctx, uint8_t align, int *ptr)
411926deccbSFrançois Tigeot {
412926deccbSFrançois Tigeot 	uint32_t val = 0xCDCDCDCD;
413926deccbSFrançois Tigeot 
414926deccbSFrançois Tigeot 	switch (align) {
415926deccbSFrançois Tigeot 	case ATOM_SRC_DWORD:
416926deccbSFrançois Tigeot 		val = U32(*ptr);
417926deccbSFrançois Tigeot 		(*ptr) += 4;
418926deccbSFrançois Tigeot 		break;
419926deccbSFrançois Tigeot 	case ATOM_SRC_WORD0:
420926deccbSFrançois Tigeot 	case ATOM_SRC_WORD8:
421926deccbSFrançois Tigeot 	case ATOM_SRC_WORD16:
422926deccbSFrançois Tigeot 		val = U16(*ptr);
423926deccbSFrançois Tigeot 		(*ptr) += 2;
424926deccbSFrançois Tigeot 		break;
425926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE0:
426926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE8:
427926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE16:
428926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE24:
429926deccbSFrançois Tigeot 		val = U8(*ptr);
430926deccbSFrançois Tigeot 		(*ptr)++;
431926deccbSFrançois Tigeot 		break;
432926deccbSFrançois Tigeot 	}
433926deccbSFrançois Tigeot 	return val;
434926deccbSFrançois Tigeot }
435926deccbSFrançois Tigeot 
atom_get_dst(atom_exec_context * ctx,int arg,uint8_t attr,int * ptr,uint32_t * saved,int print)436926deccbSFrançois Tigeot static uint32_t atom_get_dst(atom_exec_context *ctx, int arg, uint8_t attr,
437926deccbSFrançois Tigeot 			     int *ptr, uint32_t *saved, int print)
438926deccbSFrançois Tigeot {
439926deccbSFrançois Tigeot 	return atom_get_src_int(ctx,
440926deccbSFrançois Tigeot 				arg | atom_dst_to_src[(attr >> 3) &
441926deccbSFrançois Tigeot 						      7][(attr >> 6) & 3] << 3,
442926deccbSFrançois Tigeot 				ptr, saved, print);
443926deccbSFrançois Tigeot }
444926deccbSFrançois Tigeot 
atom_skip_dst(atom_exec_context * ctx,int arg,uint8_t attr,int * ptr)445926deccbSFrançois Tigeot static void atom_skip_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr)
446926deccbSFrançois Tigeot {
447926deccbSFrançois Tigeot 	atom_skip_src_int(ctx,
448926deccbSFrançois Tigeot 			  arg | atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) &
449926deccbSFrançois Tigeot 								 3] << 3, ptr);
450926deccbSFrançois Tigeot }
451926deccbSFrançois Tigeot 
atom_put_dst(atom_exec_context * ctx,int arg,uint8_t attr,int * ptr,uint32_t val,uint32_t saved)452926deccbSFrançois Tigeot static void atom_put_dst(atom_exec_context *ctx, int arg, uint8_t attr,
453926deccbSFrançois Tigeot 			 int *ptr, uint32_t val, uint32_t saved)
454926deccbSFrançois Tigeot {
455926deccbSFrançois Tigeot 	uint32_t align =
456926deccbSFrançois Tigeot 	    atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3], old_val =
457926deccbSFrançois Tigeot 	    val, idx;
458926deccbSFrançois Tigeot 	struct atom_context *gctx = ctx->ctx;
459926deccbSFrançois Tigeot 	old_val &= atom_arg_mask[align] >> atom_arg_shift[align];
460926deccbSFrançois Tigeot 	val <<= atom_arg_shift[align];
461926deccbSFrançois Tigeot 	val &= atom_arg_mask[align];
462926deccbSFrançois Tigeot 	saved &= ~atom_arg_mask[align];
463926deccbSFrançois Tigeot 	val |= saved;
464926deccbSFrançois Tigeot 	switch (arg) {
465926deccbSFrançois Tigeot 	case ATOM_ARG_REG:
466926deccbSFrançois Tigeot 		idx = U16(*ptr);
467926deccbSFrançois Tigeot 		(*ptr) += 2;
468ee479021SImre Vadász 		ATOM_DEBUG_PRINT("REG[0x%04X]", idx);
469926deccbSFrançois Tigeot 		idx += gctx->reg_block;
470926deccbSFrançois Tigeot 		switch (gctx->io_mode) {
471926deccbSFrançois Tigeot 		case ATOM_IO_MM:
472926deccbSFrançois Tigeot 			if (idx == 0)
473926deccbSFrançois Tigeot 				gctx->card->reg_write(gctx->card, idx,
474926deccbSFrançois Tigeot 						      val << 2);
475926deccbSFrançois Tigeot 			else
476926deccbSFrançois Tigeot 				gctx->card->reg_write(gctx->card, idx, val);
477926deccbSFrançois Tigeot 			break;
478926deccbSFrançois Tigeot 		case ATOM_IO_PCI:
479926deccbSFrançois Tigeot 			DRM_INFO(
480926deccbSFrançois Tigeot 			       "PCI registers are not implemented.\n");
481926deccbSFrançois Tigeot 			return;
482926deccbSFrançois Tigeot 		case ATOM_IO_SYSIO:
483926deccbSFrançois Tigeot 			DRM_INFO(
484926deccbSFrançois Tigeot 			       "SYSIO registers are not implemented.\n");
485926deccbSFrançois Tigeot 			return;
486926deccbSFrançois Tigeot 		default:
487926deccbSFrançois Tigeot 			if (!(gctx->io_mode & 0x80)) {
488926deccbSFrançois Tigeot 				DRM_INFO("Bad IO mode.\n");
489926deccbSFrançois Tigeot 				return;
490926deccbSFrançois Tigeot 			}
491926deccbSFrançois Tigeot 			if (!gctx->iio[gctx->io_mode & 0xFF]) {
492926deccbSFrançois Tigeot 				DRM_INFO(
493926deccbSFrançois Tigeot 				       "Undefined indirect IO write method %d.\n",
494926deccbSFrançois Tigeot 				       gctx->io_mode & 0x7F);
495926deccbSFrançois Tigeot 				return;
496926deccbSFrançois Tigeot 			}
497926deccbSFrançois Tigeot 			atom_iio_execute(gctx, gctx->iio[gctx->io_mode & 0xFF],
498926deccbSFrançois Tigeot 					 idx, val);
499926deccbSFrançois Tigeot 		}
500926deccbSFrançois Tigeot 		break;
501926deccbSFrançois Tigeot 	case ATOM_ARG_PS:
502926deccbSFrançois Tigeot 		idx = U8(*ptr);
503926deccbSFrançois Tigeot 		(*ptr)++;
504ee479021SImre Vadász 		ATOM_DEBUG_PRINT("PS[0x%02X]", idx);
505926deccbSFrançois Tigeot 		ctx->ps[idx] = cpu_to_le32(val);
506926deccbSFrançois Tigeot 		break;
507926deccbSFrançois Tigeot 	case ATOM_ARG_WS:
508926deccbSFrançois Tigeot 		idx = U8(*ptr);
509926deccbSFrançois Tigeot 		(*ptr)++;
510ee479021SImre Vadász 		ATOM_DEBUG_PRINT("WS[0x%02X]", idx);
511926deccbSFrançois Tigeot 		switch (idx) {
512926deccbSFrançois Tigeot 		case ATOM_WS_QUOTIENT:
513926deccbSFrançois Tigeot 			gctx->divmul[0] = val;
514926deccbSFrançois Tigeot 			break;
515926deccbSFrançois Tigeot 		case ATOM_WS_REMAINDER:
516926deccbSFrançois Tigeot 			gctx->divmul[1] = val;
517926deccbSFrançois Tigeot 			break;
518926deccbSFrançois Tigeot 		case ATOM_WS_DATAPTR:
519926deccbSFrançois Tigeot 			gctx->data_block = val;
520926deccbSFrançois Tigeot 			break;
521926deccbSFrançois Tigeot 		case ATOM_WS_SHIFT:
522926deccbSFrançois Tigeot 			gctx->shift = val;
523926deccbSFrançois Tigeot 			break;
524926deccbSFrançois Tigeot 		case ATOM_WS_OR_MASK:
525926deccbSFrançois Tigeot 		case ATOM_WS_AND_MASK:
526926deccbSFrançois Tigeot 			break;
527926deccbSFrançois Tigeot 		case ATOM_WS_FB_WINDOW:
528926deccbSFrançois Tigeot 			gctx->fb_base = val;
529926deccbSFrançois Tigeot 			break;
530926deccbSFrançois Tigeot 		case ATOM_WS_ATTRIBUTES:
531926deccbSFrançois Tigeot 			gctx->io_attr = val;
532926deccbSFrançois Tigeot 			break;
533926deccbSFrançois Tigeot 		case ATOM_WS_REGPTR:
534926deccbSFrançois Tigeot 			gctx->reg_block = val;
535926deccbSFrançois Tigeot 			break;
536926deccbSFrançois Tigeot 		default:
537926deccbSFrançois Tigeot 			ctx->ws[idx] = val;
538926deccbSFrançois Tigeot 		}
539926deccbSFrançois Tigeot 		break;
540926deccbSFrançois Tigeot 	case ATOM_ARG_FB:
541926deccbSFrançois Tigeot 		idx = U8(*ptr);
542926deccbSFrançois Tigeot 		(*ptr)++;
543926deccbSFrançois Tigeot 		if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
544926deccbSFrançois Tigeot 			DRM_ERROR("ATOM: fb write beyond scratch region: %d vs. %d\n",
545926deccbSFrançois Tigeot 				  gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
546926deccbSFrançois Tigeot 		} else
547926deccbSFrançois Tigeot 			gctx->scratch[(gctx->fb_base / 4) + idx] = val;
548ee479021SImre Vadász 		ATOM_DEBUG_PRINT("FB[0x%02X]", idx);
549926deccbSFrançois Tigeot 		break;
550926deccbSFrançois Tigeot 	case ATOM_ARG_PLL:
551926deccbSFrançois Tigeot 		idx = U8(*ptr);
552926deccbSFrançois Tigeot 		(*ptr)++;
553ee479021SImre Vadász 		ATOM_DEBUG_PRINT("PLL[0x%02X]", idx);
554926deccbSFrançois Tigeot 		gctx->card->pll_write(gctx->card, idx, val);
555926deccbSFrançois Tigeot 		break;
556926deccbSFrançois Tigeot 	case ATOM_ARG_MC:
557926deccbSFrançois Tigeot 		idx = U8(*ptr);
558926deccbSFrançois Tigeot 		(*ptr)++;
559ee479021SImre Vadász 		ATOM_DEBUG_PRINT("MC[0x%02X]", idx);
560926deccbSFrançois Tigeot 		gctx->card->mc_write(gctx->card, idx, val);
561926deccbSFrançois Tigeot 		return;
562926deccbSFrançois Tigeot 	}
563926deccbSFrançois Tigeot 	switch (align) {
564926deccbSFrançois Tigeot 	case ATOM_SRC_DWORD:
565ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[31:0] <- 0x%08X\n", old_val);
566926deccbSFrançois Tigeot 		break;
567926deccbSFrançois Tigeot 	case ATOM_SRC_WORD0:
568ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[15:0] <- 0x%04X\n", old_val);
569926deccbSFrançois Tigeot 		break;
570926deccbSFrançois Tigeot 	case ATOM_SRC_WORD8:
571ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[23:8] <- 0x%04X\n", old_val);
572926deccbSFrançois Tigeot 		break;
573926deccbSFrançois Tigeot 	case ATOM_SRC_WORD16:
574ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[31:16] <- 0x%04X\n", old_val);
575926deccbSFrançois Tigeot 		break;
576926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE0:
577ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[7:0] <- 0x%02X\n", old_val);
578926deccbSFrançois Tigeot 		break;
579926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE8:
580ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[15:8] <- 0x%02X\n", old_val);
581926deccbSFrançois Tigeot 		break;
582926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE16:
583ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[23:16] <- 0x%02X\n", old_val);
584926deccbSFrançois Tigeot 		break;
585926deccbSFrançois Tigeot 	case ATOM_SRC_BYTE24:
586ee479021SImre Vadász 		ATOM_DEBUG_PRINT(".[31:24] <- 0x%02X\n", old_val);
587926deccbSFrançois Tigeot 		break;
588926deccbSFrançois Tigeot 	}
589926deccbSFrançois Tigeot }
590926deccbSFrançois Tigeot 
atom_op_add(atom_exec_context * ctx,int * ptr,int arg)591926deccbSFrançois Tigeot static void atom_op_add(atom_exec_context *ctx, int *ptr, int arg)
592926deccbSFrançois Tigeot {
593926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
594926deccbSFrançois Tigeot 	uint32_t dst, src, saved;
595926deccbSFrançois Tigeot 	int dptr = *ptr;
596ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
597926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
598ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src: ");
599926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
600926deccbSFrançois Tigeot 	dst += src;
601ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
602926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
603926deccbSFrançois Tigeot }
604926deccbSFrançois Tigeot 
atom_op_and(atom_exec_context * ctx,int * ptr,int arg)605926deccbSFrançois Tigeot static void atom_op_and(atom_exec_context *ctx, int *ptr, int arg)
606926deccbSFrançois Tigeot {
607926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
608926deccbSFrançois Tigeot 	uint32_t dst, src, saved;
609926deccbSFrançois Tigeot 	int dptr = *ptr;
610ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
611926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
612ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src: ");
613926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
614926deccbSFrançois Tigeot 	dst &= src;
615ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
616926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
617926deccbSFrançois Tigeot }
618926deccbSFrançois Tigeot 
atom_op_beep(atom_exec_context * ctx,int * ptr,int arg)619926deccbSFrançois Tigeot static void atom_op_beep(atom_exec_context *ctx, int *ptr, int arg)
620926deccbSFrançois Tigeot {
621926deccbSFrançois Tigeot 	DRM_INFO("ATOM BIOS beeped!\n");
622926deccbSFrançois Tigeot }
623926deccbSFrançois Tigeot 
atom_op_calltable(atom_exec_context * ctx,int * ptr,int arg)624926deccbSFrançois Tigeot static void atom_op_calltable(atom_exec_context *ctx, int *ptr, int arg)
625926deccbSFrançois Tigeot {
626926deccbSFrançois Tigeot 	int idx = U8((*ptr)++);
627926deccbSFrançois Tigeot 	int r = 0;
628926deccbSFrançois Tigeot 
629926deccbSFrançois Tigeot 	if (idx < ATOM_TABLE_NAMES_CNT)
630ee479021SImre Vadász 		ATOM_SDEBUG_PRINT("   table: %d (%s)\n", idx, atom_table_names[idx]);
631926deccbSFrançois Tigeot 	else
632ee479021SImre Vadász 		ATOM_SDEBUG_PRINT("   table: %d\n", idx);
633926deccbSFrançois Tigeot 	if (U16(ctx->ctx->cmd_table + 4 + 2 * idx))
634926deccbSFrançois Tigeot 		r = atom_execute_table_locked(ctx->ctx, idx, ctx->ps + ctx->ps_shift);
635926deccbSFrançois Tigeot 	if (r) {
636926deccbSFrançois Tigeot 		ctx->abort = true;
637926deccbSFrançois Tigeot 	}
638926deccbSFrançois Tigeot }
639926deccbSFrançois Tigeot 
atom_op_clear(atom_exec_context * ctx,int * ptr,int arg)640926deccbSFrançois Tigeot static void atom_op_clear(atom_exec_context *ctx, int *ptr, int arg)
641926deccbSFrançois Tigeot {
642926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
643926deccbSFrançois Tigeot 	uint32_t saved;
644926deccbSFrançois Tigeot 	int dptr = *ptr;
645926deccbSFrançois Tigeot 	attr &= 0x38;
646926deccbSFrançois Tigeot 	attr |= atom_def_dst[attr >> 3] << 6;
647926deccbSFrançois Tigeot 	atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
648ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
649926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, 0, saved);
650926deccbSFrançois Tigeot }
651926deccbSFrançois Tigeot 
atom_op_compare(atom_exec_context * ctx,int * ptr,int arg)652926deccbSFrançois Tigeot static void atom_op_compare(atom_exec_context *ctx, int *ptr, int arg)
653926deccbSFrançois Tigeot {
654926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
655926deccbSFrançois Tigeot 	uint32_t dst, src;
656ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src1: ");
657926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
658ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src2: ");
659926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
660926deccbSFrançois Tigeot 	ctx->ctx->cs_equal = (dst == src);
661926deccbSFrançois Tigeot 	ctx->ctx->cs_above = (dst > src);
662ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   result: %s %s\n", ctx->ctx->cs_equal ? "EQ" : "NE",
663926deccbSFrançois Tigeot 	       ctx->ctx->cs_above ? "GT" : "LE");
664926deccbSFrançois Tigeot }
665926deccbSFrançois Tigeot 
atom_op_delay(atom_exec_context * ctx,int * ptr,int arg)666926deccbSFrançois Tigeot static void atom_op_delay(atom_exec_context *ctx, int *ptr, int arg)
667926deccbSFrançois Tigeot {
668926deccbSFrançois Tigeot 	unsigned count = U8((*ptr)++);
669ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   count: %d\n", count);
670926deccbSFrançois Tigeot 	if (arg == ATOM_UNIT_MICROSEC)
671c4ef309bSzrj 		udelay(count);
672926deccbSFrançois Tigeot 	else if (!drm_can_sleep())
673c4ef309bSzrj 		mdelay(count);
674926deccbSFrançois Tigeot 	else
6753ff03fddSFrançois Tigeot 		msleep(count);
676926deccbSFrançois Tigeot }
677926deccbSFrançois Tigeot 
atom_op_div(atom_exec_context * ctx,int * ptr,int arg)678926deccbSFrançois Tigeot static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
679926deccbSFrançois Tigeot {
680926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
681926deccbSFrançois Tigeot 	uint32_t dst, src;
682ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src1: ");
683926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
684ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src2: ");
685926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
686926deccbSFrançois Tigeot 	if (src != 0) {
687926deccbSFrançois Tigeot 		ctx->ctx->divmul[0] = dst / src;
688926deccbSFrançois Tigeot 		ctx->ctx->divmul[1] = dst % src;
689926deccbSFrançois Tigeot 	} else {
690926deccbSFrançois Tigeot 		ctx->ctx->divmul[0] = 0;
691926deccbSFrançois Tigeot 		ctx->ctx->divmul[1] = 0;
692926deccbSFrançois Tigeot 	}
693926deccbSFrançois Tigeot }
694926deccbSFrançois Tigeot 
atom_op_eot(atom_exec_context * ctx,int * ptr,int arg)695926deccbSFrançois Tigeot static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
696926deccbSFrançois Tigeot {
697926deccbSFrançois Tigeot 	/* functionally, a nop */
698926deccbSFrançois Tigeot }
699926deccbSFrançois Tigeot 
atom_op_jump(atom_exec_context * ctx,int * ptr,int arg)700926deccbSFrançois Tigeot static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
701926deccbSFrançois Tigeot {
702926deccbSFrançois Tigeot 	int execute = 0, target = U16(*ptr);
703926deccbSFrançois Tigeot 	unsigned long cjiffies;
704926deccbSFrançois Tigeot 
705926deccbSFrançois Tigeot 	(*ptr) += 2;
706926deccbSFrançois Tigeot 	switch (arg) {
707926deccbSFrançois Tigeot 	case ATOM_COND_ABOVE:
708926deccbSFrançois Tigeot 		execute = ctx->ctx->cs_above;
709926deccbSFrançois Tigeot 		break;
710926deccbSFrançois Tigeot 	case ATOM_COND_ABOVEOREQUAL:
711926deccbSFrançois Tigeot 		execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
712926deccbSFrançois Tigeot 		break;
713926deccbSFrançois Tigeot 	case ATOM_COND_ALWAYS:
714926deccbSFrançois Tigeot 		execute = 1;
715926deccbSFrançois Tigeot 		break;
716926deccbSFrançois Tigeot 	case ATOM_COND_BELOW:
717926deccbSFrançois Tigeot 		execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
718926deccbSFrançois Tigeot 		break;
719926deccbSFrançois Tigeot 	case ATOM_COND_BELOWOREQUAL:
720926deccbSFrançois Tigeot 		execute = !ctx->ctx->cs_above;
721926deccbSFrançois Tigeot 		break;
722926deccbSFrançois Tigeot 	case ATOM_COND_EQUAL:
723926deccbSFrançois Tigeot 		execute = ctx->ctx->cs_equal;
724926deccbSFrançois Tigeot 		break;
725926deccbSFrançois Tigeot 	case ATOM_COND_NOTEQUAL:
726926deccbSFrançois Tigeot 		execute = !ctx->ctx->cs_equal;
727926deccbSFrançois Tigeot 		break;
728926deccbSFrançois Tigeot 	}
729926deccbSFrançois Tigeot 	if (arg != ATOM_COND_ALWAYS)
730ee479021SImre Vadász 		ATOM_SDEBUG_PRINT("   taken: %s\n", execute ? "yes" : "no");
731ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   target: 0x%04X\n", target);
732926deccbSFrançois Tigeot 	if (execute) {
733926deccbSFrançois Tigeot 		if (ctx->last_jump == (ctx->start + target)) {
734926deccbSFrançois Tigeot 			cjiffies = jiffies;
735926deccbSFrançois Tigeot 			if (time_after(cjiffies, ctx->last_jump_jiffies)) {
736926deccbSFrançois Tigeot 				cjiffies -= ctx->last_jump_jiffies;
737926deccbSFrançois Tigeot 				if ((jiffies_to_msecs(cjiffies) > 5000)) {
738926deccbSFrançois Tigeot 					DRM_ERROR("atombios stuck in loop for more than 5secs aborting\n");
739926deccbSFrançois Tigeot 					ctx->abort = true;
740926deccbSFrançois Tigeot 				}
741926deccbSFrançois Tigeot 			} else {
742926deccbSFrançois Tigeot 				/* jiffies wrap around we will just wait a little longer */
743926deccbSFrançois Tigeot 				ctx->last_jump_jiffies = jiffies;
744926deccbSFrançois Tigeot 			}
745926deccbSFrançois Tigeot 		} else {
746926deccbSFrançois Tigeot 			ctx->last_jump = ctx->start + target;
747926deccbSFrançois Tigeot 			ctx->last_jump_jiffies = jiffies;
748926deccbSFrançois Tigeot 		}
749926deccbSFrançois Tigeot 		*ptr = ctx->start + target;
750926deccbSFrançois Tigeot 	}
751926deccbSFrançois Tigeot }
752926deccbSFrançois Tigeot 
atom_op_mask(atom_exec_context * ctx,int * ptr,int arg)753926deccbSFrançois Tigeot static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
754926deccbSFrançois Tigeot {
755926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
756926deccbSFrançois Tigeot 	uint32_t dst, mask, src, saved;
757926deccbSFrançois Tigeot 	int dptr = *ptr;
758ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
759926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
760926deccbSFrançois Tigeot 	mask = atom_get_src_direct(ctx, ((attr >> 3) & 7), ptr);
761ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   mask: 0x%08x", mask);
762ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src: ");
763926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
764926deccbSFrançois Tigeot 	dst &= mask;
765926deccbSFrançois Tigeot 	dst |= src;
766ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
767926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
768926deccbSFrançois Tigeot }
769926deccbSFrançois Tigeot 
atom_op_move(atom_exec_context * ctx,int * ptr,int arg)770926deccbSFrançois Tigeot static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
771926deccbSFrançois Tigeot {
772926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
773926deccbSFrançois Tigeot 	uint32_t src, saved;
774926deccbSFrançois Tigeot 	int dptr = *ptr;
775926deccbSFrançois Tigeot 	if (((attr >> 3) & 7) != ATOM_SRC_DWORD)
776926deccbSFrançois Tigeot 		atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
777926deccbSFrançois Tigeot 	else {
778926deccbSFrançois Tigeot 		atom_skip_dst(ctx, arg, attr, ptr);
779926deccbSFrançois Tigeot 		saved = 0xCDCDCDCD;
780926deccbSFrançois Tigeot 	}
781ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src: ");
782926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
783ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
784926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, src, saved);
785926deccbSFrançois Tigeot }
786926deccbSFrançois Tigeot 
atom_op_mul(atom_exec_context * ctx,int * ptr,int arg)787926deccbSFrançois Tigeot static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
788926deccbSFrançois Tigeot {
789926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
790926deccbSFrançois Tigeot 	uint32_t dst, src;
791ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src1: ");
792926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
793ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src2: ");
794926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
795926deccbSFrançois Tigeot 	ctx->ctx->divmul[0] = dst * src;
796926deccbSFrançois Tigeot }
797926deccbSFrançois Tigeot 
atom_op_nop(atom_exec_context * ctx,int * ptr,int arg)798926deccbSFrançois Tigeot static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
799926deccbSFrançois Tigeot {
800926deccbSFrançois Tigeot 	/* nothing */
801926deccbSFrançois Tigeot }
802926deccbSFrançois Tigeot 
atom_op_or(atom_exec_context * ctx,int * ptr,int arg)803926deccbSFrançois Tigeot static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
804926deccbSFrançois Tigeot {
805926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
806926deccbSFrançois Tigeot 	uint32_t dst, src, saved;
807926deccbSFrançois Tigeot 	int dptr = *ptr;
808ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
809926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
810ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src: ");
811926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
812926deccbSFrançois Tigeot 	dst |= src;
813ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
814926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
815926deccbSFrançois Tigeot }
816926deccbSFrançois Tigeot 
atom_op_postcard(atom_exec_context * ctx,int * ptr,int arg)817926deccbSFrançois Tigeot static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
818926deccbSFrançois Tigeot {
819926deccbSFrançois Tigeot 	uint8_t val = U8((*ptr)++);
820ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("POST card output: 0x%02X\n", val);
821926deccbSFrançois Tigeot }
822926deccbSFrançois Tigeot 
atom_op_repeat(atom_exec_context * ctx,int * ptr,int arg)823926deccbSFrançois Tigeot static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
824926deccbSFrançois Tigeot {
825926deccbSFrançois Tigeot 	DRM_INFO("unimplemented!\n");
826926deccbSFrançois Tigeot }
827926deccbSFrançois Tigeot 
atom_op_restorereg(atom_exec_context * ctx,int * ptr,int arg)828926deccbSFrançois Tigeot static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
829926deccbSFrançois Tigeot {
830926deccbSFrançois Tigeot 	DRM_INFO("unimplemented!\n");
831926deccbSFrançois Tigeot }
832926deccbSFrançois Tigeot 
atom_op_savereg(atom_exec_context * ctx,int * ptr,int arg)833926deccbSFrançois Tigeot static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
834926deccbSFrançois Tigeot {
835926deccbSFrançois Tigeot 	DRM_INFO("unimplemented!\n");
836926deccbSFrançois Tigeot }
837926deccbSFrançois Tigeot 
atom_op_setdatablock(atom_exec_context * ctx,int * ptr,int arg)838926deccbSFrançois Tigeot static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
839926deccbSFrançois Tigeot {
840926deccbSFrançois Tigeot 	int idx = U8(*ptr);
841926deccbSFrançois Tigeot 	(*ptr)++;
842ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   block: %d\n", idx);
843926deccbSFrançois Tigeot 	if (!idx)
844926deccbSFrançois Tigeot 		ctx->ctx->data_block = 0;
845926deccbSFrançois Tigeot 	else if (idx == 255)
846926deccbSFrançois Tigeot 		ctx->ctx->data_block = ctx->start;
847926deccbSFrançois Tigeot 	else
848926deccbSFrançois Tigeot 		ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
849ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   base: 0x%04X\n", ctx->ctx->data_block);
850926deccbSFrançois Tigeot }
851926deccbSFrançois Tigeot 
atom_op_setfbbase(atom_exec_context * ctx,int * ptr,int arg)852926deccbSFrançois Tigeot static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
853926deccbSFrançois Tigeot {
854926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
855ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   fb_base: ");
856926deccbSFrançois Tigeot 	ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
857926deccbSFrançois Tigeot }
858926deccbSFrançois Tigeot 
atom_op_setport(atom_exec_context * ctx,int * ptr,int arg)859926deccbSFrançois Tigeot static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
860926deccbSFrançois Tigeot {
861926deccbSFrançois Tigeot 	int port;
862926deccbSFrançois Tigeot 	switch (arg) {
863926deccbSFrançois Tigeot 	case ATOM_PORT_ATI:
864926deccbSFrançois Tigeot 		port = U16(*ptr);
865926deccbSFrançois Tigeot 		if (port < ATOM_IO_NAMES_CNT)
866ee479021SImre Vadász 			ATOM_SDEBUG_PRINT("   port: %d (%s)\n", port, atom_io_names[port]);
867926deccbSFrançois Tigeot 		else
868ee479021SImre Vadász 			ATOM_SDEBUG_PRINT("   port: %d\n", port);
869926deccbSFrançois Tigeot 		if (!port)
870926deccbSFrançois Tigeot 			ctx->ctx->io_mode = ATOM_IO_MM;
871926deccbSFrançois Tigeot 		else
872926deccbSFrançois Tigeot 			ctx->ctx->io_mode = ATOM_IO_IIO | port;
873926deccbSFrançois Tigeot 		(*ptr) += 2;
874926deccbSFrançois Tigeot 		break;
875926deccbSFrançois Tigeot 	case ATOM_PORT_PCI:
876926deccbSFrançois Tigeot 		ctx->ctx->io_mode = ATOM_IO_PCI;
877926deccbSFrançois Tigeot 		(*ptr)++;
878926deccbSFrançois Tigeot 		break;
879926deccbSFrançois Tigeot 	case ATOM_PORT_SYSIO:
880926deccbSFrançois Tigeot 		ctx->ctx->io_mode = ATOM_IO_SYSIO;
881926deccbSFrançois Tigeot 		(*ptr)++;
882926deccbSFrançois Tigeot 		break;
883926deccbSFrançois Tigeot 	}
884926deccbSFrançois Tigeot }
885926deccbSFrançois Tigeot 
atom_op_setregblock(atom_exec_context * ctx,int * ptr,int arg)886926deccbSFrançois Tigeot static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
887926deccbSFrançois Tigeot {
888926deccbSFrançois Tigeot 	ctx->ctx->reg_block = U16(*ptr);
889926deccbSFrançois Tigeot 	(*ptr) += 2;
890ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   base: 0x%04X\n", ctx->ctx->reg_block);
891926deccbSFrançois Tigeot }
892926deccbSFrançois Tigeot 
atom_op_shift_left(atom_exec_context * ctx,int * ptr,int arg)893926deccbSFrançois Tigeot static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
894926deccbSFrançois Tigeot {
895926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++), shift;
896926deccbSFrançois Tigeot 	uint32_t saved, dst;
897926deccbSFrançois Tigeot 	int dptr = *ptr;
898926deccbSFrançois Tigeot 	attr &= 0x38;
899926deccbSFrançois Tigeot 	attr |= atom_def_dst[attr >> 3] << 6;
900ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
901926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
902926deccbSFrançois Tigeot 	shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
903ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   shift: %d\n", shift);
904926deccbSFrançois Tigeot 	dst <<= shift;
905ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
906926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
907926deccbSFrançois Tigeot }
908926deccbSFrançois Tigeot 
atom_op_shift_right(atom_exec_context * ctx,int * ptr,int arg)909926deccbSFrançois Tigeot static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
910926deccbSFrançois Tigeot {
911926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++), shift;
912926deccbSFrançois Tigeot 	uint32_t saved, dst;
913926deccbSFrançois Tigeot 	int dptr = *ptr;
914926deccbSFrançois Tigeot 	attr &= 0x38;
915926deccbSFrançois Tigeot 	attr |= atom_def_dst[attr >> 3] << 6;
916ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
917926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
918926deccbSFrançois Tigeot 	shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
919ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   shift: %d\n", shift);
920926deccbSFrançois Tigeot 	dst >>= shift;
921ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
922926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
923926deccbSFrançois Tigeot }
924926deccbSFrançois Tigeot 
atom_op_shl(atom_exec_context * ctx,int * ptr,int arg)925926deccbSFrançois Tigeot static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
926926deccbSFrançois Tigeot {
927926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++), shift;
928926deccbSFrançois Tigeot 	uint32_t saved, dst;
929926deccbSFrançois Tigeot 	int dptr = *ptr;
930926deccbSFrançois Tigeot 	uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
931ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
932926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
933926deccbSFrançois Tigeot 	/* op needs to full dst value */
934926deccbSFrançois Tigeot 	dst = saved;
935926deccbSFrançois Tigeot 	shift = atom_get_src(ctx, attr, ptr);
936ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   shift: %d\n", shift);
937926deccbSFrançois Tigeot 	dst <<= shift;
938926deccbSFrançois Tigeot 	dst &= atom_arg_mask[dst_align];
939926deccbSFrançois Tigeot 	dst >>= atom_arg_shift[dst_align];
940ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
941926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
942926deccbSFrançois Tigeot }
943926deccbSFrançois Tigeot 
atom_op_shr(atom_exec_context * ctx,int * ptr,int arg)944926deccbSFrançois Tigeot static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
945926deccbSFrançois Tigeot {
946926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++), shift;
947926deccbSFrançois Tigeot 	uint32_t saved, dst;
948926deccbSFrançois Tigeot 	int dptr = *ptr;
949926deccbSFrançois Tigeot 	uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
950ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
951926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
952926deccbSFrançois Tigeot 	/* op needs to full dst value */
953926deccbSFrançois Tigeot 	dst = saved;
954926deccbSFrançois Tigeot 	shift = atom_get_src(ctx, attr, ptr);
955ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   shift: %d\n", shift);
956926deccbSFrançois Tigeot 	dst >>= shift;
957926deccbSFrançois Tigeot 	dst &= atom_arg_mask[dst_align];
958926deccbSFrançois Tigeot 	dst >>= atom_arg_shift[dst_align];
959ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
960926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
961926deccbSFrançois Tigeot }
962926deccbSFrançois Tigeot 
atom_op_sub(atom_exec_context * ctx,int * ptr,int arg)963926deccbSFrançois Tigeot static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
964926deccbSFrançois Tigeot {
965926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
966926deccbSFrançois Tigeot 	uint32_t dst, src, saved;
967926deccbSFrançois Tigeot 	int dptr = *ptr;
968ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
969926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
970ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src: ");
971926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
972926deccbSFrançois Tigeot 	dst -= src;
973ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
974926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
975926deccbSFrançois Tigeot }
976926deccbSFrançois Tigeot 
atom_op_switch(atom_exec_context * ctx,int * ptr,int arg)977926deccbSFrançois Tigeot static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
978926deccbSFrançois Tigeot {
979926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
980926deccbSFrançois Tigeot 	uint32_t src, val, target;
981ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   switch: ");
982926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
983926deccbSFrançois Tigeot 	while (U16(*ptr) != ATOM_CASE_END)
984926deccbSFrançois Tigeot 		if (U8(*ptr) == ATOM_CASE_MAGIC) {
985926deccbSFrançois Tigeot 			(*ptr)++;
986ee479021SImre Vadász 			ATOM_SDEBUG_PRINT("   case: ");
987926deccbSFrançois Tigeot 			val =
988926deccbSFrançois Tigeot 			    atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
989926deccbSFrançois Tigeot 					 ptr);
990926deccbSFrançois Tigeot 			target = U16(*ptr);
991926deccbSFrançois Tigeot 			if (val == src) {
992ee479021SImre Vadász 				ATOM_SDEBUG_PRINT("   target: %04X\n", target);
993926deccbSFrançois Tigeot 				*ptr = ctx->start + target;
994926deccbSFrançois Tigeot 				return;
995926deccbSFrançois Tigeot 			}
996926deccbSFrançois Tigeot 			(*ptr) += 2;
997926deccbSFrançois Tigeot 		} else {
998926deccbSFrançois Tigeot 			DRM_INFO("Bad case.\n");
999926deccbSFrançois Tigeot 			return;
1000926deccbSFrançois Tigeot 		}
1001926deccbSFrançois Tigeot 	(*ptr) += 2;
1002926deccbSFrançois Tigeot }
1003926deccbSFrançois Tigeot 
atom_op_test(atom_exec_context * ctx,int * ptr,int arg)1004926deccbSFrançois Tigeot static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
1005926deccbSFrançois Tigeot {
1006926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
1007926deccbSFrançois Tigeot 	uint32_t dst, src;
1008ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src1: ");
1009926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
1010ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src2: ");
1011926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
1012926deccbSFrançois Tigeot 	ctx->ctx->cs_equal = ((dst & src) == 0);
1013ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
1014926deccbSFrançois Tigeot }
1015926deccbSFrançois Tigeot 
atom_op_xor(atom_exec_context * ctx,int * ptr,int arg)1016926deccbSFrançois Tigeot static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
1017926deccbSFrançois Tigeot {
1018926deccbSFrançois Tigeot 	uint8_t attr = U8((*ptr)++);
1019926deccbSFrançois Tigeot 	uint32_t dst, src, saved;
1020926deccbSFrançois Tigeot 	int dptr = *ptr;
1021ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
1022926deccbSFrançois Tigeot 	dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
1023ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   src: ");
1024926deccbSFrançois Tigeot 	src = atom_get_src(ctx, attr, ptr);
1025926deccbSFrançois Tigeot 	dst ^= src;
1026ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("   dst: ");
1027926deccbSFrançois Tigeot 	atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
1028926deccbSFrançois Tigeot }
1029926deccbSFrançois Tigeot 
atom_op_debug(atom_exec_context * ctx,int * ptr,int arg)1030926deccbSFrançois Tigeot static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
1031926deccbSFrançois Tigeot {
1032926deccbSFrançois Tigeot 	DRM_INFO("unimplemented!\n");
1033926deccbSFrançois Tigeot }
1034926deccbSFrançois Tigeot 
1035926deccbSFrançois Tigeot static struct {
1036926deccbSFrançois Tigeot 	void (*func) (atom_exec_context *, int *, int);
1037926deccbSFrançois Tigeot 	int arg;
1038926deccbSFrançois Tigeot } opcode_table[ATOM_OP_CNT] = {
1039926deccbSFrançois Tigeot 	{
1040926deccbSFrançois Tigeot 	NULL, 0}, {
1041926deccbSFrançois Tigeot 	atom_op_move, ATOM_ARG_REG}, {
1042926deccbSFrançois Tigeot 	atom_op_move, ATOM_ARG_PS}, {
1043926deccbSFrançois Tigeot 	atom_op_move, ATOM_ARG_WS}, {
1044926deccbSFrançois Tigeot 	atom_op_move, ATOM_ARG_FB}, {
1045926deccbSFrançois Tigeot 	atom_op_move, ATOM_ARG_PLL}, {
1046926deccbSFrançois Tigeot 	atom_op_move, ATOM_ARG_MC}, {
1047926deccbSFrançois Tigeot 	atom_op_and, ATOM_ARG_REG}, {
1048926deccbSFrançois Tigeot 	atom_op_and, ATOM_ARG_PS}, {
1049926deccbSFrançois Tigeot 	atom_op_and, ATOM_ARG_WS}, {
1050926deccbSFrançois Tigeot 	atom_op_and, ATOM_ARG_FB}, {
1051926deccbSFrançois Tigeot 	atom_op_and, ATOM_ARG_PLL}, {
1052926deccbSFrançois Tigeot 	atom_op_and, ATOM_ARG_MC}, {
1053926deccbSFrançois Tigeot 	atom_op_or, ATOM_ARG_REG}, {
1054926deccbSFrançois Tigeot 	atom_op_or, ATOM_ARG_PS}, {
1055926deccbSFrançois Tigeot 	atom_op_or, ATOM_ARG_WS}, {
1056926deccbSFrançois Tigeot 	atom_op_or, ATOM_ARG_FB}, {
1057926deccbSFrançois Tigeot 	atom_op_or, ATOM_ARG_PLL}, {
1058926deccbSFrançois Tigeot 	atom_op_or, ATOM_ARG_MC}, {
1059926deccbSFrançois Tigeot 	atom_op_shift_left, ATOM_ARG_REG}, {
1060926deccbSFrançois Tigeot 	atom_op_shift_left, ATOM_ARG_PS}, {
1061926deccbSFrançois Tigeot 	atom_op_shift_left, ATOM_ARG_WS}, {
1062926deccbSFrançois Tigeot 	atom_op_shift_left, ATOM_ARG_FB}, {
1063926deccbSFrançois Tigeot 	atom_op_shift_left, ATOM_ARG_PLL}, {
1064926deccbSFrançois Tigeot 	atom_op_shift_left, ATOM_ARG_MC}, {
1065926deccbSFrançois Tigeot 	atom_op_shift_right, ATOM_ARG_REG}, {
1066926deccbSFrançois Tigeot 	atom_op_shift_right, ATOM_ARG_PS}, {
1067926deccbSFrançois Tigeot 	atom_op_shift_right, ATOM_ARG_WS}, {
1068926deccbSFrançois Tigeot 	atom_op_shift_right, ATOM_ARG_FB}, {
1069926deccbSFrançois Tigeot 	atom_op_shift_right, ATOM_ARG_PLL}, {
1070926deccbSFrançois Tigeot 	atom_op_shift_right, ATOM_ARG_MC}, {
1071926deccbSFrançois Tigeot 	atom_op_mul, ATOM_ARG_REG}, {
1072926deccbSFrançois Tigeot 	atom_op_mul, ATOM_ARG_PS}, {
1073926deccbSFrançois Tigeot 	atom_op_mul, ATOM_ARG_WS}, {
1074926deccbSFrançois Tigeot 	atom_op_mul, ATOM_ARG_FB}, {
1075926deccbSFrançois Tigeot 	atom_op_mul, ATOM_ARG_PLL}, {
1076926deccbSFrançois Tigeot 	atom_op_mul, ATOM_ARG_MC}, {
1077926deccbSFrançois Tigeot 	atom_op_div, ATOM_ARG_REG}, {
1078926deccbSFrançois Tigeot 	atom_op_div, ATOM_ARG_PS}, {
1079926deccbSFrançois Tigeot 	atom_op_div, ATOM_ARG_WS}, {
1080926deccbSFrançois Tigeot 	atom_op_div, ATOM_ARG_FB}, {
1081926deccbSFrançois Tigeot 	atom_op_div, ATOM_ARG_PLL}, {
1082926deccbSFrançois Tigeot 	atom_op_div, ATOM_ARG_MC}, {
1083926deccbSFrançois Tigeot 	atom_op_add, ATOM_ARG_REG}, {
1084926deccbSFrançois Tigeot 	atom_op_add, ATOM_ARG_PS}, {
1085926deccbSFrançois Tigeot 	atom_op_add, ATOM_ARG_WS}, {
1086926deccbSFrançois Tigeot 	atom_op_add, ATOM_ARG_FB}, {
1087926deccbSFrançois Tigeot 	atom_op_add, ATOM_ARG_PLL}, {
1088926deccbSFrançois Tigeot 	atom_op_add, ATOM_ARG_MC}, {
1089926deccbSFrançois Tigeot 	atom_op_sub, ATOM_ARG_REG}, {
1090926deccbSFrançois Tigeot 	atom_op_sub, ATOM_ARG_PS}, {
1091926deccbSFrançois Tigeot 	atom_op_sub, ATOM_ARG_WS}, {
1092926deccbSFrançois Tigeot 	atom_op_sub, ATOM_ARG_FB}, {
1093926deccbSFrançois Tigeot 	atom_op_sub, ATOM_ARG_PLL}, {
1094926deccbSFrançois Tigeot 	atom_op_sub, ATOM_ARG_MC}, {
1095926deccbSFrançois Tigeot 	atom_op_setport, ATOM_PORT_ATI}, {
1096926deccbSFrançois Tigeot 	atom_op_setport, ATOM_PORT_PCI}, {
1097926deccbSFrançois Tigeot 	atom_op_setport, ATOM_PORT_SYSIO}, {
1098926deccbSFrançois Tigeot 	atom_op_setregblock, 0}, {
1099926deccbSFrançois Tigeot 	atom_op_setfbbase, 0}, {
1100926deccbSFrançois Tigeot 	atom_op_compare, ATOM_ARG_REG}, {
1101926deccbSFrançois Tigeot 	atom_op_compare, ATOM_ARG_PS}, {
1102926deccbSFrançois Tigeot 	atom_op_compare, ATOM_ARG_WS}, {
1103926deccbSFrançois Tigeot 	atom_op_compare, ATOM_ARG_FB}, {
1104926deccbSFrançois Tigeot 	atom_op_compare, ATOM_ARG_PLL}, {
1105926deccbSFrançois Tigeot 	atom_op_compare, ATOM_ARG_MC}, {
1106926deccbSFrançois Tigeot 	atom_op_switch, 0}, {
1107926deccbSFrançois Tigeot 	atom_op_jump, ATOM_COND_ALWAYS}, {
1108926deccbSFrançois Tigeot 	atom_op_jump, ATOM_COND_EQUAL}, {
1109926deccbSFrançois Tigeot 	atom_op_jump, ATOM_COND_BELOW}, {
1110926deccbSFrançois Tigeot 	atom_op_jump, ATOM_COND_ABOVE}, {
1111926deccbSFrançois Tigeot 	atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
1112926deccbSFrançois Tigeot 	atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
1113926deccbSFrançois Tigeot 	atom_op_jump, ATOM_COND_NOTEQUAL}, {
1114926deccbSFrançois Tigeot 	atom_op_test, ATOM_ARG_REG}, {
1115926deccbSFrançois Tigeot 	atom_op_test, ATOM_ARG_PS}, {
1116926deccbSFrançois Tigeot 	atom_op_test, ATOM_ARG_WS}, {
1117926deccbSFrançois Tigeot 	atom_op_test, ATOM_ARG_FB}, {
1118926deccbSFrançois Tigeot 	atom_op_test, ATOM_ARG_PLL}, {
1119926deccbSFrançois Tigeot 	atom_op_test, ATOM_ARG_MC}, {
1120926deccbSFrançois Tigeot 	atom_op_delay, ATOM_UNIT_MILLISEC}, {
1121926deccbSFrançois Tigeot 	atom_op_delay, ATOM_UNIT_MICROSEC}, {
1122926deccbSFrançois Tigeot 	atom_op_calltable, 0}, {
1123926deccbSFrançois Tigeot 	atom_op_repeat, 0}, {
1124926deccbSFrançois Tigeot 	atom_op_clear, ATOM_ARG_REG}, {
1125926deccbSFrançois Tigeot 	atom_op_clear, ATOM_ARG_PS}, {
1126926deccbSFrançois Tigeot 	atom_op_clear, ATOM_ARG_WS}, {
1127926deccbSFrançois Tigeot 	atom_op_clear, ATOM_ARG_FB}, {
1128926deccbSFrançois Tigeot 	atom_op_clear, ATOM_ARG_PLL}, {
1129926deccbSFrançois Tigeot 	atom_op_clear, ATOM_ARG_MC}, {
1130926deccbSFrançois Tigeot 	atom_op_nop, 0}, {
1131926deccbSFrançois Tigeot 	atom_op_eot, 0}, {
1132926deccbSFrançois Tigeot 	atom_op_mask, ATOM_ARG_REG}, {
1133926deccbSFrançois Tigeot 	atom_op_mask, ATOM_ARG_PS}, {
1134926deccbSFrançois Tigeot 	atom_op_mask, ATOM_ARG_WS}, {
1135926deccbSFrançois Tigeot 	atom_op_mask, ATOM_ARG_FB}, {
1136926deccbSFrançois Tigeot 	atom_op_mask, ATOM_ARG_PLL}, {
1137926deccbSFrançois Tigeot 	atom_op_mask, ATOM_ARG_MC}, {
1138926deccbSFrançois Tigeot 	atom_op_postcard, 0}, {
1139926deccbSFrançois Tigeot 	atom_op_beep, 0}, {
1140926deccbSFrançois Tigeot 	atom_op_savereg, 0}, {
1141926deccbSFrançois Tigeot 	atom_op_restorereg, 0}, {
1142926deccbSFrançois Tigeot 	atom_op_setdatablock, 0}, {
1143926deccbSFrançois Tigeot 	atom_op_xor, ATOM_ARG_REG}, {
1144926deccbSFrançois Tigeot 	atom_op_xor, ATOM_ARG_PS}, {
1145926deccbSFrançois Tigeot 	atom_op_xor, ATOM_ARG_WS}, {
1146926deccbSFrançois Tigeot 	atom_op_xor, ATOM_ARG_FB}, {
1147926deccbSFrançois Tigeot 	atom_op_xor, ATOM_ARG_PLL}, {
1148926deccbSFrançois Tigeot 	atom_op_xor, ATOM_ARG_MC}, {
1149926deccbSFrançois Tigeot 	atom_op_shl, ATOM_ARG_REG}, {
1150926deccbSFrançois Tigeot 	atom_op_shl, ATOM_ARG_PS}, {
1151926deccbSFrançois Tigeot 	atom_op_shl, ATOM_ARG_WS}, {
1152926deccbSFrançois Tigeot 	atom_op_shl, ATOM_ARG_FB}, {
1153926deccbSFrançois Tigeot 	atom_op_shl, ATOM_ARG_PLL}, {
1154926deccbSFrançois Tigeot 	atom_op_shl, ATOM_ARG_MC}, {
1155926deccbSFrançois Tigeot 	atom_op_shr, ATOM_ARG_REG}, {
1156926deccbSFrançois Tigeot 	atom_op_shr, ATOM_ARG_PS}, {
1157926deccbSFrançois Tigeot 	atom_op_shr, ATOM_ARG_WS}, {
1158926deccbSFrançois Tigeot 	atom_op_shr, ATOM_ARG_FB}, {
1159926deccbSFrançois Tigeot 	atom_op_shr, ATOM_ARG_PLL}, {
1160926deccbSFrançois Tigeot 	atom_op_shr, ATOM_ARG_MC}, {
1161926deccbSFrançois Tigeot atom_op_debug, 0},};
1162926deccbSFrançois Tigeot 
atom_execute_table_locked(struct atom_context * ctx,int index,uint32_t * params)1163926deccbSFrançois Tigeot static int atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
1164926deccbSFrançois Tigeot {
1165926deccbSFrançois Tigeot 	int base = CU16(ctx->cmd_table + 4 + 2 * index);
1166926deccbSFrançois Tigeot 	int len, ws, ps, ptr;
1167926deccbSFrançois Tigeot 	unsigned char op;
1168926deccbSFrançois Tigeot 	atom_exec_context ectx;
1169926deccbSFrançois Tigeot 	int ret = 0;
1170926deccbSFrançois Tigeot 
1171926deccbSFrançois Tigeot 	if (!base)
1172926deccbSFrançois Tigeot 		return -EINVAL;
1173926deccbSFrançois Tigeot 
1174926deccbSFrançois Tigeot 	len = CU16(base + ATOM_CT_SIZE_PTR);
1175926deccbSFrançois Tigeot 	ws = CU8(base + ATOM_CT_WS_PTR);
1176926deccbSFrançois Tigeot 	ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
1177926deccbSFrançois Tigeot 	ptr = base + ATOM_CT_CODE_PTR;
1178926deccbSFrançois Tigeot 
1179ee479021SImre Vadász 	ATOM_SDEBUG_PRINT(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
1180926deccbSFrançois Tigeot 
1181926deccbSFrançois Tigeot 	ectx.ctx = ctx;
1182926deccbSFrançois Tigeot 	ectx.ps_shift = ps / 4;
1183926deccbSFrançois Tigeot 	ectx.start = base;
1184926deccbSFrançois Tigeot 	ectx.ps = params;
1185926deccbSFrançois Tigeot 	ectx.abort = false;
1186926deccbSFrançois Tigeot 	ectx.last_jump = 0;
1187926deccbSFrançois Tigeot 	if (ws)
1188c4ef309bSzrj 		ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
1189926deccbSFrançois Tigeot 	else
1190926deccbSFrançois Tigeot 		ectx.ws = NULL;
1191926deccbSFrançois Tigeot 
1192926deccbSFrançois Tigeot 	debug_depth++;
1193926deccbSFrançois Tigeot 	while (1) {
1194926deccbSFrançois Tigeot 		op = CU8(ptr++);
1195926deccbSFrançois Tigeot 		if (op < ATOM_OP_NAMES_CNT)
1196ee479021SImre Vadász 			ATOM_SDEBUG_PRINT("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
1197926deccbSFrançois Tigeot 		else
1198ee479021SImre Vadász 			ATOM_SDEBUG_PRINT("[%d] @ 0x%04X\n", op, ptr - 1);
1199926deccbSFrançois Tigeot 		if (ectx.abort) {
1200926deccbSFrançois Tigeot 			DRM_ERROR("atombios stuck executing %04X (len %d, WS %d, PS %d) @ 0x%04X\n",
1201926deccbSFrançois Tigeot 				base, len, ws, ps, ptr - 1);
1202926deccbSFrançois Tigeot 			ret = -EINVAL;
1203926deccbSFrançois Tigeot 			goto free;
1204926deccbSFrançois Tigeot 		}
1205926deccbSFrançois Tigeot 
1206926deccbSFrançois Tigeot 		if (op < ATOM_OP_CNT && op > 0)
1207926deccbSFrançois Tigeot 			opcode_table[op].func(&ectx, &ptr,
1208926deccbSFrançois Tigeot 					      opcode_table[op].arg);
1209926deccbSFrançois Tigeot 		else
1210926deccbSFrançois Tigeot 			break;
1211926deccbSFrançois Tigeot 
1212926deccbSFrançois Tigeot 		if (op == ATOM_OP_EOT)
1213926deccbSFrançois Tigeot 			break;
1214926deccbSFrançois Tigeot 	}
1215926deccbSFrançois Tigeot 	debug_depth--;
1216ee479021SImre Vadász 	ATOM_SDEBUG_PRINT("<<\n");
1217926deccbSFrançois Tigeot 
1218926deccbSFrançois Tigeot free:
1219926deccbSFrançois Tigeot 	if (ws)
1220c4ef309bSzrj 		kfree(ectx.ws);
1221926deccbSFrançois Tigeot 	return ret;
1222926deccbSFrançois Tigeot }
1223926deccbSFrançois Tigeot 
atom_execute_table_scratch_unlocked(struct atom_context * ctx,int index,uint32_t * params)1224591d5043SFrançois Tigeot int atom_execute_table_scratch_unlocked(struct atom_context *ctx, int index, uint32_t * params)
1225926deccbSFrançois Tigeot {
1226926deccbSFrançois Tigeot 	int r;
1227926deccbSFrançois Tigeot 
1228ee479021SImre Vadász 	lockmgr(&ctx->mutex, LK_EXCLUSIVE);
122957e252bfSMichael Neumann 	/* reset data block */
123057e252bfSMichael Neumann 	ctx->data_block = 0;
1231926deccbSFrançois Tigeot 	/* reset reg block */
1232926deccbSFrançois Tigeot 	ctx->reg_block = 0;
1233926deccbSFrançois Tigeot 	/* reset fb window */
1234926deccbSFrançois Tigeot 	ctx->fb_base = 0;
1235926deccbSFrançois Tigeot 	/* reset io mode */
1236926deccbSFrançois Tigeot 	ctx->io_mode = ATOM_IO_MM;
123757e252bfSMichael Neumann 	/* reset divmul */
123857e252bfSMichael Neumann 	ctx->divmul[0] = 0;
123957e252bfSMichael Neumann 	ctx->divmul[1] = 0;
1240926deccbSFrançois Tigeot 	r = atom_execute_table_locked(ctx, index, params);
1241ee479021SImre Vadász 	lockmgr(&ctx->mutex, LK_RELEASE);
1242926deccbSFrançois Tigeot 	return r;
1243926deccbSFrançois Tigeot }
1244926deccbSFrançois Tigeot 
atom_execute_table(struct atom_context * ctx,int index,uint32_t * params)1245591d5043SFrançois Tigeot int atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
1246591d5043SFrançois Tigeot {
1247591d5043SFrançois Tigeot 	int r;
1248ee479021SImre Vadász 	lockmgr(&ctx->scratch_mutex, LK_EXCLUSIVE);
1249591d5043SFrançois Tigeot 	r = atom_execute_table_scratch_unlocked(ctx, index, params);
1250ee479021SImre Vadász 	lockmgr(&ctx->scratch_mutex, LK_RELEASE);
1251591d5043SFrançois Tigeot 	return r;
1252591d5043SFrançois Tigeot }
1253591d5043SFrançois Tigeot 
1254926deccbSFrançois Tigeot static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1255926deccbSFrançois Tigeot 
atom_index_iio(struct atom_context * ctx,int base)1256926deccbSFrançois Tigeot static void atom_index_iio(struct atom_context *ctx, int base)
1257926deccbSFrançois Tigeot {
1258c4ef309bSzrj 	ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
1259b403bed8SMichael Neumann 	if (!ctx->iio)
1260b403bed8SMichael Neumann 		return;
1261926deccbSFrançois Tigeot 	while (CU8(base) == ATOM_IIO_START) {
1262926deccbSFrançois Tigeot 		ctx->iio[CU8(base + 1)] = base + 2;
1263926deccbSFrançois Tigeot 		base += 2;
1264926deccbSFrançois Tigeot 		while (CU8(base) != ATOM_IIO_END)
1265926deccbSFrançois Tigeot 			base += atom_iio_len[CU8(base)];
1266926deccbSFrançois Tigeot 		base += 3;
1267926deccbSFrançois Tigeot 	}
1268926deccbSFrançois Tigeot }
1269926deccbSFrançois Tigeot 
atom_parse(struct card_info * card,void * bios)1270926deccbSFrançois Tigeot struct atom_context *atom_parse(struct card_info *card, void *bios)
1271926deccbSFrançois Tigeot {
1272926deccbSFrançois Tigeot 	int base;
1273926deccbSFrançois Tigeot 	struct atom_context *ctx =
1274c4ef309bSzrj 	    kzalloc(sizeof(struct atom_context), GFP_KERNEL);
1275926deccbSFrançois Tigeot 	char *str;
1276926deccbSFrançois Tigeot 	char name[512];
1277926deccbSFrançois Tigeot 	int i;
1278926deccbSFrançois Tigeot 
1279926deccbSFrançois Tigeot 	if (!ctx)
1280926deccbSFrançois Tigeot 		return NULL;
1281926deccbSFrançois Tigeot 
1282926deccbSFrançois Tigeot 	ctx->card = card;
1283926deccbSFrançois Tigeot 	ctx->bios = bios;
1284926deccbSFrançois Tigeot 
1285926deccbSFrançois Tigeot 	if (CU16(0) != ATOM_BIOS_MAGIC) {
1286926deccbSFrançois Tigeot 		DRM_INFO("Invalid BIOS magic.\n");
1287c4ef309bSzrj 		kfree(ctx);
1288926deccbSFrançois Tigeot 		return NULL;
1289926deccbSFrançois Tigeot 	}
1290926deccbSFrançois Tigeot 	if (strncmp
1291926deccbSFrançois Tigeot 	    (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
1292926deccbSFrançois Tigeot 	     strlen(ATOM_ATI_MAGIC))) {
1293926deccbSFrançois Tigeot 		DRM_INFO("Invalid ATI magic.\n");
1294c4ef309bSzrj 		kfree(ctx);
1295926deccbSFrançois Tigeot 		return NULL;
1296926deccbSFrançois Tigeot 	}
1297926deccbSFrançois Tigeot 
1298926deccbSFrançois Tigeot 	base = CU16(ATOM_ROM_TABLE_PTR);
1299926deccbSFrançois Tigeot 	if (strncmp
1300926deccbSFrançois Tigeot 	    (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
1301926deccbSFrançois Tigeot 	     strlen(ATOM_ROM_MAGIC))) {
1302926deccbSFrançois Tigeot 		DRM_INFO("Invalid ATOM magic.\n");
1303c4ef309bSzrj 		kfree(ctx);
1304926deccbSFrançois Tigeot 		return NULL;
1305926deccbSFrançois Tigeot 	}
1306926deccbSFrançois Tigeot 
1307926deccbSFrançois Tigeot 	ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
1308926deccbSFrançois Tigeot 	ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
1309926deccbSFrançois Tigeot 	atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
1310b403bed8SMichael Neumann 	if (!ctx->iio) {
1311b403bed8SMichael Neumann 		atom_destroy(ctx);
1312b403bed8SMichael Neumann 		return NULL;
1313b403bed8SMichael Neumann 	}
1314926deccbSFrançois Tigeot 
1315926deccbSFrançois Tigeot 	str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
1316926deccbSFrançois Tigeot 	while (*str && ((*str == '\n') || (*str == '\r')))
1317926deccbSFrançois Tigeot 		str++;
1318926deccbSFrançois Tigeot 	/* name string isn't always 0 terminated */
1319926deccbSFrançois Tigeot 	for (i = 0; i < 511; i++) {
1320926deccbSFrançois Tigeot 		name[i] = str[i];
1321926deccbSFrançois Tigeot 		if (name[i] < '.' || name[i] > 'z') {
1322926deccbSFrançois Tigeot 			name[i] = 0;
1323926deccbSFrançois Tigeot 			break;
1324926deccbSFrançois Tigeot 		}
1325926deccbSFrançois Tigeot 	}
1326926deccbSFrançois Tigeot 	DRM_INFO("ATOM BIOS: %s\n", name);
1327926deccbSFrançois Tigeot 
1328926deccbSFrançois Tigeot 	return ctx;
1329926deccbSFrançois Tigeot }
1330926deccbSFrançois Tigeot 
atom_asic_init(struct atom_context * ctx)1331926deccbSFrançois Tigeot int atom_asic_init(struct atom_context *ctx)
1332926deccbSFrançois Tigeot {
1333926deccbSFrançois Tigeot 	struct radeon_device *rdev = ctx->card->dev->dev_private;
1334926deccbSFrançois Tigeot 	int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
1335926deccbSFrançois Tigeot 	uint32_t ps[16];
1336926deccbSFrançois Tigeot 	int ret;
1337926deccbSFrançois Tigeot 
1338926deccbSFrançois Tigeot 	memset(ps, 0, 64);
1339926deccbSFrançois Tigeot 
1340926deccbSFrançois Tigeot 	ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
1341926deccbSFrançois Tigeot 	ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
1342926deccbSFrançois Tigeot 	if (!ps[0] || !ps[1])
1343926deccbSFrançois Tigeot 		return 1;
1344926deccbSFrançois Tigeot 
1345926deccbSFrançois Tigeot 	if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
1346926deccbSFrançois Tigeot 		return 1;
1347926deccbSFrançois Tigeot 	ret = atom_execute_table(ctx, ATOM_CMD_INIT, ps);
1348926deccbSFrançois Tigeot 	if (ret)
1349926deccbSFrançois Tigeot 		return ret;
1350926deccbSFrançois Tigeot 
1351926deccbSFrançois Tigeot 	memset(ps, 0, 64);
1352926deccbSFrançois Tigeot 
1353926deccbSFrançois Tigeot 	if (rdev->family < CHIP_R600) {
1354926deccbSFrançois Tigeot 		if (CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_SPDFANCNTL))
1355926deccbSFrançois Tigeot 			atom_execute_table(ctx, ATOM_CMD_SPDFANCNTL, ps);
1356926deccbSFrançois Tigeot 	}
1357926deccbSFrançois Tigeot 	return ret;
1358926deccbSFrançois Tigeot }
1359926deccbSFrançois Tigeot 
atom_destroy(struct atom_context * ctx)1360926deccbSFrançois Tigeot void atom_destroy(struct atom_context *ctx)
1361926deccbSFrançois Tigeot {
1362c4ef309bSzrj 	kfree(ctx->iio);
1363c4ef309bSzrj 	kfree(ctx);
1364926deccbSFrançois Tigeot }
1365926deccbSFrançois Tigeot 
atom_parse_data_header(struct atom_context * ctx,int index,uint16_t * size,uint8_t * frev,uint8_t * crev,uint16_t * data_start)1366926deccbSFrançois Tigeot bool atom_parse_data_header(struct atom_context *ctx, int index,
1367926deccbSFrançois Tigeot 			    uint16_t * size, uint8_t * frev, uint8_t * crev,
1368926deccbSFrançois Tigeot 			    uint16_t * data_start)
1369926deccbSFrançois Tigeot {
1370926deccbSFrançois Tigeot 	int offset = index * 2 + 4;
1371926deccbSFrançois Tigeot 	int idx = CU16(ctx->data_table + offset);
1372926deccbSFrançois Tigeot 	u16 *mdt = (u16 *)((char *)ctx->bios + ctx->data_table + 4);
1373926deccbSFrançois Tigeot 
1374926deccbSFrançois Tigeot 	if (!mdt[index])
1375926deccbSFrançois Tigeot 		return false;
1376926deccbSFrançois Tigeot 
1377926deccbSFrançois Tigeot 	if (size)
1378926deccbSFrançois Tigeot 		*size = CU16(idx);
1379926deccbSFrançois Tigeot 	if (frev)
1380926deccbSFrançois Tigeot 		*frev = CU8(idx + 2);
1381926deccbSFrançois Tigeot 	if (crev)
1382926deccbSFrançois Tigeot 		*crev = CU8(idx + 3);
1383926deccbSFrançois Tigeot 	*data_start = idx;
1384926deccbSFrançois Tigeot 	return true;
1385926deccbSFrançois Tigeot }
1386926deccbSFrançois Tigeot 
atom_parse_cmd_header(struct atom_context * ctx,int index,uint8_t * frev,uint8_t * crev)1387926deccbSFrançois Tigeot bool atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
1388926deccbSFrançois Tigeot 			   uint8_t * crev)
1389926deccbSFrançois Tigeot {
1390926deccbSFrançois Tigeot 	int offset = index * 2 + 4;
1391926deccbSFrançois Tigeot 	int idx = CU16(ctx->cmd_table + offset);
1392926deccbSFrançois Tigeot 	u16 *mct = (u16 *)((char *)ctx->bios + ctx->cmd_table + 4);
1393926deccbSFrançois Tigeot 
1394926deccbSFrançois Tigeot 	if (!mct[index])
1395926deccbSFrançois Tigeot 		return false;
1396926deccbSFrançois Tigeot 
1397926deccbSFrançois Tigeot 	if (frev)
1398926deccbSFrançois Tigeot 		*frev = CU8(idx + 2);
1399926deccbSFrançois Tigeot 	if (crev)
1400926deccbSFrançois Tigeot 		*crev = CU8(idx + 3);
1401926deccbSFrançois Tigeot 	return true;
1402926deccbSFrançois Tigeot }
1403926deccbSFrançois Tigeot 
atom_allocate_fb_scratch(struct atom_context * ctx)1404926deccbSFrançois Tigeot int atom_allocate_fb_scratch(struct atom_context *ctx)
1405926deccbSFrançois Tigeot {
1406926deccbSFrançois Tigeot 	int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
1407926deccbSFrançois Tigeot 	uint16_t data_offset;
1408926deccbSFrançois Tigeot 	int usage_bytes = 0;
1409926deccbSFrançois Tigeot 	struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
1410926deccbSFrançois Tigeot 
1411926deccbSFrançois Tigeot 	if (atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
1412926deccbSFrançois Tigeot 		firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)((char *)ctx->bios + data_offset);
1413926deccbSFrançois Tigeot 
1414926deccbSFrançois Tigeot 		DRM_DEBUG("atom firmware requested %08x %dkb\n",
1415f43cf1b1SMichael Neumann 			  le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
1416f43cf1b1SMichael Neumann 			  le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
1417926deccbSFrançois Tigeot 
1418f43cf1b1SMichael Neumann 		usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
1419926deccbSFrançois Tigeot 	}
1420926deccbSFrançois Tigeot 	ctx->scratch_size_bytes = 0;
1421926deccbSFrançois Tigeot 	if (usage_bytes == 0)
1422926deccbSFrançois Tigeot 		usage_bytes = 20 * 1024;
1423926deccbSFrançois Tigeot 	/* allocate some scratch memory */
1424c4ef309bSzrj 	ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
1425926deccbSFrançois Tigeot 	if (!ctx->scratch)
1426926deccbSFrançois Tigeot 		return -ENOMEM;
1427926deccbSFrançois Tigeot 	ctx->scratch_size_bytes = usage_bytes;
1428926deccbSFrançois Tigeot 	return 0;
1429926deccbSFrançois Tigeot }
1430