vfpmodule.c 18.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 *  linux/arch/arm/vfp/vfpmodule.c
 *
 *  Copyright (C) 2004 ARM Limited.
 *  Written by Deep Blue Solutions Limited.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#include <linux/types.h>
12
#include <linux/cpu.h>
13
#include <linux/cpu_pm.h>
14
#include <linux/hardirq.h>
L
Linus Torvalds 已提交
15
#include <linux/kernel.h>
16
#include <linux/notifier.h>
L
Linus Torvalds 已提交
17 18
#include <linux/signal.h>
#include <linux/sched.h>
19
#include <linux/smp.h>
L
Linus Torvalds 已提交
20
#include <linux/init.h>
21 22
#include <linux/uaccess.h>
#include <linux/user.h>
23

24
#include <asm/cp15.h>
25
#include <asm/cputype.h>
26
#include <asm/system_info.h>
27
#include <asm/thread_notify.h>
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37
#include <asm/vfp.h>

#include "vfpinstr.h"
#include "vfp.h"

/*
 * Our undef handlers (in entry.S)
 */
void vfp_testing_entry(void);
void vfp_support_entry(void);
38
void vfp_null_entry(void);
L
Linus Torvalds 已提交
39

40
void (*vfp_vector)(void) = vfp_null_entry;
41

42 43 44 45 46 47 48
/*
 * Dual-use variable.
 * Used in startup: set to non-zero if VFP checks fail
 * After startup, holds VFP architecture
 */
unsigned int VFP_arch;

49 50 51 52
/*
 * The pointer to the vfpstate structure of the thread which currently
 * owns the context held in the VFP hardware, or NULL if the hardware
 * context is invalid.
53 54 55 56
 *
 * For UP, this is sufficient to tell which thread owns the VFP context.
 * However, for SMP, we also need to check the CPU number stored in the
 * saved state too to catch migrations.
57 58
 */
union vfp_state *vfp_current_hw_state[NR_CPUS];
L
Linus Torvalds 已提交
59 60

/*
61 62
 * Is 'thread's most up to date state stored in this CPUs hardware?
 * Must be called from non-preemptible context.
L
Linus Torvalds 已提交
63
 */
64 65 66 67 68 69 70 71 72 73 74 75
static bool vfp_state_in_hw(unsigned int cpu, struct thread_info *thread)
{
#ifdef CONFIG_SMP
	if (thread->vfpstate.hard.cpu != cpu)
		return false;
#endif
	return vfp_current_hw_state[cpu] == &thread->vfpstate;
}

/*
 * Force a reload of the VFP context from the thread structure.  We do
 * this by ensuring that access to the VFP hardware is disabled, and
76
 * clear vfp_current_hw_state.  Must be called from non-preemptible context.
77 78 79 80 81 82 83 84 85 86 87
 */
static void vfp_force_reload(unsigned int cpu, struct thread_info *thread)
{
	if (vfp_state_in_hw(cpu, thread)) {
		fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
		vfp_current_hw_state[cpu] = NULL;
	}
#ifdef CONFIG_SMP
	thread->vfpstate.hard.cpu = NR_CPUS;
#endif
}
L
Linus Torvalds 已提交
88

89 90 91 92 93 94 95 96 97 98
/*
 * Per-thread VFP initialization.
 */
static void vfp_thread_flush(struct thread_info *thread)
{
	union vfp_state *vfp = &thread->vfpstate;
	unsigned int cpu;

	/*
	 * Disable VFP to ensure we initialize it first.  We must ensure
99 100 101 102 103
	 * that the modification of vfp_current_hw_state[] and hardware
	 * disable are done for the same CPU and without preemption.
	 *
	 * Do this first to ensure that preemption won't overwrite our
	 * state saving should access to the VFP be enabled at this point.
104 105
	 */
	cpu = get_cpu();
106 107
	if (vfp_current_hw_state[cpu] == vfp)
		vfp_current_hw_state[cpu] = NULL;
108 109
	fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
	put_cpu();
110 111 112 113 114 115 116 117

	memset(vfp, 0, sizeof(union vfp_state));

	vfp->hard.fpexc = FPEXC_EN;
	vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
#ifdef CONFIG_SMP
	vfp->hard.cpu = NR_CPUS;
#endif
118 119
}

