1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Synopsys DesignWare PCIe host controller driver
4 *
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * https://www.samsung.com
7 *
8 * Author: Jingoo Han <[email protected]>
9 */
10
11 #include <linux/iopoll.h>
12 #include <linux/irqchip/chained_irq.h>
13 #include <linux/irqdomain.h>
14 #include <linux/msi.h>
15 #include <linux/of_address.h>
16 #include <linux/of_pci.h>
17 #include <linux/pci_regs.h>
18 #include <linux/platform_device.h>
19
20 #include "../../pci.h"
21 #include "pcie-designware.h"
22
23 static struct pci_ops dw_pcie_ops;
24 static struct pci_ops dw_child_pcie_ops;
25
dw_msi_ack_irq(struct irq_data * d)26 static void dw_msi_ack_irq(struct irq_data *d)
27 {
28 irq_chip_ack_parent(d);
29 }
30
dw_msi_mask_irq(struct irq_data * d)31 static void dw_msi_mask_irq(struct irq_data *d)
32 {
33 pci_msi_mask_irq(d);
34 irq_chip_mask_parent(d);
35 }
36
dw_msi_unmask_irq(struct irq_data * d)37 static void dw_msi_unmask_irq(struct irq_data *d)
38 {
39 pci_msi_unmask_irq(d);
40 irq_chip_unmask_parent(d);
41 }
42
43 static struct irq_chip dw_pcie_msi_irq_chip = {
44 .name = "PCI-MSI",
45 .irq_ack = dw_msi_ack_irq,
46 .irq_mask = dw_msi_mask_irq,
47 .irq_unmask = dw_msi_unmask_irq,
48 };
49
50 static struct msi_domain_info dw_pcie_msi_domain_info = {
51 .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
52 MSI_FLAG_NO_AFFINITY | MSI_FLAG_PCI_MSIX |
53 MSI_FLAG_MULTI_PCI_MSI,
54 .chip = &dw_pcie_msi_irq_chip,
55 };
56
57 /* MSI int handler */
dw_handle_msi_irq(struct dw_pcie_rp * pp)58 irqreturn_t dw_handle_msi_irq(struct dw_pcie_rp *pp)
59 {
60 int i, pos;
61 unsigned long val;
62 u32 status, num_ctrls;
63 irqreturn_t ret = IRQ_NONE;
64 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
65
66 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
67
68 for (i = 0; i < num_ctrls; i++) {
69 status = dw_pcie_readl_dbi(pci, PCIE_MSI_INTR0_STATUS +
70 (i * MSI_REG_CTRL_BLOCK_SIZE));
71 if (!status)
72 continue;
73
74 ret = IRQ_HANDLED;
75 val = status;
76 pos = 0;
77 while ((pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL,
78 pos)) != MAX_MSI_IRQS_PER_CTRL) {
79 generic_handle_domain_irq(pp->irq_domain,
80 (i * MAX_MSI_IRQS_PER_CTRL) +
81 pos);
82 pos++;
83 }
84 }
85
86 return ret;
87 }
88
89 /* Chained MSI interrupt service routine */
dw_chained_msi_isr(struct irq_desc * desc)90 static void dw_chained_msi_isr(struct irq_desc *desc)
91 {
92 struct irq_chip *chip = irq_desc_get_chip(desc);
93 struct dw_pcie_rp *pp;
94
95 chained_irq_enter(chip, desc);
96
97 pp = irq_desc_get_handler_data(desc);
98 dw_handle_msi_irq(pp);
99
100 chained_irq_exit(chip, desc);
101 }
102
dw_pci_setup_msi_msg(struct irq_data * d,struct msi_msg * msg)103 static void dw_pci_setup_msi_msg(struct irq_data *d, struct msi_msg *msg)
104 {
105 struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
106 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
107 u64 msi_target;
108
109 msi_target = (u64)pp->msi_data;
110
111 msg->address_lo = lower_32_bits(msi_target);
112 msg->address_hi = upper_32_bits(msi_target);
113
114 msg->data = d->hwirq;
115
116 dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
117 (int)d->hwirq, msg->address_hi, msg->address_lo);
118 }
119
dw_pci_bottom_mask(struct irq_data * d)120 static void dw_pci_bottom_mask(struct irq_data *d)
121 {
122 struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
123 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
124 unsigned int res, bit, ctrl;
125 unsigned long flags;
126
127 raw_spin_lock_irqsave(&pp->lock, flags);
128
129 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
130 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
131 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
132
133 pp->irq_mask[ctrl] |= BIT(bit);
134 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
135
136 raw_spin_unlock_irqrestore(&pp->lock, flags);
137 }
138
dw_pci_bottom_unmask(struct irq_data * d)139 static void dw_pci_bottom_unmask(struct irq_data *d)
140 {
141 struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
142 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
143 unsigned int res, bit, ctrl;
144 unsigned long flags;
145
146 raw_spin_lock_irqsave(&pp->lock, flags);
147
148 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
149 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
150 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
151
152 pp->irq_mask[ctrl] &= ~BIT(bit);
153 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res, pp->irq_mask[ctrl]);
154
155 raw_spin_unlock_irqrestore(&pp->lock, flags);
156 }
157
dw_pci_bottom_ack(struct irq_data * d)158 static void dw_pci_bottom_ack(struct irq_data *d)
159 {
160 struct dw_pcie_rp *pp = irq_data_get_irq_chip_data(d);
161 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
162 unsigned int res, bit, ctrl;
163
164 ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
165 res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
166 bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
167
168 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_STATUS + res, BIT(bit));
169 }
170
171 static struct irq_chip dw_pci_msi_bottom_irq_chip = {
172 .name = "DWPCI-MSI",
173 .irq_ack = dw_pci_bottom_ack,
174 .irq_compose_msi_msg = dw_pci_setup_msi_msg,
175 .irq_mask = dw_pci_bottom_mask,
176 .irq_unmask = dw_pci_bottom_unmask,
177 };
178
dw_pcie_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)179 static int dw_pcie_irq_domain_alloc(struct irq_domain *domain,
180 unsigned int virq, unsigned int nr_irqs,
181 void *args)
182 {
183 struct dw_pcie_rp *pp = domain->host_data;
184 unsigned long flags;
185 u32 i;
186 int bit;
187
188 raw_spin_lock_irqsave(&pp->lock, flags);
189
190 bit = bitmap_find_free_region(pp->msi_irq_in_use, pp->num_vectors,
191 order_base_2(nr_irqs));
192
193 raw_spin_unlock_irqrestore(&pp->lock, flags);
194
195 if (bit < 0)
196 return -ENOSPC;
197
198 for (i = 0; i < nr_irqs; i++)
199 irq_domain_set_info(domain, virq + i, bit + i,
200 pp->msi_irq_chip,
201 pp, handle_edge_irq,
202 NULL, NULL);
203
204 return 0;
205 }
206
dw_pcie_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)207 static void dw_pcie_irq_domain_free(struct irq_domain *domain,
208 unsigned int virq, unsigned int nr_irqs)
209 {
210 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
211 struct dw_pcie_rp *pp = domain->host_data;
212 unsigned long flags;
213
214 raw_spin_lock_irqsave(&pp->lock, flags);
215
216 bitmap_release_region(pp->msi_irq_in_use, d->hwirq,
217 order_base_2(nr_irqs));
218
219 raw_spin_unlock_irqrestore(&pp->lock, flags);
220 }
221
222 static const struct irq_domain_ops dw_pcie_msi_domain_ops = {
223 .alloc = dw_pcie_irq_domain_alloc,
224 .free = dw_pcie_irq_domain_free,
225 };
226
dw_pcie_allocate_domains(struct dw_pcie_rp * pp)227 int dw_pcie_allocate_domains(struct dw_pcie_rp *pp)
228 {
229 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
230 struct fwnode_handle *fwnode = of_node_to_fwnode(pci->dev->of_node);
231
232 pp->irq_domain = irq_domain_create_linear(fwnode, pp->num_vectors,
233 &dw_pcie_msi_domain_ops, pp);
234 if (!pp->irq_domain) {
235 dev_err(pci->dev, "Failed to create IRQ domain\n");
236 return -ENOMEM;
237 }
238
239 irq_domain_update_bus_token(pp->irq_domain, DOMAIN_BUS_NEXUS);
240
241 pp->msi_domain = pci_msi_create_irq_domain(fwnode,
242 &dw_pcie_msi_domain_info,
243 pp->irq_domain);
244 if (!pp->msi_domain) {
245 dev_err(pci->dev, "Failed to create MSI domain\n");
246 irq_domain_remove(pp->irq_domain);
247 return -ENOMEM;
248 }
249
250 return 0;
251 }
252
dw_pcie_free_msi(struct dw_pcie_rp * pp)253 static void dw_pcie_free_msi(struct dw_pcie_rp *pp)
254 {
255 u32 ctrl;
256
257 for (ctrl = 0; ctrl < MAX_MSI_CTRLS; ctrl++) {
258 if (pp->msi_irq[ctrl] > 0)
259 irq_set_chained_handler_and_data(pp->msi_irq[ctrl],
260 NULL, NULL);
261 }
262
263 irq_domain_remove(pp->msi_domain);
264 irq_domain_remove(pp->irq_domain);
265 }
266
dw_pcie_msi_init(struct dw_pcie_rp * pp)267 static void dw_pcie_msi_init(struct dw_pcie_rp *pp)
268 {
269 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
270 u64 msi_target = (u64)pp->msi_data;
271
272 if (!pci_msi_enabled() || !pp->has_msi_ctrl)
273 return;
274
275 /* Program the msi_data */
276 dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_LO, lower_32_bits(msi_target));
277 dw_pcie_writel_dbi(pci, PCIE_MSI_ADDR_HI, upper_32_bits(msi_target));
278 }
279
dw_pcie_parse_split_msi_irq(struct dw_pcie_rp * pp)280 static int dw_pcie_parse_split_msi_irq(struct dw_pcie_rp *pp)
281 {
282 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
283 struct device *dev = pci->dev;
284 struct platform_device *pdev = to_platform_device(dev);
285 u32 ctrl, max_vectors;
286 int irq;
287
288 /* Parse any "msiX" IRQs described in the devicetree */
289 for (ctrl = 0; ctrl < MAX_MSI_CTRLS; ctrl++) {
290 char msi_name[] = "msiX";
291
292 msi_name[3] = '0' + ctrl;
293 irq = platform_get_irq_byname_optional(pdev, msi_name);
294 if (irq == -ENXIO)
295 break;
296 if (irq < 0)
297 return dev_err_probe(dev, irq,
298 "Failed to parse MSI IRQ '%s'\n",
299 msi_name);
300
301 pp->msi_irq[ctrl] = irq;
302 }
303
304 /* If no "msiX" IRQs, caller should fallback to "msi" IRQ */
305 if (ctrl == 0)
306 return -ENXIO;
307
308 max_vectors = ctrl * MAX_MSI_IRQS_PER_CTRL;
309 if (pp->num_vectors > max_vectors) {
310 dev_warn(dev, "Exceeding number of MSI vectors, limiting to %u\n",
311 max_vectors);
312 pp->num_vectors = max_vectors;
313 }
314 if (!pp->num_vectors)
315 pp->num_vectors = max_vectors;
316
317 return 0;
318 }
319
dw_pcie_msi_host_init(struct dw_pcie_rp * pp)320 static int dw_pcie_msi_host_init(struct dw_pcie_rp *pp)
321 {
322 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
323 struct device *dev = pci->dev;
324 struct platform_device *pdev = to_platform_device(dev);
325 u64 *msi_vaddr = NULL;
326 int ret;
327 u32 ctrl, num_ctrls;
328
329 for (ctrl = 0; ctrl < MAX_MSI_CTRLS; ctrl++)
330 pp->irq_mask[ctrl] = ~0;
331
332 if (!pp->msi_irq[0]) {
333 ret = dw_pcie_parse_split_msi_irq(pp);
334 if (ret < 0 && ret != -ENXIO)
335 return ret;
336 }
337
338 if (!pp->num_vectors)
339 pp->num_vectors = MSI_DEF_NUM_VECTORS;
340 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
341
342 if (!pp->msi_irq[0]) {
343 pp->msi_irq[0] = platform_get_irq_byname_optional(pdev, "msi");
344 if (pp->msi_irq[0] < 0) {
345 pp->msi_irq[0] = platform_get_irq(pdev, 0);
346 if (pp->msi_irq[0] < 0)
347 return pp->msi_irq[0];
348 }
349 }
350
351 dev_dbg(dev, "Using %d MSI vectors\n", pp->num_vectors);
352
353 pp->msi_irq_chip = &dw_pci_msi_bottom_irq_chip;
354
355 ret = dw_pcie_allocate_domains(pp);
356 if (ret)
357 return ret;
358
359 for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
360 if (pp->msi_irq[ctrl] > 0)
361 irq_set_chained_handler_and_data(pp->msi_irq[ctrl],
362 dw_chained_msi_isr, pp);
363 }
364
365 /*
366 * Even though the iMSI-RX Module supports 64-bit addresses some
367 * peripheral PCIe devices may lack 64-bit message support. In
368 * order not to miss MSI TLPs from those devices the MSI target
369 * address has to be within the lowest 4GB.
370 *
371 * Note until there is a better alternative found the reservation is
372 * done by allocating from the artificially limited DMA-coherent
373 * memory.
374 */
375 ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
376 if (!ret)
377 msi_vaddr = dmam_alloc_coherent(dev, sizeof(u64), &pp->msi_data,
378 GFP_KERNEL);
379
380 if (!msi_vaddr) {
381 dev_warn(dev, "Failed to allocate 32-bit MSI address\n");
382 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
383 msi_vaddr = dmam_alloc_coherent(dev, sizeof(u64), &pp->msi_data,
384 GFP_KERNEL);
385 if (!msi_vaddr) {
386 dev_err(dev, "Failed to allocate MSI address\n");
387 dw_pcie_free_msi(pp);
388 return -ENOMEM;
389 }
390 }
391
392 return 0;
393 }
394
dw_pcie_host_request_msg_tlp_res(struct dw_pcie_rp * pp)395 static void dw_pcie_host_request_msg_tlp_res(struct dw_pcie_rp *pp)
396 {
397 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
398 struct resource_entry *win;
399 struct resource *res;
400
401 win = resource_list_first_type(&pp->bridge->windows, IORESOURCE_MEM);
402 if (win) {
403 res = devm_kzalloc(pci->dev, sizeof(*res), GFP_KERNEL);
404 if (!res)
405 return;
406
407 /*
408 * Allocate MSG TLP region of size 'region_align' at the end of
409 * the host bridge window.
410 */
411 res->start = win->res->end - pci->region_align + 1;
412 res->end = win->res->end;
413 res->name = "msg";
414 res->flags = win->res->flags | IORESOURCE_BUSY;
415
416 if (!devm_request_resource(pci->dev, win->res, res))
417 pp->msg_res = res;
418 }
419 }
420
dw_pcie_host_get_resources(struct dw_pcie_rp * pp)421 static int dw_pcie_host_get_resources(struct dw_pcie_rp *pp)
422 {
423 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
424 struct device *dev = pci->dev;
425 struct platform_device *pdev = to_platform_device(dev);
426 struct resource_entry *win;
427 struct resource *res;
428 int ret;
429
430 ret = dw_pcie_get_resources(pci);
431 if (ret)
432 return ret;
433
434 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config");
435 if (!res) {
436 dev_err(dev, "Missing \"config\" reg space\n");
437 return -ENODEV;
438 }
439
440 pp->cfg0_size = resource_size(res);
441 pp->cfg0_base = res->start;
442
443 pp->va_cfg0_base = devm_pci_remap_cfg_resource(dev, res);
444 if (IS_ERR(pp->va_cfg0_base))
445 return PTR_ERR(pp->va_cfg0_base);
446
447 /* Get the I/O range from DT */
448 win = resource_list_first_type(&pp->bridge->windows, IORESOURCE_IO);
449 if (win) {
450 pp->io_size = resource_size(win->res);
451 pp->io_bus_addr = win->res->start - win->offset;
452 pp->io_base = pci_pio_to_address(win->res->start);
453 }
454
455 /*
456 * visconti_pcie_cpu_addr_fixup() uses pp->io_base, so we have to
457 * call dw_pcie_parent_bus_offset() after setting pp->io_base.
458 */
459 pci->parent_bus_offset = dw_pcie_parent_bus_offset(pci, "config",
460 pp->cfg0_base);
461 return 0;
462 }
463
dw_pcie_host_init(struct dw_pcie_rp * pp)464 int dw_pcie_host_init(struct dw_pcie_rp *pp)
465 {
466 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
467 struct device *dev = pci->dev;
468 struct device_node *np = dev->of_node;
469 struct pci_host_bridge *bridge;
470 int ret;
471
472 raw_spin_lock_init(&pp->lock);
473
474 bridge = devm_pci_alloc_host_bridge(dev, 0);
475 if (!bridge)
476 return -ENOMEM;
477
478 pp->bridge = bridge;
479
480 ret = dw_pcie_host_get_resources(pp);
481 if (ret)
482 return ret;
483
484 /* Set default bus ops */
485 bridge->ops = &dw_pcie_ops;
486 bridge->child_ops = &dw_child_pcie_ops;
487
488 if (pp->ops->init) {
489 ret = pp->ops->init(pp);
490 if (ret)
491 return ret;
492 }
493
494 if (pci_msi_enabled()) {
495 pp->has_msi_ctrl = !(pp->ops->msi_init ||
496 of_property_present(np, "msi-parent") ||
497 of_property_present(np, "msi-map"));
498
499 /*
500 * For the has_msi_ctrl case the default assignment is handled
501 * in the dw_pcie_msi_host_init().
502 */
503 if (!pp->has_msi_ctrl && !pp->num_vectors) {
504 pp->num_vectors = MSI_DEF_NUM_VECTORS;
505 } else if (pp->num_vectors > MAX_MSI_IRQS) {
506 dev_err(dev, "Invalid number of vectors\n");
507 ret = -EINVAL;
508 goto err_deinit_host;
509 }
510
511 if (pp->ops->msi_init) {
512 ret = pp->ops->msi_init(pp);
513 if (ret < 0)
514 goto err_deinit_host;
515 } else if (pp->has_msi_ctrl) {
516 ret = dw_pcie_msi_host_init(pp);
517 if (ret < 0)
518 goto err_deinit_host;
519 }
520 }
521
522 dw_pcie_version_detect(pci);
523
524 dw_pcie_iatu_detect(pci);
525
526 /*
527 * Allocate the resource for MSG TLP before programming the iATU
528 * outbound window in dw_pcie_setup_rc(). Since the allocation depends
529 * on the value of 'region_align', this has to be done after
530 * dw_pcie_iatu_detect().
531 *
532 * Glue drivers need to set 'use_atu_msg' before dw_pcie_host_init() to
533 * make use of the generic MSG TLP implementation.
534 */
535 if (pp->use_atu_msg)
536 dw_pcie_host_request_msg_tlp_res(pp);
537
538 ret = dw_pcie_edma_detect(pci);
539 if (ret)
540 goto err_free_msi;
541
542 ret = dw_pcie_setup_rc(pp);
543 if (ret)
544 goto err_remove_edma;
545
546 if (!dw_pcie_link_up(pci)) {
547 ret = dw_pcie_start_link(pci);
548 if (ret)
549 goto err_remove_edma;
550 }
551
552 /*
553 * Note: Skip the link up delay only when a Link Up IRQ is present.
554 * If there is no Link Up IRQ, we should not bypass the delay
555 * because that would require users to manually rescan for devices.
556 */
557 if (!pp->use_linkup_irq)
558 /* Ignore errors, the link may come up later */
559 dw_pcie_wait_for_link(pci);
560
561 bridge->sysdata = pp;
562
563 ret = pci_host_probe(bridge);
564 if (ret)
565 goto err_stop_link;
566
567 if (pp->ops->post_init)
568 pp->ops->post_init(pp);
569
570 dwc_pcie_debugfs_init(pci);
571
572 return 0;
573
574 err_stop_link:
575 dw_pcie_stop_link(pci);
576
577 err_remove_edma:
578 dw_pcie_edma_remove(pci);
579
580 err_free_msi:
581 if (pp->has_msi_ctrl)
582 dw_pcie_free_msi(pp);
583
584 err_deinit_host:
585 if (pp->ops->deinit)
586 pp->ops->deinit(pp);
587
588 return ret;
589 }
590 EXPORT_SYMBOL_GPL(dw_pcie_host_init);
591
dw_pcie_host_deinit(struct dw_pcie_rp * pp)592 void dw_pcie_host_deinit(struct dw_pcie_rp *pp)
593 {
594 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
595
596 dwc_pcie_debugfs_deinit(pci);
597
598 pci_stop_root_bus(pp->bridge->bus);
599 pci_remove_root_bus(pp->bridge->bus);
600
601 dw_pcie_stop_link(pci);
602
603 dw_pcie_edma_remove(pci);
604
605 if (pp->has_msi_ctrl)
606 dw_pcie_free_msi(pp);
607
608 if (pp->ops->deinit)
609 pp->ops->deinit(pp);
610 }
611 EXPORT_SYMBOL_GPL(dw_pcie_host_deinit);
612
dw_pcie_other_conf_map_bus(struct pci_bus * bus,unsigned int devfn,int where)613 static void __iomem *dw_pcie_other_conf_map_bus(struct pci_bus *bus,
614 unsigned int devfn, int where)
615 {
616 struct dw_pcie_rp *pp = bus->sysdata;
617 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
618 struct dw_pcie_ob_atu_cfg atu = { 0 };
619 int type, ret;
620 u32 busdev;
621
622 /*
623 * Checking whether the link is up here is a last line of defense
624 * against platforms that forward errors on the system bus as
625 * SError upon PCI configuration transactions issued when the link
626 * is down. This check is racy by definition and does not stop
627 * the system from triggering an SError if the link goes down
628 * after this check is performed.
629 */
630 if (!dw_pcie_link_up(pci))
631 return NULL;
632
633 busdev = PCIE_ATU_BUS(bus->number) | PCIE_ATU_DEV(PCI_SLOT(devfn)) |
634 PCIE_ATU_FUNC(PCI_FUNC(devfn));
635
636 if (pci_is_root_bus(bus->parent))
637 type = PCIE_ATU_TYPE_CFG0;
638 else
639 type = PCIE_ATU_TYPE_CFG1;
640
641 atu.type = type;
642 atu.parent_bus_addr = pp->cfg0_base - pci->parent_bus_offset;
643 atu.pci_addr = busdev;
644 atu.size = pp->cfg0_size;
645
646 ret = dw_pcie_prog_outbound_atu(pci, &atu);
647 if (ret)
648 return NULL;
649
650 return pp->va_cfg0_base + where;
651 }
652
dw_pcie_rd_other_conf(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 * val)653 static int dw_pcie_rd_other_conf(struct pci_bus *bus, unsigned int devfn,
654 int where, int size, u32 *val)
655 {
656 struct dw_pcie_rp *pp = bus->sysdata;
657 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
658 struct dw_pcie_ob_atu_cfg atu = { 0 };
659 int ret;
660
661 ret = pci_generic_config_read(bus, devfn, where, size, val);
662 if (ret != PCIBIOS_SUCCESSFUL)
663 return ret;
664
665 if (pp->cfg0_io_shared) {
666 atu.type = PCIE_ATU_TYPE_IO;
667 atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
668 atu.pci_addr = pp->io_bus_addr;
669 atu.size = pp->io_size;
670
671 ret = dw_pcie_prog_outbound_atu(pci, &atu);
672 if (ret)
673 return PCIBIOS_SET_FAILED;
674 }
675
676 return PCIBIOS_SUCCESSFUL;
677 }
678
dw_pcie_wr_other_conf(struct pci_bus * bus,unsigned int devfn,int where,int size,u32 val)679 static int dw_pcie_wr_other_conf(struct pci_bus *bus, unsigned int devfn,
680 int where, int size, u32 val)
681 {
682 struct dw_pcie_rp *pp = bus->sysdata;
683 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
684 struct dw_pcie_ob_atu_cfg atu = { 0 };
685 int ret;
686
687 ret = pci_generic_config_write(bus, devfn, where, size, val);
688 if (ret != PCIBIOS_SUCCESSFUL)
689 return ret;
690
691 if (pp->cfg0_io_shared) {
692 atu.type = PCIE_ATU_TYPE_IO;
693 atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
694 atu.pci_addr = pp->io_bus_addr;
695 atu.size = pp->io_size;
696
697 ret = dw_pcie_prog_outbound_atu(pci, &atu);
698 if (ret)
699 return PCIBIOS_SET_FAILED;
700 }
701
702 return PCIBIOS_SUCCESSFUL;
703 }
704
705 static struct pci_ops dw_child_pcie_ops = {
706 .map_bus = dw_pcie_other_conf_map_bus,
707 .read = dw_pcie_rd_other_conf,
708 .write = dw_pcie_wr_other_conf,
709 };
710
dw_pcie_own_conf_map_bus(struct pci_bus * bus,unsigned int devfn,int where)711 void __iomem *dw_pcie_own_conf_map_bus(struct pci_bus *bus, unsigned int devfn, int where)
712 {
713 struct dw_pcie_rp *pp = bus->sysdata;
714 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
715
716 if (PCI_SLOT(devfn) > 0)
717 return NULL;
718
719 return pci->dbi_base + where;
720 }
721 EXPORT_SYMBOL_GPL(dw_pcie_own_conf_map_bus);
722
723 static struct pci_ops dw_pcie_ops = {
724 .map_bus = dw_pcie_own_conf_map_bus,
725 .read = pci_generic_config_read,
726 .write = pci_generic_config_write,
727 };
728
dw_pcie_iatu_setup(struct dw_pcie_rp * pp)729 static int dw_pcie_iatu_setup(struct dw_pcie_rp *pp)
730 {
731 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
732 struct dw_pcie_ob_atu_cfg atu = { 0 };
733 struct resource_entry *entry;
734 int i, ret;
735
736 /* Note the very first outbound ATU is used for CFG IOs */
737 if (!pci->num_ob_windows) {
738 dev_err(pci->dev, "No outbound iATU found\n");
739 return -EINVAL;
740 }
741
742 /*
743 * Ensure all out/inbound windows are disabled before proceeding with
744 * the MEM/IO (dma-)ranges setups.
745 */
746 for (i = 0; i < pci->num_ob_windows; i++)
747 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, i);
748
749 for (i = 0; i < pci->num_ib_windows; i++)
750 dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, i);
751
752 i = 0;
753 resource_list_for_each_entry(entry, &pp->bridge->windows) {
754 if (resource_type(entry->res) != IORESOURCE_MEM)
755 continue;
756
757 if (pci->num_ob_windows <= ++i)
758 break;
759
760 atu.index = i;
761 atu.type = PCIE_ATU_TYPE_MEM;
762 atu.parent_bus_addr = entry->res->start - pci->parent_bus_offset;
763 atu.pci_addr = entry->res->start - entry->offset;
764
765 /* Adjust iATU size if MSG TLP region was allocated before */
766 if (pp->msg_res && pp->msg_res->parent == entry->res)
767 atu.size = resource_size(entry->res) -
768 resource_size(pp->msg_res);
769 else
770 atu.size = resource_size(entry->res);
771
772 ret = dw_pcie_prog_outbound_atu(pci, &atu);
773 if (ret) {
774 dev_err(pci->dev, "Failed to set MEM range %pr\n",
775 entry->res);
776 return ret;
777 }
778 }
779
780 if (pp->io_size) {
781 if (pci->num_ob_windows > ++i) {
782 atu.index = i;
783 atu.type = PCIE_ATU_TYPE_IO;
784 atu.parent_bus_addr = pp->io_base - pci->parent_bus_offset;
785 atu.pci_addr = pp->io_bus_addr;
786 atu.size = pp->io_size;
787
788 ret = dw_pcie_prog_outbound_atu(pci, &atu);
789 if (ret) {
790 dev_err(pci->dev, "Failed to set IO range %pr\n",
791 entry->res);
792 return ret;
793 }
794 } else {
795 pp->cfg0_io_shared = true;
796 }
797 }
798
799 if (pci->num_ob_windows <= i)
800 dev_warn(pci->dev, "Ranges exceed outbound iATU size (%d)\n",
801 pci->num_ob_windows);
802
803 pp->msg_atu_index = i;
804
805 i = 0;
806 resource_list_for_each_entry(entry, &pp->bridge->dma_ranges) {
807 if (resource_type(entry->res) != IORESOURCE_MEM)
808 continue;
809
810 if (pci->num_ib_windows <= i)
811 break;
812
813 ret = dw_pcie_prog_inbound_atu(pci, i++, PCIE_ATU_TYPE_MEM,
814 entry->res->start,
815 entry->res->start - entry->offset,
816 resource_size(entry->res));
817 if (ret) {
818 dev_err(pci->dev, "Failed to set DMA range %pr\n",
819 entry->res);
820 return ret;
821 }
822 }
823
824 if (pci->num_ib_windows <= i)
825 dev_warn(pci->dev, "Dma-ranges exceed inbound iATU size (%u)\n",
826 pci->num_ib_windows);
827
828 return 0;
829 }
830
dw_pcie_setup_rc(struct dw_pcie_rp * pp)831 int dw_pcie_setup_rc(struct dw_pcie_rp *pp)
832 {
833 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
834 u32 val, ctrl, num_ctrls;
835 int ret;
836
837 /*
838 * Enable DBI read-only registers for writing/updating configuration.
839 * Write permission gets disabled towards the end of this function.
840 */
841 dw_pcie_dbi_ro_wr_en(pci);
842
843 dw_pcie_setup(pci);
844
845 if (pp->has_msi_ctrl) {
846 num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
847
848 /* Initialize IRQ Status array */
849 for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
850 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK +
851 (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
852 pp->irq_mask[ctrl]);
853 dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_ENABLE +
854 (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
855 ~0);
856 }
857 }
858
859 dw_pcie_msi_init(pp);
860
861 /* Setup RC BARs */
862 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0x00000004);
863 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0x00000000);
864
865 /* Setup interrupt pins */
866 val = dw_pcie_readl_dbi(pci, PCI_INTERRUPT_LINE);
867 val &= 0xffff00ff;
868 val |= 0x00000100;
869 dw_pcie_writel_dbi(pci, PCI_INTERRUPT_LINE, val);
870
871 /* Setup bus numbers */
872 val = dw_pcie_readl_dbi(pci, PCI_PRIMARY_BUS);
873 val &= 0xff000000;
874 val |= 0x00ff0100;
875 dw_pcie_writel_dbi(pci, PCI_PRIMARY_BUS, val);
876
877 /* Setup command register */
878 val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
879 val &= 0xffff0000;
880 val |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
881 PCI_COMMAND_MASTER | PCI_COMMAND_SERR;
882 dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
883
884 /*
885 * If the platform provides its own child bus config accesses, it means
886 * the platform uses its own address translation component rather than
887 * ATU, so we should not program the ATU here.
888 */
889 if (pp->bridge->child_ops == &dw_child_pcie_ops) {
890 ret = dw_pcie_iatu_setup(pp);
891 if (ret)
892 return ret;
893 }
894
895 dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0);
896
897 /* Program correct class for RC */
898 dw_pcie_writew_dbi(pci, PCI_CLASS_DEVICE, PCI_CLASS_BRIDGE_PCI);
899
900 val = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
901 val |= PORT_LOGIC_SPEED_CHANGE;
902 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, val);
903
904 dw_pcie_dbi_ro_wr_dis(pci);
905
906 return 0;
907 }
908 EXPORT_SYMBOL_GPL(dw_pcie_setup_rc);
909
dw_pcie_pme_turn_off(struct dw_pcie * pci)910 static int dw_pcie_pme_turn_off(struct dw_pcie *pci)
911 {
912 struct dw_pcie_ob_atu_cfg atu = { 0 };
913 void __iomem *mem;
914 int ret;
915
916 if (pci->num_ob_windows <= pci->pp.msg_atu_index)
917 return -ENOSPC;
918
919 if (!pci->pp.msg_res)
920 return -ENOSPC;
921
922 atu.code = PCIE_MSG_CODE_PME_TURN_OFF;
923 atu.routing = PCIE_MSG_TYPE_R_BC;
924 atu.type = PCIE_ATU_TYPE_MSG;
925 atu.size = resource_size(pci->pp.msg_res);
926 atu.index = pci->pp.msg_atu_index;
927
928 atu.parent_bus_addr = pci->pp.msg_res->start - pci->parent_bus_offset;
929
930 ret = dw_pcie_prog_outbound_atu(pci, &atu);
931 if (ret)
932 return ret;
933
934 mem = ioremap(pci->pp.msg_res->start, pci->region_align);
935 if (!mem)
936 return -ENOMEM;
937
938 /* A dummy write is converted to a Msg TLP */
939 writel(0, mem);
940
941 iounmap(mem);
942
943 return 0;
944 }
945
dw_pcie_suspend_noirq(struct dw_pcie * pci)946 int dw_pcie_suspend_noirq(struct dw_pcie *pci)
947 {
948 u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
949 u32 val;
950 int ret;
951
952 /*
953 * If L1SS is supported, then do not put the link into L2 as some
954 * devices such as NVMe expect low resume latency.
955 */
956 if (dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKCTL) & PCI_EXP_LNKCTL_ASPM_L1)
957 return 0;
958
959 if (pci->pp.ops->pme_turn_off) {
960 pci->pp.ops->pme_turn_off(&pci->pp);
961 } else {
962 ret = dw_pcie_pme_turn_off(pci);
963 if (ret)
964 return ret;
965 }
966
967 ret = read_poll_timeout(dw_pcie_get_ltssm, val,
968 val == DW_PCIE_LTSSM_L2_IDLE ||
969 val <= DW_PCIE_LTSSM_DETECT_WAIT,
970 PCIE_PME_TO_L2_TIMEOUT_US/10,
971 PCIE_PME_TO_L2_TIMEOUT_US, false, pci);
972 if (ret) {
973 /* Only log message when LTSSM isn't in DETECT or POLL */
974 dev_err(pci->dev, "Timeout waiting for L2 entry! LTSSM: 0x%x\n", val);
975 return ret;
976 }
977
978 /*
979 * Per PCIe r6.0, sec 5.3.3.2.1, software should wait at least
980 * 100ns after L2/L3 Ready before turning off refclock and
981 * main power. This is harmless when no endpoint is connected.
982 */
983 udelay(1);
984
985 dw_pcie_stop_link(pci);
986 if (pci->pp.ops->deinit)
987 pci->pp.ops->deinit(&pci->pp);
988
989 pci->suspended = true;
990
991 return ret;
992 }
993 EXPORT_SYMBOL_GPL(dw_pcie_suspend_noirq);
994
dw_pcie_resume_noirq(struct dw_pcie * pci)995 int dw_pcie_resume_noirq(struct dw_pcie *pci)
996 {
997 int ret;
998
999 if (!pci->suspended)
1000 return 0;
1001
1002 pci->suspended = false;
1003
1004 if (pci->pp.ops->init) {
1005 ret = pci->pp.ops->init(&pci->pp);
1006 if (ret) {
1007 dev_err(pci->dev, "Host init failed: %d\n", ret);
1008 return ret;
1009 }
1010 }
1011
1012 dw_pcie_setup_rc(&pci->pp);
1013
1014 ret = dw_pcie_start_link(pci);
1015 if (ret)
1016 return ret;
1017
1018 ret = dw_pcie_wait_for_link(pci);
1019 if (ret)
1020 return ret;
1021
1022 return ret;
1023 }
1024 EXPORT_SYMBOL_GPL(dw_pcie_resume_noirq);
1025