xref: /OK3568_Linux_fs/kernel/arch/mips/cavium-octeon/crypto/octeon-crypto.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * This file is subject to the terms and conditions of the GNU General Public
3*4882a593Smuzhiyun  * License. See the file "COPYING" in the main directory of this archive
4*4882a593Smuzhiyun  * for more details.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2004-2012 Cavium Networks
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <asm/cop2.h>
10*4882a593Smuzhiyun #include <linux/export.h>
11*4882a593Smuzhiyun #include <linux/interrupt.h>
12*4882a593Smuzhiyun #include <linux/sched/task_stack.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "octeon-crypto.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun /**
17*4882a593Smuzhiyun  * Enable access to Octeon's COP2 crypto hardware for kernel use. Wrap any
18*4882a593Smuzhiyun  * crypto operations in calls to octeon_crypto_enable/disable in order to make
19*4882a593Smuzhiyun  * sure the state of COP2 isn't corrupted if userspace is also performing
20*4882a593Smuzhiyun  * hardware crypto operations. Allocate the state parameter on the stack.
21*4882a593Smuzhiyun  * Returns with preemption disabled.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * @state: Pointer to state structure to store current COP2 state in.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * Returns: Flags to be passed to octeon_crypto_disable()
26*4882a593Smuzhiyun  */
octeon_crypto_enable(struct octeon_cop2_state * state)27*4882a593Smuzhiyun unsigned long octeon_crypto_enable(struct octeon_cop2_state *state)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	int status;
30*4882a593Smuzhiyun 	unsigned long flags;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun 	preempt_disable();
33*4882a593Smuzhiyun 	local_irq_save(flags);
34*4882a593Smuzhiyun 	status = read_c0_status();
35*4882a593Smuzhiyun 	write_c0_status(status | ST0_CU2);
36*4882a593Smuzhiyun 	if (KSTK_STATUS(current) & ST0_CU2) {
37*4882a593Smuzhiyun 		octeon_cop2_save(&(current->thread.cp2));
38*4882a593Smuzhiyun 		KSTK_STATUS(current) &= ~ST0_CU2;
39*4882a593Smuzhiyun 		status &= ~ST0_CU2;
40*4882a593Smuzhiyun 	} else if (status & ST0_CU2) {
41*4882a593Smuzhiyun 		octeon_cop2_save(state);
42*4882a593Smuzhiyun 	}
43*4882a593Smuzhiyun 	local_irq_restore(flags);
44*4882a593Smuzhiyun 	return status & ST0_CU2;
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(octeon_crypto_enable);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /**
49*4882a593Smuzhiyun  * Disable access to Octeon's COP2 crypto hardware in the kernel. This must be
50*4882a593Smuzhiyun  * called after an octeon_crypto_enable() before any context switch or return to
51*4882a593Smuzhiyun  * userspace.
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * @state:	Pointer to COP2 state to restore
54*4882a593Smuzhiyun  * @flags:	Return value from octeon_crypto_enable()
55*4882a593Smuzhiyun  */
octeon_crypto_disable(struct octeon_cop2_state * state,unsigned long crypto_flags)56*4882a593Smuzhiyun void octeon_crypto_disable(struct octeon_cop2_state *state,
57*4882a593Smuzhiyun 			   unsigned long crypto_flags)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	unsigned long flags;
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	local_irq_save(flags);
62*4882a593Smuzhiyun 	if (crypto_flags & ST0_CU2)
63*4882a593Smuzhiyun 		octeon_cop2_restore(state);
64*4882a593Smuzhiyun 	else
65*4882a593Smuzhiyun 		write_c0_status(read_c0_status() & ~ST0_CU2);
66*4882a593Smuzhiyun 	local_irq_restore(flags);
67*4882a593Smuzhiyun 	preempt_enable();
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(octeon_crypto_disable);
70