intio.c revision 1.1.2.5 1 1.1.2.5 minoura /* $NetBSD: intio.c,v 1.1.2.5 1999/02/02 23:44:58 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_dma */
186 1.1.2.4 minoura -1, /* ia_dmaintr */
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_dma = cf->cf_dma;
239 1.1.2.4 minoura ia->ia_dmaintr = cf->cf_dmaintr;
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.4 minoura if (ia->ia_intr > 0)
257 1.1.2.5 minoura printf (" intr 0x%02x", ia->ia_intr);
258 1.1.2.4 minoura if (ia->ia_dma >= 0) {
259 1.1.2.1 minoura printf (" using DMA ch%d", ia->ia_dma);
260 1.1.2.4 minoura if (ia->ia_dmaintr > 0)
261 1.1.2.5 minoura printf (" intr 0x%02x and 0x%02x",
262 1.1.2.4 minoura ia->ia_dmaintr, ia->ia_dmaintr+1);
263 1.1.2.4 minoura }
264 1.1.2.1 minoura
265 1.1.2.1 minoura return (QUIET);
266 1.1.2.1 minoura }
267 1.1.2.1 minoura
268 1.1.2.1 minoura /*
269 1.1.2.1 minoura * intio memory map manager
270 1.1.2.1 minoura */
271 1.1.2.1 minoura
272 1.1.2.1 minoura int
273 1.1.2.1 minoura intio_map_allocate_region(parent, ia, flag)
274 1.1.2.1 minoura struct device *parent;
275 1.1.2.1 minoura struct intio_attach_args *ia;
276 1.1.2.1 minoura enum intio_map_flag flag; /* INTIO_MAP_TESTONLY or INTIO_MAP_ALLOCATE */
277 1.1.2.1 minoura {
278 1.1.2.1 minoura struct intio_softc *sc = (struct intio_softc*) parent;
279 1.1.2.1 minoura struct extent *map = sc->sc_map;
280 1.1.2.1 minoura int r;
281 1.1.2.1 minoura
282 1.1.2.1 minoura r = extent_alloc_region (map, ia->ia_addr, ia->ia_size, 0);
283 1.1.2.1 minoura #ifdef DEBUG
284 1.1.2.1 minoura extent_print (map);
285 1.1.2.1 minoura #endif
286 1.1.2.1 minoura if (r == 0) {
287 1.1.2.1 minoura if (flag != INTIO_MAP_ALLOCATE)
288 1.1.2.1 minoura extent_free (map, ia->ia_addr, ia->ia_size, 0);
289 1.1.2.1 minoura return 0;
290 1.1.2.1 minoura }
291 1.1.2.1 minoura
292 1.1.2.1 minoura return -1;
293 1.1.2.1 minoura }
294 1.1.2.1 minoura
295 1.1.2.1 minoura int
296 1.1.2.1 minoura intio_map_free_region(parent, ia)
297 1.1.2.1 minoura struct device *parent;
298 1.1.2.1 minoura struct intio_attach_args *ia;
299 1.1.2.1 minoura {
300 1.1.2.1 minoura struct intio_softc *sc = (struct intio_softc*) parent;
301 1.1.2.1 minoura struct extent *map = sc->sc_map;
302 1.1.2.1 minoura
303 1.1.2.1 minoura extent_free (map, ia->ia_addr, ia->ia_size, 0);
304 1.1.2.1 minoura #ifdef DEBUG
305 1.1.2.1 minoura extent_print (map);
306 1.1.2.1 minoura #endif
307 1.1.2.1 minoura return 0;
308 1.1.2.1 minoura }
309 1.1.2.1 minoura
310 1.1.2.1 minoura void
311 1.1.2.1 minoura intio_alloc_system_ports(sc)
312 1.1.2.1 minoura struct intio_softc *sc;
313 1.1.2.1 minoura {
314 1.1.2.1 minoura extent_alloc_region (sc->sc_map, INTIO_SYSPORT, 16, 0);
315 1.1.2.5 minoura extent_alloc_region (sc->sc_map, INTIO_SICILIAN, 0x2000, 0);
316 1.1.2.1 minoura }
317 1.1.2.1 minoura
318 1.1.2.1 minoura
319 1.1.2.1 minoura /*
320 1.1.2.1 minoura * intio bus space stuff.
321 1.1.2.1 minoura */
322 1.1.2.1 minoura static int
323 1.1.2.1 minoura intio_bus_space_map(t, bpa, size, flags, bshp)
324 1.1.2.1 minoura bus_space_tag_t t;
325 1.1.2.1 minoura bus_addr_t bpa;
326 1.1.2.1 minoura bus_size_t size;
327 1.1.2.1 minoura int flags;
328 1.1.2.1 minoura bus_space_handle_t *bshp;
329 1.1.2.1 minoura {
330 1.1.2.1 minoura /*
331 1.1.2.1 minoura * Intio bus is mapped permanently.
332 1.1.2.1 minoura */
333 1.1.2.1 minoura *bshp = (bus_space_handle_t)
334 1.1.2.1 minoura ((u_int) bpa - PHYS_INTIODEV + intiobase);
335 1.1.2.1 minoura
336 1.1.2.1 minoura return (0);
337 1.1.2.1 minoura }
338 1.1.2.1 minoura
339 1.1.2.1 minoura static void
340 1.1.2.1 minoura intio_bus_space_unmap(t, bsh, size)
341 1.1.2.1 minoura bus_space_tag_t t;
342 1.1.2.1 minoura bus_space_handle_t bsh;
343 1.1.2.1 minoura bus_size_t size;
344 1.1.2.1 minoura {
345 1.1.2.1 minoura return;
346 1.1.2.1 minoura }
347 1.1.2.1 minoura
348 1.1.2.1 minoura static int
349 1.1.2.1 minoura intio_bus_space_subregion(t, bsh, offset, size, nbshp)
350 1.1.2.1 minoura bus_space_tag_t t;
351 1.1.2.1 minoura bus_space_handle_t bsh;
352 1.1.2.1 minoura bus_size_t offset, size;
353 1.1.2.1 minoura bus_space_handle_t *nbshp;
354 1.1.2.1 minoura {
355 1.1.2.1 minoura
356 1.1.2.1 minoura *nbshp = bsh + offset;
357 1.1.2.1 minoura return (0);
358 1.1.2.1 minoura }
359 1.1.2.1 minoura
360 1.1.2.1 minoura
361 1.1.2.1 minoura /*
362 1.1.2.1 minoura * interrupt handler
363 1.1.2.1 minoura */
364 1.1.2.1 minoura int
365 1.1.2.1 minoura intio_intr_establish (vector, name, handler, arg)
366 1.1.2.1 minoura int vector;
367 1.1.2.1 minoura const char *name; /* XXX */
368 1.1.2.1 minoura intio_intr_handler_t handler;
369 1.1.2.1 minoura void *arg;
370 1.1.2.1 minoura {
371 1.1.2.1 minoura if (vector < 16)
372 1.1.2.1 minoura panic ("Invalid interrupt vector");
373 1.1.2.1 minoura if (iiv[vector].iiv_handler)
374 1.1.2.1 minoura return EBUSY;
375 1.1.2.1 minoura iiv[vector].iiv_handler = handler;
376 1.1.2.1 minoura iiv[vector].iiv_arg = arg;
377 1.1.2.1 minoura
378 1.1.2.1 minoura return 0;
379 1.1.2.1 minoura }
380 1.1.2.1 minoura
381 1.1.2.1 minoura int
382 1.1.2.1 minoura intio_intr_disestablish (vector, arg)
383 1.1.2.1 minoura int vector;
384 1.1.2.1 minoura void *arg;
385 1.1.2.1 minoura {
386 1.1.2.1 minoura if (iiv[vector].iiv_handler == 0 || iiv[vector].iiv_arg != arg)
387 1.1.2.1 minoura return EINVAL;
388 1.1.2.1 minoura iiv[vector].iiv_handler = 0;
389 1.1.2.1 minoura iiv[vector].iiv_arg = 0;
390 1.1.2.1 minoura
391 1.1.2.1 minoura return 0;
392 1.1.2.1 minoura }
393 1.1.2.1 minoura
394 1.1.2.1 minoura int
395 1.1.2.1 minoura intio_intr (frame)
396 1.1.2.1 minoura struct frame *frame;
397 1.1.2.1 minoura {
398 1.1.2.1 minoura int vector = frame->f_vector / 4;
399 1.1.2.1 minoura
400 1.1.2.1 minoura /* CAUTION: HERE WE ARE IN SPLHIGH() */
401 1.1.2.1 minoura /* LOWER TO APPROPRIATE IPL AT VERY FIRST IN THE HANDLER!! */
402 1.1.2.1 minoura
403 1.1.2.1 minoura if (iiv[vector].iiv_handler == 0) {
404 1.1.2.1 minoura printf ("Stray interrupt: %d type %x\n", vector, frame->f_format);
405 1.1.2.1 minoura return 0;
406 1.1.2.1 minoura }
407 1.1.2.1 minoura
408 1.1.2.3 minoura /* TODO: update the interrupt statics. */
409 1.1.2.3 minoura
410 1.1.2.1 minoura return (*(iiv[vector].iiv_handler)) (iiv[vector].iiv_arg);
411 1.1.2.3 minoura }
412 1.1.2.3 minoura
413 1.1.2.5 minoura /*
414 1.1.2.5 minoura * Intio I/O controler interrupt
415 1.1.2.5 minoura */
416 1.1.2.5 minoura static intio_ivec = 0;
417 1.1.2.5 minoura void
418 1.1.2.5 minoura intio_set_ivec (vec)
419 1.1.2.5 minoura int vec;
420 1.1.2.5 minoura {
421 1.1.2.5 minoura vec &= 0xfc;
422 1.1.2.5 minoura
423 1.1.2.5 minoura if (intio_ivec && intio_ivec != (vec & 0xfc))
424 1.1.2.5 minoura panic ("Wrong interrupt vector for Sicilian.");
425 1.1.2.5 minoura
426 1.1.2.5 minoura intio_ivec = vec;
427 1.1.2.5 minoura intio_set_sicilian_ivec(vec);
428 1.1.2.5 minoura }
429 1.1.2.3 minoura
430 1.1.2.3 minoura
431 1.1.2.3 minoura /*
432 1.1.2.3 minoura * intio bus dma stuff. stolen from arch/i386/isa/isa_machdep.c
433 1.1.2.3 minoura */
434 1.1.2.3 minoura
435 1.1.2.3 minoura /*
436 1.1.2.3 minoura * Create an INTIO DMA map.
437 1.1.2.3 minoura */
438 1.1.2.3 minoura int
439 1.1.2.3 minoura _intio_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary, flags, dmamp)
440 1.1.2.3 minoura bus_dma_tag_t t;
441 1.1.2.3 minoura bus_size_t size;
442 1.1.2.3 minoura int nsegments;
443 1.1.2.3 minoura bus_size_t maxsegsz;
444 1.1.2.3 minoura bus_size_t boundary;
445 1.1.2.3 minoura int flags;
446 1.1.2.3 minoura bus_dmamap_t *dmamp;
447 1.1.2.3 minoura {
448 1.1.2.3 minoura struct intio_dma_cookie *cookie;
449 1.1.2.3 minoura bus_dmamap_t map;
450 1.1.2.3 minoura int error, cookieflags;
451 1.1.2.3 minoura void *cookiestore;
452 1.1.2.3 minoura size_t cookiesize;
453 1.1.2.3 minoura extern paddr_t avail_end;
454 1.1.2.3 minoura
455 1.1.2.3 minoura /* Call common function to create the basic map. */
456 1.1.2.3 minoura error = x68k_bus_dmamap_create(t, size, nsegments, maxsegsz, boundary,
457 1.1.2.3 minoura flags, dmamp);
458 1.1.2.3 minoura if (error)
459 1.1.2.3 minoura return (error);
460 1.1.2.3 minoura
461 1.1.2.3 minoura map = *dmamp;
462 1.1.2.3 minoura map->x68k_dm_cookie = NULL;
463 1.1.2.3 minoura
464 1.1.2.3 minoura cookiesize = sizeof(struct intio_dma_cookie);
465 1.1.2.3 minoura
466 1.1.2.3 minoura /*
467 1.1.2.3 minoura * INTIO only has 24-bits of address space. This means
468 1.1.2.3 minoura * we can't DMA to pages over 16M. In order to DMA to
469 1.1.2.3 minoura * arbitrary buffers, we use "bounce buffers" - pages
470 1.1.2.3 minoura * in memory below the 16M boundary. On DMA reads,
471 1.1.2.3 minoura * DMA happens to the bounce buffers, and is copied into
472 1.1.2.3 minoura * the caller's buffer. On writes, data is copied into
473 1.1.2.3 minoura * but bounce buffer, and the DMA happens from those
474 1.1.2.3 minoura * pages. To software using the DMA mapping interface,
475 1.1.2.3 minoura * this looks simply like a data cache.
476 1.1.2.3 minoura *
477 1.1.2.3 minoura * If we have more than 16M of RAM in the system, we may
478 1.1.2.3 minoura * need bounce buffers. We check and remember that here.
479 1.1.2.3 minoura *
480 1.1.2.3 minoura * ...or, there is an opposite case. The most segments
481 1.1.2.3 minoura * a transfer will require is (maxxfer / NBPG) + 1. If
482 1.1.2.3 minoura * the caller can't handle that many segments (e.g. the
483 1.1.2.3 minoura * DMAC), we may have to bounce it as well.
484 1.1.2.3 minoura */
485 1.1.2.3 minoura if (avail_end <= t->_bounce_thresh)
486 1.1.2.3 minoura /* Bouncing not necessary due to memory size. */
487 1.1.2.3 minoura map->x68k_dm_bounce_thresh = 0;
488 1.1.2.3 minoura cookieflags = 0;
489 1.1.2.3 minoura if (map->x68k_dm_bounce_thresh != 0 ||
490 1.1.2.3 minoura ((map->x68k_dm_size / NBPG) + 1) > map->x68k_dm_segcnt) {
491 1.1.2.3 minoura cookieflags |= ID_MIGHT_NEED_BOUNCE;
492 1.1.2.3 minoura cookiesize += (sizeof(bus_dma_segment_t) * map->x68k_dm_segcnt);
493 1.1.2.3 minoura }
494 1.1.2.3 minoura
495 1.1.2.3 minoura /*
496 1.1.2.3 minoura * Allocate our cookie.
497 1.1.2.3 minoura */
498 1.1.2.3 minoura if ((cookiestore = malloc(cookiesize, M_DMAMAP,
499 1.1.2.3 minoura (flags & BUS_DMA_NOWAIT) ? M_NOWAIT : M_WAITOK)) == NULL) {
500 1.1.2.3 minoura error = ENOMEM;
501 1.1.2.3 minoura goto out;
502 1.1.2.3 minoura }
503 1.1.2.3 minoura memset(cookiestore, 0, cookiesize);
504 1.1.2.3 minoura cookie = (struct intio_dma_cookie *)cookiestore;
505 1.1.2.3 minoura cookie->id_flags = cookieflags;
506 1.1.2.3 minoura map->x68k_dm_cookie = cookie;
507 1.1.2.3 minoura
508 1.1.2.3 minoura if (cookieflags & ID_MIGHT_NEED_BOUNCE) {
509 1.1.2.3 minoura /*
510 1.1.2.3 minoura * Allocate the bounce pages now if the caller
511 1.1.2.3 minoura * wishes us to do so.
512 1.1.2.3 minoura */
513 1.1.2.3 minoura if ((flags & BUS_DMA_ALLOCNOW) == 0)
514 1.1.2.3 minoura goto out;
515 1.1.2.3 minoura
516 1.1.2.3 minoura error = _intio_dma_alloc_bouncebuf(t, map, size, flags);
517 1.1.2.3 minoura }
518 1.1.2.3 minoura
519 1.1.2.3 minoura out:
520 1.1.2.3 minoura if (error) {
521 1.1.2.3 minoura if (map->x68k_dm_cookie != NULL)
522 1.1.2.3 minoura free(map->x68k_dm_cookie, M_DMAMAP);
523 1.1.2.3 minoura x68k_bus_dmamap_destroy(t, map);
524 1.1.2.3 minoura }
525 1.1.2.3 minoura return (error);
526 1.1.2.3 minoura }
527 1.1.2.3 minoura
528 1.1.2.3 minoura /*
529 1.1.2.3 minoura * Destroy an INTIO DMA map.
530 1.1.2.3 minoura */
531 1.1.2.3 minoura void
532 1.1.2.3 minoura _intio_bus_dmamap_destroy(t, map)
533 1.1.2.3 minoura bus_dma_tag_t t;
534 1.1.2.3 minoura bus_dmamap_t map;
535 1.1.2.3 minoura {
536 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
537 1.1.2.3 minoura
538 1.1.2.3 minoura /*
539 1.1.2.3 minoura * Free any bounce pages this map might hold.
540 1.1.2.3 minoura */
541 1.1.2.3 minoura if (cookie->id_flags & ID_HAS_BOUNCE)
542 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
543 1.1.2.3 minoura
544 1.1.2.3 minoura free(cookie, M_DMAMAP);
545 1.1.2.3 minoura x68k_bus_dmamap_destroy(t, map);
546 1.1.2.3 minoura }
547 1.1.2.3 minoura
548 1.1.2.3 minoura /*
549 1.1.2.3 minoura * Load an INTIO DMA map with a linear buffer.
550 1.1.2.3 minoura */
551 1.1.2.3 minoura int
552 1.1.2.3 minoura _intio_bus_dmamap_load(t, map, buf, buflen, p, flags)
553 1.1.2.3 minoura bus_dma_tag_t t;
554 1.1.2.3 minoura bus_dmamap_t map;
555 1.1.2.3 minoura void *buf;
556 1.1.2.3 minoura bus_size_t buflen;
557 1.1.2.3 minoura struct proc *p;
558 1.1.2.3 minoura int flags;
559 1.1.2.3 minoura {
560 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
561 1.1.2.3 minoura int error;
562 1.1.2.3 minoura
563 1.1.2.3 minoura /*
564 1.1.2.3 minoura * Make sure that on error condition we return "no valid mappings."
565 1.1.2.3 minoura */
566 1.1.2.3 minoura map->dm_mapsize = 0;
567 1.1.2.3 minoura map->dm_nsegs = 0;
568 1.1.2.3 minoura
569 1.1.2.3 minoura /*
570 1.1.2.3 minoura * Try to load the map the normal way. If this errors out,
571 1.1.2.3 minoura * and we can bounce, we will.
572 1.1.2.3 minoura */
573 1.1.2.3 minoura error = x68k_bus_dmamap_load(t, map, buf, buflen, p, flags);
574 1.1.2.3 minoura if (error == 0 ||
575 1.1.2.3 minoura (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
576 1.1.2.3 minoura return (error);
577 1.1.2.3 minoura
578 1.1.2.3 minoura /*
579 1.1.2.3 minoura * Allocate bounce pages, if necessary.
580 1.1.2.3 minoura */
581 1.1.2.3 minoura if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
582 1.1.2.3 minoura error = _intio_dma_alloc_bouncebuf(t, map, buflen, flags);
583 1.1.2.3 minoura if (error)
584 1.1.2.3 minoura return (error);
585 1.1.2.3 minoura }
586 1.1.2.3 minoura
587 1.1.2.3 minoura /*
588 1.1.2.3 minoura * Cache a pointer to the caller's buffer and load the DMA map
589 1.1.2.3 minoura * with the bounce buffer.
590 1.1.2.3 minoura */
591 1.1.2.3 minoura cookie->id_origbuf = buf;
592 1.1.2.3 minoura cookie->id_origbuflen = buflen;
593 1.1.2.3 minoura cookie->id_buftype = ID_BUFTYPE_LINEAR;
594 1.1.2.3 minoura error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf, buflen,
595 1.1.2.3 minoura p, flags);
596 1.1.2.3 minoura if (error) {
597 1.1.2.3 minoura /*
598 1.1.2.3 minoura * Free the bounce pages, unless our resources
599 1.1.2.3 minoura * are reserved for our exclusive use.
600 1.1.2.3 minoura */
601 1.1.2.3 minoura if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
602 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
603 1.1.2.3 minoura return (error);
604 1.1.2.3 minoura }
605 1.1.2.3 minoura
606 1.1.2.3 minoura /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
607 1.1.2.3 minoura cookie->id_flags |= ID_IS_BOUNCING;
608 1.1.2.3 minoura return (0);
609 1.1.2.3 minoura }
610 1.1.2.3 minoura
611 1.1.2.3 minoura /*
612 1.1.2.3 minoura * Like _intio_bus_dmamap_load(), but for mbufs.
613 1.1.2.3 minoura */
614 1.1.2.3 minoura int
615 1.1.2.3 minoura _intio_bus_dmamap_load_mbuf(t, map, m0, flags)
616 1.1.2.3 minoura bus_dma_tag_t t;
617 1.1.2.3 minoura bus_dmamap_t map;
618 1.1.2.3 minoura struct mbuf *m0;
619 1.1.2.3 minoura int flags;
620 1.1.2.3 minoura {
621 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
622 1.1.2.3 minoura int error;
623 1.1.2.3 minoura
624 1.1.2.3 minoura /*
625 1.1.2.3 minoura * Make sure on error condition we return "no valid mappings."
626 1.1.2.3 minoura */
627 1.1.2.3 minoura map->dm_mapsize = 0;
628 1.1.2.3 minoura map->dm_nsegs = 0;
629 1.1.2.3 minoura
630 1.1.2.3 minoura #ifdef DIAGNOSTIC
631 1.1.2.3 minoura if ((m0->m_flags & M_PKTHDR) == 0)
632 1.1.2.3 minoura panic("_intio_bus_dmamap_load_mbuf: no packet header");
633 1.1.2.3 minoura #endif
634 1.1.2.3 minoura
635 1.1.2.3 minoura if (m0->m_pkthdr.len > map->x68k_dm_size)
636 1.1.2.3 minoura return (EINVAL);
637 1.1.2.3 minoura
638 1.1.2.3 minoura /*
639 1.1.2.3 minoura * Try to load the map the normal way. If this errors out,
640 1.1.2.3 minoura * and we can bounce, we will.
641 1.1.2.3 minoura */
642 1.1.2.3 minoura error = x68k_bus_dmamap_load_mbuf(t, map, m0, flags);
643 1.1.2.3 minoura if (error == 0 ||
644 1.1.2.3 minoura (error != 0 && (cookie->id_flags & ID_MIGHT_NEED_BOUNCE) == 0))
645 1.1.2.3 minoura return (error);
646 1.1.2.3 minoura
647 1.1.2.3 minoura /*
648 1.1.2.3 minoura * Allocate bounce pages, if necessary.
649 1.1.2.3 minoura */
650 1.1.2.3 minoura if ((cookie->id_flags & ID_HAS_BOUNCE) == 0) {
651 1.1.2.3 minoura error = _intio_dma_alloc_bouncebuf(t, map, m0->m_pkthdr.len,
652 1.1.2.3 minoura flags);
653 1.1.2.3 minoura if (error)
654 1.1.2.3 minoura return (error);
655 1.1.2.3 minoura }
656 1.1.2.3 minoura
657 1.1.2.3 minoura /*
658 1.1.2.3 minoura * Cache a pointer to the caller's buffer and load the DMA map
659 1.1.2.3 minoura * with the bounce buffer.
660 1.1.2.3 minoura */
661 1.1.2.3 minoura cookie->id_origbuf = m0;
662 1.1.2.3 minoura cookie->id_origbuflen = m0->m_pkthdr.len; /* not really used */
663 1.1.2.3 minoura cookie->id_buftype = ID_BUFTYPE_MBUF;
664 1.1.2.3 minoura error = x68k_bus_dmamap_load(t, map, cookie->id_bouncebuf,
665 1.1.2.3 minoura m0->m_pkthdr.len, NULL, flags);
666 1.1.2.3 minoura if (error) {
667 1.1.2.3 minoura /*
668 1.1.2.3 minoura * Free the bounce pages, unless our resources
669 1.1.2.3 minoura * are reserved for our exclusive use.
670 1.1.2.3 minoura */
671 1.1.2.3 minoura if ((map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
672 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
673 1.1.2.3 minoura return (error);
674 1.1.2.3 minoura }
675 1.1.2.3 minoura
676 1.1.2.3 minoura /* ...so _intio_bus_dmamap_sync() knows we're bouncing */
677 1.1.2.3 minoura cookie->id_flags |= ID_IS_BOUNCING;
678 1.1.2.3 minoura return (0);
679 1.1.2.3 minoura }
680 1.1.2.3 minoura
681 1.1.2.3 minoura /*
682 1.1.2.3 minoura * Like _intio_bus_dmamap_load(), but for uios.
683 1.1.2.3 minoura */
684 1.1.2.3 minoura int
685 1.1.2.3 minoura _intio_bus_dmamap_load_uio(t, map, uio, flags)
686 1.1.2.3 minoura bus_dma_tag_t t;
687 1.1.2.3 minoura bus_dmamap_t map;
688 1.1.2.3 minoura struct uio *uio;
689 1.1.2.3 minoura int flags;
690 1.1.2.3 minoura {
691 1.1.2.3 minoura panic("_intio_bus_dmamap_load_uio: not implemented");
692 1.1.2.3 minoura }
693 1.1.2.3 minoura
694 1.1.2.3 minoura /*
695 1.1.2.3 minoura * Like _intio_bus_dmamap_load(), but for raw memory allocated with
696 1.1.2.3 minoura * bus_dmamem_alloc().
697 1.1.2.3 minoura */
698 1.1.2.3 minoura int
699 1.1.2.3 minoura _intio_bus_dmamap_load_raw(t, map, segs, nsegs, size, flags)
700 1.1.2.3 minoura bus_dma_tag_t t;
701 1.1.2.3 minoura bus_dmamap_t map;
702 1.1.2.3 minoura bus_dma_segment_t *segs;
703 1.1.2.3 minoura int nsegs;
704 1.1.2.3 minoura bus_size_t size;
705 1.1.2.3 minoura int flags;
706 1.1.2.3 minoura {
707 1.1.2.3 minoura
708 1.1.2.3 minoura panic("_intio_bus_dmamap_load_raw: not implemented");
709 1.1.2.3 minoura }
710 1.1.2.3 minoura
711 1.1.2.3 minoura /*
712 1.1.2.3 minoura * Unload an INTIO DMA map.
713 1.1.2.3 minoura */
714 1.1.2.3 minoura void
715 1.1.2.3 minoura _intio_bus_dmamap_unload(t, map)
716 1.1.2.3 minoura bus_dma_tag_t t;
717 1.1.2.3 minoura bus_dmamap_t map;
718 1.1.2.3 minoura {
719 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
720 1.1.2.3 minoura
721 1.1.2.3 minoura /*
722 1.1.2.3 minoura * If we have bounce pages, free them, unless they're
723 1.1.2.3 minoura * reserved for our exclusive use.
724 1.1.2.3 minoura */
725 1.1.2.3 minoura if ((cookie->id_flags & ID_HAS_BOUNCE) &&
726 1.1.2.3 minoura (map->x68k_dm_flags & BUS_DMA_ALLOCNOW) == 0)
727 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map);
728 1.1.2.3 minoura
729 1.1.2.3 minoura cookie->id_flags &= ~ID_IS_BOUNCING;
730 1.1.2.3 minoura cookie->id_buftype = ID_BUFTYPE_INVALID;
731 1.1.2.3 minoura
732 1.1.2.3 minoura /*
733 1.1.2.3 minoura * Do the generic bits of the unload.
734 1.1.2.3 minoura */
735 1.1.2.3 minoura x68k_bus_dmamap_unload(t, map);
736 1.1.2.3 minoura }
737 1.1.2.3 minoura
738 1.1.2.3 minoura /*
739 1.1.2.3 minoura * Synchronize an INTIO DMA map.
740 1.1.2.3 minoura */
741 1.1.2.3 minoura void
742 1.1.2.3 minoura _intio_bus_dmamap_sync(t, map, offset, len, ops)
743 1.1.2.3 minoura bus_dma_tag_t t;
744 1.1.2.3 minoura bus_dmamap_t map;
745 1.1.2.3 minoura bus_addr_t offset;
746 1.1.2.3 minoura bus_size_t len;
747 1.1.2.3 minoura int ops;
748 1.1.2.3 minoura {
749 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
750 1.1.2.3 minoura
751 1.1.2.3 minoura /*
752 1.1.2.3 minoura * Mixing PRE and POST operations is not allowed.
753 1.1.2.3 minoura */
754 1.1.2.3 minoura if ((ops & (BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE)) != 0 &&
755 1.1.2.3 minoura (ops & (BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE)) != 0)
756 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: mix PRE and POST");
757 1.1.2.3 minoura
758 1.1.2.3 minoura #ifdef DIAGNOSTIC
759 1.1.2.3 minoura if ((ops & (BUS_DMASYNC_PREWRITE|BUS_DMASYNC_POSTREAD)) != 0) {
760 1.1.2.3 minoura if (offset >= map->dm_mapsize)
761 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: bad offset");
762 1.1.2.3 minoura if (len == 0 || (offset + len) > map->dm_mapsize)
763 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: bad length");
764 1.1.2.3 minoura }
765 1.1.2.3 minoura #endif
766 1.1.2.3 minoura
767 1.1.2.3 minoura /*
768 1.1.2.3 minoura * If we're not bouncing, just return; nothing to do.
769 1.1.2.3 minoura */
770 1.1.2.3 minoura if ((cookie->id_flags & ID_IS_BOUNCING) == 0)
771 1.1.2.3 minoura return;
772 1.1.2.3 minoura
773 1.1.2.3 minoura switch (cookie->id_buftype) {
774 1.1.2.3 minoura case ID_BUFTYPE_LINEAR:
775 1.1.2.3 minoura /*
776 1.1.2.3 minoura * Nothing to do for pre-read.
777 1.1.2.3 minoura */
778 1.1.2.3 minoura
779 1.1.2.3 minoura if (ops & BUS_DMASYNC_PREWRITE) {
780 1.1.2.3 minoura /*
781 1.1.2.3 minoura * Copy the caller's buffer to the bounce buffer.
782 1.1.2.3 minoura */
783 1.1.2.3 minoura memcpy((char *)cookie->id_bouncebuf + offset,
784 1.1.2.3 minoura (char *)cookie->id_origbuf + offset, len);
785 1.1.2.3 minoura }
786 1.1.2.3 minoura
787 1.1.2.3 minoura if (ops & BUS_DMASYNC_POSTREAD) {
788 1.1.2.3 minoura /*
789 1.1.2.3 minoura * Copy the bounce buffer to the caller's buffer.
790 1.1.2.3 minoura */
791 1.1.2.3 minoura memcpy((char *)cookie->id_origbuf + offset,
792 1.1.2.3 minoura (char *)cookie->id_bouncebuf + offset, len);
793 1.1.2.3 minoura }
794 1.1.2.3 minoura
795 1.1.2.3 minoura /*
796 1.1.2.3 minoura * Nothing to do for post-write.
797 1.1.2.3 minoura */
798 1.1.2.3 minoura break;
799 1.1.2.3 minoura
800 1.1.2.3 minoura case ID_BUFTYPE_MBUF:
801 1.1.2.3 minoura {
802 1.1.2.3 minoura struct mbuf *m, *m0 = cookie->id_origbuf;
803 1.1.2.3 minoura bus_size_t minlen, moff;
804 1.1.2.3 minoura
805 1.1.2.3 minoura /*
806 1.1.2.3 minoura * Nothing to do for pre-read.
807 1.1.2.3 minoura */
808 1.1.2.3 minoura
809 1.1.2.3 minoura if (ops & BUS_DMASYNC_PREWRITE) {
810 1.1.2.3 minoura /*
811 1.1.2.3 minoura * Copy the caller's buffer to the bounce buffer.
812 1.1.2.3 minoura */
813 1.1.2.3 minoura m_copydata(m0, offset, len,
814 1.1.2.3 minoura (char *)cookie->id_bouncebuf + offset);
815 1.1.2.3 minoura }
816 1.1.2.3 minoura
817 1.1.2.3 minoura if (ops & BUS_DMASYNC_POSTREAD) {
818 1.1.2.3 minoura /*
819 1.1.2.3 minoura * Copy the bounce buffer to the caller's buffer.
820 1.1.2.3 minoura */
821 1.1.2.3 minoura for (moff = offset, m = m0; m != NULL && len != 0;
822 1.1.2.3 minoura m = m->m_next) {
823 1.1.2.3 minoura /* Find the beginning mbuf. */
824 1.1.2.3 minoura if (moff >= m->m_len) {
825 1.1.2.3 minoura moff -= m->m_len;
826 1.1.2.3 minoura continue;
827 1.1.2.3 minoura }
828 1.1.2.3 minoura
829 1.1.2.3 minoura /*
830 1.1.2.3 minoura * Now at the first mbuf to sync; nail
831 1.1.2.3 minoura * each one until we have exhausted the
832 1.1.2.3 minoura * length.
833 1.1.2.3 minoura */
834 1.1.2.3 minoura minlen = len < m->m_len - moff ?
835 1.1.2.3 minoura len : m->m_len - moff;
836 1.1.2.3 minoura
837 1.1.2.3 minoura memcpy(mtod(m, caddr_t) + moff,
838 1.1.2.3 minoura (char *)cookie->id_bouncebuf + offset,
839 1.1.2.3 minoura minlen);
840 1.1.2.3 minoura
841 1.1.2.3 minoura moff = 0;
842 1.1.2.3 minoura len -= minlen;
843 1.1.2.3 minoura offset += minlen;
844 1.1.2.3 minoura }
845 1.1.2.3 minoura }
846 1.1.2.3 minoura
847 1.1.2.3 minoura /*
848 1.1.2.3 minoura * Nothing to do for post-write.
849 1.1.2.3 minoura */
850 1.1.2.3 minoura break;
851 1.1.2.3 minoura }
852 1.1.2.3 minoura
853 1.1.2.3 minoura case ID_BUFTYPE_UIO:
854 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: ID_BUFTYPE_UIO");
855 1.1.2.3 minoura break;
856 1.1.2.3 minoura
857 1.1.2.3 minoura case ID_BUFTYPE_RAW:
858 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: ID_BUFTYPE_RAW");
859 1.1.2.3 minoura break;
860 1.1.2.3 minoura
861 1.1.2.3 minoura case ID_BUFTYPE_INVALID:
862 1.1.2.3 minoura panic("_intio_bus_dmamap_sync: ID_BUFTYPE_INVALID");
863 1.1.2.3 minoura break;
864 1.1.2.3 minoura
865 1.1.2.3 minoura default:
866 1.1.2.3 minoura printf("unknown buffer type %d\n", cookie->id_buftype);
867 1.1.2.3 minoura panic("_intio_bus_dmamap_sync");
868 1.1.2.3 minoura }
869 1.1.2.3 minoura }
870 1.1.2.3 minoura
871 1.1.2.3 minoura /*
872 1.1.2.3 minoura * Allocate memory safe for INTIO DMA.
873 1.1.2.3 minoura */
874 1.1.2.3 minoura int
875 1.1.2.3 minoura _intio_bus_dmamem_alloc(t, size, alignment, boundary, segs, nsegs, rsegs, flags)
876 1.1.2.3 minoura bus_dma_tag_t t;
877 1.1.2.3 minoura bus_size_t size, alignment, boundary;
878 1.1.2.3 minoura bus_dma_segment_t *segs;
879 1.1.2.3 minoura int nsegs;
880 1.1.2.3 minoura int *rsegs;
881 1.1.2.3 minoura int flags;
882 1.1.2.3 minoura {
883 1.1.2.3 minoura paddr_t high;
884 1.1.2.3 minoura extern paddr_t avail_end;
885 1.1.2.3 minoura
886 1.1.2.3 minoura if (avail_end > INTIO_DMA_BOUNCE_THRESHOLD)
887 1.1.2.3 minoura high = trunc_page(INTIO_DMA_BOUNCE_THRESHOLD);
888 1.1.2.3 minoura else
889 1.1.2.3 minoura high = trunc_page(avail_end);
890 1.1.2.3 minoura
891 1.1.2.3 minoura return (x68k_bus_dmamem_alloc_range(t, size, alignment, boundary,
892 1.1.2.3 minoura segs, nsegs, rsegs, flags, 0, high));
893 1.1.2.3 minoura }
894 1.1.2.3 minoura
895 1.1.2.3 minoura /**********************************************************************
896 1.1.2.3 minoura * INTIO DMA utility functions
897 1.1.2.3 minoura **********************************************************************/
898 1.1.2.3 minoura
899 1.1.2.3 minoura int
900 1.1.2.3 minoura _intio_dma_alloc_bouncebuf(t, map, size, flags)
901 1.1.2.3 minoura bus_dma_tag_t t;
902 1.1.2.3 minoura bus_dmamap_t map;
903 1.1.2.3 minoura bus_size_t size;
904 1.1.2.3 minoura int flags;
905 1.1.2.3 minoura {
906 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
907 1.1.2.3 minoura int error = 0;
908 1.1.2.3 minoura
909 1.1.2.3 minoura cookie->id_bouncebuflen = round_page(size);
910 1.1.2.3 minoura error = _intio_bus_dmamem_alloc(t, cookie->id_bouncebuflen,
911 1.1.2.3 minoura NBPG, map->x68k_dm_boundary, cookie->id_bouncesegs,
912 1.1.2.3 minoura map->x68k_dm_segcnt, &cookie->id_nbouncesegs, flags);
913 1.1.2.3 minoura if (error)
914 1.1.2.3 minoura goto out;
915 1.1.2.3 minoura error = x68k_bus_dmamem_map(t, cookie->id_bouncesegs,
916 1.1.2.3 minoura cookie->id_nbouncesegs, cookie->id_bouncebuflen,
917 1.1.2.3 minoura (caddr_t *)&cookie->id_bouncebuf, flags);
918 1.1.2.3 minoura
919 1.1.2.3 minoura out:
920 1.1.2.3 minoura if (error) {
921 1.1.2.3 minoura x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
922 1.1.2.3 minoura cookie->id_nbouncesegs);
923 1.1.2.3 minoura cookie->id_bouncebuflen = 0;
924 1.1.2.3 minoura cookie->id_nbouncesegs = 0;
925 1.1.2.3 minoura } else {
926 1.1.2.3 minoura cookie->id_flags |= ID_HAS_BOUNCE;
927 1.1.2.3 minoura }
928 1.1.2.3 minoura
929 1.1.2.3 minoura return (error);
930 1.1.2.3 minoura }
931 1.1.2.3 minoura
932 1.1.2.3 minoura void
933 1.1.2.3 minoura _intio_dma_free_bouncebuf(t, map)
934 1.1.2.3 minoura bus_dma_tag_t t;
935 1.1.2.3 minoura bus_dmamap_t map;
936 1.1.2.3 minoura {
937 1.1.2.3 minoura struct intio_dma_cookie *cookie = map->x68k_dm_cookie;
938 1.1.2.3 minoura
939 1.1.2.3 minoura x68k_bus_dmamem_unmap(t, cookie->id_bouncebuf,
940 1.1.2.3 minoura cookie->id_bouncebuflen);
941 1.1.2.3 minoura x68k_bus_dmamem_free(t, cookie->id_bouncesegs,
942 1.1.2.3 minoura cookie->id_nbouncesegs);
943 1.1.2.3 minoura cookie->id_bouncebuflen = 0;
944 1.1.2.3 minoura cookie->id_nbouncesegs = 0;
945 1.1.2.3 minoura cookie->id_flags &= ~ID_HAS_BOUNCE;
946 1.1.2.1 minoura }
947