timer-clint.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/irq.h>
  20. #include <linux/irqchip/chained_irq.h>
  21. #include <linux/irqdomain.h>
  22. #include <linux/of_irq.h>
  23. #include <linux/smp.h>
  24. #include <linux/timex.h>
  25. #ifndef CONFIG_RISCV_M_MODE
  26. #include <asm/clint.h>
  27. #endif
  28. #define CLINT_IPI_OFF 0
  29. #define CLINT_TIMER_CMP_OFF 0x4000
  30. #define CLINT_TIMER_VAL_OFF 0xbff8
  31. /* CLINT manages IPI and Timer for RISC-V M-mode */
  32. static u32 __iomem *clint_ipi_base;
  33. static unsigned int clint_ipi_irq;
  34. static u64 __iomem *clint_timer_cmp;
  35. static u64 __iomem *clint_timer_val;
  36. static unsigned long clint_timer_freq;
  37. static unsigned int clint_timer_irq;
  38. #ifdef CONFIG_RISCV_M_MODE
  39. u64 __iomem *clint_time_val;
  40. EXPORT_SYMBOL(clint_time_val);
  41. #endif
  42. #ifdef CONFIG_SMP
  43. static void clint_send_ipi(unsigned int cpu)
  44. {
  45. writel(1, clint_ipi_base + cpuid_to_hartid_map(cpu));
  46. }
  47. static void clint_clear_ipi(void)
  48. {
  49. writel(0, clint_ipi_base + cpuid_to_hartid_map(smp_processor_id()));
  50. }
  51. static void clint_ipi_interrupt(struct irq_desc *desc)
  52. {
  53. struct irq_chip *chip = irq_desc_get_chip(desc);
  54. chained_irq_enter(chip, desc);
  55. clint_clear_ipi();
  56. ipi_mux_process();
  57. chained_irq_exit(chip, desc);
  58. }
  59. #endif
  60. #ifdef CONFIG_64BIT
  61. #define clint_get_cycles() readq_relaxed(clint_timer_val)
  62. #else
  63. #define clint_get_cycles() readl_relaxed(clint_timer_val)
  64. #define clint_get_cycles_hi() readl_relaxed(((u32 *)clint_timer_val) + 1)
  65. #endif
  66. #ifdef CONFIG_64BIT
  67. static u64 notrace clint_get_cycles64(void)
  68. {
  69. return clint_get_cycles();
  70. }
  71. #else /* CONFIG_64BIT */
  72. static u64 notrace clint_get_cycles64(void)
  73. {
  74. u32 hi, lo;
  75. do {
  76. hi = clint_get_cycles_hi();
  77. lo = clint_get_cycles();
  78. } while (hi != clint_get_cycles_hi());
  79. return ((u64)hi << 32) | lo;
  80. }
  81. #endif /* CONFIG_64BIT */
  82. static u64 clint_rdtime(struct clocksource *cs)
  83. {
  84. return clint_get_cycles64();
  85. }
  86. static struct clocksource clint_clocksource = {
  87. .name = "clint_clocksource",
  88. .rating = 300,
  89. .mask = CLOCKSOURCE_MASK(64),
  90. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  91. .read = clint_rdtime,
  92. };
  93. static int clint_clock_next_event(unsigned long delta,
  94. struct clock_event_device *ce)
  95. {
  96. void __iomem *r = clint_timer_cmp +
  97. cpuid_to_hartid_map(smp_processor_id());
  98. csr_set(CSR_IE, IE_TIE);
  99. writeq_relaxed(clint_get_cycles64() + delta, r);
  100. return 0;
  101. }
  102. static DEFINE_PER_CPU(struct clock_event_device, clint_clock_event) = {
  103. .name = "clint_clockevent",
  104. .features = CLOCK_EVT_FEAT_ONESHOT,
  105. .rating = 100,
  106. .set_next_event = clint_clock_next_event,
  107. };
  108. static int clint_timer_starting_cpu(unsigned int cpu)
  109. {
  110. struct clock_event_device *ce = per_cpu_ptr(&clint_clock_event, cpu);
  111. ce->cpumask = cpumask_of(cpu);
  112. clockevents_config_and_register(ce, clint_timer_freq, 100, ULONG_MAX);
  113. enable_percpu_irq(clint_timer_irq,
  114. irq_get_trigger_type(clint_timer_irq));
  115. enable_percpu_irq(clint_ipi_irq,
  116. irq_get_trigger_type(clint_ipi_irq));
  117. return 0;
  118. }
  119. static int clint_timer_dying_cpu(unsigned int cpu)
  120. {
  121. disable_percpu_irq(clint_timer_irq);
  122. /*
  123. * Don't disable IPI when CPU goes offline because
  124. * the masking/unmasking of virtual IPIs is done
  125. * via generic IPI-Mux
  126. */
  127. return 0;
  128. }
  129. static irqreturn_t clint_timer_interrupt(int irq, void *dev_id)
  130. {
  131. struct clock_event_device *evdev = this_cpu_ptr(&clint_clock_event);
  132. csr_clear(CSR_IE, IE_TIE);
  133. evdev->event_handler(evdev);
  134. return IRQ_HANDLED;
  135. }
  136. static int __init clint_timer_init_dt(struct device_node *np)
  137. {
  138. int rc;
  139. u32 i, nr_irqs;
  140. void __iomem *base;
  141. struct of_phandle_args oirq;
  142. /*
  143. * Ensure that CLINT device interrupts are either RV_IRQ_TIMER or
  144. * RV_IRQ_SOFT. If it's anything else then we ignore the device.
  145. */
  146. nr_irqs = of_irq_count(np);
  147. for (i = 0; i < nr_irqs; i++) {
  148. if (of_irq_parse_one(np, i, &oirq)) {
  149. pr_err("%pOFP: failed to parse irq %d.\n", np, i);
  150. continue;
  151. }
  152. if ((oirq.args_count != 1) ||
  153. (oirq.args[0] != RV_IRQ_TIMER &&
  154. oirq.args[0] != RV_IRQ_SOFT)) {
  155. pr_err("%pOFP: invalid irq %d (hwirq %d)\n",
  156. np, i, oirq.args[0]);
  157. return -ENODEV;
  158. }
  159. /* Find parent irq domain and map ipi irq */
  160. if (!clint_ipi_irq &&
  161. oirq.args[0] == RV_IRQ_SOFT &&
  162. irq_find_host(oirq.np))
  163. clint_ipi_irq = irq_of_parse_and_map(np, i);
  164. /* Find parent irq domain and map timer irq */
  165. if (!clint_timer_irq &&
  166. oirq.args[0] == RV_IRQ_TIMER &&
  167. irq_find_host(oirq.np))
  168. clint_timer_irq = irq_of_parse_and_map(np, i);
  169. }
  170. /* If CLINT ipi or timer irq not found then fail */
  171. if (!clint_ipi_irq || !clint_timer_irq) {
  172. pr_err("%pOFP: ipi/timer irq not found\n", np);
  173. return -ENODEV;
  174. }
  175. base = of_iomap(np, 0);
  176. if (!base) {
  177. pr_err("%pOFP: could not map registers\n", np);
  178. return -ENODEV;
  179. }
  180. clint_ipi_base = base + CLINT_IPI_OFF;
  181. clint_timer_cmp = base + CLINT_TIMER_CMP_OFF;
  182. clint_timer_val = base + CLINT_TIMER_VAL_OFF;
  183. clint_timer_freq = riscv_timebase;
  184. #ifdef CONFIG_RISCV_M_MODE
  185. /*
  186. * Yes, that's an odd naming scheme. time_val is public, but hopefully
  187. * will die in favor of something cleaner.
  188. */
  189. clint_time_val = clint_timer_val;
  190. #endif
  191. pr_info("%pOFP: timer running at %ld Hz\n", np, clint_timer_freq);
  192. rc = clocksource_register_hz(&clint_clocksource, clint_timer_freq);
  193. if (rc) {
  194. pr_err("%pOFP: clocksource register failed [%d]\n", np, rc);
  195. goto fail_iounmap;
  196. }
  197. sched_clock_register(clint_get_cycles64, 64, clint_timer_freq);
  198. rc = request_percpu_irq(clint_timer_irq, clint_timer_interrupt,
  199. "clint-timer", &clint_clock_event);
  200. if (rc) {
  201. pr_err("registering percpu irq failed [%d]\n", rc);
  202. goto fail_iounmap;
  203. }
  204. #ifdef CONFIG_SMP
  205. rc = ipi_mux_create(BITS_PER_BYTE, clint_send_ipi);
  206. if (rc <= 0) {
  207. pr_err("unable to create muxed IPIs\n");
  208. rc = (rc < 0) ? rc : -ENODEV;
  209. goto fail_free_irq;
  210. }
  211. irq_set_chained_handler(clint_ipi_irq, clint_ipi_interrupt);
  212. riscv_ipi_set_virq_range(rc, BITS_PER_BYTE);
  213. clint_clear_ipi();
  214. #endif
  215. rc = cpuhp_setup_state(CPUHP_AP_CLINT_TIMER_STARTING,
  216. "clockevents/clint/timer:starting",
  217. clint_timer_starting_cpu,
  218. clint_timer_dying_cpu);
  219. if (rc) {
  220. pr_err("%pOFP: cpuhp setup state failed [%d]\n", np, rc);
  221. goto fail_free_irq;
  222. }
  223. return 0;
  224. fail_free_irq:
  225. free_percpu_irq(clint_timer_irq, &clint_clock_event);
  226. fail_iounmap:
  227. iounmap(base);
  228. return rc;
  229. }
  230. TIMER_OF_DECLARE(clint_timer, "riscv,clint0", clint_timer_init_dt);
  231. TIMER_OF_DECLARE(clint_timer1, "sifive,clint0", clint_timer_init_dt);