irq-gic-v3.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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. u32 maint_irq;
  1109. int maint_irq_mode;
  1110. phys_addr_t vcpu_base;
  1111. } acpi_data __initdata;
  1112. static void __init
  1113. gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
  1114. {
  1115. static int count = 0;
  1116. acpi_data.redist_regs[count].phys_base = phys_base;
  1117. acpi_data.redist_regs[count].redist_base = redist_base;
  1118. acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
  1119. count++;
  1120. }
  1121. static int __init
  1122. gic_acpi_parse_madt_redist(struct acpi_subtable_header *header,
  1123. const unsigned long end)
  1124. {
  1125. struct acpi_madt_generic_redistributor *redist =
  1126. (struct acpi_madt_generic_redistributor *)header;
  1127. void __iomem *redist_base;
  1128. redist_base = ioremap(redist->base_address, redist->length);
  1129. if (!redist_base) {
  1130. pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
  1131. return -ENOMEM;
  1132. }
  1133. gic_acpi_register_redist(redist->base_address, redist_base);
  1134. return 0;
  1135. }
  1136. static int __init
  1137. gic_acpi_parse_madt_gicc(struct acpi_subtable_header *header,
  1138. const unsigned long end)
  1139. {
  1140. struct acpi_madt_generic_interrupt *gicc =
  1141. (struct acpi_madt_generic_interrupt *)header;
  1142. u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1143. u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
  1144. void __iomem *redist_base;
  1145. /* GICC entry which has !ACPI_MADT_ENABLED is not unusable so skip */
  1146. if (!(gicc->flags & ACPI_MADT_ENABLED))
  1147. return 0;
  1148. redist_base = ioremap(gicc->gicr_base_address, size);
  1149. if (!redist_base)
  1150. return -ENOMEM;
  1151. gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
  1152. return 0;
  1153. }
  1154. static int __init gic_acpi_collect_gicr_base(void)
  1155. {
  1156. acpi_tbl_entry_handler redist_parser;
  1157. enum acpi_madt_type type;
  1158. if (acpi_data.single_redist) {
  1159. type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
  1160. redist_parser = gic_acpi_parse_madt_gicc;
  1161. } else {
  1162. type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
  1163. redist_parser = gic_acpi_parse_madt_redist;
  1164. }
  1165. /* Collect redistributor base addresses in GICR entries */
  1166. if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
  1167. return 0;
  1168. pr_info("No valid GICR entries exist\n");
  1169. return -ENODEV;
  1170. }
  1171. static int __init gic_acpi_match_gicr(struct acpi_subtable_header *header,
  1172. const unsigned long end)
  1173. {
  1174. /* Subtable presence means that redist exists, that's it */
  1175. return 0;
  1176. }
  1177. static int __init gic_acpi_match_gicc(struct acpi_subtable_header *header,
  1178. const unsigned long end)
  1179. {
  1180. struct acpi_madt_generic_interrupt *gicc =
  1181. (struct acpi_madt_generic_interrupt *)header;
  1182. /*
  1183. * If GICC is enabled and has valid gicr base address, then it means
  1184. * GICR base is presented via GICC
  1185. */
  1186. if ((gicc->flags & ACPI_MADT_ENABLED) && gicc->gicr_base_address)
  1187. return 0;
  1188. /*
  1189. * It's perfectly valid firmware can pass disabled GICC entry, driver
  1190. * should not treat as errors, skip the entry instead of probe fail.
  1191. */
  1192. if (!(gicc->flags & ACPI_MADT_ENABLED))
  1193. return 0;
  1194. return -ENODEV;
  1195. }
  1196. static int __init gic_acpi_count_gicr_regions(void)
  1197. {
  1198. int count;
  1199. /*
  1200. * Count how many redistributor regions we have. It is not allowed
  1201. * to mix redistributor description, GICR and GICC subtables have to be
  1202. * mutually exclusive.
  1203. */
  1204. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
  1205. gic_acpi_match_gicr, 0);
  1206. if (count > 0) {
  1207. acpi_data.single_redist = false;
  1208. return count;
  1209. }
  1210. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1211. gic_acpi_match_gicc, 0);
  1212. if (count > 0)
  1213. acpi_data.single_redist = true;
  1214. return count;
  1215. }
  1216. static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
  1217. struct acpi_probe_entry *ape)
  1218. {
  1219. struct acpi_madt_generic_distributor *dist;
  1220. int count;
  1221. dist = (struct acpi_madt_generic_distributor *)header;
  1222. if (dist->version != ape->driver_data)
  1223. return false;
  1224. /* We need to do that exercise anyway, the sooner the better */
  1225. count = gic_acpi_count_gicr_regions();
  1226. if (count <= 0)
  1227. return false;
  1228. acpi_data.nr_redist_regions = count;
  1229. return true;
  1230. }
  1231. static int __init gic_acpi_parse_virt_madt_gicc(struct acpi_subtable_header *header,
  1232. const unsigned long end)
  1233. {
  1234. struct acpi_madt_generic_interrupt *gicc =
  1235. (struct acpi_madt_generic_interrupt *)header;
  1236. int maint_irq_mode;
  1237. static int first_madt = true;
  1238. /* Skip unusable CPUs */
  1239. if (!(gicc->flags & ACPI_MADT_ENABLED))
  1240. return 0;
  1241. maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
  1242. ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
  1243. if (first_madt) {
  1244. first_madt = false;
  1245. acpi_data.maint_irq = gicc->vgic_interrupt;
  1246. acpi_data.maint_irq_mode = maint_irq_mode;
  1247. acpi_data.vcpu_base = gicc->gicv_base_address;
  1248. return 0;
  1249. }
  1250. /*
  1251. * The maintenance interrupt and GICV should be the same for every CPU
  1252. */
  1253. if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
  1254. (acpi_data.maint_irq_mode != maint_irq_mode) ||
  1255. (acpi_data.vcpu_base != gicc->gicv_base_address))
  1256. return -EINVAL;
  1257. return 0;
  1258. }
  1259. static bool __init gic_acpi_collect_virt_info(void)
  1260. {
  1261. int count;
  1262. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  1263. gic_acpi_parse_virt_madt_gicc, 0);
  1264. return (count > 0);
  1265. }
  1266. #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
  1267. #define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
  1268. #define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
  1269. static void __init gic_acpi_setup_kvm_info(void)
  1270. {
  1271. int irq;
  1272. if (!gic_acpi_collect_virt_info()) {
  1273. pr_warn("Unable to get hardware information used for virtualization\n");
  1274. return;
  1275. }
  1276. gic_v3_kvm_info.type = GIC_V3;
  1277. irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
  1278. acpi_data.maint_irq_mode,
  1279. ACPI_ACTIVE_HIGH);
  1280. if (irq <= 0)
  1281. return;
  1282. gic_v3_kvm_info.maint_irq = irq;
  1283. if (acpi_data.vcpu_base) {
  1284. struct resource *vcpu = &gic_v3_kvm_info.vcpu;
  1285. vcpu->flags = IORESOURCE_MEM;
  1286. vcpu->start = acpi_data.vcpu_base;
  1287. vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
  1288. }
  1289. gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
  1290. gic_set_kvm_info(&gic_v3_kvm_info);
  1291. }
  1292. static int __init
  1293. gic_acpi_init(struct acpi_subtable_header *header, const unsigned long end)
  1294. {
  1295. struct acpi_madt_generic_distributor *dist;
  1296. struct fwnode_handle *domain_handle;
  1297. size_t size;
  1298. int i, err;
  1299. /* Get distributor base address */
  1300. dist = (struct acpi_madt_generic_distributor *)header;
  1301. acpi_data.dist_base = ioremap(dist->base_address,
  1302. ACPI_GICV3_DIST_MEM_SIZE);
  1303. if (!acpi_data.dist_base) {
  1304. pr_err("Unable to map GICD registers\n");
  1305. return -ENOMEM;
  1306. }
  1307. err = gic_validate_dist_version(acpi_data.dist_base);
  1308. if (err) {
  1309. pr_err("No distributor detected at @%p, giving up\n",
  1310. acpi_data.dist_base);
  1311. goto out_dist_unmap;
  1312. }
  1313. size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
  1314. acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
  1315. if (!acpi_data.redist_regs) {
  1316. err = -ENOMEM;
  1317. goto out_dist_unmap;
  1318. }
  1319. err = gic_acpi_collect_gicr_base();
  1320. if (err)
  1321. goto out_redist_unmap;
  1322. domain_handle = irq_domain_alloc_fwnode(acpi_data.dist_base);
  1323. if (!domain_handle) {
  1324. err = -ENOMEM;
  1325. goto out_redist_unmap;
  1326. }
  1327. err = gic_init_bases(acpi_data.dist_base, acpi_data.redist_regs,
  1328. acpi_data.nr_redist_regions, 0, domain_handle);
  1329. if (err)
  1330. goto out_fwhandle_free;
  1331. acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, domain_handle);
  1332. if (static_branch_likely(&supports_deactivate_key))
  1333. gic_acpi_setup_kvm_info();
  1334. return 0;
  1335. out_fwhandle_free:
  1336. irq_domain_free_fwnode(domain_handle);
  1337. out_redist_unmap:
  1338. for (i = 0; i < acpi_data.nr_redist_regions; i++)
  1339. if (acpi_data.redist_regs[i].redist_base)
  1340. iounmap(acpi_data.redist_regs[i].redist_base);
  1341. kfree(acpi_data.redist_regs);
  1342. out_dist_unmap:
  1343. iounmap(acpi_data.dist_base);
  1344. return err;
  1345. }
  1346. IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1347. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
  1348. gic_acpi_init);
  1349. IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1350. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
  1351. gic_acpi_init);
  1352. IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  1353. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
  1354. gic_acpi_init);
  1355. #endif