xref: /linux/include/linux/virtio_pci_modern.h (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VIRTIO_PCI_MODERN_H
3 #define _LINUX_VIRTIO_PCI_MODERN_H
4 
5 #include <linux/pci.h>
6 #include <linux/virtio_config.h>
7 #include <linux/virtio_pci.h>
8 
9 /**
10  * struct virtio_pci_modern_device - info for modern PCI virtio
11  * @pci_dev:	    Ptr to the PCI device struct
12  * @common:	    Position of the common capability in the PCI config
13  * @device:	    Device-specific data (non-legacy mode)
14  * @notify_base:    Base of vq notifications (non-legacy mode)
15  * @notify_pa:	    Physical base of vq notifications
16  * @isr:	    Where to read and clear interrupt
17  * @notify_len:	    So we can sanity-check accesses
18  * @device_len:	    So we can sanity-check accesses
19  * @notify_map_cap: Capability for when we need to map notifications per-vq
20  * @notify_offset_multiplier: Multiply queue_notify_off by this value
21  *                            (non-legacy mode).
22  * @modern_bars:    Bitmask of BARs
23  * @id:		    Device and vendor id
24  * @device_id_check: Callback defined before vp_modern_probe() to be used to
25  *		    verify the PCI device is a vendor's expected device rather
26  *		    than the standard virtio PCI device
27  *		    Returns the found device id or ERRNO
28  * @dma_mask:	    Optional mask instead of the traditional DMA_BIT_MASK(64),
29  *		    for vendor devices with DMA space address limitations
30  */
31 struct virtio_pci_modern_device {
32 	struct pci_dev *pci_dev;
33 
34 	struct virtio_pci_common_cfg __iomem *common;
35 	void __iomem *device;
36 	void __iomem *notify_base;
37 	resource_size_t notify_pa;
38 	u8 __iomem *isr;
39 
40 	size_t notify_len;
41 	size_t device_len;
42 	size_t common_len;
43 
44 	int notify_map_cap;
45 
46 	u32 notify_offset_multiplier;
47 	int modern_bars;
48 	struct virtio_device_id id;
49 
50 	int (*device_id_check)(struct pci_dev *pdev);
51 	u64 dma_mask;
52 };
53 
54 /*
55  * Type-safe wrappers for io accesses.
56  * Use these to enforce at compile time the following spec requirement:
57  *
58  * The driver MUST access each field using the “natural” access
59  * method, i.e. 32-bit accesses for 32-bit fields, 16-bit accesses
60  * for 16-bit fields and 8-bit accesses for 8-bit fields.
61  */
vp_ioread8(const u8 __iomem * addr)62 static inline u8 vp_ioread8(const u8 __iomem *addr)
63 {
64 	return ioread8(addr);
65 }
vp_ioread16(const __le16 __iomem * addr)66 static inline u16 vp_ioread16 (const __le16 __iomem *addr)
67 {
68 	return ioread16(addr);
69 }
70 
vp_ioread32(const __le32 __iomem * addr)71 static inline u32 vp_ioread32(const __le32 __iomem *addr)
72 {
73 	return ioread32(addr);
74 }
75 
vp_iowrite8(u8 value,u8 __iomem * addr)76 static inline void vp_iowrite8(u8 value, u8 __iomem *addr)
77 {
78 	iowrite8(value, addr);
79 }
80 
vp_iowrite16(u16 value,__le16 __iomem * addr)81 static inline void vp_iowrite16(u16 value, __le16 __iomem *addr)
82 {
83 	iowrite16(value, addr);
84 }
85 
vp_iowrite32(u32 value,__le32 __iomem * addr)86 static inline void vp_iowrite32(u32 value, __le32 __iomem *addr)
87 {
88 	iowrite32(value, addr);
89 }
90 
vp_iowrite64_twopart(u64 val,__le32 __iomem * lo,__le32 __iomem * hi)91 static inline void vp_iowrite64_twopart(u64 val,
92 					__le32 __iomem *lo,
93 					__le32 __iomem *hi)
94 {
95 	vp_iowrite32((u32)val, lo);
96 	vp_iowrite32(val >> 32, hi);
97 }
98 
99 void
100 vp_modern_get_driver_extended_features(struct virtio_pci_modern_device *mdev,
101 				       u64 *features);
102 void vp_modern_get_extended_features(struct virtio_pci_modern_device *mdev,
103 				     u64 *features);
104 void vp_modern_set_extended_features(struct virtio_pci_modern_device *mdev,
105 				     const u64 *features);
106 
107 static inline u64
vp_modern_get_features(struct virtio_pci_modern_device * mdev)108 vp_modern_get_features(struct virtio_pci_modern_device *mdev)
109 {
110 	u64 features_array[VIRTIO_FEATURES_DWORDS];
111 
112 	vp_modern_get_extended_features(mdev, features_array);
113 	return features_array[0];
114 }
115 
116 static inline u64
vp_modern_get_driver_features(struct virtio_pci_modern_device * mdev)117 vp_modern_get_driver_features(struct virtio_pci_modern_device *mdev)
118 {
119 	u64 features_array[VIRTIO_FEATURES_DWORDS];
120 	int i;
121 
122 	vp_modern_get_driver_extended_features(mdev, features_array);
123 	for (i = 1; i < VIRTIO_FEATURES_DWORDS; ++i)
124 		WARN_ON_ONCE(features_array[i]);
125 	return features_array[0];
126 }
127 
128 static inline void
vp_modern_set_features(struct virtio_pci_modern_device * mdev,u64 features)129 vp_modern_set_features(struct virtio_pci_modern_device *mdev, u64 features)
130 {
131 	u64 features_array[VIRTIO_FEATURES_DWORDS];
132 
133 	virtio_features_from_u64(features_array, features);
134 	vp_modern_set_extended_features(mdev, features_array);
135 }
136 
137 u32 vp_modern_generation(struct virtio_pci_modern_device *mdev);
138 u8 vp_modern_get_status(struct virtio_pci_modern_device *mdev);
139 void vp_modern_set_status(struct virtio_pci_modern_device *mdev,
140 		   u8 status);
141 u16 vp_modern_queue_vector(struct virtio_pci_modern_device *mdev,
142 			   u16 idx, u16 vector);
143 u16 vp_modern_config_vector(struct virtio_pci_modern_device *mdev,
144 		     u16 vector);
145 void vp_modern_queue_address(struct virtio_pci_modern_device *mdev,
146 			     u16 index, u64 desc_addr, u64 driver_addr,
147 			     u64 device_addr);
148 void vp_modern_set_queue_enable(struct virtio_pci_modern_device *mdev,
149 				u16 idx, bool enable);
150 bool vp_modern_get_queue_enable(struct virtio_pci_modern_device *mdev,
151 				u16 idx);
152 void vp_modern_set_queue_size(struct virtio_pci_modern_device *mdev,
153 			      u16 idx, u16 size);
154 u16 vp_modern_get_queue_size(struct virtio_pci_modern_device *mdev,
155 			     u16 idx);
156 u16 vp_modern_get_num_queues(struct virtio_pci_modern_device *mdev);
157 void __iomem * vp_modern_map_vq_notify(struct virtio_pci_modern_device *mdev,
158 				       u16 index, resource_size_t *pa);
159 int vp_modern_probe(struct virtio_pci_modern_device *mdev);
160 void vp_modern_remove(struct virtio_pci_modern_device *mdev);
161 int vp_modern_get_queue_reset(struct virtio_pci_modern_device *mdev, u16 index);
162 void vp_modern_set_queue_reset(struct virtio_pci_modern_device *mdev, u16 index);
163 u16 vp_modern_avq_num(struct virtio_pci_modern_device *mdev);
164 u16 vp_modern_avq_index(struct virtio_pci_modern_device *mdev);
165 #endif
166