Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libio / swiz.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/swiz.c,v 1.3.2.1 2000/12/11 01:03:20 obrien Exp $
27  * $DragonFly: src/lib/libio/Attic/swiz.c,v 1.2 2003/06/17 04:26:49 dillon Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/mman.h>
32 #include <sys/fcntl.h>
33 #include <sys/sysctl.h>
34 #include <err.h>
35 #include <paths.h>
36 #include <machine/swiz.h>
37 #include <machine/sysarch.h>
38 #include <stdlib.h>
39 #include "io.h"
40
41 #define mb()    __asm__ __volatile__("mb"  : : : "memory")
42 #define wmb()   __asm__ __volatile__("wmb" : : : "memory")
43
44 static int              mem_fd;         /* file descriptor to /dev/mem */
45 static void            *swiz_ports;     /* mapped io ports */
46 static u_int64_t        swiz_io_base;   /* physical address of ports */
47 static u_int64_t        swiz_mem_base;  /* physical address of sparse mem */
48 static u_int64_t        swiz_dense_base; /* physical address of dense mem */
49 static u_int64_t        swiz_hae_mask;  /* mask address bits for hae */
50 static u_int32_t        swiz_hae;       /* cache of current hae */
51
52 static void
53 swiz_init()
54 {
55
56     size_t len = sizeof(u_int64_t);
57     int error;
58
59     mem_fd = open(_PATH_MEM, O_RDWR);
60     if (mem_fd < 0)
61         err(1, _PATH_MEM);
62     swiz_ports = mmap(0, 1L<<32, PROT_READ, MAP_ANON, -1, 0);
63
64     if ((error = sysctlbyname("hw.chipset.ports", &swiz_io_base, &len,
65                               0, 0)) < 0)
66             err(1, "hw.chipset.ports");
67     if ((error = sysctlbyname("hw.chipset.memory", &swiz_mem_base, &len,
68                               0, 0)) < 0)
69             err(1, "hw.chipset.memory");
70     if ((error = sysctlbyname("hw.chipset.dense", &swiz_dense_base, &len,
71                               0, 0)) < 0)
72             err(1, "hw.chipset.memory");
73     if ((error = sysctlbyname("hw.chipset.hae_mask", &swiz_hae_mask, &len,
74                               0, 0)) < 0)
75             err(1, "hw.chipset.memory");
76
77 }
78
79 static int
80 swiz_ioperm(u_int32_t from, u_int32_t num, int on)
81 {
82     u_int64_t start, end;
83     void *addr;
84
85     if (!swiz_ports)
86         swiz_init();
87
88     if (!on)
89         return -1;              /* XXX can't unmap yet */
90
91     start = trunc_page(from << 5);
92     end = round_page((from + num) << 5);
93     addr = swiz_ports + start;
94     munmap(addr, end - start);
95     mmap(addr, end - start, PROT_READ|PROT_WRITE, MAP_SHARED,
96          mem_fd, swiz_io_base + start);
97     return 0;
98 }
99
100 static u_int8_t
101 swiz_inb(u_int32_t port)
102 {
103     mb();
104     return SPARSE_READ_BYTE(swiz_ports, port);
105 }
106
107 static u_int16_t
108 swiz_inw(u_int32_t port)
109 {
110     mb();
111     return SPARSE_READ_WORD(swiz_ports, port);
112 }
113
114 static u_int32_t
115 swiz_inl(u_int32_t port)
116 {
117     mb();
118     return SPARSE_READ_LONG(swiz_ports, port);
119 }
120
121 static void
122 swiz_outb(u_int32_t port, u_int8_t val)
123 {
124     SPARSE_WRITE_BYTE(swiz_ports, port, val);
125     wmb();
126 }
127
128 static void
129 swiz_outw(u_int32_t port, u_int16_t val)
130 {
131     SPARSE_WRITE_WORD(swiz_ports, port, val);
132     wmb();
133 }
134
135 static void
136 swiz_outl(u_int32_t port, u_int32_t val)
137 {
138     SPARSE_WRITE_LONG(swiz_ports, port, val);
139     wmb();
140 }
141
142 struct swiz_mem_handle {
143     u_int32_t   phys;           /* address in PCI address-space */
144     void        *virt;          /* address in user address-space */
145     u_int32_t   size;           /* size of mapped region */
146 };
147
148 static void *
149 swiz_map_memory(u_int32_t address, u_int32_t size)
150 {
151     struct swiz_mem_handle *h;
152     h = malloc(sizeof(struct swiz_mem_handle));
153     if (!h) return 0;
154     h->phys = address;
155     h->virt = mmap(0, size << 5, PROT_READ|PROT_WRITE, MAP_SHARED,
156                    mem_fd,
157                    swiz_mem_base + ((address & ~swiz_hae_mask) << 5));
158     if ((long) h->virt == -1) {
159         free(h);
160         return 0;
161     }
162     h->size = size << 5;
163     return h;
164 }
165
166 static void
167 swiz_unmap_memory(void *handle, u_int32_t size)
168 {
169     struct swiz_mem_handle *h = handle;
170     munmap(h->virt, h->size);
171     free(h);
172 }
173
174 static void
175 swiz_sethae(vm_offset_t phys)
176 {
177     u_int32_t hae = phys & swiz_hae_mask;
178     if (hae != swiz_hae) {
179         alpha_sethae(hae);
180         swiz_hae = hae;
181     }
182 }
183
184 static u_int8_t
185 swiz_readb(void *handle, u_int32_t offset)
186 {
187     struct swiz_mem_handle *h = handle;
188     swiz_sethae(h->phys + offset);
189     return SPARSE_READ_BYTE(h->virt, offset);
190 }
191
192 static u_int16_t
193 swiz_readw(void *handle, u_int32_t offset)
194 {
195     struct swiz_mem_handle *h = handle;
196     swiz_sethae(h->phys + offset);
197     return SPARSE_READ_WORD(h->virt, offset);
198 }
199
200 static u_int32_t
201 swiz_readl(void *handle, u_int32_t offset)
202 {
203     struct swiz_mem_handle *h = handle;
204     swiz_sethae(h->phys + offset);
205     return SPARSE_READ_LONG(h->virt, offset);
206 }
207
208 static void
209 swiz_writeb(void *handle, u_int32_t offset, u_int8_t val)
210 {
211     struct swiz_mem_handle *h = handle;
212     swiz_sethae(h->phys + offset);
213     SPARSE_WRITE_BYTE(h->virt, offset, val);
214 }
215
216 static void
217 swiz_writew(void *handle, u_int32_t offset, u_int16_t val)
218 {
219     struct swiz_mem_handle *h = handle;
220     swiz_sethae(h->phys + offset);
221     SPARSE_WRITE_WORD(h->virt, offset, val);
222 }
223
224 static void
225 swiz_writel(void *handle, u_int32_t offset, u_int32_t val)
226 {
227     struct swiz_mem_handle *h = handle;
228     swiz_sethae(h->phys + offset);
229     SPARSE_WRITE_LONG(h->virt, offset, val);
230 }
231
232 struct io_ops swiz_io_ops = {
233     swiz_ioperm,
234     swiz_inb,
235     swiz_inw,
236     swiz_inl,
237     swiz_outb,
238     swiz_outw,
239     swiz_outl,
240     swiz_map_memory,
241     swiz_unmap_memory,
242     swiz_readb,
243     swiz_readw,
244     swiz_readl,
245     swiz_writeb,
246     swiz_writew,
247     swiz_writel,
248 };