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