irq-gic.c 44 KB

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