Update to LGPL v2.1.
[jlayton/glibc.git] / sysdeps / unix / sysv / linux / alpha / ioperm.c
1 /* Copyright (C) 1992, 1996-1999, 2000 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by David Mosberger.
4
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.
9
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.
14
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.  */
19
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.
23
24    On the first call to ioperm, the entire (E)ISA port space is mapped
25    into the virtual address space at address io.base.  mprotect calls
26    are then used to enable/disable access to ports.  Per page, there
27    are PAGE_SIZE>>IO_SHIFT I/O ports (e.g., 256 ports on a Low Cost Alpha
28    based system using 8KB pages).
29
30    Keep in mind that this code should be able to run in a 32bit address
31    space.  It is therefore unreasonable to expect mmap'ing the entire
32    sparse address space would work (e.g., the Low Cost Alpha chip has an
33    I/O address space that's 512MB large!).  */
34
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdio.h>
38 #include <ctype.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include <sys/types.h>
44 #include <sys/mman.h>
45 #include <sys/io.h>
46
47 #include <sysdep.h>
48 #include <sys/syscall.h>
49
50 #define PATH_ALPHA_SYSTYPE      "/etc/alpha_systype"
51 #define PATH_CPUINFO            "/proc/cpuinfo"
52
53 #define MAX_PORT        0x10000
54 #define vip             volatile int *
55 #define vuip            volatile unsigned int *
56 #define vusp            volatile unsigned short *
57 #define vucp            volatile unsigned char *
58
59 #define JENSEN_IO_BASE          (0x300000000UL)
60 #define JENSEN_SPARSE_MEM       (0x200000000UL)
61
62 /* With respect to the I/O architecture, APECS and LCA are identical,
63    so the following defines apply to LCA as well.  */
64 #define APECS_IO_BASE           (0x1c0000000UL)
65 #define APECS_SPARSE_MEM        (0x200000000UL)
66 #define APECS_DENSE_MEM         (0x300000000UL)
67
68 /* The same holds for CIA and PYXIS, except for PYXIS we prefer BWX.  */
69 #define CIA_IO_BASE             (0x8580000000UL)
70 #define CIA_SPARSE_MEM          (0x8000000000UL)
71 #define CIA_DENSE_MEM           (0x8600000000UL)
72
73 #define PYXIS_IO_BASE           (0x8900000000UL)
74 #define PYXIS_DENSE_MEM         (0x8800000000UL)
75
76 /* SABLE is EV4, GAMMA is EV5 */
77 #define T2_IO_BASE              (0x3a0000000UL)
78 #define T2_SPARSE_MEM           (0x200000000UL)
79 #define T2_DENSE_MEM            (0x3c0000000UL)
80
81 #define GAMMA_IO_BASE           (0x83a0000000UL)
82 #define GAMMA_SPARSE_MEM        (0x8200000000UL)
83 #define GAMMA_DENSE_MEM         (0x83c0000000UL)
84
85 /* NOTE: these are hardwired to PCI bus 0 addresses!!! */
86 #define MCPCIA_IO_BASE          (0xf980000000UL)
87 #define MCPCIA_SPARSE_MEM       (0xf800000000UL)
88 #define MCPCIA_DENSE_MEM        (0xf900000000UL)
89
90 /* Tsunami and Irongate use the same offsets, at least for hose 0.  */
91 #define TSUNAMI_IO_BASE         (0x801fc000000UL)
92 #define TSUNAMI_DENSE_MEM       (0x80000000000UL)
93
94 /* Polaris has SPARSE space, but we prefer to use only DENSE
95    because of some idiosyncracies in actually using SPARSE.  */
96 #define POLARIS_IO_BASE         (0xf9fc000000UL)
97 #define POLARIS_DENSE_MEM       (0xf900000000UL)
98
99 typedef enum {
100   IOSYS_UNKNOWN, IOSYS_JENSEN, IOSYS_APECS, IOSYS_CIA, IOSYS_PYXIS, IOSYS_T2,
101   IOSYS_TSUNAMI, IOSYS_MCPCIA, IOSYS_GAMMA, IOSYS_POLARIS,
102   IOSYS_CPUDEP, IOSYS_PCIDEP
103 } iosys_t;
104
105 typedef enum {
106   IOSWIZZLE_JENSEN, IOSWIZZLE_SPARSE, IOSWIZZLE_DENSE
107 } ioswizzle_t;
108
109 static struct io_system {
110   unsigned long int bus_memory_base;
111   unsigned long int sparse_bus_mem_base;
112   unsigned long int bus_io_base;
113 } io_system[] = { /* NOTE! must match iosys_t enumeration */
114 /* UNKNOWN */   {0, 0, 0},
115 /* JENSEN */    {0, JENSEN_SPARSE_MEM, JENSEN_IO_BASE},
116 /* APECS */     {APECS_DENSE_MEM, APECS_SPARSE_MEM, APECS_IO_BASE},
117 /* CIA */       {CIA_DENSE_MEM, CIA_SPARSE_MEM, CIA_IO_BASE},
118 /* PYXIS */     {PYXIS_DENSE_MEM, 0, PYXIS_IO_BASE},
119 /* T2 */        {T2_DENSE_MEM, T2_SPARSE_MEM, T2_IO_BASE},
120 /* TSUNAMI */   {TSUNAMI_DENSE_MEM, 0, TSUNAMI_IO_BASE},
121 /* MCPCIA */    {MCPCIA_DENSE_MEM, MCPCIA_SPARSE_MEM, MCPCIA_IO_BASE},
122 /* GAMMA */     {GAMMA_DENSE_MEM, GAMMA_SPARSE_MEM, GAMMA_IO_BASE},
123 /* POLARIS */   {POLARIS_DENSE_MEM, 0, POLARIS_IO_BASE},
124 /* CPUDEP */    {0, 0, 0}, /* for platforms dependent on CPU type */
125 /* PCIDEP */    {0, 0, 0}, /* for platforms dependent on core logic */
126 };
127
128 static struct platform {
129   const char       *name;
130   iosys_t           io_sys;
131 } platform[] = {
132   {"Alcor",     IOSYS_CIA},
133   {"Avanti",    IOSYS_APECS},
134   {"Cabriolet", IOSYS_APECS},
135   {"EB164",     IOSYS_PCIDEP},
136   {"EB64+",     IOSYS_APECS},
137   {"EB66",      IOSYS_APECS},
138   {"EB66P",     IOSYS_APECS},
139   {"Jensen",    IOSYS_JENSEN},
140   {"Miata",     IOSYS_PYXIS},
141   {"Mikasa",    IOSYS_CPUDEP},
142   {"Nautilus",  IOSYS_TSUNAMI},
143   {"Noname",    IOSYS_APECS},
144   {"Noritake",  IOSYS_CPUDEP},
145   {"Rawhide",   IOSYS_MCPCIA},
146   {"Ruffian",   IOSYS_PYXIS},
147   {"Sable",     IOSYS_CPUDEP},
148   {"Takara",    IOSYS_CIA},
149   {"Tsunami",   IOSYS_TSUNAMI},
150   {"XL",        IOSYS_APECS},
151 };
152
153 struct ioswtch {
154   void          (*sethae)(unsigned long int addr);
155   void          (*outb)(unsigned char b, unsigned long int port);
156   void          (*outw)(unsigned short b, unsigned long int port);
157   void          (*outl)(unsigned int b, unsigned long int port);
158   unsigned int  (*inb)(unsigned long int port);
159   unsigned int  (*inw)(unsigned long int port);
160   unsigned int  (*inl)(unsigned long int port);
161 };
162
163 static struct {
164   unsigned long int hae_cache;
165   unsigned long int     base;
166   struct ioswtch *      swp;
167   unsigned long int     bus_memory_base;
168   unsigned long int     sparse_bus_memory_base;
169   unsigned long int     io_base;
170   ioswizzle_t           swiz;
171 } io;
172
173 static inline void
174 stb_mb(unsigned char val, unsigned long addr)
175 {
176   __asm__("stb %1,%0; mb" : "=m"(*(vucp)addr) : "r"(val));
177 }
178
179 static inline void
180 stw_mb(unsigned short val, unsigned long addr)
181 {
182   __asm__("stw %1,%0; mb" : "=m"(*(vusp)addr) : "r"(val));
183 }
184
185 static inline void
186 stl_mb(unsigned int val, unsigned long addr)
187 {
188   __asm__("stl %1,%0; mb" : "=m"(*(vip)addr) : "r"(val));
189 }
190
191 /* No need to examine error -- sethae never fails.  */
192 static inline void
193 __sethae(unsigned long value)
194 {
195   register unsigned long r16 __asm__("$16") = value;
196   register unsigned long r0 __asm__("$0") = __NR_sethae;
197   __asm__ __volatile__ ("callsys"
198                         : "=r"(r0)
199                         : "0"(r0), "r" (r16)
200                         : inline_syscall_clobbers, "$19");
201 }
202
203 extern long __pciconfig_iobase(enum __pciconfig_iobase_which __which,
204                                unsigned long int __bus,
205                                unsigned long int __dfn);
206
207 static inline unsigned long int
208 port_to_cpu_addr (unsigned long int port, ioswizzle_t ioswiz, int size)
209 {
210   if (ioswiz == IOSWIZZLE_SPARSE)
211     return io.base + (port << 5) + ((size - 1) << 3);
212   else if (ioswiz == IOSWIZZLE_DENSE)
213     return port + io.base;
214   else
215     return io.base + (port << 7) + ((size - 1) << 5);
216 }
217
218 static inline void
219 inline_sethae (unsigned long int addr, ioswizzle_t ioswiz)
220 {
221   if (ioswiz == IOSWIZZLE_SPARSE)
222     {
223       unsigned long int msb;
224
225       /* no need to set hae if msb is 0: */
226       msb = addr & 0xf8000000;
227       if (msb && msb != io.hae_cache)
228         {
229           io.hae_cache = msb;
230           __sethae (msb);
231         }
232     }
233   else if (ioswiz == IOSWIZZLE_JENSEN)
234     {
235       /* HAE on the Jensen is bits 31:25 shifted right.  */
236       addr >>= 25;
237       if (addr != io.hae_cache)
238         {
239           io.hae_cache = addr;
240           __sethae (addr);
241         }
242     }
243 }
244
245 static inline void
246 inline_outb (unsigned char b, unsigned long int port, ioswizzle_t ioswiz)
247 {
248   unsigned int w;
249   unsigned long int addr = port_to_cpu_addr (port, ioswiz, 1);
250
251   asm ("insbl %2,%1,%0" : "=r" (w) : "ri" (port & 0x3), "r" (b));
252   stl_mb(w, addr);
253 }
254
255
256 static inline void
257 inline_outw (unsigned short int b, unsigned long int port, ioswizzle_t ioswiz)
258 {
259   unsigned long w;
260   unsigned long int addr = port_to_cpu_addr (port, ioswiz, 2);
261
262   asm ("inswl %2,%1,%0" : "=r" (w) : "ri" (port & 0x3), "r" (b));
263   stl_mb(w, addr);
264 }
265
266
267 static inline void
268 inline_outl (unsigned int b, unsigned long int port, ioswizzle_t ioswiz)
269 {
270   unsigned long int addr = port_to_cpu_addr (port, ioswiz, 4);
271
272   stl_mb(b, addr);
273 }
274
275
276 static inline unsigned int
277 inline_inb (unsigned long int port, ioswizzle_t ioswiz)
278 {
279   unsigned long int addr = port_to_cpu_addr (port, ioswiz, 1);
280   int result;
281
282   result = *(vip) addr;
283   result >>= (port & 3) * 8;
284   return 0xffUL & result;
285 }
286
287
288 static inline unsigned int
289 inline_inw (unsigned long int port, ioswizzle_t ioswiz)
290 {
291   unsigned long int addr = port_to_cpu_addr (port, ioswiz, 2);
292   int result;
293
294   result = *(vip) addr;
295   result >>= (port & 3) * 8;
296   return 0xffffUL & result;
297 }
298
299
300 static inline unsigned int
301 inline_inl (unsigned long int port, ioswizzle_t ioswiz)
302 {
303   unsigned long int addr = port_to_cpu_addr (port, ioswiz, 4);
304
305   return *(vuip) addr;
306 }
307
308 /*
309  * Now define the inline functions for CPUs supporting byte/word insns,
310  * and whose core logic supports I/O space accesses utilizing them.
311  *
312  * These routines could be used by MIATA, for example, because it has
313  * and EV56 plus PYXIS, but it currently uses SPARSE anyway. This is
314  * also true of RX164 which used POLARIS, but we will choose to use
315  * these routines in that case instead of SPARSE.
316  *
317  * These routines are necessary for TSUNAMI/TYPHOON based platforms,
318  * which will have (at least) EV6.
319  */
320
321 static inline unsigned long int
322 dense_port_to_cpu_addr (unsigned long int port)
323 {
324   return port + io.base;
325 }
326
327 static inline void
328 inline_bwx_outb (unsigned char b, unsigned long int port)
329 {
330   unsigned long int addr = dense_port_to_cpu_addr (port);
331   stb_mb (b, addr);
332 }
333
334 static inline void
335 inline_bwx_outw (unsigned short int b, unsigned long int port)
336 {
337   unsigned long int addr = dense_port_to_cpu_addr (port);
338   stw_mb (b, addr);
339 }
340
341 static inline void
342 inline_bwx_outl (unsigned int b, unsigned long int port)
343 {
344   unsigned long int addr = dense_port_to_cpu_addr (port);
345   stl_mb (b, addr);
346 }
347
348 static inline unsigned int
349 inline_bwx_inb (unsigned long int port)
350 {
351   unsigned long int addr = dense_port_to_cpu_addr (port);
352   unsigned char r;
353
354   __asm__ ("ldbu %0,%1" : "=r"(r) : "m"(*(vucp)addr));
355   return r;
356 }
357
358 static inline unsigned int
359 inline_bwx_inw (unsigned long int port)
360 {
361   unsigned long int addr = dense_port_to_cpu_addr (port);
362   unsigned short r;
363
364   __asm__ ("ldwu %0,%1" : "=r"(r) : "m"(*(vusp)addr));
365   return r;
366 }
367
368 static inline unsigned int
369 inline_bwx_inl (unsigned long int port)
370 {
371   unsigned long int addr = dense_port_to_cpu_addr (port);
372
373   return *(vuip) addr;
374 }
375
376 /* macros to define routines with appropriate names and functions */
377
378 /* these do either SPARSE or JENSEN swizzle */
379
380 #define DCL_SETHAE(name, ioswiz)                        \
381 static void                                             \
382 name##_sethae (unsigned long int addr)                  \
383 {                                                       \
384   inline_sethae (addr, IOSWIZZLE_##ioswiz);             \
385 }
386
387 #define DCL_OUT(name, func, type, ioswiz)               \
388 static void                                             \
389 name##_##func (unsigned type b, unsigned long int addr) \
390 {                                                       \
391   inline_##func (b, addr, IOSWIZZLE_##ioswiz);          \
392 }
393
394 #define DCL_IN(name, func, ioswiz)                      \
395 static unsigned int                                     \
396 name##_##func (unsigned long int addr)                  \
397 {                                                       \
398   return inline_##func (addr, IOSWIZZLE_##ioswiz);      \
399 }
400
401 /* these do DENSE, so no swizzle is needed */
402
403 #define DCL_OUT_BWX(name, func, type)                   \
404 static void                                             \
405 name##_##func (unsigned type b, unsigned long int addr) \
406 {                                                       \
407   inline_bwx_##func (b, addr);                          \
408 }
409
410 #define DCL_IN_BWX(name, func)                          \
411 static unsigned int                                     \
412 name##_##func (unsigned long int addr)                  \
413 {                                                       \
414   return inline_bwx_##func (addr);                      \
415 }
416
417 /* now declare/define the necessary routines */
418
419 DCL_SETHAE(jensen, JENSEN)
420 DCL_OUT(jensen, outb, char,  JENSEN)
421 DCL_OUT(jensen, outw, short int, JENSEN)
422 DCL_OUT(jensen, outl, int,   JENSEN)
423 DCL_IN(jensen, inb, JENSEN)
424 DCL_IN(jensen, inw, JENSEN)
425 DCL_IN(jensen, inl, JENSEN)
426
427 DCL_SETHAE(sparse, SPARSE)
428 DCL_OUT(sparse, outb, char,  SPARSE)
429 DCL_OUT(sparse, outw, short int, SPARSE)
430 DCL_OUT(sparse, outl, int,   SPARSE)
431 DCL_IN(sparse, inb, SPARSE)
432 DCL_IN(sparse, inw, SPARSE)
433 DCL_IN(sparse, inl, SPARSE)
434
435 DCL_SETHAE(dense, DENSE)
436 DCL_OUT_BWX(dense, outb, char)
437 DCL_OUT_BWX(dense, outw, short int)
438 DCL_OUT_BWX(dense, outl, int)
439 DCL_IN_BWX(dense, inb)
440 DCL_IN_BWX(dense, inw)
441 DCL_IN_BWX(dense, inl)
442
443 /* define the "swizzle" switch */
444 static struct ioswtch ioswtch[] = {
445   {
446     jensen_sethae,
447     jensen_outb, jensen_outw, jensen_outl,
448     jensen_inb, jensen_inw, jensen_inl
449   },
450   {
451     sparse_sethae,
452     sparse_outb, sparse_outw, sparse_outl,
453     sparse_inb, sparse_inw, sparse_inl
454   },
455   {
456     dense_sethae,
457     dense_outb, dense_outw, dense_outl,
458     dense_inb, dense_inw, dense_inl
459   }
460 };
461
462 #undef DEBUG_IOPERM
463
464 /* Routine to process the /proc/cpuinfo information into the fields
465    that are required for correctly determining the platform parameters.  */
466
467 struct cpuinfo_data
468 {
469   char systype[256];            /* system type field */
470   char sysvari[256];            /* system variation field */
471   char cpumodel[256];           /* cpu model field */
472 };
473
474 static inline int
475 process_cpuinfo(struct cpuinfo_data *data)
476 {
477   int got_type, got_vari, got_model;
478   char dummy[256];
479   FILE * fp;
480   int n;
481
482   data->systype[0] = 0;
483   data->sysvari[0] = 0;
484   data->cpumodel[0] = 0;
485
486   /* If there's an /etc/alpha_systype link, we're intending to override
487      whatever's in /proc/cpuinfo.  */
488   n = __readlink (PATH_ALPHA_SYSTYPE, data->systype, 256 - 1);
489   if (n > 0)
490     {
491       data->systype[n] = '\0';
492       return 1;
493     }
494
495   fp = fopen (PATH_CPUINFO, "r");
496   if (!fp)
497     return 0;
498
499   got_type = got_vari = got_model = 0;
500
501   while (1)
502     {
503       if (fgets (dummy, 256, fp) == NULL)
504         break;
505       if (!got_type &&
506           sscanf (dummy, "system type : %256[^\n]\n", data->systype) == 1)
507         got_type = 1;
508       if (!got_vari &&
509           sscanf (dummy, "system variation : %256[^\n]\n", data->sysvari) == 1)
510         got_vari = 1;
511       if (!got_model &&
512           sscanf (dummy, "cpu model : %256[^\n]\n", data->cpumodel) == 1)
513         got_model = 1;
514     }
515
516   fclose (fp);
517
518 #ifdef DEBUG_IOPERM
519   fprintf(stderr, "system type: `%s'\n", data->systype);
520   fprintf(stderr, "system vari: `%s'\n", data->sysvari);
521   fprintf(stderr, "cpu model: `%s'\n", data->cpumodel);
522 #endif
523
524   return got_type + got_vari + got_model;
525 }
526
527
528 /*
529  * Initialize I/O system.
530  */
531 static int
532 init_iosys (void)
533 {
534   long addr;
535   int i, olderrno = errno;
536   struct cpuinfo_data data;
537
538   /* First try the pciconfig_iobase syscall added to 2.2.15 and 2.3.99.  */
539
540 #ifdef __NR_pciconfig_iobase
541   addr = __pciconfig_iobase (IOBASE_DENSE_MEM, 0, 0);
542   if (addr != -1)
543     {
544       ioswizzle_t io_swiz;
545
546       if (addr == 0)
547         {
548           /* Only Jensen doesn't have dense mem space.  */
549           io.sparse_bus_memory_base
550             = io_system[IOSYS_JENSEN].sparse_bus_mem_base;
551           io.io_base = io_system[IOSYS_JENSEN].bus_io_base;
552           io_swiz = IOSWIZZLE_JENSEN;
553         }
554       else
555         {
556           io.bus_memory_base = addr;
557
558           addr = __pciconfig_iobase (IOBASE_DENSE_IO, 0, 0);
559           if (addr != 0)
560             {
561               /* The X server uses _bus_base_sparse == 0 to know that
562                  BWX access are supported to dense mem space.  This is
563                  true of every system that supports dense io space, so
564                  never fill in io.sparse_bus_memory_base in this case.  */
565               io_swiz = IOSWIZZLE_DENSE;
566               io.io_base = addr;
567             }
568           else
569             {
570               io.sparse_bus_memory_base
571                 = __pciconfig_iobase (IOBASE_SPARSE_MEM, 0, 0);
572               io.io_base = __pciconfig_iobase (IOBASE_SPARSE_IO, 0, 0);
573               io_swiz = IOSWIZZLE_SPARSE;
574             }
575         }
576
577       io.swiz = io_swiz;
578       io.swp = &ioswtch[io_swiz];
579
580       return 0;
581     }
582 #endif
583
584   /* Second, collect the contents of /etc/alpha_systype or /proc/cpuinfo.  */
585
586   if (process_cpuinfo(&data) == 0)
587     {
588       /* This can happen if the format of /proc/cpuinfo changes.  */
589       fprintf (stderr,
590                "ioperm.init_iosys: Unable to determine system type.\n"
591                "\t(May need " PATH_ALPHA_SYSTYPE " symlink?)\n");
592       __set_errno (ENODEV);
593       return -1;
594     }
595
596   /* Translate systype name into i/o system.  */
597   for (i = 0; i < sizeof (platform) / sizeof (platform[0]); ++i)
598     {
599       if (strcmp (platform[i].name, data.systype) == 0)
600         {
601           iosys_t io_sys = platform[i].io_sys;
602
603           /* Some platforms can have either EV4 or EV5 CPUs.  */
604           if (io_sys == IOSYS_CPUDEP)
605             {
606               /* SABLE or MIKASA or NORITAKE so far.  */
607               if (strcmp (platform[i].name, "Sable") == 0)
608                 {
609                   if (strncmp (data.cpumodel, "EV4", 3) == 0)
610                     io_sys = IOSYS_T2;
611                   else if (strncmp (data.cpumodel, "EV5", 3) == 0)
612                     io_sys = IOSYS_GAMMA;
613                 }
614               else
615                 {
616                   /* This covers MIKASA/NORITAKE.  */
617                   if (strncmp (data.cpumodel, "EV4", 3) == 0)
618                     io_sys = IOSYS_APECS;
619                   else if (strncmp (data.cpumodel, "EV5", 3) == 0)
620                     io_sys = IOSYS_CIA;
621                 }
622               if (io_sys == IOSYS_CPUDEP)
623                 {
624                   /* This can happen if the format of /proc/cpuinfo changes.*/
625                   fprintf (stderr, "ioperm.init_iosys: Unable to determine"
626                            " CPU model.\n");
627                   __set_errno (ENODEV);
628                   return -1;
629                 }
630             }
631           /* Some platforms can have different core logic chipsets */
632           if (io_sys == IOSYS_PCIDEP)
633             {
634               /* EB164 so far */
635               if (strcmp (data.systype, "EB164") == 0)
636                 {
637                   if (strncmp (data.sysvari, "RX164", 5) == 0)
638                     io_sys = IOSYS_POLARIS;
639                   else if (strncmp (data.sysvari, "LX164", 5) == 0
640                            || strncmp (data.sysvari, "SX164", 5) == 0)
641                     io_sys = IOSYS_PYXIS;
642                   else
643                     io_sys = IOSYS_CIA;
644                 }
645               if (io_sys == IOSYS_PCIDEP)
646                 {
647                   /* This can happen if the format of /proc/cpuinfo changes.*/
648                   fprintf (stderr, "ioperm.init_iosys: Unable to determine"
649                            " core logic chipset.\n");
650                   __set_errno (ENODEV);
651                   return -1;
652                 }
653             }
654           io.bus_memory_base = io_system[io_sys].bus_memory_base;
655           io.sparse_bus_memory_base = io_system[io_sys].sparse_bus_mem_base;
656           io.io_base = io_system[io_sys].bus_io_base;
657
658           if (io_sys == IOSYS_JENSEN)
659             io.swiz = IOSWIZZLE_JENSEN;
660           else if (io_sys == IOSYS_TSUNAMI
661                    || io_sys == IOSYS_POLARIS
662                    || io_sys == IOSYS_PYXIS)
663             io.swiz = IOSWIZZLE_DENSE;
664           else
665             io.swiz = IOSWIZZLE_SPARSE;
666           io.swp = &ioswtch[io.swiz];
667
668           __set_errno (olderrno);
669           return 0;
670         }
671     }
672
673   __set_errno (ENODEV);
674   fprintf(stderr, "ioperm.init_iosys: Platform not recognized.\n"
675           "\t(May need " PATH_ALPHA_SYSTYPE " symlink?)\n");
676   return -1;
677 }
678
679
680 int
681 _ioperm (unsigned long int from, unsigned long int num, int turn_on)
682 {
683   unsigned long int addr, len, pagesize = __getpagesize();
684   int prot;
685
686   if (!io.swp && init_iosys() < 0)
687     {
688 #ifdef DEBUG_IOPERM
689       fprintf(stderr, "ioperm: init_iosys() failed (%m)\n");
690 #endif
691       return -1;
692     }
693
694   /* This test isn't as silly as it may look like; consider overflows! */
695   if (from >= MAX_PORT || from + num > MAX_PORT)
696     {
697       __set_errno (EINVAL);
698 #ifdef DEBUG_IOPERM
699       fprintf(stderr, "ioperm: from/num out of range\n");
700 #endif
701       return -1;
702     }
703
704 #ifdef DEBUG_IOPERM
705   fprintf(stderr, "ioperm: turn_on %d io.base %ld\n", turn_on, io.base);
706 #endif
707
708   if (turn_on)
709     {
710       if (!io.base)
711         {
712           int fd;
713
714           io.hae_cache = 0;
715           if (io.swiz != IOSWIZZLE_DENSE)
716             {
717               /* Synchronize with hw.  */
718               __sethae (0);
719             }
720
721           fd = __open ("/dev/mem", O_RDWR);
722           if (fd < 0)
723             {
724 #ifdef DEBUG_IOPERM
725               fprintf(stderr, "ioperm: /dev/mem open failed (%m)\n");
726 #endif
727               return -1;
728             }
729
730           addr = port_to_cpu_addr (0, io.swiz, 1);
731           len = port_to_cpu_addr (MAX_PORT, io.swiz, 1) - addr;
732           io.base =
733             (unsigned long int) __mmap (0, len, PROT_NONE, MAP_SHARED,
734                                         fd, io.io_base);
735           __close (fd);
736 #ifdef DEBUG_IOPERM
737           fprintf(stderr, "ioperm: mmap of len 0x%lx  returned 0x%lx\n",
738                   len, io.base);
739 #endif
740           if ((long) io.base == -1)
741             return -1;
742         }
743       prot = PROT_READ | PROT_WRITE;
744     }
745   else
746     {
747       if (!io.base)
748         return 0;       /* never was turned on... */
749
750       /* turnoff access to relevant pages: */
751       prot = PROT_NONE;
752     }
753   addr = port_to_cpu_addr (from, io.swiz, 1);
754   addr &= ~(pagesize - 1);
755   len = port_to_cpu_addr (from + num, io.swiz, 1) - addr;
756   return __mprotect ((void *) addr, len, prot);
757 }
758
759
760 int
761 _iopl (int level)
762 {
763   switch (level)
764     {
765     case 0:
766       return 0;
767
768     case 1: case 2: case 3:
769       return _ioperm (0, MAX_PORT, 1);
770
771     default:
772       __set_errno (EINVAL);
773       return -1;
774     }
775 }
776
777
778 void
779 _sethae (unsigned long int addr)
780 {
781   if (!io.swp && init_iosys () < 0)
782     return;
783
784   io.swp->sethae (addr);
785 }
786
787
788 void
789 _outb (unsigned char b, unsigned long int port)
790 {
791   if (port >= MAX_PORT)
792     return;
793
794   io.swp->outb (b, port);
795 }
796
797
798 void
799 _outw (unsigned short b, unsigned long int port)
800 {
801   if (port >= MAX_PORT)
802     return;
803
804   io.swp->outw (b, port);
805 }
806
807
808 void
809 _outl (unsigned int b, unsigned long int port)
810 {
811   if (port >= MAX_PORT)
812     return;
813
814   io.swp->outl (b, port);
815 }
816
817
818 unsigned int
819 _inb (unsigned long int port)
820 {
821   return io.swp->inb (port);
822 }
823
824
825 unsigned int
826 _inw (unsigned long int port)
827 {
828   return io.swp->inw (port);
829 }
830
831
832 unsigned int
833 _inl (unsigned long int port)
834 {
835   return io.swp->inl (port);
836 }
837
838
839 unsigned long int
840 _bus_base(void)
841 {
842   if (!io.swp && init_iosys () < 0)
843     return -1;
844   return io.bus_memory_base;
845 }
846
847 unsigned long int
848 _bus_base_sparse(void)
849 {
850   if (!io.swp && init_iosys () < 0)
851     return -1;
852   return io.sparse_bus_memory_base;
853 }
854
855 int
856 _hae_shift(void)
857 {
858   if (!io.swp && init_iosys () < 0)
859     return -1;
860   if (io.swiz == IOSWIZZLE_JENSEN)
861     return 7;
862   if (io.swiz == IOSWIZZLE_SPARSE)
863     return 5;
864   return 0;
865 }
866
867 weak_alias (_sethae, sethae);
868 weak_alias (_ioperm, ioperm);
869 weak_alias (_iopl, iopl);
870 weak_alias (_inb, inb);
871 weak_alias (_inw, inw);
872 weak_alias (_inl, inl);
873 weak_alias (_outb, outb);
874 weak_alias (_outw, outw);
875 weak_alias (_outl, outl);
876 weak_alias (_bus_base, bus_base);
877 weak_alias (_bus_base_sparse, bus_base_sparse);
878 weak_alias (_hae_shift, hae_shift);