irq-gic-v3.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #define pr_fmt(fmt) "GICv3: " fmt
  7. #include <linux/acpi.h>
  8. #include <linux/cpu.h>
  9. #include <linux/cpu_pm.h>
  10. #include <linux/delay.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kstrtox.h>
  15. #include <linux/of.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_irq.h>
  18. #include <linux/percpu.h>
  19. #include <linux/refcount.h>
  20. #include <linux/slab.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/irqchip.h>
  23. #include <linux/irqchip/arm-gic-common.h>
  24. #include <linux/irqchip/arm-gic-v3.h>
  25. #include <linux/irqchip/arm-gic-v3-prio.h>
  26. #include <linux/bitfield.h>
  27. #include <linux/bits.h>
  28. #include <linux/arm-smccc.h>
  29. #include <asm/cputype.h>
  30. #include <asm/exception.h>
  31. #include <asm/smp_plat.h>
  32. #include <asm/virt.h>
  33. #include "irq-gic-common.h"
  34. static u8 dist_prio_irq __ro_after_init = GICV3_PRIO_IRQ;
  35. static u8 dist_prio_nmi __ro_after_init = GICV3_PRIO_NMI;
  36. #define FLAGS_WORKAROUND_GICR_WAKER_MSM8996 (1ULL << 0)
  37. #define FLAGS_WORKAROUND_CAVIUM_ERRATUM_38539 (1ULL << 1)
  38. #define FLAGS_WORKAROUND_ASR_ERRATUM_8601001 (1ULL << 2)
  39. #define FLAGS_WORKAROUND_INSECURE (1ULL << 3)
  40. static struct cpumask broken_rdists __read_mostly __maybe_unused;
  41. struct redist_region {
  42. void __iomem *redist_base;
  43. phys_addr_t phys_base;
  44. bool single_redist;
  45. };
  46. struct gic_chip_data {
  47. struct fwnode_handle *fwnode;
  48. phys_addr_t dist_phys_base;
  49. void __iomem *dist_base;
  50. struct redist_region *redist_regions;
  51. struct rdists rdists;
  52. struct irq_domain *domain;
  53. u64 redist_stride;
  54. u32 nr_redist_regions;
  55. u64 flags;
  56. bool has_rss;
  57. unsigned int ppi_nr;
  58. struct partition_affinity *parts;
  59. unsigned int nr_parts;
  60. };
  61. struct partition_affinity {
  62. cpumask_t mask;
  63. struct fwnode_handle *partition_id;
  64. };
  65. #define T241_CHIPS_MAX 4
  66. static void __iomem *t241_dist_base_alias[T241_CHIPS_MAX] __read_mostly;
  67. static DEFINE_STATIC_KEY_FALSE(gic_nvidia_t241_erratum);
  68. static DEFINE_STATIC_KEY_FALSE(gic_arm64_2941627_erratum);
  69. static struct gic_chip_data gic_data __read_mostly;
  70. static DEFINE_STATIC_KEY_TRUE(supports_deactivate_key);
  71. #define GIC_ID_NR (1U << GICD_TYPER_ID_BITS(gic_data.rdists.gicd_typer))
  72. #define GIC_LINE_NR min(GICD_TYPER_SPIS(gic_data.rdists.gicd_typer), 1020U)
  73. #define GIC_ESPI_NR GICD_TYPER_ESPIS(gic_data.rdists.gicd_typer)
  74. static bool nmi_support_forbidden;
  75. /*
  76. * There are 16 SGIs, though we only actually use 8 in Linux. The other 8 SGIs
  77. * are potentially stolen by the secure side. Some code, especially code dealing
  78. * with hwirq IDs, is simplified by accounting for all 16.
  79. */
  80. #define SGI_NR 16
  81. /*
  82. * The behaviours of RPR and PMR registers differ depending on the value of
  83. * SCR_EL3.FIQ, and the behaviour of non-secure priority registers of the
  84. * distributor and redistributors depends on whether security is enabled in the
  85. * GIC.
  86. *
  87. * When security is enabled, non-secure priority values from the (re)distributor
  88. * are presented to the GIC CPUIF as follow:
  89. * (GIC_(R)DIST_PRI[irq] >> 1) | 0x80;
  90. *
  91. * If SCR_EL3.FIQ == 1, the values written to/read from PMR and RPR at non-secure
  92. * EL1 are subject to a similar operation thus matching the priorities presented
  93. * from the (re)distributor when security is enabled. When SCR_EL3.FIQ == 0,
  94. * these values are unchanged by the GIC.
  95. *
  96. * see GICv3/GICv4 Architecture Specification (IHI0069D):
  97. * - section 4.8.1 Non-secure accesses to register fields for Secure interrupt
  98. * priorities.
  99. * - Figure 4-7 Secure read of the priority field for a Non-secure Group 1
  100. * interrupt.
  101. */
  102. static DEFINE_STATIC_KEY_FALSE(supports_pseudo_nmis);
  103. static u32 gic_get_pribits(void)
  104. {
  105. u32 pribits;
  106. pribits = gic_read_ctlr();
  107. pribits &= ICC_CTLR_EL1_PRI_BITS_MASK;
  108. pribits >>= ICC_CTLR_EL1_PRI_BITS_SHIFT;
  109. pribits++;
  110. return pribits;
  111. }
  112. static bool gic_has_group0(void)
  113. {
  114. u32 val;
  115. u32 old_pmr;
  116. old_pmr = gic_read_pmr();
  117. /*
  118. * Let's find out if Group0 is under control of EL3 or not by
  119. * setting the highest possible, non-zero priority in PMR.
  120. *
  121. * If SCR_EL3.FIQ is set, the priority gets shifted down in
  122. * order for the CPU interface to set bit 7, and keep the
  123. * actual priority in the non-secure range. In the process, it
  124. * looses the least significant bit and the actual priority
  125. * becomes 0x80. Reading it back returns 0, indicating that
  126. * we're don't have access to Group0.
  127. */
  128. gic_write_pmr(BIT(8 - gic_get_pribits()));
  129. val = gic_read_pmr();
  130. gic_write_pmr(old_pmr);
  131. return val != 0;
  132. }
  133. static inline bool gic_dist_security_disabled(void)
  134. {
  135. return readl_relaxed(gic_data.dist_base + GICD_CTLR) & GICD_CTLR_DS;
  136. }
  137. static bool cpus_have_security_disabled __ro_after_init;
  138. static bool cpus_have_group0 __ro_after_init;
  139. static void __init gic_prio_init(void)
  140. {
  141. bool ds;
  142. cpus_have_group0 = gic_has_group0();
  143. ds = gic_dist_security_disabled();
  144. if ((gic_data.flags & FLAGS_WORKAROUND_INSECURE) && !ds) {
  145. if (cpus_have_group0) {
  146. u32 val;
  147. val = readl_relaxed(gic_data.dist_base + GICD_CTLR);
  148. val |= GICD_CTLR_DS;
  149. writel_relaxed(val, gic_data.dist_base + GICD_CTLR);
  150. ds = gic_dist_security_disabled();
  151. if (ds)
  152. pr_warn("Broken GIC integration, security disabled\n");
  153. } else {
  154. pr_warn("Broken GIC integration, pNMI forbidden\n");
  155. nmi_support_forbidden = true;
  156. }
  157. }
  158. cpus_have_security_disabled = ds;
  159. /*
  160. * How priority values are used by the GIC depends on two things:
  161. * the security state of the GIC (controlled by the GICD_CTLR.DS bit)
  162. * and if Group 0 interrupts can be delivered to Linux in the non-secure
  163. * world as FIQs (controlled by the SCR_EL3.FIQ bit). These affect the
  164. * way priorities are presented in ICC_PMR_EL1 and in the distributor:
  165. *
  166. * GICD_CTLR.DS | SCR_EL3.FIQ | ICC_PMR_EL1 | Distributor
  167. * -------------------------------------------------------
  168. * 1 | - | unchanged | unchanged
  169. * -------------------------------------------------------
  170. * 0 | 1 | non-secure | non-secure
  171. * -------------------------------------------------------
  172. * 0 | 0 | unchanged | non-secure
  173. *
  174. * In the non-secure view reads and writes are modified:
  175. *
  176. * - A value written is right-shifted by one and the MSB is set,
  177. * forcing the priority into the non-secure range.
  178. *
  179. * - A value read is left-shifted by one.
  180. *
  181. * In the first two cases, where ICC_PMR_EL1 and the interrupt priority
  182. * are both either modified or unchanged, we can use the same set of
  183. * priorities.
  184. *
  185. * In the last case, where only the interrupt priorities are modified to
  186. * be in the non-secure range, we program the non-secure values into
  187. * the distributor to match the PMR values we want.
  188. */
  189. if (cpus_have_group0 && !cpus_have_security_disabled) {
  190. dist_prio_irq = __gicv3_prio_to_ns(dist_prio_irq);
  191. dist_prio_nmi = __gicv3_prio_to_ns(dist_prio_nmi);
  192. }
  193. pr_info("GICD_CTLR.DS=%d, SCR_EL3.FIQ=%d\n",
  194. cpus_have_security_disabled,
  195. !cpus_have_group0);
  196. }
  197. static struct gic_kvm_info gic_v3_kvm_info __initdata;
  198. static DEFINE_PER_CPU(bool, has_rss);
  199. #define MPIDR_RS(mpidr) (((mpidr) & 0xF0UL) >> 4)
  200. #define gic_data_rdist() (this_cpu_ptr(gic_data.rdists.rdist))
  201. #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
  202. #define gic_data_rdist_sgi_base() (gic_data_rdist_rd_base() + SZ_64K)
  203. /* Our default, arbitrary priority value. Linux only uses one anyway. */
  204. #define DEFAULT_PMR_VALUE 0xf0
  205. enum gic_intid_range {
  206. SGI_RANGE,
  207. PPI_RANGE,
  208. SPI_RANGE,
  209. EPPI_RANGE,
  210. ESPI_RANGE,
  211. LPI_RANGE,
  212. __INVALID_RANGE__
  213. };
  214. static enum gic_intid_range __get_intid_range(irq_hw_number_t hwirq)
  215. {
  216. switch (hwirq) {
  217. case 0 ... 15:
  218. return SGI_RANGE;
  219. case 16 ... 31:
  220. return PPI_RANGE;
  221. case 32 ... 1019:
  222. return SPI_RANGE;
  223. case EPPI_BASE_INTID ... (EPPI_BASE_INTID + 63):
  224. return EPPI_RANGE;
  225. case ESPI_BASE_INTID ... (ESPI_BASE_INTID + 1023):
  226. return ESPI_RANGE;
  227. case 8192 ... GENMASK(23, 0):
  228. return LPI_RANGE;
  229. default:
  230. return __INVALID_RANGE__;
  231. }
  232. }
  233. static enum gic_intid_range get_intid_range(struct irq_data *d)
  234. {
  235. return __get_intid_range(d->hwirq);
  236. }
  237. static inline bool gic_irq_in_rdist(struct irq_data *d)
  238. {
  239. switch (get_intid_range(d)) {
  240. case SGI_RANGE:
  241. case PPI_RANGE:
  242. case EPPI_RANGE:
  243. return true;
  244. default:
  245. return false;
  246. }
  247. }
  248. static inline void __iomem *gic_dist_base_alias(struct irq_data *d)
  249. {
  250. if (static_branch_unlikely(&gic_nvidia_t241_erratum)) {
  251. irq_hw_number_t hwirq = irqd_to_hwirq(d);
  252. u32 chip;
  253. /*
  254. * For the erratum T241-FABRIC-4, read accesses to GICD_In{E}
  255. * registers are directed to the chip that owns the SPI. The
  256. * the alias region can also be used for writes to the
  257. * GICD_In{E} except GICD_ICENABLERn. Each chip has support
  258. * for 320 {E}SPIs. Mappings for all 4 chips:
  259. * Chip0 = 32-351
  260. * Chip1 = 352-671
  261. * Chip2 = 672-991
  262. * Chip3 = 4096-4415
  263. */
  264. switch (__get_intid_range(hwirq)) {
  265. case SPI_RANGE:
  266. chip = (hwirq - 32) / 320;
  267. break;
  268. case ESPI_RANGE:
  269. chip = 3;
  270. break;
  271. default:
  272. unreachable();
  273. }
  274. return t241_dist_base_alias[chip];
  275. }
  276. return gic_data.dist_base;
  277. }
  278. static inline void __iomem *gic_dist_base(struct irq_data *d)
  279. {
  280. switch (get_intid_range(d)) {
  281. case SGI_RANGE:
  282. case PPI_RANGE:
  283. case EPPI_RANGE:
  284. /* SGI+PPI -> SGI_base for this CPU */
  285. return gic_data_rdist_sgi_base();
  286. case SPI_RANGE:
  287. case ESPI_RANGE:
  288. /* SPI -> dist_base */
  289. return gic_data.dist_base;
  290. default:
  291. return NULL;
  292. }
  293. }
  294. static void gic_do_wait_for_rwp(void __iomem *base, u32 bit)
  295. {
  296. u32 val;
  297. int ret;
  298. ret = readl_relaxed_poll_timeout_atomic(base + GICD_CTLR, val, !(val & bit),
  299. 1, USEC_PER_SEC);
  300. if (ret == -ETIMEDOUT)
  301. pr_err_ratelimited("RWP timeout, gone fishing\n");
  302. }
  303. /* Wait for completion of a distributor change */
  304. static void gic_dist_wait_for_rwp(void)
  305. {
  306. gic_do_wait_for_rwp(gic_data.dist_base, GICD_CTLR_RWP);
  307. }
  308. /* Wait for completion of a redistributor change */
  309. static void gic_redist_wait_for_rwp(void)
  310. {
  311. gic_do_wait_for_rwp(gic_data_rdist_rd_base(), GICR_CTLR_RWP);
  312. }
  313. static void gic_enable_redist(bool enable)
  314. {
  315. void __iomem *rbase;
  316. u32 val;
  317. int ret;
  318. if (gic_data.flags & FLAGS_WORKAROUND_GICR_WAKER_MSM8996)
  319. return;
  320. rbase = gic_data_rdist_rd_base();
  321. val = readl_relaxed(rbase + GICR_WAKER);
  322. if (enable)
  323. /* Wake up this CPU redistributor */
  324. val &= ~GICR_WAKER_ProcessorSleep;
  325. else
  326. val |= GICR_WAKER_ProcessorSleep;
  327. writel_relaxed(val, rbase + GICR_WAKER);
  328. if (!enable) { /* Check that GICR_WAKER is writeable */
  329. val = readl_relaxed(rbase + GICR_WAKER);
  330. if (!(val & GICR_WAKER_ProcessorSleep))
  331. return; /* No PM support in this redistributor */
  332. }
  333. ret = readl_relaxed_poll_timeout_atomic(rbase + GICR_WAKER, val,
  334. enable ^ (bool)(val & GICR_WAKER_ChildrenAsleep),
  335. 1, USEC_PER_SEC);
  336. if (ret == -ETIMEDOUT) {
  337. pr_err_ratelimited("redistributor failed to %s...\n",
  338. enable ? "wakeup" : "sleep");
  339. }
  340. }
  341. /*
  342. * Routines to disable, enable, EOI and route interrupts
  343. */
  344. static u32 convert_offset_index(struct irq_data *d, u32 offset, u32 *index)
  345. {
  346. switch (get_intid_range(d)) {
  347. case SGI_RANGE:
  348. case PPI_RANGE:
  349. case SPI_RANGE:
  350. *index = d->hwirq;
  351. return offset;
  352. case EPPI_RANGE:
  353. /*
  354. * Contrary to the ESPI range, the EPPI range is contiguous
  355. * to the PPI range in the registers, so let's adjust the
  356. * displacement accordingly. Consistency is overrated.
  357. */
  358. *index = d->hwirq - EPPI_BASE_INTID + 32;
  359. return offset;
  360. case ESPI_RANGE:
  361. *index = d->hwirq - ESPI_BASE_INTID;
  362. switch (offset) {
  363. case GICD_ISENABLER:
  364. return GICD_ISENABLERnE;
  365. case GICD_ICENABLER:
  366. return GICD_ICENABLERnE;
  367. case GICD_ISPENDR:
  368. return GICD_ISPENDRnE;
  369. case GICD_ICPENDR:
  370. return GICD_ICPENDRnE;
  371. case GICD_ISACTIVER:
  372. return GICD_ISACTIVERnE;
  373. case GICD_ICACTIVER:
  374. return GICD_ICACTIVERnE;
  375. case GICD_IPRIORITYR:
  376. return GICD_IPRIORITYRnE;
  377. case GICD_ICFGR:
  378. return GICD_ICFGRnE;
  379. case GICD_IROUTER:
  380. return GICD_IROUTERnE;
  381. default:
  382. break;
  383. }
  384. break;
  385. default:
  386. break;
  387. }
  388. WARN_ON(1);
  389. *index = d->hwirq;
  390. return offset;
  391. }
  392. static int gic_peek_irq(struct irq_data *d, u32 offset)
  393. {
  394. void __iomem *base;
  395. u32 index, mask;
  396. offset = convert_offset_index(d, offset, &index);
  397. mask = 1 << (index % 32);
  398. if (gic_irq_in_rdist(d))
  399. base = gic_data_rdist_sgi_base();
  400. else
  401. base = gic_dist_base_alias(d);
  402. return !!(readl_relaxed(base + offset + (index / 32) * 4) & mask);
  403. }
  404. static void gic_poke_irq(struct irq_data *d, u32 offset)
  405. {
  406. void __iomem *base;
  407. u32 index, mask;
  408. offset = convert_offset_index(d, offset, &index);
  409. mask = 1 << (index % 32);
  410. if (gic_irq_in_rdist(d))
  411. base = gic_data_rdist_sgi_base();
  412. else
  413. base = gic_data.dist_base;
  414. writel_relaxed(mask, base + offset + (index / 32) * 4);
  415. }
  416. static void gic_mask_irq(struct irq_data *d)
  417. {
  418. gic_poke_irq(d, GICD_ICENABLER);
  419. if (gic_irq_in_rdist(d))
  420. gic_redist_wait_for_rwp();
  421. else
  422. gic_dist_wait_for_rwp();
  423. }
  424. static void gic_eoimode1_mask_irq(struct irq_data *d)
  425. {
  426. gic_mask_irq(d);
  427. /*
  428. * When masking a forwarded interrupt, make sure it is
  429. * deactivated as well.
  430. *
  431. * This ensures that an interrupt that is getting
  432. * disabled/masked will not get "stuck", because there is
  433. * noone to deactivate it (guest is being terminated).
  434. */
  435. if (irqd_is_forwarded_to_vcpu(d))
  436. gic_poke_irq(d, GICD_ICACTIVER);
  437. }
  438. static void gic_unmask_irq(struct irq_data *d)
  439. {
  440. gic_poke_irq(d, GICD_ISENABLER);
  441. }
  442. static inline bool gic_supports_nmi(void)
  443. {
  444. return IS_ENABLED(CONFIG_ARM64_PSEUDO_NMI) &&
  445. static_branch_likely(&supports_pseudo_nmis);
  446. }
  447. static int gic_irq_set_irqchip_state(struct irq_data *d,
  448. enum irqchip_irq_state which, bool val)
  449. {
  450. u32 reg;
  451. if (d->hwirq >= 8192) /* SGI/PPI/SPI only */
  452. return -EINVAL;
  453. switch (which) {
  454. case IRQCHIP_STATE_PENDING:
  455. reg = val ? GICD_ISPENDR : GICD_ICPENDR;
  456. break;
  457. case IRQCHIP_STATE_ACTIVE:
  458. reg = val ? GICD_ISACTIVER : GICD_ICACTIVER;
  459. break;
  460. case IRQCHIP_STATE_MASKED:
  461. if (val) {
  462. gic_mask_irq(d);
  463. return 0;
  464. }
  465. reg = GICD_ISENABLER;
  466. break;
  467. default:
  468. return -EINVAL;
  469. }
  470. gic_poke_irq(d, reg);
  471. /*
  472. * Force read-back to guarantee that the active state has taken
  473. * effect, and won't race with a guest-driven deactivation.
  474. */
  475. if (reg == GICD_ISACTIVER)
  476. gic_peek_irq(d, reg);
  477. return 0;
  478. }
  479. static int gic_irq_get_irqchip_state(struct irq_data *d,
  480. enum irqchip_irq_state which, bool *val)
  481. {
  482. if (d->hwirq >= 8192) /* PPI/SPI only */
  483. return -EINVAL;
  484. switch (which) {
  485. case IRQCHIP_STATE_PENDING:
  486. *val = gic_peek_irq(d, GICD_ISPENDR);
  487. break;
  488. case IRQCHIP_STATE_ACTIVE:
  489. *val = gic_peek_irq(d, GICD_ISACTIVER);
  490. break;
  491. case IRQCHIP_STATE_MASKED:
  492. *val = !gic_peek_irq(d, GICD_ISENABLER);
  493. break;
  494. default:
  495. return -EINVAL;
  496. }
  497. return 0;
  498. }
  499. static void gic_irq_set_prio(struct irq_data *d, u8 prio)
  500. {
  501. void __iomem *base = gic_dist_base(d);
  502. u32 offset, index;
  503. offset = convert_offset_index(d, GICD_IPRIORITYR, &index);
  504. writeb_relaxed(prio, base + offset + index);
  505. }
  506. static int gic_irq_nmi_setup(struct irq_data *d)
  507. {
  508. struct irq_desc *desc = irq_to_desc(d->irq);
  509. if (!gic_supports_nmi())
  510. return -EINVAL;
  511. if (gic_peek_irq(d, GICD_ISENABLER)) {
  512. pr_err("Cannot set NMI property of enabled IRQ %u\n", d->irq);
  513. return -EINVAL;
  514. }
  515. /*
  516. * A secondary irq_chip should be in charge of LPI request,
  517. * it should not be possible to get there
  518. */
  519. if (WARN_ON(irqd_to_hwirq(d) >= 8192))
  520. return -EINVAL;
  521. /* desc lock should already be held */
  522. if (!gic_irq_in_rdist(d))
  523. desc->handle_irq = handle_fasteoi_nmi;
  524. gic_irq_set_prio(d, dist_prio_nmi);
  525. return 0;
  526. }
  527. static void gic_irq_nmi_teardown(struct irq_data *d)
  528. {
  529. struct irq_desc *desc = irq_to_desc(d->irq);
  530. if (WARN_ON(!gic_supports_nmi()))
  531. return;
  532. if (gic_peek_irq(d, GICD_ISENABLER)) {
  533. pr_err("Cannot set NMI property of enabled IRQ %u\n", d->irq);
  534. return;
  535. }
  536. /*
  537. * A secondary irq_chip should be in charge of LPI request,
  538. * it should not be possible to get there
  539. */
  540. if (WARN_ON(irqd_to_hwirq(d) >= 8192))
  541. return;
  542. /* desc lock should already be held */
  543. if (!gic_irq_in_rdist(d))
  544. desc->handle_irq = handle_fasteoi_irq;
  545. gic_irq_set_prio(d, dist_prio_irq);
  546. }
  547. static bool gic_arm64_erratum_2941627_needed(struct irq_data *d)
  548. {
  549. enum gic_intid_range range;
  550. if (!static_branch_unlikely(&gic_arm64_2941627_erratum))
  551. return false;
  552. range = get_intid_range(d);
  553. /*
  554. * The workaround is needed if the IRQ is an SPI and
  555. * the target cpu is different from the one we are
  556. * executing on.
  557. */
  558. return (range == SPI_RANGE || range == ESPI_RANGE) &&
  559. !cpumask_test_cpu(raw_smp_processor_id(),
  560. irq_data_get_effective_affinity_mask(d));
  561. }
  562. static void gic_eoi_irq(struct irq_data *d)
  563. {
  564. write_gicreg(irqd_to_hwirq(d), ICC_EOIR1_EL1);
  565. isb();
  566. if (gic_arm64_erratum_2941627_needed(d)) {
  567. /*
  568. * Make sure the GIC stream deactivate packet
  569. * issued by ICC_EOIR1_EL1 has completed before
  570. * deactivating through GICD_IACTIVER.
  571. */
  572. dsb(sy);
  573. gic_poke_irq(d, GICD_ICACTIVER);
  574. }
  575. }
  576. static void gic_eoimode1_eoi_irq(struct irq_data *d)
  577. {
  578. /*
  579. * No need to deactivate an LPI, or an interrupt that
  580. * is is getting forwarded to a vcpu.
  581. */
  582. if (irqd_to_hwirq(d) >= 8192 || irqd_is_forwarded_to_vcpu(d))
  583. return;
  584. if (!gic_arm64_erratum_2941627_needed(d))
  585. gic_write_dir(irqd_to_hwirq(d));
  586. else
  587. gic_poke_irq(d, GICD_ICACTIVER);
  588. }
  589. static int gic_set_type(struct irq_data *d, unsigned int type)
  590. {
  591. irq_hw_number_t irq = irqd_to_hwirq(d);
  592. enum gic_intid_range range;
  593. void __iomem *base;
  594. u32 offset, index;
  595. int ret;
  596. range = get_intid_range(d);
  597. /* Interrupt configuration for SGIs can't be changed */
  598. if (range == SGI_RANGE)
  599. return type != IRQ_TYPE_EDGE_RISING ? -EINVAL : 0;
  600. /* SPIs have restrictions on the supported types */
  601. if ((range == SPI_RANGE || range == ESPI_RANGE) &&
  602. type != IRQ_TYPE_LEVEL_HIGH && type != IRQ_TYPE_EDGE_RISING)
  603. return -EINVAL;
  604. if (gic_irq_in_rdist(d))
  605. base = gic_data_rdist_sgi_base();
  606. else
  607. base = gic_dist_base_alias(d);
  608. offset = convert_offset_index(d, GICD_ICFGR, &index);
  609. ret = gic_configure_irq(index, type, base + offset);
  610. if (ret && (range == PPI_RANGE || range == EPPI_RANGE)) {
  611. /* Misconfigured PPIs are usually not fatal */
  612. pr_warn("GIC: PPI INTID%ld is secure or misconfigured\n", irq);
  613. ret = 0;
  614. }
  615. return ret;
  616. }
  617. static int gic_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu)
  618. {
  619. if (get_intid_range(d) == SGI_RANGE)
  620. return -EINVAL;
  621. if (vcpu)
  622. irqd_set_forwarded_to_vcpu(d);
  623. else
  624. irqd_clr_forwarded_to_vcpu(d);
  625. return 0;
  626. }
  627. static u64 gic_cpu_to_affinity(int cpu)
  628. {
  629. u64 mpidr = cpu_logical_map(cpu);
  630. u64 aff;
  631. /* ASR8601 needs to have its affinities shifted down... */
  632. if (unlikely(gic_data.flags & FLAGS_WORKAROUND_ASR_ERRATUM_8601001))
  633. mpidr = (MPIDR_AFFINITY_LEVEL(mpidr, 1) |
  634. (MPIDR_AFFINITY_LEVEL(mpidr, 2) << 8));
  635. aff = ((u64)MPIDR_AFFINITY_LEVEL(mpidr, 3) << 32 |
  636. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  637. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  638. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  639. return aff;
  640. }
  641. static void gic_deactivate_unhandled(u32 irqnr)
  642. {
  643. if (static_branch_likely(&supports_deactivate_key)) {
  644. if (irqnr < 8192)
  645. gic_write_dir(irqnr);
  646. } else {
  647. write_gicreg(irqnr, ICC_EOIR1_EL1);
  648. isb();
  649. }
  650. }
  651. /*
  652. * Follow a read of the IAR with any HW maintenance that needs to happen prior
  653. * to invoking the relevant IRQ handler. We must do two things:
  654. *
  655. * (1) Ensure instruction ordering between a read of IAR and subsequent
  656. * instructions in the IRQ handler using an ISB.
  657. *
  658. * It is possible for the IAR to report an IRQ which was signalled *after*
  659. * the CPU took an IRQ exception as multiple interrupts can race to be
  660. * recognized by the GIC, earlier interrupts could be withdrawn, and/or
  661. * later interrupts could be prioritized by the GIC.
  662. *
  663. * For devices which are tightly coupled to the CPU, such as PMUs, a
  664. * context synchronization event is necessary to ensure that system
  665. * register state is not stale, as these may have been indirectly written
  666. * *after* exception entry.
  667. *
  668. * (2) Execute an interrupt priority drop when EOI mode 1 is in use.
  669. */
  670. static inline void gic_complete_ack(u32 irqnr)
  671. {
  672. if (static_branch_likely(&supports_deactivate_key))
  673. write_gicreg(irqnr, ICC_EOIR1_EL1);
  674. isb();
  675. }
  676. static bool gic_rpr_is_nmi_prio(void)
  677. {
  678. if (!gic_supports_nmi())
  679. return false;
  680. return unlikely(gic_read_rpr() == GICV3_PRIO_NMI);
  681. }
  682. static bool gic_irqnr_is_special(u32 irqnr)
  683. {
  684. return irqnr >= 1020 && irqnr <= 1023;
  685. }
  686. static void __gic_handle_irq(u32 irqnr, struct pt_regs *regs)
  687. {
  688. if (gic_irqnr_is_special(irqnr))
  689. return;
  690. gic_complete_ack(irqnr);
  691. if (generic_handle_domain_irq(gic_data.domain, irqnr)) {
  692. WARN_ONCE(true, "Unexpected interrupt (irqnr %u)\n", irqnr);
  693. gic_deactivate_unhandled(irqnr);
  694. }
  695. }
  696. static void __gic_handle_nmi(u32 irqnr, struct pt_regs *regs)
  697. {
  698. if (gic_irqnr_is_special(irqnr))
  699. return;
  700. gic_complete_ack(irqnr);
  701. if (generic_handle_domain_nmi(gic_data.domain, irqnr)) {
  702. WARN_ONCE(true, "Unexpected pseudo-NMI (irqnr %u)\n", irqnr);
  703. gic_deactivate_unhandled(irqnr);
  704. }
  705. }
  706. /*
  707. * An exception has been taken from a context with IRQs enabled, and this could
  708. * be an IRQ or an NMI.
  709. *
  710. * The entry code called us with DAIF.IF set to keep NMIs masked. We must clear
  711. * DAIF.IF (and update ICC_PMR_EL1 to mask regular IRQs) prior to returning,
  712. * after handling any NMI but before handling any IRQ.
  713. *
  714. * The entry code has performed IRQ entry, and if an NMI is detected we must
  715. * perform NMI entry/exit around invoking the handler.
  716. */
  717. static void __gic_handle_irq_from_irqson(struct pt_regs *regs)
  718. {
  719. bool is_nmi;
  720. u32 irqnr;
  721. irqnr = gic_read_iar();
  722. is_nmi = gic_rpr_is_nmi_prio();
  723. if (is_nmi) {
  724. nmi_enter();
  725. __gic_handle_nmi(irqnr, regs);
  726. nmi_exit();
  727. }
  728. if (gic_prio_masking_enabled()) {
  729. gic_pmr_mask_irqs();
  730. gic_arch_enable_irqs();
  731. }
  732. if (!is_nmi)
  733. __gic_handle_irq(irqnr, regs);
  734. }
  735. /*
  736. * An exception has been taken from a context with IRQs disabled, which can only
  737. * be an NMI.
  738. *
  739. * The entry code called us with DAIF.IF set to keep NMIs masked. We must leave
  740. * DAIF.IF (and ICC_PMR_EL1) unchanged.
  741. *
  742. * The entry code has performed NMI entry.
  743. */
  744. static void __gic_handle_irq_from_irqsoff(struct pt_regs *regs)
  745. {
  746. u64 pmr;
  747. u32 irqnr;
  748. /*
  749. * We were in a context with IRQs disabled. However, the
  750. * entry code has set PMR to a value that allows any
  751. * interrupt to be acknowledged, and not just NMIs. This can
  752. * lead to surprising effects if the NMI has been retired in
  753. * the meantime, and that there is an IRQ pending. The IRQ
  754. * would then be taken in NMI context, something that nobody
  755. * wants to debug twice.
  756. *
  757. * Until we sort this, drop PMR again to a level that will
  758. * actually only allow NMIs before reading IAR, and then
  759. * restore it to what it was.
  760. */
  761. pmr = gic_read_pmr();
  762. gic_pmr_mask_irqs();
  763. isb();
  764. irqnr = gic_read_iar();
  765. gic_write_pmr(pmr);
  766. __gic_handle_nmi(irqnr, regs);
  767. }
  768. static void __exception_irq_entry gic_handle_irq(struct pt_regs *regs)
  769. {
  770. if (unlikely(gic_supports_nmi() && !interrupts_enabled(regs)))
  771. __gic_handle_irq_from_irqsoff(regs);
  772. else
  773. __gic_handle_irq_from_irqson(regs);
  774. }
  775. static void __init gic_dist_init(void)
  776. {
  777. unsigned int i;
  778. u64 affinity;
  779. void __iomem *base = gic_data.dist_base;
  780. u32 val;
  781. /* Disable the distributor */
  782. writel_relaxed(0, base + GICD_CTLR);
  783. gic_dist_wait_for_rwp();
  784. /*
  785. * Configure SPIs as non-secure Group-1. This will only matter
  786. * if the GIC only has a single security state. This will not
  787. * do the right thing if the kernel is running in secure mode,
  788. * but that's not the intended use case anyway.
  789. */
  790. for (i = 32; i < GIC_LINE_NR; i += 32)
  791. writel_relaxed(~0, base + GICD_IGROUPR + i / 8);
  792. /* Extended SPI range, not handled by the GICv2/GICv3 common code */
  793. for (i = 0; i < GIC_ESPI_NR; i += 32) {
  794. writel_relaxed(~0U, base + GICD_ICENABLERnE + i / 8);
  795. writel_relaxed(~0U, base + GICD_ICACTIVERnE + i / 8);
  796. }
  797. for (i = 0; i < GIC_ESPI_NR; i += 32)
  798. writel_relaxed(~0U, base + GICD_IGROUPRnE + i / 8);
  799. for (i = 0; i < GIC_ESPI_NR; i += 16)
  800. writel_relaxed(0, base + GICD_ICFGRnE + i / 4);
  801. for (i = 0; i < GIC_ESPI_NR; i += 4)
  802. writel_relaxed(REPEAT_BYTE_U32(dist_prio_irq),
  803. base + GICD_IPRIORITYRnE + i);
  804. /* Now do the common stuff */
  805. gic_dist_config(base, GIC_LINE_NR, dist_prio_irq);
  806. val = GICD_CTLR_ARE_NS | GICD_CTLR_ENABLE_G1A | GICD_CTLR_ENABLE_G1;
  807. if (gic_data.rdists.gicd_typer2 & GICD_TYPER2_nASSGIcap) {
  808. pr_info("Enabling SGIs without active state\n");
  809. val |= GICD_CTLR_nASSGIreq;
  810. }
  811. /* Enable distributor with ARE, Group1, and wait for it to drain */
  812. writel_relaxed(val, base + GICD_CTLR);
  813. gic_dist_wait_for_rwp();
  814. /*
  815. * Set all global interrupts to the boot CPU only. ARE must be
  816. * enabled.
  817. */
  818. affinity = gic_cpu_to_affinity(smp_processor_id());
  819. for (i = 32; i < GIC_LINE_NR; i++)
  820. gic_write_irouter(affinity, base + GICD_IROUTER + i * 8);
  821. for (i = 0; i < GIC_ESPI_NR; i++)
  822. gic_write_irouter(affinity, base + GICD_IROUTERnE + i * 8);
  823. }
  824. static int gic_iterate_rdists(int (*fn)(struct redist_region *, void __iomem *))
  825. {
  826. int ret = -ENODEV;
  827. int i;
  828. for (i = 0; i < gic_data.nr_redist_regions; i++) {
  829. void __iomem *ptr = gic_data.redist_regions[i].redist_base;
  830. u64 typer;
  831. u32 reg;
  832. reg = readl_relaxed(ptr + GICR_PIDR2) & GIC_PIDR2_ARCH_MASK;
  833. if (reg != GIC_PIDR2_ARCH_GICv3 &&
  834. reg != GIC_PIDR2_ARCH_GICv4) { /* We're in trouble... */
  835. pr_warn("No redistributor present @%p\n", ptr);
  836. break;
  837. }
  838. do {
  839. typer = gic_read_typer(ptr + GICR_TYPER);
  840. ret = fn(gic_data.redist_regions + i, ptr);
  841. if (!ret)
  842. return 0;
  843. if (gic_data.redist_regions[i].single_redist)
  844. break;
  845. if (gic_data.redist_stride) {
  846. ptr += gic_data.redist_stride;
  847. } else {
  848. ptr += SZ_64K * 2; /* Skip RD_base + SGI_base */
  849. if (typer & GICR_TYPER_VLPIS)
  850. ptr += SZ_64K * 2; /* Skip VLPI_base + reserved page */
  851. }
  852. } while (!(typer & GICR_TYPER_LAST));
  853. }
  854. return ret ? -ENODEV : 0;
  855. }
  856. static int __gic_populate_rdist(struct redist_region *region, void __iomem *ptr)
  857. {
  858. unsigned long mpidr;
  859. u64 typer;
  860. u32 aff;
  861. /*
  862. * Convert affinity to a 32bit value that can be matched to
  863. * GICR_TYPER bits [63:32].
  864. */
  865. mpidr = gic_cpu_to_affinity(smp_processor_id());
  866. aff = (MPIDR_AFFINITY_LEVEL(mpidr, 3) << 24 |
  867. MPIDR_AFFINITY_LEVEL(mpidr, 2) << 16 |
  868. MPIDR_AFFINITY_LEVEL(mpidr, 1) << 8 |
  869. MPIDR_AFFINITY_LEVEL(mpidr, 0));
  870. typer = gic_read_typer(ptr + GICR_TYPER);
  871. if ((typer >> 32) == aff) {
  872. u64 offset = ptr - region->redist_base;
  873. raw_spin_lock_init(&gic_data_rdist()->rd_lock);
  874. gic_data_rdist_rd_base() = ptr;
  875. gic_data_rdist()->phys_base = region->phys_base + offset;
  876. pr_info("CPU%d: found redistributor %lx region %d:%pa\n",
  877. smp_processor_id(), mpidr,
  878. (int)(region - gic_data.redist_regions),
  879. &gic_data_rdist()->phys_base);
  880. return 0;
  881. }
  882. /* Try next one */
  883. return 1;
  884. }
  885. static int gic_populate_rdist(void)
  886. {
  887. if (gic_iterate_rdists(__gic_populate_rdist) == 0)
  888. return 0;
  889. /* We couldn't even deal with ourselves... */
  890. WARN(true, "CPU%d: mpidr %lx has no re-distributor!\n",
  891. smp_processor_id(),
  892. (unsigned long)cpu_logical_map(smp_processor_id()));
  893. return -ENODEV;
  894. }
  895. static int __gic_update_rdist_properties(struct redist_region *region,
  896. void __iomem *ptr)
  897. {
  898. u64 typer = gic_read_typer(ptr + GICR_TYPER);
  899. u32 ctlr = readl_relaxed(ptr + GICR_CTLR);
  900. /* Boot-time cleanup */
  901. if ((typer & GICR_TYPER_VLPIS) && (typer & GICR_TYPER_RVPEID)) {
  902. u64 val;
  903. /* Deactivate any present vPE */
  904. val = gicr_read_vpendbaser(ptr + SZ_128K + GICR_VPENDBASER);
  905. if (val & GICR_VPENDBASER_Valid)
  906. gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
  907. ptr + SZ_128K + GICR_VPENDBASER);
  908. /* Mark the VPE table as invalid */
  909. val = gicr_read_vpropbaser(ptr + SZ_128K + GICR_VPROPBASER);
  910. val &= ~GICR_VPROPBASER_4_1_VALID;
  911. gicr_write_vpropbaser(val, ptr + SZ_128K + GICR_VPROPBASER);
  912. }
  913. gic_data.rdists.has_vlpis &= !!(typer & GICR_TYPER_VLPIS);
  914. /*
  915. * TYPER.RVPEID implies some form of DirectLPI, no matter what the
  916. * doc says... :-/ And CTLR.IR implies another subset of DirectLPI
  917. * that the ITS driver can make use of for LPIs (and not VLPIs).
  918. *
  919. * These are 3 different ways to express the same thing, depending
  920. * on the revision of the architecture and its relaxations over
  921. * time. Just group them under the 'direct_lpi' banner.
  922. */
  923. gic_data.rdists.has_rvpeid &= !!(typer & GICR_TYPER_RVPEID);
  924. gic_data.rdists.has_direct_lpi &= (!!(typer & GICR_TYPER_DirectLPIS) |
  925. !!(ctlr & GICR_CTLR_IR) |
  926. gic_data.rdists.has_rvpeid);
  927. gic_data.rdists.has_vpend_valid_dirty &= !!(typer & GICR_TYPER_DIRTY);
  928. /* Detect non-sensical configurations */
  929. if (WARN_ON_ONCE(gic_data.rdists.has_rvpeid && !gic_data.rdists.has_vlpis)) {
  930. gic_data.rdists.has_direct_lpi = false;
  931. gic_data.rdists.has_vlpis = false;
  932. gic_data.rdists.has_rvpeid = false;
  933. }
  934. gic_data.ppi_nr = min(GICR_TYPER_NR_PPIS(typer), gic_data.ppi_nr);
  935. return 1;
  936. }
  937. static void gic_update_rdist_properties(void)
  938. {
  939. gic_data.ppi_nr = UINT_MAX;
  940. gic_iterate_rdists(__gic_update_rdist_properties);
  941. if (WARN_ON(gic_data.ppi_nr == UINT_MAX))
  942. gic_data.ppi_nr = 0;
  943. pr_info("GICv3 features: %d PPIs%s%s\n",
  944. gic_data.ppi_nr,
  945. gic_data.has_rss ? ", RSS" : "",
  946. gic_data.rdists.has_direct_lpi ? ", DirectLPI" : "");
  947. if (gic_data.rdists.has_vlpis)
  948. pr_info("GICv4 features: %s%s%s\n",
  949. gic_data.rdists.has_direct_lpi ? "DirectLPI " : "",
  950. gic_data.rdists.has_rvpeid ? "RVPEID " : "",
  951. gic_data.rdists.has_vpend_valid_dirty ? "Valid+Dirty " : "");
  952. }
  953. static void gic_cpu_sys_reg_enable(void)
  954. {
  955. /*
  956. * Need to check that the SRE bit has actually been set. If
  957. * not, it means that SRE is disabled at EL2. We're going to
  958. * die painfully, and there is nothing we can do about it.
  959. *
  960. * Kindly inform the luser.
  961. */
  962. if (!gic_enable_sre())
  963. pr_err("GIC: unable to set SRE (disabled at EL2), panic ahead\n");
  964. }
  965. static void gic_cpu_sys_reg_init(void)
  966. {
  967. int i, cpu = smp_processor_id();
  968. u64 mpidr = gic_cpu_to_affinity(cpu);
  969. u64 need_rss = MPIDR_RS(mpidr);
  970. bool group0;
  971. u32 pribits;
  972. pribits = gic_get_pribits();
  973. group0 = gic_has_group0();
  974. /* Set priority mask register */
  975. if (!gic_prio_masking_enabled()) {
  976. write_gicreg(DEFAULT_PMR_VALUE, ICC_PMR_EL1);
  977. } else if (gic_supports_nmi()) {
  978. /*
  979. * Check that all CPUs use the same priority space.
  980. *
  981. * If there's a mismatch with the boot CPU, the system is
  982. * likely to die as interrupt masking will not work properly on
  983. * all CPUs.
  984. */
  985. WARN_ON(group0 != cpus_have_group0);
  986. WARN_ON(gic_dist_security_disabled() != cpus_have_security_disabled);
  987. }
  988. /*
  989. * Some firmwares hand over to the kernel with the BPR changed from
  990. * its reset value (and with a value large enough to prevent
  991. * any pre-emptive interrupts from working at all). Writing a zero
  992. * to BPR restores is reset value.
  993. */
  994. gic_write_bpr1(0);
  995. if (static_branch_likely(&supports_deactivate_key)) {
  996. /* EOI drops priority only (mode 1) */
  997. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop);
  998. } else {
  999. /* EOI deactivates interrupt too (mode 0) */
  1000. gic_write_ctlr(ICC_CTLR_EL1_EOImode_drop_dir);
  1001. }
  1002. /* Always whack Group0 before Group1 */
  1003. if (group0) {
  1004. switch(pribits) {
  1005. case 8:
  1006. case 7:
  1007. write_gicreg(0, ICC_AP0R3_EL1);
  1008. write_gicreg(0, ICC_AP0R2_EL1);
  1009. fallthrough;
  1010. case 6:
  1011. write_gicreg(0, ICC_AP0R1_EL1);
  1012. fallthrough;
  1013. case 5:
  1014. case 4:
  1015. write_gicreg(0, ICC_AP0R0_EL1);
  1016. }
  1017. isb();
  1018. }
  1019. switch(pribits) {
  1020. case 8:
  1021. case 7:
  1022. write_gicreg(0, ICC_AP1R3_EL1);
  1023. write_gicreg(0, ICC_AP1R2_EL1);
  1024. fallthrough;
  1025. case 6:
  1026. write_gicreg(0, ICC_AP1R1_EL1);
  1027. fallthrough;
  1028. case 5:
  1029. case 4:
  1030. write_gicreg(0, ICC_AP1R0_EL1);
  1031. }
  1032. isb();
  1033. /* ... and let's hit the road... */
  1034. gic_write_grpen1(1);
  1035. /* Keep the RSS capability status in per_cpu variable */
  1036. per_cpu(has_rss, cpu) = !!(gic_read_ctlr() & ICC_CTLR_EL1_RSS);
  1037. /* Check all the CPUs have capable of sending SGIs to other CPUs */
  1038. for_each_online_cpu(i) {
  1039. bool have_rss = per_cpu(has_rss, i) && per_cpu(has_rss, cpu);
  1040. need_rss |= MPIDR_RS(gic_cpu_to_affinity(i));
  1041. if (need_rss && (!have_rss))
  1042. pr_crit("CPU%d (%lx) can't SGI CPU%d (%lx), no RSS\n",
  1043. cpu, (unsigned long)mpidr,
  1044. i, (unsigned long)gic_cpu_to_affinity(i));
  1045. }
  1046. /**
  1047. * GIC spec says, when ICC_CTLR_EL1.RSS==1 and GICD_TYPER.RSS==0,
  1048. * writing ICC_ASGI1R_EL1 register with RS != 0 is a CONSTRAINED
  1049. * UNPREDICTABLE choice of :
  1050. * - The write is ignored.
  1051. * - The RS field is treated as 0.
  1052. */
  1053. if (need_rss && (!gic_data.has_rss))
  1054. pr_crit_once("RSS is required but GICD doesn't support it\n");
  1055. }
  1056. static bool gicv3_nolpi;
  1057. static int __init gicv3_nolpi_cfg(char *buf)
  1058. {
  1059. return kstrtobool(buf, &gicv3_nolpi);
  1060. }
  1061. early_param("irqchip.gicv3_nolpi", gicv3_nolpi_cfg);
  1062. static int gic_dist_supports_lpis(void)
  1063. {
  1064. return (IS_ENABLED(CONFIG_ARM_GIC_V3_ITS) &&
  1065. !!(readl_relaxed(gic_data.dist_base + GICD_TYPER) & GICD_TYPER_LPIS) &&
  1066. !gicv3_nolpi);
  1067. }
  1068. static void gic_cpu_init(void)
  1069. {
  1070. void __iomem *rbase;
  1071. int i;
  1072. /* Register ourselves with the rest of the world */
  1073. if (gic_populate_rdist())
  1074. return;
  1075. gic_enable_redist(true);
  1076. WARN((gic_data.ppi_nr > 16 || GIC_ESPI_NR != 0) &&
  1077. !(gic_read_ctlr() & ICC_CTLR_EL1_ExtRange),
  1078. "Distributor has extended ranges, but CPU%d doesn't\n",
  1079. smp_processor_id());
  1080. rbase = gic_data_rdist_sgi_base();
  1081. /* Configure SGIs/PPIs as non-secure Group-1 */
  1082. for (i = 0; i < gic_data.ppi_nr + SGI_NR; i += 32)
  1083. writel_relaxed(~0, rbase + GICR_IGROUPR0 + i / 8);
  1084. gic_cpu_config(rbase, gic_data.ppi_nr + SGI_NR, dist_prio_irq);
  1085. gic_redist_wait_for_rwp();
  1086. /* initialise system registers */
  1087. gic_cpu_sys_reg_init();
  1088. }
  1089. #ifdef CONFIG_SMP
  1090. #define MPIDR_TO_SGI_RS(mpidr) (MPIDR_RS(mpidr) << ICC_SGI1R_RS_SHIFT)
  1091. #define MPIDR_TO_SGI_CLUSTER_ID(mpidr) ((mpidr) & ~0xFUL)
  1092. /*
  1093. * gic_starting_cpu() is called after the last point where cpuhp is allowed
  1094. * to fail. So pre check for problems earlier.
  1095. */
  1096. static int gic_check_rdist(unsigned int cpu)
  1097. {
  1098. if (cpumask_test_cpu(cpu, &broken_rdists))
  1099. return -EINVAL;
  1100. return 0;
  1101. }
  1102. static int gic_starting_cpu(unsigned int cpu)
  1103. {
  1104. gic_cpu_sys_reg_enable();
  1105. gic_cpu_init();
  1106. if (gic_dist_supports_lpis())
  1107. its_cpu_init();
  1108. return 0;
  1109. }
  1110. static u16 gic_compute_target_list(int *base_cpu, const struct cpumask *mask,
  1111. unsigned long cluster_id)
  1112. {
  1113. int next_cpu, cpu = *base_cpu;
  1114. unsigned long mpidr;
  1115. u16 tlist = 0;
  1116. mpidr = gic_cpu_to_affinity(cpu);
  1117. while (cpu < nr_cpu_ids) {
  1118. tlist |= 1 << (mpidr & 0xf);
  1119. next_cpu = cpumask_next(cpu, mask);
  1120. if (next_cpu >= nr_cpu_ids)
  1121. goto out;
  1122. cpu = next_cpu;
  1123. mpidr = gic_cpu_to_affinity(cpu);
  1124. if (cluster_id != MPIDR_TO_SGI_CLUSTER_ID(mpidr)) {
  1125. cpu--;
  1126. goto out;
  1127. }
  1128. }
  1129. out:
  1130. *base_cpu = cpu;
  1131. return tlist;
  1132. }
  1133. #define MPIDR_TO_SGI_AFFINITY(cluster_id, level) \
  1134. (MPIDR_AFFINITY_LEVEL(cluster_id, level) \
  1135. << ICC_SGI1R_AFFINITY_## level ##_SHIFT)
  1136. static void gic_send_sgi(u64 cluster_id, u16 tlist, unsigned int irq)
  1137. {
  1138. u64 val;
  1139. val = (MPIDR_TO_SGI_AFFINITY(cluster_id, 3) |
  1140. MPIDR_TO_SGI_AFFINITY(cluster_id, 2) |
  1141. irq << ICC_SGI1R_SGI_ID_SHIFT |
  1142. MPIDR_TO_SGI_AFFINITY(cluster_id, 1) |
  1143. MPIDR_TO_SGI_RS(cluster_id) |
  1144. tlist << ICC_SGI1R_TARGET_LIST_SHIFT);
  1145. pr_devel("CPU%d: ICC_SGI1R_EL1 %llx\n", smp_processor_id(), val);
  1146. gic_write_sgi1r(val);
  1147. }
  1148. static void gic_ipi_send_mask(struct irq_data *d, const struct cpumask *mask)
  1149. {
  1150. int cpu;
  1151. if (WARN_ON(d->hwirq >= 16))
  1152. return;
  1153. /*
  1154. * Ensure that stores to Normal memory are visible to the
  1155. * other CPUs before issuing the IPI.
  1156. */
  1157. dsb(ishst);
  1158. for_each_cpu(cpu, mask) {
  1159. u64 cluster_id = MPIDR_TO_SGI_CLUSTER_ID(gic_cpu_to_affinity(cpu));
  1160. u16 tlist;
  1161. tlist = gic_compute_target_list(&cpu, mask, cluster_id);
  1162. gic_send_sgi(cluster_id, tlist, d->hwirq);
  1163. }
  1164. /* Force the above writes to ICC_SGI1R_EL1 to be executed */
  1165. isb();
  1166. }
  1167. static void __init gic_smp_init(void)
  1168. {
  1169. struct irq_fwspec sgi_fwspec = {
  1170. .fwnode = gic_data.fwnode,
  1171. .param_count = 1,
  1172. };
  1173. int base_sgi;
  1174. cpuhp_setup_state_nocalls(CPUHP_BP_PREPARE_DYN,
  1175. "irqchip/arm/gicv3:checkrdist",
  1176. gic_check_rdist, NULL);
  1177. cpuhp_setup_state_nocalls(CPUHP_AP_IRQ_GIC_STARTING,
  1178. "irqchip/arm/gicv3:starting",
  1179. gic_starting_cpu, NULL);
  1180. /* Register all 8 non-secure SGIs */
  1181. base_sgi = irq_domain_alloc_irqs(gic_data.domain, 8, NUMA_NO_NODE, &sgi_fwspec);
  1182. if (WARN_ON(base_sgi <= 0))
  1183. return;
  1184. set_smp_ipi_range(base_sgi, 8);
  1185. }
  1186. static int gic_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
  1187. bool force)
  1188. {
  1189. unsigned int cpu;
  1190. u32 offset, index;
  1191. void __iomem *reg;
  1192. int enabled;
  1193. u64 val;
  1194. if (force)
  1195. cpu = cpumask_first(mask_val);
  1196. else
  1197. cpu = cpumask_any_and(mask_val, cpu_online_mask);
  1198. if (cpu >= nr_cpu_ids)
  1199. return -EINVAL;
  1200. if (gic_irq_in_rdist(d))
  1201. return -EINVAL;
  1202. /* If interrupt was enabled, disable it first */
  1203. enabled = gic_peek_irq(d, GICD_ISENABLER);
  1204. if (enabled)
  1205. gic_mask_irq(d);
  1206. offset = convert_offset_index(d, GICD_IROUTER, &index);
  1207. reg = gic_dist_base(d) + offset + (index * 8);
  1208. val = gic_cpu_to_affinity(cpu);
  1209. gic_write_irouter(val, reg);
  1210. /*
  1211. * If the interrupt was enabled, enabled it again. Otherwise,
  1212. * just wait for the distributor to have digested our changes.
  1213. */
  1214. if (enabled)
  1215. gic_unmask_irq(d);
  1216. irq_data_update_effective_affinity(d, cpumask_of(cpu));
  1217. return IRQ_SET_MASK_OK_DONE;
  1218. }
  1219. #else
  1220. #define gic_set_affinity NULL
  1221. #define gic_ipi_send_mask NULL
  1222. #define gic_smp_init() do { } while(0)
  1223. #endif
  1224. static int gic_retrigger(struct irq_data *data)
  1225. {
  1226. return !gic_irq_set_irqchip_state(data, IRQCHIP_STATE_PENDING, true);
  1227. }
  1228. #ifdef CONFIG_CPU_PM
  1229. static int gic_cpu_pm_notifier(struct notifier_block *self,
  1230. unsigned long cmd, void *v)
  1231. {
  1232. if (cmd == CPU_PM_EXIT || cmd == CPU_PM_ENTER_FAILED) {
  1233. if (gic_dist_security_disabled())
  1234. gic_enable_redist(true);
  1235. gic_cpu_sys_reg_enable();
  1236. gic_cpu_sys_reg_init();
  1237. } else if (cmd == CPU_PM_ENTER && gic_dist_security_disabled()) {
  1238. gic_write_grpen1(0);
  1239. gic_enable_redist(false);
  1240. }
  1241. return NOTIFY_OK;
  1242. }
  1243. static struct notifier_block gic_cpu_pm_notifier_block = {
  1244. .notifier_call = gic_cpu_pm_notifier,
  1245. };
  1246. static void gic_cpu_pm_init(void)
  1247. {
  1248. cpu_pm_register_notifier(&gic_cpu_pm_notifier_block);
  1249. }
  1250. #else
  1251. static inline void gic_cpu_pm_init(void) { }
  1252. #endif /* CONFIG_CPU_PM */
  1253. static struct irq_chip gic_chip = {
  1254. .name = "GICv3",
  1255. .irq_mask = gic_mask_irq,
  1256. .irq_unmask = gic_unmask_irq,
  1257. .irq_eoi = gic_eoi_irq,
  1258. .irq_set_type = gic_set_type,
  1259. .irq_set_affinity = gic_set_affinity,
  1260. .irq_retrigger = gic_retrigger,
  1261. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  1262. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  1263. .irq_nmi_setup = gic_irq_nmi_setup,
  1264. .irq_nmi_teardown = gic_irq_nmi_teardown,
  1265. .ipi_send_mask = gic_ipi_send_mask,
  1266. .flags = IRQCHIP_SET_TYPE_MASKED |
  1267. IRQCHIP_SKIP_SET_WAKE |
  1268. IRQCHIP_MASK_ON_SUSPEND,
  1269. };
  1270. static struct irq_chip gic_eoimode1_chip = {
  1271. .name = "GICv3",
  1272. .irq_mask = gic_eoimode1_mask_irq,
  1273. .irq_unmask = gic_unmask_irq,
  1274. .irq_eoi = gic_eoimode1_eoi_irq,
  1275. .irq_set_type = gic_set_type,
  1276. .irq_set_affinity = gic_set_affinity,
  1277. .irq_retrigger = gic_retrigger,
  1278. .irq_get_irqchip_state = gic_irq_get_irqchip_state,
  1279. .irq_set_irqchip_state = gic_irq_set_irqchip_state,
  1280. .irq_set_vcpu_affinity = gic_irq_set_vcpu_affinity,
  1281. .irq_nmi_setup = gic_irq_nmi_setup,
  1282. .irq_nmi_teardown = gic_irq_nmi_teardown,
  1283. .ipi_send_mask = gic_ipi_send_mask,
  1284. .flags = IRQCHIP_SET_TYPE_MASKED |
  1285. IRQCHIP_SKIP_SET_WAKE |
  1286. IRQCHIP_MASK_ON_SUSPEND,
  1287. };
  1288. static int gic_irq_domain_map(struct irq_domain *d, unsigned int irq,
  1289. irq_hw_number_t hw)
  1290. {
  1291. struct irq_chip *chip = &gic_chip;
  1292. struct irq_data *irqd = irq_desc_get_irq_data(irq_to_desc(irq));
  1293. if (static_branch_likely(&supports_deactivate_key))
  1294. chip = &gic_eoimode1_chip;
  1295. switch (__get_intid_range(hw)) {
  1296. case SGI_RANGE:
  1297. case PPI_RANGE:
  1298. case EPPI_RANGE:
  1299. irq_set_percpu_devid(irq);
  1300. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  1301. handle_percpu_devid_irq, NULL, NULL);
  1302. break;
  1303. case SPI_RANGE:
  1304. case ESPI_RANGE:
  1305. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  1306. handle_fasteoi_irq, NULL, NULL);
  1307. irq_set_probe(irq);
  1308. irqd_set_single_target(irqd);
  1309. break;
  1310. case LPI_RANGE:
  1311. if (!gic_dist_supports_lpis())
  1312. return -EPERM;
  1313. irq_domain_set_info(d, irq, hw, chip, d->host_data,
  1314. handle_fasteoi_irq, NULL, NULL);
  1315. break;
  1316. default:
  1317. return -EPERM;
  1318. }
  1319. /* Prevents SW retriggers which mess up the ACK/EOI ordering */
  1320. irqd_set_handle_enforce_irqctx(irqd);
  1321. return 0;
  1322. }
  1323. static int gic_irq_domain_translate(struct irq_domain *d,
  1324. struct irq_fwspec *fwspec,
  1325. unsigned long *hwirq,
  1326. unsigned int *type)
  1327. {
  1328. if (fwspec->param_count == 1 && fwspec->param[0] < 16) {
  1329. *hwirq = fwspec->param[0];
  1330. *type = IRQ_TYPE_EDGE_RISING;
  1331. return 0;
  1332. }
  1333. if (is_of_node(fwspec->fwnode)) {
  1334. if (fwspec->param_count < 3)
  1335. return -EINVAL;
  1336. switch (fwspec->param[0]) {
  1337. case 0: /* SPI */
  1338. *hwirq = fwspec->param[1] + 32;
  1339. break;
  1340. case 1: /* PPI */
  1341. *hwirq = fwspec->param[1] + 16;
  1342. break;
  1343. case 2: /* ESPI */
  1344. *hwirq = fwspec->param[1] + ESPI_BASE_INTID;
  1345. break;
  1346. case 3: /* EPPI */
  1347. *hwirq = fwspec->param[1] + EPPI_BASE_INTID;
  1348. break;
  1349. case GIC_IRQ_TYPE_LPI: /* LPI */
  1350. *hwirq = fwspec->param[1];
  1351. break;
  1352. default:
  1353. return -EINVAL;
  1354. }
  1355. *type = fwspec->param[2] & IRQ_TYPE_SENSE_MASK;
  1356. /*
  1357. * Make it clear that broken DTs are... broken.
  1358. */
  1359. WARN_ON(*type == IRQ_TYPE_NONE);
  1360. return 0;
  1361. }
  1362. if (is_fwnode_irqchip(fwspec->fwnode)) {
  1363. if(fwspec->param_count != 2)
  1364. return -EINVAL;
  1365. if (fwspec->param[0] < 16) {
  1366. pr_err(FW_BUG "Illegal GSI%d translation request\n",
  1367. fwspec->param[0]);
  1368. return -EINVAL;
  1369. }
  1370. *hwirq = fwspec->param[0];
  1371. *type = fwspec->param[1];
  1372. WARN_ON(*type == IRQ_TYPE_NONE);
  1373. return 0;
  1374. }
  1375. return -EINVAL;
  1376. }
  1377. static int gic_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  1378. unsigned int nr_irqs, void *arg)
  1379. {
  1380. int i, ret;
  1381. irq_hw_number_t hwirq;
  1382. unsigned int type = IRQ_TYPE_NONE;
  1383. struct irq_fwspec *fwspec = arg;
  1384. ret = gic_irq_domain_translate(domain, fwspec, &hwirq, &type);
  1385. if (ret)
  1386. return ret;
  1387. for (i = 0; i < nr_irqs; i++) {
  1388. ret = gic_irq_domain_map(domain, virq + i, hwirq + i);
  1389. if (ret)
  1390. return ret;
  1391. }
  1392. return 0;
  1393. }
  1394. static void gic_irq_domain_free(struct irq_domain *domain, unsigned int virq,
  1395. unsigned int nr_irqs)
  1396. {
  1397. int i;
  1398. for (i = 0; i < nr_irqs; i++) {
  1399. struct irq_data *d = irq_domain_get_irq_data(domain, virq + i);
  1400. irq_set_handler(virq + i, NULL);
  1401. irq_domain_reset_irq_data(d);
  1402. }
  1403. }
  1404. static int gic_irq_domain_select(struct irq_domain *d,
  1405. struct irq_fwspec *fwspec,
  1406. enum irq_domain_bus_token bus_token)
  1407. {
  1408. irq_hw_number_t hwirq;
  1409. unsigned int type;
  1410. int ret;
  1411. /* Not for us */
  1412. if (fwspec->fwnode != d->fwnode)
  1413. return 0;
  1414. /* Handle pure domain searches */
  1415. if (!fwspec->param_count)
  1416. return d->bus_token == bus_token;
  1417. /* If this is not DT, then we have a single domain */
  1418. if (!is_of_node(fwspec->fwnode))
  1419. return 1;
  1420. ret = gic_irq_domain_translate(d, fwspec, &hwirq, &type);
  1421. if (WARN_ON_ONCE(ret))
  1422. return 0;
  1423. return d == gic_data.domain;
  1424. }
  1425. static int gic_irq_get_fwspec_info(struct irq_fwspec *fwspec, struct irq_fwspec_info *info)
  1426. {
  1427. const struct cpumask *mask = NULL;
  1428. info->flags = 0;
  1429. info->affinity = NULL;
  1430. /* ACPI is not capable of describing PPI affinity -- yet */
  1431. if (!is_of_node(fwspec->fwnode))
  1432. return 0;
  1433. /* If the specifier provides an affinity, use it */
  1434. if (fwspec->param_count == 4 && fwspec->param[3]) {
  1435. struct fwnode_handle *fw;
  1436. switch (fwspec->param[0]) {
  1437. case 1: /* PPI */
  1438. case 3: /* EPPI */
  1439. break;
  1440. default:
  1441. return 0;
  1442. }
  1443. fw = of_fwnode_handle(of_find_node_by_phandle(fwspec->param[3]));
  1444. if (!fw)
  1445. return -ENOENT;
  1446. for (int i = 0; i < gic_data.nr_parts; i++) {
  1447. if (gic_data.parts[i].partition_id == fw) {
  1448. mask = &gic_data.parts[i].mask;
  1449. break;
  1450. }
  1451. }
  1452. if (!mask)
  1453. return -ENOENT;
  1454. } else {
  1455. mask = cpu_possible_mask;
  1456. }
  1457. info->affinity = mask;
  1458. info->flags = IRQ_FWSPEC_INFO_AFFINITY_VALID;
  1459. return 0;
  1460. }
  1461. static const struct irq_domain_ops gic_irq_domain_ops = {
  1462. .translate = gic_irq_domain_translate,
  1463. .alloc = gic_irq_domain_alloc,
  1464. .free = gic_irq_domain_free,
  1465. .select = gic_irq_domain_select,
  1466. .get_fwspec_info = gic_irq_get_fwspec_info,
  1467. };
  1468. static bool gic_enable_quirk_msm8996(void *data)
  1469. {
  1470. struct gic_chip_data *d = data;
  1471. d->flags |= FLAGS_WORKAROUND_GICR_WAKER_MSM8996;
  1472. return true;
  1473. }
  1474. static bool gic_enable_quirk_cavium_38539(void *data)
  1475. {
  1476. struct gic_chip_data *d = data;
  1477. d->flags |= FLAGS_WORKAROUND_CAVIUM_ERRATUM_38539;
  1478. return true;
  1479. }
  1480. static bool gic_enable_quirk_hip06_07(void *data)
  1481. {
  1482. struct gic_chip_data *d = data;
  1483. /*
  1484. * HIP06 GICD_IIDR clashes with GIC-600 product number (despite
  1485. * not being an actual ARM implementation). The saving grace is
  1486. * that GIC-600 doesn't have ESPI, so nothing to do in that case.
  1487. * HIP07 doesn't even have a proper IIDR, and still pretends to
  1488. * have ESPI. In both cases, put them right.
  1489. */
  1490. if (d->rdists.gicd_typer & GICD_TYPER_ESPI) {
  1491. /* Zero both ESPI and the RES0 field next to it... */
  1492. d->rdists.gicd_typer &= ~GENMASK(9, 8);
  1493. return true;
  1494. }
  1495. return false;
  1496. }
  1497. #define T241_CHIPN_MASK GENMASK_ULL(45, 44)
  1498. #define T241_CHIP_GICDA_OFFSET 0x1580000
  1499. #define SMCCC_SOC_ID_T241 0x036b0241
  1500. static bool gic_enable_quirk_nvidia_t241(void *data)
  1501. {
  1502. s32 soc_id = arm_smccc_get_soc_id_version();
  1503. unsigned long chip_bmask = 0;
  1504. phys_addr_t phys;
  1505. u32 i;
  1506. /* Check JEP106 code for NVIDIA T241 chip (036b:0241) */
  1507. if ((soc_id < 0) || (soc_id != SMCCC_SOC_ID_T241))
  1508. return false;
  1509. /* Find the chips based on GICR regions PHYS addr */
  1510. for (i = 0; i < gic_data.nr_redist_regions; i++) {
  1511. chip_bmask |= BIT(FIELD_GET(T241_CHIPN_MASK,
  1512. (u64)gic_data.redist_regions[i].phys_base));
  1513. }
  1514. if (hweight32(chip_bmask) < 3)
  1515. return false;
  1516. /* Setup GICD alias regions */
  1517. for (i = 0; i < ARRAY_SIZE(t241_dist_base_alias); i++) {
  1518. if (chip_bmask & BIT(i)) {
  1519. phys = gic_data.dist_phys_base + T241_CHIP_GICDA_OFFSET;
  1520. phys |= FIELD_PREP(T241_CHIPN_MASK, i);
  1521. t241_dist_base_alias[i] = ioremap(phys, SZ_64K);
  1522. WARN_ON_ONCE(!t241_dist_base_alias[i]);
  1523. }
  1524. }
  1525. static_branch_enable(&gic_nvidia_t241_erratum);
  1526. return true;
  1527. }
  1528. static bool gic_enable_quirk_asr8601(void *data)
  1529. {
  1530. struct gic_chip_data *d = data;
  1531. d->flags |= FLAGS_WORKAROUND_ASR_ERRATUM_8601001;
  1532. return true;
  1533. }
  1534. static bool gic_enable_quirk_arm64_2941627(void *data)
  1535. {
  1536. static_branch_enable(&gic_arm64_2941627_erratum);
  1537. return true;
  1538. }
  1539. static bool gic_enable_quirk_rk3399(void *data)
  1540. {
  1541. struct gic_chip_data *d = data;
  1542. if (of_machine_is_compatible("rockchip,rk3399")) {
  1543. d->flags |= FLAGS_WORKAROUND_INSECURE;
  1544. return true;
  1545. }
  1546. return false;
  1547. }
  1548. static bool rd_set_non_coherent(void *data)
  1549. {
  1550. struct gic_chip_data *d = data;
  1551. d->rdists.flags |= RDIST_FLAGS_FORCE_NON_SHAREABLE;
  1552. return true;
  1553. }
  1554. static const struct gic_quirk gic_quirks[] = {
  1555. {
  1556. .desc = "GICv3: Qualcomm MSM8996 broken firmware",
  1557. .compatible = "qcom,msm8996-gic-v3",
  1558. .init = gic_enable_quirk_msm8996,
  1559. },
  1560. {
  1561. .desc = "GICv3: ASR erratum 8601001",
  1562. .compatible = "asr,asr8601-gic-v3",
  1563. .init = gic_enable_quirk_asr8601,
  1564. },
  1565. {
  1566. .desc = "GICv3: HIP06 erratum 161010803",
  1567. .iidr = 0x0204043b,
  1568. .mask = 0xffffffff,
  1569. .init = gic_enable_quirk_hip06_07,
  1570. },
  1571. {
  1572. .desc = "GICv3: HIP07 erratum 161010803",
  1573. .iidr = 0x00000000,
  1574. .mask = 0xffffffff,
  1575. .init = gic_enable_quirk_hip06_07,
  1576. },
  1577. {
  1578. /*
  1579. * Reserved register accesses generate a Synchronous
  1580. * External Abort. This erratum applies to:
  1581. * - ThunderX: CN88xx
  1582. * - OCTEON TX: CN83xx, CN81xx
  1583. * - OCTEON TX2: CN93xx, CN96xx, CN98xx, CNF95xx*
  1584. */
  1585. .desc = "GICv3: Cavium erratum 38539",
  1586. .iidr = 0xa000034c,
  1587. .mask = 0xe8f00fff,
  1588. .init = gic_enable_quirk_cavium_38539,
  1589. },
  1590. {
  1591. .desc = "GICv3: NVIDIA erratum T241-FABRIC-4",
  1592. .iidr = 0x0402043b,
  1593. .mask = 0xffffffff,
  1594. .init = gic_enable_quirk_nvidia_t241,
  1595. },
  1596. {
  1597. /*
  1598. * GIC-700: 2941627 workaround - IP variant [0,1]
  1599. *
  1600. */
  1601. .desc = "GICv3: ARM64 erratum 2941627",
  1602. .iidr = 0x0400043b,
  1603. .mask = 0xff0e0fff,
  1604. .init = gic_enable_quirk_arm64_2941627,
  1605. },
  1606. {
  1607. /*
  1608. * GIC-700: 2941627 workaround - IP variant [2]
  1609. */
  1610. .desc = "GICv3: ARM64 erratum 2941627",
  1611. .iidr = 0x0402043b,
  1612. .mask = 0xff0f0fff,
  1613. .init = gic_enable_quirk_arm64_2941627,
  1614. },
  1615. {
  1616. .desc = "GICv3: non-coherent attribute",
  1617. .property = "dma-noncoherent",
  1618. .init = rd_set_non_coherent,
  1619. },
  1620. {
  1621. .desc = "GICv3: Insecure RK3399 integration",
  1622. .iidr = 0x0000043b,
  1623. .mask = 0xff000fff,
  1624. .init = gic_enable_quirk_rk3399,
  1625. },
  1626. {
  1627. }
  1628. };
  1629. static void gic_enable_nmi_support(void)
  1630. {
  1631. if (!gic_prio_masking_enabled() || nmi_support_forbidden)
  1632. return;
  1633. pr_info("Pseudo-NMIs enabled using %s ICC_PMR_EL1 synchronisation\n",
  1634. gic_has_relaxed_pmr_sync() ? "relaxed" : "forced");
  1635. static_branch_enable(&supports_pseudo_nmis);
  1636. if (static_branch_likely(&supports_deactivate_key))
  1637. gic_eoimode1_chip.flags |= IRQCHIP_SUPPORTS_NMI;
  1638. else
  1639. gic_chip.flags |= IRQCHIP_SUPPORTS_NMI;
  1640. }
  1641. static int __init gic_init_bases(phys_addr_t dist_phys_base,
  1642. void __iomem *dist_base,
  1643. struct redist_region *rdist_regs,
  1644. u32 nr_redist_regions,
  1645. u64 redist_stride,
  1646. struct fwnode_handle *handle)
  1647. {
  1648. u32 typer;
  1649. int err;
  1650. if (!is_hyp_mode_available())
  1651. static_branch_disable(&supports_deactivate_key);
  1652. if (static_branch_likely(&supports_deactivate_key))
  1653. pr_info("GIC: Using split EOI/Deactivate mode\n");
  1654. gic_data.fwnode = handle;
  1655. gic_data.dist_phys_base = dist_phys_base;
  1656. gic_data.dist_base = dist_base;
  1657. gic_data.redist_regions = rdist_regs;
  1658. gic_data.nr_redist_regions = nr_redist_regions;
  1659. gic_data.redist_stride = redist_stride;
  1660. /*
  1661. * Find out how many interrupts are supported.
  1662. */
  1663. typer = readl_relaxed(gic_data.dist_base + GICD_TYPER);
  1664. gic_data.rdists.gicd_typer = typer;
  1665. gic_enable_quirks(readl_relaxed(gic_data.dist_base + GICD_IIDR),
  1666. gic_quirks, &gic_data);
  1667. pr_info("%d SPIs implemented\n", GIC_LINE_NR - 32);
  1668. pr_info("%d Extended SPIs implemented\n", GIC_ESPI_NR);
  1669. /*
  1670. * ThunderX1 explodes on reading GICD_TYPER2, in violation of the
  1671. * architecture spec (which says that reserved registers are RES0).
  1672. */
  1673. if (!(gic_data.flags & FLAGS_WORKAROUND_CAVIUM_ERRATUM_38539))
  1674. gic_data.rdists.gicd_typer2 = readl_relaxed(gic_data.dist_base + GICD_TYPER2);
  1675. gic_data.domain = irq_domain_create_tree(handle, &gic_irq_domain_ops,
  1676. &gic_data);
  1677. gic_data.rdists.rdist = alloc_percpu(typeof(*gic_data.rdists.rdist));
  1678. if (!static_branch_unlikely(&gic_nvidia_t241_erratum)) {
  1679. /* Disable GICv4.x features for the erratum T241-FABRIC-4 */
  1680. gic_data.rdists.has_rvpeid = true;
  1681. gic_data.rdists.has_vlpis = true;
  1682. gic_data.rdists.has_direct_lpi = true;
  1683. gic_data.rdists.has_vpend_valid_dirty = true;
  1684. }
  1685. if (WARN_ON(!gic_data.domain) || WARN_ON(!gic_data.rdists.rdist)) {
  1686. err = -ENOMEM;
  1687. goto out_free;
  1688. }
  1689. irq_domain_update_bus_token(gic_data.domain, DOMAIN_BUS_WIRED);
  1690. gic_data.has_rss = !!(typer & GICD_TYPER_RSS);
  1691. if (typer & GICD_TYPER_MBIS) {
  1692. err = mbi_init(handle, gic_data.domain);
  1693. if (err)
  1694. pr_err("Failed to initialize MBIs\n");
  1695. }
  1696. set_handle_irq(gic_handle_irq);
  1697. gic_update_rdist_properties();
  1698. gic_cpu_sys_reg_enable();
  1699. gic_prio_init();
  1700. gic_dist_init();
  1701. gic_cpu_init();
  1702. gic_enable_nmi_support();
  1703. gic_smp_init();
  1704. gic_cpu_pm_init();
  1705. if (gic_dist_supports_lpis()) {
  1706. its_init(handle, &gic_data.rdists, gic_data.domain, dist_prio_irq);
  1707. its_cpu_init();
  1708. its_lpi_memreserve_init();
  1709. } else {
  1710. if (IS_ENABLED(CONFIG_ARM_GIC_V2M))
  1711. gicv2m_init(handle, gic_data.domain);
  1712. }
  1713. return 0;
  1714. out_free:
  1715. if (gic_data.domain)
  1716. irq_domain_remove(gic_data.domain);
  1717. free_percpu(gic_data.rdists.rdist);
  1718. return err;
  1719. }
  1720. static int __init gic_validate_dist_version(void __iomem *dist_base)
  1721. {
  1722. u32 reg = readl_relaxed(dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1723. if (reg != GIC_PIDR2_ARCH_GICv3 && reg != GIC_PIDR2_ARCH_GICv4)
  1724. return -ENODEV;
  1725. return 0;
  1726. }
  1727. /* Create all possible partitions at boot time */
  1728. static void __init gic_populate_ppi_partitions(struct device_node *gic_node)
  1729. {
  1730. struct device_node *parts_node, *child_part;
  1731. int part_idx = 0, i;
  1732. int nr_parts;
  1733. struct partition_affinity *parts;
  1734. parts_node = of_get_child_by_name(gic_node, "ppi-partitions");
  1735. if (!parts_node)
  1736. return;
  1737. nr_parts = of_get_child_count(parts_node);
  1738. if (!nr_parts)
  1739. goto out_put_node;
  1740. parts = kzalloc_objs(*parts, nr_parts);
  1741. if (WARN_ON(!parts))
  1742. goto out_put_node;
  1743. for_each_child_of_node(parts_node, child_part) {
  1744. struct partition_affinity *part;
  1745. int n;
  1746. part = &parts[part_idx];
  1747. part->partition_id = of_fwnode_handle(child_part);
  1748. pr_info("GIC: PPI partition %pOFn[%d] { ",
  1749. child_part, part_idx);
  1750. n = of_property_count_elems_of_size(child_part, "affinity",
  1751. sizeof(u32));
  1752. WARN_ON(n <= 0);
  1753. for (i = 0; i < n; i++) {
  1754. int err, cpu;
  1755. u32 cpu_phandle;
  1756. struct device_node *cpu_node;
  1757. err = of_property_read_u32_index(child_part, "affinity",
  1758. i, &cpu_phandle);
  1759. if (WARN_ON(err))
  1760. continue;
  1761. cpu_node = of_find_node_by_phandle(cpu_phandle);
  1762. if (WARN_ON(!cpu_node))
  1763. continue;
  1764. cpu = of_cpu_node_to_id(cpu_node);
  1765. if (WARN_ON(cpu < 0)) {
  1766. of_node_put(cpu_node);
  1767. continue;
  1768. }
  1769. pr_cont("%pOF[%d] ", cpu_node, cpu);
  1770. cpumask_set_cpu(cpu, &part->mask);
  1771. of_node_put(cpu_node);
  1772. }
  1773. pr_cont("}\n");
  1774. part_idx++;
  1775. }
  1776. gic_data.parts = parts;
  1777. gic_data.nr_parts = nr_parts;
  1778. out_put_node:
  1779. of_node_put(parts_node);
  1780. }
  1781. static void __init gic_of_setup_kvm_info(struct device_node *node, u32 nr_redist_regions)
  1782. {
  1783. int ret;
  1784. struct resource r;
  1785. gic_v3_kvm_info.type = GIC_V3;
  1786. gic_v3_kvm_info.maint_irq = irq_of_parse_and_map(node, 0);
  1787. if (!gic_v3_kvm_info.maint_irq)
  1788. return;
  1789. /* Also skip GICD, GICC, GICH */
  1790. ret = of_address_to_resource(node, nr_redist_regions + 3, &r);
  1791. if (!ret)
  1792. gic_v3_kvm_info.vcpu = r;
  1793. gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
  1794. gic_v3_kvm_info.has_v4_1 = gic_data.rdists.has_rvpeid;
  1795. vgic_set_kvm_info(&gic_v3_kvm_info);
  1796. }
  1797. static void gic_request_region(resource_size_t base, resource_size_t size,
  1798. const char *name)
  1799. {
  1800. if (!request_mem_region(base, size, name))
  1801. pr_warn_once(FW_BUG "%s region %pa has overlapping address\n",
  1802. name, &base);
  1803. }
  1804. static void __iomem *gic_of_iomap(struct device_node *node, int idx,
  1805. const char *name, struct resource *res)
  1806. {
  1807. void __iomem *base;
  1808. int ret;
  1809. ret = of_address_to_resource(node, idx, res);
  1810. if (ret)
  1811. return IOMEM_ERR_PTR(ret);
  1812. gic_request_region(res->start, resource_size(res), name);
  1813. base = of_iomap(node, idx);
  1814. return base ?: IOMEM_ERR_PTR(-ENOMEM);
  1815. }
  1816. static int __init gic_of_init(struct device_node *node, struct device_node *parent)
  1817. {
  1818. phys_addr_t dist_phys_base;
  1819. void __iomem *dist_base;
  1820. struct redist_region *rdist_regs;
  1821. struct resource res;
  1822. u64 redist_stride;
  1823. u32 nr_redist_regions;
  1824. int err, i;
  1825. dist_base = gic_of_iomap(node, 0, "GICD", &res);
  1826. if (IS_ERR(dist_base)) {
  1827. pr_err("%pOF: unable to map gic dist registers\n", node);
  1828. return PTR_ERR(dist_base);
  1829. }
  1830. dist_phys_base = res.start;
  1831. err = gic_validate_dist_version(dist_base);
  1832. if (err) {
  1833. pr_err("%pOF: no distributor detected, giving up\n", node);
  1834. goto out_unmap_dist;
  1835. }
  1836. if (of_property_read_u32(node, "#redistributor-regions", &nr_redist_regions))
  1837. nr_redist_regions = 1;
  1838. rdist_regs = kzalloc_objs(*rdist_regs, nr_redist_regions);
  1839. if (!rdist_regs) {
  1840. err = -ENOMEM;
  1841. goto out_unmap_dist;
  1842. }
  1843. for (i = 0; i < nr_redist_regions; i++) {
  1844. rdist_regs[i].redist_base = gic_of_iomap(node, 1 + i, "GICR", &res);
  1845. if (IS_ERR(rdist_regs[i].redist_base)) {
  1846. pr_err("%pOF: couldn't map region %d\n", node, i);
  1847. err = -ENODEV;
  1848. goto out_unmap_rdist;
  1849. }
  1850. rdist_regs[i].phys_base = res.start;
  1851. }
  1852. if (of_property_read_u64(node, "redistributor-stride", &redist_stride))
  1853. redist_stride = 0;
  1854. gic_enable_of_quirks(node, gic_quirks, &gic_data);
  1855. err = gic_init_bases(dist_phys_base, dist_base, rdist_regs,
  1856. nr_redist_regions, redist_stride, &node->fwnode);
  1857. if (err)
  1858. goto out_unmap_rdist;
  1859. gic_populate_ppi_partitions(node);
  1860. if (static_branch_likely(&supports_deactivate_key))
  1861. gic_of_setup_kvm_info(node, nr_redist_regions);
  1862. return 0;
  1863. out_unmap_rdist:
  1864. for (i = 0; i < nr_redist_regions; i++)
  1865. if (rdist_regs[i].redist_base && !IS_ERR(rdist_regs[i].redist_base))
  1866. iounmap(rdist_regs[i].redist_base);
  1867. kfree(rdist_regs);
  1868. out_unmap_dist:
  1869. iounmap(dist_base);
  1870. return err;
  1871. }
  1872. IRQCHIP_DECLARE(gic_v3, "arm,gic-v3", gic_of_init);
  1873. #ifdef CONFIG_ACPI
  1874. static struct
  1875. {
  1876. void __iomem *dist_base;
  1877. struct redist_region *redist_regs;
  1878. u32 nr_redist_regions;
  1879. bool single_redist;
  1880. int enabled_rdists;
  1881. u32 maint_irq;
  1882. int maint_irq_mode;
  1883. phys_addr_t vcpu_base;
  1884. } acpi_data __initdata;
  1885. static void __init
  1886. gic_acpi_register_redist(phys_addr_t phys_base, void __iomem *redist_base)
  1887. {
  1888. static int count = 0;
  1889. acpi_data.redist_regs[count].phys_base = phys_base;
  1890. acpi_data.redist_regs[count].redist_base = redist_base;
  1891. acpi_data.redist_regs[count].single_redist = acpi_data.single_redist;
  1892. count++;
  1893. }
  1894. static int __init
  1895. gic_acpi_parse_madt_redist(union acpi_subtable_headers *header,
  1896. const unsigned long end)
  1897. {
  1898. struct acpi_madt_generic_redistributor *redist =
  1899. (struct acpi_madt_generic_redistributor *)header;
  1900. void __iomem *redist_base;
  1901. redist_base = ioremap(redist->base_address, redist->length);
  1902. if (!redist_base) {
  1903. pr_err("Couldn't map GICR region @%llx\n", redist->base_address);
  1904. return -ENOMEM;
  1905. }
  1906. if (acpi_get_madt_revision() >= 7 &&
  1907. (redist->flags & ACPI_MADT_GICR_NON_COHERENT))
  1908. gic_data.rdists.flags |= RDIST_FLAGS_FORCE_NON_SHAREABLE;
  1909. gic_request_region(redist->base_address, redist->length, "GICR");
  1910. gic_acpi_register_redist(redist->base_address, redist_base);
  1911. return 0;
  1912. }
  1913. static int __init
  1914. gic_acpi_parse_madt_gicc(union acpi_subtable_headers *header,
  1915. const unsigned long end)
  1916. {
  1917. struct acpi_madt_generic_interrupt *gicc =
  1918. (struct acpi_madt_generic_interrupt *)header;
  1919. u32 reg = readl_relaxed(acpi_data.dist_base + GICD_PIDR2) & GIC_PIDR2_ARCH_MASK;
  1920. u32 size = reg == GIC_PIDR2_ARCH_GICv4 ? SZ_64K * 4 : SZ_64K * 2;
  1921. void __iomem *redist_base;
  1922. /* Neither enabled or online capable means it doesn't exist, skip it */
  1923. if (!(gicc->flags & (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE)))
  1924. return 0;
  1925. /*
  1926. * Capable but disabled CPUs can be brought online later. What about
  1927. * the redistributor? ACPI doesn't want to say!
  1928. * Virtual hotplug systems can use the MADT's "always-on" GICR entries.
  1929. * Otherwise, prevent such CPUs from being brought online.
  1930. */
  1931. if (!(gicc->flags & ACPI_MADT_ENABLED)) {
  1932. int cpu = get_cpu_for_acpi_id(gicc->uid);
  1933. pr_warn("CPU %u's redistributor is inaccessible: this CPU can't be brought online\n", cpu);
  1934. if (cpu >= 0)
  1935. cpumask_set_cpu(cpu, &broken_rdists);
  1936. return 0;
  1937. }
  1938. redist_base = ioremap(gicc->gicr_base_address, size);
  1939. if (!redist_base)
  1940. return -ENOMEM;
  1941. gic_request_region(gicc->gicr_base_address, size, "GICR");
  1942. if (acpi_get_madt_revision() >= 7 &&
  1943. (gicc->flags & ACPI_MADT_GICC_NON_COHERENT))
  1944. gic_data.rdists.flags |= RDIST_FLAGS_FORCE_NON_SHAREABLE;
  1945. gic_acpi_register_redist(gicc->gicr_base_address, redist_base);
  1946. return 0;
  1947. }
  1948. static int __init gic_acpi_collect_gicr_base(void)
  1949. {
  1950. acpi_tbl_entry_handler redist_parser;
  1951. enum acpi_madt_type type;
  1952. if (acpi_data.single_redist) {
  1953. type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
  1954. redist_parser = gic_acpi_parse_madt_gicc;
  1955. } else {
  1956. type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
  1957. redist_parser = gic_acpi_parse_madt_redist;
  1958. }
  1959. /* Collect redistributor base addresses in GICR entries */
  1960. if (acpi_table_parse_madt(type, redist_parser, 0) > 0)
  1961. return 0;
  1962. pr_info("No valid GICR entries exist\n");
  1963. return -ENODEV;
  1964. }
  1965. static int __init gic_acpi_match_gicr(union acpi_subtable_headers *header,
  1966. const unsigned long end)
  1967. {
  1968. /* Subtable presence means that redist exists, that's it */
  1969. return 0;
  1970. }
  1971. static int __init gic_acpi_match_gicc(union acpi_subtable_headers *header,
  1972. const unsigned long end)
  1973. {
  1974. struct acpi_madt_generic_interrupt *gicc =
  1975. (struct acpi_madt_generic_interrupt *)header;
  1976. /*
  1977. * If GICC is enabled and has valid gicr base address, then it means
  1978. * GICR base is presented via GICC. The redistributor is only known to
  1979. * be accessible if the GICC is marked as enabled. If this bit is not
  1980. * set, we'd need to add the redistributor at runtime, which isn't
  1981. * supported.
  1982. */
  1983. if (gicc->flags & ACPI_MADT_ENABLED && gicc->gicr_base_address)
  1984. acpi_data.enabled_rdists++;
  1985. return 0;
  1986. }
  1987. static int __init gic_acpi_count_gicr_regions(void)
  1988. {
  1989. int count;
  1990. /*
  1991. * Count how many redistributor regions we have. It is not allowed
  1992. * to mix redistributor description, GICR and GICC subtables have to be
  1993. * mutually exclusive.
  1994. */
  1995. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR,
  1996. gic_acpi_match_gicr, 0);
  1997. if (count > 0) {
  1998. acpi_data.single_redist = false;
  1999. return count;
  2000. }
  2001. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  2002. gic_acpi_match_gicc, 0);
  2003. if (count > 0) {
  2004. acpi_data.single_redist = true;
  2005. count = acpi_data.enabled_rdists;
  2006. }
  2007. return count;
  2008. }
  2009. static bool __init acpi_validate_gic_table(struct acpi_subtable_header *header,
  2010. struct acpi_probe_entry *ape)
  2011. {
  2012. struct acpi_madt_generic_distributor *dist;
  2013. int count;
  2014. dist = (struct acpi_madt_generic_distributor *)header;
  2015. if (dist->version != ape->driver_data)
  2016. return false;
  2017. /* We need to do that exercise anyway, the sooner the better */
  2018. count = gic_acpi_count_gicr_regions();
  2019. if (count <= 0)
  2020. return false;
  2021. acpi_data.nr_redist_regions = count;
  2022. return true;
  2023. }
  2024. static int __init gic_acpi_parse_virt_madt_gicc(union acpi_subtable_headers *header,
  2025. const unsigned long end)
  2026. {
  2027. struct acpi_madt_generic_interrupt *gicc =
  2028. (struct acpi_madt_generic_interrupt *)header;
  2029. int maint_irq_mode;
  2030. static int first_madt = true;
  2031. if (!(gicc->flags &
  2032. (ACPI_MADT_ENABLED | ACPI_MADT_GICC_ONLINE_CAPABLE)))
  2033. return 0;
  2034. maint_irq_mode = (gicc->flags & ACPI_MADT_VGIC_IRQ_MODE) ?
  2035. ACPI_EDGE_SENSITIVE : ACPI_LEVEL_SENSITIVE;
  2036. if (first_madt) {
  2037. first_madt = false;
  2038. acpi_data.maint_irq = gicc->vgic_interrupt;
  2039. acpi_data.maint_irq_mode = maint_irq_mode;
  2040. acpi_data.vcpu_base = gicc->gicv_base_address;
  2041. return 0;
  2042. }
  2043. /*
  2044. * The maintenance interrupt and GICV should be the same for every CPU
  2045. */
  2046. if ((acpi_data.maint_irq != gicc->vgic_interrupt) ||
  2047. (acpi_data.maint_irq_mode != maint_irq_mode) ||
  2048. (acpi_data.vcpu_base != gicc->gicv_base_address))
  2049. return -EINVAL;
  2050. return 0;
  2051. }
  2052. static bool __init gic_acpi_collect_virt_info(void)
  2053. {
  2054. int count;
  2055. count = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT,
  2056. gic_acpi_parse_virt_madt_gicc, 0);
  2057. return (count > 0);
  2058. }
  2059. #define ACPI_GICV3_DIST_MEM_SIZE (SZ_64K)
  2060. #define ACPI_GICV2_VCTRL_MEM_SIZE (SZ_4K)
  2061. #define ACPI_GICV2_VCPU_MEM_SIZE (SZ_8K)
  2062. static void __init gic_acpi_setup_kvm_info(void)
  2063. {
  2064. int irq;
  2065. if (!gic_acpi_collect_virt_info()) {
  2066. pr_warn("Unable to get hardware information used for virtualization\n");
  2067. return;
  2068. }
  2069. gic_v3_kvm_info.type = GIC_V3;
  2070. irq = acpi_register_gsi(NULL, acpi_data.maint_irq,
  2071. acpi_data.maint_irq_mode,
  2072. ACPI_ACTIVE_HIGH);
  2073. if (irq <= 0)
  2074. return;
  2075. gic_v3_kvm_info.maint_irq = irq;
  2076. if (acpi_data.vcpu_base) {
  2077. struct resource *vcpu = &gic_v3_kvm_info.vcpu;
  2078. vcpu->flags = IORESOURCE_MEM;
  2079. vcpu->start = acpi_data.vcpu_base;
  2080. vcpu->end = vcpu->start + ACPI_GICV2_VCPU_MEM_SIZE - 1;
  2081. }
  2082. gic_v3_kvm_info.has_v4 = gic_data.rdists.has_vlpis;
  2083. gic_v3_kvm_info.has_v4_1 = gic_data.rdists.has_rvpeid;
  2084. vgic_set_kvm_info(&gic_v3_kvm_info);
  2085. }
  2086. static struct fwnode_handle *gsi_domain_handle;
  2087. static struct fwnode_handle *gic_v3_get_gsi_domain_id(u32 gsi)
  2088. {
  2089. return gsi_domain_handle;
  2090. }
  2091. static int __init
  2092. gic_acpi_init(union acpi_subtable_headers *header, const unsigned long end)
  2093. {
  2094. struct acpi_madt_generic_distributor *dist;
  2095. size_t size;
  2096. int i, err;
  2097. /* Get distributor base address */
  2098. dist = (struct acpi_madt_generic_distributor *)header;
  2099. acpi_data.dist_base = ioremap(dist->base_address,
  2100. ACPI_GICV3_DIST_MEM_SIZE);
  2101. if (!acpi_data.dist_base) {
  2102. pr_err("Unable to map GICD registers\n");
  2103. return -ENOMEM;
  2104. }
  2105. gic_request_region(dist->base_address, ACPI_GICV3_DIST_MEM_SIZE, "GICD");
  2106. err = gic_validate_dist_version(acpi_data.dist_base);
  2107. if (err) {
  2108. pr_err("No distributor detected at @%p, giving up\n",
  2109. acpi_data.dist_base);
  2110. goto out_dist_unmap;
  2111. }
  2112. size = sizeof(*acpi_data.redist_regs) * acpi_data.nr_redist_regions;
  2113. acpi_data.redist_regs = kzalloc(size, GFP_KERNEL);
  2114. if (!acpi_data.redist_regs) {
  2115. err = -ENOMEM;
  2116. goto out_dist_unmap;
  2117. }
  2118. err = gic_acpi_collect_gicr_base();
  2119. if (err)
  2120. goto out_redist_unmap;
  2121. gsi_domain_handle = irq_domain_alloc_fwnode(&dist->base_address);
  2122. if (!gsi_domain_handle) {
  2123. err = -ENOMEM;
  2124. goto out_redist_unmap;
  2125. }
  2126. err = gic_init_bases(dist->base_address, acpi_data.dist_base,
  2127. acpi_data.redist_regs, acpi_data.nr_redist_regions,
  2128. 0, gsi_domain_handle);
  2129. if (err)
  2130. goto out_fwhandle_free;
  2131. acpi_set_irq_model(ACPI_IRQ_MODEL_GIC, gic_v3_get_gsi_domain_id);
  2132. if (static_branch_likely(&supports_deactivate_key))
  2133. gic_acpi_setup_kvm_info();
  2134. return 0;
  2135. out_fwhandle_free:
  2136. irq_domain_free_fwnode(gsi_domain_handle);
  2137. out_redist_unmap:
  2138. for (i = 0; i < acpi_data.nr_redist_regions; i++)
  2139. if (acpi_data.redist_regs[i].redist_base)
  2140. iounmap(acpi_data.redist_regs[i].redist_base);
  2141. kfree(acpi_data.redist_regs);
  2142. out_dist_unmap:
  2143. iounmap(acpi_data.dist_base);
  2144. return err;
  2145. }
  2146. IRQCHIP_ACPI_DECLARE(gic_v3, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  2147. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V3,
  2148. gic_acpi_init);
  2149. IRQCHIP_ACPI_DECLARE(gic_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  2150. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_V4,
  2151. gic_acpi_init);
  2152. IRQCHIP_ACPI_DECLARE(gic_v3_or_v4, ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR,
  2153. acpi_validate_gic_table, ACPI_MADT_GIC_VERSION_NONE,
  2154. gic_acpi_init);
  2155. #endif