2 * Copyright 1992 by Rich Murphey <Rich@Rice.edu>
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 Rich Murphey and David Wexelblat
10 * not be used in advertising or publicity pertaining to distribution of
11 * the software without specific, written prior permission. Rich Murphey and
12 * David Wexelblat make no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
16 * RICH MURPHEY AND DAVID WEXELBLAT DISCLAIM ALL WARRANTIES WITH REGARD TO
17 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL RICH MURPHEY 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.
27 * The ARM32 code here carries the following copyright:
30 * Digital Equipment Corporation. All rights reserved.
31 * This software is furnished under license and may be used and copied only in
32 * accordance with the following terms and conditions. Subject to these
33 * conditions, you may download, copy, install, use, modify and distribute
34 * this software in source and/or binary form. No title or ownership is
37 * 1) Any source code used, modified or distributed must reproduce and retain
38 * this copyright notice and list of conditions as they appear in the
41 * 2) No right is granted to use any trade name, trademark, or logo of Digital
42 * Equipment Corporation. Neither the "Digital Equipment Corporation"
43 * name nor any trademark or logo of Digital Equipment Corporation may be
44 * used to endorse or promote products derived from this software without
45 * the prior written permission of Digital Equipment Corporation.
47 * 3) This software is provided "AS-IS" and any express or implied warranties,
48 * including but not limited to, any implied warranties of merchantability,
49 * fitness for a particular purpose, or non-infringement are disclaimed.
50 * In no event shall DIGITAL be liable for any damages whatsoever, and in
51 * particular, DIGITAL shall not be liable for special, indirect,
52 * consequential, or incidental damages or damages for lost profits, loss
53 * of revenue or loss of use, whether such damages arise in contract,
54 * negligence, tort, under statute, in equity, at law or otherwise, even
55 * if advised of the possibility of such damage.
59 #ifdef HAVE_XORG_CONFIG_H
60 #include <xorg-config.h>
66 #include "xf86_OSlib.h"
67 #include "xf86OSpriv.h"
70 #include "machine/devmap.h"
74 struct map_info memInfo
;
75 pointer regionVirtBase
;
80 static pointer
xf86MapInfoMap();
81 static void xf86MapInfoUnmap();
82 static struct memAccess
*checkMapInfo();
83 extern int vgaPhysLinearBase
;
85 /* A memAccess structure is needed for each possible region */
86 struct memAccess vgaMemInfo
= { CONSOLE_GET_MEM_INFO
, NULL
, NULL
,
88 struct memAccess linearMemInfo
= { CONSOLE_GET_LINEAR_INFO
, NULL
, NULL
,
90 struct memAccess ioMemInfo
= { CONSOLE_GET_IO_INFO
, NULL
, NULL
,
92 #endif /* __arm32__ */
94 #if defined(__NetBSD__) && !defined(MAP_FILE)
95 #define MAP_FLAGS MAP_SHARED
97 #define MAP_FLAGS (MAP_FILE | MAP_SHARED)
101 #define MAP_FAILED ((caddr_t)-1)
106 #define BUS_BASE_BWX 0L
109 /***************************************************************************/
110 /* Video Memory Mapping section */
111 /***************************************************************************/
113 static Bool useDevMem
= FALSE
;
114 static int devMemFd
= -1;
116 #define DEV_MEM "/dev/mem"
118 static pointer
mapVidMem(int, unsigned long, unsigned long, int);
119 static void unmapVidMem(int, pointer
, unsigned long);
122 * Check if /dev/mem can be mmap'd. If it can't print a warning when
126 checkDevMem(Bool warn
)
128 static Bool devMemChecked
= FALSE
;
134 devMemChecked
= TRUE
;
136 if ((fd
= open(DEV_MEM
, O_RDWR
)) >= 0)
138 /* Try to map a page at the VGA address */
139 base
= mmap((caddr_t
)0, 4096, PROT_READ
| PROT_WRITE
,
140 MAP_FLAGS
, fd
, (off_t
)0xA0000 + BUS_BASE
);
142 if (base
!= MAP_FAILED
)
144 munmap((caddr_t
)base
, 4096);
149 /* This should not happen */
152 xf86Msg(X_WARNING
, "checkDevMem: failed to mmap %s (%s)\n",
153 DEV_MEM
, strerror(errno
));
161 xf86Msg(X_WARNING
, "checkDevMem: failed to open %s (%s)\n",
162 DEV_MEM
, strerror(errno
));
169 xf86OSInitVidMem(VidMemInfoPtr pVidMem
)
173 pVidMem
->linearSupported
= useDevMem
;
174 pVidMem
->mapMem
= armMapVidMem
;
175 pVidMem
->unmapVidMem
= armUnmapVidMem
;
177 pVidMem
->initialised
= TRUE
;
181 mapVidMem(int ScreenNum
, unsigned long Base
, unsigned long Size
, int flags
)
191 FatalError("xf86MapVidMem: failed to open %s (%s)\n",
192 DEV_MEM
, strerror(errno
));
194 base
= mmap((caddr_t
)0, Size
,
195 (flags
& VIDMEM_READONLY
) ?
196 PROT_READ
: (PROT_READ
| PROT_WRITE
),
197 MAP_FLAGS
, devMemFd
, (off_t
)Base
+ BUS_BASE_BWX
);
198 if (base
== MAP_FAILED
)
200 FatalError("%s: could not mmap %s [s=%x,a=%x] (%s)\n",
201 "xf86MapVidMem", DEV_MEM
, Size
, Base
,
207 /* else, mmap /dev/vga */
208 if ((unsigned long)Base
< 0xA0000 || (unsigned long)Base
>= 0xC0000)
210 FatalError("%s: Address 0x%x outside allowable range\n",
211 "xf86MapVidMem", Base
);
214 (flags
& VIDMEM_READONLY
) ?
215 PROT_READ
: (PROT_READ
| PROT_WRITE
),
216 MAP_FLAGS
, xf86Info
.screenFd
,
217 (unsigned long)Base
- 0xA0000);
218 if (base
== MAP_FAILED
)
220 FatalError("xf86MapVidMem: Could not mmap /dev/vga (%s)\n",
227 unmapVidMem(int ScreenNum
, pointer Base
, unsigned long Size
)
229 munmap((caddr_t
)Base
, Size
);
233 * Read BIOS via mmap()ing DEV_MEM
237 xf86ReadBIOS(unsigned long Base
, unsigned long Offset
, unsigned char *Buf
,
245 if (devMemFd
== -1) {
249 psize
= xf86getpagesize();
250 Offset
+= Base
& (psize
- 1);
251 Base
&= ~(psize
- 1);
252 mlen
= (Offset
+ Len
+ psize
- 1) & ~(psize
- 1);
253 ptr
= (unsigned char *)mmap((caddr_t
)0, mlen
, PROT_READ
,
254 MAP_SHARED
, devMemFd
, (off_t
)Base
+BUS_BASE
);
258 "xf86ReadBIOS: %s mmap[s=%x,a=%x,o=%x] failed (%s)\n",
259 DEV_MEM
, Len
, Base
, Offset
, strerror(errno
));
263 ErrorF("xf86ReadBIOS: BIOS at 0x%08x has signature 0x%04x\n",
264 Base
, ptr
[0] | (ptr
[1] << 8));
266 (void)memcpy(Buf
, (void *)(ptr
+ Offset
), Len
);
267 (void)munmap((caddr_t
)ptr
, mlen
);
269 xf86MsgVerb(X_INFO
, 3, "xf86ReadBIOS(%x, %x, Buf, %x)"
270 "-> %02x %02x %02x %02x...\n",
271 Base
, Offset
, Len
, Buf
[0], Buf
[1], Buf
[2], Buf
[3]);
277 /* XXX This needs to be updated for the ND */
280 ** Find out whether the console driver provides memory mapping information
281 ** for the specified region and return the map_info pointer. Print a warning if required.
283 static struct memAccess
*
284 checkMapInfo(Bool warn
, int Region
)
286 struct memAccess
*memAccP
;
291 memAccP
= &vgaMemInfo
;
295 memAccP
= &linearMemInfo
;
299 memAccP
= &ioMemInfo
;
307 if(!memAccP
->Checked
)
309 if(ioctl(xf86Info
.screenFd
, memAccP
->ioctl
, &(memAccP
->memInfo
)) == -1)
314 "checkMapInfo: failed to get map info for region %d\n\t(%s)\n",
315 Region
, strerror(errno
));
320 if(memAccP
->memInfo
.u
.map_info_mmap
.map_offset
324 memAccP
->Checked
= TRUE
;
337 xf86MapInfoMap(struct memAccess
*memInfoP
, pointer Base
, unsigned long Size
)
339 struct map_info
*mapInfoP
= &(memInfoP
->memInfo
);
341 if (mapInfoP
->u
.map_info_mmap
.map_size
== MAP_INFO_UNKNOWN
)
343 Size
= (unsigned long)Base
+ Size
;
347 Size
= mapInfoP
->u
.map_info_mmap
.map_size
;
350 switch(mapInfoP
->method
)
353 /* Need to remap if size is unknown because we may not have
354 mapped the whole region initially */
355 if(memInfoP
->regionVirtBase
== NULL
||
356 mapInfoP
->u
.map_info_mmap
.map_size
== MAP_INFO_UNKNOWN
)
358 if((memInfoP
->regionVirtBase
=
361 PROT_READ
| PROT_WRITE
,
364 (unsigned long)mapInfoP
->u
.map_info_mmap
.map_offset
))
367 FatalError("xf86MapInfoMap: Failed to map memory at 0x%x\n\t%s\n",
368 mapInfoP
->u
.map_info_mmap
.map_offset
, strerror(errno
));
370 if(mapInfoP
->u
.map_info_mmap
.internal_offset
> 0)
371 memInfoP
->regionVirtBase
+=
372 mapInfoP
->u
.map_info_mmap
.internal_offset
;
377 FatalError("xf86MapInfoMap: Unsuported mapping method\n");
381 return (pointer
)((int)memInfoP
->regionVirtBase
+ (int)Base
);
385 xf86MapInfoUnmap(struct memAccess
*memInfoP
, unsigned long Size
)
387 struct map_info
*mapInfoP
= &(memInfoP
->memInfo
);
389 switch(mapInfoP
->method
)
392 if(memInfoP
->regionVirtBase
!= NULL
)
394 if(mapInfoP
->u
.map_info_mmap
.map_size
!= MAP_INFO_UNKNOWN
)
395 Size
= mapInfoP
->u
.map_info_mmap
.map_size
;
396 munmap((caddr_t
)memInfoP
->regionVirtBase
, Size
);
397 memInfoP
->regionVirtBase
= NULL
;
401 FatalError("xf86MapInfoMap: Unsuported mapping method\n");
407 armMapVidMem(int ScreenNum
, unsigned long Base
, unsigned long Size
, int flags
)
409 struct memAccess
*memInfoP
;
411 if((memInfoP
= checkMapInfo(FALSE
, Region
)) != NULL
)
414 ** xf86 passes in a physical address offset from the start
415 ** of physical memory, but xf86MapInfoMap expects an
416 ** offset from the start of the specified region - it gets
417 ** the physical address of the region from the display driver.
422 if (vgaPhysLinearBase
)
424 Base
-= vgaPhysLinearBase
;
432 base
= xf86MapInfoMap(memInfoP
, Base
, Size
);
435 return mapVidMem(ScreenNum
, Base
, Size
, flags
);
439 armUnmapVidMem(int ScreenNum
, pointer Base
, unsigned long Size
)
441 struct memAccess
*memInfoP
;
443 if((memInfoP
= checkMapInfo(FALSE
, Region
)) != NULL
)
445 xf86MapInfoUnmap(memInfoP
, Base
, Size
);
447 unmapVidMem(ScreenNum
, Base
, Size
);
451 static int IoFd
= -1;
459 if ((IoFd
= open("/dev/io", O_RDWR
)) == -1)
461 xf86Msg(X_WARNING
,"xf86EnableIO: "
462 "Failed to open /dev/io for extended I/O\n");
481 #if defined(USE_ARC_MMAP) || defined(__arm32__)
492 if ((fd
= open("/dev/ttyC0", O_RDWR
)) >= 0) {
493 /* Try to map a page at the pccons I/O space */
494 base
= (pointer
)mmap((caddr_t
)0, 65536, PROT_READ
| PROT_WRITE
,
495 MAP_FLAGS
, fd
, (off_t
)0x0000);
497 if (base
!= (pointer
)-1) {
501 xf86Msg(X_WARNING
,"EnableIO: failed to mmap %s (%s)\n",
502 "/dev/ttyC0", strerror(errno
));
507 xf86Msg("EnableIO: failed to open %s (%s)\n",
508 "/dev/ttyC0", strerror(errno
));
512 ExtendedEnabled
= TRUE
;
523 #endif /* USE_ARC_MMAP */
526 /***************************************************************************/
527 /* Interrupt Handling section */
528 /***************************************************************************/
531 xf86DisableInterrupts()
538 xf86EnableInterrupts()
548 * XXX This is here for reference. It needs to be handled differently for the
551 #if defined(USE_ARC_MMAP) || defined(__arm32__)
553 #ifdef USE_ARM32_MMAP
554 #define DEV_MEM_IOBASE 0x43000000
557 static Bool ScreenEnabled
[MAXSCREENS
];
558 static Bool ExtendedEnabled
= FALSE
;
559 static Bool InitDone
= FALSE
;
562 xf86EnableIOPorts(ScreenNum
)
570 struct memAccess
*memInfoP
;
574 ScreenEnabled
[ScreenNum
] = TRUE
;
580 if ((fd
= open("/dev/ttyC0", O_RDWR
)) >= 0) {
581 /* Try to map a page at the pccons I/O space */
582 base
= (pointer
)mmap((caddr_t
)0, 65536, PROT_READ
| PROT_WRITE
,
583 MAP_FLAGS
, fd
, (off_t
)0x0000);
585 if (base
!= (pointer
)-1) {
590 "EnableIOPorts: failed to mmap %s (%s)\n",
591 "/dev/ttyC0", strerror(errno
));
595 xf86Msg(X_ERROR
, "EnableIOPorts: failed to open %s (%s)\n",
596 "/dev/ttyC0", strerror(errno
));
601 IOPortBase
= (unsigned int)-1;
603 if((memInfoP
= checkMapInfo(TRUE
, MMIO_REGION
)) != NULL
)
606 * xf86MapInfoMap maps an offset from the start of video IO
607 * space (e.g. 0x3B0), but IOPortBase is expected to map to
608 * physical address 0x000, so subtract the start of video I/O
609 * space from the result. This is safe for now becase we
610 * actually mmap the start of the page, then the start of video
611 * I/O space is added as an internal offset.
613 IOPortBase
= (unsigned int)xf86MapInfoMap(memInfoP
,
615 - memInfoP
->memInfo
.u
.map_info_mmap
.internal_offset
;
616 ExtendedEnabled
= TRUE
;
619 #ifdef USE_ARM32_MMAP
622 if (devMemFd
>= 0 && useDevMem
)
624 base
= (pointer
)mmap((caddr_t
)0, 0x400, PROT_READ
| PROT_WRITE
,
625 MAP_FLAGS
, devMemFd
, (off_t
)DEV_MEM_IOBASE
);
627 if (base
!= (pointer
)-1)
628 IOPortBase
= (unsigned int)base
;
631 if (IOPortBase
== (unsigned int)-1)
633 xf86Msg(X_WARNING
,"xf86EnableIOPorts: failed to open mem device or map IO base. \n\
634 Make sure you have the Aperture Driver installed, or a kernel built with the INSECURE option\n");
638 /* We don't have the IOBASE, so we can't map the address */
639 xf86Msg(X_WARNING
,"xf86EnableIOPorts: failed to open mem device or map IO base. \n\
640 Try building the server with USE_ARM32_MMAP defined\n");
645 ExtendedEnabled
= TRUE
;
651 xf86DisableIOPorts(ScreenNum
)
656 struct memAccess
*memInfoP
;
659 ScreenEnabled
[ScreenNum
] = FALSE
;
662 if((memInfoP
= checkMapInfo(FALSE
, MMIO_REGION
)) != NULL
)
664 xf86MapInfoUnmap(memInfoP
, 0);
668 #ifdef USE_ARM32_MMAP
669 if (!ExtendedEnabled
)
672 for (i
= 0; i
< MAXSCREENS
; i
++)
673 if (ScreenEnabled
[i
])
676 munmap((caddr_t
)IOPortBase
, 0x400);
677 IOPortBase
= (unsigned int)-1;
678 ExtendedEnabled
= FALSE
;
684 #endif /* USE_ARC_MMAP || USE_ARM32_MMAP */