Add Changelog ...
[glibc.git] / sysdeps / unix / sysv / linux / ia64 / ioperm.c
blob9753d56944f3f8f5b004e3b5d7e036b4f6f55745
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, see
17 <http://www.gnu.org/licenses/>. */
19 /* I/O access is restricted to ISA port space (ports 0..65535).
20 Modern devices hopefully are sane enough not to put any performance
21 critical registers in i/o space.
23 On the first call to ioperm() or iopl(), the entire (E)ISA port
24 space is mapped into the virtual address space at address io.base.
25 mprotect() calls are then used to enable/disable access to ports.
26 Per 4KB page, there are 4 I/O ports. */
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/mman.h>
38 #define MAX_PORT 0x10000
41 * Memory fence w/accept. This should never be used in code that is
42 * not IA-64 specific.
44 #define __ia64_mf_a() __asm__ __volatile__ ("mf.a" ::: "memory")
46 static struct
48 unsigned long int base;
49 unsigned long int page_mask;
51 io;
53 __inline__ unsigned long int
54 io_offset (unsigned long int port)
56 return ((port >> 2) << 12) | (port & 0xfff);
59 int
60 _ioperm (unsigned long int from, unsigned long int num, int turn_on)
62 #if 0
63 unsigned long int addr, len, base;
64 #endif
65 unsigned long int base;
66 int prot;
68 /* this test isn't as silly as it may look like; consider overflows! */
69 if (from >= MAX_PORT || from + num > MAX_PORT)
71 __set_errno (EINVAL);
72 return -1;
75 if (turn_on)
77 if (!io.base)
79 unsigned long phys_io_base, len;
80 int fd;
82 io.page_mask = ~(__getpagesize() - 1);
84 /* get I/O base physical address from ar.k0 as per PRM: */
85 __asm__ ("mov %0=ar.k0" : "=r"(phys_io_base));
87 /* The O_SYNC flag tells the /dev/mem driver to map the
88 memory uncached: */
89 fd = __open ("/dev/mem", O_RDWR | O_SYNC);
90 if (fd < 0)
91 return -1;
93 len = io_offset (MAX_PORT);
94 #if 1
95 /* see comment below */
96 base = (unsigned long int) __mmap (0, len, PROT_READ | PROT_WRITE, MAP_SHARED,
97 fd, phys_io_base);
98 #else
99 base = (unsigned long int) __mmap (0, len, PROT_NONE, MAP_SHARED,
100 fd, phys_io_base);
101 #endif
102 __close (fd);
104 if ((long) base == -1)
105 return -1;
107 io.base = base;
109 prot = PROT_READ | PROT_WRITE;
111 else
113 if (!io.base)
114 return 0; /* never was turned on... */
116 prot = PROT_NONE;
118 #if 0
119 /* We can't do mprotect because that would cause us to lose the
120 uncached flag that the /dev/mem driver turned on. A MAP_UNCACHED
121 flag seems so much cleaner... */
122 addr = (io.base + io_offset (from)) & io.page_mask;
123 len = io.base + io_offset (from + num) - addr;
124 return mprotect ((void *) addr, len, prot);
125 #else
126 return 0;
127 #endif
131 _iopl (unsigned int level)
133 if (level > 3)
135 __set_errno (EINVAL);
136 return -1;
138 if (level)
140 int retval = _ioperm (0, MAX_PORT, 1);
141 /* Match the documented error returns of the x86 version. */
142 if (retval < 0 && errno == EACCES)
143 __set_errno (EPERM);
144 return retval;
146 return 0;
149 unsigned int
150 _inb (unsigned long int port)
152 volatile unsigned char *addr = (void *) io.base + io_offset (port);
153 unsigned char ret;
155 ret = *addr;
156 __ia64_mf_a();
157 return ret;
160 unsigned int
161 _inw (unsigned long int port)
163 volatile unsigned short *addr = (void *) io.base + io_offset (port);
164 unsigned short ret;
166 ret = *addr;
167 __ia64_mf_a();
168 return ret;
171 unsigned int
172 _inl (unsigned long int port)
174 volatile unsigned int *addr = (void *) io.base + io_offset (port);
175 unsigned int ret;
177 ret = *addr;
178 __ia64_mf_a();
179 return ret;
182 void
183 _outb (unsigned char val, unsigned long int port)
185 volatile unsigned char *addr = (void *) io.base + io_offset (port);
187 *addr = val;
188 __ia64_mf_a();
191 void
192 _outw (unsigned short val, unsigned long int port)
194 volatile unsigned short *addr = (void *) io.base + io_offset (port);
196 *addr = val;
197 __ia64_mf_a();
200 void
201 _outl (unsigned int val, unsigned long int port)
203 volatile unsigned int *addr = (void *) io.base + io_offset (port);
205 *addr = val;
206 __ia64_mf_a();
209 weak_alias (_ioperm, ioperm);
210 weak_alias (_iopl, iopl);
211 weak_alias (_inb, inb);
212 weak_alias (_inw, inw);
213 weak_alias (_inl, inl);
214 weak_alias (_outb, outb);
215 weak_alias (_outw, outw);
216 weak_alias (_outl, outl);