vtime.c 15 KB

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