alarmtimer.c 22 KB

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