blob: 75f58fc468a7119f509522ea2a01e17c272d5d4d [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Richard Cochrand94ba802011-04-22 12:03:08 +02002/*
3 * PTP 1588 clock support - private declarations for the core module.
4 *
5 * Copyright (C) 2010 OMICRON electronics GmbH
Richard Cochrand94ba802011-04-22 12:03:08 +02006 */
7#ifndef _PTP_PRIVATE_H_
8#define _PTP_PRIVATE_H_
9
10#include <linux/cdev.h>
11#include <linux/device.h>
Grygorii Strashkod9535cb2017-07-28 17:30:02 -050012#include <linux/kthread.h>
Richard Cochrand94ba802011-04-22 12:03:08 +020013#include <linux/mutex.h>
14#include <linux/posix-clock.h>
15#include <linux/ptp_clock.h>
16#include <linux/ptp_clock_kernel.h>
17#include <linux/time.h>
18
19#define PTP_MAX_TIMESTAMPS 128
20#define PTP_BUF_TIMESTAMPS 30
Yangbo Lu73f37062021-06-30 16:11:53 +080021#define PTP_DEFAULT_MAX_VCLOCKS 20
Richard Cochrand94ba802011-04-22 12:03:08 +020022
23struct timestamp_event_queue {
24 struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
25 int head;
26 int tail;
27 spinlock_t lock;
28};
29
30struct ptp_clock {
31 struct posix_clock clock;
Vladis Dronova33121e2019-12-27 03:26:27 +010032 struct device dev;
Richard Cochrand94ba802011-04-22 12:03:08 +020033 struct ptp_clock_info *info;
34 dev_t devid;
35 int index; /* index into clocks.map */
36 struct pps_device *pps_source;
Richard Cochran39a8cbd2012-09-22 07:02:01 +000037 long dialed_frequency; /* remembers the frequency adjustment */
Richard Cochrand94ba802011-04-22 12:03:08 +020038 struct timestamp_event_queue tsevq; /* simple fifo for time stamps */
39 struct mutex tsevq_mux; /* one process at a time reading the fifo */
Richard Cochran60923152014-03-20 22:21:52 +010040 struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
Richard Cochrand94ba802011-04-22 12:03:08 +020041 wait_queue_head_t tsev_wq;
42 int defunct; /* tells readers to go away when clock is being removed */
Richard Cochran653104d2014-03-20 22:21:54 +010043 struct device_attribute *pin_dev_attr;
44 struct attribute **pin_attr;
45 struct attribute_group pin_attr_group;
Dmitry Torokhov85a66e52017-02-14 10:23:34 -080046 /* 1st entry is a pointer to the real group, 2nd is NULL terminator */
47 const struct attribute_group *pin_attr_groups[2];
Grygorii Strashkod9535cb2017-07-28 17:30:02 -050048 struct kthread_worker *kworker;
49 struct kthread_delayed_work aux_work;
Yangbo Lu73f37062021-06-30 16:11:53 +080050 unsigned int max_vclocks;
51 unsigned int n_vclocks;
Yangbo Lu44c494c2021-06-30 16:11:54 +080052 int *vclock_index;
Yangbo Lu73f37062021-06-30 16:11:53 +080053 struct mutex n_vclocks_mux; /* protect concurrent n_vclocks access */
54 bool is_virtual_clock;
Gerhard Engleder42704b22022-05-06 22:01:37 +020055 bool has_cycles;
Richard Cochrand94ba802011-04-22 12:03:08 +020056};
57
Yangbo Lu5d43f9512021-06-30 16:11:52 +080058#define info_to_vclock(d) container_of((d), struct ptp_vclock, info)
59#define cc_to_vclock(d) container_of((d), struct ptp_vclock, cc)
60#define dw_to_vclock(d) container_of((d), struct ptp_vclock, refresh_work)
61
62struct ptp_vclock {
63 struct ptp_clock *pclock;
64 struct ptp_clock_info info;
65 struct ptp_clock *clock;
Gerhard Englederfcf308e2022-05-06 22:01:41 +020066 struct hlist_node vclock_hash_node;
Yangbo Lu5d43f9512021-06-30 16:11:52 +080067 struct cyclecounter cc;
68 struct timecounter tc;
Íñigo Huguet67d93ff2023-02-21 14:06:16 +010069 struct mutex lock; /* protects tc/cc */
Yangbo Lu5d43f9512021-06-30 16:11:52 +080070};
71
Richard Cochrand94ba802011-04-22 12:03:08 +020072/*
73 * The function queue_cnt() is safe for readers to call without
74 * holding q->lock. Readers use this function to verify that the queue
75 * is nonempty before proceeding with a dequeue operation. The fact
76 * that a writer might concurrently increment the tail does not
77 * matter, since the queue remains nonempty nonetheless.
78 */
79static inline int queue_cnt(struct timestamp_event_queue *q)
80{
81 int cnt = q->tail - q->head;
82 return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
83}
84
Yangbo Lu73f37062021-06-30 16:11:53 +080085/* Check if ptp virtual clock is in use */
86static inline bool ptp_vclock_in_use(struct ptp_clock *ptp)
87{
88 bool in_use = false;
89
90 if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
91 return true;
92
93 if (!ptp->is_virtual_clock && ptp->n_vclocks)
94 in_use = true;
95
96 mutex_unlock(&ptp->n_vclocks_mux);
97
98 return in_use;
99}
100
Gerhard Engleder42704b22022-05-06 22:01:37 +0200101/* Check if ptp clock shall be free running */
102static inline bool ptp_clock_freerun(struct ptp_clock *ptp)
103{
104 if (ptp->has_cycles)
105 return false;
106
107 return ptp_vclock_in_use(ptp);
108}
109
Yangbo Luacb288e2021-06-30 16:11:55 +0800110extern struct class *ptp_class;
111
Richard Cochrand94ba802011-04-22 12:03:08 +0200112/*
113 * see ptp_chardev.c
114 */
115
Richard Cochran60923152014-03-20 22:21:52 +0100116/* caller must hold pincfg_mux */
117int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
118 enum ptp_pin_function func, unsigned int chan);
119
Richard Cochrand94ba802011-04-22 12:03:08 +0200120long ptp_ioctl(struct posix_clock *pc,
121 unsigned int cmd, unsigned long arg);
122
123int ptp_open(struct posix_clock *pc, fmode_t fmode);
124
125ssize_t ptp_read(struct posix_clock *pc,
126 uint flags, char __user *buf, size_t cnt);
127
Al Viroafc9a422017-07-03 06:39:46 -0400128__poll_t ptp_poll(struct posix_clock *pc,
Richard Cochrand94ba802011-04-22 12:03:08 +0200129 struct file *fp, poll_table *wait);
130
131/*
132 * see ptp_sysfs.c
133 */
134
Greg Kroah-Hartman34991162013-07-24 15:05:20 -0700135extern const struct attribute_group *ptp_groups[];
Richard Cochrand94ba802011-04-22 12:03:08 +0200136
Dmitry Torokhov85a66e52017-02-14 10:23:34 -0800137int ptp_populate_pin_groups(struct ptp_clock *ptp);
138void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
Richard Cochrand94ba802011-04-22 12:03:08 +0200139
Yangbo Lu5d43f9512021-06-30 16:11:52 +0800140struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock);
141void ptp_vclock_unregister(struct ptp_vclock *vclock);
Richard Cochrand94ba802011-04-22 12:03:08 +0200142#endif