irq-gic-v3.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. if (static_key_true(&supports_deactivate)) {
  417. /* EOI drops priority only (mode 1) */
  418. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
  419. } else {
  420. /* EOI deactivates interrupt too (mode 0) */
  421. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  422. }
  423. /* ... and let's hit the road... */
  424. gic_write_grpen1(1);
  425. }
  426. static int gic_dist_supports_lpis(void)
  427. {
  428. return !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS);
  429. }
  430. static void gic_cpu_init(void)
  431. {
  432. void __iomem *rbase;
  433. /* Register ourselves with the rest of the world */
  434. if (gic_populate_rdist())
  435. return;
  436. gic_enable_redist(true);
  437. rbase = gic_data_rdist_sgi_base();
  438. /* Configure SGIs/PPIs as non-secure Group-1 */
  439. writel_relaxed(~0, rbase + GICR_IGROUPR0);
  440. gic_cpu_config(rbase, gic_redist_wait_for_rwp);
  441. /* Give LPIs a spin */
  442. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis())
  443. its_cpu_init();
  444. /* initialise system registers */
  445. gic_cpu_sys_reg_init();
  446. }
  447. #ifdef CONFIG_SMP
  448. static int gic_starting_cpu(unsigned int cpu)
  449. {
  450. gic_cpu_init();
  451. return 0;
  452. }
  453. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  454. unsigned long cluster_id)
  455. {
  456. int cpu = *base_cpu;
  457. unsigned long mpidr = cpu_logical_map(cpu);
  458. u16 tlist = 0;
  459. while (cpu < nr_cpu_ids) {
  460. /*
  461. * If we ever get a cluster of more than 16 CPUs, just
  462. * scream and skip that CPU.
  463. */
  464. if (WARN_ON((mpidr & 0xff) >= 16))
  465. goto out;
  466. tlist |= 1 << (mpidr & 0xf);
  467. cpu = cpumask_next(cpu, mask);
  468. if (cpu >= nr_cpu_ids)
  469. goto out;
  470. mpidr = cpu_logical_map(cpu);
  471. if (cluster_id != (mpidr & ~0xffUL)) {
  472. cpu--;
  473. goto out;
  474. }
  475. }
  476. out:
  477. *base_cpu = cpu;
  478. return tlist;
  479. }
  480. #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
  481. (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
  482. << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
  483. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  484. {
  485. u64 val;
  486. val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
  487. MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
  488. irq << ICC_SGI1R_SGI_ID_SHIFT |
  489. MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
  490. tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
  491. pr_debug("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  492. gic_write_sgi1r(val);
  493. }
  494. static void gic_raise_softirq(const struct cpumask *mask, unsigned int irq)
  495. {
  496. int cpu;
  497. if (WARN_ON(irq >= 16))
  498. return;
  499. /*
  500. * Ensure that stores to Normal memory are visible to the
  501. * other CPUs before issuing the IPI.
  502. */
  503. smp_wmb();
  504. for_each_cpu(cpu, mask) {
  505. unsigned long cluster_id = cpu_logical_map(cpu) & ~0xffUL;
  506. u16 tlist;
  507. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  508. gic_send_sgi(cluster_id, tlist, irq);
  509. }
  510. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  511. isb();
  512. }
  513. static void gic_smp_init(void)
  514. {
  515. set_smp_cross_call(gic_raise_softirq);
  516. cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GICV3_STARTING,
  517. "AP_IRQ_GICV3_STARTING", gic_starting_cpu,
  518. NULL);
  519. }
  520. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  521. bool force)
  522. {
  523. unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
  524. void __iomem *reg;
  525. int enabled;
  526. u64 val;
  527. if (gic_irq_in_rdist(d))
  528. return -EINVAL;
  529. /* If interrupt was enabled, disable it first */
  530. enabled = gic_peek_irq(d, GICD_ISENABLER);
  531. if (enabled)
  532. gic_mask_irq(d);
  533. reg = gic_dist_base(d) + GICD_IROUTER + (gic_irq(d) * 8);
  534. val = gic_mpidr_to_affinity(cpu_logical_map(cpu));
  535. gic_write_irouter(val, reg);
  536. /*
  537. * If the interrupt was enabled, enabled it again. Otherwise,
  538. * just wait for the distributor to have digested our changes.
  539. */
  540. if (enabled)
  541. gic_unmask_irq(d);
  542. else
  543. gic_dist_wait_for_rwp();
  544. return IRQ_SET_MASK_OK_DONE;
  545. }
  546. #else
  547. #define gic_set_affinity NULL
  548. #define gic_smp_init() do { } while(0)
  549. #endif
  550. #ifdef CONFIG_CPU_PM
  551. /* Check whether it's single security state view */
  552. static bool gic_dist_security_disabled(void)
  553. {
  554. return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
  555. }
  556. static int gic_cpu_pm_notifier(struct notifier_block *self,
  557. unsigned long cmd, void *v)
  558. {
  559. if (cmd == CPU_PM_EXIT) {
  560. if (gic_dist_security_disabled())
  561. gic_enable_redist(true);
  562. gic_cpu_sys_reg_init();
  563. } else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
  564. gic_write_grpen1(0);
  565. gic_enable_redist(false);
  566. }
  567. return NOTIFY_OK;
  568. }
  569. static struct notifier_block gic_cpu_pm_notifier_block = {
  570. .notifier_call = gic_cpu_pm_notifier,
  571. };
  572. static void gic_cpu_pm_init(void)
  573. {
  574. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  575. }
  576. #else
  577. static inline void gic_cpu_pm_init(void) { }
  578. #endif /* CONFIG_CPU_PM */
  579. static struct irq_chip gic_chip = {
  580. .name = "GICv3",
  581. .irq_mask = gic_mask_irq,
  582. .irq_unmask = gic_unmask_irq,
  583. .irq_eoi = gic_eoi_irq,
  584. .irq_set_type = gic_set_type,
  585. .irq_set_affinity = gic_set_affinity,
  586. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  587. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  588. .flags = IRQCHIP_SET_TYPE_MASKED,
  589. };
  590. static struct irq_chip gic_eoimode1_chip = {
  591. .name = "GICv3",
  592. .irq_mask = gic_eoimode1_mask_irq,
  593. .irq_unmask = gic_unmask_irq,
  594. .irq_eoi = gic_eoimode1_eoi_irq,
  595. .irq_set_type = gic_set_type,
  596. .irq_set_affinity = gic_set_affinity,
  597. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  598. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  599. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  600. .flags = IRQCHIP_SET_TYPE_MASKED,
  601. };
  602. #define GIC_ID_NR (1U << gic_data.rdists.id_bits)
  603. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  604. irq_hw_number_t hw)
  605. {
  606. struct irq_chip *chip = &gic_chip;
  607. if (static_key_true(&supports_deactivate))
  608. chip = &gic_eoimode1_chip;
  609. /* SGIs are private to the core kernel */
  610. if (hw < 16)
  611. return -EPERM;
  612. /* Nothing here */
  613. if (hw >= gic_data.irq_nr && hw < 8192)
  614. return -EPERM;
  615. /* Off limits */
  616. if (hw >= GIC_ID_NR)
  617. return -EPERM;
  618. /* PPIs */
  619. if (hw < 32) {
  620. irq_set_percpu_devid(irq);
  621. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  622. handle_percpu_devid_irq, NULL, NULL);
  623. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  624. }
  625. /* SPIs */
  626. if (hw >= 32 && hw < gic_data.irq_nr) {
  627. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  628. handle_fasteoi_irq, NULL, NULL);
  629. irq_set_probe(irq);
  630. }
  631. /* LPIs */
  632. if (hw >= 8192 && hw < GIC_ID_NR) {
  633. if (!gic_dist_supports_lpis())
  634. return -EPERM;
  635. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  636. handle_fasteoi_irq, NULL, NULL);
  637. }
  638. return 0;
  639. }
  640. static int gic_irq_domain_translate(struct irq_domain *d,
  641. struct irq_fwspec *fwspec,
  642. unsigned long *hwirq,
  643. unsigned int *type)
  644. {
  645. if (is_of_node(fwspec->fwnode)) {
  646. if (fwspec->param_count < 3)
  647. return -EINVAL;
  648. switch (fwspec->param[0]) {
  649. case 0: /* SPI */
  650. *hwirq = fwspec->param[1] + 32;
  651. break;
  652. case 1: /* PPI */
  653. *hwirq = fwspec->param[1] + 16;
  654. break;
  655. case GIC_IRQ_TYPE_LPI: /* LPI */
  656. *hwirq = fwspec->param[1];
  657. break;
  658. default:
  659. return -EINVAL;
  660. }
  661. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  662. return 0;
  663. }
  664. if (is_fwnode_irqchip(fwspec->fwnode)) {
  665. if(fwspec->param_count != 2)
  666. return -EINVAL;
  667. *hwirq = fwspec->param[0];
  668. *type = fwspec->param[1];
  669. return 0;
  670. }
  671. return -EINVAL;
  672. }
  673. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  674. unsigned int nr_irqs, void *arg)
  675. {
  676. int i, ret;
  677. irq_hw_number_t hwirq;
  678. unsigned int type = IRQ_TYPE_NONE;
  679. struct irq_fwspec *fwspec = arg;
  680. ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  681. if (ret)
  682. return ret;
  683. for (i = 0; i < nr_irqs; i++)
  684. gic_irq_domain_map(domain, virq + i, hwirq + i);
  685. return 0;
  686. }
  687. static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  688. unsigned int nr_irqs)
  689. {
  690. int i;
  691. for (i = 0; i < nr_irqs; i++) {
  692. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  693. irq_set_handler(virq + i, NULL);
  694. irq_domain_reset_irq_data(d);
  695. }
  696. }
  697. static int gic_irq_domain_select(struct irq_domain *d,
  698. struct irq_fwspec *fwspec,
  699. enum irq_domain_bus_token bus_token)
  700. {
  701. /* Not for us */
  702. if (fwspec->fwnode != d->fwnode)
  703. return 0;
  704. /* If this is not DT, then we have a single domain */
  705. if (!is_of_node(fwspec->fwnode))
  706. return 1;
  707. /*
  708. * If this is a PPI and we have a 4th (non-null) parameter,
  709. * then we need to match the partition domain.
  710. */
  711. if (fwspec->param_count >= 4 &&
  712. fwspec->param[0] == 1 && fwspec->param[3] != 0)
  713. return d == partition_get_domain(gic_data.ppi_descs[fwspec->param[1]]);
  714. return d == gic_data.domain;
  715. }
  716. static const struct irq_domain_ops gic_irq_domain_ops = {
  717. .translate = gic_irq_domain_translate,
  718. .alloc = gic_irq_domain_alloc,
  719. .free = gic_irq_domain_free,
  720. .select = gic_irq_domain_select,
  721. };
  722. static int partition_domain_translate(struct irq_domain *d,
  723. struct irq_fwspec *fwspec,
  724. unsigned long *hwirq,
  725. unsigned int *type)
  726. {
  727. struct device_node *np;
  728. int ret;
  729. np = of_find_node_by_phandle(fwspec->param[3]);
  730. if (WARN_ON(!np))
  731. return -EINVAL;
  732. ret = partition_translate_id(gic_data.ppi_descs[fwspec->param[1]],
  733. of_node_to_fwnode(np));
  734. if (ret < 0)
  735. return ret;
  736. *hwirq = ret;
  737. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  738. return 0;
  739. }
  740. static const struct irq_domain_ops partition_domain_ops = {
  741. .translate = partition_domain_translate,
  742. .select = gic_irq_domain_select,
  743. };
  744. static void gicv3_enable_quirks(void)
  745. {
  746. #ifdef CONFIG_ARM64
  747. if (cpus_have_cap(ARM64_WORKAROUND_CAVIUM_23154))
  748. static_branch_enable(&is_cavium_thunderx);
  749. #endif
  750. }
  751. static int __init gic_init_bases(void __iomem *dist_base,
  752. struct redist_region *rdist_regs,
  753. u32 nr_redist_regions,
  754. u64 redist_stride,
  755. struct fwnode_handle *handle)
  756. {
  757. struct device_node *node;
  758. u32 typer;
  759. int gic_irqs;
  760. int err;
  761. if (!is_hyp_mode_available())
  762. static_key_slow_dec(&supports_deactivate);
  763. if (static_key_true(&supports_deactivate))
  764. pr_info("GIC: Using split EOI/Deactivate mode\n");
  765. gic_data.fwnode = handle;
  766. gic_data.dist_base = dist_base;
  767. gic_data.redist_regions = rdist_regs;
  768. gic_data.nr_redist_regions = nr_redist_regions;
  769. gic_data.redist_stride = redist_stride;
  770. gicv3_enable_quirks();
  771. /*
  772. * Find out how many interrupts are supported.
  773. * The GIC only supports up to 1020 interrupt sources (SGI+PPI+SPI)
  774. */
  775. typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
  776. gic_data.rdists.id_bits = GICD_TYPER_ID_BITS(typer);
  777. gic_irqs = GICD_TYPER_IRQS(typer);
  778. if (gic_irqs > 1020)
  779. gic_irqs = 1020;
  780. gic_data.irq_nr = gic_irqs;
  781. gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
  782. &gic_data);
  783. gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
  784. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
  785. err = -ENOMEM;
  786. goto out_free;
  787. }
  788. set_handle_irq(gic_handle_irq);
  789. node = to_of_node(handle);
  790. if (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) && gic_dist_supports_lpis() &&
  791. node) /* Temp hack to prevent ITS init for ACPI */
  792. its_init(node, &gic_data.rdists, gic_data.domain);
  793. gic_smp_init();
  794. gic_dist_init();
  795. gic_cpu_init();
  796. gic_cpu_pm_init();
  797. return 0;
  798. out_free:
  799. if (gic_data.domain)
  800. irq_domain_remove(gic_data.domain);
  801. free_percpu(gic_data.rdists.rdist);
  802. return err;
  803. }
  804. static int __init gic_validate_dist_version(void __iomem *dist_base)
  805. {
  806. u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  807. if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
  808. return -ENODEV;
  809. return 0;
  810. }
  811. static int get_cpu_number(struct device_node *dn)
  812. {
  813. const __be32 *cell;
  814. u64 hwid;
  815. int i;
  816. cell = of_get_property(dn, "reg", NULL);
  817. if (!cell)
  818. return -1;
  819. hwid = of_read_number(cell, of_n_addr_cells(dn));
  820. /*
  821. * Non affinity bits must be set to 0 in the DT
  822. */
  823. if (hwid & ~MPIDR_HWID_BITMASK)
  824. return -1;
  825. for (i = 0; i < num_possible_cpus(); i++)
  826. if (cpu_logical_map(i) == hwid)
  827. return i;
  828. return -1;
  829. }
  830. /* Create all possible partitions at boot time */
  831. static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
  832. {
  833. struct device_node *parts_node, *child_part;
  834. int part_idx = 0, i;
  835. int nr_parts;
  836. struct partition_affinity *parts;
  837. parts_node = of_find_node_by_name(gic_node, "ppi-partitions");
  838. if (!parts_node)
  839. return;
  840. nr_parts = of_get_child_count(parts_node);
  841. if (!nr_parts)
  842. return;
  843. parts = kzalloc(sizeof(*parts) * nr_parts, GFP_KERNEL);
  844. if (WARN_ON(!parts))
  845. return;
  846. for_each_child_of_node(parts_node, child_part) {
  847. struct partition_affinity *part;
  848. int n;
  849. part = &parts[part_idx];
  850. part->partition_id = of_node_to_fwnode(child_part);
  851. pr_info("GIC: PPI partition %s[%d] { ",
  852. child_part->name, part_idx);
  853. n = of_property_count_elems_of_size(child_part, "affinity",
  854. sizeof(u32));
  855. WARN_ON(n <= 0);
  856. for (i = 0; i < n; i++) {
  857. int err, cpu;
  858. u32 cpu_phandle;
  859. struct device_node *cpu_node;
  860. err = of_property_read_u32_index(child_part, "affinity",
  861. i, &cpu_phandle);
  862. if (WARN_ON(err))
  863. continue;
  864. cpu_node = of_find_node_by_phandle(cpu_phandle);
  865. if (WARN_ON(!cpu_node))
  866. continue;
  867. cpu = get_cpu_number(cpu_node);
  868. if (WARN_ON(cpu == -1))
  869. continue;
  870. pr_cont("%s[%d] ", cpu_node->full_name, cpu);
  871. cpumask_set_cpu(cpu, &part->mask);
  872. }
  873. pr_cont("}\n");
  874. part_idx++;
  875. }
  876. for (i = 0; i < 16; i++) {
  877. unsigned int irq;
  878. struct partition_desc *desc;
  879. struct irq_fwspec ppi_fwspec = {
  880. .fwnode = gic_data.fwnode,
  881. .param_count = 3,
  882. .param = {
  883. [0] = 1,
  884. [1] = i,
  885. [2] = IRQ_TYPE_NONE,
  886. },
  887. };
  888. irq = irq_create_fwspec_mapping(&ppi_fwspec);
  889. if (WARN_ON(!irq))
  890. continue;
  891. desc = partition_create_desc(gic_data.fwnode, parts, nr_parts,
  892. irq, &partition_domain_ops);
  893. if (WARN_ON(!desc))
  894. continue;
  895. gic_data.ppi_descs[i] = desc;
  896. }
  897. }
  898. static void __init gic_of_setup_kvm_info(struct device_node *node)
  899. {
  900. int ret;
  901. struct resource r;
  902. u32 gicv_idx;
  903. gic_v3_kvm_info.type = GIC_V3;
  904. gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
  905. if (!gic_v3_kvm_info.maint_irq)
  906. return;
  907. if (of_property_read_u32(node, "#redistributor-regions",
  908. &gicv_idx))
  909. gicv_idx = 1;
  910. gicv_idx += 3; /* Also skip GICD, GICC, GICH */
  911. ret = of_address_to_resource(node, gicv_idx, &r);
  912. if (!ret)
  913. gic_v3_kvm_info.vcpu = r;
  914. gic_set_kvm_info(&gic_v3_kvm_info);
  915. }
  916. static int __init gic_of_init(struct device_node *node, struct device_node *parent)
  917. {
  918. void __iomem *dist_base;
  919. struct redist_region *rdist_regs;
  920. u64 redist_stride;
  921. u32 nr_redist_regions;
  922. int err, i;
  923. dist_base = of_iomap(node, 0);
  924. if (!dist_base) {
  925. pr_err("%s: unable to map gic dist registers\n",
  926. node->full_name);
  927. return -ENXIO;
  928. }
  929. err = gic_validate_dist_version(dist_base);
  930. if (err) {
  931. pr_err("%s: no distributor detected, giving up\n",
  932. node->full_name);
  933. goto out_unmap_dist;
  934. }
  935. if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
  936. nr_redist_regions = 1;
  937. rdist_regs = kzalloc(sizeof(*rdist_regs) * nr_redist_regions, GFP_KERNEL);
  938. if (!rdist_regs) {
  939. err = -ENOMEM;
  940. goto out_unmap_dist;
  941. }
  942. for (i = 0; i < nr_redist_regions; i++) {
  943. struct resource res;
  944. int ret;
  945. ret = of_address_to_resource(node, 1 + i, &res);
  946. rdist_regs[i].redist_base = of_iomap(node, 1 + i);
  947. if (ret || !rdist_regs[i].redist_base) {
  948. pr_err("%s: couldn't map region %d\n",
  949. node->full_name, i);
  950. err = -ENODEV;
  951. goto out_unmap_rdist;
  952. }
  953. rdist_regs[i].phys_base = res.start;
  954. }
  955. if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
  956. redist_stride = 0;
  957. err = gic_init_bases(dist_base, rdist_regs, nr_redist_regions,
  958. redist_stride, &node->fwnode);
  959. if (err)
  960. goto out_unmap_rdist;
  961. gic_populate_ppi_partitions(node);
  962. gic_of_setup_kvm_info(node);
  963. return 0;
  964. out_unmap_rdist:
  965. for (i = 0; i < nr_redist_regions; i++)
  966. if (rdist_regs[i].redist_base)
  967. iounmap(rdist_regs[i].redist_base);
  968. kfree(rdist_regs);
  969. out_unmap_dist:
  970. iounmap(dist_base);
  971. return err;
  972. }
  973. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
  974. #ifdef CONFIG_ACPI
  975. static struct
  976. {
  977. void __iomem *dist_base;
  978. struct redist_region *redist_regs;
  979. u32 nr_redist_regions;
  980. bool single_redist;
  981. u32 maint_irq;
  982. int maint_irq_mode;
  983. phys_addr_t vcpu_base;
  984. } acpi_data __initdata;
  985. static void __init
  986. gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
  987. {
  988. static int count = 0;
  989. acpi_data.redist_regs[count].phys_base = phys_base;
  990. acpi_data.redist_regs[count].redist_base = redist_base;
  991. acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
  992. count++;
  993. }
  994. static int __init
  995. gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
  996. const unsigned long end)
  997. {
  998. struct acpi_madt_generic_redistributor *redist =
  999. (struct acpi_madt_generic_redistributor *)header;
  1000. void __iomem *redist_base;
  1001. redist_base = ioremap(redist->base_address, redist->length);
  1002. if (!redist_base) {
  1003. pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
  1004. return -ENOMEM;
  1005. }
  1006. gic_acpi_register_redist(redist->base_address, redist_base);
  1007. return 0;
  1008. }
  1009. static int __init
  1010. gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
  1011. const unsigned long end)
  1012. {
  1013. struct acpi_madt_generic_interrupt *gicc =
  1014. (struct acpi_madt_generic_interrupt *)header;
  1015. u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1016. u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
  1017. void __iomem *redist_base;
  1018. redist_base = ioremap(gicc->gicr_base_address, size);
  1019. if (!redist_base)
  1020. return -ENOMEM;
  1021. gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
  1022. return 0;
  1023. }
  1024. static int __init gic_acpi_collect_gicr_base(void)
  1025. {
  1026. acpi_tbl_entry_handler redist_parser;
  1027. enum acpi_madt_type type;
  1028. if (acpi_data.single_redist) {
  1029. type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
  1030. redist_parser = gic_acpi_parse_madt_gicc;
  1031. } else {
  1032. type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
  1033. redist_parser = gic_acpi_parse_madt_redist;
  1034. }
  1035. /* Collect redistributor base addresses in GICR entries */
  1036. if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
  1037. return 0;
  1038. pr_info("No valid GICR entries exist\n");
  1039. return -ENODEV;
  1040. }
  1041. static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
  1042. const unsigned long end)
  1043. {
  1044. /* Subtable presence means that redist exists, that's it */
  1045. return 0;
  1046. }
  1047. static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
  1048. const unsigned long end)
  1049. {
  1050. struct acpi_madt_generic_interrupt *gicc =
  1051. (struct acpi_madt_generic_interrupt *)header;
  1052. /*
  1053. * If GICC is enabled and has valid gicr base address, then it means
  1054. * GICR base is presented via GICC
  1055. */
  1056. if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
  1057. return 0;
  1058. return -ENODEV;
  1059. }
  1060. static int __init gic_acpi_count_gicr_regions(void)
  1061. {
  1062. int count;
  1063. /*
  1064. * Count how many redistributor regions we have. It is not allowed
  1065. * to mix redistributor description, GICR and GICC subtables have to be
  1066. * mutually exclusive.
  1067. */
  1068. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
  1069. gic_acpi_match_gicr, 0);
  1070. if (count > 0) {
  1071. acpi_data.single_redist = false;
  1072. return count;
  1073. }
  1074. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1075. gic_acpi_match_gicc, 0);
  1076. if (count > 0)
  1077. acpi_data.single_redist = true;
  1078. return count;
  1079. }
  1080. static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
  1081. struct acpi_probe_entry *ape)
  1082. {
  1083. struct acpi_madt_generic_distributor *dist;
  1084. int count;
  1085. dist = (struct acpi_madt_generic_distributor *)header;
  1086. if (dist->version != ape->driver_data)
  1087. return false;
  1088. /* We need to do that exercise anyway, the sooner the better */
  1089. count = gic_acpi_count_gicr_regions();
  1090. if (count <= 0)
  1091. return false;
  1092. acpi_data.nr_redist_regions = count;
  1093. return true;
  1094. }
  1095. static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
  1096. const unsigned long end)
  1097. {
  1098. struct acpi_madt_generic_interrupt *gicc =
  1099. (struct acpi_madt_generic_interrupt *)header;
  1100. int maint_irq_mode;
  1101. static int first_madt = true;
  1102. /* Skip unusable CPUs */
  1103. if (!(gicc->flags & ACPI_MADT_ENABLED))
  1104. return 0;
  1105. maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
  1106. ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
  1107. if (first_madt) {
  1108. first_madt = false;
  1109. acpi_data.maint_irq = gicc->vgic_interrupt;
  1110. acpi_data.maint_irq_mode = maint_irq_mode;
  1111. acpi_data.vcpu_base = gicc->gicv_base_address;
  1112. return 0;
  1113. }
  1114. /*
  1115. * The maintenance interrupt and GICV should be the same for every CPU
  1116. */
  1117. if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
  1118. (acpi_data.maint_irq_mode != maint_irq_mode) ||
  1119. (acpi_data.vcpu_base != gicc->gicv_base_address))
  1120. return -EINVAL;
  1121. return 0;
  1122. }
  1123. static bool __init gic_acpi_collect_virt_info(void)
  1124. {
  1125. int count;
  1126. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1127. gic_acpi_parse_virt_madt_gicc, 0);
  1128. return (count > 0);
  1129. }
  1130. #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
  1131. #define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
  1132. #define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
  1133. static void __init gic_acpi_setup_kvm_info(void)
  1134. {
  1135. int irq;
  1136. if (!gic_acpi_collect_virt_info()) {
  1137. pr_warn("Unable to get hardware information used for virtualization\n");
  1138. return;
  1139. }
  1140. gic_v3_kvm_info.type = GIC_V3;
  1141. irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
  1142. acpi_data.maint_irq_mode,
  1143. ACPI_ACTIVE_HIGH);
  1144. if (irq <= 0)
  1145. return;
  1146. gic_v3_kvm_info.maint_irq = irq;
  1147. if (acpi_data.vcpu_base) {
  1148. struct resource *vcpu = &gic_v3_kvm_info.vcpu;
  1149. vcpu->flags = IORESOURCE_MEM;
  1150. vcpu->start = acpi_data.vcpu_base;
  1151. vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
  1152. }
  1153. gic_set_kvm_info(&gic_v3_kvm_info);
  1154. }
  1155. static int __init
  1156. gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
  1157. {
  1158. struct acpi_madt_generic_distributor *dist;
  1159. struct fwnode_handle *domain_handle;
  1160. size_t size;
  1161. int i, err;
  1162. /* Get distributor base address */
  1163. dist = (struct acpi_madt_generic_distributor *)header;
  1164. acpi_data.dist_base = ioremap(dist->base_address,
  1165. ACPI_GICV3_DIST_MEM_SIZE);
  1166. if (!acpi_data.dist_base) {
  1167. pr_err("Unable to map GICD registers\n");
  1168. return -ENOMEM;
  1169. }
  1170. err = gic_validate_dist_version(acpi_data.dist_base);
  1171. if (err) {
  1172. pr_err("No distributor detected at @%p, giving up",
  1173. acpi_data.dist_base);
  1174. goto out_dist_unmap;
  1175. }
  1176. size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
  1177. acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
  1178. if (!acpi_data.redist_regs) {
  1179. err = -ENOMEM;
  1180. goto out_dist_unmap;
  1181. }
  1182. err = gic_acpi_collect_gicr_base();
  1183. if (err)
  1184. goto out_redist_unmap;
  1185. domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
  1186. if (!domain_handle) {
  1187. err = -ENOMEM;
  1188. goto out_redist_unmap;
  1189. }
  1190. err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
  1191. acpi_data.nr_redist_regions, 0, domain_handle);
  1192. if (err)
  1193. goto out_fwhandle_free;
  1194. acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
  1195. gic_acpi_setup_kvm_info();
  1196. return 0;
  1197. out_fwhandle_free:
  1198. irq_domain_free_fwnode(domain_handle);
  1199. out_redist_unmap:
  1200. for (i = 0; i < acpi_data.nr_redist_regions; i++)
  1201. if (acpi_data.redist_regs[i].redist_base)
  1202. iounmap(acpi_data.redist_regs[i].redist_base);
  1203. kfree(acpi_data.redist_regs);
  1204. out_dist_unmap:
  1205. iounmap(acpi_data.dist_base);
  1206. return err;
  1207. }
  1208. IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1209. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
  1210. gic_acpi_init);
  1211. IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1212. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
  1213. gic_acpi_init);
  1214. IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1215. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
  1216. gic_acpi_init);
  1217. #endif