^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Code borrowed from powerpc/kernel/pci-common.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2014 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of_pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pci-acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/pci-ecam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * Try to assign the IRQ number when probing a new device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int pcibios_alloc_irq(struct pci_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (!acpi_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) acpi_pci_irq_enable(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * raw_pci_read/write - Platform-specific PCI config space access.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int raw_pci_read(unsigned int domain, unsigned int bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned int devfn, int reg, int len, u32 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct pci_bus *b = pci_find_bus(domain, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return PCIBIOS_DEVICE_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) return b->ops->read(b, devfn, reg, len, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int raw_pci_write(unsigned int domain, unsigned int bus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) unsigned int devfn, int reg, int len, u32 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct pci_bus *b = pci_find_bus(domain, bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return PCIBIOS_DEVICE_NOT_FOUND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return b->ops->write(b, devfn, reg, len, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) int pcibus_to_node(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return dev_to_node(&bus->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) EXPORT_SYMBOL(pcibus_to_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #ifdef CONFIG_ACPI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct acpi_pci_generic_root_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct acpi_pci_root_info common;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct pci_config_window *cfg; /* config space mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct pci_config_window *cfg = bus->sysdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct acpi_device *adev = to_acpi_device(cfg->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct acpi_pci_root *root = acpi_driver_data(adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return root->segment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!acpi_disabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) struct pci_config_window *cfg = bridge->bus->sysdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct acpi_device *adev = to_acpi_device(cfg->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct device *bus_dev = &bridge->bus->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) ACPI_COMPANION_SET(&bridge->dev, adev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) set_dev_node(bus_dev, acpi_get_node(acpi_device_handle(adev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct resource_entry *entry, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) status = acpi_pci_probe_root_resources(ci);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (!(entry->res->flags & IORESOURCE_WINDOW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) resource_list_destroy_entry(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * Lookup the bus range for the domain in MCFG, and set up config space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * mapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static struct pci_config_window *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct device *dev = &root->device->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct resource *bus_res = &root->secondary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) u16 seg = root->segment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const struct pci_ecam_ops *ecam_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct resource cfgres;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) struct acpi_device *adev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) struct pci_config_window *cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) adev = acpi_resource_consumer(&cfgres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (adev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dev_name(&adev->dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) &cfgres);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (IS_ERR(cfg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) PTR_ERR(cfg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return cfg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* release_info: free resources allocated by init_info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) struct acpi_pci_generic_root_info *ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ri = container_of(ci, struct acpi_pci_generic_root_info, common);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) pci_ecam_free(ri->cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) kfree(ci->ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) kfree(ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /* Interface called from ACPI code to setup PCI host controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) struct acpi_pci_generic_root_info *ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct pci_bus *bus, *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct acpi_pci_root_ops *root_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct pci_host_bridge *host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ri = kzalloc(sizeof(*ri), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (!ri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) root_ops = kzalloc(sizeof(*root_ops), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (!root_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) kfree(ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) ri->cfg = pci_acpi_setup_ecam_mapping(root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!ri->cfg) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) kfree(ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) kfree(root_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) root_ops->release_info = pci_acpi_generic_release_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) root_ops->prepare_resources = pci_acpi_root_prepare_resources;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) root_ops->pci_ops = (struct pci_ops *)&ri->cfg->ops->pci_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) /* If we must preserve the resource configuration, claim now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) host = pci_find_host_bridge(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (host->preserve_config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pci_bus_claim_resources(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * Assign whatever was left unassigned. If we didn't claim above,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * this will reassign everything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) pci_assign_unassigned_root_bus_resources(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) list_for_each_entry(child, &bus->children, node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) pcie_bus_configure_settings(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) void pcibios_add_bus(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) acpi_pci_add_bus(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) void pcibios_remove_bus(struct pci_bus *bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) acpi_pci_remove_bus(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) #endif