vfpmodule.c 11.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 *  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/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/init.h>
17 18

#include <asm/thread_notify.h>
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28
#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);
29
void vfp_null_entry(void);
L
Linus Torvalds 已提交
30

31
void (*vfp_vector)(void) = vfp_null_entry;
32
union vfp_state *last_VFP_context[NR_CPUS];
L
Linus Torvalds 已提交
33 34 35 36 37 38 39 40

/*
 * Dual-use variable.
 * Used in startup: set to non-zero if VFP checks fail
 * After startup, holds VFP architecture
 */
unsigned int VFP_arch;

41
static int vfp_notifier(struct notifier_block *self, unsigned long cmd, void *v)
L
Linus Torvalds 已提交
42
{
43
	struct thread_info *thread = v;
44
	union vfp_state *vfp;
45
	__u32 cpu = thread->cpu;
L
Linus Torvalds 已提交
46

47
	if (likely(cmd == THREAD_NOTIFY_SWITCH)) {
48 49 50 51 52 53 54 55
		u32 fpexc = fmrx(FPEXC);

#ifdef CONFIG_SMP
		/*
		 * On SMP, if VFP is enabled, save the old state in
		 * case the thread migrates to a different CPU. The
		 * restoring is done lazily.
		 */
56
		if ((fpexc & FPEXC_EN) && last_VFP_context[cpu]) {
57 58 59 60 61 62 63 64 65 66 67 68
			vfp_save_state(last_VFP_context[cpu], fpexc);
			last_VFP_context[cpu]->hard.cpu = cpu;
		}
		/*
		 * Thread migration, just force the reloading of the
		 * state on the new CPU in case the VFP registers
		 * contain stale data.
		 */
		if (thread->vfpstate.hard.cpu != cpu)
			last_VFP_context[cpu] = NULL;
#endif

69 70 71 72
		/*
		 * Always disable VFP so we can lazily save/restore the
		 * old state.
		 */
73
		fmxr(FPEXC, fpexc & ~FPEXC_EN);
74 75 76 77 78
		return NOTIFY_DONE;
	}

	vfp = &thread->vfpstate;
	if (cmd == THREAD_NOTIFY_FLUSH) {
79 80 81 82
		/*
		 * Per-thread VFP initialisation.
		 */
		memset(vfp, 0, sizeof(union vfp_state));
L
Linus Torvalds 已提交
83

84
		vfp->hard.fpexc = FPEXC_EN;
85
		vfp->hard.fpscr = FPSCR_ROUND_NEAREST;
L
Linus Torvalds 已提交
86

87 88 89
		/*
		 * Disable VFP to ensure we initialise it first.
		 */
90
		fmxr(FPEXC, fmrx(FPEXC) & ~FPEXC_EN);
91 92
	}

93
	/* flush and release case: Per-thread VFP cleanup. */
94 95
	if (last_VFP_context[cpu] == vfp)
		last_VFP_context[cpu] = NULL;
96

97
	return NOTIFY_DONE;
L
Linus Torvalds 已提交
98 99
}

100 101 102 103
static struct notifier_block vfp_notifier_block = {
	.notifier_call	= vfp_notifier,
};

L
Linus Torvalds 已提交
104 105 106 107 108 109 110 111 112 113 114 115
/*
 * Raise a SIGFPE for the current process.
 * sicode describes the signal being raised.
 */
