1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Volume Management Device driver 4 * Copyright (c) 2015, Intel Corporation. 5 */ 6 7 #include <linux/device.h> 8 #include <linux/interrupt.h> 9 #include <linux/irq.h> 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/msi.h> 13 #include <linux/pci.h> 14 #include <linux/srcu.h> 15 #include <linux/rculist.h> 16 #include <linux/rcupdate.h> 17 18 #include <asm/irqdomain.h> 19 #include <asm/device.h> 20 #include <asm/msi.h> 21 22 #define VMD_CFGBAR 0 23 #define VMD_MEMBAR1 2 24 #define VMD_MEMBAR2 4 25 26 #define PCI_REG_VMCAP 0x40 27 #define BUS_RESTRICT_CAP(vmcap) (vmcap & 0x1) 28 #define PCI_REG_VMCONFIG 0x44 29 #define BUS_RESTRICT_CFG(vmcfg) ((vmcfg >> 8) & 0x3) 30 #define PCI_REG_VMLOCK 0x70 31 #define MB2_SHADOW_EN(vmlock) (vmlock & 0x2) 32 33 #define MB2_SHADOW_OFFSET 0x2000 34 #define MB2_SHADOW_SIZE 16 35 36 enum vmd_features { 37 /* 38 * Device may contain registers which hint the physical location of the 39 * membars, in order to allow proper address translation during 40 * resource assignment to enable guest virtualization 41 */ 42 VMD_FEAT_HAS_MEMBAR_SHADOW = (1 << 0), 43 44 /* 45 * Device may provide root port configuration information which limits 46 * bus numbering 47 */ 48 VMD_FEAT_HAS_BUS_RESTRICTIONS = (1 << 1), 49 50 /* 51 * Device contains physical location shadow registers in 52 * vendor-specific capability space 53 */ 54 VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP = (1 << 2), 55 }; 56 57 /* 58 * Lock for manipulating VMD IRQ lists. 59 */ 60 static DEFINE_RAW_SPINLOCK(list_lock); 61 62 /** 63 * struct vmd_irq - private data to map driver IRQ to the VMD shared vector 64 * @node: list item for parent traversal. 65 * @irq: back pointer to parent. 66 * @enabled: true if driver enabled IRQ 67 * @virq: the virtual IRQ value provided to the requesting driver. 68 * 69 * Every MSI/MSI-X IRQ requested for a device in a VMD domain will be mapped to 70 * a VMD IRQ using this structure. 71 */ 72 struct vmd_irq { 73 struct list_head node; 74 struct vmd_irq_list *irq; 75 bool enabled; 76 unsigned int virq; 77 }; 78 79 /** 80 * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector 81 * @irq_list: the list of irq's the VMD one demuxes to. 82 * @srcu: SRCU struct for local synchronization. 83 * @count: number of child IRQs assigned to this vector; used to track 84 * sharing. 85 */ 86 struct vmd_irq_list { 87 struct list_head irq_list; 88 struct srcu_struct srcu; 89 unsigned int count; 90 }; 91 92 struct vmd_dev { 93 struct pci_dev *dev; 94 95 spinlock_t cfg_lock; 96 char __iomem *cfgbar; 97 98 int msix_count; 99 struct vmd_irq_list *irqs; 100 101 struct pci_sysdata sysdata; 102 struct resource resources[3]; 103 struct irq_domain *irq_domain; 104 struct pci_bus *bus; 105 u8 busn_start; 106 }; 107 108 static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus) 109 { 110 return container_of(bus->sysdata, struct vmd_dev, sysdata); 111 } 112 113 static inline unsigned int index_from_irqs(struct vmd_dev *vmd, 114 struct vmd_irq_list *irqs) 115 { 116 return irqs - vmd->irqs; 117 } 118 119 /* 120 * Drivers managing a device in a VMD domain allocate their own IRQs as before, 121 * but the MSI entry for the hardware it's driving will be programmed with a 122 * destination ID for the VMD MSI-X table. The VMD muxes interrupts in its 123 * domain into one of its own, and the VMD driver de-muxes these for the 124 * handlers sharing that VMD IRQ. The vmd irq_domain provides the operations 125 * and irq_chip to set this up. 126 */ 127 static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) 128 { 129 struct vmd_irq *vmdirq = data->chip_data; 130 struct vmd_irq_list *irq = vmdirq->irq; 131 struct vmd_dev *vmd = irq_data_get_irq_handler_data(data); 132 133 memset(msg, 0, sizeof(*msg)); 134 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH; 135 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW; 136 msg->arch_addr_lo.destid_0_7 = index_from_irqs(vmd, irq); 137 } 138 139 /* 140 * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops. 141 */ 142 static void vmd_irq_enable(struct irq_data *data) 143 { 144 struct vmd_irq *vmdirq = data->chip_data; 145 unsigned long flags; 146 147 raw_spin_lock_irqsave(&list_lock, flags); 148 WARN_ON(vmdirq->enabled); 149 list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list); 150 vmdirq->enabled = true; 151 raw_spin_unlock_irqrestore(&list_lock, flags); 152 153 data->chip->irq_unmask(data); 154 } 155 156 static void vmd_irq_disable(struct irq_data *data) 157 { 158 struct vmd_irq *vmdirq = data->chip_data; 159 unsigned long flags; 160 161 data->chip->irq_mask(data); 162 163 raw_spin_lock_irqsave(&list_lock, flags); 164 if (vmdirq->enabled) { 165 list_del_rcu(&vmdirq->node); 166 vmdirq->enabled = false; 167 } 168 raw_spin_unlock_irqrestore(&list_lock, flags); 169 } 170 171 /* 172 * XXX: Stubbed until we develop acceptable way to not create conflicts with 173 * other devices sharing the same vector. 174 */ 175 static int vmd_irq_set_affinity(struct irq_data *data, 176 const struct cpumask *dest, bool force) 177 { 178 return -EINVAL; 179 } 180 181 static struct irq_chip vmd_msi_controller = { 182 .name = "VMD-MSI", 183 .irq_enable = vmd_irq_enable, 184 .irq_disable = vmd_irq_disable, 185 .irq_compose_msi_msg = vmd_compose_msi_msg, 186 .irq_set_affinity = vmd_irq_set_affinity, 187 }; 188 189 static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info, 190 msi_alloc_info_t *arg) 191 { 192 return 0; 193 } 194 195 /* 196 * XXX: We can be even smarter selecting the best IRQ once we solve the 197 * affinity problem. 198 */ 199 static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc) 200 { 201 int i, best = 1; 202 unsigned long flags; 203 204 if (vmd->msix_count == 1) 205 return &vmd->irqs[0]; 206 207 /* 208 * White list for fast-interrupt handlers. All others will share the 209 * "slow" interrupt vector. 210 */ 211 switch (msi_desc_to_pci_dev(desc)->class) { 212 case PCI_CLASS_STORAGE_EXPRESS: 213 break; 214 default: 215 return &vmd->irqs[0]; 216 } 217 218 raw_spin_lock_irqsave(&list_lock, flags); 219 for (i = 1; i < vmd->msix_count; i++) 220 if (vmd->irqs[i].count < vmd->irqs[best].count) 221 best = i; 222 vmd->irqs[best].count++; 223 raw_spin_unlock_irqrestore(&list_lock, flags); 224 225 return &vmd->irqs[best]; 226 } 227 228 static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info, 229 unsigned int virq, irq_hw_number_t hwirq, 230 msi_alloc_info_t *arg) 231 { 232 struct msi_desc *desc = arg->desc; 233 struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus); 234 struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL); 235 unsigned int index, vector; 236 237 if (!vmdirq) 238 return -ENOMEM; 239 240 INIT_LIST_HEAD(&vmdirq->node); 241 vmdirq->irq = vmd_next_irq(vmd, desc); 242 vmdirq->virq = virq; 243 index = index_from_irqs(vmd, vmdirq->irq); 244 vector = pci_irq_vector(vmd->dev, index); 245 246 irq_domain_set_info(domain, virq, vector, info->chip, vmdirq, 247 handle_untracked_irq, vmd, NULL); 248 return 0; 249 } 250 251 static void vmd_msi_free(struct irq_domain *domain, 252 struct msi_domain_info *info, unsigned int virq) 253 { 254 struct vmd_irq *vmdirq = irq_get_chip_data(virq); 255 unsigned long flags; 256 257 synchronize_srcu(&vmdirq->irq->srcu); 258 259 /* XXX: Potential optimization to rebalance */ 260 raw_spin_lock_irqsave(&list_lock, flags); 261 vmdirq->irq->count--; 262 raw_spin_unlock_irqrestore(&list_lock, flags); 263 264 kfree(vmdirq); 265 } 266 267 static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev, 268 int nvec, msi_alloc_info_t *arg) 269 { 270 struct pci_dev *pdev = to_pci_dev(dev); 271 struct vmd_dev *vmd = vmd_from_bus(pdev->bus); 272 273 if (nvec > vmd->msix_count) 274 return vmd->msix_count; 275 276 memset(arg, 0, sizeof(*arg)); 277 return 0; 278 } 279 280 static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc) 281 { 282 arg->desc = desc; 283 } 284 285 static struct msi_domain_ops vmd_msi_domain_ops = { 286 .get_hwirq = vmd_get_hwirq, 287 .msi_init = vmd_msi_init, 288 .msi_free = vmd_msi_free, 289 .msi_prepare = vmd_msi_prepare, 290 .set_desc = vmd_set_desc, 291 }; 292 293 static struct msi_domain_info vmd_msi_domain_info = { 294 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | 295 MSI_FLAG_PCI_MSIX, 296 .ops = &vmd_msi_domain_ops, 297 .chip = &vmd_msi_controller, 298 }; 299 300 static int vmd_create_irq_domain(struct vmd_dev *vmd) 301 { 302 struct fwnode_handle *fn; 303 304 fn = irq_domain_alloc_named_id_fwnode("VMD-MSI", vmd->sysdata.domain); 305 if (!fn) 306 return -ENODEV; 307 308 vmd->irq_domain = pci_msi_create_irq_domain(fn, &vmd_msi_domain_info, NULL); 309 if (!vmd->irq_domain) { 310 irq_domain_free_fwnode(fn); 311 return -ENODEV; 312 } 313 314 return 0; 315 } 316 317 static void vmd_remove_irq_domain(struct vmd_dev *vmd) 318 { 319 if (vmd->irq_domain) { 320 struct fwnode_handle *fn = vmd->irq_domain->fwnode; 321 322 irq_domain_remove(vmd->irq_domain); 323 irq_domain_free_fwnode(fn); 324 } 325 } 326 327 static char __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus, 328 unsigned int devfn, int reg, int len) 329 { 330 char __iomem *addr = vmd->cfgbar + 331 ((bus->number - vmd->busn_start) << 20) + 332 (devfn << 12) + reg; 333 334 if ((addr - vmd->cfgbar) + len >= 335 resource_size(&vmd->dev->resource[VMD_CFGBAR])) 336 return NULL; 337 338 return addr; 339 } 340 341 /* 342 * CPU may deadlock if config space is not serialized on some versions of this 343 * hardware, so all config space access is done under a spinlock. 344 */ 345 static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg, 346 int len, u32 *value) 347 { 348 struct vmd_dev *vmd = vmd_from_bus(bus); 349 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len); 350 unsigned long flags; 351 int ret = 0; 352 353 if (!addr) 354 return -EFAULT; 355 356 spin_lock_irqsave(&vmd->cfg_lock, flags); 357 switch (len) { 358 case 1: 359 *value = readb(addr); 360 break; 361 case 2: 362 *value = readw(addr); 363 break; 364 case 4: 365 *value = readl(addr); 366 break; 367 default: 368 ret = -EINVAL; 369 break; 370 } 371 spin_unlock_irqrestore(&vmd->cfg_lock, flags); 372 return ret; 373 } 374 375 /* 376 * VMD h/w converts non-posted config writes to posted memory writes. The 377 * read-back in this function forces the completion so it returns only after 378 * the config space was written, as expected. 379 */ 380 static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg, 381 int len, u32 value) 382 { 383 struct vmd_dev *vmd = vmd_from_bus(bus); 384 char __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len); 385 unsigned long flags; 386 int ret = 0; 387 388 if (!addr) 389 return -EFAULT; 390 391 spin_lock_irqsave(&vmd->cfg_lock, flags); 392 switch (len) { 393 case 1: 394 writeb(value, addr); 395 readb(addr); 396 break; 397 case 2: 398 writew(value, addr); 399 readw(addr); 400 break; 401 case 4: 402 writel(value, addr); 403 readl(addr); 404 break; 405 default: 406 ret = -EINVAL; 407 break; 408 } 409 spin_unlock_irqrestore(&vmd->cfg_lock, flags); 410 return ret; 411 } 412 413 static struct pci_ops vmd_ops = { 414 .read = vmd_pci_read, 415 .write = vmd_pci_write, 416 }; 417 418 static void vmd_attach_resources(struct vmd_dev *vmd) 419 { 420 vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1]; 421 vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2]; 422 } 423 424 static void vmd_detach_resources(struct vmd_dev *vmd) 425 { 426 vmd->dev->resource[VMD_MEMBAR1].child = NULL; 427 vmd->dev->resource[VMD_MEMBAR2].child = NULL; 428 } 429 430 /* 431 * VMD domains start at 0x10000 to not clash with ACPI _SEG domains. 432 * Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of which the lower 433 * 16 bits are the PCI Segment Group (domain) number. Other bits are 434 * currently reserved. 435 */ 436 static int vmd_find_free_domain(void) 437 { 438 int domain = 0xffff; 439 struct pci_bus *bus = NULL; 440 441 while ((bus = pci_find_next_bus(bus)) != NULL) 442 domain = max_t(int, domain, pci_domain_nr(bus)); 443 return domain + 1; 444 } 445 446 static int vmd_get_phys_offsets(struct vmd_dev *vmd, bool native_hint, 447 resource_size_t *offset1, 448 resource_size_t *offset2) 449 { 450 struct pci_dev *dev = vmd->dev; 451 u64 phys1, phys2; 452 453 if (native_hint) { 454 u32 vmlock; 455 int ret; 456 457 ret = pci_read_config_dword(dev, PCI_REG_VMLOCK, &vmlock); 458 if (ret || vmlock == ~0) 459 return -ENODEV; 460 461 if (MB2_SHADOW_EN(vmlock)) { 462 void __iomem *membar2; 463 464 membar2 = pci_iomap(dev, VMD_MEMBAR2, 0); 465 if (!membar2) 466 return -ENOMEM; 467 phys1 = readq(membar2 + MB2_SHADOW_OFFSET); 468 phys2 = readq(membar2 + MB2_SHADOW_OFFSET + 8); 469 pci_iounmap(dev, membar2); 470 } else 471 return 0; 472 } else { 473 /* Hypervisor-Emulated Vendor-Specific Capability */ 474 int pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); 475 u32 reg, regu; 476 477 pci_read_config_dword(dev, pos + 4, ®); 478 479 /* "SHDW" */ 480 if (pos && reg == 0x53484457) { 481 pci_read_config_dword(dev, pos + 8, ®); 482 pci_read_config_dword(dev, pos + 12, ®u); 483 phys1 = (u64) regu << 32 | reg; 484 485 pci_read_config_dword(dev, pos + 16, ®); 486 pci_read_config_dword(dev, pos + 20, ®u); 487 phys2 = (u64) regu << 32 | reg; 488 } else 489 return 0; 490 } 491 492 *offset1 = dev->resource[VMD_MEMBAR1].start - 493 (phys1 & PCI_BASE_ADDRESS_MEM_MASK); 494 *offset2 = dev->resource[VMD_MEMBAR2].start - 495 (phys2 & PCI_BASE_ADDRESS_MEM_MASK); 496 497 return 0; 498 } 499 500 static int vmd_get_bus_number_start(struct vmd_dev *vmd) 501 { 502 struct pci_dev *dev = vmd->dev; 503 u16 reg; 504 505 pci_read_config_word(dev, PCI_REG_VMCAP, ®); 506 if (BUS_RESTRICT_CAP(reg)) { 507 pci_read_config_word(dev, PCI_REG_VMCONFIG, ®); 508 509 switch (BUS_RESTRICT_CFG(reg)) { 510 case 0: 511 vmd->busn_start = 0; 512 break; 513 case 1: 514 vmd->busn_start = 128; 515 break; 516 case 2: 517 vmd->busn_start = 224; 518 break; 519 default: 520 pci_err(dev, "Unknown Bus Offset Setting (%d)\n", 521 BUS_RESTRICT_CFG(reg)); 522 return -ENODEV; 523 } 524 } 525 526 return 0; 527 } 528 529 static irqreturn_t vmd_irq(int irq, void *data) 530 { 531 struct vmd_irq_list *irqs = data; 532 struct vmd_irq *vmdirq; 533 int idx; 534 535 idx = srcu_read_lock(&irqs->srcu); 536 list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node) 537 generic_handle_irq(vmdirq->virq); 538 srcu_read_unlock(&irqs->srcu, idx); 539 540 return IRQ_HANDLED; 541 } 542 543 static int vmd_alloc_irqs(struct vmd_dev *vmd) 544 { 545 struct pci_dev *dev = vmd->dev; 546 int i, err; 547 548 vmd->msix_count = pci_msix_vec_count(dev); 549 if (vmd->msix_count < 0) 550 return -ENODEV; 551 552 vmd->msix_count = pci_alloc_irq_vectors(dev, 1, vmd->msix_count, 553 PCI_IRQ_MSIX); 554 if (vmd->msix_count < 0) 555 return vmd->msix_count; 556 557 vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs), 558 GFP_KERNEL); 559 if (!vmd->irqs) 560 return -ENOMEM; 561 562 for (i = 0; i < vmd->msix_count; i++) { 563 err = init_srcu_struct(&vmd->irqs[i].srcu); 564 if (err) 565 return err; 566 567 INIT_LIST_HEAD(&vmd->irqs[i].irq_list); 568 err = devm_request_irq(&dev->dev, pci_irq_vector(dev, i), 569 vmd_irq, IRQF_NO_THREAD, 570 "vmd", &vmd->irqs[i]); 571 if (err) 572 return err; 573 } 574 575 return 0; 576 } 577 578 static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features) 579 { 580 struct pci_sysdata *sd = &vmd->sysdata; 581 struct resource *res; 582 u32 upper_bits; 583 unsigned long flags; 584 LIST_HEAD(resources); 585 resource_size_t offset[2] = {0}; 586 resource_size_t membar2_offset = 0x2000; 587 struct pci_bus *child; 588 int ret; 589 590 /* 591 * Shadow registers may exist in certain VMD device ids which allow 592 * guests to correctly assign host physical addresses to the root ports 593 * and child devices. These registers will either return the host value 594 * or 0, depending on an enable bit in the VMD device. 595 */ 596 if (features & VMD_FEAT_HAS_MEMBAR_SHADOW) { 597 membar2_offset = MB2_SHADOW_OFFSET + MB2_SHADOW_SIZE; 598 ret = vmd_get_phys_offsets(vmd, true, &offset[0], &offset[1]); 599 if (ret) 600 return ret; 601 } else if (features & VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP) { 602 ret = vmd_get_phys_offsets(vmd, false, &offset[0], &offset[1]); 603 if (ret) 604 return ret; 605 } 606 607 /* 608 * Certain VMD devices may have a root port configuration option which 609 * limits the bus range to between 0-127, 128-255, or 224-255 610 */ 611 if (features & VMD_FEAT_HAS_BUS_RESTRICTIONS) { 612 ret = vmd_get_bus_number_start(vmd); 613 if (ret) 614 return ret; 615 } 616 617 res = &vmd->dev->resource[VMD_CFGBAR]; 618 vmd->resources[0] = (struct resource) { 619 .name = "VMD CFGBAR", 620 .start = vmd->busn_start, 621 .end = vmd->busn_start + (resource_size(res) >> 20) - 1, 622 .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED, 623 }; 624 625 /* 626 * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can 627 * put 32-bit resources in the window. 628 * 629 * There's no hardware reason why a 64-bit window *couldn't* 630 * contain a 32-bit resource, but pbus_size_mem() computes the 631 * bridge window size assuming a 64-bit window will contain no 632 * 32-bit resources. __pci_assign_resource() enforces that 633 * artificial restriction to make sure everything will fit. 634 * 635 * The only way we could use a 64-bit non-prefetchable MEMBAR is 636 * if its address is <4GB so that we can convert it to a 32-bit 637 * resource. To be visible to the host OS, all VMD endpoints must 638 * be initially configured by platform BIOS, which includes setting 639 * up these resources. We can assume the device is configured 640 * according to the platform needs. 641 */ 642 res = &vmd->dev->resource[VMD_MEMBAR1]; 643 upper_bits = upper_32_bits(res->end); 644 flags = res->flags & ~IORESOURCE_SIZEALIGN; 645 if (!upper_bits) 646 flags &= ~IORESOURCE_MEM_64; 647 vmd->resources[1] = (struct resource) { 648 .name = "VMD MEMBAR1", 649 .start = res->start, 650 .end = res->end, 651 .flags = flags, 652 .parent = res, 653 }; 654 655 res = &vmd->dev->resource[VMD_MEMBAR2]; 656 upper_bits = upper_32_bits(res->end); 657 flags = res->flags & ~IORESOURCE_SIZEALIGN; 658 if (!upper_bits) 659 flags &= ~IORESOURCE_MEM_64; 660 vmd->resources[2] = (struct resource) { 661 .name = "VMD MEMBAR2", 662 .start = res->start + membar2_offset, 663 .end = res->end, 664 .flags = flags, 665 .parent = res, 666 }; 667 668 sd->vmd_dev = vmd->dev; 669 sd->domain = vmd_find_free_domain(); 670 if (sd->domain < 0) 671 return sd->domain; 672 673 sd->node = pcibus_to_node(vmd->dev->bus); 674 675 ret = vmd_create_irq_domain(vmd); 676 if (ret) 677 return ret; 678 679 /* 680 * Override the irq domain bus token so the domain can be distinguished 681 * from a regular PCI/MSI domain. 682 */ 683 irq_domain_update_bus_token(vmd->irq_domain, DOMAIN_BUS_VMD_MSI); 684 685 pci_add_resource(&resources, &vmd->resources[0]); 686 pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]); 687 pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]); 688 689 vmd->bus = pci_create_root_bus(&vmd->dev->dev, vmd->busn_start, 690 &vmd_ops, sd, &resources); 691 if (!vmd->bus) { 692 pci_free_resource_list(&resources); 693 vmd_remove_irq_domain(vmd); 694 return -ENODEV; 695 } 696 697 vmd_attach_resources(vmd); 698 if (vmd->irq_domain) 699 dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain); 700 701 pci_scan_child_bus(vmd->bus); 702 pci_assign_unassigned_bus_resources(vmd->bus); 703 704 /* 705 * VMD root buses are virtual and don't return true on pci_is_pcie() 706 * and will fail pcie_bus_configure_settings() early. It can instead be 707 * run on each of the real root ports. 708 */ 709 list_for_each_entry(child, &vmd->bus->children, node) 710 pcie_bus_configure_settings(child); 711 712 pci_bus_add_devices(vmd->bus); 713 714 WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj, 715 "domain"), "Can't create symlink to domain\n"); 716 return 0; 717 } 718 719 static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id) 720 { 721 struct vmd_dev *vmd; 722 int err; 723 724 if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20)) 725 return -ENOMEM; 726 727 vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL); 728 if (!vmd) 729 return -ENOMEM; 730 731 vmd->dev = dev; 732 err = pcim_enable_device(dev); 733 if (err < 0) 734 return err; 735 736 vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0); 737 if (!vmd->cfgbar) 738 return -ENOMEM; 739 740 pci_set_master(dev); 741 if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) && 742 dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32))) 743 return -ENODEV; 744 745 err = vmd_alloc_irqs(vmd); 746 if (err) 747 return err; 748 749 spin_lock_init(&vmd->cfg_lock); 750 pci_set_drvdata(dev, vmd); 751 err = vmd_enable_domain(vmd, (unsigned long) id->driver_data); 752 if (err) 753 return err; 754 755 dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n", 756 vmd->sysdata.domain); 757 return 0; 758 } 759 760 static void vmd_cleanup_srcu(struct vmd_dev *vmd) 761 { 762 int i; 763 764 for (i = 0; i < vmd->msix_count; i++) 765 cleanup_srcu_struct(&vmd->irqs[i].srcu); 766 } 767 768 static void vmd_remove(struct pci_dev *dev) 769 { 770 struct vmd_dev *vmd = pci_get_drvdata(dev); 771 772 sysfs_remove_link(&vmd->dev->dev.kobj, "domain"); 773 pci_stop_root_bus(vmd->bus); 774 pci_remove_root_bus(vmd->bus); 775 vmd_cleanup_srcu(vmd); 776 vmd_detach_resources(vmd); 777 vmd_remove_irq_domain(vmd); 778 } 779 780 #ifdef CONFIG_PM_SLEEP 781 static int vmd_suspend(struct device *dev) 782 { 783 struct pci_dev *pdev = to_pci_dev(dev); 784 struct vmd_dev *vmd = pci_get_drvdata(pdev); 785 int i; 786 787 for (i = 0; i < vmd->msix_count; i++) 788 devm_free_irq(dev, pci_irq_vector(pdev, i), &vmd->irqs[i]); 789 790 return 0; 791 } 792 793 static int vmd_resume(struct device *dev) 794 { 795 struct pci_dev *pdev = to_pci_dev(dev); 796 struct vmd_dev *vmd = pci_get_drvdata(pdev); 797 int err, i; 798 799 for (i = 0; i < vmd->msix_count; i++) { 800 err = devm_request_irq(dev, pci_irq_vector(pdev, i), 801 vmd_irq, IRQF_NO_THREAD, 802 "vmd", &vmd->irqs[i]); 803 if (err) 804 return err; 805 } 806 807 return 0; 808 } 809 #endif 810 static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume); 811 812 static const struct pci_device_id vmd_ids[] = { 813 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_201D), 814 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP,}, 815 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_28C0), 816 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW | 817 VMD_FEAT_HAS_BUS_RESTRICTIONS,}, 818 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x467f), 819 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP | 820 VMD_FEAT_HAS_BUS_RESTRICTIONS,}, 821 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4c3d), 822 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP | 823 VMD_FEAT_HAS_BUS_RESTRICTIONS,}, 824 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_9A0B), 825 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP | 826 VMD_FEAT_HAS_BUS_RESTRICTIONS,}, 827 {0,} 828 }; 829 MODULE_DEVICE_TABLE(pci, vmd_ids); 830 831 static struct pci_driver vmd_drv = { 832 .name = "vmd", 833 .id_table = vmd_ids, 834 .probe = vmd_probe, 835 .remove = vmd_remove, 836 .driver = { 837 .pm = &vmd_dev_pm_ops, 838 }, 839 }; 840 module_pci_driver(vmd_drv); 841 842 MODULE_AUTHOR("Intel Corporation"); 843 MODULE_LICENSE("GPL v2"); 844 MODULE_VERSION("0.6"); 845