irq-gic.c 36 KB

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