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 | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 20 | #include <linux/bottom_half.h> |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 22 | #include <linux/cpu_pm.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 23 | #include <linux/kernel.h> |
| 24 | #include <linux/init.h> |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 25 | #include <linux/percpu.h> |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 26 | #include <linux/preempt.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 27 | #include <linux/sched/signal.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 28 | #include <linux/signal.h> |
| 29 | |
| 30 | #include <asm/fpsimd.h> |
| 31 | #include <asm/cputype.h> |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 32 | #include <asm/simd.h> |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 33 | |
| 34 | #define FPEXC_IOF (1 << 0) |
| 35 | #define FPEXC_DZF (1 << 1) |
| 36 | #define FPEXC_OFF (1 << 2) |
| 37 | #define FPEXC_UFF (1 << 3) |
| 38 | #define FPEXC_IXF (1 << 4) |
| 39 | #define FPEXC_IDF (1 << 7) |
| 40 | |
| 41 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 42 | * In order to reduce the number of times the FPSIMD state is needlessly saved |
| 43 | * and restored, we need to keep track of two things: |
| 44 | * (a) for each task, we need to remember which CPU was the last one to have |
| 45 | * the task's FPSIMD state loaded into its FPSIMD registers; |
| 46 | * (b) for each CPU, we need to remember which task's userland FPSIMD state has |
| 47 | * been loaded into its FPSIMD registers most recently, or whether it has |
| 48 | * been used to perform kernel mode NEON in the meantime. |
| 49 | * |
| 50 | * 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] | 51 | * 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] | 52 | * we add the per-cpu variable 'fpsimd_last_state' (below), which contains the |
| 53 | * address of the userland FPSIMD state of the task that was loaded onto the CPU |
| 54 | * the most recently, or NULL if kernel mode NEON has been performed after that. |
| 55 | * |
| 56 | * With this in place, we no longer have to restore the next FPSIMD state right |
| 57 | * when switching between tasks. Instead, we can defer this check to userland |
| 58 | * resume, at which time we verify whether the CPU's fpsimd_last_state and the |
| 59 | * task's fpsimd_state.cpu are still mutually in sync. If this is the case, we |
| 60 | * can omit the FPSIMD restore. |
| 61 | * |
| 62 | * As an optimization, we use the thread_info flag TIF_FOREIGN_FPSTATE to |
| 63 | * indicate whether or not the userland FPSIMD state of the current task is |
| 64 | * present in the registers. The flag is set unless the FPSIMD registers of this |
| 65 | * CPU currently contain the most recent userland FPSIMD state of the current |
| 66 | * task. |
| 67 | * |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 68 | * In order to allow softirq handlers to use FPSIMD, kernel_neon_begin() may |
| 69 | * save the task's FPSIMD context back to task_struct from softirq context. |
| 70 | * To prevent this from racing with the manipulation of the task's FPSIMD state |
| 71 | * from task context and thereby corrupting the state, it is necessary to |
| 72 | * protect any manipulation of a task's fpsimd_state or TIF_FOREIGN_FPSTATE |
| 73 | * flag with local_bh_disable() unless softirqs are already masked. |
| 74 | * |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 75 | * For a certain task, the sequence may look something like this: |
| 76 | * - the task gets scheduled in; if both the task's fpsimd_state.cpu field |
| 77 | * contains the id of the current CPU, and the CPU's fpsimd_last_state per-cpu |
| 78 | * variable points to the task's fpsimd_state, the TIF_FOREIGN_FPSTATE flag is |
| 79 | * cleared, otherwise it is set; |
| 80 | * |
| 81 | * - the task returns to userland; if TIF_FOREIGN_FPSTATE is set, the task's |
| 82 | * userland FPSIMD state is copied from memory to the registers, the task's |
| 83 | * fpsimd_state.cpu field is set to the id of the current CPU, the current |
| 84 | * CPU's fpsimd_last_state pointer is set to this task's fpsimd_state and the |
| 85 | * TIF_FOREIGN_FPSTATE flag is cleared; |
| 86 | * |
| 87 | * - the task executes an ordinary syscall; upon return to userland, the |
| 88 | * TIF_FOREIGN_FPSTATE flag will still be cleared, so no FPSIMD state is |
| 89 | * restored; |
| 90 | * |
| 91 | * - the task executes a syscall which executes some NEON instructions; this is |
| 92 | * preceded by a call to kernel_neon_begin(), which copies the task's FPSIMD |
| 93 | * register contents to memory, clears the fpsimd_last_state per-cpu variable |
| 94 | * and sets the TIF_FOREIGN_FPSTATE flag; |
| 95 | * |
| 96 | * - the task gets preempted after kernel_neon_end() is called; as we have not |
| 97 | * returned from the 2nd syscall yet, TIF_FOREIGN_FPSTATE is still set so |
| 98 | * whatever is in the FPSIMD registers is not saved to memory, but discarded. |
| 99 | */ |
| 100 | static DEFINE_PER_CPU(struct fpsimd_state *, fpsimd_last_state); |
| 101 | |
| 102 | /* |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 103 | * Trapped FP/ASIMD access. |
| 104 | */ |
| 105 | void do_fpsimd_acc(unsigned int esr, struct pt_regs *regs) |
| 106 | { |
| 107 | /* TODO: implement lazy context saving/restoring */ |
| 108 | WARN_ON(1); |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Raise a SIGFPE for the current process. |
| 113 | */ |
| 114 | void do_fpsimd_exc(unsigned int esr, struct pt_regs *regs) |
| 115 | { |
| 116 | siginfo_t info; |
| 117 | unsigned int si_code = 0; |
| 118 | |
| 119 | if (esr & FPEXC_IOF) |
| 120 | si_code = FPE_FLTINV; |
| 121 | else if (esr & FPEXC_DZF) |
| 122 | si_code = FPE_FLTDIV; |
| 123 | else if (esr & FPEXC_OFF) |
| 124 | si_code = FPE_FLTOVF; |
| 125 | else if (esr & FPEXC_UFF) |
| 126 | si_code = FPE_FLTUND; |
| 127 | else if (esr & FPEXC_IXF) |
| 128 | si_code = FPE_FLTRES; |
| 129 | |
| 130 | memset(&info, 0, sizeof(info)); |
| 131 | info.si_signo = SIGFPE; |
| 132 | info.si_code = si_code; |
| 133 | info.si_addr = (void __user *)instruction_pointer(regs); |
| 134 | |
| 135 | send_sig_info(SIGFPE, &info, current); |
| 136 | } |
| 137 | |
| 138 | void fpsimd_thread_switch(struct task_struct *next) |
| 139 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 140 | if (!system_supports_fpsimd()) |
| 141 | return; |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Save the current FPSIMD state to memory, but only if whatever is in |
| 144 | * the registers is in fact the most recent userland FPSIMD state of |
| 145 | * 'current'. |
| 146 | */ |
| 147 | if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE)) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 148 | fpsimd_save_state(¤t->thread.fpsimd_state); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 149 | |
| 150 | if (next->mm) { |
| 151 | /* |
| 152 | * If we are switching to a task whose most recent userland |
| 153 | * FPSIMD state is already in the registers of *this* cpu, |
| 154 | * we can skip loading the state from memory. Otherwise, set |
| 155 | * the TIF_FOREIGN_FPSTATE flag so the state will be loaded |
| 156 | * upon the next return to userland. |
| 157 | */ |
| 158 | struct fpsimd_state *st = &next->thread.fpsimd_state; |
| 159 | |
| 160 | if (__this_cpu_read(fpsimd_last_state) == st |
| 161 | && st->cpu == smp_processor_id()) |
| 162 | clear_ti_thread_flag(task_thread_info(next), |
| 163 | TIF_FOREIGN_FPSTATE); |
| 164 | else |
| 165 | set_ti_thread_flag(task_thread_info(next), |
| 166 | TIF_FOREIGN_FPSTATE); |
| 167 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | void fpsimd_flush_thread(void) |
| 171 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 172 | if (!system_supports_fpsimd()) |
| 173 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 174 | |
| 175 | local_bh_disable(); |
| 176 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 177 | memset(¤t->thread.fpsimd_state, 0, sizeof(struct fpsimd_state)); |
Ard Biesheuvel | 674c242c | 2015-08-27 07:12:33 +0100 | [diff] [blame] | 178 | fpsimd_flush_task_state(current); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 179 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 180 | |
| 181 | local_bh_enable(); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 184 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 185 | * Save the userland FPSIMD state of 'current' to memory, but only if the state |
| 186 | * currently held in the registers does in fact belong to 'current' |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 187 | */ |
| 188 | void fpsimd_preserve_current_state(void) |
| 189 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 190 | if (!system_supports_fpsimd()) |
| 191 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 192 | |
| 193 | local_bh_disable(); |
| 194 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 195 | if (!test_thread_flag(TIF_FOREIGN_FPSTATE)) |
| 196 | fpsimd_save_state(¤t->thread.fpsimd_state); |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 197 | |
| 198 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | /* |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 202 | * Load the userland FPSIMD state of 'current' from memory, but only if the |
| 203 | * FPSIMD state already held in the registers is /not/ the most recent FPSIMD |
| 204 | * state of 'current' |
| 205 | */ |
| 206 | void fpsimd_restore_current_state(void) |
| 207 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 208 | if (!system_supports_fpsimd()) |
| 209 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 210 | |
| 211 | local_bh_disable(); |
| 212 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 213 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 214 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 215 | |
| 216 | fpsimd_load_state(st); |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 217 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 218 | st->cpu = smp_processor_id(); |
| 219 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 220 | |
| 221 | local_bh_enable(); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Load an updated userland FPSIMD state for 'current' from memory and set the |
| 226 | * flag that indicates that the FPSIMD register contents are the most recent |
| 227 | * FPSIMD state of 'current' |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 228 | */ |
| 229 | void fpsimd_update_current_state(struct fpsimd_state *state) |
| 230 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 231 | if (!system_supports_fpsimd()) |
| 232 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 233 | |
| 234 | local_bh_disable(); |
| 235 | |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 236 | fpsimd_load_state(state); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 237 | if (test_and_clear_thread_flag(TIF_FOREIGN_FPSTATE)) { |
| 238 | struct fpsimd_state *st = ¤t->thread.fpsimd_state; |
| 239 | |
Dave Martin | 5046418 | 2017-08-03 17:23:21 +0100 | [diff] [blame] | 240 | __this_cpu_write(fpsimd_last_state, st); |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 241 | st->cpu = smp_processor_id(); |
| 242 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 243 | |
| 244 | local_bh_enable(); |
Ard Biesheuvel | c51f926 | 2014-02-24 15:26:27 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 247 | /* |
| 248 | * Invalidate live CPU copies of task t's FPSIMD state |
| 249 | */ |
| 250 | void fpsimd_flush_task_state(struct task_struct *t) |
| 251 | { |
| 252 | t->thread.fpsimd_state.cpu = NR_CPUS; |
| 253 | } |
| 254 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 255 | #ifdef CONFIG_KERNEL_MODE_NEON |
| 256 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 257 | DEFINE_PER_CPU(bool, kernel_neon_busy); |
Ard Biesheuvel | 190f1ca | 2014-02-24 15:26:29 +0100 | [diff] [blame] | 258 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 259 | /* |
| 260 | * Kernel-side NEON support functions |
| 261 | */ |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 262 | |
| 263 | /* |
| 264 | * kernel_neon_begin(): obtain the CPU FPSIMD registers for use by the calling |
| 265 | * context |
| 266 | * |
| 267 | * Must not be called unless may_use_simd() returns true. |
| 268 | * Task context in the FPSIMD registers is saved back to memory as necessary. |
| 269 | * |
| 270 | * A matching call to kernel_neon_end() must be made before returning from the |
| 271 | * calling context. |
| 272 | * |
| 273 | * The caller may freely use the FPSIMD registers until kernel_neon_end() is |
| 274 | * called. |
| 275 | */ |
| 276 | void kernel_neon_begin(void) |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 277 | { |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 278 | if (WARN_ON(!system_supports_fpsimd())) |
| 279 | return; |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 280 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 281 | BUG_ON(!may_use_simd()); |
| 282 | |
| 283 | local_bh_disable(); |
| 284 | |
| 285 | __this_cpu_write(kernel_neon_busy, true); |
| 286 | |
| 287 | /* Save unsaved task fpsimd state, if any: */ |
| 288 | if (current->mm && !test_and_set_thread_flag(TIF_FOREIGN_FPSTATE)) |
| 289 | fpsimd_save_state(¤t->thread.fpsimd_state); |
| 290 | |
| 291 | /* Invalidate any task state remaining in the fpsimd regs: */ |
| 292 | __this_cpu_write(fpsimd_last_state, NULL); |
| 293 | |
| 294 | preempt_disable(); |
| 295 | |
| 296 | local_bh_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 297 | } |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 298 | EXPORT_SYMBOL(kernel_neon_begin); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 299 | |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 300 | /* |
| 301 | * kernel_neon_end(): give the CPU FPSIMD registers back to the current task |
| 302 | * |
| 303 | * Must be called from a context in which kernel_neon_begin() was previously |
| 304 | * called, with no call to kernel_neon_end() in the meantime. |
| 305 | * |
| 306 | * The caller must not use the FPSIMD registers after this function is called, |
| 307 | * unless kernel_neon_begin() is called again in the meantime. |
| 308 | */ |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 309 | void kernel_neon_end(void) |
| 310 | { |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 311 | bool busy; |
| 312 | |
Suzuki K Poulose | 82e0191 | 2016-11-08 13:56:21 +0000 | [diff] [blame] | 313 | if (!system_supports_fpsimd()) |
| 314 | return; |
Dave Martin | cb84d11 | 2017-08-03 17:23:23 +0100 | [diff] [blame^] | 315 | |
| 316 | busy = __this_cpu_xchg(kernel_neon_busy, false); |
| 317 | WARN_ON(!busy); /* No matching kernel_neon_begin()? */ |
| 318 | |
| 319 | preempt_enable(); |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 320 | } |
| 321 | EXPORT_SYMBOL(kernel_neon_end); |
| 322 | |
Dave Martin | 4328825d | 2017-08-03 17:23:22 +0100 | [diff] [blame] | 323 | DEFINE_PER_CPU(struct fpsimd_state, efi_fpsimd_state); |
| 324 | DEFINE_PER_CPU(bool, efi_fpsimd_state_used); |
| 325 | |
| 326 | /* |
| 327 | * EFI runtime services support functions |
| 328 | * |
| 329 | * The ABI for EFI runtime services allows EFI to use FPSIMD during the call. |
| 330 | * This means that for EFI (and only for EFI), we have to assume that FPSIMD |
| 331 | * is always used rather than being an optional accelerator. |
| 332 | * |
| 333 | * These functions provide the necessary support for ensuring FPSIMD |
| 334 | * save/restore in the contexts from which EFI is used. |
| 335 | * |
| 336 | * Do not use them for any other purpose -- if tempted to do so, you are |
| 337 | * either doing something wrong or you need to propose some refactoring. |
| 338 | */ |
| 339 | |
| 340 | /* |
| 341 | * __efi_fpsimd_begin(): prepare FPSIMD for making an EFI runtime services call |
| 342 | */ |
| 343 | void __efi_fpsimd_begin(void) |
| 344 | { |
| 345 | if (!system_supports_fpsimd()) |
| 346 | return; |
| 347 | |
| 348 | WARN_ON(preemptible()); |
| 349 | |
| 350 | if (may_use_simd()) |
| 351 | kernel_neon_begin(); |
| 352 | else { |
| 353 | fpsimd_save_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 354 | __this_cpu_write(efi_fpsimd_state_used, true); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /* |
| 359 | * __efi_fpsimd_end(): clean up FPSIMD after an EFI runtime services call |
| 360 | */ |
| 361 | void __efi_fpsimd_end(void) |
| 362 | { |
| 363 | if (!system_supports_fpsimd()) |
| 364 | return; |
| 365 | |
| 366 | if (__this_cpu_xchg(efi_fpsimd_state_used, false)) |
| 367 | fpsimd_load_state(this_cpu_ptr(&efi_fpsimd_state)); |
| 368 | else |
| 369 | kernel_neon_end(); |
| 370 | } |
| 371 | |
Ard Biesheuvel | 4cfb361 | 2013-07-09 14:18:12 +0100 | [diff] [blame] | 372 | #endif /* CONFIG_KERNEL_MODE_NEON */ |
| 373 | |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 374 | #ifdef CONFIG_CPU_PM |
| 375 | static int fpsimd_cpu_pm_notifier(struct notifier_block *self, |
| 376 | unsigned long cmd, void *v) |
| 377 | { |
| 378 | switch (cmd) { |
| 379 | case CPU_PM_ENTER: |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 380 | if (current->mm && !test_thread_flag(TIF_FOREIGN_FPSTATE)) |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 381 | fpsimd_save_state(¤t->thread.fpsimd_state); |
Leo Yan | 7c68a9c | 2014-09-01 11:09:51 +0800 | [diff] [blame] | 382 | this_cpu_write(fpsimd_last_state, NULL); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 383 | break; |
| 384 | case CPU_PM_EXIT: |
| 385 | if (current->mm) |
Ard Biesheuvel | 005f78c | 2014-05-08 11:20:23 +0200 | [diff] [blame] | 386 | set_thread_flag(TIF_FOREIGN_FPSTATE); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 387 | break; |
| 388 | case CPU_PM_ENTER_FAILED: |
| 389 | default: |
| 390 | return NOTIFY_DONE; |
| 391 | } |
| 392 | return NOTIFY_OK; |
| 393 | } |
| 394 | |
| 395 | static struct notifier_block fpsimd_cpu_pm_notifier_block = { |
| 396 | .notifier_call = fpsimd_cpu_pm_notifier, |
| 397 | }; |
| 398 | |
Jisheng Zhang | a7c61a3 | 2015-11-20 17:59:10 +0800 | [diff] [blame] | 399 | static void __init fpsimd_pm_init(void) |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 400 | { |
| 401 | cpu_pm_register_notifier(&fpsimd_cpu_pm_notifier_block); |
| 402 | } |
| 403 | |
| 404 | #else |
| 405 | static inline void fpsimd_pm_init(void) { } |
| 406 | #endif /* CONFIG_CPU_PM */ |
| 407 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 408 | #ifdef CONFIG_HOTPLUG_CPU |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 409 | static int fpsimd_cpu_dead(unsigned int cpu) |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 410 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 411 | per_cpu(fpsimd_last_state, cpu) = NULL; |
| 412 | return 0; |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 413 | } |
| 414 | |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 415 | static inline void fpsimd_hotplug_init(void) |
| 416 | { |
Sebastian Andrzej Siewior | c23a726 | 2016-09-06 19:04:37 +0200 | [diff] [blame] | 417 | cpuhp_setup_state_nocalls(CPUHP_ARM64_FPSIMD_DEAD, "arm64/fpsimd:dead", |
| 418 | NULL, fpsimd_cpu_dead); |
Janet Liu | 32365e6 | 2015-06-11 12:02:45 +0800 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | #else |
| 422 | static inline void fpsimd_hotplug_init(void) { } |
| 423 | #endif |
| 424 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 425 | /* |
| 426 | * FP/SIMD support code initialisation. |
| 427 | */ |
| 428 | static int __init fpsimd_init(void) |
| 429 | { |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 430 | if (elf_hwcap & HWCAP_FP) { |
| 431 | fpsimd_pm_init(); |
| 432 | fpsimd_hotplug_init(); |
| 433 | } else { |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 434 | pr_notice("Floating-point is not implemented\n"); |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 435 | } |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 436 | |
Suzuki K. Poulose | fe80f9f | 2015-10-19 14:24:53 +0100 | [diff] [blame] | 437 | if (!(elf_hwcap & HWCAP_ASIMD)) |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 438 | pr_notice("Advanced SIMD is not implemented\n"); |
Lorenzo Pieralisi | fb1ab1a | 2013-07-19 17:48:08 +0100 | [diff] [blame] | 439 | |
Catalin Marinas | 53631b5 | 2012-03-05 11:49:32 +0000 | [diff] [blame] | 440 | return 0; |
| 441 | } |
| 442 | late_initcall(fpsimd_init); |