blob: 0e14f6c0d35e0f7ef0998963390eab9587d8186e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Thomas Gleixnerdd3e6e82009-08-20 15:35:23 +02003 * Copyright (c) 1991,1992,1995 Linus Torvalds
4 * Copyright (c) 1994 Alan Modra
5 * Copyright (c) 1995 Markus Kuhn
6 * Copyright (c) 1996 Ingo Molnar
7 * Copyright (c) 1998 Andrea Arcangeli
8 * Copyright (c) 2002,2006 Vojtech Pavlik
9 * Copyright (c) 2003 Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
Thomas Gleixner2a21ad52018-09-17 14:45:35 +020013#include <linux/clocksource.h>
Thomas Gleixnerecce8502009-08-20 15:28:50 +020014#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/interrupt.h>
Nicolai Stange447ae312018-07-29 12:15:33 +020016#include <linux/irq.h>
Ralf Baechle334955e2011-06-01 19:04:57 +010017#include <linux/i8253.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/time.h>
Paul Gortmaker69c60c82011-05-26 12:22:53 -040019#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Thomas Gleixnerdd3e6e82009-08-20 15:35:23 +020021#include <asm/vsyscall.h>
22#include <asm/x86_init.h>
Thomas Gleixnerecce8502009-08-20 15:28:50 +020023#include <asm/i8259.h>
Thomas Gleixnerdd3e6e82009-08-20 15:35:23 +020024#include <asm/timer.h>
25#include <asm/hpet.h>
26#include <asm/time.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Thomas Gleixner454ede72009-08-20 16:07:40 +020028#ifdef CONFIG_X86_64
Nathan Chancellor53c13ba2018-10-12 17:53:12 -070029__visible volatile unsigned long jiffies __cacheline_aligned_in_smp = INITIAL_JIFFIES;
Thomas Gleixner454ede72009-08-20 16:07:40 +020030#endif
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032unsigned long profile_pc(struct pt_regs *regs)
33{
34 unsigned long pc = instruction_pointer(regs);
35
Andy Lutomirskif39b6f02015-03-18 18:33:33 -070036 if (!user_mode(regs) && in_lock_functions(pc)) {
Andi Kleen0cb91a22006-09-26 10:52:28 +020037#ifdef CONFIG_FRAME_POINTER
Glauber Costa2c460d02008-07-11 16:06:40 -030038 return *(unsigned long *)(regs->bp + sizeof(long));
Andi Kleen0cb91a22006-09-26 10:52:28 +020039#else
H. Peter Anvind1705c52009-10-12 11:32:31 -070040 unsigned long *sp =
41 (unsigned long *)kernel_stack_pointer(regs);
Thomas Gleixneref451282009-08-21 13:24:08 +020042 /*
43 * Return address is either directly at stack pointer
44 * or above a saved flags. Eflags has bits 22-31 zero,
45 * kernel addresses don't.
46 */
Thomas Gleixnerfe599f92008-01-30 13:30:26 +010047 if (sp[0] >> 22)
Andi Kleen0cb91a22006-09-26 10:52:28 +020048 return sp[0];
49 if (sp[1] >> 22)
50 return sp[1];
51#endif
52 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 return pc;
54}
55EXPORT_SYMBOL(profile_pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
Thomas Gleixner454ede72009-08-20 16:07:40 +020058 * Default timer interrupt handler for PIT/HPET
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Thomas Gleixner845b3942009-08-19 15:37:03 +020060static irqreturn_t timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Thomas Gleixnerecce8502009-08-20 15:28:50 +020062 global_clock_event->event_handler(global_clock_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return IRQ_HANDLED;
64}
65
Thomas Gleixner845b3942009-08-19 15:37:03 +020066static struct irqaction irq0 = {
67 .handler = timer_interrupt,
Michael Opdenackerd20d2ef2014-03-04 21:35:05 +010068 .flags = IRQF_NOBALANCING | IRQF_IRQPOLL | IRQF_TIMER,
Thomas Gleixner845b3942009-08-19 15:37:03 +020069 .name = "timer"
70};
71
Dou Liyangb1b4f2f2017-06-13 10:30:29 +080072static void __init setup_default_timer_irq(void)
Thomas Gleixner845b3942009-08-19 15:37:03 +020073{
Peter Zijlstra6d671e12017-12-22 10:20:12 +010074 /*
75 * Unconditionally register the legacy timer; even without legacy
76 * PIC/PIT we need this for the HPET0 in legacy replacement mode.
77 */
78 if (setup_irq(0, &irq0))
79 pr_info("Failed to register legacy timer interrupt\n");
Thomas Gleixner845b3942009-08-19 15:37:03 +020080}
81
82/* Default timer init function */
Zachary Amsdene30fab32007-03-05 00:30:39 -080083void __init hpet_time_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080085 if (!hpet_enable())
86 setup_pit_timer();
Thomas Gleixner845b3942009-08-19 15:37:03 +020087 setup_default_timer_irq();
88}
89
Thomas Gleixner54e26032009-09-16 08:42:26 +020090static __init void x86_late_time_init(void)
Thomas Gleixner845b3942009-08-19 15:37:03 +020091{
92 x86_init.timers.timer_init();
Dou Liyang935356c2017-09-13 17:12:54 +080093 /*
94 * After PIT/HPET timers init, select and setup
95 * the final interrupt mode for delivering IRQs.
96 */
97 x86_init.irqs.intr_mode_init();
Thomas Gleixnerdd0a70c2009-08-20 16:51:07 +020098 tsc_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Zachary Amsdene30fab32007-03-05 00:30:39 -0800101/*
Thomas Gleixner845b3942009-08-19 15:37:03 +0200102 * Initialize TSC and delay the periodic timer init to
103 * late x86_late_time_init() so ioremap works.
Zachary Amsdene30fab32007-03-05 00:30:39 -0800104 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105void __init time_init(void)
106{
Thomas Gleixner845b3942009-08-19 15:37:03 +0200107 late_time_init = x86_late_time_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
Thomas Gleixner2a21ad52018-09-17 14:45:35 +0200109
110/*
111 * Sanity check the vdso related archdata content.
112 */
113void clocksource_arch_init(struct clocksource *cs)
114{
115 if (cs->archdata.vclock_mode == VCLOCK_NONE)
116 return;
117
118 if (cs->archdata.vclock_mode > VCLOCK_MAX) {
119 pr_warn("clocksource %s registered with invalid vclock_mode %d. Disabling vclock.\n",
120 cs->name, cs->archdata.vclock_mode);
121 cs->archdata.vclock_mode = VCLOCK_NONE;
122 }
Thomas Gleixnera51e9962018-09-17 14:45:36 +0200123
124 if (cs->mask != CLOCKSOURCE_MASK(64)) {
125 pr_warn("clocksource %s registered with invalid mask %016llx. Disabling vclock.\n",
126 cs->name, cs->mask);
127 cs->archdata.vclock_mode = VCLOCK_NONE;
128 }
Thomas Gleixner2a21ad52018-09-17 14:45:35 +0200129}