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