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