xref: /freebsd/sys/dev/virtio/pci/virtio_pci.h (revision 180c02405b12e08bbb69bb693a38077db0c85e9b)
1336f459cSPeter Grehan /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
37282444bSPedro F. Giffuni  *
49da9560cSBryan Venteicher  * Copyright (c) 2017, Bryan Venteicher <bryanv@FreeBSD.org>
59da9560cSBryan Venteicher  * All rights reserved.
610b59a9bSPeter Grehan  *
7336f459cSPeter Grehan  * Redistribution and use in source and binary forms, with or without
8336f459cSPeter Grehan  * modification, are permitted provided that the following conditions
9336f459cSPeter Grehan  * are met:
10336f459cSPeter Grehan  * 1. Redistributions of source code must retain the above copyright
119da9560cSBryan Venteicher  *    notice unmodified, this list of conditions, and the following
129da9560cSBryan Venteicher  *    disclaimer.
13336f459cSPeter Grehan  * 2. Redistributions in binary form must reproduce the above copyright
14336f459cSPeter Grehan  *    notice, this list of conditions and the following disclaimer in the
15336f459cSPeter Grehan  *    documentation and/or other materials provided with the distribution.
169da9560cSBryan Venteicher  *
179da9560cSBryan Venteicher  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
189da9560cSBryan Venteicher  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
199da9560cSBryan Venteicher  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
209da9560cSBryan Venteicher  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
219da9560cSBryan Venteicher  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
229da9560cSBryan Venteicher  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
239da9560cSBryan Venteicher  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
249da9560cSBryan Venteicher  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
259da9560cSBryan Venteicher  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
269da9560cSBryan Venteicher  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2710b59a9bSPeter Grehan  */
2810b59a9bSPeter Grehan 
2910b59a9bSPeter Grehan #ifndef _VIRTIO_PCI_H
3010b59a9bSPeter Grehan #define _VIRTIO_PCI_H
3110b59a9bSPeter Grehan 
329da9560cSBryan Venteicher struct vtpci_interrupt {
339da9560cSBryan Venteicher 	struct resource		*vti_irq;
349da9560cSBryan Venteicher 	int			 vti_rid;
359da9560cSBryan Venteicher 	void			*vti_handler;
369da9560cSBryan Venteicher };
3710b59a9bSPeter Grehan 
389da9560cSBryan Venteicher struct vtpci_virtqueue {
399da9560cSBryan Venteicher 	struct virtqueue	*vtv_vq;
409da9560cSBryan Venteicher 	int			 vtv_no_intr;
419da9560cSBryan Venteicher 	int			 vtv_notify_offset;
429da9560cSBryan Venteicher };
439da9560cSBryan Venteicher 
449da9560cSBryan Venteicher struct vtpci_common {
459da9560cSBryan Venteicher 	device_t			 vtpci_dev;
46703f17d6SBryan Venteicher 	uint64_t			 vtpci_host_features;
479da9560cSBryan Venteicher 	uint64_t			 vtpci_features;
489da9560cSBryan Venteicher 	struct vtpci_virtqueue		*vtpci_vqs;
499da9560cSBryan Venteicher 	int				 vtpci_nvqs;
509da9560cSBryan Venteicher 
519da9560cSBryan Venteicher 	uint32_t			 vtpci_flags;
529da9560cSBryan Venteicher #define VTPCI_FLAG_NO_MSI		0x0001
539da9560cSBryan Venteicher #define VTPCI_FLAG_NO_MSIX		0x0002
549da9560cSBryan Venteicher #define VTPCI_FLAG_MODERN		0x0004
559da9560cSBryan Venteicher #define VTPCI_FLAG_INTX			0x1000
569da9560cSBryan Venteicher #define VTPCI_FLAG_MSI			0x2000
579da9560cSBryan Venteicher #define VTPCI_FLAG_MSIX			0x4000
589da9560cSBryan Venteicher #define VTPCI_FLAG_SHARED_MSIX		0x8000
599da9560cSBryan Venteicher #define VTPCI_FLAG_ITYPE_MASK		0xF000
609da9560cSBryan Venteicher 
619da9560cSBryan Venteicher 	/* The VirtIO PCI "bus" will only ever have one child. */
629da9560cSBryan Venteicher 	device_t			 vtpci_child_dev;
639da9560cSBryan Venteicher 	struct virtio_feature_desc	*vtpci_child_feat_desc;
6410b59a9bSPeter Grehan 
6510b59a9bSPeter Grehan 	/*
669da9560cSBryan Venteicher 	 * Ideally, each virtqueue that the driver provides a callback for will
679da9560cSBryan Venteicher 	 * receive its own MSIX vector. If there are not sufficient vectors
689da9560cSBryan Venteicher 	 * available, then attempt to have all the VQs share one vector. For
699da9560cSBryan Venteicher 	 * MSIX, the configuration changed notifications must be on their own
709da9560cSBryan Venteicher 	 * vector.
719da9560cSBryan Venteicher 	 *
729da9560cSBryan Venteicher 	 * If MSIX is not available, attempt to have the whole device share
739da9560cSBryan Venteicher 	 * one MSI vector, and then, finally, one intx interrupt.
7410b59a9bSPeter Grehan 	 */
759da9560cSBryan Venteicher 	struct vtpci_interrupt		 vtpci_device_interrupt;
769da9560cSBryan Venteicher 	struct vtpci_interrupt		*vtpci_msix_vq_interrupts;
779da9560cSBryan Venteicher 	int				 vtpci_nmsix_resources;
789da9560cSBryan Venteicher };
7910b59a9bSPeter Grehan 
809da9560cSBryan Venteicher extern int vtpci_disable_msix;
8110b59a9bSPeter Grehan 
829da9560cSBryan Venteicher static inline device_t
vtpci_child_device(struct vtpci_common * cn)839da9560cSBryan Venteicher vtpci_child_device(struct vtpci_common *cn)
849da9560cSBryan Venteicher {
859da9560cSBryan Venteicher 	return (cn->vtpci_child_dev);
869da9560cSBryan Venteicher }
8710b59a9bSPeter Grehan 
889da9560cSBryan Venteicher static inline bool
vtpci_is_msix_available(struct vtpci_common * cn)899da9560cSBryan Venteicher vtpci_is_msix_available(struct vtpci_common *cn)
909da9560cSBryan Venteicher {
919da9560cSBryan Venteicher 	return ((cn->vtpci_flags & VTPCI_FLAG_NO_MSIX) == 0);
929da9560cSBryan Venteicher }
9310b59a9bSPeter Grehan 
949da9560cSBryan Venteicher static inline bool
vtpci_is_msix_enabled(struct vtpci_common * cn)959da9560cSBryan Venteicher vtpci_is_msix_enabled(struct vtpci_common *cn)
969da9560cSBryan Venteicher {
979da9560cSBryan Venteicher 	return ((cn->vtpci_flags & VTPCI_FLAG_MSIX) != 0);
989da9560cSBryan Venteicher }
999da9560cSBryan Venteicher 
1009da9560cSBryan Venteicher static inline bool
vtpci_is_modern(struct vtpci_common * cn)1019da9560cSBryan Venteicher vtpci_is_modern(struct vtpci_common *cn)
1029da9560cSBryan Venteicher {
1039da9560cSBryan Venteicher 	return ((cn->vtpci_flags & VTPCI_FLAG_MODERN) != 0);
1049da9560cSBryan Venteicher }
1059da9560cSBryan Venteicher 
1069da9560cSBryan Venteicher static inline int
vtpci_virtqueue_count(struct vtpci_common * cn)1079da9560cSBryan Venteicher vtpci_virtqueue_count(struct vtpci_common *cn)
1089da9560cSBryan Venteicher {
1099da9560cSBryan Venteicher 	return (cn->vtpci_nvqs);
1109da9560cSBryan Venteicher }
1119da9560cSBryan Venteicher 
1129da9560cSBryan Venteicher void	vtpci_init(struct vtpci_common *cn, device_t dev, bool modern);
1139da9560cSBryan Venteicher int	vtpci_add_child(struct vtpci_common *cn);
1149da9560cSBryan Venteicher int	vtpci_delete_child(struct vtpci_common *cn);
1159da9560cSBryan Venteicher void	vtpci_child_detached(struct vtpci_common *cn);
1169da9560cSBryan Venteicher int	vtpci_reinit(struct vtpci_common *cn);
1179da9560cSBryan Venteicher 
1189da9560cSBryan Venteicher uint64_t vtpci_negotiate_features(struct vtpci_common *cn,
1199da9560cSBryan Venteicher 	     uint64_t child_features, uint64_t host_features);
120ccb576a8SMina Galić bool	 vtpci_with_feature(struct vtpci_common *cn, uint64_t feature);
1219da9560cSBryan Venteicher 
1229da9560cSBryan Venteicher int	vtpci_read_ivar(struct vtpci_common *cn, int index, uintptr_t *result);
1239da9560cSBryan Venteicher int	vtpci_write_ivar(struct vtpci_common *cn, int index, uintptr_t value);
1249da9560cSBryan Venteicher 
125*180c0240SMina Galić int	vtpci_alloc_virtqueues(struct vtpci_common *cn, int nvqs,
1269da9560cSBryan Venteicher 	    struct vq_alloc_info *vq_info);
1279da9560cSBryan Venteicher int	vtpci_setup_interrupts(struct vtpci_common *cn, enum intr_type type);
1289da9560cSBryan Venteicher void	vtpci_release_child_resources(struct vtpci_common *cn);
12910b59a9bSPeter Grehan 
13010b59a9bSPeter Grehan #endif /* _VIRTIO_PCI_H */
131