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 device - representation of a vDPA device
32 * @dev: underlying device
33 * @dma_dev: the actual device that is performing DMA
34 * @config: the configuration ops for this device.
35 * @index: device index
36 * @features_valid: were features initialized? for legacy guests
40 struct device *dma_dev;
41 const struct vdpa_config_ops *config;
47 * vDPA_config_ops - operations for configuring a vDPA device.
48 * Note: vDPA device drivers are required to implement all of the
49 * operations unless it is mentioned to be optional in the following
52 * @set_vq_address: Set the address of virtqueue
54 * @idx: virtqueue index
55 * @desc_area: address of desc area
56 * @driver_area: address of driver area
57 * @device_area: address of device area
58 * Returns integer: success (0) or error (< 0)
59 * @set_vq_num: Set the size of virtqueue
61 * @idx: virtqueue index
62 * @num: the size of virtqueue
63 * @kick_vq: Kick the virtqueue
65 * @idx: virtqueue index
66 * @set_vq_cb: Set the interrupt callback function for
69 * @idx: virtqueue index
70 * @cb: virtio-vdev interrupt callback structure
71 * @set_vq_ready: Set ready status for a virtqueue
73 * @idx: virtqueue index
74 * @ready: ready (true) not ready(false)
75 * @get_vq_ready: Get ready status for a virtqueue
77 * @idx: virtqueue index
78 * Returns boolean: ready (true) or not (false)
79 * @set_vq_state: Set the state for a virtqueue
81 * @idx: virtqueue index
82 * @state: virtqueue state (last_avail_idx)
83 * Returns integer: success (0) or error (< 0)
84 * @get_vq_state: Get the state for a virtqueue
86 * @idx: virtqueue index
87 * Returns virtqueue state (last_avail_idx)
88 * @get_vq_notification: Get the notification area for a virtqueue
90 * @idx: virtqueue index
91 * Returns the notifcation area
92 * @get_vq_irq: Get the irq number of a virtqueue (optional,
93 * but must implemented if require vq irq offloading)
95 * @idx: virtqueue index
96 * Returns int: irq number of a virtqueue,
97 * negative number if no irq assigned.
98 * @get_vq_align: Get the virtqueue align requirement
101 * Returns virtqueue algin requirement
102 * @get_features: Get virtio features supported by the device
104 * Returns the virtio features support by the
106 * @set_features: Set virtio features supported by the driver
108 * @features: feature support by the driver
109 * Returns integer: success (0) or error (< 0)
110 * @set_config_cb: Set the config interrupt callback
112 * @cb: virtio-vdev interrupt callback structure
113 * @get_vq_num_max: Get the max size of virtqueue
115 * Returns u16: max size of virtqueue
116 * @get_device_id: Get virtio device id
118 * Returns u32: virtio device id
119 * @get_vendor_id: Get id for the vendor that provides this device
121 * Returns u32: virtio vendor id
122 * @get_status: Get the device status
124 * Returns u8: virtio device status
125 * @set_status: Set the device status
127 * @status: virtio device status
128 * @get_config: Read from device specific configuration space
130 * @offset: offset from the beginning of
131 * configuration space
132 * @buf: buffer used to read to
133 * @len: the length to read from
134 * configuration space
135 * @set_config: Write to device specific configuration space
137 * @offset: offset from the beginning of
138 * configuration space
139 * @buf: buffer used to write from
140 * @len: the length to write to
141 * configuration space
142 * @get_generation: Get device config generation (optional)
144 * Returns u32: device generation
145 * @set_map: Set device memory mapping (optional)
146 * Needed for device that using device
147 * specific DMA translation (on-chip IOMMU)
149 * @iotlb: vhost memory mapping to be
151 * Returns integer: success (0) or error (< 0)
152 * @dma_map: Map an area of PA to IOVA (optional)
153 * Needed for device that using device
154 * specific DMA translation (on-chip IOMMU)
155 * and preferring incremental map.
157 * @iova: iova to be mapped
158 * @size: size of the area
159 * @pa: physical address for the map
160 * @perm: device access permission (VHOST_MAP_XX)
161 * Returns integer: success (0) or error (< 0)
162 * @dma_unmap: Unmap an area of IOVA (optional but
163 * must be implemented with dma_map)
164 * Needed for device that using device
165 * specific DMA translation (on-chip IOMMU)
166 * and preferring incremental unmap.
168 * @iova: iova to be unmapped
169 * @size: size of the area
170 * Returns integer: success (0) or error (< 0)
171 * @free: Free resources that belongs to vDPA (optional)
174 struct vdpa_config_ops {
176 int (*set_vq_address)(struct vdpa_device *vdev,
177 u16 idx, u64 desc_area, u64 driver_area,
179 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
180 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
181 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
182 struct vdpa_callback *cb);
183 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
184 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
185 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, u64 state);
186 u64 (*get_vq_state)(struct vdpa_device *vdev, u16 idx);
187 struct vdpa_notification_area
188 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
189 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
192 u32 (*get_vq_align)(struct vdpa_device *vdev);
193 u64 (*get_features)(struct vdpa_device *vdev);
194 int (*set_features)(struct vdpa_device *vdev, u64 features);
195 void (*set_config_cb)(struct vdpa_device *vdev,
196 struct vdpa_callback *cb);
197 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
198 u32 (*get_device_id)(struct vdpa_device *vdev);
199 u32 (*get_vendor_id)(struct vdpa_device *vdev);
200 u8 (*get_status)(struct vdpa_device *vdev);
201 void (*set_status)(struct vdpa_device *vdev, u8 status);
202 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
203 void *buf, unsigned int len);
204 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
205 const void *buf, unsigned int len);
206 u32 (*get_generation)(struct vdpa_device *vdev);
209 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
210 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
212 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
214 /* Free device resources */
215 void (*free)(struct vdpa_device *vdev);
218 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
219 const struct vdpa_config_ops *config,
222 #define vdpa_alloc_device(dev_struct, member, parent, config) \
223 container_of(__vdpa_alloc_device( \
225 sizeof(dev_struct) + \
226 BUILD_BUG_ON_ZERO(offsetof( \
227 dev_struct, member))), \
230 int vdpa_register_device(struct vdpa_device *vdev);
231 void vdpa_unregister_device(struct vdpa_device *vdev);
234 * vdpa_driver - operations for a vDPA driver
235 * @driver: underlying device driver
236 * @probe: the function to call when a device is found. Returns 0 or -errno.
237 * @remove: the function to call when a device is removed.
240 struct device_driver driver;
241 int (*probe)(struct vdpa_device *vdev);
242 void (*remove)(struct vdpa_device *vdev);
245 #define vdpa_register_driver(drv) \
246 __vdpa_register_driver(drv, THIS_MODULE)
247 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
248 void vdpa_unregister_driver(struct vdpa_driver *drv);
250 #define module_vdpa_driver(__vdpa_driver) \
251 module_driver(__vdpa_driver, vdpa_register_driver, \
252 vdpa_unregister_driver)
254 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
256 return container_of(driver, struct vdpa_driver, driver);
259 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
261 return container_of(_dev, struct vdpa_device, dev);
264 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
266 return dev_get_drvdata(&vdev->dev);
269 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
271 dev_set_drvdata(&vdev->dev, data);
274 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
276 return vdev->dma_dev;
279 static inline void vdpa_reset(struct vdpa_device *vdev)
281 const struct vdpa_config_ops *ops = vdev->config;
283 vdev->features_valid = false;
284 ops->set_status(vdev, 0);
287 static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
289 const struct vdpa_config_ops *ops = vdev->config;
291 vdev->features_valid = true;
292 return ops->set_features(vdev, features);
296 static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset,
297 void *buf, unsigned int len)
299 const struct vdpa_config_ops *ops = vdev->config;
302 * Config accesses aren't supposed to trigger before features are set.
303 * If it does happen we assume a legacy guest.
305 if (!vdev->features_valid)
306 vdpa_set_features(vdev, 0);
307 ops->get_config(vdev, offset, buf, len);
310 #endif /* _LINUX_VDPA_H */