1
2
3
4
5
6
7
8
9
10
11
12
13#include <linux/interrupt.h>
14#include <linux/irq.h>
15#include <linux/irqdomain.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/msi.h>
19#include <linux/of_address.h>
20#include <linux/of_pci.h>
21#include <linux/of_platform.h>
22#include <linux/of_irq.h>
23#include <linux/pci.h>
24#include <linux/pci-ecam.h>
25#include <linux/platform_device.h>
26
27#include "../pci.h"
28
29
30#define XILINX_PCIE_REG_BIR 0x00000130
31#define XILINX_PCIE_REG_IDR 0x00000138
32#define XILINX_PCIE_REG_IMR 0x0000013c
33#define XILINX_PCIE_REG_PSCR 0x00000144
34#define XILINX_PCIE_REG_RPSC 0x00000148
35#define XILINX_PCIE_REG_MSIBASE1 0x0000014c
36#define XILINX_PCIE_REG_MSIBASE2 0x00000150
37#define XILINX_PCIE_REG_RPEFR 0x00000154
38#define XILINX_PCIE_REG_RPIFR1 0x00000158
39#define XILINX_PCIE_REG_RPIFR2 0x0000015c
40
41
42#define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
43#define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
44#define XILINX_PCIE_INTR_STR_ERR BIT(2)
45#define XILINX_PCIE_INTR_HOT_RESET BIT(3)
46#define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
47#define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
48#define XILINX_PCIE_INTR_NONFATAL BIT(10)
49#define XILINX_PCIE_INTR_FATAL BIT(11)
50#define XILINX_PCIE_INTR_INTX BIT(16)
51#define XILINX_PCIE_INTR_MSI BIT(17)
52#define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
53#define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
54#define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
55#define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
56#define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
57#define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
58#define XILINX_PCIE_INTR_MST_DECERR BIT(26)
59#define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
60#define XILINX_PCIE_INTR_MST_ERRP BIT(28)
61#define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
62#define XILINX_PCIE_IMR_ENABLE_MASK 0x1FF30F0D
63#define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
64
65
66#define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
67#define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
68#define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
69
70
71#define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
72#define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
73#define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
74#define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
75#define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
76
77
78#define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
79#define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
80
81
82#define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
83
84
85#define XILINX_PCIE_REG_RPSC_BEN BIT(0)
86
87
88#define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
89
90
91#define XILINX_NUM_MSI_IRQS 128
92
93
94
95
96
97
98
99
100
101
102
103struct xilinx_pcie_port {
104 void __iomem *reg_base;
105 u32 irq;
106 unsigned long msi_pages;
107 struct device *dev;
108 struct irq_domain *msi_domain;
109 struct irq_domain *leg_domain;
110 struct list_head resources;
111};
112
113static DECLARE_BITMAP(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
114
115static inline u32 pcie_read(struct xilinx_pcie_port *port, u32 reg)
116{
117 return readl(port->reg_base + reg);
118}
119
120static inline void pcie_write(struct xilinx_pcie_port *port, u32 val, u32 reg)
121{
122 writel(val, port->reg_base + reg);
123}
124
125static inline bool xilinx_pcie_link_up(struct xilinx_pcie_port *port)
126{
127 return (pcie_read(port, XILINX_PCIE_REG_PSCR) &
128 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
129}
130
131
132
133
134
135static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie_port *port)
136{
137 struct device *dev = port->dev;
138 unsigned long val = pcie_read(port, XILINX_PCIE_REG_RPEFR);
139
140 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
141 dev_dbg(dev, "Requester ID %lu\n",
142 val & XILINX_PCIE_RPEFR_REQ_ID);
143 pcie_write(port, XILINX_PCIE_RPEFR_ALL_MASK,
144 XILINX_PCIE_REG_RPEFR);
145 }
146}
147
148
149
150
151
152
153
154
155static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
156{
157 struct xilinx_pcie_port *port = bus->sysdata;
158
159
160 if (!pci_is_root_bus(bus)) {
161 if (!xilinx_pcie_link_up(port))
162 return false;
163 } else if (devfn > 0) {
164
165 return false;
166 }
167 return true;
168}
169
170
171
172
173
174
175
176
177
178
179static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
180 unsigned int devfn, int where)
181{
182 struct xilinx_pcie_port *port = bus->sysdata;
183
184 if (!xilinx_pcie_valid_device(bus, devfn))
185 return NULL;
186
187 return port->reg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
188}
189
190
191static struct pci_ops xilinx_pcie_ops = {
192 .map_bus = xilinx_pcie_map_bus,
193 .read = pci_generic_config_read,
194 .write = pci_generic_config_write,
195};
196
197
198
199
200
201
202
203static void xilinx_pcie_destroy_msi(unsigned int irq)
204{
205 struct msi_desc *msi;
206 struct xilinx_pcie_port *port;
207 struct irq_data *d = irq_get_irq_data(irq);
208 irq_hw_number_t hwirq = irqd_to_hwirq(d);
209
210 if (!test_bit(hwirq, msi_irq_in_use)) {
211 msi = irq_get_msi_desc(irq);
212 port = msi_desc_to_pci_sysdata(msi);
213 dev_err(port->dev, "Trying to free unused MSI#%d\n", irq);
214 } else {
215 clear_bit(hwirq, msi_irq_in_use);
216 }
217}
218
219
220
221
222
223
224static int xilinx_pcie_assign_msi(void)
225{
226 int pos;
227
228 pos = find_first_zero_bit(msi_irq_in_use, XILINX_NUM_MSI_IRQS);
229 if (pos < XILINX_NUM_MSI_IRQS)
230 set_bit(pos, msi_irq_in_use);
231 else
232 return -ENOSPC;
233
234 return pos;
235}
236
237
238
239
240
241
242static void xilinx_msi_teardown_irq(struct msi_controller *chip,
243 unsigned int irq)
244{
245 xilinx_pcie_destroy_msi(irq);
246 irq_dispose_mapping(irq);
247}
248
249
250
251
252
253
254
255
256
257static int xilinx_pcie_msi_setup_irq(struct msi_controller *chip,
258 struct pci_dev *pdev,
259 struct msi_desc *desc)
260{
261 struct xilinx_pcie_port *port = pdev->bus->sysdata;
262 unsigned int irq;
263 int hwirq;
264 struct msi_msg msg;
265 phys_addr_t msg_addr;
266
267 hwirq = xilinx_pcie_assign_msi();
268 if (hwirq < 0)
269 return hwirq;
270
271 irq = irq_create_mapping(port->msi_domain, hwirq);
272 if (!irq)
273 return -EINVAL;
274
275 irq_set_msi_desc(irq, desc);
276
277 msg_addr = virt_to_phys((void *)port->msi_pages);
278
279 msg.address_hi = 0;
280 msg.address_lo = msg_addr;
281 msg.data = irq;
282
283 pci_write_msi_msg(irq, &msg);
284
285 return 0;
286}
287
288
289static struct msi_controller xilinx_pcie_msi_chip = {
290 .setup_irq = xilinx_pcie_msi_setup_irq,
291 .teardown_irq = xilinx_msi_teardown_irq,
292};
293
294
295static struct irq_chip xilinx_msi_irq_chip = {
296 .name = "Xilinx PCIe MSI",
297 .irq_enable = pci_msi_unmask_irq,
298 .irq_disable = pci_msi_mask_irq,
299 .irq_mask = pci_msi_mask_irq,
300 .irq_unmask = pci_msi_unmask_irq,
301};
302
303
304
305
306
307
308
309
310
311static int xilinx_pcie_msi_map(struct irq_domain *domain, unsigned int irq,
312 irq_hw_number_t hwirq)
313{
314 irq_set_chip_and_handler(irq, &xilinx_msi_irq_chip, handle_simple_irq);
315 irq_set_chip_data(irq, domain->host_data);
316
317 return 0;
318}
319
320
321static const struct irq_domain_ops msi_domain_ops = {
322 .map = xilinx_pcie_msi_map,
323};
324
325
326
327
328
329static int xilinx_pcie_enable_msi(struct xilinx_pcie_port *port)
330{
331 phys_addr_t msg_addr;
332
333 port->msi_pages = __get_free_pages(GFP_KERNEL, 0);
334 if (!port->msi_pages)
335 return -ENOMEM;
336
337 msg_addr = virt_to_phys((void *)port->msi_pages);
338 pcie_write(port, 0x0, XILINX_PCIE_REG_MSIBASE1);
339 pcie_write(port, msg_addr, XILINX_PCIE_REG_MSIBASE2);
340
341 return 0;
342}
343
344
345
346
347
348
349
350
351
352
353
354static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
355 irq_hw_number_t hwirq)
356{
357 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
358 irq_set_chip_data(irq, domain->host_data);
359
360 return 0;
361}
362
363
364static const struct irq_domain_ops intx_domain_ops = {
365 .map = xilinx_pcie_intx_map,
366 .xlate = pci_irqd_intx_xlate,
367};
368
369
370
371
372
373
374
375
376
377
378static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
379{
380 struct xilinx_pcie_port *port = (struct xilinx_pcie_port *)data;
381 struct device *dev = port->dev;
382 u32 val, mask, status;
383
384
385 val = pcie_read(port, XILINX_PCIE_REG_IDR);
386 mask = pcie_read(port, XILINX_PCIE_REG_IMR);
387
388 status = val & mask;
389 if (!status)
390 return IRQ_NONE;
391
392 if (status & XILINX_PCIE_INTR_LINK_DOWN)
393 dev_warn(dev, "Link Down\n");
394
395 if (status & XILINX_PCIE_INTR_ECRC_ERR)
396 dev_warn(dev, "ECRC failed\n");
397
398 if (status & XILINX_PCIE_INTR_STR_ERR)
399 dev_warn(dev, "Streaming error\n");
400
401 if (status & XILINX_PCIE_INTR_HOT_RESET)
402 dev_info(dev, "Hot reset\n");
403
404 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
405 dev_warn(dev, "ECAM access timeout\n");
406
407 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
408 dev_warn(dev, "Correctable error message\n");
409 xilinx_pcie_clear_err_interrupts(port);
410 }
411
412 if (status & XILINX_PCIE_INTR_NONFATAL) {
413 dev_warn(dev, "Non fatal error message\n");
414 xilinx_pcie_clear_err_interrupts(port);
415 }
416
417 if (status & XILINX_PCIE_INTR_FATAL) {
418 dev_warn(dev, "Fatal error message\n");
419 xilinx_pcie_clear_err_interrupts(port);
420 }
421
422 if (status & (XILINX_PCIE_INTR_INTX | XILINX_PCIE_INTR_MSI)) {
423 val = pcie_read(port, XILINX_PCIE_REG_RPIFR1);
424
425
426 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
427 dev_warn(dev, "RP Intr FIFO1 read error\n");
428 goto error;
429 }
430
431
432 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
433 val = pcie_read(port, XILINX_PCIE_REG_RPIFR2) &
434 XILINX_PCIE_RPIFR2_MSG_DATA;
435 } else {
436 val = (val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
437 XILINX_PCIE_RPIFR1_INTR_SHIFT;
438 val = irq_find_mapping(port->leg_domain, val);
439 }
440
441
442 pcie_write(port, XILINX_PCIE_RPIFR1_ALL_MASK,
443 XILINX_PCIE_REG_RPIFR1);
444
445
446 if (IS_ENABLED(CONFIG_PCI_MSI) ||
447 !(val & XILINX_PCIE_RPIFR1_MSI_INTR))
448 generic_handle_irq(val);
449 }
450
451 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
452 dev_warn(dev, "Slave unsupported request\n");
453
454 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
455 dev_warn(dev, "Slave unexpected completion\n");
456
457 if (status & XILINX_PCIE_INTR_SLV_COMPL)
458 dev_warn(dev, "Slave completion timeout\n");
459
460 if (status & XILINX_PCIE_INTR_SLV_ERRP)
461 dev_warn(dev, "Slave Error Poison\n");
462
463 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
464 dev_warn(dev, "Slave Completer Abort\n");
465
466 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
467 dev_warn(dev, "Slave Illegal Burst\n");
468
469 if (status & XILINX_PCIE_INTR_MST_DECERR)
470 dev_warn(dev, "Master decode error\n");
471
472 if (status & XILINX_PCIE_INTR_MST_SLVERR)
473 dev_warn(dev, "Master slave error\n");
474
475 if (status & XILINX_PCIE_INTR_MST_ERRP)
476 dev_warn(dev, "Master error poison\n");
477
478error:
479
480 pcie_write(port, status, XILINX_PCIE_REG_IDR);
481
482 return IRQ_HANDLED;
483}
484
485
486
487
488
489
490
491static int xilinx_pcie_init_irq_domain(struct xilinx_pcie_port *port)
492{
493 struct device *dev = port->dev;
494 struct device_node *node = dev->of_node;
495 struct device_node *pcie_intc_node;
496 int ret;
497
498
499 pcie_intc_node = of_get_next_child(node, NULL);
500 if (!pcie_intc_node) {
501 dev_err(dev, "No PCIe Intc node found\n");
502 return -ENODEV;
503 }
504
505 port->leg_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
506 &intx_domain_ops,
507 port);
508 of_node_put(pcie_intc_node);
509 if (!port->leg_domain) {
510 dev_err(dev, "Failed to get a INTx IRQ domain\n");
511 return -ENODEV;
512 }
513
514
515 if (IS_ENABLED(CONFIG_PCI_MSI)) {
516 port->msi_domain = irq_domain_add_linear(node,
517 XILINX_NUM_MSI_IRQS,
518 &msi_domain_ops,
519 &xilinx_pcie_msi_chip);
520 if (!port->msi_domain) {
521 dev_err(dev, "Failed to get a MSI IRQ domain\n");
522 return -ENODEV;
523 }
524
525 ret = xilinx_pcie_enable_msi(port);
526 if (ret)
527 return ret;
528 }
529
530 return 0;
531}
532
533
534
535
536
537static void xilinx_pcie_init_port(struct xilinx_pcie_port *port)
538{
539 struct device *dev = port->dev;
540
541 if (xilinx_pcie_link_up(port))
542 dev_info(dev, "PCIe Link is UP\n");
543 else
544 dev_info(dev, "PCIe Link is DOWN\n");
545
546
547 pcie_write(port, ~XILINX_PCIE_IDR_ALL_MASK,
548 XILINX_PCIE_REG_IMR);
549
550
551 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_IDR) &
552 XILINX_PCIE_IMR_ALL_MASK,
553 XILINX_PCIE_REG_IDR);
554
555
556 pcie_write(port, XILINX_PCIE_IMR_ENABLE_MASK, XILINX_PCIE_REG_IMR);
557
558
559 pcie_write(port, pcie_read(port, XILINX_PCIE_REG_RPSC) |
560 XILINX_PCIE_REG_RPSC_BEN,
561 XILINX_PCIE_REG_RPSC);
562}
563
564
565
566
567
568
569
570static int xilinx_pcie_parse_dt(struct xilinx_pcie_port *port)
571{
572 struct device *dev = port->dev;
573 struct device_node *node = dev->of_node;
574 struct resource regs;
575 int err;
576
577 err = of_address_to_resource(node, 0, ®s);
578 if (err) {
579 dev_err(dev, "missing \"reg\" property\n");
580 return err;
581 }
582
583 port->reg_base = devm_pci_remap_cfg_resource(dev, ®s);
584 if (IS_ERR(port->reg_base))
585 return PTR_ERR(port->reg_base);
586
587 port->irq = irq_of_parse_and_map(node, 0);
588 err = devm_request_irq(dev, port->irq, xilinx_pcie_intr_handler,
589 IRQF_SHARED | IRQF_NO_THREAD,
590 "xilinx-pcie", port);
591 if (err) {
592 dev_err(dev, "unable to request irq %d\n", port->irq);
593 return err;
594 }
595
596 return 0;
597}
598
599
600
601
602
603
604
605static int xilinx_pcie_probe(struct platform_device *pdev)
606{
607 struct device *dev = &pdev->dev;
608 struct xilinx_pcie_port *port;
609 struct pci_host_bridge *bridge;
610 int err;
611
612 if (!dev->of_node)
613 return -ENODEV;
614
615 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*port));
616 if (!bridge)
617 return -ENODEV;
618
619 port = pci_host_bridge_priv(bridge);
620
621 port->dev = dev;
622
623 err = xilinx_pcie_parse_dt(port);
624 if (err) {
625 dev_err(dev, "Parsing DT failed\n");
626 return err;
627 }
628
629 xilinx_pcie_init_port(port);
630
631 err = xilinx_pcie_init_irq_domain(port);
632 if (err) {
633 dev_err(dev, "Failed creating IRQ Domain\n");
634 return err;
635 }
636
637 bridge->sysdata = port;
638 bridge->ops = &xilinx_pcie_ops;
639
640#ifdef CONFIG_PCI_MSI
641 xilinx_pcie_msi_chip.dev = dev;
642 bridge->msi = &xilinx_pcie_msi_chip;
643#endif
644 return pci_host_probe(bridge);
645}
646
647static const struct of_device_id xilinx_pcie_of_match[] = {
648 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
649 {}
650};
651
652static struct platform_driver xilinx_pcie_driver = {
653 .driver = {
654 .name = "xilinx-pcie",
655 .of_match_table = xilinx_pcie_of_match,
656 .suppress_bind_attrs = true,
657 },
658 .probe = xilinx_pcie_probe,
659};
660builtin_platform_driver(xilinx_pcie_driver);
661