Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 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> |
Stefano Stabellini | 0e058e5 | 2010-10-21 17:40:08 +0100 | [diff] [blame] | 13 | #include <asm/io_apic.h> |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 14 | #include <asm/pci_x86.h> |
| 15 | |
| 16 | #include <asm/xen/hypervisor.h> |
| 17 | |
Stefano Stabellini | 3942b74 | 2010-06-24 17:50:18 +0100 | [diff] [blame] | 18 | #include <xen/features.h> |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 19 | #include <xen/events.h> |
| 20 | #include <asm/xen/pci.h> |
| 21 | |
Stefano Stabellini | 42a1de5 | 2010-06-24 16:42:04 +0100 | [diff] [blame] | 22 | #ifdef CONFIG_ACPI |
Ian Campbell | 9a62661 | 2011-02-18 16:43:30 +0000 | [diff] [blame] | 23 | static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi, |
| 24 | int trigger, int polarity) |
Stefano Stabellini | 42a1de5 | 2010-06-24 16:42:04 +0100 | [diff] [blame] | 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 | |
Ian Campbell | 9a62661 | 2011-02-18 16:43:30 +0000 | [diff] [blame] | 45 | if (trigger == ACPI_EDGE_SENSITIVE) { |
Stefano Stabellini | 42a1de5 | 2010-06-24 16:42:04 +0100 | [diff] [blame] | 46 | shareable = 0; |
| 47 | name = "ioapic-edge"; |
| 48 | } else { |
| 49 | shareable = 1; |
| 50 | name = "ioapic-level"; |
| 51 | } |
| 52 | |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 53 | irq = xen_bind_pirq_gsi_to_irq(gsi, map_irq.pirq, shareable, name); |
Stefano Stabellini | 42a1de5 | 2010-06-24 16:42:04 +0100 | [diff] [blame] | 54 | |
| 55 | printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq); |
| 56 | |
| 57 | return irq; |
| 58 | } |
| 59 | #endif |
| 60 | |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 61 | #if defined(CONFIG_PCI_MSI) |
| 62 | #include <linux/msi.h> |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 63 | #include <asm/msidef.h> |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 64 | |
| 65 | struct xen_pci_frontend_ops *xen_pci_frontend; |
| 66 | EXPORT_SYMBOL_GPL(xen_pci_frontend); |
| 67 | |
Stefano Stabellini | af42b8d | 2010-12-01 14:51:44 +0000 | [diff] [blame] | 68 | #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \ |
| 69 | MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0)) |
| 70 | |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 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 | |
Stefano Stabellini | af42b8d | 2010-12-01 14:51:44 +0000 | [diff] [blame] | 84 | msg->data = XEN_PIRQ_MSI_DATA; |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 88 | { |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 89 | int irq, pirq; |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 90 | struct msi_desc *msidesc; |
| 91 | struct msi_msg msg; |
| 92 | |
| 93 | list_for_each_entry(msidesc, &dev->msi_list, list) { |
Stefano Stabellini | af42b8d | 2010-12-01 14:51:44 +0000 | [diff] [blame] | 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); |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 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) |
Stefano Stabellini | af42b8d | 2010-12-01 14:51:44 +0000 | [diff] [blame] | 101 | goto error; |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 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); |
Stefano Stabellini | af42b8d | 2010-12-01 14:51:44 +0000 | [diff] [blame] | 108 | } |
Ian Campbell | ca1d8fe | 2011-02-18 16:43:36 +0000 | [diff] [blame] | 109 | irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq, 0, |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 110 | (type == PCI_CAP_ID_MSIX) ? |
| 111 | "msi-x" : "msi"); |
| 112 | if (irq < 0) |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 113 | goto error; |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 114 | dev_dbg(&dev->dev, |
| 115 | "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq); |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 116 | } |
| 117 | return 0; |
| 118 | |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 119 | error: |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 120 | dev_err(&dev->dev, |
| 121 | "Xen PCI frontend has not registered MSI/MSI-X support!\n"); |
| 122 | return -ENODEV; |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 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 | |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 140 | if (type == PCI_CAP_ID_MSIX) |
Konrad Rzeszutek Wilk | cc0f89c | 2011-02-17 12:02:23 -0500 | [diff] [blame] | 141 | ret = xen_pci_frontend_enable_msix(dev, v, nvec); |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 142 | else |
Konrad Rzeszutek Wilk | cc0f89c | 2011-02-17 12:02:23 -0500 | [diff] [blame] | 143 | ret = xen_pci_frontend_enable_msi(dev, v); |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 144 | if (ret) |
| 145 | goto error; |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 146 | i = 0; |
| 147 | list_for_each_entry(msidesc, &dev->msi_list, list) { |
Ian Campbell | ca1d8fe | 2011-02-18 16:43:36 +0000 | [diff] [blame] | 148 | irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 0, |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 149 | (type == PCI_CAP_ID_MSIX) ? |
| 150 | "pcifront-msi-x" : |
| 151 | "pcifront-msi"); |
| 152 | if (irq < 0) |
Jiri Slaby | 07cf2a6 | 2010-11-06 10:06:49 +0100 | [diff] [blame] | 153 | goto free; |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 154 | i++; |
| 155 | } |
| 156 | kfree(v); |
| 157 | return 0; |
| 158 | |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 159 | error: |
Ian Campbell | bf480d9 | 2011-02-18 16:43:32 +0000 | [diff] [blame] | 160 | dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n"); |
Jiri Slaby | 07cf2a6 | 2010-11-06 10:06:49 +0100 | [diff] [blame] | 161 | free: |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 162 | kfree(v); |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | static void xen_teardown_msi_irqs(struct pci_dev *dev) |
| 167 | { |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 168 | struct msi_desc *msidesc; |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 169 | |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 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); |
Konrad Rzeszutek Wilk | 3d74a53 | 2011-02-17 16:12:51 -0500 | [diff] [blame] | 175 | |
| 176 | /* Free the IRQ's and the msidesc using the generic code. */ |
| 177 | default_teardown_msi_irqs(dev); |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static void xen_teardown_msi_irq(unsigned int irq) |
| 181 | { |
| 182 | xen_destroy_irq(irq); |
| 183 | } |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 184 | |
Ian Campbell | 260a7d4 | 2011-02-18 16:43:26 +0000 | [diff] [blame] | 185 | #ifdef CONFIG_XEN_DOM0 |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 186 | static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 187 | { |
Ian Campbell | 71eef7d | 2011-02-18 17:06:55 +0000 | [diff] [blame] | 188 | int ret = 0; |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 189 | struct msi_desc *msidesc; |
| 190 | |
| 191 | list_for_each_entry(msidesc, &dev->msi_list, list) { |
Ian Campbell | 71eef7d | 2011-02-18 17:06:55 +0000 | [diff] [blame] | 192 | struct physdev_map_pirq map_irq; |
| 193 | |
| 194 | memset(&map_irq, 0, sizeof(map_irq)); |
| 195 | map_irq.domid = DOMID_SELF; |
| 196 | map_irq.type = MAP_PIRQ_TYPE_MSI; |
| 197 | map_irq.index = -1; |
| 198 | map_irq.pirq = -1; |
| 199 | map_irq.bus = dev->bus->number; |
| 200 | map_irq.devfn = dev->devfn; |
| 201 | |
| 202 | if (type == PCI_CAP_ID_MSIX) { |
| 203 | int pos; |
| 204 | u32 table_offset, bir; |
| 205 | |
| 206 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 207 | |
| 208 | pci_read_config_dword(dev, pos + PCI_MSIX_TABLE, |
| 209 | &table_offset); |
| 210 | bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); |
| 211 | |
| 212 | map_irq.table_base = pci_resource_start(dev, bir); |
| 213 | map_irq.entry_nr = msidesc->msi_attrib.entry_nr; |
| 214 | } |
| 215 | |
| 216 | ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); |
| 217 | if (ret) { |
| 218 | dev_warn(&dev->dev, "xen map irq failed %d\n", ret); |
| 219 | goto out; |
| 220 | } |
| 221 | |
| 222 | ret = xen_bind_pirq_msi_to_irq(dev, msidesc, |
| 223 | map_irq.pirq, map_irq.index, |
| 224 | (type == PCI_CAP_ID_MSIX) ? |
| 225 | "msi-x" : "msi"); |
| 226 | if (ret < 0) |
| 227 | goto out; |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 228 | } |
Ian Campbell | 71eef7d | 2011-02-18 17:06:55 +0000 | [diff] [blame] | 229 | ret = 0; |
| 230 | out: |
| 231 | return ret; |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 232 | } |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 233 | #endif |
Ian Campbell | 260a7d4 | 2011-02-18 16:43:26 +0000 | [diff] [blame] | 234 | #endif |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 235 | |
| 236 | static int xen_pcifront_enable_irq(struct pci_dev *dev) |
| 237 | { |
| 238 | int rc; |
| 239 | int share = 1; |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 240 | int pirq; |
Ian Campbell | 3f2a230 | 2011-01-11 17:20:13 +0000 | [diff] [blame] | 241 | u8 gsi; |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 242 | |
Ian Campbell | 3f2a230 | 2011-01-11 17:20:13 +0000 | [diff] [blame] | 243 | rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi); |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 244 | if (rc < 0) { |
Ian Campbell | 3f2a230 | 2011-01-11 17:20:13 +0000 | [diff] [blame] | 245 | dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n", |
| 246 | rc); |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 247 | return rc; |
| 248 | } |
Ian Campbell | 3f2a230 | 2011-01-11 17:20:13 +0000 | [diff] [blame] | 249 | |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 250 | rc = xen_allocate_pirq_gsi(gsi); |
| 251 | if (rc < 0) { |
| 252 | dev_warn(&dev->dev, "Xen PCI: failed to allocate a PIRQ for GSI%d: %d\n", |
| 253 | gsi, rc); |
| 254 | return rc; |
| 255 | } |
| 256 | pirq = rc; |
| 257 | |
Ian Campbell | 3f2a230 | 2011-01-11 17:20:13 +0000 | [diff] [blame] | 258 | if (gsi < NR_IRQS_LEGACY) |
| 259 | share = 0; |
| 260 | |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 261 | rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront"); |
Ian Campbell | 3f2a230 | 2011-01-11 17:20:13 +0000 | [diff] [blame] | 262 | if (rc < 0) { |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 263 | dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n", |
| 264 | gsi, pirq, rc); |
Ian Campbell | 3f2a230 | 2011-01-11 17:20:13 +0000 | [diff] [blame] | 265 | return rc; |
| 266 | } |
| 267 | |
| 268 | dev->irq = rc; |
| 269 | dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq); |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int __init pci_xen_init(void) |
| 274 | { |
| 275 | if (!xen_pv_domain() || xen_initial_domain()) |
| 276 | return -ENODEV; |
| 277 | |
| 278 | printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n"); |
| 279 | |
| 280 | pcibios_set_cache_line_size(); |
| 281 | |
| 282 | pcibios_enable_irq = xen_pcifront_enable_irq; |
| 283 | pcibios_disable_irq = NULL; |
| 284 | |
| 285 | #ifdef CONFIG_ACPI |
| 286 | /* Keep ACPI out of the picture */ |
| 287 | acpi_noirq = 1; |
| 288 | #endif |
| 289 | |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 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; |
| 294 | #endif |
| 295 | return 0; |
| 296 | } |
Stefano Stabellini | 3942b74 | 2010-06-24 17:50:18 +0100 | [diff] [blame] | 297 | |
| 298 | int __init pci_xen_hvm_init(void) |
| 299 | { |
| 300 | if (!xen_feature(XENFEAT_hvm_pirqs)) |
| 301 | return 0; |
| 302 | |
| 303 | #ifdef CONFIG_ACPI |
| 304 | /* |
| 305 | * We don't want to change the actual ACPI delivery model, |
| 306 | * just how GSIs get registered. |
| 307 | */ |
| 308 | __acpi_register_gsi = acpi_register_gsi_xen_hvm; |
| 309 | #endif |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 310 | |
| 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; |
| 314 | #endif |
Stefano Stabellini | 3942b74 | 2010-06-24 17:50:18 +0100 | [diff] [blame] | 315 | return 0; |
| 316 | } |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 317 | |
| 318 | #ifdef CONFIG_XEN_DOM0 |
| 319 | static int xen_register_pirq(u32 gsi, int triggering) |
| 320 | { |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 321 | int rc, pirq, irq = -1; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 322 | struct physdev_map_pirq map_irq; |
| 323 | int shareable = 0; |
| 324 | char *name; |
| 325 | |
| 326 | if (!xen_pv_domain()) |
| 327 | return -1; |
| 328 | |
| 329 | if (triggering == ACPI_EDGE_SENSITIVE) { |
| 330 | shareable = 0; |
| 331 | name = "ioapic-edge"; |
| 332 | } else { |
| 333 | shareable = 1; |
| 334 | name = "ioapic-level"; |
| 335 | } |
| 336 | |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 337 | pirq = xen_allocate_pirq_gsi(gsi); |
| 338 | if (pirq < 0) |
| 339 | goto out; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 340 | |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 341 | irq = xen_bind_pirq_gsi_to_irq(gsi, pirq, shareable, name); |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 342 | if (irq < 0) |
| 343 | goto out; |
| 344 | |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 345 | printk(KERN_DEBUG "xen: --> pirq=%d -> irq=%d\n", pirq, irq); |
| 346 | |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 347 | map_irq.domid = DOMID_SELF; |
| 348 | map_irq.type = MAP_PIRQ_TYPE_GSI; |
| 349 | map_irq.index = gsi; |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 350 | map_irq.pirq = pirq; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 351 | |
| 352 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); |
| 353 | if (rc) { |
| 354 | printk(KERN_WARNING "xen map irq failed %d\n", rc); |
| 355 | return -1; |
| 356 | } |
| 357 | |
| 358 | out: |
| 359 | return irq; |
| 360 | } |
| 361 | |
| 362 | static int xen_register_gsi(u32 gsi, int triggering, int polarity) |
| 363 | { |
| 364 | int rc, irq; |
| 365 | struct physdev_setup_gsi setup_gsi; |
| 366 | |
| 367 | if (!xen_pv_domain()) |
| 368 | return -1; |
| 369 | |
| 370 | printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n", |
| 371 | gsi, triggering, polarity); |
| 372 | |
| 373 | irq = xen_register_pirq(gsi, triggering); |
| 374 | |
| 375 | setup_gsi.gsi = gsi; |
| 376 | setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1); |
| 377 | setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1); |
| 378 | |
| 379 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi); |
| 380 | if (rc == -EEXIST) |
| 381 | printk(KERN_INFO "Already setup the GSI :%d\n", gsi); |
| 382 | else if (rc) { |
| 383 | printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n", |
| 384 | gsi, rc); |
| 385 | } |
| 386 | |
| 387 | return irq; |
| 388 | } |
| 389 | |
| 390 | static __init void xen_setup_acpi_sci(void) |
| 391 | { |
| 392 | int rc; |
| 393 | int trigger, polarity; |
| 394 | int gsi = acpi_sci_override_gsi; |
| 395 | |
| 396 | if (!gsi) |
| 397 | return; |
| 398 | |
| 399 | rc = acpi_get_override_irq(gsi, &trigger, &polarity); |
| 400 | if (rc) { |
| 401 | printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi" |
| 402 | " sci, rc=%d\n", rc); |
| 403 | return; |
| 404 | } |
| 405 | trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
| 406 | polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; |
| 407 | |
| 408 | printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d " |
| 409 | "polarity=%d\n", gsi, trigger, polarity); |
| 410 | |
| 411 | gsi = xen_register_gsi(gsi, trigger, polarity); |
| 412 | printk(KERN_INFO "xen: acpi sci %d\n", gsi); |
| 413 | |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | static int acpi_register_gsi_xen(struct device *dev, u32 gsi, |
| 418 | int trigger, int polarity) |
| 419 | { |
| 420 | return xen_register_gsi(gsi, trigger, polarity); |
| 421 | } |
| 422 | |
| 423 | static int __init pci_xen_initial_domain(void) |
| 424 | { |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 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; |
| 428 | #endif |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 429 | xen_setup_acpi_sci(); |
| 430 | __acpi_register_gsi = acpi_register_gsi_xen; |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | void __init xen_setup_pirqs(void) |
| 436 | { |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 437 | int pirq, irq; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 438 | |
| 439 | pci_xen_initial_domain(); |
| 440 | |
| 441 | if (0 == nr_ioapics) { |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 442 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) { |
| 443 | pirq = xen_allocate_pirq_gsi(irq); |
| 444 | if (WARN(pirq < 0, |
| 445 | "Could not allocate PIRQ for legacy interrupt\n")) |
| 446 | break; |
| 447 | irq = xen_bind_pirq_gsi_to_irq(irq, pirq, 0, "xt-pic"); |
| 448 | } |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 449 | return; |
| 450 | } |
| 451 | |
| 452 | /* Pre-allocate legacy irqs */ |
| 453 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) { |
| 454 | int trigger, polarity; |
| 455 | |
| 456 | if (acpi_get_override_irq(irq, &trigger, &polarity) == -1) |
| 457 | continue; |
| 458 | |
| 459 | xen_register_pirq(irq, |
| 460 | trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE); |
| 461 | } |
| 462 | } |
| 463 | #endif |
Konrad Rzeszutek Wilk | c55fa78 | 2010-11-08 14:13:35 -0500 | [diff] [blame^] | 464 | |
| 465 | struct xen_device_domain_owner { |
| 466 | domid_t domain; |
| 467 | struct pci_dev *dev; |
| 468 | struct list_head list; |
| 469 | }; |
| 470 | |
| 471 | static DEFINE_SPINLOCK(dev_domain_list_spinlock); |
| 472 | static struct list_head dev_domain_list = LIST_HEAD_INIT(dev_domain_list); |
| 473 | |
| 474 | static struct xen_device_domain_owner *find_device(struct pci_dev *dev) |
| 475 | { |
| 476 | struct xen_device_domain_owner *owner; |
| 477 | |
| 478 | list_for_each_entry(owner, &dev_domain_list, list) { |
| 479 | if (owner->dev == dev) |
| 480 | return owner; |
| 481 | } |
| 482 | return NULL; |
| 483 | } |
| 484 | |
| 485 | int xen_find_device_domain_owner(struct pci_dev *dev) |
| 486 | { |
| 487 | struct xen_device_domain_owner *owner; |
| 488 | int domain = -ENODEV; |
| 489 | |
| 490 | spin_lock(&dev_domain_list_spinlock); |
| 491 | owner = find_device(dev); |
| 492 | if (owner) |
| 493 | domain = owner->domain; |
| 494 | spin_unlock(&dev_domain_list_spinlock); |
| 495 | return domain; |
| 496 | } |
| 497 | EXPORT_SYMBOL_GPL(xen_find_device_domain_owner); |
| 498 | |
| 499 | int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain) |
| 500 | { |
| 501 | struct xen_device_domain_owner *owner; |
| 502 | |
| 503 | owner = kzalloc(sizeof(struct xen_device_domain_owner), GFP_KERNEL); |
| 504 | if (!owner) |
| 505 | return -ENODEV; |
| 506 | |
| 507 | spin_lock(&dev_domain_list_spinlock); |
| 508 | if (find_device(dev)) { |
| 509 | spin_unlock(&dev_domain_list_spinlock); |
| 510 | kfree(owner); |
| 511 | return -EEXIST; |
| 512 | } |
| 513 | owner->domain = domain; |
| 514 | owner->dev = dev; |
| 515 | list_add_tail(&owner->list, &dev_domain_list); |
| 516 | spin_unlock(&dev_domain_list_spinlock); |
| 517 | return 0; |
| 518 | } |
| 519 | EXPORT_SYMBOL_GPL(xen_register_device_domain_owner); |
| 520 | |
| 521 | int xen_unregister_device_domain_owner(struct pci_dev *dev) |
| 522 | { |
| 523 | struct xen_device_domain_owner *owner; |
| 524 | |
| 525 | spin_lock(&dev_domain_list_spinlock); |
| 526 | owner = find_device(dev); |
| 527 | if (!owner) { |
| 528 | spin_unlock(&dev_domain_list_spinlock); |
| 529 | return -ENODEV; |
| 530 | } |
| 531 | list_del(&owner->list); |
| 532 | spin_unlock(&dev_domain_list_spinlock); |
| 533 | kfree(owner); |
| 534 | return 0; |
| 535 | } |
| 536 | EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner); |