120
static void vfp_thread_exit(struct thread_info *thread)
121 122 123
{
	/* release case: Per-thread VFP cleanup. */
	union vfp_state *vfp = &thread->vfpstate;
124
	unsigned int cpu = get_cpu();
125

126 127
	if (vfp_current_hw_state[cpu] == vfp)
		vfp_current_hw_state[cpu] = NULL;
128
	put_cpu();
129 130
}

131 132 133 134 135 136
static void vfp_thread_copy(struct thread_info *thread)
{
	struct thread_info *parent = current_thread_info();

	vfp_sync_hwstate(parent);
	thread->vfpstate = parent->vfpstate;
137 138 139
#ifdef CONFIG_SMP
	thread->vfpstate.hard.cpu = NR_CPUS;
#endif
140 141
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
/*
 * When this function is called with the following 'cmd's, the following
 * is true while this function is being run:
 *  THREAD_NOFTIFY_SWTICH:
 *   - the previously running thread will not be scheduled onto another CPU.
 *   - the next thread to be run (v) will not be running on another CPU.
 *   - thread->cpu is the local CPU number
 *   - not preemptible as we're called in the middle of a thread switch
 *  THREAD_NOTIFY_FLUSH:
 *   - the thread (v) will be running on the local CPU, so
 *	v === current_thread_info()
 *   - thread->cpu is the local CPU number at the time it is accessed,
 *	but may change at any time.
 *   - we could be preempted if tree preempt rcu is enabled, so
 *	it is unsafe to use thread->cpu.
157 158 159 160 161 162 163
 *  THREAD_NOTIFY_EXIT
 *   - the thread (v) will be running on the local CPU, so
 *	v === current_thread_info()
 *   - thread->cpu is the local CPU number at the time it is accessed,
 *	but may change at any time.
 *   - we could be preempted if tree preempt rcu is enabled, so
 *	it is unsafe to use thread->cpu.
164
 */
165
static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
L
Linus Torvalds 已提交
166
{
167
	struct thread_info *thread = v;
168 169 170 171
	u32 fpexc;
#ifdef CONFIG_SMP
	unsigned int cpu;
#endif
L
Linus Torvalds 已提交
172

173 174 175
	switch (cmd) {
	case THREAD_NOTIFY_SWITCH:
		fpexc = fmrx(FPEXC);
176 177

#ifdef CONFIG_SMP
178
		cpu = thread->cpu;
179

180 181 182 183 184
		/*
		 * On SMP, if VFP is enabled, save the old state in
		 * case the thread migrates to a different CPU. The
		 * restoring is done lazily.
		 */
185
		if ((fpexc & FPEXC_EN) && vfp_current_hw_state[cpu])
186
			vfp_save_state(vfp_current_hw_state[cpu], fpexc);
187 188
#endif

189 190 191 192
		/*
		 * Always disable VFP so we can lazily save/restore the
		 * old state.
		 */
193
		fmxr(FPEXC, fpexc & ~FPEXC_EN);
194
		break;
195

196
	case THREAD_NOTIFY_FLUSH:
197
		vfp_thread_flush(thread);
198 199 200
		break;

	case THREAD_NOTIFY_EXIT:
201
		vfp_thread_exit(thread);
202 203 204 205
		break;

	case THREAD_NOTIFY_COPY:
		vfp_thread_copy(thread);
206 207
		break;
	}
208

209
	return NOTIFY_DONE;
L
Linus Torvalds 已提交
210 211
}

212 213 214 215
static struct notifier_block vfp_notifier_block = {
	.notifier_call	= vfp_notifier,
};

L
Linus Torvalds 已提交
216 217 218 219
/*
 * Raise a SIGFPE for the current process.
 * sicode describes the signal being raised.
 */
220
static void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
L
Linus Torvalds 已提交
221 222 223 224 225 226 227
{
	siginfo_t info;

	memset(&info, 0, sizeof(info));

	info.si_signo = SIGFPE;
	info.si_code = sicode;
A
Al Viro 已提交
228
	info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
L
Linus Torvalds 已提交
229 230 231 232 233 234 235 236

	/*
	 * This is the same as NWFPE, because it's not clear what
	 * this is used for
	 */
	current->thread.error_code = 0;
	current->thread.trap_no = 6;

R
Russell King 已提交
237
	send_sig_info(SIGFPE, &info, current);
L
Linus Torvalds 已提交
238 239
}

240
static void vfp_panic(char *reason, u32 inst)
L
Linus Torvalds 已提交
241 242 243 244 245
{
	int i;

	printk(KERN_ERR "VFP: Error: %s\n", reason);
	printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
246
		fmrx(FPEXC), fmrx(FPSCR), inst);
L
Linus Torvalds 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260
	for (i = 0; i < 32; i += 2)
		printk(KERN_ERR "VFP: s%2u: 0x%08x s%2u: 0x%08x\n",
		       i, vfp_get_float(i), i+1, vfp_get_float(i+1));
}

