1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/time.h>
18#include <linux/timex.h>
19#include <linux/clocksource.h>
20#include <linux/clockchips.h>
21#include <linux/hardirq.h>
22#include <linux/sched.h>
23#include <linux/smp.h>
24#include <linux/delay.h>
25#include <linux/module.h>
26#include <linux/timekeeper_internal.h>
27#include <asm/irq_regs.h>
28#include <asm/traps.h>
29#include <asm/vdso.h>
30#include <hv/hypervisor.h>
31#include <arch/interrupts.h>
32#include <arch/spr_def.h>
33
34
35
36
37
38
39
40static cycles_t cycles_per_sec __write_once;
41
42cycles_t get_clock_rate(void)
43{
44 return cycles_per_sec;
45}
46
47#if CHIP_HAS_SPLIT_CYCLE()
48cycles_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
54 while (unlikely(high != high2)) {
55 low = __insn_mfspr(SPR_CYCLE_LOW);
56 high = high2;
57 high2 = __insn_mfspr(SPR_CYCLE_HIGH);
58 }
59
60 return (((cycles_t)high) << 32) | low;
61}
62EXPORT_SYMBOL(get_cycles);
63#endif
64
65
66
67
68
69#define SCHED_CLOCK_SHIFT 10
70
71static unsigned long sched_clock_mult __write_once;
72
73static cycles_t clocksource_get_cycles(struct clocksource *cs)
74{
75 return get_cycles();
76}
77
78static struct clocksource cycle_counter_cs = {
79 .name = "cycle counter",
80 .rating = 300,
81 .read = clocksource_get_cycles,
82 .mask = CLOCKSOURCE_MASK(64),
83 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
84};
85
86
87
88
89
90void __init setup_clock(void)
91{
92 cycles_per_sec = hv_sysconf(HV_SYSCONF_CPU_SPEED);
93 sched_clock_mult =
94 clocksource_hz2mult(cycles_per_sec, SCHED_CLOCK_SHIFT);
95}
96
97void __init calibrate_delay(void)
98{
99 loops_per_jiffy = get_clock_rate() / HZ;
100 pr_info("Clock rate yields %lu.%02lu BogoMIPS (lpj=%lu)\n",
101 loops_per_jiffy/(500000/HZ),
102 (loops_per_jiffy/(5000/HZ)) % 100, loops_per_jiffy);
103}
104
105
106void __init time_init(void)
107{
108
109 clocksource_register_hz(&cycle_counter_cs, cycles_per_sec);
110
111
112 setup_tile_timer();
113}
114
115
116
117
118
119
120
121
122
123
124
125
126
127#define MAX_TICK 0x7fffffff
128#define TILE_MINSEC 5
129
130static int tile_timer_set_next_event(unsigned long ticks,
131 struct clock_event_device *evt)
132{
133 BUG_ON(ticks > MAX_TICK);
134 __insn_mtspr(SPR_TILE_TIMER_CONTROL, ticks);
135 arch_local_irq_unmask_now(INT_TILE_TIMER);
136 return 0;
137}
138
139
140
141
142
143static void tile_timer_set_mode(enum clock_event_mode mode,
144 struct clock_event_device *evt)
145{
146 arch_local_irq_mask_now(INT_TILE_TIMER);
147}
148
149
150
151
152
153static DEFINE_PER_CPU(struct clock_event_device, tile_timer) = {
154 .name = "tile timer",
155 .features = CLOCK_EVT_FEAT_ONESHOT,
156 .min_delta_ns = 1000,
157 .rating = 100,
158 .irq = -1,
159 .set_next_event = tile_timer_set_next_event,
160 .set_mode = tile_timer_set_mode,
161};
162
163void setup_tile_timer(void)
164{
165 struct clock_event_device *evt = &__get_cpu_var(tile_timer);
166
167
168 clockevents_calc_mult_shift(evt, cycles_per_sec, TILE_MINSEC);
169 evt->max_delta_ns = clockevent_delta2ns(MAX_TICK, evt);
170
171
172 evt->cpumask = cpumask_of(smp_processor_id());
173
174
175 arch_local_irq_mask_now(INT_TILE_TIMER);
176
177
178 clockevents_register_device(evt);
179}
180
181
182void do_timer_interrupt(struct pt_regs *regs, int fault_num)
183{
184 struct pt_regs *old_regs = set_irq_regs(regs);
185 struct clock_event_device *evt = &__get_cpu_var(tile_timer);
186
187
188
189
190
191 arch_local_irq_mask(INT_TILE_TIMER);
192
193
194 irq_enter();
195
196
197 __get_cpu_var(irq_stat).irq_timer_count++;
198
199
200 evt->event_handler(evt);
201
202
203
204
205
206 irq_exit();
207
208 set_irq_regs(old_regs);
209}
210
211
212
213
214
215
216
217unsigned long long sched_clock(void)
218{
219 return clocksource_cyc2ns(get_cycles(),
220 sched_clock_mult, SCHED_CLOCK_SHIFT);
221}
222
223int setup_profiling_timer(unsigned int multiplier)
224{
225 return -EINVAL;
226}
227
228
229
230
231
232cycles_t ns2cycles(unsigned long nsecs)
233{
234
235
236
237
238 struct clock_event_device *dev = &__raw_get_cpu_var(tile_timer);
239 return ((u64)nsecs * dev->mult) >> dev->shift;
240}
241
242void update_vsyscall_tz(void)
243{
244
245 ++vdso_data->tz_update_count;
246 smp_wmb();
247 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
248 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
249 smp_wmb();
250 ++vdso_data->tz_update_count;
251}
252
253void update_vsyscall(struct timekeeper *tk)
254{
255 struct timespec wall_time = tk_xtime(tk);
256 struct timespec *wtm = &tk->wall_to_monotonic;
257 struct clocksource *clock = tk->clock;
258
259 if (clock != &cycle_counter_cs)
260 return;
261
262
263 ++vdso_data->tb_update_count;
264 smp_wmb();
265 vdso_data->xtime_tod_stamp = clock->cycle_last;
266 vdso_data->xtime_clock_sec = wall_time.tv_sec;
267 vdso_data->xtime_clock_nsec = wall_time.tv_nsec;
268 vdso_data->wtom_clock_sec = wtm->tv_sec;
269 vdso_data->wtom_clock_nsec = wtm->tv_nsec;
270 vdso_data->mult = clock->mult;
271 vdso_data->shift = clock->shift;
272 smp_wmb();
273 ++vdso_data->tb_update_count;
274}
275