atom.c 33 KB

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