Merge branch 'remotes/lorenzo/pci/hyper-v'
[platform/kernel/linux-rpi.git] / drivers / pci / probe.c
index 2752046..d9fc02a 100644 (file)
@@ -19,6 +19,7 @@
 #include <linux/hypervisor.h>
 #include <linux/irqdomain.h>
 #include <linux/pm_runtime.h>
+#include <linux/bitfield.h>
 #include "pci.h"
 
 #define CARDBUS_LATENCY_TIMER  176     /* secondary latency timer */
@@ -594,6 +595,7 @@ static void pci_init_host_bridge(struct pci_host_bridge *bridge)
        bridge->native_pme = 1;
        bridge->native_ltr = 1;
        bridge->native_dpc = 1;
+       bridge->domain_nr = PCI_DOMAIN_NR_NOT_SET;
 
        device_initialize(&bridge->dev);
 }
@@ -828,11 +830,15 @@ static struct irq_domain *pci_host_bridge_msi_domain(struct pci_bus *bus)
 {
        struct irq_domain *d;
 
+       /* If the host bridge driver sets a MSI domain of the bridge, use it */
+       d = dev_get_msi_domain(bus->bridge);
+
        /*
         * Any firmware interface that can resolve the msi_domain
         * should be called from here.
         */
-       d = pci_host_bridge_of_msi_domain(bus);
+       if (!d)
+               d = pci_host_bridge_of_msi_domain(bus);
        if (!d)
                d = pci_host_bridge_acpi_msi_domain(bus);
 
@@ -898,7 +904,10 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
        bus->ops = bridge->ops;
        bus->number = bus->busn_res.start = bridge->busnr;
 #ifdef CONFIG_PCI_DOMAINS_GENERIC
-       bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
+       if (bridge->domain_nr == PCI_DOMAIN_NR_NOT_SET)
+               bus->domain_nr = pci_bus_find_domain_nr(bus, parent);
+       else
+               bus->domain_nr = bridge->domain_nr;
 #endif
 
        b = pci_find_bus(pci_domain_nr(bus), bridge->busnr);
@@ -1498,8 +1507,8 @@ void set_pcie_port_type(struct pci_dev *pdev)
        pdev->pcie_cap = pos;
        pci_read_config_word(pdev, pos + PCI_EXP_FLAGS, &reg16);
        pdev->pcie_flags_reg = reg16;
-       pci_read_config_word(pdev, pos + PCI_EXP_DEVCAP, &reg16);
-       pdev->pcie_mpss = reg16 & PCI_EXP_DEVCAP_PAYLOAD;
+       pci_read_config_dword(pdev, pos + PCI_EXP_DEVCAP, &pdev->devcap);
+       pdev->pcie_mpss = FIELD_GET(PCI_EXP_DEVCAP_PAYLOAD, pdev->devcap);
 
        parent = pci_upstream_bridge(pdev);
        if (!parent)
@@ -1576,6 +1585,26 @@ static void set_pcie_untrusted(struct pci_dev *dev)
                dev->untrusted = true;
 }
 
+static void pci_set_removable(struct pci_dev *dev)
+{
+       struct pci_dev *parent = pci_upstream_bridge(dev);
+
+       /*
+        * We (only) consider everything downstream from an external_facing
+        * device to be removable by the user. We're mainly concerned with
+        * consumer platforms with user accessible thunderbolt ports that are
+        * vulnerable to DMA attacks, and we expect those ports to be marked by
+        * the firmware as external_facing. Devices in traditional hotplug
+        * slots can technically be removed, but the expectation is that unless
+        * the port is marked with external_facing, such devices are less
+        * accessible to user / may not be removed by end user, and thus not
+        * exposed as "removable" to userspace.
+        */
+       if (parent &&
+           (parent->external_facing || dev_is_removable(&parent->dev)))
+               dev_set_removable(&dev->dev, DEVICE_REMOVABLE);
+}
+
 /**
  * pci_ext_cfg_is_aliased - Is ext config space just an alias of std config?
  * @dev: PCI device
@@ -1789,6 +1818,9 @@ int pci_setup_device(struct pci_dev *dev)
        dev->error_state = pci_channel_io_normal;
        set_pcie_port_type(dev);
 
+       pci_set_of_node(dev);
+       pci_set_acpi_fwnode(dev);
+
        pci_dev_assign_slot(dev);
 
        /*
@@ -1823,6 +1855,8 @@ int pci_setup_device(struct pci_dev *dev)
        /* Early fixups, before probing the BARs */
        pci_fixup_device(pci_fixup_early, dev);
 
+       pci_set_removable(dev);
+
        pci_info(dev, "[%04x:%04x] type %02x class %#08x\n",
                 dev->vendor, dev->device, dev->hdr_type, dev->class);
 
@@ -1924,6 +1958,7 @@ int pci_setup_device(struct pci_dev *dev)
        default:                                    /* unknown header */
                pci_err(dev, "unknown header type %02x, ignoring device\n",
                        dev->hdr_type);
+               pci_release_of_node(dev);
                return -EIO;
 
        bad:
@@ -2203,7 +2238,6 @@ static void pci_release_capabilities(struct pci_dev *dev)
 {
        pci_aer_exit(dev);
        pci_rcec_exit(dev);
-       pci_vpd_release(dev);
        pci_iov_release(dev);
        pci_free_cap_save_buffers(dev);
 }
@@ -2227,6 +2261,7 @@ static void pci_release_dev(struct device *dev)
        pci_bus_put(pci_dev->bus);
        kfree(pci_dev->driver_override);
        bitmap_free(pci_dev->dma_alias_mask);
+       dev_dbg(dev, "device released\n");
        kfree(pci_dev);
 }
 
@@ -2351,10 +2386,7 @@ static struct pci_dev *pci_scan_device(struct pci_bus *bus, int devfn)
        dev->vendor = l & 0xffff;
        dev->device = (l >> 16) & 0xffff;
 
-       pci_set_of_node(dev);
-
        if (pci_setup_device(dev)) {
-               pci_release_of_node(dev);
                pci_bus_put(dev->bus);
                kfree(dev);
                return NULL;
@@ -2405,9 +2437,7 @@ static void pci_init_capabilities(struct pci_dev *dev)
        pci_rcec_init(dev);             /* Root Complex Event Collector */
 
        pcie_report_downtraining(dev);
-
-       if (pci_probe_reset_function(dev) == 0)
-               dev->reset_fn = 1;
+       pci_init_reset_methods(dev);
 }
 
 /*