time.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/ia64/kernel/time.c
  4. *
  5. * Copyright (C) 1998-2003 Hewlett-Packard Co
  6. * Stephane Eranian <[email protected]>
  7. * David Mosberger <[email protected]>
  8. * Copyright (C) 1999 Don Dugger <[email protected]>
  9. * Copyright (C) 1999-2000 VA Linux Systems
  10. * Copyright (C) 1999-2000 Walt Drummond <[email protected]>
  11. */
  12. #include <linux/cpu.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/profile.h>
  17. #include <linux/sched.h>
  18. #include <linux/time.h>
  19. #include <linux/nmi.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/efi.h>
  22. #include <linux/timex.h>
  23. #include <linux/timekeeper_internal.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/sched/cputime.h>
  26. #include <asm/delay.h>
  27. #include <asm/efi.h>
  28. #include <asm/hw_irq.h>
  29. #include <asm/ptrace.h>
  30. #include <asm/sal.h>
  31. #include <asm/sections.h>
  32. #include "fsyscall_gtod_data.h"
  33. #include "irq.h"
  34. static u64 itc_get_cycles(struct clocksource *cs);
  35. struct fsyscall_gtod_data_t fsyscall_gtod_data;
  36. struct itc_jitter_data_t itc_jitter_data;
  37. volatile int time_keeper_id = 0; /* smp_processor_id() of time-keeper */
  38. #ifdef CONFIG_IA64_DEBUG_IRQ
  39. unsigned long last_cli_ip;
  40. EXPORT_SYMBOL(last_cli_ip);
  41. #endif
  42. static struct clocksource clocksource_itc = {
  43. .name = "itc",
  44. .rating = 350,
  45. .read = itc_get_cycles,
  46. .mask = CLOCKSOURCE_MASK(64),
  47. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  48. };
  49. static struct clocksource *itc_clocksource;
  50. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  51. #include <linux/kernel_stat.h>
  52. extern u64 cycle_to_nsec(u64 cyc);
  53. void vtime_flush(struct task_struct *tsk)
  54. {
  55. struct thread_info *ti = task_thread_info(tsk);
  56. u64 delta;
  57. if (ti->utime)
  58. account_user_time(tsk, cycle_to_nsec(ti->utime));
  59. if (ti->gtime)
  60. account_guest_time(tsk, cycle_to_nsec(ti->gtime));
  61. if (ti->idle_time)
  62. account_idle_time(cycle_to_nsec(ti->idle_time));
  63. if (ti->stime) {
  64. delta = cycle_to_nsec(ti->stime);
  65. account_system_index_time(tsk, delta, CPUTIME_SYSTEM);
  66. }
  67. if (ti->hardirq_time) {
  68. delta = cycle_to_nsec(ti->hardirq_time);
  69. account_system_index_time(tsk, delta, CPUTIME_IRQ);
  70. }
  71. if (ti->softirq_time) {
  72. delta = cycle_to_nsec(ti->softirq_time);
  73. account_system_index_time(tsk, delta, CPUTIME_SOFTIRQ);
  74. }
  75. ti->utime = 0;
  76. ti->gtime = 0;
  77. ti->idle_time = 0;
  78. ti->stime = 0;
  79. ti->hardirq_time = 0;
  80. ti->softirq_time = 0;
  81. }
  82. /*
  83. * Called from the context switch with interrupts disabled, to charge all
  84. * accumulated times to the current process, and to prepare accounting on
  85. * the next process.
  86. */
  87. void arch_vtime_task_switch(struct task_struct *prev)
  88. {
  89. struct thread_info *pi = task_thread_info(prev);
  90. struct thread_info *ni = task_thread_info(current);
  91. ni->ac_stamp = pi->ac_stamp;
  92. ni->ac_stime = ni->ac_utime = 0;
  93. }
  94. /*
  95. * Account time for a transition between system, hard irq or soft irq state.
  96. * Note that this function is called with interrupts enabled.
  97. */
  98. static __u64 vtime_delta(struct task_struct *tsk)
  99. {
  100. struct thread_info *ti = task_thread_info(tsk);
  101. __u64 now, delta_stime;
  102. WARN_ON_ONCE(!irqs_disabled());
  103. now = ia64_get_itc();
  104. delta_stime = now - ti->ac_stamp;
  105. ti->ac_stamp = now;
  106. return delta_stime;
  107. }
  108. void vtime_account_kernel(struct task_struct *tsk)
  109. {
  110. struct thread_info *ti = task_thread_info(tsk);
  111. __u64 stime = vtime_delta(tsk);
  112. if (tsk->flags & PF_VCPU)
  113. ti->gtime += stime;
  114. else
  115. ti->stime += stime;
  116. }
  117. EXPORT_SYMBOL_GPL(vtime_account_kernel);
  118. void vtime_account_idle(struct task_struct *tsk)
  119. {
  120. struct thread_info *ti = task_thread_info(tsk);
  121. ti->idle_time += vtime_delta(tsk);
  122. }
  123. void vtime_account_softirq(struct task_struct *tsk)
  124. {
  125. struct thread_info *ti = task_thread_info(tsk);
  126. ti->softirq_time += vtime_delta(tsk);
  127. }
  128. void vtime_account_hardirq(struct task_struct *tsk)
  129. {
  130. struct thread_info *ti = task_thread_info(tsk);
  131. ti->hardirq_time += vtime_delta(tsk);
  132. }
  133. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  134. static irqreturn_t
  135. timer_interrupt (int irq, void *dev_id)
  136. {
  137. unsigned long new_itm;
  138. if (cpu_is_offline(smp_processor_id())) {
  139. return IRQ_HANDLED;
  140. }
  141. new_itm = local_cpu_data->itm_next;
  142. if (!time_after(ia64_get_itc(), new_itm))
  143. printk(KERN_ERR "Oops: timer tick before it's due (itc=%lx,itm=%lx)\n",
  144. ia64_get_itc(), new_itm);
  145. while (1) {
  146. new_itm += local_cpu_data->itm_delta;
  147. legacy_timer_tick(smp_processor_id() == time_keeper_id);
  148. local_cpu_data->itm_next = new_itm;
  149. if (time_after(new_itm, ia64_get_itc()))
  150. break;
  151. /*
  152. * Allow IPIs to interrupt the timer loop.
  153. */
  154. local_irq_enable();
  155. local_irq_disable();
  156. }
  157. do {
  158. /*
  159. * If we're too close to the next clock tick for
  160. * comfort, we increase the safety margin by
  161. * intentionally dropping the next tick(s). We do NOT
  162. * update itm.next because that would force us to call
  163. * xtime_update() which in turn would let our clock run
  164. * too fast (with the potentially devastating effect
  165. * of losing monotony of time).
  166. */
  167. while (!time_after(new_itm, ia64_get_itc() + local_cpu_data->itm_delta/2))
  168. new_itm += local_cpu_data->itm_delta;
  169. ia64_set_itm(new_itm);
  170. /* double check, in case we got hit by a (slow) PMI: */
  171. } while (time_after_eq(ia64_get_itc(), new_itm));
  172. return IRQ_HANDLED;
  173. }
  174. /*
  175. * Encapsulate access to the itm structure for SMP.
  176. */
  177. void
  178. ia64_cpu_local_tick (void)
  179. {
  180. int cpu = smp_processor_id();
  181. unsigned long shift = 0, delta;
  182. /* arrange for the cycle counter to generate a timer interrupt: */
  183. ia64_set_itv(IA64_TIMER_VECTOR);
  184. delta = local_cpu_data->itm_delta;
  185. /*
  186. * Stagger the timer tick for each CPU so they don't occur all at (almost) the
  187. * same time:
  188. */
  189. if (cpu) {
  190. unsigned long hi = 1UL << ia64_fls(cpu);
  191. shift = (2*(cpu - hi) + 1) * delta/hi/2;
  192. }
  193. local_cpu_data->itm_next = ia64_get_itc() + delta + shift;
  194. ia64_set_itm(local_cpu_data->itm_next);
  195. }
  196. static int nojitter;
  197. static int __init nojitter_setup(char *str)
  198. {
  199. nojitter = 1;
  200. printk("Jitter checking for ITC timers disabled\n");
  201. return 1;
  202. }
  203. __setup("nojitter", nojitter_setup);
  204. void ia64_init_itm(void)
  205. {
  206. unsigned long platform_base_freq, itc_freq;
  207. struct pal_freq_ratio itc_ratio, proc_ratio;
  208. long status, platform_base_drift, itc_drift;
  209. /*
  210. * According to SAL v2.6, we need to use a SAL call to determine the platform base
  211. * frequency and then a PAL call to determine the frequency ratio between the ITC
  212. * and the base frequency.
  213. */
  214. status = ia64_sal_freq_base(SAL_FREQ_BASE_PLATFORM,
  215. &platform_base_freq, &platform_base_drift);
  216. if (status != 0) {
  217. printk(KERN_ERR "SAL_FREQ_BASE_PLATFORM failed: %s\n", ia64_sal_strerror(status));
  218. } else {
  219. status = ia64_pal_freq_ratios(&proc_ratio, NULL, &itc_ratio);
  220. if (status != 0)
  221. printk(KERN_ERR "PAL_FREQ_RATIOS failed with status=%ld\n", status);
  222. }
  223. if (status != 0) {
  224. /* invent "random" values */
  225. printk(KERN_ERR
  226. "SAL/PAL failed to obtain frequency info---inventing reasonable values\n");
  227. platform_base_freq = 100000000;
  228. platform_base_drift = -1; /* no drift info */
  229. itc_ratio.num = 3;
  230. itc_ratio.den = 1;
  231. }
  232. if (platform_base_freq < 40000000) {
  233. printk(KERN_ERR "Platform base frequency %lu bogus---resetting to 75MHz!\n",
  234. platform_base_freq);
  235. platform_base_freq = 75000000;
  236. platform_base_drift = -1;
  237. }
  238. if (!proc_ratio.den)
  239. proc_ratio.den = 1; /* avoid division by zero */
  240. if (!itc_ratio.den)
  241. itc_ratio.den = 1; /* avoid division by zero */
  242. itc_freq = (platform_base_freq*itc_ratio.num)/itc_ratio.den;
  243. local_cpu_data->itm_delta = (itc_freq + HZ/2) / HZ;
  244. printk(KERN_DEBUG "CPU %d: base freq=%lu.%03luMHz, ITC ratio=%u/%u, "
  245. "ITC freq=%lu.%03luMHz", smp_processor_id(),
  246. platform_base_freq / 1000000, (platform_base_freq / 1000) % 1000,
  247. itc_ratio.num, itc_ratio.den, itc_freq / 1000000, (itc_freq / 1000) % 1000);
  248. if (platform_base_drift != -1) {
  249. itc_drift = platform_base_drift*itc_ratio.num/itc_ratio.den;
  250. printk("+/-%ldppm\n", itc_drift);
  251. } else {
  252. itc_drift = -1;
  253. printk("\n");
  254. }
  255. local_cpu_data->proc_freq = (platform_base_freq*proc_ratio.num)/proc_ratio.den;
  256. local_cpu_data->itc_freq = itc_freq;
  257. local_cpu_data->cyc_per_usec = (itc_freq + USEC_PER_SEC/2) / USEC_PER_SEC;
  258. local_cpu_data->nsec_per_cyc = ((NSEC_PER_SEC<<IA64_NSEC_PER_CYC_SHIFT)
  259. + itc_freq/2)/itc_freq;
  260. if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
  261. #ifdef CONFIG_SMP
  262. /* On IA64 in an SMP configuration ITCs are never accurately synchronized.
  263. * Jitter compensation requires a cmpxchg which may limit
  264. * the scalability of the syscalls for retrieving time.
  265. * The ITC synchronization is usually successful to within a few
  266. * ITC ticks but this is not a sure thing. If you need to improve
  267. * timer performance in SMP situations then boot the kernel with the
  268. * "nojitter" option. However, doing so may result in time fluctuating (maybe
  269. * even going backward) if the ITC offsets between the individual CPUs
  270. * are too large.
  271. */
  272. if (!nojitter)
  273. itc_jitter_data.itc_jitter = 1;
  274. #endif
  275. } else
  276. /*
  277. * ITC is drifty and we have not synchronized the ITCs in smpboot.c.
  278. * ITC values may fluctuate significantly between processors.
  279. * Clock should not be used for hrtimers. Mark itc as only
  280. * useful for boot and testing.
  281. *
  282. * Note that jitter compensation is off! There is no point of
  283. * synchronizing ITCs since they may be large differentials
  284. * that change over time.
  285. *
  286. * The only way to fix this would be to repeatedly sync the
  287. * ITCs. Until that time we have to avoid ITC.
  288. */
  289. clocksource_itc.rating = 50;
  290. /* avoid softlock up message when cpu is unplug and plugged again. */
  291. touch_softlockup_watchdog();
  292. /* Setup the CPU local timer tick */
  293. ia64_cpu_local_tick();
  294. if (!itc_clocksource) {
  295. clocksource_register_hz(&clocksource_itc,
  296. local_cpu_data->itc_freq);
  297. itc_clocksource = &clocksource_itc;
  298. }
  299. }
  300. static u64 itc_get_cycles(struct clocksource *cs)
  301. {
  302. unsigned long lcycle, now, ret;
  303. if (!itc_jitter_data.itc_jitter)
  304. return get_cycles();
  305. lcycle = itc_jitter_data.itc_lastcycle;
  306. now = get_cycles();
  307. if (lcycle && time_after(lcycle, now))
  308. return lcycle;
  309. /*
  310. * Keep track of the last timer value returned.
  311. * In an SMP environment, you could lose out in contention of
  312. * cmpxchg. If so, your cmpxchg returns new value which the
  313. * winner of contention updated to. Use the new value instead.
  314. */
  315. ret = cmpxchg(&itc_jitter_data.itc_lastcycle, lcycle, now);
  316. if (unlikely(ret != lcycle))
  317. return ret;
  318. return now;
  319. }
  320. void read_persistent_clock64(struct timespec64 *ts)
  321. {
  322. efi_gettimeofday(ts);
  323. }
  324. void __init
  325. time_init (void)
  326. {
  327. register_percpu_irq(IA64_TIMER_VECTOR, timer_interrupt, IRQF_IRQPOLL,
  328. "timer");
  329. ia64_init_itm();
  330. }
  331. /*
  332. * Generic udelay assumes that if preemption is allowed and the thread
  333. * migrates to another CPU, that the ITC values are synchronized across
  334. * all CPUs.
  335. */
  336. static void
  337. ia64_itc_udelay (unsigned long usecs)
  338. {
  339. unsigned long start = ia64_get_itc();
  340. unsigned long end = start + usecs*local_cpu_data->cyc_per_usec;
  341. while (time_before(ia64_get_itc(), end))
  342. cpu_relax();
  343. }
  344. void (*ia64_udelay)(unsigned long usecs) = &ia64_itc_udelay;
  345. void
  346. udelay (unsigned long usecs)
  347. {
  348. (*ia64_udelay)(usecs);
  349. }
  350. EXPORT_SYMBOL(udelay);
  351. /* IA64 doesn't cache the timezone */
  352. void update_vsyscall_tz(void)
  353. {
  354. }
  355. void update_vsyscall(struct timekeeper *tk)
  356. {
  357. write_seqcount_begin(&fsyscall_gtod_data.seq);
  358. /* copy vsyscall data */
  359. fsyscall_gtod_data.clk_mask = tk->tkr_mono.mask;
  360. fsyscall_gtod_data.clk_mult = tk->tkr_mono.mult;
  361. fsyscall_gtod_data.clk_shift = tk->tkr_mono.shift;
  362. fsyscall_gtod_data.clk_fsys_mmio = tk->tkr_mono.clock->archdata.fsys_mmio;
  363. fsyscall_gtod_data.clk_cycle_last = tk->tkr_mono.cycle_last;
  364. fsyscall_gtod_data.wall_time.sec = tk->xtime_sec;
  365. fsyscall_gtod_data.wall_time.snsec = tk->tkr_mono.xtime_nsec;
  366. fsyscall_gtod_data.monotonic_time.sec = tk->xtime_sec
  367. + tk->wall_to_monotonic.tv_sec;
  368. fsyscall_gtod_data.monotonic_time.snsec = tk->tkr_mono.xtime_nsec
  369. + ((u64)tk->wall_to_monotonic.tv_nsec
  370. << tk->tkr_mono.shift);
  371. /* normalize */
  372. while (fsyscall_gtod_data.monotonic_time.snsec >=
  373. (((u64)NSEC_PER_SEC) << tk->tkr_mono.shift)) {
  374. fsyscall_gtod_data.monotonic_time.snsec -=
  375. ((u64)NSEC_PER_SEC) << tk->tkr_mono.shift;
  376. fsyscall_gtod_data.monotonic_time.sec++;
  377. }
  378. write_seqcount_end(&fsyscall_gtod_data.seq);
  379. }