atom.c 32 KB

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