PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/ia64/kernel/time.c

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