riscv_timer.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. * Copyright (C) 2017 SiFive
  5. */
  6. #include <linux/clocksource.h>
  7. #include <linux/clockchips.h>
  8. #include <linux/cpu.h>
  9. #include <linux/delay.h>
  10. #include <linux/irq.h>
  11. #include <asm/sbi.h>
  12. /*
  13. * All RISC-V systems have a timer attached to every hart. These timers can be
  14. * read by the 'rdcycle' pseudo instruction, and can use the SBI to setup
  15. * events. In order to abstract the architecture-specific timer reading and
  16. * setting functions away from the clock event insertion code, we provide
  17. * function pointers to the clockevent subsystem that perform two basic
  18. * operations: rdtime() reads the timer on the current CPU, and
  19. * next_event(delta) sets the next timer event to 'delta' cycles in the future.
  20. * As the timers are inherently a per-cpu resource, these callbacks perform
  21. * operations on the current hart. There is guaranteed to be exactly one timer
  22. * per hart on all RISC-V systems.
  23. */
  24. static int riscv_clock_next_event(unsigned long delta,
  25. struct clock_event_device *ce)
  26. {
  27. csr_set(sie, SIE_STIE);
  28. sbi_set_timer(get_cycles64() + delta);
  29. return 0;
  30. }
  31. static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
  32. .name = "riscv_timer_clockevent",
  33. .features = CLOCK_EVT_FEAT_ONESHOT,
  34. .rating = 100,
  35. .set_next_event = riscv_clock_next_event,
  36. };
  37. /*
  38. * It is guaranteed that all the timers across all the harts are synchronized
  39. * within one tick of each other, so while this could technically go
  40. * backwards when hopping between CPUs, practically it won't happen.
  41. */
  42. static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
  43. {
  44. return get_cycles64();
  45. }
  46. static DEFINE_PER_CPU(struct clocksource, riscv_clocksource) = {
  47. .name = "riscv_clocksource",
  48. .rating = 300,
  49. .mask = CLOCKSOURCE_MASK(BITS_PER_LONG),
  50. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  51. .read = riscv_clocksource_rdtime,
  52. };
  53. static int riscv_timer_starting_cpu(unsigned int cpu)
  54. {
  55. struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
  56. ce->cpumask = cpumask_of(cpu);
  57. clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
  58. csr_set(sie, SIE_STIE);
  59. return 0;
  60. }
  61. static int riscv_timer_dying_cpu(unsigned int cpu)
  62. {
  63. csr_clear(sie, SIE_STIE);
  64. return 0;
  65. }
  66. /* called directly from the low-level interrupt handler */
  67. void riscv_timer_interrupt(void)
  68. {
  69. struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
  70. csr_clear(sie, SIE_STIE);
  71. evdev->event_handler(evdev);
  72. }
  73. static int __init riscv_timer_init_dt(struct device_node *n)
  74. {
  75. int cpu_id = riscv_of_processor_hart(n), error;
  76. struct clocksource *cs;
  77. if (cpu_id != smp_processor_id())
  78. return 0;
  79. cs = per_cpu_ptr(&riscv_clocksource, cpu_id);
  80. clocksource_register_hz(cs, riscv_timebase);
  81. error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
  82. "clockevents/riscv/timer:starting",
  83. riscv_timer_starting_cpu, riscv_timer_dying_cpu);
  84. if (error)
  85. pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
  86. error, cpu_id);
  87. return error;
  88. }
  89. TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);