1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PTP 1588 clock support
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
6 */
7 #include <linux/idr.h>
8 #include <linux/device.h>
9 #include <linux/err.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/posix-clock.h>
14 #include <linux/pps_kernel.h>
15 #include <linux/slab.h>
16 #include <linux/syscalls.h>
17 #include <linux/uaccess.h>
18 #include <uapi/linux/sched/types.h>
19
20 #include "ptp_private.h"
21
22 #define PTP_MAX_ALARMS 4
23 #define PTP_PPS_DEFAULTS (PPS_CAPTUREASSERT | PPS_OFFSETASSERT)
24 #define PTP_PPS_EVENT PPS_CAPTUREASSERT
25 #define PTP_PPS_MODE (PTP_PPS_DEFAULTS | PPS_CANWAIT | PPS_TSFMT_TSPEC)
26
27 struct class *ptp_class;
28
29 /* private globals */
30
31 static dev_t ptp_devt;
32
33 static DEFINE_IDA(ptp_clocks_map);
34
35 /* time stamp event queue operations */
36
queue_free(struct timestamp_event_queue * q)37 static inline int queue_free(struct timestamp_event_queue *q)
38 {
39 return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
40 }
41
enqueue_external_timestamp(struct timestamp_event_queue * queue,struct ptp_clock_event * src)42 static void enqueue_external_timestamp(struct timestamp_event_queue *queue,
43 struct ptp_clock_event *src)
44 {
45 struct ptp_extts_event *dst;
46 unsigned long flags;
47 s64 seconds;
48 u32 remainder;
49
50 seconds = div_u64_rem(src->timestamp, 1000000000, &remainder);
51
52 spin_lock_irqsave(&queue->lock, flags);
53
54 dst = &queue->buf[queue->tail];
55 dst->index = src->index;
56 dst->t.sec = seconds;
57 dst->t.nsec = remainder;
58
59 /* Both WRITE_ONCE() are paired with READ_ONCE() in queue_cnt() */
60 if (!queue_free(queue))
61 WRITE_ONCE(queue->head, (queue->head + 1) % PTP_MAX_TIMESTAMPS);
62
63 WRITE_ONCE(queue->tail, (queue->tail + 1) % PTP_MAX_TIMESTAMPS);
64
65 spin_unlock_irqrestore(&queue->lock, flags);
66 }
67
68 /* posix clock implementation */
69
ptp_clock_getres(struct posix_clock * pc,struct timespec64 * tp)70 static int ptp_clock_getres(struct posix_clock *pc, struct timespec64 *tp)
71 {
72 tp->tv_sec = 0;
73 tp->tv_nsec = 1;
74 return 0;
75 }
76
ptp_clock_settime(struct posix_clock * pc,const struct timespec64 * tp)77 static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp)
78 {
79 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
80
81 if (ptp_clock_freerun(ptp)) {
82 pr_err("ptp: physical clock is free running\n");
83 return -EBUSY;
84 }
85
86 return ptp->info->settime64(ptp->info, tp);
87 }
88
ptp_clock_gettime(struct posix_clock * pc,struct timespec64 * tp)89 static int ptp_clock_gettime(struct posix_clock *pc, struct timespec64 *tp)
90 {
91 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
92 int err;
93
94 if (ptp->info->gettimex64)
95 err = ptp->info->gettimex64(ptp->info, tp, NULL);
96 else
97 err = ptp->info->gettime64(ptp->info, tp);
98 return err;
99 }
100
ptp_clock_adjtime(struct posix_clock * pc,struct __kernel_timex * tx)101 static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
102 {
103 struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
104 struct ptp_clock_info *ops;
105 int err = -EOPNOTSUPP;
106
107 if (ptp_clock_freerun(ptp)) {
108 pr_err("ptp: physical clock is free running\n");
109 return -EBUSY;
110 }
111
112 ops = ptp->info;
113
114 if (tx->modes & ADJ_SETOFFSET) {
115 struct timespec64 ts;
116 ktime_t kt;
117 s64 delta;
118
119 ts.tv_sec = tx->time.tv_sec;
120 ts.tv_nsec = tx->time.tv_usec;
121
122 if (!(tx->modes & ADJ_NANO))
123 ts.tv_nsec *= 1000;
124
125 if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC)
126 return -EINVAL;
127
128 kt = timespec64_to_ktime(ts);
129 delta = ktime_to_ns(kt);
130 err = ops->adjtime(ops, delta);
131 } else if (tx->modes & ADJ_FREQUENCY) {
132 long ppb = scaled_ppm_to_ppb(tx->freq);
133 if (ppb > ops->max_adj || ppb < -ops->max_adj)
134 return -ERANGE;
135 err = ops->adjfine(ops, tx->freq);
136 if (!err)
137 ptp->dialed_frequency = tx->freq;
138 } else if (tx->modes & ADJ_OFFSET) {
139 if (ops->adjphase) {
140 s32 max_phase_adj = ops->getmaxphase(ops);
141 s32 offset = tx->offset;
142
143 if (!(tx->modes & ADJ_NANO))
144 offset *= NSEC_PER_USEC;
145
146 if (offset > max_phase_adj || offset < -max_phase_adj)
147 return -ERANGE;
148
149 err = ops->adjphase(ops, offset);
150 }
151 } else if (tx->modes == 0) {
152 tx->freq = ptp->dialed_frequency;
153 err = 0;
154 }
155
156 return err;
157 }
158
159 static struct posix_clock_operations ptp_clock_ops = {
160 .owner = THIS_MODULE,
161 .clock_adjtime = ptp_clock_adjtime,
162 .clock_gettime = ptp_clock_gettime,
163 .clock_getres = ptp_clock_getres,
164 .clock_settime = ptp_clock_settime,
165 .ioctl = ptp_ioctl,
166 .open = ptp_open,
167 .poll = ptp_poll,
168 .read = ptp_read,
169 };
170
ptp_clock_release(struct device * dev)171 static void ptp_clock_release(struct device *dev)
172 {
173 struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
174
175 ptp_cleanup_pin_groups(ptp);
176 kfree(ptp->vclock_index);
177 mutex_destroy(&ptp->tsevq_mux);
178 mutex_destroy(&ptp->pincfg_mux);
179 mutex_destroy(&ptp->n_vclocks_mux);
180 ida_free(&ptp_clocks_map, ptp->index);
181 kfree(ptp);
182 }
183
ptp_getcycles64(struct ptp_clock_info * info,struct timespec64 * ts)184 static int ptp_getcycles64(struct ptp_clock_info *info, struct timespec64 *ts)
185 {
186 if (info->getcyclesx64)
187 return info->getcyclesx64(info, ts, NULL);
188 else
189 return info->gettime64(info, ts);
190 }
191
ptp_enable(struct ptp_clock_info * ptp,struct ptp_clock_request * request,int on)192 static int ptp_enable(struct ptp_clock_info *ptp, struct ptp_clock_request *request, int on)
193 {
194 return -EOPNOTSUPP;
195 }
196
ptp_aux_kworker(struct kthread_work * work)197 static void ptp_aux_kworker(struct kthread_work *work)
198 {
199 struct ptp_clock *ptp = container_of(work, struct ptp_clock,
200 aux_work.work);
201 struct ptp_clock_info *info = ptp->info;
202 long delay;
203
204 delay = info->do_aux_work(info);
205
206 if (delay >= 0)
207 kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay);
208 }
209
210 /* public interface */
211
ptp_clock_register(struct ptp_clock_info * info,struct device * parent)212 struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
213 struct device *parent)
214 {
215 struct ptp_clock *ptp;
216 int err = 0, index, major = MAJOR(ptp_devt);
217 size_t size;
218
219 if (info->n_alarm > PTP_MAX_ALARMS)
220 return ERR_PTR(-EINVAL);
221
222 /* Initialize a clock structure. */
223 err = -ENOMEM;
224 ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL);
225 if (ptp == NULL)
226 goto no_memory;
227
228 index = ida_alloc_max(&ptp_clocks_map, MINORMASK, GFP_KERNEL);
229 if (index < 0) {
230 err = index;
231 goto no_slot;
232 }
233
234 ptp->clock.ops = ptp_clock_ops;
235 ptp->info = info;
236 ptp->devid = MKDEV(major, index);
237 ptp->index = index;
238 spin_lock_init(&ptp->tsevq.lock);
239 mutex_init(&ptp->tsevq_mux);
240 mutex_init(&ptp->pincfg_mux);
241 mutex_init(&ptp->n_vclocks_mux);
242 init_waitqueue_head(&ptp->tsev_wq);
243
244 if (ptp->info->getcycles64 || ptp->info->getcyclesx64) {
245 ptp->has_cycles = true;
246 if (!ptp->info->getcycles64 && ptp->info->getcyclesx64)
247 ptp->info->getcycles64 = ptp_getcycles64;
248 } else {
249 /* Free running cycle counter not supported, use time. */
250 ptp->info->getcycles64 = ptp_getcycles64;
251
252 if (ptp->info->gettimex64)
253 ptp->info->getcyclesx64 = ptp->info->gettimex64;
254
255 if (ptp->info->getcrosststamp)
256 ptp->info->getcrosscycles = ptp->info->getcrosststamp;
257 }
258
259 if (!ptp->info->enable)
260 ptp->info->enable = ptp_enable;
261
262 if (ptp->info->do_aux_work) {
263 kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker);
264 ptp->kworker = kthread_create_worker(0, "ptp%d", ptp->index);
265 if (IS_ERR(ptp->kworker)) {
266 err = PTR_ERR(ptp->kworker);
267 pr_err("failed to create ptp aux_worker %d\n", err);
268 goto kworker_err;
269 }
270 }
271
272 /* PTP virtual clock is being registered under physical clock */
273 if (parent && parent->class && parent->class->name &&
274 strcmp(parent->class->name, "ptp") == 0)
275 ptp->is_virtual_clock = true;
276
277 if (!ptp->is_virtual_clock) {
278 ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS;
279
280 size = sizeof(int) * ptp->max_vclocks;
281 ptp->vclock_index = kzalloc(size, GFP_KERNEL);
282 if (!ptp->vclock_index) {
283 err = -ENOMEM;
284 goto no_mem_for_vclocks;
285 }
286 }
287
288 err = ptp_populate_pin_groups(ptp);
289 if (err)
290 goto no_pin_groups;
291
292 /* Register a new PPS source. */
293 if (info->pps) {
294 struct pps_source_info pps;
295 memset(&pps, 0, sizeof(pps));
296 snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
297 pps.mode = PTP_PPS_MODE;
298 pps.owner = info->owner;
299 ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
300 if (IS_ERR(ptp->pps_source)) {
301 err = PTR_ERR(ptp->pps_source);
302 pr_err("failed to register pps source\n");
303 goto no_pps;
304 }
305 ptp->pps_source->lookup_cookie = ptp;
306 }
307
308 /* Initialize a new device of our class in our clock structure. */
309 device_initialize(&ptp->dev);
310 ptp->dev.devt = ptp->devid;
311 ptp->dev.class = ptp_class;
312 ptp->dev.parent = parent;
313 ptp->dev.groups = ptp->pin_attr_groups;
314 ptp->dev.release = ptp_clock_release;
315 dev_set_drvdata(&ptp->dev, ptp);
316 dev_set_name(&ptp->dev, "ptp%d", ptp->index);
317
318 /* Create a posix clock and link it to the device. */
319 err = posix_clock_register(&ptp->clock, &ptp->dev);
320 if (err) {
321 if (ptp->pps_source)
322 pps_unregister_source(ptp->pps_source);
323
324 if (ptp->kworker)
325 kthread_destroy_worker(ptp->kworker);
326
327 put_device(&ptp->dev);
328
329 pr_err("failed to create posix clock\n");
330 return ERR_PTR(err);
331 }
332
333 return ptp;
334
335 no_pps:
336 ptp_cleanup_pin_groups(ptp);
337 no_pin_groups:
338 kfree(ptp->vclock_index);
339 no_mem_for_vclocks:
340 if (ptp->kworker)
341 kthread_destroy_worker(ptp->kworker);
342 kworker_err:
343 mutex_destroy(&ptp->tsevq_mux);
344 mutex_destroy(&ptp->pincfg_mux);
345 mutex_destroy(&ptp->n_vclocks_mux);
346 ida_free(&ptp_clocks_map, index);
347 no_slot:
348 kfree(ptp);
349 no_memory:
350 return ERR_PTR(err);
351 }
352 EXPORT_SYMBOL(ptp_clock_register);
353
unregister_vclock(struct device * dev,void * data)354 static int unregister_vclock(struct device *dev, void *data)
355 {
356 struct ptp_clock *ptp = dev_get_drvdata(dev);
357
358 ptp_vclock_unregister(info_to_vclock(ptp->info));
359 return 0;
360 }
361
ptp_clock_unregister(struct ptp_clock * ptp)362 int ptp_clock_unregister(struct ptp_clock *ptp)
363 {
364 if (ptp_vclock_in_use(ptp)) {
365 device_for_each_child(&ptp->dev, NULL, unregister_vclock);
366 }
367
368 ptp->defunct = 1;
369 wake_up_interruptible(&ptp->tsev_wq);
370
371 if (ptp->kworker) {
372 kthread_cancel_delayed_work_sync(&ptp->aux_work);
373 kthread_destroy_worker(ptp->kworker);
374 }
375
376 /* Release the clock's resources. */
377 if (ptp->pps_source)
378 pps_unregister_source(ptp->pps_source);
379
380 posix_clock_unregister(&ptp->clock);
381
382 return 0;
383 }
384 EXPORT_SYMBOL(ptp_clock_unregister);
385
ptp_clock_event(struct ptp_clock * ptp,struct ptp_clock_event * event)386 void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
387 {
388 struct pps_event_time evt;
389
390 switch (event->type) {
391
392 case PTP_CLOCK_ALARM:
393 break;
394
395 case PTP_CLOCK_EXTTS:
396 enqueue_external_timestamp(&ptp->tsevq, event);
397 wake_up_interruptible(&ptp->tsev_wq);
398 break;
399
400 case PTP_CLOCK_PPS:
401 pps_get_ts(&evt);
402 pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
403 break;
404
405 case PTP_CLOCK_PPSUSR:
406 pps_event(ptp->pps_source, &event->pps_times,
407 PTP_PPS_EVENT, NULL);
408 break;
409 }
410 }
411 EXPORT_SYMBOL(ptp_clock_event);
412
ptp_clock_index(struct ptp_clock * ptp)413 int ptp_clock_index(struct ptp_clock *ptp)
414 {
415 return ptp->index;
416 }
417 EXPORT_SYMBOL(ptp_clock_index);
418
ptp_find_pin(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)419 int ptp_find_pin(struct ptp_clock *ptp,
420 enum ptp_pin_function func, unsigned int chan)
421 {
422 struct ptp_pin_desc *pin = NULL;
423 int i;
424
425 for (i = 0; i < ptp->info->n_pins; i++) {
426 if (ptp->info->pin_config[i].func == func &&
427 ptp->info->pin_config[i].chan == chan) {
428 pin = &ptp->info->pin_config[i];
429 break;
430 }
431 }
432
433 return pin ? i : -1;
434 }
435 EXPORT_SYMBOL(ptp_find_pin);
436
ptp_find_pin_unlocked(struct ptp_clock * ptp,enum ptp_pin_function func,unsigned int chan)437 int ptp_find_pin_unlocked(struct ptp_clock *ptp,
438 enum ptp_pin_function func, unsigned int chan)
439 {
440 int result;
441
442 mutex_lock(&ptp->pincfg_mux);
443
444 result = ptp_find_pin(ptp, func, chan);
445
446 mutex_unlock(&ptp->pincfg_mux);
447
448 return result;
449 }
450 EXPORT_SYMBOL(ptp_find_pin_unlocked);
451
ptp_schedule_worker(struct ptp_clock * ptp,unsigned long delay)452 int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay)
453 {
454 return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay);
455 }
456 EXPORT_SYMBOL(ptp_schedule_worker);
457
ptp_cancel_worker_sync(struct ptp_clock * ptp)458 void ptp_cancel_worker_sync(struct ptp_clock *ptp)
459 {
460 kthread_cancel_delayed_work_sync(&ptp->aux_work);
461 }
462 EXPORT_SYMBOL(ptp_cancel_worker_sync);
463
464 /* module operations */
465
ptp_exit(void)466 static void __exit ptp_exit(void)
467 {
468 class_destroy(ptp_class);
469 unregister_chrdev_region(ptp_devt, MINORMASK + 1);
470 ida_destroy(&ptp_clocks_map);
471 }
472
ptp_init(void)473 static int __init ptp_init(void)
474 {
475 int err;
476
477 ptp_class = class_create("ptp");
478 if (IS_ERR(ptp_class)) {
479 pr_err("ptp: failed to allocate class\n");
480 return PTR_ERR(ptp_class);
481 }
482
483 err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
484 if (err < 0) {
485 pr_err("ptp: failed to allocate device region\n");
486 goto no_region;
487 }
488
489 ptp_class->dev_groups = ptp_groups;
490 pr_info("PTP clock support registered\n");
491 return 0;
492
493 no_region:
494 class_destroy(ptp_class);
495 return err;
496 }
497
498 subsys_initcall(ptp_init);
499 module_exit(ptp_exit);
500
501 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
502 MODULE_DESCRIPTION("PTP clocks support");
503 MODULE_LICENSE("GPL");
504