isa_machdep.c revision 1.2 1 1.2 fvdl /* $NetBSD: isa_machdep.c,v 1.2 2003/03/02 18:11:03 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.2 fvdl __KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.2 2003/03/02 18:11:03 fvdl Exp $");
80 1.1 fvdl
81 1.1 fvdl #define ISA_DMA_STATS
82 1.1 fvdl
83 1.1 fvdl #include <sys/param.h>
84 1.1 fvdl #include <sys/systm.h>
85 1.1 fvdl #include <sys/kernel.h>
86 1.1 fvdl #include <sys/syslog.h>
87 1.1 fvdl #include <sys/device.h>
88 1.1 fvdl #include <sys/malloc.h>
89 1.1 fvdl #include <sys/proc.h>
90 1.1 fvdl #include <sys/mbuf.h>
91 1.1 fvdl
92 1.1 fvdl #define _X86_BUS_DMA_PRIVATE
93 1.1 fvdl #include <machine/bus.h>
94 1.1 fvdl
95 1.1 fvdl #include <machine/pio.h>
96 1.1 fvdl #include <machine/cpufunc.h>
97 1.1 fvdl
98 1.1 fvdl #include <dev/isa/isareg.h>
99 1.1 fvdl #include <dev/isa/isavar.h>
100 1.1 fvdl
101 1.1 fvdl #include <uvm/uvm_extern.h>
102 1.1 fvdl
103 1.1 fvdl #include "ioapic.h"
104 1.1 fvdl
105 1.1 fvdl #if NIOAPIC > 0
106 1.1 fvdl #include <machine/i82093var.h>
107 1.1 fvdl #include <machine/mpbiosvar.h>
108 1.1 fvdl #endif
109 1.1 fvdl
110 1.2 fvdl #ifndef __x86_64__
111 1.1 fvdl #include "mca.h"
112 1.1 fvdl #if NMCA > 0
113 1.1 fvdl #include <machine/mca_machdep.h> /* for MCA_system */
114 1.1 fvdl #endif
115 1.1 fvdl #include "eisa.h" /* XXX */
116 1.2 fvdl #endif
117 1.1 fvdl
118 1.1 fvdl /*
119 1.1 fvdl * ISA can only DMA to 0-16M.
120 1.1 fvdl */
121 1.1 fvdl #define ISA_DMA_BOUNCE_THRESHOLD (16 * 1024 * 1024)
122 1.1 fvdl
123 1.1 fvdl extern paddr_t avail_end;
124 1.1 fvdl
125 1.1 fvdl #define IDTVEC(name) __CONCAT(X,name)
126 1.1 fvdl typedef void (vector) __P((void));
127 1.1 fvdl extern vector *IDTVEC(intr)[];
128 1.1 fvdl
129 1.1 fvdl /*
130 1.1 fvdl * Cookie used by ISA dma. A pointer to one of these it stashed in
131 1.1 fvdl * the DMA map.
132 1.1 fvdl */
133 1.1 fvdl struct x86_isa_dma_cookie {
134 1.1 fvdl int id_flags; /* flags; see below */
135 1.1 fvdl
136 1.1 fvdl /*
137 1.1 fvdl * Information about the original buffer used during
138 1.1 fvdl * DMA map syncs. Note that origibuflen is only used
139 1.1 fvdl * for ID_BUFTYPE_LINEAR.
140 1.1 fvdl */
141 1.1 fvdl void *id_origbuf; /* pointer to orig buffer if
142 1.1 fvdl bouncing */
143 1.1 fvdl bus_size_t id_origbuflen; /* ...and size */
144 1.1 fvdl int id_buftype; /* type of buffer */
145 1.1 fvdl
146 1.1 fvdl void *id_bouncebuf; /* pointer to the bounce buffer */
147 1.1 fvdl bus_size_t id_bouncebuflen; /* ...and size */
148 1.1 fvdl int id_nbouncesegs; /* number of valid bounce segs */
149 1.1 fvdl bus_dma_segment_t id_bouncesegs[0]; /* array of bounce buffer
150 1.1 fvdl physical memory segments */
151 1.1 fvdl };
152 1.1 fvdl
153 1.1 fvdl /* id_flags */
154 1.1 fvdl #define ID_MIGHT_NEED_BOUNCE 0x01 /* map could need bounce buffers */
155 1.1 fvdl #define ID_HAS_BOUNCE 0x02 /* map currently has bounce buffers */
156 1.1 fvdl #define ID_IS_BOUNCING 0x04 /* map is bouncing current xfer */
157 1.1 fvdl
158 1.1 fvdl /* id_buftype */
159 1.1 fvdl #define ID_BUFTYPE_INVALID 0
160 1.1 fvdl #define ID_BUFTYPE_LINEAR 1
161 1.1 fvdl #define ID_BUFTYPE_MBUF 2
162 1.1 fvdl #define ID_BUFTYPE_UIO 3
163 1.1 fvdl #define ID_BUFTYPE_RAW 4
164 1.1 fvdl
165 1.1 fvdl int _isa_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int,
166 1.1 fvdl bus_size_t, bus_size_t, int, bus_dmamap_t *));
167 1.1 fvdl void _isa_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
168 1.1 fvdl int _isa_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
169 1.1 fvdl bus_size_t, struct proc *, int));
170 1.1 fvdl int _isa_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
171 1.1 fvdl struct mbuf *, int));
172 1.1 fvdl int _isa_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
173 1.1 fvdl struct uio *, int));
174 1.1 fvdl int _isa_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
175 1.1 fvdl bus_dma_segment_t *, int, bus_size_t, int));
176 1.1 fvdl void _isa_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
177 1.1 fvdl void _isa_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
178 1.1 fvdl bus_addr_t, bus_size_t, int));
179 1.1 fvdl
180 1.1 fvdl int _isa_bus_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t,
181 1.1 fvdl bus_size_t, bus_dma_segment_t *, int, int *, int));
182 1.1 fvdl
183 1.1 fvdl int _isa_dma_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
184 1.1 fvdl bus_size_t, int));
185 1.1 fvdl void _isa_dma_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
186 1.1 fvdl
187 1.1 fvdl /*
188 1.1 fvdl * Entry points for ISA DMA. These are mostly wrappers around
189 1.1 fvdl * the generic functions that understand how to deal with bounce
190 1.1 fvdl * buffers, if necessary.
191 1.1 fvdl */
192 1.1 fvdl struct x86_bus_dma_tag isa_bus_dma_tag = {
193 1.1 fvdl ISA_DMA_BOUNCE_THRESHOLD,
194 1.1 fvdl _isa_bus_dmamap_create,
195 1.1 fvdl _isa_bus_dmamap_destroy,
196 1.1 fvdl _isa_bus_dmamap_load,
197 1.1 fvdl _isa_bus_dmamap_load_mbuf,
198 1.1 fvdl _isa_bus_dmamap_load_uio,
199 1.1 fvdl _isa_bus_dmamap_load_raw,
200 1.1 fvdl _isa_bus_dmamap_unload,
201 1.1 fvdl _isa_bus_dmamap_sync,
202 1.1 fvdl _isa_bus_dmamem_alloc,
203 1.1 fvdl _bus_dmamem_free,
204 1.1 fvdl _bus_dmamem_map,
205 1.1 fvdl _bus_dmamem_unmap,
206 1.1 fvdl _bus_dmamem_mmap,
207 1.1 fvdl };
208 1.1 fvdl
209 1.1 fvdl #define LEGAL_IRQ(x) ((x) >= 0 && (x) < NUM_LEGACY_IRQS && (x) != 2)
210 1.1 fvdl
211 1.1 fvdl int
212 1.1 fvdl isa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
213 1.1 fvdl {
214 1.1 fvdl int i, tmp, bestirq, count;
215 1.1 fvdl struct intrhand **p, *q;
216 1.1 fvdl struct intrsource *isp;
217 1.1 fvdl struct cpu_info *ci;
218 1.1 fvdl
219 1.1 fvdl if (type == IST_NONE)
220 1.1 fvdl panic("intr_alloc: bogus type");
221 1.1 fvdl
222 1.1 fvdl ci = &cpu_info_primary;
223 1.1 fvdl
224 1.1 fvdl bestirq = -1;
225 1.1 fvdl count = -1;
226 1.1 fvdl
227 1.1 fvdl /* some interrupts should never be dynamically allocated */
228 1.1 fvdl mask &= 0xdef8;
229 1.1 fvdl
230 1.1 fvdl /*
231 1.1 fvdl * XXX some interrupts will be used later (6 for fdc, 12 for pms).
232 1.1 fvdl * the right answer is to do "breadth-first" searching of devices.
233 1.1 fvdl */
234 1.1 fvdl mask &= 0xefbf;
235 1.1 fvdl
236 1.1 fvdl simple_lock(&ci->ci_slock);
237 1.1 fvdl
238 1.1 fvdl for (i = 0; i < NUM_LEGACY_IRQS; i++) {
239 1.1 fvdl if (LEGAL_IRQ(i) == 0 || (mask & (1<<i)) == 0)
240 1.1 fvdl continue;
241 1.1 fvdl isp = ci->ci_isources[i];
242 1.1 fvdl if (isp == NULL) {
243 1.1 fvdl /*
244 1.1 fvdl * if nothing's using the irq, just return it
245 1.1 fvdl */
246 1.1 fvdl *irq = i;
247 1.1 fvdl simple_unlock(&ci->ci_slock);
248 1.1 fvdl return (0);
249 1.1 fvdl }
250 1.1 fvdl
251 1.1 fvdl switch(isp->is_type) {
252 1.1 fvdl case IST_EDGE:
253 1.1 fvdl case IST_LEVEL:
254 1.1 fvdl if (type != isp->is_type)
255 1.1 fvdl continue;
256 1.1 fvdl /*
257 1.1 fvdl * if the irq is shareable, count the number of other
258 1.1 fvdl * handlers, and if it's smaller than the last irq like
259 1.1 fvdl * this, remember it
260 1.1 fvdl *
261 1.1 fvdl * XXX We should probably also consider the
262 1.1 fvdl * interrupt level and stick IPL_TTY with other
263 1.1 fvdl * IPL_TTY, etc.
264 1.1 fvdl */
265 1.1 fvdl for (p = &isp->is_handlers, tmp = 0; (q = *p) != NULL;
266 1.1 fvdl p = &q->ih_next, tmp++)
267 1.1 fvdl ;
268 1.1 fvdl if ((bestirq == -1) || (count > tmp)) {
269 1.1 fvdl bestirq = i;
270 1.1 fvdl count = tmp;
271 1.1 fvdl }
272 1.1 fvdl break;
273 1.1 fvdl
274 1.1 fvdl case IST_PULSE:
275 1.1 fvdl /* this just isn't shareable */
276 1.1 fvdl continue;
277 1.1 fvdl }
278 1.1 fvdl }
279 1.1 fvdl
280 1.1 fvdl simple_unlock(&ci->ci_slock);
281 1.1 fvdl
282 1.1 fvdl if (bestirq == -1)
283 1.1 fvdl return (1);
284 1.1 fvdl
285 1.1 fvdl *irq = bestirq;
286 1.1 fvdl
287 1.1 fvdl return (0);
288 1.1 fvdl }
289 1.1 fvdl
290 1.1 fvdl const struct evcnt *
291 1.1 fvdl isa_intr_evcnt(isa_chipset_tag_t ic, int irq)
292 1.1 fvdl {
293 1.1 fvdl
294 1.1 fvdl /* XXX for now, no evcnt parent reported */
295 1.1 fvdl return NULL;
296 1.1 fvdl }
297 1.1 fvdl
298 1.1 fvdl void *
299 1.1 fvdl isa_intr_establish(ic, irq, type, level, ih_fun, ih_arg)
300 1.1 fvdl isa_chipset_tag_t ic;
301 1.1 fvdl int irq;
302 1.1 fvdl int type;
303 1.1 fvdl int level;
304 1.1 fvdl int (*ih_fun) __P((void *));
305 1.1 fvdl void *ih_arg;
306 1.1 fvdl {
307 1.1 fvdl struct pic *pic;
308 1.1 fvdl int pin;
309 1.1 fvdl #if NIOAPIC > 0
310 1.1 fvdl int mpih;
311 1.1 fvdl #endif
312 1.1 fvdl
313 1.1 fvdl pin = irq;
314 1.1 fvdl pic = &i8259_pic;
315 1.1 fvdl
316 1.1 fvdl #if NIOAPIC > 0
317 1.1 fvdl if (mp_busses != NULL) {
318 1.1 fvdl if (intr_find_mpmapping(mp_isa_bus, irq, &mpih) == 0 ||
319 1.1 fvdl intr_find_mpmapping(mp_eisa_bus, irq, &mpih) == 0) {
320 1.1 fvdl if (!APIC_IRQ_ISLEGACY(mpih)) {
321 1.1 fvdl pin = APIC_IRQ_PIN(mpih);
322 1.1 fvdl pic = (struct pic *)
323 1.1 fvdl ioapic_find(APIC_IRQ_APIC(mpih));
324 1.1 fvdl if (pic == NULL) {
325 1.1 fvdl printf("isa_intr_establish: "
326 1.1 fvdl "unknown apic %d\n",
327 1.1 fvdl APIC_IRQ_APIC(mpih));
328 1.1 fvdl return NULL;
329 1.1 fvdl }
330 1.1 fvdl }
331 1.1 fvdl } else
332 1.1 fvdl printf("isa_intr_establish: no MP mapping found\n");
333 1.1 fvdl }
334 1.1 fvdl #endif
335 1.1 fvdl return intr_establish(irq, pic, pin, type, level, ih_fun, ih_arg);
336 1.1 fvdl }
337 1.1 fvdl
338 1.1 fvdl /*
339 1.1 fvdl * Deregister an interrupt handler.
340 1.1 fvdl */
341 1.1 fvdl void
342 1.1 fvdl isa_intr_disestablish(ic, arg)
343 1.1 fvdl isa_chipset_tag_t ic;
344 1.1 fvdl void *arg;
345 1.1 fvdl {
346 1.1 fvdl struct intrhand *ih = arg;
347 1.1 fvdl
348 1.1 fvdl if (!LEGAL_IRQ(ih->ih_pin))
349 1.1 fvdl panic("intr_disestablish: bogus irq");
350 1.1 fvdl
351 1.1 fvdl intr_disestablish(ih);
352 1.1 fvdl }
353 1.1 fvdl
354 1.1 fvdl void
355 1.1 fvdl isa_attach_hook(parent, self, iba)
356 1.1 fvdl struct device *parent, *self;
357 1.1 fvdl struct isabus_attach_args *iba;
358 1.1 fvdl {
359 1.1 fvdl extern struct x86_isa_chipset i386_isa_chipset;
360 1.1 fvdl extern int isa_has_been_seen;
361 1.1 fvdl
362 1.1 fvdl /*
363 1.1 fvdl * Notify others that might need to know that the ISA bus
364 1.1 fvdl * has now been attached.
365 1.1 fvdl */
366 1.1 fvdl if (isa_has_been_seen)
367 1.1 fvdl panic("isaattach: ISA bus already seen!");
368 1.1 fvdl isa_has_been_seen = 1;
369 1.1 fvdl
370 1.1 fvdl /*
371 1.1 fvdl * Since we can only have one ISA bus, we just use a single
372 1.1 fvdl * statically allocated ISA chipset structure. Pass it up
373 1.1 fvdl * now.
374 1.1 fvdl */
375 1.1 fvdl iba->iba_ic = &i386_isa_chipset;
376 1.1 fvdl }
377 1.1 fvdl
378 1.1 fvdl int
379 1.1 fvdl isa_mem_alloc(t, size, align, boundary, flags, addrp, bshp)
380 1.1 fvdl bus_space_tag_t t;
381 1.1 fvdl bus_size_t size, align;
382 1.1 fvdl bus_addr_t boundary;
383 1.1 fvdl int flags;
384 1.1 fvdl bus_addr_t *addrp;
385 1.1 fvdl bus_space_handle_t *bshp;
386 1.1 fvdl {
387 1.1 fvdl
388 1.1 fvdl /*
389 1.1 fvdl * Allocate physical address space in the ISA hole.
390 1.1 fvdl */
391 1.1 fvdl return (bus_space_alloc(t, IOM_BEGIN, IOM_END - 1, size, align,
392 1.1 fvdl boundary, flags, addrp, bshp));
393 1.1 fvdl }
394 1.1 fvdl
395 1.1 fvdl void
396 1.1 fvdl isa_mem_free(t, bsh, size)
397 1.1 fvdl bus_space_tag_t t;
398 1.1 fvdl bus_space_handle_t bsh;
399 1.1 fvdl bus_size_t size;
400 1.1 fvdl {
401 1.1 fvdl
402 1.1 fvdl bus_space_free(t, bsh, size);
403 1.1 fvdl }
404 1.1 fvdl
405 1.1 fvdl /**********************************************************************
406 1.1 fvdl * bus.h dma interface entry points
407 1.1 fvdl **********************************************************************/
408 1.1 fvdl
409 1.1 fvdl #ifdef ISA_DMA_STATS
410 1.1 fvdl #define STAT_INCR(v) (v)++
411 1.1 fvdl #define STAT_DECR(v) do { \
412 1.1 fvdl if ((v) == 0) \
413 1.1 fvdl printf("%s:%d -- Already 0!\n", __FILE__, __LINE__); \
414 1.1 fvdl else \
415 1.1 fvdl (v)--; \
416 1.1 fvdl } while (0)
417 1.1 fvdl u_long isa_dma_stats_loads;
418 1.1 fvdl u_long isa_dma_stats_bounces;
419 1.1 fvdl u_long isa_dma_stats_nbouncebufs;
420 1.1 fvdl #else
421 1.1 fvdl #define STAT_INCR(v)
422 1.1 fvdl #define STAT_DECR(v)
423 1.1 fvdl #endif
424 1.1 fvdl
425 1.1 fvdl /*
426 1.1 fvdl * Create an ISA DMA map.
427 1.1 fvdl */
428 1.1 fvdl int
429 1.1 fvdl _isa_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
430 1.1 fvdl bus_dma_tag_t t;
431 1.1 fvdl bus_size_t size;
432 1.1 fvdl int nsegments;
433 1.1 fvdl bus_size_t maxsegsz;
434 1.1 fvdl bus_size_t boundary;
435 1.1 fvdl int flags;
436 1.1 fvdl bus_dmamap_t *dmamp;
437 1.1 fvdl {
438 1.1 fvdl struct x86_isa_dma_cookie *cookie;
439 1.1 fvdl bus_dmamap_t map;
440 1.1 fvdl int error, cookieflags;
441 1.1 fvdl void *cookiestore;
442 1.1 fvdl size_t cookiesize;
443 1.1 fvdl
444 1.1 fvdl /* Call common function to create the basic map. */
445 1.1 fvdl error = _bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
446 1.1 fvdl flags, dmamp);
447 1.1 fvdl if (error)
448 1.1 fvdl return (error);
449 1.1 fvdl
450 1.1 fvdl map = *dmamp;
451 1.1 fvdl map->_dm_cookie = NULL;
452 1.1 fvdl
453 1.1 fvdl cookiesize = sizeof(struct x86_isa_dma_cookie);
454 1.1 fvdl
455 1.1 fvdl /*
456 1.1 fvdl * ISA only has 24-bits of address space. This means
457 1.1 fvdl * we can't DMA to pages over 16M. In order to DMA to
458 1.1 fvdl * arbitrary buffers, we use "bounce buffers" - pages
459 1.1 fvdl * in memory below the 16M boundary. On DMA reads,
460 1.1 fvdl * DMA happens to the bounce buffers, and is copied into
461 1.1 fvdl * the caller's buffer. On writes, data is copied into
462 1.1 fvdl * but bounce buffer, and the DMA happens from those
463 1.1 fvdl * pages. To software using the DMA mapping interface,
464 1.1 fvdl * this looks simply like a data cache.
465 1.1 fvdl *
466 1.1 fvdl * If we have more than 16M of RAM in the system, we may
467 1.1 fvdl * need bounce buffers. We check and remember that here.
468 1.1 fvdl *
469 1.1 fvdl * There are exceptions, however. VLB devices can do
470 1.1 fvdl * 32-bit DMA, and indicate that here.
471 1.1 fvdl *
472 1.1 fvdl * ...or, there is an opposite case. The most segments
473 1.1 fvdl * a transfer will require is (maxxfer / PAGE_SIZE) + 1. If
474 1.1 fvdl * the caller can't handle that many segments (e.g. the
475 1.1 fvdl * ISA DMA controller), we may have to bounce it as well.
476 1.1 fvdl */
477 1.1 fvdl if (avail_end <= t->_bounce_thresh ||
478 1.1 fvdl (flags & ISABUS_DMA_32BIT) != 0) {
479 1.1 fvdl /* Bouncing not necessary due to memory size. */
480 1.1 fvdl map->_dm_bounce_thresh = 0;
481 1.1 fvdl }
482 1.1 fvdl cookieflags = 0;
483 1.1 fvdl if (map->_dm_bounce_thresh != 0 ||
484 1.1 fvdl ((map->_dm_size / PAGE_SIZE) + 1) > map->_dm_segcnt) {
485 1.1 fvdl cookieflags |= ID_MIGHT_NEED_BOUNCE;
486 1.1 fvdl cookiesize += (sizeof(bus_dma_segment_t) * map->_dm_segcnt);
487 1.1 fvdl }
488 1.1 fvdl
489 1.1 fvdl /*
490 1.1 fvdl * Allocate our cookie.
491 1.1 fvdl */
492 1.1 fvdl if ((cookiestore = malloc(cookiesize, M_DMAMAP,
493 1.1 fvdl (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
494 1.1 fvdl error = ENOMEM;
495 1.1 fvdl goto out;
496 1.1 fvdl }
497 1.1 fvdl memset(cookiestore, 0, cookiesize);
498 1.1 fvdl cookie = (struct x86_isa_dma_cookie *)cookiestore;
499 1.1 fvdl cookie->id_flags = cookieflags;
500 1.1 fvdl map->_dm_cookie = cookie;
501 1.1 fvdl
502 1.1 fvdl if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
503 1.1 fvdl /*
504 1.1 fvdl * Allocate the bounce pages now if the caller
505 1.1 fvdl * wishes us to do so.
506 1.1 fvdl */
507 1.1 fvdl if ((flags & BUS_DMA_ALLOCNOW) == 0)
508 1.1 fvdl goto out;
509 1.1 fvdl
510 1.1 fvdl error = _isa_dma_alloc_bouncebuf(t, map, size, flags);
511 1.1 fvdl }
512 1.1 fvdl
513 1.1 fvdl out:
514 1.1 fvdl if (error) {
515 1.1 fvdl if (map->_dm_cookie != NULL)
516 1.1 fvdl free(map->_dm_cookie, M_DMAMAP);
517 1.1 fvdl _bus_dmamap_destroy(t, map);
518 1.1 fvdl }
519 1.1 fvdl return (error);
520 1.1 fvdl }
521 1.1 fvdl
522 1.1 fvdl /*
523 1.1 fvdl * Destroy an ISA DMA map.
524 1.1 fvdl */
525 1.1 fvdl void
526 1.1 fvdl _isa_bus_dmamap_destroy(t, map)
527 1.1 fvdl bus_dma_tag_t t;
528 1.1 fvdl bus_dmamap_t map;
529 1.1 fvdl {
530 1.1 fvdl struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
531 1.1 fvdl
532 1.1 fvdl /*
533 1.1 fvdl * Free any bounce pages this map might hold.
534 1.1 fvdl */
535 1.1 fvdl if (cookie->id_flags & ID_HAS_BOUNCE)
536 1.1 fvdl _isa_dma_free_bouncebuf(t, map);
537 1.1 fvdl
538 1.1 fvdl free(cookie, M_DMAMAP);
539 1.1 fvdl _bus_dmamap_destroy(t, map);
540 1.1 fvdl }
541 1.1 fvdl
542 1.1 fvdl /*
543 1.1 fvdl * Load an ISA DMA map with a linear buffer.
544 1.1 fvdl */
545 1.1 fvdl int
546 1.1 fvdl _isa_bus_dmamap_load(t, map, buf, buflen, p, flags)
547 1.1 fvdl bus_dma_tag_t t;
548 1.1 fvdl bus_dmamap_t map;
549 1.1 fvdl void *buf;
550 1.1 fvdl bus_size_t buflen;
551 1.1 fvdl struct proc *p;
552 1.1 fvdl int flags;
553 1.1 fvdl {
554 1.1 fvdl struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
555 1.1 fvdl int error;
556 1.1 fvdl
557 1.1 fvdl STAT_INCR(isa_dma_stats_loads);
558 1.1 fvdl
559 1.1 fvdl /*
560 1.1 fvdl * Make sure that on error condition we return "no valid mappings."
561 1.1 fvdl */
562 1.1 fvdl map->dm_mapsize = 0;
563 1.1 fvdl map->dm_nsegs = 0;
564 1.1 fvdl
565 1.1 fvdl /*
566 1.1 fvdl * Try to load the map the normal way. If this errors out,
567 1.1 fvdl * and we can bounce, we will.
568 1.1 fvdl */
569 1.1 fvdl error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
570 1.1 fvdl if (error == 0 ||
571 1.1 fvdl (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
572 1.1 fvdl return (error);
573 1.1 fvdl
574 1.1 fvdl /*
575 1.1 fvdl * First attempt failed; bounce it.
576 1.1 fvdl */
577 1.1 fvdl
578 1.1 fvdl STAT_INCR(isa_dma_stats_bounces);
579 1.1 fvdl
580 1.1 fvdl /*
581 1.1 fvdl * Allocate bounce pages, if necessary.
582 1.1 fvdl */
583 1.1 fvdl if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
584 1.1 fvdl error = _isa_dma_alloc_bouncebuf(t, map, buflen, flags);
585 1.1 fvdl if (error)
586 1.1 fvdl return (error);
587 1.1 fvdl }
588 1.1 fvdl
589 1.1 fvdl /*
590 1.1 fvdl * Cache a pointer to the caller's buffer and load the DMA map
591 1.1 fvdl * with the bounce buffer.
592 1.1 fvdl */
593 1.1 fvdl cookie->id_origbuf = buf;
594 1.1 fvdl cookie->id_origbuflen = buflen;
595 1.1 fvdl cookie->id_buftype = ID_BUFTYPE_LINEAR;
596 1.1 fvdl error = _bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
597 1.1 fvdl p, flags);
598 1.1 fvdl if (error) {
599 1.1 fvdl /*
600 1.1 fvdl * Free the bounce pages, unless our resources
601 1.1 fvdl * are reserved for our exclusive use.
602 1.1 fvdl */
603 1.1 fvdl if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
604 1.1 fvdl _isa_dma_free_bouncebuf(t, map);
605 1.1 fvdl return (error);
606 1.1 fvdl }
607 1.1 fvdl
608 1.1 fvdl /* ...so _isa_bus_dmamap_sync() knows we're bouncing */
609 1.1 fvdl cookie->id_flags |= ID_IS_BOUNCING;
610 1.1 fvdl return (0);
611 1.1 fvdl }
612 1.1 fvdl
613 1.1 fvdl /*
614 1.1 fvdl * Like _isa_bus_dmamap_load(), but for mbufs.
615 1.1 fvdl */
616 1.1 fvdl int
617 1.1 fvdl _isa_bus_dmamap_load_mbuf(t, map, m0, flags)
618 1.1 fvdl bus_dma_tag_t t;
619 1.1 fvdl bus_dmamap_t map;
620 1.1 fvdl struct mbuf *m0;
621 1.1 fvdl int flags;
622 1.1 fvdl {
623 1.1 fvdl struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
624 1.1 fvdl int error;
625 1.1 fvdl
626 1.1 fvdl /*
627 1.1 fvdl * Make sure on error condition we return "no valid mappings."
628 1.1 fvdl */
629 1.1 fvdl map->dm_mapsize = 0;
630 1.1 fvdl map->dm_nsegs = 0;
631 1.1 fvdl
632 1.1 fvdl #ifdef DIAGNOSTIC
633 1.1 fvdl if ((m0->m_flags & M_PKTHDR) == 0)
634 1.1 fvdl panic("_isa_bus_dmamap_load_mbuf: no packet header");
635 1.1 fvdl #endif
636 1.1 fvdl
637 1.1 fvdl if (m0->m_pkthdr.len > map->_dm_size)
638 1.1 fvdl return (EINVAL);
639 1.1 fvdl
640 1.1 fvdl /*
641 1.1 fvdl * Try to load the map the normal way. If this errors out,
642 1.1 fvdl * and we can bounce, we will.
643 1.1 fvdl */
644 1.1 fvdl error = _bus_dmamap_load_mbuf(t, map, m0, flags);
645 1.1 fvdl if (error == 0 ||
646 1.1 fvdl (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
647 1.1 fvdl return (error);
648 1.1 fvdl
649 1.1 fvdl /*
650 1.1 fvdl * First attempt failed; bounce it.
651 1.1 fvdl */
652 1.1 fvdl
653 1.1 fvdl STAT_INCR(isa_dma_stats_bounces);
654 1.1 fvdl
655 1.1 fvdl /*
656 1.1 fvdl * Allocate bounce pages, if necessary.
657 1.1 fvdl */
658 1.1 fvdl if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
659 1.1 fvdl error = _isa_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
660 1.1 fvdl flags);
661 1.1 fvdl if (error)
662 1.1 fvdl return (error);
663 1.1 fvdl }
664 1.1 fvdl
665 1.1 fvdl /*
666 1.1 fvdl * Cache a pointer to the caller's buffer and load the DMA map
667 1.1 fvdl * with the bounce buffer.
668 1.1 fvdl */
669 1.1 fvdl cookie->id_origbuf = m0;
670 1.1 fvdl cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
671 1.1 fvdl cookie->id_buftype = ID_BUFTYPE_MBUF;
672 1.1 fvdl error = _bus_dmamap_load(t, map, cookie->id_bouncebuf,
673 1.1 fvdl m0->m_pkthdr.len, NULL, flags);
674 1.1 fvdl if (error) {
675 1.1 fvdl /*
676 1.1 fvdl * Free the bounce pages, unless our resources
677 1.1 fvdl * are reserved for our exclusive use.
678 1.1 fvdl */
679 1.1 fvdl if ((map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
680 1.1 fvdl _isa_dma_free_bouncebuf(t, map);
681 1.1 fvdl return (error);
682 1.1 fvdl }
683 1.1 fvdl
684 1.1 fvdl /* ...so _isa_bus_dmamap_sync() knows we're bouncing */
685 1.1 fvdl cookie->id_flags |= ID_IS_BOUNCING;
686 1.1 fvdl return (0);
687 1.1 fvdl }
688 1.1 fvdl
689 1.1 fvdl /*
690 1.1 fvdl * Like _isa_bus_dmamap_load(), but for uios.
691 1.1 fvdl */
692 1.1 fvdl int
693 1.1 fvdl _isa_bus_dmamap_load_uio(t, map, uio, flags)
694 1.1 fvdl bus_dma_tag_t t;
695 1.1 fvdl bus_dmamap_t map;
696 1.1 fvdl struct uio *uio;
697 1.1 fvdl int flags;
698 1.1 fvdl {
699 1.1 fvdl
700 1.1 fvdl panic("_isa_bus_dmamap_load_uio: not implemented");
701 1.1 fvdl }
702 1.1 fvdl
703 1.1 fvdl /*
704 1.1 fvdl * Like _isa_bus_dmamap_load(), but for raw memory allocated with
705 1.1 fvdl * bus_dmamem_alloc().
706 1.1 fvdl */
707 1.1 fvdl int
708 1.1 fvdl _isa_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
709 1.1 fvdl bus_dma_tag_t t;
710 1.1 fvdl bus_dmamap_t map;
711 1.1 fvdl bus_dma_segment_t *segs;
712 1.1 fvdl int nsegs;
713 1.1 fvdl bus_size_t size;
714 1.1 fvdl int flags;
715 1.1 fvdl {
716 1.1 fvdl
717 1.1 fvdl panic("_isa_bus_dmamap_load_raw: not implemented");
718 1.1 fvdl }
719 1.1 fvdl
720 1.1 fvdl /*
721 1.1 fvdl * Unload an ISA DMA map.
722 1.1 fvdl */
723 1.1 fvdl void
724 1.1 fvdl _isa_bus_dmamap_unload(t, map)
725 1.1 fvdl bus_dma_tag_t t;
726 1.1 fvdl bus_dmamap_t map;
727 1.1 fvdl {
728 1.1 fvdl struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
729 1.1 fvdl
730 1.1 fvdl /*
731 1.1 fvdl * If we have bounce pages, free them, unless they're
732 1.1 fvdl * reserved for our exclusive use.
733 1.1 fvdl */
734 1.1 fvdl if ((cookie->id_flags & ID_HAS_BOUNCE) &&
735 1.1 fvdl (map->_dm_flags & BUS_DMA_ALLOCNOW) == 0)
736 1.1 fvdl _isa_dma_free_bouncebuf(t, map);
737 1.1 fvdl
738 1.1 fvdl cookie->id_flags &= ~ID_IS_BOUNCING;
739 1.1 fvdl cookie->id_buftype = ID_BUFTYPE_INVALID;
740 1.1 fvdl
741 1.1 fvdl /*
742 1.1 fvdl * Do the generic bits of the unload.
743 1.1 fvdl */
744 1.1 fvdl _bus_dmamap_unload(t, map);
745 1.1 fvdl }
746 1.1 fvdl
747 1.1 fvdl /*
748 1.1 fvdl * Synchronize an ISA DMA map.
749 1.1 fvdl */
750 1.1 fvdl void
751 1.1 fvdl _isa_bus_dmamap_sync(t, map, offset, len, ops)
752 1.1 fvdl bus_dma_tag_t t;
753 1.1 fvdl bus_dmamap_t map;
754 1.1 fvdl bus_addr_t offset;
755 1.1 fvdl bus_size_t len;
756 1.1 fvdl int ops;
757 1.1 fvdl {
758 1.1 fvdl struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
759 1.1 fvdl
760 1.1 fvdl /*
761 1.1 fvdl * Mixing PRE and POST operations is not allowed.
762 1.1 fvdl */
763 1.1 fvdl if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
764 1.1 fvdl (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
765 1.1 fvdl panic("_isa_bus_dmamap_sync: mix PRE and POST");
766 1.1 fvdl
767 1.1 fvdl #ifdef DIAGNOSTIC
768 1.1 fvdl if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
769 1.1 fvdl if (offset >= map->dm_mapsize)
770 1.1 fvdl panic("_isa_bus_dmamap_sync: bad offset");
771 1.1 fvdl if (len == 0 || (offset + len) > map->dm_mapsize)
772 1.1 fvdl panic("_isa_bus_dmamap_sync: bad length");
773 1.1 fvdl }
774 1.1 fvdl #endif
775 1.1 fvdl
776 1.1 fvdl /*
777 1.1 fvdl * If we're not bouncing, just return; nothing to do.
778 1.1 fvdl */
779 1.1 fvdl if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
780 1.1 fvdl return;
781 1.1 fvdl
782 1.1 fvdl switch (cookie->id_buftype) {
783 1.1 fvdl case ID_BUFTYPE_LINEAR:
784 1.1 fvdl /*
785 1.1 fvdl * Nothing to do for pre-read.
786 1.1 fvdl */
787 1.1 fvdl
788 1.1 fvdl if (ops & BUS_DMASYNC_PREWRITE) {
789 1.1 fvdl /*
790 1.1 fvdl * Copy the caller's buffer to the bounce buffer.
791 1.1 fvdl */
792 1.1 fvdl memcpy((char *)cookie->id_bouncebuf + offset,
793 1.1 fvdl (char *)cookie->id_origbuf + offset, len);
794 1.1 fvdl }
795 1.1 fvdl
796 1.1 fvdl if (ops & BUS_DMASYNC_POSTREAD) {
797 1.1 fvdl /*
798 1.1 fvdl * Copy the bounce buffer to the caller's buffer.
799 1.1 fvdl */
800 1.1 fvdl memcpy((char *)cookie->id_origbuf + offset,
801 1.1 fvdl (char *)cookie->id_bouncebuf + offset, len);
802 1.1 fvdl }
803 1.1 fvdl
804 1.1 fvdl /*
805 1.1 fvdl * Nothing to do for post-write.
806 1.1 fvdl */
807 1.1 fvdl break;
808 1.1 fvdl
809 1.1 fvdl case ID_BUFTYPE_MBUF:
810 1.1 fvdl {
811 1.1 fvdl struct mbuf *m, *m0 = cookie->id_origbuf;
812 1.1 fvdl bus_size_t minlen, moff;
813 1.1 fvdl
814 1.1 fvdl /*
815 1.1 fvdl * Nothing to do for pre-read.
816 1.1 fvdl */
817 1.1 fvdl
818 1.1 fvdl if (ops & BUS_DMASYNC_PREWRITE) {
819 1.1 fvdl /*
820 1.1 fvdl * Copy the caller's buffer to the bounce buffer.
821 1.1 fvdl */
822 1.1 fvdl m_copydata(m0, offset, len,
823 1.1 fvdl (char *)cookie->id_bouncebuf + offset);
824 1.1 fvdl }
825 1.1 fvdl
826 1.1 fvdl if (ops & BUS_DMASYNC_POSTREAD) {
827 1.1 fvdl /*
828 1.1 fvdl * Copy the bounce buffer to the caller's buffer.
829 1.1 fvdl */
830 1.1 fvdl for (moff = offset, m = m0; m != NULL && len != 0;
831 1.1 fvdl m = m->m_next) {
832 1.1 fvdl /* Find the beginning mbuf. */
833 1.1 fvdl if (moff >= m->m_len) {
834 1.1 fvdl moff -= m->m_len;
835 1.1 fvdl continue;
836 1.1 fvdl }
837 1.1 fvdl
838 1.1 fvdl /*
839 1.1 fvdl * Now at the first mbuf to sync; nail
840 1.1 fvdl * each one until we have exhausted the
841 1.1 fvdl * length.
842 1.1 fvdl */
843 1.1 fvdl minlen = len < m->m_len - moff ?
844 1.1 fvdl len : m->m_len - moff;
845 1.1 fvdl
846 1.1 fvdl memcpy(mtod(m, caddr_t) + moff,
847 1.1 fvdl (char *)cookie->id_bouncebuf + offset,
848 1.1 fvdl minlen);
849 1.1 fvdl
850 1.1 fvdl moff = 0;
851 1.1 fvdl len -= minlen;
852 1.1 fvdl offset += minlen;
853 1.1 fvdl }
854 1.1 fvdl }
855 1.1 fvdl
856 1.1 fvdl /*
857 1.1 fvdl * Nothing to do for post-write.
858 1.1 fvdl */
859 1.1 fvdl break;
860 1.1 fvdl }
861 1.1 fvdl
862 1.1 fvdl case ID_BUFTYPE_UIO:
863 1.1 fvdl panic("_isa_bus_dmamap_sync: ID_BUFTYPE_UIO");
864 1.1 fvdl break;
865 1.1 fvdl
866 1.1 fvdl case ID_BUFTYPE_RAW:
867 1.1 fvdl panic("_isa_bus_dmamap_sync: ID_BUFTYPE_RAW");
868 1.1 fvdl break;
869 1.1 fvdl
870 1.1 fvdl case ID_BUFTYPE_INVALID:
871 1.1 fvdl panic("_isa_bus_dmamap_sync: ID_BUFTYPE_INVALID");
872 1.1 fvdl break;
873 1.1 fvdl
874 1.1 fvdl default:
875 1.1 fvdl printf("unknown buffer type %d\n", cookie->id_buftype);
876 1.1 fvdl panic("_isa_bus_dmamap_sync");
877 1.1 fvdl }
878 1.1 fvdl }
879 1.1 fvdl
880 1.1 fvdl /*
881 1.1 fvdl * Allocate memory safe for ISA DMA.
882 1.1 fvdl */
883 1.1 fvdl int
884 1.1 fvdl _isa_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
885 1.1 fvdl bus_dma_tag_t t;
886 1.1 fvdl bus_size_t size, alignment, boundary;
887 1.1 fvdl bus_dma_segment_t *segs;
888 1.1 fvdl int nsegs;
889 1.1 fvdl int *rsegs;
890 1.1 fvdl int flags;
891 1.1 fvdl {
892 1.1 fvdl paddr_t high;
893 1.1 fvdl
894 1.1 fvdl if (avail_end > ISA_DMA_BOUNCE_THRESHOLD)
895 1.1 fvdl high = trunc_page(ISA_DMA_BOUNCE_THRESHOLD);
896 1.1 fvdl else
897 1.1 fvdl high = trunc_page(avail_end);
898 1.1 fvdl
899 1.1 fvdl return (_bus_dmamem_alloc_range(t, size, alignment, boundary,
900 1.1 fvdl segs, nsegs, rsegs, flags, 0, high));
901 1.1 fvdl }
902 1.1 fvdl
903 1.1 fvdl /**********************************************************************
904 1.1 fvdl * ISA DMA utility functions
905 1.1 fvdl **********************************************************************/
906 1.1 fvdl
907 1.1 fvdl int
908 1.1 fvdl _isa_dma_alloc_bouncebuf(t, map, size, flags)
909 1.1 fvdl bus_dma_tag_t t;
910 1.1 fvdl bus_dmamap_t map;
911 1.1 fvdl bus_size_t size;
912 1.1 fvdl int flags;
913 1.1 fvdl {
914 1.1 fvdl struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
915 1.1 fvdl int error = 0;
916 1.1 fvdl
917 1.1 fvdl cookie->id_bouncebuflen = round_page(size);
918 1.1 fvdl error = _isa_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
919 1.1 fvdl PAGE_SIZE, map->_dm_boundary, cookie->id_bouncesegs,
920 1.1 fvdl map->_dm_segcnt, &cookie->id_nbouncesegs, flags);
921 1.1 fvdl if (error)
922 1.1 fvdl goto out;
923 1.1 fvdl error = _bus_dmamem_map(t, cookie->id_bouncesegs,
924 1.1 fvdl cookie->id_nbouncesegs, cookie->id_bouncebuflen,
925 1.1 fvdl (caddr_t *)&cookie->id_bouncebuf, flags);
926 1.1 fvdl
927 1.1 fvdl out:
928 1.1 fvdl if (error) {
929 1.1 fvdl _bus_dmamem_free(t, cookie->id_bouncesegs,
930 1.1 fvdl cookie->id_nbouncesegs);
931 1.1 fvdl cookie->id_bouncebuflen = 0;
932 1.1 fvdl cookie->id_nbouncesegs = 0;
933 1.1 fvdl } else {
934 1.1 fvdl cookie->id_flags |= ID_HAS_BOUNCE;
935 1.1 fvdl STAT_INCR(isa_dma_stats_nbouncebufs);
936 1.1 fvdl }
937 1.1 fvdl
938 1.1 fvdl return (error);
939 1.1 fvdl }
940 1.1 fvdl
941 1.1 fvdl void
942 1.1 fvdl _isa_dma_free_bouncebuf(t, map)
943 1.1 fvdl bus_dma_tag_t t;
944 1.1 fvdl bus_dmamap_t map;
945 1.1 fvdl {
946 1.1 fvdl struct x86_isa_dma_cookie *cookie = map->_dm_cookie;
947 1.1 fvdl
948 1.1 fvdl STAT_DECR(isa_dma_stats_nbouncebufs);
949 1.1 fvdl
950 1.1 fvdl _bus_dmamem_unmap(t, cookie->id_bouncebuf,
951 1.1 fvdl cookie->id_bouncebuflen);
952 1.1 fvdl _bus_dmamem_free(t, cookie->id_bouncesegs,
953 1.1 fvdl cookie->id_nbouncesegs);
954 1.1 fvdl cookie->id_bouncebuflen = 0;
955 1.1 fvdl cookie->id_nbouncesegs = 0;
956 1.1 fvdl cookie->id_flags &= ~ID_HAS_BOUNCE;
957 1.1 fvdl }
958