First import
[xorg_rtime.git] / xorg-server-1.4 / hw / xfree86 / os-support / sysv / sysv_video.c
blob5811947bdab7877958d0c2baaf10a42fc2f161d1
1 /*
2 * Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany
3 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the names of Thomas Roell and David Wexelblat
10 * not be used in advertising or publicity pertaining to distribution of
11 * the software without specific, written prior permission. Thomas Roell and
12 * David Wexelblat makes no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
14 * implied warranty.
16 * THOMAS ROELL AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD TO
17 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THOMAS ROELL OR DAVID WEXELBLAT BE LIABLE FOR
19 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #ifdef HAVE_XORG_CONFIG_H
27 #include <xorg-config.h>
28 #endif
30 #include <X11/X.h>
32 #define _NEED_SYSI86
33 #include "xf86.h"
34 #include "xf86Priv.h"
35 #include "xf86_OSlib.h"
36 #include "xf86OSpriv.h"
38 #ifndef MAP_FAILED
39 #define MAP_FAILED ((void *)-1)
40 #endif
42 #ifndef SI86IOPL
43 #define SET_IOPL() sysi86(SI86V86,V86SC_IOPL,PS_IOPL)
44 #define RESET_IOPL() sysi86(SI86V86,V86SC_IOPL,0)
45 #else
46 #define SET_IOPL() sysi86(SI86IOPL,3)
47 #define RESET_IOPL() sysi86(SI86IOPL,0)
48 #endif
50 /***************************************************************************/
51 /* Video Memory Mapping section */
52 /***************************************************************************/
55 * XXX Support for SVR3 will need to be reworked if needed. In particular
56 * the Region parameter is no longer passed, and will need to be dealt
57 * with internally if required.
58 * OK, i'll rework that thing ... (clean it up a lot)
59 * SVR3 Support only with SVR3_MMAPDRV (mr)
63 #ifdef HAS_SVR3_MMAPDRV
64 #ifndef MMAP_DEBUG
65 #define MMAP_DEBUG 3
66 #endif
68 struct kd_memloc MapDSC;
69 int mmapFd = -2;
71 static int
72 mmapStat(pointer Base, unsigned long Size) {
74 int nmmreg,i=0,region=-1;
75 mmapinfo_t *ibuf;
77 nmmreg = ioctl(mmapFd, GETNMMREG);
79 if(nmmreg <= 0)
80 xf86MsgVerb(X_INFO, MMAP_DEBUG,
81 "\nNo physical memory mapped currently.\n\n");
82 else {
83 if((ibuf = (mmapinfo_t *)malloc(nmmreg*sizeof(mmapinfo_t))) == NULL)
84 xf86Msg(X_WARNING,
85 "Couldn't allocate memory 4 mmapinfo_t\n");
86 else {
87 if(ioctl(mmapFd, GETMMREG, ibuf) != -1)
89 xf86MsgVerb(X_INFO, MMAP_DEBUG,
90 "# mmapStat: [Size=%x,Base=%x]\n", Size, Base);
91 xf86MsgVerb(X_INFO, MMAP_DEBUG,
92 "# Physical Address Size Reference Count\n");
93 for(i = 0; i < nmmreg; i++) {
94 xf86MsgVerb(X_INFO, MMAP_DEBUG,
95 "%-4d 0x%08X %5dk %5d ",
96 i, ibuf[i].physaddr, ibuf[i].length/1024, ibuf[i].refcnt);
97 if (ibuf[i].physaddr == Base || ibuf[i].length == Size ) {
98 xf86MsgVerb(X_INFO, MMAP_DEBUG,"MATCH !!!");
99 if (region==-1) region=i;
101 xf86ErrorFVerb(MMAP_DEBUG, "\n");
103 xf86ErrorFVerb(MMAP_DEBUG, "\n");
105 free(ibuf);
108 if (region == -1 && nmmreg > 0) region=region * i;
109 return(region);
111 #endif
114 static Bool
115 linearVidMem()
117 #ifdef SVR4
118 return TRUE;
119 #elif defined(HAS_SVR3_MMAPDRV)
120 xf86MsgVerb(X_INFO, MMAP_DEBUG,
121 "# xf86LinearVidMem: MMAP 2.2.2 called\n");
123 if(mmapFd >= 0) return TRUE;
125 if ((mmapFd = open("/dev/mmap", O_RDWR)) != -1)
127 if(ioctl(mmapFd, GETVERSION) < 0x0222) {
128 xf86Msg(X_WARNING,
129 "xf86LinearVidMem: MMAP 2.2.2 or above required\n");
130 xf86ErrorF("\tlinear memory access disabled\n");
131 return FALSE;
133 return TRUE;
135 xf86Msg(X_WARNING, "xf86LinearVidMem: failed to open /dev/mmap (%s)\n",
136 strerror(errno));
137 xf86ErrorF("\tlinear memory access disabled\n");
138 return FALSE;
139 #endif
142 static pointer
143 mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
145 pointer base;
146 int fd;
148 #if defined(SVR4)
149 fd = open(DEV_MEM, (flags & VIDMEM_READONLY) ? O_RDONLY : O_RDWR);
150 if (fd < 0)
152 FatalError("xf86MapVidMem: failed to open %s (%s)\n",
153 DEV_MEM, strerror(errno));
155 base = mmap((caddr_t)0, Size,
156 (flags & VIDMEM_READONLY) ?
157 PROT_READ : (PROT_READ | PROT_WRITE),
158 MAP_SHARED, fd, (off_t)Base);
159 close(fd);
160 if (base == MAP_FAILED)
162 FatalError("%s: Could not mmap framebuffer [s=%x,a=%x] (%s)\n",
163 "xf86MapVidMem", Size, Base, strerror(errno));
165 #else /* SVR4 */
166 #ifdef HAS_SVR3_MMAPDRV
168 xf86MsgVerb(X_INFO, MMAP_DEBUG, "# xf86MapVidMem: MMAP 2.2.2 called\n");
169 xf86MsgVerb(X_INFO, MMAP_DEBUG,
170 "MMAP_VERSION: 0x%x\n",ioctl(mmapFd, GETVERSION));
171 if (ioctl(mmapFd, GETVERSION) == -1)
173 xf86LinearVidMem();
175 xf86MsgVerb(X_INFO, MMAP_DEBUG,
176 "MMAP_VERSION: 0x%x\n",ioctl(mmapFd, GETVERSION));
177 xf86MsgVerb(X_INFO, MMAP_DEBUG,
178 "xf86MapVidMem: Screen: %d\n", ScreenNum);
179 mmapStat(Base,Size);
180 /* To force the MMAP driver to provide the address */
181 base = (pointer)0;
182 xf86MsgVerb(X_INFO, MMAP_DEBUG,
183 "xf86MapVidMem: [s=%x,a=%x]\n", Size, Base);
184 MapDSC.vaddr = (char *)base;
185 MapDSC.physaddr = (char *)Base;
186 MapDSC.length = Size;
187 MapDSC.ioflg = 1;
188 if(mmapFd >= 0)
190 if((base = (pointer)ioctl(mmapFd, MAP, &MapDSC)) == (pointer)-1)
192 FatalError("%s: Could not mmap framebuffer [s=%x,a=%x] (%s)\n",
193 "xf86MapVidMem", Size, Base, strerror(errno));
194 /* NOTREACHED */
197 /* Next time we want the same address! */
198 MapDSC.vaddr = (char *)base;
201 xf86MsgVerb(X_INFO, MMAP_DEBUG,
202 "MapDSC.vaddr : 0x%x\n", MapDSC.vaddr);
203 xf86MsgVerb(X_INFO, MMAP_DEBUG,
204 "MapDSC.physaddr: 0x%x\n", MapDSC.physaddr);
205 xf86MsgVerb(X_INFO, MMAP_DEBUG,
206 "MapDSC.length : %d\n", MapDSC.length);
207 mmapStat(Base,Size);
208 xf86MsgVerb(X_INFO, MMAP_DEBUG,
209 "xf86MapVidMem: [s=%x,a=%x,b=%x]\n", Size, Base, base);
210 xf86MsgVerb(X_INFO, MMAP_DEBUG,
211 "xf86MapVidMem: SUCCEED Mapping FrameBuffer \n");
212 #endif /* HAS_SVR3_MMAPDRV */
213 #endif /* SVR4 */
214 return(base);
217 /* ARGSUSED */
218 static void
219 unmapVidMem(int ScreenNum, pointer Base, unsigned long Size)
221 #if defined (SVR4)
222 munmap(Base, Size);
223 #else /* SVR4 */
224 #ifdef HAS_SVR3_MMAPDRV
225 xf86MsgVerb(X_INFO, MMAP_DEBUG,
226 "# xf86UnMapVidMem: UNMapping FrameBuffer\n");
227 mmapStat(Base,Size);
228 ioctl(mmapFd, UNMAPRM , Base);
229 mmapStat(Base,Size);
230 xf86MsgVerb(X_INFO, MMAP_DEBUG,
231 "# xf86UnMapVidMem: Screen: %d [v=%x]\n", ScreenNum, Base);
232 #endif /* HAS_SVR3_MMAPDRV */
233 #endif /* SVR4 */
234 return;
237 #if defined(SVR4) && defined(i386) && !defined(sun)
239 * For some SVR4 versions, a 32-bit read is done for the first location
240 * in each page when the page is first mapped. If this is done while
241 * memory access is enabled for regions that have read side-effects,
242 * this can cause unexpected results, including lockups on some hardware.
243 * This function is called to make sure each page is mapped while it is
244 * safe to do so.
248 * XXX Should get this the correct way (see os/xalloc.c), but since this is
249 * for one platform I'll be lazy.
251 #define X_PAGE_SIZE 4096
253 static void
254 readSideEffects(int ScreenNum, pointer Base, unsigned long Size)
256 unsigned long base, end, addr;
257 CARD32 val;
259 base = (unsigned long)Base;
260 end = base + Size;
262 for (addr = base; addr < end; addr += X_PAGE_SIZE)
263 val = *(volatile CARD32 *)addr;
265 #endif
267 void
268 xf86OSInitVidMem(VidMemInfoPtr pVidMem)
270 pVidMem->linearSupported = linearVidMem();
271 pVidMem->mapMem = mapVidMem;
272 pVidMem->unmapMem = unmapVidMem;
273 #if defined(SVR4) && defined(i386) && !defined(sun)
274 pVidMem->readSideEffects = readSideEffects;
275 #endif
276 pVidMem->initialised = TRUE;
279 /***************************************************************************/
280 /* I/O Permissions section */
281 /***************************************************************************/
283 static Bool ExtendedEnabled = FALSE;
284 static Bool InitDone = FALSE;
286 _X_EXPORT Bool
287 xf86EnableIO()
289 int i;
291 if (ExtendedEnabled)
292 return TRUE;
294 if (SET_IOPL() < 0)
296 xf86Msg(X_WARNING,
297 "xf86EnableIO: Failed to set IOPL for extended I/O\n");
298 return FALSE;
300 ExtendedEnabled = TRUE;
302 return TRUE;
305 _X_EXPORT void
306 xf86DisableIO()
308 if (!ExtendedEnabled)
309 return;
311 RESET_IOPL();
312 ExtendedEnabled = FALSE;
314 return;
317 /***************************************************************************/
318 /* Interrupt Handling section */
319 /***************************************************************************/
321 _X_EXPORT Bool
322 xf86DisableInterrupts()
324 if (!ExtendedEnabled)
326 if (SET_IOPL() < 0)
328 return(FALSE);
332 #ifdef __GNUC__
333 __asm__ __volatile__("cli");
334 #else
335 asm("cli");
336 #endif /* __GNUC__ */
338 if (!ExtendedEnabled)
340 RESET_IOPL();
342 return(TRUE);
345 _X_EXPORT void
346 xf86EnableInterrupts()
348 if (!ExtendedEnabled)
350 if (SET_IOPL() < 0)
352 return;
356 #ifdef __GNUC__
357 __asm__ __volatile__("sti");
358 #else
359 asm("sti");
360 #endif /* __GNUC__ */
362 if (!ExtendedEnabled)
364 RESET_IOPL();
366 return;