cpufeature.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Contains CPU feature definitions
  4. *
  5. * Copyright (C) 2015 ARM Ltd.
  6. *
  7. * A note for the weary kernel hacker: the code here is confusing and hard to
  8. * follow! That's partly because it's solving a nasty problem, but also because
  9. * there's a little bit of over-abstraction that tends to obscure what's going
  10. * on behind a maze of helper functions and macros.
  11. *
  12. * The basic problem is that hardware folks have started gluing together CPUs
  13. * with distinct architectural features; in some cases even creating SoCs where
  14. * user-visible instructions are available only on a subset of the available
  15. * cores. We try to address this by snapshotting the feature registers of the
  16. * boot CPU and comparing these with the feature registers of each secondary
  17. * CPU when bringing them up. If there is a mismatch, then we update the
  18. * snapshot state to indicate the lowest-common denominator of the feature,
  19. * known as the "safe" value. This snapshot state can be queried to view the
  20. * "sanitised" value of a feature register.
  21. *
  22. * The sanitised register values are used to decide which capabilities we
  23. * have in the system. These may be in the form of traditional "hwcaps"
  24. * advertised to userspace or internal "cpucaps" which are used to configure
  25. * things like alternative patching and static keys. While a feature mismatch
  26. * may result in a TAINT_CPU_OUT_OF_SPEC kernel taint, a capability mismatch
  27. * may prevent a CPU from being onlined at all.
  28. *
  29. * Some implementation details worth remembering:
  30. *
  31. * - Mismatched features are *always* sanitised to a "safe" value, which
  32. * usually indicates that the feature is not supported.
  33. *
  34. * - A mismatched feature marked with FTR_STRICT will cause a "SANITY CHECK"
  35. * warning when onlining an offending CPU and the kernel will be tainted
  36. * with TAINT_CPU_OUT_OF_SPEC.
  37. *
  38. * - Features marked as FTR_VISIBLE have their sanitised value visible to
  39. * userspace. FTR_VISIBLE features in registers that are only visible
  40. * to EL0 by trapping *must* have a corresponding HWCAP so that late
  41. * onlining of CPUs cannot lead to features disappearing at runtime.
  42. *
  43. * - A "feature" is typically a 4-bit register field. A "capability" is the
  44. * high-level description derived from the sanitised field value.
  45. *
  46. * - Read the Arm ARM (DDI 0487F.a) section D13.1.3 ("Principles of the ID
  47. * scheme for fields in ID registers") to understand when feature fields
  48. * may be signed or unsigned (FTR_SIGNED and FTR_UNSIGNED accordingly).
  49. *
  50. * - KVM exposes its own view of the feature registers to guest operating
  51. * systems regardless of FTR_VISIBLE. This is typically driven from the
  52. * sanitised register values to allow virtual CPUs to be migrated between
  53. * arbitrary physical CPUs, but some features not present on the host are
  54. * also advertised and emulated. Look at sys_reg_descs[] for the gory
  55. * details.
  56. *
  57. * - If the arm64_ftr_bits[] for a register has a missing field, then this
  58. * field is treated as STRICT RES0, including for read_sanitised_ftr_reg().
  59. * This is stronger than FTR_HIDDEN and can be used to hide features from
  60. * KVM guests.
  61. */
  62. #define pr_fmt(fmt) "CPU features: " fmt
  63. #include <linux/bsearch.h>
  64. #include <linux/cpumask.h>
  65. #include <linux/crash_dump.h>
  66. #include <linux/sort.h>
  67. #include <linux/stop_machine.h>
  68. #include <linux/sysfs.h>
  69. #include <linux/types.h>
  70. #include <linux/minmax.h>
  71. #include <linux/mm.h>
  72. #include <linux/cpu.h>
  73. #include <linux/kasan.h>
  74. #include <linux/percpu.h>
  75. #include <asm/cpu.h>
  76. #include <asm/cpufeature.h>
  77. #include <asm/cpu_ops.h>
  78. #include <asm/fpsimd.h>
  79. #include <asm/hwcap.h>
  80. #include <asm/insn.h>
  81. #include <asm/kvm_host.h>
  82. #include <asm/mmu_context.h>
  83. #include <asm/mte.h>
  84. #include <asm/processor.h>
  85. #include <asm/smp.h>
  86. #include <asm/sysreg.h>
  87. #include <asm/traps.h>
  88. #include <asm/vectors.h>
  89. #include <asm/virt.h>
  90. /* Kernel representation of AT_HWCAP and AT_HWCAP2 */
  91. static DECLARE_BITMAP(elf_hwcap, MAX_CPU_FEATURES) __read_mostly;
  92. #ifdef CONFIG_COMPAT
  93. #define COMPAT_ELF_HWCAP_DEFAULT \
  94. (COMPAT_HWCAP_HALF|COMPAT_HWCAP_THUMB|\
  95. COMPAT_HWCAP_FAST_MULT|COMPAT_HWCAP_EDSP|\
  96. COMPAT_HWCAP_TLS|COMPAT_HWCAP_IDIV|\
  97. COMPAT_HWCAP_LPAE)
  98. unsigned int compat_elf_hwcap __read_mostly = COMPAT_ELF_HWCAP_DEFAULT;
  99. unsigned int compat_elf_hwcap2 __read_mostly;
  100. #endif
  101. DECLARE_BITMAP(cpu_hwcaps, ARM64_NCAPS);
  102. EXPORT_SYMBOL(cpu_hwcaps);
  103. static struct arm64_cpu_capabilities const __ro_after_init *cpu_hwcaps_ptrs[ARM64_NCAPS];
  104. DECLARE_BITMAP(boot_capabilities, ARM64_NCAPS);
  105. bool arm64_use_ng_mappings = false;
  106. EXPORT_SYMBOL(arm64_use_ng_mappings);
  107. DEFINE_PER_CPU_READ_MOSTLY(const char *, this_cpu_vector) = vectors;
  108. /*
  109. * Permit PER_LINUX32 and execve() of 32-bit binaries even if not all CPUs
  110. * support it?
  111. */
  112. static bool __read_mostly allow_mismatched_32bit_el0;
  113. /*
  114. * Static branch enabled only if allow_mismatched_32bit_el0 is set and we have
  115. * seen at least one CPU capable of 32-bit EL0.
  116. */
  117. DEFINE_STATIC_KEY_FALSE(arm64_mismatched_32bit_el0);
  118. /*
  119. * Mask of CPUs supporting 32-bit EL0.
  120. * Only valid if arm64_mismatched_32bit_el0 is enabled.
  121. */
  122. static cpumask_var_t cpu_32bit_el0_mask __cpumask_var_read_mostly;
  123. void dump_cpu_features(void)
  124. {
  125. /* file-wide pr_fmt adds "CPU features: " prefix */
  126. pr_emerg("0x%*pb\n", ARM64_NCAPS, &cpu_hwcaps);
  127. }
  128. #define __ARM64_FTR_BITS(SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
  129. { \
  130. .sign = SIGNED, \
  131. .visible = VISIBLE, \
  132. .strict = STRICT, \
  133. .type = TYPE, \
  134. .shift = SHIFT, \
  135. .width = WIDTH, \
  136. .safe_val = SAFE_VAL, \
  137. }
  138. /* Define a feature with unsigned values */
  139. #define ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
  140. __ARM64_FTR_BITS(FTR_UNSIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
  141. /* Define a feature with a signed value */
  142. #define S_ARM64_FTR_BITS(VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL) \
  143. __ARM64_FTR_BITS(FTR_SIGNED, VISIBLE, STRICT, TYPE, SHIFT, WIDTH, SAFE_VAL)
  144. #define ARM64_FTR_END \
  145. { \
  146. .width = 0, \
  147. }
  148. static void cpu_enable_cnp(struct arm64_cpu_capabilities const *cap);
  149. static bool __system_matches_cap(unsigned int n);
  150. /*
  151. * NOTE: Any changes to the visibility of features should be kept in
  152. * sync with the documentation of the CPU feature register ABI.
  153. */
  154. static const struct arm64_ftr_bits ftr_id_aa64isar0[] = {
  155. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_RNDR_SHIFT, 4, 0),
  156. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_TLB_SHIFT, 4, 0),
  157. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_TS_SHIFT, 4, 0),
  158. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_FHM_SHIFT, 4, 0),
  159. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_DP_SHIFT, 4, 0),
  160. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SM4_SHIFT, 4, 0),
  161. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SM3_SHIFT, 4, 0),
  162. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA3_SHIFT, 4, 0),
  163. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_RDM_SHIFT, 4, 0),
  164. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_ATOMIC_SHIFT, 4, 0),
  165. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_CRC32_SHIFT, 4, 0),
  166. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA2_SHIFT, 4, 0),
  167. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_SHA1_SHIFT, 4, 0),
  168. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR0_EL1_AES_SHIFT, 4, 0),
  169. ARM64_FTR_END,
  170. };
  171. static const struct arm64_ftr_bits ftr_id_aa64isar1[] = {
  172. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_I8MM_SHIFT, 4, 0),
  173. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_DGH_SHIFT, 4, 0),
  174. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_BF16_SHIFT, 4, 0),
  175. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_SPECRES_SHIFT, 4, 0),
  176. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_SB_SHIFT, 4, 0),
  177. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_FRINTTS_SHIFT, 4, 0),
  178. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
  179. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_GPI_SHIFT, 4, 0),
  180. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
  181. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_GPA_SHIFT, 4, 0),
  182. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_LRCPC_SHIFT, 4, 0),
  183. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_FCMA_SHIFT, 4, 0),
  184. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_JSCVT_SHIFT, 4, 0),
  185. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
  186. FTR_STRICT, FTR_EXACT, ID_AA64ISAR1_EL1_API_SHIFT, 4, 0),
  187. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
  188. FTR_STRICT, FTR_EXACT, ID_AA64ISAR1_EL1_APA_SHIFT, 4, 0),
  189. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR1_EL1_DPB_SHIFT, 4, 0),
  190. ARM64_FTR_END,
  191. };
  192. static const struct arm64_ftr_bits ftr_id_aa64isar2[] = {
  193. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_CLRBHB_SHIFT, 4, 0),
  194. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_BC_SHIFT, 4, 0),
  195. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
  196. FTR_STRICT, FTR_EXACT, ID_AA64ISAR2_EL1_APA3_SHIFT, 4, 0),
  197. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_PTR_AUTH),
  198. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_GPA3_SHIFT, 4, 0),
  199. ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_RPRES_SHIFT, 4, 0),
  200. ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64ISAR2_EL1_WFxT_SHIFT, 4, 0),
  201. ARM64_FTR_END,
  202. };
  203. static const struct arm64_ftr_bits ftr_id_aa64pfr0[] = {
  204. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV3_SHIFT, 4, 0),
  205. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_CSV2_SHIFT, 4, 0),
  206. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_DIT_SHIFT, 4, 0),
  207. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AMU_SHIFT, 4, 0),
  208. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_MPAM_SHIFT, 4, 0),
  209. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SEL2_SHIFT, 4, 0),
  210. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  211. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_SVE_SHIFT, 4, 0),
  212. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_RAS_SHIFT, 4, 0),
  213. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_GIC_SHIFT, 4, 0),
  214. S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_AdvSIMD_SHIFT, 4, ID_AA64PFR0_EL1_AdvSIMD_NI),
  215. S_ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_FP_SHIFT, 4, ID_AA64PFR0_EL1_FP_NI),
  216. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL3_SHIFT, 4, 0),
  217. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL2_SHIFT, 4, 0),
  218. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL1_SHIFT, 4, ID_AA64PFR0_EL1_ELx_64BIT_ONLY),
  219. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR0_EL1_EL0_SHIFT, 4, ID_AA64PFR0_EL1_ELx_64BIT_ONLY),
  220. ARM64_FTR_END,
  221. };
  222. static const struct arm64_ftr_bits ftr_id_aa64pfr1[] = {
  223. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  224. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_SME_SHIFT, 4, 0),
  225. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_MPAM_frac_SHIFT, 4, 0),
  226. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_RAS_frac_SHIFT, 4, 0),
  227. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_MTE),
  228. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_MTE_SHIFT, 4, ID_AA64PFR1_EL1_MTE_NI),
  229. ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_SSBS_SHIFT, 4, ID_AA64PFR1_EL1_SSBS_NI),
  230. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_BTI),
  231. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64PFR1_EL1_BT_SHIFT, 4, 0),
  232. ARM64_FTR_END,
  233. };
  234. static const struct arm64_ftr_bits ftr_id_aa64zfr0[] = {
  235. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  236. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_F64MM_SHIFT, 4, 0),
  237. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  238. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_F32MM_SHIFT, 4, 0),
  239. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  240. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_I8MM_SHIFT, 4, 0),
  241. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  242. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SM4_SHIFT, 4, 0),
  243. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  244. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SHA3_SHIFT, 4, 0),
  245. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  246. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_BF16_SHIFT, 4, 0),
  247. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  248. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_BitPerm_SHIFT, 4, 0),
  249. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  250. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_AES_SHIFT, 4, 0),
  251. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SVE),
  252. FTR_STRICT, FTR_LOWER_SAFE, ID_AA64ZFR0_EL1_SVEver_SHIFT, 4, 0),
  253. ARM64_FTR_END,
  254. };
  255. static const struct arm64_ftr_bits ftr_id_aa64smfr0[] = {
  256. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  257. FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_FA64_SHIFT, 1, 0),
  258. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  259. FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_I16I64_SHIFT, 4, 0),
  260. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  261. FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F64F64_SHIFT, 1, 0),
  262. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  263. FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_I8I32_SHIFT, 4, 0),
  264. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  265. FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F16F32_SHIFT, 1, 0),
  266. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  267. FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_B16F32_SHIFT, 1, 0),
  268. ARM64_FTR_BITS(FTR_VISIBLE_IF_IS_ENABLED(CONFIG_ARM64_SME),
  269. FTR_STRICT, FTR_EXACT, ID_AA64SMFR0_EL1_F32F32_SHIFT, 1, 0),
  270. ARM64_FTR_END,
  271. };
  272. static const struct arm64_ftr_bits ftr_id_aa64mmfr0[] = {
  273. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_ECV_SHIFT, 4, 0),
  274. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_FGT_SHIFT, 4, 0),
  275. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_EXS_SHIFT, 4, 0),
  276. /*
  277. * Page size not being supported at Stage-2 is not fatal. You
  278. * just give up KVM if PAGE_SIZE isn't supported there. Go fix
  279. * your favourite nesting hypervisor.
  280. *
  281. * There is a small corner case where the hypervisor explicitly
  282. * advertises a given granule size at Stage-2 (value 2) on some
  283. * vCPUs, and uses the fallback to Stage-1 (value 0) for other
  284. * vCPUs. Although this is not forbidden by the architecture, it
  285. * indicates that the hypervisor is being silly (or buggy).
  286. *
  287. * We make no effort to cope with this and pretend that if these
  288. * fields are inconsistent across vCPUs, then it isn't worth
  289. * trying to bring KVM up.
  290. */
  291. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN4_2_SHIFT, 4, 1),
  292. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN64_2_SHIFT, 4, 1),
  293. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64MMFR0_EL1_TGRAN16_2_SHIFT, 4, 1),
  294. /*
  295. * We already refuse to boot CPUs that don't support our configured
  296. * page size, so we can only detect mismatches for a page size other
  297. * than the one we're currently using. Unfortunately, SoCs like this
  298. * exist in the wild so, even though we don't like it, we'll have to go
  299. * along with it and treat them as non-strict.
  300. */
  301. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN4_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN4_NI),
  302. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN64_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN64_NI),
  303. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_TGRAN16_SHIFT, 4, ID_AA64MMFR0_EL1_TGRAN16_NI),
  304. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_BIGENDEL0_SHIFT, 4, 0),
  305. /* Linux shouldn't care about secure memory */
  306. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_SNSMEM_SHIFT, 4, 0),
  307. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_BIGEND_SHIFT, 4, 0),
  308. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_ASIDBITS_SHIFT, 4, 0),
  309. /*
  310. * Differing PARange is fine as long as all peripherals and memory are mapped
  311. * within the minimum PARange of all CPUs
  312. */
  313. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR0_EL1_PARANGE_SHIFT, 4, 0),
  314. ARM64_FTR_END,
  315. };
  316. static const struct arm64_ftr_bits ftr_id_aa64mmfr1[] = {
  317. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_TIDCP1_SHIFT, 4, 0),
  318. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_AFP_SHIFT, 4, 0),
  319. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_ETS_SHIFT, 4, 0),
  320. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_TWED_SHIFT, 4, 0),
  321. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_XNX_SHIFT, 4, 0),
  322. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_AA64MMFR1_EL1_SpecSEI_SHIFT, 4, 0),
  323. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_PAN_SHIFT, 4, 0),
  324. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_LO_SHIFT, 4, 0),
  325. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_HPDS_SHIFT, 4, 0),
  326. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_VH_SHIFT, 4, 0),
  327. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_VMIDBits_SHIFT, 4, 0),
  328. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR1_EL1_HAFDBS_SHIFT, 4, 0),
  329. ARM64_FTR_END,
  330. };
  331. static const struct arm64_ftr_bits ftr_id_aa64mmfr2[] = {
  332. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_E0PD_SHIFT, 4, 0),
  333. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_EVT_SHIFT, 4, 0),
  334. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_BBM_SHIFT, 4, 0),
  335. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_TTL_SHIFT, 4, 0),
  336. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_FWB_SHIFT, 4, 0),
  337. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_IDS_SHIFT, 4, 0),
  338. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_AT_SHIFT, 4, 0),
  339. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_ST_SHIFT, 4, 0),
  340. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_NV_SHIFT, 4, 0),
  341. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_CCIDX_SHIFT, 4, 0),
  342. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_VARange_SHIFT, 4, 0),
  343. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_IESB_SHIFT, 4, 0),
  344. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_LSM_SHIFT, 4, 0),
  345. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_UAO_SHIFT, 4, 0),
  346. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64MMFR2_EL1_CnP_SHIFT, 4, 0),
  347. ARM64_FTR_END,
  348. };
  349. static const struct arm64_ftr_bits ftr_ctr[] = {
  350. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, 31, 1, 1), /* RES1 */
  351. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_DIC_SHIFT, 1, 1),
  352. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_IDC_SHIFT, 1, 1),
  353. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_EL0_CWG_SHIFT, 4, 0),
  354. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_HIGHER_OR_ZERO_SAFE, CTR_EL0_ERG_SHIFT, 4, 0),
  355. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_DminLine_SHIFT, 4, 1),
  356. /*
  357. * Linux can handle differing I-cache policies. Userspace JITs will
  358. * make use of *minLine.
  359. * If we have differing I-cache policies, report it as the weakest - VIPT.
  360. */
  361. ARM64_FTR_BITS(FTR_VISIBLE, FTR_NONSTRICT, FTR_EXACT, CTR_EL0_L1Ip_SHIFT, 2, CTR_EL0_L1Ip_VIPT), /* L1Ip */
  362. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, CTR_EL0_IminLine_SHIFT, 4, 0),
  363. ARM64_FTR_END,
  364. };
  365. static struct arm64_ftr_override __ro_after_init no_override = { };
  366. struct arm64_ftr_reg arm64_ftr_reg_ctrel0 = {
  367. .name = "SYS_CTR_EL0",
  368. .ftr_bits = ftr_ctr,
  369. .override = &no_override,
  370. };
  371. static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
  372. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_INNERSHR_SHIFT, 4, 0xf),
  373. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_FCSE_SHIFT, 4, 0),
  374. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_MMFR0_AUXREG_SHIFT, 4, 0),
  375. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_TCM_SHIFT, 4, 0),
  376. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_SHARELVL_SHIFT, 4, 0),
  377. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_OUTERSHR_SHIFT, 4, 0xf),
  378. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_PMSA_SHIFT, 4, 0),
  379. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR0_VMSA_SHIFT, 4, 0),
  380. ARM64_FTR_END,
  381. };
  382. static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
  383. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_DoubleLock_SHIFT, 4, 0),
  384. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_PMSVer_SHIFT, 4, 0),
  385. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_CTX_CMPs_SHIFT, 4, 0),
  386. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_WRPs_SHIFT, 4, 0),
  387. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_BRPs_SHIFT, 4, 0),
  388. /*
  389. * We can instantiate multiple PMU instances with different levels
  390. * of support.
  391. */
  392. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_AA64DFR0_EL1_PMUVer_SHIFT, 4, 0),
  393. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, ID_AA64DFR0_EL1_DebugVer_SHIFT, 4, 0x6),
  394. ARM64_FTR_END,
  395. };
  396. static const struct arm64_ftr_bits ftr_mvfr0[] = {
  397. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_FPROUND_SHIFT, 4, 0),
  398. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_FPSHVEC_SHIFT, 4, 0),
  399. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_FPSQRT_SHIFT, 4, 0),
  400. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_FPDIVIDE_SHIFT, 4, 0),
  401. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_FPTRAP_SHIFT, 4, 0),
  402. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_FPDP_SHIFT, 4, 0),
  403. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_FPSP_SHIFT, 4, 0),
  404. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR0_SIMD_SHIFT, 4, 0),
  405. ARM64_FTR_END,
  406. };
  407. static const struct arm64_ftr_bits ftr_mvfr1[] = {
  408. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_SIMDFMAC_SHIFT, 4, 0),
  409. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_FPHP_SHIFT, 4, 0),
  410. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_SIMDHP_SHIFT, 4, 0),
  411. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_SIMDSP_SHIFT, 4, 0),
  412. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_SIMDINT_SHIFT, 4, 0),
  413. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_SIMDLS_SHIFT, 4, 0),
  414. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_FPDNAN_SHIFT, 4, 0),
  415. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR1_FPFTZ_SHIFT, 4, 0),
  416. ARM64_FTR_END,
  417. };
  418. static const struct arm64_ftr_bits ftr_mvfr2[] = {
  419. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR2_FPMISC_SHIFT, 4, 0),
  420. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, MVFR2_SIMDMISC_SHIFT, 4, 0),
  421. ARM64_FTR_END,
  422. };
  423. static const struct arm64_ftr_bits ftr_dczid[] = {
  424. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_EXACT, DCZID_EL0_DZP_SHIFT, 1, 1),
  425. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, DCZID_EL0_BS_SHIFT, 4, 0),
  426. ARM64_FTR_END,
  427. };
  428. static const struct arm64_ftr_bits ftr_gmid[] = {
  429. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, GMID_EL1_BS_SHIFT, 4, 0),
  430. ARM64_FTR_END,
  431. };
  432. static const struct arm64_ftr_bits ftr_id_isar0[] = {
  433. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_DIVIDE_SHIFT, 4, 0),
  434. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_DEBUG_SHIFT, 4, 0),
  435. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_COPROC_SHIFT, 4, 0),
  436. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_CMPBRANCH_SHIFT, 4, 0),
  437. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_BITFIELD_SHIFT, 4, 0),
  438. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_BITCOUNT_SHIFT, 4, 0),
  439. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR0_SWAP_SHIFT, 4, 0),
  440. ARM64_FTR_END,
  441. };
  442. static const struct arm64_ftr_bits ftr_id_isar5[] = {
  443. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_RDM_SHIFT, 4, 0),
  444. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_CRC32_SHIFT, 4, 0),
  445. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA2_SHIFT, 4, 0),
  446. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SHA1_SHIFT, 4, 0),
  447. ARM64_FTR_BITS(FTR_VISIBLE, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_AES_SHIFT, 4, 0),
  448. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR5_SEVL_SHIFT, 4, 0),
  449. ARM64_FTR_END,
  450. };
  451. static const struct arm64_ftr_bits ftr_id_mmfr4[] = {
  452. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_EVT_SHIFT, 4, 0),
  453. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_CCIDX_SHIFT, 4, 0),
  454. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_LSM_SHIFT, 4, 0),
  455. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_HPDS_SHIFT, 4, 0),
  456. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_CNP_SHIFT, 4, 0),
  457. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_XNX_SHIFT, 4, 0),
  458. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR4_AC2_SHIFT, 4, 0),
  459. /*
  460. * SpecSEI = 1 indicates that the PE might generate an SError on an
  461. * external abort on speculative read. It is safe to assume that an
  462. * SError might be generated than it will not be. Hence it has been
  463. * classified as FTR_HIGHER_SAFE.
  464. */
  465. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_HIGHER_SAFE, ID_MMFR4_SPECSEI_SHIFT, 4, 0),
  466. ARM64_FTR_END,
  467. };
  468. static const struct arm64_ftr_bits ftr_id_isar4[] = {
  469. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SWP_FRAC_SHIFT, 4, 0),
  470. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_PSR_M_SHIFT, 4, 0),
  471. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SYNCH_PRIM_FRAC_SHIFT, 4, 0),
  472. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_BARRIER_SHIFT, 4, 0),
  473. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_SMC_SHIFT, 4, 0),
  474. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WRITEBACK_SHIFT, 4, 0),
  475. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_WITHSHIFTS_SHIFT, 4, 0),
  476. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR4_UNPRIV_SHIFT, 4, 0),
  477. ARM64_FTR_END,
  478. };
  479. static const struct arm64_ftr_bits ftr_id_mmfr5[] = {
  480. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_MMFR5_ETS_SHIFT, 4, 0),
  481. ARM64_FTR_END,
  482. };
  483. static const struct arm64_ftr_bits ftr_id_isar6[] = {
  484. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_I8MM_SHIFT, 4, 0),
  485. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_BF16_SHIFT, 4, 0),
  486. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SPECRES_SHIFT, 4, 0),
  487. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_SB_SHIFT, 4, 0),
  488. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_FHM_SHIFT, 4, 0),
  489. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_DP_SHIFT, 4, 0),
  490. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_ISAR6_JSCVT_SHIFT, 4, 0),
  491. ARM64_FTR_END,
  492. };
  493. static const struct arm64_ftr_bits ftr_id_pfr0[] = {
  494. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_DIT_SHIFT, 4, 0),
  495. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR0_CSV2_SHIFT, 4, 0),
  496. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_STATE3_SHIFT, 4, 0),
  497. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_STATE2_SHIFT, 4, 0),
  498. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_STATE1_SHIFT, 4, 0),
  499. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR0_STATE0_SHIFT, 4, 0),
  500. ARM64_FTR_END,
  501. };
  502. static const struct arm64_ftr_bits ftr_id_pfr1[] = {
  503. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GIC_SHIFT, 4, 0),
  504. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRT_FRAC_SHIFT, 4, 0),
  505. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SEC_FRAC_SHIFT, 4, 0),
  506. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_GENTIMER_SHIFT, 4, 0),
  507. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_VIRTUALIZATION_SHIFT, 4, 0),
  508. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_MPROGMOD_SHIFT, 4, 0),
  509. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_SECURITY_SHIFT, 4, 0),
  510. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_PFR1_PROGMOD_SHIFT, 4, 0),
  511. ARM64_FTR_END,
  512. };
  513. static const struct arm64_ftr_bits ftr_id_pfr2[] = {
  514. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR2_SSBS_SHIFT, 4, 0),
  515. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_PFR2_CSV3_SHIFT, 4, 0),
  516. ARM64_FTR_END,
  517. };
  518. static const struct arm64_ftr_bits ftr_id_dfr0[] = {
  519. /* [31:28] TraceFilt */
  520. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_EXACT, ID_DFR0_PERFMON_SHIFT, 4, 0),
  521. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_MPROFDBG_SHIFT, 4, 0),
  522. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_MMAPTRC_SHIFT, 4, 0),
  523. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_COPTRC_SHIFT, 4, 0),
  524. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_MMAPDBG_SHIFT, 4, 0),
  525. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_COPSDBG_SHIFT, 4, 0),
  526. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR0_COPDBG_SHIFT, 4, 0),
  527. ARM64_FTR_END,
  528. };
  529. static const struct arm64_ftr_bits ftr_id_dfr1[] = {
  530. S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_DFR1_MTPMU_SHIFT, 4, 0),
  531. ARM64_FTR_END,
  532. };
  533. static const struct arm64_ftr_bits ftr_zcr[] = {
  534. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
  535. ZCR_ELx_LEN_SHIFT, ZCR_ELx_LEN_WIDTH, 0), /* LEN */
  536. ARM64_FTR_END,
  537. };
  538. static const struct arm64_ftr_bits ftr_smcr[] = {
  539. ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE,
  540. SMCR_ELx_LEN_SHIFT, SMCR_ELx_LEN_WIDTH, 0), /* LEN */
  541. ARM64_FTR_END,
  542. };
  543. /*
  544. * Common ftr bits for a 32bit register with all hidden, strict
  545. * attributes, with 4bit feature fields and a default safe value of
  546. * 0. Covers the following 32bit registers:
  547. * id_isar[1-3], id_mmfr[1-3]
  548. */
  549. static const struct arm64_ftr_bits ftr_generic_32bits[] = {
  550. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 28, 4, 0),
  551. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 24, 4, 0),
  552. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 20, 4, 0),
  553. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 16, 4, 0),
  554. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 12, 4, 0),
  555. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 8, 4, 0),
  556. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 4, 4, 0),
  557. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, 0, 4, 0),
  558. ARM64_FTR_END,
  559. };
  560. /* Table for a single 32bit feature value */
  561. static const struct arm64_ftr_bits ftr_single32[] = {
  562. ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_EXACT, 0, 32, 0),
  563. ARM64_FTR_END,
  564. };
  565. static const struct arm64_ftr_bits ftr_raz[] = {
  566. ARM64_FTR_END,
  567. };
  568. #define __ARM64_FTR_REG_OVERRIDE(id_str, id, table, ovr) { \
  569. .sys_id = id, \
  570. .reg = &(struct arm64_ftr_reg){ \
  571. .name = id_str, \
  572. .override = (ovr), \
  573. .ftr_bits = &((table)[0]), \
  574. }}
  575. #define ARM64_FTR_REG_OVERRIDE(id, table, ovr) \
  576. __ARM64_FTR_REG_OVERRIDE(#id, id, table, ovr)
  577. #define ARM64_FTR_REG(id, table) \
  578. __ARM64_FTR_REG_OVERRIDE(#id, id, table, &no_override)
  579. struct arm64_ftr_override __ro_after_init id_aa64mmfr1_override;
  580. struct arm64_ftr_override __ro_after_init id_aa64pfr0_override;
  581. struct arm64_ftr_override __ro_after_init id_aa64pfr1_override;
  582. struct arm64_ftr_override __ro_after_init id_aa64zfr0_override;
  583. struct arm64_ftr_override __ro_after_init id_aa64smfr0_override;
  584. struct arm64_ftr_override __ro_after_init id_aa64isar1_override;
  585. struct arm64_ftr_override __ro_after_init id_aa64isar2_override;
  586. static const struct __ftr_reg_entry {
  587. u32 sys_id;
  588. struct arm64_ftr_reg *reg;
  589. } arm64_ftr_regs[] = {
  590. /* Op1 = 0, CRn = 0, CRm = 1 */
  591. ARM64_FTR_REG(SYS_ID_PFR0_EL1, ftr_id_pfr0),
  592. ARM64_FTR_REG(SYS_ID_PFR1_EL1, ftr_id_pfr1),
  593. ARM64_FTR_REG(SYS_ID_DFR0_EL1, ftr_id_dfr0),
  594. ARM64_FTR_REG(SYS_ID_MMFR0_EL1, ftr_id_mmfr0),
  595. ARM64_FTR_REG(SYS_ID_MMFR1_EL1, ftr_generic_32bits),
  596. ARM64_FTR_REG(SYS_ID_MMFR2_EL1, ftr_generic_32bits),
  597. ARM64_FTR_REG(SYS_ID_MMFR3_EL1, ftr_generic_32bits),
  598. /* Op1 = 0, CRn = 0, CRm = 2 */
  599. ARM64_FTR_REG(SYS_ID_ISAR0_EL1, ftr_id_isar0),
  600. ARM64_FTR_REG(SYS_ID_ISAR1_EL1, ftr_generic_32bits),
  601. ARM64_FTR_REG(SYS_ID_ISAR2_EL1, ftr_generic_32bits),
  602. ARM64_FTR_REG(SYS_ID_ISAR3_EL1, ftr_generic_32bits),
  603. ARM64_FTR_REG(SYS_ID_ISAR4_EL1, ftr_id_isar4),
  604. ARM64_FTR_REG(SYS_ID_ISAR5_EL1, ftr_id_isar5),
  605. ARM64_FTR_REG(SYS_ID_MMFR4_EL1, ftr_id_mmfr4),
  606. ARM64_FTR_REG(SYS_ID_ISAR6_EL1, ftr_id_isar6),
  607. /* Op1 = 0, CRn = 0, CRm = 3 */
  608. ARM64_FTR_REG(SYS_MVFR0_EL1, ftr_mvfr0),
  609. ARM64_FTR_REG(SYS_MVFR1_EL1, ftr_mvfr1),
  610. ARM64_FTR_REG(SYS_MVFR2_EL1, ftr_mvfr2),
  611. ARM64_FTR_REG(SYS_ID_PFR2_EL1, ftr_id_pfr2),
  612. ARM64_FTR_REG(SYS_ID_DFR1_EL1, ftr_id_dfr1),
  613. ARM64_FTR_REG(SYS_ID_MMFR5_EL1, ftr_id_mmfr5),
  614. /* Op1 = 0, CRn = 0, CRm = 4 */
  615. ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64PFR0_EL1, ftr_id_aa64pfr0,
  616. &id_aa64pfr0_override),
  617. ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64PFR1_EL1, ftr_id_aa64pfr1,
  618. &id_aa64pfr1_override),
  619. ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ZFR0_EL1, ftr_id_aa64zfr0,
  620. &id_aa64zfr0_override),
  621. ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64SMFR0_EL1, ftr_id_aa64smfr0,
  622. &id_aa64smfr0_override),
  623. /* Op1 = 0, CRn = 0, CRm = 5 */
  624. ARM64_FTR_REG(SYS_ID_AA64DFR0_EL1, ftr_id_aa64dfr0),
  625. ARM64_FTR_REG(SYS_ID_AA64DFR1_EL1, ftr_raz),
  626. /* Op1 = 0, CRn = 0, CRm = 6 */
  627. ARM64_FTR_REG(SYS_ID_AA64ISAR0_EL1, ftr_id_aa64isar0),
  628. ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ISAR1_EL1, ftr_id_aa64isar1,
  629. &id_aa64isar1_override),
  630. ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64ISAR2_EL1, ftr_id_aa64isar2,
  631. &id_aa64isar2_override),
  632. /* Op1 = 0, CRn = 0, CRm = 7 */
  633. ARM64_FTR_REG(SYS_ID_AA64MMFR0_EL1, ftr_id_aa64mmfr0),
  634. ARM64_FTR_REG_OVERRIDE(SYS_ID_AA64MMFR1_EL1, ftr_id_aa64mmfr1,
  635. &id_aa64mmfr1_override),
  636. ARM64_FTR_REG(SYS_ID_AA64MMFR2_EL1, ftr_id_aa64mmfr2),
  637. /* Op1 = 0, CRn = 1, CRm = 2 */
  638. ARM64_FTR_REG(SYS_ZCR_EL1, ftr_zcr),
  639. ARM64_FTR_REG(SYS_SMCR_EL1, ftr_smcr),
  640. /* Op1 = 1, CRn = 0, CRm = 0 */
  641. ARM64_FTR_REG(SYS_GMID_EL1, ftr_gmid),
  642. /* Op1 = 3, CRn = 0, CRm = 0 */
  643. { SYS_CTR_EL0, &arm64_ftr_reg_ctrel0 },
  644. ARM64_FTR_REG(SYS_DCZID_EL0, ftr_dczid),
  645. /* Op1 = 3, CRn = 14, CRm = 0 */
  646. ARM64_FTR_REG(SYS_CNTFRQ_EL0, ftr_single32),
  647. };
  648. static int search_cmp_ftr_reg(const void *id, const void *regp)
  649. {
  650. return (int)(unsigned long)id - (int)((const struct __ftr_reg_entry *)regp)->sys_id;
  651. }
  652. /*
  653. * get_arm64_ftr_reg_nowarn - Looks up a feature register entry using
  654. * its sys_reg() encoding. With the array arm64_ftr_regs sorted in the
  655. * ascending order of sys_id, we use binary search to find a matching
  656. * entry.
  657. *
  658. * returns - Upon success, matching ftr_reg entry for id.
  659. * - NULL on failure. It is upto the caller to decide
  660. * the impact of a failure.
  661. */
  662. static struct arm64_ftr_reg *get_arm64_ftr_reg_nowarn(u32 sys_id)
  663. {
  664. const struct __ftr_reg_entry *ret;
  665. ret = bsearch((const void *)(unsigned long)sys_id,
  666. arm64_ftr_regs,
  667. ARRAY_SIZE(arm64_ftr_regs),
  668. sizeof(arm64_ftr_regs[0]),
  669. search_cmp_ftr_reg);
  670. if (ret)
  671. return ret->reg;
  672. return NULL;
  673. }
  674. /*
  675. * get_arm64_ftr_reg - Looks up a feature register entry using
  676. * its sys_reg() encoding. This calls get_arm64_ftr_reg_nowarn().
  677. *
  678. * returns - Upon success, matching ftr_reg entry for id.
  679. * - NULL on failure but with an WARN_ON().
  680. */
  681. struct arm64_ftr_reg *get_arm64_ftr_reg(u32 sys_id)
  682. {
  683. struct arm64_ftr_reg *reg;
  684. reg = get_arm64_ftr_reg_nowarn(sys_id);
  685. /*
  686. * Requesting a non-existent register search is an error. Warn
  687. * and let the caller handle it.
  688. */
  689. WARN_ON(!reg);
  690. return reg;
  691. }
  692. static u64 arm64_ftr_set_value(const struct arm64_ftr_bits *ftrp, s64 reg,
  693. s64 ftr_val)
  694. {
  695. u64 mask = arm64_ftr_mask(ftrp);
  696. reg &= ~mask;
  697. reg |= (ftr_val << ftrp->shift) & mask;
  698. return reg;
  699. }
  700. static s64 arm64_ftr_safe_value(const struct arm64_ftr_bits *ftrp, s64 new,
  701. s64 cur)
  702. {
  703. s64 ret = 0;
  704. switch (ftrp->type) {
  705. case FTR_EXACT:
  706. ret = ftrp->safe_val;
  707. break;
  708. case FTR_LOWER_SAFE:
  709. ret = min(new, cur);
  710. break;
  711. case FTR_HIGHER_OR_ZERO_SAFE:
  712. if (!cur || !new)
  713. break;
  714. fallthrough;
  715. case FTR_HIGHER_SAFE:
  716. ret = max(new, cur);
  717. break;
  718. default:
  719. BUG();
  720. }
  721. return ret;
  722. }
  723. static void __init sort_ftr_regs(void)
  724. {
  725. unsigned int i;
  726. for (i = 0; i < ARRAY_SIZE(arm64_ftr_regs); i++) {
  727. const struct arm64_ftr_reg *ftr_reg = arm64_ftr_regs[i].reg;
  728. const struct arm64_ftr_bits *ftr_bits = ftr_reg->ftr_bits;
  729. unsigned int j = 0;
  730. /*
  731. * Features here must be sorted in descending order with respect
  732. * to their shift values and should not overlap with each other.
  733. */
  734. for (; ftr_bits->width != 0; ftr_bits++, j++) {
  735. unsigned int width = ftr_reg->ftr_bits[j].width;
  736. unsigned int shift = ftr_reg->ftr_bits[j].shift;
  737. unsigned int prev_shift;
  738. WARN((shift + width) > 64,
  739. "%s has invalid feature at shift %d\n",
  740. ftr_reg->name, shift);
  741. /*
  742. * Skip the first feature. There is nothing to
  743. * compare against for now.
  744. */
  745. if (j == 0)
  746. continue;
  747. prev_shift = ftr_reg->ftr_bits[j - 1].shift;
  748. WARN((shift + width) > prev_shift,
  749. "%s has feature overlap at shift %d\n",
  750. ftr_reg->name, shift);
  751. }
  752. /*
  753. * Skip the first register. There is nothing to
  754. * compare against for now.
  755. */
  756. if (i == 0)
  757. continue;
  758. /*
  759. * Registers here must be sorted in ascending order with respect
  760. * to sys_id for subsequent binary search in get_arm64_ftr_reg()
  761. * to work correctly.
  762. */
  763. BUG_ON(arm64_ftr_regs[i].sys_id <= arm64_ftr_regs[i - 1].sys_id);
  764. }
  765. }
  766. /*
  767. * Initialise the CPU feature register from Boot CPU values.
  768. * Also initiliases the strict_mask for the register.
  769. * Any bits that are not covered by an arm64_ftr_bits entry are considered
  770. * RES0 for the system-wide value, and must strictly match.
  771. */
  772. static void init_cpu_ftr_reg(u32 sys_reg, u64 new)
  773. {
  774. u64 val = 0;
  775. u64 strict_mask = ~0x0ULL;
  776. u64 user_mask = 0;
  777. u64 valid_mask = 0;
  778. const struct arm64_ftr_bits *ftrp;
  779. struct arm64_ftr_reg *reg = get_arm64_ftr_reg(sys_reg);
  780. if (!reg)
  781. return;
  782. for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
  783. u64 ftr_mask = arm64_ftr_mask(ftrp);
  784. s64 ftr_new = arm64_ftr_value(ftrp, new);
  785. s64 ftr_ovr = arm64_ftr_value(ftrp, reg->override->val);
  786. if ((ftr_mask & reg->override->mask) == ftr_mask) {
  787. s64 tmp = arm64_ftr_safe_value(ftrp, ftr_ovr, ftr_new);
  788. char *str = NULL;
  789. if (ftr_ovr != tmp) {
  790. /* Unsafe, remove the override */
  791. reg->override->mask &= ~ftr_mask;
  792. reg->override->val &= ~ftr_mask;
  793. tmp = ftr_ovr;
  794. str = "ignoring override";
  795. } else if (ftr_new != tmp) {
  796. /* Override was valid */
  797. ftr_new = tmp;
  798. str = "forced";
  799. } else if (ftr_ovr == tmp) {
  800. /* Override was the safe value */
  801. str = "already set";
  802. }
  803. if (str)
  804. pr_warn("%s[%d:%d]: %s to %llx\n",
  805. reg->name,
  806. ftrp->shift + ftrp->width - 1,
  807. ftrp->shift, str, tmp);
  808. } else if ((ftr_mask & reg->override->val) == ftr_mask) {
  809. reg->override->val &= ~ftr_mask;
  810. pr_warn("%s[%d:%d]: impossible override, ignored\n",
  811. reg->name,
  812. ftrp->shift + ftrp->width - 1,
  813. ftrp->shift);
  814. }
  815. val = arm64_ftr_set_value(ftrp, val, ftr_new);
  816. valid_mask |= ftr_mask;
  817. if (!ftrp->strict)
  818. strict_mask &= ~ftr_mask;
  819. if (ftrp->visible)
  820. user_mask |= ftr_mask;
  821. else
  822. reg->user_val = arm64_ftr_set_value(ftrp,
  823. reg->user_val,
  824. ftrp->safe_val);
  825. }
  826. val &= valid_mask;
  827. reg->sys_val = val;
  828. reg->strict_mask = strict_mask;
  829. reg->user_mask = user_mask;
  830. }
  831. extern const struct arm64_cpu_capabilities arm64_errata[];
  832. static const struct arm64_cpu_capabilities arm64_features[];
  833. static void __init
  834. init_cpu_hwcaps_indirect_list_from_array(const struct arm64_cpu_capabilities *caps)
  835. {
  836. for (; caps->matches; caps++) {
  837. if (WARN(caps->capability >= ARM64_NCAPS,
  838. "Invalid capability %d\n", caps->capability))
  839. continue;
  840. if (WARN(cpu_hwcaps_ptrs[caps->capability],
  841. "Duplicate entry for capability %d\n",
  842. caps->capability))
  843. continue;
  844. cpu_hwcaps_ptrs[caps->capability] = caps;
  845. }
  846. }
  847. static void __init init_cpu_hwcaps_indirect_list(void)
  848. {
  849. init_cpu_hwcaps_indirect_list_from_array(arm64_features);
  850. init_cpu_hwcaps_indirect_list_from_array(arm64_errata);
  851. }
  852. static void __init setup_boot_cpu_capabilities(void);
  853. static void init_32bit_cpu_features(struct cpuinfo_32bit *info)
  854. {
  855. init_cpu_ftr_reg(SYS_ID_DFR0_EL1, info->reg_id_dfr0);
  856. init_cpu_ftr_reg(SYS_ID_DFR1_EL1, info->reg_id_dfr1);
  857. init_cpu_ftr_reg(SYS_ID_ISAR0_EL1, info->reg_id_isar0);
  858. init_cpu_ftr_reg(SYS_ID_ISAR1_EL1, info->reg_id_isar1);
  859. init_cpu_ftr_reg(SYS_ID_ISAR2_EL1, info->reg_id_isar2);
  860. init_cpu_ftr_reg(SYS_ID_ISAR3_EL1, info->reg_id_isar3);
  861. init_cpu_ftr_reg(SYS_ID_ISAR4_EL1, info->reg_id_isar4);
  862. init_cpu_ftr_reg(SYS_ID_ISAR5_EL1, info->reg_id_isar5);
  863. init_cpu_ftr_reg(SYS_ID_ISAR6_EL1, info->reg_id_isar6);
  864. init_cpu_ftr_reg(SYS_ID_MMFR0_EL1, info->reg_id_mmfr0);
  865. init_cpu_ftr_reg(SYS_ID_MMFR1_EL1, info->reg_id_mmfr1);
  866. init_cpu_ftr_reg(SYS_ID_MMFR2_EL1, info->reg_id_mmfr2);
  867. init_cpu_ftr_reg(SYS_ID_MMFR3_EL1, info->reg_id_mmfr3);
  868. init_cpu_ftr_reg(SYS_ID_MMFR4_EL1, info->reg_id_mmfr4);
  869. init_cpu_ftr_reg(SYS_ID_MMFR5_EL1, info->reg_id_mmfr5);
  870. init_cpu_ftr_reg(SYS_ID_PFR0_EL1, info->reg_id_pfr0);
  871. init_cpu_ftr_reg(SYS_ID_PFR1_EL1, info->reg_id_pfr1);
  872. init_cpu_ftr_reg(SYS_ID_PFR2_EL1, info->reg_id_pfr2);
  873. init_cpu_ftr_reg(SYS_MVFR0_EL1, info->reg_mvfr0);
  874. init_cpu_ftr_reg(SYS_MVFR1_EL1, info->reg_mvfr1);
  875. init_cpu_ftr_reg(SYS_MVFR2_EL1, info->reg_mvfr2);
  876. }
  877. void __init init_cpu_features(struct cpuinfo_arm64 *info)
  878. {
  879. /* Before we start using the tables, make sure it is sorted */
  880. sort_ftr_regs();
  881. init_cpu_ftr_reg(SYS_CTR_EL0, info->reg_ctr);
  882. init_cpu_ftr_reg(SYS_DCZID_EL0, info->reg_dczid);
  883. init_cpu_ftr_reg(SYS_CNTFRQ_EL0, info->reg_cntfrq);
  884. init_cpu_ftr_reg(SYS_ID_AA64DFR0_EL1, info->reg_id_aa64dfr0);
  885. init_cpu_ftr_reg(SYS_ID_AA64DFR1_EL1, info->reg_id_aa64dfr1);
  886. init_cpu_ftr_reg(SYS_ID_AA64ISAR0_EL1, info->reg_id_aa64isar0);
  887. init_cpu_ftr_reg(SYS_ID_AA64ISAR1_EL1, info->reg_id_aa64isar1);
  888. init_cpu_ftr_reg(SYS_ID_AA64ISAR2_EL1, info->reg_id_aa64isar2);
  889. init_cpu_ftr_reg(SYS_ID_AA64MMFR0_EL1, info->reg_id_aa64mmfr0);
  890. init_cpu_ftr_reg(SYS_ID_AA64MMFR1_EL1, info->reg_id_aa64mmfr1);
  891. init_cpu_ftr_reg(SYS_ID_AA64MMFR2_EL1, info->reg_id_aa64mmfr2);
  892. init_cpu_ftr_reg(SYS_ID_AA64PFR0_EL1, info->reg_id_aa64pfr0);
  893. init_cpu_ftr_reg(SYS_ID_AA64PFR1_EL1, info->reg_id_aa64pfr1);
  894. init_cpu_ftr_reg(SYS_ID_AA64ZFR0_EL1, info->reg_id_aa64zfr0);
  895. init_cpu_ftr_reg(SYS_ID_AA64SMFR0_EL1, info->reg_id_aa64smfr0);
  896. if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0))
  897. init_32bit_cpu_features(&info->aarch32);
  898. if (IS_ENABLED(CONFIG_ARM64_SVE) &&
  899. id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1))) {
  900. info->reg_zcr = read_zcr_features();
  901. init_cpu_ftr_reg(SYS_ZCR_EL1, info->reg_zcr);
  902. vec_init_vq_map(ARM64_VEC_SVE);
  903. }
  904. if (IS_ENABLED(CONFIG_ARM64_SME) &&
  905. id_aa64pfr1_sme(read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1))) {
  906. info->reg_smcr = read_smcr_features();
  907. /*
  908. * We mask out SMPS since even if the hardware
  909. * supports priorities the kernel does not at present
  910. * and we block access to them.
  911. */
  912. info->reg_smidr = read_cpuid(SMIDR_EL1) & ~SMIDR_EL1_SMPS;
  913. init_cpu_ftr_reg(SYS_SMCR_EL1, info->reg_smcr);
  914. vec_init_vq_map(ARM64_VEC_SME);
  915. }
  916. if (id_aa64pfr1_mte(info->reg_id_aa64pfr1))
  917. init_cpu_ftr_reg(SYS_GMID_EL1, info->reg_gmid);
  918. /*
  919. * Initialize the indirect array of CPU hwcaps capabilities pointers
  920. * before we handle the boot CPU below.
  921. */
  922. init_cpu_hwcaps_indirect_list();
  923. /*
  924. * Detect and enable early CPU capabilities based on the boot CPU,
  925. * after we have initialised the CPU feature infrastructure.
  926. */
  927. setup_boot_cpu_capabilities();
  928. }
  929. static void update_cpu_ftr_reg(struct arm64_ftr_reg *reg, u64 new)
  930. {
  931. const struct arm64_ftr_bits *ftrp;
  932. for (ftrp = reg->ftr_bits; ftrp->width; ftrp++) {
  933. s64 ftr_cur = arm64_ftr_value(ftrp, reg->sys_val);
  934. s64 ftr_new = arm64_ftr_value(ftrp, new);
  935. if (ftr_cur == ftr_new)
  936. continue;
  937. /* Find a safe value */
  938. ftr_new = arm64_ftr_safe_value(ftrp, ftr_new, ftr_cur);
  939. reg->sys_val = arm64_ftr_set_value(ftrp, reg->sys_val, ftr_new);
  940. }
  941. }
  942. static int check_update_ftr_reg(u32 sys_id, int cpu, u64 val, u64 boot)
  943. {
  944. struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
  945. if (!regp)
  946. return 0;
  947. update_cpu_ftr_reg(regp, val);
  948. if ((boot & regp->strict_mask) == (val & regp->strict_mask))
  949. return 0;
  950. pr_warn("SANITY CHECK: Unexpected variation in %s. Boot CPU: %#016llx, CPU%d: %#016llx\n",
  951. regp->name, boot, cpu, val);
  952. return 1;
  953. }
  954. static void relax_cpu_ftr_reg(u32 sys_id, int field)
  955. {
  956. const struct arm64_ftr_bits *ftrp;
  957. struct arm64_ftr_reg *regp = get_arm64_ftr_reg(sys_id);
  958. if (!regp)
  959. return;
  960. for (ftrp = regp->ftr_bits; ftrp->width; ftrp++) {
  961. if (ftrp->shift == field) {
  962. regp->strict_mask &= ~arm64_ftr_mask(ftrp);
  963. break;
  964. }
  965. }
  966. /* Bogus field? */
  967. WARN_ON(!ftrp->width);
  968. }
  969. static void lazy_init_32bit_cpu_features(struct cpuinfo_arm64 *info,
  970. struct cpuinfo_arm64 *boot)
  971. {
  972. static bool boot_cpu_32bit_regs_overridden = false;
  973. if (!allow_mismatched_32bit_el0 || boot_cpu_32bit_regs_overridden)
  974. return;
  975. if (id_aa64pfr0_32bit_el0(boot->reg_id_aa64pfr0))
  976. return;
  977. boot->aarch32 = info->aarch32;
  978. init_32bit_cpu_features(&boot->aarch32);
  979. boot_cpu_32bit_regs_overridden = true;
  980. }
  981. static int update_32bit_cpu_features(int cpu, struct cpuinfo_32bit *info,
  982. struct cpuinfo_32bit *boot)
  983. {
  984. int taint = 0;
  985. u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
  986. /*
  987. * If we don't have AArch32 at EL1, then relax the strictness of
  988. * EL1-dependent register fields to avoid spurious sanity check fails.
  989. */
  990. if (!id_aa64pfr0_32bit_el1(pfr0)) {
  991. relax_cpu_ftr_reg(SYS_ID_ISAR4_EL1, ID_ISAR4_SMC_SHIFT);
  992. relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRT_FRAC_SHIFT);
  993. relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SEC_FRAC_SHIFT);
  994. relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_VIRTUALIZATION_SHIFT);
  995. relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_SECURITY_SHIFT);
  996. relax_cpu_ftr_reg(SYS_ID_PFR1_EL1, ID_PFR1_PROGMOD_SHIFT);
  997. }
  998. taint |= check_update_ftr_reg(SYS_ID_DFR0_EL1, cpu,
  999. info->reg_id_dfr0, boot->reg_id_dfr0);
  1000. taint |= check_update_ftr_reg(SYS_ID_DFR1_EL1, cpu,
  1001. info->reg_id_dfr1, boot->reg_id_dfr1);
  1002. taint |= check_update_ftr_reg(SYS_ID_ISAR0_EL1, cpu,
  1003. info->reg_id_isar0, boot->reg_id_isar0);
  1004. taint |= check_update_ftr_reg(SYS_ID_ISAR1_EL1, cpu,
  1005. info->reg_id_isar1, boot->reg_id_isar1);
  1006. taint |= check_update_ftr_reg(SYS_ID_ISAR2_EL1, cpu,
  1007. info->reg_id_isar2, boot->reg_id_isar2);
  1008. taint |= check_update_ftr_reg(SYS_ID_ISAR3_EL1, cpu,
  1009. info->reg_id_isar3, boot->reg_id_isar3);
  1010. taint |= check_update_ftr_reg(SYS_ID_ISAR4_EL1, cpu,
  1011. info->reg_id_isar4, boot->reg_id_isar4);
  1012. taint |= check_update_ftr_reg(SYS_ID_ISAR5_EL1, cpu,
  1013. info->reg_id_isar5, boot->reg_id_isar5);
  1014. taint |= check_update_ftr_reg(SYS_ID_ISAR6_EL1, cpu,
  1015. info->reg_id_isar6, boot->reg_id_isar6);
  1016. /*
  1017. * Regardless of the value of the AuxReg field, the AIFSR, ADFSR, and
  1018. * ACTLR formats could differ across CPUs and therefore would have to
  1019. * be trapped for virtualization anyway.
  1020. */
  1021. taint |= check_update_ftr_reg(SYS_ID_MMFR0_EL1, cpu,
  1022. info->reg_id_mmfr0, boot->reg_id_mmfr0);
  1023. taint |= check_update_ftr_reg(SYS_ID_MMFR1_EL1, cpu,
  1024. info->reg_id_mmfr1, boot->reg_id_mmfr1);
  1025. taint |= check_update_ftr_reg(SYS_ID_MMFR2_EL1, cpu,
  1026. info->reg_id_mmfr2, boot->reg_id_mmfr2);
  1027. taint |= check_update_ftr_reg(SYS_ID_MMFR3_EL1, cpu,
  1028. info->reg_id_mmfr3, boot->reg_id_mmfr3);
  1029. taint |= check_update_ftr_reg(SYS_ID_MMFR4_EL1, cpu,
  1030. info->reg_id_mmfr4, boot->reg_id_mmfr4);
  1031. taint |= check_update_ftr_reg(SYS_ID_MMFR5_EL1, cpu,
  1032. info->reg_id_mmfr5, boot->reg_id_mmfr5);
  1033. taint |= check_update_ftr_reg(SYS_ID_PFR0_EL1, cpu,
  1034. info->reg_id_pfr0, boot->reg_id_pfr0);
  1035. taint |= check_update_ftr_reg(SYS_ID_PFR1_EL1, cpu,
  1036. info->reg_id_pfr1, boot->reg_id_pfr1);
  1037. taint |= check_update_ftr_reg(SYS_ID_PFR2_EL1, cpu,
  1038. info->reg_id_pfr2, boot->reg_id_pfr2);
  1039. taint |= check_update_ftr_reg(SYS_MVFR0_EL1, cpu,
  1040. info->reg_mvfr0, boot->reg_mvfr0);
  1041. taint |= check_update_ftr_reg(SYS_MVFR1_EL1, cpu,
  1042. info->reg_mvfr1, boot->reg_mvfr1);
  1043. taint |= check_update_ftr_reg(SYS_MVFR2_EL1, cpu,
  1044. info->reg_mvfr2, boot->reg_mvfr2);
  1045. return taint;
  1046. }
  1047. /*
  1048. * Update system wide CPU feature registers with the values from a
  1049. * non-boot CPU. Also performs SANITY checks to make sure that there
  1050. * aren't any insane variations from that of the boot CPU.
  1051. */
  1052. void update_cpu_features(int cpu,
  1053. struct cpuinfo_arm64 *info,
  1054. struct cpuinfo_arm64 *boot)
  1055. {
  1056. int taint = 0;
  1057. /*
  1058. * The kernel can handle differing I-cache policies, but otherwise
  1059. * caches should look identical. Userspace JITs will make use of
  1060. * *minLine.
  1061. */
  1062. taint |= check_update_ftr_reg(SYS_CTR_EL0, cpu,
  1063. info->reg_ctr, boot->reg_ctr);
  1064. /*
  1065. * Userspace may perform DC ZVA instructions. Mismatched block sizes
  1066. * could result in too much or too little memory being zeroed if a
  1067. * process is preempted and migrated between CPUs.
  1068. */
  1069. taint |= check_update_ftr_reg(SYS_DCZID_EL0, cpu,
  1070. info->reg_dczid, boot->reg_dczid);
  1071. /* If different, timekeeping will be broken (especially with KVM) */
  1072. taint |= check_update_ftr_reg(SYS_CNTFRQ_EL0, cpu,
  1073. info->reg_cntfrq, boot->reg_cntfrq);
  1074. /*
  1075. * The kernel uses self-hosted debug features and expects CPUs to
  1076. * support identical debug features. We presently need CTX_CMPs, WRPs,
  1077. * and BRPs to be identical.
  1078. * ID_AA64DFR1 is currently RES0.
  1079. */
  1080. taint |= check_update_ftr_reg(SYS_ID_AA64DFR0_EL1, cpu,
  1081. info->reg_id_aa64dfr0, boot->reg_id_aa64dfr0);
  1082. taint |= check_update_ftr_reg(SYS_ID_AA64DFR1_EL1, cpu,
  1083. info->reg_id_aa64dfr1, boot->reg_id_aa64dfr1);
  1084. /*
  1085. * Even in big.LITTLE, processors should be identical instruction-set
  1086. * wise.
  1087. */
  1088. taint |= check_update_ftr_reg(SYS_ID_AA64ISAR0_EL1, cpu,
  1089. info->reg_id_aa64isar0, boot->reg_id_aa64isar0);
  1090. taint |= check_update_ftr_reg(SYS_ID_AA64ISAR1_EL1, cpu,
  1091. info->reg_id_aa64isar1, boot->reg_id_aa64isar1);
  1092. taint |= check_update_ftr_reg(SYS_ID_AA64ISAR2_EL1, cpu,
  1093. info->reg_id_aa64isar2, boot->reg_id_aa64isar2);
  1094. /*
  1095. * Differing PARange support is fine as long as all peripherals and
  1096. * memory are mapped within the minimum PARange of all CPUs.
  1097. * Linux should not care about secure memory.
  1098. */
  1099. taint |= check_update_ftr_reg(SYS_ID_AA64MMFR0_EL1, cpu,
  1100. info->reg_id_aa64mmfr0, boot->reg_id_aa64mmfr0);
  1101. taint |= check_update_ftr_reg(SYS_ID_AA64MMFR1_EL1, cpu,
  1102. info->reg_id_aa64mmfr1, boot->reg_id_aa64mmfr1);
  1103. taint |= check_update_ftr_reg(SYS_ID_AA64MMFR2_EL1, cpu,
  1104. info->reg_id_aa64mmfr2, boot->reg_id_aa64mmfr2);
  1105. taint |= check_update_ftr_reg(SYS_ID_AA64PFR0_EL1, cpu,
  1106. info->reg_id_aa64pfr0, boot->reg_id_aa64pfr0);
  1107. taint |= check_update_ftr_reg(SYS_ID_AA64PFR1_EL1, cpu,
  1108. info->reg_id_aa64pfr1, boot->reg_id_aa64pfr1);
  1109. taint |= check_update_ftr_reg(SYS_ID_AA64ZFR0_EL1, cpu,
  1110. info->reg_id_aa64zfr0, boot->reg_id_aa64zfr0);
  1111. taint |= check_update_ftr_reg(SYS_ID_AA64SMFR0_EL1, cpu,
  1112. info->reg_id_aa64smfr0, boot->reg_id_aa64smfr0);
  1113. if (IS_ENABLED(CONFIG_ARM64_SVE) &&
  1114. id_aa64pfr0_sve(read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1))) {
  1115. info->reg_zcr = read_zcr_features();
  1116. taint |= check_update_ftr_reg(SYS_ZCR_EL1, cpu,
  1117. info->reg_zcr, boot->reg_zcr);
  1118. /* Probe vector lengths */
  1119. if (!system_capabilities_finalized())
  1120. vec_update_vq_map(ARM64_VEC_SVE);
  1121. }
  1122. if (IS_ENABLED(CONFIG_ARM64_SME) &&
  1123. id_aa64pfr1_sme(read_sanitised_ftr_reg(SYS_ID_AA64PFR1_EL1))) {
  1124. info->reg_smcr = read_smcr_features();
  1125. /*
  1126. * We mask out SMPS since even if the hardware
  1127. * supports priorities the kernel does not at present
  1128. * and we block access to them.
  1129. */
  1130. info->reg_smidr = read_cpuid(SMIDR_EL1) & ~SMIDR_EL1_SMPS;
  1131. taint |= check_update_ftr_reg(SYS_SMCR_EL1, cpu,
  1132. info->reg_smcr, boot->reg_smcr);
  1133. /* Probe vector lengths */
  1134. if (!system_capabilities_finalized())
  1135. vec_update_vq_map(ARM64_VEC_SME);
  1136. }
  1137. /*
  1138. * The kernel uses the LDGM/STGM instructions and the number of tags
  1139. * they read/write depends on the GMID_EL1.BS field. Check that the
  1140. * value is the same on all CPUs.
  1141. */
  1142. if (IS_ENABLED(CONFIG_ARM64_MTE) &&
  1143. id_aa64pfr1_mte(info->reg_id_aa64pfr1)) {
  1144. taint |= check_update_ftr_reg(SYS_GMID_EL1, cpu,
  1145. info->reg_gmid, boot->reg_gmid);
  1146. }
  1147. /*
  1148. * If we don't have AArch32 at all then skip the checks entirely
  1149. * as the register values may be UNKNOWN and we're not going to be
  1150. * using them for anything.
  1151. *
  1152. * This relies on a sanitised view of the AArch64 ID registers
  1153. * (e.g. SYS_ID_AA64PFR0_EL1), so we call it last.
  1154. */
  1155. if (id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0)) {
  1156. lazy_init_32bit_cpu_features(info, boot);
  1157. taint |= update_32bit_cpu_features(cpu, &info->aarch32,
  1158. &boot->aarch32);
  1159. }
  1160. /*
  1161. * Mismatched CPU features are a recipe for disaster. Don't even
  1162. * pretend to support them.
  1163. */
  1164. if (taint) {
  1165. pr_warn_once("Unsupported CPU feature variation detected.\n");
  1166. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  1167. }
  1168. }
  1169. u64 read_sanitised_ftr_reg(u32 id)
  1170. {
  1171. struct arm64_ftr_reg *regp = get_arm64_ftr_reg(id);
  1172. if (!regp)
  1173. return 0;
  1174. return regp->sys_val;
  1175. }
  1176. EXPORT_SYMBOL_GPL(read_sanitised_ftr_reg);
  1177. #define read_sysreg_case(r) \
  1178. case r: val = read_sysreg_s(r); break;
  1179. /*
  1180. * __read_sysreg_by_encoding() - Used by a STARTING cpu before cpuinfo is populated.
  1181. * Read the system register on the current CPU
  1182. */
  1183. u64 __read_sysreg_by_encoding(u32 sys_id)
  1184. {
  1185. struct arm64_ftr_reg *regp;
  1186. u64 val;
  1187. switch (sys_id) {
  1188. read_sysreg_case(SYS_ID_PFR0_EL1);
  1189. read_sysreg_case(SYS_ID_PFR1_EL1);
  1190. read_sysreg_case(SYS_ID_PFR2_EL1);
  1191. read_sysreg_case(SYS_ID_DFR0_EL1);
  1192. read_sysreg_case(SYS_ID_DFR1_EL1);
  1193. read_sysreg_case(SYS_ID_MMFR0_EL1);
  1194. read_sysreg_case(SYS_ID_MMFR1_EL1);
  1195. read_sysreg_case(SYS_ID_MMFR2_EL1);
  1196. read_sysreg_case(SYS_ID_MMFR3_EL1);
  1197. read_sysreg_case(SYS_ID_MMFR4_EL1);
  1198. read_sysreg_case(SYS_ID_MMFR5_EL1);
  1199. read_sysreg_case(SYS_ID_ISAR0_EL1);
  1200. read_sysreg_case(SYS_ID_ISAR1_EL1);
  1201. read_sysreg_case(SYS_ID_ISAR2_EL1);
  1202. read_sysreg_case(SYS_ID_ISAR3_EL1);
  1203. read_sysreg_case(SYS_ID_ISAR4_EL1);
  1204. read_sysreg_case(SYS_ID_ISAR5_EL1);
  1205. read_sysreg_case(SYS_ID_ISAR6_EL1);
  1206. read_sysreg_case(SYS_MVFR0_EL1);
  1207. read_sysreg_case(SYS_MVFR1_EL1);
  1208. read_sysreg_case(SYS_MVFR2_EL1);
  1209. read_sysreg_case(SYS_ID_AA64PFR0_EL1);
  1210. read_sysreg_case(SYS_ID_AA64PFR1_EL1);
  1211. read_sysreg_case(SYS_ID_AA64ZFR0_EL1);
  1212. read_sysreg_case(SYS_ID_AA64SMFR0_EL1);
  1213. read_sysreg_case(SYS_ID_AA64DFR0_EL1);
  1214. read_sysreg_case(SYS_ID_AA64DFR1_EL1);
  1215. read_sysreg_case(SYS_ID_AA64MMFR0_EL1);
  1216. read_sysreg_case(SYS_ID_AA64MMFR1_EL1);
  1217. read_sysreg_case(SYS_ID_AA64MMFR2_EL1);
  1218. read_sysreg_case(SYS_ID_AA64ISAR0_EL1);
  1219. read_sysreg_case(SYS_ID_AA64ISAR1_EL1);
  1220. read_sysreg_case(SYS_ID_AA64ISAR2_EL1);
  1221. read_sysreg_case(SYS_CNTFRQ_EL0);
  1222. read_sysreg_case(SYS_CTR_EL0);
  1223. read_sysreg_case(SYS_DCZID_EL0);
  1224. default:
  1225. BUG();
  1226. return 0;
  1227. }
  1228. regp = get_arm64_ftr_reg(sys_id);
  1229. if (regp) {
  1230. val &= ~regp->override->mask;
  1231. val |= (regp->override->val & regp->override->mask);
  1232. }
  1233. return val;
  1234. }
  1235. #include <linux/irqchip/arm-gic-v3.h>
  1236. static bool
  1237. has_always(const struct arm64_cpu_capabilities *entry, int scope)
  1238. {
  1239. return true;
  1240. }
  1241. static bool
  1242. feature_matches(u64 reg, const struct arm64_cpu_capabilities *entry)
  1243. {
  1244. int val = cpuid_feature_extract_field_width(reg, entry->field_pos,
  1245. entry->field_width,
  1246. entry->sign);
  1247. return val >= entry->min_field_value;
  1248. }
  1249. static u64
  1250. read_scoped_sysreg(const struct arm64_cpu_capabilities *entry, int scope)
  1251. {
  1252. WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
  1253. if (scope == SCOPE_SYSTEM)
  1254. return read_sanitised_ftr_reg(entry->sys_reg);
  1255. else
  1256. return __read_sysreg_by_encoding(entry->sys_reg);
  1257. }
  1258. static bool
  1259. has_user_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
  1260. {
  1261. int mask;
  1262. struct arm64_ftr_reg *regp;
  1263. u64 val = read_scoped_sysreg(entry, scope);
  1264. regp = get_arm64_ftr_reg(entry->sys_reg);
  1265. if (!regp)
  1266. return false;
  1267. mask = cpuid_feature_extract_unsigned_field_width(regp->user_mask,
  1268. entry->field_pos,
  1269. entry->field_width);
  1270. if (!mask)
  1271. return false;
  1272. return feature_matches(val, entry);
  1273. }
  1274. static bool
  1275. has_cpuid_feature(const struct arm64_cpu_capabilities *entry, int scope)
  1276. {
  1277. u64 val = read_scoped_sysreg(entry, scope);
  1278. return feature_matches(val, entry);
  1279. }
  1280. const struct cpumask *system_32bit_el0_cpumask(void)
  1281. {
  1282. if (!system_supports_32bit_el0())
  1283. return cpu_none_mask;
  1284. if (static_branch_unlikely(&arm64_mismatched_32bit_el0))
  1285. return cpu_32bit_el0_mask;
  1286. return cpu_possible_mask;
  1287. }
  1288. EXPORT_SYMBOL_GPL(system_32bit_el0_cpumask);
  1289. static int __init parse_32bit_el0_param(char *str)
  1290. {
  1291. allow_mismatched_32bit_el0 = true;
  1292. return 0;
  1293. }
  1294. early_param("allow_mismatched_32bit_el0", parse_32bit_el0_param);
  1295. static ssize_t aarch32_el0_show(struct device *dev,
  1296. struct device_attribute *attr, char *buf)
  1297. {
  1298. const struct cpumask *mask = system_32bit_el0_cpumask();
  1299. return sysfs_emit(buf, "%*pbl\n", cpumask_pr_args(mask));
  1300. }
  1301. static const DEVICE_ATTR_RO(aarch32_el0);
  1302. static int __init aarch32_el0_sysfs_init(void)
  1303. {
  1304. if (!allow_mismatched_32bit_el0)
  1305. return 0;
  1306. return device_create_file(cpu_subsys.dev_root, &dev_attr_aarch32_el0);
  1307. }
  1308. device_initcall(aarch32_el0_sysfs_init);
  1309. static bool has_32bit_el0(const struct arm64_cpu_capabilities *entry, int scope)
  1310. {
  1311. if (!has_cpuid_feature(entry, scope))
  1312. return allow_mismatched_32bit_el0;
  1313. if (scope == SCOPE_SYSTEM)
  1314. pr_info("detected: 32-bit EL0 Support\n");
  1315. return true;
  1316. }
  1317. static bool has_useable_gicv3_cpuif(const struct arm64_cpu_capabilities *entry, int scope)
  1318. {
  1319. bool has_sre;
  1320. if (!has_cpuid_feature(entry, scope))
  1321. return false;
  1322. has_sre = gic_enable_sre();
  1323. if (!has_sre)
  1324. pr_warn_once("%s present but disabled by higher exception level\n",
  1325. entry->desc);
  1326. return has_sre;
  1327. }
  1328. static bool has_no_hw_prefetch(const struct arm64_cpu_capabilities *entry, int __unused)
  1329. {
  1330. u32 midr = read_cpuid_id();
  1331. /* Cavium ThunderX pass 1.x and 2.x */
  1332. return midr_is_cpu_model_range(midr, MIDR_THUNDERX,
  1333. MIDR_CPU_VAR_REV(0, 0),
  1334. MIDR_CPU_VAR_REV(1, MIDR_REVISION_MASK));
  1335. }
  1336. static bool has_no_fpsimd(const struct arm64_cpu_capabilities *entry, int __unused)
  1337. {
  1338. u64 pfr0 = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1);
  1339. return cpuid_feature_extract_signed_field(pfr0,
  1340. ID_AA64PFR0_EL1_FP_SHIFT) < 0;
  1341. }
  1342. static bool has_cache_idc(const struct arm64_cpu_capabilities *entry,
  1343. int scope)
  1344. {
  1345. u64 ctr;
  1346. if (scope == SCOPE_SYSTEM)
  1347. ctr = arm64_ftr_reg_ctrel0.sys_val;
  1348. else
  1349. ctr = read_cpuid_effective_cachetype();
  1350. return ctr & BIT(CTR_EL0_IDC_SHIFT);
  1351. }
  1352. static void cpu_emulate_effective_ctr(const struct arm64_cpu_capabilities *__unused)
  1353. {
  1354. /*
  1355. * If the CPU exposes raw CTR_EL0.IDC = 0, while effectively
  1356. * CTR_EL0.IDC = 1 (from CLIDR values), we need to trap accesses
  1357. * to the CTR_EL0 on this CPU and emulate it with the real/safe
  1358. * value.
  1359. */
  1360. if (!(read_cpuid_cachetype() & BIT(CTR_EL0_IDC_SHIFT)))
  1361. sysreg_clear_set(sctlr_el1, SCTLR_EL1_UCT, 0);
  1362. }
  1363. static bool has_cache_dic(const struct arm64_cpu_capabilities *entry,
  1364. int scope)
  1365. {
  1366. u64 ctr;
  1367. if (scope == SCOPE_SYSTEM)
  1368. ctr = arm64_ftr_reg_ctrel0.sys_val;
  1369. else
  1370. ctr = read_cpuid_cachetype();
  1371. return ctr & BIT(CTR_EL0_DIC_SHIFT);
  1372. }
  1373. static bool __maybe_unused
  1374. has_useable_cnp(const struct arm64_cpu_capabilities *entry, int scope)
  1375. {
  1376. /*
  1377. * Kdump isn't guaranteed to power-off all secondary CPUs, CNP
  1378. * may share TLB entries with a CPU stuck in the crashed
  1379. * kernel.
  1380. */
  1381. if (is_kdump_kernel())
  1382. return false;
  1383. if (cpus_have_const_cap(ARM64_WORKAROUND_NVIDIA_CARMEL_CNP))
  1384. return false;
  1385. return has_cpuid_feature(entry, scope);
  1386. }
  1387. /*
  1388. * This check is triggered during the early boot before the cpufeature
  1389. * is initialised. Checking the status on the local CPU allows the boot
  1390. * CPU to detect the need for non-global mappings and thus avoiding a
  1391. * pagetable re-write after all the CPUs are booted. This check will be
  1392. * anyway run on individual CPUs, allowing us to get the consistent
  1393. * state once the SMP CPUs are up and thus make the switch to non-global
  1394. * mappings if required.
  1395. */
  1396. bool kaslr_requires_kpti(void)
  1397. {
  1398. if (!IS_ENABLED(CONFIG_RANDOMIZE_BASE))
  1399. return false;
  1400. /*
  1401. * E0PD does a similar job to KPTI so can be used instead
  1402. * where available.
  1403. */
  1404. if (IS_ENABLED(CONFIG_ARM64_E0PD)) {
  1405. u64 mmfr2 = read_sysreg_s(SYS_ID_AA64MMFR2_EL1);
  1406. if (cpuid_feature_extract_unsigned_field(mmfr2,
  1407. ID_AA64MMFR2_EL1_E0PD_SHIFT))
  1408. return false;
  1409. }
  1410. /*
  1411. * Systems affected by Cavium erratum 24756 are incompatible
  1412. * with KPTI.
  1413. */
  1414. if (IS_ENABLED(CONFIG_CAVIUM_ERRATUM_27456)) {
  1415. extern const struct midr_range cavium_erratum_27456_cpus[];
  1416. if (is_midr_in_range_list(read_cpuid_id(),
  1417. cavium_erratum_27456_cpus))
  1418. return false;
  1419. }
  1420. return kaslr_offset() > 0;
  1421. }
  1422. static bool __meltdown_safe = true;
  1423. static int __kpti_forced; /* 0: not forced, >0: forced on, <0: forced off */
  1424. static bool unmap_kernel_at_el0(const struct arm64_cpu_capabilities *entry,
  1425. int scope)
  1426. {
  1427. /* List of CPUs that are not vulnerable and don't need KPTI */
  1428. static const struct midr_range kpti_safe_list[] = {
  1429. MIDR_ALL_VERSIONS(MIDR_CAVIUM_THUNDERX2),
  1430. MIDR_ALL_VERSIONS(MIDR_BRCM_VULCAN),
  1431. MIDR_ALL_VERSIONS(MIDR_BRAHMA_B53),
  1432. MIDR_ALL_VERSIONS(MIDR_CORTEX_A35),
  1433. MIDR_ALL_VERSIONS(MIDR_CORTEX_A53),
  1434. MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
  1435. MIDR_ALL_VERSIONS(MIDR_CORTEX_A57),
  1436. MIDR_ALL_VERSIONS(MIDR_CORTEX_A72),
  1437. MIDR_ALL_VERSIONS(MIDR_CORTEX_A73),
  1438. MIDR_ALL_VERSIONS(MIDR_HISI_TSV110),
  1439. MIDR_ALL_VERSIONS(MIDR_NVIDIA_CARMEL),
  1440. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_GOLD),
  1441. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_2XX_SILVER),
  1442. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_3XX_SILVER),
  1443. MIDR_ALL_VERSIONS(MIDR_QCOM_KRYO_4XX_SILVER),
  1444. { /* sentinel */ }
  1445. };
  1446. char const *str = "kpti command line option";
  1447. bool meltdown_safe;
  1448. meltdown_safe = is_midr_in_range_list(read_cpuid_id(), kpti_safe_list);
  1449. /* Defer to CPU feature registers */
  1450. if (has_cpuid_feature(entry, scope))
  1451. meltdown_safe = true;
  1452. if (!meltdown_safe)
  1453. __meltdown_safe = false;
  1454. /*
  1455. * For reasons that aren't entirely clear, enabling KPTI on Cavium
  1456. * ThunderX leads to apparent I-cache corruption of kernel text, which
  1457. * ends as well as you might imagine. Don't even try. We cannot rely
  1458. * on the cpus_have_*cap() helpers here to detect the CPU erratum
  1459. * because cpucap detection order may change. However, since we know
  1460. * affected CPUs are always in a homogeneous configuration, it is
  1461. * safe to rely on this_cpu_has_cap() here.
  1462. */
  1463. if (this_cpu_has_cap(ARM64_WORKAROUND_CAVIUM_27456)) {
  1464. str = "ARM64_WORKAROUND_CAVIUM_27456";
  1465. __kpti_forced = -1;
  1466. }
  1467. /* Useful for KASLR robustness */
  1468. if (kaslr_requires_kpti()) {
  1469. if (!__kpti_forced) {
  1470. str = "KASLR";
  1471. __kpti_forced = 1;
  1472. }
  1473. }
  1474. if (cpu_mitigations_off() && !__kpti_forced) {
  1475. str = "mitigations=off";
  1476. __kpti_forced = -1;
  1477. }
  1478. if (!IS_ENABLED(CONFIG_UNMAP_KERNEL_AT_EL0)) {
  1479. pr_info_once("kernel page table isolation disabled by kernel configuration\n");
  1480. return false;
  1481. }
  1482. /* Forced? */
  1483. if (__kpti_forced) {
  1484. pr_info_once("kernel page table isolation forced %s by %s\n",
  1485. __kpti_forced > 0 ? "ON" : "OFF", str);
  1486. return __kpti_forced > 0;
  1487. }
  1488. return !meltdown_safe;
  1489. }
  1490. #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
  1491. #define KPTI_NG_TEMP_VA (-(1UL << PMD_SHIFT))
  1492. extern
  1493. void create_kpti_ng_temp_pgd(pgd_t *pgdir, phys_addr_t phys, unsigned long virt,
  1494. phys_addr_t size, pgprot_t prot,
  1495. phys_addr_t (*pgtable_alloc)(int), int flags);
  1496. static phys_addr_t kpti_ng_temp_alloc;
  1497. static phys_addr_t kpti_ng_pgd_alloc(int shift)
  1498. {
  1499. kpti_ng_temp_alloc -= PAGE_SIZE;
  1500. return kpti_ng_temp_alloc;
  1501. }
  1502. static void
  1503. kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
  1504. {
  1505. typedef void (kpti_remap_fn)(int, int, phys_addr_t, unsigned long);
  1506. extern kpti_remap_fn idmap_kpti_install_ng_mappings;
  1507. kpti_remap_fn *remap_fn;
  1508. int cpu = smp_processor_id();
  1509. int levels = CONFIG_PGTABLE_LEVELS;
  1510. int order = order_base_2(levels);
  1511. u64 kpti_ng_temp_pgd_pa = 0;
  1512. pgd_t *kpti_ng_temp_pgd;
  1513. u64 alloc = 0;
  1514. if (__this_cpu_read(this_cpu_vector) == vectors) {
  1515. const char *v = arm64_get_bp_hardening_vector(EL1_VECTOR_KPTI);
  1516. __this_cpu_write(this_cpu_vector, v);
  1517. }
  1518. /*
  1519. * We don't need to rewrite the page-tables if either we've done
  1520. * it already or we have KASLR enabled and therefore have not
  1521. * created any global mappings at all.
  1522. */
  1523. if (arm64_use_ng_mappings)
  1524. return;
  1525. remap_fn = (void *)__pa_symbol(idmap_kpti_install_ng_mappings);
  1526. if (!cpu) {
  1527. alloc = __get_free_pages(GFP_ATOMIC | __GFP_ZERO, order);
  1528. kpti_ng_temp_pgd = (pgd_t *)(alloc + (levels - 1) * PAGE_SIZE);
  1529. kpti_ng_temp_alloc = kpti_ng_temp_pgd_pa = __pa(kpti_ng_temp_pgd);
  1530. //
  1531. // Create a minimal page table hierarchy that permits us to map
  1532. // the swapper page tables temporarily as we traverse them.
  1533. //
  1534. // The physical pages are laid out as follows:
  1535. //
  1536. // +--------+-/-------+-/------ +-\\--------+
  1537. // : PTE[] : | PMD[] : | PUD[] : || PGD[] :
  1538. // +--------+-\-------+-\------ +-//--------+
  1539. // ^
  1540. // The first page is mapped into this hierarchy at a PMD_SHIFT
  1541. // aligned virtual address, so that we can manipulate the PTE
  1542. // level entries while the mapping is active. The first entry
  1543. // covers the PTE[] page itself, the remaining entries are free
  1544. // to be used as a ad-hoc fixmap.
  1545. //
  1546. create_kpti_ng_temp_pgd(kpti_ng_temp_pgd, __pa(alloc),
  1547. KPTI_NG_TEMP_VA, PAGE_SIZE, PAGE_KERNEL,
  1548. kpti_ng_pgd_alloc, 0);
  1549. }
  1550. cpu_install_idmap();
  1551. remap_fn(cpu, num_online_cpus(), kpti_ng_temp_pgd_pa, KPTI_NG_TEMP_VA);
  1552. cpu_uninstall_idmap();
  1553. if (!cpu) {
  1554. free_pages(alloc, order);
  1555. arm64_use_ng_mappings = true;
  1556. }
  1557. }
  1558. #else
  1559. static void
  1560. kpti_install_ng_mappings(const struct arm64_cpu_capabilities *__unused)
  1561. {
  1562. }
  1563. #endif /* CONFIG_UNMAP_KERNEL_AT_EL0 */
  1564. static int __init parse_kpti(char *str)
  1565. {
  1566. bool enabled;
  1567. int ret = strtobool(str, &enabled);
  1568. if (ret)
  1569. return ret;
  1570. __kpti_forced = enabled ? 1 : -1;
  1571. return 0;
  1572. }
  1573. early_param("kpti", parse_kpti);
  1574. #ifdef CONFIG_ARM64_HW_AFDBM
  1575. static inline void __cpu_enable_hw_dbm(void)
  1576. {
  1577. u64 tcr = read_sysreg(tcr_el1) | TCR_HD;
  1578. write_sysreg(tcr, tcr_el1);
  1579. isb();
  1580. local_flush_tlb_all();
  1581. }
  1582. static bool cpu_has_broken_dbm(void)
  1583. {
  1584. /* List of CPUs which have broken DBM support. */
  1585. static const struct midr_range cpus[] = {
  1586. #ifdef CONFIG_ARM64_ERRATUM_1024718
  1587. MIDR_ALL_VERSIONS(MIDR_CORTEX_A55),
  1588. /* Kryo4xx Silver (rdpe => r1p0) */
  1589. MIDR_REV(MIDR_QCOM_KRYO_4XX_SILVER, 0xd, 0xe),
  1590. #endif
  1591. #ifdef CONFIG_ARM64_ERRATUM_2051678
  1592. MIDR_REV_RANGE(MIDR_CORTEX_A510, 0, 0, 2),
  1593. #endif
  1594. {},
  1595. };
  1596. return is_midr_in_range_list(read_cpuid_id(), cpus);
  1597. }
  1598. static bool cpu_can_use_dbm(const struct arm64_cpu_capabilities *cap)
  1599. {
  1600. return has_cpuid_feature(cap, SCOPE_LOCAL_CPU) &&
  1601. !cpu_has_broken_dbm();
  1602. }
  1603. static void cpu_enable_hw_dbm(struct arm64_cpu_capabilities const *cap)
  1604. {
  1605. if (cpu_can_use_dbm(cap))
  1606. __cpu_enable_hw_dbm();
  1607. }
  1608. static bool has_hw_dbm(const struct arm64_cpu_capabilities *cap,
  1609. int __unused)
  1610. {
  1611. static bool detected = false;
  1612. /*
  1613. * DBM is a non-conflicting feature. i.e, the kernel can safely
  1614. * run a mix of CPUs with and without the feature. So, we
  1615. * unconditionally enable the capability to allow any late CPU
  1616. * to use the feature. We only enable the control bits on the
  1617. * CPU, if it actually supports.
  1618. *
  1619. * We have to make sure we print the "feature" detection only
  1620. * when at least one CPU actually uses it. So check if this CPU
  1621. * can actually use it and print the message exactly once.
  1622. *
  1623. * This is safe as all CPUs (including secondary CPUs - due to the
  1624. * LOCAL_CPU scope - and the hotplugged CPUs - via verification)
  1625. * goes through the "matches" check exactly once. Also if a CPU
  1626. * matches the criteria, it is guaranteed that the CPU will turn
  1627. * the DBM on, as the capability is unconditionally enabled.
  1628. */
  1629. if (!detected && cpu_can_use_dbm(cap)) {
  1630. detected = true;
  1631. pr_info("detected: Hardware dirty bit management\n");
  1632. }
  1633. return true;
  1634. }
  1635. #endif
  1636. #ifdef CONFIG_ARM64_AMU_EXTN
  1637. /*
  1638. * The "amu_cpus" cpumask only signals that the CPU implementation for the
  1639. * flagged CPUs supports the Activity Monitors Unit (AMU) but does not provide
  1640. * information regarding all the events that it supports. When a CPU bit is
  1641. * set in the cpumask, the user of this feature can only rely on the presence
  1642. * of the 4 fixed counters for that CPU. But this does not guarantee that the
  1643. * counters are enabled or access to these counters is enabled by code
  1644. * executed at higher exception levels (firmware).
  1645. */
  1646. static struct cpumask amu_cpus __read_mostly;
  1647. bool cpu_has_amu_feat(int cpu)
  1648. {
  1649. return cpumask_test_cpu(cpu, &amu_cpus);
  1650. }
  1651. int get_cpu_with_amu_feat(void)
  1652. {
  1653. return cpumask_any(&amu_cpus);
  1654. }
  1655. static void cpu_amu_enable(struct arm64_cpu_capabilities const *cap)
  1656. {
  1657. if (has_cpuid_feature(cap, SCOPE_LOCAL_CPU)) {
  1658. pr_info("detected CPU%d: Activity Monitors Unit (AMU)\n",
  1659. smp_processor_id());
  1660. cpumask_set_cpu(smp_processor_id(), &amu_cpus);
  1661. /* 0 reference values signal broken/disabled counters */
  1662. if (!this_cpu_has_cap(ARM64_WORKAROUND_2457168))
  1663. update_freq_counters_refs();
  1664. }
  1665. }
  1666. static bool has_amu(const struct arm64_cpu_capabilities *cap,
  1667. int __unused)
  1668. {
  1669. /*
  1670. * The AMU extension is a non-conflicting feature: the kernel can
  1671. * safely run a mix of CPUs with and without support for the
  1672. * activity monitors extension. Therefore, unconditionally enable
  1673. * the capability to allow any late CPU to use the feature.
  1674. *
  1675. * With this feature unconditionally enabled, the cpu_enable
  1676. * function will be called for all CPUs that match the criteria,
  1677. * including secondary and hotplugged, marking this feature as
  1678. * present on that respective CPU. The enable function will also
  1679. * print a detection message.
  1680. */
  1681. return true;
  1682. }
  1683. #else
  1684. int get_cpu_with_amu_feat(void)
  1685. {
  1686. return nr_cpu_ids;
  1687. }
  1688. #endif
  1689. static bool runs_at_el2(const struct arm64_cpu_capabilities *entry, int __unused)
  1690. {
  1691. return is_kernel_in_hyp_mode();
  1692. }
  1693. static void cpu_copy_el2regs(const struct arm64_cpu_capabilities *__unused)
  1694. {
  1695. /*
  1696. * Copy register values that aren't redirected by hardware.
  1697. *
  1698. * Before code patching, we only set tpidr_el1, all CPUs need to copy
  1699. * this value to tpidr_el2 before we patch the code. Once we've done
  1700. * that, freshly-onlined CPUs will set tpidr_el2, so we don't need to
  1701. * do anything here.
  1702. */
  1703. if (!alternative_is_applied(ARM64_HAS_VIRT_HOST_EXTN))
  1704. write_sysreg(read_sysreg(tpidr_el1), tpidr_el2);
  1705. }
  1706. #ifdef CONFIG_ARM64_PAN
  1707. static void cpu_enable_pan(const struct arm64_cpu_capabilities *__unused)
  1708. {
  1709. /*
  1710. * We modify PSTATE. This won't work from irq context as the PSTATE
  1711. * is discarded once we return from the exception.
  1712. */
  1713. WARN_ON_ONCE(in_interrupt());
  1714. sysreg_clear_set(sctlr_el1, SCTLR_EL1_SPAN, 0);
  1715. set_pstate_pan(1);
  1716. }
  1717. #endif /* CONFIG_ARM64_PAN */
  1718. #ifdef CONFIG_ARM64_RAS_EXTN
  1719. static void cpu_clear_disr(const struct arm64_cpu_capabilities *__unused)
  1720. {
  1721. /* Firmware may have left a deferred SError in this register. */
  1722. write_sysreg_s(0, SYS_DISR_EL1);
  1723. }
  1724. #endif /* CONFIG_ARM64_RAS_EXTN */
  1725. #ifdef CONFIG_ARM64_PTR_AUTH
  1726. static bool has_address_auth_cpucap(const struct arm64_cpu_capabilities *entry, int scope)
  1727. {
  1728. int boot_val, sec_val;
  1729. /* We don't expect to be called with SCOPE_SYSTEM */
  1730. WARN_ON(scope == SCOPE_SYSTEM);
  1731. /*
  1732. * The ptr-auth feature levels are not intercompatible with lower
  1733. * levels. Hence we must match ptr-auth feature level of the secondary
  1734. * CPUs with that of the boot CPU. The level of boot cpu is fetched
  1735. * from the sanitised register whereas direct register read is done for
  1736. * the secondary CPUs.
  1737. * The sanitised feature state is guaranteed to match that of the
  1738. * boot CPU as a mismatched secondary CPU is parked before it gets
  1739. * a chance to update the state, with the capability.
  1740. */
  1741. boot_val = cpuid_feature_extract_field(read_sanitised_ftr_reg(entry->sys_reg),
  1742. entry->field_pos, entry->sign);
  1743. if (scope & SCOPE_BOOT_CPU)
  1744. return boot_val >= entry->min_field_value;
  1745. /* Now check for the secondary CPUs with SCOPE_LOCAL_CPU scope */
  1746. sec_val = cpuid_feature_extract_field(__read_sysreg_by_encoding(entry->sys_reg),
  1747. entry->field_pos, entry->sign);
  1748. return (sec_val >= entry->min_field_value) && (sec_val == boot_val);
  1749. }
  1750. static bool has_address_auth_metacap(const struct arm64_cpu_capabilities *entry,
  1751. int scope)
  1752. {
  1753. bool api = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_IMP_DEF], scope);
  1754. bool apa = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA5], scope);
  1755. bool apa3 = has_address_auth_cpucap(cpu_hwcaps_ptrs[ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA3], scope);
  1756. return apa || apa3 || api;
  1757. }
  1758. static bool has_generic_auth(const struct arm64_cpu_capabilities *entry,
  1759. int __unused)
  1760. {
  1761. bool gpi = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_IMP_DEF);
  1762. bool gpa = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH_QARMA5);
  1763. bool gpa3 = __system_matches_cap(ARM64_HAS_GENERIC_AUTH_ARCH_QARMA3);
  1764. return gpa || gpa3 || gpi;
  1765. }
  1766. #endif /* CONFIG_ARM64_PTR_AUTH */
  1767. #ifdef CONFIG_ARM64_E0PD
  1768. static void cpu_enable_e0pd(struct arm64_cpu_capabilities const *cap)
  1769. {
  1770. if (this_cpu_has_cap(ARM64_HAS_E0PD))
  1771. sysreg_clear_set(tcr_el1, 0, TCR_E0PD1);
  1772. }
  1773. #endif /* CONFIG_ARM64_E0PD */
  1774. #ifdef CONFIG_ARM64_PSEUDO_NMI
  1775. static bool enable_pseudo_nmi;
  1776. static int __init early_enable_pseudo_nmi(char *p)
  1777. {
  1778. return strtobool(p, &enable_pseudo_nmi);
  1779. }
  1780. early_param("irqchip.gicv3_pseudo_nmi", early_enable_pseudo_nmi);
  1781. static bool can_use_gic_priorities(const struct arm64_cpu_capabilities *entry,
  1782. int scope)
  1783. {
  1784. return enable_pseudo_nmi && has_useable_gicv3_cpuif(entry, scope);
  1785. }
  1786. #endif
  1787. #ifdef CONFIG_ARM64_BTI
  1788. static void bti_enable(const struct arm64_cpu_capabilities *__unused)
  1789. {
  1790. /*
  1791. * Use of X16/X17 for tail-calls and trampolines that jump to
  1792. * function entry points using BR is a requirement for
  1793. * marking binaries with GNU_PROPERTY_AARCH64_FEATURE_1_BTI.
  1794. * So, be strict and forbid other BRs using other registers to
  1795. * jump onto a PACIxSP instruction:
  1796. */
  1797. sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_BT0 | SCTLR_EL1_BT1);
  1798. isb();
  1799. }
  1800. #endif /* CONFIG_ARM64_BTI */
  1801. #ifdef CONFIG_ARM64_MTE
  1802. static void cpu_enable_mte(struct arm64_cpu_capabilities const *cap)
  1803. {
  1804. sysreg_clear_set(sctlr_el1, 0, SCTLR_ELx_ATA | SCTLR_EL1_ATA0);
  1805. mte_cpu_setup();
  1806. /*
  1807. * Clear the tags in the zero page. This needs to be done via the
  1808. * linear map which has the Tagged attribute.
  1809. */
  1810. if (!page_mte_tagged(ZERO_PAGE(0))) {
  1811. mte_clear_page_tags(lm_alias(empty_zero_page));
  1812. set_page_mte_tagged(ZERO_PAGE(0));
  1813. }
  1814. kasan_init_hw_tags_cpu();
  1815. }
  1816. #endif /* CONFIG_ARM64_MTE */
  1817. static void elf_hwcap_fixup(void)
  1818. {
  1819. #ifdef CONFIG_ARM64_ERRATUM_1742098
  1820. if (cpus_have_const_cap(ARM64_WORKAROUND_1742098))
  1821. compat_elf_hwcap2 &= ~COMPAT_HWCAP2_AES;
  1822. #endif /* ARM64_ERRATUM_1742098 */
  1823. }
  1824. #ifdef CONFIG_KVM
  1825. static bool is_kvm_protected_mode(const struct arm64_cpu_capabilities *entry, int __unused)
  1826. {
  1827. return kvm_get_mode() == KVM_MODE_PROTECTED;
  1828. }
  1829. #endif /* CONFIG_KVM */
  1830. static void cpu_trap_el0_impdef(const struct arm64_cpu_capabilities *__unused)
  1831. {
  1832. sysreg_clear_set(sctlr_el1, 0, SCTLR_EL1_TIDCP);
  1833. }
  1834. /* Internal helper functions to match cpu capability type */
  1835. static bool
  1836. cpucap_late_cpu_optional(const struct arm64_cpu_capabilities *cap)
  1837. {
  1838. return !!(cap->type & ARM64_CPUCAP_OPTIONAL_FOR_LATE_CPU);
  1839. }
  1840. static bool
  1841. cpucap_late_cpu_permitted(const struct arm64_cpu_capabilities *cap)
  1842. {
  1843. return !!(cap->type & ARM64_CPUCAP_PERMITTED_FOR_LATE_CPU);
  1844. }
  1845. static bool
  1846. cpucap_panic_on_conflict(const struct arm64_cpu_capabilities *cap)
  1847. {
  1848. return !!(cap->type & ARM64_CPUCAP_PANIC_ON_CONFLICT);
  1849. }
  1850. static const struct arm64_cpu_capabilities arm64_features[] = {
  1851. {
  1852. .capability = ARM64_ALWAYS_BOOT,
  1853. .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
  1854. .matches = has_always,
  1855. },
  1856. {
  1857. .capability = ARM64_ALWAYS_SYSTEM,
  1858. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1859. .matches = has_always,
  1860. },
  1861. {
  1862. .desc = "GIC system register CPU interface",
  1863. .capability = ARM64_HAS_SYSREG_GIC_CPUIF,
  1864. .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
  1865. .matches = has_useable_gicv3_cpuif,
  1866. .sys_reg = SYS_ID_AA64PFR0_EL1,
  1867. .field_pos = ID_AA64PFR0_EL1_GIC_SHIFT,
  1868. .field_width = 4,
  1869. .sign = FTR_UNSIGNED,
  1870. .min_field_value = 1,
  1871. },
  1872. {
  1873. .desc = "Enhanced Counter Virtualization",
  1874. .capability = ARM64_HAS_ECV,
  1875. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1876. .matches = has_cpuid_feature,
  1877. .sys_reg = SYS_ID_AA64MMFR0_EL1,
  1878. .field_pos = ID_AA64MMFR0_EL1_ECV_SHIFT,
  1879. .field_width = 4,
  1880. .sign = FTR_UNSIGNED,
  1881. .min_field_value = 1,
  1882. },
  1883. #ifdef CONFIG_ARM64_PAN
  1884. {
  1885. .desc = "Privileged Access Never",
  1886. .capability = ARM64_HAS_PAN,
  1887. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1888. .matches = has_cpuid_feature,
  1889. .sys_reg = SYS_ID_AA64MMFR1_EL1,
  1890. .field_pos = ID_AA64MMFR1_EL1_PAN_SHIFT,
  1891. .field_width = 4,
  1892. .sign = FTR_UNSIGNED,
  1893. .min_field_value = 1,
  1894. .cpu_enable = cpu_enable_pan,
  1895. },
  1896. #endif /* CONFIG_ARM64_PAN */
  1897. #ifdef CONFIG_ARM64_EPAN
  1898. {
  1899. .desc = "Enhanced Privileged Access Never",
  1900. .capability = ARM64_HAS_EPAN,
  1901. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1902. .matches = has_cpuid_feature,
  1903. .sys_reg = SYS_ID_AA64MMFR1_EL1,
  1904. .field_pos = ID_AA64MMFR1_EL1_PAN_SHIFT,
  1905. .field_width = 4,
  1906. .sign = FTR_UNSIGNED,
  1907. .min_field_value = 3,
  1908. },
  1909. #endif /* CONFIG_ARM64_EPAN */
  1910. #ifdef CONFIG_ARM64_LSE_ATOMICS
  1911. {
  1912. .desc = "LSE atomic instructions",
  1913. .capability = ARM64_HAS_LSE_ATOMICS,
  1914. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1915. .matches = has_cpuid_feature,
  1916. .sys_reg = SYS_ID_AA64ISAR0_EL1,
  1917. .field_pos = ID_AA64ISAR0_EL1_ATOMIC_SHIFT,
  1918. .field_width = 4,
  1919. .sign = FTR_UNSIGNED,
  1920. .min_field_value = 2,
  1921. },
  1922. #endif /* CONFIG_ARM64_LSE_ATOMICS */
  1923. {
  1924. .desc = "Software prefetching using PRFM",
  1925. .capability = ARM64_HAS_NO_HW_PREFETCH,
  1926. .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
  1927. .matches = has_no_hw_prefetch,
  1928. },
  1929. {
  1930. .desc = "Virtualization Host Extensions",
  1931. .capability = ARM64_HAS_VIRT_HOST_EXTN,
  1932. .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
  1933. .matches = runs_at_el2,
  1934. .cpu_enable = cpu_copy_el2regs,
  1935. },
  1936. {
  1937. .capability = ARM64_HAS_32BIT_EL0_DO_NOT_USE,
  1938. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1939. .matches = has_32bit_el0,
  1940. .sys_reg = SYS_ID_AA64PFR0_EL1,
  1941. .sign = FTR_UNSIGNED,
  1942. .field_pos = ID_AA64PFR0_EL1_EL0_SHIFT,
  1943. .field_width = 4,
  1944. .min_field_value = ID_AA64PFR0_EL1_ELx_32BIT_64BIT,
  1945. },
  1946. #ifdef CONFIG_KVM
  1947. {
  1948. .desc = "32-bit EL1 Support",
  1949. .capability = ARM64_HAS_32BIT_EL1,
  1950. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1951. .matches = has_cpuid_feature,
  1952. .sys_reg = SYS_ID_AA64PFR0_EL1,
  1953. .sign = FTR_UNSIGNED,
  1954. .field_pos = ID_AA64PFR0_EL1_EL1_SHIFT,
  1955. .field_width = 4,
  1956. .min_field_value = ID_AA64PFR0_EL1_ELx_32BIT_64BIT,
  1957. },
  1958. {
  1959. .desc = "Protected KVM",
  1960. .capability = ARM64_KVM_PROTECTED_MODE,
  1961. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1962. .matches = is_kvm_protected_mode,
  1963. },
  1964. #endif
  1965. {
  1966. .desc = "Kernel page table isolation (KPTI)",
  1967. .capability = ARM64_UNMAP_KERNEL_AT_EL0,
  1968. .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
  1969. /*
  1970. * The ID feature fields below are used to indicate that
  1971. * the CPU doesn't need KPTI. See unmap_kernel_at_el0 for
  1972. * more details.
  1973. */
  1974. .sys_reg = SYS_ID_AA64PFR0_EL1,
  1975. .field_pos = ID_AA64PFR0_EL1_CSV3_SHIFT,
  1976. .field_width = 4,
  1977. .min_field_value = 1,
  1978. .matches = unmap_kernel_at_el0,
  1979. .cpu_enable = kpti_install_ng_mappings,
  1980. },
  1981. {
  1982. /* FP/SIMD is not implemented */
  1983. .capability = ARM64_HAS_NO_FPSIMD,
  1984. .type = ARM64_CPUCAP_BOOT_RESTRICTED_CPU_LOCAL_FEATURE,
  1985. .min_field_value = 0,
  1986. .matches = has_no_fpsimd,
  1987. },
  1988. #ifdef CONFIG_ARM64_PMEM
  1989. {
  1990. .desc = "Data cache clean to Point of Persistence",
  1991. .capability = ARM64_HAS_DCPOP,
  1992. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  1993. .matches = has_cpuid_feature,
  1994. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  1995. .field_pos = ID_AA64ISAR1_EL1_DPB_SHIFT,
  1996. .field_width = 4,
  1997. .min_field_value = 1,
  1998. },
  1999. {
  2000. .desc = "Data cache clean to Point of Deep Persistence",
  2001. .capability = ARM64_HAS_DCPODP,
  2002. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2003. .matches = has_cpuid_feature,
  2004. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  2005. .sign = FTR_UNSIGNED,
  2006. .field_pos = ID_AA64ISAR1_EL1_DPB_SHIFT,
  2007. .field_width = 4,
  2008. .min_field_value = 2,
  2009. },
  2010. #endif
  2011. #ifdef CONFIG_ARM64_SVE
  2012. {
  2013. .desc = "Scalable Vector Extension",
  2014. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2015. .capability = ARM64_SVE,
  2016. .sys_reg = SYS_ID_AA64PFR0_EL1,
  2017. .sign = FTR_UNSIGNED,
  2018. .field_pos = ID_AA64PFR0_EL1_SVE_SHIFT,
  2019. .field_width = 4,
  2020. .min_field_value = ID_AA64PFR0_EL1_SVE_IMP,
  2021. .matches = has_cpuid_feature,
  2022. .cpu_enable = sve_kernel_enable,
  2023. },
  2024. #endif /* CONFIG_ARM64_SVE */
  2025. #ifdef CONFIG_ARM64_RAS_EXTN
  2026. {
  2027. .desc = "RAS Extension Support",
  2028. .capability = ARM64_HAS_RAS_EXTN,
  2029. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2030. .matches = has_cpuid_feature,
  2031. .sys_reg = SYS_ID_AA64PFR0_EL1,
  2032. .sign = FTR_UNSIGNED,
  2033. .field_pos = ID_AA64PFR0_EL1_RAS_SHIFT,
  2034. .field_width = 4,
  2035. .min_field_value = ID_AA64PFR0_EL1_RAS_IMP,
  2036. .cpu_enable = cpu_clear_disr,
  2037. },
  2038. #endif /* CONFIG_ARM64_RAS_EXTN */
  2039. #ifdef CONFIG_ARM64_AMU_EXTN
  2040. {
  2041. /*
  2042. * The feature is enabled by default if CONFIG_ARM64_AMU_EXTN=y.
  2043. * Therefore, don't provide .desc as we don't want the detection
  2044. * message to be shown until at least one CPU is detected to
  2045. * support the feature.
  2046. */
  2047. .capability = ARM64_HAS_AMU_EXTN,
  2048. .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
  2049. .matches = has_amu,
  2050. .sys_reg = SYS_ID_AA64PFR0_EL1,
  2051. .sign = FTR_UNSIGNED,
  2052. .field_pos = ID_AA64PFR0_EL1_AMU_SHIFT,
  2053. .field_width = 4,
  2054. .min_field_value = ID_AA64PFR0_EL1_AMU_IMP,
  2055. .cpu_enable = cpu_amu_enable,
  2056. },
  2057. #endif /* CONFIG_ARM64_AMU_EXTN */
  2058. {
  2059. .desc = "Data cache clean to the PoU not required for I/D coherence",
  2060. .capability = ARM64_HAS_CACHE_IDC,
  2061. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2062. .matches = has_cache_idc,
  2063. .cpu_enable = cpu_emulate_effective_ctr,
  2064. },
  2065. {
  2066. .desc = "Instruction cache invalidation not required for I/D coherence",
  2067. .capability = ARM64_HAS_CACHE_DIC,
  2068. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2069. .matches = has_cache_dic,
  2070. },
  2071. {
  2072. .desc = "Stage-2 Force Write-Back",
  2073. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2074. .capability = ARM64_HAS_STAGE2_FWB,
  2075. .sys_reg = SYS_ID_AA64MMFR2_EL1,
  2076. .sign = FTR_UNSIGNED,
  2077. .field_pos = ID_AA64MMFR2_EL1_FWB_SHIFT,
  2078. .field_width = 4,
  2079. .min_field_value = 1,
  2080. .matches = has_cpuid_feature,
  2081. },
  2082. {
  2083. .desc = "ARMv8.4 Translation Table Level",
  2084. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2085. .capability = ARM64_HAS_ARMv8_4_TTL,
  2086. .sys_reg = SYS_ID_AA64MMFR2_EL1,
  2087. .sign = FTR_UNSIGNED,
  2088. .field_pos = ID_AA64MMFR2_EL1_TTL_SHIFT,
  2089. .field_width = 4,
  2090. .min_field_value = 1,
  2091. .matches = has_cpuid_feature,
  2092. },
  2093. {
  2094. .desc = "TLB range maintenance instructions",
  2095. .capability = ARM64_HAS_TLB_RANGE,
  2096. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2097. .matches = has_cpuid_feature,
  2098. .sys_reg = SYS_ID_AA64ISAR0_EL1,
  2099. .field_pos = ID_AA64ISAR0_EL1_TLB_SHIFT,
  2100. .field_width = 4,
  2101. .sign = FTR_UNSIGNED,
  2102. .min_field_value = ID_AA64ISAR0_EL1_TLB_RANGE,
  2103. },
  2104. #ifdef CONFIG_ARM64_HW_AFDBM
  2105. {
  2106. /*
  2107. * Since we turn this on always, we don't want the user to
  2108. * think that the feature is available when it may not be.
  2109. * So hide the description.
  2110. *
  2111. * .desc = "Hardware pagetable Dirty Bit Management",
  2112. *
  2113. */
  2114. .type = ARM64_CPUCAP_WEAK_LOCAL_CPU_FEATURE,
  2115. .capability = ARM64_HW_DBM,
  2116. .sys_reg = SYS_ID_AA64MMFR1_EL1,
  2117. .sign = FTR_UNSIGNED,
  2118. .field_pos = ID_AA64MMFR1_EL1_HAFDBS_SHIFT,
  2119. .field_width = 4,
  2120. .min_field_value = 2,
  2121. .matches = has_hw_dbm,
  2122. .cpu_enable = cpu_enable_hw_dbm,
  2123. },
  2124. #endif
  2125. {
  2126. .desc = "CRC32 instructions",
  2127. .capability = ARM64_HAS_CRC32,
  2128. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2129. .matches = has_cpuid_feature,
  2130. .sys_reg = SYS_ID_AA64ISAR0_EL1,
  2131. .field_pos = ID_AA64ISAR0_EL1_CRC32_SHIFT,
  2132. .field_width = 4,
  2133. .min_field_value = 1,
  2134. },
  2135. {
  2136. .desc = "Speculative Store Bypassing Safe (SSBS)",
  2137. .capability = ARM64_SSBS,
  2138. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2139. .matches = has_cpuid_feature,
  2140. .sys_reg = SYS_ID_AA64PFR1_EL1,
  2141. .field_pos = ID_AA64PFR1_EL1_SSBS_SHIFT,
  2142. .field_width = 4,
  2143. .sign = FTR_UNSIGNED,
  2144. .min_field_value = ID_AA64PFR1_EL1_SSBS_IMP,
  2145. },
  2146. #ifdef CONFIG_ARM64_CNP
  2147. {
  2148. .desc = "Common not Private translations",
  2149. .capability = ARM64_HAS_CNP,
  2150. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2151. .matches = has_useable_cnp,
  2152. .sys_reg = SYS_ID_AA64MMFR2_EL1,
  2153. .sign = FTR_UNSIGNED,
  2154. .field_pos = ID_AA64MMFR2_EL1_CnP_SHIFT,
  2155. .field_width = 4,
  2156. .min_field_value = 1,
  2157. .cpu_enable = cpu_enable_cnp,
  2158. },
  2159. #endif
  2160. {
  2161. .desc = "Speculation barrier (SB)",
  2162. .capability = ARM64_HAS_SB,
  2163. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2164. .matches = has_cpuid_feature,
  2165. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  2166. .field_pos = ID_AA64ISAR1_EL1_SB_SHIFT,
  2167. .field_width = 4,
  2168. .sign = FTR_UNSIGNED,
  2169. .min_field_value = 1,
  2170. },
  2171. #ifdef CONFIG_ARM64_PTR_AUTH
  2172. {
  2173. .desc = "Address authentication (architected QARMA5 algorithm)",
  2174. .capability = ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA5,
  2175. .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
  2176. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  2177. .sign = FTR_UNSIGNED,
  2178. .field_pos = ID_AA64ISAR1_EL1_APA_SHIFT,
  2179. .field_width = 4,
  2180. .min_field_value = ID_AA64ISAR1_EL1_APA_PAuth,
  2181. .matches = has_address_auth_cpucap,
  2182. },
  2183. {
  2184. .desc = "Address authentication (architected QARMA3 algorithm)",
  2185. .capability = ARM64_HAS_ADDRESS_AUTH_ARCH_QARMA3,
  2186. .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
  2187. .sys_reg = SYS_ID_AA64ISAR2_EL1,
  2188. .sign = FTR_UNSIGNED,
  2189. .field_pos = ID_AA64ISAR2_EL1_APA3_SHIFT,
  2190. .field_width = 4,
  2191. .min_field_value = ID_AA64ISAR2_EL1_APA3_PAuth,
  2192. .matches = has_address_auth_cpucap,
  2193. },
  2194. {
  2195. .desc = "Address authentication (IMP DEF algorithm)",
  2196. .capability = ARM64_HAS_ADDRESS_AUTH_IMP_DEF,
  2197. .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
  2198. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  2199. .sign = FTR_UNSIGNED,
  2200. .field_pos = ID_AA64ISAR1_EL1_API_SHIFT,
  2201. .field_width = 4,
  2202. .min_field_value = ID_AA64ISAR1_EL1_API_PAuth,
  2203. .matches = has_address_auth_cpucap,
  2204. },
  2205. {
  2206. .capability = ARM64_HAS_ADDRESS_AUTH,
  2207. .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
  2208. .matches = has_address_auth_metacap,
  2209. },
  2210. {
  2211. .desc = "Generic authentication (architected QARMA5 algorithm)",
  2212. .capability = ARM64_HAS_GENERIC_AUTH_ARCH_QARMA5,
  2213. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2214. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  2215. .sign = FTR_UNSIGNED,
  2216. .field_pos = ID_AA64ISAR1_EL1_GPA_SHIFT,
  2217. .field_width = 4,
  2218. .min_field_value = ID_AA64ISAR1_EL1_GPA_IMP,
  2219. .matches = has_cpuid_feature,
  2220. },
  2221. {
  2222. .desc = "Generic authentication (architected QARMA3 algorithm)",
  2223. .capability = ARM64_HAS_GENERIC_AUTH_ARCH_QARMA3,
  2224. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2225. .sys_reg = SYS_ID_AA64ISAR2_EL1,
  2226. .sign = FTR_UNSIGNED,
  2227. .field_pos = ID_AA64ISAR2_EL1_GPA3_SHIFT,
  2228. .field_width = 4,
  2229. .min_field_value = ID_AA64ISAR2_EL1_GPA3_IMP,
  2230. .matches = has_cpuid_feature,
  2231. },
  2232. {
  2233. .desc = "Generic authentication (IMP DEF algorithm)",
  2234. .capability = ARM64_HAS_GENERIC_AUTH_IMP_DEF,
  2235. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2236. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  2237. .sign = FTR_UNSIGNED,
  2238. .field_pos = ID_AA64ISAR1_EL1_GPI_SHIFT,
  2239. .field_width = 4,
  2240. .min_field_value = ID_AA64ISAR1_EL1_GPI_IMP,
  2241. .matches = has_cpuid_feature,
  2242. },
  2243. {
  2244. .capability = ARM64_HAS_GENERIC_AUTH,
  2245. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2246. .matches = has_generic_auth,
  2247. },
  2248. #endif /* CONFIG_ARM64_PTR_AUTH */
  2249. #ifdef CONFIG_ARM64_PSEUDO_NMI
  2250. {
  2251. /*
  2252. * Depends on having GICv3
  2253. */
  2254. .desc = "IRQ priority masking",
  2255. .capability = ARM64_HAS_IRQ_PRIO_MASKING,
  2256. .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
  2257. .matches = can_use_gic_priorities,
  2258. .sys_reg = SYS_ID_AA64PFR0_EL1,
  2259. .field_pos = ID_AA64PFR0_EL1_GIC_SHIFT,
  2260. .field_width = 4,
  2261. .sign = FTR_UNSIGNED,
  2262. .min_field_value = 1,
  2263. },
  2264. #endif
  2265. #ifdef CONFIG_ARM64_E0PD
  2266. {
  2267. .desc = "E0PD",
  2268. .capability = ARM64_HAS_E0PD,
  2269. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2270. .sys_reg = SYS_ID_AA64MMFR2_EL1,
  2271. .sign = FTR_UNSIGNED,
  2272. .field_width = 4,
  2273. .field_pos = ID_AA64MMFR2_EL1_E0PD_SHIFT,
  2274. .matches = has_cpuid_feature,
  2275. .min_field_value = 1,
  2276. .cpu_enable = cpu_enable_e0pd,
  2277. },
  2278. #endif
  2279. {
  2280. .desc = "Random Number Generator",
  2281. .capability = ARM64_HAS_RNG,
  2282. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2283. .matches = has_cpuid_feature,
  2284. .sys_reg = SYS_ID_AA64ISAR0_EL1,
  2285. .field_pos = ID_AA64ISAR0_EL1_RNDR_SHIFT,
  2286. .field_width = 4,
  2287. .sign = FTR_UNSIGNED,
  2288. .min_field_value = 1,
  2289. },
  2290. #ifdef CONFIG_ARM64_BTI
  2291. {
  2292. .desc = "Branch Target Identification",
  2293. .capability = ARM64_BTI,
  2294. #ifdef CONFIG_ARM64_BTI_KERNEL
  2295. .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
  2296. #else
  2297. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2298. #endif
  2299. .matches = has_cpuid_feature,
  2300. .cpu_enable = bti_enable,
  2301. .sys_reg = SYS_ID_AA64PFR1_EL1,
  2302. .field_pos = ID_AA64PFR1_EL1_BT_SHIFT,
  2303. .field_width = 4,
  2304. .min_field_value = ID_AA64PFR1_EL1_BT_IMP,
  2305. .sign = FTR_UNSIGNED,
  2306. },
  2307. #endif
  2308. #ifdef CONFIG_ARM64_MTE
  2309. {
  2310. .desc = "Memory Tagging Extension",
  2311. .capability = ARM64_MTE,
  2312. .type = ARM64_CPUCAP_STRICT_BOOT_CPU_FEATURE,
  2313. .matches = has_cpuid_feature,
  2314. .sys_reg = SYS_ID_AA64PFR1_EL1,
  2315. .field_pos = ID_AA64PFR1_EL1_MTE_SHIFT,
  2316. .field_width = 4,
  2317. .min_field_value = ID_AA64PFR1_EL1_MTE_MTE2,
  2318. .sign = FTR_UNSIGNED,
  2319. .cpu_enable = cpu_enable_mte,
  2320. },
  2321. {
  2322. .desc = "Asymmetric MTE Tag Check Fault",
  2323. .capability = ARM64_MTE_ASYMM,
  2324. .type = ARM64_CPUCAP_BOOT_CPU_FEATURE,
  2325. .matches = has_cpuid_feature,
  2326. .sys_reg = SYS_ID_AA64PFR1_EL1,
  2327. .field_pos = ID_AA64PFR1_EL1_MTE_SHIFT,
  2328. .field_width = 4,
  2329. .min_field_value = ID_AA64PFR1_EL1_MTE_MTE3,
  2330. .sign = FTR_UNSIGNED,
  2331. },
  2332. #endif /* CONFIG_ARM64_MTE */
  2333. {
  2334. .desc = "RCpc load-acquire (LDAPR)",
  2335. .capability = ARM64_HAS_LDAPR,
  2336. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2337. .sys_reg = SYS_ID_AA64ISAR1_EL1,
  2338. .sign = FTR_UNSIGNED,
  2339. .field_pos = ID_AA64ISAR1_EL1_LRCPC_SHIFT,
  2340. .field_width = 4,
  2341. .matches = has_cpuid_feature,
  2342. .min_field_value = 1,
  2343. },
  2344. #ifdef CONFIG_ARM64_SME
  2345. {
  2346. .desc = "Scalable Matrix Extension",
  2347. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2348. .capability = ARM64_SME,
  2349. .sys_reg = SYS_ID_AA64PFR1_EL1,
  2350. .sign = FTR_UNSIGNED,
  2351. .field_pos = ID_AA64PFR1_EL1_SME_SHIFT,
  2352. .field_width = 4,
  2353. .min_field_value = ID_AA64PFR1_EL1_SME_IMP,
  2354. .matches = has_cpuid_feature,
  2355. .cpu_enable = sme_kernel_enable,
  2356. },
  2357. /* FA64 should be sorted after the base SME capability */
  2358. {
  2359. .desc = "FA64",
  2360. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2361. .capability = ARM64_SME_FA64,
  2362. .sys_reg = SYS_ID_AA64SMFR0_EL1,
  2363. .sign = FTR_UNSIGNED,
  2364. .field_pos = ID_AA64SMFR0_EL1_FA64_SHIFT,
  2365. .field_width = 1,
  2366. .min_field_value = ID_AA64SMFR0_EL1_FA64_IMP,
  2367. .matches = has_cpuid_feature,
  2368. .cpu_enable = fa64_kernel_enable,
  2369. },
  2370. #endif /* CONFIG_ARM64_SME */
  2371. {
  2372. .desc = "WFx with timeout",
  2373. .capability = ARM64_HAS_WFXT,
  2374. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2375. .sys_reg = SYS_ID_AA64ISAR2_EL1,
  2376. .sign = FTR_UNSIGNED,
  2377. .field_pos = ID_AA64ISAR2_EL1_WFxT_SHIFT,
  2378. .field_width = 4,
  2379. .matches = has_cpuid_feature,
  2380. .min_field_value = ID_AA64ISAR2_EL1_WFxT_IMP,
  2381. },
  2382. {
  2383. .desc = "Trap EL0 IMPLEMENTATION DEFINED functionality",
  2384. .capability = ARM64_HAS_TIDCP1,
  2385. .type = ARM64_CPUCAP_SYSTEM_FEATURE,
  2386. .sys_reg = SYS_ID_AA64MMFR1_EL1,
  2387. .sign = FTR_UNSIGNED,
  2388. .field_pos = ID_AA64MMFR1_EL1_TIDCP1_SHIFT,
  2389. .field_width = 4,
  2390. .min_field_value = ID_AA64MMFR1_EL1_TIDCP1_IMP,
  2391. .matches = has_cpuid_feature,
  2392. .cpu_enable = cpu_trap_el0_impdef,
  2393. },
  2394. {},
  2395. };
  2396. #define HWCAP_CPUID_MATCH(reg, field, width, s, min_value) \
  2397. .matches = has_user_cpuid_feature, \
  2398. .sys_reg = reg, \
  2399. .field_pos = field, \
  2400. .field_width = width, \
  2401. .sign = s, \
  2402. .min_field_value = min_value,
  2403. #define __HWCAP_CAP(name, cap_type, cap) \
  2404. .desc = name, \
  2405. .type = ARM64_CPUCAP_SYSTEM_FEATURE, \
  2406. .hwcap_type = cap_type, \
  2407. .hwcap = cap, \
  2408. #define HWCAP_CAP(reg, field, width, s, min_value, cap_type, cap) \
  2409. { \
  2410. __HWCAP_CAP(#cap, cap_type, cap) \
  2411. HWCAP_CPUID_MATCH(reg, field, width, s, min_value) \
  2412. }
  2413. #define HWCAP_MULTI_CAP(list, cap_type, cap) \
  2414. { \
  2415. __HWCAP_CAP(#cap, cap_type, cap) \
  2416. .matches = cpucap_multi_entry_cap_matches, \
  2417. .match_list = list, \
  2418. }
  2419. #define HWCAP_CAP_MATCH(match, cap_type, cap) \
  2420. { \
  2421. __HWCAP_CAP(#cap, cap_type, cap) \
  2422. .matches = match, \
  2423. }
  2424. #ifdef CONFIG_ARM64_PTR_AUTH
  2425. static const struct arm64_cpu_capabilities ptr_auth_hwcap_addr_matches[] = {
  2426. {
  2427. HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_APA_SHIFT,
  2428. 4, FTR_UNSIGNED,
  2429. ID_AA64ISAR1_EL1_APA_PAuth)
  2430. },
  2431. {
  2432. HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_APA3_SHIFT,
  2433. 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_APA3_PAuth)
  2434. },
  2435. {
  2436. HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_API_SHIFT,
  2437. 4, FTR_UNSIGNED, ID_AA64ISAR1_EL1_API_PAuth)
  2438. },
  2439. {},
  2440. };
  2441. static const struct arm64_cpu_capabilities ptr_auth_hwcap_gen_matches[] = {
  2442. {
  2443. HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_GPA_SHIFT,
  2444. 4, FTR_UNSIGNED, ID_AA64ISAR1_EL1_GPA_IMP)
  2445. },
  2446. {
  2447. HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_GPA3_SHIFT,
  2448. 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_GPA3_IMP)
  2449. },
  2450. {
  2451. HWCAP_CPUID_MATCH(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_GPI_SHIFT,
  2452. 4, FTR_UNSIGNED, ID_AA64ISAR1_EL1_GPI_IMP)
  2453. },
  2454. {},
  2455. };
  2456. #endif
  2457. static const struct arm64_cpu_capabilities arm64_elf_hwcaps[] = {
  2458. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_PMULL),
  2459. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_AES),
  2460. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA1_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA1),
  2461. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA2_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA2),
  2462. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA2_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_SHA512),
  2463. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_CRC32_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_CRC32),
  2464. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_ATOMIC_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ATOMICS),
  2465. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_RDM_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDRDM),
  2466. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SHA3_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SHA3),
  2467. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SM3_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM3),
  2468. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_SM4_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SM4),
  2469. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_DP_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDDP),
  2470. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_FHM_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDFHM),
  2471. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_TS_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FLAGM),
  2472. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_TS_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_FLAGM2),
  2473. HWCAP_CAP(SYS_ID_AA64ISAR0_EL1, ID_AA64ISAR0_EL1_RNDR_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RNG),
  2474. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_FP_SHIFT, 4, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_FP),
  2475. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_FP_SHIFT, 4, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FPHP),
  2476. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_AdvSIMD_SHIFT, 4, FTR_SIGNED, 0, CAP_HWCAP, KERNEL_HWCAP_ASIMD),
  2477. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_AdvSIMD_SHIFT, 4, FTR_SIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ASIMDHP),
  2478. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_DIT_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DIT),
  2479. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_DPB_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DCPOP),
  2480. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_DPB_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_DCPODP),
  2481. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_JSCVT_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_JSCVT),
  2482. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_FCMA_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FCMA),
  2483. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_LRCPC_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_LRCPC),
  2484. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_LRCPC_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_ILRCPC),
  2485. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_FRINTTS_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_FRINT),
  2486. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_SB_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_SB),
  2487. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_BF16),
  2488. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, 2, CAP_HWCAP, KERNEL_HWCAP_EBF16),
  2489. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_DGH_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_DGH),
  2490. HWCAP_CAP(SYS_ID_AA64ISAR1_EL1, ID_AA64ISAR1_EL1_I8MM_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_I8MM),
  2491. HWCAP_CAP(SYS_ID_AA64MMFR2_EL1, ID_AA64MMFR2_EL1_AT_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_USCAT),
  2492. #ifdef CONFIG_ARM64_SVE
  2493. HWCAP_CAP(SYS_ID_AA64PFR0_EL1, ID_AA64PFR0_EL1_SVE_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR0_EL1_SVE_IMP, CAP_HWCAP, KERNEL_HWCAP_SVE),
  2494. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_SVEver_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_SVEver_SVE2, CAP_HWCAP, KERNEL_HWCAP_SVE2),
  2495. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_AES_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEAES),
  2496. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_AES_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_AES_PMULL128, CAP_HWCAP, KERNEL_HWCAP_SVEPMULL),
  2497. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_BitPerm_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_BitPerm_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEBITPERM),
  2498. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_BF16_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEBF16),
  2499. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_BF16_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_BF16_EBF16, CAP_HWCAP, KERNEL_HWCAP_SVE_EBF16),
  2500. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_SHA3_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_SHA3_IMP, CAP_HWCAP, KERNEL_HWCAP_SVESHA3),
  2501. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_SM4_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_SM4_IMP, CAP_HWCAP, KERNEL_HWCAP_SVESM4),
  2502. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_I8MM_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_I8MM_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEI8MM),
  2503. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_F32MM_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_F32MM_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF32MM),
  2504. HWCAP_CAP(SYS_ID_AA64ZFR0_EL1, ID_AA64ZFR0_EL1_F64MM_SHIFT, 4, FTR_UNSIGNED, ID_AA64ZFR0_EL1_F64MM_IMP, CAP_HWCAP, KERNEL_HWCAP_SVEF64MM),
  2505. #endif
  2506. HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_SSBS_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_SSBS_SSBS2, CAP_HWCAP, KERNEL_HWCAP_SSBS),
  2507. #ifdef CONFIG_ARM64_BTI
  2508. HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_BT_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_BT_IMP, CAP_HWCAP, KERNEL_HWCAP_BTI),
  2509. #endif
  2510. #ifdef CONFIG_ARM64_PTR_AUTH
  2511. HWCAP_MULTI_CAP(ptr_auth_hwcap_addr_matches, CAP_HWCAP, KERNEL_HWCAP_PACA),
  2512. HWCAP_MULTI_CAP(ptr_auth_hwcap_gen_matches, CAP_HWCAP, KERNEL_HWCAP_PACG),
  2513. #endif
  2514. #ifdef CONFIG_ARM64_MTE
  2515. HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_MTE_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_MTE_MTE2, CAP_HWCAP, KERNEL_HWCAP_MTE),
  2516. HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_MTE_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_MTE_MTE3, CAP_HWCAP, KERNEL_HWCAP_MTE3),
  2517. #endif /* CONFIG_ARM64_MTE */
  2518. HWCAP_CAP(SYS_ID_AA64MMFR0_EL1, ID_AA64MMFR0_EL1_ECV_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_ECV),
  2519. HWCAP_CAP(SYS_ID_AA64MMFR1_EL1, ID_AA64MMFR1_EL1_AFP_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_AFP),
  2520. HWCAP_CAP(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_RPRES_SHIFT, 4, FTR_UNSIGNED, 1, CAP_HWCAP, KERNEL_HWCAP_RPRES),
  2521. HWCAP_CAP(SYS_ID_AA64ISAR2_EL1, ID_AA64ISAR2_EL1_WFxT_SHIFT, 4, FTR_UNSIGNED, ID_AA64ISAR2_EL1_WFxT_IMP, CAP_HWCAP, KERNEL_HWCAP_WFXT),
  2522. #ifdef CONFIG_ARM64_SME
  2523. HWCAP_CAP(SYS_ID_AA64PFR1_EL1, ID_AA64PFR1_EL1_SME_SHIFT, 4, FTR_UNSIGNED, ID_AA64PFR1_EL1_SME_IMP, CAP_HWCAP, KERNEL_HWCAP_SME),
  2524. HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_FA64_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_FA64_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_FA64),
  2525. HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_I16I64_SHIFT, 4, FTR_UNSIGNED, ID_AA64SMFR0_EL1_I16I64_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_I16I64),
  2526. HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_F64F64_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_F64F64_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F64F64),
  2527. HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_I8I32_SHIFT, 4, FTR_UNSIGNED, ID_AA64SMFR0_EL1_I8I32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_I8I32),
  2528. HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_F16F32_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_F16F32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F16F32),
  2529. HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_B16F32_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_B16F32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_B16F32),
  2530. HWCAP_CAP(SYS_ID_AA64SMFR0_EL1, ID_AA64SMFR0_EL1_F32F32_SHIFT, 1, FTR_UNSIGNED, ID_AA64SMFR0_EL1_F32F32_IMP, CAP_HWCAP, KERNEL_HWCAP_SME_F32F32),
  2531. #endif /* CONFIG_ARM64_SME */
  2532. {},
  2533. };
  2534. #ifdef CONFIG_COMPAT
  2535. static bool compat_has_neon(const struct arm64_cpu_capabilities *cap, int scope)
  2536. {
  2537. /*
  2538. * Check that all of MVFR1_EL1.{SIMDSP, SIMDInt, SIMDLS} are available,
  2539. * in line with that of arm32 as in vfp_init(). We make sure that the
  2540. * check is future proof, by making sure value is non-zero.
  2541. */
  2542. u32 mvfr1;
  2543. WARN_ON(scope == SCOPE_LOCAL_CPU && preemptible());
  2544. if (scope == SCOPE_SYSTEM)
  2545. mvfr1 = read_sanitised_ftr_reg(SYS_MVFR1_EL1);
  2546. else
  2547. mvfr1 = read_sysreg_s(SYS_MVFR1_EL1);
  2548. return cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDSP_SHIFT) &&
  2549. cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDINT_SHIFT) &&
  2550. cpuid_feature_extract_unsigned_field(mvfr1, MVFR1_SIMDLS_SHIFT);
  2551. }
  2552. #endif
  2553. static const struct arm64_cpu_capabilities compat_elf_hwcaps[] = {
  2554. #ifdef CONFIG_COMPAT
  2555. HWCAP_CAP_MATCH(compat_has_neon, CAP_COMPAT_HWCAP, COMPAT_HWCAP_NEON),
  2556. HWCAP_CAP(SYS_MVFR1_EL1, MVFR1_SIMDFMAC_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv4),
  2557. /* Arm v8 mandates MVFR0.FPDP == {0, 2}. So, piggy back on this for the presence of VFP support */
  2558. HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_FPDP_SHIFT, 4, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFP),
  2559. HWCAP_CAP(SYS_MVFR0_EL1, MVFR0_FPDP_SHIFT, 4, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP, COMPAT_HWCAP_VFPv3),
  2560. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, 4, FTR_UNSIGNED, 2, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_PMULL),
  2561. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_AES_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_AES),
  2562. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA1_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA1),
  2563. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_SHA2_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_SHA2),
  2564. HWCAP_CAP(SYS_ID_ISAR5_EL1, ID_ISAR5_CRC32_SHIFT, 4, FTR_UNSIGNED, 1, CAP_COMPAT_HWCAP2, COMPAT_HWCAP2_CRC32),
  2565. #endif
  2566. {},
  2567. };
  2568. static void cap_set_elf_hwcap(const struct arm64_cpu_capabilities *cap)
  2569. {
  2570. switch (cap->hwcap_type) {
  2571. case CAP_HWCAP:
  2572. cpu_set_feature(cap->hwcap);
  2573. break;
  2574. #ifdef CONFIG_COMPAT
  2575. case CAP_COMPAT_HWCAP:
  2576. compat_elf_hwcap |= (u32)cap->hwcap;
  2577. break;
  2578. case CAP_COMPAT_HWCAP2:
  2579. compat_elf_hwcap2 |= (u32)cap->hwcap;
  2580. break;
  2581. #endif
  2582. default:
  2583. WARN_ON(1);
  2584. break;
  2585. }
  2586. }
  2587. /* Check if we have a particular HWCAP enabled */
  2588. static bool cpus_have_elf_hwcap(const struct arm64_cpu_capabilities *cap)
  2589. {
  2590. bool rc;
  2591. switch (cap->hwcap_type) {
  2592. case CAP_HWCAP:
  2593. rc = cpu_have_feature(cap->hwcap);
  2594. break;
  2595. #ifdef CONFIG_COMPAT
  2596. case CAP_COMPAT_HWCAP:
  2597. rc = (compat_elf_hwcap & (u32)cap->hwcap) != 0;
  2598. break;
  2599. case CAP_COMPAT_HWCAP2:
  2600. rc = (compat_elf_hwcap2 & (u32)cap->hwcap) != 0;
  2601. break;
  2602. #endif
  2603. default:
  2604. WARN_ON(1);
  2605. rc = false;
  2606. }
  2607. return rc;
  2608. }
  2609. static void setup_elf_hwcaps(const struct arm64_cpu_capabilities *hwcaps)
  2610. {
  2611. /* We support emulation of accesses to CPU ID feature registers */
  2612. cpu_set_named_feature(CPUID);
  2613. for (; hwcaps->matches; hwcaps++)
  2614. if (hwcaps->matches(hwcaps, cpucap_default_scope(hwcaps)))
  2615. cap_set_elf_hwcap(hwcaps);
  2616. }
  2617. static void update_cpu_capabilities(u16 scope_mask)
  2618. {
  2619. int i;
  2620. const struct arm64_cpu_capabilities *caps;
  2621. scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
  2622. for (i = 0; i < ARM64_NCAPS; i++) {
  2623. caps = cpu_hwcaps_ptrs[i];
  2624. if (!caps || !(caps->type & scope_mask) ||
  2625. cpus_have_cap(caps->capability) ||
  2626. !caps->matches(caps, cpucap_default_scope(caps)))
  2627. continue;
  2628. if (caps->desc)
  2629. pr_info("detected: %s\n", caps->desc);
  2630. cpus_set_cap(caps->capability);
  2631. if ((scope_mask & SCOPE_BOOT_CPU) && (caps->type & SCOPE_BOOT_CPU))
  2632. set_bit(caps->capability, boot_capabilities);
  2633. }
  2634. }
  2635. /*
  2636. * Enable all the available capabilities on this CPU. The capabilities
  2637. * with BOOT_CPU scope are handled separately and hence skipped here.
  2638. */
  2639. static int cpu_enable_non_boot_scope_capabilities(void *__unused)
  2640. {
  2641. int i;
  2642. u16 non_boot_scope = SCOPE_ALL & ~SCOPE_BOOT_CPU;
  2643. for_each_available_cap(i) {
  2644. const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[i];
  2645. if (WARN_ON(!cap))
  2646. continue;
  2647. if (!(cap->type & non_boot_scope))
  2648. continue;
  2649. if (cap->cpu_enable)
  2650. cap->cpu_enable(cap);
  2651. }
  2652. return 0;
  2653. }
  2654. /*
  2655. * Run through the enabled capabilities and enable() it on all active
  2656. * CPUs
  2657. */
  2658. static void __init enable_cpu_capabilities(u16 scope_mask)
  2659. {
  2660. int i;
  2661. const struct arm64_cpu_capabilities *caps;
  2662. bool boot_scope;
  2663. scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
  2664. boot_scope = !!(scope_mask & SCOPE_BOOT_CPU);
  2665. for (i = 0; i < ARM64_NCAPS; i++) {
  2666. unsigned int num;
  2667. caps = cpu_hwcaps_ptrs[i];
  2668. if (!caps || !(caps->type & scope_mask))
  2669. continue;
  2670. num = caps->capability;
  2671. if (!cpus_have_cap(num))
  2672. continue;
  2673. if (boot_scope && caps->cpu_enable)
  2674. /*
  2675. * Capabilities with SCOPE_BOOT_CPU scope are finalised
  2676. * before any secondary CPU boots. Thus, each secondary
  2677. * will enable the capability as appropriate via
  2678. * check_local_cpu_capabilities(). The only exception is
  2679. * the boot CPU, for which the capability must be
  2680. * enabled here. This approach avoids costly
  2681. * stop_machine() calls for this case.
  2682. */
  2683. caps->cpu_enable(caps);
  2684. }
  2685. /*
  2686. * For all non-boot scope capabilities, use stop_machine()
  2687. * as it schedules the work allowing us to modify PSTATE,
  2688. * instead of on_each_cpu() which uses an IPI, giving us a
  2689. * PSTATE that disappears when we return.
  2690. */
  2691. if (!boot_scope)
  2692. stop_machine(cpu_enable_non_boot_scope_capabilities,
  2693. NULL, cpu_online_mask);
  2694. }
  2695. /*
  2696. * Run through the list of capabilities to check for conflicts.
  2697. * If the system has already detected a capability, take necessary
  2698. * action on this CPU.
  2699. */
  2700. static void verify_local_cpu_caps(u16 scope_mask)
  2701. {
  2702. int i;
  2703. bool cpu_has_cap, system_has_cap;
  2704. const struct arm64_cpu_capabilities *caps;
  2705. scope_mask &= ARM64_CPUCAP_SCOPE_MASK;
  2706. for (i = 0; i < ARM64_NCAPS; i++) {
  2707. caps = cpu_hwcaps_ptrs[i];
  2708. if (!caps || !(caps->type & scope_mask))
  2709. continue;
  2710. cpu_has_cap = caps->matches(caps, SCOPE_LOCAL_CPU);
  2711. system_has_cap = cpus_have_cap(caps->capability);
  2712. if (system_has_cap) {
  2713. /*
  2714. * Check if the new CPU misses an advertised feature,
  2715. * which is not safe to miss.
  2716. */
  2717. if (!cpu_has_cap && !cpucap_late_cpu_optional(caps))
  2718. break;
  2719. /*
  2720. * We have to issue cpu_enable() irrespective of
  2721. * whether the CPU has it or not, as it is enabeld
  2722. * system wide. It is upto the call back to take
  2723. * appropriate action on this CPU.
  2724. */
  2725. if (caps->cpu_enable)
  2726. caps->cpu_enable(caps);
  2727. } else {
  2728. /*
  2729. * Check if the CPU has this capability if it isn't
  2730. * safe to have when the system doesn't.
  2731. */
  2732. if (cpu_has_cap && !cpucap_late_cpu_permitted(caps))
  2733. break;
  2734. }
  2735. }
  2736. if (i < ARM64_NCAPS) {
  2737. pr_crit("CPU%d: Detected conflict for capability %d (%s), System: %d, CPU: %d\n",
  2738. smp_processor_id(), caps->capability,
  2739. caps->desc, system_has_cap, cpu_has_cap);
  2740. if (cpucap_panic_on_conflict(caps))
  2741. cpu_panic_kernel();
  2742. else
  2743. cpu_die_early();
  2744. }
  2745. }
  2746. /*
  2747. * Check for CPU features that are used in early boot
  2748. * based on the Boot CPU value.
  2749. */
  2750. static void check_early_cpu_features(void)
  2751. {
  2752. verify_cpu_asid_bits();
  2753. verify_local_cpu_caps(SCOPE_BOOT_CPU);
  2754. }
  2755. static void
  2756. __verify_local_elf_hwcaps(const struct arm64_cpu_capabilities *caps)
  2757. {
  2758. for (; caps->matches; caps++)
  2759. if (cpus_have_elf_hwcap(caps) && !caps->matches(caps, SCOPE_LOCAL_CPU)) {
  2760. pr_crit("CPU%d: missing HWCAP: %s\n",
  2761. smp_processor_id(), caps->desc);
  2762. cpu_die_early();
  2763. }
  2764. }
  2765. static void verify_local_elf_hwcaps(void)
  2766. {
  2767. __verify_local_elf_hwcaps(arm64_elf_hwcaps);
  2768. if (id_aa64pfr0_32bit_el0(read_cpuid(ID_AA64PFR0_EL1)))
  2769. __verify_local_elf_hwcaps(compat_elf_hwcaps);
  2770. }
  2771. static void verify_sve_features(void)
  2772. {
  2773. u64 safe_zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
  2774. u64 zcr = read_zcr_features();
  2775. unsigned int safe_len = safe_zcr & ZCR_ELx_LEN_MASK;
  2776. unsigned int len = zcr & ZCR_ELx_LEN_MASK;
  2777. if (len < safe_len || vec_verify_vq_map(ARM64_VEC_SVE)) {
  2778. pr_crit("CPU%d: SVE: vector length support mismatch\n",
  2779. smp_processor_id());
  2780. cpu_die_early();
  2781. }
  2782. /* Add checks on other ZCR bits here if necessary */
  2783. }
  2784. static void verify_sme_features(void)
  2785. {
  2786. u64 safe_smcr = read_sanitised_ftr_reg(SYS_SMCR_EL1);
  2787. u64 smcr = read_smcr_features();
  2788. unsigned int safe_len = safe_smcr & SMCR_ELx_LEN_MASK;
  2789. unsigned int len = smcr & SMCR_ELx_LEN_MASK;
  2790. if (len < safe_len || vec_verify_vq_map(ARM64_VEC_SME)) {
  2791. pr_crit("CPU%d: SME: vector length support mismatch\n",
  2792. smp_processor_id());
  2793. cpu_die_early();
  2794. }
  2795. /* Add checks on other SMCR bits here if necessary */
  2796. }
  2797. static void verify_hyp_capabilities(void)
  2798. {
  2799. u64 safe_mmfr1, mmfr0, mmfr1;
  2800. int parange, ipa_max;
  2801. unsigned int safe_vmid_bits, vmid_bits;
  2802. if (!IS_ENABLED(CONFIG_KVM))
  2803. return;
  2804. safe_mmfr1 = read_sanitised_ftr_reg(SYS_ID_AA64MMFR1_EL1);
  2805. mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
  2806. mmfr1 = read_cpuid(ID_AA64MMFR1_EL1);
  2807. /* Verify VMID bits */
  2808. safe_vmid_bits = get_vmid_bits(safe_mmfr1);
  2809. vmid_bits = get_vmid_bits(mmfr1);
  2810. if (vmid_bits < safe_vmid_bits) {
  2811. pr_crit("CPU%d: VMID width mismatch\n", smp_processor_id());
  2812. cpu_die_early();
  2813. }
  2814. /* Verify IPA range */
  2815. parange = cpuid_feature_extract_unsigned_field(mmfr0,
  2816. ID_AA64MMFR0_EL1_PARANGE_SHIFT);
  2817. ipa_max = id_aa64mmfr0_parange_to_phys_shift(parange);
  2818. if (ipa_max < get_kvm_ipa_limit()) {
  2819. pr_crit("CPU%d: IPA range mismatch\n", smp_processor_id());
  2820. cpu_die_early();
  2821. }
  2822. }
  2823. /*
  2824. * Run through the enabled system capabilities and enable() it on this CPU.
  2825. * The capabilities were decided based on the available CPUs at the boot time.
  2826. * Any new CPU should match the system wide status of the capability. If the
  2827. * new CPU doesn't have a capability which the system now has enabled, we
  2828. * cannot do anything to fix it up and could cause unexpected failures. So
  2829. * we park the CPU.
  2830. */
  2831. static void verify_local_cpu_capabilities(void)
  2832. {
  2833. /*
  2834. * The capabilities with SCOPE_BOOT_CPU are checked from
  2835. * check_early_cpu_features(), as they need to be verified
  2836. * on all secondary CPUs.
  2837. */
  2838. verify_local_cpu_caps(SCOPE_ALL & ~SCOPE_BOOT_CPU);
  2839. verify_local_elf_hwcaps();
  2840. if (system_supports_sve())
  2841. verify_sve_features();
  2842. if (system_supports_sme())
  2843. verify_sme_features();
  2844. if (is_hyp_mode_available())
  2845. verify_hyp_capabilities();
  2846. }
  2847. void check_local_cpu_capabilities(void)
  2848. {
  2849. /*
  2850. * All secondary CPUs should conform to the early CPU features
  2851. * in use by the kernel based on boot CPU.
  2852. */
  2853. check_early_cpu_features();
  2854. /*
  2855. * If we haven't finalised the system capabilities, this CPU gets
  2856. * a chance to update the errata work arounds and local features.
  2857. * Otherwise, this CPU should verify that it has all the system
  2858. * advertised capabilities.
  2859. */
  2860. if (!system_capabilities_finalized())
  2861. update_cpu_capabilities(SCOPE_LOCAL_CPU);
  2862. else
  2863. verify_local_cpu_capabilities();
  2864. }
  2865. static void __init setup_boot_cpu_capabilities(void)
  2866. {
  2867. /* Detect capabilities with either SCOPE_BOOT_CPU or SCOPE_LOCAL_CPU */
  2868. update_cpu_capabilities(SCOPE_BOOT_CPU | SCOPE_LOCAL_CPU);
  2869. /* Enable the SCOPE_BOOT_CPU capabilities alone right away */
  2870. enable_cpu_capabilities(SCOPE_BOOT_CPU);
  2871. }
  2872. bool this_cpu_has_cap(unsigned int n)
  2873. {
  2874. if (!WARN_ON(preemptible()) && n < ARM64_NCAPS) {
  2875. const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
  2876. if (cap)
  2877. return cap->matches(cap, SCOPE_LOCAL_CPU);
  2878. }
  2879. return false;
  2880. }
  2881. EXPORT_SYMBOL_GPL(this_cpu_has_cap);
  2882. /*
  2883. * This helper function is used in a narrow window when,
  2884. * - The system wide safe registers are set with all the SMP CPUs and,
  2885. * - The SYSTEM_FEATURE cpu_hwcaps may not have been set.
  2886. * In all other cases cpus_have_{const_}cap() should be used.
  2887. */
  2888. static bool __maybe_unused __system_matches_cap(unsigned int n)
  2889. {
  2890. if (n < ARM64_NCAPS) {
  2891. const struct arm64_cpu_capabilities *cap = cpu_hwcaps_ptrs[n];
  2892. if (cap)
  2893. return cap->matches(cap, SCOPE_SYSTEM);
  2894. }
  2895. return false;
  2896. }
  2897. void cpu_set_feature(unsigned int num)
  2898. {
  2899. set_bit(num, elf_hwcap);
  2900. }
  2901. bool cpu_have_feature(unsigned int num)
  2902. {
  2903. return test_bit(num, elf_hwcap);
  2904. }
  2905. EXPORT_SYMBOL_GPL(cpu_have_feature);
  2906. unsigned long cpu_get_elf_hwcap(void)
  2907. {
  2908. /*
  2909. * We currently only populate the first 32 bits of AT_HWCAP. Please
  2910. * note that for userspace compatibility we guarantee that bits 62
  2911. * and 63 will always be returned as 0.
  2912. */
  2913. return elf_hwcap[0];
  2914. }
  2915. unsigned long cpu_get_elf_hwcap2(void)
  2916. {
  2917. return elf_hwcap[1];
  2918. }
  2919. static void __init setup_system_capabilities(void)
  2920. {
  2921. /*
  2922. * We have finalised the system-wide safe feature
  2923. * registers, finalise the capabilities that depend
  2924. * on it. Also enable all the available capabilities,
  2925. * that are not enabled already.
  2926. */
  2927. update_cpu_capabilities(SCOPE_SYSTEM);
  2928. enable_cpu_capabilities(SCOPE_ALL & ~SCOPE_BOOT_CPU);
  2929. }
  2930. void __init setup_cpu_features(void)
  2931. {
  2932. u32 cwg;
  2933. setup_system_capabilities();
  2934. setup_elf_hwcaps(arm64_elf_hwcaps);
  2935. if (system_supports_32bit_el0()) {
  2936. setup_elf_hwcaps(compat_elf_hwcaps);
  2937. elf_hwcap_fixup();
  2938. }
  2939. if (system_uses_ttbr0_pan())
  2940. pr_info("emulated: Privileged Access Never (PAN) using TTBR0_EL1 switching\n");
  2941. sve_setup();
  2942. sme_setup();
  2943. minsigstksz_setup();
  2944. /*
  2945. * Check for sane CTR_EL0.CWG value.
  2946. */
  2947. cwg = cache_type_cwg();
  2948. if (!cwg)
  2949. pr_warn("No Cache Writeback Granule information, assuming %d\n",
  2950. ARCH_DMA_MINALIGN);
  2951. }
  2952. static int enable_mismatched_32bit_el0(unsigned int cpu)
  2953. {
  2954. /*
  2955. * The first 32-bit-capable CPU we detected and so can no longer
  2956. * be offlined by userspace. -1 indicates we haven't yet onlined
  2957. * a 32-bit-capable CPU.
  2958. */
  2959. static int lucky_winner = -1;
  2960. struct cpuinfo_arm64 *info = &per_cpu(cpu_data, cpu);
  2961. bool cpu_32bit = id_aa64pfr0_32bit_el0(info->reg_id_aa64pfr0);
  2962. if (cpu_32bit) {
  2963. cpumask_set_cpu(cpu, cpu_32bit_el0_mask);
  2964. static_branch_enable_cpuslocked(&arm64_mismatched_32bit_el0);
  2965. }
  2966. if (cpumask_test_cpu(0, cpu_32bit_el0_mask) == cpu_32bit)
  2967. return 0;
  2968. if (lucky_winner >= 0)
  2969. return 0;
  2970. /*
  2971. * We've detected a mismatch. We need to keep one of our CPUs with
  2972. * 32-bit EL0 online so that is_cpu_allowed() doesn't end up rejecting
  2973. * every CPU in the system for a 32-bit task.
  2974. */
  2975. lucky_winner = cpu_32bit ? cpu : cpumask_any_and(cpu_32bit_el0_mask,
  2976. cpu_active_mask);
  2977. get_cpu_device(lucky_winner)->offline_disabled = true;
  2978. setup_elf_hwcaps(compat_elf_hwcaps);
  2979. elf_hwcap_fixup();
  2980. pr_info("Asymmetric 32-bit EL0 support detected on CPU %u; CPU hot-unplug disabled on CPU %u\n",
  2981. cpu, lucky_winner);
  2982. return 0;
  2983. }
  2984. static int __init init_32bit_el0_mask(void)
  2985. {
  2986. if (!allow_mismatched_32bit_el0)
  2987. return 0;
  2988. if (!zalloc_cpumask_var(&cpu_32bit_el0_mask, GFP_KERNEL))
  2989. return -ENOMEM;
  2990. return cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  2991. "arm64/mismatched_32bit_el0:online",
  2992. enable_mismatched_32bit_el0, NULL);
  2993. }
  2994. subsys_initcall_sync(init_32bit_el0_mask);
  2995. static void __maybe_unused cpu_enable_cnp(struct arm64_cpu_capabilities const *cap)
  2996. {
  2997. cpu_replace_ttbr1(lm_alias(swapper_pg_dir), idmap_pg_dir);
  2998. }
  2999. /*
  3000. * We emulate only the following system register space.
  3001. * Op0 = 0x3, CRn = 0x0, Op1 = 0x0, CRm = [0, 2 - 7]
  3002. * See Table C5-6 System instruction encodings for System register accesses,
  3003. * ARMv8 ARM(ARM DDI 0487A.f) for more details.
  3004. */
  3005. static inline bool __attribute_const__ is_emulated(u32 id)
  3006. {
  3007. return (sys_reg_Op0(id) == 0x3 &&
  3008. sys_reg_CRn(id) == 0x0 &&
  3009. sys_reg_Op1(id) == 0x0 &&
  3010. (sys_reg_CRm(id) == 0 ||
  3011. ((sys_reg_CRm(id) >= 2) && (sys_reg_CRm(id) <= 7))));
  3012. }
  3013. /*
  3014. * With CRm == 0, reg should be one of :
  3015. * MIDR_EL1, MPIDR_EL1 or REVIDR_EL1.
  3016. */
  3017. static inline int emulate_id_reg(u32 id, u64 *valp)
  3018. {
  3019. switch (id) {
  3020. case SYS_MIDR_EL1:
  3021. *valp = read_cpuid_id();
  3022. break;
  3023. case SYS_MPIDR_EL1:
  3024. *valp = SYS_MPIDR_SAFE_VAL;
  3025. break;
  3026. case SYS_REVIDR_EL1:
  3027. /* IMPLEMENTATION DEFINED values are emulated with 0 */
  3028. *valp = 0;
  3029. break;
  3030. default:
  3031. return -EINVAL;
  3032. }
  3033. return 0;
  3034. }
  3035. static int emulate_sys_reg(u32 id, u64 *valp)
  3036. {
  3037. struct arm64_ftr_reg *regp;
  3038. if (!is_emulated(id))
  3039. return -EINVAL;
  3040. if (sys_reg_CRm(id) == 0)
  3041. return emulate_id_reg(id, valp);
  3042. regp = get_arm64_ftr_reg_nowarn(id);
  3043. if (regp)
  3044. *valp = arm64_ftr_reg_user_value(regp);
  3045. else
  3046. /*
  3047. * The untracked registers are either IMPLEMENTATION DEFINED
  3048. * (e.g, ID_AFR0_EL1) or reserved RAZ.
  3049. */
  3050. *valp = 0;
  3051. return 0;
  3052. }
  3053. int do_emulate_mrs(struct pt_regs *regs, u32 sys_reg, u32 rt)
  3054. {
  3055. int rc;
  3056. u64 val;
  3057. rc = emulate_sys_reg(sys_reg, &val);
  3058. if (!rc) {
  3059. pt_regs_write_reg(regs, rt, val);
  3060. arm64_skip_faulting_instruction(regs, AARCH64_INSN_SIZE);
  3061. }
  3062. return rc;
  3063. }
  3064. static int emulate_mrs(struct pt_regs *regs, u32 insn)
  3065. {
  3066. u32 sys_reg, rt;
  3067. /*
  3068. * sys_reg values are defined as used in mrs/msr instruction.
  3069. * shift the imm value to get the encoding.
  3070. */
  3071. sys_reg = (u32)aarch64_insn_decode_immediate(AARCH64_INSN_IMM_16, insn) << 5;
  3072. rt = aarch64_insn_decode_register(AARCH64_INSN_REGTYPE_RT, insn);
  3073. return do_emulate_mrs(regs, sys_reg, rt);
  3074. }
  3075. static struct undef_hook mrs_hook = {
  3076. .instr_mask = 0xffff0000,
  3077. .instr_val = 0xd5380000,
  3078. .pstate_mask = PSR_AA32_MODE_MASK,
  3079. .pstate_val = PSR_MODE_EL0t,
  3080. .fn = emulate_mrs,
  3081. };
  3082. static int __init enable_mrs_emulation(void)
  3083. {
  3084. register_undef_hook(&mrs_hook);
  3085. return 0;
  3086. }
  3087. core_initcall(enable_mrs_emulation);
  3088. enum mitigation_state arm64_get_meltdown_state(void)
  3089. {
  3090. if (__meltdown_safe)
  3091. return SPECTRE_UNAFFECTED;
  3092. if (arm64_kernel_unmapped_at_el0())
  3093. return SPECTRE_MITIGATED;
  3094. return SPECTRE_VULNERABLE;
  3095. }
  3096. ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr,
  3097. char *buf)
  3098. {
  3099. switch (arm64_get_meltdown_state()) {
  3100. case SPECTRE_UNAFFECTED:
  3101. return sprintf(buf, "Not affected\n");
  3102. case SPECTRE_MITIGATED:
  3103. return sprintf(buf, "Mitigation: PTI\n");
  3104. default:
  3105. return sprintf(buf, "Vulnerable\n");
  3106. }
  3107. }