timer.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. /*
  2. * linux/kernel/timer.c
  3. *
  4. * Kernel internal timers, kernel timekeeping, basic process system calls
  5. *
  6. * Copyright (C) 1991, 1992 Linus Torvalds
  7. *
  8. * 1997-01-28 Modified by Finn Arne Gangstad to make timers scale better.
  9. *
  10. * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
  11. * "A Kernel Model for Precision Timekeeping" by Dave Mills
  12. * 1998-12-24 Fixed a xtime SMP race (we need the xtime_lock rw spinlock to
  13. * serialize accesses to xtime/lost_ticks).
  14. * Copyright (C) 1998 Andrea Arcangeli
  15. * 1999-03-10 Improved NTP compatibility by Ulrich Windl
  16. * 2002-05-31 Move sys_sysinfo here and make its locking sane, Robert Love
  17. * 2000-10-05 Implemented scalable SMP per-CPU timer handling.
  18. * Copyright (C) 2000, 2001, 2002 Ingo Molnar
  19. * Designed by David S. Miller, Alexey Kuznetsov and Ingo Molnar
  20. */
  21. #include <linux/kernel_stat.h>
  22. #include <linux/module.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/percpu.h>
  25. #include <linux/init.h>
  26. #include <linux/mm.h>
  27. #include <linux/swap.h>
  28. #include <linux/notifier.h>
  29. #include <linux/thread_info.h>
  30. #include <linux/time.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/posix-timers.h>
  33. #include <linux/cpu.h>
  34. #include <linux/syscalls.h>
  35. #include <linux/delay.h>
  36. #include <asm/uaccess.h>
  37. #include <asm/unistd.h>
  38. #include <asm/div64.h>
  39. #include <asm/timex.h>
  40. #include <asm/io.h>
  41. #ifdef CONFIG_TIME_INTERPOLATION
  42. static void time_interpolator_update(long delta_nsec);
  43. #else
  44. #define time_interpolator_update(x)
  45. #endif
  46. u64 jiffies_64 __cacheline_aligned_in_smp = INITIAL_JIFFIES;
  47. EXPORT_SYMBOL(jiffies_64);
  48. /*
  49. * per-CPU timer vector definitions:
  50. */
  51. #define TVN_BITS (CONFIG_BASE_SMALL ? 4 : 6)
  52. #define TVR_BITS (CONFIG_BASE_SMALL ? 6 : 8)
  53. #define TVN_SIZE (1 << TVN_BITS)
  54. #define TVR_SIZE (1 << TVR_BITS)
  55. #define TVN_MASK (TVN_SIZE - 1)
  56. #define TVR_MASK (TVR_SIZE - 1)
  57. struct timer_base_s {
  58. spinlock_t lock;
  59. struct timer_list *running_timer;
  60. };
  61. typedef struct tvec_s {
  62. struct list_head vec[TVN_SIZE];
  63. } tvec_t;
  64. typedef struct tvec_root_s {
  65. struct list_head vec[TVR_SIZE];
  66. } tvec_root_t;
  67. struct tvec_t_base_s {
  68. struct timer_base_s t_base;
  69. unsigned long timer_jiffies;
  70. tvec_root_t tv1;
  71. tvec_t tv2;
  72. tvec_t tv3;
  73. tvec_t tv4;
  74. tvec_t tv5;
  75. } ____cacheline_aligned_in_smp;
  76. typedef struct tvec_t_base_s tvec_base_t;
  77. static DEFINE_PER_CPU(tvec_base_t, tvec_bases);
  78. static inline void set_running_timer(tvec_base_t *base,
  79. struct timer_list *timer)
  80. {
  81. #ifdef CONFIG_SMP
  82. base->t_base.running_timer = timer;
  83. #endif
  84. }
  85. static void internal_add_timer(tvec_base_t *base, struct timer_list *timer)
  86. {
  87. unsigned long expires = timer->expires;
  88. unsigned long idx = expires - base->timer_jiffies;
  89. struct list_head *vec;
  90. if (idx < TVR_SIZE) {
  91. int i = expires & TVR_MASK;
  92. vec = base->tv1.vec + i;
  93. } else if (idx < 1 << (TVR_BITS + TVN_BITS)) {
  94. int i = (expires >> TVR_BITS) & TVN_MASK;
  95. vec = base->tv2.vec + i;
  96. } else if (idx < 1 << (TVR_BITS + 2 * TVN_BITS)) {
  97. int i = (expires >> (TVR_BITS + TVN_BITS)) & TVN_MASK;
  98. vec = base->tv3.vec + i;
  99. } else if (idx < 1 << (TVR_BITS + 3 * TVN_BITS)) {
  100. int i = (expires >> (TVR_BITS + 2 * TVN_BITS)) & TVN_MASK;
  101. vec = base->tv4.vec + i;
  102. } else if ((signed long) idx < 0) {
  103. /*
  104. * Can happen if you add a timer with expires == jiffies,
  105. * or you set a timer to go off in the past
  106. */
  107. vec = base->tv1.vec + (base->timer_jiffies & TVR_MASK);
  108. } else {
  109. int i;
  110. /* If the timeout is larger than 0xffffffff on 64-bit
  111. * architectures then we use the maximum timeout:
  112. */
  113. if (idx > 0xffffffffUL) {
  114. idx = 0xffffffffUL;
  115. expires = idx + base->timer_jiffies;
  116. }
  117. i = (expires >> (TVR_BITS + 3 * TVN_BITS)) & TVN_MASK;
  118. vec = base->tv5.vec + i;
  119. }
  120. /*
  121. * Timers are FIFO:
  122. */
  123. list_add_tail(&timer->entry, vec);
  124. }
  125. typedef struct timer_base_s timer_base_t;
  126. /*
  127. * Used by TIMER_INITIALIZER, we can't use per_cpu(tvec_bases)
  128. * at compile time, and we need timer->base to lock the timer.
  129. */
  130. timer_base_t __init_timer_base
  131. ____cacheline_aligned_in_smp = { .lock = SPIN_LOCK_UNLOCKED };
  132. EXPORT_SYMBOL(__init_timer_base);
  133. /***
  134. * init_timer - initialize a timer.
  135. * @timer: the timer to be initialized
  136. *
  137. * init_timer() must be done to a timer prior calling *any* of the
  138. * other timer functions.
  139. */
  140. void fastcall init_timer(struct timer_list *timer)
  141. {
  142. timer->entry.next = NULL;
  143. timer->base = &per_cpu(tvec_bases, raw_smp_processor_id()).t_base;
  144. }
  145. EXPORT_SYMBOL(init_timer);
  146. static inline void detach_timer(struct timer_list *timer,
  147. int clear_pending)
  148. {
  149. struct list_head *entry = &timer->entry;
  150. __list_del(entry->prev, entry->next);
  151. if (clear_pending)
  152. entry->next = NULL;
  153. entry->prev = LIST_POISON2;
  154. }
  155. /*
  156. * We are using hashed locking: holding per_cpu(tvec_bases).t_base.lock
  157. * means that all timers which are tied to this base via timer->base are
  158. * locked, and the base itself is locked too.
  159. *
  160. * So __run_timers/migrate_timers can safely modify all timers which could
  161. * be found on ->tvX lists.
  162. *
  163. * When the timer's base is locked, and the timer removed from list, it is
  164. * possible to set timer->base = NULL and drop the lock: the timer remains
  165. * locked.
  166. */
  167. static timer_base_t *lock_timer_base(struct timer_list *timer,
  168. unsigned long *flags)
  169. {
  170. timer_base_t *base;
  171. for (;;) {
  172. base = timer->base;
  173. if (likely(base != NULL)) {
  174. spin_lock_irqsave(&base->lock, *flags);
  175. if (likely(base == timer->base))
  176. return base;
  177. /* The timer has migrated to another CPU */
  178. spin_unlock_irqrestore(&base->lock, *flags);
  179. }
  180. cpu_relax();
  181. }
  182. }
  183. int __mod_timer(struct timer_list *timer, unsigned long expires)
  184. {
  185. timer_base_t *base;
  186. tvec_base_t *new_base;
  187. unsigned long flags;
  188. int ret = 0;
  189. BUG_ON(!timer->function);
  190. base = lock_timer_base(timer, &flags);
  191. if (timer_pending(timer)) {
  192. detach_timer(timer, 0);
  193. ret = 1;
  194. }
  195. new_base = &__get_cpu_var(tvec_bases);
  196. if (base != &new_base->t_base) {
  197. /*
  198. * We are trying to schedule the timer on the local CPU.
  199. * However we can't change timer's base while it is running,
  200. * otherwise del_timer_sync() can't detect that the timer's
  201. * handler yet has not finished. This also guarantees that
  202. * the timer is serialized wrt itself.
  203. */
  204. if (unlikely(base->running_timer == timer)) {
  205. /* The timer remains on a former base */
  206. new_base = container_of(base, tvec_base_t, t_base);
  207. } else {
  208. /* See the comment in lock_timer_base() */
  209. timer->base = NULL;
  210. spin_unlock(&base->lock);
  211. spin_lock(&new_base->t_base.lock);
  212. timer->base = &new_base->t_base;
  213. }
  214. }
  215. timer->expires = expires;
  216. internal_add_timer(new_base, timer);
  217. spin_unlock_irqrestore(&new_base->t_base.lock, flags);
  218. return ret;
  219. }
  220. EXPORT_SYMBOL(__mod_timer);
  221. /***
  222. * add_timer_on - start a timer on a particular CPU
  223. * @timer: the timer to be added
  224. * @cpu: the CPU to start it on
  225. *
  226. * This is not very scalable on SMP. Double adds are not possible.
  227. */
  228. void add_timer_on(struct timer_list *timer, int cpu)
  229. {
  230. tvec_base_t *base = &per_cpu(tvec_bases, cpu);
  231. unsigned long flags;
  232. BUG_ON(timer_pending(timer) || !timer->function);
  233. spin_lock_irqsave(&base->t_base.lock, flags);
  234. timer->base = &base->t_base;
  235. internal_add_timer(base, timer);
  236. spin_unlock_irqrestore(&base->t_base.lock, flags);
  237. }
  238. /***
  239. * mod_timer - modify a timer's timeout
  240. * @timer: the timer to be modified
  241. *
  242. * mod_timer is a more efficient way to update the expire field of an
  243. * active timer (if the timer is inactive it will be activated)
  244. *
  245. * mod_timer(timer, expires) is equivalent to:
  246. *
  247. * del_timer(timer); timer->expires = expires; add_timer(timer);
  248. *
  249. * Note that if there are multiple unserialized concurrent users of the
  250. * same timer, then mod_timer() is the only safe way to modify the timeout,
  251. * since add_timer() cannot modify an already running timer.
  252. *
  253. * The function returns whether it has modified a pending timer or not.
  254. * (ie. mod_timer() of an inactive timer returns 0, mod_timer() of an
  255. * active timer returns 1.)
  256. */
  257. int mod_timer(struct timer_list *timer, unsigned long expires)
  258. {
  259. BUG_ON(!timer->function);
  260. /*
  261. * This is a common optimization triggered by the
  262. * networking code - if the timer is re-modified
  263. * to be the same thing then just return:
  264. */
  265. if (timer->expires == expires && timer_pending(timer))
  266. return 1;
  267. return __mod_timer(timer, expires);
  268. }
  269. EXPORT_SYMBOL(mod_timer);
  270. /***
  271. * del_timer - deactive a timer.
  272. * @timer: the timer to be deactivated
  273. *
  274. * del_timer() deactivates a timer - this works on both active and inactive
  275. * timers.
  276. *
  277. * The function returns whether it has deactivated a pending timer or not.
  278. * (ie. del_timer() of an inactive timer returns 0, del_timer() of an
  279. * active timer returns 1.)
  280. */
  281. int del_timer(struct timer_list *timer)
  282. {
  283. timer_base_t *base;
  284. unsigned long flags;
  285. int ret = 0;
  286. if (timer_pending(timer)) {
  287. base = lock_timer_base(timer, &flags);
  288. if (timer_pending(timer)) {
  289. detach_timer(timer, 1);
  290. ret = 1;
  291. }
  292. spin_unlock_irqrestore(&base->lock, flags);
  293. }
  294. return ret;
  295. }
  296. EXPORT_SYMBOL(del_timer);
  297. #ifdef CONFIG_SMP
  298. /*
  299. * This function tries to deactivate a timer. Upon successful (ret >= 0)
  300. * exit the timer is not queued and the handler is not running on any CPU.
  301. *
  302. * It must not be called from interrupt contexts.
  303. */
  304. int try_to_del_timer_sync(struct timer_list *timer)
  305. {
  306. timer_base_t *base;
  307. unsigned long flags;
  308. int ret = -1;
  309. base = lock_timer_base(timer, &flags);
  310. if (base->running_timer == timer)
  311. goto out;
  312. ret = 0;
  313. if (timer_pending(timer)) {
  314. detach_timer(timer, 1);
  315. ret = 1;
  316. }
  317. out:
  318. spin_unlock_irqrestore(&base->lock, flags);
  319. return ret;
  320. }
  321. /***
  322. * del_timer_sync - deactivate a timer and wait for the handler to finish.
  323. * @timer: the timer to be deactivated
  324. *
  325. * This function only differs from del_timer() on SMP: besides deactivating
  326. * the timer it also makes sure the handler has finished executing on other
  327. * CPUs.
  328. *
  329. * Synchronization rules: callers must prevent restarting of the timer,
  330. * otherwise this function is meaningless. It must not be called from
  331. * interrupt contexts. The caller must not hold locks which would prevent
  332. * completion of the timer's handler. The timer's handler must not call
  333. * add_timer_on(). Upon exit the timer is not queued and the handler is
  334. * not running on any CPU.
  335. *
  336. * The function returns whether it has deactivated a pending timer or not.
  337. */
  338. int del_timer_sync(struct timer_list *timer)
  339. {
  340. for (;;) {
  341. int ret = try_to_del_timer_sync(timer);
  342. if (ret >= 0)
  343. return ret;
  344. }
  345. }
  346. EXPORT_SYMBOL(del_timer_sync);
  347. #endif
  348. static int cascade(tvec_base_t *base, tvec_t *tv, int index)
  349. {
  350. /* cascade all the timers from tv up one level */
  351. struct list_head *head, *curr;
  352. head = tv->vec + index;
  353. curr = head->next;
  354. /*
  355. * We are removing _all_ timers from the list, so we don't have to
  356. * detach them individually, just clear the list afterwards.
  357. */
  358. while (curr != head) {
  359. struct timer_list *tmp;
  360. tmp = list_entry(curr, struct timer_list, entry);
  361. BUG_ON(tmp->base != &base->t_base);
  362. curr = curr->next;
  363. internal_add_timer(base, tmp);
  364. }
  365. INIT_LIST_HEAD(head);
  366. return index;
  367. }
  368. /***
  369. * __run_timers - run all expired timers (if any) on this CPU.
  370. * @base: the timer vector to be processed.
  371. *
  372. * This function cascades all vectors and executes all expired timer
  373. * vectors.
  374. */
  375. #define INDEX(N) (base->timer_jiffies >> (TVR_BITS + N * TVN_BITS)) & TVN_MASK
  376. static inline void __run_timers(tvec_base_t *base)
  377. {
  378. struct timer_list *timer;
  379. spin_lock_irq(&base->t_base.lock);
  380. while (time_after_eq(jiffies, base->timer_jiffies)) {
  381. struct list_head work_list = LIST_HEAD_INIT(work_list);
  382. struct list_head *head = &work_list;
  383. int index = base->timer_jiffies & TVR_MASK;
  384. /*
  385. * Cascade timers:
  386. */
  387. if (!index &&
  388. (!cascade(base, &base->tv2, INDEX(0))) &&
  389. (!cascade(base, &base->tv3, INDEX(1))) &&
  390. !cascade(base, &base->tv4, INDEX(2)))
  391. cascade(base, &base->tv5, INDEX(3));
  392. ++base->timer_jiffies;
  393. list_splice_init(base->tv1.vec + index, &work_list);
  394. while (!list_empty(head)) {
  395. void (*fn)(unsigned long);
  396. unsigned long data;
  397. timer = list_entry(head->next,struct timer_list,entry);
  398. fn = timer->function;
  399. data = timer->data;
  400. set_running_timer(base, timer);
  401. detach_timer(timer, 1);
  402. spin_unlock_irq(&base->t_base.lock);
  403. {
  404. int preempt_count = preempt_count();
  405. fn(data);
  406. if (preempt_count != preempt_count()) {
  407. printk(KERN_WARNING "huh, entered %p "
  408. "with preempt_count %08x, exited"
  409. " with %08x?\n",
  410. fn, preempt_count,
  411. preempt_count());
  412. BUG();
  413. }
  414. }
  415. spin_lock_irq(&base->t_base.lock);
  416. }
  417. }
  418. set_running_timer(base, NULL);
  419. spin_unlock_irq(&base->t_base.lock);
  420. }
  421. #ifdef CONFIG_NO_IDLE_HZ
  422. /*
  423. * Find out when the next timer event is due to happen. This
  424. * is used on S/390 to stop all activity when a cpus is idle.
  425. * This functions needs to be called disabled.
  426. */
  427. unsigned long next_timer_interrupt(void)
  428. {
  429. tvec_base_t *base;
  430. struct list_head *list;
  431. struct timer_list *nte;
  432. unsigned long expires;
  433. tvec_t *varray[4];
  434. int i, j;
  435. base = &__get_cpu_var(tvec_bases);
  436. spin_lock(&base->t_base.lock);
  437. expires = base->timer_jiffies + (LONG_MAX >> 1);
  438. list = 0;
  439. /* Look for timer events in tv1. */
  440. j = base->timer_jiffies & TVR_MASK;
  441. do {
  442. list_for_each_entry(nte, base->tv1.vec + j, entry) {
  443. expires = nte->expires;
  444. if (j < (base->timer_jiffies & TVR_MASK))
  445. list = base->tv2.vec + (INDEX(0));
  446. goto found;
  447. }
  448. j = (j + 1) & TVR_MASK;
  449. } while (j != (base->timer_jiffies & TVR_MASK));
  450. /* Check tv2-tv5. */
  451. varray[0] = &base->tv2;
  452. varray[1] = &base->tv3;
  453. varray[2] = &base->tv4;
  454. varray[3] = &base->tv5;
  455. for (i = 0; i < 4; i++) {
  456. j = INDEX(i);
  457. do {
  458. if (list_empty(varray[i]->vec + j)) {
  459. j = (j + 1) & TVN_MASK;
  460. continue;
  461. }
  462. list_for_each_entry(nte, varray[i]->vec + j, entry)
  463. if (time_before(nte->expires, expires))
  464. expires = nte->expires;
  465. if (j < (INDEX(i)) && i < 3)
  466. list = varray[i + 1]->vec + (INDEX(i + 1));
  467. goto found;
  468. } while (j != (INDEX(i)));
  469. }
  470. found:
  471. if (list) {
  472. /*
  473. * The search wrapped. We need to look at the next list
  474. * from next tv element that would cascade into tv element
  475. * where we found the timer element.
  476. */
  477. list_for_each_entry(nte, list, entry) {
  478. if (time_before(nte->expires, expires))
  479. expires = nte->expires;
  480. }
  481. }
  482. spin_unlock(&base->t_base.lock);
  483. return expires;
  484. }
  485. #endif
  486. /******************************************************************/
  487. /*
  488. * Timekeeping variables
  489. */
  490. unsigned long tick_usec = TICK_USEC; /* USER_HZ period (usec) */
  491. unsigned long tick_nsec = TICK_NSEC; /* ACTHZ period (nsec) */
  492. /*
  493. * The current time
  494. * wall_to_monotonic is what we need to add to xtime (or xtime corrected
  495. * for sub jiffie times) to get to monotonic time. Monotonic is pegged
  496. * at zero at system boot time, so wall_to_monotonic will be negative,
  497. * however, we will ALWAYS keep the tv_nsec part positive so we can use
  498. * the usual normalization.
  499. */
  500. struct timespec xtime __attribute__ ((aligned (16)));
  501. struct timespec wall_to_monotonic __attribute__ ((aligned (16)));
  502. EXPORT_SYMBOL(xtime);
  503. /* Don't completely fail for HZ > 500. */
  504. int tickadj = 500/HZ ? : 1; /* microsecs */
  505. /*
  506. * phase-lock loop variables
  507. */
  508. /* TIME_ERROR prevents overwriting the CMOS clock */
  509. int time_state = TIME_OK; /* clock synchronization status */
  510. int time_status = STA_UNSYNC; /* clock status bits */
  511. long time_offset; /* time adjustment (us) */
  512. long time_constant = 2; /* pll time constant */
  513. long time_tolerance = MAXFREQ; /* frequency tolerance (ppm) */
  514. long time_precision = 1; /* clock precision (us) */
  515. long time_maxerror = NTP_PHASE_LIMIT; /* maximum error (us) */
  516. long time_esterror = NTP_PHASE_LIMIT; /* estimated error (us) */
  517. static long time_phase; /* phase offset (scaled us) */
  518. long time_freq = (((NSEC_PER_SEC + HZ/2) % HZ - HZ/2) << SHIFT_USEC) / NSEC_PER_USEC;
  519. /* frequency offset (scaled ppm)*/
  520. static long time_adj; /* tick adjust (scaled 1 / HZ) */
  521. long time_reftime; /* time at last adjustment (s) */
  522. long time_adjust;
  523. long time_next_adjust;
  524. /*
  525. * this routine handles the overflow of the microsecond field
  526. *
  527. * The tricky bits of code to handle the accurate clock support
  528. * were provided by Dave Mills (Mills@UDEL.EDU) of NTP fame.
  529. * They were originally developed for SUN and DEC kernels.
  530. * All the kudos should go to Dave for this stuff.
  531. *
  532. */
  533. static void second_overflow(void)
  534. {
  535. long ltemp;
  536. /* Bump the maxerror field */
  537. time_maxerror += time_tolerance >> SHIFT_USEC;
  538. if (time_maxerror > NTP_PHASE_LIMIT) {
  539. time_maxerror = NTP_PHASE_LIMIT;
  540. time_status |= STA_UNSYNC;
  541. }
  542. /*
  543. * Leap second processing. If in leap-insert state at the end of the
  544. * day, the system clock is set back one second; if in leap-delete
  545. * state, the system clock is set ahead one second. The microtime()
  546. * routine or external clock driver will insure that reported time is
  547. * always monotonic. The ugly divides should be replaced.
  548. */
  549. switch (time_state) {
  550. case TIME_OK:
  551. if (time_status & STA_INS)
  552. time_state = TIME_INS;
  553. else if (time_status & STA_DEL)
  554. time_state = TIME_DEL;
  555. break;
  556. case TIME_INS:
  557. if (xtime.tv_sec % 86400 == 0) {
  558. xtime.tv_sec--;
  559. wall_to_monotonic.tv_sec++;
  560. /*
  561. * The timer interpolator will make time change
  562. * gradually instead of an immediate jump by one second
  563. */
  564. time_interpolator_update(-NSEC_PER_SEC);
  565. time_state = TIME_OOP;
  566. clock_was_set();
  567. printk(KERN_NOTICE "Clock: inserting leap second "
  568. "23:59:60 UTC\n");
  569. }
  570. break;
  571. case TIME_DEL:
  572. if ((xtime.tv_sec + 1) % 86400 == 0) {
  573. xtime.tv_sec++;
  574. wall_to_monotonic.tv_sec--;
  575. /*
  576. * Use of time interpolator for a gradual change of
  577. * time
  578. */
  579. time_interpolator_update(NSEC_PER_SEC);
  580. time_state = TIME_WAIT;
  581. clock_was_set();
  582. printk(KERN_NOTICE "Clock: deleting leap second "
  583. "23:59:59 UTC\n");
  584. }
  585. break;
  586. case TIME_OOP:
  587. time_state = TIME_WAIT;
  588. break;
  589. case TIME_WAIT:
  590. if (!(time_status & (STA_INS | STA_DEL)))
  591. time_state = TIME_OK;
  592. }
  593. /*
  594. * Compute the phase adjustment for the next second. In PLL mode, the
  595. * offset is reduced by a fixed factor times the time constant. In FLL
  596. * mode the offset is used directly. In either mode, the maximum phase
  597. * adjustment for each second is clamped so as to spread the adjustment
  598. * over not more than the number of seconds between updates.
  599. */
  600. ltemp = time_offset;
  601. if (!(time_status & STA_FLL))
  602. ltemp = shift_right(ltemp, SHIFT_KG + time_constant);
  603. ltemp = min(ltemp, (MAXPHASE / MINSEC) << SHIFT_UPDATE);
  604. ltemp = max(ltemp, -(MAXPHASE / MINSEC) << SHIFT_UPDATE);
  605. time_offset -= ltemp;
  606. time_adj = ltemp << (SHIFT_SCALE - SHIFT_HZ - SHIFT_UPDATE);
  607. /*
  608. * Compute the frequency estimate and additional phase adjustment due
  609. * to frequency error for the next second. When the PPS signal is
  610. * engaged, gnaw on the watchdog counter and update the frequency
  611. * computed by the pll and the PPS signal.
  612. */
  613. pps_valid++;
  614. if (pps_valid == PPS_VALID) { /* PPS signal lost */
  615. pps_jitter = MAXTIME;
  616. pps_stabil = MAXFREQ;
  617. time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
  618. STA_PPSWANDER | STA_PPSERROR);
  619. }
  620. ltemp = time_freq + pps_freq;
  621. time_adj += shift_right(ltemp,(SHIFT_USEC + SHIFT_HZ - SHIFT_SCALE));
  622. #if HZ == 100
  623. /*
  624. * Compensate for (HZ==100) != (1 << SHIFT_HZ). Add 25% and 3.125% to
  625. * get 128.125; => only 0.125% error (p. 14)
  626. */
  627. time_adj += shift_right(time_adj, 2) + shift_right(time_adj, 5);
  628. #endif
  629. #if HZ == 250
  630. /*
  631. * Compensate for (HZ==250) != (1 << SHIFT_HZ). Add 1.5625% and
  632. * 0.78125% to get 255.85938; => only 0.05% error (p. 14)
  633. */
  634. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  635. #endif
  636. #if HZ == 1000
  637. /*
  638. * Compensate for (HZ==1000) != (1 << SHIFT_HZ). Add 1.5625% and
  639. * 0.78125% to get 1023.4375; => only 0.05% error (p. 14)
  640. */
  641. time_adj += shift_right(time_adj, 6) + shift_right(time_adj, 7);
  642. #endif
  643. }
  644. /* in the NTP reference this is called "hardclock()" */
  645. static void update_wall_time_one_tick(void)
  646. {
  647. long time_adjust_step, delta_nsec;
  648. if ((time_adjust_step = time_adjust) != 0 ) {
  649. /*
  650. * We are doing an adjtime thing. Prepare time_adjust_step to
  651. * be within bounds. Note that a positive time_adjust means we
  652. * want the clock to run faster.
  653. *
  654. * Limit the amount of the step to be in the range
  655. * -tickadj .. +tickadj
  656. */
  657. time_adjust_step = min(time_adjust_step, (long)tickadj);
  658. time_adjust_step = max(time_adjust_step, (long)-tickadj);
  659. /* Reduce by this step the amount of time left */
  660. time_adjust -= time_adjust_step;
  661. }
  662. delta_nsec = tick_nsec + time_adjust_step * 1000;
  663. /*
  664. * Advance the phase, once it gets to one microsecond, then
  665. * advance the tick more.
  666. */
  667. time_phase += time_adj;
  668. if ((time_phase >= FINENSEC) || (time_phase <= -FINENSEC)) {
  669. long ltemp = shift_right(time_phase, (SHIFT_SCALE - 10));
  670. time_phase -= ltemp << (SHIFT_SCALE - 10);
  671. delta_nsec += ltemp;
  672. }
  673. xtime.tv_nsec += delta_nsec;
  674. time_interpolator_update(delta_nsec);
  675. /* Changes by adjtime() do not take effect till next tick. */
  676. if (time_next_adjust != 0) {
  677. time_adjust = time_next_adjust;
  678. time_next_adjust = 0;
  679. }
  680. }
  681. /*
  682. * Using a loop looks inefficient, but "ticks" is
  683. * usually just one (we shouldn't be losing ticks,
  684. * we're doing this this way mainly for interrupt
  685. * latency reasons, not because we think we'll
  686. * have lots of lost timer ticks
  687. */
  688. static void update_wall_time(unsigned long ticks)
  689. {
  690. do {
  691. ticks--;
  692. update_wall_time_one_tick();
  693. if (xtime.tv_nsec >= 1000000000) {
  694. xtime.tv_nsec -= 1000000000;
  695. xtime.tv_sec++;
  696. second_overflow();
  697. }
  698. } while (ticks);
  699. }
  700. /*
  701. * Called from the timer interrupt handler to charge one tick to the current
  702. * process. user_tick is 1 if the tick is user time, 0 for system.
  703. */
  704. void update_process_times(int user_tick)
  705. {
  706. struct task_struct *p = current;
  707. int cpu = smp_processor_id();
  708. /* Note: this timer irq context must be accounted for as well. */
  709. if (user_tick)
  710. account_user_time(p, jiffies_to_cputime(1));
  711. else
  712. account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
  713. run_local_timers();
  714. if (rcu_pending(cpu))
  715. rcu_check_callbacks(cpu, user_tick);
  716. scheduler_tick();
  717. run_posix_cpu_timers(p);
  718. }
  719. /*
  720. * Nr of active tasks - counted in fixed-point numbers
  721. */
  722. static unsigned long count_active_tasks(void)
  723. {
  724. return (nr_running() + nr_uninterruptible()) * FIXED_1;
  725. }
  726. /*
  727. * Hmm.. Changed this, as the GNU make sources (load.c) seems to
  728. * imply that avenrun[] is the standard name for this kind of thing.
  729. * Nothing else seems to be standardized: the fractional size etc
  730. * all seem to differ on different machines.
  731. *
  732. * Requires xtime_lock to access.
  733. */
  734. unsigned long avenrun[3];
  735. EXPORT_SYMBOL(avenrun);
  736. /*
  737. * calc_load - given tick count, update the avenrun load estimates.
  738. * This is called while holding a write_lock on xtime_lock.
  739. */
  740. static inline void calc_load(unsigned long ticks)
  741. {
  742. unsigned long active_tasks; /* fixed-point */
  743. static int count = LOAD_FREQ;
  744. count -= ticks;
  745. if (count < 0) {
  746. count += LOAD_FREQ;
  747. active_tasks = count_active_tasks();
  748. CALC_LOAD(avenrun[0], EXP_1, active_tasks);
  749. CALC_LOAD(avenrun[1], EXP_5, active_tasks);
  750. CALC_LOAD(avenrun[2], EXP_15, active_tasks);
  751. }
  752. }
  753. /* jiffies at the most recent update of wall time */
  754. unsigned long wall_jiffies = INITIAL_JIFFIES;
  755. /*
  756. * This read-write spinlock protects us from races in SMP while
  757. * playing with xtime and avenrun.
  758. */
  759. #ifndef ARCH_HAVE_XTIME_LOCK
  760. seqlock_t xtime_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
  761. EXPORT_SYMBOL(xtime_lock);
  762. #endif
  763. /*
  764. * This function runs timers and the timer-tq in bottom half context.
  765. */
  766. static void run_timer_softirq(struct softirq_action *h)
  767. {
  768. tvec_base_t *base = &__get_cpu_var(tvec_bases);
  769. hrtimer_run_queues();
  770. if (time_after_eq(jiffies, base->timer_jiffies))
  771. __run_timers(base);
  772. }
  773. /*
  774. * Called by the local, per-CPU timer interrupt on SMP.
  775. */
  776. void run_local_timers(void)
  777. {
  778. raise_softirq(TIMER_SOFTIRQ);
  779. }
  780. /*
  781. * Called by the timer interrupt. xtime_lock must already be taken
  782. * by the timer IRQ!
  783. */
  784. static inline void update_times(void)
  785. {
  786. unsigned long ticks;
  787. ticks = jiffies - wall_jiffies;
  788. if (ticks) {
  789. wall_jiffies += ticks;
  790. update_wall_time(ticks);
  791. }
  792. calc_load(ticks);
  793. }
  794. /*
  795. * The 64-bit jiffies value is not atomic - you MUST NOT read it
  796. * without sampling the sequence number in xtime_lock.
  797. * jiffies is defined in the linker script...
  798. */
  799. void do_timer(struct pt_regs *regs)
  800. {
  801. jiffies_64++;
  802. update_times();
  803. softlockup_tick(regs);
  804. }
  805. #ifdef __ARCH_WANT_SYS_ALARM
  806. /*
  807. * For backwards compatibility? This can be done in libc so Alpha
  808. * and all newer ports shouldn't need it.
  809. */
  810. asmlinkage unsigned long sys_alarm(unsigned int seconds)
  811. {
  812. struct itimerval it_new, it_old;
  813. unsigned int oldalarm;
  814. it_new.it_interval.tv_sec = it_new.it_interval.tv_usec = 0;
  815. it_new.it_value.tv_sec = seconds;
  816. it_new.it_value.tv_usec = 0;
  817. do_setitimer(ITIMER_REAL, &it_new, &it_old);
  818. oldalarm = it_old.it_value.tv_sec;
  819. /* ehhh.. We can't return 0 if we have an alarm pending.. */
  820. /* And we'd better return too much than too little anyway */
  821. if ((!oldalarm && it_old.it_value.tv_usec) || it_old.it_value.tv_usec >= 500000)
  822. oldalarm++;
  823. return oldalarm;
  824. }
  825. #endif
  826. #ifndef __alpha__
  827. /*
  828. * The Alpha uses getxpid, getxuid, and getxgid instead. Maybe this
  829. * should be moved into arch/i386 instead?
  830. */
  831. /**
  832. * sys_getpid - return the thread group id of the current process
  833. *
  834. * Note, despite the name, this returns the tgid not the pid. The tgid and
  835. * the pid are identical unless CLONE_THREAD was specified on clone() in
  836. * which case the tgid is the same in all threads of the same group.
  837. *
  838. * This is SMP safe as current->tgid does not change.
  839. */
  840. asmlinkage long sys_getpid(void)
  841. {
  842. return current->tgid;
  843. }
  844. /*
  845. * Accessing ->group_leader->real_parent is not SMP-safe, it could
  846. * change from under us. However, rather than getting any lock
  847. * we can use an optimistic algorithm: get the parent
  848. * pid, and go back and check that the parent is still
  849. * the same. If it has changed (which is extremely unlikely
  850. * indeed), we just try again..
  851. *
  852. * NOTE! This depends on the fact that even if we _do_
  853. * get an old value of "parent", we can happily dereference
  854. * the pointer (it was and remains a dereferencable kernel pointer
  855. * no matter what): we just can't necessarily trust the result
  856. * until we know that the parent pointer is valid.
  857. *
  858. * NOTE2: ->group_leader never changes from under us.
  859. */
  860. asmlinkage long sys_getppid(void)
  861. {
  862. int pid;
  863. struct task_struct *me = current;
  864. struct task_struct *parent;
  865. parent = me->group_leader->real_parent;
  866. for (;;) {
  867. pid = parent->tgid;
  868. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
  869. {
  870. struct task_struct *old = parent;
  871. /*
  872. * Make sure we read the pid before re-reading the
  873. * parent pointer:
  874. */
  875. smp_rmb();
  876. parent = me->group_leader->real_parent;
  877. if (old != parent)
  878. continue;
  879. }
  880. #endif
  881. break;
  882. }
  883. return pid;
  884. }
  885. asmlinkage long sys_getuid(void)
  886. {
  887. /* Only we change this so SMP safe */
  888. return current->uid;
  889. }
  890. asmlinkage long sys_geteuid(void)
  891. {
  892. /* Only we change this so SMP safe */
  893. return current->euid;
  894. }
  895. asmlinkage long sys_getgid(void)
  896. {
  897. /* Only we change this so SMP safe */
  898. return current->gid;
  899. }
  900. asmlinkage long sys_getegid(void)
  901. {
  902. /* Only we change this so SMP safe */
  903. return current->egid;
  904. }
  905. #endif
  906. static void process_timeout(unsigned long __data)
  907. {
  908. wake_up_process((task_t *)__data);
  909. }
  910. /**
  911. * schedule_timeout - sleep until timeout
  912. * @timeout: timeout value in jiffies
  913. *
  914. * Make the current task sleep until @timeout jiffies have
  915. * elapsed. The routine will return immediately unless
  916. * the current task state has been set (see set_current_state()).
  917. *
  918. * You can set the task state as follows -
  919. *
  920. * %TASK_UNINTERRUPTIBLE - at least @timeout jiffies are guaranteed to
  921. * pass before the routine returns. The routine will return 0
  922. *
  923. * %TASK_INTERRUPTIBLE - the routine may return early if a signal is
  924. * delivered to the current task. In this case the remaining time
  925. * in jiffies will be returned, or 0 if the timer expired in time
  926. *
  927. * The current task state is guaranteed to be TASK_RUNNING when this
  928. * routine returns.
  929. *
  930. * Specifying a @timeout value of %MAX_SCHEDULE_TIMEOUT will schedule
  931. * the CPU away without a bound on the timeout. In this case the return
  932. * value will be %MAX_SCHEDULE_TIMEOUT.
  933. *
  934. * In all cases the return value is guaranteed to be non-negative.
  935. */
  936. fastcall signed long __sched schedule_timeout(signed long timeout)
  937. {
  938. struct timer_list timer;
  939. unsigned long expire;
  940. switch (timeout)
  941. {
  942. case MAX_SCHEDULE_TIMEOUT:
  943. /*
  944. * These two special cases are useful to be comfortable
  945. * in the caller. Nothing more. We could take
  946. * MAX_SCHEDULE_TIMEOUT from one of the negative value
  947. * but I' d like to return a valid offset (>=0) to allow
  948. * the caller to do everything it want with the retval.
  949. */
  950. schedule();
  951. goto out;
  952. default:
  953. /*
  954. * Another bit of PARANOID. Note that the retval will be
  955. * 0 since no piece of kernel is supposed to do a check
  956. * for a negative retval of schedule_timeout() (since it
  957. * should never happens anyway). You just have the printk()
  958. * that will tell you if something is gone wrong and where.
  959. */
  960. if (timeout < 0)
  961. {
  962. printk(KERN_ERR "schedule_timeout: wrong timeout "
  963. "value %lx from %p\n", timeout,
  964. __builtin_return_address(0));
  965. current->state = TASK_RUNNING;
  966. goto out;
  967. }
  968. }
  969. expire = timeout + jiffies;
  970. setup_timer(&timer, process_timeout, (unsigned long)current);
  971. __mod_timer(&timer, expire);
  972. schedule();
  973. del_singleshot_timer_sync(&timer);
  974. timeout = expire - jiffies;
  975. out:
  976. return timeout < 0 ? 0 : timeout;
  977. }
  978. EXPORT_SYMBOL(schedule_timeout);
  979. /*
  980. * We can use __set_current_state() here because schedule_timeout() calls
  981. * schedule() unconditionally.
  982. */
  983. signed long __sched schedule_timeout_interruptible(signed long timeout)
  984. {
  985. __set_current_state(TASK_INTERRUPTIBLE);
  986. return schedule_timeout(timeout);
  987. }
  988. EXPORT_SYMBOL(schedule_timeout_interruptible);
  989. signed long __sched schedule_timeout_uninterruptible(signed long timeout)
  990. {
  991. __set_current_state(TASK_UNINTERRUPTIBLE);
  992. return schedule_timeout(timeout);
  993. }
  994. EXPORT_SYMBOL(schedule_timeout_uninterruptible);
  995. /* Thread ID - the internal kernel "pid" */
  996. asmlinkage long sys_gettid(void)
  997. {
  998. return current->pid;
  999. }
  1000. /*
  1001. * sys_sysinfo - fill in sysinfo struct
  1002. */
  1003. asmlinkage long sys_sysinfo(struct sysinfo __user *info)
  1004. {
  1005. struct sysinfo val;
  1006. unsigned long mem_total, sav_total;
  1007. unsigned int mem_unit, bitcount;
  1008. unsigned long seq;
  1009. memset((char *)&val, 0, sizeof(struct sysinfo));
  1010. do {
  1011. struct timespec tp;
  1012. seq = read_seqbegin(&xtime_lock);
  1013. /*
  1014. * This is annoying. The below is the same thing
  1015. * posix_get_clock_monotonic() does, but it wants to
  1016. * take the lock which we want to cover the loads stuff
  1017. * too.
  1018. */
  1019. getnstimeofday(&tp);
  1020. tp.tv_sec += wall_to_monotonic.tv_sec;
  1021. tp.tv_nsec += wall_to_monotonic.tv_nsec;
  1022. if (tp.tv_nsec - NSEC_PER_SEC >= 0) {
  1023. tp.tv_nsec = tp.tv_nsec - NSEC_PER_SEC;
  1024. tp.tv_sec++;
  1025. }
  1026. val.uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
  1027. val.loads[0] = avenrun[0] << (SI_LOAD_SHIFT - FSHIFT);
  1028. val.loads[1] = avenrun[1] << (SI_LOAD_SHIFT - FSHIFT);
  1029. val.loads[2] = avenrun[2] << (SI_LOAD_SHIFT - FSHIFT);
  1030. val.procs = nr_threads;
  1031. } while (read_seqretry(&xtime_lock, seq));
  1032. si_meminfo(&val);
  1033. si_swapinfo(&val);
  1034. /*
  1035. * If the sum of all the available memory (i.e. ram + swap)
  1036. * is less than can be stored in a 32 bit unsigned long then
  1037. * we can be binary compatible with 2.2.x kernels. If not,
  1038. * well, in that case 2.2.x was broken anyways...
  1039. *
  1040. * -Erik Andersen <andersee@debian.org>
  1041. */
  1042. mem_total = val.totalram + val.totalswap;
  1043. if (mem_total < val.totalram || mem_total < val.totalswap)
  1044. goto out;
  1045. bitcount = 0;
  1046. mem_unit = val.mem_unit;
  1047. while (mem_unit > 1) {
  1048. bitcount++;
  1049. mem_unit >>= 1;
  1050. sav_total = mem_total;
  1051. mem_total <<= 1;
  1052. if (mem_total < sav_total)
  1053. goto out;
  1054. }
  1055. /*
  1056. * If mem_total did not overflow, multiply all memory values by
  1057. * val.mem_unit and set it to 1. This leaves things compatible
  1058. * with 2.2.x, and also retains compatibility with earlier 2.4.x
  1059. * kernels...
  1060. */
  1061. val.mem_unit = 1;
  1062. val.totalram <<= bitcount;
  1063. val.freeram <<= bitcount;
  1064. val.sharedram <<= bitcount;
  1065. val.bufferram <<= bitcount;
  1066. val.totalswap <<= bitcount;
  1067. val.freeswap <<= bitcount;
  1068. val.totalhigh <<= bitcount;
  1069. val.freehigh <<= bitcount;
  1070. out:
  1071. if (copy_to_user(info, &val, sizeof(struct sysinfo)))
  1072. return -EFAULT;
  1073. return 0;
  1074. }
  1075. static void __devinit init_timers_cpu(int cpu)
  1076. {
  1077. int j;
  1078. tvec_base_t *base;
  1079. base = &per_cpu(tvec_bases, cpu);
  1080. spin_lock_init(&base->t_base.lock);
  1081. for (j = 0; j < TVN_SIZE; j++) {
  1082. INIT_LIST_HEAD(base->tv5.vec + j);
  1083. INIT_LIST_HEAD(base->tv4.vec + j);
  1084. INIT_LIST_HEAD(base->tv3.vec + j);
  1085. INIT_LIST_HEAD(base->tv2.vec + j);
  1086. }
  1087. for (j = 0; j < TVR_SIZE; j++)
  1088. INIT_LIST_HEAD(base->tv1.vec + j);
  1089. base->timer_jiffies = jiffies;
  1090. }
  1091. #ifdef CONFIG_HOTPLUG_CPU
  1092. static void migrate_timer_list(tvec_base_t *new_base, struct list_head *head)
  1093. {
  1094. struct timer_list *timer;
  1095. while (!list_empty(head)) {
  1096. timer = list_entry(head->next, struct timer_list, entry);
  1097. detach_timer(timer, 0);
  1098. timer->base = &new_base->t_base;
  1099. internal_add_timer(new_base, timer);
  1100. }
  1101. }
  1102. static void __devinit migrate_timers(int cpu)
  1103. {
  1104. tvec_base_t *old_base;
  1105. tvec_base_t *new_base;
  1106. int i;
  1107. BUG_ON(cpu_online(cpu));
  1108. old_base = &per_cpu(tvec_bases, cpu);
  1109. new_base = &get_cpu_var(tvec_bases);
  1110. local_irq_disable();
  1111. spin_lock(&new_base->t_base.lock);
  1112. spin_lock(&old_base->t_base.lock);
  1113. if (old_base->t_base.running_timer)
  1114. BUG();
  1115. for (i = 0; i < TVR_SIZE; i++)
  1116. migrate_timer_list(new_base, old_base->tv1.vec + i);
  1117. for (i = 0; i < TVN_SIZE; i++) {
  1118. migrate_timer_list(new_base, old_base->tv2.vec + i);
  1119. migrate_timer_list(new_base, old_base->tv3.vec + i);
  1120. migrate_timer_list(new_base, old_base->tv4.vec + i);
  1121. migrate_timer_list(new_base, old_base->tv5.vec + i);
  1122. }
  1123. spin_unlock(&old_base->t_base.lock);
  1124. spin_unlock(&new_base->t_base.lock);
  1125. local_irq_enable();
  1126. put_cpu_var(tvec_bases);
  1127. }
  1128. #endif /* CONFIG_HOTPLUG_CPU */
  1129. static int __devinit timer_cpu_notify(struct notifier_block *self,
  1130. unsigned long action, void *hcpu)
  1131. {
  1132. long cpu = (long)hcpu;
  1133. switch(action) {
  1134. case CPU_UP_PREPARE:
  1135. init_timers_cpu(cpu);
  1136. break;
  1137. #ifdef CONFIG_HOTPLUG_CPU
  1138. case CPU_DEAD:
  1139. migrate_timers(cpu);
  1140. break;
  1141. #endif
  1142. default:
  1143. break;
  1144. }
  1145. return NOTIFY_OK;
  1146. }
  1147. static struct notifier_block __devinitdata timers_nb = {
  1148. .notifier_call = timer_cpu_notify,
  1149. };
  1150. void __init init_timers(void)
  1151. {
  1152. timer_cpu_notify(&timers_nb, (unsigned long)CPU_UP_PREPARE,
  1153. (void *)(long)smp_processor_id());
  1154. register_cpu_notifier(&timers_nb);
  1155. open_softirq(TIMER_SOFTIRQ, run_timer_softirq, NULL);
  1156. }
  1157. #ifdef CONFIG_TIME_INTERPOLATION
  1158. struct time_interpolator *time_interpolator;
  1159. static struct time_interpolator *time_interpolator_list;
  1160. static DEFINE_SPINLOCK(time_interpolator_lock);
  1161. static inline u64 time_interpolator_get_cycles(unsigned int src)
  1162. {
  1163. unsigned long (*x)(void);
  1164. switch (src)
  1165. {
  1166. case TIME_SOURCE_FUNCTION:
  1167. x = time_interpolator->addr;
  1168. return x();
  1169. case TIME_SOURCE_MMIO64 :
  1170. return readq((void __iomem *) time_interpolator->addr);
  1171. case TIME_SOURCE_MMIO32 :
  1172. return readl((void __iomem *) time_interpolator->addr);
  1173. default: return get_cycles();
  1174. }
  1175. }
  1176. static inline u64 time_interpolator_get_counter(int writelock)
  1177. {
  1178. unsigned int src = time_interpolator->source;
  1179. if (time_interpolator->jitter)
  1180. {
  1181. u64 lcycle;
  1182. u64 now;
  1183. do {
  1184. lcycle = time_interpolator->last_cycle;
  1185. now = time_interpolator_get_cycles(src);
  1186. if (lcycle && time_after(lcycle, now))
  1187. return lcycle;
  1188. /* When holding the xtime write lock, there's no need
  1189. * to add the overhead of the cmpxchg. Readers are
  1190. * force to retry until the write lock is released.
  1191. */
  1192. if (writelock) {
  1193. time_interpolator->last_cycle = now;
  1194. return now;
  1195. }
  1196. /* Keep track of the last timer value returned. The use of cmpxchg here
  1197. * will cause contention in an SMP environment.
  1198. */
  1199. } while (unlikely(cmpxchg(&time_interpolator->last_cycle, lcycle, now) != lcycle));
  1200. return now;
  1201. }
  1202. else
  1203. return time_interpolator_get_cycles(src);
  1204. }
  1205. void time_interpolator_reset(void)
  1206. {
  1207. time_interpolator->offset = 0;
  1208. time_interpolator->last_counter = time_interpolator_get_counter(1);
  1209. }
  1210. #define GET_TI_NSECS(count,i) (((((count) - i->last_counter) & (i)->mask) * (i)->nsec_per_cyc) >> (i)->shift)
  1211. unsigned long time_interpolator_get_offset(void)
  1212. {
  1213. /* If we do not have a time interpolator set up then just return zero */
  1214. if (!time_interpolator)
  1215. return 0;
  1216. return time_interpolator->offset +
  1217. GET_TI_NSECS(time_interpolator_get_counter(0), time_interpolator);
  1218. }
  1219. #define INTERPOLATOR_ADJUST 65536
  1220. #define INTERPOLATOR_MAX_SKIP 10*INTERPOLATOR_ADJUST
  1221. static void time_interpolator_update(long delta_nsec)
  1222. {
  1223. u64 counter;
  1224. unsigned long offset;
  1225. /* If there is no time interpolator set up then do nothing */
  1226. if (!time_interpolator)
  1227. return;
  1228. /*
  1229. * The interpolator compensates for late ticks by accumulating the late
  1230. * time in time_interpolator->offset. A tick earlier than expected will
  1231. * lead to a reset of the offset and a corresponding jump of the clock
  1232. * forward. Again this only works if the interpolator clock is running
  1233. * slightly slower than the regular clock and the tuning logic insures
  1234. * that.
  1235. */
  1236. counter = time_interpolator_get_counter(1);
  1237. offset = time_interpolator->offset +
  1238. GET_TI_NSECS(counter, time_interpolator);
  1239. if (delta_nsec < 0 || (unsigned long) delta_nsec < offset)
  1240. time_interpolator->offset = offset - delta_nsec;
  1241. else {
  1242. time_interpolator->skips++;
  1243. time_interpolator->ns_skipped += delta_nsec - offset;
  1244. time_interpolator->offset = 0;
  1245. }
  1246. time_interpolator->last_counter = counter;
  1247. /* Tuning logic for time interpolator invoked every minute or so.
  1248. * Decrease interpolator clock speed if no skips occurred and an offset is carried.
  1249. * Increase interpolator clock speed if we skip too much time.
  1250. */
  1251. if (jiffies % INTERPOLATOR_ADJUST == 0)
  1252. {
  1253. if (time_interpolator->skips == 0 && time_interpolator->offset > TICK_NSEC)
  1254. time_interpolator->nsec_per_cyc--;
  1255. if (time_interpolator->ns_skipped > INTERPOLATOR_MAX_SKIP && time_interpolator->offset == 0)
  1256. time_interpolator->nsec_per_cyc++;
  1257. time_interpolator->skips = 0;
  1258. time_interpolator->ns_skipped = 0;
  1259. }
  1260. }
  1261. static inline int
  1262. is_better_time_interpolator(struct time_interpolator *new)
  1263. {
  1264. if (!time_interpolator)
  1265. return 1;
  1266. return new->frequency > 2*time_interpolator->frequency ||
  1267. (unsigned long)new->drift < (unsigned long)time_interpolator->drift;
  1268. }
  1269. void
  1270. register_time_interpolator(struct time_interpolator *ti)
  1271. {
  1272. unsigned long flags;
  1273. /* Sanity check */
  1274. if (ti->frequency == 0 || ti->mask == 0)
  1275. BUG();
  1276. ti->nsec_per_cyc = ((u64)NSEC_PER_SEC << ti->shift) / ti->frequency;
  1277. spin_lock(&time_interpolator_lock);
  1278. write_seqlock_irqsave(&xtime_lock, flags);
  1279. if (is_better_time_interpolator(ti)) {
  1280. time_interpolator = ti;
  1281. time_interpolator_reset();
  1282. }
  1283. write_sequnlock_irqrestore(&xtime_lock, flags);
  1284. ti->next = time_interpolator_list;
  1285. time_interpolator_list = ti;
  1286. spin_unlock(&time_interpolator_lock);
  1287. }
  1288. void
  1289. unregister_time_interpolator(struct time_interpolator *ti)
  1290. {
  1291. struct time_interpolator *curr, **prev;
  1292. unsigned long flags;
  1293. spin_lock(&time_interpolator_lock);
  1294. prev = &time_interpolator_list;
  1295. for (curr = *prev; curr; curr = curr->next) {
  1296. if (curr == ti) {
  1297. *prev = curr->next;
  1298. break;
  1299. }
  1300. prev = &curr->next;
  1301. }
  1302. write_seqlock_irqsave(&xtime_lock, flags);
  1303. if (ti == time_interpolator) {
  1304. /* we lost the best time-interpolator: */
  1305. time_interpolator = NULL;
  1306. /* find the next-best interpolator */
  1307. for (curr = time_interpolator_list; curr; curr = curr->next)
  1308. if (is_better_time_interpolator(curr))
  1309. time_interpolator = curr;
  1310. time_interpolator_reset();
  1311. }
  1312. write_sequnlock_irqrestore(&xtime_lock, flags);
  1313. spin_unlock(&time_interpolator_lock);
  1314. }
  1315. #endif /* CONFIG_TIME_INTERPOLATION */
  1316. /**
  1317. * msleep - sleep safely even with waitqueue interruptions
  1318. * @msecs: Time in milliseconds to sleep for
  1319. */
  1320. void msleep(unsigned int msecs)
  1321. {
  1322. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1323. while (timeout)
  1324. timeout = schedule_timeout_uninterruptible(timeout);
  1325. }
  1326. EXPORT_SYMBOL(msleep);
  1327. /**
  1328. * msleep_interruptible - sleep waiting for signals
  1329. * @msecs: Time in milliseconds to sleep for
  1330. */
  1331. unsigned long msleep_interruptible(unsigned int msecs)
  1332. {
  1333. unsigned long timeout = msecs_to_jiffies(msecs) + 1;
  1334. while (timeout && !signal_pending(current))
  1335. timeout = schedule_timeout_interruptible(timeout);
  1336. return jiffies_to_msecs(timeout);
  1337. }
  1338. EXPORT_SYMBOL(msleep_interruptible);