time.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Common time routines among all ppc machines.
  4. *
  5. * Written by Cort Dougan (cort@cs.nmt.edu) to merge
  6. * Paul Mackerras' version and mine for PReP and Pmac.
  7. * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
  8. * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
  9. *
  10. * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
  11. * to make clock more stable (2.4.0-test5). The only thing
  12. * that this code assumes is that the timebases have been synchronized
  13. * by firmware on SMP and are never stopped (never do sleep
  14. * on SMP then, nap and doze are OK).
  15. *
  16. * Speeded up do_gettimeofday by getting rid of references to
  17. * xtime (which required locks for consistency). (mikejc@us.ibm.com)
  18. *
  19. * TODO (not necessarily in this file):
  20. * - improve precision and reproducibility of timebase frequency
  21. * measurement at boot time.
  22. * - for astronomical applications: add a new function to get
  23. * non ambiguous timestamps even around leap seconds. This needs
  24. * a new timestamp format and a good name.
  25. *
  26. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  27. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  28. */
  29. #include <linux/errno.h>
  30. #include <linux/export.h>
  31. #include <linux/sched.h>
  32. #include <linux/sched/clock.h>
  33. #include <linux/sched/cputime.h>
  34. #include <linux/kernel.h>
  35. #include <linux/param.h>
  36. #include <linux/string.h>
  37. #include <linux/mm.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/timex.h>
  40. #include <linux/kernel_stat.h>
  41. #include <linux/time.h>
  42. #include <linux/init.h>
  43. #include <linux/profile.h>
  44. #include <linux/cpu.h>
  45. #include <linux/security.h>
  46. #include <linux/percpu.h>
  47. #include <linux/rtc.h>
  48. #include <linux/jiffies.h>
  49. #include <linux/posix-timers.h>
  50. #include <linux/irq.h>
  51. #include <linux/delay.h>
  52. #include <linux/irq_work.h>
  53. #include <linux/of_clk.h>
  54. #include <linux/suspend.h>
  55. #include <linux/processor.h>
  56. #include <linux/mc146818rtc.h>
  57. #include <linux/platform_device.h>
  58. #include <asm/trace.h>
  59. #include <asm/interrupt.h>
  60. #include <asm/io.h>
  61. #include <asm/nvram.h>
  62. #include <asm/cache.h>
  63. #include <asm/machdep.h>
  64. #include <linux/uaccess.h>
  65. #include <asm/time.h>
  66. #include <asm/irq.h>
  67. #include <asm/div64.h>
  68. #include <asm/smp.h>
  69. #include <asm/vdso_datapage.h>
  70. #include <asm/firmware.h>
  71. #include <asm/mce.h>
  72. /* powerpc clocksource/clockevent code */
  73. #include <linux/clockchips.h>
  74. #include <linux/timekeeper_internal.h>
  75. static u64 timebase_read(struct clocksource *);
  76. static struct clocksource clocksource_timebase = {
  77. .name = "timebase",
  78. .rating = 400,
  79. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  80. .mask = CLOCKSOURCE_MASK(64),
  81. .read = timebase_read,
  82. .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER,
  83. };
  84. #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
  85. u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
  86. EXPORT_SYMBOL_GPL(decrementer_max); /* for KVM HDEC */
  87. static int decrementer_set_next_event(unsigned long evt,
  88. struct clock_event_device *dev);
  89. static int decrementer_shutdown(struct clock_event_device *evt);
  90. struct clock_event_device decrementer_clockevent = {
  91. .name = "decrementer",
  92. .rating = 200,
  93. .irq = 0,
  94. .set_next_event = decrementer_set_next_event,
  95. .set_state_oneshot_stopped = decrementer_shutdown,
  96. .set_state_shutdown = decrementer_shutdown,
  97. .tick_resume = decrementer_shutdown,
  98. .features = CLOCK_EVT_FEAT_ONESHOT |
  99. CLOCK_EVT_FEAT_C3STOP,
  100. };
  101. EXPORT_SYMBOL(decrementer_clockevent);
  102. /*
  103. * This always puts next_tb beyond now, so the clock event will never fire
  104. * with the usual comparison, no need for a separate test for stopped.
  105. */
  106. #define DEC_CLOCKEVENT_STOPPED ~0ULL
  107. DEFINE_PER_CPU(u64, decrementers_next_tb) = DEC_CLOCKEVENT_STOPPED;
  108. EXPORT_SYMBOL_GPL(decrementers_next_tb);
  109. static DEFINE_PER_CPU(struct clock_event_device, decrementers);
  110. #define XSEC_PER_SEC (1024*1024)
  111. #ifdef CONFIG_PPC64
  112. #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
  113. #else
  114. /* compute ((xsec << 12) * max) >> 32 */
  115. #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
  116. #endif
  117. unsigned long tb_ticks_per_jiffy;
  118. unsigned long tb_ticks_per_usec = 100; /* sane default */
  119. EXPORT_SYMBOL(tb_ticks_per_usec);
  120. unsigned long tb_ticks_per_sec;
  121. EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime conversions */
  122. DEFINE_SPINLOCK(rtc_lock);
  123. EXPORT_SYMBOL_GPL(rtc_lock);
  124. static u64 tb_to_ns_scale __read_mostly;
  125. static unsigned tb_to_ns_shift __read_mostly;
  126. static u64 boot_tb __read_mostly;
  127. extern struct timezone sys_tz;
  128. static long timezone_offset;
  129. unsigned long ppc_proc_freq;
  130. EXPORT_SYMBOL_GPL(ppc_proc_freq);
  131. unsigned long ppc_tb_freq;
  132. EXPORT_SYMBOL_GPL(ppc_tb_freq);
  133. bool tb_invalid;
  134. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  135. /*
  136. * Read the SPURR on systems that have it, otherwise the PURR,
  137. * or if that doesn't exist return the timebase value passed in.
  138. */
  139. static inline unsigned long read_spurr(unsigned long tb)
  140. {
  141. if (cpu_has_feature(CPU_FTR_SPURR))
  142. return mfspr(SPRN_SPURR);
  143. if (cpu_has_feature(CPU_FTR_PURR))
  144. return mfspr(SPRN_PURR);
  145. return tb;
  146. }
  147. /*
  148. * Account time for a transition between system, hard irq
  149. * or soft irq state.
  150. */
  151. static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
  152. unsigned long now, unsigned long stime)
  153. {
  154. unsigned long stime_scaled = 0;
  155. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  156. unsigned long nowscaled, deltascaled;
  157. unsigned long utime, utime_scaled;
  158. nowscaled = read_spurr(now);
  159. deltascaled = nowscaled - acct->startspurr;
  160. acct->startspurr = nowscaled;
  161. utime = acct->utime - acct->utime_sspurr;
  162. acct->utime_sspurr = acct->utime;
  163. /*
  164. * Because we don't read the SPURR on every kernel entry/exit,
  165. * deltascaled includes both user and system SPURR ticks.
  166. * Apportion these ticks to system SPURR ticks and user
  167. * SPURR ticks in the same ratio as the system time (delta)
  168. * and user time (udelta) values obtained from the timebase
  169. * over the same interval. The system ticks get accounted here;
  170. * the user ticks get saved up in paca->user_time_scaled to be
  171. * used by account_process_tick.
  172. */
  173. stime_scaled = stime;
  174. utime_scaled = utime;
  175. if (deltascaled != stime + utime) {
  176. if (utime) {
  177. stime_scaled = deltascaled * stime / (stime + utime);
  178. utime_scaled = deltascaled - stime_scaled;
  179. } else {
  180. stime_scaled = deltascaled;
  181. }
  182. }
  183. acct->utime_scaled += utime_scaled;
  184. #endif
  185. return stime_scaled;
  186. }
  187. static unsigned long vtime_delta(struct cpu_accounting_data *acct,
  188. unsigned long *stime_scaled,
  189. unsigned long *steal_time)
  190. {
  191. unsigned long now, stime;
  192. WARN_ON_ONCE(!irqs_disabled());
  193. now = mftb();
  194. stime = now - acct->starttime;
  195. acct->starttime = now;
  196. *stime_scaled = vtime_delta_scaled(acct, now, stime);
  197. if (IS_ENABLED(CONFIG_PPC_SPLPAR) &&
  198. firmware_has_feature(FW_FEATURE_SPLPAR))
  199. *steal_time = pseries_calculate_stolen_time(now);
  200. else
  201. *steal_time = 0;
  202. return stime;
  203. }
  204. static void vtime_delta_kernel(struct cpu_accounting_data *acct,
  205. unsigned long *stime, unsigned long *stime_scaled)
  206. {
  207. unsigned long steal_time;
  208. *stime = vtime_delta(acct, stime_scaled, &steal_time);
  209. *stime -= min(*stime, steal_time);
  210. acct->steal_time += steal_time;
  211. }
  212. void vtime_account_kernel(struct task_struct *tsk)
  213. {
  214. struct cpu_accounting_data *acct = get_accounting(tsk);
  215. unsigned long stime, stime_scaled;
  216. vtime_delta_kernel(acct, &stime, &stime_scaled);
  217. if (tsk->flags & PF_VCPU) {
  218. acct->gtime += stime;
  219. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  220. acct->utime_scaled += stime_scaled;
  221. #endif
  222. } else {
  223. acct->stime += stime;
  224. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  225. acct->stime_scaled += stime_scaled;
  226. #endif
  227. }
  228. }
  229. EXPORT_SYMBOL_GPL(vtime_account_kernel);
  230. void vtime_account_idle(struct task_struct *tsk)
  231. {
  232. unsigned long stime, stime_scaled, steal_time;
  233. struct cpu_accounting_data *acct = get_accounting(tsk);
  234. stime = vtime_delta(acct, &stime_scaled, &steal_time);
  235. acct->idle_time += stime + steal_time;
  236. }
  237. static void vtime_account_irq_field(struct cpu_accounting_data *acct,
  238. unsigned long *field)
  239. {
  240. unsigned long stime, stime_scaled;
  241. vtime_delta_kernel(acct, &stime, &stime_scaled);
  242. *field += stime;
  243. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  244. acct->stime_scaled += stime_scaled;
  245. #endif
  246. }
  247. void vtime_account_softirq(struct task_struct *tsk)
  248. {
  249. struct cpu_accounting_data *acct = get_accounting(tsk);
  250. vtime_account_irq_field(acct, &acct->softirq_time);
  251. }
  252. void vtime_account_hardirq(struct task_struct *tsk)
  253. {
  254. struct cpu_accounting_data *acct = get_accounting(tsk);
  255. vtime_account_irq_field(acct, &acct->hardirq_time);
  256. }
  257. static void vtime_flush_scaled(struct task_struct *tsk,
  258. struct cpu_accounting_data *acct)
  259. {
  260. #ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
  261. if (acct->utime_scaled)
  262. tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
  263. if (acct->stime_scaled)
  264. tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
  265. acct->utime_scaled = 0;
  266. acct->utime_sspurr = 0;
  267. acct->stime_scaled = 0;
  268. #endif
  269. }
  270. /*
  271. * Account the whole cputime accumulated in the paca
  272. * Must be called with interrupts disabled.
  273. * Assumes that vtime_account_kernel/idle() has been called
  274. * recently (i.e. since the last entry from usermode) so that
  275. * get_paca()->user_time_scaled is up to date.
  276. */
  277. void vtime_flush(struct task_struct *tsk)
  278. {
  279. struct cpu_accounting_data *acct = get_accounting(tsk);
  280. if (acct->utime)
  281. account_user_time(tsk, cputime_to_nsecs(acct->utime));
  282. if (acct->gtime)
  283. account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
  284. if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
  285. account_steal_time(cputime_to_nsecs(acct->steal_time));
  286. acct->steal_time = 0;
  287. }
  288. if (acct->idle_time)
  289. account_idle_time(cputime_to_nsecs(acct->idle_time));
  290. if (acct->stime)
  291. account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
  292. CPUTIME_SYSTEM);
  293. if (acct->hardirq_time)
  294. account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
  295. CPUTIME_IRQ);
  296. if (acct->softirq_time)
  297. account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
  298. CPUTIME_SOFTIRQ);
  299. vtime_flush_scaled(tsk, acct);
  300. acct->utime = 0;
  301. acct->gtime = 0;
  302. acct->idle_time = 0;
  303. acct->stime = 0;
  304. acct->hardirq_time = 0;
  305. acct->softirq_time = 0;
  306. }
  307. /*
  308. * Called from the context switch with interrupts disabled, to charge all
  309. * accumulated times to the current process, and to prepare accounting on
  310. * the next process.
  311. */
  312. void vtime_task_switch(struct task_struct *prev)
  313. {
  314. if (is_idle_task(prev))
  315. vtime_account_idle(prev);
  316. else
  317. vtime_account_kernel(prev);
  318. vtime_flush(prev);
  319. if (!IS_ENABLED(CONFIG_PPC64)) {
  320. struct cpu_accounting_data *acct = get_accounting(current);
  321. struct cpu_accounting_data *acct0 = get_accounting(prev);
  322. acct->starttime = acct0->starttime;
  323. }
  324. }
  325. #endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  326. void __no_kcsan __delay(unsigned long loops)
  327. {
  328. unsigned long start;
  329. spin_begin();
  330. if (tb_invalid) {
  331. /*
  332. * TB is in error state and isn't ticking anymore.
  333. * HMI handler was unable to recover from TB error.
  334. * Return immediately, so that kernel won't get stuck here.
  335. */
  336. spin_cpu_relax();
  337. } else {
  338. start = mftb();
  339. while (mftb() - start < loops)
  340. spin_cpu_relax();
  341. }
  342. spin_end();
  343. }
  344. EXPORT_SYMBOL(__delay);
  345. void __no_kcsan udelay(unsigned long usecs)
  346. {
  347. __delay(tb_ticks_per_usec * usecs);
  348. }
  349. EXPORT_SYMBOL(udelay);
  350. #ifdef CONFIG_SMP
  351. unsigned long profile_pc(struct pt_regs *regs)
  352. {
  353. unsigned long pc = instruction_pointer(regs);
  354. if (in_lock_functions(pc))
  355. return regs->link;
  356. return pc;
  357. }
  358. EXPORT_SYMBOL(profile_pc);
  359. #endif
  360. #ifdef CONFIG_IRQ_WORK
  361. /*
  362. * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
  363. */
  364. #ifdef CONFIG_PPC64
  365. static inline unsigned long test_irq_work_pending(void)
  366. {
  367. unsigned long x;
  368. asm volatile("lbz %0,%1(13)"
  369. : "=r" (x)
  370. : "i" (offsetof(struct paca_struct, irq_work_pending)));
  371. return x;
  372. }
  373. static inline void set_irq_work_pending_flag(void)
  374. {
  375. asm volatile("stb %0,%1(13)" : :
  376. "r" (1),
  377. "i" (offsetof(struct paca_struct, irq_work_pending)));
  378. }
  379. static inline void clear_irq_work_pending(void)
  380. {
  381. asm volatile("stb %0,%1(13)" : :
  382. "r" (0),
  383. "i" (offsetof(struct paca_struct, irq_work_pending)));
  384. }
  385. #else /* 32-bit */
  386. DEFINE_PER_CPU(u8, irq_work_pending);
  387. #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
  388. #define test_irq_work_pending() __this_cpu_read(irq_work_pending)
  389. #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
  390. #endif /* 32 vs 64 bit */
  391. void arch_irq_work_raise(void)
  392. {
  393. /*
  394. * 64-bit code that uses irq soft-mask can just cause an immediate
  395. * interrupt here that gets soft masked, if this is called under
  396. * local_irq_disable(). It might be possible to prevent that happening
  397. * by noticing interrupts are disabled and setting decrementer pending
  398. * to be replayed when irqs are enabled. The problem there is that
  399. * tracing can call irq_work_raise, including in code that does low
  400. * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on)
  401. * which could get tangled up if we're messing with the same state
  402. * here.
  403. */
  404. preempt_disable();
  405. set_irq_work_pending_flag();
  406. set_dec(1);
  407. preempt_enable();
  408. }
  409. static void set_dec_or_work(u64 val)
  410. {
  411. set_dec(val);
  412. /* We may have raced with new irq work */
  413. if (unlikely(test_irq_work_pending()))
  414. set_dec(1);
  415. }
  416. #else /* CONFIG_IRQ_WORK */
  417. #define test_irq_work_pending() 0
  418. #define clear_irq_work_pending()
  419. static void set_dec_or_work(u64 val)
  420. {
  421. set_dec(val);
  422. }
  423. #endif /* CONFIG_IRQ_WORK */
  424. #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
  425. void timer_rearm_host_dec(u64 now)
  426. {
  427. u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
  428. WARN_ON_ONCE(!arch_irqs_disabled());
  429. WARN_ON_ONCE(mfmsr() & MSR_EE);
  430. if (now >= *next_tb) {
  431. local_paca->irq_happened |= PACA_IRQ_DEC;
  432. } else {
  433. now = *next_tb - now;
  434. if (now > decrementer_max)
  435. now = decrementer_max;
  436. set_dec_or_work(now);
  437. }
  438. }
  439. EXPORT_SYMBOL_GPL(timer_rearm_host_dec);
  440. #endif
  441. /*
  442. * timer_interrupt - gets called when the decrementer overflows,
  443. * with interrupts disabled.
  444. */
  445. DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)
  446. {
  447. struct clock_event_device *evt = this_cpu_ptr(&decrementers);
  448. u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
  449. struct pt_regs *old_regs;
  450. u64 now;
  451. /*
  452. * Some implementations of hotplug will get timer interrupts while
  453. * offline, just ignore these.
  454. */
  455. if (unlikely(!cpu_online(smp_processor_id()))) {
  456. set_dec(decrementer_max);
  457. return;
  458. }
  459. /* Conditionally hard-enable interrupts. */
  460. if (should_hard_irq_enable(regs)) {
  461. /*
  462. * Ensure a positive value is written to the decrementer, or
  463. * else some CPUs will continue to take decrementer exceptions.
  464. * When the PPC_WATCHDOG (decrementer based) is configured,
  465. * keep this at most 31 bits, which is about 4 seconds on most
  466. * systems, which gives the watchdog a chance of catching timer
  467. * interrupt hard lockups.
  468. */
  469. if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
  470. set_dec(0x7fffffff);
  471. else
  472. set_dec(decrementer_max);
  473. do_hard_irq_enable();
  474. }
  475. #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
  476. if (atomic_read(&ppc_n_lost_interrupts) != 0)
  477. __do_IRQ(regs);
  478. #endif
  479. old_regs = set_irq_regs(regs);
  480. trace_timer_interrupt_entry(regs);
  481. if (test_irq_work_pending()) {
  482. clear_irq_work_pending();
  483. mce_run_irq_context_handlers();
  484. irq_work_run();
  485. }
  486. now = get_tb();
  487. if (now >= *next_tb) {
  488. evt->event_handler(evt);
  489. __this_cpu_inc(irq_stat.timer_irqs_event);
  490. } else {
  491. now = *next_tb - now;
  492. if (now > decrementer_max)
  493. now = decrementer_max;
  494. set_dec_or_work(now);
  495. __this_cpu_inc(irq_stat.timer_irqs_others);
  496. }
  497. trace_timer_interrupt_exit(regs);
  498. set_irq_regs(old_regs);
  499. }
  500. EXPORT_SYMBOL(timer_interrupt);
  501. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  502. void timer_broadcast_interrupt(void)
  503. {
  504. tick_receive_broadcast();
  505. __this_cpu_inc(irq_stat.broadcast_irqs_event);
  506. }
  507. #endif
  508. #ifdef CONFIG_SUSPEND
  509. /* Overrides the weak version in kernel/power/main.c */
  510. void arch_suspend_disable_irqs(void)
  511. {
  512. if (ppc_md.suspend_disable_irqs)
  513. ppc_md.suspend_disable_irqs();
  514. /* Disable the decrementer, so that it doesn't interfere
  515. * with suspending.
  516. */
  517. set_dec(decrementer_max);
  518. local_irq_disable();
  519. set_dec(decrementer_max);
  520. }
  521. /* Overrides the weak version in kernel/power/main.c */
  522. void arch_suspend_enable_irqs(void)
  523. {
  524. local_irq_enable();
  525. if (ppc_md.suspend_enable_irqs)
  526. ppc_md.suspend_enable_irqs();
  527. }
  528. #endif
  529. unsigned long long tb_to_ns(unsigned long long ticks)
  530. {
  531. return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
  532. }
  533. EXPORT_SYMBOL_GPL(tb_to_ns);
  534. /*
  535. * Scheduler clock - returns current time in nanosec units.
  536. *
  537. * Note: mulhdu(a, b) (multiply high double unsigned) returns
  538. * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
  539. * are 64-bit unsigned numbers.
  540. */
  541. notrace unsigned long long sched_clock(void)
  542. {
  543. return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
  544. }
  545. #ifdef CONFIG_PPC_PSERIES
  546. /*
  547. * Running clock - attempts to give a view of time passing for a virtualised
  548. * kernels.
  549. * Uses the VTB register if available otherwise a next best guess.
  550. */
  551. unsigned long long running_clock(void)
  552. {
  553. /*
  554. * Don't read the VTB as a host since KVM does not switch in host
  555. * timebase into the VTB when it takes a guest off the CPU, reading the
  556. * VTB would result in reading 'last switched out' guest VTB.
  557. *
  558. * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
  559. * would be unsafe to rely only on the #ifdef above.
  560. */
  561. if (firmware_has_feature(FW_FEATURE_LPAR) &&
  562. cpu_has_feature(CPU_FTR_ARCH_207S))
  563. return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
  564. /*
  565. * This is a next best approximation without a VTB.
  566. * On a host which is running bare metal there should never be any stolen
  567. * time and on a host which doesn't do any virtualisation TB *should* equal
  568. * VTB so it makes no difference anyway.
  569. */
  570. return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
  571. }
  572. #endif
  573. static int __init get_freq(char *name, int cells, unsigned long *val)
  574. {
  575. struct device_node *cpu;
  576. const __be32 *fp;
  577. int found = 0;
  578. /* The cpu node should have timebase and clock frequency properties */
  579. cpu = of_find_node_by_type(NULL, "cpu");
  580. if (cpu) {
  581. fp = of_get_property(cpu, name, NULL);
  582. if (fp) {
  583. found = 1;
  584. *val = of_read_ulong(fp, cells);
  585. }
  586. of_node_put(cpu);
  587. }
  588. return found;
  589. }
  590. static void start_cpu_decrementer(void)
  591. {
  592. #ifdef CONFIG_BOOKE
  593. unsigned int tcr;
  594. /* Clear any pending timer interrupts */
  595. mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
  596. tcr = mfspr(SPRN_TCR);
  597. /*
  598. * The watchdog may have already been enabled by u-boot. So leave
  599. * TRC[WP] (Watchdog Period) alone.
  600. */
  601. tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */
  602. tcr |= TCR_DIE; /* Enable decrementer */
  603. mtspr(SPRN_TCR, tcr);
  604. #endif
  605. }
  606. void __init generic_calibrate_decr(void)
  607. {
  608. ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
  609. if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
  610. !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
  611. printk(KERN_ERR "WARNING: Estimating decrementer frequency "
  612. "(not found)\n");
  613. }
  614. ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
  615. if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
  616. !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
  617. printk(KERN_ERR "WARNING: Estimating processor frequency "
  618. "(not found)\n");
  619. }
  620. }
  621. int update_persistent_clock64(struct timespec64 now)
  622. {
  623. struct rtc_time tm;
  624. if (!ppc_md.set_rtc_time)
  625. return -ENODEV;
  626. rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
  627. return ppc_md.set_rtc_time(&tm);
  628. }
  629. static void __read_persistent_clock(struct timespec64 *ts)
  630. {
  631. struct rtc_time tm;
  632. static int first = 1;
  633. ts->tv_nsec = 0;
  634. /* XXX this is a little fragile but will work okay in the short term */
  635. if (first) {
  636. first = 0;
  637. if (ppc_md.time_init)
  638. timezone_offset = ppc_md.time_init();
  639. /* get_boot_time() isn't guaranteed to be safe to call late */
  640. if (ppc_md.get_boot_time) {
  641. ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
  642. return;
  643. }
  644. }
  645. if (!ppc_md.get_rtc_time) {
  646. ts->tv_sec = 0;
  647. return;
  648. }
  649. ppc_md.get_rtc_time(&tm);
  650. ts->tv_sec = rtc_tm_to_time64(&tm);
  651. }
  652. void read_persistent_clock64(struct timespec64 *ts)
  653. {
  654. __read_persistent_clock(ts);
  655. /* Sanitize it in case real time clock is set below EPOCH */
  656. if (ts->tv_sec < 0) {
  657. ts->tv_sec = 0;
  658. ts->tv_nsec = 0;
  659. }
  660. }
  661. /* clocksource code */
  662. static notrace u64 timebase_read(struct clocksource *cs)
  663. {
  664. return (u64)get_tb();
  665. }
  666. static void __init clocksource_init(void)
  667. {
  668. struct clocksource *clock = &clocksource_timebase;
  669. if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
  670. printk(KERN_ERR "clocksource: %s is already registered\n",
  671. clock->name);
  672. return;
  673. }
  674. printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
  675. clock->name, clock->mult, clock->shift);
  676. }
  677. static int decrementer_set_next_event(unsigned long evt,
  678. struct clock_event_device *dev)
  679. {
  680. __this_cpu_write(decrementers_next_tb, get_tb() + evt);
  681. set_dec_or_work(evt);
  682. return 0;
  683. }
  684. static int decrementer_shutdown(struct clock_event_device *dev)
  685. {
  686. __this_cpu_write(decrementers_next_tb, DEC_CLOCKEVENT_STOPPED);
  687. set_dec_or_work(decrementer_max);
  688. return 0;
  689. }
  690. static void register_decrementer_clockevent(int cpu)
  691. {
  692. struct clock_event_device *dec = &per_cpu(decrementers, cpu);
  693. *dec = decrementer_clockevent;
  694. dec->cpumask = cpumask_of(cpu);
  695. clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
  696. printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
  697. dec->name, dec->mult, dec->shift, cpu);
  698. /* Set values for KVM, see kvm_emulate_dec() */
  699. decrementer_clockevent.mult = dec->mult;
  700. decrementer_clockevent.shift = dec->shift;
  701. }
  702. static void enable_large_decrementer(void)
  703. {
  704. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  705. return;
  706. if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
  707. return;
  708. /*
  709. * If we're running as the hypervisor we need to enable the LD manually
  710. * otherwise firmware should have done it for us.
  711. */
  712. if (cpu_has_feature(CPU_FTR_HVMODE))
  713. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
  714. }
  715. static void __init set_decrementer_max(void)
  716. {
  717. struct device_node *cpu;
  718. u32 bits = 32;
  719. /* Prior to ISAv3 the decrementer is always 32 bit */
  720. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  721. return;
  722. cpu = of_find_node_by_type(NULL, "cpu");
  723. if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
  724. if (bits > 64 || bits < 32) {
  725. pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
  726. bits = 32;
  727. }
  728. /* calculate the signed maximum given this many bits */
  729. decrementer_max = (1ul << (bits - 1)) - 1;
  730. }
  731. of_node_put(cpu);
  732. pr_info("time_init: %u bit decrementer (max: %llx)\n",
  733. bits, decrementer_max);
  734. }
  735. static void __init init_decrementer_clockevent(void)
  736. {
  737. register_decrementer_clockevent(smp_processor_id());
  738. }
  739. void secondary_cpu_time_init(void)
  740. {
  741. /* Enable and test the large decrementer for this cpu */
  742. enable_large_decrementer();
  743. /* Start the decrementer on CPUs that have manual control
  744. * such as BookE
  745. */
  746. start_cpu_decrementer();
  747. /* FIME: Should make unrelated change to move snapshot_timebase
  748. * call here ! */
  749. register_decrementer_clockevent(smp_processor_id());
  750. }
  751. /* This function is only called on the boot processor */
  752. void __init time_init(void)
  753. {
  754. struct div_result res;
  755. u64 scale;
  756. unsigned shift;
  757. /* Normal PowerPC with timebase register */
  758. if (ppc_md.calibrate_decr)
  759. ppc_md.calibrate_decr();
  760. else
  761. generic_calibrate_decr();
  762. printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
  763. ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
  764. printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
  765. ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
  766. tb_ticks_per_jiffy = ppc_tb_freq / HZ;
  767. tb_ticks_per_sec = ppc_tb_freq;
  768. tb_ticks_per_usec = ppc_tb_freq / 1000000;
  769. /*
  770. * Compute scale factor for sched_clock.
  771. * The calibrate_decr() function has set tb_ticks_per_sec,
  772. * which is the timebase frequency.
  773. * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
  774. * the 128-bit result as a 64.64 fixed-point number.
  775. * We then shift that number right until it is less than 1.0,
  776. * giving us the scale factor and shift count to use in
  777. * sched_clock().
  778. */
  779. div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
  780. scale = res.result_low;
  781. for (shift = 0; res.result_high != 0; ++shift) {
  782. scale = (scale >> 1) | (res.result_high << 63);
  783. res.result_high >>= 1;
  784. }
  785. tb_to_ns_scale = scale;
  786. tb_to_ns_shift = shift;
  787. /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
  788. boot_tb = get_tb();
  789. /* If platform provided a timezone (pmac), we correct the time */
  790. if (timezone_offset) {
  791. sys_tz.tz_minuteswest = -timezone_offset / 60;
  792. sys_tz.tz_dsttime = 0;
  793. }
  794. vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
  795. /* initialise and enable the large decrementer (if we have one) */
  796. set_decrementer_max();
  797. enable_large_decrementer();
  798. /* Start the decrementer on CPUs that have manual control
  799. * such as BookE
  800. */
  801. start_cpu_decrementer();
  802. /* Register the clocksource */
  803. clocksource_init();
  804. init_decrementer_clockevent();
  805. tick_setup_hrtimer_broadcast();
  806. of_clk_init(NULL);
  807. enable_sched_clock_irqtime();
  808. }
  809. /*
  810. * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
  811. * result.
  812. */
  813. void div128_by_32(u64 dividend_high, u64 dividend_low,
  814. unsigned divisor, struct div_result *dr)
  815. {
  816. unsigned long a, b, c, d;
  817. unsigned long w, x, y, z;
  818. u64 ra, rb, rc;
  819. a = dividend_high >> 32;
  820. b = dividend_high & 0xffffffff;
  821. c = dividend_low >> 32;
  822. d = dividend_low & 0xffffffff;
  823. w = a / divisor;
  824. ra = ((u64)(a - (w * divisor)) << 32) + b;
  825. rb = ((u64) do_div(ra, divisor) << 32) + c;
  826. x = ra;
  827. rc = ((u64) do_div(rb, divisor) << 32) + d;
  828. y = rb;
  829. do_div(rc, divisor);
  830. z = rc;
  831. dr->result_high = ((u64)w << 32) + x;
  832. dr->result_low = ((u64)y << 32) + z;
  833. }
  834. /* We don't need to calibrate delay, we use the CPU timebase for that */
  835. void calibrate_delay(void)
  836. {
  837. /* Some generic code (such as spinlock debug) use loops_per_jiffy
  838. * as the number of __delay(1) in a jiffy, so make it so
  839. */
  840. loops_per_jiffy = tb_ticks_per_jiffy;
  841. }
  842. #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
  843. static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
  844. {
  845. ppc_md.get_rtc_time(tm);
  846. return 0;
  847. }
  848. static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
  849. {
  850. if (!ppc_md.set_rtc_time)
  851. return -EOPNOTSUPP;
  852. if (ppc_md.set_rtc_time(tm) < 0)
  853. return -EOPNOTSUPP;
  854. return 0;
  855. }
  856. static const struct rtc_class_ops rtc_generic_ops = {
  857. .read_time = rtc_generic_get_time,
  858. .set_time = rtc_generic_set_time,
  859. };
  860. static int __init rtc_init(void)
  861. {
  862. struct platform_device *pdev;
  863. if (!ppc_md.get_rtc_time)
  864. return -ENODEV;
  865. pdev = platform_device_register_data(NULL, "rtc-generic", -1,
  866. &rtc_generic_ops,
  867. sizeof(rtc_generic_ops));
  868. return PTR_ERR_OR_ZERO(pdev);
  869. }
  870. device_initcall(rtc_init);
  871. #endif