1 /* mga_state.c -- State support for MGA G200/G400 -*- linux-c -*-
2 * Created: Thu Jan 27 02:53:43 2000 by jhartmann@precisioninsight.com
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 * Jeff Hartmann <jhartmann@valinux.com>
29 * Keith Whitwell <keith@tungstengraphics.com>
32 * Gareth Hughes <gareth@valinux.com>
40 /* ================================================================
41 * DMA hardware state programming functions
44 static void mga_emit_clip_rect( drm_mga_private_t *dev_priv,
45 drm_clip_rect_t *box )
47 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
48 drm_mga_context_regs_t *ctx = &sarea_priv->context_state;
49 unsigned int pitch = dev_priv->front_pitch;
54 /* Force reset of DWGCTL on G400 (eliminates clip disable bit).
56 if (dev_priv->chipset == MGA_CARD_TYPE_G400) {
57 DMA_BLOCK(MGA_DWGCTL, ctx->dwgctl,
58 MGA_LEN + MGA_EXEC, 0x80000000,
59 MGA_DWGCTL, ctx->dwgctl,
60 MGA_LEN + MGA_EXEC, 0x80000000);
62 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
63 MGA_CXBNDRY, ((box->x2 - 1) << 16) | box->x1,
64 MGA_YTOP, box->y1 * pitch,
65 MGA_YBOT, (box->y2 - 1) * pitch);
70 static __inline__ void mga_g200_emit_context( drm_mga_private_t *dev_priv )
72 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
73 drm_mga_context_regs_t *ctx = &sarea_priv->context_state;
78 DMA_BLOCK( MGA_DSTORG, ctx->dstorg,
79 MGA_MACCESS, ctx->maccess,
80 MGA_PLNWT, ctx->plnwt,
81 MGA_DWGCTL, ctx->dwgctl );
83 DMA_BLOCK( MGA_ALPHACTRL, ctx->alphactrl,
84 MGA_FOGCOL, ctx->fogcolor,
85 MGA_WFLAG, ctx->wflag,
86 MGA_ZORG, dev_priv->depth_offset );
88 DMA_BLOCK( MGA_FCOL, ctx->fcol,
89 MGA_DMAPAD, 0x00000000,
90 MGA_DMAPAD, 0x00000000,
91 MGA_DMAPAD, 0x00000000 );
96 static __inline__ void mga_g400_emit_context( drm_mga_private_t *dev_priv )
98 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
99 drm_mga_context_regs_t *ctx = &sarea_priv->context_state;
104 DMA_BLOCK( MGA_DSTORG, ctx->dstorg,
105 MGA_MACCESS, ctx->maccess,
106 MGA_PLNWT, ctx->plnwt,
107 MGA_DWGCTL, ctx->dwgctl );
109 DMA_BLOCK( MGA_ALPHACTRL, ctx->alphactrl,
110 MGA_FOGCOL, ctx->fogcolor,
111 MGA_WFLAG, ctx->wflag,
112 MGA_ZORG, dev_priv->depth_offset );
114 DMA_BLOCK( MGA_WFLAG1, ctx->wflag,
115 MGA_TDUALSTAGE0, ctx->tdualstage0,
116 MGA_TDUALSTAGE1, ctx->tdualstage1,
117 MGA_FCOL, ctx->fcol );
119 DMA_BLOCK( MGA_STENCIL, ctx->stencil,
120 MGA_STENCILCTL, ctx->stencilctl,
121 MGA_DMAPAD, 0x00000000,
122 MGA_DMAPAD, 0x00000000 );
127 static __inline__ void mga_g200_emit_tex0( drm_mga_private_t *dev_priv )
129 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
130 drm_mga_texture_regs_t *tex = &sarea_priv->tex_state[0];
135 DMA_BLOCK( MGA_TEXCTL2, tex->texctl2,
136 MGA_TEXCTL, tex->texctl,
137 MGA_TEXFILTER, tex->texfilter,
138 MGA_TEXBORDERCOL, tex->texbordercol );
140 DMA_BLOCK( MGA_TEXORG, tex->texorg,
141 MGA_TEXORG1, tex->texorg1,
142 MGA_TEXORG2, tex->texorg2,
143 MGA_TEXORG3, tex->texorg3 );
145 DMA_BLOCK( MGA_TEXORG4, tex->texorg4,
146 MGA_TEXWIDTH, tex->texwidth,
147 MGA_TEXHEIGHT, tex->texheight,
148 MGA_WR24, tex->texwidth );
150 DMA_BLOCK( MGA_WR34, tex->texheight,
151 MGA_TEXTRANS, 0x0000ffff,
152 MGA_TEXTRANSHIGH, 0x0000ffff,
153 MGA_DMAPAD, 0x00000000 );
158 static __inline__ void mga_g400_emit_tex0( drm_mga_private_t *dev_priv )
160 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
161 drm_mga_texture_regs_t *tex = &sarea_priv->tex_state[0];
164 /* printk("mga_g400_emit_tex0 %x %x %x\n", tex->texorg, */
165 /* tex->texctl, tex->texctl2); */
169 DMA_BLOCK( MGA_TEXCTL2, tex->texctl2 | MGA_G400_TC2_MAGIC,
170 MGA_TEXCTL, tex->texctl,
171 MGA_TEXFILTER, tex->texfilter,
172 MGA_TEXBORDERCOL, tex->texbordercol );
174 DMA_BLOCK( MGA_TEXORG, tex->texorg,
175 MGA_TEXORG1, tex->texorg1,
176 MGA_TEXORG2, tex->texorg2,
177 MGA_TEXORG3, tex->texorg3 );
179 DMA_BLOCK( MGA_TEXORG4, tex->texorg4,
180 MGA_TEXWIDTH, tex->texwidth,
181 MGA_TEXHEIGHT, tex->texheight,
182 MGA_WR49, 0x00000000 );
184 DMA_BLOCK( MGA_WR57, 0x00000000,
185 MGA_WR53, 0x00000000,
186 MGA_WR61, 0x00000000,
187 MGA_WR52, MGA_G400_WR_MAGIC );
189 DMA_BLOCK( MGA_WR60, MGA_G400_WR_MAGIC,
190 MGA_WR54, tex->texwidth | MGA_G400_WR_MAGIC,
191 MGA_WR62, tex->texheight | MGA_G400_WR_MAGIC,
192 MGA_DMAPAD, 0x00000000 );
194 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
195 MGA_DMAPAD, 0x00000000,
196 MGA_TEXTRANS, 0x0000ffff,
197 MGA_TEXTRANSHIGH, 0x0000ffff );
202 static __inline__ void mga_g400_emit_tex1( drm_mga_private_t *dev_priv )
204 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
205 drm_mga_texture_regs_t *tex = &sarea_priv->tex_state[1];
208 /* printk("mga_g400_emit_tex1 %x %x %x\n", tex->texorg, */
209 /* tex->texctl, tex->texctl2); */
213 DMA_BLOCK( MGA_TEXCTL2, (tex->texctl2 |
216 MGA_TEXCTL, tex->texctl,
217 MGA_TEXFILTER, tex->texfilter,
218 MGA_TEXBORDERCOL, tex->texbordercol );
220 DMA_BLOCK( MGA_TEXORG, tex->texorg,
221 MGA_TEXORG1, tex->texorg1,
222 MGA_TEXORG2, tex->texorg2,
223 MGA_TEXORG3, tex->texorg3 );
225 DMA_BLOCK( MGA_TEXORG4, tex->texorg4,
226 MGA_TEXWIDTH, tex->texwidth,
227 MGA_TEXHEIGHT, tex->texheight,
228 MGA_WR49, 0x00000000 );
230 DMA_BLOCK( MGA_WR57, 0x00000000,
231 MGA_WR53, 0x00000000,
232 MGA_WR61, 0x00000000,
233 MGA_WR52, tex->texwidth | MGA_G400_WR_MAGIC );
235 DMA_BLOCK( MGA_WR60, tex->texheight | MGA_G400_WR_MAGIC,
236 MGA_TEXTRANS, 0x0000ffff,
237 MGA_TEXTRANSHIGH, 0x0000ffff,
238 MGA_TEXCTL2, tex->texctl2 | MGA_G400_TC2_MAGIC );
243 static __inline__ void mga_g200_emit_pipe( drm_mga_private_t *dev_priv )
245 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
246 unsigned int pipe = sarea_priv->warp_pipe;
251 DMA_BLOCK( MGA_WIADDR, MGA_WMODE_SUSPEND,
252 MGA_WVRTXSZ, 0x00000007,
253 MGA_WFLAG, 0x00000000,
254 MGA_WR24, 0x00000000 );
256 DMA_BLOCK( MGA_WR25, 0x00000100,
257 MGA_WR34, 0x00000000,
258 MGA_WR42, 0x0000ffff,
259 MGA_WR60, 0x0000ffff );
261 /* Padding required to to hardware bug.
263 DMA_BLOCK(MGA_DMAPAD, 0xffffffff,
264 MGA_DMAPAD, 0xffffffff,
265 MGA_DMAPAD, 0xffffffff,
266 MGA_WIADDR, (dev_priv->warp_pipe_phys[pipe] |
267 MGA_WMODE_START | dev_priv->wagp_enable));
272 static __inline__ void mga_g400_emit_pipe( drm_mga_private_t *dev_priv )
274 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
275 unsigned int pipe = sarea_priv->warp_pipe;
278 /* printk("mga_g400_emit_pipe %x\n", pipe); */
282 DMA_BLOCK( MGA_WIADDR2, MGA_WMODE_SUSPEND,
283 MGA_DMAPAD, 0x00000000,
284 MGA_DMAPAD, 0x00000000,
285 MGA_DMAPAD, 0x00000000 );
287 if ( pipe & MGA_T2 ) {
288 DMA_BLOCK( MGA_WVRTXSZ, 0x00001e09,
289 MGA_DMAPAD, 0x00000000,
290 MGA_DMAPAD, 0x00000000,
291 MGA_DMAPAD, 0x00000000 );
293 DMA_BLOCK( MGA_WACCEPTSEQ, 0x00000000,
294 MGA_WACCEPTSEQ, 0x00000000,
295 MGA_WACCEPTSEQ, 0x00000000,
296 MGA_WACCEPTSEQ, 0x1e000000 );
298 if ( dev_priv->warp_pipe & MGA_T2 ) {
299 /* Flush the WARP pipe */
300 DMA_BLOCK( MGA_YDST, 0x00000000,
301 MGA_FXLEFT, 0x00000000,
302 MGA_FXRIGHT, 0x00000001,
303 MGA_DWGCTL, MGA_DWGCTL_FLUSH );
305 DMA_BLOCK( MGA_LEN + MGA_EXEC, 0x00000001,
306 MGA_DWGSYNC, 0x00007000,
307 MGA_TEXCTL2, MGA_G400_TC2_MAGIC,
308 MGA_LEN + MGA_EXEC, 0x00000000 );
310 DMA_BLOCK( MGA_TEXCTL2, (MGA_DUALTEX |
312 MGA_LEN + MGA_EXEC, 0x00000000,
313 MGA_TEXCTL2, MGA_G400_TC2_MAGIC,
314 MGA_DMAPAD, 0x00000000 );
317 DMA_BLOCK( MGA_WVRTXSZ, 0x00001807,
318 MGA_DMAPAD, 0x00000000,
319 MGA_DMAPAD, 0x00000000,
320 MGA_DMAPAD, 0x00000000 );
322 DMA_BLOCK( MGA_WACCEPTSEQ, 0x00000000,
323 MGA_WACCEPTSEQ, 0x00000000,
324 MGA_WACCEPTSEQ, 0x00000000,
325 MGA_WACCEPTSEQ, 0x18000000 );
328 DMA_BLOCK( MGA_WFLAG, 0x00000000,
329 MGA_WFLAG1, 0x00000000,
330 MGA_WR56, MGA_G400_WR56_MAGIC,
331 MGA_DMAPAD, 0x00000000 );
333 DMA_BLOCK( MGA_WR49, 0x00000000, /* tex0 */
334 MGA_WR57, 0x00000000, /* tex0 */
335 MGA_WR53, 0x00000000, /* tex1 */
336 MGA_WR61, 0x00000000 ); /* tex1 */
338 DMA_BLOCK( MGA_WR54, MGA_G400_WR_MAGIC, /* tex0 width */
339 MGA_WR62, MGA_G400_WR_MAGIC, /* tex0 height */
340 MGA_WR52, MGA_G400_WR_MAGIC, /* tex1 width */
341 MGA_WR60, MGA_G400_WR_MAGIC ); /* tex1 height */
343 /* Padding required to to hardware bug */
344 DMA_BLOCK(MGA_DMAPAD, 0xffffffff,
345 MGA_DMAPAD, 0xffffffff,
346 MGA_DMAPAD, 0xffffffff,
347 MGA_WIADDR2, (dev_priv->warp_pipe_phys[pipe] |
348 MGA_WMODE_START | dev_priv->wagp_enable));
353 static void mga_g200_emit_state( drm_mga_private_t *dev_priv )
355 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
356 unsigned int dirty = sarea_priv->dirty;
358 if ( sarea_priv->warp_pipe != dev_priv->warp_pipe ) {
359 mga_g200_emit_pipe( dev_priv );
360 dev_priv->warp_pipe = sarea_priv->warp_pipe;
363 if ( dirty & MGA_UPLOAD_CONTEXT ) {
364 mga_g200_emit_context( dev_priv );
365 sarea_priv->dirty &= ~MGA_UPLOAD_CONTEXT;
368 if ( dirty & MGA_UPLOAD_TEX0 ) {
369 mga_g200_emit_tex0( dev_priv );
370 sarea_priv->dirty &= ~MGA_UPLOAD_TEX0;
374 static void mga_g400_emit_state( drm_mga_private_t *dev_priv )
376 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
377 unsigned int dirty = sarea_priv->dirty;
378 int multitex = sarea_priv->warp_pipe & MGA_T2;
380 if ( sarea_priv->warp_pipe != dev_priv->warp_pipe ) {
381 mga_g400_emit_pipe( dev_priv );
382 dev_priv->warp_pipe = sarea_priv->warp_pipe;
385 if ( dirty & MGA_UPLOAD_CONTEXT ) {
386 mga_g400_emit_context( dev_priv );
387 sarea_priv->dirty &= ~MGA_UPLOAD_CONTEXT;
390 if ( dirty & MGA_UPLOAD_TEX0 ) {
391 mga_g400_emit_tex0( dev_priv );
392 sarea_priv->dirty &= ~MGA_UPLOAD_TEX0;
395 if ( (dirty & MGA_UPLOAD_TEX1) && multitex ) {
396 mga_g400_emit_tex1( dev_priv );
397 sarea_priv->dirty &= ~MGA_UPLOAD_TEX1;
402 /* ================================================================
403 * SAREA state verification
406 /* Disallow all write destinations except the front and backbuffer.
408 static int mga_verify_context( drm_mga_private_t *dev_priv )
410 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
411 drm_mga_context_regs_t *ctx = &sarea_priv->context_state;
413 if ( ctx->dstorg != dev_priv->front_offset &&
414 ctx->dstorg != dev_priv->back_offset ) {
415 DRM_ERROR( "*** bad DSTORG: %x (front %x, back %x)\n\n",
416 ctx->dstorg, dev_priv->front_offset,
417 dev_priv->back_offset );
419 return DRM_ERR(EINVAL);
425 /* Disallow texture reads from PCI space.
427 static int mga_verify_tex( drm_mga_private_t *dev_priv, int unit )
429 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
430 drm_mga_texture_regs_t *tex = &sarea_priv->tex_state[unit];
433 org = tex->texorg & (MGA_TEXORGMAP_MASK | MGA_TEXORGACC_MASK);
435 if ( org == (MGA_TEXORGMAP_SYSMEM | MGA_TEXORGACC_PCI) ) {
436 DRM_ERROR( "*** bad TEXORG: 0x%x, unit %d\n",
439 return DRM_ERR(EINVAL);
445 static int mga_verify_state( drm_mga_private_t *dev_priv )
447 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
448 unsigned int dirty = sarea_priv->dirty;
451 if ( sarea_priv->nbox > MGA_NR_SAREA_CLIPRECTS )
452 sarea_priv->nbox = MGA_NR_SAREA_CLIPRECTS;
454 if ( dirty & MGA_UPLOAD_CONTEXT )
455 ret |= mga_verify_context( dev_priv );
457 if ( dirty & MGA_UPLOAD_TEX0 )
458 ret |= mga_verify_tex( dev_priv, 0 );
460 if (dev_priv->chipset >= MGA_CARD_TYPE_G400) {
461 if (dirty & MGA_UPLOAD_TEX1)
462 ret |= mga_verify_tex(dev_priv, 1);
464 if ( dirty & MGA_UPLOAD_PIPE )
465 ret |= ( sarea_priv->warp_pipe > MGA_MAX_G400_PIPES );
467 if ( dirty & MGA_UPLOAD_PIPE )
468 ret |= ( sarea_priv->warp_pipe > MGA_MAX_G200_PIPES );
474 static int mga_verify_iload( drm_mga_private_t *dev_priv,
475 unsigned int dstorg, unsigned int length )
477 if ( dstorg < dev_priv->texture_offset ||
478 dstorg + length > (dev_priv->texture_offset +
479 dev_priv->texture_size) ) {
480 DRM_ERROR( "*** bad iload DSTORG: 0x%x\n", dstorg );
481 return DRM_ERR(EINVAL);
484 if ( length & MGA_ILOAD_MASK ) {
485 DRM_ERROR( "*** bad iload length: 0x%x\n",
486 length & MGA_ILOAD_MASK );
487 return DRM_ERR(EINVAL);
493 static int mga_verify_blit( drm_mga_private_t *dev_priv,
494 unsigned int srcorg, unsigned int dstorg )
496 if ( (srcorg & 0x3) == (MGA_SRCACC_PCI | MGA_SRCMAP_SYSMEM) ||
497 (dstorg & 0x3) == (MGA_SRCACC_PCI | MGA_SRCMAP_SYSMEM) ) {
498 DRM_ERROR( "*** bad blit: src=0x%x dst=0x%x\n",
500 return DRM_ERR(EINVAL);
506 /* ================================================================
510 static void mga_dma_dispatch_clear( drm_device_t *dev,
511 drm_mga_clear_t *clear )
513 drm_mga_private_t *dev_priv = dev->dev_private;
514 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
515 drm_mga_context_regs_t *ctx = &sarea_priv->context_state;
516 drm_clip_rect_t *pbox = sarea_priv->boxes;
517 int nbox = sarea_priv->nbox;
524 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
525 MGA_DMAPAD, 0x00000000,
526 MGA_DWGSYNC, 0x00007100,
527 MGA_DWGSYNC, 0x00007000 );
531 for ( i = 0 ; i < nbox ; i++ ) {
532 drm_clip_rect_t *box = &pbox[i];
533 u32 height = box->y2 - box->y1;
535 DRM_DEBUG( " from=%d,%d to=%d,%d\n",
536 box->x1, box->y1, box->x2, box->y2 );
538 if ( clear->flags & MGA_FRONT ) {
541 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
542 MGA_PLNWT, clear->color_mask,
543 MGA_YDSTLEN, (box->y1 << 16) | height,
544 MGA_FXBNDRY, (box->x2 << 16) | box->x1 );
546 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
547 MGA_FCOL, clear->clear_color,
548 MGA_DSTORG, dev_priv->front_offset,
549 MGA_DWGCTL + MGA_EXEC,
550 dev_priv->clear_cmd );
556 if ( clear->flags & MGA_BACK ) {
559 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
560 MGA_PLNWT, clear->color_mask,
561 MGA_YDSTLEN, (box->y1 << 16) | height,
562 MGA_FXBNDRY, (box->x2 << 16) | box->x1 );
564 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
565 MGA_FCOL, clear->clear_color,
566 MGA_DSTORG, dev_priv->back_offset,
567 MGA_DWGCTL + MGA_EXEC,
568 dev_priv->clear_cmd );
573 if ( clear->flags & MGA_DEPTH ) {
576 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
577 MGA_PLNWT, clear->depth_mask,
578 MGA_YDSTLEN, (box->y1 << 16) | height,
579 MGA_FXBNDRY, (box->x2 << 16) | box->x1 );
581 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
582 MGA_FCOL, clear->clear_depth,
583 MGA_DSTORG, dev_priv->depth_offset,
584 MGA_DWGCTL + MGA_EXEC,
585 dev_priv->clear_cmd );
594 /* Force reset of DWGCTL */
595 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
596 MGA_DMAPAD, 0x00000000,
597 MGA_PLNWT, ctx->plnwt,
598 MGA_DWGCTL, ctx->dwgctl );
605 static void mga_dma_dispatch_swap( drm_device_t *dev )
607 drm_mga_private_t *dev_priv = dev->dev_private;
608 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
609 drm_mga_context_regs_t *ctx = &sarea_priv->context_state;
610 drm_clip_rect_t *pbox = sarea_priv->boxes;
611 int nbox = sarea_priv->nbox;
616 sarea_priv->last_frame.head = dev_priv->prim.tail;
617 sarea_priv->last_frame.wrap = dev_priv->prim.last_wrap;
619 BEGIN_DMA( 4 + nbox );
621 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
622 MGA_DMAPAD, 0x00000000,
623 MGA_DWGSYNC, 0x00007100,
624 MGA_DWGSYNC, 0x00007000 );
626 DMA_BLOCK( MGA_DSTORG, dev_priv->front_offset,
627 MGA_MACCESS, dev_priv->maccess,
628 MGA_SRCORG, dev_priv->back_offset,
629 MGA_AR5, dev_priv->front_pitch );
631 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
632 MGA_DMAPAD, 0x00000000,
633 MGA_PLNWT, 0xffffffff,
634 MGA_DWGCTL, MGA_DWGCTL_COPY );
636 for ( i = 0 ; i < nbox ; i++ ) {
637 drm_clip_rect_t *box = &pbox[i];
638 u32 height = box->y2 - box->y1;
639 u32 start = box->y1 * dev_priv->front_pitch;
641 DRM_DEBUG( " from=%d,%d to=%d,%d\n",
642 box->x1, box->y1, box->x2, box->y2 );
644 DMA_BLOCK( MGA_AR0, start + box->x2 - 1,
645 MGA_AR3, start + box->x1,
646 MGA_FXBNDRY, ((box->x2 - 1) << 16) | box->x1,
647 MGA_YDSTLEN + MGA_EXEC,
648 (box->y1 << 16) | height );
651 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
652 MGA_PLNWT, ctx->plnwt,
653 MGA_SRCORG, dev_priv->front_offset,
654 MGA_DWGCTL, ctx->dwgctl );
660 DRM_DEBUG( "%s... done.\n", __FUNCTION__ );
663 static void mga_dma_dispatch_vertex( drm_device_t *dev, drm_buf_t *buf )
665 drm_mga_private_t *dev_priv = dev->dev_private;
666 drm_mga_buf_priv_t *buf_priv = buf->dev_private;
667 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
668 u32 address = (u32) buf->bus_address;
669 u32 length = (u32) buf->used;
672 DRM_DEBUG( "vertex: buf=%d used=%d\n", buf->idx, buf->used );
675 buf_priv->dispatched = 1;
677 MGA_EMIT_STATE( dev_priv, sarea_priv->dirty );
680 if ( i < sarea_priv->nbox ) {
681 mga_emit_clip_rect( dev_priv,
682 &sarea_priv->boxes[i] );
687 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
688 MGA_DMAPAD, 0x00000000,
689 MGA_SECADDRESS, (address |
691 MGA_SECEND, ((address + length) |
692 dev_priv->dma_access));
695 } while ( ++i < sarea_priv->nbox );
698 if ( buf_priv->discard ) {
699 AGE_BUFFER( buf_priv );
702 buf_priv->dispatched = 0;
704 mga_freelist_put( dev, buf );
710 static void mga_dma_dispatch_indices( drm_device_t *dev, drm_buf_t *buf,
711 unsigned int start, unsigned int end )
713 drm_mga_private_t *dev_priv = dev->dev_private;
714 drm_mga_buf_priv_t *buf_priv = buf->dev_private;
715 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
716 u32 address = (u32) buf->bus_address;
719 DRM_DEBUG( "indices: buf=%d start=%d end=%d\n", buf->idx, start, end );
721 if ( start != end ) {
722 buf_priv->dispatched = 1;
724 MGA_EMIT_STATE( dev_priv, sarea_priv->dirty );
727 if ( i < sarea_priv->nbox ) {
728 mga_emit_clip_rect( dev_priv,
729 &sarea_priv->boxes[i] );
734 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
735 MGA_DMAPAD, 0x00000000,
736 MGA_SETUPADDRESS, address + start,
737 MGA_SETUPEND, ((address + end) |
738 dev_priv->dma_access));
741 } while ( ++i < sarea_priv->nbox );
744 if ( buf_priv->discard ) {
745 AGE_BUFFER( buf_priv );
748 buf_priv->dispatched = 0;
750 mga_freelist_put( dev, buf );
756 /* This copies a 64 byte aligned agp region to the frambuffer with a
757 * standard blit, the ioctl needs to do checking.
759 static void mga_dma_dispatch_iload( drm_device_t *dev, drm_buf_t *buf,
760 unsigned int dstorg, unsigned int length )
762 drm_mga_private_t *dev_priv = dev->dev_private;
763 drm_mga_buf_priv_t *buf_priv = buf->dev_private;
764 drm_mga_context_regs_t *ctx = &dev_priv->sarea_priv->context_state;
765 u32 srcorg = buf->bus_address | dev_priv->dma_access | MGA_SRCMAP_SYSMEM;
768 DRM_DEBUG( "buf=%d used=%d\n", buf->idx, buf->used );
774 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
775 MGA_DMAPAD, 0x00000000,
776 MGA_DWGSYNC, 0x00007100,
777 MGA_DWGSYNC, 0x00007000 );
779 DMA_BLOCK( MGA_DSTORG, dstorg,
780 MGA_MACCESS, 0x00000000,
784 DMA_BLOCK( MGA_PITCH, 64,
785 MGA_PLNWT, 0xffffffff,
786 MGA_DMAPAD, 0x00000000,
787 MGA_DWGCTL, MGA_DWGCTL_COPY );
789 DMA_BLOCK( MGA_AR0, 63,
791 MGA_FXBNDRY, (63 << 16) | 0,
792 MGA_YDSTLEN + MGA_EXEC, y2 );
794 DMA_BLOCK( MGA_PLNWT, ctx->plnwt,
795 MGA_SRCORG, dev_priv->front_offset,
796 MGA_PITCH, dev_priv->front_pitch,
797 MGA_DWGSYNC, 0x00007000 );
801 AGE_BUFFER( buf_priv );
805 buf_priv->dispatched = 0;
807 mga_freelist_put( dev, buf );
812 static void mga_dma_dispatch_blit( drm_device_t *dev,
813 drm_mga_blit_t *blit )
815 drm_mga_private_t *dev_priv = dev->dev_private;
816 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
817 drm_mga_context_regs_t *ctx = &sarea_priv->context_state;
818 drm_clip_rect_t *pbox = sarea_priv->boxes;
819 int nbox = sarea_priv->nbox;
824 BEGIN_DMA( 4 + nbox );
826 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
827 MGA_DMAPAD, 0x00000000,
828 MGA_DWGSYNC, 0x00007100,
829 MGA_DWGSYNC, 0x00007000 );
831 DMA_BLOCK( MGA_DWGCTL, MGA_DWGCTL_COPY,
832 MGA_PLNWT, blit->planemask,
833 MGA_SRCORG, blit->srcorg,
834 MGA_DSTORG, blit->dstorg );
836 DMA_BLOCK( MGA_SGN, scandir,
837 MGA_MACCESS, dev_priv->maccess,
838 MGA_AR5, blit->ydir * blit->src_pitch,
839 MGA_PITCH, blit->dst_pitch );
841 for ( i = 0 ; i < nbox ; i++ ) {
842 int srcx = pbox[i].x1 + blit->delta_sx;
843 int srcy = pbox[i].y1 + blit->delta_sy;
844 int dstx = pbox[i].x1 + blit->delta_dx;
845 int dsty = pbox[i].y1 + blit->delta_dy;
846 int h = pbox[i].y2 - pbox[i].y1;
847 int w = pbox[i].x2 - pbox[i].x1 - 1;
850 if ( blit->ydir == -1 ) {
851 srcy = blit->height - srcy - 1;
854 start = srcy * blit->src_pitch + srcx;
856 DMA_BLOCK( MGA_AR0, start + w,
858 MGA_FXBNDRY, ((dstx + w) << 16) | (dstx & 0xffff),
859 MGA_YDSTLEN + MGA_EXEC, (dsty << 16) | h );
862 /* Do something to flush AGP?
865 /* Force reset of DWGCTL */
866 DMA_BLOCK( MGA_DMAPAD, 0x00000000,
867 MGA_PLNWT, ctx->plnwt,
868 MGA_PITCH, dev_priv->front_pitch,
869 MGA_DWGCTL, ctx->dwgctl );
875 /* ================================================================
879 static int mga_dma_clear( DRM_IOCTL_ARGS )
882 drm_mga_private_t *dev_priv = dev->dev_private;
883 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
884 drm_mga_clear_t clear;
886 LOCK_TEST_WITH_RETURN( dev, filp );
888 DRM_COPY_FROM_USER_IOCTL( clear, (drm_mga_clear_t __user *)data, sizeof(clear) );
890 if ( sarea_priv->nbox > MGA_NR_SAREA_CLIPRECTS )
891 sarea_priv->nbox = MGA_NR_SAREA_CLIPRECTS;
893 WRAP_TEST_WITH_RETURN( dev_priv );
895 mga_dma_dispatch_clear( dev, &clear );
897 /* Make sure we restore the 3D state next time.
899 dev_priv->sarea_priv->dirty |= MGA_UPLOAD_CONTEXT;
904 static int mga_dma_swap( DRM_IOCTL_ARGS )
907 drm_mga_private_t *dev_priv = dev->dev_private;
908 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
910 LOCK_TEST_WITH_RETURN( dev, filp );
912 if ( sarea_priv->nbox > MGA_NR_SAREA_CLIPRECTS )
913 sarea_priv->nbox = MGA_NR_SAREA_CLIPRECTS;
915 WRAP_TEST_WITH_RETURN( dev_priv );
917 mga_dma_dispatch_swap( dev );
919 /* Make sure we restore the 3D state next time.
921 dev_priv->sarea_priv->dirty |= MGA_UPLOAD_CONTEXT;
926 static int mga_dma_vertex( DRM_IOCTL_ARGS )
929 drm_mga_private_t *dev_priv = dev->dev_private;
930 drm_device_dma_t *dma = dev->dma;
932 drm_mga_buf_priv_t *buf_priv;
933 drm_mga_vertex_t vertex;
935 LOCK_TEST_WITH_RETURN( dev, filp );
937 DRM_COPY_FROM_USER_IOCTL( vertex,
938 (drm_mga_vertex_t __user *)data,
941 if(vertex.idx < 0 || vertex.idx > dma->buf_count) return DRM_ERR(EINVAL);
942 buf = dma->buflist[vertex.idx];
943 buf_priv = buf->dev_private;
945 buf->used = vertex.used;
946 buf_priv->discard = vertex.discard;
948 if ( !mga_verify_state( dev_priv ) ) {
949 if ( vertex.discard ) {
950 if ( buf_priv->dispatched == 1 )
951 AGE_BUFFER( buf_priv );
952 buf_priv->dispatched = 0;
953 mga_freelist_put( dev, buf );
955 return DRM_ERR(EINVAL);
958 WRAP_TEST_WITH_RETURN( dev_priv );
960 mga_dma_dispatch_vertex( dev, buf );
965 static int mga_dma_indices( DRM_IOCTL_ARGS )
968 drm_mga_private_t *dev_priv = dev->dev_private;
969 drm_device_dma_t *dma = dev->dma;
971 drm_mga_buf_priv_t *buf_priv;
972 drm_mga_indices_t indices;
974 LOCK_TEST_WITH_RETURN( dev, filp );
976 DRM_COPY_FROM_USER_IOCTL( indices,
977 (drm_mga_indices_t __user *)data,
980 if(indices.idx < 0 || indices.idx > dma->buf_count) return DRM_ERR(EINVAL);
982 buf = dma->buflist[indices.idx];
983 buf_priv = buf->dev_private;
985 buf_priv->discard = indices.discard;
987 if ( !mga_verify_state( dev_priv ) ) {
988 if ( indices.discard ) {
989 if ( buf_priv->dispatched == 1 )
990 AGE_BUFFER( buf_priv );
991 buf_priv->dispatched = 0;
992 mga_freelist_put( dev, buf );
994 return DRM_ERR(EINVAL);
997 WRAP_TEST_WITH_RETURN( dev_priv );
999 mga_dma_dispatch_indices( dev, buf, indices.start, indices.end );
1004 static int mga_dma_iload( DRM_IOCTL_ARGS )
1007 drm_device_dma_t *dma = dev->dma;
1008 drm_mga_private_t *dev_priv = dev->dev_private;
1010 drm_mga_buf_priv_t *buf_priv;
1011 drm_mga_iload_t iload;
1014 LOCK_TEST_WITH_RETURN( dev, filp );
1016 DRM_COPY_FROM_USER_IOCTL( iload, (drm_mga_iload_t __user *)data, sizeof(iload) );
1019 if ( mga_do_wait_for_idle( dev_priv ) < 0 ) {
1020 if ( MGA_DMA_DEBUG )
1021 DRM_INFO( "%s: -EBUSY\n", __FUNCTION__ );
1022 return DRM_ERR(EBUSY);
1025 if(iload.idx < 0 || iload.idx > dma->buf_count) return DRM_ERR(EINVAL);
1027 buf = dma->buflist[iload.idx];
1028 buf_priv = buf->dev_private;
1030 if ( mga_verify_iload( dev_priv, iload.dstorg, iload.length ) ) {
1031 mga_freelist_put( dev, buf );
1032 return DRM_ERR(EINVAL);
1035 WRAP_TEST_WITH_RETURN( dev_priv );
1037 mga_dma_dispatch_iload( dev, buf, iload.dstorg, iload.length );
1039 /* Make sure we restore the 3D state next time.
1041 dev_priv->sarea_priv->dirty |= MGA_UPLOAD_CONTEXT;
1046 static int mga_dma_blit( DRM_IOCTL_ARGS )
1049 drm_mga_private_t *dev_priv = dev->dev_private;
1050 drm_mga_sarea_t *sarea_priv = dev_priv->sarea_priv;
1051 drm_mga_blit_t blit;
1054 LOCK_TEST_WITH_RETURN( dev, filp );
1056 DRM_COPY_FROM_USER_IOCTL( blit, (drm_mga_blit_t __user *)data, sizeof(blit) );
1058 if ( sarea_priv->nbox > MGA_NR_SAREA_CLIPRECTS )
1059 sarea_priv->nbox = MGA_NR_SAREA_CLIPRECTS;
1061 if ( mga_verify_blit( dev_priv, blit.srcorg, blit.dstorg ) )
1062 return DRM_ERR(EINVAL);
1064 WRAP_TEST_WITH_RETURN( dev_priv );
1066 mga_dma_dispatch_blit( dev, &blit );
1068 /* Make sure we restore the 3D state next time.
1070 dev_priv->sarea_priv->dirty |= MGA_UPLOAD_CONTEXT;
1075 static int mga_getparam( DRM_IOCTL_ARGS )
1078 drm_mga_private_t *dev_priv = dev->dev_private;
1079 drm_mga_getparam_t param;
1083 DRM_ERROR( "%s called with no initialization\n", __FUNCTION__ );
1084 return DRM_ERR(EINVAL);
1087 DRM_COPY_FROM_USER_IOCTL( param, (drm_mga_getparam_t __user *)data,
1090 DRM_DEBUG( "pid=%d\n", DRM_CURRENTPID );
1092 switch( param.param ) {
1093 case MGA_PARAM_IRQ_NR:
1096 case MGA_PARAM_CARD_TYPE:
1097 value = dev_priv->chipset;
1100 return DRM_ERR(EINVAL);
1103 if ( DRM_COPY_TO_USER( param.value, &value, sizeof(int) ) ) {
1104 DRM_ERROR( "copy_to_user\n" );
1105 return DRM_ERR(EFAULT);
1111 static int mga_set_fence(DRM_IOCTL_ARGS)
1114 drm_mga_private_t *dev_priv = dev->dev_private;
1119 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
1120 return DRM_ERR(EINVAL);
1123 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
1125 /* I would normal do this assignment in the declaration of temp,
1126 * but dev_priv may be NULL.
1129 temp = dev_priv->next_fence_to_post;
1130 dev_priv->next_fence_to_post++;
1133 DMA_BLOCK(MGA_DMAPAD, 0x00000000,
1134 MGA_DMAPAD, 0x00000000,
1135 MGA_DMAPAD, 0x00000000,
1136 MGA_SOFTRAP, 0x00000000);
1139 if (DRM_COPY_TO_USER( (u32 __user *) data, & temp, sizeof(u32))) {
1140 DRM_ERROR("copy_to_user\n");
1141 return DRM_ERR(EFAULT);
1147 static int mga_wait_fence(DRM_IOCTL_ARGS)
1150 drm_mga_private_t *dev_priv = dev->dev_private;
1154 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
1155 return DRM_ERR(EINVAL);
1158 DRM_COPY_FROM_USER_IOCTL(fence, (u32 __user *) data, sizeof(u32));
1160 DRM_DEBUG("pid=%d\n", DRM_CURRENTPID);
1162 mga_driver_fence_wait(dev, & fence);
1164 if (DRM_COPY_TO_USER( (u32 __user *) data, & fence, sizeof(u32))) {
1165 DRM_ERROR("copy_to_user\n");
1166 return DRM_ERR(EFAULT);
1172 drm_ioctl_desc_t mga_ioctls[] = {
1173 [DRM_IOCTL_NR(DRM_MGA_INIT)] = {mga_dma_init, 1, 1},
1174 [DRM_IOCTL_NR(DRM_MGA_FLUSH)] = {mga_dma_flush, 1, 0},
1175 [DRM_IOCTL_NR(DRM_MGA_RESET)] = {mga_dma_reset, 1, 0},
1176 [DRM_IOCTL_NR(DRM_MGA_SWAP)] = {mga_dma_swap, 1, 0},
1177 [DRM_IOCTL_NR(DRM_MGA_CLEAR)] = {mga_dma_clear, 1, 0},
1178 [DRM_IOCTL_NR(DRM_MGA_VERTEX)] = {mga_dma_vertex, 1, 0},
1179 [DRM_IOCTL_NR(DRM_MGA_INDICES)] = {mga_dma_indices, 1, 0},
1180 [DRM_IOCTL_NR(DRM_MGA_ILOAD)] = {mga_dma_iload, 1, 0},
1181 [DRM_IOCTL_NR(DRM_MGA_BLIT)] = {mga_dma_blit, 1, 0},
1182 [DRM_IOCTL_NR(DRM_MGA_GETPARAM)] = {mga_getparam, 1, 0},
1183 [DRM_IOCTL_NR(DRM_MGA_SET_FENCE)] = {mga_set_fence, 1, 0},
1184 [DRM_IOCTL_NR(DRM_MGA_WAIT_FENCE)] = {mga_wait_fence, 1, 0},
1185 [DRM_IOCTL_NR(DRM_MGA_DMA_BOOTSTRAP)] = {mga_dma_bootstrap, 1, 1},
1189 int mga_max_ioctl = DRM_ARRAY_SIZE(mga_ioctls);