irq-gic-v3.c 35 KB

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