1 1.11 riastrad /* $NetBSD: virtio_acpi.c,v 1.11 2025/01/14 16:46:38 riastradh Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation 8 1.1 jmcneill * by Jared McNeill <jmcneill (at) invisible.ca>. 9 1.1 jmcneill * 10 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 11 1.1 jmcneill * modification, are permitted provided that the following conditions 12 1.1 jmcneill * are met: 13 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 14 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 15 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 17 1.1 jmcneill * documentation and/or other materials provided with the distribution. 18 1.1 jmcneill * 19 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE. 30 1.1 jmcneill */ 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/cdefs.h> 33 1.11 riastrad __KERNEL_RCSID(0, "$NetBSD: virtio_acpi.c,v 1.11 2025/01/14 16:46:38 riastradh Exp $"); 34 1.1 jmcneill 35 1.1 jmcneill #include <sys/param.h> 36 1.1 jmcneill #include <sys/bus.h> 37 1.1 jmcneill #include <sys/cpu.h> 38 1.1 jmcneill #include <sys/device.h> 39 1.1 jmcneill 40 1.1 jmcneill #include <dev/acpi/acpireg.h> 41 1.1 jmcneill #include <dev/acpi/acpivar.h> 42 1.2 jmcneill #include <dev/acpi/acpi_intr.h> 43 1.1 jmcneill 44 1.1 jmcneill #define VIRTIO_PRIVATE 45 1.1 jmcneill #include <dev/virtio/virtio_mmiovar.h> 46 1.1 jmcneill 47 1.1 jmcneill struct virtio_acpi_softc { 48 1.1 jmcneill struct virtio_mmio_softc sc_msc; 49 1.2 jmcneill ACPI_HANDLE sc_handle; 50 1.1 jmcneill int sc_irq; 51 1.1 jmcneill int sc_irqtype; 52 1.1 jmcneill }; 53 1.1 jmcneill 54 1.1 jmcneill static int virtio_acpi_match(device_t, cfdata_t, void *); 55 1.1 jmcneill static void virtio_acpi_attach(device_t, device_t, void *); 56 1.1 jmcneill static int virtio_acpi_rescan(device_t, const char *, const int *); 57 1.1 jmcneill static int virtio_acpi_detach(device_t, int); 58 1.1 jmcneill 59 1.10 yamaguch static int virtio_acpi_alloc_interrupts(struct virtio_mmio_softc *); 60 1.1 jmcneill static void virtio_acpi_free_interrupts(struct virtio_mmio_softc *); 61 1.1 jmcneill 62 1.1 jmcneill CFATTACH_DECL3_NEW(virtio_acpi, sizeof(struct virtio_acpi_softc), 63 1.1 jmcneill virtio_acpi_match, virtio_acpi_attach, virtio_acpi_detach, NULL, 64 1.11 riastrad virtio_acpi_rescan, NULL, 0); 65 1.1 jmcneill 66 1.7 thorpej static const struct device_compatible_entry compat_data[] = { 67 1.7 thorpej { .compat = "LNRO0005" }, 68 1.7 thorpej DEVICE_COMPAT_EOL 69 1.1 jmcneill }; 70 1.1 jmcneill 71 1.1 jmcneill static int 72 1.1 jmcneill virtio_acpi_match(device_t parent, cfdata_t cf, void *aux) 73 1.1 jmcneill { 74 1.1 jmcneill struct acpi_attach_args *aa = aux; 75 1.1 jmcneill 76 1.7 thorpej return acpi_compatible_match(aa, compat_data); 77 1.1 jmcneill } 78 1.1 jmcneill 79 1.1 jmcneill static void 80 1.1 jmcneill virtio_acpi_attach(device_t parent, device_t self, void *aux) 81 1.1 jmcneill { 82 1.1 jmcneill struct virtio_acpi_softc * const sc = device_private(self); 83 1.1 jmcneill struct virtio_mmio_softc * const msc = &sc->sc_msc; 84 1.1 jmcneill struct virtio_softc * const vsc = &msc->sc_sc; 85 1.1 jmcneill struct acpi_attach_args *aa = aux; 86 1.1 jmcneill struct acpi_resources res; 87 1.1 jmcneill struct acpi_mem *mem; 88 1.1 jmcneill struct acpi_irq *irq; 89 1.1 jmcneill ACPI_STATUS rv; 90 1.1 jmcneill int error; 91 1.1 jmcneill 92 1.2 jmcneill sc->sc_handle = aa->aa_node->ad_handle; 93 1.1 jmcneill msc->sc_iot = aa->aa_memt; 94 1.1 jmcneill vsc->sc_dev = self; 95 1.3 skrll 96 1.3 skrll if (BUS_DMA_TAG_VALID(aa->aa_dmat64)) { 97 1.3 skrll aprint_verbose(": using 64-bit DMA"); 98 1.3 skrll vsc->sc_dmat = aa->aa_dmat64; 99 1.3 skrll } else { 100 1.3 skrll aprint_verbose(": using 32-bit DMA"); 101 1.3 skrll vsc->sc_dmat = aa->aa_dmat; 102 1.3 skrll } 103 1.1 jmcneill 104 1.1 jmcneill rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS", 105 1.1 jmcneill &res, &acpi_resource_parse_ops_default); 106 1.1 jmcneill if (ACPI_FAILURE(rv)) 107 1.1 jmcneill return; 108 1.1 jmcneill 109 1.1 jmcneill mem = acpi_res_mem(&res, 0); 110 1.1 jmcneill if (mem == NULL) { 111 1.1 jmcneill aprint_error_dev(self, "couldn't find mem resource\n"); 112 1.1 jmcneill goto done; 113 1.1 jmcneill } 114 1.1 jmcneill 115 1.1 jmcneill irq = acpi_res_irq(&res, 0); 116 1.1 jmcneill if (irq == NULL) { 117 1.1 jmcneill aprint_error_dev(self, "couldn't find irq resource\n"); 118 1.1 jmcneill goto done; 119 1.1 jmcneill } 120 1.1 jmcneill sc->sc_irq = irq->ar_irq; 121 1.1 jmcneill sc->sc_irqtype = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL; 122 1.1 jmcneill 123 1.1 jmcneill error = bus_space_map(msc->sc_iot, mem->ar_base, mem->ar_length, 0, &msc->sc_ioh); 124 1.1 jmcneill if (error) { 125 1.1 jmcneill aprint_error_dev(self, "couldn't map registers\n"); 126 1.1 jmcneill return; 127 1.1 jmcneill } 128 1.1 jmcneill msc->sc_iosize = mem->ar_length; 129 1.1 jmcneill 130 1.10 yamaguch msc->sc_alloc_interrupts = virtio_acpi_alloc_interrupts; 131 1.1 jmcneill msc->sc_free_interrupts = virtio_acpi_free_interrupts; 132 1.1 jmcneill 133 1.1 jmcneill virtio_mmio_common_attach(msc); 134 1.1 jmcneill virtio_acpi_rescan(self, "virtio", NULL); 135 1.1 jmcneill 136 1.1 jmcneill done: 137 1.1 jmcneill acpi_resource_cleanup(&res); 138 1.1 jmcneill } 139 1.1 jmcneill 140 1.1 jmcneill static int 141 1.1 jmcneill virtio_acpi_detach(device_t self, int flags) 142 1.1 jmcneill { 143 1.1 jmcneill struct virtio_acpi_softc * const sc = device_private(self); 144 1.1 jmcneill struct virtio_mmio_softc * const msc = &sc->sc_msc; 145 1.1 jmcneill 146 1.1 jmcneill return virtio_mmio_common_detach(msc, flags); 147 1.1 jmcneill } 148 1.1 jmcneill 149 1.1 jmcneill static int 150 1.1 jmcneill virtio_acpi_rescan(device_t self, const char *ifattr, const int *locs) 151 1.1 jmcneill { 152 1.1 jmcneill struct virtio_acpi_softc * const sc = device_private(self); 153 1.1 jmcneill struct virtio_mmio_softc * const msc = &sc->sc_msc; 154 1.1 jmcneill struct virtio_softc * const vsc = &msc->sc_sc; 155 1.1 jmcneill struct virtio_attach_args va; 156 1.1 jmcneill 157 1.1 jmcneill if (vsc->sc_child) 158 1.1 jmcneill return 0; 159 1.1 jmcneill 160 1.1 jmcneill memset(&va, 0, sizeof(va)); 161 1.1 jmcneill va.sc_childdevid = vsc->sc_childdevid; 162 1.1 jmcneill 163 1.9 thorpej config_found(self, &va, NULL, CFARGS_NONE); 164 1.5 reinoud 165 1.5 reinoud if (virtio_attach_failed(vsc)) 166 1.5 reinoud return 0; 167 1.5 reinoud 168 1.1 jmcneill return 0; 169 1.1 jmcneill } 170 1.1 jmcneill 171 1.1 jmcneill static int 172 1.10 yamaguch virtio_acpi_alloc_interrupts(struct virtio_mmio_softc *msc) 173 1.1 jmcneill { 174 1.1 jmcneill struct virtio_acpi_softc * const sc = (struct virtio_acpi_softc *)msc; 175 1.1 jmcneill struct virtio_softc * const vsc = &msc->sc_sc; 176 1.1 jmcneill 177 1.4 jmcneill msc->sc_ih = acpi_intr_establish(vsc->sc_dev, 178 1.4 jmcneill (uint64_t)(uintptr_t)sc->sc_handle, 179 1.2 jmcneill IPL_VM, false, virtio_mmio_intr, msc, device_xname(vsc->sc_dev)); 180 1.1 jmcneill if (msc->sc_ih == NULL) { 181 1.1 jmcneill aprint_error_dev(vsc->sc_dev, "couldn't install interrupt handler\n"); 182 1.1 jmcneill return -1; 183 1.1 jmcneill } 184 1.1 jmcneill 185 1.1 jmcneill aprint_normal_dev(vsc->sc_dev, "interrupting on irq %d\n", sc->sc_irq); 186 1.1 jmcneill 187 1.1 jmcneill return 0; 188 1.1 jmcneill } 189 1.1 jmcneill 190 1.1 jmcneill static void 191 1.1 jmcneill virtio_acpi_free_interrupts(struct virtio_mmio_softc *msc) 192 1.1 jmcneill { 193 1.1 jmcneill if (msc->sc_ih != NULL) { 194 1.2 jmcneill acpi_intr_disestablish(msc->sc_ih); 195 1.1 jmcneill msc->sc_ih = NULL; 196 1.1 jmcneill } 197 1.1 jmcneill } 198