irq-gic.c 26 KB

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