xref: /linux/drivers/pci/controller/dwc/pcie-designware-host.c (revision 8bcca26585585ae4b44d25d30f351ad0afa4976b)
16e0832faSShawn Lin // SPDX-License-Identifier: GPL-2.0
26e0832faSShawn Lin /*
36e0832faSShawn Lin  * Synopsys DesignWare PCIe host controller driver
46e0832faSShawn Lin  *
56e0832faSShawn Lin  * Copyright (C) 2013 Samsung Electronics Co., Ltd.
67ecd4a81SAlexander A. Klimov  *		https://www.samsung.com
76e0832faSShawn Lin  *
86e0832faSShawn Lin  * Author: Jingoo Han <jg1.han@samsung.com>
96e0832faSShawn Lin  */
106e0832faSShawn Lin 
116e0832faSShawn Lin #include <linux/irqchip/chained_irq.h>
126e0832faSShawn Lin #include <linux/irqdomain.h>
13bbd8810dSKrzysztof Wilczynski #include <linux/msi.h>
146e0832faSShawn Lin #include <linux/of_address.h>
156e0832faSShawn Lin #include <linux/of_pci.h>
166e0832faSShawn Lin #include <linux/pci_regs.h>
176e0832faSShawn Lin #include <linux/platform_device.h>
186e0832faSShawn Lin 
196e0832faSShawn Lin #include "../../pci.h"
206e0832faSShawn Lin #include "pcie-designware.h"
216e0832faSShawn Lin 
226e0832faSShawn Lin static struct pci_ops dw_pcie_ops;
23c2b0c098SRob Herring static struct pci_ops dw_child_pcie_ops;
246e0832faSShawn Lin 
256e0832faSShawn Lin static void dw_msi_ack_irq(struct irq_data *d)
266e0832faSShawn Lin {
276e0832faSShawn Lin 	irq_chip_ack_parent(d);
286e0832faSShawn Lin }
296e0832faSShawn Lin 
306e0832faSShawn Lin static void dw_msi_mask_irq(struct irq_data *d)
316e0832faSShawn Lin {
326e0832faSShawn Lin 	pci_msi_mask_irq(d);
336e0832faSShawn Lin 	irq_chip_mask_parent(d);
346e0832faSShawn Lin }
356e0832faSShawn Lin 
366e0832faSShawn Lin static void dw_msi_unmask_irq(struct irq_data *d)
376e0832faSShawn Lin {
386e0832faSShawn Lin 	pci_msi_unmask_irq(d);
396e0832faSShawn Lin 	irq_chip_unmask_parent(d);
406e0832faSShawn Lin }
416e0832faSShawn Lin 
426e0832faSShawn Lin static struct irq_chip dw_pcie_msi_irq_chip = {
436e0832faSShawn Lin 	.name = "PCI-MSI",
446e0832faSShawn Lin 	.irq_ack = dw_msi_ack_irq,
456e0832faSShawn Lin 	.irq_mask = dw_msi_mask_irq,
466e0832faSShawn Lin 	.irq_unmask = dw_msi_unmask_irq,
476e0832faSShawn Lin };
486e0832faSShawn Lin 
496e0832faSShawn Lin static struct msi_domain_info dw_pcie_msi_domain_info = {
506e0832faSShawn Lin 	.flags	= (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
516e0832faSShawn Lin 		   MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
526e0832faSShawn Lin 	.chip	= &dw_pcie_msi_irq_chip,
536e0832faSShawn Lin };
546e0832faSShawn Lin 
556e0832faSShawn Lin /* MSI int handler */
566e0832faSShawn Lin irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
576e0832faSShawn Lin {
586e0832faSShawn Lin 	int i, pos, irq;
591137e61dSNiklas Cassel 	unsigned long val;
601137e61dSNiklas Cassel 	u32 status, num_ctrls;
616e0832faSShawn Lin 	irqreturn_t ret = IRQ_NONE;
62f81c770dSRob Herring 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
636e0832faSShawn Lin 
646e0832faSShawn Lin 	num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
656e0832faSShawn Lin 
666e0832faSShawn Lin 	for (i = 0; i < num_ctrls; i++) {
67f81c770dSRob Herring 		status = dw_pcie_readl_dbi(pci, PCIE_MSI_INTR0_STATUS +
68f81c770dSRob Herring 					   (i * MSI_REG_CTRL_BLOCK_SIZE));
691137e61dSNiklas Cassel 		if (!status)
706e0832faSShawn Lin 			continue;
716e0832faSShawn Lin 
726e0832faSShawn Lin 		ret = IRQ_HANDLED;
731137e61dSNiklas Cassel 		val = status;
746e0832faSShawn Lin 		pos = 0;
751137e61dSNiklas Cassel 		while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
766e0832faSShawn Lin 					    pos)) != MAX_MSI_IRQS_PER_CTRL) {
776e0832faSShawn Lin 			irq = irq_find_mapping(pp->irq_domain,
786e0832faSShawn Lin 					       (i * MAX_MSI_IRQS_PER_CTRL) +
796e0832faSShawn Lin 					       pos);
806e0832faSShawn Lin 			generic_handle_irq(irq);
816e0832faSShawn Lin 			pos++;
826e0832faSShawn Lin 		}
836e0832faSShawn Lin 	}
846e0832faSShawn Lin 
856e0832faSShawn Lin 	return ret;
866e0832faSShawn Lin }
876e0832faSShawn Lin 
886e0832faSShawn Lin /* Chained MSI interrupt service routine */
896e0832faSShawn Lin static void dw_chained_msi_isr(struct irq_desc *desc)
906e0832faSShawn Lin {
916e0832faSShawn Lin 	struct irq_chip *chip = irq_desc_get_chip(desc);
926e0832faSShawn Lin 	struct pcie_port *pp;
936e0832faSShawn Lin 
946e0832faSShawn Lin 	chained_irq_enter(chip, desc);
956e0832faSShawn Lin 
966e0832faSShawn Lin 	pp = irq_desc_get_handler_data(desc);
976e0832faSShawn Lin 	dw_handle_msi_irq(pp);
986e0832faSShawn Lin 
996e0832faSShawn Lin 	chained_irq_exit(chip, desc);
1006e0832faSShawn Lin }
1016e0832faSShawn Lin 
10259ea68b3SGustavo Pimentel static void dw_pci_setup_msi_msg(struct irq_data *d, struct msi_msg *msg)
1036e0832faSShawn Lin {
10459ea68b3SGustavo Pimentel 	struct pcie_port *pp = irq_data_get_irq_chip_data(d);
1056e0832faSShawn Lin 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1066e0832faSShawn Lin 	u64 msi_target;
1076e0832faSShawn Lin 
1086e0832faSShawn Lin 	msi_target = (u64)pp->msi_data;
1096e0832faSShawn Lin 
1106e0832faSShawn Lin 	msg->address_lo = lower_32_bits(msi_target);
1116e0832faSShawn Lin 	msg->address_hi = upper_32_bits(msi_target);
1126e0832faSShawn Lin 
11359ea68b3SGustavo Pimentel 	msg->data = d->hwirq;
1146e0832faSShawn Lin 
1156e0832faSShawn Lin 	dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
11659ea68b3SGustavo Pimentel 		(int)d->hwirq, msg->address_hi, msg->address_lo);
1176e0832faSShawn Lin }
1186e0832faSShawn Lin 
119fd5288a3SGustavo Pimentel static int dw_pci_msi_set_affinity(struct irq_data *d,
1206e0832faSShawn Lin 				   const struct cpumask *mask, bool force)
1216e0832faSShawn Lin {
1226e0832faSShawn Lin 	return -EINVAL;
1236e0832faSShawn Lin }
1246e0832faSShawn Lin 
12540e9892eSGustavo Pimentel static void dw_pci_bottom_mask(struct irq_data *d)
1266e0832faSShawn Lin {
12740e9892eSGustavo Pimentel 	struct pcie_port *pp = irq_data_get_irq_chip_data(d);
128f81c770dSRob Herring 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1296e0832faSShawn Lin 	unsigned int res, bit, ctrl;
1306e0832faSShawn Lin 	unsigned long flags;
1316e0832faSShawn Lin 
1326e0832faSShawn Lin 	raw_spin_lock_irqsave(&pp->lock, flags);
1336e0832faSShawn Lin 
13440e9892eSGustavo Pimentel 	ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
1356e0832faSShawn Lin 	res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
13640e9892eSGustavo Pimentel 	bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
1376e0832faSShawn Lin 
13865772257SGustavo Pimentel 	pp->irq_mask[ctrl] |= BIT(bit);
139f81c770dSRob Herring 	dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
1406e0832faSShawn Lin 
1416e0832faSShawn Lin 	raw_spin_unlock_irqrestore(&pp->lock, flags);
1426e0832faSShawn Lin }
1436e0832faSShawn Lin 
14440e9892eSGustavo Pimentel static void dw_pci_bottom_unmask(struct irq_data *d)
1456e0832faSShawn Lin {
14640e9892eSGustavo Pimentel 	struct pcie_port *pp = irq_data_get_irq_chip_data(d);
147f81c770dSRob Herring 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1486e0832faSShawn Lin 	unsigned int res, bit, ctrl;
1496e0832faSShawn Lin 	unsigned long flags;
1506e0832faSShawn Lin 
1516e0832faSShawn Lin 	raw_spin_lock_irqsave(&pp->lock, flags);
1526e0832faSShawn Lin 
15340e9892eSGustavo Pimentel 	ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
1546e0832faSShawn Lin 	res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
15540e9892eSGustavo Pimentel 	bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
1566e0832faSShawn Lin 
15765772257SGustavo Pimentel 	pp->irq_mask[ctrl] &= ~BIT(bit);
158f81c770dSRob Herring 	dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
1596e0832faSShawn Lin 
1606e0832faSShawn Lin 	raw_spin_unlock_irqrestore(&pp->lock, flags);
1616e0832faSShawn Lin }
1626e0832faSShawn Lin 
1636e0832faSShawn Lin static void dw_pci_bottom_ack(struct irq_data *d)
1646e0832faSShawn Lin {
1653f7bb2ecSMarc Zyngier 	struct pcie_port *pp  = irq_data_get_irq_chip_data(d);
166f81c770dSRob Herring 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1673f7bb2ecSMarc Zyngier 	unsigned int res, bit, ctrl;
1686e0832faSShawn Lin 
1693f7bb2ecSMarc Zyngier 	ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
1703f7bb2ecSMarc Zyngier 	res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
1713f7bb2ecSMarc Zyngier 	bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
1726e0832faSShawn Lin 
173f81c770dSRob Herring 	dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_STATUS + res, BIT(bit));
1746e0832faSShawn Lin }
1756e0832faSShawn Lin 
1766e0832faSShawn Lin static struct irq_chip dw_pci_msi_bottom_irq_chip = {
1776e0832faSShawn Lin 	.name = "DWPCI-MSI",
1786e0832faSShawn Lin 	.irq_ack = dw_pci_bottom_ack,
1796e0832faSShawn Lin 	.irq_compose_msi_msg = dw_pci_setup_msi_msg,
1806e0832faSShawn Lin 	.irq_set_affinity = dw_pci_msi_set_affinity,
1816e0832faSShawn Lin 	.irq_mask = dw_pci_bottom_mask,
1826e0832faSShawn Lin 	.irq_unmask = dw_pci_bottom_unmask,
1836e0832faSShawn Lin };
1846e0832faSShawn Lin 
1856e0832faSShawn Lin static int dw_pcie_irq_domain_alloc(struct irq_domain *domain,
1866e0832faSShawn Lin 				    unsigned int virq, unsigned int nr_irqs,
1876e0832faSShawn Lin 				    void *args)
1886e0832faSShawn Lin {
1896e0832faSShawn Lin 	struct pcie_port *pp = domain->host_data;
1906e0832faSShawn Lin 	unsigned long flags;
1916e0832faSShawn Lin 	u32 i;
1926e0832faSShawn Lin 	int bit;
1936e0832faSShawn Lin 
1946e0832faSShawn Lin 	raw_spin_lock_irqsave(&pp->lock, flags);
1956e0832faSShawn Lin 
1966e0832faSShawn Lin 	bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors,
1976e0832faSShawn Lin 				      order_base_2(nr_irqs));
1986e0832faSShawn Lin 
1996e0832faSShawn Lin 	raw_spin_unlock_irqrestore(&pp->lock, flags);
2006e0832faSShawn Lin 
2016e0832faSShawn Lin 	if (bit < 0)
2026e0832faSShawn Lin 		return -ENOSPC;
2036e0832faSShawn Lin 
2046e0832faSShawn Lin 	for (i = 0; i < nr_irqs; i++)
2056e0832faSShawn Lin 		irq_domain_set_info(domain, virq + i, bit + i,
2069f67437bSKishon Vijay Abraham I 				    pp->msi_irq_chip,
2076e0832faSShawn Lin 				    pp, handle_edge_irq,
2086e0832faSShawn Lin 				    NULL, NULL);
2096e0832faSShawn Lin 
2106e0832faSShawn Lin 	return 0;
2116e0832faSShawn Lin }
2126e0832faSShawn Lin 
2136e0832faSShawn Lin static void dw_pcie_irq_domain_free(struct irq_domain *domain,
2146e0832faSShawn Lin 				    unsigned int virq, unsigned int nr_irqs)
2156e0832faSShawn Lin {
2164cfae0f1SGustavo Pimentel 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
21703f8c1b3SKishon Vijay Abraham I 	struct pcie_port *pp = domain->host_data;
2186e0832faSShawn Lin 	unsigned long flags;
2196e0832faSShawn Lin 
2206e0832faSShawn Lin 	raw_spin_lock_irqsave(&pp->lock, flags);
2216e0832faSShawn Lin 
2224cfae0f1SGustavo Pimentel 	bitmap_release_region(pp->msi_irq_in_use, d->hwirq,
2236e0832faSShawn Lin 			      order_base_2(nr_irqs));
2246e0832faSShawn Lin 
2256e0832faSShawn Lin 	raw_spin_unlock_irqrestore(&pp->lock, flags);
2266e0832faSShawn Lin }
2276e0832faSShawn Lin 
2286e0832faSShawn Lin static const struct irq_domain_ops dw_pcie_msi_domain_ops = {
2296e0832faSShawn Lin 	.alloc	= dw_pcie_irq_domain_alloc,
2306e0832faSShawn Lin 	.free	= dw_pcie_irq_domain_free,
2316e0832faSShawn Lin };
2326e0832faSShawn Lin 
2336e0832faSShawn Lin int dw_pcie_allocate_domains(struct pcie_port *pp)
2346e0832faSShawn Lin {
2356e0832faSShawn Lin 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
2366e0832faSShawn Lin 	struct fwnode_handle *fwnode = of_node_to_fwnode(pci->dev->of_node);
2376e0832faSShawn Lin 
2386e0832faSShawn Lin 	pp->irq_domain = irq_domain_create_linear(fwnode, pp->num_vectors,
2396e0832faSShawn Lin 					       &dw_pcie_msi_domain_ops, pp);
2406e0832faSShawn Lin 	if (!pp->irq_domain) {
2416e0832faSShawn Lin 		dev_err(pci->dev, "Failed to create IRQ domain\n");
2426e0832faSShawn Lin 		return -ENOMEM;
2436e0832faSShawn Lin 	}
2446e0832faSShawn Lin 
2450414b93eSMarc Zyngier 	irq_domain_update_bus_token(pp->irq_domain, DOMAIN_BUS_NEXUS);
2460414b93eSMarc Zyngier 
2476e0832faSShawn Lin 	pp->msi_domain = pci_msi_create_irq_domain(fwnode,
2486e0832faSShawn Lin 						   &dw_pcie_msi_domain_info,
2496e0832faSShawn Lin 						   pp->irq_domain);
2506e0832faSShawn Lin 	if (!pp->msi_domain) {
2516e0832faSShawn Lin 		dev_err(pci->dev, "Failed to create MSI domain\n");
2526e0832faSShawn Lin 		irq_domain_remove(pp->irq_domain);
2536e0832faSShawn Lin 		return -ENOMEM;
2546e0832faSShawn Lin 	}
2556e0832faSShawn Lin 
2566e0832faSShawn Lin 	return 0;
2576e0832faSShawn Lin }
2586e0832faSShawn Lin 
25959fbab1aSRob Herring static void dw_pcie_free_msi(struct pcie_port *pp)
2606e0832faSShawn Lin {
261ad1cc6b7SMartin Kaiser 	if (pp->msi_irq)
262ad1cc6b7SMartin Kaiser 		irq_set_chained_handler_and_data(pp->msi_irq, NULL, NULL);
2636e0832faSShawn Lin 
2646e0832faSShawn Lin 	irq_domain_remove(pp->msi_domain);
2656e0832faSShawn Lin 	irq_domain_remove(pp->irq_domain);
266dc69a3d5SJisheng Zhang 
26707940c36SJisheng Zhang 	if (pp->msi_data) {
26807940c36SJisheng Zhang 		struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
26907940c36SJisheng Zhang 		struct device *dev = pci->dev;
27007940c36SJisheng Zhang 
27107940c36SJisheng Zhang 		dma_unmap_single_attrs(dev, pp->msi_data, sizeof(pp->msi_msg),
27207940c36SJisheng Zhang 				       DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
27307940c36SJisheng Zhang 	}
2746e0832faSShawn Lin }
2756e0832faSShawn Lin 
27659fbab1aSRob Herring static void dw_pcie_msi_init(struct pcie_port *pp)
2776e0832faSShawn Lin {
2786e0832faSShawn Lin 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
27907940c36SJisheng Zhang 	u64 msi_target = (u64)pp->msi_data;
2806e0832faSShawn Lin 
28159fbab1aSRob Herring 	if (!pci_msi_enabled() || !pp->has_msi_ctrl)
282cf627713SRob Herring 		return;
283cf627713SRob Herring 
2846e0832faSShawn Lin 	/* Program the msi_data */
285f81c770dSRob Herring 	dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_LO, lower_32_bits(msi_target));
286f81c770dSRob Herring 	dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_HI, upper_32_bits(msi_target));
2876e0832faSShawn Lin }
2886e0832faSShawn Lin 
2896e0832faSShawn Lin int dw_pcie_host_init(struct pcie_port *pp)
2906e0832faSShawn Lin {
2916e0832faSShawn Lin 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
2926e0832faSShawn Lin 	struct device *dev = pci->dev;
2936e0832faSShawn Lin 	struct device_node *np = dev->of_node;
2946e0832faSShawn Lin 	struct platform_device *pdev = to_platform_device(dev);
2957fe71aa8SRob Herring 	struct resource_entry *win;
2966e0832faSShawn Lin 	struct pci_host_bridge *bridge;
2976e0832faSShawn Lin 	struct resource *cfg_res;
2986e0832faSShawn Lin 	int ret;
2996e0832faSShawn Lin 
3006e0832faSShawn Lin 	raw_spin_lock_init(&pci->pp.lock);
3016e0832faSShawn Lin 
3026e0832faSShawn Lin 	cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
3036e0832faSShawn Lin 	if (cfg_res) {
3042ef6b06aSRob Herring 		pp->cfg0_size = resource_size(cfg_res);
3056e0832faSShawn Lin 		pp->cfg0_base = cfg_res->start;
3062f5ab5afSRob Herring 
3072f5ab5afSRob Herring 		pp->va_cfg0_base = devm_pci_remap_cfg_resource(dev, cfg_res);
3082f5ab5afSRob Herring 		if (IS_ERR(pp->va_cfg0_base))
3092f5ab5afSRob Herring 			return PTR_ERR(pp->va_cfg0_base);
3102f5ab5afSRob Herring 	} else {
3116e0832faSShawn Lin 		dev_err(dev, "Missing *config* reg space\n");
3122f5ab5afSRob Herring 		return -ENODEV;
3136e0832faSShawn Lin 	}
3146e0832faSShawn Lin 
315a0fd361dSRob Herring 	if (!pci->dbi_base) {
316a0fd361dSRob Herring 		struct resource *dbi_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
317a0fd361dSRob Herring 		pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_res);
318a0fd361dSRob Herring 		if (IS_ERR(pci->dbi_base))
319a0fd361dSRob Herring 			return PTR_ERR(pci->dbi_base);
320a0fd361dSRob Herring 	}
321a0fd361dSRob Herring 
322e6fdd3bfSJisheng Zhang 	bridge = devm_pci_alloc_host_bridge(dev, 0);
3236e0832faSShawn Lin 	if (!bridge)
3246e0832faSShawn Lin 		return -ENOMEM;
3256e0832faSShawn Lin 
326444ddca5SRob Herring 	pp->bridge = bridge;
327444ddca5SRob Herring 
3282f5ab5afSRob Herring 	/* Get the I/O range from DT */
3292f5ab5afSRob Herring 	win = resource_list_first_type(&bridge->windows, IORESOURCE_IO);
3302f5ab5afSRob Herring 	if (win) {
3310f71c60fSRob Herring 		pp->io_size = resource_size(win->res);
3320f71c60fSRob Herring 		pp->io_bus_addr = win->res->start - win->offset;
3330f71c60fSRob Herring 		pp->io_base = pci_pio_to_address(win->res->start);
3346e0832faSShawn Lin 	}
3356e0832faSShawn Lin 
33639bc5006SRob Herring 	if (pci->link_gen < 1)
33739bc5006SRob Herring 		pci->link_gen = of_pci_get_max_link_speed(np);
33839bc5006SRob Herring 
3399e2b5de5SJisheng Zhang 	if (pci_msi_enabled()) {
340f78f0263SRob Herring 		pp->has_msi_ctrl = !(pp->ops->msi_host_init ||
341f78f0263SRob Herring 				     of_property_read_bool(np, "msi-parent") ||
342f78f0263SRob Herring 				     of_property_read_bool(np, "msi-map"));
343f78f0263SRob Herring 
344331e9bceSRob Herring 		if (!pp->num_vectors) {
3456e0832faSShawn Lin 			pp->num_vectors = MSI_DEF_NUM_VECTORS;
346331e9bceSRob Herring 		} else if (pp->num_vectors > MAX_MSI_IRQS) {
347331e9bceSRob Herring 			dev_err(dev, "Invalid number of vectors\n");
348e6fdd3bfSJisheng Zhang 			return -EINVAL;
3496e0832faSShawn Lin 		}
3506e0832faSShawn Lin 
351f78f0263SRob Herring 		if (pp->ops->msi_host_init) {
352f78f0263SRob Herring 			ret = pp->ops->msi_host_init(pp);
353f78f0263SRob Herring 			if (ret < 0)
354f78f0263SRob Herring 				return ret;
355f78f0263SRob Herring 		} else if (pp->has_msi_ctrl) {
3565bcb1757SRob Herring 			if (!pp->msi_irq) {
3575bcb1757SRob Herring 				pp->msi_irq = platform_get_irq_byname_optional(pdev, "msi");
3585bcb1757SRob Herring 				if (pp->msi_irq < 0) {
3595bcb1757SRob Herring 					pp->msi_irq = platform_get_irq(pdev, 0);
3605bcb1757SRob Herring 					if (pp->msi_irq < 0)
3615bcb1757SRob Herring 						return pp->msi_irq;
3625bcb1757SRob Herring 				}
3635bcb1757SRob Herring 			}
3645bcb1757SRob Herring 
365117c3b60SKishon Vijay Abraham I 			pp->msi_irq_chip = &dw_pci_msi_bottom_irq_chip;
366117c3b60SKishon Vijay Abraham I 
3676e0832faSShawn Lin 			ret = dw_pcie_allocate_domains(pp);
3686e0832faSShawn Lin 			if (ret)
369e6fdd3bfSJisheng Zhang 				return ret;
3706e0832faSShawn Lin 
3715bcb1757SRob Herring 			if (pp->msi_irq > 0)
3726e0832faSShawn Lin 				irq_set_chained_handler_and_data(pp->msi_irq,
3736e0832faSShawn Lin 							    dw_chained_msi_isr,
3746e0832faSShawn Lin 							    pp);
37507940c36SJisheng Zhang 
376660c4865SVidya Sagar 			ret = dma_set_mask(pci->dev, DMA_BIT_MASK(32));
37799e629f1SAlexander Lobakin 			if (ret)
37899e629f1SAlexander Lobakin 				dev_warn(pci->dev, "Failed to set DMA mask to 32-bit. Devices with only 32-bit MSI support may not work properly\n");
379660c4865SVidya Sagar 
38007940c36SJisheng Zhang 			pp->msi_data = dma_map_single_attrs(pci->dev, &pp->msi_msg,
38107940c36SJisheng Zhang 						      sizeof(pp->msi_msg),
38207940c36SJisheng Zhang 						      DMA_FROM_DEVICE,
38307940c36SJisheng Zhang 						      DMA_ATTR_SKIP_CPU_SYNC);
38407940c36SJisheng Zhang 			if (dma_mapping_error(pci->dev, pp->msi_data)) {
38507940c36SJisheng Zhang 				dev_err(pci->dev, "Failed to map MSI data\n");
38607940c36SJisheng Zhang 				pp->msi_data = 0;
38707940c36SJisheng Zhang 				goto err_free_msi;
38807940c36SJisheng Zhang 			}
3896e0832faSShawn Lin 		}
3906e0832faSShawn Lin 	}
3916e0832faSShawn Lin 
392444ddca5SRob Herring 	/* Set default bus ops */
393444ddca5SRob Herring 	bridge->ops = &dw_pcie_ops;
394c2b0c098SRob Herring 	bridge->child_ops = &dw_child_pcie_ops;
395444ddca5SRob Herring 
3966e0832faSShawn Lin 	if (pp->ops->host_init) {
3976e0832faSShawn Lin 		ret = pp->ops->host_init(pp);
3986e0832faSShawn Lin 		if (ret)
3999e2b5de5SJisheng Zhang 			goto err_free_msi;
4006e0832faSShawn Lin 	}
401*8bcca265SHou Zhiqiang 	dw_pcie_iatu_detect(pci);
4026e0832faSShawn Lin 
403b9ac0f9dSRob Herring 	dw_pcie_setup_rc(pp);
40459fbab1aSRob Herring 
405a2f882d8SJisheng Zhang 	if (!dw_pcie_link_up(pci) && pci->ops && pci->ops->start_link) {
406886a9c13SRob Herring 		ret = pci->ops->start_link(pci);
407886a9c13SRob Herring 		if (ret)
408886a9c13SRob Herring 			goto err_free_msi;
409886a9c13SRob Herring 	}
410886a9c13SRob Herring 
411886a9c13SRob Herring 	/* Ignore errors, the link may come up later */
412886a9c13SRob Herring 	dw_pcie_wait_for_link(pci);
413886a9c13SRob Herring 
4146e0832faSShawn Lin 	bridge->sysdata = pp;
4156e0832faSShawn Lin 
4161df79305SRob Herring 	ret = pci_host_probe(bridge);
4171df79305SRob Herring 	if (!ret)
4186e0832faSShawn Lin 		return 0;
4196e0832faSShawn Lin 
4209e2b5de5SJisheng Zhang err_free_msi:
421f78f0263SRob Herring 	if (pp->has_msi_ctrl)
4229e2b5de5SJisheng Zhang 		dw_pcie_free_msi(pp);
4236e0832faSShawn Lin 	return ret;
4246e0832faSShawn Lin }
425ca98329dSVidya Sagar EXPORT_SYMBOL_GPL(dw_pcie_host_init);
4266e0832faSShawn Lin 
4279d071cadSVidya Sagar void dw_pcie_host_deinit(struct pcie_port *pp)
4289d071cadSVidya Sagar {
4295808d43eSRob Herring 	pci_stop_root_bus(pp->bridge->bus);
4305808d43eSRob Herring 	pci_remove_root_bus(pp->bridge->bus);
431f78f0263SRob Herring 	if (pp->has_msi_ctrl)
4329d071cadSVidya Sagar 		dw_pcie_free_msi(pp);
4339d071cadSVidya Sagar }
434ca98329dSVidya Sagar EXPORT_SYMBOL_GPL(dw_pcie_host_deinit);
4359d071cadSVidya Sagar 
436c2b0c098SRob Herring static void __iomem *dw_pcie_other_conf_map_bus(struct pci_bus *bus,
437c2b0c098SRob Herring 						unsigned int devfn, int where)
4386e0832faSShawn Lin {
439c2b0c098SRob Herring 	int type;
4402ef6b06aSRob Herring 	u32 busdev;
441c2b0c098SRob Herring 	struct pcie_port *pp = bus->sysdata;
4426e0832faSShawn Lin 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
4436e0832faSShawn Lin 
44415b23906SHou Zhiqiang 	/*
44515b23906SHou Zhiqiang 	 * Checking whether the link is up here is a last line of defense
44615b23906SHou Zhiqiang 	 * against platforms that forward errors on the system bus as
44715b23906SHou Zhiqiang 	 * SError upon PCI configuration transactions issued when the link
44815b23906SHou Zhiqiang 	 * is down. This check is racy by definition and does not stop
44915b23906SHou Zhiqiang 	 * the system from triggering an SError if the link goes down
45015b23906SHou Zhiqiang 	 * after this check is performed.
45115b23906SHou Zhiqiang 	 */
45215b23906SHou Zhiqiang 	if (!dw_pcie_link_up(pci))
45315b23906SHou Zhiqiang 		return NULL;
45415b23906SHou Zhiqiang 
4556e0832faSShawn Lin 	busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
4566e0832faSShawn Lin 		 PCIE_ATU_FUNC(PCI_FUNC(devfn));
4576e0832faSShawn Lin 
4582ef6b06aSRob Herring 	if (pci_is_root_bus(bus->parent))
4596e0832faSShawn Lin 		type = PCIE_ATU_TYPE_CFG0;
4602ef6b06aSRob Herring 	else
4616e0832faSShawn Lin 		type = PCIE_ATU_TYPE_CFG1;
4622ef6b06aSRob Herring 
4636e0832faSShawn Lin 
4649f9e59a4SRob Herring 	dw_pcie_prog_outbound_atu(pci, 0, type, pp->cfg0_base, busdev, pp->cfg0_size);
465689e349aSAndrey Smirnov 
4662ef6b06aSRob Herring 	return pp->va_cfg0_base + where;
467c2b0c098SRob Herring }
468c2b0c098SRob Herring 
469c2b0c098SRob Herring static int dw_pcie_rd_other_conf(struct pci_bus *bus, unsigned int devfn,
470c2b0c098SRob Herring 				 int where, int size, u32 *val)
471c2b0c098SRob Herring {
472c2b0c098SRob Herring 	int ret;
473c2b0c098SRob Herring 	struct pcie_port *pp = bus->sysdata;
474c2b0c098SRob Herring 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
475c2b0c098SRob Herring 
476c2b0c098SRob Herring 	ret = pci_generic_config_read(bus, devfn, where, size, val);
477c2b0c098SRob Herring 
4789f9e59a4SRob Herring 	if (!ret && pci->io_cfg_atu_shared)
4799f9e59a4SRob Herring 		dw_pcie_prog_outbound_atu(pci, 0, PCIE_ATU_TYPE_IO, pp->io_base,
4806e0832faSShawn Lin 					  pp->io_bus_addr, pp->io_size);
4816e0832faSShawn Lin 
4826e0832faSShawn Lin 	return ret;
4836e0832faSShawn Lin }
4846e0832faSShawn Lin 
485c2b0c098SRob Herring static int dw_pcie_wr_other_conf(struct pci_bus *bus, unsigned int devfn,
4866e0832faSShawn Lin 				 int where, int size, u32 val)
4876e0832faSShawn Lin {
488c2b0c098SRob Herring 	int ret;
4896e0832faSShawn Lin 	struct pcie_port *pp = bus->sysdata;
490c2b0c098SRob Herring 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
4916e0832faSShawn Lin 
492c2b0c098SRob Herring 	ret = pci_generic_config_write(bus, devfn, where, size, val);
4936e0832faSShawn Lin 
4949f9e59a4SRob Herring 	if (!ret && pci->io_cfg_atu_shared)
4959f9e59a4SRob Herring 		dw_pcie_prog_outbound_atu(pci, 0, PCIE_ATU_TYPE_IO, pp->io_base,
496c2b0c098SRob Herring 					  pp->io_bus_addr, pp->io_size);
4976e0832faSShawn Lin 
498c2b0c098SRob Herring 	return ret;
4996e0832faSShawn Lin }
5006e0832faSShawn Lin 
501c2b0c098SRob Herring static struct pci_ops dw_child_pcie_ops = {
502c2b0c098SRob Herring 	.map_bus = dw_pcie_other_conf_map_bus,
503c2b0c098SRob Herring 	.read = dw_pcie_rd_other_conf,
504c2b0c098SRob Herring 	.write = dw_pcie_wr_other_conf,
505c2b0c098SRob Herring };
506c2b0c098SRob Herring 
50727e7ed01SRob Herring void __iomem *dw_pcie_own_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
50827e7ed01SRob Herring {
50927e7ed01SRob Herring 	struct pcie_port *pp = bus->sysdata;
51027e7ed01SRob Herring 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
51127e7ed01SRob Herring 
51227e7ed01SRob Herring 	if (PCI_SLOT(devfn) > 0)
51327e7ed01SRob Herring 		return NULL;
51427e7ed01SRob Herring 
51527e7ed01SRob Herring 	return pci->dbi_base + where;
51627e7ed01SRob Herring }
51727e7ed01SRob Herring EXPORT_SYMBOL_GPL(dw_pcie_own_conf_map_bus);
51827e7ed01SRob Herring 
5196e0832faSShawn Lin static struct pci_ops dw_pcie_ops = {
520c2b0c098SRob Herring 	.map_bus = dw_pcie_own_conf_map_bus,
521c2b0c098SRob Herring 	.read = pci_generic_config_read,
522c2b0c098SRob Herring 	.write = pci_generic_config_write,
5236e0832faSShawn Lin };
5246e0832faSShawn Lin 
5256e0832faSShawn Lin void dw_pcie_setup_rc(struct pcie_port *pp)
5266e0832faSShawn Lin {
527458ad06cSRob Herring 	int i;
5286e0832faSShawn Lin 	u32 val, ctrl, num_ctrls;
5296e0832faSShawn Lin 	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
5306e0832faSShawn Lin 
5313924bc2fSVidya Sagar 	/*
5323924bc2fSVidya Sagar 	 * Enable DBI read-only registers for writing/updating configuration.
5333924bc2fSVidya Sagar 	 * Write permission gets disabled towards the end of this function.
5343924bc2fSVidya Sagar 	 */
5353924bc2fSVidya Sagar 	dw_pcie_dbi_ro_wr_en(pci);
5363924bc2fSVidya Sagar 
5376e0832faSShawn Lin 	dw_pcie_setup(pci);
5386e0832faSShawn Lin 
539f78f0263SRob Herring 	if (pp->has_msi_ctrl) {
5406e0832faSShawn Lin 		num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
5416e0832faSShawn Lin 
5426e0832faSShawn Lin 		/* Initialize IRQ Status array */
543830920e0SMarc Zyngier 		for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
544a348d015SGustavo Pimentel 			pp->irq_mask[ctrl] = ~0;
545f81c770dSRob Herring 			dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK +
5466e0832faSShawn Lin 					    (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
547f81c770dSRob Herring 					    pp->irq_mask[ctrl]);
548f81c770dSRob Herring 			dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_ENABLE +
549830920e0SMarc Zyngier 					    (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
550f81c770dSRob Herring 					    ~0);
551830920e0SMarc Zyngier 		}
552fd8a44bdSKishon Vijay Abraham I 	}
5536e0832faSShawn Lin 
554294353d9SJisheng Zhang 	dw_pcie_msi_init(pp);
555294353d9SJisheng Zhang 
5566e0832faSShawn Lin 	/* Setup RC BARs */
5576e0832faSShawn Lin 	dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
5586e0832faSShawn Lin 	dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
5596e0832faSShawn Lin 
5606e0832faSShawn Lin 	/* Setup interrupt pins */
5616e0832faSShawn Lin 	val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
5626e0832faSShawn Lin 	val &= 0xffff00ff;
5636e0832faSShawn Lin 	val |= 0x00000100;
5646e0832faSShawn Lin 	dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
5656e0832faSShawn Lin 
5666e0832faSShawn Lin 	/* Setup bus numbers */
5676e0832faSShawn Lin 	val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
5686e0832faSShawn Lin 	val &= 0xff000000;
5696e0832faSShawn Lin 	val |= 0x00ff0100;
5706e0832faSShawn Lin 	dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
5716e0832faSShawn Lin 
5726e0832faSShawn Lin 	/* Setup command register */
5736e0832faSShawn Lin 	val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
5746e0832faSShawn Lin 	val &= 0xffff0000;
5756e0832faSShawn Lin 	val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
5766e0832faSShawn Lin 		PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
5776e0832faSShawn Lin 	dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
5786e0832faSShawn Lin 
579458ad06cSRob Herring 	/* Ensure all outbound windows are disabled so there are multiple matches */
580281f1f99SRob Herring 	for (i = 0; i < pci->num_ob_windows; i++)
581458ad06cSRob Herring 		dw_pcie_disable_atu(pci, i, DW_PCIE_REGION_OUTBOUND);
582458ad06cSRob Herring 
5836e0832faSShawn Lin 	/*
584444ddca5SRob Herring 	 * If the platform provides its own child bus config accesses, it means
585444ddca5SRob Herring 	 * the platform uses its own address translation component rather than
586444ddca5SRob Herring 	 * ATU, so we should not program the ATU here.
5876e0832faSShawn Lin 	 */
588c2b0c098SRob Herring 	if (pp->bridge->child_ops == &dw_child_pcie_ops) {
5899f9e59a4SRob Herring 		int atu_idx = 0;
5909f9e59a4SRob Herring 		struct resource_entry *entry;
5919fff3256SRob Herring 
5929fff3256SRob Herring 		/* Get last memory resource entry */
5939f9e59a4SRob Herring 		resource_list_for_each_entry(entry, &pp->bridge->windows) {
5949f9e59a4SRob Herring 			if (resource_type(entry->res) != IORESOURCE_MEM)
5959f9e59a4SRob Herring 				continue;
5960f71c60fSRob Herring 
597281f1f99SRob Herring 			if (pci->num_ob_windows <= ++atu_idx)
5989f9e59a4SRob Herring 				break;
5999f9e59a4SRob Herring 
6009f9e59a4SRob Herring 			dw_pcie_prog_outbound_atu(pci, atu_idx,
6010f71c60fSRob Herring 						  PCIE_ATU_TYPE_MEM, entry->res->start,
6020f71c60fSRob Herring 						  entry->res->start - entry->offset,
6030f71c60fSRob Herring 						  resource_size(entry->res));
6049f9e59a4SRob Herring 		}
6059f9e59a4SRob Herring 
6069f9e59a4SRob Herring 		if (pp->io_size) {
607281f1f99SRob Herring 			if (pci->num_ob_windows > ++atu_idx)
6089f9e59a4SRob Herring 				dw_pcie_prog_outbound_atu(pci, atu_idx,
6096e0832faSShawn Lin 							  PCIE_ATU_TYPE_IO, pp->io_base,
6106e0832faSShawn Lin 							  pp->io_bus_addr, pp->io_size);
6119f9e59a4SRob Herring 			else
6129f9e59a4SRob Herring 				pci->io_cfg_atu_shared = true;
6139f9e59a4SRob Herring 		}
6149f9e59a4SRob Herring 
615281f1f99SRob Herring 		if (pci->num_ob_windows <= atu_idx)
6169f9e59a4SRob Herring 			dev_warn(pci->dev, "Resources exceed number of ATU entries (%d)",
617281f1f99SRob Herring 				 pci->num_ob_windows);
6186e0832faSShawn Lin 	}
6196e0832faSShawn Lin 
620f81c770dSRob Herring 	dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
6216e0832faSShawn Lin 
6226e0832faSShawn Lin 	/* Program correct class for RC */
623f81c770dSRob Herring 	dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
6246e0832faSShawn Lin 
625f81c770dSRob Herring 	val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
6266e0832faSShawn Lin 	val |= PORT_LOGIC_SPEED_CHANGE;
627f81c770dSRob Herring 	dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
6283924bc2fSVidya Sagar 
6293924bc2fSVidya Sagar 	dw_pcie_dbi_ro_wr_dis(pci);
6306e0832faSShawn Lin }
631ca98329dSVidya Sagar EXPORT_SYMBOL_GPL(dw_pcie_setup_rc);
632