2 * Copyright (c) 2015 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <machine/bus.h>
32 #include <machine/bus_dma.h>
33 #include <machine/resource.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
40 #include <sys/queue.h>
46 #include <vm/vm_map.h>
48 #include <dev/proto/proto.h>
49 #include <dev/proto/proto_dev.h>
50 #include <dev/proto/proto_busdma.h>
52 MALLOC_DEFINE(M_PROTO_BUSDMA, "proto_busdma", "DMA management data");
54 #define BNDRY_MIN(a, b) \
55 (((a) == 0) ? (b) : (((b) == 0) ? (a) : MIN((a), (b))))
57 struct proto_callback_bundle {
58 struct proto_busdma *busdma;
60 struct proto_ioc_busdma *ioc;
64 proto_busdma_tag_create(struct proto_busdma *busdma, struct proto_tag *parent,
65 struct proto_ioc_busdma *ioc)
67 struct proto_tag *tag;
69 /* Make sure that when a boundary is specified, it's a power of 2 */
70 if (ioc->u.tag.bndry != 0 &&
71 (ioc->u.tag.bndry & (ioc->u.tag.bndry - 1)) != 0)
75 * If nsegs is 1, ignore maxsegsz. What this means is that if we have
76 * just 1 segment, then maxsz should be equal to maxsegsz. To keep it
77 * simple for us, limit maxsegsz to maxsz in any case.
79 if (ioc->u.tag.maxsegsz > ioc->u.tag.maxsz || ioc->u.tag.nsegs == 1)
80 ioc->u.tag.maxsegsz = ioc->u.tag.maxsz;
82 tag = malloc(sizeof(*tag), M_PROTO_BUSDMA, M_WAITOK | M_ZERO);
85 LIST_INSERT_HEAD(&parent->children, tag, peers);
86 tag->align = MAX(ioc->u.tag.align, parent->align);
87 tag->bndry = BNDRY_MIN(ioc->u.tag.bndry, parent->bndry);
88 tag->maxaddr = MIN(ioc->u.tag.maxaddr, parent->maxaddr);
89 tag->maxsz = MIN(ioc->u.tag.maxsz, parent->maxsz);
90 tag->maxsegsz = MIN(ioc->u.tag.maxsegsz, parent->maxsegsz);
91 tag->nsegs = MIN(ioc->u.tag.nsegs, parent->nsegs);
92 tag->datarate = MIN(ioc->u.tag.datarate, parent->datarate);
93 /* Write constraints back */
94 ioc->u.tag.align = tag->align;
95 ioc->u.tag.bndry = tag->bndry;
96 ioc->u.tag.maxaddr = tag->maxaddr;
97 ioc->u.tag.maxsz = tag->maxsz;
98 ioc->u.tag.maxsegsz = tag->maxsegsz;
99 ioc->u.tag.nsegs = tag->nsegs;
100 ioc->u.tag.datarate = tag->datarate;
102 tag->align = ioc->u.tag.align;
103 tag->bndry = ioc->u.tag.bndry;
104 tag->maxaddr = ioc->u.tag.maxaddr;
105 tag->maxsz = ioc->u.tag.maxsz;
106 tag->maxsegsz = ioc->u.tag.maxsegsz;
107 tag->nsegs = ioc->u.tag.nsegs;
108 tag->datarate = ioc->u.tag.datarate;
110 LIST_INSERT_HEAD(&busdma->tags, tag, tags);
111 ioc->result = (uintptr_t)(void *)tag;
116 proto_busdma_tag_destroy(struct proto_busdma *busdma, struct proto_tag *tag)
119 if (!LIST_EMPTY(&tag->mds))
121 if (!LIST_EMPTY(&tag->children))
124 if (tag->parent != NULL) {
125 LIST_REMOVE(tag, peers);
128 LIST_REMOVE(tag, tags);
129 free(tag, M_PROTO_BUSDMA);
133 static struct proto_tag *
134 proto_busdma_tag_lookup(struct proto_busdma *busdma, u_long key)
136 struct proto_tag *tag;
138 LIST_FOREACH(tag, &busdma->tags, tags) {
139 if ((void *)tag == (void *)key)
146 proto_busdma_md_destroy_internal(struct proto_busdma *busdma,
150 LIST_REMOVE(md, mds);
151 LIST_REMOVE(md, peers);
153 bus_dmamap_unload(md->bd_tag, md->bd_map);
154 if (md->virtaddr != NULL)
155 bus_dmamem_free(md->bd_tag, md->virtaddr, md->bd_map);
157 bus_dmamap_destroy(md->bd_tag, md->bd_map);
158 bus_dma_tag_destroy(md->bd_tag);
159 free(md, M_PROTO_BUSDMA);
164 proto_busdma_mem_alloc_callback(void *arg, bus_dma_segment_t *segs, int nseg,
167 struct proto_callback_bundle *pcb = arg;
169 pcb->ioc->u.md.bus_nsegs = nseg;
170 pcb->ioc->u.md.bus_addr = segs[0].ds_addr;
174 proto_busdma_mem_alloc(struct proto_busdma *busdma, struct proto_tag *tag,
175 struct proto_ioc_busdma *ioc)
177 struct proto_callback_bundle pcb;
181 md = malloc(sizeof(*md), M_PROTO_BUSDMA, M_WAITOK | M_ZERO);
184 error = bus_dma_tag_create(busdma->bd_roottag, tag->align, tag->bndry,
185 tag->maxaddr, BUS_SPACE_MAXADDR, NULL, NULL, tag->maxsz,
186 tag->nsegs, tag->maxsegsz, 0, NULL, NULL, &md->bd_tag);
188 free(md, M_PROTO_BUSDMA);
191 error = bus_dmamem_alloc(md->bd_tag, &md->virtaddr, 0, &md->bd_map);
193 bus_dma_tag_destroy(md->bd_tag);
194 free(md, M_PROTO_BUSDMA);
197 md->physaddr = pmap_kextract((uintptr_t)(md->virtaddr));
201 error = bus_dmamap_load(md->bd_tag, md->bd_map, md->virtaddr,
202 tag->maxsz, proto_busdma_mem_alloc_callback, &pcb, BUS_DMA_NOWAIT);
204 bus_dmamem_free(md->bd_tag, md->virtaddr, md->bd_map);
205 bus_dma_tag_destroy(md->bd_tag);
206 free(md, M_PROTO_BUSDMA);
209 LIST_INSERT_HEAD(&tag->mds, md, peers);
210 LIST_INSERT_HEAD(&busdma->mds, md, mds);
211 ioc->u.md.virt_addr = (uintptr_t)md->virtaddr;
212 ioc->u.md.virt_size = tag->maxsz;
213 ioc->u.md.phys_nsegs = 1;
214 ioc->u.md.phys_addr = md->physaddr;
215 ioc->result = (uintptr_t)(void *)md;
220 proto_busdma_mem_free(struct proto_busdma *busdma, struct proto_md *md)
223 if (md->virtaddr == NULL)
225 return (proto_busdma_md_destroy_internal(busdma, md));
229 proto_busdma_md_create(struct proto_busdma *busdma, struct proto_tag *tag,
230 struct proto_ioc_busdma *ioc)
235 md = malloc(sizeof(*md), M_PROTO_BUSDMA, M_WAITOK | M_ZERO);
238 error = bus_dma_tag_create(busdma->bd_roottag, tag->align, tag->bndry,
239 tag->maxaddr, BUS_SPACE_MAXADDR, NULL, NULL, tag->maxsz,
240 tag->nsegs, tag->maxsegsz, 0, NULL, NULL, &md->bd_tag);
242 free(md, M_PROTO_BUSDMA);
245 error = bus_dmamap_create(md->bd_tag, 0, &md->bd_map);
247 bus_dma_tag_destroy(md->bd_tag);
248 free(md, M_PROTO_BUSDMA);
252 LIST_INSERT_HEAD(&tag->mds, md, peers);
253 LIST_INSERT_HEAD(&busdma->mds, md, mds);
254 ioc->result = (uintptr_t)(void *)md;
259 proto_busdma_md_destroy(struct proto_busdma *busdma, struct proto_md *md)
262 if (md->virtaddr != NULL)
264 return (proto_busdma_md_destroy_internal(busdma, md));
268 proto_busdma_md_load_callback(void *arg, bus_dma_segment_t *segs, int nseg,
269 bus_size_t sz, int error)
271 struct proto_callback_bundle *pcb = arg;
273 pcb->ioc->u.md.bus_nsegs = nseg;
274 pcb->ioc->u.md.bus_addr = segs[0].ds_addr;
278 proto_busdma_md_load(struct proto_busdma *busdma, struct proto_md *md,
279 struct proto_ioc_busdma *ioc, struct thread *td)
281 struct proto_callback_bundle pcb;
287 iov.iov_base = (void *)(uintptr_t)ioc->u.md.virt_addr;
288 iov.iov_len = ioc->u.md.virt_size;
292 uio.uio_resid = iov.iov_len;
293 uio.uio_segflg = UIO_USERSPACE;
294 uio.uio_rw = UIO_READ;
300 error = bus_dmamap_load_uio(md->bd_tag, md->bd_map, &uio,
301 proto_busdma_md_load_callback, &pcb, BUS_DMA_NOWAIT);
305 /* XXX determine *all* physical memory segments */
306 pmap = vmspace_pmap(td->td_proc->p_vmspace);
307 md->physaddr = pmap_extract(pmap, ioc->u.md.virt_addr);
308 ioc->u.md.phys_nsegs = 1; /* XXX */
309 ioc->u.md.phys_addr = md->physaddr;
314 proto_busdma_md_unload(struct proto_busdma *busdma, struct proto_md *md)
319 bus_dmamap_unload(md->bd_tag, md->bd_map);
325 proto_busdma_sync(struct proto_busdma *busdma, struct proto_md *md,
326 struct proto_ioc_busdma *ioc)
331 bus_dmamap_sync(md->bd_tag, md->bd_map, ioc->u.sync.op);
335 static struct proto_md *
336 proto_busdma_md_lookup(struct proto_busdma *busdma, u_long key)
340 LIST_FOREACH(md, &busdma->mds, mds) {
341 if ((void *)md == (void *)key)
347 struct proto_busdma *
348 proto_busdma_attach(struct proto_softc *sc)
350 struct proto_busdma *busdma;
352 busdma = malloc(sizeof(*busdma), M_PROTO_BUSDMA, M_WAITOK | M_ZERO);
357 proto_busdma_detach(struct proto_softc *sc, struct proto_busdma *busdma)
360 proto_busdma_cleanup(sc, busdma);
361 free(busdma, M_PROTO_BUSDMA);
366 proto_busdma_cleanup(struct proto_softc *sc, struct proto_busdma *busdma)
368 struct proto_md *md, *md1;
369 struct proto_tag *tag, *tag1;
371 LIST_FOREACH_SAFE(md, &busdma->mds, mds, md1)
372 proto_busdma_md_destroy_internal(busdma, md);
373 LIST_FOREACH_SAFE(tag, &busdma->tags, tags, tag1)
374 proto_busdma_tag_destroy(busdma, tag);
379 proto_busdma_ioctl(struct proto_softc *sc, struct proto_busdma *busdma,
380 struct proto_ioc_busdma *ioc, struct thread *td)
382 struct proto_tag *tag;
387 switch (ioc->request) {
388 case PROTO_IOC_BUSDMA_TAG_CREATE:
389 busdma->bd_roottag = bus_get_dma_tag(sc->sc_dev);
390 error = proto_busdma_tag_create(busdma, NULL, ioc);
392 case PROTO_IOC_BUSDMA_TAG_DERIVE:
393 tag = proto_busdma_tag_lookup(busdma, ioc->key);
398 error = proto_busdma_tag_create(busdma, tag, ioc);
400 case PROTO_IOC_BUSDMA_TAG_DESTROY:
401 tag = proto_busdma_tag_lookup(busdma, ioc->key);
406 error = proto_busdma_tag_destroy(busdma, tag);
408 case PROTO_IOC_BUSDMA_MEM_ALLOC:
409 tag = proto_busdma_tag_lookup(busdma, ioc->u.md.tag);
414 error = proto_busdma_mem_alloc(busdma, tag, ioc);
416 case PROTO_IOC_BUSDMA_MEM_FREE:
417 md = proto_busdma_md_lookup(busdma, ioc->key);
422 error = proto_busdma_mem_free(busdma, md);
424 case PROTO_IOC_BUSDMA_MD_CREATE:
425 tag = proto_busdma_tag_lookup(busdma, ioc->u.md.tag);
430 error = proto_busdma_md_create(busdma, tag, ioc);
432 case PROTO_IOC_BUSDMA_MD_DESTROY:
433 md = proto_busdma_md_lookup(busdma, ioc->key);
438 error = proto_busdma_md_destroy(busdma, md);
440 case PROTO_IOC_BUSDMA_MD_LOAD:
441 md = proto_busdma_md_lookup(busdma, ioc->key);
446 error = proto_busdma_md_load(busdma, md, ioc, td);
448 case PROTO_IOC_BUSDMA_MD_UNLOAD:
449 md = proto_busdma_md_lookup(busdma, ioc->key);
454 error = proto_busdma_md_unload(busdma, md);
456 case PROTO_IOC_BUSDMA_SYNC:
457 md = proto_busdma_md_lookup(busdma, ioc->key);
462 error = proto_busdma_sync(busdma, md, ioc);
472 proto_busdma_mmap_allowed(struct proto_busdma *busdma, vm_paddr_t physaddr)
476 LIST_FOREACH(md, &busdma->mds, mds) {
477 if (physaddr >= trunc_page(md->physaddr) &&
478 physaddr <= trunc_page(md->physaddr + md->tag->maxsz))