time.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * Support the cycle counter clocksource and tile timer clock event device.
  15. */
  16. #include <linux/time.h>
  17. #include <linux/timex.h>
  18. #include <linux/clocksource.h>
  19. #include <linux/clockchips.h>
  20. #include <linux/hardirq.h>
  21. #include <linux/sched.h>
  22. #include <linux/smp.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <asm/irq_regs.h>
  26. #include <asm/traps.h>
  27. #include <hv/hypervisor.h>
  28. #include <arch/interrupts.h>
  29. #include <arch/spr_def.h>
  30. /*
  31. * Define the cycle counter clock source.
  32. */
  33. /* How many cycles per second we are running at. */
  34. static cycles_t cycles_per_sec __write_once;
  35. cycles_t get_clock_rate(void)
  36. {
  37. return cycles_per_sec;
  38. }
  39. #if CHIP_HAS_SPLIT_CYCLE()
  40. cycles_t get_cycles(void)
  41. {
  42. unsigned int high = __insn_mfspr(SPR_CYCLE_HIGH);
  43. unsigned int low = __insn_mfspr(SPR_CYCLE_LOW);
  44. unsigned int high2 = __insn_mfspr(SPR_CYCLE_HIGH);
  45. while (unlikely(high != high2)) {
  46. low = __insn_mfspr(SPR_CYCLE_LOW);
  47. high = high2;
  48. high2 = __insn_mfspr(SPR_CYCLE_HIGH);
  49. }
  50. return (((cycles_t)high) << 32) | low;
  51. }
  52. EXPORT_SYMBOL(get_cycles);
  53. #endif
  54. /*
  55. * We use a relatively small shift value so that sched_clock()
  56. * won't wrap around very often.
  57. */
  58. #define SCHED_CLOCK_SHIFT 10
  59. static unsigned long sched_clock_mult __write_once;
  60. static cycles_t clocksource_get_cycles(struct clocksource *cs)
  61. {
  62. return get_cycles();
  63. }
  64. static struct clocksource cycle_counter_cs = {
  65. .name = "cycle counter",
  66. .rating = 300,
  67. .read = clocksource_get_cycles,
  68. .mask = CLOCKSOURCE_MASK(64),
  69. .shift = 22, /* typical value, e.g. x86 tsc uses this */
  70. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  71. };
  72. /*
  73. * Called very early from setup_arch() to set cycles_per_sec.
  74. * We initialize it early so we can use it to set up loops_per_jiffy.
  75. */
  76. void __init setup_clock(void)
  77. {
  78. cycles_per_sec = hv_sysconf(HV_SYSCONF_CPU_SPEED);
  79. sched_clock_mult =
  80. clocksource_hz2mult(cycles_per_sec, SCHED_CLOCK_SHIFT);
  81. cycle_counter_cs.mult =
  82. clocksource_hz2mult(cycles_per_sec, cycle_counter_cs.shift);
  83. }
  84. void __init calibrate_delay(void)
  85. {
  86. loops_per_jiffy = get_clock_rate() / HZ;
  87. pr_info("Clock rate yields %lu.%02lu BogoMIPS (lpj=%lu)\n",
  88. loops_per_jiffy/(500000/HZ),
  89. (loops_per_jiffy/(5000/HZ)) % 100, loops_per_jiffy);
  90. }
  91. /* Called fairly late in init/main.c, but before we go smp. */
  92. void __init time_init(void)
  93. {
  94. /* Initialize and register the clock source. */
  95. clocksource_register(&cycle_counter_cs);
  96. /* Start up the tile-timer interrupt source on the boot cpu. */
  97. setup_tile_timer();
  98. }
  99. /*
  100. * Define the tile timer clock event device. The timer is driven by
  101. * the TILE_TIMER_CONTROL register, which consists of a 31-bit down
  102. * counter, plus bit 31, which signifies that the counter has wrapped
  103. * from zero to (2**31) - 1. The INT_TILE_TIMER interrupt will be
  104. * raised as long as bit 31 is set.
  105. *
  106. * The TILE_MINSEC value represents the largest range of real-time
  107. * we can possibly cover with the timer, based on MAX_TICK combined
  108. * with the slowest reasonable clock rate we might run at.
  109. */
  110. #define MAX_TICK 0x7fffffff /* we have 31 bits of countdown timer */
  111. #define TILE_MINSEC 5 /* timer covers no more than 5 seconds */
  112. static int tile_timer_set_next_event(unsigned long ticks,
  113. struct clock_event_device *evt)
  114. {
  115. BUG_ON(ticks > MAX_TICK);
  116. __insn_mtspr(SPR_TILE_TIMER_CONTROL, ticks);
  117. arch_local_irq_unmask_now(INT_TILE_TIMER);
  118. return 0;
  119. }
  120. /*
  121. * Whenever anyone tries to change modes, we just mask interrupts
  122. * and wait for the next event to get set.
  123. */
  124. static void tile_timer_set_mode(enum clock_event_mode mode,
  125. struct clock_event_device *evt)
  126. {
  127. arch_local_irq_mask_now(INT_TILE_TIMER);
  128. }
  129. /*
  130. * Set min_delta_ns to 1 microsecond, since it takes about
  131. * that long to fire the interrupt.
  132. */
  133. static DEFINE_PER_CPU(struct clock_event_device, tile_timer) = {
  134. .name = "tile timer",
  135. .features = CLOCK_EVT_FEAT_ONESHOT,
  136. .min_delta_ns = 1000,
  137. .rating = 100,
  138. .irq = -1,
  139. .set_next_event = tile_timer_set_next_event,
  140. .set_mode = tile_timer_set_mode,
  141. };
  142. void __cpuinit setup_tile_timer(void)
  143. {
  144. struct clock_event_device *evt = &__get_cpu_var(tile_timer);
  145. /* Fill in fields that are speed-specific. */
  146. clockevents_calc_mult_shift(evt, cycles_per_sec, TILE_MINSEC);
  147. evt->max_delta_ns = clockevent_delta2ns(MAX_TICK, evt);
  148. /* Mark as being for this cpu only. */
  149. evt->cpumask = cpumask_of(smp_processor_id());
  150. /* Start out with timer not firing. */
  151. arch_local_irq_mask_now(INT_TILE_TIMER);
  152. /* Register tile timer. */
  153. clockevents_register_device(evt);
  154. }
  155. /* Called from the interrupt vector. */
  156. void do_timer_interrupt(struct pt_regs *regs, int fault_num)
  157. {
  158. struct pt_regs *old_regs = set_irq_regs(regs);
  159. struct clock_event_device *evt = &__get_cpu_var(tile_timer);
  160. /*
  161. * Mask the timer interrupt here, since we are a oneshot timer
  162. * and there are now by definition no events pending.
  163. */
  164. arch_local_irq_mask(INT_TILE_TIMER);
  165. /* Track time spent here in an interrupt context */
  166. irq_enter();
  167. /* Track interrupt count. */
  168. __get_cpu_var(irq_stat).irq_timer_count++;
  169. /* Call the generic timer handler */
  170. evt->event_handler(evt);
  171. /*
  172. * Track time spent against the current process again and
  173. * process any softirqs if they are waiting.
  174. */
  175. irq_exit();
  176. set_irq_regs(old_regs);
  177. }
  178. /*
  179. * Scheduler clock - returns current time in nanosec units.
  180. * Note that with LOCKDEP, this is called during lockdep_init(), and
  181. * we will claim that sched_clock() is zero for a little while, until
  182. * we run setup_clock(), above.
  183. */
  184. unsigned long long sched_clock(void)
  185. {
  186. return clocksource_cyc2ns(get_cycles(),
  187. sched_clock_mult, SCHED_CLOCK_SHIFT);
  188. }
  189. int setup_profiling_timer(unsigned int multiplier)
  190. {
  191. return -EINVAL;
  192. }
  193. /*
  194. * Use the tile timer to convert nsecs to core clock cycles, relying
  195. * on it having the same frequency as SPR_CYCLE.
  196. */
  197. cycles_t ns2cycles(unsigned long nsecs)
  198. {
  199. struct clock_event_device *dev = &__get_cpu_var(tile_timer);
  200. return ((u64)nsecs * dev->mult) >> dev->shift;
  201. }