vtime.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. static ext_int_info_t ext_int_info_timer;
  25. static DEFINE_PER_CPU(struct vtimer_queue, virt_cpu_timer);
  26. /*
  27. * Update process times based on virtual cpu times stored by entry.S
  28. * to the lowcore fields user_timer, system_timer & steal_clock.
  29. */
  30. static void do_account_vtime(struct task_struct *tsk, int hardirq_offset)
  31. {
  32. struct thread_info *ti = task_thread_info(tsk);
  33. __u64 timer, clock, user, system, steal;
  34. timer = S390_lowcore.last_update_timer;
  35. clock = S390_lowcore.last_update_clock;
  36. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  37. " STCK %1" /* Store current tod clock value */
  38. : "=m" (S390_lowcore.last_update_timer),
  39. "=m" (S390_lowcore.last_update_clock) );
  40. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  41. S390_lowcore.steal_timer += S390_lowcore.last_update_clock - clock;
  42. user = S390_lowcore.user_timer - ti->user_timer;
  43. S390_lowcore.steal_timer -= user;
  44. ti->user_timer = S390_lowcore.user_timer;
  45. account_user_time(tsk, user, user);
  46. system = S390_lowcore.system_timer - ti->system_timer;
  47. S390_lowcore.steal_timer -= system;
  48. ti->system_timer = S390_lowcore.system_timer;
  49. if (idle_task(smp_processor_id()) != current)
  50. account_system_time(tsk, hardirq_offset, system, system);
  51. else
  52. account_idle_time(system);
  53. steal = S390_lowcore.steal_timer;
  54. if ((s64) steal > 0) {
  55. S390_lowcore.steal_timer = 0;
  56. if (idle_task(smp_processor_id()) != current)
  57. account_steal_time(steal);
  58. else
  59. account_idle_time(steal);
  60. }
  61. }
  62. void account_vtime(struct task_struct *prev, struct task_struct *next)
  63. {
  64. struct thread_info *ti;
  65. do_account_vtime(prev, 0);
  66. ti = task_thread_info(prev);
  67. ti->user_timer = S390_lowcore.user_timer;
  68. ti->system_timer = S390_lowcore.system_timer;
  69. ti = task_thread_info(next);
  70. S390_lowcore.user_timer = ti->user_timer;
  71. S390_lowcore.system_timer = ti->system_timer;
  72. }
  73. void account_process_tick(struct task_struct *tsk, int user_tick)
  74. {
  75. do_account_vtime(tsk, HARDIRQ_OFFSET);
  76. }
  77. /*
  78. * Update process times based on virtual cpu times stored by entry.S
  79. * to the lowcore fields user_timer, system_timer & steal_clock.
  80. */
  81. void account_system_vtime(struct task_struct *tsk)
  82. {
  83. struct thread_info *ti = task_thread_info(tsk);
  84. __u64 timer, system;
  85. timer = S390_lowcore.last_update_timer;
  86. asm volatile (" STPT %0" /* Store current cpu timer value */
  87. : "=m" (S390_lowcore.last_update_timer) );
  88. S390_lowcore.system_timer += timer - S390_lowcore.last_update_timer;
  89. system = S390_lowcore.system_timer - ti->system_timer;
  90. S390_lowcore.steal_timer -= system;
  91. ti->system_timer = S390_lowcore.system_timer;
  92. if (in_irq() || idle_task(smp_processor_id()) != current)
  93. account_system_time(tsk, 0, system, system);
  94. else
  95. account_idle_time(system);
  96. }
  97. EXPORT_SYMBOL_GPL(account_system_vtime);
  98. static inline void set_vtimer(__u64 expires)
  99. {
  100. struct vtimer_queue *vq = &__get_cpu_var(virt_cpu_timer);
  101. __u64 timer;
  102. asm volatile (" STPT %0\n" /* Store current cpu timer value */
  103. " SPT %1" /* Set new value immediatly afterwards */
  104. : "=m" (timer) : "m" (expires) );
  105. S390_lowcore.system_timer += S390_lowcore.last_update_timer - timer;
  106. S390_lowcore.last_update_timer = expires;
  107. /* store expire time for this CPU timer */
  108. vq->to_expire = expires;
  109. }
  110. void vtime_start_cpu_timer(void)
  111. {
  112. struct vtimer_queue *vt_list;
  113. vt_list = &__get_cpu_var(virt_cpu_timer);
  114. /* CPU timer interrupt is pending, don't reprogramm it */
  115. if (vt_list->idle & 1LL<<63)
  116. return;
  117. if (!list_empty(&vt_list->list))
  118. set_vtimer(vt_list->idle);
  119. }
  120. void vtime_stop_cpu_timer(void)
  121. {
  122. struct vtimer_queue *vt_list;
  123. vt_list = &__get_cpu_var(virt_cpu_timer);
  124. /* nothing to do */
  125. if (list_empty(&vt_list->list)) {
  126. vt_list->idle = VTIMER_MAX_SLICE;
  127. goto fire;
  128. }
  129. /* store the actual expire value */
  130. asm volatile ("STPT %0" : "=m" (vt_list->idle));
  131. /*
  132. * If the CPU timer is negative we don't reprogramm
  133. * it because we will get instantly an interrupt.
  134. */
  135. if (vt_list->idle & 1LL<<63)
  136. return;
  137. vt_list->offset += vt_list->to_expire - vt_list->idle;
  138. /*
  139. * We cannot halt the CPU timer, we just write a value that
  140. * nearly never expires (only after 71 years) and re-write
  141. * the stored expire value if we continue the timer
  142. */
  143. fire:
  144. set_vtimer(VTIMER_MAX_SLICE);
  145. }
  146. /*
  147. * Sorted add to a list. List is linear searched until first bigger
  148. * element is found.
  149. */
  150. static void list_add_sorted(struct vtimer_list *timer, struct list_head *head)
  151. {
  152. struct vtimer_list *event;
  153. list_for_each_entry(event, head, entry) {
  154. if (event->expires > timer->expires) {
  155. list_add_tail(&timer->entry, &event->entry);
  156. return;
  157. }
  158. }
  159. list_add_tail(&timer->entry, head);
  160. }
  161. /*
  162. * Do the callback functions of expired vtimer events.
  163. * Called from within the interrupt handler.
  164. */
  165. static void do_callbacks(struct list_head *cb_list)
  166. {
  167. struct vtimer_queue *vt_list;
  168. struct vtimer_list *event, *tmp;
  169. void (*fn)(unsigned long);
  170. unsigned long data;
  171. if (list_empty(cb_list))
  172. return;
  173. vt_list = &__get_cpu_var(virt_cpu_timer);
  174. list_for_each_entry_safe(event, tmp, cb_list, entry) {
  175. fn = event->function;
  176. data = event->data;
  177. fn(data);
  178. if (!event->interval)
  179. /* delete one shot timer */
  180. list_del_init(&event->entry);
  181. else {
  182. /* move interval timer back to list */
  183. spin_lock(&vt_list->lock);
  184. list_del_init(&event->entry);
  185. list_add_sorted(event, &vt_list->list);
  186. spin_unlock(&vt_list->lock);
  187. }
  188. }
  189. }
  190. /*
  191. * Handler for the virtual CPU timer.
  192. */
  193. static void do_cpu_timer_interrupt(__u16 error_code)
  194. {
  195. __u64 next, delta;
  196. struct vtimer_queue *vt_list;
  197. struct vtimer_list *event, *tmp;
  198. struct list_head *ptr;
  199. /* the callback queue */
  200. struct list_head cb_list;
  201. INIT_LIST_HEAD(&cb_list);
  202. vt_list = &__get_cpu_var(virt_cpu_timer);
  203. /* walk timer list, fire all expired events */
  204. spin_lock(&vt_list->lock);
  205. if (vt_list->to_expire < VTIMER_MAX_SLICE)
  206. vt_list->offset += vt_list->to_expire;
  207. list_for_each_entry_safe(event, tmp, &vt_list->list, entry) {
  208. if (event->expires > vt_list->offset)
  209. /* found first unexpired event, leave */
  210. break;
  211. /* re-charge interval timer, we have to add the offset */
  212. if (event->interval)
  213. event->expires = event->interval + vt_list->offset;
  214. /* move expired timer to the callback queue */
  215. list_move_tail(&event->entry, &cb_list);
  216. }
  217. spin_unlock(&vt_list->lock);
  218. do_callbacks(&cb_list);
  219. /* next event is first in list */
  220. spin_lock(&vt_list->lock);
  221. if (!list_empty(&vt_list->list)) {
  222. ptr = vt_list->list.next;
  223. event = list_entry(ptr, struct vtimer_list, entry);
  224. next = event->expires - vt_list->offset;
  225. /* add the expired time from this interrupt handler
  226. * and the callback functions
  227. */
  228. asm volatile ("STPT %0" : "=m" (delta));
  229. delta = 0xffffffffffffffffLL - delta + 1;
  230. vt_list->offset += delta;
  231. next -= delta;
  232. } else {
  233. vt_list->offset = 0;
  234. next = VTIMER_MAX_SLICE;
  235. }
  236. spin_unlock(&vt_list->lock);
  237. set_vtimer(next);
  238. }
  239. void init_virt_timer(struct vtimer_list *timer)
  240. {
  241. timer->function = NULL;
  242. INIT_LIST_HEAD(&timer->entry);
  243. spin_lock_init(&timer->lock);
  244. }
  245. EXPORT_SYMBOL(init_virt_timer);
  246. static inline int vtimer_pending(struct vtimer_list *timer)
  247. {
  248. return (!list_empty(&timer->entry));
  249. }
  250. /*
  251. * this function should only run on the specified CPU
  252. */
  253. static void internal_add_vtimer(struct vtimer_list *timer)
  254. {
  255. unsigned long flags;
  256. __u64 done;
  257. struct vtimer_list *event;
  258. struct vtimer_queue *vt_list;
  259. vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
  260. spin_lock_irqsave(&vt_list->lock, flags);
  261. BUG_ON(timer->cpu != smp_processor_id());
  262. /* if list is empty we only have to set the timer */
  263. if (list_empty(&vt_list->list)) {
  264. /* reset the offset, this may happen if the last timer was
  265. * just deleted by mod_virt_timer and the interrupt
  266. * didn't happen until here
  267. */
  268. vt_list->offset = 0;
  269. goto fire;
  270. }
  271. /* save progress */
  272. asm volatile ("STPT %0" : "=m" (done));
  273. /* calculate completed work */
  274. done = vt_list->to_expire - done + vt_list->offset;
  275. vt_list->offset = 0;
  276. list_for_each_entry(event, &vt_list->list, entry)
  277. event->expires -= done;
  278. fire:
  279. list_add_sorted(timer, &vt_list->list);
  280. /* get first element, which is the next vtimer slice */
  281. event = list_entry(vt_list->list.next, struct vtimer_list, entry);
  282. set_vtimer(event->expires);
  283. spin_unlock_irqrestore(&vt_list->lock, flags);
  284. /* release CPU acquired in prepare_vtimer or mod_virt_timer() */
  285. put_cpu();
  286. }
  287. static inline void prepare_vtimer(struct vtimer_list *timer)
  288. {
  289. BUG_ON(!timer->function);
  290. BUG_ON(!timer->expires || timer->expires > VTIMER_MAX_SLICE);
  291. BUG_ON(vtimer_pending(timer));
  292. timer->cpu = get_cpu();
  293. }
  294. /*
  295. * add_virt_timer - add an oneshot virtual CPU timer
  296. */
  297. void add_virt_timer(void *new)
  298. {
  299. struct vtimer_list *timer;
  300. timer = (struct vtimer_list *)new;
  301. prepare_vtimer(timer);
  302. timer->interval = 0;
  303. internal_add_vtimer(timer);
  304. }
  305. EXPORT_SYMBOL(add_virt_timer);
  306. /*
  307. * add_virt_timer_int - add an interval virtual CPU timer
  308. */
  309. void add_virt_timer_periodic(void *new)
  310. {
  311. struct vtimer_list *timer;
  312. timer = (struct vtimer_list *)new;
  313. prepare_vtimer(timer);
  314. timer->interval = timer->expires;
  315. internal_add_vtimer(timer);
  316. }
  317. EXPORT_SYMBOL(add_virt_timer_periodic);
  318. /*
  319. * If we change a pending timer the function must be called on the CPU
  320. * where the timer is running on, e.g. by smp_call_function_single()
  321. *
  322. * The original mod_timer adds the timer if it is not pending. For compatibility
  323. * we do the same. The timer will be added on the current CPU as a oneshot timer.
  324. *
  325. * returns whether it has modified a pending timer (1) or not (0)
  326. */
  327. int mod_virt_timer(struct vtimer_list *timer, __u64 expires)
  328. {
  329. struct vtimer_queue *vt_list;
  330. unsigned long flags;
  331. int cpu;
  332. BUG_ON(!timer->function);
  333. BUG_ON(!expires || expires > VTIMER_MAX_SLICE);
  334. /*
  335. * This is a common optimization triggered by the
  336. * networking code - if the timer is re-modified
  337. * to be the same thing then just return:
  338. */
  339. if (timer->expires == expires && vtimer_pending(timer))
  340. return 1;
  341. cpu = get_cpu();
  342. vt_list = &per_cpu(virt_cpu_timer, cpu);
  343. /* check if we run on the right CPU */
  344. BUG_ON(timer->cpu != cpu);
  345. /* disable interrupts before test if timer is pending */
  346. spin_lock_irqsave(&vt_list->lock, flags);
  347. /* if timer isn't pending add it on the current CPU */
  348. if (!vtimer_pending(timer)) {
  349. spin_unlock_irqrestore(&vt_list->lock, flags);
  350. /* we do not activate an interval timer with mod_virt_timer */
  351. timer->interval = 0;
  352. timer->expires = expires;
  353. timer->cpu = cpu;
  354. internal_add_vtimer(timer);
  355. return 0;
  356. }
  357. list_del_init(&timer->entry);
  358. timer->expires = expires;
  359. /* also change the interval if we have an interval timer */
  360. if (timer->interval)
  361. timer->interval = expires;
  362. /* the timer can't expire anymore so we can release the lock */
  363. spin_unlock_irqrestore(&vt_list->lock, flags);
  364. internal_add_vtimer(timer);
  365. return 1;
  366. }
  367. EXPORT_SYMBOL(mod_virt_timer);
  368. /*
  369. * delete a virtual timer
  370. *
  371. * returns whether the deleted timer was pending (1) or not (0)
  372. */
  373. int del_virt_timer(struct vtimer_list *timer)
  374. {
  375. unsigned long flags;
  376. struct vtimer_queue *vt_list;
  377. /* check if timer is pending */
  378. if (!vtimer_pending(timer))
  379. return 0;
  380. vt_list = &per_cpu(virt_cpu_timer, timer->cpu);
  381. spin_lock_irqsave(&vt_list->lock, flags);
  382. /* we don't interrupt a running timer, just let it expire! */
  383. list_del_init(&timer->entry);
  384. /* last timer removed */
  385. if (list_empty(&vt_list->list)) {
  386. vt_list->to_expire = 0;
  387. vt_list->offset = 0;
  388. }
  389. spin_unlock_irqrestore(&vt_list->lock, flags);
  390. return 1;
  391. }
  392. EXPORT_SYMBOL(del_virt_timer);
  393. /*
  394. * Start the virtual CPU timer on the current CPU.
  395. */
  396. void init_cpu_vtimer(void)
  397. {
  398. struct vtimer_queue *vt_list;
  399. /* kick the virtual timer */
  400. S390_lowcore.exit_timer = VTIMER_MAX_SLICE;
  401. S390_lowcore.last_update_timer = VTIMER_MAX_SLICE;
  402. asm volatile ("STCK %0" : "=m" (S390_lowcore.last_update_clock));
  403. asm volatile ("SPT %0" : : "m" (S390_lowcore.last_update_timer));
  404. /* enable cpu timer interrupts */
  405. __ctl_set_bit(0,10);
  406. vt_list = &__get_cpu_var(virt_cpu_timer);
  407. INIT_LIST_HEAD(&vt_list->list);
  408. spin_lock_init(&vt_list->lock);
  409. vt_list->to_expire = 0;
  410. vt_list->offset = 0;
  411. vt_list->idle = 0;
  412. }
  413. void __init vtime_init(void)
  414. {
  415. /* request the cpu timer external interrupt */
  416. if (register_early_external_interrupt(0x1005, do_cpu_timer_interrupt,
  417. &ext_int_info_timer) != 0)
  418. panic("Couldn't request external interrupt 0x1005");
  419. /* Enable cpu timer interrupts on the boot cpu. */
  420. init_cpu_vtimer();
  421. }