Commit | Line | Data |
---|---|---|
b5401a96 AN |
1 | /* |
2 | * Xen PCI Frontend Stub - puts some "dummy" functions in to the Linux | |
3 | * x86 PCI core to support the Xen PCI Frontend | |
4 | * | |
5 | * Author: Ryan Wilson <hap9@epoch.ncsc.mil> | |
6 | */ | |
7 | #include <linux/module.h> | |
8 | #include <linux/init.h> | |
9 | #include <linux/pci.h> | |
10 | #include <linux/acpi.h> | |
11 | ||
12 | #include <linux/io.h> | |
13 | #include <asm/pci_x86.h> | |
14 | ||
15 | #include <asm/xen/hypervisor.h> | |
16 | ||
3942b740 | 17 | #include <xen/features.h> |
b5401a96 AN |
18 | #include <xen/events.h> |
19 | #include <asm/xen/pci.h> | |
20 | ||
42a1de56 SS |
21 | #ifdef CONFIG_ACPI |
22 | static int xen_hvm_register_pirq(u32 gsi, int triggering) | |
23 | { | |
24 | int rc, irq; | |
25 | struct physdev_map_pirq map_irq; | |
26 | int shareable = 0; | |
27 | char *name; | |
28 | ||
29 | if (!xen_hvm_domain()) | |
30 | return -1; | |
31 | ||
32 | map_irq.domid = DOMID_SELF; | |
33 | map_irq.type = MAP_PIRQ_TYPE_GSI; | |
34 | map_irq.index = gsi; | |
35 | map_irq.pirq = -1; | |
36 | ||
37 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); | |
38 | if (rc) { | |
39 | printk(KERN_WARNING "xen map irq failed %d\n", rc); | |
40 | return -1; | |
41 | } | |
42 | ||
43 | if (triggering == ACPI_EDGE_SENSITIVE) { | |
44 | shareable = 0; | |
45 | name = "ioapic-edge"; | |
46 | } else { | |
47 | shareable = 1; | |
48 | name = "ioapic-level"; | |
49 | } | |
50 | ||
51 | irq = xen_map_pirq_gsi(map_irq.pirq, gsi, shareable, name); | |
52 | ||
53 | printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq); | |
54 | ||
55 | return irq; | |
56 | } | |
90f6881e JF |
57 | |
58 | static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi, | |
59 | int trigger, int polarity) | |
60 | { | |
61 | return xen_hvm_register_pirq(gsi, trigger); | |
62 | } | |
42a1de56 SS |
63 | #endif |
64 | ||
b5401a96 AN |
65 | #if defined(CONFIG_PCI_MSI) |
66 | #include <linux/msi.h> | |
809f9267 | 67 | #include <asm/msidef.h> |
b5401a96 AN |
68 | |
69 | struct xen_pci_frontend_ops *xen_pci_frontend; | |
70 | EXPORT_SYMBOL_GPL(xen_pci_frontend); | |
71 | ||
809f9267 SS |
72 | static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq, |
73 | struct msi_msg *msg) | |
74 | { | |
75 | /* We set vector == 0 to tell the hypervisor we don't care about it, | |
76 | * but we want a pirq setup instead. | |
77 | * We use the dest_id field to pass the pirq that we want. */ | |
78 | msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq); | |
79 | msg->address_lo = | |
80 | MSI_ADDR_BASE_LO | | |
81 | MSI_ADDR_DEST_MODE_PHYSICAL | | |
82 | MSI_ADDR_REDIRECTION_CPU | | |
83 | MSI_ADDR_DEST_ID(pirq); | |
84 | ||
85 | msg->data = | |
86 | MSI_DATA_TRIGGER_EDGE | | |
87 | MSI_DATA_LEVEL_ASSERT | | |
88 | /* delivery mode reserved */ | |
89 | (3 << 8) | | |
90 | MSI_DATA_VECTOR(0); | |
91 | } | |
92 | ||
93 | static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) | |
94 | { | |
95 | int irq, pirq, ret = 0; | |
96 | struct msi_desc *msidesc; | |
97 | struct msi_msg msg; | |
98 | ||
99 | list_for_each_entry(msidesc, &dev->msi_list, list) { | |
100 | xen_allocate_pirq_msi((type == PCI_CAP_ID_MSIX) ? | |
101 | "msi-x" : "msi", &irq, &pirq); | |
102 | if (irq < 0 || pirq < 0) | |
103 | goto error; | |
104 | printk(KERN_DEBUG "xen: msi --> irq=%d, pirq=%d\n", irq, pirq); | |
105 | xen_msi_compose_msg(dev, pirq, &msg); | |
106 | ret = set_irq_msi(irq, msidesc); | |
107 | if (ret < 0) | |
108 | goto error_while; | |
109 | write_msi_msg(irq, &msg); | |
110 | } | |
111 | return 0; | |
112 | ||
113 | error_while: | |
114 | unbind_from_irqhandler(irq, NULL); | |
115 | error: | |
116 | if (ret == -ENODEV) | |
117 | dev_err(&dev->dev, "Xen PCI frontend has not registered" \ | |
118 | " MSI/MSI-X support!\n"); | |
119 | ||
120 | return ret; | |
121 | } | |
122 | ||
b5401a96 AN |
123 | /* |
124 | * For MSI interrupts we have to use drivers/xen/event.s functions to | |
125 | * allocate an irq_desc and setup the right */ | |
126 | ||
127 | ||
128 | static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) | |
129 | { | |
130 | int irq, ret, i; | |
131 | struct msi_desc *msidesc; | |
132 | int *v; | |
133 | ||
134 | v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL); | |
135 | if (!v) | |
136 | return -ENOMEM; | |
137 | ||
138 | if (!xen_initial_domain()) { | |
139 | if (type == PCI_CAP_ID_MSIX) | |
140 | ret = xen_pci_frontend_enable_msix(dev, &v, nvec); | |
141 | else | |
142 | ret = xen_pci_frontend_enable_msi(dev, &v); | |
143 | if (ret) | |
144 | goto error; | |
145 | } | |
146 | i = 0; | |
147 | list_for_each_entry(msidesc, &dev->msi_list, list) { | |
148 | irq = xen_allocate_pirq(v[i], 0, /* not sharable */ | |
149 | (type == PCI_CAP_ID_MSIX) ? | |
150 | "pcifront-msi-x" : "pcifront-msi"); | |
151 | if (irq < 0) | |
152 | return -1; | |
153 | ||
154 | ret = set_irq_msi(irq, msidesc); | |
155 | if (ret) | |
156 | goto error_while; | |
157 | i++; | |
158 | } | |
159 | kfree(v); | |
160 | return 0; | |
161 | ||
162 | error_while: | |
163 | unbind_from_irqhandler(irq, NULL); | |
164 | error: | |
165 | if (ret == -ENODEV) | |
166 | dev_err(&dev->dev, "Xen PCI frontend has not registered" \ | |
167 | " MSI/MSI-X support!\n"); | |
168 | ||
169 | kfree(v); | |
170 | return ret; | |
171 | } | |
172 | ||
173 | static void xen_teardown_msi_irqs(struct pci_dev *dev) | |
174 | { | |
175 | /* Only do this when were are in non-privileged mode.*/ | |
176 | if (!xen_initial_domain()) { | |
177 | struct msi_desc *msidesc; | |
178 | ||
179 | msidesc = list_entry(dev->msi_list.next, struct msi_desc, list); | |
180 | if (msidesc->msi_attrib.is_msix) | |
181 | xen_pci_frontend_disable_msix(dev); | |
182 | else | |
183 | xen_pci_frontend_disable_msi(dev); | |
184 | } | |
185 | ||
186 | } | |
187 | ||
188 | static void xen_teardown_msi_irq(unsigned int irq) | |
189 | { | |
190 | xen_destroy_irq(irq); | |
191 | } | |
192 | #endif | |
193 | ||
194 | static int xen_pcifront_enable_irq(struct pci_dev *dev) | |
195 | { | |
196 | int rc; | |
197 | int share = 1; | |
198 | ||
199 | dev_info(&dev->dev, "Xen PCI enabling IRQ: %d\n", dev->irq); | |
200 | ||
201 | if (dev->irq < 0) | |
202 | return -EINVAL; | |
203 | ||
204 | if (dev->irq < NR_IRQS_LEGACY) | |
205 | share = 0; | |
206 | ||
207 | rc = xen_allocate_pirq(dev->irq, share, "pcifront"); | |
208 | if (rc < 0) { | |
209 | dev_warn(&dev->dev, "Xen PCI IRQ: %d, failed to register:%d\n", | |
210 | dev->irq, rc); | |
211 | return rc; | |
212 | } | |
213 | return 0; | |
214 | } | |
215 | ||
216 | int __init pci_xen_init(void) | |
217 | { | |
218 | if (!xen_pv_domain() || xen_initial_domain()) | |
219 | return -ENODEV; | |
220 | ||
221 | printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n"); | |
222 | ||
223 | pcibios_set_cache_line_size(); | |
224 | ||
225 | pcibios_enable_irq = xen_pcifront_enable_irq; | |
226 | pcibios_disable_irq = NULL; | |
227 | ||
228 | #ifdef CONFIG_ACPI | |
229 | /* Keep ACPI out of the picture */ | |
230 | acpi_noirq = 1; | |
231 | #endif | |
232 | ||
b5401a96 AN |
233 | #ifdef CONFIG_PCI_MSI |
234 | x86_msi.setup_msi_irqs = xen_setup_msi_irqs; | |
235 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; | |
236 | x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs; | |
237 | #endif | |
238 | return 0; | |
239 | } | |
3942b740 SS |
240 | |
241 | int __init pci_xen_hvm_init(void) | |
242 | { | |
243 | if (!xen_feature(XENFEAT_hvm_pirqs)) | |
244 | return 0; | |
245 | ||
246 | #ifdef CONFIG_ACPI | |
247 | /* | |
248 | * We don't want to change the actual ACPI delivery model, | |
249 | * just how GSIs get registered. | |
250 | */ | |
251 | __acpi_register_gsi = acpi_register_gsi_xen_hvm; | |
252 | #endif | |
809f9267 SS |
253 | |
254 | #ifdef CONFIG_PCI_MSI | |
255 | x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs; | |
256 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; | |
257 | #endif | |
3942b740 SS |
258 | return 0; |
259 | } |