]> Git Repo - linux.git/blob - drivers/vfio/pci/vfio_pci_private.h
drm/vram-helper: stop using TTM placement flags
[linux.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 <[email protected]>
5  *
6  * Derived from original vfio:
7  * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
8  * Author: Tom Lyon, [email protected]
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 virqfd           *virqfd;
37         void __iomem            *addr;
38         uint64_t                data;
39         loff_t                  pos;
40         int                     bar;
41         int                     count;
42 };
43
44 struct vfio_pci_irq_ctx {
45         struct eventfd_ctx      *trigger;
46         struct virqfd           *unmask;
47         struct virqfd           *mask;
48         char                    *name;
49         bool                    masked;
50         struct irq_bypass_producer      producer;
51 };
52
53 struct vfio_pci_device;
54 struct vfio_pci_region;
55
56 struct 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);
61         int     (*mmap)(struct vfio_pci_device *vdev,
62                         struct vfio_pci_region *region,
63                         struct vm_area_struct *vma);
64         int     (*add_capability)(struct vfio_pci_device *vdev,
65                                   struct vfio_pci_region *region,
66                                   struct vfio_info_cap *caps);
67 };
68
69 struct 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
78 struct vfio_pci_dummy_resource {
79         struct resource         resource;
80         int                     index;
81         struct list_head        res_next;
82 };
83
84 struct vfio_pci_reflck {
85         struct kref             kref;
86         struct mutex            lock;
87 };
88
89 struct vfio_pci_vf_token {
90         struct mutex            lock;
91         uuid_t                  uuid;
92         int                     users;
93 };
94
95 struct vfio_pci_mmap_vma {
96         struct vm_area_struct   *vma;
97         struct list_head        vma_next;
98 };
99
100 struct vfio_pci_device {
101         struct pci_dev          *pdev;
102         void __iomem            *barmap[PCI_STD_NUM_BARS];
103         bool                    bar_mmap_supported[PCI_STD_NUM_BARS];
104         u8                      *pci_config_map;
105         u8                      *vconfig;
106         struct perm_bits        *msi_perm;
107         spinlock_t              irqlock;
108         struct mutex            igate;
109         struct vfio_pci_irq_ctx *ctx;
110         int                     num_ctx;
111         int                     irq_type;
112         int                     num_regions;
113         struct vfio_pci_region  *region;
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;
124         bool                    has_vga;
125         bool                    needs_reset;
126         bool                    nointx;
127         bool                    needs_pm_restore;
128         struct pci_saved_state  *pci_saved_state;
129         struct pci_saved_state  *pm_save;
130         struct vfio_pci_reflck  *reflck;
131         int                     refcnt;
132         int                     ioeventfds_nr;
133         struct eventfd_ctx      *err_trigger;
134         struct eventfd_ctx      *req_trigger;
135         struct list_head        dummy_resources_list;
136         struct mutex            ioeventfds_lock;
137         struct list_head        ioeventfds_list;
138         struct vfio_pci_vf_token        *vf_token;
139         struct notifier_block   nb;
140         struct mutex            vma_lock;
141         struct list_head        vma_list;
142         struct rw_semaphore     memory_lock;
143 };
144
145 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
146 #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
147 #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
148 #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
149 #define irq_is(vdev, type) (vdev->irq_type == type)
150
151 extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
152 extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
153
154 extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
155                                    uint32_t flags, unsigned index,
156                                    unsigned start, unsigned count, void *data);
157
158 extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
159                                   char __user *buf, size_t count,
160                                   loff_t *ppos, bool iswrite);
161
162 extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
163                                size_t count, loff_t *ppos, bool iswrite);
164
165 extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
166                                size_t count, loff_t *ppos, bool iswrite);
167
168 extern long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
169                                uint64_t data, int count, int fd);
170
171 extern int vfio_pci_init_perm_bits(void);
172 extern void vfio_pci_uninit_perm_bits(void);
173
174 extern int vfio_config_init(struct vfio_pci_device *vdev);
175 extern void vfio_config_free(struct vfio_pci_device *vdev);
176
177 extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
178                                         unsigned int type, unsigned int subtype,
179                                         const struct vfio_pci_regops *ops,
180                                         size_t size, u32 flags, void *data);
181
182 extern int vfio_pci_set_power_state(struct vfio_pci_device *vdev,
183                                     pci_power_t state);
184
185 extern bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev);
186 extern void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device
187                                                     *vdev);
188 extern u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev);
189 extern void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev,
190                                                u16 cmd);
191
192 #ifdef CONFIG_VFIO_PCI_IGD
193 extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
194 #else
195 static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
196 {
197         return -ENODEV;
198 }
199 #endif
200 #ifdef CONFIG_VFIO_PCI_NVLINK2
201 extern int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev);
202 extern int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev);
203 #else
204 static inline int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev)
205 {
206         return -ENODEV;
207 }
208
209 static inline int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev)
210 {
211         return -ENODEV;
212 }
213 #endif
214 #endif /* VFIO_PCI_PRIVATE_H */
This page took 0.045464 seconds and 4 git commands to generate.