cpufeature.c 128 KB

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