ale-leehrtz has uploaded this change for review.
dahdi-base: add software clock correction of the coretimer
Without a hardware clock source, DAHDI is paced by its coretimer, which
runs from the host clock. For an E1-over-IP trunkdev this is not the
clock of the remote E1, so the jitter buffer slowly drains or overflows.
Allow user space to discipline the coretimer: it now runs at a rate of
(1 + fudge / 2^32). When a new fudge is set, the raw and fudged time
offsets are re-anchored so the coretimer stays phase-continuous. The
scaling uses the existing 96-bit fixed-point lineartime helpers, without
any allocation, as it runs in timer context.
Two new ioctls on /dev/dahdi/channel, allocated downwards from 252 like
the trunkdev ones to avoid clashes with upstream DAHDI:
DAHDI_SW_CLOCK_GET: read target/current fudge and the coretimer interval
DAHDI_SW_CLOCK_SET: set the target fudge (a single __s32)
This is used by the SD-RI-PLL in osmo-e1d, see the related ticket.
Related: OS#7116
Change-Id: I93243a48566b337b3bb8f728d0ccf4b756755e4d
---
M drivers/dahdi/dahdi-base.c
M include/dahdi/user.h
2 files changed, 633 insertions(+), 0 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/dahdi-linux refs/changes/34/44234/1
diff --git a/drivers/dahdi/dahdi-base.c b/drivers/dahdi/dahdi-base.c
index 657eb34..bfee1d4 100644
--- a/drivers/dahdi/dahdi-base.c
+++ b/drivers/dahdi/dahdi-base.c
@@ -16,6 +16,8 @@
*
* Copyright (C) 2001 Jim Dixon / Zapata Telephony.
* Copyright (C) 2001 - 2012 Digium, Inc.
+ * Copyright (C) 2025 - 2026 C. Alejandra Carreon-Jimenez <alejandra.carreon@ale-leehrtz.org>
+ * (software clock correction of the coretimer)
*
* All rights reserved.
*
@@ -205,6 +207,17 @@
atomic_t count;
atomic_t shutdown;
atomic_t last_count;
+
+ /* SD-RI-PLL: the coretimer clock is scaled by (1 + fudge / 2^32),
+ * with the fudge supplied from user space via DAHDI_SW_CLOCK_SET.
+ * target_fudge is written by the ioctl; current_fudge is what the
+ * timer applies. The offsets keep the clock phase-continuous across
+ * a fudge change. fudge_lock serialises the ioctl against the timer. */
+ int32_t target_fudge;
+ int32_t current_fudge;
+ ktime_t raw_time_offset;
+ ktime_t fudged_time_offset;
+ spinlock_t fudge_lock;
} core_timer;
#endif /* CONFIG_DAHDI_CORE_TIMER */
@@ -7008,6 +7021,36 @@
if (res)
file->private_data = NULL;
return res;
+#ifdef CONFIG_DAHDI_CORE_TIMER
+ case DAHDI_SW_CLOCK_GET: {
+ struct dahdi_sw_clock clk;
+ struct timespec64 ts = ktime_to_timespec64(core_timer.start_interval);
+ unsigned long flags;
+
+ spin_lock_irqsave(&core_timer.fudge_lock, flags);
+ clk.target_fudge = core_timer.target_fudge;
+ clk.current_fudge = core_timer.current_fudge;
+ spin_unlock_irqrestore(&core_timer.fudge_lock, flags);
+ clk.interval_sec = ts.tv_sec;
+ clk.interval_nsec = ts.tv_nsec;
+
+ if (copy_to_user((void __user *)data, &clk, sizeof(clk)))
+ return -EFAULT;
+ return 0;
+ }
+ case DAHDI_SW_CLOCK_SET: {
+ __s32 fudge;
+ unsigned long flags;
+
+ if (get_user(fudge, (__s32 __user *)data))
+ return -EFAULT;
+
+ spin_lock_irqsave(&core_timer.fudge_lock, flags);
+ core_timer.target_fudge = fudge;
+ spin_unlock_irqrestore(&core_timer.fudge_lock, flags);
+ return 0;
+ }
+#endif /* CONFIG_DAHDI_CORE_TIMER */
default:
return -ENOSYS;
}
@@ -10086,6 +10129,575 @@
return atomic_read(&ct->count) * DAHDI_MSECS_PER_CHUNK;
}
+/*
+ * SD-RI-PLL: software clock correction of the coretimer.
+ *
+ * User space (osmo-e1d) may discipline the coretimer through the
+ * DAHDI_SW_CLOCK_{GET,SET} ioctls: the coretimer then runs at a rate of
+ * (1 + fudge / 2^32). The time scaling uses 96-bit fixed-point arithmetic
+ * (2^64 * h + 2^32 * m + l nanoseconds) with 32-bit limbs only, so it needs
+ * neither 64-bit division nor floating point in the kernel.
+ */
+
+/* We work with 32bit unsigned integers and 32bit signed integers */
+typedef uint32_t ui32_t;
+typedef int32_t si32_t;
+
+/* 2^64 * h + 2^32 * m + l gives time in nanoseconds since an epoch */
+typedef struct {
+ ui32_t h;
+ ui32_t m;
+ ui32_t l;
+} lineartime_t;
+
+/* Helper function for addition: 2^32 * cout + s = a + b + cin, cin
+ either 0 or 1
+*/
+static void __lineartime_add_ui32(ui32_t *cout, ui32_t *s,
+ ui32_t a, ui32_t b, ui32_t cin) {
+ ui32_t co, sum;
+
+ co = (ui32_t) 0;
+ sum = a + b;
+ if (sum < a) co += (ui32_t) 1;
+ sum += cin;
+ if (sum < cin) co += (ui32_t) 1;
+ *cout = co;
+ *s = sum;
+}
+
+/* Helper function for multiplication: 2^32 * h + l = a * b + c */
+static void __lineartime_muladd_ui32(ui32_t *h, ui32_t *l,
+ ui32_t a, ui32_t b, ui32_t c) {
+ ui32_t hi, lo;
+ ui32_t ah, al, bh, bl, ch, cl;
+ ui32_t ahbh, ahbl, albh, albl;
+ ui32_t ahblh, ahbll, albhh, albhl, alblh, albll;
+ ui32_t t1, t1h, t1l;
+ ui32_t t2, t2h, t2l;
+
+ /* Split the arguments */
+ ah = a >> 16;
+ al = a & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ bh = b >> 16;
+ bl = b & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ ch = c >> 16;
+ cl = c & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ /* Four partial multiplications */
+ ahbh = ah * bh;
+ ahbl = ah * bl;
+ albh = al * bh;
+ albl = al * bl;
+
+ /* Split the intermediates */
+ ahblh = ahbl >> 16;
+ ahbll = ahbl & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ albhh = albh >> 16;
+ albhl = albh & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ alblh = albl >> 16;
+ albll = albl & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ /*
+
+ |---- ahbh ----|
+ |---- ahblh ----|---- ahbll ----|
+ |---- albhh ----|---- albhl ----|
+ |---- alblh ----|---- albll ----|
+ |---- ch ----|---- cl ----|
+
+ */
+ t1 = albll + cl;
+
+ /* Resplit */
+ t1h = t1 >> 16;
+ t1l = t1 & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ /*
+
+ |---- ahbh ----|
+ |---- ahblh ----|---- ahbll ----|
+ |---- albhh ----|---- albhl ----|
+ |---- alblh ----|
+ |---- ch ----|
+ |---- t1h ----|---- t1l ----|
+
+ */
+ t2 = ahbll + albhl + alblh + ch + t1h;
+
+ /* Resplit */
+ t2h = t2 >> 16;
+ t2l = t2 & ((((ui32_t) 1) << 16) - ((ui32_t) 1));
+
+ /*
+
+ |---- ahbh ----|
+ |---- ahblh ----|
+ |---- albhh ----|
+ |---- t2h ----|---- t2l ----|
+ |---- t1l ----|
+
+ */
+ lo = (t2l << 16) + t1l;
+ hi = ahbh + ahblh + albhh + t2h;
+
+ /* Write back */
+ *h = hi;
+ *l = lo;
+}
+
+/* Helper function: convert time64_t to two times 32bits:
+ 2^32 * h + l <- t
+*/
+static void __lineartime_time_to_limbs(ui32_t *h, ui32_t *l,
+ time64_t t) {
+ ui32_t hi, lo;
+ ui32_t hh, hl, lh, ll;
+ time64_t tm, t1, t2, t3;
+
+ if (t < ((time64_t) 0)) {
+ hi = (ui32_t) 0;
+ lo = (ui32_t) 0;
+ } else {
+ tm = t;
+
+ t1 = tm >> 16;
+ t2 = t1 << 16;
+ t3 = tm - t2;
+ tm = t1;
+ ll = (ui32_t) t3;
+
+ t1 = tm >> 16;
+ t2 = t1 << 16;
+ t3 = tm - t2;
+ tm = t1;
+ lh = (ui32_t) t3;
+
+ t1 = tm >> 16;
+ t2 = t1 << 16;
+ t3 = tm - t2;
+ tm = t1;
+ hl = (ui32_t) t3;
+
+ t1 = tm >> 16;
+ t2 = t1 << 16;
+ t3 = tm - t2;
+ tm = t1;
+ hh = (ui32_t) t3;
+
+ hi = (hh << 16) + hl;
+ lo = (lh << 16) + ll;
+ }
+
+ *h = hi;
+ *l = lo;
+}
+
+/* Helper function: divide 2^32 * h + l by 10^9
+
+ The function assumes that
+
+ 0 <= 2^32 * h + l < 2^32 * 10^9
+
+ The function returns q and r such that
+
+ 10^9 * q + r = 2^32 * h + l
+
+ and 0 <= r <= 10^9 - 1.
+
+*/
+static void __lineartime_div_by_ten_to_nine(ui32_t *q, ui32_t *r,
+ ui32_t h, ui32_t l) {
+ const ui32_t two_52_by_five_9 = (ui32_t) 2305843009ull;
+ const ui32_t ten_9 = (ui32_t) 1000000000ull;
+ ui32_t ph, pm, t, pl, w, tl, rl, qo, re;
+
+ /* 2^32 * t + pl = l * floor(2^52 / 5^9) */
+ __lineartime_muladd_ui32(&t, &pl, l, two_52_by_five_9, ((ui32_t) 0));
+
+ /* 2^32 * ph + pm = h * floor(2^52 / 5^9) + t */
+ __lineartime_muladd_ui32(&ph, &pm, h, two_52_by_five_9, t);
+
+ /* 2^64 * ph + 2^32 * pm + pl =
+ = 2^32 * (2^32 * ph + pm) + pl
+ = 2^32 * (h * floor(2^52 / 5^9) + t) + pl
+ = 2^32 * h * floor(2^52 / 5^9) + 2^32 * t + pl
+ = 2^32 * h * floor(2^52 / 5^9) + l * floor(2^52 / 5^9)
+ = (2^32 * h + l) * floor(2^52 / 5^9)
+ = (2^32 * h + l) * (2^52 / 5^9 + delta1) w/ -1 < delta1 <= 0
+ = (2^32 * h + l) * (2^61 / 10^9 + delta1)
+ = ((2^32 * h + l) / 10^9 + delta2) * 2^61
+
+ where delta2 = delta1 * (2^32 * h + l) / 2^61
+
+ We have delta2 bounded as follows:
+
+ 0 >= delta2 > -1 * 2^32 * 10^9 / 2^61
+ = -2^-29 * 10^9
+ > -1.87
+
+ We know that 0 <= 2^-61 * pl <= 2^-61 * (2^32 - 1) < 2^-29.
+
+ This means that
+
+ w = 2^3 * ph + floor(pm / 2^29) =
+ = floor(2^3 * ph + 2^-29 * pm)
+ = floor(2^3 * ph + 2^-29 * pm + 2^-61 * pl)
+ = floor((2^32 * h + l) / 10^9 + delta2)
+ = floor((2^32 * h + l) / 10^9) + floor(delta3)
+ = floor((2^32 * h + l) / 10^9) - delta4
+
+ with delta3 = (2^32 * h + l) / 10^9
+ - floor((2^32 * h + l) / 10^9)
+ + delta2
+
+ and delta4 = -floor(delta3).
+
+ It is clear that delta3 is bounded by
+
+ -1.87 < delta3 < 1.
+
+ Therefore delta4 \in {0, 1, 2}.
+
+ Clearly, w is bounded by
+
+ 0 <= w < floor(2^32 * 10^9 / 10^9) - 0 = 2^32.
+
+ */
+ w = (ph << 3) + (pm >> 29);
+
+ /* 2^32 * th + tl = w * 10^9
+
+ th does not get computed, see below.
+
+ The next operation will do mod 2^32 arithmetic. This is accounted
+ for.
+ */
+ tl = w * ten_9;
+
+ /* 2^32 * th + tl
+ = (floor((2^32 * h + l) / 10^9) - delta4) * 10^9
+ = floor((2^32 * h + l) / 10^9) * 10^9 - delta4 * 10^9
+
+ Therefore
+
+ 2^32 * rh + rl
+ = 2^32 * h + l - (2^32 * th + tl)
+ = 2^32 * h + l - floor((2^32 * h + l) / 10^9) * 10^9
+ + delta4 * 10^9
+
+ Clearly,
+ 0 <= 2^32 * h + l - floor((2^32 * h + l) / 10^9) * 10^9
+ < 10^9
+
+ Hence
+
+ 0 <= 2^32 * rh + rl < 10^9 + 2 * 10^9 = 3 * 10^9 < 2^32
+
+ This means, rh will be zero all the time and does not need to be
+ computed. This also means that th does not need to be computed.
+
+ We get
+
+ rl = 2^32 * h + l - (2^32 * th + tl)
+
+ and
+
+ 2^32 * h + l = w * 10^9 + rl.
+
+ Therefore
+
+ qo = floor((2^32 * h + l) / 10^9) =
+ = floor((w * 10^9 + rl) / 10^9)
+ = floor(w + rl / 10^9)
+ = w + floor(rl / 10^9)
+
+ Clearly, 0 <= qo < floor(2^32 * 10^9 / 10^9) = 2^32.
+
+ */
+ rl = l - tl;
+ qo = w + (rl / ten_9);
+
+ /* Compute the actual remainder.
+
+ qo behaves like w in the multiplication and subtraction above.
+
+ The modulo 2^32 behavior of the arithmetic has been accounted
+ for.
+
+ */
+ re = l - (qo * ten_9);
+
+ /* Write back the results */
+ *q = qo;
+ *r = re;
+}
+
+/* Helper function: divide 2^64 * h + 2^32 * m + l by 10^9.
+
+ Return 2^64 * qh + 2^32 * qm + ql and r such that
+
+ (2^64 * qh + 2^32 * qm + ql) * 10^9 + r
+ = 2^64 * h + 2^32 * m + l
+
+*/
+static void __lineartime_long_div_by_ten_to_nine(ui32_t *qh,
+ ui32_t *qm,
+ ui32_t *ql,
+ ui32_t *r,
+ ui32_t h,
+ ui32_t m,
+ ui32_t l) {
+ ui32_t ra, rb, rc, rd;
+ ui32_t qa, qb, qc;
+
+ ra = (ui32_t) 0;
+
+ /* qa * 10^9 + rb = 2^32 * ra + h */
+ __lineartime_div_by_ten_to_nine(&qa, &rb, ra, h);
+
+ /* qb * 10^9 + rc = 2^32 * rb + m */
+ __lineartime_div_by_ten_to_nine(&qb, &rc, rb, m);
+
+ /* qc * 10^9 + rd = 2^32 * rc + l */
+ __lineartime_div_by_ten_to_nine(&qc, &rd, rc, l);
+
+ /* Here we have
+
+ (2^64 * qa + 2^32 * qb + qc) * 10^9 + rd
+ = 2^32 * (2^32 * qa + qb) * 10^9 + qc * 10^9 + rd
+ = 2^32 * ((2^32 * qa + qb) * 10^9 + rc) + l
+ = 2^32 * (2^32 * qa * 10^9 + qb * 10^9 + rc) + l
+ = 2^32 * (2^32 * (qa * 10^9 + rb) + m) + l
+ = 2^32 * (2^32 * (2^32 * ra + h) + m) + l
+ = 2^32 * (2^32 * h + m) + l
+ = 2^64 * h + 2^32 * m + l
+
+ with 0 <= rd <= 10^9 - 1.
+
+ We have hence what we need.
+
+ */
+ *qh = qa;
+ *qm = qb;
+ *ql = qc;
+ *r = rd;
+}
+
+/* rop <- op, rop becomes zero if op is negative */
+static void timespec_to_lineartime(lineartime_t *rop,
+ const struct timespec64 *op) {
+ ui32_t sh, sl, ns;
+ ui32_t t1h, t1l;
+ ui32_t t2h, t2l;
+
+ /* (2^32 * sh + sl) * 10^9 + ns <- op */
+ __lineartime_time_to_limbs(&sh, &sl, op->tv_sec);
+ ns = (ui32_t) op->tv_nsec;
+
+ /* 2^32 * t1h + t1l = sl * 10^9 + ns */
+ __lineartime_muladd_ui32(&t1h, &t1l, sl, ((ui32_t) 1000000000ull), ns);
+
+ /* 2^32 * t2h + t2l = sh * 10^9 + t1h */
+ __lineartime_muladd_ui32(&t2h, &t2l, sh, ((ui32_t) 1000000000ull), t1h);
+
+ /* 2^64 * t2h + 2^32 * t2l + t1l =
+ = 2^32 * (2^32 * t2h + t2l) + t1l
+ = 2^32 * (sh * 10^9 + t1h) + t1l
+ = 2^32 * sh * 10^9 + 2^32 * t1h + t1l
+ = 2^32 * sh * 10^9 + sl * 10^9 + ns
+ = (2^32 * sh + sl) * 10^9 + ns
+ = op
+ */
+ rop->h = t2h;
+ rop->m = t2l;
+ rop->l = t1l;
+}
+
+/* rop <- op, rop becomes zero if op cannot be represented */
+static void lineartime_to_timespec(struct timespec64 *rop,
+ const lineartime_t *op) {
+ ui32_t sh, sm, sl, ns;
+ time64_t sec, t;
+ ui32_t th, tm, tl;
+ long nsl;
+ ui32_t tns;
+
+ __lineartime_long_div_by_ten_to_nine(&sh, &sm, &sl,
+ &ns,
+ op->h, op->m, op->l);
+
+ sec = (time64_t) 0;
+
+ t = (time64_t) sh;
+ sec = ((sec << 16) << 16) + t;
+
+ t = (time64_t) sm;
+ sec = ((sec << 16) << 16) + t;
+
+ t = (time64_t) sl;
+ sec = ((sec << 16) << 16) + t;
+
+ th = (ui32_t) 0;
+ __lineartime_time_to_limbs(&tm, &tl, sec);
+
+ nsl = (long) ns;
+ tns = (ui32_t) nsl;
+
+ if ((th != sh) ||
+ (tm != sm) ||
+ (tl != sl) ||
+ (nsl < ((long) 0)) ||
+ (tns != ns)) {
+ rop->tv_sec = (time64_t) 0;
+ rop->tv_nsec = (long) 0;
+ return;
+ }
+
+ rop->tv_sec = sec;
+ rop->tv_nsec = nsl;
+}
+
+/* rop <- op1 + op2 mod 2^96ns */
+static void lineartime_add(lineartime_t *rop,
+ const lineartime_t *op1, const lineartime_t *op2) {
+ ui32_t cin, cout;
+
+ cin = (ui32_t) 0;
+
+ __lineartime_add_ui32(&cout, &rop->l, op1->l, op2->l, cin);
+ cin = cout;
+
+ __lineartime_add_ui32(&cout, &rop->m, op1->m, op2->m, cin);
+ cin = cout;
+
+ __lineartime_add_ui32(&cout, &rop->h, op1->h, op2->h, cin);
+}
+
+/* rop <- op1 - op2 mod 2^96ns */
+static void lineartime_sub(lineartime_t *rop,
+ const lineartime_t *op1, const lineartime_t *op2) {
+ ui32_t cin, cout;
+
+ cin = (ui32_t) 1;
+
+ __lineartime_add_ui32(&cout, &rop->l, op1->l, ~op2->l, cin);
+ cin = cout;
+
+ __lineartime_add_ui32(&cout, &rop->m, op1->m, ~op2->m, cin);
+ cin = cout;
+
+ __lineartime_add_ui32(&cout, &rop->h, op1->h, ~op2->h, cin);
+}
+
+/* rop <- op1 * (1 + op2/2^32) mod 2^96ns */
+static void lineartime_scale(lineartime_t *rop,
+ const lineartime_t *op1, si32_t *op2) {
+ int neg;
+ ui32_t absop2;
+ lineartime_t t;
+ ui32_t d, c, b, a, h1, h2;
+
+ /* op1 * (1 + op2/2^32) = op1 + op1 * op2 / 2^32
+ = op1 +/- op1 * |op2| / 2^32
+ */
+ if (*op2 >= ((si32_t) 0)) {
+ absop2 = (ui32_t) *op2;
+ neg = 0;
+ } else {
+ absop2 = ((ui32_t) (-(*op2 + ((si32_t) 1)))) + ((ui32_t) 1);
+ neg = 1;
+ }
+
+ /* t <- (op1 * absop2) / 2^32
+ = ((2^64 * op1->h + 2^32 * op1->m + op1->l) * absop2) / 2^32
+ */
+ __lineartime_muladd_ui32(&h1, &a, op1->l, absop2, ((ui32_t) 0));
+ __lineartime_muladd_ui32(&h2, &b, op1->m, absop2, h1);
+ __lineartime_muladd_ui32(&d, &c, op1->h, absop2, h2);
+
+ /* 2^96 * d + 2^64 * c + 2^32 * b + a =
+ = 2^64 * (2^32 * d + c) + 2^32 * b + a
+ = 2^64 * (op1->h * absop2 + h2) + 2^32 * b + a
+ = 2^64 * op1->h * absop2 + 2^32 * (2^32 * h2 + b) + a
+ = 2^64 * op1->h * absop2 + 2^32 * (op1->m * absop2 + h1) + a
+ = 2^64 * op1->h * absop2 + 2^32 * op1->m * absop2 + 2^32 * h1 + a
+ = 2^64 * op1->h * absop2 + 2^32 * op1->m * absop2 + op1->l * absop2 + 0
+ = (2^64 * op1->h + 2^32 * op1->m + op1->l) * absop2
+
+ */
+ t.h = d;
+ t.m = c;
+ t.l = b;
+
+ /* rop <- op1 +/- t */
+ if (neg) {
+ lineartime_sub(rop, op1, &t);
+ } else {
+ lineartime_add(rop, op1, &t);
+ }
+}
+
+/*
+ * fudged_now = fudged_offset + (now - unfudged_offset) * (1 + fudge/2^32)
+ *
+ * Computed with the 96-bit fixed-point lineartime_t helpers: integer-only,
+ * O(1), no allocation. This runs from the coretimer callback (timer/softirq
+ * context), so every operand lives on the stack.
+ */
+static ktime_t compute_fudged_now(ktime_t now, si32_t fudge,
+ ktime_t unfudged_offset, ktime_t fudged_offset)
+{
+ struct timespec64 ts_now, ts_unfudged, ts_fudged, ts_result;
+ lineartime_t lt_now, lt_unfudged, lt_fudged_offset;
+ lineartime_t lt_diff, lt_scaled, lt_result;
+
+ ts_now = ktime_to_timespec64(now);
+ ts_unfudged = ktime_to_timespec64(unfudged_offset);
+ ts_fudged = ktime_to_timespec64(fudged_offset);
+
+ timespec_to_lineartime(<_now, &ts_now);
+ timespec_to_lineartime(<_unfudged, &ts_unfudged);
+ timespec_to_lineartime(<_fudged_offset, &ts_fudged);
+
+ lineartime_sub(<_diff, <_now, <_unfudged); /* now - unfudged_offset */
+ lineartime_scale(<_scaled, <_diff, &fudge); /* * (1 + fudge/2^32) */
+ lineartime_add(<_result, <_scaled, <_fudged_offset); /* + fudged_offset */
+
+ lineartime_to_timespec(&ts_result, <_result);
+ return timespec64_to_ktime(ts_result);
+}
+
+/*
+ * Apply the software-PLL fudge to the raw monotonic time. When user space
+ * has pushed a new target fudge, re-anchor the offsets so the fudged clock
+ * stays phase-continuous across the change.
+ */
+static ktime_t coretimer_fudge(ktime_t now, struct core_timer *ct)
+{
+ ktime_t fudged_now;
+ unsigned long flags;
+
+ fudged_now = compute_fudged_now(now, ct->current_fudge,
+ ct->raw_time_offset,
+ ct->fudged_time_offset);
+
+ spin_lock_irqsave(&ct->fudge_lock, flags);
+ if (ct->target_fudge != ct->current_fudge) {
+ ct->current_fudge = ct->target_fudge;
+ ct->raw_time_offset = now;
+ ct->fudged_time_offset = fudged_now;
+ }
+ spin_unlock_irqrestore(&ct->fudge_lock, flags);
+
+ return fudged_now;
+}
+
+
static void coretimer_func(TIMER_DATA_TYPE unused)
{
unsigned long flags;
@@ -10098,6 +10710,9 @@
now = ktime_get();
+ /* SD-RI-PLL: scale the coretimer clock by the user-space fudge */
+ now = coretimer_fudge(now, &core_timer);
+
if (atomic_read(&core_timer.count) ==
atomic_read(&core_timer.last_count)) {
@@ -10171,6 +10786,11 @@
core_timer.start_interval = ktime_get();
atomic_set(&core_timer.count, 0);
atomic_set(&core_timer.shutdown, 0);
+ spin_lock_init(&core_timer.fudge_lock);
+ core_timer.target_fudge = 0;
+ core_timer.current_fudge = 0;
+ core_timer.raw_time_offset = core_timer.start_interval;
+ core_timer.fudged_time_offset = core_timer.start_interval;
core_timer.interval = max(msecs_to_jiffies(DAHDI_MSECS_PER_CHUNK), 1UL);
if (core_timer.interval < (HZ/250))
core_timer.interval = (HZ/250);
diff --git a/include/dahdi/user.h b/include/dahdi/user.h
index 17e7a0e..c75dabb 100644
--- a/include/dahdi/user.h
+++ b/include/dahdi/user.h
@@ -1243,6 +1243,19 @@
};
#define DAHDI_TRUNKDEV_OPEN _IOWR(DAHDI_CODE, 253, struct dahdi_trunkdev_open)
+/* Software clock correction of the DAHDI coretimer (SD-RI-PLL). The coretimer
+ * runs at a rate of (1 + fudge / 2^32); user space steers it by setting the
+ * target fudge via DAHDI_SW_CLOCK_SET. Numbers are allocated downwards from
+ * 252, like the trunkdev ones above, to avoid clashes with upstream DAHDI. */
+struct dahdi_sw_clock {
+ __s32 interval_sec; /* coretimer start interval, seconds */
+ __s32 interval_nsec; /* coretimer start interval, nanoseconds */
+ __s32 target_fudge; /* last fudge set via DAHDI_SW_CLOCK_SET */
+ __s32 current_fudge; /* fudge currently applied by the coretimer */
+};
+#define DAHDI_SW_CLOCK_GET _IOR(DAHDI_CODE, 252, struct dahdi_sw_clock)
+#define DAHDI_SW_CLOCK_SET _IOW(DAHDI_CODE, 251, __s32) /* new target fudge */
+
#endif /* _DAHDI_USER_H */
To view, visit change 44234. To unsubscribe, or for help writing mail filters, visit settings.