OSDN Git Service

radeon: fix legacy LVDS
[android-x86/external-libdrm.git] / linux-core / atom.c
1 /*
2  * Copyright 2008 Advanced Micro Devices, Inc.  
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Author: Stanislaw Skowronek
23  */
24
25 #include <linux/module.h>
26 #include <linux/sched.h>
27
28 #define ATOM_DEBUG
29
30 #include "atom.h"
31 #include "atom-names.h"
32 #include "atom-bits.h"
33
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
41
42 #define ATOM_PORT_ATI   0
43 #define ATOM_PORT_PCI   1
44 #define ATOM_PORT_SYSIO 2
45
46 #define ATOM_UNIT_MICROSEC      0
47 #define ATOM_UNIT_MILLISEC      1
48
49 #define PLL_INDEX       2
50 #define PLL_DATA        3
51
52 typedef struct {
53     struct atom_context *ctx;
54
55     uint32_t *ps, *ws;
56     int ps_shift;
57     uint16_t start;
58 } atom_exec_context;
59
60 int atom_debug = 0;
61 void atom_execute_table(struct atom_context *ctx, int index, uint32_t *params);
62
63 static uint32_t atom_arg_mask[8] = {0xFFFFFFFF, 0xFFFF, 0xFFFF00, 0xFFFF0000, 0xFF, 0xFF00, 0xFF0000, 0xFF000000};
64 static int atom_arg_shift[8] = {0, 0, 8, 16, 0, 8, 16, 24};
65 static int atom_dst_to_src[8][4] = {    // translate destination alignment field to the source alignment encoding
66     { 0, 0, 0, 0 },
67     { 1, 2, 3, 0 },
68     { 1, 2, 3, 0 },
69     { 1, 2, 3, 0 },
70     { 4, 5, 6, 7 },
71     { 4, 5, 6, 7 },
72     { 4, 5, 6, 7 },
73     { 4, 5, 6, 7 },
74 };
75 static int atom_def_dst[8] = { 0, 0, 1, 2, 0, 1, 2, 3 };
76
77 static int debug_depth = 0;
78 #ifdef ATOM_DEBUG
79 static void debug_print_spaces(int n)
80 {
81     while(n--)
82         printk("   ");
83 }
84 #define DEBUG(...) do if(atom_debug) { printk(KERN_DEBUG __VA_ARGS__); } while(0)
85 #define SDEBUG(...) do if(atom_debug) { printk(KERN_DEBUG); debug_print_spaces(debug_depth); printk(__VA_ARGS__); } while(0)
86 #else
87 #define DEBUG(...) do { } while(0)
88 #define SDEBUG(...) do { } while(0)
89 #endif
90
91 static uint32_t atom_iio_execute(struct atom_context *ctx, int base, uint32_t index, uint32_t data)
92 {
93     uint32_t temp = 0xCDCDCDCD;
94     while(1)
95         switch(CU8(base)) {
96         case ATOM_IIO_NOP:
97             base++;
98             break;
99         case ATOM_IIO_READ:
100             temp = ctx->card->reg_read(ctx->card, CU16(base+1));
101             base+=3;
102             break;
103         case ATOM_IIO_WRITE:
104             ctx->card->reg_write(ctx->card, CU16(base+1), temp);
105             base+=3;
106             break;
107         case ATOM_IIO_CLEAR:
108             temp &= ~((0xFFFFFFFF >> (32-CU8(base+1))) << CU8(base+2));
109             base+=3;
110             break;
111         case ATOM_IIO_SET:
112             temp |= (0xFFFFFFFF >> (32-CU8(base+1))) << CU8(base+2);
113             base+=3;
114             break;
115         case ATOM_IIO_MOVE_INDEX:
116             temp &= ~((0xFFFFFFFF >> (32-CU8(base+1))) << CU8(base+2));
117             temp |= ((index >> CU8(base+2)) & (0xFFFFFFFF >> (32-CU8(base+1)))) << CU8(base+3);
118             base+=4;
119             break;
120         case ATOM_IIO_MOVE_DATA:
121             temp &= ~((0xFFFFFFFF >> (32-CU8(base+1))) << CU8(base+2));
122             temp |= ((data >> CU8(base+2)) & (0xFFFFFFFF >> (32-CU8(base+1)))) << CU8(base+3);
123             base+=4;
124             break;
125         case ATOM_IIO_MOVE_ATTR:
126             temp &= ~((0xFFFFFFFF >> (32-CU8(base+1))) << CU8(base+2));
127             temp |= ((ctx->io_attr >> CU8(base+2)) & (0xFFFFFFFF >> (32-CU8(base+1)))) << CU8(base+3);
128             base+=4;
129             break;
130         case ATOM_IIO_END:
131             return temp;
132         default:
133             printk(KERN_INFO "Unknown IIO opcode.\n");
134             return 0;
135         }
136 }
137
138 static uint32_t atom_get_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr, uint32_t *saved, int print)
139 {
140     uint32_t idx, val = 0xCDCDCDCD, align, arg;
141     struct atom_context *gctx = ctx->ctx;
142     arg = attr & 7;
143     align = (attr >> 3) & 7;
144     switch(arg) {
145     case ATOM_ARG_REG:
146         idx = U16(*ptr);
147         (*ptr)+=2;
148         if(print)
149             DEBUG("REG[0x%04X]", idx);
150         idx += gctx->reg_block;
151         switch(gctx->io_mode) {
152         case ATOM_IO_MM:
153             val = gctx->card->reg_read(gctx->card, idx);
154             break;
155         case ATOM_IO_PCI:
156             printk(KERN_INFO "PCI registers are not implemented.\n");
157             return 0;
158         case ATOM_IO_SYSIO:
159             printk(KERN_INFO "SYSIO registers are not implemented.\n");
160             return 0;
161         default:
162             if(!(gctx->io_mode&0x80)) {
163                 printk(KERN_INFO "Bad IO mode.\n");
164                 return 0;
165             }
166             if(!gctx->iio[gctx->io_mode&0x7F]) {
167                 printk(KERN_INFO "Undefined indirect IO read method %d.\n", gctx->io_mode&0x7F);
168                 return 0;
169             }
170             val = atom_iio_execute(gctx, gctx->iio[gctx->io_mode&0x7F], idx, 0);
171         }
172         break;
173     case ATOM_ARG_PS:
174         idx = U8(*ptr);
175         (*ptr)++;
176         val = ctx->ps[idx];
177         if(print)
178             DEBUG("PS[0x%02X,0x%04X]", idx, val);
179         break;
180     case ATOM_ARG_WS:
181         idx = U8(*ptr);
182         (*ptr)++;
183         if(print)
184             DEBUG("WS[0x%02X]", idx);
185         switch(idx) {
186         case ATOM_WS_QUOTIENT:
187             val = gctx->divmul[0];
188             break;
189         case ATOM_WS_REMAINDER:
190             val = gctx->divmul[1];
191             break;
192         case ATOM_WS_DATAPTR:
193             val = gctx->data_block;
194             break;
195         case ATOM_WS_SHIFT:
196             val = gctx->shift;
197             break;
198         case ATOM_WS_OR_MASK:
199             val = 1<<gctx->shift;
200             break;
201         case ATOM_WS_AND_MASK:
202             val = ~(1<<gctx->shift);
203             break;
204         case ATOM_WS_FB_WINDOW:
205             val = gctx->fb_base;
206             break;
207         case ATOM_WS_ATTRIBUTES:
208             val = gctx->io_attr;
209             break;
210         default:
211             val = ctx->ws[idx];
212         }
213         break;
214     case ATOM_ARG_ID:
215         idx = U16(*ptr);
216         (*ptr)+=2;
217         if(print) {
218             if(gctx->data_block)
219                 DEBUG("ID[0x%04X+%04X]", idx, gctx->data_block);
220             else
221                 DEBUG("ID[0x%04X]", idx);
222         }
223         val = U32(idx + gctx->data_block);
224         break;
225     case ATOM_ARG_FB:
226         idx = U8(*ptr);
227         (*ptr)++;
228         if(print)
229             DEBUG("FB[0x%02X]", idx);
230         printk(KERN_INFO "FB access is not implemented.\n");
231         return 0;
232     case ATOM_ARG_IMM:
233         switch(align) {
234         case ATOM_SRC_DWORD:
235             val = U32(*ptr);
236             (*ptr)+=4;
237             if(print)
238                 DEBUG("IMM 0x%08X\n", val);
239             return val;
240         case ATOM_SRC_WORD0:
241         case ATOM_SRC_WORD8:
242         case ATOM_SRC_WORD16:
243             val = U16(*ptr);
244             (*ptr)+=2;
245             if(print)
246                 DEBUG("IMM 0x%04X\n", val);
247             return val;
248         case ATOM_SRC_BYTE0:
249         case ATOM_SRC_BYTE8:
250         case ATOM_SRC_BYTE16:
251         case ATOM_SRC_BYTE24:
252             val = U8(*ptr);
253             (*ptr)++;
254             if(print)
255                 DEBUG("IMM 0x%02X\n", val);
256             return val;
257         }
258         return 0;
259     case ATOM_ARG_PLL:
260         idx = U8(*ptr);
261         (*ptr)++;
262         if(print)
263             DEBUG("PLL[0x%02X]", idx);
264         gctx->card->reg_write(gctx->card, PLL_INDEX, idx);
265         val = gctx->card->reg_read(gctx->card, PLL_DATA);
266         break;
267     case ATOM_ARG_MC:
268         idx = U8(*ptr);
269         (*ptr)++;
270         if(print)
271             DEBUG("MC[0x%02X]", idx);
272         val = gctx->card->mc_read(gctx->card, idx);
273         printk(KERN_INFO "MC registers are not implemented.\n");
274         return 0;
275     }
276     if(saved)
277         *saved = val;
278     val &= atom_arg_mask[align];
279     val >>= atom_arg_shift[align];
280     if(print)
281         switch(align) {
282         case ATOM_SRC_DWORD:
283             DEBUG(".[31:0] -> 0x%08X\n", val);
284             break;
285         case ATOM_SRC_WORD0:
286             DEBUG(".[15:0] -> 0x%04X\n", val);
287             break;
288         case ATOM_SRC_WORD8:
289             DEBUG(".[23:8] -> 0x%04X\n", val);
290             break;
291         case ATOM_SRC_WORD16:
292             DEBUG(".[31:16] -> 0x%04X\n", val);
293             break;
294         case ATOM_SRC_BYTE0:
295             DEBUG(".[7:0] -> 0x%02X\n", val);
296             break;
297         case ATOM_SRC_BYTE8:
298             DEBUG(".[15:8] -> 0x%02X\n", val);
299             break;
300         case ATOM_SRC_BYTE16:
301             DEBUG(".[23:16] -> 0x%02X\n", val);
302             break;
303         case ATOM_SRC_BYTE24:
304             DEBUG(".[31:24] -> 0x%02X\n", val);
305             break;
306         }
307     return val;
308 }
309
310 static void atom_skip_src_int(atom_exec_context *ctx, uint8_t attr, int *ptr)
311 {
312     uint32_t align = (attr >> 3) & 7, arg = attr & 7;
313     switch(arg) {
314     case ATOM_ARG_REG:
315     case ATOM_ARG_ID:
316         (*ptr)+=2;
317         break;
318     case ATOM_ARG_PLL:
319     case ATOM_ARG_MC:
320     case ATOM_ARG_PS:
321     case ATOM_ARG_WS:
322     case ATOM_ARG_FB:
323         (*ptr)++;
324         break;
325     case ATOM_ARG_IMM:
326         switch(align) {
327         case ATOM_SRC_DWORD:
328             (*ptr)+=4;
329             return;
330         case ATOM_SRC_WORD0:
331         case ATOM_SRC_WORD8:
332         case ATOM_SRC_WORD16:
333             (*ptr)+=2;
334             return;
335         case ATOM_SRC_BYTE0:
336         case ATOM_SRC_BYTE8:
337         case ATOM_SRC_BYTE16:
338         case ATOM_SRC_BYTE24:
339             (*ptr)++;
340             return;
341         }
342         return;
343     }
344 }
345
346 static uint32_t atom_get_src(atom_exec_context *ctx, uint8_t attr, int *ptr)
347 {
348     return atom_get_src_int(ctx, attr, ptr, NULL, 1);
349 }
350
351 static uint32_t atom_get_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr, uint32_t *saved, int print)
352 {
353     return atom_get_src_int(ctx, arg|atom_dst_to_src[(attr>>3)&7][(attr>>6)&3]<<3, ptr, saved, print);
354 }
355
356 static void atom_skip_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr)
357 {
358     atom_skip_src_int(ctx, arg|atom_dst_to_src[(attr>>3)&7][(attr>>6)&3]<<3, ptr);
359 }
360
361 static void atom_put_dst(atom_exec_context *ctx, int arg, uint8_t attr, int *ptr, uint32_t val, uint32_t saved)
362 {
363     uint32_t align = atom_dst_to_src[(attr>>3)&7][(attr>>6)&3], old_val = val, idx;
364     struct atom_context *gctx = ctx->ctx;
365     old_val &= atom_arg_mask[align] >> atom_arg_shift[align];
366     val <<= atom_arg_shift[align];
367     val &= atom_arg_mask[align];
368     saved &= ~atom_arg_mask[align];
369     val |= saved;
370     switch(arg) {
371     case ATOM_ARG_REG:
372         idx = U16(*ptr);
373         (*ptr)+=2;
374         DEBUG("REG[0x%04X]", idx);
375         idx += gctx->reg_block;
376         switch(gctx->io_mode) {
377         case ATOM_IO_MM:
378             if(idx == 0)
379                 gctx->card->reg_write(gctx->card, idx, val<<2);
380             else
381                 gctx->card->reg_write(gctx->card, idx, val);
382             break;
383         case ATOM_IO_PCI:
384             printk(KERN_INFO "PCI registers are not implemented.\n");
385             return;
386         case ATOM_IO_SYSIO:
387             printk(KERN_INFO "SYSIO registers are not implemented.\n");
388             return;
389         default:
390             if(!(gctx->io_mode&0x80)) {
391                 printk(KERN_INFO "Bad IO mode.\n");
392                 return;
393             }
394             if(!gctx->iio[gctx->io_mode&0xFF]) {
395                 printk(KERN_INFO "Undefined indirect IO write method %d.\n", gctx->io_mode&0x7F);
396                 return;
397             }
398             atom_iio_execute(gctx, gctx->iio[gctx->io_mode&0xFF], idx, val);
399         }
400         break;
401     case ATOM_ARG_PS:
402         idx = U8(*ptr);
403         (*ptr)++;
404         DEBUG("PS[0x%02X]", idx);
405         ctx->ps[idx] = val;
406         break;
407     case ATOM_ARG_WS:
408         idx = U8(*ptr);
409         (*ptr)++;
410         DEBUG("WS[0x%02X]", idx);
411         switch(idx) {
412         case ATOM_WS_QUOTIENT:
413             gctx->divmul[0] = val;
414             break;
415         case ATOM_WS_REMAINDER:
416             gctx->divmul[1] = val;
417             break;
418         case ATOM_WS_DATAPTR:
419             gctx->data_block = val;
420             break;
421         case ATOM_WS_SHIFT:
422             gctx->shift = val;
423             break;
424         case ATOM_WS_OR_MASK:
425         case ATOM_WS_AND_MASK:
426             break;
427         case ATOM_WS_FB_WINDOW:
428             gctx->fb_base = val;
429             break;
430         case ATOM_WS_ATTRIBUTES:
431             gctx->io_attr = val;
432             break;
433         default:
434             ctx->ws[idx] = val;
435         }
436         break;
437     case ATOM_ARG_FB:
438         idx = U8(*ptr);
439         (*ptr)++;
440         DEBUG("FB[0x%02X]", idx);
441         printk(KERN_INFO "FB access is not implemented.\n");
442         return;
443     case ATOM_ARG_PLL:
444         idx = U8(*ptr);
445         (*ptr)++;
446         DEBUG("PLL[0x%02X]", idx);
447         gctx->card->reg_write(gctx->card, PLL_INDEX, idx);
448         gctx->card->reg_write(gctx->card, PLL_DATA, val);
449         break;
450     case ATOM_ARG_MC:
451         idx = U8(*ptr);
452         (*ptr)++;
453         DEBUG("MC[0x%02X]", idx);
454         gctx->card->mc_write(gctx->card, idx, val);
455         printk(KERN_INFO "MC registers are not implemented.\n");
456         return;
457     }
458     switch(align) {
459     case ATOM_SRC_DWORD:
460         DEBUG(".[31:0] <- 0x%08X\n", old_val);
461         break;
462     case ATOM_SRC_WORD0:
463         DEBUG(".[15:0] <- 0x%04X\n", old_val);
464         break;
465     case ATOM_SRC_WORD8:
466         DEBUG(".[23:8] <- 0x%04X\n", old_val);
467         break;
468     case ATOM_SRC_WORD16:
469         DEBUG(".[31:16] <- 0x%04X\n", old_val);
470         break;
471     case ATOM_SRC_BYTE0:
472         DEBUG(".[7:0] <- 0x%02X\n", old_val);
473         break;
474     case ATOM_SRC_BYTE8:
475         DEBUG(".[15:8] <- 0x%02X\n", old_val);
476         break;
477     case ATOM_SRC_BYTE16:
478         DEBUG(".[23:16] <- 0x%02X\n", old_val);
479         break;
480     case ATOM_SRC_BYTE24:
481         DEBUG(".[31:24] <- 0x%02X\n", old_val);
482         break;
483     }
484 }
485
486 static void atom_op_add(atom_exec_context *ctx, int *ptr, int arg)
487 {
488     uint8_t attr = U8((*ptr)++);
489     uint32_t dst, src, saved;
490     int dptr = *ptr;
491     SDEBUG("   dst: ");
492     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
493     SDEBUG("   src: ");
494     src = atom_get_src(ctx, attr, ptr);
495     dst += src;
496     SDEBUG("   dst: ");
497     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
498 }
499
500 static void atom_op_and(atom_exec_context *ctx, int *ptr, int arg)
501 {
502     uint8_t attr = U8((*ptr)++);
503     uint32_t dst, src, saved;
504     int dptr = *ptr;
505     SDEBUG("   dst: ");
506     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
507     SDEBUG("   src: ");
508     src = atom_get_src(ctx, attr, ptr);
509     dst &= src;
510     SDEBUG("   dst: ");
511     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
512 }
513
514 static void atom_op_beep(atom_exec_context *ctx, int *ptr, int arg)
515 {
516     printk("ATOM BIOS beeped!\n");
517 }
518
519 static void atom_op_calltable(atom_exec_context *ctx, int *ptr, int arg)
520 {
521     int idx = U8((*ptr)++);
522     if(idx < ATOM_TABLE_NAMES_CNT)
523         SDEBUG("   table: %d (%s)\n", idx, atom_table_names[idx]);
524     else
525         SDEBUG("   table: %d\n", idx);
526     if(U16(ctx->ctx->cmd_table + 4 + 2*idx))
527         atom_execute_table(ctx->ctx, idx, ctx->ps+ctx->ps_shift);
528 }
529
530 static void atom_op_clear(atom_exec_context *ctx, int *ptr, int arg)
531 {
532     uint8_t attr = U8((*ptr)++);
533     uint32_t saved;
534     int dptr = *ptr;
535     attr &= 0x38;
536     attr |= atom_def_dst[attr>>3]<<6;
537     atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
538     SDEBUG("   dst: ");
539     atom_put_dst(ctx, arg, attr, &dptr, 0, saved);
540 }
541
542 static void atom_op_compare(atom_exec_context *ctx, int *ptr, int arg)
543 {
544     uint8_t attr = U8((*ptr)++);
545     uint32_t dst, src;
546     SDEBUG("   src1: ");
547     dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
548     SDEBUG("   src2: ");
549     src = atom_get_src(ctx, attr, ptr);
550     ctx->ctx->cs_equal = (dst == src);
551     ctx->ctx->cs_above = (dst > src);
552     SDEBUG("   result: %s %s\n", ctx->ctx->cs_equal?"EQ":"NE", ctx->ctx->cs_above?"GT":"LE");
553 }
554
555 static void atom_op_delay(atom_exec_context *ctx, int *ptr, int arg)
556 {
557     uint8_t count = U8((*ptr)++);
558     SDEBUG("   count: %d\n", count);
559     if(arg == ATOM_UNIT_MICROSEC)
560         schedule_timeout_uninterruptible(usecs_to_jiffies(count));
561     else
562         schedule_timeout_uninterruptible(msecs_to_jiffies(count));
563 }
564
565 static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
566 {
567     uint8_t attr = U8((*ptr)++);
568     uint32_t dst, src;
569     SDEBUG("   src1: ");
570     dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
571     SDEBUG("   src2: ");
572     src = atom_get_src(ctx, attr, ptr);
573     if(src != 0) {
574         ctx->ctx->divmul[0] = dst/src;
575         ctx->ctx->divmul[1] = dst%src;
576     } else {
577         ctx->ctx->divmul[0] = 0;
578         ctx->ctx->divmul[1] = 0;
579     }
580 }
581
582 static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
583 {
584     /* functionally, a nop */
585 }
586
587 static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
588 {
589     int execute = 0, target = U16(*ptr);
590     (*ptr)+=2;
591     switch(arg) {
592     case ATOM_COND_ABOVE:
593         execute = ctx->ctx->cs_above;
594         break;
595     case ATOM_COND_ABOVEOREQUAL:
596         execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
597         break;
598     case ATOM_COND_ALWAYS:
599         execute = 1;
600         break;
601     case ATOM_COND_BELOW:
602         execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
603         break;
604     case ATOM_COND_BELOWOREQUAL:
605         execute = !ctx->ctx->cs_above;
606         break;
607     case ATOM_COND_EQUAL:
608         execute = ctx->ctx->cs_equal;
609         break;
610     case ATOM_COND_NOTEQUAL:
611         execute = !ctx->ctx->cs_equal;
612         break;
613     }
614     if(arg != ATOM_COND_ALWAYS)
615         SDEBUG("   taken: %s\n", execute?"yes":"no");
616     SDEBUG("   target: 0x%04X\n", target);
617     if(execute)
618         *ptr = ctx->start+target;
619 }
620
621 static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
622 {
623     uint8_t attr = U8((*ptr)++);
624     uint32_t dst, src1, src2, saved;
625     int dptr = *ptr;
626     SDEBUG("   dst: ");
627     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
628     SDEBUG("   src1: ");
629     src1 = atom_get_src(ctx, attr, ptr);
630     SDEBUG("   src2: ");
631     src2 = atom_get_src(ctx, attr, ptr);
632     dst &= src1;
633     dst |= src2;
634     SDEBUG("   dst: ");
635     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
636 }
637
638 static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
639 {
640     uint8_t attr = U8((*ptr)++);
641     uint32_t src, saved;
642     int dptr = *ptr;
643     if(((attr>>3)&7) != ATOM_SRC_DWORD)
644         atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
645     else {
646         atom_skip_dst(ctx, arg, attr, ptr);
647         saved = 0xCDCDCDCD;
648     }
649     SDEBUG("   src: ");
650     src = atom_get_src(ctx, attr, ptr);
651     SDEBUG("   dst: ");
652     atom_put_dst(ctx, arg, attr, &dptr, src, saved);
653 }
654
655 static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
656 {
657     uint8_t attr = U8((*ptr)++);
658     uint32_t dst, src;
659     SDEBUG("   src1: ");
660     dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
661     SDEBUG("   src2: ");
662     src = atom_get_src(ctx, attr, ptr);
663     ctx->ctx->divmul[0] = dst*src;
664 }
665
666 static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
667 {
668     /* nothing */
669 }
670
671 static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
672 {
673     uint8_t attr = U8((*ptr)++);
674     uint32_t dst, src, saved;
675     int dptr = *ptr;
676     SDEBUG("   dst: ");
677     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
678     SDEBUG("   src: ");
679     src = atom_get_src(ctx, attr, ptr);
680     dst |= src;
681     SDEBUG("   dst: ");
682     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
683 }
684
685 static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
686 {
687     uint8_t val = U8((*ptr)++);
688     SDEBUG("POST card output: 0x%02X\n", val);
689 }
690
691 static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
692 {
693     printk(KERN_INFO "unimplemented!\n");
694 }
695
696 static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
697 {
698     printk(KERN_INFO "unimplemented!\n");
699 }
700
701 static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
702 {
703     printk(KERN_INFO "unimplemented!\n");
704 }
705
706 static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
707 {
708     int idx = U8(*ptr);
709     (*ptr)++;
710     SDEBUG("   block: %d\n", idx);
711     if(!idx)
712         ctx->ctx->data_block = 0;
713     else if(idx==255)
714         ctx->ctx->data_block = ctx->start;
715     else
716         ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2*idx);
717     SDEBUG("   base: 0x%04X\n", ctx->ctx->data_block);
718 }
719
720 static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
721 {
722     uint8_t attr = U8((*ptr)++);
723     SDEBUG("   fb_base: ");
724     ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
725 }
726
727 static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
728 {
729     int port;
730     switch(arg) {
731     case ATOM_PORT_ATI:
732         port = U16(*ptr);
733         if(port < ATOM_IO_NAMES_CNT)
734                 SDEBUG("   port: %d (%s)\n", port, atom_io_names[port]);
735         else
736                 SDEBUG("   port: %d\n", port);
737         if(!port)
738             ctx->ctx->io_mode = ATOM_IO_MM;
739         else
740             ctx->ctx->io_mode = ATOM_IO_IIO|port;
741         (*ptr)+=2;
742         break;
743     case ATOM_PORT_PCI:
744         ctx->ctx->io_mode = ATOM_IO_PCI;
745         (*ptr)++;
746         break;
747     case ATOM_PORT_SYSIO:
748         ctx->ctx->io_mode = ATOM_IO_SYSIO;
749         (*ptr)++;
750         break;
751     }
752 }
753
754 static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
755 {
756     ctx->ctx->reg_block = U16(*ptr);
757     (*ptr)+=2;
758     SDEBUG("   base: 0x%04X\n", ctx->ctx->reg_block);
759 }
760
761 static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
762 {
763     uint8_t attr = U8((*ptr)++), shift;
764     uint32_t saved, dst;
765     int dptr = *ptr;
766     attr &= 0x38;
767     attr |= atom_def_dst[attr>>3]<<6;
768     SDEBUG("   dst: ");
769     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
770     shift = U8((*ptr)++);
771     SDEBUG("   shift: %d\n", shift);
772     dst <<= shift;
773     SDEBUG("   dst: ");
774     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
775 }
776
777 static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
778 {
779     uint8_t attr = U8((*ptr)++), shift;
780     uint32_t saved, dst;
781     int dptr = *ptr;
782     attr &= 0x38;
783     attr |= atom_def_dst[attr>>3]<<6;
784     SDEBUG("   dst: ");
785     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
786     shift = U8((*ptr)++);
787     SDEBUG("   shift: %d\n", shift);
788     dst >>= shift;
789     SDEBUG("   dst: ");
790     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
791 }
792
793 static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
794 {
795     uint8_t attr = U8((*ptr)++);
796     uint32_t dst, src, saved;
797     int dptr = *ptr;
798     SDEBUG("   dst: ");
799     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
800     SDEBUG("   src: ");
801     src = atom_get_src(ctx, attr, ptr);
802     dst -= src;
803     SDEBUG("   dst: ");
804     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
805 }
806
807 static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
808 {
809     uint8_t attr = U8((*ptr)++);
810     uint32_t src, val, target;
811     SDEBUG("   switch: ");
812     src = atom_get_src(ctx, attr, ptr);
813     while(U16(*ptr) != ATOM_CASE_END)
814         if(U8(*ptr) == ATOM_CASE_MAGIC) {
815             (*ptr)++;
816             SDEBUG("   case: ");
817             val = atom_get_src(ctx, (attr&0x38)|ATOM_ARG_IMM, ptr);
818             target = U16(*ptr);
819             if(val == src) {
820                 SDEBUG("   target: %04X\n", target);
821                 *ptr = ctx->start+target;
822                 return;
823             }
824             (*ptr) += 2;
825         } else {
826             printk(KERN_INFO "Bad case.\n");
827             return;
828         }
829     (*ptr) += 2;
830 }
831
832 static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
833 {
834     uint8_t attr = U8((*ptr)++);
835     uint32_t dst, src;
836     SDEBUG("   src1: ");
837     dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
838     SDEBUG("   src2: ");
839     src = atom_get_src(ctx, attr, ptr);
840     ctx->ctx->cs_equal = ((dst & src) == 0);
841     SDEBUG("   result: %s\n", ctx->ctx->cs_equal?"EQ":"NE");
842 }
843
844 static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
845 {
846     uint8_t attr = U8((*ptr)++);
847     uint32_t dst, src, saved;
848     int dptr = *ptr;
849     SDEBUG("   dst: ");
850     dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
851     SDEBUG("   src: ");
852     src = atom_get_src(ctx, attr, ptr);
853     dst ^= src;
854     SDEBUG("   dst: ");
855     atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
856 }
857
858 static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
859 {
860     printk(KERN_INFO "unimplemented!\n");
861 }
862
863 static struct {
864     void (*func)(atom_exec_context *, int *, int);
865     int arg;
866 } opcode_table[ATOM_OP_CNT] = {
867     { NULL, 0 },
868     { atom_op_move, ATOM_ARG_REG },
869     { atom_op_move, ATOM_ARG_PS },
870     { atom_op_move, ATOM_ARG_WS },
871     { atom_op_move, ATOM_ARG_FB },
872     { atom_op_move, ATOM_ARG_PLL },
873     { atom_op_move, ATOM_ARG_MC },
874     { atom_op_and, ATOM_ARG_REG },
875     { atom_op_and, ATOM_ARG_PS },
876     { atom_op_and, ATOM_ARG_WS },
877     { atom_op_and, ATOM_ARG_FB },
878     { atom_op_and, ATOM_ARG_PLL },
879     { atom_op_and, ATOM_ARG_MC },
880     { atom_op_or, ATOM_ARG_REG },
881     { atom_op_or, ATOM_ARG_PS },
882     { atom_op_or, ATOM_ARG_WS },
883     { atom_op_or, ATOM_ARG_FB },
884     { atom_op_or, ATOM_ARG_PLL },
885     { atom_op_or, ATOM_ARG_MC },
886     { atom_op_shl, ATOM_ARG_REG },
887     { atom_op_shl, ATOM_ARG_PS },
888     { atom_op_shl, ATOM_ARG_WS },
889     { atom_op_shl, ATOM_ARG_FB },
890     { atom_op_shl, ATOM_ARG_PLL },
891     { atom_op_shl, ATOM_ARG_MC },
892     { atom_op_shr, ATOM_ARG_REG },
893     { atom_op_shr, ATOM_ARG_PS },
894     { atom_op_shr, ATOM_ARG_WS },
895     { atom_op_shr, ATOM_ARG_FB },
896     { atom_op_shr, ATOM_ARG_PLL },
897     { atom_op_shr, ATOM_ARG_MC },
898     { atom_op_mul, ATOM_ARG_REG },
899     { atom_op_mul, ATOM_ARG_PS },
900     { atom_op_mul, ATOM_ARG_WS },
901     { atom_op_mul, ATOM_ARG_FB },
902     { atom_op_mul, ATOM_ARG_PLL },
903     { atom_op_mul, ATOM_ARG_MC },
904     { atom_op_div, ATOM_ARG_REG },
905     { atom_op_div, ATOM_ARG_PS },
906     { atom_op_div, ATOM_ARG_WS },
907     { atom_op_div, ATOM_ARG_FB },
908     { atom_op_div, ATOM_ARG_PLL },
909     { atom_op_div, ATOM_ARG_MC },
910     { atom_op_add, ATOM_ARG_REG },
911     { atom_op_add, ATOM_ARG_PS },
912     { atom_op_add, ATOM_ARG_WS },
913     { atom_op_add, ATOM_ARG_FB },
914     { atom_op_add, ATOM_ARG_PLL },
915     { atom_op_add, ATOM_ARG_MC },
916     { atom_op_sub, ATOM_ARG_REG },
917     { atom_op_sub, ATOM_ARG_PS },
918     { atom_op_sub, ATOM_ARG_WS },
919     { atom_op_sub, ATOM_ARG_FB },
920     { atom_op_sub, ATOM_ARG_PLL },
921     { atom_op_sub, ATOM_ARG_MC },
922     { atom_op_setport, ATOM_PORT_ATI },
923     { atom_op_setport, ATOM_PORT_PCI },
924     { atom_op_setport, ATOM_PORT_SYSIO },
925     { atom_op_setregblock, 0 },
926     { atom_op_setfbbase, 0 },
927     { atom_op_compare, ATOM_ARG_REG },
928     { atom_op_compare, ATOM_ARG_PS },
929     { atom_op_compare, ATOM_ARG_WS },
930     { atom_op_compare, ATOM_ARG_FB },
931     { atom_op_compare, ATOM_ARG_PLL },
932     { atom_op_compare, ATOM_ARG_MC },
933     { atom_op_switch, 0 },
934     { atom_op_jump, ATOM_COND_ALWAYS },
935     { atom_op_jump, ATOM_COND_EQUAL },
936     { atom_op_jump, ATOM_COND_BELOW },
937     { atom_op_jump, ATOM_COND_ABOVE },
938     { atom_op_jump, ATOM_COND_BELOWOREQUAL },
939     { atom_op_jump, ATOM_COND_ABOVEOREQUAL },
940     { atom_op_jump, ATOM_COND_NOTEQUAL },
941     { atom_op_test, ATOM_ARG_REG },
942     { atom_op_test, ATOM_ARG_PS },
943     { atom_op_test, ATOM_ARG_WS },
944     { atom_op_test, ATOM_ARG_FB },
945     { atom_op_test, ATOM_ARG_PLL },
946     { atom_op_test, ATOM_ARG_MC },
947     { atom_op_delay, ATOM_UNIT_MILLISEC },
948     { atom_op_delay, ATOM_UNIT_MICROSEC },
949     { atom_op_calltable, 0 },
950     { atom_op_repeat, 0 },
951     { atom_op_clear, ATOM_ARG_REG },
952     { atom_op_clear, ATOM_ARG_PS },
953     { atom_op_clear, ATOM_ARG_WS },
954     { atom_op_clear, ATOM_ARG_FB },
955     { atom_op_clear, ATOM_ARG_PLL },
956     { atom_op_clear, ATOM_ARG_MC },
957     { atom_op_nop, 0 },
958     { atom_op_eot, 0 },
959     { atom_op_mask, ATOM_ARG_REG },
960     { atom_op_mask, ATOM_ARG_PS },
961     { atom_op_mask, ATOM_ARG_WS },
962     { atom_op_mask, ATOM_ARG_FB },
963     { atom_op_mask, ATOM_ARG_PLL },
964     { atom_op_mask, ATOM_ARG_MC },
965     { atom_op_postcard, 0 },
966     { atom_op_beep, 0 },
967     { atom_op_savereg, 0 },
968     { atom_op_restorereg, 0 },
969     { atom_op_setdatablock, 0 },
970     { atom_op_xor, ATOM_ARG_REG },
971     { atom_op_xor, ATOM_ARG_PS },
972     { atom_op_xor, ATOM_ARG_WS },
973     { atom_op_xor, ATOM_ARG_FB },
974     { atom_op_xor, ATOM_ARG_PLL },
975     { atom_op_xor, ATOM_ARG_MC },
976     { atom_op_shl, ATOM_ARG_REG },
977     { atom_op_shl, ATOM_ARG_PS },
978     { atom_op_shl, ATOM_ARG_WS },
979     { atom_op_shl, ATOM_ARG_FB },
980     { atom_op_shl, ATOM_ARG_PLL },
981     { atom_op_shl, ATOM_ARG_MC },
982     { atom_op_shr, ATOM_ARG_REG },
983     { atom_op_shr, ATOM_ARG_PS },
984     { atom_op_shr, ATOM_ARG_WS },
985     { atom_op_shr, ATOM_ARG_FB },
986     { atom_op_shr, ATOM_ARG_PLL },
987     { atom_op_shr, ATOM_ARG_MC },
988     { atom_op_debug, 0 },
989 };
990
991 void atom_execute_table(struct atom_context *ctx, int index, uint32_t *params)
992 {
993     int base = CU16(ctx->cmd_table+4+2*index);
994     int len, ws, ps, ptr;
995     unsigned char op;
996     atom_exec_context ectx;
997
998     if(!base)
999         return;
1000
1001     len = CU16(base+ATOM_CT_SIZE_PTR);
1002     ws = CU8(base+ATOM_CT_WS_PTR);
1003     ps = CU8(base+ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
1004     ptr = base+ATOM_CT_CODE_PTR;
1005
1006     SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
1007
1008     /* reset reg block */
1009     ctx->reg_block = 0;
1010     ectx.ctx = ctx;
1011     ectx.ps_shift = ps/4;
1012     ectx.start = base;
1013     ectx.ps = params;
1014     if(ws)
1015         ectx.ws = kzalloc(4*ws, GFP_KERNEL);
1016     else
1017         ectx.ws = NULL;
1018
1019     debug_depth++;
1020     while(1) {
1021         op = CU8(ptr++);
1022         if(op<ATOM_OP_NAMES_CNT)
1023             SDEBUG("%s @ 0x%04X\n", atom_op_names[op], ptr-1);
1024         else
1025             SDEBUG("[%d] @ 0x%04X\n", op, ptr-1);
1026
1027         if(op<ATOM_OP_CNT && op>0)
1028             opcode_table[op].func(&ectx, &ptr, opcode_table[op].arg);
1029         else
1030             break;
1031
1032         if(op == ATOM_OP_EOT)
1033             break;
1034     }
1035     debug_depth--;
1036     SDEBUG("<<\n");
1037
1038     if(ws)
1039         kfree(ectx.ws);
1040 }
1041
1042 static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
1043 static void atom_index_iio(struct atom_context *ctx, int base)
1044 {
1045     ctx->iio = kzalloc(2*256, GFP_KERNEL);
1046     while(CU8(base) == ATOM_IIO_START) {
1047         ctx->iio[CU8(base+1)] = base+2;
1048         base += 2;
1049         while(CU8(base) != ATOM_IIO_END)
1050             base += atom_iio_len[CU8(base)];
1051         base += 3;
1052     }
1053 }
1054
1055 struct atom_context *atom_parse(struct card_info *card, void *bios)
1056 {
1057     int base;
1058     struct atom_context *ctx = kzalloc(sizeof(struct atom_context), GFP_KERNEL);
1059     char *str;
1060
1061     ctx->card = card;
1062     ctx->bios = bios;
1063
1064     if(CU16(0) != ATOM_BIOS_MAGIC) {
1065         printk(KERN_INFO "Invalid BIOS magic.\n");
1066         kfree(ctx);
1067         return NULL;
1068     }
1069     if(strncmp(CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC, strlen(ATOM_ATI_MAGIC))) {
1070         printk(KERN_INFO "Invalid ATI magic.\n");
1071         kfree(ctx);
1072         return NULL;
1073     }
1074
1075     base = CU16(ATOM_ROM_TABLE_PTR);
1076     if(strncmp(CSTR(base+ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC, strlen(ATOM_ROM_MAGIC))) {
1077         printk(KERN_INFO "Invalid ATOM magic.\n");
1078         kfree(ctx);
1079         return NULL;
1080     }
1081
1082     ctx->cmd_table = CU16(base+ATOM_ROM_CMD_PTR);
1083     ctx->data_table = CU16(base+ATOM_ROM_DATA_PTR);
1084     atom_index_iio(ctx, CU16(ctx->data_table+ATOM_DATA_IIO_PTR)+4);
1085
1086     str = CSTR(CU16(base+ATOM_ROM_MSG_PTR));
1087     while(*str && ((*str == '\n') || (*str == '\r')))
1088         str++;
1089     printk(KERN_INFO "ATOM BIOS: %s", str);
1090
1091     return ctx;
1092 }
1093
1094 int atom_asic_init(struct atom_context *ctx)
1095 {
1096     int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
1097     uint32_t ps[16];
1098     memset(ps, 0, 64);
1099
1100     ps[0] = CU32(hwi + ATOM_FWI_DEFSCLK_PTR);
1101     ps[1] = CU32(hwi + ATOM_FWI_DEFMCLK_PTR);
1102     if(!ps[0] || !ps[1])
1103         return 1;
1104
1105     if(!CU16(ctx->cmd_table+4+2*ATOM_CMD_INIT))
1106         return 1;
1107     atom_execute_table(ctx, ATOM_CMD_INIT, ps);
1108
1109     return 0;
1110 }
1111
1112 void atom_destroy(struct atom_context *ctx)
1113 {
1114     if(ctx->iio)
1115         kfree(ctx->iio);
1116     kfree(ctx);
1117 }
1118
1119
1120 void atom_parse_data_header(struct atom_context *ctx, int index, uint16_t *size, uint8_t *frev, uint8_t *crev, uint16_t *data_start)
1121 {
1122         int offset = index * 2 + 4;
1123         int idx = CU16(ctx->data_table + offset);
1124
1125         if (size)
1126                 *size = CU16(idx);
1127         if (frev)
1128                 *frev = CU8(idx + 2);
1129         if (crev)
1130                 *crev = CU8(idx + 3);
1131         *data_start = idx;
1132         return;
1133 }
1134
1135 void atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t *frev, uint8_t *crev)
1136 {
1137         int offset = index * 2 + 4;
1138         int idx = CU16(ctx->cmd_table + offset);
1139
1140         if (frev)
1141                 *frev = CU8(idx + 2);
1142         if (crev)
1143                 *crev = CU8(idx + 3);
1144         return;
1145 }