Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 1 | /* |
Konrad Rzeszutek Wilk | 996c34a | 2011-06-06 12:22:23 -0400 | [diff] [blame^] | 2 | * Xen PCI - handle PCI (INTx) and MSI infrastructure calls for PV, HVM and |
| 3 | * initial domain support. We also handle the DSDT _PRT callbacks for GSI's |
| 4 | * used in HVM and initial domain mode (PV does not parse ACPI, so it has no |
| 5 | * concept of GSIs). Under PV we hook under the pnbbios API for IRQs and |
| 6 | * 0xcf8 PCI configuration read/write. |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 7 | * |
| 8 | * Author: Ryan Wilson <hap9@epoch.ncsc.mil> |
Konrad Rzeszutek Wilk | 996c34a | 2011-06-06 12:22:23 -0400 | [diff] [blame^] | 9 | * Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> |
| 10 | * Stefano Stabellini <stefano.stabellini@eu.citrix.com> |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 11 | */ |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/pci.h> |
| 15 | #include <linux/acpi.h> |
| 16 | |
| 17 | #include <linux/io.h> |
Stefano Stabellini | 0e058e5 | 2010-10-21 17:40:08 +0100 | [diff] [blame] | 18 | #include <asm/io_apic.h> |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 19 | #include <asm/pci_x86.h> |
| 20 | |
| 21 | #include <asm/xen/hypervisor.h> |
| 22 | |
Stefano Stabellini | 3942b74 | 2010-06-24 17:50:18 +0100 | [diff] [blame] | 23 | #include <xen/features.h> |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 24 | #include <xen/events.h> |
| 25 | #include <asm/xen/pci.h> |
| 26 | |
Konrad Rzeszutek Wilk | fef6e26 | 2011-07-06 10:16:21 -0400 | [diff] [blame] | 27 | static int xen_pcifront_enable_irq(struct pci_dev *dev) |
| 28 | { |
| 29 | int rc; |
| 30 | int share = 1; |
| 31 | int pirq; |
| 32 | u8 gsi; |
| 33 | |
| 34 | rc = pci_read_config_byte(dev, PCI_INTERRUPT_LINE, &gsi); |
| 35 | if (rc < 0) { |
| 36 | dev_warn(&dev->dev, "Xen PCI: failed to read interrupt line: %d\n", |
| 37 | rc); |
| 38 | return rc; |
| 39 | } |
| 40 | |
| 41 | rc = xen_allocate_pirq_gsi(gsi); |
| 42 | if (rc < 0) { |
| 43 | dev_warn(&dev->dev, "Xen PCI: failed to allocate a PIRQ for GSI%d: %d\n", |
| 44 | gsi, rc); |
| 45 | return rc; |
| 46 | } |
| 47 | pirq = rc; |
| 48 | |
| 49 | if (gsi < NR_IRQS_LEGACY) |
| 50 | share = 0; |
| 51 | |
| 52 | rc = xen_bind_pirq_gsi_to_irq(gsi, pirq, share, "pcifront"); |
| 53 | if (rc < 0) { |
| 54 | dev_warn(&dev->dev, "Xen PCI: failed to bind GSI%d (PIRQ%d) to IRQ: %d\n", |
| 55 | gsi, pirq, rc); |
| 56 | return rc; |
| 57 | } |
| 58 | |
| 59 | dev->irq = rc; |
| 60 | dev_info(&dev->dev, "Xen PCI mapped GSI%d to IRQ%d\n", gsi, dev->irq); |
| 61 | return 0; |
| 62 | } |
| 63 | |
Stefano Stabellini | 42a1de5 | 2010-06-24 16:42:04 +0100 | [diff] [blame] | 64 | #ifdef CONFIG_ACPI |
Ian Campbell | 9a62661 | 2011-02-18 16:43:30 +0000 | [diff] [blame] | 65 | static int acpi_register_gsi_xen_hvm(struct device *dev, u32 gsi, |
| 66 | int trigger, int polarity) |
Stefano Stabellini | 42a1de5 | 2010-06-24 16:42:04 +0100 | [diff] [blame] | 67 | { |
| 68 | int rc, irq; |
| 69 | struct physdev_map_pirq map_irq; |
| 70 | int shareable = 0; |
| 71 | char *name; |
| 72 | |
| 73 | if (!xen_hvm_domain()) |
| 74 | return -1; |
| 75 | |
| 76 | map_irq.domid = DOMID_SELF; |
| 77 | map_irq.type = MAP_PIRQ_TYPE_GSI; |
| 78 | map_irq.index = gsi; |
| 79 | map_irq.pirq = -1; |
| 80 | |
| 81 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); |
| 82 | if (rc) { |
| 83 | printk(KERN_WARNING "xen map irq failed %d\n", rc); |
| 84 | return -1; |
| 85 | } |
| 86 | |
Ian Campbell | 9a62661 | 2011-02-18 16:43:30 +0000 | [diff] [blame] | 87 | if (trigger == ACPI_EDGE_SENSITIVE) { |
Stefano Stabellini | 42a1de5 | 2010-06-24 16:42:04 +0100 | [diff] [blame] | 88 | shareable = 0; |
| 89 | name = "ioapic-edge"; |
| 90 | } else { |
| 91 | shareable = 1; |
| 92 | name = "ioapic-level"; |
| 93 | } |
| 94 | |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 95 | 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] | 96 | |
| 97 | printk(KERN_DEBUG "xen: --> irq=%d, pirq=%d\n", irq, map_irq.pirq); |
| 98 | |
| 99 | return irq; |
| 100 | } |
| 101 | #endif |
| 102 | |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 103 | #ifdef CONFIG_XEN_DOM0 |
Konrad Rzeszutek Wilk | ee339fe | 2011-07-06 09:43:16 -0400 | [diff] [blame] | 104 | static int xen_register_pirq(u32 gsi, int gsi_override, int triggering) |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 105 | { |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 106 | int rc, pirq, irq = -1; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 107 | struct physdev_map_pirq map_irq; |
| 108 | int shareable = 0; |
| 109 | char *name; |
| 110 | |
| 111 | if (!xen_pv_domain()) |
| 112 | return -1; |
| 113 | |
| 114 | if (triggering == ACPI_EDGE_SENSITIVE) { |
| 115 | shareable = 0; |
| 116 | name = "ioapic-edge"; |
| 117 | } else { |
| 118 | shareable = 1; |
| 119 | name = "ioapic-level"; |
| 120 | } |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 121 | pirq = xen_allocate_pirq_gsi(gsi); |
| 122 | if (pirq < 0) |
| 123 | goto out; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 124 | |
Konrad Rzeszutek Wilk | ee339fe | 2011-07-06 09:43:16 -0400 | [diff] [blame] | 125 | if (gsi_override >= 0) |
| 126 | irq = xen_bind_pirq_gsi_to_irq(gsi_override, pirq, shareable, name); |
Konrad Rzeszutek Wilk | 155a16f | 2011-06-30 09:18:27 -0400 | [diff] [blame] | 127 | else |
| 128 | irq = xen_bind_pirq_gsi_to_irq(gsi, pirq, shareable, name); |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 129 | if (irq < 0) |
| 130 | goto out; |
| 131 | |
Konrad Rzeszutek Wilk | 155a16f | 2011-06-30 09:18:27 -0400 | [diff] [blame] | 132 | printk(KERN_DEBUG "xen: --> pirq=%d -> irq=%d (gsi=%d)\n", pirq, irq, gsi); |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 133 | |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 134 | map_irq.domid = DOMID_SELF; |
| 135 | map_irq.type = MAP_PIRQ_TYPE_GSI; |
| 136 | map_irq.index = gsi; |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 137 | map_irq.pirq = pirq; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 138 | |
| 139 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); |
| 140 | if (rc) { |
| 141 | printk(KERN_WARNING "xen map irq failed %d\n", rc); |
| 142 | return -1; |
| 143 | } |
| 144 | |
| 145 | out: |
| 146 | return irq; |
| 147 | } |
| 148 | |
Konrad Rzeszutek Wilk | ee339fe | 2011-07-06 09:43:16 -0400 | [diff] [blame] | 149 | static int xen_register_gsi(u32 gsi, int gsi_override, int triggering, int polarity) |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 150 | { |
| 151 | int rc, irq; |
| 152 | struct physdev_setup_gsi setup_gsi; |
| 153 | |
| 154 | if (!xen_pv_domain()) |
| 155 | return -1; |
| 156 | |
| 157 | printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n", |
| 158 | gsi, triggering, polarity); |
| 159 | |
Konrad Rzeszutek Wilk | ee339fe | 2011-07-06 09:43:16 -0400 | [diff] [blame] | 160 | irq = xen_register_pirq(gsi, gsi_override, triggering); |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 161 | |
| 162 | setup_gsi.gsi = gsi; |
| 163 | setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1); |
| 164 | setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1); |
| 165 | |
| 166 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi); |
| 167 | if (rc == -EEXIST) |
| 168 | printk(KERN_INFO "Already setup the GSI :%d\n", gsi); |
| 169 | else if (rc) { |
| 170 | printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n", |
| 171 | gsi, rc); |
| 172 | } |
| 173 | |
| 174 | return irq; |
| 175 | } |
| 176 | |
Konrad Rzeszutek Wilk | fef6e26 | 2011-07-06 10:16:21 -0400 | [diff] [blame] | 177 | static int acpi_register_gsi_xen(struct device *dev, u32 gsi, |
| 178 | int trigger, int polarity) |
| 179 | { |
| 180 | return xen_register_gsi(gsi, -1 /* no GSI override */, trigger, polarity); |
| 181 | } |
| 182 | #endif |
| 183 | |
| 184 | #if defined(CONFIG_PCI_MSI) |
| 185 | #include <linux/msi.h> |
| 186 | #include <asm/msidef.h> |
| 187 | |
| 188 | struct xen_pci_frontend_ops *xen_pci_frontend; |
| 189 | EXPORT_SYMBOL_GPL(xen_pci_frontend); |
| 190 | |
Konrad Rzeszutek Wilk | fef6e26 | 2011-07-06 10:16:21 -0400 | [diff] [blame] | 191 | static int xen_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 192 | { |
| 193 | int irq, ret, i; |
| 194 | struct msi_desc *msidesc; |
| 195 | int *v; |
| 196 | |
| 197 | v = kzalloc(sizeof(int) * max(1, nvec), GFP_KERNEL); |
| 198 | if (!v) |
| 199 | return -ENOMEM; |
| 200 | |
| 201 | if (type == PCI_CAP_ID_MSIX) |
| 202 | ret = xen_pci_frontend_enable_msix(dev, v, nvec); |
| 203 | else |
| 204 | ret = xen_pci_frontend_enable_msi(dev, v); |
| 205 | if (ret) |
| 206 | goto error; |
| 207 | i = 0; |
| 208 | list_for_each_entry(msidesc, &dev->msi_list, list) { |
| 209 | irq = xen_bind_pirq_msi_to_irq(dev, msidesc, v[i], 0, |
| 210 | (type == PCI_CAP_ID_MSIX) ? |
| 211 | "pcifront-msi-x" : |
| 212 | "pcifront-msi", |
| 213 | DOMID_SELF); |
| 214 | if (irq < 0) |
| 215 | goto free; |
| 216 | i++; |
| 217 | } |
| 218 | kfree(v); |
| 219 | return 0; |
| 220 | |
| 221 | error: |
| 222 | dev_err(&dev->dev, "Xen PCI frontend has not registered MSI/MSI-X support!\n"); |
| 223 | free: |
| 224 | kfree(v); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | #define XEN_PIRQ_MSI_DATA (MSI_DATA_TRIGGER_EDGE | \ |
| 229 | MSI_DATA_LEVEL_ASSERT | (3 << 8) | MSI_DATA_VECTOR(0)) |
| 230 | |
| 231 | static void xen_msi_compose_msg(struct pci_dev *pdev, unsigned int pirq, |
| 232 | struct msi_msg *msg) |
| 233 | { |
| 234 | /* We set vector == 0 to tell the hypervisor we don't care about it, |
| 235 | * but we want a pirq setup instead. |
| 236 | * We use the dest_id field to pass the pirq that we want. */ |
| 237 | msg->address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(pirq); |
| 238 | msg->address_lo = |
| 239 | MSI_ADDR_BASE_LO | |
| 240 | MSI_ADDR_DEST_MODE_PHYSICAL | |
| 241 | MSI_ADDR_REDIRECTION_CPU | |
| 242 | MSI_ADDR_DEST_ID(pirq); |
| 243 | |
| 244 | msg->data = XEN_PIRQ_MSI_DATA; |
| 245 | } |
| 246 | |
| 247 | static int xen_hvm_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 248 | { |
| 249 | int irq, pirq; |
| 250 | struct msi_desc *msidesc; |
| 251 | struct msi_msg msg; |
| 252 | |
| 253 | list_for_each_entry(msidesc, &dev->msi_list, list) { |
| 254 | __read_msi_msg(msidesc, &msg); |
| 255 | pirq = MSI_ADDR_EXT_DEST_ID(msg.address_hi) | |
| 256 | ((msg.address_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xff); |
| 257 | if (msg.data != XEN_PIRQ_MSI_DATA || |
| 258 | xen_irq_from_pirq(pirq) < 0) { |
| 259 | pirq = xen_allocate_pirq_msi(dev, msidesc); |
| 260 | if (pirq < 0) |
| 261 | goto error; |
| 262 | xen_msi_compose_msg(dev, pirq, &msg); |
| 263 | __write_msi_msg(msidesc, &msg); |
| 264 | dev_dbg(&dev->dev, "xen: msi bound to pirq=%d\n", pirq); |
| 265 | } else { |
| 266 | dev_dbg(&dev->dev, |
| 267 | "xen: msi already bound to pirq=%d\n", pirq); |
| 268 | } |
| 269 | irq = xen_bind_pirq_msi_to_irq(dev, msidesc, pirq, 0, |
| 270 | (type == PCI_CAP_ID_MSIX) ? |
| 271 | "msi-x" : "msi", |
| 272 | DOMID_SELF); |
| 273 | if (irq < 0) |
| 274 | goto error; |
| 275 | dev_dbg(&dev->dev, |
| 276 | "xen: msi --> pirq=%d --> irq=%d\n", pirq, irq); |
| 277 | } |
| 278 | return 0; |
| 279 | |
| 280 | error: |
| 281 | dev_err(&dev->dev, |
| 282 | "Xen PCI frontend has not registered MSI/MSI-X support!\n"); |
| 283 | return -ENODEV; |
| 284 | } |
| 285 | |
| 286 | #ifdef CONFIG_XEN_DOM0 |
| 287 | static int xen_initdom_setup_msi_irqs(struct pci_dev *dev, int nvec, int type) |
| 288 | { |
| 289 | int ret = 0; |
| 290 | struct msi_desc *msidesc; |
| 291 | |
| 292 | list_for_each_entry(msidesc, &dev->msi_list, list) { |
| 293 | struct physdev_map_pirq map_irq; |
| 294 | domid_t domid; |
| 295 | |
| 296 | domid = ret = xen_find_device_domain_owner(dev); |
| 297 | /* N.B. Casting int's -ENODEV to uint16_t results in 0xFFED, |
| 298 | * hence check ret value for < 0. */ |
| 299 | if (ret < 0) |
| 300 | domid = DOMID_SELF; |
| 301 | |
| 302 | memset(&map_irq, 0, sizeof(map_irq)); |
| 303 | map_irq.domid = domid; |
| 304 | map_irq.type = MAP_PIRQ_TYPE_MSI; |
| 305 | map_irq.index = -1; |
| 306 | map_irq.pirq = -1; |
| 307 | map_irq.bus = dev->bus->number; |
| 308 | map_irq.devfn = dev->devfn; |
| 309 | |
| 310 | if (type == PCI_CAP_ID_MSIX) { |
| 311 | int pos; |
| 312 | u32 table_offset, bir; |
| 313 | |
| 314 | pos = pci_find_capability(dev, PCI_CAP_ID_MSIX); |
| 315 | |
| 316 | pci_read_config_dword(dev, pos + PCI_MSIX_TABLE, |
| 317 | &table_offset); |
| 318 | bir = (u8)(table_offset & PCI_MSIX_FLAGS_BIRMASK); |
| 319 | |
| 320 | map_irq.table_base = pci_resource_start(dev, bir); |
| 321 | map_irq.entry_nr = msidesc->msi_attrib.entry_nr; |
| 322 | } |
| 323 | |
| 324 | ret = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); |
| 325 | if (ret) { |
| 326 | dev_warn(&dev->dev, "xen map irq failed %d for %d domain\n", |
| 327 | ret, domid); |
| 328 | goto out; |
| 329 | } |
| 330 | |
| 331 | ret = xen_bind_pirq_msi_to_irq(dev, msidesc, |
| 332 | map_irq.pirq, map_irq.index, |
| 333 | (type == PCI_CAP_ID_MSIX) ? |
| 334 | "msi-x" : "msi", |
| 335 | domid); |
| 336 | if (ret < 0) |
| 337 | goto out; |
| 338 | } |
| 339 | ret = 0; |
| 340 | out: |
| 341 | return ret; |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | static void xen_teardown_msi_irqs(struct pci_dev *dev) |
| 346 | { |
| 347 | struct msi_desc *msidesc; |
| 348 | |
| 349 | msidesc = list_entry(dev->msi_list.next, struct msi_desc, list); |
| 350 | if (msidesc->msi_attrib.is_msix) |
| 351 | xen_pci_frontend_disable_msix(dev); |
| 352 | else |
| 353 | xen_pci_frontend_disable_msi(dev); |
| 354 | |
| 355 | /* Free the IRQ's and the msidesc using the generic code. */ |
| 356 | default_teardown_msi_irqs(dev); |
| 357 | } |
| 358 | |
| 359 | static void xen_teardown_msi_irq(unsigned int irq) |
| 360 | { |
| 361 | xen_destroy_irq(irq); |
| 362 | } |
| 363 | |
| 364 | #endif |
| 365 | |
Konrad Rzeszutek Wilk | fef6e26 | 2011-07-06 10:16:21 -0400 | [diff] [blame] | 366 | int __init pci_xen_init(void) |
| 367 | { |
| 368 | if (!xen_pv_domain() || xen_initial_domain()) |
| 369 | return -ENODEV; |
| 370 | |
| 371 | printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n"); |
| 372 | |
| 373 | pcibios_set_cache_line_size(); |
| 374 | |
| 375 | pcibios_enable_irq = xen_pcifront_enable_irq; |
| 376 | pcibios_disable_irq = NULL; |
| 377 | |
| 378 | #ifdef CONFIG_ACPI |
| 379 | /* Keep ACPI out of the picture */ |
| 380 | acpi_noirq = 1; |
| 381 | #endif |
| 382 | |
| 383 | #ifdef CONFIG_PCI_MSI |
| 384 | x86_msi.setup_msi_irqs = xen_setup_msi_irqs; |
| 385 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; |
| 386 | x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs; |
| 387 | #endif |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | int __init pci_xen_hvm_init(void) |
| 392 | { |
| 393 | if (!xen_feature(XENFEAT_hvm_pirqs)) |
| 394 | return 0; |
| 395 | |
| 396 | #ifdef CONFIG_ACPI |
| 397 | /* |
| 398 | * We don't want to change the actual ACPI delivery model, |
| 399 | * just how GSIs get registered. |
| 400 | */ |
| 401 | __acpi_register_gsi = acpi_register_gsi_xen_hvm; |
| 402 | #endif |
| 403 | |
| 404 | #ifdef CONFIG_PCI_MSI |
| 405 | x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs; |
| 406 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; |
| 407 | #endif |
| 408 | return 0; |
| 409 | } |
| 410 | |
| 411 | #ifdef CONFIG_XEN_DOM0 |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 412 | static __init void xen_setup_acpi_sci(void) |
| 413 | { |
| 414 | int rc; |
| 415 | int trigger, polarity; |
| 416 | int gsi = acpi_sci_override_gsi; |
Konrad Rzeszutek Wilk | ee339fe | 2011-07-06 09:43:16 -0400 | [diff] [blame] | 417 | int irq = -1; |
| 418 | int gsi_override = -1; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 419 | |
| 420 | if (!gsi) |
| 421 | return; |
| 422 | |
| 423 | rc = acpi_get_override_irq(gsi, &trigger, &polarity); |
| 424 | if (rc) { |
| 425 | printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi" |
| 426 | " sci, rc=%d\n", rc); |
| 427 | return; |
| 428 | } |
| 429 | trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
| 430 | polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; |
Konrad Rzeszutek Wilk | 996c34a | 2011-06-06 12:22:23 -0400 | [diff] [blame^] | 431 | |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 432 | printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d " |
| 433 | "polarity=%d\n", gsi, trigger, polarity); |
| 434 | |
Konrad Rzeszutek Wilk | ee339fe | 2011-07-06 09:43:16 -0400 | [diff] [blame] | 435 | /* Before we bind the GSI to a Linux IRQ, check whether |
| 436 | * we need to override it with bus_irq (IRQ) value. Usually for |
| 437 | * IRQs below IRQ_LEGACY_IRQ this holds IRQ == GSI, as so: |
| 438 | * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 9 low level) |
| 439 | * but there are oddballs where the IRQ != GSI: |
| 440 | * ACPI: INT_SRC_OVR (bus 0 bus_irq 9 global_irq 20 low level) |
| 441 | * which ends up being: gsi_to_irq[9] == 20 |
| 442 | * (which is what acpi_gsi_to_irq ends up calling when starting the |
| 443 | * the ACPI interpreter and keels over since IRQ 9 has not been |
| 444 | * setup as we had setup IRQ 20 for it). |
| 445 | */ |
| 446 | /* Check whether the GSI != IRQ */ |
| 447 | if (acpi_gsi_to_irq(gsi, &irq) == 0) { |
| 448 | if (irq >= 0 && irq != gsi) |
| 449 | /* Bugger, we MUST have that IRQ. */ |
| 450 | gsi_override = irq; |
| 451 | } |
| 452 | |
| 453 | gsi = xen_register_gsi(gsi, gsi_override, trigger, polarity); |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 454 | printk(KERN_INFO "xen: acpi sci %d\n", gsi); |
| 455 | |
| 456 | return; |
| 457 | } |
| 458 | |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 459 | static int __init pci_xen_initial_domain(void) |
| 460 | { |
Qing He | f731e3ef | 2010-10-11 15:30:09 +0100 | [diff] [blame] | 461 | #ifdef CONFIG_PCI_MSI |
| 462 | x86_msi.setup_msi_irqs = xen_initdom_setup_msi_irqs; |
| 463 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; |
| 464 | #endif |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 465 | xen_setup_acpi_sci(); |
| 466 | __acpi_register_gsi = acpi_register_gsi_xen; |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | void __init xen_setup_pirqs(void) |
| 472 | { |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 473 | int pirq, irq; |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 474 | |
| 475 | pci_xen_initial_domain(); |
| 476 | |
| 477 | if (0 == nr_ioapics) { |
Ian Campbell | f4d0635 | 2011-03-10 16:08:07 +0000 | [diff] [blame] | 478 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) { |
| 479 | pirq = xen_allocate_pirq_gsi(irq); |
| 480 | if (WARN(pirq < 0, |
| 481 | "Could not allocate PIRQ for legacy interrupt\n")) |
| 482 | break; |
| 483 | irq = xen_bind_pirq_gsi_to_irq(irq, pirq, 0, "xt-pic"); |
| 484 | } |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 485 | return; |
| 486 | } |
| 487 | |
| 488 | /* Pre-allocate legacy irqs */ |
| 489 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) { |
| 490 | int trigger, polarity; |
| 491 | |
| 492 | if (acpi_get_override_irq(irq, &trigger, &polarity) == -1) |
| 493 | continue; |
| 494 | |
Konrad Rzeszutek Wilk | ee339fe | 2011-07-06 09:43:16 -0400 | [diff] [blame] | 495 | xen_register_pirq(irq, -1 /* no GSI override */, |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame] | 496 | trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE); |
| 497 | } |
| 498 | } |
Konrad Rzeszutek Wilk | c55fa78 | 2010-11-08 14:13:35 -0500 | [diff] [blame] | 499 | |
| 500 | struct xen_device_domain_owner { |
| 501 | domid_t domain; |
| 502 | struct pci_dev *dev; |
| 503 | struct list_head list; |
| 504 | }; |
| 505 | |
| 506 | static DEFINE_SPINLOCK(dev_domain_list_spinlock); |
| 507 | static struct list_head dev_domain_list = LIST_HEAD_INIT(dev_domain_list); |
| 508 | |
| 509 | static struct xen_device_domain_owner *find_device(struct pci_dev *dev) |
| 510 | { |
| 511 | struct xen_device_domain_owner *owner; |
| 512 | |
| 513 | list_for_each_entry(owner, &dev_domain_list, list) { |
| 514 | if (owner->dev == dev) |
| 515 | return owner; |
| 516 | } |
| 517 | return NULL; |
| 518 | } |
| 519 | |
| 520 | int xen_find_device_domain_owner(struct pci_dev *dev) |
| 521 | { |
| 522 | struct xen_device_domain_owner *owner; |
| 523 | int domain = -ENODEV; |
| 524 | |
| 525 | spin_lock(&dev_domain_list_spinlock); |
| 526 | owner = find_device(dev); |
| 527 | if (owner) |
| 528 | domain = owner->domain; |
| 529 | spin_unlock(&dev_domain_list_spinlock); |
| 530 | return domain; |
| 531 | } |
| 532 | EXPORT_SYMBOL_GPL(xen_find_device_domain_owner); |
| 533 | |
| 534 | int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain) |
| 535 | { |
| 536 | struct xen_device_domain_owner *owner; |
| 537 | |
| 538 | owner = kzalloc(sizeof(struct xen_device_domain_owner), GFP_KERNEL); |
| 539 | if (!owner) |
| 540 | return -ENODEV; |
| 541 | |
| 542 | spin_lock(&dev_domain_list_spinlock); |
| 543 | if (find_device(dev)) { |
| 544 | spin_unlock(&dev_domain_list_spinlock); |
| 545 | kfree(owner); |
| 546 | return -EEXIST; |
| 547 | } |
| 548 | owner->domain = domain; |
| 549 | owner->dev = dev; |
| 550 | list_add_tail(&owner->list, &dev_domain_list); |
| 551 | spin_unlock(&dev_domain_list_spinlock); |
| 552 | return 0; |
| 553 | } |
| 554 | EXPORT_SYMBOL_GPL(xen_register_device_domain_owner); |
| 555 | |
| 556 | int xen_unregister_device_domain_owner(struct pci_dev *dev) |
| 557 | { |
| 558 | struct xen_device_domain_owner *owner; |
| 559 | |
| 560 | spin_lock(&dev_domain_list_spinlock); |
| 561 | owner = find_device(dev); |
| 562 | if (!owner) { |
| 563 | spin_unlock(&dev_domain_list_spinlock); |
| 564 | return -ENODEV; |
| 565 | } |
| 566 | list_del(&owner->list); |
| 567 | spin_unlock(&dev_domain_list_spinlock); |
| 568 | kfree(owner); |
| 569 | return 0; |
| 570 | } |
| 571 | EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner); |
Konrad Rzeszutek Wilk | 7c1bfd6 | 2011-05-16 13:47:30 -0400 | [diff] [blame] | 572 | #endif |