Changeset View
Changeset View
Standalone View
Standalone View
sys/dev/gasket/gasket_core.c
- This file was added.
| /* | |||||
| * Copyright (c) 2026 Abdelkader Boudih <freebsd@seuros.com> | |||||
| * | |||||
| * SPDX-License-Identifier: BSD-2-Clause | |||||
| */ | |||||
| #include <sys/systm.h> | |||||
| #include <sys/conf.h> | |||||
| #include <sys/fcntl.h> | |||||
| #include <sys/lock.h> | |||||
| #include <sys/module.h> | |||||
| #include <sys/proc.h> | |||||
| #include <sys/rwlock.h> | |||||
| #include <sys/sx.h> | |||||
| #include <sys/vnode.h> | |||||
| #include <vm/vm.h> | |||||
| #include <vm/vm_param.h> | |||||
| #include <vm/pmap.h> | |||||
| #include <vm/vm_extern.h> | |||||
| #include <vm/vm_map.h> | |||||
| #include <vm/vm_object.h> | |||||
| #include <vm/vm_page.h> | |||||
| #include <vm/vm_pager.h> | |||||
| #include <dev/gasket/gasket_ioctl.h> | |||||
| #include <dev/gasket/gasket.h> | |||||
| #include <dev/gasket/gasket_page_table.h> | |||||
| #include "gasket_if.h" | |||||
| static MALLOC_DEFINE(M_GASKET, "gasket", "Gasket accelerator framework"); | |||||
| #define GASKET_EVENT_RING 256 | |||||
| struct gasket_softc; | |||||
| /* Lock order: gs_lock -> gpt_lock, vm_map -> VM object; never together. */ | |||||
| struct gasket_maphandle { | |||||
| struct gasket_softc *gm_sc; | |||||
| bool gm_coherent; | |||||
| vm_paddr_t gm_bar_pa; | |||||
| bus_size_t gm_base; | |||||
| const vm_paddr_t *gm_pages; | |||||
| u_int gm_npages; | |||||
| uint64_t gm_gen; | |||||
| }; | |||||
| struct gasket_softc { | |||||
| device_t gs_dev; | |||||
| const struct gasket_desc *gs_desc; | |||||
| struct cdev *gs_cdev; | |||||
| struct sx gs_lock; | |||||
| int gs_opens; | |||||
| bool gs_dying; | |||||
| struct gasket_page_table **gs_page_tables; | |||||
| bus_dma_tag_t gs_coherent_tag; | |||||
| bus_dmamap_t gs_coherent_map; | |||||
| void *gs_coherent_vaddr; | |||||
| vm_paddr_t *gs_coherent_pages; /* CPU side */ | |||||
| bus_addr_t gs_coherent_paddr; /* device */ | |||||
| bus_size_t gs_coherent_size; | |||||
| u_int gs_coherent_npages; | |||||
| uint64_t gs_coherent_gen; | |||||
| u_int gs_coherent_maps; | |||||
| u_int gs_bar_maps; | |||||
| u_int gs_coherent_pt; | |||||
| bool gs_coherent_defer; | |||||
| bool gs_suspended; | |||||
| struct proc *gs_owner; | |||||
| u_int gs_writers; | |||||
| /* Set when a quiesce failed and the mappings had to be retained. */ | |||||
| bool gs_stuck; | |||||
| struct mtx gs_evlock; | |||||
| struct knlist gs_note; | |||||
| struct gasket_interrupt_event *gs_ring; | |||||
| uint64_t *gs_counts; | |||||
| u_int gs_ev_head; | |||||
| u_int gs_ev_tail; | |||||
| uint32_t gs_ev_dropped; | |||||
| bool gs_ev_waiting; | |||||
| }; | |||||
| static int gasket_coherent_alloc(struct gasket_softc *sc, u_int index, | |||||
| bus_size_t size); | |||||
| static const struct cdev_pager_ops gasket_pager_ops; | |||||
| static int gasket_coherent_free(struct gasket_softc *sc); | |||||
| static void gasket_map_release(struct gasket_maphandle *mh); | |||||
| static int gasket_map_buffer(struct gasket_softc *sc, | |||||
| struct gasket_page_table *pt, vm_offset_t host_addr, | |||||
| uint64_t dev_addr, size_t size, vm_prot_t prot); | |||||
| static int gasket_classify_and_hold(struct gasket_softc *sc, | |||||
| vm_offset_t addr, vm_size_t size, vm_prot_t prot, | |||||
| enum gasket_map_kind *kindp, vm_ooffset_t *coh_offp, | |||||
| uint64_t *genp, vm_page_t *pages, u_int npages); | |||||
| static d_open_t gasket_cdev_open; | |||||
| static d_read_t gasket_cdev_read; | |||||
| static d_kqfilter_t gasket_cdev_kqfilter; | |||||
| static d_close_t gasket_cdev_close; | |||||
| static d_ioctl_t gasket_cdev_ioctl; | |||||
| static d_mmap_single_t gasket_cdev_mmap_single; | |||||
| static struct cdevsw gasket_cdevsw = { | |||||
| .d_version = D_VERSION, | |||||
| .d_open = gasket_cdev_open, | |||||
| .d_read = gasket_cdev_read, | |||||
| .d_kqfilter = gasket_cdev_kqfilter, | |||||
| .d_close = gasket_cdev_close, | |||||
| .d_ioctl = gasket_cdev_ioctl, | |||||
| .d_mmap_single = gasket_cdev_mmap_single, | |||||
| .d_flags = D_TRACKCLOSE, | |||||
| .d_name = "gasket", | |||||
| }; | |||||
| int | |||||
| gasket_wait8(struct resource *csr, bus_size_t off, uint64_t mask, uint64_t val, | |||||
| int retries, int delay_ms) | |||||
| { | |||||
| int i; | |||||
| for (i = 0; i < retries; i++) { | |||||
| if ((gasket_rd8(csr, off) & mask) == val) | |||||
| return (0); | |||||
markj: Is there some specific reason this needs to be 0660 instead of 0600? If not I'd prefer to use… | |||||
| pause_sbt("gskwait", mstosbt(delay_ms), 0, C_HARDCLOCK); | |||||
| } | |||||
| return (ETIMEDOUT); | |||||
| } | |||||
| bool | |||||
| gasket_is_open(struct gasket_softc *sc) | |||||
| { | |||||
| bool open; | |||||
| sx_slock(&sc->gs_lock); | |||||
| open = sc->gs_opens > 0; | |||||
| sx_sunlock(&sc->gs_lock); | |||||
| return (open); | |||||
| } | |||||
| int | |||||
| gasket_suspend_begin(struct gasket_softc *sc) | |||||
| { | |||||
| if (sc == NULL) | |||||
| return (0); | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (sc->gs_opens > 0 || sc->gs_coherent_maps != 0 || | |||||
| sc->gs_bar_maps != 0) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EBUSY); | |||||
| } | |||||
| sc->gs_suspended = true; | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (0); | |||||
| } | |||||
| void | |||||
| gasket_resume(struct gasket_softc *sc) | |||||
| { | |||||
| if (sc == NULL) | |||||
| return; | |||||
| sx_xlock(&sc->gs_lock); | |||||
| sc->gs_suspended = false; | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| } | |||||
| static int | |||||
| gasket_cdev_open(struct cdev *cdev, int oflags, int devtype __unused, | |||||
| struct thread *td) | |||||
| { | |||||
| struct gasket_softc *sc = cdev->si_drv1; | |||||
| bool writable = (oflags & FWRITE) != 0; | |||||
| int error = 0; | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (sc->gs_dying) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (ENXIO); | |||||
| } | |||||
| if (sc->gs_suspended) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EBUSY); | |||||
| } | |||||
| if (sc->gs_stuck) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (ENXIO); | |||||
| } | |||||
| if (writable && sc->gs_writers != 0 && | |||||
| sc->gs_owner != td->td_proc) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EBUSY); | |||||
| } | |||||
| if (sc->gs_opens == 0) { | |||||
| error = GASKET_RESET(sc->gs_dev); | |||||
| if (error != 0) { | |||||
| (void)GASKET_ENTER_RESET(sc->gs_dev); | |||||
| } else { | |||||
| error = GASKET_OPEN(sc->gs_dev); | |||||
| if (error != 0) { | |||||
| (void)GASKET_ENTER_RESET(sc->gs_dev); | |||||
| } | |||||
| } | |||||
| } | |||||
| if (error == 0) { | |||||
| if (sc->gs_opens == 0) { | |||||
| mtx_lock(&sc->gs_evlock); | |||||
| sc->gs_ev_head = sc->gs_ev_tail = 0; | |||||
| sc->gs_ev_dropped = 0; | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| } | |||||
| sc->gs_coherent_defer = false; | |||||
| if (writable) { | |||||
| sc->gs_owner = td->td_proc; | |||||
| sc->gs_writers++; | |||||
| } | |||||
| sc->gs_opens++; | |||||
| } | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (error); | |||||
| } | |||||
| static int | |||||
| gasket_cdev_close(struct cdev *cdev, int fflag, int devtype __unused, | |||||
| struct thread *td __unused) | |||||
| { | |||||
| struct gasket_softc *sc = cdev->si_drv1; | |||||
| struct gasket_page_table *pt; | |||||
| u_int i; | |||||
| int error; | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (sc->gs_opens > 0) | |||||
| sc->gs_opens--; | |||||
| if ((fflag & FWRITE) != 0 && sc->gs_writers > 0 && | |||||
| --sc->gs_writers == 0) | |||||
| sc->gs_owner = NULL; | |||||
| if (sc->gs_opens == 0) { | |||||
| if (!sc->gs_dying) { | |||||
| GASKET_CLOSE(sc->gs_dev); | |||||
| error = GASKET_QUIESCE(sc->gs_dev); | |||||
| if (error != 0) { | |||||
| device_printf(sc->gs_dev, | |||||
| "device did not quiesce (%d), resetting\n", | |||||
| error); | |||||
| if (GASKET_RESET(sc->gs_dev) != 0) | |||||
| device_printf(sc->gs_dev, | |||||
| "reset also failed, asking the " | |||||
| "engine anyway\n"); | |||||
| error = GASKET_QUIESCE(sc->gs_dev); | |||||
| } | |||||
| if (error != 0) { | |||||
| device_printf(sc->gs_dev, | |||||
| "device will not stop (%d), retaining its " | |||||
| "mappings and refusing to detach\n", error); | |||||
| sc->gs_stuck = true; | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (0); | |||||
| } | |||||
| (void)GASKET_ENTER_RESET(sc->gs_dev); | |||||
| } | |||||
| for (i = 0; i < sc->gs_desc->gd_npage_tables; i++) { | |||||
| pt = sc->gs_page_tables[i]; | |||||
| gasket_page_table_unmap_all(pt); | |||||
| (void)gasket_page_table_partition(pt, | |||||
| gasket_page_table_num_entries(pt)); | |||||
| } | |||||
| if (gasket_coherent_free(sc) == EBUSY) | |||||
| sc->gs_coherent_defer = true; | |||||
| } | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (0); | |||||
| } | |||||
| static int | |||||
| gasket_cdev_ioctl(struct cdev *cdev, u_long cmd, caddr_t data, int fflag, | |||||
| struct thread *td __unused) | |||||
| { | |||||
| struct gasket_softc *sc = cdev->si_drv1; | |||||
| u_int i; | |||||
| int error; | |||||
| switch (cmd) { | |||||
| case GASKET_IOCTL_NUMBER_PAGE_TABLES: | |||||
| case GASKET_IOCTL_PAGE_TABLE_SIZE: | |||||
| case GASKET_IOCTL_SIMPLE_PAGE_TABLE_SIZE: | |||||
| break; | |||||
| default: | |||||
| if ((fflag & FWRITE) == 0) | |||||
| return (EPERM); | |||||
| break; | |||||
| } | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (sc->gs_dying) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (ENXIO); | |||||
| } | |||||
| switch (cmd) { | |||||
| case GASKET_IOCTL_PAGE_TABLE_SIZE: | |||||
| case GASKET_IOCTL_SIMPLE_PAGE_TABLE_SIZE: { | |||||
| const struct gasket_page_table_ioctl *pti = (const void *)data; | |||||
| if (pti->page_table_index > UINT_MAX) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EINVAL); | |||||
| } | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_PARTITION_PAGE_TABLE: | |||||
| case GASKET_IOCTL_UNMAP_BUFFER: { | |||||
| const struct gasket_page_table_ioctl *pti = (const void *)data; | |||||
| if (pti->page_table_index > UINT_MAX || | |||||
| pti->size > UINT_MAX) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EINVAL); | |||||
| } | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_MAP_BUFFER: { | |||||
| const struct gasket_page_table_ioctl *pti = (const void *)data; | |||||
| if (pti->page_table_index > UINT_MAX || | |||||
| pti->size > UINT_MAX || | |||||
| pti->host_address > (uint64_t)VM_MAX_ADDRESS) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EINVAL); | |||||
| } | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_MAP_BUFFER_FLAGS: { | |||||
| const struct gasket_page_table_ioctl_flags *ptf = | |||||
| (const void *)data; | |||||
| if (ptf->base.page_table_index > UINT_MAX || | |||||
| ptf->base.size > UINT_MAX || | |||||
| ptf->base.host_address > (uint64_t)VM_MAX_ADDRESS) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EINVAL); | |||||
| } | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_CONFIG_COHERENT_ALLOCATOR: { | |||||
| const struct gasket_coherent_alloc_config_ioctl *cfg = | |||||
| (const void *)data; | |||||
| if (cfg->page_table_index >= sc->gs_desc->gd_npage_tables) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EINVAL); | |||||
| } | |||||
| break; | |||||
| } | |||||
| } | |||||
| switch (cmd) { | |||||
| case GASKET_IOCTL_RESET: | |||||
| error = GASKET_QUIESCE(sc->gs_dev); | |||||
| if (error != 0) | |||||
| break; | |||||
| for (i = 0; i < sc->gs_desc->gd_npage_tables; i++) | |||||
| gasket_page_table_unmap_all(sc->gs_page_tables[i]); | |||||
| error = GASKET_RESET(sc->gs_dev); | |||||
| break; | |||||
| case GASKET_IOCTL_NUMBER_PAGE_TABLES: | |||||
| *(uint64_t *)data = sc->gs_desc->gd_npage_tables; | |||||
| error = 0; | |||||
| break; | |||||
| case GASKET_IOCTL_PAGE_TABLE_SIZE: | |||||
| case GASKET_IOCTL_SIMPLE_PAGE_TABLE_SIZE: { | |||||
| struct gasket_page_table_ioctl *pti = (void *)data; | |||||
| struct gasket_page_table *pt; | |||||
| pt = gasket_page_table_get(sc, pti->page_table_index); | |||||
| if (pt == NULL) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| if (cmd == GASKET_IOCTL_SIMPLE_PAGE_TABLE_SIZE) | |||||
| pti->size = gasket_page_table_num_simple_entries(pt); | |||||
| else | |||||
| pti->size = gasket_page_table_num_entries(pt); | |||||
| error = 0; | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_CLEAR_INTERRUPT_COUNTS: | |||||
| mtx_lock(&sc->gs_evlock); | |||||
| memset(sc->gs_counts, 0, sc->gs_desc->gd_ninterrupts * | |||||
| sizeof(*sc->gs_counts)); | |||||
| sc->gs_ev_head = sc->gs_ev_tail = 0; | |||||
| sc->gs_ev_dropped = 0; | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| error = 0; | |||||
| break; | |||||
| case GASKET_IOCTL_PARTITION_PAGE_TABLE: { | |||||
| struct gasket_page_table_ioctl *pti = (void *)data; | |||||
| struct gasket_page_table *pt; | |||||
| pt = gasket_page_table_get(sc, pti->page_table_index); | |||||
| if (pt == NULL) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| error = gasket_page_table_partition(pt, pti->size); | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_CONFIG_COHERENT_ALLOCATOR: { | |||||
| struct gasket_coherent_alloc_config_ioctl *cfg = (void *)data; | |||||
| if (cfg->enable != 0) { | |||||
| error = gasket_coherent_alloc(sc, | |||||
| (u_int)cfg->page_table_index, cfg->size); | |||||
| if (error == 0) { | |||||
| cfg->dma_address = | |||||
| sc->gs_desc->gd_coherent_base; | |||||
| } | |||||
| } else { | |||||
| error = gasket_coherent_free(sc); | |||||
| if (error == 0) | |||||
| cfg->dma_address = 0; | |||||
| } | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_MAP_BUFFER: { | |||||
| struct gasket_page_table_ioctl *pti = (void *)data; | |||||
| struct gasket_page_table *pt; | |||||
| pt = gasket_page_table_get(sc, pti->page_table_index); | |||||
| if (pt == NULL) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| error = gasket_map_buffer(sc, pt, | |||||
| (vm_offset_t)pti->host_address, pti->device_address, | |||||
| pti->size, VM_PROT_READ | VM_PROT_WRITE); | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_MAP_BUFFER_FLAGS: { | |||||
| struct gasket_page_table_ioctl_flags *ptf = (void *)data; | |||||
| struct gasket_page_table *pt; | |||||
| int prot; | |||||
| pt = gasket_page_table_get(sc, ptf->base.page_table_index); | |||||
| if (pt == NULL) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| switch (ptf->flags & GASKET_DMA_NONE) { | |||||
| case GASKET_DMA_BIDIRECTIONAL: | |||||
| prot = VM_PROT_READ | VM_PROT_WRITE; | |||||
| break; | |||||
| case GASKET_DMA_TO_DEVICE: | |||||
| prot = VM_PROT_READ; | |||||
| break; | |||||
| case GASKET_DMA_FROM_DEVICE: | |||||
| prot = VM_PROT_WRITE; | |||||
| break; | |||||
| default: | |||||
| error = EINVAL; | |||||
| goto done; | |||||
| } | |||||
| error = gasket_map_buffer(sc, pt, | |||||
| (vm_offset_t)ptf->base.host_address, | |||||
| ptf->base.device_address, ptf->base.size, prot); | |||||
| break; | |||||
| } | |||||
| case GASKET_IOCTL_UNMAP_BUFFER: { | |||||
| struct gasket_page_table_ioctl *pti = (void *)data; | |||||
| struct gasket_page_table *pt; | |||||
| pt = gasket_page_table_get(sc, pti->page_table_index); | |||||
| if (pt == NULL) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| error = gasket_page_table_unmap(pt, pti->device_address, | |||||
| pti->size); | |||||
| break; | |||||
| } | |||||
| default: | |||||
| error = GASKET_IOCTL(sc->gs_dev, cmd, data, fflag); | |||||
| break; | |||||
| } | |||||
| done: | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (error); | |||||
| } | |||||
| void | |||||
| gasket_interrupt_notify(struct gasket_softc *sc, u_int index) | |||||
| { | |||||
| struct gasket_interrupt_event *ev; | |||||
| u_int next; | |||||
| if (sc == NULL || index >= sc->gs_desc->gd_ninterrupts) | |||||
| return; | |||||
| mtx_lock(&sc->gs_evlock); | |||||
| sc->gs_counts[index]++; | |||||
| next = (sc->gs_ev_head + 1) % GASKET_EVENT_RING; | |||||
| if (next == sc->gs_ev_tail) { | |||||
| sc->gs_ev_dropped++; | |||||
| } else { | |||||
| ev = &sc->gs_ring[sc->gs_ev_head]; | |||||
| ev->gie_interrupt = index; | |||||
| ev->gie_dropped = sc->gs_ev_dropped; | |||||
| ev->gie_count = sc->gs_counts[index]; | |||||
| sc->gs_ev_head = next; | |||||
| sc->gs_ev_dropped = 0; | |||||
| } | |||||
| if (sc->gs_ev_waiting) { | |||||
| sc->gs_ev_waiting = false; | |||||
| wakeup(&sc->gs_ring); | |||||
| } | |||||
| KNOTE_LOCKED(&sc->gs_note, 0); | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| } | |||||
| bool | |||||
| gasket_coherent_bus_addr(struct gasket_softc *sc, bus_addr_t *addrp) | |||||
| { | |||||
| bool present; | |||||
| if (sc == NULL) | |||||
| return (false); | |||||
| sx_slock(&sc->gs_lock); | |||||
| present = sc->gs_coherent_vaddr != NULL; | |||||
| if (present) | |||||
| *addrp = sc->gs_coherent_paddr; | |||||
| sx_sunlock(&sc->gs_lock); | |||||
| return (present); | |||||
| } | |||||
| struct gasket_page_table * | |||||
| gasket_page_table_get(struct gasket_softc *sc, u_int index) | |||||
| { | |||||
| if (sc == NULL || index >= sc->gs_desc->gd_npage_tables) | |||||
| return (NULL); | |||||
| return (sc->gs_page_tables[index]); | |||||
| } | |||||
| static int | |||||
| gasket_cdev_read(struct cdev *cdev, struct uio *uio, int ioflag) | |||||
| { | |||||
| struct gasket_softc *sc = cdev->si_drv1; | |||||
| struct gasket_interrupt_event ev; | |||||
| int error; | |||||
| if (uio->uio_resid < (ssize_t)sizeof(ev)) | |||||
| return (EINVAL); | |||||
| error = 0; | |||||
| mtx_lock(&sc->gs_evlock); | |||||
| while (sc->gs_ev_head == sc->gs_ev_tail) { | |||||
| if (sc->gs_dying) { | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| return (ENXIO); | |||||
| } | |||||
| if ((ioflag & IO_NDELAY) != 0) { | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| return (EWOULDBLOCK); | |||||
| } | |||||
| sc->gs_ev_waiting = true; | |||||
| error = msleep(&sc->gs_ring, &sc->gs_evlock, PCATCH, "gskev", | |||||
| 0); | |||||
| if (error != 0 && error != EWOULDBLOCK) { | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| return (error); | |||||
| } | |||||
| error = 0; | |||||
| } | |||||
| while (sc->gs_ev_head != sc->gs_ev_tail && | |||||
| uio->uio_resid >= (ssize_t)sizeof(ev)) { | |||||
| ev = sc->gs_ring[sc->gs_ev_tail]; | |||||
| sc->gs_ev_tail = (sc->gs_ev_tail + 1) % GASKET_EVENT_RING; | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| error = uiomove(&ev, sizeof(ev), uio); | |||||
| mtx_lock(&sc->gs_evlock); | |||||
| if (error != 0) | |||||
| break; | |||||
| } | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| return (error); | |||||
| } | |||||
| static void | |||||
| gasket_kq_detach(struct knote *kn) | |||||
| { | |||||
| struct gasket_softc *sc = kn->kn_hook; | |||||
| knlist_remove(&sc->gs_note, kn, 0); | |||||
| } | |||||
| static int | |||||
| gasket_kq_event(struct knote *kn, long hint __unused) | |||||
| { | |||||
| struct gasket_softc *sc = kn->kn_hook; | |||||
| u_int pending; | |||||
| pending = (sc->gs_ev_head + GASKET_EVENT_RING - sc->gs_ev_tail) % | |||||
| GASKET_EVENT_RING; | |||||
| kn->kn_data = (int64_t)pending * sizeof(struct gasket_interrupt_event); | |||||
| if (sc->gs_dying) { | |||||
| kn->kn_flags |= EV_EOF; | |||||
| return (1); | |||||
| } | |||||
| return (pending != 0); | |||||
| } | |||||
| static const struct filterops gasket_read_filterops = { | |||||
| .f_isfd = 1, | |||||
| .f_detach = gasket_kq_detach, | |||||
| .f_event = gasket_kq_event, | |||||
| }; | |||||
| static int | |||||
| gasket_cdev_kqfilter(struct cdev *cdev, struct knote *kn) | |||||
| { | |||||
| struct gasket_softc *sc = cdev->si_drv1; | |||||
| if (kn->kn_filter != EVFILT_READ) | |||||
| return (EINVAL); | |||||
| kn->kn_fop = &gasket_read_filterops; | |||||
| kn->kn_hook = sc; | |||||
| knlist_add(&sc->gs_note, kn, 0); | |||||
| return (0); | |||||
| } | |||||
| struct gasket_coherent_load { | |||||
| bus_addr_t gcl_addr; | |||||
| int gcl_error; | |||||
| }; | |||||
| static void | |||||
| gasket_coherent_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) | |||||
| { | |||||
| struct gasket_coherent_load *load = arg; | |||||
| if (error != 0) { | |||||
| load->gcl_error = error; | |||||
| return; | |||||
| } | |||||
| if (nsegs != 1) { | |||||
| load->gcl_error = EFBIG; | |||||
| return; | |||||
| } | |||||
| load->gcl_addr = segs[0].ds_addr; | |||||
| load->gcl_error = 0; | |||||
| } | |||||
| static int | |||||
| gasket_coherent_free(struct gasket_softc *sc) | |||||
| { | |||||
| struct gasket_page_table *pt; | |||||
| sx_assert(&sc->gs_lock, SA_XLOCKED); | |||||
| if (sc->gs_coherent_vaddr == NULL) | |||||
| return (0); | |||||
| if (sc->gs_coherent_maps != 0) | |||||
| return (EBUSY); | |||||
| pt = sc->gs_page_tables[sc->gs_coherent_pt]; | |||||
| gasket_page_table_unmap_coherent(pt); | |||||
| gasket_page_table_set_coherent(pt, NULL, 0, 0); | |||||
| if (sc->gs_coherent_vaddr != NULL) { | |||||
| bus_dmamap_unload(sc->gs_coherent_tag, sc->gs_coherent_map); | |||||
| bus_dmamem_free(sc->gs_coherent_tag, sc->gs_coherent_vaddr, | |||||
| sc->gs_coherent_map); | |||||
| sc->gs_coherent_vaddr = NULL; | |||||
| sc->gs_coherent_paddr = 0; | |||||
| sc->gs_coherent_size = 0; | |||||
| free(sc->gs_coherent_pages, M_GASKET); | |||||
| sc->gs_coherent_pages = NULL; | |||||
| sc->gs_coherent_npages = 0; | |||||
| } | |||||
| if (sc->gs_coherent_tag != NULL) { | |||||
| bus_dma_tag_destroy(sc->gs_coherent_tag); | |||||
| sc->gs_coherent_tag = NULL; | |||||
| } | |||||
| return (0); | |||||
| } | |||||
| static int | |||||
| gasket_coherent_alloc(struct gasket_softc *sc, u_int index, bus_size_t size) | |||||
| { | |||||
| struct gasket_coherent_load load; | |||||
| bus_addr_t addr; | |||||
| u_int i; | |||||
| int error; | |||||
| sx_assert(&sc->gs_lock, SA_XLOCKED); | |||||
| if (size == 0 || size > sc->gs_desc->gd_coherent_size) | |||||
| return (EINVAL); | |||||
| if (sc->gs_coherent_vaddr != NULL) | |||||
| return (EBUSY); | |||||
| size = roundup2(size, PAGE_SIZE); | |||||
| error = bus_dma_tag_create(bus_get_dma_tag(sc->gs_dev), PAGE_SIZE, 0, | |||||
| BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, | |||||
adrianUnsubmitted Not Done Inline Actionsso are all of these devices fine with 64 bit DMA? adrian: so are all of these devices fine with 64 bit DMA? | |||||
| 0, NULL, NULL, &sc->gs_coherent_tag); | |||||
| if (error != 0) | |||||
| return (ENOMEM); | |||||
| error = bus_dmamem_alloc(sc->gs_coherent_tag, &sc->gs_coherent_vaddr, | |||||
| BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, | |||||
| &sc->gs_coherent_map); | |||||
| if (error != 0) { | |||||
| bus_dma_tag_destroy(sc->gs_coherent_tag); | |||||
| sc->gs_coherent_tag = NULL; | |||||
| return (ENOMEM); | |||||
| } | |||||
| /* Zero is a legal bus address, so failure is reported separately. */ | |||||
| load.gcl_addr = 0; | |||||
| load.gcl_error = ENOMEM; | |||||
| error = bus_dmamap_load(sc->gs_coherent_tag, sc->gs_coherent_map, | |||||
| sc->gs_coherent_vaddr, size, gasket_coherent_cb, &load, | |||||
| BUS_DMA_NOWAIT); | |||||
| if (error == 0 && load.gcl_error != 0) | |||||
| error = load.gcl_error; | |||||
| if (error != 0) { | |||||
| if (load.gcl_error == 0) | |||||
| bus_dmamap_unload(sc->gs_coherent_tag, | |||||
| sc->gs_coherent_map); | |||||
| bus_dmamem_free(sc->gs_coherent_tag, sc->gs_coherent_vaddr, | |||||
| sc->gs_coherent_map); | |||||
| bus_dma_tag_destroy(sc->gs_coherent_tag); | |||||
| sc->gs_coherent_tag = NULL; | |||||
| sc->gs_coherent_vaddr = NULL; | |||||
| return (ENOMEM); | |||||
| } | |||||
| addr = load.gcl_addr; | |||||
| sc->gs_coherent_paddr = addr; | |||||
| sc->gs_coherent_size = size; | |||||
| sc->gs_coherent_npages = size / PAGE_SIZE; | |||||
| sc->gs_coherent_gen++; | |||||
| sc->gs_coherent_pages = mallocarray(sc->gs_coherent_npages, | |||||
| sizeof(*sc->gs_coherent_pages), M_GASKET, M_WAITOK | M_ZERO); | |||||
| for (i = 0; i < sc->gs_coherent_npages; i++) { | |||||
| sc->gs_coherent_pages[i] = vtophys((char *)sc->gs_coherent_vaddr | |||||
| + | |||||
| (size_t)i * PAGE_SIZE); | |||||
| } | |||||
| sc->gs_coherent_pt = index; | |||||
| gasket_page_table_set_coherent(sc->gs_page_tables[index], | |||||
| sc->gs_coherent_pages, sc->gs_coherent_npages, addr); | |||||
| return (0); | |||||
| } | |||||
| static bool | |||||
| gasket_region_contains(const struct gasket_desc *desc, vm_ooffset_t offset, | |||||
| vm_size_t size) | |||||
| { | |||||
| const struct gasket_region *gr; | |||||
| u_int i; | |||||
| for (i = 0; i < desc->gd_nregions; i++) { | |||||
| gr = &desc->gd_regions[i]; | |||||
| if (offset >= gr->gr_offset && | |||||
| size <= gr->gr_size && | |||||
| offset - gr->gr_offset <= gr->gr_size - size) | |||||
| return (true); | |||||
| } | |||||
| return (false); | |||||
| } | |||||
| /* | |||||
| * Validated here, not in d_mmap(): the device pager keys its object on the | |||||
| * cdev, so later mmap(2) calls reuse it and never reach d_mmap(). | |||||
| */ | |||||
| static bool | |||||
| gasket_coherent_contains(struct gasket_softc *sc, vm_ooffset_t offset, | |||||
| vm_size_t size) | |||||
| { | |||||
| const bus_size_t base = sc->gs_desc->gd_coherent_base; | |||||
| if (sc->gs_coherent_vaddr == NULL) | |||||
| return (false); | |||||
| return (offset >= base && size <= sc->gs_coherent_size && | |||||
| (bus_size_t)offset - base <= sc->gs_coherent_size - size); | |||||
| } | |||||
| static int | |||||
| gasket_cdev_mmap_single(struct cdev *cdev, vm_ooffset_t *offset, | |||||
| vm_size_t size, struct vm_object **objp, int nprot) | |||||
| { | |||||
| struct gasket_softc *sc = cdev->si_drv1; | |||||
| struct gasket_maphandle *mh; | |||||
| vm_object_t obj; | |||||
| bool coherent; | |||||
| if (size == 0 || (*offset & PAGE_MASK) != 0) | |||||
| return (EINVAL); | |||||
| mh = malloc(sizeof(*mh), M_GASKET, M_WAITOK | M_ZERO); | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (sc->gs_dying) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| free(mh, M_GASKET); | |||||
| return (ENXIO); | |||||
| } | |||||
| mh->gm_sc = sc; | |||||
| if (gasket_region_contains(sc->gs_desc, *offset, size)) { | |||||
| coherent = false; | |||||
| mh->gm_coherent = false; | |||||
| mh->gm_bar_pa = (vm_paddr_t)rman_get_start(sc->gs_desc->gd_csr); | |||||
| sc->gs_bar_maps++; | |||||
| } else if (gasket_coherent_contains(sc, *offset, size)) { | |||||
| coherent = true; | |||||
| mh->gm_coherent = true; | |||||
| mh->gm_base = sc->gs_desc->gd_coherent_base; | |||||
| mh->gm_pages = sc->gs_coherent_pages; | |||||
| mh->gm_npages = sc->gs_coherent_npages; | |||||
| mh->gm_gen = sc->gs_coherent_gen; | |||||
| sc->gs_coherent_maps++; | |||||
| } else { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| free(mh, M_GASKET); | |||||
| return (EINVAL); | |||||
| } | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| obj = cdev_pager_allocate(mh, OBJT_DEVICE, &gasket_pager_ops, size, | |||||
| nprot, *offset, curthread->td_ucred); | |||||
| if (obj == NULL) { | |||||
| gasket_map_release(mh); | |||||
| return (ENXIO); | |||||
| } | |||||
| /* Control registers must not be cached; coherent memory stays WB. */ | |||||
adrianUnsubmitted Not Done Inline ActionsThis means the consumer needs to do two mmaps, right? One for data and one for control/registers, so the correct memory attributes are used? adrian: This means the consumer needs to do two mmaps, right? One for data and one for… | |||||
| if (!coherent) { | |||||
| VM_OBJECT_WLOCK(obj); | |||||
| vm_object_set_memattr(obj, VM_MEMATTR_UNCACHEABLE); | |||||
| VM_OBJECT_WUNLOCK(obj); | |||||
| } | |||||
| *objp = obj; | |||||
| return (0); | |||||
| } | |||||
| static bool | |||||
| gasket_paddr_for_offset(const struct gasket_maphandle *mh, vm_ooffset_t offset, | |||||
| vm_paddr_t *paddr) | |||||
| { | |||||
| u_int idx; | |||||
| if (!mh->gm_coherent) { | |||||
| if (!gasket_region_contains(mh->gm_sc->gs_desc, offset, | |||||
| PAGE_SIZE)) | |||||
| return (false); | |||||
| *paddr = mh->gm_bar_pa + offset; | |||||
| return (true); | |||||
| } | |||||
| if (mh->gm_pages == NULL || offset < mh->gm_base) | |||||
| return (false); | |||||
| idx = (u_int)((offset - mh->gm_base) / PAGE_SIZE); | |||||
| if (idx >= mh->gm_npages) | |||||
| return (false); | |||||
| *paddr = mh->gm_pages[idx]; | |||||
| return (true); | |||||
| } | |||||
| static void | |||||
| gasket_map_release(struct gasket_maphandle *mh) | |||||
| { | |||||
| struct gasket_softc *sc = mh->gm_sc; | |||||
| bool finish_free; | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (mh->gm_coherent) { | |||||
| KASSERT(sc->gs_coherent_maps != 0, | |||||
| ("gasket: coherent mapping count underflow")); | |||||
| sc->gs_coherent_maps--; | |||||
| finish_free = sc->gs_coherent_maps == 0 && | |||||
| sc->gs_coherent_defer; | |||||
| } else { | |||||
| KASSERT(sc->gs_bar_maps != 0, | |||||
| ("gasket: register mapping count underflow")); | |||||
| sc->gs_bar_maps--; | |||||
| finish_free = false; | |||||
| } | |||||
| if (finish_free) { | |||||
| sc->gs_coherent_defer = false; | |||||
| (void)gasket_coherent_free(sc); | |||||
| } | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| free(mh, M_GASKET); | |||||
| } | |||||
| static int | |||||
| gasket_pager_ctor(void *handle __unused, vm_ooffset_t size __unused, | |||||
| vm_prot_t prot __unused, vm_ooffset_t foff __unused, | |||||
| struct ucred *cred __unused, u_short *color) | |||||
| { | |||||
| *color = 0; | |||||
| return (0); | |||||
| } | |||||
| static void | |||||
| gasket_pager_dtor(void *handle) | |||||
| { | |||||
| gasket_map_release(handle); | |||||
| } | |||||
| static int | |||||
| gasket_pager_fault(vm_object_t obj, vm_ooffset_t offset, int prot __unused, | |||||
| vm_page_t *mres) | |||||
| { | |||||
| const struct gasket_maphandle *mh = obj->handle; | |||||
| vm_paddr_t paddr; | |||||
| vm_page_t page; | |||||
| if (!gasket_paddr_for_offset(mh, offset, &paddr)) | |||||
| return (VM_PAGER_FAIL); | |||||
| if (((*mres)->flags & PG_FICTITIOUS) != 0) { | |||||
| vm_page_updatefake(*mres, paddr, obj->memattr); | |||||
| vm_page_valid(*mres); | |||||
| return (VM_PAGER_OK); | |||||
| } | |||||
| VM_OBJECT_WUNLOCK(obj); | |||||
| page = vm_page_getfake(paddr, obj->memattr); | |||||
| VM_OBJECT_WLOCK(obj); | |||||
| vm_page_replace(page, obj, (*mres)->pindex, *mres); | |||||
| *mres = page; | |||||
| vm_page_valid(page); | |||||
| return (VM_PAGER_OK); | |||||
| } | |||||
| static const struct cdev_pager_ops gasket_pager_ops = { | |||||
| .cdev_pg_ctor = gasket_pager_ctor, | |||||
| .cdev_pg_dtor = gasket_pager_dtor, | |||||
| .cdev_pg_fault = gasket_pager_fault, | |||||
| }; | |||||
| static bool | |||||
| gasket_object_is_coherent(struct gasket_softc *sc, vm_object_t obj, | |||||
| uint64_t *genp) | |||||
| { | |||||
| const struct gasket_maphandle *mh; | |||||
| if (obj == NULL || obj->type != OBJT_DEVICE || | |||||
| obj->un_pager.devp.ops != &gasket_pager_ops) | |||||
| return (false); | |||||
| mh = obj->un_pager.devp.handle; | |||||
| if (mh == NULL || mh->gm_sc != sc || !mh->gm_coherent) | |||||
| return (false); | |||||
| if (genp != NULL) | |||||
| *genp = mh->gm_gen; | |||||
| return (true); | |||||
| } | |||||
| /* | |||||
| * Classify from the address space, not from the pages a fault returns: a fake | |||||
| * page is freed by its own pager regardless of wiring. | |||||
| */ | |||||
| static int | |||||
adrianUnsubmitted Not Done Inline ActionsI think this function in particular deserves a doxygen style comment on what it does and entry/exit values. adrian: I think this function in particular deserves a doxygen style comment on what it does and… | |||||
| gasket_classify_range_locked(struct gasket_softc *sc, vm_offset_t addr, | |||||
| vm_size_t size, enum gasket_map_kind *kindp, vm_ooffset_t *coh_offp, | |||||
| uint64_t *genp) | |||||
| { | |||||
| vm_map_t map = &curproc->p_vmspace->vm_map; | |||||
| vm_map_entry_t entry; | |||||
| vm_object_t obj; | |||||
| vm_offset_t end, va; | |||||
| int error = 0; | |||||
| end = addr + size; | |||||
| *kindp = GASKET_MAP_NONE; | |||||
| *coh_offp = 0; | |||||
| if (!vm_map_lookup_entry(map, addr, &entry)) | |||||
| return (EFAULT); | |||||
| /* A submap entry's union holds a vm_map, not a vm_object. */ | |||||
| if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) | |||||
| return (EINVAL); | |||||
| if (gasket_object_is_coherent(sc, entry->object.vm_object, genp)) { | |||||
| if (end > entry->end) | |||||
| return (EINVAL); | |||||
| *kindp = GASKET_MAP_COHERENT; | |||||
| *coh_offp = entry->offset + (addr - entry->start) - | |||||
| sc->gs_desc->gd_coherent_base; | |||||
| return (0); | |||||
| } | |||||
| for (va = addr; va < end; va = entry->end) { | |||||
| if (!vm_map_lookup_entry(map, va, &entry)) { | |||||
| error = EFAULT; | |||||
| break; | |||||
| } | |||||
| if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| obj = entry->object.vm_object; | |||||
| if (gasket_object_is_coherent(sc, obj, NULL)) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| if (obj != NULL && (obj->type == OBJT_DEVICE || | |||||
| obj->type == OBJT_MGTDEVICE || obj->type == OBJT_PHYS || | |||||
| obj->type == OBJT_SG)) { | |||||
| error = EINVAL; | |||||
| break; | |||||
| } | |||||
| } | |||||
| if (error == 0) | |||||
| *kindp = GASKET_MAP_HOST; | |||||
| return (error); | |||||
| } | |||||
| static int | |||||
| gasket_map_buffer(struct gasket_softc *sc, struct gasket_page_table *pt, | |||||
| vm_offset_t host_addr, uint64_t dev_addr, size_t size, vm_prot_t prot) | |||||
| { | |||||
| enum gasket_map_kind kind; | |||||
| vm_ooffset_t coh_off; | |||||
| vm_page_t *pages; | |||||
| uint64_t gen = 0; | |||||
| u_int i, npages; | |||||
| int error; | |||||
| if (size == 0 || (size & PAGE_MASK) != 0 || | |||||
| (host_addr & PAGE_MASK) != 0 || host_addr + size < host_addr) | |||||
| return (EINVAL); | |||||
| npages = (u_int)(size >> PAGE_SHIFT); | |||||
| if ((size >> PAGE_SHIFT) != npages) | |||||
| return (EINVAL); | |||||
| if (npages > gasket_page_table_max_pages(pt)) | |||||
| return (EINVAL); | |||||
| pages = mallocarray(npages, sizeof(*pages), M_GASKET, | |||||
| M_WAITOK | M_ZERO); | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| error = gasket_classify_and_hold(sc, host_addr, size, prot, &kind, | |||||
| &coh_off, &gen, pages, npages); | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (error == 0) { | |||||
| if (sc->gs_dying || sc->gs_stuck) | |||||
| error = ENXIO; | |||||
| else if (kind == GASKET_MAP_COHERENT && | |||||
| (sc->gs_coherent_vaddr == NULL || | |||||
| gen != sc->gs_coherent_gen)) { | |||||
| error = ENXIO; | |||||
| } else | |||||
| error = gasket_page_table_map(pt, host_addr, dev_addr, | |||||
| size, prot, kind, coh_off, pages); | |||||
| for (i = 0; i < npages; i++) { | |||||
| if (pages[i] != NULL) | |||||
| vm_page_unhold_pages(&pages[i], 1); | |||||
| } | |||||
| } | |||||
| free(pages, M_GASKET); | |||||
| return (error); | |||||
| } | |||||
| #define GASKET_HOLD_ATTEMPTS 8 | |||||
| static int | |||||
| gasket_classify_and_hold(struct gasket_softc *sc, vm_offset_t addr, | |||||
| vm_size_t size, vm_prot_t prot, enum gasket_map_kind *kindp, | |||||
| vm_ooffset_t *coh_offp, uint64_t *genp, vm_page_t *pages, u_int npages) | |||||
| { | |||||
| vm_map_t map = &curproc->p_vmspace->vm_map; | |||||
| pmap_t pmap = vmspace_pmap(curproc->p_vmspace); | |||||
| vm_prot_t fault_prot; | |||||
| vm_offset_t va; | |||||
| u_int i; | |||||
| int attempt, error; | |||||
| fault_prot = prot; | |||||
| if ((fault_prot & VM_PROT_WRITE) != 0) | |||||
| fault_prot |= VM_PROT_READ; | |||||
| for (attempt = 0; attempt < GASKET_HOLD_ATTEMPTS; attempt++) { | |||||
| for (va = addr; va < addr + size; va += PAGE_SIZE) { | |||||
| if (vm_fault(map, va, fault_prot, VM_FAULT_NORMAL, | |||||
| NULL) != KERN_SUCCESS) | |||||
| return (EFAULT); | |||||
| } | |||||
| vm_map_lock_read(map); | |||||
| error = gasket_classify_range_locked(sc, addr, size, kindp, | |||||
| coh_offp, genp); | |||||
| if (error != 0) { | |||||
| vm_map_unlock_read(map); | |||||
| return (error); | |||||
| } | |||||
| if (*kindp == GASKET_MAP_COHERENT) { | |||||
| vm_map_unlock_read(map); | |||||
| return (0); | |||||
| } | |||||
| for (i = 0; i < npages; i++) { | |||||
| pages[i] = pmap_extract_and_hold(pmap, | |||||
| addr + (vm_offset_t)i * PAGE_SIZE, fault_prot); | |||||
| if (pages[i] == NULL) | |||||
| break; | |||||
| if ((pages[i]->oflags & VPO_UNMANAGED) != 0 || | |||||
| (pages[i]->flags & PG_FICTITIOUS) != 0) { | |||||
| /* Classified, but not ours to keep. */ | |||||
| vm_page_unhold_pages(pages, i + 1); | |||||
| memset(pages, 0, npages * sizeof(*pages)); | |||||
| vm_map_unlock_read(map); | |||||
| return (EINVAL); | |||||
| } | |||||
| /* DMA leaves no CPU dirty bit behind. */ | |||||
| if ((prot & VM_PROT_WRITE) != 0 && | |||||
| pages[i]->dirty != VM_PAGE_BITS_ALL) | |||||
| vm_page_dirty(pages[i]); | |||||
| } | |||||
| vm_map_unlock_read(map); | |||||
| if (i == npages) | |||||
| return (0); | |||||
| vm_page_unhold_pages(pages, i); | |||||
| memset(pages, 0, npages * sizeof(*pages)); | |||||
| } | |||||
| return (EFAULT); | |||||
| } | |||||
| int | |||||
| gasket_attach(device_t dev, const struct gasket_desc *desc, | |||||
| struct gasket_softc **scp) | |||||
| { | |||||
| struct make_dev_args mda; | |||||
| struct gasket_softc *sc; | |||||
| u_int i; | |||||
| int error; | |||||
| KASSERT(desc->gd_csr != NULL, ("gasket: no control register window")); | |||||
| KASSERT(desc->gd_name != NULL, ("gasket: no device name")); | |||||
| sc = malloc(sizeof(*sc), M_GASKET, M_WAITOK | M_ZERO); | |||||
| sc->gs_dev = dev; | |||||
| sc->gs_desc = desc; | |||||
| sx_init(&sc->gs_lock, "gasket"); | |||||
| mtx_init(&sc->gs_evlock, "gasket ev", NULL, MTX_DEF); | |||||
| knlist_init_mtx(&sc->gs_note, &sc->gs_evlock); | |||||
| sc->gs_ring = mallocarray(GASKET_EVENT_RING, sizeof(*sc->gs_ring), | |||||
| M_GASKET, M_WAITOK | M_ZERO); | |||||
| sc->gs_counts = mallocarray(MAX(desc->gd_ninterrupts, 1), | |||||
| sizeof(*sc->gs_counts), M_GASKET, M_WAITOK | M_ZERO); | |||||
| if (desc->gd_npage_tables != 0) { | |||||
| sc->gs_page_tables = mallocarray(desc->gd_npage_tables, | |||||
| sizeof(*sc->gs_page_tables), M_GASKET, M_WAITOK | M_ZERO); | |||||
| for (i = 0; i < desc->gd_npage_tables; i++) { | |||||
| error = gasket_page_table_init(dev, desc->gd_csr, | |||||
| &desc->gd_page_tables[i], &sc->gs_page_tables[i]); | |||||
| if (error != 0) { | |||||
| device_printf(dev, | |||||
| "cannot init page table %u: %d\n", i, | |||||
| error); | |||||
| goto fail; | |||||
| } | |||||
| } | |||||
| } | |||||
| make_dev_args_init(&mda); | |||||
| mda.mda_devsw = &gasket_cdevsw; | |||||
| mda.mda_unit = device_get_unit(dev); | |||||
| mda.mda_uid = UID_ROOT; | |||||
| mda.mda_gid = GID_WHEEL; | |||||
| mda.mda_mode = 0660; | |||||
| mda.mda_si_drv1 = sc; | |||||
| error = make_dev_s(&mda, &sc->gs_cdev, "%s%d", desc->gd_name, | |||||
| device_get_unit(dev)); | |||||
| if (error != 0) { | |||||
| device_printf(dev, "cannot create device node: %d\n", error); | |||||
| goto fail; | |||||
| } | |||||
| *scp = sc; | |||||
| return (0); | |||||
| fail: | |||||
| for (i = 0; i < desc->gd_npage_tables; i++) { | |||||
| if (sc->gs_page_tables != NULL && | |||||
| sc->gs_page_tables[i] != NULL) | |||||
| gasket_page_table_free(sc->gs_page_tables[i]); | |||||
| } | |||||
| free(sc->gs_page_tables, M_GASKET); | |||||
| free(sc->gs_counts, M_GASKET); | |||||
| free(sc->gs_ring, M_GASKET); | |||||
| knlist_destroy(&sc->gs_note); | |||||
| mtx_destroy(&sc->gs_evlock); | |||||
| sx_destroy(&sc->gs_lock); | |||||
| free(sc, M_GASKET); | |||||
| return (error); | |||||
| } | |||||
| int | |||||
| gasket_detach_begin(struct gasket_softc *sc) | |||||
| { | |||||
| if (sc == NULL) | |||||
| return (0); | |||||
| sx_xlock(&sc->gs_lock); | |||||
| if (sc->gs_opens > 0 || sc->gs_coherent_maps != 0 || | |||||
| sc->gs_bar_maps != 0) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EBUSY); | |||||
| } | |||||
| if (sc->gs_stuck) { | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (EBUSY); | |||||
| } | |||||
| sc->gs_dying = true; | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| return (0); | |||||
| } | |||||
| void | |||||
| gasket_detach_abort(struct gasket_softc *sc) | |||||
| { | |||||
| if (sc == NULL) | |||||
| return; | |||||
| sx_xlock(&sc->gs_lock); | |||||
| sc->gs_dying = false; | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| } | |||||
| void | |||||
| gasket_detach_finish(struct gasket_softc *sc) | |||||
| { | |||||
| u_int i; | |||||
| if (sc == NULL) | |||||
| return; | |||||
| mtx_lock(&sc->gs_evlock); | |||||
| sc->gs_ev_waiting = false; | |||||
| wakeup(&sc->gs_ring); | |||||
| KNOTE_LOCKED(&sc->gs_note, 0); | |||||
| mtx_unlock(&sc->gs_evlock); | |||||
| destroy_dev(sc->gs_cdev); | |||||
| /* Not knlist_detach(): it frees a knlist embedded in the softc. */ | |||||
| knlist_clear(&sc->gs_note, 0); | |||||
| knlist_destroy(&sc->gs_note); | |||||
| sx_xlock(&sc->gs_lock); | |||||
| gasket_coherent_free(sc); | |||||
| sx_xunlock(&sc->gs_lock); | |||||
| for (i = 0; i < sc->gs_desc->gd_npage_tables; i++) | |||||
| gasket_page_table_free(sc->gs_page_tables[i]); | |||||
| free(sc->gs_page_tables, M_GASKET); | |||||
| free(sc->gs_counts, M_GASKET); | |||||
| free(sc->gs_ring, M_GASKET); | |||||
| mtx_destroy(&sc->gs_evlock); | |||||
| sx_destroy(&sc->gs_lock); | |||||
| free(sc, M_GASKET); | |||||
| } | |||||
| static int | |||||
| gasket_modevent(module_t mod __unused, int type, void *data __unused) | |||||
| { | |||||
| switch (type) { | |||||
| case MOD_LOAD: | |||||
| case MOD_UNLOAD: | |||||
| return (0); | |||||
| default: | |||||
| return (EOPNOTSUPP); | |||||
| } | |||||
| } | |||||
| static moduledata_t gasket_mod = { | |||||
| "gasket", | |||||
| gasket_modevent, | |||||
| NULL | |||||
| }; | |||||
| DECLARE_MODULE(gasket, gasket_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST); | |||||
| MODULE_VERSION(gasket, 1); | |||||
Is there some specific reason this needs to be 0660 instead of 0600? If not I'd prefer to use the latter.