void vfp_raise_sigfpe(unsigned int sicode, struct pt_regs *regs)
{
	siginfo_t info;

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

	info.si_signo = SIGFPE;
	info.si_code = sicode;
A
Al Viro 已提交
116
	info.si_addr = (void __user *)(instruction_pointer(regs) - 4);
L
Linus Torvalds 已提交
117 118 119 120 121 122 123 124

	/*
	 * 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 已提交
125
	send_sig_info(SIGFPE, &info, current);
L
Linus Torvalds 已提交
126 127
}

128
static void vfp_panic(char *reason, u32 inst)
L
Linus Torvalds 已提交
129 130 131 132 133
{
	int i;

	printk(KERN_ERR "VFP: Error: %s\n", reason);
	printk(KERN_ERR "VFP: EXC 0x%08x SCR 0x%08x INST 0x%08x\n",
134
		fmrx(FPEXC), fmrx(FPSCR), inst);
L
Linus Torvalds 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
	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);

149
	if (exceptions == VFP_EXCEPTION_ERROR) {
150
		vfp_panic("unhandled bounce", inst);
L
Linus Torvalds 已提交
151 152 153 154 155
		vfp_raise_sigfpe(0, regs);
		return;
	}

	/*
156
	 * Update the FPSCR with the additional exception flags.
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169 170
	 * Comparison instructions always return at least one of
	 * these flags set.
	 */
	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.
	 */
171
	RAISE(FPSCR_DZC, FPSCR_DZE, FPE_FLTDIV);
L
Linus Torvalds 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185
	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)
{
186
	u32 exceptions = VFP_EXCEPTION_ERROR;
L
Linus Torvalds 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

	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.
		 */
	}
214
	return exceptions & ~VFP_NAN_FLAG;
L
Linus Torvalds 已提交
215 216 217 218 219
}

/*
 * Package up a bounce condition.
 */
220
void VFP_bounce(u32 trigger, u32 fpexc, struct pt_regs *regs)
L
Linus Torvalds 已提交
221
{
222
	u32 fpscr, orig_fpscr, fpsid, exceptions;
L
Linus Torvalds 已提交
223 224 225 226

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

	/*
227 228 229 230 231 232 233 234 235 236 237
	 * 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 已提交
238
	 */
239
	fmxr(FPEXC, fpexc & ~(FPEXC_EX|FPEXC_DEX|FPEXC_FP2V|FPEXC_VV|FPEXC_TRAP_MASK));
L
Linus Torvalds 已提交
240

241
	fpsid = fmrx(FPSID);
L
Linus Torvalds 已提交
242 243 244
	orig_fpscr = fpscr = fmrx(FPSCR);

	/*
245
	 * Check for the special VFP subarch 1 and FPSCR.IXE bit case
L
Linus Torvalds 已提交
246
	 */
247 248 249 250 251
	if ((fpsid & FPSID_ARCH_MASK) == (1 << FPSID_ARCH_BIT)
	    && (fpscr & FPSCR_IXE)) {
		/*
		 * Synchronous exception, emulate the trigger instruction
		 */
L
Linus Torvalds 已提交
252 253 254
		goto emulate;
	}

255 256 257 258 259 260 261 262 263 264 265 266 267 268
	if (fpexc & FPEXC_EX) {
		/*
		 * Asynchronous exception. The instruction is read from FPINST
		 * and the interrupted instruction has to be restarted.
		 */
		trigger = fmrx(FPINST);
		regs->ARM_pc -= 4;
	} 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);
269
		goto exit;
270
	}
L
Linus Torvalds 已提交
271 272

	/*
273 274 275
	 * 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 已提交
276
	 */
277
	if (fpexc & (FPEXC_EX | FPEXC_VV)) {
L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290
		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.
	 */
291
	exceptions = vfp_emulate_instruction(trigger, fpscr, regs);
L
Linus Torvalds 已提交
292
	if (exceptions)
293
		vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
L
Linus Torvalds 已提交
294 295

	/*
296 297
	 * 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 已提交
298
	 */
299
	if (fpexc ^ (FPEXC_EX | FPEXC_FP2V))
300
		goto exit;
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309

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

 emulate:
310
	exceptions = vfp_emulate_instruction(trigger, orig_fpscr, regs);
L
Linus Torvalds 已提交
311 312
	if (exceptions)
		vfp_raise_exceptions(exceptions, trigger, orig_fpscr, regs);
313 314
 exit:
	preempt_enable();
L
Linus Torvalds 已提交
315
}
316

