irq-gic.c 41 KB

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