fpsimd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /*
  2. * FP/SIMD context switching and fault handling
  3. *
  4. * Copyright (C) 2012 ARM Ltd.
  5. * Author: Catalin Marinas <catalin.marinas@arm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/bitmap.h>
  20. #include <linux/bottom_half.h>
  21. #include <linux/bug.h>
  22. #include <linux/cache.h>
  23. #include <linux/compat.h>
  24. #include <linux/cpu.h>
  25. #include <linux/cpu_pm.h>
  26. #include <linux/kernel.h>
  27. #include <linux/linkage.h>
  28. #include <linux/irqflags.h>
  29. #include <linux/init.h>
  30. #include <linux/percpu.h>
  31. #include <linux/prctl.h>
  32. #include <linux/preempt.h>
  33. #include <linux/ptrace.h>
  34. #include <linux/sched/signal.h>
  35. #include <linux/sched/task_stack.h>
  36. #include <linux/signal.h>
  37. #include <linux/slab.h>
  38. #include <linux/stddef.h>
  39. #include <linux/sysctl.h>
  40. #include <asm/esr.h>
  41. #include <asm/fpsimd.h>
  42. #include <asm/cpufeature.h>
  43. #include <asm/cputype.h>
  44. #include <asm/processor.h>
  45. #include <asm/simd.h>
  46. #include <asm/sigcontext.h>
  47. #include <asm/sysreg.h>
  48. #include <asm/traps.h>
  49. #define FPEXC_IOF (1 << 0)
  50. #define FPEXC_DZF (1 << 1)
  51. #define FPEXC_OFF (1 << 2)
  52. #define FPEXC_UFF (1 << 3)
  53. #define FPEXC_IXF (1 << 4)
  54. #define FPEXC_IDF (1 << 7)
  55. /*
  56. * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
  57. *
  58. * In order to reduce the number of times the FPSIMD state is needlessly saved
  59. * and restored, we need to keep track of two things:
  60. * (a) for each task, we need to remember which CPU was the last one to have
  61. * the task's FPSIMD state loaded into its FPSIMD registers;
  62. * (b) for each CPU, we need to remember which task's userland FPSIMD state has
  63. * been loaded into its FPSIMD registers most recently, or whether it has
  64. * been used to perform kernel mode NEON in the meantime.
  65. *
  66. * For (a), we add a fpsimd_cpu field to thread_struct, which gets updated to
  67. * the id of the current CPU every time the state is loaded onto a CPU. For (b),
  68. * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the
  69. * address of the userland FPSIMD state of the task that was loaded onto the CPU
  70. * the most recently, or NULL if kernel mode NEON has been performed after that.
  71. *
  72. * With this in place, we no longer have to restore the next FPSIMD state right
  73. * when switching between tasks. Instead, we can defer this check to userland
  74. * resume, at which time we verify whether the CPU's fpsimd_last_state and the
  75. * task's fpsimd_cpu are still mutually in sync. If this is the case, we
  76. * can omit the FPSIMD restore.
  77. *
  78. * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to
  79. * indicate whether or not the userland FPSIMD state of the current task is
  80. * present in the registers. The flag is set unless the FPSIMD registers of this
  81. * CPU currently contain the most recent userland FPSIMD state of the current
  82. * task.
  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 local_bh_disable() unless softirqs are already masked.
  90. *
  91. * For a certain task, the sequence may look something like this:
  92. * - the task gets scheduled in; if both the task's fpsimd_cpu field
  93. * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu
  94. * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is
  95. * cleared, otherwise it is set;
  96. *
  97. * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's
  98. * userland FPSIMD state is copied from memory to the registers, the task's
  99. * fpsimd_cpu field is set to the id of the current CPU, the current
  100. * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the
  101. * TIF_FOREIGN_FPSTATE flag is cleared;
  102. *
  103. * - the task executes an ordinary syscall; upon return to userland, the
  104. * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is
  105. * restored;
  106. *
  107. * - the task executes a syscall which executes some NEON instructions; this is
  108. * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD
  109. * register contents to memory, clears the fpsimd_last_state per-cpu variable
  110. * and sets the TIF_FOREIGN_FPSTATE flag;
  111. *
  112. * - the task gets preempted after kernel_neon_end() is called; as we have not
  113. * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so
  114. * whatever is in the FPSIMD registers is not saved to memory, but discarded.
  115. */
  116. struct fpsimd_last_state_struct {
  117. struct user_fpsimd_state *st;
  118. };
  119. static DEFINE_PER_CPU(struct fpsimd_last_state_struct, fpsimd_last_state);
  120. /* Default VL for tasks that don't set it explicitly: */
  121. static int sve_default_vl = -1;
  122. #ifdef CONFIG_ARM64_SVE
  123. /* Maximum supported vector length across all CPUs (initially poisoned) */
  124. int __ro_after_init sve_max_vl = SVE_VL_MIN;
  125. /* Set of available vector lengths, as vq_to_bit(vq): */
  126. static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
  127. static void __percpu *efi_sve_state;
  128. #else /* ! CONFIG_ARM64_SVE */
  129. /* Dummy declaration for code that will be optimised out: */
  130. extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
  131. extern void __percpu *efi_sve_state;
  132. #endif /* ! CONFIG_ARM64_SVE */
  133. /*
  134. * Call __sve_free() directly only if you know task can't be scheduled
  135. * or preempted.
  136. */
  137. static void __sve_free(struct task_struct *task)
  138. {
  139. kfree(task->thread.sve_state);
  140. task->thread.sve_state = NULL;
  141. }
  142. static void sve_free(struct task_struct *task)
  143. {
  144. WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
  145. __sve_free(task);
  146. }
  147. /*
  148. * TIF_SVE controls whether a task can use SVE without trapping while
  149. * in userspace, and also the way a task's FPSIMD/SVE state is stored
  150. * in thread_struct.
  151. *
  152. * The kernel uses this flag to track whether a user task is actively
  153. * using SVE, and therefore whether full SVE register state needs to
  154. * be tracked. If not, the cheaper FPSIMD context handling code can
  155. * be used instead of the more costly SVE equivalents.
  156. *
  157. * * TIF_SVE set:
  158. *
  159. * The task can execute SVE instructions while in userspace without
  160. * trapping to the kernel.
  161. *
  162. * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
  163. * corresponding Zn), P0-P15 and FFR are encoded in in
  164. * task->thread.sve_state, formatted appropriately for vector
  165. * length task->thread.sve_vl.
  166. *
  167. * task->thread.sve_state must point to a valid buffer at least
  168. * sve_state_size(task) bytes in size.
  169. *
  170. * During any syscall, the kernel may optionally clear TIF_SVE and
  171. * discard the vector state except for the FPSIMD subset.
  172. *
  173. * * TIF_SVE clear:
  174. *
  175. * An attempt by the user task to execute an SVE instruction causes
  176. * do_sve_acc() to be called, which does some preparation and then
  177. * sets TIF_SVE.
  178. *
  179. * When stored, FPSIMD registers V0-V31 are encoded in
  180. * task->thread.uw.fpsimd_state; bits [max : 128] for each of Z0-Z31 are
  181. * logically zero but not stored anywhere; P0-P15 and FFR are not
  182. * stored and have unspecified values from userspace's point of
  183. * view. For hygiene purposes, the kernel zeroes them on next use,
  184. * but userspace is discouraged from relying on this.
  185. *
  186. * task->thread.sve_state does not need to be non-NULL, valid or any
  187. * particular size: it must not be dereferenced.
  188. *
  189. * * FPSR and FPCR are always stored in task->thread.uw.fpsimd_state
  190. * irrespective of whether TIF_SVE is clear or set, since these are
  191. * not vector length dependent.
  192. */
  193. /*
  194. * Update current's FPSIMD/SVE registers from thread_struct.
  195. *
  196. * This function should be called only when the FPSIMD/SVE state in
  197. * thread_struct is known to be up to date, when preparing to enter
  198. * userspace.
  199. *
  200. * Softirqs (and preemption) must be disabled.
  201. */
  202. static void task_fpsimd_load(void)
  203. {
  204. WARN_ON(!in_softirq() && !irqs_disabled());
  205. WARN_ON(!system_supports_fpsimd());
  206. if (system_supports_sve() && test_thread_flag(TIF_SVE))
  207. sve_load_state(sve_pffr(&current->thread),
  208. &current->thread.uw.fpsimd_state.fpsr,
  209. sve_vq_from_vl(current->thread.sve_vl) - 1);
  210. else
  211. fpsimd_load_state(&current->thread.uw.fpsimd_state);
  212. }
  213. /*
  214. * Ensure FPSIMD/SVE storage in memory for the loaded context is up to
  215. * date with respect to the CPU registers.
  216. *
  217. * Softirqs (and preemption) must be disabled.
  218. */
  219. void fpsimd_save(void)
  220. {
  221. struct user_fpsimd_state *st = __this_cpu_read(fpsimd_last_state.st);
  222. /* set by fpsimd_bind_task_to_cpu() or fpsimd_bind_state_to_cpu() */
  223. WARN_ON(!system_supports_fpsimd());
  224. WARN_ON(!in_softirq() && !irqs_disabled());
  225. if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
  226. if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
  227. if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
  228. /*
  229. * Can't save the user regs, so current would
  230. * re-enter user with corrupt state.
  231. * There's no way to recover, so kill it:
  232. */
  233. force_signal_inject(SIGKILL, SI_KERNEL, 0);
  234. return;
  235. }
  236. sve_save_state(sve_pffr(&current->thread), &st->fpsr);
  237. } else
  238. fpsimd_save_state(st);
  239. }
  240. }
  241. /*
  242. * Helpers to translate bit indices in sve_vq_map to VQ values (and
  243. * vice versa). This allows find_next_bit() to be used to find the
  244. * _maximum_ VQ not exceeding a certain value.
  245. */
  246. static unsigned int vq_to_bit(unsigned int vq)
  247. {
  248. return SVE_VQ_MAX - vq;
  249. }
  250. static unsigned int bit_to_vq(unsigned int bit)
  251. {
  252. if (WARN_ON(bit >= SVE_VQ_MAX))
  253. bit = SVE_VQ_MAX - 1;
  254. return SVE_VQ_MAX - bit;
  255. }
  256. /*
  257. * All vector length selection from userspace comes through here.
  258. * We're on a slow path, so some sanity-checks are included.
  259. * If things go wrong there's a bug somewhere, but try to fall back to a
  260. * safe choice.
  261. */
  262. static unsigned int find_supported_vector_length(unsigned int vl)
  263. {
  264. int bit;
  265. int max_vl = sve_max_vl;
  266. if (WARN_ON(!sve_vl_valid(vl)))
  267. vl = SVE_VL_MIN;
  268. if (WARN_ON(!sve_vl_valid(max_vl)))
  269. max_vl = SVE_VL_MIN;
  270. if (vl > max_vl)
  271. vl = max_vl;
  272. bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
  273. vq_to_bit(sve_vq_from_vl(vl)));
  274. return sve_vl_from_vq(bit_to_vq(bit));
  275. }
  276. #if defined(CONFIG_ARM64_SVE) && defined(CONFIG_SYSCTL)
  277. static int sve_proc_do_default_vl(struct ctl_table *table, int write,
  278. void __user *buffer, size_t *lenp,
  279. loff_t *ppos)
  280. {
  281. int ret;
  282. int vl = sve_default_vl;
  283. struct ctl_table tmp_table = {
  284. .data = &vl,
  285. .maxlen = sizeof(vl),
  286. };
  287. ret = proc_dointvec(&tmp_table, write, buffer, lenp, ppos);
  288. if (ret || !write)
  289. return ret;
  290. /* Writing -1 has the special meaning "set to max": */
  291. if (vl == -1)
  292. vl = sve_max_vl;
  293. if (!sve_vl_valid(vl))
  294. return -EINVAL;
  295. sve_default_vl = find_supported_vector_length(vl);
  296. return 0;
  297. }
  298. static struct ctl_table sve_default_vl_table[] = {
  299. {
  300. .procname = "sve_default_vector_length",
  301. .mode = 0644,
  302. .proc_handler = sve_proc_do_default_vl,
  303. },
  304. { }
  305. };
  306. static int __init sve_sysctl_init(void)
  307. {
  308. if (system_supports_sve())
  309. if (!register_sysctl("abi", sve_default_vl_table))
  310. return -EINVAL;
  311. return 0;
  312. }
  313. #else /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
  314. static int __init sve_sysctl_init(void) { return 0; }
  315. #endif /* ! (CONFIG_ARM64_SVE && CONFIG_SYSCTL) */
  316. #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
  317. (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
  318. /*
  319. * Transfer the FPSIMD state in task->thread.uw.fpsimd_state to
  320. * task->thread.sve_state.
  321. *
  322. * Task can be a non-runnable task, or current. In the latter case,
  323. * softirqs (and preemption) must be disabled.
  324. * task->thread.sve_state must point to at least sve_state_size(task)
  325. * bytes of allocated kernel memory.
  326. * task->thread.uw.fpsimd_state must be up to date before calling this
  327. * function.
  328. */
  329. static void fpsimd_to_sve(struct task_struct *task)
  330. {
  331. unsigned int vq;
  332. void *sst = task->thread.sve_state;
  333. struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
  334. unsigned int i;
  335. if (!system_supports_sve())
  336. return;
  337. vq = sve_vq_from_vl(task->thread.sve_vl);
  338. for (i = 0; i < 32; ++i)
  339. memcpy(ZREG(sst, vq, i), &fst->vregs[i],
  340. sizeof(fst->vregs[i]));
  341. }
  342. /*
  343. * Transfer the SVE state in task->thread.sve_state to
  344. * task->thread.uw.fpsimd_state.
  345. *
  346. * Task can be a non-runnable task, or current. In the latter case,
  347. * softirqs (and preemption) must be disabled.
  348. * task->thread.sve_state must point to at least sve_state_size(task)
  349. * bytes of allocated kernel memory.
  350. * task->thread.sve_state must be up to date before calling this function.
  351. */
  352. static void sve_to_fpsimd(struct task_struct *task)
  353. {
  354. unsigned int vq;
  355. void const *sst = task->thread.sve_state;
  356. struct user_fpsimd_state *fst = &task->thread.uw.fpsimd_state;
  357. unsigned int i;
  358. if (!system_supports_sve())
  359. return;
  360. vq = sve_vq_from_vl(task->thread.sve_vl);
  361. for (i = 0; i < 32; ++i)
  362. memcpy(&fst->vregs[i], ZREG(sst, vq, i),
  363. sizeof(fst->vregs[i]));
  364. }
  365. #ifdef CONFIG_ARM64_SVE
  366. /*
  367. * Return how many bytes of memory are required to store the full SVE
  368. * state for task, given task's currently configured vector length.
  369. */
  370. size_t sve_state_size(struct task_struct const *task)
  371. {
  372. return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
  373. }
  374. /*
  375. * Ensure that task->thread.sve_state is allocated and sufficiently large.
  376. *
  377. * This function should be used only in preparation for replacing
  378. * task->thread.sve_state with new data. The memory is always zeroed
  379. * here to prevent stale data from showing through: this is done in
  380. * the interest of testability and predictability: except in the
  381. * do_sve_acc() case, there is no ABI requirement to hide stale data
  382. * written previously be task.
  383. */
  384. void sve_alloc(struct task_struct *task)
  385. {
  386. if (task->thread.sve_state) {
  387. memset(task->thread.sve_state, 0, sve_state_size(current));
  388. return;
  389. }
  390. /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
  391. task->thread.sve_state =
  392. kzalloc(sve_state_size(task), GFP_KERNEL);
  393. /*
  394. * If future SVE revisions can have larger vectors though,
  395. * this may cease to be true:
  396. */
  397. BUG_ON(!task->thread.sve_state);
  398. }
  399. /*
  400. * Ensure that task->thread.sve_state is up to date with respect to
  401. * the user task, irrespective of when SVE is in use or not.
  402. *
  403. * This should only be called by ptrace. task must be non-runnable.
  404. * task->thread.sve_state must point to at least sve_state_size(task)
  405. * bytes of allocated kernel memory.
  406. */
  407. void fpsimd_sync_to_sve(struct task_struct *task)
  408. {
  409. if (!test_tsk_thread_flag(task, TIF_SVE))
  410. fpsimd_to_sve(task);
  411. }
  412. /*
  413. * Ensure that task->thread.uw.fpsimd_state is up to date with respect to
  414. * the user task, irrespective of whether SVE is in use or not.
  415. *
  416. * This should only be called by ptrace. task must be non-runnable.
  417. * task->thread.sve_state must point to at least sve_state_size(task)
  418. * bytes of allocated kernel memory.
  419. */
  420. void sve_sync_to_fpsimd(struct task_struct *task)
  421. {
  422. if (test_tsk_thread_flag(task, TIF_SVE))
  423. sve_to_fpsimd(task);
  424. }
  425. /*
  426. * Ensure that task->thread.sve_state is up to date with respect to
  427. * the task->thread.uw.fpsimd_state.
  428. *
  429. * This should only be called by ptrace to merge new FPSIMD register
  430. * values into a task for which SVE is currently active.
  431. * task must be non-runnable.
  432. * task->thread.sve_state must point to at least sve_state_size(task)
  433. * bytes of allocated kernel memory.
  434. * task->thread.uw.fpsimd_state must already have been initialised with
  435. * the new FPSIMD register values to be merged in.
  436. */
  437. void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
  438. {
  439. unsigned int vq;
  440. void *sst = task->thread.sve_state;
  441. struct user_fpsimd_state const *fst = &task->thread.uw.fpsimd_state;
  442. unsigned int i;
  443. if (!test_tsk_thread_flag(task, TIF_SVE))
  444. return;
  445. vq = sve_vq_from_vl(task->thread.sve_vl);
  446. memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
  447. for (i = 0; i < 32; ++i)
  448. memcpy(ZREG(sst, vq, i), &fst->vregs[i],
  449. sizeof(fst->vregs[i]));
  450. }
  451. int sve_set_vector_length(struct task_struct *task,
  452. unsigned long vl, unsigned long flags)
  453. {
  454. if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
  455. PR_SVE_SET_VL_ONEXEC))
  456. return -EINVAL;
  457. if (!sve_vl_valid(vl))
  458. return -EINVAL;
  459. /*
  460. * Clamp to the maximum vector length that VL-agnostic SVE code can
  461. * work with. A flag may be assigned in the future to allow setting
  462. * of larger vector lengths without confusing older software.
  463. */
  464. if (vl > SVE_VL_ARCH_MAX)
  465. vl = SVE_VL_ARCH_MAX;
  466. vl = find_supported_vector_length(vl);
  467. if (flags & (PR_SVE_VL_INHERIT |
  468. PR_SVE_SET_VL_ONEXEC))
  469. task->thread.sve_vl_onexec = vl;
  470. else
  471. /* Reset VL to system default on next exec: */
  472. task->thread.sve_vl_onexec = 0;
  473. /* Only actually set the VL if not deferred: */
  474. if (flags & PR_SVE_SET_VL_ONEXEC)
  475. goto out;
  476. if (vl == task->thread.sve_vl)
  477. goto out;
  478. /*
  479. * To ensure the FPSIMD bits of the SVE vector registers are preserved,
  480. * write any live register state back to task_struct, and convert to a
  481. * non-SVE thread.
  482. */
  483. if (task == current) {
  484. local_bh_disable();
  485. fpsimd_save();
  486. set_thread_flag(TIF_FOREIGN_FPSTATE);
  487. }
  488. fpsimd_flush_task_state(task);
  489. if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
  490. sve_to_fpsimd(task);
  491. if (task == current)
  492. local_bh_enable();
  493. /*
  494. * Force reallocation of task SVE state to the correct size
  495. * on next use:
  496. */
  497. sve_free(task);
  498. task->thread.sve_vl = vl;
  499. out:
  500. update_tsk_thread_flag(task, TIF_SVE_VL_INHERIT,
  501. flags & PR_SVE_VL_INHERIT);
  502. return 0;
  503. }
  504. /*
  505. * Encode the current vector length and flags for return.
  506. * This is only required for prctl(): ptrace has separate fields
  507. *
  508. * flags are as for sve_set_vector_length().
  509. */
  510. static int sve_prctl_status(unsigned long flags)
  511. {
  512. int ret;
  513. if (flags & PR_SVE_SET_VL_ONEXEC)
  514. ret = current->thread.sve_vl_onexec;
  515. else
  516. ret = current->thread.sve_vl;
  517. if (test_thread_flag(TIF_SVE_VL_INHERIT))
  518. ret |= PR_SVE_VL_INHERIT;
  519. return ret;
  520. }
  521. /* PR_SVE_SET_VL */
  522. int sve_set_current_vl(unsigned long arg)
  523. {
  524. unsigned long vl, flags;
  525. int ret;
  526. vl = arg & PR_SVE_VL_LEN_MASK;
  527. flags = arg & ~vl;
  528. if (!system_supports_sve())
  529. return -EINVAL;
  530. ret = sve_set_vector_length(current, vl, flags);
  531. if (ret)
  532. return ret;
  533. return sve_prctl_status(flags);
  534. }
  535. /* PR_SVE_GET_VL */
  536. int sve_get_current_vl(void)
  537. {
  538. if (!system_supports_sve())
  539. return -EINVAL;
  540. return sve_prctl_status(0);
  541. }
  542. /*
  543. * Bitmap for temporary storage of the per-CPU set of supported vector lengths
  544. * during secondary boot.
  545. */
  546. static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX);
  547. static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
  548. {
  549. unsigned int vq, vl;
  550. unsigned long zcr;
  551. bitmap_zero(map, SVE_VQ_MAX);
  552. zcr = ZCR_ELx_LEN_MASK;
  553. zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
  554. for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
  555. write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
  556. vl = sve_get_vl();
  557. vq = sve_vq_from_vl(vl); /* skip intervening lengths */
  558. set_bit(vq_to_bit(vq), map);
  559. }
  560. }
  561. void __init sve_init_vq_map(void)
  562. {
  563. sve_probe_vqs(sve_vq_map);
  564. }
  565. /*
  566. * If we haven't committed to the set of supported VQs yet, filter out
  567. * those not supported by the current CPU.
  568. */
  569. void sve_update_vq_map(void)
  570. {
  571. sve_probe_vqs(sve_secondary_vq_map);
  572. bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX);
  573. }
  574. /* Check whether the current CPU supports all VQs in the committed set */
  575. int sve_verify_vq_map(void)
  576. {
  577. int ret = 0;
  578. sve_probe_vqs(sve_secondary_vq_map);
  579. bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map,
  580. SVE_VQ_MAX);
  581. if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) {
  582. pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
  583. smp_processor_id());
  584. ret = -EINVAL;
  585. }
  586. return ret;
  587. }
  588. static void __init sve_efi_setup(void)
  589. {
  590. if (!IS_ENABLED(CONFIG_EFI))
  591. return;
  592. /*
  593. * alloc_percpu() warns and prints a backtrace if this goes wrong.
  594. * This is evidence of a crippled system and we are returning void,
  595. * so no attempt is made to handle this situation here.
  596. */
  597. if (!sve_vl_valid(sve_max_vl))
  598. goto fail;
  599. efi_sve_state = __alloc_percpu(
  600. SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
  601. if (!efi_sve_state)
  602. goto fail;
  603. return;
  604. fail:
  605. panic("Cannot allocate percpu memory for EFI SVE save/restore");
  606. }
  607. /*
  608. * Enable SVE for EL1.
  609. * Intended for use by the cpufeatures code during CPU boot.
  610. */
  611. void sve_kernel_enable(const struct arm64_cpu_capabilities *__always_unused p)
  612. {
  613. write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
  614. isb();
  615. }
  616. /*
  617. * Read the pseudo-ZCR used by cpufeatures to identify the supported SVE
  618. * vector length.
  619. *
  620. * Use only if SVE is present.
  621. * This function clobbers the SVE vector length.
  622. */
  623. u64 read_zcr_features(void)
  624. {
  625. u64 zcr;
  626. unsigned int vq_max;
  627. /*
  628. * Set the maximum possible VL, and write zeroes to all other
  629. * bits to see if they stick.
  630. */
  631. sve_kernel_enable(NULL);
  632. write_sysreg_s(ZCR_ELx_LEN_MASK, SYS_ZCR_EL1);
  633. zcr = read_sysreg_s(SYS_ZCR_EL1);
  634. zcr &= ~(u64)ZCR_ELx_LEN_MASK; /* find sticky 1s outside LEN field */
  635. vq_max = sve_vq_from_vl(sve_get_vl());
  636. zcr |= vq_max - 1; /* set LEN field to maximum effective value */
  637. return zcr;
  638. }
  639. void __init sve_setup(void)
  640. {
  641. u64 zcr;
  642. if (!system_supports_sve())
  643. return;
  644. /*
  645. * The SVE architecture mandates support for 128-bit vectors,
  646. * so sve_vq_map must have at least SVE_VQ_MIN set.
  647. * If something went wrong, at least try to patch it up:
  648. */
  649. if (WARN_ON(!test_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map)))
  650. set_bit(vq_to_bit(SVE_VQ_MIN), sve_vq_map);
  651. zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
  652. sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
  653. /*
  654. * Sanity-check that the max VL we determined through CPU features
  655. * corresponds properly to sve_vq_map. If not, do our best:
  656. */
  657. if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
  658. sve_max_vl = find_supported_vector_length(sve_max_vl);
  659. /*
  660. * For the default VL, pick the maximum supported value <= 64.
  661. * VL == 64 is guaranteed not to grow the signal frame.
  662. */
  663. sve_default_vl = find_supported_vector_length(64);
  664. pr_info("SVE: maximum available vector length %u bytes per vector\n",
  665. sve_max_vl);
  666. pr_info("SVE: default vector length %u bytes per vector\n",
  667. sve_default_vl);
  668. sve_efi_setup();
  669. }
  670. /*
  671. * Called from the put_task_struct() path, which cannot get here
  672. * unless dead_task is really dead and not schedulable.
  673. */
  674. void fpsimd_release_task(struct task_struct *dead_task)
  675. {
  676. __sve_free(dead_task);
  677. }
  678. #endif /* CONFIG_ARM64_SVE */
  679. /*
  680. * Trapped SVE access
  681. *
  682. * Storage is allocated for the full SVE state, the current FPSIMD
  683. * register contents are migrated across, and TIF_SVE is set so that
  684. * the SVE access trap will be disabled the next time this task
  685. * reaches ret_to_user.
  686. *
  687. * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
  688. * would have disabled the SVE access trap for userspace during
  689. * ret_to_user, making an SVE access trap impossible in that case.
  690. */
  691. asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
  692. {
  693. /* Even if we chose not to use SVE, the hardware could still trap: */
  694. if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
  695. force_signal_inject(SIGILL, ILL_ILLOPC, regs->pc);
  696. return;
  697. }
  698. sve_alloc(current);
  699. local_bh_disable();
  700. fpsimd_save();
  701. fpsimd_to_sve(current);
  702. /* Force ret_to_user to reload the registers: */
  703. fpsimd_flush_task_state(current);
  704. set_thread_flag(TIF_FOREIGN_FPSTATE);
  705. if (test_and_set_thread_flag(TIF_SVE))
  706. WARN_ON(1); /* SVE access shouldn't have trapped */
  707. local_bh_enable();
  708. }
  709. /*
  710. * Trapped FP/ASIMD access.
  711. */
  712. asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
  713. {
  714. /* TODO: implement lazy context saving/restoring */
  715. WARN_ON(1);
  716. }
  717. /*
  718. * Raise a SIGFPE for the current process.
  719. */
  720. asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
  721. {
  722. siginfo_t info;
  723. unsigned int si_code = FPE_FLTUNK;
  724. if (esr & ESR_ELx_FP_EXC_TFV) {
  725. if (esr & FPEXC_IOF)
  726. si_code = FPE_FLTINV;
  727. else if (esr & FPEXC_DZF)
  728. si_code = FPE_FLTDIV;
  729. else if (esr & FPEXC_OFF)
  730. si_code = FPE_FLTOVF;
  731. else if (esr & FPEXC_UFF)
  732. si_code = FPE_FLTUND;
  733. else if (esr & FPEXC_IXF)
  734. si_code = FPE_FLTRES;
  735. }
  736. clear_siginfo(&info);
  737. info.si_signo = SIGFPE;
  738. info.si_code = si_code;
  739. info.si_addr = (void __user *)instruction_pointer(regs);
  740. send_sig_info(SIGFPE, &info, current);
  741. }
  742. void fpsimd_thread_switch(struct task_struct *next)
  743. {
  744. bool wrong_task, wrong_cpu;
  745. if (!system_supports_fpsimd())
  746. return;
  747. /* Save unsaved fpsimd state, if any: */
  748. fpsimd_save();
  749. /*
  750. * Fix up TIF_FOREIGN_FPSTATE to correctly describe next's
  751. * state. For kernel threads, FPSIMD registers are never loaded
  752. * and wrong_task and wrong_cpu will always be true.
  753. */
  754. wrong_task = __this_cpu_read(fpsimd_last_state.st) !=
  755. &next->thread.uw.fpsimd_state;
  756. wrong_cpu = next->thread.fpsimd_cpu != smp_processor_id();
  757. update_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE,
  758. wrong_task || wrong_cpu);
  759. }
  760. void fpsimd_flush_thread(void)
  761. {
  762. int vl, supported_vl;
  763. if (!system_supports_fpsimd())
  764. return;
  765. local_bh_disable();
  766. memset(&current->thread.uw.fpsimd_state, 0,
  767. sizeof(current->thread.uw.fpsimd_state));
  768. fpsimd_flush_task_state(current);
  769. if (system_supports_sve()) {
  770. clear_thread_flag(TIF_SVE);
  771. sve_free(current);
  772. /*
  773. * Reset the task vector length as required.
  774. * This is where we ensure that all user tasks have a valid
  775. * vector length configured: no kernel task can become a user
  776. * task without an exec and hence a call to this function.
  777. * By the time the first call to this function is made, all
  778. * early hardware probing is complete, so sve_default_vl
  779. * should be valid.
  780. * If a bug causes this to go wrong, we make some noise and
  781. * try to fudge thread.sve_vl to a safe value here.
  782. */
  783. vl = current->thread.sve_vl_onexec ?
  784. current->thread.sve_vl_onexec : sve_default_vl;
  785. if (WARN_ON(!sve_vl_valid(vl)))
  786. vl = SVE_VL_MIN;
  787. supported_vl = find_supported_vector_length(vl);
  788. if (WARN_ON(supported_vl != vl))
  789. vl = supported_vl;
  790. current->thread.sve_vl = vl;
  791. /*
  792. * If the task is not set to inherit, ensure that the vector
  793. * length will be reset by a subsequent exec:
  794. */
  795. if (!test_thread_flag(TIF_SVE_VL_INHERIT))
  796. current->thread.sve_vl_onexec = 0;
  797. }
  798. set_thread_flag(TIF_FOREIGN_FPSTATE);
  799. local_bh_enable();
  800. }
  801. /*
  802. * Save the userland FPSIMD state of 'current' to memory, but only if the state
  803. * currently held in the registers does in fact belong to 'current'
  804. */
  805. void fpsimd_preserve_current_state(void)
  806. {
  807. if (!system_supports_fpsimd())
  808. return;
  809. local_bh_disable();
  810. fpsimd_save();
  811. local_bh_enable();
  812. }
  813. /*
  814. * Like fpsimd_preserve_current_state(), but ensure that
  815. * current->thread.uw.fpsimd_state is updated so that it can be copied to
  816. * the signal frame.
  817. */
  818. void fpsimd_signal_preserve_current_state(void)
  819. {
  820. fpsimd_preserve_current_state();
  821. if (system_supports_sve() && test_thread_flag(TIF_SVE))
  822. sve_to_fpsimd(current);
  823. }
  824. /*
  825. * Associate current's FPSIMD context with this cpu
  826. * Preemption must be disabled when calling this function.
  827. */
  828. void fpsimd_bind_task_to_cpu(void)
  829. {
  830. struct fpsimd_last_state_struct *last =
  831. this_cpu_ptr(&fpsimd_last_state);
  832. WARN_ON(!system_supports_fpsimd());
  833. last->st = &current->thread.uw.fpsimd_state;
  834. current->thread.fpsimd_cpu = smp_processor_id();
  835. if (system_supports_sve()) {
  836. /* Toggle SVE trapping for userspace if needed */
  837. if (test_thread_flag(TIF_SVE))
  838. sve_user_enable();
  839. else
  840. sve_user_disable();
  841. /* Serialised by exception return to user */
  842. }
  843. }
  844. void fpsimd_bind_state_to_cpu(struct user_fpsimd_state *st)
  845. {
  846. struct fpsimd_last_state_struct *last =
  847. this_cpu_ptr(&fpsimd_last_state);
  848. WARN_ON(!system_supports_fpsimd());
  849. WARN_ON(!in_softirq() && !irqs_disabled());
  850. last->st = st;
  851. }
  852. /*
  853. * Load the userland FPSIMD state of 'current' from memory, but only if the
  854. * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
  855. * state of 'current'
  856. */
  857. void fpsimd_restore_current_state(void)
  858. {
  859. /*
  860. * For the tasks that were created before we detected the absence of
  861. * FP/SIMD, the TIF_FOREIGN_FPSTATE could be set via fpsimd_thread_switch(),
  862. * e.g, init. This could be then inherited by the children processes.
  863. * If we later detect that the system doesn't support FP/SIMD,
  864. * we must clear the flag for all the tasks to indicate that the
  865. * FPSTATE is clean (as we can't have one) to avoid looping for ever in
  866. * do_notify_resume().
  867. */
  868. if (!system_supports_fpsimd()) {
  869. clear_thread_flag(TIF_FOREIGN_FPSTATE);
  870. return;
  871. }
  872. local_bh_disable();
  873. if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
  874. task_fpsimd_load();
  875. fpsimd_bind_task_to_cpu();
  876. }
  877. local_bh_enable();
  878. }
  879. /*
  880. * Load an updated userland FPSIMD state for 'current' from memory and set the
  881. * flag that indicates that the FPSIMD register contents are the most recent
  882. * FPSIMD state of 'current'
  883. */
  884. void fpsimd_update_current_state(struct user_fpsimd_state const *state)
  885. {
  886. if (WARN_ON(!system_supports_fpsimd()))
  887. return;
  888. local_bh_disable();
  889. current->thread.uw.fpsimd_state = *state;
  890. if (system_supports_sve() && test_thread_flag(TIF_SVE))
  891. fpsimd_to_sve(current);
  892. task_fpsimd_load();
  893. fpsimd_bind_task_to_cpu();
  894. clear_thread_flag(TIF_FOREIGN_FPSTATE);
  895. local_bh_enable();
  896. }
  897. /*
  898. * Invalidate live CPU copies of task t's FPSIMD state
  899. */
  900. void fpsimd_flush_task_state(struct task_struct *t)
  901. {
  902. t->thread.fpsimd_cpu = NR_CPUS;
  903. }
  904. void fpsimd_flush_cpu_state(void)
  905. {
  906. WARN_ON(!system_supports_fpsimd());
  907. __this_cpu_write(fpsimd_last_state.st, NULL);
  908. set_thread_flag(TIF_FOREIGN_FPSTATE);
  909. }
  910. #ifdef CONFIG_KERNEL_MODE_NEON
  911. DEFINE_PER_CPU(bool, kernel_neon_busy);
  912. EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
  913. /*
  914. * Kernel-side NEON support functions
  915. */
  916. /*
  917. * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
  918. * context
  919. *
  920. * Must not be called unless may_use_simd() returns true.
  921. * Task context in the FPSIMD registers is saved back to memory as necessary.
  922. *
  923. * A matching call to kernel_neon_end() must be made before returning from the
  924. * calling context.
  925. *
  926. * The caller may freely use the FPSIMD registers until kernel_neon_end() is
  927. * called.
  928. */
  929. void kernel_neon_begin(void)
  930. {
  931. if (WARN_ON(!system_supports_fpsimd()))
  932. return;
  933. BUG_ON(!may_use_simd());
  934. local_bh_disable();
  935. __this_cpu_write(kernel_neon_busy, true);
  936. /* Save unsaved fpsimd state, if any: */
  937. fpsimd_save();
  938. /* Invalidate any task state remaining in the fpsimd regs: */
  939. fpsimd_flush_cpu_state();
  940. preempt_disable();
  941. local_bh_enable();
  942. }
  943. EXPORT_SYMBOL(kernel_neon_begin);
  944. /*
  945. * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
  946. *
  947. * Must be called from a context in which kernel_neon_begin() was previously
  948. * called, with no call to kernel_neon_end() in the meantime.
  949. *
  950. * The caller must not use the FPSIMD registers after this function is called,
  951. * unless kernel_neon_begin() is called again in the meantime.
  952. */
  953. void kernel_neon_end(void)
  954. {
  955. bool busy;
  956. if (!system_supports_fpsimd())
  957. return;
  958. busy = __this_cpu_xchg(kernel_neon_busy, false);
  959. WARN_ON(!busy); /* No matching kernel_neon_begin()? */
  960. preempt_enable();
  961. }
  962. EXPORT_SYMBOL(kernel_neon_end);
  963. #ifdef CONFIG_EFI
  964. static DEFINE_PER_CPU(struct user_fpsimd_state, efi_fpsimd_state);
  965. static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
  966. static DEFINE_PER_CPU(bool, efi_sve_state_used);
  967. /*
  968. * EFI runtime services support functions
  969. *
  970. * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
  971. * This means that for EFI (and only for EFI), we have to assume that FPSIMD
  972. * is always used rather than being an optional accelerator.
  973. *
  974. * These functions provide the necessary support for ensuring FPSIMD
  975. * save/restore in the contexts from which EFI is used.
  976. *
  977. * Do not use them for any other purpose -- if tempted to do so, you are
  978. * either doing something wrong or you need to propose some refactoring.
  979. */
  980. /*
  981. * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
  982. */
  983. void __efi_fpsimd_begin(void)
  984. {
  985. if (!system_supports_fpsimd())
  986. return;
  987. WARN_ON(preemptible());
  988. if (may_use_simd()) {
  989. kernel_neon_begin();
  990. } else {
  991. /*
  992. * If !efi_sve_state, SVE can't be in use yet and doesn't need
  993. * preserving:
  994. */
  995. if (system_supports_sve() && likely(efi_sve_state)) {
  996. char *sve_state = this_cpu_ptr(efi_sve_state);
  997. __this_cpu_write(efi_sve_state_used, true);
  998. sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
  999. &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
  1000. } else {
  1001. fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
  1002. }
  1003. __this_cpu_write(efi_fpsimd_state_used, true);
  1004. }
  1005. }
  1006. /*
  1007. * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
  1008. */
  1009. void __efi_fpsimd_end(void)
  1010. {
  1011. if (!system_supports_fpsimd())
  1012. return;
  1013. if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
  1014. kernel_neon_end();
  1015. } else {
  1016. if (system_supports_sve() &&
  1017. likely(__this_cpu_read(efi_sve_state_used))) {
  1018. char const *sve_state = this_cpu_ptr(efi_sve_state);
  1019. sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
  1020. &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
  1021. sve_vq_from_vl(sve_get_vl()) - 1);
  1022. __this_cpu_write(efi_sve_state_used, false);
  1023. } else {
  1024. fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
  1025. }
  1026. }
  1027. }
  1028. #endif /* CONFIG_EFI */
  1029. #endif /* CONFIG_KERNEL_MODE_NEON */
  1030. #ifdef CONFIG_CPU_PM
  1031. static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
  1032. unsigned long cmd, void *v)
  1033. {
  1034. switch (cmd) {
  1035. case CPU_PM_ENTER:
  1036. fpsimd_save();
  1037. fpsimd_flush_cpu_state();
  1038. break;
  1039. case CPU_PM_EXIT:
  1040. break;
  1041. case CPU_PM_ENTER_FAILED:
  1042. default:
  1043. return NOTIFY_DONE;
  1044. }
  1045. return NOTIFY_OK;
  1046. }
  1047. static struct notifier_block fpsimd_cpu_pm_notifier_block = {
  1048. .notifier_call = fpsimd_cpu_pm_notifier,
  1049. };
  1050. static void __init fpsimd_pm_init(void)
  1051. {
  1052. cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
  1053. }
  1054. #else
  1055. static inline void fpsimd_pm_init(void) { }
  1056. #endif /* CONFIG_CPU_PM */
  1057. #ifdef CONFIG_HOTPLUG_CPU
  1058. static int fpsimd_cpu_dead(unsigned int cpu)
  1059. {
  1060. per_cpu(fpsimd_last_state.st, cpu) = NULL;
  1061. return 0;
  1062. }
  1063. static inline void fpsimd_hotplug_init(void)
  1064. {
  1065. cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
  1066. NULL, fpsimd_cpu_dead);
  1067. }
  1068. #else
  1069. static inline void fpsimd_hotplug_init(void) { }
  1070. #endif
  1071. /*
  1072. * FP/SIMD support code initialisation.
  1073. */
  1074. static int __init fpsimd_init(void)
  1075. {
  1076. if (elf_hwcap & HWCAP_FP) {
  1077. fpsimd_pm_init();
  1078. fpsimd_hotplug_init();
  1079. } else {
  1080. pr_notice("Floating-point is not implemented\n");
  1081. }
  1082. if (!(elf_hwcap & HWCAP_ASIMD))
  1083. pr_notice("Advanced SIMD is not implemented\n");
  1084. return sve_sysctl_init();
  1085. }
  1086. core_initcall(fpsimd_init);