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 <linux/slab.h>
28 #include <asm/unaligned.h>
33 #include "atom-names.h"
34 #include "atom-bits.h"
36 #define ATOM_COND_ABOVE 0
37 #define ATOM_COND_ABOVEOREQUAL 1
38 #define ATOM_COND_ALWAYS 2
39 #define ATOM_COND_BELOW 3
40 #define ATOM_COND_BELOWOREQUAL 4
41 #define ATOM_COND_EQUAL 5
42 #define ATOM_COND_NOTEQUAL 6
44 #define ATOM_PORT_ATI 0
45 #define ATOM_PORT_PCI 1
46 #define ATOM_PORT_SYSIO 2
48 #define ATOM_UNIT_MICROSEC 0
49 #define ATOM_UNIT_MILLISEC 1
55 struct atom_context
*ctx
;
60 unsigned long last_jump_jiffies
;
65 static int atom_execute_table_locked(struct atom_context
*ctx
, int index
, uint32_t * params
);
66 int atom_execute_table(struct atom_context
*ctx
, int index
, uint32_t * params
);
68 static uint32_t atom_arg_mask
[8] =
69 { 0xFFFFFFFF, 0xFFFF, 0xFFFF00, 0xFFFF0000, 0xFF, 0xFF00, 0xFF0000,
71 static int atom_arg_shift
[8] = { 0, 0, 8, 16, 0, 8, 16, 24 };
73 static int atom_dst_to_src
[8][4] = {
74 /* translate destination alignment field to the source alignment encoding */
84 static int atom_def_dst
[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
86 static int debug_depth
= 0;
88 static void debug_print_spaces(int n
)
94 #define DEBUG(...) do if (atom_debug) { printk(KERN_DEBUG __VA_ARGS__); } while (0)
95 #define SDEBUG(...) do if (atom_debug) { printk(KERN_DEBUG); debug_print_spaces(debug_depth); printk(__VA_ARGS__); } while (0)
97 #define DEBUG(...) do { } while (0)
98 #define SDEBUG(...) do { } while (0)
101 static uint32_t atom_iio_execute(struct atom_context
*ctx
, int base
,
102 uint32_t index
, uint32_t data
)
104 uint32_t temp
= 0xCDCDCDCD;
111 temp
= ctx
->card
->ioreg_read(ctx
->card
, CU16(base
+ 1));
115 ctx
->card
->ioreg_write(ctx
->card
, CU16(base
+ 1), temp
);
120 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
126 (0xFFFFFFFF >> (32 - CU8(base
+ 1))) << CU8(base
+
130 case ATOM_IIO_MOVE_INDEX
:
132 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
135 ((index
>> CU8(base
+ 2)) &
136 (0xFFFFFFFF >> (32 - CU8(base
+ 1)))) << CU8(base
+
140 case ATOM_IIO_MOVE_DATA
:
142 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
145 ((data
>> CU8(base
+ 2)) &
146 (0xFFFFFFFF >> (32 - CU8(base
+ 1)))) << CU8(base
+
150 case ATOM_IIO_MOVE_ATTR
:
152 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
156 io_attr
>> CU8(base
+ 2)) & (0xFFFFFFFF >> (32 -
167 printk(KERN_INFO
"Unknown IIO opcode.\n");
172 static uint32_t atom_get_src_int(atom_exec_context
*ctx
, uint8_t attr
,
173 int *ptr
, uint32_t *saved
, int print
)
175 uint32_t idx
, val
= 0xCDCDCDCD, align
, arg
;
176 struct atom_context
*gctx
= ctx
->ctx
;
178 align
= (attr
>> 3) & 7;
184 DEBUG("REG[0x%04X]", idx
);
185 idx
+= gctx
->reg_block
;
186 switch (gctx
->io_mode
) {
188 val
= gctx
->card
->reg_read(gctx
->card
, idx
);
192 "PCI registers are not implemented.\n");
196 "SYSIO registers are not implemented.\n");
199 if (!(gctx
->io_mode
& 0x80)) {
200 printk(KERN_INFO
"Bad IO mode.\n");
203 if (!gctx
->iio
[gctx
->io_mode
& 0x7F]) {
205 "Undefined indirect IO read method %d.\n",
206 gctx
->io_mode
& 0x7F);
210 atom_iio_execute(gctx
,
211 gctx
->iio
[gctx
->io_mode
& 0x7F],
218 /* get_unaligned_le32 avoids unaligned accesses from atombios
219 * tables, noticed on a DEC Alpha. */
220 val
= get_unaligned_le32((u32
*)&ctx
->ps
[idx
]);
222 DEBUG("PS[0x%02X,0x%04X]", idx
, val
);
228 DEBUG("WS[0x%02X]", idx
);
230 case ATOM_WS_QUOTIENT
:
231 val
= gctx
->divmul
[0];
233 case ATOM_WS_REMAINDER
:
234 val
= gctx
->divmul
[1];
236 case ATOM_WS_DATAPTR
:
237 val
= gctx
->data_block
;
242 case ATOM_WS_OR_MASK
:
243 val
= 1 << gctx
->shift
;
245 case ATOM_WS_AND_MASK
:
246 val
= ~(1 << gctx
->shift
);
248 case ATOM_WS_FB_WINDOW
:
251 case ATOM_WS_ATTRIBUTES
:
255 val
= gctx
->reg_block
;
265 if (gctx
->data_block
)
266 DEBUG("ID[0x%04X+%04X]", idx
, gctx
->data_block
);
268 DEBUG("ID[0x%04X]", idx
);
270 val
= U32(idx
+ gctx
->data_block
);
275 val
= gctx
->scratch
[((gctx
->fb_base
+ idx
) / 4)];
277 DEBUG("FB[0x%02X]", idx
);
285 DEBUG("IMM 0x%08X\n", val
);
289 case ATOM_SRC_WORD16
:
293 DEBUG("IMM 0x%04X\n", val
);
297 case ATOM_SRC_BYTE16
:
298 case ATOM_SRC_BYTE24
:
302 DEBUG("IMM 0x%02X\n", val
);
310 DEBUG("PLL[0x%02X]", idx
);
311 val
= gctx
->card
->pll_read(gctx
->card
, idx
);
317 DEBUG("MC[0x%02X]", idx
);
318 val
= gctx
->card
->mc_read(gctx
->card
, idx
);
323 val
&= atom_arg_mask
[align
];
324 val
>>= atom_arg_shift
[align
];
328 DEBUG(".[31:0] -> 0x%08X\n", val
);
331 DEBUG(".[15:0] -> 0x%04X\n", val
);
334 DEBUG(".[23:8] -> 0x%04X\n", val
);
336 case ATOM_SRC_WORD16
:
337 DEBUG(".[31:16] -> 0x%04X\n", val
);
340 DEBUG(".[7:0] -> 0x%02X\n", val
);
343 DEBUG(".[15:8] -> 0x%02X\n", val
);
345 case ATOM_SRC_BYTE16
:
346 DEBUG(".[23:16] -> 0x%02X\n", val
);
348 case ATOM_SRC_BYTE24
:
349 DEBUG(".[31:24] -> 0x%02X\n", val
);
355 static void atom_skip_src_int(atom_exec_context
*ctx
, uint8_t attr
, int *ptr
)
357 uint32_t align
= (attr
>> 3) & 7, arg
= attr
& 7;
377 case ATOM_SRC_WORD16
:
382 case ATOM_SRC_BYTE16
:
383 case ATOM_SRC_BYTE24
:
391 static uint32_t atom_get_src(atom_exec_context
*ctx
, uint8_t attr
, int *ptr
)
393 return atom_get_src_int(ctx
, attr
, ptr
, NULL
, 1);
396 static uint32_t atom_get_src_direct(atom_exec_context
*ctx
, uint8_t align
, int *ptr
)
398 uint32_t val
= 0xCDCDCDCD;
407 case ATOM_SRC_WORD16
:
413 case ATOM_SRC_BYTE16
:
414 case ATOM_SRC_BYTE24
:
422 static uint32_t atom_get_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
,
423 int *ptr
, uint32_t *saved
, int print
)
425 return atom_get_src_int(ctx
,
426 arg
| atom_dst_to_src
[(attr
>> 3) &
427 7][(attr
>> 6) & 3] << 3,
431 static void atom_skip_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
, int *ptr
)
433 atom_skip_src_int(ctx
,
434 arg
| atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) &
438 static void atom_put_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
,
439 int *ptr
, uint32_t val
, uint32_t saved
)
442 atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) & 3], old_val
=
444 struct atom_context
*gctx
= ctx
->ctx
;
445 old_val
&= atom_arg_mask
[align
] >> atom_arg_shift
[align
];
446 val
<<= atom_arg_shift
[align
];
447 val
&= atom_arg_mask
[align
];
448 saved
&= ~atom_arg_mask
[align
];
454 DEBUG("REG[0x%04X]", idx
);
455 idx
+= gctx
->reg_block
;
456 switch (gctx
->io_mode
) {
459 gctx
->card
->reg_write(gctx
->card
, idx
,
462 gctx
->card
->reg_write(gctx
->card
, idx
, val
);
466 "PCI registers are not implemented.\n");
470 "SYSIO registers are not implemented.\n");
473 if (!(gctx
->io_mode
& 0x80)) {
474 printk(KERN_INFO
"Bad IO mode.\n");
477 if (!gctx
->iio
[gctx
->io_mode
& 0xFF]) {
479 "Undefined indirect IO write method %d.\n",
480 gctx
->io_mode
& 0x7F);
483 atom_iio_execute(gctx
, gctx
->iio
[gctx
->io_mode
& 0xFF],
490 DEBUG("PS[0x%02X]", idx
);
491 ctx
->ps
[idx
] = cpu_to_le32(val
);
496 DEBUG("WS[0x%02X]", idx
);
498 case ATOM_WS_QUOTIENT
:
499 gctx
->divmul
[0] = val
;
501 case ATOM_WS_REMAINDER
:
502 gctx
->divmul
[1] = val
;
504 case ATOM_WS_DATAPTR
:
505 gctx
->data_block
= val
;
510 case ATOM_WS_OR_MASK
:
511 case ATOM_WS_AND_MASK
:
513 case ATOM_WS_FB_WINDOW
:
516 case ATOM_WS_ATTRIBUTES
:
520 gctx
->reg_block
= val
;
529 gctx
->scratch
[((gctx
->fb_base
+ idx
) / 4)] = val
;
530 DEBUG("FB[0x%02X]", idx
);
535 DEBUG("PLL[0x%02X]", idx
);
536 gctx
->card
->pll_write(gctx
->card
, idx
, val
);
541 DEBUG("MC[0x%02X]", idx
);
542 gctx
->card
->mc_write(gctx
->card
, idx
, val
);
547 DEBUG(".[31:0] <- 0x%08X\n", old_val
);
550 DEBUG(".[15:0] <- 0x%04X\n", old_val
);
553 DEBUG(".[23:8] <- 0x%04X\n", old_val
);
555 case ATOM_SRC_WORD16
:
556 DEBUG(".[31:16] <- 0x%04X\n", old_val
);
559 DEBUG(".[7:0] <- 0x%02X\n", old_val
);
562 DEBUG(".[15:8] <- 0x%02X\n", old_val
);
564 case ATOM_SRC_BYTE16
:
565 DEBUG(".[23:16] <- 0x%02X\n", old_val
);
567 case ATOM_SRC_BYTE24
:
568 DEBUG(".[31:24] <- 0x%02X\n", old_val
);
573 static void atom_op_add(atom_exec_context
*ctx
, int *ptr
, int arg
)
575 uint8_t attr
= U8((*ptr
)++);
576 uint32_t dst
, src
, saved
;
579 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
581 src
= atom_get_src(ctx
, attr
, ptr
);
584 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
587 static void atom_op_and(atom_exec_context
*ctx
, int *ptr
, int arg
)
589 uint8_t attr
= U8((*ptr
)++);
590 uint32_t dst
, src
, saved
;
593 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
595 src
= atom_get_src(ctx
, attr
, ptr
);
598 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
601 static void atom_op_beep(atom_exec_context
*ctx
, int *ptr
, int arg
)
603 printk("ATOM BIOS beeped!\n");
606 static void atom_op_calltable(atom_exec_context
*ctx
, int *ptr
, int arg
)
608 int idx
= U8((*ptr
)++);
611 if (idx
< ATOM_TABLE_NAMES_CNT
)
612 SDEBUG(" table: %d (%s)\n", idx
, atom_table_names
[idx
]);
614 SDEBUG(" table: %d\n", idx
);
615 if (U16(ctx
->ctx
->cmd_table
+ 4 + 2 * idx
))
616 r
= atom_execute_table_locked(ctx
->ctx
, idx
, ctx
->ps
+ ctx
->ps_shift
);
622 static void atom_op_clear(atom_exec_context
*ctx
, int *ptr
, int arg
)
624 uint8_t attr
= U8((*ptr
)++);
628 attr
|= atom_def_dst
[attr
>> 3] << 6;
629 atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 0);
631 atom_put_dst(ctx
, arg
, attr
, &dptr
, 0, saved
);
634 static void atom_op_compare(atom_exec_context
*ctx
, int *ptr
, int arg
)
636 uint8_t attr
= U8((*ptr
)++);
639 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
641 src
= atom_get_src(ctx
, attr
, ptr
);
642 ctx
->ctx
->cs_equal
= (dst
== src
);
643 ctx
->ctx
->cs_above
= (dst
> src
);
644 SDEBUG(" result: %s %s\n", ctx
->ctx
->cs_equal
? "EQ" : "NE",
645 ctx
->ctx
->cs_above
? "GT" : "LE");
648 static void atom_op_delay(atom_exec_context
*ctx
, int *ptr
, int arg
)
650 uint8_t count
= U8((*ptr
)++);
651 SDEBUG(" count: %d\n", count
);
652 if (arg
== ATOM_UNIT_MICROSEC
)
655 schedule_timeout_uninterruptible(msecs_to_jiffies(count
));
658 static void atom_op_div(atom_exec_context
*ctx
, int *ptr
, int arg
)
660 uint8_t attr
= U8((*ptr
)++);
663 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
665 src
= atom_get_src(ctx
, attr
, ptr
);
667 ctx
->ctx
->divmul
[0] = dst
/ src
;
668 ctx
->ctx
->divmul
[1] = dst
% src
;
670 ctx
->ctx
->divmul
[0] = 0;
671 ctx
->ctx
->divmul
[1] = 0;
675 static void atom_op_eot(atom_exec_context
*ctx
, int *ptr
, int arg
)
677 /* functionally, a nop */
680 static void atom_op_jump(atom_exec_context
*ctx
, int *ptr
, int arg
)
682 int execute
= 0, target
= U16(*ptr
);
683 unsigned long cjiffies
;
687 case ATOM_COND_ABOVE
:
688 execute
= ctx
->ctx
->cs_above
;
690 case ATOM_COND_ABOVEOREQUAL
:
691 execute
= ctx
->ctx
->cs_above
|| ctx
->ctx
->cs_equal
;
693 case ATOM_COND_ALWAYS
:
696 case ATOM_COND_BELOW
:
697 execute
= !(ctx
->ctx
->cs_above
|| ctx
->ctx
->cs_equal
);
699 case ATOM_COND_BELOWOREQUAL
:
700 execute
= !ctx
->ctx
->cs_above
;
702 case ATOM_COND_EQUAL
:
703 execute
= ctx
->ctx
->cs_equal
;
705 case ATOM_COND_NOTEQUAL
:
706 execute
= !ctx
->ctx
->cs_equal
;
709 if (arg
!= ATOM_COND_ALWAYS
)
710 SDEBUG(" taken: %s\n", execute
? "yes" : "no");
711 SDEBUG(" target: 0x%04X\n", target
);
713 if (ctx
->last_jump
== (ctx
->start
+ target
)) {
715 if (time_after(cjiffies
, ctx
->last_jump_jiffies
)) {
716 cjiffies
-= ctx
->last_jump_jiffies
;
717 if ((jiffies_to_msecs(cjiffies
) > 5000)) {
718 DRM_ERROR("atombios stuck in loop for more than 5secs aborting\n");
722 /* jiffies wrap around we will just wait a little longer */
723 ctx
->last_jump_jiffies
= jiffies
;
726 ctx
->last_jump
= ctx
->start
+ target
;
727 ctx
->last_jump_jiffies
= jiffies
;
729 *ptr
= ctx
->start
+ target
;
733 static void atom_op_mask(atom_exec_context
*ctx
, int *ptr
, int arg
)
735 uint8_t attr
= U8((*ptr
)++);
736 uint32_t dst
, src1
, src2
, saved
;
739 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
741 src1
= atom_get_src_direct(ctx
, ((attr
>> 3) & 7), ptr
);
743 src2
= atom_get_src(ctx
, attr
, ptr
);
747 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
750 static void atom_op_move(atom_exec_context
*ctx
, int *ptr
, int arg
)
752 uint8_t attr
= U8((*ptr
)++);
755 if (((attr
>> 3) & 7) != ATOM_SRC_DWORD
)
756 atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 0);
758 atom_skip_dst(ctx
, arg
, attr
, ptr
);
762 src
= atom_get_src(ctx
, attr
, ptr
);
764 atom_put_dst(ctx
, arg
, attr
, &dptr
, src
, saved
);
767 static void atom_op_mul(atom_exec_context
*ctx
, int *ptr
, int arg
)
769 uint8_t attr
= U8((*ptr
)++);
772 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
774 src
= atom_get_src(ctx
, attr
, ptr
);
775 ctx
->ctx
->divmul
[0] = dst
* src
;
778 static void atom_op_nop(atom_exec_context
*ctx
, int *ptr
, int arg
)
783 static void atom_op_or(atom_exec_context
*ctx
, int *ptr
, int arg
)
785 uint8_t attr
= U8((*ptr
)++);
786 uint32_t dst
, src
, saved
;
789 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
791 src
= atom_get_src(ctx
, attr
, ptr
);
794 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
797 static void atom_op_postcard(atom_exec_context
*ctx
, int *ptr
, int arg
)
799 uint8_t val
= U8((*ptr
)++);
800 SDEBUG("POST card output: 0x%02X\n", val
);
803 static void atom_op_repeat(atom_exec_context
*ctx
, int *ptr
, int arg
)
805 printk(KERN_INFO
"unimplemented!\n");
808 static void atom_op_restorereg(atom_exec_context
*ctx
, int *ptr
, int arg
)
810 printk(KERN_INFO
"unimplemented!\n");
813 static void atom_op_savereg(atom_exec_context
*ctx
, int *ptr
, int arg
)
815 printk(KERN_INFO
"unimplemented!\n");
818 static void atom_op_setdatablock(atom_exec_context
*ctx
, int *ptr
, int arg
)
822 SDEBUG(" block: %d\n", idx
);
824 ctx
->ctx
->data_block
= 0;
826 ctx
->ctx
->data_block
= ctx
->start
;
828 ctx
->ctx
->data_block
= U16(ctx
->ctx
->data_table
+ 4 + 2 * idx
);
829 SDEBUG(" base: 0x%04X\n", ctx
->ctx
->data_block
);
832 static void atom_op_setfbbase(atom_exec_context
*ctx
, int *ptr
, int arg
)
834 uint8_t attr
= U8((*ptr
)++);
835 SDEBUG(" fb_base: ");
836 ctx
->ctx
->fb_base
= atom_get_src(ctx
, attr
, ptr
);
839 static void atom_op_setport(atom_exec_context
*ctx
, int *ptr
, int arg
)
845 if (port
< ATOM_IO_NAMES_CNT
)
846 SDEBUG(" port: %d (%s)\n", port
, atom_io_names
[port
]);
848 SDEBUG(" port: %d\n", port
);
850 ctx
->ctx
->io_mode
= ATOM_IO_MM
;
852 ctx
->ctx
->io_mode
= ATOM_IO_IIO
| port
;
856 ctx
->ctx
->io_mode
= ATOM_IO_PCI
;
859 case ATOM_PORT_SYSIO
:
860 ctx
->ctx
->io_mode
= ATOM_IO_SYSIO
;
866 static void atom_op_setregblock(atom_exec_context
*ctx
, int *ptr
, int arg
)
868 ctx
->ctx
->reg_block
= U16(*ptr
);
870 SDEBUG(" base: 0x%04X\n", ctx
->ctx
->reg_block
);
873 static void atom_op_shift_left(atom_exec_context
*ctx
, int *ptr
, int arg
)
875 uint8_t attr
= U8((*ptr
)++), shift
;
879 attr
|= atom_def_dst
[attr
>> 3] << 6;
881 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
882 shift
= atom_get_src_direct(ctx
, ATOM_SRC_BYTE0
, ptr
);
883 SDEBUG(" shift: %d\n", shift
);
886 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
889 static void atom_op_shift_right(atom_exec_context
*ctx
, int *ptr
, int arg
)
891 uint8_t attr
= U8((*ptr
)++), shift
;
895 attr
|= atom_def_dst
[attr
>> 3] << 6;
897 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
898 shift
= atom_get_src_direct(ctx
, ATOM_SRC_BYTE0
, ptr
);
899 SDEBUG(" shift: %d\n", shift
);
902 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
905 static void atom_op_shl(atom_exec_context
*ctx
, int *ptr
, int arg
)
907 uint8_t attr
= U8((*ptr
)++), shift
;
910 uint32_t dst_align
= atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) & 3];
912 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
913 /* op needs to full dst value */
915 shift
= atom_get_src(ctx
, attr
, ptr
);
916 SDEBUG(" shift: %d\n", shift
);
918 dst
&= atom_arg_mask
[dst_align
];
919 dst
>>= atom_arg_shift
[dst_align
];
921 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
924 static void atom_op_shr(atom_exec_context
*ctx
, int *ptr
, int arg
)
926 uint8_t attr
= U8((*ptr
)++), shift
;
929 uint32_t dst_align
= atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) & 3];
931 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
932 /* op needs to full dst value */
934 shift
= atom_get_src(ctx
, attr
, ptr
);
935 SDEBUG(" shift: %d\n", shift
);
937 dst
&= atom_arg_mask
[dst_align
];
938 dst
>>= atom_arg_shift
[dst_align
];
940 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
943 static void atom_op_sub(atom_exec_context
*ctx
, int *ptr
, int arg
)
945 uint8_t attr
= U8((*ptr
)++);
946 uint32_t dst
, src
, saved
;
949 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
951 src
= atom_get_src(ctx
, attr
, ptr
);
954 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
957 static void atom_op_switch(atom_exec_context
*ctx
, int *ptr
, int arg
)
959 uint8_t attr
= U8((*ptr
)++);
960 uint32_t src
, val
, target
;
962 src
= atom_get_src(ctx
, attr
, ptr
);
963 while (U16(*ptr
) != ATOM_CASE_END
)
964 if (U8(*ptr
) == ATOM_CASE_MAGIC
) {
968 atom_get_src(ctx
, (attr
& 0x38) | ATOM_ARG_IMM
,
972 SDEBUG(" target: %04X\n", target
);
973 *ptr
= ctx
->start
+ target
;
978 printk(KERN_INFO
"Bad case.\n");
984 static void atom_op_test(atom_exec_context
*ctx
, int *ptr
, int arg
)
986 uint8_t attr
= U8((*ptr
)++);
989 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
991 src
= atom_get_src(ctx
, attr
, ptr
);
992 ctx
->ctx
->cs_equal
= ((dst
& src
) == 0);
993 SDEBUG(" result: %s\n", ctx
->ctx
->cs_equal
? "EQ" : "NE");
996 static void atom_op_xor(atom_exec_context
*ctx
, int *ptr
, int arg
)
998 uint8_t attr
= U8((*ptr
)++);
999 uint32_t dst
, src
, saved
;
1002 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
1004 src
= atom_get_src(ctx
, attr
, ptr
);
1007 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
1010 static void atom_op_debug(atom_exec_context
*ctx
, int *ptr
, int arg
)
1012 printk(KERN_INFO
"unimplemented!\n");
1016 void (*func
) (atom_exec_context
*, int *, int);
1018 } opcode_table
[ATOM_OP_CNT
] = {
1021 atom_op_move
, ATOM_ARG_REG
}, {
1022 atom_op_move
, ATOM_ARG_PS
}, {
1023 atom_op_move
, ATOM_ARG_WS
}, {
1024 atom_op_move
, ATOM_ARG_FB
}, {
1025 atom_op_move
, ATOM_ARG_PLL
}, {
1026 atom_op_move
, ATOM_ARG_MC
}, {
1027 atom_op_and
, ATOM_ARG_REG
}, {
1028 atom_op_and
, ATOM_ARG_PS
}, {
1029 atom_op_and
, ATOM_ARG_WS
}, {
1030 atom_op_and
, ATOM_ARG_FB
}, {
1031 atom_op_and
, ATOM_ARG_PLL
}, {
1032 atom_op_and
, ATOM_ARG_MC
}, {
1033 atom_op_or
, ATOM_ARG_REG
}, {
1034 atom_op_or
, ATOM_ARG_PS
}, {
1035 atom_op_or
, ATOM_ARG_WS
}, {
1036 atom_op_or
, ATOM_ARG_FB
}, {
1037 atom_op_or
, ATOM_ARG_PLL
}, {
1038 atom_op_or
, ATOM_ARG_MC
}, {
1039 atom_op_shift_left
, ATOM_ARG_REG
}, {
1040 atom_op_shift_left
, ATOM_ARG_PS
}, {
1041 atom_op_shift_left
, ATOM_ARG_WS
}, {
1042 atom_op_shift_left
, ATOM_ARG_FB
}, {
1043 atom_op_shift_left
, ATOM_ARG_PLL
}, {
1044 atom_op_shift_left
, ATOM_ARG_MC
}, {
1045 atom_op_shift_right
, ATOM_ARG_REG
}, {
1046 atom_op_shift_right
, ATOM_ARG_PS
}, {
1047 atom_op_shift_right
, ATOM_ARG_WS
}, {
1048 atom_op_shift_right
, ATOM_ARG_FB
}, {
1049 atom_op_shift_right
, ATOM_ARG_PLL
}, {
1050 atom_op_shift_right
, ATOM_ARG_MC
}, {
1051 atom_op_mul
, ATOM_ARG_REG
}, {
1052 atom_op_mul
, ATOM_ARG_PS
}, {
1053 atom_op_mul
, ATOM_ARG_WS
}, {
1054 atom_op_mul
, ATOM_ARG_FB
}, {
1055 atom_op_mul
, ATOM_ARG_PLL
}, {
1056 atom_op_mul
, ATOM_ARG_MC
}, {
1057 atom_op_div
, ATOM_ARG_REG
}, {
1058 atom_op_div
, ATOM_ARG_PS
}, {
1059 atom_op_div
, ATOM_ARG_WS
}, {
1060 atom_op_div
, ATOM_ARG_FB
}, {
1061 atom_op_div
, ATOM_ARG_PLL
}, {
1062 atom_op_div
, ATOM_ARG_MC
}, {
1063 atom_op_add
, ATOM_ARG_REG
}, {
1064 atom_op_add
, ATOM_ARG_PS
}, {
1065 atom_op_add
, ATOM_ARG_WS
}, {
1066 atom_op_add
, ATOM_ARG_FB
}, {
1067 atom_op_add
, ATOM_ARG_PLL
}, {
1068 atom_op_add
, ATOM_ARG_MC
}, {
1069 atom_op_sub
, ATOM_ARG_REG
}, {
1070 atom_op_sub
, ATOM_ARG_PS
}, {
1071 atom_op_sub
, ATOM_ARG_WS
}, {
1072 atom_op_sub
, ATOM_ARG_FB
}, {
1073 atom_op_sub
, ATOM_ARG_PLL
}, {
1074 atom_op_sub
, ATOM_ARG_MC
}, {
1075 atom_op_setport
, ATOM_PORT_ATI
}, {
1076 atom_op_setport
, ATOM_PORT_PCI
}, {
1077 atom_op_setport
, ATOM_PORT_SYSIO
}, {
1078 atom_op_setregblock
, 0}, {
1079 atom_op_setfbbase
, 0}, {
1080 atom_op_compare
, ATOM_ARG_REG
}, {
1081 atom_op_compare
, ATOM_ARG_PS
}, {
1082 atom_op_compare
, ATOM_ARG_WS
}, {
1083 atom_op_compare
, ATOM_ARG_FB
}, {
1084 atom_op_compare
, ATOM_ARG_PLL
}, {
1085 atom_op_compare
, ATOM_ARG_MC
}, {
1086 atom_op_switch
, 0}, {
1087 atom_op_jump
, ATOM_COND_ALWAYS
}, {
1088 atom_op_jump
, ATOM_COND_EQUAL
}, {
1089 atom_op_jump
, ATOM_COND_BELOW
}, {
1090 atom_op_jump
, ATOM_COND_ABOVE
}, {
1091 atom_op_jump
, ATOM_COND_BELOWOREQUAL
}, {
1092 atom_op_jump
, ATOM_COND_ABOVEOREQUAL
}, {
1093 atom_op_jump
, ATOM_COND_NOTEQUAL
}, {
1094 atom_op_test
, ATOM_ARG_REG
}, {
1095 atom_op_test
, ATOM_ARG_PS
}, {
1096 atom_op_test
, ATOM_ARG_WS
}, {
1097 atom_op_test
, ATOM_ARG_FB
}, {
1098 atom_op_test
, ATOM_ARG_PLL
}, {
1099 atom_op_test
, ATOM_ARG_MC
}, {
1100 atom_op_delay
, ATOM_UNIT_MILLISEC
}, {
1101 atom_op_delay
, ATOM_UNIT_MICROSEC
}, {
1102 atom_op_calltable
, 0}, {
1103 atom_op_repeat
, 0}, {
1104 atom_op_clear
, ATOM_ARG_REG
}, {
1105 atom_op_clear
, ATOM_ARG_PS
}, {
1106 atom_op_clear
, ATOM_ARG_WS
}, {
1107 atom_op_clear
, ATOM_ARG_FB
}, {
1108 atom_op_clear
, ATOM_ARG_PLL
}, {
1109 atom_op_clear
, ATOM_ARG_MC
}, {
1112 atom_op_mask
, ATOM_ARG_REG
}, {
1113 atom_op_mask
, ATOM_ARG_PS
}, {
1114 atom_op_mask
, ATOM_ARG_WS
}, {
1115 atom_op_mask
, ATOM_ARG_FB
}, {
1116 atom_op_mask
, ATOM_ARG_PLL
}, {
1117 atom_op_mask
, ATOM_ARG_MC
}, {
1118 atom_op_postcard
, 0}, {
1120 atom_op_savereg
, 0}, {
1121 atom_op_restorereg
, 0}, {
1122 atom_op_setdatablock
, 0}, {
1123 atom_op_xor
, ATOM_ARG_REG
}, {
1124 atom_op_xor
, ATOM_ARG_PS
}, {
1125 atom_op_xor
, ATOM_ARG_WS
}, {
1126 atom_op_xor
, ATOM_ARG_FB
}, {
1127 atom_op_xor
, ATOM_ARG_PLL
}, {
1128 atom_op_xor
, ATOM_ARG_MC
}, {
1129 atom_op_shl
, ATOM_ARG_REG
}, {
1130 atom_op_shl
, ATOM_ARG_PS
}, {
1131 atom_op_shl
, ATOM_ARG_WS
}, {
1132 atom_op_shl
, ATOM_ARG_FB
}, {
1133 atom_op_shl
, ATOM_ARG_PLL
}, {
1134 atom_op_shl
, ATOM_ARG_MC
}, {
1135 atom_op_shr
, ATOM_ARG_REG
}, {
1136 atom_op_shr
, ATOM_ARG_PS
}, {
1137 atom_op_shr
, ATOM_ARG_WS
}, {
1138 atom_op_shr
, ATOM_ARG_FB
}, {
1139 atom_op_shr
, ATOM_ARG_PLL
}, {
1140 atom_op_shr
, ATOM_ARG_MC
}, {
1141 atom_op_debug
, 0},};
1143 static int atom_execute_table_locked(struct atom_context
*ctx
, int index
, uint32_t * params
)
1145 int base
= CU16(ctx
->cmd_table
+ 4 + 2 * index
);
1146 int len
, ws
, ps
, ptr
;
1148 atom_exec_context ectx
;
1154 len
= CU16(base
+ ATOM_CT_SIZE_PTR
);
1155 ws
= CU8(base
+ ATOM_CT_WS_PTR
);
1156 ps
= CU8(base
+ ATOM_CT_PS_PTR
) & ATOM_CT_PS_MASK
;
1157 ptr
= base
+ ATOM_CT_CODE_PTR
;
1159 SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base
, len
, ws
, ps
);
1162 ectx
.ps_shift
= ps
/ 4;
1168 ectx
.ws
= kzalloc(4 * ws
, GFP_KERNEL
);
1175 if (op
< ATOM_OP_NAMES_CNT
)
1176 SDEBUG("%s @ 0x%04X\n", atom_op_names
[op
], ptr
- 1);
1178 SDEBUG("[%d] @ 0x%04X\n", op
, ptr
- 1);
1180 DRM_ERROR("atombios stuck executing %04X (len %d, WS %d, PS %d) @ 0x%04X\n",
1181 base
, len
, ws
, ps
, ptr
- 1);
1186 if (op
< ATOM_OP_CNT
&& op
> 0)
1187 opcode_table
[op
].func(&ectx
, &ptr
,
1188 opcode_table
[op
].arg
);
1192 if (op
== ATOM_OP_EOT
)
1204 int atom_execute_table(struct atom_context
*ctx
, int index
, uint32_t * params
)
1208 mutex_lock(&ctx
->mutex
);
1209 /* reset reg block */
1211 /* reset fb window */
1214 ctx
->io_mode
= ATOM_IO_MM
;
1215 r
= atom_execute_table_locked(ctx
, index
, params
);
1216 mutex_unlock(&ctx
->mutex
);
1220 static int atom_iio_len
[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1222 static void atom_index_iio(struct atom_context
*ctx
, int base
)
1224 ctx
->iio
= kzalloc(2 * 256, GFP_KERNEL
);
1225 while (CU8(base
) == ATOM_IIO_START
) {
1226 ctx
->iio
[CU8(base
+ 1)] = base
+ 2;
1228 while (CU8(base
) != ATOM_IIO_END
)
1229 base
+= atom_iio_len
[CU8(base
)];
1234 struct atom_context
*atom_parse(struct card_info
*card
, void *bios
)
1237 struct atom_context
*ctx
=
1238 kzalloc(sizeof(struct atom_context
), GFP_KERNEL
);
1246 if (CU16(0) != ATOM_BIOS_MAGIC
) {
1247 printk(KERN_INFO
"Invalid BIOS magic.\n");
1252 (CSTR(ATOM_ATI_MAGIC_PTR
), ATOM_ATI_MAGIC
,
1253 strlen(ATOM_ATI_MAGIC
))) {
1254 printk(KERN_INFO
"Invalid ATI magic.\n");
1259 base
= CU16(ATOM_ROM_TABLE_PTR
);
1261 (CSTR(base
+ ATOM_ROM_MAGIC_PTR
), ATOM_ROM_MAGIC
,
1262 strlen(ATOM_ROM_MAGIC
))) {
1263 printk(KERN_INFO
"Invalid ATOM magic.\n");
1268 ctx
->cmd_table
= CU16(base
+ ATOM_ROM_CMD_PTR
);
1269 ctx
->data_table
= CU16(base
+ ATOM_ROM_DATA_PTR
);
1270 atom_index_iio(ctx
, CU16(ctx
->data_table
+ ATOM_DATA_IIO_PTR
) + 4);
1272 str
= CSTR(CU16(base
+ ATOM_ROM_MSG_PTR
));
1273 while (*str
&& ((*str
== '\n') || (*str
== '\r')))
1275 /* name string isn't always 0 terminated */
1276 for (i
= 0; i
< 511; i
++) {
1278 if (name
[i
] < '.' || name
[i
] > 'z') {
1283 printk(KERN_INFO
"ATOM BIOS: %s\n", name
);
1288 int atom_asic_init(struct atom_context
*ctx
)
1290 int hwi
= CU16(ctx
->data_table
+ ATOM_DATA_FWI_PTR
);
1294 ps
[0] = cpu_to_le32(CU32(hwi
+ ATOM_FWI_DEFSCLK_PTR
));
1295 ps
[1] = cpu_to_le32(CU32(hwi
+ ATOM_FWI_DEFMCLK_PTR
));
1296 if (!ps
[0] || !ps
[1])
1299 if (!CU16(ctx
->cmd_table
+ 4 + 2 * ATOM_CMD_INIT
))
1301 return atom_execute_table(ctx
, ATOM_CMD_INIT
, ps
);
1304 void atom_destroy(struct atom_context
*ctx
)
1311 bool atom_parse_data_header(struct atom_context
*ctx
, int index
,
1312 uint16_t * size
, uint8_t * frev
, uint8_t * crev
,
1313 uint16_t * data_start
)
1315 int offset
= index
* 2 + 4;
1316 int idx
= CU16(ctx
->data_table
+ offset
);
1317 u16
*mdt
= (u16
*)(ctx
->bios
+ ctx
->data_table
+ 4);
1325 *frev
= CU8(idx
+ 2);
1327 *crev
= CU8(idx
+ 3);
1332 bool atom_parse_cmd_header(struct atom_context
*ctx
, int index
, uint8_t * frev
,
1335 int offset
= index
* 2 + 4;
1336 int idx
= CU16(ctx
->cmd_table
+ offset
);
1337 u16
*mct
= (u16
*)(ctx
->bios
+ ctx
->cmd_table
+ 4);
1343 *frev
= CU8(idx
+ 2);
1345 *crev
= CU8(idx
+ 3);
1349 int atom_allocate_fb_scratch(struct atom_context
*ctx
)
1351 int index
= GetIndexIntoMasterTable(DATA
, VRAM_UsageByFirmware
);
1352 uint16_t data_offset
;
1353 int usage_bytes
= 0;
1354 struct _ATOM_VRAM_USAGE_BY_FIRMWARE
*firmware_usage
;
1356 if (atom_parse_data_header(ctx
, index
, NULL
, NULL
, NULL
, &data_offset
)) {
1357 firmware_usage
= (struct _ATOM_VRAM_USAGE_BY_FIRMWARE
*)(ctx
->bios
+ data_offset
);
1359 DRM_DEBUG("atom firmware requested %08x %dkb\n",
1360 firmware_usage
->asFirmwareVramReserveInfo
[0].ulStartAddrUsedByFirmware
,
1361 firmware_usage
->asFirmwareVramReserveInfo
[0].usFirmwareUseInKb
);
1363 usage_bytes
= firmware_usage
->asFirmwareVramReserveInfo
[0].usFirmwareUseInKb
* 1024;
1365 if (usage_bytes
== 0)
1366 usage_bytes
= 20 * 1024;
1367 /* allocate some scratch memory */
1368 ctx
->scratch
= kzalloc(usage_bytes
, GFP_KERNEL
);