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 {
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
48 struct device *dma_dev;
49 const struct vdpa_config_ops *config;
56 * vDPA_config_ops - operations for configuring a vDPA device.
57 * Note: vDPA device drivers are required to implement all of the
58 * operations unless it is mentioned to be optional in the following
61 * @set_vq_address: Set the address of virtqueue
63 * @idx: virtqueue index
64 * @desc_area: address of desc area
65 * @driver_area: address of driver area
66 * @device_area: address of device area
67 * Returns integer: success (0) or error (< 0)
68 * @set_vq_num: Set the size of virtqueue
70 * @idx: virtqueue index
71 * @num: the size of virtqueue
72 * @kick_vq: Kick the virtqueue
74 * @idx: virtqueue index
75 * @set_vq_cb: Set the interrupt callback function for
78 * @idx: virtqueue index
79 * @cb: virtio-vdev interrupt callback structure
80 * @set_vq_ready: Set ready status for a virtqueue
82 * @idx: virtqueue index
83 * @ready: ready (true) not ready(false)
84 * @get_vq_ready: Get ready status for a virtqueue
86 * @idx: virtqueue index
87 * Returns boolean: ready (true) or not (false)
88 * @set_vq_state: Set the state for a virtqueue
90 * @idx: virtqueue index
91 * @state: pointer to set virtqueue state (last_avail_idx)
92 * Returns integer: success (0) or error (< 0)
93 * @get_vq_state: Get the state for a virtqueue
95 * @idx: virtqueue index
96 * @state: pointer to returned state (last_avail_idx)
97 * @get_vq_notification: Get the notification area for a virtqueue
99 * @idx: virtqueue index
100 * Returns the notifcation area
101 * @get_vq_irq: Get the irq number of a virtqueue (optional,
102 * but must implemented if require vq irq offloading)
104 * @idx: virtqueue index
105 * Returns int: irq number of a virtqueue,
106 * negative number if no irq assigned.
107 * @get_vq_align: Get the virtqueue align requirement
110 * Returns virtqueue algin requirement
111 * @get_features: Get virtio features supported by the device
113 * Returns the virtio features support by the
115 * @set_features: Set virtio features supported by the driver
117 * @features: feature support by the driver
118 * Returns integer: success (0) or error (< 0)
119 * @set_config_cb: Set the config interrupt callback
121 * @cb: virtio-vdev interrupt callback structure
122 * @get_vq_num_max: Get the max size of virtqueue
124 * Returns u16: max size of virtqueue
125 * @get_device_id: Get virtio device id
127 * Returns u32: virtio device id
128 * @get_vendor_id: Get id for the vendor that provides this device
130 * Returns u32: virtio vendor id
131 * @get_status: Get the device status
133 * Returns u8: virtio device status
134 * @set_status: Set the device status
136 * @status: virtio device status
137 * @get_config: Read from device specific configuration space
139 * @offset: offset from the beginning of
140 * configuration space
141 * @buf: buffer used to read to
142 * @len: the length to read from
143 * configuration space
144 * @set_config: Write to device specific configuration space
146 * @offset: offset from the beginning of
147 * configuration space
148 * @buf: buffer used to write from
149 * @len: the length to write to
150 * configuration space
151 * @get_generation: Get device config generation (optional)
153 * Returns u32: device generation
154 * @set_map: Set device memory mapping (optional)
155 * Needed for device that using device
156 * specific DMA translation (on-chip IOMMU)
158 * @iotlb: vhost memory mapping to be
160 * Returns integer: success (0) or error (< 0)
161 * @dma_map: Map an area of PA to IOVA (optional)
162 * Needed for device that using device
163 * specific DMA translation (on-chip IOMMU)
164 * and preferring incremental map.
166 * @iova: iova to be mapped
167 * @size: size of the area
168 * @pa: physical address for the map
169 * @perm: device access permission (VHOST_MAP_XX)
170 * Returns integer: success (0) or error (< 0)
171 * @dma_unmap: Unmap an area of IOVA (optional but
172 * must be implemented with dma_map)
173 * Needed for device that using device
174 * specific DMA translation (on-chip IOMMU)
175 * and preferring incremental unmap.
177 * @iova: iova to be unmapped
178 * @size: size of the area
179 * Returns integer: success (0) or error (< 0)
180 * @free: Free resources that belongs to vDPA (optional)
183 struct vdpa_config_ops {
185 int (*set_vq_address)(struct vdpa_device *vdev,
186 u16 idx, u64 desc_area, u64 driver_area,
188 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
189 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
190 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
191 struct vdpa_callback *cb);
192 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
193 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
194 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
195 const struct vdpa_vq_state *state);
196 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
197 struct vdpa_vq_state *state);
198 struct vdpa_notification_area
199 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
200 /* vq irq is not expected to be changed once DRIVER_OK is set */
201 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
204 u32 (*get_vq_align)(struct vdpa_device *vdev);
205 u64 (*get_features)(struct vdpa_device *vdev);
206 int (*set_features)(struct vdpa_device *vdev, u64 features);
207 void (*set_config_cb)(struct vdpa_device *vdev,
208 struct vdpa_callback *cb);
209 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
210 u32 (*get_device_id)(struct vdpa_device *vdev);
211 u32 (*get_vendor_id)(struct vdpa_device *vdev);
212 u8 (*get_status)(struct vdpa_device *vdev);
213 void (*set_status)(struct vdpa_device *vdev, u8 status);
214 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
215 void *buf, unsigned int len);
216 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
217 const void *buf, unsigned int len);
218 u32 (*get_generation)(struct vdpa_device *vdev);
221 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
222 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
224 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
226 /* Free device resources */
227 void (*free)(struct vdpa_device *vdev);
230 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
231 const struct vdpa_config_ops *config,
235 #define vdpa_alloc_device(dev_struct, member, parent, config, nvqs) \
236 container_of(__vdpa_alloc_device( \
237 parent, config, nvqs, \
238 sizeof(dev_struct) + \
239 BUILD_BUG_ON_ZERO(offsetof( \
240 dev_struct, member))), \
243 int vdpa_register_device(struct vdpa_device *vdev);
244 void vdpa_unregister_device(struct vdpa_device *vdev);
247 * vdpa_driver - operations for a vDPA driver
248 * @driver: underlying device driver
249 * @probe: the function to call when a device is found. Returns 0 or -errno.
250 * @remove: the function to call when a device is removed.
253 struct device_driver driver;
254 int (*probe)(struct vdpa_device *vdev);
255 void (*remove)(struct vdpa_device *vdev);
258 #define vdpa_register_driver(drv) \
259 __vdpa_register_driver(drv, THIS_MODULE)
260 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
261 void vdpa_unregister_driver(struct vdpa_driver *drv);
263 #define module_vdpa_driver(__vdpa_driver) \
264 module_driver(__vdpa_driver, vdpa_register_driver, \
265 vdpa_unregister_driver)
267 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
269 return container_of(driver, struct vdpa_driver, driver);
272 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
274 return container_of(_dev, struct vdpa_device, dev);
277 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
279 return dev_get_drvdata(&vdev->dev);
282 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
284 dev_set_drvdata(&vdev->dev, data);
287 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
289 return vdev->dma_dev;
292 static inline void vdpa_reset(struct vdpa_device *vdev)
294 const struct vdpa_config_ops *ops = vdev->config;
296 vdev->features_valid = false;
297 ops->set_status(vdev, 0);
300 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
302 const struct vdpa_config_ops *ops = vdev->config;
304 vdev->features_valid = true;
305 return ops->set_features(vdev, features);
309 static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
310 void *buf, unsigned int len)
312 const struct vdpa_config_ops *ops = vdev->config;
315 * Config accesses aren't supposed to trigger before features are set.
316 * If it does happen we assume a legacy guest.
318 if (!vdev->features_valid)
319 vdpa_set_features(vdev, 0);
320 ops->get_config(vdev, offset, buf, len);
323 #endif /* _LINUX_VDPA_H */