vtime.c 15 KB

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