fpsimd.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * FP/SIMD context switching and fault handling
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Author: Catalin Marinas <[email protected]>
  7. */
  8. #include <linux/bitmap.h>
  9. #include <linux/bitops.h>
  10. #include <linux/bottom_half.h>
  11. #include <linux/bug.h>
  12. #include <linux/cache.h>
  13. #include <linux/compat.h>
  14. #include <linux/compiler.h>
  15. #include <linux/cpu.h>
  16. #include <linux/cpu_pm.h>
  17. #include <linux/ctype.h>
  18. #include <linux/kernel.h>
  19. #include <linux/linkage.h>
  20. #include <linux/irqflags.h>
  21. #include <linux/init.h>
  22. #include <linux/percpu.h>
  23. #include <linux/prctl.h>
  24. #include <linux/preempt.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/sched/signal.h>
  27. #include <linux/sched/task_stack.h>
  28. #include <linux/signal.h>
  29. #include <linux/slab.h>
  30. #include <linux/stddef.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/swab.h>
  33. #include <asm/esr.h>
  34. #include <asm/exception.h>
  35. #include <asm/fpsimd.h>
  36. #include <asm/cpufeature.h>
  37. #include <asm/cputype.h>
  38. #include <asm/neon.h>
  39. #include <asm/processor.h>
  40. #include <asm/simd.h>
  41. #include <asm/sigcontext.h>
  42. #include <asm/sysreg.h>
  43. #include <asm/traps.h>
  44. #include <asm/virt.h>
  45. #define FPEXC_IOF (1 << 0)
  46. #define FPEXC_DZF (1 << 1)
  47. #define FPEXC_OFF (1 << 2)
  48. #define FPEXC_UFF (1 << 3)
  49. #define FPEXC_IXF (1 << 4)
  50. #define FPEXC_IDF (1 << 7)
  51. /*
  52. * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
  53. *
  54. * In order to reduce the number of times the FPSIMD state is needlessly saved
  55. * and restored, we need to keep track of two things:
  56. * (a) for each task, we need to remember which CPU was the last one to have
  57. * the task's FPSIMD state loaded into its FPSIMD registers;
  58. * (b) for each CPU, we need to remember which task's userland FPSIMD state has
  59. * been loaded into its FPSIMD registers most recently, or whether it has
  60. * been used to perform kernel mode NEON in the meantime.
  61. *
  62. * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
  63. * the id of the current CPU every time the state is loaded onto a CPU. For (b),
  64. * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
  65. * address of the userland FPSIMD state of the task that was loaded onto the CPU
  66. * the most recently, or NULL if kernel mode NEON has been performed after that.
  67. *
  68. * With this in place, we no longer have to restore the next FPSIMD state right
  69. * when switching between tasks. Instead, we can defer this check to userland
  70. * resume, at which time we verify whether the CPU's fpsimd_last_state and the
  71. * task's fpsimd_cpu are still mutually in sync. If this is the case, we
  72. * can omit the FPSIMD restore.
  73. *
  74. * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
  75. * indicate whether or not the userland FPSIMD state of the current task is
  76. * present in the registers. The flag is set unless the FPSIMD registers of this
  77. * CPU currently contain the most recent userland FPSIMD state of the current
  78. * task. If the task is behaving as a VMM, then this is will be managed by
  79. * KVM which will clear it to indicate that the vcpu FPSIMD state is currently
  80. * loaded on the CPU, allowing the state to be saved if a FPSIMD-aware
  81. * softirq kicks in. Upon vcpu_put(), KVM will save the vcpu FP state and
  82. * flag the register state as invalid.
  83. *
  84. * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may
  85. * save the task's FPSIMD context back to task_struct from softirq context.
  86. * To prevent this from racing with the manipulation of the task's FPSIMD state
  87. * from task context and thereby corrupting the state, it is necessary to
  88. * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE
  89. * flag with {, __}get_cpu_fpsimd_context(). This will still allow softirqs to
  90. * run but prevent them to use FPSIMD.
  91. *
  92. * For a certain task, the sequence may look something like this:
  93. * - the task gets scheduled in; if both the task's fpsimd_cpu field
  94. * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
  95. * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
  96. * cleared, otherwise it is set;
  97. *
  98. * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
  99. * userland FPSIMD state is copied from memory to the registers, the task's
  100. * fpsimd_cpu field is set to the id of the current CPU, the current
  101. * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
  102. * TIF_FOREIGN_FPSTATE flag is cleared;
  103. *
  104. * - the task executes an ordinary syscall; upon return to userland, the
  105. * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
  106. * restored;
  107. *
  108. * - the task executes a syscall which executes some NEON instructions; this is
  109. * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
  110. * register contents to memory, clears the fpsimd_last_state per-cpu variable
  111. * and sets the TIF_FOREIGN_FPSTATE flag;
  112. *
  113. * - the task gets preempted after kernel_neon_end() is called; as we have not
  114. * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
  115. * whatever is in the FPSIMD registers is not saved to memory, but discarded.
  116. */
  117. struct fpsimd_last_state_struct {
  118. struct user_fpsimd_state *st;
  119. void *sve_state;
  120. void *za_state;
  121. u64 *svcr;
  122. unsigned int sve_vl;
  123. unsigned int sme_vl;
  124. };
  125. static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
  126. __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX] = {
  127. #ifdef CONFIG_ARM64_SVE
  128. [ARM64_VEC_SVE] = {
  129. .type = ARM64_VEC_SVE,
  130. .name = "SVE",
  131. .min_vl = SVE_VL_MIN,
  132. .max_vl = SVE_VL_MIN,
  133. .max_virtualisable_vl = SVE_VL_MIN,
  134. },
  135. #endif
  136. #ifdef CONFIG_ARM64_SME
  137. [ARM64_VEC_SME] = {
  138. .type = ARM64_VEC_SME,
  139. .name = "SME",
  140. },
  141. #endif
  142. };
  143. static unsigned int vec_vl_inherit_flag(enum vec_type type)
  144. {
  145. switch (type) {
  146. case ARM64_VEC_SVE:
  147. return TIF_SVE_VL_INHERIT;
  148. case ARM64_VEC_SME:
  149. return TIF_SME_VL_INHERIT;
  150. default:
  151. WARN_ON_ONCE(1);
  152. return 0;
  153. }
  154. }
  155. struct vl_config {
  156. int __default_vl; /* Default VL for tasks */
  157. };
  158. static struct vl_config vl_config[ARM64_VEC_MAX];
  159. static inline int get_default_vl(enum vec_type type)
  160. {
  161. return READ_ONCE(vl_config[type].__default_vl);
  162. }
  163. #ifdef CONFIG_ARM64_SVE
  164. static inline int get_sve_default_vl(void)
  165. {
  166. return get_default_vl(ARM64_VEC_SVE);
  167. }
  168. static inline void set_default_vl(enum vec_type type, int val)
  169. {
  170. WRITE_ONCE(vl_config[type].__default_vl, val);
  171. }
  172. static inline void set_sve_default_vl(int val)
  173. {
  174. set_default_vl(ARM64_VEC_SVE, val);
  175. }
  176. static void __percpu *efi_sve_state;
  177. #else /* ! CONFIG_ARM64_SVE */
  178. /* Dummy declaration for code that will be optimised out: */
  179. extern void __percpu *efi_sve_state;
  180. #endif /* ! CONFIG_ARM64_SVE */
  181. #ifdef CONFIG_ARM64_SME
  182. static int get_sme_default_vl(void)
  183. {
  184. return get_default_vl(ARM64_VEC_SME);
  185. }
  186. static void set_sme_default_vl(int val)
  187. {
  188. set_default_vl(ARM64_VEC_SME, val);
  189. }
  190. static void sme_free(struct task_struct *);
  191. #else
  192. static inline void sme_free(struct task_struct *t) { }
  193. #endif
  194. DEFINE_PER_CPU(bool, fpsimd_context_busy);
  195. EXPORT_PER_CPU_SYMBOL(fpsimd_context_busy);
  196. static void fpsimd_bind_task_to_cpu(void);
  197. static void __get_cpu_fpsimd_context(void)
  198. {
  199. bool busy = __this_cpu_xchg(fpsimd_context_busy, true);
  200. WARN_ON(busy);
  201. }
  202. /*
  203. * Claim ownership of the CPU FPSIMD context for use by the calling context.
  204. *
  205. * The caller may freely manipulate the FPSIMD context metadata until
  206. * put_cpu_fpsimd_context() is called.
  207. *
  208. * The double-underscore version must only be called if you know the task
  209. * can't be preempted.
  210. *
  211. * On RT kernels local_bh_disable() is not sufficient because it only
  212. * serializes soft interrupt related sections via a local lock, but stays
  213. * preemptible. Disabling preemption is the right choice here as bottom
  214. * half processing is always in thread context on RT kernels so it
  215. * implicitly prevents bottom half processing as well.
  216. */
  217. static void get_cpu_fpsimd_context(void)
  218. {
  219. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  220. local_bh_disable();
  221. else
  222. preempt_disable();
  223. __get_cpu_fpsimd_context();
  224. }
  225. static void __put_cpu_fpsimd_context(void)
  226. {
  227. bool busy = __this_cpu_xchg(fpsimd_context_busy, false);
  228. WARN_ON(!busy); /* No matching get_cpu_fpsimd_context()? */
  229. }
  230. /*
  231. * Release the CPU FPSIMD context.
  232. *
  233. * Must be called from a context in which get_cpu_fpsimd_context() was
  234. * previously called, with no call to put_cpu_fpsimd_context() in the
  235. * meantime.
  236. */
  237. static void put_cpu_fpsimd_context(void)
  238. {
  239. __put_cpu_fpsimd_context();
  240. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  241. local_bh_enable();
  242. else
  243. preempt_enable();
  244. }
  245. static bool have_cpu_fpsimd_context(void)
  246. {
  247. return !preemptible() && __this_cpu_read(fpsimd_context_busy);
  248. }
  249. unsigned int task_get_vl(const struct task_struct *task, enum vec_type type)
  250. {
  251. return task->thread.vl[type];
  252. }
  253. void task_set_vl(struct task_struct *task, enum vec_type type,
  254. unsigned long vl)
  255. {
  256. task->thread.vl[type] = vl;
  257. }
  258. unsigned int task_get_vl_onexec(const struct task_struct *task,
  259. enum vec_type type)
  260. {
  261. return task->thread.vl_onexec[type];
  262. }
  263. void task_set_vl_onexec(struct task_struct *task, enum vec_type type,
  264. unsigned long vl)
  265. {
  266. task->thread.vl_onexec[type] = vl;
  267. }
  268. /*
  269. * TIF_SME controls whether a task can use SME without trapping while
  270. * in userspace, when TIF_SME is set then we must have storage
  271. * alocated in sve_state and za_state to store the contents of both ZA
  272. * and the SVE registers for both streaming and non-streaming modes.
  273. *
  274. * If both SVCR.ZA and SVCR.SM are disabled then at any point we
  275. * may disable TIF_SME and reenable traps.
  276. */
  277. /*
  278. * TIF_SVE controls whether a task can use SVE without trapping while
  279. * in userspace, and also (together with TIF_SME) the way a task's
  280. * FPSIMD/SVE state is stored in thread_struct.
  281. *
  282. * The kernel uses this flag to track whether a user task is actively
  283. * using SVE, and therefore whether full SVE register state needs to
  284. * be tracked. If not, the cheaper FPSIMD context handling code can
  285. * be used instead of the more costly SVE equivalents.
  286. *
  287. * * TIF_SVE or SVCR.SM set:
  288. *
  289. * The task can execute SVE instructions while in userspace without
  290. * trapping to the kernel.
  291. *
  292. * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
  293. * corresponding Zn), P0-P15 and FFR are encoded in
  294. * task->thread.sve_state, formatted appropriately for vector
  295. * length task->thread.sve_vl or, if SVCR.SM is set,
  296. * task->thread.sme_vl.
  297. *
  298. * task->thread.sve_state must point to a valid buffer at least
  299. * sve_state_size(task) bytes in size.
  300. *
  301. * During any syscall, the kernel may optionally clear TIF_SVE and
  302. * discard the vector state except for the FPSIMD subset.
  303. *
  304. * * TIF_SVE clear:
  305. *
  306. * An attempt by the user task to execute an SVE instruction causes
  307. * do_sve_acc() to be called, which does some preparation and then
  308. * sets TIF_SVE.
  309. *
  310. * When stored, FPSIMD registers V0-V31 are encoded in
  311. * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
  312. * logically zero but not stored anywhere; P0-P15 and FFR are not
  313. * stored and have unspecified values from userspace's point of
  314. * view. For hygiene purposes, the kernel zeroes them on next use,
  315. * but userspace is discouraged from relying on this.
  316. *
  317. * task->thread.sve_state does not need to be non-NULL, valid or any
  318. * particular size: it must not be dereferenced.
  319. *
  320. * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
  321. * irrespective of whether TIF_SVE is clear or set, since these are
  322. * not vector length dependent.
  323. */
  324. /*
  325. * Update current's FPSIMD/SVE registers from thread_struct.
  326. *
  327. * This function should be called only when the FPSIMD/SVE state in
  328. * thread_struct is known to be up to date, when preparing to enter
  329. * userspace.
  330. */
  331. static void task_fpsimd_load(void)
  332. {
  333. bool restore_sve_regs = false;
  334. bool restore_ffr;
  335. WARN_ON(!system_supports_fpsimd());
  336. WARN_ON(!have_cpu_fpsimd_context());
  337. /* Check if we should restore SVE first */
  338. if (IS_ENABLED(CONFIG_ARM64_SVE) && test_thread_flag(TIF_SVE)) {
  339. sve_set_vq(sve_vq_from_vl(task_get_sve_vl(current)) - 1);
  340. restore_sve_regs = true;
  341. restore_ffr = true;
  342. }
  343. /* Restore SME, override SVE register configuration if needed */
  344. if (system_supports_sme()) {
  345. unsigned long sme_vl = task_get_sme_vl(current);
  346. /* Ensure VL is set up for restoring data */
  347. if (test_thread_flag(TIF_SME))
  348. sme_set_vq(sve_vq_from_vl(sme_vl) - 1);
  349. write_sysreg_s(current->thread.svcr, SYS_SVCR);
  350. if (thread_za_enabled(&current->thread))
  351. za_load_state(current->thread.za_state);
  352. if (thread_sm_enabled(&current->thread)) {
  353. restore_sve_regs = true;
  354. restore_ffr = system_supports_fa64();
  355. }
  356. }
  357. if (restore_sve_regs)
  358. sve_load_state(sve_pffr(&current->thread),
  359. &current->thread.uw.fpsimd_state.fpsr,
  360. restore_ffr);
  361. else
  362. fpsimd_load_state(&current->thread.uw.fpsimd_state);
  363. }
  364. /*
  365. * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
  366. * date with respect to the CPU registers. Note carefully that the
  367. * current context is the context last bound to the CPU stored in
  368. * last, if KVM is involved this may be the guest VM context rather
  369. * than the host thread for the VM pointed to by current. This means
  370. * that we must always reference the state storage via last rather
  371. * than via current, other than the TIF_ flags which KVM will
  372. * carefully maintain for us.
  373. */
  374. static void fpsimd_save(void)
  375. {
  376. struct fpsimd_last_state_struct const *last =
  377. this_cpu_ptr(&fpsimd_last_state);
  378. /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
  379. bool save_sve_regs = false;
  380. bool save_ffr;
  381. unsigned int vl;
  382. WARN_ON(!system_supports_fpsimd());
  383. WARN_ON(!have_cpu_fpsimd_context());
  384. if (test_thread_flag(TIF_FOREIGN_FPSTATE))
  385. return;
  386. if (test_thread_flag(TIF_SVE)) {
  387. save_sve_regs = true;
  388. save_ffr = true;
  389. vl = last->sve_vl;
  390. }
  391. if (system_supports_sme()) {
  392. u64 *svcr = last->svcr;
  393. *svcr = read_sysreg_s(SYS_SVCR);
  394. if (*svcr & SVCR_ZA_MASK)
  395. za_save_state(last->za_state);
  396. /* If we are in streaming mode override regular SVE. */
  397. if (*svcr & SVCR_SM_MASK) {
  398. save_sve_regs = true;
  399. save_ffr = system_supports_fa64();
  400. vl = last->sme_vl;
  401. }
  402. }
  403. if (IS_ENABLED(CONFIG_ARM64_SVE) && save_sve_regs) {
  404. /* Get the configured VL from RDVL, will account for SM */
  405. if (WARN_ON(sve_get_vl() != vl)) {
  406. /*
  407. * Can't save the user regs, so current would
  408. * re-enter user with corrupt state.
  409. * There's no way to recover, so kill it:
  410. */
  411. force_signal_inject(SIGKILL, SI_KERNEL, 0, 0);
  412. return;
  413. }
  414. sve_save_state((char *)last->sve_state +
  415. sve_ffr_offset(vl),
  416. &last->st->fpsr, save_ffr);
  417. } else {
  418. fpsimd_save_state(last->st);
  419. }
  420. }
  421. /*
  422. * All vector length selection from userspace comes through here.
  423. * We're on a slow path, so some sanity-checks are included.
  424. * If things go wrong there's a bug somewhere, but try to fall back to a
  425. * safe choice.
  426. */
  427. static unsigned int find_supported_vector_length(enum vec_type type,
  428. unsigned int vl)
  429. {
  430. struct vl_info *info = &vl_info[type];
  431. int bit;
  432. int max_vl = info->max_vl;
  433. if (WARN_ON(!sve_vl_valid(vl)))
  434. vl = info->min_vl;
  435. if (WARN_ON(!sve_vl_valid(max_vl)))
  436. max_vl = info->min_vl;
  437. if (vl > max_vl)
  438. vl = max_vl;
  439. if (vl < info->min_vl)
  440. vl = info->min_vl;
  441. bit = find_next_bit(info->vq_map, SVE_VQ_MAX,
  442. __vq_to_bit(sve_vq_from_vl(vl)));
  443. return sve_vl_from_vq(__bit_to_vq(bit));
  444. }
  445. #if defined(CONFIG_ARM64_SVE) && defined(CONFIG_SYSCTL)
  446. static int vec_proc_do_default_vl(struct ctl_table *table, int write,
  447. void *buffer, size_t *lenp, loff_t *ppos)
  448. {
  449. struct vl_info *info = table->extra1;
  450. enum vec_type type = info->type;
  451. int ret;
  452. int vl = get_default_vl(type);
  453. struct ctl_table tmp_table = {
  454. .data = &vl,
  455. .maxlen = sizeof(vl),
  456. };
  457. ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
  458. if (ret || !write)
  459. return ret;
  460. /* Writing -1 has the special meaning "set to max": */
  461. if (vl == -1)
  462. vl = info->max_vl;
  463. if (!sve_vl_valid(vl))
  464. return -EINVAL;
  465. set_default_vl(type, find_supported_vector_length(type, vl));
  466. return 0;
  467. }
  468. static struct ctl_table sve_default_vl_table[] = {
  469. {
  470. .procname = "sve_default_vector_length",
  471. .mode = 0644,
  472. .proc_handler = vec_proc_do_default_vl,
  473. .extra1 = &vl_info[ARM64_VEC_SVE],
  474. },
  475. { }
  476. };
  477. static int __init sve_sysctl_init(void)
  478. {
  479. if (system_supports_sve())
  480. if (!register_sysctl("abi", sve_default_vl_table))
  481. return -EINVAL;
  482. return 0;
  483. }
  484. #else /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
  485. static int __init sve_sysctl_init(void) { return 0; }
  486. #endif /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
  487. #if defined(CONFIG_ARM64_SME) && defined(CONFIG_SYSCTL)
  488. static struct ctl_table sme_default_vl_table[] = {
  489. {
  490. .procname = "sme_default_vector_length",
  491. .mode = 0644,
  492. .proc_handler = vec_proc_do_default_vl,
  493. .extra1 = &vl_info[ARM64_VEC_SME],
  494. },
  495. { }
  496. };
  497. static int __init sme_sysctl_init(void)
  498. {
  499. if (system_supports_sme())
  500. if (!register_sysctl("abi", sme_default_vl_table))
  501. return -EINVAL;
  502. return 0;
  503. }
  504. #else /* ! (CONFIG_ARM64_SME && CONFIG_SYSCTL) */
  505. static int __init sme_sysctl_init(void) { return 0; }
  506. #endif /* ! (CONFIG_ARM64_SME && CONFIG_SYSCTL) */
  507. #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
  508. (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
  509. #ifdef CONFIG_CPU_BIG_ENDIAN
  510. static __uint128_t arm64_cpu_to_le128(__uint128_t x)
  511. {
  512. u64 a = swab64(x);
  513. u64 b = swab64(x >> 64);
  514. return ((__uint128_t)a << 64) | b;
  515. }
  516. #else
  517. static __uint128_t arm64_cpu_to_le128(__uint128_t x)
  518. {
  519. return x;
  520. }
  521. #endif
  522. #define arm64_le128_to_cpu(x) arm64_cpu_to_le128(x)
  523. static void __fpsimd_to_sve(void *sst, struct user_fpsimd_state const *fst,
  524. unsigned int vq)
  525. {
  526. unsigned int i;
  527. __uint128_t *p;
  528. for (i = 0; i < SVE_NUM_ZREGS; ++i) {
  529. p = (__uint128_t *)ZREG(sst, vq, i);
  530. *p = arm64_cpu_to_le128(fst->vregs[i]);
  531. }
  532. }
  533. /*
  534. * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
  535. * task->thread.sve_state.
  536. *
  537. * Task can be a non-runnable task, or current. In the latter case,
  538. * the caller must have ownership of the cpu FPSIMD context before calling
  539. * this function.
  540. * task->thread.sve_state must point to at least sve_state_size(task)
  541. * bytes of allocated kernel memory.
  542. * task->thread.uw.fpsimd_state must be up to date before calling this
  543. * function.
  544. */
  545. static void fpsimd_to_sve(struct task_struct *task)
  546. {
  547. unsigned int vq;
  548. void *sst = task->thread.sve_state;
  549. struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
  550. if (!system_supports_sve() && !system_supports_sme())
  551. return;
  552. vq = sve_vq_from_vl(thread_get_cur_vl(&task->thread));
  553. __fpsimd_to_sve(sst, fst, vq);
  554. }
  555. /*
  556. * Transfer the SVE state in task->thread.sve_state to
  557. * task->thread.uw.fpsimd_state.
  558. *
  559. * Task can be a non-runnable task, or current. In the latter case,
  560. * the caller must have ownership of the cpu FPSIMD context before calling
  561. * this function.
  562. * task->thread.sve_state must point to at least sve_state_size(task)
  563. * bytes of allocated kernel memory.
  564. * task->thread.sve_state must be up to date before calling this function.
  565. */
  566. static void sve_to_fpsimd(struct task_struct *task)
  567. {
  568. unsigned int vq, vl;
  569. void const *sst = task->thread.sve_state;
  570. struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
  571. unsigned int i;
  572. __uint128_t const *p;
  573. if (!system_supports_sve() && !system_supports_sme())
  574. return;
  575. vl = thread_get_cur_vl(&task->thread);
  576. vq = sve_vq_from_vl(vl);
  577. for (i = 0; i < SVE_NUM_ZREGS; ++i) {
  578. p = (__uint128_t const *)ZREG(sst, vq, i);
  579. fst->vregs[i] = arm64_le128_to_cpu(*p);
  580. }
  581. }
  582. #ifdef CONFIG_ARM64_SVE
  583. /*
  584. * Call __sve_free() directly only if you know task can't be scheduled
  585. * or preempted.
  586. */
  587. static void __sve_free(struct task_struct *task)
  588. {
  589. kfree(task->thread.sve_state);
  590. task->thread.sve_state = NULL;
  591. }
  592. static void sve_free(struct task_struct *task)
  593. {
  594. WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
  595. __sve_free(task);
  596. }
  597. /*
  598. * Return how many bytes of memory are required to store the full SVE
  599. * state for task, given task's currently configured vector length.
  600. */
  601. size_t sve_state_size(struct task_struct const *task)
  602. {
  603. unsigned int vl = 0;
  604. if (system_supports_sve())
  605. vl = task_get_sve_vl(task);
  606. if (system_supports_sme())
  607. vl = max(vl, task_get_sme_vl(task));
  608. return SVE_SIG_REGS_SIZE(sve_vq_from_vl(vl));
  609. }
  610. /*
  611. * Ensure that task->thread.sve_state is allocated and sufficiently large.
  612. *
  613. * This function should be used only in preparation for replacing
  614. * task->thread.sve_state with new data. The memory is always zeroed
  615. * here to prevent stale data from showing through: this is done in
  616. * the interest of testability and predictability: except in the
  617. * do_sve_acc() case, there is no ABI requirement to hide stale data
  618. * written previously be task.
  619. */
  620. void sve_alloc(struct task_struct *task, bool flush)
  621. {
  622. if (task->thread.sve_state) {
  623. if (flush)
  624. memset(task->thread.sve_state, 0,
  625. sve_state_size(task));
  626. return;
  627. }
  628. /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
  629. task->thread.sve_state =
  630. kzalloc(sve_state_size(task), GFP_KERNEL);
  631. }
  632. /*
  633. * Force the FPSIMD state shared with SVE to be updated in the SVE state
  634. * even if the SVE state is the current active state.
  635. *
  636. * This should only be called by ptrace. task must be non-runnable.
  637. * task->thread.sve_state must point to at least sve_state_size(task)
  638. * bytes of allocated kernel memory.
  639. */
  640. void fpsimd_force_sync_to_sve(struct task_struct *task)
  641. {
  642. fpsimd_to_sve(task);
  643. }
  644. /*
  645. * Ensure that task->thread.sve_state is up to date with respect to
  646. * the user task, irrespective of when SVE is in use or not.
  647. *
  648. * This should only be called by ptrace. task must be non-runnable.
  649. * task->thread.sve_state must point to at least sve_state_size(task)
  650. * bytes of allocated kernel memory.
  651. */
  652. void fpsimd_sync_to_sve(struct task_struct *task)
  653. {
  654. if (!test_tsk_thread_flag(task, TIF_SVE) &&
  655. !thread_sm_enabled(&task->thread))
  656. fpsimd_to_sve(task);
  657. }
  658. /*
  659. * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
  660. * the user task, irrespective of whether SVE is in use or not.
  661. *
  662. * This should only be called by ptrace. task must be non-runnable.
  663. * task->thread.sve_state must point to at least sve_state_size(task)
  664. * bytes of allocated kernel memory.
  665. */
  666. void sve_sync_to_fpsimd(struct task_struct *task)
  667. {
  668. if (test_tsk_thread_flag(task, TIF_SVE) ||
  669. thread_sm_enabled(&task->thread))
  670. sve_to_fpsimd(task);
  671. }
  672. /*
  673. * Ensure that task->thread.sve_state is up to date with respect to
  674. * the task->thread.uw.fpsimd_state.
  675. *
  676. * This should only be called by ptrace to merge new FPSIMD register
  677. * values into a task for which SVE is currently active.
  678. * task must be non-runnable.
  679. * task->thread.sve_state must point to at least sve_state_size(task)
  680. * bytes of allocated kernel memory.
  681. * task->thread.uw.fpsimd_state must already have been initialised with
  682. * the new FPSIMD register values to be merged in.
  683. */
  684. void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
  685. {
  686. unsigned int vq;
  687. void *sst = task->thread.sve_state;
  688. struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
  689. if (!test_tsk_thread_flag(task, TIF_SVE) &&
  690. !thread_sm_enabled(&task->thread))
  691. return;
  692. vq = sve_vq_from_vl(thread_get_cur_vl(&task->thread));
  693. memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
  694. __fpsimd_to_sve(sst, fst, vq);
  695. }
  696. int vec_set_vector_length(struct task_struct *task, enum vec_type type,
  697. unsigned long vl, unsigned long flags)
  698. {
  699. bool free_sme = false;
  700. if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
  701. PR_SVE_SET_VL_ONEXEC))
  702. return -EINVAL;
  703. if (!sve_vl_valid(vl))
  704. return -EINVAL;
  705. /*
  706. * Clamp to the maximum vector length that VL-agnostic code
  707. * can work with. A flag may be assigned in the future to
  708. * allow setting of larger vector lengths without confusing
  709. * older software.
  710. */
  711. if (vl > VL_ARCH_MAX)
  712. vl = VL_ARCH_MAX;
  713. vl = find_supported_vector_length(type, vl);
  714. if (flags & (PR_SVE_VL_INHERIT |
  715. PR_SVE_SET_VL_ONEXEC))
  716. task_set_vl_onexec(task, type, vl);
  717. else
  718. /* Reset VL to system default on next exec: */
  719. task_set_vl_onexec(task, type, 0);
  720. /* Only actually set the VL if not deferred: */
  721. if (flags & PR_SVE_SET_VL_ONEXEC)
  722. goto out;
  723. if (vl == task_get_vl(task, type))
  724. goto out;
  725. /*
  726. * To ensure the FPSIMD bits of the SVE vector registers are preserved,
  727. * write any live register state back to task_struct, and convert to a
  728. * regular FPSIMD thread.
  729. */
  730. if (task == current) {
  731. get_cpu_fpsimd_context();
  732. fpsimd_save();
  733. }
  734. fpsimd_flush_task_state(task);
  735. if (test_and_clear_tsk_thread_flag(task, TIF_SVE) ||
  736. thread_sm_enabled(&task->thread))
  737. sve_to_fpsimd(task);
  738. if (system_supports_sme()) {
  739. if (type == ARM64_VEC_SME ||
  740. !(task->thread.svcr & (SVCR_SM_MASK | SVCR_ZA_MASK))) {
  741. /*
  742. * We are changing the SME VL or weren't using
  743. * SME anyway, discard the state and force a
  744. * reallocation.
  745. */
  746. task->thread.svcr &= ~(SVCR_SM_MASK |
  747. SVCR_ZA_MASK);
  748. clear_tsk_thread_flag(task, TIF_SME);
  749. free_sme = true;
  750. }
  751. }
  752. if (task == current)
  753. put_cpu_fpsimd_context();
  754. task_set_vl(task, type, vl);
  755. /*
  756. * Free the changed states if they are not in use, SME will be
  757. * reallocated to the correct size on next use and we just
  758. * allocate SVE now in case it is needed for use in streaming
  759. * mode.
  760. */
  761. if (system_supports_sve()) {
  762. sve_free(task);
  763. sve_alloc(task, true);
  764. }
  765. if (free_sme)
  766. sme_free(task);
  767. out:
  768. update_tsk_thread_flag(task, vec_vl_inherit_flag(type),
  769. flags & PR_SVE_VL_INHERIT);
  770. return 0;
  771. }
  772. /*
  773. * Encode the current vector length and flags for return.
  774. * This is only required for prctl(): ptrace has separate fields.
  775. * SVE and SME use the same bits for _ONEXEC and _INHERIT.
  776. *
  777. * flags are as for vec_set_vector_length().
  778. */
  779. static int vec_prctl_status(enum vec_type type, unsigned long flags)
  780. {
  781. int ret;
  782. if (flags & PR_SVE_SET_VL_ONEXEC)
  783. ret = task_get_vl_onexec(current, type);
  784. else
  785. ret = task_get_vl(current, type);
  786. if (test_thread_flag(vec_vl_inherit_flag(type)))
  787. ret |= PR_SVE_VL_INHERIT;
  788. return ret;
  789. }
  790. /* PR_SVE_SET_VL */
  791. int sve_set_current_vl(unsigned long arg)
  792. {
  793. unsigned long vl, flags;
  794. int ret;
  795. vl = arg & PR_SVE_VL_LEN_MASK;
  796. flags = arg & ~vl;
  797. if (!system_supports_sve() || is_compat_task())
  798. return -EINVAL;
  799. ret = vec_set_vector_length(current, ARM64_VEC_SVE, vl, flags);
  800. if (ret)
  801. return ret;
  802. return vec_prctl_status(ARM64_VEC_SVE, flags);
  803. }
  804. /* PR_SVE_GET_VL */
  805. int sve_get_current_vl(void)
  806. {
  807. if (!system_supports_sve() || is_compat_task())
  808. return -EINVAL;
  809. return vec_prctl_status(ARM64_VEC_SVE, 0);
  810. }
  811. #ifdef CONFIG_ARM64_SME
  812. /* PR_SME_SET_VL */
  813. int sme_set_current_vl(unsigned long arg)
  814. {
  815. unsigned long vl, flags;
  816. int ret;
  817. vl = arg & PR_SME_VL_LEN_MASK;
  818. flags = arg & ~vl;
  819. if (!system_supports_sme() || is_compat_task())
  820. return -EINVAL;
  821. ret = vec_set_vector_length(current, ARM64_VEC_SME, vl, flags);
  822. if (ret)
  823. return ret;
  824. return vec_prctl_status(ARM64_VEC_SME, flags);
  825. }
  826. /* PR_SME_GET_VL */
  827. int sme_get_current_vl(void)
  828. {
  829. if (!system_supports_sme() || is_compat_task())
  830. return -EINVAL;
  831. return vec_prctl_status(ARM64_VEC_SME, 0);
  832. }
  833. #endif /* CONFIG_ARM64_SME */
  834. static void vec_probe_vqs(struct vl_info *info,
  835. DECLARE_BITMAP(map, SVE_VQ_MAX))
  836. {
  837. unsigned int vq, vl;
  838. bitmap_zero(map, SVE_VQ_MAX);
  839. for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
  840. write_vl(info->type, vq - 1); /* self-syncing */
  841. switch (info->type) {
  842. case ARM64_VEC_SVE:
  843. vl = sve_get_vl();
  844. break;
  845. case ARM64_VEC_SME:
  846. vl = sme_get_vl();
  847. break;
  848. default:
  849. vl = 0;
  850. break;
  851. }
  852. /* Minimum VL identified? */
  853. if (sve_vq_from_vl(vl) > vq)
  854. break;
  855. vq = sve_vq_from_vl(vl); /* skip intervening lengths */
  856. set_bit(__vq_to_bit(vq), map);
  857. }
  858. }
  859. /*
  860. * Initialise the set of known supported VQs for the boot CPU.
  861. * This is called during kernel boot, before secondary CPUs are brought up.
  862. */
  863. void __init vec_init_vq_map(enum vec_type type)
  864. {
  865. struct vl_info *info = &vl_info[type];
  866. vec_probe_vqs(info, info->vq_map);
  867. bitmap_copy(info->vq_partial_map, info->vq_map, SVE_VQ_MAX);
  868. }
  869. /*
  870. * If we haven't committed to the set of supported VQs yet, filter out
  871. * those not supported by the current CPU.
  872. * This function is called during the bring-up of early secondary CPUs only.
  873. */
  874. void vec_update_vq_map(enum vec_type type)
  875. {
  876. struct vl_info *info = &vl_info[type];
  877. DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
  878. vec_probe_vqs(info, tmp_map);
  879. bitmap_and(info->vq_map, info->vq_map, tmp_map, SVE_VQ_MAX);
  880. bitmap_or(info->vq_partial_map, info->vq_partial_map, tmp_map,
  881. SVE_VQ_MAX);
  882. }
  883. /*
  884. * Check whether the current CPU supports all VQs in the committed set.
  885. * This function is called during the bring-up of late secondary CPUs only.
  886. */
  887. int vec_verify_vq_map(enum vec_type type)
  888. {
  889. struct vl_info *info = &vl_info[type];
  890. DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
  891. unsigned long b;
  892. vec_probe_vqs(info, tmp_map);
  893. bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
  894. if (bitmap_intersects(tmp_map, info->vq_map, SVE_VQ_MAX)) {
  895. pr_warn("%s: cpu%d: Required vector length(s) missing\n",
  896. info->name, smp_processor_id());
  897. return -EINVAL;
  898. }
  899. if (!IS_ENABLED(CONFIG_KVM) || !is_hyp_mode_available())
  900. return 0;
  901. /*
  902. * For KVM, it is necessary to ensure that this CPU doesn't
  903. * support any vector length that guests may have probed as
  904. * unsupported.
  905. */
  906. /* Recover the set of supported VQs: */
  907. bitmap_complement(tmp_map, tmp_map, SVE_VQ_MAX);
  908. /* Find VQs supported that are not globally supported: */
  909. bitmap_andnot(tmp_map, tmp_map, info->vq_map, SVE_VQ_MAX);
  910. /* Find the lowest such VQ, if any: */
  911. b = find_last_bit(tmp_map, SVE_VQ_MAX);
  912. if (b >= SVE_VQ_MAX)
  913. return 0; /* no mismatches */
  914. /*
  915. * Mismatches above sve_max_virtualisable_vl are fine, since
  916. * no guest is allowed to configure ZCR_EL2.LEN to exceed this:
  917. */
  918. if (sve_vl_from_vq(__bit_to_vq(b)) <= info->max_virtualisable_vl) {
  919. pr_warn("%s: cpu%d: Unsupported vector length(s) present\n",
  920. info->name, smp_processor_id());
  921. return -EINVAL;
  922. }
  923. return 0;
  924. }
  925. static void __init sve_efi_setup(void)
  926. {
  927. int max_vl = 0;
  928. int i;
  929. if (!IS_ENABLED(CONFIG_EFI))
  930. return;
  931. for (i = 0; i < ARRAY_SIZE(vl_info); i++)
  932. max_vl = max(vl_info[i].max_vl, max_vl);
  933. /*
  934. * alloc_percpu() warns and prints a backtrace if this goes wrong.
  935. * This is evidence of a crippled system and we are returning void,
  936. * so no attempt is made to handle this situation here.
  937. */
  938. if (!sve_vl_valid(max_vl))
  939. goto fail;
  940. efi_sve_state = __alloc_percpu(
  941. SVE_SIG_REGS_SIZE(sve_vq_from_vl(max_vl)), SVE_VQ_BYTES);
  942. if (!efi_sve_state)
  943. goto fail;
  944. return;
  945. fail:
  946. panic("Cannot allocate percpu memory for EFI SVE save/restore");
  947. }
  948. /*
  949. * Enable SVE for EL1.
  950. * Intended for use by the cpufeatures code during CPU boot.
  951. */
  952. void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
  953. {
  954. write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
  955. isb();
  956. }
  957. /*
  958. * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
  959. * vector length.
  960. *
  961. * Use only if SVE is present.
  962. * This function clobbers the SVE vector length.
  963. */
  964. u64 read_zcr_features(void)
  965. {
  966. /*
  967. * Set the maximum possible VL, and write zeroes to all other
  968. * bits to see if they stick.
  969. */
  970. sve_kernel_enable(NULL);
  971. write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
  972. /* Return LEN value that would be written to get the maximum VL */
  973. return sve_vq_from_vl(sve_get_vl()) - 1;
  974. }
  975. void __init sve_setup(void)
  976. {
  977. struct vl_info *info = &vl_info[ARM64_VEC_SVE];
  978. u64 zcr;
  979. DECLARE_BITMAP(tmp_map, SVE_VQ_MAX);
  980. unsigned long b;
  981. if (!system_supports_sve())
  982. return;
  983. /*
  984. * The SVE architecture mandates support for 128-bit vectors,
  985. * so sve_vq_map must have at least SVE_VQ_MIN set.
  986. * If something went wrong, at least try to patch it up:
  987. */
  988. if (WARN_ON(!test_bit(__vq_to_bit(SVE_VQ_MIN), info->vq_map)))
  989. set_bit(__vq_to_bit(SVE_VQ_MIN), info->vq_map);
  990. zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
  991. info->max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
  992. /*
  993. * Sanity-check that the max VL we determined through CPU features
  994. * corresponds properly to sve_vq_map. If not, do our best:
  995. */
  996. if (WARN_ON(info->max_vl != find_supported_vector_length(ARM64_VEC_SVE,
  997. info->max_vl)))
  998. info->max_vl = find_supported_vector_length(ARM64_VEC_SVE,
  999. info->max_vl);
  1000. /*
  1001. * For the default VL, pick the maximum supported value <= 64.
  1002. * VL == 64 is guaranteed not to grow the signal frame.
  1003. */
  1004. set_sve_default_vl(find_supported_vector_length(ARM64_VEC_SVE, 64));
  1005. bitmap_andnot(tmp_map, info->vq_partial_map, info->vq_map,
  1006. SVE_VQ_MAX);
  1007. b = find_last_bit(tmp_map, SVE_VQ_MAX);
  1008. if (b >= SVE_VQ_MAX)
  1009. /* No non-virtualisable VLs found */
  1010. info->max_virtualisable_vl = SVE_VQ_MAX;
  1011. else if (WARN_ON(b == SVE_VQ_MAX - 1))
  1012. /* No virtualisable VLs? This is architecturally forbidden. */
  1013. info->max_virtualisable_vl = SVE_VQ_MIN;
  1014. else /* b + 1 < SVE_VQ_MAX */
  1015. info->max_virtualisable_vl = sve_vl_from_vq(__bit_to_vq(b + 1));
  1016. if (info->max_virtualisable_vl > info->max_vl)
  1017. info->max_virtualisable_vl = info->max_vl;
  1018. pr_info("%s: maximum available vector length %u bytes per vector\n",
  1019. info->name, info->max_vl);
  1020. pr_info("%s: default vector length %u bytes per vector\n",
  1021. info->name, get_sve_default_vl());
  1022. /* KVM decides whether to support mismatched systems. Just warn here: */
  1023. if (sve_max_virtualisable_vl() < sve_max_vl())
  1024. pr_warn("%s: unvirtualisable vector lengths present\n",
  1025. info->name);
  1026. sve_efi_setup();
  1027. }
  1028. /*
  1029. * Called from the put_task_struct() path, which cannot get here
  1030. * unless dead_task is really dead and not schedulable.
  1031. */
  1032. void fpsimd_release_task(struct task_struct *dead_task)
  1033. {
  1034. __sve_free(dead_task);
  1035. sme_free(dead_task);
  1036. }
  1037. #endif /* CONFIG_ARM64_SVE */
  1038. #ifdef CONFIG_ARM64_SME
  1039. /*
  1040. * Ensure that task->thread.za_state is allocated and sufficiently large.
  1041. *
  1042. * This function should be used only in preparation for replacing
  1043. * task->thread.za_state with new data. The memory is always zeroed
  1044. * here to prevent stale data from showing through: this is done in
  1045. * the interest of testability and predictability, the architecture
  1046. * guarantees that when ZA is enabled it will be zeroed.
  1047. */
  1048. void sme_alloc(struct task_struct *task, bool flush)
  1049. {
  1050. if (task->thread.za_state && flush) {
  1051. memset(task->thread.za_state, 0, za_state_size(task));
  1052. return;
  1053. }
  1054. /* This could potentially be up to 64K. */
  1055. task->thread.za_state =
  1056. kzalloc(za_state_size(task), GFP_KERNEL);
  1057. }
  1058. static void sme_free(struct task_struct *task)
  1059. {
  1060. kfree(task->thread.za_state);
  1061. task->thread.za_state = NULL;
  1062. }
  1063. void sme_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
  1064. {
  1065. /* Set priority for all PEs to architecturally defined minimum */
  1066. write_sysreg_s(read_sysreg_s(SYS_SMPRI_EL1) & ~SMPRI_EL1_PRIORITY_MASK,
  1067. SYS_SMPRI_EL1);
  1068. /* Allow SME in kernel */
  1069. write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_SMEN_EL1EN, CPACR_EL1);
  1070. isb();
  1071. /* Allow EL0 to access TPIDR2 */
  1072. write_sysreg(read_sysreg(SCTLR_EL1) | SCTLR_ELx_ENTP2, SCTLR_EL1);
  1073. isb();
  1074. }
  1075. /*
  1076. * This must be called after sme_kernel_enable(), we rely on the
  1077. * feature table being sorted to ensure this.
  1078. */
  1079. void fa64_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
  1080. {
  1081. /* Allow use of FA64 */
  1082. write_sysreg_s(read_sysreg_s(SYS_SMCR_EL1) | SMCR_ELx_FA64_MASK,
  1083. SYS_SMCR_EL1);
  1084. }
  1085. /*
  1086. * Read the pseudo-SMCR used by cpufeatures to identify the supported
  1087. * vector length.
  1088. *
  1089. * Use only if SME is present.
  1090. * This function clobbers the SME vector length.
  1091. */
  1092. u64 read_smcr_features(void)
  1093. {
  1094. sme_kernel_enable(NULL);
  1095. /*
  1096. * Set the maximum possible VL.
  1097. */
  1098. write_sysreg_s(read_sysreg_s(SYS_SMCR_EL1) | SMCR_ELx_LEN_MASK,
  1099. SYS_SMCR_EL1);
  1100. /* Return LEN value that would be written to get the maximum VL */
  1101. return sve_vq_from_vl(sme_get_vl()) - 1;
  1102. }
  1103. void __init sme_setup(void)
  1104. {
  1105. struct vl_info *info = &vl_info[ARM64_VEC_SME];
  1106. u64 smcr;
  1107. int min_bit;
  1108. if (!system_supports_sme())
  1109. return;
  1110. /*
  1111. * SME doesn't require any particular vector length be
  1112. * supported but it does require at least one. We should have
  1113. * disabled the feature entirely while bringing up CPUs but
  1114. * let's double check here.
  1115. */
  1116. WARN_ON(bitmap_empty(info->vq_map, SVE_VQ_MAX));
  1117. min_bit = find_last_bit(info->vq_map, SVE_VQ_MAX);
  1118. info->min_vl = sve_vl_from_vq(__bit_to_vq(min_bit));
  1119. smcr = read_sanitised_ftr_reg(SYS_SMCR_EL1);
  1120. info->max_vl = sve_vl_from_vq((smcr & SMCR_ELx_LEN_MASK) + 1);
  1121. /*
  1122. * Sanity-check that the max VL we determined through CPU features
  1123. * corresponds properly to sme_vq_map. If not, do our best:
  1124. */
  1125. if (WARN_ON(info->max_vl != find_supported_vector_length(ARM64_VEC_SME,
  1126. info->max_vl)))
  1127. info->max_vl = find_supported_vector_length(ARM64_VEC_SME,
  1128. info->max_vl);
  1129. WARN_ON(info->min_vl > info->max_vl);
  1130. /*
  1131. * For the default VL, pick the maximum supported value <= 32
  1132. * (256 bits) if there is one since this is guaranteed not to
  1133. * grow the signal frame when in streaming mode, otherwise the
  1134. * minimum available VL will be used.
  1135. */
  1136. set_sme_default_vl(find_supported_vector_length(ARM64_VEC_SME, 32));
  1137. pr_info("SME: minimum available vector length %u bytes per vector\n",
  1138. info->min_vl);
  1139. pr_info("SME: maximum available vector length %u bytes per vector\n",
  1140. info->max_vl);
  1141. pr_info("SME: default vector length %u bytes per vector\n",
  1142. get_sme_default_vl());
  1143. }
  1144. #endif /* CONFIG_ARM64_SME */
  1145. static void sve_init_regs(void)
  1146. {
  1147. /*
  1148. * Convert the FPSIMD state to SVE, zeroing all the state that
  1149. * is not shared with FPSIMD. If (as is likely) the current
  1150. * state is live in the registers then do this there and
  1151. * update our metadata for the current task including
  1152. * disabling the trap, otherwise update our in-memory copy.
  1153. * We are guaranteed to not be in streaming mode, we can only
  1154. * take a SVE trap when not in streaming mode and we can't be
  1155. * in streaming mode when taking a SME trap.
  1156. */
  1157. if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
  1158. unsigned long vq_minus_one =
  1159. sve_vq_from_vl(task_get_sve_vl(current)) - 1;
  1160. sve_set_vq(vq_minus_one);
  1161. sve_flush_live(true, vq_minus_one);
  1162. fpsimd_bind_task_to_cpu();
  1163. } else {
  1164. fpsimd_to_sve(current);
  1165. }
  1166. }
  1167. /*
  1168. * Trapped SVE access
  1169. *
  1170. * Storage is allocated for the full SVE state, the current FPSIMD
  1171. * register contents are migrated across, and the access trap is
  1172. * disabled.
  1173. *
  1174. * TIF_SVE should be clear on entry: otherwise, fpsimd_restore_current_state()
  1175. * would have disabled the SVE access trap for userspace during
  1176. * ret_to_user, making an SVE access trap impossible in that case.
  1177. */
  1178. void do_sve_acc(unsigned long esr, struct pt_regs *regs)
  1179. {
  1180. /* Even if we chose not to use SVE, the hardware could still trap: */
  1181. if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
  1182. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  1183. return;
  1184. }
  1185. sve_alloc(current, true);
  1186. if (!current->thread.sve_state) {
  1187. force_sig(SIGKILL);
  1188. return;
  1189. }
  1190. get_cpu_fpsimd_context();
  1191. if (test_and_set_thread_flag(TIF_SVE))
  1192. WARN_ON(1); /* SVE access shouldn't have trapped */
  1193. /*
  1194. * Even if the task can have used streaming mode we can only
  1195. * generate SVE access traps in normal SVE mode and
  1196. * transitioning out of streaming mode may discard any
  1197. * streaming mode state. Always clear the high bits to avoid
  1198. * any potential errors tracking what is properly initialised.
  1199. */
  1200. sve_init_regs();
  1201. put_cpu_fpsimd_context();
  1202. }
  1203. /*
  1204. * Trapped SME access
  1205. *
  1206. * Storage is allocated for the full SVE and SME state, the current
  1207. * FPSIMD register contents are migrated to SVE if SVE is not already
  1208. * active, and the access trap is disabled.
  1209. *
  1210. * TIF_SME should be clear on entry: otherwise, fpsimd_restore_current_state()
  1211. * would have disabled the SME access trap for userspace during
  1212. * ret_to_user, making an SVE access trap impossible in that case.
  1213. */
  1214. void do_sme_acc(unsigned long esr, struct pt_regs *regs)
  1215. {
  1216. /* Even if we chose not to use SME, the hardware could still trap: */
  1217. if (unlikely(!system_supports_sme()) || WARN_ON(is_compat_task())) {
  1218. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  1219. return;
  1220. }
  1221. /*
  1222. * If this not a trap due to SME being disabled then something
  1223. * is being used in the wrong mode, report as SIGILL.
  1224. */
  1225. if (ESR_ELx_ISS(esr) != ESR_ELx_SME_ISS_SME_DISABLED) {
  1226. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc, 0);
  1227. return;
  1228. }
  1229. sve_alloc(current, false);
  1230. sme_alloc(current, true);
  1231. if (!current->thread.sve_state || !current->thread.za_state) {
  1232. force_sig(SIGKILL);
  1233. return;
  1234. }
  1235. get_cpu_fpsimd_context();
  1236. /* With TIF_SME userspace shouldn't generate any traps */
  1237. if (test_and_set_thread_flag(TIF_SME))
  1238. WARN_ON(1);
  1239. if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
  1240. unsigned long vq_minus_one =
  1241. sve_vq_from_vl(task_get_sme_vl(current)) - 1;
  1242. sme_set_vq(vq_minus_one);
  1243. fpsimd_bind_task_to_cpu();
  1244. }
  1245. put_cpu_fpsimd_context();
  1246. }
  1247. /*
  1248. * Trapped FP/ASIMD access.
  1249. */
  1250. void do_fpsimd_acc(unsigned long esr, struct pt_regs *regs)
  1251. {
  1252. /* TODO: implement lazy context saving/restoring */
  1253. WARN_ON(1);
  1254. }
  1255. /*
  1256. * Raise a SIGFPE for the current process.
  1257. */
  1258. void do_fpsimd_exc(unsigned long esr, struct pt_regs *regs)
  1259. {
  1260. unsigned int si_code = FPE_FLTUNK;
  1261. if (esr & ESR_ELx_FP_EXC_TFV) {
  1262. if (esr & FPEXC_IOF)
  1263. si_code = FPE_FLTINV;
  1264. else if (esr & FPEXC_DZF)
  1265. si_code = FPE_FLTDIV;
  1266. else if (esr & FPEXC_OFF)
  1267. si_code = FPE_FLTOVF;
  1268. else if (esr & FPEXC_UFF)
  1269. si_code = FPE_FLTUND;
  1270. else if (esr & FPEXC_IXF)
  1271. si_code = FPE_FLTRES;
  1272. }
  1273. send_sig_fault(SIGFPE, si_code,
  1274. (void __user *)instruction_pointer(regs),
  1275. current);
  1276. }
  1277. void fpsimd_thread_switch(struct task_struct *next)
  1278. {
  1279. bool wrong_task, wrong_cpu;
  1280. if (!system_supports_fpsimd())
  1281. return;
  1282. __get_cpu_fpsimd_context();
  1283. /* Save unsaved fpsimd state, if any: */
  1284. fpsimd_save();
  1285. /*
  1286. * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
  1287. * state. For kernel threads, FPSIMD registers are never loaded
  1288. * and wrong_task and wrong_cpu will always be true.
  1289. */
  1290. wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
  1291. &next->thread.uw.fpsimd_state;
  1292. wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
  1293. update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
  1294. wrong_task || wrong_cpu);
  1295. __put_cpu_fpsimd_context();
  1296. }
  1297. static void fpsimd_flush_thread_vl(enum vec_type type)
  1298. {
  1299. int vl, supported_vl;
  1300. /*
  1301. * Reset the task vector length as required. This is where we
  1302. * ensure that all user tasks have a valid vector length
  1303. * configured: no kernel task can become a user task without
  1304. * an exec and hence a call to this function. By the time the
  1305. * first call to this function is made, all early hardware
  1306. * probing is complete, so __sve_default_vl should be valid.
  1307. * If a bug causes this to go wrong, we make some noise and
  1308. * try to fudge thread.sve_vl to a safe value here.
  1309. */
  1310. vl = task_get_vl_onexec(current, type);
  1311. if (!vl)
  1312. vl = get_default_vl(type);
  1313. if (WARN_ON(!sve_vl_valid(vl)))
  1314. vl = vl_info[type].min_vl;
  1315. supported_vl = find_supported_vector_length(type, vl);
  1316. if (WARN_ON(supported_vl != vl))
  1317. vl = supported_vl;
  1318. task_set_vl(current, type, vl);
  1319. /*
  1320. * If the task is not set to inherit, ensure that the vector
  1321. * length will be reset by a subsequent exec:
  1322. */
  1323. if (!test_thread_flag(vec_vl_inherit_flag(type)))
  1324. task_set_vl_onexec(current, type, 0);
  1325. }
  1326. void fpsimd_flush_thread(void)
  1327. {
  1328. void *sve_state = NULL;
  1329. void *za_state = NULL;
  1330. if (!system_supports_fpsimd())
  1331. return;
  1332. get_cpu_fpsimd_context();
  1333. fpsimd_flush_task_state(current);
  1334. memset(&current->thread.uw.fpsimd_state, 0,
  1335. sizeof(current->thread.uw.fpsimd_state));
  1336. if (system_supports_sve()) {
  1337. clear_thread_flag(TIF_SVE);
  1338. /* Defer kfree() while in atomic context */
  1339. sve_state = current->thread.sve_state;
  1340. current->thread.sve_state = NULL;
  1341. fpsimd_flush_thread_vl(ARM64_VEC_SVE);
  1342. }
  1343. if (system_supports_sme()) {
  1344. clear_thread_flag(TIF_SME);
  1345. /* Defer kfree() while in atomic context */
  1346. za_state = current->thread.za_state;
  1347. current->thread.za_state = NULL;
  1348. fpsimd_flush_thread_vl(ARM64_VEC_SME);
  1349. current->thread.svcr = 0;
  1350. }
  1351. put_cpu_fpsimd_context();
  1352. kfree(sve_state);
  1353. kfree(za_state);
  1354. }
  1355. /*
  1356. * Save the userland FPSIMD state of 'current' to memory, but only if the state
  1357. * currently held in the registers does in fact belong to 'current'
  1358. */
  1359. void fpsimd_preserve_current_state(void)
  1360. {
  1361. if (!system_supports_fpsimd())
  1362. return;
  1363. get_cpu_fpsimd_context();
  1364. fpsimd_save();
  1365. put_cpu_fpsimd_context();
  1366. }
  1367. /*
  1368. * Like fpsimd_preserve_current_state(), but ensure that
  1369. * current->thread.uw.fpsimd_state is updated so that it can be copied to
  1370. * the signal frame.
  1371. */
  1372. void fpsimd_signal_preserve_current_state(void)
  1373. {
  1374. fpsimd_preserve_current_state();
  1375. if (test_thread_flag(TIF_SVE))
  1376. sve_to_fpsimd(current);
  1377. }
  1378. /*
  1379. * Associate current's FPSIMD context with this cpu
  1380. * The caller must have ownership of the cpu FPSIMD context before calling
  1381. * this function.
  1382. */
  1383. static void fpsimd_bind_task_to_cpu(void)
  1384. {
  1385. struct fpsimd_last_state_struct *last =
  1386. this_cpu_ptr(&fpsimd_last_state);
  1387. WARN_ON(!system_supports_fpsimd());
  1388. last->st = &current->thread.uw.fpsimd_state;
  1389. last->sve_state = current->thread.sve_state;
  1390. last->za_state = current->thread.za_state;
  1391. last->sve_vl = task_get_sve_vl(current);
  1392. last->sme_vl = task_get_sme_vl(current);
  1393. last->svcr = &current->thread.svcr;
  1394. current->thread.fpsimd_cpu = smp_processor_id();
  1395. /*
  1396. * Toggle SVE and SME trapping for userspace if needed, these
  1397. * are serialsied by ret_to_user().
  1398. */
  1399. if (system_supports_sme()) {
  1400. if (test_thread_flag(TIF_SME))
  1401. sme_user_enable();
  1402. else
  1403. sme_user_disable();
  1404. }
  1405. if (system_supports_sve()) {
  1406. if (test_thread_flag(TIF_SVE))
  1407. sve_user_enable();
  1408. else
  1409. sve_user_disable();
  1410. }
  1411. }
  1412. void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st, void *sve_state,
  1413. unsigned int sve_vl, void *za_state,
  1414. unsigned int sme_vl, u64 *svcr)
  1415. {
  1416. struct fpsimd_last_state_struct *last =
  1417. this_cpu_ptr(&fpsimd_last_state);
  1418. WARN_ON(!system_supports_fpsimd());
  1419. WARN_ON(!in_softirq() && !irqs_disabled());
  1420. last->st = st;
  1421. last->svcr = svcr;
  1422. last->sve_state = sve_state;
  1423. last->za_state = za_state;
  1424. last->sve_vl = sve_vl;
  1425. last->sme_vl = sme_vl;
  1426. }
  1427. /*
  1428. * Load the userland FPSIMD state of 'current' from memory, but only if the
  1429. * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
  1430. * state of 'current'. This is called when we are preparing to return to
  1431. * userspace to ensure that userspace sees a good register state.
  1432. */
  1433. void fpsimd_restore_current_state(void)
  1434. {
  1435. /*
  1436. * For the tasks that were created before we detected the absence of
  1437. * FP/SIMD, the TIF_FOREIGN_FPSTATE could be set via fpsimd_thread_switch(),
  1438. * e.g, init. This could be then inherited by the children processes.
  1439. * If we later detect that the system doesn't support FP/SIMD,
  1440. * we must clear the flag for all the tasks to indicate that the
  1441. * FPSTATE is clean (as we can't have one) to avoid looping for ever in
  1442. * do_notify_resume().
  1443. */
  1444. if (!system_supports_fpsimd()) {
  1445. clear_thread_flag(TIF_FOREIGN_FPSTATE);
  1446. return;
  1447. }
  1448. get_cpu_fpsimd_context();
  1449. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  1450. task_fpsimd_load();
  1451. fpsimd_bind_task_to_cpu();
  1452. }
  1453. put_cpu_fpsimd_context();
  1454. }
  1455. /*
  1456. * Load an updated userland FPSIMD state for 'current' from memory and set the
  1457. * flag that indicates that the FPSIMD register contents are the most recent
  1458. * FPSIMD state of 'current'. This is used by the signal code to restore the
  1459. * register state when returning from a signal handler in FPSIMD only cases,
  1460. * any SVE context will be discarded.
  1461. */
  1462. void fpsimd_update_current_state(struct user_fpsimd_state const *state)
  1463. {
  1464. if (WARN_ON(!system_supports_fpsimd()))
  1465. return;
  1466. get_cpu_fpsimd_context();
  1467. current->thread.uw.fpsimd_state = *state;
  1468. if (test_thread_flag(TIF_SVE))
  1469. fpsimd_to_sve(current);
  1470. task_fpsimd_load();
  1471. fpsimd_bind_task_to_cpu();
  1472. clear_thread_flag(TIF_FOREIGN_FPSTATE);
  1473. put_cpu_fpsimd_context();
  1474. }
  1475. /*
  1476. * Invalidate live CPU copies of task t's FPSIMD state
  1477. *
  1478. * This function may be called with preemption enabled. The barrier()
  1479. * ensures that the assignment to fpsimd_cpu is visible to any
  1480. * preemption/softirq that could race with set_tsk_thread_flag(), so
  1481. * that TIF_FOREIGN_FPSTATE cannot be spuriously re-cleared.
  1482. *
  1483. * The final barrier ensures that TIF_FOREIGN_FPSTATE is seen set by any
  1484. * subsequent code.
  1485. */
  1486. void fpsimd_flush_task_state(struct task_struct *t)
  1487. {
  1488. t->thread.fpsimd_cpu = NR_CPUS;
  1489. /*
  1490. * If we don't support fpsimd, bail out after we have
  1491. * reset the fpsimd_cpu for this task and clear the
  1492. * FPSTATE.
  1493. */
  1494. if (!system_supports_fpsimd())
  1495. return;
  1496. barrier();
  1497. set_tsk_thread_flag(t, TIF_FOREIGN_FPSTATE);
  1498. barrier();
  1499. }
  1500. /*
  1501. * Invalidate any task's FPSIMD state that is present on this cpu.
  1502. * The FPSIMD context should be acquired with get_cpu_fpsimd_context()
  1503. * before calling this function.
  1504. */
  1505. static void fpsimd_flush_cpu_state(void)
  1506. {
  1507. WARN_ON(!system_supports_fpsimd());
  1508. __this_cpu_write(fpsimd_last_state.st, NULL);
  1509. /*
  1510. * Leaving streaming mode enabled will cause issues for any kernel
  1511. * NEON and leaving streaming mode or ZA enabled may increase power
  1512. * consumption.
  1513. */
  1514. if (system_supports_sme())
  1515. sme_smstop();
  1516. set_thread_flag(TIF_FOREIGN_FPSTATE);
  1517. }
  1518. /*
  1519. * Save the FPSIMD state to memory and invalidate cpu view.
  1520. * This function must be called with preemption disabled.
  1521. */
  1522. void fpsimd_save_and_flush_cpu_state(void)
  1523. {
  1524. if (!system_supports_fpsimd())
  1525. return;
  1526. WARN_ON(preemptible());
  1527. __get_cpu_fpsimd_context();
  1528. fpsimd_save();
  1529. fpsimd_flush_cpu_state();
  1530. __put_cpu_fpsimd_context();
  1531. }
  1532. #ifdef CONFIG_KERNEL_MODE_NEON
  1533. /*
  1534. * Kernel-side NEON support functions
  1535. */
  1536. /*
  1537. * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
  1538. * context
  1539. *
  1540. * Must not be called unless may_use_simd() returns true.
  1541. * Task context in the FPSIMD registers is saved back to memory as necessary.
  1542. *
  1543. * A matching call to kernel_neon_end() must be made before returning from the
  1544. * calling context.
  1545. *
  1546. * The caller may freely use the FPSIMD registers until kernel_neon_end() is
  1547. * called.
  1548. */
  1549. void kernel_neon_begin(void)
  1550. {
  1551. if (WARN_ON(!system_supports_fpsimd()))
  1552. return;
  1553. BUG_ON(!may_use_simd());
  1554. get_cpu_fpsimd_context();
  1555. /* Save unsaved fpsimd state, if any: */
  1556. fpsimd_save();
  1557. /* Invalidate any task state remaining in the fpsimd regs: */
  1558. fpsimd_flush_cpu_state();
  1559. }
  1560. EXPORT_SYMBOL(kernel_neon_begin);
  1561. /*
  1562. * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
  1563. *
  1564. * Must be called from a context in which kernel_neon_begin() was previously
  1565. * called, with no call to kernel_neon_end() in the meantime.
  1566. *
  1567. * The caller must not use the FPSIMD registers after this function is called,
  1568. * unless kernel_neon_begin() is called again in the meantime.
  1569. */
  1570. void kernel_neon_end(void)
  1571. {
  1572. if (!system_supports_fpsimd())
  1573. return;
  1574. put_cpu_fpsimd_context();
  1575. }
  1576. EXPORT_SYMBOL(kernel_neon_end);
  1577. #ifdef CONFIG_EFI
  1578. static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
  1579. static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
  1580. static DEFINE_PER_CPU(bool, efi_sve_state_used);
  1581. static DEFINE_PER_CPU(bool, efi_sm_state);
  1582. /*
  1583. * EFI runtime services support functions
  1584. *
  1585. * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
  1586. * This means that for EFI (and only for EFI), we have to assume that FPSIMD
  1587. * is always used rather than being an optional accelerator.
  1588. *
  1589. * These functions provide the necessary support for ensuring FPSIMD
  1590. * save/restore in the contexts from which EFI is used.
  1591. *
  1592. * Do not use them for any other purpose -- if tempted to do so, you are
  1593. * either doing something wrong or you need to propose some refactoring.
  1594. */
  1595. /*
  1596. * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
  1597. */
  1598. void __efi_fpsimd_begin(void)
  1599. {
  1600. if (!system_supports_fpsimd())
  1601. return;
  1602. WARN_ON(preemptible());
  1603. if (may_use_simd()) {
  1604. kernel_neon_begin();
  1605. } else {
  1606. /*
  1607. * If !efi_sve_state, SVE can't be in use yet and doesn't need
  1608. * preserving:
  1609. */
  1610. if (system_supports_sve() && likely(efi_sve_state)) {
  1611. char *sve_state = this_cpu_ptr(efi_sve_state);
  1612. bool ffr = true;
  1613. u64 svcr;
  1614. __this_cpu_write(efi_sve_state_used, true);
  1615. if (system_supports_sme()) {
  1616. svcr = read_sysreg_s(SYS_SVCR);
  1617. __this_cpu_write(efi_sm_state,
  1618. svcr & SVCR_SM_MASK);
  1619. /*
  1620. * Unless we have FA64 FFR does not
  1621. * exist in streaming mode.
  1622. */
  1623. if (!system_supports_fa64())
  1624. ffr = !(svcr & SVCR_SM_MASK);
  1625. }
  1626. sve_save_state(sve_state + sve_ffr_offset(sve_max_vl()),
  1627. &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
  1628. ffr);
  1629. if (system_supports_sme())
  1630. sysreg_clear_set_s(SYS_SVCR,
  1631. SVCR_SM_MASK, 0);
  1632. } else {
  1633. fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
  1634. }
  1635. __this_cpu_write(efi_fpsimd_state_used, true);
  1636. }
  1637. }
  1638. /*
  1639. * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
  1640. */
  1641. void __efi_fpsimd_end(void)
  1642. {
  1643. if (!system_supports_fpsimd())
  1644. return;
  1645. if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
  1646. kernel_neon_end();
  1647. } else {
  1648. if (system_supports_sve() &&
  1649. likely(__this_cpu_read(efi_sve_state_used))) {
  1650. char const *sve_state = this_cpu_ptr(efi_sve_state);
  1651. bool ffr = true;
  1652. /*
  1653. * Restore streaming mode; EFI calls are
  1654. * normal function calls so should not return in
  1655. * streaming mode.
  1656. */
  1657. if (system_supports_sme()) {
  1658. if (__this_cpu_read(efi_sm_state)) {
  1659. sysreg_clear_set_s(SYS_SVCR,
  1660. 0,
  1661. SVCR_SM_MASK);
  1662. /*
  1663. * Unless we have FA64 FFR does not
  1664. * exist in streaming mode.
  1665. */
  1666. if (!system_supports_fa64())
  1667. ffr = false;
  1668. }
  1669. }
  1670. sve_load_state(sve_state + sve_ffr_offset(sve_max_vl()),
  1671. &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
  1672. ffr);
  1673. __this_cpu_write(efi_sve_state_used, false);
  1674. } else {
  1675. fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
  1676. }
  1677. }
  1678. }
  1679. #endif /* CONFIG_EFI */
  1680. #endif /* CONFIG_KERNEL_MODE_NEON */
  1681. #ifdef CONFIG_CPU_PM
  1682. static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
  1683. unsigned long cmd, void *v)
  1684. {
  1685. switch (cmd) {
  1686. case CPU_PM_ENTER:
  1687. fpsimd_save_and_flush_cpu_state();
  1688. break;
  1689. case CPU_PM_EXIT:
  1690. break;
  1691. case CPU_PM_ENTER_FAILED:
  1692. default:
  1693. return NOTIFY_DONE;
  1694. }
  1695. return NOTIFY_OK;
  1696. }
  1697. static struct notifier_block fpsimd_cpu_pm_notifier_block = {
  1698. .notifier_call = fpsimd_cpu_pm_notifier,
  1699. };
  1700. static void __init fpsimd_pm_init(void)
  1701. {
  1702. cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
  1703. }
  1704. #else
  1705. static inline void fpsimd_pm_init(void) { }
  1706. #endif /* CONFIG_CPU_PM */
  1707. #ifdef CONFIG_HOTPLUG_CPU
  1708. static int fpsimd_cpu_dead(unsigned int cpu)
  1709. {
  1710. per_cpu(fpsimd_last_state.st, cpu) = NULL;
  1711. return 0;
  1712. }
  1713. static inline void fpsimd_hotplug_init(void)
  1714. {
  1715. cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
  1716. NULL, fpsimd_cpu_dead);
  1717. }
  1718. #else
  1719. static inline void fpsimd_hotplug_init(void) { }
  1720. #endif
  1721. /*
  1722. * FP/SIMD support code initialisation.
  1723. */
  1724. static int __init fpsimd_init(void)
  1725. {
  1726. if (cpu_have_named_feature(FP)) {
  1727. fpsimd_pm_init();
  1728. fpsimd_hotplug_init();
  1729. } else {
  1730. pr_notice("Floating-point is not implemented\n");
  1731. }
  1732. if (!cpu_have_named_feature(ASIMD))
  1733. pr_notice("Advanced SIMD is not implemented\n");
  1734. if (cpu_have_named_feature(SME) && !cpu_have_named_feature(SVE))
  1735. pr_notice("SME is implemented but not SVE\n");
  1736. sve_sysctl_init();
  1737. sme_sysctl_init();
  1738. return 0;
  1739. }
  1740. core_initcall(fpsimd_init);