at v5.13 13 kB view raw
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 * struct vdpa_calllback - 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 * struct vdpa_notification_area - 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 * struct vdpa_vq_state - vDPA vq_state definition 32 * @avail_index: available index 33 */ 34struct vdpa_vq_state { 35 u16 avail_index; 36}; 37 38struct vdpa_mgmt_dev; 39 40/** 41 * struct 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 */ 51struct 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 * struct 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 */ 66struct vdpa_iova_range { 67 u64 first; 68 u64 last; 69}; 70 71/** 72 * struct 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_size: Get the size of the configuration space 154 * @vdev: vdpa device 155 * Returns size_t: configuration size 156 * @get_config: Read from device specific configuration space 157 * @vdev: vdpa device 158 * @offset: offset from the beginning of 159 * configuration space 160 * @buf: buffer used to read to 161 * @len: the length to read from 162 * configuration space 163 * @set_config: Write to device specific configuration space 164 * @vdev: vdpa device 165 * @offset: offset from the beginning of 166 * configuration space 167 * @buf: buffer used to write from 168 * @len: the length to write to 169 * configuration space 170 * @get_generation: Get device config generation (optional) 171 * @vdev: vdpa device 172 * Returns u32: device generation 173 * @get_iova_range: Get supported iova range (optional) 174 * @vdev: vdpa device 175 * Returns the iova range supported by 176 * the device. 177 * @set_map: Set device memory mapping (optional) 178 * Needed for device that using device 179 * specific DMA translation (on-chip IOMMU) 180 * @vdev: vdpa device 181 * @iotlb: vhost memory mapping to be 182 * used by the vDPA 183 * Returns integer: success (0) or error (< 0) 184 * @dma_map: Map an area of PA to IOVA (optional) 185 * Needed for device that using device 186 * specific DMA translation (on-chip IOMMU) 187 * and preferring incremental map. 188 * @vdev: vdpa device 189 * @iova: iova to be mapped 190 * @size: size of the area 191 * @pa: physical address for the map 192 * @perm: device access permission (VHOST_MAP_XX) 193 * Returns integer: success (0) or error (< 0) 194 * @dma_unmap: Unmap an area of IOVA (optional but 195 * must be implemented with dma_map) 196 * Needed for device that using device 197 * specific DMA translation (on-chip IOMMU) 198 * and preferring incremental unmap. 199 * @vdev: vdpa device 200 * @iova: iova to be unmapped 201 * @size: size of the area 202 * Returns integer: success (0) or error (< 0) 203 * @free: Free resources that belongs to vDPA (optional) 204 * @vdev: vdpa device 205 */ 206struct vdpa_config_ops { 207 /* Virtqueue ops */ 208 int (*set_vq_address)(struct vdpa_device *vdev, 209 u16 idx, u64 desc_area, u64 driver_area, 210 u64 device_area); 211 void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num); 212 void (*kick_vq)(struct vdpa_device *vdev, u16 idx); 213 void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx, 214 struct vdpa_callback *cb); 215 void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready); 216 bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx); 217 int (*set_vq_state)(struct vdpa_device *vdev, u16 idx, 218 const struct vdpa_vq_state *state); 219 int (*get_vq_state)(struct vdpa_device *vdev, u16 idx, 220 struct vdpa_vq_state *state); 221 struct vdpa_notification_area 222 (*get_vq_notification)(struct vdpa_device *vdev, u16 idx); 223 /* vq irq is not expected to be changed once DRIVER_OK is set */ 224 int (*get_vq_irq)(struct vdpa_device *vdv, u16 idx); 225 226 /* Device ops */ 227 u32 (*get_vq_align)(struct vdpa_device *vdev); 228 u64 (*get_features)(struct vdpa_device *vdev); 229 int (*set_features)(struct vdpa_device *vdev, u64 features); 230 void (*set_config_cb)(struct vdpa_device *vdev, 231 struct vdpa_callback *cb); 232 u16 (*get_vq_num_max)(struct vdpa_device *vdev); 233 u32 (*get_device_id)(struct vdpa_device *vdev); 234 u32 (*get_vendor_id)(struct vdpa_device *vdev); 235 u8 (*get_status)(struct vdpa_device *vdev); 236 void (*set_status)(struct vdpa_device *vdev, u8 status); 237 size_t (*get_config_size)(struct vdpa_device *vdev); 238 void (*get_config)(struct vdpa_device *vdev, unsigned int offset, 239 void *buf, unsigned int len); 240 void (*set_config)(struct vdpa_device *vdev, unsigned int offset, 241 const void *buf, unsigned int len); 242 u32 (*get_generation)(struct vdpa_device *vdev); 243 struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev); 244 245 /* DMA ops */ 246 int (*set_map)(struct vdpa_device *vdev, struct vhost_iotlb *iotlb); 247 int (*dma_map)(struct vdpa_device *vdev, u64 iova, u64 size, 248 u64 pa, u32 perm); 249 int (*dma_unmap)(struct vdpa_device *vdev, u64 iova, u64 size); 250 251 /* Free device resources */ 252 void (*free)(struct vdpa_device *vdev); 253}; 254 255struct vdpa_device *__vdpa_alloc_device(struct device *parent, 256 const struct vdpa_config_ops *config, 257 size_t size, const char *name); 258 259#define vdpa_alloc_device(dev_struct, member, parent, config, name) \ 260 container_of(__vdpa_alloc_device( \ 261 parent, config, \ 262 sizeof(dev_struct) + \ 263 BUILD_BUG_ON_ZERO(offsetof( \ 264 dev_struct, member)), name), \ 265 dev_struct, member) 266 267int vdpa_register_device(struct vdpa_device *vdev, int nvqs); 268void vdpa_unregister_device(struct vdpa_device *vdev); 269 270int _vdpa_register_device(struct vdpa_device *vdev, int nvqs); 271void _vdpa_unregister_device(struct vdpa_device *vdev); 272 273/** 274 * struct vdpa_driver - operations for a vDPA driver 275 * @driver: underlying device driver 276 * @probe: the function to call when a device is found. Returns 0 or -errno. 277 * @remove: the function to call when a device is removed. 278 */ 279struct vdpa_driver { 280 struct device_driver driver; 281 int (*probe)(struct vdpa_device *vdev); 282 void (*remove)(struct vdpa_device *vdev); 283}; 284 285#define vdpa_register_driver(drv) \ 286 __vdpa_register_driver(drv, THIS_MODULE) 287int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner); 288void vdpa_unregister_driver(struct vdpa_driver *drv); 289 290#define module_vdpa_driver(__vdpa_driver) \ 291 module_driver(__vdpa_driver, vdpa_register_driver, \ 292 vdpa_unregister_driver) 293 294static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver) 295{ 296 return container_of(driver, struct vdpa_driver, driver); 297} 298 299static inline struct vdpa_device *dev_to_vdpa(struct device *_dev) 300{ 301 return container_of(_dev, struct vdpa_device, dev); 302} 303 304static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev) 305{ 306 return dev_get_drvdata(&vdev->dev); 307} 308 309static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data) 310{ 311 dev_set_drvdata(&vdev->dev, data); 312} 313 314static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev) 315{ 316 return vdev->dma_dev; 317} 318 319static inline void vdpa_reset(struct vdpa_device *vdev) 320{ 321 const struct vdpa_config_ops *ops = vdev->config; 322 323 vdev->features_valid = false; 324 ops->set_status(vdev, 0); 325} 326 327static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features) 328{ 329 const struct vdpa_config_ops *ops = vdev->config; 330 331 vdev->features_valid = true; 332 return ops->set_features(vdev, features); 333} 334 335 336static inline void vdpa_get_config(struct vdpa_device *vdev, unsigned offset, 337 void *buf, unsigned int len) 338{ 339 const struct vdpa_config_ops *ops = vdev->config; 340 341 /* 342 * Config accesses aren't supposed to trigger before features are set. 343 * If it does happen we assume a legacy guest. 344 */ 345 if (!vdev->features_valid) 346 vdpa_set_features(vdev, 0); 347 ops->get_config(vdev, offset, buf, len); 348} 349 350/** 351 * struct vdpa_mgmtdev_ops - vdpa device ops 352 * @dev_add: Add a vdpa device using alloc and register 353 * @mdev: parent device to use for device addition 354 * @name: name of the new vdpa device 355 * Driver need to add a new device using _vdpa_register_device() 356 * after fully initializing the vdpa device. Driver must return 0 357 * on success or appropriate error code. 358 * @dev_del: Remove a vdpa device using unregister 359 * @mdev: parent device to use for device removal 360 * @dev: vdpa device to remove 361 * Driver need to remove the specified device by calling 362 * _vdpa_unregister_device(). 363 */ 364struct vdpa_mgmtdev_ops { 365 int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name); 366 void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev); 367}; 368 369struct vdpa_mgmt_dev { 370 struct device *device; 371 const struct vdpa_mgmtdev_ops *ops; 372 const struct virtio_device_id *id_table; /* supported ids */ 373 struct list_head list; 374}; 375 376int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev); 377void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev); 378 379#endif /* _LINUX_VDPA_H */