atom.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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. uint8_t val = U8((*ptr)++);
  969. SDEBUG("DEBUG output: 0x%02X\n", val);
  970. }
  971. static struct {
  972. void (*func) (atom_exec_context *, int *, int);
  973. int arg;
  974. } opcode_table[ATOM_OP_CNT] = {
  975. {
  976. NULL, 0}, {
  977. atom_op_move, ATOM_ARG_REG}, {
  978. atom_op_move, ATOM_ARG_PS}, {
  979. atom_op_move, ATOM_ARG_WS}, {
  980. atom_op_move, ATOM_ARG_FB}, {
  981. atom_op_move, ATOM_ARG_PLL}, {
  982. atom_op_move, ATOM_ARG_MC}, {
  983. atom_op_and, ATOM_ARG_REG}, {
  984. atom_op_and, ATOM_ARG_PS}, {
  985. atom_op_and, ATOM_ARG_WS}, {
  986. atom_op_and, ATOM_ARG_FB}, {
  987. atom_op_and, ATOM_ARG_PLL}, {
  988. atom_op_and, ATOM_ARG_MC}, {
  989. atom_op_or, ATOM_ARG_REG}, {
  990. atom_op_or, ATOM_ARG_PS}, {
  991. atom_op_or, ATOM_ARG_WS}, {
  992. atom_op_or, ATOM_ARG_FB}, {
  993. atom_op_or, ATOM_ARG_PLL}, {
  994. atom_op_or, ATOM_ARG_MC}, {
  995. atom_op_shift_left, ATOM_ARG_REG}, {
  996. atom_op_shift_left, ATOM_ARG_PS}, {
  997. atom_op_shift_left, ATOM_ARG_WS}, {
  998. atom_op_shift_left, ATOM_ARG_FB}, {
  999. atom_op_shift_left, ATOM_ARG_PLL}, {
  1000. atom_op_shift_left, ATOM_ARG_MC}, {
  1001. atom_op_shift_right, ATOM_ARG_REG}, {
  1002. atom_op_shift_right, ATOM_ARG_PS}, {
  1003. atom_op_shift_right, ATOM_ARG_WS}, {
  1004. atom_op_shift_right, ATOM_ARG_FB}, {
  1005. atom_op_shift_right, ATOM_ARG_PLL}, {
  1006. atom_op_shift_right, ATOM_ARG_MC}, {
  1007. atom_op_mul, ATOM_ARG_REG}, {
  1008. atom_op_mul, ATOM_ARG_PS}, {
  1009. atom_op_mul, ATOM_ARG_WS}, {
  1010. atom_op_mul, ATOM_ARG_FB}, {
  1011. atom_op_mul, ATOM_ARG_PLL}, {
  1012. atom_op_mul, ATOM_ARG_MC}, {
  1013. atom_op_div, ATOM_ARG_REG}, {
  1014. atom_op_div, ATOM_ARG_PS}, {
  1015. atom_op_div, ATOM_ARG_WS}, {
  1016. atom_op_div, ATOM_ARG_FB}, {
  1017. atom_op_div, ATOM_ARG_PLL}, {
  1018. atom_op_div, ATOM_ARG_MC}, {
  1019. atom_op_add, ATOM_ARG_REG}, {
  1020. atom_op_add, ATOM_ARG_PS}, {
  1021. atom_op_add, ATOM_ARG_WS}, {
  1022. atom_op_add, ATOM_ARG_FB}, {
  1023. atom_op_add, ATOM_ARG_PLL}, {
  1024. atom_op_add, ATOM_ARG_MC}, {
  1025. atom_op_sub, ATOM_ARG_REG}, {
  1026. atom_op_sub, ATOM_ARG_PS}, {
  1027. atom_op_sub, ATOM_ARG_WS}, {
  1028. atom_op_sub, ATOM_ARG_FB}, {
  1029. atom_op_sub, ATOM_ARG_PLL}, {
  1030. atom_op_sub, ATOM_ARG_MC}, {
  1031. atom_op_setport, ATOM_PORT_ATI}, {
  1032. atom_op_setport, ATOM_PORT_PCI}, {
  1033. atom_op_setport, ATOM_PORT_SYSIO}, {
  1034. atom_op_setregblock, 0}, {
  1035. atom_op_setfbbase, 0}, {
  1036. atom_op_compare, ATOM_ARG_REG}, {
  1037. atom_op_compare, ATOM_ARG_PS}, {
  1038. atom_op_compare, ATOM_ARG_WS}, {
  1039. atom_op_compare, ATOM_ARG_FB}, {
  1040. atom_op_compare, ATOM_ARG_PLL}, {
  1041. atom_op_compare, ATOM_ARG_MC}, {
  1042. atom_op_switch, 0}, {
  1043. atom_op_jump, ATOM_COND_ALWAYS}, {
  1044. atom_op_jump, ATOM_COND_EQUAL}, {
  1045. atom_op_jump, ATOM_COND_BELOW}, {
  1046. atom_op_jump, ATOM_COND_ABOVE}, {
  1047. atom_op_jump, ATOM_COND_BELOWOREQUAL}, {
  1048. atom_op_jump, ATOM_COND_ABOVEOREQUAL}, {
  1049. atom_op_jump, ATOM_COND_NOTEQUAL}, {
  1050. atom_op_test, ATOM_ARG_REG}, {
  1051. atom_op_test, ATOM_ARG_PS}, {
  1052. atom_op_test, ATOM_ARG_WS}, {
  1053. atom_op_test, ATOM_ARG_FB}, {
  1054. atom_op_test, ATOM_ARG_PLL}, {
  1055. atom_op_test, ATOM_ARG_MC}, {
  1056. atom_op_delay, ATOM_UNIT_MILLISEC}, {
  1057. atom_op_delay, ATOM_UNIT_MICROSEC}, {
  1058. atom_op_calltable, 0}, {
  1059. atom_op_repeat, 0}, {
  1060. atom_op_clear, ATOM_ARG_REG}, {
  1061. atom_op_clear, ATOM_ARG_PS}, {
  1062. atom_op_clear, ATOM_ARG_WS}, {
  1063. atom_op_clear, ATOM_ARG_FB}, {
  1064. atom_op_clear, ATOM_ARG_PLL}, {
  1065. atom_op_clear, ATOM_ARG_MC}, {
  1066. atom_op_nop, 0}, {
  1067. atom_op_eot, 0}, {
  1068. atom_op_mask, ATOM_ARG_REG}, {
  1069. atom_op_mask, ATOM_ARG_PS}, {
  1070. atom_op_mask, ATOM_ARG_WS}, {
  1071. atom_op_mask, ATOM_ARG_FB}, {
  1072. atom_op_mask, ATOM_ARG_PLL}, {
  1073. atom_op_mask, ATOM_ARG_MC}, {
  1074. atom_op_postcard, 0}, {
  1075. atom_op_beep, 0}, {
  1076. atom_op_savereg, 0}, {
  1077. atom_op_restorereg, 0}, {
  1078. atom_op_setdatablock, 0}, {
  1079. atom_op_xor, ATOM_ARG_REG}, {
  1080. atom_op_xor, ATOM_ARG_PS}, {
  1081. atom_op_xor, ATOM_ARG_WS}, {
  1082. atom_op_xor, ATOM_ARG_FB}, {
  1083. atom_op_xor, ATOM_ARG_PLL}, {
  1084. atom_op_xor, ATOM_ARG_MC}, {
  1085. atom_op_shl, ATOM_ARG_REG}, {
  1086. atom_op_shl, ATOM_ARG_PS}, {
  1087. atom_op_shl, ATOM_ARG_WS}, {
  1088. atom_op_shl, ATOM_ARG_FB}, {
  1089. atom_op_shl, ATOM_ARG_PLL}, {
  1090. atom_op_shl, ATOM_ARG_MC}, {
  1091. atom_op_shr, ATOM_ARG_REG}, {
  1092. atom_op_shr, ATOM_ARG_PS}, {
  1093. atom_op_shr, ATOM_ARG_WS}, {
  1094. atom_op_shr, ATOM_ARG_FB}, {
  1095. atom_op_shr, ATOM_ARG_PLL}, {
  1096. atom_op_shr, ATOM_ARG_MC}, {
  1097. atom_op_debug, 0},};
  1098. static int amdgpu_atom_execute_table_locked(struct atom_context *ctx, int index, uint32_t * params)
  1099. {
  1100. int base = CU16(ctx->cmd_table + 4 + 2 * index);
  1101. int len, ws, ps, ptr;
  1102. unsigned char op;
  1103. atom_exec_context ectx;
  1104. int ret = 0;
  1105. if (!base)
  1106. return -EINVAL;
  1107. len = CU16(base + ATOM_CT_SIZE_PTR);
  1108. ws = CU8(base + ATOM_CT_WS_PTR);
  1109. ps = CU8(base + ATOM_CT_PS_PTR) & ATOM_CT_PS_MASK;
  1110. ptr = base + ATOM_CT_CODE_PTR;
  1111. SDEBUG(">> execute %04X (len %d, WS %d, PS %d)\n", base, len, ws, ps);
  1112. ectx.ctx = ctx;
  1113. ectx.ps_shift = ps / 4;
  1114. ectx.start = base;
  1115. ectx.ps = params;
  1116. ectx.abort = false;
  1117. ectx.last_jump = 0;
  1118. if (ws)
  1119. ectx.ws = kzalloc(4 * ws, GFP_KERNEL);
  1120. else
  1121. ectx.ws = NULL;
  1122. debug_depth++;
  1123. while (1) {
  1124. op = CU8(ptr++);
  1125. if (op < ATOM_OP_NAMES_CNT)
  1126. SDEBUG("%s @ 0x%04X\n", atom_op_names[op], ptr - 1);
  1127. else
  1128. SDEBUG("[%d] @ 0x%04X\n", op, ptr - 1);
  1129. if (ectx.abort) {
  1130. DRM_ERROR("atombios stuck executing %04X (len %d, WS %d, PS %d) @ 0x%04X\n",
  1131. base, len, ws, ps, ptr - 1);
  1132. ret = -EINVAL;
  1133. goto free;
  1134. }
  1135. if (op < ATOM_OP_CNT && op > 0)
  1136. opcode_table[op].func(&ectx, &ptr,
  1137. opcode_table[op].arg);
  1138. else
  1139. break;
  1140. if (op == ATOM_OP_EOT)
  1141. break;
  1142. }
  1143. debug_depth--;
  1144. SDEBUG("<<\n");
  1145. free:
  1146. if (ws)
  1147. kfree(ectx.ws);
  1148. return ret;
  1149. }
  1150. int amdgpu_atom_execute_table(struct atom_context *ctx, int index, uint32_t * params)
  1151. {
  1152. int r;
  1153. mutex_lock(&ctx->mutex);
  1154. /* reset data block */
  1155. ctx->data_block = 0;
  1156. /* reset reg block */
  1157. ctx->reg_block = 0;
  1158. /* reset fb window */
  1159. ctx->fb_base = 0;
  1160. /* reset io mode */
  1161. ctx->io_mode = ATOM_IO_MM;
  1162. /* reset divmul */
  1163. ctx->divmul[0] = 0;
  1164. ctx->divmul[1] = 0;
  1165. r = amdgpu_atom_execute_table_locked(ctx, index, params);
  1166. mutex_unlock(&ctx->mutex);
  1167. return r;
  1168. }
  1169. static int atom_iio_len[] = { 1, 2, 3, 3, 3, 3, 4, 4, 4, 3 };
  1170. static void atom_index_iio(struct atom_context *ctx, int base)
  1171. {
  1172. ctx->iio = kzalloc(2 * 256, GFP_KERNEL);
  1173. if (!ctx->iio)
  1174. return;
  1175. while (CU8(base) == ATOM_IIO_START) {
  1176. ctx->iio[CU8(base + 1)] = base + 2;
  1177. base += 2;
  1178. while (CU8(base) != ATOM_IIO_END)
  1179. base += atom_iio_len[CU8(base)];
  1180. base += 3;
  1181. }
  1182. }
  1183. struct atom_context *amdgpu_atom_parse(struct card_info *card, void *bios)
  1184. {
  1185. int base;
  1186. struct atom_context *ctx =
  1187. kzalloc(sizeof(struct atom_context), GFP_KERNEL);
  1188. char *str;
  1189. char name[512];
  1190. int i;
  1191. if (!ctx)
  1192. return NULL;
  1193. ctx->card = card;
  1194. ctx->bios = bios;
  1195. if (CU16(0) != ATOM_BIOS_MAGIC) {
  1196. printk(KERN_INFO "Invalid BIOS magic.\n");
  1197. kfree(ctx);
  1198. return NULL;
  1199. }
  1200. if (strncmp
  1201. (CSTR(ATOM_ATI_MAGIC_PTR), ATOM_ATI_MAGIC,
  1202. strlen(ATOM_ATI_MAGIC))) {
  1203. printk(KERN_INFO "Invalid ATI magic.\n");
  1204. kfree(ctx);
  1205. return NULL;
  1206. }
  1207. base = CU16(ATOM_ROM_TABLE_PTR);
  1208. if (strncmp
  1209. (CSTR(base + ATOM_ROM_MAGIC_PTR), ATOM_ROM_MAGIC,
  1210. strlen(ATOM_ROM_MAGIC))) {
  1211. printk(KERN_INFO "Invalid ATOM magic.\n");
  1212. kfree(ctx);
  1213. return NULL;
  1214. }
  1215. ctx->cmd_table = CU16(base + ATOM_ROM_CMD_PTR);
  1216. ctx->data_table = CU16(base + ATOM_ROM_DATA_PTR);
  1217. atom_index_iio(ctx, CU16(ctx->data_table + ATOM_DATA_IIO_PTR) + 4);
  1218. if (!ctx->iio) {
  1219. amdgpu_atom_destroy(ctx);
  1220. return NULL;
  1221. }
  1222. str = CSTR(CU16(base + ATOM_ROM_MSG_PTR));
  1223. while (*str && ((*str == '\n') || (*str == '\r')))
  1224. str++;
  1225. /* name string isn't always 0 terminated */
  1226. for (i = 0; i < 511; i++) {
  1227. name[i] = str[i];
  1228. if (name[i] < '.' || name[i] > 'z') {
  1229. name[i] = 0;
  1230. break;
  1231. }
  1232. }
  1233. printk(KERN_INFO "ATOM BIOS: %s\n", name);
  1234. return ctx;
  1235. }
  1236. int amdgpu_atom_asic_init(struct atom_context *ctx)
  1237. {
  1238. int hwi = CU16(ctx->data_table + ATOM_DATA_FWI_PTR);
  1239. uint32_t ps[16];
  1240. int ret;
  1241. memset(ps, 0, 64);
  1242. ps[0] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFSCLK_PTR));
  1243. ps[1] = cpu_to_le32(CU32(hwi + ATOM_FWI_DEFMCLK_PTR));
  1244. if (!ps[0] || !ps[1])
  1245. return 1;
  1246. if (!CU16(ctx->cmd_table + 4 + 2 * ATOM_CMD_INIT))
  1247. return 1;
  1248. ret = amdgpu_atom_execute_table(ctx, ATOM_CMD_INIT, ps);
  1249. if (ret)
  1250. return ret;
  1251. memset(ps, 0, 64);
  1252. return ret;
  1253. }
  1254. void amdgpu_atom_destroy(struct atom_context *ctx)
  1255. {
  1256. kfree(ctx->iio);
  1257. kfree(ctx);
  1258. }
  1259. bool amdgpu_atom_parse_data_header(struct atom_context *ctx, int index,
  1260. uint16_t * size, uint8_t * frev, uint8_t * crev,
  1261. uint16_t * data_start)
  1262. {
  1263. int offset = index * 2 + 4;
  1264. int idx = CU16(ctx->data_table + offset);
  1265. u16 *mdt = (u16 *)(ctx->bios + ctx->data_table + 4);
  1266. if (!mdt[index])
  1267. return false;
  1268. if (size)
  1269. *size = CU16(idx);
  1270. if (frev)
  1271. *frev = CU8(idx + 2);
  1272. if (crev)
  1273. *crev = CU8(idx + 3);
  1274. *data_start = idx;
  1275. return true;
  1276. }
  1277. bool amdgpu_atom_parse_cmd_header(struct atom_context *ctx, int index, uint8_t * frev,
  1278. uint8_t * crev)
  1279. {
  1280. int offset = index * 2 + 4;
  1281. int idx = CU16(ctx->cmd_table + offset);
  1282. u16 *mct = (u16 *)(ctx->bios + ctx->cmd_table + 4);
  1283. if (!mct[index])
  1284. return false;
  1285. if (frev)
  1286. *frev = CU8(idx + 2);
  1287. if (crev)
  1288. *crev = CU8(idx + 3);
  1289. return true;
  1290. }
  1291. int amdgpu_atom_allocate_fb_scratch(struct atom_context *ctx)
  1292. {
  1293. int index = GetIndexIntoMasterTable(DATA, VRAM_UsageByFirmware);
  1294. uint16_t data_offset;
  1295. int usage_bytes = 0;
  1296. struct _ATOM_VRAM_USAGE_BY_FIRMWARE *firmware_usage;
  1297. if (amdgpu_atom_parse_data_header(ctx, index, NULL, NULL, NULL, &data_offset)) {
  1298. firmware_usage = (struct _ATOM_VRAM_USAGE_BY_FIRMWARE *)(ctx->bios + data_offset);
  1299. DRM_DEBUG("atom firmware requested %08x %dkb\n",
  1300. le32_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].ulStartAddrUsedByFirmware),
  1301. le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb));
  1302. usage_bytes = le16_to_cpu(firmware_usage->asFirmwareVramReserveInfo[0].usFirmwareUseInKb) * 1024;
  1303. }
  1304. ctx->scratch_size_bytes = 0;
  1305. if (usage_bytes == 0)
  1306. usage_bytes = 20 * 1024;
  1307. /* allocate some scratch memory */
  1308. ctx->scratch = kzalloc(usage_bytes, GFP_KERNEL);
  1309. if (!ctx->scratch)
  1310. return -ENOMEM;
  1311. ctx->scratch_size_bytes = usage_bytes;
  1312. return 0;
  1313. }