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
45 * @nvqs: maximum number of supported virtqueues
49 struct device *dma_dev;
50 const struct vdpa_config_ops *config;
57 * vDPA IOVA range - the IOVA range support by the device
58 * @first: start of the IOVA range
59 * @last: end of the IOVA range
61 struct vdpa_iova_range {
67 * vDPA_config_ops - operations for configuring a vDPA device.
68 * Note: vDPA device drivers are required to implement all of the
69 * operations unless it is mentioned to be optional in the following
72 * @set_vq_address: Set the address of virtqueue
74 * @idx: virtqueue index
75 * @desc_area: address of desc area
76 * @driver_area: address of driver area
77 * @device_area: address of device area
78 * Returns integer: success (0) or error (< 0)
79 * @set_vq_num: Set the size of virtqueue
81 * @idx: virtqueue index
82 * @num: the size of virtqueue
83 * @kick_vq: Kick the virtqueue
85 * @idx: virtqueue index
86 * @set_vq_cb: Set the interrupt callback function for
89 * @idx: virtqueue index
90 * @cb: virtio-vdev interrupt callback structure
91 * @set_vq_ready: Set ready status for a virtqueue
93 * @idx: virtqueue index
94 * @ready: ready (true) not ready(false)
95 * @get_vq_ready: Get ready status for a virtqueue
97 * @idx: virtqueue index
98 * Returns boolean: ready (true) or not (false)
99 * @set_vq_state: Set the state for a virtqueue
101 * @idx: virtqueue index
102 * @state: pointer to set virtqueue state (last_avail_idx)
103 * Returns integer: success (0) or error (< 0)
104 * @get_vq_state: Get the state for a virtqueue
106 * @idx: virtqueue index
107 * @state: pointer to returned state (last_avail_idx)
108 * @get_vq_notification: Get the notification area for a virtqueue
110 * @idx: virtqueue index
111 * Returns the notifcation area
112 * @get_vq_irq: Get the irq number of a virtqueue (optional,
113 * but must implemented if require vq irq offloading)
115 * @idx: virtqueue index
116 * Returns int: irq number of a virtqueue,
117 * negative number if no irq assigned.
118 * @get_vq_align: Get the virtqueue align requirement
121 * Returns virtqueue algin requirement
122 * @get_features: Get virtio features supported by the device
124 * Returns the virtio features support by the
126 * @set_features: Set virtio features supported by the driver
128 * @features: feature support by the driver
129 * Returns integer: success (0) or error (< 0)
130 * @set_config_cb: Set the config interrupt callback
132 * @cb: virtio-vdev interrupt callback structure
133 * @get_vq_num_max: Get the max size of virtqueue
135 * Returns u16: max size of virtqueue
136 * @get_device_id: Get virtio device id
138 * Returns u32: virtio device id
139 * @get_vendor_id: Get id for the vendor that provides this device
141 * Returns u32: virtio vendor id
142 * @get_status: Get the device status
144 * Returns u8: virtio device status
145 * @set_status: Set the device status
147 * @status: virtio device status
148 * @get_config: Read from device specific configuration space
150 * @offset: offset from the beginning of
151 * configuration space
152 * @buf: buffer used to read to
153 * @len: the length to read from
154 * configuration space
155 * @set_config: Write to device specific configuration space
157 * @offset: offset from the beginning of
158 * configuration space
159 * @buf: buffer used to write from
160 * @len: the length to write to
161 * configuration space
162 * @get_generation: Get device config generation (optional)
164 * Returns u32: device generation
165 * @get_iova_range: Get supported iova range (optional)
167 * Returns the iova range supported by
169 * @set_map: Set device memory mapping (optional)
170 * Needed for device that using device
171 * specific DMA translation (on-chip IOMMU)
173 * @iotlb: vhost memory mapping to be
175 * Returns integer: success (0) or error (< 0)
176 * @dma_map: Map an area of PA to IOVA (optional)
177 * Needed for device that using device
178 * specific DMA translation (on-chip IOMMU)
179 * and preferring incremental map.
181 * @iova: iova to be mapped
182 * @size: size of the area
183 * @pa: physical address for the map
184 * @perm: device access permission (VHOST_MAP_XX)
185 * Returns integer: success (0) or error (< 0)
186 * @dma_unmap: Unmap an area of IOVA (optional but
187 * must be implemented with dma_map)
188 * Needed for device that using device
189 * specific DMA translation (on-chip IOMMU)
190 * and preferring incremental unmap.
192 * @iova: iova to be unmapped
193 * @size: size of the area
194 * Returns integer: success (0) or error (< 0)
195 * @free: Free resources that belongs to vDPA (optional)
198 struct vdpa_config_ops {
200 int (*set_vq_address)(struct vdpa_device *vdev,
201 u16 idx, u64 desc_area, u64 driver_area,
203 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
204 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
205 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
206 struct vdpa_callback *cb);
207 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
208 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
209 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
210 const struct vdpa_vq_state *state);
211 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
212 struct vdpa_vq_state *state);
213 struct vdpa_notification_area
214 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
215 /* vq irq is not expected to be changed once DRIVER_OK is set */
216 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
219 u32 (*get_vq_align)(struct vdpa_device *vdev);
220 u64 (*get_features)(struct vdpa_device *vdev);
221 int (*set_features)(struct vdpa_device *vdev, u64 features);
222 void (*set_config_cb)(struct vdpa_device *vdev,
223 struct vdpa_callback *cb);
224 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
225 u32 (*get_device_id)(struct vdpa_device *vdev);
226 u32 (*get_vendor_id)(struct vdpa_device *vdev);
227 u8 (*get_status)(struct vdpa_device *vdev);
228 void (*set_status)(struct vdpa_device *vdev, u8 status);
229 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
230 void *buf, unsigned int len);
231 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
232 const void *buf, unsigned int len);
233 u32 (*get_generation)(struct vdpa_device *vdev);
234 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
237 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
238 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
240 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
242 /* Free device resources */
243 void (*free)(struct vdpa_device *vdev);
246 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
247 const struct vdpa_config_ops *config,
251 #define vdpa_alloc_device(dev_struct, member, parent, config, nvqs) \
252 container_of(__vdpa_alloc_device( \
253 parent, config, nvqs, \
254 sizeof(dev_struct) + \
255 BUILD_BUG_ON_ZERO(offsetof( \
256 dev_struct, member))), \
259 int vdpa_register_device(struct vdpa_device *vdev);
260 void vdpa_unregister_device(struct vdpa_device *vdev);
263 * vdpa_driver - operations for a vDPA driver
264 * @driver: underlying device driver
265 * @probe: the function to call when a device is found. Returns 0 or -errno.
266 * @remove: the function to call when a device is removed.
269 struct device_driver driver;
270 int (*probe)(struct vdpa_device *vdev);
271 void (*remove)(struct vdpa_device *vdev);
274 #define vdpa_register_driver(drv) \
275 __vdpa_register_driver(drv, THIS_MODULE)
276 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
277 void vdpa_unregister_driver(struct vdpa_driver *drv);
279 #define module_vdpa_driver(__vdpa_driver) \
280 module_driver(__vdpa_driver, vdpa_register_driver, \
281 vdpa_unregister_driver)
283 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
285 return container_of(driver, struct vdpa_driver, driver);
288 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
290 return container_of(_dev, struct vdpa_device, dev);
293 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
295 return dev_get_drvdata(&vdev->dev);
298 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
300 dev_set_drvdata(&vdev->dev, data);
303 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
305 return vdev->dma_dev;
308 static inline void vdpa_reset(struct vdpa_device *vdev)
310 const struct vdpa_config_ops *ops = vdev->config;
312 vdev->features_valid = false;
313 ops->set_status(vdev, 0);
316 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
318 const struct vdpa_config_ops *ops = vdev->config;
320 vdev->features_valid = true;
321 return ops->set_features(vdev, features);
325 static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
326 void *buf, unsigned int len)
328 const struct vdpa_config_ops *ops = vdev->config;
331 * Config accesses aren't supposed to trigger before features are set.
332 * If it does happen we assume a legacy guest.
334 if (!vdev->features_valid)
335 vdpa_set_features(vdev, 0);
336 ops->get_config(vdev, offset, buf, len);
339 #endif /* _LINUX_VDPA_H */