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 | ||
aac50c0b EC |
30 | /** |
31 | * vDPA vq_state definition | |
32 | * @avail_index: available index | |
33 | */ | |
34 | struct vdpa_vq_state { | |
35 | u16 avail_index; | |
36 | }; | |
37 | ||
961e9c84 JW |
38 | /** |
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 | |
452639a6 | 44 | * @features_valid: were features initialized? for legacy guests |
961e9c84 JW |
45 | */ |
46 | struct vdpa_device { | |
47 | struct device dev; | |
48 | struct device *dma_dev; | |
49 | const struct vdpa_config_ops *config; | |
50 | unsigned int index; | |
452639a6 | 51 | bool features_valid; |
a9974489 | 52 | int nvqs; |
961e9c84 JW |
53 | }; |
54 | ||
55 | /** | |
56 | * vDPA_config_ops - operations for configuring a vDPA device. | |
57 | * Note: vDPA device drivers are required to implement all of the | |
58 | * operations unless it is mentioned to be optional in the following | |
59 | * list. | |
60 | * | |
61 | * @set_vq_address: Set the address of virtqueue | |
62 | * @vdev: vdpa device | |
63 | * @idx: virtqueue index | |
64 | * @desc_area: address of desc area | |
65 | * @driver_area: address of driver area | |
66 | * @device_area: address of device area | |
67 | * Returns integer: success (0) or error (< 0) | |
68 | * @set_vq_num: Set the size of virtqueue | |
69 | * @vdev: vdpa device | |
70 | * @idx: virtqueue index | |
71 | * @num: the size of virtqueue | |
72 | * @kick_vq: Kick the virtqueue | |
73 | * @vdev: vdpa device | |
74 | * @idx: virtqueue index | |
75 | * @set_vq_cb: Set the interrupt callback function for | |
76 | * a virtqueue | |
77 | * @vdev: vdpa device | |
78 | * @idx: virtqueue index | |
79 | * @cb: virtio-vdev interrupt callback structure | |
80 | * @set_vq_ready: Set ready status for a virtqueue | |
81 | * @vdev: vdpa device | |
82 | * @idx: virtqueue index | |
83 | * @ready: ready (true) not ready(false) | |
84 | * @get_vq_ready: Get ready status for a virtqueue | |
85 | * @vdev: vdpa device | |
86 | * @idx: virtqueue index | |
87 | * Returns boolean: ready (true) or not (false) | |
88 | * @set_vq_state: Set the state for a virtqueue | |
89 | * @vdev: vdpa device | |
90 | * @idx: virtqueue index | |
aac50c0b | 91 | * @state: pointer to set virtqueue state (last_avail_idx) |
961e9c84 JW |
92 | * Returns integer: success (0) or error (< 0) |
93 | * @get_vq_state: Get the state for a virtqueue | |
94 | * @vdev: vdpa device | |
95 | * @idx: virtqueue index | |
aac50c0b | 96 | * @state: pointer to returned state (last_avail_idx) |
c25a26e6 JW |
97 | * @get_vq_notification: Get the notification area for a virtqueue |
98 | * @vdev: vdpa device | |
99 | * @idx: virtqueue index | |
100 | * Returns the notifcation area | |
7164675a ZL |
101 | * @get_vq_irq: Get the irq number of a virtqueue (optional, |
102 | * but must implemented if require vq irq offloading) | |
103 | * @vdev: vdpa device | |
104 | * @idx: virtqueue index | |
105 | * Returns int: irq number of a virtqueue, | |
106 | * negative number if no irq assigned. | |
961e9c84 JW |
107 | * @get_vq_align: Get the virtqueue align requirement |
108 | * for the device | |
109 | * @vdev: vdpa device | |
110 | * Returns virtqueue algin requirement | |
111 | * @get_features: Get virtio features supported by the device | |
112 | * @vdev: vdpa device | |
113 | * Returns the virtio features support by the | |
114 | * device | |
115 | * @set_features: Set virtio features supported by the driver | |
116 | * @vdev: vdpa device | |
117 | * @features: feature support by the driver | |
118 | * Returns integer: success (0) or error (< 0) | |
119 | * @set_config_cb: Set the config interrupt callback | |
120 | * @vdev: vdpa device | |
121 | * @cb: virtio-vdev interrupt callback structure | |
122 | * @get_vq_num_max: Get the max size of virtqueue | |
123 | * @vdev: vdpa device | |
124 | * Returns u16: max size of virtqueue | |
125 | * @get_device_id: Get virtio device id | |
126 | * @vdev: vdpa device | |
127 | * Returns u32: virtio device id | |
128 | * @get_vendor_id: Get id for the vendor that provides this device | |
129 | * @vdev: vdpa device | |
130 | * Returns u32: virtio vendor id | |
131 | * @get_status: Get the device status | |
132 | * @vdev: vdpa device | |
133 | * Returns u8: virtio device status | |
134 | * @set_status: Set the device status | |
135 | * @vdev: vdpa device | |
136 | * @status: virtio device status | |
137 | * @get_config: Read from device specific configuration space | |
138 | * @vdev: vdpa device | |
139 | * @offset: offset from the beginning of | |
140 | * configuration space | |
141 | * @buf: buffer used to read to | |
142 | * @len: the length to read from | |
143 | * configuration space | |
144 | * @set_config: Write to device specific configuration space | |
145 | * @vdev: vdpa device | |
146 | * @offset: offset from the beginning of | |
147 | * configuration space | |
148 | * @buf: buffer used to write from | |
149 | * @len: the length to write to | |
150 | * configuration space | |
151 | * @get_generation: Get device config generation (optional) | |
152 | * @vdev: vdpa device | |
153 | * Returns u32: device generation | |
154 | * @set_map: Set device memory mapping (optional) | |
155 | * Needed for device that using device | |
156 | * specific DMA translation (on-chip IOMMU) | |
157 | * @vdev: vdpa device | |
158 | * @iotlb: vhost memory mapping to be | |
159 | * used by the vDPA | |
160 | * Returns integer: success (0) or error (< 0) | |
161 | * @dma_map: Map an area of PA to IOVA (optional) | |
162 | * Needed for device that using device | |
163 | * specific DMA translation (on-chip IOMMU) | |
164 | * and preferring incremental map. | |
165 | * @vdev: vdpa device | |
166 | * @iova: iova to be mapped | |
167 | * @size: size of the area | |
168 | * @pa: physical address for the map | |
169 | * @perm: device access permission (VHOST_MAP_XX) | |
170 | * Returns integer: success (0) or error (< 0) | |
171 | * @dma_unmap: Unmap an area of IOVA (optional but | |
172 | * must be implemented with dma_map) | |
173 | * Needed for device that using device | |
174 | * specific DMA translation (on-chip IOMMU) | |
175 | * and preferring incremental unmap. | |
176 | * @vdev: vdpa device | |
177 | * @iova: iova to be unmapped | |
178 | * @size: size of the area | |
179 | * Returns integer: success (0) or error (< 0) | |
180 | * @free: Free resources that belongs to vDPA (optional) | |
181 | * @vdev: vdpa device | |
182 | */ | |
183 | struct vdpa_config_ops { | |
184 | /* Virtqueue ops */ | |
185 | int (*set_vq_address)(struct vdpa_device *vdev, | |
186 | u16 idx, u64 desc_area, u64 driver_area, | |
187 | u64 device_area); | |
188 | void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num); | |
189 | void (*kick_vq)(struct vdpa_device *vdev, u16 idx); | |
190 | void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx, | |
191 | struct vdpa_callback *cb); | |
192 | void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready); | |
193 | bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx); | |
aac50c0b EC |
194 | int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, |
195 | const struct vdpa_vq_state *state); | |
23750e39 EC |
196 | int (*get_vq_state)(struct vdpa_device *vdev, u16 idx, |
197 | struct vdpa_vq_state *state); | |
c25a26e6 JW |
198 | struct vdpa_notification_area |
199 | (*get_vq_notification)(struct vdpa_device *vdev, u16 idx); | |
4c05433b | 200 | /* vq irq is not expected to be changed once DRIVER_OK is set */ |
7164675a | 201 | int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx); |
961e9c84 JW |
202 | |
203 | /* Device ops */ | |
425a5070 | 204 | u32 (*get_vq_align)(struct vdpa_device *vdev); |
961e9c84 JW |
205 | u64 (*get_features)(struct vdpa_device *vdev); |
206 | int (*set_features)(struct vdpa_device *vdev, u64 features); | |
207 | void (*set_config_cb)(struct vdpa_device *vdev, | |
208 | struct vdpa_callback *cb); | |
209 | u16 (*get_vq_num_max)(struct vdpa_device *vdev); | |
210 | u32 (*get_device_id)(struct vdpa_device *vdev); | |
211 | u32 (*get_vendor_id)(struct vdpa_device *vdev); | |
212 | u8 (*get_status)(struct vdpa_device *vdev); | |
213 | void (*set_status)(struct vdpa_device *vdev, u8 status); | |
214 | void (*get_config)(struct vdpa_device *vdev, unsigned int offset, | |
215 | void *buf, unsigned int len); | |
216 | void (*set_config)(struct vdpa_device *vdev, unsigned int offset, | |
217 | const void *buf, unsigned int len); | |
218 | u32 (*get_generation)(struct vdpa_device *vdev); | |
219 | ||
220 | /* DMA ops */ | |
221 | int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb); | |
222 | int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size, | |
223 | u64 pa, u32 perm); | |
224 | int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size); | |
225 | ||
226 | /* Free device resources */ | |
227 | void (*free)(struct vdpa_device *vdev); | |
228 | }; | |
229 | ||
230 | struct vdpa_device *__vdpa_alloc_device(struct device *parent, | |
231 | const struct vdpa_config_ops *config, | |
a9974489 | 232 | int nvqs, |
961e9c84 JW |
233 | size_t size); |
234 | ||
a9974489 | 235 | #define vdpa_alloc_device(dev_struct, member, parent, config, nvqs) \ |
961e9c84 | 236 | container_of(__vdpa_alloc_device( \ |
a9974489 | 237 | parent, config, nvqs, \ |
961e9c84 JW |
238 | sizeof(dev_struct) + \ |
239 | BUILD_BUG_ON_ZERO(offsetof( \ | |
240 | dev_struct, member))), \ | |
241 | dev_struct, member) | |
242 | ||
243 | int vdpa_register_device(struct vdpa_device *vdev); | |
244 | void vdpa_unregister_device(struct vdpa_device *vdev); | |
245 | ||
246 | /** | |
247 | * vdpa_driver - operations for a vDPA driver | |
248 | * @driver: underlying device driver | |
249 | * @probe: the function to call when a device is found. Returns 0 or -errno. | |
250 | * @remove: the function to call when a device is removed. | |
251 | */ | |
252 | struct vdpa_driver { | |
253 | struct device_driver driver; | |
254 | int (*probe)(struct vdpa_device *vdev); | |
255 | void (*remove)(struct vdpa_device *vdev); | |
256 | }; | |
257 | ||
258 | #define vdpa_register_driver(drv) \ | |
259 | __vdpa_register_driver(drv, THIS_MODULE) | |
260 | int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner); | |
261 | void vdpa_unregister_driver(struct vdpa_driver *drv); | |
262 | ||
263 | #define module_vdpa_driver(__vdpa_driver) \ | |
264 | module_driver(__vdpa_driver, vdpa_register_driver, \ | |
265 | vdpa_unregister_driver) | |
266 | ||
267 | static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver) | |
268 | { | |
269 | return container_of(driver, struct vdpa_driver, driver); | |
270 | } | |
271 | ||
272 | static inline struct vdpa_device *dev_to_vdpa(struct device *_dev) | |
273 | { | |
274 | return container_of(_dev, struct vdpa_device, dev); | |
275 | } | |
276 | ||
277 | static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev) | |
278 | { | |
279 | return dev_get_drvdata(&vdev->dev); | |
280 | } | |
281 | ||
282 | static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data) | |
283 | { | |
284 | dev_set_drvdata(&vdev->dev, data); | |
285 | } | |
286 | ||
287 | static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev) | |
288 | { | |
289 | return vdev->dma_dev; | |
290 | } | |
452639a6 MT |
291 | |
292 | static inline void vdpa_reset(struct vdpa_device *vdev) | |
293 | { | |
294 | const struct vdpa_config_ops *ops = vdev->config; | |
295 | ||
296 | vdev->features_valid = false; | |
297 | ops->set_status(vdev, 0); | |
298 | } | |
299 | ||
300 | static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) | |
301 | { | |
302 | const struct vdpa_config_ops *ops = vdev->config; | |
303 | ||
304 | vdev->features_valid = true; | |
305 | return ops->set_features(vdev, features); | |
306 | } | |
307 | ||
308 | ||
309 | static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, | |
310 | void *buf, unsigned int len) | |
311 | { | |
312 | const struct vdpa_config_ops *ops = vdev->config; | |
313 | ||
314 | /* | |
315 | * Config accesses aren't supposed to trigger before features are set. | |
316 | * If it does happen we assume a legacy guest. | |
317 | */ | |
318 | if (!vdev->features_valid) | |
319 | vdpa_set_features(vdev, 0); | |
320 | ops->get_config(vdev, offset, buf, len); | |
321 | } | |
322 | ||
961e9c84 | 323 | #endif /* _LINUX_VDPA_H */ |