gic.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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/list.h>
  28. #include <linux/smp.h>
  29. #include <linux/cpu_pm.h>
  30. #include <linux/cpumask.h>
  31. #include <linux/io.h>
  32. #include <asm/irq.h>
  33. #include <asm/mach/irq.h>
  34. #include <asm/hardware/gic.h>
  35. static DEFINE_SPINLOCK(irq_controller_lock);
  36. /* Address of GIC 0 CPU interface */
  37. void __iomem *gic_cpu_base_addr __read_mostly;
  38. /*
  39. * Supported arch specific GIC irq extension.
  40. * Default make them NULL.
  41. */
  42. struct irq_chip gic_arch_extn = {
  43. .irq_eoi = NULL,
  44. .irq_mask = NULL,
  45. .irq_unmask = NULL,
  46. .irq_retrigger = NULL,
  47. .irq_set_type = NULL,
  48. .irq_set_wake = NULL,
  49. };
  50. #ifndef MAX_GIC_NR
  51. #define MAX_GIC_NR 1
  52. #endif
  53. static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
  54. static inline void __iomem *gic_dist_base(struct irq_data *d)
  55. {
  56. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  57. return gic_data->dist_base;
  58. }
  59. static inline void __iomem *gic_cpu_base(struct irq_data *d)
  60. {
  61. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  62. return gic_data->cpu_base;
  63. }
  64. static inline unsigned int gic_irq(struct irq_data *d)
  65. {
  66. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  67. return d->irq - gic_data->irq_offset;
  68. }
  69. /*
  70. * Routines to acknowledge, disable and enable interrupts
  71. */
  72. static void gic_mask_irq(struct irq_data *d)
  73. {
  74. u32 mask = 1 << (d->irq % 32);
  75. spin_lock(&irq_controller_lock);
  76. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_CLEAR + (gic_irq(d) / 32) * 4);
  77. if (gic_arch_extn.irq_mask)
  78. gic_arch_extn.irq_mask(d);
  79. spin_unlock(&irq_controller_lock);
  80. }
  81. static void gic_unmask_irq(struct irq_data *d)
  82. {
  83. u32 mask = 1 << (d->irq % 32);
  84. spin_lock(&irq_controller_lock);
  85. if (gic_arch_extn.irq_unmask)
  86. gic_arch_extn.irq_unmask(d);
  87. writel_relaxed(mask, gic_dist_base(d) + GIC_DIST_ENABLE_SET + (gic_irq(d) / 32) * 4);
  88. spin_unlock(&irq_controller_lock);
  89. }
  90. static void gic_eoi_irq(struct irq_data *d)
  91. {
  92. if (gic_arch_extn.irq_eoi) {
  93. spin_lock(&irq_controller_lock);
  94. gic_arch_extn.irq_eoi(d);
  95. spin_unlock(&irq_controller_lock);
  96. }
  97. writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
  98. }
  99. static int gic_set_type(struct irq_data *d, unsigned int type)
  100. {
  101. void __iomem *base = gic_dist_base(d);
  102. unsigned int gicirq = gic_irq(d);
  103. u32 enablemask = 1 << (gicirq % 32);
  104. u32 enableoff = (gicirq / 32) * 4;
  105. u32 confmask = 0x2 << ((gicirq % 16) * 2);
  106. u32 confoff = (gicirq / 16) * 4;
  107. bool enabled = false;
  108. u32 val;
  109. /* Interrupt configuration for SGIs can't be changed */
  110. if (gicirq < 16)
  111. return -EINVAL;
  112. if (type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  113. return -EINVAL;
  114. spin_lock(&irq_controller_lock);
  115. if (gic_arch_extn.irq_set_type)
  116. gic_arch_extn.irq_set_type(d, type);
  117. val = readl_relaxed(base + GIC_DIST_CONFIG + confoff);
  118. if (type == IRQ_TYPE_LEVEL_HIGH)
  119. val &= ~confmask;
  120. else if (type == IRQ_TYPE_EDGE_RISING)
  121. val |= confmask;
  122. /*
  123. * As recommended by the spec, disable the interrupt before changing
  124. * the configuration
  125. */
  126. if (readl_relaxed(base + GIC_DIST_ENABLE_SET + enableoff) & enablemask) {
  127. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_CLEAR + enableoff);
  128. enabled = true;
  129. }
  130. writel_relaxed(val, base + GIC_DIST_CONFIG + confoff);
  131. if (enabled)
  132. writel_relaxed(enablemask, base + GIC_DIST_ENABLE_SET + enableoff);
  133. spin_unlock(&irq_controller_lock);
  134. return 0;
  135. }
  136. static int gic_retrigger(struct irq_data *d)
  137. {
  138. if (gic_arch_extn.irq_retrigger)
  139. return gic_arch_extn.irq_retrigger(d);
  140. return -ENXIO;
  141. }
  142. #ifdef CONFIG_SMP
  143. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  144. bool force)
  145. {
  146. void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
  147. unsigned int shift = (d->irq % 4) * 8;
  148. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  149. u32 val, mask, bit;
  150. if (cpu >= 8 || cpu >= nr_cpu_ids)
  151. return -EINVAL;
  152. mask = 0xff << shift;
  153. bit = 1 << (cpu + shift);
  154. spin_lock(&irq_controller_lock);
  155. val = readl_relaxed(reg) & ~mask;
  156. writel_relaxed(val | bit, reg);
  157. spin_unlock(&irq_controller_lock);
  158. return IRQ_SET_MASK_OK;
  159. }
  160. #endif
  161. #ifdef CONFIG_PM
  162. static int gic_set_wake(struct irq_data *d, unsigned int on)
  163. {
  164. int ret = -ENXIO;
  165. if (gic_arch_extn.irq_set_wake)
  166. ret = gic_arch_extn.irq_set_wake(d, on);
  167. return ret;
  168. }
  169. #else
  170. #define gic_set_wake NULL
  171. #endif
  172. static void gic_handle_cascade_irq(unsigned int irq, struct irq_desc *desc)
  173. {
  174. struct gic_chip_data *chip_data = irq_get_handler_data(irq);
  175. struct irq_chip *chip = irq_get_chip(irq);
  176. unsigned int cascade_irq, gic_irq;
  177. unsigned long status;
  178. chained_irq_enter(chip, desc);
  179. spin_lock(&irq_controller_lock);
  180. status = readl_relaxed(chip_data->cpu_base + GIC_CPU_INTACK);
  181. spin_unlock(&irq_controller_lock);
  182. gic_irq = (status & 0x3ff);
  183. if (gic_irq == 1023)
  184. goto out;
  185. cascade_irq = gic_irq + chip_data->irq_offset;
  186. if (unlikely(gic_irq < 32 || gic_irq > 1020 || cascade_irq >= NR_IRQS))
  187. do_bad_IRQ(cascade_irq, desc);
  188. else
  189. generic_handle_irq(cascade_irq);
  190. out:
  191. chained_irq_exit(chip, desc);
  192. }
  193. static struct irq_chip gic_chip = {
  194. .name = "GIC",
  195. .irq_mask = gic_mask_irq,
  196. .irq_unmask = gic_unmask_irq,
  197. .irq_eoi = gic_eoi_irq,
  198. .irq_set_type = gic_set_type,
  199. .irq_retrigger = gic_retrigger,
  200. #ifdef CONFIG_SMP
  201. .irq_set_affinity = gic_set_affinity,
  202. #endif
  203. .irq_set_wake = gic_set_wake,
  204. };
  205. void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  206. {
  207. if (gic_nr >= MAX_GIC_NR)
  208. BUG();
  209. if (irq_set_handler_data(irq, &gic_data[gic_nr]) != 0)
  210. BUG();
  211. irq_set_chained_handler(irq, gic_handle_cascade_irq);
  212. }
  213. static void __init gic_dist_init(struct gic_chip_data *gic,
  214. unsigned int irq_start)
  215. {
  216. unsigned int gic_irqs, irq_limit, i;
  217. void __iomem *base = gic->dist_base;
  218. u32 cpumask = 1 << smp_processor_id();
  219. cpumask |= cpumask << 8;
  220. cpumask |= cpumask << 16;
  221. writel_relaxed(0, base + GIC_DIST_CTRL);
  222. /*
  223. * Find out how many interrupts are supported.
  224. * The GIC only supports up to 1020 interrupt sources.
  225. */
  226. gic_irqs = readl_relaxed(base + GIC_DIST_CTR) & 0x1f;
  227. gic_irqs = (gic_irqs + 1) * 32;
  228. if (gic_irqs > 1020)
  229. gic_irqs = 1020;
  230. gic->gic_irqs = gic_irqs;
  231. /*
  232. * Set all global interrupts to be level triggered, active low.
  233. */
  234. for (i = 32; i < gic_irqs; i += 16)
  235. writel_relaxed(0, base + GIC_DIST_CONFIG + i * 4 / 16);
  236. /*
  237. * Set all global interrupts to this CPU only.
  238. */
  239. for (i = 32; i < gic_irqs; i += 4)
  240. writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  241. /*
  242. * Set priority on all global interrupts.
  243. */
  244. for (i = 32; i < gic_irqs; i += 4)
  245. writel_relaxed(0xa0a0a0a0, base + GIC_DIST_PRI + i * 4 / 4);
  246. /*
  247. * Disable all interrupts. Leave the PPI and SGIs alone
  248. * as these enables are banked registers.
  249. */
  250. for (i = 32; i < gic_irqs; i += 32)
  251. writel_relaxed(0xffffffff, base + GIC_DIST_ENABLE_CLEAR + i * 4 / 32);
  252. /*
  253. * Limit number of interrupts registered to the platform maximum
  254. */
  255. irq_limit = gic->irq_offset + gic_irqs;
  256. if (WARN_ON(irq_limit > NR_IRQS))
  257. irq_limit = NR_IRQS;
  258. /*
  259. * Setup the Linux IRQ subsystem.
  260. */
  261. for (i = irq_start; i < irq_limit; i++) {
  262. irq_set_chip_and_handler(i, &gic_chip, handle_fasteoi_irq);
  263. irq_set_chip_data(i, gic);
  264. set_irq_flags(i, IRQF_VALID | IRQF_PROBE);
  265. }
  266. writel_relaxed(1, base + GIC_DIST_CTRL);
  267. }
  268. static void __cpuinit gic_cpu_init(struct gic_chip_data *gic)
  269. {
  270. void __iomem *dist_base = gic->dist_base;
  271. void __iomem *base = gic->cpu_base;
  272. int i;
  273. /*
  274. * Deal with the banked PPI and SGI interrupts - disable all
  275. * PPI interrupts, ensure all SGI interrupts are enabled.
  276. */
  277. writel_relaxed(0xffff0000, dist_base + GIC_DIST_ENABLE_CLEAR);
  278. writel_relaxed(0x0000ffff, dist_base + GIC_DIST_ENABLE_SET);
  279. /*
  280. * Set priority on PPI and SGI interrupts
  281. */
  282. for (i = 0; i < 32; i += 4)
  283. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4 / 4);
  284. writel_relaxed(0xf0, base + GIC_CPU_PRIMASK);
  285. writel_relaxed(1, base + GIC_CPU_CTRL);
  286. }
  287. #ifdef CONFIG_CPU_PM
  288. /*
  289. * Saves the GIC distributor registers during suspend or idle. Must be called
  290. * with interrupts disabled but before powering down the GIC. After calling
  291. * this function, no interrupts will be delivered by the GIC, and another
  292. * platform-specific wakeup source must be enabled.
  293. */
  294. static void gic_dist_save(unsigned int gic_nr)
  295. {
  296. unsigned int gic_irqs;
  297. void __iomem *dist_base;
  298. int i;
  299. if (gic_nr >= MAX_GIC_NR)
  300. BUG();
  301. gic_irqs = gic_data[gic_nr].gic_irqs;
  302. dist_base = gic_data[gic_nr].dist_base;
  303. if (!dist_base)
  304. return;
  305. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  306. gic_data[gic_nr].saved_spi_conf[i] =
  307. readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  308. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  309. gic_data[gic_nr].saved_spi_target[i] =
  310. readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  311. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  312. gic_data[gic_nr].saved_spi_enable[i] =
  313. readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  314. }
  315. /*
  316. * Restores the GIC distributor registers during resume or when coming out of
  317. * idle. Must be called before enabling interrupts. If a level interrupt
  318. * that occured while the GIC was suspended is still present, it will be
  319. * handled normally, but any edge interrupts that occured will not be seen by
  320. * the GIC and need to be handled by the platform-specific wakeup source.
  321. */
  322. static void gic_dist_restore(unsigned int gic_nr)
  323. {
  324. unsigned int gic_irqs;
  325. unsigned int i;
  326. void __iomem *dist_base;
  327. if (gic_nr >= MAX_GIC_NR)
  328. BUG();
  329. gic_irqs = gic_data[gic_nr].gic_irqs;
  330. dist_base = gic_data[gic_nr].dist_base;
  331. if (!dist_base)
  332. return;
  333. writel_relaxed(0, dist_base + GIC_DIST_CTRL);
  334. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  335. writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
  336. dist_base + GIC_DIST_CONFIG + i * 4);
  337. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  338. writel_relaxed(0xa0a0a0a0,
  339. dist_base + GIC_DIST_PRI + i * 4);
  340. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  341. writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
  342. dist_base + GIC_DIST_TARGET + i * 4);
  343. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  344. writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
  345. dist_base + GIC_DIST_ENABLE_SET + i * 4);
  346. writel_relaxed(1, dist_base + GIC_DIST_CTRL);
  347. }
  348. static void gic_cpu_save(unsigned int gic_nr)
  349. {
  350. int i;
  351. u32 *ptr;
  352. void __iomem *dist_base;
  353. void __iomem *cpu_base;
  354. if (gic_nr >= MAX_GIC_NR)
  355. BUG();
  356. dist_base = gic_data[gic_nr].dist_base;
  357. cpu_base = gic_data[gic_nr].cpu_base;
  358. if (!dist_base || !cpu_base)
  359. return;
  360. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  361. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  362. ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  363. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  364. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  365. ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  366. }
  367. static void gic_cpu_restore(unsigned int gic_nr)
  368. {
  369. int i;
  370. u32 *ptr;
  371. void __iomem *dist_base;
  372. void __iomem *cpu_base;
  373. if (gic_nr >= MAX_GIC_NR)
  374. BUG();
  375. dist_base = gic_data[gic_nr].dist_base;
  376. cpu_base = gic_data[gic_nr].cpu_base;
  377. if (!dist_base || !cpu_base)
  378. return;
  379. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  380. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  381. writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
  382. ptr = __this_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  383. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  384. writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
  385. for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
  386. writel_relaxed(0xa0a0a0a0, dist_base + GIC_DIST_PRI + i * 4);
  387. writel_relaxed(0xf0, cpu_base + GIC_CPU_PRIMASK);
  388. writel_relaxed(1, cpu_base + GIC_CPU_CTRL);
  389. }
  390. static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  391. {
  392. int i;
  393. for (i = 0; i < MAX_GIC_NR; i++) {
  394. switch (cmd) {
  395. case CPU_PM_ENTER:
  396. gic_cpu_save(i);
  397. break;
  398. case CPU_PM_ENTER_FAILED:
  399. case CPU_PM_EXIT:
  400. gic_cpu_restore(i);
  401. break;
  402. case CPU_CLUSTER_PM_ENTER:
  403. gic_dist_save(i);
  404. break;
  405. case CPU_CLUSTER_PM_ENTER_FAILED:
  406. case CPU_CLUSTER_PM_EXIT:
  407. gic_dist_restore(i);
  408. break;
  409. }
  410. }
  411. return NOTIFY_OK;
  412. }
  413. static struct notifier_block gic_notifier_block = {
  414. .notifier_call = gic_notifier,
  415. };
  416. static void __init gic_pm_init(struct gic_chip_data *gic)
  417. {
  418. gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
  419. sizeof(u32));
  420. BUG_ON(!gic->saved_ppi_enable);
  421. gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
  422. sizeof(u32));
  423. BUG_ON(!gic->saved_ppi_conf);
  424. cpu_pm_register_notifier(&gic_notifier_block);
  425. }
  426. #else
  427. static void __init gic_pm_init(struct gic_chip_data *gic)
  428. {
  429. }
  430. #endif
  431. void __init gic_init(unsigned int gic_nr, unsigned int irq_start,
  432. void __iomem *dist_base, void __iomem *cpu_base)
  433. {
  434. struct gic_chip_data *gic;
  435. BUG_ON(gic_nr >= MAX_GIC_NR);
  436. gic = &gic_data[gic_nr];
  437. gic->dist_base = dist_base;
  438. gic->cpu_base = cpu_base;
  439. gic->irq_offset = (irq_start - 1) & ~31;
  440. if (gic_nr == 0)
  441. gic_cpu_base_addr = cpu_base;
  442. gic_chip.flags |= gic_arch_extn.flags;
  443. gic_dist_init(gic, irq_start);
  444. gic_cpu_init(gic);
  445. gic_pm_init(gic);
  446. }
  447. void __cpuinit gic_secondary_init(unsigned int gic_nr)
  448. {
  449. BUG_ON(gic_nr >= MAX_GIC_NR);
  450. gic_cpu_init(&gic_data[gic_nr]);
  451. }
  452. void __cpuinit gic_enable_ppi(unsigned int irq)
  453. {
  454. unsigned long flags;
  455. local_irq_save(flags);
  456. irq_set_status_flags(irq, IRQ_NOPROBE);
  457. gic_unmask_irq(irq_get_irq_data(irq));
  458. local_irq_restore(flags);
  459. }
  460. #ifdef CONFIG_SMP
  461. void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  462. {
  463. unsigned long map = *cpus_addr(*mask);
  464. /*
  465. * Ensure that stores to Normal memory are visible to the
  466. * other CPUs before issuing the IPI.
  467. */
  468. dsb();
  469. /* this always happens on GIC0 */
  470. writel_relaxed(map << 16 | irq, gic_data[0].dist_base + GIC_DIST_SOFTINT);
  471. }
  472. #endif