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 * Returns with preemption disabled.
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 */
26unsigned long octeon_crypto_enable(struct octeon_cop2_state *state)
27{
28	int status;
29	unsigned long flags;
30
31	preempt_disable();
32	local_irq_save(flags);
33	status = read_c0_status();
34	write_c0_status(status | ST0_CU2);
35	if (KSTK_STATUS(current) & ST0_CU2) {
36		octeon_cop2_save(&(current->thread.cp2));
37		KSTK_STATUS(current) &= ~ST0_CU2;
38		status &= ~ST0_CU2;
39	} else if (status & ST0_CU2) {
40		octeon_cop2_save(state);
41	}
42	local_irq_restore(flags);
43	return status & ST0_CU2;
44}
45EXPORT_SYMBOL_GPL(octeon_crypto_enable);
46
47/**
48 * Disable access to Octeon's COP2 crypto hardware in the kernel. This must be
49 * called after an octeon_crypto_enable() before any context switch or return to
50 * userspace.
51 *
52 * @state:	Pointer to COP2 state to restore
53 * @flags:	Return value from octeon_crypto_enable()
54 */
55void octeon_crypto_disable(struct octeon_cop2_state *state,
56			   unsigned long crypto_flags)
57{
58	unsigned long flags;
59
60	local_irq_save(flags);
61	if (crypto_flags & ST0_CU2)
62		octeon_cop2_restore(state);
63	else
64		write_c0_status(read_c0_status() & ~ST0_CU2);
65	local_irq_restore(flags);
66	preempt_enable();
67}
68EXPORT_SYMBOL_GPL(octeon_crypto_disable);
69