blob: b82d44693b9dc07d80b6d7ba41f85f38f47f2ccc [file] [log] [blame]
Catalin Marinas53631b52012-03-05 11:49:32 +00001/*
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
Dave Martin7582e222017-10-31 15:51:08 +000020#include <linux/bitmap.h>
Dave Martincb84d112017-08-03 17:23:23 +010021#include <linux/bottom_half.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000022#include <linux/bug.h>
Dave Martin7582e222017-10-31 15:51:08 +000023#include <linux/cache.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000024#include <linux/compat.h>
Janet Liu32365e62015-06-11 12:02:45 +080025#include <linux/cpu.h>
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +010026#include <linux/cpu_pm.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000027#include <linux/kernel.h>
Dave Martin94ef7ec2017-10-31 15:50:54 +000028#include <linux/linkage.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000029#include <linux/irqflags.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000030#include <linux/init.h>
Dave Martincb84d112017-08-03 17:23:23 +010031#include <linux/percpu.h>
Dave Martin4328825d2017-08-03 17:23:22 +010032#include <linux/preempt.h>
Dave Martin7582e222017-10-31 15:51:08 +000033#include <linux/prctl.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000034#include <linux/ptrace.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010035#include <linux/sched/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000036#include <linux/sched/task_stack.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000037#include <linux/signal.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000038#include <linux/slab.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000039
40#include <asm/fpsimd.h>
41#include <asm/cputype.h>
Dave Martin4328825d2017-08-03 17:23:22 +010042#include <asm/simd.h>
Dave Martinbc0ee472017-10-31 15:51:05 +000043#include <asm/sigcontext.h>
44#include <asm/sysreg.h>
45#include <asm/traps.h>
Catalin Marinas53631b52012-03-05 11:49:32 +000046
47#define FPEXC_IOF (1 << 0)
48#define FPEXC_DZF (1 << 1)
49#define FPEXC_OFF (1 << 2)
50#define FPEXC_UFF (1 << 3)
51#define FPEXC_IXF (1 << 4)
52#define FPEXC_IDF (1 << 7)
53
54/*
Dave Martinbc0ee472017-10-31 15:51:05 +000055 * (Note: in this discussion, statements about FPSIMD apply equally to SVE.)
56 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020057 * 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
Adam Buchbinderef769e32016-02-24 09:52:41 -080066 * the id of the current CPU every time the state is loaded onto a CPU. For (b),
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020067 * 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 *
Dave Martincb84d112017-08-03 17:23:23 +010083 * 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 *
Ard Biesheuvel005f78c2014-05-08 11:20:23 +020090 * 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 */
115static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state);
116
Dave Martin79ab0472017-10-31 15:51:06 +0000117/* Default VL for tasks that don't set it explicitly: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000118static int sve_default_vl = -1;
Dave Martin79ab0472017-10-31 15:51:06 +0000119
Dave Martin7582e222017-10-31 15:51:08 +0000120#ifdef CONFIG_ARM64_SVE
121
122/* Maximum supported vector length across all CPUs (initially poisoned) */
123int __ro_after_init sve_max_vl = -1;
124/* Set of available vector lengths, as vq_to_bit(vq): */
Dave Martin2e0f2472017-10-31 15:51:10 +0000125static __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000126static void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000127
128#else /* ! CONFIG_ARM64_SVE */
129
130/* Dummy declaration for code that will be optimised out: */
Dave Martin2e0f2472017-10-31 15:51:10 +0000131extern __ro_after_init DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX);
Dave Martinfdfa9762017-10-31 15:51:12 +0000132extern void __percpu *efi_sve_state;
Dave Martin7582e222017-10-31 15:51:08 +0000133
134#endif /* ! CONFIG_ARM64_SVE */
135
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200136/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000137 * Call __sve_free() directly only if you know task can't be scheduled
138 * or preempted.
139 */
140static void __sve_free(struct task_struct *task)
141{
142 kfree(task->thread.sve_state);
143 task->thread.sve_state = NULL;
144}
145
146static void sve_free(struct task_struct *task)
147{
148 WARN_ON(test_tsk_thread_flag(task, TIF_SVE));
149
150 __sve_free(task);
151}
152
153
154/* Offset of FFR in the SVE register dump */
155static size_t sve_ffr_offset(int vl)
156{
157 return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
158}
159
160static void *sve_pffr(struct task_struct *task)
161{
162 return (char *)task->thread.sve_state +
163 sve_ffr_offset(task->thread.sve_vl);
164}
165
166static void change_cpacr(u64 val, u64 mask)
167{
168 u64 cpacr = read_sysreg(CPACR_EL1);
169 u64 new = (cpacr & ~mask) | val;
170
171 if (new != cpacr)
172 write_sysreg(new, CPACR_EL1);
173}
174
175static void sve_user_disable(void)
176{
177 change_cpacr(0, CPACR_EL1_ZEN_EL0EN);
178}
179
180static void sve_user_enable(void)
181{
182 change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN);
183}
184
185/*
186 * TIF_SVE controls whether a task can use SVE without trapping while
187 * in userspace, and also the way a task's FPSIMD/SVE state is stored
188 * in thread_struct.
189 *
190 * The kernel uses this flag to track whether a user task is actively
191 * using SVE, and therefore whether full SVE register state needs to
192 * be tracked. If not, the cheaper FPSIMD context handling code can
193 * be used instead of the more costly SVE equivalents.
194 *
195 * * TIF_SVE set:
196 *
197 * The task can execute SVE instructions while in userspace without
198 * trapping to the kernel.
199 *
200 * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the
201 * corresponding Zn), P0-P15 and FFR are encoded in in
202 * task->thread.sve_state, formatted appropriately for vector
203 * length task->thread.sve_vl.
204 *
205 * task->thread.sve_state must point to a valid buffer at least
206 * sve_state_size(task) bytes in size.
207 *
208 * During any syscall, the kernel may optionally clear TIF_SVE and
209 * discard the vector state except for the FPSIMD subset.
210 *
211 * * TIF_SVE clear:
212 *
213 * An attempt by the user task to execute an SVE instruction causes
214 * do_sve_acc() to be called, which does some preparation and then
215 * sets TIF_SVE.
216 *
217 * When stored, FPSIMD registers V0-V31 are encoded in
218 * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are
219 * logically zero but not stored anywhere; P0-P15 and FFR are not
220 * stored and have unspecified values from userspace's point of
221 * view. For hygiene purposes, the kernel zeroes them on next use,
222 * but userspace is discouraged from relying on this.
223 *
224 * task->thread.sve_state does not need to be non-NULL, valid or any
225 * particular size: it must not be dereferenced.
226 *
227 * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of
228 * whether TIF_SVE is clear or set, since these are not vector length
229 * dependent.
230 */
231
232/*
233 * Update current's FPSIMD/SVE registers from thread_struct.
234 *
235 * This function should be called only when the FPSIMD/SVE state in
236 * thread_struct is known to be up to date, when preparing to enter
237 * userspace.
238 *
239 * Softirqs (and preemption) must be disabled.
240 */
241static void task_fpsimd_load(void)
242{
243 WARN_ON(!in_softirq() && !irqs_disabled());
244
245 if (system_supports_sve() && test_thread_flag(TIF_SVE))
246 sve_load_state(sve_pffr(current),
247 &current->thread.fpsimd_state.fpsr,
248 sve_vq_from_vl(current->thread.sve_vl) - 1);
249 else
250 fpsimd_load_state(&current->thread.fpsimd_state);
251
252 if (system_supports_sve()) {
253 /* Toggle SVE trapping for userspace if needed */
254 if (test_thread_flag(TIF_SVE))
255 sve_user_enable();
256 else
257 sve_user_disable();
258
259 /* Serialised by exception return to user */
260 }
261}
262
263/*
264 * Ensure current's FPSIMD/SVE storage in thread_struct is up to date
265 * with respect to the CPU registers.
266 *
267 * Softirqs (and preemption) must be disabled.
268 */
269static void task_fpsimd_save(void)
270{
271 WARN_ON(!in_softirq() && !irqs_disabled());
272
273 if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) {
274 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
275 if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) {
276 /*
277 * Can't save the user regs, so current would
278 * re-enter user with corrupt state.
279 * There's no way to recover, so kill it:
280 */
281 force_signal_inject(
282 SIGKILL, 0, current_pt_regs(), 0);
283 return;
284 }
285
286 sve_save_state(sve_pffr(current),
287 &current->thread.fpsimd_state.fpsr);
288 } else
289 fpsimd_save_state(&current->thread.fpsimd_state);
290 }
291}
292
Dave Martin7582e222017-10-31 15:51:08 +0000293/*
294 * Helpers to translate bit indices in sve_vq_map to VQ values (and
295 * vice versa). This allows find_next_bit() to be used to find the
296 * _maximum_ VQ not exceeding a certain value.
297 */
298
299static unsigned int vq_to_bit(unsigned int vq)
300{
301 return SVE_VQ_MAX - vq;
302}
303
304static unsigned int bit_to_vq(unsigned int bit)
305{
306 if (WARN_ON(bit >= SVE_VQ_MAX))
307 bit = SVE_VQ_MAX - 1;
308
309 return SVE_VQ_MAX - bit;
310}
311
312/*
313 * All vector length selection from userspace comes through here.
314 * We're on a slow path, so some sanity-checks are included.
315 * If things go wrong there's a bug somewhere, but try to fall back to a
316 * safe choice.
317 */
318static unsigned int find_supported_vector_length(unsigned int vl)
319{
320 int bit;
321 int max_vl = sve_max_vl;
322
323 if (WARN_ON(!sve_vl_valid(vl)))
324 vl = SVE_VL_MIN;
325
326 if (WARN_ON(!sve_vl_valid(max_vl)))
327 max_vl = SVE_VL_MIN;
328
329 if (vl > max_vl)
330 vl = max_vl;
331
332 bit = find_next_bit(sve_vq_map, SVE_VQ_MAX,
333 vq_to_bit(sve_vq_from_vl(vl)));
334 return sve_vl_from_vq(bit_to_vq(bit));
335}
336
Dave Martinbc0ee472017-10-31 15:51:05 +0000337#define ZREG(sve_state, vq, n) ((char *)(sve_state) + \
338 (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET))
339
340/*
341 * Transfer the FPSIMD state in task->thread.fpsimd_state to
342 * task->thread.sve_state.
343 *
344 * Task can be a non-runnable task, or current. In the latter case,
345 * softirqs (and preemption) must be disabled.
346 * task->thread.sve_state must point to at least sve_state_size(task)
347 * bytes of allocated kernel memory.
348 * task->thread.fpsimd_state must be up to date before calling this function.
349 */
350static void fpsimd_to_sve(struct task_struct *task)
351{
352 unsigned int vq;
353 void *sst = task->thread.sve_state;
354 struct fpsimd_state const *fst = &task->thread.fpsimd_state;
355 unsigned int i;
356
357 if (!system_supports_sve())
358 return;
359
360 vq = sve_vq_from_vl(task->thread.sve_vl);
361 for (i = 0; i < 32; ++i)
362 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
363 sizeof(fst->vregs[i]));
364}
365
Dave Martin8cd969d2017-10-31 15:51:07 +0000366/*
367 * Transfer the SVE state in task->thread.sve_state to
368 * task->thread.fpsimd_state.
369 *
370 * Task can be a non-runnable task, or current. In the latter case,
371 * softirqs (and preemption) must be disabled.
372 * task->thread.sve_state must point to at least sve_state_size(task)
373 * bytes of allocated kernel memory.
374 * task->thread.sve_state must be up to date before calling this function.
375 */
376static void sve_to_fpsimd(struct task_struct *task)
377{
378 unsigned int vq;
379 void const *sst = task->thread.sve_state;
380 struct fpsimd_state *fst = &task->thread.fpsimd_state;
381 unsigned int i;
382
383 if (!system_supports_sve())
384 return;
385
386 vq = sve_vq_from_vl(task->thread.sve_vl);
387 for (i = 0; i < 32; ++i)
388 memcpy(&fst->vregs[i], ZREG(sst, vq, i),
389 sizeof(fst->vregs[i]));
390}
391
Dave Martinbc0ee472017-10-31 15:51:05 +0000392#ifdef CONFIG_ARM64_SVE
393
394/*
395 * Return how many bytes of memory are required to store the full SVE
396 * state for task, given task's currently configured vector length.
397 */
398size_t sve_state_size(struct task_struct const *task)
399{
400 return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl));
401}
402
403/*
404 * Ensure that task->thread.sve_state is allocated and sufficiently large.
405 *
406 * This function should be used only in preparation for replacing
407 * task->thread.sve_state with new data. The memory is always zeroed
408 * here to prevent stale data from showing through: this is done in
409 * the interest of testability and predictability: except in the
410 * do_sve_acc() case, there is no ABI requirement to hide stale data
411 * written previously be task.
412 */
413void sve_alloc(struct task_struct *task)
414{
415 if (task->thread.sve_state) {
416 memset(task->thread.sve_state, 0, sve_state_size(current));
417 return;
418 }
419
420 /* This is a small allocation (maximum ~8KB) and Should Not Fail. */
421 task->thread.sve_state =
422 kzalloc(sve_state_size(task), GFP_KERNEL);
423
424 /*
425 * If future SVE revisions can have larger vectors though,
426 * this may cease to be true:
427 */
428 BUG_ON(!task->thread.sve_state);
429}
430
Dave Martin43d4da2c42017-10-31 15:51:13 +0000431
432/*
433 * Ensure that task->thread.sve_state is up to date with respect to
434 * the user task, irrespective of when SVE is in use or not.
435 *
436 * This should only be called by ptrace. task must be non-runnable.
437 * task->thread.sve_state must point to at least sve_state_size(task)
438 * bytes of allocated kernel memory.
439 */
440void fpsimd_sync_to_sve(struct task_struct *task)
441{
442 if (!test_tsk_thread_flag(task, TIF_SVE))
443 fpsimd_to_sve(task);
444}
445
446/*
447 * Ensure that task->thread.fpsimd_state is up to date with respect to
448 * the user task, irrespective of whether SVE is in use or not.
449 *
450 * This should only be called by ptrace. task must be non-runnable.
451 * task->thread.sve_state must point to at least sve_state_size(task)
452 * bytes of allocated kernel memory.
453 */
454void sve_sync_to_fpsimd(struct task_struct *task)
455{
456 if (test_tsk_thread_flag(task, TIF_SVE))
457 sve_to_fpsimd(task);
458}
459
460/*
461 * Ensure that task->thread.sve_state is up to date with respect to
462 * the task->thread.fpsimd_state.
463 *
464 * This should only be called by ptrace to merge new FPSIMD register
465 * values into a task for which SVE is currently active.
466 * task must be non-runnable.
467 * task->thread.sve_state must point to at least sve_state_size(task)
468 * bytes of allocated kernel memory.
469 * task->thread.fpsimd_state must already have been initialised with
470 * the new FPSIMD register values to be merged in.
471 */
472void sve_sync_from_fpsimd_zeropad(struct task_struct *task)
473{
474 unsigned int vq;
475 void *sst = task->thread.sve_state;
476 struct fpsimd_state const *fst = &task->thread.fpsimd_state;
477 unsigned int i;
478
479 if (!test_tsk_thread_flag(task, TIF_SVE))
480 return;
481
482 vq = sve_vq_from_vl(task->thread.sve_vl);
483
484 memset(sst, 0, SVE_SIG_REGS_SIZE(vq));
485
486 for (i = 0; i < 32; ++i)
487 memcpy(ZREG(sst, vq, i), &fst->vregs[i],
488 sizeof(fst->vregs[i]));
489}
490
Dave Martin7582e222017-10-31 15:51:08 +0000491int sve_set_vector_length(struct task_struct *task,
492 unsigned long vl, unsigned long flags)
493{
494 if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT |
495 PR_SVE_SET_VL_ONEXEC))
496 return -EINVAL;
497
498 if (!sve_vl_valid(vl))
499 return -EINVAL;
500
501 /*
502 * Clamp to the maximum vector length that VL-agnostic SVE code can
503 * work with. A flag may be assigned in the future to allow setting
504 * of larger vector lengths without confusing older software.
505 */
506 if (vl > SVE_VL_ARCH_MAX)
507 vl = SVE_VL_ARCH_MAX;
508
509 vl = find_supported_vector_length(vl);
510
511 if (flags & (PR_SVE_VL_INHERIT |
512 PR_SVE_SET_VL_ONEXEC))
513 task->thread.sve_vl_onexec = vl;
514 else
515 /* Reset VL to system default on next exec: */
516 task->thread.sve_vl_onexec = 0;
517
518 /* Only actually set the VL if not deferred: */
519 if (flags & PR_SVE_SET_VL_ONEXEC)
520 goto out;
521
522 if (vl == task->thread.sve_vl)
523 goto out;
524
525 /*
526 * To ensure the FPSIMD bits of the SVE vector registers are preserved,
527 * write any live register state back to task_struct, and convert to a
528 * non-SVE thread.
529 */
530 if (task == current) {
531 local_bh_disable();
532
533 task_fpsimd_save();
534 set_thread_flag(TIF_FOREIGN_FPSTATE);
535 }
536
537 fpsimd_flush_task_state(task);
538 if (test_and_clear_tsk_thread_flag(task, TIF_SVE))
539 sve_to_fpsimd(task);
540
541 if (task == current)
542 local_bh_enable();
543
544 /*
545 * Force reallocation of task SVE state to the correct size
546 * on next use:
547 */
548 sve_free(task);
549
550 task->thread.sve_vl = vl;
551
552out:
553 if (flags & PR_SVE_VL_INHERIT)
554 set_tsk_thread_flag(task, TIF_SVE_VL_INHERIT);
555 else
556 clear_tsk_thread_flag(task, TIF_SVE_VL_INHERIT);
557
558 return 0;
559}
560
Dave Martinbc0ee472017-10-31 15:51:05 +0000561/*
Dave Martin2e0f2472017-10-31 15:51:10 +0000562 * Bitmap for temporary storage of the per-CPU set of supported vector lengths
563 * during secondary boot.
564 */
565static DECLARE_BITMAP(sve_secondary_vq_map, SVE_VQ_MAX);
566
567static void sve_probe_vqs(DECLARE_BITMAP(map, SVE_VQ_MAX))
568{
569 unsigned int vq, vl;
570 unsigned long zcr;
571
572 bitmap_zero(map, SVE_VQ_MAX);
573
574 zcr = ZCR_ELx_LEN_MASK;
575 zcr = read_sysreg_s(SYS_ZCR_EL1) & ~zcr;
576
577 for (vq = SVE_VQ_MAX; vq >= SVE_VQ_MIN; --vq) {
578 write_sysreg_s(zcr | (vq - 1), SYS_ZCR_EL1); /* self-syncing */
579 vl = sve_get_vl();
580 vq = sve_vq_from_vl(vl); /* skip intervening lengths */
581 set_bit(vq_to_bit(vq), map);
582 }
583}
584
585void __init sve_init_vq_map(void)
586{
587 sve_probe_vqs(sve_vq_map);
588}
589
590/*
591 * If we haven't committed to the set of supported VQs yet, filter out
592 * those not supported by the current CPU.
593 */
594void sve_update_vq_map(void)
595{
596 sve_probe_vqs(sve_secondary_vq_map);
597 bitmap_and(sve_vq_map, sve_vq_map, sve_secondary_vq_map, SVE_VQ_MAX);
598}
599
600/* Check whether the current CPU supports all VQs in the committed set */
601int sve_verify_vq_map(void)
602{
603 int ret = 0;
604
605 sve_probe_vqs(sve_secondary_vq_map);
606 bitmap_andnot(sve_secondary_vq_map, sve_vq_map, sve_secondary_vq_map,
607 SVE_VQ_MAX);
608 if (!bitmap_empty(sve_secondary_vq_map, SVE_VQ_MAX)) {
609 pr_warn("SVE: cpu%d: Required vector length(s) missing\n",
610 smp_processor_id());
611 ret = -EINVAL;
612 }
613
614 return ret;
615}
616
Dave Martinfdfa9762017-10-31 15:51:12 +0000617static void __init sve_efi_setup(void)
618{
619 if (!IS_ENABLED(CONFIG_EFI))
620 return;
621
622 /*
623 * alloc_percpu() warns and prints a backtrace if this goes wrong.
624 * This is evidence of a crippled system and we are returning void,
625 * so no attempt is made to handle this situation here.
626 */
627 if (!sve_vl_valid(sve_max_vl))
628 goto fail;
629
630 efi_sve_state = __alloc_percpu(
631 SVE_SIG_REGS_SIZE(sve_vq_from_vl(sve_max_vl)), SVE_VQ_BYTES);
632 if (!efi_sve_state)
633 goto fail;
634
635 return;
636
637fail:
638 panic("Cannot allocate percpu memory for EFI SVE save/restore");
639}
640
Dave Martin2e0f2472017-10-31 15:51:10 +0000641/*
642 * Enable SVE for EL1.
643 * Intended for use by the cpufeatures code during CPU boot.
644 */
645int sve_kernel_enable(void *__always_unused p)
646{
647 write_sysreg(read_sysreg(CPACR_EL1) | CPACR_EL1_ZEN_EL1EN, CPACR_EL1);
648 isb();
649
650 return 0;
651}
652
653void __init sve_setup(void)
654{
655 u64 zcr;
656
657 if (!system_supports_sve())
658 return;
659
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
668 zcr = read_sanitised_ftr_reg(SYS_ZCR_EL1);
669 sve_max_vl = sve_vl_from_vq((zcr & ZCR_ELx_LEN_MASK) + 1);
670
671 /*
672 * Sanity-check that the max VL we determined through CPU features
673 * corresponds properly to sve_vq_map. If not, do our best:
674 */
675 if (WARN_ON(sve_max_vl != find_supported_vector_length(sve_max_vl)))
676 sve_max_vl = find_supported_vector_length(sve_max_vl);
677
678 /*
679 * For the default VL, pick the maximum supported value <= 64.
680 * VL == 64 is guaranteed not to grow the signal frame.
681 */
682 sve_default_vl = find_supported_vector_length(64);
683
684 pr_info("SVE: maximum available vector length %u bytes per vector\n",
685 sve_max_vl);
686 pr_info("SVE: default vector length %u bytes per vector\n",
687 sve_default_vl);
Dave Martinfdfa9762017-10-31 15:51:12 +0000688
689 sve_efi_setup();
Dave Martin2e0f2472017-10-31 15:51:10 +0000690}
691
692/*
Dave Martinbc0ee472017-10-31 15:51:05 +0000693 * Called from the put_task_struct() path, which cannot get here
694 * unless dead_task is really dead and not schedulable.
695 */
696void fpsimd_release_task(struct task_struct *dead_task)
697{
698 __sve_free(dead_task);
699}
700
701#endif /* CONFIG_ARM64_SVE */
702
703/*
704 * Trapped SVE access
705 *
706 * Storage is allocated for the full SVE state, the current FPSIMD
707 * register contents are migrated across, and TIF_SVE is set so that
708 * the SVE access trap will be disabled the next time this task
709 * reaches ret_to_user.
710 *
711 * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load()
712 * would have disabled the SVE access trap for userspace during
713 * ret_to_user, making an SVE access trap impossible in that case.
714 */
715asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs)
716{
717 /* Even if we chose not to use SVE, the hardware could still trap: */
718 if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) {
719 force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0);
720 return;
721 }
722
723 sve_alloc(current);
724
725 local_bh_disable();
726
727 task_fpsimd_save();
728 fpsimd_to_sve(current);
729
730 /* Force ret_to_user to reload the registers: */
731 fpsimd_flush_task_state(current);
732 set_thread_flag(TIF_FOREIGN_FPSTATE);
733
734 if (test_and_set_thread_flag(TIF_SVE))
735 WARN_ON(1); /* SVE access shouldn't have trapped */
736
737 local_bh_enable();
738}
739
740/*
Catalin Marinas53631b52012-03-05 11:49:32 +0000741 * Trapped FP/ASIMD access.
742 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000743asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000744{
745 /* TODO: implement lazy context saving/restoring */
746 WARN_ON(1);
747}
748
749/*
750 * Raise a SIGFPE for the current process.
751 */
Dave Martin94ef7ec2017-10-31 15:50:54 +0000752asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs)
Catalin Marinas53631b52012-03-05 11:49:32 +0000753{
754 siginfo_t info;
755 unsigned int si_code = 0;
756
757 if (esr & FPEXC_IOF)
758 si_code = FPE_FLTINV;
759 else if (esr & FPEXC_DZF)
760 si_code = FPE_FLTDIV;
761 else if (esr & FPEXC_OFF)
762 si_code = FPE_FLTOVF;
763 else if (esr & FPEXC_UFF)
764 si_code = FPE_FLTUND;
765 else if (esr & FPEXC_IXF)
766 si_code = FPE_FLTRES;
767
768 memset(&info, 0, sizeof(info));
769 info.si_signo = SIGFPE;
770 info.si_code = si_code;
771 info.si_addr = (void __user *)instruction_pointer(regs);
772
773 send_sig_info(SIGFPE, &info, current);
774}
775
776void fpsimd_thread_switch(struct task_struct *next)
777{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000778 if (!system_supports_fpsimd())
779 return;
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200780 /*
781 * Save the current FPSIMD state to memory, but only if whatever is in
782 * the registers is in fact the most recent userland FPSIMD state of
783 * 'current'.
784 */
Dave Martinbc0ee472017-10-31 15:51:05 +0000785 if (current->mm)
786 task_fpsimd_save();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200787
788 if (next->mm) {
789 /*
790 * If we are switching to a task whose most recent userland
791 * FPSIMD state is already in the registers of *this* cpu,
792 * we can skip loading the state from memory. Otherwise, set
793 * the TIF_FOREIGN_FPSTATE flag so the state will be loaded
794 * upon the next return to userland.
795 */
796 struct fpsimd_state *st = &next->thread.fpsimd_state;
797
798 if (__this_cpu_read(fpsimd_last_state) == st
799 && st->cpu == smp_processor_id())
Dave Martin9cf5b542017-10-31 15:50:59 +0000800 clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200801 else
Dave Martin9cf5b542017-10-31 15:50:59 +0000802 set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200803 }
Catalin Marinas53631b52012-03-05 11:49:32 +0000804}
805
806void fpsimd_flush_thread(void)
807{
Dave Martin7582e222017-10-31 15:51:08 +0000808 int vl, supported_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000809
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000810 if (!system_supports_fpsimd())
811 return;
Dave Martincb84d112017-08-03 17:23:23 +0100812
813 local_bh_disable();
814
Catalin Marinas53631b52012-03-05 11:49:32 +0000815 memset(&current->thread.fpsimd_state, 0, sizeof(struct fpsimd_state));
Ard Biesheuvel674c242c2015-08-27 07:12:33 +0100816 fpsimd_flush_task_state(current);
Dave Martinbc0ee472017-10-31 15:51:05 +0000817
818 if (system_supports_sve()) {
819 clear_thread_flag(TIF_SVE);
820 sve_free(current);
821
822 /*
823 * Reset the task vector length as required.
824 * This is where we ensure that all user tasks have a valid
825 * vector length configured: no kernel task can become a user
826 * task without an exec and hence a call to this function.
Dave Martin2e0f2472017-10-31 15:51:10 +0000827 * By the time the first call to this function is made, all
828 * early hardware probing is complete, so sve_default_vl
829 * should be valid.
Dave Martinbc0ee472017-10-31 15:51:05 +0000830 * If a bug causes this to go wrong, we make some noise and
831 * try to fudge thread.sve_vl to a safe value here.
832 */
Dave Martin79ab0472017-10-31 15:51:06 +0000833 vl = current->thread.sve_vl_onexec ?
834 current->thread.sve_vl_onexec : sve_default_vl;
Dave Martinbc0ee472017-10-31 15:51:05 +0000835
836 if (WARN_ON(!sve_vl_valid(vl)))
837 vl = SVE_VL_MIN;
838
Dave Martin7582e222017-10-31 15:51:08 +0000839 supported_vl = find_supported_vector_length(vl);
840 if (WARN_ON(supported_vl != vl))
841 vl = supported_vl;
842
Dave Martinbc0ee472017-10-31 15:51:05 +0000843 current->thread.sve_vl = vl;
Dave Martin79ab0472017-10-31 15:51:06 +0000844
845 /*
846 * If the task is not set to inherit, ensure that the vector
847 * length will be reset by a subsequent exec:
848 */
849 if (!test_thread_flag(TIF_SVE_VL_INHERIT))
850 current->thread.sve_vl_onexec = 0;
Dave Martinbc0ee472017-10-31 15:51:05 +0000851 }
852
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200853 set_thread_flag(TIF_FOREIGN_FPSTATE);
Dave Martincb84d112017-08-03 17:23:23 +0100854
855 local_bh_enable();
Catalin Marinas53631b52012-03-05 11:49:32 +0000856}
857
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100858/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200859 * Save the userland FPSIMD state of 'current' to memory, but only if the state
860 * currently held in the registers does in fact belong to 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100861 */
862void fpsimd_preserve_current_state(void)
863{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000864 if (!system_supports_fpsimd())
865 return;
Dave Martincb84d112017-08-03 17:23:23 +0100866
867 local_bh_disable();
Dave Martin8cd969d2017-10-31 15:51:07 +0000868 task_fpsimd_save();
Dave Martincb84d112017-08-03 17:23:23 +0100869 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100870}
871
872/*
Dave Martin8cd969d2017-10-31 15:51:07 +0000873 * Like fpsimd_preserve_current_state(), but ensure that
874 * current->thread.fpsimd_state is updated so that it can be copied to
875 * the signal frame.
876 */
877void fpsimd_signal_preserve_current_state(void)
878{
879 fpsimd_preserve_current_state();
880 if (system_supports_sve() && test_thread_flag(TIF_SVE))
881 sve_to_fpsimd(current);
882}
883
884/*
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200885 * Load the userland FPSIMD state of 'current' from memory, but only if the
886 * FPSIMD state already held in the registers is /not/ the most recent FPSIMD
887 * state of 'current'
888 */
889void fpsimd_restore_current_state(void)
890{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000891 if (!system_supports_fpsimd())
892 return;
Dave Martincb84d112017-08-03 17:23:23 +0100893
894 local_bh_disable();
895
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200896 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
897 struct fpsimd_state *st = &current->thread.fpsimd_state;
898
Dave Martinbc0ee472017-10-31 15:51:05 +0000899 task_fpsimd_load();
Dave Martin50464182017-08-03 17:23:21 +0100900 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200901 st->cpu = smp_processor_id();
902 }
Dave Martincb84d112017-08-03 17:23:23 +0100903
904 local_bh_enable();
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200905}
906
907/*
908 * Load an updated userland FPSIMD state for 'current' from memory and set the
909 * flag that indicates that the FPSIMD register contents are the most recent
910 * FPSIMD state of 'current'
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100911 */
912void fpsimd_update_current_state(struct fpsimd_state *state)
913{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000914 if (!system_supports_fpsimd())
915 return;
Dave Martincb84d112017-08-03 17:23:23 +0100916
917 local_bh_disable();
918
Dave Martin8cd969d2017-10-31 15:51:07 +0000919 if (system_supports_sve() && test_thread_flag(TIF_SVE)) {
920 current->thread.fpsimd_state = *state;
921 fpsimd_to_sve(current);
922 }
923 task_fpsimd_load();
924
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200925 if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) {
926 struct fpsimd_state *st = &current->thread.fpsimd_state;
927
Dave Martin50464182017-08-03 17:23:21 +0100928 __this_cpu_write(fpsimd_last_state, st);
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200929 st->cpu = smp_processor_id();
930 }
Dave Martincb84d112017-08-03 17:23:23 +0100931
932 local_bh_enable();
Ard Biesheuvelc51f9262014-02-24 15:26:27 +0100933}
934
Ard Biesheuvel005f78c2014-05-08 11:20:23 +0200935/*
936 * Invalidate live CPU copies of task t's FPSIMD state
937 */
938void fpsimd_flush_task_state(struct task_struct *t)
939{
940 t->thread.fpsimd_state.cpu = NR_CPUS;
941}
942
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100943#ifdef CONFIG_KERNEL_MODE_NEON
944
Dave Martincb84d112017-08-03 17:23:23 +0100945DEFINE_PER_CPU(bool, kernel_neon_busy);
Catalin Marinas11cefd52017-08-07 12:36:35 +0100946EXPORT_PER_CPU_SYMBOL(kernel_neon_busy);
Ard Biesheuvel190f1ca2014-02-24 15:26:29 +0100947
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100948/*
949 * Kernel-side NEON support functions
950 */
Dave Martincb84d112017-08-03 17:23:23 +0100951
952/*
953 * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling
954 * context
955 *
956 * Must not be called unless may_use_simd() returns true.
957 * Task context in the FPSIMD registers is saved back to memory as necessary.
958 *
959 * A matching call to kernel_neon_end() must be made before returning from the
960 * calling context.
961 *
962 * The caller may freely use the FPSIMD registers until kernel_neon_end() is
963 * called.
964 */
965void kernel_neon_begin(void)
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100966{
Suzuki K Poulose82e01912016-11-08 13:56:21 +0000967 if (WARN_ON(!system_supports_fpsimd()))
968 return;
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100969
Dave Martincb84d112017-08-03 17:23:23 +0100970 BUG_ON(!may_use_simd());
971
972 local_bh_disable();
973
974 __this_cpu_write(kernel_neon_busy, true);
975
976 /* Save unsaved task fpsimd state, if any: */
Dave Martin1bd3f932017-10-31 15:51:11 +0000977 if (current->mm) {
978 task_fpsimd_save();
979 set_thread_flag(TIF_FOREIGN_FPSTATE);
980 }
Dave Martincb84d112017-08-03 17:23:23 +0100981
982 /* Invalidate any task state remaining in the fpsimd regs: */
983 __this_cpu_write(fpsimd_last_state, NULL);
984
985 preempt_disable();
986
987 local_bh_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100988}
Dave Martincb84d112017-08-03 17:23:23 +0100989EXPORT_SYMBOL(kernel_neon_begin);
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +0100990
Dave Martincb84d112017-08-03 17:23:23 +0100991/*
992 * kernel_neon_end(): give the CPU FPSIMD registers back to the current task
993 *
994 * Must be called from a context in which kernel_neon_begin() was previously
995 * called, with no call to kernel_neon_end() in the meantime.
996 *
997 * The caller must not use the FPSIMD registers after this function is called,
998 * unless kernel_neon_begin() is called again in the meantime.
999 */
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001000void kernel_neon_end(void)
1001{
Dave Martincb84d112017-08-03 17:23:23 +01001002 bool busy;
1003
Suzuki K Poulose82e01912016-11-08 13:56:21 +00001004 if (!system_supports_fpsimd())
1005 return;
Dave Martincb84d112017-08-03 17:23:23 +01001006
1007 busy = __this_cpu_xchg(kernel_neon_busy, false);
1008 WARN_ON(!busy); /* No matching kernel_neon_begin()? */
1009
1010 preempt_enable();
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001011}
1012EXPORT_SYMBOL(kernel_neon_end);
1013
Dave Martine580b8b2017-09-18 09:40:12 +01001014#ifdef CONFIG_EFI
1015
Dave Martin3b660232017-08-18 14:53:47 +01001016static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state);
1017static DEFINE_PER_CPU(bool, efi_fpsimd_state_used);
Dave Martinfdfa9762017-10-31 15:51:12 +00001018static DEFINE_PER_CPU(bool, efi_sve_state_used);
Dave Martin4328825d2017-08-03 17:23:22 +01001019
1020/*
1021 * EFI runtime services support functions
1022 *
1023 * The ABI for EFI runtime services allows EFI to use FPSIMD during the call.
1024 * This means that for EFI (and only for EFI), we have to assume that FPSIMD
1025 * is always used rather than being an optional accelerator.
1026 *
1027 * These functions provide the necessary support for ensuring FPSIMD
1028 * save/restore in the contexts from which EFI is used.
1029 *
1030 * Do not use them for any other purpose -- if tempted to do so, you are
1031 * either doing something wrong or you need to propose some refactoring.
1032 */
1033
1034/*
1035 * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call
1036 */
1037void __efi_fpsimd_begin(void)
1038{
1039 if (!system_supports_fpsimd())
1040 return;
1041
1042 WARN_ON(preemptible());
1043
Dave Martinfdfa9762017-10-31 15:51:12 +00001044 if (may_use_simd()) {
Dave Martin4328825d2017-08-03 17:23:22 +01001045 kernel_neon_begin();
Dave Martinfdfa9762017-10-31 15:51:12 +00001046 } else {
1047 /*
1048 * If !efi_sve_state, SVE can't be in use yet and doesn't need
1049 * preserving:
1050 */
1051 if (system_supports_sve() && likely(efi_sve_state)) {
1052 char *sve_state = this_cpu_ptr(efi_sve_state);
1053
1054 __this_cpu_write(efi_sve_state_used, true);
1055
1056 sve_save_state(sve_state + sve_ffr_offset(sve_max_vl),
1057 &this_cpu_ptr(&efi_fpsimd_state)->fpsr);
1058 } else {
1059 fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state));
1060 }
1061
Dave Martin4328825d2017-08-03 17:23:22 +01001062 __this_cpu_write(efi_fpsimd_state_used, true);
1063 }
1064}
1065
1066/*
1067 * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call
1068 */
1069void __efi_fpsimd_end(void)
1070{
1071 if (!system_supports_fpsimd())
1072 return;
1073
Dave Martinfdfa9762017-10-31 15:51:12 +00001074 if (!__this_cpu_xchg(efi_fpsimd_state_used, false)) {
Dave Martin4328825d2017-08-03 17:23:22 +01001075 kernel_neon_end();
Dave Martinfdfa9762017-10-31 15:51:12 +00001076 } else {
1077 if (system_supports_sve() &&
1078 likely(__this_cpu_read(efi_sve_state_used))) {
1079 char const *sve_state = this_cpu_ptr(efi_sve_state);
1080
1081 sve_load_state(sve_state + sve_ffr_offset(sve_max_vl),
1082 &this_cpu_ptr(&efi_fpsimd_state)->fpsr,
1083 sve_vq_from_vl(sve_get_vl()) - 1);
1084
1085 __this_cpu_write(efi_sve_state_used, false);
1086 } else {
1087 fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state));
1088 }
1089 }
Dave Martin4328825d2017-08-03 17:23:22 +01001090}
1091
Dave Martine580b8b2017-09-18 09:40:12 +01001092#endif /* CONFIG_EFI */
1093
Ard Biesheuvel4cfb3612013-07-09 14:18:12 +01001094#endif /* CONFIG_KERNEL_MODE_NEON */
1095
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001096#ifdef CONFIG_CPU_PM
1097static int fpsimd_cpu_pm_notifier(struct notifier_block *self,
1098 unsigned long cmd, void *v)
1099{
1100 switch (cmd) {
1101 case CPU_PM_ENTER:
Dave Martinbc0ee472017-10-31 15:51:05 +00001102 if (current->mm)
1103 task_fpsimd_save();
Leo Yan7c68a9c2014-09-01 11:09:51 +08001104 this_cpu_write(fpsimd_last_state, NULL);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001105 break;
1106 case CPU_PM_EXIT:
1107 if (current->mm)
Ard Biesheuvel005f78c2014-05-08 11:20:23 +02001108 set_thread_flag(TIF_FOREIGN_FPSTATE);
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001109 break;
1110 case CPU_PM_ENTER_FAILED:
1111 default:
1112 return NOTIFY_DONE;
1113 }
1114 return NOTIFY_OK;
1115}
1116
1117static struct notifier_block fpsimd_cpu_pm_notifier_block = {
1118 .notifier_call = fpsimd_cpu_pm_notifier,
1119};
1120
Jisheng Zhanga7c61a32015-11-20 17:59:10 +08001121static void __init fpsimd_pm_init(void)
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001122{
1123 cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block);
1124}
1125
1126#else
1127static inline void fpsimd_pm_init(void) { }
1128#endif /* CONFIG_CPU_PM */
1129
Janet Liu32365e62015-06-11 12:02:45 +08001130#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001131static int fpsimd_cpu_dead(unsigned int cpu)
Janet Liu32365e62015-06-11 12:02:45 +08001132{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001133 per_cpu(fpsimd_last_state, cpu) = NULL;
1134 return 0;
Janet Liu32365e62015-06-11 12:02:45 +08001135}
1136
Janet Liu32365e62015-06-11 12:02:45 +08001137static inline void fpsimd_hotplug_init(void)
1138{
Sebastian Andrzej Siewiorc23a7262016-09-06 19:04:37 +02001139 cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead",
1140 NULL, fpsimd_cpu_dead);
Janet Liu32365e62015-06-11 12:02:45 +08001141}
1142
1143#else
1144static inline void fpsimd_hotplug_init(void) { }
1145#endif
1146
Catalin Marinas53631b52012-03-05 11:49:32 +00001147/*
1148 * FP/SIMD support code initialisation.
1149 */
1150static int __init fpsimd_init(void)
1151{
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001152 if (elf_hwcap & HWCAP_FP) {
1153 fpsimd_pm_init();
1154 fpsimd_hotplug_init();
1155 } else {
Catalin Marinas53631b52012-03-05 11:49:32 +00001156 pr_notice("Floating-point is not implemented\n");
Catalin Marinas53631b52012-03-05 11:49:32 +00001157 }
Catalin Marinas53631b52012-03-05 11:49:32 +00001158
Suzuki K. Poulosefe80f9f2015-10-19 14:24:53 +01001159 if (!(elf_hwcap & HWCAP_ASIMD))
Catalin Marinas53631b52012-03-05 11:49:32 +00001160 pr_notice("Advanced SIMD is not implemented\n");
Lorenzo Pieralisifb1ab1a2013-07-19 17:48:08 +01001161
Catalin Marinas53631b52012-03-05 11:49:32 +00001162 return 0;
1163}
1164late_initcall(fpsimd_init);