cevt-r4k.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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/smtc_ipi.h>
  15. #include <asm/time.h>
  16. #include <asm/cevt-r4k.h>
  17. #include <asm/gic.h>
  18. /*
  19. * The SMTC Kernel for the 34K, 1004K, et. al. replaces several
  20. * of these routines with SMTC-specific variants.
  21. */
  22. #ifndef CONFIG_MIPS_MT_SMTC
  23. static int mips_next_event(unsigned long delta,
  24. struct clock_event_device *evt)
  25. {
  26. unsigned int cnt;
  27. int res;
  28. cnt = read_c0_count();
  29. cnt += delta;
  30. write_c0_compare(cnt);
  31. res = ((int)(read_c0_count() - cnt) >= 0) ? -ETIME : 0;
  32. return res;
  33. }
  34. #endif /* CONFIG_MIPS_MT_SMTC */
  35. void mips_set_clock_mode(enum clock_event_mode mode,
  36. struct clock_event_device *evt)
  37. {
  38. /* Nothing to do ... */
  39. }
  40. DEFINE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  41. int cp0_timer_irq_installed;
  42. #ifndef CONFIG_MIPS_MT_SMTC
  43. irqreturn_t c0_compare_interrupt(int irq, void *dev_id)
  44. {
  45. const int r2 = cpu_has_mips_r2;
  46. struct clock_event_device *cd;
  47. int cpu = smp_processor_id();
  48. /*
  49. * Suckage alert:
  50. * Before R2 of the architecture there was no way to see if a
  51. * performance counter interrupt was pending, so we have to run
  52. * the performance counter interrupt handler anyway.
  53. */
  54. if (handle_perf_irq(r2))
  55. goto out;
  56. /*
  57. * The same applies to performance counter interrupts. But with the
  58. * above we now know that the reason we got here must be a timer
  59. * interrupt. Being the paranoiacs we are we check anyway.
  60. */
  61. if (!r2 || (read_c0_cause() & (1 << 30))) {
  62. /* Clear Count/Compare Interrupt */
  63. write_c0_compare(read_c0_compare());
  64. cd = &per_cpu(mips_clockevent_device, cpu);
  65. #ifdef CONFIG_CEVT_GIC
  66. if (!gic_present)
  67. #endif
  68. cd->event_handler(cd);
  69. }
  70. out:
  71. return IRQ_HANDLED;
  72. }
  73. #endif /* Not CONFIG_MIPS_MT_SMTC */
  74. struct irqaction c0_compare_irqaction = {
  75. .handler = c0_compare_interrupt,
  76. .flags = IRQF_PERCPU | IRQF_TIMER,
  77. .name = "timer",
  78. };
  79. void mips_event_handler(struct clock_event_device *dev)
  80. {
  81. }
  82. /*
  83. * FIXME: This doesn't hold for the relocated E9000 compare interrupt.
  84. */
  85. static int c0_compare_int_pending(void)
  86. {
  87. #ifdef CONFIG_IRQ_GIC
  88. if (cpu_has_veic)
  89. return gic_get_timer_pending();
  90. #endif
  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. #ifndef CONFIG_MIPS_MT_SMTC
  149. int __cpuinit r4k_clockevent_init(void)
  150. {
  151. unsigned int cpu = smp_processor_id();
  152. struct clock_event_device *cd;
  153. unsigned int irq;
  154. if (!cpu_has_counter || !mips_hpt_frequency)
  155. return -ENXIO;
  156. if (!c0_compare_int_usable())
  157. return -ENXIO;
  158. /*
  159. * With vectored interrupts things are getting platform specific.
  160. * get_c0_compare_int is a hook to allow a platform to return the
  161. * interrupt number of it's liking.
  162. */
  163. irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  164. if (get_c0_compare_int)
  165. irq = get_c0_compare_int();
  166. cd = &per_cpu(mips_clockevent_device, cpu);
  167. cd->name = "MIPS";
  168. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  169. clockevent_set_clock(cd, mips_hpt_frequency);
  170. /* Calculate the min / max delta */
  171. cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);
  172. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  173. cd->rating = 300;
  174. cd->irq = irq;
  175. cd->cpumask = cpumask_of(cpu);
  176. cd->set_next_event = mips_next_event;
  177. cd->set_mode = mips_set_clock_mode;
  178. cd->event_handler = mips_event_handler;
  179. #ifdef CONFIG_CEVT_GIC
  180. if (!gic_present)
  181. #endif
  182. clockevents_register_device(cd);
  183. if (cp0_timer_irq_installed)
  184. return 0;
  185. cp0_timer_irq_installed = 1;
  186. setup_irq(irq, &c0_compare_irqaction);
  187. return 0;
  188. }
  189. #endif /* Not CONFIG_MIPS_MT_SMTC */