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
;
63 static void atom_execute_table_locked(struct atom_context
*ctx
, int index
, uint32_t * params
);
64 void atom_execute_table(struct atom_context
*ctx
, int index
, uint32_t * params
);
66 static uint32_t atom_arg_mask
[8] =
67 { 0xFFFFFFFF, 0xFFFF, 0xFFFF00, 0xFFFF0000, 0xFF, 0xFF00, 0xFF0000,
69 static int atom_arg_shift
[8] = { 0, 0, 8, 16, 0, 8, 16, 24 };
71 static int atom_dst_to_src
[8][4] = {
72 /* translate destination alignment field to the source alignment encoding */
82 static int atom_def_dst
[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
84 static int debug_depth
= 0;
86 static void debug_print_spaces(int n
)
92 #define DEBUG(...) do if (atom_debug) { printk(KERN_DEBUG __VA_ARGS__); } while (0)
93 #define SDEBUG(...) do if (atom_debug) { printk(KERN_DEBUG); debug_print_spaces(debug_depth); printk(__VA_ARGS__); } while (0)
95 #define DEBUG(...) do { } while (0)
96 #define SDEBUG(...) do { } while (0)
99 static uint32_t atom_iio_execute(struct atom_context
*ctx
, int base
,
100 uint32_t index
, uint32_t data
)
102 uint32_t temp
= 0xCDCDCDCD;
109 temp
= ctx
->card
->reg_read(ctx
->card
, CU16(base
+ 1));
113 (void)ctx
->card
->reg_read(ctx
->card
, CU16(base
+ 1));
114 ctx
->card
->reg_write(ctx
->card
, CU16(base
+ 1), temp
);
119 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
125 (0xFFFFFFFF >> (32 - CU8(base
+ 1))) << CU8(base
+
129 case ATOM_IIO_MOVE_INDEX
:
131 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
134 ((index
>> CU8(base
+ 2)) &
135 (0xFFFFFFFF >> (32 - CU8(base
+ 1)))) << CU8(base
+
139 case ATOM_IIO_MOVE_DATA
:
141 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
144 ((data
>> CU8(base
+ 2)) &
145 (0xFFFFFFFF >> (32 - CU8(base
+ 1)))) << CU8(base
+
149 case ATOM_IIO_MOVE_ATTR
:
151 ~((0xFFFFFFFF >> (32 - CU8(base
+ 1))) <<
155 io_attr
>> CU8(base
+ 2)) & (0xFFFFFFFF >> (32 -
166 printk(KERN_INFO
"Unknown IIO opcode.\n");
171 static uint32_t atom_get_src_int(atom_exec_context
*ctx
, uint8_t attr
,
172 int *ptr
, uint32_t *saved
, int print
)
174 uint32_t idx
, val
= 0xCDCDCDCD, align
, arg
;
175 struct atom_context
*gctx
= ctx
->ctx
;
177 align
= (attr
>> 3) & 7;
183 DEBUG("REG[0x%04X]", idx
);
184 idx
+= gctx
->reg_block
;
185 switch (gctx
->io_mode
) {
187 val
= gctx
->card
->reg_read(gctx
->card
, idx
);
191 "PCI registers are not implemented.\n");
195 "SYSIO registers are not implemented.\n");
198 if (!(gctx
->io_mode
& 0x80)) {
199 printk(KERN_INFO
"Bad IO mode.\n");
202 if (!gctx
->iio
[gctx
->io_mode
& 0x7F]) {
204 "Undefined indirect IO read method %d.\n",
205 gctx
->io_mode
& 0x7F);
209 atom_iio_execute(gctx
,
210 gctx
->iio
[gctx
->io_mode
& 0x7F],
217 /* get_unaligned_le32 avoids unaligned accesses from atombios
218 * tables, noticed on a DEC Alpha. */
219 val
= get_unaligned_le32((u32
*)&ctx
->ps
[idx
]);
221 DEBUG("PS[0x%02X,0x%04X]", idx
, val
);
227 DEBUG("WS[0x%02X]", idx
);
229 case ATOM_WS_QUOTIENT
:
230 val
= gctx
->divmul
[0];
232 case ATOM_WS_REMAINDER
:
233 val
= gctx
->divmul
[1];
235 case ATOM_WS_DATAPTR
:
236 val
= gctx
->data_block
;
241 case ATOM_WS_OR_MASK
:
242 val
= 1 << gctx
->shift
;
244 case ATOM_WS_AND_MASK
:
245 val
= ~(1 << gctx
->shift
);
247 case ATOM_WS_FB_WINDOW
:
250 case ATOM_WS_ATTRIBUTES
:
254 val
= gctx
->reg_block
;
264 if (gctx
->data_block
)
265 DEBUG("ID[0x%04X+%04X]", idx
, gctx
->data_block
);
267 DEBUG("ID[0x%04X]", idx
);
269 val
= U32(idx
+ gctx
->data_block
);
274 val
= gctx
->scratch
[((gctx
->fb_base
+ idx
) / 4)];
276 DEBUG("FB[0x%02X]", idx
);
284 DEBUG("IMM 0x%08X\n", val
);
288 case ATOM_SRC_WORD16
:
292 DEBUG("IMM 0x%04X\n", val
);
296 case ATOM_SRC_BYTE16
:
297 case ATOM_SRC_BYTE24
:
301 DEBUG("IMM 0x%02X\n", val
);
309 DEBUG("PLL[0x%02X]", idx
);
310 val
= gctx
->card
->pll_read(gctx
->card
, idx
);
316 DEBUG("MC[0x%02X]", idx
);
317 val
= gctx
->card
->mc_read(gctx
->card
, idx
);
322 val
&= atom_arg_mask
[align
];
323 val
>>= atom_arg_shift
[align
];
327 DEBUG(".[31:0] -> 0x%08X\n", val
);
330 DEBUG(".[15:0] -> 0x%04X\n", val
);
333 DEBUG(".[23:8] -> 0x%04X\n", val
);
335 case ATOM_SRC_WORD16
:
336 DEBUG(".[31:16] -> 0x%04X\n", val
);
339 DEBUG(".[7:0] -> 0x%02X\n", val
);
342 DEBUG(".[15:8] -> 0x%02X\n", val
);
344 case ATOM_SRC_BYTE16
:
345 DEBUG(".[23:16] -> 0x%02X\n", val
);
347 case ATOM_SRC_BYTE24
:
348 DEBUG(".[31:24] -> 0x%02X\n", val
);
354 static void atom_skip_src_int(atom_exec_context
*ctx
, uint8_t attr
, int *ptr
)
356 uint32_t align
= (attr
>> 3) & 7, arg
= attr
& 7;
376 case ATOM_SRC_WORD16
:
381 case ATOM_SRC_BYTE16
:
382 case ATOM_SRC_BYTE24
:
390 static uint32_t atom_get_src(atom_exec_context
*ctx
, uint8_t attr
, int *ptr
)
392 return atom_get_src_int(ctx
, attr
, ptr
, NULL
, 1);
395 static uint32_t atom_get_src_direct(atom_exec_context
*ctx
, uint8_t align
, int *ptr
)
397 uint32_t val
= 0xCDCDCDCD;
406 case ATOM_SRC_WORD16
:
412 case ATOM_SRC_BYTE16
:
413 case ATOM_SRC_BYTE24
:
421 static uint32_t atom_get_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
,
422 int *ptr
, uint32_t *saved
, int print
)
424 return atom_get_src_int(ctx
,
425 arg
| atom_dst_to_src
[(attr
>> 3) &
426 7][(attr
>> 6) & 3] << 3,
430 static void atom_skip_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
, int *ptr
)
432 atom_skip_src_int(ctx
,
433 arg
| atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) &
437 static void atom_put_dst(atom_exec_context
*ctx
, int arg
, uint8_t attr
,
438 int *ptr
, uint32_t val
, uint32_t saved
)
441 atom_dst_to_src
[(attr
>> 3) & 7][(attr
>> 6) & 3], old_val
=
443 struct atom_context
*gctx
= ctx
->ctx
;
444 old_val
&= atom_arg_mask
[align
] >> atom_arg_shift
[align
];
445 val
<<= atom_arg_shift
[align
];
446 val
&= atom_arg_mask
[align
];
447 saved
&= ~atom_arg_mask
[align
];
453 DEBUG("REG[0x%04X]", idx
);
454 idx
+= gctx
->reg_block
;
455 switch (gctx
->io_mode
) {
458 gctx
->card
->reg_write(gctx
->card
, idx
,
461 gctx
->card
->reg_write(gctx
->card
, idx
, val
);
465 "PCI registers are not implemented.\n");
469 "SYSIO registers are not implemented.\n");
472 if (!(gctx
->io_mode
& 0x80)) {
473 printk(KERN_INFO
"Bad IO mode.\n");
476 if (!gctx
->iio
[gctx
->io_mode
& 0xFF]) {
478 "Undefined indirect IO write method %d.\n",
479 gctx
->io_mode
& 0x7F);
482 atom_iio_execute(gctx
, gctx
->iio
[gctx
->io_mode
& 0xFF],
489 DEBUG("PS[0x%02X]", idx
);
490 ctx
->ps
[idx
] = cpu_to_le32(val
);
495 DEBUG("WS[0x%02X]", idx
);
497 case ATOM_WS_QUOTIENT
:
498 gctx
->divmul
[0] = val
;
500 case ATOM_WS_REMAINDER
:
501 gctx
->divmul
[1] = val
;
503 case ATOM_WS_DATAPTR
:
504 gctx
->data_block
= val
;
509 case ATOM_WS_OR_MASK
:
510 case ATOM_WS_AND_MASK
:
512 case ATOM_WS_FB_WINDOW
:
515 case ATOM_WS_ATTRIBUTES
:
519 gctx
->reg_block
= val
;
528 gctx
->scratch
[((gctx
->fb_base
+ idx
) / 4)] = val
;
529 DEBUG("FB[0x%02X]", idx
);
534 DEBUG("PLL[0x%02X]", idx
);
535 gctx
->card
->pll_write(gctx
->card
, idx
, val
);
540 DEBUG("MC[0x%02X]", idx
);
541 gctx
->card
->mc_write(gctx
->card
, idx
, val
);
546 DEBUG(".[31:0] <- 0x%08X\n", old_val
);
549 DEBUG(".[15:0] <- 0x%04X\n", old_val
);
552 DEBUG(".[23:8] <- 0x%04X\n", old_val
);
554 case ATOM_SRC_WORD16
:
555 DEBUG(".[31:16] <- 0x%04X\n", old_val
);
558 DEBUG(".[7:0] <- 0x%02X\n", old_val
);
561 DEBUG(".[15:8] <- 0x%02X\n", old_val
);
563 case ATOM_SRC_BYTE16
:
564 DEBUG(".[23:16] <- 0x%02X\n", old_val
);
566 case ATOM_SRC_BYTE24
:
567 DEBUG(".[31:24] <- 0x%02X\n", old_val
);
572 static void atom_op_add(atom_exec_context
*ctx
, int *ptr
, int arg
)
574 uint8_t attr
= U8((*ptr
)++);
575 uint32_t dst
, src
, saved
;
578 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
580 src
= atom_get_src(ctx
, attr
, ptr
);
583 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
586 static void atom_op_and(atom_exec_context
*ctx
, int *ptr
, int arg
)
588 uint8_t attr
= U8((*ptr
)++);
589 uint32_t dst
, src
, saved
;
592 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
594 src
= atom_get_src(ctx
, attr
, ptr
);
597 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
600 static void atom_op_beep(atom_exec_context
*ctx
, int *ptr
, int arg
)
602 printk("ATOM BIOS beeped!\n");
605 static void atom_op_calltable(atom_exec_context
*ctx
, int *ptr
, int arg
)
607 int idx
= U8((*ptr
)++);
608 if (idx
< ATOM_TABLE_NAMES_CNT
)
609 SDEBUG(" table: %d (%s)\n", idx
, atom_table_names
[idx
]);
611 SDEBUG(" table: %d\n", idx
);
612 if (U16(ctx
->ctx
->cmd_table
+ 4 + 2 * idx
))
613 atom_execute_table_locked(ctx
->ctx
, idx
, ctx
->ps
+ ctx
->ps_shift
);
616 static void atom_op_clear(atom_exec_context
*ctx
, int *ptr
, int arg
)
618 uint8_t attr
= U8((*ptr
)++);
622 attr
|= atom_def_dst
[attr
>> 3] << 6;
623 atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 0);
625 atom_put_dst(ctx
, arg
, attr
, &dptr
, 0, saved
);
628 static void atom_op_compare(atom_exec_context
*ctx
, int *ptr
, int arg
)
630 uint8_t attr
= U8((*ptr
)++);
633 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
635 src
= atom_get_src(ctx
, attr
, ptr
);
636 ctx
->ctx
->cs_equal
= (dst
== src
);
637 ctx
->ctx
->cs_above
= (dst
> src
);
638 SDEBUG(" result: %s %s\n", ctx
->ctx
->cs_equal
? "EQ" : "NE",
639 ctx
->ctx
->cs_above
? "GT" : "LE");
642 static void atom_op_delay(atom_exec_context
*ctx
, int *ptr
, int arg
)
644 uint8_t count
= U8((*ptr
)++);
645 SDEBUG(" count: %d\n", count
);
646 if (arg
== ATOM_UNIT_MICROSEC
)
649 schedule_timeout_uninterruptible(msecs_to_jiffies(count
));
652 static void atom_op_div(atom_exec_context
*ctx
, int *ptr
, int arg
)
654 uint8_t attr
= U8((*ptr
)++);
657 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
659 src
= atom_get_src(ctx
, attr
, ptr
);
661 ctx
->ctx
->divmul
[0] = dst
/ src
;
662 ctx
->ctx
->divmul
[1] = dst
% src
;
664 ctx
->ctx
->divmul
[0] = 0;
665 ctx
->ctx
->divmul
[1] = 0;
669 static void atom_op_eot(atom_exec_context
*ctx
, int *ptr
, int arg
)
671 /* functionally, a nop */
674 static void atom_op_jump(atom_exec_context
*ctx
, int *ptr
, int arg
)
676 int execute
= 0, target
= U16(*ptr
);
679 case ATOM_COND_ABOVE
:
680 execute
= ctx
->ctx
->cs_above
;
682 case ATOM_COND_ABOVEOREQUAL
:
683 execute
= ctx
->ctx
->cs_above
|| ctx
->ctx
->cs_equal
;
685 case ATOM_COND_ALWAYS
:
688 case ATOM_COND_BELOW
:
689 execute
= !(ctx
->ctx
->cs_above
|| ctx
->ctx
->cs_equal
);
691 case ATOM_COND_BELOWOREQUAL
:
692 execute
= !ctx
->ctx
->cs_above
;
694 case ATOM_COND_EQUAL
:
695 execute
= ctx
->ctx
->cs_equal
;
697 case ATOM_COND_NOTEQUAL
:
698 execute
= !ctx
->ctx
->cs_equal
;
701 if (arg
!= ATOM_COND_ALWAYS
)
702 SDEBUG(" taken: %s\n", execute
? "yes" : "no");
703 SDEBUG(" target: 0x%04X\n", target
);
705 *ptr
= ctx
->start
+ target
;
708 static void atom_op_mask(atom_exec_context
*ctx
, int *ptr
, int arg
)
710 uint8_t attr
= U8((*ptr
)++);
711 uint32_t dst
, src1
, src2
, saved
;
714 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
716 src1
= atom_get_src_direct(ctx
, ((attr
>> 3) & 7), ptr
);
718 src2
= atom_get_src(ctx
, attr
, ptr
);
722 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
725 static void atom_op_move(atom_exec_context
*ctx
, int *ptr
, int arg
)
727 uint8_t attr
= U8((*ptr
)++);
730 if (((attr
>> 3) & 7) != ATOM_SRC_DWORD
)
731 atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 0);
733 atom_skip_dst(ctx
, arg
, attr
, ptr
);
737 src
= atom_get_src(ctx
, attr
, ptr
);
739 atom_put_dst(ctx
, arg
, attr
, &dptr
, src
, saved
);
742 static void atom_op_mul(atom_exec_context
*ctx
, int *ptr
, int arg
)
744 uint8_t attr
= U8((*ptr
)++);
747 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
749 src
= atom_get_src(ctx
, attr
, ptr
);
750 ctx
->ctx
->divmul
[0] = dst
* src
;
753 static void atom_op_nop(atom_exec_context
*ctx
, int *ptr
, int arg
)
758 static void atom_op_or(atom_exec_context
*ctx
, int *ptr
, int arg
)
760 uint8_t attr
= U8((*ptr
)++);
761 uint32_t dst
, src
, saved
;
764 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
766 src
= atom_get_src(ctx
, attr
, ptr
);
769 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
772 static void atom_op_postcard(atom_exec_context
*ctx
, int *ptr
, int arg
)
774 uint8_t val
= U8((*ptr
)++);
775 SDEBUG("POST card output: 0x%02X\n", val
);
778 static void atom_op_repeat(atom_exec_context
*ctx
, int *ptr
, int arg
)
780 printk(KERN_INFO
"unimplemented!\n");
783 static void atom_op_restorereg(atom_exec_context
*ctx
, int *ptr
, int arg
)
785 printk(KERN_INFO
"unimplemented!\n");
788 static void atom_op_savereg(atom_exec_context
*ctx
, int *ptr
, int arg
)
790 printk(KERN_INFO
"unimplemented!\n");
793 static void atom_op_setdatablock(atom_exec_context
*ctx
, int *ptr
, int arg
)
797 SDEBUG(" block: %d\n", idx
);
799 ctx
->ctx
->data_block
= 0;
801 ctx
->ctx
->data_block
= ctx
->start
;
803 ctx
->ctx
->data_block
= U16(ctx
->ctx
->data_table
+ 4 + 2 * idx
);
804 SDEBUG(" base: 0x%04X\n", ctx
->ctx
->data_block
);
807 static void atom_op_setfbbase(atom_exec_context
*ctx
, int *ptr
, int arg
)
809 uint8_t attr
= U8((*ptr
)++);
810 SDEBUG(" fb_base: ");
811 ctx
->ctx
->fb_base
= atom_get_src(ctx
, attr
, ptr
);
814 static void atom_op_setport(atom_exec_context
*ctx
, int *ptr
, int arg
)
820 if (port
< ATOM_IO_NAMES_CNT
)
821 SDEBUG(" port: %d (%s)\n", port
, atom_io_names
[port
]);
823 SDEBUG(" port: %d\n", port
);
825 ctx
->ctx
->io_mode
= ATOM_IO_MM
;
827 ctx
->ctx
->io_mode
= ATOM_IO_IIO
| port
;
831 ctx
->ctx
->io_mode
= ATOM_IO_PCI
;
834 case ATOM_PORT_SYSIO
:
835 ctx
->ctx
->io_mode
= ATOM_IO_SYSIO
;
841 static void atom_op_setregblock(atom_exec_context
*ctx
, int *ptr
, int arg
)
843 ctx
->ctx
->reg_block
= U16(*ptr
);
845 SDEBUG(" base: 0x%04X\n", ctx
->ctx
->reg_block
);
848 static void atom_op_shift_left(atom_exec_context
*ctx
, int *ptr
, int arg
)
850 uint8_t attr
= U8((*ptr
)++), shift
;
854 attr
|= atom_def_dst
[attr
>> 3] << 6;
856 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
857 shift
= atom_get_src_direct(ctx
, ATOM_SRC_BYTE0
, ptr
);
858 SDEBUG(" shift: %d\n", shift
);
861 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
864 static void atom_op_shift_right(atom_exec_context
*ctx
, int *ptr
, int arg
)
866 uint8_t attr
= U8((*ptr
)++), shift
;
870 attr
|= atom_def_dst
[attr
>> 3] << 6;
872 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
873 shift
= atom_get_src_direct(ctx
, ATOM_SRC_BYTE0
, ptr
);
874 SDEBUG(" shift: %d\n", shift
);
877 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
880 static void atom_op_shl(atom_exec_context
*ctx
, int *ptr
, int arg
)
882 uint8_t attr
= U8((*ptr
)++), shift
;
886 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
887 shift
= atom_get_src(ctx
, attr
, ptr
);
888 SDEBUG(" shift: %d\n", shift
);
891 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
894 static void atom_op_shr(atom_exec_context
*ctx
, int *ptr
, int arg
)
896 uint8_t attr
= U8((*ptr
)++), shift
;
900 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
901 shift
= atom_get_src(ctx
, attr
, ptr
);
902 SDEBUG(" shift: %d\n", shift
);
905 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
908 static void atom_op_sub(atom_exec_context
*ctx
, int *ptr
, int arg
)
910 uint8_t attr
= U8((*ptr
)++);
911 uint32_t dst
, src
, saved
;
914 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
916 src
= atom_get_src(ctx
, attr
, ptr
);
919 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
922 static void atom_op_switch(atom_exec_context
*ctx
, int *ptr
, int arg
)
924 uint8_t attr
= U8((*ptr
)++);
925 uint32_t src
, val
, target
;
927 src
= atom_get_src(ctx
, attr
, ptr
);
928 while (U16(*ptr
) != ATOM_CASE_END
)
929 if (U8(*ptr
) == ATOM_CASE_MAGIC
) {
933 atom_get_src(ctx
, (attr
& 0x38) | ATOM_ARG_IMM
,
937 SDEBUG(" target: %04X\n", target
);
938 *ptr
= ctx
->start
+ target
;
943 printk(KERN_INFO
"Bad case.\n");
949 static void atom_op_test(atom_exec_context
*ctx
, int *ptr
, int arg
)
951 uint8_t attr
= U8((*ptr
)++);
954 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, NULL
, 1);
956 src
= atom_get_src(ctx
, attr
, ptr
);
957 ctx
->ctx
->cs_equal
= ((dst
& src
) == 0);
958 SDEBUG(" result: %s\n", ctx
->ctx
->cs_equal
? "EQ" : "NE");
961 static void atom_op_xor(atom_exec_context
*ctx
, int *ptr
, int arg
)
963 uint8_t attr
= U8((*ptr
)++);
964 uint32_t dst
, src
, saved
;
967 dst
= atom_get_dst(ctx
, arg
, attr
, ptr
, &saved
, 1);
969 src
= atom_get_src(ctx
, attr
, ptr
);
972 atom_put_dst(ctx
, arg
, attr
, &dptr
, dst
, saved
);
975 static void atom_op_debug(atom_exec_context
*ctx
, int *ptr
, int arg
)
977 printk(KERN_INFO
"unimplemented!\n");
981 void (*func
) (atom_exec_context
*, int *, int);
983 } opcode_table
[ATOM_OP_CNT
] = {
986 atom_op_move
, ATOM_ARG_REG
}, {
987 atom_op_move
, ATOM_ARG_PS
}, {
988 atom_op_move
, ATOM_ARG_WS
}, {
989 atom_op_move
, ATOM_ARG_FB
}, {
990 atom_op_move
, ATOM_ARG_PLL
}, {
991 atom_op_move
, ATOM_ARG_MC
}, {
992 atom_op_and
, ATOM_ARG_REG
}, {
993 atom_op_and
, ATOM_ARG_PS
}, {
994 atom_op_and
, ATOM_ARG_WS
}, {
995 atom_op_and
, ATOM_ARG_FB
}, {
996 atom_op_and
, ATOM_ARG_PLL
}, {
997 atom_op_and
, ATOM_ARG_MC
}, {
998 atom_op_or
, ATOM_ARG_REG
}, {
999 atom_op_or
, ATOM_ARG_PS
}, {
1000 atom_op_or
, ATOM_ARG_WS
}, {
1001 atom_op_or
, ATOM_ARG_FB
}, {
1002 atom_op_or
, ATOM_ARG_PLL
}, {
1003 atom_op_or
, ATOM_ARG_MC
}, {
1004 atom_op_shift_left
, ATOM_ARG_REG
}, {
1005 atom_op_shift_left
, ATOM_ARG_PS
}, {
1006 atom_op_shift_left
, ATOM_ARG_WS
}, {
1007 atom_op_shift_left
, ATOM_ARG_FB
}, {
1008 atom_op_shift_left
, ATOM_ARG_PLL
}, {
1009 atom_op_shift_left
, ATOM_ARG_MC
}, {
1010 atom_op_shift_right
, ATOM_ARG_REG
}, {
1011 atom_op_shift_right
, ATOM_ARG_PS
}, {
1012 atom_op_shift_right
, ATOM_ARG_WS
}, {
1013 atom_op_shift_right
, ATOM_ARG_FB
}, {
1014 atom_op_shift_right
, ATOM_ARG_PLL
}, {
1015 atom_op_shift_right
, ATOM_ARG_MC
}, {
1016 atom_op_mul
, ATOM_ARG_REG
}, {
1017 atom_op_mul
, ATOM_ARG_PS
}, {
1018 atom_op_mul
, ATOM_ARG_WS
}, {
1019 atom_op_mul
, ATOM_ARG_FB
}, {
1020 atom_op_mul
, ATOM_ARG_PLL
}, {
1021 atom_op_mul
, ATOM_ARG_MC
}, {
1022 atom_op_div
, ATOM_ARG_REG
}, {
1023 atom_op_div
, ATOM_ARG_PS
}, {
1024 atom_op_div
, ATOM_ARG_WS
}, {
1025 atom_op_div
, ATOM_ARG_FB
}, {
1026 atom_op_div
, ATOM_ARG_PLL
}, {
1027 atom_op_div
, ATOM_ARG_MC
}, {
1028 atom_op_add
, ATOM_ARG_REG
}, {
1029 atom_op_add
, ATOM_ARG_PS
}, {
1030 atom_op_add
, ATOM_ARG_WS
}, {
1031 atom_op_add
, ATOM_ARG_FB
}, {
1032 atom_op_add
, ATOM_ARG_PLL
}, {
1033 atom_op_add
, ATOM_ARG_MC
}, {
1034 atom_op_sub
, ATOM_ARG_REG
}, {
1035 atom_op_sub
, ATOM_ARG_PS
}, {
1036 atom_op_sub
, ATOM_ARG_WS
}, {
1037 atom_op_sub
, ATOM_ARG_FB
}, {
1038 atom_op_sub
, ATOM_ARG_PLL
}, {
1039 atom_op_sub
, ATOM_ARG_MC
}, {
1040 atom_op_setport
, ATOM_PORT_ATI
}, {
1041 atom_op_setport
, ATOM_PORT_PCI
}, {
1042 atom_op_setport
, ATOM_PORT_SYSIO
}, {
1043 atom_op_setregblock
, 0}, {
1044 atom_op_setfbbase
, 0}, {
1045 atom_op_compare
, ATOM_ARG_REG
}, {
1046 atom_op_compare
, ATOM_ARG_PS
}, {
1047 atom_op_compare
, ATOM_ARG_WS
}, {
1048 atom_op_compare
, ATOM_ARG_FB
}, {
1049 atom_op_compare
, ATOM_ARG_PLL
}, {
1050 atom_op_compare
, ATOM_ARG_MC
}, {
1051 atom_op_switch
, 0}, {
1052 atom_op_jump
, ATOM_COND_ALWAYS
}, {
1053 atom_op_jump
, ATOM_COND_EQUAL
}, {
1054 atom_op_jump
, ATOM_COND_BELOW
}, {
1055 atom_op_jump
, ATOM_COND_ABOVE
}, {
1056 atom_op_jump
, ATOM_COND_BELOWOREQUAL
}, {
1057 atom_op_jump
, ATOM_COND_ABOVEOREQUAL
}, {
1058 atom_op_jump
, ATOM_COND_NOTEQUAL
}, {
1059 atom_op_test
, ATOM_ARG_REG
}, {
1060 atom_op_test
, ATOM_ARG_PS
}, {
1061 atom_op_test
, ATOM_ARG_WS
}, {
1062 atom_op_test
, ATOM_ARG_FB
}, {
1063 atom_op_test
, ATOM_ARG_PLL
}, {
1064 atom_op_test
, ATOM_ARG_MC
}, {
1065 atom_op_delay
, ATOM_UNIT_MILLISEC
}, {
1066 atom_op_delay
, ATOM_UNIT_MICROSEC
}, {
1067 atom_op_calltable
, 0}, {
1068 atom_op_repeat
, 0}, {
1069 atom_op_clear
, ATOM_ARG_REG
}, {
1070 atom_op_clear
, ATOM_ARG_PS
}, {
1071 atom_op_clear
, ATOM_ARG_WS
}, {
1072 atom_op_clear
, ATOM_ARG_FB
}, {
1073 atom_op_clear
, ATOM_ARG_PLL
}, {
1074 atom_op_clear
, ATOM_ARG_MC
}, {
1077 atom_op_mask
, ATOM_ARG_REG
}, {
1078 atom_op_mask
, ATOM_ARG_PS
}, {
1079 atom_op_mask
, ATOM_ARG_WS
}, {
1080 atom_op_mask
, ATOM_ARG_FB
}, {
1081 atom_op_mask
, ATOM_ARG_PLL
}, {
1082 atom_op_mask
, ATOM_ARG_MC
}, {
1083 atom_op_postcard
, 0}, {
1085 atom_op_savereg
, 0}, {
1086 atom_op_restorereg
, 0}, {
1087 atom_op_setdatablock
, 0}, {
1088 atom_op_xor
, ATOM_ARG_REG
}, {
1089 atom_op_xor
, ATOM_ARG_PS
}, {
1090 atom_op_xor
, ATOM_ARG_WS
}, {
1091 atom_op_xor
, ATOM_ARG_FB
}, {
1092 atom_op_xor
, ATOM_ARG_PLL
}, {
1093 atom_op_xor
, ATOM_ARG_MC
}, {
1094 atom_op_shl
, ATOM_ARG_REG
}, {
1095 atom_op_shl
, ATOM_ARG_PS
}, {
1096 atom_op_shl
, ATOM_ARG_WS
}, {
1097 atom_op_shl
, ATOM_ARG_FB
}, {
1098 atom_op_shl
, ATOM_ARG_PLL
}, {
1099 atom_op_shl
, ATOM_ARG_MC
}, {
1100 atom_op_shr
, ATOM_ARG_REG
}, {
1101 atom_op_shr
, ATOM_ARG_PS
}, {
1102 atom_op_shr
, ATOM_ARG_WS
}, {
1103 atom_op_shr
, ATOM_ARG_FB
}, {
1104 atom_op_shr
, ATOM_ARG_PLL
}, {
1105 atom_op_shr
, ATOM_ARG_MC
}, {
1106 atom_op_debug
, 0},};
1108 static void atom_execute_table_locked(struct atom_context
*ctx
, int index
, uint32_t * params
)
1110 int base
= CU16(ctx
->cmd_table
+ 4 + 2 * index
);
1111 int len
, ws
, ps
, ptr
;
1113 atom_exec_context ectx
;
1118 len
= CU16(base
+ ATOM_CT_SIZE_PTR
);
1119 ws
= CU8(base
+ ATOM_CT_WS_PTR
);
1120 ps
= CU8(base
+ ATOM_CT_PS_PTR
) & ATOM_CT_PS_MASK
;
1121 ptr
= base
+ ATOM_CT_CODE_PTR
;
1123 SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base
, len
, ws
, ps
);
1126 ectx
.ps_shift
= ps
/ 4;
1130 ectx
.ws
= kzalloc(4 * ws
, GFP_KERNEL
);
1137 if (op
< ATOM_OP_NAMES_CNT
)
1138 SDEBUG("%s @ 0x%04X\n", atom_op_names
[op
], ptr
- 1);
1140 SDEBUG("[%d] @ 0x%04X\n", op
, ptr
- 1);
1142 if (op
< ATOM_OP_CNT
&& op
> 0)
1143 opcode_table
[op
].func(&ectx
, &ptr
,
1144 opcode_table
[op
].arg
);
1148 if (op
== ATOM_OP_EOT
)
1158 void atom_execute_table(struct atom_context
*ctx
, int index
, uint32_t * params
)
1160 mutex_lock(&ctx
->mutex
);
1161 /* reset reg block */
1163 /* reset fb window */
1166 ctx
->io_mode
= ATOM_IO_MM
;
1167 atom_execute_table_locked(ctx
, index
, params
);
1168 mutex_unlock(&ctx
->mutex
);
1171 static int atom_iio_len
[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1173 static void atom_index_iio(struct atom_context
*ctx
, int base
)
1175 ctx
->iio
= kzalloc(2 * 256, GFP_KERNEL
);
1176 while (CU8(base
) == ATOM_IIO_START
) {
1177 ctx
->iio
[CU8(base
+ 1)] = base
+ 2;
1179 while (CU8(base
) != ATOM_IIO_END
)
1180 base
+= atom_iio_len
[CU8(base
)];
1185 struct atom_context
*atom_parse(struct card_info
*card
, void *bios
)
1188 struct atom_context
*ctx
=
1189 kzalloc(sizeof(struct atom_context
), GFP_KERNEL
);
1197 if (CU16(0) != ATOM_BIOS_MAGIC
) {
1198 printk(KERN_INFO
"Invalid BIOS magic.\n");
1203 (CSTR(ATOM_ATI_MAGIC_PTR
), ATOM_ATI_MAGIC
,
1204 strlen(ATOM_ATI_MAGIC
))) {
1205 printk(KERN_INFO
"Invalid ATI magic.\n");
1210 base
= CU16(ATOM_ROM_TABLE_PTR
);
1212 (CSTR(base
+ ATOM_ROM_MAGIC_PTR
), ATOM_ROM_MAGIC
,
1213 strlen(ATOM_ROM_MAGIC
))) {
1214 printk(KERN_INFO
"Invalid ATOM magic.\n");
1219 ctx
->cmd_table
= CU16(base
+ ATOM_ROM_CMD_PTR
);
1220 ctx
->data_table
= CU16(base
+ ATOM_ROM_DATA_PTR
);
1221 atom_index_iio(ctx
, CU16(ctx
->data_table
+ ATOM_DATA_IIO_PTR
) + 4);
1223 str
= CSTR(CU16(base
+ ATOM_ROM_MSG_PTR
));
1224 while (*str
&& ((*str
== '\n') || (*str
== '\r')))
1226 /* name string isn't always 0 terminated */
1227 for (i
= 0; i
< 511; i
++) {
1229 if (name
[i
] < '.' || name
[i
] > 'z') {
1234 printk(KERN_INFO
"ATOM BIOS: %s\n", name
);
1239 int atom_asic_init(struct atom_context
*ctx
)
1241 int hwi
= CU16(ctx
->data_table
+ ATOM_DATA_FWI_PTR
);
1245 ps
[0] = cpu_to_le32(CU32(hwi
+ ATOM_FWI_DEFSCLK_PTR
));
1246 ps
[1] = cpu_to_le32(CU32(hwi
+ ATOM_FWI_DEFMCLK_PTR
));
1247 if (!ps
[0] || !ps
[1])
1250 if (!CU16(ctx
->cmd_table
+ 4 + 2 * ATOM_CMD_INIT
))
1252 atom_execute_table(ctx
, ATOM_CMD_INIT
, ps
);
1257 void atom_destroy(struct atom_context
*ctx
)
1264 void atom_parse_data_header(struct atom_context
*ctx
, int index
,
1265 uint16_t * size
, uint8_t * frev
, uint8_t * crev
,
1266 uint16_t * data_start
)
1268 int offset
= index
* 2 + 4;
1269 int idx
= CU16(ctx
->data_table
+ offset
);
1274 *frev
= CU8(idx
+ 2);
1276 *crev
= CU8(idx
+ 3);
1281 void atom_parse_cmd_header(struct atom_context
*ctx
, int index
, uint8_t * frev
,
1284 int offset
= index
* 2 + 4;
1285 int idx
= CU16(ctx
->cmd_table
+ offset
);
1288 *frev
= CU8(idx
+ 2);
1290 *crev
= CU8(idx
+ 3);
1294 int atom_allocate_fb_scratch(struct atom_context
*ctx
)
1296 int index
= GetIndexIntoMasterTable(DATA
, VRAM_UsageByFirmware
);
1297 uint16_t data_offset
;
1299 struct _ATOM_VRAM_USAGE_BY_FIRMWARE
*firmware_usage
;
1301 atom_parse_data_header(ctx
, index
, NULL
, NULL
, NULL
, &data_offset
);
1303 firmware_usage
= (struct _ATOM_VRAM_USAGE_BY_FIRMWARE
*)(ctx
->bios
+ data_offset
);
1305 DRM_DEBUG("atom firmware requested %08x %dkb\n",
1306 firmware_usage
->asFirmwareVramReserveInfo
[0].ulStartAddrUsedByFirmware
,
1307 firmware_usage
->asFirmwareVramReserveInfo
[0].usFirmwareUseInKb
);
1309 usage_bytes
= firmware_usage
->asFirmwareVramReserveInfo
[0].usFirmwareUseInKb
* 1024;
1310 if (usage_bytes
== 0)
1311 usage_bytes
= 20 * 1024;
1312 /* allocate some scratch memory */
1313 ctx
->scratch
= kzalloc(usage_bytes
, GFP_KERNEL
);