vtime.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 <asm/s390_ext.h>
  23. #include <asm/timer.h>
  24. #include <asm/irq_regs.h>
  25. #include <asm/cputime.h>
  26. static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
  27. DEFINE_PER_CPU(struct s390_idle_data, s390_idle);
  28. static inline __u64 get_vtimer(void)
  29. {
  30. __u64 timer;
  31. asm volatile("STPT %0" : "=m" (timer));
  32. return timer;
  33. }
  34. static inline void set_vtimer(__u64 expires)
  35. {
  36. __u64 timer;
  37. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  38. " SPT %1" /* Set new value immediatly afterwards */
  39. : "=m" (timer) : "m" (expires) );
  40. S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
  41. S390_lowcore.last_update_timer = expires;
  42. }
  43. /*
  44. * Update process times based on virtual cpu times stored by entry.S
  45. * to the lowcore fields user_timer, system_timer & steal_clock.
  46. */
  47. static void do_account_vtime(struct task_struct *tsk, int hardirq_offset)
  48. {
  49. struct thread_info *ti = task_thread_info(tsk);
  50. __u64 timer, clock, user, system, steal;
  51. timer = S390_lowcore.last_update_timer;
  52. clock = S390_lowcore.last_update_clock;
  53. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  54. " STCK %1" /* Store current tod clock value */
  55. : "=m" (S390_lowcore.last_update_timer),
  56. "=m" (S390_lowcore.last_update_clock) );
  57. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  58. S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
  59. user = S390_lowcore.user_timer - ti->user_timer;
  60. S390_lowcore.steal_timer -= user;
  61. ti->user_timer = S390_lowcore.user_timer;
  62. account_user_time(tsk, user, user);
  63. system = S390_lowcore.system_timer - ti->system_timer;
  64. S390_lowcore.steal_timer -= system;
  65. ti->system_timer = S390_lowcore.system_timer;
  66. account_system_time(tsk, hardirq_offset, system, system);
  67. steal = S390_lowcore.steal_timer;
  68. if ((s64) steal > 0) {
  69. S390_lowcore.steal_timer = 0;
  70. account_steal_time(steal);
  71. }
  72. }
  73. void account_vtime(struct task_struct *prev, struct task_struct *next)
  74. {
  75. struct thread_info *ti;
  76. do_account_vtime(prev, 0);
  77. ti = task_thread_info(prev);
  78. ti->user_timer = S390_lowcore.user_timer;
  79. ti->system_timer = S390_lowcore.system_timer;
  80. ti = task_thread_info(next);
  81. S390_lowcore.user_timer = ti->user_timer;
  82. S390_lowcore.system_timer = ti->system_timer;
  83. }
  84. void account_process_tick(struct task_struct *tsk, int user_tick)
  85. {
  86. do_account_vtime(tsk, HARDIRQ_OFFSET);
  87. }
  88. /*
  89. * Update process times based on virtual cpu times stored by entry.S
  90. * to the lowcore fields user_timer, system_timer & steal_clock.
  91. */
  92. void account_system_vtime(struct task_struct *tsk)
  93. {
  94. struct thread_info *ti = task_thread_info(tsk);
  95. __u64 timer, system;
  96. timer = S390_lowcore.last_update_timer;
  97. S390_lowcore.last_update_timer = get_vtimer();
  98. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  99. system = S390_lowcore.system_timer - ti->system_timer;
  100. S390_lowcore.steal_timer -= system;
  101. ti->system_timer = S390_lowcore.system_timer;
  102. account_system_time(tsk, 0, system, system);
  103. }
  104. EXPORT_SYMBOL_GPL(account_system_vtime);
  105. void vtime_start_cpu(__u64 int_clock, __u64 enter_timer)
  106. {
  107. struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
  108. struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
  109. __u64 idle_time, expires;
  110. if (idle->idle_enter == 0ULL)
  111. return;
  112. /* Account time spent with enabled wait psw loaded as idle time. */
  113. idle_time = int_clock - idle->idle_enter;
  114. account_idle_time(idle_time);
  115. S390_lowcore.steal_timer +=
  116. idle->idle_enter - S390_lowcore.last_update_clock;
  117. S390_lowcore.last_update_clock = int_clock;
  118. /* Account system time spent going idle. */
  119. S390_lowcore.system_timer += S390_lowcore.last_update_timer - vq->idle;
  120. S390_lowcore.last_update_timer = enter_timer;
  121. /* Restart vtime CPU timer */
  122. if (vq->do_spt) {
  123. /* Program old expire value but first save progress. */
  124. expires = vq->idle - enter_timer;
  125. expires += get_vtimer();
  126. set_vtimer(expires);
  127. } else {
  128. /* Don't account the CPU timer delta while the cpu was idle. */
  129. vq->elapsed -= vq->idle - enter_timer;
  130. }
  131. idle->sequence++;
  132. smp_wmb();
  133. idle->idle_time += idle_time;
  134. idle->idle_enter = 0ULL;
  135. idle->idle_count++;
  136. smp_wmb();
  137. idle->sequence++;
  138. }
  139. void vtime_stop_cpu(void)
  140. {
  141. struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
  142. struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
  143. psw_t psw;
  144. /* Wait for external, I/O or machine check interrupt. */
  145. psw.mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_IO | PSW_MASK_EXT;
  146. idle->nohz_delay = 0;
  147. /* Check if the CPU timer needs to be reprogrammed. */
  148. if (vq->do_spt) {
  149. __u64 vmax = VTIMER_MAX_SLICE;
  150. /*
  151. * The inline assembly is equivalent to
  152. * vq->idle = get_cpu_timer();
  153. * set_cpu_timer(VTIMER_MAX_SLICE);
  154. * idle->idle_enter = get_clock();
  155. * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
  156. * PSW_MASK_IO | PSW_MASK_EXT);
  157. * The difference is that the inline assembly makes sure that
  158. * the last three instruction are stpt, stck and lpsw in that
  159. * order. This is done to increase the precision.
  160. */
  161. asm volatile(
  162. #ifndef CONFIG_64BIT
  163. " basr 1,0\n"
  164. "0: ahi 1,1f-0b\n"
  165. " st 1,4(%2)\n"
  166. #else /* CONFIG_64BIT */
  167. " larl 1,1f\n"
  168. " stg 1,8(%2)\n"
  169. #endif /* CONFIG_64BIT */
  170. " stpt 0(%4)\n"
  171. " spt 0(%5)\n"
  172. " stck 0(%3)\n"
  173. #ifndef CONFIG_64BIT
  174. " lpsw 0(%2)\n"
  175. #else /* CONFIG_64BIT */
  176. " lpswe 0(%2)\n"
  177. #endif /* CONFIG_64BIT */
  178. "1:"
  179. : "=m" (idle->idle_enter), "=m" (vq->idle)
  180. : "a" (&psw), "a" (&idle->idle_enter),
  181. "a" (&vq->idle), "a" (&vmax), "m" (vmax), "m" (psw)
  182. : "memory", "cc", "1");
  183. } else {
  184. /*
  185. * The inline assembly is equivalent to
  186. * vq->idle = get_cpu_timer();
  187. * idle->idle_enter = get_clock();
  188. * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
  189. * PSW_MASK_IO | PSW_MASK_EXT);
  190. * The difference is that the inline assembly makes sure that
  191. * the last three instruction are stpt, stck and lpsw in that
  192. * order. This is done to increase the precision.
  193. */
  194. asm volatile(
  195. #ifndef CONFIG_64BIT
  196. " basr 1,0\n"
  197. "0: ahi 1,1f-0b\n"
  198. " st 1,4(%2)\n"
  199. #else /* CONFIG_64BIT */
  200. " larl 1,1f\n"
  201. " stg 1,8(%2)\n"
  202. #endif /* CONFIG_64BIT */
  203. " stpt 0(%4)\n"
  204. " stck 0(%3)\n"
  205. #ifndef CONFIG_64BIT
  206. " lpsw 0(%2)\n"
  207. #else /* CONFIG_64BIT */
  208. " lpswe 0(%2)\n"
  209. #endif /* CONFIG_64BIT */
  210. "1:"
  211. : "=m" (idle->idle_enter), "=m" (vq->idle)
  212. : "a" (&psw), "a" (&idle->idle_enter),
  213. "a" (&vq->idle), "m" (psw)
  214. : "memory", "cc", "1");
  215. }
  216. }
  217. cputime64_t s390_get_idle_time(int cpu)
  218. {
  219. struct s390_idle_data *idle;
  220. unsigned long long now, idle_time, idle_enter;
  221. unsigned int sequence;
  222. idle = &per_cpu(s390_idle, cpu);
  223. now = get_clock();
  224. repeat:
  225. sequence = idle->sequence;
  226. smp_rmb();
  227. if (sequence & 1)
  228. goto repeat;
  229. idle_time = 0;
  230. idle_enter = idle->idle_enter;
  231. if (idle_enter != 0ULL && idle_enter < now)
  232. idle_time = now - idle_enter;
  233. smp_rmb();
  234. if (idle->sequence != sequence)
  235. goto repeat;
  236. return idle_time;
  237. }
  238. /*
  239. * Sorted add to a list. List is linear searched until first bigger
  240. * element is found.
  241. */
  242. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  243. {
  244. struct vtimer_list *event;
  245. list_for_each_entry(event, head, entry) {
  246. if (event->expires > timer->expires) {
  247. list_add_tail(&timer->entry, &event->entry);
  248. return;
  249. }
  250. }
  251. list_add_tail(&timer->entry, head);
  252. }
  253. /*
  254. * Do the callback functions of expired vtimer events.
  255. * Called from within the interrupt handler.
  256. */
  257. static void do_callbacks(struct list_head *cb_list)
  258. {
  259. struct vtimer_queue *vq;
  260. struct vtimer_list *event, *tmp;
  261. if (list_empty(cb_list))
  262. return;
  263. vq = &__get_cpu_var(virt_cpu_timer);
  264. list_for_each_entry_safe(event, tmp, cb_list, entry) {
  265. list_del_init(&event->entry);
  266. (event->function)(event->data);
  267. if (event->interval) {
  268. /* Recharge interval timer */
  269. event->expires = event->interval + vq->elapsed;
  270. spin_lock(&vq->lock);
  271. list_add_sorted(event, &vq->list);
  272. spin_unlock(&vq->lock);
  273. }
  274. }
  275. }
  276. /*
  277. * Handler for the virtual CPU timer.
  278. */
  279. static void do_cpu_timer_interrupt(unsigned int ext_int_code,
  280. unsigned int param32, unsigned long param64)
  281. {
  282. struct vtimer_queue *vq;
  283. struct vtimer_list *event, *tmp;
  284. struct list_head cb_list; /* the callback queue */
  285. __u64 elapsed, next;
  286. INIT_LIST_HEAD(&cb_list);
  287. vq = &__get_cpu_var(virt_cpu_timer);
  288. /* walk timer list, fire all expired events */
  289. spin_lock(&vq->lock);
  290. elapsed = vq->elapsed + (vq->timer - S390_lowcore.async_enter_timer);
  291. BUG_ON((s64) elapsed < 0);
  292. vq->elapsed = 0;
  293. list_for_each_entry_safe(event, tmp, &vq->list, entry) {
  294. if (event->expires < elapsed)
  295. /* move expired timer to the callback queue */
  296. list_move_tail(&event->entry, &cb_list);
  297. else
  298. event->expires -= elapsed;
  299. }
  300. spin_unlock(&vq->lock);
  301. vq->do_spt = list_empty(&cb_list);
  302. do_callbacks(&cb_list);
  303. /* next event is first in list */
  304. next = VTIMER_MAX_SLICE;
  305. spin_lock(&vq->lock);
  306. if (!list_empty(&vq->list)) {
  307. event = list_first_entry(&vq->list, struct vtimer_list, entry);
  308. next = event->expires;
  309. } else
  310. vq->do_spt = 0;
  311. spin_unlock(&vq->lock);
  312. /*
  313. * To improve precision add the time spent by the
  314. * interrupt handler to the elapsed time.
  315. * Note: CPU timer counts down and we got an interrupt,
  316. * the current content is negative
  317. */
  318. elapsed = S390_lowcore.async_enter_timer - get_vtimer();
  319. set_vtimer(next - elapsed);
  320. vq->timer = next - elapsed;
  321. vq->elapsed = elapsed;
  322. }
  323. void init_virt_timer(struct vtimer_list *timer)
  324. {
  325. timer->function = NULL;
  326. INIT_LIST_HEAD(&timer->entry);
  327. }
  328. EXPORT_SYMBOL(init_virt_timer);
  329. static inline int vtimer_pending(struct vtimer_list *timer)
  330. {
  331. return (!list_empty(&timer->entry));
  332. }
  333. /*
  334. * this function should only run on the specified CPU
  335. */
  336. static void internal_add_vtimer(struct vtimer_list *timer)
  337. {
  338. struct vtimer_queue *vq;
  339. unsigned long flags;
  340. __u64 left, expires;
  341. vq = &per_cpu(virt_cpu_timer, timer->cpu);
  342. spin_lock_irqsave(&vq->lock, flags);
  343. BUG_ON(timer->cpu != smp_processor_id());
  344. if (list_empty(&vq->list)) {
  345. /* First timer on this cpu, just program it. */
  346. list_add(&timer->entry, &vq->list);
  347. set_vtimer(timer->expires);
  348. vq->timer = timer->expires;
  349. vq->elapsed = 0;
  350. } else {
  351. /* Check progress of old timers. */
  352. expires = timer->expires;
  353. left = get_vtimer();
  354. if (likely((s64) expires < (s64) left)) {
  355. /* The new timer expires before the current timer. */
  356. set_vtimer(expires);
  357. vq->elapsed += vq->timer - left;
  358. vq->timer = expires;
  359. } else {
  360. vq->elapsed += vq->timer - left;
  361. vq->timer = left;
  362. }
  363. /* Insert new timer into per cpu list. */
  364. timer->expires += vq->elapsed;
  365. list_add_sorted(timer, &vq->list);
  366. }
  367. spin_unlock_irqrestore(&vq->lock, flags);
  368. /* release CPU acquired in prepare_vtimer or mod_virt_timer() */
  369. put_cpu();
  370. }
  371. static inline void prepare_vtimer(struct vtimer_list *timer)
  372. {
  373. BUG_ON(!timer->function);
  374. BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
  375. BUG_ON(vtimer_pending(timer));
  376. timer->cpu = get_cpu();
  377. }
  378. /*
  379. * add_virt_timer - add an oneshot virtual CPU timer
  380. */
  381. void add_virt_timer(void *new)
  382. {
  383. struct vtimer_list *timer;
  384. timer = (struct vtimer_list *)new;
  385. prepare_vtimer(timer);
  386. timer->interval = 0;
  387. internal_add_vtimer(timer);
  388. }
  389. EXPORT_SYMBOL(add_virt_timer);
  390. /*
  391. * add_virt_timer_int - add an interval virtual CPU timer
  392. */
  393. void add_virt_timer_periodic(void *new)
  394. {
  395. struct vtimer_list *timer;
  396. timer = (struct vtimer_list *)new;
  397. prepare_vtimer(timer);
  398. timer->interval = timer->expires;
  399. internal_add_vtimer(timer);
  400. }
  401. EXPORT_SYMBOL(add_virt_timer_periodic);
  402. int __mod_vtimer(struct vtimer_list *timer, __u64 expires, int periodic)
  403. {
  404. struct vtimer_queue *vq;
  405. unsigned long flags;
  406. int cpu;
  407. BUG_ON(!timer->function);
  408. BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
  409. if (timer->expires == expires && vtimer_pending(timer))
  410. return 1;
  411. cpu = get_cpu();
  412. vq = &per_cpu(virt_cpu_timer, cpu);
  413. /* disable interrupts before test if timer is pending */
  414. spin_lock_irqsave(&vq->lock, flags);
  415. /* if timer isn't pending add it on the current CPU */
  416. if (!vtimer_pending(timer)) {
  417. spin_unlock_irqrestore(&vq->lock, flags);
  418. if (periodic)
  419. timer->interval = expires;
  420. else
  421. timer->interval = 0;
  422. timer->expires = expires;
  423. timer->cpu = cpu;
  424. internal_add_vtimer(timer);
  425. return 0;
  426. }
  427. /* check if we run on the right CPU */
  428. BUG_ON(timer->cpu != cpu);
  429. list_del_init(&timer->entry);
  430. timer->expires = expires;
  431. if (periodic)
  432. timer->interval = expires;
  433. /* the timer can't expire anymore so we can release the lock */
  434. spin_unlock_irqrestore(&vq->lock, flags);
  435. internal_add_vtimer(timer);
  436. return 1;
  437. }
  438. /*
  439. * If we change a pending timer the function must be called on the CPU
  440. * where the timer is running on.
  441. *
  442. * returns whether it has modified a pending timer (1) or not (0)
  443. */
  444. int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
  445. {
  446. return __mod_vtimer(timer, expires, 0);
  447. }
  448. EXPORT_SYMBOL(mod_virt_timer);
  449. /*
  450. * If we change a pending timer the function must be called on the CPU
  451. * where the timer is running on.
  452. *
  453. * returns whether it has modified a pending timer (1) or not (0)
  454. */
  455. int mod_virt_timer_periodic(struct vtimer_list *timer, __u64 expires)
  456. {
  457. return __mod_vtimer(timer, expires, 1);
  458. }
  459. EXPORT_SYMBOL(mod_virt_timer_periodic);
  460. /*
  461. * delete a virtual timer
  462. *
  463. * returns whether the deleted timer was pending (1) or not (0)
  464. */
  465. int del_virt_timer(struct vtimer_list *timer)
  466. {
  467. unsigned long flags;
  468. struct vtimer_queue *vq;
  469. /* check if timer is pending */
  470. if (!vtimer_pending(timer))
  471. return 0;
  472. vq = &per_cpu(virt_cpu_timer, timer->cpu);
  473. spin_lock_irqsave(&vq->lock, flags);
  474. /* we don't interrupt a running timer, just let it expire! */
  475. list_del_init(&timer->entry);
  476. spin_unlock_irqrestore(&vq->lock, flags);
  477. return 1;
  478. }
  479. EXPORT_SYMBOL(del_virt_timer);
  480. /*
  481. * Start the virtual CPU timer on the current CPU.
  482. */
  483. void init_cpu_vtimer(void)
  484. {
  485. struct vtimer_queue *vq;
  486. /* initialize per cpu vtimer structure */
  487. vq = &__get_cpu_var(virt_cpu_timer);
  488. INIT_LIST_HEAD(&vq->list);
  489. spin_lock_init(&vq->lock);
  490. /* enable cpu timer interrupts */
  491. __ctl_set_bit(0,10);
  492. }
  493. static int __cpuinit s390_nohz_notify(struct notifier_block *self,
  494. unsigned long action, void *hcpu)
  495. {
  496. struct s390_idle_data *idle;
  497. long cpu = (long) hcpu;
  498. idle = &per_cpu(s390_idle, cpu);
  499. switch (action) {
  500. case CPU_DYING:
  501. case CPU_DYING_FROZEN:
  502. idle->nohz_delay = 0;
  503. default:
  504. break;
  505. }
  506. return NOTIFY_OK;
  507. }
  508. void __init vtime_init(void)
  509. {
  510. /* request the cpu timer external interrupt */
  511. if (register_external_interrupt(0x1005, do_cpu_timer_interrupt))
  512. panic("Couldn't request external interrupt 0x1005");
  513. /* Enable cpu timer interrupts on the boot cpu. */
  514. init_cpu_vtimer();
  515. cpu_notifier(s390_nohz_notify, 0);
  516. }