xref: /linux/drivers/ptp/ptp_private.h (revision 63467137ecc0ff6f804d53903ad87a2f0397a18b)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * PTP 1588 clock support - private declarations for the core module.
4  *
5  * Copyright (C) 2010 OMICRON electronics GmbH
6  */
7 #ifndef _PTP_PRIVATE_H_
8 #define _PTP_PRIVATE_H_
9 
10 #include <linux/cdev.h>
11 #include <linux/device.h>
12 #include <linux/kthread.h>
13 #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 #include <linux/list.h>
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 
22 #define PTP_MAX_TIMESTAMPS 128
23 #define PTP_BUF_TIMESTAMPS 30
24 #define PTP_DEFAULT_MAX_VCLOCKS 20
25 #define PTP_MAX_CHANNELS 2048
26 
27 enum {
28 	PTP_LOCK_PHYSICAL = 0,
29 	PTP_LOCK_VIRTUAL,
30 };
31 
32 struct timestamp_event_queue {
33 	struct ptp_extts_event buf[PTP_MAX_TIMESTAMPS];
34 	int head;
35 	int tail;
36 	spinlock_t lock;
37 	struct list_head qlist;
38 	unsigned long *mask;
39 	struct dentry *debugfs_instance;
40 	struct debugfs_u32_array dfs_bitmap;
41 };
42 
43 struct ptp_clock {
44 	struct posix_clock clock;
45 	struct device dev;
46 	struct ptp_clock_info *info;
47 	dev_t devid;
48 	int index; /* index into clocks.map */
49 	struct pps_device *pps_source;
50 	long dialed_frequency; /* remembers the frequency adjustment */
51 	struct list_head tsevqs; /* timestamp fifo list */
52 	spinlock_t tsevqs_lock; /* protects tsevqs from concurrent access */
53 	struct mutex pincfg_mux; /* protect concurrent info->pin_config access */
54 	wait_queue_head_t tsev_wq;
55 	int defunct; /* tells readers to go away when clock is being removed */
56 	struct device_attribute *pin_dev_attr;
57 	struct attribute **pin_attr;
58 	struct attribute_group pin_attr_group;
59 	/* 1st entry is a pointer to the real group, 2nd is NULL terminator */
60 	const struct attribute_group *pin_attr_groups[2];
61 	struct kthread_worker *kworker;
62 	struct kthread_delayed_work aux_work;
63 	unsigned int max_vclocks;
64 	unsigned int n_vclocks;
65 	int *vclock_index;
66 	struct mutex n_vclocks_mux; /* protect concurrent n_vclocks access */
67 	bool is_virtual_clock;
68 	bool has_cycles;
69 	struct dentry *debugfs_root;
70 };
71 
72 #define info_to_vclock(d) container_of((d), struct ptp_vclock, info)
73 #define cc_to_vclock(d) container_of((d), struct ptp_vclock, cc)
74 #define dw_to_vclock(d) container_of((d), struct ptp_vclock, refresh_work)
75 
76 struct ptp_vclock {
77 	struct ptp_clock *pclock;
78 	struct ptp_clock_info info;
79 	struct ptp_clock *clock;
80 	struct hlist_node vclock_hash_node;
81 	struct cyclecounter cc;
82 	struct timecounter tc;
83 	struct mutex lock;	/* protects tc/cc */
84 };
85 
86 /*
87  * The function queue_cnt() is safe for readers to call without
88  * holding q->lock. Readers use this function to verify that the queue
89  * is nonempty before proceeding with a dequeue operation. The fact
90  * that a writer might concurrently increment the tail does not
91  * matter, since the queue remains nonempty nonetheless.
92  */
queue_cnt(const struct timestamp_event_queue * q)93 static inline int queue_cnt(const struct timestamp_event_queue *q)
94 {
95 	/*
96 	 * Paired with WRITE_ONCE() in enqueue_external_timestamp(),
97 	 * ptp_read(), extts_fifo_show().
98 	 */
99 	int cnt = READ_ONCE(q->tail) - READ_ONCE(q->head);
100 	return cnt < 0 ? PTP_MAX_TIMESTAMPS + cnt : cnt;
101 }
102 
103 /* Check if ptp virtual clock is in use */
ptp_vclock_in_use(struct ptp_clock * ptp)104 static inline bool ptp_vclock_in_use(struct ptp_clock *ptp)
105 {
106 	bool in_use = false;
107 
108 	/* Virtual clocks can't be stacked on top of virtual clocks.
109 	 * Avoid acquiring the n_vclocks_mux on virtual clocks, to allow this
110 	 * function to be called from code paths where the n_vclocks_mux of the
111 	 * parent physical clock is already held. Functionally that's not an
112 	 * issue, but lockdep would complain, because they have the same lock
113 	 * class.
114 	 */
115 	if (ptp->is_virtual_clock)
116 		return false;
117 
118 	if (mutex_lock_interruptible(&ptp->n_vclocks_mux))
119 		return true;
120 
121 	if (ptp->n_vclocks)
122 		in_use = true;
123 
124 	mutex_unlock(&ptp->n_vclocks_mux);
125 
126 	return in_use;
127 }
128 
129 /* Check if ptp clock shall be free running */
ptp_clock_freerun(struct ptp_clock * ptp)130 static inline bool ptp_clock_freerun(struct ptp_clock *ptp)
131 {
132 	if (ptp->has_cycles)
133 		return false;
134 
135 	return ptp_vclock_in_use(ptp);
136 }
137 
138 extern const struct class ptp_class;
139 
140 /*
141  * see ptp_chardev.c
142  */
143 
144 /* caller must hold pincfg_mux */
145 int ptp_set_pinfunc(struct ptp_clock *ptp, unsigned int pin,
146 		    enum ptp_pin_function func, unsigned int chan);
147 
148 long ptp_ioctl(struct posix_clock_context *pccontext, unsigned int cmd,
149 	       unsigned long arg);
150 
151 int ptp_open(struct posix_clock_context *pccontext, fmode_t fmode);
152 
153 int ptp_release(struct posix_clock_context *pccontext);
154 
155 ssize_t ptp_read(struct posix_clock_context *pccontext, uint flags, char __user *buf,
156 		 size_t cnt);
157 
158 __poll_t ptp_poll(struct posix_clock_context *pccontext, struct file *fp,
159 		  poll_table *wait);
160 
161 /*
162  * see ptp_sysfs.c
163  */
164 
165 extern const struct attribute_group *ptp_groups[];
166 
167 int ptp_populate_pin_groups(struct ptp_clock *ptp);
168 void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
169 
170 struct ptp_vclock *ptp_vclock_register(struct ptp_clock *pclock);
171 void ptp_vclock_unregister(struct ptp_vclock *vclock);
172 #endif
173