cevt-r4k.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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) 2007 MIPS Technologies, Inc.
  7. * Copyright (C) 2007 Ralf Baechle <ralf@linux-mips.org>
  8. */
  9. #include <linux/clockchips.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/percpu.h>
  12. #include <linux/smp.h>
  13. #include <linux/irq.h>
  14. #include <asm/time.h>
  15. #include <asm/cevt-r4k.h>
  16. static int mips_next_event(unsigned long delta,
  17. struct clock_event_device *evt)
  18. {
  19. unsigned int cnt;
  20. int res;
  21. cnt = read_c0_count();
  22. cnt += delta;
  23. write_c0_compare(cnt);
  24. res = ((int)(read_c0_count() - cnt) >= 0) ? -ETIME : 0;
  25. return res;
  26. }
  27. DEFINE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  28. int cp0_timer_irq_installed;
  29. /*
  30. * Possibly handle a performance counter interrupt.
  31. * Return true if the timer interrupt should not be checked
  32. */
  33. static inline int handle_perf_irq(int r2)
  34. {
  35. /*
  36. * The performance counter overflow interrupt may be shared with the
  37. * timer interrupt (cp0_perfcount_irq < 0). If it is and a
  38. * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
  39. * and we can't reliably determine if a counter interrupt has also
  40. * happened (!r2) then don't check for a timer interrupt.
  41. */
  42. return (cp0_perfcount_irq < 0) &&
  43. perf_irq() == IRQ_HANDLED &&
  44. !r2;
  45. }
  46. irqreturn_t c0_compare_interrupt(int irq, void *dev_id)
  47. {
  48. const int r2 = cpu_has_mips_r2_r6;
  49. struct clock_event_device *cd;
  50. int cpu = smp_processor_id();
  51. /*
  52. * Suckage alert:
  53. * Before R2 of the architecture there was no way to see if a
  54. * performance counter interrupt was pending, so we have to run
  55. * the performance counter interrupt handler anyway.
  56. */
  57. if (handle_perf_irq(r2))
  58. return IRQ_HANDLED;
  59. /*
  60. * The same applies to performance counter interrupts. But with the
  61. * above we now know that the reason we got here must be a timer
  62. * interrupt. Being the paranoiacs we are we check anyway.
  63. */
  64. if (!r2 || (read_c0_cause() & CAUSEF_TI)) {
  65. /* Clear Count/Compare Interrupt */
  66. write_c0_compare(read_c0_compare());
  67. cd = &per_cpu(mips_clockevent_device, cpu);
  68. cd->event_handler(cd);
  69. return IRQ_HANDLED;
  70. }
  71. return IRQ_NONE;
  72. }
  73. struct irqaction c0_compare_irqaction = {
  74. .handler = c0_compare_interrupt,
  75. /*
  76. * IRQF_SHARED: The timer interrupt may be shared with other interrupts
  77. * such as perf counter and FDC interrupts.
  78. */
  79. .flags = IRQF_PERCPU | IRQF_TIMER | IRQF_SHARED,
  80. .name = "timer",
  81. };
  82. void mips_event_handler(struct clock_event_device *dev)
  83. {
  84. }
  85. /*
  86. * FIXME: This doesn't hold for the relocated E9000 compare interrupt.
  87. */
  88. static int c0_compare_int_pending(void)
  89. {
  90. /* When cpu_has_mips_r2, this checks Cause.TI instead of Cause.IP7 */
  91. return (read_c0_cause() >> cp0_compare_irq_shift) & (1ul << CAUSEB_IP);
  92. }
  93. /*
  94. * Compare interrupt can be routed and latched outside the core,
  95. * so wait up to worst case number of cycle counter ticks for timer interrupt
  96. * changes to propagate to the cause register.
  97. */
  98. #define COMPARE_INT_SEEN_TICKS 50
  99. int c0_compare_int_usable(void)
  100. {
  101. unsigned int delta;
  102. unsigned int cnt;
  103. #ifdef CONFIG_KVM_GUEST
  104. return 1;
  105. #endif
  106. /*
  107. * IP7 already pending? Try to clear it by acking the timer.
  108. */
  109. if (c0_compare_int_pending()) {
  110. cnt = read_c0_count();
  111. write_c0_compare(cnt);
  112. back_to_back_c0_hazard();
  113. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  114. if (!c0_compare_int_pending())
  115. break;
  116. if (c0_compare_int_pending())
  117. return 0;
  118. }
  119. for (delta = 0x10; delta <= 0x400000; delta <<= 1) {
  120. cnt = read_c0_count();
  121. cnt += delta;
  122. write_c0_compare(cnt);
  123. back_to_back_c0_hazard();
  124. if ((int)(read_c0_count() - cnt) < 0)
  125. break;
  126. /* increase delta if the timer was already expired */
  127. }
  128. while ((int)(read_c0_count() - cnt) <= 0)
  129. ; /* Wait for expiry */
  130. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  131. if (c0_compare_int_pending())
  132. break;
  133. if (!c0_compare_int_pending())
  134. return 0;
  135. cnt = read_c0_count();
  136. write_c0_compare(cnt);
  137. back_to_back_c0_hazard();
  138. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  139. if (!c0_compare_int_pending())
  140. break;
  141. if (c0_compare_int_pending())
  142. return 0;
  143. /*
  144. * Feels like a real count / compare timer.
  145. */
  146. return 1;
  147. }
  148. unsigned int __weak get_c0_compare_int(void)
  149. {
  150. return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  151. }
  152. int r4k_clockevent_init(void)
  153. {
  154. unsigned int cpu = smp_processor_id();
  155. struct clock_event_device *cd;
  156. unsigned int irq;
  157. if (!cpu_has_counter || !mips_hpt_frequency)
  158. return -ENXIO;
  159. if (!c0_compare_int_usable())
  160. return -ENXIO;
  161. /*
  162. * With vectored interrupts things are getting platform specific.
  163. * get_c0_compare_int is a hook to allow a platform to return the
  164. * interrupt number of its liking.
  165. */
  166. irq = get_c0_compare_int();
  167. cd = &per_cpu(mips_clockevent_device, cpu);
  168. cd->name = "MIPS";
  169. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  170. CLOCK_EVT_FEAT_C3STOP |
  171. CLOCK_EVT_FEAT_PERCPU;
  172. clockevent_set_clock(cd, mips_hpt_frequency);
  173. /* Calculate the min / max delta */
  174. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  175. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  176. cd->rating = 300;
  177. cd->irq = irq;
  178. cd->cpumask = cpumask_of(cpu);
  179. cd->set_next_event = mips_next_event;
  180. cd->event_handler = mips_event_handler;
  181. clockevents_register_device(cd);
  182. if (cp0_timer_irq_installed)
  183. return 0;
  184. cp0_timer_irq_installed = 1;
  185. setup_irq(irq, &c0_compare_irqaction);
  186. return 0;
  187. }