Skip to content

Commit e2d8cce

Browse files
johnstultz-workgregkh
authored andcommitted
staging: android-alarm: Convert ALARM_ELAPSED_REALTIME to use CLOCK_BOOTTIME
The ALARM_ELAPSED_REALTIME clock domain in Android pointed to the need for something similar in linux system-wide (instead of limited to just the alarm interface). Thus CLOCK_BOOTTIME was introduced into the upstream kernel in 2.6.39. This patch attempts to convert the android alarm timer to utilize the kernel's CLOCK_BOOTTIME clockid for ALARM_ELAPSED_REALTIME, instead of managing it itself. CC: Colin Cross <ccross@android.com> CC: Thomas Gleixner <tglx@linutronix.de> CC: Android Kernel Team <kernel-team@android.com> Signed-off-by: John Stultz <john.stultz@linaro.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8f9064a commit e2d8cce

3 files changed

Lines changed: 22 additions & 58 deletions

File tree

‎drivers/staging/android/alarm-dev.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,7 @@ static long alarm_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
179179
break;
180180
case ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP:
181181
case ANDROID_ALARM_ELAPSED_REALTIME:
182-
tmp_time =
183-
ktime_to_timespec(alarm_get_elapsed_realtime());
182+
get_monotonic_boottime(&tmp_time);
184183
break;
185184
case ANDROID_ALARM_TYPE_COUNT:
186185
case ANDROID_ALARM_SYSTEMTIME:

‎drivers/staging/android/alarm.c‎

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ struct alarm_queue {
6565
struct rb_root alarms;
6666
struct rb_node *first;
6767
struct hrtimer timer;
68-
ktime_t delta;
6968
bool stopped;
7069
ktime_t stopped_time;
7170
};
@@ -107,8 +106,8 @@ static void update_timer_locked(struct alarm_queue *base, bool head_removed)
107106
}
108107

109108
hrtimer_try_to_cancel(&base->timer);
110-
base->timer.node.expires = ktime_add(base->delta, alarm->expires);
111-
base->timer._softexpires = ktime_add(base->delta, alarm->softexpires);
109+
base->timer.node.expires = alarm->expires;
110+
base->timer._softexpires = alarm->softexpires;
112111
hrtimer_start_expires(&base->timer, HRTIMER_MODE_ABS);
113112
}
114113

@@ -279,10 +278,6 @@ int android_alarm_set_rtc(struct timespec new_time)
279278
alarms[i].stopped = true;
280279
alarms[i].stopped_time = timespec_to_ktime(tmp_time);
281280
}
282-
alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
283-
alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
284-
ktime_sub(alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta,
285-
timespec_to_ktime(timespec_sub(tmp_time, new_time)));
286281
spin_unlock_irqrestore(&alarm_slock, flags);
287282
ret = do_settimeofday(&new_time);
288283
spin_lock_irqsave(&alarm_slock, flags);
@@ -310,24 +305,6 @@ int android_alarm_set_rtc(struct timespec new_time)
310305
return ret;
311306
}
312307

313-
/**
314-
* alarm_get_elapsed_realtime - get the elapsed real time in ktime_t format
315-
*
316-
* returns the time in ktime_t format
317-
*/
318-
ktime_t alarm_get_elapsed_realtime(void)
319-
{
320-
ktime_t now;
321-
unsigned long flags;
322-
struct alarm_queue *base = &alarms[ANDROID_ALARM_ELAPSED_REALTIME];
323-
324-
spin_lock_irqsave(&alarm_slock, flags);
325-
now = base->stopped ? base->stopped_time : ktime_get_real();
326-
now = ktime_sub(now, base->delta);
327-
spin_unlock_irqrestore(&alarm_slock, flags);
328-
return now;
329-
}
330-
331308
static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer)
332309
{
333310
struct alarm_queue *base;
@@ -339,7 +316,6 @@ static enum hrtimer_restart alarm_timer_triggered(struct hrtimer *timer)
339316

340317
base = container_of(timer, struct alarm_queue, timer);
341318
now = base->stopped ? base->stopped_time : hrtimer_cb_get_time(timer);
342-
now = ktime_sub(now, base->delta);
343319

344320
pr_alarm(INT, "alarm_timer_triggered type %td at %lld\n",
345321
base - alarms, ktime_to_ns(now));
@@ -536,40 +512,25 @@ static struct platform_driver alarm_driver = {
536512
}
537513
};
538514