/*
 * Process bitmask of exception conditions.
 */
static void vfp_raise_exceptions(u32 exceptions, u32 inst, u32 fpscr, struct pt_regs *regs)
{
	int si_code = 0;

	pr_debug("VFP: raising exceptions %08x\n", exceptions);

261
	if (exceptions == VFP_EXCEPTION_ERROR) {
262
		vfp_panic("unhandled bounce", inst);
L
Linus Torvalds 已提交
263 264 265 266 267
		vfp_raise_sigfpe(0, regs);
		return;
	}

	/*
268
	 * If any of the status flags are set, update the FPSCR.
L
Linus Torvalds 已提交
269 270 271
	 * Comparison instructions always return at least one of
	 * these flags set.
	 */
272 273 274
	if (exceptions & (FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V))
		fpscr &= ~(FPSCR_N|FPSCR_Z|FPSCR_C|FPSCR_V);

L
Linus Torvalds 已提交
275 276 277 278 279 280 281 282 283 284 285
	fpscr |= exceptions;

	fmxr(FPSCR, fpscr);

#define RAISE(stat,en,sig)				\
	if (exceptions & stat && fpscr & en)		\
		si_code = sig;

	/*
	 * These are arranged in priority order, least to highest.
	 */
286
	RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
L
Linus Torvalds 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300
	RAISE(FPSCR_IXC, FPSCR_IXE, FPE_FLTRES);
	RAISE(FPSCR_UFC, FPSCR_UFE, FPE_FLTUND);
	RAISE(FPSCR_OFC, FPSCR_OFE, FPE_FLTOVF);
	RAISE(FPSCR_IOC, FPSCR_IOE, FPE_FLTINV);

	if (si_code)
		vfp_raise_sigfpe(si_code, regs);
}

/*
 * Emulate a VFP instruction.
 */
static u32 vfp_emulate_instruction(u32 inst, u32 fpscr, struct pt_regs *regs)
{
301
	u32 exceptions = VFP_EXCEPTION_ERROR;
L
Linus Torvalds 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

	pr_debug("VFP: emulate: INST=0x%08x SCR=0x%08x\n", inst, fpscr);

	if (INST_CPRTDO(inst)) {
		if (!INST_CPRT(inst)) {
			/*
			 * CPDO
			 */
			if (vfp_single(inst)) {
				exceptions = vfp_single_cpdo(inst, fpscr);
			} else {
				exceptions = vfp_double_cpdo(inst, fpscr);
			}
		} else {
			/*
			 * A CPRT instruction can not appear in FPINST2, nor
			 * can it cause an exception.  Therefore, we do not
			 * have to emulate it.
			 */
		}
	} else {
		/*
		 * A CPDT instruction can not appear in FPINST2, nor can
		 * it cause an exception.  Therefore, we do not have to
		 * emulate it.
		 */
	}
329
	return exceptions & ~VFP_NAN_FLAG;
L
Linus Torvalds 已提交
330 331 332 333 334
}

