gic.c 21 KB

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