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, 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 ATOM_DEBUG_PRINT(...) do if (atom_debug) { kprintf(__FILE__ __VA_ARGS__); } while (0)
95 #define ATOM_SDEBUG_PRINT(...) do if (atom_debug) { kprintf(__FILE__); debug_print_spaces(debug_depth); kprintf(__VA_ARGS__); } while (0)
97 #define ATOM_DEBUG_PRINT(...) do { } while (0)
98 #define ATOM_SDEBUG_PRINT(...) do { } while (0)
101 static uint32_t atom_iio_execute(struct atom_context *ctx, int base,
102 uint32_t index, uint32_t data)
104 struct radeon_device *rdev = ctx->card->dev->dev_private;
105 uint32_t temp = 0xCDCDCDCD;
113 temp = ctx->card->ioreg_read(ctx->card, CU16(base + 1));
117 if (rdev->family == CHIP_RV515)
118 (void)ctx->card->ioreg_read(ctx->card, CU16(base + 1));
119 ctx->card->ioreg_write(ctx->card, CU16(base + 1), temp);
124 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
130 (0xFFFFFFFF >> (32 - CU8(base + 1))) << CU8(base +
134 case ATOM_IIO_MOVE_INDEX:
136 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
139 ((index >> CU8(base + 2)) &
140 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
144 case ATOM_IIO_MOVE_DATA:
146 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
149 ((data >> CU8(base + 2)) &
150 (0xFFFFFFFF >> (32 - CU8(base + 1)))) << CU8(base +
154 case ATOM_IIO_MOVE_ATTR:
156 ~((0xFFFFFFFF >> (32 - CU8(base + 1))) <<
160 io_attr >> CU8(base + 2)) & (0xFFFFFFFF >> (32 -
171 DRM_INFO("Unknown IIO opcode.\n");
176 static uint32_t atom_get_src_int(atom_exec_context *ctx, uint8_t attr,
177 int *ptr, uint32_t *saved, int print)
179 uint32_t idx, val = 0xCDCDCDCD, align, arg;
180 struct atom_context *gctx = ctx->ctx;
182 align = (attr >> 3) & 7;
185 *saved = 0; /* avoid bogus gcc warning */
192 ATOM_DEBUG_PRINT("REG[0x%04X]", idx);
193 idx += gctx->reg_block;
194 switch (gctx->io_mode) {
196 val = gctx->card->reg_read(gctx->card, idx);
200 "PCI registers are not implemented.\n");
204 "SYSIO registers are not implemented.\n");
207 if (!(gctx->io_mode & 0x80)) {
208 DRM_INFO("Bad IO mode.\n");
211 if (!gctx->iio[gctx->io_mode & 0x7F]) {
213 "Undefined indirect IO read method %d.\n",
214 gctx->io_mode & 0x7F);
218 atom_iio_execute(gctx,
219 gctx->iio[gctx->io_mode & 0x7F],
226 /* get_unaligned_le32 avoids unaligned accesses from atombios
227 * tables, noticed on a DEC Alpha. */
228 val = get_unaligned_le32((u32 *)&ctx->ps[idx]);
230 ATOM_DEBUG_PRINT("PS[0x%02X,0x%04X]", idx, val);
236 ATOM_DEBUG_PRINT("WS[0x%02X]", idx);
238 case ATOM_WS_QUOTIENT:
239 val = gctx->divmul[0];
241 case ATOM_WS_REMAINDER:
242 val = gctx->divmul[1];
244 case ATOM_WS_DATAPTR:
245 val = gctx->data_block;
250 case ATOM_WS_OR_MASK:
251 val = 1 << gctx->shift;
253 case ATOM_WS_AND_MASK:
254 val = ~(1 << gctx->shift);
256 case ATOM_WS_FB_WINDOW:
259 case ATOM_WS_ATTRIBUTES:
263 val = gctx->reg_block;
273 if (gctx->data_block)
274 ATOM_DEBUG_PRINT("ID[0x%04X+%04X]", idx, gctx->data_block);
276 ATOM_DEBUG_PRINT("ID[0x%04X]", idx);
278 val = U32(idx + gctx->data_block);
283 if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
284 DRM_ERROR("ATOM: fb read beyond scratch region: %d vs. %d\n",
285 gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
288 val = gctx->scratch[(gctx->fb_base / 4) + idx];
290 ATOM_DEBUG_PRINT("FB[0x%02X]", idx);
298 ATOM_DEBUG_PRINT("IMM 0x%08X\n", val);
302 case ATOM_SRC_WORD16:
306 ATOM_DEBUG_PRINT("IMM 0x%04X\n", val);
310 case ATOM_SRC_BYTE16:
311 case ATOM_SRC_BYTE24:
315 ATOM_DEBUG_PRINT("IMM 0x%02X\n", val);
323 ATOM_DEBUG_PRINT("PLL[0x%02X]", idx);
324 val = gctx->card->pll_read(gctx->card, idx);
330 ATOM_DEBUG_PRINT("MC[0x%02X]", idx);
331 val = gctx->card->mc_read(gctx->card, idx);
336 val &= atom_arg_mask[align];
337 val >>= atom_arg_shift[align];
341 ATOM_DEBUG_PRINT(".[31:0] -> 0x%08X\n", val);
344 ATOM_DEBUG_PRINT(".[15:0] -> 0x%04X\n", val);
347 ATOM_DEBUG_PRINT(".[23:8] -> 0x%04X\n", val);
349 case ATOM_SRC_WORD16:
350 ATOM_DEBUG_PRINT(".[31:16] -> 0x%04X\n", val);
353 ATOM_DEBUG_PRINT(".[7:0] -> 0x%02X\n", val);
356 ATOM_DEBUG_PRINT(".[15:8] -> 0x%02X\n", val);
358 case ATOM_SRC_BYTE16:
359 ATOM_DEBUG_PRINT(".[23:16] -> 0x%02X\n", val);
361 case ATOM_SRC_BYTE24:
362 ATOM_DEBUG_PRINT(".[31:24] -> 0x%02X\n", val);
368 static void atom_skip_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr)
370 uint32_t align = (attr >> 3) & 7, arg = attr & 7;
390 case ATOM_SRC_WORD16:
395 case ATOM_SRC_BYTE16:
396 case ATOM_SRC_BYTE24:
404 static uint32_t atom_get_src(atom_exec_context *ctx, uint8_t attr, int *ptr)
406 return atom_get_src_int(ctx, attr, ptr, NULL, 1);
409 static uint32_t atom_get_src_direct(atom_exec_context *ctx, uint8_t align, int *ptr)
411 uint32_t val = 0xCDCDCDCD;
420 case ATOM_SRC_WORD16:
426 case ATOM_SRC_BYTE16:
427 case ATOM_SRC_BYTE24:
435 static uint32_t atom_get_dst(atom_exec_context *ctx, int arg, uint8_t attr,
436 int *ptr, uint32_t *saved, int print)
438 return atom_get_src_int(ctx,
439 arg | atom_dst_to_src[(attr >> 3) &
440 7][(attr >> 6) & 3] << 3,
444 static void atom_skip_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr)
446 atom_skip_src_int(ctx,
447 arg | atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) &
451 static void atom_put_dst(atom_exec_context *ctx, int arg, uint8_t attr,
452 int *ptr, uint32_t val, uint32_t saved)
455 atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3], old_val =
457 struct atom_context *gctx = ctx->ctx;
458 old_val &= atom_arg_mask[align] >> atom_arg_shift[align];
459 val <<= atom_arg_shift[align];
460 val &= atom_arg_mask[align];
461 saved &= ~atom_arg_mask[align];
467 ATOM_DEBUG_PRINT("REG[0x%04X]", idx);
468 idx += gctx->reg_block;
469 switch (gctx->io_mode) {
472 gctx->card->reg_write(gctx->card, idx,
475 gctx->card->reg_write(gctx->card, idx, val);
479 "PCI registers are not implemented.\n");
483 "SYSIO registers are not implemented.\n");
486 if (!(gctx->io_mode & 0x80)) {
487 DRM_INFO("Bad IO mode.\n");
490 if (!gctx->iio[gctx->io_mode & 0xFF]) {
492 "Undefined indirect IO write method %d.\n",
493 gctx->io_mode & 0x7F);
496 atom_iio_execute(gctx, gctx->iio[gctx->io_mode & 0xFF],
503 ATOM_DEBUG_PRINT("PS[0x%02X]", idx);
504 ctx->ps[idx] = cpu_to_le32(val);
509 ATOM_DEBUG_PRINT("WS[0x%02X]", idx);
511 case ATOM_WS_QUOTIENT:
512 gctx->divmul[0] = val;
514 case ATOM_WS_REMAINDER:
515 gctx->divmul[1] = val;
517 case ATOM_WS_DATAPTR:
518 gctx->data_block = val;
523 case ATOM_WS_OR_MASK:
524 case ATOM_WS_AND_MASK:
526 case ATOM_WS_FB_WINDOW:
529 case ATOM_WS_ATTRIBUTES:
533 gctx->reg_block = val;
542 if ((gctx->fb_base + (idx * 4)) > gctx->scratch_size_bytes) {
543 DRM_ERROR("ATOM: fb write beyond scratch region: %d vs. %d\n",
544 gctx->fb_base + (idx * 4), gctx->scratch_size_bytes);
546 gctx->scratch[(gctx->fb_base / 4) + idx] = val;
547 ATOM_DEBUG_PRINT("FB[0x%02X]", idx);
552 ATOM_DEBUG_PRINT("PLL[0x%02X]", idx);
553 gctx->card->pll_write(gctx->card, idx, val);
558 ATOM_DEBUG_PRINT("MC[0x%02X]", idx);
559 gctx->card->mc_write(gctx->card, idx, val);
564 ATOM_DEBUG_PRINT(".[31:0] <- 0x%08X\n", old_val);
567 ATOM_DEBUG_PRINT(".[15:0] <- 0x%04X\n", old_val);
570 ATOM_DEBUG_PRINT(".[23:8] <- 0x%04X\n", old_val);
572 case ATOM_SRC_WORD16:
573 ATOM_DEBUG_PRINT(".[31:16] <- 0x%04X\n", old_val);
576 ATOM_DEBUG_PRINT(".[7:0] <- 0x%02X\n", old_val);
579 ATOM_DEBUG_PRINT(".[15:8] <- 0x%02X\n", old_val);
581 case ATOM_SRC_BYTE16:
582 ATOM_DEBUG_PRINT(".[23:16] <- 0x%02X\n", old_val);
584 case ATOM_SRC_BYTE24:
585 ATOM_DEBUG_PRINT(".[31:24] <- 0x%02X\n", old_val);
590 static void atom_op_add(atom_exec_context *ctx, int *ptr, int arg)
592 uint8_t attr = U8((*ptr)++);
593 uint32_t dst, src, saved;
595 ATOM_SDEBUG_PRINT(" dst: ");
596 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
597 ATOM_SDEBUG_PRINT(" src: ");
598 src = atom_get_src(ctx, attr, ptr);
600 ATOM_SDEBUG_PRINT(" dst: ");
601 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
604 static void atom_op_and(atom_exec_context *ctx, int *ptr, int arg)
606 uint8_t attr = U8((*ptr)++);
607 uint32_t dst, src, saved;
609 ATOM_SDEBUG_PRINT(" dst: ");
610 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
611 ATOM_SDEBUG_PRINT(" src: ");
612 src = atom_get_src(ctx, attr, ptr);
614 ATOM_SDEBUG_PRINT(" dst: ");
615 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
618 static void atom_op_beep(atom_exec_context *ctx, int *ptr, int arg)
620 DRM_INFO("ATOM BIOS beeped!\n");
623 static void atom_op_calltable(atom_exec_context *ctx, int *ptr, int arg)
625 int idx = U8((*ptr)++);
628 if (idx < ATOM_TABLE_NAMES_CNT)
629 ATOM_SDEBUG_PRINT(" table: %d (%s)\n", idx, atom_table_names[idx]);
631 ATOM_SDEBUG_PRINT(" table: %d\n", idx);
632 if (U16(ctx->ctx->cmd_table + 4 + 2 * idx))
633 r = atom_execute_table_locked(ctx->ctx, idx, ctx->ps + ctx->ps_shift);
639 static void atom_op_clear(atom_exec_context *ctx, int *ptr, int arg)
641 uint8_t attr = U8((*ptr)++);
645 attr |= atom_def_dst[attr >> 3] << 6;
646 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
647 ATOM_SDEBUG_PRINT(" dst: ");
648 atom_put_dst(ctx, arg, attr, &dptr, 0, saved);
651 static void atom_op_compare(atom_exec_context *ctx, int *ptr, int arg)
653 uint8_t attr = U8((*ptr)++);
655 ATOM_SDEBUG_PRINT(" src1: ");
656 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
657 ATOM_SDEBUG_PRINT(" src2: ");
658 src = atom_get_src(ctx, attr, ptr);
659 ctx->ctx->cs_equal = (dst == src);
660 ctx->ctx->cs_above = (dst > src);
661 ATOM_SDEBUG_PRINT(" result: %s %s\n", ctx->ctx->cs_equal ? "EQ" : "NE",
662 ctx->ctx->cs_above ? "GT" : "LE");
665 static void atom_op_delay(atom_exec_context *ctx, int *ptr, int arg)
667 unsigned count = U8((*ptr)++);
668 ATOM_SDEBUG_PRINT(" count: %d\n", count);
669 if (arg == ATOM_UNIT_MICROSEC)
671 else if (!drm_can_sleep())
677 static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
679 uint8_t attr = U8((*ptr)++);
681 ATOM_SDEBUG_PRINT(" src1: ");
682 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
683 ATOM_SDEBUG_PRINT(" src2: ");
684 src = atom_get_src(ctx, attr, ptr);
686 ctx->ctx->divmul[0] = dst / src;
687 ctx->ctx->divmul[1] = dst % src;
689 ctx->ctx->divmul[0] = 0;
690 ctx->ctx->divmul[1] = 0;
694 static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
696 /* functionally, a nop */
699 static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
701 int execute = 0, target = U16(*ptr);
702 unsigned long cjiffies;
706 case ATOM_COND_ABOVE:
707 execute = ctx->ctx->cs_above;
709 case ATOM_COND_ABOVEOREQUAL:
710 execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
712 case ATOM_COND_ALWAYS:
715 case ATOM_COND_BELOW:
716 execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
718 case ATOM_COND_BELOWOREQUAL:
719 execute = !ctx->ctx->cs_above;
721 case ATOM_COND_EQUAL:
722 execute = ctx->ctx->cs_equal;
724 case ATOM_COND_NOTEQUAL:
725 execute = !ctx->ctx->cs_equal;
728 if (arg != ATOM_COND_ALWAYS)
729 ATOM_SDEBUG_PRINT(" taken: %s\n", execute ? "yes" : "no");
730 ATOM_SDEBUG_PRINT(" target: 0x%04X\n", target);
732 if (ctx->last_jump == (ctx->start + target)) {
734 if (time_after(cjiffies, ctx->last_jump_jiffies)) {
735 cjiffies -= ctx->last_jump_jiffies;
736 if ((jiffies_to_msecs(cjiffies) > 5000)) {
737 DRM_ERROR("atombios stuck in loop for more than 5secs aborting\n");
741 /* jiffies wrap around we will just wait a little longer */
742 ctx->last_jump_jiffies = jiffies;
745 ctx->last_jump = ctx->start + target;
746 ctx->last_jump_jiffies = jiffies;
748 *ptr = ctx->start + target;
752 static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
754 uint8_t attr = U8((*ptr)++);
755 uint32_t dst, mask, src, saved;
757 ATOM_SDEBUG_PRINT(" dst: ");
758 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
759 mask = atom_get_src_direct(ctx, ((attr >> 3) & 7), ptr);
760 ATOM_SDEBUG_PRINT(" mask: 0x%08x", mask);
761 ATOM_SDEBUG_PRINT(" src: ");
762 src = atom_get_src(ctx, attr, ptr);
765 ATOM_SDEBUG_PRINT(" dst: ");
766 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
769 static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
771 uint8_t attr = U8((*ptr)++);
774 if (((attr >> 3) & 7) != ATOM_SRC_DWORD)
775 atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
777 atom_skip_dst(ctx, arg, attr, ptr);
780 ATOM_SDEBUG_PRINT(" src: ");
781 src = atom_get_src(ctx, attr, ptr);
782 ATOM_SDEBUG_PRINT(" dst: ");
783 atom_put_dst(ctx, arg, attr, &dptr, src, saved);
786 static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
788 uint8_t attr = U8((*ptr)++);
790 ATOM_SDEBUG_PRINT(" src1: ");
791 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
792 ATOM_SDEBUG_PRINT(" src2: ");
793 src = atom_get_src(ctx, attr, ptr);
794 ctx->ctx->divmul[0] = dst * src;
797 static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
802 static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
804 uint8_t attr = U8((*ptr)++);
805 uint32_t dst, src, saved;
807 ATOM_SDEBUG_PRINT(" dst: ");
808 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
809 ATOM_SDEBUG_PRINT(" src: ");
810 src = atom_get_src(ctx, attr, ptr);
812 ATOM_SDEBUG_PRINT(" dst: ");
813 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
816 static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
818 uint8_t val = U8((*ptr)++);
819 ATOM_SDEBUG_PRINT("POST card output: 0x%02X\n", val);
822 static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
824 DRM_INFO("unimplemented!\n");
827 static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
829 DRM_INFO("unimplemented!\n");
832 static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
834 DRM_INFO("unimplemented!\n");
837 static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
841 ATOM_SDEBUG_PRINT(" block: %d\n", idx);
843 ctx->ctx->data_block = 0;
845 ctx->ctx->data_block = ctx->start;
847 ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
848 ATOM_SDEBUG_PRINT(" base: 0x%04X\n", ctx->ctx->data_block);
851 static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
853 uint8_t attr = U8((*ptr)++);
854 ATOM_SDEBUG_PRINT(" fb_base: ");
855 ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
858 static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
864 if (port < ATOM_IO_NAMES_CNT)
865 ATOM_SDEBUG_PRINT(" port: %d (%s)\n", port, atom_io_names[port]);
867 ATOM_SDEBUG_PRINT(" port: %d\n", port);
869 ctx->ctx->io_mode = ATOM_IO_MM;
871 ctx->ctx->io_mode = ATOM_IO_IIO | port;
875 ctx->ctx->io_mode = ATOM_IO_PCI;
878 case ATOM_PORT_SYSIO:
879 ctx->ctx->io_mode = ATOM_IO_SYSIO;
885 static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
887 ctx->ctx->reg_block = U16(*ptr);
889 ATOM_SDEBUG_PRINT(" base: 0x%04X\n", ctx->ctx->reg_block);
892 static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
894 uint8_t attr = U8((*ptr)++), shift;
898 attr |= atom_def_dst[attr >> 3] << 6;
899 ATOM_SDEBUG_PRINT(" dst: ");
900 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
901 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
902 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
904 ATOM_SDEBUG_PRINT(" dst: ");
905 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
908 static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
910 uint8_t attr = U8((*ptr)++), shift;
914 attr |= atom_def_dst[attr >> 3] << 6;
915 ATOM_SDEBUG_PRINT(" dst: ");
916 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
917 shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
918 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
920 ATOM_SDEBUG_PRINT(" dst: ");
921 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
924 static void atom_op_shl(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];
930 ATOM_SDEBUG_PRINT(" dst: ");
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 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
937 dst &= atom_arg_mask[dst_align];
938 dst >>= atom_arg_shift[dst_align];
939 ATOM_SDEBUG_PRINT(" dst: ");
940 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
943 static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
945 uint8_t attr = U8((*ptr)++), shift;
948 uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
949 ATOM_SDEBUG_PRINT(" dst: ");
950 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
951 /* op needs to full dst value */
953 shift = atom_get_src(ctx, attr, ptr);
954 ATOM_SDEBUG_PRINT(" shift: %d\n", shift);
956 dst &= atom_arg_mask[dst_align];
957 dst >>= atom_arg_shift[dst_align];
958 ATOM_SDEBUG_PRINT(" dst: ");
959 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
962 static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
964 uint8_t attr = U8((*ptr)++);
965 uint32_t dst, src, saved;
967 ATOM_SDEBUG_PRINT(" dst: ");
968 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
969 ATOM_SDEBUG_PRINT(" src: ");
970 src = atom_get_src(ctx, attr, ptr);
972 ATOM_SDEBUG_PRINT(" dst: ");
973 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
976 static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
978 uint8_t attr = U8((*ptr)++);
979 uint32_t src, val, target;
980 ATOM_SDEBUG_PRINT(" switch: ");
981 src = atom_get_src(ctx, attr, ptr);
982 while (U16(*ptr) != ATOM_CASE_END)
983 if (U8(*ptr) == ATOM_CASE_MAGIC) {
985 ATOM_SDEBUG_PRINT(" case: ");
987 atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
991 ATOM_SDEBUG_PRINT(" target: %04X\n", target);
992 *ptr = ctx->start + target;
997 DRM_INFO("Bad case.\n");
1003 static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
1005 uint8_t attr = U8((*ptr)++);
1007 ATOM_SDEBUG_PRINT(" src1: ");
1008 dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
1009 ATOM_SDEBUG_PRINT(" src2: ");
1010 src = atom_get_src(ctx, attr, ptr);
1011 ctx->ctx->cs_equal = ((dst & src) == 0);
1012 ATOM_SDEBUG_PRINT(" result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
1015 static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
1017 uint8_t attr = U8((*ptr)++);
1018 uint32_t dst, src, saved;
1020 ATOM_SDEBUG_PRINT(" dst: ");
1021 dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
1022 ATOM_SDEBUG_PRINT(" src: ");
1023 src = atom_get_src(ctx, attr, ptr);
1025 ATOM_SDEBUG_PRINT(" dst: ");
1026 atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
1029 static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
1031 DRM_INFO("unimplemented!\n");
1035 void (*func) (atom_exec_context *, int *, int);
1037 } opcode_table[ATOM_OP_CNT] = {
1040 atom_op_move, ATOM_ARG_REG}, {
1041 atom_op_move, ATOM_ARG_PS}, {
1042 atom_op_move, ATOM_ARG_WS}, {
1043 atom_op_move, ATOM_ARG_FB}, {
1044 atom_op_move, ATOM_ARG_PLL}, {
1045 atom_op_move, ATOM_ARG_MC}, {
1046 atom_op_and, ATOM_ARG_REG}, {
1047 atom_op_and, ATOM_ARG_PS}, {
1048 atom_op_and, ATOM_ARG_WS}, {
1049 atom_op_and, ATOM_ARG_FB}, {
1050 atom_op_and, ATOM_ARG_PLL}, {
1051 atom_op_and, ATOM_ARG_MC}, {
1052 atom_op_or, ATOM_ARG_REG}, {
1053 atom_op_or, ATOM_ARG_PS}, {
1054 atom_op_or, ATOM_ARG_WS}, {
1055 atom_op_or, ATOM_ARG_FB}, {
1056 atom_op_or, ATOM_ARG_PLL}, {
1057 atom_op_or, ATOM_ARG_MC}, {
1058 atom_op_shift_left, ATOM_ARG_REG}, {
1059 atom_op_shift_left, ATOM_ARG_PS}, {
1060 atom_op_shift_left, ATOM_ARG_WS}, {
1061 atom_op_shift_left, ATOM_ARG_FB}, {
1062 atom_op_shift_left, ATOM_ARG_PLL}, {
1063 atom_op_shift_left, ATOM_ARG_MC}, {
1064 atom_op_shift_right, ATOM_ARG_REG}, {
1065 atom_op_shift_right, ATOM_ARG_PS}, {
1066 atom_op_shift_right, ATOM_ARG_WS}, {
1067 atom_op_shift_right, ATOM_ARG_FB}, {
1068 atom_op_shift_right, ATOM_ARG_PLL}, {
1069 atom_op_shift_right, ATOM_ARG_MC}, {
1070 atom_op_mul, ATOM_ARG_REG}, {
1071 atom_op_mul, ATOM_ARG_PS}, {
1072 atom_op_mul, ATOM_ARG_WS}, {
1073 atom_op_mul, ATOM_ARG_FB}, {
1074 atom_op_mul, ATOM_ARG_PLL}, {
1075 atom_op_mul, ATOM_ARG_MC}, {
1076 atom_op_div, ATOM_ARG_REG}, {
1077 atom_op_div, ATOM_ARG_PS}, {
1078 atom_op_div, ATOM_ARG_WS}, {
1079 atom_op_div, ATOM_ARG_FB}, {
1080 atom_op_div, ATOM_ARG_PLL}, {
1081 atom_op_div, ATOM_ARG_MC}, {
1082 atom_op_add, ATOM_ARG_REG}, {
1083 atom_op_add, ATOM_ARG_PS}, {
1084 atom_op_add, ATOM_ARG_WS}, {
1085 atom_op_add, ATOM_ARG_FB}, {
1086 atom_op_add, ATOM_ARG_PLL}, {
1087 atom_op_add, ATOM_ARG_MC}, {
1088 atom_op_sub, ATOM_ARG_REG}, {
1089 atom_op_sub, ATOM_ARG_PS}, {
1090 atom_op_sub, ATOM_ARG_WS}, {
1091 atom_op_sub, ATOM_ARG_FB}, {
1092 atom_op_sub, ATOM_ARG_PLL}, {
1093 atom_op_sub, ATOM_ARG_MC}, {
1094 atom_op_setport, ATOM_PORT_ATI}, {
1095 atom_op_setport, ATOM_PORT_PCI}, {
1096 atom_op_setport, ATOM_PORT_SYSIO}, {
1097 atom_op_setregblock, 0}, {
1098 atom_op_setfbbase, 0}, {
1099 atom_op_compare, ATOM_ARG_REG}, {
1100 atom_op_compare, ATOM_ARG_PS}, {
1101 atom_op_compare, ATOM_ARG_WS}, {
1102 atom_op_compare, ATOM_ARG_FB}, {
1103 atom_op_compare, ATOM_ARG_PLL}, {
1104 atom_op_compare, ATOM_ARG_MC}, {
1105 atom_op_switch, 0}, {
1106 atom_op_jump, ATOM_COND_ALWAYS}, {
1107 atom_op_jump, ATOM_COND_EQUAL}, {
1108 atom_op_jump, ATOM_COND_BELOW}, {
1109 atom_op_jump, ATOM_COND_ABOVE}, {
1110 atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
1111 atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
1112 atom_op_jump, ATOM_COND_NOTEQUAL}, {
1113 atom_op_test, ATOM_ARG_REG}, {
1114 atom_op_test, ATOM_ARG_PS}, {
1115 atom_op_test, ATOM_ARG_WS}, {
1116 atom_op_test, ATOM_ARG_FB}, {
1117 atom_op_test, ATOM_ARG_PLL}, {
1118 atom_op_test, ATOM_ARG_MC}, {
1119 atom_op_delay, ATOM_UNIT_MILLISEC}, {
1120 atom_op_delay, ATOM_UNIT_MICROSEC}, {
1121 atom_op_calltable, 0}, {
1122 atom_op_repeat, 0}, {
1123 atom_op_clear, ATOM_ARG_REG}, {
1124 atom_op_clear, ATOM_ARG_PS}, {
1125 atom_op_clear, ATOM_ARG_WS}, {
1126 atom_op_clear, ATOM_ARG_FB}, {
1127 atom_op_clear, ATOM_ARG_PLL}, {
1128 atom_op_clear, ATOM_ARG_MC}, {
1131 atom_op_mask, ATOM_ARG_REG}, {
1132 atom_op_mask, ATOM_ARG_PS}, {
1133 atom_op_mask, ATOM_ARG_WS}, {
1134 atom_op_mask, ATOM_ARG_FB}, {
1135 atom_op_mask, ATOM_ARG_PLL}, {
1136 atom_op_mask, ATOM_ARG_MC}, {
1137 atom_op_postcard, 0}, {
1139 atom_op_savereg, 0}, {
1140 atom_op_restorereg, 0}, {
1141 atom_op_setdatablock, 0}, {
1142 atom_op_xor, ATOM_ARG_REG}, {
1143 atom_op_xor, ATOM_ARG_PS}, {
1144 atom_op_xor, ATOM_ARG_WS}, {
1145 atom_op_xor, ATOM_ARG_FB}, {
1146 atom_op_xor, ATOM_ARG_PLL}, {
1147 atom_op_xor, ATOM_ARG_MC}, {
1148 atom_op_shl, ATOM_ARG_REG}, {
1149 atom_op_shl, ATOM_ARG_PS}, {
1150 atom_op_shl, ATOM_ARG_WS}, {
1151 atom_op_shl, ATOM_ARG_FB}, {
1152 atom_op_shl, ATOM_ARG_PLL}, {
1153 atom_op_shl, ATOM_ARG_MC}, {
1154 atom_op_shr, ATOM_ARG_REG}, {
1155 atom_op_shr, ATOM_ARG_PS}, {
1156 atom_op_shr, ATOM_ARG_WS}, {
1157 atom_op_shr, ATOM_ARG_FB}, {
1158 atom_op_shr, ATOM_ARG_PLL}, {
1159 atom_op_shr, ATOM_ARG_MC}, {
1160 atom_op_debug, 0},};
1162 static int atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
1164 int base = CU16(ctx->cmd_table + 4 + 2 * index);
1165 int len, ws, ps, ptr;
1167 atom_exec_context ectx;
1173 len = CU16(base + ATOM_CT_SIZE_PTR);
1174 ws = CU8(base + ATOM_CT_WS_PTR);
1175 ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
1176 ptr = base + ATOM_CT_CODE_PTR;
1178 ATOM_SDEBUG_PRINT(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
1181 ectx.ps_shift = ps / 4;
1187 ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
1194 if (op < ATOM_OP_NAMES_CNT)
1195 ATOM_SDEBUG_PRINT("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
1197 ATOM_SDEBUG_PRINT("[%d] @ 0x%04X\n", op, ptr - 1);
1199 DRM_ERROR("atombios stuck executing %04X (len %d, WS %d, PS %d) @ 0x%04X\n",
1200 base, len, ws, ps, ptr - 1);
1205 if (op < ATOM_OP_CNT && op > 0)
1206 opcode_table[op].func(&ectx, &ptr,
1207 opcode_table[op].arg);
1211 if (op == ATOM_OP_EOT)
1215 ATOM_SDEBUG_PRINT("<<\n");
1223 int atom_execute_table_scratch_unlocked(struct atom_context *ctx, int index, uint32_t * params)
1227 lockmgr(&ctx->mutex, LK_EXCLUSIVE);
1228 /* reset data block */
1229 ctx->data_block = 0;
1230 /* reset reg block */
1232 /* reset fb window */
1235 ctx->io_mode = ATOM_IO_MM;
1239 r = atom_execute_table_locked(ctx, index, params);
1240 lockmgr(&ctx->mutex, LK_RELEASE);
1244 int atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
1247 lockmgr(&ctx->scratch_mutex, LK_EXCLUSIVE);
1248 r = atom_execute_table_scratch_unlocked(ctx, index, params);
1249 lockmgr(&ctx->scratch_mutex, LK_RELEASE);
1253 static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1255 static void atom_index_iio(struct atom_context *ctx, int base)
1257 ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
1260 while (CU8(base) == ATOM_IIO_START) {
1261 ctx->iio[CU8(base + 1)] = base + 2;
1263 while (CU8(base) != ATOM_IIO_END)
1264 base += atom_iio_len[CU8(base)];
1269 struct atom_context *atom_parse(struct card_info *card, void *bios)
1272 struct atom_context *ctx =
1273 kzalloc(sizeof(struct atom_context), GFP_KERNEL);
1284 if (CU16(0) != ATOM_BIOS_MAGIC) {
1285 DRM_INFO("Invalid BIOS magic.\n");
1290 (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
1291 strlen(ATOM_ATI_MAGIC))) {
1292 DRM_INFO("Invalid ATI magic.\n");
1297 base = CU16(ATOM_ROM_TABLE_PTR);
1299 (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
1300 strlen(ATOM_ROM_MAGIC))) {
1301 DRM_INFO("Invalid ATOM magic.\n");
1306 ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
1307 ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
1308 atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
1314 str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
1315 while (*str && ((*str == '\n') || (*str == '\r')))
1317 /* name string isn't always 0 terminated */
1318 for (i = 0; i < 511; i++) {
1320 if (name[i] < '.' || name[i] > 'z') {
1325 DRM_INFO("ATOM BIOS: %s\n", name);
1330 int atom_asic_init(struct atom_context *ctx)
1332 struct radeon_device *rdev = ctx->card->dev->dev_private;
1333 int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
1339 ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
1340 ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
1341 if (!ps[0] || !ps[1])
1344 if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
1346 ret = atom_execute_table(ctx, ATOM_CMD_INIT, ps);
1352 if (rdev->family < CHIP_R600) {
1353 if (CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_SPDFANCNTL))
1354 atom_execute_table(ctx, ATOM_CMD_SPDFANCNTL, ps);
1359 void atom_destroy(struct atom_context *ctx)
1365 bool atom_parse_data_header(struct atom_context *ctx, int index,
1366 uint16_t * size, uint8_t * frev, uint8_t * crev,
1367 uint16_t * data_start)
1369 int offset = index * 2 + 4;
1370 int idx = CU16(ctx->data_table + offset);
1371 u16 *mdt = (u16 *)((char *)ctx->bios + ctx->data_table + 4);
1379 *frev = CU8(idx + 2);
1381 *crev = CU8(idx + 3);
1386 bool atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
1389 int offset = index * 2 + 4;
1390 int idx = CU16(ctx->cmd_table + offset);
1391 u16 *mct = (u16 *)((char *)ctx->bios + ctx->cmd_table + 4);
1397 *frev = CU8(idx + 2);
1399 *crev = CU8(idx + 3);
1403 int atom_allocate_fb_scratch(struct atom_context *ctx)
1405 int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
1406 uint16_t data_offset;
1407 int usage_bytes = 0;
1408 struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
1410 if (atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
1411 firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)((char *)ctx->bios + data_offset);
1413 DRM_DEBUG("atom firmware requested %08x %dkb\n",
1414 le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
1415 le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
1417 usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
1419 ctx->scratch_size_bytes = 0;
1420 if (usage_bytes == 0)
1421 usage_bytes = 20 * 1024;
1422 /* allocate some scratch memory */
1423 ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
1426 ctx->scratch_size_bytes = usage_bytes;