/*
 * Package up a bounce condition.
 */
335
void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
L
Linus Torvalds 已提交
336
{
337
	u32 fpscr, orig_fpscr, fpsid, exceptions;
L
Linus Torvalds 已提交
338 339 340 341

	pr_debug("VFP: bounce: trigger %08x fpexc %08x\n", trigger, fpexc);

	/*
342 343 344 345 346 347 348 349 350 351 352
	 * At this point, FPEXC can have the following configuration:
	 *
	 *  EX DEX IXE
	 *  0   1   x   - synchronous exception
	 *  1   x   0   - asynchronous exception
	 *  1   x   1   - sychronous on VFP subarch 1 and asynchronous on later
	 *  0   0   1   - synchronous on VFP9 (non-standard subarch 1
	 *                implementation), undefined otherwise
	 *
	 * Clear various bits and enable access to the VFP so we can
	 * handle the bounce.
L
Linus Torvalds 已提交
353
	 */
354
	fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
L
Linus Torvalds 已提交
355

356
	fpsid = fmrx(FPSID);
L
Linus Torvalds 已提交
357 358 359
	orig_fpscr = fpscr = fmrx(FPSCR);

	/*
360
	 * Check for the special VFP subarch 1 and FPSCR.IXE bit case
L
Linus Torvalds 已提交
361
	 */
362 363 364 365 366
	if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
	    && (fpscr & FPSCR_IXE)) {
		/*
		 * Synchronous exception, emulate the trigger instruction
		 */
L
Linus Torvalds 已提交
367 368 369
		goto emulate;
	}

370
	if (fpexc & FPEXC_EX) {
371
#ifndef CONFIG_CPU_FEROCEON
372 373 374 375 376 377
		/*
		 * Asynchronous exception. The instruction is read from FPINST
		 * and the interrupted instruction has to be restarted.
		 */
		trigger = fmrx(FPINST);
		regs->ARM_pc -= 4;
378
#endif
379 380 381 382 383 384 385
	} else if (!(fpexc & FPEXC_DEX)) {
		/*
		 * Illegal combination of bits. It can be caused by an
		 * unallocated VFP instruction but with FPSCR.IXE set and not
		 * on VFP subarch 1.
		 */
		 vfp_raise_exceptions(VFP_EXCEPTION_ERROR, trigger, fpscr, regs);
386
		goto exit;
387
	}
L
Linus Torvalds 已提交
388 389

	/*
390 391 392
	 * Modify fpscr to indicate the number of iterations remaining.
	 * If FPEXC.EX is 0, FPEXC.DEX is 1 and the FPEXC.VV bit indicates
	 * whether FPEXC.VECITR or FPSCR.LEN is used.
L
Linus Torvalds 已提交
393
	 */
394
	if (fpexc & (FPEXC_EX | FPEXC_VV)) {
L
Linus Torvalds 已提交
395 396 397 398 399 400 401 402 403 404 405 406 407
		u32 len;

		len = fpexc + (1 << FPEXC_LENGTH_BIT);

		fpscr &= ~FPSCR_LENGTH_MASK;
		fpscr |= (len & FPEXC_LENGTH_MASK) << (FPSCR_LENGTH_BIT - FPEXC_LENGTH_BIT);
	}

	/*
	 * Handle the first FP instruction.  We used to take note of the
	 * FPEXC bounce reason, but this appears to be unreliable.
	 * Emulate the bounced instruction instead.
	 */
408
	exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
L
Linus Torvalds 已提交
409
	if (exceptions)
410
		vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
L
Linus Torvalds 已提交
411 412

	/*
413 414
	 * If there isn't a second FP instruction, exit now. Note that
	 * the FPEXC.FP2V bit is valid only if FPEXC.EX is 1.
L
Linus Torvalds 已提交
415
	 */
