vdpa: Add an opaque pointer for vdpa_config_ops.dma_map()
[linux-block.git] / include / linux / vdpa.h
CommitLineData
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/**
d0f9164e 11 * struct vdpa_calllback - vDPA callback definition.
961e9c84
JW
12 * @callback: interrupt callback function
13 * @private: the data passed to the callback function
14 */
15struct vdpa_callback {
16 irqreturn_t (*callback)(void *data);
17 void *private;
18};
19
c25a26e6 20/**
d0f9164e 21 * struct vdpa_notification_area - vDPA notification area
c25a26e6
JW
22 * @addr: base address of the notification area
23 * @size: size of the notification area
24 */
25struct vdpa_notification_area {
26 resource_size_t addr;
27 resource_size_t size;
28};
29
aac50c0b 30/**
530a5678 31 * struct vdpa_vq_state_split - vDPA split virtqueue state
aac50c0b
EC
32 * @avail_index: available index
33 */
530a5678 34struct vdpa_vq_state_split {
aac50c0b
EC
35 u16 avail_index;
36};
37
530a5678
JW
38/**
39 * struct vdpa_vq_state_packed - vDPA packed virtqueue state
40 * @last_avail_counter: last driver ring wrap counter observed by device
41 * @last_avail_idx: device available index
42 * @last_used_counter: device ring wrap counter
43 * @last_used_idx: used index
44 */
45struct vdpa_vq_state_packed {
86e17a51
XY
46 u16 last_avail_counter:1;
47 u16 last_avail_idx:15;
48 u16 last_used_counter:1;
49 u16 last_used_idx:15;
530a5678
JW
50};
51
52struct vdpa_vq_state {
86e17a51
XY
53 union {
54 struct vdpa_vq_state_split split;
55 struct vdpa_vq_state_packed packed;
56 };
530a5678
JW
57};
58
33b34750
PP
59struct vdpa_mgmt_dev;
60
961e9c84 61/**
d0f9164e 62 * struct vdpa_device - representation of a vDPA device
961e9c84
JW
63 * @dev: underlying device
64 * @dma_dev: the actual device that is performing DMA
65 * @config: the configuration ops for this device.
66 * @index: device index
452639a6 67 * @features_valid: were features initialized? for legacy guests
476c135e 68 * @nvqs: maximum number of supported virtqueues
903f7bca
PP
69 * @mdev: management device pointer; caller must setup when registering device as part
70 * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
961e9c84
JW
71 */
72struct vdpa_device {
73 struct device dev;
74 struct device *dma_dev;
75 const struct vdpa_config_ops *config;
76 unsigned int index;
452639a6 77 bool features_valid;
a9974489 78 int nvqs;
903f7bca 79 struct vdpa_mgmt_dev *mdev;
961e9c84
JW
80};
81
3f1b623a 82/**
d0f9164e 83 * struct vdpa_iova_range - the IOVA range support by the device
3f1b623a
JW
84 * @first: start of the IOVA range
85 * @last: end of the IOVA range
86 */
87struct vdpa_iova_range {
88 u64 first;
89 u64 last;
90};
91
961e9c84 92/**
d0f9164e 93 * struct vdpa_config_ops - operations for configuring a vDPA device.
961e9c84
JW
94 * Note: vDPA device drivers are required to implement all of the
95 * operations unless it is mentioned to be optional in the following
96 * list.
97 *
98 * @set_vq_address: Set the address of virtqueue
99 * @vdev: vdpa device
100 * @idx: virtqueue index
101 * @desc_area: address of desc area
102 * @driver_area: address of driver area
103 * @device_area: address of device area
104 * Returns integer: success (0) or error (< 0)
105 * @set_vq_num: Set the size of virtqueue
106 * @vdev: vdpa device
107 * @idx: virtqueue index
108 * @num: the size of virtqueue
109 * @kick_vq: Kick the virtqueue
110 * @vdev: vdpa device
111 * @idx: virtqueue index
112 * @set_vq_cb: Set the interrupt callback function for
113 * a virtqueue
114 * @vdev: vdpa device
115 * @idx: virtqueue index
116 * @cb: virtio-vdev interrupt callback structure
117 * @set_vq_ready: Set ready status for a virtqueue
118 * @vdev: vdpa device
119 * @idx: virtqueue index
120 * @ready: ready (true) not ready(false)
121 * @get_vq_ready: Get ready status for a virtqueue
122 * @vdev: vdpa device
123 * @idx: virtqueue index
124 * Returns boolean: ready (true) or not (false)
125 * @set_vq_state: Set the state for a virtqueue
126 * @vdev: vdpa device
127 * @idx: virtqueue index
aac50c0b 128 * @state: pointer to set virtqueue state (last_avail_idx)
961e9c84
JW
129 * Returns integer: success (0) or error (< 0)
130 * @get_vq_state: Get the state for a virtqueue
131 * @vdev: vdpa device
132 * @idx: virtqueue index
aac50c0b 133 * @state: pointer to returned state (last_avail_idx)
86e17a51 134 * @get_vq_notification: Get the notification area for a virtqueue
c25a26e6
JW
135 * @vdev: vdpa device
136 * @idx: virtqueue index
137 * Returns the notifcation area
7164675a
ZL
138 * @get_vq_irq: Get the irq number of a virtqueue (optional,
139 * but must implemented if require vq irq offloading)
140 * @vdev: vdpa device
141 * @idx: virtqueue index
142 * Returns int: irq number of a virtqueue,
143 * negative number if no irq assigned.
961e9c84
JW
144 * @get_vq_align: Get the virtqueue align requirement
145 * for the device
146 * @vdev: vdpa device
147 * Returns virtqueue algin requirement
148 * @get_features: Get virtio features supported by the device
149 * @vdev: vdpa device
150 * Returns the virtio features support by the
151 * device
152 * @set_features: Set virtio features supported by the driver
153 * @vdev: vdpa device
154 * @features: feature support by the driver
155 * Returns integer: success (0) or error (< 0)
156 * @set_config_cb: Set the config interrupt callback
157 * @vdev: vdpa device
158 * @cb: virtio-vdev interrupt callback structure
159 * @get_vq_num_max: Get the max size of virtqueue
160 * @vdev: vdpa device
161 * Returns u16: max size of virtqueue
162 * @get_device_id: Get virtio device id
163 * @vdev: vdpa device
164 * Returns u32: virtio device id
165 * @get_vendor_id: Get id for the vendor that provides this device
166 * @vdev: vdpa device
167 * Returns u32: virtio vendor id
168 * @get_status: Get the device status
169 * @vdev: vdpa device
170 * Returns u8: virtio device status
171 * @set_status: Set the device status
172 * @vdev: vdpa device
173 * @status: virtio device status
0686082d
XY
174 * @reset: Reset device
175 * @vdev: vdpa device
176 * Returns integer: success (0) or error (< 0)
442706f9
SG
177 * @get_config_size: Get the size of the configuration space
178 * @vdev: vdpa device
179 * Returns size_t: configuration size
961e9c84
JW
180 * @get_config: Read from device specific configuration space
181 * @vdev: vdpa device
182 * @offset: offset from the beginning of
183 * configuration space
184 * @buf: buffer used to read to
185 * @len: the length to read from
186 * configuration space
187 * @set_config: Write to device specific configuration space
188 * @vdev: vdpa device
189 * @offset: offset from the beginning of
190 * configuration space
191 * @buf: buffer used to write from
192 * @len: the length to write to
193 * configuration space
194 * @get_generation: Get device config generation (optional)
195 * @vdev: vdpa device
196 * Returns u32: device generation
3f1b623a
JW
197 * @get_iova_range: Get supported iova range (optional)
198 * @vdev: vdpa device
199 * Returns the iova range supported by
200 * the device.
961e9c84
JW
201 * @set_map: Set device memory mapping (optional)
202 * Needed for device that using device
203 * specific DMA translation (on-chip IOMMU)
204 * @vdev: vdpa device
205 * @iotlb: vhost memory mapping to be
206 * used by the vDPA
207 * Returns integer: success (0) or error (< 0)
208 * @dma_map: Map an area of PA to IOVA (optional)
209 * Needed for device that using device
210 * specific DMA translation (on-chip IOMMU)
211 * and preferring incremental map.
212 * @vdev: vdpa device
213 * @iova: iova to be mapped
214 * @size: size of the area
215 * @pa: physical address for the map
216 * @perm: device access permission (VHOST_MAP_XX)
217 * Returns integer: success (0) or error (< 0)
218 * @dma_unmap: Unmap an area of IOVA (optional but
219 * must be implemented with dma_map)
220 * Needed for device that using device
221 * specific DMA translation (on-chip IOMMU)
222 * and preferring incremental unmap.
223 * @vdev: vdpa device
224 * @iova: iova to be unmapped
225 * @size: size of the area
226 * Returns integer: success (0) or error (< 0)
227 * @free: Free resources that belongs to vDPA (optional)
228 * @vdev: vdpa device
229 */
230struct vdpa_config_ops {
231 /* Virtqueue ops */
232 int (*set_vq_address)(struct vdpa_device *vdev,
233 u16 idx, u64 desc_area, u64 driver_area,
234 u64 device_area);
235 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
236 void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
237 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
238 struct vdpa_callback *cb);
239 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
240 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
aac50c0b
EC
241 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
242 const struct vdpa_vq_state *state);
23750e39
EC
243 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
244 struct vdpa_vq_state *state);
c25a26e6
JW
245 struct vdpa_notification_area
246 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
4c05433b 247 /* vq irq is not expected to be changed once DRIVER_OK is set */
7164675a 248 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx);
961e9c84
JW
249
250 /* Device ops */
425a5070 251 u32 (*get_vq_align)(struct vdpa_device *vdev);
961e9c84
JW
252 u64 (*get_features)(struct vdpa_device *vdev);
253 int (*set_features)(struct vdpa_device *vdev, u64 features);
254 void (*set_config_cb)(struct vdpa_device *vdev,
255 struct vdpa_callback *cb);
256 u16 (*get_vq_num_max)(struct vdpa_device *vdev);
257 u32 (*get_device_id)(struct vdpa_device *vdev);
258 u32 (*get_vendor_id)(struct vdpa_device *vdev);
259 u8 (*get_status)(struct vdpa_device *vdev);
260 void (*set_status)(struct vdpa_device *vdev, u8 status);
0686082d 261 int (*reset)(struct vdpa_device *vdev);
442706f9 262 size_t (*get_config_size)(struct vdpa_device *vdev);
961e9c84
JW
263 void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
264 void *buf, unsigned int len);
265 void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
266 const void *buf, unsigned int len);
267 u32 (*get_generation)(struct vdpa_device *vdev);
3f1b623a 268 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
961e9c84
JW
269
270 /* DMA ops */
271 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
272 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
c10fb945 273 u64 pa, u32 perm, void *opaque);
961e9c84
JW
274 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
275
276 /* Free device resources */
277 void (*free)(struct vdpa_device *vdev);
278};
279
280struct vdpa_device *__vdpa_alloc_device(struct device *parent,
281 const struct vdpa_config_ops *config,
f00bdce0 282 size_t size, const char *name);
961e9c84 283
c8d182bd
XY
284/**
285 * vdpa_alloc_device - allocate and initilaize a vDPA device
286 *
287 * @dev_struct: the type of the parent structure
288 * @member: the name of struct vdpa_device within the @dev_struct
289 * @parent: the parent device
290 * @config: the bus operations that is supported by this device
291 * @name: name of the vdpa device
292 *
293 * Return allocated data structure or ERR_PTR upon error
294 */
f00bdce0 295#define vdpa_alloc_device(dev_struct, member, parent, config, name) \
961e9c84 296 container_of(__vdpa_alloc_device( \
f00bdce0 297 parent, config, \
961e9c84
JW
298 sizeof(dev_struct) + \
299 BUILD_BUG_ON_ZERO(offsetof( \
fd70a406 300 dev_struct, member)), name), \
961e9c84
JW
301 dev_struct, member)
302
f00bdce0 303int vdpa_register_device(struct vdpa_device *vdev, int nvqs);
961e9c84
JW
304void vdpa_unregister_device(struct vdpa_device *vdev);
305
f00bdce0 306int _vdpa_register_device(struct vdpa_device *vdev, int nvqs);
903f7bca
PP
307void _vdpa_unregister_device(struct vdpa_device *vdev);
308
961e9c84 309/**
d0f9164e 310 * struct vdpa_driver - operations for a vDPA driver
961e9c84
JW
311 * @driver: underlying device driver
312 * @probe: the function to call when a device is found. Returns 0 or -errno.
313 * @remove: the function to call when a device is removed.
314 */
315struct vdpa_driver {
316 struct device_driver driver;
317 int (*probe)(struct vdpa_device *vdev);
318 void (*remove)(struct vdpa_device *vdev);
319};
320
321#define vdpa_register_driver(drv) \
322 __vdpa_register_driver(drv, THIS_MODULE)
323int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
324void vdpa_unregister_driver(struct vdpa_driver *drv);
325
326#define module_vdpa_driver(__vdpa_driver) \
327 module_driver(__vdpa_driver, vdpa_register_driver, \
328 vdpa_unregister_driver)
329
330static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
331{
332 return container_of(driver, struct vdpa_driver, driver);
333}
334
335static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
336{
337 return container_of(_dev, struct vdpa_device, dev);
338}
339
340static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
341{
342 return dev_get_drvdata(&vdev->dev);
343}
344
345static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
346{
347 dev_set_drvdata(&vdev->dev, data);
348}
349
350static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
351{
352 return vdev->dma_dev;
353}
452639a6 354
0686082d 355static inline int vdpa_reset(struct vdpa_device *vdev)
452639a6 356{
86e17a51 357 const struct vdpa_config_ops *ops = vdev->config;
452639a6
MT
358
359 vdev->features_valid = false;
0686082d 360 return ops->reset(vdev);
452639a6
MT
361}
362
363static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
364{
86e17a51 365 const struct vdpa_config_ops *ops = vdev->config;
452639a6
MT
366
367 vdev->features_valid = true;
86e17a51 368 return ops->set_features(vdev, features);
452639a6
MT
369}
370
86e17a51
XY
371static inline void vdpa_get_config(struct vdpa_device *vdev,
372 unsigned int offset, void *buf,
373 unsigned int len)
452639a6 374{
86e17a51 375 const struct vdpa_config_ops *ops = vdev->config;
452639a6
MT
376
377 /*
378 * Config accesses aren't supposed to trigger before features are set.
379 * If it does happen we assume a legacy guest.
380 */
381 if (!vdev->features_valid)
382 vdpa_set_features(vdev, 0);
383 ops->get_config(vdev, offset, buf, len);
384}
385
33b34750 386/**
d0f9164e
PP
387 * struct vdpa_mgmtdev_ops - vdpa device ops
388 * @dev_add: Add a vdpa device using alloc and register
389 * @mdev: parent device to use for device addition
390 * @name: name of the new vdpa device
391 * Driver need to add a new device using _vdpa_register_device()
392 * after fully initializing the vdpa device. Driver must return 0
393 * on success or appropriate error code.
394 * @dev_del: Remove a vdpa device using unregister
395 * @mdev: parent device to use for device removal
396 * @dev: vdpa device to remove
397 * Driver need to remove the specified device by calling
398 * _vdpa_unregister_device().
33b34750
PP
399 */
400struct vdpa_mgmtdev_ops {
401 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name);
402 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
403};
404
405struct vdpa_mgmt_dev {
406 struct device *device;
407 const struct vdpa_mgmtdev_ops *ops;
408 const struct virtio_device_id *id_table; /* supported ids */
409 struct list_head list;
410};
411
412int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
413void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
414
961e9c84 415#endif /* _LINUX_VDPA_H */