1 // SPDX-License-Identifier: GPL-2.0-only
3 * Code borrowed from powerpc/kernel/pci-common.c
5 * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
6 * Copyright (C) 2014 ARM Ltd.
9 #include <linux/acpi.h>
10 #include <linux/init.h>
12 #include <linux/kernel.h>
14 #include <linux/of_pci.h>
15 #include <linux/of_platform.h>
16 #include <linux/pci.h>
17 #include <linux/pci-acpi.h>
18 #include <linux/pci-ecam.h>
19 #include <linux/slab.h>
23 * Try to assign the IRQ number when probing a new device
25 int pcibios_alloc_irq(struct pci_dev
*dev
)
28 acpi_pci_irq_enable(dev
);
35 * raw_pci_read/write - Platform-specific PCI config space access.
37 int raw_pci_read(unsigned int domain
, unsigned int bus
,
38 unsigned int devfn
, int reg
, int len
, u32
*val
)
40 struct pci_bus
*b
= pci_find_bus(domain
, bus
);
43 return PCIBIOS_DEVICE_NOT_FOUND
;
44 return b
->ops
->read(b
, devfn
, reg
, len
, val
);
47 int raw_pci_write(unsigned int domain
, unsigned int bus
,
48 unsigned int devfn
, int reg
, int len
, u32 val
)
50 struct pci_bus
*b
= pci_find_bus(domain
, bus
);
53 return PCIBIOS_DEVICE_NOT_FOUND
;
54 return b
->ops
->write(b
, devfn
, reg
, len
, val
);
59 int pcibus_to_node(struct pci_bus
*bus
)
61 return dev_to_node(&bus
->dev
);
63 EXPORT_SYMBOL(pcibus_to_node
);
69 struct acpi_pci_generic_root_info
{
70 struct acpi_pci_root_info common
;
71 struct pci_config_window
*cfg
; /* config space mapping */
74 int acpi_pci_bus_find_domain_nr(struct pci_bus
*bus
)
76 struct pci_config_window
*cfg
= bus
->sysdata
;
77 struct acpi_device
*adev
= to_acpi_device(cfg
->parent
);
78 struct acpi_pci_root
*root
= acpi_driver_data(adev
);
83 int pcibios_root_bridge_prepare(struct pci_host_bridge
*bridge
)
86 struct pci_config_window
*cfg
= bridge
->bus
->sysdata
;
87 struct acpi_device
*adev
= to_acpi_device(cfg
->parent
);
88 struct device
*bus_dev
= &bridge
->bus
->dev
;
90 ACPI_COMPANION_SET(&bridge
->dev
, adev
);
91 set_dev_node(bus_dev
, acpi_get_node(acpi_device_handle(adev
)));
97 static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info
*ci
)
99 struct resource_entry
*entry
, *tmp
;
102 status
= acpi_pci_probe_root_resources(ci
);
103 resource_list_for_each_entry_safe(entry
, tmp
, &ci
->resources
) {
104 if (!(entry
->res
->flags
& IORESOURCE_WINDOW
))
105 resource_list_destroy_entry(entry
);
111 * Lookup the bus range for the domain in MCFG, and set up config space
114 static struct pci_config_window
*
115 pci_acpi_setup_ecam_mapping(struct acpi_pci_root
*root
)
117 struct device
*dev
= &root
->device
->dev
;
118 struct resource
*bus_res
= &root
->secondary
;
119 u16 seg
= root
->segment
;
120 struct pci_ecam_ops
*ecam_ops
;
121 struct resource cfgres
;
122 struct acpi_device
*adev
;
123 struct pci_config_window
*cfg
;
126 ret
= pci_mcfg_lookup(root
, &cfgres
, &ecam_ops
);
128 dev_err(dev
, "%04x:%pR ECAM region not found\n", seg
, bus_res
);
132 adev
= acpi_resource_consumer(&cfgres
);
134 dev_info(dev
, "ECAM area %pR reserved by %s\n", &cfgres
,
135 dev_name(&adev
->dev
));
137 dev_warn(dev
, FW_BUG
"ECAM area %pR not reserved in ACPI namespace\n",
140 cfg
= pci_ecam_create(dev
, &cfgres
, bus_res
, ecam_ops
);
142 dev_err(dev
, "%04x:%pR error %ld mapping ECAM\n", seg
, bus_res
,
150 /* release_info: free resources allocated by init_info */
151 static void pci_acpi_generic_release_info(struct acpi_pci_root_info
*ci
)
153 struct acpi_pci_generic_root_info
*ri
;
155 ri
= container_of(ci
, struct acpi_pci_generic_root_info
, common
);
156 pci_ecam_free(ri
->cfg
);
161 /* Interface called from ACPI code to setup PCI host controller */
162 struct pci_bus
*pci_acpi_scan_root(struct acpi_pci_root
*root
)
164 struct acpi_pci_generic_root_info
*ri
;
165 struct pci_bus
*bus
, *child
;
166 struct acpi_pci_root_ops
*root_ops
;
167 struct pci_host_bridge
*host
;
169 ri
= kzalloc(sizeof(*ri
), GFP_KERNEL
);
173 root_ops
= kzalloc(sizeof(*root_ops
), GFP_KERNEL
);
179 ri
->cfg
= pci_acpi_setup_ecam_mapping(root
);
186 root_ops
->release_info
= pci_acpi_generic_release_info
;
187 root_ops
->prepare_resources
= pci_acpi_root_prepare_resources
;
188 root_ops
->pci_ops
= &ri
->cfg
->ops
->pci_ops
;
189 bus
= acpi_pci_root_create(root
, root_ops
, &ri
->common
, ri
->cfg
);
193 /* If we must preserve the resource configuration, claim now */
194 host
= pci_find_host_bridge(bus
);
195 if (host
->preserve_config
)
196 pci_bus_claim_resources(bus
);
199 * Assign whatever was left unassigned. If we didn't claim above,
200 * this will reassign everything.
202 pci_assign_unassigned_root_bus_resources(bus
);
204 list_for_each_entry(child
, &bus
->children
, node
)
205 pcie_bus_configure_settings(child
);
210 void pcibios_add_bus(struct pci_bus
*bus
)
212 acpi_pci_add_bus(bus
);
215 void pcibios_remove_bus(struct pci_bus
*bus
)
217 acpi_pci_remove_bus(bus
);