Home | History | Annotate | Line # | Download | only in acpi
virtio_acpi.c revision 1.3.2.1
      1  1.3.2.1   thorpej /* $NetBSD: virtio_acpi.c,v 1.3.2.1 2020/12/14 14:38:05 thorpej 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.3.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: virtio_acpi.c,v 1.3.2.1 2020/12/14 14:38:05 thorpej 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.1  jmcneill static int	virtio_acpi_setup_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.1  jmcneill     virtio_acpi_rescan, (void *)voidop, DVF_DETACH_SHUTDOWN);
     65      1.1  jmcneill 
     66      1.1  jmcneill static const char * const compatible[] = {
     67      1.1  jmcneill 	"LNRO0005",
     68      1.1  jmcneill 	NULL
     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.1  jmcneill 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
     77      1.1  jmcneill 		return 0;
     78      1.1  jmcneill 
     79      1.1  jmcneill 	return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
     80      1.1  jmcneill }
     81      1.1  jmcneill 
     82      1.1  jmcneill static void
     83      1.1  jmcneill virtio_acpi_attach(device_t parent, device_t self, void *aux)
     84      1.1  jmcneill {
     85      1.1  jmcneill 	struct virtio_acpi_softc * const sc = device_private(self);
     86      1.1  jmcneill 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
     87      1.1  jmcneill 	struct virtio_softc * const vsc = &msc->sc_sc;
     88      1.1  jmcneill 	struct acpi_attach_args *aa = aux;
     89      1.1  jmcneill 	struct acpi_resources res;
     90      1.1  jmcneill 	struct acpi_mem *mem;
     91      1.1  jmcneill 	struct acpi_irq *irq;
     92      1.1  jmcneill 	ACPI_STATUS rv;
     93      1.1  jmcneill 	int error;
     94      1.1  jmcneill 
     95      1.2  jmcneill 	sc->sc_handle = aa->aa_node->ad_handle;
     96      1.1  jmcneill 	msc->sc_iot = aa->aa_memt;
     97      1.1  jmcneill 	vsc->sc_dev = self;
     98      1.3     skrll 
     99      1.3     skrll 	if (BUS_DMA_TAG_VALID(aa->aa_dmat64)) {
    100      1.3     skrll 		aprint_verbose(": using 64-bit DMA");
    101      1.3     skrll 		vsc->sc_dmat = aa->aa_dmat64;
    102      1.3     skrll 	} else {
    103      1.3     skrll 		aprint_verbose(": using 32-bit DMA");
    104      1.3     skrll 		vsc->sc_dmat = aa->aa_dmat;
    105      1.3     skrll 	}
    106      1.1  jmcneill 
    107      1.1  jmcneill 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
    108      1.1  jmcneill 	    &res, &acpi_resource_parse_ops_default);
    109      1.1  jmcneill 	if (ACPI_FAILURE(rv))
    110      1.1  jmcneill 		return;
    111      1.1  jmcneill 
    112      1.1  jmcneill 	mem = acpi_res_mem(&res, 0);
    113      1.1  jmcneill 	if (mem == NULL) {
    114      1.1  jmcneill 		aprint_error_dev(self, "couldn't find mem resource\n");
    115      1.1  jmcneill 		goto done;
    116      1.1  jmcneill 	}
    117      1.1  jmcneill 
    118      1.1  jmcneill 	irq = acpi_res_irq(&res, 0);
    119      1.1  jmcneill 	if (irq == NULL) {
    120      1.1  jmcneill 		aprint_error_dev(self, "couldn't find irq resource\n");
    121      1.1  jmcneill 		goto done;
    122      1.1  jmcneill 	}
    123      1.1  jmcneill 	sc->sc_irq = irq->ar_irq;
    124      1.1  jmcneill 	sc->sc_irqtype = (irq->ar_type == ACPI_EDGE_SENSITIVE) ? IST_EDGE : IST_LEVEL;
    125      1.1  jmcneill 
    126      1.1  jmcneill 	error = bus_space_map(msc->sc_iot, mem->ar_base, mem->ar_length, 0, &msc->sc_ioh);
    127      1.1  jmcneill 	if (error) {
    128      1.1  jmcneill 		aprint_error_dev(self, "couldn't map registers\n");
    129      1.1  jmcneill 		return;
    130      1.1  jmcneill 	}
    131      1.1  jmcneill 	msc->sc_iosize = mem->ar_length;
    132      1.1  jmcneill 
    133      1.1  jmcneill 	msc->sc_setup_interrupts = virtio_acpi_setup_interrupts;
    134      1.1  jmcneill 	msc->sc_free_interrupts = virtio_acpi_free_interrupts;
    135      1.1  jmcneill 
    136      1.1  jmcneill 	virtio_mmio_common_attach(msc);
    137      1.1  jmcneill 	virtio_acpi_rescan(self, "virtio", NULL);
    138      1.1  jmcneill 
    139      1.1  jmcneill done:
    140      1.1  jmcneill 	acpi_resource_cleanup(&res);
    141      1.1  jmcneill }
    142      1.1  jmcneill 
    143      1.1  jmcneill static int
    144      1.1  jmcneill virtio_acpi_detach(device_t self, int flags)
    145      1.1  jmcneill {
    146      1.1  jmcneill 	struct virtio_acpi_softc * const sc = device_private(self);
    147      1.1  jmcneill 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
    148      1.1  jmcneill 
    149      1.1  jmcneill 	return virtio_mmio_common_detach(msc, flags);
    150      1.1  jmcneill }
    151      1.1  jmcneill 
    152      1.1  jmcneill static int
    153      1.1  jmcneill virtio_acpi_rescan(device_t self, const char *ifattr, const int *locs)
    154      1.1  jmcneill {
    155      1.1  jmcneill 	struct virtio_acpi_softc * const sc = device_private(self);
    156      1.1  jmcneill 	struct virtio_mmio_softc * const msc = &sc->sc_msc;
    157      1.1  jmcneill 	struct virtio_softc * const vsc = &msc->sc_sc;
    158      1.1  jmcneill 	struct virtio_attach_args va;
    159      1.1  jmcneill 
    160      1.1  jmcneill 	if (vsc->sc_child)
    161      1.1  jmcneill 		return 0;
    162      1.1  jmcneill 
    163      1.1  jmcneill 	memset(&va, 0, sizeof(va));
    164      1.1  jmcneill 	va.sc_childdevid = vsc->sc_childdevid;
    165      1.1  jmcneill 
    166      1.1  jmcneill 	config_found_ia(self, ifattr, &va, virtiobusprint);
    167      1.1  jmcneill 
    168      1.1  jmcneill 	return 0;
    169      1.1  jmcneill }
    170      1.1  jmcneill 
    171      1.1  jmcneill static int
    172      1.1  jmcneill virtio_acpi_setup_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.3.2.1   thorpej 	msc->sc_ih = acpi_intr_establish(vsc->sc_dev,
    178  1.3.2.1   thorpej 	    (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