]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/arm/ioperm.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / arm / ioperm.c
1 /* Copyright (C) 1998-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Phil Blundell, based on the Alpha version by
4 David Mosberger.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library. If not, see
18 <http://www.gnu.org/licenses/>. */
19
20 /* I/O port access on the ARM is something of a fiction. What we do is to
21 map an appropriate area of /dev/mem into user space so that a program
22 can blast away at the hardware in such a way as to generate I/O cycles
23 on the bus. To insulate user code from dependencies on particular
24 hardware we don't allow calls to inb() and friends to be inlined, but
25 force them to come through code in here every time. Performance-critical
26 registers tend to be memory mapped these days so this should be no big
27 problem. */
28
29 /* Once upon a time this file used mprotect to enable and disable
30 access to particular areas of I/O space. Unfortunately the
31 mprotect syscall also has the side effect of enabling caching for
32 the area affected (this is a kernel limitation). So we now just
33 enable all the ports all of the time. */
34
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41
42 #include <sys/types.h>
43 #include <sys/mman.h>
44
45 #include <sys/sysctl.h>
46
47 #define MAX_PORT 0x10000
48
49 static struct {
50 unsigned long int base;
51 unsigned long int io_base;
52 unsigned int shift;
53 unsigned int initdone; /* since all the above could be 0 */
54 } io;
55
56 #define IO_ADDR(port) (io.base + ((port) << io.shift))
57
58 /*
59 * Initialize I/O system. The io_bae and port_shift values are fetched
60 * using sysctl (CTL_BUS, CTL_BUS_ISA, ISA_*).
61 */
62
63 static int
64 init_iosys (void)
65 {
66 static int iobase_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_BASE };
67 static int ioshift_name[] = { CTL_BUS, CTL_BUS_ISA, BUS_ISA_PORT_SHIFT };
68 size_t len = sizeof(io.base);
69
70 if (! __sysctl (iobase_name, 3, &io.io_base, &len, NULL, 0)
71 && ! __sysctl (ioshift_name, 3, &io.shift, &len, NULL, 0))
72 {
73 io.initdone = 1;
74 return 0;
75 }
76
77 /* sysctl has failed... */
78 __set_errno (ENODEV);
79 return -1;
80 }
81
82 int
83 _ioperm (unsigned long int from, unsigned long int num, int turn_on)
84 {
85 if (! io.initdone && init_iosys () < 0)
86 return -1;
87
88 /* this test isn't as silly as it may look like; consider overflows! */
89 if (from >= MAX_PORT || from + num > MAX_PORT)
90 {
91 __set_errno (EINVAL);
92 return -1;
93 }
94
95 if (turn_on)
96 {
97 if (! io.base)
98 {
99 int fd;
100
101 fd = __open ("/dev/mem", O_RDWR);
102 if (fd < 0)
103 return -1;
104
105 io.base =
106 (unsigned long int) __mmap (0, MAX_PORT << io.shift,
107 PROT_READ | PROT_WRITE,
108 MAP_SHARED, fd, io.io_base);
109 __close (fd);
110 if ((long) io.base == -1)
111 return -1;
112 }
113 }
114
115 return 0;
116 }
117
118
119 int
120 _iopl (unsigned int level)
121 {
122 if (level > 3)
123 {
124 __set_errno (EINVAL);
125 return -1;
126 }
127 if (level)
128 {
129 return _ioperm (0, MAX_PORT, 1);
130 }
131 return 0;
132 }
133
134
135 void
136 _outb (unsigned char b, unsigned long int port)
137 {
138 *((volatile unsigned char *)(IO_ADDR (port))) = b;
139 }
140
141
142 void
143 _outw (unsigned short b, unsigned long int port)
144 {
145 *((volatile unsigned short *)(IO_ADDR (port))) = b;
146 }
147
148
149 void
150 _outl (unsigned int b, unsigned long int port)
151 {
152 *((volatile unsigned long *)(IO_ADDR (port))) = b;
153 }
154
155
156 unsigned int
157 _inb (unsigned long int port)
158 {
159 return *((volatile unsigned char *)(IO_ADDR (port)));
160 }
161
162
163 unsigned int
164 _inw (unsigned long int port)
165 {
166 return *((volatile unsigned short *)(IO_ADDR (port)));
167 }
168
169
170 unsigned int
171 _inl (unsigned long int port)
172 {
173 return *((volatile unsigned long *)(IO_ADDR (port)));
174 }
175
176 weak_alias (_ioperm, ioperm);
177 weak_alias (_iopl, iopl);
178 weak_alias (_inb, inb);
179 weak_alias (_inw, inw);
180 weak_alias (_inl, inl);
181 weak_alias (_outb, outb);
182 weak_alias (_outw, outw);
183 weak_alias (_outl, outl);