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 acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi,
24 int trigger, int polarity)
27 struct physdev_map_pirq map_irq;
31 if (!xen_hvm_domain())
34 map_irq.domid = DOMID_SELF;
35 map_irq.type = MAP_PIRQ_TYPE_GSI;
39 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
41 printk(KERN_WARNING "xen map irq failed %d\n", rc);
45 if (trigger == ACPI_EDGE_SENSITIVE) {
50 name = "ioapic-level";
53 irq = xen_bind_pirq_gsi_to_irq(gsi, map_irq.pirq, shareable, name);
55 printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq);
61 #if defined(CONFIG_PCI_MSI)
62 #include <linux/msi.h>
63 #include <asm/msidef.h>
65 struct xen_pci_frontend_ops *xen_pci_frontend;
66 EXPORT_SYMBOL_GPL(xen_pci_frontend);
68 #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \
69 MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0))
71 static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq,
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);
80 MSI_ADDR_DEST_MODE_PHYSICAL |
81 MSI_ADDR_REDIRECTION_CPU |
82 MSI_ADDR_DEST_ID(pirq);
84 msg->data = XEN_PIRQ_MSI_DATA;
87 static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
90 struct msi_desc *msidesc;
93 list_for_each_entry(msidesc, &dev->msi_list, list) {
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);
97 if (msg.data != XEN_PIRQ_MSI_DATA ||
98 xen_irq_from_pirq(pirq) < 0) {
99 pirq = xen_allocate_pirq_msi(dev, msidesc);
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);
107 "xen: msi already bound to pirq=%d\n", pirq);
109 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq, 0,
110 (type == PCI_CAP_ID_MSIX) ?
115 "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq);
121 "Xen PCI frontend has not registered MSI/MSI-X support!\n");
126 * For MSI interrupts we have to use drivers/xen/event.s functions to
127 * allocate an irq_desc and setup the right */
130 static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
133 struct msi_desc *msidesc;
136 v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL);
140 if (type == PCI_CAP_ID_MSIX)
141 ret = xen_pci_frontend_enable_msix(dev, v, nvec);
143 ret = xen_pci_frontend_enable_msi(dev, v);
147 list_for_each_entry(msidesc, &dev->msi_list, list) {
148 irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 0,
149 (type == PCI_CAP_ID_MSIX) ?
160 dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n");
166 static void xen_teardown_msi_irqs(struct pci_dev *dev)
168 struct msi_desc *msidesc;
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);
174 xen_pci_frontend_disable_msi(dev);
176 /* Free the IRQ's and the msidesc using the generic code. */
177 default_teardown_msi_irqs(dev);
180 static void xen_teardown_msi_irq(unsigned int irq)
182 xen_destroy_irq(irq);
185 #ifdef CONFIG_XEN_DOM0
186 static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
189 struct msi_desc *msidesc;
191 list_for_each_entry(msidesc, &dev->msi_list, list) {
192 struct physdev_map_pirq map_irq;
194 memset(&map_irq, 0, sizeof(map_irq));
195 map_irq.domid = DOMID_SELF;
196 map_irq.type = MAP_PIRQ_TYPE_MSI;
199 map_irq.bus = dev->bus->number;
200 map_irq.devfn = dev->devfn;
202 if (type == PCI_CAP_ID_MSIX) {
204 u32 table_offset, bir;
206 pos = pci_find_capability(dev, PCI_CAP_ID_MSIX);
208 pci_read_config_dword(dev, pos + PCI_MSIX_TABLE,
210 bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK);
212 map_irq.table_base = pci_resource_start(dev, bir);
213 map_irq.entry_nr = msidesc->msi_attrib.entry_nr;
216 ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
218 dev_warn(&dev->dev, "xen map irq failed %d\n", ret);
222 ret = xen_bind_pirq_msi_to_irq(dev, msidesc,
223 map_irq.pirq, map_irq.index,
224 (type == PCI_CAP_ID_MSIX) ?
236 static int xen_pcifront_enable_irq(struct pci_dev *dev)
243 rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi);
245 dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n",
250 rc = xen_allocate_pirq_gsi(gsi);
252 dev_warn(&dev->dev, "Xen PCI: failed to allocate a PIRQ for GSI%d: %d\n",
258 if (gsi < NR_IRQS_LEGACY)
261 rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront");
263 dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n",
269 dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq);
273 int __init pci_xen_init(void)
275 if (!xen_pv_domain() || xen_initial_domain())
278 printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n");
280 pcibios_set_cache_line_size();
282 pcibios_enable_irq = xen_pcifront_enable_irq;
283 pcibios_disable_irq = NULL;
286 /* Keep ACPI out of the picture */
290 #ifdef CONFIG_PCI_MSI
291 x86_msi.setup_msi_irqs = xen_setup_msi_irqs;
292 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
293 x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs;
298 int __init pci_xen_hvm_init(void)
300 if (!xen_feature(XENFEAT_hvm_pirqs))
305 * We don't want to change the actual ACPI delivery model,
306 * just how GSIs get registered.
308 __acpi_register_gsi = acpi_register_gsi_xen_hvm;
311 #ifdef CONFIG_PCI_MSI
312 x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs;
313 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
318 #ifdef CONFIG_XEN_DOM0
319 static int xen_register_pirq(u32 gsi, int triggering)
321 int rc, pirq, irq = -1;
322 struct physdev_map_pirq map_irq;
326 if (!xen_pv_domain())
329 if (triggering == ACPI_EDGE_SENSITIVE) {
331 name = "ioapic-edge";
334 name = "ioapic-level";
337 pirq = xen_allocate_pirq_gsi(gsi);
341 irq = xen_bind_pirq_gsi_to_irq(gsi, pirq, shareable, name);
345 printk(KERN_DEBUG "xen: --> pirq=%d -> irq=%d\n", pirq, irq);
347 map_irq.domid = DOMID_SELF;
348 map_irq.type = MAP_PIRQ_TYPE_GSI;
352 rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq);
354 printk(KERN_WARNING "xen map irq failed %d\n", rc);
362 static int xen_register_gsi(u32 gsi, int triggering, int polarity)
365 struct physdev_setup_gsi setup_gsi;
367 if (!xen_pv_domain())
370 printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n",
371 gsi, triggering, polarity);
373 irq = xen_register_pirq(gsi, triggering);
376 setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1);
377 setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1);
379 rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi);
381 printk(KERN_INFO "Already setup the GSI :%d\n", gsi);
383 printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n",
390 static __init void xen_setup_acpi_sci(void)
393 int trigger, polarity;
394 int gsi = acpi_sci_override_gsi;
399 rc = acpi_get_override_irq(gsi, &trigger, &polarity);
401 printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi"
402 " sci, rc=%d\n", rc);
405 trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE;
406 polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH;
408 printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d "
409 "polarity=%d\n", gsi, trigger, polarity);
411 gsi = xen_register_gsi(gsi, trigger, polarity);
412 printk(KERN_INFO "xen: acpi sci %d\n", gsi);
417 static int acpi_register_gsi_xen(struct device *dev, u32 gsi,
418 int trigger, int polarity)
420 return xen_register_gsi(gsi, trigger, polarity);
423 static int __init pci_xen_initial_domain(void)
425 #ifdef CONFIG_PCI_MSI
426 x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs;
427 x86_msi.teardown_msi_irq = xen_teardown_msi_irq;
429 xen_setup_acpi_sci();
430 __acpi_register_gsi = acpi_register_gsi_xen;
435 void __init xen_setup_pirqs(void)
439 pci_xen_initial_domain();
441 if (0 == nr_ioapics) {
442 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
443 pirq = xen_allocate_pirq_gsi(irq);
445 "Could not allocate PIRQ for legacy interrupt\n"))
447 irq = xen_bind_pirq_gsi_to_irq(irq, pirq, 0, "xt-pic");
452 /* Pre-allocate legacy irqs */
453 for (irq = 0; irq < NR_IRQS_LEGACY; irq++) {
454 int trigger, polarity;
456 if (acpi_get_override_irq(irq, &trigger, &polarity) == -1)
459 xen_register_pirq(irq,
460 trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE);