Home | History | Annotate | Line # | Download | only in virtio
virtio_mmio.c revision 1.4
      1  1.4   reinoud /*	$NetBSD: virtio_mmio.c,v 1.4 2021/01/20 19:46:48 reinoud Exp $	*/
      2  1.1  jakllsch /*	$OpenBSD: virtio_mmio.c,v 1.2 2017/02/24 17:12:31 patrick Exp $	*/
      3  1.1  jakllsch 
      4  1.1  jakllsch /*
      5  1.1  jakllsch  * Copyright (c) 2014 Patrick Wildt <patrick (at) blueri.se>
      6  1.1  jakllsch  * Copyright (c) 2012 Stefan Fritsch.
      7  1.1  jakllsch  * Copyright (c) 2010 Minoura Makoto.
      8  1.1  jakllsch  * All rights reserved.
      9  1.1  jakllsch  *
     10  1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
     11  1.1  jakllsch  * modification, are permitted provided that the following conditions
     12  1.1  jakllsch  * are met:
     13  1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     14  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     15  1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     18  1.1  jakllsch  *
     19  1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     20  1.1  jakllsch  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     21  1.1  jakllsch  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     22  1.1  jakllsch  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     23  1.1  jakllsch  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     24  1.1  jakllsch  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  1.1  jakllsch  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  1.1  jakllsch  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  1.1  jakllsch  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     28  1.1  jakllsch  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  1.1  jakllsch  */
     30  1.1  jakllsch 
     31  1.1  jakllsch #include <sys/cdefs.h>
     32  1.4   reinoud __KERNEL_RCSID(0, "$NetBSD: virtio_mmio.c,v 1.4 2021/01/20 19:46:48 reinoud Exp $");
     33  1.1  jakllsch 
     34  1.1  jakllsch #include <sys/param.h>
     35  1.1  jakllsch #include <sys/systm.h>
     36  1.1  jakllsch #include <sys/kernel.h>
     37  1.1  jakllsch #include <sys/device.h>
     38  1.1  jakllsch #include <sys/mutex.h>
     39  1.1  jakllsch 
     40  1.1  jakllsch #define VIRTIO_PRIVATE
     41  1.1  jakllsch #include <dev/virtio/virtio_mmiovar.h>
     42  1.1  jakllsch 
     43  1.1  jakllsch #define VIRTIO_MMIO_MAGIC		('v' | 'i' << 8 | 'r' << 16 | 't' << 24)
     44  1.1  jakllsch 
     45  1.1  jakllsch #define VIRTIO_MMIO_MAGIC_VALUE		0x000
     46  1.1  jakllsch #define VIRTIO_MMIO_VERSION		0x004
     47  1.1  jakllsch #define VIRTIO_MMIO_DEVICE_ID		0x008
     48  1.1  jakllsch #define VIRTIO_MMIO_VENDOR_ID		0x00c
     49  1.1  jakllsch #define VIRTIO_MMIO_HOST_FEATURES	0x010
     50  1.1  jakllsch #define VIRTIO_MMIO_HOST_FEATURES_SEL	0x014
     51  1.1  jakllsch #define VIRTIO_MMIO_GUEST_FEATURES	0x020
     52  1.1  jakllsch #define VIRTIO_MMIO_GUEST_FEATURES_SEL	0x024
     53  1.1  jakllsch #define VIRTIO_MMIO_GUEST_PAGE_SIZE	0x028
     54  1.1  jakllsch #define VIRTIO_MMIO_QUEUE_SEL		0x030
     55  1.1  jakllsch #define VIRTIO_MMIO_QUEUE_NUM_MAX	0x034
     56  1.1  jakllsch #define VIRTIO_MMIO_QUEUE_NUM		0x038
     57  1.1  jakllsch #define VIRTIO_MMIO_QUEUE_ALIGN		0x03c
     58  1.1  jakllsch #define VIRTIO_MMIO_QUEUE_PFN		0x040
     59  1.1  jakllsch #define VIRTIO_MMIO_QUEUE_NOTIFY	0x050
     60  1.1  jakllsch #define VIRTIO_MMIO_INTERRUPT_STATUS	0x060
     61  1.1  jakllsch #define VIRTIO_MMIO_INTERRUPT_ACK	0x064
     62  1.1  jakllsch #define VIRTIO_MMIO_STATUS		0x070
     63  1.1  jakllsch #define VIRTIO_MMIO_CONFIG		0x100
     64  1.1  jakllsch 
     65  1.1  jakllsch #define VIRTIO_MMIO_INT_VRING		(1 << 0)
     66  1.1  jakllsch #define VIRTIO_MMIO_INT_CONFIG		(1 << 1)
     67  1.1  jakllsch 
     68  1.1  jakllsch /*
     69  1.4   reinoud  * MMIO configuration space for virtio-mmio v1 is in guest byte order.
     70  1.4   reinoud  *
     71  1.4   reinoud  * AArch64 BE is special in that its bus space functions always read little
     72  1.4   reinoud  * endian like on the PCI bus and thus need swapping to read host endian
     73  1.4   reinoud  * registers.
     74  1.4   reinoud  *
     75  1.4   reinoud  * XXX this might also be true for other big endian machines.
     76  1.4   reinoud  * XXX: TODO test virtio MMIO on non AArch64 big endian machines.
     77  1.3  jmcneill  */
     78  1.4   reinoud 
     79  1.3  jmcneill #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
     80  1.3  jmcneill #define	VIO16TOH(x)	le16toh(x)
     81  1.3  jmcneill #define	VIO32TOH(x)	le32toh(x)
     82  1.3  jmcneill #define	VIO64TOH(x)	le64toh(x)
     83  1.3  jmcneill #define	HTOVIO16(x)	htole16(x)
     84  1.3  jmcneill #define	HTOVIO32(x)	htole32(x)
     85  1.3  jmcneill #define	HTOVIO64(x)	htole64(x)
     86  1.4   reinoud #define VIODEVRW_SWAP false	/* can only be native endian now */
     87  1.3  jmcneill #else
     88  1.3  jmcneill #define	VIO16TOH(x)	(x)
     89  1.3  jmcneill #define	VIO32TOH(x)	(x)
     90  1.3  jmcneill #define	VIO64TOH(x)	(x)
     91  1.3  jmcneill #define	HTOVIO16(x)	(x)
     92  1.3  jmcneill #define	HTOVIO32(x)	(x)
     93  1.3  jmcneill #define	HTOVIO64(x)	(x)
     94  1.4   reinoud #define VIODEVRW_SWAP false	/* will only be native endian now */
     95  1.3  jmcneill #endif
     96  1.3  jmcneill 
     97  1.1  jakllsch 
     98  1.1  jakllsch static void	virtio_mmio_kick(struct virtio_softc *, uint16_t);
     99  1.1  jakllsch static uint8_t	virtio_mmio_read_device_config_1(struct virtio_softc *, int);
    100  1.1  jakllsch static uint16_t	virtio_mmio_read_device_config_2(struct virtio_softc *, int);
    101  1.1  jakllsch static uint32_t	virtio_mmio_read_device_config_4(struct virtio_softc *, int);
    102  1.1  jakllsch static uint64_t	virtio_mmio_read_device_config_8(struct virtio_softc *, int);
    103  1.1  jakllsch static void	virtio_mmio_write_device_config_1(struct virtio_softc *, int, uint8_t);
    104  1.1  jakllsch static void	virtio_mmio_write_device_config_2(struct virtio_softc *, int, uint16_t);
    105  1.1  jakllsch static void	virtio_mmio_write_device_config_4(struct virtio_softc *, int, uint32_t);
    106  1.1  jakllsch static void	virtio_mmio_write_device_config_8(struct virtio_softc *, int, uint64_t);
    107  1.4   reinoud 
    108  1.1  jakllsch static uint16_t	virtio_mmio_read_queue_size(struct virtio_softc *, uint16_t);
    109  1.4   reinoud static void	virtio_mmio_setup_queue(struct virtio_softc *, uint16_t, uint64_t);
    110  1.1  jakllsch static void	virtio_mmio_set_status(struct virtio_softc *, int);
    111  1.4   reinoud static void	virtio_mmio_negotiate_features(struct virtio_softc *, uint64_t);
    112  1.1  jakllsch static int	virtio_mmio_setup_interrupts(struct virtio_softc *);
    113  1.1  jakllsch static void	virtio_mmio_free_interrupts(struct virtio_softc *);
    114  1.1  jakllsch 
    115  1.1  jakllsch static const struct virtio_ops virtio_mmio_ops = {
    116  1.1  jakllsch 	.kick = virtio_mmio_kick,
    117  1.4   reinoud 
    118  1.1  jakllsch 	.read_dev_cfg_1 = virtio_mmio_read_device_config_1,
    119  1.1  jakllsch 	.read_dev_cfg_2 = virtio_mmio_read_device_config_2,
    120  1.1  jakllsch 	.read_dev_cfg_4 = virtio_mmio_read_device_config_4,
    121  1.1  jakllsch 	.read_dev_cfg_8 = virtio_mmio_read_device_config_8,
    122  1.1  jakllsch 	.write_dev_cfg_1 = virtio_mmio_write_device_config_1,
    123  1.1  jakllsch 	.write_dev_cfg_2 = virtio_mmio_write_device_config_2,
    124  1.1  jakllsch 	.write_dev_cfg_4 = virtio_mmio_write_device_config_4,
    125  1.1  jakllsch 	.write_dev_cfg_8 = virtio_mmio_write_device_config_8,
    126  1.4   reinoud 
    127  1.1  jakllsch 	.read_queue_size = virtio_mmio_read_queue_size,
    128  1.1  jakllsch 	.setup_queue = virtio_mmio_setup_queue,
    129  1.1  jakllsch 	.set_status = virtio_mmio_set_status,
    130  1.1  jakllsch 	.neg_features = virtio_mmio_negotiate_features,
    131  1.1  jakllsch 	.setup_interrupts = virtio_mmio_setup_interrupts,
    132  1.1  jakllsch 	.free_interrupts = virtio_mmio_free_interrupts,
    133  1.1  jakllsch };
    134  1.1  jakllsch 
    135  1.1  jakllsch static uint16_t
    136  1.1  jakllsch virtio_mmio_read_queue_size(struct virtio_softc *vsc, uint16_t idx)
    137  1.1  jakllsch {
    138  1.1  jakllsch 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    139  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
    140  1.1  jakllsch 	return bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    141  1.1  jakllsch 	    VIRTIO_MMIO_QUEUE_NUM_MAX);
    142  1.1  jakllsch }
    143  1.1  jakllsch 
    144  1.1  jakllsch static void
    145  1.4   reinoud virtio_mmio_setup_queue(struct virtio_softc *vsc, uint16_t idx, uint64_t addr)
    146  1.1  jakllsch {
    147  1.1  jakllsch 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    148  1.1  jakllsch 
    149  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_SEL, idx);
    150  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM,
    151  1.1  jakllsch 	    bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NUM_MAX));
    152  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_ALIGN,
    153  1.1  jakllsch 	    VIRTIO_PAGE_SIZE);
    154  1.4   reinoud 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_PFN,
    155  1.4   reinoud 	    addr / VIRTIO_PAGE_SIZE);
    156  1.1  jakllsch }
    157  1.1  jakllsch 
    158  1.1  jakllsch static void
    159  1.1  jakllsch virtio_mmio_set_status(struct virtio_softc *vsc, int status)
    160  1.1  jakllsch {
    161  1.1  jakllsch 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    162  1.1  jakllsch 	int old = 0;
    163  1.1  jakllsch 
    164  1.1  jakllsch 	if (status != 0)
    165  1.1  jakllsch 		old = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    166  1.1  jakllsch 				       VIRTIO_MMIO_STATUS);
    167  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_STATUS,
    168  1.1  jakllsch 			  status|old);
    169  1.1  jakllsch }
    170  1.1  jakllsch 
    171  1.1  jakllsch void
    172  1.1  jakllsch virtio_mmio_common_attach(struct virtio_mmio_softc *sc)
    173  1.1  jakllsch {
    174  1.1  jakllsch 	struct virtio_softc *vsc = &sc->sc_sc;
    175  1.4   reinoud 	device_t self = vsc->sc_dev;
    176  1.1  jakllsch 	uint32_t id, magic, ver;
    177  1.1  jakllsch 
    178  1.1  jakllsch 	magic = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    179  1.1  jakllsch 	    VIRTIO_MMIO_MAGIC_VALUE);
    180  1.1  jakllsch 	if (magic != VIRTIO_MMIO_MAGIC) {
    181  1.1  jakllsch 		aprint_error_dev(vsc->sc_dev,
    182  1.1  jakllsch 		    "wrong magic value 0x%08x; giving up\n", magic);
    183  1.1  jakllsch 		return;
    184  1.1  jakllsch 	}
    185  1.1  jakllsch 
    186  1.1  jakllsch 	ver = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_VERSION);
    187  1.1  jakllsch 	if (ver != 1) {
    188  1.1  jakllsch 		aprint_error_dev(vsc->sc_dev,
    189  1.1  jakllsch 		    "unknown version 0x%02x; giving up\n", ver);
    190  1.1  jakllsch 		return;
    191  1.1  jakllsch 	}
    192  1.1  jakllsch 
    193  1.1  jakllsch 	id = bus_space_read_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_DEVICE_ID);
    194  1.1  jakllsch 
    195  1.1  jakllsch 	/* we could use PAGE_SIZE, but virtio(4) assumes 4KiB for now */
    196  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_GUEST_PAGE_SIZE,
    197  1.1  jakllsch 	    VIRTIO_PAGE_SIZE);
    198  1.1  jakllsch 
    199  1.4   reinoud 	/* no device connected. */
    200  1.1  jakllsch 	if (id == 0)
    201  1.1  jakllsch 		return;
    202  1.1  jakllsch 
    203  1.4   reinoud 	virtio_print_device_type(self, id, ver);
    204  1.1  jakllsch 	vsc->sc_ops = &virtio_mmio_ops;
    205  1.4   reinoud 	vsc->sc_devcfg_swap = VIODEVRW_SWAP;
    206  1.4   reinoud 
    207  1.4   reinoud 	/* set up our device config tag */
    208  1.4   reinoud 	vsc->sc_devcfg_iosize = sc->sc_iosize - VIRTIO_MMIO_CONFIG;
    209  1.4   reinoud 	vsc->sc_devcfg_iot = sc->sc_iot;
    210  1.4   reinoud 	if (bus_space_subregion(sc->sc_iot, sc->sc_ioh,
    211  1.4   reinoud 			VIRTIO_MMIO_CONFIG, vsc->sc_devcfg_iosize,
    212  1.4   reinoud 			&vsc->sc_devcfg_ioh)) {
    213  1.4   reinoud 		aprint_error_dev(self, "can't map config i/o space\n");
    214  1.4   reinoud 		return;
    215  1.4   reinoud 	}
    216  1.1  jakllsch 
    217  1.1  jakllsch 	virtio_device_reset(vsc);
    218  1.1  jakllsch 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
    219  1.1  jakllsch 	virtio_mmio_set_status(vsc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
    220  1.1  jakllsch 
    221  1.1  jakllsch 	/* XXX: use softc as aux... */
    222  1.1  jakllsch 	vsc->sc_childdevid = id;
    223  1.1  jakllsch 	vsc->sc_child = NULL;
    224  1.1  jakllsch }
    225  1.1  jakllsch 
    226  1.1  jakllsch int
    227  1.1  jakllsch virtio_mmio_common_detach(struct virtio_mmio_softc *sc, int flags)
    228  1.1  jakllsch {
    229  1.1  jakllsch 	struct virtio_softc *vsc = &sc->sc_sc;
    230  1.1  jakllsch 	int r;
    231  1.1  jakllsch 
    232  1.1  jakllsch 	if (vsc->sc_child != NULL && vsc->sc_child != VIRTIO_CHILD_FAILED) {
    233  1.1  jakllsch 		r = config_detach(vsc->sc_child, flags);
    234  1.1  jakllsch 		if (r)
    235  1.1  jakllsch 			return r;
    236  1.1  jakllsch 	}
    237  1.1  jakllsch 	KASSERT(vsc->sc_child == NULL || vsc->sc_child == VIRTIO_CHILD_FAILED);
    238  1.1  jakllsch 	KASSERT(vsc->sc_vqs == NULL);
    239  1.1  jakllsch 	KASSERT(sc->sc_ih == NULL);
    240  1.1  jakllsch 
    241  1.1  jakllsch 	if (sc->sc_iosize) {
    242  1.1  jakllsch 		bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_iosize);
    243  1.1  jakllsch 		sc->sc_iosize = 0;
    244  1.1  jakllsch 	}
    245  1.1  jakllsch 
    246  1.1  jakllsch 	return 0;
    247  1.1  jakllsch }
    248  1.1  jakllsch 
    249  1.1  jakllsch /*
    250  1.1  jakllsch  * Feature negotiation.
    251  1.1  jakllsch  */
    252  1.4   reinoud static void
    253  1.4   reinoud virtio_mmio_negotiate_features(struct virtio_softc *vsc, uint64_t
    254  1.1  jakllsch     guest_features)
    255  1.1  jakllsch {
    256  1.1  jakllsch 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    257  1.1  jakllsch 	uint32_t r;
    258  1.1  jakllsch 
    259  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    260  1.1  jakllsch 	    VIRTIO_MMIO_HOST_FEATURES_SEL, 0);
    261  1.1  jakllsch 	r = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    262  1.1  jakllsch 				VIRTIO_MMIO_HOST_FEATURES);
    263  1.1  jakllsch 	r &= guest_features;
    264  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    265  1.1  jakllsch 	    VIRTIO_MMIO_GUEST_FEATURES_SEL, 0);
    266  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    267  1.1  jakllsch 			  VIRTIO_MMIO_GUEST_FEATURES, r);
    268  1.4   reinoud 
    269  1.4   reinoud 	vsc->sc_active_features = r;
    270  1.1  jakllsch }
    271  1.1  jakllsch 
    272  1.4   reinoud #
    273  1.1  jakllsch /*
    274  1.1  jakllsch  * Device configuration registers.
    275  1.1  jakllsch  */
    276  1.4   reinoud 
    277  1.4   reinoud /* ----------------------------------------------------
    278  1.4   reinoud  * Read/write device config code
    279  1.4   reinoud  * ----------------------------------------------------*/
    280  1.4   reinoud 
    281  1.1  jakllsch static uint8_t
    282  1.1  jakllsch virtio_mmio_read_device_config_1(struct virtio_softc *vsc, int index)
    283  1.1  jakllsch {
    284  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    285  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    286  1.4   reinoud 
    287  1.4   reinoud 	return bus_space_read_1(iot, ioh, index);
    288  1.1  jakllsch }
    289  1.1  jakllsch 
    290  1.1  jakllsch static uint16_t
    291  1.1  jakllsch virtio_mmio_read_device_config_2(struct virtio_softc *vsc, int index)
    292  1.1  jakllsch {
    293  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    294  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    295  1.4   reinoud 
    296  1.4   reinoud 	return VIO16TOH(bus_space_read_2(iot, ioh, index));
    297  1.1  jakllsch }
    298  1.1  jakllsch 
    299  1.1  jakllsch static uint32_t
    300  1.1  jakllsch virtio_mmio_read_device_config_4(struct virtio_softc *vsc, int index)
    301  1.1  jakllsch {
    302  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    303  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    304  1.4   reinoud 
    305  1.4   reinoud 	return VIO32TOH(bus_space_read_4(iot, ioh, index));
    306  1.1  jakllsch }
    307  1.1  jakllsch 
    308  1.1  jakllsch static uint64_t
    309  1.1  jakllsch virtio_mmio_read_device_config_8(struct virtio_softc *vsc, int index)
    310  1.1  jakllsch {
    311  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    312  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    313  1.1  jakllsch 	uint64_t r;
    314  1.1  jakllsch 
    315  1.4   reinoud 	r = bus_space_read_4(iot, ioh, index + sizeof(uint32_t));
    316  1.1  jakllsch 	r <<= 32;
    317  1.4   reinoud 	r += bus_space_read_4(iot, ioh, index);
    318  1.3  jmcneill 	return VIO64TOH(r);
    319  1.1  jakllsch }
    320  1.1  jakllsch 
    321  1.1  jakllsch static void
    322  1.1  jakllsch virtio_mmio_write_device_config_1(struct virtio_softc *vsc,
    323  1.1  jakllsch 			     int index, uint8_t value)
    324  1.1  jakllsch {
    325  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    326  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    327  1.4   reinoud 
    328  1.4   reinoud 	bus_space_write_1(iot, ioh, index, value);
    329  1.1  jakllsch }
    330  1.1  jakllsch 
    331  1.1  jakllsch static void
    332  1.1  jakllsch virtio_mmio_write_device_config_2(struct virtio_softc *vsc,
    333  1.1  jakllsch 			     int index, uint16_t value)
    334  1.1  jakllsch {
    335  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    336  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    337  1.4   reinoud 
    338  1.4   reinoud 	value = HTOVIO16(value);
    339  1.4   reinoud 	bus_space_write_2(iot, ioh, index, value);
    340  1.1  jakllsch }
    341  1.1  jakllsch 
    342  1.1  jakllsch static void
    343  1.1  jakllsch virtio_mmio_write_device_config_4(struct virtio_softc *vsc,
    344  1.1  jakllsch 			     int index, uint32_t value)
    345  1.1  jakllsch {
    346  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    347  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    348  1.4   reinoud 
    349  1.4   reinoud 	value = HTOVIO32(value);
    350  1.4   reinoud 	bus_space_write_4(iot, ioh, index, value);
    351  1.1  jakllsch }
    352  1.1  jakllsch 
    353  1.1  jakllsch static void
    354  1.1  jakllsch virtio_mmio_write_device_config_8(struct virtio_softc *vsc,
    355  1.1  jakllsch 			     int index, uint64_t value)
    356  1.1  jakllsch {
    357  1.4   reinoud 	bus_space_tag_t	   iot = vsc->sc_devcfg_iot;
    358  1.4   reinoud 	bus_space_handle_t ioh = vsc->sc_devcfg_ioh;
    359  1.4   reinoud 
    360  1.4   reinoud 	value = HTOVIO64(value);
    361  1.4   reinoud 	bus_space_write_4(iot, ioh, index, value & 0xffffffff);
    362  1.4   reinoud 	bus_space_write_4(iot, ioh, index + sizeof(uint32_t), value >> 32);
    363  1.1  jakllsch }
    364  1.1  jakllsch 
    365  1.4   reinoud 
    366  1.1  jakllsch /*
    367  1.1  jakllsch  * Interrupt handler.
    368  1.1  jakllsch  */
    369  1.1  jakllsch int
    370  1.1  jakllsch virtio_mmio_intr(void *arg)
    371  1.1  jakllsch {
    372  1.1  jakllsch 	struct virtio_mmio_softc *sc = arg;
    373  1.1  jakllsch 	struct virtio_softc *vsc = &sc->sc_sc;
    374  1.1  jakllsch 	int isr, r = 0;
    375  1.1  jakllsch 
    376  1.1  jakllsch 	/* check and ack the interrupt */
    377  1.1  jakllsch 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh,
    378  1.1  jakllsch 			       VIRTIO_MMIO_INTERRUPT_STATUS);
    379  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh,
    380  1.1  jakllsch 			  VIRTIO_MMIO_INTERRUPT_ACK, isr);
    381  1.1  jakllsch 	if ((isr & VIRTIO_MMIO_INT_CONFIG) &&
    382  1.1  jakllsch 	    (vsc->sc_config_change != NULL))
    383  1.1  jakllsch 		r = (vsc->sc_config_change)(vsc);
    384  1.1  jakllsch 	if ((isr & VIRTIO_MMIO_INT_VRING) &&
    385  1.1  jakllsch 	    (vsc->sc_intrhand != NULL)) {
    386  1.1  jakllsch 		if (vsc->sc_soft_ih != NULL)
    387  1.1  jakllsch 			softint_schedule(vsc->sc_soft_ih);
    388  1.1  jakllsch 		else
    389  1.1  jakllsch 			r |= (vsc->sc_intrhand)(vsc);
    390  1.1  jakllsch 	}
    391  1.1  jakllsch 
    392  1.1  jakllsch 	return r;
    393  1.1  jakllsch }
    394  1.1  jakllsch 
    395  1.1  jakllsch static void
    396  1.1  jakllsch virtio_mmio_kick(struct virtio_softc *vsc, uint16_t idx)
    397  1.1  jakllsch {
    398  1.1  jakllsch 	struct virtio_mmio_softc *sc = (struct virtio_mmio_softc *)vsc;
    399  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, VIRTIO_MMIO_QUEUE_NOTIFY,
    400  1.1  jakllsch 	    idx);
    401  1.1  jakllsch }
    402  1.1  jakllsch 
    403  1.1  jakllsch static int
    404  1.1  jakllsch virtio_mmio_setup_interrupts(struct virtio_softc *vsc)
    405  1.1  jakllsch {
    406  1.1  jakllsch 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
    407  1.1  jakllsch 
    408  1.1  jakllsch 	return sc->sc_setup_interrupts(sc);
    409  1.1  jakllsch }
    410  1.1  jakllsch 
    411  1.1  jakllsch static void
    412  1.1  jakllsch virtio_mmio_free_interrupts(struct virtio_softc *vsc)
    413  1.1  jakllsch {
    414  1.1  jakllsch 	struct virtio_mmio_softc * const sc = (struct virtio_mmio_softc *)vsc;
    415  1.1  jakllsch 
    416  1.1  jakllsch 	sc->sc_free_interrupts(sc);
    417  1.1  jakllsch }
    418