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