irq-gic.c 40 KB

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