vtime.c 14 KB

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