irq-gic.c 33 KB

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