sched_clock.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * sched_clock.c: Generic sched_clock() support, to extend low level
  3. * hardware time counters to full 64-bit ns values.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clocksource.h>
  10. #include <linux/init.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/ktime.h>
  13. #include <linux/kernel.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/clock.h>
  17. #include <linux/syscore_ops.h>
  18. #include <linux/hrtimer.h>
  19. #include <linux/sched_clock.h>
  20. #include <linux/seqlock.h>
  21. #include <linux/bitops.h>
  22. #include <mt-plat/mtk_sys_timer.h>
  23. /**
  24. * struct clock_read_data - data required to read from sched_clock()
  25. *
  26. * @epoch_ns: sched_clock() value at last update
  27. * @epoch_cyc: Clock cycle value at last update.
  28. * @sched_clock_mask: Bitmask for two's complement subtraction of non 64bit
  29. * clocks.
  30. * @read_sched_clock: Current clock source (or dummy source when suspended).
  31. * @mult: Multipler for scaled math conversion.
  32. * @shift: Shift value for scaled math conversion.
  33. *
  34. * Care must be taken when updating this structure; it is read by
  35. * some very hot code paths. It occupies <=40 bytes and, when combined
  36. * with the seqcount used to synchronize access, comfortably fits into
  37. * a 64 byte cache line.
  38. */
  39. struct clock_read_data {
  40. u64 epoch_ns;
  41. u64 epoch_cyc;
  42. u64 sched_clock_mask;
  43. u64 (*read_sched_clock)(void);
  44. u32 mult;
  45. u32 shift;
  46. };
  47. /**
  48. * struct clock_data - all data needed for sched_clock() (including
  49. * registration of a new clock source)
  50. *
  51. * @seq: Sequence counter for protecting updates. The lowest
  52. * bit is the index for @read_data.
  53. * @read_data: Data required to read from sched_clock.
  54. * @wrap_kt: Duration for which clock can run before wrapping.
  55. * @rate: Tick rate of the registered clock.
  56. * @actual_read_sched_clock: Registered hardware level clock read function.
  57. *
  58. * The ordering of this structure has been chosen to optimize cache
  59. * performance. In particular 'seq' and 'read_data[0]' (combined) should fit
  60. * into a single 64-byte cache line.
  61. */
  62. struct clock_data {
  63. seqcount_t seq;
  64. struct clock_read_data read_data[2];
  65. ktime_t wrap_kt;
  66. unsigned long rate;
  67. u64 (*actual_read_sched_clock)(void);
  68. };
  69. static struct hrtimer sched_clock_timer;
  70. static int irqtime = -1;
  71. core_param(irqtime, irqtime, int, 0400);
  72. static u64 notrace jiffy_sched_clock_read(void)
  73. {
  74. /*
  75. * We don't need to use get_jiffies_64 on 32-bit arches here
  76. * because we register with BITS_PER_LONG
  77. */
  78. return (u64)(jiffies - INITIAL_JIFFIES);
  79. }
  80. static struct clock_data cd ____cacheline_aligned = {
  81. .read_data[0] = { .mult = NSEC_PER_SEC / HZ,
  82. .read_sched_clock = jiffy_sched_clock_read, },
  83. .actual_read_sched_clock = jiffy_sched_clock_read,
  84. };
  85. static inline u64 notrace cyc_to_ns(u64 cyc, u32 mult, u32 shift)
  86. {
  87. return (cyc * mult) >> shift;
  88. }
  89. unsigned long long notrace sched_clock(void)
  90. {
  91. u64 cyc, res;
  92. unsigned long seq;
  93. struct clock_read_data *rd;
  94. do {
  95. seq = raw_read_seqcount(&cd.seq);
  96. rd = cd.read_data + (seq & 1);
  97. cyc = (rd->read_sched_clock() - rd->epoch_cyc) &
  98. rd->sched_clock_mask;
  99. res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
  100. } while (read_seqcount_retry(&cd.seq, seq));
  101. return res;
  102. }
  103. /*
  104. * alternative sched_clock to get arch_timer cycle as well
  105. */
  106. unsigned long long notrace sched_clock_get_cyc(unsigned long long *cyc_ret)
  107. {
  108. u64 cyc, cyc_cur, res;
  109. unsigned long seq;
  110. struct clock_read_data *rd;
  111. do {
  112. seq = raw_read_seqcount(&cd.seq);
  113. rd = cd.read_data + (seq & 1);
  114. cyc_cur = rd->read_sched_clock();
  115. cyc = (cyc_cur - rd->epoch_cyc) &
  116. rd->sched_clock_mask;
  117. res = rd->epoch_ns + cyc_to_ns(cyc, rd->mult, rd->shift);
  118. } while (read_seqcount_retry(&cd.seq, seq));
  119. if (cyc_ret)
  120. *cyc_ret = cyc_cur;
  121. return res;
  122. }
  123. /*
  124. * Updating the data required to read the clock.
  125. *
  126. * sched_clock() will never observe mis-matched data even if called from
  127. * an NMI. We do this by maintaining an odd/even copy of the data and
  128. * steering sched_clock() to one or the other using a sequence counter.
  129. * In order to preserve the data cache profile of sched_clock() as much
  130. * as possible the system reverts back to the even copy when the update
  131. * completes; the odd copy is used *only* during an update.
  132. */
  133. static void update_clock_read_data(struct clock_read_data *rd)
  134. {
  135. /* update the backup (odd) copy with the new data */
  136. cd.read_data[1] = *rd;
  137. /* steer readers towards the odd copy */
  138. raw_write_seqcount_latch(&cd.seq);
  139. /* now its safe for us to update the normal (even) copy */
  140. cd.read_data[0] = *rd;
  141. /* switch readers back to the even copy */
  142. raw_write_seqcount_latch(&cd.seq);
  143. }
  144. /*
  145. * Atomically update the sched_clock() epoch.
  146. */
  147. static void update_sched_clock(void)
  148. {
  149. u64 cyc;
  150. u64 ns;
  151. struct clock_read_data rd;
  152. rd = cd.read_data[0];
  153. cyc = cd.actual_read_sched_clock();
  154. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  155. rd.epoch_ns = ns;
  156. rd.epoch_cyc = cyc;
  157. update_clock_read_data(&rd);
  158. }
  159. static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
  160. {
  161. update_sched_clock();
  162. hrtimer_forward_now(hrt, cd.wrap_kt);
  163. /* snchronize new sched_clock base to co-processors */
  164. #ifndef CONFIG_FPGA_EARLY_PORTING
  165. sys_timer_timesync_sync_base(SYS_TIMER_TIMESYNC_FLAG_ASYNC);
  166. #endif
  167. return HRTIMER_RESTART;
  168. }
  169. void __init
  170. sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
  171. {
  172. u64 res, wrap, new_mask, new_epoch, cyc, ns;
  173. u32 new_mult, new_shift;
  174. unsigned long r;
  175. char r_unit;
  176. struct clock_read_data rd;
  177. if (cd.rate > rate)
  178. return;
  179. WARN_ON(!irqs_disabled());
  180. /* Calculate the mult/shift to convert counter ticks to ns. */
  181. clocks_calc_mult_shift(&new_mult, &new_shift, rate, NSEC_PER_SEC, 3600);
  182. new_mask = CLOCKSOURCE_MASK(bits);
  183. cd.rate = rate;
  184. /* Calculate how many nanosecs until we risk wrapping */
  185. wrap = clocks_calc_max_nsecs(new_mult, new_shift, 0, new_mask, NULL);
  186. cd.wrap_kt = ns_to_ktime(wrap);
  187. rd = cd.read_data[0];
  188. /* Update epoch for new counter and update 'epoch_ns' from old counter*/
  189. new_epoch = read();
  190. cyc = cd.actual_read_sched_clock();
  191. ns = rd.epoch_ns + cyc_to_ns((cyc - rd.epoch_cyc) & rd.sched_clock_mask, rd.mult, rd.shift);
  192. cd.actual_read_sched_clock = read;
  193. rd.read_sched_clock = read;
  194. rd.sched_clock_mask = new_mask;
  195. rd.mult = new_mult;
  196. rd.shift = new_shift;
  197. rd.epoch_cyc = new_epoch;
  198. rd.epoch_ns = ns;
  199. update_clock_read_data(&rd);
  200. if (sched_clock_timer.function != NULL) {
  201. /* update timeout for clock wrap */
  202. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
  203. }
  204. r = rate;
  205. if (r >= 4000000) {
  206. r /= 1000000;
  207. r_unit = 'M';
  208. } else {
  209. if (r >= 1000) {
  210. r /= 1000;
  211. r_unit = 'k';
  212. } else {
  213. r_unit = ' ';
  214. }
  215. }
  216. /* Calculate the ns resolution of this counter */
  217. res = cyc_to_ns(1ULL, new_mult, new_shift);
  218. pr_info("sched_clock: %u bits at %lu%cHz, resolution %lluns, wraps every %lluns\n",
  219. bits, r, r_unit, res, wrap);
  220. /* Enable IRQ time accounting if we have a fast enough sched_clock() */
  221. if (irqtime > 0 || (irqtime == -1 && rate >= 1000000))
  222. enable_sched_clock_irqtime();
  223. pr_debug("Registered %pF as sched_clock source\n", read);
  224. }
  225. void __init sched_clock_postinit(void)
  226. {
  227. /*
  228. * If no sched_clock() function has been provided at that point,
  229. * make it the final one one.
  230. */
  231. if (cd.actual_read_sched_clock == jiffy_sched_clock_read)
  232. sched_clock_register(jiffy_sched_clock_read, BITS_PER_LONG, HZ);
  233. update_sched_clock();
  234. /*
  235. * Start the timer to keep sched_clock() properly updated and
  236. * sets the initial epoch.
  237. */
  238. hrtimer_init(&sched_clock_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  239. sched_clock_timer.function = sched_clock_poll;
  240. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
  241. }
  242. /*
  243. * Clock read function for use when the clock is suspended.
  244. *
  245. * This function makes it appear to sched_clock() as if the clock
  246. * stopped counting at its last update.
  247. *
  248. * This function must only be called from the critical
  249. * section in sched_clock(). It relies on the read_seqcount_retry()
  250. * at the end of the critical section to be sure we observe the
  251. * correct copy of 'epoch_cyc'.
  252. */
  253. static u64 notrace suspended_sched_clock_read(void)
  254. {
  255. unsigned long seq = raw_read_seqcount(&cd.seq);
  256. return cd.read_data[seq & 1].epoch_cyc;
  257. }
  258. int sched_clock_suspend(void)
  259. {
  260. struct clock_read_data *rd = &cd.read_data[0];
  261. update_sched_clock();
  262. hrtimer_cancel(&sched_clock_timer);
  263. rd->read_sched_clock = suspended_sched_clock_read;
  264. /* snchronize new sched_clock base to co-processors */
  265. #ifndef CONFIG_FPGA_EARLY_PORTING
  266. sys_timer_timesync_sync_base(SYS_TIMER_TIMESYNC_FLAG_SYNC |
  267. SYS_TIMER_TIMESYNC_FLAG_FREEZE);
  268. #endif
  269. return 0;
  270. }
  271. void sched_clock_resume(void)
  272. {
  273. struct clock_read_data *rd = &cd.read_data[0];
  274. rd->epoch_cyc = cd.actual_read_sched_clock();
  275. hrtimer_start(&sched_clock_timer, cd.wrap_kt, HRTIMER_MODE_REL);
  276. rd->read_sched_clock = cd.actual_read_sched_clock;
  277. /* snchronize new sched_clock base to co-processors */
  278. #ifndef CONFIG_FPGA_EARLY_PORTING
  279. sys_timer_timesync_sync_base(SYS_TIMER_TIMESYNC_FLAG_SYNC |
  280. SYS_TIMER_TIMESYNC_FLAG_UNFREEZE);
  281. #endif
  282. }
  283. static struct syscore_ops sched_clock_ops = {
  284. .suspend = sched_clock_suspend,
  285. .resume = sched_clock_resume,
  286. };
  287. static int __init sched_clock_syscore_init(void)
  288. {
  289. register_syscore_ops(&sched_clock_ops);
  290. return 0;
  291. }
  292. device_initcall(sched_clock_syscore_init);