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