blob: b67d93a609a2d6e331b4a06f696e6ee80d5796de [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Common time routines among all ppc machines.
4 *
5 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6 * Paul Mackerras' version and mine for PReP and Pmac.
7 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
8 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
9 *
10 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
11 * to make clock more stable (2.4.0-test5). The only thing
12 * that this code assumes is that the timebases have been synchronized
13 * by firmware on SMP and are never stopped (never do sleep
14 * on SMP then, nap and doze are OK).
15 *
16 * Speeded up do_gettimeofday by getting rid of references to
17 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
18 *
19 * TODO (not necessarily in this file):
20 * - improve precision and reproducibility of timebase frequency
Stephen Rothwellf5339272012-03-15 18:18:00 +000021 * measurement at boot time.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 * - for astronomical applications: add a new function to get
23 * non ambiguous timestamps even around leap seconds. This needs
24 * a new timestamp format and a good name.
25 *
26 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
27 * "A Kernel Model for Precision Timekeeping" by Dave Mills
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/errno.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -040031#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/sched.h>
Ingo Molnare6017572017-02-01 16:36:40 +010033#include <linux/sched/clock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/kernel.h>
35#include <linux/param.h>
36#include <linux/string.h>
37#include <linux/mm.h>
38#include <linux/interrupt.h>
39#include <linux/timex.h>
40#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/time.h>
42#include <linux/init.h>
43#include <linux/profile.h>
44#include <linux/cpu.h>
45#include <linux/security.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100046#include <linux/percpu.h>
47#include <linux/rtc.h>
Paul Mackerras092b8f32006-02-20 10:38:56 +110048#include <linux/jiffies.h>
Paul Mackerrasc6622f62006-02-24 10:06:59 +110049#include <linux/posix-timers.h>
David Howells7d12e782006-10-05 14:55:46 +010050#include <linux/irq.h>
Benjamin Herrenschmidt177996e2009-06-09 21:12:00 +000051#include <linux/delay.h>
Peter Zijlstrae360adb2010-10-14 14:01:34 +080052#include <linux/irq_work.h>
Geert Uytterhoeven60083062020-02-13 09:38:04 +010053#include <linux/of_clk.h>
Daniel Axtens7f92bc52016-01-06 11:45:51 +110054#include <linux/suspend.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010055#include <linux/sched/cputime.h>
Pingfan Liub709e322020-10-22 14:51:19 +080056#include <linux/sched/clock.h>
Nicholas Piggin4e287e62017-06-06 23:08:32 +100057#include <linux/processor.h>
Anton Blanchard6795b852009-10-26 18:49:14 +000058#include <asm/trace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Nicholas Piggin3a965702021-01-30 23:08:38 +100060#include <asm/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <asm/nvram.h>
63#include <asm/cache.h>
64#include <asm/machdep.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080065#include <linux/uaccess.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100066#include <asm/time.h>
67#include <asm/prom.h>
68#include <asm/irq.h>
69#include <asm/div64.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110070#include <asm/smp.h>
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +110071#include <asm/vdso_datapage.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100072#include <asm/firmware.h>
Daniel Axtens0545d542016-09-06 15:32:43 +100073#include <asm/asm-prototypes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Tony Breeds4a4cfe32007-09-22 07:35:52 +100075/* powerpc clocksource/clockevent code */
76
Tony Breedsd831d0b2007-09-21 13:26:03 +100077#include <linux/clockchips.h>
John Stultz189374a2012-09-04 15:27:48 -040078#include <linux/timekeeper_internal.h>
Tony Breeds4a4cfe32007-09-22 07:35:52 +100079
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010080static u64 timebase_read(struct clocksource *);
Tony Breeds4a4cfe32007-09-22 07:35:52 +100081static struct clocksource clocksource_timebase = {
82 .name = "timebase",
83 .rating = 400,
84 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
85 .mask = CLOCKSOURCE_MASK(64),
Tony Breeds4a4cfe32007-09-22 07:35:52 +100086 .read = timebase_read,
Christophe Leroyab037dd2020-11-27 00:10:05 +110087 .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER,
Tony Breeds4a4cfe32007-09-22 07:35:52 +100088};
89
Oliver O'Halloran799010242016-07-01 16:20:39 +100090#define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
91u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
Tony Breedsd831d0b2007-09-21 13:26:03 +100092
93static int decrementer_set_next_event(unsigned long evt,
94 struct clock_event_device *dev);
Viresh Kumar37a13e72015-07-16 16:56:25 +053095static int decrementer_shutdown(struct clock_event_device *evt);
Tony Breedsd831d0b2007-09-21 13:26:03 +100096
Bharat Bhushan6e359942012-04-18 06:01:19 +000097struct clock_event_device decrementer_clockevent = {
Viresh Kumar37a13e72015-07-16 16:56:25 +053098 .name = "decrementer",
99 .rating = 200,
100 .irq = 0,
101 .set_next_event = decrementer_set_next_event,
Anton Blanchard817593602018-10-02 09:01:05 +1000102 .set_state_oneshot_stopped = decrementer_shutdown,
Viresh Kumar37a13e72015-07-16 16:56:25 +0530103 .set_state_shutdown = decrementer_shutdown,
104 .tick_resume = decrementer_shutdown,
105 .features = CLOCK_EVT_FEAT_ONESHOT |
106 CLOCK_EVT_FEAT_C3STOP,
Tony Breedsd831d0b2007-09-21 13:26:03 +1000107};
Bharat Bhushan6e359942012-04-18 06:01:19 +0000108EXPORT_SYMBOL(decrementer_clockevent);
Tony Breedsd831d0b2007-09-21 13:26:03 +1000109
Anton Blanchard7df10272011-11-23 20:07:22 +0000110DEFINE_PER_CPU(u64, decrementers_next_tb);
111static DEFINE_PER_CPU(struct clock_event_device, decrementers);
Tony Breedsd831d0b2007-09-21 13:26:03 +1000112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#define XSEC_PER_SEC (1024*1024)
114
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000115#ifdef CONFIG_PPC64
116#define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
117#else
118/* compute ((xsec << 12) * max) >> 32 */
119#define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
120#endif
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122unsigned long tb_ticks_per_jiffy;
123unsigned long tb_ticks_per_usec = 100; /* sane default */
124EXPORT_SYMBOL(tb_ticks_per_usec);
125unsigned long tb_ticks_per_sec;
Paul Mackerras2cf82c02006-02-27 15:41:47 +1100126EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */
Paul Mackerras092b8f32006-02-20 10:38:56 +1100127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128DEFINE_SPINLOCK(rtc_lock);
Benjamin Herrenschmidt6ae3db12005-06-27 14:36:35 -0700129EXPORT_SYMBOL_GPL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Tony Breedsfc9069fe2007-07-04 14:04:31 +1000131static u64 tb_to_ns_scale __read_mostly;
132static unsigned tb_to_ns_shift __read_mostly;
Heiko Schocher364a1242010-11-22 21:30:33 +0000133static u64 boot_tb __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135extern struct timezone sys_tz;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000136static long timezone_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000138unsigned long ppc_proc_freq;
Timur Tabi55ec2fc2010-09-20 11:23:41 -0500139EXPORT_SYMBOL_GPL(ppc_proc_freq);
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000140unsigned long ppc_tb_freq;
Timur Tabi55ec2fc2010-09-20 11:23:41 -0500141EXPORT_SYMBOL_GPL(ppc_tb_freq);
Paul Mackerras96c44502005-10-23 17:14:56 +1000142
Mahesh Salgaonkarde269122019-03-05 01:12:19 +0530143bool tb_invalid;
144
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200145#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100146/*
Frederic Weisbeckere7f340c2017-01-31 04:09:48 +0100147 * Factor for converting from cputime_t (timebase ticks) to
148 * microseconds. This is stored as 0.64 fixed-point binary fraction.
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100149 */
Andreas Schwab9f5072d2011-12-09 11:35:08 +0000150u64 __cputime_usec_factor;
151EXPORT_SYMBOL(__cputime_usec_factor);
Stanislaw Gruszkaa42548a2009-07-29 12:15:29 +0200152
Christophe Leroyc223c902016-05-17 08:33:46 +0200153#ifdef CONFIG_PPC_SPLPAR
Paul Mackerras872e4392010-08-31 01:59:53 +0000154void (*dtl_consumer)(struct dtl_entry *, u64);
Christophe Leroyc223c902016-05-17 08:33:46 +0200155#endif
156
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100157static void calc_cputime_factors(void)
158{
159 struct div_result res;
160
Andreas Schwab9f5072d2011-12-09 11:35:08 +0000161 div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
162 __cputime_usec_factor = res.result_low;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100163}
164
165/*
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000166 * Read the SPURR on systems that have it, otherwise the PURR,
167 * or if that doesn't exist return the timebase value passed in.
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100168 */
Christophe Leroyabcff862018-08-02 07:53:59 +0000169static inline unsigned long read_spurr(unsigned long tb)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100170{
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000171 if (cpu_has_feature(CPU_FTR_SPURR))
172 return mfspr(SPRN_SPURR);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100173 if (cpu_has_feature(CPU_FTR_PURR))
174 return mfspr(SPRN_PURR);
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000175 return tb;
176}
177
178#ifdef CONFIG_PPC_SPLPAR
179
Peter Zijlstrad6bdceb2020-05-29 22:41:01 +0200180#include <asm/dtl.h>
181
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000182/*
183 * Scan the dispatch trace log and count up the stolen time.
184 * Should be called with interrupts disabled.
185 */
186static u64 scan_dispatch_log(u64 stop_tb)
187{
Paul Mackerras872e4392010-08-31 01:59:53 +0000188 u64 i = local_paca->dtl_ridx;
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000189 struct dtl_entry *dtl = local_paca->dtl_curr;
190 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
191 struct lppaca *vpa = local_paca->lppaca_ptr;
192 u64 tb_delta;
193 u64 stolen = 0;
194 u64 dtb;
195
Anton Blanchard84ffae52011-04-07 21:44:21 +0000196 if (!dtl)
197 return 0;
198
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +1000199 if (i == be64_to_cpu(vpa->dtl_idx))
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000200 return 0;
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +1000201 while (i < be64_to_cpu(vpa->dtl_idx)) {
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +1000202 dtb = be64_to_cpu(dtl->timebase);
203 tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
204 be32_to_cpu(dtl->ready_to_enqueue_time);
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000205 barrier();
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +1000206 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000207 /* buffer has overflowed */
Anton Blanchard7ffcf8e2013-08-07 02:01:46 +1000208 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000209 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
210 continue;
211 }
212 if (dtb > stop_tb)
213 break;
Anton Blanchard84b07382013-11-17 11:39:05 +1100214 if (dtl_consumer)
215 dtl_consumer(dtl, i);
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000216 stolen += tb_delta;
217 ++i;
218 ++dtl;
219 if (dtl == dtl_end)
220 dtl = local_paca->dispatch_log;
221 }
222 local_paca->dtl_ridx = i;
223 local_paca->dtl_curr = dtl;
224 return stolen;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100225}
226
227/*
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000228 * Accumulate stolen time by scanning the dispatch trace log.
229 * Called on entry from user mode.
Michael Neuling4603ac12007-10-18 03:06:37 -0700230 */
Michael Ellermaneb8e20f2019-10-13 21:23:51 +1100231void notrace accumulate_stolen_time(void)
Michael Neuling4603ac12007-10-18 03:06:37 -0700232{
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000233 u64 sst, ust;
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +0530234 unsigned long save_irq_soft_mask = irq_soft_mask_return();
Christophe Leroyc223c902016-05-17 08:33:46 +0200235 struct cpu_accounting_data *acct = &local_paca->accounting;
Tejun Heob18ae082011-01-03 03:49:25 +0000236
237 /* We are called early in the exception entry, before
238 * soft/hard_enabled are sync'ed to the expected state
239 * for the exception. We are hard disabled but the PACA
240 * needs to reflect that so various debug stuff doesn't
241 * complain
242 */
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +0530243 irq_soft_mask_set(IRQS_DISABLED);
Tejun Heob18ae082011-01-03 03:49:25 +0000244
Christophe Leroyc223c902016-05-17 08:33:46 +0200245 sst = scan_dispatch_log(acct->starttime_user);
246 ust = scan_dispatch_log(acct->starttime);
Frederic Weisbecker8c8b73c2017-01-05 18:11:45 +0100247 acct->stime -= sst;
248 acct->utime -= ust;
Frederic Weisbeckerf828c3d2017-01-05 18:11:46 +0100249 acct->steal_time += ust + sst;
Tejun Heob18ae082011-01-03 03:49:25 +0000250
Madhavan Srinivasan4e26bc42017-12-20 09:25:50 +0530251 irq_soft_mask_set(save_irq_soft_mask);
Michael Neuling4603ac12007-10-18 03:06:37 -0700252}
253
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000254static inline u64 calculate_stolen_time(u64 stop_tb)
255{
Aneesh Kumar K.Va6201da2018-04-02 13:03:37 +0530256 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
257 return 0;
258
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100259 if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx))
260 return scan_dispatch_log(stop_tb);
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000261
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100262 return 0;
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000263}
264
265#else /* CONFIG_PPC_SPLPAR */
266static inline u64 calculate_stolen_time(u64 stop_tb)
267{
268 return 0;
269}
270
271#endif /* CONFIG_PPC_SPLPAR */
272
Michael Neuling4603ac12007-10-18 03:06:37 -0700273/*
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100274 * Account time for a transition between system, hard irq
275 * or soft irq state.
276 */
Christophe Leroyb38a1812018-08-02 07:53:57 +0000277static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
278 unsigned long now, unsigned long stime)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100279{
Christophe Leroyabcff862018-08-02 07:53:59 +0000280 unsigned long stime_scaled = 0;
281#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
Christophe Leroyb38a1812018-08-02 07:53:57 +0000282 unsigned long nowscaled, deltascaled;
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100283 unsigned long utime, utime_scaled;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100284
Michael Neuling4603ac12007-10-18 03:06:37 -0700285 nowscaled = read_spurr(now);
Christophe Leroyc223c902016-05-17 08:33:46 +0200286 deltascaled = nowscaled - acct->startspurr;
287 acct->startspurr = nowscaled;
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100288 utime = acct->utime - acct->utime_sspurr;
Frederic Weisbecker8c8b73c2017-01-05 18:11:45 +0100289 acct->utime_sspurr = acct->utime;
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000290
291 /*
292 * Because we don't read the SPURR on every kernel entry/exit,
293 * deltascaled includes both user and system SPURR ticks.
294 * Apportion these ticks to system SPURR ticks and user
295 * SPURR ticks in the same ratio as the system time (delta)
296 * and user time (udelta) values obtained from the timebase
297 * over the same interval. The system ticks get accounted here;
298 * the user ticks get saved up in paca->user_time_scaled to be
299 * used by account_process_tick.
300 */
Christophe Leroyb38a1812018-08-02 07:53:57 +0000301 stime_scaled = stime;
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100302 utime_scaled = utime;
303 if (deltascaled != stime + utime) {
304 if (utime) {
Christophe Leroyb38a1812018-08-02 07:53:57 +0000305 stime_scaled = deltascaled * stime / (stime + utime);
306 utime_scaled = deltascaled - stime_scaled;
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000307 } else {
Christophe Leroyb38a1812018-08-02 07:53:57 +0000308 stime_scaled = deltascaled;
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000309 }
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100310 }
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100311 acct->utime_scaled += utime_scaled;
Christophe Leroyabcff862018-08-02 07:53:59 +0000312#endif
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000313
Christophe Leroyb38a1812018-08-02 07:53:57 +0000314 return stime_scaled;
315}
316
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100317static unsigned long vtime_delta(struct cpu_accounting_data *acct,
Christophe Leroyb38a1812018-08-02 07:53:57 +0000318 unsigned long *stime_scaled,
319 unsigned long *steal_time)
320{
321 unsigned long now, stime;
Christophe Leroyb38a1812018-08-02 07:53:57 +0000322
323 WARN_ON_ONCE(!irqs_disabled());
324
325 now = mftb();
326 stime = now - acct->starttime;
327 acct->starttime = now;
328
329 *stime_scaled = vtime_delta_scaled(acct, now, stime);
330
331 *steal_time = calculate_stolen_time(now);
332
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100333 return stime;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100334}
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200335
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100336static void vtime_delta_kernel(struct cpu_accounting_data *acct,
337 unsigned long *stime, unsigned long *stime_scaled)
338{
339 unsigned long steal_time;
340
341 *stime = vtime_delta(acct, stime_scaled, &steal_time);
342 *stime -= min(*stime, steal_time);
343 acct->steal_time += steal_time;
344}
345
Frederic Weisbeckerf83eeb12019-10-03 18:17:44 +0200346void vtime_account_kernel(struct task_struct *tsk)
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200347{
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100348 struct cpu_accounting_data *acct = get_accounting(tsk);
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100349 unsigned long stime, stime_scaled;
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200350
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100351 vtime_delta_kernel(acct, &stime, &stime_scaled);
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100352
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100353 if (tsk->flags & PF_VCPU) {
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100354 acct->gtime += stime;
Christophe Leroyabcff862018-08-02 07:53:59 +0000355#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100356 acct->utime_scaled += stime_scaled;
Christophe Leroyabcff862018-08-02 07:53:59 +0000357#endif
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100358 } else {
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100359 acct->stime += stime;
Christophe Leroyabcff862018-08-02 07:53:59 +0000360#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100361 acct->stime_scaled += stime_scaled;
Christophe Leroyabcff862018-08-02 07:53:59 +0000362#endif
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100363 }
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200364}
Frederic Weisbeckerf83eeb12019-10-03 18:17:44 +0200365EXPORT_SYMBOL_GPL(vtime_account_kernel);
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200366
Frederic Weisbeckerfd25b4c2012-11-13 18:21:22 +0100367void vtime_account_idle(struct task_struct *tsk)
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200368{
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100369 unsigned long stime, stime_scaled, steal_time;
370 struct cpu_accounting_data *acct = get_accounting(tsk);
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200371
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100372 stime = vtime_delta(acct, &stime_scaled, &steal_time);
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100373 acct->idle_time += stime + steal_time;
Frederic Weisbeckera7e1a9e2012-09-08 16:14:02 +0200374}
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100375
Frederic Weisbecker8a6a5922020-12-02 12:57:30 +0100376static void vtime_account_irq_field(struct cpu_accounting_data *acct,
377 unsigned long *field)
378{
379 unsigned long stime, stime_scaled;
380
381 vtime_delta_kernel(acct, &stime, &stime_scaled);
382 *field += stime;
383#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
384 acct->stime_scaled += stime_scaled;
385#endif
386}
387
388void vtime_account_softirq(struct task_struct *tsk)
389{
390 struct cpu_accounting_data *acct = get_accounting(tsk);
391 vtime_account_irq_field(acct, &acct->softirq_time);
392}
393
394void vtime_account_hardirq(struct task_struct *tsk)
395{
396 struct cpu_accounting_data *acct = get_accounting(tsk);
397 vtime_account_irq_field(acct, &acct->hardirq_time);
398}
399
Christophe Leroyb38a1812018-08-02 07:53:57 +0000400static void vtime_flush_scaled(struct task_struct *tsk,
401 struct cpu_accounting_data *acct)
402{
Christophe Leroyabcff862018-08-02 07:53:59 +0000403#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
Christophe Leroyb38a1812018-08-02 07:53:57 +0000404 if (acct->utime_scaled)
405 tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
406 if (acct->stime_scaled)
407 tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
408
409 acct->utime_scaled = 0;
410 acct->utime_sspurr = 0;
411 acct->stime_scaled = 0;
Christophe Leroyabcff862018-08-02 07:53:59 +0000412#endif
Christophe Leroyb38a1812018-08-02 07:53:57 +0000413}
414
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100415/*
Frederic Weisbeckerc8d7dab2017-01-05 18:11:50 +0100416 * Account the whole cputime accumulated in the paca
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100417 * Must be called with interrupts disabled.
Frederic Weisbeckerf83eeb12019-10-03 18:17:44 +0200418 * Assumes that vtime_account_kernel/idle() has been called
Frederic Weisbeckerbcebdf82012-11-13 23:51:06 +0100419 * recently (i.e. since the last entry from usermode) so that
Paul Mackerrascf9efce2010-08-26 19:56:43 +0000420 * get_paca()->user_time_scaled is up to date.
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100421 */
Frederic Weisbeckerc8d7dab2017-01-05 18:11:50 +0100422void vtime_flush(struct task_struct *tsk)
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100423{
Christophe Leroyc223c902016-05-17 08:33:46 +0200424 struct cpu_accounting_data *acct = get_accounting(tsk);
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100425
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100426 if (acct->utime)
Frederic Weisbecker23244a52017-01-31 04:09:37 +0100427 account_user_time(tsk, cputime_to_nsecs(acct->utime));
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100428
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100429 if (acct->gtime)
Frederic Weisbeckerfb8b0492017-01-31 04:09:40 +0100430 account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100431
Christophe Leroy51eeef92018-08-02 07:54:01 +0000432 if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
Frederic Weisbeckerbe9095e2017-01-31 04:09:38 +0100433 account_steal_time(cputime_to_nsecs(acct->steal_time));
Christophe Leroy51eeef92018-08-02 07:54:01 +0000434 acct->steal_time = 0;
435 }
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100436
437 if (acct->idle_time)
Frederic Weisbecker18b43a92017-01-31 04:09:39 +0100438 account_idle_time(cputime_to_nsecs(acct->idle_time));
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100439
440 if (acct->stime)
Frederic Weisbeckerfb8b0492017-01-31 04:09:40 +0100441 account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
442 CPUTIME_SYSTEM);
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100443
444 if (acct->hardirq_time)
Frederic Weisbeckerfb8b0492017-01-31 04:09:40 +0100445 account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
446 CPUTIME_IRQ);
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100447 if (acct->softirq_time)
Frederic Weisbeckerfb8b0492017-01-31 04:09:40 +0100448 account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
449 CPUTIME_SOFTIRQ);
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100450
Christophe Leroyb38a1812018-08-02 07:53:57 +0000451 vtime_flush_scaled(tsk, acct);
452
Frederic Weisbecker8c8b73c2017-01-05 18:11:45 +0100453 acct->utime = 0;
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100454 acct->gtime = 0;
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100455 acct->idle_time = 0;
456 acct->stime = 0;
Frederic Weisbeckera19ff1a2017-01-05 18:11:47 +0100457 acct->hardirq_time = 0;
458 acct->softirq_time = 0;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100459}
460
Frederic Weisbeckerabf917c2012-07-25 07:56:04 +0200461#else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100462#define calc_cputime_factors()
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100463#endif
464
Paul Mackerras6defa382005-11-18 13:44:17 +1100465void __delay(unsigned long loops)
466{
467 unsigned long start;
Paul Mackerras6defa382005-11-18 13:44:17 +1100468
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000469 spin_begin();
Christophe Leroya4c5a352020-09-29 06:48:38 +0000470 if (tb_invalid) {
Mahesh Salgaonkarde269122019-03-05 01:12:19 +0530471 /*
472 * TB is in error state and isn't ticking anymore.
473 * HMI handler was unable to recover from TB error.
474 * Return immediately, so that kernel won't get stuck here.
475 */
476 spin_cpu_relax();
Paul Mackerras6defa382005-11-18 13:44:17 +1100477 } else {
Christophe Leroy942e8912020-10-01 12:42:41 +0000478 start = mftb();
479 while (mftb() - start < loops)
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000480 spin_cpu_relax();
Paul Mackerras6defa382005-11-18 13:44:17 +1100481 }
Nicholas Piggin4e287e62017-06-06 23:08:32 +1000482 spin_end();
Paul Mackerras6defa382005-11-18 13:44:17 +1100483}
484EXPORT_SYMBOL(__delay);
485
486void udelay(unsigned long usecs)
487{
488 __delay(tb_ticks_per_usec * usecs);
489}
490EXPORT_SYMBOL(udelay);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492#ifdef CONFIG_SMP
493unsigned long profile_pc(struct pt_regs *regs)
494{
495 unsigned long pc = instruction_pointer(regs);
496
497 if (in_lock_functions(pc))
498 return regs->link;
499
500 return pc;
501}
502EXPORT_SYMBOL(profile_pc);
503#endif
504
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800505#ifdef CONFIG_IRQ_WORK
Paul Mackerras105988c2009-06-17 21:50:04 +1000506
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000507/*
508 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
509 */
510#ifdef CONFIG_PPC64
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800511static inline unsigned long test_irq_work_pending(void)
Paul Mackerras105988c2009-06-17 21:50:04 +1000512{
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000513 unsigned long x;
514
515 asm volatile("lbz %0,%1(13)"
516 : "=r" (x)
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800517 : "i" (offsetof(struct paca_struct, irq_work_pending)));
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000518 return x;
Paul Mackerras105988c2009-06-17 21:50:04 +1000519}
520
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800521static inline void set_irq_work_pending_flag(void)
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000522{
523 asm volatile("stb %0,%1(13)" : :
524 "r" (1),
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800525 "i" (offsetof(struct paca_struct, irq_work_pending)));
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000526}
527
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800528static inline void clear_irq_work_pending(void)
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000529{
530 asm volatile("stb %0,%1(13)" : :
531 "r" (0),
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800532 "i" (offsetof(struct paca_struct, irq_work_pending)));
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000533}
534
535#else /* 32-bit */
536
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800537DEFINE_PER_CPU(u8, irq_work_pending);
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000538
Christoph Lameter69111ba2014-10-21 15:23:25 -0500539#define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
540#define test_irq_work_pending() __this_cpu_read(irq_work_pending)
541#define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
Paul Mackerras105988c2009-06-17 21:50:04 +1000542
Nicholas Pigginabc3fce2020-04-02 22:04:01 +1000543#endif /* 32 vs 64 bit */
544
Peter Zijlstra4f8b50b2011-06-27 17:22:43 +0200545void arch_irq_work_raise(void)
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000546{
Nicholas Pigginabc3fce2020-04-02 22:04:01 +1000547 /*
548 * 64-bit code that uses irq soft-mask can just cause an immediate
549 * interrupt here that gets soft masked, if this is called under
550 * local_irq_disable(). It might be possible to prevent that happening
551 * by noticing interrupts are disabled and setting decrementer pending
552 * to be replayed when irqs are enabled. The problem there is that
553 * tracing can call irq_work_raise, including in code that does low
554 * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on)
555 * which could get tangled up if we're messing with the same state
556 * here.
557 */
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000558 preempt_disable();
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800559 set_irq_work_pending_flag();
Paul Mackerras0fe1ac42010-04-13 20:46:04 +0000560 set_dec(1);
561 preempt_enable();
562}
563
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800564#else /* CONFIG_IRQ_WORK */
Paul Mackerras105988c2009-06-17 21:50:04 +1000565
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800566#define test_irq_work_pending() 0
567#define clear_irq_work_pending()
Paul Mackerras105988c2009-06-17 21:50:04 +1000568
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800569#endif /* CONFIG_IRQ_WORK */
Paul Mackerras105988c2009-06-17 21:50:04 +1000570
Preeti U Murthy1b783952014-02-26 05:38:01 +0530571/*
572 * timer_interrupt - gets called when the decrementer overflows,
573 * with interrupts disabled.
574 */
Nicholas Piggin3a965702021-01-30 23:08:38 +1000575DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)
Preeti U Murthy1b783952014-02-26 05:38:01 +0530576{
Nicholas Piggin3f984622018-05-05 03:19:31 +1000577 struct clock_event_device *evt = this_cpu_ptr(&decrementers);
Christoph Lameter69111ba2014-10-21 15:23:25 -0500578 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
Nicholas Piggin3f984622018-05-05 03:19:31 +1000579 struct pt_regs *old_regs;
580 u64 now;
Preeti U Murthy1b783952014-02-26 05:38:01 +0530581
Nicholas Piggin59d512e2020-11-07 11:43:36 +1000582 /*
583 * Some implementations of hotplug will get timer interrupts while
584 * offline, just ignore these.
Preeti U Murthy1b783952014-02-26 05:38:01 +0530585 */
Nicholas Piggina7cba022018-05-05 03:19:32 +1000586 if (unlikely(!cpu_online(smp_processor_id()))) {
Nicholas Piggina7cba022018-05-05 03:19:32 +1000587 set_dec(decrementer_max);
Preeti U Murthy1b783952014-02-26 05:38:01 +0530588 return;
589 }
590
Nicholas Piggina7cba022018-05-05 03:19:32 +1000591 /* Ensure a positive value is written to the decrementer, or else
592 * some CPUs will continue to take decrementer exceptions. When the
593 * PPC_WATCHDOG (decrementer based) is configured, keep this at most
594 * 31 bits, which is about 4 seconds on most systems, which gives
595 * the watchdog a chance of catching timer interrupt hard lockups.
596 */
597 if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
598 set_dec(0x7fffffff);
599 else
600 set_dec(decrementer_max);
601
Preeti U Murthy1b783952014-02-26 05:38:01 +0530602 /* Conditionally hard-enable interrupts now that the DEC has been
603 * bumped to its maximum value
604 */
605 may_hard_irq_enable();
606
607
Paul Bolle6e0fdf92014-05-20 22:24:58 +0200608#if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
Preeti U Murthy1b783952014-02-26 05:38:01 +0530609 if (atomic_read(&ppc_n_lost_interrupts) != 0)
610 do_IRQ(regs);
611#endif
612
613 old_regs = set_irq_regs(regs);
Nicholas Piggin1b1b6a62021-01-30 23:08:44 +1000614
Nicholas Piggin3f984622018-05-05 03:19:31 +1000615 trace_timer_interrupt_entry(regs);
Preeti U Murthy1b783952014-02-26 05:38:01 +0530616
Nicholas Piggin3f984622018-05-05 03:19:31 +1000617 if (test_irq_work_pending()) {
618 clear_irq_work_pending();
619 irq_work_run();
620 }
621
Christophe Leroy6601ec12020-09-29 06:48:39 +0000622 now = get_tb();
Nicholas Piggin3f984622018-05-05 03:19:31 +1000623 if (now >= *next_tb) {
624 *next_tb = ~(u64)0;
625 if (evt->event_handler)
626 evt->event_handler(evt);
627 __this_cpu_inc(irq_stat.timer_irqs_event);
628 } else {
629 now = *next_tb - now;
630 if (now <= decrementer_max)
631 set_dec(now);
632 /* We may have raced with new irq work */
633 if (test_irq_work_pending())
634 set_dec(1);
635 __this_cpu_inc(irq_stat.timer_irqs_others);
636 }
637
638 trace_timer_interrupt_exit(regs);
Nicholas Piggin1b1b6a62021-01-30 23:08:44 +1000639
David Howells7d12e782006-10-05 14:55:46 +0100640 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
Al Viro9445aa12016-01-13 23:33:46 -0500642EXPORT_SYMBOL(timer_interrupt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Nicholas Pigginbc907112018-05-05 03:19:33 +1000644#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
Nicholas Piggin3f984622018-05-05 03:19:31 +1000645void timer_broadcast_interrupt(void)
646{
647 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
Nicholas Piggin3f984622018-05-05 03:19:31 +1000648
Nicholas Piggin3f984622018-05-05 03:19:31 +1000649 *next_tb = ~(u64)0;
650 tick_receive_broadcast();
Nicholas Piggine360cd32018-05-05 03:19:35 +1000651 __this_cpu_inc(irq_stat.broadcast_irqs_event);
Nicholas Piggin3f984622018-05-05 03:19:31 +1000652}
Nicholas Pigginbc907112018-05-05 03:19:33 +1000653#endif
Nicholas Piggin3f984622018-05-05 03:19:31 +1000654
Scott Wood7ac5dde2007-12-13 04:35:19 +1100655#ifdef CONFIG_SUSPEND
Paul Mackerrasd75d68c2010-06-20 19:04:14 +0000656static void generic_suspend_disable_irqs(void)
Scott Wood7ac5dde2007-12-13 04:35:19 +1100657{
Scott Wood7ac5dde2007-12-13 04:35:19 +1100658 /* Disable the decrementer, so that it doesn't interfere
659 * with suspending.
660 */
661
Oliver O'Halloran799010242016-07-01 16:20:39 +1000662 set_dec(decrementer_max);
Scott Wood7ac5dde2007-12-13 04:35:19 +1100663 local_irq_disable();
Oliver O'Halloran799010242016-07-01 16:20:39 +1000664 set_dec(decrementer_max);
Scott Wood7ac5dde2007-12-13 04:35:19 +1100665}
666
Paul Mackerrasd75d68c2010-06-20 19:04:14 +0000667static void generic_suspend_enable_irqs(void)
Scott Wood7ac5dde2007-12-13 04:35:19 +1100668{
Scott Wood7ac5dde2007-12-13 04:35:19 +1100669 local_irq_enable();
Scott Wood7ac5dde2007-12-13 04:35:19 +1100670}
671
672/* Overrides the weak version in kernel/power/main.c */
673void arch_suspend_disable_irqs(void)
674{
675 if (ppc_md.suspend_disable_irqs)
676 ppc_md.suspend_disable_irqs();
677 generic_suspend_disable_irqs();
678}
679
680/* Overrides the weak version in kernel/power/main.c */
681void arch_suspend_enable_irqs(void)
682{
683 generic_suspend_enable_irqs();
684 if (ppc_md.suspend_enable_irqs)
685 ppc_md.suspend_enable_irqs();
686}
687#endif
688
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100689unsigned long long tb_to_ns(unsigned long long ticks)
690{
691 return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
692}
693EXPORT_SYMBOL_GPL(tb_to_ns);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * Scheduler clock - returns current time in nanosec units.
697 *
698 * Note: mulhdu(a, b) (multiply high double unsigned) returns
699 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
700 * are 64-bit unsigned numbers.
701 */
Santosh Sivaraj6b847d72017-06-20 13:14:47 +0530702notrace unsigned long long sched_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Tony Breedsfc9069fe2007-07-04 14:04:31 +1000704 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Cyril Bur4be1b292015-02-12 15:01:28 -0800707
708#ifdef CONFIG_PPC_PSERIES
709
710/*
711 * Running clock - attempts to give a view of time passing for a virtualised
712 * kernels.
713 * Uses the VTB register if available otherwise a next best guess.
714 */
715unsigned long long running_clock(void)
716{
717 /*
718 * Don't read the VTB as a host since KVM does not switch in host
719 * timebase into the VTB when it takes a guest off the CPU, reading the
720 * VTB would result in reading 'last switched out' guest VTB.
721 *
722 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
723 * would be unsafe to rely only on the #ifdef above.
724 */
725 if (firmware_has_feature(FW_FEATURE_LPAR) &&
726 cpu_has_feature(CPU_FTR_ARCH_207S))
727 return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
728
729 /*
730 * This is a next best approximation without a VTB.
731 * On a host which is running bare metal there should never be any stolen
732 * time and on a host which doesn't do any virtualisation TB *should* equal
733 * VTB so it makes no difference anyway.
734 */
Frédéric Weisbecker9f3768e2017-02-21 16:18:41 +0100735 return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
Cyril Bur4be1b292015-02-12 15:01:28 -0800736}
737#endif
738
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000739static int __init get_freq(char *name, int cells, unsigned long *val)
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000740{
741 struct device_node *cpu;
Anton Blanchard6f7aba72013-08-07 02:01:34 +1000742 const __be32 *fp;
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000743 int found = 0;
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000744
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000745 /* The cpu node should have timebase and clock frequency properties */
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000746 cpu = of_find_node_by_type(NULL, "cpu");
747
Olaf Heringd8a81882006-02-04 10:34:56 +0100748 if (cpu) {
Stephen Rothwelle2eb6392007-04-03 22:26:41 +1000749 fp = of_get_property(cpu, name, NULL);
Olaf Heringd8a81882006-02-04 10:34:56 +0100750 if (fp) {
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000751 found = 1;
Paul Mackerrasa4dc7ff2006-09-19 14:06:27 +1000752 *val = of_read_ulong(fp, cells);
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000753 }
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000754
755 of_node_put(cpu);
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000756 }
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000757
758 return found;
759}
760
Anton Blancharde51df2c2014-08-20 08:55:18 +1000761static void start_cpu_decrementer(void)
Benjamin Herrenschmidt77c0a702009-08-28 14:25:04 +1000762{
763#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
Ivan Mikhaylov6e2f03e2017-05-19 18:47:05 +0300764 unsigned int tcr;
765
Benjamin Herrenschmidt77c0a702009-08-28 14:25:04 +1000766 /* Clear any pending timer interrupts */
767 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
768
Ivan Mikhaylov6e2f03e2017-05-19 18:47:05 +0300769 tcr = mfspr(SPRN_TCR);
770 /*
771 * The watchdog may have already been enabled by u-boot. So leave
772 * TRC[WP] (Watchdog Period) alone.
773 */
774 tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */
775 tcr |= TCR_DIE; /* Enable decrementer */
776 mtspr(SPRN_TCR, tcr);
777#endif
Benjamin Herrenschmidt77c0a702009-08-28 14:25:04 +1000778}
779
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000780void __init generic_calibrate_decr(void)
781{
782 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
783
784 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
785 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
786
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000787 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
788 "(not found)\n");
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000789 }
Anton Blanchard0bb474a42006-06-20 18:47:26 +1000790
791 ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
792
793 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
794 !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
795
796 printk(KERN_ERR "WARNING: Estimating processor frequency "
797 "(not found)\n");
798 }
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000799}
Arnd Bergmann10f7e7c2005-06-23 09:43:07 +1000800
Arnd Bergmann5235afa2018-04-23 10:36:41 +0200801int update_persistent_clock64(struct timespec64 now)
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000802{
803 struct rtc_time tm;
804
Tony Breedsaa3be5f2007-09-21 13:26:02 +1000805 if (!ppc_md.set_rtc_time)
Jason Gunthorpe023f3332012-12-17 14:30:53 -0700806 return -ENODEV;
Tony Breedsaa3be5f2007-09-21 13:26:02 +1000807
Arnd Bergmann5235afa2018-04-23 10:36:41 +0200808 rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
Tony Breedsaa3be5f2007-09-21 13:26:02 +1000809
810 return ppc_md.set_rtc_time(&tm);
811}
812
Arnd Bergmann5bfd6432018-04-23 10:36:40 +0200813static void __read_persistent_clock(struct timespec64 *ts)
Tony Breedsaa3be5f2007-09-21 13:26:02 +1000814{
815 struct rtc_time tm;
816 static int first = 1;
817
Martin Schwidefskyd90246c2009-08-22 22:23:13 +0200818 ts->tv_nsec = 0;
Tony Breedsaa3be5f2007-09-21 13:26:02 +1000819 /* XXX this is a litle fragile but will work okay in the short term */
820 if (first) {
821 first = 0;
822 if (ppc_md.time_init)
823 timezone_offset = ppc_md.time_init();
824
825 /* get_boot_time() isn't guaranteed to be safe to call late */
Martin Schwidefskyd90246c2009-08-22 22:23:13 +0200826 if (ppc_md.get_boot_time) {
827 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
828 return;
829 }
Tony Breedsaa3be5f2007-09-21 13:26:02 +1000830 }
Martin Schwidefskyd90246c2009-08-22 22:23:13 +0200831 if (!ppc_md.get_rtc_time) {
832 ts->tv_sec = 0;
833 return;
834 }
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000835 ppc_md.get_rtc_time(&tm);
Benjamin Herrenschmidt978d7eb2009-11-01 19:11:03 +0000836
Arnd Bergmann5bfd6432018-04-23 10:36:40 +0200837 ts->tv_sec = rtc_tm_to_time64(&tm);
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000838}
839
Arnd Bergmann5bfd6432018-04-23 10:36:40 +0200840void read_persistent_clock64(struct timespec64 *ts)
Benjamin Herrenschmidt978d7eb2009-11-01 19:11:03 +0000841{
842 __read_persistent_clock(ts);
843
844 /* Sanitize it in case real time clock is set below EPOCH */
845 if (ts->tv_sec < 0) {
846 ts->tv_sec = 0;
847 ts->tv_nsec = 0;
848 }
849
850}
851
Tony Breeds4a4cfe32007-09-22 07:35:52 +1000852/* clocksource code */
Santosh Sivaraj6b847d72017-06-20 13:14:47 +0530853static notrace u64 timebase_read(struct clocksource *cs)
Tony Breeds4a4cfe32007-09-22 07:35:52 +1000854{
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +0100855 return (u64)get_tb();
Tony Breeds4a4cfe32007-09-22 07:35:52 +1000856}
857
Michael Ellerman1c21a292008-05-08 14:27:19 +1000858static void __init clocksource_init(void)
Tony Breeds4a4cfe32007-09-22 07:35:52 +1000859{
Christophe Leroya4c5a352020-09-29 06:48:38 +0000860 struct clocksource *clock = &clocksource_timebase;
Tony Breeds4a4cfe32007-09-22 07:35:52 +1000861
Anton Blanchard11b86332011-11-23 20:07:19 +0000862 if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
Tony Breeds4a4cfe32007-09-22 07:35:52 +1000863 printk(KERN_ERR "clocksource: %s is already registered\n",
864 clock->name);
865 return;
866 }
867
868 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
869 clock->name, clock->mult, clock->shift);
870}
871
Tony Breedsd831d0b2007-09-21 13:26:03 +1000872static int decrementer_set_next_event(unsigned long evt,
873 struct clock_event_device *dev)
874{
Christophe Leroy6601ec12020-09-29 06:48:39 +0000875 __this_cpu_write(decrementers_next_tb, get_tb() + evt);
Tony Breedsd831d0b2007-09-21 13:26:03 +1000876 set_dec(evt);
Benjamin Herrenschmidt0215f7d2014-01-14 17:11:39 +1100877
878 /* We may have raced with new irq work */
879 if (test_irq_work_pending())
880 set_dec(1);
881
Tony Breedsd831d0b2007-09-21 13:26:03 +1000882 return 0;
883}
884
Viresh Kumar37a13e72015-07-16 16:56:25 +0530885static int decrementer_shutdown(struct clock_event_device *dev)
Tony Breedsd831d0b2007-09-21 13:26:03 +1000886{
Oliver O'Halloran799010242016-07-01 16:20:39 +1000887 decrementer_set_next_event(decrementer_max, dev);
Viresh Kumar37a13e72015-07-16 16:56:25 +0530888 return 0;
Tony Breedsd831d0b2007-09-21 13:26:03 +1000889}
890
891static void register_decrementer_clockevent(int cpu)
892{
Anton Blanchard7df10272011-11-23 20:07:22 +0000893 struct clock_event_device *dec = &per_cpu(decrementers, cpu);
Tony Breedsd831d0b2007-09-21 13:26:03 +1000894
895 *dec = decrementer_clockevent;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030896 dec->cpumask = cpumask_of(cpu);
Tony Breedsd831d0b2007-09-21 13:26:03 +1000897
Anton Blanchard8b78fdb2018-10-02 09:01:04 +1000898 clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
899
Anton Blanchardb919ee82010-02-07 19:26:29 +0000900 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
901 dec->name, dec->mult, dec->shift, cpu);
Michael Ellermanb4d16ab2018-10-17 23:39:41 +1100902
903 /* Set values for KVM, see kvm_emulate_dec() */
904 decrementer_clockevent.mult = dec->mult;
905 decrementer_clockevent.shift = dec->shift;
Tony Breedsd831d0b2007-09-21 13:26:03 +1000906}
907
Oliver O'Halloran799010242016-07-01 16:20:39 +1000908static void enable_large_decrementer(void)
909{
910 if (!cpu_has_feature(CPU_FTR_ARCH_300))
911 return;
912
913 if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
914 return;
915
916 /*
917 * If we're running as the hypervisor we need to enable the LD manually
918 * otherwise firmware should have done it for us.
919 */
920 if (cpu_has_feature(CPU_FTR_HVMODE))
921 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
922}
923
924static void __init set_decrementer_max(void)
925{
926 struct device_node *cpu;
927 u32 bits = 32;
928
929 /* Prior to ISAv3 the decrementer is always 32 bit */
930 if (!cpu_has_feature(CPU_FTR_ARCH_300))
931 return;
932
933 cpu = of_find_node_by_type(NULL, "cpu");
934
935 if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
936 if (bits > 64 || bits < 32) {
937 pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
938 bits = 32;
939 }
940
941 /* calculate the signed maximum given this many bits */
942 decrementer_max = (1ul << (bits - 1)) - 1;
943 }
944
945 of_node_put(cpu);
946
947 pr_info("time_init: %u bit decrementer (max: %llx)\n",
948 bits, decrementer_max);
949}
950
Milton Millerc4818872007-12-14 15:52:10 +1100951static void __init init_decrementer_clockevent(void)
Tony Breedsd831d0b2007-09-21 13:26:03 +1000952{
Anton Blanchard8b78fdb2018-10-02 09:01:04 +1000953 register_decrementer_clockevent(smp_processor_id());
Tony Breedsd831d0b2007-09-21 13:26:03 +1000954}
955
956void secondary_cpu_time_init(void)
957{
Oliver O'Halloran799010242016-07-01 16:20:39 +1000958 /* Enable and test the large decrementer for this cpu */
959 enable_large_decrementer();
960
Benjamin Herrenschmidt77c0a702009-08-28 14:25:04 +1000961 /* Start the decrementer on CPUs that have manual control
962 * such as BookE
963 */
964 start_cpu_decrementer();
965
Tony Breedsd831d0b2007-09-21 13:26:03 +1000966 /* FIME: Should make unrelatred change to move snapshot_timebase
967 * call here ! */
968 register_decrementer_clockevent(smp_processor_id());
969}
970
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000971/* This function is only called on the boot processor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972void __init time_init(void)
973{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct div_result res;
Paul Mackerrasd75d68c2010-06-20 19:04:14 +0000975 u64 scale;
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000976 unsigned shift;
977
Christophe Leroya4c5a352020-09-29 06:48:38 +0000978 /* Normal PowerPC with timebase register */
979 ppc_md.calibrate_decr();
980 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
981 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
982 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
983 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
Paul Mackerras374e99d2005-10-20 21:04:51 +1000984
985 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
Paul Mackerras092b8f32006-02-20 10:38:56 +1100986 tb_ticks_per_sec = ppc_tb_freq;
Paul Mackerras374e99d2005-10-20 21:04:51 +1000987 tb_ticks_per_usec = ppc_tb_freq / 1000000;
Paul Mackerrasc6622f62006-02-24 10:06:59 +1100988 calc_cputime_factors();
Paul Mackerras092b8f32006-02-20 10:38:56 +1100989
990 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * Compute scale factor for sched_clock.
992 * The calibrate_decr() function has set tb_ticks_per_sec,
993 * which is the timebase frequency.
994 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
995 * the 128-bit result as a 64.64 fixed-point number.
996 * We then shift that number right until it is less than 1.0,
997 * giving us the scale factor and shift count to use in
998 * sched_clock().
999 */
1000 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
1001 scale = res.result_low;
1002 for (shift = 0; res.result_high != 0; ++shift) {
1003 scale = (scale >> 1) | (res.result_high << 63);
1004 res.result_high >>= 1;
1005 }
1006 tb_to_ns_scale = scale;
1007 tb_to_ns_shift = shift;
Tony Breedsfc9069fe2007-07-04 14:04:31 +10001008 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
Christophe Leroy6601ec12020-09-29 06:48:39 +00001009 boot_tb = get_tb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Paul Mackerras092b8f32006-02-20 10:38:56 +11001011 /* If platform provided a timezone (pmac), we correct the time */
Anton Blanchard621692c2011-11-23 20:07:21 +00001012 if (timezone_offset) {
Paul Mackerras092b8f32006-02-20 10:38:56 +11001013 sys_tz.tz_minuteswest = -timezone_offset / 60;
1014 sys_tz.tz_dsttime = 0;
Anton Blanchard621692c2011-11-23 20:07:21 +00001015 }
Paul Mackerras092b8f32006-02-20 10:38:56 +11001016
Benjamin Herrenschmidta7f290d2005-11-11 21:15:21 +11001017 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Oliver O'Halloran799010242016-07-01 16:20:39 +10001019 /* initialise and enable the large decrementer (if we have one) */
1020 set_decrementer_max();
1021 enable_large_decrementer();
1022
Benjamin Herrenschmidt77c0a702009-08-28 14:25:04 +10001023 /* Start the decrementer on CPUs that have manual control
1024 * such as BookE
1025 */
1026 start_cpu_decrementer();
1027
Stephen Rothwellf5339272012-03-15 18:18:00 +00001028 /* Register the clocksource */
1029 clocksource_init();
Tony Breeds4a4cfe32007-09-22 07:35:52 +10001030
Tony Breedsd831d0b2007-09-21 13:26:03 +10001031 init_decrementer_clockevent();
Preeti U Murthy0d948732014-02-26 05:39:06 +05301032 tick_setup_hrtimer_broadcast();
Kevin Haof0d37302014-12-03 16:53:52 +08001033
Kevin Haof0d37302014-12-03 16:53:52 +08001034 of_clk_init(NULL);
Pingfan Liub709e322020-10-22 14:51:19 +08001035 enable_sched_clock_irqtime();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038/*
1039 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1040 * result.
1041 */
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001042void div128_by_32(u64 dividend_high, u64 dividend_low,
1043 unsigned divisor, struct div_result *dr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001045 unsigned long a, b, c, d;
1046 unsigned long w, x, y, z;
1047 u64 ra, rb, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 a = dividend_high >> 32;
1050 b = dividend_high & 0xffffffff;
1051 c = dividend_low >> 32;
1052 d = dividend_low & 0xffffffff;
1053
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001054 w = a / divisor;
1055 ra = ((u64)(a - (w * divisor)) << 32) + b;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001057 rb = ((u64) do_div(ra, divisor) << 32) + c;
1058 x = ra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001060 rc = ((u64) do_div(rb, divisor) << 32) + d;
1061 y = rb;
1062
1063 do_div(rc, divisor);
1064 z = rc;
Paul Mackerrasf2783c12005-10-20 09:23:26 +10001065
1066 dr->result_high = ((u64)w << 32) + x;
1067 dr->result_low = ((u64)y << 32) + z;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069}
Geert Uytterhoevenbcd68a72009-02-19 16:50:46 +01001070
Benjamin Herrenschmidt177996e2009-06-09 21:12:00 +00001071/* We don't need to calibrate delay, we use the CPU timebase for that */
1072void calibrate_delay(void)
1073{
1074 /* Some generic code (such as spinlock debug) use loops_per_jiffy
1075 * as the number of __delay(1) in a jiffy, so make it so
1076 */
1077 loops_per_jiffy = tb_ticks_per_jiffy;
1078}
1079
Arnd Bergmann169047f2016-05-30 20:58:00 +02001080#if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1081static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1082{
1083 ppc_md.get_rtc_time(tm);
Alexandre Belloni890ae792018-02-21 22:46:33 +01001084 return 0;
Arnd Bergmann169047f2016-05-30 20:58:00 +02001085}
1086
1087static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1088{
1089 if (!ppc_md.set_rtc_time)
1090 return -EOPNOTSUPP;
1091
1092 if (ppc_md.set_rtc_time(tm) < 0)
1093 return -EOPNOTSUPP;
1094
1095 return 0;
1096}
1097
1098static const struct rtc_class_ops rtc_generic_ops = {
1099 .read_time = rtc_generic_get_time,
1100 .set_time = rtc_generic_set_time,
1101};
1102
Geert Uytterhoevenbcd68a72009-02-19 16:50:46 +01001103static int __init rtc_init(void)
1104{
1105 struct platform_device *pdev;
1106
1107 if (!ppc_md.get_rtc_time)
1108 return -ENODEV;
1109
Arnd Bergmann169047f2016-05-30 20:58:00 +02001110 pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1111 &rtc_generic_ops,
1112 sizeof(rtc_generic_ops));
Geert Uytterhoevenbcd68a72009-02-19 16:50:46 +01001113
Rusty Russell8c6ffba2013-07-15 11:20:32 +09301114 return PTR_ERR_OR_ZERO(pdev);
Geert Uytterhoevenbcd68a72009-02-19 16:50:46 +01001115}
1116
Paul Gortmaker8f6b9512015-05-01 20:05:49 -04001117device_initcall(rtc_init);
Arnd Bergmann169047f2016-05-30 20:58:00 +02001118#endif