blob: 42d8116a4a0021e0af6339480393a0074a833012 [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Synopsys DesignWare PCIe host controller driver
4 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
Olivier Deprez157378f2022-04-04 15:47:50 +02006 * https://www.samsung.com
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 *
8 * Author: Jingoo Han <jg1.han@samsung.com>
9 */
10
11#include <linux/irqchip/chained_irq.h>
12#include <linux/irqdomain.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020013#include <linux/msi.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000014#include <linux/of_address.h>
15#include <linux/of_pci.h>
16#include <linux/pci_regs.h>
17#include <linux/platform_device.h>
18
19#include "../../pci.h"
20#include "pcie-designware.h"
21
22static struct pci_ops dw_pcie_ops;
Olivier Deprez157378f2022-04-04 15:47:50 +020023static struct pci_ops dw_child_pcie_ops;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024
25static void dw_msi_ack_irq(struct irq_data *d)
26{
27 irq_chip_ack_parent(d);
28}
29
30static void dw_msi_mask_irq(struct irq_data *d)
31{
32 pci_msi_mask_irq(d);
33 irq_chip_mask_parent(d);
34}
35
36static void dw_msi_unmask_irq(struct irq_data *d)
37{
38 pci_msi_unmask_irq(d);
39 irq_chip_unmask_parent(d);
40}
41
42static struct irq_chip dw_pcie_msi_irq_chip = {
43 .name = "PCI-MSI",
44 .irq_ack = dw_msi_ack_irq,
45 .irq_mask = dw_msi_mask_irq,
46 .irq_unmask = dw_msi_unmask_irq,
47};
48
49static struct msi_domain_info dw_pcie_msi_domain_info = {
50 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
51 MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
52 .chip = &dw_pcie_msi_irq_chip,
53};
54
55/* MSI int handler */
56irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
57{
58 int i, pos, irq;
Olivier Deprez0e641232021-09-23 10:07:05 +020059 unsigned long val;
60 u32 status, num_ctrls;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000061 irqreturn_t ret = IRQ_NONE;
Olivier Deprez157378f2022-04-04 15:47:50 +020062 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063
64 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
65
66 for (i = 0; i < num_ctrls; i++) {
Olivier Deprez157378f2022-04-04 15:47:50 +020067 status = dw_pcie_readl_dbi(pci, PCIE_MSI_INTR0_STATUS +
68 (i * MSI_REG_CTRL_BLOCK_SIZE));
Olivier Deprez0e641232021-09-23 10:07:05 +020069 if (!status)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070 continue;
71
72 ret = IRQ_HANDLED;
Olivier Deprez0e641232021-09-23 10:07:05 +020073 val = status;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000074 pos = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +020075 while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 pos)) != MAX_MSI_IRQS_PER_CTRL) {
77 irq = irq_find_mapping(pp->irq_domain,
78 (i * MAX_MSI_IRQS_PER_CTRL) +
79 pos);
80 generic_handle_irq(irq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000081 pos++;
82 }
83 }
84
85 return ret;
86}
87
88/* Chained MSI interrupt service routine */
89static void dw_chained_msi_isr(struct irq_desc *desc)
90{
91 struct irq_chip *chip = irq_desc_get_chip(desc);
92 struct pcie_port *pp;
93
94 chained_irq_enter(chip, desc);
95
96 pp = irq_desc_get_handler_data(desc);
97 dw_handle_msi_irq(pp);
98
99 chained_irq_exit(chip, desc);
100}
101
David Brazdil0f672f62019-12-10 10:32:29 +0000102static void dw_pci_setup_msi_msg(struct irq_data *d, struct msi_msg *msg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103{
David Brazdil0f672f62019-12-10 10:32:29 +0000104 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000105 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
106 u64 msi_target;
107
David Brazdil0f672f62019-12-10 10:32:29 +0000108 msi_target = (u64)pp->msi_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109
110 msg->address_lo = lower_32_bits(msi_target);
111 msg->address_hi = upper_32_bits(msi_target);
112
David Brazdil0f672f62019-12-10 10:32:29 +0000113 msg->data = d->hwirq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114
115 dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000116 (int)d->hwirq, msg->address_hi, msg->address_lo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000117}
118
David Brazdil0f672f62019-12-10 10:32:29 +0000119static int dw_pci_msi_set_affinity(struct irq_data *d,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120 const struct cpumask *mask, bool force)
121{
122 return -EINVAL;
123}
124
David Brazdil0f672f62019-12-10 10:32:29 +0000125static void dw_pci_bottom_mask(struct irq_data *d)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000126{
David Brazdil0f672f62019-12-10 10:32:29 +0000127 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Olivier Deprez157378f2022-04-04 15:47:50 +0200128 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129 unsigned int res, bit, ctrl;
130 unsigned long flags;
131
132 raw_spin_lock_irqsave(&pp->lock, flags);
133
David Brazdil0f672f62019-12-10 10:32:29 +0000134 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
135 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
136 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137
David Brazdil0f672f62019-12-10 10:32:29 +0000138 pp->irq_mask[ctrl] |= BIT(bit);
Olivier Deprez157378f2022-04-04 15:47:50 +0200139 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000140
141 raw_spin_unlock_irqrestore(&pp->lock, flags);
142}
143
David Brazdil0f672f62019-12-10 10:32:29 +0000144static void dw_pci_bottom_unmask(struct irq_data *d)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000145{
David Brazdil0f672f62019-12-10 10:32:29 +0000146 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Olivier Deprez157378f2022-04-04 15:47:50 +0200147 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148 unsigned int res, bit, ctrl;
149 unsigned long flags;
150
151 raw_spin_lock_irqsave(&pp->lock, flags);
152
David Brazdil0f672f62019-12-10 10:32:29 +0000153 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
154 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
155 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156
David Brazdil0f672f62019-12-10 10:32:29 +0000157 pp->irq_mask[ctrl] &= ~BIT(bit);
Olivier Deprez157378f2022-04-04 15:47:50 +0200158 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000159
160 raw_spin_unlock_irqrestore(&pp->lock, flags);
161}
162
163static void dw_pci_bottom_ack(struct irq_data *d)
164{
David Brazdil0f672f62019-12-10 10:32:29 +0000165 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Olivier Deprez157378f2022-04-04 15:47:50 +0200166 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
David Brazdil0f672f62019-12-10 10:32:29 +0000167 unsigned int res, bit, ctrl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000168
David Brazdil0f672f62019-12-10 10:32:29 +0000169 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
170 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
171 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000172
Olivier Deprez157378f2022-04-04 15:47:50 +0200173 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_STATUS + res, BIT(bit));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000174}
175
176static struct irq_chip dw_pci_msi_bottom_irq_chip = {
177 .name = "DWPCI-MSI",
178 .irq_ack = dw_pci_bottom_ack,
179 .irq_compose_msi_msg = dw_pci_setup_msi_msg,
180 .irq_set_affinity = dw_pci_msi_set_affinity,
181 .irq_mask = dw_pci_bottom_mask,
182 .irq_unmask = dw_pci_bottom_unmask,
183};
184
185static int dw_pcie_irq_domain_alloc(struct irq_domain *domain,
186 unsigned int virq, unsigned int nr_irqs,
187 void *args)
188{
189 struct pcie_port *pp = domain->host_data;
190 unsigned long flags;
191 u32 i;
192 int bit;
193
194 raw_spin_lock_irqsave(&pp->lock, flags);
195
196 bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors,
197 order_base_2(nr_irqs));
198
199 raw_spin_unlock_irqrestore(&pp->lock, flags);
200
201 if (bit < 0)
202 return -ENOSPC;
203
204 for (i = 0; i < nr_irqs; i++)
205 irq_domain_set_info(domain, virq + i, bit + i,
David Brazdil0f672f62019-12-10 10:32:29 +0000206 pp->msi_irq_chip,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000207 pp, handle_edge_irq,
208 NULL, NULL);
209
210 return 0;
211}
212
213static void dw_pcie_irq_domain_free(struct irq_domain *domain,
214 unsigned int virq, unsigned int nr_irqs)
215{
David Brazdil0f672f62019-12-10 10:32:29 +0000216 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
Olivier Deprez157378f2022-04-04 15:47:50 +0200217 struct pcie_port *pp = domain->host_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218 unsigned long flags;
219
220 raw_spin_lock_irqsave(&pp->lock, flags);
221
David Brazdil0f672f62019-12-10 10:32:29 +0000222 bitmap_release_region(pp->msi_irq_in_use, d->hwirq,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000223 order_base_2(nr_irqs));
224
225 raw_spin_unlock_irqrestore(&pp->lock, flags);
226}
227
228static const struct irq_domain_ops dw_pcie_msi_domain_ops = {
229 .alloc = dw_pcie_irq_domain_alloc,
230 .free = dw_pcie_irq_domain_free,
231};
232
233int dw_pcie_allocate_domains(struct pcie_port *pp)
234{
235 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
236 struct fwnode_handle *fwnode = of_node_to_fwnode(pci->dev->of_node);
237
238 pp->irq_domain = irq_domain_create_linear(fwnode, pp->num_vectors,
239 &dw_pcie_msi_domain_ops, pp);
240 if (!pp->irq_domain) {
241 dev_err(pci->dev, "Failed to create IRQ domain\n");
242 return -ENOMEM;
243 }
244
Olivier Deprez0e641232021-09-23 10:07:05 +0200245 irq_domain_update_bus_token(pp->irq_domain, DOMAIN_BUS_NEXUS);
246
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 pp->msi_domain = pci_msi_create_irq_domain(fwnode,
248 &dw_pcie_msi_domain_info,
249 pp->irq_domain);
250 if (!pp->msi_domain) {
251 dev_err(pci->dev, "Failed to create MSI domain\n");
252 irq_domain_remove(pp->irq_domain);
253 return -ENOMEM;
254 }
255
256 return 0;
257}
258
259void dw_pcie_free_msi(struct pcie_port *pp)
260{
David Brazdil0f672f62019-12-10 10:32:29 +0000261 if (pp->msi_irq) {
262 irq_set_chained_handler(pp->msi_irq, NULL);
263 irq_set_handler_data(pp->msi_irq, NULL);
264 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000265
266 irq_domain_remove(pp->msi_domain);
267 irq_domain_remove(pp->irq_domain);
David Brazdil0f672f62019-12-10 10:32:29 +0000268
Olivier Deprez157378f2022-04-04 15:47:50 +0200269 if (pp->msi_data) {
270 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
271 struct device *dev = pci->dev;
272
273 dma_unmap_single_attrs(dev, pp->msi_data, sizeof(pp->msi_msg),
274 DMA_FROM_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
275 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276}
277
278void dw_pcie_msi_init(struct pcie_port *pp)
279{
280 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Olivier Deprez157378f2022-04-04 15:47:50 +0200281 u64 msi_target = (u64)pp->msi_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000282
Olivier Deprez157378f2022-04-04 15:47:50 +0200283 if (!IS_ENABLED(CONFIG_PCI_MSI))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000284 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000285
286 /* Program the msi_data */
Olivier Deprez157378f2022-04-04 15:47:50 +0200287 dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_LO, lower_32_bits(msi_target));
288 dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_HI, upper_32_bits(msi_target));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000289}
David Brazdil0f672f62019-12-10 10:32:29 +0000290EXPORT_SYMBOL_GPL(dw_pcie_msi_init);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000291
292int dw_pcie_host_init(struct pcie_port *pp)
293{
294 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
295 struct device *dev = pci->dev;
296 struct device_node *np = dev->of_node;
297 struct platform_device *pdev = to_platform_device(dev);
Olivier Deprez157378f2022-04-04 15:47:50 +0200298 struct resource_entry *win;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000299 struct pci_host_bridge *bridge;
300 struct resource *cfg_res;
301 int ret;
302
303 raw_spin_lock_init(&pci->pp.lock);
304
305 cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
306 if (cfg_res) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200307 pp->cfg0_size = resource_size(cfg_res);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000308 pp->cfg0_base = cfg_res->start;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000309 } else if (!pp->va_cfg0_base) {
310 dev_err(dev, "Missing *config* reg space\n");
311 }
312
David Brazdil0f672f62019-12-10 10:32:29 +0000313 bridge = devm_pci_alloc_host_bridge(dev, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000314 if (!bridge)
315 return -ENOMEM;
316
Olivier Deprez157378f2022-04-04 15:47:50 +0200317 pp->bridge = bridge;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000318
319 /* Get the I/O and memory ranges from DT */
Olivier Deprez157378f2022-04-04 15:47:50 +0200320 resource_list_for_each_entry(win, &bridge->windows) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000321 switch (resource_type(win->res)) {
322 case IORESOURCE_IO:
Olivier Deprez157378f2022-04-04 15:47:50 +0200323 pp->io_size = resource_size(win->res);
324 pp->io_bus_addr = win->res->start - win->offset;
325 pp->io_base = pci_pio_to_address(win->res->start);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000326 break;
327 case 0:
Olivier Deprez157378f2022-04-04 15:47:50 +0200328 dev_err(dev, "Missing *config* reg space\n");
329 pp->cfg0_size = resource_size(win->res);
330 pp->cfg0_base = win->res->start;
331 if (!pci->dbi_base) {
332 pci->dbi_base = devm_pci_remap_cfgspace(dev,
333 pp->cfg0_base,
334 pp->cfg0_size);
335 if (!pci->dbi_base) {
336 dev_err(dev, "Error with ioremap\n");
337 return -ENOMEM;
338 }
339 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000340 break;
341 }
342 }
343
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000344 if (!pp->va_cfg0_base) {
345 pp->va_cfg0_base = devm_pci_remap_cfgspace(dev,
346 pp->cfg0_base, pp->cfg0_size);
347 if (!pp->va_cfg0_base) {
348 dev_err(dev, "Error with ioremap in function\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000349 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000350 }
351 }
352
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000353 ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport);
354 if (ret)
355 pci->num_viewport = 2;
356
Olivier Deprez157378f2022-04-04 15:47:50 +0200357 if (pci->link_gen < 1)
358 pci->link_gen = of_pci_get_max_link_speed(np);
359
David Brazdil0f672f62019-12-10 10:32:29 +0000360 if (pci_msi_enabled()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361 /*
362 * If a specific SoC driver needs to change the
363 * default number of vectors, it needs to implement
364 * the set_num_vectors callback.
365 */
366 if (!pp->ops->set_num_vectors) {
367 pp->num_vectors = MSI_DEF_NUM_VECTORS;
368 } else {
369 pp->ops->set_num_vectors(pp);
370
371 if (pp->num_vectors > MAX_MSI_IRQS ||
372 pp->num_vectors == 0) {
373 dev_err(dev,
374 "Invalid number of vectors\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000375 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000376 }
377 }
378
379 if (!pp->ops->msi_host_init) {
David Brazdil0f672f62019-12-10 10:32:29 +0000380 pp->msi_irq_chip = &dw_pci_msi_bottom_irq_chip;
381
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382 ret = dw_pcie_allocate_domains(pp);
383 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000384 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000385
386 if (pp->msi_irq)
387 irq_set_chained_handler_and_data(pp->msi_irq,
388 dw_chained_msi_isr,
389 pp);
Olivier Deprez157378f2022-04-04 15:47:50 +0200390
391 pp->msi_data = dma_map_single_attrs(pci->dev, &pp->msi_msg,
392 sizeof(pp->msi_msg),
393 DMA_FROM_DEVICE,
394 DMA_ATTR_SKIP_CPU_SYNC);
Olivier Deprez92d4c212022-12-06 15:05:30 +0100395 ret = dma_mapping_error(pci->dev, pp->msi_data);
396 if (ret) {
Olivier Deprez157378f2022-04-04 15:47:50 +0200397 dev_err(pci->dev, "Failed to map MSI data\n");
398 pp->msi_data = 0;
399 goto err_free_msi;
400 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000401 } else {
402 ret = pp->ops->msi_host_init(pp);
403 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000404 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000405 }
406 }
407
Olivier Deprez157378f2022-04-04 15:47:50 +0200408 /* Set default bus ops */
409 bridge->ops = &dw_pcie_ops;
410 bridge->child_ops = &dw_child_pcie_ops;
411
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000412 if (pp->ops->host_init) {
413 ret = pp->ops->host_init(pp);
414 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000415 goto err_free_msi;
416 }
417
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000418 bridge->sysdata = pp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000419
Olivier Deprez157378f2022-04-04 15:47:50 +0200420 ret = pci_host_probe(bridge);
421 if (!ret)
422 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000423
David Brazdil0f672f62019-12-10 10:32:29 +0000424err_free_msi:
425 if (pci_msi_enabled() && !pp->ops->msi_host_init)
426 dw_pcie_free_msi(pp);
427 return ret;
428}
429EXPORT_SYMBOL_GPL(dw_pcie_host_init);
430
431void dw_pcie_host_deinit(struct pcie_port *pp)
432{
Olivier Deprez157378f2022-04-04 15:47:50 +0200433 pci_stop_root_bus(pp->bridge->bus);
434 pci_remove_root_bus(pp->bridge->bus);
David Brazdil0f672f62019-12-10 10:32:29 +0000435 if (pci_msi_enabled() && !pp->ops->msi_host_init)
436 dw_pcie_free_msi(pp);
437}
438EXPORT_SYMBOL_GPL(dw_pcie_host_deinit);
439
Olivier Deprez157378f2022-04-04 15:47:50 +0200440static void __iomem *dw_pcie_other_conf_map_bus(struct pci_bus *bus,
441 unsigned int devfn, int where)
David Brazdil0f672f62019-12-10 10:32:29 +0000442{
Olivier Deprez157378f2022-04-04 15:47:50 +0200443 int type;
444 u32 busdev;
445 struct pcie_port *pp = bus->sysdata;
David Brazdil0f672f62019-12-10 10:32:29 +0000446 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
447
Olivier Deprez157378f2022-04-04 15:47:50 +0200448 /*
449 * Checking whether the link is up here is a last line of defense
450 * against platforms that forward errors on the system bus as
451 * SError upon PCI configuration transactions issued when the link
452 * is down. This check is racy by definition and does not stop
453 * the system from triggering an SError if the link goes down
454 * after this check is performed.
455 */
456 if (!dw_pcie_link_up(pci))
457 return NULL;
458
David Brazdil0f672f62019-12-10 10:32:29 +0000459 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
460 PCIE_ATU_FUNC(PCI_FUNC(devfn));
461
Olivier Deprez157378f2022-04-04 15:47:50 +0200462 if (pci_is_root_bus(bus->parent))
David Brazdil0f672f62019-12-10 10:32:29 +0000463 type = PCIE_ATU_TYPE_CFG0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200464 else
David Brazdil0f672f62019-12-10 10:32:29 +0000465 type = PCIE_ATU_TYPE_CFG1;
Olivier Deprez157378f2022-04-04 15:47:50 +0200466
David Brazdil0f672f62019-12-10 10:32:29 +0000467
468 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
Olivier Deprez157378f2022-04-04 15:47:50 +0200469 type, pp->cfg0_base,
470 busdev, pp->cfg0_size);
David Brazdil0f672f62019-12-10 10:32:29 +0000471
Olivier Deprez157378f2022-04-04 15:47:50 +0200472 return pp->va_cfg0_base + where;
473}
474
475static int dw_pcie_rd_other_conf(struct pci_bus *bus, unsigned int devfn,
476 int where, int size, u32 *val)
477{
478 int ret;
479 struct pcie_port *pp = bus->sysdata;
480 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
481
482 ret = pci_generic_config_read(bus, devfn, where, size, val);
483
484 if (!ret && pci->num_viewport <= 2)
David Brazdil0f672f62019-12-10 10:32:29 +0000485 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
486 PCIE_ATU_TYPE_IO, pp->io_base,
487 pp->io_bus_addr, pp->io_size);
488
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000489 return ret;
490}
491
Olivier Deprez157378f2022-04-04 15:47:50 +0200492static int dw_pcie_wr_other_conf(struct pci_bus *bus, unsigned int devfn,
493 int where, int size, u32 val)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000494{
Olivier Deprez157378f2022-04-04 15:47:50 +0200495 int ret;
496 struct pcie_port *pp = bus->sysdata;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000497 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
498
Olivier Deprez157378f2022-04-04 15:47:50 +0200499 ret = pci_generic_config_write(bus, devfn, where, size, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500
Olivier Deprez157378f2022-04-04 15:47:50 +0200501 if (!ret && pci->num_viewport <= 2)
502 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
503 PCIE_ATU_TYPE_IO, pp->io_base,
504 pp->io_bus_addr, pp->io_size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000505
Olivier Deprez157378f2022-04-04 15:47:50 +0200506 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000507}
508
Olivier Deprez157378f2022-04-04 15:47:50 +0200509static struct pci_ops dw_child_pcie_ops = {
510 .map_bus = dw_pcie_other_conf_map_bus,
511 .read = dw_pcie_rd_other_conf,
512 .write = dw_pcie_wr_other_conf,
513};
514
515void __iomem *dw_pcie_own_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000516{
517 struct pcie_port *pp = bus->sysdata;
Olivier Deprez157378f2022-04-04 15:47:50 +0200518 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000519
Olivier Deprez157378f2022-04-04 15:47:50 +0200520 if (PCI_SLOT(devfn) > 0)
521 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000522
Olivier Deprez157378f2022-04-04 15:47:50 +0200523 return pci->dbi_base + where;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000524}
Olivier Deprez157378f2022-04-04 15:47:50 +0200525EXPORT_SYMBOL_GPL(dw_pcie_own_conf_map_bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000526
527static struct pci_ops dw_pcie_ops = {
Olivier Deprez157378f2022-04-04 15:47:50 +0200528 .map_bus = dw_pcie_own_conf_map_bus,
529 .read = pci_generic_config_read,
530 .write = pci_generic_config_write,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000531};
532
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533void dw_pcie_setup_rc(struct pcie_port *pp)
534{
535 u32 val, ctrl, num_ctrls;
536 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
537
David Brazdil0f672f62019-12-10 10:32:29 +0000538 /*
539 * Enable DBI read-only registers for writing/updating configuration.
540 * Write permission gets disabled towards the end of this function.
541 */
542 dw_pcie_dbi_ro_wr_en(pci);
543
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000544 dw_pcie_setup(pci);
545
Olivier Deprez157378f2022-04-04 15:47:50 +0200546 if (pci_msi_enabled() && !pp->ops->msi_host_init) {
David Brazdil0f672f62019-12-10 10:32:29 +0000547 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000548
David Brazdil0f672f62019-12-10 10:32:29 +0000549 /* Initialize IRQ Status array */
550 for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
551 pp->irq_mask[ctrl] = ~0;
Olivier Deprez157378f2022-04-04 15:47:50 +0200552 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK +
David Brazdil0f672f62019-12-10 10:32:29 +0000553 (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
Olivier Deprez157378f2022-04-04 15:47:50 +0200554 pp->irq_mask[ctrl]);
555 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_ENABLE +
David Brazdil0f672f62019-12-10 10:32:29 +0000556 (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
Olivier Deprez157378f2022-04-04 15:47:50 +0200557 ~0);
David Brazdil0f672f62019-12-10 10:32:29 +0000558 }
559 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000560
561 /* Setup RC BARs */
562 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
563 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
564
565 /* Setup interrupt pins */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000566 val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
567 val &= 0xffff00ff;
568 val |= 0x00000100;
569 dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000570
571 /* Setup bus numbers */
572 val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
573 val &= 0xff000000;
574 val |= 0x00ff0100;
575 dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
576
577 /* Setup command register */
578 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
579 val &= 0xffff0000;
580 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
581 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
582 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
583
584 /*
Olivier Deprez157378f2022-04-04 15:47:50 +0200585 * If the platform provides its own child bus config accesses, it means
586 * the platform uses its own address translation component rather than
587 * ATU, so we should not program the ATU here.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000588 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200589 if (pp->bridge->child_ops == &dw_child_pcie_ops) {
590 struct resource_entry *tmp, *entry = NULL;
591
592 /* Get last memory resource entry */
593 resource_list_for_each_entry(tmp, &pp->bridge->windows)
594 if (resource_type(tmp->res) == IORESOURCE_MEM)
595 entry = tmp;
596
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000597 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0,
Olivier Deprez157378f2022-04-04 15:47:50 +0200598 PCIE_ATU_TYPE_MEM, entry->res->start,
599 entry->res->start - entry->offset,
600 resource_size(entry->res));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000601 if (pci->num_viewport > 2)
602 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2,
603 PCIE_ATU_TYPE_IO, pp->io_base,
604 pp->io_bus_addr, pp->io_size);
605 }
606
Olivier Deprez157378f2022-04-04 15:47:50 +0200607 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000608
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000609 /* Program correct class for RC */
Olivier Deprez157378f2022-04-04 15:47:50 +0200610 dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000611
Olivier Deprez157378f2022-04-04 15:47:50 +0200612 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000613 val |= PORT_LOGIC_SPEED_CHANGE;
Olivier Deprez157378f2022-04-04 15:47:50 +0200614 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
David Brazdil0f672f62019-12-10 10:32:29 +0000615
616 dw_pcie_dbi_ro_wr_dis(pci);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000617}
David Brazdil0f672f62019-12-10 10:32:29 +0000618EXPORT_SYMBOL_GPL(dw_pcie_setup_rc);