xref: /openbmc/linux/drivers/ptp/ptp_clock.c (revision 61ae993c)
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 
37 static inline int queue_free(struct timestamp_event_queue *q)
38 {
39 	return PTP_MAX_TIMESTAMPS - queue_cnt(q) - 1;
40 }
41 
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 
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 
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 
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 
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 		ptp->dialed_frequency = tx->freq;
137 	} else if (tx->modes & ADJ_OFFSET) {
138 		if (ops->adjphase) {
139 			s32 max_phase_adj = ops->getmaxphase(ops);
140 			s32 offset = tx->offset;
141 
142 			if (!(tx->modes & ADJ_NANO))
143 				offset *= NSEC_PER_USEC;
144 
145 			if (offset > max_phase_adj || offset < -max_phase_adj)
146 				return -ERANGE;
147 
148 			err = ops->adjphase(ops, offset);
149 		}
150 	} else if (tx->modes == 0) {
151 		tx->freq = ptp->dialed_frequency;
152 		err = 0;
153 	}
154 
155 	return err;
156 }
157 
158 static struct posix_clock_operations ptp_clock_ops = {
159 	.owner		= THIS_MODULE,
160 	.clock_adjtime	= ptp_clock_adjtime,
161 	.clock_gettime	= ptp_clock_gettime,
162 	.clock_getres	= ptp_clock_getres,
163 	.clock_settime	= ptp_clock_settime,
164 	.ioctl		= ptp_ioctl,
165 	.open		= ptp_open,
166 	.poll		= ptp_poll,
167 	.read		= ptp_read,
168 };
169 
170 static void ptp_clock_release(struct device *dev)
171 {
172 	struct ptp_clock *ptp = container_of(dev, struct ptp_clock, dev);
173 
174 	ptp_cleanup_pin_groups(ptp);
175 	kfree(ptp->vclock_index);
176 	mutex_destroy(&ptp->tsevq_mux);
177 	mutex_destroy(&ptp->pincfg_mux);
178 	mutex_destroy(&ptp->n_vclocks_mux);
179 	ida_free(&ptp_clocks_map, ptp->index);
180 	kfree(ptp);
181 }
182 
183 static int ptp_getcycles64(struct ptp_clock_info *info, struct timespec64 *ts)
184 {
185 	if (info->getcyclesx64)
186 		return info->getcyclesx64(info, ts, NULL);
187 	else
188 		return info->gettime64(info, ts);
189 }
190 
191 static void ptp_aux_kworker(struct kthread_work *work)
192 {
193 	struct ptp_clock *ptp = container_of(work, struct ptp_clock,
194 					     aux_work.work);
195 	struct ptp_clock_info *info = ptp->info;
196 	long delay;
197 
198 	delay = info->do_aux_work(info);
199 
200 	if (delay >= 0)
201 		kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay);
202 }
203 
204 /* public interface */
205 
206 struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
207 				     struct device *parent)
208 {
209 	struct ptp_clock *ptp;
210 	int err = 0, index, major = MAJOR(ptp_devt);
211 	size_t size;
212 
213 	if (info->n_alarm > PTP_MAX_ALARMS)
214 		return ERR_PTR(-EINVAL);
215 
216 	/* Initialize a clock structure. */
217 	err = -ENOMEM;
218 	ptp = kzalloc(sizeof(struct ptp_clock), GFP_KERNEL);
219 	if (ptp == NULL)
220 		goto no_memory;
221 
222 	index = ida_alloc_max(&ptp_clocks_map, MINORMASK, GFP_KERNEL);
223 	if (index < 0) {
224 		err = index;
225 		goto no_slot;
226 	}
227 
228 	ptp->clock.ops = ptp_clock_ops;
229 	ptp->info = info;
230 	ptp->devid = MKDEV(major, index);
231 	ptp->index = index;
232 	spin_lock_init(&ptp->tsevq.lock);
233 	mutex_init(&ptp->tsevq_mux);
234 	mutex_init(&ptp->pincfg_mux);
235 	mutex_init(&ptp->n_vclocks_mux);
236 	init_waitqueue_head(&ptp->tsev_wq);
237 
238 	if (ptp->info->getcycles64 || ptp->info->getcyclesx64) {
239 		ptp->has_cycles = true;
240 		if (!ptp->info->getcycles64 && ptp->info->getcyclesx64)
241 			ptp->info->getcycles64 = ptp_getcycles64;
242 	} else {
243 		/* Free running cycle counter not supported, use time. */
244 		ptp->info->getcycles64 = ptp_getcycles64;
245 
246 		if (ptp->info->gettimex64)
247 			ptp->info->getcyclesx64 = ptp->info->gettimex64;
248 
249 		if (ptp->info->getcrosststamp)
250 			ptp->info->getcrosscycles = ptp->info->getcrosststamp;
251 	}
252 
253 	if (ptp->info->do_aux_work) {
254 		kthread_init_delayed_work(&ptp->aux_work, ptp_aux_kworker);
255 		ptp->kworker = kthread_create_worker(0, "ptp%d", ptp->index);
256 		if (IS_ERR(ptp->kworker)) {
257 			err = PTR_ERR(ptp->kworker);
258 			pr_err("failed to create ptp aux_worker %d\n", err);
259 			goto kworker_err;
260 		}
261 	}
262 
263 	/* PTP virtual clock is being registered under physical clock */
264 	if (parent && parent->class && parent->class->name &&
265 	    strcmp(parent->class->name, "ptp") == 0)
266 		ptp->is_virtual_clock = true;
267 
268 	if (!ptp->is_virtual_clock) {
269 		ptp->max_vclocks = PTP_DEFAULT_MAX_VCLOCKS;
270 
271 		size = sizeof(int) * ptp->max_vclocks;
272 		ptp->vclock_index = kzalloc(size, GFP_KERNEL);
273 		if (!ptp->vclock_index) {
274 			err = -ENOMEM;
275 			goto no_mem_for_vclocks;
276 		}
277 	}
278 
279 	err = ptp_populate_pin_groups(ptp);
280 	if (err)
281 		goto no_pin_groups;
282 
283 	/* Register a new PPS source. */
284 	if (info->pps) {
285 		struct pps_source_info pps;
286 		memset(&pps, 0, sizeof(pps));
287 		snprintf(pps.name, PPS_MAX_NAME_LEN, "ptp%d", index);
288 		pps.mode = PTP_PPS_MODE;
289 		pps.owner = info->owner;
290 		ptp->pps_source = pps_register_source(&pps, PTP_PPS_DEFAULTS);
291 		if (IS_ERR(ptp->pps_source)) {
292 			err = PTR_ERR(ptp->pps_source);
293 			pr_err("failed to register pps source\n");
294 			goto no_pps;
295 		}
296 		ptp->pps_source->lookup_cookie = ptp;
297 	}
298 
299 	/* Initialize a new device of our class in our clock structure. */
300 	device_initialize(&ptp->dev);
301 	ptp->dev.devt = ptp->devid;
302 	ptp->dev.class = ptp_class;
303 	ptp->dev.parent = parent;
304 	ptp->dev.groups = ptp->pin_attr_groups;
305 	ptp->dev.release = ptp_clock_release;
306 	dev_set_drvdata(&ptp->dev, ptp);
307 	dev_set_name(&ptp->dev, "ptp%d", ptp->index);
308 
309 	/* Create a posix clock and link it to the device. */
310 	err = posix_clock_register(&ptp->clock, &ptp->dev);
311 	if (err) {
312 		if (ptp->pps_source)
313 			pps_unregister_source(ptp->pps_source);
314 
315 		if (ptp->kworker)
316 			kthread_destroy_worker(ptp->kworker);
317 
318 		put_device(&ptp->dev);
319 
320 		pr_err("failed to create posix clock\n");
321 		return ERR_PTR(err);
322 	}
323 
324 	return ptp;
325 
326 no_pps:
327 	ptp_cleanup_pin_groups(ptp);
328 no_pin_groups:
329 	kfree(ptp->vclock_index);
330 no_mem_for_vclocks:
331 	if (ptp->kworker)
332 		kthread_destroy_worker(ptp->kworker);
333 kworker_err:
334 	mutex_destroy(&ptp->tsevq_mux);
335 	mutex_destroy(&ptp->pincfg_mux);
336 	mutex_destroy(&ptp->n_vclocks_mux);
337 	ida_free(&ptp_clocks_map, index);
338 no_slot:
339 	kfree(ptp);
340 no_memory:
341 	return ERR_PTR(err);
342 }
343 EXPORT_SYMBOL(ptp_clock_register);
344 
345 static int unregister_vclock(struct device *dev, void *data)
346 {
347 	struct ptp_clock *ptp = dev_get_drvdata(dev);
348 
349 	ptp_vclock_unregister(info_to_vclock(ptp->info));
350 	return 0;
351 }
352 
353 int ptp_clock_unregister(struct ptp_clock *ptp)
354 {
355 	if (ptp_vclock_in_use(ptp)) {
356 		device_for_each_child(&ptp->dev, NULL, unregister_vclock);
357 	}
358 
359 	ptp->defunct = 1;
360 	wake_up_interruptible(&ptp->tsev_wq);
361 
362 	if (ptp->kworker) {
363 		kthread_cancel_delayed_work_sync(&ptp->aux_work);
364 		kthread_destroy_worker(ptp->kworker);
365 	}
366 
367 	/* Release the clock's resources. */
368 	if (ptp->pps_source)
369 		pps_unregister_source(ptp->pps_source);
370 
371 	posix_clock_unregister(&ptp->clock);
372 
373 	return 0;
374 }
375 EXPORT_SYMBOL(ptp_clock_unregister);
376 
377 void ptp_clock_event(struct ptp_clock *ptp, struct ptp_clock_event *event)
378 {
379 	struct pps_event_time evt;
380 
381 	switch (event->type) {
382 
383 	case PTP_CLOCK_ALARM:
384 		break;
385 
386 	case PTP_CLOCK_EXTTS:
387 		enqueue_external_timestamp(&ptp->tsevq, event);
388 		wake_up_interruptible(&ptp->tsev_wq);
389 		break;
390 
391 	case PTP_CLOCK_PPS:
392 		pps_get_ts(&evt);
393 		pps_event(ptp->pps_source, &evt, PTP_PPS_EVENT, NULL);
394 		break;
395 
396 	case PTP_CLOCK_PPSUSR:
397 		pps_event(ptp->pps_source, &event->pps_times,
398 			  PTP_PPS_EVENT, NULL);
399 		break;
400 	}
401 }
402 EXPORT_SYMBOL(ptp_clock_event);
403 
404 int ptp_clock_index(struct ptp_clock *ptp)
405 {
406 	return ptp->index;
407 }
408 EXPORT_SYMBOL(ptp_clock_index);
409 
410 int ptp_find_pin(struct ptp_clock *ptp,
411 		 enum ptp_pin_function func, unsigned int chan)
412 {
413 	struct ptp_pin_desc *pin = NULL;
414 	int i;
415 
416 	for (i = 0; i < ptp->info->n_pins; i++) {
417 		if (ptp->info->pin_config[i].func == func &&
418 		    ptp->info->pin_config[i].chan == chan) {
419 			pin = &ptp->info->pin_config[i];
420 			break;
421 		}
422 	}
423 
424 	return pin ? i : -1;
425 }
426 EXPORT_SYMBOL(ptp_find_pin);
427 
428 int ptp_find_pin_unlocked(struct ptp_clock *ptp,
429 			  enum ptp_pin_function func, unsigned int chan)
430 {
431 	int result;
432 
433 	mutex_lock(&ptp->pincfg_mux);
434 
435 	result = ptp_find_pin(ptp, func, chan);
436 
437 	mutex_unlock(&ptp->pincfg_mux);
438 
439 	return result;
440 }
441 EXPORT_SYMBOL(ptp_find_pin_unlocked);
442 
443 int ptp_schedule_worker(struct ptp_clock *ptp, unsigned long delay)
444 {
445 	return kthread_mod_delayed_work(ptp->kworker, &ptp->aux_work, delay);
446 }
447 EXPORT_SYMBOL(ptp_schedule_worker);
448 
449 void ptp_cancel_worker_sync(struct ptp_clock *ptp)
450 {
451 	kthread_cancel_delayed_work_sync(&ptp->aux_work);
452 }
453 EXPORT_SYMBOL(ptp_cancel_worker_sync);
454 
455 /* module operations */
456 
457 static void __exit ptp_exit(void)
458 {
459 	class_destroy(ptp_class);
460 	unregister_chrdev_region(ptp_devt, MINORMASK + 1);
461 	ida_destroy(&ptp_clocks_map);
462 }
463 
464 static int __init ptp_init(void)
465 {
466 	int err;
467 
468 	ptp_class = class_create("ptp");
469 	if (IS_ERR(ptp_class)) {
470 		pr_err("ptp: failed to allocate class\n");
471 		return PTR_ERR(ptp_class);
472 	}
473 
474 	err = alloc_chrdev_region(&ptp_devt, 0, MINORMASK + 1, "ptp");
475 	if (err < 0) {
476 		pr_err("ptp: failed to allocate device region\n");
477 		goto no_region;
478 	}
479 
480 	ptp_class->dev_groups = ptp_groups;
481 	pr_info("PTP clock support registered\n");
482 	return 0;
483 
484 no_region:
485 	class_destroy(ptp_class);
486 	return err;
487 }
488 
489 subsys_initcall(ptp_init);
490 module_exit(ptp_exit);
491 
492 MODULE_AUTHOR("Richard Cochran <richardcochran@gmail.com>");
493 MODULE_DESCRIPTION("PTP clocks support");
494 MODULE_LICENSE("GPL");
495