xref: /freebsd/sys/dev/virtio/pci/virtio_pci.h (revision 410556f1f10fd35b350102725fd8504c3cb0afc8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2017, Bryan Venteicher <bryanv@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice unmodified, this list of conditions, and the following
12  *    disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _VIRTIO_PCI_H
32 #define _VIRTIO_PCI_H
33 
34 struct vtpci_interrupt {
35 	struct resource		*vti_irq;
36 	int			 vti_rid;
37 	void			*vti_handler;
38 };
39 
40 struct vtpci_virtqueue {
41 	struct virtqueue	*vtv_vq;
42 	int			 vtv_no_intr;
43 	int			 vtv_notify_offset;
44 };
45 
46 struct vtpci_common {
47 	device_t			 vtpci_dev;
48 	uint64_t			 vtpci_host_features;
49 	uint64_t			 vtpci_features;
50 	struct vtpci_virtqueue		*vtpci_vqs;
51 	int				 vtpci_nvqs;
52 
53 	uint32_t			 vtpci_flags;
54 #define VTPCI_FLAG_NO_MSI		0x0001
55 #define VTPCI_FLAG_NO_MSIX		0x0002
56 #define VTPCI_FLAG_MODERN		0x0004
57 #define VTPCI_FLAG_INTX			0x1000
58 #define VTPCI_FLAG_MSI			0x2000
59 #define VTPCI_FLAG_MSIX			0x4000
60 #define VTPCI_FLAG_SHARED_MSIX		0x8000
61 #define VTPCI_FLAG_ITYPE_MASK		0xF000
62 
63 	/* The VirtIO PCI "bus" will only ever have one child. */
64 	device_t			 vtpci_child_dev;
65 	struct virtio_feature_desc	*vtpci_child_feat_desc;
66 
67 	/*
68 	 * Ideally, each virtqueue that the driver provides a callback for will
69 	 * receive its own MSIX vector. If there are not sufficient vectors
70 	 * available, then attempt to have all the VQs share one vector. For
71 	 * MSIX, the configuration changed notifications must be on their own
72 	 * vector.
73 	 *
74 	 * If MSIX is not available, attempt to have the whole device share
75 	 * one MSI vector, and then, finally, one intx interrupt.
76 	 */
77 	struct vtpci_interrupt		 vtpci_device_interrupt;
78 	struct vtpci_interrupt		*vtpci_msix_vq_interrupts;
79 	int				 vtpci_nmsix_resources;
80 };
81 
82 extern int vtpci_disable_msix;
83 
84 static inline device_t
85 vtpci_child_device(struct vtpci_common *cn)
86 {
87 	return (cn->vtpci_child_dev);
88 }
89 
90 static inline bool
91 vtpci_is_msix_available(struct vtpci_common *cn)
92 {
93 	return ((cn->vtpci_flags & VTPCI_FLAG_NO_MSIX) == 0);
94 }
95 
96 static inline bool
97 vtpci_is_msix_enabled(struct vtpci_common *cn)
98 {
99 	return ((cn->vtpci_flags & VTPCI_FLAG_MSIX) != 0);
100 }
101 
102 static inline bool
103 vtpci_is_modern(struct vtpci_common *cn)
104 {
105 	return ((cn->vtpci_flags & VTPCI_FLAG_MODERN) != 0);
106 }
107 
108 static inline int
109 vtpci_virtqueue_count(struct vtpci_common *cn)
110 {
111 	return (cn->vtpci_nvqs);
112 }
113 
114 void	vtpci_init(struct vtpci_common *cn, device_t dev, bool modern);
115 int	vtpci_add_child(struct vtpci_common *cn);
116 int	vtpci_delete_child(struct vtpci_common *cn);
117 void	vtpci_child_detached(struct vtpci_common *cn);
118 int	vtpci_reinit(struct vtpci_common *cn);
119 
120 uint64_t vtpci_negotiate_features(struct vtpci_common *cn,
121 	     uint64_t child_features, uint64_t host_features);
122 int	 vtpci_with_feature(struct vtpci_common *cn, uint64_t feature);
123 
124 int	vtpci_read_ivar(struct vtpci_common *cn, int index, uintptr_t *result);
125 int	vtpci_write_ivar(struct vtpci_common *cn, int index, uintptr_t value);
126 
127 int	vtpci_alloc_virtqueues(struct vtpci_common *cn, int flags, int nvqs,
128 	    struct vq_alloc_info *vq_info);
129 int	vtpci_setup_interrupts(struct vtpci_common *cn, enum intr_type type);
130 void	vtpci_release_child_resources(struct vtpci_common *cn);
131 
132 #endif /* _VIRTIO_PCI_H */
133