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 | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 32 | #include <linux/preempt.h> |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame^] | 33 | #include <linux/prctl.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 34 | #include <linux/ptrace.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 35 | #include <linux/sched/signal.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 36 | #include <linux/sched/task_stack.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 37 | #include <linux/signal.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 38 | #include <linux/slab.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 39 | |
| 40 | #include <asm/fpsimd.h> |
| 41 | #include <asm/cputype.h> |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 42 | #include <asm/simd.h> |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 43 | #include <asm/sigcontext.h> |
| 44 | #include <asm/sysreg.h> |
| 45 | #include <asm/traps.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 46 | |
| 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 Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 55 | * (Note: in this discussion, statements about FPSIMD apply equally to SVE.) |
| 56 | * |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 57 | * In order to reduce the number of times the FPSIMD state is needlessly saved |
| 58 | * and restored, we need to keep track of two things: |
| 59 | * (a) for each task, we need to remember which CPU was the last one to have |
| 60 | * the task's FPSIMD state loaded into its FPSIMD registers; |
| 61 | * (b) for each CPU, we need to remember which task's userland FPSIMD state has |
| 62 | * been loaded into its FPSIMD registers most recently, or whether it has |
| 63 | * been used to perform kernel mode NEON in the meantime. |
| 64 | * |
| 65 | * For (a), we add a 'cpu' field to struct fpsimd_state, which gets updated to |
Adam Buchbinder | ef769e3 | 2016-02-24 09:52:41 -0800 | [diff] [blame] | 66 | * 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] | 67 | * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the |
| 68 | * address of the userland FPSIMD state of the task that was loaded onto the CPU |
| 69 | * the most recently, or NULL if kernel mode NEON has been performed after that. |
| 70 | * |
| 71 | * With this in place, we no longer have to restore the next FPSIMD state right |
| 72 | * when switching between tasks. Instead, we can defer this check to userland |
| 73 | * resume, at which time we verify whether the CPU's fpsimd_last_state and the |
| 74 | * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we |
| 75 | * can omit the FPSIMD restore. |
| 76 | * |
| 77 | * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to |
| 78 | * indicate whether or not the userland FPSIMD state of the current task is |
| 79 | * present in the registers. The flag is set unless the FPSIMD registers of this |
| 80 | * CPU currently contain the most recent userland FPSIMD state of the current |
| 81 | * task. |
| 82 | * |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 83 | * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may |
| 84 | * save the task's FPSIMD context back to task_struct from softirq context. |
| 85 | * To prevent this from racing with the manipulation of the task's FPSIMD state |
| 86 | * from task context and thereby corrupting the state, it is necessary to |
| 87 | * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE |
| 88 | * flag with local_bh_disable() unless softirqs are already masked. |
| 89 | * |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 90 | * For a certain task, the sequence may look something like this: |
| 91 | * - the task gets scheduled in; if both the task's fpsimd_state.cpu field |
| 92 | * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu |
| 93 | * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is |
| 94 | * cleared, otherwise it is set; |
| 95 | * |
| 96 | * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's |
| 97 | * userland FPSIMD state is copied from memory to the registers, the task's |
| 98 | * fpsimd_state.cpu field is set to the id of the current CPU, the current |
| 99 | * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the |
| 100 | * TIF_FOREIGN_FPSTATE flag is cleared; |
| 101 | * |
| 102 | * - the task executes an ordinary syscall; upon return to userland, the |
| 103 | * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is |
| 104 | * restored; |
| 105 | * |
| 106 | * - the task executes a syscall which executes some NEON instructions; this is |
| 107 | * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD |
| 108 | * register contents to memory, clears the fpsimd_last_state per-cpu variable |
| 109 | * and sets the TIF_FOREIGN_FPSTATE flag; |
| 110 | * |
| 111 | * - the task gets preempted after kernel_neon_end() is called; as we have not |
| 112 | * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so |
| 113 | * whatever is in the FPSIMD registers is not saved to memory, but discarded. |
| 114 | */ |
| 115 | static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state); |
| 116 | |
Dave Martin | 79ab047 | 2017-10-31 15:51:06 +0000 | [diff] [blame] | 117 | /* Default VL for tasks that don't set it explicitly: */ |
| 118 | static int sve_default_vl = SVE_VL_MIN; |
| 119 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame^] | 120 | #ifdef CONFIG_ARM64_SVE |
| 121 | |
| 122 | /* Maximum supported vector length across all CPUs (initially poisoned) */ |
| 123 | int __ro_after_init sve_max_vl = -1; |
| 124 | /* Set of available vector lengths, as vq_to_bit(vq): */ |
| 125 | static DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX); |
| 126 | |
| 127 | #else /* ! CONFIG_ARM64_SVE */ |
| 128 | |
| 129 | /* Dummy declaration for code that will be optimised out: */ |
| 130 | extern DECLARE_BITMAP(sve_vq_map, SVE_VQ_MAX); |
| 131 | |
| 132 | #endif /* ! CONFIG_ARM64_SVE */ |
| 133 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 134 | /* |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 135 | * Call __sve_free() directly only if you know task can't be scheduled |
| 136 | * or preempted. |
| 137 | */ |
| 138 | static void __sve_free(struct task_struct *task) |
| 139 | { |
| 140 | kfree(task->thread.sve_state); |
| 141 | task->thread.sve_state = NULL; |
| 142 | } |
| 143 | |
| 144 | static void sve_free(struct task_struct *task) |
| 145 | { |
| 146 | WARN_ON(test_tsk_thread_flag(task, TIF_SVE)); |
| 147 | |
| 148 | __sve_free(task); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | /* Offset of FFR in the SVE register dump */ |
| 153 | static size_t sve_ffr_offset(int vl) |
| 154 | { |
| 155 | return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET; |
| 156 | } |
| 157 | |
| 158 | static void *sve_pffr(struct task_struct *task) |
| 159 | { |
| 160 | return (char *)task->thread.sve_state + |
| 161 | sve_ffr_offset(task->thread.sve_vl); |
| 162 | } |
| 163 | |
| 164 | static void change_cpacr(u64 val, u64 mask) |
| 165 | { |
| 166 | u64 cpacr = read_sysreg(CPACR_EL1); |
| 167 | u64 new = (cpacr & ~mask) | val; |
| 168 | |
| 169 | if (new != cpacr) |
| 170 | write_sysreg(new, CPACR_EL1); |
| 171 | } |
| 172 | |
| 173 | static void sve_user_disable(void) |
| 174 | { |
| 175 | change_cpacr(0, CPACR_EL1_ZEN_EL0EN); |
| 176 | } |
| 177 | |
| 178 | static void sve_user_enable(void) |
| 179 | { |
| 180 | change_cpacr(CPACR_EL1_ZEN_EL0EN, CPACR_EL1_ZEN_EL0EN); |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * TIF_SVE controls whether a task can use SVE without trapping while |
| 185 | * in userspace, and also the way a task's FPSIMD/SVE state is stored |
| 186 | * in thread_struct. |
| 187 | * |
| 188 | * The kernel uses this flag to track whether a user task is actively |
| 189 | * using SVE, and therefore whether full SVE register state needs to |
| 190 | * be tracked. If not, the cheaper FPSIMD context handling code can |
| 191 | * be used instead of the more costly SVE equivalents. |
| 192 | * |
| 193 | * * TIF_SVE set: |
| 194 | * |
| 195 | * The task can execute SVE instructions while in userspace without |
| 196 | * trapping to the kernel. |
| 197 | * |
| 198 | * When stored, Z0-Z31 (incorporating Vn in bits[127:0] or the |
| 199 | * corresponding Zn), P0-P15 and FFR are encoded in in |
| 200 | * task->thread.sve_state, formatted appropriately for vector |
| 201 | * length task->thread.sve_vl. |
| 202 | * |
| 203 | * task->thread.sve_state must point to a valid buffer at least |
| 204 | * sve_state_size(task) bytes in size. |
| 205 | * |
| 206 | * During any syscall, the kernel may optionally clear TIF_SVE and |
| 207 | * discard the vector state except for the FPSIMD subset. |
| 208 | * |
| 209 | * * TIF_SVE clear: |
| 210 | * |
| 211 | * An attempt by the user task to execute an SVE instruction causes |
| 212 | * do_sve_acc() to be called, which does some preparation and then |
| 213 | * sets TIF_SVE. |
| 214 | * |
| 215 | * When stored, FPSIMD registers V0-V31 are encoded in |
| 216 | * task->fpsimd_state; bits [max : 128] for each of Z0-Z31 are |
| 217 | * logically zero but not stored anywhere; P0-P15 and FFR are not |
| 218 | * stored and have unspecified values from userspace's point of |
| 219 | * view. For hygiene purposes, the kernel zeroes them on next use, |
| 220 | * but userspace is discouraged from relying on this. |
| 221 | * |
| 222 | * task->thread.sve_state does not need to be non-NULL, valid or any |
| 223 | * particular size: it must not be dereferenced. |
| 224 | * |
| 225 | * * FPSR and FPCR are always stored in task->fpsimd_state irrespctive of |
| 226 | * whether TIF_SVE is clear or set, since these are not vector length |
| 227 | * dependent. |
| 228 | */ |
| 229 | |
| 230 | /* |
| 231 | * Update current's FPSIMD/SVE registers from thread_struct. |
| 232 | * |
| 233 | * This function should be called only when the FPSIMD/SVE state in |
| 234 | * thread_struct is known to be up to date, when preparing to enter |
| 235 | * userspace. |
| 236 | * |
| 237 | * Softirqs (and preemption) must be disabled. |
| 238 | */ |
| 239 | static void task_fpsimd_load(void) |
| 240 | { |
| 241 | WARN_ON(!in_softirq() && !irqs_disabled()); |
| 242 | |
| 243 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) |
| 244 | sve_load_state(sve_pffr(current), |
| 245 | ¤t->thread.fpsimd_state.fpsr, |
| 246 | sve_vq_from_vl(current->thread.sve_vl) - 1); |
| 247 | else |
| 248 | fpsimd_load_state(¤t->thread.fpsimd_state); |
| 249 | |
| 250 | if (system_supports_sve()) { |
| 251 | /* Toggle SVE trapping for userspace if needed */ |
| 252 | if (test_thread_flag(TIF_SVE)) |
| 253 | sve_user_enable(); |
| 254 | else |
| 255 | sve_user_disable(); |
| 256 | |
| 257 | /* Serialised by exception return to user */ |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Ensure current's FPSIMD/SVE storage in thread_struct is up to date |
| 263 | * with respect to the CPU registers. |
| 264 | * |
| 265 | * Softirqs (and preemption) must be disabled. |
| 266 | */ |
| 267 | static void task_fpsimd_save(void) |
| 268 | { |
| 269 | WARN_ON(!in_softirq() && !irqs_disabled()); |
| 270 | |
| 271 | if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 272 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) { |
| 273 | if (WARN_ON(sve_get_vl() != current->thread.sve_vl)) { |
| 274 | /* |
| 275 | * Can't save the user regs, so current would |
| 276 | * re-enter user with corrupt state. |
| 277 | * There's no way to recover, so kill it: |
| 278 | */ |
| 279 | force_signal_inject( |
| 280 | SIGKILL, 0, current_pt_regs(), 0); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | sve_save_state(sve_pffr(current), |
| 285 | ¤t->thread.fpsimd_state.fpsr); |
| 286 | } else |
| 287 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| 288 | } |
| 289 | } |
| 290 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame^] | 291 | /* |
| 292 | * Helpers to translate bit indices in sve_vq_map to VQ values (and |
| 293 | * vice versa). This allows find_next_bit() to be used to find the |
| 294 | * _maximum_ VQ not exceeding a certain value. |
| 295 | */ |
| 296 | |
| 297 | static unsigned int vq_to_bit(unsigned int vq) |
| 298 | { |
| 299 | return SVE_VQ_MAX - vq; |
| 300 | } |
| 301 | |
| 302 | static unsigned int bit_to_vq(unsigned int bit) |
| 303 | { |
| 304 | if (WARN_ON(bit >= SVE_VQ_MAX)) |
| 305 | bit = SVE_VQ_MAX - 1; |
| 306 | |
| 307 | return SVE_VQ_MAX - bit; |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * All vector length selection from userspace comes through here. |
| 312 | * We're on a slow path, so some sanity-checks are included. |
| 313 | * If things go wrong there's a bug somewhere, but try to fall back to a |
| 314 | * safe choice. |
| 315 | */ |
| 316 | static unsigned int find_supported_vector_length(unsigned int vl) |
| 317 | { |
| 318 | int bit; |
| 319 | int max_vl = sve_max_vl; |
| 320 | |
| 321 | if (WARN_ON(!sve_vl_valid(vl))) |
| 322 | vl = SVE_VL_MIN; |
| 323 | |
| 324 | if (WARN_ON(!sve_vl_valid(max_vl))) |
| 325 | max_vl = SVE_VL_MIN; |
| 326 | |
| 327 | if (vl > max_vl) |
| 328 | vl = max_vl; |
| 329 | |
| 330 | bit = find_next_bit(sve_vq_map, SVE_VQ_MAX, |
| 331 | vq_to_bit(sve_vq_from_vl(vl))); |
| 332 | return sve_vl_from_vq(bit_to_vq(bit)); |
| 333 | } |
| 334 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 335 | #define ZREG(sve_state, vq, n) ((char *)(sve_state) + \ |
| 336 | (SVE_SIG_ZREG_OFFSET(vq, n) - SVE_SIG_REGS_OFFSET)) |
| 337 | |
| 338 | /* |
| 339 | * Transfer the FPSIMD state in task->thread.fpsimd_state to |
| 340 | * task->thread.sve_state. |
| 341 | * |
| 342 | * Task can be a non-runnable task, or current. In the latter case, |
| 343 | * softirqs (and preemption) must be disabled. |
| 344 | * task->thread.sve_state must point to at least sve_state_size(task) |
| 345 | * bytes of allocated kernel memory. |
| 346 | * task->thread.fpsimd_state must be up to date before calling this function. |
| 347 | */ |
| 348 | static void fpsimd_to_sve(struct task_struct *task) |
| 349 | { |
| 350 | unsigned int vq; |
| 351 | void *sst = task->thread.sve_state; |
| 352 | struct fpsimd_state const *fst = &task->thread.fpsimd_state; |
| 353 | unsigned int i; |
| 354 | |
| 355 | if (!system_supports_sve()) |
| 356 | return; |
| 357 | |
| 358 | vq = sve_vq_from_vl(task->thread.sve_vl); |
| 359 | for (i = 0; i < 32; ++i) |
| 360 | memcpy(ZREG(sst, vq, i), &fst->vregs[i], |
| 361 | sizeof(fst->vregs[i])); |
| 362 | } |
| 363 | |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 364 | /* |
| 365 | * Transfer the SVE state in task->thread.sve_state to |
| 366 | * task->thread.fpsimd_state. |
| 367 | * |
| 368 | * Task can be a non-runnable task, or current. In the latter case, |
| 369 | * softirqs (and preemption) must be disabled. |
| 370 | * task->thread.sve_state must point to at least sve_state_size(task) |
| 371 | * bytes of allocated kernel memory. |
| 372 | * task->thread.sve_state must be up to date before calling this function. |
| 373 | */ |
| 374 | static void sve_to_fpsimd(struct task_struct *task) |
| 375 | { |
| 376 | unsigned int vq; |
| 377 | void const *sst = task->thread.sve_state; |
| 378 | struct fpsimd_state *fst = &task->thread.fpsimd_state; |
| 379 | unsigned int i; |
| 380 | |
| 381 | if (!system_supports_sve()) |
| 382 | return; |
| 383 | |
| 384 | vq = sve_vq_from_vl(task->thread.sve_vl); |
| 385 | for (i = 0; i < 32; ++i) |
| 386 | memcpy(&fst->vregs[i], ZREG(sst, vq, i), |
| 387 | sizeof(fst->vregs[i])); |
| 388 | } |
| 389 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 390 | #ifdef CONFIG_ARM64_SVE |
| 391 | |
| 392 | /* |
| 393 | * Return how many bytes of memory are required to store the full SVE |
| 394 | * state for task, given task's currently configured vector length. |
| 395 | */ |
| 396 | size_t sve_state_size(struct task_struct const *task) |
| 397 | { |
| 398 | return SVE_SIG_REGS_SIZE(sve_vq_from_vl(task->thread.sve_vl)); |
| 399 | } |
| 400 | |
| 401 | /* |
| 402 | * Ensure that task->thread.sve_state is allocated and sufficiently large. |
| 403 | * |
| 404 | * This function should be used only in preparation for replacing |
| 405 | * task->thread.sve_state with new data. The memory is always zeroed |
| 406 | * here to prevent stale data from showing through: this is done in |
| 407 | * the interest of testability and predictability: except in the |
| 408 | * do_sve_acc() case, there is no ABI requirement to hide stale data |
| 409 | * written previously be task. |
| 410 | */ |
| 411 | void sve_alloc(struct task_struct *task) |
| 412 | { |
| 413 | if (task->thread.sve_state) { |
| 414 | memset(task->thread.sve_state, 0, sve_state_size(current)); |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | /* This is a small allocation (maximum ~8KB) and Should Not Fail. */ |
| 419 | task->thread.sve_state = |
| 420 | kzalloc(sve_state_size(task), GFP_KERNEL); |
| 421 | |
| 422 | /* |
| 423 | * If future SVE revisions can have larger vectors though, |
| 424 | * this may cease to be true: |
| 425 | */ |
| 426 | BUG_ON(!task->thread.sve_state); |
| 427 | } |
| 428 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame^] | 429 | int sve_set_vector_length(struct task_struct *task, |
| 430 | unsigned long vl, unsigned long flags) |
| 431 | { |
| 432 | if (flags & ~(unsigned long)(PR_SVE_VL_INHERIT | |
| 433 | PR_SVE_SET_VL_ONEXEC)) |
| 434 | return -EINVAL; |
| 435 | |
| 436 | if (!sve_vl_valid(vl)) |
| 437 | return -EINVAL; |
| 438 | |
| 439 | /* |
| 440 | * Clamp to the maximum vector length that VL-agnostic SVE code can |
| 441 | * work with. A flag may be assigned in the future to allow setting |
| 442 | * of larger vector lengths without confusing older software. |
| 443 | */ |
| 444 | if (vl > SVE_VL_ARCH_MAX) |
| 445 | vl = SVE_VL_ARCH_MAX; |
| 446 | |
| 447 | vl = find_supported_vector_length(vl); |
| 448 | |
| 449 | if (flags & (PR_SVE_VL_INHERIT | |
| 450 | PR_SVE_SET_VL_ONEXEC)) |
| 451 | task->thread.sve_vl_onexec = vl; |
| 452 | else |
| 453 | /* Reset VL to system default on next exec: */ |
| 454 | task->thread.sve_vl_onexec = 0; |
| 455 | |
| 456 | /* Only actually set the VL if not deferred: */ |
| 457 | if (flags & PR_SVE_SET_VL_ONEXEC) |
| 458 | goto out; |
| 459 | |
| 460 | if (vl == task->thread.sve_vl) |
| 461 | goto out; |
| 462 | |
| 463 | /* |
| 464 | * To ensure the FPSIMD bits of the SVE vector registers are preserved, |
| 465 | * write any live register state back to task_struct, and convert to a |
| 466 | * non-SVE thread. |
| 467 | */ |
| 468 | if (task == current) { |
| 469 | local_bh_disable(); |
| 470 | |
| 471 | task_fpsimd_save(); |
| 472 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
| 473 | } |
| 474 | |
| 475 | fpsimd_flush_task_state(task); |
| 476 | if (test_and_clear_tsk_thread_flag(task, TIF_SVE)) |
| 477 | sve_to_fpsimd(task); |
| 478 | |
| 479 | if (task == current) |
| 480 | local_bh_enable(); |
| 481 | |
| 482 | /* |
| 483 | * Force reallocation of task SVE state to the correct size |
| 484 | * on next use: |
| 485 | */ |
| 486 | sve_free(task); |
| 487 | |
| 488 | task->thread.sve_vl = vl; |
| 489 | |
| 490 | out: |
| 491 | if (flags & PR_SVE_VL_INHERIT) |
| 492 | set_tsk_thread_flag(task, TIF_SVE_VL_INHERIT); |
| 493 | else |
| 494 | clear_tsk_thread_flag(task, TIF_SVE_VL_INHERIT); |
| 495 | |
| 496 | return 0; |
| 497 | } |
| 498 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 499 | /* |
| 500 | * Called from the put_task_struct() path, which cannot get here |
| 501 | * unless dead_task is really dead and not schedulable. |
| 502 | */ |
| 503 | void fpsimd_release_task(struct task_struct *dead_task) |
| 504 | { |
| 505 | __sve_free(dead_task); |
| 506 | } |
| 507 | |
| 508 | #endif /* CONFIG_ARM64_SVE */ |
| 509 | |
| 510 | /* |
| 511 | * Trapped SVE access |
| 512 | * |
| 513 | * Storage is allocated for the full SVE state, the current FPSIMD |
| 514 | * register contents are migrated across, and TIF_SVE is set so that |
| 515 | * the SVE access trap will be disabled the next time this task |
| 516 | * reaches ret_to_user. |
| 517 | * |
| 518 | * TIF_SVE should be clear on entry: otherwise, task_fpsimd_load() |
| 519 | * would have disabled the SVE access trap for userspace during |
| 520 | * ret_to_user, making an SVE access trap impossible in that case. |
| 521 | */ |
| 522 | asmlinkage void do_sve_acc(unsigned int esr, struct pt_regs *regs) |
| 523 | { |
| 524 | /* Even if we chose not to use SVE, the hardware could still trap: */ |
| 525 | if (unlikely(!system_supports_sve()) || WARN_ON(is_compat_task())) { |
| 526 | force_signal_inject(SIGILL, ILL_ILLOPC, regs, 0); |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | sve_alloc(current); |
| 531 | |
| 532 | local_bh_disable(); |
| 533 | |
| 534 | task_fpsimd_save(); |
| 535 | fpsimd_to_sve(current); |
| 536 | |
| 537 | /* Force ret_to_user to reload the registers: */ |
| 538 | fpsimd_flush_task_state(current); |
| 539 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
| 540 | |
| 541 | if (test_and_set_thread_flag(TIF_SVE)) |
| 542 | WARN_ON(1); /* SVE access shouldn't have trapped */ |
| 543 | |
| 544 | local_bh_enable(); |
| 545 | } |
| 546 | |
| 547 | /* |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 548 | * Trapped FP/ASIMD access. |
| 549 | */ |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 550 | asmlinkage void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 551 | { |
| 552 | /* TODO: implement lazy context saving/restoring */ |
| 553 | WARN_ON(1); |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * Raise a SIGFPE for the current process. |
| 558 | */ |
Dave Martin | 94ef7ec | 2017-10-31 15:50:54 +0000 | [diff] [blame] | 559 | asmlinkage void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 560 | { |
| 561 | siginfo_t info; |
| 562 | unsigned int si_code = 0; |
| 563 | |
| 564 | if (esr & FPEXC_IOF) |
| 565 | si_code = FPE_FLTINV; |
| 566 | else if (esr & FPEXC_DZF) |
| 567 | si_code = FPE_FLTDIV; |
| 568 | else if (esr & FPEXC_OFF) |
| 569 | si_code = FPE_FLTOVF; |
| 570 | else if (esr & FPEXC_UFF) |
| 571 | si_code = FPE_FLTUND; |
| 572 | else if (esr & FPEXC_IXF) |
| 573 | si_code = FPE_FLTRES; |
| 574 | |
| 575 | memset(&info, 0, sizeof(info)); |
| 576 | info.si_signo = SIGFPE; |
| 577 | info.si_code = si_code; |
| 578 | info.si_addr = (void __user *)instruction_pointer(regs); |
| 579 | |
| 580 | send_sig_info(SIGFPE, &info, current); |
| 581 | } |
| 582 | |
| 583 | void fpsimd_thread_switch(struct task_struct *next) |
| 584 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 585 | if (!system_supports_fpsimd()) |
| 586 | return; |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 587 | /* |
| 588 | * Save the current FPSIMD state to memory, but only if whatever is in |
| 589 | * the registers is in fact the most recent userland FPSIMD state of |
| 590 | * 'current'. |
| 591 | */ |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 592 | if (current->mm) |
| 593 | task_fpsimd_save(); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 594 | |
| 595 | if (next->mm) { |
| 596 | /* |
| 597 | * If we are switching to a task whose most recent userland |
| 598 | * FPSIMD state is already in the registers of *this* cpu, |
| 599 | * we can skip loading the state from memory. Otherwise, set |
| 600 | * the TIF_FOREIGN_FPSTATE flag so the state will be loaded |
| 601 | * upon the next return to userland. |
| 602 | */ |
| 603 | struct fpsimd_state *st = &next->thread.fpsimd_state; |
| 604 | |
| 605 | if (__this_cpu_read(fpsimd_last_state) == st |
| 606 | && st->cpu == smp_processor_id()) |
Dave Martin | 9cf5b54 | 2017-10-31 15:50:59 +0000 | [diff] [blame] | 607 | clear_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 608 | else |
Dave Martin | 9cf5b54 | 2017-10-31 15:50:59 +0000 | [diff] [blame] | 609 | set_tsk_thread_flag(next, TIF_FOREIGN_FPSTATE); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 610 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void fpsimd_flush_thread(void) |
| 614 | { |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame^] | 615 | int vl, supported_vl; |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 616 | |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 617 | if (!system_supports_fpsimd()) |
| 618 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 619 | |
| 620 | local_bh_disable(); |
| 621 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 622 | memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state)); |
Ard Biesheuvel | 674c242c | 2015-08-27 07:12:33 +0100 | [diff] [blame] | 623 | fpsimd_flush_task_state(current); |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 624 | |
| 625 | if (system_supports_sve()) { |
| 626 | clear_thread_flag(TIF_SVE); |
| 627 | sve_free(current); |
| 628 | |
| 629 | /* |
| 630 | * Reset the task vector length as required. |
| 631 | * This is where we ensure that all user tasks have a valid |
| 632 | * vector length configured: no kernel task can become a user |
| 633 | * task without an exec and hence a call to this function. |
| 634 | * If a bug causes this to go wrong, we make some noise and |
| 635 | * try to fudge thread.sve_vl to a safe value here. |
| 636 | */ |
Dave Martin | 79ab047 | 2017-10-31 15:51:06 +0000 | [diff] [blame] | 637 | vl = current->thread.sve_vl_onexec ? |
| 638 | current->thread.sve_vl_onexec : sve_default_vl; |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 639 | |
| 640 | if (WARN_ON(!sve_vl_valid(vl))) |
| 641 | vl = SVE_VL_MIN; |
| 642 | |
Dave Martin | 7582e22 | 2017-10-31 15:51:08 +0000 | [diff] [blame^] | 643 | supported_vl = find_supported_vector_length(vl); |
| 644 | if (WARN_ON(supported_vl != vl)) |
| 645 | vl = supported_vl; |
| 646 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 647 | current->thread.sve_vl = vl; |
Dave Martin | 79ab047 | 2017-10-31 15:51:06 +0000 | [diff] [blame] | 648 | |
| 649 | /* |
| 650 | * If the task is not set to inherit, ensure that the vector |
| 651 | * length will be reset by a subsequent exec: |
| 652 | */ |
| 653 | if (!test_thread_flag(TIF_SVE_VL_INHERIT)) |
| 654 | current->thread.sve_vl_onexec = 0; |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 657 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 658 | |
| 659 | local_bh_enable(); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 660 | } |
| 661 | |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 662 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 663 | * Save the userland FPSIMD state of 'current' to memory, but only if the state |
| 664 | * currently held in the registers does in fact belong to 'current' |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 665 | */ |
| 666 | void fpsimd_preserve_current_state(void) |
| 667 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 668 | if (!system_supports_fpsimd()) |
| 669 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 670 | |
| 671 | local_bh_disable(); |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 672 | task_fpsimd_save(); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 673 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 677 | * Like fpsimd_preserve_current_state(), but ensure that |
| 678 | * current->thread.fpsimd_state is updated so that it can be copied to |
| 679 | * the signal frame. |
| 680 | */ |
| 681 | void fpsimd_signal_preserve_current_state(void) |
| 682 | { |
| 683 | fpsimd_preserve_current_state(); |
| 684 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) |
| 685 | sve_to_fpsimd(current); |
| 686 | } |
| 687 | |
| 688 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 689 | * Load the userland FPSIMD state of 'current' from memory, but only if the |
| 690 | * FPSIMD state already held in the registers is /not/ the most recent FPSIMD |
| 691 | * state of 'current' |
| 692 | */ |
| 693 | void fpsimd_restore_current_state(void) |
| 694 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 695 | if (!system_supports_fpsimd()) |
| 696 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 697 | |
| 698 | local_bh_disable(); |
| 699 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 700 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 701 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 702 | |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 703 | task_fpsimd_load(); |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 704 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 705 | st->cpu = smp_processor_id(); |
| 706 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 707 | |
| 708 | local_bh_enable(); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /* |
| 712 | * Load an updated userland FPSIMD state for 'current' from memory and set the |
| 713 | * flag that indicates that the FPSIMD register contents are the most recent |
| 714 | * FPSIMD state of 'current' |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 715 | */ |
| 716 | void fpsimd_update_current_state(struct fpsimd_state *state) |
| 717 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 718 | if (!system_supports_fpsimd()) |
| 719 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 720 | |
| 721 | local_bh_disable(); |
| 722 | |
Dave Martin | 8cd969d | 2017-10-31 15:51:07 +0000 | [diff] [blame] | 723 | if (system_supports_sve() && test_thread_flag(TIF_SVE)) { |
| 724 | current->thread.fpsimd_state = *state; |
| 725 | fpsimd_to_sve(current); |
| 726 | } |
| 727 | task_fpsimd_load(); |
| 728 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 729 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 730 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 731 | |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 732 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 733 | st->cpu = smp_processor_id(); |
| 734 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 735 | |
| 736 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 737 | } |
| 738 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 739 | /* |
| 740 | * Invalidate live CPU copies of task t's FPSIMD state |
| 741 | */ |
| 742 | void fpsimd_flush_task_state(struct task_struct *t) |
| 743 | { |
| 744 | t->thread.fpsimd_state.cpu = NR_CPUS; |
| 745 | } |
| 746 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 747 | #ifdef CONFIG_KERNEL_MODE_NEON |
| 748 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 749 | DEFINE_PER_CPU(bool, kernel_neon_busy); |
Catalin Marinas | 11cefd5 | 2017-08-07 12:36:35 +0100 | [diff] [blame] | 750 | EXPORT_PER_CPU_SYMBOL(kernel_neon_busy); |
Ard Biesheuvel | 190f1ca | 2014-02-24 15:26:29 +0100 | [diff] [blame] | 751 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 752 | /* |
| 753 | * Kernel-side NEON support functions |
| 754 | */ |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 755 | |
| 756 | /* |
| 757 | * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling |
| 758 | * context |
| 759 | * |
| 760 | * Must not be called unless may_use_simd() returns true. |
| 761 | * Task context in the FPSIMD registers is saved back to memory as necessary. |
| 762 | * |
| 763 | * A matching call to kernel_neon_end() must be made before returning from the |
| 764 | * calling context. |
| 765 | * |
| 766 | * The caller may freely use the FPSIMD registers until kernel_neon_end() is |
| 767 | * called. |
| 768 | */ |
| 769 | void kernel_neon_begin(void) |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 770 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 771 | if (WARN_ON(!system_supports_fpsimd())) |
| 772 | return; |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 773 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 774 | BUG_ON(!may_use_simd()); |
| 775 | |
| 776 | local_bh_disable(); |
| 777 | |
| 778 | __this_cpu_write(kernel_neon_busy, true); |
| 779 | |
| 780 | /* Save unsaved task fpsimd state, if any: */ |
| 781 | if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE)) |
| 782 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| 783 | |
| 784 | /* Invalidate any task state remaining in the fpsimd regs: */ |
| 785 | __this_cpu_write(fpsimd_last_state, NULL); |
| 786 | |
| 787 | preempt_disable(); |
| 788 | |
| 789 | local_bh_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 790 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 791 | EXPORT_SYMBOL(kernel_neon_begin); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 792 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 793 | /* |
| 794 | * kernel_neon_end(): give the CPU FPSIMD registers back to the current task |
| 795 | * |
| 796 | * Must be called from a context in which kernel_neon_begin() was previously |
| 797 | * called, with no call to kernel_neon_end() in the meantime. |
| 798 | * |
| 799 | * The caller must not use the FPSIMD registers after this function is called, |
| 800 | * unless kernel_neon_begin() is called again in the meantime. |
| 801 | */ |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 802 | void kernel_neon_end(void) |
| 803 | { |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 804 | bool busy; |
| 805 | |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 806 | if (!system_supports_fpsimd()) |
| 807 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame] | 808 | |
| 809 | busy = __this_cpu_xchg(kernel_neon_busy, false); |
| 810 | WARN_ON(!busy); /* No matching kernel_neon_begin()? */ |
| 811 | |
| 812 | preempt_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 813 | } |
| 814 | EXPORT_SYMBOL(kernel_neon_end); |
| 815 | |
Dave Martin | e580b8b | 2017-09-18 09:40:12 +0100 | [diff] [blame] | 816 | #ifdef CONFIG_EFI |
| 817 | |
Dave Martin | 3b66023 | 2017-08-18 14:53:47 +0100 | [diff] [blame] | 818 | static DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state); |
| 819 | static DEFINE_PER_CPU(bool, efi_fpsimd_state_used); |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 820 | |
| 821 | /* |
| 822 | * EFI runtime services support functions |
| 823 | * |
| 824 | * The ABI for EFI runtime services allows EFI to use FPSIMD during the call. |
| 825 | * This means that for EFI (and only for EFI), we have to assume that FPSIMD |
| 826 | * is always used rather than being an optional accelerator. |
| 827 | * |
| 828 | * These functions provide the necessary support for ensuring FPSIMD |
| 829 | * save/restore in the contexts from which EFI is used. |
| 830 | * |
| 831 | * Do not use them for any other purpose -- if tempted to do so, you are |
| 832 | * either doing something wrong or you need to propose some refactoring. |
| 833 | */ |
| 834 | |
| 835 | /* |
| 836 | * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call |
| 837 | */ |
| 838 | void __efi_fpsimd_begin(void) |
| 839 | { |
| 840 | if (!system_supports_fpsimd()) |
| 841 | return; |
| 842 | |
| 843 | WARN_ON(preemptible()); |
| 844 | |
| 845 | if (may_use_simd()) |
| 846 | kernel_neon_begin(); |
| 847 | else { |
| 848 | fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 849 | __this_cpu_write(efi_fpsimd_state_used, true); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | /* |
| 854 | * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call |
| 855 | */ |
| 856 | void __efi_fpsimd_end(void) |
| 857 | { |
| 858 | if (!system_supports_fpsimd()) |
| 859 | return; |
| 860 | |
| 861 | if (__this_cpu_xchg(efi_fpsimd_state_used, false)) |
| 862 | fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 863 | else |
| 864 | kernel_neon_end(); |
| 865 | } |
| 866 | |
Dave Martin | e580b8b | 2017-09-18 09:40:12 +0100 | [diff] [blame] | 867 | #endif /* CONFIG_EFI */ |
| 868 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 869 | #endif /* CONFIG_KERNEL_MODE_NEON */ |
| 870 | |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 871 | #ifdef CONFIG_CPU_PM |
| 872 | static int fpsimd_cpu_pm_notifier(struct notifier_block *self, |
| 873 | unsigned long cmd, void *v) |
| 874 | { |
| 875 | switch (cmd) { |
| 876 | case CPU_PM_ENTER: |
Dave Martin | bc0ee47 | 2017-10-31 15:51:05 +0000 | [diff] [blame] | 877 | if (current->mm) |
| 878 | task_fpsimd_save(); |
Leo Yan | 7c68a9c | 2014-09-01 11:09:51 +0800 | [diff] [blame] | 879 | this_cpu_write(fpsimd_last_state, NULL); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 880 | break; |
| 881 | case CPU_PM_EXIT: |
| 882 | if (current->mm) |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 883 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 884 | break; |
| 885 | case CPU_PM_ENTER_FAILED: |
| 886 | default: |
| 887 | return NOTIFY_DONE; |
| 888 | } |
| 889 | return NOTIFY_OK; |
| 890 | } |
| 891 | |
| 892 | static struct notifier_block fpsimd_cpu_pm_notifier_block = { |
| 893 | .notifier_call = fpsimd_cpu_pm_notifier, |
| 894 | }; |
| 895 | |
Jisheng Zhang | a7c61a3 | 2015-11-20 17:59:10 +0800 | [diff] [blame] | 896 | static void __init fpsimd_pm_init(void) |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 897 | { |
| 898 | cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block); |
| 899 | } |
| 900 | |
| 901 | #else |
| 902 | static inline void fpsimd_pm_init(void) { } |
| 903 | #endif /* CONFIG_CPU_PM */ |
| 904 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 905 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 906 | static int fpsimd_cpu_dead(unsigned int cpu) |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 907 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 908 | per_cpu(fpsimd_last_state, cpu) = NULL; |
| 909 | return 0; |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 910 | } |
| 911 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 912 | static inline void fpsimd_hotplug_init(void) |
| 913 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 914 | cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead", |
| 915 | NULL, fpsimd_cpu_dead); |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | #else |
| 919 | static inline void fpsimd_hotplug_init(void) { } |
| 920 | #endif |
| 921 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 922 | /* |
| 923 | * FP/SIMD support code initialisation. |
| 924 | */ |
| 925 | static int __init fpsimd_init(void) |
| 926 | { |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 927 | if (elf_hwcap & HWCAP_FP) { |
| 928 | fpsimd_pm_init(); |
| 929 | fpsimd_hotplug_init(); |
| 930 | } else { |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 931 | pr_notice("Floating-point is not implemented\n"); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 932 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 933 | |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 934 | if (!(elf_hwcap & HWCAP_ASIMD)) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 935 | pr_notice("Advanced SIMD is not implemented\n"); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 936 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 937 | return 0; |
| 938 | } |
| 939 | late_initcall(fpsimd_init); |