2 * Copyright 2008 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Author: Stanislaw Skowronek
25 #include <linux/module.h>
26 #include <linux/sched.h>
27 #include <asm/unaligned.h>
32 #include "atom-names.h"
33 #include "atom-bits.h"
35 #define ATOM_COND_ABOVE 0
36 #define ATOM_COND_ABOVEOREQUAL 1
37 #define ATOM_COND_ALWAYS 2
38 #define ATOM_COND_BELOW 3
39 #define ATOM_COND_BELOWOREQUAL 4
40 #define ATOM_COND_EQUAL 5
41 #define ATOM_COND_NOTEQUAL 6
43 #define ATOM_PORT_ATI 0
44 #define ATOM_PORT_PCI 1
45 #define ATOM_PORT_SYSIO 2
47 #define ATOM_UNIT_MICROSEC 0
48 #define ATOM_UNIT_MILLISEC 1
54 struct atom_context *ctx;
62 static void atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params);
63 void atom_execute_table(struct atom_context *ctx, int index, uint32_t * params);
65 static uint32_t atom_arg_mask[8] =
66 { 0xFFFFFFFF, 0xFFFF, 0xFFFF00, 0xFFFF0000, 0xFF, 0xFF00, 0xFF0000,
68 static int atom_arg_shift[8] = { 0, 0, 8, 16, 0, 8, 16, 24 };
70 static int atom_dst_to_src[8][4] = {
71 /* translate destination alignment field to the source alignment encoding */
81 static int atom_def_dst[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
83 static int debug_depth = 0;
85 static void debug_print_spaces(int n)
91 #define DEBUG(...) do if (atom_debug) { printk(KERN_DEBUG __VA_ARGS__); } while (0)
92 #define SDEBUG(...) do if (atom_debug) { printk(KERN_DEBUG); debug_print_spaces(debug_depth); printk(__VA_ARGS__); } while (0)
94 #define DEBUG(...) do { } while (0)
95 #define SDEBUG(...) do { } while (0)
98 static uint32_t atom_iio_execute(struct atom_context *ctx, int base,
99 uint32_t index, uint32_t data)
101 uint32_t temp = 0xCDCDCDCD;
108 temp = ctx->card->reg_read(ctx->card, CU16(base + 1));
112 (void)ctx->card->reg_read(ctx->card, CU16(base + 1));
113 ctx->card->reg_write(ctx->card, CU16(base + 1), temp);
118 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
124 (0xFFFFFFFF >> (32 - CU8(base + 1))) << CU8(base +
128 case ATOM_IIO_MOVE_INDEX:
130 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
133 ((index >> CU8(base + 2)) &
134 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
138 case ATOM_IIO_MOVE_DATA:
140 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
143 ((data >> CU8(base + 2)) &
144 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
148 case ATOM_IIO_MOVE_ATTR:
150 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
154 io_attr >> CU8(base + 2)) & (0xFFFFFFFF >> (32 -
165 printk(KERN_INFO "Unknown IIO opcode.\n");
170 static uint32_t atom_get_src_int(atom_exec_context *ctx, uint8_t attr,
171 int *ptr, uint32_t *saved, int print)
173 uint32_t idx, val = 0xCDCDCDCD, align, arg;
174 struct atom_context *gctx = ctx->ctx;
176 align = (attr >> 3) & 7;
182 DEBUG("REG[0x%04X]", idx);
183 idx += gctx->reg_block;
184 switch (gctx->io_mode) {
186 val = gctx->card->reg_read(gctx->card, idx);
190 "PCI registers are not implemented.\n");
194 "SYSIO registers are not implemented.\n");
197 if (!(gctx->io_mode & 0x80)) {
198 printk(KERN_INFO "Bad IO mode.\n");
201 if (!gctx->iio[gctx->io_mode & 0x7F]) {
203 "Undefined indirect IO read method %d.\n",
204 gctx->io_mode & 0x7F);
208 atom_iio_execute(gctx,
209 gctx->iio[gctx->io_mode & 0x7F],
216 /* get_unaligned_le32 avoids unaligned accesses from atombios
217 * tables, noticed on a DEC Alpha. */
218 val = get_unaligned_le32((u32 *)&ctx->ps[idx]);
220 DEBUG("PS[0x%02X,0x%04X]", idx, val);
226 DEBUG("WS[0x%02X]", idx);
228 case ATOM_WS_QUOTIENT:
229 val = gctx->divmul[0];
231 case ATOM_WS_REMAINDER:
232 val = gctx->divmul[1];
234 case ATOM_WS_DATAPTR:
235 val = gctx->data_block;
240 case ATOM_WS_OR_MASK:
241 val = 1 << gctx->shift;
243 case ATOM_WS_AND_MASK:
244 val = ~(1 << gctx->shift);
246 case ATOM_WS_FB_WINDOW:
249 case ATOM_WS_ATTRIBUTES:
253 val = gctx->reg_block;
263 if (gctx->data_block)
264 DEBUG("ID[0x%04X+%04X]", idx, gctx->data_block);
266 DEBUG("ID[0x%04X]", idx);
268 val = U32(idx + gctx->data_block);
273 val = gctx->scratch[((gctx->fb_base + idx) / 4)];
275 DEBUG("FB[0x%02X]", idx);
283 DEBUG("IMM 0x%08X\n", val);
287 case ATOM_SRC_WORD16:
291 DEBUG("IMM 0x%04X\n", val);
295 case ATOM_SRC_BYTE16:
296 case ATOM_SRC_BYTE24:
300 DEBUG("IMM 0x%02X\n", val);
308 DEBUG("PLL[0x%02X]", idx);
309 val = gctx->card->pll_read(gctx->card, idx);
315 DEBUG("MC[0x%02X]", idx);
316 val = gctx->card->mc_read(gctx->card, idx);
321 val &= atom_arg_mask[align];
322 val >>= atom_arg_shift[align];
326 DEBUG(".[31:0] -> 0x%08X\n", val);
329 DEBUG(".[15:0] -> 0x%04X\n", val);
332 DEBUG(".[23:8] -> 0x%04X\n", val);
334 case ATOM_SRC_WORD16:
335 DEBUG(".[31:16] -> 0x%04X\n", val);
338 DEBUG(".[7:0] -> 0x%02X\n", val);
341 DEBUG(".[15:8] -> 0x%02X\n", val);
343 case ATOM_SRC_BYTE16:
344 DEBUG(".[23:16] -> 0x%02X\n", val);
346 case ATOM_SRC_BYTE24:
347 DEBUG(".[31:24] -> 0x%02X\n", val);
353 static void atom_skip_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr)
355 uint32_t align = (attr >> 3) & 7, arg = attr & 7;
375 case ATOM_SRC_WORD16:
380 case ATOM_SRC_BYTE16:
381 case ATOM_SRC_BYTE24:
389 static uint32_t atom_get_src(atom_exec_context *ctx, uint8_t attr, int *ptr)
391 return atom_get_src_int(ctx, attr, ptr, NULL, 1);
394 static uint32_t atom_get_src_direct(atom_exec_context *ctx, uint8_t align, int *ptr)
396 uint32_t val = 0xCDCDCDCD;
405 case ATOM_SRC_WORD16:
411 case ATOM_SRC_BYTE16:
412 case ATOM_SRC_BYTE24:
420 static uint32_t atom_get_dst(atom_exec_context *ctx, int arg, uint8_t attr,
421 int *ptr, uint32_t *saved, int print)
423 return atom_get_src_int(ctx,
424 arg | atom_dst_to_src[(attr >> 3) &
425 7][(attr >> 6) & 3] << 3,
429 static void atom_skip_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr)
431 atom_skip_src_int(ctx,
432 arg | atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) &
436 static void atom_put_dst(atom_exec_context *ctx, int arg, uint8_t attr,
437 int *ptr, uint32_t val, uint32_t saved)
440 atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3], old_val =
442 struct atom_context *gctx = ctx->ctx;
443 old_val &= atom_arg_mask[align] >> atom_arg_shift[align];
444 val <<= atom_arg_shift[align];
445 val &= atom_arg_mask[align];
446 saved &= ~atom_arg_mask[align];
452 DEBUG("REG[0x%04X]", idx);
453 idx += gctx->reg_block;
454 switch (gctx->io_mode) {
457 gctx->card->reg_write(gctx->card, idx,
460 gctx->card->reg_write(gctx->card, idx, val);
464 "PCI registers are not implemented.\n");
468 "SYSIO registers are not implemented.\n");
471 if (!(gctx->io_mode & 0x80)) {
472 printk(KERN_INFO "Bad IO mode.\n");
475 if (!gctx->iio[gctx->io_mode & 0xFF]) {
477 "Undefined indirect IO write method %d.\n",
478 gctx->io_mode & 0x7F);
481 atom_iio_execute(gctx, gctx->iio[gctx->io_mode & 0xFF],
488 DEBUG("PS[0x%02X]", idx);
489 ctx->ps[idx] = cpu_to_le32(val);
494 DEBUG("WS[0x%02X]", idx);
496 case ATOM_WS_QUOTIENT:
497 gctx->divmul[0] = val;
499 case ATOM_WS_REMAINDER:
500 gctx->divmul[1] = val;
502 case ATOM_WS_DATAPTR:
503 gctx->data_block = val;
508 case ATOM_WS_OR_MASK:
509 case ATOM_WS_AND_MASK:
511 case ATOM_WS_FB_WINDOW:
514 case ATOM_WS_ATTRIBUTES:
518 gctx->reg_block = val;
527 gctx->scratch[((gctx->fb_base + idx) / 4)] = val;
528 DEBUG("FB[0x%02X]", idx);
533 DEBUG("PLL[0x%02X]", idx);
534 gctx->card->pll_write(gctx->card, idx, val);
539 DEBUG("MC[0x%02X]", idx);
540 gctx->card->mc_write(gctx->card, idx, val);
545 DEBUG(".[31:0] <- 0x%08X\n", old_val);
548 DEBUG(".[15:0] <- 0x%04X\n", old_val);
551 DEBUG(".[23:8] <- 0x%04X\n", old_val);
553 case ATOM_SRC_WORD16:
554 DEBUG(".[31:16] <- 0x%04X\n", old_val);
557 DEBUG(".[7:0] <- 0x%02X\n", old_val);
560 DEBUG(".[15:8] <- 0x%02X\n", old_val);
562 case ATOM_SRC_BYTE16:
563 DEBUG(".[23:16] <- 0x%02X\n", old_val);
565 case ATOM_SRC_BYTE24:
566 DEBUG(".[31:24] <- 0x%02X\n", old_val);
571 static void atom_op_add(atom_exec_context *ctx, int *ptr, int arg)
573 uint8_t attr = U8((*ptr)++);
574 uint32_t dst, src, saved;
577 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
579 src = atom_get_src(ctx, attr, ptr);
582 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
585 static void atom_op_and(atom_exec_context *ctx, int *ptr, int arg)
587 uint8_t attr = U8((*ptr)++);
588 uint32_t dst, src, saved;
591 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
593 src = atom_get_src(ctx, attr, ptr);
596 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
599 static void atom_op_beep(atom_exec_context *ctx, int *ptr, int arg)
601 printk("ATOM BIOS beeped!\n");
604 static void atom_op_calltable(atom_exec_context *ctx, int *ptr, int arg)
606 int idx = U8((*ptr)++);
607 if (idx < ATOM_TABLE_NAMES_CNT)
608 SDEBUG(" table: %d (%s)\n", idx, atom_table_names[idx]);
610 SDEBUG(" table: %d\n", idx);
611 if (U16(ctx->ctx->cmd_table + 4 + 2 * idx))
612 atom_execute_table_locked(ctx->ctx, idx, ctx->ps + ctx->ps_shift);
615 static void atom_op_clear(atom_exec_context *ctx, int *ptr, int arg)
617 uint8_t attr = U8((*ptr)++);
621 attr |= atom_def_dst[attr >> 3] << 6;
622 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
624 atom_put_dst(ctx, arg, attr, &dptr, 0, saved);
627 static void atom_op_compare(atom_exec_context *ctx, int *ptr, int arg)
629 uint8_t attr = U8((*ptr)++);
632 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
634 src = atom_get_src(ctx, attr, ptr);
635 ctx->ctx->cs_equal = (dst == src);
636 ctx->ctx->cs_above = (dst > src);
637 SDEBUG(" result: %s %s\n", ctx->ctx->cs_equal ? "EQ" : "NE",
638 ctx->ctx->cs_above ? "GT" : "LE");
641 static void atom_op_delay(atom_exec_context *ctx, int *ptr, int arg)
643 uint8_t count = U8((*ptr)++);
644 SDEBUG(" count: %d\n", count);
645 if (arg == ATOM_UNIT_MICROSEC)
648 schedule_timeout_uninterruptible(msecs_to_jiffies(count));
651 static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
653 uint8_t attr = U8((*ptr)++);
656 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
658 src = atom_get_src(ctx, attr, ptr);
660 ctx->ctx->divmul[0] = dst / src;
661 ctx->ctx->divmul[1] = dst % src;
663 ctx->ctx->divmul[0] = 0;
664 ctx->ctx->divmul[1] = 0;
668 static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
670 /* functionally, a nop */
673 static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
675 int execute = 0, target = U16(*ptr);
678 case ATOM_COND_ABOVE:
679 execute = ctx->ctx->cs_above;
681 case ATOM_COND_ABOVEOREQUAL:
682 execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
684 case ATOM_COND_ALWAYS:
687 case ATOM_COND_BELOW:
688 execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
690 case ATOM_COND_BELOWOREQUAL:
691 execute = !ctx->ctx->cs_above;
693 case ATOM_COND_EQUAL:
694 execute = ctx->ctx->cs_equal;
696 case ATOM_COND_NOTEQUAL:
697 execute = !ctx->ctx->cs_equal;
700 if (arg != ATOM_COND_ALWAYS)
701 SDEBUG(" taken: %s\n", execute ? "yes" : "no");
702 SDEBUG(" target: 0x%04X\n", target);
704 *ptr = ctx->start + target;
707 static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
709 uint8_t attr = U8((*ptr)++);
710 uint32_t dst, src1, src2, saved;
713 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
715 src1 = atom_get_src_direct(ctx, ((attr >> 3) & 7), ptr);
717 src2 = atom_get_src(ctx, attr, ptr);
721 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
724 static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
726 uint8_t attr = U8((*ptr)++);
729 if (((attr >> 3) & 7) != ATOM_SRC_DWORD)
730 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
732 atom_skip_dst(ctx, arg, attr, ptr);
736 src = atom_get_src(ctx, attr, ptr);
738 atom_put_dst(ctx, arg, attr, &dptr, src, saved);
741 static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
743 uint8_t attr = U8((*ptr)++);
746 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
748 src = atom_get_src(ctx, attr, ptr);
749 ctx->ctx->divmul[0] = dst * src;
752 static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
757 static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
759 uint8_t attr = U8((*ptr)++);
760 uint32_t dst, src, saved;
763 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
765 src = atom_get_src(ctx, attr, ptr);
768 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
771 static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
773 uint8_t val = U8((*ptr)++);
774 SDEBUG("POST card output: 0x%02X\n", val);
777 static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
779 printk(KERN_INFO "unimplemented!\n");
782 static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
784 printk(KERN_INFO "unimplemented!\n");
787 static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
789 printk(KERN_INFO "unimplemented!\n");
792 static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
796 SDEBUG(" block: %d\n", idx);
798 ctx->ctx->data_block = 0;
800 ctx->ctx->data_block = ctx->start;
802 ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
803 SDEBUG(" base: 0x%04X\n", ctx->ctx->data_block);
806 static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
808 uint8_t attr = U8((*ptr)++);
809 SDEBUG(" fb_base: ");
810 ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
813 static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
819 if (port < ATOM_IO_NAMES_CNT)
820 SDEBUG(" port: %d (%s)\n", port, atom_io_names[port]);
822 SDEBUG(" port: %d\n", port);
824 ctx->ctx->io_mode = ATOM_IO_MM;
826 ctx->ctx->io_mode = ATOM_IO_IIO | port;
830 ctx->ctx->io_mode = ATOM_IO_PCI;
833 case ATOM_PORT_SYSIO:
834 ctx->ctx->io_mode = ATOM_IO_SYSIO;
840 static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
842 ctx->ctx->reg_block = U16(*ptr);
844 SDEBUG(" base: 0x%04X\n", ctx->ctx->reg_block);
847 static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
849 uint8_t attr = U8((*ptr)++), shift;
853 attr |= atom_def_dst[attr >> 3] << 6;
855 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
856 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
857 SDEBUG(" shift: %d\n", shift);
860 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
863 static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
865 uint8_t attr = U8((*ptr)++), shift;
869 attr |= atom_def_dst[attr >> 3] << 6;
871 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
872 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
873 SDEBUG(" shift: %d\n", shift);
876 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
879 static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
881 uint8_t attr = U8((*ptr)++), shift;
885 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
886 shift = atom_get_src(ctx, attr, ptr);
887 SDEBUG(" shift: %d\n", shift);
890 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
893 static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
895 uint8_t attr = U8((*ptr)++), shift;
899 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
900 shift = atom_get_src(ctx, attr, ptr);
901 SDEBUG(" shift: %d\n", shift);
904 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
907 static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
909 uint8_t attr = U8((*ptr)++);
910 uint32_t dst, src, saved;
913 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
915 src = atom_get_src(ctx, attr, ptr);
918 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
921 static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
923 uint8_t attr = U8((*ptr)++);
924 uint32_t src, val, target;
926 src = atom_get_src(ctx, attr, ptr);
927 while (U16(*ptr) != ATOM_CASE_END)
928 if (U8(*ptr) == ATOM_CASE_MAGIC) {
932 atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
936 SDEBUG(" target: %04X\n", target);
937 *ptr = ctx->start + target;
942 printk(KERN_INFO "Bad case.\n");
948 static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
950 uint8_t attr = U8((*ptr)++);
953 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
955 src = atom_get_src(ctx, attr, ptr);
956 ctx->ctx->cs_equal = ((dst & src) == 0);
957 SDEBUG(" result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
960 static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
962 uint8_t attr = U8((*ptr)++);
963 uint32_t dst, src, saved;
966 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
968 src = atom_get_src(ctx, attr, ptr);
971 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
974 static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
976 printk(KERN_INFO "unimplemented!\n");
980 void (*func) (atom_exec_context *, int *, int);
982 } opcode_table[ATOM_OP_CNT] = {
985 atom_op_move, ATOM_ARG_REG}, {
986 atom_op_move, ATOM_ARG_PS}, {
987 atom_op_move, ATOM_ARG_WS}, {
988 atom_op_move, ATOM_ARG_FB}, {
989 atom_op_move, ATOM_ARG_PLL}, {
990 atom_op_move, ATOM_ARG_MC}, {
991 atom_op_and, ATOM_ARG_REG}, {
992 atom_op_and, ATOM_ARG_PS}, {
993 atom_op_and, ATOM_ARG_WS}, {
994 atom_op_and, ATOM_ARG_FB}, {
995 atom_op_and, ATOM_ARG_PLL}, {
996 atom_op_and, ATOM_ARG_MC}, {
997 atom_op_or, ATOM_ARG_REG}, {
998 atom_op_or, ATOM_ARG_PS}, {
999 atom_op_or, ATOM_ARG_WS}, {
1000 atom_op_or, ATOM_ARG_FB}, {
1001 atom_op_or, ATOM_ARG_PLL}, {
1002 atom_op_or, ATOM_ARG_MC}, {
1003 atom_op_shift_left, ATOM_ARG_REG}, {
1004 atom_op_shift_left, ATOM_ARG_PS}, {
1005 atom_op_shift_left, ATOM_ARG_WS}, {
1006 atom_op_shift_left, ATOM_ARG_FB}, {
1007 atom_op_shift_left, ATOM_ARG_PLL}, {
1008 atom_op_shift_left, ATOM_ARG_MC}, {
1009 atom_op_shift_right, ATOM_ARG_REG}, {
1010 atom_op_shift_right, ATOM_ARG_PS}, {
1011 atom_op_shift_right, ATOM_ARG_WS}, {
1012 atom_op_shift_right, ATOM_ARG_FB}, {
1013 atom_op_shift_right, ATOM_ARG_PLL}, {
1014 atom_op_shift_right, ATOM_ARG_MC}, {
1015 atom_op_mul, ATOM_ARG_REG}, {
1016 atom_op_mul, ATOM_ARG_PS}, {
1017 atom_op_mul, ATOM_ARG_WS}, {
1018 atom_op_mul, ATOM_ARG_FB}, {
1019 atom_op_mul, ATOM_ARG_PLL}, {
1020 atom_op_mul, ATOM_ARG_MC}, {
1021 atom_op_div, ATOM_ARG_REG}, {
1022 atom_op_div, ATOM_ARG_PS}, {
1023 atom_op_div, ATOM_ARG_WS}, {
1024 atom_op_div, ATOM_ARG_FB}, {
1025 atom_op_div, ATOM_ARG_PLL}, {
1026 atom_op_div, ATOM_ARG_MC}, {
1027 atom_op_add, ATOM_ARG_REG}, {
1028 atom_op_add, ATOM_ARG_PS}, {
1029 atom_op_add, ATOM_ARG_WS}, {
1030 atom_op_add, ATOM_ARG_FB}, {
1031 atom_op_add, ATOM_ARG_PLL}, {
1032 atom_op_add, ATOM_ARG_MC}, {
1033 atom_op_sub, ATOM_ARG_REG}, {
1034 atom_op_sub, ATOM_ARG_PS}, {
1035 atom_op_sub, ATOM_ARG_WS}, {
1036 atom_op_sub, ATOM_ARG_FB}, {
1037 atom_op_sub, ATOM_ARG_PLL}, {
1038 atom_op_sub, ATOM_ARG_MC}, {
1039 atom_op_setport, ATOM_PORT_ATI}, {
1040 atom_op_setport, ATOM_PORT_PCI}, {
1041 atom_op_setport, ATOM_PORT_SYSIO}, {
1042 atom_op_setregblock, 0}, {
1043 atom_op_setfbbase, 0}, {
1044 atom_op_compare, ATOM_ARG_REG}, {
1045 atom_op_compare, ATOM_ARG_PS}, {
1046 atom_op_compare, ATOM_ARG_WS}, {
1047 atom_op_compare, ATOM_ARG_FB}, {
1048 atom_op_compare, ATOM_ARG_PLL}, {
1049 atom_op_compare, ATOM_ARG_MC}, {
1050 atom_op_switch, 0}, {
1051 atom_op_jump, ATOM_COND_ALWAYS}, {
1052 atom_op_jump, ATOM_COND_EQUAL}, {
1053 atom_op_jump, ATOM_COND_BELOW}, {
1054 atom_op_jump, ATOM_COND_ABOVE}, {
1055 atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
1056 atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
1057 atom_op_jump, ATOM_COND_NOTEQUAL}, {
1058 atom_op_test, ATOM_ARG_REG}, {
1059 atom_op_test, ATOM_ARG_PS}, {
1060 atom_op_test, ATOM_ARG_WS}, {
1061 atom_op_test, ATOM_ARG_FB}, {
1062 atom_op_test, ATOM_ARG_PLL}, {
1063 atom_op_test, ATOM_ARG_MC}, {
1064 atom_op_delay, ATOM_UNIT_MILLISEC}, {
1065 atom_op_delay, ATOM_UNIT_MICROSEC}, {
1066 atom_op_calltable, 0}, {
1067 atom_op_repeat, 0}, {
1068 atom_op_clear, ATOM_ARG_REG}, {
1069 atom_op_clear, ATOM_ARG_PS}, {
1070 atom_op_clear, ATOM_ARG_WS}, {
1071 atom_op_clear, ATOM_ARG_FB}, {
1072 atom_op_clear, ATOM_ARG_PLL}, {
1073 atom_op_clear, ATOM_ARG_MC}, {
1076 atom_op_mask, ATOM_ARG_REG}, {
1077 atom_op_mask, ATOM_ARG_PS}, {
1078 atom_op_mask, ATOM_ARG_WS}, {
1079 atom_op_mask, ATOM_ARG_FB}, {
1080 atom_op_mask, ATOM_ARG_PLL}, {
1081 atom_op_mask, ATOM_ARG_MC}, {
1082 atom_op_postcard, 0}, {
1084 atom_op_savereg, 0}, {
1085 atom_op_restorereg, 0}, {
1086 atom_op_setdatablock, 0}, {
1087 atom_op_xor, ATOM_ARG_REG}, {
1088 atom_op_xor, ATOM_ARG_PS}, {
1089 atom_op_xor, ATOM_ARG_WS}, {
1090 atom_op_xor, ATOM_ARG_FB}, {
1091 atom_op_xor, ATOM_ARG_PLL}, {
1092 atom_op_xor, ATOM_ARG_MC}, {
1093 atom_op_shl, ATOM_ARG_REG}, {
1094 atom_op_shl, ATOM_ARG_PS}, {
1095 atom_op_shl, ATOM_ARG_WS}, {
1096 atom_op_shl, ATOM_ARG_FB}, {
1097 atom_op_shl, ATOM_ARG_PLL}, {
1098 atom_op_shl, ATOM_ARG_MC}, {
1099 atom_op_shr, ATOM_ARG_REG}, {
1100 atom_op_shr, ATOM_ARG_PS}, {
1101 atom_op_shr, ATOM_ARG_WS}, {
1102 atom_op_shr, ATOM_ARG_FB}, {
1103 atom_op_shr, ATOM_ARG_PLL}, {
1104 atom_op_shr, ATOM_ARG_MC}, {
1105 atom_op_debug, 0},};
1107 static void atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
1109 int base = CU16(ctx->cmd_table + 4 + 2 * index);
1110 int len, ws, ps, ptr;
1112 atom_exec_context ectx;
1117 len = CU16(base + ATOM_CT_SIZE_PTR);
1118 ws = CU8(base + ATOM_CT_WS_PTR);
1119 ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
1120 ptr = base + ATOM_CT_CODE_PTR;
1122 SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
1125 ectx.ps_shift = ps / 4;
1129 ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
1136 if (op < ATOM_OP_NAMES_CNT)
1137 SDEBUG("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
1139 SDEBUG("[%d] @ 0x%04X\n", op, ptr - 1);
1141 if (op < ATOM_OP_CNT && op > 0)
1142 opcode_table[op].func(&ectx, &ptr,
1143 opcode_table[op].arg);
1147 if (op == ATOM_OP_EOT)
1157 void atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
1159 mutex_lock(&ctx->mutex);
1160 /* reset reg block */
1162 /* reset fb window */
1165 ctx->io_mode = ATOM_IO_MM;
1166 atom_execute_table_locked(ctx, index, params);
1167 mutex_unlock(&ctx->mutex);
1170 static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1172 static void atom_index_iio(struct atom_context *ctx, int base)
1174 ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
1175 while (CU8(base) == ATOM_IIO_START) {
1176 ctx->iio[CU8(base + 1)] = base + 2;
1178 while (CU8(base) != ATOM_IIO_END)
1179 base += atom_iio_len[CU8(base)];
1184 struct atom_context *atom_parse(struct card_info *card, void *bios)
1187 struct atom_context *ctx =
1188 kzalloc(sizeof(struct atom_context), GFP_KERNEL);
1196 if (CU16(0) != ATOM_BIOS_MAGIC) {
1197 printk(KERN_INFO "Invalid BIOS magic.\n");
1202 (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
1203 strlen(ATOM_ATI_MAGIC))) {
1204 printk(KERN_INFO "Invalid ATI magic.\n");
1209 base = CU16(ATOM_ROM_TABLE_PTR);
1211 (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
1212 strlen(ATOM_ROM_MAGIC))) {
1213 printk(KERN_INFO "Invalid ATOM magic.\n");
1218 ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
1219 ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
1220 atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
1222 str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
1223 while (*str && ((*str == '\n') || (*str == '\r')))
1225 /* name string isn't always 0 terminated */
1226 for (i = 0; i < 511; i++) {
1228 if (name[i] < '.' || name[i] > 'z') {
1233 printk(KERN_INFO "ATOM BIOS: %s\n", name);
1238 int atom_asic_init(struct atom_context *ctx)
1240 int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
1244 ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
1245 ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
1246 if (!ps[0] || !ps[1])
1249 if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
1251 atom_execute_table(ctx, ATOM_CMD_INIT, ps);
1256 void atom_destroy(struct atom_context *ctx)
1263 void atom_parse_data_header(struct atom_context *ctx, int index,
1264 uint16_t * size, uint8_t * frev, uint8_t * crev,
1265 uint16_t * data_start)
1267 int offset = index * 2 + 4;
1268 int idx = CU16(ctx->data_table + offset);
1273 *frev = CU8(idx + 2);
1275 *crev = CU8(idx + 3);
1280 void atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
1283 int offset = index * 2 + 4;
1284 int idx = CU16(ctx->cmd_table + offset);
1287 *frev = CU8(idx + 2);
1289 *crev = CU8(idx + 3);
1293 int atom_allocate_fb_scratch(struct atom_context *ctx)
1295 int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
1296 uint16_t data_offset;
1298 struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
1300 atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset);
1302 firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
1304 DRM_DEBUG("atom firmware requested %08x %dkb\n",
1305 firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware,
1306 firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb);
1308 usage_bytes = firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb * 1024;
1309 if (usage_bytes == 0)
1310 usage_bytes = 20 * 1024;
1311 /* allocate some scratch memory */
1312 ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);