0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/interrupt.h>
0014 #include <linux/irq.h>
0015 #include <linux/irqdomain.h>
0016 #include <linux/kernel.h>
0017 #include <linux/init.h>
0018 #include <linux/msi.h>
0019 #include <linux/of_address.h>
0020 #include <linux/of_pci.h>
0021 #include <linux/of_platform.h>
0022 #include <linux/of_irq.h>
0023 #include <linux/pci.h>
0024 #include <linux/pci-ecam.h>
0025 #include <linux/platform_device.h>
0026
0027 #include "../pci.h"
0028
0029
0030 #define XILINX_PCIE_REG_BIR 0x00000130
0031 #define XILINX_PCIE_REG_IDR 0x00000138
0032 #define XILINX_PCIE_REG_IMR 0x0000013c
0033 #define XILINX_PCIE_REG_PSCR 0x00000144
0034 #define XILINX_PCIE_REG_RPSC 0x00000148
0035 #define XILINX_PCIE_REG_MSIBASE1 0x0000014c
0036 #define XILINX_PCIE_REG_MSIBASE2 0x00000150
0037 #define XILINX_PCIE_REG_RPEFR 0x00000154
0038 #define XILINX_PCIE_REG_RPIFR1 0x00000158
0039 #define XILINX_PCIE_REG_RPIFR2 0x0000015c
0040
0041
0042 #define XILINX_PCIE_INTR_LINK_DOWN BIT(0)
0043 #define XILINX_PCIE_INTR_ECRC_ERR BIT(1)
0044 #define XILINX_PCIE_INTR_STR_ERR BIT(2)
0045 #define XILINX_PCIE_INTR_HOT_RESET BIT(3)
0046 #define XILINX_PCIE_INTR_CFG_TIMEOUT BIT(8)
0047 #define XILINX_PCIE_INTR_CORRECTABLE BIT(9)
0048 #define XILINX_PCIE_INTR_NONFATAL BIT(10)
0049 #define XILINX_PCIE_INTR_FATAL BIT(11)
0050 #define XILINX_PCIE_INTR_INTX BIT(16)
0051 #define XILINX_PCIE_INTR_MSI BIT(17)
0052 #define XILINX_PCIE_INTR_SLV_UNSUPP BIT(20)
0053 #define XILINX_PCIE_INTR_SLV_UNEXP BIT(21)
0054 #define XILINX_PCIE_INTR_SLV_COMPL BIT(22)
0055 #define XILINX_PCIE_INTR_SLV_ERRP BIT(23)
0056 #define XILINX_PCIE_INTR_SLV_CMPABT BIT(24)
0057 #define XILINX_PCIE_INTR_SLV_ILLBUR BIT(25)
0058 #define XILINX_PCIE_INTR_MST_DECERR BIT(26)
0059 #define XILINX_PCIE_INTR_MST_SLVERR BIT(27)
0060 #define XILINX_PCIE_INTR_MST_ERRP BIT(28)
0061 #define XILINX_PCIE_IMR_ALL_MASK 0x1FF30FED
0062 #define XILINX_PCIE_IMR_ENABLE_MASK 0x1FF30F0D
0063 #define XILINX_PCIE_IDR_ALL_MASK 0xFFFFFFFF
0064
0065
0066 #define XILINX_PCIE_RPEFR_ERR_VALID BIT(18)
0067 #define XILINX_PCIE_RPEFR_REQ_ID GENMASK(15, 0)
0068 #define XILINX_PCIE_RPEFR_ALL_MASK 0xFFFFFFFF
0069
0070
0071 #define XILINX_PCIE_RPIFR1_INTR_VALID BIT(31)
0072 #define XILINX_PCIE_RPIFR1_MSI_INTR BIT(30)
0073 #define XILINX_PCIE_RPIFR1_INTR_MASK GENMASK(28, 27)
0074 #define XILINX_PCIE_RPIFR1_ALL_MASK 0xFFFFFFFF
0075 #define XILINX_PCIE_RPIFR1_INTR_SHIFT 27
0076
0077
0078 #define XILINX_PCIE_BIR_ECAM_SZ_MASK GENMASK(18, 16)
0079 #define XILINX_PCIE_BIR_ECAM_SZ_SHIFT 16
0080
0081
0082 #define XILINX_PCIE_RPIFR2_MSG_DATA GENMASK(15, 0)
0083
0084
0085 #define XILINX_PCIE_REG_RPSC_BEN BIT(0)
0086
0087
0088 #define XILINX_PCIE_REG_PSCR_LNKUP BIT(11)
0089
0090
0091 #define XILINX_NUM_MSI_IRQS 128
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103 struct xilinx_pcie {
0104 struct device *dev;
0105 void __iomem *reg_base;
0106 unsigned long msi_map[BITS_TO_LONGS(XILINX_NUM_MSI_IRQS)];
0107 struct mutex map_lock;
0108 struct irq_domain *msi_domain;
0109 struct irq_domain *leg_domain;
0110 struct list_head resources;
0111 };
0112
0113 static inline u32 pcie_read(struct xilinx_pcie *pcie, u32 reg)
0114 {
0115 return readl(pcie->reg_base + reg);
0116 }
0117
0118 static inline void pcie_write(struct xilinx_pcie *pcie, u32 val, u32 reg)
0119 {
0120 writel(val, pcie->reg_base + reg);
0121 }
0122
0123 static inline bool xilinx_pcie_link_up(struct xilinx_pcie *pcie)
0124 {
0125 return (pcie_read(pcie, XILINX_PCIE_REG_PSCR) &
0126 XILINX_PCIE_REG_PSCR_LNKUP) ? 1 : 0;
0127 }
0128
0129
0130
0131
0132
0133 static void xilinx_pcie_clear_err_interrupts(struct xilinx_pcie *pcie)
0134 {
0135 struct device *dev = pcie->dev;
0136 unsigned long val = pcie_read(pcie, XILINX_PCIE_REG_RPEFR);
0137
0138 if (val & XILINX_PCIE_RPEFR_ERR_VALID) {
0139 dev_dbg(dev, "Requester ID %lu\n",
0140 val & XILINX_PCIE_RPEFR_REQ_ID);
0141 pcie_write(pcie, XILINX_PCIE_RPEFR_ALL_MASK,
0142 XILINX_PCIE_REG_RPEFR);
0143 }
0144 }
0145
0146
0147
0148
0149
0150
0151
0152
0153 static bool xilinx_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
0154 {
0155 struct xilinx_pcie *pcie = bus->sysdata;
0156
0157
0158 if (!pci_is_root_bus(bus)) {
0159 if (!xilinx_pcie_link_up(pcie))
0160 return false;
0161 } else if (devfn > 0) {
0162
0163 return false;
0164 }
0165 return true;
0166 }
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 static void __iomem *xilinx_pcie_map_bus(struct pci_bus *bus,
0178 unsigned int devfn, int where)
0179 {
0180 struct xilinx_pcie *pcie = bus->sysdata;
0181
0182 if (!xilinx_pcie_valid_device(bus, devfn))
0183 return NULL;
0184
0185 return pcie->reg_base + PCIE_ECAM_OFFSET(bus->number, devfn, where);
0186 }
0187
0188
0189 static struct pci_ops xilinx_pcie_ops = {
0190 .map_bus = xilinx_pcie_map_bus,
0191 .read = pci_generic_config_read,
0192 .write = pci_generic_config_write,
0193 };
0194
0195
0196
0197 static void xilinx_msi_top_irq_ack(struct irq_data *d)
0198 {
0199
0200
0201
0202
0203
0204 }
0205
0206 static struct irq_chip xilinx_msi_top_chip = {
0207 .name = "PCIe MSI",
0208 .irq_ack = xilinx_msi_top_irq_ack,
0209 };
0210
0211 static int xilinx_msi_set_affinity(struct irq_data *d, const struct cpumask *mask, bool force)
0212 {
0213 return -EINVAL;
0214 }
0215
0216 static void xilinx_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
0217 {
0218 struct xilinx_pcie *pcie = irq_data_get_irq_chip_data(data);
0219 phys_addr_t pa = ALIGN_DOWN(virt_to_phys(pcie), SZ_4K);
0220
0221 msg->address_lo = lower_32_bits(pa);
0222 msg->address_hi = upper_32_bits(pa);
0223 msg->data = data->hwirq;
0224 }
0225
0226 static struct irq_chip xilinx_msi_bottom_chip = {
0227 .name = "Xilinx MSI",
0228 .irq_set_affinity = xilinx_msi_set_affinity,
0229 .irq_compose_msi_msg = xilinx_compose_msi_msg,
0230 };
0231
0232 static int xilinx_msi_domain_alloc(struct irq_domain *domain, unsigned int virq,
0233 unsigned int nr_irqs, void *args)
0234 {
0235 struct xilinx_pcie *pcie = domain->host_data;
0236 int hwirq, i;
0237
0238 mutex_lock(&pcie->map_lock);
0239
0240 hwirq = bitmap_find_free_region(pcie->msi_map, XILINX_NUM_MSI_IRQS, order_base_2(nr_irqs));
0241
0242 mutex_unlock(&pcie->map_lock);
0243
0244 if (hwirq < 0)
0245 return -ENOSPC;
0246
0247 for (i = 0; i < nr_irqs; i++)
0248 irq_domain_set_info(domain, virq + i, hwirq + i,
0249 &xilinx_msi_bottom_chip, domain->host_data,
0250 handle_edge_irq, NULL, NULL);
0251
0252 return 0;
0253 }
0254
0255 static void xilinx_msi_domain_free(struct irq_domain *domain, unsigned int virq,
0256 unsigned int nr_irqs)
0257 {
0258 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
0259 struct xilinx_pcie *pcie = domain->host_data;
0260
0261 mutex_lock(&pcie->map_lock);
0262
0263 bitmap_release_region(pcie->msi_map, d->hwirq, order_base_2(nr_irqs));
0264
0265 mutex_unlock(&pcie->map_lock);
0266 }
0267
0268 static const struct irq_domain_ops xilinx_msi_domain_ops = {
0269 .alloc = xilinx_msi_domain_alloc,
0270 .free = xilinx_msi_domain_free,
0271 };
0272
0273 static struct msi_domain_info xilinx_msi_info = {
0274 .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
0275 .chip = &xilinx_msi_top_chip,
0276 };
0277
0278 static int xilinx_allocate_msi_domains(struct xilinx_pcie *pcie)
0279 {
0280 struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
0281 struct irq_domain *parent;
0282
0283 parent = irq_domain_create_linear(fwnode, XILINX_NUM_MSI_IRQS,
0284 &xilinx_msi_domain_ops, pcie);
0285 if (!parent) {
0286 dev_err(pcie->dev, "failed to create IRQ domain\n");
0287 return -ENOMEM;
0288 }
0289 irq_domain_update_bus_token(parent, DOMAIN_BUS_NEXUS);
0290
0291 pcie->msi_domain = pci_msi_create_irq_domain(fwnode, &xilinx_msi_info, parent);
0292 if (!pcie->msi_domain) {
0293 dev_err(pcie->dev, "failed to create MSI domain\n");
0294 irq_domain_remove(parent);
0295 return -ENOMEM;
0296 }
0297
0298 return 0;
0299 }
0300
0301 static void xilinx_free_msi_domains(struct xilinx_pcie *pcie)
0302 {
0303 struct irq_domain *parent = pcie->msi_domain->parent;
0304
0305 irq_domain_remove(pcie->msi_domain);
0306 irq_domain_remove(parent);
0307 }
0308
0309
0310
0311
0312
0313
0314
0315
0316
0317
0318
0319 static int xilinx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
0320 irq_hw_number_t hwirq)
0321 {
0322 irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
0323 irq_set_chip_data(irq, domain->host_data);
0324
0325 return 0;
0326 }
0327
0328
0329 static const struct irq_domain_ops intx_domain_ops = {
0330 .map = xilinx_pcie_intx_map,
0331 .xlate = pci_irqd_intx_xlate,
0332 };
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343 static irqreturn_t xilinx_pcie_intr_handler(int irq, void *data)
0344 {
0345 struct xilinx_pcie *pcie = (struct xilinx_pcie *)data;
0346 struct device *dev = pcie->dev;
0347 u32 val, mask, status;
0348
0349
0350 val = pcie_read(pcie, XILINX_PCIE_REG_IDR);
0351 mask = pcie_read(pcie, XILINX_PCIE_REG_IMR);
0352
0353 status = val & mask;
0354 if (!status)
0355 return IRQ_NONE;
0356
0357 if (status & XILINX_PCIE_INTR_LINK_DOWN)
0358 dev_warn(dev, "Link Down\n");
0359
0360 if (status & XILINX_PCIE_INTR_ECRC_ERR)
0361 dev_warn(dev, "ECRC failed\n");
0362
0363 if (status & XILINX_PCIE_INTR_STR_ERR)
0364 dev_warn(dev, "Streaming error\n");
0365
0366 if (status & XILINX_PCIE_INTR_HOT_RESET)
0367 dev_info(dev, "Hot reset\n");
0368
0369 if (status & XILINX_PCIE_INTR_CFG_TIMEOUT)
0370 dev_warn(dev, "ECAM access timeout\n");
0371
0372 if (status & XILINX_PCIE_INTR_CORRECTABLE) {
0373 dev_warn(dev, "Correctable error message\n");
0374 xilinx_pcie_clear_err_interrupts(pcie);
0375 }
0376
0377 if (status & XILINX_PCIE_INTR_NONFATAL) {
0378 dev_warn(dev, "Non fatal error message\n");
0379 xilinx_pcie_clear_err_interrupts(pcie);
0380 }
0381
0382 if (status & XILINX_PCIE_INTR_FATAL) {
0383 dev_warn(dev, "Fatal error message\n");
0384 xilinx_pcie_clear_err_interrupts(pcie);
0385 }
0386
0387 if (status & (XILINX_PCIE_INTR_INTX | XILINX_PCIE_INTR_MSI)) {
0388 struct irq_domain *domain;
0389
0390 val = pcie_read(pcie, XILINX_PCIE_REG_RPIFR1);
0391
0392
0393 if (!(val & XILINX_PCIE_RPIFR1_INTR_VALID)) {
0394 dev_warn(dev, "RP Intr FIFO1 read error\n");
0395 goto error;
0396 }
0397
0398
0399 if (val & XILINX_PCIE_RPIFR1_MSI_INTR) {
0400 val = pcie_read(pcie, XILINX_PCIE_REG_RPIFR2) &
0401 XILINX_PCIE_RPIFR2_MSG_DATA;
0402 domain = pcie->msi_domain->parent;
0403 } else {
0404 val = (val & XILINX_PCIE_RPIFR1_INTR_MASK) >>
0405 XILINX_PCIE_RPIFR1_INTR_SHIFT;
0406 domain = pcie->leg_domain;
0407 }
0408
0409
0410 pcie_write(pcie, XILINX_PCIE_RPIFR1_ALL_MASK,
0411 XILINX_PCIE_REG_RPIFR1);
0412
0413 generic_handle_domain_irq(domain, val);
0414 }
0415
0416 if (status & XILINX_PCIE_INTR_SLV_UNSUPP)
0417 dev_warn(dev, "Slave unsupported request\n");
0418
0419 if (status & XILINX_PCIE_INTR_SLV_UNEXP)
0420 dev_warn(dev, "Slave unexpected completion\n");
0421
0422 if (status & XILINX_PCIE_INTR_SLV_COMPL)
0423 dev_warn(dev, "Slave completion timeout\n");
0424
0425 if (status & XILINX_PCIE_INTR_SLV_ERRP)
0426 dev_warn(dev, "Slave Error Poison\n");
0427
0428 if (status & XILINX_PCIE_INTR_SLV_CMPABT)
0429 dev_warn(dev, "Slave Completer Abort\n");
0430
0431 if (status & XILINX_PCIE_INTR_SLV_ILLBUR)
0432 dev_warn(dev, "Slave Illegal Burst\n");
0433
0434 if (status & XILINX_PCIE_INTR_MST_DECERR)
0435 dev_warn(dev, "Master decode error\n");
0436
0437 if (status & XILINX_PCIE_INTR_MST_SLVERR)
0438 dev_warn(dev, "Master slave error\n");
0439
0440 if (status & XILINX_PCIE_INTR_MST_ERRP)
0441 dev_warn(dev, "Master error poison\n");
0442
0443 error:
0444
0445 pcie_write(pcie, status, XILINX_PCIE_REG_IDR);
0446
0447 return IRQ_HANDLED;
0448 }
0449
0450
0451
0452
0453
0454
0455
0456 static int xilinx_pcie_init_irq_domain(struct xilinx_pcie *pcie)
0457 {
0458 struct device *dev = pcie->dev;
0459 struct device_node *pcie_intc_node;
0460 int ret;
0461
0462
0463 pcie_intc_node = of_get_next_child(dev->of_node, NULL);
0464 if (!pcie_intc_node) {
0465 dev_err(dev, "No PCIe Intc node found\n");
0466 return -ENODEV;
0467 }
0468
0469 pcie->leg_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
0470 &intx_domain_ops,
0471 pcie);
0472 of_node_put(pcie_intc_node);
0473 if (!pcie->leg_domain) {
0474 dev_err(dev, "Failed to get a INTx IRQ domain\n");
0475 return -ENODEV;
0476 }
0477
0478
0479 if (IS_ENABLED(CONFIG_PCI_MSI)) {
0480 phys_addr_t pa = ALIGN_DOWN(virt_to_phys(pcie), SZ_4K);
0481
0482 ret = xilinx_allocate_msi_domains(pcie);
0483 if (ret)
0484 return ret;
0485
0486 pcie_write(pcie, upper_32_bits(pa), XILINX_PCIE_REG_MSIBASE1);
0487 pcie_write(pcie, lower_32_bits(pa), XILINX_PCIE_REG_MSIBASE2);
0488 }
0489
0490 return 0;
0491 }
0492
0493
0494
0495
0496
0497 static void xilinx_pcie_init_port(struct xilinx_pcie *pcie)
0498 {
0499 struct device *dev = pcie->dev;
0500
0501 if (xilinx_pcie_link_up(pcie))
0502 dev_info(dev, "PCIe Link is UP\n");
0503 else
0504 dev_info(dev, "PCIe Link is DOWN\n");
0505
0506
0507 pcie_write(pcie, ~XILINX_PCIE_IDR_ALL_MASK,
0508 XILINX_PCIE_REG_IMR);
0509
0510
0511 pcie_write(pcie, pcie_read(pcie, XILINX_PCIE_REG_IDR) &
0512 XILINX_PCIE_IMR_ALL_MASK,
0513 XILINX_PCIE_REG_IDR);
0514
0515
0516 pcie_write(pcie, XILINX_PCIE_IMR_ENABLE_MASK, XILINX_PCIE_REG_IMR);
0517
0518
0519 pcie_write(pcie, pcie_read(pcie, XILINX_PCIE_REG_RPSC) |
0520 XILINX_PCIE_REG_RPSC_BEN,
0521 XILINX_PCIE_REG_RPSC);
0522 }
0523
0524
0525
0526
0527
0528
0529
0530 static int xilinx_pcie_parse_dt(struct xilinx_pcie *pcie)
0531 {
0532 struct device *dev = pcie->dev;
0533 struct device_node *node = dev->of_node;
0534 struct resource regs;
0535 unsigned int irq;
0536 int err;
0537
0538 err = of_address_to_resource(node, 0, ®s);
0539 if (err) {
0540 dev_err(dev, "missing \"reg\" property\n");
0541 return err;
0542 }
0543
0544 pcie->reg_base = devm_pci_remap_cfg_resource(dev, ®s);
0545 if (IS_ERR(pcie->reg_base))
0546 return PTR_ERR(pcie->reg_base);
0547
0548 irq = irq_of_parse_and_map(node, 0);
0549 err = devm_request_irq(dev, irq, xilinx_pcie_intr_handler,
0550 IRQF_SHARED | IRQF_NO_THREAD,
0551 "xilinx-pcie", pcie);
0552 if (err) {
0553 dev_err(dev, "unable to request irq %d\n", irq);
0554 return err;
0555 }
0556
0557 return 0;
0558 }
0559
0560
0561
0562
0563
0564
0565
0566 static int xilinx_pcie_probe(struct platform_device *pdev)
0567 {
0568 struct device *dev = &pdev->dev;
0569 struct xilinx_pcie *pcie;
0570 struct pci_host_bridge *bridge;
0571 int err;
0572
0573 if (!dev->of_node)
0574 return -ENODEV;
0575
0576 bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
0577 if (!bridge)
0578 return -ENODEV;
0579
0580 pcie = pci_host_bridge_priv(bridge);
0581 mutex_init(&pcie->map_lock);
0582 pcie->dev = dev;
0583
0584 err = xilinx_pcie_parse_dt(pcie);
0585 if (err) {
0586 dev_err(dev, "Parsing DT failed\n");
0587 return err;
0588 }
0589
0590 xilinx_pcie_init_port(pcie);
0591
0592 err = xilinx_pcie_init_irq_domain(pcie);
0593 if (err) {
0594 dev_err(dev, "Failed creating IRQ Domain\n");
0595 return err;
0596 }
0597
0598 bridge->sysdata = pcie;
0599 bridge->ops = &xilinx_pcie_ops;
0600
0601 err = pci_host_probe(bridge);
0602 if (err)
0603 xilinx_free_msi_domains(pcie);
0604
0605 return err;
0606 }
0607
0608 static const struct of_device_id xilinx_pcie_of_match[] = {
0609 { .compatible = "xlnx,axi-pcie-host-1.00.a", },
0610 {}
0611 };
0612
0613 static struct platform_driver xilinx_pcie_driver = {
0614 .driver = {
0615 .name = "xilinx-pcie",
0616 .of_match_table = xilinx_pcie_of_match,
0617 .suppress_bind_attrs = true,
0618 },
0619 .probe = xilinx_pcie_probe,
0620 };
0621 builtin_platform_driver(xilinx_pcie_driver);