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