xscale-cp0.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * linux/arch/arm/kernel/xscale-cp0.c
  3. *
  4. * XScale DSP and iWMMXt coprocessor context switching and handling
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/signal.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/io.h>
  16. #include <asm/thread_notify.h>
  17. #include <asm/cputype.h>
  18. asm(" .arch armv5te\n");
  19. static inline void dsp_save_state(u32 *state)
  20. {
  21. __asm__ __volatile__ (
  22. "mrrc p0, 0, %0, %1, c0\n"
  23. : "=r" (state[0]), "=r" (state[1]));
  24. }
  25. static inline void dsp_load_state(u32 *state)
  26. {
  27. __asm__ __volatile__ (
  28. "mcrr p0, 0, %0, %1, c0\n"
  29. : : "r" (state[0]), "r" (state[1]));
  30. }
  31. static int dsp_do(struct notifier_block *self, unsigned long cmd, void *t)
  32. {
  33. struct thread_info *thread = t;
  34. switch (cmd) {
  35. case THREAD_NOTIFY_FLUSH:
  36. thread->cpu_context.extra[0] = 0;
  37. thread->cpu_context.extra[1] = 0;
  38. break;
  39. case THREAD_NOTIFY_SWITCH:
  40. dsp_save_state(current_thread_info()->cpu_context.extra);
  41. dsp_load_state(thread->cpu_context.extra);
  42. break;
  43. }
  44. return NOTIFY_DONE;
  45. }
  46. static struct notifier_block dsp_notifier_block = {
  47. .notifier_call = dsp_do,
  48. };
  49. #ifdef CONFIG_IWMMXT
  50. static int iwmmxt_do(struct notifier_block *self, unsigned long cmd, void *t)
  51. {
  52. struct thread_info *thread = t;
  53. switch (cmd) {
  54. case THREAD_NOTIFY_FLUSH:
  55. /*
  56. * flush_thread() zeroes thread->fpstate, so no need
  57. * to do anything here.
  58. *
  59. * FALLTHROUGH: Ensure we don't try to overwrite our newly
  60. * initialised state information on the first fault.
  61. */
  62. case THREAD_NOTIFY_EXIT:
  63. iwmmxt_task_release(thread);
  64. break;
  65. case THREAD_NOTIFY_SWITCH:
  66. iwmmxt_task_switch(thread);
  67. break;
  68. }
  69. return NOTIFY_DONE;
  70. }
  71. static struct notifier_block iwmmxt_notifier_block = {
  72. .notifier_call = iwmmxt_do,
  73. };
  74. #endif
  75. static u32 __init xscale_cp_access_read(void)
  76. {
  77. u32 value;
  78. __asm__ __volatile__ (
  79. "mrc p15, 0, %0, c15, c1, 0\n\t"
  80. : "=r" (value));
  81. return value;
  82. }
  83. static void __init xscale_cp_access_write(u32 value)
  84. {
  85. u32 temp;
  86. __asm__ __volatile__ (
  87. "mcr p15, 0, %1, c15, c1, 0\n\t"
  88. "mrc p15, 0, %0, c15, c1, 0\n\t"
  89. "mov %0, %0\n\t"
  90. "sub pc, pc, #4\n\t"
  91. : "=r" (temp) : "r" (value));
  92. }
  93. /*
  94. * Detect whether we have a MAC coprocessor (40 bit register) or an
  95. * iWMMXt coprocessor (64 bit registers) by loading 00000100:00000000
  96. * into a coprocessor register and reading it back, and checking
  97. * whether the upper word survived intact.
  98. */
  99. static int __init cpu_has_iwmmxt(void)
  100. {
  101. u32 lo;
  102. u32 hi;
  103. /*
  104. * This sequence is interpreted by the DSP coprocessor as:
  105. * mar acc0, %2, %3
  106. * mra %0, %1, acc0
  107. *
  108. * And by the iWMMXt coprocessor as:
  109. * tmcrr wR0, %2, %3
  110. * tmrrc %0, %1, wR0
  111. */
  112. __asm__ __volatile__ (
  113. "mcrr p0, 0, %2, %3, c0\n"
  114. "mrrc p0, 0, %0, %1, c0\n"
  115. : "=r" (lo), "=r" (hi)
  116. : "r" (0), "r" (0x100));
  117. return !!hi;
  118. }
  119. /*
  120. * If we detect that the CPU has iWMMXt (and CONFIG_IWMMXT=y), we
  121. * disable CP0/CP1 on boot, and let call_fpe() and the iWMMXt lazy
  122. * switch code handle iWMMXt context switching. If on the other
  123. * hand the CPU has a DSP coprocessor, we keep access to CP0 enabled
  124. * all the time, and save/restore acc0 on context switch in non-lazy
  125. * fashion.
  126. */
  127. static int __init xscale_cp0_init(void)
  128. {
  129. u32 cp_access;
  130. /* do not attempt to probe iwmmxt on non-xscale family CPUs */
  131. if (!cpu_is_xscale_family())
  132. return 0;
  133. cp_access = xscale_cp_access_read() & ~3;
  134. xscale_cp_access_write(cp_access | 1);
  135. if (cpu_has_iwmmxt()) {
  136. #ifndef CONFIG_IWMMXT
  137. pr_warn("CAUTION: XScale iWMMXt coprocessor detected, but kernel support is missing.\n");
  138. #else
  139. pr_info("XScale iWMMXt coprocessor detected.\n");
  140. elf_hwcap |= HWCAP_IWMMXT;
  141. thread_register_notifier(&iwmmxt_notifier_block);
  142. #endif
  143. } else {
  144. pr_info("XScale DSP coprocessor detected.\n");
  145. thread_register_notifier(&dsp_notifier_block);
  146. cp_access |= 1;
  147. }
  148. xscale_cp_access_write(cp_access);
  149. return 0;
  150. }
  151. late_initcall(xscale_cp0_init);