vtime.c 13 KB

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