2 * Xen PCI Frontend Stub - puts some "dummy" functions in to the Linux
3 * x86 PCI core to support the Xen PCI Frontend
5 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
7 #include <linux/module.h>
8 #include <linux/init.h>
10 #include <linux/acpi.h>
13 #include <asm/io_apic.h>
14 #include <asm/pci_x86.h>
16 #include <asm/xen/hypervisor.h>
18 #include <xen/features.h>
19 #include <xen/events.h>
20 #include <asm/xen/pci.h>
23 static int xen_hvm_register_pirq(u32 gsi, int triggering)
26 struct physdev_map_pirq map_irq;
30 if (!xen_hvm_domain())
33 map_irq.domid = DOMID_SELF;
34 map_irq.type = MAP_PIRQ_TYPE_GSI;
38 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
40 printk(KERN_WARNING "xen map irq failed %d\n", rc);
44 if (triggering == ACPI_EDGE_SENSITIVE) {
49 name = "ioapic-level";
52 irq = xen_map_pirq_gsi(map_irq.pirq, gsi, shareable, name);
54 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
59 static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
60 int trigger, int polarity)
62 return xen_hvm_register_pirq(gsi, trigger);
66 #if defined(CONFIG_PCI_MSI)
67 #include <linux/msi.h>
68 #include <asm/msidef.h>
70 struct xen_pci_frontend_ops *xen_pci_frontend;
71 EXPORT_SYMBOL_GPL(xen_pci_frontend);
73 #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
74 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
76 static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
79 /* We set vector == 0 to tell the hypervisor we don't care about it,
80 * but we want a pirq setup instead.
81 * We use the dest_id field to pass the pirq that we want. */
82 msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq);
85 MSI_ADDR_DEST_MODE_PHYSICAL |
86 MSI_ADDR_REDIRECTION_CPU |
87 MSI_ADDR_DEST_ID(pirq);
89 msg->data = XEN_PIRQ_MSI_DATA;
92 static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
94 int irq, pirq, ret = 0;
95 struct msi_desc *msidesc;
98 list_for_each_entry(msidesc, &dev->msi_list, list) {
99 __read_msi_msg(msidesc, &msg);
100 pirq = MSI_ADDR_EXT_DEST_ID(msg.address_hi) |
101 ((msg.address_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xff);
102 if (xen_irq_from_pirq(pirq) >= 0 && msg.data == XEN_PIRQ_MSI_DATA) {
103 xen_allocate_pirq_msi((type == PCI_CAP_ID_MSIX) ?
104 "msi-x" : "msi", &irq, &pirq, XEN_ALLOC_IRQ);
107 ret = set_irq_msi(irq, msidesc);
110 printk(KERN_DEBUG "xen: msi already setup: msi --> irq=%d"
111 " pirq=%d\n", irq, pirq);
114 xen_allocate_pirq_msi((type == PCI_CAP_ID_MSIX) ?
115 "msi-x" : "msi", &irq, &pirq, (XEN_ALLOC_IRQ | XEN_ALLOC_PIRQ));
116 if (irq < 0 || pirq < 0)
118 printk(KERN_DEBUG "xen: msi --> irq=%d, pirq=%d\n", irq, pirq);
119 xen_msi_compose_msg(dev, pirq, &msg);
120 ret = set_irq_msi(irq, msidesc);
123 write_msi_msg(irq, &msg);
128 unbind_from_irqhandler(irq, NULL);
131 dev_err(&dev->dev, "Xen PCI frontend has not registered" \
132 " MSI/MSI-X support!\n");
138 * For MSI interrupts we have to use drivers/xen/event.s functions to
139 * allocate an irq_desc and setup the right */
142 static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
145 struct msi_desc *msidesc;
148 v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
152 if (type == PCI_CAP_ID_MSIX)
153 ret = xen_pci_frontend_enable_msix(dev, v, nvec);
155 ret = xen_pci_frontend_enable_msi(dev, v);
159 list_for_each_entry(msidesc, &dev->msi_list, list) {
160 xen_allocate_pirq_msi(
161 (type == PCI_CAP_ID_MSIX) ?
162 "pcifront-msi-x" : "pcifront-msi",
163 &irq, &v[i], XEN_ALLOC_IRQ);
168 ret = set_irq_msi(irq, msidesc);
177 unbind_from_irqhandler(irq, NULL);
180 dev_err(&dev->dev, "Xen PCI frontend has not registered" \
181 " MSI/MSI-X support!\n");
187 static void xen_teardown_msi_irqs(struct pci_dev *dev)
189 struct msi_desc *msidesc;
191 msidesc = list_entry(dev->msi_list.next, struct msi_desc, list);
192 if (msidesc->msi_attrib.is_msix)
193 xen_pci_frontend_disable_msix(dev);
195 xen_pci_frontend_disable_msi(dev);
197 /* Free the IRQ's and the msidesc using the generic code. */
198 default_teardown_msi_irqs(dev);
201 static void xen_teardown_msi_irq(unsigned int irq)
203 xen_destroy_irq(irq);
206 #ifdef CONFIG_XEN_DOM0
207 static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
210 struct msi_desc *msidesc;
212 list_for_each_entry(msidesc, &dev->msi_list, list) {
213 irq = xen_create_msi_irq(dev, msidesc, type);
217 ret = set_irq_msi(irq, msidesc);
224 xen_destroy_irq(irq);
230 static int xen_pcifront_enable_irq(struct pci_dev *dev)
236 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
238 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
243 if (gsi < NR_IRQS_LEGACY)
246 rc = xen_allocate_pirq(gsi, share, "pcifront");
248 dev_warn(&dev->dev, "Xen PCI: failed to register GSI%d: %d\n",
254 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
258 int __init pci_xen_init(void)
260 if (!xen_pv_domain() || xen_initial_domain())
263 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
265 pcibios_set_cache_line_size();
267 pcibios_enable_irq = xen_pcifront_enable_irq;
268 pcibios_disable_irq = NULL;
271 /* Keep ACPI out of the picture */
275 #ifdef CONFIG_PCI_MSI
276 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
277 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
278 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
283 int __init pci_xen_hvm_init(void)
285 if (!xen_feature(XENFEAT_hvm_pirqs))
290 * We don't want to change the actual ACPI delivery model,
291 * just how GSIs get registered.
293 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
296 #ifdef CONFIG_PCI_MSI
297 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
298 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
303 #ifdef CONFIG_XEN_DOM0
304 static int xen_register_pirq(u32 gsi, int triggering)
307 struct physdev_map_pirq map_irq;
311 if (!xen_pv_domain())
314 if (triggering == ACPI_EDGE_SENSITIVE) {
316 name = "ioapic-edge";
319 name = "ioapic-level";
322 irq = xen_allocate_pirq(gsi, shareable, name);
324 printk(KERN_DEBUG "xen: --> irq=%d\n", irq);
329 map_irq.domid = DOMID_SELF;
330 map_irq.type = MAP_PIRQ_TYPE_GSI;
334 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
336 printk(KERN_WARNING "xen map irq failed %d\n", rc);
344 static int xen_register_gsi(u32 gsi, int triggering, int polarity)
347 struct physdev_setup_gsi setup_gsi;
349 if (!xen_pv_domain())
352 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
353 gsi, triggering, polarity);
355 irq = xen_register_pirq(gsi, triggering);
358 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
359 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
361 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
363 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
365 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
372 static __init void xen_setup_acpi_sci(void)
375 int trigger, polarity;
376 int gsi = acpi_sci_override_gsi;
381 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
383 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
384 " sci, rc=%d\n", rc);
387 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
388 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
390 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
391 "polarity=%d\n", gsi, trigger, polarity);
393 gsi = xen_register_gsi(gsi, trigger, polarity);
394 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
399 static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
400 int trigger, int polarity)
402 return xen_register_gsi(gsi, trigger, polarity);
405 static int __init pci_xen_initial_domain(void)
407 #ifdef CONFIG_PCI_MSI
408 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
409 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
411 xen_setup_acpi_sci();
412 __acpi_register_gsi = acpi_register_gsi_xen;
417 void __init xen_setup_pirqs(void)
421 pci_xen_initial_domain();
423 if (0 == nr_ioapics) {
424 for (irq = 0; irq < NR_IRQS_LEGACY; irq++)
425 xen_allocate_pirq(irq, 0, "xt-pic");
429 /* Pre-allocate legacy irqs */
430 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
431 int trigger, polarity;
433 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
436 xen_register_pirq(irq,
437 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);