atom.c 33 KB

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