1 /*
2  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
3  *     Author: Alex Williamson <alex.williamson@redhat.com>
4  *
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.
8  *
9  * Derived from original vfio:
10  * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
11  * Author: Tom Lyon, pugs@cisco.com
12  */
13 
14 #include <linux/mutex.h>
15 #include <linux/pci.h>
16 #include <linux/irqbypass.h>
17 #include <linux/types.h>
18 
19 #ifndef VFIO_PCI_PRIVATE_H
20 #define VFIO_PCI_PRIVATE_H
21 
22 #define VFIO_PCI_OFFSET_SHIFT   40
23 
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)
27 
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 */
31 
32 /* Cap maximum number of ioeventfds per device (arbitrary) */
33 #define VFIO_PCI_IOEVENTFD_MAX		1000
34 
35 struct vfio_pci_ioeventfd {
36 	struct list_head	next;
37 	struct virqfd		*virqfd;
38 	void __iomem		*addr;
39 	uint64_t		data;
40 	loff_t			pos;
41 	int			bar;
42 	int			count;
43 };
44 
45 struct vfio_pci_irq_ctx {
46 	struct eventfd_ctx	*trigger;
47 	struct virqfd		*unmask;
48 	struct virqfd		*mask;
49 	char			*name;
50 	bool			masked;
51 	struct irq_bypass_producer	producer;
52 };
53 
54 struct vfio_pci_device;
55 struct vfio_pci_region;
56 
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 };
63 
64 struct vfio_pci_region {
65 	u32				type;
66 	u32				subtype;
67 	const struct vfio_pci_regops	*ops;
68 	void				*data;
69 	size_t				size;
70 	u32				flags;
71 };
72 
73 struct vfio_pci_dummy_resource {
74 	struct resource		resource;
75 	int			index;
76 	struct list_head	res_next;
77 };
78 
79 struct vfio_pci_device {
80 	struct pci_dev		*pdev;
81 	void __iomem		*barmap[PCI_STD_RESOURCE_END + 1];
82 	bool			bar_mmap_supported[PCI_STD_RESOURCE_END + 1];
83 	u8			*pci_config_map;
84 	u8			*vconfig;
85 	struct perm_bits	*msi_perm;
86 	spinlock_t		irqlock;
87 	struct mutex		igate;
88 	struct vfio_pci_irq_ctx	*ctx;
89 	int			num_ctx;
90 	int			irq_type;
91 	int			num_regions;
92 	struct vfio_pci_region	*region;
93 	u8			msi_qmax;
94 	u8			msix_bar;
95 	u16			msix_size;
96 	u32			msix_offset;
97 	u32			rbar[7];
98 	bool			pci_2_3;
99 	bool			virq_disabled;
100 	bool			reset_works;
101 	bool			extended_caps;
102 	bool			bardirty;
103 	bool			has_vga;
104 	bool			needs_reset;
105 	bool			nointx;
106 	struct pci_saved_state	*pci_saved_state;
107 	int			refcnt;
108 	int			ioeventfds_nr;
109 	struct eventfd_ctx	*err_trigger;
110 	struct eventfd_ctx	*req_trigger;
111 	struct list_head	dummy_resources_list;
112 	struct mutex		ioeventfds_lock;
113 	struct list_head	ioeventfds_list;
114 };
115 
116 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
117 #define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
118 #define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
119 #define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
120 #define irq_is(vdev, type) (vdev->irq_type == type)
121 
122 extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
123 extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
124 
125 extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
126 				   uint32_t flags, unsigned index,
127 				   unsigned start, unsigned count, void *data);
128 
129 extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
130 				  char __user *buf, size_t count,
131 				  loff_t *ppos, bool iswrite);
132 
133 extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
134 			       size_t count, loff_t *ppos, bool iswrite);
135 
136 extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
137 			       size_t count, loff_t *ppos, bool iswrite);
138 
139 extern long vfio_pci_ioeventfd(struct vfio_pci_device *vdev, loff_t offset,
140 			       uint64_t data, int count, int fd);
141 
142 extern int vfio_pci_init_perm_bits(void);
143 extern void vfio_pci_uninit_perm_bits(void);
144 
145 extern int vfio_config_init(struct vfio_pci_device *vdev);
146 extern void vfio_config_free(struct vfio_pci_device *vdev);
147 
148 extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
149 					unsigned int type, unsigned int subtype,
150 					const struct vfio_pci_regops *ops,
151 					size_t size, u32 flags, void *data);
152 #ifdef CONFIG_VFIO_PCI_IGD
153 extern int vfio_pci_igd_init(struct vfio_pci_device *vdev);
154 #else
vfio_pci_igd_init(struct vfio_pci_device * vdev)155 static inline int vfio_pci_igd_init(struct vfio_pci_device *vdev)
156 {
157 	return -ENODEV;
158 }
159 #endif
160 #endif /* VFIO_PCI_PRIVATE_H */
161