time.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_possible_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. .max_delta_ticks = 0xffffffff,
  61. .min_delta_ns = TIMER_MIN_DELTA,
  62. .min_delta_ticks = TIMER_MIN_DELTA, // microsecond resolution should be enough for anyone, same as 640K RAM
  63. .irq = 0,
  64. .mult = 1,
  65. };
  66. static irqreturn_t um_timer(int irq, void *dev)
  67. {
  68. if (get_current()->mm != NULL)
  69. {
  70. /* userspace - relay signal, results in correct userspace timers */
  71. os_alarm_process(get_current()->mm->context.id.u.pid);
  72. }
  73. (*timer_clockevent.event_handler)(&timer_clockevent);
  74. return IRQ_HANDLED;
  75. }
  76. static u64 timer_read(struct clocksource *cs)
  77. {
  78. return os_nsecs() / TIMER_MULTIPLIER;
  79. }
  80. static struct clocksource timer_clocksource = {
  81. .name = "timer",
  82. .rating = 300,
  83. .read = timer_read,
  84. .mask = CLOCKSOURCE_MASK(64),
  85. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  86. };
  87. static void __init um_timer_setup(void)
  88. {
  89. int err;
  90. err = request_irq(TIMER_IRQ, um_timer, IRQF_TIMER, "hr timer", NULL);
  91. if (err != 0)
  92. printk(KERN_ERR "register_timer : request_irq failed - "
  93. "errno = %d\n", -err);
  94. err = os_timer_create(NULL);
  95. if (err != 0) {
  96. printk(KERN_ERR "creation of timer failed - errno = %d\n", -err);
  97. return;
  98. }
  99. err = clocksource_register_hz(&timer_clocksource, NSEC_PER_SEC/TIMER_MULTIPLIER);
  100. if (err) {
  101. printk(KERN_ERR "clocksource_register_hz returned %d\n", err);
  102. return;
  103. }
  104. clockevents_register_device(&timer_clockevent);
  105. }
  106. void read_persistent_clock64(struct timespec64 *ts)
  107. {
  108. long long nsecs = os_persistent_clock_emulation();
  109. set_normalized_timespec64(ts, nsecs / NSEC_PER_SEC,
  110. nsecs % NSEC_PER_SEC);
  111. }
  112. void __init time_init(void)
  113. {
  114. timer_set_signal_handler();
  115. late_time_init = um_timer_setup;
  116. }