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