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
:
257 if (gctx
->data_block
)
258 DEBUG("ID[0x%04X+%04X]", idx
, gctx
->data_block
);
260 DEBUG("ID[0x%04X]", idx
);
262 val
= U32(idx
+ gctx
->data_block
);
267 val
= gctx
->scratch
[((gctx
->fb_base
+ idx
) / 4)];
269 DEBUG("FB[0x%02X]", idx
);
277 DEBUG("IMM 0x%08X\n", val
);
281 case ATOM_SRC_WORD16
:
285 DEBUG("IMM 0x%04X\n", val
);
289 case ATOM_SRC_BYTE16
:
290 case ATOM_SRC_BYTE24
:
294 DEBUG("IMM 0x%02X\n", val
);
302 DEBUG("PLL[0x%02X]", idx
);
303 val
= gctx
->card
->pll_read(gctx
->card
, idx
);
309 DEBUG("MC[0x%02X]", idx
);
310 val
= gctx
->card
->mc_read(gctx
->card
, idx
);
315 val
&= atom_arg_mask
[align
];
316 val
>>= atom_arg_shift
[align
];
320 DEBUG(".[31:0] -> 0x%08X\n", val
);
323 DEBUG(".[15:0] -> 0x%04X\n", val
);
326 DEBUG(".[23:8] -> 0x%04X\n", val
);
328 case ATOM_SRC_WORD16
:
329 DEBUG(".[31:16] -> 0x%04X\n", val
);
332 DEBUG(".[7:0] -> 0x%02X\n", val
);
335 DEBUG(".[15:8] -> 0x%02X\n", val
);
337 case ATOM_SRC_BYTE16
:
338 DEBUG(".[23:16] -> 0x%02X\n", val
);
340 case ATOM_SRC_BYTE24
:
341 DEBUG(".[31:24] -> 0x%02X\n", val
);
347 static void atom_skip_src_int(atom_exec_context
*ctx
, uint8_t attr
, int *ptr
)
349 uint32_t align
= (attr
>> 3) & 7, arg
= attr
& 7;
369 case ATOM_SRC_WORD16
:
374 case ATOM_SRC_BYTE16
:
375 case ATOM_SRC_BYTE24
:
383 static uint32_t atom_get_src(atom_exec_context
*ctx
, uint8_t attr
, int *ptr
)
385 return atom_get_src_int(ctx
, attr
, ptr
, NULL
, 1);
388 static uint32_t atom_get_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
,
389 int *ptr
, uint32_t *saved
, int print
)
391 return atom_get_src_int(ctx
,
392 arg
| atom_dst_to_src
[(attr
>> 3) &
393 7][(attr
>> 6) & 3] << 3,
397 static void atom_skip_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
, int *ptr
)
399 atom_skip_src_int(ctx
,
400 arg
| atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) &
404 static void atom_put_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
,
405 int *ptr
, uint32_t val
, uint32_t saved
)
408 atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) & 3], old_val
=
410 struct atom_context
*gctx
= ctx
->ctx
;
411 old_val
&= atom_arg_mask
[align
] >> atom_arg_shift
[align
];
412 val
<<= atom_arg_shift
[align
];
413 val
&= atom_arg_mask
[align
];
414 saved
&= ~atom_arg_mask
[align
];
420 DEBUG("REG[0x%04X]", idx
);
421 idx
+= gctx
->reg_block
;
422 switch (gctx
->io_mode
) {
425 gctx
->card
->reg_write(gctx
->card
, idx
,
428 gctx
->card
->reg_write(gctx
->card
, idx
, val
);
432 "PCI registers are not implemented.\n");
436 "SYSIO registers are not implemented.\n");
439 if (!(gctx
->io_mode
& 0x80)) {
440 printk(KERN_INFO
"Bad IO mode.\n");
443 if (!gctx
->iio
[gctx
->io_mode
& 0xFF]) {
445 "Undefined indirect IO write method %d.\n",
446 gctx
->io_mode
& 0x7F);
449 atom_iio_execute(gctx
, gctx
->iio
[gctx
->io_mode
& 0xFF],
456 DEBUG("PS[0x%02X]", idx
);
457 ctx
->ps
[idx
] = cpu_to_le32(val
);
462 DEBUG("WS[0x%02X]", idx
);
464 case ATOM_WS_QUOTIENT
:
465 gctx
->divmul
[0] = val
;
467 case ATOM_WS_REMAINDER
:
468 gctx
->divmul
[1] = val
;
470 case ATOM_WS_DATAPTR
:
471 gctx
->data_block
= val
;
476 case ATOM_WS_OR_MASK
:
477 case ATOM_WS_AND_MASK
:
479 case ATOM_WS_FB_WINDOW
:
482 case ATOM_WS_ATTRIBUTES
:
492 gctx
->scratch
[((gctx
->fb_base
+ idx
) / 4)] = val
;
493 DEBUG("FB[0x%02X]", idx
);
498 DEBUG("PLL[0x%02X]", idx
);
499 gctx
->card
->pll_write(gctx
->card
, idx
, val
);
504 DEBUG("MC[0x%02X]", idx
);
505 gctx
->card
->mc_write(gctx
->card
, idx
, val
);
510 DEBUG(".[31:0] <- 0x%08X\n", old_val
);
513 DEBUG(".[15:0] <- 0x%04X\n", old_val
);
516 DEBUG(".[23:8] <- 0x%04X\n", old_val
);
518 case ATOM_SRC_WORD16
:
519 DEBUG(".[31:16] <- 0x%04X\n", old_val
);
522 DEBUG(".[7:0] <- 0x%02X\n", old_val
);
525 DEBUG(".[15:8] <- 0x%02X\n", old_val
);
527 case ATOM_SRC_BYTE16
:
528 DEBUG(".[23:16] <- 0x%02X\n", old_val
);
530 case ATOM_SRC_BYTE24
:
531 DEBUG(".[31:24] <- 0x%02X\n", old_val
);
536 static void atom_op_add(atom_exec_context
*ctx
, int *ptr
, int arg
)
538 uint8_t attr
= U8((*ptr
)++);
539 uint32_t dst
, src
, saved
;
542 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
544 src
= atom_get_src(ctx
, attr
, ptr
);
547 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
550 static void atom_op_and(atom_exec_context
*ctx
, int *ptr
, int arg
)
552 uint8_t attr
= U8((*ptr
)++);
553 uint32_t dst
, src
, saved
;
556 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
558 src
= atom_get_src(ctx
, attr
, ptr
);
561 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
564 static void atom_op_beep(atom_exec_context
*ctx
, int *ptr
, int arg
)
566 printk("ATOM BIOS beeped!\n");
569 static void atom_op_calltable(atom_exec_context
*ctx
, int *ptr
, int arg
)
571 int idx
= U8((*ptr
)++);
572 if (idx
< ATOM_TABLE_NAMES_CNT
)
573 SDEBUG(" table: %d (%s)\n", idx
, atom_table_names
[idx
]);
575 SDEBUG(" table: %d\n", idx
);
576 if (U16(ctx
->ctx
->cmd_table
+ 4 + 2 * idx
))
577 atom_execute_table_locked(ctx
->ctx
, idx
, ctx
->ps
+ ctx
->ps_shift
);
580 static void atom_op_clear(atom_exec_context
*ctx
, int *ptr
, int arg
)
582 uint8_t attr
= U8((*ptr
)++);
586 attr
|= atom_def_dst
[attr
>> 3] << 6;
587 atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 0);
589 atom_put_dst(ctx
, arg
, attr
, &dptr
, 0, saved
);
592 static void atom_op_compare(atom_exec_context
*ctx
, int *ptr
, int arg
)
594 uint8_t attr
= U8((*ptr
)++);
597 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
599 src
= atom_get_src(ctx
, attr
, ptr
);
600 ctx
->ctx
->cs_equal
= (dst
== src
);
601 ctx
->ctx
->cs_above
= (dst
> src
);
602 SDEBUG(" result: %s %s\n", ctx
->ctx
->cs_equal
? "EQ" : "NE",
603 ctx
->ctx
->cs_above
? "GT" : "LE");
606 static void atom_op_delay(atom_exec_context
*ctx
, int *ptr
, int arg
)
608 uint8_t count
= U8((*ptr
)++);
609 SDEBUG(" count: %d\n", count
);
610 if (arg
== ATOM_UNIT_MICROSEC
)
611 schedule_timeout_uninterruptible(usecs_to_jiffies(count
));
613 schedule_timeout_uninterruptible(msecs_to_jiffies(count
));
616 static void atom_op_div(atom_exec_context
*ctx
, int *ptr
, int arg
)
618 uint8_t attr
= U8((*ptr
)++);
621 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
623 src
= atom_get_src(ctx
, attr
, ptr
);
625 ctx
->ctx
->divmul
[0] = dst
/ src
;
626 ctx
->ctx
->divmul
[1] = dst
% src
;
628 ctx
->ctx
->divmul
[0] = 0;
629 ctx
->ctx
->divmul
[1] = 0;
633 static void atom_op_eot(atom_exec_context
*ctx
, int *ptr
, int arg
)
635 /* functionally, a nop */
638 static void atom_op_jump(atom_exec_context
*ctx
, int *ptr
, int arg
)
640 int execute
= 0, target
= U16(*ptr
);
643 case ATOM_COND_ABOVE
:
644 execute
= ctx
->ctx
->cs_above
;
646 case ATOM_COND_ABOVEOREQUAL
:
647 execute
= ctx
->ctx
->cs_above
|| ctx
->ctx
->cs_equal
;
649 case ATOM_COND_ALWAYS
:
652 case ATOM_COND_BELOW
:
653 execute
= !(ctx
->ctx
->cs_above
|| ctx
->ctx
->cs_equal
);
655 case ATOM_COND_BELOWOREQUAL
:
656 execute
= !ctx
->ctx
->cs_above
;
658 case ATOM_COND_EQUAL
:
659 execute
= ctx
->ctx
->cs_equal
;
661 case ATOM_COND_NOTEQUAL
:
662 execute
= !ctx
->ctx
->cs_equal
;
665 if (arg
!= ATOM_COND_ALWAYS
)
666 SDEBUG(" taken: %s\n", execute
? "yes" : "no");
667 SDEBUG(" target: 0x%04X\n", target
);
669 *ptr
= ctx
->start
+ target
;
672 static void atom_op_mask(atom_exec_context
*ctx
, int *ptr
, int arg
)
674 uint8_t attr
= U8((*ptr
)++);
675 uint32_t dst
, src1
, src2
, saved
;
678 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
680 src1
= atom_get_src(ctx
, attr
, ptr
);
682 src2
= atom_get_src(ctx
, attr
, ptr
);
686 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
689 static void atom_op_move(atom_exec_context
*ctx
, int *ptr
, int arg
)
691 uint8_t attr
= U8((*ptr
)++);
694 if (((attr
>> 3) & 7) != ATOM_SRC_DWORD
)
695 atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 0);
697 atom_skip_dst(ctx
, arg
, attr
, ptr
);
701 src
= atom_get_src(ctx
, attr
, ptr
);
703 atom_put_dst(ctx
, arg
, attr
, &dptr
, src
, saved
);
706 static void atom_op_mul(atom_exec_context
*ctx
, int *ptr
, int arg
)
708 uint8_t attr
= U8((*ptr
)++);
711 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
713 src
= atom_get_src(ctx
, attr
, ptr
);
714 ctx
->ctx
->divmul
[0] = dst
* src
;
717 static void atom_op_nop(atom_exec_context
*ctx
, int *ptr
, int arg
)
722 static void atom_op_or(atom_exec_context
*ctx
, int *ptr
, int arg
)
724 uint8_t attr
= U8((*ptr
)++);
725 uint32_t dst
, src
, saved
;
728 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
730 src
= atom_get_src(ctx
, attr
, ptr
);
733 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
736 static void atom_op_postcard(atom_exec_context
*ctx
, int *ptr
, int arg
)
738 uint8_t val
= U8((*ptr
)++);
739 SDEBUG("POST card output: 0x%02X\n", val
);
742 static void atom_op_repeat(atom_exec_context
*ctx
, int *ptr
, int arg
)
744 printk(KERN_INFO
"unimplemented!\n");
747 static void atom_op_restorereg(atom_exec_context
*ctx
, int *ptr
, int arg
)
749 printk(KERN_INFO
"unimplemented!\n");
752 static void atom_op_savereg(atom_exec_context
*ctx
, int *ptr
, int arg
)
754 printk(KERN_INFO
"unimplemented!\n");
757 static void atom_op_setdatablock(atom_exec_context
*ctx
, int *ptr
, int arg
)
761 SDEBUG(" block: %d\n", idx
);
763 ctx
->ctx
->data_block
= 0;
765 ctx
->ctx
->data_block
= ctx
->start
;
767 ctx
->ctx
->data_block
= U16(ctx
->ctx
->data_table
+ 4 + 2 * idx
);
768 SDEBUG(" base: 0x%04X\n", ctx
->ctx
->data_block
);
771 static void atom_op_setfbbase(atom_exec_context
*ctx
, int *ptr
, int arg
)
773 uint8_t attr
= U8((*ptr
)++);
774 SDEBUG(" fb_base: ");
775 ctx
->ctx
->fb_base
= atom_get_src(ctx
, attr
, ptr
);
778 static void atom_op_setport(atom_exec_context
*ctx
, int *ptr
, int arg
)
784 if (port
< ATOM_IO_NAMES_CNT
)
785 SDEBUG(" port: %d (%s)\n", port
, atom_io_names
[port
]);
787 SDEBUG(" port: %d\n", port
);
789 ctx
->ctx
->io_mode
= ATOM_IO_MM
;
791 ctx
->ctx
->io_mode
= ATOM_IO_IIO
| port
;
795 ctx
->ctx
->io_mode
= ATOM_IO_PCI
;
798 case ATOM_PORT_SYSIO
:
799 ctx
->ctx
->io_mode
= ATOM_IO_SYSIO
;
805 static void atom_op_setregblock(atom_exec_context
*ctx
, int *ptr
, int arg
)
807 ctx
->ctx
->reg_block
= U16(*ptr
);
809 SDEBUG(" base: 0x%04X\n", ctx
->ctx
->reg_block
);
812 static void atom_op_shl(atom_exec_context
*ctx
, int *ptr
, int arg
)
814 uint8_t attr
= U8((*ptr
)++), shift
;
818 attr
|= atom_def_dst
[attr
>> 3] << 6;
820 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
821 shift
= U8((*ptr
)++);
822 SDEBUG(" shift: %d\n", shift
);
825 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
828 static void atom_op_shr(atom_exec_context
*ctx
, int *ptr
, int arg
)
830 uint8_t attr
= U8((*ptr
)++), shift
;
834 attr
|= atom_def_dst
[attr
>> 3] << 6;
836 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
837 shift
= U8((*ptr
)++);
838 SDEBUG(" shift: %d\n", shift
);
841 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
844 static void atom_op_sub(atom_exec_context
*ctx
, int *ptr
, int arg
)
846 uint8_t attr
= U8((*ptr
)++);
847 uint32_t dst
, src
, saved
;
850 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
852 src
= atom_get_src(ctx
, attr
, ptr
);
855 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
858 static void atom_op_switch(atom_exec_context
*ctx
, int *ptr
, int arg
)
860 uint8_t attr
= U8((*ptr
)++);
861 uint32_t src
, val
, target
;
863 src
= atom_get_src(ctx
, attr
, ptr
);
864 while (U16(*ptr
) != ATOM_CASE_END
)
865 if (U8(*ptr
) == ATOM_CASE_MAGIC
) {
869 atom_get_src(ctx
, (attr
& 0x38) | ATOM_ARG_IMM
,
873 SDEBUG(" target: %04X\n", target
);
874 *ptr
= ctx
->start
+ target
;
879 printk(KERN_INFO
"Bad case.\n");
885 static void atom_op_test(atom_exec_context
*ctx
, int *ptr
, int arg
)
887 uint8_t attr
= U8((*ptr
)++);
890 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
892 src
= atom_get_src(ctx
, attr
, ptr
);
893 ctx
->ctx
->cs_equal
= ((dst
& src
) == 0);
894 SDEBUG(" result: %s\n", ctx
->ctx
->cs_equal
? "EQ" : "NE");
897 static void atom_op_xor(atom_exec_context
*ctx
, int *ptr
, int arg
)
899 uint8_t attr
= U8((*ptr
)++);
900 uint32_t dst
, src
, saved
;
903 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
905 src
= atom_get_src(ctx
, attr
, ptr
);
908 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
911 static void atom_op_debug(atom_exec_context
*ctx
, int *ptr
, int arg
)
913 printk(KERN_INFO
"unimplemented!\n");
917 void (*func
) (atom_exec_context
*, int *, int);
919 } opcode_table
[ATOM_OP_CNT
] = {
922 atom_op_move
, ATOM_ARG_REG
}, {
923 atom_op_move
, ATOM_ARG_PS
}, {
924 atom_op_move
, ATOM_ARG_WS
}, {
925 atom_op_move
, ATOM_ARG_FB
}, {
926 atom_op_move
, ATOM_ARG_PLL
}, {
927 atom_op_move
, ATOM_ARG_MC
}, {
928 atom_op_and
, ATOM_ARG_REG
}, {
929 atom_op_and
, ATOM_ARG_PS
}, {
930 atom_op_and
, ATOM_ARG_WS
}, {
931 atom_op_and
, ATOM_ARG_FB
}, {
932 atom_op_and
, ATOM_ARG_PLL
}, {
933 atom_op_and
, ATOM_ARG_MC
}, {
934 atom_op_or
, ATOM_ARG_REG
}, {
935 atom_op_or
, ATOM_ARG_PS
}, {
936 atom_op_or
, ATOM_ARG_WS
}, {
937 atom_op_or
, ATOM_ARG_FB
}, {
938 atom_op_or
, ATOM_ARG_PLL
}, {
939 atom_op_or
, ATOM_ARG_MC
}, {
940 atom_op_shl
, ATOM_ARG_REG
}, {
941 atom_op_shl
, ATOM_ARG_PS
}, {
942 atom_op_shl
, ATOM_ARG_WS
}, {
943 atom_op_shl
, ATOM_ARG_FB
}, {
944 atom_op_shl
, ATOM_ARG_PLL
}, {
945 atom_op_shl
, ATOM_ARG_MC
}, {
946 atom_op_shr
, ATOM_ARG_REG
}, {
947 atom_op_shr
, ATOM_ARG_PS
}, {
948 atom_op_shr
, ATOM_ARG_WS
}, {
949 atom_op_shr
, ATOM_ARG_FB
}, {
950 atom_op_shr
, ATOM_ARG_PLL
}, {
951 atom_op_shr
, ATOM_ARG_MC
}, {
952 atom_op_mul
, ATOM_ARG_REG
}, {
953 atom_op_mul
, ATOM_ARG_PS
}, {
954 atom_op_mul
, ATOM_ARG_WS
}, {
955 atom_op_mul
, ATOM_ARG_FB
}, {
956 atom_op_mul
, ATOM_ARG_PLL
}, {
957 atom_op_mul
, ATOM_ARG_MC
}, {
958 atom_op_div
, ATOM_ARG_REG
}, {
959 atom_op_div
, ATOM_ARG_PS
}, {
960 atom_op_div
, ATOM_ARG_WS
}, {
961 atom_op_div
, ATOM_ARG_FB
}, {
962 atom_op_div
, ATOM_ARG_PLL
}, {
963 atom_op_div
, ATOM_ARG_MC
}, {
964 atom_op_add
, ATOM_ARG_REG
}, {
965 atom_op_add
, ATOM_ARG_PS
}, {
966 atom_op_add
, ATOM_ARG_WS
}, {
967 atom_op_add
, ATOM_ARG_FB
}, {
968 atom_op_add
, ATOM_ARG_PLL
}, {
969 atom_op_add
, ATOM_ARG_MC
}, {
970 atom_op_sub
, ATOM_ARG_REG
}, {
971 atom_op_sub
, ATOM_ARG_PS
}, {
972 atom_op_sub
, ATOM_ARG_WS
}, {
973 atom_op_sub
, ATOM_ARG_FB
}, {
974 atom_op_sub
, ATOM_ARG_PLL
}, {
975 atom_op_sub
, ATOM_ARG_MC
}, {
976 atom_op_setport
, ATOM_PORT_ATI
}, {
977 atom_op_setport
, ATOM_PORT_PCI
}, {
978 atom_op_setport
, ATOM_PORT_SYSIO
}, {
979 atom_op_setregblock
, 0}, {
980 atom_op_setfbbase
, 0}, {
981 atom_op_compare
, ATOM_ARG_REG
}, {
982 atom_op_compare
, ATOM_ARG_PS
}, {
983 atom_op_compare
, ATOM_ARG_WS
}, {
984 atom_op_compare
, ATOM_ARG_FB
}, {
985 atom_op_compare
, ATOM_ARG_PLL
}, {
986 atom_op_compare
, ATOM_ARG_MC
}, {
987 atom_op_switch
, 0}, {
988 atom_op_jump
, ATOM_COND_ALWAYS
}, {
989 atom_op_jump
, ATOM_COND_EQUAL
}, {
990 atom_op_jump
, ATOM_COND_BELOW
}, {
991 atom_op_jump
, ATOM_COND_ABOVE
}, {
992 atom_op_jump
, ATOM_COND_BELOWOREQUAL
}, {
993 atom_op_jump
, ATOM_COND_ABOVEOREQUAL
}, {
994 atom_op_jump
, ATOM_COND_NOTEQUAL
}, {
995 atom_op_test
, ATOM_ARG_REG
}, {
996 atom_op_test
, ATOM_ARG_PS
}, {
997 atom_op_test
, ATOM_ARG_WS
}, {
998 atom_op_test
, ATOM_ARG_FB
}, {
999 atom_op_test
, ATOM_ARG_PLL
}, {
1000 atom_op_test
, ATOM_ARG_MC
}, {
1001 atom_op_delay
, ATOM_UNIT_MILLISEC
}, {
1002 atom_op_delay
, ATOM_UNIT_MICROSEC
}, {
1003 atom_op_calltable
, 0}, {
1004 atom_op_repeat
, 0}, {
1005 atom_op_clear
, ATOM_ARG_REG
}, {
1006 atom_op_clear
, ATOM_ARG_PS
}, {
1007 atom_op_clear
, ATOM_ARG_WS
}, {
1008 atom_op_clear
, ATOM_ARG_FB
}, {
1009 atom_op_clear
, ATOM_ARG_PLL
}, {
1010 atom_op_clear
, ATOM_ARG_MC
}, {
1013 atom_op_mask
, ATOM_ARG_REG
}, {
1014 atom_op_mask
, ATOM_ARG_PS
}, {
1015 atom_op_mask
, ATOM_ARG_WS
}, {
1016 atom_op_mask
, ATOM_ARG_FB
}, {
1017 atom_op_mask
, ATOM_ARG_PLL
}, {
1018 atom_op_mask
, ATOM_ARG_MC
}, {
1019 atom_op_postcard
, 0}, {
1021 atom_op_savereg
, 0}, {
1022 atom_op_restorereg
, 0}, {
1023 atom_op_setdatablock
, 0}, {
1024 atom_op_xor
, ATOM_ARG_REG
}, {
1025 atom_op_xor
, ATOM_ARG_PS
}, {
1026 atom_op_xor
, ATOM_ARG_WS
}, {
1027 atom_op_xor
, ATOM_ARG_FB
}, {
1028 atom_op_xor
, ATOM_ARG_PLL
}, {
1029 atom_op_xor
, ATOM_ARG_MC
}, {
1030 atom_op_shl
, ATOM_ARG_REG
}, {
1031 atom_op_shl
, ATOM_ARG_PS
}, {
1032 atom_op_shl
, ATOM_ARG_WS
}, {
1033 atom_op_shl
, ATOM_ARG_FB
}, {
1034 atom_op_shl
, ATOM_ARG_PLL
}, {
1035 atom_op_shl
, ATOM_ARG_MC
}, {
1036 atom_op_shr
, ATOM_ARG_REG
}, {
1037 atom_op_shr
, ATOM_ARG_PS
}, {
1038 atom_op_shr
, ATOM_ARG_WS
}, {
1039 atom_op_shr
, ATOM_ARG_FB
}, {
1040 atom_op_shr
, ATOM_ARG_PLL
}, {
1041 atom_op_shr
, ATOM_ARG_MC
}, {
1042 atom_op_debug
, 0},};
1044 static void atom_execute_table_locked(struct atom_context
*ctx
, int index
, uint32_t * params
)
1046 int base
= CU16(ctx
->cmd_table
+ 4 + 2 * index
);
1047 int len
, ws
, ps
, ptr
;
1049 atom_exec_context ectx
;
1054 len
= CU16(base
+ ATOM_CT_SIZE_PTR
);
1055 ws
= CU8(base
+ ATOM_CT_WS_PTR
);
1056 ps
= CU8(base
+ ATOM_CT_PS_PTR
) & ATOM_CT_PS_MASK
;
1057 ptr
= base
+ ATOM_CT_CODE_PTR
;
1059 SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base
, len
, ws
, ps
);
1061 /* reset reg block */
1064 ectx
.ps_shift
= ps
/ 4;
1068 ectx
.ws
= kzalloc(4 * ws
, GFP_KERNEL
);
1075 if (op
< ATOM_OP_NAMES_CNT
)
1076 SDEBUG("%s @ 0x%04X\n", atom_op_names
[op
], ptr
- 1);
1078 SDEBUG("[%d] @ 0x%04X\n", op
, ptr
- 1);
1080 if (op
< ATOM_OP_CNT
&& op
> 0)
1081 opcode_table
[op
].func(&ectx
, &ptr
,
1082 opcode_table
[op
].arg
);
1086 if (op
== ATOM_OP_EOT
)
1096 void atom_execute_table(struct atom_context
*ctx
, int index
, uint32_t * params
)
1098 mutex_lock(&ctx
->mutex
);
1099 atom_execute_table_locked(ctx
, index
, params
);
1100 mutex_unlock(&ctx
->mutex
);
1103 static int atom_iio_len
[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1105 static void atom_index_iio(struct atom_context
*ctx
, int base
)
1107 ctx
->iio
= kzalloc(2 * 256, GFP_KERNEL
);
1108 while (CU8(base
) == ATOM_IIO_START
) {
1109 ctx
->iio
[CU8(base
+ 1)] = base
+ 2;
1111 while (CU8(base
) != ATOM_IIO_END
)
1112 base
+= atom_iio_len
[CU8(base
)];
1117 struct atom_context
*atom_parse(struct card_info
*card
, void *bios
)
1120 struct atom_context
*ctx
=
1121 kzalloc(sizeof(struct atom_context
), GFP_KERNEL
);
1129 if (CU16(0) != ATOM_BIOS_MAGIC
) {
1130 printk(KERN_INFO
"Invalid BIOS magic.\n");
1135 (CSTR(ATOM_ATI_MAGIC_PTR
), ATOM_ATI_MAGIC
,
1136 strlen(ATOM_ATI_MAGIC
))) {
1137 printk(KERN_INFO
"Invalid ATI magic.\n");
1142 base
= CU16(ATOM_ROM_TABLE_PTR
);
1144 (CSTR(base
+ ATOM_ROM_MAGIC_PTR
), ATOM_ROM_MAGIC
,
1145 strlen(ATOM_ROM_MAGIC
))) {
1146 printk(KERN_INFO
"Invalid ATOM magic.\n");
1151 ctx
->cmd_table
= CU16(base
+ ATOM_ROM_CMD_PTR
);
1152 ctx
->data_table
= CU16(base
+ ATOM_ROM_DATA_PTR
);
1153 atom_index_iio(ctx
, CU16(ctx
->data_table
+ ATOM_DATA_IIO_PTR
) + 4);
1155 str
= CSTR(CU16(base
+ ATOM_ROM_MSG_PTR
));
1156 while (*str
&& ((*str
== '\n') || (*str
== '\r')))
1158 /* name string isn't always 0 terminated */
1159 for (i
= 0; i
< 511; i
++) {
1161 if (name
[i
] < '.' || name
[i
] > 'z') {
1166 printk(KERN_INFO
"ATOM BIOS: %s\n", name
);
1171 int atom_asic_init(struct atom_context
*ctx
)
1173 int hwi
= CU16(ctx
->data_table
+ ATOM_DATA_FWI_PTR
);
1177 ps
[0] = cpu_to_le32(CU32(hwi
+ ATOM_FWI_DEFSCLK_PTR
));
1178 ps
[1] = cpu_to_le32(CU32(hwi
+ ATOM_FWI_DEFMCLK_PTR
));
1179 if (!ps
[0] || !ps
[1])
1182 if (!CU16(ctx
->cmd_table
+ 4 + 2 * ATOM_CMD_INIT
))
1184 atom_execute_table(ctx
, ATOM_CMD_INIT
, ps
);
1189 void atom_destroy(struct atom_context
*ctx
)
1196 void atom_parse_data_header(struct atom_context
*ctx
, int index
,
1197 uint16_t * size
, uint8_t * frev
, uint8_t * crev
,
1198 uint16_t * data_start
)
1200 int offset
= index
* 2 + 4;
1201 int idx
= CU16(ctx
->data_table
+ offset
);
1206 *frev
= CU8(idx
+ 2);
1208 *crev
= CU8(idx
+ 3);
1213 void atom_parse_cmd_header(struct atom_context
*ctx
, int index
, uint8_t * frev
,
1216 int offset
= index
* 2 + 4;
1217 int idx
= CU16(ctx
->cmd_table
+ offset
);
1220 *frev
= CU8(idx
+ 2);
1222 *crev
= CU8(idx
+ 3);
1226 int atom_allocate_fb_scratch(struct atom_context
*ctx
)
1228 int index
= GetIndexIntoMasterTable(DATA
, VRAM_UsageByFirmware
);
1229 uint16_t data_offset
;
1231 struct _ATOM_VRAM_USAGE_BY_FIRMWARE
*firmware_usage
;
1233 atom_parse_data_header(ctx
, index
, NULL
, NULL
, NULL
, &data_offset
);
1235 firmware_usage
= (struct _ATOM_VRAM_USAGE_BY_FIRMWARE
*)(ctx
->bios
+ data_offset
);
1237 DRM_DEBUG("atom firmware requested %08x %dkb\n",
1238 firmware_usage
->asFirmwareVramReserveInfo
[0].ulStartAddrUsedByFirmware
,
1239 firmware_usage
->asFirmwareVramReserveInfo
[0].usFirmwareUseInKb
);
1241 usage_bytes
= firmware_usage
->asFirmwareVramReserveInfo
[0].usFirmwareUseInKb
* 1024;
1242 if (usage_bytes
== 0)
1243 usage_bytes
= 20 * 1024;
1244 /* allocate some scratch memory */
1245 ctx
->scratch
= kzalloc(usage_bytes
, GFP_KERNEL
);