atom.c 32 KB

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