Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * This file contains the jiffies based clocksource.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <linux/clocksource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "timekeeping.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /* Since jiffies uses a simple TICK_NSEC multiplier
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * conversion, the .shift value could be zero. However
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * this would make NTP adjustments impossible as they are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * shift both the nominator and denominator the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * amount, and give ntp adjustments in units of 1/2^8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * The value 8 is somewhat carefully chosen, as anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * larger can result in overflows. TICK_NSEC grows as HZ
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * shrinks, so values greater than 8 overflow 32bits when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * HZ=100.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #if HZ < 34
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define JIFFIES_SHIFT	6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #elif HZ < 67
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #define JIFFIES_SHIFT	7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define JIFFIES_SHIFT	8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) static u64 jiffies_read(struct clocksource *cs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	return (u64) jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * The Jiffies based clocksource is the lowest common
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * denominator clock source which should function on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * all systems. It has the same coarse resolution as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * the timer interrupt frequency HZ and it suffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * inaccuracies caused by missed or lost timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * interrupts and the inability for the timer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * interrupt hardware to accuratly tick at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * requested HZ value. It is also not recommended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * for "tick-less" systems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) static struct clocksource clocksource_jiffies = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	.name		= "jiffies",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	.rating		= 1, /* lowest valid rating*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	.read		= jiffies_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	.mask		= CLOCKSOURCE_MASK(32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	.mult		= TICK_NSEC << JIFFIES_SHIFT, /* details above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	.shift		= JIFFIES_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	.max_cycles	= 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) __cacheline_aligned_in_smp seqcount_t jiffies_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) #if (BITS_PER_LONG < 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) u64 get_jiffies_64(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	u64 ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		seq = read_seqcount_begin(&jiffies_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		ret = jiffies_64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	} while (read_seqcount_retry(&jiffies_seq, seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) EXPORT_SYMBOL(get_jiffies_64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) EXPORT_SYMBOL(jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) static int __init init_jiffies_clocksource(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	return __clocksource_register(&clocksource_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) core_initcall(init_jiffies_clocksource);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) struct clocksource * __init __weak clocksource_default_clock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	return &clocksource_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) static struct clocksource refined_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) int register_refined_jiffies(long cycles_per_second)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	u64 nsec_per_tick, shift_hz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	long cycles_per_tick;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	refined_jiffies = clocksource_jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	refined_jiffies.name = "refined-jiffies";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	refined_jiffies.rating++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	/* Calc cycles per tick */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	cycles_per_tick = (cycles_per_second + HZ/2)/HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	/* shift_hz stores hz<<8 for extra accuracy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	shift_hz = (u64)cycles_per_second << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	shift_hz += cycles_per_tick/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	do_div(shift_hz, cycles_per_tick);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	/* Calculate nsec_per_tick using shift_hz */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	nsec_per_tick = (u64)NSEC_PER_SEC << 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	nsec_per_tick += (u32)shift_hz/2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	do_div(nsec_per_tick, (u32)shift_hz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	__clocksource_register(&refined_jiffies);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }