mips-gic-timer.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 void gic_clockevent_cpu_init(unsigned int cpu,
  66. struct clock_event_device *cd)
  67. {
  68. cd->name = "MIPS GIC";
  69. cd->features = CLOCK_EVT_FEAT_ONESHOT |
  70. CLOCK_EVT_FEAT_C3STOP;
  71. cd->rating = 350;
  72. cd->irq = gic_timer_irq;
  73. cd->cpumask = cpumask_of(cpu);
  74. cd->set_next_event = gic_next_event;
  75. clockevents_config_and_register(cd, gic_frequency, 0x300, 0x7fffffff);
  76. enable_percpu_irq(gic_timer_irq, IRQ_TYPE_NONE);
  77. }
  78. static void gic_clockevent_cpu_exit(struct clock_event_device *cd)
  79. {
  80. disable_percpu_irq(gic_timer_irq);
  81. }
  82. static void gic_update_frequency(void *data)
  83. {
  84. unsigned long rate = (unsigned long)data;
  85. clockevents_update_freq(this_cpu_ptr(&gic_clockevent_device), rate);
  86. }
  87. static int gic_starting_cpu(unsigned int cpu)
  88. {
  89. /* Ensure the GIC counter is running */
  90. clear_gic_config(GIC_CONFIG_COUNTSTOP);
  91. gic_clockevent_cpu_init(cpu, this_cpu_ptr(&gic_clockevent_device));
  92. return 0;
  93. }
  94. static int gic_clk_notifier(struct notifier_block *nb, unsigned long action,
  95. void *data)
  96. {
  97. struct clk_notifier_data *cnd = data;
  98. if (action == POST_RATE_CHANGE) {
  99. gic_clocksource_unstable("ref clock rate change");
  100. on_each_cpu(gic_update_frequency, (void *)cnd->new_rate, 1);
  101. }
  102. return NOTIFY_OK;
  103. }
  104. static int gic_dying_cpu(unsigned int cpu)
  105. {
  106. gic_clockevent_cpu_exit(this_cpu_ptr(&gic_clockevent_device));
  107. return 0;
  108. }
  109. static struct notifier_block gic_clk_nb = {
  110. .notifier_call = gic_clk_notifier,
  111. };
  112. static int gic_clockevent_init(void)
  113. {
  114. int ret;
  115. if (!gic_frequency)
  116. return -ENXIO;
  117. ret = request_percpu_irq(gic_timer_irq, gic_compare_interrupt,
  118. "timer", &gic_clockevent_device);
  119. if (ret < 0) {
  120. pr_err("IRQ %d setup failed (%d)\n", gic_timer_irq, ret);
  121. return ret;
  122. }
  123. cpuhp_setup_state(CPUHP_AP_MIPS_GIC_TIMER_STARTING,
  124. "clockevents/mips/gic/timer:starting",
  125. gic_starting_cpu, gic_dying_cpu);
  126. return 0;
  127. }
  128. static u64 gic_hpt_read(struct clocksource *cs)
  129. {
  130. return gic_read_count();
  131. }
  132. static u64 gic_hpt_read_multicluster(struct clocksource *cs)
  133. {
  134. unsigned int hi, hi2, lo;
  135. u64 count;
  136. mips_cm_lock_other(0, 0, 0, CM_GCR_Cx_OTHER_BLOCK_GLOBAL);
  137. if (mips_cm_is64) {
  138. count = read_gic_redir_counter();
  139. goto out;
  140. }
  141. hi = read_gic_redir_counter_32h();
  142. while (true) {
  143. lo = read_gic_redir_counter_32l();
  144. /* If hi didn't change then lo didn't wrap & we're done */
  145. hi2 = read_gic_redir_counter_32h();
  146. if (hi2 == hi)
  147. break;
  148. /* Otherwise, repeat with the latest hi value */
  149. hi = hi2;
  150. }
  151. count = (((u64)hi) << 32) + lo;
  152. out:
  153. mips_cm_unlock_other();
  154. return count;
  155. }
  156. static struct clocksource gic_clocksource = {
  157. .name = "GIC",
  158. .read = gic_hpt_read,
  159. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  160. .vdso_clock_mode = VDSO_CLOCKMODE_GIC,
  161. };
  162. static void gic_clocksource_unstable(char *reason)
  163. {
  164. if (gic_clock_unstable)
  165. return;
  166. gic_clock_unstable = true;
  167. pr_info("GIC timer is unstable due to %s\n", reason);
  168. clocksource_mark_unstable(&gic_clocksource);
  169. }
  170. static int __init __gic_clocksource_init(void)
  171. {
  172. int ret;
  173. /* Set clocksource mask. */
  174. gic_count_width = read_gic_config() & GIC_CONFIG_COUNTBITS;
  175. gic_count_width >>= __ffs(GIC_CONFIG_COUNTBITS);
  176. gic_count_width *= 4;
  177. gic_count_width += 32;
  178. gic_clocksource.mask = CLOCKSOURCE_MASK(gic_count_width);
  179. /* Calculate a somewhat reasonable rating value. */
  180. if (mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ))
  181. gic_clocksource.rating = 300; /* Good when frequecy is stable */
  182. else
  183. gic_clocksource.rating = 200;
  184. gic_clocksource.rating += clamp(gic_frequency / 10000000, 0, 99);
  185. if (mips_cps_multicluster_cpus()) {
  186. gic_clocksource.read = &gic_hpt_read_multicluster;
  187. gic_clocksource.vdso_clock_mode = VDSO_CLOCKMODE_NONE;
  188. }
  189. ret = clocksource_register_hz(&gic_clocksource, gic_frequency);
  190. if (ret < 0)
  191. pr_warn("Unable to register clocksource\n");
  192. return ret;
  193. }
  194. static int __init gic_clocksource_of_init(struct device_node *node)
  195. {
  196. struct clk *clk;
  197. int ret;
  198. if (!mips_gic_present() || !node->parent ||
  199. !of_device_is_compatible(node->parent, "mti,gic")) {
  200. pr_warn("No DT definition\n");
  201. return -ENXIO;
  202. }
  203. clk = of_clk_get(node, 0);
  204. if (!IS_ERR(clk)) {
  205. ret = clk_prepare_enable(clk);
  206. if (ret < 0) {
  207. pr_err("Failed to enable clock\n");
  208. clk_put(clk);
  209. return ret;
  210. }
  211. gic_frequency = clk_get_rate(clk);
  212. } else if (of_property_read_u32(node, "clock-frequency",
  213. &gic_frequency)) {
  214. pr_err("Frequency not specified\n");
  215. return -EINVAL;
  216. }
  217. gic_timer_irq = irq_of_parse_and_map(node, 0);
  218. if (!gic_timer_irq) {
  219. pr_err("IRQ not specified\n");
  220. return -EINVAL;
  221. }
  222. ret = __gic_clocksource_init();
  223. if (ret)
  224. return ret;
  225. ret = gic_clockevent_init();
  226. if (!ret && !IS_ERR(clk)) {
  227. if (clk_notifier_register(clk, &gic_clk_nb) < 0)
  228. pr_warn("Unable to register clock notifier\n");
  229. }
  230. /*
  231. * It's safe to use the MIPS GIC timer as a sched clock source only if
  232. * its ticks are stable, which is true on either the platforms with
  233. * stable CPU frequency or on the platforms with CM3 and CPU frequency
  234. * change performed by the CPC core clocks divider.
  235. */
  236. if ((mips_cm_revision() >= CM_REV_CM3 || !IS_ENABLED(CONFIG_CPU_FREQ)) &&
  237. !mips_cps_multicluster_cpus()) {
  238. sched_clock_register(mips_cm_is64 ?
  239. gic_read_count_64 : gic_read_count_2x32,
  240. gic_count_width, gic_frequency);
  241. }
  242. return 0;
  243. }
  244. TIMER_OF_DECLARE(mips_gic_timer, "mti,gic-timer",
  245. gic_clocksource_of_init);