Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libio / io.c
1 /*-
2  * Copyright (c) 1998 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/lib/libio/io.c,v 1.3 2000/02/12 14:57:01 dfr Exp $
27  */
28
29 #include <sys/types.h>
30 #include <sys/sysctl.h>
31 #include <err.h>
32 #include "io.h"
33
34 static struct io_ops *ops;
35
36 int
37 ioperm(unsigned long from, unsigned long num, int on)
38 {
39     int error;
40     int bwx;
41     size_t len = sizeof(bwx);
42
43     if ((error = sysctlbyname("hw.chipset.bwx", &bwx, &len, 0, 0)) < 0)
44         return error;
45     if (bwx)
46         ops = &bwx_io_ops;
47     else
48         ops = &swiz_io_ops;
49
50     return ops->ioperm(from, num, on);
51 }
52
53 u_int8_t
54 inb(u_int32_t port)
55 {
56     return ops->inb(port);
57 }
58
59 u_int16_t
60 inw(u_int32_t port)
61 {
62     return ops->inw(port);
63 }
64
65 u_int32_t
66 inl(u_int32_t port)
67 {
68     return ops->inl(port);
69 }
70
71 void
72 outb(u_int32_t port, u_int8_t val)
73 {
74     ops->outb(port, val);
75 }
76
77 void
78 outw(u_int32_t port, u_int16_t val)
79 {
80     ops->outw(port, val);
81 }
82
83 void
84 outl(u_int32_t port, u_int32_t val)
85 {
86     ops->outl(port, val);
87 }
88
89 void *
90 map_memory(u_int32_t address, u_int32_t size)
91 {
92     return ops->map_memory(address, size);
93 }
94
95 void
96 unmap_memory(void *handle, u_int32_t size)
97 {
98     ops->unmap_memory(handle, size);
99 }
100
101 u_int8_t
102 readb(void *handle, u_int32_t offset)
103 {
104     return ops->readb(handle, offset);
105 }
106
107 u_int16_t
108 readw(void *handle, u_int32_t offset)
109 {
110     return ops->readw(handle, offset);
111 }
112
113 u_int32_t
114 readl(void *handle, u_int32_t offset)
115 {
116     return ops->readl(handle, offset);
117 }
118
119 void
120 writeb(void *handle, u_int32_t offset, u_int8_t val)
121 {
122     ops->writeb(handle, offset, val);
123     __asm__ __volatile__ ("mb");
124 }
125
126 void
127 writew(void *handle, u_int32_t offset, u_int16_t val)
128 {
129     ops->writew(handle, offset, val);
130     __asm__ __volatile__ ("mb");
131 }
132
133 void
134 writel(void *handle, u_int32_t offset, u_int32_t val)
135 {
136     ops->writel(handle, offset, val);
137     __asm__ __volatile__ ("mb");
138 }
139
140 void
141 writeb_nb(void *handle, u_int32_t offset, u_int8_t val)
142 {
143     return ops->writeb(handle, offset, val);
144 }
145
146 void
147 writew_nb(void *handle, u_int32_t offset, u_int16_t val)
148 {
149     return ops->writew(handle, offset, val);
150 }
151
152 void
153 writel_nb(void *handle, u_int32_t offset, u_int32_t val)
154 {
155     return ops->writel(handle, offset, val);
156 }
157
158 u_int64_t
159 dense_base(void)
160 {
161     static u_int64_t base = 0;
162
163     if (base == 0) {
164         size_t len = sizeof(base);
165         int error;
166         if ((error = sysctlbyname("hw.chipset.dense", &base, &len,
167                                   0, 0)) < 0)
168             err(1, "hw.chipset.dense");
169     }
170
171     return base;
172 }