Skip to content

Commit 90adda9

Browse files
hrtimer: Add hrtimer support for CLOCK_TAI
Add hrtimer support for CLOCK_TAI, as well as posix timer interfaces. Signed-off-by: John Stultz <john.stultz@linaro.org>
1 parent 1ff3c96 commit 90adda9

5 files changed

Lines changed: 44 additions & 3 deletions

File tree

‎include/linux/hrtimer.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ enum hrtimer_base_type {
157157
HRTIMER_BASE_MONOTONIC,
158158
HRTIMER_BASE_REALTIME,
159159
HRTIMER_BASE_BOOTTIME,
160+
HRTIMER_BASE_TAI,
160161
HRTIMER_MAX_CLOCK_BASES,
161162
};
162163

@@ -327,7 +328,9 @@ extern ktime_t ktime_get(void);
327328
extern ktime_t ktime_get_real(void);
328329
extern ktime_t ktime_get_boottime(void);
329330
extern ktime_t ktime_get_monotonic_offset(void);
330-
extern ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot);
331+
extern ktime_t ktime_get_clocktai(void);
332+
extern ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
333+
ktime_t *offs_tai);
331334

332335
DECLARE_PER_CPU(struct tick_device, tick_cpu_device);
333336

‎include/linux/timekeeper_internal.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ struct timekeeper {
6464
struct timespec raw_time;
6565
/* The current UTC to TAI offset in seconds */
6666
s32 tai_offset;
67+
/* Offset clock monotonic -> clock tai */
68+
ktime_t offs_tai;
6769

6870
/* Seqlock for all timekeeper values */
6971
seqlock_t lock;

‎kernel/hrtimer.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,20 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
8383
.get_time = &ktime_get_boottime,
8484
.resolution = KTIME_LOW_RES,
8585
},
86+
{
87+
.index = HRTIMER_BASE_TAI,
88+
.clockid = CLOCK_TAI,
89+
.get_time = &ktime_get_clocktai,
90+
.resolution = KTIME_LOW_RES,
91+
},
8692
}
8793
};
8894

8995
static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
9096
[CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
9197
[CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
9298
[CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
99+
[CLOCK_TAI] = HRTIMER_BASE_TAI,
93100
};
94101

95102
static inline int hrtimer_clockid_to_base(clockid_t clock_id)
@@ -106,15 +113,19 @@ static void hrtimer_get_softirq_time(struct hrtimer_cpu_base *base)
106113
{
107114
ktime_t xtim, mono, boot;
108115
struct timespec xts, tom, slp;
116+
s32 tai_offset;
109117

110118
get_xtime_and_monotonic_and_sleep_offset(&xts, &tom, &slp);
119+
tai_offset = timekeeping_get_tai_offset();
111120

112121
xtim = timespec_to_ktime(xts);
113122
mono = ktime_add(xtim, timespec_to_ktime(tom));
114123
boot = ktime_add(mono, timespec_to_ktime(slp));
115124
base->clock_base[HRTIMER_BASE_REALTIME].softirq_time = xtim;
116125
base->clock_base[HRTIMER_BASE_MONOTONIC].softirq_time = mono;
117126
base->clock_base[HRTIMER_BASE_BOOTTIME].softirq_time = boot;
127+
base->clock_base[HRTIMER_BASE_TAI].softirq_time =
128+
ktime_add(xtim, ktime_set(tai_offset, 0));
118129
}
119130

120131
/*
@@ -651,8 +662,9 @@ static inline ktime_t hrtimer_update_base(struct hrtimer_cpu_base *base)
651662
{
652663
ktime_t *offs_real = &base->clock_base[HRTIMER_BASE_REALTIME].offset;
653664
ktime_t *offs_boot = &base->clock_base[HRTIMER_BASE_BOOTTIME].offset;
665+
ktime_t *offs_tai = &base->clock_base[HRTIMER_BASE_TAI].offset;
654666

655-
return ktime_get_update_offsets(offs_real, offs_boot);
667+
return ktime_get_update_offsets(offs_real, offs_boot, offs_tai);
656668
}
657669

658670
/*

‎kernel/posix-timers.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ static __init int init_posix_timers(void)
269269
struct k_clock clock_tai = {
270270
.clock_getres = hrtimer_get_res,
271271
.clock_get = posix_get_tai,
272+
.nsleep = common_nsleep,
273+
.nsleep_restart = hrtimer_nanosleep_restart,
274+
.timer_create = common_timer_create,
275+
.timer_set = common_timer_set,
276+
.timer_get = common_timer_get,
277+
.timer_del = common_timer_del,
272278
};
273279
struct k_clock clock_boottime = {
274280
.clock_getres = hrtimer_get_res,

‎kernel/time/timekeeping.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ static void tk_set_wall_to_mono(struct timekeeper *tk, struct timespec wtm)
6767
tk->wall_to_monotonic = wtm;
6868
set_normalized_timespec(&tmp, -wtm.tv_sec, -wtm.tv_nsec);
6969
tk->offs_real = timespec_to_ktime(tmp);
70+
tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tk->tai_offset, 0));
7071
}
7172

7273
static void tk_set_sleep_time(struct timekeeper *tk, struct timespec t)
@@ -409,6 +410,20 @@ void timekeeping_clocktai(struct timespec *ts)
409410
EXPORT_SYMBOL(timekeeping_clocktai);
410411

411412

413+
/**
414+
* ktime_get_clocktai - Returns the TAI time of day in a ktime
415+
*
416+
* Returns the time of day in a ktime.
417+
*/
418+
ktime_t ktime_get_clocktai(void)
419+
{
420+
struct timespec ts;
421+
422+
timekeeping_clocktai(&ts);
423+
return timespec_to_ktime(ts);
424+
}
425+
EXPORT_SYMBOL(ktime_get_clocktai);
426+
412427
#ifdef CONFIG_NTP_PPS
413428

414429
/**
@@ -569,6 +584,7 @@ s32 timekeeping_get_tai_offset(void)
569584
void __timekeeping_set_tai_offset(struct timekeeper *tk, s32 tai_offset)
570585
{
571586
tk->tai_offset = tai_offset;
587+
tk->offs_tai = ktime_sub(tk->offs_real, ktime_set(tai_offset, 0));
572588
}
573589

574590
/**
@@ -1539,7 +1555,8 @@ void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
15391555
* Returns current monotonic time and updates the offsets
15401556
* Called from hrtimer_interupt() or retrigger_next_event()
15411557
*/
1542-
ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
1558+
ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot,
1559+
ktime_t *offs_tai)
15431560
{
15441561
struct timekeeper *tk = &timekeeper;
15451562
ktime_t now;
@@ -1554,6 +1571,7 @@ ktime_t ktime_get_update_offsets(ktime_t *offs_real, ktime_t *offs_boot)
15541571

15551572
*offs_real = tk->offs_real;
15561573
*offs_boot = tk->offs_boot;
1574+
*offs_tai = tk->offs_tai;
15571575
} while (read_seqretry(&tk->lock, seq));
15581576

15591577
now = ktime_add_ns(ktime_set(secs, 0), nsecs);

0 commit comments

Comments
 (0)