mach64_accel.c 11 KB

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