Index: head/sys/dev/proto/proto.h =================================================================== --- head/sys/dev/proto/proto.h (revision 349713) +++ head/sys/dev/proto/proto.h (revision 349714) @@ -1,68 +1,70 @@ /*- - * Copyright (c) 2014, 2015 Marcel Moolenaar + * Copyright (c) 2014, 2015, 2019 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _DEV_PROTO_H_ #define _DEV_PROTO_H_ #define PROTO_RES_MAX 16 #define PROTO_RES_UNUSED 0 #define PROTO_RES_PCICFG 10 #define PROTO_RES_BUSDMA 11 struct proto_res { - int r_type; + u_int r_type:8; + u_int r_opened:1; int r_rid; union { struct resource *res; void *busdma; } r_d; u_long r_size; union { void *cookie; struct cdev *cdev; } r_u; - uintptr_t r_opened; }; struct proto_softc { device_t sc_dev; struct proto_res sc_res[PROTO_RES_MAX]; int sc_rescnt; + int sc_opencnt; + struct mtx sc_mtx; }; extern devclass_t proto_devclass; extern char proto_driver_name[]; int proto_add_resource(struct proto_softc *, int, int, struct resource *); int proto_probe(device_t dev, const char *prefix, char ***devnamesp); int proto_attach(device_t dev); int proto_detach(device_t dev); #endif /* _DEV_PROTO_H_ */ Index: head/sys/dev/proto/proto_busdma.c =================================================================== --- head/sys/dev/proto/proto_busdma.c (revision 349713) +++ head/sys/dev/proto/proto_busdma.c (revision 349714) @@ -1,487 +1,506 @@ /*- - * Copyright (c) 2015 Marcel Moolenaar + * Copyright (c) 2015, 2019 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include MALLOC_DEFINE(M_PROTO_BUSDMA, "proto_busdma", "DMA management data"); #define BNDRY_MIN(a, b) \ (((a) == 0) ? (b) : (((b) == 0) ? (a) : MIN((a), (b)))) struct proto_callback_bundle { struct proto_busdma *busdma; struct proto_md *md; struct proto_ioc_busdma *ioc; }; static int proto_busdma_tag_create(struct proto_busdma *busdma, struct proto_tag *parent, struct proto_ioc_busdma *ioc) { struct proto_tag *tag; /* Make sure that when a boundary is specified, it's a power of 2 */ if (ioc->u.tag.bndry != 0 && (ioc->u.tag.bndry & (ioc->u.tag.bndry - 1)) != 0) return (EINVAL); /* * If nsegs is 1, ignore maxsegsz. What this means is that if we have * just 1 segment, then maxsz should be equal to maxsegsz. To keep it * simple for us, limit maxsegsz to maxsz in any case. */ if (ioc->u.tag.maxsegsz > ioc->u.tag.maxsz || ioc->u.tag.nsegs == 1) ioc->u.tag.maxsegsz = ioc->u.tag.maxsz; tag = malloc(sizeof(*tag), M_PROTO_BUSDMA, M_WAITOK | M_ZERO); if (parent != NULL) { tag->parent = parent; LIST_INSERT_HEAD(&parent->children, tag, peers); tag->align = MAX(ioc->u.tag.align, parent->align); tag->bndry = BNDRY_MIN(ioc->u.tag.bndry, parent->bndry); tag->maxaddr = MIN(ioc->u.tag.maxaddr, parent->maxaddr); tag->maxsz = MIN(ioc->u.tag.maxsz, parent->maxsz); tag->maxsegsz = MIN(ioc->u.tag.maxsegsz, parent->maxsegsz); tag->nsegs = MIN(ioc->u.tag.nsegs, parent->nsegs); tag->datarate = MIN(ioc->u.tag.datarate, parent->datarate); /* Write constraints back */ ioc->u.tag.align = tag->align; ioc->u.tag.bndry = tag->bndry; ioc->u.tag.maxaddr = tag->maxaddr; ioc->u.tag.maxsz = tag->maxsz; ioc->u.tag.maxsegsz = tag->maxsegsz; ioc->u.tag.nsegs = tag->nsegs; ioc->u.tag.datarate = tag->datarate; } else { tag->align = ioc->u.tag.align; tag->bndry = ioc->u.tag.bndry; tag->maxaddr = ioc->u.tag.maxaddr; tag->maxsz = ioc->u.tag.maxsz; tag->maxsegsz = ioc->u.tag.maxsegsz; tag->nsegs = ioc->u.tag.nsegs; tag->datarate = ioc->u.tag.datarate; } LIST_INSERT_HEAD(&busdma->tags, tag, tags); ioc->result = (uintptr_t)(void *)tag; return (0); } static int proto_busdma_tag_destroy(struct proto_busdma *busdma, struct proto_tag *tag) { if (!LIST_EMPTY(&tag->mds)) return (EBUSY); if (!LIST_EMPTY(&tag->children)) return (EBUSY); if (tag->parent != NULL) { LIST_REMOVE(tag, peers); tag->parent = NULL; } LIST_REMOVE(tag, tags); free(tag, M_PROTO_BUSDMA); return (0); } static struct proto_tag * proto_busdma_tag_lookup(struct proto_busdma *busdma, u_long key) { struct proto_tag *tag; LIST_FOREACH(tag, &busdma->tags, tags) { if ((void *)tag == (void *)key) return (tag); } return (NULL); } static int proto_busdma_md_destroy_internal(struct proto_busdma *busdma, struct proto_md *md) { LIST_REMOVE(md, mds); LIST_REMOVE(md, peers); if (md->physaddr) bus_dmamap_unload(md->bd_tag, md->bd_map); if (md->virtaddr != NULL) bus_dmamem_free(md->bd_tag, md->virtaddr, md->bd_map); else bus_dmamap_destroy(md->bd_tag, md->bd_map); bus_dma_tag_destroy(md->bd_tag); free(md, M_PROTO_BUSDMA); return (0); } static void proto_busdma_mem_alloc_callback(void *arg, bus_dma_segment_t *segs, int nseg, int error) { struct proto_callback_bundle *pcb = arg; pcb->ioc->u.md.bus_nsegs = nseg; pcb->ioc->u.md.bus_addr = segs[0].ds_addr; } static int proto_busdma_mem_alloc(struct proto_busdma *busdma, struct proto_tag *tag, struct proto_ioc_busdma *ioc) { struct proto_callback_bundle pcb; struct proto_md *md; int error; md = malloc(sizeof(*md), M_PROTO_BUSDMA, M_WAITOK | M_ZERO); md->tag = tag; error = bus_dma_tag_create(busdma->bd_roottag, tag->align, tag->bndry, tag->maxaddr, BUS_SPACE_MAXADDR, NULL, NULL, tag->maxsz, tag->nsegs, tag->maxsegsz, 0, NULL, NULL, &md->bd_tag); if (error) { free(md, M_PROTO_BUSDMA); return (error); } error = bus_dmamem_alloc(md->bd_tag, &md->virtaddr, 0, &md->bd_map); if (error) { bus_dma_tag_destroy(md->bd_tag); free(md, M_PROTO_BUSDMA); return (error); } md->physaddr = pmap_kextract((uintptr_t)(md->virtaddr)); pcb.busdma = busdma; pcb.md = md; pcb.ioc = ioc; error = bus_dmamap_load(md->bd_tag, md->bd_map, md->virtaddr, tag->maxsz, proto_busdma_mem_alloc_callback, &pcb, BUS_DMA_NOWAIT); if (error) { bus_dmamem_free(md->bd_tag, md->virtaddr, md->bd_map); bus_dma_tag_destroy(md->bd_tag); free(md, M_PROTO_BUSDMA); return (error); } LIST_INSERT_HEAD(&tag->mds, md, peers); LIST_INSERT_HEAD(&busdma->mds, md, mds); ioc->u.md.virt_addr = (uintptr_t)md->virtaddr; ioc->u.md.virt_size = tag->maxsz; ioc->u.md.phys_nsegs = 1; ioc->u.md.phys_addr = md->physaddr; ioc->result = (uintptr_t)(void *)md; return (0); } static int proto_busdma_mem_free(struct proto_busdma *busdma, struct proto_md *md) { if (md->virtaddr == NULL) return (ENXIO); return (proto_busdma_md_destroy_internal(busdma, md)); } static int proto_busdma_md_create(struct proto_busdma *busdma, struct proto_tag *tag, struct proto_ioc_busdma *ioc) { struct proto_md *md; int error; md = malloc(sizeof(*md), M_PROTO_BUSDMA, M_WAITOK | M_ZERO); md->tag = tag; error = bus_dma_tag_create(busdma->bd_roottag, tag->align, tag->bndry, tag->maxaddr, BUS_SPACE_MAXADDR, NULL, NULL, tag->maxsz, tag->nsegs, tag->maxsegsz, 0, NULL, NULL, &md->bd_tag); if (error) { free(md, M_PROTO_BUSDMA); return (error); } error = bus_dmamap_create(md->bd_tag, 0, &md->bd_map); if (error) { bus_dma_tag_destroy(md->bd_tag); free(md, M_PROTO_BUSDMA); return (error); } LIST_INSERT_HEAD(&tag->mds, md, peers); LIST_INSERT_HEAD(&busdma->mds, md, mds); ioc->result = (uintptr_t)(void *)md; return (0); } static int proto_busdma_md_destroy(struct proto_busdma *busdma, struct proto_md *md) { if (md->virtaddr != NULL) return (ENXIO); return (proto_busdma_md_destroy_internal(busdma, md)); } static void proto_busdma_md_load_callback(void *arg, bus_dma_segment_t *segs, int nseg, bus_size_t sz, int error) { struct proto_callback_bundle *pcb = arg; pcb->ioc->u.md.bus_nsegs = nseg; pcb->ioc->u.md.bus_addr = segs[0].ds_addr; } static int proto_busdma_md_load(struct proto_busdma *busdma, struct proto_md *md, struct proto_ioc_busdma *ioc, struct thread *td) { struct proto_callback_bundle pcb; struct iovec iov; struct uio uio; pmap_t pmap; int error; iov.iov_base = (void *)(uintptr_t)ioc->u.md.virt_addr; iov.iov_len = ioc->u.md.virt_size; uio.uio_iov = &iov; uio.uio_iovcnt = 1; uio.uio_offset = 0; uio.uio_resid = iov.iov_len; uio.uio_segflg = UIO_USERSPACE; uio.uio_rw = UIO_READ; uio.uio_td = td; pcb.busdma = busdma; pcb.md = md; pcb.ioc = ioc; error = bus_dmamap_load_uio(md->bd_tag, md->bd_map, &uio, proto_busdma_md_load_callback, &pcb, BUS_DMA_NOWAIT); if (error) return (error); /* XXX determine *all* physical memory segments */ pmap = vmspace_pmap(td->td_proc->p_vmspace); md->physaddr = pmap_extract(pmap, ioc->u.md.virt_addr); ioc->u.md.phys_nsegs = 1; /* XXX */ ioc->u.md.phys_addr = md->physaddr; return (0); } static int proto_busdma_md_unload(struct proto_busdma *busdma, struct proto_md *md) { if (!md->physaddr) return (ENXIO); bus_dmamap_unload(md->bd_tag, md->bd_map); md->physaddr = 0; return (0); } static int proto_busdma_sync(struct proto_busdma *busdma, struct proto_md *md, struct proto_ioc_busdma *ioc) { u_int ops; ops = BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE | BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE; if (ioc->u.sync.op & ~ops) return (EINVAL); if (!md->physaddr) return (ENXIO); bus_dmamap_sync(md->bd_tag, md->bd_map, ioc->u.sync.op); return (0); } static struct proto_md * proto_busdma_md_lookup(struct proto_busdma *busdma, u_long key) { struct proto_md *md; LIST_FOREACH(md, &busdma->mds, mds) { if ((void *)md == (void *)key) return (md); } return (NULL); } struct proto_busdma * proto_busdma_attach(struct proto_softc *sc) { struct proto_busdma *busdma; busdma = malloc(sizeof(*busdma), M_PROTO_BUSDMA, M_WAITOK | M_ZERO); + sx_init(&busdma->sxlck, "proto-busdma"); return (busdma); } int proto_busdma_detach(struct proto_softc *sc, struct proto_busdma *busdma) { proto_busdma_cleanup(sc, busdma); + sx_destroy(&busdma->sxlck); free(busdma, M_PROTO_BUSDMA); return (0); } int proto_busdma_cleanup(struct proto_softc *sc, struct proto_busdma *busdma) { struct proto_md *md, *md1; struct proto_tag *tag, *tag1; + sx_xlock(&busdma->sxlck); LIST_FOREACH_SAFE(md, &busdma->mds, mds, md1) proto_busdma_md_destroy_internal(busdma, md); LIST_FOREACH_SAFE(tag, &busdma->tags, tags, tag1) proto_busdma_tag_destroy(busdma, tag); + sx_xunlock(&busdma->sxlck); return (0); } int proto_busdma_ioctl(struct proto_softc *sc, struct proto_busdma *busdma, struct proto_ioc_busdma *ioc, struct thread *td) { struct proto_tag *tag; struct proto_md *md; int error; + sx_xlock(&busdma->sxlck); + error = 0; switch (ioc->request) { case PROTO_IOC_BUSDMA_TAG_CREATE: busdma->bd_roottag = bus_get_dma_tag(sc->sc_dev); error = proto_busdma_tag_create(busdma, NULL, ioc); break; case PROTO_IOC_BUSDMA_TAG_DERIVE: tag = proto_busdma_tag_lookup(busdma, ioc->key); if (tag == NULL) { error = EINVAL; break; } error = proto_busdma_tag_create(busdma, tag, ioc); break; case PROTO_IOC_BUSDMA_TAG_DESTROY: tag = proto_busdma_tag_lookup(busdma, ioc->key); if (tag == NULL) { error = EINVAL; break; } error = proto_busdma_tag_destroy(busdma, tag); break; case PROTO_IOC_BUSDMA_MEM_ALLOC: tag = proto_busdma_tag_lookup(busdma, ioc->u.md.tag); if (tag == NULL) { error = EINVAL; break; } error = proto_busdma_mem_alloc(busdma, tag, ioc); break; case PROTO_IOC_BUSDMA_MEM_FREE: md = proto_busdma_md_lookup(busdma, ioc->key); if (md == NULL) { error = EINVAL; break; } error = proto_busdma_mem_free(busdma, md); break; case PROTO_IOC_BUSDMA_MD_CREATE: tag = proto_busdma_tag_lookup(busdma, ioc->u.md.tag); if (tag == NULL) { error = EINVAL; break; } error = proto_busdma_md_create(busdma, tag, ioc); break; case PROTO_IOC_BUSDMA_MD_DESTROY: md = proto_busdma_md_lookup(busdma, ioc->key); if (md == NULL) { error = EINVAL; break; } error = proto_busdma_md_destroy(busdma, md); break; case PROTO_IOC_BUSDMA_MD_LOAD: md = proto_busdma_md_lookup(busdma, ioc->key); if (md == NULL) { error = EINVAL; break; } error = proto_busdma_md_load(busdma, md, ioc, td); break; case PROTO_IOC_BUSDMA_MD_UNLOAD: md = proto_busdma_md_lookup(busdma, ioc->key); if (md == NULL) { error = EINVAL; break; } error = proto_busdma_md_unload(busdma, md); break; case PROTO_IOC_BUSDMA_SYNC: md = proto_busdma_md_lookup(busdma, ioc->key); if (md == NULL) { error = EINVAL; break; } error = proto_busdma_sync(busdma, md, ioc); break; default: error = EINVAL; break; } + + sx_xunlock(&busdma->sxlck); + return (error); } int proto_busdma_mmap_allowed(struct proto_busdma *busdma, vm_paddr_t physaddr) { struct proto_md *md; + int result; + sx_xlock(&busdma->sxlck); + + result = 0; LIST_FOREACH(md, &busdma->mds, mds) { if (physaddr >= trunc_page(md->physaddr) && - physaddr <= trunc_page(md->physaddr + md->tag->maxsz)) - return (1); + physaddr <= trunc_page(md->physaddr + md->tag->maxsz)) { + result = 1; + break; + } } - return (0); + + sx_xunlock(&busdma->sxlck); + + return (result); } Index: head/sys/dev/proto/proto_busdma.h =================================================================== --- head/sys/dev/proto/proto_busdma.h (revision 349713) +++ head/sys/dev/proto/proto_busdma.h (revision 349714) @@ -1,75 +1,76 @@ /*- - * Copyright (c) 2015 Marcel Moolenaar + * Copyright (c) 2015, 2019 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _DEV_PROTO_BUSDMA_H_ #define _DEV_PROTO_BUSDMA_H_ struct proto_md; struct proto_tag { LIST_ENTRY(proto_tag) tags; struct proto_tag *parent; LIST_ENTRY(proto_tag) peers; LIST_HEAD(,proto_tag) children; LIST_HEAD(,proto_md) mds; bus_addr_t align; bus_addr_t bndry; bus_addr_t maxaddr; bus_size_t maxsz; bus_size_t maxsegsz; u_int nsegs; u_int datarate; }; struct proto_md { LIST_ENTRY(proto_md) mds; LIST_ENTRY(proto_md) peers; struct proto_tag *tag; void *virtaddr; vm_paddr_t physaddr; bus_dma_tag_t bd_tag; bus_dmamap_t bd_map; }; struct proto_busdma { LIST_HEAD(,proto_tag) tags; LIST_HEAD(,proto_md) mds; bus_dma_tag_t bd_roottag; + struct sx sxlck; }; struct proto_busdma *proto_busdma_attach(struct proto_softc *); int proto_busdma_detach(struct proto_softc *, struct proto_busdma *); int proto_busdma_cleanup(struct proto_softc *, struct proto_busdma *); int proto_busdma_ioctl(struct proto_softc *, struct proto_busdma *, struct proto_ioc_busdma *, struct thread *); int proto_busdma_mmap_allowed(struct proto_busdma *, vm_paddr_t); #endif /* _DEV_PROTO_BUSDMA_H_ */ Index: head/sys/dev/proto/proto_core.c =================================================================== --- head/sys/dev/proto/proto_core.c (revision 349713) +++ head/sys/dev/proto/proto_core.c (revision 349714) @@ -1,495 +1,523 @@ /*- - * Copyright (c) 2014, 2015 Marcel Moolenaar + * Copyright (c) 2014, 2015, 2019 Marcel Moolenaar * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include CTASSERT(SYS_RES_IRQ != PROTO_RES_UNUSED && SYS_RES_DRQ != PROTO_RES_UNUSED && SYS_RES_MEMORY != PROTO_RES_UNUSED && SYS_RES_IOPORT != PROTO_RES_UNUSED); CTASSERT(SYS_RES_IRQ != PROTO_RES_PCICFG && SYS_RES_DRQ != PROTO_RES_PCICFG && SYS_RES_MEMORY != PROTO_RES_PCICFG && SYS_RES_IOPORT != PROTO_RES_PCICFG); CTASSERT(SYS_RES_IRQ != PROTO_RES_BUSDMA && SYS_RES_DRQ != PROTO_RES_BUSDMA && SYS_RES_MEMORY != PROTO_RES_BUSDMA && SYS_RES_IOPORT != PROTO_RES_BUSDMA); devclass_t proto_devclass; char proto_driver_name[] = "proto"; static d_open_t proto_open; static d_close_t proto_close; static d_read_t proto_read; static d_write_t proto_write; static d_ioctl_t proto_ioctl; static d_mmap_t proto_mmap; struct cdevsw proto_devsw = { .d_version = D_VERSION, .d_flags = 0, .d_name = proto_driver_name, .d_open = proto_open, .d_close = proto_close, .d_read = proto_read, .d_write = proto_write, .d_ioctl = proto_ioctl, .d_mmap = proto_mmap, }; static MALLOC_DEFINE(M_PROTO, "PROTO", "PROTO driver"); int proto_add_resource(struct proto_softc *sc, int type, int rid, struct resource *res) { struct proto_res *r; if (type == PROTO_RES_UNUSED) return (EINVAL); if (sc->sc_rescnt == PROTO_RES_MAX) return (ENOSPC); r = sc->sc_res + sc->sc_rescnt++; r->r_type = type; r->r_rid = rid; r->r_d.res = res; return (0); } #ifdef notyet static int proto_intr(void *arg) { struct proto_softc *sc = arg; /* XXX TODO */ return (FILTER_HANDLED); } #endif int proto_probe(device_t dev, const char *prefix, char ***devnamesp) { char **devnames = *devnamesp; const char *dn, *ep, *ev; size_t pfxlen; int idx, names; if (devnames == NULL) { pfxlen = strlen(prefix); names = 1; /* NULL pointer */ ev = kern_getenv("hw.proto.attach"); if (ev != NULL) { dn = ev; while (*dn != '\0') { ep = dn; while (*ep != ',' && *ep != '\0') ep++; if ((ep - dn) > pfxlen && strncmp(dn, prefix, pfxlen) == 0) names++; dn = (*ep == ',') ? ep + 1 : ep; } } devnames = malloc(names * sizeof(caddr_t), M_DEVBUF, M_WAITOK | M_ZERO); *devnamesp = devnames; if (ev != NULL) { dn = ev; idx = 0; while (*dn != '\0') { ep = dn; while (*ep != ',' && *ep != '\0') ep++; if ((ep - dn) > pfxlen && strncmp(dn, prefix, pfxlen) == 0) { devnames[idx] = malloc(ep - dn + 1, M_DEVBUF, M_WAITOK | M_ZERO); memcpy(devnames[idx], dn, ep - dn); idx++; } dn = (*ep == ',') ? ep + 1 : ep; } freeenv(__DECONST(char *, ev)); } } dn = device_get_desc(dev); while (*devnames != NULL) { if (strcmp(dn, *devnames) == 0) return (BUS_PROBE_SPECIFIC); devnames++; } return (BUS_PROBE_HOOVER); } int proto_attach(device_t dev) { struct proto_softc *sc; struct proto_res *r; u_int res; sc = device_get_softc(dev); sc->sc_dev = dev; + mtx_init(&sc->sc_mtx, "proto-softc", NULL, MTX_DEF); for (res = 0; res < sc->sc_rescnt; res++) { r = sc->sc_res + res; switch (r->r_type) { case SYS_RES_IRQ: /* XXX TODO */ break; case SYS_RES_DRQ: break; case SYS_RES_MEMORY: case SYS_RES_IOPORT: r->r_size = rman_get_size(r->r_d.res); r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600, "proto/%s/%02x.%s", device_get_desc(dev), r->r_rid, (r->r_type == SYS_RES_IOPORT) ? "io" : "mem"); r->r_u.cdev->si_drv1 = sc; r->r_u.cdev->si_drv2 = r; break; case PROTO_RES_PCICFG: r->r_size = 4096; r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600, "proto/%s/pcicfg", device_get_desc(dev)); r->r_u.cdev->si_drv1 = sc; r->r_u.cdev->si_drv2 = r; break; case PROTO_RES_BUSDMA: r->r_d.busdma = proto_busdma_attach(sc); r->r_size = 0; /* no read(2) nor write(2) */ r->r_u.cdev = make_dev(&proto_devsw, res, 0, 0, 0600, "proto/%s/busdma", device_get_desc(dev)); r->r_u.cdev->si_drv1 = sc; r->r_u.cdev->si_drv2 = r; break; } } return (0); } int proto_detach(device_t dev) { struct proto_softc *sc; struct proto_res *r; u_int res; sc = device_get_softc(dev); - /* Don't detach if we have open device files. */ - for (res = 0; res < sc->sc_rescnt; res++) { - r = sc->sc_res + res; - if (r->r_opened) - return (EBUSY); - } + mtx_lock(&sc->sc_mtx); + if (sc->sc_opencnt == 0) + sc->sc_opencnt = -1; + mtx_unlock(&sc->sc_mtx); + if (sc->sc_opencnt > 0) + return (EBUSY); for (res = 0; res < sc->sc_rescnt; res++) { r = sc->sc_res + res; + switch (r->r_type) { case SYS_RES_IRQ: /* XXX TODO */ bus_release_resource(dev, r->r_type, r->r_rid, r->r_d.res); break; case SYS_RES_DRQ: bus_release_resource(dev, r->r_type, r->r_rid, r->r_d.res); break; case SYS_RES_MEMORY: case SYS_RES_IOPORT: + destroy_dev(r->r_u.cdev); bus_release_resource(dev, r->r_type, r->r_rid, r->r_d.res); - destroy_dev(r->r_u.cdev); break; case PROTO_RES_PCICFG: destroy_dev(r->r_u.cdev); break; case PROTO_RES_BUSDMA: - proto_busdma_detach(sc, r->r_d.busdma); destroy_dev(r->r_u.cdev); + proto_busdma_detach(sc, r->r_d.busdma); break; } r->r_type = PROTO_RES_UNUSED; } + mtx_lock(&sc->sc_mtx); sc->sc_rescnt = 0; + sc->sc_opencnt = 0; + mtx_unlock(&sc->sc_mtx); + mtx_destroy(&sc->sc_mtx); return (0); } /* * Device functions */ static int proto_open(struct cdev *cdev, int oflags, int devtype, struct thread *td) { struct proto_res *r; + struct proto_softc *sc; + int error; - r = cdev->si_drv2; - if (!atomic_cmpset_acq_ptr(&r->r_opened, 0UL, (uintptr_t)td->td_proc)) - return (EBUSY); - return (0); + sc = cdev->si_drv1; + mtx_lock(&sc->sc_mtx); + if (sc->sc_opencnt >= 0) { + r = cdev->si_drv2; + if (!r->r_opened) { + r->r_opened = 1; + sc->sc_opencnt++; + error = 0; + } else + error = EBUSY; + } else + error = ENXIO; + mtx_unlock(&sc->sc_mtx); + return (error); } static int proto_close(struct cdev *cdev, int fflag, int devtype, struct thread *td) { struct proto_res *r; struct proto_softc *sc; + int error; sc = cdev->si_drv1; - r = cdev->si_drv2; - if (!atomic_cmpset_acq_ptr(&r->r_opened, (uintptr_t)td->td_proc, 0UL)) - return (ENXIO); - if (r->r_type == PROTO_RES_BUSDMA) - proto_busdma_cleanup(sc, r->r_d.busdma); - return (0); + mtx_lock(&sc->sc_mtx); + if (sc->sc_opencnt > 0) { + r = cdev->si_drv2; + if (r->r_opened) { + if (r->r_type == PROTO_RES_BUSDMA) + proto_busdma_cleanup(sc, r->r_d.busdma); + r->r_opened = 0; + sc->sc_opencnt--; + error = 0; + } else + error = ENXIO; + } else + error = ENXIO; + mtx_unlock(&sc->sc_mtx); + return (error); } static int proto_read(struct cdev *cdev, struct uio *uio, int ioflag) { union { uint8_t x1[8]; uint16_t x2[4]; uint32_t x4[2]; uint64_t x8[1]; } buf; struct proto_softc *sc; struct proto_res *r; device_t dev; off_t ofs; u_long width; int error; sc = cdev->si_drv1; dev = sc->sc_dev; r = cdev->si_drv2; width = uio->uio_resid; if (width < 1 || width > 8 || bitcount16(width) > 1) return (EIO); ofs = uio->uio_offset; if (ofs + width > r->r_size) return (EIO); switch (width) { case 1: buf.x1[0] = (r->r_type == PROTO_RES_PCICFG) ? pci_read_config(dev, ofs, 1) : bus_read_1(r->r_d.res, ofs); break; case 2: buf.x2[0] = (r->r_type == PROTO_RES_PCICFG) ? pci_read_config(dev, ofs, 2) : bus_read_2(r->r_d.res, ofs); break; case 4: buf.x4[0] = (r->r_type == PROTO_RES_PCICFG) ? pci_read_config(dev, ofs, 4) : bus_read_4(r->r_d.res, ofs); break; #ifndef __i386__ case 8: if (r->r_type == PROTO_RES_PCICFG) return (EINVAL); buf.x8[0] = bus_read_8(r->r_d.res, ofs); break; #endif default: return (EIO); } error = uiomove(&buf, width, uio); return (error); } static int proto_write(struct cdev *cdev, struct uio *uio, int ioflag) { union { uint8_t x1[8]; uint16_t x2[4]; uint32_t x4[2]; uint64_t x8[1]; } buf; struct proto_softc *sc; struct proto_res *r; device_t dev; off_t ofs; u_long width; int error; sc = cdev->si_drv1; dev = sc->sc_dev; r = cdev->si_drv2; width = uio->uio_resid; if (width < 1 || width > 8 || bitcount16(width) > 1) return (EIO); ofs = uio->uio_offset; if (ofs + width > r->r_size) return (EIO); error = uiomove(&buf, width, uio); if (error) return (error); switch (width) { case 1: if (r->r_type == PROTO_RES_PCICFG) pci_write_config(dev, ofs, buf.x1[0], 1); else bus_write_1(r->r_d.res, ofs, buf.x1[0]); break; case 2: if (r->r_type == PROTO_RES_PCICFG) pci_write_config(dev, ofs, buf.x2[0], 2); else bus_write_2(r->r_d.res, ofs, buf.x2[0]); break; case 4: if (r->r_type == PROTO_RES_PCICFG) pci_write_config(dev, ofs, buf.x4[0], 4); else bus_write_4(r->r_d.res, ofs, buf.x4[0]); break; #ifndef __i386__ case 8: if (r->r_type == PROTO_RES_PCICFG) return (EINVAL); bus_write_8(r->r_d.res, ofs, buf.x8[0]); break; #endif default: return (EIO); } return (0); } static int proto_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, struct thread *td) { struct proto_ioc_region *region; struct proto_ioc_busdma *busdma; struct proto_res *r; struct proto_softc *sc; int error; sc = cdev->si_drv1; r = cdev->si_drv2; error = 0; switch (cmd) { case PROTO_IOC_REGION: if (r->r_type == PROTO_RES_BUSDMA) { error = EINVAL; break; } region = (struct proto_ioc_region *)data; region->size = r->r_size; if (r->r_type == PROTO_RES_PCICFG) region->address = 0; else region->address = rman_get_start(r->r_d.res); break; case PROTO_IOC_BUSDMA: if (r->r_type != PROTO_RES_BUSDMA) { error = EINVAL; break; } busdma = (struct proto_ioc_busdma *)data; error = proto_busdma_ioctl(sc, r->r_d.busdma, busdma, td); break; default: error = ENOIOCTL; break; } return (error); } static int proto_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr, int prot, vm_memattr_t *memattr) { struct proto_res *r; if (offset & PAGE_MASK) return (EINVAL); if (prot & PROT_EXEC) return (EACCES); r = cdev->si_drv2; switch (r->r_type) { case SYS_RES_MEMORY: if (offset >= r->r_size) return (EINVAL); *paddr = rman_get_start(r->r_d.res) + offset; #ifndef __sparc64__ *memattr = VM_MEMATTR_UNCACHEABLE; #endif break; case PROTO_RES_BUSDMA: if (!proto_busdma_mmap_allowed(r->r_d.busdma, offset)) return (EINVAL); *paddr = offset; break; default: return (ENXIO); } return (0); }