linux/include/linux/vfio.h
<<
>>
Prefs
   1/*
   2 * VFIO API definition
   3 *
   4 * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
   5 *     Author: Alex Williamson <alex.williamson@redhat.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 as
   9 * published by the Free Software Foundation.
  10 */
  11#ifndef VFIO_H
  12#define VFIO_H
  13
  14
  15#include <linux/iommu.h>
  16#include <linux/mm.h>
  17#include <linux/workqueue.h>
  18#include <linux/poll.h>
  19#include <uapi/linux/vfio.h>
  20
  21/**
  22 * struct vfio_device_ops - VFIO bus driver device callbacks
  23 *
  24 * @open: Called when userspace creates new file descriptor for device
  25 * @release: Called when userspace releases file descriptor for device
  26 * @read: Perform read(2) on device file descriptor
  27 * @write: Perform write(2) on device file descriptor
  28 * @ioctl: Perform ioctl(2) on device file descriptor, supporting VFIO_DEVICE_*
  29 *         operations documented below
  30 * @mmap: Perform mmap(2) on a region of the device file descriptor
  31 * @request: Request for the bus driver to release the device
  32 * @match: Optional device name match callback (return: 0 for no-match, >0 for
  33 *         match, -errno for abort (ex. match with insufficient or incorrect
  34 *         additional args)
  35 */
  36struct vfio_device_ops {
  37        char    *name;
  38        int     (*open)(void *device_data);
  39        void    (*release)(void *device_data);
  40        ssize_t (*read)(void *device_data, char __user *buf,
  41                        size_t count, loff_t *ppos);
  42        ssize_t (*write)(void *device_data, const char __user *buf,
  43                         size_t count, loff_t *size);
  44        long    (*ioctl)(void *device_data, unsigned int cmd,
  45                         unsigned long arg);
  46        int     (*mmap)(void *device_data, struct vm_area_struct *vma);
  47        void    (*request)(void *device_data, unsigned int count);
  48        int     (*match)(void *device_data, char *buf);
  49};
  50
  51extern struct iommu_group *vfio_iommu_group_get(struct device *dev);
  52extern void vfio_iommu_group_put(struct iommu_group *group, struct device *dev);
  53
  54extern int vfio_add_group_dev(struct device *dev,
  55                              const struct vfio_device_ops *ops,
  56                              void *device_data);
  57
  58extern void *vfio_del_group_dev(struct device *dev);
  59extern struct vfio_device *vfio_device_get_from_dev(struct device *dev);
  60extern void vfio_device_put(struct vfio_device *device);
  61extern void *vfio_device_data(struct vfio_device *device);
  62
  63/**
  64 * struct vfio_iommu_driver_ops - VFIO IOMMU driver callbacks
  65 */
  66struct vfio_iommu_driver_ops {
  67        char            *name;
  68        struct module   *owner;
  69        void            *(*open)(unsigned long arg);
  70        void            (*release)(void *iommu_data);
  71        ssize_t         (*read)(void *iommu_data, char __user *buf,
  72                                size_t count, loff_t *ppos);
  73        ssize_t         (*write)(void *iommu_data, const char __user *buf,
  74                                 size_t count, loff_t *size);
  75        long            (*ioctl)(void *iommu_data, unsigned int cmd,
  76                                 unsigned long arg);
  77        int             (*mmap)(void *iommu_data, struct vm_area_struct *vma);
  78        int             (*attach_group)(void *iommu_data,
  79                                        struct iommu_group *group);
  80        void            (*detach_group)(void *iommu_data,
  81                                        struct iommu_group *group);
  82        int             (*pin_pages)(void *iommu_data,
  83                                     struct iommu_group *group,
  84                                     unsigned long *user_pfn,
  85                                     int npage, int prot,
  86                                     unsigned long *phys_pfn);
  87        int             (*unpin_pages)(void *iommu_data,
  88                                       unsigned long *user_pfn, int npage);
  89        int             (*register_notifier)(void *iommu_data,
  90                                             unsigned long *events,
  91                                             struct notifier_block *nb);
  92        int             (*unregister_notifier)(void *iommu_data,
  93                                               struct notifier_block *nb);
  94        int             (*dma_rw)(void *iommu_data, dma_addr_t user_iova,
  95                                  void *data, size_t count, bool write);
  96};
  97
  98extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
  99
 100extern void vfio_unregister_iommu_driver(
 101                                const struct vfio_iommu_driver_ops *ops);
 102
 103/*
 104 * External user API
 105 */
 106extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
 107extern void vfio_group_put_external_user(struct vfio_group *group);
 108extern struct vfio_group *vfio_group_get_external_user_from_dev(struct device
 109                                                                *dev);
 110extern bool vfio_external_group_match_file(struct vfio_group *group,
 111                                           struct file *filep);
 112extern int vfio_external_user_iommu_id(struct vfio_group *group);
 113extern long vfio_external_check_extension(struct vfio_group *group,
 114                                          unsigned long arg);
 115
 116#define VFIO_PIN_PAGES_MAX_ENTRIES      (PAGE_SIZE/sizeof(unsigned long))
 117
 118extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
 119                          int npage, int prot, unsigned long *phys_pfn);
 120extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
 121                            int npage);
 122
 123extern int vfio_group_pin_pages(struct vfio_group *group,
 124                                unsigned long *user_iova_pfn, int npage,
 125                                int prot, unsigned long *phys_pfn);
 126extern int vfio_group_unpin_pages(struct vfio_group *group,
 127                                  unsigned long *user_iova_pfn, int npage);
 128
 129extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
 130                       void *data, size_t len, bool write);
 131
 132/* each type has independent events */
 133enum vfio_notify_type {
 134        VFIO_IOMMU_NOTIFY = 0,
 135        VFIO_GROUP_NOTIFY = 1,
 136};
 137
 138/* events for VFIO_IOMMU_NOTIFY */
 139#define VFIO_IOMMU_NOTIFY_DMA_UNMAP     BIT(0)
 140
 141/* events for VFIO_GROUP_NOTIFY */
 142#define VFIO_GROUP_NOTIFY_SET_KVM       BIT(0)
 143
 144extern int vfio_register_notifier(struct device *dev,
 145                                  enum vfio_notify_type type,
 146                                  unsigned long *required_events,
 147                                  struct notifier_block *nb);
 148extern int vfio_unregister_notifier(struct device *dev,
 149                                    enum vfio_notify_type type,
 150                                    struct notifier_block *nb);
 151
 152struct kvm;
 153extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
 154
 155/*
 156 * Sub-module helpers
 157 */
 158struct vfio_info_cap {
 159        struct vfio_info_cap_header *buf;
 160        size_t size;
 161};
 162extern struct vfio_info_cap_header *vfio_info_cap_add(
 163                struct vfio_info_cap *caps, size_t size, u16 id, u16 version);
 164extern void vfio_info_cap_shift(struct vfio_info_cap *caps, size_t offset);
 165
 166extern int vfio_info_add_capability(struct vfio_info_cap *caps,
 167                                    struct vfio_info_cap_header *cap,
 168                                    size_t size);
 169
 170extern int vfio_set_irqs_validate_and_prepare(struct vfio_irq_set *hdr,
 171                                              int num_irqs, int max_irq_type,
 172                                              size_t *data_size);
 173
 174struct pci_dev;
 175#if IS_ENABLED(CONFIG_VFIO_SPAPR_EEH)
 176extern void vfio_spapr_pci_eeh_open(struct pci_dev *pdev);
 177extern void vfio_spapr_pci_eeh_release(struct pci_dev *pdev);
 178extern long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
 179                                       unsigned int cmd,
 180                                       unsigned long arg);
 181#else
 182static inline void vfio_spapr_pci_eeh_open(struct pci_dev *pdev)
 183{
 184}
 185
 186static inline void vfio_spapr_pci_eeh_release(struct pci_dev *pdev)
 187{
 188}
 189
 190static inline long vfio_spapr_iommu_eeh_ioctl(struct iommu_group *group,
 191                                              unsigned int cmd,
 192                                              unsigned long arg)
 193{
 194        return -ENOTTY;
 195}
 196#endif /* CONFIG_VFIO_SPAPR_EEH */
 197
 198/*
 199 * IRQfd - generic
 200 */
 201struct virqfd {
 202        void                    *opaque;
 203        struct eventfd_ctx      *eventfd;
 204        int                     (*handler)(void *, void *);
 205        void                    (*thread)(void *, void *);
 206        void                    *data;
 207        struct work_struct      inject;
 208        wait_queue_entry_t              wait;
 209        poll_table              pt;
 210        struct work_struct      shutdown;
 211        struct virqfd           **pvirqfd;
 212};
 213
 214extern int vfio_virqfd_enable(void *opaque,
 215                              int (*handler)(void *, void *),
 216                              void (*thread)(void *, void *),
 217                              void *data, struct virqfd **pvirqfd, int fd);
 218extern void vfio_virqfd_disable(struct virqfd **pvirqfd);
 219
 220#endif /* VFIO_H */
 221