irq-gic-v3.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*
  2. * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/cpu.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/of.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_irq.h>
  24. #include <linux/percpu.h>
  25. #include <linux/slab.h>
  26. #include <linux/irqchip.h>
  27. #include <linux/irqchip/arm-gic-v3.h>
  28. #include <asm/cputype.h>
  29. #include <asm/exception.h>
  30. #include <asm/smp_plat.h>
  31. #include <asm/virt.h>
  32. #include "irq-gic-common.h"
  33. struct redist_region {
  34. void __iomem *redist_base;
  35. phys_addr_t phys_base;
  36. };
  37. struct gic_chip_data {
  38. void __iomem *dist_base;
  39. struct redist_region *redist_regions;
  40. struct rdists rdists;
  41. struct irq_domain *domain;
  42. u64 redist_stride;
  43. u32 nr_redist_regions;
  44. unsigned int irq_nr;
  45. };
  46. static struct gic_chip_data gic_data __read_mostly;
  47. static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
  48. #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
  49. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  50. #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
  51. /* Our default, arbitrary priority value. Linux only uses one anyway. */
  52. #define DEFAULT_PMR_VALUE 0xf0
  53. static inline unsigned int gic_irq(struct irq_data *d)
  54. {
  55. return d->hwirq;
  56. }
  57. static inline int gic_irq_in_rdist(struct irq_data *d)
  58. {
  59. return gic_irq(d) < 32;
  60. }
  61. static inline void __iomem *gic_dist_base(struct irq_data *d)
  62. {
  63. if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
  64. return gic_data_rdist_sgi_base();
  65. if (d->hwirq <= 1023) /* SPI -> dist_base */
  66. return gic_data.dist_base;
  67. return NULL;
  68. }
  69. static void gic_do_wait_for_rwp(void __iomem *base)
  70. {
  71. u32 count = 1000000; /* 1s! */
  72. while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
  73. count--;
  74. if (!count) {
  75. pr_err_ratelimited("RWP timeout, gone fishing\n");
  76. return;
  77. }
  78. cpu_relax();
  79. udelay(1);
  80. };
  81. }
  82. /* Wait for completion of a distributor change */
  83. static void gic_dist_wait_for_rwp(void)
  84. {
  85. gic_do_wait_for_rwp(gic_data.dist_base);
  86. }
  87. /* Wait for completion of a redistributor change */
  88. static void gic_redist_wait_for_rwp(void)
  89. {
  90. gic_do_wait_for_rwp(gic_data_rdist_rd_base());
  91. }
  92. #ifdef CONFIG_ARM64
  93. static DEFINE_STATIC_KEY_FALSE(is_cavium_thunderx);
  94. static u64 __maybe_unused gic_read_iar(void)
  95. {
  96. if (static_branch_unlikely(&is_cavium_thunderx))
  97. return gic_read_iar_cavium_thunderx();
  98. else
  99. return gic_read_iar_common();
  100. }
  101. #endif
  102. static void gic_enable_redist(bool enable)
  103. {
  104. void __iomem *rbase;
  105. u32 count = 1000000; /* 1s! */
  106. u32 val;
  107. rbase = gic_data_rdist_rd_base();
  108. val = readl_relaxed(rbase + GICR_WAKER);
  109. if (enable)
  110. /* Wake up this CPU redistributor */
  111. val &= ~GICR_WAKER_ProcessorSleep;
  112. else
  113. val |= GICR_WAKER_ProcessorSleep;
  114. writel_relaxed(val, rbase + GICR_WAKER);
  115. if (!enable) { /* Check that GICR_WAKER is writeable */
  116. val = readl_relaxed(rbase + GICR_WAKER);
  117. if (!(val & GICR_WAKER_ProcessorSleep))
  118. return; /* No PM support in this redistributor */
  119. }
  120. while (--count) {
  121. val = readl_relaxed(rbase + GICR_WAKER);
  122. if (enable ^ (val & GICR_WAKER_ChildrenAsleep))
  123. break;
  124. cpu_relax();
  125. udelay(1);
  126. };
  127. if (!count)
  128. pr_err_ratelimited("redistributor failed to %s...\n",
  129. enable ? "wakeup" : "sleep");
  130. }
  131. /*
  132. * Routines to disable, enable, EOI and route interrupts
  133. */
  134. static int gic_peek_irq(struct irq_data *d, u32 offset)
  135. {
  136. u32 mask = 1 << (gic_irq(d) % 32);
  137. void __iomem *base;
  138. if (gic_irq_in_rdist(d))
  139. base = gic_data_rdist_sgi_base();
  140. else
  141. base = gic_data.dist_base;
  142. return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
  143. }
  144. static void gic_poke_irq(struct irq_data *d, u32 offset)
  145. {
  146. u32 mask = 1 << (gic_irq(d) % 32);
  147. void (*rwp_wait)(void);
  148. void __iomem *base;
  149. if (gic_irq_in_rdist(d)) {
  150. base = gic_data_rdist_sgi_base();
  151. rwp_wait = gic_redist_wait_for_rwp;
  152. } else {
  153. base = gic_data.dist_base;
  154. rwp_wait = gic_dist_wait_for_rwp;
  155. }
  156. writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
  157. rwp_wait();
  158. }
  159. static void gic_mask_irq(struct irq_data *d)
  160. {
  161. gic_poke_irq(d, GICD_ICENABLER);
  162. }
  163. static void gic_eoimode1_mask_irq(struct irq_data *d)
  164. {
  165. gic_mask_irq(d);
  166. /*
  167. * When masking a forwarded interrupt, make sure it is
  168. * deactivated as well.
  169. *
  170. * This ensures that an interrupt that is getting
  171. * disabled/masked will not get "stuck", because there is
  172. * noone to deactivate it (guest is being terminated).
  173. */
  174. if (irqd_is_forwarded_to_vcpu(d))
  175. gic_poke_irq(d, GICD_ICACTIVER);
  176. }
  177. static void gic_unmask_irq(struct irq_data *d)
  178. {
  179. gic_poke_irq(d, GICD_ISENABLER);
  180. }
  181. static int gic_irq_set_irqchip_state(struct irq_data *d,
  182. enum irqchip_irq_state which, bool val)
  183. {
  184. u32 reg;
  185. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  186. return -EINVAL;
  187. switch (which) {
  188. case IRQCHIP_STATE_PENDING:
  189. reg = val ? GICD_ISPENDR : GICD_ICPENDR;
  190. break;
  191. case IRQCHIP_STATE_ACTIVE:
  192. reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
  193. break;
  194. case IRQCHIP_STATE_MASKED:
  195. reg = val ? GICD_ICENABLER : GICD_ISENABLER;
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. gic_poke_irq(d, reg);
  201. return 0;
  202. }
  203. static int gic_irq_get_irqchip_state(struct irq_data *d,
  204. enum irqchip_irq_state which, bool *val)
  205. {
  206. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  207. return -EINVAL;
  208. switch (which) {
  209. case IRQCHIP_STATE_PENDING:
  210. *val = gic_peek_irq(d, GICD_ISPENDR);
  211. break;
  212. case IRQCHIP_STATE_ACTIVE:
  213. *val = gic_peek_irq(d, GICD_ISACTIVER);
  214. break;
  215. case IRQCHIP_STATE_MASKED:
  216. *val = !gic_peek_irq(d, GICD_ISENABLER);
  217. break;
  218. default:
  219. return -EINVAL;
  220. }
  221. return 0;
  222. }
  223. static void gic_eoi_irq(struct irq_data *d)
  224. {
  225. gic_write_eoir(gic_irq(d));
  226. }
  227. static void gic_eoimode1_eoi_irq(struct irq_data *d)
  228. {
  229. /*
  230. * No need to deactivate an LPI, or an interrupt that
  231. * is is getting forwarded to a vcpu.
  232. */
  233. if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
  234. return;
  235. gic_write_dir(gic_irq(d));
  236. }
  237. static int gic_set_type(struct irq_data *d, unsigned int type)
  238. {
  239. unsigned int irq = gic_irq(d);
  240. void (*rwp_wait)(void);
  241. void __iomem *base;
  242. /* Interrupt configuration for SGIs can't be changed */
  243. if (irq < 16)
  244. return -EINVAL;
  245. /* SPIs have restrictions on the supported types */
  246. if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
  247. type != IRQ_TYPE_EDGE_RISING)
  248. return -EINVAL;
  249. if (gic_irq_in_rdist(d)) {
  250. base = gic_data_rdist_sgi_base();
  251. rwp_wait = gic_redist_wait_for_rwp;
  252. } else {
  253. base = gic_data.dist_base;
  254. rwp_wait = gic_dist_wait_for_rwp;
  255. }
  256. return gic_configure_irq(irq, type, base, rwp_wait);
  257. }
  258. static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  259. {
  260. if (vcpu)
  261. irqd_set_forwarded_to_vcpu(d);
  262. else
  263. irqd_clr_forwarded_to_vcpu(d);
  264. return 0;
  265. }
  266. static u64 gic_mpidr_to_affinity(unsigned long mpidr)
  267. {
  268. u64 aff;
  269. aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
  270. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  271. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  272. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  273. return aff;
  274. }
  275. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  276. {
  277. u32 irqnr;
  278. do {
  279. irqnr = gic_read_iar();
  280. if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
  281. int err;
  282. if (static_key_true(&supports_deactivate))
  283. gic_write_eoir(irqnr);
  284. err = handle_domain_irq(gic_data.domain, irqnr, regs);
  285. if (err) {
  286. WARN_ONCE(true, "Unexpected interrupt received!\n");
  287. if (static_key_true(&supports_deactivate)) {
  288. if (irqnr < 8192)
  289. gic_write_dir(irqnr);
  290. } else {
  291. gic_write_eoir(irqnr);
  292. }
  293. }
  294. continue;
  295. }
  296. if (irqnr < 16) {
  297. gic_write_eoir(irqnr);
  298. if (static_key_true(&supports_deactivate))
  299. gic_write_dir(irqnr);
  300. #ifdef CONFIG_SMP
  301. /*
  302. * Unlike GICv2, we don't need an smp_rmb() here.
  303. * The control dependency from gic_read_iar to
  304. * the ISB in gic_write_eoir is enough to ensure
  305. * that any shared data read by handle_IPI will
  306. * be read after the ACK.
  307. */
  308. handle_IPI(irqnr, regs);
  309. #else
  310. WARN_ONCE(true, "Unexpected SGI received!\n");
  311. #endif
  312. continue;
  313. }
  314. } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
  315. }
  316. static void __init gic_dist_init(void)
  317. {
  318. unsigned int i;
  319. u64 affinity;
  320. void __iomem *base = gic_data.dist_base;
  321. /* Disable the distributor */
  322. writel_relaxed(0, base + GICD_CTLR);
  323. gic_dist_wait_for_rwp();
  324. /*
  325. * Configure SPIs as non-secure Group-1. This will only matter
  326. * if the GIC only has a single security state. This will not
  327. * do the right thing if the kernel is running in secure mode,
  328. * but that's not the intended use case anyway.
  329. */
  330. for (i = 32; i < gic_data.irq_nr; i += 32)
  331. writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
  332. gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
  333. /* Enable distributor with ARE, Group1 */
  334. writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
  335. base + GICD_CTLR);
  336. /*
  337. * Set all global interrupts to the boot CPU only. ARE must be
  338. * enabled.
  339. */
  340. affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
  341. for (i = 32; i < gic_data.irq_nr; i++)
  342. gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
  343. }
  344. static int gic_populate_rdist(void)
  345. {
  346. unsigned long mpidr = cpu_logical_map(smp_processor_id());
  347. u64 typer;
  348. u32 aff;
  349. int i;
  350. /*
  351. * Convert affinity to a 32bit value that can be matched to
  352. * GICR_TYPER bits [63:32].
  353. */
  354. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
  355. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  356. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  357. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  358. for (i = 0; i < gic_data.nr_redist_regions; i++) {
  359. void __iomem *ptr = gic_data.redist_regions[i].redist_base;
  360. u32 reg;
  361. reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
  362. if (reg != GIC_PIDR2_ARCH_GICv3 &&
  363. reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
  364. pr_warn("No redistributor present @%p\n", ptr);
  365. break;
  366. }
  367. do {
  368. typer = gic_read_typer(ptr + GICR_TYPER);
  369. if ((typer >> 32) == aff) {
  370. u64 offset = ptr - gic_data.redist_regions[i].redist_base;
  371. gic_data_rdist_rd_base() = ptr;
  372. gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset;
  373. pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
  374. smp_processor_id(), mpidr, i,
  375. &gic_data_rdist()->phys_base);
  376. return 0;
  377. }
  378. if (gic_data.redist_stride) {
  379. ptr += gic_data.redist_stride;
  380. } else {
  381. ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
  382. if (typer & GICR_TYPER_VLPIS)
  383. ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
  384. }
  385. } while (!(typer & GICR_TYPER_LAST));
  386. }
  387. /* We couldn't even deal with ourselves... */
  388. WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
  389. smp_processor_id(), mpidr);
  390. return -ENODEV;
  391. }
  392. static void gic_cpu_sys_reg_init(void)
  393. {
  394. /*
  395. * Need to check that the SRE bit has actually been set. If
  396. * not, it means that SRE is disabled at EL2. We're going to
  397. * die painfully, and there is nothing we can do about it.
  398. *
  399. * Kindly inform the luser.
  400. */
  401. if (!gic_enable_sre())
  402. pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
  403. /* Set priority mask register */
  404. gic_write_pmr(DEFAULT_PMR_VALUE);
  405. if (static_key_true(&supports_deactivate)) {
  406. /* EOI drops priority only (mode 1) */
  407. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
  408. } else {
  409. /* EOI deactivates interrupt too (mode 0) */
  410. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  411. }
  412. /* ... and let's hit the road... */
  413. gic_write_grpen1(1);
  414. }
  415. static int gic_dist_supports_lpis(void)
  416. {
  417. return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
  418. }
  419. static void gic_cpu_init(void)
  420. {
  421. void __iomem *rbase;
  422. /* Register ourselves with the rest of the world */
  423. if (gic_populate_rdist())
  424. return;
  425. gic_enable_redist(true);
  426. rbase = gic_data_rdist_sgi_base();
  427. /* Configure SGIs/PPIs as non-secure Group-1 */
  428. writel_relaxed(~0, rbase + GICR_IGROUPR0);
  429. gic_cpu_config(rbase, gic_redist_wait_for_rwp);
  430. /* Give LPIs a spin */
  431. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  432. its_cpu_init();
  433. /* initialise system registers */
  434. gic_cpu_sys_reg_init();
  435. }
  436. #ifdef CONFIG_SMP
  437. static int gic_secondary_init(struct notifier_block *nfb,
  438. unsigned long action, void *hcpu)
  439. {
  440. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  441. gic_cpu_init();
  442. return NOTIFY_OK;
  443. }
  444. /*
  445. * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
  446. * priority because the GIC needs to be up before the ARM generic timers.
  447. */
  448. static struct notifier_block gic_cpu_notifier = {
  449. .notifier_call = gic_secondary_init,
  450. .priority = 100,
  451. };
  452. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  453. unsigned long cluster_id)
  454. {
  455. int next_cpu, cpu = *base_cpu;
  456. unsigned long mpidr = cpu_logical_map(cpu);
  457. u16 tlist = 0;
  458. while (cpu < nr_cpu_ids) {
  459. /*
  460. * If we ever get a cluster of more than 16 CPUs, just
  461. * scream and skip that CPU.
  462. */
  463. if (WARN_ON((mpidr & 0xff) >= 16))
  464. goto out;
  465. tlist |= 1 << (mpidr & 0xf);
  466. next_cpu = cpumask_next(cpu, mask);
  467. if (next_cpu >= nr_cpu_ids)
  468. goto out;
  469. cpu = next_cpu;
  470. mpidr = cpu_logical_map(cpu);
  471. if (cluster_id != (mpidr & ~0xffUL)) {
  472. cpu--;
  473. goto out;
  474. }
  475. }
  476. out:
  477. *base_cpu = cpu;
  478. return tlist;
  479. }
  480. #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
  481. (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
  482. << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
  483. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  484. {
  485. u64 val;
  486. val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
  487. MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
  488. irq << ICC_SGI1R_SGI_ID_SHIFT |
  489. MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
  490. tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
  491. pr_devel("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  492. gic_write_sgi1r(val);
  493. }
  494. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  495. {
  496. int cpu;
  497. if (WARN_ON(irq >= 16))
  498. return;
  499. /*
  500. * Ensure that stores to Normal memory are visible to the
  501. * other CPUs before issuing the IPI.
  502. */
  503. wmb();
  504. for_each_cpu(cpu, mask) {
  505. unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
  506. u16 tlist;
  507. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  508. gic_send_sgi(cluster_id, tlist, irq);
  509. }
  510. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  511. isb();
  512. }
  513. static void gic_smp_init(void)
  514. {
  515. set_smp_cross_call(gic_raise_softirq);
  516. register_cpu_notifier(&gic_cpu_notifier);
  517. }
  518. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  519. bool force)
  520. {
  521. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  522. void __iomem *reg;
  523. int enabled;
  524. u64 val;
  525. if (cpu >= nr_cpu_ids)
  526. return -EINVAL;
  527. if (gic_irq_in_rdist(d))
  528. return -EINVAL;
  529. /* If interrupt was enabled, disable it first */
  530. enabled = gic_peek_irq(d, GICD_ISENABLER);
  531. if (enabled)
  532. gic_mask_irq(d);
  533. reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
  534. val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
  535. gic_write_irouter(val, reg);
  536. /*
  537. * If the interrupt was enabled, enabled it again. Otherwise,
  538. * just wait for the distributor to have digested our changes.
  539. */
  540. if (enabled)
  541. gic_unmask_irq(d);
  542. else
  543. gic_dist_wait_for_rwp();
  544. return IRQ_SET_MASK_OK;
  545. }
  546. #else
  547. #define gic_set_affinity NULL
  548. #define gic_smp_init() do { } while(0)
  549. #endif
  550. #ifdef CONFIG_CPU_PM
  551. static int gic_cpu_pm_notifier(struct notifier_block *self,
  552. unsigned long cmd, void *v)
  553. {
  554. if (cmd == CPU_PM_EXIT) {
  555. gic_enable_redist(true);
  556. gic_cpu_sys_reg_init();
  557. } else if (cmd == CPU_PM_ENTER) {
  558. gic_write_grpen1(0);
  559. gic_enable_redist(false);
  560. }
  561. return NOTIFY_OK;
  562. }
  563. static struct notifier_block gic_cpu_pm_notifier_block = {
  564. .notifier_call = gic_cpu_pm_notifier,
  565. };
  566. static void gic_cpu_pm_init(void)
  567. {
  568. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  569. }
  570. #else
  571. static inline void gic_cpu_pm_init(void) { }
  572. #endif /* CONFIG_CPU_PM */
  573. static struct irq_chip gic_chip = {
  574. .name = "GICv3",
  575. .irq_mask = gic_mask_irq,
  576. .irq_unmask = gic_unmask_irq,
  577. .irq_eoi = gic_eoi_irq,
  578. .irq_set_type = gic_set_type,
  579. .irq_set_affinity = gic_set_affinity,
  580. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  581. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  582. .flags = IRQCHIP_SET_TYPE_MASKED,
  583. };
  584. static struct irq_chip gic_eoimode1_chip = {
  585. .name = "GICv3",
  586. .irq_mask = gic_eoimode1_mask_irq,
  587. .irq_unmask = gic_unmask_irq,
  588. .irq_eoi = gic_eoimode1_eoi_irq,
  589. .irq_set_type = gic_set_type,
  590. .irq_set_affinity = gic_set_affinity,
  591. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  592. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  593. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  594. .flags = IRQCHIP_SET_TYPE_MASKED,
  595. };
  596. #define GIC_ID_NR (1U << gic_data.rdists.id_bits)
  597. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  598. irq_hw_number_t hw)
  599. {
  600. struct irq_chip *chip = &gic_chip;
  601. if (static_key_true(&supports_deactivate))
  602. chip = &gic_eoimode1_chip;
  603. /* SGIs are private to the core kernel */
  604. if (hw < 16)
  605. return -EPERM;
  606. /* Nothing here */
  607. if (hw >= gic_data.irq_nr && hw < 8192)
  608. return -EPERM;
  609. /* Off limits */
  610. if (hw >= GIC_ID_NR)
  611. return -EPERM;
  612. /* PPIs */
  613. if (hw < 32) {
  614. irq_set_percpu_devid(irq);
  615. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  616. handle_percpu_devid_irq, NULL, NULL);
  617. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  618. }
  619. /* SPIs */
  620. if (hw >= 32 && hw < gic_data.irq_nr) {
  621. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  622. handle_fasteoi_irq, NULL, NULL);
  623. irq_set_probe(irq);
  624. }
  625. /* LPIs */
  626. if (hw >= 8192 && hw < GIC_ID_NR) {
  627. if (!gic_dist_supports_lpis())
  628. return -EPERM;
  629. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  630. handle_fasteoi_irq, NULL, NULL);
  631. }
  632. return 0;
  633. }
  634. static int gic_irq_domain_translate(struct irq_domain *d,
  635. struct irq_fwspec *fwspec,
  636. unsigned long *hwirq,
  637. unsigned int *type)
  638. {
  639. if (is_of_node(fwspec->fwnode)) {
  640. if (fwspec->param_count < 3)
  641. return -EINVAL;
  642. switch (fwspec->param[0]) {
  643. case 0: /* SPI */
  644. *hwirq = fwspec->param[1] + 32;
  645. break;
  646. case 1: /* PPI */
  647. *hwirq = fwspec->param[1] + 16;
  648. break;
  649. case GIC_IRQ_TYPE_LPI: /* LPI */
  650. *hwirq = fwspec->param[1];
  651. break;
  652. default:
  653. return -EINVAL;
  654. }
  655. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  656. return 0;
  657. }
  658. return -EINVAL;
  659. }
  660. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  661. unsigned int nr_irqs, void *arg)
  662. {
  663. int i, ret;
  664. irq_hw_number_t hwirq;
  665. unsigned int type = IRQ_TYPE_NONE;
  666. struct irq_fwspec *fwspec = arg;
  667. ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  668. if (ret)
  669. return ret;
  670. for (i = 0; i < nr_irqs; i++)
  671. gic_irq_domain_map(domain, virq + i, hwirq + i);
  672. return 0;
  673. }
  674. static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  675. unsigned int nr_irqs)
  676. {
  677. int i;
  678. for (i = 0; i < nr_irqs; i++) {
  679. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  680. irq_set_handler(virq + i, NULL);
  681. irq_domain_reset_irq_data(d);
  682. }
  683. }
  684. static const struct irq_domain_ops gic_irq_domain_ops = {
  685. .translate = gic_irq_domain_translate,
  686. .alloc = gic_irq_domain_alloc,
  687. .free = gic_irq_domain_free,
  688. };
  689. static void gicv3_enable_quirks(void)
  690. {
  691. #ifdef CONFIG_ARM64
  692. if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
  693. static_branch_enable(&is_cavium_thunderx);
  694. #endif
  695. }
  696. static int __init gic_of_init(struct device_node *node, struct device_node *parent)
  697. {
  698. void __iomem *dist_base;
  699. struct redist_region *rdist_regs;
  700. u64 redist_stride;
  701. u32 nr_redist_regions;
  702. u32 typer;
  703. u32 reg;
  704. int gic_irqs;
  705. int err;
  706. int i;
  707. dist_base = of_iomap(node, 0);
  708. if (!dist_base) {
  709. pr_err("%s: unable to map gic dist registers\n",
  710. node->full_name);
  711. return -ENXIO;
  712. }
  713. reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  714. if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4) {
  715. pr_err("%s: no distributor detected, giving up\n",
  716. node->full_name);
  717. err = -ENODEV;
  718. goto out_unmap_dist;
  719. }
  720. if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
  721. nr_redist_regions = 1;
  722. rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
  723. if (!rdist_regs) {
  724. err = -ENOMEM;
  725. goto out_unmap_dist;
  726. }
  727. for (i = 0; i < nr_redist_regions; i++) {
  728. struct resource res;
  729. int ret;
  730. ret = of_address_to_resource(node, 1 + i, &res);
  731. rdist_regs[i].redist_base = of_iomap(node, 1 + i);
  732. if (ret || !rdist_regs[i].redist_base) {
  733. pr_err("%s: couldn't map region %d\n",
  734. node->full_name, i);
  735. err = -ENODEV;
  736. goto out_unmap_rdist;
  737. }
  738. rdist_regs[i].phys_base = res.start;
  739. }
  740. if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
  741. redist_stride = 0;
  742. if (!is_hyp_mode_available())
  743. static_key_slow_dec(&supports_deactivate);
  744. if (static_key_true(&supports_deactivate))
  745. pr_info("GIC: Using split EOI/Deactivate mode\n");
  746. gic_data.dist_base = dist_base;
  747. gic_data.redist_regions = rdist_regs;
  748. gic_data.nr_redist_regions = nr_redist_regions;
  749. gic_data.redist_stride = redist_stride;
  750. gicv3_enable_quirks();
  751. /*
  752. * Find out how many interrupts are supported.
  753. * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
  754. */
  755. typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
  756. gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
  757. gic_irqs = GICD_TYPER_IRQS(typer);
  758. if (gic_irqs > 1020)
  759. gic_irqs = 1020;
  760. gic_data.irq_nr = gic_irqs;
  761. gic_data.domain = irq_domain_add_tree(node, &gic_irq_domain_ops,
  762. &gic_data);
  763. gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
  764. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
  765. err = -ENOMEM;
  766. goto out_free;
  767. }
  768. set_handle_irq(gic_handle_irq);
  769. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  770. its_init(node, &gic_data.rdists, gic_data.domain);
  771. gic_smp_init();
  772. gic_dist_init();
  773. gic_cpu_init();
  774. gic_cpu_pm_init();
  775. return 0;
  776. out_free:
  777. if (gic_data.domain)
  778. irq_domain_remove(gic_data.domain);
  779. free_percpu(gic_data.rdists.rdist);
  780. out_unmap_rdist:
  781. for (i = 0; i < nr_redist_regions; i++)
  782. if (rdist_regs[i].redist_base)
  783. iounmap(rdist_regs[i].redist_base);
  784. kfree(rdist_regs);
  785. out_unmap_dist:
  786. iounmap(dist_base);
  787. return err;
  788. }
  789. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);