xscale-cp0.c 3.9 KB

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