mach64_accel.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * ATI Mach64 Hardware Acceleration
  3. */
  4. #include <linux/delay.h>
  5. #include <asm/unaligned.h>
  6. #include <linux/fb.h>
  7. #include <video/mach64.h>
  8. #include "atyfb.h"
  9. /*
  10. * Generic Mach64 routines
  11. */
  12. /* this is for DMA GUI engine! work in progress */
  13. typedef struct {
  14. u32 frame_buf_offset;
  15. u32 system_mem_addr;
  16. u32 command;
  17. u32 reserved;
  18. } BM_DESCRIPTOR_ENTRY;
  19. #define LAST_DESCRIPTOR (1 << 31)
  20. #define SYSTEM_TO_FRAME_BUFFER 0
  21. static u32 rotation24bpp(u32 dx, u32 direction)
  22. {
  23. u32 rotation;
  24. if (direction & DST_X_LEFT_TO_RIGHT) {
  25. rotation = (dx / 4) % 6;
  26. } else {
  27. rotation = ((dx + 2) / 4) % 6;
  28. }
  29. return ((rotation << 8) | DST_24_ROTATION_ENABLE);
  30. }
  31. void aty_reset_engine(const struct atyfb_par *par)
  32. {
  33. /* reset engine */
  34. aty_st_le32(GEN_TEST_CNTL,
  35. aty_ld_le32(GEN_TEST_CNTL, par) &
  36. ~(GUI_ENGINE_ENABLE | HWCURSOR_ENABLE), par);
  37. /* enable engine */
  38. aty_st_le32(GEN_TEST_CNTL,
  39. aty_ld_le32(GEN_TEST_CNTL, par) | GUI_ENGINE_ENABLE, par);
  40. /* ensure engine is not locked up by clearing any FIFO or */
  41. /* HOST errors */
  42. aty_st_le32(BUS_CNTL,
  43. aty_ld_le32(BUS_CNTL, par) | BUS_HOST_ERR_ACK | BUS_FIFO_ERR_ACK, par);
  44. }
  45. static void reset_GTC_3D_engine(const struct atyfb_par *par)
  46. {
  47. aty_st_le32(SCALE_3D_CNTL, 0xc0, par);
  48. mdelay(GTC_3D_RESET_DELAY);
  49. aty_st_le32(SETUP_CNTL, 0x00, par);
  50. mdelay(GTC_3D_RESET_DELAY);
  51. aty_st_le32(SCALE_3D_CNTL, 0x00, par);
  52. mdelay(GTC_3D_RESET_DELAY);
  53. }
  54. void aty_init_engine(struct atyfb_par *par, struct fb_info *info)
  55. {
  56. u32 pitch_value;
  57. u32 vxres;
  58. /* determine modal information from global mode structure */
  59. pitch_value = info->fix.line_length / (info->var.bits_per_pixel / 8);
  60. vxres = info->var.xres_virtual;
  61. if (info->var.bits_per_pixel == 24) {
  62. /* In 24 bpp, the engine is in 8 bpp - this requires that all */
  63. /* horizontal coordinates and widths must be adjusted */
  64. pitch_value *= 3;
  65. vxres *= 3;
  66. }
  67. /* On GTC (RagePro), we need to reset the 3D engine before */
  68. if (M64_HAS(RESET_3D))
  69. reset_GTC_3D_engine(par);
  70. /* Reset engine, enable, and clear any engine errors */
  71. aty_reset_engine(par);
  72. /* Ensure that vga page pointers are set to zero - the upper */
  73. /* page pointers are set to 1 to handle overflows in the */
  74. /* lower page */
  75. aty_st_le32(MEM_VGA_WP_SEL, 0x00010000, par);
  76. aty_st_le32(MEM_VGA_RP_SEL, 0x00010000, par);
  77. /* ---- Setup standard engine context ---- */
  78. /* All GUI registers here are FIFOed - therefore, wait for */
  79. /* the appropriate number of empty FIFO entries */
  80. wait_for_fifo(14, par);
  81. /* enable all registers to be loaded for context loads */
  82. aty_st_le32(CONTEXT_MASK, 0xFFFFFFFF, par);
  83. /* set destination pitch to modal pitch, set offset to zero */
  84. aty_st_le32(DST_OFF_PITCH, (pitch_value / 8) << 22, par);
  85. /* zero these registers (set them to a known state) */
  86. aty_st_le32(DST_Y_X, 0, par);
  87. aty_st_le32(DST_HEIGHT, 0, par);
  88. aty_st_le32(DST_BRES_ERR, 0, par);
  89. aty_st_le32(DST_BRES_INC, 0, par);
  90. aty_st_le32(DST_BRES_DEC, 0, par);
  91. /* set destination drawing attributes */
  92. aty_st_le32(DST_CNTL, DST_LAST_PEL | DST_Y_TOP_TO_BOTTOM |
  93. DST_X_LEFT_TO_RIGHT, par);
  94. /* set source pitch to modal pitch, set offset to zero */
  95. aty_st_le32(SRC_OFF_PITCH, (pitch_value / 8) << 22, par);
  96. /* set these registers to a known state */
  97. aty_st_le32(SRC_Y_X, 0, par);
  98. aty_st_le32(SRC_HEIGHT1_WIDTH1, 1, par);
  99. aty_st_le32(SRC_Y_X_START, 0, par);
  100. aty_st_le32(SRC_HEIGHT2_WIDTH2, 1, par);
  101. /* set source pixel retrieving attributes */
  102. aty_st_le32(SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT, par);
  103. /* set host attributes */
  104. wait_for_fifo(13, par);
  105. aty_st_le32(HOST_CNTL, 0, par);
  106. /* set pattern attributes */
  107. aty_st_le32(PAT_REG0, 0, par);
  108. aty_st_le32(PAT_REG1, 0, par);
  109. aty_st_le32(PAT_CNTL, 0, par);
  110. /* set scissors to modal size */
  111. aty_st_le32(SC_LEFT, 0, par);
  112. aty_st_le32(SC_TOP, 0, par);
  113. aty_st_le32(SC_BOTTOM, par->crtc.vyres - 1, par);
  114. aty_st_le32(SC_RIGHT, vxres - 1, par);
  115. /* set background color to minimum value (usually BLACK) */
  116. aty_st_le32(DP_BKGD_CLR, 0, par);
  117. /* set foreground color to maximum value (usually WHITE) */
  118. aty_st_le32(DP_FRGD_CLR, 0xFFFFFFFF, par);
  119. /* set write mask to effect all pixel bits */
  120. aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF, par);
  121. /* set foreground mix to overpaint and background mix to */
  122. /* no-effect */
  123. aty_st_le32(DP_MIX, FRGD_MIX_S | BKGD_MIX_D, par);
  124. /* set primary source pixel channel to foreground color */
  125. /* register */
  126. aty_st_le32(DP_SRC, FRGD_SRC_FRGD_CLR, par);
  127. /* set compare functionality to false (no-effect on */
  128. /* destination) */
  129. wait_for_fifo(3, par);
  130. aty_st_le32(CLR_CMP_CLR, 0, par);
  131. aty_st_le32(CLR_CMP_MASK, 0xFFFFFFFF, par);
  132. aty_st_le32(CLR_CMP_CNTL, 0, par);
  133. /* set pixel depth */
  134. wait_for_fifo(2, par);
  135. aty_st_le32(DP_PIX_WIDTH, par->crtc.dp_pix_width, par);
  136. aty_st_le32(DP_CHAIN_MASK, par->crtc.dp_chain_mask, par);
  137. wait_for_fifo(5, par);
  138. aty_st_le32(SCALE_3D_CNTL, 0, par);
  139. aty_st_le32(Z_CNTL, 0, par);
  140. aty_st_le32(CRTC_INT_CNTL, aty_ld_le32(CRTC_INT_CNTL, par) & ~0x20,
  141. par);
  142. aty_st_le32(GUI_TRAJ_CNTL, 0x100023, par);
  143. /* insure engine is idle before leaving */
  144. wait_for_idle(par);
  145. }
  146. /*
  147. * Accelerated functions
  148. */
  149. static inline void draw_rect(s16 x, s16 y, u16 width, u16 height,
  150. struct atyfb_par *par)
  151. {
  152. /* perform rectangle fill */
  153. wait_for_fifo(2, par);
  154. aty_st_le32(DST_Y_X, (x << 16) | y, par);
  155. aty_st_le32(DST_HEIGHT_WIDTH, (width << 16) | height, par);
  156. par->blitter_may_be_busy = 1;
  157. }
  158. void atyfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
  159. {
  160. struct atyfb_par *par = (struct atyfb_par *) info->par;
  161. u32 dy = area->dy, sy = area->sy, direction = DST_LAST_PEL;
  162. u32 sx = area->sx, dx = area->dx, width = area->width, rotation = 0;
  163. if (par->asleep)
  164. return;
  165. if (!area->width || !area->height)
  166. return;
  167. if (!par->accel_flags) {
  168. cfb_copyarea(info, area);
  169. return;
  170. }
  171. if (info->var.bits_per_pixel == 24) {
  172. /* In 24 bpp, the engine is in 8 bpp - this requires that all */
  173. /* horizontal coordinates and widths must be adjusted */
  174. sx *= 3;
  175. dx *= 3;
  176. width *= 3;
  177. }
  178. if (area->sy < area->dy) {
  179. dy += area->height - 1;
  180. sy += area->height - 1;
  181. } else
  182. direction |= DST_Y_TOP_TO_BOTTOM;
  183. if (sx < dx) {
  184. dx += width - 1;
  185. sx += width - 1;
  186. } else
  187. direction |= DST_X_LEFT_TO_RIGHT;
  188. if (info->var.bits_per_pixel == 24) {
  189. rotation = rotation24bpp(dx, direction);
  190. }
  191. wait_for_fifo(4, par);
  192. aty_st_le32(DP_SRC, FRGD_SRC_BLIT, par);
  193. aty_st_le32(SRC_Y_X, (sx << 16) | sy, par);
  194. aty_st_le32(SRC_HEIGHT1_WIDTH1, (width << 16) | area->height, par);
  195. aty_st_le32(DST_CNTL, direction | rotation, par);
  196. draw_rect(dx, dy, width, area->height, par);
  197. }
  198. void atyfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  199. {
  200. struct atyfb_par *par = (struct atyfb_par *) info->par;
  201. u32 color, dx = rect->dx, width = rect->width, rotation = 0;
  202. if (par->asleep)
  203. return;
  204. if (!rect->width || !rect->height)
  205. return;
  206. if (!par->accel_flags) {
  207. cfb_fillrect(info, rect);
  208. return;
  209. }
  210. if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  211. info->fix.visual == FB_VISUAL_DIRECTCOLOR)
  212. color = ((u32 *)(info->pseudo_palette))[rect->color];
  213. else
  214. color = rect->color;
  215. if (info->var.bits_per_pixel == 24) {
  216. /* In 24 bpp, the engine is in 8 bpp - this requires that all */
  217. /* horizontal coordinates and widths must be adjusted */
  218. dx *= 3;
  219. width *= 3;
  220. rotation = rotation24bpp(dx, DST_X_LEFT_TO_RIGHT);
  221. }
  222. wait_for_fifo(3, par);
  223. aty_st_le32(DP_FRGD_CLR, color, par);
  224. aty_st_le32(DP_SRC,
  225. BKGD_SRC_BKGD_CLR | FRGD_SRC_FRGD_CLR | MONO_SRC_ONE,
  226. par);
  227. aty_st_le32(DST_CNTL,
  228. DST_LAST_PEL | DST_Y_TOP_TO_BOTTOM |
  229. DST_X_LEFT_TO_RIGHT | rotation, par);
  230. draw_rect(dx, rect->dy, width, rect->height, par);
  231. }
  232. void atyfb_imageblit(struct fb_info *info, const struct fb_image *image)
  233. {
  234. struct atyfb_par *par = (struct atyfb_par *) info->par;
  235. u32 src_bytes, dx = image->dx, dy = image->dy, width = image->width;
  236. u32 pix_width_save, pix_width, host_cntl, rotation = 0, src, mix;
  237. if (par->asleep)
  238. return;
  239. if (!image->width || !image->height)
  240. return;
  241. if (!par->accel_flags ||
  242. (image->depth != 1 && info->var.bits_per_pixel != image->depth)) {
  243. cfb_imageblit(info, image);
  244. return;
  245. }
  246. pix_width = pix_width_save = aty_ld_le32(DP_PIX_WIDTH, par);
  247. host_cntl = aty_ld_le32(HOST_CNTL, par) | HOST_BYTE_ALIGN;
  248. switch (image->depth) {
  249. case 1:
  250. pix_width &= ~(BYTE_ORDER_MASK | HOST_MASK);
  251. pix_width |= (BYTE_ORDER_MSB_TO_LSB | HOST_1BPP);
  252. break;
  253. case 4:
  254. pix_width &= ~(BYTE_ORDER_MASK | HOST_MASK);
  255. pix_width |= (BYTE_ORDER_MSB_TO_LSB | HOST_4BPP);
  256. break;
  257. case 8:
  258. pix_width &= ~HOST_MASK;
  259. pix_width |= HOST_8BPP;
  260. break;
  261. case 15:
  262. pix_width &= ~HOST_MASK;
  263. pix_width |= HOST_15BPP;
  264. break;
  265. case 16:
  266. pix_width &= ~HOST_MASK;
  267. pix_width |= HOST_16BPP;
  268. break;
  269. case 24:
  270. pix_width &= ~HOST_MASK;
  271. pix_width |= HOST_24BPP;
  272. break;
  273. case 32:
  274. pix_width &= ~HOST_MASK;
  275. pix_width |= HOST_32BPP;
  276. break;
  277. }
  278. if (info->var.bits_per_pixel == 24) {
  279. /* In 24 bpp, the engine is in 8 bpp - this requires that all */
  280. /* horizontal coordinates and widths must be adjusted */
  281. dx *= 3;
  282. width *= 3;
  283. rotation = rotation24bpp(dx, DST_X_LEFT_TO_RIGHT);
  284. pix_width &= ~DST_MASK;
  285. pix_width |= DST_8BPP;
  286. /*
  287. * since Rage 3D IIc we have DP_HOST_TRIPLE_EN bit
  288. * this hwaccelerated triple has an issue with not aligned data
  289. */
  290. if (M64_HAS(HW_TRIPLE) && image->width % 8 == 0)
  291. pix_width |= DP_HOST_TRIPLE_EN;
  292. }
  293. if (image->depth == 1) {
  294. u32 fg, bg;
  295. if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  296. info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
  297. fg = ((u32*)(info->pseudo_palette))[image->fg_color];
  298. bg = ((u32*)(info->pseudo_palette))[image->bg_color];
  299. } else {
  300. fg = image->fg_color;
  301. bg = image->bg_color;
  302. }
  303. wait_for_fifo(2, par);
  304. aty_st_le32(DP_BKGD_CLR, bg, par);
  305. aty_st_le32(DP_FRGD_CLR, fg, par);
  306. src = MONO_SRC_HOST | FRGD_SRC_FRGD_CLR | BKGD_SRC_BKGD_CLR;
  307. mix = FRGD_MIX_S | BKGD_MIX_S;
  308. } else {
  309. src = MONO_SRC_ONE | FRGD_SRC_HOST;
  310. mix = FRGD_MIX_D_XOR_S | BKGD_MIX_D;
  311. }
  312. wait_for_fifo(6, par);
  313. aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF, par);
  314. aty_st_le32(DP_PIX_WIDTH, pix_width, par);
  315. aty_st_le32(DP_MIX, mix, par);
  316. aty_st_le32(DP_SRC, src, par);
  317. aty_st_le32(HOST_CNTL, host_cntl, par);
  318. aty_st_le32(DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT | rotation, par);
  319. draw_rect(dx, dy, width, image->height, par);
  320. src_bytes = (((image->width * image->depth) + 7) / 8) * image->height;
  321. /* manual triple each pixel */
  322. if (info->var.bits_per_pixel == 24 && !(pix_width & DP_HOST_TRIPLE_EN)) {
  323. int inbit, outbit, mult24, byte_id_in_dword, width;
  324. u8 *pbitmapin = (u8*)image->data, *pbitmapout;
  325. u32 hostdword;
  326. for (width = image->width, inbit = 7, mult24 = 0; src_bytes; ) {
  327. for (hostdword = 0, pbitmapout = (u8*)&hostdword, byte_id_in_dword = 0;
  328. byte_id_in_dword < 4 && src_bytes;
  329. byte_id_in_dword++, pbitmapout++) {
  330. for (outbit = 7; outbit >= 0; outbit--) {
  331. *pbitmapout |= (((*pbitmapin >> inbit) & 1) << outbit);
  332. mult24++;
  333. /* next bit */
  334. if (mult24 == 3) {
  335. mult24 = 0;
  336. inbit--;
  337. width--;
  338. }
  339. /* next byte */
  340. if (inbit < 0 || width == 0) {
  341. src_bytes--;
  342. pbitmapin++;
  343. inbit = 7;
  344. if (width == 0) {
  345. width = image->width;
  346. outbit = 0;
  347. }
  348. }
  349. }
  350. }
  351. wait_for_fifo(1, par);
  352. aty_st_le32(HOST_DATA0, hostdword, par);
  353. }
  354. } else {
  355. u32 *pbitmap, dwords = (src_bytes + 3) / 4;
  356. for (pbitmap = (u32*)(image->data); dwords; dwords--, pbitmap++) {
  357. wait_for_fifo(1, par);
  358. aty_st_le32(HOST_DATA0, get_unaligned_le32(pbitmap), par);
  359. }
  360. }
  361. /* restore pix_width */
  362. wait_for_fifo(1, par);
  363. aty_st_le32(DP_PIX_WIDTH, pix_width_save, par);
  364. }