Home | History | Annotate | Line # | Download | only in fdt
virtio_mmio_fdt.c revision 1.2
      1 /* $NetBSD: virtio_mmio_fdt.c,v 1.2 2018/06/15 17:13:43 jakllsch Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2018 Jonathan A. Kollasch
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: virtio_mmio_fdt.c,v 1.2 2018/06/15 17:13:43 jakllsch Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/systm.h>
     34 
     35 #include <sys/device.h>
     36 
     37 #include <dev/fdt/fdtvar.h>
     38 
     39 #define VIRTIO_PRIVATE
     40 #include <dev/virtio/virtio_mmiovar.h>
     41 
     42 static int	virtio_mmio_fdt_match(device_t, cfdata_t, void *);
     43 static void	virtio_mmio_fdt_attach(device_t, device_t, void *);
     44 static int	virtio_mmio_fdt_rescan(device_t, const char *, const int *);
     45 static int	virtio_mmio_fdt_detach(device_t, int);
     46 
     47 static int	virtio_mmio_fdt_setup_interrupts(struct virtio_mmio_softc *);
     48 static void	virtio_mmio_fdt_free_interrupts(struct virtio_mmio_softc *);
     49 
     50 struct virtio_mmio_fdt_softc {
     51 	struct virtio_mmio_softc	sc_msc;
     52 	int				sc_phandle;
     53 };
     54 
     55 CFATTACH_DECL3_NEW(virtio_mmio_fdt, sizeof(struct virtio_mmio_fdt_softc),
     56     virtio_mmio_fdt_match, virtio_mmio_fdt_attach, virtio_mmio_fdt_detach, NULL,
     57     virtio_mmio_fdt_rescan, (void *)voidop, DVF_DETACH_SHUTDOWN);
     58 
     59 static const char * const compatible[] = {
     60 	"virtio,mmio",
     61 	NULL
     62 };
     63 
     64 static int
     65 virtio_mmio_fdt_match(device_t parent, cfdata_t match, void *aux)
     66 {
     67 	struct fdt_attach_args * const faa = aux;
     68 
     69 	return of_match_compatible(faa->faa_phandle, compatible);
     70 }
     71 
     72 static void
     73 virtio_mmio_fdt_attach(device_t parent, device_t self, void *aux)
     74 {
     75 	struct virtio_mmio_fdt_softc * const fsc = device_private(self);
     76 	struct virtio_mmio_softc * const msc = &fsc->sc_msc;
     77 	struct virtio_softc * const vsc = &msc->sc_sc;
     78 	struct fdt_attach_args * const faa = aux;
     79 	bus_addr_t addr;
     80 	bus_size_t size;
     81 	int error;
     82 
     83 	aprint_normal("\n");
     84 	aprint_naive("\n");
     85 
     86 	if (fdtbus_get_reg(faa->faa_phandle, 0, &addr, &size) != 0) {
     87 		aprint_error_dev(self, "couldn't get registers\n");
     88 		return;
     89 	}
     90 
     91 	fsc->sc_phandle = faa->faa_phandle;
     92 	msc->sc_iot = faa->faa_bst;
     93 	vsc->sc_dev = self;
     94 	vsc->sc_dmat = faa->faa_dmat;
     95 
     96 	error = bus_space_map(msc->sc_iot, addr, size, 0, &msc->sc_ioh);
     97 	if (error) {
     98 		aprint_error_dev(self, "couldn't map %#llx: %d",
     99 		    (uint64_t)addr, error);
    100 		return;
    101 	}
    102 	msc->sc_iosize = size;
    103 
    104 	msc->sc_setup_interrupts = virtio_mmio_fdt_setup_interrupts;
    105 	msc->sc_free_interrupts = virtio_mmio_fdt_free_interrupts;
    106 
    107 	virtio_mmio_common_attach(msc);
    108 	virtio_mmio_fdt_rescan(self, "virtio", NULL);
    109 }
    110 
    111 /* ARGSUSED */
    112 static int
    113 virtio_mmio_fdt_rescan(device_t self, const char *attr, const int *scan_flags)
    114 {
    115 	struct virtio_mmio_fdt_softc * const fsc = device_private(self);
    116 	struct virtio_mmio_softc * const msc = &fsc->sc_msc;
    117 	struct virtio_softc * const vsc = &msc->sc_sc;
    118 	struct virtio_attach_args va;
    119 
    120 	if (vsc->sc_child)	/* Child already attached? */
    121 		return 0;
    122 	memset(&va, 0, sizeof(va));
    123 	va.sc_childdevid = vsc->sc_childdevid;
    124 
    125 	config_found_ia(self, attr, &va, virtiobusprint);
    126 
    127 	if (vsc->sc_child == NULL) {
    128 		return 0;
    129 	}
    130 
    131 	if (vsc->sc_child == VIRTIO_CHILD_FAILED) {
    132 		aprint_error_dev(self, "virtio configuration failed\n");
    133 		return 0;
    134 	}
    135 
    136 	/*
    137 	 * Make sure child drivers initialize interrupts via call
    138 	 * to virtio_child_attach_finish().
    139 	 */
    140 	KASSERT(msc->sc_ih != NULL);
    141 
    142 	return 0;
    143 }
    144 
    145 static int
    146 virtio_mmio_fdt_detach(device_t self, int flags)
    147 {
    148 	struct virtio_mmio_fdt_softc * const fsc = device_private(self);
    149 	struct virtio_mmio_softc * const msc = &fsc->sc_msc;
    150 
    151 	return virtio_mmio_common_detach(msc, flags);
    152 }
    153 
    154 static int
    155 virtio_mmio_fdt_setup_interrupts(struct virtio_mmio_softc *msc)
    156 {
    157 	struct virtio_mmio_fdt_softc * const fsc = (void *)msc;
    158 	struct virtio_softc * const vsc = &msc->sc_sc;
    159 	char intrstr[128];
    160 	int flags = 0;
    161 
    162 	if (!fdtbus_intr_str(fsc->sc_phandle, 0, intrstr, sizeof(intrstr))) {
    163 		aprint_error_dev(vsc->sc_dev, "failed to decode interrupt\n");
    164 		return -1;
    165 	}
    166 
    167 	if (vsc->sc_flags & VIRTIO_F_PCI_INTR_MPSAFE)
    168 		flags |= FDT_INTR_MPSAFE;
    169 
    170 	msc->sc_ih = fdtbus_intr_establish(fsc->sc_phandle, 0, vsc->sc_ipl,
    171 	    flags, virtio_mmio_intr, msc);
    172 	if (msc->sc_ih == NULL) {
    173 		aprint_error_dev(vsc->sc_dev,
    174 		    "failed to establish interrupt on %s\n", intrstr);
    175 		return -1;
    176 	}
    177 	aprint_normal_dev(vsc->sc_dev, "interrupting on %s\n", intrstr);
    178 
    179 	return 0;
    180 }
    181 
    182 static void
    183 virtio_mmio_fdt_free_interrupts(struct virtio_mmio_softc *msc)
    184 {
    185 	struct virtio_mmio_fdt_softc * const fsc = (void *)msc;
    186 
    187 	if (msc->sc_ih != NULL) {
    188 		fdtbus_intr_disestablish(fsc->sc_phandle, msc->sc_ih);
    189 		msc->sc_ih = NULL;
    190 	}
    191 }
    192