Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Generic sched_clock() support, to extend low level hardware time
0004  * counters to full 64-bit ns values.
0005  */
0006 #include <linux/clocksource.h>
0007 #include <linux/init.h>
0008 #include <linux/jiffies.h>
0009 #include <linux/ktime.h>
0010 #include <linux/kernel.h>
0011 #include <linux/math.h>
0012 #include <linux/moduleparam.h>
0013 #include <linux/sched.h>
0014 #include <linux/sched/clock.h>
0015 #include <linux/syscore_ops.h>
0016 #include <linux/hrtimer.h>
0017 #include <linux/sched_clock.h>
0018 #include <linux/seqlock.h>
0019 #include <linux/bitops.h>
0020 
0021 #include "timekeeping.h"
0022 
0023 /**
0024  * struct clock_data - all data needed for sched_clock() (including
0025  *                     registration of a new clock source)
0026  *
0027  * @seq:        Sequence counter for protecting updates. The lowest
0028  *          bit is the index for @read_data.
0029  * @read_data:      Data required to read from sched_clock.
0030  * @wrap_kt:        Duration for which clock can run before wrapping.
0031  * @rate:       Tick rate of the registered clock.
0032  * @actual_read_sched_clock: Registered hardware level clock read function.
0033  *
0034  * The ordering of this structure has been chosen to optimize cache
0035  * performance. In particular 'seq' and 'read_data[0]' (combined) should fit
0036  * into a single 64-byte cache line.
0037  */
0038 struct clock_data {
0039     seqcount_latch_t    seq;
0040     struct clock_read_data  read_data[2];
0041     ktime_t         wrap_kt;
0042     unsigned long       rate;
0043 
0044     u64 (*actual_read_sched_clock)(void);
0045 };
0046 
0047 static struct hrtimer sched_clock_timer;
0048 static int irqtime = -1;
0049 
0050 core_param(irqtime, irqtime, int, 0400);
0051 
0052 static u64 notrace jiffy_sched_clock_read(void)
0053 {
0054     /*
0055      * We don't need to use get_jiffies_64 on 32-bit arches here
0056      * because we register with BITS_PER_LONG
0057      */
0058     return (u64)(jiffies - INITIAL_JIFFIES);
0059 }
0060 
0061 static struct clock_data cd ____cacheline_aligned = {
0062     .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
0063               .read_sched_clock = jiffy_sched_clock_read, },
0064     .actual_read_sched_clock = jiffy_sched_clock_read,
0065 };
0066 
0067 static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
0068 {
0069     return (cyc * mult) >> shift;
0070 }
0071 
0072 notrace struct clock_read_data *sched_clock_read_begin(unsigned int *seq)
0073 {
0074     *seq = raw_read_seqcount_latch(&cd.seq);
0075     return cd.read_data + (*seq & 1);
0076 }
0077 
0078 notrace int sched_clock_read_retry(unsigned int seq)
0079 {
0080     return read_seqcount_latch_retry(&cd.seq, seq);
0081 }
0082 
0083 unsigned long long notrace sched_clock(void)
0084 {
0085     u64 cyc, res;
0086     unsigned int seq;
0087     struct clock_read_data *rd;
0088 
0089     do {
0090         rd = sched_clock_read_begin(&seq);
0091 
0092         cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
0093               rd->sched_clock_mask;
0094         res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
0095     } while (sched_clock_read_retry(seq));
0096 
0097     return res;
0098 }
0099 
0100 /*
0101  * Updating the data required to read the clock.
0102  *
0103  * sched_clock() will never observe mis-matched data even if called from
0104  * an NMI. We do this by maintaining an odd/even copy of the data and
0105  * steering sched_clock() to one or the other using a sequence counter.
0106  * In order to preserve the data cache profile of sched_clock() as much
0107  * as possible the system reverts back to the even copy when the update
0108  * completes; the odd copy is used *only* during an update.
0109  */
0110 static void update_clock_read_data(struct clock_read_data *rd)
0111 {
0112     /* update the backup (odd) copy with the new data */
0113     cd.read_data[1] = *rd;
0114 
0115     /* steer readers towards the odd copy */
0116     raw_write_seqcount_latch(&cd.seq);
0117 
0118     /* now its safe for us to update the normal (even) copy */
0119     cd.read_data[0] = *rd;
0120 
0121     /* switch readers back to the even copy */
0122     raw_write_seqcount_latch(&cd.seq);
0123 }
0124 
0125 /*
0126  * Atomically update the sched_clock() epoch.
0127  */
0128 static void update_sched_clock(void)
0129 {
0130     u64 cyc;
0131     u64 ns;
0132     struct clock_read_data rd;
0133 
0134     rd = cd.read_data[0];
0135 
0136     cyc = cd.actual_read_sched_clock();
0137     ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
0138 
0139     rd.epoch_ns = ns;
0140     rd.epoch_cyc = cyc;
0141 
0142     update_clock_read_data(&rd);
0143 }
0144 
0145 static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
0146 {
0147     update_sched_clock();
0148     hrtimer_forward_now(hrt, cd.wrap_kt);
0149 
0150     return HRTIMER_RESTART;
0151 }
0152 
0153 void __init
0154 sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
0155 {
0156     u64 res, wrap, new_mask, new_epoch, cyc, ns;
0157     u32 new_mult, new_shift;
0158     unsigned long r, flags;
0159     char r_unit;
0160     struct clock_read_data rd;
0161 
0162     if (cd.rate > rate)
0163         return;
0164 
0165     /* Cannot register a sched_clock with interrupts on */
0166     local_irq_save(flags);
0167 
0168     /* Calculate the mult/shift to convert counter ticks to ns. */
0169     clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
0170 
0171     new_mask = CLOCKSOURCE_MASK(bits);
0172     cd.rate = rate;
0173 
0174     /* Calculate how many nanosecs until we risk wrapping */
0175     wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
0176     cd.wrap_kt = ns_to_ktime(wrap);
0177 
0178     rd = cd.read_data[0];
0179 
0180     /* Update epoch for new counter and update 'epoch_ns' from old counter*/
0181     new_epoch = read();
0182     cyc = cd.actual_read_sched_clock();
0183     ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
0184     cd.actual_read_sched_clock = read;
0185 
0186     rd.read_sched_clock = read;
0187     rd.sched_clock_mask = new_mask;
0188     rd.mult         = new_mult;
0189     rd.shift        = new_shift;
0190     rd.epoch_cyc        = new_epoch;
0191     rd.epoch_ns     = ns;
0192 
0193     update_clock_read_data(&rd);
0194 
0195     if (sched_clock_timer.function != NULL) {
0196         /* update timeout for clock wrap */
0197         hrtimer_start(&sched_clock_timer, cd.wrap_kt,
0198                   HRTIMER_MODE_REL_HARD);
0199     }
0200 
0201     r = rate;
0202     if (r >= 4000000) {
0203         r = DIV_ROUND_CLOSEST(r, 1000000);
0204         r_unit = 'M';
0205     } else if (r >= 4000) {
0206         r = DIV_ROUND_CLOSEST(r, 1000);
0207         r_unit = 'k';
0208     } else {
0209         r_unit = ' ';
0210     }
0211 
0212     /* Calculate the ns resolution of this counter */
0213     res = cyc_to_ns(1ULL, new_mult, new_shift);
0214 
0215     pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
0216         bits, r, r_unit, res, wrap);
0217 
0218     /* Enable IRQ time accounting if we have a fast enough sched_clock() */
0219     if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
0220         enable_sched_clock_irqtime();
0221 
0222     local_irq_restore(flags);
0223 
0224     pr_debug("Registered %pS as sched_clock source\n", read);
0225 }
0226 
0227 void __init generic_sched_clock_init(void)
0228 {
0229     /*
0230      * If no sched_clock() function has been provided at that point,
0231      * make it the final one.
0232      */
0233     if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
0234         sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
0235 
0236     update_sched_clock();
0237 
0238     /*
0239      * Start the timer to keep sched_clock() properly updated and
0240      * sets the initial epoch.
0241      */
0242     hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
0243     sched_clock_timer.function = sched_clock_poll;
0244     hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
0245 }
0246 
0247 /*
0248  * Clock read function for use when the clock is suspended.
0249  *
0250  * This function makes it appear to sched_clock() as if the clock
0251  * stopped counting at its last update.
0252  *
0253  * This function must only be called from the critical
0254  * section in sched_clock(). It relies on the read_seqcount_retry()
0255  * at the end of the critical section to be sure we observe the
0256  * correct copy of 'epoch_cyc'.
0257  */
0258 static u64 notrace suspended_sched_clock_read(void)
0259 {
0260     unsigned int seq = raw_read_seqcount_latch(&cd.seq);
0261 
0262     return cd.read_data[seq & 1].epoch_cyc;
0263 }
0264 
0265 int sched_clock_suspend(void)
0266 {
0267     struct clock_read_data *rd = &cd.read_data[0];
0268 
0269     update_sched_clock();
0270     hrtimer_cancel(&sched_clock_timer);
0271     rd->read_sched_clock = suspended_sched_clock_read;
0272 
0273     return 0;
0274 }
0275 
0276 void sched_clock_resume(void)
0277 {
0278     struct clock_read_data *rd = &cd.read_data[0];
0279 
0280     rd->epoch_cyc = cd.actual_read_sched_clock();
0281     hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL_HARD);
0282     rd->read_sched_clock = cd.actual_read_sched_clock;
0283 }
0284 
0285 static struct syscore_ops sched_clock_ops = {
0286     .suspend    = sched_clock_suspend,
0287     .resume     = sched_clock_resume,
0288 };
0289 
0290 static int __init sched_clock_syscore_init(void)
0291 {
0292     register_syscore_ops(&sched_clock_ops);
0293 
0294     return 0;
0295 }
0296 device_initcall(sched_clock_syscore_init);