atom.c 34 KB

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