fpsimd.c 59 KB

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