blob: fbcb211cceb43b6901637b82689b984fc7915c99 [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.
6 * http://www.samsung.com
7 *
8 * Author: Jingoo Han <jg1.han@samsung.com>
9 */
10
11#include <linux/irqchip/chained_irq.h>
12#include <linux/irqdomain.h>
13#include <linux/of_address.h>
14#include <linux/of_pci.h>
15#include <linux/pci_regs.h>
16#include <linux/platform_device.h>
17
18#include "../../pci.h"
19#include "pcie-designware.h"
20
21static struct pci_ops dw_pcie_ops;
22
23static int dw_pcie_rd_own_conf(struct pcie_port *pp, int where, int size,
24 u32 *val)
25{
26 struct dw_pcie *pci;
27
28 if (pp->ops->rd_own_conf)
29 return pp->ops->rd_own_conf(pp, where, size, val);
30
31 pci = to_dw_pcie_from_pp(pp);
32 return dw_pcie_read(pci->dbi_base + where, size, val);
33}
34
35static int dw_pcie_wr_own_conf(struct pcie_port *pp, int where, int size,
36 u32 val)
37{
38 struct dw_pcie *pci;
39
40 if (pp->ops->wr_own_conf)
41 return pp->ops->wr_own_conf(pp, where, size, val);
42
43 pci = to_dw_pcie_from_pp(pp);
44 return dw_pcie_write(pci->dbi_base + where, size, val);
45}
46
47static void dw_msi_ack_irq(struct irq_data *d)
48{
49 irq_chip_ack_parent(d);
50}
51
52static void dw_msi_mask_irq(struct irq_data *d)
53{
54 pci_msi_mask_irq(d);
55 irq_chip_mask_parent(d);
56}
57
58static void dw_msi_unmask_irq(struct irq_data *d)
59{
60 pci_msi_unmask_irq(d);
61 irq_chip_unmask_parent(d);
62}
63
64static struct irq_chip dw_pcie_msi_irq_chip = {
65 .name = "PCI-MSI",
66 .irq_ack = dw_msi_ack_irq,
67 .irq_mask = dw_msi_mask_irq,
68 .irq_unmask = dw_msi_unmask_irq,
69};
70
71static struct msi_domain_info dw_pcie_msi_domain_info = {
72 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
73 MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
74 .chip = &dw_pcie_msi_irq_chip,
75};
76
77/* MSI int handler */
78irqreturn_t dw_handle_msi_irq(struct pcie_port *pp)
79{
80 int i, pos, irq;
Olivier Deprez0e641232021-09-23 10:07:05 +020081 unsigned long val;
82 u32 status, num_ctrls;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000083 irqreturn_t ret = IRQ_NONE;
84
85 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
86
87 for (i = 0; i < num_ctrls; i++) {
88 dw_pcie_rd_own_conf(pp, PCIE_MSI_INTR0_STATUS +
89 (i * MSI_REG_CTRL_BLOCK_SIZE),
Olivier Deprez0e641232021-09-23 10:07:05 +020090 4, &status);
91 if (!status)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092 continue;
93
94 ret = IRQ_HANDLED;
Olivier Deprez0e641232021-09-23 10:07:05 +020095 val = status;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000096 pos = 0;
Olivier Deprez0e641232021-09-23 10:07:05 +020097 while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000098 pos)) != MAX_MSI_IRQS_PER_CTRL) {
99 irq = irq_find_mapping(pp->irq_domain,
100 (i * MAX_MSI_IRQS_PER_CTRL) +
101 pos);
102 generic_handle_irq(irq);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000103 pos++;
104 }
105 }
106
107 return ret;
108}
109
110/* Chained MSI interrupt service routine */
111static void dw_chained_msi_isr(struct irq_desc *desc)
112{
113 struct irq_chip *chip = irq_desc_get_chip(desc);
114 struct pcie_port *pp;
115
116 chained_irq_enter(chip, desc);
117
118 pp = irq_desc_get_handler_data(desc);
119 dw_handle_msi_irq(pp);
120
121 chained_irq_exit(chip, desc);
122}
123
David Brazdil0f672f62019-12-10 10:32:29 +0000124static void dw_pci_setup_msi_msg(struct irq_data *d, struct msi_msg *msg)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000125{
David Brazdil0f672f62019-12-10 10:32:29 +0000126 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000127 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
128 u64 msi_target;
129
David Brazdil0f672f62019-12-10 10:32:29 +0000130 msi_target = (u64)pp->msi_data;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131
132 msg->address_lo = lower_32_bits(msi_target);
133 msg->address_hi = upper_32_bits(msi_target);
134
David Brazdil0f672f62019-12-10 10:32:29 +0000135 msg->data = d->hwirq;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000136
137 dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
David Brazdil0f672f62019-12-10 10:32:29 +0000138 (int)d->hwirq, msg->address_hi, msg->address_lo);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000139}
140
David Brazdil0f672f62019-12-10 10:32:29 +0000141static int dw_pci_msi_set_affinity(struct irq_data *d,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000142 const struct cpumask *mask, bool force)
143{
144 return -EINVAL;
145}
146
David Brazdil0f672f62019-12-10 10:32:29 +0000147static void dw_pci_bottom_mask(struct irq_data *d)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000148{
David Brazdil0f672f62019-12-10 10:32:29 +0000149 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000150 unsigned int res, bit, ctrl;
151 unsigned long flags;
152
153 raw_spin_lock_irqsave(&pp->lock, flags);
154
David Brazdil0f672f62019-12-10 10:32:29 +0000155 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
156 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
157 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000158
David Brazdil0f672f62019-12-10 10:32:29 +0000159 pp->irq_mask[ctrl] |= BIT(bit);
160 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_MASK + res, 4,
161 pp->irq_mask[ctrl]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000162
163 raw_spin_unlock_irqrestore(&pp->lock, flags);
164}
165
David Brazdil0f672f62019-12-10 10:32:29 +0000166static void dw_pci_bottom_unmask(struct irq_data *d)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000167{
David Brazdil0f672f62019-12-10 10:32:29 +0000168 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169 unsigned int res, bit, ctrl;
170 unsigned long flags;
171
172 raw_spin_lock_irqsave(&pp->lock, flags);
173
David Brazdil0f672f62019-12-10 10:32:29 +0000174 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
175 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
176 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000177
David Brazdil0f672f62019-12-10 10:32:29 +0000178 pp->irq_mask[ctrl] &= ~BIT(bit);
179 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_MASK + res, 4,
180 pp->irq_mask[ctrl]);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181
182 raw_spin_unlock_irqrestore(&pp->lock, flags);
183}
184
185static void dw_pci_bottom_ack(struct irq_data *d)
186{
David Brazdil0f672f62019-12-10 10:32:29 +0000187 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
188 unsigned int res, bit, ctrl;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000189
David Brazdil0f672f62019-12-10 10:32:29 +0000190 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
191 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
192 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193
David Brazdil0f672f62019-12-10 10:32:29 +0000194 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_STATUS + res, 4, BIT(bit));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195}
196
197static struct irq_chip dw_pci_msi_bottom_irq_chip = {
198 .name = "DWPCI-MSI",
199 .irq_ack = dw_pci_bottom_ack,
200 .irq_compose_msi_msg = dw_pci_setup_msi_msg,
201 .irq_set_affinity = dw_pci_msi_set_affinity,
202 .irq_mask = dw_pci_bottom_mask,
203 .irq_unmask = dw_pci_bottom_unmask,
204};
205
206static int dw_pcie_irq_domain_alloc(struct irq_domain *domain,
207 unsigned int virq, unsigned int nr_irqs,
208 void *args)
209{
210 struct pcie_port *pp = domain->host_data;
211 unsigned long flags;
212 u32 i;
213 int bit;
214
215 raw_spin_lock_irqsave(&pp->lock, flags);
216
217 bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors,
218 order_base_2(nr_irqs));
219
220 raw_spin_unlock_irqrestore(&pp->lock, flags);
221
222 if (bit < 0)
223 return -ENOSPC;
224
225 for (i = 0; i < nr_irqs; i++)
226 irq_domain_set_info(domain, virq + i, bit + i,
David Brazdil0f672f62019-12-10 10:32:29 +0000227 pp->msi_irq_chip,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000228 pp, handle_edge_irq,
229 NULL, NULL);
230
231 return 0;
232}
233
234static void dw_pcie_irq_domain_free(struct irq_domain *domain,
235 unsigned int virq, unsigned int nr_irqs)
236{
David Brazdil0f672f62019-12-10 10:32:29 +0000237 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
238 struct pcie_port *pp = irq_data_get_irq_chip_data(d);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000239 unsigned long flags;
240
241 raw_spin_lock_irqsave(&pp->lock, flags);
242
David Brazdil0f672f62019-12-10 10:32:29 +0000243 bitmap_release_region(pp->msi_irq_in_use, d->hwirq,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000244 order_base_2(nr_irqs));
245
246 raw_spin_unlock_irqrestore(&pp->lock, flags);
247}
248
249static const struct irq_domain_ops dw_pcie_msi_domain_ops = {
250 .alloc = dw_pcie_irq_domain_alloc,
251 .free = dw_pcie_irq_domain_free,
252};
253
254int dw_pcie_allocate_domains(struct pcie_port *pp)
255{
256 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
257 struct fwnode_handle *fwnode = of_node_to_fwnode(pci->dev->of_node);
258
259 pp->irq_domain = irq_domain_create_linear(fwnode, pp->num_vectors,
260 &dw_pcie_msi_domain_ops, pp);
261 if (!pp->irq_domain) {
262 dev_err(pci->dev, "Failed to create IRQ domain\n");
263 return -ENOMEM;
264 }
265
Olivier Deprez0e641232021-09-23 10:07:05 +0200266 irq_domain_update_bus_token(pp->irq_domain, DOMAIN_BUS_NEXUS);
267
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000268 pp->msi_domain = pci_msi_create_irq_domain(fwnode,
269 &dw_pcie_msi_domain_info,
270 pp->irq_domain);
271 if (!pp->msi_domain) {
272 dev_err(pci->dev, "Failed to create MSI domain\n");
273 irq_domain_remove(pp->irq_domain);
274 return -ENOMEM;
275 }
276
277 return 0;
278}
279
280void dw_pcie_free_msi(struct pcie_port *pp)
281{
David Brazdil0f672f62019-12-10 10:32:29 +0000282 if (pp->msi_irq) {
283 irq_set_chained_handler(pp->msi_irq, NULL);
284 irq_set_handler_data(pp->msi_irq, NULL);
285 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000286
287 irq_domain_remove(pp->msi_domain);
288 irq_domain_remove(pp->irq_domain);
David Brazdil0f672f62019-12-10 10:32:29 +0000289
290 if (pp->msi_page)
291 __free_page(pp->msi_page);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000292}
293
294void dw_pcie_msi_init(struct pcie_port *pp)
295{
296 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
297 struct device *dev = pci->dev;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000298 u64 msi_target;
299
David Brazdil0f672f62019-12-10 10:32:29 +0000300 pp->msi_page = alloc_page(GFP_KERNEL);
301 pp->msi_data = dma_map_page(dev, pp->msi_page, 0, PAGE_SIZE,
302 DMA_FROM_DEVICE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000303 if (dma_mapping_error(dev, pp->msi_data)) {
304 dev_err(dev, "Failed to map MSI data\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000305 __free_page(pp->msi_page);
306 pp->msi_page = NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000307 return;
308 }
309 msi_target = (u64)pp->msi_data;
310
311 /* Program the msi_data */
312 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_LO, 4,
313 lower_32_bits(msi_target));
314 dw_pcie_wr_own_conf(pp, PCIE_MSI_ADDR_HI, 4,
315 upper_32_bits(msi_target));
316}
David Brazdil0f672f62019-12-10 10:32:29 +0000317EXPORT_SYMBOL_GPL(dw_pcie_msi_init);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000318
319int dw_pcie_host_init(struct pcie_port *pp)
320{
321 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
322 struct device *dev = pci->dev;
323 struct device_node *np = dev->of_node;
324 struct platform_device *pdev = to_platform_device(dev);
325 struct resource_entry *win, *tmp;
David Brazdil0f672f62019-12-10 10:32:29 +0000326 struct pci_bus *child;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000327 struct pci_host_bridge *bridge;
328 struct resource *cfg_res;
David Brazdil0f672f62019-12-10 10:32:29 +0000329 u32 hdr_type;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000330 int ret;
331
332 raw_spin_lock_init(&pci->pp.lock);
333
334 cfg_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
335 if (cfg_res) {
336 pp->cfg0_size = resource_size(cfg_res) >> 1;
337 pp->cfg1_size = resource_size(cfg_res) >> 1;
338 pp->cfg0_base = cfg_res->start;
339 pp->cfg1_base = cfg_res->start + pp->cfg0_size;
340 } else if (!pp->va_cfg0_base) {
341 dev_err(dev, "Missing *config* reg space\n");
342 }
343
David Brazdil0f672f62019-12-10 10:32:29 +0000344 bridge = devm_pci_alloc_host_bridge(dev, 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000345 if (!bridge)
346 return -ENOMEM;
347
348 ret = devm_of_pci_get_host_bridge_resources(dev, 0, 0xff,
349 &bridge->windows, &pp->io_base);
350 if (ret)
351 return ret;
352
353 ret = devm_request_pci_bus_resources(dev, &bridge->windows);
354 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000355 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356
357 /* Get the I/O and memory ranges from DT */
358 resource_list_for_each_entry_safe(win, tmp, &bridge->windows) {
359 switch (resource_type(win->res)) {
360 case IORESOURCE_IO:
361 ret = devm_pci_remap_iospace(dev, win->res,
362 pp->io_base);
363 if (ret) {
364 dev_warn(dev, "Error %d: failed to map resource %pR\n",
365 ret, win->res);
366 resource_list_destroy_entry(win);
367 } else {
368 pp->io = win->res;
369 pp->io->name = "I/O";
370 pp->io_size = resource_size(pp->io);
371 pp->io_bus_addr = pp->io->start - win->offset;
372 }
373 break;
374 case IORESOURCE_MEM:
375 pp->mem = win->res;
376 pp->mem->name = "MEM";
377 pp->mem_size = resource_size(pp->mem);
378 pp->mem_bus_addr = pp->mem->start - win->offset;
379 break;
380 case 0:
381 pp->cfg = win->res;
382 pp->cfg0_size = resource_size(pp->cfg) >> 1;
383 pp->cfg1_size = resource_size(pp->cfg) >> 1;
384 pp->cfg0_base = pp->cfg->start;
385 pp->cfg1_base = pp->cfg->start + pp->cfg0_size;
386 break;
387 case IORESOURCE_BUS:
388 pp->busn = win->res;
389 break;
390 }
391 }
392
393 if (!pci->dbi_base) {
394 pci->dbi_base = devm_pci_remap_cfgspace(dev,
395 pp->cfg->start,
396 resource_size(pp->cfg));
397 if (!pci->dbi_base) {
398 dev_err(dev, "Error with ioremap\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000399 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000400 }
401 }
402
403 pp->mem_base = pp->mem->start;
404
405 if (!pp->va_cfg0_base) {
406 pp->va_cfg0_base = devm_pci_remap_cfgspace(dev,
407 pp->cfg0_base, pp->cfg0_size);
408 if (!pp->va_cfg0_base) {
409 dev_err(dev, "Error with ioremap in function\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000410 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000411 }
412 }
413
414 if (!pp->va_cfg1_base) {
415 pp->va_cfg1_base = devm_pci_remap_cfgspace(dev,
416 pp->cfg1_base,
417 pp->cfg1_size);
418 if (!pp->va_cfg1_base) {
419 dev_err(dev, "Error with ioremap\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000420 return -ENOMEM;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000421 }
422 }
423
424 ret = of_property_read_u32(np, "num-viewport", &pci->num_viewport);
425 if (ret)
426 pci->num_viewport = 2;
427
David Brazdil0f672f62019-12-10 10:32:29 +0000428 if (pci_msi_enabled()) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000429 /*
430 * If a specific SoC driver needs to change the
431 * default number of vectors, it needs to implement
432 * the set_num_vectors callback.
433 */
434 if (!pp->ops->set_num_vectors) {
435 pp->num_vectors = MSI_DEF_NUM_VECTORS;
436 } else {
437 pp->ops->set_num_vectors(pp);
438
439 if (pp->num_vectors > MAX_MSI_IRQS ||
440 pp->num_vectors == 0) {
441 dev_err(dev,
442 "Invalid number of vectors\n");
David Brazdil0f672f62019-12-10 10:32:29 +0000443 return -EINVAL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000444 }
445 }
446
447 if (!pp->ops->msi_host_init) {
David Brazdil0f672f62019-12-10 10:32:29 +0000448 pp->msi_irq_chip = &dw_pci_msi_bottom_irq_chip;
449
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450 ret = dw_pcie_allocate_domains(pp);
451 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000452 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000453
454 if (pp->msi_irq)
455 irq_set_chained_handler_and_data(pp->msi_irq,
456 dw_chained_msi_isr,
457 pp);
458 } else {
459 ret = pp->ops->msi_host_init(pp);
460 if (ret < 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000461 return ret;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000462 }
463 }
464
465 if (pp->ops->host_init) {
466 ret = pp->ops->host_init(pp);
467 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000468 goto err_free_msi;
469 }
470
471 ret = dw_pcie_rd_own_conf(pp, PCI_HEADER_TYPE, 1, &hdr_type);
472 if (ret != PCIBIOS_SUCCESSFUL) {
473 dev_err(pci->dev, "Failed reading PCI_HEADER_TYPE cfg space reg (ret: 0x%x)\n",
474 ret);
475 ret = pcibios_err_to_errno(ret);
476 goto err_free_msi;
477 }
478 if (hdr_type != PCI_HEADER_TYPE_BRIDGE) {
479 dev_err(pci->dev,
480 "PCIe controller is not set to bridge type (hdr_type: 0x%x)!\n",
481 hdr_type);
482 ret = -EIO;
483 goto err_free_msi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000484 }
485
486 pp->root_bus_nr = pp->busn->start;
487
488 bridge->dev.parent = dev;
489 bridge->sysdata = pp;
490 bridge->busnr = pp->root_bus_nr;
491 bridge->ops = &dw_pcie_ops;
492 bridge->map_irq = of_irq_parse_and_map_pci;
493 bridge->swizzle_irq = pci_common_swizzle;
494
495 ret = pci_scan_root_bus_bridge(bridge);
496 if (ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000497 goto err_free_msi;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000498
David Brazdil0f672f62019-12-10 10:32:29 +0000499 pp->root_bus = bridge->bus;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000500
501 if (pp->ops->scan_bus)
502 pp->ops->scan_bus(pp);
503
David Brazdil0f672f62019-12-10 10:32:29 +0000504 pci_bus_size_bridges(pp->root_bus);
505 pci_bus_assign_resources(pp->root_bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000506
David Brazdil0f672f62019-12-10 10:32:29 +0000507 list_for_each_entry(child, &pp->root_bus->children, node)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000508 pcie_bus_configure_settings(child);
509
David Brazdil0f672f62019-12-10 10:32:29 +0000510 pci_bus_add_devices(pp->root_bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000511 return 0;
512
David Brazdil0f672f62019-12-10 10:32:29 +0000513err_free_msi:
514 if (pci_msi_enabled() && !pp->ops->msi_host_init)
515 dw_pcie_free_msi(pp);
516 return ret;
517}
518EXPORT_SYMBOL_GPL(dw_pcie_host_init);
519
520void dw_pcie_host_deinit(struct pcie_port *pp)
521{
522 pci_stop_root_bus(pp->root_bus);
523 pci_remove_root_bus(pp->root_bus);
524 if (pci_msi_enabled() && !pp->ops->msi_host_init)
525 dw_pcie_free_msi(pp);
526}
527EXPORT_SYMBOL_GPL(dw_pcie_host_deinit);
528
529static int dw_pcie_access_other_conf(struct pcie_port *pp, struct pci_bus *bus,
530 u32 devfn, int where, int size, u32 *val,
531 bool write)
532{
533 int ret, type;
534 u32 busdev, cfg_size;
535 u64 cpu_addr;
536 void __iomem *va_cfg_base;
537 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
538
539 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
540 PCIE_ATU_FUNC(PCI_FUNC(devfn));
541
542 if (bus->parent->number == pp->root_bus_nr) {
543 type = PCIE_ATU_TYPE_CFG0;
544 cpu_addr = pp->cfg0_base;
545 cfg_size = pp->cfg0_size;
546 va_cfg_base = pp->va_cfg0_base;
547 } else {
548 type = PCIE_ATU_TYPE_CFG1;
549 cpu_addr = pp->cfg1_base;
550 cfg_size = pp->cfg1_size;
551 va_cfg_base = pp->va_cfg1_base;
552 }
553
554 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
555 type, cpu_addr,
556 busdev, cfg_size);
557 if (write)
558 ret = dw_pcie_write(va_cfg_base + where, size, *val);
559 else
560 ret = dw_pcie_read(va_cfg_base + where, size, val);
561
562 if (pci->num_viewport <= 2)
563 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX1,
564 PCIE_ATU_TYPE_IO, pp->io_base,
565 pp->io_bus_addr, pp->io_size);
566
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000567 return ret;
568}
569
570static int dw_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus,
571 u32 devfn, int where, int size, u32 *val)
572{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000573 if (pp->ops->rd_other_conf)
David Brazdil0f672f62019-12-10 10:32:29 +0000574 return pp->ops->rd_other_conf(pp, bus, devfn, where,
575 size, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000576
David Brazdil0f672f62019-12-10 10:32:29 +0000577 return dw_pcie_access_other_conf(pp, bus, devfn, where, size, val,
578 false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000579}
580
581static int dw_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus,
582 u32 devfn, int where, int size, u32 val)
583{
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000584 if (pp->ops->wr_other_conf)
David Brazdil0f672f62019-12-10 10:32:29 +0000585 return pp->ops->wr_other_conf(pp, bus, devfn, where,
586 size, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000587
David Brazdil0f672f62019-12-10 10:32:29 +0000588 return dw_pcie_access_other_conf(pp, bus, devfn, where, size, &val,
589 true);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000590}
591
592static int dw_pcie_valid_device(struct pcie_port *pp, struct pci_bus *bus,
593 int dev)
594{
595 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
596
597 /* If there is no link, then there is no device */
598 if (bus->number != pp->root_bus_nr) {
599 if (!dw_pcie_link_up(pci))
600 return 0;
601 }
602
603 /* Access only one slot on each root port */
604 if (bus->number == pp->root_bus_nr && dev > 0)
605 return 0;
606
607 return 1;
608}
609
610static int dw_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where,
611 int size, u32 *val)
612{
613 struct pcie_port *pp = bus->sysdata;
614
615 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn))) {
616 *val = 0xffffffff;
617 return PCIBIOS_DEVICE_NOT_FOUND;
618 }
619
620 if (bus->number == pp->root_bus_nr)
621 return dw_pcie_rd_own_conf(pp, where, size, val);
622
623 return dw_pcie_rd_other_conf(pp, bus, devfn, where, size, val);
624}
625
626static int dw_pcie_wr_conf(struct pci_bus *bus, u32 devfn,
627 int where, int size, u32 val)
628{
629 struct pcie_port *pp = bus->sysdata;
630
631 if (!dw_pcie_valid_device(pp, bus, PCI_SLOT(devfn)))
632 return PCIBIOS_DEVICE_NOT_FOUND;
633
634 if (bus->number == pp->root_bus_nr)
635 return dw_pcie_wr_own_conf(pp, where, size, val);
636
637 return dw_pcie_wr_other_conf(pp, bus, devfn, where, size, val);
638}
639
640static struct pci_ops dw_pcie_ops = {
641 .read = dw_pcie_rd_conf,
642 .write = dw_pcie_wr_conf,
643};
644
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000645void dw_pcie_setup_rc(struct pcie_port *pp)
646{
647 u32 val, ctrl, num_ctrls;
648 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
649
David Brazdil0f672f62019-12-10 10:32:29 +0000650 /*
651 * Enable DBI read-only registers for writing/updating configuration.
652 * Write permission gets disabled towards the end of this function.
653 */
654 dw_pcie_dbi_ro_wr_en(pci);
655
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000656 dw_pcie_setup(pci);
657
David Brazdil0f672f62019-12-10 10:32:29 +0000658 if (!pp->ops->msi_host_init) {
659 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000660
David Brazdil0f672f62019-12-10 10:32:29 +0000661 /* Initialize IRQ Status array */
662 for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
663 pp->irq_mask[ctrl] = ~0;
664 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_MASK +
665 (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
666 4, pp->irq_mask[ctrl]);
667 dw_pcie_wr_own_conf(pp, PCIE_MSI_INTR0_ENABLE +
668 (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
669 4, ~0);
670 }
671 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000672
673 /* Setup RC BARs */
674 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
675 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
676
677 /* Setup interrupt pins */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000678 val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
679 val &= 0xffff00ff;
680 val |= 0x00000100;
681 dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000682
683 /* Setup bus numbers */
684 val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
685 val &= 0xff000000;
686 val |= 0x00ff0100;
687 dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
688
689 /* Setup command register */
690 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
691 val &= 0xffff0000;
692 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
693 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
694 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
695
696 /*
697 * If the platform provides ->rd_other_conf, it means the platform
698 * uses its own address translation component rather than ATU, so
699 * we should not program the ATU here.
700 */
701 if (!pp->ops->rd_other_conf) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000702 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX0,
703 PCIE_ATU_TYPE_MEM, pp->mem_base,
704 pp->mem_bus_addr, pp->mem_size);
705 if (pci->num_viewport > 2)
706 dw_pcie_prog_outbound_atu(pci, PCIE_ATU_REGION_INDEX2,
707 PCIE_ATU_TYPE_IO, pp->io_base,
708 pp->io_bus_addr, pp->io_size);
709 }
710
711 dw_pcie_wr_own_conf(pp, PCI_BASE_ADDRESS_0, 4, 0);
712
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000713 /* Program correct class for RC */
714 dw_pcie_wr_own_conf(pp, PCI_CLASS_DEVICE, 2, PCI_CLASS_BRIDGE_PCI);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000715
716 dw_pcie_rd_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, &val);
717 val |= PORT_LOGIC_SPEED_CHANGE;
718 dw_pcie_wr_own_conf(pp, PCIE_LINK_WIDTH_SPEED_CONTROL, 4, val);
David Brazdil0f672f62019-12-10 10:32:29 +0000719
720 dw_pcie_dbi_ro_wr_dis(pci);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721}
David Brazdil0f672f62019-12-10 10:32:29 +0000722EXPORT_SYMBOL_GPL(dw_pcie_setup_rc);