irq-gic.c 42 KB

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