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
39 struct device *dma_dev;
40 const struct vdpa_config_ops *config;
45 * vDPA_config_ops - operations for configuring a vDPA device.
46 * Note: vDPA device drivers are required to implement all of the
47 * operations unless it is mentioned to be optional in the following
50 * @set_vq_address: Set the address of virtqueue
52 * @idx: virtqueue index
53 * @desc_area: address of desc area
54 * @driver_area: address of driver area
55 * @device_area: address of device area
56 * Returns integer: success (0) or error (< 0)
57 * @set_vq_num: Set the size of virtqueue
59 * @idx: virtqueue index
60 * @num: the size of virtqueue
61 * @kick_vq: Kick the virtqueue
63 * @idx: virtqueue index
64 * @set_vq_cb: Set the interrupt callback function for
67 * @idx: virtqueue index
68 * @cb: virtio-vdev interrupt callback structure
69 * @set_vq_ready: Set ready status for a virtqueue
71 * @idx: virtqueue index
72 * @ready: ready (true) not ready(false)
73 * @get_vq_ready: Get ready status for a virtqueue
75 * @idx: virtqueue index
76 * Returns boolean: ready (true) or not (false)
77 * @set_vq_state: Set the state for a virtqueue
79 * @idx: virtqueue index
80 * @state: virtqueue state (last_avail_idx)
81 * Returns integer: success (0) or error (< 0)
82 * @get_vq_state: Get the state for a virtqueue
84 * @idx: virtqueue index
85 * Returns virtqueue state (last_avail_idx)
86 * @get_vq_notification: Get the notification area for a virtqueue
88 * @idx: virtqueue index
89 * Returns the notifcation area
90 * @get_vq_align: Get the virtqueue align requirement
93 * Returns virtqueue algin requirement
94 * @get_features: Get virtio features supported by the device
96 * Returns the virtio features support by the
98 * @set_features: Set virtio features supported by the driver
100 * @features: feature support by the driver
101 * Returns integer: success (0) or error (< 0)
102 * @set_config_cb: Set the config interrupt callback
104 * @cb: virtio-vdev interrupt callback structure
105 * @get_vq_num_max: Get the max size of virtqueue
107 * Returns u16: max size of virtqueue
108 * @get_device_id: Get virtio device id
110 * Returns u32: virtio device id
111 * @get_vendor_id: Get id for the vendor that provides this device
113 * Returns u32: virtio vendor id
114 * @get_status: Get the device status
116 * Returns u8: virtio device status
117 * @set_status: Set the device status
119 * @status: virtio device status
120 * @get_config: Read from device specific configuration space
122 * @offset: offset from the beginning of
123 * configuration space
124 * @buf: buffer used to read to
125 * @len: the length to read from
126 * configuration space
127 * @set_config: Write to device specific configuration space
129 * @offset: offset from the beginning of
130 * configuration space
131 * @buf: buffer used to write from
132 * @len: the length to write to
133 * configuration space
134 * @get_generation: Get device config generation (optional)
136 * Returns u32: device generation
137 * @set_map: Set device memory mapping (optional)
138 * Needed for device that using device
139 * specific DMA translation (on-chip IOMMU)
141 * @iotlb: vhost memory mapping to be
143 * Returns integer: success (0) or error (< 0)
144 * @dma_map: Map an area of PA to IOVA (optional)
145 * Needed for device that using device
146 * specific DMA translation (on-chip IOMMU)
147 * and preferring incremental map.
149 * @iova: iova to be mapped
150 * @size: size of the area
151 * @pa: physical address for the map
152 * @perm: device access permission (VHOST_MAP_XX)
153 * Returns integer: success (0) or error (< 0)
154 * @dma_unmap: Unmap an area of IOVA (optional but
155 * must be implemented with dma_map)
156 * Needed for device that using device
157 * specific DMA translation (on-chip IOMMU)
158 * and preferring incremental unmap.
160 * @iova: iova to be unmapped
161 * @size: size of the area
162 * Returns integer: success (0) or error (< 0)
163 * @free: Free resources that belongs to vDPA (optional)
166 struct vdpa_config_ops {
168 int (*set_vq_address)(struct vdpa_device *vdev,
169 u16 idx, u64 desc_area, u64 driver_area,
171 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
172 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
173 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
174 struct vdpa_callback *cb);
175 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
176 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
177 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, u64 state);
178 u64 (*get_vq_state)(struct vdpa_device *vdev, u16 idx);
179 struct vdpa_notification_area
180 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
183 u32 (*get_vq_align)(struct vdpa_device *vdev);
184 u64 (*get_features)(struct vdpa_device *vdev);
185 int (*set_features)(struct vdpa_device *vdev, u64 features);
186 void (*set_config_cb)(struct vdpa_device *vdev,
187 struct vdpa_callback *cb);
188 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
189 u32 (*get_device_id)(struct vdpa_device *vdev);
190 u32 (*get_vendor_id)(struct vdpa_device *vdev);
191 u8 (*get_status)(struct vdpa_device *vdev);
192 void (*set_status)(struct vdpa_device *vdev, u8 status);
193 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
194 void *buf, unsigned int len);
195 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
196 const void *buf, unsigned int len);
197 u32 (*get_generation)(struct vdpa_device *vdev);
200 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
201 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
203 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
205 /* Free device resources */
206 void (*free)(struct vdpa_device *vdev);
209 struct vdpa_device *__vdpa_alloc_device(struct device *parent,
210 const struct vdpa_config_ops *config,
213 #define vdpa_alloc_device(dev_struct, member, parent, config) \
214 container_of(__vdpa_alloc_device( \
216 sizeof(dev_struct) + \
217 BUILD_BUG_ON_ZERO(offsetof( \
218 dev_struct, member))), \
221 int vdpa_register_device(struct vdpa_device *vdev);
222 void vdpa_unregister_device(struct vdpa_device *vdev);
225 * vdpa_driver - operations for a vDPA driver
226 * @driver: underlying device driver
227 * @probe: the function to call when a device is found. Returns 0 or -errno.
228 * @remove: the function to call when a device is removed.
231 struct device_driver driver;
232 int (*probe)(struct vdpa_device *vdev);
233 void (*remove)(struct vdpa_device *vdev);
236 #define vdpa_register_driver(drv) \
237 __vdpa_register_driver(drv, THIS_MODULE)
238 int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
239 void vdpa_unregister_driver(struct vdpa_driver *drv);
241 #define module_vdpa_driver(__vdpa_driver) \
242 module_driver(__vdpa_driver, vdpa_register_driver, \
243 vdpa_unregister_driver)
245 static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
247 return container_of(driver, struct vdpa_driver, driver);
250 static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
252 return container_of(_dev, struct vdpa_device, dev);
255 static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
257 return dev_get_drvdata(&vdev->dev);
260 static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
262 dev_set_drvdata(&vdev->dev, data);
265 static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
267 return vdev->dma_dev;
269 #endif /* _LINUX_VDPA_H */