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