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