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>
31 #include "atom-names.h"
32 #include "atom-bits.h"
34 #define ATOM_COND_ABOVE 0
35 #define ATOM_COND_ABOVEOREQUAL 1
36 #define ATOM_COND_ALWAYS 2
37 #define ATOM_COND_BELOW 3
38 #define ATOM_COND_BELOWOREQUAL 4
39 #define ATOM_COND_EQUAL 5
40 #define ATOM_COND_NOTEQUAL 6
42 #define ATOM_PORT_ATI 0
43 #define ATOM_PORT_PCI 1
44 #define ATOM_PORT_SYSIO 2
46 #define ATOM_UNIT_MICROSEC 0
47 #define ATOM_UNIT_MILLISEC 1
53 struct atom_context *ctx;
61 static void atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params);
62 void atom_execute_table(struct atom_context *ctx, int index, uint32_t * params);
64 static uint32_t atom_arg_mask[8] =
65 { 0xFFFFFFFF, 0xFFFF, 0xFFFF00, 0xFFFF0000, 0xFF, 0xFF00, 0xFF0000,
67 static int atom_arg_shift[8] = { 0, 0, 8, 16, 0, 8, 16, 24 };
69 static int atom_dst_to_src[8][4] = {
70 /* translate destination alignment field to the source alignment encoding */
80 static int atom_def_dst[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
82 static int debug_depth = 0;
84 static void debug_print_spaces(int n)
90 #define DEBUG(...) do if (atom_debug) { printk(KERN_DEBUG __VA_ARGS__); } while (0)
91 #define SDEBUG(...) do if (atom_debug) { printk(KERN_DEBUG); debug_print_spaces(debug_depth); printk(__VA_ARGS__); } while (0)
93 #define DEBUG(...) do { } while (0)
94 #define SDEBUG(...) do { } while (0)
97 static uint32_t atom_iio_execute(struct atom_context *ctx, int base,
98 uint32_t index, uint32_t data)
100 uint32_t temp = 0xCDCDCDCD;
107 temp = ctx->card->reg_read(ctx->card, CU16(base + 1));
111 (void)ctx->card->reg_read(ctx->card, CU16(base + 1));
112 ctx->card->reg_write(ctx->card, CU16(base + 1), temp);
117 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
123 (0xFFFFFFFF >> (32 - CU8(base + 1))) << CU8(base +
127 case ATOM_IIO_MOVE_INDEX:
129 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
132 ((index >> CU8(base + 2)) &
133 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
137 case ATOM_IIO_MOVE_DATA:
139 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
142 ((data >> CU8(base + 2)) &
143 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
147 case ATOM_IIO_MOVE_ATTR:
149 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
153 io_attr >> CU8(base + 2)) & (0xFFFFFFFF >> (32 -
164 printk(KERN_INFO "Unknown IIO opcode.\n");
169 static uint32_t atom_get_src_int(atom_exec_context *ctx, uint8_t attr,
170 int *ptr, uint32_t *saved, int print)
172 uint32_t idx, val = 0xCDCDCDCD, align, arg;
173 struct atom_context *gctx = ctx->ctx;
175 align = (attr >> 3) & 7;
181 DEBUG("REG[0x%04X]", idx);
182 idx += gctx->reg_block;
183 switch (gctx->io_mode) {
185 val = gctx->card->reg_read(gctx->card, idx);
189 "PCI registers are not implemented.\n");
193 "SYSIO registers are not implemented.\n");
196 if (!(gctx->io_mode & 0x80)) {
197 printk(KERN_INFO "Bad IO mode.\n");
200 if (!gctx->iio[gctx->io_mode & 0x7F]) {
202 "Undefined indirect IO read method %d.\n",
203 gctx->io_mode & 0x7F);
207 atom_iio_execute(gctx,
208 gctx->iio[gctx->io_mode & 0x7F],
215 val = le32_to_cpu(ctx->ps[idx]);
217 DEBUG("PS[0x%02X,0x%04X]", idx, val);
223 DEBUG("WS[0x%02X]", idx);
225 case ATOM_WS_QUOTIENT:
226 val = gctx->divmul[0];
228 case ATOM_WS_REMAINDER:
229 val = gctx->divmul[1];
231 case ATOM_WS_DATAPTR:
232 val = gctx->data_block;
237 case ATOM_WS_OR_MASK:
238 val = 1 << gctx->shift;
240 case ATOM_WS_AND_MASK:
241 val = ~(1 << gctx->shift);
243 case ATOM_WS_FB_WINDOW:
246 case ATOM_WS_ATTRIBUTES:
250 val = gctx->reg_block;
260 if (gctx->data_block)
261 DEBUG("ID[0x%04X+%04X]", idx, gctx->data_block);
263 DEBUG("ID[0x%04X]", idx);
265 val = U32(idx + gctx->data_block);
270 val = gctx->scratch[((gctx->fb_base + idx) / 4)];
272 DEBUG("FB[0x%02X]", idx);
280 DEBUG("IMM 0x%08X\n", val);
284 case ATOM_SRC_WORD16:
288 DEBUG("IMM 0x%04X\n", val);
292 case ATOM_SRC_BYTE16:
293 case ATOM_SRC_BYTE24:
297 DEBUG("IMM 0x%02X\n", val);
305 DEBUG("PLL[0x%02X]", idx);
306 val = gctx->card->pll_read(gctx->card, idx);
312 DEBUG("MC[0x%02X]", idx);
313 val = gctx->card->mc_read(gctx->card, idx);
318 val &= atom_arg_mask[align];
319 val >>= atom_arg_shift[align];
323 DEBUG(".[31:0] -> 0x%08X\n", val);
326 DEBUG(".[15:0] -> 0x%04X\n", val);
329 DEBUG(".[23:8] -> 0x%04X\n", val);
331 case ATOM_SRC_WORD16:
332 DEBUG(".[31:16] -> 0x%04X\n", val);
335 DEBUG(".[7:0] -> 0x%02X\n", val);
338 DEBUG(".[15:8] -> 0x%02X\n", val);
340 case ATOM_SRC_BYTE16:
341 DEBUG(".[23:16] -> 0x%02X\n", val);
343 case ATOM_SRC_BYTE24:
344 DEBUG(".[31:24] -> 0x%02X\n", val);
350 static void atom_skip_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr)
352 uint32_t align = (attr >> 3) & 7, arg = attr & 7;
372 case ATOM_SRC_WORD16:
377 case ATOM_SRC_BYTE16:
378 case ATOM_SRC_BYTE24:
386 static uint32_t atom_get_src(atom_exec_context *ctx, uint8_t attr, int *ptr)
388 return atom_get_src_int(ctx, attr, ptr, NULL, 1);
391 static uint32_t atom_get_src_direct(atom_exec_context *ctx, uint8_t align, int *ptr)
393 uint32_t val = 0xCDCDCDCD;
402 case ATOM_SRC_WORD16:
408 case ATOM_SRC_BYTE16:
409 case ATOM_SRC_BYTE24:
417 static uint32_t atom_get_dst(atom_exec_context *ctx, int arg, uint8_t attr,
418 int *ptr, uint32_t *saved, int print)
420 return atom_get_src_int(ctx,
421 arg | atom_dst_to_src[(attr >> 3) &
422 7][(attr >> 6) & 3] << 3,
426 static void atom_skip_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr)
428 atom_skip_src_int(ctx,
429 arg | atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) &
433 static void atom_put_dst(atom_exec_context *ctx, int arg, uint8_t attr,
434 int *ptr, uint32_t val, uint32_t saved)
437 atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3], old_val =
439 struct atom_context *gctx = ctx->ctx;
440 old_val &= atom_arg_mask[align] >> atom_arg_shift[align];
441 val <<= atom_arg_shift[align];
442 val &= atom_arg_mask[align];
443 saved &= ~atom_arg_mask[align];
449 DEBUG("REG[0x%04X]", idx);
450 idx += gctx->reg_block;
451 switch (gctx->io_mode) {
454 gctx->card->reg_write(gctx->card, idx,
457 gctx->card->reg_write(gctx->card, idx, val);
461 "PCI registers are not implemented.\n");
465 "SYSIO registers are not implemented.\n");
468 if (!(gctx->io_mode & 0x80)) {
469 printk(KERN_INFO "Bad IO mode.\n");
472 if (!gctx->iio[gctx->io_mode & 0xFF]) {
474 "Undefined indirect IO write method %d.\n",
475 gctx->io_mode & 0x7F);
478 atom_iio_execute(gctx, gctx->iio[gctx->io_mode & 0xFF],
485 DEBUG("PS[0x%02X]", idx);
486 ctx->ps[idx] = cpu_to_le32(val);
491 DEBUG("WS[0x%02X]", idx);
493 case ATOM_WS_QUOTIENT:
494 gctx->divmul[0] = val;
496 case ATOM_WS_REMAINDER:
497 gctx->divmul[1] = val;
499 case ATOM_WS_DATAPTR:
500 gctx->data_block = val;
505 case ATOM_WS_OR_MASK:
506 case ATOM_WS_AND_MASK:
508 case ATOM_WS_FB_WINDOW:
511 case ATOM_WS_ATTRIBUTES:
515 gctx->reg_block = val;
524 gctx->scratch[((gctx->fb_base + idx) / 4)] = val;
525 DEBUG("FB[0x%02X]", idx);
530 DEBUG("PLL[0x%02X]", idx);
531 gctx->card->pll_write(gctx->card, idx, val);
536 DEBUG("MC[0x%02X]", idx);
537 gctx->card->mc_write(gctx->card, idx, val);
542 DEBUG(".[31:0] <- 0x%08X\n", old_val);
545 DEBUG(".[15:0] <- 0x%04X\n", old_val);
548 DEBUG(".[23:8] <- 0x%04X\n", old_val);
550 case ATOM_SRC_WORD16:
551 DEBUG(".[31:16] <- 0x%04X\n", old_val);
554 DEBUG(".[7:0] <- 0x%02X\n", old_val);
557 DEBUG(".[15:8] <- 0x%02X\n", old_val);
559 case ATOM_SRC_BYTE16:
560 DEBUG(".[23:16] <- 0x%02X\n", old_val);
562 case ATOM_SRC_BYTE24:
563 DEBUG(".[31:24] <- 0x%02X\n", old_val);
568 static void atom_op_add(atom_exec_context *ctx, int *ptr, int arg)
570 uint8_t attr = U8((*ptr)++);
571 uint32_t dst, src, saved;
574 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
576 src = atom_get_src(ctx, attr, ptr);
579 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
582 static void atom_op_and(atom_exec_context *ctx, int *ptr, int arg)
584 uint8_t attr = U8((*ptr)++);
585 uint32_t dst, src, saved;
588 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
590 src = atom_get_src(ctx, attr, ptr);
593 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
596 static void atom_op_beep(atom_exec_context *ctx, int *ptr, int arg)
598 printk("ATOM BIOS beeped!\n");
601 static void atom_op_calltable(atom_exec_context *ctx, int *ptr, int arg)
603 int idx = U8((*ptr)++);
604 if (idx < ATOM_TABLE_NAMES_CNT)
605 SDEBUG(" table: %d (%s)\n", idx, atom_table_names[idx]);
607 SDEBUG(" table: %d\n", idx);
608 if (U16(ctx->ctx->cmd_table + 4 + 2 * idx))
609 atom_execute_table_locked(ctx->ctx, idx, ctx->ps + ctx->ps_shift);
612 static void atom_op_clear(atom_exec_context *ctx, int *ptr, int arg)
614 uint8_t attr = U8((*ptr)++);
618 attr |= atom_def_dst[attr >> 3] << 6;
619 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
621 atom_put_dst(ctx, arg, attr, &dptr, 0, saved);
624 static void atom_op_compare(atom_exec_context *ctx, int *ptr, int arg)
626 uint8_t attr = U8((*ptr)++);
629 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
631 src = atom_get_src(ctx, attr, ptr);
632 ctx->ctx->cs_equal = (dst == src);
633 ctx->ctx->cs_above = (dst > src);
634 SDEBUG(" result: %s %s\n", ctx->ctx->cs_equal ? "EQ" : "NE",
635 ctx->ctx->cs_above ? "GT" : "LE");
638 static void atom_op_delay(atom_exec_context *ctx, int *ptr, int arg)
640 uint8_t count = U8((*ptr)++);
641 SDEBUG(" count: %d\n", count);
642 if (arg == ATOM_UNIT_MICROSEC)
644 // schedule_timeout_uninterruptible(usecs_to_jiffies(count));
646 schedule_timeout_uninterruptible(msecs_to_jiffies(count));
649 static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
651 uint8_t attr = U8((*ptr)++);
654 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
656 src = atom_get_src(ctx, attr, ptr);
658 ctx->ctx->divmul[0] = dst / src;
659 ctx->ctx->divmul[1] = dst % src;
661 ctx->ctx->divmul[0] = 0;
662 ctx->ctx->divmul[1] = 0;
666 static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
668 /* functionally, a nop */
671 static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
673 int execute = 0, target = U16(*ptr);
676 case ATOM_COND_ABOVE:
677 execute = ctx->ctx->cs_above;
679 case ATOM_COND_ABOVEOREQUAL:
680 execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
682 case ATOM_COND_ALWAYS:
685 case ATOM_COND_BELOW:
686 execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
688 case ATOM_COND_BELOWOREQUAL:
689 execute = !ctx->ctx->cs_above;
691 case ATOM_COND_EQUAL:
692 execute = ctx->ctx->cs_equal;
694 case ATOM_COND_NOTEQUAL:
695 execute = !ctx->ctx->cs_equal;
698 if (arg != ATOM_COND_ALWAYS)
699 SDEBUG(" taken: %s\n", execute ? "yes" : "no");
700 SDEBUG(" target: 0x%04X\n", target);
702 *ptr = ctx->start + target;
705 static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
707 uint8_t attr = U8((*ptr)++);
708 uint32_t dst, src1, src2, saved;
711 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
713 src1 = atom_get_src_direct(ctx, ((attr >> 3) & 7), ptr);
715 src2 = atom_get_src(ctx, attr, ptr);
719 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
722 static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
724 uint8_t attr = U8((*ptr)++);
727 if (((attr >> 3) & 7) != ATOM_SRC_DWORD)
728 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
730 atom_skip_dst(ctx, arg, attr, ptr);
734 src = atom_get_src(ctx, attr, ptr);
736 atom_put_dst(ctx, arg, attr, &dptr, src, saved);
739 static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
741 uint8_t attr = U8((*ptr)++);
744 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
746 src = atom_get_src(ctx, attr, ptr);
747 ctx->ctx->divmul[0] = dst * src;
750 static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
755 static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
757 uint8_t attr = U8((*ptr)++);
758 uint32_t dst, src, saved;
761 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
763 src = atom_get_src(ctx, attr, ptr);
766 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
769 static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
771 uint8_t val = U8((*ptr)++);
772 SDEBUG("POST card output: 0x%02X\n", val);
775 static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
777 printk(KERN_INFO "unimplemented!\n");
780 static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
782 printk(KERN_INFO "unimplemented!\n");
785 static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
787 printk(KERN_INFO "unimplemented!\n");
790 static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
794 SDEBUG(" block: %d\n", idx);
796 ctx->ctx->data_block = 0;
798 ctx->ctx->data_block = ctx->start;
800 ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
801 SDEBUG(" base: 0x%04X\n", ctx->ctx->data_block);
804 static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
806 uint8_t attr = U8((*ptr)++);
807 SDEBUG(" fb_base: ");
808 ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
811 static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
817 if (port < ATOM_IO_NAMES_CNT)
818 SDEBUG(" port: %d (%s)\n", port, atom_io_names[port]);
820 SDEBUG(" port: %d\n", port);
822 ctx->ctx->io_mode = ATOM_IO_MM;
824 ctx->ctx->io_mode = ATOM_IO_IIO | port;
828 ctx->ctx->io_mode = ATOM_IO_PCI;
831 case ATOM_PORT_SYSIO:
832 ctx->ctx->io_mode = ATOM_IO_SYSIO;
838 static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
840 ctx->ctx->reg_block = U16(*ptr);
842 SDEBUG(" base: 0x%04X\n", ctx->ctx->reg_block);
845 static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
847 uint8_t attr = U8((*ptr)++), shift;
851 attr |= atom_def_dst[attr >> 3] << 6;
853 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
854 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
855 SDEBUG(" shift: %d\n", shift);
858 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
861 static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
863 uint8_t attr = U8((*ptr)++), shift;
867 attr |= atom_def_dst[attr >> 3] << 6;
869 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
870 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
871 SDEBUG(" shift: %d\n", shift);
874 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
877 static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
879 uint8_t attr = U8((*ptr)++), shift;
883 attr |= atom_def_dst[attr >> 3] << 6;
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 attr |= atom_def_dst[attr >> 3] << 6;
901 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
902 shift = atom_get_src(ctx, attr, ptr);
903 SDEBUG(" shift: %d\n", shift);
906 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
909 static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
911 uint8_t attr = U8((*ptr)++);
912 uint32_t dst, src, saved;
915 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
917 src = atom_get_src(ctx, attr, ptr);
920 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
923 static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
925 uint8_t attr = U8((*ptr)++);
926 uint32_t src, val, target;
928 src = atom_get_src(ctx, attr, ptr);
929 while (U16(*ptr) != ATOM_CASE_END)
930 if (U8(*ptr) == ATOM_CASE_MAGIC) {
934 atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
938 SDEBUG(" target: %04X\n", target);
939 *ptr = ctx->start + target;
944 printk(KERN_INFO "Bad case.\n");
950 static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
952 uint8_t attr = U8((*ptr)++);
955 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
957 src = atom_get_src(ctx, attr, ptr);
958 ctx->ctx->cs_equal = ((dst & src) == 0);
959 SDEBUG(" result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
962 static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
964 uint8_t attr = U8((*ptr)++);
965 uint32_t dst, src, saved;
968 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
970 src = atom_get_src(ctx, attr, ptr);
973 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
976 static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
978 printk(KERN_INFO "unimplemented!\n");
982 void (*func) (atom_exec_context *, int *, int);
984 } opcode_table[ATOM_OP_CNT] = {
987 atom_op_move, ATOM_ARG_REG}, {
988 atom_op_move, ATOM_ARG_PS}, {
989 atom_op_move, ATOM_ARG_WS}, {
990 atom_op_move, ATOM_ARG_FB}, {
991 atom_op_move, ATOM_ARG_PLL}, {
992 atom_op_move, ATOM_ARG_MC}, {
993 atom_op_and, ATOM_ARG_REG}, {
994 atom_op_and, ATOM_ARG_PS}, {
995 atom_op_and, ATOM_ARG_WS}, {
996 atom_op_and, ATOM_ARG_FB}, {
997 atom_op_and, ATOM_ARG_PLL}, {
998 atom_op_and, ATOM_ARG_MC}, {
999 atom_op_or, ATOM_ARG_REG}, {
1000 atom_op_or, ATOM_ARG_PS}, {
1001 atom_op_or, ATOM_ARG_WS}, {
1002 atom_op_or, ATOM_ARG_FB}, {
1003 atom_op_or, ATOM_ARG_PLL}, {
1004 atom_op_or, ATOM_ARG_MC}, {
1005 atom_op_shift_left, ATOM_ARG_REG}, {
1006 atom_op_shift_left, ATOM_ARG_PS}, {
1007 atom_op_shift_left, ATOM_ARG_WS}, {
1008 atom_op_shift_left, ATOM_ARG_FB}, {
1009 atom_op_shift_left, ATOM_ARG_PLL}, {
1010 atom_op_shift_left, ATOM_ARG_MC}, {
1011 atom_op_shift_right, ATOM_ARG_REG}, {
1012 atom_op_shift_right, ATOM_ARG_PS}, {
1013 atom_op_shift_right, ATOM_ARG_WS}, {
1014 atom_op_shift_right, ATOM_ARG_FB}, {
1015 atom_op_shift_right, ATOM_ARG_PLL}, {
1016 atom_op_shift_right, ATOM_ARG_MC}, {
1017 atom_op_mul, ATOM_ARG_REG}, {
1018 atom_op_mul, ATOM_ARG_PS}, {
1019 atom_op_mul, ATOM_ARG_WS}, {
1020 atom_op_mul, ATOM_ARG_FB}, {
1021 atom_op_mul, ATOM_ARG_PLL}, {
1022 atom_op_mul, ATOM_ARG_MC}, {
1023 atom_op_div, ATOM_ARG_REG}, {
1024 atom_op_div, ATOM_ARG_PS}, {
1025 atom_op_div, ATOM_ARG_WS}, {
1026 atom_op_div, ATOM_ARG_FB}, {
1027 atom_op_div, ATOM_ARG_PLL}, {
1028 atom_op_div, ATOM_ARG_MC}, {
1029 atom_op_add, ATOM_ARG_REG}, {
1030 atom_op_add, ATOM_ARG_PS}, {
1031 atom_op_add, ATOM_ARG_WS}, {
1032 atom_op_add, ATOM_ARG_FB}, {
1033 atom_op_add, ATOM_ARG_PLL}, {
1034 atom_op_add, ATOM_ARG_MC}, {
1035 atom_op_sub, ATOM_ARG_REG}, {
1036 atom_op_sub, ATOM_ARG_PS}, {
1037 atom_op_sub, ATOM_ARG_WS}, {
1038 atom_op_sub, ATOM_ARG_FB}, {
1039 atom_op_sub, ATOM_ARG_PLL}, {
1040 atom_op_sub, ATOM_ARG_MC}, {
1041 atom_op_setport, ATOM_PORT_ATI}, {
1042 atom_op_setport, ATOM_PORT_PCI}, {
1043 atom_op_setport, ATOM_PORT_SYSIO}, {
1044 atom_op_setregblock, 0}, {
1045 atom_op_setfbbase, 0}, {
1046 atom_op_compare, ATOM_ARG_REG}, {
1047 atom_op_compare, ATOM_ARG_PS}, {
1048 atom_op_compare, ATOM_ARG_WS}, {
1049 atom_op_compare, ATOM_ARG_FB}, {
1050 atom_op_compare, ATOM_ARG_PLL}, {
1051 atom_op_compare, ATOM_ARG_MC}, {
1052 atom_op_switch, 0}, {
1053 atom_op_jump, ATOM_COND_ALWAYS}, {
1054 atom_op_jump, ATOM_COND_EQUAL}, {
1055 atom_op_jump, ATOM_COND_BELOW}, {
1056 atom_op_jump, ATOM_COND_ABOVE}, {
1057 atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
1058 atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
1059 atom_op_jump, ATOM_COND_NOTEQUAL}, {
1060 atom_op_test, ATOM_ARG_REG}, {
1061 atom_op_test, ATOM_ARG_PS}, {
1062 atom_op_test, ATOM_ARG_WS}, {
1063 atom_op_test, ATOM_ARG_FB}, {
1064 atom_op_test, ATOM_ARG_PLL}, {
1065 atom_op_test, ATOM_ARG_MC}, {
1066 atom_op_delay, ATOM_UNIT_MILLISEC}, {
1067 atom_op_delay, ATOM_UNIT_MICROSEC}, {
1068 atom_op_calltable, 0}, {
1069 atom_op_repeat, 0}, {
1070 atom_op_clear, ATOM_ARG_REG}, {
1071 atom_op_clear, ATOM_ARG_PS}, {
1072 atom_op_clear, ATOM_ARG_WS}, {
1073 atom_op_clear, ATOM_ARG_FB}, {
1074 atom_op_clear, ATOM_ARG_PLL}, {
1075 atom_op_clear, ATOM_ARG_MC}, {
1078 atom_op_mask, ATOM_ARG_REG}, {
1079 atom_op_mask, ATOM_ARG_PS}, {
1080 atom_op_mask, ATOM_ARG_WS}, {
1081 atom_op_mask, ATOM_ARG_FB}, {
1082 atom_op_mask, ATOM_ARG_PLL}, {
1083 atom_op_mask, ATOM_ARG_MC}, {
1084 atom_op_postcard, 0}, {
1086 atom_op_savereg, 0}, {
1087 atom_op_restorereg, 0}, {
1088 atom_op_setdatablock, 0}, {
1089 atom_op_xor, ATOM_ARG_REG}, {
1090 atom_op_xor, ATOM_ARG_PS}, {
1091 atom_op_xor, ATOM_ARG_WS}, {
1092 atom_op_xor, ATOM_ARG_FB}, {
1093 atom_op_xor, ATOM_ARG_PLL}, {
1094 atom_op_xor, ATOM_ARG_MC}, {
1095 atom_op_shl, ATOM_ARG_REG}, {
1096 atom_op_shl, ATOM_ARG_PS}, {
1097 atom_op_shl, ATOM_ARG_WS}, {
1098 atom_op_shl, ATOM_ARG_FB}, {
1099 atom_op_shl, ATOM_ARG_PLL}, {
1100 atom_op_shl, ATOM_ARG_MC}, {
1101 atom_op_shr, ATOM_ARG_REG}, {
1102 atom_op_shr, ATOM_ARG_PS}, {
1103 atom_op_shr, ATOM_ARG_WS}, {
1104 atom_op_shr, ATOM_ARG_FB}, {
1105 atom_op_shr, ATOM_ARG_PLL}, {
1106 atom_op_shr, ATOM_ARG_MC}, {
1107 atom_op_debug, 0},};
1109 static void atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
1111 int base = CU16(ctx->cmd_table + 4 + 2 * index);
1112 int len, ws, ps, ptr;
1114 atom_exec_context ectx;
1119 len = CU16(base + ATOM_CT_SIZE_PTR);
1120 ws = CU8(base + ATOM_CT_WS_PTR);
1121 ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
1122 ptr = base + ATOM_CT_CODE_PTR;
1124 SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
1127 ectx.ps_shift = ps / 4;
1131 ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
1138 if (op < ATOM_OP_NAMES_CNT)
1139 SDEBUG("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
1141 SDEBUG("[%d] @ 0x%04X\n", op, ptr - 1);
1143 if (op < ATOM_OP_CNT && op > 0)
1144 opcode_table[op].func(&ectx, &ptr,
1145 opcode_table[op].arg);
1149 if (op == ATOM_OP_EOT)
1159 void atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
1161 mutex_lock(&ctx->mutex);
1162 /* reset reg block */
1164 /* reset fb window */
1167 ctx->io_mode = ATOM_IO_MM;
1168 atom_execute_table_locked(ctx, index, params);
1169 mutex_unlock(&ctx->mutex);
1172 static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1174 static void atom_index_iio(struct atom_context *ctx, int base)
1176 ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
1177 while (CU8(base) == ATOM_IIO_START) {
1178 ctx->iio[CU8(base + 1)] = base + 2;
1180 while (CU8(base) != ATOM_IIO_END)
1181 base += atom_iio_len[CU8(base)];
1186 struct atom_context *atom_parse(struct card_info *card, void *bios)
1189 struct atom_context *ctx =
1190 kzalloc(sizeof(struct atom_context), GFP_KERNEL);
1198 if (CU16(0) != ATOM_BIOS_MAGIC) {
1199 printk(KERN_INFO "Invalid BIOS magic.\n");
1204 (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
1205 strlen(ATOM_ATI_MAGIC))) {
1206 printk(KERN_INFO "Invalid ATI magic.\n");
1211 base = CU16(ATOM_ROM_TABLE_PTR);
1213 (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
1214 strlen(ATOM_ROM_MAGIC))) {
1215 printk(KERN_INFO "Invalid ATOM magic.\n");
1220 ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
1221 ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
1222 atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
1224 str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
1225 while (*str && ((*str == '\n') || (*str == '\r')))
1227 /* name string isn't always 0 terminated */
1228 for (i = 0; i < 511; i++) {
1230 if (name[i] < '.' || name[i] > 'z') {
1235 printk(KERN_INFO "ATOM BIOS: %s\n", name);
1240 int atom_asic_init(struct atom_context *ctx)
1242 int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
1246 ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
1247 ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
1248 if (!ps[0] || !ps[1])
1251 if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
1253 atom_execute_table(ctx, ATOM_CMD_INIT, ps);
1258 void atom_destroy(struct atom_context *ctx)
1265 void atom_parse_data_header(struct atom_context *ctx, int index,
1266 uint16_t * size, uint8_t * frev, uint8_t * crev,
1267 uint16_t * data_start)
1269 int offset = index * 2 + 4;
1270 int idx = CU16(ctx->data_table + offset);
1275 *frev = CU8(idx + 2);
1277 *crev = CU8(idx + 3);
1282 void atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
1285 int offset = index * 2 + 4;
1286 int idx = CU16(ctx->cmd_table + offset);
1289 *frev = CU8(idx + 2);
1291 *crev = CU8(idx + 3);
1295 int atom_allocate_fb_scratch(struct atom_context *ctx)
1297 int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
1298 uint16_t data_offset;
1300 struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
1302 atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset);
1304 firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
1306 DRM_DEBUG("atom firmware requested %08x %dkb\n",
1307 firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware,
1308 firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb);
1310 usage_bytes = firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb * 1024;
1311 if (usage_bytes == 0)
1312 usage_bytes = 20 * 1024;
1313 /* allocate some scratch memory */
1314 ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);