cevt-r4k.c 8.6 KB

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