Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * VFIO API definition
0004  *
0005  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
0006  *     Author: Alex Williamson <alex.williamson@redhat.com>
0007  */
0008 #ifndef VFIO_H
0009 #define VFIO_H
0010 
0011 
0012 #include <linux/iommu.h>
0013 #include <linux/mm.h>
0014 #include <linux/workqueue.h>
0015 #include <linux/poll.h>
0016 #include <uapi/linux/vfio.h>
0017 
0018 struct kvm;
0019 
0020 /*
0021  * VFIO devices can be placed in a set, this allows all devices to share this
0022  * structure and the VFIO core will provide a lock that is held around
0023  * open_device()/close_device() for all devices in the set.
0024  */
0025 struct vfio_device_set {
0026     void *set_id;
0027     struct mutex lock;
0028     struct list_head device_list;
0029     unsigned int device_count;
0030 };
0031 
0032 struct vfio_device {
0033     struct device *dev;
0034     const struct vfio_device_ops *ops;
0035     /*
0036      * mig_ops is a static property of the vfio_device which must be set
0037      * prior to registering the vfio_device.
0038      */
0039     const struct vfio_migration_ops *mig_ops;
0040     struct vfio_group *group;
0041     struct vfio_device_set *dev_set;
0042     struct list_head dev_set_list;
0043     unsigned int migration_flags;
0044     /* Driver must reference the kvm during open_device or never touch it */
0045     struct kvm *kvm;
0046 
0047     /* Members below here are private, not for driver use */
0048     refcount_t refcount;
0049     unsigned int open_count;
0050     struct completion comp;
0051     struct list_head group_next;
0052     struct list_head iommu_entry;
0053 };
0054 
0055 /**
0056  * struct vfio_device_ops - VFIO bus driver device callbacks
0057  *
0058  * @open_device: Called when the first file descriptor is opened for this device
0059  * @close_device: Opposite of open_device
0060  * @read: Perform read(2) on device file descriptor
0061  * @write: Perform write(2) on device file descriptor
0062  * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
0063  *         operations documented below
0064  * @mmap: Perform mmap(2) on a region of the device file descriptor
0065  * @request: Request for the bus driver to release the device
0066  * @match: Optional device name match callback (return: 0 for no-match, >0 for
0067  *         match, -errno for abort (ex. match with insufficient or incorrect
0068  *         additional args)
0069  * @dma_unmap: Called when userspace unmaps IOVA from the container
0070  *             this device is attached to.
0071  * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl
0072  */
0073 struct vfio_device_ops {
0074     char    *name;
0075     int (*open_device)(struct vfio_device *vdev);
0076     void    (*close_device)(struct vfio_device *vdev);
0077     ssize_t (*read)(struct vfio_device *vdev, char __user *buf,
0078             size_t count, loff_t *ppos);
0079     ssize_t (*write)(struct vfio_device *vdev, const char __user *buf,
0080              size_t count, loff_t *size);
0081     long    (*ioctl)(struct vfio_device *vdev, unsigned int cmd,
0082              unsigned long arg);
0083     int (*mmap)(struct vfio_device *vdev, struct vm_area_struct *vma);
0084     void    (*request)(struct vfio_device *vdev, unsigned int count);
0085     int (*match)(struct vfio_device *vdev, char *buf);
0086     void    (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length);
0087     int (*device_feature)(struct vfio_device *device, u32 flags,
0088                   void __user *arg, size_t argsz);
0089 };
0090 
0091 /**
0092  * @migration_set_state: Optional callback to change the migration state for
0093  *         devices that support migration. It's mandatory for
0094  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
0095  *         The returned FD is used for data transfer according to the FSM
0096  *         definition. The driver is responsible to ensure that FD reaches end
0097  *         of stream or error whenever the migration FSM leaves a data transfer
0098  *         state or before close_device() returns.
0099  * @migration_get_state: Optional callback to get the migration state for
0100  *         devices that support migration. It's mandatory for
0101  *         VFIO_DEVICE_FEATURE_MIGRATION migration support.
0102  */
0103 struct vfio_migration_ops {
0104     struct file *(*migration_set_state)(
0105         struct vfio_device *device,
0106         enum vfio_device_mig_state new_state);
0107     int (*migration_get_state)(struct vfio_device *device,
0108                    enum vfio_device_mig_state *curr_state);
0109 };
0110 
0111 /**
0112  * vfio_check_feature - Validate user input for the VFIO_DEVICE_FEATURE ioctl
0113  * @flags: Arg from the device_feature op
0114  * @argsz: Arg from the device_feature op
0115  * @supported_ops: Combination of VFIO_DEVICE_FEATURE_GET and SET the driver
0116  *                 supports
0117  * @minsz: Minimum data size the driver accepts
0118  *
0119  * For use in a driver's device_feature op. Checks that the inputs to the
0120  * VFIO_DEVICE_FEATURE ioctl are correct for the driver's feature. Returns 1 if
0121  * the driver should execute the get or set, otherwise the relevant
0122  * value should be returned.
0123  */
0124 static inline int vfio_check_feature(u32 flags, size_t argsz, u32 supported_ops,
0125                     size_t minsz)
0126 {
0127     if ((flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)) &
0128         ~supported_ops)
0129         return -EINVAL;
0130     if (flags & VFIO_DEVICE_FEATURE_PROBE)
0131         return 0;
0132     /* Without PROBE one of GET or SET must be requested */
0133     if (!(flags & (VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_SET)))
0134         return -EINVAL;
0135     if (argsz < minsz)
0136         return -EINVAL;
0137     return 1;
0138 }
0139 
0140 void vfio_init_group_dev(struct vfio_device *device, struct device *dev,
0141              const struct vfio_device_ops *ops);
0142 void vfio_uninit_group_dev(struct vfio_device *device);
0143 int vfio_register_group_dev(struct vfio_device *device);
0144 int vfio_register_emulated_iommu_dev(struct vfio_device *device);
0145 void vfio_unregister_group_dev(struct vfio_device *device);
0146 
0147 int vfio_assign_device_set(struct vfio_device *device, void *set_id);
0148 
0149 int vfio_mig_get_next_state(struct vfio_device *device,
0150                 enum vfio_device_mig_state cur_fsm,
0151                 enum vfio_device_mig_state new_fsm,
0152                 enum vfio_device_mig_state *next_fsm);
0153 
0154 /*
0155  * External user API
0156  */
0157 struct iommu_group *vfio_file_iommu_group(struct file *file);
0158 bool vfio_file_enforced_coherent(struct file *file);
0159 void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
0160 bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
0161 
0162 #define VFIO_PIN_PAGES_MAX_ENTRIES  (PAGE_SIZE/sizeof(unsigned long))
0163 
0164 int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
0165            int npage, int prot, struct page **pages);
0166 void vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova, int npage);
0167 int vfio_dma_rw(struct vfio_device *device, dma_addr_t iova,
0168         void *data, size_t len, bool write);
0169 
0170 /*
0171  * Sub-module helpers
0172  */
0173 struct vfio_info_cap {
0174     struct vfio_info_cap_header *buf;
0175     size_t size;
0176 };
0177 struct vfio_info_cap_header *vfio_info_cap_add(struct vfio_info_cap *caps,
0178                            size_t size, u16 id,
0179                            u16 version);
0180 void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
0181 
0182 int vfio_info_add_capability(struct vfio_info_cap *caps,
0183                  struct vfio_info_cap_header *cap, size_t size);
0184 
0185 int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
0186                        int num_irqs, int max_irq_type,
0187                        size_t *data_size);
0188 
0189 struct pci_dev;
0190 #if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
0191 void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
0192 void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
0193 long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group, unsigned int cmd,
0194                 unsigned long arg);
0195 #else
0196 static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
0197 {
0198 }
0199 
0200 static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
0201 {
0202 }
0203 
0204 static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
0205                           unsigned int cmd,
0206                           unsigned long arg)
0207 {
0208     return -ENOTTY;
0209 }
0210 #endif /* CONFIG_VFIO_SPAPR_EEH */
0211 
0212 /*
0213  * IRQfd - generic
0214  */
0215 struct virqfd {
0216     void            *opaque;
0217     struct eventfd_ctx  *eventfd;
0218     int         (*handler)(void *, void *);
0219     void            (*thread)(void *, void *);
0220     void            *data;
0221     struct work_struct  inject;
0222     wait_queue_entry_t      wait;
0223     poll_table      pt;
0224     struct work_struct  shutdown;
0225     struct virqfd       **pvirqfd;
0226 };
0227 
0228 int vfio_virqfd_enable(void *opaque, int (*handler)(void *, void *),
0229                void (*thread)(void *, void *), void *data,
0230                struct virqfd **pvirqfd, int fd);
0231 void vfio_virqfd_disable(struct virqfd **pvirqfd);
0232 
0233 #endif /* VFIO_H */