irq-gic.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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.h>
  32. #include <linux/cpu_pm.h>
  33. #include <linux/cpumask.h>
  34. #include <linux/io.h>
  35. #include <linux/of.h>
  36. #include <linux/of_address.h>
  37. #include <linux/of_irq.h>
  38. #include <linux/irqdomain.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/percpu.h>
  41. #include <linux/slab.h>
  42. #include <linux/irqchip/chained_irq.h>
  43. #include <linux/irqchip/arm-gic.h>
  44. #include <asm/cputype.h>
  45. #include <asm/irq.h>
  46. #include <asm/exception.h>
  47. #include <asm/smp_plat.h>
  48. #include "irqchip.h"
  49. union gic_base {
  50. void __iomem *common_base;
  51. void __percpu * __iomem *percpu_base;
  52. };
  53. struct gic_chip_data {
  54. union gic_base dist_base;
  55. union gic_base cpu_base;
  56. #ifdef CONFIG_CPU_PM
  57. u32 saved_spi_enable[DIV_ROUND_UP(1020, 32)];
  58. u32 saved_spi_conf[DIV_ROUND_UP(1020, 16)];
  59. u32 saved_spi_target[DIV_ROUND_UP(1020, 4)];
  60. u32 __percpu *saved_ppi_enable;
  61. u32 __percpu *saved_ppi_conf;
  62. #endif
  63. struct irq_domain *domain;
  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. * The GIC mapping of CPU interfaces does not necessarily match
  72. * the logical CPU numbering. Let's use a mapping as returned
  73. * by the GIC itself.
  74. */
  75. #define NR_GIC_CPU_IF 8
  76. static u8 gic_cpu_map[NR_GIC_CPU_IF] __read_mostly;
  77. /*
  78. * Supported arch specific GIC irq extension.
  79. * Default make them NULL.
  80. */
  81. struct irq_chip gic_arch_extn = {
  82. .irq_eoi = NULL,
  83. .irq_mask = NULL,
  84. .irq_unmask = NULL,
  85. .irq_retrigger = NULL,
  86. .irq_set_type = NULL,
  87. .irq_set_wake = NULL,
  88. };
  89. #ifndef MAX_GIC_NR
  90. #define MAX_GIC_NR 1
  91. #endif
  92. static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
  93. #ifdef CONFIG_GIC_NON_BANKED
  94. static void __iomem *gic_get_percpu_base(union gic_base *base)
  95. {
  96. return *__this_cpu_ptr(base->percpu_base);
  97. }
  98. static void __iomem *gic_get_common_base(union gic_base *base)
  99. {
  100. return base->common_base;
  101. }
  102. static inline void __iomem *gic_data_dist_base(struct gic_chip_data *data)
  103. {
  104. return data->get_base(&data->dist_base);
  105. }
  106. static inline void __iomem *gic_data_cpu_base(struct gic_chip_data *data)
  107. {
  108. return data->get_base(&data->cpu_base);
  109. }
  110. static inline void gic_set_base_accessor(struct gic_chip_data *data,
  111. void __iomem *(*f)(union gic_base *))
  112. {
  113. data->get_base = f;
  114. }
  115. #else
  116. #define gic_data_dist_base(d) ((d)->dist_base.common_base)
  117. #define gic_data_cpu_base(d) ((d)->cpu_base.common_base)
  118. #define gic_set_base_accessor(d, f)
  119. #endif
  120. static inline void __iomem *gic_dist_base(struct irq_data *d)
  121. {
  122. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  123. return gic_data_dist_base(gic_data);
  124. }
  125. static inline void __iomem *gic_cpu_base(struct irq_data *d)
  126. {
  127. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  128. return gic_data_cpu_base(gic_data);
  129. }
  130. static inline unsigned int gic_irq(struct irq_data *d)
  131. {
  132. return d->hwirq;
  133. }
  134. /*
  135. * Routines to acknowledge, disable and enable interrupts
  136. */
  137. static void gic_mask_irq(struct irq_data *d)
  138. {
  139. u32 mask = 1 << (gic_irq(d) % 32);
  140. raw_spin_lock(&irq_controller_lock);
  141. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
  142. if (gic_arch_extn.irq_mask)
  143. gic_arch_extn.irq_mask(d);
  144. raw_spin_unlock(&irq_controller_lock);
  145. }
  146. static void gic_unmask_irq(struct irq_data *d)
  147. {
  148. u32 mask = 1 << (gic_irq(d) % 32);
  149. raw_spin_lock(&irq_controller_lock);
  150. if (gic_arch_extn.irq_unmask)
  151. gic_arch_extn.irq_unmask(d);
  152. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
  153. raw_spin_unlock(&irq_controller_lock);
  154. }
  155. static void gic_eoi_irq(struct irq_data *d)
  156. {
  157. if (gic_arch_extn.irq_eoi) {
  158. raw_spin_lock(&irq_controller_lock);
  159. gic_arch_extn.irq_eoi(d);
  160. raw_spin_unlock(&irq_controller_lock);
  161. }
  162. writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
  163. }
  164. static int gic_set_type(struct irq_data *d, unsigned int type)
  165. {
  166. void __iomem *base = gic_dist_base(d);
  167. unsigned int gicirq = gic_irq(d);
  168. u32 enablemask = 1 << (gicirq % 32);
  169. u32 enableoff = (gicirq / 32) * 4;
  170. u32 confmask = 0x2 << ((gicirq % 16) * 2);
  171. u32 confoff = (gicirq / 16) * 4;
  172. bool enabled = false;
  173. u32 val;
  174. /* Interrupt configuration for SGIs can't be changed */
  175. if (gicirq < 16)
  176. return -EINVAL;
  177. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  178. return -EINVAL;
  179. raw_spin_lock(&irq_controller_lock);
  180. if (gic_arch_extn.irq_set_type)
  181. gic_arch_extn.irq_set_type(d, type);
  182. val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
  183. if (type == IRQ_TYPE_LEVEL_HIGH)
  184. val &= ~confmask;
  185. else if (type == IRQ_TYPE_EDGE_RISING)
  186. val |= confmask;
  187. /*
  188. * As recommended by the spec, disable the interrupt before changing
  189. * the configuration
  190. */
  191. if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
  192. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
  193. enabled = true;
  194. }
  195. writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
  196. if (enabled)
  197. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
  198. raw_spin_unlock(&irq_controller_lock);
  199. return 0;
  200. }
  201. static int gic_retrigger(struct irq_data *d)
  202. {
  203. if (gic_arch_extn.irq_retrigger)
  204. return gic_arch_extn.irq_retrigger(d);
  205. /* the genirq layer expects 0 if we can't retrigger in hardware */
  206. return 0;
  207. }
  208. #ifdef CONFIG_SMP
  209. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  210. bool force)
  211. {
  212. void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
  213. unsigned int cpu, shift = (gic_irq(d) % 4) * 8;
  214. u32 val, mask, bit;
  215. if (!force)
  216. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  217. else
  218. cpu = cpumask_first(mask_val);
  219. if (cpu >= NR_GIC_CPU_IF || cpu >= nr_cpu_ids)
  220. return -EINVAL;
  221. raw_spin_lock(&irq_controller_lock);
  222. mask = 0xff << shift;
  223. bit = gic_cpu_map[cpu] << shift;
  224. val = readl_relaxed(reg) & ~mask;
  225. writel_relaxed(val | bit, reg);
  226. raw_spin_unlock(&irq_controller_lock);
  227. return IRQ_SET_MASK_OK;
  228. }
  229. #endif
  230. #ifdef CONFIG_PM
  231. static int gic_set_wake(struct irq_data *d, unsigned int on)
  232. {
  233. int ret = -ENXIO;
  234. if (gic_arch_extn.irq_set_wake)
  235. ret = gic_arch_extn.irq_set_wake(d, on);
  236. return ret;
  237. }
  238. #else
  239. #define gic_set_wake NULL
  240. #endif
  241. static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  242. {
  243. u32 irqstat, irqnr;
  244. struct gic_chip_data *gic = &gic_data[0];
  245. void __iomem *cpu_base = gic_data_cpu_base(gic);
  246. do {
  247. irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
  248. irqnr = irqstat & GICC_IAR_INT_ID_MASK;
  249. if (likely(irqnr > 15 && irqnr < 1021)) {
  250. irqnr = irq_find_mapping(gic->domain, irqnr);
  251. handle_IRQ(irqnr, regs);
  252. continue;
  253. }
  254. if (irqnr < 16) {
  255. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  256. #ifdef CONFIG_SMP
  257. handle_IPI(irqnr, regs);
  258. #endif
  259. continue;
  260. }
  261. break;
  262. } while (1);
  263. }
  264. static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
  265. {
  266. struct gic_chip_data *chip_data = irq_get_handler_data(irq);
  267. struct irq_chip *chip = irq_get_chip(irq);
  268. unsigned int cascade_irq, gic_irq;
  269. unsigned long status;
  270. chained_irq_enter(chip, desc);
  271. raw_spin_lock(&irq_controller_lock);
  272. status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
  273. raw_spin_unlock(&irq_controller_lock);
  274. gic_irq = (status & 0x3ff);
  275. if (gic_irq == 1023)
  276. goto out;
  277. cascade_irq = irq_find_mapping(chip_data->domain, gic_irq);
  278. if (unlikely(gic_irq < 32 || gic_irq > 1020))
  279. handle_bad_irq(cascade_irq, desc);
  280. else
  281. generic_handle_irq(cascade_irq);
  282. out:
  283. chained_irq_exit(chip, desc);
  284. }
  285. static struct irq_chip gic_chip = {
  286. .name = "GIC",
  287. .irq_mask = gic_mask_irq,
  288. .irq_unmask = gic_unmask_irq,
  289. .irq_eoi = gic_eoi_irq,
  290. .irq_set_type = gic_set_type,
  291. .irq_retrigger = gic_retrigger,
  292. #ifdef CONFIG_SMP
  293. .irq_set_affinity = gic_set_affinity,
  294. #endif
  295. .irq_set_wake = gic_set_wake,
  296. };
  297. void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  298. {
  299. if (gic_nr >= MAX_GIC_NR)
  300. BUG();
  301. if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
  302. BUG();
  303. irq_set_chained_handler(irq, gic_handle_cascade_irq);
  304. }
  305. static u8 gic_get_cpumask(struct gic_chip_data *gic)
  306. {
  307. void __iomem *base = gic_data_dist_base(gic);
  308. u32 mask, i;
  309. for (i = mask = 0; i < 32; i += 4) {
  310. mask = readl_relaxed(base + GIC_DIST_TARGET + i);
  311. mask |= mask >> 16;
  312. mask |= mask >> 8;
  313. if (mask)
  314. break;
  315. }
  316. if (!mask)
  317. pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
  318. return mask;
  319. }
  320. static void __init gic_dist_init(struct gic_chip_data *gic)
  321. {
  322. unsigned int i;
  323. u32 cpumask;
  324. unsigned int gic_irqs = gic->gic_irqs;
  325. void __iomem *base = gic_data_dist_base(gic);
  326. writel_relaxed(0, base + GIC_DIST_CTRL);
  327. /*
  328. * Set all global interrupts to be level triggered, active low.
  329. */
  330. for (i = 32; i < gic_irqs; i += 16)
  331. writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
  332. /*
  333. * Set all global interrupts to this CPU only.
  334. */
  335. cpumask = gic_get_cpumask(gic);
  336. cpumask |= cpumask << 8;
  337. cpumask |= cpumask << 16;
  338. for (i = 32; i < gic_irqs; i += 4)
  339. writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  340. /*
  341. * Set priority on all global interrupts.
  342. */
  343. for (i = 32; i < gic_irqs; i += 4)
  344. writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
  345. /*
  346. * Disable all interrupts. Leave the PPI and SGIs alone
  347. * as these enables are banked registers.
  348. */
  349. for (i = 32; i < gic_irqs; i += 32)
  350. writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
  351. writel_relaxed(1, base + GIC_DIST_CTRL);
  352. }
  353. static void gic_cpu_init(struct gic_chip_data *gic)
  354. {
  355. void __iomem *dist_base = gic_data_dist_base(gic);
  356. void __iomem *base = gic_data_cpu_base(gic);
  357. unsigned int cpu_mask, cpu = smp_processor_id();
  358. int i;
  359. /*
  360. * Get what the GIC says our CPU mask is.
  361. */
  362. BUG_ON(cpu >= NR_GIC_CPU_IF);
  363. cpu_mask = gic_get_cpumask(gic);
  364. gic_cpu_map[cpu] = cpu_mask;
  365. /*
  366. * Clear our mask from the other map entries in case they're
  367. * still undefined.
  368. */
  369. for (i = 0; i < NR_GIC_CPU_IF; i++)
  370. if (i != cpu)
  371. gic_cpu_map[i] &= ~cpu_mask;
  372. /*
  373. * Deal with the banked PPI and SGI interrupts - disable all
  374. * PPI interrupts, ensure all SGI interrupts are enabled.
  375. */
  376. writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
  377. writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
  378. /*
  379. * Set priority on PPI and SGI interrupts
  380. */
  381. for (i = 0; i < 32; i += 4)
  382. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
  383. writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
  384. writel_relaxed(1, base + GIC_CPU_CTRL);
  385. }
  386. void gic_cpu_if_down(void)
  387. {
  388. void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
  389. writel_relaxed(0, cpu_base + GIC_CPU_CTRL);
  390. }
  391. #ifdef CONFIG_CPU_PM
  392. /*
  393. * Saves the GIC distributor registers during suspend or idle. Must be called
  394. * with interrupts disabled but before powering down the GIC. After calling
  395. * this function, no interrupts will be delivered by the GIC, and another
  396. * platform-specific wakeup source must be enabled.
  397. */
  398. static void gic_dist_save(unsigned int gic_nr)
  399. {
  400. unsigned int gic_irqs;
  401. void __iomem *dist_base;
  402. int i;
  403. if (gic_nr >= MAX_GIC_NR)
  404. BUG();
  405. gic_irqs = gic_data[gic_nr].gic_irqs;
  406. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  407. if (!dist_base)
  408. return;
  409. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  410. gic_data[gic_nr].saved_spi_conf[i] =
  411. readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  412. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  413. gic_data[gic_nr].saved_spi_target[i] =
  414. readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  415. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  416. gic_data[gic_nr].saved_spi_enable[i] =
  417. readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  418. }
  419. /*
  420. * Restores the GIC distributor registers during resume or when coming out of
  421. * idle. Must be called before enabling interrupts. If a level interrupt
  422. * that occured while the GIC was suspended is still present, it will be
  423. * handled normally, but any edge interrupts that occured will not be seen by
  424. * the GIC and need to be handled by the platform-specific wakeup source.
  425. */
  426. static void gic_dist_restore(unsigned int gic_nr)
  427. {
  428. unsigned int gic_irqs;
  429. unsigned int i;
  430. void __iomem *dist_base;
  431. if (gic_nr >= MAX_GIC_NR)
  432. BUG();
  433. gic_irqs = gic_data[gic_nr].gic_irqs;
  434. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  435. if (!dist_base)
  436. return;
  437. writel_relaxed(0, dist_base + GIC_DIST_CTRL);
  438. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  439. writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
  440. dist_base + GIC_DIST_CONFIG + i * 4);
  441. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  442. writel_relaxed(0xa0a0a0a0,
  443. dist_base + GIC_DIST_PRI + i * 4);
  444. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  445. writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
  446. dist_base + GIC_DIST_TARGET + i * 4);
  447. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  448. writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
  449. dist_base + GIC_DIST_ENABLE_SET + i * 4);
  450. writel_relaxed(1, dist_base + GIC_DIST_CTRL);
  451. }
  452. static void gic_cpu_save(unsigned int gic_nr)
  453. {
  454. int i;
  455. u32 *ptr;
  456. void __iomem *dist_base;
  457. void __iomem *cpu_base;
  458. if (gic_nr >= MAX_GIC_NR)
  459. BUG();
  460. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  461. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  462. if (!dist_base || !cpu_base)
  463. return;
  464. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  465. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  466. ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  467. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  468. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  469. ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  470. }
  471. static void gic_cpu_restore(unsigned int gic_nr)
  472. {
  473. int i;
  474. u32 *ptr;
  475. void __iomem *dist_base;
  476. void __iomem *cpu_base;
  477. if (gic_nr >= MAX_GIC_NR)
  478. BUG();
  479. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  480. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  481. if (!dist_base || !cpu_base)
  482. return;
  483. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  484. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  485. writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
  486. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  487. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  488. writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
  489. for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
  490. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
  491. writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
  492. writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
  493. }
  494. static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  495. {
  496. int i;
  497. for (i = 0; i < MAX_GIC_NR; i++) {
  498. #ifdef CONFIG_GIC_NON_BANKED
  499. /* Skip over unused GICs */
  500. if (!gic_data[i].get_base)
  501. continue;
  502. #endif
  503. switch (cmd) {
  504. case CPU_PM_ENTER:
  505. gic_cpu_save(i);
  506. break;
  507. case CPU_PM_ENTER_FAILED:
  508. case CPU_PM_EXIT:
  509. gic_cpu_restore(i);
  510. break;
  511. case CPU_CLUSTER_PM_ENTER:
  512. gic_dist_save(i);
  513. break;
  514. case CPU_CLUSTER_PM_ENTER_FAILED:
  515. case CPU_CLUSTER_PM_EXIT:
  516. gic_dist_restore(i);
  517. break;
  518. }
  519. }
  520. return NOTIFY_OK;
  521. }
  522. static struct notifier_block gic_notifier_block = {
  523. .notifier_call = gic_notifier,
  524. };
  525. static void __init gic_pm_init(struct gic_chip_data *gic)
  526. {
  527. gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
  528. sizeof(u32));
  529. BUG_ON(!gic->saved_ppi_enable);
  530. gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
  531. sizeof(u32));
  532. BUG_ON(!gic->saved_ppi_conf);
  533. if (gic == &gic_data[0])
  534. cpu_pm_register_notifier(&gic_notifier_block);
  535. }
  536. #else
  537. static void __init gic_pm_init(struct gic_chip_data *gic)
  538. {
  539. }
  540. #endif
  541. #ifdef CONFIG_SMP
  542. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  543. {
  544. int cpu;
  545. unsigned long flags, map = 0;
  546. raw_spin_lock_irqsave(&irq_controller_lock, flags);
  547. /* Convert our logical CPU mask into a physical one. */
  548. for_each_cpu(cpu, mask)
  549. map |= gic_cpu_map[cpu];
  550. /*
  551. * Ensure that stores to Normal memory are visible to the
  552. * other CPUs before they observe us issuing the IPI.
  553. */
  554. dmb(ishst);
  555. /* this always happens on GIC0 */
  556. writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
  557. raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
  558. }
  559. #endif
  560. #ifdef CONFIG_BL_SWITCHER
  561. /*
  562. * gic_send_sgi - send a SGI directly to given CPU interface number
  563. *
  564. * cpu_id: the ID for the destination CPU interface
  565. * irq: the IPI number to send a SGI for
  566. */
  567. void gic_send_sgi(unsigned int cpu_id, unsigned int irq)
  568. {
  569. BUG_ON(cpu_id >= NR_GIC_CPU_IF);
  570. cpu_id = 1 << cpu_id;
  571. /* this always happens on GIC0 */
  572. writel_relaxed((cpu_id << 16) | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
  573. }
  574. /*
  575. * gic_get_cpu_id - get the CPU interface ID for the specified CPU
  576. *
  577. * @cpu: the logical CPU number to get the GIC ID for.
  578. *
  579. * Return the CPU interface ID for the given logical CPU number,
  580. * or -1 if the CPU number is too large or the interface ID is
  581. * unknown (more than one bit set).
  582. */
  583. int gic_get_cpu_id(unsigned int cpu)
  584. {
  585. unsigned int cpu_bit;
  586. if (cpu >= NR_GIC_CPU_IF)
  587. return -1;
  588. cpu_bit = gic_cpu_map[cpu];
  589. if (cpu_bit & (cpu_bit - 1))
  590. return -1;
  591. return __ffs(cpu_bit);
  592. }
  593. /*
  594. * gic_migrate_target - migrate IRQs to another CPU interface
  595. *
  596. * @new_cpu_id: the CPU target ID to migrate IRQs to
  597. *
  598. * Migrate all peripheral interrupts with a target matching the current CPU
  599. * to the interface corresponding to @new_cpu_id. The CPU interface mapping
  600. * is also updated. Targets to other CPU interfaces are unchanged.
  601. * This must be called with IRQs locally disabled.
  602. */
  603. void gic_migrate_target(unsigned int new_cpu_id)
  604. {
  605. unsigned int cur_cpu_id, gic_irqs, gic_nr = 0;
  606. void __iomem *dist_base;
  607. int i, ror_val, cpu = smp_processor_id();
  608. u32 val, cur_target_mask, active_mask;
  609. if (gic_nr >= MAX_GIC_NR)
  610. BUG();
  611. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  612. if (!dist_base)
  613. return;
  614. gic_irqs = gic_data[gic_nr].gic_irqs;
  615. cur_cpu_id = __ffs(gic_cpu_map[cpu]);
  616. cur_target_mask = 0x01010101 << cur_cpu_id;
  617. ror_val = (cur_cpu_id - new_cpu_id) & 31;
  618. raw_spin_lock(&irq_controller_lock);
  619. /* Update the target interface for this logical CPU */
  620. gic_cpu_map[cpu] = 1 << new_cpu_id;
  621. /*
  622. * Find all the peripheral interrupts targetting the current
  623. * CPU interface and migrate them to the new CPU interface.
  624. * We skip DIST_TARGET 0 to 7 as they are read-only.
  625. */
  626. for (i = 8; i < DIV_ROUND_UP(gic_irqs, 4); i++) {
  627. val = readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  628. active_mask = val & cur_target_mask;
  629. if (active_mask) {
  630. val &= ~active_mask;
  631. val |= ror32(active_mask, ror_val);
  632. writel_relaxed(val, dist_base + GIC_DIST_TARGET + i*4);
  633. }
  634. }
  635. raw_spin_unlock(&irq_controller_lock);
  636. /*
  637. * Now let's migrate and clear any potential SGIs that might be
  638. * pending for us (cur_cpu_id). Since GIC_DIST_SGI_PENDING_SET
  639. * is a banked register, we can only forward the SGI using
  640. * GIC_DIST_SOFTINT. The original SGI source is lost but Linux
  641. * doesn't use that information anyway.
  642. *
  643. * For the same reason we do not adjust SGI source information
  644. * for previously sent SGIs by us to other CPUs either.
  645. */
  646. for (i = 0; i < 16; i += 4) {
  647. int j;
  648. val = readl_relaxed(dist_base + GIC_DIST_SGI_PENDING_SET + i);
  649. if (!val)
  650. continue;
  651. writel_relaxed(val, dist_base + GIC_DIST_SGI_PENDING_CLEAR + i);
  652. for (j = i; j < i + 4; j++) {
  653. if (val & 0xff)
  654. writel_relaxed((1 << (new_cpu_id + 16)) | j,
  655. dist_base + GIC_DIST_SOFTINT);
  656. val >>= 8;
  657. }
  658. }
  659. }
  660. /*
  661. * gic_get_sgir_physaddr - get the physical address for the SGI register
  662. *
  663. * REturn the physical address of the SGI register to be used
  664. * by some early assembly code when the kernel is not yet available.
  665. */
  666. static unsigned long gic_dist_physaddr;
  667. unsigned long gic_get_sgir_physaddr(void)
  668. {
  669. if (!gic_dist_physaddr)
  670. return 0;
  671. return gic_dist_physaddr + GIC_DIST_SOFTINT;
  672. }
  673. void __init gic_init_physaddr(struct device_node *node)
  674. {
  675. struct resource res;
  676. if (of_address_to_resource(node, 0, &res) == 0) {
  677. gic_dist_physaddr = res.start;
  678. pr_info("GIC physical location is %#lx\n", gic_dist_physaddr);
  679. }
  680. }
  681. #else
  682. #define gic_init_physaddr(node) do { } while (0)
  683. #endif
  684. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  685. irq_hw_number_t hw)
  686. {
  687. if (hw < 32) {
  688. irq_set_percpu_devid(irq);
  689. irq_set_chip_and_handler(irq, &gic_chip,
  690. handle_percpu_devid_irq);
  691. set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
  692. } else {
  693. irq_set_chip_and_handler(irq, &gic_chip,
  694. handle_fasteoi_irq);
  695. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  696. gic_routable_irq_domain_ops->map(d, irq, hw);
  697. }
  698. irq_set_chip_data(irq, d->host_data);
  699. return 0;
  700. }
  701. static void gic_irq_domain_unmap(struct irq_domain *d, unsigned int irq)
  702. {
  703. gic_routable_irq_domain_ops->unmap(d, irq);
  704. }
  705. static int gic_irq_domain_xlate(struct irq_domain *d,
  706. struct device_node *controller,
  707. const u32 *intspec, unsigned int intsize,
  708. unsigned long *out_hwirq, unsigned int *out_type)
  709. {
  710. unsigned long ret = 0;
  711. if (d->of_node != controller)
  712. return -EINVAL;
  713. if (intsize < 3)
  714. return -EINVAL;
  715. /* Get the interrupt number and add 16 to skip over SGIs */
  716. *out_hwirq = intspec[1] + 16;
  717. /* For SPIs, we need to add 16 more to get the GIC irq ID number */
  718. if (!intspec[0]) {
  719. ret = gic_routable_irq_domain_ops->xlate(d, controller,
  720. intspec,
  721. intsize,
  722. out_hwirq,
  723. out_type);
  724. if (IS_ERR_VALUE(ret))
  725. return ret;
  726. }
  727. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  728. return ret;
  729. }
  730. #ifdef CONFIG_SMP
  731. static int gic_secondary_init(struct notifier_block *nfb, unsigned long action,
  732. void *hcpu)
  733. {
  734. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  735. gic_cpu_init(&gic_data[0]);
  736. return NOTIFY_OK;
  737. }
  738. /*
  739. * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
  740. * priority because the GIC needs to be up before the ARM generic timers.
  741. */
  742. static struct notifier_block gic_cpu_notifier = {
  743. .notifier_call = gic_secondary_init,
  744. .priority = 100,
  745. };
  746. #endif
  747. static const struct irq_domain_ops gic_irq_domain_ops = {
  748. .map = gic_irq_domain_map,
  749. .unmap = gic_irq_domain_unmap,
  750. .xlate = gic_irq_domain_xlate,
  751. };
  752. /* Default functions for routable irq domain */
  753. static int gic_routable_irq_domain_map(struct irq_domain *d, unsigned int irq,
  754. irq_hw_number_t hw)
  755. {
  756. return 0;
  757. }
  758. static void gic_routable_irq_domain_unmap(struct irq_domain *d,
  759. unsigned int irq)
  760. {
  761. }
  762. static int gic_routable_irq_domain_xlate(struct irq_domain *d,
  763. struct device_node *controller,
  764. const u32 *intspec, unsigned int intsize,
  765. unsigned long *out_hwirq,
  766. unsigned int *out_type)
  767. {
  768. *out_hwirq += 16;
  769. return 0;
  770. }
  771. const struct irq_domain_ops gic_default_routable_irq_domain_ops = {
  772. .map = gic_routable_irq_domain_map,
  773. .unmap = gic_routable_irq_domain_unmap,
  774. .xlate = gic_routable_irq_domain_xlate,
  775. };
  776. const struct irq_domain_ops *gic_routable_irq_domain_ops =
  777. &gic_default_routable_irq_domain_ops;
  778. void __init gic_init_bases(unsigned int gic_nr, int irq_start,
  779. void __iomem *dist_base, void __iomem *cpu_base,
  780. u32 percpu_offset, struct device_node *node)
  781. {
  782. irq_hw_number_t hwirq_base;
  783. struct gic_chip_data *gic;
  784. int gic_irqs, irq_base, i;
  785. int nr_routable_irqs;
  786. BUG_ON(gic_nr >= MAX_GIC_NR);
  787. gic = &gic_data[gic_nr];
  788. #ifdef CONFIG_GIC_NON_BANKED
  789. if (percpu_offset) { /* Frankein-GIC without banked registers... */
  790. unsigned int cpu;
  791. gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
  792. gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
  793. if (WARN_ON(!gic->dist_base.percpu_base ||
  794. !gic->cpu_base.percpu_base)) {
  795. free_percpu(gic->dist_base.percpu_base);
  796. free_percpu(gic->cpu_base.percpu_base);
  797. return;
  798. }
  799. for_each_possible_cpu(cpu) {
  800. u32 mpidr = cpu_logical_map(cpu);
  801. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  802. unsigned long offset = percpu_offset * core_id;
  803. *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
  804. *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
  805. }
  806. gic_set_base_accessor(gic, gic_get_percpu_base);
  807. } else
  808. #endif
  809. { /* Normal, sane GIC... */
  810. WARN(percpu_offset,
  811. "GIC_NON_BANKED not enabled, ignoring %08x offset!",
  812. percpu_offset);
  813. gic->dist_base.common_base = dist_base;
  814. gic->cpu_base.common_base = cpu_base;
  815. gic_set_base_accessor(gic, gic_get_common_base);
  816. }
  817. /*
  818. * Initialize the CPU interface map to all CPUs.
  819. * It will be refined as each CPU probes its ID.
  820. */
  821. for (i = 0; i < NR_GIC_CPU_IF; i++)
  822. gic_cpu_map[i] = 0xff;
  823. /*
  824. * For primary GICs, skip over SGIs.
  825. * For secondary GICs, skip over PPIs, too.
  826. */
  827. if (gic_nr == 0 && (irq_start & 31) > 0) {
  828. hwirq_base = 16;
  829. if (irq_start != -1)
  830. irq_start = (irq_start & ~31) + 16;
  831. } else {
  832. hwirq_base = 32;
  833. }
  834. /*
  835. * Find out how many interrupts are supported.
  836. * The GIC only supports up to 1020 interrupt sources.
  837. */
  838. gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
  839. gic_irqs = (gic_irqs + 1) * 32;
  840. if (gic_irqs > 1020)
  841. gic_irqs = 1020;
  842. gic->gic_irqs = gic_irqs;
  843. gic_irqs -= hwirq_base; /* calculate # of irqs to allocate */
  844. if (of_property_read_u32(node, "arm,routable-irqs",
  845. &nr_routable_irqs)) {
  846. irq_base = irq_alloc_descs(irq_start, 16, gic_irqs,
  847. numa_node_id());
  848. if (IS_ERR_VALUE(irq_base)) {
  849. WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
  850. irq_start);
  851. irq_base = irq_start;
  852. }
  853. gic->domain = irq_domain_add_legacy(node, gic_irqs, irq_base,
  854. hwirq_base, &gic_irq_domain_ops, gic);
  855. } else {
  856. gic->domain = irq_domain_add_linear(node, nr_routable_irqs,
  857. &gic_irq_domain_ops,
  858. gic);
  859. }
  860. if (WARN_ON(!gic->domain))
  861. return;
  862. if (gic_nr == 0) {
  863. #ifdef CONFIG_SMP
  864. set_smp_cross_call(gic_raise_softirq);
  865. register_cpu_notifier(&gic_cpu_notifier);
  866. #endif
  867. set_handle_irq(gic_handle_irq);
  868. }
  869. gic_chip.flags |= gic_arch_extn.flags;
  870. gic_dist_init(gic);
  871. gic_cpu_init(gic);
  872. gic_pm_init(gic);
  873. }
  874. #ifdef CONFIG_OF
  875. static int gic_cnt __initdata;
  876. static int __init
  877. gic_of_init(struct device_node *node, struct device_node *parent)
  878. {
  879. void __iomem *cpu_base;
  880. void __iomem *dist_base;
  881. u32 percpu_offset;
  882. int irq;
  883. if (WARN_ON(!node))
  884. return -ENODEV;
  885. dist_base = of_iomap(node, 0);
  886. WARN(!dist_base, "unable to map gic dist registers\n");
  887. cpu_base = of_iomap(node, 1);
  888. WARN(!cpu_base, "unable to map gic cpu registers\n");
  889. if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
  890. percpu_offset = 0;
  891. gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset, node);
  892. if (!gic_cnt)
  893. gic_init_physaddr(node);
  894. if (parent) {
  895. irq = irq_of_parse_and_map(node, 0);
  896. gic_cascade_irq(gic_cnt, irq);
  897. }
  898. gic_cnt++;
  899. return 0;
  900. }
  901. IRQCHIP_DECLARE(gic_400, "arm,gic-400", gic_of_init);
  902. IRQCHIP_DECLARE(cortex_a15_gic, "arm,cortex-a15-gic", gic_of_init);
  903. IRQCHIP_DECLARE(cortex_a9_gic, "arm,cortex-a9-gic", gic_of_init);
  904. IRQCHIP_DECLARE(cortex_a7_gic, "arm,cortex-a7-gic", gic_of_init);
  905. IRQCHIP_DECLARE(msm_8660_qgic, "qcom,msm-8660-qgic", gic_of_init);
  906. IRQCHIP_DECLARE(msm_qgic2, "qcom,msm-qgic2", gic_of_init);
  907. #endif