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