···11+/*22+ * Copyright (C) 2002 ARM Limited, All Rights Reserved.33+ *44+ * This program is free software; you can redistribute it and/or modify55+ * it under the terms of the GNU General Public License version 2 as66+ * published by the Free Software Foundation.77+ *88+ * This program is distributed in the hope that it will be useful,99+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1010+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1111+ * GNU General Public License for more details.1212+ *1313+ * You should have received a copy of the GNU General Public License1414+ * along with this program. If not, see <http://www.gnu.org/licenses/>.1515+ */1616+1717+#include <linux/interrupt.h>1818+#include <linux/io.h>1919+#include <linux/irq.h>2020+#include <linux/irqchip/arm-gic.h>2121+2222+#include "irq-gic-common.h"2323+2424+void gic_configure_irq(unsigned int irq, unsigned int type,2525+ void __iomem *base, void (*sync_access)(void))2626+{2727+ u32 enablemask = 1 << (irq % 32);2828+ u32 enableoff = (irq / 32) * 4;2929+ u32 confmask = 0x2 << ((irq % 16) * 2);3030+ u32 confoff = (irq / 16) * 4;3131+ bool enabled = false;3232+ u32 val;3333+3434+ /*3535+ * Read current configuration register, and insert the config3636+ * for "irq", depending on "type".3737+ */3838+ val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);3939+ if (type == IRQ_TYPE_LEVEL_HIGH)4040+ val &= ~confmask;4141+ else if (type == IRQ_TYPE_EDGE_RISING)4242+ val |= confmask;4343+4444+ /*4545+ * As recommended by the spec, disable the interrupt before changing4646+ * the configuration4747+ */4848+ if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {4949+ writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);5050+ if (sync_access)5151+ sync_access();5252+ enabled = true;5353+ }5454+5555+ /*5656+ * Write back the new configuration, and possibly re-enable5757+ * the interrupt.5858+ */5959+ writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);6060+6161+ if (enabled)6262+ writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);6363+6464+ if (sync_access)6565+ sync_access();6666+}6767+6868+void __init gic_dist_config(void __iomem *base, int gic_irqs,6969+ void (*sync_access)(void))7070+{7171+ unsigned int i;7272+7373+ /*7474+ * Set all global interrupts to be level triggered, active low.7575+ */7676+ for (i = 32; i < gic_irqs; i += 16)7777+ writel_relaxed(0, base + GIC_DIST_CONFIG + i / 4);7878+7979+ /*8080+ * Set priority on all global interrupts.8181+ */8282+ for (i = 32; i < gic_irqs; i += 4)8383+ writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i);8484+8585+ /*8686+ * Disable all interrupts. Leave the PPI and SGIs alone8787+ * as they are enabled by redistributor registers.8888+ */8989+ for (i = 32; i < gic_irqs; i += 32)9090+ writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i / 8);9191+9292+ if (sync_access)9393+ sync_access();9494+}9595+9696+void gic_cpu_config(void __iomem *base, void (*sync_access)(void))9797+{9898+ int i;9999+100100+ /*101101+ * Deal with the banked PPI and SGI interrupts - disable all102102+ * PPI interrupts, ensure all SGI interrupts are enabled.103103+ */104104+ writel_relaxed(0xffff0000, base + GIC_DIST_ENABLE_CLEAR);105105+ writel_relaxed(0x0000ffff, base + GIC_DIST_ENABLE_SET);106106+107107+ /*108108+ * Set priority on PPI and SGI interrupts109109+ */110110+ for (i = 0; i < 32; i += 4)111111+ writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);112112+113113+ if (sync_access)114114+ sync_access();115115+}
+29
drivers/irqchip/irq-gic-common.h
···11+/*22+ * Copyright (C) 2002 ARM Limited, All Rights Reserved.33+ *44+ * This program is free software; you can redistribute it and/or modify55+ * it under the terms of the GNU General Public License version 2 as66+ * published by the Free Software Foundation.77+ *88+ * This program is distributed in the hope that it will be useful,99+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1010+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1111+ * GNU General Public License for more details.1212+ *1313+ * You should have received a copy of the GNU General Public License1414+ * along with this program. If not, see <http://www.gnu.org/licenses/>.1515+ */1616+1717+#ifndef _IRQ_GIC_COMMON_H1818+#define _IRQ_GIC_COMMON_H1919+2020+#include <linux/of.h>2121+#include <linux/irqdomain.h>2222+2323+void gic_configure_irq(unsigned int irq, unsigned int type,2424+ void __iomem *base, void (*sync_access)(void));2525+void gic_dist_config(void __iomem *base, int gic_irqs,2626+ void (*sync_access)(void));2727+void gic_cpu_config(void __iomem *base, void (*sync_access)(void));2828+2929+#endif /* _IRQ_GIC_COMMON_H */
+692
drivers/irqchip/irq-gic-v3.c
···11+/*22+ * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.33+ * Author: Marc Zyngier <marc.zyngier@arm.com>44+ *55+ * This program is free software; you can redistribute it and/or modify66+ * it under the terms of the GNU General Public License version 2 as77+ * published by the Free Software Foundation.88+ *99+ * This program is distributed in the hope that it will be useful,1010+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1111+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1212+ * GNU General Public License for more details.1313+ *1414+ * You should have received a copy of the GNU General Public License1515+ * along with this program. If not, see <http://www.gnu.org/licenses/>.1616+ */1717+1818+#include <linux/cpu.h>1919+#include <linux/delay.h>2020+#include <linux/interrupt.h>2121+#include <linux/of.h>2222+#include <linux/of_address.h>2323+#include <linux/of_irq.h>2424+#include <linux/percpu.h>2525+#include <linux/slab.h>2626+2727+#include <linux/irqchip/arm-gic-v3.h>2828+2929+#include <asm/cputype.h>3030+#include <asm/exception.h>3131+#include <asm/smp_plat.h>3232+3333+#include "irq-gic-common.h"3434+#include "irqchip.h"3535+3636+struct gic_chip_data {3737+ void __iomem *dist_base;3838+ void __iomem **redist_base;3939+ void __percpu __iomem **rdist;4040+ struct irq_domain *domain;4141+ u64 redist_stride;4242+ u32 redist_regions;4343+ unsigned int irq_nr;4444+};4545+4646+static struct gic_chip_data gic_data __read_mostly;4747+4848+#define gic_data_rdist() (this_cpu_ptr(gic_data.rdist))4949+#define gic_data_rdist_rd_base() (*gic_data_rdist())5050+#define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)5151+5252+/* Our default, arbitrary priority value. Linux only uses one anyway. */5353+#define DEFAULT_PMR_VALUE 0xf05454+5555+static inline unsigned int gic_irq(struct irq_data *d)5656+{5757+ return d->hwirq;5858+}5959+6060+static inline int gic_irq_in_rdist(struct irq_data *d)6161+{6262+ return gic_irq(d) < 32;6363+}6464+6565+static inline void __iomem *gic_dist_base(struct irq_data *d)6666+{6767+ if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */6868+ return gic_data_rdist_sgi_base();6969+7070+ if (d->hwirq <= 1023) /* SPI -> dist_base */7171+ return gic_data.dist_base;7272+7373+ if (d->hwirq >= 8192)7474+ BUG(); /* LPI Detected!!! */7575+7676+ return NULL;7777+}7878+7979+static void gic_do_wait_for_rwp(void __iomem *base)8080+{8181+ u32 count = 1000000; /* 1s! */8282+8383+ while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {8484+ count--;8585+ if (!count) {8686+ pr_err_ratelimited("RWP timeout, gone fishing\n");8787+ return;8888+ }8989+ cpu_relax();9090+ udelay(1);9191+ };9292+}9393+9494+/* Wait for completion of a distributor change */9595+static void gic_dist_wait_for_rwp(void)9696+{9797+ gic_do_wait_for_rwp(gic_data.dist_base);9898+}9999+100100+/* Wait for completion of a redistributor change */101101+static void gic_redist_wait_for_rwp(void)102102+{103103+ gic_do_wait_for_rwp(gic_data_rdist_rd_base());104104+}105105+106106+/* Low level accessors */107107+static u64 gic_read_iar(void)108108+{109109+ u64 irqstat;110110+111111+ asm volatile("mrs %0, " __stringify(ICC_IAR1_EL1) : "=r" (irqstat));112112+ return irqstat;113113+}114114+115115+static void gic_write_pmr(u64 val)116116+{117117+ asm volatile("msr " __stringify(ICC_PMR_EL1) ", %0" : : "r" (val));118118+}119119+120120+static void gic_write_ctlr(u64 val)121121+{122122+ asm volatile("msr " __stringify(ICC_CTLR_EL1) ", %0" : : "r" (val));123123+ isb();124124+}125125+126126+static void gic_write_grpen1(u64 val)127127+{128128+ asm volatile("msr " __stringify(ICC_GRPEN1_EL1) ", %0" : : "r" (val));129129+ isb();130130+}131131+132132+static void gic_write_sgi1r(u64 val)133133+{134134+ asm volatile("msr " __stringify(ICC_SGI1R_EL1) ", %0" : : "r" (val));135135+}136136+137137+static void gic_enable_sre(void)138138+{139139+ u64 val;140140+141141+ asm volatile("mrs %0, " __stringify(ICC_SRE_EL1) : "=r" (val));142142+ val |= ICC_SRE_EL1_SRE;143143+ asm volatile("msr " __stringify(ICC_SRE_EL1) ", %0" : : "r" (val));144144+ isb();145145+146146+ /*147147+ * Need to check that the SRE bit has actually been set. If148148+ * not, it means that SRE is disabled at EL2. We're going to149149+ * die painfully, and there is nothing we can do about it.150150+ *151151+ * Kindly inform the luser.152152+ */153153+ asm volatile("mrs %0, " __stringify(ICC_SRE_EL1) : "=r" (val));154154+ if (!(val & ICC_SRE_EL1_SRE))155155+ pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");156156+}157157+158158+static void gic_enable_redist(void)159159+{160160+ void __iomem *rbase;161161+ u32 count = 1000000; /* 1s! */162162+ u32 val;163163+164164+ rbase = gic_data_rdist_rd_base();165165+166166+ /* Wake up this CPU redistributor */167167+ val = readl_relaxed(rbase + GICR_WAKER);168168+ val &= ~GICR_WAKER_ProcessorSleep;169169+ writel_relaxed(val, rbase + GICR_WAKER);170170+171171+ while (readl_relaxed(rbase + GICR_WAKER) & GICR_WAKER_ChildrenAsleep) {172172+ count--;173173+ if (!count) {174174+ pr_err_ratelimited("redist didn't wake up...\n");175175+ return;176176+ }177177+ cpu_relax();178178+ udelay(1);179179+ };180180+}181181+182182+/*183183+ * Routines to disable, enable, EOI and route interrupts184184+ */185185+static void gic_poke_irq(struct irq_data *d, u32 offset)186186+{187187+ u32 mask = 1 << (gic_irq(d) % 32);188188+ void (*rwp_wait)(void);189189+ void __iomem *base;190190+191191+ if (gic_irq_in_rdist(d)) {192192+ base = gic_data_rdist_sgi_base();193193+ rwp_wait = gic_redist_wait_for_rwp;194194+ } else {195195+ base = gic_data.dist_base;196196+ rwp_wait = gic_dist_wait_for_rwp;197197+ }198198+199199+ writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);200200+ rwp_wait();201201+}202202+203203+static int gic_peek_irq(struct irq_data *d, u32 offset)204204+{205205+ u32 mask = 1 << (gic_irq(d) % 32);206206+ void __iomem *base;207207+208208+ if (gic_irq_in_rdist(d))209209+ base = gic_data_rdist_sgi_base();210210+ else211211+ base = gic_data.dist_base;212212+213213+ return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);214214+}215215+216216+static void gic_mask_irq(struct irq_data *d)217217+{218218+ gic_poke_irq(d, GICD_ICENABLER);219219+}220220+221221+static void gic_unmask_irq(struct irq_data *d)222222+{223223+ gic_poke_irq(d, GICD_ISENABLER);224224+}225225+226226+static void gic_eoi_irq(struct irq_data *d)227227+{228228+ gic_write_eoir(gic_irq(d));229229+}230230+231231+static int gic_set_type(struct irq_data *d, unsigned int type)232232+{233233+ unsigned int irq = gic_irq(d);234234+ void (*rwp_wait)(void);235235+ void __iomem *base;236236+237237+ /* Interrupt configuration for SGIs can't be changed */238238+ if (irq < 16)239239+ return -EINVAL;240240+241241+ if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)242242+ return -EINVAL;243243+244244+ if (gic_irq_in_rdist(d)) {245245+ base = gic_data_rdist_sgi_base();246246+ rwp_wait = gic_redist_wait_for_rwp;247247+ } else {248248+ base = gic_data.dist_base;249249+ rwp_wait = gic_dist_wait_for_rwp;250250+ }251251+252252+ gic_configure_irq(irq, type, base, rwp_wait);253253+254254+ return 0;255255+}256256+257257+static u64 gic_mpidr_to_affinity(u64 mpidr)258258+{259259+ u64 aff;260260+261261+ aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |262262+ MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |263263+ MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |264264+ MPIDR_AFFINITY_LEVEL(mpidr, 0));265265+266266+ return aff;267267+}268268+269269+static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)270270+{271271+ u64 irqnr;272272+273273+ do {274274+ irqnr = gic_read_iar();275275+276276+ if (likely(irqnr > 15 && irqnr < 1020)) {277277+ u64 irq = irq_find_mapping(gic_data.domain, irqnr);278278+ if (likely(irq)) {279279+ handle_IRQ(irq, regs);280280+ continue;281281+ }282282+283283+ WARN_ONCE(true, "Unexpected SPI received!\n");284284+ gic_write_eoir(irqnr);285285+ }286286+ if (irqnr < 16) {287287+ gic_write_eoir(irqnr);288288+#ifdef CONFIG_SMP289289+ handle_IPI(irqnr, regs);290290+#else291291+ WARN_ONCE(true, "Unexpected SGI received!\n");292292+#endif293293+ continue;294294+ }295295+ } while (irqnr != ICC_IAR1_EL1_SPURIOUS);296296+}297297+298298+static void __init gic_dist_init(void)299299+{300300+ unsigned int i;301301+ u64 affinity;302302+ void __iomem *base = gic_data.dist_base;303303+304304+ /* Disable the distributor */305305+ writel_relaxed(0, base + GICD_CTLR);306306+ gic_dist_wait_for_rwp();307307+308308+ gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);309309+310310+ /* Enable distributor with ARE, Group1 */311311+ writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,312312+ base + GICD_CTLR);313313+314314+ /*315315+ * Set all global interrupts to the boot CPU only. ARE must be316316+ * enabled.317317+ */318318+ affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));319319+ for (i = 32; i < gic_data.irq_nr; i++)320320+ writeq_relaxed(affinity, base + GICD_IROUTER + i * 8);321321+}322322+323323+static int gic_populate_rdist(void)324324+{325325+ u64 mpidr = cpu_logical_map(smp_processor_id());326326+ u64 typer;327327+ u32 aff;328328+ int i;329329+330330+ /*331331+ * Convert affinity to a 32bit value that can be matched to332332+ * GICR_TYPER bits [63:32].333333+ */334334+ aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |335335+ MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |336336+ MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |337337+ MPIDR_AFFINITY_LEVEL(mpidr, 0));338338+339339+ for (i = 0; i < gic_data.redist_regions; i++) {340340+ void __iomem *ptr = gic_data.redist_base[i];341341+ u32 reg;342342+343343+ reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;344344+ if (reg != GIC_PIDR2_ARCH_GICv3 &&345345+ reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */346346+ pr_warn("No redistributor present @%p\n", ptr);347347+ break;348348+ }349349+350350+ do {351351+ typer = readq_relaxed(ptr + GICR_TYPER);352352+ if ((typer >> 32) == aff) {353353+ gic_data_rdist_rd_base() = ptr;354354+ pr_info("CPU%d: found redistributor %llx @%p\n",355355+ smp_processor_id(),356356+ (unsigned long long)mpidr, ptr);357357+ return 0;358358+ }359359+360360+ if (gic_data.redist_stride) {361361+ ptr += gic_data.redist_stride;362362+ } else {363363+ ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */364364+ if (typer & GICR_TYPER_VLPIS)365365+ ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */366366+ }367367+ } while (!(typer & GICR_TYPER_LAST));368368+ }369369+370370+ /* We couldn't even deal with ourselves... */371371+ WARN(true, "CPU%d: mpidr %llx has no re-distributor!\n",372372+ smp_processor_id(), (unsigned long long)mpidr);373373+ return -ENODEV;374374+}375375+376376+static void gic_cpu_init(void)377377+{378378+ void __iomem *rbase;379379+380380+ /* Register ourselves with the rest of the world */381381+ if (gic_populate_rdist())382382+ return;383383+384384+ gic_enable_redist();385385+386386+ rbase = gic_data_rdist_sgi_base();387387+388388+ gic_cpu_config(rbase, gic_redist_wait_for_rwp);389389+390390+ /* Enable system registers */391391+ gic_enable_sre();392392+393393+ /* Set priority mask register */394394+ gic_write_pmr(DEFAULT_PMR_VALUE);395395+396396+ /* EOI deactivates interrupt too (mode 0) */397397+ gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);398398+399399+ /* ... and let's hit the road... */400400+ gic_write_grpen1(1);401401+}402402+403403+#ifdef CONFIG_SMP404404+static int gic_secondary_init(struct notifier_block *nfb,405405+ unsigned long action, void *hcpu)406406+{407407+ if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)408408+ gic_cpu_init();409409+ return NOTIFY_OK;410410+}411411+412412+/*413413+ * Notifier for enabling the GIC CPU interface. Set an arbitrarily high414414+ * priority because the GIC needs to be up before the ARM generic timers.415415+ */416416+static struct notifier_block gic_cpu_notifier = {417417+ .notifier_call = gic_secondary_init,418418+ .priority = 100,419419+};420420+421421+static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,422422+ u64 cluster_id)423423+{424424+ int cpu = *base_cpu;425425+ u64 mpidr = cpu_logical_map(cpu);426426+ u16 tlist = 0;427427+428428+ while (cpu < nr_cpu_ids) {429429+ /*430430+ * If we ever get a cluster of more than 16 CPUs, just431431+ * scream and skip that CPU.432432+ */433433+ if (WARN_ON((mpidr & 0xff) >= 16))434434+ goto out;435435+436436+ tlist |= 1 << (mpidr & 0xf);437437+438438+ cpu = cpumask_next(cpu, mask);439439+ if (cpu == nr_cpu_ids)440440+ goto out;441441+442442+ mpidr = cpu_logical_map(cpu);443443+444444+ if (cluster_id != (mpidr & ~0xffUL)) {445445+ cpu--;446446+ goto out;447447+ }448448+ }449449+out:450450+ *base_cpu = cpu;451451+ return tlist;452452+}453453+454454+static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)455455+{456456+ u64 val;457457+458458+ val = (MPIDR_AFFINITY_LEVEL(cluster_id, 3) << 48 |459459+ MPIDR_AFFINITY_LEVEL(cluster_id, 2) << 32 |460460+ irq << 24 |461461+ MPIDR_AFFINITY_LEVEL(cluster_id, 1) << 16 |462462+ tlist);463463+464464+ pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);465465+ gic_write_sgi1r(val);466466+}467467+468468+static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)469469+{470470+ int cpu;471471+472472+ if (WARN_ON(irq >= 16))473473+ return;474474+475475+ /*476476+ * Ensure that stores to Normal memory are visible to the477477+ * other CPUs before issuing the IPI.478478+ */479479+ smp_wmb();480480+481481+ for_each_cpu_mask(cpu, *mask) {482482+ u64 cluster_id = cpu_logical_map(cpu) & ~0xffUL;483483+ u16 tlist;484484+485485+ tlist = gic_compute_target_list(&cpu, mask, cluster_id);486486+ gic_send_sgi(cluster_id, tlist, irq);487487+ }488488+489489+ /* Force the above writes to ICC_SGI1R_EL1 to be executed */490490+ isb();491491+}492492+493493+static void gic_smp_init(void)494494+{495495+ set_smp_cross_call(gic_raise_softirq);496496+ register_cpu_notifier(&gic_cpu_notifier);497497+}498498+499499+static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,500500+ bool force)501501+{502502+ unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);503503+ void __iomem *reg;504504+ int enabled;505505+ u64 val;506506+507507+ if (gic_irq_in_rdist(d))508508+ return -EINVAL;509509+510510+ /* If interrupt was enabled, disable it first */511511+ enabled = gic_peek_irq(d, GICD_ISENABLER);512512+ if (enabled)513513+ gic_mask_irq(d);514514+515515+ reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);516516+ val = gic_mpidr_to_affinity(cpu_logical_map(cpu));517517+518518+ writeq_relaxed(val, reg);519519+520520+ /*521521+ * If the interrupt was enabled, enabled it again. Otherwise,522522+ * just wait for the distributor to have digested our changes.523523+ */524524+ if (enabled)525525+ gic_unmask_irq(d);526526+ else527527+ gic_dist_wait_for_rwp();528528+529529+ return IRQ_SET_MASK_OK;530530+}531531+#else532532+#define gic_set_affinity NULL533533+#define gic_smp_init() do { } while(0)534534+#endif535535+536536+static struct irq_chip gic_chip = {537537+ .name = "GICv3",538538+ .irq_mask = gic_mask_irq,539539+ .irq_unmask = gic_unmask_irq,540540+ .irq_eoi = gic_eoi_irq,541541+ .irq_set_type = gic_set_type,542542+ .irq_set_affinity = gic_set_affinity,543543+};544544+545545+static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,546546+ irq_hw_number_t hw)547547+{548548+ /* SGIs are private to the core kernel */549549+ if (hw < 16)550550+ return -EPERM;551551+ /* PPIs */552552+ if (hw < 32) {553553+ irq_set_percpu_devid(irq);554554+ irq_set_chip_and_handler(irq, &gic_chip,555555+ handle_percpu_devid_irq);556556+ set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);557557+ }558558+ /* SPIs */559559+ if (hw >= 32 && hw < gic_data.irq_nr) {560560+ irq_set_chip_and_handler(irq, &gic_chip,561561+ handle_fasteoi_irq);562562+ set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);563563+ }564564+ irq_set_chip_data(irq, d->host_data);565565+ return 0;566566+}567567+568568+static int gic_irq_domain_xlate(struct irq_domain *d,569569+ struct device_node *controller,570570+ const u32 *intspec, unsigned int intsize,571571+ unsigned long *out_hwirq, unsigned int *out_type)572572+{573573+ if (d->of_node != controller)574574+ return -EINVAL;575575+ if (intsize < 3)576576+ return -EINVAL;577577+578578+ switch(intspec[0]) {579579+ case 0: /* SPI */580580+ *out_hwirq = intspec[1] + 32;581581+ break;582582+ case 1: /* PPI */583583+ *out_hwirq = intspec[1] + 16;584584+ break;585585+ default:586586+ return -EINVAL;587587+ }588588+589589+ *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;590590+ return 0;591591+}592592+593593+static const struct irq_domain_ops gic_irq_domain_ops = {594594+ .map = gic_irq_domain_map,595595+ .xlate = gic_irq_domain_xlate,596596+};597597+598598+static int __init gic_of_init(struct device_node *node, struct device_node *parent)599599+{600600+ void __iomem *dist_base;601601+ void __iomem **redist_base;602602+ u64 redist_stride;603603+ u32 redist_regions;604604+ u32 reg;605605+ int gic_irqs;606606+ int err;607607+ int i;608608+609609+ dist_base = of_iomap(node, 0);610610+ if (!dist_base) {611611+ pr_err("%s: unable to map gic dist registers\n",612612+ node->full_name);613613+ return -ENXIO;614614+ }615615+616616+ reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;617617+ if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4) {618618+ pr_err("%s: no distributor detected, giving up\n",619619+ node->full_name);620620+ err = -ENODEV;621621+ goto out_unmap_dist;622622+ }623623+624624+ if (of_property_read_u32(node, "#redistributor-regions", &redist_regions))625625+ redist_regions = 1;626626+627627+ redist_base = kzalloc(sizeof(*redist_base) * redist_regions, GFP_KERNEL);628628+ if (!redist_base) {629629+ err = -ENOMEM;630630+ goto out_unmap_dist;631631+ }632632+633633+ for (i = 0; i < redist_regions; i++) {634634+ redist_base[i] = of_iomap(node, 1 + i);635635+ if (!redist_base[i]) {636636+ pr_err("%s: couldn't map region %d\n",637637+ node->full_name, i);638638+ err = -ENODEV;639639+ goto out_unmap_rdist;640640+ }641641+ }642642+643643+ if (of_property_read_u64(node, "redistributor-stride", &redist_stride))644644+ redist_stride = 0;645645+646646+ gic_data.dist_base = dist_base;647647+ gic_data.redist_base = redist_base;648648+ gic_data.redist_regions = redist_regions;649649+ gic_data.redist_stride = redist_stride;650650+651651+ /*652652+ * Find out how many interrupts are supported.653653+ * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)654654+ */655655+ gic_irqs = readl_relaxed(gic_data.dist_base + GICD_TYPER) & 0x1f;656656+ gic_irqs = (gic_irqs + 1) * 32;657657+ if (gic_irqs > 1020)658658+ gic_irqs = 1020;659659+ gic_data.irq_nr = gic_irqs;660660+661661+ gic_data.domain = irq_domain_add_tree(node, &gic_irq_domain_ops,662662+ &gic_data);663663+ gic_data.rdist = alloc_percpu(typeof(*gic_data.rdist));664664+665665+ if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdist)) {666666+ err = -ENOMEM;667667+ goto out_free;668668+ }669669+670670+ set_handle_irq(gic_handle_irq);671671+672672+ gic_smp_init();673673+ gic_dist_init();674674+ gic_cpu_init();675675+676676+ return 0;677677+678678+out_free:679679+ if (gic_data.domain)680680+ irq_domain_remove(gic_data.domain);681681+ free_percpu(gic_data.rdist);682682+out_unmap_rdist:683683+ for (i = 0; i < redist_regions; i++)684684+ if (redist_base[i])685685+ iounmap(redist_base[i]);686686+ kfree(redist_base);687687+out_unmap_dist:688688+ iounmap(dist_base);689689+ return err;690690+}691691+692692+IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
+4-55
drivers/irqchip/irq-gic.c
···4646#include <asm/exception.h>4747#include <asm/smp_plat.h>48484949+#include "irq-gic-common.h"4950#include "irqchip.h"50515152union gic_base {···189188{190189 void __iomem *base = gic_dist_base(d);191190 unsigned int gicirq = gic_irq(d);192192- u32 enablemask = 1 << (gicirq % 32);193193- u32 enableoff = (gicirq / 32) * 4;194194- u32 confmask = 0x2 << ((gicirq % 16) * 2);195195- u32 confoff = (gicirq / 16) * 4;196196- bool enabled = false;197197- u32 val;198191199192 /* Interrupt configuration for SGIs can't be changed */200193 if (gicirq < 16)···202207 if (gic_arch_extn.irq_set_type)203208 gic_arch_extn.irq_set_type(d, type);204209205205- val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);206206- if (type == IRQ_TYPE_LEVEL_HIGH)207207- val &= ~confmask;208208- else if (type == IRQ_TYPE_EDGE_RISING)209209- val |= confmask;210210-211211- /*212212- * As recommended by the spec, disable the interrupt before changing213213- * the configuration214214- */215215- if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {216216- writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);217217- enabled = true;218218- }219219-220220- writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);221221-222222- if (enabled)223223- writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);210210+ gic_configure_irq(gicirq, type, base, NULL);224211225212 raw_spin_unlock(&irq_controller_lock);226213···364387 writel_relaxed(0, base + GIC_DIST_CTRL);365388366389 /*367367- * Set all global interrupts to be level triggered, active low.368368- */369369- for (i = 32; i < gic_irqs; i += 16)370370- writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);371371-372372- /*373390 * Set all global interrupts to this CPU only.374391 */375392 cpumask = gic_get_cpumask(gic);···372401 for (i = 32; i < gic_irqs; i += 4)373402 writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);374403375375- /*376376- * Set priority on all global interrupts.377377- */378378- for (i = 32; i < gic_irqs; i += 4)379379- writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);380380-381381- /*382382- * Disable all interrupts. Leave the PPI and SGIs alone383383- * as these enables are banked registers.384384- */385385- for (i = 32; i < gic_irqs; i += 32)386386- writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);404404+ gic_dist_config(base, gic_irqs, NULL);387405388406 writel_relaxed(1, base + GIC_DIST_CTRL);389407}···399439 if (i != cpu)400440 gic_cpu_map[i] &= ~cpu_mask;401441402402- /*403403- * Deal with the banked PPI and SGI interrupts - disable all404404- * PPI interrupts, ensure all SGI interrupts are enabled.405405- */406406- writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);407407- writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);408408-409409- /*410410- * Set priority on PPI and SGI interrupts411411- */412412- for (i = 0; i < 32; i += 4)413413- writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);442442+ gic_cpu_config(dist_base, NULL);414443415444 writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);416445 writel_relaxed(1, base + GIC_CPU_CTRL);
+198
include/linux/irqchip/arm-gic-v3.h
···11+/*22+ * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.33+ * Author: Marc Zyngier <marc.zyngier@arm.com>44+ *55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 as88+ * published by the Free Software Foundation.99+ *1010+ * This program is distributed in the hope that it will be useful,1111+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1212+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1313+ * GNU General Public License for more details.1414+ *1515+ * You should have received a copy of the GNU General Public License1616+ * along with this program. If not, see <http://www.gnu.org/licenses/>.1717+ */1818+#ifndef __LINUX_IRQCHIP_ARM_GIC_V3_H1919+#define __LINUX_IRQCHIP_ARM_GIC_V3_H2020+2121+/*2222+ * Distributor registers. We assume we're running non-secure, with ARE2323+ * being set. Secure-only and non-ARE registers are not described.2424+ */2525+#define GICD_CTLR 0x00002626+#define GICD_TYPER 0x00042727+#define GICD_IIDR 0x00082828+#define GICD_STATUSR 0x00102929+#define GICD_SETSPI_NSR 0x00403030+#define GICD_CLRSPI_NSR 0x00483131+#define GICD_SETSPI_SR 0x00503232+#define GICD_CLRSPI_SR 0x00583333+#define GICD_SEIR 0x00683434+#define GICD_ISENABLER 0x01003535+#define GICD_ICENABLER 0x01803636+#define GICD_ISPENDR 0x02003737+#define GICD_ICPENDR 0x02803838+#define GICD_ISACTIVER 0x03003939+#define GICD_ICACTIVER 0x03804040+#define GICD_IPRIORITYR 0x04004141+#define GICD_ICFGR 0x0C004242+#define GICD_IROUTER 0x60004343+#define GICD_PIDR2 0xFFE84444+4545+#define GICD_CTLR_RWP (1U << 31)4646+#define GICD_CTLR_ARE_NS (1U << 4)4747+#define GICD_CTLR_ENABLE_G1A (1U << 1)4848+#define GICD_CTLR_ENABLE_G1 (1U << 0)4949+5050+#define GICD_IROUTER_SPI_MODE_ONE (0U << 31)5151+#define GICD_IROUTER_SPI_MODE_ANY (1U << 31)5252+5353+#define GIC_PIDR2_ARCH_MASK 0xf05454+#define GIC_PIDR2_ARCH_GICv3 0x305555+#define GIC_PIDR2_ARCH_GICv4 0x405656+5757+/*5858+ * Re-Distributor registers, offsets from RD_base5959+ */6060+#define GICR_CTLR GICD_CTLR6161+#define GICR_IIDR 0x00046262+#define GICR_TYPER 0x00086363+#define GICR_STATUSR GICD_STATUSR6464+#define GICR_WAKER 0x00146565+#define GICR_SETLPIR 0x00406666+#define GICR_CLRLPIR 0x00486767+#define GICR_SEIR GICD_SEIR6868+#define GICR_PROPBASER 0x00706969+#define GICR_PENDBASER 0x00787070+#define GICR_INVLPIR 0x00A07171+#define GICR_INVALLR 0x00B07272+#define GICR_SYNCR 0x00C07373+#define GICR_MOVLPIR 0x01007474+#define GICR_MOVALLR 0x01107575+#define GICR_PIDR2 GICD_PIDR27676+7777+#define GICR_WAKER_ProcessorSleep (1U << 1)7878+#define GICR_WAKER_ChildrenAsleep (1U << 2)7979+8080+/*8181+ * Re-Distributor registers, offsets from SGI_base8282+ */8383+#define GICR_ISENABLER0 GICD_ISENABLER8484+#define GICR_ICENABLER0 GICD_ICENABLER8585+#define GICR_ISPENDR0 GICD_ISPENDR8686+#define GICR_ICPENDR0 GICD_ICPENDR8787+#define GICR_ISACTIVER0 GICD_ISACTIVER8888+#define GICR_ICACTIVER0 GICD_ICACTIVER8989+#define GICR_IPRIORITYR0 GICD_IPRIORITYR9090+#define GICR_ICFGR0 GICD_ICFGR9191+9292+#define GICR_TYPER_VLPIS (1U << 1)9393+#define GICR_TYPER_LAST (1U << 4)9494+9595+/*9696+ * CPU interface registers9797+ */9898+#define ICC_CTLR_EL1_EOImode_drop_dir (0U << 1)9999+#define ICC_CTLR_EL1_EOImode_drop (1U << 1)100100+#define ICC_SRE_EL1_SRE (1U << 0)101101+102102+/*103103+ * Hypervisor interface registers (SRE only)104104+ */105105+#define ICH_LR_VIRTUAL_ID_MASK ((1UL << 32) - 1)106106+107107+#define ICH_LR_EOI (1UL << 41)108108+#define ICH_LR_GROUP (1UL << 60)109109+#define ICH_LR_STATE (3UL << 62)110110+#define ICH_LR_PENDING_BIT (1UL << 62)111111+#define ICH_LR_ACTIVE_BIT (1UL << 63)112112+113113+#define ICH_MISR_EOI (1 << 0)114114+#define ICH_MISR_U (1 << 1)115115+116116+#define ICH_HCR_EN (1 << 0)117117+#define ICH_HCR_UIE (1 << 1)118118+119119+#define ICH_VMCR_CTLR_SHIFT 0120120+#define ICH_VMCR_CTLR_MASK (0x21f << ICH_VMCR_CTLR_SHIFT)121121+#define ICH_VMCR_BPR1_SHIFT 18122122+#define ICH_VMCR_BPR1_MASK (7 << ICH_VMCR_BPR1_SHIFT)123123+#define ICH_VMCR_BPR0_SHIFT 21124124+#define ICH_VMCR_BPR0_MASK (7 << ICH_VMCR_BPR0_SHIFT)125125+#define ICH_VMCR_PMR_SHIFT 24126126+#define ICH_VMCR_PMR_MASK (0xffUL << ICH_VMCR_PMR_SHIFT)127127+128128+#define ICC_EOIR1_EL1 S3_0_C12_C12_1129129+#define ICC_IAR1_EL1 S3_0_C12_C12_0130130+#define ICC_SGI1R_EL1 S3_0_C12_C11_5131131+#define ICC_PMR_EL1 S3_0_C4_C6_0132132+#define ICC_CTLR_EL1 S3_0_C12_C12_4133133+#define ICC_SRE_EL1 S3_0_C12_C12_5134134+#define ICC_GRPEN1_EL1 S3_0_C12_C12_7135135+136136+#define ICC_IAR1_EL1_SPURIOUS 0x3ff137137+138138+#define ICC_SRE_EL2 S3_4_C12_C9_5139139+140140+#define ICC_SRE_EL2_SRE (1 << 0)141141+#define ICC_SRE_EL2_ENABLE (1 << 3)142142+143143+/*144144+ * System register definitions145145+ */146146+#define ICH_VSEIR_EL2 S3_4_C12_C9_4147147+#define ICH_HCR_EL2 S3_4_C12_C11_0148148+#define ICH_VTR_EL2 S3_4_C12_C11_1149149+#define ICH_MISR_EL2 S3_4_C12_C11_2150150+#define ICH_EISR_EL2 S3_4_C12_C11_3151151+#define ICH_ELSR_EL2 S3_4_C12_C11_5152152+#define ICH_VMCR_EL2 S3_4_C12_C11_7153153+154154+#define __LR0_EL2(x) S3_4_C12_C12_ ## x155155+#define __LR8_EL2(x) S3_4_C12_C13_ ## x156156+157157+#define ICH_LR0_EL2 __LR0_EL2(0)158158+#define ICH_LR1_EL2 __LR0_EL2(1)159159+#define ICH_LR2_EL2 __LR0_EL2(2)160160+#define ICH_LR3_EL2 __LR0_EL2(3)161161+#define ICH_LR4_EL2 __LR0_EL2(4)162162+#define ICH_LR5_EL2 __LR0_EL2(5)163163+#define ICH_LR6_EL2 __LR0_EL2(6)164164+#define ICH_LR7_EL2 __LR0_EL2(7)165165+#define ICH_LR8_EL2 __LR8_EL2(0)166166+#define ICH_LR9_EL2 __LR8_EL2(1)167167+#define ICH_LR10_EL2 __LR8_EL2(2)168168+#define ICH_LR11_EL2 __LR8_EL2(3)169169+#define ICH_LR12_EL2 __LR8_EL2(4)170170+#define ICH_LR13_EL2 __LR8_EL2(5)171171+#define ICH_LR14_EL2 __LR8_EL2(6)172172+#define ICH_LR15_EL2 __LR8_EL2(7)173173+174174+#define __AP0Rx_EL2(x) S3_4_C12_C8_ ## x175175+#define ICH_AP0R0_EL2 __AP0Rx_EL2(0)176176+#define ICH_AP0R1_EL2 __AP0Rx_EL2(1)177177+#define ICH_AP0R2_EL2 __AP0Rx_EL2(2)178178+#define ICH_AP0R3_EL2 __AP0Rx_EL2(3)179179+180180+#define __AP1Rx_EL2(x) S3_4_C12_C9_ ## x181181+#define ICH_AP1R0_EL2 __AP1Rx_EL2(0)182182+#define ICH_AP1R1_EL2 __AP1Rx_EL2(1)183183+#define ICH_AP1R2_EL2 __AP1Rx_EL2(2)184184+#define ICH_AP1R3_EL2 __AP1Rx_EL2(3)185185+186186+#ifndef __ASSEMBLY__187187+188188+#include <linux/stringify.h>189189+190190+static inline void gic_write_eoir(u64 irq)191191+{192192+ asm volatile("msr " __stringify(ICC_EOIR1_EL1) ", %0" : : "r" (irq));193193+ isb();194194+}195195+196196+#endif197197+198198+#endif