317 318 319 320 321 322 323 324 325 326
static void vfp_enable(void *unused)
{
	u32 access = get_copro_access();

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

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
#ifdef CONFIG_PM
#include <linux/sysdev.h>

static int vfp_pm_suspend(struct sys_device *dev, pm_message_t state)
{
	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 */
	memset(last_VFP_context, 0, sizeof(last_VFP_context));

	return 0;
}

static int vfp_pm_resume(struct sys_device *dev)
{
	/* 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);

	return 0;
}

static struct sysdev_class vfp_pm_sysclass = {
	.name		= "vfp",
	.suspend	= vfp_pm_suspend,
	.resume		= vfp_pm_resume,
};

static struct sys_device vfp_pm_sysdev = {
	.cls	= &vfp_pm_sysclass,
};

static void vfp_pm_init(void)
{
	sysdev_class_register(&vfp_pm_sysclass);
	sysdev_register(&vfp_pm_sysdev);
}


#else
static inline void vfp_pm_init(void) { }
#endif /* CONFIG_PM */

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
/*
 * Synchronise the hardware VFP state of a thread other than current with the
 * saved one. This function is used by the ptrace mechanism.
 */
#ifdef CONFIG_SMP
void vfp_sync_state(struct thread_info *thread)
{
	/*
	 * On SMP systems, the VFP state is automatically saved at every
	 * context switch. We mark the thread VFP state as belonging to a
	 * non-existent CPU so that the saved one will be reloaded when
	 * needed.
	 */
	thread->vfpstate.hard.cpu = NR_CPUS;
}
#else
void vfp_sync_state(struct thread_info *thread)
{
	unsigned int cpu = get_cpu();
	u32 fpexc = fmrx(FPEXC);

	/*
	 * If VFP is enabled, the previous state was already saved and
	 * last_VFP_context updated.
	 */
	if (fpexc & FPEXC_EN)
		goto out;

	if (!last_VFP_context[cpu])
		goto out;

	/*
	 * Save the last VFP state on this CPU.
	 */
	fmxr(FPEXC, fpexc | FPEXC_EN);
	vfp_save_state(last_VFP_context[cpu], fpexc);
	fmxr(FPEXC, fpexc);

	/*
	 * Set the context to NULL to force a reload the next time the thread
	 * uses the VFP.
	 */
	last_VFP_context[cpu] = NULL;

out:
	put_cpu();
}
#endif

431 432
#include <linux/smp.h>

L
Linus Torvalds 已提交
433 434 435 436 437 438
/*
 * VFP support code initialisation.
 */
static int __init vfp_init(void)
{
	unsigned int vfpsid;
439 440
	unsigned int cpu_arch = cpu_architecture();

441 442
	if (cpu_arch >= CPU_ARCH_ARMv6)
		vfp_enable(NULL);
L
Linus Torvalds 已提交
443 444 445 446 447 448

	/*
	 * 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.
	 */
449
	vfp_vector = vfp_testing_entry;
450
	barrier();
L
Linus Torvalds 已提交
451
	vfpsid = fmrx(FPSID);
452
	barrier();
453
	vfp_vector = vfp_null_entry;
L
Linus Torvalds 已提交
454 455

	printk(KERN_INFO "VFP support v0.3: ");
456
	if (VFP_arch)
L
Linus Torvalds 已提交
457
		printk("not present\n");
458
	else if (vfpsid & FPSID_NODOUBLE) {
L
Linus Torvalds 已提交
459 460
		printk("no double precision support\n");
	} else {
461
		smp_call_function(vfp_enable, NULL, 1);
462

L
Linus Torvalds 已提交
463 464 465 466 467 468 469
		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);
470

L
Linus Torvalds 已提交
471
		vfp_vector = vfp_support_entry;
472 473

		thread_register_notifier(&vfp_notifier_block);
474
		vfp_pm_init();
475 476 477 478 479 480

		/*
		 * We detected VFP, and the support code is
		 * in place; report VFP support to userspace.
		 */
		elf_hwcap |= HWCAP_VFP;
481 482 483 484 485 486 487 488 489 490 491 492
#ifdef CONFIG_VFPv3
		if (VFP_arch >= 3) {
			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
493 494 495 496 497 498 499 500 501
#ifdef CONFIG_NEON
		/*
		 * Check for the presence of the Advanced SIMD
		 * load/store instructions, integer and single
		 * precision floating point operations.
		 */
		if ((fmrx(MVFR1) & 0x000fff00) == 0x00011100)
			elf_hwcap |= HWCAP_NEON;
#endif
L
Linus Torvalds 已提交
502 503 504 505 506
	}
	return 0;
}

late_initcall(vfp_init);