mach64_accel.c 12 KB

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