ptp_private.h 4.9 KB

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