vtime.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * arch/s390/kernel/vtime.c
  3. * Virtual cpu timer based timer functions.
  4. *
  5. * S390 version
  6. * Copyright (C) 2004 IBM Deutschland Entwicklung GmbH, IBM Corporation
  7. * Author(s): Jan Glauber <jan.glauber@de.ibm.com>
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/time.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/smp.h>
  15. #include <linux/types.h>
  16. #include <linux/timex.h>
  17. #include <linux/notifier.h>
  18. #include <linux/kernel_stat.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/posix-timers.h>
  21. #include <linux/cpu.h>
  22. #include <linux/kprobes.h>
  23. #include <asm/timer.h>
  24. #include <asm/irq_regs.h>
  25. #include <asm/cputime.h>
  26. #include <asm/irq.h>
  27. #include "entry.h"
  28. static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
  29. DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
  30. static inline __u64 get_vtimer(void)
  31. {
  32. __u64 timer;
  33. asm volatile("STPT %0" : "=m" (timer));
  34. return timer;
  35. }
  36. static inline void set_vtimer(__u64 expires)
  37. {
  38. __u64 timer;
  39. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  40. " SPT %1" /* Set new value immediately afterwards */
  41. : "=m" (timer) : "m" (expires) );
  42. S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
  43. S390_lowcore.last_update_timer = expires;
  44. }
  45. /*
  46. * Update process times based on virtual cpu times stored by entry.S
  47. * to the lowcore fields user_timer, system_timer & steal_clock.
  48. */
  49. static void do_account_vtime(struct task_struct *tsk, int hardirq_offset)
  50. {
  51. struct thread_info *ti = task_thread_info(tsk);
  52. __u64 timer, clock, user, system, steal;
  53. timer = S390_lowcore.last_update_timer;
  54. clock = S390_lowcore.last_update_clock;
  55. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  56. " STCK %1" /* Store current tod clock value */
  57. : "=m" (S390_lowcore.last_update_timer),
  58. "=m" (S390_lowcore.last_update_clock) );
  59. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  60. S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
  61. user = S390_lowcore.user_timer - ti->user_timer;
  62. S390_lowcore.steal_timer -= user;
  63. ti->user_timer = S390_lowcore.user_timer;
  64. account_user_time(tsk, user, user);
  65. system = S390_lowcore.system_timer - ti->system_timer;
  66. S390_lowcore.steal_timer -= system;
  67. ti->system_timer = S390_lowcore.system_timer;
  68. account_system_time(tsk, hardirq_offset, system, system);
  69. steal = S390_lowcore.steal_timer;
  70. if ((s64) steal > 0) {
  71. S390_lowcore.steal_timer = 0;
  72. account_steal_time(steal);
  73. }
  74. }
  75. void account_vtime(struct task_struct *prev, struct task_struct *next)
  76. {
  77. struct thread_info *ti;
  78. do_account_vtime(prev, 0);
  79. ti = task_thread_info(prev);
  80. ti->user_timer = S390_lowcore.user_timer;
  81. ti->system_timer = S390_lowcore.system_timer;
  82. ti = task_thread_info(next);
  83. S390_lowcore.user_timer = ti->user_timer;
  84. S390_lowcore.system_timer = ti->system_timer;
  85. }
  86. void account_process_tick(struct task_struct *tsk, int user_tick)
  87. {
  88. do_account_vtime(tsk, HARDIRQ_OFFSET);
  89. }
  90. /*
  91. * Update process times based on virtual cpu times stored by entry.S
  92. * to the lowcore fields user_timer, system_timer & steal_clock.
  93. */
  94. void account_system_vtime(struct task_struct *tsk)
  95. {
  96. struct thread_info *ti = task_thread_info(tsk);
  97. __u64 timer, system;
  98. timer = S390_lowcore.last_update_timer;
  99. S390_lowcore.last_update_timer = get_vtimer();
  100. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  101. system = S390_lowcore.system_timer - ti->system_timer;
  102. S390_lowcore.steal_timer -= system;
  103. ti->system_timer = S390_lowcore.system_timer;
  104. account_system_time(tsk, 0, system, system);
  105. }
  106. EXPORT_SYMBOL_GPL(account_system_vtime);
  107. void __kprobes vtime_stop_cpu(void)
  108. {
  109. struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
  110. struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
  111. unsigned long long idle_time;
  112. unsigned long psw_mask;
  113. trace_hardirqs_on();
  114. /* Don't trace preempt off for idle. */
  115. stop_critical_timings();
  116. /* Wait for external, I/O or machine check interrupt. */
  117. psw_mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_DAT |
  118. PSW_MASK_IO | PSW_MASK_EXT | PSW_MASK_MCHECK;
  119. idle->nohz_delay = 0;
  120. /* Call the assembler magic in entry.S */
  121. psw_idle(idle, vq, psw_mask, !list_empty(&vq->list));
  122. /* Reenable preemption tracer. */
  123. start_critical_timings();
  124. /* Account time spent with enabled wait psw loaded as idle time. */
  125. idle->sequence++;
  126. smp_wmb();
  127. idle_time = idle->idle_exit - idle->idle_enter;
  128. idle->idle_time += idle_time;
  129. idle->idle_enter = idle->idle_exit = 0ULL;
  130. idle->idle_count++;
  131. account_idle_time(idle_time);
  132. smp_wmb();
  133. idle->sequence++;
  134. }
  135. cputime64_t s390_get_idle_time(int cpu)
  136. {
  137. struct s390_idle_data *idle = &per_cpu(s390_idle, cpu);
  138. unsigned long long now, idle_enter, idle_exit;
  139. unsigned int sequence;
  140. do {
  141. now = get_clock();
  142. sequence = ACCESS_ONCE(idle->sequence);
  143. idle_enter = ACCESS_ONCE(idle->idle_enter);
  144. idle_exit = ACCESS_ONCE(idle->idle_exit);
  145. } while ((sequence & 1) || (idle->sequence != sequence));
  146. return idle_enter ? ((idle_exit ? : now) - idle_enter) : 0;
  147. }
  148. /*
  149. * Sorted add to a list. List is linear searched until first bigger
  150. * element is found.
  151. */
  152. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  153. {
  154. struct vtimer_list *event;
  155. list_for_each_entry(event, head, entry) {
  156. if (event->expires > timer->expires) {
  157. list_add_tail(&timer->entry, &event->entry);
  158. return;
  159. }
  160. }
  161. list_add_tail(&timer->entry, head);
  162. }
  163. /*
  164. * Do the callback functions of expired vtimer events.
  165. * Called from within the interrupt handler.
  166. */
  167. static void do_callbacks(struct list_head *cb_list)
  168. {
  169. struct vtimer_queue *vq;
  170. struct vtimer_list *event, *tmp;
  171. if (list_empty(cb_list))
  172. return;
  173. vq = &__get_cpu_var(virt_cpu_timer);
  174. list_for_each_entry_safe(event, tmp, cb_list, entry) {
  175. list_del_init(&event->entry);
  176. (event->function)(event->data);
  177. if (event->interval) {
  178. /* Recharge interval timer */
  179. event->expires = event->interval + vq->elapsed;
  180. spin_lock(&vq->lock);
  181. list_add_sorted(event, &vq->list);
  182. spin_unlock(&vq->lock);
  183. }
  184. }
  185. }
  186. /*
  187. * Handler for the virtual CPU timer.
  188. */
  189. static void do_cpu_timer_interrupt(struct ext_code ext_code,
  190. unsigned int param32, unsigned long param64)
  191. {
  192. struct vtimer_queue *vq;
  193. struct vtimer_list *event, *tmp;
  194. struct list_head cb_list; /* the callback queue */
  195. __u64 elapsed, next;
  196. kstat_cpu(smp_processor_id()).irqs[EXTINT_TMR]++;
  197. INIT_LIST_HEAD(&cb_list);
  198. vq = &__get_cpu_var(virt_cpu_timer);
  199. /* walk timer list, fire all expired events */
  200. spin_lock(&vq->lock);
  201. elapsed = vq->elapsed + (vq->timer - S390_lowcore.async_enter_timer);
  202. BUG_ON((s64) elapsed < 0);
  203. vq->elapsed = 0;
  204. list_for_each_entry_safe(event, tmp, &vq->list, entry) {
  205. if (event->expires < elapsed)
  206. /* move expired timer to the callback queue */
  207. list_move_tail(&event->entry, &cb_list);
  208. else
  209. event->expires -= elapsed;
  210. }
  211. spin_unlock(&vq->lock);
  212. do_callbacks(&cb_list);
  213. /* next event is first in list */
  214. next = VTIMER_MAX_SLICE;
  215. spin_lock(&vq->lock);
  216. if (!list_empty(&vq->list)) {
  217. event = list_first_entry(&vq->list, struct vtimer_list, entry);
  218. next = event->expires;
  219. }
  220. spin_unlock(&vq->lock);
  221. /*
  222. * To improve precision add the time spent by the
  223. * interrupt handler to the elapsed time.
  224. * Note: CPU timer counts down and we got an interrupt,
  225. * the current content is negative
  226. */
  227. elapsed = S390_lowcore.async_enter_timer - get_vtimer();
  228. set_vtimer(next - elapsed);
  229. vq->timer = next - elapsed;
  230. vq->elapsed = elapsed;
  231. }
  232. void init_virt_timer(struct vtimer_list *timer)
  233. {
  234. timer->function = NULL;
  235. INIT_LIST_HEAD(&timer->entry);
  236. }
  237. EXPORT_SYMBOL(init_virt_timer);
  238. static inline int vtimer_pending(struct vtimer_list *timer)
  239. {
  240. return (!list_empty(&timer->entry));
  241. }
  242. /*
  243. * this function should only run on the specified CPU
  244. */
  245. static void internal_add_vtimer(struct vtimer_list *timer)
  246. {
  247. struct vtimer_queue *vq;
  248. unsigned long flags;
  249. __u64 left, expires;
  250. vq = &per_cpu(virt_cpu_timer, timer->cpu);
  251. spin_lock_irqsave(&vq->lock, flags);
  252. BUG_ON(timer->cpu != smp_processor_id());
  253. if (list_empty(&vq->list)) {
  254. /* First timer on this cpu, just program it. */
  255. list_add(&timer->entry, &vq->list);
  256. set_vtimer(timer->expires);
  257. vq->timer = timer->expires;
  258. vq->elapsed = 0;
  259. } else {
  260. /* Check progress of old timers. */
  261. expires = timer->expires;
  262. left = get_vtimer();
  263. if (likely((s64) expires < (s64) left)) {
  264. /* The new timer expires before the current timer. */
  265. set_vtimer(expires);
  266. vq->elapsed += vq->timer - left;
  267. vq->timer = expires;
  268. } else {
  269. vq->elapsed += vq->timer - left;
  270. vq->timer = left;
  271. }
  272. /* Insert new timer into per cpu list. */
  273. timer->expires += vq->elapsed;
  274. list_add_sorted(timer, &vq->list);
  275. }
  276. spin_unlock_irqrestore(&vq->lock, flags);
  277. /* release CPU acquired in prepare_vtimer or mod_virt_timer() */
  278. put_cpu();
  279. }
  280. static inline void prepare_vtimer(struct vtimer_list *timer)
  281. {
  282. BUG_ON(!timer->function);
  283. BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
  284. BUG_ON(vtimer_pending(timer));
  285. timer->cpu = get_cpu();
  286. }
  287. /*
  288. * add_virt_timer - add an oneshot virtual CPU timer
  289. */
  290. void add_virt_timer(void *new)
  291. {
  292. struct vtimer_list *timer;
  293. timer = (struct vtimer_list *)new;
  294. prepare_vtimer(timer);
  295. timer->interval = 0;
  296. internal_add_vtimer(timer);
  297. }
  298. EXPORT_SYMBOL(add_virt_timer);
  299. /*
  300. * add_virt_timer_int - add an interval virtual CPU timer
  301. */
  302. void add_virt_timer_periodic(void *new)
  303. {
  304. struct vtimer_list *timer;
  305. timer = (struct vtimer_list *)new;
  306. prepare_vtimer(timer);
  307. timer->interval = timer->expires;
  308. internal_add_vtimer(timer);
  309. }
  310. EXPORT_SYMBOL(add_virt_timer_periodic);
  311. static int __mod_vtimer(struct vtimer_list *timer, __u64 expires, int periodic)
  312. {
  313. struct vtimer_queue *vq;
  314. unsigned long flags;
  315. int cpu;
  316. BUG_ON(!timer->function);
  317. BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
  318. if (timer->expires == expires && vtimer_pending(timer))
  319. return 1;
  320. cpu = get_cpu();
  321. vq = &per_cpu(virt_cpu_timer, cpu);
  322. /* disable interrupts before test if timer is pending */
  323. spin_lock_irqsave(&vq->lock, flags);
  324. /* if timer isn't pending add it on the current CPU */
  325. if (!vtimer_pending(timer)) {
  326. spin_unlock_irqrestore(&vq->lock, flags);
  327. if (periodic)
  328. timer->interval = expires;
  329. else
  330. timer->interval = 0;
  331. timer->expires = expires;
  332. timer->cpu = cpu;
  333. internal_add_vtimer(timer);
  334. return 0;
  335. }
  336. /* check if we run on the right CPU */
  337. BUG_ON(timer->cpu != cpu);
  338. list_del_init(&timer->entry);
  339. timer->expires = expires;
  340. if (periodic)
  341. timer->interval = expires;
  342. /* the timer can't expire anymore so we can release the lock */
  343. spin_unlock_irqrestore(&vq->lock, flags);
  344. internal_add_vtimer(timer);
  345. return 1;
  346. }
  347. /*
  348. * If we change a pending timer the function must be called on the CPU
  349. * where the timer is running on.
  350. *
  351. * returns whether it has modified a pending timer (1) or not (0)
  352. */
  353. int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
  354. {
  355. return __mod_vtimer(timer, expires, 0);
  356. }
  357. EXPORT_SYMBOL(mod_virt_timer);
  358. /*
  359. * If we change a pending timer the function must be called on the CPU
  360. * where the timer is running on.
  361. *
  362. * returns whether it has modified a pending timer (1) or not (0)
  363. */
  364. int mod_virt_timer_periodic(struct vtimer_list *timer, __u64 expires)
  365. {
  366. return __mod_vtimer(timer, expires, 1);
  367. }
  368. EXPORT_SYMBOL(mod_virt_timer_periodic);
  369. /*
  370. * delete a virtual timer
  371. *
  372. * returns whether the deleted timer was pending (1) or not (0)
  373. */
  374. int del_virt_timer(struct vtimer_list *timer)
  375. {
  376. unsigned long flags;
  377. struct vtimer_queue *vq;
  378. /* check if timer is pending */
  379. if (!vtimer_pending(timer))
  380. return 0;
  381. vq = &per_cpu(virt_cpu_timer, timer->cpu);
  382. spin_lock_irqsave(&vq->lock, flags);
  383. /* we don't interrupt a running timer, just let it expire! */
  384. list_del_init(&timer->entry);
  385. spin_unlock_irqrestore(&vq->lock, flags);
  386. return 1;
  387. }
  388. EXPORT_SYMBOL(del_virt_timer);
  389. /*
  390. * Start the virtual CPU timer on the current CPU.
  391. */
  392. void init_cpu_vtimer(void)
  393. {
  394. struct vtimer_queue *vq;
  395. /* initialize per cpu vtimer structure */
  396. vq = &__get_cpu_var(virt_cpu_timer);
  397. INIT_LIST_HEAD(&vq->list);
  398. spin_lock_init(&vq->lock);
  399. /* enable cpu timer interrupts */
  400. __ctl_set_bit(0,10);
  401. /* set initial cpu timer */
  402. set_vtimer(0x7fffffffffffffffULL);
  403. }
  404. static int __cpuinit s390_nohz_notify(struct notifier_block *self,
  405. unsigned long action, void *hcpu)
  406. {
  407. struct s390_idle_data *idle;
  408. long cpu = (long) hcpu;
  409. idle = &per_cpu(s390_idle, cpu);
  410. switch (action) {
  411. case CPU_DYING:
  412. case CPU_DYING_FROZEN:
  413. idle->nohz_delay = 0;
  414. default:
  415. break;
  416. }
  417. return NOTIFY_OK;
  418. }
  419. void __init vtime_init(void)
  420. {
  421. /* request the cpu timer external interrupt */
  422. if (register_external_interrupt(0x1005, do_cpu_timer_interrupt))
  423. panic("Couldn't request external interrupt 0x1005");
  424. /* Enable cpu timer interrupts on the boot cpu. */
  425. init_cpu_vtimer();
  426. cpu_notifier(s390_nohz_notify, 0);
  427. }