Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libio / bwx.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/bwx.c,v 1.4.2.1 2000/12/11 01:03:20 obrien Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/mman.h>
31 #include <sys/fcntl.h>
32 #include <sys/sysctl.h>
33 #include <err.h>
34 #include <paths.h>
35 #include <machine/bwx.h>
36 #include <machine/sysarch.h>
37 #include <stdlib.h>
38 #include "io.h"
39
40 #define mb()    __asm__ __volatile__("mb"  : : : "memory")
41 #define wmb()   __asm__ __volatile__("wmb" : : : "memory")
42
43 static int              mem_fd;         /* file descriptor to /dev/mem */
44 static void            *bwx_int1_ports; /* mapped int1 io ports */
45 static void            *bwx_int2_ports; /* mapped int2 io ports */
46 static void            *bwx_int4_ports; /* mapped int4 io ports */
47 static u_int64_t        bwx_io_base;    /* physical address of ports */
48 static u_int64_t        bwx_mem_base;   /* physical address of bwx mem */
49
50 static void
51 bwx_init()
52 {
53     size_t len = sizeof(u_int64_t);
54     int error;
55
56     mem_fd = open(_PATH_MEM, O_RDWR);
57     if (mem_fd < 0)
58         err(1, _PATH_MEM);
59     bwx_int1_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
60     bwx_int2_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
61     bwx_int4_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
62
63     if ((error = sysctlbyname("hw.chipset.ports", &bwx_io_base, &len,
64                               0, 0)) < 0)
65         err(1, "hw.chipset.ports");
66     if ((error = sysctlbyname("hw.chipset.memory", &bwx_mem_base, &len,
67                               0, 0)) < 0)
68         err(1, "hw.chipset.memory");
69 }
70
71 static int
72 bwx_ioperm(u_int32_t from, u_int32_t num, int on)
73 {
74     u_int32_t start, end;
75
76     if (!bwx_int1_ports)
77         bwx_init();
78
79     if (!on)
80         return -1;              /* XXX can't unmap yet */
81    
82     start = trunc_page(from);
83     end = round_page(from + num);
84     
85     munmap(bwx_int1_ports + start, end-start);
86     munmap(bwx_int2_ports + start, end-start);
87     munmap(bwx_int4_ports + start, end-start);
88     mmap(bwx_int1_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
89          mem_fd, bwx_io_base + BWX_EV56_INT1 + start);
90     mmap(bwx_int2_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
91          mem_fd, bwx_io_base + BWX_EV56_INT2 + start);
92     mmap(bwx_int4_ports + start, end-start, PROT_READ|PROT_WRITE, MAP_SHARED,
93          mem_fd, bwx_io_base + BWX_EV56_INT4 + start);
94     return 0;
95 }
96
97 static u_int8_t
98 bwx_inb(u_int32_t port)
99 {
100     mb();
101     return ldbu((vm_offset_t)bwx_int1_ports + port);
102 }
103
104 static u_int16_t
105 bwx_inw(u_int32_t port)
106 {
107     mb();
108     return ldwu((vm_offset_t)bwx_int2_ports + port);
109 }
110
111 static u_int32_t
112 bwx_inl(u_int32_t port)
113 {
114     mb();
115     return ldl((vm_offset_t)bwx_int4_ports + port);
116 }
117
118 static void
119 bwx_outb(u_int32_t port, u_int8_t val)
120 {
121     stb((vm_offset_t)bwx_int1_ports + port, val);
122     wmb();
123 }
124
125 static void
126 bwx_outw(u_int32_t port, u_int16_t val)
127 {
128     stw((vm_offset_t)bwx_int2_ports + port, val);
129     wmb();
130 }
131
132 static void
133 bwx_outl(u_int32_t port, u_int32_t val)
134 {
135     stl((vm_offset_t)bwx_int4_ports + port, val);
136     wmb();
137 }
138
139 struct bwx_mem_handle {
140     void        *virt1;         /* int1 address in user address-space */
141     void        *virt2;         /* int2 address in user address-space */
142     void        *virt4;         /* int4 address in user address-space */
143 };
144
145 static void *
146 bwx_map_memory(u_int32_t address, u_int32_t size)
147 {
148     struct bwx_mem_handle *h;
149     h = malloc(sizeof(struct bwx_mem_handle));
150     if (!h) return 0;
151     h->virt1 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
152                     mem_fd, bwx_mem_base + BWX_EV56_INT1 + address);
153     if ((long) h->virt1 == -1) {
154         free(h);
155         return 0;
156     }
157     h->virt2 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
158                     mem_fd, bwx_mem_base + BWX_EV56_INT2 + address);
159     if ((long) h->virt2 == -1) {
160         munmap(h->virt1, size);
161         free(h);
162         return 0;
163     }
164     h->virt4 = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
165                     mem_fd, bwx_mem_base + BWX_EV56_INT4 + address);
166     if ((long) h->virt4 == -1) {
167         munmap(h->virt1, size);
168         munmap(h->virt2, size);
169         free(h);
170         return 0;
171     }
172     return h;
173 }
174
175 static void
176 bwx_unmap_memory(void *handle, u_int32_t size)
177 {
178     struct bwx_mem_handle *h = handle;
179     munmap(h->virt1, size);
180     munmap(h->virt2, size);
181     munmap(h->virt4, size);
182     free(h);
183 }
184
185 static u_int8_t
186 bwx_readb(void *handle, u_int32_t offset)
187 {
188     struct bwx_mem_handle *h = handle;
189     return ldbu((vm_offset_t)h->virt1 + offset);
190 }
191
192 static u_int16_t
193 bwx_readw(void *handle, u_int32_t offset)
194 {
195     struct bwx_mem_handle *h = handle;
196     return ldwu((vm_offset_t)h->virt2 + offset);
197 }
198
199 static u_int32_t
200 bwx_readl(void *handle, u_int32_t offset)
201 {
202     struct bwx_mem_handle *h = handle;
203     return ldl((vm_offset_t)h->virt4 + offset);
204 }
205
206 static void
207 bwx_writeb(void *handle, u_int32_t offset, u_int8_t val)
208 {
209     struct bwx_mem_handle *h = handle;
210     stb_nb((vm_offset_t)h->virt1 + offset, val);
211 }
212
213 static void
214 bwx_writew(void *handle, u_int32_t offset, u_int16_t val)
215 {
216     struct bwx_mem_handle *h = handle;
217     stw_nb((vm_offset_t)h->virt2 + offset, val);
218 }
219
220 static void
221 bwx_writel(void *handle, u_int32_t offset, u_int32_t val)
222 {
223     struct bwx_mem_handle *h = handle;
224     stl_nb((vm_offset_t)h->virt4 + offset, val);
225 }
226
227 struct io_ops bwx_io_ops = {
228     bwx_ioperm,
229     bwx_inb,
230     bwx_inw,
231     bwx_inl,
232     bwx_outb,
233     bwx_outw,
234     bwx_outl,
235     bwx_map_memory,
236     bwx_unmap_memory,
237     bwx_readb,
238     bwx_readw,
239     bwx_readl,
240     bwx_writeb,
241     bwx_writew,
242     bwx_writel,
243 };