Skip to content

Commit 02ecee0

Browse files
Lakshmi-SowjanyaKAGA-KOKO
authored andcommitted
timekeeping: Add function to convert realtime to base clock
PPS (Pulse Per Second) generates a hardware pulse every second based on CLOCK_REALTIME. This works fine when the pulse is generated in software from a hrtimer callback function. For hardware which generates the pulse by programming a timer it is required to convert CLOCK_REALTIME to the underlying hardware clock. The X86 Timed IO device is based on the Always Running Timer (ART), which is the base clock of the TSC, which is usually the system clocksource on X86. The core code already has functionality to convert base clock timestamps to system clocksource timestamps, but there is no support for converting the other way around. Provide the required functionality to support such devices in a generic way to avoid code duplication in drivers: 1) ktime_real_to_base_clock() to convert a CLOCK_REALTIME timestamp to a base clock timestamp 2) timekeeping_clocksource_has_base() to allow drivers to validate that the system clocksource is based on a particular clocksource ID. [ tglx: Simplify timekeeping_clocksource_has_base() and add missing READ_ONCE() ] Co-developed-by: Thomas Gleixner <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Co-developed-by: Christopher S. Hall <[email protected]> Signed-off-by: Christopher S. Hall <[email protected]> Signed-off-by: Lakshmi Sowjanya D <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0f532a7 commit 02ecee0

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

include/linux/timekeeping.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ struct system_counterval_t {
318318
bool use_nsecs;
319319
};
320320

321+
extern bool ktime_real_to_base_clock(ktime_t treal,
322+
enum clocksource_ids base_id, u64 *cycles);
323+
extern bool timekeeping_clocksource_has_base(enum clocksource_ids id);
324+
321325
/*
322326
* Get cross timestamp between system clock and device clock
323327
*/

kernel/time/timekeeping.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,68 @@ static bool convert_base_to_cs(struct system_counterval_t *scv)
12351235
return true;
12361236
}
12371237

1238+
static bool convert_cs_to_base(u64 *cycles, enum clocksource_ids base_id)
1239+
{
1240+
struct clocksource *cs = tk_core.timekeeper.tkr_mono.clock;
1241+
struct clocksource_base *base;
1242+
1243+
/*
1244+
* Check whether base_id matches the base clock. Prevent the compiler from
1245+
* re-evaluating @base as the clocksource might change concurrently.
1246+
*/
1247+
base = READ_ONCE(cs->base);
1248+
if (!base || base->id != base_id)
1249+
return false;
1250+
1251+
*cycles -= base->offset;
1252+
if (!convert_clock(cycles, base->denominator, base->numerator))
1253+
return false;
1254+
return true;
1255+
}
1256+
1257+
static bool convert_ns_to_cs(u64 *delta)
1258+
{
1259+
struct tk_read_base *tkr = &tk_core.timekeeper.tkr_mono;
1260+
1261+
if (BITS_TO_BYTES(fls64(*delta) + tkr->shift) >= sizeof(*delta))
1262+
return false;
1263+
1264+
*delta = div_u64((*delta << tkr->shift) - tkr->xtime_nsec, tkr->mult);
1265+
return true;
1266+
}
1267+
1268+
/**
1269+
* ktime_real_to_base_clock() - Convert CLOCK_REALTIME timestamp to a base clock timestamp
1270+
* @treal: CLOCK_REALTIME timestamp to convert
1271+
* @base_id: base clocksource id
1272+
* @cycles: pointer to store the converted base clock timestamp
1273+
*
1274+
* Converts a supplied, future realtime clock value to the corresponding base clock value.
1275+
*
1276+
* Return: true if the conversion is successful, false otherwise.
1277+
*/
1278+
bool ktime_real_to_base_clock(ktime_t treal, enum clocksource_ids base_id, u64 *cycles)
1279+
{
1280+
struct timekeeper *tk = &tk_core.timekeeper;
1281+
unsigned int seq;
1282+
u64 delta;
1283+
1284+
do {
1285+
seq = read_seqcount_begin(&tk_core.seq);
1286+
if ((u64)treal < tk->tkr_mono.base_real)
1287+
return false;
1288+
delta = (u64)treal - tk->tkr_mono.base_real;
1289+
if (!convert_ns_to_cs(&delta))
1290+
return false;
1291+
*cycles = tk->tkr_mono.cycle_last + delta;
1292+
if (!convert_cs_to_base(cycles, base_id))
1293+
return false;
1294+
} while (read_seqcount_retry(&tk_core.seq, seq));
1295+
1296+
return true;
1297+
}
1298+
EXPORT_SYMBOL_GPL(ktime_real_to_base_clock);
1299+
12381300
/**
12391301
* get_device_system_crosststamp - Synchronously capture system/device timestamp
12401302
* @get_time_fn: Callback to get simultaneous device time and
@@ -1346,6 +1408,30 @@ int get_device_system_crosststamp(int (*get_time_fn)
13461408
}
13471409
EXPORT_SYMBOL_GPL(get_device_system_crosststamp);
13481410

1411+
/**
1412+
* timekeeping_clocksource_has_base - Check whether the current clocksource
1413+
* is based on given a base clock
1414+
* @id: base clocksource ID
1415+
*
1416+
* Note: The return value is a snapshot which can become invalid right
1417+
* after the function returns.
1418+
*
1419+
* Return: true if the timekeeper clocksource has a base clock with @id,
1420+
* false otherwise
1421+
*/
1422+
bool timekeeping_clocksource_has_base(enum clocksource_ids id)
1423+
{
1424+
/*
1425+
* This is a snapshot, so no point in using the sequence
1426+
* count. Just prevent the compiler from re-evaluating @base as the
1427+
* clocksource might change concurrently.
1428+
*/
1429+
struct clocksource_base *base = READ_ONCE(tk_core.timekeeper.tkr_mono.clock->base);
1430+
1431+
return base ? base->id == id : false;
1432+
}
1433+
EXPORT_SYMBOL_GPL(timekeeping_clocksource_has_base);
1434+
13491435
/**
13501436
* do_settimeofday64 - Sets the time of day.
13511437
* @ts: pointer to the timespec64 variable containing the new time

0 commit comments

Comments
 (0)