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
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>
35 #include "xf86_OSlib.h"
36 #include "xf86OSpriv.h"
39 #define MAP_FAILED ((void *)-1)
43 #define SET_IOPL() sysi86(SI86V86,V86SC_IOPL,PS_IOPL)
44 #define RESET_IOPL() sysi86(SI86V86,V86SC_IOPL,0)
46 #define SET_IOPL() sysi86(SI86IOPL,3)
47 #define RESET_IOPL() sysi86(SI86IOPL,0)
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
68 struct kd_memloc MapDSC
;
72 mmapStat(pointer Base
, unsigned long Size
) {
74 int nmmreg
,i
=0,region
=-1;
77 nmmreg
= ioctl(mmapFd
, GETNMMREG
);
80 xf86MsgVerb(X_INFO
, MMAP_DEBUG
,
81 "\nNo physical memory mapped currently.\n\n");
83 if((ibuf
= (mmapinfo_t
*)malloc(nmmreg
*sizeof(mmapinfo_t
))) == NULL
)
85 "Couldn't allocate memory 4 mmapinfo_t\n");
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");
108 if (region
== -1 && nmmreg
> 0) region
=region
* i
;
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) {
129 "xf86LinearVidMem: MMAP 2.2.2 or above required\n");
130 xf86ErrorF("\tlinear memory access disabled\n");
135 xf86Msg(X_WARNING
, "xf86LinearVidMem: failed to open /dev/mmap (%s)\n",
137 xf86ErrorF("\tlinear memory access disabled\n");
143 mapVidMem(int ScreenNum
, unsigned long Base
, unsigned long Size
, int flags
)
149 fd
= open(DEV_MEM
, (flags
& VIDMEM_READONLY
) ? O_RDONLY
: O_RDWR
);
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
);
160 if (base
== MAP_FAILED
)
162 FatalError("%s: Could not mmap framebuffer [s=%x,a=%x] (%s)\n",
163 "xf86MapVidMem", Size
, Base
, strerror(errno
));
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)
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
);
180 /* To force the MMAP driver to provide the address */
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
;
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
));
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
);
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 */
219 unmapVidMem(int ScreenNum
, pointer Base
, unsigned long Size
)
224 #ifdef HAS_SVR3_MMAPDRV
225 xf86MsgVerb(X_INFO
, MMAP_DEBUG
,
226 "# xf86UnMapVidMem: UNMapping FrameBuffer\n");
228 ioctl(mmapFd
, UNMAPRM
, Base
);
230 xf86MsgVerb(X_INFO
, MMAP_DEBUG
,
231 "# xf86UnMapVidMem: Screen: %d [v=%x]\n", ScreenNum
, Base
);
232 #endif /* HAS_SVR3_MMAPDRV */
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
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
254 readSideEffects(int ScreenNum
, pointer Base
, unsigned long Size
)
256 unsigned long base
, end
, addr
;
259 base
= (unsigned long)Base
;
262 for (addr
= base
; addr
< end
; addr
+= X_PAGE_SIZE
)
263 val
= *(volatile CARD32
*)addr
;
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
;
276 pVidMem
->initialised
= TRUE
;
279 /***************************************************************************/
280 /* I/O Permissions section */
281 /***************************************************************************/
283 static Bool ExtendedEnabled
= FALSE
;
284 static Bool InitDone
= FALSE
;
297 "xf86EnableIO: Failed to set IOPL for extended I/O\n");
300 ExtendedEnabled
= TRUE
;
308 if (!ExtendedEnabled
)
312 ExtendedEnabled
= FALSE
;
317 /***************************************************************************/
318 /* Interrupt Handling section */
319 /***************************************************************************/
322 xf86DisableInterrupts()
324 if (!ExtendedEnabled
)
333 __asm__
__volatile__("cli");
336 #endif /* __GNUC__ */
338 if (!ExtendedEnabled
)
346 xf86EnableInterrupts()
348 if (!ExtendedEnabled
)
357 __asm__
__volatile__("sti");
360 #endif /* __GNUC__ */
362 if (!ExtendedEnabled
)