time.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Common time service routines for LoongArch machines.
  4. *
  5. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  6. */
  7. #include <linux/clockchips.h>
  8. #include <linux/delay.h>
  9. #include <linux/export.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched_clock.h>
  14. #include <linux/spinlock.h>
  15. #include <asm/cpu-features.h>
  16. #include <asm/loongarch.h>
  17. #include <asm/time.h>
  18. u64 cpu_clock_freq;
  19. EXPORT_SYMBOL(cpu_clock_freq);
  20. u64 const_clock_freq;
  21. EXPORT_SYMBOL(const_clock_freq);
  22. static DEFINE_RAW_SPINLOCK(state_lock);
  23. static DEFINE_PER_CPU(struct clock_event_device, constant_clockevent_device);
  24. static void constant_event_handler(struct clock_event_device *dev)
  25. {
  26. }
  27. irqreturn_t constant_timer_interrupt(int irq, void *data)
  28. {
  29. int cpu = smp_processor_id();
  30. struct clock_event_device *cd;
  31. /* Clear Timer Interrupt */
  32. write_csr_tintclear(CSR_TINTCLR_TI);
  33. cd = &per_cpu(constant_clockevent_device, cpu);
  34. cd->event_handler(cd);
  35. return IRQ_HANDLED;
  36. }
  37. static int constant_set_state_oneshot(struct clock_event_device *evt)
  38. {
  39. unsigned long timer_config;
  40. raw_spin_lock(&state_lock);
  41. timer_config = csr_read64(LOONGARCH_CSR_TCFG);
  42. timer_config |= CSR_TCFG_EN;
  43. timer_config &= ~CSR_TCFG_PERIOD;
  44. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  45. raw_spin_unlock(&state_lock);
  46. return 0;
  47. }
  48. static int constant_set_state_oneshot_stopped(struct clock_event_device *evt)
  49. {
  50. unsigned long timer_config;
  51. raw_spin_lock(&state_lock);
  52. timer_config = csr_read64(LOONGARCH_CSR_TCFG);
  53. timer_config &= ~CSR_TCFG_EN;
  54. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  55. raw_spin_unlock(&state_lock);
  56. return 0;
  57. }
  58. static int constant_set_state_periodic(struct clock_event_device *evt)
  59. {
  60. unsigned long period;
  61. unsigned long timer_config;
  62. raw_spin_lock(&state_lock);
  63. period = const_clock_freq / HZ;
  64. timer_config = period & CSR_TCFG_VAL;
  65. timer_config |= (CSR_TCFG_PERIOD | CSR_TCFG_EN);
  66. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  67. raw_spin_unlock(&state_lock);
  68. return 0;
  69. }
  70. static int constant_set_state_shutdown(struct clock_event_device *evt)
  71. {
  72. return 0;
  73. }
  74. static int constant_timer_next_event(unsigned long delta, struct clock_event_device *evt)
  75. {
  76. unsigned long timer_config;
  77. delta &= CSR_TCFG_VAL;
  78. timer_config = delta | CSR_TCFG_EN;
  79. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  80. return 0;
  81. }
  82. static unsigned long __init get_loops_per_jiffy(void)
  83. {
  84. unsigned long lpj = (unsigned long)const_clock_freq;
  85. do_div(lpj, HZ);
  86. return lpj;
  87. }
  88. static long init_timeval;
  89. void sync_counter(void)
  90. {
  91. /* Ensure counter begin at 0 */
  92. csr_write64(-init_timeval, LOONGARCH_CSR_CNTC);
  93. }
  94. static int get_timer_irq(void)
  95. {
  96. struct irq_domain *d = irq_find_matching_fwnode(cpuintc_handle, DOMAIN_BUS_ANY);
  97. if (d)
  98. return irq_create_mapping(d, EXCCODE_TIMER - EXCCODE_INT_START);
  99. return -EINVAL;
  100. }
  101. int constant_clockevent_init(void)
  102. {
  103. unsigned int cpu = smp_processor_id();
  104. unsigned long min_delta = 0x600;
  105. unsigned long max_delta = (1UL << 48) - 1;
  106. struct clock_event_device *cd;
  107. static int irq = 0, timer_irq_installed = 0;
  108. if (!timer_irq_installed) {
  109. irq = get_timer_irq();
  110. if (irq < 0)
  111. pr_err("Failed to map irq %d (timer)\n", irq);
  112. }
  113. cd = &per_cpu(constant_clockevent_device, cpu);
  114. cd->name = "Constant";
  115. cd->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_PERCPU;
  116. cd->irq = irq;
  117. cd->rating = 320;
  118. cd->cpumask = cpumask_of(cpu);
  119. cd->set_state_oneshot = constant_set_state_oneshot;
  120. cd->set_state_oneshot_stopped = constant_set_state_oneshot_stopped;
  121. cd->set_state_periodic = constant_set_state_periodic;
  122. cd->set_state_shutdown = constant_set_state_shutdown;
  123. cd->set_next_event = constant_timer_next_event;
  124. cd->event_handler = constant_event_handler;
  125. clockevents_config_and_register(cd, const_clock_freq, min_delta, max_delta);
  126. if (timer_irq_installed)
  127. return 0;
  128. timer_irq_installed = 1;
  129. sync_counter();
  130. if (request_irq(irq, constant_timer_interrupt, IRQF_PERCPU | IRQF_TIMER, "timer", NULL))
  131. pr_err("Failed to request irq %d (timer)\n", irq);
  132. lpj_fine = get_loops_per_jiffy();
  133. pr_info("Constant clock event device register\n");
  134. return 0;
  135. }
  136. static u64 read_const_counter(struct clocksource *clk)
  137. {
  138. return drdtime();
  139. }
  140. static u64 native_sched_clock(void)
  141. {
  142. return read_const_counter(NULL);
  143. }
  144. static struct clocksource clocksource_const = {
  145. .name = "Constant",
  146. .rating = 400,
  147. .read = read_const_counter,
  148. .mask = CLOCKSOURCE_MASK(64),
  149. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  150. .vdso_clock_mode = VDSO_CLOCKMODE_CPU,
  151. };
  152. int __init constant_clocksource_init(void)
  153. {
  154. int res;
  155. unsigned long freq = const_clock_freq;
  156. res = clocksource_register_hz(&clocksource_const, freq);
  157. sched_clock_register(native_sched_clock, 64, freq);
  158. pr_info("Constant clock source device register\n");
  159. return res;
  160. }
  161. void __init time_init(void)
  162. {
  163. if (!cpu_has_cpucfg)
  164. const_clock_freq = cpu_clock_freq;
  165. else
  166. const_clock_freq = calc_const_freq();
  167. init_timeval = drdtime() - csr_read64(LOONGARCH_CSR_CNTC);
  168. constant_clockevent_init();
  169. constant_clocksource_init();
  170. }