time.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  1. /*
  2. * Common time routines among all ppc machines.
  3. *
  4. * Written by Cort Dougan (cort@cs.nmt.edu) to merge
  5. * Paul Mackerras' version and mine for PReP and Pmac.
  6. * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
  7. * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
  8. *
  9. * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
  10. * to make clock more stable (2.4.0-test5). The only thing
  11. * that this code assumes is that the timebases have been synchronized
  12. * by firmware on SMP and are never stopped (never do sleep
  13. * on SMP then, nap and doze are OK).
  14. *
  15. * Speeded up do_gettimeofday by getting rid of references to
  16. * xtime (which required locks for consistency). (mikejc@us.ibm.com)
  17. *
  18. * TODO (not necessarily in this file):
  19. * - improve precision and reproducibility of timebase frequency
  20. * measurement at boot time.
  21. * - for astronomical applications: add a new function to get
  22. * non ambiguous timestamps even around leap seconds. This needs
  23. * a new timestamp format and a good name.
  24. *
  25. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  26. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  27. *
  28. * This program is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU General Public License
  30. * as published by the Free Software Foundation; either version
  31. * 2 of the License, or (at your option) any later version.
  32. */
  33. #include <linux/errno.h>
  34. #include <linux/export.h>
  35. #include <linux/sched.h>
  36. #include <linux/sched/clock.h>
  37. #include <linux/kernel.h>
  38. #include <linux/param.h>
  39. #include <linux/string.h>
  40. #include <linux/mm.h>
  41. #include <linux/interrupt.h>
  42. #include <linux/timex.h>
  43. #include <linux/kernel_stat.h>
  44. #include <linux/time.h>
  45. #include <linux/clockchips.h>
  46. #include <linux/init.h>
  47. #include <linux/profile.h>
  48. #include <linux/cpu.h>
  49. #include <linux/security.h>
  50. #include <linux/percpu.h>
  51. #include <linux/rtc.h>
  52. #include <linux/jiffies.h>
  53. #include <linux/posix-timers.h>
  54. #include <linux/irq.h>
  55. #include <linux/delay.h>
  56. #include <linux/irq_work.h>
  57. #include <linux/clk-provider.h>
  58. #include <linux/suspend.h>
  59. #include <linux/rtc.h>
  60. #include <linux/sched/cputime.h>
  61. #include <asm/trace.h>
  62. #include <asm/io.h>
  63. #include <asm/processor.h>
  64. #include <asm/nvram.h>
  65. #include <asm/cache.h>
  66. #include <asm/machdep.h>
  67. #include <linux/uaccess.h>
  68. #include <asm/time.h>
  69. #include <asm/prom.h>
  70. #include <asm/irq.h>
  71. #include <asm/div64.h>
  72. #include <asm/smp.h>
  73. #include <asm/vdso_datapage.h>
  74. #include <asm/firmware.h>
  75. #include <asm/asm-prototypes.h>
  76. /* powerpc clocksource/clockevent code */
  77. #include <linux/clockchips.h>
  78. #include <linux/timekeeper_internal.h>
  79. static u64 rtc_read(struct clocksource *);
  80. static struct clocksource clocksource_rtc = {
  81. .name = "rtc",
  82. .rating = 400,
  83. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  84. .mask = CLOCKSOURCE_MASK(64),
  85. .read = rtc_read,
  86. };
  87. static u64 timebase_read(struct clocksource *);
  88. static struct clocksource clocksource_timebase = {
  89. .name = "timebase",
  90. .rating = 400,
  91. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  92. .mask = CLOCKSOURCE_MASK(64),
  93. .read = timebase_read,
  94. };
  95. #define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
  96. u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
  97. static int decrementer_set_next_event(unsigned long evt,
  98. struct clock_event_device *dev);
  99. static int decrementer_shutdown(struct clock_event_device *evt);
  100. struct clock_event_device decrementer_clockevent = {
  101. .name = "decrementer",
  102. .rating = 200,
  103. .irq = 0,
  104. .set_next_event = decrementer_set_next_event,
  105. .set_state_shutdown = decrementer_shutdown,
  106. .tick_resume = decrementer_shutdown,
  107. .features = CLOCK_EVT_FEAT_ONESHOT |
  108. CLOCK_EVT_FEAT_C3STOP,
  109. };
  110. EXPORT_SYMBOL(decrementer_clockevent);
  111. DEFINE_PER_CPU(u64, decrementers_next_tb);
  112. static DEFINE_PER_CPU(struct clock_event_device, decrementers);
  113. #define XSEC_PER_SEC (1024*1024)
  114. #ifdef CONFIG_PPC64
  115. #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
  116. #else
  117. /* compute ((xsec << 12) * max) >> 32 */
  118. #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
  119. #endif
  120. unsigned long tb_ticks_per_jiffy;
  121. unsigned long tb_ticks_per_usec = 100; /* sane default */
  122. EXPORT_SYMBOL(tb_ticks_per_usec);
  123. unsigned long tb_ticks_per_sec;
  124. EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */
  125. DEFINE_SPINLOCK(rtc_lock);
  126. EXPORT_SYMBOL_GPL(rtc_lock);
  127. static u64 tb_to_ns_scale __read_mostly;
  128. static unsigned tb_to_ns_shift __read_mostly;
  129. static u64 boot_tb __read_mostly;
  130. extern struct timezone sys_tz;
  131. static long timezone_offset;
  132. unsigned long ppc_proc_freq;
  133. EXPORT_SYMBOL_GPL(ppc_proc_freq);
  134. unsigned long ppc_tb_freq;
  135. EXPORT_SYMBOL_GPL(ppc_tb_freq);
  136. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  137. /*
  138. * Factor for converting from cputime_t (timebase ticks) to
  139. * microseconds. This is stored as 0.64 fixed-point binary fraction.
  140. */
  141. u64 __cputime_usec_factor;
  142. EXPORT_SYMBOL(__cputime_usec_factor);
  143. #ifdef CONFIG_PPC_SPLPAR
  144. void (*dtl_consumer)(struct dtl_entry *, u64);
  145. #endif
  146. #ifdef CONFIG_PPC64
  147. #define get_accounting(tsk) (&get_paca()->accounting)
  148. #else
  149. #define get_accounting(tsk) (&task_thread_info(tsk)->accounting)
  150. #endif
  151. static void calc_cputime_factors(void)
  152. {
  153. struct div_result res;
  154. div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
  155. __cputime_usec_factor = res.result_low;
  156. }
  157. /*
  158. * Read the SPURR on systems that have it, otherwise the PURR,
  159. * or if that doesn't exist return the timebase value passed in.
  160. */
  161. static unsigned long read_spurr(unsigned long tb)
  162. {
  163. if (cpu_has_feature(CPU_FTR_SPURR))
  164. return mfspr(SPRN_SPURR);
  165. if (cpu_has_feature(CPU_FTR_PURR))
  166. return mfspr(SPRN_PURR);
  167. return tb;
  168. }
  169. #ifdef CONFIG_PPC_SPLPAR
  170. /*
  171. * Scan the dispatch trace log and count up the stolen time.
  172. * Should be called with interrupts disabled.
  173. */
  174. static u64 scan_dispatch_log(u64 stop_tb)
  175. {
  176. u64 i = local_paca->dtl_ridx;
  177. struct dtl_entry *dtl = local_paca->dtl_curr;
  178. struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
  179. struct lppaca *vpa = local_paca->lppaca_ptr;
  180. u64 tb_delta;
  181. u64 stolen = 0;
  182. u64 dtb;
  183. if (!dtl)
  184. return 0;
  185. if (i == be64_to_cpu(vpa->dtl_idx))
  186. return 0;
  187. while (i < be64_to_cpu(vpa->dtl_idx)) {
  188. dtb = be64_to_cpu(dtl->timebase);
  189. tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
  190. be32_to_cpu(dtl->ready_to_enqueue_time);
  191. barrier();
  192. if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
  193. /* buffer has overflowed */
  194. i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
  195. dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
  196. continue;
  197. }
  198. if (dtb > stop_tb)
  199. break;
  200. if (dtl_consumer)
  201. dtl_consumer(dtl, i);
  202. stolen += tb_delta;
  203. ++i;
  204. ++dtl;
  205. if (dtl == dtl_end)
  206. dtl = local_paca->dispatch_log;
  207. }
  208. local_paca->dtl_ridx = i;
  209. local_paca->dtl_curr = dtl;
  210. return stolen;
  211. }
  212. /*
  213. * Accumulate stolen time by scanning the dispatch trace log.
  214. * Called on entry from user mode.
  215. */
  216. void accumulate_stolen_time(void)
  217. {
  218. u64 sst, ust;
  219. u8 save_soft_enabled = local_paca->soft_enabled;
  220. struct cpu_accounting_data *acct = &local_paca->accounting;
  221. /* We are called early in the exception entry, before
  222. * soft/hard_enabled are sync'ed to the expected state
  223. * for the exception. We are hard disabled but the PACA
  224. * needs to reflect that so various debug stuff doesn't
  225. * complain
  226. */
  227. local_paca->soft_enabled = 0;
  228. sst = scan_dispatch_log(acct->starttime_user);
  229. ust = scan_dispatch_log(acct->starttime);
  230. acct->stime -= sst;
  231. acct->utime -= ust;
  232. acct->steal_time += ust + sst;
  233. local_paca->soft_enabled = save_soft_enabled;
  234. }
  235. static inline u64 calculate_stolen_time(u64 stop_tb)
  236. {
  237. if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx))
  238. return scan_dispatch_log(stop_tb);
  239. return 0;
  240. }
  241. #else /* CONFIG_PPC_SPLPAR */
  242. static inline u64 calculate_stolen_time(u64 stop_tb)
  243. {
  244. return 0;
  245. }
  246. #endif /* CONFIG_PPC_SPLPAR */
  247. /*
  248. * Account time for a transition between system, hard irq
  249. * or soft irq state.
  250. */
  251. static unsigned long vtime_delta(struct task_struct *tsk,
  252. unsigned long *stime_scaled,
  253. unsigned long *steal_time)
  254. {
  255. unsigned long now, nowscaled, deltascaled;
  256. unsigned long stime;
  257. unsigned long utime, utime_scaled;
  258. struct cpu_accounting_data *acct = get_accounting(tsk);
  259. WARN_ON_ONCE(!irqs_disabled());
  260. now = mftb();
  261. nowscaled = read_spurr(now);
  262. stime = now - acct->starttime;
  263. acct->starttime = now;
  264. deltascaled = nowscaled - acct->startspurr;
  265. acct->startspurr = nowscaled;
  266. *steal_time = calculate_stolen_time(now);
  267. utime = acct->utime - acct->utime_sspurr;
  268. acct->utime_sspurr = acct->utime;
  269. /*
  270. * Because we don't read the SPURR on every kernel entry/exit,
  271. * deltascaled includes both user and system SPURR ticks.
  272. * Apportion these ticks to system SPURR ticks and user
  273. * SPURR ticks in the same ratio as the system time (delta)
  274. * and user time (udelta) values obtained from the timebase
  275. * over the same interval. The system ticks get accounted here;
  276. * the user ticks get saved up in paca->user_time_scaled to be
  277. * used by account_process_tick.
  278. */
  279. *stime_scaled = stime;
  280. utime_scaled = utime;
  281. if (deltascaled != stime + utime) {
  282. if (utime) {
  283. *stime_scaled = deltascaled * stime / (stime + utime);
  284. utime_scaled = deltascaled - *stime_scaled;
  285. } else {
  286. *stime_scaled = deltascaled;
  287. }
  288. }
  289. acct->utime_scaled += utime_scaled;
  290. return stime;
  291. }
  292. void vtime_account_system(struct task_struct *tsk)
  293. {
  294. unsigned long stime, stime_scaled, steal_time;
  295. struct cpu_accounting_data *acct = get_accounting(tsk);
  296. stime = vtime_delta(tsk, &stime_scaled, &steal_time);
  297. stime -= min(stime, steal_time);
  298. acct->steal_time += steal_time;
  299. if ((tsk->flags & PF_VCPU) && !irq_count()) {
  300. acct->gtime += stime;
  301. acct->utime_scaled += stime_scaled;
  302. } else {
  303. if (hardirq_count())
  304. acct->hardirq_time += stime;
  305. else if (in_serving_softirq())
  306. acct->softirq_time += stime;
  307. else
  308. acct->stime += stime;
  309. acct->stime_scaled += stime_scaled;
  310. }
  311. }
  312. EXPORT_SYMBOL_GPL(vtime_account_system);
  313. void vtime_account_idle(struct task_struct *tsk)
  314. {
  315. unsigned long stime, stime_scaled, steal_time;
  316. struct cpu_accounting_data *acct = get_accounting(tsk);
  317. stime = vtime_delta(tsk, &stime_scaled, &steal_time);
  318. acct->idle_time += stime + steal_time;
  319. }
  320. /*
  321. * Account the whole cputime accumulated in the paca
  322. * Must be called with interrupts disabled.
  323. * Assumes that vtime_account_system/idle() has been called
  324. * recently (i.e. since the last entry from usermode) so that
  325. * get_paca()->user_time_scaled is up to date.
  326. */
  327. void vtime_flush(struct task_struct *tsk)
  328. {
  329. struct cpu_accounting_data *acct = get_accounting(tsk);
  330. if (acct->utime)
  331. account_user_time(tsk, cputime_to_nsecs(acct->utime));
  332. if (acct->utime_scaled)
  333. tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
  334. if (acct->gtime)
  335. account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
  336. if (acct->steal_time)
  337. account_steal_time(cputime_to_nsecs(acct->steal_time));
  338. if (acct->idle_time)
  339. account_idle_time(cputime_to_nsecs(acct->idle_time));
  340. if (acct->stime)
  341. account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
  342. CPUTIME_SYSTEM);
  343. if (acct->stime_scaled)
  344. tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
  345. if (acct->hardirq_time)
  346. account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
  347. CPUTIME_IRQ);
  348. if (acct->softirq_time)
  349. account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
  350. CPUTIME_SOFTIRQ);
  351. acct->utime = 0;
  352. acct->utime_scaled = 0;
  353. acct->utime_sspurr = 0;
  354. acct->gtime = 0;
  355. acct->steal_time = 0;
  356. acct->idle_time = 0;
  357. acct->stime = 0;
  358. acct->stime_scaled = 0;
  359. acct->hardirq_time = 0;
  360. acct->softirq_time = 0;
  361. }
  362. #ifdef CONFIG_PPC32
  363. /*
  364. * Called from the context switch with interrupts disabled, to charge all
  365. * accumulated times to the current process, and to prepare accounting on
  366. * the next process.
  367. */
  368. void arch_vtime_task_switch(struct task_struct *prev)
  369. {
  370. struct cpu_accounting_data *acct = get_accounting(current);
  371. acct->starttime = get_accounting(prev)->starttime;
  372. acct->startspurr = get_accounting(prev)->startspurr;
  373. }
  374. #endif /* CONFIG_PPC32 */
  375. #else /* ! CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
  376. #define calc_cputime_factors()
  377. #endif
  378. void __delay(unsigned long loops)
  379. {
  380. unsigned long start;
  381. int diff;
  382. if (__USE_RTC()) {
  383. start = get_rtcl();
  384. do {
  385. /* the RTCL register wraps at 1000000000 */
  386. diff = get_rtcl() - start;
  387. if (diff < 0)
  388. diff += 1000000000;
  389. } while (diff < loops);
  390. } else {
  391. start = get_tbl();
  392. while (get_tbl() - start < loops)
  393. HMT_low();
  394. HMT_medium();
  395. }
  396. }
  397. EXPORT_SYMBOL(__delay);
  398. void udelay(unsigned long usecs)
  399. {
  400. __delay(tb_ticks_per_usec * usecs);
  401. }
  402. EXPORT_SYMBOL(udelay);
  403. #ifdef CONFIG_SMP
  404. unsigned long profile_pc(struct pt_regs *regs)
  405. {
  406. unsigned long pc = instruction_pointer(regs);
  407. if (in_lock_functions(pc))
  408. return regs->link;
  409. return pc;
  410. }
  411. EXPORT_SYMBOL(profile_pc);
  412. #endif
  413. #ifdef CONFIG_IRQ_WORK
  414. /*
  415. * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
  416. */
  417. #ifdef CONFIG_PPC64
  418. static inline unsigned long test_irq_work_pending(void)
  419. {
  420. unsigned long x;
  421. asm volatile("lbz %0,%1(13)"
  422. : "=r" (x)
  423. : "i" (offsetof(struct paca_struct, irq_work_pending)));
  424. return x;
  425. }
  426. static inline void set_irq_work_pending_flag(void)
  427. {
  428. asm volatile("stb %0,%1(13)" : :
  429. "r" (1),
  430. "i" (offsetof(struct paca_struct, irq_work_pending)));
  431. }
  432. static inline void clear_irq_work_pending(void)
  433. {
  434. asm volatile("stb %0,%1(13)" : :
  435. "r" (0),
  436. "i" (offsetof(struct paca_struct, irq_work_pending)));
  437. }
  438. #else /* 32-bit */
  439. DEFINE_PER_CPU(u8, irq_work_pending);
  440. #define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
  441. #define test_irq_work_pending() __this_cpu_read(irq_work_pending)
  442. #define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
  443. #endif /* 32 vs 64 bit */
  444. void arch_irq_work_raise(void)
  445. {
  446. preempt_disable();
  447. set_irq_work_pending_flag();
  448. set_dec(1);
  449. preempt_enable();
  450. }
  451. #else /* CONFIG_IRQ_WORK */
  452. #define test_irq_work_pending() 0
  453. #define clear_irq_work_pending()
  454. #endif /* CONFIG_IRQ_WORK */
  455. static void __timer_interrupt(void)
  456. {
  457. struct pt_regs *regs = get_irq_regs();
  458. u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
  459. struct clock_event_device *evt = this_cpu_ptr(&decrementers);
  460. u64 now;
  461. trace_timer_interrupt_entry(regs);
  462. if (test_irq_work_pending()) {
  463. clear_irq_work_pending();
  464. irq_work_run();
  465. }
  466. now = get_tb_or_rtc();
  467. if (now >= *next_tb) {
  468. *next_tb = ~(u64)0;
  469. if (evt->event_handler)
  470. evt->event_handler(evt);
  471. __this_cpu_inc(irq_stat.timer_irqs_event);
  472. } else {
  473. now = *next_tb - now;
  474. if (now <= decrementer_max)
  475. set_dec(now);
  476. /* We may have raced with new irq work */
  477. if (test_irq_work_pending())
  478. set_dec(1);
  479. __this_cpu_inc(irq_stat.timer_irqs_others);
  480. }
  481. #ifdef CONFIG_PPC64
  482. /* collect purr register values often, for accurate calculations */
  483. if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
  484. struct cpu_usage *cu = this_cpu_ptr(&cpu_usage_array);
  485. cu->current_tb = mfspr(SPRN_PURR);
  486. }
  487. #endif
  488. trace_timer_interrupt_exit(regs);
  489. }
  490. /*
  491. * timer_interrupt - gets called when the decrementer overflows,
  492. * with interrupts disabled.
  493. */
  494. void timer_interrupt(struct pt_regs * regs)
  495. {
  496. struct pt_regs *old_regs;
  497. u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
  498. /* Ensure a positive value is written to the decrementer, or else
  499. * some CPUs will continue to take decrementer exceptions.
  500. */
  501. set_dec(decrementer_max);
  502. /* Some implementations of hotplug will get timer interrupts while
  503. * offline, just ignore these and we also need to set
  504. * decrementers_next_tb as MAX to make sure __check_irq_replay
  505. * don't replay timer interrupt when return, otherwise we'll trap
  506. * here infinitely :(
  507. */
  508. if (!cpu_online(smp_processor_id())) {
  509. *next_tb = ~(u64)0;
  510. return;
  511. }
  512. /* Conditionally hard-enable interrupts now that the DEC has been
  513. * bumped to its maximum value
  514. */
  515. may_hard_irq_enable();
  516. #if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
  517. if (atomic_read(&ppc_n_lost_interrupts) != 0)
  518. do_IRQ(regs);
  519. #endif
  520. old_regs = set_irq_regs(regs);
  521. irq_enter();
  522. __timer_interrupt();
  523. irq_exit();
  524. set_irq_regs(old_regs);
  525. }
  526. EXPORT_SYMBOL(timer_interrupt);
  527. /*
  528. * Hypervisor decrementer interrupts shouldn't occur but are sometimes
  529. * left pending on exit from a KVM guest. We don't need to do anything
  530. * to clear them, as they are edge-triggered.
  531. */
  532. void hdec_interrupt(struct pt_regs *regs)
  533. {
  534. }
  535. #ifdef CONFIG_SUSPEND
  536. static void generic_suspend_disable_irqs(void)
  537. {
  538. /* Disable the decrementer, so that it doesn't interfere
  539. * with suspending.
  540. */
  541. set_dec(decrementer_max);
  542. local_irq_disable();
  543. set_dec(decrementer_max);
  544. }
  545. static void generic_suspend_enable_irqs(void)
  546. {
  547. local_irq_enable();
  548. }
  549. /* Overrides the weak version in kernel/power/main.c */
  550. void arch_suspend_disable_irqs(void)
  551. {
  552. if (ppc_md.suspend_disable_irqs)
  553. ppc_md.suspend_disable_irqs();
  554. generic_suspend_disable_irqs();
  555. }
  556. /* Overrides the weak version in kernel/power/main.c */
  557. void arch_suspend_enable_irqs(void)
  558. {
  559. generic_suspend_enable_irqs();
  560. if (ppc_md.suspend_enable_irqs)
  561. ppc_md.suspend_enable_irqs();
  562. }
  563. #endif
  564. unsigned long long tb_to_ns(unsigned long long ticks)
  565. {
  566. return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
  567. }
  568. EXPORT_SYMBOL_GPL(tb_to_ns);
  569. /*
  570. * Scheduler clock - returns current time in nanosec units.
  571. *
  572. * Note: mulhdu(a, b) (multiply high double unsigned) returns
  573. * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
  574. * are 64-bit unsigned numbers.
  575. */
  576. unsigned long long sched_clock(void)
  577. {
  578. if (__USE_RTC())
  579. return get_rtc();
  580. return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
  581. }
  582. #ifdef CONFIG_PPC_PSERIES
  583. /*
  584. * Running clock - attempts to give a view of time passing for a virtualised
  585. * kernels.
  586. * Uses the VTB register if available otherwise a next best guess.
  587. */
  588. unsigned long long running_clock(void)
  589. {
  590. /*
  591. * Don't read the VTB as a host since KVM does not switch in host
  592. * timebase into the VTB when it takes a guest off the CPU, reading the
  593. * VTB would result in reading 'last switched out' guest VTB.
  594. *
  595. * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
  596. * would be unsafe to rely only on the #ifdef above.
  597. */
  598. if (firmware_has_feature(FW_FEATURE_LPAR) &&
  599. cpu_has_feature(CPU_FTR_ARCH_207S))
  600. return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
  601. /*
  602. * This is a next best approximation without a VTB.
  603. * On a host which is running bare metal there should never be any stolen
  604. * time and on a host which doesn't do any virtualisation TB *should* equal
  605. * VTB so it makes no difference anyway.
  606. */
  607. return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
  608. }
  609. #endif
  610. static int __init get_freq(char *name, int cells, unsigned long *val)
  611. {
  612. struct device_node *cpu;
  613. const __be32 *fp;
  614. int found = 0;
  615. /* The cpu node should have timebase and clock frequency properties */
  616. cpu = of_find_node_by_type(NULL, "cpu");
  617. if (cpu) {
  618. fp = of_get_property(cpu, name, NULL);
  619. if (fp) {
  620. found = 1;
  621. *val = of_read_ulong(fp, cells);
  622. }
  623. of_node_put(cpu);
  624. }
  625. return found;
  626. }
  627. static void start_cpu_decrementer(void)
  628. {
  629. #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
  630. /* Clear any pending timer interrupts */
  631. mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
  632. /* Enable decrementer interrupt */
  633. mtspr(SPRN_TCR, TCR_DIE);
  634. #endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */
  635. }
  636. void __init generic_calibrate_decr(void)
  637. {
  638. ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
  639. if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
  640. !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
  641. printk(KERN_ERR "WARNING: Estimating decrementer frequency "
  642. "(not found)\n");
  643. }
  644. ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
  645. if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
  646. !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
  647. printk(KERN_ERR "WARNING: Estimating processor frequency "
  648. "(not found)\n");
  649. }
  650. }
  651. int update_persistent_clock(struct timespec now)
  652. {
  653. struct rtc_time tm;
  654. if (!ppc_md.set_rtc_time)
  655. return -ENODEV;
  656. to_tm(now.tv_sec + 1 + timezone_offset, &tm);
  657. tm.tm_year -= 1900;
  658. tm.tm_mon -= 1;
  659. return ppc_md.set_rtc_time(&tm);
  660. }
  661. static void __read_persistent_clock(struct timespec *ts)
  662. {
  663. struct rtc_time tm;
  664. static int first = 1;
  665. ts->tv_nsec = 0;
  666. /* XXX this is a litle fragile but will work okay in the short term */
  667. if (first) {
  668. first = 0;
  669. if (ppc_md.time_init)
  670. timezone_offset = ppc_md.time_init();
  671. /* get_boot_time() isn't guaranteed to be safe to call late */
  672. if (ppc_md.get_boot_time) {
  673. ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
  674. return;
  675. }
  676. }
  677. if (!ppc_md.get_rtc_time) {
  678. ts->tv_sec = 0;
  679. return;
  680. }
  681. ppc_md.get_rtc_time(&tm);
  682. ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
  683. tm.tm_hour, tm.tm_min, tm.tm_sec);
  684. }
  685. void read_persistent_clock(struct timespec *ts)
  686. {
  687. __read_persistent_clock(ts);
  688. /* Sanitize it in case real time clock is set below EPOCH */
  689. if (ts->tv_sec < 0) {
  690. ts->tv_sec = 0;
  691. ts->tv_nsec = 0;
  692. }
  693. }
  694. /* clocksource code */
  695. static u64 rtc_read(struct clocksource *cs)
  696. {
  697. return (u64)get_rtc();
  698. }
  699. static u64 timebase_read(struct clocksource *cs)
  700. {
  701. return (u64)get_tb();
  702. }
  703. void update_vsyscall_old(struct timespec *wall_time, struct timespec *wtm,
  704. struct clocksource *clock, u32 mult, u64 cycle_last)
  705. {
  706. u64 new_tb_to_xs, new_stamp_xsec;
  707. u32 frac_sec;
  708. if (clock != &clocksource_timebase)
  709. return;
  710. /* Make userspace gettimeofday spin until we're done. */
  711. ++vdso_data->tb_update_count;
  712. smp_mb();
  713. /* 19342813113834067 ~= 2^(20+64) / 1e9 */
  714. new_tb_to_xs = (u64) mult * (19342813113834067ULL >> clock->shift);
  715. new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC;
  716. do_div(new_stamp_xsec, 1000000000);
  717. new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC;
  718. BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC);
  719. /* this is tv_nsec / 1e9 as a 0.32 fraction */
  720. frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32;
  721. /*
  722. * tb_update_count is used to allow the userspace gettimeofday code
  723. * to assure itself that it sees a consistent view of the tb_to_xs and
  724. * stamp_xsec variables. It reads the tb_update_count, then reads
  725. * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
  726. * the two values of tb_update_count match and are even then the
  727. * tb_to_xs and stamp_xsec values are consistent. If not, then it
  728. * loops back and reads them again until this criteria is met.
  729. * We expect the caller to have done the first increment of
  730. * vdso_data->tb_update_count already.
  731. */
  732. vdso_data->tb_orig_stamp = cycle_last;
  733. vdso_data->stamp_xsec = new_stamp_xsec;
  734. vdso_data->tb_to_xs = new_tb_to_xs;
  735. vdso_data->wtom_clock_sec = wtm->tv_sec;
  736. vdso_data->wtom_clock_nsec = wtm->tv_nsec;
  737. vdso_data->stamp_xtime = *wall_time;
  738. vdso_data->stamp_sec_fraction = frac_sec;
  739. smp_wmb();
  740. ++(vdso_data->tb_update_count);
  741. }
  742. void update_vsyscall_tz(void)
  743. {
  744. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  745. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  746. }
  747. static void __init clocksource_init(void)
  748. {
  749. struct clocksource *clock;
  750. if (__USE_RTC())
  751. clock = &clocksource_rtc;
  752. else
  753. clock = &clocksource_timebase;
  754. if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
  755. printk(KERN_ERR "clocksource: %s is already registered\n",
  756. clock->name);
  757. return;
  758. }
  759. printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
  760. clock->name, clock->mult, clock->shift);
  761. }
  762. static int decrementer_set_next_event(unsigned long evt,
  763. struct clock_event_device *dev)
  764. {
  765. __this_cpu_write(decrementers_next_tb, get_tb_or_rtc() + evt);
  766. set_dec(evt);
  767. /* We may have raced with new irq work */
  768. if (test_irq_work_pending())
  769. set_dec(1);
  770. return 0;
  771. }
  772. static int decrementer_shutdown(struct clock_event_device *dev)
  773. {
  774. decrementer_set_next_event(decrementer_max, dev);
  775. return 0;
  776. }
  777. /* Interrupt handler for the timer broadcast IPI */
  778. void tick_broadcast_ipi_handler(void)
  779. {
  780. u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
  781. *next_tb = get_tb_or_rtc();
  782. __timer_interrupt();
  783. }
  784. static void register_decrementer_clockevent(int cpu)
  785. {
  786. struct clock_event_device *dec = &per_cpu(decrementers, cpu);
  787. *dec = decrementer_clockevent;
  788. dec->cpumask = cpumask_of(cpu);
  789. printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
  790. dec->name, dec->mult, dec->shift, cpu);
  791. clockevents_register_device(dec);
  792. }
  793. static void enable_large_decrementer(void)
  794. {
  795. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  796. return;
  797. if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
  798. return;
  799. /*
  800. * If we're running as the hypervisor we need to enable the LD manually
  801. * otherwise firmware should have done it for us.
  802. */
  803. if (cpu_has_feature(CPU_FTR_HVMODE))
  804. mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
  805. }
  806. static void __init set_decrementer_max(void)
  807. {
  808. struct device_node *cpu;
  809. u32 bits = 32;
  810. /* Prior to ISAv3 the decrementer is always 32 bit */
  811. if (!cpu_has_feature(CPU_FTR_ARCH_300))
  812. return;
  813. cpu = of_find_node_by_type(NULL, "cpu");
  814. if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
  815. if (bits > 64 || bits < 32) {
  816. pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
  817. bits = 32;
  818. }
  819. /* calculate the signed maximum given this many bits */
  820. decrementer_max = (1ul << (bits - 1)) - 1;
  821. }
  822. of_node_put(cpu);
  823. pr_info("time_init: %u bit decrementer (max: %llx)\n",
  824. bits, decrementer_max);
  825. }
  826. static void __init init_decrementer_clockevent(void)
  827. {
  828. int cpu = smp_processor_id();
  829. clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4);
  830. decrementer_clockevent.max_delta_ns =
  831. clockevent_delta2ns(decrementer_max, &decrementer_clockevent);
  832. decrementer_clockevent.max_delta_ticks = decrementer_max;
  833. decrementer_clockevent.min_delta_ns =
  834. clockevent_delta2ns(2, &decrementer_clockevent);
  835. decrementer_clockevent.min_delta_ticks = 2;
  836. register_decrementer_clockevent(cpu);
  837. }
  838. void secondary_cpu_time_init(void)
  839. {
  840. /* Enable and test the large decrementer for this cpu */
  841. enable_large_decrementer();
  842. /* Start the decrementer on CPUs that have manual control
  843. * such as BookE
  844. */
  845. start_cpu_decrementer();
  846. /* FIME: Should make unrelatred change to move snapshot_timebase
  847. * call here ! */
  848. register_decrementer_clockevent(smp_processor_id());
  849. }
  850. /* This function is only called on the boot processor */
  851. void __init time_init(void)
  852. {
  853. struct div_result res;
  854. u64 scale;
  855. unsigned shift;
  856. if (__USE_RTC()) {
  857. /* 601 processor: dec counts down by 128 every 128ns */
  858. ppc_tb_freq = 1000000000;
  859. } else {
  860. /* Normal PowerPC with timebase register */
  861. ppc_md.calibrate_decr();
  862. printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
  863. ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
  864. printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
  865. ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
  866. }
  867. tb_ticks_per_jiffy = ppc_tb_freq / HZ;
  868. tb_ticks_per_sec = ppc_tb_freq;
  869. tb_ticks_per_usec = ppc_tb_freq / 1000000;
  870. calc_cputime_factors();
  871. /*
  872. * Compute scale factor for sched_clock.
  873. * The calibrate_decr() function has set tb_ticks_per_sec,
  874. * which is the timebase frequency.
  875. * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
  876. * the 128-bit result as a 64.64 fixed-point number.
  877. * We then shift that number right until it is less than 1.0,
  878. * giving us the scale factor and shift count to use in
  879. * sched_clock().
  880. */
  881. div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
  882. scale = res.result_low;
  883. for (shift = 0; res.result_high != 0; ++shift) {
  884. scale = (scale >> 1) | (res.result_high << 63);
  885. res.result_high >>= 1;
  886. }
  887. tb_to_ns_scale = scale;
  888. tb_to_ns_shift = shift;
  889. /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
  890. boot_tb = get_tb_or_rtc();
  891. /* If platform provided a timezone (pmac), we correct the time */
  892. if (timezone_offset) {
  893. sys_tz.tz_minuteswest = -timezone_offset / 60;
  894. sys_tz.tz_dsttime = 0;
  895. }
  896. vdso_data->tb_update_count = 0;
  897. vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
  898. /* initialise and enable the large decrementer (if we have one) */
  899. set_decrementer_max();
  900. enable_large_decrementer();
  901. /* Start the decrementer on CPUs that have manual control
  902. * such as BookE
  903. */
  904. start_cpu_decrementer();
  905. /* Register the clocksource */
  906. clocksource_init();
  907. init_decrementer_clockevent();
  908. tick_setup_hrtimer_broadcast();
  909. #ifdef CONFIG_COMMON_CLK
  910. of_clk_init(NULL);
  911. #endif
  912. }
  913. #define FEBRUARY 2
  914. #define STARTOFTIME 1970
  915. #define SECDAY 86400L
  916. #define SECYR (SECDAY * 365)
  917. #define leapyear(year) ((year) % 4 == 0 && \
  918. ((year) % 100 != 0 || (year) % 400 == 0))
  919. #define days_in_year(a) (leapyear(a) ? 366 : 365)
  920. #define days_in_month(a) (month_days[(a) - 1])
  921. static int month_days[12] = {
  922. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  923. };
  924. void to_tm(int tim, struct rtc_time * tm)
  925. {
  926. register int i;
  927. register long hms, day;
  928. day = tim / SECDAY;
  929. hms = tim % SECDAY;
  930. /* Hours, minutes, seconds are easy */
  931. tm->tm_hour = hms / 3600;
  932. tm->tm_min = (hms % 3600) / 60;
  933. tm->tm_sec = (hms % 3600) % 60;
  934. /* Number of years in days */
  935. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  936. day -= days_in_year(i);
  937. tm->tm_year = i;
  938. /* Number of months in days left */
  939. if (leapyear(tm->tm_year))
  940. days_in_month(FEBRUARY) = 29;
  941. for (i = 1; day >= days_in_month(i); i++)
  942. day -= days_in_month(i);
  943. days_in_month(FEBRUARY) = 28;
  944. tm->tm_mon = i;
  945. /* Days are what is left over (+1) from all that. */
  946. tm->tm_mday = day + 1;
  947. /*
  948. * No-one uses the day of the week.
  949. */
  950. tm->tm_wday = -1;
  951. }
  952. EXPORT_SYMBOL(to_tm);
  953. /*
  954. * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
  955. * result.
  956. */
  957. void div128_by_32(u64 dividend_high, u64 dividend_low,
  958. unsigned divisor, struct div_result *dr)
  959. {
  960. unsigned long a, b, c, d;
  961. unsigned long w, x, y, z;
  962. u64 ra, rb, rc;
  963. a = dividend_high >> 32;
  964. b = dividend_high & 0xffffffff;
  965. c = dividend_low >> 32;
  966. d = dividend_low & 0xffffffff;
  967. w = a / divisor;
  968. ra = ((u64)(a - (w * divisor)) << 32) + b;
  969. rb = ((u64) do_div(ra, divisor) << 32) + c;
  970. x = ra;
  971. rc = ((u64) do_div(rb, divisor) << 32) + d;
  972. y = rb;
  973. do_div(rc, divisor);
  974. z = rc;
  975. dr->result_high = ((u64)w << 32) + x;
  976. dr->result_low = ((u64)y << 32) + z;
  977. }
  978. /* We don't need to calibrate delay, we use the CPU timebase for that */
  979. void calibrate_delay(void)
  980. {
  981. /* Some generic code (such as spinlock debug) use loops_per_jiffy
  982. * as the number of __delay(1) in a jiffy, so make it so
  983. */
  984. loops_per_jiffy = tb_ticks_per_jiffy;
  985. }
  986. #if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
  987. static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
  988. {
  989. ppc_md.get_rtc_time(tm);
  990. return rtc_valid_tm(tm);
  991. }
  992. static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
  993. {
  994. if (!ppc_md.set_rtc_time)
  995. return -EOPNOTSUPP;
  996. if (ppc_md.set_rtc_time(tm) < 0)
  997. return -EOPNOTSUPP;
  998. return 0;
  999. }
  1000. static const struct rtc_class_ops rtc_generic_ops = {
  1001. .read_time = rtc_generic_get_time,
  1002. .set_time = rtc_generic_set_time,
  1003. };
  1004. static int __init rtc_init(void)
  1005. {
  1006. struct platform_device *pdev;
  1007. if (!ppc_md.get_rtc_time)
  1008. return -ENODEV;
  1009. pdev = platform_device_register_data(NULL, "rtc-generic", -1,
  1010. &rtc_generic_ops,
  1011. sizeof(rtc_generic_ops));
  1012. return PTR_ERR_OR_ZERO(pdev);
  1013. }
  1014. device_initcall(rtc_init);
  1015. #endif