irq-gic.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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/irq.h>
  45. #include <asm/exception.h>
  46. #include <asm/smp_plat.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 *__this_cpu_ptr(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. u32 enablemask = 1 << (gicirq % 32);
  168. u32 enableoff = (gicirq / 32) * 4;
  169. u32 confmask = 0x2 << ((gicirq % 16) * 2);
  170. u32 confoff = (gicirq / 16) * 4;
  171. bool enabled = false;
  172. u32 val;
  173. /* Interrupt configuration for SGIs can't be changed */
  174. if (gicirq < 16)
  175. return -EINVAL;
  176. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  177. return -EINVAL;
  178. raw_spin_lock(&irq_controller_lock);
  179. if (gic_arch_extn.irq_set_type)
  180. gic_arch_extn.irq_set_type(d, type);
  181. val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
  182. if (type == IRQ_TYPE_LEVEL_HIGH)
  183. val &= ~confmask;
  184. else if (type == IRQ_TYPE_EDGE_RISING)
  185. val |= confmask;
  186. /*
  187. * As recommended by the spec, disable the interrupt before changing
  188. * the configuration
  189. */
  190. if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
  191. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
  192. enabled = true;
  193. }
  194. writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
  195. if (enabled)
  196. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
  197. raw_spin_unlock(&irq_controller_lock);
  198. return 0;
  199. }
  200. static int gic_retrigger(struct irq_data *d)
  201. {
  202. if (gic_arch_extn.irq_retrigger)
  203. return gic_arch_extn.irq_retrigger(d);
  204. /* the genirq layer expects 0 if we can't retrigger in hardware */
  205. return 0;
  206. }
  207. #ifdef CONFIG_SMP
  208. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  209. bool force)
  210. {
  211. void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
  212. unsigned int shift = (gic_irq(d) % 4) * 8;
  213. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  214. u32 val, mask, bit;
  215. if (cpu >= NR_GIC_CPU_IF || cpu >= nr_cpu_ids)
  216. return -EINVAL;
  217. raw_spin_lock(&irq_controller_lock);
  218. mask = 0xff << shift;
  219. bit = gic_cpu_map[cpu] << shift;
  220. val = readl_relaxed(reg) & ~mask;
  221. writel_relaxed(val | bit, reg);
  222. raw_spin_unlock(&irq_controller_lock);
  223. return IRQ_SET_MASK_OK;
  224. }
  225. #endif
  226. #ifdef CONFIG_PM
  227. static int gic_set_wake(struct irq_data *d, unsigned int on)
  228. {
  229. int ret = -ENXIO;
  230. if (gic_arch_extn.irq_set_wake)
  231. ret = gic_arch_extn.irq_set_wake(d, on);
  232. return ret;
  233. }
  234. #else
  235. #define gic_set_wake NULL
  236. #endif
  237. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  238. {
  239. u32 irqstat, irqnr;
  240. struct gic_chip_data *gic = &gic_data[0];
  241. void __iomem *cpu_base = gic_data_cpu_base(gic);
  242. do {
  243. irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
  244. irqnr = irqstat & ~0x1c00;
  245. if (likely(irqnr > 15 && irqnr < 1021)) {
  246. irqnr = irq_find_mapping(gic->domain, irqnr);
  247. handle_IRQ(irqnr, regs);
  248. continue;
  249. }
  250. if (irqnr < 16) {
  251. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  252. #ifdef CONFIG_SMP
  253. handle_IPI(irqnr, regs);
  254. #endif
  255. continue;
  256. }
  257. break;
  258. } while (1);
  259. }
  260. static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
  261. {
  262. struct gic_chip_data *chip_data = irq_get_handler_data(irq);
  263. struct irq_chip *chip = irq_get_chip(irq);
  264. unsigned int cascade_irq, gic_irq;
  265. unsigned long status;
  266. chained_irq_enter(chip, desc);
  267. raw_spin_lock(&irq_controller_lock);
  268. status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
  269. raw_spin_unlock(&irq_controller_lock);
  270. gic_irq = (status & 0x3ff);
  271. if (gic_irq == 1023)
  272. goto out;
  273. cascade_irq = irq_find_mapping(chip_data->domain, gic_irq);
  274. if (unlikely(gic_irq < 32 || gic_irq > 1020))
  275. handle_bad_irq(cascade_irq, desc);
  276. else
  277. generic_handle_irq(cascade_irq);
  278. out:
  279. chained_irq_exit(chip, desc);
  280. }
  281. static struct irq_chip gic_chip = {
  282. .name = "GIC",
  283. .irq_mask = gic_mask_irq,
  284. .irq_unmask = gic_unmask_irq,
  285. .irq_eoi = gic_eoi_irq,
  286. .irq_set_type = gic_set_type,
  287. .irq_retrigger = gic_retrigger,
  288. #ifdef CONFIG_SMP
  289. .irq_set_affinity = gic_set_affinity,
  290. #endif
  291. .irq_set_wake = gic_set_wake,
  292. };
  293. void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  294. {
  295. if (gic_nr >= MAX_GIC_NR)
  296. BUG();
  297. if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
  298. BUG();
  299. irq_set_chained_handler(irq, gic_handle_cascade_irq);
  300. }
  301. static u8 gic_get_cpumask(struct gic_chip_data *gic)
  302. {
  303. void __iomem *base = gic_data_dist_base(gic);
  304. u32 mask, i;
  305. for (i = mask = 0; i < 32; i += 4) {
  306. mask = readl_relaxed(base + GIC_DIST_TARGET + i);
  307. mask |= mask >> 16;
  308. mask |= mask >> 8;
  309. if (mask)
  310. break;
  311. }
  312. if (!mask)
  313. pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
  314. return mask;
  315. }
  316. static void __init gic_dist_init(struct gic_chip_data *gic)
  317. {
  318. unsigned int i;
  319. u32 cpumask;
  320. unsigned int gic_irqs = gic->gic_irqs;
  321. void __iomem *base = gic_data_dist_base(gic);
  322. writel_relaxed(0, base + GIC_DIST_CTRL);
  323. /*
  324. * Set all global interrupts to be level triggered, active low.
  325. */
  326. for (i = 32; i < gic_irqs; i += 16)
  327. writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
  328. /*
  329. * Set all global interrupts to this CPU only.
  330. */
  331. cpumask = gic_get_cpumask(gic);
  332. cpumask |= cpumask << 8;
  333. cpumask |= cpumask << 16;
  334. for (i = 32; i < gic_irqs; i += 4)
  335. writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  336. /*
  337. * Set priority on all global interrupts.
  338. */
  339. for (i = 32; i < gic_irqs; i += 4)
  340. writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
  341. /*
  342. * Disable all interrupts. Leave the PPI and SGIs alone
  343. * as these enables are banked registers.
  344. */
  345. for (i = 32; i < gic_irqs; i += 32)
  346. writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
  347. writel_relaxed(1, base + GIC_DIST_CTRL);
  348. }
  349. static void gic_cpu_init(struct gic_chip_data *gic)
  350. {
  351. void __iomem *dist_base = gic_data_dist_base(gic);
  352. void __iomem *base = gic_data_cpu_base(gic);
  353. unsigned int cpu_mask, cpu = smp_processor_id();
  354. int i;
  355. /*
  356. * Get what the GIC says our CPU mask is.
  357. */
  358. BUG_ON(cpu >= NR_GIC_CPU_IF);
  359. cpu_mask = gic_get_cpumask(gic);
  360. gic_cpu_map[cpu] = cpu_mask;
  361. /*
  362. * Clear our mask from the other map entries in case they're
  363. * still undefined.
  364. */
  365. for (i = 0; i < NR_GIC_CPU_IF; i++)
  366. if (i != cpu)
  367. gic_cpu_map[i] &= ~cpu_mask;
  368. /*
  369. * Deal with the banked PPI and SGI interrupts - disable all
  370. * PPI interrupts, ensure all SGI interrupts are enabled.
  371. */
  372. writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
  373. writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
  374. /*
  375. * Set priority on PPI and SGI interrupts
  376. */
  377. for (i = 0; i < 32; i += 4)
  378. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
  379. writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
  380. writel_relaxed(1, base + GIC_CPU_CTRL);
  381. }
  382. void gic_cpu_if_down(void)
  383. {
  384. void __iomem *cpu_base = gic_data_cpu_base(&gic_data[0]);
  385. writel_relaxed(0, cpu_base + GIC_CPU_CTRL);
  386. }
  387. #ifdef CONFIG_CPU_PM
  388. /*
  389. * Saves the GIC distributor registers during suspend or idle. Must be called
  390. * with interrupts disabled but before powering down the GIC. After calling
  391. * this function, no interrupts will be delivered by the GIC, and another
  392. * platform-specific wakeup source must be enabled.
  393. */
  394. static void gic_dist_save(unsigned int gic_nr)
  395. {
  396. unsigned int gic_irqs;
  397. void __iomem *dist_base;
  398. int i;
  399. if (gic_nr >= MAX_GIC_NR)
  400. BUG();
  401. gic_irqs = gic_data[gic_nr].gic_irqs;
  402. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  403. if (!dist_base)
  404. return;
  405. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  406. gic_data[gic_nr].saved_spi_conf[i] =
  407. readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  408. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  409. gic_data[gic_nr].saved_spi_target[i] =
  410. readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  411. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  412. gic_data[gic_nr].saved_spi_enable[i] =
  413. readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  414. }
  415. /*
  416. * Restores the GIC distributor registers during resume or when coming out of
  417. * idle. Must be called before enabling interrupts. If a level interrupt
  418. * that occured while the GIC was suspended is still present, it will be
  419. * handled normally, but any edge interrupts that occured will not be seen by
  420. * the GIC and need to be handled by the platform-specific wakeup source.
  421. */
  422. static void gic_dist_restore(unsigned int gic_nr)
  423. {
  424. unsigned int gic_irqs;
  425. unsigned int i;
  426. void __iomem *dist_base;
  427. if (gic_nr >= MAX_GIC_NR)
  428. BUG();
  429. gic_irqs = gic_data[gic_nr].gic_irqs;
  430. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  431. if (!dist_base)
  432. return;
  433. writel_relaxed(0, dist_base + GIC_DIST_CTRL);
  434. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  435. writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
  436. dist_base + GIC_DIST_CONFIG + i * 4);
  437. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  438. writel_relaxed(0xa0a0a0a0,
  439. dist_base + GIC_DIST_PRI + i * 4);
  440. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  441. writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
  442. dist_base + GIC_DIST_TARGET + i * 4);
  443. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  444. writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
  445. dist_base + GIC_DIST_ENABLE_SET + i * 4);
  446. writel_relaxed(1, dist_base + GIC_DIST_CTRL);
  447. }
  448. static void gic_cpu_save(unsigned int gic_nr)
  449. {
  450. int i;
  451. u32 *ptr;
  452. void __iomem *dist_base;
  453. void __iomem *cpu_base;
  454. if (gic_nr >= MAX_GIC_NR)
  455. BUG();
  456. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  457. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  458. if (!dist_base || !cpu_base)
  459. return;
  460. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  461. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  462. ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  463. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  464. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  465. ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  466. }
  467. static void gic_cpu_restore(unsigned int gic_nr)
  468. {
  469. int i;
  470. u32 *ptr;
  471. void __iomem *dist_base;
  472. void __iomem *cpu_base;
  473. if (gic_nr >= MAX_GIC_NR)
  474. BUG();
  475. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  476. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  477. if (!dist_base || !cpu_base)
  478. return;
  479. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  480. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  481. writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
  482. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  483. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  484. writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
  485. for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
  486. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
  487. writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
  488. writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
  489. }
  490. static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  491. {
  492. int i;
  493. for (i = 0; i < MAX_GIC_NR; i++) {
  494. #ifdef CONFIG_GIC_NON_BANKED
  495. /* Skip over unused GICs */
  496. if (!gic_data[i].get_base)
  497. continue;
  498. #endif
  499. switch (cmd) {
  500. case CPU_PM_ENTER:
  501. gic_cpu_save(i);
  502. break;
  503. case CPU_PM_ENTER_FAILED:
  504. case CPU_PM_EXIT:
  505. gic_cpu_restore(i);
  506. break;
  507. case CPU_CLUSTER_PM_ENTER:
  508. gic_dist_save(i);
  509. break;
  510. case CPU_CLUSTER_PM_ENTER_FAILED:
  511. case CPU_CLUSTER_PM_EXIT:
  512. gic_dist_restore(i);
  513. break;
  514. }
  515. }
  516. return NOTIFY_OK;
  517. }
  518. static struct notifier_block gic_notifier_block = {
  519. .notifier_call = gic_notifier,
  520. };
  521. static void __init gic_pm_init(struct gic_chip_data *gic)
  522. {
  523. gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
  524. sizeof(u32));
  525. BUG_ON(!gic->saved_ppi_enable);
  526. gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
  527. sizeof(u32));
  528. BUG_ON(!gic->saved_ppi_conf);
  529. if (gic == &gic_data[0])
  530. cpu_pm_register_notifier(&gic_notifier_block);
  531. }
  532. #else
  533. static void __init gic_pm_init(struct gic_chip_data *gic)
  534. {
  535. }
  536. #endif
  537. #ifdef CONFIG_SMP
  538. void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  539. {
  540. int cpu;
  541. unsigned long flags, map = 0;
  542. raw_spin_lock_irqsave(&irq_controller_lock, flags);
  543. /* Convert our logical CPU mask into a physical one. */
  544. for_each_cpu(cpu, mask)
  545. map |= gic_cpu_map[cpu];
  546. /*
  547. * Ensure that stores to Normal memory are visible to the
  548. * other CPUs before issuing the IPI.
  549. */
  550. dsb();
  551. /* this always happens on GIC0 */
  552. writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
  553. raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
  554. }
  555. #endif
  556. #ifdef CONFIG_BL_SWITCHER
  557. /*
  558. * gic_get_cpu_id - get the CPU interface ID for the specified CPU
  559. *
  560. * @cpu: the logical CPU number to get the GIC ID for.
  561. *
  562. * Return the CPU interface ID for the given logical CPU number,
  563. * or -1 if the CPU number is too large or the interface ID is
  564. * unknown (more than one bit set).
  565. */
  566. int gic_get_cpu_id(unsigned int cpu)
  567. {
  568. unsigned int cpu_bit;
  569. if (cpu >= NR_GIC_CPU_IF)
  570. return -1;
  571. cpu_bit = gic_cpu_map[cpu];
  572. if (cpu_bit & (cpu_bit - 1))
  573. return -1;
  574. return __ffs(cpu_bit);
  575. }
  576. /*
  577. * gic_migrate_target - migrate IRQs to another CPU interface
  578. *
  579. * @new_cpu_id: the CPU target ID to migrate IRQs to
  580. *
  581. * Migrate all peripheral interrupts with a target matching the current CPU
  582. * to the interface corresponding to @new_cpu_id. The CPU interface mapping
  583. * is also updated. Targets to other CPU interfaces are unchanged.
  584. * This must be called with IRQs locally disabled.
  585. */
  586. void gic_migrate_target(unsigned int new_cpu_id)
  587. {
  588. unsigned int cur_cpu_id, gic_irqs, gic_nr = 0;
  589. void __iomem *dist_base;
  590. int i, ror_val, cpu = smp_processor_id();
  591. u32 val, cur_target_mask, active_mask;
  592. if (gic_nr >= MAX_GIC_NR)
  593. BUG();
  594. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  595. if (!dist_base)
  596. return;
  597. gic_irqs = gic_data[gic_nr].gic_irqs;
  598. cur_cpu_id = __ffs(gic_cpu_map[cpu]);
  599. cur_target_mask = 0x01010101 << cur_cpu_id;
  600. ror_val = (cur_cpu_id - new_cpu_id) & 31;
  601. raw_spin_lock(&irq_controller_lock);
  602. /* Update the target interface for this logical CPU */
  603. gic_cpu_map[cpu] = 1 << new_cpu_id;
  604. /*
  605. * Find all the peripheral interrupts targetting the current
  606. * CPU interface and migrate them to the new CPU interface.
  607. * We skip DIST_TARGET 0 to 7 as they are read-only.
  608. */
  609. for (i = 8; i < DIV_ROUND_UP(gic_irqs, 4); i++) {
  610. val = readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  611. active_mask = val & cur_target_mask;
  612. if (active_mask) {
  613. val &= ~active_mask;
  614. val |= ror32(active_mask, ror_val);
  615. writel_relaxed(val, dist_base + GIC_DIST_TARGET + i*4);
  616. }
  617. }
  618. raw_spin_unlock(&irq_controller_lock);
  619. /*
  620. * Now let's migrate and clear any potential SGIs that might be
  621. * pending for us (cur_cpu_id). Since GIC_DIST_SGI_PENDING_SET
  622. * is a banked register, we can only forward the SGI using
  623. * GIC_DIST_SOFTINT. The original SGI source is lost but Linux
  624. * doesn't use that information anyway.
  625. *
  626. * For the same reason we do not adjust SGI source information
  627. * for previously sent SGIs by us to other CPUs either.
  628. */
  629. for (i = 0; i < 16; i += 4) {
  630. int j;
  631. val = readl_relaxed(dist_base + GIC_DIST_SGI_PENDING_SET + i);
  632. if (!val)
  633. continue;
  634. writel_relaxed(val, dist_base + GIC_DIST_SGI_PENDING_CLEAR + i);
  635. for (j = i; j < i + 4; j++) {
  636. if (val & 0xff)
  637. writel_relaxed((1 << (new_cpu_id + 16)) | j,
  638. dist_base + GIC_DIST_SOFTINT);
  639. val >>= 8;
  640. }
  641. }
  642. }
  643. #endif
  644. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  645. irq_hw_number_t hw)
  646. {
  647. if (hw < 32) {
  648. irq_set_percpu_devid(irq);
  649. irq_set_chip_and_handler(irq, &gic_chip,
  650. handle_percpu_devid_irq);
  651. set_irq_flags(irq, IRQF_VALID | IRQF_NOAUTOEN);
  652. } else {
  653. irq_set_chip_and_handler(irq, &gic_chip,
  654. handle_fasteoi_irq);
  655. set_irq_flags(irq, IRQF_VALID | IRQF_PROBE);
  656. }
  657. irq_set_chip_data(irq, d->host_data);
  658. return 0;
  659. }
  660. static int gic_irq_domain_xlate(struct irq_domain *d,
  661. struct device_node *controller,
  662. const u32 *intspec, unsigned int intsize,
  663. unsigned long *out_hwirq, unsigned int *out_type)
  664. {
  665. if (d->of_node != controller)
  666. return -EINVAL;
  667. if (intsize < 3)
  668. return -EINVAL;
  669. /* Get the interrupt number and add 16 to skip over SGIs */
  670. *out_hwirq = intspec[1] + 16;
  671. /* For SPIs, we need to add 16 more to get the GIC irq ID number */
  672. if (!intspec[0])
  673. *out_hwirq += 16;
  674. *out_type = intspec[2] & IRQ_TYPE_SENSE_MASK;
  675. return 0;
  676. }
  677. #ifdef CONFIG_SMP
  678. static int gic_secondary_init(struct notifier_block *nfb, unsigned long action,
  679. void *hcpu)
  680. {
  681. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  682. gic_cpu_init(&gic_data[0]);
  683. return NOTIFY_OK;
  684. }
  685. /*
  686. * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
  687. * priority because the GIC needs to be up before the ARM generic timers.
  688. */
  689. static struct notifier_block gic_cpu_notifier = {
  690. .notifier_call = gic_secondary_init,
  691. .priority = 100,
  692. };
  693. #endif
  694. const struct irq_domain_ops gic_irq_domain_ops = {
  695. .map = gic_irq_domain_map,
  696. .xlate = gic_irq_domain_xlate,
  697. };
  698. void __init gic_init_bases(unsigned int gic_nr, int irq_start,
  699. void __iomem *dist_base, void __iomem *cpu_base,
  700. u32 percpu_offset, struct device_node *node)
  701. {
  702. irq_hw_number_t hwirq_base;
  703. struct gic_chip_data *gic;
  704. int gic_irqs, irq_base, i;
  705. BUG_ON(gic_nr >= MAX_GIC_NR);
  706. gic = &gic_data[gic_nr];
  707. #ifdef CONFIG_GIC_NON_BANKED
  708. if (percpu_offset) { /* Frankein-GIC without banked registers... */
  709. unsigned int cpu;
  710. gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
  711. gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
  712. if (WARN_ON(!gic->dist_base.percpu_base ||
  713. !gic->cpu_base.percpu_base)) {
  714. free_percpu(gic->dist_base.percpu_base);
  715. free_percpu(gic->cpu_base.percpu_base);
  716. return;
  717. }
  718. for_each_possible_cpu(cpu) {
  719. unsigned long offset = percpu_offset * cpu_logical_map(cpu);
  720. *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
  721. *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
  722. }
  723. gic_set_base_accessor(gic, gic_get_percpu_base);
  724. } else
  725. #endif
  726. { /* Normal, sane GIC... */
  727. WARN(percpu_offset,
  728. "GIC_NON_BANKED not enabled, ignoring %08x offset!",
  729. percpu_offset);
  730. gic->dist_base.common_base = dist_base;
  731. gic->cpu_base.common_base = cpu_base;
  732. gic_set_base_accessor(gic, gic_get_common_base);
  733. }
  734. /*
  735. * Initialize the CPU interface map to all CPUs.
  736. * It will be refined as each CPU probes its ID.
  737. */
  738. for (i = 0; i < NR_GIC_CPU_IF; i++)
  739. gic_cpu_map[i] = 0xff;
  740. /*
  741. * For primary GICs, skip over SGIs.
  742. * For secondary GICs, skip over PPIs, too.
  743. */
  744. if (gic_nr == 0 && (irq_start & 31) > 0) {
  745. hwirq_base = 16;
  746. if (irq_start != -1)
  747. irq_start = (irq_start & ~31) + 16;
  748. } else {
  749. hwirq_base = 32;
  750. }
  751. /*
  752. * Find out how many interrupts are supported.
  753. * The GIC only supports up to 1020 interrupt sources.
  754. */
  755. gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
  756. gic_irqs = (gic_irqs + 1) * 32;
  757. if (gic_irqs > 1020)
  758. gic_irqs = 1020;
  759. gic->gic_irqs = gic_irqs;
  760. gic_irqs -= hwirq_base; /* calculate # of irqs to allocate */
  761. irq_base = irq_alloc_descs(irq_start, 16, gic_irqs, numa_node_id());
  762. if (IS_ERR_VALUE(irq_base)) {
  763. WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
  764. irq_start);
  765. irq_base = irq_start;
  766. }
  767. gic->domain = irq_domain_add_legacy(node, gic_irqs, irq_base,
  768. hwirq_base, &gic_irq_domain_ops, gic);
  769. if (WARN_ON(!gic->domain))
  770. return;
  771. #ifdef CONFIG_SMP
  772. set_smp_cross_call(gic_raise_softirq);
  773. register_cpu_notifier(&gic_cpu_notifier);
  774. #endif
  775. set_handle_irq(gic_handle_irq);
  776. gic_chip.flags |= gic_arch_extn.flags;
  777. gic_dist_init(gic);
  778. gic_cpu_init(gic);
  779. gic_pm_init(gic);
  780. }
  781. #ifdef CONFIG_OF
  782. static int gic_cnt __initdata;
  783. int __init gic_of_init(struct device_node *node, struct device_node *parent)
  784. {
  785. void __iomem *cpu_base;
  786. void __iomem *dist_base;
  787. u32 percpu_offset;
  788. int irq;
  789. if (WARN_ON(!node))
  790. return -ENODEV;
  791. dist_base = of_iomap(node, 0);
  792. WARN(!dist_base, "unable to map gic dist registers\n");
  793. cpu_base = of_iomap(node, 1);
  794. WARN(!cpu_base, "unable to map gic cpu registers\n");
  795. if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
  796. percpu_offset = 0;
  797. gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset, node);
  798. if (parent) {
  799. irq = irq_of_parse_and_map(node, 0);
  800. gic_cascade_irq(gic_cnt, irq);
  801. }
  802. gic_cnt++;
  803. return 0;
  804. }
  805. IRQCHIP_DECLARE(cortex_a15_gic, "arm,cortex-a15-gic", gic_of_init);
  806. IRQCHIP_DECLARE(cortex_a9_gic, "arm,cortex-a9-gic", gic_of_init);
  807. IRQCHIP_DECLARE(msm_8660_qgic, "qcom,msm-8660-qgic", gic_of_init);
  808. IRQCHIP_DECLARE(msm_qgic2, "qcom,msm-qgic2", gic_of_init);
  809. #endif