isa_machdep.c revision 1.7 1 1.7 fvdl /* $NetBSD: isa_machdep.c,v 1.7 2003/05/07 21:33:58 fvdl Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*-
4 1.1 fvdl * Copyright (c) 1996, 1997, 1998 The NetBSD Foundation, Inc.
5 1.1 fvdl * All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * This code is derived from software contributed to The NetBSD Foundation
8 1.1 fvdl * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9 1.1 fvdl * Simulation Facility, NASA Ames Research Center.
10 1.1 fvdl *
11 1.1 fvdl * Redistribution and use in source and binary forms, with or without
12 1.1 fvdl * modification, are permitted provided that the following conditions
13 1.1 fvdl * are met:
14 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
15 1.1 fvdl * notice, this list of conditions and the following disclaimer.
16 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
18 1.1 fvdl * documentation and/or other materials provided with the distribution.
19 1.1 fvdl * 3. All advertising materials mentioning features or use of this software
20 1.1 fvdl * must display the following acknowledgement:
21 1.1 fvdl * This product includes software developed by the NetBSD
22 1.1 fvdl * Foundation, Inc. and its contributors.
23 1.1 fvdl * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 fvdl * contributors may be used to endorse or promote products derived
25 1.1 fvdl * from this software without specific prior written permission.
26 1.1 fvdl *
27 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 fvdl * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 fvdl * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 fvdl * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 fvdl * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 fvdl * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 fvdl * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 fvdl * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 fvdl * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 fvdl * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 fvdl * POSSIBILITY OF SUCH DAMAGE.
38 1.1 fvdl */
39 1.1 fvdl
40 1.1 fvdl /*-
41 1.1 fvdl * Copyright (c) 1991 The Regents of the University of California.
42 1.1 fvdl * All rights reserved.
43 1.1 fvdl *
44 1.1 fvdl * This code is derived from software contributed to Berkeley by
45 1.1 fvdl * William Jolitz.
46 1.1 fvdl *
47 1.1 fvdl * Redistribution and use in source and binary forms, with or without
48 1.1 fvdl * modification, are permitted provided that the following conditions
49 1.1 fvdl * are met:
50 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
51 1.1 fvdl * notice, this list of conditions and the following disclaimer.
52 1.1 fvdl * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 fvdl * notice, this list of conditions and the following disclaimer in the
54 1.1 fvdl * documentation and/or other materials provided with the distribution.
55 1.1 fvdl * 3. All advertising materials mentioning features or use of this software
56 1.1 fvdl * must display the following acknowledgement:
57 1.1 fvdl * This product includes software developed by the University of
58 1.1 fvdl * California, Berkeley and its contributors.
59 1.1 fvdl * 4. Neither the name of the University nor the names of its contributors
60 1.1 fvdl * may be used to endorse or promote products derived from this software
61 1.1 fvdl * without specific prior written permission.
62 1.1 fvdl *
63 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 fvdl * SUCH DAMAGE.
74 1.1 fvdl *
75 1.1 fvdl * @(#)isa.c 7.2 (Berkeley) 5/13/91
76 1.1 fvdl */
77 1.1 fvdl
78 1.1 fvdl #include <sys/cdefs.h>
79 1.7 fvdl __KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.7 2003/05/07 21:33:58 fvdl Exp $");
80 1.1 fvdl
81 1.1 fvdl #include <sys/param.h>
82 1.1 fvdl #include <sys/systm.h>
83 1.1 fvdl #include <sys/kernel.h>
84 1.1 fvdl #include <sys/syslog.h>
85 1.1 fvdl #include <sys/device.h>
86 1.1 fvdl #include <sys/malloc.h>
87 1.1 fvdl #include <sys/proc.h>
88 1.1 fvdl #include <sys/mbuf.h>
89 1.1 fvdl
90 1.1 fvdl #define _X86_BUS_DMA_PRIVATE
91 1.1 fvdl #include <machine/bus.h>
92 1.1 fvdl
93 1.1 fvdl #include <machine/pio.h>
94 1.1 fvdl #include <machine/cpufunc.h>
95 1.1 fvdl
96 1.1 fvdl #include <dev/isa/isareg.h>
97 1.1 fvdl #include <dev/isa/isavar.h>
98 1.1 fvdl
99 1.1 fvdl #include <uvm/uvm_extern.h>
100 1.1 fvdl
101 1.1 fvdl #include "ioapic.h"
102 1.1 fvdl
103 1.1 fvdl #if NIOAPIC > 0
104 1.1 fvdl #include <machine/i82093var.h>
105 1.1 fvdl #include <machine/mpbiosvar.h>
106 1.2 fvdl #endif
107 1.1 fvdl
108 1.7 fvdl static int _isa_dma_may_bounce(bus_dma_tag_t, bus_dmamap_t, int, int *);
109 1.1 fvdl
110 1.1 fvdl struct x86_bus_dma_tag isa_bus_dma_tag = {
111 1.7 fvdl ISA_DMA_BOUNCE_THRESHOLD, /* _bounce_thresh */
112 1.7 fvdl 0, /* _bounce_alloc_lo */
113 1.7 fvdl ISA_DMA_BOUNCE_THRESHOLD, /* _bounce_alloc_hi */
114 1.7 fvdl _isa_dma_may_bounce,
115 1.7 fvdl _bus_dmamap_create,
116 1.7 fvdl _bus_dmamap_destroy,
117 1.7 fvdl _bus_dmamap_load,
118 1.7 fvdl _bus_dmamap_load_mbuf,
119 1.7 fvdl _bus_dmamap_load_uio,
120 1.7 fvdl _bus_dmamap_load_raw,
121 1.7 fvdl _bus_dmamap_unload,
122 1.7 fvdl _bus_dmamap_sync,
123 1.7 fvdl _bus_dmamem_alloc,
124 1.1 fvdl _bus_dmamem_free,
125 1.1 fvdl _bus_dmamem_map,
126 1.1 fvdl _bus_dmamem_unmap,
127 1.1 fvdl _bus_dmamem_mmap,
128 1.1 fvdl };
129 1.1 fvdl
130 1.7 fvdl #define IDTVEC(name) __CONCAT(X,name)
131 1.7 fvdl typedef void (vector) __P((void));
132 1.7 fvdl extern vector *IDTVEC(intr)[];
133 1.7 fvdl
134 1.1 fvdl #define LEGAL_IRQ(x) ((x) >= 0 && (x) < NUM_LEGACY_IRQS && (x) != 2)
135 1.1 fvdl
136 1.1 fvdl int
137 1.1 fvdl isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
138 1.1 fvdl {
139 1.1 fvdl int i, tmp, bestirq, count;
140 1.1 fvdl struct intrhand **p, *q;
141 1.1 fvdl struct intrsource *isp;
142 1.1 fvdl struct cpu_info *ci;
143 1.1 fvdl
144 1.1 fvdl if (type == IST_NONE)
145 1.1 fvdl panic("intr_alloc: bogus type");
146 1.1 fvdl
147 1.1 fvdl ci = &cpu_info_primary;
148 1.1 fvdl
149 1.1 fvdl bestirq = -1;
150 1.1 fvdl count = -1;
151 1.1 fvdl
152 1.1 fvdl /* some interrupts should never be dynamically allocated */
153 1.1 fvdl mask &= 0xdef8;
154 1.1 fvdl
155 1.1 fvdl /*
156 1.1 fvdl * XXX some interrupts will be used later (6 for fdc, 12 for pms).
157 1.1 fvdl * the right answer is to do "breadth-first" searching of devices.
158 1.1 fvdl */
159 1.1 fvdl mask &= 0xefbf;
160 1.1 fvdl
161 1.1 fvdl simple_lock(&ci->ci_slock);
162 1.1 fvdl
163 1.1 fvdl for (i = 0; i < NUM_LEGACY_IRQS; i++) {
164 1.1 fvdl if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
165 1.1 fvdl continue;
166 1.1 fvdl isp = ci->ci_isources[i];
167 1.1 fvdl if (isp == NULL) {
168 1.1 fvdl /*
169 1.1 fvdl * if nothing's using the irq, just return it
170 1.1 fvdl */
171 1.1 fvdl *irq = i;
172 1.1 fvdl simple_unlock(&ci->ci_slock);
173 1.1 fvdl return (0);
174 1.1 fvdl }
175 1.1 fvdl
176 1.1 fvdl switch(isp->is_type) {
177 1.1 fvdl case IST_EDGE:
178 1.1 fvdl case IST_LEVEL:
179 1.1 fvdl if (type != isp->is_type)
180 1.1 fvdl continue;
181 1.1 fvdl /*
182 1.1 fvdl * if the irq is shareable, count the number of other
183 1.1 fvdl * handlers, and if it's smaller than the last irq like
184 1.1 fvdl * this, remember it
185 1.1 fvdl *
186 1.1 fvdl * XXX We should probably also consider the
187 1.1 fvdl * interrupt level and stick IPL_TTY with other
188 1.1 fvdl * IPL_TTY, etc.
189 1.1 fvdl */
190 1.1 fvdl for (p = &isp->is_handlers, tmp = 0; (q = *p) != NULL;
191 1.1 fvdl p = &q->ih_next, tmp++)
192 1.1 fvdl ;
193 1.1 fvdl if ((bestirq == -1) || (count > tmp)) {
194 1.1 fvdl bestirq = i;
195 1.1 fvdl count = tmp;
196 1.1 fvdl }
197 1.1 fvdl break;
198 1.1 fvdl
199 1.1 fvdl case IST_PULSE:
200 1.1 fvdl /* this just isn't shareable */
201 1.1 fvdl continue;
202 1.1 fvdl }
203 1.1 fvdl }
204 1.1 fvdl
205 1.1 fvdl simple_unlock(&ci->ci_slock);
206 1.1 fvdl
207 1.1 fvdl if (bestirq == -1)
208 1.1 fvdl return (1);
209 1.1 fvdl
210 1.1 fvdl *irq = bestirq;
211 1.1 fvdl
212 1.1 fvdl return (0);
213 1.1 fvdl }
214 1.1 fvdl
215 1.1 fvdl const struct evcnt *
216 1.1 fvdl isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
217 1.1 fvdl {
218 1.1 fvdl
219 1.1 fvdl /* XXX for now, no evcnt parent reported */
220 1.1 fvdl return NULL;
221 1.1 fvdl }
222 1.1 fvdl
223 1.1 fvdl void *
224 1.1 fvdl isa_intr_establish(ic, irq, type, level, ih_fun, ih_arg)
225 1.1 fvdl isa_chipset_tag_t ic;
226 1.1 fvdl int irq;
227 1.1 fvdl int type;
228 1.1 fvdl int level;
229 1.1 fvdl int (*ih_fun) __P((void *));
230 1.1 fvdl void *ih_arg;
231 1.1 fvdl {
232 1.1 fvdl struct pic *pic;
233 1.1 fvdl int pin;
234 1.1 fvdl #if NIOAPIC > 0
235 1.1 fvdl int mpih;
236 1.1 fvdl #endif
237 1.1 fvdl
238 1.1 fvdl pin = irq;
239 1.1 fvdl pic = &i8259_pic;
240 1.1 fvdl
241 1.1 fvdl #if NIOAPIC > 0
242 1.1 fvdl if (mp_busses != NULL) {
243 1.1 fvdl if (intr_find_mpmapping(mp_isa_bus, irq, &mpih) == 0 ||
244 1.1 fvdl intr_find_mpmapping(mp_eisa_bus, irq, &mpih) == 0) {
245 1.1 fvdl if (!APIC_IRQ_ISLEGACY(mpih)) {
246 1.1 fvdl pin = APIC_IRQ_PIN(mpih);
247 1.1 fvdl pic = (struct pic *)
248 1.1 fvdl ioapic_find(APIC_IRQ_APIC(mpih));
249 1.1 fvdl if (pic == NULL) {
250 1.1 fvdl printf("isa_intr_establish: "
251 1.1 fvdl "unknown apic %d\n",
252 1.1 fvdl APIC_IRQ_APIC(mpih));
253 1.1 fvdl return NULL;
254 1.1 fvdl }
255 1.1 fvdl }
256 1.1 fvdl } else
257 1.1 fvdl printf("isa_intr_establish: no MP mapping found\n");
258 1.1 fvdl }
259 1.1 fvdl #endif
260 1.1 fvdl return intr_establish(irq, pic, pin, type, level, ih_fun, ih_arg);
261 1.1 fvdl }
262 1.1 fvdl
263 1.1 fvdl /*
264 1.1 fvdl * Deregister an interrupt handler.
265 1.1 fvdl */
266 1.1 fvdl void
267 1.1 fvdl isa_intr_disestablish(ic, arg)
268 1.1 fvdl isa_chipset_tag_t ic;
269 1.1 fvdl void *arg;
270 1.1 fvdl {
271 1.1 fvdl struct intrhand *ih = arg;
272 1.1 fvdl
273 1.1 fvdl if (!LEGAL_IRQ(ih->ih_pin))
274 1.1 fvdl panic("intr_disestablish: bogus irq");
275 1.1 fvdl
276 1.1 fvdl intr_disestablish(ih);
277 1.1 fvdl }
278 1.1 fvdl
279 1.1 fvdl void
280 1.1 fvdl isa_attach_hook(parent, self, iba)
281 1.1 fvdl struct device *parent, *self;
282 1.1 fvdl struct isabus_attach_args *iba;
283 1.1 fvdl {
284 1.4 fvdl extern struct x86_isa_chipset x86_isa_chipset;
285 1.1 fvdl extern int isa_has_been_seen;
286 1.1 fvdl
287 1.1 fvdl /*
288 1.1 fvdl * Notify others that might need to know that the ISA bus
289 1.1 fvdl * has now been attached.
290 1.1 fvdl */
291 1.1 fvdl if (isa_has_been_seen)
292 1.1 fvdl panic("isaattach: ISA bus already seen!");
293 1.1 fvdl isa_has_been_seen = 1;
294 1.1 fvdl
295 1.1 fvdl /*
296 1.1 fvdl * Since we can only have one ISA bus, we just use a single
297 1.1 fvdl * statically allocated ISA chipset structure. Pass it up
298 1.1 fvdl * now.
299 1.1 fvdl */
300 1.4 fvdl iba->iba_ic = &x86_isa_chipset;
301 1.1 fvdl }
302 1.1 fvdl
303 1.1 fvdl int
304 1.1 fvdl isa_mem_alloc(t, size, align, boundary, flags, addrp, bshp)
305 1.1 fvdl bus_space_tag_t t;
306 1.1 fvdl bus_size_t size, align;
307 1.1 fvdl bus_addr_t boundary;
308 1.1 fvdl int flags;
309 1.1 fvdl bus_addr_t *addrp;
310 1.1 fvdl bus_space_handle_t *bshp;
311 1.1 fvdl {
312 1.1 fvdl
313 1.1 fvdl /*
314 1.1 fvdl * Allocate physical address space in the ISA hole.
315 1.1 fvdl */
316 1.1 fvdl return (bus_space_alloc(t, IOM_BEGIN, IOM_END - 1, size, align,
317 1.1 fvdl boundary, flags, addrp, bshp));
318 1.1 fvdl }
319 1.1 fvdl
320 1.1 fvdl void
321 1.1 fvdl isa_mem_free(t, bsh, size)
322 1.1 fvdl bus_space_tag_t t;
323 1.1 fvdl bus_space_handle_t bsh;
324 1.1 fvdl bus_size_t size;
325 1.1 fvdl {
326 1.1 fvdl
327 1.1 fvdl bus_space_free(t, bsh, size);
328 1.1 fvdl }
329 1.1 fvdl
330 1.1 fvdl /*
331 1.7 fvdl * ISA only has 24-bits of address space. This means
332 1.7 fvdl * we can't DMA to pages over 16M. In order to DMA to
333 1.7 fvdl * arbitrary buffers, we use "bounce buffers" - pages
334 1.7 fvdl * in memory below the 16M boundary. On DMA reads,
335 1.7 fvdl * DMA happens to the bounce buffers, and is copied into
336 1.7 fvdl * the caller's buffer. On writes, data is copied into
337 1.7 fvdl * but bounce buffer, and the DMA happens from those
338 1.7 fvdl * pages. To software using the DMA mapping interface,
339 1.7 fvdl * this looks simply like a data cache.
340 1.7 fvdl *
341 1.7 fvdl * If we have more than 16M of RAM in the system, we may
342 1.7 fvdl * need bounce buffers. We check and remember that here.
343 1.7 fvdl *
344 1.7 fvdl * There are exceptions, however. VLB devices can do
345 1.7 fvdl * 32-bit DMA, and indicate that here.
346 1.7 fvdl *
347 1.7 fvdl * ...or, there is an opposite case. The most segments
348 1.7 fvdl * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If
349 1.7 fvdl * the caller can't handle that many segments (e.g. the
350 1.7 fvdl * ISA DMA controller), we may have to bounce it as well.
351 1.7 fvdl */
352 1.7 fvdl static int
353 1.7 fvdl _isa_dma_may_bounce(bus_dma_tag_t t, bus_dmamap_t map, int flags,
354 1.7 fvdl int *cookieflagsp)
355 1.1 fvdl {
356 1.7 fvdl if ((flags & ISABUS_DMA_32BIT) != 0)
357 1.1 fvdl map->_dm_bounce_thresh = 0;
358 1.1 fvdl
359 1.7 fvdl if (((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt)
360 1.7 fvdl *cookieflagsp |= X86_DMA_ID_MIGHT_NEED_BOUNCE;
361 1.7 fvdl return 0;
362 1.1 fvdl }
363