atom.c 34 KB

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