timer-clint.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020 Western Digital Corporation or its affiliates.
  4. *
  5. * Most of the M-mode (i.e. NoMMU) RISC-V systems usually have a
  6. * CLINT MMIO timer device.
  7. */
  8. #define pr_fmt(fmt) "clint: " fmt
  9. #include <linux/bitops.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/cpu.h>
  13. #include <linux/delay.h>
  14. #include <linux/module.h>
  15. #include <linux/of_address.h>
  16. #include <linux/sched_clock.h>
  17. #include <linux/io-64-nonatomic-lo-hi.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/smp.h>
  21. #include <linux/timex.h>
  22. #ifndef CONFIG_RISCV_M_MODE
  23. #include <asm/clint.h>
  24. #endif
  25. #define CLINT_IPI_OFF 0
  26. #define CLINT_TIMER_CMP_OFF 0x4000
  27. #define CLINT_TIMER_VAL_OFF 0xbff8
  28. /* CLINT manages IPI and Timer for RISC-V M-mode */
  29. static u32 __iomem *clint_ipi_base;
  30. static u64 __iomem *clint_timer_cmp;
  31. static u64 __iomem *clint_timer_val;
  32. static unsigned long clint_timer_freq;
  33. static unsigned int clint_timer_irq;
  34. #ifdef CONFIG_RISCV_M_MODE
  35. u64 __iomem *clint_time_val;
  36. EXPORT_SYMBOL(clint_time_val);
  37. #endif
  38. static void clint_send_ipi(const struct cpumask *target)
  39. {
  40. unsigned int cpu;
  41. for_each_cpu(cpu, target)
  42. writel(1, clint_ipi_base + cpuid_to_hartid_map(cpu));
  43. }
  44. static void clint_clear_ipi(void)
  45. {
  46. writel(0, clint_ipi_base + cpuid_to_hartid_map(smp_processor_id()));
  47. }
  48. static struct riscv_ipi_ops clint_ipi_ops = {
  49. .ipi_inject = clint_send_ipi,
  50. .ipi_clear = clint_clear_ipi,
  51. };
  52. #ifdef CONFIG_64BIT
  53. #define clint_get_cycles() readq_relaxed(clint_timer_val)
  54. #else
  55. #define clint_get_cycles() readl_relaxed(clint_timer_val)
  56. #define clint_get_cycles_hi() readl_relaxed(((u32 *)clint_timer_val) + 1)
  57. #endif
  58. #ifdef CONFIG_64BIT
  59. static u64 notrace clint_get_cycles64(void)
  60. {
  61. return clint_get_cycles();
  62. }
  63. #else /* CONFIG_64BIT */
  64. static u64 notrace clint_get_cycles64(void)
  65. {
  66. u32 hi, lo;
  67. do {
  68. hi = clint_get_cycles_hi();
  69. lo = clint_get_cycles();
  70. } while (hi != clint_get_cycles_hi());
  71. return ((u64)hi << 32) | lo;
  72. }
  73. #endif /* CONFIG_64BIT */
  74. static u64 clint_rdtime(struct clocksource *cs)
  75. {
  76. return clint_get_cycles64();
  77. }
  78. static struct clocksource clint_clocksource = {
  79. .name = "clint_clocksource",
  80. .rating = 300,
  81. .mask = CLOCKSOURCE_MASK(64),
  82. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  83. .read = clint_rdtime,
  84. };
  85. static int clint_clock_next_event(unsigned long delta,
  86. struct clock_event_device *ce)
  87. {
  88. void __iomem *r = clint_timer_cmp +
  89. cpuid_to_hartid_map(smp_processor_id());
  90. csr_set(CSR_IE, IE_TIE);
  91. writeq_relaxed(clint_get_cycles64() + delta, r);
  92. return 0;
  93. }
  94. static DEFINE_PER_CPU(struct clock_event_device, clint_clock_event) = {
  95. .name = "clint_clockevent",
  96. .features = CLOCK_EVT_FEAT_ONESHOT,
  97. .rating = 100,
  98. .set_next_event = clint_clock_next_event,
  99. };
  100. static int clint_timer_starting_cpu(unsigned int cpu)
  101. {
  102. struct clock_event_device *ce = per_cpu_ptr(&clint_clock_event, cpu);
  103. ce->cpumask = cpumask_of(cpu);
  104. clockevents_config_and_register(ce, clint_timer_freq, 100, 0x7fffffff);
  105. enable_percpu_irq(clint_timer_irq,
  106. irq_get_trigger_type(clint_timer_irq));
  107. return 0;
  108. }
  109. static int clint_timer_dying_cpu(unsigned int cpu)
  110. {
  111. disable_percpu_irq(clint_timer_irq);
  112. return 0;
  113. }
  114. static irqreturn_t clint_timer_interrupt(int irq, void *dev_id)
  115. {
  116. struct clock_event_device *evdev = this_cpu_ptr(&clint_clock_event);
  117. csr_clear(CSR_IE, IE_TIE);
  118. evdev->event_handler(evdev);
  119. return IRQ_HANDLED;
  120. }
  121. static int __init clint_timer_init_dt(struct device_node *np)
  122. {
  123. int rc;
  124. u32 i, nr_irqs;
  125. void __iomem *base;
  126. struct of_phandle_args oirq;
  127. /*
  128. * Ensure that CLINT device interrupts are either RV_IRQ_TIMER or
  129. * RV_IRQ_SOFT. If it's anything else then we ignore the device.
  130. */
  131. nr_irqs = of_irq_count(np);
  132. for (i = 0; i < nr_irqs; i++) {
  133. if (of_irq_parse_one(np, i, &oirq)) {
  134. pr_err("%pOFP: failed to parse irq %d.\n", np, i);
  135. continue;
  136. }
  137. if ((oirq.args_count != 1) ||
  138. (oirq.args[0] != RV_IRQ_TIMER &&
  139. oirq.args[0] != RV_IRQ_SOFT)) {
  140. pr_err("%pOFP: invalid irq %d (hwirq %d)\n",
  141. np, i, oirq.args[0]);
  142. return -ENODEV;
  143. }
  144. /* Find parent irq domain and map timer irq */
  145. if (!clint_timer_irq &&
  146. oirq.args[0] == RV_IRQ_TIMER &&
  147. irq_find_host(oirq.np))
  148. clint_timer_irq = irq_of_parse_and_map(np, i);
  149. }
  150. /* If CLINT timer irq not found then fail */
  151. if (!clint_timer_irq) {
  152. pr_err("%pOFP: timer irq not found\n", np);
  153. return -ENODEV;
  154. }
  155. base = of_iomap(np, 0);
  156. if (!base) {
  157. pr_err("%pOFP: could not map registers\n", np);
  158. return -ENODEV;
  159. }
  160. clint_ipi_base = base + CLINT_IPI_OFF;
  161. clint_timer_cmp = base + CLINT_TIMER_CMP_OFF;
  162. clint_timer_val = base + CLINT_TIMER_VAL_OFF;
  163. clint_timer_freq = riscv_timebase;
  164. #ifdef CONFIG_RISCV_M_MODE
  165. /*
  166. * Yes, that's an odd naming scheme. time_val is public, but hopefully
  167. * will die in favor of something cleaner.
  168. */
  169. clint_time_val = clint_timer_val;
  170. #endif
  171. pr_info("%pOFP: timer running at %ld Hz\n", np, clint_timer_freq);
  172. rc = clocksource_register_hz(&clint_clocksource, clint_timer_freq);
  173. if (rc) {
  174. pr_err("%pOFP: clocksource register failed [%d]\n", np, rc);
  175. goto fail_iounmap;
  176. }
  177. sched_clock_register(clint_get_cycles64, 64, clint_timer_freq);
  178. rc = request_percpu_irq(clint_timer_irq, clint_timer_interrupt,
  179. "clint-timer", &clint_clock_event);
  180. if (rc) {
  181. pr_err("registering percpu irq failed [%d]\n", rc);
  182. goto fail_iounmap;
  183. }
  184. rc = cpuhp_setup_state(CPUHP_AP_CLINT_TIMER_STARTING,
  185. "clockevents/clint/timer:starting",
  186. clint_timer_starting_cpu,
  187. clint_timer_dying_cpu);
  188. if (rc) {
  189. pr_err("%pOFP: cpuhp setup state failed [%d]\n", np, rc);
  190. goto fail_free_irq;
  191. }
  192. riscv_set_ipi_ops(&clint_ipi_ops);
  193. clint_clear_ipi();
  194. return 0;
  195. fail_free_irq:
  196. free_irq(clint_timer_irq, &clint_clock_event);
  197. fail_iounmap:
  198. iounmap(base);
  199. return rc;
  200. }
  201. TIMER_OF_DECLARE(clint_timer, "riscv,clint0", clint_timer_init_dt);
  202. TIMER_OF_DECLARE(clint_timer1, "sifive,clint0", clint_timer_init_dt);