timer-riscv.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. * All RISC-V systems have a timer attached to every hart. These timers can
  7. * either be read from the "time" and "timeh" CSRs, and can use the SBI to
  8. * setup events, or directly accessed using MMIO registers.
  9. */
  10. #define pr_fmt(fmt) "riscv-timer: " fmt
  11. #include <linux/clocksource.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/cpu.h>
  14. #include <linux/delay.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqdomain.h>
  17. #include <linux/module.h>
  18. #include <linux/sched_clock.h>
  19. #include <linux/io-64-nonatomic-lo-hi.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/of_irq.h>
  22. #include <clocksource/timer-riscv.h>
  23. #include <asm/smp.h>
  24. #include <asm/hwcap.h>
  25. #include <asm/sbi.h>
  26. #include <asm/timex.h>
  27. static DEFINE_STATIC_KEY_FALSE(riscv_sstc_available);
  28. static int riscv_clock_next_event(unsigned long delta,
  29. struct clock_event_device *ce)
  30. {
  31. u64 next_tval = get_cycles64() + delta;
  32. csr_set(CSR_IE, IE_TIE);
  33. if (static_branch_likely(&riscv_sstc_available)) {
  34. #if defined(CONFIG_32BIT)
  35. csr_write(CSR_STIMECMP, next_tval & 0xFFFFFFFF);
  36. csr_write(CSR_STIMECMPH, next_tval >> 32);
  37. #else
  38. csr_write(CSR_STIMECMP, next_tval);
  39. #endif
  40. } else
  41. sbi_set_timer(next_tval);
  42. return 0;
  43. }
  44. static unsigned int riscv_clock_event_irq;
  45. static DEFINE_PER_CPU(struct clock_event_device, riscv_clock_event) = {
  46. .name = "riscv_timer_clockevent",
  47. .features = CLOCK_EVT_FEAT_ONESHOT,
  48. .rating = 100,
  49. .set_next_event = riscv_clock_next_event,
  50. };
  51. /*
  52. * It is guaranteed that all the timers across all the harts are synchronized
  53. * within one tick of each other, so while this could technically go
  54. * backwards when hopping between CPUs, practically it won't happen.
  55. */
  56. static unsigned long long riscv_clocksource_rdtime(struct clocksource *cs)
  57. {
  58. return get_cycles64();
  59. }
  60. static u64 notrace riscv_sched_clock(void)
  61. {
  62. return get_cycles64();
  63. }
  64. static struct clocksource riscv_clocksource = {
  65. .name = "riscv_clocksource",
  66. .rating = 300,
  67. .mask = CLOCKSOURCE_MASK(64),
  68. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  69. .read = riscv_clocksource_rdtime,
  70. };
  71. static int riscv_timer_starting_cpu(unsigned int cpu)
  72. {
  73. struct clock_event_device *ce = per_cpu_ptr(&riscv_clock_event, cpu);
  74. ce->cpumask = cpumask_of(cpu);
  75. ce->irq = riscv_clock_event_irq;
  76. clockevents_config_and_register(ce, riscv_timebase, 100, 0x7fffffff);
  77. enable_percpu_irq(riscv_clock_event_irq,
  78. irq_get_trigger_type(riscv_clock_event_irq));
  79. return 0;
  80. }
  81. static int riscv_timer_dying_cpu(unsigned int cpu)
  82. {
  83. disable_percpu_irq(riscv_clock_event_irq);
  84. return 0;
  85. }
  86. void riscv_cs_get_mult_shift(u32 *mult, u32 *shift)
  87. {
  88. *mult = riscv_clocksource.mult;
  89. *shift = riscv_clocksource.shift;
  90. }
  91. EXPORT_SYMBOL_GPL(riscv_cs_get_mult_shift);
  92. /* called directly from the low-level interrupt handler */
  93. static irqreturn_t riscv_timer_interrupt(int irq, void *dev_id)
  94. {
  95. struct clock_event_device *evdev = this_cpu_ptr(&riscv_clock_event);
  96. csr_clear(CSR_IE, IE_TIE);
  97. evdev->event_handler(evdev);
  98. return IRQ_HANDLED;
  99. }
  100. static int __init riscv_timer_init_dt(struct device_node *n)
  101. {
  102. int cpuid, error;
  103. unsigned long hartid;
  104. struct device_node *child;
  105. struct irq_domain *domain;
  106. error = riscv_of_processor_hartid(n, &hartid);
  107. if (error < 0) {
  108. pr_warn("Not valid hartid for node [%pOF] error = [%lu]\n",
  109. n, hartid);
  110. return error;
  111. }
  112. cpuid = riscv_hartid_to_cpuid(hartid);
  113. if (cpuid < 0) {
  114. pr_warn("Invalid cpuid for hartid [%lu]\n", hartid);
  115. return cpuid;
  116. }
  117. if (cpuid != smp_processor_id())
  118. return 0;
  119. domain = NULL;
  120. child = of_get_compatible_child(n, "riscv,cpu-intc");
  121. if (!child) {
  122. pr_err("Failed to find INTC node [%pOF]\n", n);
  123. return -ENODEV;
  124. }
  125. domain = irq_find_host(child);
  126. of_node_put(child);
  127. if (!domain) {
  128. pr_err("Failed to find IRQ domain for node [%pOF]\n", n);
  129. return -ENODEV;
  130. }
  131. riscv_clock_event_irq = irq_create_mapping(domain, RV_IRQ_TIMER);
  132. if (!riscv_clock_event_irq) {
  133. pr_err("Failed to map timer interrupt for node [%pOF]\n", n);
  134. return -ENODEV;
  135. }
  136. pr_info("%s: Registering clocksource cpuid [%d] hartid [%lu]\n",
  137. __func__, cpuid, hartid);
  138. error = clocksource_register_hz(&riscv_clocksource, riscv_timebase);
  139. if (error) {
  140. pr_err("RISCV timer register failed [%d] for cpu = [%d]\n",
  141. error, cpuid);
  142. return error;
  143. }
  144. sched_clock_register(riscv_sched_clock, 64, riscv_timebase);
  145. error = request_percpu_irq(riscv_clock_event_irq,
  146. riscv_timer_interrupt,
  147. "riscv-timer", &riscv_clock_event);
  148. if (error) {
  149. pr_err("registering percpu irq failed [%d]\n", error);
  150. return error;
  151. }
  152. if (riscv_isa_extension_available(NULL, SSTC)) {
  153. pr_info("Timer interrupt in S-mode is available via sstc extension\n");
  154. static_branch_enable(&riscv_sstc_available);
  155. }
  156. error = cpuhp_setup_state(CPUHP_AP_RISCV_TIMER_STARTING,
  157. "clockevents/riscv/timer:starting",
  158. riscv_timer_starting_cpu, riscv_timer_dying_cpu);
  159. if (error)
  160. pr_err("cpu hp setup state failed for RISCV timer [%d]\n",
  161. error);
  162. return error;
  163. }
  164. TIMER_OF_DECLARE(riscv_timer, "riscv", riscv_timer_init_dt);