1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/kernel.h>
6 #include <linux/device.h>
7 #include <linux/interrupt.h>
8 #include <linux/vhost_iotlb.h>
11 * vDPA callback definition.
12 * @callback: interrupt callback function
13 * @private: the data passed to the callback function
15 struct vdpa_callback {
16 irqreturn_t (*callback)(void *data);
21 * vDPA notification area
22 * @addr: base address of the notification area
23 * @size: size of the notification area
25 struct vdpa_notification_area {
31 * vDPA vq_state definition
32 * @avail_index: available index
34 struct vdpa_vq_state {
41 * vDPA device - representation of a vDPA device
42 * @dev: underlying device
43 * @dma_dev: the actual device that is performing DMA
44 * @config: the configuration ops for this device.
45 * @index: device index
46 * @features_valid: were features initialized? for legacy guests
47 * @nvqs: maximum number of supported virtqueues
48 * @mdev: management device pointer; caller must setup when registering device as part
49 * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
53 struct device *dma_dev;
54 const struct vdpa_config_ops *config;
58 struct vdpa_mgmt_dev *mdev;
62 * vDPA IOVA range - the IOVA range support by the device
63 * @first: start of the IOVA range
64 * @last: end of the IOVA range
66 struct vdpa_iova_range {
72 * vDPA_config_ops - operations for configuring a vDPA device.
73 * Note: vDPA device drivers are required to implement all of the
74 * operations unless it is mentioned to be optional in the following
77 * @set_vq_address: Set the address of virtqueue
79 * @idx: virtqueue index
80 * @desc_area: address of desc area
81 * @driver_area: address of driver area
82 * @device_area: address of device area
83 * Returns integer: success (0) or error (< 0)
84 * @set_vq_num: Set the size of virtqueue
86 * @idx: virtqueue index
87 * @num: the size of virtqueue
88 * @kick_vq: Kick the virtqueue
90 * @idx: virtqueue index
91 * @set_vq_cb: Set the interrupt callback function for
94 * @idx: virtqueue index
95 * @cb: virtio-vdev interrupt callback structure
96 * @set_vq_ready: Set ready status for a virtqueue
98 * @idx: virtqueue index
99 * @ready: ready (true) not ready(false)
100 * @get_vq_ready: Get ready status for a virtqueue
102 * @idx: virtqueue index
103 * Returns boolean: ready (true) or not (false)
104 * @set_vq_state: Set the state for a virtqueue
106 * @idx: virtqueue index
107 * @state: pointer to set virtqueue state (last_avail_idx)
108 * Returns integer: success (0) or error (< 0)
109 * @get_vq_state: Get the state for a virtqueue
111 * @idx: virtqueue index
112 * @state: pointer to returned state (last_avail_idx)
113 * @get_vq_notification: Get the notification area for a virtqueue
115 * @idx: virtqueue index
116 * Returns the notifcation area
117 * @get_vq_irq: Get the irq number of a virtqueue (optional,
118 * but must implemented if require vq irq offloading)
120 * @idx: virtqueue index
121 * Returns int: irq number of a virtqueue,
122 * negative number if no irq assigned.
123 * @get_vq_align: Get the virtqueue align requirement
126 * Returns virtqueue algin requirement
127 * @get_features: Get virtio features supported by the device
129 * Returns the virtio features support by the
131 * @set_features: Set virtio features supported by the driver
133 * @features: feature support by the driver
134 * Returns integer: success (0) or error (< 0)
135 * @set_config_cb: Set the config interrupt callback
137 * @cb: virtio-vdev interrupt callback structure
138 * @get_vq_num_max: Get the max size of virtqueue
140 * Returns u16: max size of virtqueue
141 * @get_device_id: Get virtio device id
143 * Returns u32: virtio device id
144 * @get_vendor_id: Get id for the vendor that provides this device
146 * Returns u32: virtio vendor id
147 * @get_status: Get the device status
149 * Returns u8: virtio device status
150 * @set_status: Set the device status
152 * @status: virtio device status
153 * @get_config: Read from device specific configuration space
155 * @offset: offset from the beginning of
156 * configuration space
157 * @buf: buffer used to read to
158 * @len: the length to read from
159 * configuration space
160 * @set_config: Write to device specific configuration space
162 * @offset: offset from the beginning of
163 * configuration space
164 * @buf: buffer used to write from
165 * @len: the length to write to
166 * configuration space
167 * @get_generation: Get device config generation (optional)
169 * Returns u32: device generation
170 * @get_iova_range: Get supported iova range (optional)
172 * Returns the iova range supported by
174 * @set_map: Set device memory mapping (optional)
175 * Needed for device that using device
176 * specific DMA translation (on-chip IOMMU)
178 * @iotlb: vhost memory mapping to be
180 * Returns integer: success (0) or error (< 0)
181 * @dma_map: Map an area of PA to IOVA (optional)
182 * Needed for device that using device
183 * specific DMA translation (on-chip IOMMU)
184 * and preferring incremental map.
186 * @iova: iova to be mapped
187 * @size: size of the area
188 * @pa: physical address for the map
189 * @perm: device access permission (VHOST_MAP_XX)
190 * Returns integer: success (0) or error (< 0)
191 * @dma_unmap: Unmap an area of IOVA (optional but
192 * must be implemented with dma_map)
193 * Needed for device that using device
194 * specific DMA translation (on-chip IOMMU)
195 * and preferring incremental unmap.
197 * @iova: iova to be unmapped
198 * @size: size of the area
199 * Returns integer: success (0) or error (< 0)
200 * @free: Free resources that belongs to vDPA (optional)
203 struct vdpa_config_ops {
205 int (*set_vq_address)(struct vdpa_device *vdev,
206 u16 idx, u64 desc_area, u64 driver_area,
208 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
209 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
210 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
211 struct vdpa_callback *cb);
212 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
213 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
214 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
215 const struct vdpa_vq_state *state);
216 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
217 struct vdpa_vq_state *state);
218 struct vdpa_notification_area
219 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
220 /* vq irq is not expected to be changed once DRIVER_OK is set */
221 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
224 u32 (*get_vq_align)(struct vdpa_device *vdev);
225 u64 (*get_features)(struct vdpa_device *vdev);
226 int (*set_features)(struct vdpa_device *vdev, u64 features);
227 void (*set_config_cb)(struct vdpa_device *vdev,
228 struct vdpa_callback *cb);
229 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
230 u32 (*get_device_id)(struct vdpa_device *vdev);
231 u32 (*get_vendor_id)(struct vdpa_device *vdev);
232 u8 (*get_status)(struct vdpa_device *vdev);
233 void (*set_status)(struct vdpa_device *vdev, u8 status);
234 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
235 void *buf, unsigned int len);
236 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
237 const void *buf, unsigned int len);
238 u32 (*get_generation)(struct vdpa_device *vdev);
239 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
242 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
243 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
245 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
247 /* Free device resources */
248 void (*free)(struct vdpa_device *vdev);
251 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
252 const struct vdpa_config_ops *config,
253 size_t size, const char *name);
255 #define vdpa_alloc_device(dev_struct, member, parent, config, name) \
256 container_of(__vdpa_alloc_device( \
258 sizeof(dev_struct) + \
259 BUILD_BUG_ON_ZERO(offsetof( \
260 dev_struct, member)), name), \
263 int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
264 void vdpa_unregister_device(struct vdpa_device *vdev);
266 int _vdpa_register_device(struct vdpa_device *vdev, int nvqs);
267 void _vdpa_unregister_device(struct vdpa_device *vdev);
270 * vdpa_driver - operations for a vDPA driver
271 * @driver: underlying device driver
272 * @probe: the function to call when a device is found. Returns 0 or -errno.
273 * @remove: the function to call when a device is removed.
276 struct device_driver driver;
277 int (*probe)(struct vdpa_device *vdev);
278 void (*remove)(struct vdpa_device *vdev);
281 #define vdpa_register_driver(drv) \
282 __vdpa_register_driver(drv, THIS_MODULE)
283 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
284 void vdpa_unregister_driver(struct vdpa_driver *drv);
286 #define module_vdpa_driver(__vdpa_driver) \
287 module_driver(__vdpa_driver, vdpa_register_driver, \
288 vdpa_unregister_driver)
290 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
292 return container_of(driver, struct vdpa_driver, driver);
295 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
297 return container_of(_dev, struct vdpa_device, dev);
300 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
302 return dev_get_drvdata(&vdev->dev);
305 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
307 dev_set_drvdata(&vdev->dev, data);
310 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
312 return vdev->dma_dev;
315 static inline void vdpa_reset(struct vdpa_device *vdev)
317 const struct vdpa_config_ops *ops = vdev->config;
319 vdev->features_valid = false;
320 ops->set_status(vdev, 0);
323 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
325 const struct vdpa_config_ops *ops = vdev->config;
327 vdev->features_valid = true;
328 return ops->set_features(vdev, features);
332 static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
333 void *buf, unsigned int len)
335 const struct vdpa_config_ops *ops = vdev->config;
338 * Config accesses aren't supposed to trigger before features are set.
339 * If it does happen we assume a legacy guest.
341 if (!vdev->features_valid)
342 vdpa_set_features(vdev, 0);
343 ops->get_config(vdev, offset, buf, len);
347 * vdpa_mgmtdev_ops - vdpa device ops
348 * @dev_add: Add a vdpa device using alloc and register
349 * @mdev: parent device to use for device addition
350 * @name: name of the new vdpa device
351 * Driver need to add a new device using _vdpa_register_device()
352 * after fully initializing the vdpa device. Driver must return 0
353 * on success or appropriate error code.
354 * @dev_del: Remove a vdpa device using unregister
355 * @mdev: parent device to use for device removal
356 * @dev: vdpa device to remove
357 * Driver need to remove the specified device by calling
358 * _vdpa_unregister_device().
360 struct vdpa_mgmtdev_ops {
361 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name);
362 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
365 struct vdpa_mgmt_dev {
366 struct device *device;
367 const struct vdpa_mgmtdev_ops *ops;
368 const struct virtio_device_id *id_table; /* supported ids */
369 struct list_head list;
372 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
373 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
375 #endif /* _LINUX_VDPA_H */