time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/paravirt.h>
  18. #include <asm/time.h>
  19. u64 cpu_clock_freq;
  20. EXPORT_SYMBOL(cpu_clock_freq);
  21. u64 const_clock_freq;
  22. EXPORT_SYMBOL(const_clock_freq);
  23. static DEFINE_RAW_SPINLOCK(state_lock);
  24. static DEFINE_PER_CPU(struct clock_event_device, constant_clockevent_device);
  25. static void constant_event_handler(struct clock_event_device *dev)
  26. {
  27. }
  28. static irqreturn_t constant_timer_interrupt(int irq, void *data)
  29. {
  30. int cpu = smp_processor_id();
  31. struct clock_event_device *cd;
  32. /* Clear Timer Interrupt */
  33. write_csr_tintclear(CSR_TINTCLR_TI);
  34. cd = &per_cpu(constant_clockevent_device, cpu);
  35. cd->event_handler(cd);
  36. return IRQ_HANDLED;
  37. }
  38. static int constant_set_state_oneshot(struct clock_event_device *evt)
  39. {
  40. unsigned long timer_config;
  41. raw_spin_lock(&state_lock);
  42. timer_config = csr_read64(LOONGARCH_CSR_TCFG);
  43. timer_config |= CSR_TCFG_EN;
  44. timer_config &= ~CSR_TCFG_PERIOD;
  45. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  46. raw_spin_unlock(&state_lock);
  47. return 0;
  48. }
  49. static int constant_set_state_periodic(struct clock_event_device *evt)
  50. {
  51. unsigned long period;
  52. unsigned long timer_config;
  53. raw_spin_lock(&state_lock);
  54. period = const_clock_freq / HZ;
  55. timer_config = period & CSR_TCFG_VAL;
  56. timer_config |= (CSR_TCFG_PERIOD | CSR_TCFG_EN);
  57. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  58. raw_spin_unlock(&state_lock);
  59. return 0;
  60. }
  61. static int constant_set_state_shutdown(struct clock_event_device *evt)
  62. {
  63. unsigned long timer_config;
  64. raw_spin_lock(&state_lock);
  65. timer_config = csr_read64(LOONGARCH_CSR_TCFG);
  66. timer_config &= ~CSR_TCFG_EN;
  67. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  68. raw_spin_unlock(&state_lock);
  69. return 0;
  70. }
  71. static int constant_timer_next_event(unsigned long delta, struct clock_event_device *evt)
  72. {
  73. unsigned long timer_config;
  74. delta &= CSR_TCFG_VAL;
  75. timer_config = delta | CSR_TCFG_EN;
  76. csr_write64(timer_config, LOONGARCH_CSR_TCFG);
  77. return 0;
  78. }
  79. static unsigned long __init get_loops_per_jiffy(void)
  80. {
  81. unsigned long lpj = (unsigned long)const_clock_freq;
  82. do_div(lpj, HZ);
  83. return lpj;
  84. }
  85. static long init_offset __nosavedata;
  86. void save_counter(void)
  87. {
  88. init_offset = drdtime();
  89. }
  90. void sync_counter(void)
  91. {
  92. /* Ensure counter begin at 0 */
  93. csr_write64(init_offset, LOONGARCH_CSR_CNTC);
  94. }
  95. int constant_clockevent_init(void)
  96. {
  97. unsigned int cpu = smp_processor_id();
  98. unsigned long min_delta = 0x600;
  99. unsigned long max_delta = (1UL << 48) - 1;
  100. struct clock_event_device *cd;
  101. static int irq = 0, timer_irq_installed = 0;
  102. if (!timer_irq_installed) {
  103. irq = get_percpu_irq(INT_TI);
  104. if (irq < 0)
  105. pr_err("Failed to map irq %d (timer)\n", irq);
  106. }
  107. cd = &per_cpu(constant_clockevent_device, cpu);
  108. cd->name = "Constant";
  109. cd->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_PERCPU;
  110. cd->irq = irq;
  111. cd->rating = 320;
  112. cd->cpumask = cpumask_of(cpu);
  113. cd->set_state_oneshot = constant_set_state_oneshot;
  114. cd->set_state_oneshot_stopped = constant_set_state_shutdown;
  115. cd->set_state_periodic = constant_set_state_periodic;
  116. cd->set_state_shutdown = constant_set_state_shutdown;
  117. cd->set_next_event = constant_timer_next_event;
  118. cd->event_handler = constant_event_handler;
  119. clockevents_config_and_register(cd, const_clock_freq, min_delta, max_delta);
  120. if (timer_irq_installed)
  121. return 0;
  122. timer_irq_installed = 1;
  123. sync_counter();
  124. if (request_irq(irq, constant_timer_interrupt, IRQF_PERCPU | IRQF_TIMER, "timer", NULL))
  125. pr_err("Failed to request irq %d (timer)\n", irq);
  126. lpj_fine = get_loops_per_jiffy();
  127. pr_info("Constant clock event device register\n");
  128. return 0;
  129. }
  130. static u64 read_const_counter(struct clocksource *clk)
  131. {
  132. return drdtime();
  133. }
  134. static noinstr u64 sched_clock_read(void)
  135. {
  136. return drdtime();
  137. }
  138. static struct clocksource clocksource_const = {
  139. .name = "Constant",
  140. .rating = 400,
  141. .read = read_const_counter,
  142. .mask = CLOCKSOURCE_MASK(64),
  143. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  144. .vdso_clock_mode = VDSO_CLOCKMODE_CPU,
  145. };
  146. int __init constant_clocksource_init(void)
  147. {
  148. int res;
  149. unsigned long freq = const_clock_freq;
  150. res = clocksource_register_hz(&clocksource_const, freq);
  151. sched_clock_register(sched_clock_read, 64, freq);
  152. pr_info("Constant clock source device register\n");
  153. return res;
  154. }
  155. void __init time_init(void)
  156. {
  157. if (!cpu_has_cpucfg)
  158. const_clock_freq = cpu_clock_freq;
  159. else
  160. const_clock_freq = calc_const_freq();
  161. init_offset = -(drdtime() - csr_read64(LOONGARCH_CSR_CNTC));
  162. constant_clockevent_init();
  163. constant_clocksource_init();
  164. pv_time_init();
  165. }