cevt-r4k.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #include <asm/gic.h>
  17. static int mips_next_event(unsigned long delta,
  18. struct clock_event_device *evt)
  19. {
  20. unsigned int cnt;
  21. int res;
  22. cnt = read_c0_count();
  23. cnt += delta;
  24. write_c0_compare(cnt);
  25. res = ((int)(read_c0_count() - cnt) >= 0) ? -ETIME : 0;
  26. return res;
  27. }
  28. void mips_set_clock_mode(enum clock_event_mode mode,
  29. struct clock_event_device *evt)
  30. {
  31. /* Nothing to do ... */
  32. }
  33. DEFINE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  34. int cp0_timer_irq_installed;
  35. irqreturn_t c0_compare_interrupt(int irq, void *dev_id)
  36. {
  37. const int r2 = cpu_has_mips_r2;
  38. struct clock_event_device *cd;
  39. int cpu = smp_processor_id();
  40. /*
  41. * Suckage alert:
  42. * Before R2 of the architecture there was no way to see if a
  43. * performance counter interrupt was pending, so we have to run
  44. * the performance counter interrupt handler anyway.
  45. */
  46. if (handle_perf_irq(r2))
  47. goto out;
  48. /*
  49. * The same applies to performance counter interrupts. But with the
  50. * above we now know that the reason we got here must be a timer
  51. * interrupt. Being the paranoiacs we are we check anyway.
  52. */
  53. if (!r2 || (read_c0_cause() & (1 << 30))) {
  54. /* Clear Count/Compare Interrupt */
  55. write_c0_compare(read_c0_compare());
  56. cd = &per_cpu(mips_clockevent_device, cpu);
  57. cd->event_handler(cd);
  58. }
  59. out:
  60. return IRQ_HANDLED;
  61. }
  62. struct irqaction c0_compare_irqaction = {
  63. .handler = c0_compare_interrupt,
  64. .flags = IRQF_PERCPU | IRQF_TIMER,
  65. .name = "timer",
  66. };
  67. void mips_event_handler(struct clock_event_device *dev)
  68. {
  69. }
  70. /*
  71. * FIXME: This doesn't hold for the relocated E9000 compare interrupt.
  72. */
  73. static int c0_compare_int_pending(void)
  74. {
  75. #ifdef CONFIG_IRQ_GIC
  76. if (cpu_has_veic)
  77. return gic_get_timer_pending();
  78. #endif
  79. return (read_c0_cause() >> cp0_compare_irq_shift) & (1ul << CAUSEB_IP);
  80. }
  81. /*
  82. * Compare interrupt can be routed and latched outside the core,
  83. * so wait up to worst case number of cycle counter ticks for timer interrupt
  84. * changes to propagate to the cause register.
  85. */
  86. #define COMPARE_INT_SEEN_TICKS 50
  87. int c0_compare_int_usable(void)
  88. {
  89. unsigned int delta;
  90. unsigned int cnt;
  91. #ifdef CONFIG_KVM_GUEST
  92. return 1;
  93. #endif
  94. /*
  95. * IP7 already pending? Try to clear it by acking the timer.
  96. */
  97. if (c0_compare_int_pending()) {
  98. cnt = read_c0_count();
  99. write_c0_compare(cnt);
  100. back_to_back_c0_hazard();
  101. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  102. if (!c0_compare_int_pending())
  103. break;
  104. if (c0_compare_int_pending())
  105. return 0;
  106. }
  107. for (delta = 0x10; delta <= 0x400000; delta <<= 1) {
  108. cnt = read_c0_count();
  109. cnt += delta;
  110. write_c0_compare(cnt);
  111. back_to_back_c0_hazard();
  112. if ((int)(read_c0_count() - cnt) < 0)
  113. break;
  114. /* increase delta if the timer was already expired */
  115. }
  116. while ((int)(read_c0_count() - cnt) <= 0)
  117. ; /* Wait for expiry */
  118. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  119. if (c0_compare_int_pending())
  120. break;
  121. if (!c0_compare_int_pending())
  122. return 0;
  123. cnt = read_c0_count();
  124. write_c0_compare(cnt);
  125. back_to_back_c0_hazard();
  126. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  127. if (!c0_compare_int_pending())
  128. break;
  129. if (c0_compare_int_pending())
  130. return 0;
  131. /*
  132. * Feels like a real count / compare timer.
  133. */
  134. return 1;
  135. }
  136. int r4k_clockevent_init(void)
  137. {
  138. unsigned int cpu = smp_processor_id();
  139. struct clock_event_device *cd;
  140. unsigned int irq;
  141. if (!cpu_has_counter || !mips_hpt_frequency)
  142. return -ENXIO;
  143. if (!c0_compare_int_usable())
  144. return -ENXIO;
  145. /*
  146. * With vectored interrupts things are getting platform specific.
  147. * get_c0_compare_int is a hook to allow a platform to return the
  148. * interrupt number of it's liking.
  149. */
  150. irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  151. if (get_c0_compare_int)
  152. irq = get_c0_compare_int();
  153. cd = &per_cpu(mips_clockevent_device, cpu);
  154. cd->name = "MIPS";
  155. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  156. CLOCK_EVT_FEAT_C3STOP |
  157. CLOCK_EVT_FEAT_PERCPU;
  158. clockevent_set_clock(cd, mips_hpt_frequency);
  159. /* Calculate the min / max delta */
  160. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  161. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  162. cd->rating = 300;
  163. cd->irq = irq;
  164. cd->cpumask = cpumask_of(cpu);
  165. cd->set_next_event = mips_next_event;
  166. cd->set_mode = mips_set_clock_mode;
  167. cd->event_handler = mips_event_handler;
  168. clockevents_register_device(cd);
  169. if (cp0_timer_irq_installed)
  170. return 0;
  171. cp0_timer_irq_installed = 1;
  172. setup_irq(irq, &c0_compare_irqaction);
  173. return 0;
  174. }