1 // SPDX-License-Identifier: GPL-2.0
3 * Volume Management Device driver
4 * Copyright (c) 2015, Intel Corporation.
7 #include <linux/device.h>
8 #include <linux/interrupt.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/msi.h>
13 #include <linux/pci.h>
14 #include <linux/pci-acpi.h>
15 #include <linux/pci-ecam.h>
16 #include <linux/srcu.h>
17 #include <linux/rculist.h>
18 #include <linux/rcupdate.h>
20 #include <asm/irqdomain.h>
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 VMCONFIG_MSI_REMAP 0x2
31 #define PCI_REG_VMLOCK 0x70
32 #define MB2_SHADOW_EN(vmlock) (vmlock & 0x2)
34 #define MB2_SHADOW_OFFSET 0x2000
35 #define MB2_SHADOW_SIZE 16
39 * Device may contain registers which hint the physical location of the
40 * membars, in order to allow proper address translation during
41 * resource assignment to enable guest virtualization
43 VMD_FEAT_HAS_MEMBAR_SHADOW = (1 << 0),
46 * Device may provide root port configuration information which limits
49 VMD_FEAT_HAS_BUS_RESTRICTIONS = (1 << 1),
52 * Device contains physical location shadow registers in
53 * vendor-specific capability space
55 VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP = (1 << 2),
58 * Device may use MSI-X vector 0 for software triggering and will not
59 * be used for MSI remapping
61 VMD_FEAT_OFFSET_FIRST_VECTOR = (1 << 3),
64 * Device can bypass remapping MSI-X transactions into its MSI-X table,
65 * avoiding the requirement of a VMD MSI domain for child device
68 VMD_FEAT_CAN_BYPASS_MSI_REMAP = (1 << 4),
71 static DEFINE_IDA(vmd_instance_ida);
74 * Lock for manipulating VMD IRQ lists.
76 static DEFINE_RAW_SPINLOCK(list_lock);
79 * struct vmd_irq - private data to map driver IRQ to the VMD shared vector
80 * @node: list item for parent traversal.
81 * @irq: back pointer to parent.
82 * @enabled: true if driver enabled IRQ
83 * @virq: the virtual IRQ value provided to the requesting driver.
85 * Every MSI/MSI-X IRQ requested for a device in a VMD domain will be mapped to
86 * a VMD IRQ using this structure.
89 struct list_head node;
90 struct vmd_irq_list *irq;
96 * struct vmd_irq_list - list of driver requested IRQs mapping to a VMD vector
97 * @irq_list: the list of irq's the VMD one demuxes to.
98 * @srcu: SRCU struct for local synchronization.
99 * @count: number of child IRQs assigned to this vector; used to track
101 * @virq: The underlying VMD Linux interrupt number
103 struct vmd_irq_list {
104 struct list_head irq_list;
105 struct srcu_struct srcu;
114 void __iomem *cfgbar;
117 struct vmd_irq_list *irqs;
119 struct pci_sysdata sysdata;
120 struct resource resources[3];
121 struct irq_domain *irq_domain;
129 static inline struct vmd_dev *vmd_from_bus(struct pci_bus *bus)
131 return container_of(bus->sysdata, struct vmd_dev, sysdata);
134 static inline unsigned int index_from_irqs(struct vmd_dev *vmd,
135 struct vmd_irq_list *irqs)
137 return irqs - vmd->irqs;
141 * Drivers managing a device in a VMD domain allocate their own IRQs as before,
142 * but the MSI entry for the hardware it's driving will be programmed with a
143 * destination ID for the VMD MSI-X table. The VMD muxes interrupts in its
144 * domain into one of its own, and the VMD driver de-muxes these for the
145 * handlers sharing that VMD IRQ. The vmd irq_domain provides the operations
146 * and irq_chip to set this up.
148 static void vmd_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
150 struct vmd_irq *vmdirq = data->chip_data;
151 struct vmd_irq_list *irq = vmdirq->irq;
152 struct vmd_dev *vmd = irq_data_get_irq_handler_data(data);
154 memset(msg, 0, sizeof(*msg));
155 msg->address_hi = X86_MSI_BASE_ADDRESS_HIGH;
156 msg->arch_addr_lo.base_address = X86_MSI_BASE_ADDRESS_LOW;
157 msg->arch_addr_lo.destid_0_7 = index_from_irqs(vmd, irq);
161 * We rely on MSI_FLAG_USE_DEF_CHIP_OPS to set the IRQ mask/unmask ops.
163 static void vmd_irq_enable(struct irq_data *data)
165 struct vmd_irq *vmdirq = data->chip_data;
168 raw_spin_lock_irqsave(&list_lock, flags);
169 WARN_ON(vmdirq->enabled);
170 list_add_tail_rcu(&vmdirq->node, &vmdirq->irq->irq_list);
171 vmdirq->enabled = true;
172 raw_spin_unlock_irqrestore(&list_lock, flags);
174 data->chip->irq_unmask(data);
177 static void vmd_irq_disable(struct irq_data *data)
179 struct vmd_irq *vmdirq = data->chip_data;
182 data->chip->irq_mask(data);
184 raw_spin_lock_irqsave(&list_lock, flags);
185 if (vmdirq->enabled) {
186 list_del_rcu(&vmdirq->node);
187 vmdirq->enabled = false;
189 raw_spin_unlock_irqrestore(&list_lock, flags);
193 * XXX: Stubbed until we develop acceptable way to not create conflicts with
194 * other devices sharing the same vector.
196 static int vmd_irq_set_affinity(struct irq_data *data,
197 const struct cpumask *dest, bool force)
202 static struct irq_chip vmd_msi_controller = {
204 .irq_enable = vmd_irq_enable,
205 .irq_disable = vmd_irq_disable,
206 .irq_compose_msi_msg = vmd_compose_msi_msg,
207 .irq_set_affinity = vmd_irq_set_affinity,
210 static irq_hw_number_t vmd_get_hwirq(struct msi_domain_info *info,
211 msi_alloc_info_t *arg)
217 * XXX: We can be even smarter selecting the best IRQ once we solve the
220 static struct vmd_irq_list *vmd_next_irq(struct vmd_dev *vmd, struct msi_desc *desc)
225 if (vmd->msix_count == 1 + vmd->first_vec)
226 return &vmd->irqs[vmd->first_vec];
229 * White list for fast-interrupt handlers. All others will share the
230 * "slow" interrupt vector.
232 switch (msi_desc_to_pci_dev(desc)->class) {
233 case PCI_CLASS_STORAGE_EXPRESS:
236 return &vmd->irqs[vmd->first_vec];
239 raw_spin_lock_irqsave(&list_lock, flags);
240 best = vmd->first_vec + 1;
241 for (i = best; i < vmd->msix_count; i++)
242 if (vmd->irqs[i].count < vmd->irqs[best].count)
244 vmd->irqs[best].count++;
245 raw_spin_unlock_irqrestore(&list_lock, flags);
247 return &vmd->irqs[best];
250 static int vmd_msi_init(struct irq_domain *domain, struct msi_domain_info *info,
251 unsigned int virq, irq_hw_number_t hwirq,
252 msi_alloc_info_t *arg)
254 struct msi_desc *desc = arg->desc;
255 struct vmd_dev *vmd = vmd_from_bus(msi_desc_to_pci_dev(desc)->bus);
256 struct vmd_irq *vmdirq = kzalloc(sizeof(*vmdirq), GFP_KERNEL);
261 INIT_LIST_HEAD(&vmdirq->node);
262 vmdirq->irq = vmd_next_irq(vmd, desc);
265 irq_domain_set_info(domain, virq, vmdirq->irq->virq, info->chip, vmdirq,
266 handle_untracked_irq, vmd, NULL);
270 static void vmd_msi_free(struct irq_domain *domain,
271 struct msi_domain_info *info, unsigned int virq)
273 struct vmd_irq *vmdirq = irq_get_chip_data(virq);
276 synchronize_srcu(&vmdirq->irq->srcu);
278 /* XXX: Potential optimization to rebalance */
279 raw_spin_lock_irqsave(&list_lock, flags);
280 vmdirq->irq->count--;
281 raw_spin_unlock_irqrestore(&list_lock, flags);
286 static int vmd_msi_prepare(struct irq_domain *domain, struct device *dev,
287 int nvec, msi_alloc_info_t *arg)
289 struct pci_dev *pdev = to_pci_dev(dev);
290 struct vmd_dev *vmd = vmd_from_bus(pdev->bus);
292 if (nvec > vmd->msix_count)
293 return vmd->msix_count;
295 memset(arg, 0, sizeof(*arg));
299 static void vmd_set_desc(msi_alloc_info_t *arg, struct msi_desc *desc)
304 static struct msi_domain_ops vmd_msi_domain_ops = {
305 .get_hwirq = vmd_get_hwirq,
306 .msi_init = vmd_msi_init,
307 .msi_free = vmd_msi_free,
308 .msi_prepare = vmd_msi_prepare,
309 .set_desc = vmd_set_desc,
312 static struct msi_domain_info vmd_msi_domain_info = {
313 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
315 .ops = &vmd_msi_domain_ops,
316 .chip = &vmd_msi_controller,
319 static void vmd_set_msi_remapping(struct vmd_dev *vmd, bool enable)
323 pci_read_config_word(vmd->dev, PCI_REG_VMCONFIG, ®);
324 reg = enable ? (reg & ~VMCONFIG_MSI_REMAP) :
325 (reg | VMCONFIG_MSI_REMAP);
326 pci_write_config_word(vmd->dev, PCI_REG_VMCONFIG, reg);
329 static int vmd_create_irq_domain(struct vmd_dev *vmd)
331 struct fwnode_handle *fn;
333 fn = irq_domain_alloc_named_id_fwnode("VMD-MSI", vmd->sysdata.domain);
337 vmd->irq_domain = pci_msi_create_irq_domain(fn, &vmd_msi_domain_info, NULL);
338 if (!vmd->irq_domain) {
339 irq_domain_free_fwnode(fn);
346 static void vmd_remove_irq_domain(struct vmd_dev *vmd)
349 * Some production BIOS won't enable remapping between soft reboots.
350 * Ensure remapping is restored before unloading the driver.
352 if (!vmd->msix_count)
353 vmd_set_msi_remapping(vmd, true);
355 if (vmd->irq_domain) {
356 struct fwnode_handle *fn = vmd->irq_domain->fwnode;
358 irq_domain_remove(vmd->irq_domain);
359 irq_domain_free_fwnode(fn);
363 static void __iomem *vmd_cfg_addr(struct vmd_dev *vmd, struct pci_bus *bus,
364 unsigned int devfn, int reg, int len)
366 unsigned int busnr_ecam = bus->number - vmd->busn_start;
367 u32 offset = PCIE_ECAM_OFFSET(busnr_ecam, devfn, reg);
369 if (offset + len >= resource_size(&vmd->dev->resource[VMD_CFGBAR]))
372 return vmd->cfgbar + offset;
376 * CPU may deadlock if config space is not serialized on some versions of this
377 * hardware, so all config space access is done under a spinlock.
379 static int vmd_pci_read(struct pci_bus *bus, unsigned int devfn, int reg,
382 struct vmd_dev *vmd = vmd_from_bus(bus);
383 void __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
390 spin_lock_irqsave(&vmd->cfg_lock, flags);
393 *value = readb(addr);
396 *value = readw(addr);
399 *value = readl(addr);
405 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
410 * VMD h/w converts non-posted config writes to posted memory writes. The
411 * read-back in this function forces the completion so it returns only after
412 * the config space was written, as expected.
414 static int vmd_pci_write(struct pci_bus *bus, unsigned int devfn, int reg,
417 struct vmd_dev *vmd = vmd_from_bus(bus);
418 void __iomem *addr = vmd_cfg_addr(vmd, bus, devfn, reg, len);
425 spin_lock_irqsave(&vmd->cfg_lock, flags);
443 spin_unlock_irqrestore(&vmd->cfg_lock, flags);
447 static struct pci_ops vmd_ops = {
448 .read = vmd_pci_read,
449 .write = vmd_pci_write,
453 static struct acpi_device *vmd_acpi_find_companion(struct pci_dev *pci_dev)
455 struct pci_host_bridge *bridge;
458 if (pci_dev->bus->ops != &vmd_ops)
461 bridge = pci_find_host_bridge(pci_dev->bus);
462 busnr = pci_dev->bus->number - bridge->bus->number;
464 * The address computation below is only applicable to relative bus
470 addr = (busnr << 24) | ((u32)pci_dev->devfn << 16) | 0x8000FFFFU;
472 dev_dbg(&pci_dev->dev, "Looking for ACPI companion (address 0x%x)\n",
475 return acpi_find_child_device(ACPI_COMPANION(bridge->dev.parent), addr,
479 static bool hook_installed;
481 static void vmd_acpi_begin(void)
483 if (pci_acpi_set_companion_lookup_hook(vmd_acpi_find_companion))
486 hook_installed = true;
489 static void vmd_acpi_end(void)
494 pci_acpi_clear_companion_lookup_hook();
495 hook_installed = false;
498 static inline void vmd_acpi_begin(void) { }
499 static inline void vmd_acpi_end(void) { }
500 #endif /* CONFIG_ACPI */
502 static void vmd_domain_reset(struct vmd_dev *vmd)
504 u16 bus, max_buses = resource_size(&vmd->resources[0]);
505 u8 dev, functions, fn, hdr_type;
508 for (bus = 0; bus < max_buses; bus++) {
509 for (dev = 0; dev < 32; dev++) {
510 base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
511 PCI_DEVFN(dev, 0), 0);
513 hdr_type = readb(base + PCI_HEADER_TYPE) &
514 PCI_HEADER_TYPE_MASK;
516 functions = (hdr_type & 0x80) ? 8 : 1;
517 for (fn = 0; fn < functions; fn++) {
518 base = vmd->cfgbar + PCIE_ECAM_OFFSET(bus,
519 PCI_DEVFN(dev, fn), 0);
521 hdr_type = readb(base + PCI_HEADER_TYPE) &
522 PCI_HEADER_TYPE_MASK;
524 if (hdr_type != PCI_HEADER_TYPE_BRIDGE ||
525 (readw(base + PCI_CLASS_DEVICE) !=
526 PCI_CLASS_BRIDGE_PCI))
529 memset_io(base + PCI_IO_BASE, 0,
530 PCI_ROM_ADDRESS1 - PCI_IO_BASE);
536 static void vmd_attach_resources(struct vmd_dev *vmd)
538 vmd->dev->resource[VMD_MEMBAR1].child = &vmd->resources[1];
539 vmd->dev->resource[VMD_MEMBAR2].child = &vmd->resources[2];
542 static void vmd_detach_resources(struct vmd_dev *vmd)
544 vmd->dev->resource[VMD_MEMBAR1].child = NULL;
545 vmd->dev->resource[VMD_MEMBAR2].child = NULL;
549 * VMD domains start at 0x10000 to not clash with ACPI _SEG domains.
550 * Per ACPI r6.0, sec 6.5.6, _SEG returns an integer, of which the lower
551 * 16 bits are the PCI Segment Group (domain) number. Other bits are
552 * currently reserved.
554 static int vmd_find_free_domain(void)
557 struct pci_bus *bus = NULL;
559 while ((bus = pci_find_next_bus(bus)) != NULL)
560 domain = max_t(int, domain, pci_domain_nr(bus));
564 static int vmd_get_phys_offsets(struct vmd_dev *vmd, bool native_hint,
565 resource_size_t *offset1,
566 resource_size_t *offset2)
568 struct pci_dev *dev = vmd->dev;
575 ret = pci_read_config_dword(dev, PCI_REG_VMLOCK, &vmlock);
576 if (ret || PCI_POSSIBLE_ERROR(vmlock))
579 if (MB2_SHADOW_EN(vmlock)) {
580 void __iomem *membar2;
582 membar2 = pci_iomap(dev, VMD_MEMBAR2, 0);
585 phys1 = readq(membar2 + MB2_SHADOW_OFFSET);
586 phys2 = readq(membar2 + MB2_SHADOW_OFFSET + 8);
587 pci_iounmap(dev, membar2);
591 /* Hypervisor-Emulated Vendor-Specific Capability */
592 int pos = pci_find_capability(dev, PCI_CAP_ID_VNDR);
595 pci_read_config_dword(dev, pos + 4, ®);
598 if (pos && reg == 0x53484457) {
599 pci_read_config_dword(dev, pos + 8, ®);
600 pci_read_config_dword(dev, pos + 12, ®u);
601 phys1 = (u64) regu << 32 | reg;
603 pci_read_config_dword(dev, pos + 16, ®);
604 pci_read_config_dword(dev, pos + 20, ®u);
605 phys2 = (u64) regu << 32 | reg;
610 *offset1 = dev->resource[VMD_MEMBAR1].start -
611 (phys1 & PCI_BASE_ADDRESS_MEM_MASK);
612 *offset2 = dev->resource[VMD_MEMBAR2].start -
613 (phys2 & PCI_BASE_ADDRESS_MEM_MASK);
618 static int vmd_get_bus_number_start(struct vmd_dev *vmd)
620 struct pci_dev *dev = vmd->dev;
623 pci_read_config_word(dev, PCI_REG_VMCAP, ®);
624 if (BUS_RESTRICT_CAP(reg)) {
625 pci_read_config_word(dev, PCI_REG_VMCONFIG, ®);
627 switch (BUS_RESTRICT_CFG(reg)) {
632 vmd->busn_start = 128;
635 vmd->busn_start = 224;
638 pci_err(dev, "Unknown Bus Offset Setting (%d)\n",
639 BUS_RESTRICT_CFG(reg));
647 static irqreturn_t vmd_irq(int irq, void *data)
649 struct vmd_irq_list *irqs = data;
650 struct vmd_irq *vmdirq;
653 idx = srcu_read_lock(&irqs->srcu);
654 list_for_each_entry_rcu(vmdirq, &irqs->irq_list, node)
655 generic_handle_irq(vmdirq->virq);
656 srcu_read_unlock(&irqs->srcu, idx);
661 static int vmd_alloc_irqs(struct vmd_dev *vmd)
663 struct pci_dev *dev = vmd->dev;
666 vmd->msix_count = pci_msix_vec_count(dev);
667 if (vmd->msix_count < 0)
670 vmd->msix_count = pci_alloc_irq_vectors(dev, vmd->first_vec + 1,
671 vmd->msix_count, PCI_IRQ_MSIX);
672 if (vmd->msix_count < 0)
673 return vmd->msix_count;
675 vmd->irqs = devm_kcalloc(&dev->dev, vmd->msix_count, sizeof(*vmd->irqs),
680 for (i = 0; i < vmd->msix_count; i++) {
681 err = init_srcu_struct(&vmd->irqs[i].srcu);
685 INIT_LIST_HEAD(&vmd->irqs[i].irq_list);
686 vmd->irqs[i].virq = pci_irq_vector(dev, i);
687 err = devm_request_irq(&dev->dev, vmd->irqs[i].virq,
688 vmd_irq, IRQF_NO_THREAD,
689 vmd->name, &vmd->irqs[i]);
698 * Since VMD is an aperture to regular PCIe root ports, only allow it to
699 * control features that the OS is allowed to control on the physical PCI bus.
701 static void vmd_copy_host_bridge_flags(struct pci_host_bridge *root_bridge,
702 struct pci_host_bridge *vmd_bridge)
704 vmd_bridge->native_pcie_hotplug = root_bridge->native_pcie_hotplug;
705 vmd_bridge->native_shpc_hotplug = root_bridge->native_shpc_hotplug;
706 vmd_bridge->native_aer = root_bridge->native_aer;
707 vmd_bridge->native_pme = root_bridge->native_pme;
708 vmd_bridge->native_ltr = root_bridge->native_ltr;
709 vmd_bridge->native_dpc = root_bridge->native_dpc;
712 static int vmd_enable_domain(struct vmd_dev *vmd, unsigned long features)
714 struct pci_sysdata *sd = &vmd->sysdata;
715 struct resource *res;
718 LIST_HEAD(resources);
719 resource_size_t offset[2] = {0};
720 resource_size_t membar2_offset = 0x2000;
721 struct pci_bus *child;
725 * Shadow registers may exist in certain VMD device ids which allow
726 * guests to correctly assign host physical addresses to the root ports
727 * and child devices. These registers will either return the host value
728 * or 0, depending on an enable bit in the VMD device.
730 if (features & VMD_FEAT_HAS_MEMBAR_SHADOW) {
731 membar2_offset = MB2_SHADOW_OFFSET + MB2_SHADOW_SIZE;
732 ret = vmd_get_phys_offsets(vmd, true, &offset[0], &offset[1]);
735 } else if (features & VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP) {
736 ret = vmd_get_phys_offsets(vmd, false, &offset[0], &offset[1]);
742 * Certain VMD devices may have a root port configuration option which
743 * limits the bus range to between 0-127, 128-255, or 224-255
745 if (features & VMD_FEAT_HAS_BUS_RESTRICTIONS) {
746 ret = vmd_get_bus_number_start(vmd);
751 res = &vmd->dev->resource[VMD_CFGBAR];
752 vmd->resources[0] = (struct resource) {
753 .name = "VMD CFGBAR",
754 .start = vmd->busn_start,
755 .end = vmd->busn_start + (resource_size(res) >> 20) - 1,
756 .flags = IORESOURCE_BUS | IORESOURCE_PCI_FIXED,
760 * If the window is below 4GB, clear IORESOURCE_MEM_64 so we can
761 * put 32-bit resources in the window.
763 * There's no hardware reason why a 64-bit window *couldn't*
764 * contain a 32-bit resource, but pbus_size_mem() computes the
765 * bridge window size assuming a 64-bit window will contain no
766 * 32-bit resources. __pci_assign_resource() enforces that
767 * artificial restriction to make sure everything will fit.
769 * The only way we could use a 64-bit non-prefetchable MEMBAR is
770 * if its address is <4GB so that we can convert it to a 32-bit
771 * resource. To be visible to the host OS, all VMD endpoints must
772 * be initially configured by platform BIOS, which includes setting
773 * up these resources. We can assume the device is configured
774 * according to the platform needs.
776 res = &vmd->dev->resource[VMD_MEMBAR1];
777 upper_bits = upper_32_bits(res->end);
778 flags = res->flags & ~IORESOURCE_SIZEALIGN;
780 flags &= ~IORESOURCE_MEM_64;
781 vmd->resources[1] = (struct resource) {
782 .name = "VMD MEMBAR1",
789 res = &vmd->dev->resource[VMD_MEMBAR2];
790 upper_bits = upper_32_bits(res->end);
791 flags = res->flags & ~IORESOURCE_SIZEALIGN;
793 flags &= ~IORESOURCE_MEM_64;
794 vmd->resources[2] = (struct resource) {
795 .name = "VMD MEMBAR2",
796 .start = res->start + membar2_offset,
802 sd->vmd_dev = vmd->dev;
803 sd->domain = vmd_find_free_domain();
807 sd->node = pcibus_to_node(vmd->dev->bus);
810 * Currently MSI remapping must be enabled in guest passthrough mode
811 * due to some missing interrupt remapping plumbing. This is probably
812 * acceptable because the guest is usually CPU-limited and MSI
813 * remapping doesn't become a performance bottleneck.
815 if (!(features & VMD_FEAT_CAN_BYPASS_MSI_REMAP) ||
816 offset[0] || offset[1]) {
817 ret = vmd_alloc_irqs(vmd);
821 vmd_set_msi_remapping(vmd, true);
823 ret = vmd_create_irq_domain(vmd);
828 * Override the IRQ domain bus token so the domain can be
829 * distinguished from a regular PCI/MSI domain.
831 irq_domain_update_bus_token(vmd->irq_domain, DOMAIN_BUS_VMD_MSI);
833 vmd_set_msi_remapping(vmd, false);
836 pci_add_resource(&resources, &vmd->resources[0]);
837 pci_add_resource_offset(&resources, &vmd->resources[1], offset[0]);
838 pci_add_resource_offset(&resources, &vmd->resources[2], offset[1]);
840 vmd->bus = pci_create_root_bus(&vmd->dev->dev, vmd->busn_start,
841 &vmd_ops, sd, &resources);
843 pci_free_resource_list(&resources);
844 vmd_remove_irq_domain(vmd);
848 vmd_copy_host_bridge_flags(pci_find_host_bridge(vmd->dev->bus),
849 to_pci_host_bridge(vmd->bus->bridge));
851 vmd_attach_resources(vmd);
853 dev_set_msi_domain(&vmd->bus->dev, vmd->irq_domain);
855 dev_set_msi_domain(&vmd->bus->dev,
856 dev_get_msi_domain(&vmd->dev->dev));
860 pci_scan_child_bus(vmd->bus);
861 vmd_domain_reset(vmd);
862 list_for_each_entry(child, &vmd->bus->children, node)
863 pci_reset_bus(child->self);
864 pci_assign_unassigned_bus_resources(vmd->bus);
867 * VMD root buses are virtual and don't return true on pci_is_pcie()
868 * and will fail pcie_bus_configure_settings() early. It can instead be
869 * run on each of the real root ports.
871 list_for_each_entry(child, &vmd->bus->children, node)
872 pcie_bus_configure_settings(child);
874 pci_bus_add_devices(vmd->bus);
878 WARN(sysfs_create_link(&vmd->dev->dev.kobj, &vmd->bus->dev.kobj,
879 "domain"), "Can't create symlink to domain\n");
883 static int vmd_probe(struct pci_dev *dev, const struct pci_device_id *id)
885 unsigned long features = (unsigned long) id->driver_data;
889 if (resource_size(&dev->resource[VMD_CFGBAR]) < (1 << 20))
892 vmd = devm_kzalloc(&dev->dev, sizeof(*vmd), GFP_KERNEL);
897 vmd->instance = ida_simple_get(&vmd_instance_ida, 0, 0, GFP_KERNEL);
898 if (vmd->instance < 0)
899 return vmd->instance;
901 vmd->name = devm_kasprintf(&dev->dev, GFP_KERNEL, "vmd%d",
905 goto out_release_instance;
908 err = pcim_enable_device(dev);
910 goto out_release_instance;
912 vmd->cfgbar = pcim_iomap(dev, VMD_CFGBAR, 0);
915 goto out_release_instance;
919 if (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(64)) &&
920 dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32))) {
922 goto out_release_instance;
925 if (features & VMD_FEAT_OFFSET_FIRST_VECTOR)
928 spin_lock_init(&vmd->cfg_lock);
929 pci_set_drvdata(dev, vmd);
930 err = vmd_enable_domain(vmd, features);
932 goto out_release_instance;
934 dev_info(&vmd->dev->dev, "Bound to PCI domain %04x\n",
935 vmd->sysdata.domain);
938 out_release_instance:
939 ida_simple_remove(&vmd_instance_ida, vmd->instance);
943 static void vmd_cleanup_srcu(struct vmd_dev *vmd)
947 for (i = 0; i < vmd->msix_count; i++)
948 cleanup_srcu_struct(&vmd->irqs[i].srcu);
951 static void vmd_remove(struct pci_dev *dev)
953 struct vmd_dev *vmd = pci_get_drvdata(dev);
955 sysfs_remove_link(&vmd->dev->dev.kobj, "domain");
956 pci_stop_root_bus(vmd->bus);
957 pci_remove_root_bus(vmd->bus);
958 vmd_cleanup_srcu(vmd);
959 vmd_detach_resources(vmd);
960 vmd_remove_irq_domain(vmd);
961 ida_simple_remove(&vmd_instance_ida, vmd->instance);
964 #ifdef CONFIG_PM_SLEEP
965 static int vmd_suspend(struct device *dev)
967 struct pci_dev *pdev = to_pci_dev(dev);
968 struct vmd_dev *vmd = pci_get_drvdata(pdev);
971 for (i = 0; i < vmd->msix_count; i++)
972 devm_free_irq(dev, vmd->irqs[i].virq, &vmd->irqs[i]);
977 static int vmd_resume(struct device *dev)
979 struct pci_dev *pdev = to_pci_dev(dev);
980 struct vmd_dev *vmd = pci_get_drvdata(pdev);
983 for (i = 0; i < vmd->msix_count; i++) {
984 err = devm_request_irq(dev, vmd->irqs[i].virq,
985 vmd_irq, IRQF_NO_THREAD,
986 vmd->name, &vmd->irqs[i]);
994 static SIMPLE_DEV_PM_OPS(vmd_dev_pm_ops, vmd_suspend, vmd_resume);
996 static const struct pci_device_id vmd_ids[] = {
997 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_201D),
998 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP,},
999 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_28C0),
1000 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW |
1001 VMD_FEAT_HAS_BUS_RESTRICTIONS |
1002 VMD_FEAT_CAN_BYPASS_MSI_REMAP,},
1003 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x467f),
1004 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP |
1005 VMD_FEAT_HAS_BUS_RESTRICTIONS |
1006 VMD_FEAT_OFFSET_FIRST_VECTOR,},
1007 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4c3d),
1008 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP |
1009 VMD_FEAT_HAS_BUS_RESTRICTIONS |
1010 VMD_FEAT_OFFSET_FIRST_VECTOR,},
1011 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xa77f),
1012 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP |
1013 VMD_FEAT_HAS_BUS_RESTRICTIONS |
1014 VMD_FEAT_OFFSET_FIRST_VECTOR,},
1015 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x7d0b),
1016 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP |
1017 VMD_FEAT_HAS_BUS_RESTRICTIONS |
1018 VMD_FEAT_OFFSET_FIRST_VECTOR,},
1019 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0xad0b),
1020 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP |
1021 VMD_FEAT_HAS_BUS_RESTRICTIONS |
1022 VMD_FEAT_OFFSET_FIRST_VECTOR,},
1023 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_VMD_9A0B),
1024 .driver_data = VMD_FEAT_HAS_MEMBAR_SHADOW_VSCAP |
1025 VMD_FEAT_HAS_BUS_RESTRICTIONS |
1026 VMD_FEAT_OFFSET_FIRST_VECTOR,},
1029 MODULE_DEVICE_TABLE(pci, vmd_ids);
1031 static struct pci_driver vmd_drv = {
1033 .id_table = vmd_ids,
1035 .remove = vmd_remove,
1037 .pm = &vmd_dev_pm_ops,
1040 module_pci_driver(vmd_drv);
1042 MODULE_AUTHOR("Intel Corporation");
1043 MODULE_LICENSE("GPL v2");
1044 MODULE_VERSION("0.6");