atom.c 29 KB

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