time.c 26 KB

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