PCI: mediatek: Add new generation controller support
Subsystems (5)
v5 cover PCI: mediatek: Add new generation controller support
These series patches add pcie-mediatek-gen3.c and dt-bindings file to support new generation PCIe controller.
Changes in v5:
1. Remove unused macros
2. Modify the config read/write callbacks, set the config byte field in TLP header and use pci_generic_config_read32/write32 to access the config space
3. Fix the settings of translation window, both MEM and IO regions works properly
4. Fix typos
Changes in v4:
1. Fix PCIe power up/down flow
2. Use "mac" and "phy" for reset names
3. Add clock names
4. Fix the variables type
Changes in v3:
1. Remove standard property in binding document
2. Return error number when get_optional* API throws an error
3. Use the bulk clk APIs
Changes in v2:
1. Fix the typo of dt-bindings patch
2. Remove the unnecessary properties in binding document
3. dispos the irq mappings of msi top domain when irq teardown
Jianjun Wang (3):
dt-bindings: PCI: mediatek: Add YAML schema
PCI: mediatek-gen3: Add MediaTek Gen3 driver for MT8192
MAINTAINERS: update entry for MediaTek PCIe controller
.../bindings/pci/mediatek-pcie-gen3.yaml | 135 +++ MAINTAINERS | 1 + drivers/pci/controller/Kconfig | 13 + drivers/pci/controller/Makefile | 1 + drivers/pci/controller/pcie-mediatek-gen3.c | 1039 +++++++++++++++++ 5 files changed, 1189 insertions(+) create mode 100644 Documentation/devicetree/bindings/pci/mediatek-pcie-gen3.yaml create mode 100644 drivers/pci/controller/pcie-mediatek-gen3.c
Signature · 5 lines
2.25.1 _______________________________________________ Linux-mediatek mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-mediatek
PATCH v5 1/3 dt-bindings: PCI: mediatek: Add YAML schema
Add YAML schemas documentation for Gen3 PCIe controller on MediaTek SoCs.
--- .../bindings/pci/mediatek-pcie-gen3.yaml | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Documentation/devicetree/bindings/pci/mediatek-pcie-gen3.yaml
Documentation/devicetree/bindings/pci/mediatek-pcie-gen3.yaml new file +135 -0
@@ -0,0 +1,135 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/pci/mediatek-pcie-gen3.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Gen3 PCIe controller on MediaTek SoCs + +maintainers: + - Jianjun Wang <[email protected]> + +allOf: + - $ref: /schemas/pci/pci-bus.yaml# + +properties: + compatible: + const: mediatek,mt8192-pcie + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + ranges: + minItems: 1 + maxItems: 8 + + resets: + minItems: 1 + maxItems: 2 + + reset-names: + anyOf: + - const: mac + - const: phy + + clocks: + maxItems: 5 + + clock-names: + items: + - const: tl_26m + - const: tl_96m + - const: tl_32k + - const: peri_26m + - const: top_133m + + assigned-clocks: + maxItems: 1 + + assigned-clock-parents: + maxItems: 1 + + phys: + maxItems: 1 + + '#interrupt-cells': + const: 1 + + interrupt-controller: + description: Interrupt controller node for handling legacy PCI interrupts. + type: object + properties: + '#address-cells': + const: 0 + '#interrupt-cells': + const: 1 + interrupt-controller: true + + required: + - '#address-cells' + - '#interrupt-cells' + - interrupt-controller + + additionalProperties: false + +required: + - compatible + - reg + - interrupts + - ranges + - clocks + - '#interrupt-cells' + - interrupt-controller + +unevaluatedProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/arm-gic.h> + #include <dt-bindings/interrupt-controller/irq.h> + + bus { + #address-cells = <2>; + #size-cells = <2>; + + pcie: pcie@11230000 { + compatible = "mediatek,mt8192-pcie"; + device_type = "pci"; + #address-cells = <3>; + #size-cells = <2>; + reg = <0x00 0x11230000 0x00 0x4000>; + reg-names = "pcie-mac"; + interrupts = <GIC_SPI 251 IRQ_TYPE_LEVEL_HIGH 0>; + bus-range = <0x00 0xff>; + ranges = <0x82000000 0x00 0x12000000 0x00 + 0x12000000 0x00 0x1000000>; + clocks = <&infracfg 40>, + <&infracfg 43>, + <&infracfg 97>, + <&infracfg 99>, + <&infracfg 111>; + clock-names = "tl_26m", "tl_96m", "tl_32k", "peri_26m", "top_133m"; + assigned-clocks = <&topckgen 50>; + assigned-clock-parents = <&topckgen 91>; + + phys = <&pciephy>; + phy-names = "pcie-phy"; + resets = <&infracfg_rst 0>; + reset-names = "phy"; + + #interrupt-cells = <1>; + interrupt-map-mask = <0 0 0 0x7>; + interrupt-map = <0 0 0 1 &pcie_intc 0>, + <0 0 0 2 &pcie_intc 1>, + <0 0 0 3 &pcie_intc 2>, + <0 0 0 4 &pcie_intc 3>; + pcie_intc: interrupt-controller { + #address-cells = <0>; + #interrupt-cells = <1>; + interrupt-controller; + }; + }; + };
Signature · 1 line
2.25.1
PATCH v5 3/3 MAINTAINERS: Add Jianjun Wang as MediaTek PCI co-maintainer
In reply to Jianjun WangUpdate entry for MediaTek PCIe controller, add Jianjun Wang as MediaTek PCI co-maintainer.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -13459,6 +13459,7 @@ F: drivers/pci/controller/dwc/pcie-histb.c PCIE DRIVER FOR MEDIATEK M: Ryder Lee <[email protected]> +M: Jianjun Wang <[email protected]> L: [email protected] L: [email protected] S: Supported
Signature · 1 line
2.25.1
PATCH v5 2/3 PCI: mediatek-gen3: Add MediaTek Gen3 driver for MT8192
In reply to Jianjun WangMediaTek's PCIe host controller has three generation HWs, the new generation HW is an individual bridge, it supports Gen3 speed and up to 256 MSI interrupt numbers for multi-function devices.
Add support for new Gen3 controller which can be found on MT8192.
---
This patch dependents on "PCI: Export pci_pio_to_address() for module use"[1] to build as a kernel module.
This interface will be used by PCI host drivers for PIO translation, export it to support compiling those drivers as kernel modules.
[1]http://lists.infradead.org/pipermail/linux-mediatek/2020-December/019504.html
--- drivers/pci/controller/Kconfig | 13 + drivers/pci/controller/Makefile | 1 + drivers/pci/controller/pcie-mediatek-gen3.c | 1039 +++++++++++++++++++ 3 files changed, 1053 insertions(+) create mode 100644 drivers/pci/controller/pcie-mediatek-gen3.c
drivers/pci/controller/Kconfig +13 -0
@@ -239,6 +239,19 @@ config PCIE_MEDIATEK Say Y here if you want to enable PCIe controller support on MediaTek SoCs. +config PCIE_MEDIATEK_GEN3 + tristate "MediaTek Gen3 PCIe controller" + depends on ARCH_MEDIATEK || COMPILE_TEST + depends on PCI_MSI_IRQ_DOMAIN + help + Adds support for PCIe Gen3 MAC controller for MediaTek SoCs. + This PCIe controller is compatible with Gen3, Gen2 and Gen1 speed, + and support up to 256 MSI interrupt numbers for + multi-function devices. + + Say Y here if you want to enable Gen3 PCIe controller support on + MediaTek SoCs. + config PCIE_TANGO_SMP8759 bool "Tango SMP8759 PCIe controller (DANGEROUS)" depends on ARCH_TANGO && PCI_MSI && OF
drivers/pci/controller/Makefile +1 -0
@@ -27,6 +27,7 @@ obj-$(CONFIG_PCIE_ROCKCHIP) += pcie-rockchip.o obj-$(CONFIG_PCIE_ROCKCHIP_EP) += pcie-rockchip-ep.o obj-$(CONFIG_PCIE_ROCKCHIP_HOST) += pcie-rockchip-host.o obj-$(CONFIG_PCIE_MEDIATEK) += pcie-mediatek.o +obj-$(CONFIG_PCIE_MEDIATEK_GEN3) += pcie-mediatek-gen3.o obj-$(CONFIG_PCIE_TANGO_SMP8759) += pcie-tango.o obj-$(CONFIG_VMD) += vmd.o obj-$(CONFIG_PCIE_BRCMSTB) += pcie-brcmstb.o
drivers/pci/controller/pcie-mediatek-gen3.c new file +1039 -0
1,039 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [v5,2/3] PCI: mediatek-gen3: Add MediaTek Gen3 driver for MT8192
Quoted 9 lines · Jianjun Wang
MediaTek's PCIe host controller has three generation HWs, the new generation HW is an individual bridge, it supports Gen3 speed and up to 256 MSI interrupt numbers for multi-function devices.
Add support for new Gen3 controller which can be found on MT8192.
Signed-off-by Jianjun Wang <[email protected]>Acked-by Ryder Lee <[email protected]>
FWIW, I looked at Rob and Bjorn's comments on v4, and they seem to have been addressed (with one small nit highlighted below).
Quoted 22 lines
---
This patch dependents on "PCI: Exportpci_pio_to_address()for module use"[1] to build as a kernel module.This interface will be used by PCI host drivers for PIO translation, export it to support compiling those drivers as kernel modules.
[1]http://lists.infradead.org/pipermail/linux-mediatek/2020-December/019504.html
--- drivers/pci/controller/Kconfig | 13 + drivers/pci/controller/Makefile | 1 + drivers/pci/controller/pcie-mediatek-gen3.c | 1039 +++++++++++++++++++ 3 files changed, 1053 insertions(+) create mode 100644 drivers/pci/controller/pcie-mediatek-gen3.c[snip]
drivers/pci/controller/pcie-mediatek-gen3.c new file +0
-0
drivers/pci/controller/pcie-mediatek-gen3.c line 216
Quoted 9 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +8
-0@@ -0,0 +1,1039 @@ +static int mtk_pcie_set_trans_table (struct mtk_pcie_port *port, +resource_size_t cpu_addr, +resource_size_t pci_addr, +resource_size_t size, +unsigned long type,int num) +{ +void __iomem *table; +u32 val =0 ;
You don't need to init val to 0.
Quoted 46 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +46
-0+ +if (num >=PCIE_MAX_TRANS_TABLES ) { +dev_notice (port->dev,"not enough translate table[%d] for addr: %#llx, limited to [%d]\n" , + num, (unsigned long long ) cpu_addr, +PCIE_MAX_TRANS_TABLES ); +return -ENODEV ; + } + + table = port->base +PCIE_TRANS_TABLE_BASE_REG + + num *PCIE_ATR_TLB_SET_OFFSET ; + +writel (lower_32_bits (cpu_addr) |PCIE_ATR_SIZE (fls (size) -1 ), table); +writel (upper_32_bits (cpu_addr), table +PCIE_ATR_SRC_ADDR_MSB_OFFSET ); +writel (lower_32_bits (pci_addr), table +PCIE_ATR_TRSL_ADDR_LSB_OFFSET ); +writel (upper_32_bits (pci_addr), table +PCIE_ATR_TRSL_ADDR_MSB_OFFSET ); + +if (type ==IORESOURCE_IO ) + val =PCIE_ATR_TYPE_IO |PCIE_ATR_TLP_TYPE_IO ; +else + val =PCIE_ATR_TYPE_MEM |PCIE_ATR_TLP_TYPE_MEM ; + +writel (val, table +PCIE_ATR_TRSL_PARAM_OFFSET ); + +return 0 ; +} + +static int mtk_pcie_startup_port (struct mtk_pcie_port *port) +{ +struct resource_entry *entry; +struct pci_host_bridge *host =pci_host_bridge_from_priv (port); +unsigned int table_index =0 ; +int err; +u32 val; + +/* Set as RC mode */ + val =readl (port->base +PCIE_SETTING_REG ); + val |=PCIE_RC_MODE ; +writel (val, port->base +PCIE_SETTING_REG ); + +/* Set class code */ + val =readl (port->base +PCIE_PCI_IDS_1 ); + val &= ~GENMASK (31 ,8 ); + val |=PCI_CLASS (PCI_CLASS_BRIDGE_PCI <<8 ); +writel (val, port->base +PCIE_PCI_IDS_1 ); + +/* Assert all reset signals */
drivers/pci/controller/pcie-mediatek-gen3.c line 272
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+ val =readl (port->base +PCIE_RST_CTRL_REG ); + val |=PCIE_MAC_RSTB |PCIE_PHY_RSTB |PCIE_BRG_RSTB |PCIE_PE_RSTB ; +writel (val, port->base +PCIE_RST_CTRL_REG ); + +/* De-assert reset signals */ + val &= ~(PCIE_MAC_RSTB |PCIE_PHY_RSTB |PCIE_BRG_RSTB ); +writel (val, port->base +PCIE_RST_CTRL_REG ); + +/* Delay 100ms to wait the reference clocks become stable */ +usleep_range (100 *1000 ,120 *1000 );
Any reason not to use msleep(100)?
Quoted 63 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +62
-0+ +/* De-assert PERST# signal */ + val &= ~PCIE_PE_RSTB ; +writel (val, port->base +PCIE_RST_CTRL_REG ); + +/* Check if the link is up or not */ + err =readl_poll_timeout (port->base +PCIE_LINK_STATUS_REG , val, + !!(val &PCIE_PORT_LINKUP ),20 , +50 *USEC_PER_MSEC ); +if (err) { + val =readl (port->base +PCIE_LTSSM_STATUS_REG ); +dev_notice (port->dev,"PCIe link down, ltssm reg val: %#x\n" , + val); +return err; + } + +/* Set PCIe translation windows */ +resource_list_for_each_entry (entry, &host->windows) { +struct resource *res = entry->res; +unsigned long type =resource_type (res); +resource_size_t cpu_addr; +resource_size_t pci_addr; +resource_size_t size; +const char *range_type; + +if (type ==IORESOURCE_IO ) { + cpu_addr =pci_pio_to_address (res->start); + range_type ="IO" ; + }else if (type ==IORESOURCE_MEM ) { + cpu_addr = res->start; + range_type ="MEM" ; + }else { +continue ; + } + + pci_addr = res->start - entry->offset; + size =resource_size (res); + err =mtk_pcie_set_trans_table (port, cpu_addr, pci_addr, size, + type, table_index); +if (err) +return err; + +dev_dbg (port->dev,"set %s trans window[%d]: cpu_addr = %#llx, pci_addr = %#llx, size = %#llx\n" , + range_type, table_index, (unsigned long long ) cpu_addr, + (unsigned long long ) pci_addr, + (unsigned long long ) size); + + table_index++; + } + +return 0 ; +} + +static irq_hw_number_t mtk_pcie_msi_get_hwirq (struct msi_domain_info *info, +msi_alloc_info_t *arg) +{ +struct msi_desc *entry = arg->desc; +struct mtk_pcie_port *port = info->chip_data; +int hwirq; + +mutex_lock (&port->lock); +
drivers/pci/controller/pcie-mediatek-gen3.c
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+ hwirq =bitmap_find_free_region (port->msi_irq_in_use,PCIE_MSI_IRQS_NUM , +order_base_2 (entry->nvec_used)); +if (hwirq <0 ) { +mutex_unlock (&port->lock); +return -ENOSPC ; + } + +mutex_unlock (&port->lock); + +return hwirq;
Code is good, but I had to look twice to make sure the mutex is unlocked. Is the following marginally better?
hwirq = ...;
mutex_unlock(&port->lock);
if (hwirq < 0)
return -ENOSPC;
return hwirq;
Quoted 6 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +5
-0+} + +static void mtk_pcie_msi_handler (struct irq_desc *desc) +{ +struct mtk_pcie_msi *msi_info =irq_desc_get_handler_data (desc);
drivers/pci/controller/pcie-mediatek-gen3.c line 491
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+struct irq_chip *irqchip =irq_desc_get_chip (desc); +unsigned long msi_enable, msi_status; +unsigned int virq; +irq_hw_number_t bit, hwirq; + +chained_irq_enter (irqchip, desc); + + msi_enable =readl (msi_info->base +PCIE_MSI_ENABLE_OFFSET ); +while ((msi_status =readl (msi_info->base +PCIE_MSI_STATUS_OFFSET ))) { + msi_status &= msi_enable;
I don't know much about MSI, but what happens if you have a bit that is set in PCIE_MSI_STATUS_OFFSET register, but not in msi_enable? Sounds like you'll just spin-loop forever without acknowledging the interrupt.
Quoted 11 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+for_each_set_bit (bit, &msi_status,PCIE_MSI_IRQS_PER_SET ) { + hwirq = bit + msi_info->index *PCIE_MSI_IRQS_PER_SET ; + virq =irq_find_mapping (msi_info->domain, hwirq); +generic_handle_irq (virq); + } + } + +chained_irq_exit (irqchip, desc); +} +
drivers/pci/controller/pcie-mediatek-gen3.c line 976
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+static int __maybe_unused mtk_pcie_suspend_noirq (struct device *dev) +{ +struct mtk_pcie_port *port =dev_get_drvdata (dev); +int err; +u32 val; + +/* Trigger link to L2 state */ + err =mtk_pcie_turn_off_link (port); +if (err) { +dev_notice (port->dev,"can not enter L2 state\n" );
Rob suggested dev_error here.
(and IMHO, or lot of the other dev_notice above should probably get dev_error)
Quoted 8 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +8
-0+return err; + } + +/* Pull down the PERST# pin */ + val =readl (port->base +PCIE_RST_CTRL_REG ); + val |=PCIE_PE_RSTB ; +writel (val, port->base +PCIE_RST_CTRL_REG ); +
drivers/pci/controller/pcie-mediatek-gen3.c line 993
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +9
-0+dev_dbg (port->dev,"enter L2 state success" ); + +clk_bulk_disable_unprepare (port->num_clks, port->clks); + +phy_power_off (port->phy); + +return 0 ; +} +
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Re: [v5,2/3] PCI: mediatek-gen3: Add MediaTek Gen3 driver for MT8192
Quoted 98 lines · Nicolas Boichat
Quoted 9 lines · Jianjun Wang
MediaTek's PCIe host controller has three generation HWs, the new generation HW is an individual bridge, it supports Gen3 speed and up to 256 MSI interrupt numbers for multi-function devices.
Add support for new Gen3 controller which can be found on MT8192.
Signed-off-by Jianjun Wang <[email protected]>Acked-by Ryder Lee <[email protected]>FWIW, I looked at Rob and Bjorn's comments on v4, and they seem to have been addressed (with one small nit highlighted below).
Quoted 22 lines
---
This patch dependents on "PCI: Exportpci_pio_to_address()for module use"[1] to build as a kernel module.This interface will be used by PCI host drivers for PIO translation, export it to support compiling those drivers as kernel modules.
[1]http://lists.infradead.org/pipermail/linux-mediatek/2020-December/019504.html
--- drivers/pci/controller/Kconfig | 13 + drivers/pci/controller/Makefile | 1 + drivers/pci/controller/pcie-mediatek-gen3.c | 1039 +++++++++++++++++++ 3 files changed, 1053 insertions(+) create mode 100644 drivers/pci/controller/pcie-mediatek-gen3.c[snip]
drivers/pci/controller/pcie-mediatek-gen3.c new file +0
-0drivers/pci/controller/pcie-mediatek-gen3.c line 216
Quoted 9 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +8
-0@@ -0,0 +1,1039 @@ +static int mtk_pcie_set_trans_table (struct mtk_pcie_port *port, +resource_size_t cpu_addr, +resource_size_t pci_addr, +resource_size_t size, +unsigned long type,int num) +{ +void __iomem *table; +u32 val =0 ;You don't need to init val to 0.
Quoted 50 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +50
-0+ +if (num >=PCIE_MAX_TRANS_TABLES ) { +dev_notice (port->dev,"not enough translate table[%d] for addr: %#llx, limited to [%d]\n" , + num, (unsigned long long ) cpu_addr, +PCIE_MAX_TRANS_TABLES ); +return -ENODEV ; + } + + table = port->base +PCIE_TRANS_TABLE_BASE_REG + + num *PCIE_ATR_TLB_SET_OFFSET ; + +writel (lower_32_bits (cpu_addr) |PCIE_ATR_SIZE (fls (size) -1 ), table); +writel (upper_32_bits (cpu_addr), table +PCIE_ATR_SRC_ADDR_MSB_OFFSET ); +writel (lower_32_bits (pci_addr), table +PCIE_ATR_TRSL_ADDR_LSB_OFFSET ); +writel (upper_32_bits (pci_addr), table +PCIE_ATR_TRSL_ADDR_MSB_OFFSET ); + +if (type ==IORESOURCE_IO ) + val =PCIE_ATR_TYPE_IO |PCIE_ATR_TLP_TYPE_IO ; +else + val =PCIE_ATR_TYPE_MEM |PCIE_ATR_TLP_TYPE_MEM ; + +writel (val, table +PCIE_ATR_TRSL_PARAM_OFFSET ); + +return 0 ; +} + +static int mtk_pcie_startup_port (struct mtk_pcie_port *port) +{ +struct resource_entry *entry; +struct pci_host_bridge *host =pci_host_bridge_from_priv (port); +unsigned int table_index =0 ; +int err; +u32 val; + +/* Set as RC mode */ + val =readl (port->base +PCIE_SETTING_REG ); + val |=PCIE_RC_MODE ; +writel (val, port->base +PCIE_SETTING_REG ); + +/* Set class code */ + val =readl (port->base +PCIE_PCI_IDS_1 ); + val &= ~GENMASK (31 ,8 ); + val |=PCI_CLASS (PCI_CLASS_BRIDGE_PCI <<8 ); +writel (val, port->base +PCIE_PCI_IDS_1 ); + +/* Assert all reset signals */ + val =readl (port->base +PCIE_RST_CTRL_REG ); + val |=PCIE_MAC_RSTB |PCIE_PHY_RSTB |PCIE_BRG_RSTB |PCIE_PE_RSTB ; +writel (val, port->base +PCIE_RST_CTRL_REG ); +
Quoted 8 lines
drivers/pci/controller/pcie-mediatek-gen3.c line 272
Quoted 6 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +6
-0+/* De-assert reset signals */ + val &= ~(PCIE_MAC_RSTB |PCIE_PHY_RSTB |PCIE_BRG_RSTB ); +writel (val, port->base +PCIE_RST_CTRL_REG ); + +/* Delay 100ms to wait the reference clocks become stable */ +usleep_range (100 *1000 ,120 *1000 );Any reason not to use msleep(100)?
No special reasons, but it seems the msleep() should be used when the sleep time is more than 20ms (base on Documentation/timers/timers-howto.rst).
I will replace to msleep(100) in the next version, thanks for your review.
Quoted 78 lines
Quoted 64 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +62
-0+ +/* De-assert PERST# signal */ + val &= ~PCIE_PE_RSTB ; +writel (val, port->base +PCIE_RST_CTRL_REG ); + +/* Check if the link is up or not */ + err =readl_poll_timeout (port->base +PCIE_LINK_STATUS_REG , val, + !!(val &PCIE_PORT_LINKUP ),20 , +50 *USEC_PER_MSEC ); +if (err) { + val =readl (port->base +PCIE_LTSSM_STATUS_REG ); +dev_notice (port->dev,"PCIe link down, ltssm reg val: %#x\n" , + val); +return err; + } + +/* Set PCIe translation windows */ +resource_list_for_each_entry (entry, &host->windows) { +struct resource *res = entry->res; +unsigned long type =resource_type (res); +resource_size_t cpu_addr; +resource_size_t pci_addr; +resource_size_t size; +const char *range_type; + +if (type ==IORESOURCE_IO ) { + cpu_addr =pci_pio_to_address (res->start); + range_type ="IO" ; + }else if (type ==IORESOURCE_MEM ) { + cpu_addr = res->start; + range_type ="MEM" ; + }else { +continue ; + } + + pci_addr = res->start - entry->offset; + size =resource_size (res); + err =mtk_pcie_set_trans_table (port, cpu_addr, pci_addr, size, + type, table_index); +if (err) +return err; + +dev_dbg (port->dev,"set %s trans window[%d]: cpu_addr = %#llx, pci_addr = %#llx, size = %#llx\n" , + range_type, table_index, (unsigned long long ) cpu_addr, + (unsigned long long ) pci_addr, + (unsigned long long ) size); + + table_index++; + } + +return 0 ; +} + +static irq_hw_number_t mtk_pcie_msi_get_hwirq (struct msi_domain_info *info, +msi_alloc_info_t *arg) +{ +struct msi_desc *entry = arg->desc; +struct mtk_pcie_port *port = info->chip_data; +int hwirq; + +mutex_lock (&port->lock); +drivers/pci/controller/pcie-mediatek-gen3.c
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+ hwirq =bitmap_find_free_region (port->msi_irq_in_use,PCIE_MSI_IRQS_NUM , +order_base_2 (entry->nvec_used)); +if (hwirq <0 ) { +mutex_unlock (&port->lock); +return -ENOSPC ; + } + +mutex_unlock (&port->lock); + +return hwirq;Code is good, but I had to look twice to make sure the mutex is unlocked. Is the following marginally better?
Quoted 8 lines
hwirq = ...;
mutex_unlock(&port->lock);if (hwirq < 0)
return-ENOSPC;return hwirq;
Impressive, I will fix it in the next version, and I think the hwirq can be returned directly since it will be a negative value if bitmap_find_free_region is failed. The code will be like the following:
hwirq = ...;
mutex_unlock(&port->lock);
return hwirq;
Quoted 12 lines
Quoted 12 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+} + +static void mtk_pcie_msi_handler (struct irq_desc *desc) +{ +struct mtk_pcie_msi *msi_info =irq_desc_get_handler_data (desc); +struct irq_chip *irqchip =irq_desc_get_chip (desc); +unsigned long msi_enable, msi_status; +unsigned int virq; +irq_hw_number_t bit, hwirq; +
Quoted 8 lines
drivers/pci/controller/pcie-mediatek-gen3.c line 491
Quoted 5 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +5
-0+chained_irq_enter (irqchip, desc); + + msi_enable =readl (msi_info->base +PCIE_MSI_ENABLE_OFFSET ); +while ((msi_status =readl (msi_info->base +PCIE_MSI_STATUS_OFFSET ))) { + msi_status &= msi_enable;I don't know much about MSI, but what happens if you have a bit that is set in
PCIE_MSI_STATUS_OFFSETregister, but not inmsi_enable?
If the bit that in PCIE_MSI_STATUS_OFFSET register is set but not in msi_enable, it must be an abnormal usage of MSI or something goes wrong, it should be ignored in case we can not find the corresponding handler.
Quoted 2 lines
Sounds like you'll just spin-loop forever without acknowledging the interrupt.
The interrupt will be acknowledged in the irq_ack callback of mtk_msi_irq_chip, which belongs to the msi_domain.
Quoted 25 lines
Quoted 12 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+for_each_set_bit (bit, &msi_status,PCIE_MSI_IRQS_PER_SET ) { + hwirq = bit + msi_info->index *PCIE_MSI_IRQS_PER_SET ; + virq =irq_find_mapping (msi_info->domain, hwirq); +generic_handle_irq (virq); + } + } + +chained_irq_exit (irqchip, desc); +} +drivers/pci/controller/pcie-mediatek-gen3.c line 976
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+static int __maybe_unused mtk_pcie_suspend_noirq (struct device *dev) +{ +struct mtk_pcie_port *port =dev_get_drvdata (dev); +int err; +u32 val; + +/* Trigger link to L2 state */ + err =mtk_pcie_turn_off_link (port); +if (err) { +dev_notice (port->dev,"can not enter L2 state\n" );Rob suggested
dev_errorhere.
Quoted 1 line
(and IMHO, or lot of the other
dev_noticeabove should probably getdev_error)
I will replace to dev_err, thanks for your review.
Quoted 24 lines
Quoted 9 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +8
-0+return err; + } + +/* Pull down the PERST# pin */ + val =readl (port->base +PCIE_RST_CTRL_REG ); + val |=PCIE_PE_RSTB ; +writel (val, port->base +PCIE_RST_CTRL_REG ); +drivers/pci/controller/pcie-mediatek-gen3.c line 993
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +9
-0+dev_dbg (port->dev,"enter L2 state success" ); + +clk_bulk_disable_unprepare (port->num_clks, port->clks); + +phy_power_off (port->phy); + +return 0 ; +} +_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Re: [v5,2/3] PCI: mediatek-gen3: Add MediaTek Gen3 driver for MT8192
Quoted 40 lines · Jianjun Wang
Quoted 25 lines · Nicolas Boichat
Quoted 10 lines · Jianjun Wang
[snip]
drivers/pci/controller/pcie-mediatek-gen3.c new file +9
-0+static irq_hw_number_t mtk_pcie_msi_get_hwirq (struct msi_domain_info *info, +msi_alloc_info_t *arg) +{ +struct msi_desc *entry = arg->desc; +struct mtk_pcie_port *port = info->chip_data; +int hwirq; + +mutex_lock (&port->lock); +drivers/pci/controller/pcie-mediatek-gen3.c line 436
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+ hwirq =bitmap_find_free_region (port->msi_irq_in_use,PCIE_MSI_IRQS_NUM , +order_base_2 (entry->nvec_used)); +if (hwirq <0 ) { +mutex_unlock (&port->lock); +return -ENOSPC ; + } + +mutex_unlock (&port->lock); + +return hwirq;Code is good, but I had to look twice to make sure the mutex is unlocked. Is the following marginally better?
Quoted 8 lines
hwirq = ...;
mutex_unlock(&port->lock);if (hwirq < 0)
return-ENOSPC;return hwirq;
Impressive, I will fix it in the next version, and I think the hwirq can be returned directly since it will be a negative value if
bitmap_find_free_regionis failed. The code will be like the following:
Quoted 5 lines
hwirq = ...;
mutex_unlock(&port->lock);return hwirq;
SG, as long as you're okay with returning -ENOMEM instead of -ENOSPC.
But now I'm having doubt if negative return values are ok, as irq_hw_number_t is unsigned long.
msi_domain_alloc
(https://elixir.bootlin.com/linux/latest/source/kernel/irq/msi.c#L143)
uses it to call irq_find_mapping
(https://elixir.bootlin.com/linux/latest/source/kernel/irq/irqdomain.c#L882)
without check, and I'm not convinced irq_find_mapping will error out gracefully...
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+} + +static void mtk_pcie_msi_handler (struct irq_desc *desc) +{ +struct mtk_pcie_msi *msi_info =irq_desc_get_handler_data (desc); +struct irq_chip *irqchip =irq_desc_get_chip (desc); +unsigned long msi_enable, msi_status; +unsigned int virq; +irq_hw_number_t bit, hwirq; +Quoted 8 lines
drivers/pci/controller/pcie-mediatek-gen3.c line 491
Quoted 5 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +5
-0+chained_irq_enter (irqchip, desc); + + msi_enable =readl (msi_info->base +PCIE_MSI_ENABLE_OFFSET ); +while ((msi_status =readl (msi_info->base +PCIE_MSI_STATUS_OFFSET ))) { + msi_status &= msi_enable;I don't know much about MSI, but what happens if you have a bit that is set in
PCIE_MSI_STATUS_OFFSETregister, but not inmsi_enable?If the bit that in
PCIE_MSI_STATUS_OFFSETregister is set but not inmsi_enable, it must be an abnormal usage of MSI or something goes wrong, it should be ignored in case we can not find the corresponding handler.
Quoted 5 lines
Quoted 2 lines
Sounds like you'll just spin-loop forever without acknowledging the interrupt.
The interrupt will be acknowledged in the
irq_ackcallback ofmtk_msi_irq_chip, which belongs to themsi_domain.
Let's try to go through it (and please explain to me if I get this wrong).
Say we have:
msi_enable = [PCIE_MSI_ENABLE_OFFSET] = 0x1;
while loop:
msi_status = [PCIE_MSI_STATUS_OFFSET] = 0x3;msi_status &= msi_enable => msi_status = 0x3 & 0x1 = 0x1; for_each_set_bit(msi_status) {
do something that presumably will disable the MSI interrupt status?
}
(next loop iteration)
msi_status = [PCIE_MSI_STATUS_OFFSET] = 0x2; msi_status &= msi_enable => msi_status = 0x2 & 0x1 = 0x0; for_each_set_bit(msi_status) => does nothing.
msi_status = [PCIE_MSI_STATUS_OFFSET] = 0x2;
(infinite loop)
Basically, I'm wondering if you should replace the while condition statement with:
while ((msi_status = readl(msi_info->base + PCIE_MSI_STATUS_OFFSET) & msi_enable))
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek
Re: [v5,2/3] PCI: mediatek-gen3: Add MediaTek Gen3 driver for MT8192
Quoted 52 lines · Nicolas Boichat
Quoted 40 lines · Jianjun Wang
Quoted 25 lines · Nicolas Boichat
Quoted 10 lines · Jianjun Wang
[snip]
drivers/pci/controller/pcie-mediatek-gen3.c new file +9
-0+static irq_hw_number_t mtk_pcie_msi_get_hwirq (struct msi_domain_info *info, +msi_alloc_info_t *arg) +{ +struct msi_desc *entry = arg->desc; +struct mtk_pcie_port *port = info->chip_data; +int hwirq; + +mutex_lock (&port->lock); +drivers/pci/controller/pcie-mediatek-gen3.c line 436
Quoted 10 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+ hwirq =bitmap_find_free_region (port->msi_irq_in_use,PCIE_MSI_IRQS_NUM , +order_base_2 (entry->nvec_used)); +if (hwirq <0 ) { +mutex_unlock (&port->lock); +return -ENOSPC ; + } + +mutex_unlock (&port->lock); + +return hwirq;Code is good, but I had to look twice to make sure the mutex is unlocked. Is the following marginally better?
Quoted 8 lines
hwirq = ...;
mutex_unlock(&port->lock);if (hwirq < 0)
return-ENOSPC;return hwirq;
Impressive, I will fix it in the next version, and I think the hwirq can be returned directly since it will be a negative value if
bitmap_find_free_regionis failed. The code will be like the following:Quoted 5 lines
hwirq = ...;
mutex_unlock(&port->lock);return hwirq;
SG, as long as you're okay with returning
-ENOMEMinstead of-ENOSPC.But now I'm having doubt if negative return values are ok, as
irq_hw_number_tis unsigned long.
Quoted 7 lines
msi_domain_alloc
(https://elixir.bootlin.com/linux/latest/source/kernel/irq/msi.c#L143)
uses it to callirq_find_mapping
(https://elixir.bootlin.com/linux/latest/source/kernel/irq/irqdomain.c#L882)
without check, and I'm not convincedirq_find_mappingwill error out gracefully...
I see, it seems the msi_domain_alloc function assume the get_hwirq callback always success, maybe I should allocate the real hwirq in the msi_prepare
(https://elixir.bootlin.com/linux/latest/source/kernel/irq/msi.c#L304)
and set it to arg->hwirq, and override the set_desc
(https://elixir.bootlin.com/linux/latest/source/drivers/pci/msi.c#L1405)
to prevent the modify of arg->hwirq.
Quoted 35 lines
Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +10
-0+} + +static void mtk_pcie_msi_handler (struct irq_desc *desc) +{ +struct mtk_pcie_msi *msi_info =irq_desc_get_handler_data (desc); +struct irq_chip *irqchip =irq_desc_get_chip (desc); +unsigned long msi_enable, msi_status; +unsigned int virq; +irq_hw_number_t bit, hwirq; +Quoted 8 lines
drivers/pci/controller/pcie-mediatek-gen3.c line 491
Quoted 5 lines
drivers/pci/controller/pcie-mediatek-gen3.c new file +5
-0+chained_irq_enter (irqchip, desc); + + msi_enable =readl (msi_info->base +PCIE_MSI_ENABLE_OFFSET ); +while ((msi_status =readl (msi_info->base +PCIE_MSI_STATUS_OFFSET ))) { + msi_status &= msi_enable;I don't know much about MSI, but what happens if you have a bit that is set in
PCIE_MSI_STATUS_OFFSETregister, but not inmsi_enable?If the bit that in
PCIE_MSI_STATUS_OFFSETregister is set but not inmsi_enable, it must be an abnormal usage of MSI or something goes wrong, it should be ignored in case we can not find the corresponding handler.Quoted 5 lines
Quoted 2 lines
Sounds like you'll just spin-loop forever without acknowledging the interrupt.
The interrupt will be acknowledged in the
irq_ackcallback ofmtk_msi_irq_chip, which belongs to themsi_domain.Let's try to go through it (and please explain to me if I get this wrong).
Say we have:
Quoted 8 lines
msi_enable= [PCIE_MSI_ENABLE_OFFSET] =0x1;while loop:
msi_status= [PCIE_MSI_STATUS_OFFSET] =0x3;msi_status&=msi_enable=>msi_status=0x3&0x1=0x1;for_each_set_bit(msi_status){
do something that presumably will disable the MSI interrupt status?
Yes, the corresponding interrupt status will be cleared.
Quoted 7 lines
}
(next loop iteration)
msi_status= [PCIE_MSI_STATUS_OFFSET] =0x2;msi_status&=msi_enable=>msi_status=0x2&0x1=0x0;for_each_set_bit(msi_status)=> does nothing.
Quoted 9 lines
msi_status= [PCIE_MSI_STATUS_OFFSET] =0x2;
(infinite loop)Basically, I'm wondering if you should replace the while condition statement with:
while ((
msi_status=readl(msi_info->base + PCIE_MSI_STATUS_OFFSET)&msi_enable))
Yes, it will be a dead loop if we receive an abnormal interrupt status, I will fix it in the next version, thanks for your kindly review.
Quoted 4 lines
_______________________________________________
Linux-mediatek mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-mediatek