Re: [PATCH 1/2] pci: altera: Add StratixXX PCIe support
From: Ley Foon Tan
Date: Sun Dec 30 2018 - 21:01:48 EST
On Fri, 2018-12-28 at 21:12 -0600, Bjorn Helgaas wrote:
> Run "git log --oneline drivers/pci/controller" and make your subject
> line
> match in style, phrasing, and capitalization.
>
> On Thu, Dec 27, 2018 at 01:09:45AM +0800, Ley Foon Tan wrote:
> >
> > Add PCIe rootport support for StratixXX device.
> s/rootport/Root Port/
Noted.
>
> I don't know what the correct styling of "StratixXX" is.ÂÂYou also
> have
> "Stratix xx" below.ÂÂBoth (and the subject line) should probably
> match?
Will change it to Stratix10.
>
> >
> > Main differences:
> > - HIP interface
> > - TLP programming flow
> It'd be nice to elaborate on these differences a little bit.
Okay.
>
> >
> > Signed-off-by: Ley Foon Tan <ley.foon.tan@xxxxxxxxx>
> > ---
> > Âdrivers/pci/controller/KconfigÂÂÂÂÂÂÂ|ÂÂÂÂ2 +-
> > Âdrivers/pci/controller/pcie-altera.c |ÂÂ228
> > +++++++++++++++++++++++++++++++---
> > Â2 files changed, 209 insertions(+), 21 deletions(-)
> > Âmode change 100644 => 100755 drivers/pci/controller/Kconfig
> >
> > diff --git a/drivers/pci/controller/Kconfig
> > b/drivers/pci/controller/Kconfig
> > old mode 100644
> > new mode 100755
> > index 6671946..6012f30
> > --- a/drivers/pci/controller/Kconfig
> > +++ b/drivers/pci/controller/Kconfig
> > @@ -175,7 +175,7 @@ config PCIE_IPROC_MSI
> > Â
> > Âconfig PCIE_ALTERA
> > Â bool "Altera PCIe controller"
> > - depends on ARM || NIOS2 || COMPILE_TEST
> > + depends on ARM || NIOS2 || ARM64 || COMPILE_TEST
> > Â help
> > Â ÂÂSay Y here if you want to enable PCIe controller support
> > on Altera
> > Â ÂÂFPGA.
> > diff --git a/drivers/pci/controller/pcie-altera.c
> > b/drivers/pci/controller/pcie-altera.c
> > index 7d05e51..b9d6eb2 100644
> > --- a/drivers/pci/controller/pcie-altera.c
> > +++ b/drivers/pci/controller/pcie-altera.c
> > @@ -11,6 +11,7 @@
> > Â#include <linux/irqchip/chained_irq.h>
> > Â#include <linux/init.h>
> > Â#include <linux/of_address.h>
> > +#include <linux/of_device.h>
> > Â#include <linux/of_irq.h>
> > Â#include <linux/of_pci.h>
> > Â#include <linux/pci.h>
> > @@ -37,7 +38,12 @@
> > Â#define RP_LTSSM_MASK 0x1f
> > Â#define LTSSM_L0 0xf
> > Â
> > -#define PCIE_CAP_OFFSET 0x80
> > +#define SXX_RP_TX_CNTRL 0x2004
> > +#define SXX_RP_RXCPL_REG 0x2008
> > +#define SXX_RP_RXCPL_STATUS 0x200C
> > +#define SXX_RP_CFG_ADDR(pcie, reg) \
> > + ((pcie->hip_base) + (reg) + (1 << 20))
> > +
> > Â/* TLP configuration type 0 and 1 */
> > Â#define TLP_FMTTYPE_CFGRD0 0x04 /*
> > Configuration Read Type 0 */
> > Â#define TLP_FMTTYPE_CFGWR0 0x44 /*
> > Configuration Write Type 0 */
> > @@ -49,18 +55,19 @@
> > Â#define RP_DEVFN 0
> > Â#define TLP_REQ_ID(bus, devfn) (((bus) << 8) |
> > (devfn))
> > Â#define TLP_CFGRD_DW0(pcie, bus)
> > \
> > -ÂÂÂÂ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGRD0
> > \
> > - ÂÂÂÂ: TLP_FMTTYPE_CFGRD1) << 24) |
> > \
> > +ÂÂÂÂ((((bus == pcie->root_bus_nr) ? pcie->pcie_data->cfgrd0
> > \
> > + ÂÂ: pcie->pcie_data->cfgrd1) <<
> > 24) | \
> > ÂÂÂÂÂÂTLP_PAYLOAD_SIZE)
> > Â#define TLP_CFGWR_DW0(pcie, bus)
> > \
> > -ÂÂÂÂ((((bus == pcie->root_bus_nr) ? TLP_FMTTYPE_CFGWR0
> > \
> > - ÂÂÂÂ: TLP_FMTTYPE_CFGWR1) << 24) |
> > \
> > +ÂÂÂÂ((((bus == pcie->root_bus_nr) ? pcie->pcie_data->cfgwr0
> > \
> > + ÂÂ: pcie->pcie_data->cfgwr1) <<
> > 24) | \
> > ÂÂÂÂÂÂTLP_PAYLOAD_SIZE)
> > Â#define TLP_CFG_DW1(pcie, tag, be) \
> > ÂÂÂÂÂ(((TLP_REQ_ID(pcie->root_bus_nr,ÂÂRP_DEVFN)) << 16) | (tag <<
> > 8) | (be))
> > Â#define TLP_CFG_DW2(bus, devfn, offset) \
> > Â (((bus) << 24) | ((devfn) << 16) |
> > (offset))
> > Â#define TLP_COMP_STATUS(s) (((s) >> 13) & 7)
> > +#define TLP_BYTE_COUNT(s) (((s) >> 0) & 0xfff)
> > Â#define TLP_HDR_SIZE 3
> > Â#define TLP_LOOP 500
> > Â
> > @@ -72,11 +79,30 @@
> > Âstruct altera_pcie {
> > Â struct platform_device *pdev;
> > Â void __iomem *cra_base; /* DT Cra */
> > + void __iomem *hip_base;
> IIRC, the "DT Cra" comment was a hint about what DT property contains
> this data.ÂÂIt'd be good to use the comment consistently (either
> always or never).
Will remove it.
>
> >
> > Â int irq;
> > Â u8 root_bus_nr;
> > Â struct irq_domain *irq_domain;
> > Â struct resource bus_range;
> > Â struct list_head resources;
> > + const struct altera_pcie_data *pcie_data;
> > +};
> > +
> > +struct altera_pcie_data {
> > + int (*tlp_read_pkt)(struct altera_pcie *pcie, u32 *value);
> > + void (*tlp_write_pkt)(struct altera_pcie *pcie, u32
> > *headers,
> > + ÂÂÂÂÂÂu32 data, bool align);
> > + bool (*get_link_status)(struct altera_pcie *pcie);
> > + int (*rp_read_cfg)(struct altera_pcie *pcie, int where,
> > + ÂÂÂÂÂÂint size, u32 *value);
> > + int (*rp_write_cfg)(struct altera_pcie *pcie, u8 bus, int
> > where,
> > + ÂÂÂÂÂÂÂint size, u32 value);
> > + bool sxx_flag; /* Stratix xx */
> > + u32 cap_offset; /* Capabilities register
> > offset */
> I guess this is the *PCIe* Capability?ÂÂThere are lots of
> capabilities, so maybe the comment could be more specific.
Yes, It is PCIe capability structure register offset.Â
Will update comment.
>
> >
> > + u32 cfgrd0;
> > + u32 cfgrd1;
> > + u32 cfgwr0;
> > + u32 cfgwr1;
> > Â};
> > Â
> > Âstruct tlp_rp_regpair_t {
> > @@ -101,6 +127,13 @@ static bool altera_pcie_link_up(struct
> > altera_pcie *pcie)
> > Â return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) ==
> > LTSSM_L0);
> > Â}
> > Â
> > +/* Instead of LTSSM state we use the link capabilities information
> > */
> > +static bool sxx_altera_pcie_link_up(struct altera_pcie *pcie)
> > +{
> > + return !!(readw(SXX_RP_CFG_ADDR(pcie, pcie->pcie_data-
> > >cap_offset + PCI_EXP_LNKSTA))
> > + & PCI_EXP_LNKSTA_DLLLA);
> > +}
> > +
> > Â/*
> > Â * Altera PCIe port uses BAR0 of RC's configuration space as the
> > translation
> > Â * from PCI bus to native BUS.ÂÂEntire DDR region is mapped into
> > PCIe space
> > @@ -128,12 +161,18 @@ static void tlp_write_tx(struct altera_pcie
> > *pcie,
> > Â cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
> > Â}
> > Â
> > +static void sxx_tlp_write_tx(struct altera_pcie *pcie, u32 reg0,
> > u32 ctrl)
> > +{
> > + cra_writel(pcie, reg0, RP_TX_REG0);
> > + cra_writel(pcie, ctrl, SXX_RP_TX_CNTRL);
> > +}
> > +
> > Âstatic bool altera_pcie_valid_device(struct altera_pcie *pcie,
> > Â ÂÂÂÂÂstruct pci_bus *bus, int dev)
> > Â{
> > Â /* If there is no link, then there is no device */
> > Â if (bus->number != pcie->root_bus_nr) {
> > - if (!altera_pcie_link_up(pcie))
> > + if (!pcie->pcie_data->get_link_status(pcie))
> > Â return false;
> > Â }
> > Â
> > @@ -183,6 +222,46 @@ static int tlp_read_packet(struct altera_pcie
> > *pcie, u32 *value)
> > Â return PCIBIOS_DEVICE_NOT_FOUND;
> > Â}
> > Â
> > +static int sxx_tlp_read_packet(struct altera_pcie *pcie, u32
> > *value)
> > +{
> > + int i;
> > + u32 ctrl;
> > + u32 comp_status;
> > + u32 dw[4];
> > + u32 count = 0;
> > +
> > + for (i = 0; i < TLP_LOOP; i++) {
> > + ctrl = cra_readl(pcie, SXX_RP_RXCPL_STATUS);
> > + if (!(ctrl & RP_RXCPL_SOP))
> > + continue;
> > +
> > + /* read first DW */
> s/read/Read/ to match other comments.
Noted.
>
> >
> > + dw[count++] = cra_readl(pcie, SXX_RP_RXCPL_REG);
> > +
> > + /* Poll for EOP */
> > + for (i = 0; i < TLP_LOOP; i++) {
> > + ctrl = cra_readl(pcie,
> > SXX_RP_RXCPL_STATUS);
> > + dw[count++] = cra_readl(pcie,
> > SXX_RP_RXCPL_REG);
> > + if (ctrl & RP_RXCPL_EOP) {
> > + comp_status =
> > TLP_COMP_STATUS(dw[1]);
> > + if (comp_status)
> > + return
> > PCIBIOS_DEVICE_NOT_FOUND;
> > +
> > + if (value &&
> > + ÂÂÂÂTLP_BYTE_COUNT(dw[1]) ==
> > sizeof(u32) &&
> > + ÂÂÂÂcount >= 3)
> > + *value = dw[3];
> > +
> > + return PCIBIOS_SUCCESSFUL;
> > + }
> > + }
> > +
> > + udelay(5);
> > + }
> > +
> > + return PCIBIOS_DEVICE_NOT_FOUND;
> > +}
> > +
> > Âstatic void tlp_write_packet(struct altera_pcie *pcie, u32
> > *headers,
> > Â ÂÂÂÂÂu32 data, bool align)
> > Â{
> > @@ -210,6 +289,18 @@ static void tlp_write_packet(struct
> > altera_pcie *pcie, u32 *headers,
> > Â tlp_write_tx(pcie, &tlp_rp_regdata);
> > Â}
> > Â
> > +static void sxx_tlp_write_packet(struct altera_pcie *pcie, u32
> > *headers,
> > + Âu32 data, bool dummy)
> > +{
> > + sxx_tlp_write_tx(pcie, headers[0], RP_TX_SOP);
> > +
> > + sxx_tlp_write_tx(pcie, headers[1], 0);
> > +
> > + sxx_tlp_write_tx(pcie, headers[2], 0);
> > +
> > + sxx_tlp_write_tx(pcie, data, RP_TX_EOP);
> Blank lines seem superfluous since these are all parallel.
Will remove blank lines.
> +}
> > +
> > Âstatic int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus,
> > u32 devfn,
> > Â ÂÂÂÂÂÂint where, u8 byte_en, u32 *value)
> > Â{
> > @@ -219,9 +310,9 @@ static int tlp_cfg_dword_read(struct
> > altera_pcie *pcie, u8 bus, u32 devfn,
> > Â headers[1] = TLP_CFG_DW1(pcie, TLP_READ_TAG, byte_en);
> > Â headers[2] = TLP_CFG_DW2(bus, devfn, where);
> > Â
> > - tlp_write_packet(pcie, headers, 0, false);
> > + pcie->pcie_data->tlp_write_pkt(pcie, headers, 0, false);
> > Â
> > - return tlp_read_packet(pcie, value);
> > + return pcie->pcie_data->tlp_read_pkt(pcie, value);
> > Â}
> > Â
> > Âstatic int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus,
> > u32 devfn,
> > @@ -236,11 +327,11 @@ static int tlp_cfg_dword_write(struct
> > altera_pcie *pcie, u8 bus, u32 devfn,
> > Â
> > Â /* check alignment to Qword */
> > Â if ((where & 0x7) == 0)
> > - tlp_write_packet(pcie, headers, value, true);
> > + pcie->pcie_data->tlp_write_pkt(pcie, headers,
> > value, true);
> > Â else
> > - tlp_write_packet(pcie, headers, value, false);
> > + pcie->pcie_data->tlp_write_pkt(pcie, headers,
> > value, false);
> > Â
> > - ret = tlp_read_packet(pcie, NULL);
> > + ret = pcie->pcie_data->tlp_read_pkt(pcie, NULL);
> > Â if (ret != PCIBIOS_SUCCESSFUL)
> > Â return ret;
> > Â
> > @@ -254,6 +345,52 @@ static int tlp_cfg_dword_write(struct
> > altera_pcie *pcie, u8 bus, u32 devfn,
> > Â return PCIBIOS_SUCCESSFUL;
> > Â}
> > Â
> > +static int sxx_rp_read_cfg(struct altera_pcie *pcie, int where,
> > + ÂÂÂint size, u32 *value)
> > +{
> > + void *addr = SXX_RP_CFG_ADDR(pcie, where);
> > +
> > + switch (size) {
> > + case 1:
> > + *value = readb(addr);
> > + break;
> > + case 2:
> > + *value = readw(addr);
> > + break;
> > + default:
> > + *value = readl(addr);
> > + break;
> > + }
> > +
> > + return PCIBIOS_SUCCESSFUL;
> > +}
> > +
> > +static int sxx_rp_write_cfg(struct altera_pcie *pcie, u8 bus, int
> > where,
> > + ÂÂÂint size, u32 value)
> > +{
> > + void *addr = SXX_RP_CFG_ADDR(pcie, where);
> > +
> > + switch (size) {
> > + case 1:
> > + writeb(value, addr);
> > + break;
> > + case 2:
> > + writew(value, addr);
> > + break;
> > + default:
> > + writel(value, addr);
> > + break;
> > + }
> > +
> > + /* Monitor changes to PCI_PRIMARY_BUS register on root
> > port
> > + * and update local copy of root bus number accordingly.
> > + */
> Multi-line comment style:
>
> /*
> * Monitor ...
> */
Noted.
>
> >
> > + if (bus == pcie->root_bus_nr && where == PCI_PRIMARY_BUS)
> > + pcie->root_bus_nr = (u8)(value);
> > +
> > + return PCIBIOS_SUCCESSFUL;
> > +}
> > +
> > Âstatic int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8
> > busno,
> > Â Âunsigned int devfn, int where,
> > int size,
> > Â Âu32 *value)
> > @@ -262,6 +399,9 @@ static int _altera_pcie_cfg_read(struct
> > altera_pcie *pcie, u8 busno,
> > Â u32 data;
> > Â u8 byte_en;
> > Â
> > + if (busno == pcie->root_bus_nr && pcie->pcie_data-
> > >rp_read_cfg)
> > + return pcie->pcie_data->rp_read_cfg(pcie, where,
> > size, value);
> > +
> > Â switch (size) {
> > Â case 1:
> > Â byte_en = 1 << (where & 3);
> > @@ -302,6 +442,10 @@ static int _altera_pcie_cfg_write(struct
> > altera_pcie *pcie, u8 busno,
> > Â u32 shift = 8 * (where & 3);
> > Â u8 byte_en;
> > Â
> > + if (busno == pcie->root_bus_nr && pcie->pcie_data-
> > >rp_write_cfg)
> > + return pcie->pcie_data->rp_write_cfg(pcie, busno,
> > where,
> > + size,
> > value);
> > +
> > Â switch (size) {
> > Â case 1:
> > Â data32 = (value & 0xff) << shift;
> > @@ -365,7 +509,7 @@ static int altera_read_cap_word(struct
> > altera_pcie *pcie, u8 busno,
> > Â int ret;
> > Â
> > Â ret = _altera_pcie_cfg_read(pcie, busno, devfn,
> > - ÂÂÂÂPCIE_CAP_OFFSET + offset,
> > sizeof(*value),
> > + ÂÂÂÂpcie->pcie_data->cap_offset +
> > offset, sizeof(*value),
> > Â ÂÂÂÂ&data);
> > Â *value = data;
> > Â return ret;
> > @@ -375,7 +519,7 @@ static int altera_write_cap_word(struct
> > altera_pcie *pcie, u8 busno,
> > Â Âunsigned int devfn, int offset,
> > u16 value)
> > Â{
> > Â return _altera_pcie_cfg_write(pcie, busno, devfn,
> > - ÂÂÂÂÂÂPCIE_CAP_OFFSET + offset,
> > sizeof(value),
> > + ÂÂÂÂÂÂpcie->pcie_data->cap_offset
> > + offset, sizeof(value),
> > Â ÂÂÂÂÂÂvalue);
> > Â}
> > Â
> > @@ -403,7 +547,7 @@ static void altera_wait_link_retrain(struct
> > altera_pcie *pcie)
> > Â /* Wait for link is up */
> > Â start_jiffies = jiffies;
> > Â for (;;) {
> > - if (altera_pcie_link_up(pcie))
> > + if (pcie->pcie_data->get_link_status(pcie))
> > Â break;
> > Â
> > Â if (time_after(jiffies, start_jiffies +
> > LINK_UP_TIMEOUT)) {
> > @@ -418,7 +562,7 @@ static void altera_pcie_retrain(struct
> > altera_pcie *pcie)
> > Â{
> > Â u16 linkcap, linkstat, linkctl;
> > Â
> > - if (!altera_pcie_link_up(pcie))
> > + if (!pcie->pcie_data->get_link_status(pcie))
> > Â return;
> > Â
> > Â /*
> > @@ -540,12 +684,20 @@ static int altera_pcie_parse_dt(struct
> > altera_pcie *pcie)
> > Â struct device *dev = &pcie->pdev->dev;
> > Â struct platform_device *pdev = pcie->pdev;
> > Â struct resource *cra;
> > + struct resource *hip;
> > Â
> > Â cra = platform_get_resource_byname(pdev, IORESOURCE_MEM,
> > "Cra");
> > Â pcie->cra_base = devm_ioremap_resource(dev, cra);
> > Â if (IS_ERR(pcie->cra_base))
> > Â return PTR_ERR(pcie->cra_base);
> > Â
> > + if (pcie->pcie_data->sxx_flag) {
> > + hip = platform_get_resource_byname(pdev,
> > IORESOURCE_MEM, "Hip");
> > + pcie->hip_base = devm_ioremap_resource(&pdev->dev,
> > hip);
> > + if (IS_ERR(pcie->hip_base))
> > + return PTR_ERR(pcie->hip_base);
> > + }
> > +
> > Â /* setup IRQ */
> > Â pcie->irq = platform_get_irq(pdev, 0);
> > Â if (pcie->irq < 0) {
> > @@ -562,6 +714,40 @@ static void altera_pcie_host_init(struct
> > altera_pcie *pcie)
> > Â altera_pcie_retrain(pcie);
> > Â}
> > Â
> > +static struct altera_pcie_data pci_1_0_funcs = {
> "*_funcs" seems a little too specific since this contains several
> data items in addition to function pointers.
Will change "*_funcs" to "*_data".
>
> >
> > + .tlp_read_pkt = tlp_read_packet,
> > + .tlp_write_pkt = tlp_write_packet,
> > + .get_link_status = altera_pcie_link_up,
> >
> > + .rp_read_cfg = NULL,
> > + .rp_write_cfg = NULL,
> > + .sxx_flag = false,
> Since the above items are all zero and will be automatically
> initialized that way, it's not necessary to mention them.ÂÂPersonal
> preference, either way is fine by me.
Will remove these.
>
> >
> > + .cap_offset = 0x80,
> > + .cfgrd0 = TLP_FMTTYPE_CFGRD0,
> > + .cfgrd1 = TLP_FMTTYPE_CFGRD1,
> > + .cfgwr0 = TLP_FMTTYPE_CFGWR0,
> > + .cfgwr1 = TLP_FMTTYPE_CFGWR1,
> > +};
> > +
> > +static struct altera_pcie_data pci_2_0_funcs = {
> > + .tlp_read_pkt = sxx_tlp_read_packet,
> > + .tlp_write_pkt = sxx_tlp_write_packet,
> > + .get_link_status = sxx_altera_pcie_link_up,
> > + .rp_read_cfg = sxx_rp_read_cfg,
> > + .rp_write_cfg = sxx_rp_write_cfg,
> > + .sxx_flag = true,
> > + .cap_offset = 0x70,
> > + .cfgrd0 = TLP_FMTTYPE_CFGRD1,
> > + .cfgrd1 = TLP_FMTTYPE_CFGRD0,
> > + .cfgwr0 = TLP_FMTTYPE_CFGWR1,
> > + .cfgwr1 = TLP_FMTTYPE_CFGWR0,
> It's "interesting" that these type0/type1 values are exactly reversed
> from the 1.0 device.ÂÂI assume that's actually correct, but if so, I
> think the macros might be more confusing than just using bare
> numbers,
> because the macro names and comments end up being wrong for 2.0.ÂÂOr
> you could define SXX_TLP_FMTTYPE_* macros I guess.
Okay, will define new macros for these.
>
> >
> > +};
> > +
> > +static const struct of_device_id altera_pcie_of_match[] = {
> > + { .compatible = "altr,pcie-root-port-1.0", .data =
> > &pci_1_0_funcs },
> > + { .compatible = "altr,pcie-root-port-2.0", .data =
> > &pci_2_0_funcs },
> > + {},
> > +};
> > +
> > Âstatic int altera_pcie_probe(struct platform_device *pdev)
> > Â{
> > Â struct device *dev = &pdev->dev;
> > @@ -570,6 +756,7 @@ static int altera_pcie_probe(struct
> > platform_device *pdev)
> > Â struct pci_bus *child;
> > Â struct pci_host_bridge *bridge;
> > Â int ret;
> > + const struct of_device_id *match;
> > Â
> > Â bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
> > Â if (!bridge)
> > @@ -578,6 +765,12 @@ static int altera_pcie_probe(struct
> > platform_device *pdev)
> > Â pcie = pci_host_bridge_priv(bridge);
> > Â pcie->pdev = pdev;
> > Â
> > + match = of_match_device(altera_pcie_of_match, &pdev->dev);
> > + if (!match)
> > + return -ENODEV;
> > +
> > + pcie->pcie_data = match->data;
> > +
> > Â ret = altera_pcie_parse_dt(pcie);
> > Â if (ret) {
> > Â dev_err(dev, "Parsing DT failed\n");
> > @@ -628,11 +821,6 @@ static int altera_pcie_probe(struct
> > platform_device *pdev)
> > Â return ret;
> > Â}
> > Â
> > -static const struct of_device_id altera_pcie_of_match[] = {
> > - { .compatible = "altr,pcie-root-port-1.0", },
> > - {},
> > -};
> > -
> > Âstatic struct platform_driver altera_pcie_driver = {
> > Â .probe = altera_pcie_probe,
> > Â .driver = {