blob: 0b034ebbda2a1c2dd0125a6d4c2135dd886eb440 [file] [log] [blame]
Gennady Sharapovcff65c42006-01-18 17:42:42 -08001/*
Anton Ivanov2eb5f312015-11-02 16:16:37 +00002 * Copyright (C) 2015 Anton Ivanov (aivanov@{brocade.com,kot-begemot.co.uk})
3 * Copyright (C) 2015 Thomas Meyer (thomas@m3y3r.de)
4 * Copyright (C) 2012-2014 Cisco Systems
Jeff Dikeba180fd2007-10-16 01:27:00 -07005 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Licensed under the GPL
7 */
8
Jeff Dikec5d4bb12008-02-04 22:31:14 -08009#include <linux/clockchips.h>
Adrian Bunk7d195a52008-04-29 00:59:18 -070010#include <linux/init.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080011#include <linux/interrupt.h>
12#include <linux/jiffies.h>
Anton Ivanov2eb5f312015-11-02 16:16:37 +000013#include <linux/mm.h>
14#include <linux/sched.h>
15#include <linux/spinlock.h>
Jeff Dikec5d4bb12008-02-04 22:31:14 -080016#include <linux/threads.h>
17#include <asm/irq.h>
18#include <asm/param.h>
Al Viro37185b32012-10-08 03:27:32 +010019#include <kern_util.h>
20#include <os.h>
Anton Ivanov2eb5f312015-11-02 16:16:37 +000021#include <timer-internal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Martin Pärteld3c1cfc2012-08-02 00:49:17 +020023void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Jeff Dike31ccc1f2007-10-16 01:27:24 -070025 unsigned long flags;
Jeff Dike31ccc1f2007-10-16 01:27:24 -070026
27 local_irq_save(flags);
Jeff Diked2753a6d2007-10-16 01:27:25 -070028 do_IRQ(TIMER_IRQ, regs);
Jeff Dike31ccc1f2007-10-16 01:27:24 -070029 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030}
31
Viresh Kumar71b52802015-07-16 16:56:31 +053032static int itimer_shutdown(struct clock_event_device *evt)
Gennady Sharapovcff65c42006-01-18 17:42:42 -080033{
Anton Ivanov2eb5f312015-11-02 16:16:37 +000034 os_timer_disable();
Viresh Kumar71b52802015-07-16 16:56:31 +053035 return 0;
36}
Gennady Sharapovcff65c42006-01-18 17:42:42 -080037
Viresh Kumar71b52802015-07-16 16:56:31 +053038static int itimer_set_periodic(struct clock_event_device *evt)
39{
Anton Ivanov2eb5f312015-11-02 16:16:37 +000040 os_timer_set_interval(NULL, NULL);
Viresh Kumar71b52802015-07-16 16:56:31 +053041 return 0;
Gennady Sharapovcff65c42006-01-18 17:42:42 -080042}
43
Jeff Diked2753a6d2007-10-16 01:27:25 -070044static int itimer_next_event(unsigned long delta,
45 struct clock_event_device *evt)
46{
Anton Ivanov2eb5f312015-11-02 16:16:37 +000047 return os_timer_one_shot(delta);
Jeff Diked2753a6d2007-10-16 01:27:25 -070048}
49
Anton Ivanov2eb5f312015-11-02 16:16:37 +000050static int itimer_one_shot(struct clock_event_device *evt)
51{
52 os_timer_one_shot(1);
53 return 0;
54}
55
56static struct clock_event_device timer_clockevent = {
57 .name = "posix-timer",
Viresh Kumar71b52802015-07-16 16:56:31 +053058 .rating = 250,
59 .cpumask = cpu_all_mask,
60 .features = CLOCK_EVT_FEAT_PERIODIC |
61 CLOCK_EVT_FEAT_ONESHOT,
62 .set_state_shutdown = itimer_shutdown,
63 .set_state_periodic = itimer_set_periodic,
Anton Ivanov2eb5f312015-11-02 16:16:37 +000064 .set_state_oneshot = itimer_one_shot,
Viresh Kumar71b52802015-07-16 16:56:31 +053065 .set_next_event = itimer_next_event,
Anton Ivanov2eb5f312015-11-02 16:16:37 +000066 .shift = 0,
67 .max_delta_ns = 0xffffffff,
Nicolai Stange8ab3a282017-03-30 21:59:42 +020068 .max_delta_ticks = 0xffffffff,
69 .min_delta_ns = TIMER_MIN_DELTA,
70 .min_delta_ticks = TIMER_MIN_DELTA, // microsecond resolution should be enough for anyone, same as 640K RAM
Viresh Kumar71b52802015-07-16 16:56:31 +053071 .irq = 0,
Anton Ivanov2eb5f312015-11-02 16:16:37 +000072 .mult = 1,
Jeff Dike31ccc1f2007-10-16 01:27:24 -070073};
74
75static irqreturn_t um_timer(int irq, void *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Anton Ivanov2eb5f312015-11-02 16:16:37 +000077 if (get_current()->mm != NULL)
78 {
79 /* userspace - relay signal, results in correct userspace timers */
80 os_alarm_process(get_current()->mm->context.id.u.pid);
81 }
82
83 (*timer_clockevent.event_handler)(&timer_clockevent);
Gennady Sharapovcff65c42006-01-18 17:42:42 -080084
Jeff Dike572e6142006-06-30 01:55:56 -070085 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010088static u64 timer_read(struct clocksource *cs)
Jeff Dike791a6442007-10-16 01:27:25 -070089{
Anton Ivanov2eb5f312015-11-02 16:16:37 +000090 return os_nsecs() / TIMER_MULTIPLIER;
Jeff Dike791a6442007-10-16 01:27:25 -070091}
92
Anton Ivanov2eb5f312015-11-02 16:16:37 +000093static struct clocksource timer_clocksource = {
94 .name = "timer",
Jeff Dike791a6442007-10-16 01:27:25 -070095 .rating = 300,
Anton Ivanov2eb5f312015-11-02 16:16:37 +000096 .read = timer_read,
Jeff Dike791a6442007-10-16 01:27:25 -070097 .mask = CLOCKSOURCE_MASK(64),
Jeff Dike791a6442007-10-16 01:27:25 -070098 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
99};
100
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000101static void __init timer_setup(void)
Jeff Dikeaceb34342006-07-10 04:45:05 -0700102{
103 int err;
104
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000105 err = request_irq(TIMER_IRQ, um_timer, IRQF_TIMER, "hr timer", NULL);
Jeff Dikeba180fd2007-10-16 01:27:00 -0700106 if (err != 0)
Jeff Dike537ae942006-09-25 23:33:05 -0700107 printk(KERN_ERR "register_timer : request_irq failed - "
Jeff Dikeaceb34342006-07-10 04:45:05 -0700108 "errno = %d\n", -err);
109
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000110 err = os_timer_create(NULL);
111 if (err != 0) {
112 printk(KERN_ERR "creation of timer failed - errno = %d\n", -err);
113 return;
114 }
115
116 err = clocksource_register_hz(&timer_clocksource, NSEC_PER_SEC/TIMER_MULTIPLIER);
Jeff Dike791a6442007-10-16 01:27:25 -0700117 if (err) {
John Stultz60d687e2010-04-26 20:25:16 -0700118 printk(KERN_ERR "clocksource_register_hz returned %d\n", err);
Jeff Dike791a6442007-10-16 01:27:25 -0700119 return;
120 }
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000121 clockevents_register_device(&timer_clockevent);
Jeff Dikeaceb34342006-07-10 04:45:05 -0700122}
123
John Stultz9f31f572010-07-13 17:56:24 -0700124void read_persistent_clock(struct timespec *ts)
125{
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000126 long long nsecs = os_persistent_clock_emulation();
Thomas Gleixnerb2923072010-08-03 20:34:48 +0200127
John Stultz9f31f572010-07-13 17:56:24 -0700128 set_normalized_timespec(ts, nsecs / NSEC_PER_SEC,
129 nsecs % NSEC_PER_SEC);
130}
131
Jeff Dike31ccc1f2007-10-16 01:27:24 -0700132void __init time_init(void)
Jeff Dikeaceb34342006-07-10 04:45:05 -0700133{
Anton Ivanov2eb5f312015-11-02 16:16:37 +0000134 timer_set_signal_handler();
135 late_time_init = timer_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}