Merge tag 'riscv-for-linus-5.20-mw0' of git://git.kernel.org/pub/scm/linux/kernel...
[platform/kernel/linux-starfive.git] / drivers / irqchip / irq-sifive-plic.c
index 4710d97..2f47848 100644 (file)
 #define        PLIC_DISABLE_THRESHOLD          0x7
 #define        PLIC_ENABLE_THRESHOLD           0
 
+#define PLIC_QUIRK_EDGE_INTERRUPT      0
+
 struct plic_priv {
        struct cpumask lmask;
        struct irq_domain *irqdomain;
        void __iomem *regs;
+       unsigned long plic_quirks;
 };
 
 struct plic_handler {
@@ -81,6 +84,8 @@ static int plic_parent_irq __ro_after_init;
 static bool plic_cpuhp_setup_done __ro_after_init;
 static DEFINE_PER_CPU(struct plic_handler, plic_handlers);
 
+static int plic_irq_set_type(struct irq_data *d, unsigned int type);
+
 static void __plic_toggle(void __iomem *enable_base, int hwirq, int enable)
 {
        u32 __iomem *reg = enable_base + (hwirq / 32) * sizeof(u32);
@@ -103,37 +108,43 @@ static inline void plic_irq_toggle(const struct cpumask *mask,
                                   struct irq_data *d, int enable)
 {
        int cpu;
-       struct plic_priv *priv = irq_data_get_irq_chip_data(d);
 
-       writel(enable, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
        for_each_cpu(cpu, mask) {
                struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
 
-               if (handler->present &&
-                   cpumask_test_cpu(cpu, &handler->priv->lmask))
-                       plic_toggle(handler, d->hwirq, enable);
+               plic_toggle(handler, d->hwirq, enable);
        }
 }
 
+static void plic_irq_enable(struct irq_data *d)
+{
+       plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 1);
+}
+
+static void plic_irq_disable(struct irq_data *d)
+{
+       plic_irq_toggle(irq_data_get_effective_affinity_mask(d), d, 0);
+}
+
 static void plic_irq_unmask(struct irq_data *d)
 {
-       struct cpumask amask;
-       unsigned int cpu;
        struct plic_priv *priv = irq_data_get_irq_chip_data(d);
 
-       cpumask_and(&amask, &priv->lmask, cpu_online_mask);
-       cpu = cpumask_any_and(irq_data_get_affinity_mask(d),
-                                          &amask);
-       if (WARN_ON_ONCE(cpu >= nr_cpu_ids))
-               return;
-       plic_irq_toggle(cpumask_of(cpu), d, 1);
+       writel(1, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
 }
 
 static void plic_irq_mask(struct irq_data *d)
 {
        struct plic_priv *priv = irq_data_get_irq_chip_data(d);
 
-       plic_irq_toggle(&priv->lmask, d, 0);
+       writel(0, priv->regs + PRIORITY_BASE + d->hwirq * PRIORITY_PER_ID);
+}
+
+static void plic_irq_eoi(struct irq_data *d)
+{
+       struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
+
+       writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
 }
 
 #ifdef CONFIG_SMP
@@ -154,38 +165,68 @@ static int plic_set_affinity(struct irq_data *d,
        if (cpu >= nr_cpu_ids)
                return -EINVAL;
 
-       plic_irq_toggle(&priv->lmask, d, 0);
-       plic_irq_toggle(cpumask_of(cpu), d, !irqd_irq_masked(d));
+       plic_irq_disable(d);
 
        irq_data_update_effective_affinity(d, cpumask_of(cpu));
 
+       if (!irqd_irq_disabled(d))
+               plic_irq_enable(d);
+
        return IRQ_SET_MASK_OK_DONE;
 }
 #endif
 
-static void plic_irq_eoi(struct irq_data *d)
-{
-       struct plic_handler *handler = this_cpu_ptr(&plic_handlers);
-
-       if (irqd_irq_masked(d)) {
-               plic_irq_unmask(d);
-               writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
-               plic_irq_mask(d);
-       } else {
-               writel(d->hwirq, handler->hart_base + CONTEXT_CLAIM);
-       }
-}
+static struct irq_chip plic_edge_chip = {
+       .name           = "SiFive PLIC",
+       .irq_enable     = plic_irq_enable,
+       .irq_disable    = plic_irq_disable,
+       .irq_ack        = plic_irq_eoi,
+       .irq_mask       = plic_irq_mask,
+       .irq_unmask     = plic_irq_unmask,
+#ifdef CONFIG_SMP
+       .irq_set_affinity = plic_set_affinity,
+#endif
+       .irq_set_type   = plic_irq_set_type,
+       .flags          = IRQCHIP_AFFINITY_PRE_STARTUP,
+};
 
 static struct irq_chip plic_chip = {
        .name           = "SiFive PLIC",
+       .irq_enable     = plic_irq_enable,
+       .irq_disable    = plic_irq_disable,
        .irq_mask       = plic_irq_mask,
        .irq_unmask     = plic_irq_unmask,
        .irq_eoi        = plic_irq_eoi,
 #ifdef CONFIG_SMP
        .irq_set_affinity = plic_set_affinity,
 #endif
+       .irq_set_type   = plic_irq_set_type,
+       .flags          = IRQCHIP_AFFINITY_PRE_STARTUP,
 };
 
+static int plic_irq_set_type(struct irq_data *d, unsigned int type)
+{
+       struct plic_priv *priv = irq_data_get_irq_chip_data(d);
+
+       if (!test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
+               return IRQ_SET_MASK_OK_NOCOPY;
+
+       switch (type) {
+       case IRQ_TYPE_EDGE_RISING:
+               irq_set_chip_handler_name_locked(d, &plic_edge_chip,
+                                                handle_edge_irq, NULL);
+               break;
+       case IRQ_TYPE_LEVEL_HIGH:
+               irq_set_chip_handler_name_locked(d, &plic_chip,
+                                                handle_fasteoi_irq, NULL);
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       return IRQ_SET_MASK_OK;
+}
+
 static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
                              irq_hw_number_t hwirq)
 {
@@ -198,6 +239,19 @@ static int plic_irqdomain_map(struct irq_domain *d, unsigned int irq,
        return 0;
 }
 
+static int plic_irq_domain_translate(struct irq_domain *d,
+                                    struct irq_fwspec *fwspec,
+                                    unsigned long *hwirq,
+                                    unsigned int *type)
+{
+       struct plic_priv *priv = d->host_data;
+
+       if (test_bit(PLIC_QUIRK_EDGE_INTERRUPT, &priv->plic_quirks))
+               return irq_domain_translate_twocell(d, fwspec, hwirq, type);
+
+       return irq_domain_translate_onecell(d, fwspec, hwirq, type);
+}
+
 static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
                                 unsigned int nr_irqs, void *arg)
 {
@@ -206,7 +260,7 @@ static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
        unsigned int type;
        struct irq_fwspec *fwspec = arg;
 
-       ret = irq_domain_translate_onecell(domain, fwspec, &hwirq, &type);
+       ret = plic_irq_domain_translate(domain, fwspec, &hwirq, &type);
        if (ret)
                return ret;
 
@@ -220,7 +274,7 @@ static int plic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
 }
 
 static const struct irq_domain_ops plic_irqdomain_ops = {
-       .translate      = irq_domain_translate_onecell,
+       .translate      = plic_irq_domain_translate,
        .alloc          = plic_irq_domain_alloc,
        .free           = irq_domain_free_irqs_top,
 };
@@ -281,8 +335,9 @@ static int plic_starting_cpu(unsigned int cpu)
        return 0;
 }
 
-static int __init plic_init(struct device_node *node,
-               struct device_node *parent)
+static int __init __plic_init(struct device_node *node,
+                             struct device_node *parent,
+                             unsigned long plic_quirks)
 {
        int error = 0, nr_contexts, nr_handlers = 0, i;
        u32 nr_irqs;
@@ -293,6 +348,8 @@ static int __init plic_init(struct device_node *node,
        if (!priv)
                return -ENOMEM;
 
+       priv->plic_quirks = plic_quirks;
+
        priv->regs = of_iomap(node, 0);
        if (WARN_ON(!priv->regs)) {
                error = -EIO;
@@ -383,8 +440,11 @@ static int __init plic_init(struct device_node *node,
                        i * CONTEXT_ENABLE_SIZE;
                handler->priv = priv;
 done:
-               for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
+               for (hwirq = 1; hwirq <= nr_irqs; hwirq++) {
                        plic_toggle(handler, hwirq, 0);
+                       writel(1, priv->regs + PRIORITY_BASE +
+                                 hwirq * PRIORITY_PER_ID);
+               }
                nr_handlers++;
        }
 
@@ -411,6 +471,20 @@ out_free_priv:
        return error;
 }
 
+static int __init plic_init(struct device_node *node,
+                           struct device_node *parent)
+{
+       return __plic_init(node, parent, 0);
+}
+
 IRQCHIP_DECLARE(sifive_plic, "sifive,plic-1.0.0", plic_init);
 IRQCHIP_DECLARE(riscv_plic0, "riscv,plic0", plic_init); /* for legacy systems */
-IRQCHIP_DECLARE(thead_c900_plic, "thead,c900-plic", plic_init); /* for firmware driver */
+
+static int __init plic_edge_init(struct device_node *node,
+                                struct device_node *parent)
+{
+       return __plic_init(node, parent, BIT(PLIC_QUIRK_EDGE_INTERRUPT));
+}
+
+IRQCHIP_DECLARE(andestech_nceplic100, "andestech,nceplic100", plic_edge_init);
+IRQCHIP_DECLARE(thead_c900_plic, "thead,c900-plic", plic_edge_init);