atom.c 30 KB

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