irq-gic-v3.c 35 KB

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