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