2 * Copyright (C) 2012 Red Hat, Inc. All rights reserved.
3 * Author: Alex Williamson <alex.williamson@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Derived from original vfio:
10 * Copyright 2010 Cisco Systems, Inc. All rights reserved.
11 * Author: Tom Lyon, pugs@cisco.com
14 #include <linux/mutex.h>
15 #include <linux/pci.h>
16 #include <linux/irqbypass.h>
17 #include <linux/types.h>
19 #ifndef VFIO_PCI_PRIVATE_H
20 #define VFIO_PCI_PRIVATE_H
22 #define VFIO_PCI_OFFSET_SHIFT 40
24 #define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
25 #define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
26 #define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
28 /* Special capability IDs predefined access */
29 #define PCI_CAP_ID_INVALID 0xFF /* default raw access */
30 #define PCI_CAP_ID_INVALID_VIRT 0xFE /* default virt access */
32 /* Cap maximum number of ioeventfds per device (arbitrary) */
33 #define VFIO_PCI_IOEVENTFD_MAX 1000
35 struct vfio_pci_ioeventfd {
36 struct list_head next;
37 struct virqfd *virqfd;
45 struct vfio_pci_irq_ctx {
46 struct eventfd_ctx *trigger;
47 struct virqfd *unmask;
51 struct irq_bypass_producer producer;
54 struct vfio_pci_device;
55 struct vfio_pci_region;
57 struct vfio_pci_regops {
58 size_t (*rw)(struct vfio_pci_device *vdev, char __user *buf,
59 size_t count, loff_t *ppos, bool iswrite);
60 void (*release)(struct vfio_pci_device *vdev,
61 struct vfio_pci_region *region);
62 int (*mmap)(struct vfio_pci_device *vdev,
63 struct vfio_pci_region *region,
64 struct vm_area_struct *vma);
65 int (*add_capability)(struct vfio_pci_device *vdev,
66 struct vfio_pci_region *region,
67 struct vfio_info_cap *caps);
70 struct vfio_pci_region {
73 const struct vfio_pci_regops *ops;
79 struct vfio_pci_dummy_resource {
80 struct resource resource;
82 struct list_head res_next;
85 struct vfio_pci_device {
87 void __iomem *barmap[PCI_STD_RESOURCE_END + 1];
88 bool bar_mmap_supported[PCI_STD_RESOURCE_END + 1];
91 struct perm_bits *msi_perm;
94 struct vfio_pci_irq_ctx *ctx;
98 struct vfio_pci_region *region;
112 struct pci_saved_state *pci_saved_state;
115 struct eventfd_ctx *err_trigger;
116 struct eventfd_ctx *req_trigger;
117 struct list_head dummy_resources_list;
118 struct mutex ioeventfds_lock;
119 struct list_head ioeventfds_list;
122 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
123 #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
124 #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
125 #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
126 #define irq_is(vdev, type) (vdev->irq_type == type)
128 extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
129 extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
131 extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
132 uint32_t flags, unsigned index,
133 unsigned start, unsigned count, void *data);
135 extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
136 char __user *buf, size_t count,
137 loff_t *ppos, bool iswrite);
139 extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
140 size_t count, loff_t *ppos, bool iswrite);
142 extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
143 size_t count, loff_t *ppos, bool iswrite);
145 extern long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
146 uint64_t data, int count, int fd);
148 extern int vfio_pci_init_perm_bits(void);
149 extern void vfio_pci_uninit_perm_bits(void);
151 extern int vfio_config_init(struct vfio_pci_device *vdev);
152 extern void vfio_config_free(struct vfio_pci_device *vdev);
154 extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
155 unsigned int type, unsigned int subtype,
156 const struct vfio_pci_regops *ops,
157 size_t size, u32 flags, void *data);
158 #ifdef CONFIG_VFIO_PCI_IGD
159 extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
161 static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
166 #ifdef CONFIG_VFIO_PCI_NVLINK2
167 extern int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev);
168 extern int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev);
170 static inline int vfio_pci_nvdia_v100_nvlink2_init(struct vfio_pci_device *vdev)
175 static inline int vfio_pci_ibm_npu2_init(struct vfio_pci_device *vdev)
180 #endif /* VFIO_PCI_PRIVATE_H */