atom.c 33 KB

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