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