octeon-crypto.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #include <asm/cop2.h>
  9. #include <linux/module.h>
  10. #include <linux/interrupt.h>
  11. #include "octeon-crypto.h"
  12. /**
  13. * Enable access to Octeon's COP2 crypto hardware for kernel use. Wrap any
  14. * crypto operations in calls to octeon_crypto_enable/disable in order to make
  15. * sure the state of COP2 isn't corrupted if userspace is also performing
  16. * hardware crypto operations. Allocate the state parameter on the stack.
  17. * Returns with preemption disabled.
  18. *
  19. * @state: Pointer to state structure to store current COP2 state in.
  20. *
  21. * Returns: Flags to be passed to octeon_crypto_disable()
  22. */
  23. unsigned long octeon_crypto_enable(struct octeon_cop2_state *state)
  24. {
  25. int status;
  26. unsigned long flags;
  27. preempt_disable();
  28. local_irq_save(flags);
  29. status = read_c0_status();
  30. write_c0_status(status | ST0_CU2);
  31. if (KSTK_STATUS(current) & ST0_CU2) {
  32. octeon_cop2_save(&(current->thread.cp2));
  33. KSTK_STATUS(current) &= ~ST0_CU2;
  34. status &= ~ST0_CU2;
  35. } else if (status & ST0_CU2) {
  36. octeon_cop2_save(state);
  37. }
  38. local_irq_restore(flags);
  39. return status & ST0_CU2;
  40. }
  41. EXPORT_SYMBOL_GPL(octeon_crypto_enable);
  42. /**
  43. * Disable access to Octeon's COP2 crypto hardware in the kernel. This must be
  44. * called after an octeon_crypto_enable() before any context switch or return to
  45. * userspace.
  46. *
  47. * @state: Pointer to COP2 state to restore
  48. * @flags: Return value from octeon_crypto_enable()
  49. */
  50. void octeon_crypto_disable(struct octeon_cop2_state *state,
  51. unsigned long crypto_flags)
  52. {
  53. unsigned long flags;
  54. local_irq_save(flags);
  55. if (crypto_flags & ST0_CU2)
  56. octeon_cop2_restore(state);
  57. else
  58. write_c0_status(read_c0_status() & ~ST0_CU2);
  59. local_irq_restore(flags);
  60. preempt_enable();
  61. }
  62. EXPORT_SYMBOL_GPL(octeon_crypto_disable);