atom.c 33 KB

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