irq-gic.c 40 KB

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