416
	if (fpexc ^ (FPEXC_EX | FPEXC_FP2V))
417
		goto exit;
L
Linus Torvalds 已提交
418 419 420 421 422 423 424 425 426

	/*
	 * The barrier() here prevents fpinst2 being read
	 * before the condition above.
	 */
	barrier();
	trigger = fmrx(FPINST2);

 emulate:
427
	exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
L
Linus Torvalds 已提交
428 429
	if (exceptions)
		vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
430 431
 exit:
	preempt_enable();
L
Linus Torvalds 已提交
432
}
433

434 435
static void vfp_enable(void *unused)
{
436 437 438 439
	u32 access;

	BUG_ON(preemptible());
	access = get_copro_access();
440 441 442 443 444 445 446

	/*
	 * Enable full access to VFP (cp10 and cp11)
	 */
	set_copro_access(access | CPACC_FULL(10) | CPACC_FULL(11));
}

447
#ifdef CONFIG_CPU_PM
448
static int vfp_pm_suspend(void)
449 450 451 452 453 454 455 456 457 458 459 460 461 462
{
	struct thread_info *ti = current_thread_info();
	u32 fpexc = fmrx(FPEXC);

	/* if vfp is on, then save state for resumption */
	if (fpexc & FPEXC_EN) {
		printk(KERN_DEBUG "%s: saving vfp state\n", __func__);
		vfp_save_state(&ti->vfpstate, fpexc);

		/* disable, just in case */
		fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
	}

	/* clear any information we had about last context state */
463
	memset(vfp_current_hw_state, 0, sizeof(vfp_current_hw_state));
464 465 466 467

	return 0;
}

468
static void vfp_pm_resume(void)
469 470 471 472 473 474 475 476
{
	/* ensure we have access to the vfp */
	vfp_enable(NULL);

	/* and disable it to ensure the next usage restores the state */
	fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
}

477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
static int vfp_cpu_pm_notifier(struct notifier_block *self, unsigned long cmd,
	void *v)
{
	switch (cmd) {
	case CPU_PM_ENTER:
		vfp_pm_suspend();
		break;
	case CPU_PM_ENTER_FAILED:
	case CPU_PM_EXIT:
		vfp_pm_resume();
		break;
	}
	return NOTIFY_OK;
}

static struct notifier_block vfp_cpu_pm_notifier_block = {
	.notifier_call = vfp_cpu_pm_notifier,
494 495 496 497
};

static void vfp_pm_init(void)
{
498
	cpu_pm_register_notifier(&vfp_cpu_pm_notifier_block);
499 500 501 502
}

#else
static inline void vfp_pm_init(void) { }
503
#endif /* CONFIG_CPU_PM */
504

505 506 507 508
/*
 * Ensure that the VFP state stored in 'thread->vfpstate' is up to date
 * with the hardware state.
 */
509
void vfp_sync_hwstate(struct thread_info *thread)
510 511 512
{
	unsigned int cpu = get_cpu();

513
	if (vfp_state_in_hw(cpu, thread)) {
R
Russell King 已提交
514
		u32 fpexc = fmrx(FPEXC);
515

R
Russell King 已提交
516 517 518 519 520
		/*
		 * Save the last VFP state on this CPU.
		 */
		fmxr(FPEXC, fpexc | FPEXC_EN);
		vfp_save_state(&thread->vfpstate, fpexc | FPEXC_EN);
521 522
		fmxr(FPEXC, fpexc);
	}
523

524 525 526
	put_cpu();
}

527
/* Ensure that the thread reloads the hardware VFP state on the next use. */
528 529 530
void vfp_flush_hwstate(struct thread_info *thread)
{
	unsigned int cpu = get_cpu();
531

532
	vfp_force_reload(cpu, thread);
533

534 535 536
	put_cpu();
}

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
/*
 * Save the current VFP state into the provided structures and prepare
 * for entry into a new function (signal handler).
 */
