123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 |
- /*-
- * SPDX-License-Identifier: BSD-2-Clause
- *
- * Copyright (c) 2019-2024 Ruslan Bukin <br@bsdpad.com>
- *
- * This software was developed by SRI International and the University of
- * Cambridge Computer Laboratory (Department of Computer Science and
- * Technology) under DARPA contract HR0011-18-C-0016 ("ECATS"), as part of the
- * DARPA SSITH research programme.
- *
- * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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.
- */
- /*
- * Intel Stratix 10 FPGA Manager.
- *
- * FPGA Programming Example:
- * dd if=cheri.core.rbf of=/dev/fpga_partial0 bs=512k
- */
- #include <sys/param.h>
- #include <sys/systm.h>
- #include <sys/bus.h>
- #include <sys/kernel.h>
- #include <sys/module.h>
- #include <sys/malloc.h>
- #include <sys/rman.h>
- #include <sys/timeet.h>
- #include <sys/timetc.h>
- #include <sys/conf.h>
- #include <sys/uio.h>
- #include <sys/sx.h>
- #include <dev/ofw/openfirm.h>
- #include <dev/ofw/ofw_bus.h>
- #include <dev/ofw/ofw_bus_subr.h>
- #include <arm64/intel/stratix10-svc.h>
- #include <machine/bus.h>
- #include <machine/cpu.h>
- #include <machine/intr.h>
- #define SVC_BUF_SIZE (512 * 1024)
- struct fpgamgr_s10_softc {
- struct cdev *mgr_cdev;
- struct cdev *mgr_cdev_partial;
- device_t dev;
- device_t s10_svc_dev;
- struct s10_svc_mem mem;
- struct sx sx;
- int opened;
- };
- static int
- fpga_open(struct cdev *dev, int flags __unused,
- int fmt __unused, struct thread *td __unused)
- {
- struct fpgamgr_s10_softc *sc;
- struct s10_svc_msg msg;
- int ret;
- int err;
- sc = dev->si_drv1;
- sx_xlock(&sc->sx);
- if (sc->opened) {
- sx_xunlock(&sc->sx);
- return (EBUSY);
- }
- err = s10_svc_allocate_memory(sc->s10_svc_dev,
- &sc->mem, SVC_BUF_SIZE);
- if (err != 0) {
- sx_xunlock(&sc->sx);
- return (ENXIO);
- }
- bzero(&msg, sizeof(struct s10_svc_msg));
- msg.command = COMMAND_RECONFIG;
- if (dev == sc->mgr_cdev_partial)
- msg.flags |= COMMAND_RECONFIG_FLAG_PARTIAL;
- ret = s10_svc_send(sc->s10_svc_dev, &msg);
- if (ret != 0) {
- sx_xunlock(&sc->sx);
- return (ENXIO);
- }
- sc->opened = 1;
- sx_xunlock(&sc->sx);
- return (0);
- }
- static int
- fpga_submit(struct cdev *dev)
- {
- struct fpgamgr_s10_softc *sc;
- struct s10_svc_msg msg;
- int ret;
- sc = dev->si_drv1;
- bzero(&msg, sizeof(struct s10_svc_msg));
- msg.command = COMMAND_RECONFIG_DATA_SUBMIT;
- msg.payload = (void *)sc->mem.paddr;
- msg.payload_length = sc->mem.fill;
- ret = s10_svc_send(sc->s10_svc_dev, &msg);
- if (ret != 0) {
- device_printf(sc->dev, "Failed to submit data\n");
- s10_svc_free_memory(sc->s10_svc_dev, &sc->mem);
- sc->opened = 0;
- return (ENXIO);
- }
- /* Claim memory buffer back. */
- bzero(&msg, sizeof(struct s10_svc_msg));
- msg.command = COMMAND_RECONFIG_DATA_CLAIM;
- ret = s10_svc_send(sc->s10_svc_dev, &msg);
- if (ret)
- device_printf(sc->dev, "Can't claim buffer back.\n");
- return (ret);
- }
- static int
- fpga_write(struct cdev *dev, struct uio *uio, int ioflag)
- {
- struct fpgamgr_s10_softc *sc;
- vm_offset_t addr;
- int error;
- int amnt;
- int ret;
- sc = dev->si_drv1;
- sx_xlock(&sc->sx);
- if (sc->opened == 0) {
- /* Device closed. */
- sx_xunlock(&sc->sx);
- return (ENXIO);
- }
- while (uio->uio_resid > 0) {
- addr = sc->mem.vaddr + sc->mem.fill;
- amnt = MIN(uio->uio_resid, (SVC_BUF_SIZE - sc->mem.fill));
- error = uiomove((void *)addr, amnt, uio);
- if (error) {
- device_printf(sc->dev, "uiomove returned error %d\n",
- error);
- break;
- }
- sc->mem.fill += amnt;
- if (sc->mem.fill == SVC_BUF_SIZE) {
- ret = fpga_submit(dev);
- if (ret) {
- sx_xunlock(&sc->sx);
- return (ret);
- }
- sc->mem.fill = 0;
- }
- }
- sx_xunlock(&sc->sx);
- return (0);
- }
- static int
- fpga_close(struct cdev *dev, int flags __unused,
- int fmt __unused, struct thread *td __unused)
- {
- struct fpgamgr_s10_softc *sc;
- int ret;
- sc = dev->si_drv1;
- sx_xlock(&sc->sx);
- if (sc->opened == 0) {
- /* Device closed. */
- sx_xunlock(&sc->sx);
- return (ENXIO);
- }
- if (sc->mem.fill > 0) {
- ret = fpga_submit(dev);
- if (ret) {
- sx_xunlock(&sc->sx);
- return (ret);
- }
- sc->mem.fill = 0;
- }
- s10_svc_free_memory(sc->s10_svc_dev, &sc->mem);
- sc->opened = 0;
- sx_xunlock(&sc->sx);
- return (0);
- }
- static int
- fpga_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
- struct thread *td)
- {
- return (0);
- }
- static struct cdevsw fpga_cdevsw = {
- .d_version = D_VERSION,
- .d_open = fpga_open,
- .d_close = fpga_close,
- .d_write = fpga_write,
- .d_ioctl = fpga_ioctl,
- .d_name = "FPGA Manager",
- };
- static int
- fpgamgr_s10_probe(device_t dev)
- {
- if (!ofw_bus_status_okay(dev))
- return (ENXIO);
- if (!ofw_bus_is_compatible(dev, "intel,stratix10-soc-fpga-mgr"))
- return (ENXIO);
- device_set_desc(dev, "Stratix 10 SOC FPGA Manager");
- return (BUS_PROBE_DEFAULT);
- }
- static int
- fpgamgr_s10_attach(device_t dev)
- {
- struct fpgamgr_s10_softc *sc;
- devclass_t dc;
- sc = device_get_softc(dev);
- sc->dev = dev;
- dc = devclass_find("s10_svc");
- if (dc == NULL)
- return (ENXIO);
- sc->s10_svc_dev = devclass_get_device(dc, 0);
- if (sc->s10_svc_dev == NULL)
- return (ENXIO);
- sc->mgr_cdev = make_dev(&fpga_cdevsw, 0, UID_ROOT, GID_WHEEL,
- 0600, "fpga%d", device_get_unit(sc->dev));
- if (sc->mgr_cdev == NULL) {
- device_printf(dev, "Failed to create character device.\n");
- return (ENXIO);
- }
- sc->mgr_cdev_partial = make_dev(&fpga_cdevsw, 0, UID_ROOT, GID_WHEEL,
- 0600, "fpga_partial%d", device_get_unit(sc->dev));
- if (sc->mgr_cdev_partial == NULL) {
- device_printf(dev, "Failed to create character device.\n");
- return (ENXIO);
- }
- sx_init(&sc->sx, "s10 fpga");
- sc->mgr_cdev->si_drv1 = sc;
- sc->mgr_cdev_partial->si_drv1 = sc;
- return (0);
- }
- static int
- fpgamgr_s10_detach(device_t dev)
- {
- struct fpgamgr_s10_softc *sc;
- sc = device_get_softc(dev);
- destroy_dev(sc->mgr_cdev);
- destroy_dev(sc->mgr_cdev_partial);
- sx_destroy(&sc->sx);
- return (0);
- }
- static device_method_t fpgamgr_s10_methods[] = {
- DEVMETHOD(device_probe, fpgamgr_s10_probe),
- DEVMETHOD(device_attach, fpgamgr_s10_attach),
- DEVMETHOD(device_detach, fpgamgr_s10_detach),
- { 0, 0 }
- };
- static driver_t fpgamgr_s10_driver = {
- "fpgamgr_s10",
- fpgamgr_s10_methods,
- sizeof(struct fpgamgr_s10_softc),
- };
- DRIVER_MODULE(fpgamgr_s10, simplebus, fpgamgr_s10_driver, 0, 0);
|