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