blob: 987b4d311fde9981c8f1a5a512aac4106e317eda [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* 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
16#ifndef VFIO_PCI_PRIVATE_H
17#define VFIO_PCI_PRIVATE_H
18
19#define VFIO_PCI_OFFSET_SHIFT 40
20
21#define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
22#define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
23#define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
24
25/* Special capability IDs predefined access */
26#define PCI_CAP_ID_INVALID 0xFF /* default raw access */
27#define PCI_CAP_ID_INVALID_VIRT 0xFE /* default virt access */
28
29/* Cap maximum number of ioeventfds per device (arbitrary) */
30#define VFIO_PCI_IOEVENTFD_MAX 1000
31
32struct vfio_pci_ioeventfd {
33 struct list_head next;
34 struct virqfd *virqfd;
35 void __iomem *addr;
36 uint64_t data;
37 loff_t pos;
38 int bar;
39 int count;
40};
41
42struct vfio_pci_irq_ctx {
43 struct eventfd_ctx *trigger;
44 struct virqfd *unmask;
45 struct virqfd *mask;
46 char *name;
47 bool masked;
48 struct irq_bypass_producer producer;
49};
50
51struct vfio_pci_device;
52struct vfio_pci_region;
53
54struct vfio_pci_regops {
55 size_t (*rw)(struct vfio_pci_device *vdev, char __user *buf,
56 size_t count, loff_t *ppos, bool iswrite);
57 void (*release)(struct vfio_pci_device *vdev,
58 struct vfio_pci_region *region);
59 int (*mmap)(struct vfio_pci_device *vdev,
60 struct vfio_pci_region *region,
61 struct vm_area_struct *vma);
62 int (*add_capability)(struct vfio_pci_device *vdev,
63 struct vfio_pci_region *region,
64 struct vfio_info_cap *caps);
65};
66
67struct vfio_pci_region {
68 u32 type;
69 u32 subtype;
70 const struct vfio_pci_regops *ops;
71 void *data;
72 size_t size;
73 u32 flags;
74};
75
76struct vfio_pci_dummy_resource {
77 struct resource resource;
78 int index;
79 struct list_head res_next;
80};
81
82struct vfio_pci_reflck {
83 struct kref kref;
84 struct mutex lock;
85};
86
87struct vfio_pci_mmap_vma {
88 struct vm_area_struct *vma;
89 struct list_head vma_next;
90};
91
92struct vfio_pci_device {
93 struct pci_dev *pdev;
94 void __iomem *barmap[PCI_STD_RESOURCE_END + 1];
95 bool bar_mmap_supported[PCI_STD_RESOURCE_END + 1];
96 u8 *pci_config_map;
97 u8 *vconfig;
98 struct perm_bits *msi_perm;
99 spinlock_t irqlock;
100 struct mutex igate;
101 struct vfio_pci_irq_ctx *ctx;
102 int num_ctx;
103 int irq_type;
104 int num_regions;
105 struct vfio_pci_region *region;
106 u8 msi_qmax;
107 u8 msix_bar;
108 u16 msix_size;
109 u32 msix_offset;
110 u32 rbar[7];
111 bool pci_2_3;
112 bool virq_disabled;
113 bool reset_works;
114 bool extended_caps;
115 bool bardirty;
116 bool has_vga;
117 bool needs_reset;
118 bool nointx;
119 bool needs_pm_restore;
120 struct pci_saved_state *pci_saved_state;
121 struct pci_saved_state *pm_save;
122 struct vfio_pci_reflck *reflck;
123 int refcnt;
124 int ioeventfds_nr;
125 struct eventfd_ctx *err_trigger;
126 struct eventfd_ctx *req_trigger;
127 struct list_head dummy_resources_list;
128 struct mutex ioeventfds_lock;
129 struct list_head ioeventfds_list;
130 struct mutex vma_lock;
131 struct list_head vma_list;
132 struct rw_semaphore memory_lock;
133};
134
135#define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
136#define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
137#define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
138#define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
139#define irq_is(vdev, type) (vdev->irq_type == type)
140
141extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
142extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
143
144extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
145 uint32_t flags, unsigned index,
146 unsigned start, unsigned count, void *data);
147
148extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
149 char __user *buf, size_t count,
150 loff_t *ppos, bool iswrite);
151
152extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
153 size_t count, loff_t *ppos, bool iswrite);
154
155extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
156 size_t count, loff_t *ppos, bool iswrite);
157
158extern long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
159 uint64_t data, int count, int fd);
160
161extern int vfio_pci_init_perm_bits(void);
162extern void vfio_pci_uninit_perm_bits(void);
163
164extern int vfio_config_init(struct vfio_pci_device *vdev);
165extern void vfio_config_free(struct vfio_pci_device *vdev);
166
167extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
168 unsigned int type, unsigned int subtype,
169 const struct vfio_pci_regops *ops,
170 size_t size, u32 flags, void *data);
171
172extern int vfio_pci_set_power_state(struct vfio_pci_device *vdev,
173 pci_power_t state);
174
175extern bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev);
176extern void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device
177 *vdev);
178extern u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev);
179extern void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev,
180 u16 cmd);
181
182#ifdef CONFIG_VFIO_PCI_IGD
183extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
184#else
185static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
186{
187 return -ENODEV;
188}
189#endif
190#ifdef CONFIG_VFIO_PCI_NVLINK2
191extern int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev);
192extern int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev);
193#else
194static inline int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev)
195{
196 return -ENODEV;
197}
198
199static inline int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev)
200{
201 return -ENODEV;
202}
203#endif
204#endif /* VFIO_PCI_PRIVATE_H */