blob: b996b2cf0703876fc5d65d20926ea2470c62fb8f [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/arch/arm/kernel/time.c
4 *
5 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
6 * Modifications for ARM (C) 1994-2001 Russell King
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This file contains the ARM-specific time handling details:
9 * reading the RTC at bootup, etc...
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020011#include <linux/clk-provider.h>
Arnd Bergmannf414f132013-03-25 11:14:57 -050012#include <linux/clocksource.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020013#include <linux/errno.h>
14#include <linux/export.h>
15#include <linux/init.h>
16#include <linux/interrupt.h>
Frederik Deweerdte317c8c2006-10-06 18:58:24 +000017#include <linux/irq.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020018#include <linux/kernel.h>
19#include <linux/profile.h>
20#include <linux/sched.h>
Stephen Boyd38ff87f2013-06-01 23:39:40 -070021#include <linux/sched_clock.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020022#include <linux/smp.h>
23#include <linux/time.h>
24#include <linux/timex.h>
25#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Russell King8ff14432010-12-20 10:18:36 +000027#include <asm/mach/arch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/mach/time.h>
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +020029#include <asm/stacktrace.h>
30#include <asm/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Arnd Bergmann63216d32011-06-10 13:58:30 +000032#if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || \
33 defined(CONFIG_NVRAM) || defined(CONFIG_NVRAM_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* this needs a better home */
35DEFINE_SPINLOCK(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036EXPORT_SYMBOL(rtc_lock);
David Brownell9dd34942007-01-17 22:11:27 +010037#endif /* pc-style 'CMOS' RTC support */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39/* change this if you have some constant time drift */
40#define USECS_PER_JIFFY (1000000/HZ)
41
42#ifdef CONFIG_SMP
43unsigned long profile_pc(struct pt_regs *regs)
44{
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010045 struct stackframe frame;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010047 if (!in_lock_functions(regs->ARM_pc))
48 return regs->ARM_pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Nikolay Borisova3250c92014-06-03 19:48:58 +010050 arm_get_current_stackframe(regs, &frame);
Catalin Marinas2d7c11b2009-02-11 13:07:53 +010051 do {
52 int ret = unwind_frame(&frame);
53 if (ret < 0)
54 return 0;
55 } while (in_lock_functions(frame.pc));
56
57 return frame.pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59EXPORT_SYMBOL(profile_pc);
60#endif
61
Kevin Hilman9e4559d2007-03-14 17:33:24 +010062#ifndef CONFIG_GENERIC_CLOCKEVENTS
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Kernel system timer support.
65 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070066void timer_tick(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067{
Frederik Deweerdte317c8c2006-10-06 18:58:24 +000068 profile_tick(CPU_PROFILING);
Torben Hohn6906e332011-01-27 15:59:21 +010069 xtime_update(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#ifndef CONFIG_SMP
Russell Kingc97d4862006-10-25 13:59:16 +010071 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#endif
73}
Kevin Hilman9e4559d2007-03-14 17:33:24 +010074#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Xunlei Pangcb850712015-04-01 20:34:26 -070076static void dummy_clock_access(struct timespec64 *ts)
Marc Zyngierbd0493e2012-05-05 19:28:44 +010077{
78 ts->tv_sec = 0;
79 ts->tv_nsec = 0;
80}
81
82static clock_access_fn __read_persistent_clock = dummy_clock_access;
Marc Zyngierbd0493e2012-05-05 19:28:44 +010083
Xunlei Pangcb850712015-04-01 20:34:26 -070084void read_persistent_clock64(struct timespec64 *ts)
Marc Zyngierbd0493e2012-05-05 19:28:44 +010085{
86 __read_persistent_clock(ts);
87}
88
Pavel Tatashin227e3952018-07-19 16:55:37 -040089int __init register_persistent_clock(clock_access_fn read_persistent)
Marc Zyngierbd0493e2012-05-05 19:28:44 +010090{
91 /* Only allow the clockaccess functions to be registered once */
Pavel Tatashin227e3952018-07-19 16:55:37 -040092 if (__read_persistent_clock == dummy_clock_access) {
Marc Zyngierbd0493e2012-05-05 19:28:44 +010093 if (read_persistent)
94 __read_persistent_clock = read_persistent;
Marc Zyngierbd0493e2012-05-05 19:28:44 +010095 return 0;
96 }
97
98 return -EINVAL;
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101void __init time_init(void)
102{
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +0200103 if (machine_desc->init_time) {
Arnd Bergmannf414f132013-03-25 11:14:57 -0500104 machine_desc->init_time();
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +0200105 } else {
106#ifdef CONFIG_COMMON_CLK
107 of_clk_init(NULL);
108#endif
Daniel Lezcanoba5d08c2017-05-26 17:40:46 +0200109 timer_probe();
Sebastian Hesselbarth4178bac2013-09-04 12:24:03 +0200110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}