blob: 3640351171b8f49f7d8e10caef72d957dbd02972 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/vfp/vfpmodule.c
3 *
4 * Copyright (C) 2004 ARM Limited.
5 * Written by Deep Blue Solutions Limited.
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#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
Russell King90b44192010-12-18 10:59:49 +000013#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Russell King90b44192010-12-18 10:59:49 +000015#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/signal.h>
17#include <linux/sched.h>
Russell King90b44192010-12-18 10:59:49 +000018#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/init.h>
Russell Kingd6551e82006-06-21 13:31:52 +010020
Tony Lindgren5aaf2542010-07-01 13:41:05 +010021#include <asm/cputype.h>
Russell Kingd6551e82006-06-21 13:31:52 +010022#include <asm/thread_notify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/vfp.h>
24
25#include "vfpinstr.h"
26#include "vfp.h"
27
28/*
29 * Our undef handlers (in entry.S)
30 */
31void vfp_testing_entry(void);
32void vfp_support_entry(void);
Russell King5d4cae52007-06-10 12:22:20 +010033void vfp_null_entry(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Russell King5d4cae52007-06-10 12:22:20 +010035void (*vfp_vector)(void) = vfp_null_entry;
Russell Kingaf61bdf2011-07-09 13:44:04 +010036
37/*
38 * The pointer to the vfpstate structure of the thread which currently
39 * owns the context held in the VFP hardware, or NULL if the hardware
40 * context is invalid.
41 */
42union vfp_state *vfp_current_hw_state[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * Dual-use variable.
46 * Used in startup: set to non-zero if VFP checks fail
47 * After startup, holds VFP architecture
48 */
49unsigned int VFP_arch;
50
Russell King0d782dc2009-12-12 14:47:40 +000051/*
52 * Per-thread VFP initialization.
53 */
54static void vfp_thread_flush(struct thread_info *thread)
55{
56 union vfp_state *vfp = &thread->vfpstate;
57 unsigned int cpu;
58
59 memset(vfp, 0, sizeof(union vfp_state));
60
61 vfp->hard.fpexc = FPEXC_EN;
62 vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
63
64 /*
65 * Disable VFP to ensure we initialize it first. We must ensure
Russell Kingaf61bdf2011-07-09 13:44:04 +010066 * that the modification of vfp_current_hw_state[] and hardware disable
Russell King0d782dc2009-12-12 14:47:40 +000067 * are done for the same CPU and without preemption.
68 */
69 cpu = get_cpu();
Russell Kingaf61bdf2011-07-09 13:44:04 +010070 if (vfp_current_hw_state[cpu] == vfp)
71 vfp_current_hw_state[cpu] = NULL;
Russell King0d782dc2009-12-12 14:47:40 +000072 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
73 put_cpu();
74}
75
Russell King797245f2009-12-18 14:34:43 +000076static void vfp_thread_exit(struct thread_info *thread)
Russell King0d782dc2009-12-12 14:47:40 +000077{
78 /* release case: Per-thread VFP cleanup. */
79 union vfp_state *vfp = &thread->vfpstate;
Russell King797245f2009-12-18 14:34:43 +000080 unsigned int cpu = get_cpu();
Russell King0d782dc2009-12-12 14:47:40 +000081
Russell Kingaf61bdf2011-07-09 13:44:04 +010082 if (vfp_current_hw_state[cpu] == vfp)
83 vfp_current_hw_state[cpu] = NULL;
Russell King797245f2009-12-18 14:34:43 +000084 put_cpu();
Russell King0d782dc2009-12-12 14:47:40 +000085}
86
Catalin Marinasc98c0972011-04-06 16:17:17 +010087static void vfp_thread_copy(struct thread_info *thread)
88{
89 struct thread_info *parent = current_thread_info();
90
91 vfp_sync_hwstate(parent);
92 thread->vfpstate = parent->vfpstate;
93}
94
Russell King0d782dc2009-12-12 14:47:40 +000095/*
96 * When this function is called with the following 'cmd's, the following
97 * is true while this function is being run:
98 * THREAD_NOFTIFY_SWTICH:
99 * - the previously running thread will not be scheduled onto another CPU.
100 * - the next thread to be run (v) will not be running on another CPU.
101 * - thread->cpu is the local CPU number
102 * - not preemptible as we're called in the middle of a thread switch
103 * THREAD_NOTIFY_FLUSH:
104 * - the thread (v) will be running on the local CPU, so
105 * v === current_thread_info()
106 * - thread->cpu is the local CPU number at the time it is accessed,
107 * but may change at any time.
108 * - we could be preempted if tree preempt rcu is enabled, so
109 * it is unsafe to use thread->cpu.
Russell King797245f2009-12-18 14:34:43 +0000110 * THREAD_NOTIFY_EXIT
111 * - the thread (v) will be running on the local CPU, so
112 * v === current_thread_info()
113 * - thread->cpu is the local CPU number at the time it is accessed,
114 * but may change at any time.
115 * - we could be preempted if tree preempt rcu is enabled, so
116 * it is unsafe to use thread->cpu.
Russell King0d782dc2009-12-12 14:47:40 +0000117 */
Russell Kingd6551e82006-06-21 13:31:52 +0100118static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Russell Kingd6551e82006-06-21 13:31:52 +0100120 struct thread_info *thread = v;
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100121 u32 fpexc;
122#ifdef CONFIG_SMP
123 unsigned int cpu;
124#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100126 switch (cmd) {
127 case THREAD_NOTIFY_SWITCH:
128 fpexc = fmrx(FPEXC);
Catalin Marinasc6428462007-01-24 18:47:08 +0100129
130#ifdef CONFIG_SMP
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100131 cpu = thread->cpu;
Russell King0d782dc2009-12-12 14:47:40 +0000132
Catalin Marinasc6428462007-01-24 18:47:08 +0100133 /*
134 * On SMP, if VFP is enabled, save the old state in
135 * case the thread migrates to a different CPU. The
136 * restoring is done lazily.
137 */
Russell Kingaf61bdf2011-07-09 13:44:04 +0100138 if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu]) {
139 vfp_save_state(vfp_current_hw_state[cpu], fpexc);
140 vfp_current_hw_state[cpu]->hard.cpu = cpu;
Catalin Marinasc6428462007-01-24 18:47:08 +0100141 }
142 /*
143 * Thread migration, just force the reloading of the
144 * state on the new CPU in case the VFP registers
145 * contain stale data.
146 */
147 if (thread->vfpstate.hard.cpu != cpu)
Russell Kingaf61bdf2011-07-09 13:44:04 +0100148 vfp_current_hw_state[cpu] = NULL;
Catalin Marinasc6428462007-01-24 18:47:08 +0100149#endif
150
Russell King681a4992006-08-27 12:38:34 +0100151 /*
152 * Always disable VFP so we can lazily save/restore the
153 * old state.
154 */
Russell King228adef2007-07-18 09:37:10 +0100155 fmxr(FPEXC, fpexc & ~FPEXC_EN);
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100156 break;
Russell King681a4992006-08-27 12:38:34 +0100157
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100158 case THREAD_NOTIFY_FLUSH:
Russell King0d782dc2009-12-12 14:47:40 +0000159 vfp_thread_flush(thread);
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100160 break;
161
162 case THREAD_NOTIFY_EXIT:
Russell King797245f2009-12-18 14:34:43 +0000163 vfp_thread_exit(thread);
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100164 break;
Catalin Marinasc98c0972011-04-06 16:17:17 +0100165
166 case THREAD_NOTIFY_COPY:
167 vfp_thread_copy(thread);
168 break;
Catalin Marinas2e82669a2011-04-06 16:16:29 +0100169 }
Russell King681a4992006-08-27 12:38:34 +0100170
Russell Kingd6551e82006-06-21 13:31:52 +0100171 return NOTIFY_DONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Russell Kingd6551e82006-06-21 13:31:52 +0100174static struct notifier_block vfp_notifier_block = {
175 .notifier_call = vfp_notifier,
176};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/*
179 * Raise a SIGFPE for the current process.
180 * sicode describes the signal being raised.
181 */
Russell King2bbd7e92011-01-08 12:05:09 +0000182static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 siginfo_t info;
185
186 memset(&info, 0, sizeof(info));
187
188 info.si_signo = SIGFPE;
189 info.si_code = sicode;
Al Viro35d59fc2006-10-11 17:22:44 +0100190 info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 /*
193 * This is the same as NWFPE, because it's not clear what
194 * this is used for
195 */
196 current->thread.error_code = 0;
197 current->thread.trap_no = 6;
198
Russell Kingda411192005-06-29 23:02:02 +0100199 send_sig_info(SIGFPE, &info, current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Catalin Marinasc98929c2007-11-22 18:32:01 +0100202static void vfp_panic(char *reason, u32 inst)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int i;
205
206 printk(KERN_ERR "VFP: Error: %s\n", reason);
207 printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
Catalin Marinasc98929c2007-11-22 18:32:01 +0100208 fmrx(FPEXC), fmrx(FPSCR), inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 for (i = 0; i < 32; i += 2)
210 printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
211 i, vfp_get_float(i), i+1, vfp_get_float(i+1));
212}
213
214/*
215 * Process bitmask of exception conditions.
216 */
217static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
218{
219 int si_code = 0;
220
221 pr_debug("VFP: raising exceptions %08x\n", exceptions);
222
Daniel Jacobowitz7c6f2512006-08-27 12:42:08 +0100223 if (exceptions == VFP_EXCEPTION_ERROR) {
Catalin Marinasc98929c2007-11-22 18:32:01 +0100224 vfp_panic("unhandled bounce", inst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 vfp_raise_sigfpe(0, regs);
226 return;
227 }
228
229 /*
Catalin Marinasdbead402010-02-01 18:50:40 +0100230 * If any of the status flags are set, update the FPSCR.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * Comparison instructions always return at least one of
232 * these flags set.
233 */
Catalin Marinasdbead402010-02-01 18:50:40 +0100234 if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
235 fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 fpscr |= exceptions;
238
239 fmxr(FPSCR, fpscr);
240
241#define RAISE(stat,en,sig) \
242 if (exceptions & stat && fpscr & en) \
243 si_code = sig;
244
245 /*
246 * These are arranged in priority order, least to highest.
247 */
Takashi Ohmasae0f205d2006-10-23 11:19:40 +0100248 RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
250 RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
251 RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
252 RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);
253
254 if (si_code)
255 vfp_raise_sigfpe(si_code, regs);
256}
257
258/*
259 * Emulate a VFP instruction.
260 */
261static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
262{
Daniel Jacobowitz7c6f2512006-08-27 12:42:08 +0100263 u32 exceptions = VFP_EXCEPTION_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);
266
267 if (INST_CPRTDO(inst)) {
268 if (!INST_CPRT(inst)) {
269 /*
270 * CPDO
271 */
272 if (vfp_single(inst)) {
273 exceptions = vfp_single_cpdo(inst, fpscr);
274 } else {
275 exceptions = vfp_double_cpdo(inst, fpscr);
276 }
277 } else {
278 /*
279 * A CPRT instruction can not appear in FPINST2, nor
280 * can it cause an exception. Therefore, we do not
281 * have to emulate it.
282 */
283 }
284 } else {
285 /*
286 * A CPDT instruction can not appear in FPINST2, nor can
287 * it cause an exception. Therefore, we do not have to
288 * emulate it.
289 */
290 }
Russell King928bd1b2006-04-25 20:41:27 +0100291 return exceptions & ~VFP_NAN_FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
295 * Package up a bounce condition.
296 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100297void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Catalin Marinasc98929c2007-11-22 18:32:01 +0100299 u32 fpscr, orig_fpscr, fpsid, exceptions;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);
302
303 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100304 * At this point, FPEXC can have the following configuration:
305 *
306 * EX DEX IXE
307 * 0 1 x - synchronous exception
308 * 1 x 0 - asynchronous exception
309 * 1 x 1 - sychronous on VFP subarch 1 and asynchronous on later
310 * 0 0 1 - synchronous on VFP9 (non-standard subarch 1
311 * implementation), undefined otherwise
312 *
313 * Clear various bits and enable access to the VFP so we can
314 * handle the bounce.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100316 fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Catalin Marinasc98929c2007-11-22 18:32:01 +0100318 fpsid = fmrx(FPSID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 orig_fpscr = fpscr = fmrx(FPSCR);
320
321 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100322 * Check for the special VFP subarch 1 and FPSCR.IXE bit case
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100324 if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
325 && (fpscr & FPSCR_IXE)) {
326 /*
327 * Synchronous exception, emulate the trigger instruction
328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto emulate;
330 }
331
Catalin Marinasc98929c2007-11-22 18:32:01 +0100332 if (fpexc & FPEXC_EX) {
Catalin Marinas85d69432009-05-30 14:00:18 +0100333#ifndef CONFIG_CPU_FEROCEON
Catalin Marinasc98929c2007-11-22 18:32:01 +0100334 /*
335 * Asynchronous exception. The instruction is read from FPINST
336 * and the interrupted instruction has to be restarted.
337 */
338 trigger = fmrx(FPINST);
339 regs->ARM_pc -= 4;
Catalin Marinas85d69432009-05-30 14:00:18 +0100340#endif
Catalin Marinasc98929c2007-11-22 18:32:01 +0100341 } else if (!(fpexc & FPEXC_DEX)) {
342 /*
343 * Illegal combination of bits. It can be caused by an
344 * unallocated VFP instruction but with FPSCR.IXE set and not
345 * on VFP subarch 1.
346 */
347 vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
George G. Davisf2255be2009-04-01 20:27:18 +0100348 goto exit;
Catalin Marinasc98929c2007-11-22 18:32:01 +0100349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100352 * Modify fpscr to indicate the number of iterations remaining.
353 * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
354 * whether FPEXC.VECITR or FPSCR.LEN is used.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100356 if (fpexc & (FPEXC_EX | FPEXC_VV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 u32 len;
358
359 len = fpexc + (1 << FPEXC_LENGTH_BIT);
360
361 fpscr &= ~FPSCR_LENGTH_MASK;
362 fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
363 }
364
365 /*
366 * Handle the first FP instruction. We used to take note of the
367 * FPEXC bounce reason, but this appears to be unreliable.
368 * Emulate the bounced instruction instead.
369 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100370 exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (exceptions)
Catalin Marinasc98929c2007-11-22 18:32:01 +0100372 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /*
Catalin Marinasc98929c2007-11-22 18:32:01 +0100375 * If there isn't a second FP instruction, exit now. Note that
376 * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Catalin Marinasc98929c2007-11-22 18:32:01 +0100378 if (fpexc ^ (FPEXC_EX | FPEXC_FP2V))
George G. Davisf2255be2009-04-01 20:27:18 +0100379 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /*
382 * The barrier() here prevents fpinst2 being read
383 * before the condition above.
384 */
385 barrier();
386 trigger = fmrx(FPINST2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 emulate:
Catalin Marinasc98929c2007-11-22 18:32:01 +0100389 exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (exceptions)
391 vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
George G. Davisf2255be2009-04-01 20:27:18 +0100392 exit:
393 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Russell Kingefe90d22006-12-08 15:22:20 +0000395
Russell King8e140362007-01-02 23:40:30 +0000396static void vfp_enable(void *unused)
397{
398 u32 access = get_copro_access();
399
400 /*
401 * Enable full access to VFP (cp10 and cp11)
402 */
403 set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
404}
405
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100406#ifdef CONFIG_PM
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200407#include <linux/syscore_ops.h>
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100408
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200409static int vfp_pm_suspend(void)
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100410{
411 struct thread_info *ti = current_thread_info();
412 u32 fpexc = fmrx(FPEXC);
413
414 /* if vfp is on, then save state for resumption */
415 if (fpexc & FPEXC_EN) {
416 printk(KERN_DEBUG "%s: saving vfp state\n", __func__);
417 vfp_save_state(&ti->vfpstate, fpexc);
418
419 /* disable, just in case */
420 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
421 }
422
423 /* clear any information we had about last context state */
Russell Kingaf61bdf2011-07-09 13:44:04 +0100424 memset(vfp_current_hw_state, 0, sizeof(vfp_current_hw_state));
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100425
426 return 0;
427}
428
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200429static void vfp_pm_resume(void)
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100430{
431 /* ensure we have access to the vfp */
432 vfp_enable(NULL);
433
434 /* and disable it to ensure the next usage restores the state */
435 fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100436}
437
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200438static struct syscore_ops vfp_pm_syscore_ops = {
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100439 .suspend = vfp_pm_suspend,
440 .resume = vfp_pm_resume,
441};
442
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100443static void vfp_pm_init(void)
444{
Rafael J. Wysocki328f5cc2011-04-22 22:02:33 +0200445 register_syscore_ops(&vfp_pm_syscore_ops);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100446}
447
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100448#else
449static inline void vfp_pm_init(void) { }
450#endif /* CONFIG_PM */
451
Russell Kingad187f92010-02-06 11:36:23 +0000452void vfp_sync_hwstate(struct thread_info *thread)
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100453{
454 unsigned int cpu = get_cpu();
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100455
456 /*
Russell King54cb3db2010-02-06 11:27:45 +0000457 * If the thread we're interested in is the current owner of the
458 * hardware VFP state, then we need to save its state.
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100459 */
Russell Kingaf61bdf2011-07-09 13:44:04 +0100460 if (vfp_current_hw_state[cpu] == &thread->vfpstate) {
Russell King54cb3db2010-02-06 11:27:45 +0000461 u32 fpexc = fmrx(FPEXC);
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100462
Russell King54cb3db2010-02-06 11:27:45 +0000463 /*
464 * Save the last VFP state on this CPU.
465 */
466 fmxr(FPEXC, fpexc | FPEXC_EN);
467 vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN);
Russell Kingad187f92010-02-06 11:36:23 +0000468 fmxr(FPEXC, fpexc);
469 }
470
471 put_cpu();
472}
473
474void vfp_flush_hwstate(struct thread_info *thread)
475{
476 unsigned int cpu = get_cpu();
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100477
478 /*
Russell Kingad187f92010-02-06 11:36:23 +0000479 * If the thread we're interested in is the current owner of the
480 * hardware VFP state, then we need to save its state.
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100481 */
Russell Kingaf61bdf2011-07-09 13:44:04 +0100482 if (vfp_current_hw_state[cpu] == &thread->vfpstate) {
Russell Kingad187f92010-02-06 11:36:23 +0000483 u32 fpexc = fmrx(FPEXC);
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100484
Russell King54cb3db2010-02-06 11:27:45 +0000485 fmxr(FPEXC, fpexc & ~FPEXC_EN);
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100486
Russell King54cb3db2010-02-06 11:27:45 +0000487 /*
488 * Set the context to NULL to force a reload the next time
489 * the thread uses the VFP.
490 */
Russell Kingaf61bdf2011-07-09 13:44:04 +0100491 vfp_current_hw_state[cpu] = NULL;
Russell King54cb3db2010-02-06 11:27:45 +0000492 }
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100493
Imre Deak5c5cac62010-04-11 15:57:07 +0100494#ifdef CONFIG_SMP
495 /*
496 * For SMP we still have to take care of the case where the thread
497 * migrates to another CPU and then back to the original CPU on which
498 * the last VFP user is still the same thread. Mark the thread VFP
499 * state as belonging to a non-existent CPU so that the saved one will
500 * be reloaded in the above case.
501 */
502 thread->vfpstate.hard.cpu = NR_CPUS;
503#endif
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100504 put_cpu();
505}
Catalin Marinas3d1228e2009-02-11 13:12:56 +0100506
Russell King90b44192010-12-18 10:59:49 +0000507/*
508 * VFP hardware can lose all context when a CPU goes offline.
Russell King74c25be2011-01-31 14:43:25 +0000509 * As we will be running in SMP mode with CPU hotplug, we will save the
510 * hardware state at every thread switch. We clear our held state when
511 * a CPU has been killed, indicating that the VFP hardware doesn't contain
512 * a threads VFP state. When a CPU starts up, we re-enable access to the
513 * VFP hardware.
Russell King90b44192010-12-18 10:59:49 +0000514 *
515 * Both CPU_DYING and CPU_STARTING are called on the CPU which
516 * is being offlined/onlined.
517 */
518static int vfp_hotplug(struct notifier_block *b, unsigned long action,
519 void *hcpu)
520{
521 if (action == CPU_DYING || action == CPU_DYING_FROZEN) {
522 unsigned int cpu = (long)hcpu;
Russell Kingaf61bdf2011-07-09 13:44:04 +0100523 vfp_current_hw_state[cpu] = NULL;
Russell King90b44192010-12-18 10:59:49 +0000524 } else if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
525 vfp_enable(NULL);
526 return NOTIFY_OK;
527}
Russell King8e140362007-01-02 23:40:30 +0000528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529/*
530 * VFP support code initialisation.
531 */
532static int __init vfp_init(void)
533{
534 unsigned int vfpsid;
Russell Kingefe90d22006-12-08 15:22:20 +0000535 unsigned int cpu_arch = cpu_architecture();
Russell Kingefe90d22006-12-08 15:22:20 +0000536
Catalin Marinasc98929c2007-11-22 18:32:01 +0100537 if (cpu_arch >= CPU_ARCH_ARMv6)
538 vfp_enable(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /*
541 * First check that there is a VFP that we can use.
542 * The handler is already setup to just log calls, so
543 * we just need to read the VFPSID register.
544 */
Russell King5d4cae52007-06-10 12:22:20 +0100545 vfp_vector = vfp_testing_entry;
Tzachi Perelsteinb9338a72007-09-09 14:24:59 +0100546 barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 vfpsid = fmrx(FPSID);
Russell King8e140362007-01-02 23:40:30 +0000548 barrier();
Russell King5d4cae52007-06-10 12:22:20 +0100549 vfp_vector = vfp_null_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 printk(KERN_INFO "VFP support v0.3: ");
Catalin Marinasc98929c2007-11-22 18:32:01 +0100552 if (VFP_arch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 printk("not present\n");
Catalin Marinasc98929c2007-11-22 18:32:01 +0100554 else if (vfpsid & FPSID_NODOUBLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 printk("no double precision support\n");
556 } else {
Russell King90b44192010-12-18 10:59:49 +0000557 hotcpu_notifier(vfp_hotplug, 0);
558
Jens Axboe8691e5a2008-06-06 11:18:06 +0200559 smp_call_function(vfp_enable, NULL, 1);
Russell King8e140362007-01-02 23:40:30 +0000560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT; /* Extract the architecture version */
562 printk("implementor %02x architecture %d part %02x variant %x rev %x\n",
563 (vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
564 (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
565 (vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
566 (vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
567 (vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
Russell Kingefe90d22006-12-08 15:22:20 +0000568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 vfp_vector = vfp_support_entry;
Russell Kingd6551e82006-06-21 13:31:52 +0100570
571 thread_register_notifier(&vfp_notifier_block);
Ben Dooksfc0b7a22008-12-18 12:26:54 +0100572 vfp_pm_init();
Russell Kingefe90d22006-12-08 15:22:20 +0000573
574 /*
575 * We detected VFP, and the support code is
576 * in place; report VFP support to userspace.
577 */
578 elf_hwcap |= HWCAP_VFP;
Catalin Marinas7279dc32009-02-11 13:13:56 +0100579#ifdef CONFIG_VFPv3
Catalin Marinas325ffc32010-03-26 15:44:57 +0100580 if (VFP_arch >= 2) {
Catalin Marinas7279dc32009-02-11 13:13:56 +0100581 elf_hwcap |= HWCAP_VFPv3;
582
583 /*
584 * Check for VFPv3 D16. CPUs in this configuration
585 * only have 16 x 64bit registers.
586 */
587 if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1)
588 elf_hwcap |= HWCAP_VFPv3D16;
589 }
590#endif
Catalin Marinas2bedbdf2008-11-06 13:23:07 +0000591#ifdef CONFIG_NEON
592 /*
593 * Check for the presence of the Advanced SIMD
594 * load/store instructions, integer and single
Tony Lindgren5aaf2542010-07-01 13:41:05 +0100595 * precision floating point operations. Only check
596 * for NEON if the hardware has the MVFR registers.
Catalin Marinas2bedbdf2008-11-06 13:23:07 +0000597 */
Tony Lindgren5aaf2542010-07-01 13:41:05 +0100598 if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
599 if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
600 elf_hwcap |= HWCAP_NEON;
601 }
Catalin Marinas2bedbdf2008-11-06 13:23:07 +0000602#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
604 return 0;
605}
606
607late_initcall(vfp_init);