cop2-ex.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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) 2013 Broadcom Corporation.
  7. *
  8. * based on arch/mips/cavium-octeon/cpu.c
  9. * Copyright (C) 2009 Wind River Systems,
  10. * written by Ralf Baechle <ralf@linux-mips.org>
  11. */
  12. #include <linux/init.h>
  13. #include <linux/irqflags.h>
  14. #include <linux/notifier.h>
  15. #include <linux/prefetch.h>
  16. #include <linux/sched.h>
  17. #include <asm/cop2.h>
  18. #include <asm/current.h>
  19. #include <asm/mipsregs.h>
  20. #include <asm/page.h>
  21. #include <asm/netlogic/mips-extns.h>
  22. /*
  23. * 64 bit ops are done in inline assembly to support 32 bit
  24. * compilation
  25. */
  26. void nlm_cop2_save(struct nlm_cop2_state *r)
  27. {
  28. asm volatile(
  29. ".set push\n"
  30. ".set noat\n"
  31. "dmfc2 $1, $0, 0\n"
  32. "sd $1, 0(%1)\n"
  33. "dmfc2 $1, $0, 1\n"
  34. "sd $1, 8(%1)\n"
  35. "dmfc2 $1, $0, 2\n"
  36. "sd $1, 16(%1)\n"
  37. "dmfc2 $1, $0, 3\n"
  38. "sd $1, 24(%1)\n"
  39. "dmfc2 $1, $1, 0\n"
  40. "sd $1, 0(%2)\n"
  41. "dmfc2 $1, $1, 1\n"
  42. "sd $1, 8(%2)\n"
  43. "dmfc2 $1, $1, 2\n"
  44. "sd $1, 16(%2)\n"
  45. "dmfc2 $1, $1, 3\n"
  46. "sd $1, 24(%2)\n"
  47. ".set pop\n"
  48. : "=m"(*r)
  49. : "r"(r->tx), "r"(r->rx));
  50. r->tx_msg_status = __read_32bit_c2_register($2, 0);
  51. r->rx_msg_status = __read_32bit_c2_register($3, 0) & 0x0fffffff;
  52. }
  53. void nlm_cop2_restore(struct nlm_cop2_state *r)
  54. {
  55. u32 rstat;
  56. asm volatile(
  57. ".set push\n"
  58. ".set noat\n"
  59. "ld $1, 0(%1)\n"
  60. "dmtc2 $1, $0, 0\n"
  61. "ld $1, 8(%1)\n"
  62. "dmtc2 $1, $0, 1\n"
  63. "ld $1, 16(%1)\n"
  64. "dmtc2 $1, $0, 2\n"
  65. "ld $1, 24(%1)\n"
  66. "dmtc2 $1, $0, 3\n"
  67. "ld $1, 0(%2)\n"
  68. "dmtc2 $1, $1, 0\n"
  69. "ld $1, 8(%2)\n"
  70. "dmtc2 $1, $1, 1\n"
  71. "ld $1, 16(%2)\n"
  72. "dmtc2 $1, $1, 2\n"
  73. "ld $1, 24(%2)\n"
  74. "dmtc2 $1, $1, 3\n"
  75. ".set pop\n"
  76. : : "m"(*r), "r"(r->tx), "r"(r->rx));
  77. __write_32bit_c2_register($2, 0, r->tx_msg_status);
  78. rstat = __read_32bit_c2_register($3, 0) & 0xf0000000u;
  79. __write_32bit_c2_register($3, 0, r->rx_msg_status | rstat);
  80. }
  81. static int nlm_cu2_call(struct notifier_block *nfb, unsigned long action,
  82. void *data)
  83. {
  84. unsigned long flags;
  85. unsigned int status;
  86. switch (action) {
  87. case CU2_EXCEPTION:
  88. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  89. break;
  90. local_irq_save(flags);
  91. KSTK_STATUS(current) |= ST0_CU2;
  92. status = read_c0_status();
  93. write_c0_status(status | ST0_CU2);
  94. nlm_cop2_restore(&(current->thread.cp2));
  95. write_c0_status(status & ~ST0_CU2);
  96. local_irq_restore(flags);
  97. pr_info("COP2 access enabled for pid %d (%s)\n",
  98. current->pid, current->comm);
  99. return NOTIFY_BAD; /* Don't call default notifier */
  100. }
  101. return NOTIFY_OK; /* Let default notifier send signals */
  102. }
  103. static int __init nlm_cu2_setup(void)
  104. {
  105. return cu2_notifier(nlm_cu2_call, 0);
  106. }
  107. early_initcall(nlm_cu2_setup);