int vfp_preserve_user_clear_hwstate(struct user_vfp __user *ufp,
				    struct user_vfp_exc __user *ufp_exc)
{
	struct thread_info *thread = current_thread_info();
	struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
	int err = 0;

	/* Ensure that the saved hwstate is up-to-date. */
	vfp_sync_hwstate(thread);

	/*
	 * Copy the floating point registers. There can be unused
	 * registers see asm/hwcap.h for details.
	 */
	err |= __copy_to_user(&ufp->fpregs, &hwstate->fpregs,
			      sizeof(hwstate->fpregs));
	/*
	 * Copy the status and control register.
	 */
	__put_user_error(hwstate->fpscr, &ufp->fpscr, err);

	/*
	 * Copy the exception registers.
	 */
	__put_user_error(hwstate->fpexc, &ufp_exc->fpexc, err);
	__put_user_error(hwstate->fpinst, &ufp_exc->fpinst, err);
	__put_user_error(hwstate->fpinst2, &ufp_exc->fpinst2, err);

	if (err)
		return -EFAULT;
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

	/* Ensure that VFP is disabled. */
	vfp_flush_hwstate(thread);

	/*
	 * As per the PCS, clear the length and stride bits for function
	 * entry.
	 */
	hwstate->fpscr &= ~(FPSCR_LENGTH_MASK | FPSCR_STRIDE_MASK);

	/*
	 * Disable VFP in the hwstate so that we can detect if it gets
	 * used.
	 */
	hwstate->fpexc &= ~FPEXC_EN;
586 587 588 589 590 591 592 593 594 595 596 597
	return 0;
}

/* Sanitise and restore the current VFP state from the provided structures. */
int vfp_restore_user_hwstate(struct user_vfp __user *ufp,
			     struct user_vfp_exc __user *ufp_exc)
{
	struct thread_info *thread = current_thread_info();
	struct vfp_hard_struct *hwstate = &thread->vfpstate.hard;
	unsigned long fpexc;
	int err = 0;

598 599 600 601 602 603
	/*
	 * If VFP has been used, then disable it to avoid corrupting
	 * the new thread state.
	 */
	if (hwstate->fpexc & FPEXC_EN)
		vfp_flush_hwstate(thread);
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

	/*
	 * Copy the floating point registers. There can be unused
	 * registers see asm/hwcap.h for details.
	 */
	err |= __copy_from_user(&hwstate->fpregs, &ufp->fpregs,
				sizeof(hwstate->fpregs));
	/*
	 * Copy the status and control register.
	 */
	__get_user_error(hwstate->fpscr, &ufp->fpscr, err);

	/*
	 * Sanitise and restore the exception registers.
	 */
	__get_user_error(fpexc, &ufp_exc->fpexc, err);

	/* Ensure the VFP is enabled. */
	fpexc |= FPEXC_EN;

	/* Ensure FPINST2 is invalid and the exception flag is cleared. */
	fpexc &= ~(FPEXC_EX | FPEXC_FP2V);
	hwstate->fpexc = fpexc;

	__get_user_error(hwstate->fpinst, &ufp_exc->fpinst, err);
	__get_user_error(hwstate->fpinst2, &ufp_exc->fpinst2, err);

	return err ? -EFAULT : 0;
}

634 635
/*
 * VFP hardware can lose all context when a CPU goes offline.
636 637 638 639 640
 * As we will be running in SMP mode with CPU hotplug, we will save the
 * hardware state at every thread switch.  We clear our held state when
 * a CPU has been killed, indicating that the VFP hardware doesn't contain
 * a threads VFP state.  When a CPU starts up, we re-enable access to the
 * VFP hardware.
641 642 643 644 645 646 647 648
 *
 * Both CPU_DYING and CPU_STARTING are called on the CPU which
 * is being offlined/onlined.
 */
