Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 */
15struct vdpa_callback {
16 irqreturn_t (*callback)(void *data);
17 void *private;
18};
19
20/**
21 * vDPA notification area
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
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
36 */
37struct vdpa_device {
38 struct device dev;
39 struct device *dma_dev;
40 const struct vdpa_config_ops *config;
41 unsigned int index;
42};
43
44/**
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
48 * list.
49 *
50 * @set_vq_address: Set the address of virtqueue
51 * @vdev: vdpa device
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
58 * @vdev: vdpa device
59 * @idx: virtqueue index
60 * @num: the size of virtqueue
61 * @kick_vq: Kick the virtqueue
62 * @vdev: vdpa device
63 * @idx: virtqueue index
64 * @set_vq_cb: Set the interrupt callback function for
65 * a virtqueue
66 * @vdev: vdpa device
67 * @idx: virtqueue index
68 * @cb: virtio-vdev interrupt callback structure
69 * @set_vq_ready: Set ready status for a virtqueue
70 * @vdev: vdpa device
71 * @idx: virtqueue index
72 * @ready: ready (true) not ready(false)
73 * @get_vq_ready: Get ready status for a virtqueue
74 * @vdev: vdpa device
75 * @idx: virtqueue index
76 * Returns boolean: ready (true) or not (false)
77 * @set_vq_state: Set the state for a virtqueue
78 * @vdev: vdpa device
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
83 * @vdev: vdpa device
84 * @idx: virtqueue index
85 * Returns virtqueue state (last_avail_idx)
86 * @get_vq_notification: Get the notification area for a virtqueue
87 * @vdev: vdpa device
88 * @idx: virtqueue index
89 * Returns the notifcation area
90 * @get_vq_align: Get the virtqueue align requirement
91 * for the device
92 * @vdev: vdpa device
93 * Returns virtqueue algin requirement
94 * @get_features: Get virtio features supported by the device
95 * @vdev: vdpa device
96 * Returns the virtio features support by the
97 * device
98 * @set_features: Set virtio features supported by the driver
99 * @vdev: vdpa device
100 * @features: feature support by the driver
101 * Returns integer: success (0) or error (< 0)
102 * @set_config_cb: Set the config interrupt callback
103 * @vdev: vdpa device
104 * @cb: virtio-vdev interrupt callback structure
105 * @get_vq_num_max: Get the max size of virtqueue
106 * @vdev: vdpa device
107 * Returns u16: max size of virtqueue
108 * @get_device_id: Get virtio device id
109 * @vdev: vdpa device
110 * Returns u32: virtio device id
111 * @get_vendor_id: Get id for the vendor that provides this device
112 * @vdev: vdpa device
113 * Returns u32: virtio vendor id
114 * @get_status: Get the device status
115 * @vdev: vdpa device
116 * Returns u8: virtio device status
117 * @set_status: Set the device status
118 * @vdev: vdpa device
119 * @status: virtio device status
120 * @get_config: Read from device specific configuration space
121 * @vdev: vdpa device
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
128 * @vdev: vdpa device
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)
135 * @vdev: vdpa device
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)
140 * @vdev: vdpa device
141 * @iotlb: vhost memory mapping to be
142 * used by the vDPA
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.
148 * @vdev: vdpa device
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.
159 * @vdev: vdpa device
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)
164 * @vdev: vdpa device
165 */
166struct vdpa_config_ops {
167 /* Virtqueue ops */
168 int (*set_vq_address)(struct vdpa_device *vdev,
169 u16 idx, u64 desc_area, u64 driver_area,
170 u64 device_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);
181
182 /* Device ops */
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);
198
199 /* DMA ops */
200 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb);
201 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size,
202 u64 pa, u32 perm);
203 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size);
204
205 /* Free device resources */
206 void (*free)(struct vdpa_device *vdev);
207};
208
209struct vdpa_device *__vdpa_alloc_device(struct device *parent,
210 const struct vdpa_config_ops *config,
211 size_t size);
212
213#define vdpa_alloc_device(dev_struct, member, parent, config) \
214 container_of(__vdpa_alloc_device( \
215 parent, config, \
216 sizeof(dev_struct) + \
217 BUILD_BUG_ON_ZERO(offsetof( \
218 dev_struct, member))), \
219 dev_struct, member)
220
221int vdpa_register_device(struct vdpa_device *vdev);
222void vdpa_unregister_device(struct vdpa_device *vdev);
223
224/**
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.
229 */
230struct vdpa_driver {
231 struct device_driver driver;
232 int (*probe)(struct vdpa_device *vdev);
233 void (*remove)(struct vdpa_device *vdev);
234};
235
236#define vdpa_register_driver(drv) \
237 __vdpa_register_driver(drv, THIS_MODULE)
238int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
239void vdpa_unregister_driver(struct vdpa_driver *drv);
240
241#define module_vdpa_driver(__vdpa_driver) \
242 module_driver(__vdpa_driver, vdpa_register_driver, \
243 vdpa_unregister_driver)
244
245static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
246{
247 return container_of(driver, struct vdpa_driver, driver);
248}
249
250static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
251{
252 return container_of(_dev, struct vdpa_device, dev);
253}
254
255static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
256{
257 return dev_get_drvdata(&vdev->dev);
258}
259
260static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
261{
262 dev_set_drvdata(&vdev->dev, data);
263}
264
265static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
266{
267 return vdev->dma_dev;
268}
269#endif /* _LINUX_VDPA_H */