time.c 8.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 *  arch/s390/kernel/time.c
 *    Time of day based timer functions.
 *
 *  S390 version
 *    Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
 *    Author(s): Hartmut Penner (hp@de.ibm.com),
 *               Martin Schwidefsky (schwidefsky@de.ibm.com),
 *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
 *
 *  Derived from "arch/i386/kernel/time.c"
 *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
 */

#include <linux/errno.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/param.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/smp.h>
#include <linux/types.h>
#include <linux/profile.h>
#include <linux/timex.h>
#include <linux/notifier.h>
31
#include <linux/clocksource.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37

#include <asm/uaccess.h>
#include <asm/delay.h>
#include <asm/s390_ext.h>
#include <asm/div64.h>
#include <asm/irq.h>
H
Heiko Carstens 已提交
38
#include <asm/irq_regs.h>
L
Linus Torvalds 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <asm/timer.h>

/* change this if you have some constant time drift */
#define USECS_PER_JIFFY     ((unsigned long) 1000000/HZ)
#define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)

/*
 * Create a small time difference between the timer interrupts
 * on the different cpus to avoid lock contention.
 */
#define CPU_DEVIATION       (smp_processor_id() << 12)

#define TICK_SIZE tick

static ext_int_info_t ext_int_info_cc;
static u64 init_timer_cc;
static u64 jiffies_timer_cc;
static u64 xtime_cc;

/*
 * Scheduler clock - returns current time in nanosec units.
 */
unsigned long long sched_clock(void)
{
63
	return ((get_clock() - jiffies_timer_cc) * 125) >> 9;
L
Linus Torvalds 已提交
64 65
}

66 67 68 69 70 71 72 73 74
/*
 * Monotonic_clock - returns # of nanoseconds passed since time_init()
 */
unsigned long long monotonic_clock(void)
{
	return sched_clock();
}
EXPORT_SYMBOL(monotonic_clock);

L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84 85 86
void tod_to_timeval(__u64 todval, struct timespec *xtime)
{
	unsigned long long sec;

	sec = todval >> 12;
	do_div(sec, 1000000);
	xtime->tv_sec = sec;
	todval -= (sec * 1000000) << 12;
	xtime->tv_nsec = ((todval * 1000) >> 12);
}

#ifdef CONFIG_PROFILING
H
Heiko Carstens 已提交
87
#define s390_do_profile()	profile_tick(CPU_PROFILING)
L
Linus Torvalds 已提交
88
#else
H
Heiko Carstens 已提交
89
#define s390_do_profile()	do { ; } while(0)
L
Linus Torvalds 已提交
90 91 92 93 94 95 96
#endif /* CONFIG_PROFILING */


/*
 * timer_interrupt() needs to keep up the real-time clock,
 * as well as call the "do_timer()" routine every clocktick
 */
