First import
[xorg_rtime.git] / xorg-server-1.4 / hw / kdrive / nvidia / nvidia.c
blobbe42e561a6f020a45aed969aabec56d08d601e34
1 /*
2 * Copyright © 2003 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Keith Packard makes no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
23 #ifdef HAVE_CONFIG_H
24 #include <kdrive-config.h>
25 #endif
26 #include "nvidia.h"
27 #include "kaa.h"
28 #include <sys/io.h>
30 static Bool
31 nvidiaCardInit (KdCardInfo *card)
33 NvidiaCardInfo *nvidiac;
35 nvidiac = (NvidiaCardInfo *) xalloc (sizeof (NvidiaCardInfo));
36 if (!nvidiac)
37 return FALSE;
39 (void) nvidiaMapReg (card, nvidiac);
41 if (!vesaInitialize (card, &nvidiac->vesa))
43 xfree (nvidiac);
44 return FALSE;
47 card->driver = nvidiac;
49 return TRUE;
52 static Bool
53 nvidiaScreenInit (KdScreenInfo *screen)
55 NvidiaCardInfo *nvidiac = screen->card->driver;
56 NvidiaScreenInfo *nvidias;
57 int screen_size, memory;
59 nvidias = (NvidiaScreenInfo *) xalloc (sizeof (NvidiaScreenInfo));
60 if (!nvidias)
61 return FALSE;
62 memset (nvidias, '\0', sizeof (NvidiaScreenInfo));
63 if (!vesaScreenInitialize (screen, &nvidias->vesa))
65 xfree (nvidias);
66 return FALSE;
68 if (!nvidiac->reg_base)
69 screen->dumb = TRUE;
70 if (nvidias->vesa.mapping != VESA_LINEAR)
71 screen->dumb = TRUE;
72 nvidias->screen = nvidias->vesa.fb;
73 memory = nvidias->vesa.fb_size;
74 screen_size = screen->fb[0].byteStride * screen->height;
75 if (nvidias->screen && memory >= screen_size + 2048)
77 memory -= 2048;
78 nvidias->cursor_base = nvidias->screen + memory - 2048;
80 else
81 nvidias->cursor_base = 0;
82 screen->softCursor = TRUE; /* XXX for now */
83 memory -= screen_size;
84 if (memory > screen->fb[0].byteStride)
86 nvidias->off_screen = nvidias->screen + screen_size;
87 nvidias->off_screen_size = memory;
89 else
91 nvidias->off_screen = 0;
92 nvidias->off_screen_size = 0;
94 screen->driver = nvidias;
95 return TRUE;
98 static Bool
99 nvidiaInitScreen (ScreenPtr pScreen)
101 #if 0
102 #ifdef XV
103 KdScreenPriv(pScreen);
104 NvidiaCardInfo *nvidiac = pScreenPriv->screen->card->driver;
105 if (nvidiac->media_reg && nvidiac->reg)
106 nvidiaInitVideo(pScreen);
107 #endif
108 #endif
109 return vesaInitScreen (pScreen);
112 #ifdef RANDR
113 static Bool
114 nvidiaRandRSetConfig (ScreenPtr pScreen,
115 Rotation rotation,
116 int rate,
117 RRScreenSizePtr pSize)
119 kaaWaitSync (pScreen);
121 if (!vesaRandRSetConfig (pScreen, rotation, rate, pSize))
122 return FALSE;
124 return TRUE;
127 static void
128 nvidiaRandRInit (ScreenPtr pScreen)
130 rrScrPriv(pScreen);
132 pScrPriv->rrSetConfig = nvidiaRandRSetConfig;
134 #endif
136 static Bool
137 nvidiaFinishInitScreen (ScreenPtr pScreen)
139 Bool ret;
140 ret = vesaFinishInitScreen (pScreen);
141 #ifdef RANDR
142 nvidiaRandRInit (pScreen);
143 #endif
144 return ret;
147 void
148 nvidiaPreserve (KdCardInfo *card)
150 vesaPreserve(card);
153 void
154 nvidiaOutb (NvidiaCardInfo *nvidiac, CARD16 port, CARD8 val)
156 asm volatile ("outb %b0,%w1" : : "a" (val), "d" (port));
159 CARD8
160 nvidiaInb (NvidiaCardInfo *nvidiac, CARD16 port)
162 CARD8 v;
163 asm volatile ("inb %w1,%b0" : "=a" (v) : "d" (port));
164 return v;
167 CARD8
168 nvidiaGetIndex (NvidiaCardInfo *nvidiac, CARD16 addr, CARD16 data, CARD8 id)
170 CARD8 ret;
171 DBGOUT ("nvidiaGetIndex(0x%x,0x%x)\n", addr, id);
172 nvidiaOutb (nvidiac, addr, id);
173 ret = nvidiaInb (nvidiac, data);
174 DBGOUT (" -> 0x%x\n", ret);
175 return ret;
178 void
179 nvidiaSetIndex (NvidiaCardInfo *nvidiac, CARD16 addr, CARD16 data, CARD8 id, CARD8 val)
181 DBGOUT ("nvidiaSetIndex(0x%x,0x%x) = 0x%x\n", addr, id, val);
182 nvidiaOutb (nvidiac, addr, id);
183 nvidiaOutb (nvidiac, data, val);
186 static void vgaLockUnlock (NvidiaCardInfo *nvidiac, Bool lock)
188 CARD8 cr11;
189 ENTER ();
190 cr11 = nvidiaGetIndex (nvidiac, 0x3d4, 0x3d5, 0x11);
191 if (lock) cr11 |= 0x80;
192 else cr11 &= ~0x80;
193 nvidiaSetIndex (nvidiac, 0x3d4, 0x3d5, 0x11, cr11);
194 LEAVE ();
197 static void nvidiaLockUnlock (NvidiaCardInfo *nvidiac, Bool lock)
199 if (NVIDIA_IS_3(nvidiac))
200 nvidiaSetIndex (nvidiac, 0x3c4, 0x3c5, 0x06, lock ? 0x99 : 0x57);
201 else
202 nvidiaSetIndex (nvidiac, 0x3c4, 0x3c5, 0x1f, lock ? 0x99 : 0x57);
203 vgaLockUnlock(nvidiac, lock);
206 Bool
207 nvidiaMapReg (KdCardInfo *card, NvidiaCardInfo *nvidiac)
209 nvidiac->reg_base = (CARD8 *) KdMapDevice (NVIDIA_REG_BASE(card),
210 NVIDIA_REG_SIZE(card));
212 if (!nvidiac->reg_base)
214 nvidiac->mmio = 0;
215 nvidiac->rop = 0;
216 nvidiac->blt = 0;
217 nvidiac->rect = 0;
218 return FALSE;
221 nvidiac->mmio = (CARD8 *) (nvidiac->reg_base + NVIDIA_MMIO_OFF(card));
222 nvidiac->rop = (NvidiaRop *) (nvidiac->reg_base + NVIDIA_ROP_OFF(card));
223 nvidiac->rect = (NvidiaRectangle *) (nvidiac->reg_base + NVIDIA_RECTANGLE_OFF(card));
224 nvidiac->blt = (NvidiaScreenBlt *) (nvidiac->reg_base + NVIDIA_BLT_OFF(card));
225 nvidiac->busy = (NvidiaBusy *) (nvidiac->reg_base + NVIDIA_BUSY_OFF(card));
226 KdSetMappedMode (NVIDIA_REG_BASE(card),
227 NVIDIA_REG_SIZE(card),
228 KD_MAPPED_MODE_REGISTERS);
229 return TRUE;
232 void
233 nvidiaUnmapReg (KdCardInfo *card, NvidiaCardInfo *nvidiac)
235 if (nvidiac->reg_base)
237 KdResetMappedMode (NVIDIA_REG_BASE(card),
238 NVIDIA_REG_SIZE(card),
239 KD_MAPPED_MODE_REGISTERS);
240 KdUnmapDevice ((void *) nvidiac->reg_base, NVIDIA_REG_SIZE(card));
241 nvidiac->reg_base = 0;
242 nvidiac->rop = 0;
243 nvidiac->blt = 0;
244 nvidiac->rect = 0;
248 void
249 nvidiaSetMMIO (KdCardInfo *card, NvidiaCardInfo *nvidiac)
251 if (!nvidiac->reg_base)
252 nvidiaMapReg (card, nvidiac);
253 nvidiaLockUnlock (nvidiac, FALSE);
254 nvidiac->fifo_free = 0;
255 nvidiac->fifo_size = nvidiac->rop->FifoFree.FifoFree;
258 void
259 nvidiaResetMMIO (KdCardInfo *card, NvidiaCardInfo *nvidiac)
261 nvidiaUnmapReg (card, nvidiac);
262 nvidiaLockUnlock (nvidiac, TRUE);
265 Bool
266 nvidiaEnable (ScreenPtr pScreen)
268 KdScreenPriv(pScreen);
269 NvidiaCardInfo *nvidiac = pScreenPriv->card->driver;
271 if (!vesaEnable (pScreen))
272 return FALSE;
274 nvidiaSetMMIO (pScreenPriv->card, nvidiac);
275 #ifdef XV
276 KdXVEnable (pScreen);
277 #endif
278 return TRUE;
281 void
282 nvidiaDisable (ScreenPtr pScreen)
284 KdScreenPriv(pScreen);
285 NvidiaCardInfo *nvidiac = pScreenPriv->card->driver;
287 #ifdef XV
288 KdXVDisable (pScreen);
289 #endif
290 nvidiaResetMMIO (pScreenPriv->card, nvidiac);
291 vesaDisable (pScreen);
294 static Bool
295 nvidiaDPMS (ScreenPtr pScreen, int mode)
297 return vesaDPMS (pScreen, mode);
300 static void
301 nvidiaRestore (KdCardInfo *card)
303 NvidiaCardInfo *nvidiac = card->driver;
305 nvidiaResetMMIO (card, nvidiac);
306 vesaRestore (card);
309 static void
310 nvidiaScreenFini (KdScreenInfo *screen)
312 NvidiaScreenInfo *nvidias = (NvidiaScreenInfo *) screen->driver;
314 vesaScreenFini (screen);
315 xfree (nvidias);
316 screen->driver = 0;
319 static void
320 nvidiaCardFini (KdCardInfo *card)
322 NvidiaCardInfo *nvidiac = card->driver;
324 nvidiaUnmapReg (card, nvidiac);
325 vesaCardFini (card);
328 #define nvidiaCursorInit 0 /* initCursor */
329 #define nvidiaCursorEnable 0 /* enableCursor */
330 #define nvidiaCursorDisable 0 /* disableCursor */
331 #define nvidiaCursorFini 0 /* finiCursor */
332 #define nvidiaRecolorCursor 0 /* recolorCursor */
334 KdCardFuncs nvidiaFuncs = {
335 nvidiaCardInit, /* cardinit */
336 nvidiaScreenInit, /* scrinit */
337 nvidiaInitScreen, /* initScreen */
338 nvidiaFinishInitScreen, /* finishInitScreen */
339 vesaCreateResources, /* createRes */
340 nvidiaPreserve, /* preserve */
341 nvidiaEnable, /* enable */
342 nvidiaDPMS, /* dpms */
343 nvidiaDisable, /* disable */
344 nvidiaRestore, /* restore */
345 nvidiaScreenFini, /* scrfini */
346 nvidiaCardFini, /* cardfini */
348 nvidiaCursorInit, /* initCursor */
349 nvidiaCursorEnable, /* enableCursor */
350 nvidiaCursorDisable, /* disableCursor */
351 nvidiaCursorFini, /* finiCursor */
352 nvidiaRecolorCursor, /* recolorCursor */
354 nvidiaDrawInit, /* initAccel */
355 nvidiaDrawEnable, /* enableAccel */
356 nvidiaDrawDisable, /* disableAccel */
357 nvidiaDrawFini, /* finiAccel */
359 vesaGetColors, /* getColors */
360 vesaPutColors, /* putColors */