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"
73 struct map_info memInfo
;
74 pointer regionVirtBase
;
79 static pointer
xf86MapInfoMap();
80 static void xf86MapInfoUnmap();
81 static struct memAccess
*checkMapInfo();
82 extern int vgaPhysLinearBase
;
84 /* A memAccess structure is needed for each possible region */
85 struct memAccess vgaMemInfo
= { CONSOLE_GET_MEM_INFO
, NULL
, NULL
,
89 struct memAccess linearMemInfo
= { CONSOLE_GET_LINEAR_INFO
, NULL
, NULL
,
93 struct memAccess ioMemInfo
= { CONSOLE_GET_IO_INFO
, NULL
, NULL
,
96 #endif /* __arm32__ */
98 #if defined(__NetBSD__) && !defined(MAP_FILE)
99 #define MAP_FLAGS MAP_SHARED
101 #define MAP_FLAGS (MAP_FILE | MAP_SHARED)
105 #define BUS_BASE_BWX 0L
107 /***************************************************************************/
108 /* Video Memory Mapping section */
109 /***************************************************************************/
111 static Bool useDevMem
= FALSE
;
112 static int devMemFd
= -1;
114 static pointer
mapVidMem(int, unsigned long, unsigned long, int);
115 static void unmapVidMem(int, pointer
, unsigned long);
118 * Check if /dev/mem can be mmap'd. If it can't print a warning when
122 checkDevMem(Bool warn
)
124 static Bool devMemChecked
= FALSE
;
130 devMemChecked
= TRUE
;
132 if ((fd
= open(DEV_MEM
, O_RDWR
)) >= 0) {
133 /* Try to map a page at the VGA address */
134 base
= mmap((caddr_t
) 0, 4096, PROT_READ
| PROT_WRITE
,
135 MAP_FLAGS
, fd
, (off_t
) 0xA0000 + BUS_BASE
);
137 if (base
!= MAP_FAILED
) {
138 munmap((caddr_t
) base
, 4096);
144 /* This should not happen */
146 xf86Msg(X_WARNING
, "checkDevMem: failed to mmap %s (%s)\n",
147 DEV_MEM
, strerror(errno
));
154 xf86Msg(X_WARNING
, "checkDevMem: failed to open %s (%s)\n",
155 DEV_MEM
, strerror(errno
));
162 xf86OSInitVidMem(VidMemInfoPtr pVidMem
)
166 pVidMem
->linearSupported
= useDevMem
;
167 pVidMem
->mapMem
= armMapVidMem
;
168 pVidMem
->unmapVidMem
= armUnmapVidMem
;
170 pVidMem
->initialised
= TRUE
;
174 mapVidMem(int ScreenNum
, unsigned long Base
, unsigned long Size
, int flags
)
182 FatalError("xf86MapVidMem: failed to open %s (%s)\n",
183 DEV_MEM
, strerror(errno
));
185 base
= mmap((caddr_t
) 0, Size
,
186 (flags
& VIDMEM_READONLY
) ?
187 PROT_READ
: (PROT_READ
| PROT_WRITE
),
188 MAP_FLAGS
, devMemFd
, (off_t
) Base
+ BUS_BASE_BWX
);
189 if (base
== MAP_FAILED
) {
190 FatalError("%s: could not mmap %s [s=%x,a=%x] (%s)\n",
191 "xf86MapVidMem", DEV_MEM
, Size
, Base
, strerror(errno
));
196 /* else, mmap /dev/vga */
197 if ((unsigned long) Base
< 0xA0000 || (unsigned long) Base
>= 0xC0000) {
198 FatalError("%s: Address 0x%x outside allowable range\n",
199 "xf86MapVidMem", Base
);
202 (flags
& VIDMEM_READONLY
) ?
203 PROT_READ
: (PROT_READ
| PROT_WRITE
),
204 MAP_FLAGS
, xf86Info
.consoleFd
, (unsigned long) Base
- 0xA0000);
205 if (base
== MAP_FAILED
) {
206 FatalError("xf86MapVidMem: Could not mmap /dev/vga (%s)\n",
213 unmapVidMem(int ScreenNum
, pointer Base
, unsigned long Size
)
215 munmap((caddr_t
) Base
, Size
);
219 * Read BIOS via mmap()ing DEV_MEM
223 xf86ReadBIOS(unsigned long Base
, unsigned long Offset
, unsigned char *Buf
,
231 if (devMemFd
== -1) {
235 psize
= getpagesize();
236 Offset
+= Base
& (psize
- 1);
237 Base
&= ~(psize
- 1);
238 mlen
= (Offset
+ Len
+ psize
- 1) & ~(psize
- 1);
239 ptr
= (unsigned char *) mmap((caddr_t
) 0, mlen
, PROT_READ
,
240 MAP_SHARED
, devMemFd
, (off_t
) Base
+ BUS_BASE
);
241 if ((long) ptr
== -1) {
243 "xf86ReadBIOS: %s mmap[s=%x,a=%x,o=%x] failed (%s)\n",
244 DEV_MEM
, Len
, Base
, Offset
, strerror(errno
));
248 ErrorF("xf86ReadBIOS: BIOS at 0x%08x has signature 0x%04x\n",
249 Base
, ptr
[0] | (ptr
[1] << 8));
251 (void) memcpy(Buf
, (void *) (ptr
+ Offset
), Len
);
252 (void) munmap((caddr_t
) ptr
, mlen
);
254 xf86MsgVerb(X_INFO
, 3, "xf86ReadBIOS(%x, %x, Buf, %x)"
255 "-> %02x %02x %02x %02x...\n",
256 Base
, Offset
, Len
, Buf
[0], Buf
[1], Buf
[2], Buf
[3]);
261 /* XXX This needs to be updated for the ND */
264 ** Find out whether the console driver provides memory mapping information
265 ** for the specified region and return the map_info pointer. Print a warning if required.
267 static struct memAccess
*
268 checkMapInfo(Bool warn
, int Region
)
270 struct memAccess
*memAccP
;
274 memAccP
= &vgaMemInfo
;
278 memAccP
= &linearMemInfo
;
282 memAccP
= &ioMemInfo
;
290 if (!memAccP
->Checked
) {
291 if (ioctl(xf86Info
.consoleFd
, memAccP
->ioctl
, &(memAccP
->memInfo
)) ==
295 "checkMapInfo: failed to get map info for region %d\n\t(%s)\n",
296 Region
, strerror(errno
));
300 if (memAccP
->memInfo
.u
.map_info_mmap
.map_offset
!= MAP_INFO_UNKNOWN
)
303 memAccP
->Checked
= TRUE
;
314 xf86MapInfoMap(struct memAccess
*memInfoP
, pointer Base
, unsigned long Size
)
316 struct map_info
*mapInfoP
= &(memInfoP
->memInfo
);
318 if (mapInfoP
->u
.map_info_mmap
.map_size
== MAP_INFO_UNKNOWN
) {
319 Size
= (unsigned long) Base
+ Size
;
322 Size
= mapInfoP
->u
.map_info_mmap
.map_size
;
325 switch (mapInfoP
->method
) {
327 /* Need to remap if size is unknown because we may not have
328 mapped the whole region initially */
329 if (memInfoP
->regionVirtBase
== NULL
||
330 mapInfoP
->u
.map_info_mmap
.map_size
== MAP_INFO_UNKNOWN
) {
331 if ((memInfoP
->regionVirtBase
=
334 PROT_READ
| PROT_WRITE
,
337 (unsigned long) mapInfoP
->u
.map_info_mmap
.map_offset
))
340 ("xf86MapInfoMap: Failed to map memory at 0x%x\n\t%s\n",
341 mapInfoP
->u
.map_info_mmap
.map_offset
, strerror(errno
));
343 if (mapInfoP
->u
.map_info_mmap
.internal_offset
> 0)
344 memInfoP
->regionVirtBase
+=
345 mapInfoP
->u
.map_info_mmap
.internal_offset
;
350 FatalError("xf86MapInfoMap: Unsuported mapping method\n");
354 return (pointer
) ((int) memInfoP
->regionVirtBase
+ (int) Base
);
358 xf86MapInfoUnmap(struct memAccess
*memInfoP
, unsigned long Size
)
360 struct map_info
*mapInfoP
= &(memInfoP
->memInfo
);
362 switch (mapInfoP
->method
) {
364 if (memInfoP
->regionVirtBase
!= NULL
) {
365 if (mapInfoP
->u
.map_info_mmap
.map_size
!= MAP_INFO_UNKNOWN
)
366 Size
= mapInfoP
->u
.map_info_mmap
.map_size
;
367 munmap((caddr_t
) memInfoP
->regionVirtBase
, Size
);
368 memInfoP
->regionVirtBase
= NULL
;
372 FatalError("xf86MapInfoMap: Unsuported mapping method\n");
378 armMapVidMem(int ScreenNum
, unsigned long Base
, unsigned long Size
, int flags
)
380 struct memAccess
*memInfoP
;
382 if ((memInfoP
= checkMapInfo(FALSE
, Region
)) != NULL
) {
384 ** xf86 passes in a physical address offset from the start
385 ** of physical memory, but xf86MapInfoMap expects an
386 ** offset from the start of the specified region - it gets
387 ** the physical address of the region from the display driver.
391 if (vgaPhysLinearBase
) {
392 Base
-= vgaPhysLinearBase
;
400 base
= xf86MapInfoMap(memInfoP
, Base
, Size
);
403 return mapVidMem(ScreenNum
, Base
, Size
, flags
);
407 armUnmapVidMem(int ScreenNum
, pointer Base
, unsigned long Size
)
409 struct memAccess
*memInfoP
;
411 if ((memInfoP
= checkMapInfo(FALSE
, Region
)) != NULL
) {
412 xf86MapInfoUnmap(memInfoP
, Base
, Size
);
414 unmapVidMem(ScreenNum
, Base
, Size
);
418 static int IoFd
= -1;
426 if ((IoFd
= open("/dev/io", O_RDWR
)) == -1) {
427 xf86Msg(X_WARNING
, "xf86EnableIO: "
428 "Failed to open /dev/io for extended I/O\n");
447 #if defined(USE_ARC_MMAP) || defined(__arm32__)
458 if ((fd
= open("/dev/ttyC0", O_RDWR
)) >= 0) {
459 /* Try to map a page at the pccons I/O space */
460 base
= (pointer
) mmap((caddr_t
) 0, 65536, PROT_READ
| PROT_WRITE
,
461 MAP_FLAGS
, fd
, (off_t
) 0x0000);
463 if (base
!= (pointer
) -1) {
467 xf86Msg(X_WARNING
, "EnableIO: failed to mmap %s (%s)\n",
468 "/dev/ttyC0", strerror(errno
));
473 xf86Msg("EnableIO: failed to open %s (%s)\n",
474 "/dev/ttyC0", strerror(errno
));
478 ExtendedEnabled
= TRUE
;
489 #endif /* USE_ARC_MMAP */
493 * XXX This is here for reference. It needs to be handled differently for the
496 #if defined(USE_ARC_MMAP) || defined(__arm32__)
498 #ifdef USE_ARM32_MMAP
499 #define DEV_MEM_IOBASE 0x43000000
502 static Bool ScreenEnabled
[MAXSCREENS
];
503 static Bool ExtendedEnabled
= FALSE
;
504 static Bool InitDone
= FALSE
;
507 xf86EnableIOPorts(ScreenNum
)
515 struct memAccess
*memInfoP
;
519 ScreenEnabled
[ScreenNum
] = TRUE
;
525 if ((fd
= open("/dev/ttyC0", O_RDWR
)) >= 0) {
526 /* Try to map a page at the pccons I/O space */
527 base
= (pointer
) mmap((caddr_t
) 0, 65536, PROT_READ
| PROT_WRITE
,
528 MAP_FLAGS
, fd
, (off_t
) 0x0000);
530 if (base
!= (pointer
) -1) {
535 "EnableIOPorts: failed to mmap %s (%s)\n",
536 "/dev/ttyC0", strerror(errno
));
540 xf86Msg(X_ERROR
, "EnableIOPorts: failed to open %s (%s)\n",
541 "/dev/ttyC0", strerror(errno
));
546 IOPortBase
= (unsigned int) -1;
548 if ((memInfoP
= checkMapInfo(TRUE
, MMIO_REGION
)) != NULL
) {
550 * xf86MapInfoMap maps an offset from the start of video IO
551 * space (e.g. 0x3B0), but IOPortBase is expected to map to
552 * physical address 0x000, so subtract the start of video I/O
553 * space from the result. This is safe for now becase we
554 * actually mmap the start of the page, then the start of video
555 * I/O space is added as an internal offset.
557 IOPortBase
= (unsigned int) xf86MapInfoMap(memInfoP
, (caddr_t
) 0x0, 0L)
558 - memInfoP
->memInfo
.u
.map_info_mmap
.internal_offset
;
559 ExtendedEnabled
= TRUE
;
562 #ifdef USE_ARM32_MMAP
565 if (devMemFd
>= 0 && useDevMem
) {
566 base
= (pointer
) mmap((caddr_t
) 0, 0x400, PROT_READ
| PROT_WRITE
,
567 MAP_FLAGS
, devMemFd
, (off_t
) DEV_MEM_IOBASE
);
569 if (base
!= (pointer
) -1)
570 IOPortBase
= (unsigned int) base
;
573 if (IOPortBase
== (unsigned int) -1) {
575 "xf86EnableIOPorts: failed to open mem device or map IO base. \n\
576 Make sure you have the Aperture Driver installed, or a kernel built with the INSECURE option\n");
580 /* We don't have the IOBASE, so we can't map the address */
582 "xf86EnableIOPorts: failed to open mem device or map IO base. \n\
583 Try building the server with USE_ARM32_MMAP defined\n");
588 ExtendedEnabled
= TRUE
;
594 xf86DisableIOPorts(ScreenNum
)
600 struct memAccess
*memInfoP
;
603 ScreenEnabled
[ScreenNum
] = FALSE
;
606 if ((memInfoP
= checkMapInfo(FALSE
, MMIO_REGION
)) != NULL
) {
607 xf86MapInfoUnmap(memInfoP
, 0);
611 #ifdef USE_ARM32_MMAP
612 if (!ExtendedEnabled
)
615 for (i
= 0; i
< MAXSCREENS
; i
++)
616 if (ScreenEnabled
[i
])
619 munmap((caddr_t
) IOPortBase
, 0x400);
620 IOPortBase
= (unsigned int) -1;
621 ExtendedEnabled
= FALSE
;
627 #endif /* USE_ARC_MMAP || USE_ARM32_MMAP */