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