atom.c 32 KB

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