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 | |
| 138 | if (!xen_initial_domain()) { |
| 139 | if (type == PCI_CAP_ID_MSIX) |
| 140 | ret = xen_pci_frontend_enable_msix(dev, &v, nvec); |
| 141 | else |
| 142 | ret = xen_pci_frontend_enable_msi(dev, &v); |
| 143 | if (ret) |
| 144 | goto error; |
| 145 | } |
| 146 | i = 0; |
| 147 | list_for_each_entry(msidesc, &dev->msi_list, list) { |
| 148 | irq = xen_allocate_pirq(v[i], 0, /* not sharable */ |
| 149 | (type == PCI_CAP_ID_MSIX) ? |
| 150 | "pcifront-msi-x" : "pcifront-msi"); |
| 151 | if (irq < 0) |
| 152 | return -1; |
| 153 | |
| 154 | ret = set_irq_msi(irq, msidesc); |
| 155 | if (ret) |
| 156 | goto error_while; |
| 157 | i++; |
| 158 | } |
| 159 | kfree(v); |
| 160 | return 0; |
| 161 | |
| 162 | error_while: |
| 163 | unbind_from_irqhandler(irq, NULL); |
| 164 | error: |
| 165 | if (ret == -ENODEV) |
| 166 | dev_err(&dev->dev, "Xen PCI frontend has not registered" \ |
| 167 | " MSI/MSI-X support!\n"); |
| 168 | |
| 169 | kfree(v); |
| 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | static void xen_teardown_msi_irqs(struct pci_dev *dev) |
| 174 | { |
| 175 | /* Only do this when were are in non-privileged mode.*/ |
| 176 | if (!xen_initial_domain()) { |
| 177 | struct msi_desc *msidesc; |
| 178 | |
| 179 | msidesc = list_entry(dev->msi_list.next, struct msi_desc, list); |
| 180 | if (msidesc->msi_attrib.is_msix) |
| 181 | xen_pci_frontend_disable_msix(dev); |
| 182 | else |
| 183 | xen_pci_frontend_disable_msi(dev); |
| 184 | } |
| 185 | |
| 186 | } |
| 187 | |
| 188 | static void xen_teardown_msi_irq(unsigned int irq) |
| 189 | { |
| 190 | xen_destroy_irq(irq); |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | static int xen_pcifront_enable_irq(struct pci_dev *dev) |
| 195 | { |
| 196 | int rc; |
| 197 | int share = 1; |
| 198 | |
| 199 | dev_info(&dev->dev, "Xen PCI enabling IRQ: %d\n", dev->irq); |
| 200 | |
| 201 | if (dev->irq < 0) |
| 202 | return -EINVAL; |
| 203 | |
| 204 | if (dev->irq < NR_IRQS_LEGACY) |
| 205 | share = 0; |
| 206 | |
| 207 | rc = xen_allocate_pirq(dev->irq, share, "pcifront"); |
| 208 | if (rc < 0) { |
| 209 | dev_warn(&dev->dev, "Xen PCI IRQ: %d, failed to register:%d\n", |
| 210 | dev->irq, rc); |
| 211 | return rc; |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int __init pci_xen_init(void) |
| 217 | { |
| 218 | if (!xen_pv_domain() || xen_initial_domain()) |
| 219 | return -ENODEV; |
| 220 | |
| 221 | printk(KERN_INFO "PCI: setting up Xen PCI frontend stub\n"); |
| 222 | |
| 223 | pcibios_set_cache_line_size(); |
| 224 | |
| 225 | pcibios_enable_irq = xen_pcifront_enable_irq; |
| 226 | pcibios_disable_irq = NULL; |
| 227 | |
| 228 | #ifdef CONFIG_ACPI |
| 229 | /* Keep ACPI out of the picture */ |
| 230 | acpi_noirq = 1; |
| 231 | #endif |
| 232 | |
Alex Nixon | b5401a9 | 2010-03-18 16:31:34 -0400 | [diff] [blame] | 233 | #ifdef CONFIG_PCI_MSI |
| 234 | x86_msi.setup_msi_irqs = xen_setup_msi_irqs; |
| 235 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; |
| 236 | x86_msi.teardown_msi_irqs = xen_teardown_msi_irqs; |
| 237 | #endif |
| 238 | return 0; |
| 239 | } |
Stefano Stabellini | 3942b74 | 2010-06-24 17:50:18 +0100 | [diff] [blame] | 240 | |
| 241 | int __init pci_xen_hvm_init(void) |
| 242 | { |
| 243 | if (!xen_feature(XENFEAT_hvm_pirqs)) |
| 244 | return 0; |
| 245 | |
| 246 | #ifdef CONFIG_ACPI |
| 247 | /* |
| 248 | * We don't want to change the actual ACPI delivery model, |
| 249 | * just how GSIs get registered. |
| 250 | */ |
| 251 | __acpi_register_gsi = acpi_register_gsi_xen_hvm; |
| 252 | #endif |
Stefano Stabellini | 809f926 | 2010-07-01 17:10:39 +0100 | [diff] [blame] | 253 | |
| 254 | #ifdef CONFIG_PCI_MSI |
| 255 | x86_msi.setup_msi_irqs = xen_hvm_setup_msi_irqs; |
| 256 | x86_msi.teardown_msi_irq = xen_teardown_msi_irq; |
| 257 | #endif |
Stefano Stabellini | 3942b74 | 2010-06-24 17:50:18 +0100 | [diff] [blame] | 258 | return 0; |
| 259 | } |
Jeremy Fitzhardinge | 38aa66f | 2010-09-02 14:51:39 +0100 | [diff] [blame^] | 260 | |
| 261 | #ifdef CONFIG_XEN_DOM0 |
| 262 | static int xen_register_pirq(u32 gsi, int triggering) |
| 263 | { |
| 264 | int rc, irq; |
| 265 | struct physdev_map_pirq map_irq; |
| 266 | int shareable = 0; |
| 267 | char *name; |
| 268 | |
| 269 | if (!xen_pv_domain()) |
| 270 | return -1; |
| 271 | |
| 272 | if (triggering == ACPI_EDGE_SENSITIVE) { |
| 273 | shareable = 0; |
| 274 | name = "ioapic-edge"; |
| 275 | } else { |
| 276 | shareable = 1; |
| 277 | name = "ioapic-level"; |
| 278 | } |
| 279 | |
| 280 | irq = xen_allocate_pirq(gsi, shareable, name); |
| 281 | |
| 282 | printk(KERN_DEBUG "xen: --> irq=%d\n", irq); |
| 283 | |
| 284 | if (irq < 0) |
| 285 | goto out; |
| 286 | |
| 287 | map_irq.domid = DOMID_SELF; |
| 288 | map_irq.type = MAP_PIRQ_TYPE_GSI; |
| 289 | map_irq.index = gsi; |
| 290 | map_irq.pirq = irq; |
| 291 | |
| 292 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_map_pirq, &map_irq); |
| 293 | if (rc) { |
| 294 | printk(KERN_WARNING "xen map irq failed %d\n", rc); |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | out: |
| 299 | return irq; |
| 300 | } |
| 301 | |
| 302 | static int xen_register_gsi(u32 gsi, int triggering, int polarity) |
| 303 | { |
| 304 | int rc, irq; |
| 305 | struct physdev_setup_gsi setup_gsi; |
| 306 | |
| 307 | if (!xen_pv_domain()) |
| 308 | return -1; |
| 309 | |
| 310 | printk(KERN_DEBUG "xen: registering gsi %u triggering %d polarity %d\n", |
| 311 | gsi, triggering, polarity); |
| 312 | |
| 313 | irq = xen_register_pirq(gsi, triggering); |
| 314 | |
| 315 | setup_gsi.gsi = gsi; |
| 316 | setup_gsi.triggering = (triggering == ACPI_EDGE_SENSITIVE ? 0 : 1); |
| 317 | setup_gsi.polarity = (polarity == ACPI_ACTIVE_HIGH ? 0 : 1); |
| 318 | |
| 319 | rc = HYPERVISOR_physdev_op(PHYSDEVOP_setup_gsi, &setup_gsi); |
| 320 | if (rc == -EEXIST) |
| 321 | printk(KERN_INFO "Already setup the GSI :%d\n", gsi); |
| 322 | else if (rc) { |
| 323 | printk(KERN_ERR "Failed to setup GSI :%d, err_code:%d\n", |
| 324 | gsi, rc); |
| 325 | } |
| 326 | |
| 327 | return irq; |
| 328 | } |
| 329 | |
| 330 | static __init void xen_setup_acpi_sci(void) |
| 331 | { |
| 332 | int rc; |
| 333 | int trigger, polarity; |
| 334 | int gsi = acpi_sci_override_gsi; |
| 335 | |
| 336 | if (!gsi) |
| 337 | return; |
| 338 | |
| 339 | rc = acpi_get_override_irq(gsi, &trigger, &polarity); |
| 340 | if (rc) { |
| 341 | printk(KERN_WARNING "xen: acpi_get_override_irq failed for acpi" |
| 342 | " sci, rc=%d\n", rc); |
| 343 | return; |
| 344 | } |
| 345 | trigger = trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE; |
| 346 | polarity = polarity ? ACPI_ACTIVE_LOW : ACPI_ACTIVE_HIGH; |
| 347 | |
| 348 | printk(KERN_INFO "xen: sci override: global_irq=%d trigger=%d " |
| 349 | "polarity=%d\n", gsi, trigger, polarity); |
| 350 | |
| 351 | gsi = xen_register_gsi(gsi, trigger, polarity); |
| 352 | printk(KERN_INFO "xen: acpi sci %d\n", gsi); |
| 353 | |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | static int acpi_register_gsi_xen(struct device *dev, u32 gsi, |
| 358 | int trigger, int polarity) |
| 359 | { |
| 360 | return xen_register_gsi(gsi, trigger, polarity); |
| 361 | } |
| 362 | |
| 363 | static int __init pci_xen_initial_domain(void) |
| 364 | { |
| 365 | xen_setup_acpi_sci(); |
| 366 | __acpi_register_gsi = acpi_register_gsi_xen; |
| 367 | |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | void __init xen_setup_pirqs(void) |
| 372 | { |
| 373 | int irq; |
| 374 | |
| 375 | pci_xen_initial_domain(); |
| 376 | |
| 377 | if (0 == nr_ioapics) { |
| 378 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) |
| 379 | xen_allocate_pirq(irq, 0, "xt-pic"); |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | /* Pre-allocate legacy irqs */ |
| 384 | for (irq = 0; irq < NR_IRQS_LEGACY; irq++) { |
| 385 | int trigger, polarity; |
| 386 | |
| 387 | if (acpi_get_override_irq(irq, &trigger, &polarity) == -1) |
| 388 | continue; |
| 389 | |
| 390 | xen_register_pirq(irq, |
| 391 | trigger ? ACPI_LEVEL_SENSITIVE : ACPI_EDGE_SENSITIVE); |
| 392 | } |
| 393 | } |
| 394 | #endif |