Merge tag 'xfs-5.13-merge-5' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[linux-2.6-microblaze.git] / drivers / vfio / pci / vfio_pci_private.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
4  *     Author: Alex Williamson <alex.williamson@redhat.com>
5  *
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>
13 #include <linux/irqbypass.h>
14 #include <linux/types.h>
15 #include <linux/uuid.h>
16 #include <linux/notifier.h>
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
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
31 /* Cap maximum number of ioeventfds per device (arbitrary) */
32 #define VFIO_PCI_IOEVENTFD_MAX          1000
33
34 struct vfio_pci_ioeventfd {
35         struct list_head        next;
36         struct vfio_pci_device  *vdev;
37         struct virqfd           *virqfd;
38         void __iomem            *addr;
39         uint64_t                data;
40         loff_t                  pos;
41         int                     bar;
42         int                     count;
43         bool                    test_mem;
44 };
45
46 struct vfio_pci_irq_ctx {
47         struct eventfd_ctx      *trigger;
48         struct virqfd           *unmask;
49         struct virqfd           *mask;
50         char                    *name;
51         bool                    masked;
52         struct irq_bypass_producer      producer;
53 };
54
55 struct vfio_pci_device;
56 struct vfio_pci_region;
57
58 struct vfio_pci_regops {
59         size_t  (*rw)(struct vfio_pci_device *vdev, char __user *buf,
60                       size_t count, loff_t *ppos, bool iswrite);
61         void    (*release)(struct vfio_pci_device *vdev,
62                            struct vfio_pci_region *region);
63         int     (*mmap)(struct vfio_pci_device *vdev,
64                         struct vfio_pci_region *region,
65                         struct vm_area_struct *vma);
66         int     (*add_capability)(struct vfio_pci_device *vdev,
67                                   struct vfio_pci_region *region,
68                                   struct vfio_info_cap *caps);
69 };
70
71 struct vfio_pci_region {
72         u32                             type;
73         u32                             subtype;
74         const struct vfio_pci_regops    *ops;
75         void                            *data;
76         size_t                          size;
77         u32                             flags;
78 };
79
80 struct vfio_pci_dummy_resource {
81         struct resource         resource;
82         int                     index;
83         struct list_head        res_next;
84 };
85
86 struct vfio_pci_reflck {
87         struct kref             kref;
88         struct mutex            lock;
89 };
90
91 struct vfio_pci_vf_token {
92         struct mutex            lock;
93         uuid_t                  uuid;
94         int                     users;
95 };
96
97 struct vfio_pci_mmap_vma {
98         struct vm_area_struct   *vma;
99         struct list_head        vma_next;
100 };
101
102 struct vfio_pci_device {
103         struct vfio_device      vdev;
104         struct pci_dev          *pdev;
105         void __iomem            *barmap[PCI_STD_NUM_BARS];
106         bool                    bar_mmap_supported[PCI_STD_NUM_BARS];
107         u8                      *pci_config_map;
108         u8                      *vconfig;
109         struct perm_bits        *msi_perm;
110         spinlock_t              irqlock;
111         struct mutex            igate;
112         struct vfio_pci_irq_ctx *ctx;
113         int                     num_ctx;
114         int                     irq_type;
115         int                     num_regions;
116         struct vfio_pci_region  *region;
117         u8                      msi_qmax;
118         u8                      msix_bar;
119         u16                     msix_size;
120         u32                     msix_offset;
121         u32                     rbar[7];
122         bool                    pci_2_3;
123         bool                    virq_disabled;
124         bool                    reset_works;
125         bool                    extended_caps;
126         bool                    bardirty;
127         bool                    has_vga;
128         bool                    needs_reset;
129         bool                    nointx;
130         bool                    needs_pm_restore;
131         struct pci_saved_state  *pci_saved_state;
132         struct pci_saved_state  *pm_save;
133         struct vfio_pci_reflck  *reflck;
134         int                     refcnt;
135         int                     ioeventfds_nr;
136         struct eventfd_ctx      *err_trigger;
137         struct eventfd_ctx      *req_trigger;
138         struct list_head        dummy_resources_list;
139         struct mutex            ioeventfds_lock;
140         struct list_head        ioeventfds_list;
141         struct vfio_pci_vf_token        *vf_token;
142         struct notifier_block   nb;
143         struct mutex            vma_lock;
144         struct list_head        vma_list;
145         struct rw_semaphore     memory_lock;
146 };
147
148 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
149 #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
150 #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
151 #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
152 #define irq_is(vdev, type) (vdev->irq_type == type)
153
154 extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
155 extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
156
157 extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
158                                    uint32_t flags, unsigned index,
159                                    unsigned start, unsigned count, void *data);
160
161 extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
162                                   char __user *buf, size_t count,
163                                   loff_t *ppos, bool iswrite);
164
165 extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
166                                size_t count, loff_t *ppos, bool iswrite);
167
168 extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
169                                size_t count, loff_t *ppos, bool iswrite);
170
171 extern long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
172                                uint64_t data, int count, int fd);
173
174 extern int vfio_pci_init_perm_bits(void);
175 extern void vfio_pci_uninit_perm_bits(void);
176
177 extern int vfio_config_init(struct vfio_pci_device *vdev);
178 extern void vfio_config_free(struct vfio_pci_device *vdev);
179
180 extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
181                                         unsigned int type, unsigned int subtype,
182                                         const struct vfio_pci_regops *ops,
183                                         size_t size, u32 flags, void *data);
184
185 extern int vfio_pci_set_power_state(struct vfio_pci_device *vdev,
186                                     pci_power_t state);
187
188 extern bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev);
189 extern void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device
190                                                     *vdev);
191 extern u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev);
192 extern void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev,
193                                                u16 cmd);
194
195 #ifdef CONFIG_VFIO_PCI_IGD
196 extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
197 #else
198 static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
199 {
200         return -ENODEV;
201 }
202 #endif
203
204 #ifdef CONFIG_S390
205 extern int vfio_pci_info_zdev_add_caps(struct vfio_pci_device *vdev,
206                                        struct vfio_info_cap *caps);
207 #else
208 static inline int vfio_pci_info_zdev_add_caps(struct vfio_pci_device *vdev,
209                                               struct vfio_info_cap *caps)
210 {
211         return -ENODEV;
212 }
213 #endif
214
215 #endif /* VFIO_PCI_PRIVATE_H */