1 1.4 skrll /* $NetBSD: virtio_mainbus.c,v 1.4 2021/11/03 07:53:56 skrll Exp $ */ 2 1.1 reinoud 3 1.1 reinoud /* 4 1.1 reinoud * Copyright (c) 2021 The NetBSD Foundation, Inc. 5 1.1 reinoud * All rights reserved. 6 1.1 reinoud * 7 1.1 reinoud * This code is derived from software contributed to The NetBSD Foundation 8 1.1 reinoud * by Reinoud Zandijk 9 1.1 reinoud * 10 1.1 reinoud * Redistribution and use in source and binary forms, with or without 11 1.1 reinoud * modification, are permitted provided that the following conditions 12 1.1 reinoud * are met: 13 1.1 reinoud * 1. Redistributions of source code must retain the above copyright 14 1.1 reinoud * notice, this list of conditions and the following disclaimer. 15 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 reinoud * notice, this list of conditions and the following disclaimer in the 17 1.1 reinoud * documentation and/or other materials provided with the distribution. 18 1.1 reinoud * 19 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 1.1 reinoud * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 reinoud * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 reinoud * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 23 1.1 reinoud * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 1.1 reinoud * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 1.1 reinoud * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 26 1.1 reinoud * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 1.1 reinoud * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 28 1.1 reinoud * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 29 1.1 reinoud * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 1.1 reinoud */ 31 1.1 reinoud 32 1.1 reinoud #include <sys/cdefs.h> 33 1.4 skrll __KERNEL_RCSID(0, "$NetBSD: virtio_mainbus.c,v 1.4 2021/11/03 07:53:56 skrll Exp $"); 34 1.1 reinoud 35 1.1 reinoud #include <sys/param.h> 36 1.1 reinoud #include <sys/systm.h> 37 1.1 reinoud 38 1.1 reinoud #include <sys/device.h> 39 1.1 reinoud #include <evbmips/mipssim/autoconf.h> 40 1.1 reinoud #include <evbmips/mipssim/mipssimreg.h> 41 1.1 reinoud 42 1.1 reinoud #define VIRTIO_PRIVATE 43 1.1 reinoud #include <dev/virtio/virtio_mmiovar.h> 44 1.1 reinoud 45 1.1 reinoud 46 1.1 reinoud static int virtio_mainbus_match(device_t, cfdata_t, void *); 47 1.1 reinoud static void virtio_mainbus_attach(device_t, device_t, void *); 48 1.1 reinoud static int virtio_mainbus_rescan(device_t, const char *, const int *); 49 1.1 reinoud static int virtio_mainbus_detach(device_t, int); 50 1.1 reinoud 51 1.4 skrll static int virtio_mainbus_alloc_interrupts(struct virtio_mmio_softc *); 52 1.1 reinoud static void virtio_mainbus_free_interrupts(struct virtio_mmio_softc *); 53 1.1 reinoud 54 1.1 reinoud struct virtio_mainbus_softc { 55 1.1 reinoud struct virtio_mmio_softc sc_msc; 56 1.1 reinoud 57 1.1 reinoud int sc_irq; 58 1.1 reinoud void *sc_ih; 59 1.1 reinoud }; 60 1.1 reinoud 61 1.1 reinoud 62 1.1 reinoud CFATTACH_DECL3_NEW(virtio_mainbus, sizeof(struct virtio_mainbus_softc), 63 1.1 reinoud virtio_mainbus_match, virtio_mainbus_attach, 64 1.1 reinoud virtio_mainbus_detach, NULL, 65 1.1 reinoud virtio_mainbus_rescan, (void *)voidop, DVF_DETACH_SHUTDOWN); 66 1.1 reinoud 67 1.1 reinoud 68 1.1 reinoud static int 69 1.1 reinoud virtio_mainbus_match(device_t parent, cfdata_t match, void *aux) 70 1.1 reinoud { 71 1.1 reinoud struct mainbus_attach_args *ma = aux; 72 1.1 reinoud 73 1.1 reinoud /* TODO can we check here if its present? */ 74 1.1 reinoud if (strcmp(ma->ma_name, match->cf_name) == 0) 75 1.1 reinoud return (1<<1); 76 1.1 reinoud 77 1.1 reinoud return (0); 78 1.1 reinoud } 79 1.1 reinoud 80 1.1 reinoud 81 1.1 reinoud void 82 1.1 reinoud virtio_mainbus_attach(device_t parent, device_t self, void *aux) 83 1.1 reinoud { 84 1.1 reinoud struct virtio_mainbus_softc *sc = device_private(self); 85 1.1 reinoud struct virtio_mmio_softc *msc = &sc->sc_msc; 86 1.1 reinoud struct virtio_softc *vsc = &msc->sc_sc; 87 1.1 reinoud struct mainbus_attach_args *ma = aux; 88 1.1 reinoud bus_space_handle_t ioh; 89 1.1 reinoud 90 1.1 reinoud if (bus_space_map(ma->ma_iot, ma->ma_addr, VIRTIO_STRIDE, 0, &ioh) != 0) { 91 1.1 reinoud aprint_error(": can't map i/o space\n"); 92 1.1 reinoud return; 93 1.1 reinoud } 94 1.1 reinoud 95 1.1 reinoud aprint_normal("\n"); 96 1.1 reinoud aprint_naive("\n"); 97 1.1 reinoud 98 1.1 reinoud sc->sc_irq = ma->ma_irq; 99 1.1 reinoud 100 1.1 reinoud msc->sc_iot = ma->ma_iot; 101 1.1 reinoud msc->sc_ioh = ioh; 102 1.1 reinoud msc->sc_iosize = VIRTIO_STRIDE; 103 1.4 skrll msc->sc_alloc_interrupts = virtio_mainbus_alloc_interrupts; 104 1.1 reinoud msc->sc_free_interrupts = virtio_mainbus_free_interrupts; 105 1.1 reinoud 106 1.1 reinoud vsc->sc_dev = self; 107 1.1 reinoud vsc->sc_dmat = ma->ma_dmat; 108 1.1 reinoud if (virtio_mmio_common_probe_present(msc)) 109 1.1 reinoud virtio_mmio_common_attach(msc); 110 1.1 reinoud 111 1.2 thorpej virtio_mainbus_rescan(self, NULL, NULL); 112 1.1 reinoud } 113 1.1 reinoud 114 1.1 reinoud 115 1.1 reinoud /* ARGSUSED */ 116 1.1 reinoud static int 117 1.2 thorpej virtio_mainbus_rescan(device_t self, const char *ifattr, const int *locs) 118 1.1 reinoud { 119 1.1 reinoud struct virtio_mainbus_softc *sc = device_private(self); 120 1.1 reinoud struct virtio_mmio_softc *msc = &sc->sc_msc; 121 1.1 reinoud struct virtio_softc *vsc = &msc->sc_sc; 122 1.1 reinoud struct virtio_attach_args va; 123 1.1 reinoud 124 1.1 reinoud if (vsc->sc_child) /* child already attached? */ 125 1.1 reinoud return 0; 126 1.1 reinoud 127 1.1 reinoud memset(&va, 0, sizeof(va)); 128 1.1 reinoud va.sc_childdevid = vsc->sc_childdevid; 129 1.1 reinoud 130 1.3 thorpej config_found(self, &va, NULL, CFARGS_NONE); 131 1.1 reinoud 132 1.1 reinoud if (virtio_attach_failed(vsc)) 133 1.1 reinoud return 0; 134 1.1 reinoud return 0; 135 1.1 reinoud } 136 1.1 reinoud 137 1.1 reinoud 138 1.1 reinoud static int 139 1.1 reinoud virtio_mainbus_detach(device_t self, int flags) 140 1.1 reinoud { 141 1.1 reinoud struct virtio_mainbus_softc *sc = device_private(self); 142 1.1 reinoud struct virtio_mmio_softc * const msc = &sc->sc_msc; 143 1.1 reinoud 144 1.1 reinoud return virtio_mmio_common_detach(msc, flags); 145 1.1 reinoud } 146 1.1 reinoud 147 1.1 reinoud 148 1.1 reinoud static int 149 1.4 skrll virtio_mainbus_alloc_interrupts(struct virtio_mmio_softc *msc) 150 1.1 reinoud { 151 1.1 reinoud struct virtio_mainbus_softc *sc = (struct virtio_mainbus_softc *) msc; 152 1.1 reinoud struct virtio_softc * const vsc = &msc->sc_sc; 153 1.1 reinoud 154 1.1 reinoud msc->sc_ih = evbmips_intr_establish(sc->sc_irq, virtio_mmio_intr, 155 1.1 reinoud msc); 156 1.1 reinoud if (msc->sc_ih == NULL) { 157 1.1 reinoud aprint_error_dev(vsc->sc_dev, 158 1.1 reinoud "couldn't install interrupt handler\n"); 159 1.1 reinoud return -1; 160 1.1 reinoud } 161 1.1 reinoud 162 1.1 reinoud aprint_normal_dev(vsc->sc_dev, "interrupting at irq %d\n", sc->sc_irq); 163 1.1 reinoud 164 1.1 reinoud return 0; 165 1.1 reinoud } 166 1.1 reinoud 167 1.1 reinoud 168 1.1 reinoud static void 169 1.1 reinoud virtio_mainbus_free_interrupts(struct virtio_mmio_softc *msc) 170 1.1 reinoud { 171 1.1 reinoud panic("%s not implemented\n", __func__); 172 1.1 reinoud } 173