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 2596e0832faSShawn Lin void dw_pcie_free_msi(struct pcie_port *pp) 2606e0832faSShawn Lin { 2613ebc269cSJisheng Zhang if (pp->msi_irq) { 2626e0832faSShawn Lin irq_set_chained_handler(pp->msi_irq, NULL); 2636e0832faSShawn Lin irq_set_handler_data(pp->msi_irq, NULL); 2643ebc269cSJisheng Zhang } 2656e0832faSShawn Lin 2666e0832faSShawn Lin irq_domain_remove(pp->msi_domain); 2676e0832faSShawn Lin irq_domain_remove(pp->irq_domain); 268dc69a3d5SJisheng Zhang 26907940c36SJisheng Zhang if (pp->msi_data) { 27007940c36SJisheng Zhang struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 27107940c36SJisheng Zhang struct device *dev = pci->dev; 27207940c36SJisheng Zhang 27307940c36SJisheng Zhang dma_unmap_single_attrs(dev, pp->msi_data, sizeof(pp->msi_msg), 27407940c36SJisheng Zhang DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC); 27507940c36SJisheng Zhang } 2766e0832faSShawn Lin } 2776e0832faSShawn Lin 2786e0832faSShawn Lin void dw_pcie_msi_init(struct pcie_port *pp) 2796e0832faSShawn Lin { 2806e0832faSShawn Lin struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 28107940c36SJisheng Zhang u64 msi_target = (u64)pp->msi_data; 2826e0832faSShawn Lin 283cf627713SRob Herring if (!IS_ENABLED(CONFIG_PCI_MSI)) 284cf627713SRob Herring return; 285cf627713SRob Herring 2866e0832faSShawn Lin /* Program the msi_data */ 287f81c770dSRob Herring dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_LO, lower_32_bits(msi_target)); 288f81c770dSRob Herring dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_HI, upper_32_bits(msi_target)); 2896e0832faSShawn Lin } 290ca98329dSVidya Sagar EXPORT_SYMBOL_GPL(dw_pcie_msi_init); 2916e0832faSShawn Lin 2926e0832faSShawn Lin int dw_pcie_host_init(struct pcie_port *pp) 2936e0832faSShawn Lin { 2946e0832faSShawn Lin struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 2956e0832faSShawn Lin struct device *dev = pci->dev; 2966e0832faSShawn Lin struct device_node *np = dev->of_node; 2976e0832faSShawn Lin struct platform_device *pdev = to_platform_device(dev); 2987fe71aa8SRob Herring struct resource_entry *win; 2996e0832faSShawn Lin struct pci_host_bridge *bridge; 3006e0832faSShawn Lin struct resource *cfg_res; 3016e0832faSShawn Lin int ret; 3026e0832faSShawn Lin 3036e0832faSShawn Lin raw_spin_lock_init(&pci->pp.lock); 3046e0832faSShawn Lin 3056e0832faSShawn Lin cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config"); 3066e0832faSShawn Lin if (cfg_res) { 3072ef6b06aSRob Herring pp->cfg0_size = resource_size(cfg_res); 3086e0832faSShawn Lin pp->cfg0_base = cfg_res->start; 3096e0832faSShawn Lin } else if (!pp->va_cfg0_base) { 3106e0832faSShawn Lin dev_err(dev, "Missing *config* reg space\n"); 3116e0832faSShawn Lin } 3126e0832faSShawn Lin 313*a0fd361dSRob Herring if (!pci->dbi_base) { 314*a0fd361dSRob Herring struct resource *dbi_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi"); 315*a0fd361dSRob Herring pci->dbi_base = devm_pci_remap_cfg_resource(dev, dbi_res); 316*a0fd361dSRob Herring if (IS_ERR(pci->dbi_base)) 317*a0fd361dSRob Herring return PTR_ERR(pci->dbi_base); 318*a0fd361dSRob Herring } 319*a0fd361dSRob Herring 320e6fdd3bfSJisheng Zhang bridge = devm_pci_alloc_host_bridge(dev, 0); 3216e0832faSShawn Lin if (!bridge) 3226e0832faSShawn Lin return -ENOMEM; 3236e0832faSShawn Lin 324444ddca5SRob Herring pp->bridge = bridge; 325444ddca5SRob Herring 3266e0832faSShawn Lin /* Get the I/O and memory ranges from DT */ 3277fe71aa8SRob Herring resource_list_for_each_entry(win, &bridge->windows) { 3286e0832faSShawn Lin switch (resource_type(win->res)) { 3296e0832faSShawn Lin case IORESOURCE_IO: 3300f71c60fSRob Herring pp->io_size = resource_size(win->res); 3310f71c60fSRob Herring pp->io_bus_addr = win->res->start - win->offset; 3320f71c60fSRob Herring pp->io_base = pci_pio_to_address(win->res->start); 3336e0832faSShawn Lin break; 3346e0832faSShawn Lin case 0: 3352ef6b06aSRob Herring dev_err(dev, "Missing *config* reg space\n"); 3362ef6b06aSRob Herring pp->cfg0_size = resource_size(win->res); 3372ef6b06aSRob Herring pp->cfg0_base = win->res->start; 3386e0832faSShawn Lin if (!pci->dbi_base) { 3396e0832faSShawn Lin pci->dbi_base = devm_pci_remap_cfgspace(dev, 3402ef6b06aSRob Herring pp->cfg0_base, 3412ef6b06aSRob Herring pp->cfg0_size); 3426e0832faSShawn Lin if (!pci->dbi_base) { 3436e0832faSShawn Lin dev_err(dev, "Error with ioremap\n"); 344e6fdd3bfSJisheng Zhang return -ENOMEM; 3456e0832faSShawn Lin } 3466e0832faSShawn Lin } 3472ef6b06aSRob Herring break; 3482ef6b06aSRob Herring } 3492ef6b06aSRob Herring } 3506e0832faSShawn Lin 3516e0832faSShawn Lin if (!pp->va_cfg0_base) { 3526e0832faSShawn Lin pp->va_cfg0_base = devm_pci_remap_cfgspace(dev, 3536e0832faSShawn Lin pp->cfg0_base, pp->cfg0_size); 3546e0832faSShawn Lin if (!pp->va_cfg0_base) { 3556e0832faSShawn Lin dev_err(dev, "Error with ioremap in function\n"); 356e6fdd3bfSJisheng Zhang return -ENOMEM; 3576e0832faSShawn Lin } 3586e0832faSShawn Lin } 3596e0832faSShawn Lin 3606e0832faSShawn Lin ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport); 3616e0832faSShawn Lin if (ret) 3626e0832faSShawn Lin pci->num_viewport = 2; 3636e0832faSShawn Lin 36439bc5006SRob Herring if (pci->link_gen < 1) 36539bc5006SRob Herring pci->link_gen = of_pci_get_max_link_speed(np); 36639bc5006SRob Herring 3679e2b5de5SJisheng Zhang if (pci_msi_enabled()) { 3686e0832faSShawn Lin /* 3696e0832faSShawn Lin * If a specific SoC driver needs to change the 3706e0832faSShawn Lin * default number of vectors, it needs to implement 3716e0832faSShawn Lin * the set_num_vectors callback. 3726e0832faSShawn Lin */ 3736e0832faSShawn Lin if (!pp->ops->set_num_vectors) { 3746e0832faSShawn Lin pp->num_vectors = MSI_DEF_NUM_VECTORS; 3756e0832faSShawn Lin } else { 3766e0832faSShawn Lin pp->ops->set_num_vectors(pp); 3776e0832faSShawn Lin 3786e0832faSShawn Lin if (pp->num_vectors > MAX_MSI_IRQS || 3796e0832faSShawn Lin pp->num_vectors == 0) { 3806e0832faSShawn Lin dev_err(dev, 3816e0832faSShawn Lin "Invalid number of vectors\n"); 382e6fdd3bfSJisheng Zhang return -EINVAL; 3836e0832faSShawn Lin } 3846e0832faSShawn Lin } 3856e0832faSShawn Lin 3866e0832faSShawn Lin if (!pp->ops->msi_host_init) { 387117c3b60SKishon Vijay Abraham I pp->msi_irq_chip = &dw_pci_msi_bottom_irq_chip; 388117c3b60SKishon Vijay Abraham I 3896e0832faSShawn Lin ret = dw_pcie_allocate_domains(pp); 3906e0832faSShawn Lin if (ret) 391e6fdd3bfSJisheng Zhang return ret; 3926e0832faSShawn Lin 3936e0832faSShawn Lin if (pp->msi_irq) 3946e0832faSShawn Lin irq_set_chained_handler_and_data(pp->msi_irq, 3956e0832faSShawn Lin dw_chained_msi_isr, 3966e0832faSShawn Lin pp); 39707940c36SJisheng Zhang 39807940c36SJisheng Zhang pp->msi_data = dma_map_single_attrs(pci->dev, &pp->msi_msg, 39907940c36SJisheng Zhang sizeof(pp->msi_msg), 40007940c36SJisheng Zhang DMA_FROM_DEVICE, 40107940c36SJisheng Zhang DMA_ATTR_SKIP_CPU_SYNC); 40207940c36SJisheng Zhang if (dma_mapping_error(pci->dev, pp->msi_data)) { 40307940c36SJisheng Zhang dev_err(pci->dev, "Failed to map MSI data\n"); 40407940c36SJisheng Zhang pp->msi_data = 0; 40507940c36SJisheng Zhang goto err_free_msi; 40607940c36SJisheng Zhang } 4076e0832faSShawn Lin } else { 4086e0832faSShawn Lin ret = pp->ops->msi_host_init(pp); 4096e0832faSShawn Lin if (ret < 0) 410e6fdd3bfSJisheng Zhang return ret; 4116e0832faSShawn Lin } 4126e0832faSShawn Lin } 4136e0832faSShawn Lin 414444ddca5SRob Herring /* Set default bus ops */ 415444ddca5SRob Herring bridge->ops = &dw_pcie_ops; 416c2b0c098SRob Herring bridge->child_ops = &dw_child_pcie_ops; 417444ddca5SRob Herring 4186e0832faSShawn Lin if (pp->ops->host_init) { 4196e0832faSShawn Lin ret = pp->ops->host_init(pp); 4206e0832faSShawn Lin if (ret) 4219e2b5de5SJisheng Zhang goto err_free_msi; 4226e0832faSShawn Lin } 4236e0832faSShawn Lin 4246e0832faSShawn Lin bridge->sysdata = pp; 4256e0832faSShawn Lin 4261df79305SRob Herring ret = pci_host_probe(bridge); 4271df79305SRob Herring if (!ret) 4286e0832faSShawn Lin return 0; 4296e0832faSShawn Lin 4309e2b5de5SJisheng Zhang err_free_msi: 4319e2b5de5SJisheng Zhang if (pci_msi_enabled() && !pp->ops->msi_host_init) 4329e2b5de5SJisheng Zhang dw_pcie_free_msi(pp); 4336e0832faSShawn Lin return ret; 4346e0832faSShawn Lin } 435ca98329dSVidya Sagar EXPORT_SYMBOL_GPL(dw_pcie_host_init); 4366e0832faSShawn Lin 4379d071cadSVidya Sagar void dw_pcie_host_deinit(struct pcie_port *pp) 4389d071cadSVidya Sagar { 4395808d43eSRob Herring pci_stop_root_bus(pp->bridge->bus); 4405808d43eSRob Herring pci_remove_root_bus(pp->bridge->bus); 4419d071cadSVidya Sagar if (pci_msi_enabled() && !pp->ops->msi_host_init) 4429d071cadSVidya Sagar dw_pcie_free_msi(pp); 4439d071cadSVidya Sagar } 444ca98329dSVidya Sagar EXPORT_SYMBOL_GPL(dw_pcie_host_deinit); 4459d071cadSVidya Sagar 446c2b0c098SRob Herring static void __iomem *dw_pcie_other_conf_map_bus(struct pci_bus *bus, 447c2b0c098SRob Herring unsigned int devfn, int where) 4486e0832faSShawn Lin { 449c2b0c098SRob Herring int type; 4502ef6b06aSRob Herring u32 busdev; 451c2b0c098SRob Herring struct pcie_port *pp = bus->sysdata; 4526e0832faSShawn Lin struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 4536e0832faSShawn Lin 45415b23906SHou Zhiqiang /* 45515b23906SHou Zhiqiang * Checking whether the link is up here is a last line of defense 45615b23906SHou Zhiqiang * against platforms that forward errors on the system bus as 45715b23906SHou Zhiqiang * SError upon PCI configuration transactions issued when the link 45815b23906SHou Zhiqiang * is down. This check is racy by definition and does not stop 45915b23906SHou Zhiqiang * the system from triggering an SError if the link goes down 46015b23906SHou Zhiqiang * after this check is performed. 46115b23906SHou Zhiqiang */ 46215b23906SHou Zhiqiang if (!dw_pcie_link_up(pci)) 46315b23906SHou Zhiqiang return NULL; 46415b23906SHou Zhiqiang 4656e0832faSShawn Lin busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) | 4666e0832faSShawn Lin PCIE_ATU_FUNC(PCI_FUNC(devfn)); 4676e0832faSShawn Lin 4682ef6b06aSRob Herring if (pci_is_root_bus(bus->parent)) 4696e0832faSShawn Lin type = PCIE_ATU_TYPE_CFG0; 4702ef6b06aSRob Herring else 4716e0832faSShawn Lin type = PCIE_ATU_TYPE_CFG1; 4722ef6b06aSRob Herring 4736e0832faSShawn Lin 4749f9e59a4SRob Herring dw_pcie_prog_outbound_atu(pci, 0, type, pp->cfg0_base, busdev, pp->cfg0_size); 475689e349aSAndrey Smirnov 4762ef6b06aSRob Herring return pp->va_cfg0_base + where; 477c2b0c098SRob Herring } 478c2b0c098SRob Herring 479c2b0c098SRob Herring static int dw_pcie_rd_other_conf(struct pci_bus *bus, unsigned int devfn, 480c2b0c098SRob Herring int where, int size, u32 *val) 481c2b0c098SRob Herring { 482c2b0c098SRob Herring int ret; 483c2b0c098SRob Herring struct pcie_port *pp = bus->sysdata; 484c2b0c098SRob Herring struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 485c2b0c098SRob Herring 486c2b0c098SRob Herring ret = pci_generic_config_read(bus, devfn, where, size, val); 487c2b0c098SRob Herring 4889f9e59a4SRob Herring if (!ret && pci->io_cfg_atu_shared) 4899f9e59a4SRob Herring dw_pcie_prog_outbound_atu(pci, 0, PCIE_ATU_TYPE_IO, pp->io_base, 4906e0832faSShawn Lin pp->io_bus_addr, pp->io_size); 4916e0832faSShawn Lin 4926e0832faSShawn Lin return ret; 4936e0832faSShawn Lin } 4946e0832faSShawn Lin 495c2b0c098SRob Herring static int dw_pcie_wr_other_conf(struct pci_bus *bus, unsigned int devfn, 4966e0832faSShawn Lin int where, int size, u32 val) 4976e0832faSShawn Lin { 498c2b0c098SRob Herring int ret; 4996e0832faSShawn Lin struct pcie_port *pp = bus->sysdata; 500c2b0c098SRob Herring struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 5016e0832faSShawn Lin 502c2b0c098SRob Herring ret = pci_generic_config_write(bus, devfn, where, size, val); 5036e0832faSShawn Lin 5049f9e59a4SRob Herring if (!ret && pci->io_cfg_atu_shared) 5059f9e59a4SRob Herring dw_pcie_prog_outbound_atu(pci, 0, PCIE_ATU_TYPE_IO, pp->io_base, 506c2b0c098SRob Herring pp->io_bus_addr, pp->io_size); 5076e0832faSShawn Lin 508c2b0c098SRob Herring return ret; 5096e0832faSShawn Lin } 5106e0832faSShawn Lin 511c2b0c098SRob Herring static struct pci_ops dw_child_pcie_ops = { 512c2b0c098SRob Herring .map_bus = dw_pcie_other_conf_map_bus, 513c2b0c098SRob Herring .read = dw_pcie_rd_other_conf, 514c2b0c098SRob Herring .write = dw_pcie_wr_other_conf, 515c2b0c098SRob Herring }; 516c2b0c098SRob Herring 51727e7ed01SRob Herring void __iomem *dw_pcie_own_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where) 51827e7ed01SRob Herring { 51927e7ed01SRob Herring struct pcie_port *pp = bus->sysdata; 52027e7ed01SRob Herring struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 52127e7ed01SRob Herring 52227e7ed01SRob Herring if (PCI_SLOT(devfn) > 0) 52327e7ed01SRob Herring return NULL; 52427e7ed01SRob Herring 52527e7ed01SRob Herring return pci->dbi_base + where; 52627e7ed01SRob Herring } 52727e7ed01SRob Herring EXPORT_SYMBOL_GPL(dw_pcie_own_conf_map_bus); 52827e7ed01SRob Herring 5296e0832faSShawn Lin static struct pci_ops dw_pcie_ops = { 530c2b0c098SRob Herring .map_bus = dw_pcie_own_conf_map_bus, 531c2b0c098SRob Herring .read = pci_generic_config_read, 532c2b0c098SRob Herring .write = pci_generic_config_write, 5336e0832faSShawn Lin }; 5346e0832faSShawn Lin 5356e0832faSShawn Lin void dw_pcie_setup_rc(struct pcie_port *pp) 5366e0832faSShawn Lin { 5376e0832faSShawn Lin u32 val, ctrl, num_ctrls; 5386e0832faSShawn Lin struct dw_pcie *pci = to_dw_pcie_from_pp(pp); 5396e0832faSShawn Lin 5403924bc2fSVidya Sagar /* 5413924bc2fSVidya Sagar * Enable DBI read-only registers for writing/updating configuration. 5423924bc2fSVidya Sagar * Write permission gets disabled towards the end of this function. 5433924bc2fSVidya Sagar */ 5443924bc2fSVidya Sagar dw_pcie_dbi_ro_wr_en(pci); 5453924bc2fSVidya Sagar 5466e0832faSShawn Lin dw_pcie_setup(pci); 5476e0832faSShawn Lin 54874283324SJisheng Zhang if (pci_msi_enabled() && !pp->ops->msi_host_init) { 5496e0832faSShawn Lin num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL; 5506e0832faSShawn Lin 5516e0832faSShawn Lin /* Initialize IRQ Status array */ 552830920e0SMarc Zyngier for (ctrl = 0; ctrl < num_ctrls; ctrl++) { 553a348d015SGustavo Pimentel pp->irq_mask[ctrl] = ~0; 554f81c770dSRob Herring dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + 5556e0832faSShawn Lin (ctrl * MSI_REG_CTRL_BLOCK_SIZE), 556f81c770dSRob Herring pp->irq_mask[ctrl]); 557f81c770dSRob Herring dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_ENABLE + 558830920e0SMarc Zyngier (ctrl * MSI_REG_CTRL_BLOCK_SIZE), 559f81c770dSRob Herring ~0); 560830920e0SMarc Zyngier } 561fd8a44bdSKishon Vijay Abraham I } 5626e0832faSShawn Lin 5636e0832faSShawn Lin /* Setup RC BARs */ 5646e0832faSShawn Lin dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004); 5656e0832faSShawn Lin dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000); 5666e0832faSShawn Lin 5676e0832faSShawn Lin /* Setup interrupt pins */ 5686e0832faSShawn Lin val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE); 5696e0832faSShawn Lin val &= 0xffff00ff; 5706e0832faSShawn Lin val |= 0x00000100; 5716e0832faSShawn Lin dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val); 5726e0832faSShawn Lin 5736e0832faSShawn Lin /* Setup bus numbers */ 5746e0832faSShawn Lin val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS); 5756e0832faSShawn Lin val &= 0xff000000; 5766e0832faSShawn Lin val |= 0x00ff0100; 5776e0832faSShawn Lin dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val); 5786e0832faSShawn Lin 5796e0832faSShawn Lin /* Setup command register */ 5806e0832faSShawn Lin val = dw_pcie_readl_dbi(pci, PCI_COMMAND); 5816e0832faSShawn Lin val &= 0xffff0000; 5826e0832faSShawn Lin val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | 5836e0832faSShawn Lin PCI_COMMAND_MASTER | PCI_COMMAND_SERR; 5846e0832faSShawn Lin dw_pcie_writel_dbi(pci, PCI_COMMAND, val); 5856e0832faSShawn Lin 5866e0832faSShawn Lin /* 587444ddca5SRob Herring * If the platform provides its own child bus config accesses, it means 588444ddca5SRob Herring * the platform uses its own address translation component rather than 589444ddca5SRob Herring * ATU, so we should not program the ATU here. 5906e0832faSShawn Lin */ 591c2b0c098SRob Herring if (pp->bridge->child_ops == &dw_child_pcie_ops) { 5929f9e59a4SRob Herring int atu_idx = 0; 5939f9e59a4SRob Herring struct resource_entry *entry; 5949fff3256SRob Herring 5959fff3256SRob Herring /* Get last memory resource entry */ 5969f9e59a4SRob Herring resource_list_for_each_entry(entry, &pp->bridge->windows) { 5979f9e59a4SRob Herring if (resource_type(entry->res) != IORESOURCE_MEM) 5989f9e59a4SRob Herring continue; 5990f71c60fSRob Herring 6009f9e59a4SRob Herring if (pci->num_viewport <= ++atu_idx) 6019f9e59a4SRob Herring break; 6029f9e59a4SRob Herring 6039f9e59a4SRob Herring dw_pcie_prog_outbound_atu(pci, atu_idx, 6040f71c60fSRob Herring PCIE_ATU_TYPE_MEM, entry->res->start, 6050f71c60fSRob Herring entry->res->start - entry->offset, 6060f71c60fSRob Herring resource_size(entry->res)); 6079f9e59a4SRob Herring } 6089f9e59a4SRob Herring 6099f9e59a4SRob Herring if (pp->io_size) { 6109f9e59a4SRob Herring if (pci->num_viewport > ++atu_idx) 6119f9e59a4SRob Herring dw_pcie_prog_outbound_atu(pci, atu_idx, 6126e0832faSShawn Lin PCIE_ATU_TYPE_IO, pp->io_base, 6136e0832faSShawn Lin pp->io_bus_addr, pp->io_size); 6149f9e59a4SRob Herring else 6159f9e59a4SRob Herring pci->io_cfg_atu_shared = true; 6169f9e59a4SRob Herring } 6179f9e59a4SRob Herring 6189f9e59a4SRob Herring if (pci->num_viewport <= atu_idx) 6199f9e59a4SRob Herring dev_warn(pci->dev, "Resources exceed number of ATU entries (%d)", 6209f9e59a4SRob Herring pci->num_viewport); 6216e0832faSShawn Lin } 6226e0832faSShawn Lin 623f81c770dSRob Herring dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0); 6246e0832faSShawn Lin 6256e0832faSShawn Lin /* Program correct class for RC */ 626f81c770dSRob Herring dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI); 6276e0832faSShawn Lin 628f81c770dSRob Herring val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL); 6296e0832faSShawn Lin val |= PORT_LOGIC_SPEED_CHANGE; 630f81c770dSRob Herring dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val); 6313924bc2fSVidya Sagar 6323924bc2fSVidya Sagar dw_pcie_dbi_ro_wr_dis(pci); 6336e0832faSShawn Lin } 634ca98329dSVidya Sagar EXPORT_SYMBOL_GPL(dw_pcie_setup_rc); 635