Commit | Line | Data |
---|---|---|
961e9c84 JW |
1 | /* SPDX-License-Identifier: GPL-2.0 */ |
2 | #ifndef _LINUX_VDPA_H | |
3 | #define _LINUX_VDPA_H | |
4 | ||
5 | #include <linux/kernel.h> | |
6 | #include <linux/device.h> | |
7 | #include <linux/interrupt.h> | |
8 | #include <linux/vhost_iotlb.h> | |
9 | ||
10 | /** | |
11 | * vDPA callback definition. | |
12 | * @callback: interrupt callback function | |
13 | * @private: the data passed to the callback function | |
14 | */ | |
15 | struct vdpa_callback { | |
16 | irqreturn_t (*callback)(void *data); | |
17 | void *private; | |
18 | }; | |
19 | ||
c25a26e6 JW |
20 | /** |
21 | * vDPA notification area | |
22 | * @addr: base address of the notification area | |
23 | * @size: size of the notification area | |
24 | */ | |
25 | struct vdpa_notification_area { | |
26 | resource_size_t addr; | |
27 | resource_size_t size; | |
28 | }; | |
29 | ||
961e9c84 JW |
30 | /** |
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 | |
452639a6 | 36 | * @features_valid: were features initialized? for legacy guests |
961e9c84 JW |
37 | */ |
38 | struct vdpa_device { | |
39 | struct device dev; | |
40 | struct device *dma_dev; | |
41 | const struct vdpa_config_ops *config; | |
42 | unsigned int index; | |
452639a6 | 43 | bool features_valid; |
961e9c84 JW |
44 | }; |
45 | ||
46 | /** | |
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 | |
50 | * list. | |
51 | * | |
52 | * @set_vq_address: Set the address of virtqueue | |
53 | * @vdev: vdpa device | |
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 | |
60 | * @vdev: vdpa device | |
61 | * @idx: virtqueue index | |
62 | * @num: the size of virtqueue | |
63 | * @kick_vq: Kick the virtqueue | |
64 | * @vdev: vdpa device | |
65 | * @idx: virtqueue index | |
66 | * @set_vq_cb: Set the interrupt callback function for | |
67 | * a virtqueue | |
68 | * @vdev: vdpa device | |
69 | * @idx: virtqueue index | |
70 | * @cb: virtio-vdev interrupt callback structure | |
71 | * @set_vq_ready: Set ready status for a virtqueue | |
72 | * @vdev: vdpa device | |
73 | * @idx: virtqueue index | |
74 | * @ready: ready (true) not ready(false) | |
75 | * @get_vq_ready: Get ready status for a virtqueue | |
76 | * @vdev: vdpa device | |
77 | * @idx: virtqueue index | |
78 | * Returns boolean: ready (true) or not (false) | |
79 | * @set_vq_state: Set the state for a virtqueue | |
80 | * @vdev: vdpa device | |
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 | |
85 | * @vdev: vdpa device | |
86 | * @idx: virtqueue index | |
87 | * Returns virtqueue state (last_avail_idx) | |
c25a26e6 JW |
88 | * @get_vq_notification: Get the notification area for a virtqueue |
89 | * @vdev: vdpa device | |
90 | * @idx: virtqueue index | |
91 | * Returns the notifcation area | |
7164675a ZL |
92 | * @get_vq_irq: Get the irq number of a virtqueue (optional, |
93 | * but must implemented if require vq irq offloading) | |
94 | * @vdev: vdpa device | |
95 | * @idx: virtqueue index | |
96 | * Returns int: irq number of a virtqueue, | |
97 | * negative number if no irq assigned. | |
961e9c84 JW |
98 | * @get_vq_align: Get the virtqueue align requirement |
99 | * for the device | |
100 | * @vdev: vdpa device | |
101 | * Returns virtqueue algin requirement | |
102 | * @get_features: Get virtio features supported by the device | |
103 | * @vdev: vdpa device | |
104 | * Returns the virtio features support by the | |
105 | * device | |
106 | * @set_features: Set virtio features supported by the driver | |
107 | * @vdev: vdpa device | |
108 | * @features: feature support by the driver | |
109 | * Returns integer: success (0) or error (< 0) | |
110 | * @set_config_cb: Set the config interrupt callback | |
111 | * @vdev: vdpa device | |
112 | * @cb: virtio-vdev interrupt callback structure | |
113 | * @get_vq_num_max: Get the max size of virtqueue | |
114 | * @vdev: vdpa device | |
115 | * Returns u16: max size of virtqueue | |
116 | * @get_device_id: Get virtio device id | |
117 | * @vdev: vdpa device | |
118 | * Returns u32: virtio device id | |
119 | * @get_vendor_id: Get id for the vendor that provides this device | |
120 | * @vdev: vdpa device | |
121 | * Returns u32: virtio vendor id | |
122 | * @get_status: Get the device status | |
123 | * @vdev: vdpa device | |
124 | * Returns u8: virtio device status | |
125 | * @set_status: Set the device status | |
126 | * @vdev: vdpa device | |
127 | * @status: virtio device status | |
128 | * @get_config: Read from device specific configuration space | |
129 | * @vdev: vdpa device | |
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 | |
136 | * @vdev: vdpa device | |
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) | |
143 | * @vdev: vdpa device | |
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) | |
148 | * @vdev: vdpa device | |
149 | * @iotlb: vhost memory mapping to be | |
150 | * used by the vDPA | |
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. | |
156 | * @vdev: vdpa device | |
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. | |
167 | * @vdev: vdpa device | |
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) | |
172 | * @vdev: vdpa device | |
173 | */ | |
174 | struct vdpa_config_ops { | |
175 | /* Virtqueue ops */ | |
176 | int (*set_vq_address)(struct vdpa_device *vdev, | |
177 | u16 idx, u64 desc_area, u64 driver_area, | |
178 | u64 device_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); | |
c25a26e6 JW |
187 | struct vdpa_notification_area |
188 | (*get_vq_notification)(struct vdpa_device *vdev, u16 idx); | |
4c05433b | 189 | /* vq irq is not expected to be changed once DRIVER_OK is set */ |
7164675a | 190 | int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx); |
961e9c84 JW |
191 | |
192 | /* Device ops */ | |
425a5070 | 193 | u32 (*get_vq_align)(struct vdpa_device *vdev); |
961e9c84 JW |
194 | u64 (*get_features)(struct vdpa_device *vdev); |
195 | int (*set_features)(struct vdpa_device *vdev, u64 features); | |
196 | void (*set_config_cb)(struct vdpa_device *vdev, | |
197 | struct vdpa_callback *cb); | |
198 | u16 (*get_vq_num_max)(struct vdpa_device *vdev); | |
199 | u32 (*get_device_id)(struct vdpa_device *vdev); | |
200 | u32 (*get_vendor_id)(struct vdpa_device *vdev); | |
201 | u8 (*get_status)(struct vdpa_device *vdev); | |
202 | void (*set_status)(struct vdpa_device *vdev, u8 status); | |
203 | void (*get_config)(struct vdpa_device *vdev, unsigned int offset, | |
204 | void *buf, unsigned int len); | |
205 | void (*set_config)(struct vdpa_device *vdev, unsigned int offset, | |
206 | const void *buf, unsigned int len); | |
207 | u32 (*get_generation)(struct vdpa_device *vdev); | |
208 | ||
209 | /* DMA ops */ | |
210 | int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb); | |
211 | int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size, | |
212 | u64 pa, u32 perm); | |
213 | int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size); | |
214 | ||
215 | /* Free device resources */ | |
216 | void (*free)(struct vdpa_device *vdev); | |
217 | }; | |
218 | ||
219 | struct vdpa_device *__vdpa_alloc_device(struct device *parent, | |
220 | const struct vdpa_config_ops *config, | |
221 | size_t size); | |
222 | ||
223 | #define vdpa_alloc_device(dev_struct, member, parent, config) \ | |
224 | container_of(__vdpa_alloc_device( \ | |
225 | parent, config, \ | |
226 | sizeof(dev_struct) + \ | |
227 | BUILD_BUG_ON_ZERO(offsetof( \ | |
228 | dev_struct, member))), \ | |
229 | dev_struct, member) | |
230 | ||
231 | int vdpa_register_device(struct vdpa_device *vdev); | |
232 | void vdpa_unregister_device(struct vdpa_device *vdev); | |
233 | ||
234 | /** | |
235 | * vdpa_driver - operations for a vDPA driver | |
236 | * @driver: underlying device driver | |
237 | * @probe: the function to call when a device is found. Returns 0 or -errno. | |
238 | * @remove: the function to call when a device is removed. | |
239 | */ | |
240 | struct vdpa_driver { | |
241 | struct device_driver driver; | |
242 | int (*probe)(struct vdpa_device *vdev); | |
243 | void (*remove)(struct vdpa_device *vdev); | |
244 | }; | |
245 | ||
246 | #define vdpa_register_driver(drv) \ | |
247 | __vdpa_register_driver(drv, THIS_MODULE) | |
248 | int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner); | |
249 | void vdpa_unregister_driver(struct vdpa_driver *drv); | |
250 | ||
251 | #define module_vdpa_driver(__vdpa_driver) \ | |
252 | module_driver(__vdpa_driver, vdpa_register_driver, \ | |
253 | vdpa_unregister_driver) | |
254 | ||
255 | static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver) | |
256 | { | |
257 | return container_of(driver, struct vdpa_driver, driver); | |
258 | } | |
259 | ||
260 | static inline struct vdpa_device *dev_to_vdpa(struct device *_dev) | |
261 | { | |
262 | return container_of(_dev, struct vdpa_device, dev); | |
263 | } | |
264 | ||
265 | static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev) | |
266 | { | |
267 | return dev_get_drvdata(&vdev->dev); | |
268 | } | |
269 | ||
270 | static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data) | |
271 | { | |
272 | dev_set_drvdata(&vdev->dev, data); | |
273 | } | |
274 | ||
275 | static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev) | |
276 | { | |
277 | return vdev->dma_dev; | |
278 | } | |
452639a6 MT |
279 | |
280 | static inline void vdpa_reset(struct vdpa_device *vdev) | |
281 | { | |
282 | const struct vdpa_config_ops *ops = vdev->config; | |
283 | ||
284 | vdev->features_valid = false; | |
285 | ops->set_status(vdev, 0); | |
286 | } | |
287 | ||
288 | static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) | |
289 | { | |
290 | const struct vdpa_config_ops *ops = vdev->config; | |
291 | ||
292 | vdev->features_valid = true; | |
293 | return ops->set_features(vdev, features); | |
294 | } | |
295 | ||
296 | ||
297 | static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, | |
298 | void *buf, unsigned int len) | |
299 | { | |
300 | const struct vdpa_config_ops *ops = vdev->config; | |
301 | ||
302 | /* | |
303 | * Config accesses aren't supposed to trigger before features are set. | |
304 | * If it does happen we assume a legacy guest. | |
305 | */ | |
306 | if (!vdev->features_valid) | |
307 | vdpa_set_features(vdev, 0); | |
308 | ops->get_config(vdev, offset, buf, len); | |
309 | } | |
310 | ||
961e9c84 | 311 | #endif /* _LINUX_VDPA_H */ |