• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_VDPA_H
3 #define _LINUX_VDPA_H
4 
5 #include <linux/kernel.h>
6 #include <linux/device.h>
7 #include <linux/interrupt.h>
8 #include <linux/vhost_iotlb.h>
9 
10 /**
11  * vDPA callback definition.
12  * @callback: interrupt callback function
13  * @private: the data passed to the callback function
14  */
15 struct vdpa_callback {
16 	irqreturn_t (*callback)(void *data);
17 	void *private;
18 };
19 
20 /**
21  * vDPA notification area
22  * @addr: base address of the notification area
23  * @size: size of the notification area
24  */
25 struct vdpa_notification_area {
26 	resource_size_t addr;
27 	resource_size_t size;
28 };
29 
30 /**
31  * vDPA vq_state definition
32  * @avail_index: available index
33  */
34 struct vdpa_vq_state {
35 	u16	avail_index;
36 };
37 
38 /**
39  * vDPA device - representation of a vDPA device
40  * @dev: underlying device
41  * @dma_dev: the actual device that is performing DMA
42  * @config: the configuration ops for this device.
43  * @index: device index
44  * @features_valid: were features initialized? for legacy guests
45  */
46 struct vdpa_device {
47 	struct device dev;
48 	struct device *dma_dev;
49 	const struct vdpa_config_ops *config;
50 	unsigned int index;
51 	bool features_valid;
52 	int nvqs;
53 };
54 
55 /**
56  * vDPA IOVA range - the IOVA range support by the device
57  * @first: start of the IOVA range
58  * @last: end of the IOVA range
59  */
60 struct vdpa_iova_range {
61 	u64 first;
62 	u64 last;
63 };
64 
65 /**
66  * vDPA_config_ops - operations for configuring a vDPA device.
67  * Note: vDPA device drivers are required to implement all of the
68  * operations unless it is mentioned to be optional in the following
69  * list.
70  *
71  * @set_vq_address:		Set the address of virtqueue
72  *				@vdev: vdpa device
73  *				@idx: virtqueue index
74  *				@desc_area: address of desc area
75  *				@driver_area: address of driver area
76  *				@device_area: address of device area
77  *				Returns integer: success (0) or error (< 0)
78  * @set_vq_num:			Set the size of virtqueue
79  *				@vdev: vdpa device
80  *				@idx: virtqueue index
81  *				@num: the size of virtqueue
82  * @kick_vq:			Kick the virtqueue
83  *				@vdev: vdpa device
84  *				@idx: virtqueue index
85  * @set_vq_cb:			Set the interrupt callback function for
86  *				a virtqueue
87  *				@vdev: vdpa device
88  *				@idx: virtqueue index
89  *				@cb: virtio-vdev interrupt callback structure
90  * @set_vq_ready:		Set ready status for a virtqueue
91  *				@vdev: vdpa device
92  *				@idx: virtqueue index
93  *				@ready: ready (true) not ready(false)
94  * @get_vq_ready:		Get ready status for a virtqueue
95  *				@vdev: vdpa device
96  *				@idx: virtqueue index
97  *				Returns boolean: ready (true) or not (false)
98  * @set_vq_state:		Set the state for a virtqueue
99  *				@vdev: vdpa device
100  *				@idx: virtqueue index
101  *				@state: pointer to set virtqueue state (last_avail_idx)
102  *				Returns integer: success (0) or error (< 0)
103  * @get_vq_state:		Get the state for a virtqueue
104  *				@vdev: vdpa device
105  *				@idx: virtqueue index
106  *				@state: pointer to returned state (last_avail_idx)
107  * @get_vq_notification: 	Get the notification area for a virtqueue
108  *				@vdev: vdpa device
109  *				@idx: virtqueue index
110  *				Returns the notifcation area
111  * @get_vq_irq:			Get the irq number of a virtqueue (optional,
112  *				but must implemented if require vq irq offloading)
113  *				@vdev: vdpa device
114  *				@idx: virtqueue index
115  *				Returns int: irq number of a virtqueue,
116  *				negative number if no irq assigned.
117  * @get_vq_align:		Get the virtqueue align requirement
118  *				for the device
119  *				@vdev: vdpa device
120  *				Returns virtqueue algin requirement
121  * @get_features:		Get virtio features supported by the device
122  *				@vdev: vdpa device
123  *				Returns the virtio features support by the
124  *				device
125  * @set_features:		Set virtio features supported by the driver
126  *				@vdev: vdpa device
127  *				@features: feature support by the driver
128  *				Returns integer: success (0) or error (< 0)
129  * @set_config_cb:		Set the config interrupt callback
130  *				@vdev: vdpa device
131  *				@cb: virtio-vdev interrupt callback structure
132  * @get_vq_num_max:		Get the max size of virtqueue
133  *				@vdev: vdpa device
134  *				Returns u16: max size of virtqueue
135  * @get_device_id:		Get virtio device id
136  *				@vdev: vdpa device
137  *				Returns u32: virtio device id
138  * @get_vendor_id:		Get id for the vendor that provides this device
139  *				@vdev: vdpa device
140  *				Returns u32: virtio vendor id
141  * @get_status:			Get the device status
142  *				@vdev: vdpa device
143  *				Returns u8: virtio device status
144  * @set_status:			Set the device status
145  *				@vdev: vdpa device
146  *				@status: virtio device status
147  * @get_config:			Read from device specific configuration space
148  *				@vdev: vdpa device
149  *				@offset: offset from the beginning of
150  *				configuration space
151  *				@buf: buffer used to read to
152  *				@len: the length to read from
153  *				configuration space
154  * @set_config:			Write to device specific configuration space
155  *				@vdev: vdpa device
156  *				@offset: offset from the beginning of
157  *				configuration space
158  *				@buf: buffer used to write from
159  *				@len: the length to write to
160  *				configuration space
161  * @get_generation:		Get device config generation (optional)
162  *				@vdev: vdpa device
163  *				Returns u32: device generation
164  * @get_iova_range:		Get supported iova range (optional)
165  *				@vdev: vdpa device
166  *				Returns the iova range supported by
167  *				the device.
168  * @set_map:			Set device memory mapping (optional)
169  *				Needed for device that using device
170  *				specific DMA translation (on-chip IOMMU)
171  *				@vdev: vdpa device
172  *				@iotlb: vhost memory mapping to be
173  *				used by the vDPA
174  *				Returns integer: success (0) or error (< 0)
175  * @dma_map:			Map an area of PA to IOVA (optional)
176  *				Needed for device that using device
177  *				specific DMA translation (on-chip IOMMU)
178  *				and preferring incremental map.
179  *				@vdev: vdpa device
180  *				@iova: iova to be mapped
181  *				@size: size of the area
182  *				@pa: physical address for the map
183  *				@perm: device access permission (VHOST_MAP_XX)
184  *				Returns integer: success (0) or error (< 0)
185  * @dma_unmap:			Unmap an area of IOVA (optional but
186  *				must be implemented with dma_map)
187  *				Needed for device that using device
188  *				specific DMA translation (on-chip IOMMU)
189  *				and preferring incremental unmap.
190  *				@vdev: vdpa device
191  *				@iova: iova to be unmapped
192  *				@size: size of the area
193  *				Returns integer: success (0) or error (< 0)
194  * @free:			Free resources that belongs to vDPA (optional)
195  *				@vdev: vdpa device
196  */
197 struct vdpa_config_ops {
198 	/* Virtqueue ops */
199 	int (*set_vq_address)(struct vdpa_device *vdev,
200 			      u16 idx, u64 desc_area, u64 driver_area,
201 			      u64 device_area);
202 	void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
203 	void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
204 	void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
205 			  struct vdpa_callback *cb);
206 	void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
207 	bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
208 	int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
209 			    const struct vdpa_vq_state *state);
210 	int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
211 			    struct vdpa_vq_state *state);
212 	struct vdpa_notification_area
213 	(*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
214 	/* vq irq is not expected to be changed once DRIVER_OK is set */
215 	int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
216 
217 	/* Device ops */
218 	u32 (*get_vq_align)(struct vdpa_device *vdev);
219 	u64 (*get_features)(struct vdpa_device *vdev);
220 	int (*set_features)(struct vdpa_device *vdev, u64 features);
221 	void (*set_config_cb)(struct vdpa_device *vdev,
222 			      struct vdpa_callback *cb);
223 	u16 (*get_vq_num_max)(struct vdpa_device *vdev);
224 	u32 (*get_device_id)(struct vdpa_device *vdev);
225 	u32 (*get_vendor_id)(struct vdpa_device *vdev);
226 	u8 (*get_status)(struct vdpa_device *vdev);
227 	void (*set_status)(struct vdpa_device *vdev, u8 status);
228 	void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
229 			   void *buf, unsigned int len);
230 	void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
231 			   const void *buf, unsigned int len);
232 	u32 (*get_generation)(struct vdpa_device *vdev);
233 	struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
234 
235 	/* DMA ops */
236 	int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
237 	int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
238 		       u64 pa, u32 perm);
239 	int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
240 
241 	/* Free device resources */
242 	void (*free)(struct vdpa_device *vdev);
243 };
244 
245 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
246 					const struct vdpa_config_ops *config,
247 					int nvqs,
248 					size_t size);
249 
250 #define vdpa_alloc_device(dev_struct, member, parent, config, nvqs)   \
251 			  container_of(__vdpa_alloc_device( \
252 				       parent, config, nvqs, \
253 				       sizeof(dev_struct) + \
254 				       BUILD_BUG_ON_ZERO(offsetof( \
255 				       dev_struct, member))), \
256 				       dev_struct, member)
257 
258 int vdpa_register_device(struct vdpa_device *vdev);
259 void vdpa_unregister_device(struct vdpa_device *vdev);
260 
261 /**
262  * vdpa_driver - operations for a vDPA driver
263  * @driver: underlying device driver
264  * @probe: the function to call when a device is found.  Returns 0 or -errno.
265  * @remove: the function to call when a device is removed.
266  */
267 struct vdpa_driver {
268 	struct device_driver driver;
269 	int (*probe)(struct vdpa_device *vdev);
270 	void (*remove)(struct vdpa_device *vdev);
271 };
272 
273 #define vdpa_register_driver(drv) \
274 	__vdpa_register_driver(drv, THIS_MODULE)
275 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
276 void vdpa_unregister_driver(struct vdpa_driver *drv);
277 
278 #define module_vdpa_driver(__vdpa_driver) \
279 	module_driver(__vdpa_driver, vdpa_register_driver,	\
280 		      vdpa_unregister_driver)
281 
drv_to_vdpa(struct device_driver * driver)282 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
283 {
284 	return container_of(driver, struct vdpa_driver, driver);
285 }
286 
dev_to_vdpa(struct device * _dev)287 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
288 {
289 	return container_of(_dev, struct vdpa_device, dev);
290 }
291 
vdpa_get_drvdata(const struct vdpa_device * vdev)292 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
293 {
294 	return dev_get_drvdata(&vdev->dev);
295 }
296 
vdpa_set_drvdata(struct vdpa_device * vdev,void * data)297 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
298 {
299 	dev_set_drvdata(&vdev->dev, data);
300 }
301 
vdpa_get_dma_dev(struct vdpa_device * vdev)302 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
303 {
304 	return vdev->dma_dev;
305 }
306 
vdpa_reset(struct vdpa_device * vdev)307 static inline void vdpa_reset(struct vdpa_device *vdev)
308 {
309         const struct vdpa_config_ops *ops = vdev->config;
310 
311 	vdev->features_valid = false;
312         ops->set_status(vdev, 0);
313 }
314 
vdpa_set_features(struct vdpa_device * vdev,u64 features)315 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
316 {
317         const struct vdpa_config_ops *ops = vdev->config;
318 
319 	vdev->features_valid = true;
320         return ops->set_features(vdev, features);
321 }
322 
323 
vdpa_get_config(struct vdpa_device * vdev,unsigned offset,void * buf,unsigned int len)324 static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
325 				   void *buf, unsigned int len)
326 {
327         const struct vdpa_config_ops *ops = vdev->config;
328 
329 	/*
330 	 * Config accesses aren't supposed to trigger before features are set.
331 	 * If it does happen we assume a legacy guest.
332 	 */
333 	if (!vdev->features_valid)
334 		vdpa_set_features(vdev, 0);
335 	ops->get_config(vdev, offset, buf, len);
336 }
337 
338 #endif /* _LINUX_VDPA_H */
339