irq-gic.c 36 KB

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