irq-gic-v3.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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/arm-gic-v3.h>
  27. #include <asm/cputype.h>
  28. #include <asm/exception.h>
  29. #include <asm/smp_plat.h>
  30. #include "irq-gic-common.h"
  31. #include "irqchip.h"
  32. struct gic_chip_data {
  33. void __iomem *dist_base;
  34. void __iomem **redist_base;
  35. void __iomem * __percpu *rdist;
  36. struct irq_domain *domain;
  37. u64 redist_stride;
  38. u32 redist_regions;
  39. unsigned int irq_nr;
  40. };
  41. static struct gic_chip_data gic_data __read_mostly;
  42. #define gic_data_rdist() (this_cpu_ptr(gic_data.rdist))
  43. #define gic_data_rdist_rd_base() (*gic_data_rdist())
  44. #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
  45. /* Our default, arbitrary priority value. Linux only uses one anyway. */
  46. #define DEFAULT_PMR_VALUE 0xf0
  47. static inline unsigned int gic_irq(struct irq_data *d)
  48. {
  49. return d->hwirq;
  50. }
  51. static inline int gic_irq_in_rdist(struct irq_data *d)
  52. {
  53. return gic_irq(d) < 32;
  54. }
  55. static inline void __iomem *gic_dist_base(struct irq_data *d)
  56. {
  57. if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
  58. return gic_data_rdist_sgi_base();
  59. if (d->hwirq <= 1023) /* SPI -> dist_base */
  60. return gic_data.dist_base;
  61. if (d->hwirq >= 8192)
  62. BUG(); /* LPI Detected!!! */
  63. return NULL;
  64. }
  65. static void gic_do_wait_for_rwp(void __iomem *base)
  66. {
  67. u32 count = 1000000; /* 1s! */
  68. while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
  69. count--;
  70. if (!count) {
  71. pr_err_ratelimited("RWP timeout, gone fishing\n");
  72. return;
  73. }
  74. cpu_relax();
  75. udelay(1);
  76. };
  77. }
  78. /* Wait for completion of a distributor change */
  79. static void gic_dist_wait_for_rwp(void)
  80. {
  81. gic_do_wait_for_rwp(gic_data.dist_base);
  82. }
  83. /* Wait for completion of a redistributor change */
  84. static void gic_redist_wait_for_rwp(void)
  85. {
  86. gic_do_wait_for_rwp(gic_data_rdist_rd_base());
  87. }
  88. /* Low level accessors */
  89. static u64 __maybe_unused gic_read_iar(void)
  90. {
  91. u64 irqstat;
  92. asm volatile("mrs_s %0, " __stringify(ICC_IAR1_EL1) : "=r" (irqstat));
  93. return irqstat;
  94. }
  95. static void __maybe_unused gic_write_pmr(u64 val)
  96. {
  97. asm volatile("msr_s " __stringify(ICC_PMR_EL1) ", %0" : : "r" (val));
  98. }
  99. static void __maybe_unused gic_write_ctlr(u64 val)
  100. {
  101. asm volatile("msr_s " __stringify(ICC_CTLR_EL1) ", %0" : : "r" (val));
  102. isb();
  103. }
  104. static void __maybe_unused gic_write_grpen1(u64 val)
  105. {
  106. asm volatile("msr_s " __stringify(ICC_GRPEN1_EL1) ", %0" : : "r" (val));
  107. isb();
  108. }
  109. static void __maybe_unused gic_write_sgi1r(u64 val)
  110. {
  111. asm volatile("msr_s " __stringify(ICC_SGI1R_EL1) ", %0" : : "r" (val));
  112. }
  113. static void gic_enable_sre(void)
  114. {
  115. u64 val;
  116. asm volatile("mrs_s %0, " __stringify(ICC_SRE_EL1) : "=r" (val));
  117. val |= ICC_SRE_EL1_SRE;
  118. asm volatile("msr_s " __stringify(ICC_SRE_EL1) ", %0" : : "r" (val));
  119. isb();
  120. /*
  121. * Need to check that the SRE bit has actually been set. If
  122. * not, it means that SRE is disabled at EL2. We're going to
  123. * die painfully, and there is nothing we can do about it.
  124. *
  125. * Kindly inform the luser.
  126. */
  127. asm volatile("mrs_s %0, " __stringify(ICC_SRE_EL1) : "=r" (val));
  128. if (!(val & ICC_SRE_EL1_SRE))
  129. pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
  130. }
  131. static void gic_enable_redist(bool enable)
  132. {
  133. void __iomem *rbase;
  134. u32 count = 1000000; /* 1s! */
  135. u32 val;
  136. rbase = gic_data_rdist_rd_base();
  137. val = readl_relaxed(rbase + GICR_WAKER);
  138. if (enable)
  139. /* Wake up this CPU redistributor */
  140. val &= ~GICR_WAKER_ProcessorSleep;
  141. else
  142. val |= GICR_WAKER_ProcessorSleep;
  143. writel_relaxed(val, rbase + GICR_WAKER);
  144. if (!enable) { /* Check that GICR_WAKER is writeable */
  145. val = readl_relaxed(rbase + GICR_WAKER);
  146. if (!(val & GICR_WAKER_ProcessorSleep))
  147. return; /* No PM support in this redistributor */
  148. }
  149. while (count--) {
  150. val = readl_relaxed(rbase + GICR_WAKER);
  151. if (enable ^ (val & GICR_WAKER_ChildrenAsleep))
  152. break;
  153. cpu_relax();
  154. udelay(1);
  155. };
  156. if (!count)
  157. pr_err_ratelimited("redistributor failed to %s...\n",
  158. enable ? "wakeup" : "sleep");
  159. }
  160. /*
  161. * Routines to disable, enable, EOI and route interrupts
  162. */
  163. static void gic_poke_irq(struct irq_data *d, u32 offset)
  164. {
  165. u32 mask = 1 << (gic_irq(d) % 32);
  166. void (*rwp_wait)(void);
  167. void __iomem *base;
  168. if (gic_irq_in_rdist(d)) {
  169. base = gic_data_rdist_sgi_base();
  170. rwp_wait = gic_redist_wait_for_rwp;
  171. } else {
  172. base = gic_data.dist_base;
  173. rwp_wait = gic_dist_wait_for_rwp;
  174. }
  175. writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
  176. rwp_wait();
  177. }
  178. static void gic_mask_irq(struct irq_data *d)
  179. {
  180. gic_poke_irq(d, GICD_ICENABLER);
  181. }
  182. static void gic_unmask_irq(struct irq_data *d)
  183. {
  184. gic_poke_irq(d, GICD_ISENABLER);
  185. }
  186. static void gic_eoi_irq(struct irq_data *d)
  187. {
  188. gic_write_eoir(gic_irq(d));
  189. }
  190. static int gic_set_type(struct irq_data *d, unsigned int type)
  191. {
  192. unsigned int irq = gic_irq(d);
  193. void (*rwp_wait)(void);
  194. void __iomem *base;
  195. /* Interrupt configuration for SGIs can't be changed */
  196. if (irq < 16)
  197. return -EINVAL;
  198. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  199. return -EINVAL;
  200. if (gic_irq_in_rdist(d)) {
  201. base = gic_data_rdist_sgi_base();
  202. rwp_wait = gic_redist_wait_for_rwp;
  203. } else {
  204. base = gic_data.dist_base;
  205. rwp_wait = gic_dist_wait_for_rwp;
  206. }
  207. gic_configure_irq(irq, type, base, rwp_wait);
  208. return 0;
  209. }
  210. static u64 gic_mpidr_to_affinity(u64 mpidr)
  211. {
  212. u64 aff;
  213. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
  214. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  215. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  216. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  217. return aff;
  218. }
  219. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  220. {
  221. u64 irqnr;
  222. do {
  223. irqnr = gic_read_iar();
  224. if (likely(irqnr > 15 && irqnr < 1020)) {
  225. int err;
  226. err = handle_domain_irq(gic_data.domain, irqnr, regs);
  227. if (err) {
  228. WARN_ONCE(true, "Unexpected SPI received!\n");
  229. gic_write_eoir(irqnr);
  230. }
  231. continue;
  232. }
  233. if (irqnr < 16) {
  234. gic_write_eoir(irqnr);
  235. #ifdef CONFIG_SMP
  236. handle_IPI(irqnr, regs);
  237. #else
  238. WARN_ONCE(true, "Unexpected SGI received!\n");
  239. #endif
  240. continue;
  241. }
  242. } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
  243. }
  244. static void __init gic_dist_init(void)
  245. {
  246. unsigned int i;
  247. u64 affinity;
  248. void __iomem *base = gic_data.dist_base;
  249. /* Disable the distributor */
  250. writel_relaxed(0, base + GICD_CTLR);
  251. gic_dist_wait_for_rwp();
  252. gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
  253. /* Enable distributor with ARE, Group1 */
  254. writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
  255. base + GICD_CTLR);
  256. /*
  257. * Set all global interrupts to the boot CPU only. ARE must be
  258. * enabled.
  259. */
  260. affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
  261. for (i = 32; i < gic_data.irq_nr; i++)
  262. writeq_relaxed(affinity, base + GICD_IROUTER + i * 8);
  263. }
  264. static int gic_populate_rdist(void)
  265. {
  266. u64 mpidr = cpu_logical_map(smp_processor_id());
  267. u64 typer;
  268. u32 aff;
  269. int i;
  270. /*
  271. * Convert affinity to a 32bit value that can be matched to
  272. * GICR_TYPER bits [63:32].
  273. */
  274. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
  275. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  276. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  277. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  278. for (i = 0; i < gic_data.redist_regions; i++) {
  279. void __iomem *ptr = gic_data.redist_base[i];
  280. u32 reg;
  281. reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
  282. if (reg != GIC_PIDR2_ARCH_GICv3 &&
  283. reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
  284. pr_warn("No redistributor present @%p\n", ptr);
  285. break;
  286. }
  287. do {
  288. typer = readq_relaxed(ptr + GICR_TYPER);
  289. if ((typer >> 32) == aff) {
  290. gic_data_rdist_rd_base() = ptr;
  291. pr_info("CPU%d: found redistributor %llx @%p\n",
  292. smp_processor_id(),
  293. (unsigned long long)mpidr, ptr);
  294. return 0;
  295. }
  296. if (gic_data.redist_stride) {
  297. ptr += gic_data.redist_stride;
  298. } else {
  299. ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
  300. if (typer & GICR_TYPER_VLPIS)
  301. ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
  302. }
  303. } while (!(typer & GICR_TYPER_LAST));
  304. }
  305. /* We couldn't even deal with ourselves... */
  306. WARN(true, "CPU%d: mpidr %llx has no re-distributor!\n",
  307. smp_processor_id(), (unsigned long long)mpidr);
  308. return -ENODEV;
  309. }
  310. static void gic_cpu_sys_reg_init(void)
  311. {
  312. /* Enable system registers */
  313. gic_enable_sre();
  314. /* Set priority mask register */
  315. gic_write_pmr(DEFAULT_PMR_VALUE);
  316. /* EOI deactivates interrupt too (mode 0) */
  317. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  318. /* ... and let's hit the road... */
  319. gic_write_grpen1(1);
  320. }
  321. static void gic_cpu_init(void)
  322. {
  323. void __iomem *rbase;
  324. /* Register ourselves with the rest of the world */
  325. if (gic_populate_rdist())
  326. return;
  327. gic_enable_redist(true);
  328. rbase = gic_data_rdist_sgi_base();
  329. gic_cpu_config(rbase, gic_redist_wait_for_rwp);
  330. /* initialise system registers */
  331. gic_cpu_sys_reg_init();
  332. }
  333. #ifdef CONFIG_SMP
  334. static int gic_peek_irq(struct irq_data *d, u32 offset)
  335. {
  336. u32 mask = 1 << (gic_irq(d) % 32);
  337. void __iomem *base;
  338. if (gic_irq_in_rdist(d))
  339. base = gic_data_rdist_sgi_base();
  340. else
  341. base = gic_data.dist_base;
  342. return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
  343. }
  344. static int gic_secondary_init(struct notifier_block *nfb,
  345. unsigned long action, void *hcpu)
  346. {
  347. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  348. gic_cpu_init();
  349. return NOTIFY_OK;
  350. }
  351. /*
  352. * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
  353. * priority because the GIC needs to be up before the ARM generic timers.
  354. */
  355. static struct notifier_block gic_cpu_notifier = {
  356. .notifier_call = gic_secondary_init,
  357. .priority = 100,
  358. };
  359. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  360. u64 cluster_id)
  361. {
  362. int cpu = *base_cpu;
  363. u64 mpidr = cpu_logical_map(cpu);
  364. u16 tlist = 0;
  365. while (cpu < nr_cpu_ids) {
  366. /*
  367. * If we ever get a cluster of more than 16 CPUs, just
  368. * scream and skip that CPU.
  369. */
  370. if (WARN_ON((mpidr & 0xff) >= 16))
  371. goto out;
  372. tlist |= 1 << (mpidr & 0xf);
  373. cpu = cpumask_next(cpu, mask);
  374. if (cpu == nr_cpu_ids)
  375. goto out;
  376. mpidr = cpu_logical_map(cpu);
  377. if (cluster_id != (mpidr & ~0xffUL)) {
  378. cpu--;
  379. goto out;
  380. }
  381. }
  382. out:
  383. *base_cpu = cpu;
  384. return tlist;
  385. }
  386. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  387. {
  388. u64 val;
  389. val = (MPIDR_AFFINITY_LEVEL(cluster_id, 3) << 48 |
  390. MPIDR_AFFINITY_LEVEL(cluster_id, 2) << 32 |
  391. irq << 24 |
  392. MPIDR_AFFINITY_LEVEL(cluster_id, 1) << 16 |
  393. tlist);
  394. pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  395. gic_write_sgi1r(val);
  396. }
  397. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  398. {
  399. int cpu;
  400. if (WARN_ON(irq >= 16))
  401. return;
  402. /*
  403. * Ensure that stores to Normal memory are visible to the
  404. * other CPUs before issuing the IPI.
  405. */
  406. smp_wmb();
  407. for_each_cpu_mask(cpu, *mask) {
  408. u64 cluster_id = cpu_logical_map(cpu) & ~0xffUL;
  409. u16 tlist;
  410. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  411. gic_send_sgi(cluster_id, tlist, irq);
  412. }
  413. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  414. isb();
  415. }
  416. static void gic_smp_init(void)
  417. {
  418. set_smp_cross_call(gic_raise_softirq);
  419. register_cpu_notifier(&gic_cpu_notifier);
  420. }
  421. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  422. bool force)
  423. {
  424. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  425. void __iomem *reg;
  426. int enabled;
  427. u64 val;
  428. if (gic_irq_in_rdist(d))
  429. return -EINVAL;
  430. /* If interrupt was enabled, disable it first */
  431. enabled = gic_peek_irq(d, GICD_ISENABLER);
  432. if (enabled)
  433. gic_mask_irq(d);
  434. reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
  435. val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
  436. writeq_relaxed(val, reg);
  437. /*
  438. * If the interrupt was enabled, enabled it again. Otherwise,
  439. * just wait for the distributor to have digested our changes.
  440. */
  441. if (enabled)
  442. gic_unmask_irq(d);
  443. else
  444. gic_dist_wait_for_rwp();
  445. return IRQ_SET_MASK_OK;
  446. }
  447. #else
  448. #define gic_set_affinity NULL
  449. #define gic_smp_init() do { } while(0)
  450. #endif
  451. #ifdef CONFIG_CPU_PM
  452. static int gic_cpu_pm_notifier(struct notifier_block *self,
  453. unsigned long cmd, void *v)
  454. {
  455. if (cmd == CPU_PM_EXIT) {
  456. gic_enable_redist(true);
  457. gic_cpu_sys_reg_init();
  458. } else if (cmd == CPU_PM_ENTER) {
  459. gic_write_grpen1(0);
  460. gic_enable_redist(false);
  461. }
  462. return NOTIFY_OK;
  463. }
  464. static struct notifier_block gic_cpu_pm_notifier_block = {
  465. .notifier_call = gic_cpu_pm_notifier,
  466. };
  467. static void gic_cpu_pm_init(void)
  468. {
  469. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  470. }
  471. #else
  472. static inline void gic_cpu_pm_init(void) { }
  473. #endif /* CONFIG_CPU_PM */
  474. static struct irq_chip gic_chip = {
  475. .name = "GICv3",
  476. .irq_mask = gic_mask_irq,
  477. .irq_unmask = gic_unmask_irq,
  478. .irq_eoi = gic_eoi_irq,
  479. .irq_set_type = gic_set_type,
  480. .irq_set_affinity = gic_set_affinity,
  481. };
  482. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  483. irq_hw_number_t hw)
  484. {
  485. /* SGIs are private to the core kernel */
  486. if (hw < 16)
  487. return -EPERM;
  488. /* PPIs */
  489. if (hw < 32) {
  490. irq_set_percpu_devid(irq);
  491. irq_set_chip_and_handler(irq, &gic_chip,
  492. handle_percpu_devid_irq);
  493. set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
  494. }
  495. /* SPIs */
  496. if (hw >= 32 && hw < gic_data.irq_nr) {
  497. irq_set_chip_and_handler(irq, &gic_chip,
  498. handle_fasteoi_irq);
  499. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  500. }
  501. irq_set_chip_data(irq, d->host_data);
  502. return 0;
  503. }
  504. static int gic_irq_domain_xlate(struct irq_domain *d,
  505. struct device_node *controller,
  506. const u32 *intspec, unsigned int intsize,
  507. unsigned long *out_hwirq, unsigned int *out_type)
  508. {
  509. if (d->of_node != controller)
  510. return -EINVAL;
  511. if (intsize < 3)
  512. return -EINVAL;
  513. switch(intspec[0]) {
  514. case 0: /* SPI */
  515. *out_hwirq = intspec[1] + 32;
  516. break;
  517. case 1: /* PPI */
  518. *out_hwirq = intspec[1] + 16;
  519. break;
  520. default:
  521. return -EINVAL;
  522. }
  523. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  524. return 0;
  525. }
  526. static const struct irq_domain_ops gic_irq_domain_ops = {
  527. .map = gic_irq_domain_map,
  528. .xlate = gic_irq_domain_xlate,
  529. };
  530. static int __init gic_of_init(struct device_node *node, struct device_node *parent)
  531. {
  532. void __iomem *dist_base;
  533. void __iomem **redist_base;
  534. u64 redist_stride;
  535. u32 redist_regions;
  536. u32 reg;
  537. int gic_irqs;
  538. int err;
  539. int i;
  540. dist_base = of_iomap(node, 0);
  541. if (!dist_base) {
  542. pr_err("%s: unable to map gic dist registers\n",
  543. node->full_name);
  544. return -ENXIO;
  545. }
  546. reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  547. if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4) {
  548. pr_err("%s: no distributor detected, giving up\n",
  549. node->full_name);
  550. err = -ENODEV;
  551. goto out_unmap_dist;
  552. }
  553. if (of_property_read_u32(node, "#redistributor-regions", &redist_regions))
  554. redist_regions = 1;
  555. redist_base = kzalloc(sizeof(*redist_base) * redist_regions, GFP_KERNEL);
  556. if (!redist_base) {
  557. err = -ENOMEM;
  558. goto out_unmap_dist;
  559. }
  560. for (i = 0; i < redist_regions; i++) {
  561. redist_base[i] = of_iomap(node, 1 + i);
  562. if (!redist_base[i]) {
  563. pr_err("%s: couldn't map region %d\n",
  564. node->full_name, i);
  565. err = -ENODEV;
  566. goto out_unmap_rdist;
  567. }
  568. }
  569. if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
  570. redist_stride = 0;
  571. gic_data.dist_base = dist_base;
  572. gic_data.redist_base = redist_base;
  573. gic_data.redist_regions = redist_regions;
  574. gic_data.redist_stride = redist_stride;
  575. /*
  576. * Find out how many interrupts are supported.
  577. * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
  578. */
  579. gic_irqs = readl_relaxed(gic_data.dist_base + GICD_TYPER) & 0x1f;
  580. gic_irqs = (gic_irqs + 1) * 32;
  581. if (gic_irqs > 1020)
  582. gic_irqs = 1020;
  583. gic_data.irq_nr = gic_irqs;
  584. gic_data.domain = irq_domain_add_tree(node, &gic_irq_domain_ops,
  585. &gic_data);
  586. gic_data.rdist = alloc_percpu(typeof(*gic_data.rdist));
  587. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdist)) {
  588. err = -ENOMEM;
  589. goto out_free;
  590. }
  591. set_handle_irq(gic_handle_irq);
  592. gic_smp_init();
  593. gic_dist_init();
  594. gic_cpu_init();
  595. gic_cpu_pm_init();
  596. return 0;
  597. out_free:
  598. if (gic_data.domain)
  599. irq_domain_remove(gic_data.domain);
  600. free_percpu(gic_data.rdist);
  601. out_unmap_rdist:
  602. for (i = 0; i < redist_regions; i++)
  603. if (redist_base[i])
  604. iounmap(redist_base[i]);
  605. kfree(redist_base);
  606. out_unmap_dist:
  607. iounmap(dist_base);
  608. return err;
  609. }
  610. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);