mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / tile / kernel / time.c
blobf95d65f3162bc3a567df4ec346ca69fe605babbc
1 /*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
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.
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.
14 * Support the cycle counter clocksource and tile timer clock event device.
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/sched/clock.h>
24 #include <linux/smp.h>
25 #include <linux/delay.h>
26 #include <linux/module.h>
27 #include <linux/timekeeper_internal.h>
28 #include <asm/irq_regs.h>
29 #include <asm/traps.h>
30 #include <asm/vdso.h>
31 #include <hv/hypervisor.h>
32 #include <arch/interrupts.h>
33 #include <arch/spr_def.h>
37 * Define the cycle counter clock source.
40 /* How many cycles per second we are running at. */
41 static cycles_t cycles_per_sec __ro_after_init;
43 cycles_t get_clock_rate(void)
45 return cycles_per_sec;
48 #if CHIP_HAS_SPLIT_CYCLE()
49 cycles_t get_cycles(void)
51 unsigned int high = __insn_mfspr(SPR_CYCLE_HIGH);
52 unsigned int low = __insn_mfspr(SPR_CYCLE_LOW);
53 unsigned int high2 = __insn_mfspr(SPR_CYCLE_HIGH);
55 while (unlikely(high != high2)) {
56 low = __insn_mfspr(SPR_CYCLE_LOW);
57 high = high2;
58 high2 = __insn_mfspr(SPR_CYCLE_HIGH);
61 return (((cycles_t)high) << 32) | low;
63 EXPORT_SYMBOL(get_cycles);
64 #endif
67 * We use a relatively small shift value so that sched_clock()
68 * won't wrap around very often.
70 #define SCHED_CLOCK_SHIFT 10
72 static unsigned long sched_clock_mult __ro_after_init;
74 static cycles_t clocksource_get_cycles(struct clocksource *cs)
76 return get_cycles();
79 static struct clocksource cycle_counter_cs = {
80 .name = "cycle counter",
81 .rating = 300,
82 .read = clocksource_get_cycles,
83 .mask = CLOCKSOURCE_MASK(64),
84 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
88 * Called very early from setup_arch() to set cycles_per_sec.
89 * We initialize it early so we can use it to set up loops_per_jiffy.
91 void __init setup_clock(void)
93 cycles_per_sec = hv_sysconf(HV_SYSCONF_CPU_SPEED);
94 sched_clock_mult =
95 clocksource_hz2mult(cycles_per_sec, SCHED_CLOCK_SHIFT);
98 void __init calibrate_delay(void)
100 loops_per_jiffy = get_clock_rate() / HZ;
101 pr_info("Clock rate yields %lu.%02lu BogoMIPS (lpj=%lu)\n",
102 loops_per_jiffy / (500000 / HZ),
103 (loops_per_jiffy / (5000 / HZ)) % 100, loops_per_jiffy);
106 /* Called fairly late in init/main.c, but before we go smp. */
107 void __init time_init(void)
109 /* Initialize and register the clock source. */
110 clocksource_register_hz(&cycle_counter_cs, cycles_per_sec);
112 /* Start up the tile-timer interrupt source on the boot cpu. */
113 setup_tile_timer();
117 * Define the tile timer clock event device. The timer is driven by
118 * the TILE_TIMER_CONTROL register, which consists of a 31-bit down
119 * counter, plus bit 31, which signifies that the counter has wrapped
120 * from zero to (2**31) - 1. The INT_TILE_TIMER interrupt will be
121 * raised as long as bit 31 is set.
123 * The TILE_MINSEC value represents the largest range of real-time
124 * we can possibly cover with the timer, based on MAX_TICK combined
125 * with the slowest reasonable clock rate we might run at.
128 #define MAX_TICK 0x7fffffff /* we have 31 bits of countdown timer */
129 #define TILE_MINSEC 5 /* timer covers no more than 5 seconds */
131 static int tile_timer_set_next_event(unsigned long ticks,
132 struct clock_event_device *evt)
134 BUG_ON(ticks > MAX_TICK);
135 __insn_mtspr(SPR_TILE_TIMER_CONTROL, ticks);
136 arch_local_irq_unmask_now(INT_TILE_TIMER);
137 return 0;
141 * Whenever anyone tries to change modes, we just mask interrupts
142 * and wait for the next event to get set.
144 static int tile_timer_shutdown(struct clock_event_device *evt)
146 arch_local_irq_mask_now(INT_TILE_TIMER);
147 return 0;
151 * Set min_delta_ns to 1 microsecond, since it takes about
152 * that long to fire the interrupt.
154 static DEFINE_PER_CPU(struct clock_event_device, tile_timer) = {
155 .name = "tile timer",
156 .features = CLOCK_EVT_FEAT_ONESHOT,
157 .min_delta_ns = 1000,
158 .min_delta_ticks = 1,
159 .max_delta_ticks = MAX_TICK,
160 .rating = 100,
161 .irq = -1,
162 .set_next_event = tile_timer_set_next_event,
163 .set_state_shutdown = tile_timer_shutdown,
164 .set_state_oneshot = tile_timer_shutdown,
165 .set_state_oneshot_stopped = tile_timer_shutdown,
166 .tick_resume = tile_timer_shutdown,
169 void setup_tile_timer(void)
171 struct clock_event_device *evt = this_cpu_ptr(&tile_timer);
173 /* Fill in fields that are speed-specific. */
174 clockevents_calc_mult_shift(evt, cycles_per_sec, TILE_MINSEC);
175 evt->max_delta_ns = clockevent_delta2ns(MAX_TICK, evt);
177 /* Mark as being for this cpu only. */
178 evt->cpumask = cpumask_of(smp_processor_id());
180 /* Start out with timer not firing. */
181 arch_local_irq_mask_now(INT_TILE_TIMER);
183 /* Register tile timer. */
184 clockevents_register_device(evt);
187 /* Called from the interrupt vector. */
188 void do_timer_interrupt(struct pt_regs *regs, int fault_num)
190 struct pt_regs *old_regs = set_irq_regs(regs);
191 struct clock_event_device *evt = this_cpu_ptr(&tile_timer);
194 * Mask the timer interrupt here, since we are a oneshot timer
195 * and there are now by definition no events pending.
197 arch_local_irq_mask(INT_TILE_TIMER);
199 /* Track time spent here in an interrupt context */
200 irq_enter();
202 /* Track interrupt count. */
203 __this_cpu_inc(irq_stat.irq_timer_count);
205 /* Call the generic timer handler */
206 evt->event_handler(evt);
209 * Track time spent against the current process again and
210 * process any softirqs if they are waiting.
212 irq_exit();
214 set_irq_regs(old_regs);
218 * Scheduler clock - returns current time in nanosec units.
219 * Note that with LOCKDEP, this is called during lockdep_init(), and
220 * we will claim that sched_clock() is zero for a little while, until
221 * we run setup_clock(), above.
223 unsigned long long sched_clock(void)
225 return mult_frac(get_cycles(),
226 sched_clock_mult, 1ULL << SCHED_CLOCK_SHIFT);
229 int setup_profiling_timer(unsigned int multiplier)
231 return -EINVAL;
235 * Use the tile timer to convert nsecs to core clock cycles, relying
236 * on it having the same frequency as SPR_CYCLE.
238 cycles_t ns2cycles(unsigned long nsecs)
241 * We do not have to disable preemption here as each core has the same
242 * clock frequency.
244 struct clock_event_device *dev = raw_cpu_ptr(&tile_timer);
247 * as in clocksource.h and x86's timer.h, we split the calculation
248 * into 2 parts to avoid unecessary overflow of the intermediate
249 * value. This will not lead to any loss of precision.
251 u64 quot = (u64)nsecs >> dev->shift;
252 u64 rem = (u64)nsecs & ((1ULL << dev->shift) - 1);
253 return quot * dev->mult + ((rem * dev->mult) >> dev->shift);
256 void update_vsyscall_tz(void)
258 write_seqcount_begin(&vdso_data->tz_seq);
259 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
260 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
261 write_seqcount_end(&vdso_data->tz_seq);
264 void update_vsyscall(struct timekeeper *tk)
266 if (tk->tkr_mono.clock != &cycle_counter_cs)
267 return;
269 write_seqcount_begin(&vdso_data->tb_seq);
271 vdso_data->cycle_last = tk->tkr_mono.cycle_last;
272 vdso_data->mask = tk->tkr_mono.mask;
273 vdso_data->mult = tk->tkr_mono.mult;
274 vdso_data->shift = tk->tkr_mono.shift;
276 vdso_data->wall_time_sec = tk->xtime_sec;
277 vdso_data->wall_time_snsec = tk->tkr_mono.xtime_nsec;
279 vdso_data->monotonic_time_sec = tk->xtime_sec
280 + tk->wall_to_monotonic.tv_sec;
281 vdso_data->monotonic_time_snsec = tk->tkr_mono.xtime_nsec
282 + ((u64)tk->wall_to_monotonic.tv_nsec
283 << tk->tkr_mono.shift);
284 while (vdso_data->monotonic_time_snsec >=
285 (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift)) {
286 vdso_data->monotonic_time_snsec -=
287 ((u64)NSEC_PER_SEC) << tk->tkr_mono.shift;
288 vdso_data->monotonic_time_sec++;
291 vdso_data->wall_time_coarse_sec = tk->xtime_sec;
292 vdso_data->wall_time_coarse_nsec = (long)(tk->tkr_mono.xtime_nsec >>
293 tk->tkr_mono.shift);
295 vdso_data->monotonic_time_coarse_sec =
296 vdso_data->wall_time_coarse_sec + tk->wall_to_monotonic.tv_sec;
297 vdso_data->monotonic_time_coarse_nsec =
298 vdso_data->wall_time_coarse_nsec + tk->wall_to_monotonic.tv_nsec;
300 while (vdso_data->monotonic_time_coarse_nsec >= NSEC_PER_SEC) {
301 vdso_data->monotonic_time_coarse_nsec -= NSEC_PER_SEC;
302 vdso_data->monotonic_time_coarse_sec++;
305 write_seqcount_end(&vdso_data->tb_seq);