1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * Mock-up PTP Hardware Clock driver for virtual network devices
7 * Create a PTP clock which offers PTP time manipulation operations
8 * using a timecounter/cyclecounter on top of CLOCK_MONOTONIC_RAW.
11 #include <linux/ptp_clock_kernel.h>
12 #include <linux/ptp_mock.h>
13 #include <linux/timecounter.h>
15 /* Clamp scaled_ppm between -2,097,152,000 and 2,097,152,000,
16 * and thus "adj" between -68,719,476 and 68,719,476
18 #define MOCK_PHC_MAX_ADJ_PPB 32000000
19 /* Timestamps from ktime_get_raw() have 1 ns resolution, so the scale factor
20 * (MULT >> SHIFT) needs to be 1. Pick SHIFT as 31 bits, which translates
21 * MULT(freq 0) into 0x80000000.
23 #define MOCK_PHC_CC_SHIFT 31
24 #define MOCK_PHC_CC_MULT (1 << MOCK_PHC_CC_SHIFT)
25 #define MOCK_PHC_FADJ_SHIFT 9
26 #define MOCK_PHC_FADJ_DENOMINATOR 15625ULL
28 /* The largest cycle_delta that timecounter_read_delta() can handle without a
29 * 64-bit overflow during the multiplication with cc->mult, given the max "adj"
30 * we permit, is ~8.3 seconds. Make sure readouts are more frequent than that.
32 #define MOCK_PHC_REFRESH_INTERVAL (HZ * 5)
34 #define info_to_phc(d) container_of((d), struct mock_phc, info)
37 struct ptp_clock_info info
;
38 struct ptp_clock
*clock
;
39 struct timecounter tc
;
40 struct cyclecounter cc
;
44 static u64
mock_phc_cc_read(const struct cyclecounter
*cc
)
46 return ktime_get_raw_ns();
49 static int mock_phc_adjfine(struct ptp_clock_info
*info
, long scaled_ppm
)
51 struct mock_phc
*phc
= info_to_phc(info
);
54 adj
= (s64
)scaled_ppm
<< MOCK_PHC_FADJ_SHIFT
;
55 adj
= div_s64(adj
, MOCK_PHC_FADJ_DENOMINATOR
);
57 spin_lock(&phc
->lock
);
58 timecounter_read(&phc
->tc
);
59 phc
->cc
.mult
= MOCK_PHC_CC_MULT
+ adj
;
60 spin_unlock(&phc
->lock
);
65 static int mock_phc_adjtime(struct ptp_clock_info
*info
, s64 delta
)
67 struct mock_phc
*phc
= info_to_phc(info
);
69 spin_lock(&phc
->lock
);
70 timecounter_adjtime(&phc
->tc
, delta
);
71 spin_unlock(&phc
->lock
);
76 static int mock_phc_settime64(struct ptp_clock_info
*info
,
77 const struct timespec64
*ts
)
79 struct mock_phc
*phc
= info_to_phc(info
);
80 u64 ns
= timespec64_to_ns(ts
);
82 spin_lock(&phc
->lock
);
83 timecounter_init(&phc
->tc
, &phc
->cc
, ns
);
84 spin_unlock(&phc
->lock
);
89 static int mock_phc_gettime64(struct ptp_clock_info
*info
, struct timespec64
*ts
)
91 struct mock_phc
*phc
= info_to_phc(info
);
94 spin_lock(&phc
->lock
);
95 ns
= timecounter_read(&phc
->tc
);
96 spin_unlock(&phc
->lock
);
98 *ts
= ns_to_timespec64(ns
);
103 static long mock_phc_refresh(struct ptp_clock_info
*info
)
105 struct timespec64 ts
;
107 mock_phc_gettime64(info
, &ts
);
109 return MOCK_PHC_REFRESH_INTERVAL
;
112 int mock_phc_index(struct mock_phc
*phc
)
114 return ptp_clock_index(phc
->clock
);
116 EXPORT_SYMBOL_GPL(mock_phc_index
);
118 struct mock_phc
*mock_phc_create(struct device
*dev
)
120 struct mock_phc
*phc
;
123 phc
= kzalloc(sizeof(*phc
), GFP_KERNEL
);
129 phc
->info
= (struct ptp_clock_info
) {
130 .owner
= THIS_MODULE
,
131 .name
= "Mock-up PTP clock",
132 .max_adj
= MOCK_PHC_MAX_ADJ_PPB
,
133 .adjfine
= mock_phc_adjfine
,
134 .adjtime
= mock_phc_adjtime
,
135 .gettime64
= mock_phc_gettime64
,
136 .settime64
= mock_phc_settime64
,
137 .do_aux_work
= mock_phc_refresh
,
140 phc
->cc
= (struct cyclecounter
) {
141 .read
= mock_phc_cc_read
,
142 .mask
= CYCLECOUNTER_MASK(64),
143 .mult
= MOCK_PHC_CC_MULT
,
144 .shift
= MOCK_PHC_CC_SHIFT
,
147 spin_lock_init(&phc
->lock
);
148 timecounter_init(&phc
->tc
, &phc
->cc
, 0);
150 phc
->clock
= ptp_clock_register(&phc
->info
, dev
);
151 if (IS_ERR(phc
->clock
)) {
152 err
= PTR_ERR(phc
->clock
);
156 ptp_schedule_worker(phc
->clock
, MOCK_PHC_REFRESH_INTERVAL
);
165 EXPORT_SYMBOL_GPL(mock_phc_create
);
167 void mock_phc_destroy(struct mock_phc
*phc
)
169 ptp_clock_unregister(phc
->clock
);
172 EXPORT_SYMBOL_GPL(mock_phc_destroy
);
174 MODULE_DESCRIPTION("Mock-up PTP Hardware Clock driver");
175 MODULE_LICENSE("GPL");