atom.c 32 KB

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