vtime.c 14 KB

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