time.c 31 KB

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