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