Linux RISC-V AIA Support
Subsystems (6)
v4 cover Linux RISC-V AIA Support
The RISC-V AIA specification is now frozen as-per the RISC-V international process. The latest frozen specifcation can be found at:
https://github.com/riscv/riscv-aia/releases/download/1.0-RC1/riscv-interrupts-1.0-RC1.pdf
At a high-level, the AIA specification adds three things:
1) AIA CSRs
- Improved local interrupt support
2) Incoming Message Signaled Interrupt Controller (IMSIC)
- Per-HART MSI controller
- Support MSI virtualization
- Support IPI along with virtualization
3) Advanced Platform-Level Interrupt Controller (APLIC)
- Wired interrupt controller
- In MSI-mode, converts wired interrupt into MSIs (i.e. MSI generator)
- In Direct-mode, injects external interrupts directly into HARTs
For an overview of the AIA specification, refer the recent AIA virtualization talk at KVM Forum 2022:
https://static.sched.com/hosted_files/kvmforum2022/a1/AIA_Virtualization_in_KVM_RISCV_final.pdf
https://www.youtube.com/watch?v=r071dL8Z0yo
The PATCH2 of this series conflicts with the "irqchip/riscv-intc: Add ACPI support" patch of the "Add basic ACPI support for RISC-V" series hence this series is based upon the "Add basic ACPI support for RISC-V" series. (Refer, https://lore.kernel.org/lkml/[email protected]/)
To test this series, use QEMU v7.2 (or higher) and OpenSBI v1.2 (or higher).
These patches can also be found in the riscv_aia_v4 branch at:
https://github.com/avpatel/linux.git
Changes since v3:
- Rebased on Linux-6.4-rc6
- Droped PATCH2 of v3 series instead we now set FWNODE_FLAG_BEST_EFFORT via IRQCHIP_DECLARE()
- Extend riscv_fw_parent_hartid() to support both DT and ACPI in PATCH1
- Extend iommu_dma_compose_msi_msg() instead of adding iommu_dma_select_msi() in PATCH6
- Addressed Conor's comments in PATCH3
- Addressed Conor's and Rob's comments in PATCH7
Changes since v2:
- Rebased on Linux-6.4-rc1
- Addressed Rob's comments on DT bindings patches 4 and 8.
- Addessed Marc's comments on IMSIC driver PATCH5
- Replaced use of OF apis in APLIC and IMSIC drivers with FWNODE apis this makes both drivers easily portable for ACPI support. This also removes unnecessary indirection from the APLIC and IMSIC drivers.
- PATCH1 is a new patch for portability with ACPI support
- PATCH2 is a new patch to fix probing in APLIC drivers for APLIC-only systems.
- PATCH7 is a new patch which addresses the IOMMU DMA domain issues pointed out by SiFive
Changes since v1:
- Rebased on Linux-6.2-rc2
- Addressed comments on IMSIC DT bindings for PATCH4
- Use raw_spin_lock_irqsave() on ids_lock for PATCH5
- Improved MMIO alignment checks in PATCH5 to allow MMIO regions with holes.
- Addressed comments on APLIC DT bindings for PATCH6
- Fixed warning splat in aplic_msi_write_msg() caused by zeroed MSI message in PATCH7
- Dropped DT property riscv,slow-ipi instead will have module parameter in future.
Anup Patel (10):
RISC-V: Add riscv_fw_parent_hartid() function irqchip/riscv-intc: Add support for RISC-V AIA
dt-bindings: interrupt-controller: Add RISC-V incoming MSI controller
irqchip: Add RISC-V incoming MSI controller driver irqchip/riscv-imsic: Add support for PCI MSI irqdomain irqchip/riscv-imsic: Improve IOMMU DMA support
dt-bindings: interrupt-controller: Add RISC-V advanced PLIC
irqchip: Add RISC-V advanced PLIC driver
RISC-V: Select APLIC and IMSIC drivers
MAINTAINERS: Add entry for RISC-V AIA drivers
.../interrupt-controller/riscv,aplic.yaml | 169 +++ .../interrupt-controller/riscv,imsics.yaml | 172 +++ MAINTAINERS | 12 + arch/riscv/Kconfig | 2 + arch/riscv/include/asm/processor.h | 3 + arch/riscv/kernel/cpu.c | 16 + drivers/iommu/dma-iommu.c | 24 +- drivers/irqchip/Kconfig | 20 +- drivers/irqchip/Makefile | 2 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++ drivers/irqchip/irq-riscv-imsic.c | 1076 +++++++++++++++++ drivers/irqchip/irq-riscv-intc.c | 36 +- include/linux/irqchip/riscv-aplic.h | 119 ++ include/linux/irqchip/riscv-imsic.h | 86 ++ 14 files changed, 2492 insertions(+), 10 deletions(-) create mode 100644 Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml create mode 100644 Documentation/devicetree/bindings/interrupt-controller/riscv,imsics.yaml create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 drivers/irqchip/irq-riscv-imsic.c create mode 100644 include/linux/irqchip/riscv-aplic.h create mode 100644 include/linux/irqchip/riscv-imsic.h
Signature · 1 line
2.34.1
PATCH v4 1/10 RISC-V: Add riscv_fw_parent_hartid() function
We add common riscv_fw_parent_hartid() which help device drivers to get parent hartid of the INTC (i.e. local interrupt controller) fwnode. This should work for both DT and ACPI.
--- arch/riscv/include/asm/processor.h | 3 +++ arch/riscv/kernel/cpu.c | 16 ++++++++++++++++ 2 files changed, 19 insertions(+)
arch/riscv/include/asm/processor.h +3 -0
@@ -77,6 +77,9 @@ struct device_node;int riscv_of_processor_hartid (struct device_node *node,unsigned long *hartid);int riscv_of_parent_hartid (struct device_node *node,unsigned long *hartid); +struct fwnode_handle ; +int riscv_fw_parent_hartid (struct fwnode_handle *node,unsigned long *hartid); +extern void riscv_fill_hwcap (void );extern int arch_dup_task_struct (struct task_struct *dst,struct task_struct *src);
arch/riscv/kernel/cpu.c +16 -0
@@ -73,6 +73,22 @@ int riscv_of_parent_hartid(struct device_node *node, unsigned long *hartid)return -1 ; } +/* Find hart ID of the CPU fwnode under which given fwnode falls. */ +int riscv_fw_parent_hartid (struct fwnode_handle *node,unsigned long *hartid) +{ +int rc; +u64 temp; + +if (!is_of_node (node)) { + rc =fwnode_property_read_u64_array (node,"hartid" , &temp,1 ); +if (!rc) + *hartid = temp; + }else + rc =riscv_of_parent_hartid (to_of_node (node), hartid); + +return rc; +} + DEFINE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);unsigned long riscv_cached_mvendorid (unsigned int cpu_id)
Signature · 1 line
2.34.1
PATCH v4 2/10 irqchip/riscv-intc: Add support for RISC-V AIA
In reply to Anup PatelThe RISC-V advanced interrupt architecture (AIA) extends the per-HART local interrupts in following ways:
1. Minimum 64 local interrupts for both RV32 and RV64
2. Ability to process multiple pending local interrupts in same interrupt handler
3. Priority configuration for each local interrupts
4. Special CSRs to configure/access the per-HART MSI controller
This patch adds support for RISC-V AIA in the RISC-V intc driver.
--- drivers/irqchip/irq-riscv-intc.c | 36 ++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-)
drivers/irqchip/irq-riscv-intc.c +30 -6
@@ -17,6 +17,7 @@#include <linux/module.h> #include <linux/of.h> #include <linux/smp.h> +#include <asm/hwcap.h> static struct irq_domain *intc_domain;
@@ -30,6 +31,15 @@ static asmlinkage void riscv_intc_irq(struct pt_regs *regs)generic_handle_domain_irq (intc_domain, cause); } +static asmlinkage void riscv_intc_aia_irq(struct pt_regs *regs) +{ +unsigned long topi; + +while ((topi =csr_read (CSR_TOPI ))) +generic_handle_domain_irq (intc_domain, + topi >>TOPI_IID_SHIFT ); +} +/* * On RISC-V systems local interrupts are masked or unmasked by writing * the SIE (Supervisor Interrupt Enable) CSR. As CSRs can only be written
@@ -39,12 +49,18 @@ static asmlinkage void riscv_intc_irq(struct pt_regs *regs)static void riscv_intc_irq_mask (struct irq_data *d) { -csr_clear (CSR_IE ,BIT (d->hwirq)); +if (d->hwirq <BITS_PER_LONG ) +csr_clear (CSR_IE ,BIT (d->hwirq)); +else +csr_clear (CSR_IEH ,BIT (d->hwirq -BITS_PER_LONG )); }static void riscv_intc_irq_unmask (struct irq_data *d) { -csr_set (CSR_IE ,BIT (d->hwirq)); +if (d->hwirq <BITS_PER_LONG ) +csr_set (CSR_IE ,BIT (d->hwirq)); +else +csr_set (CSR_IEH ,BIT (d->hwirq -BITS_PER_LONG )); }static void riscv_intc_irq_eoi (struct irq_data *d)
@@ -115,16 +131,22 @@ static struct fwnode_handle *riscv_intc_hwnode(void)static int __init riscv_intc_init_common (struct fwnode_handle *fn) { -int rc; +int rc, nr_irqs =BITS_PER_LONG ; + +if (riscv_isa_extension_available (NULL , SxAIA) &&BITS_PER_LONG ==32 ) + nr_irqs = nr_irqs *2 ; - intc_domain =irq_domain_create_linear (fn,BITS_PER_LONG , + intc_domain =irq_domain_create_linear (fn, nr_irqs, &riscv_intc_domain_ops,NULL );if (!intc_domain) {pr_err ("unable to add IRQ domain\n" );return -ENXIO ; } - rc =set_handle_irq (&riscv_intc_irq); +if (riscv_isa_extension_available (NULL , SxAIA)) + rc =set_handle_irq (&riscv_intc_aia_irq); +else + rc =set_handle_irq (&riscv_intc_irq);if (rc) {pr_err ("failed to set irq handler\n" );return rc;
@@ -132,7 +154,9 @@ static int __init riscv_intc_init_common(struct fwnode_handle *fn)riscv_set_intc_hwnode_fn (riscv_intc_hwnode); -pr_info ("%d local interrupts mapped\n" ,BITS_PER_LONG ); +pr_info ("%d local interrupts mapped%s\n" , + nr_irqs, (riscv_isa_extension_available (NULL , SxAIA)) ? +" using AIA" :"" );return 0 ; }
Signature · 1 line
2.34.1
PATCH v4 3/10 dt-bindings: interrupt-controller: Add RISC-V incoming MSI controller
In reply to Anup PatelWe add DT bindings document for the RISC-V incoming MSI controller (IMSIC) defined by the RISC-V advanced interrupt architecture (AIA) specification.
--- .../interrupt-controller/riscv,imsics.yaml | 172 ++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 Documentation/devicetree/bindings/interrupt-controller/riscv,imsics.yaml
Documentation/devicetree/bindings/interrupt-controller/riscv,imsics.yaml new file +172 -0
@@ -0,0 +1,172 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/interrupt-controller/riscv,imsics.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: RISC-V Incoming MSI Controller (IMSIC) + +maintainers: + - Anup Patel <[email protected]> + +description: | + The RISC-V advanced interrupt architecture (AIA) defines a per-CPU incoming + MSI controller (IMSIC) for handling MSIs in a RISC-V platform. The RISC-V + AIA specification can be found at https://github.com/riscv/riscv-aia. + + The IMSIC is a per-CPU (or per-HART) device with separate interrupt file + for each privilege level (machine or supervisor). The configuration of + a IMSIC interrupt file is done using AIA CSRs and it also has a 4KB MMIO + space to receive MSIs from devices. Each IMSIC interrupt file supports a + fixed number of interrupt identities (to distinguish MSIs from devices) + which is same for given privilege level across CPUs (or HARTs). + + The device tree of a RISC-V platform will have one IMSIC device tree node + for each privilege level (machine or supervisor) which collectively describe + IMSIC interrupt files at that privilege level across CPUs (or HARTs). + + The arrangement of IMSIC interrupt files in MMIO space of a RISC-V platform + follows a particular scheme defined by the RISC-V AIA specification. A IMSIC + group is a set of IMSIC interrupt files co-located in MMIO space and we can + have multiple IMSIC groups (i.e. clusters, sockets, chiplets, etc) in a + RISC-V platform. The MSI target address of a IMSIC interrupt file at given + privilege level (machine or supervisor) encodes group index, HART index, + and guest index (shown below). + + XLEN-1 > (HART Index MSB) 12 0 + | | | | + ------------------------------------------------------------- + |xxxxxx|Group Index|xxxxxxxxxxx|HART Index|Guest Index| 0 | + ------------------------------------------------------------- + +allOf: + - $ref: /schemas/interrupt-controller.yaml# + - $ref: /schemas/interrupt-controller/msi-controller.yaml# + +properties: + compatible: + items: + - enum: + - qemu,imsics + - const: riscv,imsics + + reg: + minItems: 1 + maxItems: 16384 + description: + Base address of each IMSIC group. + + interrupt-controller: true + + "#interrupt-cells": + const: 0 + + msi-controller: true + + "#msi-cells": + const: 0 + + interrupts-extended: + minItems: 1 + maxItems: 16384 + description: + This property represents the set of CPUs (or HARTs) for which given + device tree node describes the IMSIC interrupt files. Each node pointed + to should be a riscv,cpu-intc node, which has a CPU node (i.e. RISC-V + HART) as parent. + + riscv,num-ids: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 63 + maximum: 2047 + description: + Number of interrupt identities supported by IMSIC interrupt file. + + riscv,num-guest-ids: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 63 + maximum: 2047 + description: + Number of interrupt identities are supported by IMSIC guest interrupt + file. When not specified it is assumed to be same as specified by the + riscv,num-ids property. + + riscv,guest-index-bits: + minimum: 0 + maximum: 7 + default: 0 + description: + Number of guest index bits in the MSI target address. + + riscv,hart-index-bits: + minimum: 0 + maximum: 15 + description: + Number of HART index bits in the MSI target address. When not + specified it is calculated based on the interrupts-extended property. + + riscv,group-index-bits: + minimum: 0 + maximum: 7 + default: 0 + description: + Number of group index bits in the MSI target address. + + riscv,group-index-shift: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 0 + maximum: 55 + default: 24 + description: + The least significant bit position of the group index bits in the + MSI target address. + +required: + - compatible + - reg + - interrupt-controller + - msi-controller + - "#msi-cells" + - interrupts-extended + - riscv,num-ids + +unevaluatedProperties: false + +examples: + - | + // Example 1 (Machine-level IMSIC files with just one group): + + interrupt-controller@24000000 { + compatible = "qemu,imsics", "riscv,imsics"; + interrupts-extended = <&cpu1_intc 11>, + <&cpu2_intc 11>, + <&cpu3_intc 11>, + <&cpu4_intc 11>; + reg = <0x28000000 0x4000>; + interrupt-controller; + #interrupt-cells = <0>; + msi-controller; + #msi-cells = <0>; + riscv,num-ids = <127>; + }; + + - | + // Example 2 (Supervisor-level IMSIC files with two groups): + + interrupt-controller@28000000 { + compatible = "qemu,imsics", "riscv,imsics"; + interrupts-extended = <&cpu1_intc 9>, + <&cpu2_intc 9>, + <&cpu3_intc 9>, + <&cpu4_intc 9>; + reg = <0x28000000 0x2000>, /* Group0 IMSICs */ + <0x29000000 0x2000>; /* Group1 IMSICs */ + interrupt-controller; + #interrupt-cells = <0>; + msi-controller; + #msi-cells = <0>; + riscv,num-ids = <127>; + riscv,group-index-bits = <1>; + riscv,group-index-shift = <24>; + }; +...
Signature · 1 line
2.34.1
PATCH v4 4/10 irqchip: Add RISC-V incoming MSI controller driver
In reply to Anup PatelThe RISC-V advanced interrupt architecture (AIA) specification defines a new MSI controller for managing MSIs and IPIs on a RISC-V platform.
This new MSI controller is referred to as incoming message signalled interrupt controller (IMSIC) which manages MSI on per-HART (or per-CPU) basis. (For more details refer https://github.com/riscv/riscv-aia)
This patch adds an irqchip driver for RISC-V IMSIC which provides IPIs and platform MSIs to the Linux RISC-V kernel.
--- drivers/irqchip/Kconfig | 7 +- drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-imsic.c | 1028 +++++++++++++++++++++++++++ include/linux/irqchip/riscv-imsic.h | 86 +++ 4 files changed, 1121 insertions(+), 1 deletion(-) create mode 100644 drivers/irqchip/irq-riscv-imsic.c create mode 100644 include/linux/irqchip/riscv-imsic.h
drivers/irqchip/Kconfig +6 -1
@@ -30,7 +30,6 @@ config ARM_GIC_V2M config GIC_NON_BANKED bool - config ARM_GIC_V3 bool select IRQ_DOMAIN_HIERARCHY
@@ -545,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_IMSIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config EXYNOS_IRQ_COMBINER bool "Samsung Exynos IRQ combiner support" if COMPILE_TEST depends on (ARCH_EXYNOS && ARM) || COMPILE_TEST
drivers/irqchip/Makefile +1 -0
@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.o obj-$(CONFIG_IMX_INTMUX) += irq-imx-intmux.o
drivers/irqchip/irq-riscv-imsic.c new file +1028 -0
1,028 lines not on this page · raw message
include/linux/irqchip/riscv-imsic.h new file +86 -0
86 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 5/10 irqchip/riscv-imsic: Add support for PCI MSI irqdomain
In reply to Anup PatelThe Linux PCI framework requires it's own dedicated MSI irqdomain so let us create PCI MSI irqdomain as child of the IMSIC base irqdomain.
--- drivers/irqchip/Kconfig | 7 +++++ drivers/irqchip/irq-riscv-imsic.c | 49 +++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+)
drivers/irqchip/Kconfig +7 -0
@@ -550,6 +550,13 @@ config RISCV_IMSIC select IRQ_DOMAIN_HIERARCHY select GENERIC_MSI_IRQ +config RISCV_IMSIC_PCI + bool + depends on RISCV_IMSIC + depends on PCI + depends on PCI_MSI + default RISCV_IMSIC + config EXYNOS_IRQ_COMBINER bool "Samsung Exynos IRQ combiner support" if COMPILE_TEST depends on (ARCH_EXYNOS && ARM) || COMPILE_TEST
drivers/irqchip/irq-riscv-imsic.c +49 -0
79 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v4 6/10 irqchip/riscv-imsic: Improve IOMMU DMA support
In reply to Anup PatelWe have a separate RISC-V IMSIC MSI address for each CPU so changing MSI (or IRQ) affinity results in re-programming of MSI address in the PCIe (or platform) device.
Currently, the iommu_dma_prepare_msi() is called only once at the time of IRQ allocation so IOMMU DMA domain will only have mapping for one MSI page. This means iommu_dma_compose_msi_msg() called by imsic_irq_compose_msi_msg() will always use the same MSI page irrespective to target CPU MSI address. In other words, changing MSI (or IRQ) affinity for device using IOMMU DMA domain will not work.
To address the above issue, we do the following:
1) Map MSI pages for all CPUs in imsic_irq_domain_alloc() using iommu_dma_prepare_msi().
2) Extend iommu_dma_compose_msi_msg() to lookup the correct msi_page whenever the msi_page stored as iommu cookie does not match.
--- drivers/iommu/dma-iommu.c | 24 +++++++++++++++++++++--- drivers/irqchip/irq-riscv-imsic.c | 23 +++++++++++------------ 2 files changed, 32 insertions(+), 15 deletions(-)
drivers/iommu/dma-iommu.c +21 -3
@@ -1687,14 +1687,32 @@ void iommu_dma_compose_msi_msg(struct msi_desc *desc, struct msi_msg *msg)struct device *dev =msi_desc_to_dev (desc);const struct iommu_domain *domain =iommu_get_domain_for_dev (dev);const struct iommu_dma_msi_page *msi_page; +struct iommu_dma_cookie *cookie; +phys_addr_t msi_addr; - msi_page =msi_desc_get_iommu_cookie (desc); +if (!domain || !domain->iova_cookie) +return ; -if (!domain || !domain->iova_cookie ||WARN_ON (!msi_page)) + cookie = domain->iova_cookie; + msi_page =msi_desc_get_iommu_cookie (desc); +if (!msi_page || msi_page->phys != msi_addr) { + msi_addr = ((u64 )msg->address_hi <<32 ) | msg->address_lo; + msi_addr &= ~(phys_addr_t)(cookie_msi_granule (cookie) -1 ); + +msi_desc_set_iommu_cookie (desc,NULL ); +list_for_each_entry (msi_page, &cookie->msi_page_list, list) { +if (msi_page->phys == msi_addr) { +msi_desc_set_iommu_cookie (desc, msi_page); +break ; + } + } + msi_page =msi_desc_get_iommu_cookie (desc); + } +if (WARN_ON (!msi_page))return ; msg->address_hi =upper_32_bits (msi_page->iova); - msg->address_lo &=cookie_msi_granule (domain->iova_cookie) -1 ; + msg->address_lo &=cookie_msi_granule (cookie) -1 ; msg->address_lo +=lower_32_bits (msi_page->iova); }
drivers/irqchip/irq-riscv-imsic.c +11 -12
@@ -493,11 +493,18 @@ static int imsic_irq_domain_alloc(struct irq_domain *domain,int i, hwirq, err =0 ;unsigned int cpu; - err =imsic_get_cpu (&imsic->lmask, false, &cpu); -if (err) -return err; +/* Map MSI address of all CPUs */ +for_each_cpu (cpu, &imsic->lmask) { + err =imsic_cpu_page_phys (cpu,0 , &msi_addr); +if (err) +return err; + + err =iommu_dma_prepare_msi (info->desc, msi_addr); +if (err) +return err; + } - err =imsic_cpu_page_phys (cpu,0 , &msi_addr); + err =imsic_get_cpu (&imsic->lmask, false, &cpu);if (err)return err;
@@ -505,10 +512,6 @@ static int imsic_irq_domain_alloc(struct irq_domain *domain,if (hwirq <0 )return hwirq; - err =iommu_dma_prepare_msi (info->desc, msi_addr); -if (err) - goto fail; -for (i =0 ; i < nr_irqs; i++) {imsic_id_set_target (hwirq + i, cpu); irq_domain_set_info(domain, virq + i, hwirq + i,
@@ -528,10 +531,6 @@ static int imsic_irq_domain_alloc(struct irq_domain *domain, }return 0 ; - -fail: -imsic_ids_free (hwirq,get_count_order (nr_irqs)); -return err; } static void imsic_irq_domain_free(struct irq_domain *domain,
Signature · 1 line
2.34.1
Re: [PATCH v4 06/10] irqchip/riscv-imsic: Improve IOMMU DMA support
Quoted 11 lines · Anup Patel
We have a separate RISC-V IMSIC MSI address for each CPU so changing MSI (or IRQ) affinity results in re-programming of MSI address in the PCIe (or platform) device.
Currently, the
iommu_dma_prepare_msi()is called only once at the time of IRQ allocation so IOMMU DMA domain will only have mapping for one MSI page. This meansiommu_dma_compose_msi_msg()called byimsic_irq_compose_msi_msg()will always use the same MSI page irrespective to target CPU MSI address. In other words, changing MSI (or IRQ) affinity for device using IOMMU DMA domain will not work.
You didn't answer my question from last time - there seems to be no iommu driver here so why are you messing with iommu_dma_prepare_msi()?
This path is only for platforms that have IOMMU drivers that translate the MSI window. You should add this code to link the interrupt controller to the iommu driver when you introduce the iommu driver, not in this series?
And, as I said before, I'd like to NOT see new users of iommu_dma_prepare_msi() since it is a very problematic API.
This hacking of it here is not making it better :(
Jason
Re: [PATCH v4 06/10] irqchip/riscv-imsic: Improve IOMMU DMA support
Quoted 22 lines · Jason Gunthorpe
Quoted 11 lines · Anup Patel
We have a separate RISC-V IMSIC MSI address for each CPU so changing MSI (or IRQ) affinity results in re-programming of MSI address in the PCIe (or platform) device.
Currently, the
iommu_dma_prepare_msi()is called only once at the time of IRQ allocation so IOMMU DMA domain will only have mapping for one MSI page. This meansiommu_dma_compose_msi_msg()called byimsic_irq_compose_msi_msg()will always use the same MSI page irrespective to target CPU MSI address. In other words, changing MSI (or IRQ) affinity for device using IOMMU DMA domain will not work.You didn't answer my question from last time - there seems to be no iommu driver here so why are you messing with
iommu_dma_prepare_msi()?This path is only for platforms that have IOMMU drivers that translate the MSI window. You should add this code to link the interrupt controller to the iommu driver when you introduce the iommu driver, not in this series?
Quoted 4 lines
And, as I said before, I'd like to NOT see new users of
iommu_dma_prepare_msi()since it is a very problematic API.This hacking of it here is not making it better :(
I misunderstood your previous comments.
We can certainly deal with this later when the IOMMU driver is available for RISC-V. I will drop this patch in the next revision.
Regards,
Anup
Re: [PATCH v4 06/10] irqchip/riscv-imsic: Improve IOMMU DMA support
Quoted 26 lines · Anup Patel
Quoted 25 lines · Jason Gunthorpe
Quoted 11 lines · Anup Patel
We have a separate RISC-V IMSIC MSI address for each CPU so changing MSI (or IRQ) affinity results in re-programming of MSI address in the PCIe (or platform) device.
Currently, the
iommu_dma_prepare_msi()is called only once at the time of IRQ allocation so IOMMU DMA domain will only have mapping for one MSI page. This meansiommu_dma_compose_msi_msg()called byimsic_irq_compose_msi_msg()will always use the same MSI page irrespective to target CPU MSI address. In other words, changing MSI (or IRQ) affinity for device using IOMMU DMA domain will not work.You didn't answer my question from last time - there seems to be no iommu driver here so why are you messing with
iommu_dma_prepare_msi()?This path is only for platforms that have IOMMU drivers that translate the MSI window. You should add this code to link the interrupt controller to the iommu driver when you introduce the iommu driver, not in this series?
And, as I said before, I'd like to NOT see new users of
iommu_dma_prepare_msi()since it is a very problematic API.
Quoted 7 lines
Quoted 1 line
This hacking of it here is not making it better :(
I misunderstood your previous comments.
We can certainly deal with this later when the IOMMU driver is available for RISC-V. I will drop this patch in the next revision.
Not only just this patch but the calls to iommu_dma_prepare_msi() and related APIs in the prior patch too. Assume the MSI window is directly visible to DMA without translation.
When you come with an iommu driver we can discuss how best to proceed.
Thanks,
Jason
Re: [PATCH v4 06/10] irqchip/riscv-imsic: Improve IOMMU DMA support
Quoted 32 lines · Jason Gunthorpe
Quoted 30 lines · Anup Patel
Quoted 22 lines · Jason Gunthorpe
Quoted 11 lines · Anup Patel
We have a separate RISC-V IMSIC MSI address for each CPU so changing MSI (or IRQ) affinity results in re-programming of MSI address in the PCIe (or platform) device.
Currently, the
iommu_dma_prepare_msi()is called only once at the time of IRQ allocation so IOMMU DMA domain will only have mapping for one MSI page. This meansiommu_dma_compose_msi_msg()called byimsic_irq_compose_msi_msg()will always use the same MSI page irrespective to target CPU MSI address. In other words, changing MSI (or IRQ) affinity for device using IOMMU DMA domain will not work.You didn't answer my question from last time - there seems to be no iommu driver here so why are you messing with
iommu_dma_prepare_msi()?This path is only for platforms that have IOMMU drivers that translate the MSI window. You should add this code to link the interrupt controller to the iommu driver when you introduce the iommu driver, not in this series?
Quoted 4 lines
And, as I said before, I'd like to NOT see new users of
iommu_dma_prepare_msi()since it is a very problematic API.This hacking of it here is not making it better :(
I misunderstood your previous comments.
Quoted 7 lines
Quoted 3 lines
We can certainly deal with this later when the IOMMU driver is available for RISC-V. I will drop this patch in the next revision.
Not only just this patch but the calls to
iommu_dma_prepare_msi()and related APIs in the prior patch too. Assume the MSI window is directly visible to DMA without translation.
Okay, I will remove iommu_dma_xyz() usage from IMSIC driver in the next revision.
Quoted 2 lines
When you come with an iommu driver we can discuss how best to proceed.
Yes, that's better.
Regards,
Anup
PATCH v4 7/10 dt-bindings: interrupt-controller: Add RISC-V advanced PLIC
In reply to Anup PatelWe add DT bindings document for RISC-V advanced platform level interrupt controller (APLIC) defined by the RISC-V advanced interrupt architecture (AIA) specification.
--- .../interrupt-controller/riscv,aplic.yaml | 169 ++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml
Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml new file +169 -0
@@ -0,0 +1,169 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/interrupt-controller/riscv,aplic.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: RISC-V Advanced Platform Level Interrupt Controller (APLIC) + +maintainers: + - Anup Patel <[email protected]> + +description: + The RISC-V advanced interrupt architecture (AIA) defines an advanced + platform level interrupt controller (APLIC) for handling wired interrupts + in a RISC-V platform. The RISC-V AIA specification can be found at + https://github.com/riscv/riscv-aia. + + The RISC-V APLIC is implemented as hierarchical APLIC domains where all + interrupt sources connect to the root APLIC domain and a parent APLIC + domain can delegate interrupt sources to it's child APLIC domains. There + is one device tree node for each APLIC domain. + +allOf: + - $ref: /schemas/interrupt-controller.yaml# + +properties: + compatible: + items: + - enum: + - qemu,aplic + - const: riscv,aplic + + reg: + maxItems: 1 + + interrupt-controller: true + + "#interrupt-cells": + const: 2 + + interrupts-extended: + minItems: 1 + maxItems: 16384 + description: + Given APLIC domain directly injects external interrupts to a set of + RISC-V HARTS (or CPUs). Each node pointed to should be a riscv,cpu-intc + node, which has a CPU node (i.e. RISC-V HART) as parent. + + msi-parent: + description: + Given APLIC domain forwards wired interrupts as MSIs to a AIA incoming + message signaled interrupt controller (IMSIC). If both "msi-parent" and + "interrupts-extended" properties are present then it means the APLIC + domain supports both MSI mode and Direct mode in HW. In this case, the + APLIC driver has to choose between MSI mode or Direct mode. + + riscv,num-sources: + $ref: /schemas/types.yaml#/definitions/uint32 + minimum: 1 + maximum: 1023 + description: + Specifies the number of wired interrupt sources supported by this + APLIC domain. + + riscv,children: + $ref: /schemas/types.yaml#/definitions/phandle-array + minItems: 1 + maxItems: 1024 + items: + maxItems: 1 + description: + A list of child APLIC domains for the given APLIC domain. Each child + APLIC domain is assigned a child index in increasing order, with the + first child APLIC domain assigned child index 0. The APLIC domain child + index is used by firmware to delegate interrupts from the given APLIC + domain to a particular child APLIC domain. + + riscv,delegation: + $ref: /schemas/types.yaml#/definitions/phandle-array + minItems: 1 + maxItems: 1024 + items: + items: + - description: child APLIC domain phandle + - description: first interrupt number of the parent APLIC domain (inclusive) + - description: last interrupt number of the parent APLIC domain (inclusive) + description: + A interrupt delegation list where each entry is a triple consisting + of child APLIC domain phandle, first interrupt number of the parent + APLIC domain, and last interrupt number of the parent APLIC domain. + Firmware must configure interrupt delegation registers based on + interrupt delegation list. + +required: + - compatible + - reg + - interrupt-controller + - "#interrupt-cells" + - riscv,num-sources + +anyOf: + - required: + - interrupts-extended + - required: + - msi-parent + +unevaluatedProperties: false + +examples: + - | + // Example 1 (APLIC domains directly injecting interrupt to HARTs): + + interrupt-controller@c000000 { + compatible = "qemu,aplic", "riscv,aplic"; + interrupts-extended = <&cpu1_intc 11>, + <&cpu2_intc 11>, + <&cpu3_intc 11>, + <&cpu4_intc 11>; + reg = <0xc000000 0x4080>; + interrupt-controller; + #interrupt-cells = <2>; + riscv,num-sources = <63>; + riscv,children = <&aplic1>, <&aplic2>; + riscv,delegation = <&aplic1 1 63>; + }; + + aplic1: interrupt-controller@d000000 { + compatible = "qemu,aplic", "riscv,aplic"; + interrupts-extended = <&cpu1_intc 9>, + <&cpu2_intc 9>; + reg = <0xd000000 0x4080>; + interrupt-controller; + #interrupt-cells = <2>; + riscv,num-sources = <63>; + }; + + aplic2: interrupt-controller@e000000 { + compatible = "qemu,aplic", "riscv,aplic"; + interrupts-extended = <&cpu3_intc 9>, + <&cpu4_intc 9>; + reg = <0xe000000 0x4080>; + interrupt-controller; + #interrupt-cells = <2>; + riscv,num-sources = <63>; + }; + + - | + // Example 2 (APLIC domains forwarding interrupts as MSIs): + + interrupt-controller@c000000 { + compatible = "qemu,aplic", "riscv,aplic"; + msi-parent = <&imsic_mlevel>; + reg = <0xc000000 0x4000>; + interrupt-controller; + #interrupt-cells = <2>; + riscv,num-sources = <63>; + riscv,children = <&aplic3>; + riscv,delegation = <&aplic3 1 63>; + }; + + aplic3: interrupt-controller@d000000 { + compatible = "qemu,aplic", "riscv,aplic"; + msi-parent = <&imsic_slevel>; + reg = <0xd000000 0x4000>; + interrupt-controller; + #interrupt-cells = <2>; + riscv,num-sources = <63>; + }; +...
Signature · 1 line
2.34.1
Re: [PATCH v4 07/10] dt-bindings: interrupt-controller: Add RISC-V advanced PLIC
Hey Anup,
Mostly looks good, once minor comment.
Quoted 31 lines · Anup Patel
Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml new file +31
-0+ riscv,children: + $ref: /schemas/types.yaml#/definitions/phandle-array + minItems: 1 + maxItems: 1024 + items: + maxItems: 1 + description: + A list of child APLIC domains for the given APLIC domain. Each child + APLIC domain is assigned a child index in increasing order, with the + first child APLIC domain assigned child index 0. The APLIC domain child + index is used by firmware to delegate interrupts from the given APLIC + domain to a particular child APLIC domain. + + riscv,delegation: + $ref: /schemas/types.yaml#/definitions/phandle-array + minItems: 1 + maxItems: 1024 + items: + items: + - description: child APLIC domain phandle + - description: first interrupt number of the parent APLIC domain (inclusive) + - description: last interrupt number of the parent APLIC domain (inclusive) + description: + A interrupt delegation list where each entry is a triple consisting + of child APLIC domain phandle, first interrupt number of the parent + APLIC domain, and last interrupt number of the parent APLIC domain. + Firmware must configure interrupt delegation registers based on + interrupt delegation list. + +required: + - compatible
Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml line 105
Quoted 10 lines
Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml new file +10
-0+ - reg + - interrupt-controller + - "#interrupt-cells" + - riscv,num-sources + +anyOf: + - required: + - interrupts-extended + - required: + - msi-parent
Not sure if you missed this from the last version, but I asked if we needed a
dependencies:
riscv,delegate: [ riscv,children ]
IOW, I don't think it is valid to have a delegation without having children?
Otherwise,
Cheers,
Conor.
Re: [PATCH v4 07/10] dt-bindings: interrupt-controller: Add RISC-V advanced PLIC
Quoted 54 lines · Conor Dooley
Hey Anup,
Mostly looks good, once minor comment.
Quoted 31 lines · Anup Patel
Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml new file +31
-0+ riscv,children: + $ref: /schemas/types.yaml#/definitions/phandle-array + minItems: 1 + maxItems: 1024 + items: + maxItems: 1 + description: + A list of child APLIC domains for the given APLIC domain. Each child + APLIC domain is assigned a child index in increasing order, with the + first child APLIC domain assigned child index 0. The APLIC domain child + index is used by firmware to delegate interrupts from the given APLIC + domain to a particular child APLIC domain. + + riscv,delegation: + $ref: /schemas/types.yaml#/definitions/phandle-array + minItems: 1 + maxItems: 1024 + items: + items: + - description: child APLIC domain phandle + - description: first interrupt number of the parent APLIC domain (inclusive) + - description: last interrupt number of the parent APLIC domain (inclusive) + description: + A interrupt delegation list where each entry is a triple consisting + of child APLIC domain phandle, first interrupt number of the parent + APLIC domain, and last interrupt number of the parent APLIC domain. + Firmware must configure interrupt delegation registers based on + interrupt delegation list. + +required: + - compatibleDocumentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml line 105
Quoted 10 lines
Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml new file +10
-0+ - reg + - interrupt-controller + - "#interrupt-cells" + - riscv,num-sources + +anyOf: + - required: + - interrupts-extended + - required: + - msi-parentNot sure if you missed this from the last version, but I asked if we needed a
dependencies:
riscv,delegate: [ riscv,children ]
Quoted 2 lines
IOW, I don't think it is valid to have a delegation without having children?
Ahh, yes. I missed this one. I will update in the next revision.
Quoted 6 lines
Otherwise,
Reviewed-by Conor Dooley <[email protected]>Cheers,
Conor.
Regards,
Anup
PATCH v4 8/10 irqchip: Add RISC-V advanced PLIC driver
In reply to Anup PatelThe RISC-V advanced interrupt architecture (AIA) specification defines a new interrupt controller for managing wired interrupts on a RISC-V platform. This new interrupt controller is referred to as advanced platform-level interrupt controller (APLIC) which can forward wired interrupts to CPUs (or HARTs) as local interrupts OR as message signaled interrupts.
(For more details refer https://github.com/riscv/riscv-aia)
This patch adds an irqchip driver for RISC-V APLIC found on RISC-V platforms.
--- drivers/irqchip/Kconfig | 6 + drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++++++++++++++++++ include/linux/irqchip/riscv-aplic.h | 119 +++++ 4 files changed, 891 insertions(+) create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 include/linux/irqchip/riscv-aplic.h
drivers/irqchip/Kconfig +6 -0
@@ -544,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_APLIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config RISCV_IMSIC bool depends on RISCV
drivers/irqchip/Makefile +1 -0
@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic.o obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.o
drivers/irqchip/irq-riscv-aplic.c new file +765 -0
765 lines not on this page · raw message
include/linux/irqchip/riscv-aplic.h new file +119 -0
119 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 808 lines · Anup Patel
The RISC-V advanced interrupt architecture (AIA) specification defines a new interrupt controller for managing wired interrupts on a RISC-V platform. This new interrupt controller is referred to as advanced platform-level interrupt controller (APLIC) which can forward wired interrupts to CPUs (or HARTs) as local interrupts OR as message signaled interrupts.
(For more details refer https://github.com/riscv/riscv-aia)This patch adds an irqchip driver for RISC-V APLIC found on RISC-V platforms.
Signed-off-by Anup Patel <[email protected]>--- drivers/irqchip/Kconfig | 6 + drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++++++++++++++++++ include/linux/irqchip/riscv-aplic.h | 119 +++++ 4 files changed, 891 insertions(+) create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 include/linux/irqchip/riscv-aplic.hdrivers/irqchip/Kconfig +6
-0@@ -544,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_APLIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config RISCV_IMSIC bool depends on RISCVdrivers/irqchip/Makefile +1
-0@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic.o obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.odrivers/irqchip/irq-riscv-aplic.c new file +751
-0@@ -0,0 +1,765 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ + +#define pr_fmt (fmt) "riscv-aplic: " fmt +#include <linux/bitops.h> +#include <linux/cpu.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/irq.h> +#include <linux/irqchip.h> +#include <linux/irqchip/chained_irq.h> +#include <linux/irqchip/riscv-aplic.h> +#include <linux/irqchip/riscv-imsic.h> +#include <linux/irqdomain.h> +#include <linux/module.h> +#include <linux/msi.h> +#include <linux/platform_device.h> +#include <linux/smp.h> + +#define APLIC_DEFAULT_PRIORITY 1 +#define APLIC_DISABLE_IDELIVERY 0 +#define APLIC_ENABLE_IDELIVERY 1 +#define APLIC_DISABLE_ITHRESHOLD 1 +#define APLIC_ENABLE_ITHRESHOLD 0 + +struct aplic_msicfg { +phys_addr_t base_ppn; +u32 hhxs; +u32 hhxw; +u32 lhxs; +u32 lhxw; +}; + +struct aplic_idc { +unsigned int hart_index; +void __iomem *regs; +struct aplic_priv *priv; +}; + +struct aplic_priv { +struct fwnode_handle *fwnode; +u32 gsi_base; +u32 nr_irqs; +u32 nr_idcs; +void __iomem *regs; +struct irq_domain *irqdomain; +struct aplic_msicfg msicfg; +struct cpumask lmask; +}; + +static unsigned int aplic_idc_parent_irq; +static DEFINE_PER_CPU(struct aplic_idc, aplic_idcs); + +static void aplic_irq_unmask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_SETIENUM ); + +if (!priv->nr_idcs) +irq_chip_unmask_parent (d); +} + +static void aplic_irq_mask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_CLRIENUM ); + +if (!priv->nr_idcs) +irq_chip_mask_parent (d); +} + +static int aplic_set_type (struct irq_data *d,unsigned int type) +{ +u32 val =0 ; +void __iomem *sourcecfg; +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +switch (type) { +case IRQ_TYPE_NONE : + val =APLIC_SOURCECFG_SM_INACTIVE ; +break ; +case IRQ_TYPE_LEVEL_LOW : + val =APLIC_SOURCECFG_SM_LEVEL_LOW ; +break ; +case IRQ_TYPE_LEVEL_HIGH : + val =APLIC_SOURCECFG_SM_LEVEL_HIGH ; +break ; +case IRQ_TYPE_EDGE_FALLING : + val =APLIC_SOURCECFG_SM_EDGE_FALL ; +break ; +case IRQ_TYPE_EDGE_RISING : + val =APLIC_SOURCECFG_SM_EDGE_RISE ; +break ; +default : +return -EINVAL ; + } + + sourcecfg = priv->regs +APLIC_SOURCECFG_BASE ; + sourcecfg += (d->hwirq -1 ) *sizeof (u32); +writel (val, sourcecfg); + +return 0 ; +} + +static void aplic_irq_eoi (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +u32 reg_off, reg_mask; + +/* +* EOI handling only required only for level-triggered +* interrupts in APLIC MSI mode. +*/ + +if (priv->nr_idcs) +return ; + + reg_off =APLIC_CLRIP_BASE + ((d->hwirq /APLIC_IRQBITS_PER_REG ) *4 ); + reg_mask =BIT (d->hwirq %APLIC_IRQBITS_PER_REG ); +switch (irqd_get_trigger_type (d)) { +case IRQ_TYPE_LEVEL_LOW : +if (!(readl (priv->regs + reg_off) & reg_mask)) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; +case IRQ_TYPE_LEVEL_HIGH : +if (readl (priv->regs + reg_off) & reg_mask) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; + } +} + +#ifdef CONFIG_SMP +static int aplic_set_affinity (struct irq_data *d, +const struct cpumask *mask_val,bool force) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_idc *idc; +unsigned int cpu, val; +struct cpumask amask; +void __iomem *target; + +if (!priv->nr_idcs) +return irq_chip_set_affinity_parent (d, mask_val, force); + +cpumask_and (&amask, &priv->lmask, mask_val); + +if (force) + cpu =cpumask_first (&amask); +else + cpu =cpumask_any_and (&amask, cpu_online_mask); + +if (cpu >= nr_cpu_ids) +return -EINVAL ; + + idc =per_cpu_ptr (&aplic_idcs, cpu); + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +writel (val, target); + +irq_data_update_effective_affinity (d,cpumask_of (cpu)); + +return IRQ_SET_MASK_OK_DONE ; +} +#endif + +static struct irq_chip aplic_chip = { + .name ="RISC-V APLIC" , + .irq_mask = aplic_irq_mask, + .irq_unmask = aplic_irq_unmask, + .irq_set_type = aplic_set_type, + .irq_eoi = aplic_irq_eoi, +#ifdef CONFIG_SMP + .irq_set_affinity = aplic_set_affinity, +#endif + .flags =IRQCHIP_SET_TYPE_MASKED | +IRQCHIP_SKIP_SET_WAKE | +IRQCHIP_MASK_ON_SUSPEND , +}; + +static int aplic_irqdomain_translate (struct irq_fwspec *fwspec, +u32 gsi_base, +unsigned long *hwirq, +unsigned int *type) +{ +if (WARN_ON (fwspec->param_count <2 )) +return -EINVAL ; +if (WARN_ON (!fwspec->param[0 ])) +return -EINVAL ; + +/* For DT, gsi_base is always zero. */ + *hwirq = fwspec->param[0 ] - gsi_base; + *type = fwspec->param[1 ] &IRQ_TYPE_SENSE_MASK ; + +WARN_ON (*type ==IRQ_TYPE_NONE ); + +return 0 ; +} + +static int aplic_irqdomain_msi_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv =platform_msi_get_host_data (d); + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_msi_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv =platform_msi_get_host_data (domain); + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + + ret =platform_msi_device_domain_alloc (domain, virq, nr_irqs); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +/* +* APLIC does not implement irq_disable() so Linux interrupt +* subsystem will take a lazy approach for disabling an APLIC +* interrupt. This means APLIC interrupts are left unmasked +* upon system suspend and interrupts are not processed +* immediately upon system wake up. To tackle this, we disable +* the lazy approach for all APLIC interrupts. +*/ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_msi_ops = { + .translate = aplic_irqdomain_msi_translate, + .alloc = aplic_irqdomain_msi_alloc, + .free = platform_msi_device_domain_free, +}; + +static int aplic_irqdomain_idc_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv = d->host_data; + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_idc_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv = domain->host_data; + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +irq_set_affinity (virq + i, &priv->lmask); +/* See the reason described in aplic_irqdomain_msi_alloc() */ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_idc_ops = { + .translate = aplic_irqdomain_idc_translate, + .alloc = aplic_irqdomain_idc_alloc, + .free = irq_domain_free_irqs_top, +}; + +static void aplic_init_hw_irqs (struct aplic_priv *priv) +{ +int i; + +/* Disable all interrupts */ +for (i =0 ; i <= priv->nr_irqs; i +=32 ) +writel (-1U , priv->regs +APLIC_CLRIE_BASE + + (i /32 ) *sizeof (u32)); + +/* Set interrupt type and default priority for all interrupts */ +for (i =1 ; i <= priv->nr_irqs; i++) { +writel (0 , priv->regs +APLIC_SOURCECFG_BASE + + (i -1 ) *sizeof (u32)); +writel (APLIC_DEFAULT_PRIORITY , + priv->regs +APLIC_TARGET_BASE + + (i -1 ) *sizeof (u32)); + } + +/* Clear APLIC domaincfg */ +writel (0 , priv->regs +APLIC_DOMAINCFG ); +} + +static void aplic_init_hw_global (struct aplic_priv *priv) +{ +u32 val; +#ifdef CONFIG_RISCV_M_MODE +u32 valH; + +if (!priv->nr_idcs) { + val = priv->msicfg.base_ppn; + valH = (priv->msicfg.base_ppn >>32 ) & + APLIC_xMSICFGADDRH_BAPPN_MASK; + valH |= (priv->msicfg.lhxw & APLIC_xMSICFGADDRH_LHXW_MASK) + << APLIC_xMSICFGADDRH_LHXW_SHIFT; + valH |= (priv->msicfg.hhxw & APLIC_xMSICFGADDRH_HHXW_MASK) + << APLIC_xMSICFGADDRH_HHXW_SHIFT; + valH |= (priv->msicfg.lhxs & APLIC_xMSICFGADDRH_LHXS_MASK) + << APLIC_xMSICFGADDRH_LHXS_SHIFT; + valH |= (priv->msicfg.hhxs & APLIC_xMSICFGADDRH_HHXS_MASK) + << APLIC_xMSICFGADDRH_HHXS_SHIFT; +writel (val, priv->regs + APLIC_xMSICFGADDR); +writel (valH, priv->regs + APLIC_xMSICFGADDRH); + } +#endif + +/* Setup APLIC domaincfg register */ + val =readl (priv->regs +APLIC_DOMAINCFG ); + val |=APLIC_DOMAINCFG_IE ; +if (!priv->nr_idcs) + val |=APLIC_DOMAINCFG_DM ; +writel (val, priv->regs +APLIC_DOMAINCFG ); +if (readl (priv->regs +APLIC_DOMAINCFG ) != val) +pr_warn ("%pfwP: unable to write 0x%x in domaincfg\n" , + priv->fwnode, val); +} + +static void aplic_msi_write_msg (struct msi_desc *desc,struct msi_msg *msg) +{ +unsigned int group_index, hart_index, guest_index, val; +struct irq_data *d =irq_get_irq_data (desc->irq); +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_msicfg *mc = &priv->msicfg; +phys_addr_t tppn, tbppn, msg_addr; +void __iomem *target; + +/* For zeroed MSI, simply write zero into the target register */ +if (!msg->address_hi && !msg->address_lo && !msg->data) { + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); +writel (0 , target); +return ; + } + +/* Sanity check on message data */ +WARN_ON (msg->data >APLIC_TARGET_EIID_MASK ); + +/* Compute target MSI address */ + msg_addr = (((u64 )msg->address_hi) <<32 ) | msg->address_lo; + tppn = msg_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + +/* Compute target HART Base PPN */ + tbppn = tppn; + tbppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); +WARN_ON (tbppn != mc->base_ppn); + +/* Compute target group and hart indexes */ + group_index = (tppn >>APLIC_xMSICFGADDR_PPN_HHX_SHIFT (mc->hhxs)) & +APLIC_xMSICFGADDR_PPN_HHX_MASK (mc->hhxw); + hart_index = (tppn >>APLIC_xMSICFGADDR_PPN_LHX_SHIFT (mc->lhxs)) & +APLIC_xMSICFGADDR_PPN_LHX_MASK (mc->lhxw); + hart_index |= (group_index << mc->lhxw); +WARN_ON (hart_index >APLIC_TARGET_HART_IDX_MASK ); + +/* Compute target guest index */ + guest_index = tppn &APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); +WARN_ON (guest_index >APLIC_TARGET_GUEST_IDX_MASK ); + +/* Update IRQ TARGET register */ + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = (hart_index &APLIC_TARGET_HART_IDX_MASK ) + <<APLIC_TARGET_HART_IDX_SHIFT ; + val |= (guest_index &APLIC_TARGET_GUEST_IDX_MASK ) + <<APLIC_TARGET_GUEST_IDX_SHIFT ; + val |= (msg->data &APLIC_TARGET_EIID_MASK ); +writel (val, target); +} + +static int aplic_setup_msi (struct aplic_priv *priv) +{ +struct aplic_msicfg *mc = &priv->msicfg; +const struct imsic_global_config *imsic_global; + +/* +* The APLIC outgoing MSI config registers assume target MSI +* controller to be RISC-V AIA IMSIC controller. +*/ + imsic_global =imsic_get_global_config (); +if (!imsic_global) { +pr_err ("%pfwP: IMSIC global config not found\n" , + priv->fwnode); +return -ENODEV ; + } + +/* Find number of guest index bits (LHXS) */ + mc->lhxs = imsic_global->guest_index_bits; +if (APLIC_xMSICFGADDRH_LHXS_MASK < mc->lhxs) { +pr_err ("%pfwP: IMSIC guest index bits big for APLIC LHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of HART index bits (LHXW) */ + mc->lhxw = imsic_global->hart_index_bits; +if (APLIC_xMSICFGADDRH_LHXW_MASK < mc->lhxw) { +pr_err ("%pfwP: IMSIC hart index bits big for APLIC LHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of group index bits (HHXW) */ + mc->hhxw = imsic_global->group_index_bits; +if (APLIC_xMSICFGADDRH_HHXW_MASK < mc->hhxw) { +pr_err ("%pfwP: IMSIC group index bits big for APLIC HHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find first bit position of group index (HHXS) */ + mc->hhxs = imsic_global->group_index_shift; +if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) { +pr_err ("%pfwP: IMSIC group index shift should be >= %d\n" , + priv->fwnode, (2 * APLIC_xMSICFGADDR_PPN_SHIFT)); +return -EINVAL ; + } + mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT); +if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) { +pr_err ("%pfwP: IMSIC group index shift big for APLIC HHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Compute PPN base */ + mc->base_ppn = imsic_global->base_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); + +/* Use all possible CPUs as lmask */ +cpumask_copy (&priv->lmask, cpu_possible_mask); + +return 0 ; +} + +/* +* To handle an APLIC IDC interrupts, we just read the CLAIMI register +* which will return highest priority pending interrupt and clear the +* pending bit of the interrupt. This process is repeated until CLAIMI +* register return zero value. +*/ +static void aplic_idc_handle_irq (struct irq_desc *desc) +{ +struct aplic_idc *idc =this_cpu_ptr (&aplic_idcs); +struct irq_chip *chip =irq_desc_get_chip (desc); +irq_hw_number_t hw_irq; +int irq; + +chained_irq_enter (chip, desc); + +while ((hw_irq =readl (idc->regs +APLIC_IDC_CLAIMI ))) { + hw_irq = hw_irq >>APLIC_IDC_TOPI_ID_SHIFT ; + irq =irq_find_mapping (idc->priv->irqdomain, hw_irq); + +if (unlikely (irq <=0 )) +pr_warn_ratelimited ("hw_irq %lu mapping not found\n" , + hw_irq); +else +generic_handle_irq (irq); + } + +chained_irq_exit (chip, desc); +} + +static void aplic_idc_set_delivery (struct aplic_idc *idc,bool en) +{ +u32 de = (en) ?APLIC_ENABLE_IDELIVERY :APLIC_DISABLE_IDELIVERY ; +u32 th = (en) ?APLIC_ENABLE_ITHRESHOLD :APLIC_DISABLE_ITHRESHOLD ; + +/* Priority must be less than threshold for interrupt triggering */ +writel (th, idc->regs +APLIC_IDC_ITHRESHOLD ); + +/* Delivery must be set to 1 for interrupt triggering */ +writel (de, idc->regs +APLIC_IDC_IDELIVERY ); +} + +static int aplic_idc_dying_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +disable_percpu_irq (aplic_idc_parent_irq); + +return 0 ; +} + +static int aplic_idc_starting_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +enable_percpu_irq (aplic_idc_parent_irq, +irq_get_trigger_type (aplic_idc_parent_irq)); + +return 0 ; +} + +static int aplic_setup_idc (struct aplic_priv *priv) +{ +int i, j, rc, cpu, setup_count =0 ; +struct fwnode_reference_args parent; +struct irq_domain *domain; +unsigned long hartid; +struct aplic_idc *idc; +u32 val; + +/* Setup per-CPU IDC and target CPU mask */ +for (i =0 ; i < priv->nr_idcs; i++) { + rc =fwnode_property_get_reference_args (priv->fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , i, &parent); +if (rc) { +pr_warn ("%pfwP: parent irq for IDC%d not found\n" , + priv->fwnode, i); +continue ; + } + +/* +* Skip interrupts other than external interrupts for +* current privilege level. +*/ +if (parent.args[0 ] !=RV_IRQ_EXT ) +continue ; + + rc =riscv_fw_parent_hartid (parent.fwnode, &hartid); +if (rc) { +pr_warn ("%pfwP: invalid hartid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + + cpu =riscv_hartid_to_cpuid (hartid); +if (cpu <0 ) { +pr_warn ("%pfwP: invalid cpuid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + +cpumask_set_cpu (cpu, &priv->lmask); + + idc =per_cpu_ptr (&aplic_idcs, cpu); + idc->hart_index = i; + idc->regs = priv->regs +APLIC_IDC_BASE + i *APLIC_IDC_SIZE ; + idc->priv = priv; + +aplic_idc_set_delivery (idc, true); + +/* +* Boot cpu might not have APLIC hart_index = 0 so check +* and update target registers of all interrupts. +*/ +if (cpu ==smp_processor_id () && idc->hart_index) { + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +for (j =1 ; j <= priv->nr_irqs; j++) +writel (val, priv->regs +APLIC_TARGET_BASE + + (j -1 ) *sizeof (u32)); + } + + setup_count++; + } + +/* Find parent domain and register chained handler */ + domain =irq_find_matching_fwnode (riscv_get_intc_hwnode (), +DOMAIN_BUS_ANY ); +if (!aplic_idc_parent_irq && domain) { + aplic_idc_parent_irq =irq_create_mapping (domain,RV_IRQ_EXT ); +if (aplic_idc_parent_irq) { +irq_set_chained_handler (aplic_idc_parent_irq, + aplic_idc_handle_irq); + +/* +* Setup CPUHP notifier to enable IDC parent +* interrupt on all CPUs +*/ +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"irqchip/riscv/aplic:starting" , + aplic_idc_starting_cpu, + aplic_idc_dying_cpu); + } + } + +/* Fail if we were not able to setup IDC for any CPU */ +return (setup_count) ?0 : -ENODEV ; +} + +static int aplic_probe (struct platform_device *pdev) +{ +struct fwnode_handle *fwnode = pdev->dev.fwnode; +struct fwnode_reference_args parent; +struct aplic_priv *priv; +struct resource *res; +phys_addr_t pa; +int rc; + + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + priv->fwnode = fwnode; + +/* Map the MMIO registers */ + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +if (!res) { +pr_err ("%pfwP: failed to get MMIO resource\n" , fwnode); +return -EINVAL ; + } + priv->regs =devm_ioremap (&pdev->dev, res->start,resource_size (res)); +if (!priv->regs) { +pr_err ("%pfwP: failed map MMIO registers\n" , fwnode); +return -ENOMEM ; + } + +/* +* Find out GSI base number +* +* Note: DT does not define "riscv,gsi-base" property so GSI +* base is always zero for DT. +*/ + rc =fwnode_property_read_u32_array (fwnode,"riscv,gsi-base" , + &priv->gsi_base,1 ); +if (rc) + priv->gsi_base =0 ; + +/* Find out number of interrupt sources */ + rc =fwnode_property_read_u32_array (fwnode,"riscv,num-sources" , + &priv->nr_irqs,1 ); +if (rc) { +pr_err ("%pfwP: failed to get number of interrupt sources\n" , + fwnode); +return rc; + } + +/* Setup initial state APLIC interrupts */ +aplic_init_hw_irqs (priv); + +/* +* Find out number of IDCs based on parent interrupts +* +* If "msi-parent" property is present then we ignore the +* APLIC IDCs which forces the APLIC driver to use MSI mode. +*/ +if (!fwnode_property_present (fwnode,"msi-parent" )) { +while (!fwnode_property_get_reference_args (fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , priv->nr_idcs, &parent)) + priv->nr_idcs++; + } + +/* Setup IDCs or MSIs based on number of IDCs */ +if (priv->nr_idcs) + rc =aplic_setup_idc (priv); +else + rc =aplic_setup_msi (priv); +if (rc) { +pr_err ("%pfwP: failed setup %s\n" , + fwnode, priv->nr_idcs ?"IDCs" :"MSIs" ); +return rc; + } + +/* Setup global config and interrupt delivery */ +aplic_init_hw_global (priv); + +/* Create irq domain instance for the APLIC */ +if (priv->nr_idcs) + priv->irqdomain =irq_domain_create_linear ( + priv->fwnode, + priv->nr_irqs +1 , + &aplic_irqdomain_idc_ops, + priv); +else + priv->irqdomain =platform_msi_create_device_domain ( + &pdev->dev, + priv->nr_irqs +1 , + aplic_msi_write_msg, + &aplic_irqdomain_msi_ops, + priv); +if (!priv->irqdomain) { +pr_err ("%pfwP: failed to add irq domain\n" , priv->fwnode); +return -ENOMEM ; + } + +/* Advertise the interrupt controller */ +if (priv->nr_idcs) { +pr_info ("%pfwP: %d interrupts directly connected to %d CPUs\n" , + priv->fwnode, priv->nr_irqs, priv->nr_idcs); + }else { + pa = priv->msicfg.base_ppn << APLIC_xMSICFGADDR_PPN_SHIFT; +pr_info ("%pfwP: %d interrupts forwared to MSI base %pa\n" , + priv->fwnode, priv->nr_irqs, &pa); + } + +return 0 ; +} + +static const struct of_device_id aplic_match[] = { + { .compatible ="riscv,aplic" }, + {} +}; + +static struct platform_driver aplic_driver = { + .driver = { + .name ="riscv-aplic" , + .of_match_table = aplic_match, + }, + .probe = aplic_probe, +}; +builtin_platform_driver (aplic_driver); + +static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early
drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;
NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 4 lines
drivers/irqchip/irq-riscv-aplic.c new file +4
-0+of_node_clear_flag (node,OF_POPULATED ); +return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);
This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the IRQCHIP_PLATFORM_DRIVER_BEGIN() and related macros. Grep for plenty of examples.
I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue. But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Hi Marc,
Please consider this patch Nacked as long as FWNODE_FLAG_BEST_EFFORT is used or until Anup actually works with us to debug the real issue.
-Saravana
[1] - https://lore.kernel.org/lkml/CAAhSdy2p6K70fc2yZLPdVGqEq61Y8F7WVT2J8st5mQrzBi4WHg@mail.gmail.com/
Quoted 128 lines
include/linux/irqchip/riscv-aplic.h new file +119
-1@@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ +#ifndef __LINUX_IRQCHIP_RISCV_APLIC_H +#define __LINUX_IRQCHIP_RISCV_APLIC_H + +#include <linux/bitops.h> + +#define APLIC_MAX_IDC BIT(14) +#define APLIC_MAX_SOURCE 1024 + +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_DOMAINCFG_RDONLY 0x80000000 +#define APLIC_DOMAINCFG_IE BIT(8) +#define APLIC_DOMAINCFG_DM BIT(2) +#define APLIC_DOMAINCFG_BE BIT(0) + +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_D BIT(10) +#define APLIC_SOURCECFG_CHILDIDX_MASK 0x000003ff +#define APLIC_SOURCECFG_SM_MASK 0x00000007 +#define APLIC_SOURCECFG_SM_INACTIVE 0x0 +#define APLIC_SOURCECFG_SM_DETACH 0x1 +#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4 +#define APLIC_SOURCECFG_SM_EDGE_FALL 0x5 +#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6 +#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 + +#define APLIC_MMSICFGADDR 0x1bc0 +#define APLIC_MMSICFGADDRH 0x1bc4 +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#ifdef CONFIG_RISCV_M_MODE +#define APLIC_xMSICFGADDR APLIC_MMSICFGADDR +#define APLIC_xMSICFGADDRH APLIC_MMSICFGADDRH +#else +#define APLIC_xMSICFGADDR APLIC_SMSICFGADDR +#define APLIC_xMSICFGADDRH APLIC_SMSICFGADDRH +#endif + +#define APLIC_xMSICFGADDRH_L BIT(31) +#define APLIC_xMSICFGADDRH_HHXS_MASK 0x1f +#define APLIC_xMSICFGADDRH_HHXS_SHIFT 24 +#define APLIC_xMSICFGADDRH_LHXS_MASK 0x7 +#define APLIC_xMSICFGADDRH_LHXS_SHIFT 20 +#define APLIC_xMSICFGADDRH_HHXW_MASK 0x7 +#define APLIC_xMSICFGADDRH_HHXW_SHIFT 16 +#define APLIC_xMSICFGADDRH_LHXW_MASK 0xf +#define APLIC_xMSICFGADDRH_LHXW_SHIFT 12 +#define APLIC_xMSICFGADDRH_BAPPN_MASK 0xfff + +#define APLIC_xMSICFGADDR_PPN_SHIFT 12 + +#define APLIC_xMSICFGADDR_PPN_HART (__lhxs) \ + (BIT(__lhxs) - 1) + +#define APLIC_xMSICFGADDR_PPN_LHX_MASK (__lhxw) \ + (BIT(__lhxw) - 1) +#define APLIC_xMSICFGADDR_PPN_LHX_SHIFT (__lhxs) \ + ((__lhxs)) +#define APLIC_xMSICFGADDR_PPN_LHX (__lhxw, __lhxs) \ + (APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) << \ + APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs)) + +#define APLIC_xMSICFGADDR_PPN_HHX_MASK (__hhxw) \ + (BIT(__hhxw) - 1) +#define APLIC_xMSICFGADDR_PPN_HHX_SHIFT (__hhxs) \ + ((__hhxs) + APLIC_xMSICFGADDR_PPN_SHIFT) +#define APLIC_xMSICFGADDR_PPN_HHX (__hhxw, __hhxs) \ + (APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) << \ + APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs)) + +#define APLIC_IRQBITS_PER_REG 32 + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 +#define APLIC_SETIPNUM_BE 0x2004 + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_HART_IDX_SHIFT 18 +#define APLIC_TARGET_HART_IDX_MASK 0x3fff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_GUEST_IDX_MASK 0x3f +#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_EIID_MASK 0x7ff + +#define APLIC_IDC_BASE 0x4000 +#define APLIC_IDC_SIZE 32 + +#define APLIC_IDC_IDELIVERY 0x00 + +#define APLIC_IDC_IFORCE 0x04 + +#define APLIC_IDC_ITHRESHOLD 0x08 + +#define APLIC_IDC_TOPI 0x18 +#define APLIC_IDC_TOPI_ID_SHIFT 16 +#define APLIC_IDC_TOPI_ID_MASK 0x3ff +#define APLIC_IDC_TOPI_PRIO_MASK 0xff + +#define APLIC_IDC_CLAIMI 0x1c + +#endif --2.34.1
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Hey Saravana,
Quoted 1 line · Saravana Kannan
On Tue, Jun 13, 2023 at 8:35 AM Anup Patel <[email protected]> wrote:
btw, please try to delete the 100s of lines of unrelated context when replying
Quoted 28 lines
Quoted 5 lines
drivers/irqchip/irq-riscv-aplic.c new file +5
-0+static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 4 lines
drivers/irqchip/irq-riscv-aplic.c new file +4
-0+of_node_clear_flag (node,OF_POPULATED ); +return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
Quoted 6 lines
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue.
There isn't a dts file for this because there's no publicly available hardware that actually has an APLIC. Maybe Ventana have pre-production silicon that has it, but otherwise it's a QEMU job.
Cheers,
Conor.
Quoted 11 lines
But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.-Saravana
[1] - https://lore.kernel.org/lkml/CAAhSdy2p6K70fc2yZLPdVGqEq61Y8F7WVT2J8st5mQrzBi4WHg@mail.gmail.com/
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 8 lines · Conor Dooley
Hey Saravana,
Quoted 1 line · Saravana Kannan
On Tue, Jun 13, 2023 at 8:35 AM Anup Patel <[email protected]> wrote:
btw, please try to delete the 100s of lines of unrelated context when replying
I always feel like some people like me to do this and others don't. Also, at times, people might want to reference the other lines of code when replying to my point. That's why I generally leave them in.
Quoted 20 lines
Quoted 20 lines
Quoted 6 lines
drivers/irqchip/irq-riscv-aplic.c new file +5
-0+static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
Quoted 1 line
Quoted 1 line
Quoted 1 line
+
of_node_clear_flag(node, OF_POPULATED);
Also, this part is not needed if the macros I mentioned below are used.
Quoted 12 lines
Quoted 12 lines
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 3 lines
drivers/irqchip/irq-riscv-aplic.c new file +3
-0+return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.
Quoted 6 lines
Quoted 2 lines
I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue.
There isn't a dts file for this because there's no publicly available hardware that actually has an APLIC. Maybe Ventana have pre-production silicon that has it, but otherwise it's a QEMU job.
1. QEMU example is fine too if it can be reproduced. I just asked for a dts file because I need the full global view of the dependencies. At a minimum, I'd at least expect to see some example DT and explanation of what dependency is causing the IRQ device to not be initialized on time, etc. Instead I just see random uses of flags with no description of the actual issue.
2. If it's not a dts available upstream, why should these drivers be accepted? I thought the norm was to only accept drivers that can actually be used.
-Saravana
Quoted 15 lines
Cheers,
Conor.Quoted 11 lines
But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.-Saravana
[1] - https://lore.kernel.org/lkml/CAAhSdy2p6K70fc2yZLPdVGqEq61Y8F7WVT2J8st5mQrzBi4WHg@mail.gmail.com/
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 10 lines · Saravana Kannan
Quoted 5 lines · Conor Dooley
Quoted 1 line · Saravana Kannan
On Tue, Jun 13, 2023 at 8:35 AM Anup Patel <[email protected]> wrote:
btw, please try to delete the 100s of lines of unrelated context when replying
I always feel like some people like me to do this and others don't. Also, at times, people might want to reference the other lines of code when replying to my point. That's why I generally leave them in.
Yah, perhaps I cull too aggressively but there's a middle ground ;)
Quoted 42 lines
Quoted 19 lines
Quoted 19 lines
Quoted 5 lines
drivers/irqchip/irq-riscv-aplic.c new file +5
-0+static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
Quoted 1 line
Quoted 1 line
Quoted 1 line
+
of_node_clear_flag(node, OF_POPULATED);Also, this part is not needed if the macros I mentioned below are used.
Quoted 12 lines
Quoted 12 lines
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 3 lines
drivers/irqchip/irq-riscv-aplic.c new file +3
-0+return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.Quoted 6 lines
Quoted 2 lines
I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue.
There isn't a dts file for this because there's no publicly available hardware that actually has an APLIC. Maybe Ventana have pre-production silicon that has it, but otherwise it's a QEMU job.
Quoted 6 lines
1. QEMU example is fine too if it can be reproduced. I just asked for a dts file because I need the full global view of the dependencies. At a minimum, I'd at least expect to see some example DT and explanation of what dependency is causing the IRQ device to not be initialized on time, etc. Instead I just see random uses of flags with no description of the actual issue.
It's Anup's responsibility to provide you with that information, I have not reproduced this issue, so I won't mislead you with QEMU invocations that may not be what's required to reproduce.
Quoted 3 lines
2. If it's not a dts available upstream, why should these drivers be accepted? I thought the norm was to only accept drivers that can actually be used.
I think it's not unusual (and desirable?) to start the upstreaming process for stuff before hardware is publicly available, so that once it is, support is already upstream, or close to. I do know that people have tested this series in FPGA based hardware emulation platforms etc. Posting patches for it also helps avoid duplication of effort between the various vendors in RISC-V land, who would otherwise have to write their own drivers. Also, the documented RISC-V policy for accepting support for ISA stuff says:
We'll only accept patches for new modules or extensions if the specifications for those modules or extensions are listed as being unlikely to be incompatibly changed in the future. For specifications from the RISC-V foundation this means "Frozen" (Documentation/riscv/patch-acceptance.rst)
AIA (the spec behind the APLIC/IMSIC) is frozen, and qualifies from a RISC-V point of view. What Marc is willing to accept, in terms of pre-production hardware support, is up to him obviously!
Cheers,
Conor.
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.-Saravana
[1] - https://lore.kernel.org/lkml/CAAhSdy2p6K70fc2yZLPdVGqEq61Y8F7WVT2J8st5mQrzBi4WHg@mail.gmail.com/
Anup Patel
maintainerRe: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
In reply to Saravana KannanQuoted 843 lines · Saravana Kannan
Quoted 808 lines · Anup Patel
The RISC-V advanced interrupt architecture (AIA) specification defines a new interrupt controller for managing wired interrupts on a RISC-V platform. This new interrupt controller is referred to as advanced platform-level interrupt controller (APLIC) which can forward wired interrupts to CPUs (or HARTs) as local interrupts OR as message signaled interrupts.
(For more details refer https://github.com/riscv/riscv-aia)This patch adds an irqchip driver for RISC-V APLIC found on RISC-V platforms.
Signed-off-by Anup Patel <[email protected]>--- drivers/irqchip/Kconfig | 6 + drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++++++++++++++++++ include/linux/irqchip/riscv-aplic.h | 119 +++++ 4 files changed, 891 insertions(+) create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 include/linux/irqchip/riscv-aplic.hdrivers/irqchip/Kconfig +6
-0@@ -544,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_APLIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config RISCV_IMSIC bool depends on RISCVdrivers/irqchip/Makefile +1
-0@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic.o obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.odrivers/irqchip/irq-riscv-aplic.c new file +751
-0@@ -0,0 +1,765 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ + +#define pr_fmt (fmt) "riscv-aplic: " fmt +#include <linux/bitops.h> +#include <linux/cpu.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/irq.h> +#include <linux/irqchip.h> +#include <linux/irqchip/chained_irq.h> +#include <linux/irqchip/riscv-aplic.h> +#include <linux/irqchip/riscv-imsic.h> +#include <linux/irqdomain.h> +#include <linux/module.h> +#include <linux/msi.h> +#include <linux/platform_device.h> +#include <linux/smp.h> + +#define APLIC_DEFAULT_PRIORITY 1 +#define APLIC_DISABLE_IDELIVERY 0 +#define APLIC_ENABLE_IDELIVERY 1 +#define APLIC_DISABLE_ITHRESHOLD 1 +#define APLIC_ENABLE_ITHRESHOLD 0 + +struct aplic_msicfg { +phys_addr_t base_ppn; +u32 hhxs; +u32 hhxw; +u32 lhxs; +u32 lhxw; +}; + +struct aplic_idc { +unsigned int hart_index; +void __iomem *regs; +struct aplic_priv *priv; +}; + +struct aplic_priv { +struct fwnode_handle *fwnode; +u32 gsi_base; +u32 nr_irqs; +u32 nr_idcs; +void __iomem *regs; +struct irq_domain *irqdomain; +struct aplic_msicfg msicfg; +struct cpumask lmask; +}; + +static unsigned int aplic_idc_parent_irq; +static DEFINE_PER_CPU(struct aplic_idc, aplic_idcs); + +static void aplic_irq_unmask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_SETIENUM ); + +if (!priv->nr_idcs) +irq_chip_unmask_parent (d); +} + +static void aplic_irq_mask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_CLRIENUM ); + +if (!priv->nr_idcs) +irq_chip_mask_parent (d); +} + +static int aplic_set_type (struct irq_data *d,unsigned int type) +{ +u32 val =0 ; +void __iomem *sourcecfg; +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +switch (type) { +case IRQ_TYPE_NONE : + val =APLIC_SOURCECFG_SM_INACTIVE ; +break ; +case IRQ_TYPE_LEVEL_LOW : + val =APLIC_SOURCECFG_SM_LEVEL_LOW ; +break ; +case IRQ_TYPE_LEVEL_HIGH : + val =APLIC_SOURCECFG_SM_LEVEL_HIGH ; +break ; +case IRQ_TYPE_EDGE_FALLING : + val =APLIC_SOURCECFG_SM_EDGE_FALL ; +break ; +case IRQ_TYPE_EDGE_RISING : + val =APLIC_SOURCECFG_SM_EDGE_RISE ; +break ; +default : +return -EINVAL ; + } + + sourcecfg = priv->regs +APLIC_SOURCECFG_BASE ; + sourcecfg += (d->hwirq -1 ) *sizeof (u32); +writel (val, sourcecfg); + +return 0 ; +} + +static void aplic_irq_eoi (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +u32 reg_off, reg_mask; + +/* +* EOI handling only required only for level-triggered +* interrupts in APLIC MSI mode. +*/ + +if (priv->nr_idcs) +return ; + + reg_off =APLIC_CLRIP_BASE + ((d->hwirq /APLIC_IRQBITS_PER_REG ) *4 ); + reg_mask =BIT (d->hwirq %APLIC_IRQBITS_PER_REG ); +switch (irqd_get_trigger_type (d)) { +case IRQ_TYPE_LEVEL_LOW : +if (!(readl (priv->regs + reg_off) & reg_mask)) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; +case IRQ_TYPE_LEVEL_HIGH : +if (readl (priv->regs + reg_off) & reg_mask) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; + } +} + +#ifdef CONFIG_SMP +static int aplic_set_affinity (struct irq_data *d, +const struct cpumask *mask_val,bool force) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_idc *idc; +unsigned int cpu, val; +struct cpumask amask; +void __iomem *target; + +if (!priv->nr_idcs) +return irq_chip_set_affinity_parent (d, mask_val, force); + +cpumask_and (&amask, &priv->lmask, mask_val); + +if (force) + cpu =cpumask_first (&amask); +else + cpu =cpumask_any_and (&amask, cpu_online_mask); + +if (cpu >= nr_cpu_ids) +return -EINVAL ; + + idc =per_cpu_ptr (&aplic_idcs, cpu); + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +writel (val, target); + +irq_data_update_effective_affinity (d,cpumask_of (cpu)); + +return IRQ_SET_MASK_OK_DONE ; +} +#endif + +static struct irq_chip aplic_chip = { + .name ="RISC-V APLIC" , + .irq_mask = aplic_irq_mask, + .irq_unmask = aplic_irq_unmask, + .irq_set_type = aplic_set_type, + .irq_eoi = aplic_irq_eoi, +#ifdef CONFIG_SMP + .irq_set_affinity = aplic_set_affinity, +#endif + .flags =IRQCHIP_SET_TYPE_MASKED | +IRQCHIP_SKIP_SET_WAKE | +IRQCHIP_MASK_ON_SUSPEND , +}; + +static int aplic_irqdomain_translate (struct irq_fwspec *fwspec, +u32 gsi_base, +unsigned long *hwirq, +unsigned int *type) +{ +if (WARN_ON (fwspec->param_count <2 )) +return -EINVAL ; +if (WARN_ON (!fwspec->param[0 ])) +return -EINVAL ; + +/* For DT, gsi_base is always zero. */ + *hwirq = fwspec->param[0 ] - gsi_base; + *type = fwspec->param[1 ] &IRQ_TYPE_SENSE_MASK ; + +WARN_ON (*type ==IRQ_TYPE_NONE ); + +return 0 ; +} + +static int aplic_irqdomain_msi_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv =platform_msi_get_host_data (d); + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_msi_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv =platform_msi_get_host_data (domain); + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + + ret =platform_msi_device_domain_alloc (domain, virq, nr_irqs); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +/* +* APLIC does not implement irq_disable() so Linux interrupt +* subsystem will take a lazy approach for disabling an APLIC +* interrupt. This means APLIC interrupts are left unmasked +* upon system suspend and interrupts are not processed +* immediately upon system wake up. To tackle this, we disable +* the lazy approach for all APLIC interrupts. +*/ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_msi_ops = { + .translate = aplic_irqdomain_msi_translate, + .alloc = aplic_irqdomain_msi_alloc, + .free = platform_msi_device_domain_free, +}; + +static int aplic_irqdomain_idc_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv = d->host_data; + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_idc_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv = domain->host_data; + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +irq_set_affinity (virq + i, &priv->lmask); +/* See the reason described in aplic_irqdomain_msi_alloc() */ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_idc_ops = { + .translate = aplic_irqdomain_idc_translate, + .alloc = aplic_irqdomain_idc_alloc, + .free = irq_domain_free_irqs_top, +}; + +static void aplic_init_hw_irqs (struct aplic_priv *priv) +{ +int i; + +/* Disable all interrupts */ +for (i =0 ; i <= priv->nr_irqs; i +=32 ) +writel (-1U , priv->regs +APLIC_CLRIE_BASE + + (i /32 ) *sizeof (u32)); + +/* Set interrupt type and default priority for all interrupts */ +for (i =1 ; i <= priv->nr_irqs; i++) { +writel (0 , priv->regs +APLIC_SOURCECFG_BASE + + (i -1 ) *sizeof (u32)); +writel (APLIC_DEFAULT_PRIORITY , + priv->regs +APLIC_TARGET_BASE + + (i -1 ) *sizeof (u32)); + } + +/* Clear APLIC domaincfg */ +writel (0 , priv->regs +APLIC_DOMAINCFG ); +} + +static void aplic_init_hw_global (struct aplic_priv *priv) +{ +u32 val; +#ifdef CONFIG_RISCV_M_MODE +u32 valH; + +if (!priv->nr_idcs) { + val = priv->msicfg.base_ppn; + valH = (priv->msicfg.base_ppn >>32 ) & + APLIC_xMSICFGADDRH_BAPPN_MASK; + valH |= (priv->msicfg.lhxw & APLIC_xMSICFGADDRH_LHXW_MASK) + << APLIC_xMSICFGADDRH_LHXW_SHIFT; + valH |= (priv->msicfg.hhxw & APLIC_xMSICFGADDRH_HHXW_MASK) + << APLIC_xMSICFGADDRH_HHXW_SHIFT; + valH |= (priv->msicfg.lhxs & APLIC_xMSICFGADDRH_LHXS_MASK) + << APLIC_xMSICFGADDRH_LHXS_SHIFT; + valH |= (priv->msicfg.hhxs & APLIC_xMSICFGADDRH_HHXS_MASK) + << APLIC_xMSICFGADDRH_HHXS_SHIFT; +writel (val, priv->regs + APLIC_xMSICFGADDR); +writel (valH, priv->regs + APLIC_xMSICFGADDRH); + } +#endif + +/* Setup APLIC domaincfg register */ + val =readl (priv->regs +APLIC_DOMAINCFG ); + val |=APLIC_DOMAINCFG_IE ; +if (!priv->nr_idcs) + val |=APLIC_DOMAINCFG_DM ; +writel (val, priv->regs +APLIC_DOMAINCFG ); +if (readl (priv->regs +APLIC_DOMAINCFG ) != val) +pr_warn ("%pfwP: unable to write 0x%x in domaincfg\n" , + priv->fwnode, val); +} + +static void aplic_msi_write_msg (struct msi_desc *desc,struct msi_msg *msg) +{ +unsigned int group_index, hart_index, guest_index, val; +struct irq_data *d =irq_get_irq_data (desc->irq); +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_msicfg *mc = &priv->msicfg; +phys_addr_t tppn, tbppn, msg_addr; +void __iomem *target; + +/* For zeroed MSI, simply write zero into the target register */ +if (!msg->address_hi && !msg->address_lo && !msg->data) { + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); +writel (0 , target); +return ; + } + +/* Sanity check on message data */ +WARN_ON (msg->data >APLIC_TARGET_EIID_MASK ); + +/* Compute target MSI address */ + msg_addr = (((u64 )msg->address_hi) <<32 ) | msg->address_lo; + tppn = msg_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + +/* Compute target HART Base PPN */ + tbppn = tppn; + tbppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); +WARN_ON (tbppn != mc->base_ppn); + +/* Compute target group and hart indexes */ + group_index = (tppn >>APLIC_xMSICFGADDR_PPN_HHX_SHIFT (mc->hhxs)) & +APLIC_xMSICFGADDR_PPN_HHX_MASK (mc->hhxw); + hart_index = (tppn >>APLIC_xMSICFGADDR_PPN_LHX_SHIFT (mc->lhxs)) & +APLIC_xMSICFGADDR_PPN_LHX_MASK (mc->lhxw); + hart_index |= (group_index << mc->lhxw); +WARN_ON (hart_index >APLIC_TARGET_HART_IDX_MASK ); + +/* Compute target guest index */ + guest_index = tppn &APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); +WARN_ON (guest_index >APLIC_TARGET_GUEST_IDX_MASK ); + +/* Update IRQ TARGET register */ + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = (hart_index &APLIC_TARGET_HART_IDX_MASK ) + <<APLIC_TARGET_HART_IDX_SHIFT ; + val |= (guest_index &APLIC_TARGET_GUEST_IDX_MASK ) + <<APLIC_TARGET_GUEST_IDX_SHIFT ; + val |= (msg->data &APLIC_TARGET_EIID_MASK ); +writel (val, target); +} + +static int aplic_setup_msi (struct aplic_priv *priv) +{ +struct aplic_msicfg *mc = &priv->msicfg; +const struct imsic_global_config *imsic_global; + +/* +* The APLIC outgoing MSI config registers assume target MSI +* controller to be RISC-V AIA IMSIC controller. +*/ + imsic_global =imsic_get_global_config (); +if (!imsic_global) { +pr_err ("%pfwP: IMSIC global config not found\n" , + priv->fwnode); +return -ENODEV ; + } + +/* Find number of guest index bits (LHXS) */ + mc->lhxs = imsic_global->guest_index_bits; +if (APLIC_xMSICFGADDRH_LHXS_MASK < mc->lhxs) { +pr_err ("%pfwP: IMSIC guest index bits big for APLIC LHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of HART index bits (LHXW) */ + mc->lhxw = imsic_global->hart_index_bits; +if (APLIC_xMSICFGADDRH_LHXW_MASK < mc->lhxw) { +pr_err ("%pfwP: IMSIC hart index bits big for APLIC LHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of group index bits (HHXW) */ + mc->hhxw = imsic_global->group_index_bits; +if (APLIC_xMSICFGADDRH_HHXW_MASK < mc->hhxw) { +pr_err ("%pfwP: IMSIC group index bits big for APLIC HHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find first bit position of group index (HHXS) */ + mc->hhxs = imsic_global->group_index_shift; +if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) { +pr_err ("%pfwP: IMSIC group index shift should be >= %d\n" , + priv->fwnode, (2 * APLIC_xMSICFGADDR_PPN_SHIFT)); +return -EINVAL ; + } + mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT); +if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) { +pr_err ("%pfwP: IMSIC group index shift big for APLIC HHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Compute PPN base */ + mc->base_ppn = imsic_global->base_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); + +/* Use all possible CPUs as lmask */ +cpumask_copy (&priv->lmask, cpu_possible_mask); + +return 0 ; +} + +/* +* To handle an APLIC IDC interrupts, we just read the CLAIMI register +* which will return highest priority pending interrupt and clear the +* pending bit of the interrupt. This process is repeated until CLAIMI +* register return zero value. +*/ +static void aplic_idc_handle_irq (struct irq_desc *desc) +{ +struct aplic_idc *idc =this_cpu_ptr (&aplic_idcs); +struct irq_chip *chip =irq_desc_get_chip (desc); +irq_hw_number_t hw_irq; +int irq; + +chained_irq_enter (chip, desc); + +while ((hw_irq =readl (idc->regs +APLIC_IDC_CLAIMI ))) { + hw_irq = hw_irq >>APLIC_IDC_TOPI_ID_SHIFT ; + irq =irq_find_mapping (idc->priv->irqdomain, hw_irq); + +if (unlikely (irq <=0 )) +pr_warn_ratelimited ("hw_irq %lu mapping not found\n" , + hw_irq); +else +generic_handle_irq (irq); + } + +chained_irq_exit (chip, desc); +} + +static void aplic_idc_set_delivery (struct aplic_idc *idc,bool en) +{ +u32 de = (en) ?APLIC_ENABLE_IDELIVERY :APLIC_DISABLE_IDELIVERY ; +u32 th = (en) ?APLIC_ENABLE_ITHRESHOLD :APLIC_DISABLE_ITHRESHOLD ; + +/* Priority must be less than threshold for interrupt triggering */ +writel (th, idc->regs +APLIC_IDC_ITHRESHOLD ); + +/* Delivery must be set to 1 for interrupt triggering */ +writel (de, idc->regs +APLIC_IDC_IDELIVERY ); +} + +static int aplic_idc_dying_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +disable_percpu_irq (aplic_idc_parent_irq); + +return 0 ; +} + +static int aplic_idc_starting_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +enable_percpu_irq (aplic_idc_parent_irq, +irq_get_trigger_type (aplic_idc_parent_irq)); + +return 0 ; +} + +static int aplic_setup_idc (struct aplic_priv *priv) +{ +int i, j, rc, cpu, setup_count =0 ; +struct fwnode_reference_args parent; +struct irq_domain *domain; +unsigned long hartid; +struct aplic_idc *idc; +u32 val; + +/* Setup per-CPU IDC and target CPU mask */ +for (i =0 ; i < priv->nr_idcs; i++) { + rc =fwnode_property_get_reference_args (priv->fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , i, &parent); +if (rc) { +pr_warn ("%pfwP: parent irq for IDC%d not found\n" , + priv->fwnode, i); +continue ; + } + +/* +* Skip interrupts other than external interrupts for +* current privilege level. +*/ +if (parent.args[0 ] !=RV_IRQ_EXT ) +continue ; + + rc =riscv_fw_parent_hartid (parent.fwnode, &hartid); +if (rc) { +pr_warn ("%pfwP: invalid hartid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + + cpu =riscv_hartid_to_cpuid (hartid); +if (cpu <0 ) { +pr_warn ("%pfwP: invalid cpuid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + +cpumask_set_cpu (cpu, &priv->lmask); + + idc =per_cpu_ptr (&aplic_idcs, cpu); + idc->hart_index = i; + idc->regs = priv->regs +APLIC_IDC_BASE + i *APLIC_IDC_SIZE ; + idc->priv = priv; + +aplic_idc_set_delivery (idc, true); + +/* +* Boot cpu might not have APLIC hart_index = 0 so check +* and update target registers of all interrupts. +*/ +if (cpu ==smp_processor_id () && idc->hart_index) { + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +for (j =1 ; j <= priv->nr_irqs; j++) +writel (val, priv->regs +APLIC_TARGET_BASE + + (j -1 ) *sizeof (u32)); + } + + setup_count++; + } + +/* Find parent domain and register chained handler */ + domain =irq_find_matching_fwnode (riscv_get_intc_hwnode (), +DOMAIN_BUS_ANY ); +if (!aplic_idc_parent_irq && domain) { + aplic_idc_parent_irq =irq_create_mapping (domain,RV_IRQ_EXT ); +if (aplic_idc_parent_irq) { +irq_set_chained_handler (aplic_idc_parent_irq, + aplic_idc_handle_irq); + +/* +* Setup CPUHP notifier to enable IDC parent +* interrupt on all CPUs +*/ +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"irqchip/riscv/aplic:starting" , + aplic_idc_starting_cpu, + aplic_idc_dying_cpu); + } + } + +/* Fail if we were not able to setup IDC for any CPU */ +return (setup_count) ?0 : -ENODEV ; +} + +static int aplic_probe (struct platform_device *pdev) +{ +struct fwnode_handle *fwnode = pdev->dev.fwnode; +struct fwnode_reference_args parent; +struct aplic_priv *priv; +struct resource *res; +phys_addr_t pa; +int rc; + + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + priv->fwnode = fwnode; + +/* Map the MMIO registers */ + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +if (!res) { +pr_err ("%pfwP: failed to get MMIO resource\n" , fwnode); +return -EINVAL ; + } + priv->regs =devm_ioremap (&pdev->dev, res->start,resource_size (res)); +if (!priv->regs) { +pr_err ("%pfwP: failed map MMIO registers\n" , fwnode); +return -ENOMEM ; + } + +/* +* Find out GSI base number +* +* Note: DT does not define "riscv,gsi-base" property so GSI +* base is always zero for DT. +*/ + rc =fwnode_property_read_u32_array (fwnode,"riscv,gsi-base" , + &priv->gsi_base,1 ); +if (rc) + priv->gsi_base =0 ; + +/* Find out number of interrupt sources */ + rc =fwnode_property_read_u32_array (fwnode,"riscv,num-sources" , + &priv->nr_irqs,1 ); +if (rc) { +pr_err ("%pfwP: failed to get number of interrupt sources\n" , + fwnode); +return rc; + } + +/* Setup initial state APLIC interrupts */ +aplic_init_hw_irqs (priv); + +/* +* Find out number of IDCs based on parent interrupts +* +* If "msi-parent" property is present then we ignore the +* APLIC IDCs which forces the APLIC driver to use MSI mode. +*/ +if (!fwnode_property_present (fwnode,"msi-parent" )) { +while (!fwnode_property_get_reference_args (fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , priv->nr_idcs, &parent)) + priv->nr_idcs++; + } + +/* Setup IDCs or MSIs based on number of IDCs */ +if (priv->nr_idcs) + rc =aplic_setup_idc (priv); +else + rc =aplic_setup_msi (priv); +if (rc) { +pr_err ("%pfwP: failed setup %s\n" , + fwnode, priv->nr_idcs ?"IDCs" :"MSIs" ); +return rc; + } + +/* Setup global config and interrupt delivery */ +aplic_init_hw_global (priv); + +/* Create irq domain instance for the APLIC */ +if (priv->nr_idcs) + priv->irqdomain =irq_domain_create_linear ( + priv->fwnode, + priv->nr_irqs +1 , + &aplic_irqdomain_idc_ops, + priv); +else + priv->irqdomain =platform_msi_create_device_domain ( + &pdev->dev, + priv->nr_irqs +1 , + aplic_msi_write_msg, + &aplic_irqdomain_msi_ops, + priv); +if (!priv->irqdomain) { +pr_err ("%pfwP: failed to add irq domain\n" , priv->fwnode); +return -ENOMEM ; + } + +/* Advertise the interrupt controller */ +if (priv->nr_idcs) { +pr_info ("%pfwP: %d interrupts directly connected to %d CPUs\n" , + priv->fwnode, priv->nr_irqs, priv->nr_idcs); + }else { + pa = priv->msicfg.base_ppn << APLIC_xMSICFGADDR_PPN_SHIFT; +pr_info ("%pfwP: %d interrupts forwared to MSI base %pa\n" , + priv->fwnode, priv->nr_irqs, &pa); + } + +return 0 ; +} + +static const struct of_device_id aplic_match[] = { + { .compatible ="riscv,aplic" }, + {} +}; + +static struct platform_driver aplic_driver = { + .driver = { + .name ="riscv-aplic" , + .of_match_table = aplic_match, + }, + .probe = aplic_probe, +}; +builtin_platform_driver (aplic_driver); + +static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 4 lines
drivers/irqchip/irq-riscv-aplic.c new file +4
-0+of_node_clear_flag (node,OF_POPULATED ); +return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue. But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Quoted 4 lines
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.
Maybe I misread your previous comment.
You can easily reproduce the issue on QEMU virt machine for RISC-V:
1) Build qemu-system-riscv64 from latest QEMU master
2) Build kernel from riscv_aia_v4 branch at https://github.com/avpatel/linux.git
(Note: make sure you remove the FWNODE_FLAG_BEST_EFFORT flag from
APLIC driver at the time of building kernel)
3) Boot a APLIC-only system on QEMU virt machine
qemu-system-riscv64 -smp 4 -M virt,aia=aplic -m 1G -nographic \
-bios opensbi/build/platform/generic/firmware/fw_dynamic.bin \
-kernel ./build-riscv64/arch/riscv/boot/Image \
-append "root=/dev/ram rw console=ttyS0 earlycon" \
-initrd ./rootfs_riscv64.imgI hope the above steps help you reproduce the issue. I will certainly test whatever fix you propose.
Regards,
Anup
Quoted 133 lines
-Saravana
[1] - https://lore.kernel.org/lkml/CAAhSdy2p6K70fc2yZLPdVGqEq61Y8F7WVT2J8st5mQrzBi4WHg@mail.gmail.com/Quoted 128 lines
include/linux/irqchip/riscv-aplic.h new file +119
-1@@ -0,0 +1,119 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ +#ifndef __LINUX_IRQCHIP_RISCV_APLIC_H +#define __LINUX_IRQCHIP_RISCV_APLIC_H + +#include <linux/bitops.h> + +#define APLIC_MAX_IDC BIT(14) +#define APLIC_MAX_SOURCE 1024 + +#define APLIC_DOMAINCFG 0x0000 +#define APLIC_DOMAINCFG_RDONLY 0x80000000 +#define APLIC_DOMAINCFG_IE BIT(8) +#define APLIC_DOMAINCFG_DM BIT(2) +#define APLIC_DOMAINCFG_BE BIT(0) + +#define APLIC_SOURCECFG_BASE 0x0004 +#define APLIC_SOURCECFG_D BIT(10) +#define APLIC_SOURCECFG_CHILDIDX_MASK 0x000003ff +#define APLIC_SOURCECFG_SM_MASK 0x00000007 +#define APLIC_SOURCECFG_SM_INACTIVE 0x0 +#define APLIC_SOURCECFG_SM_DETACH 0x1 +#define APLIC_SOURCECFG_SM_EDGE_RISE 0x4 +#define APLIC_SOURCECFG_SM_EDGE_FALL 0x5 +#define APLIC_SOURCECFG_SM_LEVEL_HIGH 0x6 +#define APLIC_SOURCECFG_SM_LEVEL_LOW 0x7 + +#define APLIC_MMSICFGADDR 0x1bc0 +#define APLIC_MMSICFGADDRH 0x1bc4 +#define APLIC_SMSICFGADDR 0x1bc8 +#define APLIC_SMSICFGADDRH 0x1bcc + +#ifdef CONFIG_RISCV_M_MODE +#define APLIC_xMSICFGADDR APLIC_MMSICFGADDR +#define APLIC_xMSICFGADDRH APLIC_MMSICFGADDRH +#else +#define APLIC_xMSICFGADDR APLIC_SMSICFGADDR +#define APLIC_xMSICFGADDRH APLIC_SMSICFGADDRH +#endif + +#define APLIC_xMSICFGADDRH_L BIT(31) +#define APLIC_xMSICFGADDRH_HHXS_MASK 0x1f +#define APLIC_xMSICFGADDRH_HHXS_SHIFT 24 +#define APLIC_xMSICFGADDRH_LHXS_MASK 0x7 +#define APLIC_xMSICFGADDRH_LHXS_SHIFT 20 +#define APLIC_xMSICFGADDRH_HHXW_MASK 0x7 +#define APLIC_xMSICFGADDRH_HHXW_SHIFT 16 +#define APLIC_xMSICFGADDRH_LHXW_MASK 0xf +#define APLIC_xMSICFGADDRH_LHXW_SHIFT 12 +#define APLIC_xMSICFGADDRH_BAPPN_MASK 0xfff + +#define APLIC_xMSICFGADDR_PPN_SHIFT 12 + +#define APLIC_xMSICFGADDR_PPN_HART (__lhxs) \ + (BIT(__lhxs) - 1) + +#define APLIC_xMSICFGADDR_PPN_LHX_MASK (__lhxw) \ + (BIT(__lhxw) - 1) +#define APLIC_xMSICFGADDR_PPN_LHX_SHIFT (__lhxs) \ + ((__lhxs)) +#define APLIC_xMSICFGADDR_PPN_LHX (__lhxw, __lhxs) \ + (APLIC_xMSICFGADDR_PPN_LHX_MASK(__lhxw) << \ + APLIC_xMSICFGADDR_PPN_LHX_SHIFT(__lhxs)) + +#define APLIC_xMSICFGADDR_PPN_HHX_MASK (__hhxw) \ + (BIT(__hhxw) - 1) +#define APLIC_xMSICFGADDR_PPN_HHX_SHIFT (__hhxs) \ + ((__hhxs) + APLIC_xMSICFGADDR_PPN_SHIFT) +#define APLIC_xMSICFGADDR_PPN_HHX (__hhxw, __hhxs) \ + (APLIC_xMSICFGADDR_PPN_HHX_MASK(__hhxw) << \ + APLIC_xMSICFGADDR_PPN_HHX_SHIFT(__hhxs)) + +#define APLIC_IRQBITS_PER_REG 32 + +#define APLIC_SETIP_BASE 0x1c00 +#define APLIC_SETIPNUM 0x1cdc + +#define APLIC_CLRIP_BASE 0x1d00 +#define APLIC_CLRIPNUM 0x1ddc + +#define APLIC_SETIE_BASE 0x1e00 +#define APLIC_SETIENUM 0x1edc + +#define APLIC_CLRIE_BASE 0x1f00 +#define APLIC_CLRIENUM 0x1fdc + +#define APLIC_SETIPNUM_LE 0x2000 +#define APLIC_SETIPNUM_BE 0x2004 + +#define APLIC_GENMSI 0x3000 + +#define APLIC_TARGET_BASE 0x3004 +#define APLIC_TARGET_HART_IDX_SHIFT 18 +#define APLIC_TARGET_HART_IDX_MASK 0x3fff +#define APLIC_TARGET_GUEST_IDX_SHIFT 12 +#define APLIC_TARGET_GUEST_IDX_MASK 0x3f +#define APLIC_TARGET_IPRIO_MASK 0xff +#define APLIC_TARGET_EIID_MASK 0x7ff + +#define APLIC_IDC_BASE 0x4000 +#define APLIC_IDC_SIZE 32 + +#define APLIC_IDC_IDELIVERY 0x00 + +#define APLIC_IDC_IFORCE 0x04 + +#define APLIC_IDC_ITHRESHOLD 0x08 + +#define APLIC_IDC_TOPI 0x18 +#define APLIC_IDC_TOPI_ID_SHIFT 16 +#define APLIC_IDC_TOPI_ID_MASK 0x3ff +#define APLIC_IDC_TOPI_PRIO_MASK 0xff + +#define APLIC_IDC_CLAIMI 0x1c + +#endif --2.34.1
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 858 lines · Anup Patel
Quoted 843 lines · Saravana Kannan
Quoted 808 lines · Anup Patel
The RISC-V advanced interrupt architecture (AIA) specification defines a new interrupt controller for managing wired interrupts on a RISC-V platform. This new interrupt controller is referred to as advanced platform-level interrupt controller (APLIC) which can forward wired interrupts to CPUs (or HARTs) as local interrupts OR as message signaled interrupts.
(For more details refer https://github.com/riscv/riscv-aia)This patch adds an irqchip driver for RISC-V APLIC found on RISC-V platforms.
Signed-off-by Anup Patel <[email protected]>--- drivers/irqchip/Kconfig | 6 + drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++++++++++++++++++ include/linux/irqchip/riscv-aplic.h | 119 +++++ 4 files changed, 891 insertions(+) create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 include/linux/irqchip/riscv-aplic.hdrivers/irqchip/Kconfig +6
-0@@ -544,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_APLIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config RISCV_IMSIC bool depends on RISCVdrivers/irqchip/Makefile +1
-0@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic.o obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.odrivers/irqchip/irq-riscv-aplic.c new file +751
-0@@ -0,0 +1,765 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ + +#define pr_fmt (fmt) "riscv-aplic: " fmt +#include <linux/bitops.h> +#include <linux/cpu.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/irq.h> +#include <linux/irqchip.h> +#include <linux/irqchip/chained_irq.h> +#include <linux/irqchip/riscv-aplic.h> +#include <linux/irqchip/riscv-imsic.h> +#include <linux/irqdomain.h> +#include <linux/module.h> +#include <linux/msi.h> +#include <linux/platform_device.h> +#include <linux/smp.h> + +#define APLIC_DEFAULT_PRIORITY 1 +#define APLIC_DISABLE_IDELIVERY 0 +#define APLIC_ENABLE_IDELIVERY 1 +#define APLIC_DISABLE_ITHRESHOLD 1 +#define APLIC_ENABLE_ITHRESHOLD 0 + +struct aplic_msicfg { +phys_addr_t base_ppn; +u32 hhxs; +u32 hhxw; +u32 lhxs; +u32 lhxw; +}; + +struct aplic_idc { +unsigned int hart_index; +void __iomem *regs; +struct aplic_priv *priv; +}; + +struct aplic_priv { +struct fwnode_handle *fwnode; +u32 gsi_base; +u32 nr_irqs; +u32 nr_idcs; +void __iomem *regs; +struct irq_domain *irqdomain; +struct aplic_msicfg msicfg; +struct cpumask lmask; +}; + +static unsigned int aplic_idc_parent_irq; +static DEFINE_PER_CPU(struct aplic_idc, aplic_idcs); + +static void aplic_irq_unmask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_SETIENUM ); + +if (!priv->nr_idcs) +irq_chip_unmask_parent (d); +} + +static void aplic_irq_mask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_CLRIENUM ); + +if (!priv->nr_idcs) +irq_chip_mask_parent (d); +} + +static int aplic_set_type (struct irq_data *d,unsigned int type) +{ +u32 val =0 ; +void __iomem *sourcecfg; +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +switch (type) { +case IRQ_TYPE_NONE : + val =APLIC_SOURCECFG_SM_INACTIVE ; +break ; +case IRQ_TYPE_LEVEL_LOW : + val =APLIC_SOURCECFG_SM_LEVEL_LOW ; +break ; +case IRQ_TYPE_LEVEL_HIGH : + val =APLIC_SOURCECFG_SM_LEVEL_HIGH ; +break ; +case IRQ_TYPE_EDGE_FALLING : + val =APLIC_SOURCECFG_SM_EDGE_FALL ; +break ; +case IRQ_TYPE_EDGE_RISING : + val =APLIC_SOURCECFG_SM_EDGE_RISE ; +break ; +default : +return -EINVAL ; + } + + sourcecfg = priv->regs +APLIC_SOURCECFG_BASE ; + sourcecfg += (d->hwirq -1 ) *sizeof (u32); +writel (val, sourcecfg); + +return 0 ; +} + +static void aplic_irq_eoi (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +u32 reg_off, reg_mask; + +/* +* EOI handling only required only for level-triggered +* interrupts in APLIC MSI mode. +*/ + +if (priv->nr_idcs) +return ; + + reg_off =APLIC_CLRIP_BASE + ((d->hwirq /APLIC_IRQBITS_PER_REG ) *4 ); + reg_mask =BIT (d->hwirq %APLIC_IRQBITS_PER_REG ); +switch (irqd_get_trigger_type (d)) { +case IRQ_TYPE_LEVEL_LOW : +if (!(readl (priv->regs + reg_off) & reg_mask)) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; +case IRQ_TYPE_LEVEL_HIGH : +if (readl (priv->regs + reg_off) & reg_mask) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; + } +} + +#ifdef CONFIG_SMP +static int aplic_set_affinity (struct irq_data *d, +const struct cpumask *mask_val,bool force) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_idc *idc; +unsigned int cpu, val; +struct cpumask amask; +void __iomem *target; + +if (!priv->nr_idcs) +return irq_chip_set_affinity_parent (d, mask_val, force); + +cpumask_and (&amask, &priv->lmask, mask_val); + +if (force) + cpu =cpumask_first (&amask); +else + cpu =cpumask_any_and (&amask, cpu_online_mask); + +if (cpu >= nr_cpu_ids) +return -EINVAL ; + + idc =per_cpu_ptr (&aplic_idcs, cpu); + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +writel (val, target); + +irq_data_update_effective_affinity (d,cpumask_of (cpu)); + +return IRQ_SET_MASK_OK_DONE ; +} +#endif + +static struct irq_chip aplic_chip = { + .name ="RISC-V APLIC" , + .irq_mask = aplic_irq_mask, + .irq_unmask = aplic_irq_unmask, + .irq_set_type = aplic_set_type, + .irq_eoi = aplic_irq_eoi, +#ifdef CONFIG_SMP + .irq_set_affinity = aplic_set_affinity, +#endif + .flags =IRQCHIP_SET_TYPE_MASKED | +IRQCHIP_SKIP_SET_WAKE | +IRQCHIP_MASK_ON_SUSPEND , +}; + +static int aplic_irqdomain_translate (struct irq_fwspec *fwspec, +u32 gsi_base, +unsigned long *hwirq, +unsigned int *type) +{ +if (WARN_ON (fwspec->param_count <2 )) +return -EINVAL ; +if (WARN_ON (!fwspec->param[0 ])) +return -EINVAL ; + +/* For DT, gsi_base is always zero. */ + *hwirq = fwspec->param[0 ] - gsi_base; + *type = fwspec->param[1 ] &IRQ_TYPE_SENSE_MASK ; + +WARN_ON (*type ==IRQ_TYPE_NONE ); + +return 0 ; +} + +static int aplic_irqdomain_msi_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv =platform_msi_get_host_data (d); + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_msi_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv =platform_msi_get_host_data (domain); + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + + ret =platform_msi_device_domain_alloc (domain, virq, nr_irqs); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +/* +* APLIC does not implement irq_disable() so Linux interrupt +* subsystem will take a lazy approach for disabling an APLIC +* interrupt. This means APLIC interrupts are left unmasked +* upon system suspend and interrupts are not processed +* immediately upon system wake up. To tackle this, we disable +* the lazy approach for all APLIC interrupts. +*/ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_msi_ops = { + .translate = aplic_irqdomain_msi_translate, + .alloc = aplic_irqdomain_msi_alloc, + .free = platform_msi_device_domain_free, +}; + +static int aplic_irqdomain_idc_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv = d->host_data; + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_idc_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv = domain->host_data; + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +irq_set_affinity (virq + i, &priv->lmask); +/* See the reason described in aplic_irqdomain_msi_alloc() */ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_idc_ops = { + .translate = aplic_irqdomain_idc_translate, + .alloc = aplic_irqdomain_idc_alloc, + .free = irq_domain_free_irqs_top, +}; + +static void aplic_init_hw_irqs (struct aplic_priv *priv) +{ +int i; + +/* Disable all interrupts */ +for (i =0 ; i <= priv->nr_irqs; i +=32 ) +writel (-1U , priv->regs +APLIC_CLRIE_BASE + + (i /32 ) *sizeof (u32)); + +/* Set interrupt type and default priority for all interrupts */ +for (i =1 ; i <= priv->nr_irqs; i++) { +writel (0 , priv->regs +APLIC_SOURCECFG_BASE + + (i -1 ) *sizeof (u32)); +writel (APLIC_DEFAULT_PRIORITY , + priv->regs +APLIC_TARGET_BASE + + (i -1 ) *sizeof (u32)); + } + +/* Clear APLIC domaincfg */ +writel (0 , priv->regs +APLIC_DOMAINCFG ); +} + +static void aplic_init_hw_global (struct aplic_priv *priv) +{ +u32 val; +#ifdef CONFIG_RISCV_M_MODE +u32 valH; + +if (!priv->nr_idcs) { + val = priv->msicfg.base_ppn; + valH = (priv->msicfg.base_ppn >>32 ) & + APLIC_xMSICFGADDRH_BAPPN_MASK; + valH |= (priv->msicfg.lhxw & APLIC_xMSICFGADDRH_LHXW_MASK) + << APLIC_xMSICFGADDRH_LHXW_SHIFT; + valH |= (priv->msicfg.hhxw & APLIC_xMSICFGADDRH_HHXW_MASK) + << APLIC_xMSICFGADDRH_HHXW_SHIFT; + valH |= (priv->msicfg.lhxs & APLIC_xMSICFGADDRH_LHXS_MASK) + << APLIC_xMSICFGADDRH_LHXS_SHIFT; + valH |= (priv->msicfg.hhxs & APLIC_xMSICFGADDRH_HHXS_MASK) + << APLIC_xMSICFGADDRH_HHXS_SHIFT; +writel (val, priv->regs + APLIC_xMSICFGADDR); +writel (valH, priv->regs + APLIC_xMSICFGADDRH); + } +#endif + +/* Setup APLIC domaincfg register */ + val =readl (priv->regs +APLIC_DOMAINCFG ); + val |=APLIC_DOMAINCFG_IE ; +if (!priv->nr_idcs) + val |=APLIC_DOMAINCFG_DM ; +writel (val, priv->regs +APLIC_DOMAINCFG ); +if (readl (priv->regs +APLIC_DOMAINCFG ) != val) +pr_warn ("%pfwP: unable to write 0x%x in domaincfg\n" , + priv->fwnode, val); +} + +static void aplic_msi_write_msg (struct msi_desc *desc,struct msi_msg *msg) +{ +unsigned int group_index, hart_index, guest_index, val; +struct irq_data *d =irq_get_irq_data (desc->irq); +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_msicfg *mc = &priv->msicfg; +phys_addr_t tppn, tbppn, msg_addr; +void __iomem *target; + +/* For zeroed MSI, simply write zero into the target register */ +if (!msg->address_hi && !msg->address_lo && !msg->data) { + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); +writel (0 , target); +return ; + } + +/* Sanity check on message data */ +WARN_ON (msg->data >APLIC_TARGET_EIID_MASK ); + +/* Compute target MSI address */ + msg_addr = (((u64 )msg->address_hi) <<32 ) | msg->address_lo; + tppn = msg_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + +/* Compute target HART Base PPN */ + tbppn = tppn; + tbppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); +WARN_ON (tbppn != mc->base_ppn); + +/* Compute target group and hart indexes */ + group_index = (tppn >>APLIC_xMSICFGADDR_PPN_HHX_SHIFT (mc->hhxs)) & +APLIC_xMSICFGADDR_PPN_HHX_MASK (mc->hhxw); + hart_index = (tppn >>APLIC_xMSICFGADDR_PPN_LHX_SHIFT (mc->lhxs)) & +APLIC_xMSICFGADDR_PPN_LHX_MASK (mc->lhxw); + hart_index |= (group_index << mc->lhxw); +WARN_ON (hart_index >APLIC_TARGET_HART_IDX_MASK ); + +/* Compute target guest index */ + guest_index = tppn &APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); +WARN_ON (guest_index >APLIC_TARGET_GUEST_IDX_MASK ); + +/* Update IRQ TARGET register */ + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = (hart_index &APLIC_TARGET_HART_IDX_MASK ) + <<APLIC_TARGET_HART_IDX_SHIFT ; + val |= (guest_index &APLIC_TARGET_GUEST_IDX_MASK ) + <<APLIC_TARGET_GUEST_IDX_SHIFT ; + val |= (msg->data &APLIC_TARGET_EIID_MASK ); +writel (val, target); +} + +static int aplic_setup_msi (struct aplic_priv *priv) +{ +struct aplic_msicfg *mc = &priv->msicfg; +const struct imsic_global_config *imsic_global; + +/* +* The APLIC outgoing MSI config registers assume target MSI +* controller to be RISC-V AIA IMSIC controller. +*/ + imsic_global =imsic_get_global_config (); +if (!imsic_global) { +pr_err ("%pfwP: IMSIC global config not found\n" , + priv->fwnode); +return -ENODEV ; + } + +/* Find number of guest index bits (LHXS) */ + mc->lhxs = imsic_global->guest_index_bits; +if (APLIC_xMSICFGADDRH_LHXS_MASK < mc->lhxs) { +pr_err ("%pfwP: IMSIC guest index bits big for APLIC LHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of HART index bits (LHXW) */ + mc->lhxw = imsic_global->hart_index_bits; +if (APLIC_xMSICFGADDRH_LHXW_MASK < mc->lhxw) { +pr_err ("%pfwP: IMSIC hart index bits big for APLIC LHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of group index bits (HHXW) */ + mc->hhxw = imsic_global->group_index_bits; +if (APLIC_xMSICFGADDRH_HHXW_MASK < mc->hhxw) { +pr_err ("%pfwP: IMSIC group index bits big for APLIC HHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find first bit position of group index (HHXS) */ + mc->hhxs = imsic_global->group_index_shift; +if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) { +pr_err ("%pfwP: IMSIC group index shift should be >= %d\n" , + priv->fwnode, (2 * APLIC_xMSICFGADDR_PPN_SHIFT)); +return -EINVAL ; + } + mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT); +if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) { +pr_err ("%pfwP: IMSIC group index shift big for APLIC HHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Compute PPN base */ + mc->base_ppn = imsic_global->base_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); + +/* Use all possible CPUs as lmask */ +cpumask_copy (&priv->lmask, cpu_possible_mask); + +return 0 ; +} + +/* +* To handle an APLIC IDC interrupts, we just read the CLAIMI register +* which will return highest priority pending interrupt and clear the +* pending bit of the interrupt. This process is repeated until CLAIMI +* register return zero value. +*/ +static void aplic_idc_handle_irq (struct irq_desc *desc) +{ +struct aplic_idc *idc =this_cpu_ptr (&aplic_idcs); +struct irq_chip *chip =irq_desc_get_chip (desc); +irq_hw_number_t hw_irq; +int irq; + +chained_irq_enter (chip, desc); + +while ((hw_irq =readl (idc->regs +APLIC_IDC_CLAIMI ))) { + hw_irq = hw_irq >>APLIC_IDC_TOPI_ID_SHIFT ; + irq =irq_find_mapping (idc->priv->irqdomain, hw_irq); + +if (unlikely (irq <=0 )) +pr_warn_ratelimited ("hw_irq %lu mapping not found\n" , + hw_irq); +else +generic_handle_irq (irq); + } + +chained_irq_exit (chip, desc); +} + +static void aplic_idc_set_delivery (struct aplic_idc *idc,bool en) +{ +u32 de = (en) ?APLIC_ENABLE_IDELIVERY :APLIC_DISABLE_IDELIVERY ; +u32 th = (en) ?APLIC_ENABLE_ITHRESHOLD :APLIC_DISABLE_ITHRESHOLD ; + +/* Priority must be less than threshold for interrupt triggering */ +writel (th, idc->regs +APLIC_IDC_ITHRESHOLD ); + +/* Delivery must be set to 1 for interrupt triggering */ +writel (de, idc->regs +APLIC_IDC_IDELIVERY ); +} + +static int aplic_idc_dying_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +disable_percpu_irq (aplic_idc_parent_irq); + +return 0 ; +} + +static int aplic_idc_starting_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +enable_percpu_irq (aplic_idc_parent_irq, +irq_get_trigger_type (aplic_idc_parent_irq)); + +return 0 ; +} + +static int aplic_setup_idc (struct aplic_priv *priv) +{ +int i, j, rc, cpu, setup_count =0 ; +struct fwnode_reference_args parent; +struct irq_domain *domain; +unsigned long hartid; +struct aplic_idc *idc; +u32 val; + +/* Setup per-CPU IDC and target CPU mask */ +for (i =0 ; i < priv->nr_idcs; i++) { + rc =fwnode_property_get_reference_args (priv->fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , i, &parent); +if (rc) { +pr_warn ("%pfwP: parent irq for IDC%d not found\n" , + priv->fwnode, i); +continue ; + } + +/* +* Skip interrupts other than external interrupts for +* current privilege level. +*/ +if (parent.args[0 ] !=RV_IRQ_EXT ) +continue ; + + rc =riscv_fw_parent_hartid (parent.fwnode, &hartid); +if (rc) { +pr_warn ("%pfwP: invalid hartid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + + cpu =riscv_hartid_to_cpuid (hartid); +if (cpu <0 ) { +pr_warn ("%pfwP: invalid cpuid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + +cpumask_set_cpu (cpu, &priv->lmask); + + idc =per_cpu_ptr (&aplic_idcs, cpu); + idc->hart_index = i; + idc->regs = priv->regs +APLIC_IDC_BASE + i *APLIC_IDC_SIZE ; + idc->priv = priv; + +aplic_idc_set_delivery (idc, true); + +/* +* Boot cpu might not have APLIC hart_index = 0 so check +* and update target registers of all interrupts. +*/ +if (cpu ==smp_processor_id () && idc->hart_index) { + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +for (j =1 ; j <= priv->nr_irqs; j++) +writel (val, priv->regs +APLIC_TARGET_BASE + + (j -1 ) *sizeof (u32)); + } + + setup_count++; + } + +/* Find parent domain and register chained handler */ + domain =irq_find_matching_fwnode (riscv_get_intc_hwnode (), +DOMAIN_BUS_ANY ); +if (!aplic_idc_parent_irq && domain) { + aplic_idc_parent_irq =irq_create_mapping (domain,RV_IRQ_EXT ); +if (aplic_idc_parent_irq) { +irq_set_chained_handler (aplic_idc_parent_irq, + aplic_idc_handle_irq); + +/* +* Setup CPUHP notifier to enable IDC parent +* interrupt on all CPUs +*/ +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"irqchip/riscv/aplic:starting" , + aplic_idc_starting_cpu, + aplic_idc_dying_cpu); + } + } + +/* Fail if we were not able to setup IDC for any CPU */ +return (setup_count) ?0 : -ENODEV ; +} + +static int aplic_probe (struct platform_device *pdev) +{ +struct fwnode_handle *fwnode = pdev->dev.fwnode; +struct fwnode_reference_args parent; +struct aplic_priv *priv; +struct resource *res; +phys_addr_t pa; +int rc; + + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + priv->fwnode = fwnode; + +/* Map the MMIO registers */ + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +if (!res) { +pr_err ("%pfwP: failed to get MMIO resource\n" , fwnode); +return -EINVAL ; + } + priv->regs =devm_ioremap (&pdev->dev, res->start,resource_size (res)); +if (!priv->regs) { +pr_err ("%pfwP: failed map MMIO registers\n" , fwnode); +return -ENOMEM ; + } + +/* +* Find out GSI base number +* +* Note: DT does not define "riscv,gsi-base" property so GSI +* base is always zero for DT. +*/ + rc =fwnode_property_read_u32_array (fwnode,"riscv,gsi-base" , + &priv->gsi_base,1 ); +if (rc) + priv->gsi_base =0 ; + +/* Find out number of interrupt sources */ + rc =fwnode_property_read_u32_array (fwnode,"riscv,num-sources" , + &priv->nr_irqs,1 ); +if (rc) { +pr_err ("%pfwP: failed to get number of interrupt sources\n" , + fwnode); +return rc; + } + +/* Setup initial state APLIC interrupts */ +aplic_init_hw_irqs (priv); + +/* +* Find out number of IDCs based on parent interrupts +* +* If "msi-parent" property is present then we ignore the +* APLIC IDCs which forces the APLIC driver to use MSI mode. +*/ +if (!fwnode_property_present (fwnode,"msi-parent" )) { +while (!fwnode_property_get_reference_args (fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , priv->nr_idcs, &parent)) + priv->nr_idcs++; + } + +/* Setup IDCs or MSIs based on number of IDCs */ +if (priv->nr_idcs) + rc =aplic_setup_idc (priv); +else + rc =aplic_setup_msi (priv); +if (rc) { +pr_err ("%pfwP: failed setup %s\n" , + fwnode, priv->nr_idcs ?"IDCs" :"MSIs" ); +return rc; + } + +/* Setup global config and interrupt delivery */ +aplic_init_hw_global (priv); + +/* Create irq domain instance for the APLIC */ +if (priv->nr_idcs) + priv->irqdomain =irq_domain_create_linear ( + priv->fwnode, + priv->nr_irqs +1 , + &aplic_irqdomain_idc_ops, + priv); +else + priv->irqdomain =platform_msi_create_device_domain ( + &pdev->dev, + priv->nr_irqs +1 , + aplic_msi_write_msg, + &aplic_irqdomain_msi_ops, + priv); +if (!priv->irqdomain) { +pr_err ("%pfwP: failed to add irq domain\n" , priv->fwnode); +return -ENOMEM ; + } + +/* Advertise the interrupt controller */ +if (priv->nr_idcs) { +pr_info ("%pfwP: %d interrupts directly connected to %d CPUs\n" , + priv->fwnode, priv->nr_irqs, priv->nr_idcs); + }else { + pa = priv->msicfg.base_ppn << APLIC_xMSICFGADDR_PPN_SHIFT; +pr_info ("%pfwP: %d interrupts forwared to MSI base %pa\n" , + priv->fwnode, priv->nr_irqs, &pa); + } + +return 0 ; +} + +static const struct of_device_id aplic_match[] = { + { .compatible ="riscv,aplic" }, + {} +}; + +static struct platform_driver aplic_driver = { + .driver = { + .name ="riscv-aplic" , + .of_match_table = aplic_match, + }, + .probe = aplic_probe, +}; +builtin_platform_driver (aplic_driver); + +static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 4 lines
drivers/irqchip/irq-riscv-aplic.c new file +4
-0+of_node_clear_flag (node,OF_POPULATED ); +return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue. But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Quoted 4 lines
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.Maybe I misread your previous comment.
You can easily reproduce the issue on QEMU virt machine for RISC-V:
1) Build qemu-system-riscv64 from latest QEMU master
2) Build kernel fromriscv_aia_v4branch at https://github.com/avpatel/linux.git
(Note: make sure you remove theFWNODE_FLAG_BEST_EFFORTflag from
APLIC driver at the time of building kernel)
3) Boot a APLIC-only system on QEMU virt machine
Quoted 5 lines
qemu-system-riscv64 -smp 4 -M virt,aia=aplic -m 1G -nographic \
-bios opensbi/build/platform/generic/firmware/fw_dynamic.bin \
-kernel ./build-riscv64/arch/riscv/boot/Image \
-append "root=/dev/ram rw console=ttyS0 earlycon" \
-initrd ./rootfs_riscv64.imgUnfortunately, I don't have the time to do all that, but I generally don't need to run something to figure out the issue. It's generally fairly obvious once I look at the DT. I'll also lean on you for some debug logs.
Where is the dts file that corresponds to this QEMU run? This is the third time I'm asking for a pointer to a dts file that has this issue, can you point me to it please? I shouldn't have to say this but: put it somewhere and point me to it please. Please don't point me to some git repo and ask me to dig around.
Can you give me details on what supplier is causing the deferred probe that's a problem for you? Any other details you can provide that'll help debug this issue?
Quoted 2 lines
I hope the above steps help you reproduce the issue. I will certainly test whatever fix you propose.
Do you plan to try the fix I suggested already? The one about using the correct macros?
-Saravana
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 866 lines · Saravana Kannan
Quoted 858 lines · Anup Patel
Quoted 843 lines · Saravana Kannan
Quoted 808 lines · Anup Patel
The RISC-V advanced interrupt architecture (AIA) specification defines a new interrupt controller for managing wired interrupts on a RISC-V platform. This new interrupt controller is referred to as advanced platform-level interrupt controller (APLIC) which can forward wired interrupts to CPUs (or HARTs) as local interrupts OR as message signaled interrupts.
(For more details refer https://github.com/riscv/riscv-aia)This patch adds an irqchip driver for RISC-V APLIC found on RISC-V platforms.
Signed-off-by Anup Patel <[email protected]>--- drivers/irqchip/Kconfig | 6 + drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++++++++++++++++++ include/linux/irqchip/riscv-aplic.h | 119 +++++ 4 files changed, 891 insertions(+) create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 include/linux/irqchip/riscv-aplic.hdrivers/irqchip/Kconfig +6
-0@@ -544,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_APLIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config RISCV_IMSIC bool depends on RISCVdrivers/irqchip/Makefile +1
-0@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic.o obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.odrivers/irqchip/irq-riscv-aplic.c new file +751
-0@@ -0,0 +1,765 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ + +#define pr_fmt (fmt) "riscv-aplic: " fmt +#include <linux/bitops.h> +#include <linux/cpu.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/irq.h> +#include <linux/irqchip.h> +#include <linux/irqchip/chained_irq.h> +#include <linux/irqchip/riscv-aplic.h> +#include <linux/irqchip/riscv-imsic.h> +#include <linux/irqdomain.h> +#include <linux/module.h> +#include <linux/msi.h> +#include <linux/platform_device.h> +#include <linux/smp.h> + +#define APLIC_DEFAULT_PRIORITY 1 +#define APLIC_DISABLE_IDELIVERY 0 +#define APLIC_ENABLE_IDELIVERY 1 +#define APLIC_DISABLE_ITHRESHOLD 1 +#define APLIC_ENABLE_ITHRESHOLD 0 + +struct aplic_msicfg { +phys_addr_t base_ppn; +u32 hhxs; +u32 hhxw; +u32 lhxs; +u32 lhxw; +}; + +struct aplic_idc { +unsigned int hart_index; +void __iomem *regs; +struct aplic_priv *priv; +}; + +struct aplic_priv { +struct fwnode_handle *fwnode; +u32 gsi_base; +u32 nr_irqs; +u32 nr_idcs; +void __iomem *regs; +struct irq_domain *irqdomain; +struct aplic_msicfg msicfg; +struct cpumask lmask; +}; + +static unsigned int aplic_idc_parent_irq; +static DEFINE_PER_CPU(struct aplic_idc, aplic_idcs); + +static void aplic_irq_unmask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_SETIENUM ); + +if (!priv->nr_idcs) +irq_chip_unmask_parent (d); +} + +static void aplic_irq_mask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_CLRIENUM ); + +if (!priv->nr_idcs) +irq_chip_mask_parent (d); +} + +static int aplic_set_type (struct irq_data *d,unsigned int type) +{ +u32 val =0 ; +void __iomem *sourcecfg; +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +switch (type) { +case IRQ_TYPE_NONE : + val =APLIC_SOURCECFG_SM_INACTIVE ; +break ; +case IRQ_TYPE_LEVEL_LOW : + val =APLIC_SOURCECFG_SM_LEVEL_LOW ; +break ; +case IRQ_TYPE_LEVEL_HIGH : + val =APLIC_SOURCECFG_SM_LEVEL_HIGH ; +break ; +case IRQ_TYPE_EDGE_FALLING : + val =APLIC_SOURCECFG_SM_EDGE_FALL ; +break ; +case IRQ_TYPE_EDGE_RISING : + val =APLIC_SOURCECFG_SM_EDGE_RISE ; +break ; +default : +return -EINVAL ; + } + + sourcecfg = priv->regs +APLIC_SOURCECFG_BASE ; + sourcecfg += (d->hwirq -1 ) *sizeof (u32); +writel (val, sourcecfg); + +return 0 ; +} + +static void aplic_irq_eoi (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +u32 reg_off, reg_mask; + +/* +* EOI handling only required only for level-triggered +* interrupts in APLIC MSI mode. +*/ + +if (priv->nr_idcs) +return ; + + reg_off =APLIC_CLRIP_BASE + ((d->hwirq /APLIC_IRQBITS_PER_REG ) *4 ); + reg_mask =BIT (d->hwirq %APLIC_IRQBITS_PER_REG ); +switch (irqd_get_trigger_type (d)) { +case IRQ_TYPE_LEVEL_LOW : +if (!(readl (priv->regs + reg_off) & reg_mask)) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; +case IRQ_TYPE_LEVEL_HIGH : +if (readl (priv->regs + reg_off) & reg_mask) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; + } +} + +#ifdef CONFIG_SMP +static int aplic_set_affinity (struct irq_data *d, +const struct cpumask *mask_val,bool force) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_idc *idc; +unsigned int cpu, val; +struct cpumask amask; +void __iomem *target; + +if (!priv->nr_idcs) +return irq_chip_set_affinity_parent (d, mask_val, force); + +cpumask_and (&amask, &priv->lmask, mask_val); + +if (force) + cpu =cpumask_first (&amask); +else + cpu =cpumask_any_and (&amask, cpu_online_mask); + +if (cpu >= nr_cpu_ids) +return -EINVAL ; + + idc =per_cpu_ptr (&aplic_idcs, cpu); + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +writel (val, target); + +irq_data_update_effective_affinity (d,cpumask_of (cpu)); + +return IRQ_SET_MASK_OK_DONE ; +} +#endif + +static struct irq_chip aplic_chip = { + .name ="RISC-V APLIC" , + .irq_mask = aplic_irq_mask, + .irq_unmask = aplic_irq_unmask, + .irq_set_type = aplic_set_type, + .irq_eoi = aplic_irq_eoi, +#ifdef CONFIG_SMP + .irq_set_affinity = aplic_set_affinity, +#endif + .flags =IRQCHIP_SET_TYPE_MASKED | +IRQCHIP_SKIP_SET_WAKE | +IRQCHIP_MASK_ON_SUSPEND , +}; + +static int aplic_irqdomain_translate (struct irq_fwspec *fwspec, +u32 gsi_base, +unsigned long *hwirq, +unsigned int *type) +{ +if (WARN_ON (fwspec->param_count <2 )) +return -EINVAL ; +if (WARN_ON (!fwspec->param[0 ])) +return -EINVAL ; + +/* For DT, gsi_base is always zero. */ + *hwirq = fwspec->param[0 ] - gsi_base; + *type = fwspec->param[1 ] &IRQ_TYPE_SENSE_MASK ; + +WARN_ON (*type ==IRQ_TYPE_NONE ); + +return 0 ; +} + +static int aplic_irqdomain_msi_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv =platform_msi_get_host_data (d); + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_msi_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv =platform_msi_get_host_data (domain); + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + + ret =platform_msi_device_domain_alloc (domain, virq, nr_irqs); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +/* +* APLIC does not implement irq_disable() so Linux interrupt +* subsystem will take a lazy approach for disabling an APLIC +* interrupt. This means APLIC interrupts are left unmasked +* upon system suspend and interrupts are not processed +* immediately upon system wake up. To tackle this, we disable +* the lazy approach for all APLIC interrupts. +*/ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_msi_ops = { + .translate = aplic_irqdomain_msi_translate, + .alloc = aplic_irqdomain_msi_alloc, + .free = platform_msi_device_domain_free, +}; + +static int aplic_irqdomain_idc_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv = d->host_data; + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_idc_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv = domain->host_data; + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +irq_set_affinity (virq + i, &priv->lmask); +/* See the reason described in aplic_irqdomain_msi_alloc() */ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_idc_ops = { + .translate = aplic_irqdomain_idc_translate, + .alloc = aplic_irqdomain_idc_alloc, + .free = irq_domain_free_irqs_top, +}; + +static void aplic_init_hw_irqs (struct aplic_priv *priv) +{ +int i; + +/* Disable all interrupts */ +for (i =0 ; i <= priv->nr_irqs; i +=32 ) +writel (-1U , priv->regs +APLIC_CLRIE_BASE + + (i /32 ) *sizeof (u32)); + +/* Set interrupt type and default priority for all interrupts */ +for (i =1 ; i <= priv->nr_irqs; i++) { +writel (0 , priv->regs +APLIC_SOURCECFG_BASE + + (i -1 ) *sizeof (u32)); +writel (APLIC_DEFAULT_PRIORITY , + priv->regs +APLIC_TARGET_BASE + + (i -1 ) *sizeof (u32)); + } + +/* Clear APLIC domaincfg */ +writel (0 , priv->regs +APLIC_DOMAINCFG ); +} + +static void aplic_init_hw_global (struct aplic_priv *priv) +{ +u32 val; +#ifdef CONFIG_RISCV_M_MODE +u32 valH; + +if (!priv->nr_idcs) { + val = priv->msicfg.base_ppn; + valH = (priv->msicfg.base_ppn >>32 ) & + APLIC_xMSICFGADDRH_BAPPN_MASK; + valH |= (priv->msicfg.lhxw & APLIC_xMSICFGADDRH_LHXW_MASK) + << APLIC_xMSICFGADDRH_LHXW_SHIFT; + valH |= (priv->msicfg.hhxw & APLIC_xMSICFGADDRH_HHXW_MASK) + << APLIC_xMSICFGADDRH_HHXW_SHIFT; + valH |= (priv->msicfg.lhxs & APLIC_xMSICFGADDRH_LHXS_MASK) + << APLIC_xMSICFGADDRH_LHXS_SHIFT; + valH |= (priv->msicfg.hhxs & APLIC_xMSICFGADDRH_HHXS_MASK) + << APLIC_xMSICFGADDRH_HHXS_SHIFT; +writel (val, priv->regs + APLIC_xMSICFGADDR); +writel (valH, priv->regs + APLIC_xMSICFGADDRH); + } +#endif + +/* Setup APLIC domaincfg register */ + val =readl (priv->regs +APLIC_DOMAINCFG ); + val |=APLIC_DOMAINCFG_IE ; +if (!priv->nr_idcs) + val |=APLIC_DOMAINCFG_DM ; +writel (val, priv->regs +APLIC_DOMAINCFG ); +if (readl (priv->regs +APLIC_DOMAINCFG ) != val) +pr_warn ("%pfwP: unable to write 0x%x in domaincfg\n" , + priv->fwnode, val); +} + +static void aplic_msi_write_msg (struct msi_desc *desc,struct msi_msg *msg) +{ +unsigned int group_index, hart_index, guest_index, val; +struct irq_data *d =irq_get_irq_data (desc->irq); +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_msicfg *mc = &priv->msicfg; +phys_addr_t tppn, tbppn, msg_addr; +void __iomem *target; + +/* For zeroed MSI, simply write zero into the target register */ +if (!msg->address_hi && !msg->address_lo && !msg->data) { + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); +writel (0 , target); +return ; + } + +/* Sanity check on message data */ +WARN_ON (msg->data >APLIC_TARGET_EIID_MASK ); + +/* Compute target MSI address */ + msg_addr = (((u64 )msg->address_hi) <<32 ) | msg->address_lo; + tppn = msg_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + +/* Compute target HART Base PPN */ + tbppn = tppn; + tbppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); +WARN_ON (tbppn != mc->base_ppn); + +/* Compute target group and hart indexes */ + group_index = (tppn >>APLIC_xMSICFGADDR_PPN_HHX_SHIFT (mc->hhxs)) & +APLIC_xMSICFGADDR_PPN_HHX_MASK (mc->hhxw); + hart_index = (tppn >>APLIC_xMSICFGADDR_PPN_LHX_SHIFT (mc->lhxs)) & +APLIC_xMSICFGADDR_PPN_LHX_MASK (mc->lhxw); + hart_index |= (group_index << mc->lhxw); +WARN_ON (hart_index >APLIC_TARGET_HART_IDX_MASK ); + +/* Compute target guest index */ + guest_index = tppn &APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); +WARN_ON (guest_index >APLIC_TARGET_GUEST_IDX_MASK ); + +/* Update IRQ TARGET register */ + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = (hart_index &APLIC_TARGET_HART_IDX_MASK ) + <<APLIC_TARGET_HART_IDX_SHIFT ; + val |= (guest_index &APLIC_TARGET_GUEST_IDX_MASK ) + <<APLIC_TARGET_GUEST_IDX_SHIFT ; + val |= (msg->data &APLIC_TARGET_EIID_MASK ); +writel (val, target); +} + +static int aplic_setup_msi (struct aplic_priv *priv) +{ +struct aplic_msicfg *mc = &priv->msicfg; +const struct imsic_global_config *imsic_global; + +/* +* The APLIC outgoing MSI config registers assume target MSI +* controller to be RISC-V AIA IMSIC controller. +*/ + imsic_global =imsic_get_global_config (); +if (!imsic_global) { +pr_err ("%pfwP: IMSIC global config not found\n" , + priv->fwnode); +return -ENODEV ; + } + +/* Find number of guest index bits (LHXS) */ + mc->lhxs = imsic_global->guest_index_bits; +if (APLIC_xMSICFGADDRH_LHXS_MASK < mc->lhxs) { +pr_err ("%pfwP: IMSIC guest index bits big for APLIC LHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of HART index bits (LHXW) */ + mc->lhxw = imsic_global->hart_index_bits; +if (APLIC_xMSICFGADDRH_LHXW_MASK < mc->lhxw) { +pr_err ("%pfwP: IMSIC hart index bits big for APLIC LHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of group index bits (HHXW) */ + mc->hhxw = imsic_global->group_index_bits; +if (APLIC_xMSICFGADDRH_HHXW_MASK < mc->hhxw) { +pr_err ("%pfwP: IMSIC group index bits big for APLIC HHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find first bit position of group index (HHXS) */ + mc->hhxs = imsic_global->group_index_shift; +if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) { +pr_err ("%pfwP: IMSIC group index shift should be >= %d\n" , + priv->fwnode, (2 * APLIC_xMSICFGADDR_PPN_SHIFT)); +return -EINVAL ; + } + mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT); +if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) { +pr_err ("%pfwP: IMSIC group index shift big for APLIC HHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Compute PPN base */ + mc->base_ppn = imsic_global->base_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); + +/* Use all possible CPUs as lmask */ +cpumask_copy (&priv->lmask, cpu_possible_mask); + +return 0 ; +} + +/* +* To handle an APLIC IDC interrupts, we just read the CLAIMI register +* which will return highest priority pending interrupt and clear the +* pending bit of the interrupt. This process is repeated until CLAIMI +* register return zero value. +*/ +static void aplic_idc_handle_irq (struct irq_desc *desc) +{ +struct aplic_idc *idc =this_cpu_ptr (&aplic_idcs); +struct irq_chip *chip =irq_desc_get_chip (desc); +irq_hw_number_t hw_irq; +int irq; + +chained_irq_enter (chip, desc); + +while ((hw_irq =readl (idc->regs +APLIC_IDC_CLAIMI ))) { + hw_irq = hw_irq >>APLIC_IDC_TOPI_ID_SHIFT ; + irq =irq_find_mapping (idc->priv->irqdomain, hw_irq); + +if (unlikely (irq <=0 )) +pr_warn_ratelimited ("hw_irq %lu mapping not found\n" , + hw_irq); +else +generic_handle_irq (irq); + } + +chained_irq_exit (chip, desc); +} + +static void aplic_idc_set_delivery (struct aplic_idc *idc,bool en) +{ +u32 de = (en) ?APLIC_ENABLE_IDELIVERY :APLIC_DISABLE_IDELIVERY ; +u32 th = (en) ?APLIC_ENABLE_ITHRESHOLD :APLIC_DISABLE_ITHRESHOLD ; + +/* Priority must be less than threshold for interrupt triggering */ +writel (th, idc->regs +APLIC_IDC_ITHRESHOLD ); + +/* Delivery must be set to 1 for interrupt triggering */ +writel (de, idc->regs +APLIC_IDC_IDELIVERY ); +} + +static int aplic_idc_dying_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +disable_percpu_irq (aplic_idc_parent_irq); + +return 0 ; +} + +static int aplic_idc_starting_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +enable_percpu_irq (aplic_idc_parent_irq, +irq_get_trigger_type (aplic_idc_parent_irq)); + +return 0 ; +} + +static int aplic_setup_idc (struct aplic_priv *priv) +{ +int i, j, rc, cpu, setup_count =0 ; +struct fwnode_reference_args parent; +struct irq_domain *domain; +unsigned long hartid; +struct aplic_idc *idc; +u32 val; + +/* Setup per-CPU IDC and target CPU mask */ +for (i =0 ; i < priv->nr_idcs; i++) { + rc =fwnode_property_get_reference_args (priv->fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , i, &parent); +if (rc) { +pr_warn ("%pfwP: parent irq for IDC%d not found\n" , + priv->fwnode, i); +continue ; + } + +/* +* Skip interrupts other than external interrupts for +* current privilege level. +*/ +if (parent.args[0 ] !=RV_IRQ_EXT ) +continue ; + + rc =riscv_fw_parent_hartid (parent.fwnode, &hartid); +if (rc) { +pr_warn ("%pfwP: invalid hartid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + + cpu =riscv_hartid_to_cpuid (hartid); +if (cpu <0 ) { +pr_warn ("%pfwP: invalid cpuid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + +cpumask_set_cpu (cpu, &priv->lmask); + + idc =per_cpu_ptr (&aplic_idcs, cpu); + idc->hart_index = i; + idc->regs = priv->regs +APLIC_IDC_BASE + i *APLIC_IDC_SIZE ; + idc->priv = priv; + +aplic_idc_set_delivery (idc, true); + +/* +* Boot cpu might not have APLIC hart_index = 0 so check +* and update target registers of all interrupts. +*/ +if (cpu ==smp_processor_id () && idc->hart_index) { + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +for (j =1 ; j <= priv->nr_irqs; j++) +writel (val, priv->regs +APLIC_TARGET_BASE + + (j -1 ) *sizeof (u32)); + } + + setup_count++; + } + +/* Find parent domain and register chained handler */ + domain =irq_find_matching_fwnode (riscv_get_intc_hwnode (), +DOMAIN_BUS_ANY ); +if (!aplic_idc_parent_irq && domain) { + aplic_idc_parent_irq =irq_create_mapping (domain,RV_IRQ_EXT ); +if (aplic_idc_parent_irq) { +irq_set_chained_handler (aplic_idc_parent_irq, + aplic_idc_handle_irq); + +/* +* Setup CPUHP notifier to enable IDC parent +* interrupt on all CPUs +*/ +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"irqchip/riscv/aplic:starting" , + aplic_idc_starting_cpu, + aplic_idc_dying_cpu); + } + } + +/* Fail if we were not able to setup IDC for any CPU */ +return (setup_count) ?0 : -ENODEV ; +} + +static int aplic_probe (struct platform_device *pdev) +{ +struct fwnode_handle *fwnode = pdev->dev.fwnode; +struct fwnode_reference_args parent; +struct aplic_priv *priv; +struct resource *res; +phys_addr_t pa; +int rc; + + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + priv->fwnode = fwnode; + +/* Map the MMIO registers */ + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +if (!res) { +pr_err ("%pfwP: failed to get MMIO resource\n" , fwnode); +return -EINVAL ; + } + priv->regs =devm_ioremap (&pdev->dev, res->start,resource_size (res)); +if (!priv->regs) { +pr_err ("%pfwP: failed map MMIO registers\n" , fwnode); +return -ENOMEM ; + } + +/* +* Find out GSI base number +* +* Note: DT does not define "riscv,gsi-base" property so GSI +* base is always zero for DT. +*/ + rc =fwnode_property_read_u32_array (fwnode,"riscv,gsi-base" , + &priv->gsi_base,1 ); +if (rc) + priv->gsi_base =0 ; + +/* Find out number of interrupt sources */ + rc =fwnode_property_read_u32_array (fwnode,"riscv,num-sources" , + &priv->nr_irqs,1 ); +if (rc) { +pr_err ("%pfwP: failed to get number of interrupt sources\n" , + fwnode); +return rc; + } + +/* Setup initial state APLIC interrupts */ +aplic_init_hw_irqs (priv); + +/* +* Find out number of IDCs based on parent interrupts +* +* If "msi-parent" property is present then we ignore the +* APLIC IDCs which forces the APLIC driver to use MSI mode. +*/ +if (!fwnode_property_present (fwnode,"msi-parent" )) { +while (!fwnode_property_get_reference_args (fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , priv->nr_idcs, &parent)) + priv->nr_idcs++; + } + +/* Setup IDCs or MSIs based on number of IDCs */ +if (priv->nr_idcs) + rc =aplic_setup_idc (priv); +else + rc =aplic_setup_msi (priv); +if (rc) { +pr_err ("%pfwP: failed setup %s\n" , + fwnode, priv->nr_idcs ?"IDCs" :"MSIs" ); +return rc; + } + +/* Setup global config and interrupt delivery */ +aplic_init_hw_global (priv); + +/* Create irq domain instance for the APLIC */ +if (priv->nr_idcs) + priv->irqdomain =irq_domain_create_linear ( + priv->fwnode, + priv->nr_irqs +1 , + &aplic_irqdomain_idc_ops, + priv); +else + priv->irqdomain =platform_msi_create_device_domain ( + &pdev->dev, + priv->nr_irqs +1 , + aplic_msi_write_msg, + &aplic_irqdomain_msi_ops, + priv); +if (!priv->irqdomain) { +pr_err ("%pfwP: failed to add irq domain\n" , priv->fwnode); +return -ENOMEM ; + } + +/* Advertise the interrupt controller */ +if (priv->nr_idcs) { +pr_info ("%pfwP: %d interrupts directly connected to %d CPUs\n" , + priv->fwnode, priv->nr_irqs, priv->nr_idcs); + }else { + pa = priv->msicfg.base_ppn << APLIC_xMSICFGADDR_PPN_SHIFT; +pr_info ("%pfwP: %d interrupts forwared to MSI base %pa\n" , + priv->fwnode, priv->nr_irqs, &pa); + } + +return 0 ; +} + +static const struct of_device_id aplic_match[] = { + { .compatible ="riscv,aplic" }, + {} +}; + +static struct platform_driver aplic_driver = { + .driver = { + .name ="riscv-aplic" , + .of_match_table = aplic_match, + }, + .probe = aplic_probe, +}; +builtin_platform_driver (aplic_driver); + +static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 4 lines
drivers/irqchip/irq-riscv-aplic.c new file +4
-0+of_node_clear_flag (node,OF_POPULATED ); +return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue. But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Quoted 4 lines
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.Maybe I misread your previous comment.
You can easily reproduce the issue on QEMU virt machine for RISC-V:
1) Build qemu-system-riscv64 from latest QEMU master
2) Build kernel fromriscv_aia_v4branch at https://github.com/avpatel/linux.git
(Note: make sure you remove theFWNODE_FLAG_BEST_EFFORTflag from
APLIC driver at the time of building kernel)
3) Boot a APLIC-only system on QEMU virt machineQuoted 5 lines
qemu-system-riscv64 -smp 4 -M virt,aia=aplic -m 1G -nographic \ -bios opensbi/build/platform/generic/firmware/fw_dynamic.bin \ -kernel ./build-riscv64/arch/riscv/boot/Image \ -append "root=/dev/ram rw console=ttyS0 earlycon" \ -initrd ./rootfs_riscv64.img
Quoted 4 lines
Unfortunately, I don't have the time to do all that, but I generally don't need to run something to figure out the issue. It's generally fairly obvious once I look at the DT. I'll also lean on you for some debug logs.
The boot log with FWNODE_BEST_EFFORT flag in APLIC can be found at:
https://drive.google.com/file/d/1C-uuHbh6Zk9xkAsfGLfhb_4WighvmQp1/view?usp=sharing
The boot log without FWNODE_BEST_EFFORT flag in APLIC can be found at:
https://drive.google.com/file/d/12SRdR-2Frv_5O06kbuI_LUJ88khjf_7O/view?usp=sharing
Quoted 6 lines
Where is the dts file that corresponds to this QEMU run? This is the third time I'm asking for a pointer to a dts file that has this issue, can you point me to it please? I shouldn't have to say this but: put it somewhere and point me to it please. Please don't point me to some git repo and ask me to dig around.
For QEMU virt machine, the DTB is generated at runtime as part of virt machine creation. The DTS dumped by QEMU using the "dumpdtb" command line option can be found at:
https://drive.google.com/file/d/1EU-exItL1B7EWuoXw4q-Ypocq--5Wvn8/view?usp=sharing
Quoted 4 lines
Can you give me details on what supplier is causing the deferred probe that's a problem for you? Any other details you can provide that'll help debug this issue?
FWNODE supplier for APLIC DT node is the OF framework.
Quoted 6 lines
Quoted 3 lines
I hope the above steps help you reproduce the issue. I will certainly test whatever fix you propose.
Do you plan to try the fix I suggested already? The one about using the correct macros?
You mean use IRQCHIP_DECLARE() in the APLIC driver ? or something else ?
Regards,
Anup
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 901 lines · Anup Patel
Quoted 866 lines · Saravana Kannan
Quoted 858 lines · Anup Patel
Quoted 843 lines · Saravana Kannan
Quoted 808 lines · Anup Patel
The RISC-V advanced interrupt architecture (AIA) specification defines a new interrupt controller for managing wired interrupts on a RISC-V platform. This new interrupt controller is referred to as advanced platform-level interrupt controller (APLIC) which can forward wired interrupts to CPUs (or HARTs) as local interrupts OR as message signaled interrupts.
(For more details refer https://github.com/riscv/riscv-aia)This patch adds an irqchip driver for RISC-V APLIC found on RISC-V platforms.
Signed-off-by Anup Patel <[email protected]>--- drivers/irqchip/Kconfig | 6 + drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++++++++++++++++++ include/linux/irqchip/riscv-aplic.h | 119 +++++ 4 files changed, 891 insertions(+) create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 include/linux/irqchip/riscv-aplic.hdrivers/irqchip/Kconfig +6
-0@@ -544,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_APLIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config RISCV_IMSIC bool depends on RISCVdrivers/irqchip/Makefile +1
-0@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic.o obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.odrivers/irqchip/irq-riscv-aplic.c new file +751
-0@@ -0,0 +1,765 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ + +#define pr_fmt (fmt) "riscv-aplic: " fmt +#include <linux/bitops.h> +#include <linux/cpu.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/irq.h> +#include <linux/irqchip.h> +#include <linux/irqchip/chained_irq.h> +#include <linux/irqchip/riscv-aplic.h> +#include <linux/irqchip/riscv-imsic.h> +#include <linux/irqdomain.h> +#include <linux/module.h> +#include <linux/msi.h> +#include <linux/platform_device.h> +#include <linux/smp.h> + +#define APLIC_DEFAULT_PRIORITY 1 +#define APLIC_DISABLE_IDELIVERY 0 +#define APLIC_ENABLE_IDELIVERY 1 +#define APLIC_DISABLE_ITHRESHOLD 1 +#define APLIC_ENABLE_ITHRESHOLD 0 + +struct aplic_msicfg { +phys_addr_t base_ppn; +u32 hhxs; +u32 hhxw; +u32 lhxs; +u32 lhxw; +}; + +struct aplic_idc { +unsigned int hart_index; +void __iomem *regs; +struct aplic_priv *priv; +}; + +struct aplic_priv { +struct fwnode_handle *fwnode; +u32 gsi_base; +u32 nr_irqs; +u32 nr_idcs; +void __iomem *regs; +struct irq_domain *irqdomain; +struct aplic_msicfg msicfg; +struct cpumask lmask; +}; + +static unsigned int aplic_idc_parent_irq; +static DEFINE_PER_CPU(struct aplic_idc, aplic_idcs); + +static void aplic_irq_unmask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_SETIENUM ); + +if (!priv->nr_idcs) +irq_chip_unmask_parent (d); +} + +static void aplic_irq_mask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_CLRIENUM ); + +if (!priv->nr_idcs) +irq_chip_mask_parent (d); +} + +static int aplic_set_type (struct irq_data *d,unsigned int type) +{ +u32 val =0 ; +void __iomem *sourcecfg; +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +switch (type) { +case IRQ_TYPE_NONE : + val =APLIC_SOURCECFG_SM_INACTIVE ; +break ; +case IRQ_TYPE_LEVEL_LOW : + val =APLIC_SOURCECFG_SM_LEVEL_LOW ; +break ; +case IRQ_TYPE_LEVEL_HIGH : + val =APLIC_SOURCECFG_SM_LEVEL_HIGH ; +break ; +case IRQ_TYPE_EDGE_FALLING : + val =APLIC_SOURCECFG_SM_EDGE_FALL ; +break ; +case IRQ_TYPE_EDGE_RISING : + val =APLIC_SOURCECFG_SM_EDGE_RISE ; +break ; +default : +return -EINVAL ; + } + + sourcecfg = priv->regs +APLIC_SOURCECFG_BASE ; + sourcecfg += (d->hwirq -1 ) *sizeof (u32); +writel (val, sourcecfg); + +return 0 ; +} + +static void aplic_irq_eoi (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +u32 reg_off, reg_mask; + +/* +* EOI handling only required only for level-triggered +* interrupts in APLIC MSI mode. +*/ + +if (priv->nr_idcs) +return ; + + reg_off =APLIC_CLRIP_BASE + ((d->hwirq /APLIC_IRQBITS_PER_REG ) *4 ); + reg_mask =BIT (d->hwirq %APLIC_IRQBITS_PER_REG ); +switch (irqd_get_trigger_type (d)) { +case IRQ_TYPE_LEVEL_LOW : +if (!(readl (priv->regs + reg_off) & reg_mask)) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; +case IRQ_TYPE_LEVEL_HIGH : +if (readl (priv->regs + reg_off) & reg_mask) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; + } +} + +#ifdef CONFIG_SMP +static int aplic_set_affinity (struct irq_data *d, +const struct cpumask *mask_val,bool force) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_idc *idc; +unsigned int cpu, val; +struct cpumask amask; +void __iomem *target; + +if (!priv->nr_idcs) +return irq_chip_set_affinity_parent (d, mask_val, force); + +cpumask_and (&amask, &priv->lmask, mask_val); + +if (force) + cpu =cpumask_first (&amask); +else + cpu =cpumask_any_and (&amask, cpu_online_mask); + +if (cpu >= nr_cpu_ids) +return -EINVAL ; + + idc =per_cpu_ptr (&aplic_idcs, cpu); + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +writel (val, target); + +irq_data_update_effective_affinity (d,cpumask_of (cpu)); + +return IRQ_SET_MASK_OK_DONE ; +} +#endif + +static struct irq_chip aplic_chip = { + .name ="RISC-V APLIC" , + .irq_mask = aplic_irq_mask, + .irq_unmask = aplic_irq_unmask, + .irq_set_type = aplic_set_type, + .irq_eoi = aplic_irq_eoi, +#ifdef CONFIG_SMP + .irq_set_affinity = aplic_set_affinity, +#endif + .flags =IRQCHIP_SET_TYPE_MASKED | +IRQCHIP_SKIP_SET_WAKE | +IRQCHIP_MASK_ON_SUSPEND , +}; + +static int aplic_irqdomain_translate (struct irq_fwspec *fwspec, +u32 gsi_base, +unsigned long *hwirq, +unsigned int *type) +{ +if (WARN_ON (fwspec->param_count <2 )) +return -EINVAL ; +if (WARN_ON (!fwspec->param[0 ])) +return -EINVAL ; + +/* For DT, gsi_base is always zero. */ + *hwirq = fwspec->param[0 ] - gsi_base; + *type = fwspec->param[1 ] &IRQ_TYPE_SENSE_MASK ; + +WARN_ON (*type ==IRQ_TYPE_NONE ); + +return 0 ; +} + +static int aplic_irqdomain_msi_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv =platform_msi_get_host_data (d); + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_msi_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv =platform_msi_get_host_data (domain); + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + + ret =platform_msi_device_domain_alloc (domain, virq, nr_irqs); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +/* +* APLIC does not implement irq_disable() so Linux interrupt +* subsystem will take a lazy approach for disabling an APLIC +* interrupt. This means APLIC interrupts are left unmasked +* upon system suspend and interrupts are not processed +* immediately upon system wake up. To tackle this, we disable +* the lazy approach for all APLIC interrupts. +*/ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_msi_ops = { + .translate = aplic_irqdomain_msi_translate, + .alloc = aplic_irqdomain_msi_alloc, + .free = platform_msi_device_domain_free, +}; + +static int aplic_irqdomain_idc_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv = d->host_data; + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_idc_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv = domain->host_data; + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +irq_set_affinity (virq + i, &priv->lmask); +/* See the reason described in aplic_irqdomain_msi_alloc() */ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_idc_ops = { + .translate = aplic_irqdomain_idc_translate, + .alloc = aplic_irqdomain_idc_alloc, + .free = irq_domain_free_irqs_top, +}; + +static void aplic_init_hw_irqs (struct aplic_priv *priv) +{ +int i; + +/* Disable all interrupts */ +for (i =0 ; i <= priv->nr_irqs; i +=32 ) +writel (-1U , priv->regs +APLIC_CLRIE_BASE + + (i /32 ) *sizeof (u32)); + +/* Set interrupt type and default priority for all interrupts */ +for (i =1 ; i <= priv->nr_irqs; i++) { +writel (0 , priv->regs +APLIC_SOURCECFG_BASE + + (i -1 ) *sizeof (u32)); +writel (APLIC_DEFAULT_PRIORITY , + priv->regs +APLIC_TARGET_BASE + + (i -1 ) *sizeof (u32)); + } + +/* Clear APLIC domaincfg */ +writel (0 , priv->regs +APLIC_DOMAINCFG ); +} + +static void aplic_init_hw_global (struct aplic_priv *priv) +{ +u32 val; +#ifdef CONFIG_RISCV_M_MODE +u32 valH; + +if (!priv->nr_idcs) { + val = priv->msicfg.base_ppn; + valH = (priv->msicfg.base_ppn >>32 ) & + APLIC_xMSICFGADDRH_BAPPN_MASK; + valH |= (priv->msicfg.lhxw & APLIC_xMSICFGADDRH_LHXW_MASK) + << APLIC_xMSICFGADDRH_LHXW_SHIFT; + valH |= (priv->msicfg.hhxw & APLIC_xMSICFGADDRH_HHXW_MASK) + << APLIC_xMSICFGADDRH_HHXW_SHIFT; + valH |= (priv->msicfg.lhxs & APLIC_xMSICFGADDRH_LHXS_MASK) + << APLIC_xMSICFGADDRH_LHXS_SHIFT; + valH |= (priv->msicfg.hhxs & APLIC_xMSICFGADDRH_HHXS_MASK) + << APLIC_xMSICFGADDRH_HHXS_SHIFT; +writel (val, priv->regs + APLIC_xMSICFGADDR); +writel (valH, priv->regs + APLIC_xMSICFGADDRH); + } +#endif + +/* Setup APLIC domaincfg register */ + val =readl (priv->regs +APLIC_DOMAINCFG ); + val |=APLIC_DOMAINCFG_IE ; +if (!priv->nr_idcs) + val |=APLIC_DOMAINCFG_DM ; +writel (val, priv->regs +APLIC_DOMAINCFG ); +if (readl (priv->regs +APLIC_DOMAINCFG ) != val) +pr_warn ("%pfwP: unable to write 0x%x in domaincfg\n" , + priv->fwnode, val); +} + +static void aplic_msi_write_msg (struct msi_desc *desc,struct msi_msg *msg) +{ +unsigned int group_index, hart_index, guest_index, val; +struct irq_data *d =irq_get_irq_data (desc->irq); +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_msicfg *mc = &priv->msicfg; +phys_addr_t tppn, tbppn, msg_addr; +void __iomem *target; + +/* For zeroed MSI, simply write zero into the target register */ +if (!msg->address_hi && !msg->address_lo && !msg->data) { + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); +writel (0 , target); +return ; + } + +/* Sanity check on message data */ +WARN_ON (msg->data >APLIC_TARGET_EIID_MASK ); + +/* Compute target MSI address */ + msg_addr = (((u64 )msg->address_hi) <<32 ) | msg->address_lo; + tppn = msg_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + +/* Compute target HART Base PPN */ + tbppn = tppn; + tbppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); +WARN_ON (tbppn != mc->base_ppn); + +/* Compute target group and hart indexes */ + group_index = (tppn >>APLIC_xMSICFGADDR_PPN_HHX_SHIFT (mc->hhxs)) & +APLIC_xMSICFGADDR_PPN_HHX_MASK (mc->hhxw); + hart_index = (tppn >>APLIC_xMSICFGADDR_PPN_LHX_SHIFT (mc->lhxs)) & +APLIC_xMSICFGADDR_PPN_LHX_MASK (mc->lhxw); + hart_index |= (group_index << mc->lhxw); +WARN_ON (hart_index >APLIC_TARGET_HART_IDX_MASK ); + +/* Compute target guest index */ + guest_index = tppn &APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); +WARN_ON (guest_index >APLIC_TARGET_GUEST_IDX_MASK ); + +/* Update IRQ TARGET register */ + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = (hart_index &APLIC_TARGET_HART_IDX_MASK ) + <<APLIC_TARGET_HART_IDX_SHIFT ; + val |= (guest_index &APLIC_TARGET_GUEST_IDX_MASK ) + <<APLIC_TARGET_GUEST_IDX_SHIFT ; + val |= (msg->data &APLIC_TARGET_EIID_MASK ); +writel (val, target); +} + +static int aplic_setup_msi (struct aplic_priv *priv) +{ +struct aplic_msicfg *mc = &priv->msicfg; +const struct imsic_global_config *imsic_global; + +/* +* The APLIC outgoing MSI config registers assume target MSI +* controller to be RISC-V AIA IMSIC controller. +*/ + imsic_global =imsic_get_global_config (); +if (!imsic_global) { +pr_err ("%pfwP: IMSIC global config not found\n" , + priv->fwnode); +return -ENODEV ; + } + +/* Find number of guest index bits (LHXS) */ + mc->lhxs = imsic_global->guest_index_bits; +if (APLIC_xMSICFGADDRH_LHXS_MASK < mc->lhxs) { +pr_err ("%pfwP: IMSIC guest index bits big for APLIC LHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of HART index bits (LHXW) */ + mc->lhxw = imsic_global->hart_index_bits; +if (APLIC_xMSICFGADDRH_LHXW_MASK < mc->lhxw) { +pr_err ("%pfwP: IMSIC hart index bits big for APLIC LHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of group index bits (HHXW) */ + mc->hhxw = imsic_global->group_index_bits; +if (APLIC_xMSICFGADDRH_HHXW_MASK < mc->hhxw) { +pr_err ("%pfwP: IMSIC group index bits big for APLIC HHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find first bit position of group index (HHXS) */ + mc->hhxs = imsic_global->group_index_shift; +if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) { +pr_err ("%pfwP: IMSIC group index shift should be >= %d\n" , + priv->fwnode, (2 * APLIC_xMSICFGADDR_PPN_SHIFT)); +return -EINVAL ; + } + mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT); +if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) { +pr_err ("%pfwP: IMSIC group index shift big for APLIC HHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Compute PPN base */ + mc->base_ppn = imsic_global->base_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); + +/* Use all possible CPUs as lmask */ +cpumask_copy (&priv->lmask, cpu_possible_mask); + +return 0 ; +} + +/* +* To handle an APLIC IDC interrupts, we just read the CLAIMI register +* which will return highest priority pending interrupt and clear the +* pending bit of the interrupt. This process is repeated until CLAIMI +* register return zero value. +*/ +static void aplic_idc_handle_irq (struct irq_desc *desc) +{ +struct aplic_idc *idc =this_cpu_ptr (&aplic_idcs); +struct irq_chip *chip =irq_desc_get_chip (desc); +irq_hw_number_t hw_irq; +int irq; + +chained_irq_enter (chip, desc); + +while ((hw_irq =readl (idc->regs +APLIC_IDC_CLAIMI ))) { + hw_irq = hw_irq >>APLIC_IDC_TOPI_ID_SHIFT ; + irq =irq_find_mapping (idc->priv->irqdomain, hw_irq); + +if (unlikely (irq <=0 )) +pr_warn_ratelimited ("hw_irq %lu mapping not found\n" , + hw_irq); +else +generic_handle_irq (irq); + } + +chained_irq_exit (chip, desc); +} + +static void aplic_idc_set_delivery (struct aplic_idc *idc,bool en) +{ +u32 de = (en) ?APLIC_ENABLE_IDELIVERY :APLIC_DISABLE_IDELIVERY ; +u32 th = (en) ?APLIC_ENABLE_ITHRESHOLD :APLIC_DISABLE_ITHRESHOLD ; + +/* Priority must be less than threshold for interrupt triggering */ +writel (th, idc->regs +APLIC_IDC_ITHRESHOLD ); + +/* Delivery must be set to 1 for interrupt triggering */ +writel (de, idc->regs +APLIC_IDC_IDELIVERY ); +} + +static int aplic_idc_dying_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +disable_percpu_irq (aplic_idc_parent_irq); + +return 0 ; +} + +static int aplic_idc_starting_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +enable_percpu_irq (aplic_idc_parent_irq, +irq_get_trigger_type (aplic_idc_parent_irq)); + +return 0 ; +} + +static int aplic_setup_idc (struct aplic_priv *priv) +{ +int i, j, rc, cpu, setup_count =0 ; +struct fwnode_reference_args parent; +struct irq_domain *domain; +unsigned long hartid; +struct aplic_idc *idc; +u32 val; + +/* Setup per-CPU IDC and target CPU mask */ +for (i =0 ; i < priv->nr_idcs; i++) { + rc =fwnode_property_get_reference_args (priv->fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , i, &parent); +if (rc) { +pr_warn ("%pfwP: parent irq for IDC%d not found\n" , + priv->fwnode, i); +continue ; + } + +/* +* Skip interrupts other than external interrupts for +* current privilege level. +*/ +if (parent.args[0 ] !=RV_IRQ_EXT ) +continue ; + + rc =riscv_fw_parent_hartid (parent.fwnode, &hartid); +if (rc) { +pr_warn ("%pfwP: invalid hartid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + + cpu =riscv_hartid_to_cpuid (hartid); +if (cpu <0 ) { +pr_warn ("%pfwP: invalid cpuid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + +cpumask_set_cpu (cpu, &priv->lmask); + + idc =per_cpu_ptr (&aplic_idcs, cpu); + idc->hart_index = i; + idc->regs = priv->regs +APLIC_IDC_BASE + i *APLIC_IDC_SIZE ; + idc->priv = priv; + +aplic_idc_set_delivery (idc, true); + +/* +* Boot cpu might not have APLIC hart_index = 0 so check +* and update target registers of all interrupts. +*/ +if (cpu ==smp_processor_id () && idc->hart_index) { + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +for (j =1 ; j <= priv->nr_irqs; j++) +writel (val, priv->regs +APLIC_TARGET_BASE + + (j -1 ) *sizeof (u32)); + } + + setup_count++; + } + +/* Find parent domain and register chained handler */ + domain =irq_find_matching_fwnode (riscv_get_intc_hwnode (), +DOMAIN_BUS_ANY ); +if (!aplic_idc_parent_irq && domain) { + aplic_idc_parent_irq =irq_create_mapping (domain,RV_IRQ_EXT ); +if (aplic_idc_parent_irq) { +irq_set_chained_handler (aplic_idc_parent_irq, + aplic_idc_handle_irq); + +/* +* Setup CPUHP notifier to enable IDC parent +* interrupt on all CPUs +*/ +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"irqchip/riscv/aplic:starting" , + aplic_idc_starting_cpu, + aplic_idc_dying_cpu); + } + } + +/* Fail if we were not able to setup IDC for any CPU */ +return (setup_count) ?0 : -ENODEV ; +} + +static int aplic_probe (struct platform_device *pdev) +{ +struct fwnode_handle *fwnode = pdev->dev.fwnode; +struct fwnode_reference_args parent; +struct aplic_priv *priv; +struct resource *res; +phys_addr_t pa; +int rc; + + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + priv->fwnode = fwnode; + +/* Map the MMIO registers */ + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +if (!res) { +pr_err ("%pfwP: failed to get MMIO resource\n" , fwnode); +return -EINVAL ; + } + priv->regs =devm_ioremap (&pdev->dev, res->start,resource_size (res)); +if (!priv->regs) { +pr_err ("%pfwP: failed map MMIO registers\n" , fwnode); +return -ENOMEM ; + } + +/* +* Find out GSI base number +* +* Note: DT does not define "riscv,gsi-base" property so GSI +* base is always zero for DT. +*/ + rc =fwnode_property_read_u32_array (fwnode,"riscv,gsi-base" , + &priv->gsi_base,1 ); +if (rc) + priv->gsi_base =0 ; + +/* Find out number of interrupt sources */ + rc =fwnode_property_read_u32_array (fwnode,"riscv,num-sources" , + &priv->nr_irqs,1 ); +if (rc) { +pr_err ("%pfwP: failed to get number of interrupt sources\n" , + fwnode); +return rc; + } + +/* Setup initial state APLIC interrupts */ +aplic_init_hw_irqs (priv); + +/* +* Find out number of IDCs based on parent interrupts +* +* If "msi-parent" property is present then we ignore the +* APLIC IDCs which forces the APLIC driver to use MSI mode. +*/ +if (!fwnode_property_present (fwnode,"msi-parent" )) { +while (!fwnode_property_get_reference_args (fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , priv->nr_idcs, &parent)) + priv->nr_idcs++; + } + +/* Setup IDCs or MSIs based on number of IDCs */ +if (priv->nr_idcs) + rc =aplic_setup_idc (priv); +else + rc =aplic_setup_msi (priv); +if (rc) { +pr_err ("%pfwP: failed setup %s\n" , + fwnode, priv->nr_idcs ?"IDCs" :"MSIs" ); +return rc; + } + +/* Setup global config and interrupt delivery */ +aplic_init_hw_global (priv); + +/* Create irq domain instance for the APLIC */ +if (priv->nr_idcs) + priv->irqdomain =irq_domain_create_linear ( + priv->fwnode, + priv->nr_irqs +1 , + &aplic_irqdomain_idc_ops, + priv); +else + priv->irqdomain =platform_msi_create_device_domain ( + &pdev->dev, + priv->nr_irqs +1 , + aplic_msi_write_msg, + &aplic_irqdomain_msi_ops, + priv); +if (!priv->irqdomain) { +pr_err ("%pfwP: failed to add irq domain\n" , priv->fwnode); +return -ENOMEM ; + } + +/* Advertise the interrupt controller */ +if (priv->nr_idcs) { +pr_info ("%pfwP: %d interrupts directly connected to %d CPUs\n" , + priv->fwnode, priv->nr_irqs, priv->nr_idcs); + }else { + pa = priv->msicfg.base_ppn << APLIC_xMSICFGADDR_PPN_SHIFT; +pr_info ("%pfwP: %d interrupts forwared to MSI base %pa\n" , + priv->fwnode, priv->nr_irqs, &pa); + } + +return 0 ; +} + +static const struct of_device_id aplic_match[] = { + { .compatible ="riscv,aplic" }, + {} +}; + +static struct platform_driver aplic_driver = { + .driver = { + .name ="riscv-aplic" , + .of_match_table = aplic_match, + }, + .probe = aplic_probe, +}; +builtin_platform_driver (aplic_driver); + +static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 4 lines
drivers/irqchip/irq-riscv-aplic.c new file +4
-0+of_node_clear_flag (node,OF_POPULATED ); +return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue. But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Quoted 4 lines
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.Maybe I misread your previous comment.
You can easily reproduce the issue on QEMU virt machine for RISC-V:
1) Build qemu-system-riscv64 from latest QEMU master
2) Build kernel fromriscv_aia_v4branch at https://github.com/avpatel/linux.git
(Note: make sure you remove theFWNODE_FLAG_BEST_EFFORTflag from
APLIC driver at the time of building kernel)
3) Boot a APLIC-only system on QEMU virt machineQuoted 5 lines
qemu-system-riscv64 -smp 4 -M virt,aia=aplic -m 1G -nographic \ -bios opensbi/build/platform/generic/firmware/fw_dynamic.bin \ -kernel ./build-riscv64/arch/riscv/boot/Image \ -append "root=/dev/ram rw console=ttyS0 earlycon" \ -initrd ./rootfs_riscv64.imgQuoted 4 lines
Unfortunately, I don't have the time to do all that, but I generally don't need to run something to figure out the issue. It's generally fairly obvious once I look at the DT. I'll also lean on you for some debug logs.
The boot log with
FWNODE_BEST_EFFORTflag in APLIC can be found at:
https://drive.google.com/file/d/1C-uuHbh6Zk9xkAsfGLfhb_4WighvmQp1/view?usp=sharingThe boot log without
FWNODE_BEST_EFFORTflag in APLIC can be found at:
https://drive.google.com/file/d/12SRdR-2Frv_5O06kbuI_LUJ88khjf_7O/view?usp=sharingQuoted 6 lines
Where is the dts file that corresponds to this QEMU run? This is the third time I'm asking for a pointer to a dts file that has this issue, can you point me to it please? I shouldn't have to say this but: put it somewhere and point me to it please. Please don't point me to some git repo and ask me to dig around.
For QEMU virt machine, the DTB is generated at runtime as part of virt machine creation. The DTS dumped by QEMU using the "dumpdtb" command line option can be found at:
https://drive.google.com/file/d/1EU-exItL1B7EWuoXw4q-Ypocq--5Wvn8/view?usp=sharingQuoted 4 lines
Can you give me details on what supplier is causing the deferred probe that's a problem for you? Any other details you can provide that'll help debug this issue?
FWNODE supplier for APLIC DT node is the OF framework.
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
I hope the above steps help you reproduce the issue. I will certainly test whatever fix you propose.
Do you plan to try the fix I suggested already? The one about using the correct macros?
You mean use
IRQCHIP_DECLARE()in the APLIC driver ? or something else ?
No. My previous email asking you to NOT use IRQCHIP_DECLARE() and instead use IRQCHIP_PLATFORM_DRIVER_BEGIN/END() macros.
-Saravana
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 906 lines · Saravana Kannan
Quoted 904 lines · Anup Patel
Quoted 866 lines · Saravana Kannan
Quoted 858 lines · Anup Patel
Quoted 843 lines · Saravana Kannan
Quoted 808 lines · Anup Patel
The RISC-V advanced interrupt architecture (AIA) specification defines a new interrupt controller for managing wired interrupts on a RISC-V platform. This new interrupt controller is referred to as advanced platform-level interrupt controller (APLIC) which can forward wired interrupts to CPUs (or HARTs) as local interrupts OR as message signaled interrupts.
(For more details refer https://github.com/riscv/riscv-aia)This patch adds an irqchip driver for RISC-V APLIC found on RISC-V platforms.
Signed-off-by Anup Patel <[email protected]>--- drivers/irqchip/Kconfig | 6 + drivers/irqchip/Makefile | 1 + drivers/irqchip/irq-riscv-aplic.c | 765 ++++++++++++++++++++++++++++ include/linux/irqchip/riscv-aplic.h | 119 +++++ 4 files changed, 891 insertions(+) create mode 100644 drivers/irqchip/irq-riscv-aplic.c create mode 100644 include/linux/irqchip/riscv-aplic.hdrivers/irqchip/Kconfig +6
-0@@ -544,6 +544,12 @@ config SIFIVE_PLIC select IRQ_DOMAIN_HIERARCHY select GENERIC_IRQ_EFFECTIVE_AFF_MASK if SMP +config RISCV_APLIC + bool + depends on RISCV + select IRQ_DOMAIN_HIERARCHY + select GENERIC_MSI_IRQ + config RISCV_IMSIC bool depends on RISCVdrivers/irqchip/Makefile +1
-0@@ -95,6 +95,7 @@ obj-$(CONFIG_QCOM_MPM) += irq-qcom-mpm.o obj-$(CONFIG_CSKY_MPINTC) += irq-csky-mpintc.o obj-$(CONFIG_CSKY_APB_INTC) += irq-csky-apb-intc.o obj-$(CONFIG_RISCV_INTC) += irq-riscv-intc.o +obj-$(CONFIG_RISCV_APLIC) += irq-riscv-aplic.o obj-$(CONFIG_RISCV_IMSIC) += irq-riscv-imsic.o obj-$(CONFIG_SIFIVE_PLIC) += irq-sifive-plic.o obj-$(CONFIG_IMX_IRQSTEER) += irq-imx-irqsteer.odrivers/irqchip/irq-riscv-aplic.c new file +751
-0@@ -0,0 +1,765 @@ +// SPDX-License-Identifier: GPL-2.0 +/* +* Copyright (C) 2021 Western Digital Corporation or its affiliates. +* Copyright (C) 2022 Ventana Micro Systems Inc. +*/ + +#define pr_fmt (fmt) "riscv-aplic: " fmt +#include <linux/bitops.h> +#include <linux/cpu.h> +#include <linux/interrupt.h> +#include <linux/io.h> +#include <linux/irq.h> +#include <linux/irqchip.h> +#include <linux/irqchip/chained_irq.h> +#include <linux/irqchip/riscv-aplic.h> +#include <linux/irqchip/riscv-imsic.h> +#include <linux/irqdomain.h> +#include <linux/module.h> +#include <linux/msi.h> +#include <linux/platform_device.h> +#include <linux/smp.h> + +#define APLIC_DEFAULT_PRIORITY 1 +#define APLIC_DISABLE_IDELIVERY 0 +#define APLIC_ENABLE_IDELIVERY 1 +#define APLIC_DISABLE_ITHRESHOLD 1 +#define APLIC_ENABLE_ITHRESHOLD 0 + +struct aplic_msicfg { +phys_addr_t base_ppn; +u32 hhxs; +u32 hhxw; +u32 lhxs; +u32 lhxw; +}; + +struct aplic_idc { +unsigned int hart_index; +void __iomem *regs; +struct aplic_priv *priv; +}; + +struct aplic_priv { +struct fwnode_handle *fwnode; +u32 gsi_base; +u32 nr_irqs; +u32 nr_idcs; +void __iomem *regs; +struct irq_domain *irqdomain; +struct aplic_msicfg msicfg; +struct cpumask lmask; +}; + +static unsigned int aplic_idc_parent_irq; +static DEFINE_PER_CPU(struct aplic_idc, aplic_idcs); + +static void aplic_irq_unmask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_SETIENUM ); + +if (!priv->nr_idcs) +irq_chip_unmask_parent (d); +} + +static void aplic_irq_mask (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +writel (d->hwirq, priv->regs +APLIC_CLRIENUM ); + +if (!priv->nr_idcs) +irq_chip_mask_parent (d); +} + +static int aplic_set_type (struct irq_data *d,unsigned int type) +{ +u32 val =0 ; +void __iomem *sourcecfg; +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); + +switch (type) { +case IRQ_TYPE_NONE : + val =APLIC_SOURCECFG_SM_INACTIVE ; +break ; +case IRQ_TYPE_LEVEL_LOW : + val =APLIC_SOURCECFG_SM_LEVEL_LOW ; +break ; +case IRQ_TYPE_LEVEL_HIGH : + val =APLIC_SOURCECFG_SM_LEVEL_HIGH ; +break ; +case IRQ_TYPE_EDGE_FALLING : + val =APLIC_SOURCECFG_SM_EDGE_FALL ; +break ; +case IRQ_TYPE_EDGE_RISING : + val =APLIC_SOURCECFG_SM_EDGE_RISE ; +break ; +default : +return -EINVAL ; + } + + sourcecfg = priv->regs +APLIC_SOURCECFG_BASE ; + sourcecfg += (d->hwirq -1 ) *sizeof (u32); +writel (val, sourcecfg); + +return 0 ; +} + +static void aplic_irq_eoi (struct irq_data *d) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +u32 reg_off, reg_mask; + +/* +* EOI handling only required only for level-triggered +* interrupts in APLIC MSI mode. +*/ + +if (priv->nr_idcs) +return ; + + reg_off =APLIC_CLRIP_BASE + ((d->hwirq /APLIC_IRQBITS_PER_REG ) *4 ); + reg_mask =BIT (d->hwirq %APLIC_IRQBITS_PER_REG ); +switch (irqd_get_trigger_type (d)) { +case IRQ_TYPE_LEVEL_LOW : +if (!(readl (priv->regs + reg_off) & reg_mask)) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; +case IRQ_TYPE_LEVEL_HIGH : +if (readl (priv->regs + reg_off) & reg_mask) +writel (d->hwirq, priv->regs +APLIC_SETIPNUM_LE ); +break ; + } +} + +#ifdef CONFIG_SMP +static int aplic_set_affinity (struct irq_data *d, +const struct cpumask *mask_val,bool force) +{ +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_idc *idc; +unsigned int cpu, val; +struct cpumask amask; +void __iomem *target; + +if (!priv->nr_idcs) +return irq_chip_set_affinity_parent (d, mask_val, force); + +cpumask_and (&amask, &priv->lmask, mask_val); + +if (force) + cpu =cpumask_first (&amask); +else + cpu =cpumask_any_and (&amask, cpu_online_mask); + +if (cpu >= nr_cpu_ids) +return -EINVAL ; + + idc =per_cpu_ptr (&aplic_idcs, cpu); + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +writel (val, target); + +irq_data_update_effective_affinity (d,cpumask_of (cpu)); + +return IRQ_SET_MASK_OK_DONE ; +} +#endif + +static struct irq_chip aplic_chip = { + .name ="RISC-V APLIC" , + .irq_mask = aplic_irq_mask, + .irq_unmask = aplic_irq_unmask, + .irq_set_type = aplic_set_type, + .irq_eoi = aplic_irq_eoi, +#ifdef CONFIG_SMP + .irq_set_affinity = aplic_set_affinity, +#endif + .flags =IRQCHIP_SET_TYPE_MASKED | +IRQCHIP_SKIP_SET_WAKE | +IRQCHIP_MASK_ON_SUSPEND , +}; + +static int aplic_irqdomain_translate (struct irq_fwspec *fwspec, +u32 gsi_base, +unsigned long *hwirq, +unsigned int *type) +{ +if (WARN_ON (fwspec->param_count <2 )) +return -EINVAL ; +if (WARN_ON (!fwspec->param[0 ])) +return -EINVAL ; + +/* For DT, gsi_base is always zero. */ + *hwirq = fwspec->param[0 ] - gsi_base; + *type = fwspec->param[1 ] &IRQ_TYPE_SENSE_MASK ; + +WARN_ON (*type ==IRQ_TYPE_NONE ); + +return 0 ; +} + +static int aplic_irqdomain_msi_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv =platform_msi_get_host_data (d); + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_msi_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv =platform_msi_get_host_data (domain); + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + + ret =platform_msi_device_domain_alloc (domain, virq, nr_irqs); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +/* +* APLIC does not implement irq_disable() so Linux interrupt +* subsystem will take a lazy approach for disabling an APLIC +* interrupt. This means APLIC interrupts are left unmasked +* upon system suspend and interrupts are not processed +* immediately upon system wake up. To tackle this, we disable +* the lazy approach for all APLIC interrupts. +*/ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_msi_ops = { + .translate = aplic_irqdomain_msi_translate, + .alloc = aplic_irqdomain_msi_alloc, + .free = platform_msi_device_domain_free, +}; + +static int aplic_irqdomain_idc_translate (struct irq_domain *d, +struct irq_fwspec *fwspec, +unsigned long *hwirq, +unsigned int *type) +{ +struct aplic_priv *priv = d->host_data; + +return aplic_irqdomain_translate (fwspec, priv->gsi_base, hwirq, type); +} + +static int aplic_irqdomain_idc_alloc (struct irq_domain *domain, +unsigned int virq,unsigned int nr_irqs, +void *arg) +{ +int i, ret; +unsigned int type; +irq_hw_number_t hwirq; +struct irq_fwspec *fwspec = arg; +struct aplic_priv *priv = domain->host_data; + + ret =aplic_irqdomain_translate (fwspec, priv->gsi_base, &hwirq, &type); +if (ret) +return ret; + +for (i =0 ; i < nr_irqs; i++) { +irq_domain_set_info (domain, virq + i, hwirq + i, + &aplic_chip, priv, handle_fasteoi_irq, +NULL ,NULL ); +irq_set_affinity (virq + i, &priv->lmask); +/* See the reason described in aplic_irqdomain_msi_alloc() */ +irq_set_status_flags (virq + i,IRQ_DISABLE_UNLAZY ); + } + +return 0 ; +} + +static const struct irq_domain_ops aplic_irqdomain_idc_ops = { + .translate = aplic_irqdomain_idc_translate, + .alloc = aplic_irqdomain_idc_alloc, + .free = irq_domain_free_irqs_top, +}; + +static void aplic_init_hw_irqs (struct aplic_priv *priv) +{ +int i; + +/* Disable all interrupts */ +for (i =0 ; i <= priv->nr_irqs; i +=32 ) +writel (-1U , priv->regs +APLIC_CLRIE_BASE + + (i /32 ) *sizeof (u32)); + +/* Set interrupt type and default priority for all interrupts */ +for (i =1 ; i <= priv->nr_irqs; i++) { +writel (0 , priv->regs +APLIC_SOURCECFG_BASE + + (i -1 ) *sizeof (u32)); +writel (APLIC_DEFAULT_PRIORITY , + priv->regs +APLIC_TARGET_BASE + + (i -1 ) *sizeof (u32)); + } + +/* Clear APLIC domaincfg */ +writel (0 , priv->regs +APLIC_DOMAINCFG ); +} + +static void aplic_init_hw_global (struct aplic_priv *priv) +{ +u32 val; +#ifdef CONFIG_RISCV_M_MODE +u32 valH; + +if (!priv->nr_idcs) { + val = priv->msicfg.base_ppn; + valH = (priv->msicfg.base_ppn >>32 ) & + APLIC_xMSICFGADDRH_BAPPN_MASK; + valH |= (priv->msicfg.lhxw & APLIC_xMSICFGADDRH_LHXW_MASK) + << APLIC_xMSICFGADDRH_LHXW_SHIFT; + valH |= (priv->msicfg.hhxw & APLIC_xMSICFGADDRH_HHXW_MASK) + << APLIC_xMSICFGADDRH_HHXW_SHIFT; + valH |= (priv->msicfg.lhxs & APLIC_xMSICFGADDRH_LHXS_MASK) + << APLIC_xMSICFGADDRH_LHXS_SHIFT; + valH |= (priv->msicfg.hhxs & APLIC_xMSICFGADDRH_HHXS_MASK) + << APLIC_xMSICFGADDRH_HHXS_SHIFT; +writel (val, priv->regs + APLIC_xMSICFGADDR); +writel (valH, priv->regs + APLIC_xMSICFGADDRH); + } +#endif + +/* Setup APLIC domaincfg register */ + val =readl (priv->regs +APLIC_DOMAINCFG ); + val |=APLIC_DOMAINCFG_IE ; +if (!priv->nr_idcs) + val |=APLIC_DOMAINCFG_DM ; +writel (val, priv->regs +APLIC_DOMAINCFG ); +if (readl (priv->regs +APLIC_DOMAINCFG ) != val) +pr_warn ("%pfwP: unable to write 0x%x in domaincfg\n" , + priv->fwnode, val); +} + +static void aplic_msi_write_msg (struct msi_desc *desc,struct msi_msg *msg) +{ +unsigned int group_index, hart_index, guest_index, val; +struct irq_data *d =irq_get_irq_data (desc->irq); +struct aplic_priv *priv =irq_data_get_irq_chip_data (d); +struct aplic_msicfg *mc = &priv->msicfg; +phys_addr_t tppn, tbppn, msg_addr; +void __iomem *target; + +/* For zeroed MSI, simply write zero into the target register */ +if (!msg->address_hi && !msg->address_lo && !msg->data) { + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); +writel (0 , target); +return ; + } + +/* Sanity check on message data */ +WARN_ON (msg->data >APLIC_TARGET_EIID_MASK ); + +/* Compute target MSI address */ + msg_addr = (((u64 )msg->address_hi) <<32 ) | msg->address_lo; + tppn = msg_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + +/* Compute target HART Base PPN */ + tbppn = tppn; + tbppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + tbppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); +WARN_ON (tbppn != mc->base_ppn); + +/* Compute target group and hart indexes */ + group_index = (tppn >>APLIC_xMSICFGADDR_PPN_HHX_SHIFT (mc->hhxs)) & +APLIC_xMSICFGADDR_PPN_HHX_MASK (mc->hhxw); + hart_index = (tppn >>APLIC_xMSICFGADDR_PPN_LHX_SHIFT (mc->lhxs)) & +APLIC_xMSICFGADDR_PPN_LHX_MASK (mc->lhxw); + hart_index |= (group_index << mc->lhxw); +WARN_ON (hart_index >APLIC_TARGET_HART_IDX_MASK ); + +/* Compute target guest index */ + guest_index = tppn &APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); +WARN_ON (guest_index >APLIC_TARGET_GUEST_IDX_MASK ); + +/* Update IRQ TARGET register */ + target = priv->regs +APLIC_TARGET_BASE ; + target += (d->hwirq -1 ) *sizeof (u32); + val = (hart_index &APLIC_TARGET_HART_IDX_MASK ) + <<APLIC_TARGET_HART_IDX_SHIFT ; + val |= (guest_index &APLIC_TARGET_GUEST_IDX_MASK ) + <<APLIC_TARGET_GUEST_IDX_SHIFT ; + val |= (msg->data &APLIC_TARGET_EIID_MASK ); +writel (val, target); +} + +static int aplic_setup_msi (struct aplic_priv *priv) +{ +struct aplic_msicfg *mc = &priv->msicfg; +const struct imsic_global_config *imsic_global; + +/* +* The APLIC outgoing MSI config registers assume target MSI +* controller to be RISC-V AIA IMSIC controller. +*/ + imsic_global =imsic_get_global_config (); +if (!imsic_global) { +pr_err ("%pfwP: IMSIC global config not found\n" , + priv->fwnode); +return -ENODEV ; + } + +/* Find number of guest index bits (LHXS) */ + mc->lhxs = imsic_global->guest_index_bits; +if (APLIC_xMSICFGADDRH_LHXS_MASK < mc->lhxs) { +pr_err ("%pfwP: IMSIC guest index bits big for APLIC LHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of HART index bits (LHXW) */ + mc->lhxw = imsic_global->hart_index_bits; +if (APLIC_xMSICFGADDRH_LHXW_MASK < mc->lhxw) { +pr_err ("%pfwP: IMSIC hart index bits big for APLIC LHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find number of group index bits (HHXW) */ + mc->hhxw = imsic_global->group_index_bits; +if (APLIC_xMSICFGADDRH_HHXW_MASK < mc->hhxw) { +pr_err ("%pfwP: IMSIC group index bits big for APLIC HHXW\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Find first bit position of group index (HHXS) */ + mc->hhxs = imsic_global->group_index_shift; +if (mc->hhxs < (2 * APLIC_xMSICFGADDR_PPN_SHIFT)) { +pr_err ("%pfwP: IMSIC group index shift should be >= %d\n" , + priv->fwnode, (2 * APLIC_xMSICFGADDR_PPN_SHIFT)); +return -EINVAL ; + } + mc->hhxs -= (2 * APLIC_xMSICFGADDR_PPN_SHIFT); +if (APLIC_xMSICFGADDRH_HHXS_MASK < mc->hhxs) { +pr_err ("%pfwP: IMSIC group index shift big for APLIC HHXS\n" , + priv->fwnode); +return -EINVAL ; + } + +/* Compute PPN base */ + mc->base_ppn = imsic_global->base_addr >> APLIC_xMSICFGADDR_PPN_SHIFT; + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HART (mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_LHX (mc->lhxw, mc->lhxs); + mc->base_ppn &= ~APLIC_xMSICFGADDR_PPN_HHX (mc->hhxw, mc->hhxs); + +/* Use all possible CPUs as lmask */ +cpumask_copy (&priv->lmask, cpu_possible_mask); + +return 0 ; +} + +/* +* To handle an APLIC IDC interrupts, we just read the CLAIMI register +* which will return highest priority pending interrupt and clear the +* pending bit of the interrupt. This process is repeated until CLAIMI +* register return zero value. +*/ +static void aplic_idc_handle_irq (struct irq_desc *desc) +{ +struct aplic_idc *idc =this_cpu_ptr (&aplic_idcs); +struct irq_chip *chip =irq_desc_get_chip (desc); +irq_hw_number_t hw_irq; +int irq; + +chained_irq_enter (chip, desc); + +while ((hw_irq =readl (idc->regs +APLIC_IDC_CLAIMI ))) { + hw_irq = hw_irq >>APLIC_IDC_TOPI_ID_SHIFT ; + irq =irq_find_mapping (idc->priv->irqdomain, hw_irq); + +if (unlikely (irq <=0 )) +pr_warn_ratelimited ("hw_irq %lu mapping not found\n" , + hw_irq); +else +generic_handle_irq (irq); + } + +chained_irq_exit (chip, desc); +} + +static void aplic_idc_set_delivery (struct aplic_idc *idc,bool en) +{ +u32 de = (en) ?APLIC_ENABLE_IDELIVERY :APLIC_DISABLE_IDELIVERY ; +u32 th = (en) ?APLIC_ENABLE_ITHRESHOLD :APLIC_DISABLE_ITHRESHOLD ; + +/* Priority must be less than threshold for interrupt triggering */ +writel (th, idc->regs +APLIC_IDC_ITHRESHOLD ); + +/* Delivery must be set to 1 for interrupt triggering */ +writel (de, idc->regs +APLIC_IDC_IDELIVERY ); +} + +static int aplic_idc_dying_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +disable_percpu_irq (aplic_idc_parent_irq); + +return 0 ; +} + +static int aplic_idc_starting_cpu (unsigned int cpu) +{ +if (aplic_idc_parent_irq) +enable_percpu_irq (aplic_idc_parent_irq, +irq_get_trigger_type (aplic_idc_parent_irq)); + +return 0 ; +} + +static int aplic_setup_idc (struct aplic_priv *priv) +{ +int i, j, rc, cpu, setup_count =0 ; +struct fwnode_reference_args parent; +struct irq_domain *domain; +unsigned long hartid; +struct aplic_idc *idc; +u32 val; + +/* Setup per-CPU IDC and target CPU mask */ +for (i =0 ; i < priv->nr_idcs; i++) { + rc =fwnode_property_get_reference_args (priv->fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , i, &parent); +if (rc) { +pr_warn ("%pfwP: parent irq for IDC%d not found\n" , + priv->fwnode, i); +continue ; + } + +/* +* Skip interrupts other than external interrupts for +* current privilege level. +*/ +if (parent.args[0 ] !=RV_IRQ_EXT ) +continue ; + + rc =riscv_fw_parent_hartid (parent.fwnode, &hartid); +if (rc) { +pr_warn ("%pfwP: invalid hartid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + + cpu =riscv_hartid_to_cpuid (hartid); +if (cpu <0 ) { +pr_warn ("%pfwP: invalid cpuid for IDC%d\n" , + priv->fwnode, i); +continue ; + } + +cpumask_set_cpu (cpu, &priv->lmask); + + idc =per_cpu_ptr (&aplic_idcs, cpu); + idc->hart_index = i; + idc->regs = priv->regs +APLIC_IDC_BASE + i *APLIC_IDC_SIZE ; + idc->priv = priv; + +aplic_idc_set_delivery (idc, true); + +/* +* Boot cpu might not have APLIC hart_index = 0 so check +* and update target registers of all interrupts. +*/ +if (cpu ==smp_processor_id () && idc->hart_index) { + val = idc->hart_index &APLIC_TARGET_HART_IDX_MASK ; + val <<=APLIC_TARGET_HART_IDX_SHIFT ; + val |=APLIC_DEFAULT_PRIORITY ; +for (j =1 ; j <= priv->nr_irqs; j++) +writel (val, priv->regs +APLIC_TARGET_BASE + + (j -1 ) *sizeof (u32)); + } + + setup_count++; + } + +/* Find parent domain and register chained handler */ + domain =irq_find_matching_fwnode (riscv_get_intc_hwnode (), +DOMAIN_BUS_ANY ); +if (!aplic_idc_parent_irq && domain) { + aplic_idc_parent_irq =irq_create_mapping (domain,RV_IRQ_EXT ); +if (aplic_idc_parent_irq) { +irq_set_chained_handler (aplic_idc_parent_irq, + aplic_idc_handle_irq); + +/* +* Setup CPUHP notifier to enable IDC parent +* interrupt on all CPUs +*/ +cpuhp_setup_state (CPUHP_AP_ONLINE_DYN , +"irqchip/riscv/aplic:starting" , + aplic_idc_starting_cpu, + aplic_idc_dying_cpu); + } + } + +/* Fail if we were not able to setup IDC for any CPU */ +return (setup_count) ?0 : -ENODEV ; +} + +static int aplic_probe (struct platform_device *pdev) +{ +struct fwnode_handle *fwnode = pdev->dev.fwnode; +struct fwnode_reference_args parent; +struct aplic_priv *priv; +struct resource *res; +phys_addr_t pa; +int rc; + + priv =devm_kzalloc (&pdev->dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + priv->fwnode = fwnode; + +/* Map the MMIO registers */ + res =platform_get_resource (pdev,IORESOURCE_MEM ,0 ); +if (!res) { +pr_err ("%pfwP: failed to get MMIO resource\n" , fwnode); +return -EINVAL ; + } + priv->regs =devm_ioremap (&pdev->dev, res->start,resource_size (res)); +if (!priv->regs) { +pr_err ("%pfwP: failed map MMIO registers\n" , fwnode); +return -ENOMEM ; + } + +/* +* Find out GSI base number +* +* Note: DT does not define "riscv,gsi-base" property so GSI +* base is always zero for DT. +*/ + rc =fwnode_property_read_u32_array (fwnode,"riscv,gsi-base" , + &priv->gsi_base,1 ); +if (rc) + priv->gsi_base =0 ; + +/* Find out number of interrupt sources */ + rc =fwnode_property_read_u32_array (fwnode,"riscv,num-sources" , + &priv->nr_irqs,1 ); +if (rc) { +pr_err ("%pfwP: failed to get number of interrupt sources\n" , + fwnode); +return rc; + } + +/* Setup initial state APLIC interrupts */ +aplic_init_hw_irqs (priv); + +/* +* Find out number of IDCs based on parent interrupts +* +* If "msi-parent" property is present then we ignore the +* APLIC IDCs which forces the APLIC driver to use MSI mode. +*/ +if (!fwnode_property_present (fwnode,"msi-parent" )) { +while (!fwnode_property_get_reference_args (fwnode, +"interrupts-extended" ,"#interrupt-cells" , +0 , priv->nr_idcs, &parent)) + priv->nr_idcs++; + } + +/* Setup IDCs or MSIs based on number of IDCs */ +if (priv->nr_idcs) + rc =aplic_setup_idc (priv); +else + rc =aplic_setup_msi (priv); +if (rc) { +pr_err ("%pfwP: failed setup %s\n" , + fwnode, priv->nr_idcs ?"IDCs" :"MSIs" ); +return rc; + } + +/* Setup global config and interrupt delivery */ +aplic_init_hw_global (priv); + +/* Create irq domain instance for the APLIC */ +if (priv->nr_idcs) + priv->irqdomain =irq_domain_create_linear ( + priv->fwnode, + priv->nr_irqs +1 , + &aplic_irqdomain_idc_ops, + priv); +else + priv->irqdomain =platform_msi_create_device_domain ( + &pdev->dev, + priv->nr_irqs +1 , + aplic_msi_write_msg, + &aplic_irqdomain_msi_ops, + priv); +if (!priv->irqdomain) { +pr_err ("%pfwP: failed to add irq domain\n" , priv->fwnode); +return -ENOMEM ; + } + +/* Advertise the interrupt controller */ +if (priv->nr_idcs) { +pr_info ("%pfwP: %d interrupts directly connected to %d CPUs\n" , + priv->fwnode, priv->nr_irqs, priv->nr_idcs); + }else { + pa = priv->msicfg.base_ppn << APLIC_xMSICFGADDR_PPN_SHIFT; +pr_info ("%pfwP: %d interrupts forwared to MSI base %pa\n" , + priv->fwnode, priv->nr_irqs, &pa); + } + +return 0 ; +} + +static const struct of_device_id aplic_match[] = { + { .compatible ="riscv,aplic" }, + {} +}; + +static struct platform_driver aplic_driver = { + .driver = { + .name ="riscv-aplic" , + .of_match_table = aplic_match, + }, + .probe = aplic_probe, +}; +builtin_platform_driver (aplic_driver); + +static int __init aplic_dt_init (struct device_node *node, +struct device_node *parent) +{ +/* +* The APLIC platform driver needs to be probed early drivers/irqchip/irq-riscv-aplic.c line 761
Quoted 10 lines
drivers/irqchip/irq-riscv-aplic.c new file +10
-0+* so for device tree: +* +* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which +* provides a hint to the device driver core to probe the +* platform driver early. +* 2) Clear the OF_POPULATED flag in device_node because +* of_irq_init() sets it which prevents creation of +* platform device. +*/ + node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ;NACK. You are blindly plastering flags without trying to understand the real issue and fixing this correctly.
drivers/irqchip/irq-riscv-aplic.c line 765
Quoted 4 lines
drivers/irqchip/irq-riscv-aplic.c new file +4
-0+of_node_clear_flag (node,OF_POPULATED ); +return 0 ; +} +IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init);This macro pretty much skips the entire driver core framework to probe and calls init and you are supposed to initialize the device when the init function is called.
If you want your device/driver to follow the proper platform driver path (which is recommended), then you need to use the
IRQCHIP_PLATFORM_DRIVER_BEGIN()and related macros. Grep for plenty of examples.I offered to help you debug this issue and I asked for a dts file that corresponds to a board you are testing this on and seeing an issue. But you haven't answered my question [1] and are pointing to some random commit and blaming it. That commit has no impact on any existing devices/drivers.
Quoted 4 lines
Hi Marc,
Please consider this patch Nacked as long as
FWNODE_FLAG_BEST_EFFORTis used or until Anup actually works with us to debug the real issue.Maybe I misread your previous comment.
You can easily reproduce the issue on QEMU virt machine for RISC-V:
1) Build qemu-system-riscv64 from latest QEMU master
2) Build kernel fromriscv_aia_v4branch at https://github.com/avpatel/linux.git
(Note: make sure you remove theFWNODE_FLAG_BEST_EFFORTflag from
APLIC driver at the time of building kernel)
3) Boot a APLIC-only system on QEMU virt machineQuoted 5 lines
qemu-system-riscv64 -smp 4 -M virt,aia=aplic -m 1G -nographic \ -bios opensbi/build/platform/generic/firmware/fw_dynamic.bin \ -kernel ./build-riscv64/arch/riscv/boot/Image \ -append "root=/dev/ram rw console=ttyS0 earlycon" \ -initrd ./rootfs_riscv64.imgQuoted 4 lines
Unfortunately, I don't have the time to do all that, but I generally don't need to run something to figure out the issue. It's generally fairly obvious once I look at the DT. I'll also lean on you for some debug logs.
The boot log with
FWNODE_BEST_EFFORTflag in APLIC can be found at:
https://drive.google.com/file/d/1C-uuHbh6Zk9xkAsfGLfhb_4WighvmQp1/view?usp=sharingThe boot log without
FWNODE_BEST_EFFORTflag in APLIC can be found at:
https://drive.google.com/file/d/12SRdR-2Frv_5O06kbuI_LUJ88khjf_7O/view?usp=sharingQuoted 6 lines
Where is the dts file that corresponds to this QEMU run? This is the third time I'm asking for a pointer to a dts file that has this issue, can you point me to it please? I shouldn't have to say this but: put it somewhere and point me to it please. Please don't point me to some git repo and ask me to dig around.
For QEMU virt machine, the DTB is generated at runtime as part of virt machine creation. The DTS dumped by QEMU using the "dumpdtb" command line option can be found at:
https://drive.google.com/file/d/1EU-exItL1B7EWuoXw4q-Ypocq--5Wvn8/view?usp=sharingQuoted 4 lines
Can you give me details on what supplier is causing the deferred probe that's a problem for you? Any other details you can provide that'll help debug this issue?
FWNODE supplier for APLIC DT node is the OF framework.
Quoted 4 lines
Quoted 3 lines
I hope the above steps help you reproduce the issue. I will certainly test whatever fix you propose.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Do you plan to try the fix I suggested already? The one about using the correct macros?
You mean use
IRQCHIP_DECLARE()in the APLIC driver ? or something else ?No. My previous email asking you to NOT use
IRQCHIP_DECLARE()and instead use IRQCHIP_PLATFORM_DRIVER_BEGIN/END() macros.
I tried IRQCHIP_PLATFORM_DRIVER_BEGIN/END() macros but these macros are not suitable for APLIC driver because we need platform device pointer in the APLIC probe() to create platform MSI device domain (refer, platform_msi_create_device_domain()).
Further, I tried setting the "suppress_bind_attrs" flag in "struct platform_driveraplic_driver" just like the IRQCHIP_PLATFORM_DRIVER_END() macro but this did not work.
Regards,
Anup
Re: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
[here, let me trim all of this nonsense...]
Quoted 7 lines · Anup Patel
Quoted 2 lines
No. My previous email asking you to NOT use
IRQCHIP_DECLARE()and instead use IRQCHIP_PLATFORM_DRIVER_BEGIN/END() macros.I tried IRQCHIP_PLATFORM_DRIVER_BEGIN/END() macros but these macros are not suitable for APLIC driver because we need platform device pointer in the APLIC
probe()to create platform MSI device domain (refer,platform_msi_create_device_domain()).
Oh come on. How hard have you tried? Have you even looked at the other drivers in the tree to see how they solve this insurmountable problem with a *single* line of code?
pdev = of_find_device_by_node(node);
That's it.
Quoted 3 lines
Further, I tried setting the "
suppress_bind_attrs" flag in "structplatform_driveraplic_driver" just like theIRQCHIP_PLATFORM_DRIVER_END()macro but this did not work.
I'm not sure how relevant this is to the conversation.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Anup Patel
maintainerRe: [PATCH v4 08/10] irqchip: Add RISC-V advanced PLIC driver
Quoted 13 lines · Marc Zyngier
[here, let me trim all of this nonsense...]
Quoted 7 lines · Anup Patel
Quoted 2 lines
No. My previous email asking you to NOT use
IRQCHIP_DECLARE()and instead use IRQCHIP_PLATFORM_DRIVER_BEGIN/END() macros.I tried IRQCHIP_PLATFORM_DRIVER_BEGIN/END() macros but these macros are not suitable for APLIC driver because we need platform device pointer in the APLIC
probe()to create platform MSI device domain (refer,platform_msi_create_device_domain()).
Quoted 7 lines
Oh come on. How hard have you tried? Have you even looked at the other drivers in the tree to see how they solve this insurmountable problem with a *single* line of code?
pdev = of_find_device_by_node(node);That's it.
Please see the below diff. I tried the same thing but still the APLIC does not get probed without the FWNODE_FLAG_BEST_EFFORT flag. Please note that the current APLIC driver works unmodified for both DT and ACPI but using of_find_device_by_node() here breaks ACPI support.
(quoted diff) +0 -0
b/drivers/irqchip/irq-riscv-aplic.c
index 1e710fdf5608..9ae9e7fb905f 100644
drivers/irqchip/irq-riscv-aplic.c +7 -30
@@ -17,6 +17,7 @@#include <linux/irqdomain.h> #include <linux/module.h> #include <linux/msi.h> +#include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/smp.h>
@@ -730,36 +731,12 @@ static int aplic_probe(struct platform_device *pdev)return 0 ; } -static const struct of_device_id aplic_match[] = { - { .compatible ="riscv,aplic" }, - {} -}; - -static struct platform_driver aplic_driver = { - .driver = { - .name ="riscv-aplic" , - .of_match_table = aplic_match, - }, - .probe = aplic_probe, -}; -builtin_platform_driver (aplic_driver); - -static int __init aplic_dt_init (struct device_node *node, +static int __init aplic_of_init (struct device_node *dn,struct device_node *parent) { -/* -* The APLIC platform driver needs to be probed early -* so for device tree: -* -* 1) Set the FWNODE_FLAG_BEST_EFFORT flag in fwnode which -* provides a hint to the device driver core to probe the -* platform driver early. -* 2) Clear the OF_POPULATED flag in device_node because -* of_irq_init() sets it which prevents creation of -* platform device. -*/ - node->fwnode.flags |=FWNODE_FLAG_BEST_EFFORT ; -of_node_clear_flag (node,OF_POPULATED ); -return 0 ; +return aplic_probe (of_find_device_by_node (dn)); } -IRQCHIP_DECLARE (riscv_aplic,"riscv,aplic" , aplic_dt_init); + +IRQCHIP_PLATFORM_DRIVER_BEGIN(aplic) +IRQCHIP_MATCH("riscv,aplic", aplic_of_init) +IRQCHIP_PLATFORM_DRIVER_END(aplic)
Quoted 6 lines
Quoted 4 lines
Further, I tried setting the "
suppress_bind_attrs" flag in "structplatform_driveraplic_driver" just like theIRQCHIP_PLATFORM_DRIVER_END()macro but this did not work.I'm not sure how relevant this is to the conversation.
It's relevant because the only difference in the platform_driver registered by IRQCHIP_PLATFORM_DRIVER_END() and
"struct platform_driver aplic_driver" is the "suppress_bind_attrs" flag.
Unfortunately, setting the "suppress_bind_attrs" flag does not help as well.
Quoted 5 lines
M.--
Without deviation from the norm, progress is not possible.
Regards,
Anup
PATCH v4 9/10 RISC-V: Select APLIC and IMSIC drivers
In reply to Anup PatelThe QEMU virt machine supports AIA emulation and we also have quite a few RISC-V platforms with AIA support under development so let us select APLIC and IMSIC drivers for all RISC-V platforms.
--- arch/riscv/Kconfig | 2 ++ 1 file changed, 2 insertions(+)
arch/riscv/Kconfig +2 -0
@@ -136,6 +136,8 @@ config RISCV select PCI_DOMAINS_GENERIC if PCI select PCI_MSI if PCI select RISCV_ALTERNATIVE if !XIP_KERNEL + select RISCV_APLIC + select RISCV_IMSIC select RISCV_INTC select RISCV_TIMER if RISCV_SBI select SIFIVE_PLIC
Signature · 1 line
2.34.1
PATCH v4 10/10 MAINTAINERS: Add entry for RISC-V AIA drivers
In reply to Anup PatelAdd myself as maintainer for RISC-V AIA drivers including the RISC-V INTC driver which supports both AIA and non-AIA platforms.
--- MAINTAINERS | 12 ++++++++++++ 1 file changed, 12 insertions(+)
MAINTAINERS +12 -0
@@ -18136,6 +18136,18 @@ S: Maintained F: drivers/mtd/nand/raw/r852.c F: drivers/mtd/nand/raw/r852.h +RISC-V AIA DRIVERS +M: Anup Patel <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/interrupt-controller/riscv,aplic.yaml +F: Documentation/devicetree/bindings/interrupt-controller/riscv,imsics.yaml +F: drivers/irqchip/irq-riscv-aplic.c +F: drivers/irqchip/irq-riscv-imsic.c +F: drivers/irqchip/irq-riscv-intc.c +F: include/linux/irqchip/riscv-aplic.h +F: include/linux/irqchip/riscv-imsic.h + RISC-V ARCHITECTURE M: Paul Walmsley <[email protected]> M: Palmer Dabbelt <[email protected]>
Signature · 1 line
2.34.1