gic.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. /*
  2. * linux/arch/arm/common/gic.c
  3. *
  4. * Copyright (C) 2002 ARM Limited, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Interrupt architecture for the GIC:
  11. *
  12. * o There is one Interrupt Distributor, which receives interrupts
  13. * from system devices and sends them to the Interrupt Controllers.
  14. *
  15. * o There is one CPU Interface per CPU, which sends interrupts sent
  16. * by the Distributor, and interrupts generated locally, to the
  17. * associated CPU. The base address of the CPU interface is usually
  18. * aliased so that the same address points to different chips depending
  19. * on the CPU it is accessed from.
  20. *
  21. * Note that IRQs 0-31 are special - they are local to each CPU.
  22. * As such, the enable set/clear, pending set/clear and active bit
  23. * registers are banked per-cpu for these sources.
  24. */
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/err.h>
  28. #include <linux/module.h>
  29. #include <linux/list.h>
  30. #include <linux/smp.h>
  31. #include <linux/cpu_pm.h>
  32. #include <linux/cpumask.h>
  33. #include <linux/io.h>
  34. #include <linux/of.h>
  35. #include <linux/of_address.h>
  36. #include <linux/of_irq.h>
  37. #include <linux/irqdomain.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/percpu.h>
  40. #include <linux/slab.h>
  41. #include <asm/irq.h>
  42. #include <asm/exception.h>
  43. #include <asm/smp_plat.h>
  44. #include <asm/mach/irq.h>
  45. #include <asm/hardware/gic.h>
  46. union gic_base {
  47. void __iomem *common_base;
  48. void __percpu __iomem **percpu_base;
  49. };
  50. struct gic_chip_data {
  51. unsigned int irq_offset;
  52. union gic_base dist_base;
  53. union gic_base cpu_base;
  54. #ifdef CONFIG_CPU_PM
  55. u32 saved_spi_enable[DIV_ROUND_UP(1020, 32)];
  56. u32 saved_spi_conf[DIV_ROUND_UP(1020, 16)];
  57. u32 saved_spi_target[DIV_ROUND_UP(1020, 4)];
  58. u32 __percpu *saved_ppi_enable;
  59. u32 __percpu *saved_ppi_conf;
  60. #endif
  61. #ifdef CONFIG_IRQ_DOMAIN
  62. struct irq_domain domain;
  63. #endif
  64. unsigned int gic_irqs;
  65. #ifdef CONFIG_GIC_NON_BANKED
  66. void __iomem *(*get_base)(union gic_base *);
  67. #endif
  68. };
  69. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  70. /*
  71. * Supported arch specific GIC irq extension.
  72. * Default make them NULL.
  73. */
  74. struct irq_chip gic_arch_extn = {
  75. .irq_eoi = NULL,
  76. .irq_mask = NULL,
  77. .irq_unmask = NULL,
  78. .irq_retrigger = NULL,
  79. .irq_set_type = NULL,
  80. .irq_set_wake = NULL,
  81. };
  82. #ifndef MAX_GIC_NR
  83. #define MAX_GIC_NR 1
  84. #endif
  85. static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
  86. #ifdef CONFIG_GIC_NON_BANKED
  87. static void __iomem *gic_get_percpu_base(union gic_base *base)
  88. {
  89. return *__this_cpu_ptr(base->percpu_base);
  90. }
  91. static void __iomem *gic_get_common_base(union gic_base *base)
  92. {
  93. return base->common_base;
  94. }
  95. static inline void __iomem *gic_data_dist_base(struct gic_chip_data *data)
  96. {
  97. return data->get_base(&data->dist_base);
  98. }
  99. static inline void __iomem *gic_data_cpu_base(struct gic_chip_data *data)
  100. {
  101. return data->get_base(&data->cpu_base);
  102. }
  103. static inline void gic_set_base_accessor(struct gic_chip_data *data,
  104. void __iomem *(*f)(union gic_base *))
  105. {
  106. data->get_base = f;
  107. }
  108. #else
  109. #define gic_data_dist_base(d) ((d)->dist_base.common_base)
  110. #define gic_data_cpu_base(d) ((d)->cpu_base.common_base)
  111. #define gic_set_base_accessor(d,f)
  112. #endif
  113. static inline void __iomem *gic_dist_base(struct irq_data *d)
  114. {
  115. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  116. return gic_data_dist_base(gic_data);
  117. }
  118. static inline void __iomem *gic_cpu_base(struct irq_data *d)
  119. {
  120. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  121. return gic_data_cpu_base(gic_data);
  122. }
  123. static inline unsigned int gic_irq(struct irq_data *d)
  124. {
  125. return d->hwirq;
  126. }
  127. /*
  128. * Routines to acknowledge, disable and enable interrupts
  129. */
  130. static void gic_mask_irq(struct irq_data *d)
  131. {
  132. u32 mask = 1 << (gic_irq(d) % 32);
  133. raw_spin_lock(&irq_controller_lock);
  134. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
  135. if (gic_arch_extn.irq_mask)
  136. gic_arch_extn.irq_mask(d);
  137. raw_spin_unlock(&irq_controller_lock);
  138. }
  139. static void gic_unmask_irq(struct irq_data *d)
  140. {
  141. u32 mask = 1 << (gic_irq(d) % 32);
  142. raw_spin_lock(&irq_controller_lock);
  143. if (gic_arch_extn.irq_unmask)
  144. gic_arch_extn.irq_unmask(d);
  145. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
  146. raw_spin_unlock(&irq_controller_lock);
  147. }
  148. static void gic_eoi_irq(struct irq_data *d)
  149. {
  150. if (gic_arch_extn.irq_eoi) {
  151. raw_spin_lock(&irq_controller_lock);
  152. gic_arch_extn.irq_eoi(d);
  153. raw_spin_unlock(&irq_controller_lock);
  154. }
  155. writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
  156. }
  157. static int gic_set_type(struct irq_data *d, unsigned int type)
  158. {
  159. void __iomem *base = gic_dist_base(d);
  160. unsigned int gicirq = gic_irq(d);
  161. u32 enablemask = 1 << (gicirq % 32);
  162. u32 enableoff = (gicirq / 32) * 4;
  163. u32 confmask = 0x2 << ((gicirq % 16) * 2);
  164. u32 confoff = (gicirq / 16) * 4;
  165. bool enabled = false;
  166. u32 val;
  167. /* Interrupt configuration for SGIs can't be changed */
  168. if (gicirq < 16)
  169. return -EINVAL;
  170. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  171. return -EINVAL;
  172. raw_spin_lock(&irq_controller_lock);
  173. if (gic_arch_extn.irq_set_type)
  174. gic_arch_extn.irq_set_type(d, type);
  175. val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
  176. if (type == IRQ_TYPE_LEVEL_HIGH)
  177. val &= ~confmask;
  178. else if (type == IRQ_TYPE_EDGE_RISING)
  179. val |= confmask;
  180. /*
  181. * As recommended by the spec, disable the interrupt before changing
  182. * the configuration
  183. */
  184. if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
  185. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
  186. enabled = true;
  187. }
  188. writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
  189. if (enabled)
  190. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
  191. raw_spin_unlock(&irq_controller_lock);
  192. return 0;
  193. }
  194. static int gic_retrigger(struct irq_data *d)
  195. {
  196. if (gic_arch_extn.irq_retrigger)
  197. return gic_arch_extn.irq_retrigger(d);
  198. return -ENXIO;
  199. }
  200. #ifdef CONFIG_SMP
  201. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  202. bool force)
  203. {
  204. void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
  205. unsigned int shift = (gic_irq(d) % 4) * 8;
  206. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  207. u32 val, mask, bit;
  208. if (cpu >= 8 || cpu >= nr_cpu_ids)
  209. return -EINVAL;
  210. mask = 0xff << shift;
  211. bit = 1 << (cpu_logical_map(cpu) + shift);
  212. raw_spin_lock(&irq_controller_lock);
  213. val = readl_relaxed(reg) & ~mask;
  214. writel_relaxed(val | bit, reg);
  215. raw_spin_unlock(&irq_controller_lock);
  216. return IRQ_SET_MASK_OK;
  217. }
  218. #endif
  219. #ifdef CONFIG_PM
  220. static int gic_set_wake(struct irq_data *d, unsigned int on)
  221. {
  222. int ret = -ENXIO;
  223. if (gic_arch_extn.irq_set_wake)
  224. ret = gic_arch_extn.irq_set_wake(d, on);
  225. return ret;
  226. }
  227. #else
  228. #define gic_set_wake NULL
  229. #endif
  230. asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  231. {
  232. u32 irqstat, irqnr;
  233. struct gic_chip_data *gic = &gic_data[0];
  234. void __iomem *cpu_base = gic_data_cpu_base(gic);
  235. do {
  236. irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
  237. irqnr = irqstat & ~0x1c00;
  238. if (likely(irqnr > 15 && irqnr < 1021)) {
  239. irqnr = irq_domain_to_irq(&gic->domain, irqnr);
  240. handle_IRQ(irqnr, regs);
  241. continue;
  242. }
  243. if (irqnr < 16) {
  244. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  245. #ifdef CONFIG_SMP
  246. handle_IPI(irqnr, regs);
  247. #endif
  248. continue;
  249. }
  250. break;
  251. } while (1);
  252. }
  253. static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
  254. {
  255. struct gic_chip_data *chip_data = irq_get_handler_data(irq);
  256. struct irq_chip *chip = irq_get_chip(irq);
  257. unsigned int cascade_irq, gic_irq;
  258. unsigned long status;
  259. chained_irq_enter(chip, desc);
  260. raw_spin_lock(&irq_controller_lock);
  261. status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
  262. raw_spin_unlock(&irq_controller_lock);
  263. gic_irq = (status & 0x3ff);
  264. if (gic_irq == 1023)
  265. goto out;
  266. cascade_irq = irq_domain_to_irq(&chip_data->domain, gic_irq);
  267. if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
  268. do_bad_IRQ(cascade_irq, desc);
  269. else
  270. generic_handle_irq(cascade_irq);
  271. out:
  272. chained_irq_exit(chip, desc);
  273. }
  274. static struct irq_chip gic_chip = {
  275. .name = "GIC",
  276. .irq_mask = gic_mask_irq,
  277. .irq_unmask = gic_unmask_irq,
  278. .irq_eoi = gic_eoi_irq,
  279. .irq_set_type = gic_set_type,
  280. .irq_retrigger = gic_retrigger,
  281. #ifdef CONFIG_SMP
  282. .irq_set_affinity = gic_set_affinity,
  283. #endif
  284. .irq_set_wake = gic_set_wake,
  285. };
  286. void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  287. {
  288. if (gic_nr >= MAX_GIC_NR)
  289. BUG();
  290. if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
  291. BUG();
  292. irq_set_chained_handler(irq, gic_handle_cascade_irq);
  293. }
  294. static void __init gic_dist_init(struct gic_chip_data *gic)
  295. {
  296. unsigned int i, irq;
  297. u32 cpumask;
  298. unsigned int gic_irqs = gic->gic_irqs;
  299. struct irq_domain *domain = &gic->domain;
  300. void __iomem *base = gic_data_dist_base(gic);
  301. u32 cpu = cpu_logical_map(smp_processor_id());
  302. cpumask = 1 << cpu;
  303. cpumask |= cpumask << 8;
  304. cpumask |= cpumask << 16;
  305. writel_relaxed(0, base + GIC_DIST_CTRL);
  306. /*
  307. * Set all global interrupts to be level triggered, active low.
  308. */
  309. for (i = 32; i < gic_irqs; i += 16)
  310. writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
  311. /*
  312. * Set all global interrupts to this CPU only.
  313. */
  314. for (i = 32; i < gic_irqs; i += 4)
  315. writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  316. /*
  317. * Set priority on all global interrupts.
  318. */
  319. for (i = 32; i < gic_irqs; i += 4)
  320. writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
  321. /*
  322. * Disable all interrupts. Leave the PPI and SGIs alone
  323. * as these enables are banked registers.
  324. */
  325. for (i = 32; i < gic_irqs; i += 32)
  326. writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
  327. /*
  328. * Setup the Linux IRQ subsystem.
  329. */
  330. irq_domain_for_each_irq(domain, i, irq) {
  331. if (i < 32) {
  332. irq_set_percpu_devid(irq);
  333. irq_set_chip_and_handler(irq, &gic_chip,
  334. handle_percpu_devid_irq);
  335. set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
  336. } else {
  337. irq_set_chip_and_handler(irq, &gic_chip,
  338. handle_fasteoi_irq);
  339. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  340. }
  341. irq_set_chip_data(irq, gic);
  342. }
  343. writel_relaxed(1, base + GIC_DIST_CTRL);
  344. }
  345. static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
  346. {
  347. void __iomem *dist_base = gic_data_dist_base(gic);
  348. void __iomem *base = gic_data_cpu_base(gic);
  349. int i;
  350. /*
  351. * Deal with the banked PPI and SGI interrupts - disable all
  352. * PPI interrupts, ensure all SGI interrupts are enabled.
  353. */
  354. writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
  355. writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
  356. /*
  357. * Set priority on PPI and SGI interrupts
  358. */
  359. for (i = 0; i < 32; i += 4)
  360. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
  361. writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
  362. writel_relaxed(1, base + GIC_CPU_CTRL);
  363. }
  364. #ifdef CONFIG_CPU_PM
  365. /*
  366. * Saves the GIC distributor registers during suspend or idle. Must be called
  367. * with interrupts disabled but before powering down the GIC. After calling
  368. * this function, no interrupts will be delivered by the GIC, and another
  369. * platform-specific wakeup source must be enabled.
  370. */
  371. static void gic_dist_save(unsigned int gic_nr)
  372. {
  373. unsigned int gic_irqs;
  374. void __iomem *dist_base;
  375. int i;
  376. if (gic_nr >= MAX_GIC_NR)
  377. BUG();
  378. gic_irqs = gic_data[gic_nr].gic_irqs;
  379. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  380. if (!dist_base)
  381. return;
  382. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  383. gic_data[gic_nr].saved_spi_conf[i] =
  384. readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  385. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  386. gic_data[gic_nr].saved_spi_target[i] =
  387. readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  388. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  389. gic_data[gic_nr].saved_spi_enable[i] =
  390. readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  391. }
  392. /*
  393. * Restores the GIC distributor registers during resume or when coming out of
  394. * idle. Must be called before enabling interrupts. If a level interrupt
  395. * that occured while the GIC was suspended is still present, it will be
  396. * handled normally, but any edge interrupts that occured will not be seen by
  397. * the GIC and need to be handled by the platform-specific wakeup source.
  398. */
  399. static void gic_dist_restore(unsigned int gic_nr)
  400. {
  401. unsigned int gic_irqs;
  402. unsigned int i;
  403. void __iomem *dist_base;
  404. if (gic_nr >= MAX_GIC_NR)
  405. BUG();
  406. gic_irqs = gic_data[gic_nr].gic_irqs;
  407. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  408. if (!dist_base)
  409. return;
  410. writel_relaxed(0, dist_base + GIC_DIST_CTRL);
  411. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  412. writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
  413. dist_base + GIC_DIST_CONFIG + i * 4);
  414. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  415. writel_relaxed(0xa0a0a0a0,
  416. dist_base + GIC_DIST_PRI + i * 4);
  417. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  418. writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
  419. dist_base + GIC_DIST_TARGET + i * 4);
  420. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  421. writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
  422. dist_base + GIC_DIST_ENABLE_SET + i * 4);
  423. writel_relaxed(1, dist_base + GIC_DIST_CTRL);
  424. }
  425. static void gic_cpu_save(unsigned int gic_nr)
  426. {
  427. int i;
  428. u32 *ptr;
  429. void __iomem *dist_base;
  430. void __iomem *cpu_base;
  431. if (gic_nr >= MAX_GIC_NR)
  432. BUG();
  433. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  434. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  435. if (!dist_base || !cpu_base)
  436. return;
  437. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  438. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  439. ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  440. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  441. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  442. ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  443. }
  444. static void gic_cpu_restore(unsigned int gic_nr)
  445. {
  446. int i;
  447. u32 *ptr;
  448. void __iomem *dist_base;
  449. void __iomem *cpu_base;
  450. if (gic_nr >= MAX_GIC_NR)
  451. BUG();
  452. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  453. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  454. if (!dist_base || !cpu_base)
  455. return;
  456. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  457. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  458. writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
  459. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  460. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  461. writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
  462. for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
  463. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
  464. writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
  465. writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
  466. }
  467. static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  468. {
  469. int i;
  470. for (i = 0; i < MAX_GIC_NR; i++) {
  471. #ifdef CONFIG_GIC_NON_BANKED
  472. /* Skip over unused GICs */
  473. if (!gic_data[i].get_base)
  474. continue;
  475. #endif
  476. switch (cmd) {
  477. case CPU_PM_ENTER:
  478. gic_cpu_save(i);
  479. break;
  480. case CPU_PM_ENTER_FAILED:
  481. case CPU_PM_EXIT:
  482. gic_cpu_restore(i);
  483. break;
  484. case CPU_CLUSTER_PM_ENTER:
  485. gic_dist_save(i);
  486. break;
  487. case CPU_CLUSTER_PM_ENTER_FAILED:
  488. case CPU_CLUSTER_PM_EXIT:
  489. gic_dist_restore(i);
  490. break;
  491. }
  492. }
  493. return NOTIFY_OK;
  494. }
  495. static struct notifier_block gic_notifier_block = {
  496. .notifier_call = gic_notifier,
  497. };
  498. static void __init gic_pm_init(struct gic_chip_data *gic)
  499. {
  500. gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
  501. sizeof(u32));
  502. BUG_ON(!gic->saved_ppi_enable);
  503. gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
  504. sizeof(u32));
  505. BUG_ON(!gic->saved_ppi_conf);
  506. if (gic == &gic_data[0])
  507. cpu_pm_register_notifier(&gic_notifier_block);
  508. }
  509. #else
  510. static void __init gic_pm_init(struct gic_chip_data *gic)
  511. {
  512. }
  513. #endif
  514. #ifdef CONFIG_OF
  515. static int gic_irq_domain_dt_translate(struct irq_domain *d,
  516. struct device_node *controller,
  517. const u32 *intspec, unsigned int intsize,
  518. unsigned long *out_hwirq, unsigned int *out_type)
  519. {
  520. if (d->of_node != controller)
  521. return -EINVAL;
  522. if (intsize < 3)
  523. return -EINVAL;
  524. /* Get the interrupt number and add 16 to skip over SGIs */
  525. *out_hwirq = intspec[1] + 16;
  526. /* For SPIs, we need to add 16 more to get the GIC irq ID number */
  527. if (!intspec[0])
  528. *out_hwirq += 16;
  529. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  530. return 0;
  531. }
  532. #endif
  533. const struct irq_domain_ops gic_irq_domain_ops = {
  534. #ifdef CONFIG_OF
  535. .dt_translate = gic_irq_domain_dt_translate,
  536. #endif
  537. };
  538. void __init gic_init_bases(unsigned int gic_nr, int irq_start,
  539. void __iomem *dist_base, void __iomem *cpu_base,
  540. u32 percpu_offset)
  541. {
  542. struct gic_chip_data *gic;
  543. struct irq_domain *domain;
  544. int gic_irqs;
  545. BUG_ON(gic_nr >= MAX_GIC_NR);
  546. gic = &gic_data[gic_nr];
  547. domain = &gic->domain;
  548. #ifdef CONFIG_GIC_NON_BANKED
  549. if (percpu_offset) { /* Frankein-GIC without banked registers... */
  550. unsigned int cpu;
  551. gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
  552. gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
  553. if (WARN_ON(!gic->dist_base.percpu_base ||
  554. !gic->cpu_base.percpu_base)) {
  555. free_percpu(gic->dist_base.percpu_base);
  556. free_percpu(gic->cpu_base.percpu_base);
  557. return;
  558. }
  559. for_each_possible_cpu(cpu) {
  560. unsigned long offset = percpu_offset * cpu_logical_map(cpu);
  561. *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
  562. *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
  563. }
  564. gic_set_base_accessor(gic, gic_get_percpu_base);
  565. } else
  566. #endif
  567. { /* Normal, sane GIC... */
  568. WARN(percpu_offset,
  569. "GIC_NON_BANKED not enabled, ignoring %08x offset!",
  570. percpu_offset);
  571. gic->dist_base.common_base = dist_base;
  572. gic->cpu_base.common_base = cpu_base;
  573. gic_set_base_accessor(gic, gic_get_common_base);
  574. }
  575. /*
  576. * For primary GICs, skip over SGIs.
  577. * For secondary GICs, skip over PPIs, too.
  578. */
  579. domain->hwirq_base = 32;
  580. if (gic_nr == 0) {
  581. if ((irq_start & 31) > 0) {
  582. domain->hwirq_base = 16;
  583. if (irq_start != -1)
  584. irq_start = (irq_start & ~31) + 16;
  585. }
  586. }
  587. /*
  588. * Find out how many interrupts are supported.
  589. * The GIC only supports up to 1020 interrupt sources.
  590. */
  591. gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
  592. gic_irqs = (gic_irqs + 1) * 32;
  593. if (gic_irqs > 1020)
  594. gic_irqs = 1020;
  595. gic->gic_irqs = gic_irqs;
  596. domain->nr_irq = gic_irqs - domain->hwirq_base;
  597. domain->irq_base = irq_alloc_descs(irq_start, 16, domain->nr_irq,
  598. numa_node_id());
  599. if (IS_ERR_VALUE(domain->irq_base)) {
  600. WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
  601. irq_start);
  602. domain->irq_base = irq_start;
  603. }
  604. domain->priv = gic;
  605. domain->ops = &gic_irq_domain_ops;
  606. irq_domain_add(domain);
  607. gic_chip.flags |= gic_arch_extn.flags;
  608. gic_dist_init(gic);
  609. gic_cpu_init(gic);
  610. gic_pm_init(gic);
  611. }
  612. void __cpuinit gic_secondary_init(unsigned int gic_nr)
  613. {
  614. BUG_ON(gic_nr >= MAX_GIC_NR);
  615. gic_cpu_init(&gic_data[gic_nr]);
  616. }
  617. #ifdef CONFIG_SMP
  618. void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  619. {
  620. int cpu;
  621. unsigned long map = 0;
  622. /* Convert our logical CPU mask into a physical one. */
  623. for_each_cpu(cpu, mask)
  624. map |= 1 << cpu_logical_map(cpu);
  625. /*
  626. * Ensure that stores to Normal memory are visible to the
  627. * other CPUs before issuing the IPI.
  628. */
  629. dsb();
  630. /* this always happens on GIC0 */
  631. writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
  632. }
  633. #endif
  634. #ifdef CONFIG_OF
  635. static int gic_cnt __initdata = 0;
  636. int __init gic_of_init(struct device_node *node, struct device_node *parent)
  637. {
  638. void __iomem *cpu_base;
  639. void __iomem *dist_base;
  640. u32 percpu_offset;
  641. int irq;
  642. struct irq_domain *domain = &gic_data[gic_cnt].domain;
  643. if (WARN_ON(!node))
  644. return -ENODEV;
  645. dist_base = of_iomap(node, 0);
  646. WARN(!dist_base, "unable to map gic dist registers\n");
  647. cpu_base = of_iomap(node, 1);
  648. WARN(!cpu_base, "unable to map gic cpu registers\n");
  649. if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
  650. percpu_offset = 0;
  651. domain->of_node = of_node_get(node);
  652. gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset);
  653. if (parent) {
  654. irq = irq_of_parse_and_map(node, 0);
  655. gic_cascade_irq(gic_cnt, irq);
  656. }
  657. gic_cnt++;
  658. return 0;
  659. }
  660. #endif