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