539-
static int __init alarm_late_init(void)
540-
{
541-
unsigned long flags;
542-
struct timespec tmp_time, system_time;
543-
544-
/* this needs to run after the rtc is read at boot */
545-
spin_lock_irqsave(&alarm_slock, flags);
546-
/* We read the current rtc and system time so we can later calculate
547-
* elasped realtime to be (boot_systemtime + rtc - boot_rtc) ==
548-
* (rtc - (boot_rtc - boot_systemtime))
549-
*/
550-
getnstimeofday(&tmp_time);
551-
ktime_get_ts(&system_time);
552-
alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].delta =
553-
alarms[ANDROID_ALARM_ELAPSED_REALTIME].delta =
554-
timespec_to_ktime(timespec_sub(tmp_time, system_time));
555-
556-
spin_unlock_irqrestore(&alarm_slock, flags);
557-
return 0;
558-
}
559-
560515
static int __init alarm_driver_init(void)
561516
{
562517
int err;
563518
int i;
564519

565-
for (i = 0; i < ANDROID_ALARM_SYSTEMTIME; i++) {
566-
hrtimer_init(&alarms[i].timer,
567-
CLOCK_REALTIME, HRTIMER_MODE_ABS);
568-
alarms[i].timer.function = alarm_timer_triggered;
569-
}
520+
hrtimer_init(&alarms[ANDROID_ALARM_RTC_WAKEUP].timer,
521+
CLOCK_REALTIME, HRTIMER_MODE_ABS);
522+
hrtimer_init(&alarms[ANDROID_ALARM_RTC].timer,
523+
CLOCK_REALTIME, HRTIMER_MODE_ABS);
524+
hrtimer_init(&alarms[ANDROID_ALARM_ELAPSED_REALTIME_WAKEUP].timer,
525+
CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
526+
hrtimer_init(&alarms[ANDROID_ALARM_ELAPSED_REALTIME].timer,
527+
CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
570528
hrtimer_init(&alarms[ANDROID_ALARM_SYSTEMTIME].timer,
571-
CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
572-
alarms[ANDROID_ALARM_SYSTEMTIME].timer.function = alarm_timer_triggered;
529+
CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
530+
531+
for (i = 0; i < ANDROID_ALARM_TYPE_COUNT; i++)
532+
alarms[i].timer.function = alarm_timer_triggered;
533+
573534
err = platform_driver_register(&alarm_driver);
574535
if (err < 0)
575536
goto err1;
@@ -595,7 +556,6 @@ static void __exit alarm_exit(void)
595556
platform_driver_unregister(&alarm_driver);
596557
}
597558

598-
late_initcall(alarm_late_init);
599559
module_init(alarm_driver_init);
600560
module_exit(alarm_exit);
601561

‎drivers/staging/android/android_alarm.h‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum android_alarm_type {
3737

3838
#include <linux/ktime.h>
3939
#include <linux/rbtree.h>
40+
#include <linux/hrtimer.h>
4041

4142
/*
4243
* The alarm interface is similar to the hrtimer interface but adds support
@@ -71,7 +72,11 @@ void android_alarm_start_range(struct android_alarm *alarm, ktime_t start,
7172
ktime_t end);
7273
int android_alarm_try_to_cancel(struct android_alarm *alarm);
7374
int android_alarm_cancel(struct android_alarm *alarm);
74-
ktime_t alarm_get_elapsed_realtime(void);
75+
76+
static inline ktime_t alarm_get_elapsed_realtime(void)
77+
{
78+
return ktime_get_boottime();
79+
}
7580

7681
/* set rtc while preserving elapsed realtime */
7782
int android_alarm_set_rtc(const struct timespec ts);

0 commit comments

Comments
 (0)