cevt-r4k.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. /**
  28. * calculate_min_delta() - Calculate a good minimum delta for mips_next_event().
  29. *
  30. * Running under virtualisation can introduce overhead into mips_next_event() in
  31. * the form of hypervisor emulation of CP0_Count/CP0_Compare registers,
  32. * potentially with an unnatural frequency, which makes a fixed min_delta_ns
  33. * value inappropriate as it may be too small.
  34. *
  35. * It can also introduce occasional latency from the guest being descheduled.
  36. *
  37. * This function calculates a good minimum delta based roughly on the 75th
  38. * percentile of the time taken to do the mips_next_event() sequence, in order
  39. * to handle potentially higher overhead while also eliminating outliers due to
  40. * unpredictable hypervisor latency (which can be handled by retries).
  41. *
  42. * Return: An appropriate minimum delta for the clock event device.
  43. */
  44. static unsigned int calculate_min_delta(void)
  45. {
  46. unsigned int cnt, i, j, k, l;
  47. unsigned int buf1[4], buf2[3];
  48. unsigned int min_delta;
  49. /*
  50. * Calculate the median of 5 75th percentiles of 5 samples of how long
  51. * it takes to set CP0_Compare = CP0_Count + delta.
  52. */
  53. for (i = 0; i < 5; ++i) {
  54. for (j = 0; j < 5; ++j) {
  55. /*
  56. * This is like the code in mips_next_event(), and
  57. * directly measures the borderline "safe" delta.
  58. */
  59. cnt = read_c0_count();
  60. write_c0_compare(cnt);
  61. cnt = read_c0_count() - cnt;
  62. /* Sorted insert into buf1 */
  63. for (k = 0; k < j; ++k) {
  64. if (cnt < buf1[k]) {
  65. l = min_t(unsigned int,
  66. j, ARRAY_SIZE(buf1) - 1);
  67. for (; l > k; --l)
  68. buf1[l] = buf1[l - 1];
  69. break;
  70. }
  71. }
  72. if (k < ARRAY_SIZE(buf1))
  73. buf1[k] = cnt;
  74. }
  75. /* Sorted insert of 75th percentile into buf2 */
  76. for (k = 0; k < i && k < ARRAY_SIZE(buf2); ++k) {
  77. if (buf1[ARRAY_SIZE(buf1) - 1] < buf2[k]) {
  78. l = min_t(unsigned int,
  79. i, ARRAY_SIZE(buf2) - 1);
  80. for (; l > k; --l)
  81. buf2[l] = buf2[l - 1];
  82. break;
  83. }
  84. }
  85. if (k < ARRAY_SIZE(buf2))
  86. buf2[k] = buf1[ARRAY_SIZE(buf1) - 1];
  87. }
  88. /* Use 2 * median of 75th percentiles */
  89. min_delta = buf2[ARRAY_SIZE(buf2) - 1] * 2;
  90. /* Don't go too low */
  91. if (min_delta < 0x300)
  92. min_delta = 0x300;
  93. pr_debug("%s: median 75th percentile=%#x, min_delta=%#x\n",
  94. __func__, buf2[ARRAY_SIZE(buf2) - 1], min_delta);
  95. return min_delta;
  96. }
  97. DEFINE_PER_CPU(struct clock_event_device, mips_clockevent_device);
  98. int cp0_timer_irq_installed;
  99. /*
  100. * Possibly handle a performance counter interrupt.
  101. * Return true if the timer interrupt should not be checked
  102. */
  103. static inline int handle_perf_irq(int r2)
  104. {
  105. /*
  106. * The performance counter overflow interrupt may be shared with the
  107. * timer interrupt (cp0_perfcount_irq < 0). If it is and a
  108. * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
  109. * and we can't reliably determine if a counter interrupt has also
  110. * happened (!r2) then don't check for a timer interrupt.
  111. */
  112. return (cp0_perfcount_irq < 0) &&
  113. perf_irq() == IRQ_HANDLED &&
  114. !r2;
  115. }
  116. irqreturn_t c0_compare_interrupt(int irq, void *dev_id)
  117. {
  118. const int r2 = cpu_has_mips_r2_r6;
  119. struct clock_event_device *cd;
  120. int cpu = smp_processor_id();
  121. /*
  122. * Suckage alert:
  123. * Before R2 of the architecture there was no way to see if a
  124. * performance counter interrupt was pending, so we have to run
  125. * the performance counter interrupt handler anyway.
  126. */
  127. if (handle_perf_irq(r2))
  128. return IRQ_HANDLED;
  129. /*
  130. * The same applies to performance counter interrupts. But with the
  131. * above we now know that the reason we got here must be a timer
  132. * interrupt. Being the paranoiacs we are we check anyway.
  133. */
  134. if (!r2 || (read_c0_cause() & CAUSEF_TI)) {
  135. /* Clear Count/Compare Interrupt */
  136. write_c0_compare(read_c0_compare());
  137. cd = &per_cpu(mips_clockevent_device, cpu);
  138. cd->event_handler(cd);
  139. return IRQ_HANDLED;
  140. }
  141. return IRQ_NONE;
  142. }
  143. struct irqaction c0_compare_irqaction = {
  144. .handler = c0_compare_interrupt,
  145. /*
  146. * IRQF_SHARED: The timer interrupt may be shared with other interrupts
  147. * such as perf counter and FDC interrupts.
  148. */
  149. .flags = IRQF_PERCPU | IRQF_TIMER | IRQF_SHARED,
  150. .name = "timer",
  151. };
  152. void mips_event_handler(struct clock_event_device *dev)
  153. {
  154. }
  155. /*
  156. * FIXME: This doesn't hold for the relocated E9000 compare interrupt.
  157. */
  158. static int c0_compare_int_pending(void)
  159. {
  160. /* When cpu_has_mips_r2, this checks Cause.TI instead of Cause.IP7 */
  161. return (read_c0_cause() >> cp0_compare_irq_shift) & (1ul << CAUSEB_IP);
  162. }
  163. /*
  164. * Compare interrupt can be routed and latched outside the core,
  165. * so wait up to worst case number of cycle counter ticks for timer interrupt
  166. * changes to propagate to the cause register.
  167. */
  168. #define COMPARE_INT_SEEN_TICKS 50
  169. int c0_compare_int_usable(void)
  170. {
  171. unsigned int delta;
  172. unsigned int cnt;
  173. #ifdef CONFIG_KVM_GUEST
  174. return 1;
  175. #endif
  176. /*
  177. * IP7 already pending? Try to clear it by acking the timer.
  178. */
  179. if (c0_compare_int_pending()) {
  180. cnt = read_c0_count();
  181. write_c0_compare(cnt);
  182. back_to_back_c0_hazard();
  183. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  184. if (!c0_compare_int_pending())
  185. break;
  186. if (c0_compare_int_pending())
  187. return 0;
  188. }
  189. for (delta = 0x10; delta <= 0x400000; delta <<= 1) {
  190. cnt = read_c0_count();
  191. cnt += delta;
  192. write_c0_compare(cnt);
  193. back_to_back_c0_hazard();
  194. if ((int)(read_c0_count() - cnt) < 0)
  195. break;
  196. /* increase delta if the timer was already expired */
  197. }
  198. while ((int)(read_c0_count() - cnt) <= 0)
  199. ; /* Wait for expiry */
  200. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  201. if (c0_compare_int_pending())
  202. break;
  203. if (!c0_compare_int_pending())
  204. return 0;
  205. cnt = read_c0_count();
  206. write_c0_compare(cnt);
  207. back_to_back_c0_hazard();
  208. while (read_c0_count() < (cnt + COMPARE_INT_SEEN_TICKS))
  209. if (!c0_compare_int_pending())
  210. break;
  211. if (c0_compare_int_pending())
  212. return 0;
  213. /*
  214. * Feels like a real count / compare timer.
  215. */
  216. return 1;
  217. }
  218. unsigned int __weak get_c0_compare_int(void)
  219. {
  220. return MIPS_CPU_IRQ_BASE + cp0_compare_irq;
  221. }
  222. int r4k_clockevent_init(void)
  223. {
  224. unsigned int cpu = smp_processor_id();
  225. struct clock_event_device *cd;
  226. unsigned int irq, min_delta;
  227. if (!cpu_has_counter || !mips_hpt_frequency)
  228. return -ENXIO;
  229. if (!c0_compare_int_usable())
  230. return -ENXIO;
  231. /*
  232. * With vectored interrupts things are getting platform specific.
  233. * get_c0_compare_int is a hook to allow a platform to return the
  234. * interrupt number of its liking.
  235. */
  236. irq = get_c0_compare_int();
  237. cd = &per_cpu(mips_clockevent_device, cpu);
  238. cd->name = "MIPS";
  239. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  240. CLOCK_EVT_FEAT_C3STOP |
  241. CLOCK_EVT_FEAT_PERCPU;
  242. min_delta = calculate_min_delta();
  243. cd->rating = 300;
  244. cd->irq = irq;
  245. cd->cpumask = cpumask_of(cpu);
  246. cd->set_next_event = mips_next_event;
  247. cd->event_handler = mips_event_handler;
  248. clockevents_config_and_register(cd, mips_hpt_frequency, min_delta, 0x7fffffff);
  249. if (cp0_timer_irq_installed)
  250. return 0;
  251. cp0_timer_irq_installed = 1;
  252. setup_irq(irq, &c0_compare_irqaction);
  253. return 0;
  254. }