Home | History | Annotate | Line # | Download | only in acpi
virtio_acpi.c revision 1.5
      1 /* $NetBSD: virtio_acpi.c,v 1.5 2021/01/20 19:46:48 reinoud 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.5 2021/01/20 19:46:48 reinoud 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_setup_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, (void *)voidop, DVF_DETACH_SHUTDOWN);
     65 
     66 static const char * const compatible[] = {
     67 	"LNRO0005",
     68 	NULL
     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 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     77 		return 0;
     78 
     79 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
     80 }
     81 
     82 static void
     83 virtio_acpi_attach(device_t parent, device_t self, void *aux)
     84 {
     85 	struct virtio_acpi_softc * const sc = device_private(self);
     86 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
     87 	struct virtio_softc * const vsc = &msc->sc_sc;
     88 	struct acpi_attach_args *aa = aux;
     89 	struct acpi_resources res;
     90 	struct acpi_mem *mem;
     91 	struct acpi_irq *irq;
     92 	ACPI_STATUS rv;
     93 	int error;
     94 
     95 	sc->sc_handle = aa->aa_node->ad_handle;
     96 	msc->sc_iot = aa->aa_memt;
     97 	vsc->sc_dev = self;
     98 
     99 	if (BUS_DMA_TAG_VALID(aa->aa_dmat64)) {
    100 		aprint_verbose(": using 64-bit DMA");
    101 		vsc->sc_dmat = aa->aa_dmat64;
    102 	} else {
    103 		aprint_verbose(": using 32-bit DMA");
    104 		vsc->sc_dmat = aa->aa_dmat;
    105 	}
    106 
    107 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    108 	    &res, &acpi_resource_parse_ops_default);
    109 	if (ACPI_FAILURE(rv))
    110 		return;
    111 
    112 	mem = acpi_res_mem(&res, 0);
    113 	if (mem == NULL) {
    114 		aprint_error_dev(self, "couldn't find mem resource\n");
    115 		goto done;
    116 	}
    117 
    118 	irq = acpi_res_irq(&res, 0);
    119 	if (irq == NULL) {
    120 		aprint_error_dev(self, "couldn't find irq resource\n");
    121 		goto done;
    122 	}
    123 	sc->sc_irq = irq->ar_irq;
    124 	sc->sc_irqtype = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
    125 
    126 	error = bus_space_map(msc->sc_iot, mem->ar_base, mem->ar_length, 0, &msc->sc_ioh);
    127 	if (error) {
    128 		aprint_error_dev(self, "couldn't map registers\n");
    129 		return;
    130 	}
    131 	msc->sc_iosize = mem->ar_length;
    132 
    133 	msc->sc_setup_interrupts = virtio_acpi_setup_interrupts;
    134 	msc->sc_free_interrupts = virtio_acpi_free_interrupts;
    135 
    136 	virtio_mmio_common_attach(msc);
    137 	virtio_acpi_rescan(self, "virtio", NULL);
    138 
    139 done:
    140 	acpi_resource_cleanup(&res);
    141 }
    142 
    143 static int
    144 virtio_acpi_detach(device_t self, int flags)
    145 {
    146 	struct virtio_acpi_softc * const sc = device_private(self);
    147 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
    148 
    149 	return virtio_mmio_common_detach(msc, flags);
    150 }
    151 
    152 static int
    153 virtio_acpi_rescan(device_t self, const char *ifattr, const int *locs)
    154 {
    155 	struct virtio_acpi_softc * const sc = device_private(self);
    156 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
    157 	struct virtio_softc * const vsc = &msc->sc_sc;
    158 	struct virtio_attach_args va;
    159 
    160 	if (vsc->sc_child)
    161 		return 0;
    162 
    163 	memset(&va, 0, sizeof(va));
    164 	va.sc_childdevid = vsc->sc_childdevid;
    165 
    166 	config_found_ia(self, ifattr, &va, NULL);
    167 
    168 	if (virtio_attach_failed(vsc))
    169 		return 0;
    170 
    171 	/*
    172 	 * Make sure child drivers initialize interrupts via call
    173 	 * to virtio_child_attach_finish().
    174 	 */
    175 	KASSERT(msc->sc_ih != NULL);
    176 
    177 	return 0;
    178 }
    179 
    180 static int
    181 virtio_acpi_setup_interrupts(struct virtio_mmio_softc *msc)
    182 {
    183 	struct virtio_acpi_softc * const sc = (struct virtio_acpi_softc *)msc;
    184 	struct virtio_softc * const vsc = &msc->sc_sc;
    185 
    186 	msc->sc_ih = acpi_intr_establish(vsc->sc_dev,
    187 	    (uint64_t)(uintptr_t)sc->sc_handle,
    188             IPL_VM, false, virtio_mmio_intr, msc, device_xname(vsc->sc_dev));
    189 	if (msc->sc_ih == NULL) {
    190 		aprint_error_dev(vsc->sc_dev, "couldn't install interrupt handler\n");
    191 		return -1;
    192 	}
    193 
    194 	aprint_normal_dev(vsc->sc_dev, "interrupting on irq %d\n", sc->sc_irq);
    195 
    196 	return 0;
    197 }
    198 
    199 static void
    200 virtio_acpi_free_interrupts(struct virtio_mmio_softc *msc)
    201 {
    202 	if (msc->sc_ih != NULL) {
    203 		acpi_intr_disestablish(msc->sc_ih);
    204 		msc->sc_ih = NULL;
    205 	}
    206 }
    207