3 * ATI Mach64 Hardware Acceleration
6 #include <linux/delay.h>
8 #include <video/mach64.h>
12 * Generic Mach64 routines
15 /* this is for DMA GUI engine! work in progress */
21 } BM_DESCRIPTOR_ENTRY;
23 #define LAST_DESCRIPTOR (1 << 31)
24 #define SYSTEM_TO_FRAME_BUFFER 0
26 static u32 rotation24bpp(u32 dx, u32 direction)
29 if (direction & DST_X_LEFT_TO_RIGHT) {
30 rotation = (dx / 4) % 6;
32 rotation = ((dx + 2) / 4) % 6;
35 return ((rotation << 8) | DST_24_ROTATION_ENABLE);
38 void aty_reset_engine(const struct atyfb_par *par)
41 aty_st_le32(GEN_TEST_CNTL,
42 aty_ld_le32(GEN_TEST_CNTL, par) & ~GUI_ENGINE_ENABLE, par);
44 aty_st_le32(GEN_TEST_CNTL,
45 aty_ld_le32(GEN_TEST_CNTL, par) | GUI_ENGINE_ENABLE, par);
46 /* ensure engine is not locked up by clearing any FIFO or */
49 aty_ld_le32(BUS_CNTL, par) | BUS_HOST_ERR_ACK | BUS_FIFO_ERR_ACK, par);
52 static void reset_GTC_3D_engine(const struct atyfb_par *par)
54 aty_st_le32(SCALE_3D_CNTL, 0xc0, par);
55 mdelay(GTC_3D_RESET_DELAY);
56 aty_st_le32(SETUP_CNTL, 0x00, par);
57 mdelay(GTC_3D_RESET_DELAY);
58 aty_st_le32(SCALE_3D_CNTL, 0x00, par);
59 mdelay(GTC_3D_RESET_DELAY);
62 void aty_init_engine(struct atyfb_par *par, struct fb_info *info)
66 /* determine modal information from global mode structure */
67 pitch_value = info->var.xres_virtual;
69 if (info->var.bits_per_pixel == 24) {
70 /* In 24 bpp, the engine is in 8 bpp - this requires that all */
71 /* horizontal coordinates and widths must be adjusted */
75 /* On GTC (RagePro), we need to reset the 3D engine before */
76 if (M64_HAS(RESET_3D))
77 reset_GTC_3D_engine(par);
79 /* Reset engine, enable, and clear any engine errors */
80 aty_reset_engine(par);
81 /* Ensure that vga page pointers are set to zero - the upper */
82 /* page pointers are set to 1 to handle overflows in the */
84 aty_st_le32(MEM_VGA_WP_SEL, 0x00010000, par);
85 aty_st_le32(MEM_VGA_RP_SEL, 0x00010000, par);
87 /* ---- Setup standard engine context ---- */
89 /* All GUI registers here are FIFOed - therefore, wait for */
90 /* the appropriate number of empty FIFO entries */
91 wait_for_fifo(14, par);
93 /* enable all registers to be loaded for context loads */
94 aty_st_le32(CONTEXT_MASK, 0xFFFFFFFF, par);
96 /* set destination pitch to modal pitch, set offset to zero */
97 aty_st_le32(DST_OFF_PITCH, (pitch_value / 8) << 22, par);
99 /* zero these registers (set them to a known state) */
100 aty_st_le32(DST_Y_X, 0, par);
101 aty_st_le32(DST_HEIGHT, 0, par);
102 aty_st_le32(DST_BRES_ERR, 0, par);
103 aty_st_le32(DST_BRES_INC, 0, par);
104 aty_st_le32(DST_BRES_DEC, 0, par);
106 /* set destination drawing attributes */
107 aty_st_le32(DST_CNTL, DST_LAST_PEL | DST_Y_TOP_TO_BOTTOM |
108 DST_X_LEFT_TO_RIGHT, par);
110 /* set source pitch to modal pitch, set offset to zero */
111 aty_st_le32(SRC_OFF_PITCH, (pitch_value / 8) << 22, par);
113 /* set these registers to a known state */
114 aty_st_le32(SRC_Y_X, 0, par);
115 aty_st_le32(SRC_HEIGHT1_WIDTH1, 1, par);
116 aty_st_le32(SRC_Y_X_START, 0, par);
117 aty_st_le32(SRC_HEIGHT2_WIDTH2, 1, par);
119 /* set source pixel retrieving attributes */
120 aty_st_le32(SRC_CNTL, SRC_LINE_X_LEFT_TO_RIGHT, par);
122 /* set host attributes */
123 wait_for_fifo(13, par);
124 aty_st_le32(HOST_CNTL, 0, par);
126 /* set pattern attributes */
127 aty_st_le32(PAT_REG0, 0, par);
128 aty_st_le32(PAT_REG1, 0, par);
129 aty_st_le32(PAT_CNTL, 0, par);
131 /* set scissors to modal size */
132 aty_st_le32(SC_LEFT, 0, par);
133 aty_st_le32(SC_TOP, 0, par);
134 aty_st_le32(SC_BOTTOM, par->crtc.vyres - 1, par);
135 aty_st_le32(SC_RIGHT, pitch_value - 1, par);
137 /* set background color to minimum value (usually BLACK) */
138 aty_st_le32(DP_BKGD_CLR, 0, par);
140 /* set foreground color to maximum value (usually WHITE) */
141 aty_st_le32(DP_FRGD_CLR, 0xFFFFFFFF, par);
143 /* set write mask to effect all pixel bits */
144 aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF, par);
146 /* set foreground mix to overpaint and background mix to */
148 aty_st_le32(DP_MIX, FRGD_MIX_S | BKGD_MIX_D, par);
150 /* set primary source pixel channel to foreground color */
152 aty_st_le32(DP_SRC, FRGD_SRC_FRGD_CLR, par);
154 /* set compare functionality to false (no-effect on */
156 wait_for_fifo(3, par);
157 aty_st_le32(CLR_CMP_CLR, 0, par);
158 aty_st_le32(CLR_CMP_MASK, 0xFFFFFFFF, par);
159 aty_st_le32(CLR_CMP_CNTL, 0, par);
161 /* set pixel depth */
162 wait_for_fifo(2, par);
163 aty_st_le32(DP_PIX_WIDTH, par->crtc.dp_pix_width, par);
164 aty_st_le32(DP_CHAIN_MASK, par->crtc.dp_chain_mask, par);
166 wait_for_fifo(5, par);
167 aty_st_le32(SCALE_3D_CNTL, 0, par);
168 aty_st_le32(Z_CNTL, 0, par);
169 aty_st_le32(CRTC_INT_CNTL, aty_ld_le32(CRTC_INT_CNTL, par) & ~0x20,
171 aty_st_le32(GUI_TRAJ_CNTL, 0x100023, par);
173 /* insure engine is idle before leaving */
178 * Accelerated functions
181 static inline void draw_rect(s16 x, s16 y, u16 width, u16 height,
182 struct atyfb_par *par)
184 /* perform rectangle fill */
185 wait_for_fifo(2, par);
186 aty_st_le32(DST_Y_X, (x << 16) | y, par);
187 aty_st_le32(DST_HEIGHT_WIDTH, (width << 16) | height, par);
188 par->blitter_may_be_busy = 1;
191 void atyfb_copyarea(struct fb_info *info, const struct fb_copyarea *area)
193 struct atyfb_par *par = (struct atyfb_par *) info->par;
194 u32 dy = area->dy, sy = area->sy, direction = DST_LAST_PEL;
195 u32 sx = area->sx, dx = area->dx, width = area->width, rotation = 0;
199 if (!area->width || !area->height)
201 if (!par->accel_flags) {
202 cfb_copyarea(info, area);
206 if (info->var.bits_per_pixel == 24) {
207 /* In 24 bpp, the engine is in 8 bpp - this requires that all */
208 /* horizontal coordinates and widths must be adjusted */
214 if (area->sy < area->dy) {
215 dy += area->height - 1;
216 sy += area->height - 1;
218 direction |= DST_Y_TOP_TO_BOTTOM;
224 direction |= DST_X_LEFT_TO_RIGHT;
226 if (info->var.bits_per_pixel == 24) {
227 rotation = rotation24bpp(dx, direction);
230 wait_for_fifo(4, par);
231 aty_st_le32(DP_SRC, FRGD_SRC_BLIT, par);
232 aty_st_le32(SRC_Y_X, (sx << 16) | sy, par);
233 aty_st_le32(SRC_HEIGHT1_WIDTH1, (width << 16) | area->height, par);
234 aty_st_le32(DST_CNTL, direction | rotation, par);
235 draw_rect(dx, dy, width, area->height, par);
238 void atyfb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
240 struct atyfb_par *par = (struct atyfb_par *) info->par;
241 u32 color = rect->color, dx = rect->dx, width = rect->width, rotation = 0;
245 if (!rect->width || !rect->height)
247 if (!par->accel_flags) {
248 cfb_fillrect(info, rect);
252 color |= (rect->color << 8);
253 color |= (rect->color << 16);
255 if (info->var.bits_per_pixel == 24) {
256 /* In 24 bpp, the engine is in 8 bpp - this requires that all */
257 /* horizontal coordinates and widths must be adjusted */
260 rotation = rotation24bpp(dx, DST_X_LEFT_TO_RIGHT);
263 wait_for_fifo(3, par);
264 aty_st_le32(DP_FRGD_CLR, color, par);
266 BKGD_SRC_BKGD_CLR | FRGD_SRC_FRGD_CLR | MONO_SRC_ONE,
268 aty_st_le32(DST_CNTL,
269 DST_LAST_PEL | DST_Y_TOP_TO_BOTTOM |
270 DST_X_LEFT_TO_RIGHT | rotation, par);
271 draw_rect(dx, rect->dy, width, rect->height, par);
274 void atyfb_imageblit(struct fb_info *info, const struct fb_image *image)
276 struct atyfb_par *par = (struct atyfb_par *) info->par;
277 u32 src_bytes, dx = image->dx, dy = image->dy, width = image->width;
278 u32 pix_width_save, pix_width, host_cntl, rotation = 0, src, mix;
282 if (!image->width || !image->height)
284 if (!par->accel_flags ||
285 (image->depth != 1 && info->var.bits_per_pixel != image->depth)) {
286 cfb_imageblit(info, image);
290 pix_width = pix_width_save = aty_ld_le32(DP_PIX_WIDTH, par);
291 host_cntl = aty_ld_le32(HOST_CNTL, par) | HOST_BYTE_ALIGN;
293 switch (image->depth) {
295 pix_width &= ~(BYTE_ORDER_MASK | HOST_MASK);
296 pix_width |= (BYTE_ORDER_MSB_TO_LSB | HOST_1BPP);
299 pix_width &= ~(BYTE_ORDER_MASK | HOST_MASK);
300 pix_width |= (BYTE_ORDER_MSB_TO_LSB | HOST_4BPP);
303 pix_width &= ~HOST_MASK;
304 pix_width |= HOST_8BPP;
307 pix_width &= ~HOST_MASK;
308 pix_width |= HOST_15BPP;
311 pix_width &= ~HOST_MASK;
312 pix_width |= HOST_16BPP;
315 pix_width &= ~HOST_MASK;
316 pix_width |= HOST_24BPP;
319 pix_width &= ~HOST_MASK;
320 pix_width |= HOST_32BPP;
324 if (info->var.bits_per_pixel == 24) {
325 /* In 24 bpp, the engine is in 8 bpp - this requires that all */
326 /* horizontal coordinates and widths must be adjusted */
330 rotation = rotation24bpp(dx, DST_X_LEFT_TO_RIGHT);
332 pix_width &= ~DST_MASK;
333 pix_width |= DST_8BPP;
336 * since Rage 3D IIc we have DP_HOST_TRIPLE_EN bit
337 * this hwaccelerated triple has an issue with not aligned data
339 if (M64_HAS(HW_TRIPLE) && image->width % 8 == 0)
340 pix_width |= DP_HOST_TRIPLE_EN;
343 if (image->depth == 1) {
345 if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
346 info->fix.visual == FB_VISUAL_DIRECTCOLOR) {
347 fg = ((u32*)(info->pseudo_palette))[image->fg_color];
348 bg = ((u32*)(info->pseudo_palette))[image->bg_color];
350 fg = image->fg_color;
351 bg = image->bg_color;
354 wait_for_fifo(2, par);
355 aty_st_le32(DP_BKGD_CLR, bg, par);
356 aty_st_le32(DP_FRGD_CLR, fg, par);
357 src = MONO_SRC_HOST | FRGD_SRC_FRGD_CLR | BKGD_SRC_BKGD_CLR;
358 mix = FRGD_MIX_S | BKGD_MIX_S;
360 src = MONO_SRC_ONE | FRGD_SRC_HOST;
361 mix = FRGD_MIX_D_XOR_S | BKGD_MIX_D;
364 wait_for_fifo(6, par);
365 aty_st_le32(DP_WRITE_MASK, 0xFFFFFFFF, par);
366 aty_st_le32(DP_PIX_WIDTH, pix_width, par);
367 aty_st_le32(DP_MIX, mix, par);
368 aty_st_le32(DP_SRC, src, par);
369 aty_st_le32(HOST_CNTL, host_cntl, par);
370 aty_st_le32(DST_CNTL, DST_Y_TOP_TO_BOTTOM | DST_X_LEFT_TO_RIGHT | rotation, par);
372 draw_rect(dx, dy, width, image->height, par);
373 src_bytes = (((image->width * image->depth) + 7) / 8) * image->height;
375 /* manual triple each pixel */
376 if (info->var.bits_per_pixel == 24 && !(pix_width & DP_HOST_TRIPLE_EN)) {
377 int inbit, outbit, mult24, byte_id_in_dword, width;
378 u8 *pbitmapin = (u8*)image->data, *pbitmapout;
381 for (width = image->width, inbit = 7, mult24 = 0; src_bytes; ) {
382 for (hostdword = 0, pbitmapout = (u8*)&hostdword, byte_id_in_dword = 0;
383 byte_id_in_dword < 4 && src_bytes;
384 byte_id_in_dword++, pbitmapout++) {
385 for (outbit = 7; outbit >= 0; outbit--) {
386 *pbitmapout |= (((*pbitmapin >> inbit) & 1) << outbit);
396 if (inbit < 0 || width == 0) {
402 width = image->width;
408 wait_for_fifo(1, par);
409 aty_st_le32(HOST_DATA0, hostdword, par);
412 u32 *pbitmap, dwords = (src_bytes + 3) / 4;
413 for (pbitmap = (u32*)(image->data); dwords; dwords--, pbitmap++) {
414 wait_for_fifo(1, par);
415 aty_st_le32(HOST_DATA0, le32_to_cpup(pbitmap), par);
419 /* restore pix_width */
420 wait_for_fifo(1, par);
421 aty_st_le32(DP_PIX_WIDTH, pix_width_save, par);