irq-gic.c 26 KB

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