^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) * Generic sched_clock() support, to extend low level hardware time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * counters to full 64-bit ns values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/clocksource.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/init.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/ktime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/sched/clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/syscore_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/hrtimer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched_clock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/seqlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <trace/hooks/epoch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "timekeeping.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * struct clock_data - all data needed for sched_clock() (including
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * registration of a new clock source)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * @seq: Sequence counter for protecting updates. The lowest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * bit is the index for @read_data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @read_data: Data required to read from sched_clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * @wrap_kt: Duration for which clock can run before wrapping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * @rate: Tick rate of the registered clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * @actual_read_sched_clock: Registered hardware level clock read function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * The ordering of this structure has been chosen to optimize cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * performance. In particular 'seq' and 'read_data[0]' (combined) should fit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * into a single 64-byte cache line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct clock_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) seqcount_latch_t seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct clock_read_data read_data[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ktime_t wrap_kt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) unsigned long rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) u64 (*actual_read_sched_clock)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static struct hrtimer sched_clock_timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int irqtime = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) core_param(irqtime, irqtime, int, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static u64 notrace jiffy_sched_clock_read(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * We don't need to use get_jiffies_64 on 32-bit arches here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * because we register with BITS_PER_LONG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return (u64)(jiffies - INITIAL_JIFFIES);
^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) static struct clock_data cd ____cacheline_aligned = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) .read_sched_clock = jiffy_sched_clock_read, },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .actual_read_sched_clock = jiffy_sched_clock_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return (cyc * mult) >> shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) notrace struct clock_read_data *sched_clock_read_begin(unsigned int *seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) *seq = raw_read_seqcount_latch(&cd.seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return cd.read_data + (*seq & 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) notrace int sched_clock_read_retry(unsigned int seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return read_seqcount_latch_retry(&cd.seq, seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) unsigned long long notrace sched_clock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) u64 cyc, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned int seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct clock_read_data *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) rd = sched_clock_read_begin(&seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) rd->sched_clock_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) } while (sched_clock_read_retry(seq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^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) * Updating the data required to read the clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * sched_clock() will never observe mis-matched data even if called from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * an NMI. We do this by maintaining an odd/even copy of the data and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * steering sched_clock() to one or the other using a sequence counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * In order to preserve the data cache profile of sched_clock() as much
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) * as possible the system reverts back to the even copy when the update
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) * completes; the odd copy is used *only* during an update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static void update_clock_read_data(struct clock_read_data *rd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* update the backup (odd) copy with the new data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) cd.read_data[1] = *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /* steer readers towards the odd copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) raw_write_seqcount_latch(&cd.seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) /* now its safe for us to update the normal (even) copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) cd.read_data[0] = *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) /* switch readers back to the even copy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) raw_write_seqcount_latch(&cd.seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * Atomically update the sched_clock() epoch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) static void update_sched_clock(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u64 cyc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) u64 ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct clock_read_data rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) rd = cd.read_data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) cyc = cd.actual_read_sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) rd.epoch_ns = ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) rd.epoch_cyc = cyc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) update_clock_read_data(&rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) update_sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) hrtimer_forward_now(hrt, cd.wrap_kt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return HRTIMER_RESTART;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) u64 res, wrap, new_mask, new_epoch, cyc, ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) u32 new_mult, new_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) unsigned long r, flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) char r_unit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct clock_read_data rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (cd.rate > rate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* Cannot register a sched_clock with interrupts on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Calculate the mult/shift to convert counter ticks to ns. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) new_mask = CLOCKSOURCE_MASK(bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) cd.rate = rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* Calculate how many nanosecs until we risk wrapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) cd.wrap_kt = ns_to_ktime(wrap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) rd = cd.read_data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /* Update epoch for new counter and update 'epoch_ns' from old counter*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) new_epoch = read();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) cyc = cd.actual_read_sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) cd.actual_read_sched_clock = read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) rd.read_sched_clock = read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) rd.sched_clock_mask = new_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) rd.mult = new_mult;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) rd.shift = new_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) rd.epoch_cyc = new_epoch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) rd.epoch_ns = ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) update_clock_read_data(&rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (sched_clock_timer.function != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /* update timeout for clock wrap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) hrtimer_start(&sched_clock_timer, cd.wrap_kt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) r = rate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (r >= 4000000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) r /= 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) r_unit = 'M';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (r >= 1000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) r /= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) r_unit = 'k';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) r_unit = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) /* Calculate the ns resolution of this counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) res = cyc_to_ns(1ULL, new_mult, new_shift);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) bits, r, r_unit, res, wrap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) /* Enable IRQ time accounting if we have a fast enough sched_clock() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) enable_sched_clock_irqtime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) pr_debug("Registered %pS as sched_clock source\n", read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) EXPORT_SYMBOL_GPL(sched_clock_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) void __init generic_sched_clock_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * If no sched_clock() function has been provided at that point,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * make it the final one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) update_sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * Start the timer to keep sched_clock() properly updated and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * sets the initial epoch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) sched_clock_timer.function = sched_clock_poll;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Clock read function for use when the clock is suspended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * This function makes it appear to sched_clock() as if the clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * stopped counting at its last update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * This function must only be called from the critical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * section in sched_clock(). It relies on the read_seqcount_retry()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * at the end of the critical section to be sure we observe the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * correct copy of 'epoch_cyc'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static u64 notrace suspended_sched_clock_read(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) unsigned int seq = raw_read_seqcount_latch(&cd.seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return cd.read_data[seq & 1].epoch_cyc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int sched_clock_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) struct clock_read_data *rd = &cd.read_data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) update_sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) hrtimer_cancel(&sched_clock_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) rd->read_sched_clock = suspended_sched_clock_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) trace_android_vh_show_suspend_epoch_val(rd->epoch_ns, rd->epoch_cyc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) void sched_clock_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct clock_read_data *rd = &cd.read_data[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) rd->epoch_cyc = cd.actual_read_sched_clock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) rd->read_sched_clock = cd.actual_read_sched_clock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) trace_android_vh_show_resume_epoch_val(rd->epoch_cyc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static struct syscore_ops sched_clock_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) .suspend = sched_clock_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) .resume = sched_clock_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static int __init sched_clock_syscore_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) register_syscore_ops(&sched_clock_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) device_initcall(sched_clock_syscore_init);