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> | |
0e058e52 | 13 | #include <asm/io_apic.h> |
b5401a96 AN |
14 | #include <asm/pci_x86.h> |
15 | ||
16 | #include <asm/xen/hypervisor.h> | |
17 | ||
3942b740 | 18 | #include <xen/features.h> |
b5401a96 AN |
19 | #include <xen/events.h> |
20 | #include <asm/xen/pci.h> | |
21 | ||
42a1de56 | 22 | #ifdef CONFIG_ACPI |
9a626612 IC |
23 | static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi, |
24 | int trigger, int polarity) | |
42a1de56 SS |
25 | { |
26 | int rc, irq; | |
27 | struct physdev_map_pirq map_irq; | |
28 | int shareable = 0; | |
29 | char *name; | |
30 | ||
31 | if (!xen_hvm_domain()) | |
32 | return -1; | |
33 | ||
34 | map_irq.domid = DOMID_SELF; | |
35 | map_irq.type = MAP_PIRQ_TYPE_GSI; | |
36 | map_irq.index = gsi; | |
37 | map_irq.pirq = -1; | |
38 | ||
39 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); | |
40 | if (rc) { | |
41 | printk(KERN_WARNING "xen map irq failed %d\n", rc); | |
42 | return -1; | |
43 | } | |
44 | ||
9a626612 | 45 | if (trigger == ACPI_EDGE_SENSITIVE) { |
42a1de56 SS |
46 | shareable = 0; |
47 | name = "ioapic-edge"; | |
48 | } else { | |
49 | shareable = 1; | |
50 | name = "ioapic-level"; | |
51 | } | |
52 | ||
53 | irq = xen_map_pirq_gsi(map_irq.pirq, gsi, shareable, name); | |
54 | ||
55 | printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq); | |
56 | ||
57 | return irq; | |
58 | } | |
59 | #endif | |
60 | ||
b5401a96 AN |
61 | #if defined(CONFIG_PCI_MSI) |
62 | #include <linux/msi.h> | |
809f9267 | 63 | #include <asm/msidef.h> |
b5401a96 AN |
64 | |
65 | struct xen_pci_frontend_ops *xen_pci_frontend; | |
66 | EXPORT_SYMBOL_GPL(xen_pci_frontend); | |
67 | ||
af42b8d1 SS |
68 | #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \ |
69 | MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0)) | |
70 | ||
809f9267 SS |
71 | static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq, |
72 | struct msi_msg *msg) | |
73 | { | |
74 | /* We set vector == 0 to tell the hypervisor we don't care about it, | |
75 | * but we want a pirq setup instead. | |
76 | * We use the dest_id field to pass the pirq that we want. */ | |
77 | msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq); | |
78 | msg->address_lo = | |
79 | MSI_ADDR_BASE_LO | | |
80 | MSI_ADDR_DEST_MODE_PHYSICAL | | |
81 | MSI_ADDR_REDIRECTION_CPU | | |
82 | MSI_ADDR_DEST_ID(pirq); | |
83 | ||
af42b8d1 | 84 | msg->data = XEN_PIRQ_MSI_DATA; |
809f9267 SS |
85 | } |
86 | ||
87 | static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) | |
88 | { | |
bf480d95 | 89 | int irq, pirq; |
809f9267 SS |
90 | struct msi_desc *msidesc; |
91 | struct msi_msg msg; | |
92 | ||
93 | list_for_each_entry(msidesc, &dev->msi_list, list) { | |
af42b8d1 SS |
94 | __read_msi_msg(msidesc, &msg); |
95 | pirq = MSI_ADDR_EXT_DEST_ID(msg.address_hi) | | |
96 | ((msg.address_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xff); | |
bf480d95 IC |
97 | if (msg.data != XEN_PIRQ_MSI_DATA || |
98 | xen_irq_from_pirq(pirq) < 0) { | |
99 | pirq = xen_allocate_pirq_msi(dev, msidesc); | |
100 | if (pirq < 0) | |
af42b8d1 | 101 | goto error; |
bf480d95 IC |
102 | xen_msi_compose_msg(dev, pirq, &msg); |
103 | __write_msi_msg(msidesc, &msg); | |
104 | dev_dbg(&dev->dev, "xen: msi bound to pirq=%d\n", pirq); | |
105 | } else { | |
106 | dev_dbg(&dev->dev, | |
107 | "xen: msi already bound to pirq=%d\n", pirq); | |
af42b8d1 | 108 | } |
ca1d8fe9 | 109 | irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq, 0, |
bf480d95 IC |
110 | (type == PCI_CAP_ID_MSIX) ? |
111 | "msi-x" : "msi"); | |
112 | if (irq < 0) | |
809f9267 | 113 | goto error; |
bf480d95 IC |
114 | dev_dbg(&dev->dev, |
115 | "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq); | |
809f9267 SS |
116 | } |
117 | return 0; | |
118 | ||
809f9267 | 119 | error: |
bf480d95 IC |
120 | dev_err(&dev->dev, |
121 | "Xen PCI frontend has not registered MSI/MSI-X support!\n"); | |
122 | return -ENODEV; | |
809f9267 SS |
123 | } |
124 | ||
b5401a96 AN |
125 | /* |
126 | * For MSI interrupts we have to use drivers/xen/event.s functions to | |
127 | * allocate an irq_desc and setup the right */ | |
128 | ||
129 | ||
130 | static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) | |
131 | { | |
132 | int irq, ret, i; | |
133 | struct msi_desc *msidesc; | |
134 | int *v; | |
135 | ||
136 | v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL); | |
137 | if (!v) | |
138 | return -ENOMEM; | |
139 | ||
f731e3ef | 140 | if (type == PCI_CAP_ID_MSIX) |
cc0f89c4 | 141 | ret = xen_pci_frontend_enable_msix(dev, v, nvec); |
f731e3ef | 142 | else |
cc0f89c4 | 143 | ret = xen_pci_frontend_enable_msi(dev, v); |
f731e3ef QH |
144 | if (ret) |
145 | goto error; | |
b5401a96 AN |
146 | i = 0; |
147 | list_for_each_entry(msidesc, &dev->msi_list, list) { | |
ca1d8fe9 | 148 | irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 0, |
bf480d95 IC |
149 | (type == PCI_CAP_ID_MSIX) ? |
150 | "pcifront-msi-x" : | |
151 | "pcifront-msi"); | |
152 | if (irq < 0) | |
07cf2a64 | 153 | goto free; |
b5401a96 AN |
154 | i++; |
155 | } | |
156 | kfree(v); | |
157 | return 0; | |
158 | ||
b5401a96 | 159 | error: |
bf480d95 | 160 | dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n"); |
07cf2a64 | 161 | free: |
b5401a96 AN |
162 | kfree(v); |
163 | return ret; | |
164 | } | |
165 | ||
166 | static void xen_teardown_msi_irqs(struct pci_dev *dev) | |
167 | { | |
f731e3ef | 168 | struct msi_desc *msidesc; |
b5401a96 | 169 | |
f731e3ef QH |
170 | msidesc = list_entry(dev->msi_list.next, struct msi_desc, list); |
171 | if (msidesc->msi_attrib.is_msix) | |
172 | xen_pci_frontend_disable_msix(dev); | |
173 | else | |
174 | xen_pci_frontend_disable_msi(dev); | |
3d74a539 KRW |
175 | |
176 | /* Free the IRQ's and the msidesc using the generic code. */ | |
177 | default_teardown_msi_irqs(dev); | |
b5401a96 AN |
178 | } |
179 | ||
180 | static void xen_teardown_msi_irq(unsigned int irq) | |
181 | { | |
182 | xen_destroy_irq(irq); | |
183 | } | |
f731e3ef | 184 | |
260a7d4c | 185 | #ifdef CONFIG_XEN_DOM0 |
f731e3ef QH |
186 | static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
187 | { | |
f420e010 | 188 | int irq; |
f731e3ef QH |
189 | struct msi_desc *msidesc; |
190 | ||
191 | list_for_each_entry(msidesc, &dev->msi_list, list) { | |
192 | irq = xen_create_msi_irq(dev, msidesc, type); | |
193 | if (irq < 0) | |
194 | return -1; | |
f731e3ef QH |
195 | } |
196 | return 0; | |
f731e3ef | 197 | } |
b5401a96 | 198 | #endif |
260a7d4c | 199 | #endif |
b5401a96 AN |
200 | |
201 | static int xen_pcifront_enable_irq(struct pci_dev *dev) | |
202 | { | |
203 | int rc; | |
204 | int share = 1; | |
3f2a230c | 205 | u8 gsi; |
b5401a96 | 206 | |
3f2a230c IC |
207 | rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi); |
208 | if (rc < 0) { | |
209 | dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n", | |
210 | rc); | |
211 | return rc; | |
212 | } | |
b5401a96 | 213 | |
3f2a230c | 214 | if (gsi < NR_IRQS_LEGACY) |
b5401a96 AN |
215 | share = 0; |
216 | ||
3f2a230c | 217 | rc = xen_allocate_pirq(gsi, share, "pcifront"); |
b5401a96 | 218 | if (rc < 0) { |
3f2a230c IC |
219 | dev_warn(&dev->dev, "Xen PCI: failed to register GSI%d: %d\n", |
220 | gsi, rc); | |
b5401a96 AN |
221 | return rc; |
222 | } | |
3f2a230c IC |
223 | |
224 | dev->irq = rc; | |
225 | dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq); | |
b5401a96 AN |
226 | return 0; |
227 | } | |
228 | ||
229 | int __init pci_xen_init(void) | |
230 | { | |
231 | if (!xen_pv_domain() || xen_initial_domain()) | |
232 | return -ENODEV; | |
233 | ||
234 | printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n"); | |
235 | ||
236 | pcibios_set_cache_line_size(); | |
237 | ||
238 | pcibios_enable_irq = xen_pcifront_enable_irq; | |
239 | pcibios_disable_irq = NULL; | |
240 | ||
241 | #ifdef CONFIG_ACPI | |
242 | /* Keep ACPI out of the picture */ | |
243 | acpi_noirq = 1; | |
244 | #endif | |
245 | ||
b5401a96 AN |
246 | #ifdef CONFIG_PCI_MSI |
247 | x86_msi.setup_msi_irqs = xen_setup_msi_irqs; | |
248 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; | |
249 | x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs; | |
250 | #endif | |
251 | return 0; | |
252 | } | |
3942b740 SS |
253 | |
254 | int __init pci_xen_hvm_init(void) | |
255 | { | |
256 | if (!xen_feature(XENFEAT_hvm_pirqs)) | |
257 | return 0; | |
258 | ||
259 | #ifdef CONFIG_ACPI | |
260 | /* | |
261 | * We don't want to change the actual ACPI delivery model, | |
262 | * just how GSIs get registered. | |
263 | */ | |
264 | __acpi_register_gsi = acpi_register_gsi_xen_hvm; | |
265 | #endif | |
809f9267 SS |
266 | |
267 | #ifdef CONFIG_PCI_MSI | |
268 | x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs; | |
269 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; | |
270 | #endif | |
3942b740 SS |
271 | return 0; |
272 | } | |
38aa66fc JF |
273 | |
274 | #ifdef CONFIG_XEN_DOM0 | |
275 | static int xen_register_pirq(u32 gsi, int triggering) | |
276 | { | |
277 | int rc, irq; | |
278 | struct physdev_map_pirq map_irq; | |
279 | int shareable = 0; | |
280 | char *name; | |
281 | ||
282 | if (!xen_pv_domain()) | |
283 | return -1; | |
284 | ||
285 | if (triggering == ACPI_EDGE_SENSITIVE) { | |
286 | shareable = 0; | |
287 | name = "ioapic-edge"; | |
288 | } else { | |
289 | shareable = 1; | |
290 | name = "ioapic-level"; | |
291 | } | |
292 | ||
293 | irq = xen_allocate_pirq(gsi, shareable, name); | |
294 | ||
295 | printk(KERN_DEBUG "xen: --> irq=%d\n", irq); | |
296 | ||
297 | if (irq < 0) | |
298 | goto out; | |
299 | ||
300 | map_irq.domid = DOMID_SELF; | |
301 | map_irq.type = MAP_PIRQ_TYPE_GSI; | |
302 | map_irq.index = gsi; | |
303 | map_irq.pirq = irq; | |
304 | ||
305 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); | |
306 | if (rc) { | |
307 | printk(KERN_WARNING "xen map irq failed %d\n", rc); | |
308 | return -1; | |
309 | } | |
310 | ||
311 | out: | |
312 | return irq; | |
313 | } | |
314 | ||
315 | static int xen_register_gsi(u32 gsi, int triggering, int polarity) | |
316 | { | |
317 | int rc, irq; | |
318 | struct physdev_setup_gsi setup_gsi; | |
319 | ||
320 | if (!xen_pv_domain()) | |
321 | return -1; | |
322 | ||
323 | printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n", | |
324 | gsi, triggering, polarity); | |
325 | ||
326 | irq = xen_register_pirq(gsi, triggering); | |
327 | ||
328 | setup_gsi.gsi = gsi; | |
329 | setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1); | |
330 | setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1); | |
331 | ||
332 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi); | |
333 | if (rc == -EEXIST) | |
334 | printk(KERN_INFO "Already setup the GSI :%d\n", gsi); | |
335 | else if (rc) { | |
336 | printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n", | |
337 | gsi, rc); | |
338 | } | |
339 | ||
340 | return irq; | |
341 | } | |
342 | ||
343 | static __init void xen_setup_acpi_sci(void) | |
344 | { | |
345 | int rc; | |
346 | int trigger, polarity; | |
347 | int gsi = acpi_sci_override_gsi; | |
348 | ||
349 | if (!gsi) | |
350 | return; | |
351 | ||
352 | rc = acpi_get_override_irq(gsi, &trigger, &polarity); | |
353 | if (rc) { | |
354 | printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi" | |
355 | " sci, rc=%d\n", rc); | |
356 | return; | |
357 | } | |
358 | trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; | |
359 | polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; | |
360 | ||
361 | printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d " | |
362 | "polarity=%d\n", gsi, trigger, polarity); | |
363 | ||
364 | gsi = xen_register_gsi(gsi, trigger, polarity); | |
365 | printk(KERN_INFO "xen: acpi sci %d\n", gsi); | |
366 | ||
367 | return; | |
368 | } | |
369 | ||
370 | static int acpi_register_gsi_xen(struct device *dev, u32 gsi, | |
371 | int trigger, int polarity) | |
372 | { | |
373 | return xen_register_gsi(gsi, trigger, polarity); | |
374 | } | |
375 | ||
376 | static int __init pci_xen_initial_domain(void) | |
377 | { | |
f731e3ef QH |
378 | #ifdef CONFIG_PCI_MSI |
379 | x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs; | |
380 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; | |
381 | #endif | |
38aa66fc JF |
382 | xen_setup_acpi_sci(); |
383 | __acpi_register_gsi = acpi_register_gsi_xen; | |
384 | ||
385 | return 0; | |
386 | } | |
387 | ||
388 | void __init xen_setup_pirqs(void) | |
389 | { | |
390 | int irq; | |
391 | ||
392 | pci_xen_initial_domain(); | |
393 | ||
394 | if (0 == nr_ioapics) { | |
395 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) | |
396 | xen_allocate_pirq(irq, 0, "xt-pic"); | |
397 | return; | |
398 | } | |
399 | ||
400 | /* Pre-allocate legacy irqs */ | |
401 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) { | |
402 | int trigger, polarity; | |
403 | ||
404 | if (acpi_get_override_irq(irq, &trigger, &polarity) == -1) | |
405 | continue; | |
406 | ||
407 | xen_register_pirq(irq, | |
408 | trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE); | |
409 | } | |
410 | } | |
411 | #endif |