alarmtimer.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. /*
  2. * Alarmtimer interface
  3. *
  4. * This interface provides a timer which is similarto hrtimers,
  5. * but triggers a RTC alarm if the box is suspend.
  6. *
  7. * This interface is influenced by the Android RTC Alarm timer
  8. * interface.
  9. *
  10. * Copyright (C) 2010 IBM Corperation
  11. *
  12. * Author: John Stultz <john.stultz@linaro.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/time.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/timerqueue.h>
  21. #include <linux/rtc.h>
  22. #include <linux/alarmtimer.h>
  23. #include <linux/mutex.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/freezer.h>
  28. /**
  29. * struct alarm_base - Alarm timer bases
  30. * @lock: Lock for syncrhonized access to the base
  31. * @timerqueue: Timerqueue head managing the list of events
  32. * @timer: hrtimer used to schedule events while running
  33. * @gettime: Function to read the time correlating to the base
  34. * @base_clockid: clockid for the base
  35. */
  36. static struct alarm_base {
  37. spinlock_t lock;
  38. struct timerqueue_head timerqueue;
  39. ktime_t (*gettime)(void);
  40. clockid_t base_clockid;
  41. } alarm_bases[ALARM_NUMTYPE];
  42. /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
  43. static ktime_t freezer_delta;
  44. static DEFINE_SPINLOCK(freezer_delta_lock);
  45. static struct wakeup_source *ws;
  46. #ifdef CONFIG_RTC_CLASS
  47. /* rtc timer and device for setting alarm wakeups at suspend */
  48. static struct rtc_timer rtctimer;
  49. static struct rtc_device *rtcdev;
  50. static DEFINE_SPINLOCK(rtcdev_lock);
  51. /**
  52. * alarmtimer_get_rtcdev - Return selected rtcdevice
  53. *
  54. * This function returns the rtc device to use for wakealarms.
  55. * If one has not already been chosen, it checks to see if a
  56. * functional rtc device is available.
  57. */
  58. struct rtc_device *alarmtimer_get_rtcdev(void)
  59. {
  60. unsigned long flags;
  61. struct rtc_device *ret;
  62. spin_lock_irqsave(&rtcdev_lock, flags);
  63. ret = rtcdev;
  64. spin_unlock_irqrestore(&rtcdev_lock, flags);
  65. return ret;
  66. }
  67. EXPORT_SYMBOL_GPL(alarmtimer_get_rtcdev);
  68. static int alarmtimer_rtc_add_device(struct device *dev,
  69. struct class_interface *class_intf)
  70. {
  71. unsigned long flags;
  72. struct rtc_device *rtc = to_rtc_device(dev);
  73. if (rtcdev)
  74. return -EBUSY;
  75. if (!rtc->ops->set_alarm)
  76. return -1;
  77. if (!device_may_wakeup(rtc->dev.parent))
  78. return -1;
  79. spin_lock_irqsave(&rtcdev_lock, flags);
  80. if (!rtcdev) {
  81. rtcdev = rtc;
  82. /* hold a reference so it doesn't go away */
  83. get_device(dev);
  84. }
  85. spin_unlock_irqrestore(&rtcdev_lock, flags);
  86. return 0;
  87. }
  88. static inline void alarmtimer_rtc_timer_init(void)
  89. {
  90. rtc_timer_init(&rtctimer, NULL, NULL);
  91. }
  92. static struct class_interface alarmtimer_rtc_interface = {
  93. .add_dev = &alarmtimer_rtc_add_device,
  94. };
  95. static int alarmtimer_rtc_interface_setup(void)
  96. {
  97. alarmtimer_rtc_interface.class = rtc_class;
  98. return class_interface_register(&alarmtimer_rtc_interface);
  99. }
  100. static void alarmtimer_rtc_interface_remove(void)
  101. {
  102. class_interface_unregister(&alarmtimer_rtc_interface);
  103. }
  104. #else
  105. struct rtc_device *alarmtimer_get_rtcdev(void)
  106. {
  107. return NULL;
  108. }
  109. #define rtcdev (NULL)
  110. static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
  111. static inline void alarmtimer_rtc_interface_remove(void) { }
  112. static inline void alarmtimer_rtc_timer_init(void) { }
  113. #endif
  114. /**
  115. * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
  116. * @base: pointer to the base where the timer is being run
  117. * @alarm: pointer to alarm being enqueued.
  118. *
  119. * Adds alarm to a alarm_base timerqueue
  120. *
  121. * Must hold base->lock when calling.
  122. */
  123. static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
  124. {
  125. if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
  126. timerqueue_del(&base->timerqueue, &alarm->node);
  127. timerqueue_add(&base->timerqueue, &alarm->node);
  128. alarm->state |= ALARMTIMER_STATE_ENQUEUED;
  129. }
  130. /**
  131. * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
  132. * @base: pointer to the base where the timer is running
  133. * @alarm: pointer to alarm being removed
  134. *
  135. * Removes alarm to a alarm_base timerqueue
  136. *
  137. * Must hold base->lock when calling.
  138. */
  139. static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
  140. {
  141. if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
  142. return;
  143. timerqueue_del(&base->timerqueue, &alarm->node);
  144. alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
  145. }
  146. /**
  147. * alarmtimer_fired - Handles alarm hrtimer being fired.
  148. * @timer: pointer to hrtimer being run
  149. *
  150. * When a alarm timer fires, this runs through the timerqueue to
  151. * see which alarms expired, and runs those. If there are more alarm
  152. * timers queued for the future, we set the hrtimer to fire when
  153. * when the next future alarm timer expires.
  154. */
  155. static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
  156. {
  157. struct alarm *alarm = container_of(timer, struct alarm, timer);
  158. struct alarm_base *base = &alarm_bases[alarm->type];
  159. unsigned long flags;
  160. int ret = HRTIMER_NORESTART;
  161. int restart = ALARMTIMER_NORESTART;
  162. spin_lock_irqsave(&base->lock, flags);
  163. alarmtimer_dequeue(base, alarm);
  164. spin_unlock_irqrestore(&base->lock, flags);
  165. if (alarm->function)
  166. restart = alarm->function(alarm, base->gettime());
  167. spin_lock_irqsave(&base->lock, flags);
  168. if (restart != ALARMTIMER_NORESTART) {
  169. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  170. alarmtimer_enqueue(base, alarm);
  171. ret = HRTIMER_RESTART;
  172. }
  173. spin_unlock_irqrestore(&base->lock, flags);
  174. return ret;
  175. }
  176. ktime_t alarm_expires_remaining(const struct alarm *alarm)
  177. {
  178. struct alarm_base *base = &alarm_bases[alarm->type];
  179. return ktime_sub(alarm->node.expires, base->gettime());
  180. }
  181. EXPORT_SYMBOL_GPL(alarm_expires_remaining);
  182. #ifdef CONFIG_RTC_CLASS
  183. /**
  184. * alarmtimer_suspend - Suspend time callback
  185. * @dev: unused
  186. * @state: unused
  187. *
  188. * When we are going into suspend, we look through the bases
  189. * to see which is the soonest timer to expire. We then
  190. * set an rtc timer to fire that far into the future, which
  191. * will wake us from suspend.
  192. */
  193. static int alarmtimer_suspend(struct device *dev)
  194. {
  195. struct rtc_time tm;
  196. ktime_t min, now;
  197. unsigned long flags;
  198. struct rtc_device *rtc;
  199. int i;
  200. int ret;
  201. spin_lock_irqsave(&freezer_delta_lock, flags);
  202. min = freezer_delta;
  203. freezer_delta = ktime_set(0, 0);
  204. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  205. rtc = alarmtimer_get_rtcdev();
  206. /* If we have no rtcdev, just return */
  207. if (!rtc)
  208. return 0;
  209. /* Find the soonest timer to expire*/
  210. for (i = 0; i < ALARM_NUMTYPE; i++) {
  211. struct alarm_base *base = &alarm_bases[i];
  212. struct timerqueue_node *next;
  213. ktime_t delta;
  214. spin_lock_irqsave(&base->lock, flags);
  215. next = timerqueue_getnext(&base->timerqueue);
  216. spin_unlock_irqrestore(&base->lock, flags);
  217. if (!next)
  218. continue;
  219. delta = ktime_sub(next->expires, base->gettime());
  220. if (!min.tv64 || (delta.tv64 < min.tv64))
  221. min = delta;
  222. }
  223. if (min.tv64 == 0)
  224. return 0;
  225. if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
  226. __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
  227. return -EBUSY;
  228. }
  229. /* Setup an rtc timer to fire that far in the future */
  230. rtc_timer_cancel(rtc, &rtctimer);
  231. rtc_read_time(rtc, &tm);
  232. now = rtc_tm_to_ktime(tm);
  233. now = ktime_add(now, min);
  234. /* Set alarm, if in the past reject suspend briefly to handle */
  235. ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
  236. if (ret < 0)
  237. __pm_wakeup_event(ws, MSEC_PER_SEC);
  238. return ret;
  239. }
  240. #else
  241. static int alarmtimer_suspend(struct device *dev)
  242. {
  243. return 0;
  244. }
  245. #endif
  246. static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
  247. {
  248. ktime_t delta;
  249. unsigned long flags;
  250. struct alarm_base *base = &alarm_bases[type];
  251. delta = ktime_sub(absexp, base->gettime());
  252. spin_lock_irqsave(&freezer_delta_lock, flags);
  253. if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
  254. freezer_delta = delta;
  255. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  256. }
  257. /**
  258. * alarm_init - Initialize an alarm structure
  259. * @alarm: ptr to alarm to be initialized
  260. * @type: the type of the alarm
  261. * @function: callback that is run when the alarm fires
  262. */
  263. void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
  264. enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
  265. {
  266. timerqueue_init(&alarm->node);
  267. hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
  268. HRTIMER_MODE_ABS);
  269. alarm->timer.function = alarmtimer_fired;
  270. alarm->function = function;
  271. alarm->type = type;
  272. alarm->state = ALARMTIMER_STATE_INACTIVE;
  273. }
  274. EXPORT_SYMBOL_GPL(alarm_init);
  275. /**
  276. * alarm_start - Sets an absolute alarm to fire
  277. * @alarm: ptr to alarm to set
  278. * @start: time to run the alarm
  279. */
  280. void alarm_start(struct alarm *alarm, ktime_t start)
  281. {
  282. struct alarm_base *base = &alarm_bases[alarm->type];
  283. unsigned long flags;
  284. spin_lock_irqsave(&base->lock, flags);
  285. alarm->node.expires = start;
  286. alarmtimer_enqueue(base, alarm);
  287. hrtimer_start(&alarm->timer, alarm->node.expires, HRTIMER_MODE_ABS);
  288. spin_unlock_irqrestore(&base->lock, flags);
  289. }
  290. EXPORT_SYMBOL_GPL(alarm_start);
  291. /**
  292. * alarm_start_relative - Sets a relative alarm to fire
  293. * @alarm: ptr to alarm to set
  294. * @start: time relative to now to run the alarm
  295. */
  296. void alarm_start_relative(struct alarm *alarm, ktime_t start)
  297. {
  298. struct alarm_base *base = &alarm_bases[alarm->type];
  299. start = ktime_add_safe(start, base->gettime());
  300. alarm_start(alarm, start);
  301. }
  302. EXPORT_SYMBOL_GPL(alarm_start_relative);
  303. void alarm_restart(struct alarm *alarm)
  304. {
  305. struct alarm_base *base = &alarm_bases[alarm->type];
  306. unsigned long flags;
  307. spin_lock_irqsave(&base->lock, flags);
  308. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  309. hrtimer_restart(&alarm->timer);
  310. alarmtimer_enqueue(base, alarm);
  311. spin_unlock_irqrestore(&base->lock, flags);
  312. }
  313. EXPORT_SYMBOL_GPL(alarm_restart);
  314. /**
  315. * alarm_try_to_cancel - Tries to cancel an alarm timer
  316. * @alarm: ptr to alarm to be canceled
  317. *
  318. * Returns 1 if the timer was canceled, 0 if it was not running,
  319. * and -1 if the callback was running
  320. */
  321. int alarm_try_to_cancel(struct alarm *alarm)
  322. {
  323. struct alarm_base *base = &alarm_bases[alarm->type];
  324. unsigned long flags;
  325. int ret;
  326. spin_lock_irqsave(&base->lock, flags);
  327. ret = hrtimer_try_to_cancel(&alarm->timer);
  328. if (ret >= 0)
  329. alarmtimer_dequeue(base, alarm);
  330. spin_unlock_irqrestore(&base->lock, flags);
  331. return ret;
  332. }
  333. EXPORT_SYMBOL_GPL(alarm_try_to_cancel);
  334. /**
  335. * alarm_cancel - Spins trying to cancel an alarm timer until it is done
  336. * @alarm: ptr to alarm to be canceled
  337. *
  338. * Returns 1 if the timer was canceled, 0 if it was not active.
  339. */
  340. int alarm_cancel(struct alarm *alarm)
  341. {
  342. for (;;) {
  343. int ret = alarm_try_to_cancel(alarm);
  344. if (ret >= 0)
  345. return ret;
  346. cpu_relax();
  347. }
  348. }
  349. EXPORT_SYMBOL_GPL(alarm_cancel);
  350. u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
  351. {
  352. u64 overrun = 1;
  353. ktime_t delta;
  354. delta = ktime_sub(now, alarm->node.expires);
  355. if (delta.tv64 < 0)
  356. return 0;
  357. if (unlikely(delta.tv64 >= interval.tv64)) {
  358. s64 incr = ktime_to_ns(interval);
  359. overrun = ktime_divns(delta, incr);
  360. alarm->node.expires = ktime_add_ns(alarm->node.expires,
  361. incr*overrun);
  362. if (alarm->node.expires.tv64 > now.tv64)
  363. return overrun;
  364. /*
  365. * This (and the ktime_add() below) is the
  366. * correction for exact:
  367. */
  368. overrun++;
  369. }
  370. alarm->node.expires = ktime_add_safe(alarm->node.expires, interval);
  371. return overrun;
  372. }
  373. EXPORT_SYMBOL_GPL(alarm_forward);
  374. u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
  375. {
  376. struct alarm_base *base = &alarm_bases[alarm->type];
  377. return alarm_forward(alarm, base->gettime(), interval);
  378. }
  379. EXPORT_SYMBOL_GPL(alarm_forward_now);
  380. /**
  381. * clock2alarm - helper that converts from clockid to alarmtypes
  382. * @clockid: clockid.
  383. */
  384. static enum alarmtimer_type clock2alarm(clockid_t clockid)
  385. {
  386. if (clockid == CLOCK_REALTIME_ALARM)
  387. return ALARM_REALTIME;
  388. if (clockid == CLOCK_BOOTTIME_ALARM)
  389. return ALARM_BOOTTIME;
  390. return -1;
  391. }
  392. /**
  393. * alarm_handle_timer - Callback for posix timers
  394. * @alarm: alarm that fired
  395. *
  396. * Posix timer callback for expired alarm timers.
  397. */
  398. static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
  399. ktime_t now)
  400. {
  401. unsigned long flags;
  402. struct k_itimer *ptr = container_of(alarm, struct k_itimer,
  403. it.alarm.alarmtimer);
  404. enum alarmtimer_restart result = ALARMTIMER_NORESTART;
  405. spin_lock_irqsave(&ptr->it_lock, flags);
  406. if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
  407. if (posix_timer_event(ptr, 0) != 0)
  408. ptr->it_overrun++;
  409. }
  410. /* Re-add periodic timers */
  411. if (ptr->it.alarm.interval.tv64) {
  412. ptr->it_overrun += alarm_forward(alarm, now,
  413. ptr->it.alarm.interval);
  414. result = ALARMTIMER_RESTART;
  415. }
  416. spin_unlock_irqrestore(&ptr->it_lock, flags);
  417. return result;
  418. }
  419. /**
  420. * alarm_clock_getres - posix getres interface
  421. * @which_clock: clockid
  422. * @tp: timespec to fill
  423. *
  424. * Returns the granularity of underlying alarm base clock
  425. */
  426. static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
  427. {
  428. if (!alarmtimer_get_rtcdev())
  429. return -EINVAL;
  430. tp->tv_sec = 0;
  431. tp->tv_nsec = hrtimer_resolution;
  432. return 0;
  433. }
  434. /**
  435. * alarm_clock_get - posix clock_get interface
  436. * @which_clock: clockid
  437. * @tp: timespec to fill.
  438. *
  439. * Provides the underlying alarm base time.
  440. */
  441. static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
  442. {
  443. struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
  444. if (!alarmtimer_get_rtcdev())
  445. return -EINVAL;
  446. *tp = ktime_to_timespec(base->gettime());
  447. return 0;
  448. }
  449. /**
  450. * alarm_timer_create - posix timer_create interface
  451. * @new_timer: k_itimer pointer to manage
  452. *
  453. * Initializes the k_itimer structure.
  454. */
  455. static int alarm_timer_create(struct k_itimer *new_timer)
  456. {
  457. enum alarmtimer_type type;
  458. struct alarm_base *base;
  459. if (!alarmtimer_get_rtcdev())
  460. return -ENOTSUPP;
  461. if (!capable(CAP_WAKE_ALARM))
  462. return -EPERM;
  463. type = clock2alarm(new_timer->it_clock);
  464. base = &alarm_bases[type];
  465. alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
  466. return 0;
  467. }
  468. /**
  469. * alarm_timer_get - posix timer_get interface
  470. * @new_timer: k_itimer pointer
  471. * @cur_setting: itimerspec data to fill
  472. *
  473. * Copies out the current itimerspec data
  474. */
  475. static void alarm_timer_get(struct k_itimer *timr,
  476. struct itimerspec *cur_setting)
  477. {
  478. ktime_t relative_expiry_time =
  479. alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
  480. if (ktime_to_ns(relative_expiry_time) > 0) {
  481. cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
  482. } else {
  483. cur_setting->it_value.tv_sec = 0;
  484. cur_setting->it_value.tv_nsec = 0;
  485. }
  486. cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
  487. }
  488. /**
  489. * alarm_timer_del - posix timer_del interface
  490. * @timr: k_itimer pointer to be deleted
  491. *
  492. * Cancels any programmed alarms for the given timer.
  493. */
  494. static int alarm_timer_del(struct k_itimer *timr)
  495. {
  496. if (!rtcdev)
  497. return -ENOTSUPP;
  498. if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
  499. return TIMER_RETRY;
  500. return 0;
  501. }
  502. /**
  503. * alarm_timer_set - posix timer_set interface
  504. * @timr: k_itimer pointer to be deleted
  505. * @flags: timer flags
  506. * @new_setting: itimerspec to be used
  507. * @old_setting: itimerspec being replaced
  508. *
  509. * Sets the timer to new_setting, and starts the timer.
  510. */
  511. static int alarm_timer_set(struct k_itimer *timr, int flags,
  512. struct itimerspec *new_setting,
  513. struct itimerspec *old_setting)
  514. {
  515. ktime_t exp;
  516. if (!rtcdev)
  517. return -ENOTSUPP;
  518. if (flags & ~TIMER_ABSTIME)
  519. return -EINVAL;
  520. if (old_setting)
  521. alarm_timer_get(timr, old_setting);
  522. /* If the timer was already set, cancel it */
  523. if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
  524. return TIMER_RETRY;
  525. /* start the timer */
  526. timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
  527. /*
  528. * Rate limit to the tick as a hot fix to prevent DOS. Will be
  529. * mopped up later.
  530. */
  531. if (timr->it.alarm.interval.tv64 &&
  532. ktime_to_ns(timr->it.alarm.interval) < TICK_NSEC)
  533. timr->it.alarm.interval = ktime_set(0, TICK_NSEC);
  534. exp = timespec_to_ktime(new_setting->it_value);
  535. /* Convert (if necessary) to absolute time */
  536. if (flags != TIMER_ABSTIME) {
  537. ktime_t now;
  538. now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
  539. exp = ktime_add_safe(now, exp);
  540. }
  541. alarm_start(&timr->it.alarm.alarmtimer, exp);
  542. return 0;
  543. }
  544. /**
  545. * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
  546. * @alarm: ptr to alarm that fired
  547. *
  548. * Wakes up the task that set the alarmtimer
  549. */
  550. static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
  551. ktime_t now)
  552. {
  553. struct task_struct *task = (struct task_struct *)alarm->data;
  554. alarm->data = NULL;
  555. if (task)
  556. wake_up_process(task);
  557. return ALARMTIMER_NORESTART;
  558. }
  559. /**
  560. * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
  561. * @alarm: ptr to alarmtimer
  562. * @absexp: absolute expiration time
  563. *
  564. * Sets the alarm timer and sleeps until it is fired or interrupted.
  565. */
  566. static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
  567. {
  568. alarm->data = (void *)current;
  569. do {
  570. set_current_state(TASK_INTERRUPTIBLE);
  571. alarm_start(alarm, absexp);
  572. if (likely(alarm->data))
  573. schedule();
  574. alarm_cancel(alarm);
  575. } while (alarm->data && !signal_pending(current));
  576. __set_current_state(TASK_RUNNING);
  577. return (alarm->data == NULL);
  578. }
  579. /**
  580. * update_rmtp - Update remaining timespec value
  581. * @exp: expiration time
  582. * @type: timer type
  583. * @rmtp: user pointer to remaining timepsec value
  584. *
  585. * Helper function that fills in rmtp value with time between
  586. * now and the exp value
  587. */
  588. static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
  589. struct timespec __user *rmtp)
  590. {
  591. struct timespec rmt;
  592. ktime_t rem;
  593. rem = ktime_sub(exp, alarm_bases[type].gettime());
  594. if (rem.tv64 <= 0)
  595. return 0;
  596. rmt = ktime_to_timespec(rem);
  597. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  598. return -EFAULT;
  599. return 1;
  600. }
  601. /**
  602. * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
  603. * @restart: ptr to restart block
  604. *
  605. * Handles restarted clock_nanosleep calls
  606. */
  607. static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
  608. {
  609. enum alarmtimer_type type = restart->nanosleep.clockid;
  610. ktime_t exp;
  611. struct timespec __user *rmtp;
  612. struct alarm alarm;
  613. int ret = 0;
  614. exp.tv64 = restart->nanosleep.expires;
  615. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  616. if (alarmtimer_do_nsleep(&alarm, exp))
  617. goto out;
  618. if (freezing(current))
  619. alarmtimer_freezerset(exp, type);
  620. rmtp = restart->nanosleep.rmtp;
  621. if (rmtp) {
  622. ret = update_rmtp(exp, type, rmtp);
  623. if (ret <= 0)
  624. goto out;
  625. }
  626. /* The other values in restart are already filled in */
  627. ret = -ERESTART_RESTARTBLOCK;
  628. out:
  629. return ret;
  630. }
  631. /**
  632. * alarm_timer_nsleep - alarmtimer nanosleep
  633. * @which_clock: clockid
  634. * @flags: determins abstime or relative
  635. * @tsreq: requested sleep time (abs or rel)
  636. * @rmtp: remaining sleep time saved
  637. *
  638. * Handles clock_nanosleep calls against _ALARM clockids
  639. */
  640. static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
  641. struct timespec *tsreq, struct timespec __user *rmtp)
  642. {
  643. enum alarmtimer_type type = clock2alarm(which_clock);
  644. struct alarm alarm;
  645. ktime_t exp;
  646. int ret = 0;
  647. struct restart_block *restart;
  648. if (!alarmtimer_get_rtcdev())
  649. return -ENOTSUPP;
  650. if (flags & ~TIMER_ABSTIME)
  651. return -EINVAL;
  652. if (!capable(CAP_WAKE_ALARM))
  653. return -EPERM;
  654. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  655. exp = timespec_to_ktime(*tsreq);
  656. /* Convert (if necessary) to absolute time */
  657. if (flags != TIMER_ABSTIME) {
  658. ktime_t now = alarm_bases[type].gettime();
  659. exp = ktime_add_safe(now, exp);
  660. }
  661. if (alarmtimer_do_nsleep(&alarm, exp))
  662. goto out;
  663. if (freezing(current))
  664. alarmtimer_freezerset(exp, type);
  665. /* abs timers don't set remaining time or restart */
  666. if (flags == TIMER_ABSTIME) {
  667. ret = -ERESTARTNOHAND;
  668. goto out;
  669. }
  670. if (rmtp) {
  671. ret = update_rmtp(exp, type, rmtp);
  672. if (ret <= 0)
  673. goto out;
  674. }
  675. restart = &current->restart_block;
  676. restart->fn = alarm_timer_nsleep_restart;
  677. restart->nanosleep.clockid = type;
  678. restart->nanosleep.expires = exp.tv64;
  679. restart->nanosleep.rmtp = rmtp;
  680. ret = -ERESTART_RESTARTBLOCK;
  681. out:
  682. return ret;
  683. }
  684. /* Suspend hook structures */
  685. static const struct dev_pm_ops alarmtimer_pm_ops = {
  686. .suspend = alarmtimer_suspend,
  687. };
  688. static struct platform_driver alarmtimer_driver = {
  689. .driver = {
  690. .name = "alarmtimer",
  691. .pm = &alarmtimer_pm_ops,
  692. }
  693. };
  694. /**
  695. * alarmtimer_init - Initialize alarm timer code
  696. *
  697. * This function initializes the alarm bases and registers
  698. * the posix clock ids.
  699. */
  700. static int __init alarmtimer_init(void)
  701. {
  702. struct platform_device *pdev;
  703. int error = 0;
  704. int i;
  705. struct k_clock alarm_clock = {
  706. .clock_getres = alarm_clock_getres,
  707. .clock_get = alarm_clock_get,
  708. .timer_create = alarm_timer_create,
  709. .timer_set = alarm_timer_set,
  710. .timer_del = alarm_timer_del,
  711. .timer_get = alarm_timer_get,
  712. .nsleep = alarm_timer_nsleep,
  713. };
  714. alarmtimer_rtc_timer_init();
  715. posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
  716. posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
  717. /* Initialize alarm bases */
  718. alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
  719. alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
  720. alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
  721. alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
  722. for (i = 0; i < ALARM_NUMTYPE; i++) {
  723. timerqueue_init_head(&alarm_bases[i].timerqueue);
  724. spin_lock_init(&alarm_bases[i].lock);
  725. }
  726. error = alarmtimer_rtc_interface_setup();
  727. if (error)
  728. return error;
  729. error = platform_driver_register(&alarmtimer_driver);
  730. if (error)
  731. goto out_if;
  732. pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
  733. if (IS_ERR(pdev)) {
  734. error = PTR_ERR(pdev);
  735. goto out_drv;
  736. }
  737. ws = wakeup_source_register("alarmtimer");
  738. return 0;
  739. out_drv:
  740. platform_driver_unregister(&alarmtimer_driver);
  741. out_if:
  742. alarmtimer_rtc_interface_remove();
  743. return error;
  744. }
  745. device_initcall(alarmtimer_init);