irq-gic-v3.c 22 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. /* Low level accessors */
  93. static u64 __maybe_unused gic_read_iar(void)
  94. {
  95. u64 irqstat;
  96. asm volatile("mrs_s %0, " __stringify(ICC_IAR1_EL1) : "=r" (irqstat));
  97. return irqstat;
  98. }
  99. static void __maybe_unused gic_write_pmr(u64 val)
  100. {
  101. asm volatile("msr_s " __stringify(ICC_PMR_EL1) ", %0" : : "r" (val));
  102. }
  103. static void __maybe_unused gic_write_ctlr(u64 val)
  104. {
  105. asm volatile("msr_s " __stringify(ICC_CTLR_EL1) ", %0" : : "r" (val));
  106. isb();
  107. }
  108. static void __maybe_unused gic_write_grpen1(u64 val)
  109. {
  110. asm volatile("msr_s " __stringify(ICC_GRPEN1_EL1) ", %0" : : "r" (val));
  111. isb();
  112. }
  113. static void __maybe_unused gic_write_sgi1r(u64 val)
  114. {
  115. asm volatile("msr_s " __stringify(ICC_SGI1R_EL1) ", %0" : : "r" (val));
  116. }
  117. static void gic_enable_sre(void)
  118. {
  119. u64 val;
  120. asm volatile("mrs_s %0, " __stringify(ICC_SRE_EL1) : "=r" (val));
  121. val |= ICC_SRE_EL1_SRE;
  122. asm volatile("msr_s " __stringify(ICC_SRE_EL1) ", %0" : : "r" (val));
  123. isb();
  124. /*
  125. * Need to check that the SRE bit has actually been set. If
  126. * not, it means that SRE is disabled at EL2. We're going to
  127. * die painfully, and there is nothing we can do about it.
  128. *
  129. * Kindly inform the luser.
  130. */
  131. asm volatile("mrs_s %0, " __stringify(ICC_SRE_EL1) : "=r" (val));
  132. if (!(val & ICC_SRE_EL1_SRE))
  133. pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
  134. }
  135. static void gic_enable_redist(bool enable)
  136. {
  137. void __iomem *rbase;
  138. u32 count = 1000000; /* 1s! */
  139. u32 val;
  140. rbase = gic_data_rdist_rd_base();
  141. val = readl_relaxed(rbase + GICR_WAKER);
  142. if (enable)
  143. /* Wake up this CPU redistributor */
  144. val &= ~GICR_WAKER_ProcessorSleep;
  145. else
  146. val |= GICR_WAKER_ProcessorSleep;
  147. writel_relaxed(val, rbase + GICR_WAKER);
  148. if (!enable) { /* Check that GICR_WAKER is writeable */
  149. val = readl_relaxed(rbase + GICR_WAKER);
  150. if (!(val & GICR_WAKER_ProcessorSleep))
  151. return; /* No PM support in this redistributor */
  152. }
  153. while (count--) {
  154. val = readl_relaxed(rbase + GICR_WAKER);
  155. if (enable ^ (val & GICR_WAKER_ChildrenAsleep))
  156. break;
  157. cpu_relax();
  158. udelay(1);
  159. };
  160. if (!count)
  161. pr_err_ratelimited("redistributor failed to %s...\n",
  162. enable ? "wakeup" : "sleep");
  163. }
  164. /*
  165. * Routines to disable, enable, EOI and route interrupts
  166. */
  167. static int gic_peek_irq(struct irq_data *d, u32 offset)
  168. {
  169. u32 mask = 1 << (gic_irq(d) % 32);
  170. void __iomem *base;
  171. if (gic_irq_in_rdist(d))
  172. base = gic_data_rdist_sgi_base();
  173. else
  174. base = gic_data.dist_base;
  175. return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
  176. }
  177. static void gic_poke_irq(struct irq_data *d, u32 offset)
  178. {
  179. u32 mask = 1 << (gic_irq(d) % 32);
  180. void (*rwp_wait)(void);
  181. void __iomem *base;
  182. if (gic_irq_in_rdist(d)) {
  183. base = gic_data_rdist_sgi_base();
  184. rwp_wait = gic_redist_wait_for_rwp;
  185. } else {
  186. base = gic_data.dist_base;
  187. rwp_wait = gic_dist_wait_for_rwp;
  188. }
  189. writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
  190. rwp_wait();
  191. }
  192. static void gic_mask_irq(struct irq_data *d)
  193. {
  194. gic_poke_irq(d, GICD_ICENABLER);
  195. }
  196. static void gic_eoimode1_mask_irq(struct irq_data *d)
  197. {
  198. gic_mask_irq(d);
  199. /*
  200. * When masking a forwarded interrupt, make sure it is
  201. * deactivated as well.
  202. *
  203. * This ensures that an interrupt that is getting
  204. * disabled/masked will not get "stuck", because there is
  205. * noone to deactivate it (guest is being terminated).
  206. */
  207. if (irqd_is_forwarded_to_vcpu(d))
  208. gic_poke_irq(d, GICD_ICACTIVER);
  209. }
  210. static void gic_unmask_irq(struct irq_data *d)
  211. {
  212. gic_poke_irq(d, GICD_ISENABLER);
  213. }
  214. static int gic_irq_set_irqchip_state(struct irq_data *d,
  215. enum irqchip_irq_state which, bool val)
  216. {
  217. u32 reg;
  218. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  219. return -EINVAL;
  220. switch (which) {
  221. case IRQCHIP_STATE_PENDING:
  222. reg = val ? GICD_ISPENDR : GICD_ICPENDR;
  223. break;
  224. case IRQCHIP_STATE_ACTIVE:
  225. reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
  226. break;
  227. case IRQCHIP_STATE_MASKED:
  228. reg = val ? GICD_ICENABLER : GICD_ISENABLER;
  229. break;
  230. default:
  231. return -EINVAL;
  232. }
  233. gic_poke_irq(d, reg);
  234. return 0;
  235. }
  236. static int gic_irq_get_irqchip_state(struct irq_data *d,
  237. enum irqchip_irq_state which, bool *val)
  238. {
  239. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  240. return -EINVAL;
  241. switch (which) {
  242. case IRQCHIP_STATE_PENDING:
  243. *val = gic_peek_irq(d, GICD_ISPENDR);
  244. break;
  245. case IRQCHIP_STATE_ACTIVE:
  246. *val = gic_peek_irq(d, GICD_ISACTIVER);
  247. break;
  248. case IRQCHIP_STATE_MASKED:
  249. *val = !gic_peek_irq(d, GICD_ISENABLER);
  250. break;
  251. default:
  252. return -EINVAL;
  253. }
  254. return 0;
  255. }
  256. static void gic_eoi_irq(struct irq_data *d)
  257. {
  258. gic_write_eoir(gic_irq(d));
  259. }
  260. static void gic_eoimode1_eoi_irq(struct irq_data *d)
  261. {
  262. /*
  263. * No need to deactivate an LPI, or an interrupt that
  264. * is is getting forwarded to a vcpu.
  265. */
  266. if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
  267. return;
  268. gic_write_dir(gic_irq(d));
  269. }
  270. static int gic_set_type(struct irq_data *d, unsigned int type)
  271. {
  272. unsigned int irq = gic_irq(d);
  273. void (*rwp_wait)(void);
  274. void __iomem *base;
  275. /* Interrupt configuration for SGIs can't be changed */
  276. if (irq < 16)
  277. return -EINVAL;
  278. /* SPIs have restrictions on the supported types */
  279. if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
  280. type != IRQ_TYPE_EDGE_RISING)
  281. return -EINVAL;
  282. if (gic_irq_in_rdist(d)) {
  283. base = gic_data_rdist_sgi_base();
  284. rwp_wait = gic_redist_wait_for_rwp;
  285. } else {
  286. base = gic_data.dist_base;
  287. rwp_wait = gic_dist_wait_for_rwp;
  288. }
  289. return gic_configure_irq(irq, type, base, rwp_wait);
  290. }
  291. static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  292. {
  293. if (vcpu)
  294. irqd_set_forwarded_to_vcpu(d);
  295. else
  296. irqd_clr_forwarded_to_vcpu(d);
  297. return 0;
  298. }
  299. static u64 gic_mpidr_to_affinity(u64 mpidr)
  300. {
  301. u64 aff;
  302. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
  303. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  304. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  305. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  306. return aff;
  307. }
  308. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  309. {
  310. u64 irqnr;
  311. do {
  312. irqnr = gic_read_iar();
  313. if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
  314. int err;
  315. if (static_key_true(&supports_deactivate))
  316. gic_write_eoir(irqnr);
  317. err = handle_domain_irq(gic_data.domain, irqnr, regs);
  318. if (err) {
  319. WARN_ONCE(true, "Unexpected interrupt received!\n");
  320. if (static_key_true(&supports_deactivate)) {
  321. if (irqnr < 8192)
  322. gic_write_dir(irqnr);
  323. } else {
  324. gic_write_eoir(irqnr);
  325. }
  326. }
  327. continue;
  328. }
  329. if (irqnr < 16) {
  330. gic_write_eoir(irqnr);
  331. if (static_key_true(&supports_deactivate))
  332. gic_write_dir(irqnr);
  333. #ifdef CONFIG_SMP
  334. handle_IPI(irqnr, regs);
  335. #else
  336. WARN_ONCE(true, "Unexpected SGI received!\n");
  337. #endif
  338. continue;
  339. }
  340. } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
  341. }
  342. static void __init gic_dist_init(void)
  343. {
  344. unsigned int i;
  345. u64 affinity;
  346. void __iomem *base = gic_data.dist_base;
  347. /* Disable the distributor */
  348. writel_relaxed(0, base + GICD_CTLR);
  349. gic_dist_wait_for_rwp();
  350. gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
  351. /* Enable distributor with ARE, Group1 */
  352. writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
  353. base + GICD_CTLR);
  354. /*
  355. * Set all global interrupts to the boot CPU only. ARE must be
  356. * enabled.
  357. */
  358. affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
  359. for (i = 32; i < gic_data.irq_nr; i++)
  360. writeq_relaxed(affinity, base + GICD_IROUTER + i * 8);
  361. }
  362. static int gic_populate_rdist(void)
  363. {
  364. u64 mpidr = cpu_logical_map(smp_processor_id());
  365. u64 typer;
  366. u32 aff;
  367. int i;
  368. /*
  369. * Convert affinity to a 32bit value that can be matched to
  370. * GICR_TYPER bits [63:32].
  371. */
  372. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
  373. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  374. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  375. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  376. for (i = 0; i < gic_data.nr_redist_regions; i++) {
  377. void __iomem *ptr = gic_data.redist_regions[i].redist_base;
  378. u32 reg;
  379. reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
  380. if (reg != GIC_PIDR2_ARCH_GICv3 &&
  381. reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
  382. pr_warn("No redistributor present @%p\n", ptr);
  383. break;
  384. }
  385. do {
  386. typer = readq_relaxed(ptr + GICR_TYPER);
  387. if ((typer >> 32) == aff) {
  388. u64 offset = ptr - gic_data.redist_regions[i].redist_base;
  389. gic_data_rdist_rd_base() = ptr;
  390. gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset;
  391. pr_info("CPU%d: found redistributor %llx region %d:%pa\n",
  392. smp_processor_id(),
  393. (unsigned long long)mpidr,
  394. i, &gic_data_rdist()->phys_base);
  395. return 0;
  396. }
  397. if (gic_data.redist_stride) {
  398. ptr += gic_data.redist_stride;
  399. } else {
  400. ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
  401. if (typer & GICR_TYPER_VLPIS)
  402. ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
  403. }
  404. } while (!(typer & GICR_TYPER_LAST));
  405. }
  406. /* We couldn't even deal with ourselves... */
  407. WARN(true, "CPU%d: mpidr %llx has no re-distributor!\n",
  408. smp_processor_id(), (unsigned long long)mpidr);
  409. return -ENODEV;
  410. }
  411. static void gic_cpu_sys_reg_init(void)
  412. {
  413. /* Enable system registers */
  414. gic_enable_sre();
  415. /* Set priority mask register */
  416. gic_write_pmr(DEFAULT_PMR_VALUE);
  417. if (static_key_true(&supports_deactivate)) {
  418. /* EOI drops priority only (mode 1) */
  419. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
  420. } else {
  421. /* EOI deactivates interrupt too (mode 0) */
  422. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  423. }
  424. /* ... and let's hit the road... */
  425. gic_write_grpen1(1);
  426. }
  427. static int gic_dist_supports_lpis(void)
  428. {
  429. return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
  430. }
  431. static void gic_cpu_init(void)
  432. {
  433. void __iomem *rbase;
  434. /* Register ourselves with the rest of the world */
  435. if (gic_populate_rdist())
  436. return;
  437. gic_enable_redist(true);
  438. rbase = gic_data_rdist_sgi_base();
  439. gic_cpu_config(rbase, gic_redist_wait_for_rwp);
  440. /* Give LPIs a spin */
  441. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  442. its_cpu_init();
  443. /* initialise system registers */
  444. gic_cpu_sys_reg_init();
  445. }
  446. #ifdef CONFIG_SMP
  447. static int gic_secondary_init(struct notifier_block *nfb,
  448. unsigned long action, void *hcpu)
  449. {
  450. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  451. gic_cpu_init();
  452. return NOTIFY_OK;
  453. }
  454. /*
  455. * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
  456. * priority because the GIC needs to be up before the ARM generic timers.
  457. */
  458. static struct notifier_block gic_cpu_notifier = {
  459. .notifier_call = gic_secondary_init,
  460. .priority = 100,
  461. };
  462. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  463. u64 cluster_id)
  464. {
  465. int cpu = *base_cpu;
  466. u64 mpidr = cpu_logical_map(cpu);
  467. u16 tlist = 0;
  468. while (cpu < nr_cpu_ids) {
  469. /*
  470. * If we ever get a cluster of more than 16 CPUs, just
  471. * scream and skip that CPU.
  472. */
  473. if (WARN_ON((mpidr & 0xff) >= 16))
  474. goto out;
  475. tlist |= 1 << (mpidr & 0xf);
  476. cpu = cpumask_next(cpu, mask);
  477. if (cpu >= nr_cpu_ids)
  478. goto out;
  479. mpidr = cpu_logical_map(cpu);
  480. if (cluster_id != (mpidr & ~0xffUL)) {
  481. cpu--;
  482. goto out;
  483. }
  484. }
  485. out:
  486. *base_cpu = cpu;
  487. return tlist;
  488. }
  489. #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
  490. (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
  491. << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
  492. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  493. {
  494. u64 val;
  495. val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
  496. MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
  497. irq << ICC_SGI1R_SGI_ID_SHIFT |
  498. MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
  499. tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
  500. pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  501. gic_write_sgi1r(val);
  502. }
  503. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  504. {
  505. int cpu;
  506. if (WARN_ON(irq >= 16))
  507. return;
  508. /*
  509. * Ensure that stores to Normal memory are visible to the
  510. * other CPUs before issuing the IPI.
  511. */
  512. smp_wmb();
  513. for_each_cpu(cpu, mask) {
  514. u64 cluster_id = cpu_logical_map(cpu) & ~0xffUL;
  515. u16 tlist;
  516. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  517. gic_send_sgi(cluster_id, tlist, irq);
  518. }
  519. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  520. isb();
  521. }
  522. static void gic_smp_init(void)
  523. {
  524. set_smp_cross_call(gic_raise_softirq);
  525. register_cpu_notifier(&gic_cpu_notifier);
  526. }
  527. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  528. bool force)
  529. {
  530. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  531. void __iomem *reg;
  532. int enabled;
  533. u64 val;
  534. if (gic_irq_in_rdist(d))
  535. return -EINVAL;
  536. /* If interrupt was enabled, disable it first */
  537. enabled = gic_peek_irq(d, GICD_ISENABLER);
  538. if (enabled)
  539. gic_mask_irq(d);
  540. reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
  541. val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
  542. writeq_relaxed(val, reg);
  543. /*
  544. * If the interrupt was enabled, enabled it again. Otherwise,
  545. * just wait for the distributor to have digested our changes.
  546. */
  547. if (enabled)
  548. gic_unmask_irq(d);
  549. else
  550. gic_dist_wait_for_rwp();
  551. return IRQ_SET_MASK_OK;
  552. }
  553. #else
  554. #define gic_set_affinity NULL
  555. #define gic_smp_init() do { } while(0)
  556. #endif
  557. #ifdef CONFIG_CPU_PM
  558. static int gic_cpu_pm_notifier(struct notifier_block *self,
  559. unsigned long cmd, void *v)
  560. {
  561. if (cmd == CPU_PM_EXIT) {
  562. gic_enable_redist(true);
  563. gic_cpu_sys_reg_init();
  564. } else if (cmd == CPU_PM_ENTER) {
  565. gic_write_grpen1(0);
  566. gic_enable_redist(false);
  567. }
  568. return NOTIFY_OK;
  569. }
  570. static struct notifier_block gic_cpu_pm_notifier_block = {
  571. .notifier_call = gic_cpu_pm_notifier,
  572. };
  573. static void gic_cpu_pm_init(void)
  574. {
  575. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  576. }
  577. #else
  578. static inline void gic_cpu_pm_init(void) { }
  579. #endif /* CONFIG_CPU_PM */
  580. static struct irq_chip gic_chip = {
  581. .name = "GICv3",
  582. .irq_mask = gic_mask_irq,
  583. .irq_unmask = gic_unmask_irq,
  584. .irq_eoi = gic_eoi_irq,
  585. .irq_set_type = gic_set_type,
  586. .irq_set_affinity = gic_set_affinity,
  587. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  588. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  589. .flags = IRQCHIP_SET_TYPE_MASKED,
  590. };
  591. static struct irq_chip gic_eoimode1_chip = {
  592. .name = "GICv3",
  593. .irq_mask = gic_eoimode1_mask_irq,
  594. .irq_unmask = gic_unmask_irq,
  595. .irq_eoi = gic_eoimode1_eoi_irq,
  596. .irq_set_type = gic_set_type,
  597. .irq_set_affinity = gic_set_affinity,
  598. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  599. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  600. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  601. .flags = IRQCHIP_SET_TYPE_MASKED,
  602. };
  603. #define GIC_ID_NR (1U << gic_data.rdists.id_bits)
  604. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  605. irq_hw_number_t hw)
  606. {
  607. struct irq_chip *chip = &gic_chip;
  608. if (static_key_true(&supports_deactivate))
  609. chip = &gic_eoimode1_chip;
  610. /* SGIs are private to the core kernel */
  611. if (hw < 16)
  612. return -EPERM;
  613. /* Nothing here */
  614. if (hw >= gic_data.irq_nr && hw < 8192)
  615. return -EPERM;
  616. /* Off limits */
  617. if (hw >= GIC_ID_NR)
  618. return -EPERM;
  619. /* PPIs */
  620. if (hw < 32) {
  621. irq_set_percpu_devid(irq);
  622. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  623. handle_percpu_devid_irq, NULL, NULL);
  624. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  625. }
  626. /* SPIs */
  627. if (hw >= 32 && hw < gic_data.irq_nr) {
  628. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  629. handle_fasteoi_irq, NULL, NULL);
  630. irq_set_probe(irq);
  631. }
  632. /* LPIs */
  633. if (hw >= 8192 && hw < GIC_ID_NR) {
  634. if (!gic_dist_supports_lpis())
  635. return -EPERM;
  636. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  637. handle_fasteoi_irq, NULL, NULL);
  638. }
  639. return 0;
  640. }
  641. static int gic_irq_domain_xlate(struct irq_domain *d,
  642. struct device_node *controller,
  643. const u32 *intspec, unsigned int intsize,
  644. unsigned long *out_hwirq, unsigned int *out_type)
  645. {
  646. if (d->of_node != controller)
  647. return -EINVAL;
  648. if (intsize < 3)
  649. return -EINVAL;
  650. switch(intspec[0]) {
  651. case 0: /* SPI */
  652. *out_hwirq = intspec[1] + 32;
  653. break;
  654. case 1: /* PPI */
  655. *out_hwirq = intspec[1] + 16;
  656. break;
  657. case GIC_IRQ_TYPE_LPI: /* LPI */
  658. *out_hwirq = intspec[1];
  659. break;
  660. default:
  661. return -EINVAL;
  662. }
  663. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  664. return 0;
  665. }
  666. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  667. unsigned int nr_irqs, void *arg)
  668. {
  669. int i, ret;
  670. irq_hw_number_t hwirq;
  671. unsigned int type = IRQ_TYPE_NONE;
  672. struct of_phandle_args *irq_data = arg;
  673. ret = gic_irq_domain_xlate(domain, irq_data->np, irq_data->args,
  674. irq_data->args_count, &hwirq, &type);
  675. if (ret)
  676. return ret;
  677. for (i = 0; i < nr_irqs; i++)
  678. gic_irq_domain_map(domain, virq + i, hwirq + i);
  679. return 0;
  680. }
  681. static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  682. unsigned int nr_irqs)
  683. {
  684. int i;
  685. for (i = 0; i < nr_irqs; i++) {
  686. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  687. irq_set_handler(virq + i, NULL);
  688. irq_domain_reset_irq_data(d);
  689. }
  690. }
  691. static const struct irq_domain_ops gic_irq_domain_ops = {
  692. .xlate = gic_irq_domain_xlate,
  693. .alloc = gic_irq_domain_alloc,
  694. .free = gic_irq_domain_free,
  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. /*
  751. * Find out how many interrupts are supported.
  752. * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
  753. */
  754. typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
  755. gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
  756. gic_irqs = GICD_TYPER_IRQS(typer);
  757. if (gic_irqs > 1020)
  758. gic_irqs = 1020;
  759. gic_data.irq_nr = gic_irqs;
  760. gic_data.domain = irq_domain_add_tree(node, &gic_irq_domain_ops,
  761. &gic_data);
  762. gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
  763. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
  764. err = -ENOMEM;
  765. goto out_free;
  766. }
  767. set_handle_irq(gic_handle_irq);
  768. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  769. its_init(node, &gic_data.rdists, gic_data.domain);
  770. gic_smp_init();
  771. gic_dist_init();
  772. gic_cpu_init();
  773. gic_cpu_pm_init();
  774. return 0;
  775. out_free:
  776. if (gic_data.domain)
  777. irq_domain_remove(gic_data.domain);
  778. free_percpu(gic_data.rdists.rdist);
  779. out_unmap_rdist:
  780. for (i = 0; i < nr_redist_regions; i++)
  781. if (rdist_regs[i].redist_base)
  782. iounmap(rdist_regs[i].redist_base);
  783. kfree(rdist_regs);
  784. out_unmap_dist:
  785. iounmap(dist_base);
  786. return err;
  787. }
  788. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);