irq-gic.c 39 KB

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