time.h 4.8 KB
Newer Older
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
/*
 * Common time prototypes and such for all ppc machines.
 *
 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
 * Paul Mackerras' version and mine for PReP and Pmac.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#ifndef __POWERPC_TIME_H
#define __POWERPC_TIME_H

#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/percpu.h>

#include <asm/processor.h>

/* time.c */
extern unsigned long tb_ticks_per_jiffy;
extern unsigned long tb_ticks_per_usec;
extern unsigned long tb_ticks_per_sec;
26
extern struct clock_event_device decrementer_clockevent;
27 28 29

struct rtc_time;
extern void to_tm(int tim, struct rtc_time * tm);
30
extern void tick_broadcast_ipi_handler(void);
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

extern void generic_calibrate_decr(void);

/* Some sane defaults: 125 MHz timebase, 1GHz processor */
extern unsigned long ppc_proc_freq;
#define DEFAULT_PROC_FREQ	(DEFAULT_TB_FREQ * 8)
extern unsigned long ppc_tb_freq;
#define DEFAULT_TB_FREQ		125000000UL

struct div_result {
	u64 result_high;
	u64 result_low;
};

/* Accessor functions for the timebase (RTC on 601) registers. */
/* If one day CONFIG_POWER is added just define __USE_RTC as 1 */
#ifdef CONFIG_6xx
48
#define __USE_RTC()	(!cpu_has_feature(CPU_FTR_USE_TB))
49 50 51 52
#else
#define __USE_RTC()	0
#endif

53 54 55 56 57 58 59
#ifdef CONFIG_PPC64

/* For compatibility, get_tbl() is defined as get_tb() on ppc64 */
#define get_tbl		get_tb

#else

60 61 62
static inline unsigned long get_tbl(void)
{
#if defined(CONFIG_403GCX)
63
	unsigned long tbl;
64
	asm volatile("mfspr %0, 0x3dd" : "=r" (tbl));
65
	return tbl;
66
#else
67
	return mftbl();
68 69 70 71 72
#endif
}

static inline unsigned int get_tbu(void)
{
73
#ifdef CONFIG_403GCX
74 75
	unsigned int tbu;
	asm volatile("mfspr %0, 0x3dc" : "=r" (tbu));
76
	return tbu;
77
#else
78
	return mftbu();
79 80
#endif
}
81
#endif /* !CONFIG_PPC64 */
82 83 84 85 86 87 88 89 90

static inline unsigned int get_rtcl(void)
{
	unsigned int rtcl;

	asm volatile("mfrtcl %0" : "=r" (rtcl));
	return rtcl;
}

91 92 93 94 95 96 97 98 99 100 101
static inline u64 get_rtc(void)
{
	unsigned int hi, lo, hi2;

	do {
		asm volatile("mfrtcu %0; mfrtcl %1; mfrtcu %2"
			     : "=r" (hi), "=r" (lo), "=r" (hi2));
	} while (hi2 != hi);
	return (u64)hi * 1000000000 + lo;
}

102 103 104 105 106 107 108 109 110
static inline u64 get_vtb(void)
{
#ifdef CONFIG_PPC_BOOK3S_64
	if (cpu_has_feature(CPU_FTR_ARCH_207S))
		return mfvtb();
#endif
	return 0;
}

111 112 113 114 115
#ifdef CONFIG_PPC64
static inline u64 get_tb(void)
{
	return mftb();
}
116
#else /* CONFIG_PPC64 */
117 118 119 120 121 122 123 124 125 126 127 128
static inline u64 get_tb(void)
{
	unsigned int tbhi, tblo, tbhi2;

	do {
		tbhi = get_tbu();
		tblo = get_tbl();
		tbhi2 = get_tbu();
	} while (tbhi != tbhi2);

	return ((u64)tbhi << 32) | tblo;
}
129
#endif /* !CONFIG_PPC64 */
130

131 132 133 134 135
static inline u64 get_tb_or_rtc(void)
{
	return __USE_RTC() ? get_rtc() : get_tb();
}

136 137 138 139 140 141 142 143 144 145 146 147 148
static inline void set_tb(unsigned int upper, unsigned int lower)
{
	mtspr(SPRN_TBWL, 0);
	mtspr(SPRN_TBWU, upper);
	mtspr(SPRN_TBWL, lower);
}

/* Accessor functions for the decrementer register.
 * The 4xx doesn't even have a decrementer.  I tried to use the
 * generic timer interrupt code, which seems OK, with the 4xx PIT
 * in auto-reload mode.  The problem is PIT stops counting when it
 * hits zero.  If it would wrap, we could use it just like a decrementer.
 */
149
static inline u64 get_dec(void)
150 151 152 153 154 155 156 157
{
#if defined(CONFIG_40x)
	return (mfspr(SPRN_PIT));
#else
	return (mfspr(SPRN_DEC));
#endif
}

158 159 160 161 162
/*
 * Note: Book E and 4xx processors differ from other PowerPC processors
 * in when the decrementer generates its interrupt: on the 1 to 0
 * transition for Book E/4xx, but on the 0 to -1 transition for others.
 */
163
static inline void set_dec(u64 val)
164 165
{
#if defined(CONFIG_40x)
166
	mtspr(SPRN_PIT, (u32) val);
167
#else
168 169
#ifndef CONFIG_BOOKE
	--val;
170
#endif
171
	mtspr(SPRN_DEC, val);
172
#endif /* not 40x */
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
}

static inline unsigned long tb_ticks_since(unsigned long tstamp)
{
	if (__USE_RTC()) {
		int delta = get_rtcl() - (unsigned int) tstamp;
		return delta < 0 ? delta + 1000000000 : delta;
	}
	return get_tbl() - tstamp;
}

#define mulhwu(x,y) \
({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})

#ifdef CONFIG_PPC64
#define mulhdu(x,y) \
({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
#else
extern u64 mulhdu(u64, u64);
#endif

194 195
extern void div128_by_32(u64 dividend_high, u64 dividend_low,
			 unsigned divisor, struct div_result *dr);
196 197 198 199 200 201 202 203 204

/* Used to store Processor Utilization register (purr) values */

struct cpu_usage {
        u64 current_tb;  /* Holds the current purr register values */
};

DECLARE_PER_CPU(struct cpu_usage, cpu_usage_array);

205
extern void secondary_cpu_time_init(void);
206

207
DECLARE_PER_CPU(u64, decrementers_next_tb);
208

209 210 211
/* Convert timebase ticks to nanoseconds */
unsigned long long tb_to_ns(unsigned long long tb_ticks);

212
#endif /* __KERNEL__ */
213
#endif /* __POWERPC_TIME_H */