atom.c 30 KB

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