irq-gic.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  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/acpi.h>
  37. #include <linux/irqdomain.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/percpu.h>
  40. #include <linux/slab.h>
  41. #include <linux/irqchip.h>
  42. #include <linux/irqchip/chained_irq.h>
  43. #include <linux/irqchip/arm-gic.h>
  44. #include <asm/cputype.h>
  45. #include <asm/irq.h>
  46. #include <asm/exception.h>
  47. #include <asm/smp_plat.h>
  48. #include <asm/virt.h>
  49. #include "irq-gic-common.h"
  50. #ifdef CONFIG_ARM64
  51. #include <asm/cpufeature.h>
  52. static void gic_check_cpu_features(void)
  53. {
  54. WARN_TAINT_ONCE(cpus_have_cap(ARM64_HAS_SYSREG_GIC_CPUIF),
  55. TAINT_CPU_OUT_OF_SPEC,
  56. "GICv3 system registers enabled, broken firmware!\n");
  57. }
  58. #else
  59. #define gic_check_cpu_features() do { } while(0)
  60. #endif
  61. union gic_base {
  62. void __iomem *common_base;
  63. void __percpu * __iomem *percpu_base;
  64. };
  65. struct gic_chip_data {
  66. union gic_base dist_base;
  67. union gic_base cpu_base;
  68. #ifdef CONFIG_CPU_PM
  69. u32 saved_spi_enable[DIV_ROUND_UP(1020, 32)];
  70. u32 saved_spi_active[DIV_ROUND_UP(1020, 32)];
  71. u32 saved_spi_conf[DIV_ROUND_UP(1020, 16)];
  72. u32 saved_spi_target[DIV_ROUND_UP(1020, 4)];
  73. u32 __percpu *saved_ppi_enable;
  74. u32 __percpu *saved_ppi_active;
  75. u32 __percpu *saved_ppi_conf;
  76. #endif
  77. struct irq_domain *domain;
  78. unsigned int gic_irqs;
  79. #ifdef CONFIG_GIC_NON_BANKED
  80. void __iomem *(*get_base)(union gic_base *);
  81. #endif
  82. };
  83. static DEFINE_RAW_SPINLOCK(irq_controller_lock);
  84. /*
  85. * The GIC mapping of CPU interfaces does not necessarily match
  86. * the logical CPU numbering. Let's use a mapping as returned
  87. * by the GIC itself.
  88. */
  89. #define NR_GIC_CPU_IF 8
  90. static u8 gic_cpu_map[NR_GIC_CPU_IF] __read_mostly;
  91. static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
  92. #ifndef MAX_GIC_NR
  93. #define MAX_GIC_NR 1
  94. #endif
  95. static struct gic_chip_data gic_data[MAX_GIC_NR] __read_mostly;
  96. #ifdef CONFIG_GIC_NON_BANKED
  97. static void __iomem *gic_get_percpu_base(union gic_base *base)
  98. {
  99. return raw_cpu_read(*base->percpu_base);
  100. }
  101. static void __iomem *gic_get_common_base(union gic_base *base)
  102. {
  103. return base->common_base;
  104. }
  105. static inline void __iomem *gic_data_dist_base(struct gic_chip_data *data)
  106. {
  107. return data->get_base(&data->dist_base);
  108. }
  109. static inline void __iomem *gic_data_cpu_base(struct gic_chip_data *data)
  110. {
  111. return data->get_base(&data->cpu_base);
  112. }
  113. static inline void gic_set_base_accessor(struct gic_chip_data *data,
  114. void __iomem *(*f)(union gic_base *))
  115. {
  116. data->get_base = f;
  117. }
  118. #else
  119. #define gic_data_dist_base(d) ((d)->dist_base.common_base)
  120. #define gic_data_cpu_base(d) ((d)->cpu_base.common_base)
  121. #define gic_set_base_accessor(d, f)
  122. #endif
  123. static inline void __iomem *gic_dist_base(struct irq_data *d)
  124. {
  125. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  126. return gic_data_dist_base(gic_data);
  127. }
  128. static inline void __iomem *gic_cpu_base(struct irq_data *d)
  129. {
  130. struct gic_chip_data *gic_data = irq_data_get_irq_chip_data(d);
  131. return gic_data_cpu_base(gic_data);
  132. }
  133. static inline unsigned int gic_irq(struct irq_data *d)
  134. {
  135. return d->hwirq;
  136. }
  137. static inline bool cascading_gic_irq(struct irq_data *d)
  138. {
  139. void *data = irq_data_get_irq_handler_data(d);
  140. /*
  141. * If handler_data is set, this is a cascading interrupt, and
  142. * it cannot possibly be forwarded.
  143. */
  144. return data != NULL;
  145. }
  146. /*
  147. * Routines to acknowledge, disable and enable interrupts
  148. */
  149. static void gic_poke_irq(struct irq_data *d, u32 offset)
  150. {
  151. u32 mask = 1 << (gic_irq(d) % 32);
  152. writel_relaxed(mask, gic_dist_base(d) + offset + (gic_irq(d) / 32) * 4);
  153. }
  154. static int gic_peek_irq(struct irq_data *d, u32 offset)
  155. {
  156. u32 mask = 1 << (gic_irq(d) % 32);
  157. return !!(readl_relaxed(gic_dist_base(d) + offset + (gic_irq(d) / 32) * 4) & mask);
  158. }
  159. static void gic_mask_irq(struct irq_data *d)
  160. {
  161. gic_poke_irq(d, GIC_DIST_ENABLE_CLEAR);
  162. }
  163. static void gic_eoimode1_mask_irq(struct irq_data *d)
  164. {
  165. gic_mask_irq(d);
  166. /*
  167. * When masking a forwarded interrupt, make sure it is
  168. * deactivated as well.
  169. *
  170. * This ensures that an interrupt that is getting
  171. * disabled/masked will not get "stuck", because there is
  172. * noone to deactivate it (guest is being terminated).
  173. */
  174. if (irqd_is_forwarded_to_vcpu(d))
  175. gic_poke_irq(d, GIC_DIST_ACTIVE_CLEAR);
  176. }
  177. static void gic_unmask_irq(struct irq_data *d)
  178. {
  179. gic_poke_irq(d, GIC_DIST_ENABLE_SET);
  180. }
  181. static void gic_eoi_irq(struct irq_data *d)
  182. {
  183. writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_EOI);
  184. }
  185. static void gic_eoimode1_eoi_irq(struct irq_data *d)
  186. {
  187. /* Do not deactivate an IRQ forwarded to a vcpu. */
  188. if (irqd_is_forwarded_to_vcpu(d))
  189. return;
  190. writel_relaxed(gic_irq(d), gic_cpu_base(d) + GIC_CPU_DEACTIVATE);
  191. }
  192. static int gic_irq_set_irqchip_state(struct irq_data *d,
  193. enum irqchip_irq_state which, bool val)
  194. {
  195. u32 reg;
  196. switch (which) {
  197. case IRQCHIP_STATE_PENDING:
  198. reg = val ? GIC_DIST_PENDING_SET : GIC_DIST_PENDING_CLEAR;
  199. break;
  200. case IRQCHIP_STATE_ACTIVE:
  201. reg = val ? GIC_DIST_ACTIVE_SET : GIC_DIST_ACTIVE_CLEAR;
  202. break;
  203. case IRQCHIP_STATE_MASKED:
  204. reg = val ? GIC_DIST_ENABLE_CLEAR : GIC_DIST_ENABLE_SET;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. gic_poke_irq(d, reg);
  210. return 0;
  211. }
  212. static int gic_irq_get_irqchip_state(struct irq_data *d,
  213. enum irqchip_irq_state which, bool *val)
  214. {
  215. switch (which) {
  216. case IRQCHIP_STATE_PENDING:
  217. *val = gic_peek_irq(d, GIC_DIST_PENDING_SET);
  218. break;
  219. case IRQCHIP_STATE_ACTIVE:
  220. *val = gic_peek_irq(d, GIC_DIST_ACTIVE_SET);
  221. break;
  222. case IRQCHIP_STATE_MASKED:
  223. *val = !gic_peek_irq(d, GIC_DIST_ENABLE_SET);
  224. break;
  225. default:
  226. return -EINVAL;
  227. }
  228. return 0;
  229. }
  230. static int gic_set_type(struct irq_data *d, unsigned int type)
  231. {
  232. void __iomem *base = gic_dist_base(d);
  233. unsigned int gicirq = gic_irq(d);
  234. /* Interrupt configuration for SGIs can't be changed */
  235. if (gicirq < 16)
  236. return -EINVAL;
  237. /* SPIs have restrictions on the supported types */
  238. if (gicirq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
  239. type != IRQ_TYPE_EDGE_RISING)
  240. return -EINVAL;
  241. return gic_configure_irq(gicirq, type, base, NULL);
  242. }
  243. static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  244. {
  245. /* Only interrupts on the primary GIC can be forwarded to a vcpu. */
  246. if (cascading_gic_irq(d))
  247. return -EINVAL;
  248. if (vcpu)
  249. irqd_set_forwarded_to_vcpu(d);
  250. else
  251. irqd_clr_forwarded_to_vcpu(d);
  252. return 0;
  253. }
  254. #ifdef CONFIG_SMP
  255. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  256. bool force)
  257. {
  258. void __iomem *reg = gic_dist_base(d) + GIC_DIST_TARGET + (gic_irq(d) & ~3);
  259. unsigned int cpu, shift = (gic_irq(d) % 4) * 8;
  260. u32 val, mask, bit;
  261. unsigned long flags;
  262. if (!force)
  263. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  264. else
  265. cpu = cpumask_first(mask_val);
  266. if (cpu >= NR_GIC_CPU_IF || cpu >= nr_cpu_ids)
  267. return -EINVAL;
  268. raw_spin_lock_irqsave(&irq_controller_lock, flags);
  269. mask = 0xff << shift;
  270. bit = gic_cpu_map[cpu] << shift;
  271. val = readl_relaxed(reg) & ~mask;
  272. writel_relaxed(val | bit, reg);
  273. raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
  274. return IRQ_SET_MASK_OK;
  275. }
  276. #endif
  277. static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  278. {
  279. u32 irqstat, irqnr;
  280. struct gic_chip_data *gic = &gic_data[0];
  281. void __iomem *cpu_base = gic_data_cpu_base(gic);
  282. do {
  283. irqstat = readl_relaxed(cpu_base + GIC_CPU_INTACK);
  284. irqnr = irqstat & GICC_IAR_INT_ID_MASK;
  285. if (likely(irqnr > 15 && irqnr < 1020)) {
  286. if (static_key_true(&supports_deactivate))
  287. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  288. handle_domain_irq(gic->domain, irqnr, regs);
  289. continue;
  290. }
  291. if (irqnr < 16) {
  292. writel_relaxed(irqstat, cpu_base + GIC_CPU_EOI);
  293. if (static_key_true(&supports_deactivate))
  294. writel_relaxed(irqstat, cpu_base + GIC_CPU_DEACTIVATE);
  295. #ifdef CONFIG_SMP
  296. /*
  297. * Ensure any shared data written by the CPU sending
  298. * the IPI is read after we've read the ACK register
  299. * on the GIC.
  300. *
  301. * Pairs with the write barrier in gic_raise_softirq
  302. */
  303. smp_rmb();
  304. handle_IPI(irqnr, regs);
  305. #endif
  306. continue;
  307. }
  308. break;
  309. } while (1);
  310. }
  311. static void gic_handle_cascade_irq(struct irq_desc *desc)
  312. {
  313. struct gic_chip_data *chip_data = irq_desc_get_handler_data(desc);
  314. struct irq_chip *chip = irq_desc_get_chip(desc);
  315. unsigned int cascade_irq, gic_irq;
  316. unsigned long status;
  317. chained_irq_enter(chip, desc);
  318. raw_spin_lock(&irq_controller_lock);
  319. status = readl_relaxed(gic_data_cpu_base(chip_data) + GIC_CPU_INTACK);
  320. raw_spin_unlock(&irq_controller_lock);
  321. gic_irq = (status & GICC_IAR_INT_ID_MASK);
  322. if (gic_irq == GICC_INT_SPURIOUS)
  323. goto out;
  324. cascade_irq = irq_find_mapping(chip_data->domain, gic_irq);
  325. if (unlikely(gic_irq < 32 || gic_irq > 1020))
  326. handle_bad_irq(desc);
  327. else
  328. generic_handle_irq(cascade_irq);
  329. out:
  330. chained_irq_exit(chip, desc);
  331. }
  332. static struct irq_chip gic_chip = {
  333. .name = "GIC",
  334. .irq_mask = gic_mask_irq,
  335. .irq_unmask = gic_unmask_irq,
  336. .irq_eoi = gic_eoi_irq,
  337. .irq_set_type = gic_set_type,
  338. #ifdef CONFIG_SMP
  339. .irq_set_affinity = gic_set_affinity,
  340. #endif
  341. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  342. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  343. .flags = IRQCHIP_SET_TYPE_MASKED |
  344. IRQCHIP_SKIP_SET_WAKE |
  345. IRQCHIP_MASK_ON_SUSPEND,
  346. };
  347. static struct irq_chip gic_eoimode1_chip = {
  348. .name = "GICv2",
  349. .irq_mask = gic_eoimode1_mask_irq,
  350. .irq_unmask = gic_unmask_irq,
  351. .irq_eoi = gic_eoimode1_eoi_irq,
  352. .irq_set_type = gic_set_type,
  353. #ifdef CONFIG_SMP
  354. .irq_set_affinity = gic_set_affinity,
  355. #endif
  356. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  357. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  358. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  359. .flags = IRQCHIP_SET_TYPE_MASKED |
  360. IRQCHIP_SKIP_SET_WAKE |
  361. IRQCHIP_MASK_ON_SUSPEND,
  362. };
  363. void __init gic_cascade_irq(unsigned int gic_nr, unsigned int irq)
  364. {
  365. if (gic_nr >= MAX_GIC_NR)
  366. BUG();
  367. irq_set_chained_handler_and_data(irq, gic_handle_cascade_irq,
  368. &gic_data[gic_nr]);
  369. }
  370. static u8 gic_get_cpumask(struct gic_chip_data *gic)
  371. {
  372. void __iomem *base = gic_data_dist_base(gic);
  373. u32 mask, i;
  374. for (i = mask = 0; i < 32; i += 4) {
  375. mask = readl_relaxed(base + GIC_DIST_TARGET + i);
  376. mask |= mask >> 16;
  377. mask |= mask >> 8;
  378. if (mask)
  379. break;
  380. }
  381. if (!mask && num_possible_cpus() > 1)
  382. pr_crit("GIC CPU mask not found - kernel will fail to boot.\n");
  383. return mask;
  384. }
  385. static void gic_cpu_if_up(struct gic_chip_data *gic)
  386. {
  387. void __iomem *cpu_base = gic_data_cpu_base(gic);
  388. u32 bypass = 0;
  389. u32 mode = 0;
  390. if (static_key_true(&supports_deactivate))
  391. mode = GIC_CPU_CTRL_EOImodeNS;
  392. /*
  393. * Preserve bypass disable bits to be written back later
  394. */
  395. bypass = readl(cpu_base + GIC_CPU_CTRL);
  396. bypass &= GICC_DIS_BYPASS_MASK;
  397. writel_relaxed(bypass | mode | GICC_ENABLE, cpu_base + GIC_CPU_CTRL);
  398. }
  399. static void __init gic_dist_init(struct gic_chip_data *gic)
  400. {
  401. unsigned int i;
  402. u32 cpumask;
  403. unsigned int gic_irqs = gic->gic_irqs;
  404. void __iomem *base = gic_data_dist_base(gic);
  405. writel_relaxed(GICD_DISABLE, base + GIC_DIST_CTRL);
  406. /*
  407. * Set all global interrupts to this CPU only.
  408. */
  409. cpumask = gic_get_cpumask(gic);
  410. cpumask |= cpumask << 8;
  411. cpumask |= cpumask << 16;
  412. for (i = 32; i < gic_irqs; i += 4)
  413. writel_relaxed(cpumask, base + GIC_DIST_TARGET + i * 4 / 4);
  414. gic_dist_config(base, gic_irqs, NULL);
  415. writel_relaxed(GICD_ENABLE, base + GIC_DIST_CTRL);
  416. }
  417. static void gic_cpu_init(struct gic_chip_data *gic)
  418. {
  419. void __iomem *dist_base = gic_data_dist_base(gic);
  420. void __iomem *base = gic_data_cpu_base(gic);
  421. unsigned int cpu_mask, cpu = smp_processor_id();
  422. int i;
  423. /*
  424. * Setting up the CPU map is only relevant for the primary GIC
  425. * because any nested/secondary GICs do not directly interface
  426. * with the CPU(s).
  427. */
  428. if (gic == &gic_data[0]) {
  429. /*
  430. * Get what the GIC says our CPU mask is.
  431. */
  432. BUG_ON(cpu >= NR_GIC_CPU_IF);
  433. cpu_mask = gic_get_cpumask(gic);
  434. gic_cpu_map[cpu] = cpu_mask;
  435. /*
  436. * Clear our mask from the other map entries in case they're
  437. * still undefined.
  438. */
  439. for (i = 0; i < NR_GIC_CPU_IF; i++)
  440. if (i != cpu)
  441. gic_cpu_map[i] &= ~cpu_mask;
  442. }
  443. gic_cpu_config(dist_base, NULL);
  444. writel_relaxed(GICC_INT_PRI_THRESHOLD, base + GIC_CPU_PRIMASK);
  445. gic_cpu_if_up(gic);
  446. }
  447. int gic_cpu_if_down(unsigned int gic_nr)
  448. {
  449. void __iomem *cpu_base;
  450. u32 val = 0;
  451. if (gic_nr >= MAX_GIC_NR)
  452. return -EINVAL;
  453. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  454. val = readl(cpu_base + GIC_CPU_CTRL);
  455. val &= ~GICC_ENABLE;
  456. writel_relaxed(val, cpu_base + GIC_CPU_CTRL);
  457. return 0;
  458. }
  459. #ifdef CONFIG_CPU_PM
  460. /*
  461. * Saves the GIC distributor registers during suspend or idle. Must be called
  462. * with interrupts disabled but before powering down the GIC. After calling
  463. * this function, no interrupts will be delivered by the GIC, and another
  464. * platform-specific wakeup source must be enabled.
  465. */
  466. static void gic_dist_save(unsigned int gic_nr)
  467. {
  468. unsigned int gic_irqs;
  469. void __iomem *dist_base;
  470. int i;
  471. if (gic_nr >= MAX_GIC_NR)
  472. BUG();
  473. gic_irqs = gic_data[gic_nr].gic_irqs;
  474. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  475. if (!dist_base)
  476. return;
  477. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  478. gic_data[gic_nr].saved_spi_conf[i] =
  479. readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  480. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  481. gic_data[gic_nr].saved_spi_target[i] =
  482. readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  483. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  484. gic_data[gic_nr].saved_spi_enable[i] =
  485. readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  486. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++)
  487. gic_data[gic_nr].saved_spi_active[i] =
  488. readl_relaxed(dist_base + GIC_DIST_ACTIVE_SET + i * 4);
  489. }
  490. /*
  491. * Restores the GIC distributor registers during resume or when coming out of
  492. * idle. Must be called before enabling interrupts. If a level interrupt
  493. * that occured while the GIC was suspended is still present, it will be
  494. * handled normally, but any edge interrupts that occured will not be seen by
  495. * the GIC and need to be handled by the platform-specific wakeup source.
  496. */
  497. static void gic_dist_restore(unsigned int gic_nr)
  498. {
  499. unsigned int gic_irqs;
  500. unsigned int i;
  501. void __iomem *dist_base;
  502. if (gic_nr >= MAX_GIC_NR)
  503. BUG();
  504. gic_irqs = gic_data[gic_nr].gic_irqs;
  505. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  506. if (!dist_base)
  507. return;
  508. writel_relaxed(GICD_DISABLE, dist_base + GIC_DIST_CTRL);
  509. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 16); i++)
  510. writel_relaxed(gic_data[gic_nr].saved_spi_conf[i],
  511. dist_base + GIC_DIST_CONFIG + i * 4);
  512. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  513. writel_relaxed(GICD_INT_DEF_PRI_X4,
  514. dist_base + GIC_DIST_PRI + i * 4);
  515. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 4); i++)
  516. writel_relaxed(gic_data[gic_nr].saved_spi_target[i],
  517. dist_base + GIC_DIST_TARGET + i * 4);
  518. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++) {
  519. writel_relaxed(GICD_INT_EN_CLR_X32,
  520. dist_base + GIC_DIST_ENABLE_CLEAR + i * 4);
  521. writel_relaxed(gic_data[gic_nr].saved_spi_enable[i],
  522. dist_base + GIC_DIST_ENABLE_SET + i * 4);
  523. }
  524. for (i = 0; i < DIV_ROUND_UP(gic_irqs, 32); i++) {
  525. writel_relaxed(GICD_INT_EN_CLR_X32,
  526. dist_base + GIC_DIST_ACTIVE_CLEAR + i * 4);
  527. writel_relaxed(gic_data[gic_nr].saved_spi_active[i],
  528. dist_base + GIC_DIST_ACTIVE_SET + i * 4);
  529. }
  530. writel_relaxed(GICD_ENABLE, dist_base + GIC_DIST_CTRL);
  531. }
  532. static void gic_cpu_save(unsigned int gic_nr)
  533. {
  534. int i;
  535. u32 *ptr;
  536. void __iomem *dist_base;
  537. void __iomem *cpu_base;
  538. if (gic_nr >= MAX_GIC_NR)
  539. BUG();
  540. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  541. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  542. if (!dist_base || !cpu_base)
  543. return;
  544. ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  545. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  546. ptr[i] = readl_relaxed(dist_base + GIC_DIST_ENABLE_SET + i * 4);
  547. ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_active);
  548. for (i = 0; i < DIV_ROUND_UP(32, 32); i++)
  549. ptr[i] = readl_relaxed(dist_base + GIC_DIST_ACTIVE_SET + i * 4);
  550. ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  551. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  552. ptr[i] = readl_relaxed(dist_base + GIC_DIST_CONFIG + i * 4);
  553. }
  554. static void gic_cpu_restore(unsigned int gic_nr)
  555. {
  556. int i;
  557. u32 *ptr;
  558. void __iomem *dist_base;
  559. void __iomem *cpu_base;
  560. if (gic_nr >= MAX_GIC_NR)
  561. BUG();
  562. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  563. cpu_base = gic_data_cpu_base(&gic_data[gic_nr]);
  564. if (!dist_base || !cpu_base)
  565. return;
  566. ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_enable);
  567. for (i = 0; i < DIV_ROUND_UP(32, 32); i++) {
  568. writel_relaxed(GICD_INT_EN_CLR_X32,
  569. dist_base + GIC_DIST_ENABLE_CLEAR + i * 4);
  570. writel_relaxed(ptr[i], dist_base + GIC_DIST_ENABLE_SET + i * 4);
  571. }
  572. ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_active);
  573. for (i = 0; i < DIV_ROUND_UP(32, 32); i++) {
  574. writel_relaxed(GICD_INT_EN_CLR_X32,
  575. dist_base + GIC_DIST_ACTIVE_CLEAR + i * 4);
  576. writel_relaxed(ptr[i], dist_base + GIC_DIST_ACTIVE_SET + i * 4);
  577. }
  578. ptr = raw_cpu_ptr(gic_data[gic_nr].saved_ppi_conf);
  579. for (i = 0; i < DIV_ROUND_UP(32, 16); i++)
  580. writel_relaxed(ptr[i], dist_base + GIC_DIST_CONFIG + i * 4);
  581. for (i = 0; i < DIV_ROUND_UP(32, 4); i++)
  582. writel_relaxed(GICD_INT_DEF_PRI_X4,
  583. dist_base + GIC_DIST_PRI + i * 4);
  584. writel_relaxed(GICC_INT_PRI_THRESHOLD, cpu_base + GIC_CPU_PRIMASK);
  585. gic_cpu_if_up(&gic_data[gic_nr]);
  586. }
  587. static int gic_notifier(struct notifier_block *self, unsigned long cmd, void *v)
  588. {
  589. int i;
  590. for (i = 0; i < MAX_GIC_NR; i++) {
  591. #ifdef CONFIG_GIC_NON_BANKED
  592. /* Skip over unused GICs */
  593. if (!gic_data[i].get_base)
  594. continue;
  595. #endif
  596. switch (cmd) {
  597. case CPU_PM_ENTER:
  598. gic_cpu_save(i);
  599. break;
  600. case CPU_PM_ENTER_FAILED:
  601. case CPU_PM_EXIT:
  602. gic_cpu_restore(i);
  603. break;
  604. case CPU_CLUSTER_PM_ENTER:
  605. gic_dist_save(i);
  606. break;
  607. case CPU_CLUSTER_PM_ENTER_FAILED:
  608. case CPU_CLUSTER_PM_EXIT:
  609. gic_dist_restore(i);
  610. break;
  611. }
  612. }
  613. return NOTIFY_OK;
  614. }
  615. static struct notifier_block gic_notifier_block = {
  616. .notifier_call = gic_notifier,
  617. };
  618. static void __init gic_pm_init(struct gic_chip_data *gic)
  619. {
  620. gic->saved_ppi_enable = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
  621. sizeof(u32));
  622. BUG_ON(!gic->saved_ppi_enable);
  623. gic->saved_ppi_active = __alloc_percpu(DIV_ROUND_UP(32, 32) * 4,
  624. sizeof(u32));
  625. BUG_ON(!gic->saved_ppi_active);
  626. gic->saved_ppi_conf = __alloc_percpu(DIV_ROUND_UP(32, 16) * 4,
  627. sizeof(u32));
  628. BUG_ON(!gic->saved_ppi_conf);
  629. if (gic == &gic_data[0])
  630. cpu_pm_register_notifier(&gic_notifier_block);
  631. }
  632. #else
  633. static void __init gic_pm_init(struct gic_chip_data *gic)
  634. {
  635. }
  636. #endif
  637. #ifdef CONFIG_SMP
  638. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  639. {
  640. int cpu;
  641. unsigned long flags, map = 0;
  642. raw_spin_lock_irqsave(&irq_controller_lock, flags);
  643. /* Convert our logical CPU mask into a physical one. */
  644. for_each_cpu(cpu, mask)
  645. map |= gic_cpu_map[cpu];
  646. /*
  647. * Ensure that stores to Normal memory are visible to the
  648. * other CPUs before they observe us issuing the IPI.
  649. */
  650. dmb(ishst);
  651. /* this always happens on GIC0 */
  652. writel_relaxed(map << 16 | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
  653. raw_spin_unlock_irqrestore(&irq_controller_lock, flags);
  654. }
  655. #endif
  656. #ifdef CONFIG_BL_SWITCHER
  657. /*
  658. * gic_send_sgi - send a SGI directly to given CPU interface number
  659. *
  660. * cpu_id: the ID for the destination CPU interface
  661. * irq: the IPI number to send a SGI for
  662. */
  663. void gic_send_sgi(unsigned int cpu_id, unsigned int irq)
  664. {
  665. BUG_ON(cpu_id >= NR_GIC_CPU_IF);
  666. cpu_id = 1 << cpu_id;
  667. /* this always happens on GIC0 */
  668. writel_relaxed((cpu_id << 16) | irq, gic_data_dist_base(&gic_data[0]) + GIC_DIST_SOFTINT);
  669. }
  670. /*
  671. * gic_get_cpu_id - get the CPU interface ID for the specified CPU
  672. *
  673. * @cpu: the logical CPU number to get the GIC ID for.
  674. *
  675. * Return the CPU interface ID for the given logical CPU number,
  676. * or -1 if the CPU number is too large or the interface ID is
  677. * unknown (more than one bit set).
  678. */
  679. int gic_get_cpu_id(unsigned int cpu)
  680. {
  681. unsigned int cpu_bit;
  682. if (cpu >= NR_GIC_CPU_IF)
  683. return -1;
  684. cpu_bit = gic_cpu_map[cpu];
  685. if (cpu_bit & (cpu_bit - 1))
  686. return -1;
  687. return __ffs(cpu_bit);
  688. }
  689. /*
  690. * gic_migrate_target - migrate IRQs to another CPU interface
  691. *
  692. * @new_cpu_id: the CPU target ID to migrate IRQs to
  693. *
  694. * Migrate all peripheral interrupts with a target matching the current CPU
  695. * to the interface corresponding to @new_cpu_id. The CPU interface mapping
  696. * is also updated. Targets to other CPU interfaces are unchanged.
  697. * This must be called with IRQs locally disabled.
  698. */
  699. void gic_migrate_target(unsigned int new_cpu_id)
  700. {
  701. unsigned int cur_cpu_id, gic_irqs, gic_nr = 0;
  702. void __iomem *dist_base;
  703. int i, ror_val, cpu = smp_processor_id();
  704. u32 val, cur_target_mask, active_mask;
  705. if (gic_nr >= MAX_GIC_NR)
  706. BUG();
  707. dist_base = gic_data_dist_base(&gic_data[gic_nr]);
  708. if (!dist_base)
  709. return;
  710. gic_irqs = gic_data[gic_nr].gic_irqs;
  711. cur_cpu_id = __ffs(gic_cpu_map[cpu]);
  712. cur_target_mask = 0x01010101 << cur_cpu_id;
  713. ror_val = (cur_cpu_id - new_cpu_id) & 31;
  714. raw_spin_lock(&irq_controller_lock);
  715. /* Update the target interface for this logical CPU */
  716. gic_cpu_map[cpu] = 1 << new_cpu_id;
  717. /*
  718. * Find all the peripheral interrupts targetting the current
  719. * CPU interface and migrate them to the new CPU interface.
  720. * We skip DIST_TARGET 0 to 7 as they are read-only.
  721. */
  722. for (i = 8; i < DIV_ROUND_UP(gic_irqs, 4); i++) {
  723. val = readl_relaxed(dist_base + GIC_DIST_TARGET + i * 4);
  724. active_mask = val & cur_target_mask;
  725. if (active_mask) {
  726. val &= ~active_mask;
  727. val |= ror32(active_mask, ror_val);
  728. writel_relaxed(val, dist_base + GIC_DIST_TARGET + i*4);
  729. }
  730. }
  731. raw_spin_unlock(&irq_controller_lock);
  732. /*
  733. * Now let's migrate and clear any potential SGIs that might be
  734. * pending for us (cur_cpu_id). Since GIC_DIST_SGI_PENDING_SET
  735. * is a banked register, we can only forward the SGI using
  736. * GIC_DIST_SOFTINT. The original SGI source is lost but Linux
  737. * doesn't use that information anyway.
  738. *
  739. * For the same reason we do not adjust SGI source information
  740. * for previously sent SGIs by us to other CPUs either.
  741. */
  742. for (i = 0; i < 16; i += 4) {
  743. int j;
  744. val = readl_relaxed(dist_base + GIC_DIST_SGI_PENDING_SET + i);
  745. if (!val)
  746. continue;
  747. writel_relaxed(val, dist_base + GIC_DIST_SGI_PENDING_CLEAR + i);
  748. for (j = i; j < i + 4; j++) {
  749. if (val & 0xff)
  750. writel_relaxed((1 << (new_cpu_id + 16)) | j,
  751. dist_base + GIC_DIST_SOFTINT);
  752. val >>= 8;
  753. }
  754. }
  755. }
  756. /*
  757. * gic_get_sgir_physaddr - get the physical address for the SGI register
  758. *
  759. * REturn the physical address of the SGI register to be used
  760. * by some early assembly code when the kernel is not yet available.
  761. */
  762. static unsigned long gic_dist_physaddr;
  763. unsigned long gic_get_sgir_physaddr(void)
  764. {
  765. if (!gic_dist_physaddr)
  766. return 0;
  767. return gic_dist_physaddr + GIC_DIST_SOFTINT;
  768. }
  769. void __init gic_init_physaddr(struct device_node *node)
  770. {
  771. struct resource res;
  772. if (of_address_to_resource(node, 0, &res) == 0) {
  773. gic_dist_physaddr = res.start;
  774. pr_info("GIC physical location is %#lx\n", gic_dist_physaddr);
  775. }
  776. }
  777. #else
  778. #define gic_init_physaddr(node) do { } while (0)
  779. #endif
  780. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  781. irq_hw_number_t hw)
  782. {
  783. struct irq_chip *chip = &gic_chip;
  784. if (static_key_true(&supports_deactivate)) {
  785. if (d->host_data == (void *)&gic_data[0])
  786. chip = &gic_eoimode1_chip;
  787. }
  788. if (hw < 32) {
  789. irq_set_percpu_devid(irq);
  790. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  791. handle_percpu_devid_irq, NULL, NULL);
  792. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  793. } else {
  794. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  795. handle_fasteoi_irq, NULL, NULL);
  796. irq_set_probe(irq);
  797. }
  798. return 0;
  799. }
  800. static void gic_irq_domain_unmap(struct irq_domain *d, unsigned int irq)
  801. {
  802. }
  803. static int gic_irq_domain_translate(struct irq_domain *d,
  804. struct irq_fwspec *fwspec,
  805. unsigned long *hwirq,
  806. unsigned int *type)
  807. {
  808. if (is_of_node(fwspec->fwnode)) {
  809. if (fwspec->param_count < 3)
  810. return -EINVAL;
  811. /* Get the interrupt number and add 16 to skip over SGIs */
  812. *hwirq = fwspec->param[1] + 16;
  813. /*
  814. * For SPIs, we need to add 16 more to get the GIC irq
  815. * ID number
  816. */
  817. if (!fwspec->param[0])
  818. *hwirq += 16;
  819. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  820. return 0;
  821. }
  822. if (fwspec->fwnode->type == FWNODE_IRQCHIP) {
  823. if(fwspec->param_count != 2)
  824. return -EINVAL;
  825. *hwirq = fwspec->param[0];
  826. *type = fwspec->param[1];
  827. return 0;
  828. }
  829. return -EINVAL;
  830. }
  831. #ifdef CONFIG_SMP
  832. static int gic_secondary_init(struct notifier_block *nfb, unsigned long action,
  833. void *hcpu)
  834. {
  835. if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
  836. gic_cpu_init(&gic_data[0]);
  837. return NOTIFY_OK;
  838. }
  839. /*
  840. * Notifier for enabling the GIC CPU interface. Set an arbitrarily high
  841. * priority because the GIC needs to be up before the ARM generic timers.
  842. */
  843. static struct notifier_block gic_cpu_notifier = {
  844. .notifier_call = gic_secondary_init,
  845. .priority = 100,
  846. };
  847. #endif
  848. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  849. unsigned int nr_irqs, void *arg)
  850. {
  851. int i, ret;
  852. irq_hw_number_t hwirq;
  853. unsigned int type = IRQ_TYPE_NONE;
  854. struct irq_fwspec *fwspec = arg;
  855. ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  856. if (ret)
  857. return ret;
  858. for (i = 0; i < nr_irqs; i++)
  859. gic_irq_domain_map(domain, virq + i, hwirq + i);
  860. return 0;
  861. }
  862. static const struct irq_domain_ops gic_irq_domain_hierarchy_ops = {
  863. .translate = gic_irq_domain_translate,
  864. .alloc = gic_irq_domain_alloc,
  865. .free = irq_domain_free_irqs_top,
  866. };
  867. static const struct irq_domain_ops gic_irq_domain_ops = {
  868. .map = gic_irq_domain_map,
  869. .unmap = gic_irq_domain_unmap,
  870. };
  871. static void __init __gic_init_bases(unsigned int gic_nr, int irq_start,
  872. void __iomem *dist_base, void __iomem *cpu_base,
  873. u32 percpu_offset, struct fwnode_handle *handle)
  874. {
  875. irq_hw_number_t hwirq_base;
  876. struct gic_chip_data *gic;
  877. int gic_irqs, irq_base, i;
  878. BUG_ON(gic_nr >= MAX_GIC_NR);
  879. gic_check_cpu_features();
  880. gic = &gic_data[gic_nr];
  881. #ifdef CONFIG_GIC_NON_BANKED
  882. if (percpu_offset) { /* Frankein-GIC without banked registers... */
  883. unsigned int cpu;
  884. gic->dist_base.percpu_base = alloc_percpu(void __iomem *);
  885. gic->cpu_base.percpu_base = alloc_percpu(void __iomem *);
  886. if (WARN_ON(!gic->dist_base.percpu_base ||
  887. !gic->cpu_base.percpu_base)) {
  888. free_percpu(gic->dist_base.percpu_base);
  889. free_percpu(gic->cpu_base.percpu_base);
  890. return;
  891. }
  892. for_each_possible_cpu(cpu) {
  893. u32 mpidr = cpu_logical_map(cpu);
  894. u32 core_id = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  895. unsigned long offset = percpu_offset * core_id;
  896. *per_cpu_ptr(gic->dist_base.percpu_base, cpu) = dist_base + offset;
  897. *per_cpu_ptr(gic->cpu_base.percpu_base, cpu) = cpu_base + offset;
  898. }
  899. gic_set_base_accessor(gic, gic_get_percpu_base);
  900. } else
  901. #endif
  902. { /* Normal, sane GIC... */
  903. WARN(percpu_offset,
  904. "GIC_NON_BANKED not enabled, ignoring %08x offset!",
  905. percpu_offset);
  906. gic->dist_base.common_base = dist_base;
  907. gic->cpu_base.common_base = cpu_base;
  908. gic_set_base_accessor(gic, gic_get_common_base);
  909. }
  910. /*
  911. * Find out how many interrupts are supported.
  912. * The GIC only supports up to 1020 interrupt sources.
  913. */
  914. gic_irqs = readl_relaxed(gic_data_dist_base(gic) + GIC_DIST_CTR) & 0x1f;
  915. gic_irqs = (gic_irqs + 1) * 32;
  916. if (gic_irqs > 1020)
  917. gic_irqs = 1020;
  918. gic->gic_irqs = gic_irqs;
  919. if (handle) { /* DT/ACPI */
  920. gic->domain = irq_domain_create_linear(handle, gic_irqs,
  921. &gic_irq_domain_hierarchy_ops,
  922. gic);
  923. } else { /* Legacy support */
  924. /*
  925. * For primary GICs, skip over SGIs.
  926. * For secondary GICs, skip over PPIs, too.
  927. */
  928. if (gic_nr == 0 && (irq_start & 31) > 0) {
  929. hwirq_base = 16;
  930. if (irq_start != -1)
  931. irq_start = (irq_start & ~31) + 16;
  932. } else {
  933. hwirq_base = 32;
  934. }
  935. gic_irqs -= hwirq_base; /* calculate # of irqs to allocate */
  936. irq_base = irq_alloc_descs(irq_start, 16, gic_irqs,
  937. numa_node_id());
  938. if (IS_ERR_VALUE(irq_base)) {
  939. WARN(1, "Cannot allocate irq_descs @ IRQ%d, assuming pre-allocated\n",
  940. irq_start);
  941. irq_base = irq_start;
  942. }
  943. gic->domain = irq_domain_add_legacy(NULL, gic_irqs, irq_base,
  944. hwirq_base, &gic_irq_domain_ops, gic);
  945. }
  946. if (WARN_ON(!gic->domain))
  947. return;
  948. if (gic_nr == 0) {
  949. /*
  950. * Initialize the CPU interface map to all CPUs.
  951. * It will be refined as each CPU probes its ID.
  952. * This is only necessary for the primary GIC.
  953. */
  954. for (i = 0; i < NR_GIC_CPU_IF; i++)
  955. gic_cpu_map[i] = 0xff;
  956. #ifdef CONFIG_SMP
  957. set_smp_cross_call(gic_raise_softirq);
  958. register_cpu_notifier(&gic_cpu_notifier);
  959. #endif
  960. set_handle_irq(gic_handle_irq);
  961. if (static_key_true(&supports_deactivate))
  962. pr_info("GIC: Using split EOI/Deactivate mode\n");
  963. }
  964. gic_dist_init(gic);
  965. gic_cpu_init(gic);
  966. gic_pm_init(gic);
  967. }
  968. void __init gic_init(unsigned int gic_nr, int irq_start,
  969. void __iomem *dist_base, void __iomem *cpu_base)
  970. {
  971. /*
  972. * Non-DT/ACPI systems won't run a hypervisor, so let's not
  973. * bother with these...
  974. */
  975. static_key_slow_dec(&supports_deactivate);
  976. __gic_init_bases(gic_nr, irq_start, dist_base, cpu_base, 0, NULL);
  977. }
  978. #ifdef CONFIG_OF
  979. static int gic_cnt __initdata;
  980. static bool gic_check_eoimode(struct device_node *node, void __iomem **base)
  981. {
  982. struct resource cpuif_res;
  983. of_address_to_resource(node, 1, &cpuif_res);
  984. if (!is_hyp_mode_available())
  985. return false;
  986. if (resource_size(&cpuif_res) < SZ_8K)
  987. return false;
  988. if (resource_size(&cpuif_res) == SZ_128K) {
  989. u32 val_low, val_high;
  990. /*
  991. * Verify that we have the first 4kB of a GIC400
  992. * aliased over the first 64kB by checking the
  993. * GICC_IIDR register on both ends.
  994. */
  995. val_low = readl_relaxed(*base + GIC_CPU_IDENT);
  996. val_high = readl_relaxed(*base + GIC_CPU_IDENT + 0xf000);
  997. if ((val_low & 0xffff0fff) != 0x0202043B ||
  998. val_low != val_high)
  999. return false;
  1000. /*
  1001. * Move the base up by 60kB, so that we have a 8kB
  1002. * contiguous region, which allows us to use GICC_DIR
  1003. * at its normal offset. Please pass me that bucket.
  1004. */
  1005. *base += 0xf000;
  1006. cpuif_res.start += 0xf000;
  1007. pr_warn("GIC: Adjusting CPU interface base to %pa",
  1008. &cpuif_res.start);
  1009. }
  1010. return true;
  1011. }
  1012. static int __init
  1013. gic_of_init(struct device_node *node, struct device_node *parent)
  1014. {
  1015. void __iomem *cpu_base;
  1016. void __iomem *dist_base;
  1017. u32 percpu_offset;
  1018. int irq;
  1019. if (WARN_ON(!node))
  1020. return -ENODEV;
  1021. dist_base = of_iomap(node, 0);
  1022. WARN(!dist_base, "unable to map gic dist registers\n");
  1023. cpu_base = of_iomap(node, 1);
  1024. WARN(!cpu_base, "unable to map gic cpu registers\n");
  1025. /*
  1026. * Disable split EOI/Deactivate if either HYP is not available
  1027. * or the CPU interface is too small.
  1028. */
  1029. if (gic_cnt == 0 && !gic_check_eoimode(node, &cpu_base))
  1030. static_key_slow_dec(&supports_deactivate);
  1031. if (of_property_read_u32(node, "cpu-offset", &percpu_offset))
  1032. percpu_offset = 0;
  1033. __gic_init_bases(gic_cnt, -1, dist_base, cpu_base, percpu_offset,
  1034. &node->fwnode);
  1035. if (!gic_cnt)
  1036. gic_init_physaddr(node);
  1037. if (parent) {
  1038. irq = irq_of_parse_and_map(node, 0);
  1039. gic_cascade_irq(gic_cnt, irq);
  1040. }
  1041. if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
  1042. gicv2m_of_init(node, gic_data[gic_cnt].domain);
  1043. gic_cnt++;
  1044. return 0;
  1045. }
  1046. IRQCHIP_DECLARE(gic_400, "arm,gic-400", gic_of_init);
  1047. IRQCHIP_DECLARE(arm11mp_gic, "arm,arm11mp-gic", gic_of_init);
  1048. IRQCHIP_DECLARE(arm1176jzf_dc_gic, "arm,arm1176jzf-devchip-gic", gic_of_init);
  1049. IRQCHIP_DECLARE(cortex_a15_gic, "arm,cortex-a15-gic", gic_of_init);
  1050. IRQCHIP_DECLARE(cortex_a9_gic, "arm,cortex-a9-gic", gic_of_init);
  1051. IRQCHIP_DECLARE(cortex_a7_gic, "arm,cortex-a7-gic", gic_of_init);
  1052. IRQCHIP_DECLARE(msm_8660_qgic, "qcom,msm-8660-qgic", gic_of_init);
  1053. IRQCHIP_DECLARE(msm_qgic2, "qcom,msm-qgic2", gic_of_init);
  1054. IRQCHIP_DECLARE(pl390, "arm,pl390", gic_of_init);
  1055. #endif
  1056. #ifdef CONFIG_ACPI
  1057. static phys_addr_t cpu_phy_base __initdata;
  1058. static int __init
  1059. gic_acpi_parse_madt_cpu(struct acpi_subtable_header *header,
  1060. const unsigned long end)
  1061. {
  1062. struct acpi_madt_generic_interrupt *processor;
  1063. phys_addr_t gic_cpu_base;
  1064. static int cpu_base_assigned;
  1065. processor = (struct acpi_madt_generic_interrupt *)header;
  1066. if (BAD_MADT_GICC_ENTRY(processor, end))
  1067. return -EINVAL;
  1068. /*
  1069. * There is no support for non-banked GICv1/2 register in ACPI spec.
  1070. * All CPU interface addresses have to be the same.
  1071. */
  1072. gic_cpu_base = processor->base_address;
  1073. if (cpu_base_assigned && gic_cpu_base != cpu_phy_base)
  1074. return -EINVAL;
  1075. cpu_phy_base = gic_cpu_base;
  1076. cpu_base_assigned = 1;
  1077. return 0;
  1078. }
  1079. /* The things you have to do to just *count* something... */
  1080. static int __init acpi_dummy_func(struct acpi_subtable_header *header,
  1081. const unsigned long end)
  1082. {
  1083. return 0;
  1084. }
  1085. static bool __init acpi_gic_redist_is_present(void)
  1086. {
  1087. return acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
  1088. acpi_dummy_func, 0) > 0;
  1089. }
  1090. static bool __init gic_validate_dist(struct acpi_subtable_header *header,
  1091. struct acpi_probe_entry *ape)
  1092. {
  1093. struct acpi_madt_generic_distributor *dist;
  1094. dist = (struct acpi_madt_generic_distributor *)header;
  1095. return (dist->version == ape->driver_data &&
  1096. (dist->version != ACPI_MADT_GIC_VERSION_NONE ||
  1097. !acpi_gic_redist_is_present()));
  1098. }
  1099. #define ACPI_GICV2_DIST_MEM_SIZE (SZ_4K)
  1100. #define ACPI_GIC_CPU_IF_MEM_SIZE (SZ_8K)
  1101. static int __init gic_v2_acpi_init(struct acpi_subtable_header *header,
  1102. const unsigned long end)
  1103. {
  1104. struct acpi_madt_generic_distributor *dist;
  1105. void __iomem *cpu_base, *dist_base;
  1106. struct fwnode_handle *domain_handle;
  1107. int count;
  1108. /* Collect CPU base addresses */
  1109. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1110. gic_acpi_parse_madt_cpu, 0);
  1111. if (count <= 0) {
  1112. pr_err("No valid GICC entries exist\n");
  1113. return -EINVAL;
  1114. }
  1115. cpu_base = ioremap(cpu_phy_base, ACPI_GIC_CPU_IF_MEM_SIZE);
  1116. if (!cpu_base) {
  1117. pr_err("Unable to map GICC registers\n");
  1118. return -ENOMEM;
  1119. }
  1120. dist = (struct acpi_madt_generic_distributor *)header;
  1121. dist_base = ioremap(dist->base_address, ACPI_GICV2_DIST_MEM_SIZE);
  1122. if (!dist_base) {
  1123. pr_err("Unable to map GICD registers\n");
  1124. iounmap(cpu_base);
  1125. return -ENOMEM;
  1126. }
  1127. /*
  1128. * Disable split EOI/Deactivate if HYP is not available. ACPI
  1129. * guarantees that we'll always have a GICv2, so the CPU
  1130. * interface will always be the right size.
  1131. */
  1132. if (!is_hyp_mode_available())
  1133. static_key_slow_dec(&supports_deactivate);
  1134. /*
  1135. * Initialize GIC instance zero (no multi-GIC support).
  1136. */
  1137. domain_handle = irq_domain_alloc_fwnode(dist_base);
  1138. if (!domain_handle) {
  1139. pr_err("Unable to allocate domain handle\n");
  1140. iounmap(cpu_base);
  1141. iounmap(dist_base);
  1142. return -ENOMEM;
  1143. }
  1144. __gic_init_bases(0, -1, dist_base, cpu_base, 0, domain_handle);
  1145. acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
  1146. return 0;
  1147. }
  1148. IRQCHIP_ACPI_DECLARE(gic_v2, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1149. gic_validate_dist, ACPI_MADT_GIC_VERSION_V2,
  1150. gic_v2_acpi_init);
  1151. IRQCHIP_ACPI_DECLARE(gic_v2_maybe, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1152. gic_validate_dist, ACPI_MADT_GIC_VERSION_NONE,
  1153. gic_v2_acpi_init);
  1154. #endif