vfio-pci: Fault mmaps to enable vma tracking
[linux-block.git] / drivers / vfio / pci / vfio_pci_private.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
89e1f7d4
AW
2/*
3 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
4 * Author: Alex Williamson <alex.williamson@redhat.com>
5 *
89e1f7d4
AW
6 * Derived from original vfio:
7 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
8 * Author: Tom Lyon, pugs@cisco.com
9 */
10
11#include <linux/mutex.h>
12#include <linux/pci.h>
6d7425f1 13#include <linux/irqbypass.h>
28541d41 14#include <linux/types.h>
cc20d799 15#include <linux/uuid.h>
137e5531 16#include <linux/notifier.h>
89e1f7d4
AW
17
18#ifndef VFIO_PCI_PRIVATE_H
19#define VFIO_PCI_PRIVATE_H
20
21#define VFIO_PCI_OFFSET_SHIFT 40
22
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)
26
345d7104
AW
27/* Special capability IDs predefined access */
28#define PCI_CAP_ID_INVALID 0xFF /* default raw access */
29#define PCI_CAP_ID_INVALID_VIRT 0xFE /* default virt access */
30
30656177
AW
31/* Cap maximum number of ioeventfds per device (arbitrary) */
32#define VFIO_PCI_IOEVENTFD_MAX 1000
33
34struct vfio_pci_ioeventfd {
35 struct list_head next;
36 struct virqfd *virqfd;
37 void __iomem *addr;
38 uint64_t data;
39 loff_t pos;
40 int bar;
41 int count;
42};
43
89e1f7d4
AW
44struct vfio_pci_irq_ctx {
45 struct eventfd_ctx *trigger;
46 struct virqfd *unmask;
47 struct virqfd *mask;
48 char *name;
49 bool masked;
6d7425f1 50 struct irq_bypass_producer producer;
89e1f7d4
AW
51};
52
28541d41
AW
53struct vfio_pci_device;
54struct vfio_pci_region;
55
56struct vfio_pci_regops {
57 size_t (*rw)(struct vfio_pci_device *vdev, char __user *buf,
58 size_t count, loff_t *ppos, bool iswrite);
59 void (*release)(struct vfio_pci_device *vdev,
60 struct vfio_pci_region *region);
a15b1883
AK
61 int (*mmap)(struct vfio_pci_device *vdev,
62 struct vfio_pci_region *region,
63 struct vm_area_struct *vma);
c2c0f1cd
AK
64 int (*add_capability)(struct vfio_pci_device *vdev,
65 struct vfio_pci_region *region,
66 struct vfio_info_cap *caps);
28541d41
AW
67};
68
69struct vfio_pci_region {
70 u32 type;
71 u32 subtype;
72 const struct vfio_pci_regops *ops;
73 void *data;
74 size_t size;
75 u32 flags;
76};
77
05f0c03f
YX
78struct vfio_pci_dummy_resource {
79 struct resource resource;
80 int index;
81 struct list_head res_next;
82};
83
e309df5b
AW
84struct vfio_pci_reflck {
85 struct kref kref;
86 struct mutex lock;
87};
88
cc20d799
AW
89struct vfio_pci_vf_token {
90 struct mutex lock;
91 uuid_t uuid;
92 int users;
93};
94
11c4cd07
AW
95struct vfio_pci_mmap_vma {
96 struct vm_area_struct *vma;
97 struct list_head vma_next;
98};
99
89e1f7d4
AW
100struct vfio_pci_device {
101 struct pci_dev *pdev;
c9c13ba4
DE
102 void __iomem *barmap[PCI_STD_NUM_BARS];
103 bool bar_mmap_supported[PCI_STD_NUM_BARS];
89e1f7d4
AW
104 u8 *pci_config_map;
105 u8 *vconfig;
106 struct perm_bits *msi_perm;
107 spinlock_t irqlock;
108 struct mutex igate;
89e1f7d4
AW
109 struct vfio_pci_irq_ctx *ctx;
110 int num_ctx;
111 int irq_type;
28541d41
AW
112 int num_regions;
113 struct vfio_pci_region *region;
89e1f7d4
AW
114 u8 msi_qmax;
115 u8 msix_bar;
116 u16 msix_size;
117 u32 msix_offset;
118 u32 rbar[7];
119 bool pci_2_3;
120 bool virq_disabled;
121 bool reset_works;
122 bool extended_caps;
123 bool bardirty;
84237a82 124 bool has_vga;
bc4fba77 125 bool needs_reset;
45074405 126 bool nointx;
51ef3a00 127 bool needs_pm_restore;
89e1f7d4 128 struct pci_saved_state *pci_saved_state;
51ef3a00 129 struct pci_saved_state *pm_save;
e309df5b 130 struct vfio_pci_reflck *reflck;
61d79256 131 int refcnt;
30656177 132 int ioeventfds_nr;
dad9f897 133 struct eventfd_ctx *err_trigger;
6140a8f5 134 struct eventfd_ctx *req_trigger;
05f0c03f 135 struct list_head dummy_resources_list;
30656177
AW
136 struct mutex ioeventfds_lock;
137 struct list_head ioeventfds_list;
cc20d799 138 struct vfio_pci_vf_token *vf_token;
137e5531 139 struct notifier_block nb;
11c4cd07
AW
140 struct mutex vma_lock;
141 struct list_head vma_list;
89e1f7d4
AW
142};
143
144#define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
145#define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
146#define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
147#define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
148#define irq_is(vdev, type) (vdev->irq_type == type)
149
150extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
151extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
152
153extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
154 uint32_t flags, unsigned index,
155 unsigned start, unsigned count, void *data);
156
906ee99d
AW
157extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
158 char __user *buf, size_t count,
159 loff_t *ppos, bool iswrite);
160
161extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
162 size_t count, loff_t *ppos, bool iswrite);
89e1f7d4 163
84237a82
AW
164extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
165 size_t count, loff_t *ppos, bool iswrite);
166
30656177
AW
167extern long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
168 uint64_t data, int count, int fd);
169
89e1f7d4
AW
170extern int vfio_pci_init_perm_bits(void);
171extern void vfio_pci_uninit_perm_bits(void);
172
89e1f7d4
AW
173extern int vfio_config_init(struct vfio_pci_device *vdev);
174extern void vfio_config_free(struct vfio_pci_device *vdev);
28541d41
AW
175
176extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
177 unsigned int type, unsigned int subtype,
178 const struct vfio_pci_regops *ops,
179 size_t size, u32 flags, void *data);
51ef3a00
AW
180
181extern int vfio_pci_set_power_state(struct vfio_pci_device *vdev,
182 pci_power_t state);
183
5846ff54 184#ifdef CONFIG_VFIO_PCI_IGD
f572a960 185extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
5846ff54 186#else
f572a960 187static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
5846ff54
AW
188{
189 return -ENODEV;
190}
191#endif
7f928917
AK
192#ifdef CONFIG_VFIO_PCI_NVLINK2
193extern int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev);
194extern int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev);
195#else
196static inline int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev)
197{
198 return -ENODEV;
199}
200
201static inline int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev)
202{
203 return -ENODEV;
204}
205#endif
89e1f7d4 206#endif /* VFIO_PCI_PRIVATE_H */