atom.c 33 KB

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