mips-gic-timer.c 6.2 KB

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