H
Heiko Carstens 已提交
97
void account_ticks(void)
L
Linus Torvalds 已提交
98 99
{
	__u64 tmp;
100
	__u32 ticks;
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

	/* Calculate how many ticks have passed. */
	if (S390_lowcore.int_clock < S390_lowcore.jiffy_timer) {
		/*
		 * We have to program the clock comparator even if
		 * no tick has passed. That happens if e.g. an i/o
		 * interrupt wakes up an idle processor that has
		 * switched off its hz timer.
		 */
		tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
		asm volatile ("SCKC %0" : : "m" (tmp));
		return;
	}
	tmp = S390_lowcore.int_clock - S390_lowcore.jiffy_timer;
	if (tmp >= 2*CLK_TICKS_PER_JIFFY) {  /* more than two ticks ? */
		ticks = __div(tmp, CLK_TICKS_PER_JIFFY) + 1;
		S390_lowcore.jiffy_timer +=
			CLK_TICKS_PER_JIFFY * (__u64) ticks;
	} else if (tmp >= CLK_TICKS_PER_JIFFY) {
		ticks = 2;
		S390_lowcore.jiffy_timer += 2*CLK_TICKS_PER_JIFFY;
	} else {
		ticks = 1;
		S390_lowcore.jiffy_timer += CLK_TICKS_PER_JIFFY;
	}

	/* set clock comparator for next tick */
	tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
        asm volatile ("SCKC %0" : : "m" (tmp));

#ifdef CONFIG_SMP
	/*
	 * Do not rely on the boot cpu to do the calls to do_timer.
	 * Spread it over all cpus instead.
	 */
	write_seqlock(&xtime_lock);
	if (S390_lowcore.jiffy_timer > xtime_cc) {
138
		__u32 xticks;
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146
		tmp = S390_lowcore.jiffy_timer - xtime_cc;
		if (tmp >= 2*CLK_TICKS_PER_JIFFY) {
			xticks = __div(tmp, CLK_TICKS_PER_JIFFY);
			xtime_cc += (__u64) xticks * CLK_TICKS_PER_JIFFY;
		} else {
			xticks = 1;
			xtime_cc += CLK_TICKS_PER_JIFFY;
		}
147
		do_timer(xticks);
L
Linus Torvalds 已提交
148 149 150
	}
	write_sequnlock(&xtime_lock);
#else
151
	do_timer(ticks);
L
Linus Torvalds 已提交
152 153 154
#endif

#ifdef CONFIG_VIRT_CPU_ACCOUNTING
155
	account_tick_vtime(current);
L
Linus Torvalds 已提交
156 157
#else
	while (ticks--)
H
Heiko Carstens 已提交
158
		update_process_times(user_mode(get_irq_regs()));
L
Linus Torvalds 已提交
159 160
#endif

H
Heiko Carstens 已提交
161
	s390_do_profile();
L
Linus Torvalds 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
}

#ifdef CONFIG_NO_IDLE_HZ

#ifdef CONFIG_NO_IDLE_HZ_INIT
int sysctl_hz_timer = 0;
#else
int sysctl_hz_timer = 1;
#endif

/*
 * Stop the HZ tick on the current CPU.
 * Only cpu_idle may call this function.
 */
static inline void stop_hz_timer(void)
{
178 179
	unsigned long flags;
	unsigned long seq, next;
180
	__u64 timer, todval;
181
	int cpu = smp_processor_id();
L
Linus Torvalds 已提交
182 183 184 185

	if (sysctl_hz_timer != 0)
		return;

186
	cpu_set(cpu, nohz_cpu_mask);
L
Linus Torvalds 已提交
187 188 189 190 191

	/*
	 * Leave the clock comparator set up for the next timer
	 * tick if either rcu or a softirq is pending.
	 */
192 193
	if (rcu_needs_cpu(cpu) || local_softirq_pending()) {
		cpu_clear(cpu, nohz_cpu_mask);
L
Linus Torvalds 已提交
194 195 196 197 198 199 200
		return;
	}

	/*
	 * This cpu is going really idle. Set up the clock comparator
	 * for the next event.
	 */
201 202 203
	next = next_timer_interrupt();
	do {
		seq = read_seqbegin_irqsave(&xtime_lock, flags);
204
		timer = ((__u64) next) - ((__u64) jiffies) + jiffies_64;
205
	} while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
206 207 208 209 210 211 212 213
	todval = -1ULL;
	/* Be careful about overflows. */
	if (timer < (-1ULL / CLK_TICKS_PER_JIFFY)) {
		timer = jiffies_timer_cc + timer * CLK_TICKS_PER_JIFFY;
		if (timer >= jiffies_timer_cc)
			todval = timer;
	}
	asm volatile ("SCKC %0" : : "m" (todval));
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221
}

/*
 * Start the HZ tick on the current CPU.
 * Only cpu_idle may call this function.
 */
