1 /*
2 * Copyright 1992 by Orest Zborowski <obz@Kodak.com>
3 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
4 *
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 Orest Zborowski and David Wexelblat
10 * not be used in advertising or publicity pertaining to distribution of
11 * the software without specific, written prior permission. Orest Zborowski
12 * and David Wexelblat make no representations about the suitability of this
13 * software for any purpose. It is provided "as is" without express or
14 * implied warranty.
15 *
16 * OREST ZBOROWSKI AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD
17 * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT BE LIABLE
19 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
22 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 *
24 */
25
26 #ifdef HAVE_XORG_CONFIG_H
27 #include <xorg-config.h>
28 #endif
29
30 #include <errno.h>
31 #include <string.h>
32
33 #include <X11/X.h>
34 #include "input.h"
35 #include "scrnintstr.h"
36
37 #include "xf86.h"
38 #include "xf86Priv.h"
39 #include "xf86_OSlib.h"
40 #include "xf86OSpriv.h"
41
42 static Bool ExtendedEnabled = FALSE;
43
44 #ifdef __ia64__
45
46 #include "compiler.h"
47 #include <sys/io.h>
48
49 #elif !defined(__powerpc__) && \
50 !defined(__mc68000__) && \
51 !defined(__sparc__) && \
52 !defined(__mips__) && \
53 !defined(__nds32__) && \
54 !defined(__arm__) && \
55 !defined(__aarch64__) && \
56 !defined(__arc__) && \
57 !defined(__xtensa__)
58
59 /*
60 * Due to conflicts with "compiler.h", don't rely on <sys/io.h> to declare
61 * these.
62 */
63 extern int ioperm(unsigned long __from, unsigned long __num, int __turn_on);
64 extern int iopl(int __level);
65
66 #endif
67
68 /***************************************************************************/
69 /* Video Memory Mapping section */
70 /***************************************************************************/
71
72 void
xf86OSInitVidMem(VidMemInfoPtr pVidMem)73 xf86OSInitVidMem(VidMemInfoPtr pVidMem)
74 {
75 pVidMem->initialised = TRUE;
76 }
77
78 /***************************************************************************/
79 /* I/O Permissions section */
80 /***************************************************************************/
81
82 #if defined(__powerpc__)
83 volatile unsigned char *ioBase = NULL;
84
85 #ifndef __NR_pciconfig_iobase
86 #define __NR_pciconfig_iobase 200
87 #endif
88
89 static Bool
hwEnableIO(void)90 hwEnableIO(void)
91 {
92 int fd;
93 unsigned int ioBase_phys = syscall(__NR_pciconfig_iobase, 2, 0, 0);
94
95 fd = open("/dev/mem", O_RDWR);
96 if (ioBase == NULL) {
97 ioBase = (volatile unsigned char *) mmap(0, 0x20000,
98 PROT_READ | PROT_WRITE,
99 MAP_SHARED, fd, ioBase_phys);
100 }
101 close(fd);
102
103 return ioBase != MAP_FAILED;
104 }
105
106 static void
hwDisableIO(void)107 hwDisableIO(void)
108 {
109 munmap(ioBase, 0x20000);
110 ioBase = NULL;
111 }
112
113 #elif defined(__i386__) || defined(__x86_64__) || defined(__ia64__) || \
114 defined(__alpha__)
115
116 static Bool
hwEnableIO(void)117 hwEnableIO(void)
118 {
119 if (ioperm(0, 1024, 1) || iopl(3)) {
120 ErrorF("xf86EnableIOPorts: failed to set IOPL for I/O (%s)\n",
121 strerror(errno));
122 return FALSE;
123 }
124 #if !defined(__alpha__)
125 /* XXX: this is actually not trapping anything because of iopl(3)
126 * above */
127 ioperm(0x40, 4, 0); /* trap access to the timer chip */
128 ioperm(0x60, 4, 0); /* trap access to the keyboard controller */
129 #endif
130
131 return TRUE;
132 }
133
134 static void
hwDisableIO(void)135 hwDisableIO(void)
136 {
137 iopl(0);
138 ioperm(0, 1024, 0);
139 }
140
141 #else /* non-IO architectures */
142
143 #define hwEnableIO() TRUE
144 #define hwDisableIO() do {} while (0)
145
146 #endif
147
148 Bool
xf86EnableIO(void)149 xf86EnableIO(void)
150 {
151 if (ExtendedEnabled)
152 return TRUE;
153
154 ExtendedEnabled = hwEnableIO();
155
156 return ExtendedEnabled;
157 }
158
159 void
xf86DisableIO(void)160 xf86DisableIO(void)
161 {
162 if (!ExtendedEnabled)
163 return;
164
165 hwDisableIO();
166
167 ExtendedEnabled = FALSE;
168 }
169