Home | History | Annotate | Line # | Download | only in pci
virtio_pci.c revision 1.40
      1  1.40  yamaguch /* $NetBSD: virtio_pci.c,v 1.40 2023/03/31 07:34:26 yamaguchi Exp $ */
      2   1.1    cherry 
      3   1.1    cherry /*
      4  1.15   reinoud  * Copyright (c) 2020 The NetBSD Foundation, Inc.
      5  1.15   reinoud  * Copyright (c) 2012 Stefan Fritsch.
      6   1.1    cherry  * Copyright (c) 2010 Minoura Makoto.
      7   1.1    cherry  * All rights reserved.
      8   1.1    cherry  *
      9   1.1    cherry  * Redistribution and use in source and binary forms, with or without
     10   1.1    cherry  * modification, are permitted provided that the following conditions
     11   1.1    cherry  * are met:
     12   1.1    cherry  * 1. Redistributions of source code must retain the above copyright
     13   1.1    cherry  *    notice, this list of conditions and the following disclaimer.
     14   1.1    cherry  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1    cherry  *    notice, this list of conditions and the following disclaimer in the
     16   1.1    cherry  *    documentation and/or other materials provided with the distribution.
     17   1.1    cherry  *
     18   1.1    cherry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19   1.1    cherry  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20   1.1    cherry  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21   1.1    cherry  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22   1.1    cherry  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23   1.1    cherry  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24   1.1    cherry  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25   1.1    cherry  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26   1.1    cherry  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27   1.1    cherry  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1    cherry  */
     29   1.1    cherry 
     30   1.1    cherry #include <sys/cdefs.h>
     31  1.40  yamaguch __KERNEL_RCSID(0, "$NetBSD: virtio_pci.c,v 1.40 2023/03/31 07:34:26 yamaguchi Exp $");
     32   1.1    cherry 
     33   1.1    cherry #include <sys/param.h>
     34   1.1    cherry #include <sys/systm.h>
     35   1.4  jakllsch #include <sys/kmem.h>
     36   1.5  jakllsch #include <sys/module.h>
     37  1.19  christos #include <sys/endian.h>
     38   1.6  yamaguch #include <sys/interrupt.h>
     39  1.33  yamaguch #include <sys/syslog.h>
     40   1.1    cherry 
     41   1.1    cherry #include <sys/device.h>
     42   1.1    cherry 
     43   1.1    cherry #include <dev/pci/pcidevs.h>
     44   1.1    cherry #include <dev/pci/pcireg.h>
     45   1.1    cherry #include <dev/pci/pcivar.h>
     46   1.1    cherry 
     47  1.15   reinoud #include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
     48  1.15   reinoud #include <dev/pci/virtio_pcireg.h>
     49  1.15   reinoud 
     50   1.1    cherry #define VIRTIO_PRIVATE
     51  1.15   reinoud #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
     52   1.1    cherry 
     53   1.1    cherry 
     54  1.33  yamaguch #define VIRTIO_PCI_LOG(_sc, _use_log, _fmt, _args...)	\
     55  1.33  yamaguch do {							\
     56  1.33  yamaguch 	if ((_use_log)) {				\
     57  1.33  yamaguch 		log(LOG_DEBUG, "%s: " _fmt,		\
     58  1.33  yamaguch 		    device_xname((_sc)->sc_dev),	\
     59  1.33  yamaguch 		    ##_args);				\
     60  1.33  yamaguch 	} else {					\
     61  1.33  yamaguch 		aprint_error_dev((_sc)->sc_dev,		\
     62  1.33  yamaguch 		    _fmt, ##_args);			\
     63  1.33  yamaguch 	}						\
     64  1.33  yamaguch } while(0)
     65  1.33  yamaguch 
     66   1.4  jakllsch static int	virtio_pci_match(device_t, cfdata_t, void *);
     67   1.4  jakllsch static void	virtio_pci_attach(device_t, device_t, void *);
     68   1.4  jakllsch static int	virtio_pci_rescan(device_t, const char *, const int *);
     69   1.4  jakllsch static int	virtio_pci_detach(device_t, int);
     70   1.4  jakllsch 
     71  1.22   reinoud 
     72  1.22   reinoud #define NMAPREG		((PCI_MAPREG_END - PCI_MAPREG_START) / \
     73  1.22   reinoud 				sizeof(pcireg_t))
     74   1.4  jakllsch struct virtio_pci_softc {
     75   1.4  jakllsch 	struct virtio_softc	sc_sc;
     76  1.39  yamaguch 	bool			sc_intr_pervq;
     77  1.15   reinoud 
     78  1.15   reinoud 	/* IO space */
     79   1.4  jakllsch 	bus_space_tag_t		sc_iot;
     80   1.4  jakllsch 	bus_space_handle_t	sc_ioh;
     81   1.4  jakllsch 	bus_size_t		sc_iosize;
     82  1.15   reinoud 	bus_size_t		sc_mapped_iosize;
     83  1.15   reinoud 
     84  1.15   reinoud 	/* BARs */
     85  1.21   reinoud 	bus_space_tag_t		sc_bars_iot[NMAPREG];
     86  1.21   reinoud 	bus_space_handle_t	sc_bars_ioh[NMAPREG];
     87  1.21   reinoud 	bus_size_t		sc_bars_iosize[NMAPREG];
     88  1.15   reinoud 
     89  1.15   reinoud 	/* notify space */
     90  1.15   reinoud 	bus_space_tag_t		sc_notify_iot;
     91  1.15   reinoud 	bus_space_handle_t	sc_notify_ioh;
     92  1.15   reinoud 	bus_size_t		sc_notify_iosize;
     93  1.15   reinoud 	uint32_t		sc_notify_off_multiplier;
     94  1.15   reinoud 
     95  1.15   reinoud 	/* isr space */
     96  1.15   reinoud 	bus_space_tag_t		sc_isr_iot;
     97  1.15   reinoud 	bus_space_handle_t	sc_isr_ioh;
     98  1.15   reinoud 	bus_size_t		sc_isr_iosize;
     99  1.15   reinoud 
    100  1.15   reinoud 	/* generic */
    101   1.4  jakllsch 	struct pci_attach_args	sc_pa;
    102   1.4  jakllsch 	pci_intr_handle_t	*sc_ihp;
    103   1.4  jakllsch 	void			**sc_ihs;
    104   1.4  jakllsch 	int			sc_ihs_num;
    105  1.15   reinoud 	int			sc_devcfg_offset;	/* for 0.9 */
    106   1.4  jakllsch };
    107   1.4  jakllsch 
    108  1.15   reinoud static int	virtio_pci_attach_09(device_t, void *);
    109  1.15   reinoud static void	virtio_pci_kick_09(struct virtio_softc *, uint16_t);
    110  1.15   reinoud static uint16_t	virtio_pci_read_queue_size_09(struct virtio_softc *, uint16_t);
    111  1.15   reinoud static void	virtio_pci_setup_queue_09(struct virtio_softc *, uint16_t, uint64_t);
    112  1.15   reinoud static void	virtio_pci_set_status_09(struct virtio_softc *, int);
    113  1.15   reinoud static void	virtio_pci_negotiate_features_09(struct virtio_softc *, uint64_t);
    114  1.15   reinoud 
    115  1.15   reinoud static int	virtio_pci_attach_10(device_t, void *);
    116  1.15   reinoud static void	virtio_pci_kick_10(struct virtio_softc *, uint16_t);
    117  1.15   reinoud static uint16_t	virtio_pci_read_queue_size_10(struct virtio_softc *, uint16_t);
    118  1.15   reinoud static void	virtio_pci_setup_queue_10(struct virtio_softc *, uint16_t, uint64_t);
    119  1.15   reinoud static void	virtio_pci_set_status_10(struct virtio_softc *, int);
    120  1.15   reinoud static void	virtio_pci_negotiate_features_10(struct virtio_softc *, uint64_t);
    121  1.15   reinoud static int	virtio_pci_find_cap(struct virtio_pci_softc *psc, int cfg_type, void *buf, int buflen);
    122  1.15   reinoud 
    123  1.31  yamaguch static int	virtio_pci_alloc_interrupts(struct virtio_softc *);
    124   1.4  jakllsch static void	virtio_pci_free_interrupts(struct virtio_softc *);
    125  1.15   reinoud static int	virtio_pci_adjust_config_region(struct virtio_pci_softc *psc);
    126   1.4  jakllsch static int	virtio_pci_intr(void *arg);
    127   1.4  jakllsch static int	virtio_pci_msix_queue_intr(void *);
    128   1.4  jakllsch static int	virtio_pci_msix_config_intr(void *);
    129  1.32  yamaguch static int	virtio_pci_setup_interrupts_09(struct virtio_softc *, int);
    130  1.32  yamaguch static int	virtio_pci_setup_interrupts_10(struct virtio_softc *, int);
    131  1.31  yamaguch static int	virtio_pci_establish_msix_interrupts(struct virtio_softc *,
    132   1.4  jakllsch 		    struct pci_attach_args *);
    133  1.31  yamaguch static int	virtio_pci_establish_intx_interrupt(struct virtio_softc *,
    134   1.4  jakllsch 		    struct pci_attach_args *);
    135  1.31  yamaguch static bool	virtio_pci_msix_enabled(struct virtio_pci_softc *);
    136   1.4  jakllsch 
    137   1.4  jakllsch #define VIRTIO_MSIX_CONFIG_VECTOR_INDEX	0
    138   1.4  jakllsch #define VIRTIO_MSIX_QUEUE_VECTOR_INDEX	1
    139   1.4  jakllsch 
    140  1.27   reinoud /*
    141  1.27   reinoud  * When using PCI attached virtio on aarch64-eb under Qemu, the IO space
    142  1.27   reinoud  * suddenly read BIG_ENDIAN where it should stay LITTLE_ENDIAN. The data read
    143  1.27   reinoud  * 1 byte at a time seem OK but reading bigger lengths result in swapped
    144  1.27   reinoud  * endian. This is most notable on reading 8 byters since we can't use
    145  1.28   reinoud  * bus_space_{read,write}_8().
    146  1.27   reinoud  */
    147   1.4  jakllsch 
    148  1.27   reinoud #if defined(__aarch64__) && BYTE_ORDER == BIG_ENDIAN
    149  1.28   reinoud #	define READ_ENDIAN_09	BIG_ENDIAN	/* should be LITTLE_ENDIAN */
    150  1.27   reinoud #	define READ_ENDIAN_10	BIG_ENDIAN
    151  1.27   reinoud #	define STRUCT_ENDIAN_09	BIG_ENDIAN
    152  1.27   reinoud #	define STRUCT_ENDIAN_10	LITTLE_ENDIAN
    153  1.27   reinoud #elif BYTE_ORDER == BIG_ENDIAN
    154  1.27   reinoud #	define READ_ENDIAN_09	LITTLE_ENDIAN
    155  1.27   reinoud #	define READ_ENDIAN_10	BIG_ENDIAN
    156  1.27   reinoud #	define STRUCT_ENDIAN_09	BIG_ENDIAN
    157  1.27   reinoud #	define STRUCT_ENDIAN_10	LITTLE_ENDIAN
    158  1.27   reinoud #else /* little endian */
    159  1.27   reinoud #	define READ_ENDIAN_09	LITTLE_ENDIAN
    160  1.27   reinoud #	define READ_ENDIAN_10	LITTLE_ENDIAN
    161  1.27   reinoud #	define STRUCT_ENDIAN_09	LITTLE_ENDIAN
    162  1.27   reinoud #	define STRUCT_ENDIAN_10	LITTLE_ENDIAN
    163  1.15   reinoud #endif
    164  1.15   reinoud 
    165   1.1    cherry 
    166   1.4  jakllsch CFATTACH_DECL3_NEW(virtio_pci, sizeof(struct virtio_pci_softc),
    167   1.4  jakllsch     virtio_pci_match, virtio_pci_attach, virtio_pci_detach, NULL,
    168   1.4  jakllsch     virtio_pci_rescan, NULL, DVF_DETACH_SHUTDOWN);
    169   1.4  jakllsch 
    170  1.15   reinoud static const struct virtio_ops virtio_pci_ops_09 = {
    171  1.15   reinoud 	.kick = virtio_pci_kick_09,
    172  1.15   reinoud 	.read_queue_size = virtio_pci_read_queue_size_09,
    173  1.15   reinoud 	.setup_queue = virtio_pci_setup_queue_09,
    174  1.15   reinoud 	.set_status = virtio_pci_set_status_09,
    175  1.15   reinoud 	.neg_features = virtio_pci_negotiate_features_09,
    176  1.31  yamaguch 	.alloc_interrupts = virtio_pci_alloc_interrupts,
    177  1.15   reinoud 	.free_interrupts = virtio_pci_free_interrupts,
    178  1.31  yamaguch 	.setup_interrupts = virtio_pci_setup_interrupts_09,
    179  1.15   reinoud };
    180  1.15   reinoud 
    181  1.15   reinoud static const struct virtio_ops virtio_pci_ops_10 = {
    182  1.15   reinoud 	.kick = virtio_pci_kick_10,
    183  1.15   reinoud 	.read_queue_size = virtio_pci_read_queue_size_10,
    184  1.15   reinoud 	.setup_queue = virtio_pci_setup_queue_10,
    185  1.15   reinoud 	.set_status = virtio_pci_set_status_10,
    186  1.15   reinoud 	.neg_features = virtio_pci_negotiate_features_10,
    187  1.31  yamaguch 	.alloc_interrupts = virtio_pci_alloc_interrupts,
    188   1.4  jakllsch 	.free_interrupts = virtio_pci_free_interrupts,
    189  1.31  yamaguch 	.setup_interrupts = virtio_pci_setup_interrupts_10,
    190   1.4  jakllsch };
    191   1.1    cherry 
    192   1.1    cherry static int
    193   1.4  jakllsch virtio_pci_match(device_t parent, cfdata_t match, void *aux)
    194   1.1    cherry {
    195   1.1    cherry 	struct pci_attach_args *pa;
    196   1.1    cherry 
    197   1.1    cherry 	pa = (struct pci_attach_args *)aux;
    198   1.1    cherry 	switch (PCI_VENDOR(pa->pa_id)) {
    199   1.1    cherry 	case PCI_VENDOR_QUMRANET:
    200  1.34       uwe 		/* Transitional devices MUST have a PCI Revision ID of 0. */
    201  1.15   reinoud 		if (((PCI_PRODUCT_QUMRANET_VIRTIO_1000 <=
    202  1.15   reinoud 		      PCI_PRODUCT(pa->pa_id)) &&
    203  1.15   reinoud 		     (PCI_PRODUCT(pa->pa_id) <=
    204  1.15   reinoud 		      PCI_PRODUCT_QUMRANET_VIRTIO_103F)) &&
    205  1.15   reinoud 	              PCI_REVISION(pa->pa_class) == 0)
    206  1.15   reinoud 			return 1;
    207  1.34       uwe 		/*
    208  1.34       uwe 		 * Non-transitional devices SHOULD have a PCI Revision
    209  1.34       uwe 		 * ID of 1 or higher.  Drivers MUST match any PCI
    210  1.34       uwe 		 * Revision ID value.
    211  1.34       uwe 		 */
    212  1.15   reinoud 		if (((PCI_PRODUCT_QUMRANET_VIRTIO_1040 <=
    213  1.15   reinoud 		      PCI_PRODUCT(pa->pa_id)) &&
    214  1.15   reinoud 		     (PCI_PRODUCT(pa->pa_id) <=
    215  1.15   reinoud 		      PCI_PRODUCT_QUMRANET_VIRTIO_107F)) &&
    216  1.34       uwe 		      /* XXX: TODO */
    217  1.15   reinoud 		      PCI_REVISION(pa->pa_class) == 1)
    218   1.1    cherry 			return 1;
    219   1.1    cherry 		break;
    220   1.1    cherry 	}
    221   1.1    cherry 
    222   1.1    cherry 	return 0;
    223   1.1    cherry }
    224   1.1    cherry 
    225   1.1    cherry static void
    226   1.4  jakllsch virtio_pci_attach(device_t parent, device_t self, void *aux)
    227   1.1    cherry {
    228   1.4  jakllsch 	struct virtio_pci_softc * const psc = device_private(self);
    229   1.4  jakllsch 	struct virtio_softc * const sc = &psc->sc_sc;
    230   1.1    cherry 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    231   1.1    cherry 	pci_chipset_tag_t pc = pa->pa_pc;
    232   1.1    cherry 	pcitag_t tag = pa->pa_tag;
    233   1.1    cherry 	int revision;
    234  1.15   reinoud 	int ret;
    235   1.1    cherry 	pcireg_t id;
    236   1.2       uwe 	pcireg_t csr;
    237   1.1    cherry 
    238   1.1    cherry 	revision = PCI_REVISION(pa->pa_class);
    239  1.15   reinoud 	switch (revision) {
    240  1.15   reinoud 	case 0:
    241  1.15   reinoud 		/* subsystem ID shows what I am */
    242  1.15   reinoud 		id = PCI_SUBSYS_ID(pci_conf_read(pc, tag, PCI_SUBSYS_ID_REG));
    243  1.15   reinoud 		break;
    244  1.15   reinoud 	case 1:
    245  1.15   reinoud 		/* pci product number shows what I am */
    246  1.15   reinoud 		id = PCI_PRODUCT(pa->pa_id) - PCI_PRODUCT_QUMRANET_VIRTIO_1040;
    247  1.15   reinoud 		break;
    248  1.15   reinoud 	default:
    249   1.1    cherry 		aprint_normal(": unknown revision 0x%02x; giving up\n",
    250   1.1    cherry 			      revision);
    251   1.1    cherry 		return;
    252   1.1    cherry 	}
    253  1.15   reinoud 
    254   1.1    cherry 	aprint_normal("\n");
    255   1.1    cherry 	aprint_naive("\n");
    256  1.15   reinoud 	virtio_print_device_type(self, id, revision);
    257   1.1    cherry 
    258   1.2       uwe 	csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    259   1.2       uwe 	csr |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE;
    260   1.2       uwe 	pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
    261   1.2       uwe 
    262   1.1    cherry 	sc->sc_dev = self;
    263   1.4  jakllsch 	psc->sc_pa = *pa;
    264   1.4  jakllsch 	psc->sc_iot = pa->pa_iot;
    265  1.15   reinoud 
    266  1.15   reinoud 	sc->sc_dmat = pa->pa_dmat;
    267   1.1    cherry 	if (pci_dma64_available(pa))
    268   1.1    cherry 		sc->sc_dmat = pa->pa_dmat64;
    269   1.1    cherry 
    270  1.15   reinoud 	/* attach is dependent on revision */
    271  1.15   reinoud 	ret = 0;
    272  1.15   reinoud 	if (revision == 1) {
    273  1.15   reinoud 		/* try to attach 1.0 */
    274  1.15   reinoud 		ret = virtio_pci_attach_10(self, aux);
    275  1.15   reinoud 	}
    276  1.15   reinoud 	if (ret == 0 && revision == 0) {
    277  1.15   reinoud 		/* revision 0 means 0.9 only or both 0.9 and 1.0 */
    278  1.15   reinoud 		ret = virtio_pci_attach_09(self, aux);
    279  1.15   reinoud 	}
    280  1.15   reinoud 	if (ret) {
    281  1.15   reinoud 		aprint_error_dev(self, "cannot attach (%d)\n", ret);
    282   1.1    cherry 		return;
    283   1.1    cherry 	}
    284  1.15   reinoud 	KASSERT(sc->sc_ops);
    285  1.15   reinoud 
    286  1.15   reinoud 	/* preset config region */
    287  1.15   reinoud 	psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
    288  1.15   reinoud 	if (virtio_pci_adjust_config_region(psc))
    289  1.15   reinoud 		return;
    290   1.1    cherry 
    291  1.15   reinoud 	/* generic */
    292   1.1    cherry 	virtio_device_reset(sc);
    293   1.1    cherry 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
    294   1.1    cherry 	virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
    295   1.1    cherry 
    296  1.15   reinoud 	sc->sc_childdevid = id;
    297   1.1    cherry 	sc->sc_child = NULL;
    298  1.29   thorpej 	virtio_pci_rescan(self, NULL, NULL);
    299   1.1    cherry 	return;
    300   1.1    cherry }
    301   1.1    cherry 
    302   1.1    cherry /* ARGSUSED */
    303   1.1    cherry static int
    304  1.29   thorpej virtio_pci_rescan(device_t self, const char *ifattr, const int *locs)
    305   1.1    cherry {
    306   1.4  jakllsch 	struct virtio_pci_softc * const psc = device_private(self);
    307   1.4  jakllsch 	struct virtio_softc * const sc = &psc->sc_sc;
    308   1.1    cherry 	struct virtio_attach_args va;
    309   1.1    cherry 
    310   1.1    cherry 	if (sc->sc_child)	/* Child already attached? */
    311   1.1    cherry 		return 0;
    312   1.1    cherry 
    313   1.1    cherry 	memset(&va, 0, sizeof(va));
    314   1.1    cherry 	va.sc_childdevid = sc->sc_childdevid;
    315   1.1    cherry 
    316  1.30   thorpej 	config_found(self, &va, NULL, CFARGS_NONE);
    317   1.1    cherry 
    318  1.15   reinoud 	if (virtio_attach_failed(sc))
    319   1.1    cherry 		return 0;
    320   1.1    cherry 
    321   1.1    cherry 	return 0;
    322   1.1    cherry }
    323   1.1    cherry 
    324   1.1    cherry 
    325   1.1    cherry static int
    326   1.4  jakllsch virtio_pci_detach(device_t self, int flags)
    327   1.1    cherry {
    328   1.4  jakllsch 	struct virtio_pci_softc * const psc = device_private(self);
    329   1.4  jakllsch 	struct virtio_softc * const sc = &psc->sc_sc;
    330   1.1    cherry 	int r;
    331   1.1    cherry 
    332  1.40  yamaguch 	r = config_detach_children(self, flags);
    333  1.40  yamaguch 	if (r != 0)
    334  1.40  yamaguch 		return r;
    335   1.1    cherry 
    336   1.1    cherry 	/* Check that child detached properly */
    337  1.40  yamaguch 	KASSERT(ISSET(sc->sc_child_flags, VIRTIO_CHILD_DETACHED));
    338   1.1    cherry 	KASSERT(sc->sc_vqs == NULL);
    339   1.4  jakllsch 	KASSERT(psc->sc_ihs_num == 0);
    340   1.1    cherry 
    341   1.4  jakllsch 	if (psc->sc_iosize)
    342  1.15   reinoud 		bus_space_unmap(psc->sc_iot, psc->sc_ioh,
    343  1.15   reinoud 			psc->sc_mapped_iosize);
    344   1.4  jakllsch 	psc->sc_iosize = 0;
    345   1.1    cherry 
    346   1.1    cherry 	return 0;
    347   1.1    cherry }
    348   1.4  jakllsch 
    349  1.15   reinoud 
    350  1.15   reinoud static int
    351  1.15   reinoud virtio_pci_attach_09(device_t self, void *aux)
    352  1.15   reinoud 	//struct virtio_pci_softc *psc, struct pci_attach_args *pa)
    353  1.15   reinoud {
    354  1.15   reinoud 	struct virtio_pci_softc * const psc = device_private(self);
    355  1.15   reinoud 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    356  1.15   reinoud 	struct virtio_softc * const sc = &psc->sc_sc;
    357  1.15   reinoud //	pci_chipset_tag_t pc = pa->pa_pc;
    358  1.15   reinoud //	pcitag_t tag = pa->pa_tag;
    359  1.15   reinoud 
    360  1.15   reinoud 	/* complete IO region */
    361  1.15   reinoud 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
    362  1.15   reinoud 			   &psc->sc_iot, &psc->sc_ioh, NULL, &psc->sc_iosize)) {
    363  1.15   reinoud 		aprint_error_dev(self, "can't map i/o space\n");
    364  1.15   reinoud 		return EIO;
    365  1.15   reinoud 	}
    366  1.15   reinoud 	psc->sc_mapped_iosize = psc->sc_iosize;
    367  1.15   reinoud 
    368  1.15   reinoud 	/* queue space */
    369  1.15   reinoud 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    370  1.15   reinoud 			VIRTIO_CONFIG_QUEUE_NOTIFY, 2, &psc->sc_notify_ioh)) {
    371  1.15   reinoud 		aprint_error_dev(self, "can't map notify i/o space\n");
    372  1.15   reinoud 		return EIO;
    373  1.15   reinoud 	}
    374  1.15   reinoud 	psc->sc_notify_iosize = 2;
    375  1.15   reinoud 	psc->sc_notify_iot = psc->sc_iot;
    376  1.15   reinoud 
    377  1.15   reinoud 	/* ISR space */
    378  1.15   reinoud 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    379  1.15   reinoud 			VIRTIO_CONFIG_ISR_STATUS, 1, &psc->sc_isr_ioh)) {
    380  1.15   reinoud 		aprint_error_dev(self, "can't map isr i/o space\n");
    381  1.15   reinoud 		return EIO;
    382  1.15   reinoud 	}
    383  1.15   reinoud 	psc->sc_isr_iosize = 1;
    384  1.15   reinoud 	psc->sc_isr_iot = psc->sc_iot;
    385  1.15   reinoud 
    386  1.15   reinoud 	/* set our version 0.9 ops */
    387  1.15   reinoud 	sc->sc_ops = &virtio_pci_ops_09;
    388  1.27   reinoud 	sc->sc_bus_endian    = READ_ENDIAN_09;
    389  1.27   reinoud 	sc->sc_struct_endian = STRUCT_ENDIAN_09;
    390  1.15   reinoud 	return 0;
    391  1.15   reinoud }
    392  1.15   reinoud 
    393  1.15   reinoud 
    394  1.15   reinoud static int
    395  1.15   reinoud virtio_pci_attach_10(device_t self, void *aux)
    396   1.4  jakllsch {
    397  1.15   reinoud 	struct virtio_pci_softc * const psc = device_private(self);
    398  1.15   reinoud 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
    399  1.15   reinoud 	struct virtio_softc * const sc = &psc->sc_sc;
    400  1.15   reinoud 	pci_chipset_tag_t pc = pa->pa_pc;
    401  1.15   reinoud 	pcitag_t tag = pa->pa_tag;
    402  1.15   reinoud 
    403  1.15   reinoud 	struct virtio_pci_cap common, isr, device;
    404  1.15   reinoud 	struct virtio_pci_notify_cap notify;
    405  1.15   reinoud 	int have_device_cfg = 0;
    406  1.15   reinoud 	bus_size_t bars[NMAPREG] = { 0 };
    407  1.15   reinoud 	int bars_idx[NMAPREG] = { 0 };
    408  1.15   reinoud 	struct virtio_pci_cap *caps[] = { &common, &isr, &device, &notify.cap };
    409  1.26   reinoud 	int i, j, ret = 0;
    410  1.15   reinoud 
    411  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_COMMON_CFG,
    412  1.15   reinoud 			&common, sizeof(common)))
    413  1.15   reinoud 		return ENODEV;
    414  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_NOTIFY_CFG,
    415  1.15   reinoud 			&notify, sizeof(notify)))
    416  1.15   reinoud 		return ENODEV;
    417  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_ISR_CFG,
    418  1.15   reinoud 			&isr, sizeof(isr)))
    419  1.15   reinoud 		return ENODEV;
    420  1.15   reinoud 	if (virtio_pci_find_cap(psc, VIRTIO_PCI_CAP_DEVICE_CFG,
    421  1.15   reinoud 			&device, sizeof(device)))
    422  1.15   reinoud 		memset(&device, 0, sizeof(device));
    423  1.15   reinoud 	else
    424  1.15   reinoud 		have_device_cfg = 1;
    425  1.15   reinoud 
    426  1.15   reinoud 	/* Figure out which bars we need to map */
    427  1.15   reinoud 	for (i = 0; i < __arraycount(caps); i++) {
    428  1.15   reinoud 		int bar = caps[i]->bar;
    429  1.15   reinoud 		bus_size_t len = caps[i]->offset + caps[i]->length;
    430  1.15   reinoud 		if (caps[i]->length == 0)
    431  1.15   reinoud 			continue;
    432  1.15   reinoud 		if (bars[bar] < len)
    433  1.15   reinoud 			bars[bar] = len;
    434  1.15   reinoud 	}
    435  1.15   reinoud 
    436  1.26   reinoud 	for (i = j = 0; i < __arraycount(bars); i++) {
    437  1.15   reinoud 		int reg;
    438  1.15   reinoud 		pcireg_t type;
    439  1.15   reinoud 		if (bars[i] == 0)
    440  1.15   reinoud 			continue;
    441  1.35       uwe 		reg = PCI_BAR(i);
    442  1.15   reinoud 		type = pci_mapreg_type(pc, tag, reg);
    443  1.15   reinoud 		if (pci_mapreg_map(pa, reg, type, 0,
    444  1.15   reinoud 				&psc->sc_bars_iot[j], &psc->sc_bars_ioh[j],
    445  1.15   reinoud 				NULL, &psc->sc_bars_iosize[j])) {
    446  1.15   reinoud 			aprint_error_dev(self, "can't map bar %u \n", i);
    447  1.15   reinoud 			ret = EIO;
    448  1.15   reinoud 			goto err;
    449  1.15   reinoud 		}
    450  1.17    martin 		aprint_debug_dev(self,
    451  1.17    martin 		    "bar[%d]: iot %p, size 0x%" PRIxBUSSIZE "\n",
    452  1.17    martin 		    j, psc->sc_bars_iot[j], psc->sc_bars_iosize[j]);
    453  1.15   reinoud 		bars_idx[i] = j;
    454  1.15   reinoud 		j++;
    455  1.15   reinoud 	}
    456  1.15   reinoud 
    457  1.15   reinoud 	i = bars_idx[notify.cap.bar];
    458  1.15   reinoud 	if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    459  1.15   reinoud 			notify.cap.offset, notify.cap.length,
    460  1.15   reinoud 			&psc->sc_notify_ioh)) {
    461  1.15   reinoud 		aprint_error_dev(self, "can't map notify i/o space\n");
    462  1.15   reinoud 		ret = EIO;
    463  1.15   reinoud 		goto err;
    464  1.15   reinoud 	}
    465  1.15   reinoud 	psc->sc_notify_iosize = notify.cap.length;
    466  1.15   reinoud 	psc->sc_notify_iot = psc->sc_bars_iot[i];
    467  1.15   reinoud 	psc->sc_notify_off_multiplier = le32toh(notify.notify_off_multiplier);
    468  1.15   reinoud 
    469  1.15   reinoud 	if (have_device_cfg) {
    470  1.15   reinoud 		i = bars_idx[device.bar];
    471  1.15   reinoud 		if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    472  1.15   reinoud 				device.offset, device.length,
    473  1.15   reinoud 				&sc->sc_devcfg_ioh)) {
    474  1.15   reinoud 			aprint_error_dev(self, "can't map devcfg i/o space\n");
    475  1.15   reinoud 			ret = EIO;
    476  1.15   reinoud 			goto err;
    477  1.15   reinoud 		}
    478  1.15   reinoud 		aprint_debug_dev(self,
    479  1.15   reinoud 			"device.offset = 0x%x, device.length = 0x%x\n",
    480  1.15   reinoud 			device.offset, device.length);
    481  1.15   reinoud 		sc->sc_devcfg_iosize = device.length;
    482  1.15   reinoud 		sc->sc_devcfg_iot = psc->sc_bars_iot[i];
    483  1.15   reinoud 	}
    484  1.15   reinoud 
    485  1.15   reinoud 	i = bars_idx[isr.bar];
    486  1.15   reinoud 	if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    487  1.15   reinoud 			isr.offset, isr.length, &psc->sc_isr_ioh)) {
    488  1.15   reinoud 		aprint_error_dev(self, "can't map isr i/o space\n");
    489  1.15   reinoud 		ret = EIO;
    490  1.15   reinoud 		goto err;
    491  1.15   reinoud 	}
    492  1.15   reinoud 	psc->sc_isr_iosize = isr.length;
    493  1.15   reinoud 	psc->sc_isr_iot = psc->sc_bars_iot[i];
    494  1.15   reinoud 
    495  1.15   reinoud 	i = bars_idx[common.bar];
    496  1.15   reinoud 	if (bus_space_subregion(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    497  1.15   reinoud 			common.offset, common.length, &psc->sc_ioh)) {
    498  1.15   reinoud 		aprint_error_dev(self, "can't map common i/o space\n");
    499  1.15   reinoud 		ret = EIO;
    500  1.15   reinoud 		goto err;
    501  1.15   reinoud 	}
    502  1.15   reinoud 	psc->sc_iosize = common.length;
    503  1.15   reinoud 	psc->sc_iot = psc->sc_bars_iot[i];
    504  1.15   reinoud 	psc->sc_mapped_iosize = psc->sc_bars_iosize[i];
    505  1.15   reinoud 
    506  1.15   reinoud 	psc->sc_sc.sc_version_1 = 1;
    507  1.15   reinoud 
    508  1.15   reinoud 	/* set our version 1.0 ops */
    509  1.15   reinoud 	sc->sc_ops = &virtio_pci_ops_10;
    510  1.27   reinoud 	sc->sc_bus_endian    = READ_ENDIAN_10;
    511  1.27   reinoud 	sc->sc_struct_endian = STRUCT_ENDIAN_10;
    512  1.15   reinoud 	return 0;
    513   1.4  jakllsch 
    514  1.15   reinoud err:
    515  1.23   reinoud 	/* undo our pci_mapreg_map()s */
    516  1.23   reinoud 	for (i = 0; i < __arraycount(bars); i++) {
    517  1.26   reinoud 		if (psc->sc_bars_iosize[i] == 0)
    518  1.23   reinoud 			continue;
    519  1.26   reinoud 		bus_space_unmap(psc->sc_bars_iot[i], psc->sc_bars_ioh[i],
    520  1.26   reinoud 				psc->sc_bars_iosize[i]);
    521  1.23   reinoud 	}
    522  1.15   reinoud 	return ret;
    523   1.4  jakllsch }
    524   1.4  jakllsch 
    525  1.15   reinoud /* v1.0 attach helper */
    526  1.15   reinoud static int
    527  1.15   reinoud virtio_pci_find_cap(struct virtio_pci_softc *psc, int cfg_type, void *buf, int buflen)
    528   1.4  jakllsch {
    529  1.15   reinoud 	device_t self = psc->sc_sc.sc_dev;
    530  1.15   reinoud 	pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
    531  1.15   reinoud 	pcitag_t tag = psc->sc_pa.pa_tag;
    532  1.15   reinoud 	unsigned int offset, i, len;
    533  1.15   reinoud 	union {
    534  1.15   reinoud 		pcireg_t reg[8];
    535  1.15   reinoud 		struct virtio_pci_cap vcap;
    536  1.15   reinoud 	} *v = buf;
    537  1.15   reinoud 
    538  1.15   reinoud 	if (buflen < sizeof(struct virtio_pci_cap))
    539  1.15   reinoud 		return ERANGE;
    540  1.15   reinoud 
    541  1.15   reinoud 	if (!pci_get_capability(pc, tag, PCI_CAP_VENDSPEC, &offset, &v->reg[0]))
    542  1.15   reinoud 		return ENOENT;
    543  1.15   reinoud 
    544  1.15   reinoud 	do {
    545  1.15   reinoud 		for (i = 0; i < 4; i++)
    546  1.15   reinoud 			v->reg[i] =
    547  1.15   reinoud 				le32toh(pci_conf_read(pc, tag, offset + i * 4));
    548  1.15   reinoud 		if (v->vcap.cfg_type == cfg_type)
    549  1.15   reinoud 			break;
    550  1.15   reinoud 		offset = v->vcap.cap_next;
    551  1.15   reinoud 	} while (offset != 0);
    552  1.15   reinoud 
    553  1.15   reinoud 	if (offset == 0)
    554  1.15   reinoud 		return ENOENT;
    555  1.15   reinoud 
    556  1.15   reinoud 	if (v->vcap.cap_len > sizeof(struct virtio_pci_cap)) {
    557  1.15   reinoud 		len = roundup(v->vcap.cap_len, sizeof(pcireg_t));
    558  1.15   reinoud 		if (len > buflen) {
    559  1.15   reinoud 			aprint_error_dev(self, "%s cap too large\n", __func__);
    560  1.15   reinoud 			return ERANGE;
    561  1.15   reinoud 		}
    562  1.15   reinoud 		for (i = 4; i < len / sizeof(pcireg_t);  i++)
    563  1.15   reinoud 			v->reg[i] =
    564  1.15   reinoud 				le32toh(pci_conf_read(pc, tag, offset + i * 4));
    565  1.15   reinoud 	}
    566  1.15   reinoud 
    567  1.15   reinoud 	/* endian fixup */
    568  1.15   reinoud 	v->vcap.offset = le32toh(v->vcap.offset);
    569  1.15   reinoud 	v->vcap.length = le32toh(v->vcap.length);
    570  1.15   reinoud 	return 0;
    571   1.4  jakllsch }
    572   1.4  jakllsch 
    573  1.15   reinoud 
    574  1.15   reinoud /* -------------------------------------
    575  1.15   reinoud  * Version 0.9 support
    576  1.15   reinoud  * -------------------------------------*/
    577  1.15   reinoud 
    578  1.15   reinoud static void
    579  1.15   reinoud virtio_pci_kick_09(struct virtio_softc *sc, uint16_t idx)
    580   1.4  jakllsch {
    581   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    582  1.15   reinoud 
    583  1.15   reinoud 	bus_space_write_2(psc->sc_notify_iot, psc->sc_notify_ioh, 0, idx);
    584   1.4  jakllsch }
    585   1.4  jakllsch 
    586  1.15   reinoud /* only applicable for v 0.9 but also called for 1.0 */
    587  1.15   reinoud static int
    588  1.15   reinoud virtio_pci_adjust_config_region(struct virtio_pci_softc *psc)
    589   1.4  jakllsch {
    590  1.37       uwe 	struct virtio_softc * const sc = &psc->sc_sc;
    591  1.37       uwe 	device_t self = sc->sc_dev;
    592  1.15   reinoud 
    593  1.15   reinoud 	if (psc->sc_sc.sc_version_1)
    594  1.15   reinoud 		return 0;
    595  1.15   reinoud 
    596  1.15   reinoud 	sc->sc_devcfg_iosize = psc->sc_iosize - psc->sc_devcfg_offset;
    597  1.15   reinoud 	sc->sc_devcfg_iot = psc->sc_iot;
    598  1.15   reinoud 	if (bus_space_subregion(psc->sc_iot, psc->sc_ioh,
    599  1.15   reinoud 			psc->sc_devcfg_offset, sc->sc_devcfg_iosize,
    600  1.15   reinoud 			&sc->sc_devcfg_ioh)) {
    601  1.15   reinoud 		aprint_error_dev(self, "can't map config i/o space\n");
    602  1.15   reinoud 		return EIO;
    603  1.15   reinoud 	}
    604  1.15   reinoud 
    605  1.15   reinoud 	return 0;
    606   1.4  jakllsch }
    607   1.4  jakllsch 
    608  1.15   reinoud static uint16_t
    609  1.15   reinoud virtio_pci_read_queue_size_09(struct virtio_softc *sc, uint16_t idx)
    610   1.4  jakllsch {
    611   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    612   1.4  jakllsch 
    613  1.15   reinoud 	bus_space_write_2(psc->sc_iot, psc->sc_ioh,
    614  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_SELECT, idx);
    615  1.15   reinoud 	return bus_space_read_2(psc->sc_iot, psc->sc_ioh,
    616  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_SIZE);
    617   1.4  jakllsch }
    618   1.4  jakllsch 
    619   1.4  jakllsch static void
    620  1.15   reinoud virtio_pci_setup_queue_09(struct virtio_softc *sc, uint16_t idx, uint64_t addr)
    621   1.4  jakllsch {
    622   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    623   1.4  jakllsch 
    624  1.15   reinoud 	bus_space_write_2(psc->sc_iot, psc->sc_ioh,
    625  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_SELECT, idx);
    626  1.15   reinoud 	bus_space_write_4(psc->sc_iot, psc->sc_ioh,
    627  1.15   reinoud 	    VIRTIO_CONFIG_QUEUE_ADDRESS, addr / VIRTIO_PAGE_SIZE);
    628  1.15   reinoud 
    629  1.15   reinoud 	if (psc->sc_ihs_num > 1) {
    630  1.15   reinoud 		int vec = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    631  1.39  yamaguch 		if (psc->sc_intr_pervq)
    632  1.15   reinoud 			vec += idx;
    633  1.15   reinoud 		bus_space_write_2(psc->sc_iot, psc->sc_ioh,
    634  1.15   reinoud 		    VIRTIO_CONFIG_MSI_QUEUE_VECTOR, vec);
    635  1.15   reinoud 	}
    636   1.4  jakllsch }
    637   1.4  jakllsch 
    638   1.4  jakllsch static void
    639  1.15   reinoud virtio_pci_set_status_09(struct virtio_softc *sc, int status)
    640   1.4  jakllsch {
    641   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    642  1.15   reinoud 	int old = 0;
    643   1.4  jakllsch 
    644  1.15   reinoud 	if (status != 0) {
    645  1.15   reinoud 	    old = bus_space_read_1(psc->sc_iot, psc->sc_ioh,
    646  1.15   reinoud 		VIRTIO_CONFIG_DEVICE_STATUS);
    647  1.15   reinoud 	}
    648  1.15   reinoud 	bus_space_write_1(psc->sc_iot, psc->sc_ioh,
    649  1.15   reinoud 	    VIRTIO_CONFIG_DEVICE_STATUS, status|old);
    650   1.4  jakllsch }
    651   1.4  jakllsch 
    652   1.4  jakllsch static void
    653  1.15   reinoud virtio_pci_negotiate_features_09(struct virtio_softc *sc, uint64_t guest_features)
    654   1.4  jakllsch {
    655   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    656  1.15   reinoud 	uint32_t r;
    657  1.15   reinoud 
    658  1.15   reinoud 	r = bus_space_read_4(psc->sc_iot, psc->sc_ioh,
    659  1.15   reinoud 	    VIRTIO_CONFIG_DEVICE_FEATURES);
    660  1.15   reinoud 
    661  1.15   reinoud 	r &= guest_features;
    662  1.15   reinoud 
    663  1.15   reinoud 	bus_space_write_4(psc->sc_iot, psc->sc_ioh,
    664  1.15   reinoud 	    VIRTIO_CONFIG_GUEST_FEATURES, r);
    665   1.4  jakllsch 
    666  1.15   reinoud 	sc->sc_active_features = r;
    667   1.4  jakllsch }
    668   1.4  jakllsch 
    669  1.15   reinoud /* -------------------------------------
    670  1.15   reinoud  * Version 1.0 support
    671  1.15   reinoud  * -------------------------------------*/
    672  1.15   reinoud 
    673   1.4  jakllsch static void
    674  1.15   reinoud virtio_pci_kick_10(struct virtio_softc *sc, uint16_t idx)
    675   1.4  jakllsch {
    676   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    677  1.15   reinoud 	unsigned offset = sc->sc_vqs[idx].vq_notify_off *
    678  1.15   reinoud 		psc->sc_notify_off_multiplier;
    679   1.4  jakllsch 
    680  1.15   reinoud 	bus_space_write_2(psc->sc_notify_iot, psc->sc_notify_ioh, offset, idx);
    681   1.4  jakllsch }
    682   1.4  jakllsch 
    683  1.15   reinoud 
    684   1.4  jakllsch static uint16_t
    685  1.15   reinoud virtio_pci_read_queue_size_10(struct virtio_softc *sc, uint16_t idx)
    686   1.4  jakllsch {
    687   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    688  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    689  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    690   1.4  jakllsch 
    691  1.15   reinoud 	bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, idx);
    692  1.15   reinoud 	return bus_space_read_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SIZE);
    693   1.4  jakllsch }
    694   1.4  jakllsch 
    695  1.18   reinoud /*
    696  1.36       uwe  * By definition little endian only in v1.0.  NB: "MAY" in the text
    697  1.36       uwe  * below refers to "independently" (i.e. the order of accesses) not
    698  1.36       uwe  * "32-bit" (which is restricted by the earlier "MUST").
    699  1.24   thorpej  *
    700  1.36       uwe  * 4.1.3.1 Driver Requirements: PCI Device Layout
    701  1.36       uwe  *
    702  1.36       uwe  * For device configuration access, the driver MUST use ... 32-bit
    703  1.36       uwe  * wide and aligned accesses for ... 64-bit wide fields.  For 64-bit
    704  1.36       uwe  * fields, the driver MAY access each of the high and low 32-bit parts
    705  1.36       uwe  * of the field independently.
    706  1.20  christos  */
    707  1.19  christos static __inline void
    708  1.24   thorpej virtio_pci_bus_space_write_8(bus_space_tag_t iot, bus_space_handle_t ioh,
    709  1.19  christos      bus_size_t offset, uint64_t value)
    710  1.19  christos {
    711  1.36       uwe #if _QUAD_HIGHWORD
    712  1.19  christos 	bus_space_write_4(iot, ioh, offset, BUS_ADDR_LO32(value));
    713  1.19  christos 	bus_space_write_4(iot, ioh, offset + 4, BUS_ADDR_HI32(value));
    714  1.19  christos #else
    715  1.19  christos 	bus_space_write_4(iot, ioh, offset, BUS_ADDR_HI32(value));
    716  1.19  christos 	bus_space_write_4(iot, ioh, offset + 4, BUS_ADDR_LO32(value));
    717  1.19  christos #endif
    718  1.19  christos }
    719  1.18   reinoud 
    720   1.4  jakllsch static void
    721  1.15   reinoud virtio_pci_setup_queue_10(struct virtio_softc *sc, uint16_t idx, uint64_t addr)
    722   1.4  jakllsch {
    723   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    724  1.15   reinoud 	struct virtqueue *vq = &sc->sc_vqs[idx];
    725  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    726  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    727  1.15   reinoud 	KASSERT(vq->vq_index == idx);
    728  1.15   reinoud 
    729  1.15   reinoud 	bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, vq->vq_index);
    730  1.15   reinoud 	if (addr == 0) {
    731  1.15   reinoud 		bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_ENABLE, 0);
    732  1.24   thorpej 		virtio_pci_bus_space_write_8(iot, ioh,
    733  1.24   thorpej 		    VIRTIO_CONFIG1_QUEUE_DESC,   0);
    734  1.24   thorpej 		virtio_pci_bus_space_write_8(iot, ioh,
    735  1.24   thorpej 		    VIRTIO_CONFIG1_QUEUE_AVAIL,  0);
    736  1.24   thorpej 		virtio_pci_bus_space_write_8(iot, ioh,
    737  1.24   thorpej 		    VIRTIO_CONFIG1_QUEUE_USED,   0);
    738  1.15   reinoud 	} else {
    739  1.24   thorpej 		virtio_pci_bus_space_write_8(iot, ioh,
    740  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_DESC, addr);
    741  1.24   thorpej 		virtio_pci_bus_space_write_8(iot, ioh,
    742  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_AVAIL, addr + vq->vq_availoffset);
    743  1.24   thorpej 		virtio_pci_bus_space_write_8(iot, ioh,
    744  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_USED, addr + vq->vq_usedoffset);
    745  1.15   reinoud 		bus_space_write_2(iot, ioh,
    746  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_ENABLE, 1);
    747  1.15   reinoud 		vq->vq_notify_off = bus_space_read_2(iot, ioh,
    748  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_NOTIFY_OFF);
    749  1.15   reinoud 	}
    750   1.4  jakllsch 
    751   1.4  jakllsch 	if (psc->sc_ihs_num > 1) {
    752   1.4  jakllsch 		int vec = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    753  1.39  yamaguch 		if (psc->sc_intr_pervq)
    754   1.4  jakllsch 			vec += idx;
    755  1.15   reinoud 		bus_space_write_2(iot, ioh,
    756  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR, vec);
    757   1.4  jakllsch 	}
    758   1.4  jakllsch }
    759   1.4  jakllsch 
    760   1.4  jakllsch static void
    761  1.15   reinoud virtio_pci_set_status_10(struct virtio_softc *sc, int status)
    762   1.4  jakllsch {
    763   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    764  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    765  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    766   1.4  jakllsch 	int old = 0;
    767   1.4  jakllsch 
    768  1.15   reinoud 	if (status)
    769  1.15   reinoud 		old = bus_space_read_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS);
    770  1.15   reinoud 	bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS, status | old);
    771  1.15   reinoud }
    772  1.15   reinoud 
    773  1.15   reinoud void
    774  1.15   reinoud virtio_pci_negotiate_features_10(struct virtio_softc *sc, uint64_t guest_features)
    775  1.15   reinoud {
    776  1.15   reinoud 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    777  1.15   reinoud 	device_t self          =  sc->sc_dev;
    778  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    779  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    780  1.15   reinoud 	uint64_t host, negotiated, device_status;
    781  1.15   reinoud 
    782  1.15   reinoud 	guest_features |= VIRTIO_F_VERSION_1;
    783  1.15   reinoud 	/* notify on empty is 0.9 only */
    784  1.15   reinoud 	guest_features &= ~VIRTIO_F_NOTIFY_ON_EMPTY;
    785  1.15   reinoud 	sc->sc_active_features = 0;
    786  1.15   reinoud 
    787  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE_SELECT, 0);
    788  1.15   reinoud 	host = bus_space_read_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE);
    789  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE_SELECT, 1);
    790  1.15   reinoud 	host |= (uint64_t)
    791  1.15   reinoud 		bus_space_read_4(iot, ioh, VIRTIO_CONFIG1_DEVICE_FEATURE) << 32;
    792  1.15   reinoud 
    793  1.15   reinoud 	negotiated = host & guest_features;
    794  1.15   reinoud 
    795  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE_SELECT, 0);
    796  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE,
    797  1.15   reinoud 			negotiated & 0xffffffff);
    798  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE_SELECT, 1);
    799  1.15   reinoud 	bus_space_write_4(iot, ioh, VIRTIO_CONFIG1_DRIVER_FEATURE,
    800  1.15   reinoud 			negotiated >> 32);
    801  1.15   reinoud 	virtio_pci_set_status_10(sc, VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK);
    802  1.15   reinoud 
    803  1.15   reinoud 	device_status = bus_space_read_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS);
    804  1.15   reinoud 	if ((device_status & VIRTIO_CONFIG_DEVICE_STATUS_FEATURES_OK) == 0) {
    805  1.15   reinoud 		aprint_error_dev(self, "feature negotiation failed\n");
    806  1.15   reinoud 		bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS,
    807  1.15   reinoud 				VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
    808  1.15   reinoud 		return;
    809  1.15   reinoud 	}
    810  1.15   reinoud 
    811  1.15   reinoud 	if ((negotiated & VIRTIO_F_VERSION_1) == 0) {
    812  1.15   reinoud 		aprint_error_dev(self, "host rejected version 1\n");
    813  1.15   reinoud 		bus_space_write_1(iot, ioh, VIRTIO_CONFIG1_DEVICE_STATUS,
    814  1.15   reinoud 				VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
    815  1.15   reinoud 		return;
    816   1.4  jakllsch 	}
    817  1.15   reinoud 
    818  1.15   reinoud 	sc->sc_active_features = negotiated;
    819  1.15   reinoud 	return;
    820  1.15   reinoud }
    821  1.15   reinoud 
    822  1.15   reinoud 
    823  1.15   reinoud /* -------------------------------------
    824  1.15   reinoud  * Generic PCI interrupt code
    825  1.15   reinoud  * -------------------------------------*/
    826  1.15   reinoud 
    827  1.15   reinoud static int
    828  1.32  yamaguch virtio_pci_setup_interrupts_10(struct virtio_softc *sc, int reinit)
    829   1.4  jakllsch {
    830   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    831  1.15   reinoud 	bus_space_tag_t	   iot = psc->sc_iot;
    832  1.15   reinoud 	bus_space_handle_t ioh = psc->sc_ioh;
    833  1.15   reinoud 	int vector, ret, qid;
    834  1.15   reinoud 
    835  1.31  yamaguch 	if (!virtio_pci_msix_enabled(psc))
    836  1.31  yamaguch 		return 0;
    837  1.31  yamaguch 
    838  1.15   reinoud 	vector = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
    839  1.15   reinoud 	bus_space_write_2(iot, ioh,
    840  1.15   reinoud 		VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR, vector);
    841  1.15   reinoud 	ret = bus_space_read_2(iot, ioh, VIRTIO_CONFIG1_CONFIG_MSIX_VECTOR);
    842  1.15   reinoud 	if (ret != vector) {
    843  1.33  yamaguch 		VIRTIO_PCI_LOG(sc, reinit,
    844  1.33  yamaguch 		    "can't set config msix vector\n");
    845  1.15   reinoud 		return -1;
    846  1.15   reinoud 	}
    847  1.15   reinoud 
    848  1.15   reinoud 	for (qid = 0; qid < sc->sc_nvqs; qid++) {
    849  1.15   reinoud 		vector = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    850   1.4  jakllsch 
    851  1.39  yamaguch 		if (psc->sc_intr_pervq)
    852  1.15   reinoud 			vector += qid;
    853  1.15   reinoud 		bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_SELECT, qid);
    854  1.15   reinoud 		bus_space_write_2(iot, ioh, VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR,
    855  1.15   reinoud 			vector);
    856  1.15   reinoud 		ret = bus_space_read_2(iot, ioh,
    857  1.15   reinoud 			VIRTIO_CONFIG1_QUEUE_MSIX_VECTOR);
    858  1.15   reinoud 		if (ret != vector) {
    859  1.33  yamaguch 			VIRTIO_PCI_LOG(sc, reinit, "can't set queue %d "
    860  1.33  yamaguch 			    "msix vector\n", qid);
    861  1.15   reinoud 			return -1;
    862  1.15   reinoud 		}
    863  1.15   reinoud 	}
    864   1.4  jakllsch 
    865  1.15   reinoud 	return 0;
    866   1.4  jakllsch }
    867   1.4  jakllsch 
    868   1.4  jakllsch static int
    869  1.32  yamaguch virtio_pci_setup_interrupts_09(struct virtio_softc *sc, int reinit)
    870   1.4  jakllsch {
    871   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    872   1.4  jakllsch 	int offset, vector, ret, qid;
    873   1.4  jakllsch 
    874  1.31  yamaguch 	if (!virtio_pci_msix_enabled(psc))
    875  1.31  yamaguch 		return 0;
    876  1.31  yamaguch 
    877   1.4  jakllsch 	offset = VIRTIO_CONFIG_MSI_CONFIG_VECTOR;
    878   1.4  jakllsch 	vector = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
    879   1.4  jakllsch 
    880  1.15   reinoud 	bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, vector);
    881  1.15   reinoud 	ret = bus_space_read_2(psc->sc_iot, psc->sc_ioh, offset);
    882  1.15   reinoud 	if (ret != vector) {
    883  1.38  riastrad 		aprint_debug_dev(sc->sc_dev, "%s: expected=%d, actual=%d\n",
    884  1.38  riastrad 		    __func__, vector, ret);
    885  1.33  yamaguch 		VIRTIO_PCI_LOG(sc, reinit,
    886  1.33  yamaguch 		    "can't set config msix vector\n");
    887   1.4  jakllsch 		return -1;
    888  1.15   reinoud 	}
    889   1.4  jakllsch 
    890   1.4  jakllsch 	for (qid = 0; qid < sc->sc_nvqs; qid++) {
    891   1.4  jakllsch 		offset = VIRTIO_CONFIG_QUEUE_SELECT;
    892  1.15   reinoud 		bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, qid);
    893   1.4  jakllsch 
    894   1.4  jakllsch 		offset = VIRTIO_CONFIG_MSI_QUEUE_VECTOR;
    895   1.4  jakllsch 		vector = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    896   1.4  jakllsch 
    897  1.39  yamaguch 		if (psc->sc_intr_pervq)
    898   1.6  yamaguch 			vector += qid;
    899   1.6  yamaguch 
    900  1.15   reinoud 		bus_space_write_2(psc->sc_iot, psc->sc_ioh, offset, vector);
    901  1.15   reinoud 		ret = bus_space_read_2(psc->sc_iot, psc->sc_ioh, offset);
    902  1.15   reinoud 		if (ret != vector) {
    903  1.38  riastrad 			aprint_debug_dev(sc->sc_dev, "%s[qid=%d]:"
    904  1.38  riastrad 			    " expected=%d, actual=%d\n",
    905  1.38  riastrad 			    __func__, qid, vector, ret);
    906  1.33  yamaguch 			VIRTIO_PCI_LOG(sc, reinit, "can't set queue %d "
    907  1.33  yamaguch 			    "msix vector\n", qid);
    908   1.4  jakllsch 			return -1;
    909  1.15   reinoud 		}
    910   1.4  jakllsch 	}
    911   1.4  jakllsch 
    912   1.4  jakllsch 	return 0;
    913   1.4  jakllsch }
    914   1.4  jakllsch 
    915   1.4  jakllsch static int
    916  1.31  yamaguch virtio_pci_establish_msix_interrupts(struct virtio_softc *sc,
    917   1.4  jakllsch     struct pci_attach_args *pa)
    918   1.4  jakllsch {
    919   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
    920   1.4  jakllsch 	device_t self = sc->sc_dev;
    921   1.4  jakllsch 	pci_chipset_tag_t pc = pa->pa_pc;
    922   1.9  yamaguch 	struct virtqueue *vq;
    923   1.4  jakllsch 	char intrbuf[PCI_INTRSTR_LEN];
    924   1.6  yamaguch 	char intr_xname[INTRDEVNAMEBUF];
    925   1.4  jakllsch 	char const *intrstr;
    926   1.6  yamaguch 	int idx, qid, n;
    927   1.4  jakllsch 
    928   1.4  jakllsch 	idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
    929  1.15   reinoud 	if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
    930   1.4  jakllsch 		pci_intr_setattr(pc, &psc->sc_ihp[idx], PCI_INTR_MPSAFE, true);
    931   1.4  jakllsch 
    932   1.6  yamaguch 	snprintf(intr_xname, sizeof(intr_xname), "%s config",
    933   1.6  yamaguch 	    device_xname(sc->sc_dev));
    934   1.6  yamaguch 
    935   1.4  jakllsch 	psc->sc_ihs[idx] = pci_intr_establish_xname(pc, psc->sc_ihp[idx],
    936   1.6  yamaguch 	    sc->sc_ipl, virtio_pci_msix_config_intr, sc, intr_xname);
    937   1.4  jakllsch 	if (psc->sc_ihs[idx] == NULL) {
    938   1.4  jakllsch 		aprint_error_dev(self, "couldn't establish MSI-X for config\n");
    939   1.4  jakllsch 		goto error;
    940   1.4  jakllsch 	}
    941   1.4  jakllsch 
    942   1.4  jakllsch 	idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    943  1.39  yamaguch 	if (psc->sc_intr_pervq) {
    944   1.6  yamaguch 		for (qid = 0; qid < sc->sc_nvqs; qid++) {
    945   1.6  yamaguch 			n = idx + qid;
    946   1.9  yamaguch 			vq = &sc->sc_vqs[qid];
    947   1.6  yamaguch 
    948   1.6  yamaguch 			snprintf(intr_xname, sizeof(intr_xname), "%s vq#%d",
    949   1.6  yamaguch 			    device_xname(sc->sc_dev), qid);
    950   1.6  yamaguch 
    951  1.15   reinoud 			if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE) {
    952   1.6  yamaguch 				pci_intr_setattr(pc, &psc->sc_ihp[n],
    953   1.6  yamaguch 				    PCI_INTR_MPSAFE, true);
    954   1.6  yamaguch 			}
    955   1.6  yamaguch 
    956   1.6  yamaguch 			psc->sc_ihs[n] = pci_intr_establish_xname(pc, psc->sc_ihp[n],
    957  1.10  yamaguch 			    sc->sc_ipl, vq->vq_intrhand, vq->vq_intrhand_arg, intr_xname);
    958   1.6  yamaguch 			if (psc->sc_ihs[n] == NULL) {
    959   1.6  yamaguch 				aprint_error_dev(self, "couldn't establish MSI-X for a vq\n");
    960   1.6  yamaguch 				goto error;
    961   1.6  yamaguch 			}
    962   1.6  yamaguch 		}
    963   1.6  yamaguch 	} else {
    964  1.15   reinoud 		if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
    965   1.6  yamaguch 			pci_intr_setattr(pc, &psc->sc_ihp[idx], PCI_INTR_MPSAFE, true);
    966   1.4  jakllsch 
    967   1.6  yamaguch 		snprintf(intr_xname, sizeof(intr_xname), "%s queues",
    968   1.6  yamaguch 		    device_xname(sc->sc_dev));
    969   1.6  yamaguch 		psc->sc_ihs[idx] = pci_intr_establish_xname(pc, psc->sc_ihp[idx],
    970   1.6  yamaguch 		    sc->sc_ipl, virtio_pci_msix_queue_intr, sc, intr_xname);
    971   1.6  yamaguch 		if (psc->sc_ihs[idx] == NULL) {
    972   1.6  yamaguch 			aprint_error_dev(self, "couldn't establish MSI-X for queues\n");
    973   1.6  yamaguch 			goto error;
    974   1.6  yamaguch 		}
    975   1.4  jakllsch 	}
    976   1.4  jakllsch 
    977   1.4  jakllsch 	idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
    978   1.4  jakllsch 	intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf, sizeof(intrbuf));
    979   1.4  jakllsch 	aprint_normal_dev(self, "config interrupting at %s\n", intrstr);
    980   1.4  jakllsch 	idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
    981  1.39  yamaguch 	if (psc->sc_intr_pervq) {
    982   1.6  yamaguch 		kcpuset_t *affinity;
    983   1.6  yamaguch 		int affinity_to, r;
    984   1.6  yamaguch 
    985   1.6  yamaguch 		kcpuset_create(&affinity, false);
    986   1.6  yamaguch 
    987   1.6  yamaguch 		for (qid = 0; qid < sc->sc_nvqs; qid++) {
    988   1.6  yamaguch 			n = idx + qid;
    989   1.6  yamaguch 			affinity_to = (qid / 2) % ncpu;
    990   1.6  yamaguch 
    991   1.6  yamaguch 			intrstr = pci_intr_string(pc, psc->sc_ihp[n],
    992   1.6  yamaguch 			    intrbuf, sizeof(intrbuf));
    993   1.6  yamaguch 
    994   1.6  yamaguch 			kcpuset_zero(affinity);
    995   1.6  yamaguch 			kcpuset_set(affinity, affinity_to);
    996   1.6  yamaguch 			r = interrupt_distribute(psc->sc_ihs[n], affinity, NULL);
    997   1.6  yamaguch 			if (r == 0) {
    998   1.6  yamaguch 				aprint_normal_dev(self,
    999   1.6  yamaguch 				    "for vq #%d interrupting at %s affinity to %u\n",
   1000   1.6  yamaguch 				    qid, intrstr, affinity_to);
   1001   1.6  yamaguch 			} else {
   1002   1.6  yamaguch 				aprint_normal_dev(self,
   1003   1.6  yamaguch 				    "for vq #%d interrupting at %s\n",
   1004   1.6  yamaguch 				    qid, intrstr);
   1005   1.6  yamaguch 			}
   1006   1.6  yamaguch 		}
   1007   1.6  yamaguch 
   1008   1.6  yamaguch 		kcpuset_destroy(affinity);
   1009   1.6  yamaguch 	} else {
   1010   1.6  yamaguch 		intrstr = pci_intr_string(pc, psc->sc_ihp[idx], intrbuf, sizeof(intrbuf));
   1011   1.6  yamaguch 		aprint_normal_dev(self, "queues interrupting at %s\n", intrstr);
   1012   1.6  yamaguch 	}
   1013   1.4  jakllsch 
   1014   1.4  jakllsch 	return 0;
   1015   1.4  jakllsch 
   1016   1.4  jakllsch error:
   1017   1.4  jakllsch 	idx = VIRTIO_MSIX_CONFIG_VECTOR_INDEX;
   1018   1.4  jakllsch 	if (psc->sc_ihs[idx] != NULL)
   1019   1.4  jakllsch 		pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[idx]);
   1020   1.4  jakllsch 	idx = VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1021  1.39  yamaguch 	if (psc->sc_intr_pervq) {
   1022   1.6  yamaguch 		for (qid = 0; qid < sc->sc_nvqs; qid++) {
   1023   1.6  yamaguch 			n = idx + qid;
   1024   1.6  yamaguch 			if (psc->sc_ihs[n] == NULL)
   1025   1.6  yamaguch 				continue;
   1026   1.6  yamaguch 			pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[n]);
   1027   1.6  yamaguch 		}
   1028   1.6  yamaguch 
   1029   1.6  yamaguch 	} else {
   1030   1.6  yamaguch 		if (psc->sc_ihs[idx] != NULL)
   1031   1.6  yamaguch 			pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[idx]);
   1032   1.6  yamaguch 	}
   1033   1.4  jakllsch 
   1034   1.4  jakllsch 	return -1;
   1035   1.4  jakllsch }
   1036   1.4  jakllsch 
   1037   1.4  jakllsch static int
   1038  1.31  yamaguch virtio_pci_establish_intx_interrupt(struct virtio_softc *sc,
   1039   1.4  jakllsch     struct pci_attach_args *pa)
   1040   1.4  jakllsch {
   1041   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1042   1.4  jakllsch 	device_t self = sc->sc_dev;
   1043   1.4  jakllsch 	pci_chipset_tag_t pc = pa->pa_pc;
   1044   1.4  jakllsch 	char intrbuf[PCI_INTRSTR_LEN];
   1045   1.4  jakllsch 	char const *intrstr;
   1046   1.4  jakllsch 
   1047  1.15   reinoud 	if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
   1048   1.4  jakllsch 		pci_intr_setattr(pc, &psc->sc_ihp[0], PCI_INTR_MPSAFE, true);
   1049   1.4  jakllsch 
   1050   1.4  jakllsch 	psc->sc_ihs[0] = pci_intr_establish_xname(pc, psc->sc_ihp[0],
   1051   1.4  jakllsch 	    sc->sc_ipl, virtio_pci_intr, sc, device_xname(sc->sc_dev));
   1052   1.4  jakllsch 	if (psc->sc_ihs[0] == NULL) {
   1053   1.4  jakllsch 		aprint_error_dev(self, "couldn't establish INTx\n");
   1054   1.4  jakllsch 		return -1;
   1055   1.4  jakllsch 	}
   1056   1.4  jakllsch 
   1057   1.4  jakllsch 	intrstr = pci_intr_string(pc, psc->sc_ihp[0], intrbuf, sizeof(intrbuf));
   1058   1.4  jakllsch 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
   1059   1.4  jakllsch 
   1060   1.4  jakllsch 	return 0;
   1061   1.4  jakllsch }
   1062   1.4  jakllsch 
   1063   1.4  jakllsch static int
   1064  1.31  yamaguch virtio_pci_alloc_interrupts(struct virtio_softc *sc)
   1065   1.4  jakllsch {
   1066   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1067   1.4  jakllsch 	device_t self = sc->sc_dev;
   1068   1.4  jakllsch 	pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
   1069  1.13  jakllsch 	pcitag_t tag = psc->sc_pa.pa_tag;
   1070   1.4  jakllsch 	int error;
   1071   1.4  jakllsch 	int nmsix;
   1072  1.13  jakllsch 	int off;
   1073   1.4  jakllsch 	int counts[PCI_INTR_TYPE_SIZE];
   1074   1.4  jakllsch 	pci_intr_type_t max_type;
   1075  1.13  jakllsch 	pcireg_t ctl;
   1076   1.4  jakllsch 
   1077   1.4  jakllsch 	nmsix = pci_msix_count(psc->sc_pa.pa_pc, psc->sc_pa.pa_tag);
   1078   1.4  jakllsch 	aprint_debug_dev(self, "pci_msix_count=%d\n", nmsix);
   1079   1.4  jakllsch 
   1080   1.4  jakllsch 	/* We need at least two: one for config and the other for queues */
   1081  1.15   reinoud 	if ((sc->sc_flags & VIRTIO_F_INTR_MSIX) == 0 || nmsix < 2) {
   1082   1.4  jakllsch 		/* Try INTx only */
   1083   1.4  jakllsch 		max_type = PCI_INTR_TYPE_INTX;
   1084   1.4  jakllsch 		counts[PCI_INTR_TYPE_INTX] = 1;
   1085   1.4  jakllsch 	} else {
   1086   1.4  jakllsch 		/* Try MSI-X first and INTx second */
   1087  1.39  yamaguch 		if (ISSET(sc->sc_flags, VIRTIO_F_INTR_PERVQ) &&
   1088  1.39  yamaguch 		    sc->sc_nvqs + VIRTIO_MSIX_QUEUE_VECTOR_INDEX <= nmsix) {
   1089  1.11  yamaguch 			nmsix = sc->sc_nvqs + VIRTIO_MSIX_QUEUE_VECTOR_INDEX;
   1090  1.11  yamaguch 		} else {
   1091   1.6  yamaguch 			nmsix = 2;
   1092   1.6  yamaguch 		}
   1093   1.6  yamaguch 
   1094   1.4  jakllsch 		max_type = PCI_INTR_TYPE_MSIX;
   1095   1.6  yamaguch 		counts[PCI_INTR_TYPE_MSIX] = nmsix;
   1096   1.4  jakllsch 		counts[PCI_INTR_TYPE_MSI] = 0;
   1097   1.4  jakllsch 		counts[PCI_INTR_TYPE_INTX] = 1;
   1098   1.4  jakllsch 	}
   1099   1.4  jakllsch 
   1100   1.4  jakllsch retry:
   1101   1.4  jakllsch 	error = pci_intr_alloc(&psc->sc_pa, &psc->sc_ihp, counts, max_type);
   1102   1.4  jakllsch 	if (error != 0) {
   1103   1.4  jakllsch 		aprint_error_dev(self, "couldn't map interrupt\n");
   1104   1.4  jakllsch 		return -1;
   1105   1.4  jakllsch 	}
   1106   1.4  jakllsch 
   1107   1.4  jakllsch 	if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_MSIX) {
   1108  1.39  yamaguch 		psc->sc_intr_pervq = nmsix > 2 ? true : false;
   1109  1.12  jakllsch 		psc->sc_ihs = kmem_zalloc(sizeof(*psc->sc_ihs) * nmsix,
   1110   1.4  jakllsch 		    KM_SLEEP);
   1111   1.4  jakllsch 
   1112  1.31  yamaguch 		error = virtio_pci_establish_msix_interrupts(sc, &psc->sc_pa);
   1113   1.4  jakllsch 		if (error != 0) {
   1114   1.6  yamaguch 			kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * nmsix);
   1115   1.6  yamaguch 			pci_intr_release(pc, psc->sc_ihp, nmsix);
   1116   1.4  jakllsch 
   1117   1.4  jakllsch 			/* Retry INTx */
   1118   1.4  jakllsch 			max_type = PCI_INTR_TYPE_INTX;
   1119   1.4  jakllsch 			counts[PCI_INTR_TYPE_INTX] = 1;
   1120   1.4  jakllsch 			goto retry;
   1121   1.4  jakllsch 		}
   1122   1.4  jakllsch 
   1123   1.6  yamaguch 		psc->sc_ihs_num = nmsix;
   1124  1.15   reinoud 		psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_MSI;
   1125  1.15   reinoud 		virtio_pci_adjust_config_region(psc);
   1126   1.4  jakllsch 	} else if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_INTX) {
   1127  1.39  yamaguch 		psc->sc_intr_pervq = false;
   1128  1.12  jakllsch 		psc->sc_ihs = kmem_zalloc(sizeof(*psc->sc_ihs) * 1,
   1129   1.4  jakllsch 		    KM_SLEEP);
   1130   1.4  jakllsch 
   1131  1.31  yamaguch 		error = virtio_pci_establish_intx_interrupt(sc, &psc->sc_pa);
   1132   1.4  jakllsch 		if (error != 0) {
   1133   1.4  jakllsch 			kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * 1);
   1134   1.4  jakllsch 			pci_intr_release(pc, psc->sc_ihp, 1);
   1135   1.4  jakllsch 			return -1;
   1136   1.4  jakllsch 		}
   1137   1.4  jakllsch 
   1138   1.4  jakllsch 		psc->sc_ihs_num = 1;
   1139  1.15   reinoud 		psc->sc_devcfg_offset = VIRTIO_CONFIG_DEVICE_CONFIG_NOMSI;
   1140  1.15   reinoud 		virtio_pci_adjust_config_region(psc);
   1141  1.13  jakllsch 
   1142  1.14  jakllsch 		error = pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL);
   1143  1.13  jakllsch 		if (error != 0) {
   1144  1.13  jakllsch 			ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
   1145  1.13  jakllsch 			ctl &= ~PCI_MSIX_CTL_ENABLE;
   1146  1.13  jakllsch 			pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
   1147  1.13  jakllsch 		}
   1148   1.4  jakllsch 	}
   1149   1.4  jakllsch 
   1150  1.39  yamaguch 	if (!psc->sc_intr_pervq)
   1151  1.39  yamaguch 		CLR(sc->sc_flags, VIRTIO_F_INTR_PERVQ);
   1152   1.4  jakllsch 	return 0;
   1153   1.4  jakllsch }
   1154   1.4  jakllsch 
   1155   1.4  jakllsch static void
   1156   1.4  jakllsch virtio_pci_free_interrupts(struct virtio_softc *sc)
   1157   1.4  jakllsch {
   1158   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1159   1.4  jakllsch 
   1160   1.4  jakllsch 	for (int i = 0; i < psc->sc_ihs_num; i++) {
   1161   1.4  jakllsch 		if (psc->sc_ihs[i] == NULL)
   1162   1.4  jakllsch 			continue;
   1163   1.4  jakllsch 		pci_intr_disestablish(psc->sc_pa.pa_pc, psc->sc_ihs[i]);
   1164   1.4  jakllsch 		psc->sc_ihs[i] = NULL;
   1165   1.4  jakllsch 	}
   1166   1.4  jakllsch 
   1167   1.4  jakllsch 	if (psc->sc_ihs_num > 0)
   1168   1.4  jakllsch 		pci_intr_release(psc->sc_pa.pa_pc, psc->sc_ihp, psc->sc_ihs_num);
   1169   1.4  jakllsch 
   1170   1.4  jakllsch 	if (psc->sc_ihs != NULL) {
   1171   1.4  jakllsch 		kmem_free(psc->sc_ihs, sizeof(*psc->sc_ihs) * psc->sc_ihs_num);
   1172   1.4  jakllsch 		psc->sc_ihs = NULL;
   1173   1.4  jakllsch 	}
   1174   1.4  jakllsch 	psc->sc_ihs_num = 0;
   1175   1.4  jakllsch }
   1176   1.4  jakllsch 
   1177  1.31  yamaguch static bool
   1178  1.31  yamaguch virtio_pci_msix_enabled(struct virtio_pci_softc *psc)
   1179  1.31  yamaguch {
   1180  1.31  yamaguch 	pci_chipset_tag_t pc = psc->sc_pa.pa_pc;
   1181  1.31  yamaguch 
   1182  1.31  yamaguch 	if (pci_intr_type(pc, psc->sc_ihp[0]) == PCI_INTR_TYPE_MSIX)
   1183  1.31  yamaguch 		return true;
   1184  1.31  yamaguch 
   1185  1.31  yamaguch 	return false;
   1186  1.31  yamaguch }
   1187  1.31  yamaguch 
   1188   1.4  jakllsch /*
   1189   1.4  jakllsch  * Interrupt handler.
   1190   1.4  jakllsch  */
   1191   1.4  jakllsch static int
   1192   1.4  jakllsch virtio_pci_intr(void *arg)
   1193   1.4  jakllsch {
   1194   1.4  jakllsch 	struct virtio_softc *sc = arg;
   1195   1.4  jakllsch 	struct virtio_pci_softc * const psc = (struct virtio_pci_softc *)sc;
   1196   1.4  jakllsch 	int isr, r = 0;
   1197   1.4  jakllsch 
   1198   1.4  jakllsch 	/* check and ack the interrupt */
   1199  1.15   reinoud 	isr = bus_space_read_1(psc->sc_isr_iot, psc->sc_isr_ioh, 0);
   1200   1.4  jakllsch 	if (isr == 0)
   1201   1.4  jakllsch 		return 0;
   1202   1.4  jakllsch 	if ((isr & VIRTIO_CONFIG_ISR_CONFIG_CHANGE) &&
   1203   1.4  jakllsch 	    (sc->sc_config_change != NULL))
   1204   1.4  jakllsch 		r = (sc->sc_config_change)(sc);
   1205   1.4  jakllsch 	if (sc->sc_intrhand != NULL) {
   1206   1.4  jakllsch 		if (sc->sc_soft_ih != NULL)
   1207   1.4  jakllsch 			softint_schedule(sc->sc_soft_ih);
   1208   1.4  jakllsch 		else
   1209   1.4  jakllsch 			r |= (sc->sc_intrhand)(sc);
   1210   1.4  jakllsch 	}
   1211   1.4  jakllsch 
   1212   1.4  jakllsch 	return r;
   1213   1.4  jakllsch }
   1214   1.4  jakllsch 
   1215   1.4  jakllsch static int
   1216   1.4  jakllsch virtio_pci_msix_queue_intr(void *arg)
   1217   1.4  jakllsch {
   1218   1.4  jakllsch 	struct virtio_softc *sc = arg;
   1219   1.4  jakllsch 	int r = 0;
   1220   1.4  jakllsch 
   1221   1.4  jakllsch 	if (sc->sc_intrhand != NULL) {
   1222   1.4  jakllsch 		if (sc->sc_soft_ih != NULL)
   1223   1.4  jakllsch 			softint_schedule(sc->sc_soft_ih);
   1224   1.4  jakllsch 		else
   1225   1.4  jakllsch 			r |= (sc->sc_intrhand)(sc);
   1226   1.4  jakllsch 	}
   1227   1.4  jakllsch 
   1228   1.4  jakllsch 	return r;
   1229   1.4  jakllsch }
   1230   1.4  jakllsch 
   1231   1.4  jakllsch static int
   1232   1.4  jakllsch virtio_pci_msix_config_intr(void *arg)
   1233   1.4  jakllsch {
   1234   1.4  jakllsch 	struct virtio_softc *sc = arg;
   1235   1.4  jakllsch 	int r = 0;
   1236   1.4  jakllsch 
   1237   1.4  jakllsch 	if (sc->sc_config_change != NULL)
   1238   1.4  jakllsch 		r = (sc->sc_config_change)(sc);
   1239   1.4  jakllsch 	return r;
   1240   1.4  jakllsch }
   1241   1.5  jakllsch 
   1242   1.5  jakllsch MODULE(MODULE_CLASS_DRIVER, virtio_pci, "pci,virtio");
   1243   1.5  jakllsch 
   1244   1.5  jakllsch #ifdef _MODULE
   1245   1.5  jakllsch #include "ioconf.c"
   1246   1.5  jakllsch #endif
   1247   1.5  jakllsch 
   1248   1.5  jakllsch static int
   1249   1.5  jakllsch virtio_pci_modcmd(modcmd_t cmd, void *opaque)
   1250   1.5  jakllsch {
   1251   1.5  jakllsch 	int error = 0;
   1252   1.5  jakllsch 
   1253   1.5  jakllsch #ifdef _MODULE
   1254   1.5  jakllsch 	switch (cmd) {
   1255   1.5  jakllsch 	case MODULE_CMD_INIT:
   1256   1.5  jakllsch 		error = config_init_component(cfdriver_ioconf_virtio_pci,
   1257   1.5  jakllsch 		    cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
   1258   1.5  jakllsch 		break;
   1259   1.5  jakllsch 	case MODULE_CMD_FINI:
   1260   1.5  jakllsch 		error = config_fini_component(cfdriver_ioconf_virtio_pci,
   1261   1.5  jakllsch 		    cfattach_ioconf_virtio_pci, cfdata_ioconf_virtio_pci);
   1262   1.5  jakllsch 		break;
   1263   1.5  jakllsch 	default:
   1264   1.5  jakllsch 		error = ENOTTY;
   1265   1.5  jakllsch 		break;
   1266   1.5  jakllsch 	}
   1267   1.5  jakllsch #endif
   1268   1.5  jakllsch 
   1269   1.5  jakllsch 	return error;
   1270   1.5  jakllsch }
   1271