2 * Copyright 2008 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Author: Stanislaw Skowronek
25 #include <linux/module.h>
26 #include <linux/sched.h>
27 #include <asm/unaligned.h>
32 #include "atom-names.h"
33 #include "atom-bits.h"
35 #include <linux/delay.h>
37 #define ATOM_COND_ABOVE 0
38 #define ATOM_COND_ABOVEOREQUAL 1
39 #define ATOM_COND_ALWAYS 2
40 #define ATOM_COND_BELOW 3
41 #define ATOM_COND_BELOWOREQUAL 4
42 #define ATOM_COND_EQUAL 5
43 #define ATOM_COND_NOTEQUAL 6
45 #define ATOM_PORT_ATI 0
46 #define ATOM_PORT_PCI 1
47 #define ATOM_PORT_SYSIO 2
49 #define ATOM_UNIT_MICROSEC 0
50 #define ATOM_UNIT_MILLISEC 1
56 struct atom_context *ctx;
61 unsigned long last_jump_jiffies;
66 static int atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params);
68 static uint32_t atom_arg_mask[8] = {
69 0xFFFFFFFF, 0x0000FFFF, 0x00FFFF00, 0xFFFF0000,
70 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
72 static int atom_arg_shift[8] = { 0, 0, 8, 16, 0, 8, 16, 24 };
74 static int atom_dst_to_src[8][4] = {
75 /* translate destination alignment field to the source alignment encoding */
85 static int atom_def_dst[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
87 static int debug_depth = 0;
89 static void debug_print_spaces(int n)
95 #define ATOM_DEBUG_PRINT(...) do if (atom_debug) { kprintf(__FILE__ __VA_ARGS__); } while (0)
96 #define ATOM_SDEBUG_PRINT(...) do if (atom_debug) { kprintf(__FILE__); debug_print_spaces(debug_depth); kprintf(__VA_ARGS__); } while (0)
98 #define ATOM_DEBUG_PRINT(...) do { } while (0)
99 #define ATOM_SDEBUG_PRINT(...) do { } while (0)
102 static uint32_t atom_iio_execute(struct atom_context *ctx, int base,
103 uint32_t index, uint32_t data)
105 struct radeon_device *rdev = ctx->card->dev->dev_private;
106 uint32_t temp = 0xCDCDCDCD;
114 temp = ctx->card->ioreg_read(ctx->card, CU16(base + 1));
118 if (rdev->family == CHIP_RV515)
119 (void)ctx->card->ioreg_read(ctx->card, CU16(base + 1));
120 ctx->card->ioreg_write(ctx->card, CU16(base + 1), temp);
125 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
131 (0xFFFFFFFF >> (32 - CU8(base + 1))) << CU8(base +
135 case ATOM_IIO_MOVE_INDEX:
137 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
140 ((index >> CU8(base + 2)) &
141 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
145 case ATOM_IIO_MOVE_DATA:
147 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
150 ((data >> CU8(base + 2)) &
151 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
155 case ATOM_IIO_MOVE_ATTR:
157 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
161 io_attr >> CU8(base + 2)) & (0xFFFFFFFF >> (32 -
172 DRM_INFO("Unknown IIO opcode.\n");
177 static uint32_t atom_get_src_int(atom_exec_context *ctx, uint8_t attr,
178 int *ptr, uint32_t *saved, int print)
180 uint32_t idx, val = 0xCDCDCDCD, align, arg;
181 struct atom_context *gctx = ctx->ctx;
183 align = (attr >> 3) & 7;
186 *saved = 0; /* avoid bogus gcc warning */
193 ATOM_DEBUG_PRINT("REG[0x%04X]", idx);
194 idx += gctx->reg_block;
195 switch (gctx->io_mode) {
197 val = gctx->card->reg_read(gctx->card, idx);
201 "PCI registers are not implemented.\n");
205 "SYSIO registers are not implemented.\n");
208 if (!(gctx->io_mode & 0x80)) {
209 DRM_INFO("Bad IO mode.\n");
212 if (!gctx->iio[gctx->io_mode & 0x7F]) {
214 "Undefined indirect IO read method %d.\n",
215 gctx->io_mode & 0x7F);
219 atom_iio_execute(gctx,
220 gctx->iio[gctx->io_mode & 0x7F],
227 /* get_unaligned_le32 avoids unaligned accesses from atombios
228 * tables, noticed on a DEC Alpha. */
229 val = get_unaligned_le32((u32 *)&ctx->ps[idx]);
231 ATOM_DEBUG_PRINT("PS[0x%02X,0x%04X]", idx, val);
237 ATOM_DEBUG_PRINT("WS[0x%02X]", idx);
239 case ATOM_WS_QUOTIENT:
240 val = gctx->divmul[0];
242 case ATOM_WS_REMAINDER:
243 val = gctx->divmul[1];
245 case ATOM_WS_DATAPTR:
246 val = gctx->data_block;
251 case ATOM_WS_OR_MASK:
252 val = 1 << gctx->shift;
254 case ATOM_WS_AND_MASK:
255 val = ~(1 << gctx->shift);
257 case ATOM_WS_FB_WINDOW:
260 case ATOM_WS_ATTRIBUTES:
264 val = gctx->reg_block;
274 if (gctx->data_block)
275 ATOM_DEBUG_PRINT("ID[0x%04X+%04X]", idx, gctx->data_block);
277 ATOM_DEBUG_PRINT("ID[0x%04X]", idx);
279 val = U32(idx + gctx->data_block);
284 if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
285 DRM_ERROR("ATOM: fb read beyond scratch region: %d vs. %d\n",
286 gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
289 val = gctx->scratch[(gctx->fb_base / 4) + idx];
291 ATOM_DEBUG_PRINT("FB[0x%02X]", idx);
299 ATOM_DEBUG_PRINT("IMM 0x%08X\n", val);
303 case ATOM_SRC_WORD16:
307 ATOM_DEBUG_PRINT("IMM 0x%04X\n", val);
311 case ATOM_SRC_BYTE16:
312 case ATOM_SRC_BYTE24:
316 ATOM_DEBUG_PRINT("IMM 0x%02X\n", val);
324 ATOM_DEBUG_PRINT("PLL[0x%02X]", idx);
325 val = gctx->card->pll_read(gctx->card, idx);
331 ATOM_DEBUG_PRINT("MC[0x%02X]", idx);
332 val = gctx->card->mc_read(gctx->card, idx);
337 val &= atom_arg_mask[align];
338 val >>= atom_arg_shift[align];
342 ATOM_DEBUG_PRINT(".[31:0] -> 0x%08X\n", val);
345 ATOM_DEBUG_PRINT(".[15:0] -> 0x%04X\n", val);
348 ATOM_DEBUG_PRINT(".[23:8] -> 0x%04X\n", val);
350 case ATOM_SRC_WORD16:
351 ATOM_DEBUG_PRINT(".[31:16] -> 0x%04X\n", val);
354 ATOM_DEBUG_PRINT(".[7:0] -> 0x%02X\n", val);
357 ATOM_DEBUG_PRINT(".[15:8] -> 0x%02X\n", val);
359 case ATOM_SRC_BYTE16:
360 ATOM_DEBUG_PRINT(".[23:16] -> 0x%02X\n", val);
362 case ATOM_SRC_BYTE24:
363 ATOM_DEBUG_PRINT(".[31:24] -> 0x%02X\n", val);
369 static void atom_skip_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr)
371 uint32_t align = (attr >> 3) & 7, arg = attr & 7;
391 case ATOM_SRC_WORD16:
396 case ATOM_SRC_BYTE16:
397 case ATOM_SRC_BYTE24:
405 static uint32_t atom_get_src(atom_exec_context *ctx, uint8_t attr, int *ptr)
407 return atom_get_src_int(ctx, attr, ptr, NULL, 1);
410 static uint32_t atom_get_src_direct(atom_exec_context *ctx, uint8_t align, int *ptr)
412 uint32_t val = 0xCDCDCDCD;
421 case ATOM_SRC_WORD16:
427 case ATOM_SRC_BYTE16:
428 case ATOM_SRC_BYTE24:
436 static uint32_t atom_get_dst(atom_exec_context *ctx, int arg, uint8_t attr,
437 int *ptr, uint32_t *saved, int print)
439 return atom_get_src_int(ctx,
440 arg | atom_dst_to_src[(attr >> 3) &
441 7][(attr >> 6) & 3] << 3,
445 static void atom_skip_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr)
447 atom_skip_src_int(ctx,
448 arg | atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) &
452 static void atom_put_dst(atom_exec_context *ctx, int arg, uint8_t attr,
453 int *ptr, uint32_t val, uint32_t saved)
456 atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3], old_val =
458 struct atom_context *gctx = ctx->ctx;
459 old_val &= atom_arg_mask[align] >> atom_arg_shift[align];
460 val <<= atom_arg_shift[align];
461 val &= atom_arg_mask[align];
462 saved &= ~atom_arg_mask[align];
468 ATOM_DEBUG_PRINT("REG[0x%04X]", idx);
469 idx += gctx->reg_block;
470 switch (gctx->io_mode) {
473 gctx->card->reg_write(gctx->card, idx,
476 gctx->card->reg_write(gctx->card, idx, val);
480 "PCI registers are not implemented.\n");
484 "SYSIO registers are not implemented.\n");
487 if (!(gctx->io_mode & 0x80)) {
488 DRM_INFO("Bad IO mode.\n");
491 if (!gctx->iio[gctx->io_mode & 0xFF]) {
493 "Undefined indirect IO write method %d.\n",
494 gctx->io_mode & 0x7F);
497 atom_iio_execute(gctx, gctx->iio[gctx->io_mode & 0xFF],
504 ATOM_DEBUG_PRINT("PS[0x%02X]", idx);
505 ctx->ps[idx] = cpu_to_le32(val);
510 ATOM_DEBUG_PRINT("WS[0x%02X]", idx);
512 case ATOM_WS_QUOTIENT:
513 gctx->divmul[0] = val;
515 case ATOM_WS_REMAINDER:
516 gctx->divmul[1] = val;
518 case ATOM_WS_DATAPTR:
519 gctx->data_block = val;
524 case ATOM_WS_OR_MASK:
525 case ATOM_WS_AND_MASK:
527 case ATOM_WS_FB_WINDOW:
530 case ATOM_WS_ATTRIBUTES:
534 gctx->reg_block = val;
543 if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
544 DRM_ERROR("ATOM: fb write beyond scratch region: %d vs. %d\n",
545 gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
547 gctx->scratch[(gctx->fb_base / 4) + idx] = val;
548 ATOM_DEBUG_PRINT("FB[0x%02X]", idx);
553 ATOM_DEBUG_PRINT("PLL[0x%02X]", idx);
554 gctx->card->pll_write(gctx->card, idx, val);
559 ATOM_DEBUG_PRINT("MC[0x%02X]", idx);
560 gctx->card->mc_write(gctx->card, idx, val);
565 ATOM_DEBUG_PRINT(".[31:0] <- 0x%08X\n", old_val);
568 ATOM_DEBUG_PRINT(".[15:0] <- 0x%04X\n", old_val);
571 ATOM_DEBUG_PRINT(".[23:8] <- 0x%04X\n", old_val);
573 case ATOM_SRC_WORD16:
574 ATOM_DEBUG_PRINT(".[31:16] <- 0x%04X\n", old_val);
577 ATOM_DEBUG_PRINT(".[7:0] <- 0x%02X\n", old_val);
580 ATOM_DEBUG_PRINT(".[15:8] <- 0x%02X\n", old_val);
582 case ATOM_SRC_BYTE16:
583 ATOM_DEBUG_PRINT(".[23:16] <- 0x%02X\n", old_val);
585 case ATOM_SRC_BYTE24:
586 ATOM_DEBUG_PRINT(".[31:24] <- 0x%02X\n", old_val);
591 static void atom_op_add(atom_exec_context *ctx, int *ptr, int arg)
593 uint8_t attr = U8((*ptr)++);
594 uint32_t dst, src, saved;
596 ATOM_SDEBUG_PRINT(" dst: ");
597 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
598 ATOM_SDEBUG_PRINT(" src: ");
599 src = atom_get_src(ctx, attr, ptr);
601 ATOM_SDEBUG_PRINT(" dst: ");
602 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
605 static void atom_op_and(atom_exec_context *ctx, int *ptr, int arg)
607 uint8_t attr = U8((*ptr)++);
608 uint32_t dst, src, saved;
610 ATOM_SDEBUG_PRINT(" dst: ");
611 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
612 ATOM_SDEBUG_PRINT(" src: ");
613 src = atom_get_src(ctx, attr, ptr);
615 ATOM_SDEBUG_PRINT(" dst: ");
616 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
619 static void atom_op_beep(atom_exec_context *ctx, int *ptr, int arg)
621 DRM_INFO("ATOM BIOS beeped!\n");
624 static void atom_op_calltable(atom_exec_context *ctx, int *ptr, int arg)
626 int idx = U8((*ptr)++);
629 if (idx < ATOM_TABLE_NAMES_CNT)
630 ATOM_SDEBUG_PRINT(" table: %d (%s)\n", idx, atom_table_names[idx]);
632 ATOM_SDEBUG_PRINT(" table: %d\n", idx);
633 if (U16(ctx->ctx->cmd_table + 4 + 2 * idx))
634 r = atom_execute_table_locked(ctx->ctx, idx, ctx->ps + ctx->ps_shift);
640 static void atom_op_clear(atom_exec_context *ctx, int *ptr, int arg)
642 uint8_t attr = U8((*ptr)++);
646 attr |= atom_def_dst[attr >> 3] << 6;
647 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
648 ATOM_SDEBUG_PRINT(" dst: ");
649 atom_put_dst(ctx, arg, attr, &dptr, 0, saved);
652 static void atom_op_compare(atom_exec_context *ctx, int *ptr, int arg)
654 uint8_t attr = U8((*ptr)++);
656 ATOM_SDEBUG_PRINT(" src1: ");
657 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
658 ATOM_SDEBUG_PRINT(" src2: ");
659 src = atom_get_src(ctx, attr, ptr);
660 ctx->ctx->cs_equal = (dst == src);
661 ctx->ctx->cs_above = (dst > src);
662 ATOM_SDEBUG_PRINT(" result: %s %s\n", ctx->ctx->cs_equal ? "EQ" : "NE",
663 ctx->ctx->cs_above ? "GT" : "LE");
666 static void atom_op_delay(atom_exec_context *ctx, int *ptr, int arg)
668 unsigned count = U8((*ptr)++);
669 ATOM_SDEBUG_PRINT(" count: %d\n", count);
670 if (arg == ATOM_UNIT_MICROSEC)
672 else if (!drm_can_sleep())
678 static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
680 uint8_t attr = U8((*ptr)++);
682 ATOM_SDEBUG_PRINT(" src1: ");
683 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
684 ATOM_SDEBUG_PRINT(" src2: ");
685 src = atom_get_src(ctx, attr, ptr);
687 ctx->ctx->divmul[0] = dst / src;
688 ctx->ctx->divmul[1] = dst % src;
690 ctx->ctx->divmul[0] = 0;
691 ctx->ctx->divmul[1] = 0;
695 static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
697 /* functionally, a nop */
700 static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
702 int execute = 0, target = U16(*ptr);
703 unsigned long cjiffies;
707 case ATOM_COND_ABOVE:
708 execute = ctx->ctx->cs_above;
710 case ATOM_COND_ABOVEOREQUAL:
711 execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
713 case ATOM_COND_ALWAYS:
716 case ATOM_COND_BELOW:
717 execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
719 case ATOM_COND_BELOWOREQUAL:
720 execute = !ctx->ctx->cs_above;
722 case ATOM_COND_EQUAL:
723 execute = ctx->ctx->cs_equal;
725 case ATOM_COND_NOTEQUAL:
726 execute = !ctx->ctx->cs_equal;
729 if (arg != ATOM_COND_ALWAYS)
730 ATOM_SDEBUG_PRINT(" taken: %s\n", execute ? "yes" : "no");
731 ATOM_SDEBUG_PRINT(" target: 0x%04X\n", target);
733 if (ctx->last_jump == (ctx->start + target)) {
735 if (time_after(cjiffies, ctx->last_jump_jiffies)) {
736 cjiffies -= ctx->last_jump_jiffies;
737 if ((jiffies_to_msecs(cjiffies) > 5000)) {
738 DRM_ERROR("atombios stuck in loop for more than 5secs aborting\n");
742 /* jiffies wrap around we will just wait a little longer */
743 ctx->last_jump_jiffies = jiffies;
746 ctx->last_jump = ctx->start + target;
747 ctx->last_jump_jiffies = jiffies;
749 *ptr = ctx->start + target;
753 static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
755 uint8_t attr = U8((*ptr)++);
756 uint32_t dst, mask, src, saved;
758 ATOM_SDEBUG_PRINT(" dst: ");
759 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
760 mask = atom_get_src_direct(ctx, ((attr >> 3) & 7), ptr);
761 ATOM_SDEBUG_PRINT(" mask: 0x%08x", mask);
762 ATOM_SDEBUG_PRINT(" src: ");
763 src = atom_get_src(ctx, attr, ptr);
766 ATOM_SDEBUG_PRINT(" dst: ");
767 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
770 static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
772 uint8_t attr = U8((*ptr)++);
775 if (((attr >> 3) & 7) != ATOM_SRC_DWORD)
776 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
778 atom_skip_dst(ctx, arg, attr, ptr);
781 ATOM_SDEBUG_PRINT(" src: ");
782 src = atom_get_src(ctx, attr, ptr);
783 ATOM_SDEBUG_PRINT(" dst: ");
784 atom_put_dst(ctx, arg, attr, &dptr, src, saved);
787 static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
789 uint8_t attr = U8((*ptr)++);
791 ATOM_SDEBUG_PRINT(" src1: ");
792 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
793 ATOM_SDEBUG_PRINT(" src2: ");
794 src = atom_get_src(ctx, attr, ptr);
795 ctx->ctx->divmul[0] = dst * src;
798 static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
803 static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
805 uint8_t attr = U8((*ptr)++);
806 uint32_t dst, src, saved;
808 ATOM_SDEBUG_PRINT(" dst: ");
809 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
810 ATOM_SDEBUG_PRINT(" src: ");
811 src = atom_get_src(ctx, attr, ptr);
813 ATOM_SDEBUG_PRINT(" dst: ");
814 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
817 static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
819 uint8_t val = U8((*ptr)++);
820 ATOM_SDEBUG_PRINT("POST card output: 0x%02X\n", val);
823 static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
825 DRM_INFO("unimplemented!\n");
828 static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
830 DRM_INFO("unimplemented!\n");
833 static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
835 DRM_INFO("unimplemented!\n");
838 static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
842 ATOM_SDEBUG_PRINT(" block: %d\n", idx);
844 ctx->ctx->data_block = 0;
846 ctx->ctx->data_block = ctx->start;
848 ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
849 ATOM_SDEBUG_PRINT(" base: 0x%04X\n", ctx->ctx->data_block);
852 static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
854 uint8_t attr = U8((*ptr)++);
855 ATOM_SDEBUG_PRINT(" fb_base: ");
856 ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
859 static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
865 if (port < ATOM_IO_NAMES_CNT)
866 ATOM_SDEBUG_PRINT(" port: %d (%s)\n", port, atom_io_names[port]);
868 ATOM_SDEBUG_PRINT(" port: %d\n", port);
870 ctx->ctx->io_mode = ATOM_IO_MM;
872 ctx->ctx->io_mode = ATOM_IO_IIO | port;
876 ctx->ctx->io_mode = ATOM_IO_PCI;
879 case ATOM_PORT_SYSIO:
880 ctx->ctx->io_mode = ATOM_IO_SYSIO;
886 static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
888 ctx->ctx->reg_block = U16(*ptr);
890 ATOM_SDEBUG_PRINT(" base: 0x%04X\n", ctx->ctx->reg_block);
893 static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
895 uint8_t attr = U8((*ptr)++), shift;
899 attr |= atom_def_dst[attr >> 3] << 6;
900 ATOM_SDEBUG_PRINT(" dst: ");
901 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
902 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
903 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
905 ATOM_SDEBUG_PRINT(" dst: ");
906 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
909 static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
911 uint8_t attr = U8((*ptr)++), shift;
915 attr |= atom_def_dst[attr >> 3] << 6;
916 ATOM_SDEBUG_PRINT(" dst: ");
917 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
918 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
919 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
921 ATOM_SDEBUG_PRINT(" dst: ");
922 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
925 static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
927 uint8_t attr = U8((*ptr)++), shift;
930 uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
931 ATOM_SDEBUG_PRINT(" dst: ");
932 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
933 /* op needs to full dst value */
935 shift = atom_get_src(ctx, attr, ptr);
936 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
938 dst &= atom_arg_mask[dst_align];
939 dst >>= atom_arg_shift[dst_align];
940 ATOM_SDEBUG_PRINT(" dst: ");
941 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
944 static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
946 uint8_t attr = U8((*ptr)++), shift;
949 uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
950 ATOM_SDEBUG_PRINT(" dst: ");
951 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
952 /* op needs to full dst value */
954 shift = atom_get_src(ctx, attr, ptr);
955 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
957 dst &= atom_arg_mask[dst_align];
958 dst >>= atom_arg_shift[dst_align];
959 ATOM_SDEBUG_PRINT(" dst: ");
960 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
963 static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
965 uint8_t attr = U8((*ptr)++);
966 uint32_t dst, src, saved;
968 ATOM_SDEBUG_PRINT(" dst: ");
969 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
970 ATOM_SDEBUG_PRINT(" src: ");
971 src = atom_get_src(ctx, attr, ptr);
973 ATOM_SDEBUG_PRINT(" dst: ");
974 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
977 static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
979 uint8_t attr = U8((*ptr)++);
980 uint32_t src, val, target;
981 ATOM_SDEBUG_PRINT(" switch: ");
982 src = atom_get_src(ctx, attr, ptr);
983 while (U16(*ptr) != ATOM_CASE_END)
984 if (U8(*ptr) == ATOM_CASE_MAGIC) {
986 ATOM_SDEBUG_PRINT(" case: ");
988 atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
992 ATOM_SDEBUG_PRINT(" target: %04X\n", target);
993 *ptr = ctx->start + target;
998 DRM_INFO("Bad case.\n");
1004 static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
1006 uint8_t attr = U8((*ptr)++);
1008 ATOM_SDEBUG_PRINT(" src1: ");
1009 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
1010 ATOM_SDEBUG_PRINT(" src2: ");
1011 src = atom_get_src(ctx, attr, ptr);
1012 ctx->ctx->cs_equal = ((dst & src) == 0);
1013 ATOM_SDEBUG_PRINT(" result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
1016 static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
1018 uint8_t attr = U8((*ptr)++);
1019 uint32_t dst, src, saved;
1021 ATOM_SDEBUG_PRINT(" dst: ");
1022 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
1023 ATOM_SDEBUG_PRINT(" src: ");
1024 src = atom_get_src(ctx, attr, ptr);
1026 ATOM_SDEBUG_PRINT(" dst: ");
1027 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
1030 static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
1032 DRM_INFO("unimplemented!\n");
1036 void (*func) (atom_exec_context *, int *, int);
1038 } opcode_table[ATOM_OP_CNT] = {
1041 atom_op_move, ATOM_ARG_REG}, {
1042 atom_op_move, ATOM_ARG_PS}, {
1043 atom_op_move, ATOM_ARG_WS}, {
1044 atom_op_move, ATOM_ARG_FB}, {
1045 atom_op_move, ATOM_ARG_PLL}, {
1046 atom_op_move, ATOM_ARG_MC}, {
1047 atom_op_and, ATOM_ARG_REG}, {
1048 atom_op_and, ATOM_ARG_PS}, {
1049 atom_op_and, ATOM_ARG_WS}, {
1050 atom_op_and, ATOM_ARG_FB}, {
1051 atom_op_and, ATOM_ARG_PLL}, {
1052 atom_op_and, ATOM_ARG_MC}, {
1053 atom_op_or, ATOM_ARG_REG}, {
1054 atom_op_or, ATOM_ARG_PS}, {
1055 atom_op_or, ATOM_ARG_WS}, {
1056 atom_op_or, ATOM_ARG_FB}, {
1057 atom_op_or, ATOM_ARG_PLL}, {
1058 atom_op_or, ATOM_ARG_MC}, {
1059 atom_op_shift_left, ATOM_ARG_REG}, {
1060 atom_op_shift_left, ATOM_ARG_PS}, {
1061 atom_op_shift_left, ATOM_ARG_WS}, {
1062 atom_op_shift_left, ATOM_ARG_FB}, {
1063 atom_op_shift_left, ATOM_ARG_PLL}, {
1064 atom_op_shift_left, ATOM_ARG_MC}, {
1065 atom_op_shift_right, ATOM_ARG_REG}, {
1066 atom_op_shift_right, ATOM_ARG_PS}, {
1067 atom_op_shift_right, ATOM_ARG_WS}, {
1068 atom_op_shift_right, ATOM_ARG_FB}, {
1069 atom_op_shift_right, ATOM_ARG_PLL}, {
1070 atom_op_shift_right, ATOM_ARG_MC}, {
1071 atom_op_mul, ATOM_ARG_REG}, {
1072 atom_op_mul, ATOM_ARG_PS}, {
1073 atom_op_mul, ATOM_ARG_WS}, {
1074 atom_op_mul, ATOM_ARG_FB}, {
1075 atom_op_mul, ATOM_ARG_PLL}, {
1076 atom_op_mul, ATOM_ARG_MC}, {
1077 atom_op_div, ATOM_ARG_REG}, {
1078 atom_op_div, ATOM_ARG_PS}, {
1079 atom_op_div, ATOM_ARG_WS}, {
1080 atom_op_div, ATOM_ARG_FB}, {
1081 atom_op_div, ATOM_ARG_PLL}, {
1082 atom_op_div, ATOM_ARG_MC}, {
1083 atom_op_add, ATOM_ARG_REG}, {
1084 atom_op_add, ATOM_ARG_PS}, {
1085 atom_op_add, ATOM_ARG_WS}, {
1086 atom_op_add, ATOM_ARG_FB}, {
1087 atom_op_add, ATOM_ARG_PLL}, {
1088 atom_op_add, ATOM_ARG_MC}, {
1089 atom_op_sub, ATOM_ARG_REG}, {
1090 atom_op_sub, ATOM_ARG_PS}, {
1091 atom_op_sub, ATOM_ARG_WS}, {
1092 atom_op_sub, ATOM_ARG_FB}, {
1093 atom_op_sub, ATOM_ARG_PLL}, {
1094 atom_op_sub, ATOM_ARG_MC}, {
1095 atom_op_setport, ATOM_PORT_ATI}, {
1096 atom_op_setport, ATOM_PORT_PCI}, {
1097 atom_op_setport, ATOM_PORT_SYSIO}, {
1098 atom_op_setregblock, 0}, {
1099 atom_op_setfbbase, 0}, {
1100 atom_op_compare, ATOM_ARG_REG}, {
1101 atom_op_compare, ATOM_ARG_PS}, {
1102 atom_op_compare, ATOM_ARG_WS}, {
1103 atom_op_compare, ATOM_ARG_FB}, {
1104 atom_op_compare, ATOM_ARG_PLL}, {
1105 atom_op_compare, ATOM_ARG_MC}, {
1106 atom_op_switch, 0}, {
1107 atom_op_jump, ATOM_COND_ALWAYS}, {
1108 atom_op_jump, ATOM_COND_EQUAL}, {
1109 atom_op_jump, ATOM_COND_BELOW}, {
1110 atom_op_jump, ATOM_COND_ABOVE}, {
1111 atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
1112 atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
1113 atom_op_jump, ATOM_COND_NOTEQUAL}, {
1114 atom_op_test, ATOM_ARG_REG}, {
1115 atom_op_test, ATOM_ARG_PS}, {
1116 atom_op_test, ATOM_ARG_WS}, {
1117 atom_op_test, ATOM_ARG_FB}, {
1118 atom_op_test, ATOM_ARG_PLL}, {
1119 atom_op_test, ATOM_ARG_MC}, {
1120 atom_op_delay, ATOM_UNIT_MILLISEC}, {
1121 atom_op_delay, ATOM_UNIT_MICROSEC}, {
1122 atom_op_calltable, 0}, {
1123 atom_op_repeat, 0}, {
1124 atom_op_clear, ATOM_ARG_REG}, {
1125 atom_op_clear, ATOM_ARG_PS}, {
1126 atom_op_clear, ATOM_ARG_WS}, {
1127 atom_op_clear, ATOM_ARG_FB}, {
1128 atom_op_clear, ATOM_ARG_PLL}, {
1129 atom_op_clear, ATOM_ARG_MC}, {
1132 atom_op_mask, ATOM_ARG_REG}, {
1133 atom_op_mask, ATOM_ARG_PS}, {
1134 atom_op_mask, ATOM_ARG_WS}, {
1135 atom_op_mask, ATOM_ARG_FB}, {
1136 atom_op_mask, ATOM_ARG_PLL}, {
1137 atom_op_mask, ATOM_ARG_MC}, {
1138 atom_op_postcard, 0}, {
1140 atom_op_savereg, 0}, {
1141 atom_op_restorereg, 0}, {
1142 atom_op_setdatablock, 0}, {
1143 atom_op_xor, ATOM_ARG_REG}, {
1144 atom_op_xor, ATOM_ARG_PS}, {
1145 atom_op_xor, ATOM_ARG_WS}, {
1146 atom_op_xor, ATOM_ARG_FB}, {
1147 atom_op_xor, ATOM_ARG_PLL}, {
1148 atom_op_xor, ATOM_ARG_MC}, {
1149 atom_op_shl, ATOM_ARG_REG}, {
1150 atom_op_shl, ATOM_ARG_PS}, {
1151 atom_op_shl, ATOM_ARG_WS}, {
1152 atom_op_shl, ATOM_ARG_FB}, {
1153 atom_op_shl, ATOM_ARG_PLL}, {
1154 atom_op_shl, ATOM_ARG_MC}, {
1155 atom_op_shr, ATOM_ARG_REG}, {
1156 atom_op_shr, ATOM_ARG_PS}, {
1157 atom_op_shr, ATOM_ARG_WS}, {
1158 atom_op_shr, ATOM_ARG_FB}, {
1159 atom_op_shr, ATOM_ARG_PLL}, {
1160 atom_op_shr, ATOM_ARG_MC}, {
1161 atom_op_debug, 0},};
1163 static int atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
1165 int base = CU16(ctx->cmd_table + 4 + 2 * index);
1166 int len, ws, ps, ptr;
1168 atom_exec_context ectx;
1174 len = CU16(base + ATOM_CT_SIZE_PTR);
1175 ws = CU8(base + ATOM_CT_WS_PTR);
1176 ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
1177 ptr = base + ATOM_CT_CODE_PTR;
1179 ATOM_SDEBUG_PRINT(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
1182 ectx.ps_shift = ps / 4;
1188 ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
1195 if (op < ATOM_OP_NAMES_CNT)
1196 ATOM_SDEBUG_PRINT("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
1198 ATOM_SDEBUG_PRINT("[%d] @ 0x%04X\n", op, ptr - 1);
1200 DRM_ERROR("atombios stuck executing %04X (len %d, WS %d, PS %d) @ 0x%04X\n",
1201 base, len, ws, ps, ptr - 1);
1206 if (op < ATOM_OP_CNT && op > 0)
1207 opcode_table[op].func(&ectx, &ptr,
1208 opcode_table[op].arg);
1212 if (op == ATOM_OP_EOT)
1216 ATOM_SDEBUG_PRINT("<<\n");
1224 int atom_execute_table_scratch_unlocked(struct atom_context *ctx, int index, uint32_t * params)
1228 lockmgr(&ctx->mutex, LK_EXCLUSIVE);
1229 /* reset data block */
1230 ctx->data_block = 0;
1231 /* reset reg block */
1233 /* reset fb window */
1236 ctx->io_mode = ATOM_IO_MM;
1240 r = atom_execute_table_locked(ctx, index, params);
1241 lockmgr(&ctx->mutex, LK_RELEASE);
1245 int atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
1248 lockmgr(&ctx->scratch_mutex, LK_EXCLUSIVE);
1249 r = atom_execute_table_scratch_unlocked(ctx, index, params);
1250 lockmgr(&ctx->scratch_mutex, LK_RELEASE);
1254 static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1256 static void atom_index_iio(struct atom_context *ctx, int base)
1258 ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
1261 while (CU8(base) == ATOM_IIO_START) {
1262 ctx->iio[CU8(base + 1)] = base + 2;
1264 while (CU8(base) != ATOM_IIO_END)
1265 base += atom_iio_len[CU8(base)];
1270 struct atom_context *atom_parse(struct card_info *card, void *bios)
1273 struct atom_context *ctx =
1274 kzalloc(sizeof(struct atom_context), GFP_KERNEL);
1285 if (CU16(0) != ATOM_BIOS_MAGIC) {
1286 DRM_INFO("Invalid BIOS magic.\n");
1291 (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
1292 strlen(ATOM_ATI_MAGIC))) {
1293 DRM_INFO("Invalid ATI magic.\n");
1298 base = CU16(ATOM_ROM_TABLE_PTR);
1300 (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
1301 strlen(ATOM_ROM_MAGIC))) {
1302 DRM_INFO("Invalid ATOM magic.\n");
1307 ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
1308 ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
1309 atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
1315 str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
1316 while (*str && ((*str == '\n') || (*str == '\r')))
1318 /* name string isn't always 0 terminated */
1319 for (i = 0; i < 511; i++) {
1321 if (name[i] < '.' || name[i] > 'z') {
1326 DRM_INFO("ATOM BIOS: %s\n", name);
1331 int atom_asic_init(struct atom_context *ctx)
1333 struct radeon_device *rdev = ctx->card->dev->dev_private;
1334 int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
1340 ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
1341 ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
1342 if (!ps[0] || !ps[1])
1345 if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
1347 ret = atom_execute_table(ctx, ATOM_CMD_INIT, ps);
1353 if (rdev->family < CHIP_R600) {
1354 if (CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_SPDFANCNTL))
1355 atom_execute_table(ctx, ATOM_CMD_SPDFANCNTL, ps);
1360 void atom_destroy(struct atom_context *ctx)
1366 bool atom_parse_data_header(struct atom_context *ctx, int index,
1367 uint16_t * size, uint8_t * frev, uint8_t * crev,
1368 uint16_t * data_start)
1370 int offset = index * 2 + 4;
1371 int idx = CU16(ctx->data_table + offset);
1372 u16 *mdt = (u16 *)((char *)ctx->bios + ctx->data_table + 4);
1380 *frev = CU8(idx + 2);
1382 *crev = CU8(idx + 3);
1387 bool atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
1390 int offset = index * 2 + 4;
1391 int idx = CU16(ctx->cmd_table + offset);
1392 u16 *mct = (u16 *)((char *)ctx->bios + ctx->cmd_table + 4);
1398 *frev = CU8(idx + 2);
1400 *crev = CU8(idx + 3);
1404 int atom_allocate_fb_scratch(struct atom_context *ctx)
1406 int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
1407 uint16_t data_offset;
1408 int usage_bytes = 0;
1409 struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
1411 if (atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
1412 firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)((char *)ctx->bios + data_offset);
1414 DRM_DEBUG("atom firmware requested %08x %dkb\n",
1415 le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
1416 le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
1418 usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
1420 ctx->scratch_size_bytes = 0;
1421 if (usage_bytes == 0)
1422 usage_bytes = 20 * 1024;
1423 /* allocate some scratch memory */
1424 ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
1427 ctx->scratch_size_bytes = usage_bytes;