irq-gic-v3.c 68 KB

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