fpsimd.c 35 KB

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