18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright 2016 Broadcom
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <linux/device.h>
78c2ecf20Sopenharmony_ci#include <linux/io.h>
88c2ecf20Sopenharmony_ci#include <linux/kernel.h>
98c2ecf20Sopenharmony_ci#include <linux/module.h>
108c2ecf20Sopenharmony_ci#include <linux/pci.h>
118c2ecf20Sopenharmony_ci#include <linux/pci-ecam.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci/*
158c2ecf20Sopenharmony_ci * On 64-bit systems, we do a single ioremap for the whole config space
168c2ecf20Sopenharmony_ci * since we have enough virtual address range available.  On 32-bit, we
178c2ecf20Sopenharmony_ci * ioremap the config space for each bus individually.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_cistatic const bool per_bus_mapping = !IS_ENABLED(CONFIG_64BIT);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/*
228c2ecf20Sopenharmony_ci * Create a PCI config space window
238c2ecf20Sopenharmony_ci *  - reserve mem region
248c2ecf20Sopenharmony_ci *  - alloc struct pci_config_window with space for all mappings
258c2ecf20Sopenharmony_ci *  - ioremap the config space
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_cistruct pci_config_window *pci_ecam_create(struct device *dev,
288c2ecf20Sopenharmony_ci		struct resource *cfgres, struct resource *busr,
298c2ecf20Sopenharmony_ci		const struct pci_ecam_ops *ops)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct pci_config_window *cfg;
328c2ecf20Sopenharmony_ci	unsigned int bus_range, bus_range_max, bsz;
338c2ecf20Sopenharmony_ci	struct resource *conflict;
348c2ecf20Sopenharmony_ci	int i, err;
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	if (busr->start > busr->end)
378c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
408c2ecf20Sopenharmony_ci	if (!cfg)
418c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	cfg->parent = dev;
448c2ecf20Sopenharmony_ci	cfg->ops = ops;
458c2ecf20Sopenharmony_ci	cfg->busr.start = busr->start;
468c2ecf20Sopenharmony_ci	cfg->busr.end = busr->end;
478c2ecf20Sopenharmony_ci	cfg->busr.flags = IORESOURCE_BUS;
488c2ecf20Sopenharmony_ci	bus_range = resource_size(&cfg->busr);
498c2ecf20Sopenharmony_ci	bus_range_max = resource_size(cfgres) >> ops->bus_shift;
508c2ecf20Sopenharmony_ci	if (bus_range > bus_range_max) {
518c2ecf20Sopenharmony_ci		bus_range = bus_range_max;
528c2ecf20Sopenharmony_ci		cfg->busr.end = busr->start + bus_range - 1;
538c2ecf20Sopenharmony_ci		dev_warn(dev, "ECAM area %pR can only accommodate %pR (reduced from %pR desired)\n",
548c2ecf20Sopenharmony_ci			 cfgres, &cfg->busr, busr);
558c2ecf20Sopenharmony_ci	}
568c2ecf20Sopenharmony_ci	bsz = 1 << ops->bus_shift;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	cfg->res.start = cfgres->start;
598c2ecf20Sopenharmony_ci	cfg->res.end = cfgres->end;
608c2ecf20Sopenharmony_ci	cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
618c2ecf20Sopenharmony_ci	cfg->res.name = "PCI ECAM";
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	conflict = request_resource_conflict(&iomem_resource, &cfg->res);
648c2ecf20Sopenharmony_ci	if (conflict) {
658c2ecf20Sopenharmony_ci		err = -EBUSY;
668c2ecf20Sopenharmony_ci		dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR\n",
678c2ecf20Sopenharmony_ci			&cfg->res, conflict->name, conflict);
688c2ecf20Sopenharmony_ci		goto err_exit;
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	if (per_bus_mapping) {
728c2ecf20Sopenharmony_ci		cfg->winp = kcalloc(bus_range, sizeof(*cfg->winp), GFP_KERNEL);
738c2ecf20Sopenharmony_ci		if (!cfg->winp)
748c2ecf20Sopenharmony_ci			goto err_exit_malloc;
758c2ecf20Sopenharmony_ci		for (i = 0; i < bus_range; i++) {
768c2ecf20Sopenharmony_ci			cfg->winp[i] =
778c2ecf20Sopenharmony_ci				pci_remap_cfgspace(cfgres->start + i * bsz,
788c2ecf20Sopenharmony_ci						   bsz);
798c2ecf20Sopenharmony_ci			if (!cfg->winp[i])
808c2ecf20Sopenharmony_ci				goto err_exit_iomap;
818c2ecf20Sopenharmony_ci		}
828c2ecf20Sopenharmony_ci	} else {
838c2ecf20Sopenharmony_ci		cfg->win = pci_remap_cfgspace(cfgres->start, bus_range * bsz);
848c2ecf20Sopenharmony_ci		if (!cfg->win)
858c2ecf20Sopenharmony_ci			goto err_exit_iomap;
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	if (ops->init) {
898c2ecf20Sopenharmony_ci		err = ops->init(cfg);
908c2ecf20Sopenharmony_ci		if (err)
918c2ecf20Sopenharmony_ci			goto err_exit;
928c2ecf20Sopenharmony_ci	}
938c2ecf20Sopenharmony_ci	dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
948c2ecf20Sopenharmony_ci	return cfg;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cierr_exit_iomap:
978c2ecf20Sopenharmony_ci	dev_err(dev, "ECAM ioremap failed\n");
988c2ecf20Sopenharmony_cierr_exit_malloc:
998c2ecf20Sopenharmony_ci	err = -ENOMEM;
1008c2ecf20Sopenharmony_cierr_exit:
1018c2ecf20Sopenharmony_ci	pci_ecam_free(cfg);
1028c2ecf20Sopenharmony_ci	return ERR_PTR(err);
1038c2ecf20Sopenharmony_ci}
1048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_ecam_create);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_civoid pci_ecam_free(struct pci_config_window *cfg)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	int i;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	if (per_bus_mapping) {
1118c2ecf20Sopenharmony_ci		if (cfg->winp) {
1128c2ecf20Sopenharmony_ci			for (i = 0; i < resource_size(&cfg->busr); i++)
1138c2ecf20Sopenharmony_ci				if (cfg->winp[i])
1148c2ecf20Sopenharmony_ci					iounmap(cfg->winp[i]);
1158c2ecf20Sopenharmony_ci			kfree(cfg->winp);
1168c2ecf20Sopenharmony_ci		}
1178c2ecf20Sopenharmony_ci	} else {
1188c2ecf20Sopenharmony_ci		if (cfg->win)
1198c2ecf20Sopenharmony_ci			iounmap(cfg->win);
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci	if (cfg->res.parent)
1228c2ecf20Sopenharmony_ci		release_resource(&cfg->res);
1238c2ecf20Sopenharmony_ci	kfree(cfg);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_ecam_free);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/*
1288c2ecf20Sopenharmony_ci * Function to implement the pci_ops ->map_bus method
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_civoid __iomem *pci_ecam_map_bus(struct pci_bus *bus, unsigned int devfn,
1318c2ecf20Sopenharmony_ci			       int where)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	struct pci_config_window *cfg = bus->sysdata;
1348c2ecf20Sopenharmony_ci	unsigned int devfn_shift = cfg->ops->bus_shift - 8;
1358c2ecf20Sopenharmony_ci	unsigned int busn = bus->number;
1368c2ecf20Sopenharmony_ci	void __iomem *base;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (busn < cfg->busr.start || busn > cfg->busr.end)
1398c2ecf20Sopenharmony_ci		return NULL;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	busn -= cfg->busr.start;
1428c2ecf20Sopenharmony_ci	if (per_bus_mapping)
1438c2ecf20Sopenharmony_ci		base = cfg->winp[busn];
1448c2ecf20Sopenharmony_ci	else
1458c2ecf20Sopenharmony_ci		base = cfg->win + (busn << cfg->ops->bus_shift);
1468c2ecf20Sopenharmony_ci	return base + (devfn << devfn_shift) + where;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_ecam_map_bus);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/* ECAM ops */
1518c2ecf20Sopenharmony_ciconst struct pci_ecam_ops pci_generic_ecam_ops = {
1528c2ecf20Sopenharmony_ci	.bus_shift	= 20,
1538c2ecf20Sopenharmony_ci	.pci_ops	= {
1548c2ecf20Sopenharmony_ci		.map_bus	= pci_ecam_map_bus,
1558c2ecf20Sopenharmony_ci		.read		= pci_generic_config_read,
1568c2ecf20Sopenharmony_ci		.write		= pci_generic_config_write,
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci};
1598c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(pci_generic_ecam_ops);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
1628c2ecf20Sopenharmony_ci/* ECAM ops for 32-bit access only (non-compliant) */
1638c2ecf20Sopenharmony_ciconst struct pci_ecam_ops pci_32b_ops = {
1648c2ecf20Sopenharmony_ci	.bus_shift	= 20,
1658c2ecf20Sopenharmony_ci	.pci_ops	= {
1668c2ecf20Sopenharmony_ci		.map_bus	= pci_ecam_map_bus,
1678c2ecf20Sopenharmony_ci		.read		= pci_generic_config_read32,
1688c2ecf20Sopenharmony_ci		.write		= pci_generic_config_write32,
1698c2ecf20Sopenharmony_ci	}
1708c2ecf20Sopenharmony_ci};
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci/* ECAM ops for 32-bit read only (non-compliant) */
1738c2ecf20Sopenharmony_ciconst struct pci_ecam_ops pci_32b_read_ops = {
1748c2ecf20Sopenharmony_ci	.bus_shift	= 20,
1758c2ecf20Sopenharmony_ci	.pci_ops	= {
1768c2ecf20Sopenharmony_ci		.map_bus	= pci_ecam_map_bus,
1778c2ecf20Sopenharmony_ci		.read		= pci_generic_config_read32,
1788c2ecf20Sopenharmony_ci		.write		= pci_generic_config_write,
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci#endif
182