18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * New-style PCI core.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2004 - 2009  Paul Mundt
68c2ecf20Sopenharmony_ci * Copyright (c) 2002  M. R. Brown
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Modelled after arch/mips/pci/pci.c:
98c2ecf20Sopenharmony_ci *  Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci#include <linux/kernel.h>
128c2ecf20Sopenharmony_ci#include <linux/mm.h>
138c2ecf20Sopenharmony_ci#include <linux/pci.h>
148c2ecf20Sopenharmony_ci#include <linux/init.h>
158c2ecf20Sopenharmony_ci#include <linux/types.h>
168c2ecf20Sopenharmony_ci#include <linux/io.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
198c2ecf20Sopenharmony_ci#include <linux/export.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciunsigned long PCIBIOS_MIN_IO = 0x0000;
228c2ecf20Sopenharmony_ciunsigned long PCIBIOS_MIN_MEM = 0;
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * The PCI controller list.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistatic struct pci_channel *hose_head, **hose_tail = &hose_head;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int pci_initialized;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic void pcibios_scanbus(struct pci_channel *hose)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	static int next_busno;
348c2ecf20Sopenharmony_ci	static int need_domain_info;
358c2ecf20Sopenharmony_ci	LIST_HEAD(resources);
368c2ecf20Sopenharmony_ci	struct resource *res;
378c2ecf20Sopenharmony_ci	resource_size_t offset;
388c2ecf20Sopenharmony_ci	int i, ret;
398c2ecf20Sopenharmony_ci	struct pci_host_bridge *bridge;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	bridge = pci_alloc_host_bridge(0);
428c2ecf20Sopenharmony_ci	if (!bridge)
438c2ecf20Sopenharmony_ci		return;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	for (i = 0; i < hose->nr_resources; i++) {
468c2ecf20Sopenharmony_ci		res = hose->resources + i;
478c2ecf20Sopenharmony_ci		offset = 0;
488c2ecf20Sopenharmony_ci		if (res->flags & IORESOURCE_DISABLED)
498c2ecf20Sopenharmony_ci			continue;
508c2ecf20Sopenharmony_ci		if (res->flags & IORESOURCE_IO)
518c2ecf20Sopenharmony_ci			offset = hose->io_offset;
528c2ecf20Sopenharmony_ci		else if (res->flags & IORESOURCE_MEM)
538c2ecf20Sopenharmony_ci			offset = hose->mem_offset;
548c2ecf20Sopenharmony_ci		pci_add_resource_offset(&resources, res, offset);
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	list_splice_init(&resources, &bridge->windows);
588c2ecf20Sopenharmony_ci	bridge->dev.parent = NULL;
598c2ecf20Sopenharmony_ci	bridge->sysdata = hose;
608c2ecf20Sopenharmony_ci	bridge->busnr = next_busno;
618c2ecf20Sopenharmony_ci	bridge->ops = hose->pci_ops;
628c2ecf20Sopenharmony_ci	bridge->swizzle_irq = pci_common_swizzle;
638c2ecf20Sopenharmony_ci	bridge->map_irq = pcibios_map_platform_irq;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ret = pci_scan_root_bus_bridge(bridge);
668c2ecf20Sopenharmony_ci	if (ret) {
678c2ecf20Sopenharmony_ci		pci_free_host_bridge(bridge);
688c2ecf20Sopenharmony_ci		return;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	hose->bus = bridge->bus;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	need_domain_info = need_domain_info || hose->index;
748c2ecf20Sopenharmony_ci	hose->need_domain_info = need_domain_info;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	next_busno = hose->bus->busn_res.end + 1;
778c2ecf20Sopenharmony_ci	/* Don't allow 8-bit bus number overflow inside the hose -
788c2ecf20Sopenharmony_ci	   reserve some space for bridges. */
798c2ecf20Sopenharmony_ci	if (next_busno > 224) {
808c2ecf20Sopenharmony_ci		next_busno = 0;
818c2ecf20Sopenharmony_ci		need_domain_info = 1;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	pci_bus_size_bridges(hose->bus);
858c2ecf20Sopenharmony_ci	pci_bus_assign_resources(hose->bus);
868c2ecf20Sopenharmony_ci	pci_bus_add_devices(hose->bus);
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/*
908c2ecf20Sopenharmony_ci * This interrupt-safe spinlock protects all accesses to PCI
918c2ecf20Sopenharmony_ci * configuration space.
928c2ecf20Sopenharmony_ci */
938c2ecf20Sopenharmony_ciDEFINE_RAW_SPINLOCK(pci_config_lock);
948c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(pci_scan_mutex);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ciint register_pci_controller(struct pci_channel *hose)
978c2ecf20Sopenharmony_ci{
988c2ecf20Sopenharmony_ci	int i;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	for (i = 0; i < hose->nr_resources; i++) {
1018c2ecf20Sopenharmony_ci		struct resource *res = hose->resources + i;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci		if (res->flags & IORESOURCE_DISABLED)
1048c2ecf20Sopenharmony_ci			continue;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci		if (res->flags & IORESOURCE_IO) {
1078c2ecf20Sopenharmony_ci			if (request_resource(&ioport_resource, res) < 0)
1088c2ecf20Sopenharmony_ci				goto out;
1098c2ecf20Sopenharmony_ci		} else {
1108c2ecf20Sopenharmony_ci			if (request_resource(&iomem_resource, res) < 0)
1118c2ecf20Sopenharmony_ci				goto out;
1128c2ecf20Sopenharmony_ci		}
1138c2ecf20Sopenharmony_ci	}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	*hose_tail = hose;
1168c2ecf20Sopenharmony_ci	hose_tail = &hose->next;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	/*
1198c2ecf20Sopenharmony_ci	 * Do not panic here but later - this might happen before console init.
1208c2ecf20Sopenharmony_ci	 */
1218c2ecf20Sopenharmony_ci	if (!hose->io_map_base) {
1228c2ecf20Sopenharmony_ci		pr_warn("registering PCI controller with io_map_base unset\n");
1238c2ecf20Sopenharmony_ci	}
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	/*
1268c2ecf20Sopenharmony_ci	 * Setup the ERR/PERR and SERR timers, if available.
1278c2ecf20Sopenharmony_ci	 */
1288c2ecf20Sopenharmony_ci	pcibios_enable_timers(hose);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	/*
1318c2ecf20Sopenharmony_ci	 * Scan the bus if it is register after the PCI subsystem
1328c2ecf20Sopenharmony_ci	 * initialization.
1338c2ecf20Sopenharmony_ci	 */
1348c2ecf20Sopenharmony_ci	if (pci_initialized) {
1358c2ecf20Sopenharmony_ci		mutex_lock(&pci_scan_mutex);
1368c2ecf20Sopenharmony_ci		pcibios_scanbus(hose);
1378c2ecf20Sopenharmony_ci		mutex_unlock(&pci_scan_mutex);
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	return 0;
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ciout:
1438c2ecf20Sopenharmony_ci	for (--i; i >= 0; i--)
1448c2ecf20Sopenharmony_ci		release_resource(&hose->resources[i]);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	pr_warn("Skipping PCI bus scan due to resource conflict\n");
1478c2ecf20Sopenharmony_ci	return -1;
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_cistatic int __init pcibios_init(void)
1518c2ecf20Sopenharmony_ci{
1528c2ecf20Sopenharmony_ci	struct pci_channel *hose;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* Scan all of the recorded PCI controllers.  */
1558c2ecf20Sopenharmony_ci	for (hose = hose_head; hose; hose = hose->next)
1568c2ecf20Sopenharmony_ci		pcibios_scanbus(hose);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	pci_initialized = 1;
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return 0;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_cisubsys_initcall(pcibios_init);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/*
1658c2ecf20Sopenharmony_ci * We need to avoid collisions with `mirrored' VGA ports
1668c2ecf20Sopenharmony_ci * and other strange ISA hardware, so we always want the
1678c2ecf20Sopenharmony_ci * addresses to be allocated in the 0x000-0x0ff region
1688c2ecf20Sopenharmony_ci * modulo 0x400.
1698c2ecf20Sopenharmony_ci */
1708c2ecf20Sopenharmony_ciresource_size_t pcibios_align_resource(void *data, const struct resource *res,
1718c2ecf20Sopenharmony_ci				resource_size_t size, resource_size_t align)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct pci_dev *dev = data;
1748c2ecf20Sopenharmony_ci	struct pci_channel *hose = dev->sysdata;
1758c2ecf20Sopenharmony_ci	resource_size_t start = res->start;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (res->flags & IORESOURCE_IO) {
1788c2ecf20Sopenharmony_ci		if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
1798c2ecf20Sopenharmony_ci			start = PCIBIOS_MIN_IO + hose->resources[0].start;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		/*
1828c2ecf20Sopenharmony_ci                 * Put everything into 0x00-0xff region modulo 0x400.
1838c2ecf20Sopenharmony_ci		 */
1848c2ecf20Sopenharmony_ci		if (start & 0x300)
1858c2ecf20Sopenharmony_ci			start = (start + 0x3ff) & ~0x3ff;
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	return start;
1898c2ecf20Sopenharmony_ci}
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cistatic void __init
1928c2ecf20Sopenharmony_cipcibios_bus_report_status_early(struct pci_channel *hose,
1938c2ecf20Sopenharmony_ci				int top_bus, int current_bus,
1948c2ecf20Sopenharmony_ci				unsigned int status_mask, int warn)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	unsigned int pci_devfn;
1978c2ecf20Sopenharmony_ci	u16 status;
1988c2ecf20Sopenharmony_ci	int ret;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
2018c2ecf20Sopenharmony_ci		if (PCI_FUNC(pci_devfn))
2028c2ecf20Sopenharmony_ci			continue;
2038c2ecf20Sopenharmony_ci		ret = early_read_config_word(hose, top_bus, current_bus,
2048c2ecf20Sopenharmony_ci					     pci_devfn, PCI_STATUS, &status);
2058c2ecf20Sopenharmony_ci		if (ret != PCIBIOS_SUCCESSFUL)
2068c2ecf20Sopenharmony_ci			continue;
2078c2ecf20Sopenharmony_ci		if (status == 0xffff)
2088c2ecf20Sopenharmony_ci			continue;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		early_write_config_word(hose, top_bus, current_bus,
2118c2ecf20Sopenharmony_ci					pci_devfn, PCI_STATUS,
2128c2ecf20Sopenharmony_ci					status & status_mask);
2138c2ecf20Sopenharmony_ci		if (warn)
2148c2ecf20Sopenharmony_ci			pr_cont("(%02x:%02x: %04X) ", current_bus, pci_devfn,
2158c2ecf20Sopenharmony_ci				status);
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/*
2208c2ecf20Sopenharmony_ci * We can't use pci_find_device() here since we are
2218c2ecf20Sopenharmony_ci * called from interrupt context.
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_cistatic void __ref
2248c2ecf20Sopenharmony_cipcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
2258c2ecf20Sopenharmony_ci			  int warn)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct pci_dev *dev;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	list_for_each_entry(dev, &bus->devices, bus_list) {
2308c2ecf20Sopenharmony_ci		u16 status;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		/*
2338c2ecf20Sopenharmony_ci		 * ignore host bridge - we handle
2348c2ecf20Sopenharmony_ci		 * that separately
2358c2ecf20Sopenharmony_ci		 */
2368c2ecf20Sopenharmony_ci		if (dev->bus->number == 0 && dev->devfn == 0)
2378c2ecf20Sopenharmony_ci			continue;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci		pci_read_config_word(dev, PCI_STATUS, &status);
2408c2ecf20Sopenharmony_ci		if (status == 0xffff)
2418c2ecf20Sopenharmony_ci			continue;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci		if ((status & status_mask) == 0)
2448c2ecf20Sopenharmony_ci			continue;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci		/* clear the status errors */
2478c2ecf20Sopenharmony_ci		pci_write_config_word(dev, PCI_STATUS, status & status_mask);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci		if (warn)
2508c2ecf20Sopenharmony_ci			pr_cont("(%s: %04X) ", pci_name(dev), status);
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	list_for_each_entry(dev, &bus->devices, bus_list)
2548c2ecf20Sopenharmony_ci		if (dev->subordinate)
2558c2ecf20Sopenharmony_ci			pcibios_bus_report_status(dev->subordinate, status_mask, warn);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_civoid __ref pcibios_report_status(unsigned int status_mask, int warn)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct pci_channel *hose;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	for (hose = hose_head; hose; hose = hose->next) {
2638c2ecf20Sopenharmony_ci		if (unlikely(!hose->bus))
2648c2ecf20Sopenharmony_ci			pcibios_bus_report_status_early(hose, hose_head->index,
2658c2ecf20Sopenharmony_ci					hose->index, status_mask, warn);
2668c2ecf20Sopenharmony_ci		else
2678c2ecf20Sopenharmony_ci			pcibios_bus_report_status(hose->bus, status_mask, warn);
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci#ifndef CONFIG_GENERIC_IOMAP
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_civoid __iomem *__pci_ioport_map(struct pci_dev *dev,
2748c2ecf20Sopenharmony_ci			       unsigned long port, unsigned int nr)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct pci_channel *chan = dev->sysdata;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (unlikely(!chan->io_map_base)) {
2798c2ecf20Sopenharmony_ci		chan->io_map_base = sh_io_port_base;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci		if (pci_domains_supported)
2828c2ecf20Sopenharmony_ci			panic("To avoid data corruption io_map_base MUST be "
2838c2ecf20Sopenharmony_ci			      "set with multiple PCI domains.");
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	return (void __iomem *)(chan->io_map_base + port);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_civoid pci_iounmap(struct pci_dev *dev, void __iomem *addr)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	iounmap(addr);
2928c2ecf20Sopenharmony_ci}
2938c2ecf20Sopenharmony_ciEXPORT_SYMBOL(pci_iounmap);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci#endif /* CONFIG_GENERIC_IOMAP */
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciEXPORT_SYMBOL(PCIBIOS_MIN_IO);
2988c2ecf20Sopenharmony_ciEXPORT_SYMBOL(PCIBIOS_MIN_MEM);
299