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 struct vfio_pci_irq_ctx {
33 struct eventfd_ctx *trigger;
34 struct virqfd *unmask;
38 struct irq_bypass_producer producer;
41 struct vfio_pci_device;
42 struct vfio_pci_region;
44 struct vfio_pci_regops {
45 size_t (*rw)(struct vfio_pci_device *vdev, char __user *buf,
46 size_t count, loff_t *ppos, bool iswrite);
47 void (*release)(struct vfio_pci_device *vdev,
48 struct vfio_pci_region *region);
51 struct vfio_pci_region {
54 const struct vfio_pci_regops *ops;
60 struct vfio_pci_dummy_resource {
61 struct resource resource;
63 struct list_head res_next;
66 struct vfio_pci_mmap_vma {
67 struct vm_area_struct *vma;
68 struct list_head vma_next;
71 struct vfio_pci_device {
73 void __iomem *barmap[PCI_STD_RESOURCE_END + 1];
74 bool bar_mmap_supported[PCI_STD_RESOURCE_END + 1];
77 struct perm_bits *msi_perm;
80 struct vfio_pci_irq_ctx *ctx;
84 struct vfio_pci_region *region;
98 struct pci_saved_state *pci_saved_state;
100 struct eventfd_ctx *err_trigger;
101 struct eventfd_ctx *req_trigger;
102 struct list_head dummy_resources_list;
103 struct mutex vma_lock;
104 struct list_head vma_list;
105 struct rw_semaphore memory_lock;
108 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
109 #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
110 #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
111 #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
112 #define irq_is(vdev, type) (vdev->irq_type == type)
114 extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
115 extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
117 extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
118 uint32_t flags, unsigned index,
119 unsigned start, unsigned count, void *data);
121 extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
122 char __user *buf, size_t count,
123 loff_t *ppos, bool iswrite);
125 extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
126 size_t count, loff_t *ppos, bool iswrite);
128 extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
129 size_t count, loff_t *ppos, bool iswrite);
131 extern int vfio_pci_init_perm_bits(void);
132 extern void vfio_pci_uninit_perm_bits(void);
134 extern int vfio_config_init(struct vfio_pci_device *vdev);
135 extern void vfio_config_free(struct vfio_pci_device *vdev);
137 extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
138 unsigned int type, unsigned int subtype,
139 const struct vfio_pci_regops *ops,
140 size_t size, u32 flags, void *data);
142 extern bool __vfio_pci_memory_enabled(struct vfio_pci_device *vdev);
143 extern void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device
145 extern u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev);
146 extern void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev,
149 #ifdef CONFIG_VFIO_PCI_IGD
150 extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
152 static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
157 #endif /* VFIO_PCI_PRIVATE_H */