1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
6 * Derived from original vfio:
7 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
11 #include <linux/mutex.h>
12 #include <linux/pci.h>
13 #include <linux/vfio.h>
14 #include <linux/irqbypass.h>
15 #include <linux/types.h>
16 #include <linux/uuid.h>
17 #include <linux/notifier.h>
19 #ifndef VFIO_PCI_CORE_H
20 #define VFIO_PCI_CORE_H
22 #define VFIO_PCI_OFFSET_SHIFT 40
23 #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
24 #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
25 #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
27 struct vfio_pci_core_device;
28 struct vfio_pci_region;
30 struct vfio_pci_regops {
31 ssize_t (*rw)(struct vfio_pci_core_device *vdev, char __user *buf,
32 size_t count, loff_t *ppos, bool iswrite);
33 void (*release)(struct vfio_pci_core_device *vdev,
34 struct vfio_pci_region *region);
35 int (*mmap)(struct vfio_pci_core_device *vdev,
36 struct vfio_pci_region *region,
37 struct vm_area_struct *vma);
38 int (*add_capability)(struct vfio_pci_core_device *vdev,
39 struct vfio_pci_region *region,
40 struct vfio_info_cap *caps);
43 struct vfio_pci_region {
46 const struct vfio_pci_regops *ops;
52 struct vfio_pci_core_device {
53 struct vfio_device vdev;
55 void __iomem *barmap[PCI_STD_NUM_BARS];
56 bool bar_mmap_supported[PCI_STD_NUM_BARS];
59 struct perm_bits *msi_perm;
62 struct vfio_pci_irq_ctx *ctx;
66 struct vfio_pci_region *region;
80 bool needs_pm_restore;
82 bool pm_runtime_engaged;
83 struct pci_saved_state *pci_saved_state;
84 struct pci_saved_state *pm_save;
86 struct eventfd_ctx *err_trigger;
87 struct eventfd_ctx *req_trigger;
88 struct eventfd_ctx *pm_wake_eventfd_ctx;
89 struct list_head dummy_resources_list;
90 struct mutex ioeventfds_lock;
91 struct list_head ioeventfds_list;
92 struct vfio_pci_vf_token *vf_token;
93 struct list_head sriov_pfs_item;
94 struct vfio_pci_core_device *sriov_pf_core_dev;
95 struct notifier_block nb;
96 struct mutex vma_lock;
97 struct list_head vma_list;
98 struct rw_semaphore memory_lock;
101 /* Will be exported for vfio pci drivers usage */
102 int vfio_pci_core_register_dev_region(struct vfio_pci_core_device *vdev,
103 unsigned int type, unsigned int subtype,
104 const struct vfio_pci_regops *ops,
105 size_t size, u32 flags, void *data);
106 void vfio_pci_core_set_params(bool nointxmask, bool is_disable_vga,
107 bool is_disable_idle_d3);
108 void vfio_pci_core_close_device(struct vfio_device *core_vdev);
109 int vfio_pci_core_init_dev(struct vfio_device *core_vdev);
110 void vfio_pci_core_release_dev(struct vfio_device *core_vdev);
111 int vfio_pci_core_register_device(struct vfio_pci_core_device *vdev);
112 void vfio_pci_core_unregister_device(struct vfio_pci_core_device *vdev);
113 extern const struct pci_error_handlers vfio_pci_core_err_handlers;
114 int vfio_pci_core_sriov_configure(struct vfio_pci_core_device *vdev,
116 long vfio_pci_core_ioctl(struct vfio_device *core_vdev, unsigned int cmd,
118 int vfio_pci_core_ioctl_feature(struct vfio_device *device, u32 flags,
119 void __user *arg, size_t argsz);
120 ssize_t vfio_pci_core_read(struct vfio_device *core_vdev, char __user *buf,
121 size_t count, loff_t *ppos);
122 ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __user *buf,
123 size_t count, loff_t *ppos);
124 int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma);
125 void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int count);
126 int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf);
127 int vfio_pci_core_enable(struct vfio_pci_core_device *vdev);
128 void vfio_pci_core_disable(struct vfio_pci_core_device *vdev);
129 void vfio_pci_core_finish_enable(struct vfio_pci_core_device *vdev);
130 pci_ers_result_t vfio_pci_core_aer_err_detected(struct pci_dev *pdev,
131 pci_channel_state_t state);
133 #endif /* VFIO_PCI_CORE_H */