Hide the allocator's use of physical addresses
[nouveau] / src / nv_xaa.c
1  /***************************************************************************\
2 |*                                                                           *|
3 |*       Copyright 2003 NVIDIA, Corporation.  All rights reserved.           *|
4 |*                                                                           *|
5 |*     NOTICE TO USER:   The source code  is copyrighted under  U.S. and     *|
6 |*     international laws.  Users and possessors of this source code are     *|
7 |*     hereby granted a nonexclusive,  royalty-free copyright license to     *|
8 |*     use this code in individual and commercial software.                  *|
9 |*                                                                           *|
10 |*     Any use of this source code must include,  in the user documenta-     *|
11 |*     tion and  internal comments to the code,  notices to the end user     *|
12 |*     as follows:                                                           *|
13 |*                                                                           *|
14 |*       Copyright 2003 NVIDIA, Corporation.  All rights reserved.           *|
15 |*                                                                           *|
16 |*     NVIDIA, CORPORATION MAKES NO REPRESENTATION ABOUT THE SUITABILITY     *|
17 |*     OF  THIS SOURCE  CODE  FOR ANY PURPOSE.  IT IS  PROVIDED  "AS IS"     *|
18 |*     WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  NVIDIA, CORPOR-     *|
19 |*     ATION DISCLAIMS ALL WARRANTIES  WITH REGARD  TO THIS SOURCE CODE,     *|
20 |*     INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGE-     *|
21 |*     MENT,  AND FITNESS  FOR A PARTICULAR PURPOSE.   IN NO EVENT SHALL     *|
22 |*     NVIDIA, CORPORATION  BE LIABLE FOR ANY SPECIAL,  INDIRECT,  INCI-     *|
23 |*     DENTAL, OR CONSEQUENTIAL DAMAGES,  OR ANY DAMAGES  WHATSOEVER RE-     *|
24 |*     SULTING FROM LOSS OF USE,  DATA OR PROFITS,  WHETHER IN AN ACTION     *|
25 |*     OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  ARISING OUT OF     *|
26 |*     OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOURCE CODE.     *|
27 |*                                                                           *|
28 |*     U.S. Government  End  Users.   This source code  is a "commercial     *|
29 |*     item,"  as that  term is  defined at  48 C.F.R. 2.101 (OCT 1995),     *|
30 |*     consisting  of "commercial  computer  software"  and  "commercial     *|
31 |*     computer  software  documentation,"  as such  terms  are  used in     *|
32 |*     48 C.F.R. 12.212 (SEPT 1995)  and is provided to the U.S. Govern-     *|
33 |*     ment only as  a commercial end item.   Consistent with  48 C.F.R.     *|
34 |*     12.212 and  48 C.F.R. 227.7202-1 through  227.7202-4 (JUNE 1995),     *|
35 |*     all U.S. Government End Users  acquire the source code  with only     *|
36 |*     those rights set forth herein.                                        *|
37 |*                                                                           *|
38  \***************************************************************************/
39
40 /* $XFree86: xc/programs/Xserver/hw/xfree86/drivers/nv/nv_xaa.c,v 1.35 2004/03/20 16:25:18 mvojkovi Exp $ */
41
42 #include "nv_include.h"
43 #include "xaalocal.h"
44 #include "miline.h"
45 #include "nv_dma.h"
46 #include "nvreg.h"
47
48 static const int NVCopyROP[16] =
49 {
50    0x00,            /* GXclear */
51    0x88,            /* GXand */
52    0x44,            /* GXandReverse */
53    0xCC,            /* GXcopy */
54    0x22,            /* GXandInverted */
55    0xAA,            /* GXnoop */
56    0x66,            /* GXxor */
57    0xEE,            /* GXor */
58    0x11,            /* GXnor */
59    0x99,            /* GXequiv */
60    0x55,            /* GXinvert*/
61    0xDD,            /* GXorReverse */
62    0x33,            /* GXcopyInverted */
63    0xBB,            /* GXorInverted */
64    0x77,            /* GXnand */
65    0xFF             /* GXset */
66 };
67
68 static const int NVCopyROP_PM[16] =
69 {
70    0x0A,            /* GXclear */
71    0x8A,            /* GXand */
72    0x4A,            /* GXandReverse */
73    0xCA,            /* GXcopy */
74    0x2A,            /* GXandInverted */
75    0xAA,            /* GXnoop */
76    0x6A,            /* GXxor */
77    0xEA,            /* GXor */
78    0x1A,            /* GXnor */
79    0x9A,            /* GXequiv */
80    0x5A,            /* GXinvert*/
81    0xDA,            /* GXorReverse */
82    0x3A,            /* GXcopyInverted */
83    0xBA,            /* GXorInverted */
84    0x7A,            /* GXnand */
85    0xFA             /* GXset */
86 };
87
88 static const int NVPatternROP[16] =
89 {
90    0x00,
91    0xA0,
92    0x50,
93    0xF0,
94    0x0A,
95    0xAA,
96    0x5A,
97    0xFA,
98    0x05,
99    0xA5,
100    0x55,
101    0xF5,
102    0x0F,
103    0xAF,
104    0x5F,
105    0xFF
106 };
107
108 void
109 NVWaitVSync(NVPtr pNv)
110 {
111     NVDmaStart(pNv, 5, 0x0000012C, 1);
112     NVDmaNext (pNv, 0);
113     NVDmaStart(pNv, 5, 0x00000134, 1);
114     NVDmaNext (pNv, pNv->CRTCnumber);
115     NVDmaStart(pNv, 5, 0x00000100, 1);
116     NVDmaNext (pNv, 0);
117     NVDmaStart(pNv, 5, 0x00000130, 1);
118     NVDmaNext (pNv, 0);
119 }
120
121 /* 
122   currentRop =  0-15  solid fill
123                16-31  8x8 pattern fill
124                32-47  solid fill with planemask 
125 */
126
127 static void 
128 NVSetPattern(
129    ScrnInfoPtr pScrn,
130    CARD32 clr0,
131    CARD32 clr1,
132    CARD32 pat0,
133    CARD32 pat1
134 )
135 {
136     NVPtr pNv = NVPTR(pScrn);
137
138     NVDmaStart(pNv, NvSubImagePattern, PATTERN_COLOR_0, 4);
139     NVDmaNext (pNv, clr0);
140     NVDmaNext (pNv, clr1);
141     NVDmaNext (pNv, pat0);
142     NVDmaNext (pNv, pat1);
143 }
144
145 void 
146 NVSetRopSolid(ScrnInfoPtr pScrn, CARD32 rop, CARD32 planemask)
147 {
148     NVPtr pNv = NVPTR(pScrn);
149
150     if(planemask != ~0) {
151         NVSetPattern(pScrn, 0, planemask, ~0, ~0);
152         if(pNv->currentRop != (rop + 32)) {
153            NVDmaStart(pNv, NvSubRop, ROP_SET, 1);
154            NVDmaNext (pNv, NVCopyROP_PM[rop]);
155            pNv->currentRop = rop + 32;
156         }
157     } else 
158     if (pNv->currentRop != rop) {
159         if(pNv->currentRop >= 16)
160              NVSetPattern(pScrn, ~0, ~0, ~0, ~0);
161         NVDmaStart(pNv, NvSubRop, ROP_SET, 1);
162         NVDmaNext (pNv, NVCopyROP[rop]);
163         pNv->currentRop = rop;
164     }
165 }
166
167 static void
168 NVSetupForScreenToScreenCopy(
169    ScrnInfoPtr pScrn, 
170    int xdir, int ydir, 
171    int rop,
172    unsigned planemask, 
173    int transparency_color
174 )
175 {
176     NVPtr pNv = NVPTR(pScrn);
177
178     planemask |= ~0 << pNv->CurrentLayout.depth;
179
180     NVSetRopSolid(pScrn, rop, planemask);
181
182     pNv->DMAKickoffCallback = NVDmaKickoffCallback;
183 }
184
185 static void
186 NVSubsequentScreenToScreenCopy(
187    ScrnInfoPtr pScrn, 
188    int x1, int y1,
189    int x2, int y2, 
190    int w, int h
191 )
192 {
193     NVPtr pNv = NVPTR(pScrn);
194
195     NVDmaStart(pNv, NvSubImageBlit, BLIT_POINT_SRC, 3);
196     NVDmaNext (pNv, (y1 << 16) | x1);
197     NVDmaNext (pNv, (y2 << 16) | x2);
198     NVDmaNext (pNv, (h  << 16) | w);
199
200     if((w * h) >= 512)
201        NVDmaKickoff(pNv); 
202 }
203
204 static void
205 NVSetupForSolidFill(
206    ScrnInfoPtr pScrn, 
207    int color, 
208    int rop,
209    unsigned planemask
210 )
211 {
212    NVPtr pNv = NVPTR(pScrn);
213
214    planemask |= ~0 << pNv->CurrentLayout.depth;
215
216    NVSetRopSolid(pScrn, rop, planemask);
217    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_COLOR, 1);
218    NVDmaNext (pNv, color);
219
220    pNv->DMAKickoffCallback = NVDmaKickoffCallback;
221 }
222
223 static void
224 NVSubsequentSolidFillRect(ScrnInfoPtr pScrn, int x, int y, int w, int h)
225 {
226    NVPtr pNv = NVPTR(pScrn);
227
228    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_RECTS(0), 2);
229    NVDmaNext (pNv, (x << 16) | y);
230    NVDmaNext (pNv, (w << 16) | h);
231
232    if((w * h) >= 512)
233       NVDmaKickoff(pNv);
234 }
235
236 static void
237 NVSetupForMono8x8PatternFill (
238    ScrnInfoPtr pScrn, 
239    int patternx, int patterny,
240    int fg, int bg, 
241    int rop, 
242    unsigned planemask
243 )
244 {
245    NVPtr pNv = NVPTR(pScrn);
246
247    planemask = ~0 << pNv->CurrentLayout.depth;
248
249    fg |= planemask;
250    if(bg == -1) bg = 0;
251    else bg |= planemask;
252
253    if (pNv->currentRop != (rop + 16)) {
254        NVDmaStart(pNv, NvSubRop, ROP_SET, 1);
255        NVDmaNext (pNv, NVPatternROP[rop]);
256        pNv->currentRop = rop + 16;
257    }
258
259    NVSetPattern(pScrn, bg, fg, patternx, patterny);
260    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_COLOR, 1);
261    NVDmaNext (pNv, fg);
262
263    pNv->DMAKickoffCallback = NVDmaKickoffCallback;
264 }
265
266 static void
267 NVSubsequentMono8x8PatternFillRect(
268    ScrnInfoPtr pScrn,
269    int patternx, int patterny,
270    int x, int y, 
271    int w, int h
272 )
273 {
274    NVPtr pNv = NVPTR(pScrn);
275
276    NVDmaStart(pNv, NvSubRectangle, RECT_SOLID_RECTS(0), 2);
277    NVDmaNext (pNv, (x << 16) | y);
278    NVDmaNext (pNv, (w << 16) | h);
279
280    if((w * h) >= 512)
281       NVDmaKickoff(pNv);
282 }
283
284 static CARD32 _bg_pixel;
285 static CARD32 _fg_pixel;
286 static Bool _transparent;
287 static CARD32 _color_expand_dwords;
288 static CARD32 _color_expand_offset;
289 static int _remaining;
290 static unsigned char *_storage_buffer[1];
291
292 static void
293 NVSetupForScanlineCPUToScreenColorExpandFill (
294    ScrnInfoPtr pScrn,
295    int fg, int bg,
296    int rop,
297    unsigned int planemask
298 )
299 {
300    NVPtr pNv = NVPTR(pScrn);
301
302    CARD32 mask = ~0 << pNv->CurrentLayout.depth;
303
304    planemask |= mask;
305    _fg_pixel = fg | mask;
306
307    if(bg == -1) {
308       _transparent = TRUE;
309    } else {
310       _transparent = FALSE;
311       _bg_pixel = bg | mask;
312    }
313
314    NVSetRopSolid (pScrn, rop, planemask);
315 }
316
317 static void
318 NVSubsequentScanlineCPUToScreenColorExpandFill (
319     ScrnInfoPtr pScrn, 
320     int x, int y,
321     int w, int h,
322     int skipleft
323 )
324 {
325    NVPtr pNv = NVPTR(pScrn);
326    int bw = (w + 31) & ~31;
327
328    _color_expand_dwords = bw >> 5;
329    _remaining = h;
330
331    if(_transparent) {
332       NVDmaStart(pNv, NvSubRectangle, RECT_EXPAND_ONE_COLOR_CLIP, 5);
333       NVDmaNext (pNv, (y << 16) | ((x + skipleft) & 0xFFFF));
334       NVDmaNext (pNv, ((y + h) << 16) | ((x + w) & 0xFFFF));
335       NVDmaNext (pNv, _fg_pixel);
336       NVDmaNext (pNv, (h << 16) | bw);
337       NVDmaNext (pNv, (y << 16) | (x & 0xFFFF));
338       _color_expand_offset = RECT_EXPAND_ONE_COLOR_DATA(0);
339    } else {
340       NVDmaStart(pNv, NvSubRectangle, RECT_EXPAND_TWO_COLOR_CLIP, 7);
341       NVDmaNext (pNv, (y << 16) | ((x + skipleft) & 0xFFFF));
342       NVDmaNext (pNv, ((y + h) << 16) | ((x + w) & 0xFFFF));
343       NVDmaNext (pNv, _bg_pixel);
344       NVDmaNext (pNv, _fg_pixel);
345       NVDmaNext (pNv, (h << 16) | bw);
346       NVDmaNext (pNv, (h << 16) | bw);
347       NVDmaNext (pNv, (y << 16) | (x & 0xFFFF));
348       _color_expand_offset = RECT_EXPAND_TWO_COLOR_DATA(0); 
349    }
350
351    NVDmaStart(pNv, NvSubRectangle, _color_expand_offset, _color_expand_dwords);
352    _storage_buffer[0] = (unsigned char*)&pNv->dmaBase[pNv->dmaCurrent];
353 }
354
355 static void
356 NVSubsequentColorExpandScanline(ScrnInfoPtr pScrn, int bufno)
357 {
358    NVPtr pNv = NVPTR(pScrn);
359
360    pNv->dmaCurrent += _color_expand_dwords;
361
362    if(--_remaining) {
363        NVDmaStart(pNv, NvSubRectangle, _color_expand_offset, _color_expand_dwords);
364        _storage_buffer[0] = (unsigned char*)&pNv->dmaBase[pNv->dmaCurrent];
365    } else {
366        /* hardware bug workaround */
367        NVDmaStart(pNv, NvSubImageBlit, BLIT_POINT_SRC, 1);
368        NVDmaNext (pNv, 0);
369        NVDmaKickoff(pNv);
370    }
371 }
372
373 static void 
374 NVSetupForScanlineImageWrite(
375    ScrnInfoPtr pScrn, int rop, 
376    unsigned int planemask, 
377    int trans_color, 
378    int bpp, int depth
379 )
380 {
381    NVPtr pNv = NVPTR(pScrn);
382
383    planemask |= ~0 << pNv->CurrentLayout.depth;
384
385    NVSetRopSolid (pScrn, rop, planemask);
386 }
387
388 static CARD32 _image_size;
389 static CARD32 _image_srcpoint;
390 static CARD32 _image_dstpoint;
391 static CARD32 _image_dstpitch;
392
393 static void 
394 NVSubsequentScanlineImageWriteRect(
395    ScrnInfoPtr pScrn, 
396    int x, int y, 
397    int w, int h, 
398    int skipleft
399 )
400 {
401    NVPtr pNv = NVPTR(pScrn);
402    int Bpp = pNv->CurrentLayout.bitsPerPixel >> 3;
403    int image_srcpitch;
404
405    _image_size = (1 << 16) | (w - skipleft);
406    _image_srcpoint = skipleft;
407    _image_dstpoint = (y << 16) | (x + skipleft);
408    _remaining = h;
409    _image_dstpitch = pNv->CurrentLayout.displayWidth * Bpp;
410    image_srcpitch =  ((w * Bpp) + 63) & ~63;
411    _storage_buffer[0] = pNv->ScratchBuffer->map;
412
413    NVSync(pScrn);
414
415    NVDmaStart(pNv, NvSubContextSurfaces, SURFACE_PITCH, 2);
416    NVDmaNext (pNv, (_image_dstpitch << 16) | image_srcpitch);
417    NVDmaNext (pNv, pNv->ScratchBuffer->offset);
418 }
419
420 static void NVSubsequentImageWriteScanline(ScrnInfoPtr pScrn, int bufno)
421 {
422    NVPtr pNv = NVPTR(pScrn);
423
424    NVDmaStart(pNv, NvSubImageBlit, BLIT_POINT_SRC, 3);
425    NVDmaNext (pNv, _image_srcpoint);
426    NVDmaNext (pNv, _image_dstpoint);
427    NVDmaNext (pNv, _image_size);
428    NVDmaKickoff(pNv);
429
430    if(--_remaining) {
431       _image_dstpoint += (1 << 16);
432       NVSync(pScrn);
433    } else {
434       NVDmaStart(pNv, NvSubContextSurfaces, SURFACE_PITCH, 2);
435       NVDmaNext (pNv, _image_dstpitch | (_image_dstpitch << 16));
436       NVDmaNext (pNv, pNv->FB->offset);
437    }
438 }
439
440 static void
441 NVSetupForSolidLine(ScrnInfoPtr pScrn, int color, int rop, unsigned planemask)
442 {
443     NVPtr pNv = NVPTR(pScrn);
444
445     planemask |= ~0 << pNv->CurrentLayout.depth;
446
447     NVSetRopSolid(pScrn, rop, planemask);
448
449     _fg_pixel = color;
450
451     pNv->DMAKickoffCallback = NVDmaKickoffCallback;
452 }
453
454 static void 
455 NVSubsequentSolidHorVertLine(ScrnInfoPtr pScrn, int x, int y, int len, int dir)
456 {
457     NVPtr pNv = NVPTR(pScrn);
458
459     NVDmaStart(pNv, NvSubSolidLine, LINE_COLOR, 1);
460     NVDmaNext (pNv, _fg_pixel);
461     NVDmaStart(pNv, NvSubSolidLine, LINE_LINES(0), 2);
462     NVDmaNext (pNv, (y << 16) | ( x & 0xffff));
463     if(dir == DEGREES_0) {
464        NVDmaNext (pNv, (y << 16) | ((x + len) & 0xffff));
465     } else {
466        NVDmaNext (pNv, ((y + len) << 16) | (x & 0xffff));
467     }
468 }
469
470 static void 
471 NVSubsequentSolidTwoPointLine(
472    ScrnInfoPtr pScrn, 
473    int x1, int y1,
474    int x2, int y2, 
475    int flags
476 )
477 {
478     NVPtr pNv = NVPTR(pScrn);
479     Bool drawLast = !(flags & OMIT_LAST);
480
481     NVDmaStart(pNv, NvSubSolidLine, LINE_COLOR, 1);
482     NVDmaNext (pNv, _fg_pixel);
483     NVDmaStart(pNv, NvSubSolidLine, LINE_LINES(0), drawLast ? 4 : 2);
484     NVDmaNext (pNv, (y1 << 16) | (x1 & 0xffff));
485     NVDmaNext (pNv, (y2 << 16) | (x2 & 0xffff));
486     if(drawLast) {
487         NVDmaNext (pNv, (y2 << 16) | (x2 & 0xffff));
488         NVDmaNext (pNv, ((y2 + 1) << 16) | (x2 & 0xffff));
489     }
490 }
491
492 static void
493 NVSetClippingRectangle(ScrnInfoPtr pScrn, int x1, int y1, int x2, int y2)
494 {
495     NVPtr pNv = NVPTR(pScrn);
496     int h = y2 - y1 + 1;
497     int w = x2 - x1 + 1;
498
499     NVDmaStart(pNv, NvSubClipRectangle, CLIP_POINT, 2);
500     NVDmaNext (pNv, (y1 << 16) | x1); 
501     NVDmaNext (pNv, (h << 16) | w);
502 }
503
504 static void
505 NVDisableClipping(ScrnInfoPtr pScrn)
506 {
507     NVPtr pNv = NVPTR(pScrn);
508
509     NVDmaStart(pNv, NvSubClipRectangle, CLIP_POINT, 2);
510     NVDmaNext (pNv, 0);              
511     NVDmaNext (pNv, 0x7FFF7FFF);
512 }
513
514
515 /* Initialize XAA acceleration info */
516 Bool
517 NVXaaInit(ScreenPtr pScreen) 
518 {
519    ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum];
520    NVPtr pNv = NVPTR(pScrn);
521    XAAInfoRecPtr accel;
522
523    accel = pNv->AccelInfoRec = XAACreateInfoRec();
524    if(!accel) return FALSE;
525
526    accel->Flags = LINEAR_FRAMEBUFFER | PIXMAP_CACHE | OFFSCREEN_PIXMAPS;
527    accel->Sync = NVSync;
528
529    accel->ScreenToScreenCopyFlags = NO_TRANSPARENCY;
530    accel->SetupForScreenToScreenCopy = NVSetupForScreenToScreenCopy;
531    accel->SubsequentScreenToScreenCopy = NVSubsequentScreenToScreenCopy;
532
533    accel->SolidFillFlags = 0;
534    accel->SetupForSolidFill = NVSetupForSolidFill;
535    accel->SubsequentSolidFillRect = NVSubsequentSolidFillRect;
536
537    accel->Mono8x8PatternFillFlags = HARDWARE_PATTERN_SCREEN_ORIGIN |
538                                     HARDWARE_PATTERN_PROGRAMMED_BITS |
539                                     NO_PLANEMASK;
540    accel->SetupForMono8x8PatternFill = NVSetupForMono8x8PatternFill;
541    accel->SubsequentMono8x8PatternFillRect = NVSubsequentMono8x8PatternFillRect;
542
543    accel->ScanlineCPUToScreenColorExpandFillFlags = 
544                                     BIT_ORDER_IN_BYTE_LSBFIRST |
545                                     CPU_TRANSFER_PAD_DWORD |
546                                     LEFT_EDGE_CLIPPING |
547                                     LEFT_EDGE_CLIPPING_NEGATIVE_X;
548    accel->NumScanlineColorExpandBuffers = 1;
549    accel->SetupForScanlineCPUToScreenColorExpandFill = 
550             NVSetupForScanlineCPUToScreenColorExpandFill;
551    accel->SubsequentScanlineCPUToScreenColorExpandFill = 
552             NVSubsequentScanlineCPUToScreenColorExpandFill;
553    accel->SubsequentColorExpandScanline = 
554             NVSubsequentColorExpandScanline;
555    accel->ScanlineColorExpandBuffers = _storage_buffer;
556
557    accel->ScanlineImageWriteFlags = NO_GXCOPY |
558                                     NO_TRANSPARENCY |
559                                     LEFT_EDGE_CLIPPING |
560                                     LEFT_EDGE_CLIPPING_NEGATIVE_X;
561    accel->NumScanlineImageWriteBuffers = 1;
562    accel->SetupForScanlineImageWrite = NVSetupForScanlineImageWrite;
563    accel->SubsequentScanlineImageWriteRect = NVSubsequentScanlineImageWriteRect;
564    accel->SubsequentImageWriteScanline = NVSubsequentImageWriteScanline;
565    accel->ScanlineImageWriteBuffers = _storage_buffer;
566
567    accel->SolidLineFlags = 0;
568    accel->SetupForSolidLine = NVSetupForSolidLine;
569    accel->SubsequentSolidHorVertLine = NVSubsequentSolidHorVertLine;
570    accel->SubsequentSolidTwoPointLine = NVSubsequentSolidTwoPointLine;
571    accel->SetClippingRectangle = NVSetClippingRectangle;
572    accel->DisableClipping = NVDisableClipping;
573    accel->ClippingFlags = HARDWARE_CLIP_SOLID_LINE;
574    
575    miSetZeroLineBias(pScreen, OCTANT1 | OCTANT3 | OCTANT4 | OCTANT6);
576
577    return (XAAInit(pScreen, accel));
578 }
579