time.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 <asm/irq_regs.h>
  25. #include <asm/traps.h>
  26. #include <hv/hypervisor.h>
  27. #include <arch/interrupts.h>
  28. #include <arch/spr_def.h>
  29. /*
  30. * Define the cycle counter clock source.
  31. */
  32. /* How many cycles per second we are running at. */
  33. static cycles_t cycles_per_sec __write_once;
  34. /*
  35. * We set up shift and multiply values with a minsec of five seconds,
  36. * since our timer counter counts down 31 bits at a frequency of
  37. * no less than 500 MHz. See @minsec for clocks_calc_mult_shift().
  38. * We could use a different value for the 64-bit free-running
  39. * cycle counter, but we use the same one for consistency, and since
  40. * we will be reasonably precise with this value anyway.
  41. */
  42. #define TILE_MINSEC 5
  43. cycles_t get_clock_rate(void)
  44. {
  45. return cycles_per_sec;
  46. }
  47. #if CHIP_HAS_SPLIT_CYCLE()
  48. cycles_t get_cycles(void)
  49. {
  50. unsigned int high = __insn_mfspr(SPR_CYCLE_HIGH);
  51. unsigned int low = __insn_mfspr(SPR_CYCLE_LOW);
  52. unsigned int high2 = __insn_mfspr(SPR_CYCLE_HIGH);
  53. while (unlikely(high != high2)) {
  54. low = __insn_mfspr(SPR_CYCLE_LOW);
  55. high = high2;
  56. high2 = __insn_mfspr(SPR_CYCLE_HIGH);
  57. }
  58. return (((cycles_t)high) << 32) | low;
  59. }
  60. #endif
  61. static cycles_t clocksource_get_cycles(struct clocksource *cs)
  62. {
  63. return get_cycles();
  64. }
  65. static struct clocksource cycle_counter_cs = {
  66. .name = "cycle counter",
  67. .rating = 300,
  68. .read = clocksource_get_cycles,
  69. .mask = CLOCKSOURCE_MASK(64),
  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. clocksource_calc_mult_shift(&cycle_counter_cs, cycles_per_sec,
  80. TILE_MINSEC);
  81. }
  82. void __init calibrate_delay(void)
  83. {
  84. loops_per_jiffy = get_clock_rate() / HZ;
  85. pr_info("Clock rate yields %lu.%02lu BogoMIPS (lpj=%lu)\n",
  86. loops_per_jiffy/(500000/HZ),
  87. (loops_per_jiffy/(5000/HZ)) % 100, loops_per_jiffy);
  88. }
  89. /* Called fairly late in init/main.c, but before we go smp. */
  90. void __init time_init(void)
  91. {
  92. /* Initialize and register the clock source. */
  93. clocksource_register(&cycle_counter_cs);
  94. /* Start up the tile-timer interrupt source on the boot cpu. */
  95. setup_tile_timer();
  96. }
  97. /*
  98. * Define the tile timer clock event device. The timer is driven by
  99. * the TILE_TIMER_CONTROL register, which consists of a 31-bit down
  100. * counter, plus bit 31, which signifies that the counter has wrapped
  101. * from zero to (2**31) - 1. The INT_TILE_TIMER interrupt will be
  102. * raised as long as bit 31 is set.
  103. */
  104. #define MAX_TICK 0x7fffffff /* we have 31 bits of countdown timer */
  105. static int tile_timer_set_next_event(unsigned long ticks,
  106. struct clock_event_device *evt)
  107. {
  108. BUG_ON(ticks > MAX_TICK);
  109. __insn_mtspr(SPR_TILE_TIMER_CONTROL, ticks);
  110. raw_local_irq_unmask_now(INT_TILE_TIMER);
  111. return 0;
  112. }
  113. /*
  114. * Whenever anyone tries to change modes, we just mask interrupts
  115. * and wait for the next event to get set.
  116. */
  117. static void tile_timer_set_mode(enum clock_event_mode mode,
  118. struct clock_event_device *evt)
  119. {
  120. raw_local_irq_mask_now(INT_TILE_TIMER);
  121. }
  122. /*
  123. * Set min_delta_ns to 1 microsecond, since it takes about
  124. * that long to fire the interrupt.
  125. */
  126. static DEFINE_PER_CPU(struct clock_event_device, tile_timer) = {
  127. .name = "tile timer",
  128. .features = CLOCK_EVT_FEAT_ONESHOT,
  129. .min_delta_ns = 1000,
  130. .rating = 100,
  131. .irq = -1,
  132. .set_next_event = tile_timer_set_next_event,
  133. .set_mode = tile_timer_set_mode,
  134. };
  135. void __cpuinit setup_tile_timer(void)
  136. {
  137. struct clock_event_device *evt = &__get_cpu_var(tile_timer);
  138. /* Fill in fields that are speed-specific. */
  139. clockevents_calc_mult_shift(evt, cycles_per_sec, TILE_MINSEC);
  140. evt->max_delta_ns = clockevent_delta2ns(MAX_TICK, evt);
  141. /* Mark as being for this cpu only. */
  142. evt->cpumask = cpumask_of(smp_processor_id());
  143. /* Start out with timer not firing. */
  144. raw_local_irq_mask_now(INT_TILE_TIMER);
  145. /* Register tile timer. */
  146. clockevents_register_device(evt);
  147. }
  148. /* Called from the interrupt vector. */
  149. void do_timer_interrupt(struct pt_regs *regs, int fault_num)
  150. {
  151. struct pt_regs *old_regs = set_irq_regs(regs);
  152. struct clock_event_device *evt = &__get_cpu_var(tile_timer);
  153. /*
  154. * Mask the timer interrupt here, since we are a oneshot timer
  155. * and there are now by definition no events pending.
  156. */
  157. raw_local_irq_mask(INT_TILE_TIMER);
  158. /* Track time spent here in an interrupt context */
  159. irq_enter();
  160. /* Track interrupt count. */
  161. __get_cpu_var(irq_stat).irq_timer_count++;
  162. /* Call the generic timer handler */
  163. evt->event_handler(evt);
  164. /*
  165. * Track time spent against the current process again and
  166. * process any softirqs if they are waiting.
  167. */
  168. irq_exit();
  169. set_irq_regs(old_regs);
  170. }
  171. /*
  172. * Scheduler clock - returns current time in nanosec units.
  173. * Note that with LOCKDEP, this is called during lockdep_init(), and
  174. * we will claim that sched_clock() is zero for a little while, until
  175. * we run setup_clock(), above.
  176. */
  177. unsigned long long sched_clock(void)
  178. {
  179. return clocksource_cyc2ns(get_cycles(),
  180. cycle_counter_cs.mult,
  181. cycle_counter_cs.shift);
  182. }
  183. int setup_profiling_timer(unsigned int multiplier)
  184. {
  185. return -EINVAL;
  186. }