atom.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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. udelay(count);
  614. // schedule_timeout_uninterruptible(usecs_to_jiffies(count));
  615. else
  616. schedule_timeout_uninterruptible(msecs_to_jiffies(count));
  617. }
  618. static void atom_op_div(atom_exec_context *ctx, int *ptr, int arg)
  619. {
  620. uint8_t attr = U8((*ptr)++);
  621. uint32_t dst, src;
  622. SDEBUG(" src1: ");
  623. dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
  624. SDEBUG(" src2: ");
  625. src = atom_get_src(ctx, attr, ptr);
  626. if (src != 0) {
  627. ctx->ctx->divmul[0] = dst / src;
  628. ctx->ctx->divmul[1] = dst % src;
  629. } else {
  630. ctx->ctx->divmul[0] = 0;
  631. ctx->ctx->divmul[1] = 0;
  632. }
  633. }
  634. static void atom_op_eot(atom_exec_context *ctx, int *ptr, int arg)
  635. {
  636. /* functionally, a nop */
  637. }
  638. static void atom_op_jump(atom_exec_context *ctx, int *ptr, int arg)
  639. {
  640. int execute = 0, target = U16(*ptr);
  641. (*ptr) += 2;
  642. switch (arg) {
  643. case ATOM_COND_ABOVE:
  644. execute = ctx->ctx->cs_above;
  645. break;
  646. case ATOM_COND_ABOVEOREQUAL:
  647. execute = ctx->ctx->cs_above || ctx->ctx->cs_equal;
  648. break;
  649. case ATOM_COND_ALWAYS:
  650. execute = 1;
  651. break;
  652. case ATOM_COND_BELOW:
  653. execute = !(ctx->ctx->cs_above || ctx->ctx->cs_equal);
  654. break;
  655. case ATOM_COND_BELOWOREQUAL:
  656. execute = !ctx->ctx->cs_above;
  657. break;
  658. case ATOM_COND_EQUAL:
  659. execute = ctx->ctx->cs_equal;
  660. break;
  661. case ATOM_COND_NOTEQUAL:
  662. execute = !ctx->ctx->cs_equal;
  663. break;
  664. }
  665. if (arg != ATOM_COND_ALWAYS)
  666. SDEBUG(" taken: %s\n", execute ? "yes" : "no");
  667. SDEBUG(" target: 0x%04X\n", target);
  668. if (execute)
  669. *ptr = ctx->start + target;
  670. }
  671. static void atom_op_mask(atom_exec_context *ctx, int *ptr, int arg)
  672. {
  673. uint8_t attr = U8((*ptr)++);
  674. uint32_t dst, src1, src2, saved;
  675. int dptr = *ptr;
  676. SDEBUG(" dst: ");
  677. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  678. SDEBUG(" src1: ");
  679. src1 = atom_get_src_direct(ctx, ((attr >> 3) & 7), ptr);
  680. SDEBUG(" src2: ");
  681. src2 = atom_get_src(ctx, attr, ptr);
  682. dst &= src1;
  683. dst |= src2;
  684. SDEBUG(" dst: ");
  685. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  686. }
  687. static void atom_op_move(atom_exec_context *ctx, int *ptr, int arg)
  688. {
  689. uint8_t attr = U8((*ptr)++);
  690. uint32_t src, saved;
  691. int dptr = *ptr;
  692. if (((attr >> 3) & 7) != ATOM_SRC_DWORD)
  693. atom_get_dst(ctx, arg, attr, ptr, &saved, 0);
  694. else {
  695. atom_skip_dst(ctx, arg, attr, ptr);
  696. saved = 0xCDCDCDCD;
  697. }
  698. SDEBUG(" src: ");
  699. src = atom_get_src(ctx, attr, ptr);
  700. SDEBUG(" dst: ");
  701. atom_put_dst(ctx, arg, attr, &dptr, src, saved);
  702. }
  703. static void atom_op_mul(atom_exec_context *ctx, int *ptr, int arg)
  704. {
  705. uint8_t attr = U8((*ptr)++);
  706. uint32_t dst, src;
  707. SDEBUG(" src1: ");
  708. dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
  709. SDEBUG(" src2: ");
  710. src = atom_get_src(ctx, attr, ptr);
  711. ctx->ctx->divmul[0] = dst * src;
  712. }
  713. static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
  714. {
  715. /* nothing */
  716. }
  717. static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
  718. {
  719. uint8_t attr = U8((*ptr)++);
  720. uint32_t dst, src, saved;
  721. int dptr = *ptr;
  722. SDEBUG(" dst: ");
  723. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  724. SDEBUG(" src: ");
  725. src = atom_get_src(ctx, attr, ptr);
  726. dst |= src;
  727. SDEBUG(" dst: ");
  728. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  729. }
  730. static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
  731. {
  732. uint8_t val = U8((*ptr)++);
  733. SDEBUG("POST card output: 0x%02X\n", val);
  734. }
  735. static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
  736. {
  737. printk(KERN_INFO "unimplemented!\n");
  738. }
  739. static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
  740. {
  741. printk(KERN_INFO "unimplemented!\n");
  742. }
  743. static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
  744. {
  745. printk(KERN_INFO "unimplemented!\n");
  746. }
  747. static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
  748. {
  749. int idx = U8(*ptr);
  750. (*ptr)++;
  751. SDEBUG(" block: %d\n", idx);
  752. if (!idx)
  753. ctx->ctx->data_block = 0;
  754. else if (idx == 255)
  755. ctx->ctx->data_block = ctx->start;
  756. else
  757. ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
  758. SDEBUG(" base: 0x%04X\n", ctx->ctx->data_block);
  759. }
  760. static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
  761. {
  762. uint8_t attr = U8((*ptr)++);
  763. SDEBUG(" fb_base: ");
  764. ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
  765. }
  766. static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
  767. {
  768. int port;
  769. switch (arg) {
  770. case ATOM_PORT_ATI:
  771. port = U16(*ptr);
  772. if (port < ATOM_IO_NAMES_CNT)
  773. SDEBUG(" port: %d (%s)\n", port, atom_io_names[port]);
  774. else
  775. SDEBUG(" port: %d\n", port);
  776. if (!port)
  777. ctx->ctx->io_mode = ATOM_IO_MM;
  778. else
  779. ctx->ctx->io_mode = ATOM_IO_IIO | port;
  780. (*ptr) += 2;
  781. break;
  782. case ATOM_PORT_PCI:
  783. ctx->ctx->io_mode = ATOM_IO_PCI;
  784. (*ptr)++;
  785. break;
  786. case ATOM_PORT_SYSIO:
  787. ctx->ctx->io_mode = ATOM_IO_SYSIO;
  788. (*ptr)++;
  789. break;
  790. }
  791. }
  792. static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
  793. {
  794. ctx->ctx->reg_block = U16(*ptr);
  795. (*ptr) += 2;
  796. SDEBUG(" base: 0x%04X\n", ctx->ctx->reg_block);
  797. }
  798. static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
  799. {
  800. uint8_t attr = U8((*ptr)++), shift;
  801. uint32_t saved, dst;
  802. int dptr = *ptr;
  803. attr &= 0x38;
  804. attr |= atom_def_dst[attr >> 3] << 6;
  805. SDEBUG(" dst: ");
  806. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  807. shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
  808. SDEBUG(" shift: %d\n", shift);
  809. dst <<= shift;
  810. SDEBUG(" dst: ");
  811. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  812. }
  813. static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
  814. {
  815. uint8_t attr = U8((*ptr)++), shift;
  816. uint32_t saved, dst;
  817. int dptr = *ptr;
  818. attr &= 0x38;
  819. attr |= atom_def_dst[attr >> 3] << 6;
  820. SDEBUG(" dst: ");
  821. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  822. shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
  823. SDEBUG(" shift: %d\n", shift);
  824. dst >>= shift;
  825. SDEBUG(" dst: ");
  826. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  827. }
  828. static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
  829. {
  830. uint8_t attr = U8((*ptr)++), shift;
  831. uint32_t saved, dst;
  832. int dptr = *ptr;
  833. attr &= 0x38;
  834. attr |= atom_def_dst[attr >> 3] << 6;
  835. SDEBUG(" dst: ");
  836. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  837. shift = atom_get_src(ctx, attr, ptr);
  838. SDEBUG(" shift: %d\n", shift);
  839. dst <<= shift;
  840. SDEBUG(" dst: ");
  841. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  842. }
  843. static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
  844. {
  845. uint8_t attr = U8((*ptr)++), shift;
  846. uint32_t saved, dst;
  847. int dptr = *ptr;
  848. attr &= 0x38;
  849. attr |= atom_def_dst[attr >> 3] << 6;
  850. SDEBUG(" dst: ");
  851. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  852. shift = atom_get_src(ctx, attr, ptr);
  853. SDEBUG(" shift: %d\n", shift);
  854. dst >>= shift;
  855. SDEBUG(" dst: ");
  856. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  857. }
  858. static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
  859. {
  860. uint8_t attr = U8((*ptr)++);
  861. uint32_t dst, src, saved;
  862. int dptr = *ptr;
  863. SDEBUG(" dst: ");
  864. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  865. SDEBUG(" src: ");
  866. src = atom_get_src(ctx, attr, ptr);
  867. dst -= src;
  868. SDEBUG(" dst: ");
  869. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  870. }
  871. static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
  872. {
  873. uint8_t attr = U8((*ptr)++);
  874. uint32_t src, val, target;
  875. SDEBUG(" switch: ");
  876. src = atom_get_src(ctx, attr, ptr);
  877. while (U16(*ptr) != ATOM_CASE_END)
  878. if (U8(*ptr) == ATOM_CASE_MAGIC) {
  879. (*ptr)++;
  880. SDEBUG(" case: ");
  881. val =
  882. atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
  883. ptr);
  884. target = U16(*ptr);
  885. if (val == src) {
  886. SDEBUG(" target: %04X\n", target);
  887. *ptr = ctx->start + target;
  888. return;
  889. }
  890. (*ptr) += 2;
  891. } else {
  892. printk(KERN_INFO "Bad case.\n");
  893. return;
  894. }
  895. (*ptr) += 2;
  896. }
  897. static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
  898. {
  899. uint8_t attr = U8((*ptr)++);
  900. uint32_t dst, src;
  901. SDEBUG(" src1: ");
  902. dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
  903. SDEBUG(" src2: ");
  904. src = atom_get_src(ctx, attr, ptr);
  905. ctx->ctx->cs_equal = ((dst & src) == 0);
  906. SDEBUG(" result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
  907. }
  908. static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
  909. {
  910. uint8_t attr = U8((*ptr)++);
  911. uint32_t dst, src, saved;
  912. int dptr = *ptr;
  913. SDEBUG(" dst: ");
  914. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  915. SDEBUG(" src: ");
  916. src = atom_get_src(ctx, attr, ptr);
  917. dst ^= src;
  918. SDEBUG(" dst: ");
  919. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  920. }
  921. static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
  922. {
  923. printk(KERN_INFO "unimplemented!\n");
  924. }
  925. static struct {
  926. void (*func) (atom_exec_context *, int *, int);
  927. int arg;
  928. } opcode_table[ATOM_OP_CNT] = {
  929. {
  930. NULL, 0}, {
  931. atom_op_move, ATOM_ARG_REG}, {
  932. atom_op_move, ATOM_ARG_PS}, {
  933. atom_op_move, ATOM_ARG_WS}, {
  934. atom_op_move, ATOM_ARG_FB}, {
  935. atom_op_move, ATOM_ARG_PLL}, {
  936. atom_op_move, ATOM_ARG_MC}, {
  937. atom_op_and, ATOM_ARG_REG}, {
  938. atom_op_and, ATOM_ARG_PS}, {
  939. atom_op_and, ATOM_ARG_WS}, {
  940. atom_op_and, ATOM_ARG_FB}, {
  941. atom_op_and, ATOM_ARG_PLL}, {
  942. atom_op_and, ATOM_ARG_MC}, {
  943. atom_op_or, ATOM_ARG_REG}, {
  944. atom_op_or, ATOM_ARG_PS}, {
  945. atom_op_or, ATOM_ARG_WS}, {
  946. atom_op_or, ATOM_ARG_FB}, {
  947. atom_op_or, ATOM_ARG_PLL}, {
  948. atom_op_or, ATOM_ARG_MC}, {
  949. atom_op_shift_left, ATOM_ARG_REG}, {
  950. atom_op_shift_left, ATOM_ARG_PS}, {
  951. atom_op_shift_left, ATOM_ARG_WS}, {
  952. atom_op_shift_left, ATOM_ARG_FB}, {
  953. atom_op_shift_left, ATOM_ARG_PLL}, {
  954. atom_op_shift_left, ATOM_ARG_MC}, {
  955. atom_op_shift_right, ATOM_ARG_REG}, {
  956. atom_op_shift_right, ATOM_ARG_PS}, {
  957. atom_op_shift_right, ATOM_ARG_WS}, {
  958. atom_op_shift_right, ATOM_ARG_FB}, {
  959. atom_op_shift_right, ATOM_ARG_PLL}, {
  960. atom_op_shift_right, ATOM_ARG_MC}, {
  961. atom_op_mul, ATOM_ARG_REG}, {
  962. atom_op_mul, ATOM_ARG_PS}, {
  963. atom_op_mul, ATOM_ARG_WS}, {
  964. atom_op_mul, ATOM_ARG_FB}, {
  965. atom_op_mul, ATOM_ARG_PLL}, {
  966. atom_op_mul, ATOM_ARG_MC}, {
  967. atom_op_div, ATOM_ARG_REG}, {
  968. atom_op_div, ATOM_ARG_PS}, {
  969. atom_op_div, ATOM_ARG_WS}, {
  970. atom_op_div, ATOM_ARG_FB}, {
  971. atom_op_div, ATOM_ARG_PLL}, {
  972. atom_op_div, ATOM_ARG_MC}, {
  973. atom_op_add, ATOM_ARG_REG}, {
  974. atom_op_add, ATOM_ARG_PS}, {
  975. atom_op_add, ATOM_ARG_WS}, {
  976. atom_op_add, ATOM_ARG_FB}, {
  977. atom_op_add, ATOM_ARG_PLL}, {
  978. atom_op_add, ATOM_ARG_MC}, {
  979. atom_op_sub, ATOM_ARG_REG}, {
  980. atom_op_sub, ATOM_ARG_PS}, {
  981. atom_op_sub, ATOM_ARG_WS}, {
  982. atom_op_sub, ATOM_ARG_FB}, {
  983. atom_op_sub, ATOM_ARG_PLL}, {
  984. atom_op_sub, ATOM_ARG_MC}, {
  985. atom_op_setport, ATOM_PORT_ATI}, {
  986. atom_op_setport, ATOM_PORT_PCI}, {
  987. atom_op_setport, ATOM_PORT_SYSIO}, {
  988. atom_op_setregblock, 0}, {
  989. atom_op_setfbbase, 0}, {
  990. atom_op_compare, ATOM_ARG_REG}, {
  991. atom_op_compare, ATOM_ARG_PS}, {
  992. atom_op_compare, ATOM_ARG_WS}, {
  993. atom_op_compare, ATOM_ARG_FB}, {
  994. atom_op_compare, ATOM_ARG_PLL}, {
  995. atom_op_compare, ATOM_ARG_MC}, {
  996. atom_op_switch, 0}, {
  997. atom_op_jump, ATOM_COND_ALWAYS}, {
  998. atom_op_jump, ATOM_COND_EQUAL}, {
  999. atom_op_jump, ATOM_COND_BELOW}, {
  1000. atom_op_jump, ATOM_COND_ABOVE}, {
  1001. atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
  1002. atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
  1003. atom_op_jump, ATOM_COND_NOTEQUAL}, {
  1004. atom_op_test, ATOM_ARG_REG}, {
  1005. atom_op_test, ATOM_ARG_PS}, {
  1006. atom_op_test, ATOM_ARG_WS}, {
  1007. atom_op_test, ATOM_ARG_FB}, {
  1008. atom_op_test, ATOM_ARG_PLL}, {
  1009. atom_op_test, ATOM_ARG_MC}, {
  1010. atom_op_delay, ATOM_UNIT_MILLISEC}, {
  1011. atom_op_delay, ATOM_UNIT_MICROSEC}, {
  1012. atom_op_calltable, 0}, {
  1013. atom_op_repeat, 0}, {
  1014. atom_op_clear, ATOM_ARG_REG}, {
  1015. atom_op_clear, ATOM_ARG_PS}, {
  1016. atom_op_clear, ATOM_ARG_WS}, {
  1017. atom_op_clear, ATOM_ARG_FB}, {
  1018. atom_op_clear, ATOM_ARG_PLL}, {
  1019. atom_op_clear, ATOM_ARG_MC}, {
  1020. atom_op_nop, 0}, {
  1021. atom_op_eot, 0}, {
  1022. atom_op_mask, ATOM_ARG_REG}, {
  1023. atom_op_mask, ATOM_ARG_PS}, {
  1024. atom_op_mask, ATOM_ARG_WS}, {
  1025. atom_op_mask, ATOM_ARG_FB}, {
  1026. atom_op_mask, ATOM_ARG_PLL}, {
  1027. atom_op_mask, ATOM_ARG_MC}, {
  1028. atom_op_postcard, 0}, {
  1029. atom_op_beep, 0}, {
  1030. atom_op_savereg, 0}, {
  1031. atom_op_restorereg, 0}, {
  1032. atom_op_setdatablock, 0}, {
  1033. atom_op_xor, ATOM_ARG_REG}, {
  1034. atom_op_xor, ATOM_ARG_PS}, {
  1035. atom_op_xor, ATOM_ARG_WS}, {
  1036. atom_op_xor, ATOM_ARG_FB}, {
  1037. atom_op_xor, ATOM_ARG_PLL}, {
  1038. atom_op_xor, ATOM_ARG_MC}, {
  1039. atom_op_shl, ATOM_ARG_REG}, {
  1040. atom_op_shl, ATOM_ARG_PS}, {
  1041. atom_op_shl, ATOM_ARG_WS}, {
  1042. atom_op_shl, ATOM_ARG_FB}, {
  1043. atom_op_shl, ATOM_ARG_PLL}, {
  1044. atom_op_shl, ATOM_ARG_MC}, {
  1045. atom_op_shr, ATOM_ARG_REG}, {
  1046. atom_op_shr, ATOM_ARG_PS}, {
  1047. atom_op_shr, ATOM_ARG_WS}, {
  1048. atom_op_shr, ATOM_ARG_FB}, {
  1049. atom_op_shr, ATOM_ARG_PLL}, {
  1050. atom_op_shr, ATOM_ARG_MC}, {
  1051. atom_op_debug, 0},};
  1052. static void atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
  1053. {
  1054. int base = CU16(ctx->cmd_table + 4 + 2 * index);
  1055. int len, ws, ps, ptr;
  1056. unsigned char op;
  1057. atom_exec_context ectx;
  1058. if (!base)
  1059. return;
  1060. len = CU16(base + ATOM_CT_SIZE_PTR);
  1061. ws = CU8(base + ATOM_CT_WS_PTR);
  1062. ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
  1063. ptr = base + ATOM_CT_CODE_PTR;
  1064. SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
  1065. ectx.ctx = ctx;
  1066. ectx.ps_shift = ps / 4;
  1067. ectx.start = base;
  1068. ectx.ps = params;
  1069. if (ws)
  1070. ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
  1071. else
  1072. ectx.ws = NULL;
  1073. debug_depth++;
  1074. while (1) {
  1075. op = CU8(ptr++);
  1076. if (op < ATOM_OP_NAMES_CNT)
  1077. SDEBUG("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
  1078. else
  1079. SDEBUG("[%d] @ 0x%04X\n", op, ptr - 1);
  1080. if (op < ATOM_OP_CNT && op > 0)
  1081. opcode_table[op].func(&ectx, &ptr,
  1082. opcode_table[op].arg);
  1083. else
  1084. break;
  1085. if (op == ATOM_OP_EOT)
  1086. break;
  1087. }
  1088. debug_depth--;
  1089. SDEBUG("<<\n");
  1090. if (ws)
  1091. kfree(ectx.ws);
  1092. }
  1093. void atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
  1094. {
  1095. mutex_lock(&ctx->mutex);
  1096. /* reset reg block */
  1097. ctx->reg_block = 0;
  1098. /* reset fb window */
  1099. ctx->fb_base = 0;
  1100. /* reset io mode */
  1101. ctx->io_mode = ATOM_IO_MM;
  1102. atom_execute_table_locked(ctx, index, params);
  1103. mutex_unlock(&ctx->mutex);
  1104. }
  1105. static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
  1106. static void atom_index_iio(struct atom_context *ctx, int base)
  1107. {
  1108. ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
  1109. while (CU8(base) == ATOM_IIO_START) {
  1110. ctx->iio[CU8(base + 1)] = base + 2;
  1111. base += 2;
  1112. while (CU8(base) != ATOM_IIO_END)
  1113. base += atom_iio_len[CU8(base)];
  1114. base += 3;
  1115. }
  1116. }
  1117. struct atom_context *atom_parse(struct card_info *card, void *bios)
  1118. {
  1119. int base;
  1120. struct atom_context *ctx =
  1121. kzalloc(sizeof(struct atom_context), GFP_KERNEL);
  1122. char *str;
  1123. char name[512];
  1124. int i;
  1125. ctx->card = card;
  1126. ctx->bios = bios;
  1127. if (CU16(0) != ATOM_BIOS_MAGIC) {
  1128. printk(KERN_INFO "Invalid BIOS magic.\n");
  1129. kfree(ctx);
  1130. return NULL;
  1131. }
  1132. if (strncmp
  1133. (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
  1134. strlen(ATOM_ATI_MAGIC))) {
  1135. printk(KERN_INFO "Invalid ATI magic.\n");
  1136. kfree(ctx);
  1137. return NULL;
  1138. }
  1139. base = CU16(ATOM_ROM_TABLE_PTR);
  1140. if (strncmp
  1141. (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
  1142. strlen(ATOM_ROM_MAGIC))) {
  1143. printk(KERN_INFO "Invalid ATOM magic.\n");
  1144. kfree(ctx);
  1145. return NULL;
  1146. }
  1147. ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
  1148. ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
  1149. atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
  1150. str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
  1151. while (*str && ((*str == '\n') || (*str == '\r')))
  1152. str++;
  1153. /* name string isn't always 0 terminated */
  1154. for (i = 0; i < 511; i++) {
  1155. name[i] = str[i];
  1156. if (name[i] < '.' || name[i] > 'z') {
  1157. name[i] = 0;
  1158. break;
  1159. }
  1160. }
  1161. printk(KERN_INFO "ATOM BIOS: %s\n", name);
  1162. return ctx;
  1163. }
  1164. int atom_asic_init(struct atom_context *ctx)
  1165. {
  1166. int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
  1167. uint32_t ps[16];
  1168. memset(ps, 0, 64);
  1169. ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
  1170. ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
  1171. if (!ps[0] || !ps[1])
  1172. return 1;
  1173. if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
  1174. return 1;
  1175. atom_execute_table(ctx, ATOM_CMD_INIT, ps);
  1176. return 0;
  1177. }
  1178. void atom_destroy(struct atom_context *ctx)
  1179. {
  1180. if (ctx->iio)
  1181. kfree(ctx->iio);
  1182. kfree(ctx);
  1183. }
  1184. void atom_parse_data_header(struct atom_context *ctx, int index,
  1185. uint16_t * size, uint8_t * frev, uint8_t * crev,
  1186. uint16_t * data_start)
  1187. {
  1188. int offset = index * 2 + 4;
  1189. int idx = CU16(ctx->data_table + offset);
  1190. if (size)
  1191. *size = CU16(idx);
  1192. if (frev)
  1193. *frev = CU8(idx + 2);
  1194. if (crev)
  1195. *crev = CU8(idx + 3);
  1196. *data_start = idx;
  1197. return;
  1198. }
  1199. void atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
  1200. uint8_t * crev)
  1201. {
  1202. int offset = index * 2 + 4;
  1203. int idx = CU16(ctx->cmd_table + offset);
  1204. if (frev)
  1205. *frev = CU8(idx + 2);
  1206. if (crev)
  1207. *crev = CU8(idx + 3);
  1208. return;
  1209. }
  1210. int atom_allocate_fb_scratch(struct atom_context *ctx)
  1211. {
  1212. int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
  1213. uint16_t data_offset;
  1214. int usage_bytes;
  1215. struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
  1216. atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset);
  1217. firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
  1218. DRM_DEBUG("atom firmware requested %08x %dkb\n",
  1219. firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware,
  1220. firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb);
  1221. usage_bytes = firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb * 1024;
  1222. if (usage_bytes == 0)
  1223. usage_bytes = 20 * 1024;
  1224. /* allocate some scratch memory */
  1225. ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
  1226. if (!ctx->scratch)
  1227. return -ENOMEM;
  1228. return 0;
  1229. }