|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Copyright (C) 2012 Regents of the University of California |
| 4 | + * Copyright (C) 2017-2018 SiFive |
| 5 | + * Copyright (C) 2020 Western Digital Corporation or its affiliates. |
| 6 | + */ |
| 7 | + |
| 8 | +#define pr_fmt(fmt) "riscv-intc: " fmt |
| 9 | +#include <linux/atomic.h> |
| 10 | +#include <linux/bits.h> |
| 11 | +#include <linux/cpu.h> |
| 12 | +#include <linux/irq.h> |
| 13 | +#include <linux/irqchip.h> |
| 14 | +#include <linux/irqdomain.h> |
| 15 | +#include <linux/interrupt.h> |
| 16 | +#include <linux/module.h> |
| 17 | +#include <linux/of.h> |
| 18 | +#include <linux/smp.h> |
| 19 | + |
| 20 | +static struct irq_domain *intc_domain; |
| 21 | + |
| 22 | +static asmlinkage void riscv_intc_irq(struct pt_regs *regs) |
| 23 | +{ |
| 24 | + struct pt_regs *old_regs; |
| 25 | + unsigned long cause = regs->cause & ~CAUSE_IRQ_FLAG; |
| 26 | + |
| 27 | + if (unlikely(cause >= BITS_PER_LONG)) |
| 28 | + panic("unexpected interrupt cause"); |
| 29 | + |
| 30 | + switch (cause) { |
| 31 | + case RV_IRQ_TIMER: |
| 32 | + old_regs = set_irq_regs(regs); |
| 33 | + irq_enter(); |
| 34 | + riscv_timer_interrupt(); |
| 35 | + irq_exit(); |
| 36 | + set_irq_regs(old_regs); |
| 37 | + break; |
| 38 | +#ifdef CONFIG_SMP |
| 39 | + case RV_IRQ_SOFT: |
| 40 | + /* |
| 41 | + * We only use software interrupts to pass IPIs, so if a |
| 42 | + * non-SMP system gets one, then we don't know what to do. |
| 43 | + */ |
| 44 | + handle_IPI(regs); |
| 45 | + break; |
| 46 | +#endif |
| 47 | + default: |
| 48 | + handle_domain_irq(intc_domain, cause, regs); |
| 49 | + break; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/* |
| 54 | + * On RISC-V systems local interrupts are masked or unmasked by writing |
| 55 | + * the SIE (Supervisor Interrupt Enable) CSR. As CSRs can only be written |
| 56 | + * on the local hart, these functions can only be called on the hart that |
| 57 | + * corresponds to the IRQ chip. |
| 58 | + */ |
| 59 | + |
| 60 | +static void riscv_intc_irq_mask(struct irq_data *d) |
| 61 | +{ |
| 62 | + csr_clear(CSR_IE, BIT(d->hwirq)); |
| 63 | +} |
| 64 | + |
| 65 | +static void riscv_intc_irq_unmask(struct irq_data *d) |
| 66 | +{ |
| 67 | + csr_set(CSR_IE, BIT(d->hwirq)); |
| 68 | +} |
| 69 | + |
| 70 | +static int riscv_intc_cpu_starting(unsigned int cpu) |
| 71 | +{ |
| 72 | + csr_set(CSR_IE, BIT(RV_IRQ_SOFT)); |
| 73 | + return 0; |
| 74 | +} |
| 75 | + |
| 76 | +static int riscv_intc_cpu_dying(unsigned int cpu) |
| 77 | +{ |
| 78 | + csr_clear(CSR_IE, BIT(RV_IRQ_SOFT)); |
| 79 | + return 0; |
| 80 | +} |
| 81 | + |
| 82 | +static struct irq_chip riscv_intc_chip = { |
| 83 | + .name = "RISC-V INTC", |
| 84 | + .irq_mask = riscv_intc_irq_mask, |
| 85 | + .irq_unmask = riscv_intc_irq_unmask, |
| 86 | +}; |
| 87 | + |
| 88 | +static int riscv_intc_domain_map(struct irq_domain *d, unsigned int irq, |
| 89 | + irq_hw_number_t hwirq) |
| 90 | +{ |
| 91 | + irq_set_percpu_devid(irq); |
| 92 | + irq_domain_set_info(d, irq, hwirq, &riscv_intc_chip, d->host_data, |
| 93 | + handle_percpu_devid_irq, NULL, NULL); |
| 94 | + |
| 95 | + return 0; |
| 96 | +} |
| 97 | + |
| 98 | +static const struct irq_domain_ops riscv_intc_domain_ops = { |
| 99 | + .map = riscv_intc_domain_map, |
| 100 | + .xlate = irq_domain_xlate_onecell, |
| 101 | +}; |
| 102 | + |
| 103 | +static int __init riscv_intc_init(struct device_node *node, |
| 104 | + struct device_node *parent) |
| 105 | +{ |
| 106 | + int rc, hartid; |
| 107 | + |
| 108 | + hartid = riscv_of_parent_hartid(node); |
| 109 | + if (hartid < 0) { |
| 110 | + pr_warn("unable to fine hart id for %pOF\n", node); |
| 111 | + return 0; |
| 112 | + } |
| 113 | + |
| 114 | + /* |
| 115 | + * The DT will have one INTC DT node under each CPU (or HART) |
| 116 | + * DT node so riscv_intc_init() function will be called once |
| 117 | + * for each INTC DT node. We only need to do INTC initialization |
| 118 | + * for the INTC DT node belonging to boot CPU (or boot HART). |
| 119 | + */ |
| 120 | + if (riscv_hartid_to_cpuid(hartid) != smp_processor_id()) |
| 121 | + return 0; |
| 122 | + |
| 123 | + intc_domain = irq_domain_add_linear(node, BITS_PER_LONG, |
| 124 | + &riscv_intc_domain_ops, NULL); |
| 125 | + if (!intc_domain) { |
| 126 | + pr_err("unable to add IRQ domain\n"); |
| 127 | + return -ENXIO; |
| 128 | + } |
| 129 | + |
| 130 | + rc = set_handle_irq(&riscv_intc_irq); |
| 131 | + if (rc) { |
| 132 | + pr_err("failed to set irq handler\n"); |
| 133 | + return rc; |
| 134 | + } |
| 135 | + |
| 136 | + cpuhp_setup_state(CPUHP_AP_IRQ_RISCV_STARTING, |
| 137 | + "irqchip/riscv/intc:starting", |
| 138 | + riscv_intc_cpu_starting, |
| 139 | + riscv_intc_cpu_dying); |
| 140 | + |
| 141 | + pr_info("%d local interrupts mapped\n", BITS_PER_LONG); |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +IRQCHIP_DECLARE(riscv, "riscv,cpu-intc", riscv_intc_init); |
0 commit comments