atom.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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_mul32(atom_exec_context *ctx, int *ptr, int arg)
  753. {
  754. uint64_t val64;
  755. uint8_t attr = U8((*ptr)++);
  756. uint32_t dst, src;
  757. SDEBUG(" src1: ");
  758. dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
  759. SDEBUG(" src2: ");
  760. src = atom_get_src(ctx, attr, ptr);
  761. val64 = (uint64_t)dst * (uint64_t)src;
  762. ctx->ctx->divmul[0] = lower_32_bits(val64);
  763. ctx->ctx->divmul[1] = upper_32_bits(val64);
  764. }
  765. static void atom_op_nop(atom_exec_context *ctx, int *ptr, int arg)
  766. {
  767. /* nothing */
  768. }
  769. static void atom_op_or(atom_exec_context *ctx, int *ptr, int arg)
  770. {
  771. uint8_t attr = U8((*ptr)++);
  772. uint32_t dst, src, saved;
  773. int dptr = *ptr;
  774. SDEBUG(" dst: ");
  775. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  776. SDEBUG(" src: ");
  777. src = atom_get_src(ctx, attr, ptr);
  778. dst |= src;
  779. SDEBUG(" dst: ");
  780. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  781. }
  782. static void atom_op_postcard(atom_exec_context *ctx, int *ptr, int arg)
  783. {
  784. uint8_t val = U8((*ptr)++);
  785. SDEBUG("POST card output: 0x%02X\n", val);
  786. }
  787. static void atom_op_repeat(atom_exec_context *ctx, int *ptr, int arg)
  788. {
  789. printk(KERN_INFO "unimplemented!\n");
  790. }
  791. static void atom_op_restorereg(atom_exec_context *ctx, int *ptr, int arg)
  792. {
  793. printk(KERN_INFO "unimplemented!\n");
  794. }
  795. static void atom_op_savereg(atom_exec_context *ctx, int *ptr, int arg)
  796. {
  797. printk(KERN_INFO "unimplemented!\n");
  798. }
  799. static void atom_op_setdatablock(atom_exec_context *ctx, int *ptr, int arg)
  800. {
  801. int idx = U8(*ptr);
  802. (*ptr)++;
  803. SDEBUG(" block: %d\n", idx);
  804. if (!idx)
  805. ctx->ctx->data_block = 0;
  806. else if (idx == 255)
  807. ctx->ctx->data_block = ctx->start;
  808. else
  809. ctx->ctx->data_block = U16(ctx->ctx->data_table + 4 + 2 * idx);
  810. SDEBUG(" base: 0x%04X\n", ctx->ctx->data_block);
  811. }
  812. static void atom_op_setfbbase(atom_exec_context *ctx, int *ptr, int arg)
  813. {
  814. uint8_t attr = U8((*ptr)++);
  815. SDEBUG(" fb_base: ");
  816. ctx->ctx->fb_base = atom_get_src(ctx, attr, ptr);
  817. }
  818. static void atom_op_setport(atom_exec_context *ctx, int *ptr, int arg)
  819. {
  820. int port;
  821. switch (arg) {
  822. case ATOM_PORT_ATI:
  823. port = U16(*ptr);
  824. if (port < ATOM_IO_NAMES_CNT)
  825. SDEBUG(" port: %d (%s)\n", port, atom_io_names[port]);
  826. else
  827. SDEBUG(" port: %d\n", port);
  828. if (!port)
  829. ctx->ctx->io_mode = ATOM_IO_MM;
  830. else
  831. ctx->ctx->io_mode = ATOM_IO_IIO | port;
  832. (*ptr) += 2;
  833. break;
  834. case ATOM_PORT_PCI:
  835. ctx->ctx->io_mode = ATOM_IO_PCI;
  836. (*ptr)++;
  837. break;
  838. case ATOM_PORT_SYSIO:
  839. ctx->ctx->io_mode = ATOM_IO_SYSIO;
  840. (*ptr)++;
  841. break;
  842. }
  843. }
  844. static void atom_op_setregblock(atom_exec_context *ctx, int *ptr, int arg)
  845. {
  846. ctx->ctx->reg_block = U16(*ptr);
  847. (*ptr) += 2;
  848. SDEBUG(" base: 0x%04X\n", ctx->ctx->reg_block);
  849. }
  850. static void atom_op_shift_left(atom_exec_context *ctx, int *ptr, int arg)
  851. {
  852. uint8_t attr = U8((*ptr)++), shift;
  853. uint32_t saved, dst;
  854. int dptr = *ptr;
  855. attr &= 0x38;
  856. attr |= atom_def_dst[attr >> 3] << 6;
  857. SDEBUG(" dst: ");
  858. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  859. shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
  860. SDEBUG(" shift: %d\n", shift);
  861. dst <<= shift;
  862. SDEBUG(" dst: ");
  863. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  864. }
  865. static void atom_op_shift_right(atom_exec_context *ctx, int *ptr, int arg)
  866. {
  867. uint8_t attr = U8((*ptr)++), shift;
  868. uint32_t saved, dst;
  869. int dptr = *ptr;
  870. attr &= 0x38;
  871. attr |= atom_def_dst[attr >> 3] << 6;
  872. SDEBUG(" dst: ");
  873. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  874. shift = atom_get_src_direct(ctx, ATOM_SRC_BYTE0, ptr);
  875. SDEBUG(" shift: %d\n", shift);
  876. dst >>= shift;
  877. SDEBUG(" dst: ");
  878. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  879. }
  880. static void atom_op_shl(atom_exec_context *ctx, int *ptr, int arg)
  881. {
  882. uint8_t attr = U8((*ptr)++), shift;
  883. uint32_t saved, dst;
  884. int dptr = *ptr;
  885. uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
  886. SDEBUG(" dst: ");
  887. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  888. /* op needs to full dst value */
  889. dst = saved;
  890. shift = atom_get_src(ctx, attr, ptr);
  891. SDEBUG(" shift: %d\n", shift);
  892. dst <<= shift;
  893. dst &= atom_arg_mask[dst_align];
  894. dst >>= atom_arg_shift[dst_align];
  895. SDEBUG(" dst: ");
  896. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  897. }
  898. static void atom_op_shr(atom_exec_context *ctx, int *ptr, int arg)
  899. {
  900. uint8_t attr = U8((*ptr)++), shift;
  901. uint32_t saved, dst;
  902. int dptr = *ptr;
  903. uint32_t dst_align = atom_dst_to_src[(attr >> 3) & 7][(attr >> 6) & 3];
  904. SDEBUG(" dst: ");
  905. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  906. /* op needs to full dst value */
  907. dst = saved;
  908. shift = atom_get_src(ctx, attr, ptr);
  909. SDEBUG(" shift: %d\n", shift);
  910. dst >>= shift;
  911. dst &= atom_arg_mask[dst_align];
  912. dst >>= atom_arg_shift[dst_align];
  913. SDEBUG(" dst: ");
  914. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  915. }
  916. static void atom_op_sub(atom_exec_context *ctx, int *ptr, int arg)
  917. {
  918. uint8_t attr = U8((*ptr)++);
  919. uint32_t dst, src, saved;
  920. int dptr = *ptr;
  921. SDEBUG(" dst: ");
  922. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  923. SDEBUG(" src: ");
  924. src = atom_get_src(ctx, attr, ptr);
  925. dst -= src;
  926. SDEBUG(" dst: ");
  927. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  928. }
  929. static void atom_op_switch(atom_exec_context *ctx, int *ptr, int arg)
  930. {
  931. uint8_t attr = U8((*ptr)++);
  932. uint32_t src, val, target;
  933. SDEBUG(" switch: ");
  934. src = atom_get_src(ctx, attr, ptr);
  935. while (U16(*ptr) != ATOM_CASE_END)
  936. if (U8(*ptr) == ATOM_CASE_MAGIC) {
  937. (*ptr)++;
  938. SDEBUG(" case: ");
  939. val =
  940. atom_get_src(ctx, (attr & 0x38) | ATOM_ARG_IMM,
  941. ptr);
  942. target = U16(*ptr);
  943. if (val == src) {
  944. SDEBUG(" target: %04X\n", target);
  945. *ptr = ctx->start + target;
  946. return;
  947. }
  948. (*ptr) += 2;
  949. } else {
  950. printk(KERN_INFO "Bad case.\n");
  951. return;
  952. }
  953. (*ptr) += 2;
  954. }
  955. static void atom_op_test(atom_exec_context *ctx, int *ptr, int arg)
  956. {
  957. uint8_t attr = U8((*ptr)++);
  958. uint32_t dst, src;
  959. SDEBUG(" src1: ");
  960. dst = atom_get_dst(ctx, arg, attr, ptr, NULL, 1);
  961. SDEBUG(" src2: ");
  962. src = atom_get_src(ctx, attr, ptr);
  963. ctx->ctx->cs_equal = ((dst & src) == 0);
  964. SDEBUG(" result: %s\n", ctx->ctx->cs_equal ? "EQ" : "NE");
  965. }
  966. static void atom_op_xor(atom_exec_context *ctx, int *ptr, int arg)
  967. {
  968. uint8_t attr = U8((*ptr)++);
  969. uint32_t dst, src, saved;
  970. int dptr = *ptr;
  971. SDEBUG(" dst: ");
  972. dst = atom_get_dst(ctx, arg, attr, ptr, &saved, 1);
  973. SDEBUG(" src: ");
  974. src = atom_get_src(ctx, attr, ptr);
  975. dst ^= src;
  976. SDEBUG(" dst: ");
  977. atom_put_dst(ctx, arg, attr, &dptr, dst, saved);
  978. }
  979. static void atom_op_debug(atom_exec_context *ctx, int *ptr, int arg)
  980. {
  981. uint8_t val = U8((*ptr)++);
  982. SDEBUG("DEBUG output: 0x%02X\n", val);
  983. }
  984. static void atom_op_processds(atom_exec_context *ctx, int *ptr, int arg)
  985. {
  986. uint16_t val = U16(*ptr);
  987. (*ptr) += val + 2;
  988. SDEBUG("PROCESSDS output: 0x%02X\n", val);
  989. }
  990. static struct {
  991. void (*func) (atom_exec_context *, int *, int);
  992. int arg;
  993. } opcode_table[ATOM_OP_CNT] = {
  994. {
  995. NULL, 0}, {
  996. atom_op_move, ATOM_ARG_REG}, {
  997. atom_op_move, ATOM_ARG_PS}, {
  998. atom_op_move, ATOM_ARG_WS}, {
  999. atom_op_move, ATOM_ARG_FB}, {
  1000. atom_op_move, ATOM_ARG_PLL}, {
  1001. atom_op_move, ATOM_ARG_MC}, {
  1002. atom_op_and, ATOM_ARG_REG}, {
  1003. atom_op_and, ATOM_ARG_PS}, {
  1004. atom_op_and, ATOM_ARG_WS}, {
  1005. atom_op_and, ATOM_ARG_FB}, {
  1006. atom_op_and, ATOM_ARG_PLL}, {
  1007. atom_op_and, ATOM_ARG_MC}, {
  1008. atom_op_or, ATOM_ARG_REG}, {
  1009. atom_op_or, ATOM_ARG_PS}, {
  1010. atom_op_or, ATOM_ARG_WS}, {
  1011. atom_op_or, ATOM_ARG_FB}, {
  1012. atom_op_or, ATOM_ARG_PLL}, {
  1013. atom_op_or, ATOM_ARG_MC}, {
  1014. atom_op_shift_left, ATOM_ARG_REG}, {
  1015. atom_op_shift_left, ATOM_ARG_PS}, {
  1016. atom_op_shift_left, ATOM_ARG_WS}, {
  1017. atom_op_shift_left, ATOM_ARG_FB}, {
  1018. atom_op_shift_left, ATOM_ARG_PLL}, {
  1019. atom_op_shift_left, ATOM_ARG_MC}, {
  1020. atom_op_shift_right, ATOM_ARG_REG}, {
  1021. atom_op_shift_right, ATOM_ARG_PS}, {
  1022. atom_op_shift_right, ATOM_ARG_WS}, {
  1023. atom_op_shift_right, ATOM_ARG_FB}, {
  1024. atom_op_shift_right, ATOM_ARG_PLL}, {
  1025. atom_op_shift_right, ATOM_ARG_MC}, {
  1026. atom_op_mul, ATOM_ARG_REG}, {
  1027. atom_op_mul, ATOM_ARG_PS}, {
  1028. atom_op_mul, ATOM_ARG_WS}, {
  1029. atom_op_mul, ATOM_ARG_FB}, {
  1030. atom_op_mul, ATOM_ARG_PLL}, {
  1031. atom_op_mul, ATOM_ARG_MC}, {
  1032. atom_op_div, ATOM_ARG_REG}, {
  1033. atom_op_div, ATOM_ARG_PS}, {
  1034. atom_op_div, ATOM_ARG_WS}, {
  1035. atom_op_div, ATOM_ARG_FB}, {
  1036. atom_op_div, ATOM_ARG_PLL}, {
  1037. atom_op_div, ATOM_ARG_MC}, {
  1038. atom_op_add, ATOM_ARG_REG}, {
  1039. atom_op_add, ATOM_ARG_PS}, {
  1040. atom_op_add, ATOM_ARG_WS}, {
  1041. atom_op_add, ATOM_ARG_FB}, {
  1042. atom_op_add, ATOM_ARG_PLL}, {
  1043. atom_op_add, ATOM_ARG_MC}, {
  1044. atom_op_sub, ATOM_ARG_REG}, {
  1045. atom_op_sub, ATOM_ARG_PS}, {
  1046. atom_op_sub, ATOM_ARG_WS}, {
  1047. atom_op_sub, ATOM_ARG_FB}, {
  1048. atom_op_sub, ATOM_ARG_PLL}, {
  1049. atom_op_sub, ATOM_ARG_MC}, {
  1050. atom_op_setport, ATOM_PORT_ATI}, {
  1051. atom_op_setport, ATOM_PORT_PCI}, {
  1052. atom_op_setport, ATOM_PORT_SYSIO}, {
  1053. atom_op_setregblock, 0}, {
  1054. atom_op_setfbbase, 0}, {
  1055. atom_op_compare, ATOM_ARG_REG}, {
  1056. atom_op_compare, ATOM_ARG_PS}, {
  1057. atom_op_compare, ATOM_ARG_WS}, {
  1058. atom_op_compare, ATOM_ARG_FB}, {
  1059. atom_op_compare, ATOM_ARG_PLL}, {
  1060. atom_op_compare, ATOM_ARG_MC}, {
  1061. atom_op_switch, 0}, {
  1062. atom_op_jump, ATOM_COND_ALWAYS}, {
  1063. atom_op_jump, ATOM_COND_EQUAL}, {
  1064. atom_op_jump, ATOM_COND_BELOW}, {
  1065. atom_op_jump, ATOM_COND_ABOVE}, {
  1066. atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
  1067. atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
  1068. atom_op_jump, ATOM_COND_NOTEQUAL}, {
  1069. atom_op_test, ATOM_ARG_REG}, {
  1070. atom_op_test, ATOM_ARG_PS}, {
  1071. atom_op_test, ATOM_ARG_WS}, {
  1072. atom_op_test, ATOM_ARG_FB}, {
  1073. atom_op_test, ATOM_ARG_PLL}, {
  1074. atom_op_test, ATOM_ARG_MC}, {
  1075. atom_op_delay, ATOM_UNIT_MILLISEC}, {
  1076. atom_op_delay, ATOM_UNIT_MICROSEC}, {
  1077. atom_op_calltable, 0}, {
  1078. atom_op_repeat, 0}, {
  1079. atom_op_clear, ATOM_ARG_REG}, {
  1080. atom_op_clear, ATOM_ARG_PS}, {
  1081. atom_op_clear, ATOM_ARG_WS}, {
  1082. atom_op_clear, ATOM_ARG_FB}, {
  1083. atom_op_clear, ATOM_ARG_PLL}, {
  1084. atom_op_clear, ATOM_ARG_MC}, {
  1085. atom_op_nop, 0}, {
  1086. atom_op_eot, 0}, {
  1087. atom_op_mask, ATOM_ARG_REG}, {
  1088. atom_op_mask, ATOM_ARG_PS}, {
  1089. atom_op_mask, ATOM_ARG_WS}, {
  1090. atom_op_mask, ATOM_ARG_FB}, {
  1091. atom_op_mask, ATOM_ARG_PLL}, {
  1092. atom_op_mask, ATOM_ARG_MC}, {
  1093. atom_op_postcard, 0}, {
  1094. atom_op_beep, 0}, {
  1095. atom_op_savereg, 0}, {
  1096. atom_op_restorereg, 0}, {
  1097. atom_op_setdatablock, 0}, {
  1098. atom_op_xor, ATOM_ARG_REG}, {
  1099. atom_op_xor, ATOM_ARG_PS}, {
  1100. atom_op_xor, ATOM_ARG_WS}, {
  1101. atom_op_xor, ATOM_ARG_FB}, {
  1102. atom_op_xor, ATOM_ARG_PLL}, {
  1103. atom_op_xor, ATOM_ARG_MC}, {
  1104. atom_op_shl, ATOM_ARG_REG}, {
  1105. atom_op_shl, ATOM_ARG_PS}, {
  1106. atom_op_shl, ATOM_ARG_WS}, {
  1107. atom_op_shl, ATOM_ARG_FB}, {
  1108. atom_op_shl, ATOM_ARG_PLL}, {
  1109. atom_op_shl, ATOM_ARG_MC}, {
  1110. atom_op_shr, ATOM_ARG_REG}, {
  1111. atom_op_shr, ATOM_ARG_PS}, {
  1112. atom_op_shr, ATOM_ARG_WS}, {
  1113. atom_op_shr, ATOM_ARG_FB}, {
  1114. atom_op_shr, ATOM_ARG_PLL}, {
  1115. atom_op_shr, ATOM_ARG_MC}, {
  1116. atom_op_debug, 0}, {
  1117. atom_op_processds, 0}, {
  1118. atom_op_mul32, ATOM_ARG_PS}, {
  1119. atom_op_mul32, ATOM_ARG_WS},
  1120. };
  1121. static int amdgpu_atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
  1122. {
  1123. int base = CU16(ctx->cmd_table + 4 + 2 * index);
  1124. int len, ws, ps, ptr;
  1125. unsigned char op;
  1126. atom_exec_context ectx;
  1127. int ret = 0;
  1128. if (!base)
  1129. return -EINVAL;
  1130. len = CU16(base + ATOM_CT_SIZE_PTR);
  1131. ws = CU8(base + ATOM_CT_WS_PTR);
  1132. ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
  1133. ptr = base + ATOM_CT_CODE_PTR;
  1134. SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
  1135. ectx.ctx = ctx;
  1136. ectx.ps_shift = ps / 4;
  1137. ectx.start = base;
  1138. ectx.ps = params;
  1139. ectx.abort = false;
  1140. ectx.last_jump = 0;
  1141. if (ws)
  1142. ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
  1143. else
  1144. ectx.ws = NULL;
  1145. debug_depth++;
  1146. while (1) {
  1147. op = CU8(ptr++);
  1148. if (op < ATOM_OP_NAMES_CNT)
  1149. SDEBUG("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
  1150. else
  1151. SDEBUG("[%d] @ 0x%04X\n", op, ptr - 1);
  1152. if (ectx.abort) {
  1153. DRM_ERROR("atombios stuck executing %04X (len %d, WS %d, PS %d) @ 0x%04X\n",
  1154. base, len, ws, ps, ptr - 1);
  1155. ret = -EINVAL;
  1156. goto free;
  1157. }
  1158. if (op < ATOM_OP_CNT && op > 0)
  1159. opcode_table[op].func(&ectx, &ptr,
  1160. opcode_table[op].arg);
  1161. else
  1162. break;
  1163. if (op == ATOM_OP_EOT)
  1164. break;
  1165. }
  1166. debug_depth--;
  1167. SDEBUG("<<\n");
  1168. free:
  1169. if (ws)
  1170. kfree(ectx.ws);
  1171. return ret;
  1172. }
  1173. int amdgpu_atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
  1174. {
  1175. int r;
  1176. mutex_lock(&ctx->mutex);
  1177. /* reset data block */
  1178. ctx->data_block = 0;
  1179. /* reset reg block */
  1180. ctx->reg_block = 0;
  1181. /* reset fb window */
  1182. ctx->fb_base = 0;
  1183. /* reset io mode */
  1184. ctx->io_mode = ATOM_IO_MM;
  1185. /* reset divmul */
  1186. ctx->divmul[0] = 0;
  1187. ctx->divmul[1] = 0;
  1188. r = amdgpu_atom_execute_table_locked(ctx, index, params);
  1189. mutex_unlock(&ctx->mutex);
  1190. return r;
  1191. }
  1192. static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
  1193. static void atom_index_iio(struct atom_context *ctx, int base)
  1194. {
  1195. ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
  1196. if (!ctx->iio)
  1197. return;
  1198. while (CU8(base) == ATOM_IIO_START) {
  1199. ctx->iio[CU8(base + 1)] = base + 2;
  1200. base += 2;
  1201. while (CU8(base) != ATOM_IIO_END)
  1202. base += atom_iio_len[CU8(base)];
  1203. base += 3;
  1204. }
  1205. }
  1206. struct atom_context *amdgpu_atom_parse(struct card_info *card, void *bios)
  1207. {
  1208. int base;
  1209. struct atom_context *ctx =
  1210. kzalloc(sizeof(struct atom_context), GFP_KERNEL);
  1211. char *str;
  1212. char name[512];
  1213. int i;
  1214. if (!ctx)
  1215. return NULL;
  1216. ctx->card = card;
  1217. ctx->bios = bios;
  1218. if (CU16(0) != ATOM_BIOS_MAGIC) {
  1219. printk(KERN_INFO "Invalid BIOS magic.\n");
  1220. kfree(ctx);
  1221. return NULL;
  1222. }
  1223. if (strncmp
  1224. (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
  1225. strlen(ATOM_ATI_MAGIC))) {
  1226. printk(KERN_INFO "Invalid ATI magic.\n");
  1227. kfree(ctx);
  1228. return NULL;
  1229. }
  1230. base = CU16(ATOM_ROM_TABLE_PTR);
  1231. if (strncmp
  1232. (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
  1233. strlen(ATOM_ROM_MAGIC))) {
  1234. printk(KERN_INFO "Invalid ATOM magic.\n");
  1235. kfree(ctx);
  1236. return NULL;
  1237. }
  1238. ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
  1239. ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
  1240. atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
  1241. if (!ctx->iio) {
  1242. amdgpu_atom_destroy(ctx);
  1243. return NULL;
  1244. }
  1245. str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
  1246. while (*str && ((*str == '\n') || (*str == '\r')))
  1247. str++;
  1248. /* name string isn't always 0 terminated */
  1249. for (i = 0; i < 511; i++) {
  1250. name[i] = str[i];
  1251. if (name[i] < '.' || name[i] > 'z') {
  1252. name[i] = 0;
  1253. break;
  1254. }
  1255. }
  1256. printk(KERN_INFO "ATOM BIOS: %s\n", name);
  1257. return ctx;
  1258. }
  1259. int amdgpu_atom_asic_init(struct atom_context *ctx)
  1260. {
  1261. int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
  1262. uint32_t ps[16];
  1263. int ret;
  1264. memset(ps, 0, 64);
  1265. ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
  1266. ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
  1267. if (!ps[0] || !ps[1])
  1268. return 1;
  1269. if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
  1270. return 1;
  1271. ret = amdgpu_atom_execute_table(ctx, ATOM_CMD_INIT, ps);
  1272. if (ret)
  1273. return ret;
  1274. memset(ps, 0, 64);
  1275. return ret;
  1276. }
  1277. void amdgpu_atom_destroy(struct atom_context *ctx)
  1278. {
  1279. kfree(ctx->iio);
  1280. kfree(ctx);
  1281. }
  1282. bool amdgpu_atom_parse_data_header(struct atom_context *ctx, int index,
  1283. uint16_t * size, uint8_t * frev, uint8_t * crev,
  1284. uint16_t * data_start)
  1285. {
  1286. int offset = index * 2 + 4;
  1287. int idx = CU16(ctx->data_table + offset);
  1288. u16 *mdt = (u16 *)(ctx->bios + ctx->data_table + 4);
  1289. if (!mdt[index])
  1290. return false;
  1291. if (size)
  1292. *size = CU16(idx);
  1293. if (frev)
  1294. *frev = CU8(idx + 2);
  1295. if (crev)
  1296. *crev = CU8(idx + 3);
  1297. *data_start = idx;
  1298. return true;
  1299. }
  1300. bool amdgpu_atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
  1301. uint8_t * crev)
  1302. {
  1303. int offset = index * 2 + 4;
  1304. int idx = CU16(ctx->cmd_table + offset);
  1305. u16 *mct = (u16 *)(ctx->bios + ctx->cmd_table + 4);
  1306. if (!mct[index])
  1307. return false;
  1308. if (frev)
  1309. *frev = CU8(idx + 2);
  1310. if (crev)
  1311. *crev = CU8(idx + 3);
  1312. return true;
  1313. }
  1314. int amdgpu_atom_allocate_fb_scratch(struct atom_context *ctx)
  1315. {
  1316. int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
  1317. uint16_t data_offset;
  1318. int usage_bytes = 0;
  1319. struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
  1320. if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
  1321. firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
  1322. DRM_DEBUG("atom firmware requested %08x %dkb\n",
  1323. le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
  1324. le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
  1325. usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
  1326. }
  1327. ctx->scratch_size_bytes = 0;
  1328. if (usage_bytes == 0)
  1329. usage_bytes = 20 * 1024;
  1330. /* allocate some scratch memory */
  1331. ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
  1332. if (!ctx->scratch)
  1333. return -ENOMEM;
  1334. ctx->scratch_size_bytes = usage_bytes;
  1335. return 0;
  1336. }