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