mips-gic-timer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (C) 2012 MIPS Technologies, Inc. All rights reserved.
  3. #define pr_fmt(fmt) "mips-gic-timer: " fmt
  4. #include <linux/clk.h>
  5. #include <linux/clockchips.h>
  6. #include <linux/cpu.h>
  7. #include <linux/init.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/notifier.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/percpu.h>
  12. #include <linux/sched_clock.h>
  13. #include <linux/smp.h>
  14. #include <linux/time.h>
  15. #include <asm/mips-cps.h>
  16. static DEFINE_PER_CPU(struct clock_event_device, gic_clockevent_device);
  17. static int gic_timer_irq;
  18. static unsigned int gic_frequency;
  19. static unsigned int gic_count_width;
  20. static bool __read_mostly gic_clock_unstable;
  21. static void gic_clocksource_unstable(char *reason);
  22. static u64 notrace gic_read_count_2x32(void)
  23. {
  24. unsigned int hi, hi2, lo;
  25. do {
  26. hi = read_gic_counter_32h();
  27. lo = read_gic_counter_32l();
  28. hi2 = read_gic_counter_32h();
  29. } while (hi2 != hi);
  30. return (((u64) hi) << 32) + lo;
  31. }
  32. static u64 notrace gic_read_count_64(void)
  33. {
  34. return read_gic_counter();
  35. }
  36. static u64 notrace gic_read_count(void)
  37. {
  38. if (mips_cm_is64)
  39. return gic_read_count_64();
  40. return gic_read_count_2x32();
  41. }
  42. static int gic_next_event(unsigned long delta, struct clock_event_device *evt)
  43. {
  44. int cpu = cpumask_first(evt->cpumask);
  45. u64 cnt;
  46. int res;
  47. cnt = gic_read_count();
  48. cnt += (u64)delta;
  49. if (cpu == raw_smp_processor_id()) {
  50. write_gic_vl_compare(cnt);
  51. } else {
  52. write_gic_vl_other(mips_cm_vp_id(cpu));
  53. write_gic_vo_compare(cnt);
  54. }
  55. res = ((int)(gic_read_count() - cnt) >= 0) ? -ETIME : 0;
  56. return res;
  57. }
  58. static irqreturn_t gic_compare_interrupt(int irq, void *dev_id)
  59. {
  60. struct clock_event_device *cd = dev_id;
  61. write_gic_vl_compare(read_gic_vl_compare());
  62. cd->event_handler(cd);
  63. return IRQ_HANDLED;
  64. }
  65. static struct irqaction gic_compare_irqaction = {
  66. .handler = gic_compare_interrupt,
  67. .percpu_dev_id = &gic_clockevent_device,
  68. .flags = IRQF_PERCPU | IRQF_TIMER,
  69. .name = "timer",
  70. };
  71. static void gic_clockevent_cpu_init(unsigned int cpu,
  72. struct clock_event_device *cd)
  73. {
  74. cd->name = "MIPS GIC";
  75. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  76. CLOCK_EVT_FEAT_C3STOP;
  77. cd->rating = 350;
  78. cd->irq = gic_timer_irq;
  79. cd->cpumask = cpumask_of(cpu);
  80. cd->set_next_event = gic_next_event;
  81. clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
  82. enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
  83. }
  84. static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
  85. {
  86. disable_percpu_irq(gic_timer_irq);
  87. }
  88. static void gic_update_frequency(void *data)
  89. {
  90. unsigned long rate = (unsigned long)data;
  91. clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
  92. }
  93. static int gic_starting_cpu(unsigned int cpu)
  94. {
  95. /* Ensure the GIC counter is running */
  96. clear_gic_config(GIC_CONFIG_COUNTSTOP);
  97. gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
  98. return 0;
  99. }
  100. static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
  101. void *data)
  102. {
  103. struct clk_notifier_data *cnd = data;
  104. if (action == POST_RATE_CHANGE) {
  105. gic_clocksource_unstable("ref clock rate change");
  106. on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
  107. }
  108. return NOTIFY_OK;
  109. }
  110. static int gic_dying_cpu(unsigned int cpu)
  111. {
  112. gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
  113. return 0;
  114. }
  115. static struct notifier_block gic_clk_nb = {
  116. .notifier_call = gic_clk_notifier,
  117. };
  118. static int gic_clockevent_init(void)
  119. {
  120. int ret;
  121. if (!gic_frequency)
  122. return -ENXIO;
  123. ret = setup_percpu_irq(gic_timer_irq, &gic_compare_irqaction);
  124. if (ret < 0) {
  125. pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret);
  126. return ret;
  127. }
  128. cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
  129. "clockevents/mips/gic/timer:starting",
  130. gic_starting_cpu, gic_dying_cpu);
  131. return 0;
  132. }
  133. static u64 gic_hpt_read(struct clocksource *cs)
  134. {
  135. return gic_read_count();
  136. }
  137. static struct clocksource gic_clocksource = {
  138. .name = "GIC",
  139. .read = gic_hpt_read,
  140. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  141. .vdso_clock_mode = VDSO_CLOCKMODE_GIC,
  142. };
  143. static void gic_clocksource_unstable(char *reason)
  144. {
  145. if (gic_clock_unstable)
  146. return;
  147. gic_clock_unstable = true;
  148. pr_info("GIC timer is unstable due to %s\n", reason);
  149. clocksource_mark_unstable(&gic_clocksource);
  150. }
  151. static int __init __gic_clocksource_init(void)
  152. {
  153. int ret;
  154. /* Set clocksource mask. */
  155. gic_count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
  156. gic_count_width >>= __ffs(GIC_CONFIG_COUNTBITS);
  157. gic_count_width *= 4;
  158. gic_count_width += 32;
  159. gic_clocksource.mask = CLOCKSOURCE_MASK(gic_count_width);
  160. /* Calculate a somewhat reasonable rating value. */
  161. if (mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ))
  162. gic_clocksource.rating = 300; /* Good when frequecy is stable */
  163. else
  164. gic_clocksource.rating = 200;
  165. gic_clocksource.rating += clamp(gic_frequency / 10000000, 0, 99);
  166. ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
  167. if (ret < 0)
  168. pr_warn("Unable to register clocksource\n");
  169. return ret;
  170. }
  171. static int __init gic_clocksource_of_init(struct device_node *node)
  172. {
  173. struct clk *clk;
  174. int ret;
  175. if (!mips_gic_present() || !node->parent ||
  176. !of_device_is_compatible(node->parent, "mti,gic")) {
  177. pr_warn("No DT definition\n");
  178. return -ENXIO;
  179. }
  180. clk = of_clk_get(node, 0);
  181. if (!IS_ERR(clk)) {
  182. ret = clk_prepare_enable(clk);
  183. if (ret < 0) {
  184. pr_err("Failed to enable clock\n");
  185. clk_put(clk);
  186. return ret;
  187. }
  188. gic_frequency = clk_get_rate(clk);
  189. } else if (of_property_read_u32(node, "clock-frequency",
  190. &gic_frequency)) {
  191. pr_err("Frequency not specified\n");
  192. return -EINVAL;
  193. }
  194. gic_timer_irq = irq_of_parse_and_map(node, 0);
  195. if (!gic_timer_irq) {
  196. pr_err("IRQ not specified\n");
  197. return -EINVAL;
  198. }
  199. ret = __gic_clocksource_init();
  200. if (ret)
  201. return ret;
  202. ret = gic_clockevent_init();
  203. if (!ret && !IS_ERR(clk)) {
  204. if (clk_notifier_register(clk, &gic_clk_nb) < 0)
  205. pr_warn("Unable to register clock notifier\n");
  206. }
  207. /*
  208. * It's safe to use the MIPS GIC timer as a sched clock source only if
  209. * its ticks are stable, which is true on either the platforms with
  210. * stable CPU frequency or on the platforms with CM3 and CPU frequency
  211. * change performed by the CPC core clocks divider.
  212. */
  213. if (mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ)) {
  214. sched_clock_register(mips_cm_is64 ?
  215. gic_read_count_64 : gic_read_count_2x32,
  216. gic_count_width, gic_frequency);
  217. }
  218. return 0;
  219. }
  220. TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
  221. gic_clocksource_of_init);