time.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * 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
  5. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  6. * Licensed under the GPL
  7. */
  8. #include <linux/clockchips.h>
  9. #include <linux/init.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/mm.h>
  13. #include <linux/sched.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/threads.h>
  16. #include <asm/irq.h>
  17. #include <asm/param.h>
  18. #include <kern_util.h>
  19. #include <os.h>
  20. #include <timer-internal.h>
  21. void timer_handler(int sig, struct siginfo *unused_si, struct uml_pt_regs *regs)
  22. {
  23. unsigned long flags;
  24. local_irq_save(flags);
  25. do_IRQ(TIMER_IRQ, regs);
  26. local_irq_restore(flags);
  27. }
  28. static int itimer_shutdown(struct clock_event_device *evt)
  29. {
  30. os_timer_disable();
  31. return 0;
  32. }
  33. static int itimer_set_periodic(struct clock_event_device *evt)
  34. {
  35. os_timer_set_interval(NULL, NULL);
  36. return 0;
  37. }
  38. static int itimer_next_event(unsigned long delta,
  39. struct clock_event_device *evt)
  40. {
  41. return os_timer_one_shot(delta);
  42. }
  43. static int itimer_one_shot(struct clock_event_device *evt)
  44. {
  45. os_timer_one_shot(1);
  46. return 0;
  47. }
  48. static struct clock_event_device timer_clockevent = {
  49. .name = "posix-timer",
  50. .rating = 250,
  51. .cpumask = cpu_all_mask,
  52. .features = CLOCK_EVT_FEAT_PERIODIC |
  53. CLOCK_EVT_FEAT_ONESHOT,
  54. .set_state_shutdown = itimer_shutdown,
  55. .set_state_periodic = itimer_set_periodic,
  56. .set_state_oneshot = itimer_one_shot,
  57. .set_next_event = itimer_next_event,
  58. .shift = 0,
  59. .max_delta_ns = 0xffffffff,
  60. .min_delta_ns = TIMER_MIN_DELTA, //microsecond resolution should be enough for anyone, same as 640K RAM
  61. .irq = 0,
  62. .mult = 1,
  63. };
  64. static irqreturn_t um_timer(int irq, void *dev)
  65. {
  66. if (get_current()->mm != NULL)
  67. {
  68. /* userspace - relay signal, results in correct userspace timers */
  69. os_alarm_process(get_current()->mm->context.id.u.pid);
  70. }
  71. (*timer_clockevent.event_handler)(&timer_clockevent);
  72. return IRQ_HANDLED;
  73. }
  74. static cycle_t timer_read(struct clocksource *cs)
  75. {
  76. return os_nsecs() / TIMER_MULTIPLIER;
  77. }
  78. static struct clocksource timer_clocksource = {
  79. .name = "timer",
  80. .rating = 300,
  81. .read = timer_read,
  82. .mask = CLOCKSOURCE_MASK(64),
  83. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  84. };
  85. static void __init timer_setup(void)
  86. {
  87. int err;
  88. err = request_irq(TIMER_IRQ, um_timer, IRQF_TIMER, "hr timer", NULL);
  89. if (err != 0)
  90. printk(KERN_ERR "register_timer : request_irq failed - "
  91. "errno = %d\n", -err);
  92. err = os_timer_create(NULL);
  93. if (err != 0) {
  94. printk(KERN_ERR "creation of timer failed - errno = %d\n", -err);
  95. return;
  96. }
  97. err = clocksource_register_hz(&timer_clocksource, NSEC_PER_SEC/TIMER_MULTIPLIER);
  98. if (err) {
  99. printk(KERN_ERR "clocksource_register_hz returned %d\n", err);
  100. return;
  101. }
  102. clockevents_register_device(&timer_clockevent);
  103. }
  104. void read_persistent_clock(struct timespec *ts)
  105. {
  106. long long nsecs = os_persistent_clock_emulation();
  107. set_normalized_timespec(ts, nsecs / NSEC_PER_SEC,
  108. nsecs % NSEC_PER_SEC);
  109. }
  110. void __init time_init(void)
  111. {
  112. timer_set_signal_handler();
  113. late_time_init = timer_setup;
  114. }