static int vfp_hotplug(struct notifier_block *b, unsigned long action,
	void *hcpu)
{
	if (action == CPU_DYING || action == CPU_DYING_FROZEN) {
649
		vfp_force_reload((long)hcpu, current_thread_info());
650 651 652 653
	} else if (action == CPU_STARTING || action == CPU_STARTING_FROZEN)
		vfp_enable(NULL);
	return NOTIFY_OK;
}
654

L
Linus Torvalds 已提交
655 656 657 658 659 660
/*
 * VFP support code initialisation.
 */
static int __init vfp_init(void)
{
	unsigned int vfpsid;
661 662
	unsigned int cpu_arch = cpu_architecture();

663
	if (cpu_arch >= CPU_ARCH_ARMv6)
664
		on_each_cpu(vfp_enable, NULL, 1);
L
Linus Torvalds 已提交
665 666 667 668 669 670

	/*
	 * First check that there is a VFP that we can use.
	 * The handler is already setup to just log calls, so
	 * we just need to read the VFPSID register.
	 */
671
	vfp_vector = vfp_testing_entry;
672
	barrier();
L
Linus Torvalds 已提交
673
	vfpsid = fmrx(FPSID);
674
	barrier();
675
	vfp_vector = vfp_null_entry;
L
Linus Torvalds 已提交
676 677

	printk(KERN_INFO "VFP support v0.3: ");
678
	if (VFP_arch)
L
Linus Torvalds 已提交
679
		printk("not present\n");
680
	else if (vfpsid & FPSID_NODOUBLE) {
L
Linus Torvalds 已提交
681 682
		printk("no double precision support\n");
	} else {
683 684
		hotcpu_notifier(vfp_hotplug, 0);

L
Linus Torvalds 已提交
685 686 687 688 689 690 691
		VFP_arch = (vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT;  /* Extract the architecture version */
		printk("implementor %02x architecture %d part %02x variant %x rev %x\n",
			(vfpsid & FPSID_IMPLEMENTER_MASK) >> FPSID_IMPLEMENTER_BIT,
			(vfpsid & FPSID_ARCH_MASK) >> FPSID_ARCH_BIT,
			(vfpsid & FPSID_PART_MASK) >> FPSID_PART_BIT,
			(vfpsid & FPSID_VARIANT_MASK) >> FPSID_VARIANT_BIT,
			(vfpsid & FPSID_REV_MASK) >> FPSID_REV_BIT);
692

L
Linus Torvalds 已提交
693
		vfp_vector = vfp_support_entry;
694 695

		thread_register_notifier(&vfp_notifier_block);
696
		vfp_pm_init();
697 698 699 700 701 702

		/*
		 * We detected VFP, and the support code is
		 * in place; report VFP support to userspace.
		 */
		elf_hwcap |= HWCAP_VFP;
703
#ifdef CONFIG_VFPv3
704
		if (VFP_arch >= 2) {
705 706 707 708 709 710 711 712 713 714
			elf_hwcap |= HWCAP_VFPv3;

			/*
			 * Check for VFPv3 D16. CPUs in this configuration
			 * only have 16 x 64bit registers.
			 */
			if (((fmrx(MVFR0) & MVFR0_A_SIMD_MASK)) == 1)
				elf_hwcap |= HWCAP_VFPv3D16;
		}
#endif
715 716 717
		/*
		 * Check for the presence of the Advanced SIMD
		 * load/store instructions, integer and single
718 719
		 * precision floating point operations. Only check
		 * for NEON if the hardware has the MVFR registers.
720
		 */
721
		if ((read_cpuid_id() & 0x000f0000) == 0x000f0000) {
722
#ifdef CONFIG_NEON
723 724
			if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
				elf_hwcap |= HWCAP_NEON;
725
#endif
726 727 728
			if ((fmrx(MVFR1) & 0xf0000000) == 0x10000000)
				elf_hwcap |= HWCAP_VFPv4;
		}
L
Linus Torvalds 已提交
729 730 731 732 733
	}
	return 0;
}

late_initcall(vfp_init);