irq-gic.c 40 KB

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