irq-gic-v3.c 36 KB

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