2.9
[glibc/nacl-glibc.git] / sysdeps / unix / sysv / linux / ia64 / ioperm.c
blob89a0a36d3db5b15f4ca4d6a36b9064c838762abe
1 /* Copyright (C) 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 /* I/O access is restricted to ISA port space (ports 0..65535).
21 Modern devices hopefully are sane enough not to put any performance
22 critical registers in i/o space.
24 On the first call to ioperm() or iopl(), the entire (E)ISA port
25 space is mapped into the virtual address space at address io.base.
26 mprotect() calls are then used to enable/disable access to ports.
27 Per 4KB page, there are 4 I/O ports. */
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <ctype.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/mman.h>
39 #define MAX_PORT 0x10000
42 * Memory fence w/accept. This should never be used in code that is
43 * not IA-64 specific.
45 #define __ia64_mf_a() __asm__ __volatile__ ("mf.a" ::: "memory")
47 static struct
49 unsigned long int base;
50 unsigned long int page_mask;
52 io;
54 __inline__ unsigned long int
55 io_offset (unsigned long int port)
57 return ((port >> 2) << 12) | (port & 0xfff);
60 int
61 _ioperm (unsigned long int from, unsigned long int num, int turn_on)
63 #if 0
64 unsigned long int addr, len, base;
65 #endif
66 unsigned long int base;
67 int prot;
69 /* this test isn't as silly as it may look like; consider overflows! */
70 if (from >= MAX_PORT || from + num > MAX_PORT)
72 __set_errno (EINVAL);
73 return -1;
76 if (turn_on)
78 if (!io.base)
80 unsigned long phys_io_base, len;
81 int fd;
83 io.page_mask = ~(__getpagesize() - 1);
85 /* get I/O base physical address from ar.k0 as per PRM: */
86 __asm__ ("mov %0=ar.k0" : "=r"(phys_io_base));
88 /* The O_SYNC flag tells the /dev/mem driver to map the
89 memory uncached: */
90 fd = __open ("/dev/mem", O_RDWR | O_SYNC);
91 if (fd < 0)
92 return -1;
94 len = io_offset (MAX_PORT);
95 #if 1
96 /* see comment below */
97 base = (unsigned long int) __mmap (0, len, PROT_READ | PROT_WRITE, MAP_SHARED,
98 fd, phys_io_base);
99 #else
100 base = (unsigned long int) __mmap (0, len, PROT_NONE, MAP_SHARED,
101 fd, phys_io_base);
102 #endif
103 __close (fd);
105 if ((long) base == -1)
106 return -1;
108 io.base = base;
110 prot = PROT_READ | PROT_WRITE;
112 else
114 if (!io.base)
115 return 0; /* never was turned on... */
117 prot = PROT_NONE;
119 #if 0
120 /* We can't do mprotect because that would cause us to lose the
121 uncached flag that the /dev/mem driver turned on. A MAP_UNCACHED
122 flag seems so much cleaner... */
123 addr = (io.base + io_offset (from)) & io.page_mask;
124 len = io.base + io_offset (from + num) - addr;
125 return mprotect ((void *) addr, len, prot);
126 #else
127 return 0;
128 #endif
132 _iopl (unsigned int level)
134 if (level > 3)
136 __set_errno (EINVAL);
137 return -1;
139 if (level)
141 int retval = _ioperm (0, MAX_PORT, 1);
142 /* Match the documented error returns of the x86 version. */
143 if (retval < 0 && errno == EACCES)
144 __set_errno (EPERM);
145 return retval;
147 return 0;
150 unsigned int
151 _inb (unsigned long int port)
153 volatile unsigned char *addr = (void *) io.base + io_offset (port);
154 unsigned char ret;
156 ret = *addr;
157 __ia64_mf_a();
158 return ret;
161 unsigned int
162 _inw (unsigned long int port)
164 volatile unsigned short *addr = (void *) io.base + io_offset (port);
165 unsigned short ret;
167 ret = *addr;
168 __ia64_mf_a();
169 return ret;
172 unsigned int
173 _inl (unsigned long int port)
175 volatile unsigned int *addr = (void *) io.base + io_offset (port);
176 unsigned int ret;
178 ret = *addr;
179 __ia64_mf_a();
180 return ret;
183 void
184 _outb (unsigned char val, unsigned long int port)
186 volatile unsigned char *addr = (void *) io.base + io_offset (port);
188 *addr = val;
189 __ia64_mf_a();
192 void
193 _outw (unsigned short val, unsigned long int port)
195 volatile unsigned short *addr = (void *) io.base + io_offset (port);
197 *addr = val;
198 __ia64_mf_a();
201 void
202 _outl (unsigned int val, unsigned long int port)
204 volatile unsigned int *addr = (void *) io.base + io_offset (port);
206 *addr = val;
207 __ia64_mf_a();
210 weak_alias (_ioperm, ioperm);
211 weak_alias (_iopl, iopl);
212 weak_alias (_inb, inb);
213 weak_alias (_inw, inw);
214 weak_alias (_inl, inl);
215 weak_alias (_outb, outb);
216 weak_alias (_outw, outw);
217 weak_alias (_outl, outl);