irq-gic.c 28 KB

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