vtime.c 13 KB

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