irq-gic-v3.c 40 KB

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