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 * struct vdpa_calllback - 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 * struct vdpa_notification_area - vDPA notification area
22 * @addr: base address of the notification area
23 * @size: size of the notification area
25 struct vdpa_notification_area {
31 * struct vdpa_vq_state - vDPA vq_state definition
32 * @avail_index: available index
34 struct vdpa_vq_state {
41 * struct 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 * struct 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 * struct 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_size: Get the size of the configuration space
155 * Returns size_t: configuration size
156 * @get_config: Read from device specific configuration space
158 * @offset: offset from the beginning of
159 * configuration space
160 * @buf: buffer used to read to
161 * @len: the length to read from
162 * configuration space
163 * @set_config: Write to device specific configuration space
165 * @offset: offset from the beginning of
166 * configuration space
167 * @buf: buffer used to write from
168 * @len: the length to write to
169 * configuration space
170 * @get_generation: Get device config generation (optional)
172 * Returns u32: device generation
173 * @get_iova_range: Get supported iova range (optional)
175 * Returns the iova range supported by
177 * @set_map: Set device memory mapping (optional)
178 * Needed for device that using device
179 * specific DMA translation (on-chip IOMMU)
181 * @iotlb: vhost memory mapping to be
183 * Returns integer: success (0) or error (< 0)
184 * @dma_map: Map an area of PA to IOVA (optional)
185 * Needed for device that using device
186 * specific DMA translation (on-chip IOMMU)
187 * and preferring incremental map.
189 * @iova: iova to be mapped
190 * @size: size of the area
191 * @pa: physical address for the map
192 * @perm: device access permission (VHOST_MAP_XX)
193 * Returns integer: success (0) or error (< 0)
194 * @dma_unmap: Unmap an area of IOVA (optional but
195 * must be implemented with dma_map)
196 * Needed for device that using device
197 * specific DMA translation (on-chip IOMMU)
198 * and preferring incremental unmap.
200 * @iova: iova to be unmapped
201 * @size: size of the area
202 * Returns integer: success (0) or error (< 0)
203 * @free: Free resources that belongs to vDPA (optional)
206 struct vdpa_config_ops {
208 int (*set_vq_address)(struct vdpa_device *vdev,
209 u16 idx, u64 desc_area, u64 driver_area,
211 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
212 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
213 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
214 struct vdpa_callback *cb);
215 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
216 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
217 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
218 const struct vdpa_vq_state *state);
219 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
220 struct vdpa_vq_state *state);
221 struct vdpa_notification_area
222 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
223 /* vq irq is not expected to be changed once DRIVER_OK is set */
224 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
227 u32 (*get_vq_align)(struct vdpa_device *vdev);
228 u64 (*get_features)(struct vdpa_device *vdev);
229 int (*set_features)(struct vdpa_device *vdev, u64 features);
230 void (*set_config_cb)(struct vdpa_device *vdev,
231 struct vdpa_callback *cb);
232 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
233 u32 (*get_device_id)(struct vdpa_device *vdev);
234 u32 (*get_vendor_id)(struct vdpa_device *vdev);
235 u8 (*get_status)(struct vdpa_device *vdev);
236 void (*set_status)(struct vdpa_device *vdev, u8 status);
237 size_t (*get_config_size)(struct vdpa_device *vdev);
238 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
239 void *buf, unsigned int len);
240 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
241 const void *buf, unsigned int len);
242 u32 (*get_generation)(struct vdpa_device *vdev);
243 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
246 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
247 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
249 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
251 /* Free device resources */
252 void (*free)(struct vdpa_device *vdev);
255 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
256 const struct vdpa_config_ops *config,
257 size_t size, const char *name);
259 #define vdpa_alloc_device(dev_struct, member, parent, config, name) \
260 container_of(__vdpa_alloc_device( \
262 sizeof(dev_struct) + \
263 BUILD_BUG_ON_ZERO(offsetof( \
264 dev_struct, member)), name), \
267 int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
268 void vdpa_unregister_device(struct vdpa_device *vdev);
270 int _vdpa_register_device(struct vdpa_device *vdev, int nvqs);
271 void _vdpa_unregister_device(struct vdpa_device *vdev);
274 * struct vdpa_driver - operations for a vDPA driver
275 * @driver: underlying device driver
276 * @probe: the function to call when a device is found. Returns 0 or -errno.
277 * @remove: the function to call when a device is removed.
280 struct device_driver driver;
281 int (*probe)(struct vdpa_device *vdev);
282 void (*remove)(struct vdpa_device *vdev);
285 #define vdpa_register_driver(drv) \
286 __vdpa_register_driver(drv, THIS_MODULE)
287 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
288 void vdpa_unregister_driver(struct vdpa_driver *drv);
290 #define module_vdpa_driver(__vdpa_driver) \
291 module_driver(__vdpa_driver, vdpa_register_driver, \
292 vdpa_unregister_driver)
294 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
296 return container_of(driver, struct vdpa_driver, driver);
299 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
301 return container_of(_dev, struct vdpa_device, dev);
304 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
306 return dev_get_drvdata(&vdev->dev);
309 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
311 dev_set_drvdata(&vdev->dev, data);
314 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
316 return vdev->dma_dev;
319 static inline void vdpa_reset(struct vdpa_device *vdev)
321 const struct vdpa_config_ops *ops = vdev->config;
323 vdev->features_valid = false;
324 ops->set_status(vdev, 0);
327 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
329 const struct vdpa_config_ops *ops = vdev->config;
331 vdev->features_valid = true;
332 return ops->set_features(vdev, features);
336 static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
337 void *buf, unsigned int len)
339 const struct vdpa_config_ops *ops = vdev->config;
342 * Config accesses aren't supposed to trigger before features are set.
343 * If it does happen we assume a legacy guest.
345 if (!vdev->features_valid)
346 vdpa_set_features(vdev, 0);
347 ops->get_config(vdev, offset, buf, len);
351 * struct vdpa_mgmtdev_ops - vdpa device ops
352 * @dev_add: Add a vdpa device using alloc and register
353 * @mdev: parent device to use for device addition
354 * @name: name of the new vdpa device
355 * Driver need to add a new device using _vdpa_register_device()
356 * after fully initializing the vdpa device. Driver must return 0
357 * on success or appropriate error code.
358 * @dev_del: Remove a vdpa device using unregister
359 * @mdev: parent device to use for device removal
360 * @dev: vdpa device to remove
361 * Driver need to remove the specified device by calling
362 * _vdpa_unregister_device().
364 struct vdpa_mgmtdev_ops {
365 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name);
366 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
369 struct vdpa_mgmt_dev {
370 struct device *device;
371 const struct vdpa_mgmtdev_ops *ops;
372 const struct virtio_device_id *id_table; /* supported ids */
373 struct list_head list;
376 int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
377 void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
379 #endif /* _LINUX_VDPA_H */