irq-gic-v3.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. /*
  2. * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #define pr_fmt(fmt) "GICv3: " fmt
  18. #include <linux/acpi.h>
  19. #include <linux/cpu.h>
  20. #include <linux/cpu_pm.h>
  21. #include <linux/delay.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irqdomain.h>
  24. #include <linux/of.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/percpu.h>
  28. #include <linux/slab.h>
  29. #include <linux/irqchip.h>
  30. #include <linux/irqchip/arm-gic-common.h>
  31. #include <linux/irqchip/arm-gic-v3.h>
  32. #include <linux/irqchip/irq-partition-percpu.h>
  33. #include <asm/cputype.h>
  34. #include <asm/exception.h>
  35. #include <asm/smp_plat.h>
  36. #include <asm/virt.h>
  37. #include "irq-gic-common.h"
  38. struct redist_region {
  39. void __iomem *redist_base;
  40. phys_addr_t phys_base;
  41. bool single_redist;
  42. };
  43. struct gic_chip_data {
  44. struct fwnode_handle *fwnode;
  45. void __iomem *dist_base;
  46. struct redist_region *redist_regions;
  47. struct rdists rdists;
  48. struct irq_domain *domain;
  49. u64 redist_stride;
  50. u32 nr_redist_regions;
  51. unsigned int irq_nr;
  52. struct partition_desc *ppi_descs[16];
  53. };
  54. static struct gic_chip_data gic_data __read_mostly;
  55. static struct static_key supports_deactivate = STATIC_KEY_INIT_TRUE;
  56. static struct gic_kvm_info gic_v3_kvm_info;
  57. #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
  58. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  59. #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
  60. /* Our default, arbitrary priority value. Linux only uses one anyway. */
  61. #define DEFAULT_PMR_VALUE 0xf0
  62. static inline unsigned int gic_irq(struct irq_data *d)
  63. {
  64. return d->hwirq;
  65. }
  66. static inline int gic_irq_in_rdist(struct irq_data *d)
  67. {
  68. return gic_irq(d) < 32;
  69. }
  70. static inline void __iomem *gic_dist_base(struct irq_data *d)
  71. {
  72. if (gic_irq_in_rdist(d)) /* SGI+PPI -> SGI_base for this CPU */
  73. return gic_data_rdist_sgi_base();
  74. if (d->hwirq <= 1023) /* SPI -> dist_base */
  75. return gic_data.dist_base;
  76. return NULL;
  77. }
  78. static void gic_do_wait_for_rwp(void __iomem *base)
  79. {
  80. u32 count = 1000000; /* 1s! */
  81. while (readl_relaxed(base + GICD_CTLR) & GICD_CTLR_RWP) {
  82. count--;
  83. if (!count) {
  84. pr_err_ratelimited("RWP timeout, gone fishing\n");
  85. return;
  86. }
  87. cpu_relax();
  88. udelay(1);
  89. };
  90. }
  91. /* Wait for completion of a distributor change */
  92. static void gic_dist_wait_for_rwp(void)
  93. {
  94. gic_do_wait_for_rwp(gic_data.dist_base);
  95. }
  96. /* Wait for completion of a redistributor change */
  97. static void gic_redist_wait_for_rwp(void)
  98. {
  99. gic_do_wait_for_rwp(gic_data_rdist_rd_base());
  100. }
  101. #ifdef CONFIG_ARM64
  102. static DEFINE_STATIC_KEY_FALSE(is_cavium_thunderx);
  103. static u64 __maybe_unused gic_read_iar(void)
  104. {
  105. if (static_branch_unlikely(&is_cavium_thunderx))
  106. return gic_read_iar_cavium_thunderx();
  107. else
  108. return gic_read_iar_common();
  109. }
  110. #endif
  111. static void gic_enable_redist(bool enable)
  112. {
  113. void __iomem *rbase;
  114. u32 count = 1000000; /* 1s! */
  115. u32 val;
  116. rbase = gic_data_rdist_rd_base();
  117. val = readl_relaxed(rbase + GICR_WAKER);
  118. if (enable)
  119. /* Wake up this CPU redistributor */
  120. val &= ~GICR_WAKER_ProcessorSleep;
  121. else
  122. val |= GICR_WAKER_ProcessorSleep;
  123. writel_relaxed(val, rbase + GICR_WAKER);
  124. if (!enable) { /* Check that GICR_WAKER is writeable */
  125. val = readl_relaxed(rbase + GICR_WAKER);
  126. if (!(val & GICR_WAKER_ProcessorSleep))
  127. return; /* No PM support in this redistributor */
  128. }
  129. while (--count) {
  130. val = readl_relaxed(rbase + GICR_WAKER);
  131. if (enable ^ (bool)(val & GICR_WAKER_ChildrenAsleep))
  132. break;
  133. cpu_relax();
  134. udelay(1);
  135. };
  136. if (!count)
  137. pr_err_ratelimited("redistributor failed to %s...\n",
  138. enable ? "wakeup" : "sleep");
  139. }
  140. /*
  141. * Routines to disable, enable, EOI and route interrupts
  142. */
  143. static int gic_peek_irq(struct irq_data *d, u32 offset)
  144. {
  145. u32 mask = 1 << (gic_irq(d) % 32);
  146. void __iomem *base;
  147. if (gic_irq_in_rdist(d))
  148. base = gic_data_rdist_sgi_base();
  149. else
  150. base = gic_data.dist_base;
  151. return !!(readl_relaxed(base + offset + (gic_irq(d) / 32) * 4) & mask);
  152. }
  153. static void gic_poke_irq(struct irq_data *d, u32 offset)
  154. {
  155. u32 mask = 1 << (gic_irq(d) % 32);
  156. void (*rwp_wait)(void);
  157. void __iomem *base;
  158. if (gic_irq_in_rdist(d)) {
  159. base = gic_data_rdist_sgi_base();
  160. rwp_wait = gic_redist_wait_for_rwp;
  161. } else {
  162. base = gic_data.dist_base;
  163. rwp_wait = gic_dist_wait_for_rwp;
  164. }
  165. writel_relaxed(mask, base + offset + (gic_irq(d) / 32) * 4);
  166. rwp_wait();
  167. }
  168. static void gic_mask_irq(struct irq_data *d)
  169. {
  170. gic_poke_irq(d, GICD_ICENABLER);
  171. }
  172. static void gic_eoimode1_mask_irq(struct irq_data *d)
  173. {
  174. gic_mask_irq(d);
  175. /*
  176. * When masking a forwarded interrupt, make sure it is
  177. * deactivated as well.
  178. *
  179. * This ensures that an interrupt that is getting
  180. * disabled/masked will not get "stuck", because there is
  181. * noone to deactivate it (guest is being terminated).
  182. */
  183. if (irqd_is_forwarded_to_vcpu(d))
  184. gic_poke_irq(d, GICD_ICACTIVER);
  185. }
  186. static void gic_unmask_irq(struct irq_data *d)
  187. {
  188. gic_poke_irq(d, GICD_ISENABLER);
  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. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  195. return -EINVAL;
  196. switch (which) {
  197. case IRQCHIP_STATE_PENDING:
  198. reg = val ? GICD_ISPENDR : GICD_ICPENDR;
  199. break;
  200. case IRQCHIP_STATE_ACTIVE:
  201. reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
  202. break;
  203. case IRQCHIP_STATE_MASKED:
  204. reg = val ? GICD_ICENABLER : GICD_ISENABLER;
  205. break;
  206. default:
  207. return -EINVAL;
  208. }
  209. gic_poke_irq(d, reg);
  210. return 0;
  211. }
  212. static int gic_irq_get_irqchip_state(struct irq_data *d,
  213. enum irqchip_irq_state which, bool *val)
  214. {
  215. if (d->hwirq >= gic_data.irq_nr) /* PPI/SPI only */
  216. return -EINVAL;
  217. switch (which) {
  218. case IRQCHIP_STATE_PENDING:
  219. *val = gic_peek_irq(d, GICD_ISPENDR);
  220. break;
  221. case IRQCHIP_STATE_ACTIVE:
  222. *val = gic_peek_irq(d, GICD_ISACTIVER);
  223. break;
  224. case IRQCHIP_STATE_MASKED:
  225. *val = !gic_peek_irq(d, GICD_ISENABLER);
  226. break;
  227. default:
  228. return -EINVAL;
  229. }
  230. return 0;
  231. }
  232. static void gic_eoi_irq(struct irq_data *d)
  233. {
  234. gic_write_eoir(gic_irq(d));
  235. }
  236. static void gic_eoimode1_eoi_irq(struct irq_data *d)
  237. {
  238. /*
  239. * No need to deactivate an LPI, or an interrupt that
  240. * is is getting forwarded to a vcpu.
  241. */
  242. if (gic_irq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
  243. return;
  244. gic_write_dir(gic_irq(d));
  245. }
  246. static int gic_set_type(struct irq_data *d, unsigned int type)
  247. {
  248. unsigned int irq = gic_irq(d);
  249. void (*rwp_wait)(void);
  250. void __iomem *base;
  251. /* Interrupt configuration for SGIs can't be changed */
  252. if (irq < 16)
  253. return -EINVAL;
  254. /* SPIs have restrictions on the supported types */
  255. if (irq >= 32 && type != IRQ_TYPE_LEVEL_HIGH &&
  256. type != IRQ_TYPE_EDGE_RISING)
  257. return -EINVAL;
  258. if (gic_irq_in_rdist(d)) {
  259. base = gic_data_rdist_sgi_base();
  260. rwp_wait = gic_redist_wait_for_rwp;
  261. } else {
  262. base = gic_data.dist_base;
  263. rwp_wait = gic_dist_wait_for_rwp;
  264. }
  265. return gic_configure_irq(irq, type, base, rwp_wait);
  266. }
  267. static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  268. {
  269. if (vcpu)
  270. irqd_set_forwarded_to_vcpu(d);
  271. else
  272. irqd_clr_forwarded_to_vcpu(d);
  273. return 0;
  274. }
  275. static u64 gic_mpidr_to_affinity(unsigned long mpidr)
  276. {
  277. u64 aff;
  278. aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
  279. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  280. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  281. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  282. return aff;
  283. }
  284. static asmlinkage void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  285. {
  286. u32 irqnr;
  287. do {
  288. irqnr = gic_read_iar();
  289. if (likely(irqnr > 15 && irqnr < 1020) || irqnr >= 8192) {
  290. int err;
  291. if (static_key_true(&supports_deactivate))
  292. gic_write_eoir(irqnr);
  293. err = handle_domain_irq(gic_data.domain, irqnr, regs);
  294. if (err) {
  295. WARN_ONCE(true, "Unexpected interrupt received!\n");
  296. if (static_key_true(&supports_deactivate)) {
  297. if (irqnr < 8192)
  298. gic_write_dir(irqnr);
  299. } else {
  300. gic_write_eoir(irqnr);
  301. }
  302. }
  303. continue;
  304. }
  305. if (irqnr < 16) {
  306. gic_write_eoir(irqnr);
  307. if (static_key_true(&supports_deactivate))
  308. gic_write_dir(irqnr);
  309. #ifdef CONFIG_SMP
  310. /*
  311. * Unlike GICv2, we don't need an smp_rmb() here.
  312. * The control dependency from gic_read_iar to
  313. * the ISB in gic_write_eoir is enough to ensure
  314. * that any shared data read by handle_IPI will
  315. * be read after the ACK.
  316. */
  317. handle_IPI(irqnr, regs);
  318. #else
  319. WARN_ONCE(true, "Unexpected SGI received!\n");
  320. #endif
  321. continue;
  322. }
  323. } while (irqnr != ICC_IAR1_EL1_SPURIOUS);
  324. }
  325. static void __init gic_dist_init(void)
  326. {
  327. unsigned int i;
  328. u64 affinity;
  329. void __iomem *base = gic_data.dist_base;
  330. /* Disable the distributor */
  331. writel_relaxed(0, base + GICD_CTLR);
  332. gic_dist_wait_for_rwp();
  333. /*
  334. * Configure SPIs as non-secure Group-1. This will only matter
  335. * if the GIC only has a single security state. This will not
  336. * do the right thing if the kernel is running in secure mode,
  337. * but that's not the intended use case anyway.
  338. */
  339. for (i = 32; i < gic_data.irq_nr; i += 32)
  340. writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
  341. gic_dist_config(base, gic_data.irq_nr, gic_dist_wait_for_rwp);
  342. /* Enable distributor with ARE, Group1 */
  343. writel_relaxed(GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1,
  344. base + GICD_CTLR);
  345. /*
  346. * Set all global interrupts to the boot CPU only. ARE must be
  347. * enabled.
  348. */
  349. affinity = gic_mpidr_to_affinity(cpu_logical_map(smp_processor_id()));
  350. for (i = 32; i < gic_data.irq_nr; i++)
  351. gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
  352. }
  353. static int gic_populate_rdist(void)
  354. {
  355. unsigned long mpidr = cpu_logical_map(smp_processor_id());
  356. u64 typer;
  357. u32 aff;
  358. int i;
  359. /*
  360. * Convert affinity to a 32bit value that can be matched to
  361. * GICR_TYPER bits [63:32].
  362. */
  363. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
  364. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  365. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  366. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  367. for (i = 0; i < gic_data.nr_redist_regions; i++) {
  368. void __iomem *ptr = gic_data.redist_regions[i].redist_base;
  369. u32 reg;
  370. reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
  371. if (reg != GIC_PIDR2_ARCH_GICv3 &&
  372. reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
  373. pr_warn("No redistributor present @%p\n", ptr);
  374. break;
  375. }
  376. do {
  377. typer = gic_read_typer(ptr + GICR_TYPER);
  378. if ((typer >> 32) == aff) {
  379. u64 offset = ptr - gic_data.redist_regions[i].redist_base;
  380. gic_data_rdist_rd_base() = ptr;
  381. gic_data_rdist()->phys_base = gic_data.redist_regions[i].phys_base + offset;
  382. pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
  383. smp_processor_id(), mpidr, i,
  384. &gic_data_rdist()->phys_base);
  385. return 0;
  386. }
  387. if (gic_data.redist_regions[i].single_redist)
  388. break;
  389. if (gic_data.redist_stride) {
  390. ptr += gic_data.redist_stride;
  391. } else {
  392. ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
  393. if (typer & GICR_TYPER_VLPIS)
  394. ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
  395. }
  396. } while (!(typer & GICR_TYPER_LAST));
  397. }
  398. /* We couldn't even deal with ourselves... */
  399. WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
  400. smp_processor_id(), mpidr);
  401. return -ENODEV;
  402. }
  403. static void gic_cpu_sys_reg_init(void)
  404. {
  405. /*
  406. * Need to check that the SRE bit has actually been set. If
  407. * not, it means that SRE is disabled at EL2. We're going to
  408. * die painfully, and there is nothing we can do about it.
  409. *
  410. * Kindly inform the luser.
  411. */
  412. if (!gic_enable_sre())
  413. pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
  414. /* Set priority mask register */
  415. gic_write_pmr(DEFAULT_PMR_VALUE);
  416. /*
  417. * Some firmwares hand over to the kernel with the BPR changed from
  418. * its reset value (and with a value large enough to prevent
  419. * any pre-emptive interrupts from working at all). Writing a zero
  420. * to BPR restores is reset value.
  421. */
  422. gic_write_bpr1(0);
  423. if (static_key_true(&supports_deactivate)) {
  424. /* EOI drops priority only (mode 1) */
  425. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
  426. } else {
  427. /* EOI deactivates interrupt too (mode 0) */
  428. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  429. }
  430. /* ... and let's hit the road... */
  431. gic_write_grpen1(1);
  432. }
  433. static int gic_dist_supports_lpis(void)
  434. {
  435. return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
  436. }
  437. static void gic_cpu_init(void)
  438. {
  439. void __iomem *rbase;
  440. /* Register ourselves with the rest of the world */
  441. if (gic_populate_rdist())
  442. return;
  443. gic_enable_redist(true);
  444. rbase = gic_data_rdist_sgi_base();
  445. /* Configure SGIs/PPIs as non-secure Group-1 */
  446. writel_relaxed(~0, rbase + GICR_IGROUPR0);
  447. gic_cpu_config(rbase, gic_redist_wait_for_rwp);
  448. /* Give LPIs a spin */
  449. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  450. its_cpu_init();
  451. /* initialise system registers */
  452. gic_cpu_sys_reg_init();
  453. }
  454. #ifdef CONFIG_SMP
  455. static int gic_starting_cpu(unsigned int cpu)
  456. {
  457. gic_cpu_init();
  458. return 0;
  459. }
  460. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  461. unsigned long cluster_id)
  462. {
  463. int next_cpu, cpu = *base_cpu;
  464. unsigned long mpidr = cpu_logical_map(cpu);
  465. u16 tlist = 0;
  466. while (cpu < nr_cpu_ids) {
  467. /*
  468. * If we ever get a cluster of more than 16 CPUs, just
  469. * scream and skip that CPU.
  470. */
  471. if (WARN_ON((mpidr & 0xff) >= 16))
  472. goto out;
  473. tlist |= 1 << (mpidr & 0xf);
  474. next_cpu = cpumask_next(cpu, mask);
  475. if (next_cpu >= nr_cpu_ids)
  476. goto out;
  477. cpu = next_cpu;
  478. mpidr = cpu_logical_map(cpu);
  479. if (cluster_id != (mpidr & ~0xffUL)) {
  480. cpu--;
  481. goto out;
  482. }
  483. }
  484. out:
  485. *base_cpu = cpu;
  486. return tlist;
  487. }
  488. #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
  489. (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
  490. << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
  491. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  492. {
  493. u64 val;
  494. val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
  495. MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
  496. irq << ICC_SGI1R_SGI_ID_SHIFT |
  497. MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
  498. tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
  499. pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  500. gic_write_sgi1r(val);
  501. }
  502. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  503. {
  504. int cpu;
  505. if (WARN_ON(irq >= 16))
  506. return;
  507. /*
  508. * Ensure that stores to Normal memory are visible to the
  509. * other CPUs before issuing the IPI.
  510. */
  511. smp_wmb();
  512. for_each_cpu(cpu, mask) {
  513. unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
  514. u16 tlist;
  515. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  516. gic_send_sgi(cluster_id, tlist, irq);
  517. }
  518. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  519. isb();
  520. }
  521. static void gic_smp_init(void)
  522. {
  523. set_smp_cross_call(gic_raise_softirq);
  524. cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GICV3_STARTING,
  525. "AP_IRQ_GICV3_STARTING", gic_starting_cpu,
  526. NULL);
  527. }
  528. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  529. bool force)
  530. {
  531. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  532. void __iomem *reg;
  533. int enabled;
  534. u64 val;
  535. if (cpu >= nr_cpu_ids)
  536. return -EINVAL;
  537. if (gic_irq_in_rdist(d))
  538. return -EINVAL;
  539. /* If interrupt was enabled, disable it first */
  540. enabled = gic_peek_irq(d, GICD_ISENABLER);
  541. if (enabled)
  542. gic_mask_irq(d);
  543. reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
  544. val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
  545. gic_write_irouter(val, reg);
  546. /*
  547. * If the interrupt was enabled, enabled it again. Otherwise,
  548. * just wait for the distributor to have digested our changes.
  549. */
  550. if (enabled)
  551. gic_unmask_irq(d);
  552. else
  553. gic_dist_wait_for_rwp();
  554. return IRQ_SET_MASK_OK_DONE;
  555. }
  556. #else
  557. #define gic_set_affinity NULL
  558. #define gic_smp_init() do { } while(0)
  559. #endif
  560. #ifdef CONFIG_CPU_PM
  561. /* Check whether it's single security state view */
  562. static bool gic_dist_security_disabled(void)
  563. {
  564. return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
  565. }
  566. static int gic_cpu_pm_notifier(struct notifier_block *self,
  567. unsigned long cmd, void *v)
  568. {
  569. if (cmd == CPU_PM_EXIT) {
  570. if (gic_dist_security_disabled())
  571. gic_enable_redist(true);
  572. gic_cpu_sys_reg_init();
  573. } else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
  574. gic_write_grpen1(0);
  575. gic_enable_redist(false);
  576. }
  577. return NOTIFY_OK;
  578. }
  579. static struct notifier_block gic_cpu_pm_notifier_block = {
  580. .notifier_call = gic_cpu_pm_notifier,
  581. };
  582. static void gic_cpu_pm_init(void)
  583. {
  584. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  585. }
  586. #else
  587. static inline void gic_cpu_pm_init(void) { }
  588. #endif /* CONFIG_CPU_PM */
  589. static struct irq_chip gic_chip = {
  590. .name = "GICv3",
  591. .irq_mask = gic_mask_irq,
  592. .irq_unmask = gic_unmask_irq,
  593. .irq_eoi = gic_eoi_irq,
  594. .irq_set_type = gic_set_type,
  595. .irq_set_affinity = gic_set_affinity,
  596. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  597. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  598. .flags = IRQCHIP_SET_TYPE_MASKED,
  599. };
  600. static struct irq_chip gic_eoimode1_chip = {
  601. .name = "GICv3",
  602. .irq_mask = gic_eoimode1_mask_irq,
  603. .irq_unmask = gic_unmask_irq,
  604. .irq_eoi = gic_eoimode1_eoi_irq,
  605. .irq_set_type = gic_set_type,
  606. .irq_set_affinity = gic_set_affinity,
  607. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  608. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  609. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  610. .flags = IRQCHIP_SET_TYPE_MASKED,
  611. };
  612. #define GIC_ID_NR (1U << gic_data.rdists.id_bits)
  613. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  614. irq_hw_number_t hw)
  615. {
  616. struct irq_chip *chip = &gic_chip;
  617. if (static_key_true(&supports_deactivate))
  618. chip = &gic_eoimode1_chip;
  619. /* SGIs are private to the core kernel */
  620. if (hw < 16)
  621. return -EPERM;
  622. /* Nothing here */
  623. if (hw >= gic_data.irq_nr && hw < 8192)
  624. return -EPERM;
  625. /* Off limits */
  626. if (hw >= GIC_ID_NR)
  627. return -EPERM;
  628. /* PPIs */
  629. if (hw < 32) {
  630. irq_set_percpu_devid(irq);
  631. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  632. handle_percpu_devid_irq, NULL, NULL);
  633. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  634. }
  635. /* SPIs */
  636. if (hw >= 32 && hw < gic_data.irq_nr) {
  637. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  638. handle_fasteoi_irq, NULL, NULL);
  639. irq_set_probe(irq);
  640. }
  641. /* LPIs */
  642. if (hw >= 8192 && hw < GIC_ID_NR) {
  643. if (!gic_dist_supports_lpis())
  644. return -EPERM;
  645. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  646. handle_fasteoi_irq, NULL, NULL);
  647. }
  648. return 0;
  649. }
  650. static int gic_irq_domain_translate(struct irq_domain *d,
  651. struct irq_fwspec *fwspec,
  652. unsigned long *hwirq,
  653. unsigned int *type)
  654. {
  655. if (is_of_node(fwspec->fwnode)) {
  656. if (fwspec->param_count < 3)
  657. return -EINVAL;
  658. switch (fwspec->param[0]) {
  659. case 0: /* SPI */
  660. *hwirq = fwspec->param[1] + 32;
  661. break;
  662. case 1: /* PPI */
  663. *hwirq = fwspec->param[1] + 16;
  664. break;
  665. case GIC_IRQ_TYPE_LPI: /* LPI */
  666. *hwirq = fwspec->param[1];
  667. break;
  668. default:
  669. return -EINVAL;
  670. }
  671. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  672. return 0;
  673. }
  674. if (is_fwnode_irqchip(fwspec->fwnode)) {
  675. if(fwspec->param_count != 2)
  676. return -EINVAL;
  677. *hwirq = fwspec->param[0];
  678. *type = fwspec->param[1];
  679. return 0;
  680. }
  681. return -EINVAL;
  682. }
  683. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  684. unsigned int nr_irqs, void *arg)
  685. {
  686. int i, ret;
  687. irq_hw_number_t hwirq;
  688. unsigned int type = IRQ_TYPE_NONE;
  689. struct irq_fwspec *fwspec = arg;
  690. ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  691. if (ret)
  692. return ret;
  693. for (i = 0; i < nr_irqs; i++)
  694. gic_irq_domain_map(domain, virq + i, hwirq + i);
  695. return 0;
  696. }
  697. static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  698. unsigned int nr_irqs)
  699. {
  700. int i;
  701. for (i = 0; i < nr_irqs; i++) {
  702. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  703. irq_set_handler(virq + i, NULL);
  704. irq_domain_reset_irq_data(d);
  705. }
  706. }
  707. static int gic_irq_domain_select(struct irq_domain *d,
  708. struct irq_fwspec *fwspec,
  709. enum irq_domain_bus_token bus_token)
  710. {
  711. /* Not for us */
  712. if (fwspec->fwnode != d->fwnode)
  713. return 0;
  714. /* If this is not DT, then we have a single domain */
  715. if (!is_of_node(fwspec->fwnode))
  716. return 1;
  717. /*
  718. * If this is a PPI and we have a 4th (non-null) parameter,
  719. * then we need to match the partition domain.
  720. */
  721. if (fwspec->param_count >= 4 &&
  722. fwspec->param[0] == 1 && fwspec->param[3] != 0)
  723. return d == partition_get_domain(gic_data.ppi_descs[fwspec->param[1]]);
  724. return d == gic_data.domain;
  725. }
  726. static const struct irq_domain_ops gic_irq_domain_ops = {
  727. .translate = gic_irq_domain_translate,
  728. .alloc = gic_irq_domain_alloc,
  729. .free = gic_irq_domain_free,
  730. .select = gic_irq_domain_select,
  731. };
  732. static int partition_domain_translate(struct irq_domain *d,
  733. struct irq_fwspec *fwspec,
  734. unsigned long *hwirq,
  735. unsigned int *type)
  736. {
  737. struct device_node *np;
  738. int ret;
  739. np = of_find_node_by_phandle(fwspec->param[3]);
  740. if (WARN_ON(!np))
  741. return -EINVAL;
  742. ret = partition_translate_id(gic_data.ppi_descs[fwspec->param[1]],
  743. of_node_to_fwnode(np));
  744. if (ret < 0)
  745. return ret;
  746. *hwirq = ret;
  747. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  748. return 0;
  749. }
  750. static const struct irq_domain_ops partition_domain_ops = {
  751. .translate = partition_domain_translate,
  752. .select = gic_irq_domain_select,
  753. };
  754. static void gicv3_enable_quirks(void)
  755. {
  756. #ifdef CONFIG_ARM64
  757. if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
  758. static_branch_enable(&is_cavium_thunderx);
  759. #endif
  760. }
  761. static int __init gic_init_bases(void __iomem *dist_base,
  762. struct redist_region *rdist_regs,
  763. u32 nr_redist_regions,
  764. u64 redist_stride,
  765. struct fwnode_handle *handle)
  766. {
  767. u32 typer;
  768. int gic_irqs;
  769. int err;
  770. if (!is_hyp_mode_available())
  771. static_key_slow_dec(&supports_deactivate);
  772. if (static_key_true(&supports_deactivate))
  773. pr_info("GIC: Using split EOI/Deactivate mode\n");
  774. gic_data.fwnode = handle;
  775. gic_data.dist_base = dist_base;
  776. gic_data.redist_regions = rdist_regs;
  777. gic_data.nr_redist_regions = nr_redist_regions;
  778. gic_data.redist_stride = redist_stride;
  779. gicv3_enable_quirks();
  780. /*
  781. * Find out how many interrupts are supported.
  782. * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
  783. */
  784. typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
  785. gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
  786. gic_irqs = GICD_TYPER_IRQS(typer);
  787. if (gic_irqs > 1020)
  788. gic_irqs = 1020;
  789. gic_data.irq_nr = gic_irqs;
  790. gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
  791. &gic_data);
  792. gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
  793. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
  794. err = -ENOMEM;
  795. goto out_free;
  796. }
  797. set_handle_irq(gic_handle_irq);
  798. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  799. its_init(handle, &gic_data.rdists, gic_data.domain);
  800. gic_smp_init();
  801. gic_dist_init();
  802. gic_cpu_init();
  803. gic_cpu_pm_init();
  804. return 0;
  805. out_free:
  806. if (gic_data.domain)
  807. irq_domain_remove(gic_data.domain);
  808. free_percpu(gic_data.rdists.rdist);
  809. return err;
  810. }
  811. static int __init gic_validate_dist_version(void __iomem *dist_base)
  812. {
  813. u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  814. if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
  815. return -ENODEV;
  816. return 0;
  817. }
  818. static int get_cpu_number(struct device_node *dn)
  819. {
  820. const __be32 *cell;
  821. u64 hwid;
  822. int i;
  823. cell = of_get_property(dn, "reg", NULL);
  824. if (!cell)
  825. return -1;
  826. hwid = of_read_number(cell, of_n_addr_cells(dn));
  827. /*
  828. * Non affinity bits must be set to 0 in the DT
  829. */
  830. if (hwid & ~MPIDR_HWID_BITMASK)
  831. return -1;
  832. for (i = 0; i < num_possible_cpus(); i++)
  833. if (cpu_logical_map(i) == hwid)
  834. return i;
  835. return -1;
  836. }
  837. /* Create all possible partitions at boot time */
  838. static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
  839. {
  840. struct device_node *parts_node, *child_part;
  841. int part_idx = 0, i;
  842. int nr_parts;
  843. struct partition_affinity *parts;
  844. parts_node = of_find_node_by_name(gic_node, "ppi-partitions");
  845. if (!parts_node)
  846. return;
  847. nr_parts = of_get_child_count(parts_node);
  848. if (!nr_parts)
  849. return;
  850. parts = kzalloc(sizeof(*parts) * nr_parts, GFP_KERNEL);
  851. if (WARN_ON(!parts))
  852. return;
  853. for_each_child_of_node(parts_node, child_part) {
  854. struct partition_affinity *part;
  855. int n;
  856. part = &parts[part_idx];
  857. part->partition_id = of_node_to_fwnode(child_part);
  858. pr_info("GIC: PPI partition %s[%d] { ",
  859. child_part->name, part_idx);
  860. n = of_property_count_elems_of_size(child_part, "affinity",
  861. sizeof(u32));
  862. WARN_ON(n <= 0);
  863. for (i = 0; i < n; i++) {
  864. int err, cpu;
  865. u32 cpu_phandle;
  866. struct device_node *cpu_node;
  867. err = of_property_read_u32_index(child_part, "affinity",
  868. i, &cpu_phandle);
  869. if (WARN_ON(err))
  870. continue;
  871. cpu_node = of_find_node_by_phandle(cpu_phandle);
  872. if (WARN_ON(!cpu_node))
  873. continue;
  874. cpu = get_cpu_number(cpu_node);
  875. if (WARN_ON(cpu == -1))
  876. continue;
  877. pr_cont("%s[%d] ", cpu_node->full_name, cpu);
  878. cpumask_set_cpu(cpu, &part->mask);
  879. }
  880. pr_cont("}\n");
  881. part_idx++;
  882. }
  883. for (i = 0; i < 16; i++) {
  884. unsigned int irq;
  885. struct partition_desc *desc;
  886. struct irq_fwspec ppi_fwspec = {
  887. .fwnode = gic_data.fwnode,
  888. .param_count = 3,
  889. .param = {
  890. [0] = 1,
  891. [1] = i,
  892. [2] = IRQ_TYPE_NONE,
  893. },
  894. };
  895. irq = irq_create_fwspec_mapping(&ppi_fwspec);
  896. if (WARN_ON(!irq))
  897. continue;
  898. desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
  899. irq, &partition_domain_ops);
  900. if (WARN_ON(!desc))
  901. continue;
  902. gic_data.ppi_descs[i] = desc;
  903. }
  904. }
  905. static void __init gic_of_setup_kvm_info(struct device_node *node)
  906. {
  907. int ret;
  908. struct resource r;
  909. u32 gicv_idx;
  910. gic_v3_kvm_info.type = GIC_V3;
  911. gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
  912. if (!gic_v3_kvm_info.maint_irq)
  913. return;
  914. if (of_property_read_u32(node, "#redistributor-regions",
  915. &gicv_idx))
  916. gicv_idx = 1;
  917. gicv_idx += 3; /* Also skip GICD, GICC, GICH */
  918. ret = of_address_to_resource(node, gicv_idx, &r);
  919. if (!ret)
  920. gic_v3_kvm_info.vcpu = r;
  921. gic_set_kvm_info(&gic_v3_kvm_info);
  922. }
  923. static int __init gic_of_init(struct device_node *node, struct device_node *parent)
  924. {
  925. void __iomem *dist_base;
  926. struct redist_region *rdist_regs;
  927. u64 redist_stride;
  928. u32 nr_redist_regions;
  929. int err, i;
  930. dist_base = of_iomap(node, 0);
  931. if (!dist_base) {
  932. pr_err("%s: unable to map gic dist registers\n",
  933. node->full_name);
  934. return -ENXIO;
  935. }
  936. err = gic_validate_dist_version(dist_base);
  937. if (err) {
  938. pr_err("%s: no distributor detected, giving up\n",
  939. node->full_name);
  940. goto out_unmap_dist;
  941. }
  942. if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
  943. nr_redist_regions = 1;
  944. rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
  945. if (!rdist_regs) {
  946. err = -ENOMEM;
  947. goto out_unmap_dist;
  948. }
  949. for (i = 0; i < nr_redist_regions; i++) {
  950. struct resource res;
  951. int ret;
  952. ret = of_address_to_resource(node, 1 + i, &res);
  953. rdist_regs[i].redist_base = of_iomap(node, 1 + i);
  954. if (ret || !rdist_regs[i].redist_base) {
  955. pr_err("%s: couldn't map region %d\n",
  956. node->full_name, i);
  957. err = -ENODEV;
  958. goto out_unmap_rdist;
  959. }
  960. rdist_regs[i].phys_base = res.start;
  961. }
  962. if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
  963. redist_stride = 0;
  964. err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
  965. redist_stride, &node->fwnode);
  966. if (err)
  967. goto out_unmap_rdist;
  968. gic_populate_ppi_partitions(node);
  969. gic_of_setup_kvm_info(node);
  970. return 0;
  971. out_unmap_rdist:
  972. for (i = 0; i < nr_redist_regions; i++)
  973. if (rdist_regs[i].redist_base)
  974. iounmap(rdist_regs[i].redist_base);
  975. kfree(rdist_regs);
  976. out_unmap_dist:
  977. iounmap(dist_base);
  978. return err;
  979. }
  980. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
  981. #ifdef CONFIG_ACPI
  982. static struct
  983. {
  984. void __iomem *dist_base;
  985. struct redist_region *redist_regs;
  986. u32 nr_redist_regions;
  987. bool single_redist;
  988. u32 maint_irq;
  989. int maint_irq_mode;
  990. phys_addr_t vcpu_base;
  991. } acpi_data __initdata;
  992. static void __init
  993. gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
  994. {
  995. static int count = 0;
  996. acpi_data.redist_regs[count].phys_base = phys_base;
  997. acpi_data.redist_regs[count].redist_base = redist_base;
  998. acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
  999. count++;
  1000. }
  1001. static int __init
  1002. gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
  1003. const unsigned long end)
  1004. {
  1005. struct acpi_madt_generic_redistributor *redist =
  1006. (struct acpi_madt_generic_redistributor *)header;
  1007. void __iomem *redist_base;
  1008. redist_base = ioremap(redist->base_address, redist->length);
  1009. if (!redist_base) {
  1010. pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
  1011. return -ENOMEM;
  1012. }
  1013. gic_acpi_register_redist(redist->base_address, redist_base);
  1014. return 0;
  1015. }
  1016. static int __init
  1017. gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
  1018. const unsigned long end)
  1019. {
  1020. struct acpi_madt_generic_interrupt *gicc =
  1021. (struct acpi_madt_generic_interrupt *)header;
  1022. u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1023. u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
  1024. void __iomem *redist_base;
  1025. redist_base = ioremap(gicc->gicr_base_address, size);
  1026. if (!redist_base)
  1027. return -ENOMEM;
  1028. gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
  1029. return 0;
  1030. }
  1031. static int __init gic_acpi_collect_gicr_base(void)
  1032. {
  1033. acpi_tbl_entry_handler redist_parser;
  1034. enum acpi_madt_type type;
  1035. if (acpi_data.single_redist) {
  1036. type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
  1037. redist_parser = gic_acpi_parse_madt_gicc;
  1038. } else {
  1039. type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
  1040. redist_parser = gic_acpi_parse_madt_redist;
  1041. }
  1042. /* Collect redistributor base addresses in GICR entries */
  1043. if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
  1044. return 0;
  1045. pr_info("No valid GICR entries exist\n");
  1046. return -ENODEV;
  1047. }
  1048. static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
  1049. const unsigned long end)
  1050. {
  1051. /* Subtable presence means that redist exists, that's it */
  1052. return 0;
  1053. }
  1054. static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
  1055. const unsigned long end)
  1056. {
  1057. struct acpi_madt_generic_interrupt *gicc =
  1058. (struct acpi_madt_generic_interrupt *)header;
  1059. /*
  1060. * If GICC is enabled and has valid gicr base address, then it means
  1061. * GICR base is presented via GICC
  1062. */
  1063. if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
  1064. return 0;
  1065. return -ENODEV;
  1066. }
  1067. static int __init gic_acpi_count_gicr_regions(void)
  1068. {
  1069. int count;
  1070. /*
  1071. * Count how many redistributor regions we have. It is not allowed
  1072. * to mix redistributor description, GICR and GICC subtables have to be
  1073. * mutually exclusive.
  1074. */
  1075. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
  1076. gic_acpi_match_gicr, 0);
  1077. if (count > 0) {
  1078. acpi_data.single_redist = false;
  1079. return count;
  1080. }
  1081. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1082. gic_acpi_match_gicc, 0);
  1083. if (count > 0)
  1084. acpi_data.single_redist = true;
  1085. return count;
  1086. }
  1087. static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
  1088. struct acpi_probe_entry *ape)
  1089. {
  1090. struct acpi_madt_generic_distributor *dist;
  1091. int count;
  1092. dist = (struct acpi_madt_generic_distributor *)header;
  1093. if (dist->version != ape->driver_data)
  1094. return false;
  1095. /* We need to do that exercise anyway, the sooner the better */
  1096. count = gic_acpi_count_gicr_regions();
  1097. if (count <= 0)
  1098. return false;
  1099. acpi_data.nr_redist_regions = count;
  1100. return true;
  1101. }
  1102. static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
  1103. const unsigned long end)
  1104. {
  1105. struct acpi_madt_generic_interrupt *gicc =
  1106. (struct acpi_madt_generic_interrupt *)header;
  1107. int maint_irq_mode;
  1108. static int first_madt = true;
  1109. /* Skip unusable CPUs */
  1110. if (!(gicc->flags & ACPI_MADT_ENABLED))
  1111. return 0;
  1112. maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
  1113. ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
  1114. if (first_madt) {
  1115. first_madt = false;
  1116. acpi_data.maint_irq = gicc->vgic_interrupt;
  1117. acpi_data.maint_irq_mode = maint_irq_mode;
  1118. acpi_data.vcpu_base = gicc->gicv_base_address;
  1119. return 0;
  1120. }
  1121. /*
  1122. * The maintenance interrupt and GICV should be the same for every CPU
  1123. */
  1124. if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
  1125. (acpi_data.maint_irq_mode != maint_irq_mode) ||
  1126. (acpi_data.vcpu_base != gicc->gicv_base_address))
  1127. return -EINVAL;
  1128. return 0;
  1129. }
  1130. static bool __init gic_acpi_collect_virt_info(void)
  1131. {
  1132. int count;
  1133. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1134. gic_acpi_parse_virt_madt_gicc, 0);
  1135. return (count > 0);
  1136. }
  1137. #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
  1138. #define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
  1139. #define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
  1140. static void __init gic_acpi_setup_kvm_info(void)
  1141. {
  1142. int irq;
  1143. if (!gic_acpi_collect_virt_info()) {
  1144. pr_warn("Unable to get hardware information used for virtualization\n");
  1145. return;
  1146. }
  1147. gic_v3_kvm_info.type = GIC_V3;
  1148. irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
  1149. acpi_data.maint_irq_mode,
  1150. ACPI_ACTIVE_HIGH);
  1151. if (irq <= 0)
  1152. return;
  1153. gic_v3_kvm_info.maint_irq = irq;
  1154. if (acpi_data.vcpu_base) {
  1155. struct resource *vcpu = &gic_v3_kvm_info.vcpu;
  1156. vcpu->flags = IORESOURCE_MEM;
  1157. vcpu->start = acpi_data.vcpu_base;
  1158. vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
  1159. }
  1160. gic_set_kvm_info(&gic_v3_kvm_info);
  1161. }
  1162. static int __init
  1163. gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
  1164. {
  1165. struct acpi_madt_generic_distributor *dist;
  1166. struct fwnode_handle *domain_handle;
  1167. size_t size;
  1168. int i, err;
  1169. /* Get distributor base address */
  1170. dist = (struct acpi_madt_generic_distributor *)header;
  1171. acpi_data.dist_base = ioremap(dist->base_address,
  1172. ACPI_GICV3_DIST_MEM_SIZE);
  1173. if (!acpi_data.dist_base) {
  1174. pr_err("Unable to map GICD registers\n");
  1175. return -ENOMEM;
  1176. }
  1177. err = gic_validate_dist_version(acpi_data.dist_base);
  1178. if (err) {
  1179. pr_err("No distributor detected at @%p, giving up",
  1180. acpi_data.dist_base);
  1181. goto out_dist_unmap;
  1182. }
  1183. size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
  1184. acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
  1185. if (!acpi_data.redist_regs) {
  1186. err = -ENOMEM;
  1187. goto out_dist_unmap;
  1188. }
  1189. err = gic_acpi_collect_gicr_base();
  1190. if (err)
  1191. goto out_redist_unmap;
  1192. domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
  1193. if (!domain_handle) {
  1194. err = -ENOMEM;
  1195. goto out_redist_unmap;
  1196. }
  1197. err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
  1198. acpi_data.nr_redist_regions, 0, domain_handle);
  1199. if (err)
  1200. goto out_fwhandle_free;
  1201. acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
  1202. gic_acpi_setup_kvm_info();
  1203. return 0;
  1204. out_fwhandle_free:
  1205. irq_domain_free_fwnode(domain_handle);
  1206. out_redist_unmap:
  1207. for (i = 0; i < acpi_data.nr_redist_regions; i++)
  1208. if (acpi_data.redist_regs[i].redist_base)
  1209. iounmap(acpi_data.redist_regs[i].redist_base);
  1210. kfree(acpi_data.redist_regs);
  1211. out_dist_unmap:
  1212. iounmap(acpi_data.dist_base);
  1213. return err;
  1214. }
  1215. IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1216. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
  1217. gic_acpi_init);
  1218. IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1219. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
  1220. gic_acpi_init);
  1221. IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1222. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
  1223. gic_acpi_init);
  1224. #endif