atom.c 35 KB

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