Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

parisc: Add native high-resolution sched_clock() implementation

Add a native implementation for the sched_clock() function which utilizes the
processor-internal cycle counter (Control Register 16) as high-resolution time
source.

With this patch we now get much more fine-grained resolutions in various
in-kernel time measurements (e.g. when viewing the function tracing logs), and
probably a more accurate scheduling on SMP systems.

There are a few specific implementation details in this patch:

1. On a 32bit kernel we emulate the higher 32bits of the required 64-bit
resolution of sched_clock() by increasing a per-cpu counter at every
wrap-around of the 32bit cycle counter.

2. In a SMP system, the cycle counters of the various CPUs are not syncronized
(similiar to the TSC in a x86_64 system). To cope with this we define
HAVE_UNSTABLE_SCHED_CLOCK and let the upper layers do the adjustment work.

3. Since we need HAVE_UNSTABLE_SCHED_CLOCK, we need to provide a cmpxchg64()
function even on a 32-bit kernel.

4. A 64-bit SMP kernel which is started on a UP system will mark the
sched_clock() implementation as "stable", which means that we don't expect any
jumps in the returned counter. This is true because we then run only on one
CPU.

Signed-off-by: Helge Deller <deller@gmx.de>

+70 -9
+1
arch/parisc/Kconfig
··· 33 33 select HAVE_ARCH_AUDITSYSCALL 34 34 select HAVE_ARCH_SECCOMP_FILTER 35 35 select HAVE_ARCH_TRACEHOOK 36 + select HAVE_UNSTABLE_SCHED_CLOCK if (SMP || !64BIT) 36 37 select ARCH_NO_COHERENT_DMA_MMAP 37 38 select CPU_NO_EFFICIENT_FFS 38 39
+5 -4
arch/parisc/include/asm/cmpxchg.h
··· 52 52 /* __cmpxchg_u32/u64 defined in arch/parisc/lib/bitops.c */ 53 53 extern unsigned long __cmpxchg_u32(volatile unsigned int *m, unsigned int old, 54 54 unsigned int new_); 55 - extern unsigned long __cmpxchg_u64(volatile unsigned long *ptr, 56 - unsigned long old, unsigned long new_); 55 + extern u64 __cmpxchg_u64(volatile u64 *ptr, u64 old, u64 new_); 57 56 58 57 /* don't worry...optimizer will get rid of most of this */ 59 58 static inline unsigned long ··· 60 61 { 61 62 switch (size) { 62 63 #ifdef CONFIG_64BIT 63 - case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_); 64 + case 8: return __cmpxchg_u64((u64 *)ptr, old, new_); 64 65 #endif 65 66 case 4: return __cmpxchg_u32((unsigned int *)ptr, 66 67 (unsigned int)old, (unsigned int)new_); ··· 85 86 { 86 87 switch (size) { 87 88 #ifdef CONFIG_64BIT 88 - case 8: return __cmpxchg_u64((unsigned long *)ptr, old, new_); 89 + case 8: return __cmpxchg_u64((u64 *)ptr, old, new_); 89 90 #endif 90 91 case 4: return __cmpxchg_u32(ptr, old, new_); 91 92 default: ··· 109 110 #else 110 111 #define cmpxchg64_local(ptr, o, n) __cmpxchg64_local_generic((ptr), (o), (n)) 111 112 #endif 113 + 114 + #define cmpxchg64(ptr, o, n) __cmpxchg_u64(ptr, o, n) 112 115 113 116 #endif /* _ASM_PARISC_CMPXCHG_H_ */
+62 -1
arch/parisc/kernel/time.c
··· 38 38 39 39 static unsigned long clocktick __read_mostly; /* timer cycles per tick */ 40 40 41 + #ifndef CONFIG_64BIT 42 + /* 43 + * The processor-internal cycle counter (Control Register 16) is used as time 44 + * source for the sched_clock() function. This register is 64bit wide on a 45 + * 64-bit kernel and 32bit on a 32-bit kernel. Since sched_clock() always 46 + * requires a 64bit counter we emulate on the 32-bit kernel the higher 32bits 47 + * with a per-cpu variable which we increase every time the counter 48 + * wraps-around (which happens every ~4 secounds). 49 + */ 50 + static DEFINE_PER_CPU(unsigned long, cr16_high_32_bits); 51 + #endif 52 + 41 53 /* 42 54 * We keep time on PA-RISC Linux by using the Interval Timer which is 43 55 * a pair of registers; one is read-only and one is write-only; both ··· 119 107 * Only bottom 32-bits of next_tick are writable in CR16! 120 108 */ 121 109 mtctl(next_tick, 16); 110 + 111 + #if !defined(CONFIG_64BIT) 112 + /* check for overflow on a 32bit kernel (every ~4 seconds). */ 113 + if (unlikely(next_tick < now)) 114 + this_cpu_inc(cr16_high_32_bits); 115 + #endif 122 116 123 117 /* Skip one clocktick on purpose if we missed next_tick. 124 118 * The new CR16 must be "later" than current CR16 otherwise ··· 237 219 unsigned int cpu = smp_processor_id(); 238 220 unsigned long next_tick = mfctl(16) + clocktick; 239 221 222 + #if defined(CONFIG_HAVE_UNSTABLE_SCHED_CLOCK) && defined(CONFIG_64BIT) 223 + /* With multiple 64bit CPUs online, the cr16's are not syncronized. */ 224 + if (cpu != 0) 225 + clear_sched_clock_stable(); 226 + #endif 227 + 240 228 mtctl(next_tick, 16); /* kick off Interval Timer (CR16) */ 241 229 242 230 per_cpu(cpu_data, cpu).it_value = next_tick; ··· 270 246 } 271 247 } 272 248 249 + 250 + /* 251 + * sched_clock() framework 252 + */ 253 + 254 + static u32 cyc2ns_mul __read_mostly; 255 + static u32 cyc2ns_shift __read_mostly; 256 + 257 + u64 sched_clock(void) 258 + { 259 + u64 now; 260 + 261 + /* Get current cycle counter (Control Register 16). */ 262 + #ifdef CONFIG_64BIT 263 + now = mfctl(16); 264 + #else 265 + now = mfctl(16) + (((u64) this_cpu_read(cr16_high_32_bits)) << 32); 266 + #endif 267 + 268 + /* return the value in ns (cycles_2_ns) */ 269 + return mul_u64_u32_shr(now, cyc2ns_mul, cyc2ns_shift); 270 + } 271 + 272 + 273 + /* 274 + * timer interrupt and sched_clock() initialization 275 + */ 276 + 273 277 void __init time_init(void) 274 278 { 275 279 unsigned long current_cr16_khz; 276 280 281 + current_cr16_khz = PAGE0->mem_10msec/10; /* kHz */ 277 282 clocktick = (100 * PAGE0->mem_10msec) / HZ; 283 + 284 + /* calculate mult/shift values for cr16 */ 285 + clocks_calc_mult_shift(&cyc2ns_mul, &cyc2ns_shift, current_cr16_khz, 286 + NSEC_PER_MSEC, 0); 287 + 288 + #if defined(CONFIG_HAVE_UNSTABLE_SCHED_CLOCK) && defined(CONFIG_64BIT) 289 + /* At bootup only one 64bit CPU is online and cr16 is "stable" */ 290 + set_sched_clock_stable(); 291 + #endif 278 292 279 293 start_cpu_itimer(); /* get CPU 0 started */ 280 294 281 295 /* register at clocksource framework */ 282 - current_cr16_khz = PAGE0->mem_10msec/10; /* kHz */ 283 296 clocksource_register_khz(&clocksource_cr16, current_cr16_khz); 284 297 }
+2 -4
arch/parisc/lib/bitops.c
··· 55 55 } 56 56 57 57 58 - #ifdef CONFIG_64BIT 59 - unsigned long __cmpxchg_u64(volatile unsigned long *ptr, unsigned long old, unsigned long new) 58 + u64 __cmpxchg_u64(volatile u64 *ptr, u64 old, u64 new) 60 59 { 61 60 unsigned long flags; 62 - unsigned long prev; 61 + u64 prev; 63 62 64 63 _atomic_spin_lock_irqsave(ptr, flags); 65 64 if ((prev = *ptr) == old) ··· 66 67 _atomic_spin_unlock_irqrestore(ptr, flags); 67 68 return prev; 68 69 } 69 - #endif 70 70 71 71 unsigned long __cmpxchg_u32(volatile unsigned int *ptr, unsigned int old, unsigned int new) 72 72 {