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