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 IOVA range - the IOVA range support by the device
57 * @first: start of the IOVA range
58 * @last: end of the IOVA range
60 struct vdpa_iova_range {
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
71 * @set_vq_address: Set the address of virtqueue
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
80 * @idx: virtqueue index
81 * @num: the size of virtqueue
82 * @kick_vq: Kick the virtqueue
84 * @idx: virtqueue index
85 * @set_vq_cb: Set the interrupt callback function for
88 * @idx: virtqueue index
89 * @cb: virtio-vdev interrupt callback structure
90 * @set_vq_ready: Set ready status for a virtqueue
92 * @idx: virtqueue index
93 * @ready: ready (true) not ready(false)
94 * @get_vq_ready: Get ready status for a virtqueue
96 * @idx: virtqueue index
97 * Returns boolean: ready (true) or not (false)
98 * @set_vq_state: Set the state for a virtqueue
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
105 * @idx: virtqueue index
106 * @state: pointer to returned state (last_avail_idx)
107 * @get_vq_notification: Get the notification area for a virtqueue
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)
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
120 * Returns virtqueue algin requirement
121 * @get_features: Get virtio features supported by the device
123 * Returns the virtio features support by the
125 * @set_features: Set virtio features supported by the driver
127 * @features: feature support by the driver
128 * Returns integer: success (0) or error (< 0)
129 * @set_config_cb: Set the config interrupt callback
131 * @cb: virtio-vdev interrupt callback structure
132 * @get_vq_num_max: Get the max size of virtqueue
134 * Returns u16: max size of virtqueue
135 * @get_device_id: Get virtio device id
137 * Returns u32: virtio device id
138 * @get_vendor_id: Get id for the vendor that provides this device
140 * Returns u32: virtio vendor id
141 * @get_status: Get the device status
143 * Returns u8: virtio device status
144 * @set_status: Set the device status
146 * @status: virtio device status
147 * @get_config: Read from device specific configuration space
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
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)
163 * Returns u32: device generation
164 * @get_iova_range: Get supported iova range (optional)
166 * Returns the iova range supported by
168 * @set_map: Set device memory mapping (optional)
169 * Needed for device that using device
170 * specific DMA translation (on-chip IOMMU)
172 * @iotlb: vhost memory mapping to be
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.
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.
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)
197 struct vdpa_config_ops {
199 int (*set_vq_address)(struct vdpa_device *vdev,
200 u16 idx, u64 desc_area, u64 driver_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);
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);
236 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
237 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
239 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
241 /* Free device resources */
242 void (*free)(struct vdpa_device *vdev);
245 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
246 const struct vdpa_config_ops *config,
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))), \
258 int vdpa_register_device(struct vdpa_device *vdev);
259 void vdpa_unregister_device(struct vdpa_device *vdev);
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.
268 struct device_driver driver;
269 int (*probe)(struct vdpa_device *vdev);
270 void (*remove)(struct vdpa_device *vdev);
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);
278 #define module_vdpa_driver(__vdpa_driver) \
279 module_driver(__vdpa_driver, vdpa_register_driver, \
280 vdpa_unregister_driver)
282 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
284 return container_of(driver, struct vdpa_driver, driver);
287 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
289 return container_of(_dev, struct vdpa_device, dev);
292 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
294 return dev_get_drvdata(&vdev->dev);
297 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
299 dev_set_drvdata(&vdev->dev, data);
302 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
304 return vdev->dma_dev;
307 static inline void vdpa_reset(struct vdpa_device *vdev)
309 const struct vdpa_config_ops *ops = vdev->config;
311 vdev->features_valid = false;
312 ops->set_status(vdev, 0);
315 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
317 const struct vdpa_config_ops *ops = vdev->config;
319 vdev->features_valid = true;
320 return ops->set_features(vdev, features);
324 static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
325 void *buf, unsigned int len)
327 const struct vdpa_config_ops *ops = vdev->config;
330 * Config accesses aren't supposed to trigger before features are set.
331 * If it does happen we assume a legacy guest.
333 if (!vdev->features_valid)
334 vdpa_set_features(vdev, 0);
335 ops->get_config(vdev, offset, buf, len);
338 #endif /* _LINUX_VDPA_H */