fpsimd.c 34 KB

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