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