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