intio.c revision 1.1.2.3 1 1.1.2.3 minoura /* $NetBSD: intio.c,v 1.1.2.3 1999/01/30 15:07:40 minoura Exp $ */
2 1.1.2.1 minoura
3 1.1.2.1 minoura /*
4 1.1.2.1 minoura *
5 1.1.2.1 minoura * Copyright (c) 1998 NetBSD Foundation, Inc.
6 1.1.2.1 minoura * All rights reserved.
7 1.1.2.1 minoura *
8 1.1.2.1 minoura * Redistribution and use in source and binary forms, with or without
9 1.1.2.1 minoura * modification, are permitted provided that the following conditions
10 1.1.2.1 minoura * are met:
11 1.1.2.1 minoura * 1. Redistributions of source code must retain the above copyright
12 1.1.2.1 minoura * notice, this list of conditions and the following disclaimer.
13 1.1.2.1 minoura * 2. Redistributions in binary form must reproduce the above copyright
14 1.1.2.1 minoura * notice, this list of conditions and the following disclaimer in the
15 1.1.2.1 minoura * documentation and/or other materials provided with the distribution.
16 1.1.2.1 minoura * 3. All advertising materials mentioning features or use of this software
17 1.1.2.1 minoura * must display the following acknowledgement:
18 1.1.2.1 minoura * This product includes software developed by Charles D. Cranor and
19 1.1.2.1 minoura * Washington University.
20 1.1.2.1 minoura * 4. The name of the author may not be used to endorse or promote products
21 1.1.2.1 minoura * derived from this software without specific prior written permission.
22 1.1.2.1 minoura *
23 1.1.2.1 minoura * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.1.2.1 minoura * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.1.2.1 minoura * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.1.2.1 minoura * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.1.2.1 minoura * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.1.2.1 minoura * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.1.2.1 minoura * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1.2.1 minoura * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.1.2.1 minoura * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.1.2.1 minoura * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.1.2.1 minoura */
34 1.1.2.1 minoura
35 1.1.2.1 minoura /*
36 1.1.2.1 minoura * NetBSD/x68k internal I/O virtual bus.
37 1.1.2.1 minoura */
38 1.1.2.1 minoura
39 1.1.2.1 minoura #include <sys/param.h>
40 1.1.2.1 minoura #include <sys/systm.h>
41 1.1.2.1 minoura #include <sys/device.h>
42 1.1.2.1 minoura #include <sys/malloc.h>
43 1.1.2.3 minoura #include <sys/mbuf.h>
44 1.1.2.1 minoura #include <sys/extent.h>
45 1.1.2.3 minoura #include <vm/vm.h>
46 1.1.2.1 minoura
47 1.1.2.1 minoura #include <machine/bus.h>
48 1.1.2.1 minoura #include <machine/cpu.h>
49 1.1.2.1 minoura #include <machine/frame.h>
50 1.1.2.1 minoura
51 1.1.2.1 minoura #include <arch/x68k/dev/intiovar.h>
52 1.1.2.1 minoura #include <arch/x68k/dev/mfp.h>
53 1.1.2.1 minoura
54 1.1.2.1 minoura
55 1.1.2.1 minoura /*
56 1.1.2.1 minoura * bus_space(9) interface
57 1.1.2.1 minoura */
58 1.1.2.1 minoura static int intio_bus_space_map __P((bus_space_tag_t, bus_addr_t, bus_size_t, int, bus_space_handle_t *));
59 1.1.2.1 minoura static void intio_bus_space_unmap __P((bus_space_tag_t, bus_space_handle_t, bus_size_t));
60 1.1.2.1 minoura static int intio_bus_space_subregion __P((bus_space_tag_t, bus_space_handle_t, bus_size_t, bus_size_t, bus_space_handle_t *));
61 1.1.2.1 minoura
62 1.1.2.1 minoura static struct x68k_bus_space intio_bus = {
63 1.1.2.1 minoura #if 0
64 1.1.2.1 minoura X68K_INTIO_BUS,
65 1.1.2.1 minoura #endif
66 1.1.2.1 minoura intio_bus_space_map, intio_bus_space_unmap, intio_bus_space_subregion,
67 1.1.2.1 minoura x68k_bus_space_alloc, x68k_bus_space_free,
68 1.1.2.1 minoura #if 0
69 1.1.2.1 minoura x68k_bus_space_barrier,
70 1.1.2.1 minoura #endif
71 1.1.2.1 minoura x68k_bus_space_read_1, x68k_bus_space_read_2, x68k_bus_space_read_4,
72 1.1.2.1 minoura x68k_bus_space_read_multi_1, x68k_bus_space_read_multi_2,
73 1.1.2.1 minoura x68k_bus_space_read_multi_4,
74 1.1.2.1 minoura x68k_bus_space_read_region_1, x68k_bus_space_read_region_2,
75 1.1.2.1 minoura x68k_bus_space_read_region_4,
76 1.1.2.1 minoura
77 1.1.2.1 minoura x68k_bus_space_write_1, x68k_bus_space_write_2, x68k_bus_space_write_4,
78 1.1.2.1 minoura x68k_bus_space_write_multi_1, x68k_bus_space_write_multi_2,
79 1.1.2.1 minoura x68k_bus_space_write_multi_4,
80 1.1.2.1 minoura x68k_bus_space_write_region_1, x68k_bus_space_write_region_2,
81 1.1.2.1 minoura x68k_bus_space_write_region_4,
82 1.1.2.1 minoura
83 1.1.2.1 minoura x68k_bus_space_set_region_1, x68k_bus_space_set_region_2,
84 1.1.2.1 minoura x68k_bus_space_set_region_4,
85 1.1.2.1 minoura x68k_bus_space_copy_region_1, x68k_bus_space_copy_region_2,
86 1.1.2.1 minoura x68k_bus_space_copy_region_4,
87 1.1.2.1 minoura
88 1.1.2.1 minoura 0
89 1.1.2.1 minoura };
90 1.1.2.1 minoura
91 1.1.2.3 minoura /*
92 1.1.2.3 minoura * bus_dma(9) interface
93 1.1.2.3 minoura */
94 1.1.2.3 minoura #define INTIO_DMA_BOUNCE_THRESHOLD (16 * 1024 * 1024)
95 1.1.2.3 minoura int _intio_bus_dmamap_create __P((bus_dma_tag_t, bus_size_t, int,
96 1.1.2.3 minoura bus_size_t, bus_size_t, int, bus_dmamap_t *));
97 1.1.2.3 minoura void _intio_bus_dmamap_destroy __P((bus_dma_tag_t, bus_dmamap_t));
98 1.1.2.3 minoura int _intio_bus_dmamap_load __P((bus_dma_tag_t, bus_dmamap_t, void *,
99 1.1.2.3 minoura bus_size_t, struct proc *, int));
100 1.1.2.3 minoura int _intio_bus_dmamap_load_mbuf __P((bus_dma_tag_t, bus_dmamap_t,
101 1.1.2.3 minoura struct mbuf *, int));
102 1.1.2.3 minoura int _intio_bus_dmamap_load_uio __P((bus_dma_tag_t, bus_dmamap_t,
103 1.1.2.3 minoura struct uio *, int));
104 1.1.2.3 minoura int _intio_bus_dmamap_load_raw __P((bus_dma_tag_t, bus_dmamap_t,
105 1.1.2.3 minoura bus_dma_segment_t *, int, bus_size_t, int));
106 1.1.2.3 minoura void _intio_bus_dmamap_unload __P((bus_dma_tag_t, bus_dmamap_t));
107 1.1.2.3 minoura void _intio_bus_dmamap_sync __P((bus_dma_tag_t, bus_dmamap_t,
108 1.1.2.3 minoura bus_addr_t, bus_size_t, int));
109 1.1.2.3 minoura
110 1.1.2.3 minoura int _intio_bus_dmamem_alloc __P((bus_dma_tag_t, bus_size_t, bus_size_t,
111 1.1.2.3 minoura bus_size_t, bus_dma_segment_t *, int, int *, int));
112 1.1.2.3 minoura
113 1.1.2.3 minoura int _intio_dma_alloc_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t,
114 1.1.2.3 minoura bus_size_t, int));
115 1.1.2.3 minoura void _intio_dma_free_bouncebuf __P((bus_dma_tag_t, bus_dmamap_t));
116 1.1.2.3 minoura
117 1.1.2.3 minoura struct x68k_bus_dma intio_bus_dma = {
118 1.1.2.3 minoura INTIO_DMA_BOUNCE_THRESHOLD,
119 1.1.2.3 minoura _intio_bus_dmamap_create,
120 1.1.2.3 minoura _intio_bus_dmamap_destroy,
121 1.1.2.3 minoura _intio_bus_dmamap_load,
122 1.1.2.3 minoura _intio_bus_dmamap_load_mbuf,
123 1.1.2.3 minoura _intio_bus_dmamap_load_uio,
124 1.1.2.3 minoura _intio_bus_dmamap_load_raw,
125 1.1.2.3 minoura _intio_bus_dmamap_unload,
126 1.1.2.3 minoura _intio_bus_dmamap_sync,
127 1.1.2.3 minoura _intio_bus_dmamem_alloc,
128 1.1.2.3 minoura x68k_bus_dmamem_free,
129 1.1.2.3 minoura x68k_bus_dmamem_map,
130 1.1.2.3 minoura x68k_bus_dmamem_unmap,
131 1.1.2.3 minoura x68k_bus_dmamem_mmap,
132 1.1.2.3 minoura };
133 1.1.2.1 minoura
134 1.1.2.1 minoura /*
135 1.1.2.1 minoura * autoconf stuff
136 1.1.2.1 minoura */
137 1.1.2.1 minoura static int intio_match __P((struct device *, struct cfdata *, void *));
138 1.1.2.1 minoura static void intio_attach __P((struct device *, struct device *, void *));
139 1.1.2.1 minoura static int intio_search __P((struct device *, struct cfdata *cf, void *));
140 1.1.2.1 minoura static int intio_print __P((void *, const char *));
141 1.1.2.1 minoura static void intio_alloc_system_ports __P((struct intio_softc*));
142 1.1.2.1 minoura
143 1.1.2.1 minoura struct cfattach intio_ca = {
144 1.1.2.1 minoura sizeof(struct intio_softc), intio_match, intio_attach
145 1.1.2.1 minoura };
146 1.1.2.1 minoura
147 1.1.2.1 minoura static struct intio_interrupt_vector {
148 1.1.2.1 minoura intio_intr_handler_t iiv_handler;
149 1.1.2.1 minoura void *iiv_arg;
150 1.1.2.1 minoura } iiv[256] = {0,};
151 1.1.2.1 minoura
152 1.1.2.1 minoura extern struct cfdriver intio_cd;
153 1.1.2.1 minoura
154 1.1.2.1 minoura /* used in console initialization */
155 1.1.2.1 minoura extern int x68k_realconfig;
156 1.1.2.1 minoura int x68k_config_found __P((struct cfdata *, struct device *,
157 1.1.2.1 minoura void *, cfprint_t));
158 1.1.2.1 minoura static struct cfdata *cfdata_intiobus = NULL;
159 1.1.2.1 minoura
160 1.1.2.1 minoura static int
161 1.1.2.1 minoura intio_match(parent, cf, aux)
162 1.1.2.1 minoura struct device *parent;
163 1.1.2.1 minoura struct cfdata *cf;
164 1.1.2.1 minoura void *aux; /* NULL */
165 1.1.2.1 minoura {
166 1.1.2.1 minoura if (strcmp(aux, intio_cd.cd_name) != 0)
167 1.1.2.1 minoura return (0);
168 1.1.2.1 minoura if (cf->cf_unit != 0)
169 1.1.2.1 minoura return (0);
170 1.1.2.1 minoura if (x68k_realconfig == 0)
171 1.1.2.1 minoura cfdata_intiobus = cf; /* XXX */
172 1.1.2.1 minoura
173 1.1.2.1 minoura return (1);
174 1.1.2.1 minoura }
175 1.1.2.1 minoura
176 1.1.2.1 minoura
177 1.1.2.1 minoura /* used in console initialization: configure only MFP */
178 1.1.2.1 minoura static struct intio_attach_args initial_ia = {
179 1.1.2.1 minoura &intio_bus,
180 1.1.2.1 minoura 0/*XXX*/,
181 1.1.2.1 minoura
182 1.1.2.2 minoura "mfp", /* ia_name */
183 1.1.2.1 minoura MFP_ADDR, /* ia_addr */
184 1.1.2.1 minoura MFP_INTR, /* ia_intr */
185 1.1.2.1 minoura -1, /* ia_errintr */
186 1.1.2.1 minoura -1 /* ia_dma */
187 1.1.2.1 minoura };
188 1.1.2.1 minoura
189 1.1.2.1 minoura static void
190 1.1.2.1 minoura intio_attach(parent, self, aux)
191 1.1.2.1 minoura struct device *parent, *self;
192 1.1.2.1 minoura void *aux; /* NULL */
193 1.1.2.1 minoura {
194 1.1.2.1 minoura struct intio_softc *sc = (struct intio_softc *)self;
195 1.1.2.1 minoura struct intio_attach_args ia;
196 1.1.2.1 minoura
197 1.1.2.1 minoura if (self == NULL) {
198 1.1.2.1 minoura /* console only init */
199 1.1.2.1 minoura x68k_config_found(cfdata_intiobus, NULL, &initial_ia, NULL);
200 1.1.2.1 minoura return;
201 1.1.2.1 minoura }
202 1.1.2.1 minoura
203 1.1.2.3 minoura printf (" mapped at %08p\n", intiobase);
204 1.1.2.1 minoura
205 1.1.2.1 minoura sc->sc_map = extent_create("intiomap",
206 1.1.2.1 minoura PHYS_INTIODEV,
207 1.1.2.1 minoura PHYS_INTIODEV + 0x400000,
208 1.1.2.1 minoura M_DEVBUF, NULL, NULL, EX_NOWAIT);
209 1.1.2.1 minoura intio_alloc_system_ports (sc);
210 1.1.2.1 minoura
211 1.1.2.1 minoura sc->sc_bst = &intio_bus;
212 1.1.2.1 minoura sc->sc_bst->x68k_bus_device = self;
213 1.1.2.3 minoura sc->sc_dmat = &intio_bus_dma;
214 1.1.2.3 minoura sc->sc_dmac = 0;
215 1.1.2.3 minoura
216 1.1.2.1 minoura bzero(iiv, sizeof (struct intio_interrupt_vector) * 256);
217 1.1.2.1 minoura
218 1.1.2.1 minoura ia.ia_bst = sc->sc_bst;
219 1.1.2.1 minoura ia.ia_dmat = sc->sc_dmat;
220 1.1.2.1 minoura
221 1.1.2.1 minoura config_search (intio_search, self, &ia);
222 1.1.2.1 minoura }
223 1.1.2.1 minoura
224 1.1.2.1 minoura static int
225 1.1.2.1 minoura intio_search(parent, cf, aux)
226 1.1.2.1 minoura struct device *parent;
227 1.1.2.1 minoura struct cfdata *cf;
228 1.1.2.1 minoura void *aux;
229 1.1.2.1 minoura {
230 1.1.2.1 minoura struct intio_attach_args *ia = aux;
231 1.1.2.1 minoura struct intio_softc *sc = (struct intio_softc *)parent;
232 1.1.2.1 minoura
233 1.1.2.1 minoura ia->ia_bst = sc->sc_bst;
234 1.1.2.1 minoura ia->ia_dmat = sc->sc_dmat;
235 1.1.2.2 minoura ia->ia_name = cf->cf_driver->cd_name;
236 1.1.2.1 minoura ia->ia_addr = cf->cf_addr;
237 1.1.2.1 minoura ia->ia_intr = cf->cf_intr;
238 1.1.2.1 minoura ia->ia_errintr = cf->cf_errintr;
239 1.1.2.1 minoura ia->ia_dma = cf->cf_dma;
240 1.1.2.1 minoura
241 1.1.2.1 minoura if ((*cf->cf_attach->ca_match)(parent, cf, ia) > 0)
242 1.1.2.1 minoura config_attach(parent, cf, ia, intio_print);
243 1.1.2.1 minoura
244 1.1.2.1 minoura return (0);
245 1.1.2.1 minoura }
246 1.1.2.1 minoura
247 1.1.2.1 minoura static int
248 1.1.2.1 minoura intio_print(aux, name)
249 1.1.2.1 minoura void *aux;
250 1.1.2.1 minoura const char *name;
251 1.1.2.1 minoura {
252 1.1.2.1 minoura struct intio_attach_args *ia = aux;
253 1.1.2.1 minoura
254 1.1.2.1 minoura /* if (ia->ia_addr > 0) */
255 1.1.2.1 minoura printf (" addr 0x%06x", ia->ia_addr);
256 1.1.2.1 minoura if (ia->ia_intr > 0) {
257 1.1.2.1 minoura printf (" interrupting at 0x%02x", ia->ia_intr);
258 1.1.2.1 minoura if (ia->ia_errintr > 0)
259 1.1.2.1 minoura printf (" and 0x%02x", ia->ia_errintr);
260 1.1.2.1 minoura }
261 1.1.2.1 minoura if (ia->ia_dma >= 0)
262 1.1.2.1 minoura printf (" using DMA ch%d", ia->ia_dma);
263 1.1.2.1 minoura
264 1.1.2.1 minoura return (QUIET);
265 1.1.2.1 minoura }
266 1.1.2.1 minoura
267 1.1.2.1 minoura /*
268 1.1.2.1 minoura * intio memory map manager
269 1.1.2.1 minoura */
270 1.1.2.1 minoura
271 1.1.2.1 minoura int
272 1.1.2.1 minoura intio_map_allocate_region(parent, ia, flag)
273 1.1.2.1 minoura struct device *parent;
274 1.1.2.1 minoura struct intio_attach_args *ia;
275 1.1.2.1 minoura enum intio_map_flag flag; /* INTIO_MAP_TESTONLY or INTIO_MAP_ALLOCATE */
276 1.1.2.1 minoura {
277 1.1.2.1 minoura struct intio_softc *sc = (struct intio_softc*) parent;
278 1.1.2.1 minoura struct extent *map = sc->sc_map;
279 1.1.2.1 minoura int r;
280 1.1.2.1 minoura
281 1.1.2.1 minoura r = extent_alloc_region (map, ia->ia_addr, ia->ia_size, 0);
282 1.1.2.1 minoura #ifdef DEBUG
283 1.1.2.1 minoura extent_print (map);
284 1.1.2.1 minoura #endif
285 1.1.2.1 minoura if (r == 0) {
286 1.1.2.1 minoura if (flag != INTIO_MAP_ALLOCATE)
287 1.1.2.1 minoura extent_free (map, ia->ia_addr, ia->ia_size, 0);
288 1.1.2.1 minoura return 0;
289 1.1.2.1 minoura }
290 1.1.2.1 minoura
291 1.1.2.1 minoura return -1;
292 1.1.2.1 minoura }
293 1.1.2.1 minoura
294 1.1.2.1 minoura int
295 1.1.2.1 minoura intio_map_free_region(parent, ia)
296 1.1.2.1 minoura struct device *parent;
297 1.1.2.1 minoura struct intio_attach_args *ia;
298 1.1.2.1 minoura {
299 1.1.2.1 minoura struct intio_softc *sc = (struct intio_softc*) parent;
300 1.1.2.1 minoura struct extent *map = sc->sc_map;
301 1.1.2.1 minoura
302 1.1.2.1 minoura extent_free (map, ia->ia_addr, ia->ia_size, 0);
303 1.1.2.1 minoura #ifdef DEBUG
304 1.1.2.1 minoura extent_print (map);
305 1.1.2.1 minoura #endif
306 1.1.2.1 minoura return 0;
307 1.1.2.1 minoura }
308 1.1.2.1 minoura
309 1.1.2.1 minoura void
310 1.1.2.1 minoura intio_alloc_system_ports(sc)
311 1.1.2.1 minoura struct intio_softc *sc;
312 1.1.2.1 minoura {
313 1.1.2.1 minoura extent_alloc_region (sc->sc_map, INTIO_SYSPORT, 16, 0);
314 1.1.2.1 minoura }
315 1.1.2.1 minoura
316 1.1.2.1 minoura
317 1.1.2.1 minoura /*
318 1.1.2.1 minoura * intio bus space stuff.
319 1.1.2.1 minoura */
320 1.1.2.1 minoura static int
321 1.1.2.1 minoura intio_bus_space_map(t, bpa, size, flags, bshp)
322 1.1.2.1 minoura bus_space_tag_t t;
323 1.1.2.1 minoura bus_addr_t bpa;
324 1.1.2.1 minoura bus_size_t size;
325 1.1.2.1 minoura int flags;
326 1.1.2.1 minoura bus_space_handle_t *bshp;
327 1.1.2.1 minoura {
328 1.1.2.1 minoura /*
329 1.1.2.1 minoura * Intio bus is mapped permanently.
330 1.1.2.1 minoura */
331 1.1.2.1 minoura *bshp = (bus_space_handle_t)
332 1.1.2.1 minoura ((u_int) bpa - PHYS_INTIODEV + intiobase);
333 1.1.2.1 minoura
334 1.1.2.1 minoura return (0);
335 1.1.2.1 minoura }
336 1.1.2.1 minoura
337 1.1.2.1 minoura static void
338 1.1.2.1 minoura intio_bus_space_unmap(t, bsh, size)
339 1.1.2.1 minoura bus_space_tag_t t;
340 1.1.2.1 minoura bus_space_handle_t bsh;
341 1.1.2.1 minoura bus_size_t size;
342 1.1.2.1 minoura {
343 1.1.2.1 minoura return;
344 1.1.2.1 minoura }
345 1.1.2.1 minoura
346 1.1.2.1 minoura static int
347 1.1.2.1 minoura intio_bus_space_subregion(t, bsh, offset, size, nbshp)
348 1.1.2.1 minoura bus_space_tag_t t;
349 1.1.2.1 minoura bus_space_handle_t bsh;
350 1.1.2.1 minoura bus_size_t offset, size;
351 1.1.2.1 minoura bus_space_handle_t *nbshp;
352 1.1.2.1 minoura {
353 1.1.2.1 minoura
354 1.1.2.1 minoura *nbshp = bsh + offset;
355 1.1.2.1 minoura return (0);
356 1.1.2.1 minoura }
357 1.1.2.1 minoura
358 1.1.2.1 minoura
359 1.1.2.1 minoura /*
360 1.1.2.1 minoura * interrupt handler
361 1.1.2.1 minoura */
362 1.1.2.1 minoura int
363 1.1.2.1 minoura intio_intr_establish (vector, name, handler, arg)
364 1.1.2.1 minoura int vector;
365 1.1.2.1 minoura const char *name; /* XXX */
366 1.1.2.1 minoura intio_intr_handler_t handler;
367 1.1.2.1 minoura void *arg;
368 1.1.2.1 minoura {
369 1.1.2.1 minoura if (vector < 16)
370 1.1.2.1 minoura panic ("Invalid interrupt vector");
371 1.1.2.1 minoura if (iiv[vector].iiv_handler)
372 1.1.2.1 minoura return EBUSY;
373 1.1.2.1 minoura iiv[vector].iiv_handler = handler;
374 1.1.2.1 minoura iiv[vector].iiv_arg = arg;
375 1.1.2.1 minoura
376 1.1.2.1 minoura return 0;
377 1.1.2.1 minoura }
378 1.1.2.1 minoura
379 1.1.2.1 minoura int
380 1.1.2.1 minoura intio_intr_disestablish (vector, arg)
381 1.1.2.1 minoura int vector;
382 1.1.2.1 minoura void *arg;
383 1.1.2.1 minoura {
384 1.1.2.1 minoura if (iiv[vector].iiv_handler == 0 || iiv[vector].iiv_arg != arg)
385 1.1.2.1 minoura return EINVAL;
386 1.1.2.1 minoura iiv[vector].iiv_handler = 0;
387 1.1.2.1 minoura iiv[vector].iiv_arg = 0;
388 1.1.2.1 minoura
389 1.1.2.1 minoura return 0;
390 1.1.2.1 minoura }
391 1.1.2.1 minoura
392 1.1.2.1 minoura int
393 1.1.2.1 minoura intio_intr (frame)
394 1.1.2.1 minoura struct frame *frame;
395 1.1.2.1 minoura {
396 1.1.2.1 minoura int vector = frame->f_vector / 4;
397 1.1.2.1 minoura
398 1.1.2.1 minoura /* CAUTION: HERE WE ARE IN SPLHIGH() */
399 1.1.2.1 minoura /* LOWER TO APPROPRIATE IPL AT VERY FIRST IN THE HANDLER!! */
400 1.1.2.1 minoura
401 1.1.2.1 minoura if (iiv[vector].iiv_handler == 0) {
402 1.1.2.1 minoura printf ("Stray interrupt: %d type %x\n", vector, frame->f_format);
403 1.1.2.1 minoura return 0;
404 1.1.2.1 minoura }
405 1.1.2.1 minoura
406 1.1.2.3 minoura /* TODO: update the interrupt statics. */
407 1.1.2.3 minoura
408 1.1.2.1 minoura return (*(iiv[vector].iiv_handler)) (iiv[vector].iiv_arg);
409 1.1.2.3 minoura }
410 1.1.2.3 minoura
411 1.1.2.3 minoura
412 1.1.2.3 minoura
413 1.1.2.3 minoura /*
414 1.1.2.3 minoura * intio bus dma stuff. stolen from arch/i386/isa/isa_machdep.c
415 1.1.2.3 minoura */
416 1.1.2.3 minoura
417 1.1.2.3 minoura /*
418 1.1.2.3 minoura * Create an INTIO DMA map.
419 1.1.2.3 minoura */
420 1.1.2.3 minoura int
421 1.1.2.3 minoura _intio_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
422 1.1.2.3 minoura bus_dma_tag_t t;
423 1.1.2.3 minoura bus_size_t size;
424 1.1.2.3 minoura int nsegments;
425 1.1.2.3 minoura bus_size_t maxsegsz;
426 1.1.2.3 minoura bus_size_t boundary;
427 1.1.2.3 minoura int flags;
428 1.1.2.3 minoura bus_dmamap_t *dmamp;
429 1.1.2.3 minoura {
430 1.1.2.3 minoura struct intio_dma_cookie *cookie;
431 1.1.2.3 minoura bus_dmamap_t map;
432 1.1.2.3 minoura int error, cookieflags;
433 1.1.2.3 minoura void *cookiestore;
434 1.1.2.3 minoura size_t cookiesize;
435 1.1.2.3 minoura extern paddr_t avail_end;
436 1.1.2.3 minoura
437 1.1.2.3 minoura /* Call common function to create the basic map. */
438 1.1.2.3 minoura error = x68k_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
439 1.1.2.3 minoura flags, dmamp);
440 1.1.2.3 minoura if (error)
441 1.1.2.3 minoura return (error);
442 1.1.2.3 minoura
443 1.1.2.3 minoura map = *dmamp;
444 1.1.2.3 minoura map->x68k_dm_cookie = NULL;
445 1.1.2.3 minoura
446 1.1.2.3 minoura cookiesize = sizeof(struct intio_dma_cookie);
447 1.1.2.3 minoura
448 1.1.2.3 minoura /*
449 1.1.2.3 minoura * INTIO only has 24-bits of address space. This means
450 1.1.2.3 minoura * we can't DMA to pages over 16M. In order to DMA to
451 1.1.2.3 minoura * arbitrary buffers, we use "bounce buffers" - pages
452 1.1.2.3 minoura * in memory below the 16M boundary. On DMA reads,
453 1.1.2.3 minoura * DMA happens to the bounce buffers, and is copied into
454 1.1.2.3 minoura * the caller's buffer. On writes, data is copied into
455 1.1.2.3 minoura * but bounce buffer, and the DMA happens from those
456 1.1.2.3 minoura * pages. To software using the DMA mapping interface,
457 1.1.2.3 minoura * this looks simply like a data cache.
458 1.1.2.3 minoura *
459 1.1.2.3 minoura * If we have more than 16M of RAM in the system, we may
460 1.1.2.3 minoura * need bounce buffers. We check and remember that here.
461 1.1.2.3 minoura *
462 1.1.2.3 minoura * ...or, there is an opposite case. The most segments
463 1.1.2.3 minoura * a transfer will require is (maxxfer / NBPG) + 1. If
464 1.1.2.3 minoura * the caller can't handle that many segments (e.g. the
465 1.1.2.3 minoura * DMAC), we may have to bounce it as well.
466 1.1.2.3 minoura */
467 1.1.2.3 minoura if (avail_end <= t->_bounce_thresh)
468 1.1.2.3 minoura /* Bouncing not necessary due to memory size. */
469 1.1.2.3 minoura map->x68k_dm_bounce_thresh = 0;
470 1.1.2.3 minoura cookieflags = 0;
471 1.1.2.3 minoura if (map->x68k_dm_bounce_thresh != 0 ||
472 1.1.2.3 minoura ((map->x68k_dm_size / NBPG) + 1) > map->x68k_dm_segcnt) {
473 1.1.2.3 minoura cookieflags |= ID_MIGHT_NEED_BOUNCE;
474 1.1.2.3 minoura cookiesize += (sizeof(bus_dma_segment_t) * map->x68k_dm_segcnt);
475 1.1.2.3 minoura }
476 1.1.2.3 minoura
477 1.1.2.3 minoura /*
478 1.1.2.3 minoura * Allocate our cookie.
479 1.1.2.3 minoura */
480 1.1.2.3 minoura if ((cookiestore = malloc(cookiesize, M_DMAMAP,
481 1.1.2.3 minoura (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
482 1.1.2.3 minoura error = ENOMEM;
483 1.1.2.3 minoura goto out;
484 1.1.2.3 minoura }
485 1.1.2.3 minoura memset(cookiestore, 0, cookiesize);
486 1.1.2.3 minoura cookie = (struct intio_dma_cookie *)cookiestore;
487 1.1.2.3 minoura cookie->id_flags = cookieflags;
488 1.1.2.3 minoura map->x68k_dm_cookie = cookie;
489 1.1.2.3 minoura
490 1.1.2.3 minoura if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
491 1.1.2.3 minoura /*
492 1.1.2.3 minoura * Allocate the bounce pages now if the caller
493 1.1.2.3 minoura * wishes us to do so.
494 1.1.2.3 minoura */
495 1.1.2.3 minoura if ((flags & BUS_DMA_ALLOCNOW) == 0)
496 1.1.2.3 minoura goto out;
497 1.1.2.3 minoura
498 1.1.2.3 minoura error = _intio_dma_alloc_bouncebuf(t, map, size, flags);
499 1.1.2.3 minoura }
500 1.1.2.3 minoura
501 1.1.2.3 minoura out:
502 1.1.2.3 minoura if (error) {
503 1.1.2.3 minoura if (map->x68k_dm_cookie != NULL)
504 1.1.2.3 minoura free(map->x68k_dm_cookie, M_DMAMAP);
505 1.1.2.3 minoura x68k_bus_dmamap_destroy(t, map);
506 1.1.2.3 minoura }
507 1.1.2.3 minoura return (error);
508 1.1.2.3 minoura }
509 1.1.2.3 minoura
510 1.1.2.3 minoura /*
511 1.1.2.3 minoura * Destroy an INTIO DMA map.
512 1.1.2.3 minoura */
513 1.1.2.3 minoura void
514 1.1.2.3 minoura _intio_bus_dmamap_destroy(t, map)
515 1.1.2.3 minoura bus_dma_tag_t t;
516 1.1.2.3 minoura bus_dmamap_t map;
517 1.1.2.3 minoura {
518 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
519 1.1.2.3 minoura
520 1.1.2.3 minoura /*
521 1.1.2.3 minoura * Free any bounce pages this map might hold.
522 1.1.2.3 minoura */
523 1.1.2.3 minoura if (cookie->id_flags & ID_HAS_BOUNCE)
524 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
525 1.1.2.3 minoura
526 1.1.2.3 minoura free(cookie, M_DMAMAP);
527 1.1.2.3 minoura x68k_bus_dmamap_destroy(t, map);
528 1.1.2.3 minoura }
529 1.1.2.3 minoura
530 1.1.2.3 minoura /*
531 1.1.2.3 minoura * Load an INTIO DMA map with a linear buffer.
532 1.1.2.3 minoura */
533 1.1.2.3 minoura int
534 1.1.2.3 minoura _intio_bus_dmamap_load(t, map, buf, buflen, p, flags)
535 1.1.2.3 minoura bus_dma_tag_t t;
536 1.1.2.3 minoura bus_dmamap_t map;
537 1.1.2.3 minoura void *buf;
538 1.1.2.3 minoura bus_size_t buflen;
539 1.1.2.3 minoura struct proc *p;
540 1.1.2.3 minoura int flags;
541 1.1.2.3 minoura {
542 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
543 1.1.2.3 minoura int error;
544 1.1.2.3 minoura
545 1.1.2.3 minoura /*
546 1.1.2.3 minoura * Make sure that on error condition we return "no valid mappings."
547 1.1.2.3 minoura */
548 1.1.2.3 minoura map->dm_mapsize = 0;
549 1.1.2.3 minoura map->dm_nsegs = 0;
550 1.1.2.3 minoura
551 1.1.2.3 minoura /*
552 1.1.2.3 minoura * Try to load the map the normal way. If this errors out,
553 1.1.2.3 minoura * and we can bounce, we will.
554 1.1.2.3 minoura */
555 1.1.2.3 minoura error = x68k_bus_dmamap_load(t, map, buf, buflen, p, flags);
556 1.1.2.3 minoura if (error == 0 ||
557 1.1.2.3 minoura (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
558 1.1.2.3 minoura return (error);
559 1.1.2.3 minoura
560 1.1.2.3 minoura /*
561 1.1.2.3 minoura * Allocate bounce pages, if necessary.
562 1.1.2.3 minoura */
563 1.1.2.3 minoura if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
564 1.1.2.3 minoura error = _intio_dma_alloc_bouncebuf(t, map, buflen, flags);
565 1.1.2.3 minoura if (error)
566 1.1.2.3 minoura return (error);
567 1.1.2.3 minoura }
568 1.1.2.3 minoura
569 1.1.2.3 minoura /*
570 1.1.2.3 minoura * Cache a pointer to the caller's buffer and load the DMA map
571 1.1.2.3 minoura * with the bounce buffer.
572 1.1.2.3 minoura */
573 1.1.2.3 minoura cookie->id_origbuf = buf;
574 1.1.2.3 minoura cookie->id_origbuflen = buflen;
575 1.1.2.3 minoura cookie->id_buftype = ID_BUFTYPE_LINEAR;
576 1.1.2.3 minoura error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
577 1.1.2.3 minoura p, flags);
578 1.1.2.3 minoura if (error) {
579 1.1.2.3 minoura /*
580 1.1.2.3 minoura * Free the bounce pages, unless our resources
581 1.1.2.3 minoura * are reserved for our exclusive use.
582 1.1.2.3 minoura */
583 1.1.2.3 minoura if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
584 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
585 1.1.2.3 minoura return (error);
586 1.1.2.3 minoura }
587 1.1.2.3 minoura
588 1.1.2.3 minoura /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
589 1.1.2.3 minoura cookie->id_flags |= ID_IS_BOUNCING;
590 1.1.2.3 minoura return (0);
591 1.1.2.3 minoura }
592 1.1.2.3 minoura
593 1.1.2.3 minoura /*
594 1.1.2.3 minoura * Like _intio_bus_dmamap_load(), but for mbufs.
595 1.1.2.3 minoura */
596 1.1.2.3 minoura int
597 1.1.2.3 minoura _intio_bus_dmamap_load_mbuf(t, map, m0, flags)
598 1.1.2.3 minoura bus_dma_tag_t t;
599 1.1.2.3 minoura bus_dmamap_t map;
600 1.1.2.3 minoura struct mbuf *m0;
601 1.1.2.3 minoura int flags;
602 1.1.2.3 minoura {
603 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
604 1.1.2.3 minoura int error;
605 1.1.2.3 minoura
606 1.1.2.3 minoura /*
607 1.1.2.3 minoura * Make sure on error condition we return "no valid mappings."
608 1.1.2.3 minoura */
609 1.1.2.3 minoura map->dm_mapsize = 0;
610 1.1.2.3 minoura map->dm_nsegs = 0;
611 1.1.2.3 minoura
612 1.1.2.3 minoura #ifdef DIAGNOSTIC
613 1.1.2.3 minoura if ((m0->m_flags & M_PKTHDR) == 0)
614 1.1.2.3 minoura panic("_intio_bus_dmamap_load_mbuf: no packet header");
615 1.1.2.3 minoura #endif
616 1.1.2.3 minoura
617 1.1.2.3 minoura if (m0->m_pkthdr.len > map->x68k_dm_size)
618 1.1.2.3 minoura return (EINVAL);
619 1.1.2.3 minoura
620 1.1.2.3 minoura /*
621 1.1.2.3 minoura * Try to load the map the normal way. If this errors out,
622 1.1.2.3 minoura * and we can bounce, we will.
623 1.1.2.3 minoura */
624 1.1.2.3 minoura error = x68k_bus_dmamap_load_mbuf(t, map, m0, flags);
625 1.1.2.3 minoura if (error == 0 ||
626 1.1.2.3 minoura (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
627 1.1.2.3 minoura return (error);
628 1.1.2.3 minoura
629 1.1.2.3 minoura /*
630 1.1.2.3 minoura * Allocate bounce pages, if necessary.
631 1.1.2.3 minoura */
632 1.1.2.3 minoura if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
633 1.1.2.3 minoura error = _intio_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
634 1.1.2.3 minoura flags);
635 1.1.2.3 minoura if (error)
636 1.1.2.3 minoura return (error);
637 1.1.2.3 minoura }
638 1.1.2.3 minoura
639 1.1.2.3 minoura /*
640 1.1.2.3 minoura * Cache a pointer to the caller's buffer and load the DMA map
641 1.1.2.3 minoura * with the bounce buffer.
642 1.1.2.3 minoura */
643 1.1.2.3 minoura cookie->id_origbuf = m0;
644 1.1.2.3 minoura cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
645 1.1.2.3 minoura cookie->id_buftype = ID_BUFTYPE_MBUF;
646 1.1.2.3 minoura error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf,
647 1.1.2.3 minoura m0->m_pkthdr.len, NULL, flags);
648 1.1.2.3 minoura if (error) {
649 1.1.2.3 minoura /*
650 1.1.2.3 minoura * Free the bounce pages, unless our resources
651 1.1.2.3 minoura * are reserved for our exclusive use.
652 1.1.2.3 minoura */
653 1.1.2.3 minoura if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
654 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
655 1.1.2.3 minoura return (error);
656 1.1.2.3 minoura }
657 1.1.2.3 minoura
658 1.1.2.3 minoura /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
659 1.1.2.3 minoura cookie->id_flags |= ID_IS_BOUNCING;
660 1.1.2.3 minoura return (0);
661 1.1.2.3 minoura }
662 1.1.2.3 minoura
663 1.1.2.3 minoura /*
664 1.1.2.3 minoura * Like _intio_bus_dmamap_load(), but for uios.
665 1.1.2.3 minoura */
666 1.1.2.3 minoura int
667 1.1.2.3 minoura _intio_bus_dmamap_load_uio(t, map, uio, flags)
668 1.1.2.3 minoura bus_dma_tag_t t;
669 1.1.2.3 minoura bus_dmamap_t map;
670 1.1.2.3 minoura struct uio *uio;
671 1.1.2.3 minoura int flags;
672 1.1.2.3 minoura {
673 1.1.2.3 minoura panic("_intio_bus_dmamap_load_uio: not implemented");
674 1.1.2.3 minoura }
675 1.1.2.3 minoura
676 1.1.2.3 minoura /*
677 1.1.2.3 minoura * Like _intio_bus_dmamap_load(), but for raw memory allocated with
678 1.1.2.3 minoura * bus_dmamem_alloc().
679 1.1.2.3 minoura */
680 1.1.2.3 minoura int
681 1.1.2.3 minoura _intio_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
682 1.1.2.3 minoura bus_dma_tag_t t;
683 1.1.2.3 minoura bus_dmamap_t map;
684 1.1.2.3 minoura bus_dma_segment_t *segs;
685 1.1.2.3 minoura int nsegs;
686 1.1.2.3 minoura bus_size_t size;
687 1.1.2.3 minoura int flags;
688 1.1.2.3 minoura {
689 1.1.2.3 minoura
690 1.1.2.3 minoura panic("_intio_bus_dmamap_load_raw: not implemented");
691 1.1.2.3 minoura }
692 1.1.2.3 minoura
693 1.1.2.3 minoura /*
694 1.1.2.3 minoura * Unload an INTIO DMA map.
695 1.1.2.3 minoura */
696 1.1.2.3 minoura void
697 1.1.2.3 minoura _intio_bus_dmamap_unload(t, map)
698 1.1.2.3 minoura bus_dma_tag_t t;
699 1.1.2.3 minoura bus_dmamap_t map;
700 1.1.2.3 minoura {
701 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
702 1.1.2.3 minoura
703 1.1.2.3 minoura /*
704 1.1.2.3 minoura * If we have bounce pages, free them, unless they're
705 1.1.2.3 minoura * reserved for our exclusive use.
706 1.1.2.3 minoura */
707 1.1.2.3 minoura if ((cookie->id_flags & ID_HAS_BOUNCE) &&
708 1.1.2.3 minoura (map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
709 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
710 1.1.2.3 minoura
711 1.1.2.3 minoura cookie->id_flags &= ~ID_IS_BOUNCING;
712 1.1.2.3 minoura cookie->id_buftype = ID_BUFTYPE_INVALID;
713 1.1.2.3 minoura
714 1.1.2.3 minoura /*
715 1.1.2.3 minoura * Do the generic bits of the unload.
716 1.1.2.3 minoura */
717 1.1.2.3 minoura x68k_bus_dmamap_unload(t, map);
718 1.1.2.3 minoura }
719 1.1.2.3 minoura
720 1.1.2.3 minoura /*
721 1.1.2.3 minoura * Synchronize an INTIO DMA map.
722 1.1.2.3 minoura */
723 1.1.2.3 minoura void
724 1.1.2.3 minoura _intio_bus_dmamap_sync(t, map, offset, len, ops)
725 1.1.2.3 minoura bus_dma_tag_t t;
726 1.1.2.3 minoura bus_dmamap_t map;
727 1.1.2.3 minoura bus_addr_t offset;
728 1.1.2.3 minoura bus_size_t len;
729 1.1.2.3 minoura int ops;
730 1.1.2.3 minoura {
731 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
732 1.1.2.3 minoura
733 1.1.2.3 minoura /*
734 1.1.2.3 minoura * Mixing PRE and POST operations is not allowed.
735 1.1.2.3 minoura */
736 1.1.2.3 minoura if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
737 1.1.2.3 minoura (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
738 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: mix PRE and POST");
739 1.1.2.3 minoura
740 1.1.2.3 minoura #ifdef DIAGNOSTIC
741 1.1.2.3 minoura if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
742 1.1.2.3 minoura if (offset >= map->dm_mapsize)
743 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: bad offset");
744 1.1.2.3 minoura if (len == 0 || (offset + len) > map->dm_mapsize)
745 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: bad length");
746 1.1.2.3 minoura }
747 1.1.2.3 minoura #endif
748 1.1.2.3 minoura
749 1.1.2.3 minoura /*
750 1.1.2.3 minoura * If we're not bouncing, just return; nothing to do.
751 1.1.2.3 minoura */
752 1.1.2.3 minoura if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
753 1.1.2.3 minoura return;
754 1.1.2.3 minoura
755 1.1.2.3 minoura switch (cookie->id_buftype) {
756 1.1.2.3 minoura case ID_BUFTYPE_LINEAR:
757 1.1.2.3 minoura /*
758 1.1.2.3 minoura * Nothing to do for pre-read.
759 1.1.2.3 minoura */
760 1.1.2.3 minoura
761 1.1.2.3 minoura if (ops & BUS_DMASYNC_PREWRITE) {
762 1.1.2.3 minoura /*
763 1.1.2.3 minoura * Copy the caller's buffer to the bounce buffer.
764 1.1.2.3 minoura */
765 1.1.2.3 minoura memcpy((char *)cookie->id_bouncebuf + offset,
766 1.1.2.3 minoura (char *)cookie->id_origbuf + offset, len);
767 1.1.2.3 minoura }
768 1.1.2.3 minoura
769 1.1.2.3 minoura if (ops & BUS_DMASYNC_POSTREAD) {
770 1.1.2.3 minoura /*
771 1.1.2.3 minoura * Copy the bounce buffer to the caller's buffer.
772 1.1.2.3 minoura */
773 1.1.2.3 minoura memcpy((char *)cookie->id_origbuf + offset,
774 1.1.2.3 minoura (char *)cookie->id_bouncebuf + offset, len);
775 1.1.2.3 minoura }
776 1.1.2.3 minoura
777 1.1.2.3 minoura /*
778 1.1.2.3 minoura * Nothing to do for post-write.
779 1.1.2.3 minoura */
780 1.1.2.3 minoura break;
781 1.1.2.3 minoura
782 1.1.2.3 minoura case ID_BUFTYPE_MBUF:
783 1.1.2.3 minoura {
784 1.1.2.3 minoura struct mbuf *m, *m0 = cookie->id_origbuf;
785 1.1.2.3 minoura bus_size_t minlen, moff;
786 1.1.2.3 minoura
787 1.1.2.3 minoura /*
788 1.1.2.3 minoura * Nothing to do for pre-read.
789 1.1.2.3 minoura */
790 1.1.2.3 minoura
791 1.1.2.3 minoura if (ops & BUS_DMASYNC_PREWRITE) {
792 1.1.2.3 minoura /*
793 1.1.2.3 minoura * Copy the caller's buffer to the bounce buffer.
794 1.1.2.3 minoura */
795 1.1.2.3 minoura m_copydata(m0, offset, len,
796 1.1.2.3 minoura (char *)cookie->id_bouncebuf + offset);
797 1.1.2.3 minoura }
798 1.1.2.3 minoura
799 1.1.2.3 minoura if (ops & BUS_DMASYNC_POSTREAD) {
800 1.1.2.3 minoura /*
801 1.1.2.3 minoura * Copy the bounce buffer to the caller's buffer.
802 1.1.2.3 minoura */
803 1.1.2.3 minoura for (moff = offset, m = m0; m != NULL && len != 0;
804 1.1.2.3 minoura m = m->m_next) {
805 1.1.2.3 minoura /* Find the beginning mbuf. */
806 1.1.2.3 minoura if (moff >= m->m_len) {
807 1.1.2.3 minoura moff -= m->m_len;
808 1.1.2.3 minoura continue;
809 1.1.2.3 minoura }
810 1.1.2.3 minoura
811 1.1.2.3 minoura /*
812 1.1.2.3 minoura * Now at the first mbuf to sync; nail
813 1.1.2.3 minoura * each one until we have exhausted the
814 1.1.2.3 minoura * length.
815 1.1.2.3 minoura */
816 1.1.2.3 minoura minlen = len < m->m_len - moff ?
817 1.1.2.3 minoura len : m->m_len - moff;
818 1.1.2.3 minoura
819 1.1.2.3 minoura memcpy(mtod(m, caddr_t) + moff,
820 1.1.2.3 minoura (char *)cookie->id_bouncebuf + offset,
821 1.1.2.3 minoura minlen);
822 1.1.2.3 minoura
823 1.1.2.3 minoura moff = 0;
824 1.1.2.3 minoura len -= minlen;
825 1.1.2.3 minoura offset += minlen;
826 1.1.2.3 minoura }
827 1.1.2.3 minoura }
828 1.1.2.3 minoura
829 1.1.2.3 minoura /*
830 1.1.2.3 minoura * Nothing to do for post-write.
831 1.1.2.3 minoura */
832 1.1.2.3 minoura break;
833 1.1.2.3 minoura }
834 1.1.2.3 minoura
835 1.1.2.3 minoura case ID_BUFTYPE_UIO:
836 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: ID_BUFTYPE_UIO");
837 1.1.2.3 minoura break;
838 1.1.2.3 minoura
839 1.1.2.3 minoura case ID_BUFTYPE_RAW:
840 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: ID_BUFTYPE_RAW");
841 1.1.2.3 minoura break;
842 1.1.2.3 minoura
843 1.1.2.3 minoura case ID_BUFTYPE_INVALID:
844 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: ID_BUFTYPE_INVALID");
845 1.1.2.3 minoura break;
846 1.1.2.3 minoura
847 1.1.2.3 minoura default:
848 1.1.2.3 minoura printf("unknown buffer type %d\n", cookie->id_buftype);
849 1.1.2.3 minoura panic("_intio_bus_dmamap_sync");
850 1.1.2.3 minoura }
851 1.1.2.3 minoura }
852 1.1.2.3 minoura
853 1.1.2.3 minoura /*
854 1.1.2.3 minoura * Allocate memory safe for INTIO DMA.
855 1.1.2.3 minoura */
856 1.1.2.3 minoura int
857 1.1.2.3 minoura _intio_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
858 1.1.2.3 minoura bus_dma_tag_t t;
859 1.1.2.3 minoura bus_size_t size, alignment, boundary;
860 1.1.2.3 minoura bus_dma_segment_t *segs;
861 1.1.2.3 minoura int nsegs;
862 1.1.2.3 minoura int *rsegs;
863 1.1.2.3 minoura int flags;
864 1.1.2.3 minoura {
865 1.1.2.3 minoura paddr_t high;
866 1.1.2.3 minoura extern paddr_t avail_end;
867 1.1.2.3 minoura
868 1.1.2.3 minoura if (avail_end > INTIO_DMA_BOUNCE_THRESHOLD)
869 1.1.2.3 minoura high = trunc_page(INTIO_DMA_BOUNCE_THRESHOLD);
870 1.1.2.3 minoura else
871 1.1.2.3 minoura high = trunc_page(avail_end);
872 1.1.2.3 minoura
873 1.1.2.3 minoura return (x68k_bus_dmamem_alloc_range(t, size, alignment, boundary,
874 1.1.2.3 minoura segs, nsegs, rsegs, flags, 0, high));
875 1.1.2.3 minoura }
876 1.1.2.3 minoura
877 1.1.2.3 minoura /**********************************************************************
878 1.1.2.3 minoura * INTIO DMA utility functions
879 1.1.2.3 minoura **********************************************************************/
880 1.1.2.3 minoura
881 1.1.2.3 minoura int
882 1.1.2.3 minoura _intio_dma_alloc_bouncebuf(t, map, size, flags)
883 1.1.2.3 minoura bus_dma_tag_t t;
884 1.1.2.3 minoura bus_dmamap_t map;
885 1.1.2.3 minoura bus_size_t size;
886 1.1.2.3 minoura int flags;
887 1.1.2.3 minoura {
888 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
889 1.1.2.3 minoura int error = 0;
890 1.1.2.3 minoura
891 1.1.2.3 minoura cookie->id_bouncebuflen = round_page(size);
892 1.1.2.3 minoura error = _intio_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
893 1.1.2.3 minoura NBPG, map->x68k_dm_boundary, cookie->id_bouncesegs,
894 1.1.2.3 minoura map->x68k_dm_segcnt, &cookie->id_nbouncesegs, flags);
895 1.1.2.3 minoura if (error)
896 1.1.2.3 minoura goto out;
897 1.1.2.3 minoura error = x68k_bus_dmamem_map(t, cookie->id_bouncesegs,
898 1.1.2.3 minoura cookie->id_nbouncesegs, cookie->id_bouncebuflen,
899 1.1.2.3 minoura (caddr_t *)&cookie->id_bouncebuf, flags);
900 1.1.2.3 minoura
901 1.1.2.3 minoura out:
902 1.1.2.3 minoura if (error) {
903 1.1.2.3 minoura x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
904 1.1.2.3 minoura cookie->id_nbouncesegs);
905 1.1.2.3 minoura cookie->id_bouncebuflen = 0;
906 1.1.2.3 minoura cookie->id_nbouncesegs = 0;
907 1.1.2.3 minoura } else {
908 1.1.2.3 minoura cookie->id_flags |= ID_HAS_BOUNCE;
909 1.1.2.3 minoura }
910 1.1.2.3 minoura
911 1.1.2.3 minoura return (error);
912 1.1.2.3 minoura }
913 1.1.2.3 minoura
914 1.1.2.3 minoura void
915 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map)
916 1.1.2.3 minoura bus_dma_tag_t t;
917 1.1.2.3 minoura bus_dmamap_t map;
918 1.1.2.3 minoura {
919 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
920 1.1.2.3 minoura
921 1.1.2.3 minoura x68k_bus_dmamem_unmap(t, cookie->id_bouncebuf,
922 1.1.2.3 minoura cookie->id_bouncebuflen);
923 1.1.2.3 minoura x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
924 1.1.2.3 minoura cookie->id_nbouncesegs);
925 1.1.2.3 minoura cookie->id_bouncebuflen = 0;
926 1.1.2.3 minoura cookie->id_nbouncesegs = 0;
927 1.1.2.3 minoura cookie->id_flags &= ~ID_HAS_BOUNCE;
928 1.1.2.1 minoura }
929