vtime.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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/cpu.h>
  25. static ext_int_info_t ext_int_info_timer;
  26. static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
  27. DEFINE_PER_CPU(struct s390_idle_data, s390_idle) = {
  28. .lock = __SPIN_LOCK_UNLOCKED(s390_idle.lock)
  29. };
  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 immediatly 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 vtime_start_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. __u64 idle_time, expires;
  112. /* Account time spent with enabled wait psw loaded as idle time. */
  113. idle_time = S390_lowcore.int_clock - idle->idle_enter;
  114. account_idle_time(idle_time);
  115. S390_lowcore.last_update_clock = S390_lowcore.int_clock;
  116. /* Account system time spent going idle. */
  117. S390_lowcore.system_timer += S390_lowcore.last_update_timer - vq->idle;
  118. S390_lowcore.last_update_timer = S390_lowcore.async_enter_timer;
  119. /* Restart vtime CPU timer */
  120. if (vq->do_spt) {
  121. /* Program old expire value but first save progress. */
  122. expires = vq->idle - S390_lowcore.async_enter_timer;
  123. expires += get_vtimer();
  124. set_vtimer(expires);
  125. } else {
  126. /* Don't account the CPU timer delta while the cpu was idle. */
  127. vq->elapsed -= vq->idle - S390_lowcore.async_enter_timer;
  128. }
  129. spin_lock(&idle->lock);
  130. idle->idle_time += idle_time;
  131. idle->idle_enter = 0ULL;
  132. idle->idle_count++;
  133. spin_unlock(&idle->lock);
  134. }
  135. void vtime_stop_cpu(void)
  136. {
  137. struct s390_idle_data *idle = &__get_cpu_var(s390_idle);
  138. struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
  139. psw_t psw;
  140. /* Wait for external, I/O or machine check interrupt. */
  141. psw.mask = psw_kernel_bits | PSW_MASK_WAIT | PSW_MASK_IO | PSW_MASK_EXT;
  142. /* Check if the CPU timer needs to be reprogrammed. */
  143. if (vq->do_spt) {
  144. __u64 vmax = VTIMER_MAX_SLICE;
  145. /*
  146. * The inline assembly is equivalent to
  147. * vq->idle = get_cpu_timer();
  148. * set_cpu_timer(VTIMER_MAX_SLICE);
  149. * idle->idle_enter = get_clock();
  150. * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
  151. * PSW_MASK_IO | PSW_MASK_EXT);
  152. * The difference is that the inline assembly makes sure that
  153. * the last three instruction are stpt, stck and lpsw in that
  154. * order. This is done to increase the precision.
  155. */
  156. asm volatile(
  157. #ifndef CONFIG_64BIT
  158. " basr 1,0\n"
  159. "0: ahi 1,1f-0b\n"
  160. " st 1,4(%2)\n"
  161. #else /* CONFIG_64BIT */
  162. " larl 1,1f\n"
  163. " stg 1,8(%2)\n"
  164. #endif /* CONFIG_64BIT */
  165. " stpt 0(%4)\n"
  166. " spt 0(%5)\n"
  167. " stck 0(%3)\n"
  168. #ifndef CONFIG_64BIT
  169. " lpsw 0(%2)\n"
  170. #else /* CONFIG_64BIT */
  171. " lpswe 0(%2)\n"
  172. #endif /* CONFIG_64BIT */
  173. "1:"
  174. : "=m" (idle->idle_enter), "=m" (vq->idle)
  175. : "a" (&psw), "a" (&idle->idle_enter),
  176. "a" (&vq->idle), "a" (&vmax), "m" (vmax), "m" (psw)
  177. : "memory", "cc", "1");
  178. } else {
  179. /*
  180. * The inline assembly is equivalent to
  181. * vq->idle = get_cpu_timer();
  182. * idle->idle_enter = get_clock();
  183. * __load_psw_mask(psw_kernel_bits | PSW_MASK_WAIT |
  184. * PSW_MASK_IO | PSW_MASK_EXT);
  185. * The difference is that the inline assembly makes sure that
  186. * the last three instruction are stpt, stck and lpsw in that
  187. * order. This is done to increase the precision.
  188. */
  189. asm volatile(
  190. #ifndef CONFIG_64BIT
  191. " basr 1,0\n"
  192. "0: ahi 1,1f-0b\n"
  193. " st 1,4(%2)\n"
  194. #else /* CONFIG_64BIT */
  195. " larl 1,1f\n"
  196. " stg 1,8(%2)\n"
  197. #endif /* CONFIG_64BIT */
  198. " stpt 0(%4)\n"
  199. " stck 0(%3)\n"
  200. #ifndef CONFIG_64BIT
  201. " lpsw 0(%2)\n"
  202. #else /* CONFIG_64BIT */
  203. " lpswe 0(%2)\n"
  204. #endif /* CONFIG_64BIT */
  205. "1:"
  206. : "=m" (idle->idle_enter), "=m" (vq->idle)
  207. : "a" (&psw), "a" (&idle->idle_enter),
  208. "a" (&vq->idle), "m" (psw)
  209. : "memory", "cc", "1");
  210. }
  211. }
  212. /*
  213. * Sorted add to a list. List is linear searched until first bigger
  214. * element is found.
  215. */
  216. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  217. {
  218. struct vtimer_list *event;
  219. list_for_each_entry(event, head, entry) {
  220. if (event->expires > timer->expires) {
  221. list_add_tail(&timer->entry, &event->entry);
  222. return;
  223. }
  224. }
  225. list_add_tail(&timer->entry, head);
  226. }
  227. /*
  228. * Do the callback functions of expired vtimer events.
  229. * Called from within the interrupt handler.
  230. */
  231. static void do_callbacks(struct list_head *cb_list)
  232. {
  233. struct vtimer_queue *vq;
  234. struct vtimer_list *event, *tmp;
  235. if (list_empty(cb_list))
  236. return;
  237. vq = &__get_cpu_var(virt_cpu_timer);
  238. list_for_each_entry_safe(event, tmp, cb_list, entry) {
  239. list_del_init(&event->entry);
  240. (event->function)(event->data);
  241. if (event->interval) {
  242. /* Recharge interval timer */
  243. event->expires = event->interval + vq->elapsed;
  244. spin_lock(&vq->lock);
  245. list_add_sorted(event, &vq->list);
  246. spin_unlock(&vq->lock);
  247. }
  248. }
  249. }
  250. /*
  251. * Handler for the virtual CPU timer.
  252. */
  253. static void do_cpu_timer_interrupt(__u16 error_code)
  254. {
  255. struct vtimer_queue *vq;
  256. struct vtimer_list *event, *tmp;
  257. struct list_head cb_list; /* the callback queue */
  258. __u64 elapsed, next;
  259. INIT_LIST_HEAD(&cb_list);
  260. vq = &__get_cpu_var(virt_cpu_timer);
  261. /* walk timer list, fire all expired events */
  262. spin_lock(&vq->lock);
  263. elapsed = vq->elapsed + (vq->timer - S390_lowcore.async_enter_timer);
  264. BUG_ON((s64) elapsed < 0);
  265. vq->elapsed = 0;
  266. list_for_each_entry_safe(event, tmp, &vq->list, entry) {
  267. if (event->expires < elapsed)
  268. /* move expired timer to the callback queue */
  269. list_move_tail(&event->entry, &cb_list);
  270. else
  271. event->expires -= elapsed;
  272. }
  273. spin_unlock(&vq->lock);
  274. vq->do_spt = list_empty(&cb_list);
  275. do_callbacks(&cb_list);
  276. /* next event is first in list */
  277. next = VTIMER_MAX_SLICE;
  278. spin_lock(&vq->lock);
  279. if (!list_empty(&vq->list)) {
  280. event = list_first_entry(&vq->list, struct vtimer_list, entry);
  281. next = event->expires;
  282. } else
  283. vq->do_spt = 0;
  284. spin_unlock(&vq->lock);
  285. /*
  286. * To improve precision add the time spent by the
  287. * interrupt handler to the elapsed time.
  288. * Note: CPU timer counts down and we got an interrupt,
  289. * the current content is negative
  290. */
  291. elapsed = S390_lowcore.async_enter_timer - get_vtimer();
  292. set_vtimer(next - elapsed);
  293. vq->timer = next - elapsed;
  294. vq->elapsed = elapsed;
  295. }
  296. void init_virt_timer(struct vtimer_list *timer)
  297. {
  298. timer->function = NULL;
  299. INIT_LIST_HEAD(&timer->entry);
  300. }
  301. EXPORT_SYMBOL(init_virt_timer);
  302. static inline int vtimer_pending(struct vtimer_list *timer)
  303. {
  304. return (!list_empty(&timer->entry));
  305. }
  306. /*
  307. * this function should only run on the specified CPU
  308. */
  309. static void internal_add_vtimer(struct vtimer_list *timer)
  310. {
  311. struct vtimer_queue *vq;
  312. unsigned long flags;
  313. __u64 left, expires;
  314. vq = &per_cpu(virt_cpu_timer, timer->cpu);
  315. spin_lock_irqsave(&vq->lock, flags);
  316. BUG_ON(timer->cpu != smp_processor_id());
  317. if (list_empty(&vq->list)) {
  318. /* First timer on this cpu, just program it. */
  319. list_add(&timer->entry, &vq->list);
  320. set_vtimer(timer->expires);
  321. vq->timer = timer->expires;
  322. vq->elapsed = 0;
  323. } else {
  324. /* Check progress of old timers. */
  325. expires = timer->expires;
  326. left = get_vtimer();
  327. if (likely((s64) expires < (s64) left)) {
  328. /* The new timer expires before the current timer. */
  329. set_vtimer(expires);
  330. vq->elapsed += vq->timer - left;
  331. vq->timer = expires;
  332. } else {
  333. vq->elapsed += vq->timer - left;
  334. vq->timer = left;
  335. }
  336. /* Insert new timer into per cpu list. */
  337. timer->expires += vq->elapsed;
  338. list_add_sorted(timer, &vq->list);
  339. }
  340. spin_unlock_irqrestore(&vq->lock, flags);
  341. /* release CPU acquired in prepare_vtimer or mod_virt_timer() */
  342. put_cpu();
  343. }
  344. static inline void prepare_vtimer(struct vtimer_list *timer)
  345. {
  346. BUG_ON(!timer->function);
  347. BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
  348. BUG_ON(vtimer_pending(timer));
  349. timer->cpu = get_cpu();
  350. }
  351. /*
  352. * add_virt_timer - add an oneshot virtual CPU timer
  353. */
  354. void add_virt_timer(void *new)
  355. {
  356. struct vtimer_list *timer;
  357. timer = (struct vtimer_list *)new;
  358. prepare_vtimer(timer);
  359. timer->interval = 0;
  360. internal_add_vtimer(timer);
  361. }
  362. EXPORT_SYMBOL(add_virt_timer);
  363. /*
  364. * add_virt_timer_int - add an interval virtual CPU timer
  365. */
  366. void add_virt_timer_periodic(void *new)
  367. {
  368. struct vtimer_list *timer;
  369. timer = (struct vtimer_list *)new;
  370. prepare_vtimer(timer);
  371. timer->interval = timer->expires;
  372. internal_add_vtimer(timer);
  373. }
  374. EXPORT_SYMBOL(add_virt_timer_periodic);
  375. /*
  376. * If we change a pending timer the function must be called on the CPU
  377. * where the timer is running on, e.g. by smp_call_function_single()
  378. *
  379. * The original mod_timer adds the timer if it is not pending. For
  380. * compatibility we do the same. The timer will be added on the current
  381. * CPU as a oneshot timer.
  382. *
  383. * returns whether it has modified a pending timer (1) or not (0)
  384. */
  385. int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
  386. {
  387. struct vtimer_queue *vq;
  388. unsigned long flags;
  389. int cpu;
  390. BUG_ON(!timer->function);
  391. BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
  392. /*
  393. * This is a common optimization triggered by the
  394. * networking code - if the timer is re-modified
  395. * to be the same thing then just return:
  396. */
  397. if (timer->expires == expires && vtimer_pending(timer))
  398. return 1;
  399. cpu = get_cpu();
  400. vq = &per_cpu(virt_cpu_timer, cpu);
  401. /* check if we run on the right CPU */
  402. BUG_ON(timer->cpu != cpu);
  403. /* disable interrupts before test if timer is pending */
  404. spin_lock_irqsave(&vq->lock, flags);
  405. /* if timer isn't pending add it on the current CPU */
  406. if (!vtimer_pending(timer)) {
  407. spin_unlock_irqrestore(&vq->lock, flags);
  408. /* we do not activate an interval timer with mod_virt_timer */
  409. timer->interval = 0;
  410. timer->expires = expires;
  411. timer->cpu = cpu;
  412. internal_add_vtimer(timer);
  413. return 0;
  414. }
  415. list_del_init(&timer->entry);
  416. timer->expires = expires;
  417. /* also change the interval if we have an interval timer */
  418. if (timer->interval)
  419. timer->interval = expires;
  420. /* the timer can't expire anymore so we can release the lock */
  421. spin_unlock_irqrestore(&vq->lock, flags);
  422. internal_add_vtimer(timer);
  423. return 1;
  424. }
  425. EXPORT_SYMBOL(mod_virt_timer);
  426. /*
  427. * delete a virtual timer
  428. *
  429. * returns whether the deleted timer was pending (1) or not (0)
  430. */
  431. int del_virt_timer(struct vtimer_list *timer)
  432. {
  433. unsigned long flags;
  434. struct vtimer_queue *vq;
  435. /* check if timer is pending */
  436. if (!vtimer_pending(timer))
  437. return 0;
  438. vq = &per_cpu(virt_cpu_timer, timer->cpu);
  439. spin_lock_irqsave(&vq->lock, flags);
  440. /* we don't interrupt a running timer, just let it expire! */
  441. list_del_init(&timer->entry);
  442. spin_unlock_irqrestore(&vq->lock, flags);
  443. return 1;
  444. }
  445. EXPORT_SYMBOL(del_virt_timer);
  446. /*
  447. * Start the virtual CPU timer on the current CPU.
  448. */
  449. void init_cpu_vtimer(void)
  450. {
  451. struct thread_info *ti = current_thread_info();
  452. struct vtimer_queue *vq;
  453. S390_lowcore.user_timer = ti->user_timer;
  454. S390_lowcore.system_timer = ti->system_timer;
  455. /* kick the virtual timer */
  456. asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
  457. asm volatile ("STPT %0" : "=m" (S390_lowcore.last_update_timer));
  458. /* initialize per cpu vtimer structure */
  459. vq = &__get_cpu_var(virt_cpu_timer);
  460. INIT_LIST_HEAD(&vq->list);
  461. spin_lock_init(&vq->lock);
  462. /* enable cpu timer interrupts */
  463. __ctl_set_bit(0,10);
  464. }
  465. void __init vtime_init(void)
  466. {
  467. /* request the cpu timer external interrupt */
  468. if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
  469. &ext_int_info_timer) != 0)
  470. panic("Couldn't request external interrupt 0x1005");
  471. /* Enable cpu timer interrupts on the boot cpu. */
  472. init_cpu_vtimer();
  473. }