static inline void start_hz_timer(void)
{
H
Heiko Carstens 已提交
222 223
	BUG_ON(!in_interrupt());

L
Linus Torvalds 已提交
224 225
	if (!cpu_isset(smp_processor_id(), nohz_cpu_mask))
		return;
H
Heiko Carstens 已提交
226
	account_ticks();
L
Linus Torvalds 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	cpu_clear(smp_processor_id(), nohz_cpu_mask);
}

static int nohz_idle_notify(struct notifier_block *self,
			    unsigned long action, void *hcpu)
{
	switch (action) {
	case CPU_IDLE:
		stop_hz_timer();
		break;
	case CPU_NOT_IDLE:
		start_hz_timer();
		break;
	}
	return NOTIFY_OK;
}

static struct notifier_block nohz_idle_nb = {
	.notifier_call = nohz_idle_notify,
};

248
static void __init nohz_init(void)
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
{
	if (register_idle_notifier(&nohz_idle_nb))
		panic("Couldn't register idle notifier");
}

#endif

/*
 * Start the clock comparator on the current CPU.
 */
void init_cpu_timer(void)
{
	unsigned long cr0;
	__u64 timer;

	timer = jiffies_timer_cc + jiffies_64 * CLK_TICKS_PER_JIFFY;
	S390_lowcore.jiffy_timer = timer + CLK_TICKS_PER_JIFFY;
	timer += CLK_TICKS_PER_JIFFY + CPU_DEVIATION;
	asm volatile ("SCKC %0" : : "m" (timer));
        /* allow clock comparator timer interrupt */
	__ctl_store(cr0, 0, 0);
        cr0 |= 0x800;
	__ctl_load(cr0, 0, 0);
}

274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
static cycle_t read_tod_clock(void)
{
	return get_clock();
}

static struct clocksource clocksource_tod = {
	.name		= "tod",
	.rating		= 100,
	.read		= read_tod_clock,
	.mask		= -1ULL,
	.mult		= 1000,
	.shift		= 12,
	.is_continuous	= 1,
};


L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298 299
/*
 * Initialize the TOD clock and the CPU timer of
 * the boot cpu.
 */
void __init time_init(void)
{
	__u64 set_time_cc;
	int cc;

        /* kick the TOD clock */
300 301 302 303 304 305
	asm volatile(
		"	stck	0(%2)\n"
		"	ipm	%0\n"
		"	srl	%0,28"
		: "=d" (cc), "=m" (init_timer_cc)
		: "a" (&init_timer_cc) : "cc");
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        switch (cc) {
        case 0: /* clock in set state: all is fine */
                break;
        case 1: /* clock in non-set state: FIXME */
                printk("time_init: TOD clock in non-set state\n");
                break;
        case 2: /* clock in error state: FIXME */
                printk("time_init: TOD clock in error state\n");
                break;
        case 3: /* clock in stopped or not-operational state: FIXME */
                printk("time_init: TOD clock stopped/non-operational\n");
                break;
        }
	jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;

	/* set xtime */
	xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
	set_time_cc = init_timer_cc - 0x8126d60e46000000LL +
		(0x3c26700LL*1000000*4096);
        tod_to_timeval(set_time_cc, &xtime);
        set_normalized_timespec(&wall_to_monotonic,
                                -xtime.tv_sec, -xtime.tv_nsec);

	/* request the clock comparator external interrupt */
H
Heiko Carstens 已提交
330
	if (register_early_external_interrupt(0x1004, NULL,
L
Linus Torvalds 已提交
331 332 333
					      &ext_int_info_cc) != 0)
                panic("Couldn't request external interrupt 0x1004");

334 335 336
	if (clocksource_register(&clocksource_tod) != 0)
		panic("Could not register TOD clock source");

L
Linus Torvalds 已提交
337 338 339 340 341 342 343 344 345 346 347
        init_cpu_timer();

#ifdef CONFIG_NO_IDLE_HZ
	nohz_init();
#endif

#ifdef CONFIG_VIRT_TIMER
	vtime_init();
#endif
}