Skip to contentSkip to conversation

Add support for Intel PMC Time GPIO Driver with PHC interface changes to support additional H/W Features

PATCH In review [email protected] 9 participants 36 messages Jan 31, 2020 – Mar 8, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Add support for Intel PMC Time GPIO Driver with PHC interface changes to support additional H/W Features

From: Christopher Hall <[email protected]>

Upcoming Intel platforms will have Time-Aware GPIO (TGPIO) hardware. The TGPIO logic is driven by the Always Running Timer (ART) that's related to TSC using CPUID[15H] (See Intel SDM Invariant Time-Keeping).

The ART frequency is not adjustable. In order, to implement output adjustments an additional edge-timestamp API is added, as well, as a periodic output frequency adjustment API. Togther, these implement equivalent functionality to the existing SYS_OFFSET_* and frequency adjustment APIs.

The TGPIO hardware doesn't implement interrupts. For TGPIO input, the output edge-timestamp API is re-used to implement a user-space polling interface. For periodic input (e.g. PPS) this is fairly efficient, requiring only a marginally faster poll rate than the input event frequency.

Acknowledgment: Portions of the driver code were authored by Felipe Balbi <[email protected]>

=======================================================================

Christopher Hall (5):
drivers/ptp: Add Enhanced handling of reserve fields drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface drivers/ptp: Add user-space input polling interface x86/tsc: Add TSC support functions to support ART driven Time-Aware
GPIO
drivers/ptp: Add PMC Time-Aware GPIO Driver

 arch/x86/include/asm/tsc.h        |   6 +
 arch/x86/kernel/tsc.c             | 116 +++-
 drivers/ptp/Kconfig               |  13 +
 drivers/ptp/Makefile              |   1 +
 drivers/ptp/ptp-intel-pmc-tgpio.c | 867 ++++++++++++++++++++++++++++++
 drivers/ptp/ptp_chardev.c         |  86 ++-
 drivers/ptp/ptp_clock.c           |  13 +
 include/linux/ptp_clock_kernel.h  |   2 +
 include/uapi/linux/ptp_clock.h    |  26 +-
 9 files changed, 1099 insertions(+), 31 deletions(-)
 create mode 100644 drivers/ptp/ptp-intel-pmc-tgpio.c
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/5 drivers/ptp: Add Enhanced handling of reserve fields

From: Christopher Hall <[email protected]>

Add functions that parameterize checking and zeroing of reserve fields in ioctl arguments. Eliminates need to change this code when repurposing reserve fields.

---
 drivers/ptp/ptp_chardev.c | 60 +++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 27 deletions(-)
drivers/ptp/ptp_chardev.c +33 -27
index 9d72ab593f13..f9ad6df57fa5 100644
@@ -12,6 +12,7 @@
 #include <linux/timekeeping.h>
 
 #include <linux/nospec.h>
+#include <linux/string.h>
 
 #include "ptp_private.h"
 
@@ -106,6 +107,28 @@ int ptp_open(struct posix_clock *pc, fmode_t fmode)
 	return 0;
 }
 
+/* Returns -1 if any reserved fields are non-zero */
+static inline int _check_rsv_field(unsigned int *field, size_t size)
+{
+	unsigned int *iter;
+	int ret = 0;
+
+	for (iter = field; iter < field+size && ret == 0; ++iter)
+		ret = *iter == 0 ? 0 : -1;
+
+	return ret;
+}
+#define check_rsv_field(field) _check_rsv_field(field, ARRAY_SIZE(field))
+
+static inline void _zero_rsv_field(unsigned int *field, size_t size)
+{
+	unsigned int *iter;
+
+	for (iter = field; iter < field+size; ++iter)
+		*iter = 0;
+}
+#define zero_rsv_field(field) _zero_rsv_field(field, ARRAY_SIZE(field))
+
 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 {
 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
@@ -154,7 +177,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			req.extts.flags |= PTP_STRICT_FLAGS;
 			/* Make sure no reserved bit is set. */
 			if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
-			    req.extts.rsv[0] || req.extts.rsv[1]) {
+			    check_rsv_field(req.extts.rsv)) {
 				err = -EINVAL;
 				break;
 			}
@@ -166,8 +189,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			}
 		} else if (cmd == PTP_EXTTS_REQUEST) {
 			req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
-			req.extts.rsv[0] = 0;
-			req.extts.rsv[1] = 0;
+			zero_rsv_field(req.extts.rsv);
 		}
 		if (req.extts.index >= ops->n_ext_ts) {
 			err = -EINVAL;
@@ -188,17 +210,13 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			break;
 		}
 		if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
-			req.perout.rsv[0] || req.perout.rsv[1] ||
-			req.perout.rsv[2] || req.perout.rsv[3]) &&
-			cmd == PTP_PEROUT_REQUEST2) {
+		     check_rsv_field(req.perout.rsv)) &&
+		    cmd == PTP_PEROUT_REQUEST2) {
 			err = -EINVAL;
 			break;
 		} else if (cmd == PTP_PEROUT_REQUEST) {
 			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
-			req.perout.rsv[0] = 0;
-			req.perout.rsv[1] = 0;
-			req.perout.rsv[2] = 0;
-			req.perout.rsv[3] = 0;
+			zero_rsv_field(req.perout.rsv);
 		}
 		if (req.perout.index >= ops->n_per_out) {
 			err = -EINVAL;
@@ -258,7 +276,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			break;
 		}
 		if (extoff->n_samples > PTP_MAX_SAMPLES
-		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
+		    || check_rsv_field(extoff->rsv)) {
 			err = -EINVAL;
 			break;
 		}
@@ -318,17 +336,11 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			err = -EFAULT;
 			break;
 		}
-		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
-				|| pd.rsv[3] || pd.rsv[4])
-			&& cmd == PTP_PIN_GETFUNC2) {
+		if (check_rsv_field(pd.rsv) && cmd == PTP_PIN_GETFUNC2) {
 			err = -EINVAL;
 			break;
 		} else if (cmd == PTP_PIN_GETFUNC) {
-			pd.rsv[0] = 0;
-			pd.rsv[1] = 0;
-			pd.rsv[2] = 0;
-			pd.rsv[3] = 0;
-			pd.rsv[4] = 0;
+			zero_rsv_field(pd.rsv);
 		}
 		pin_index = pd.index;
 		if (pin_index >= ops->n_pins) {
@@ -350,17 +362,11 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			err = -EFAULT;
 			break;
 		}
-		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
-				|| pd.rsv[3] || pd.rsv[4])
-			&& cmd == PTP_PIN_SETFUNC2) {
+		if (check_rsv_field(pd.rsv) && cmd == PTP_PIN_SETFUNC2) {
 			err = -EINVAL;
 			break;
 		} else if (cmd == PTP_PIN_SETFUNC) {
-			pd.rsv[0] = 0;
-			pd.rsv[1] = 0;
-			pd.rsv[2] = 0;
-			pd.rsv[3] = 0;
-			pd.rsv[4] = 0;
+			zero_rsv_field(pd.rsv);
 		}
 		pin_index = pd.index;
 		if (pin_index >= ops->n_pins) {
Signature · 1 line
2.21.0
#2 lore raw

Re: [Intel PMC TGPIO Driver 1/5] drivers/ptp: Add Enhanced handling of reserve fields

Quoted 6 lines · On 12/11/2019 1:48 PM, [email protected] wrote:

From: Christopher Hall <[email protected]>

Add functions that parameterize checking and zeroing of reserve fields in ioctl arguments. Eliminates need to change this code when repurposing reserve fields.

Nice!

Quoted 24 lines
---
 drivers/ptp/ptp_chardev.c | 60 +++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 27 deletions(-)
drivers/ptp/ptp_chardev.c +4 -0
index 9d72ab593f13..f9ad6df57fa5 100644
@@ -12,6 +12,7 @@
 #include <linux/timekeeping.h>
 
 #include <linux/nospec.h>
+#include <linux/string.h>
 
 #include "ptp_private.h"
 
@@ -106,6 +107,28 @@ int ptp_open(struct posix_clock *pc, fmode_t fmode)
 	return 0;
 }
 
+/* Returns -1 if any reserved fields are non-zero */
+static inline int _check_rsv_field(unsigned int *field, size_t size)
+{

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 122

Quoted 10 lines
drivers/ptp/ptp_chardev.c +10 -0
+	unsigned int *iter;
+	int ret = 0;
+
+	for (iter = field; iter < field+size && ret == 0; ++iter)
+		ret = *iter == 0 ? 0 : -1;
+
+	return ret;
+}
+#define check_rsv_field(field) _check_rsv_field(field, ARRAY_SIZE(field))
+

This assumes that reserved fields will always be arrays. Seems like a reasonable restriction to me.

Are the reserved fields always integers? Seems so. Ok.

Quoted 62 lines
drivers/ptp/ptp_chardev.c +15 -11
+static inline void _zero_rsv_field(unsigned int *field, size_t size)
+{
+	unsigned int *iter;
+
+	for (iter = field; iter < field+size; ++iter)
+		*iter = 0;
+}
+#define zero_rsv_field(field) _zero_rsv_field(field, ARRAY_SIZE(field))
+
 long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 {
 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
@@ -154,7 +177,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			req.extts.flags |= PTP_STRICT_FLAGS;
 			/* Make sure no reserved bit is set. */
 			if ((req.extts.flags & ~PTP_EXTTS_VALID_FLAGS) ||
-			    req.extts.rsv[0] || req.extts.rsv[1]) {
+			    check_rsv_field(req.extts.rsv)) {
 				err = -EINVAL;
 				break;
 			}
@@ -166,8 +189,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			}
 		} else if (cmd == PTP_EXTTS_REQUEST) {
 			req.extts.flags &= PTP_EXTTS_V1_VALID_FLAGS;
-			req.extts.rsv[0] = 0;
-			req.extts.rsv[1] = 0;
+			zero_rsv_field(req.extts.rsv);
 		}
 		if (req.extts.index >= ops->n_ext_ts) {
 			err = -EINVAL;
@@ -188,17 +210,13 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			break;
 		}
 		if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) ||
-			req.perout.rsv[0] || req.perout.rsv[1] ||
-			req.perout.rsv[2] || req.perout.rsv[3]) &&
-			cmd == PTP_PEROUT_REQUEST2) {
+		     check_rsv_field(req.perout.rsv)) &&
+		    cmd == PTP_PEROUT_REQUEST2) {
 			err = -EINVAL;
 			break;
 		} else if (cmd == PTP_PEROUT_REQUEST) {
 			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
-			req.perout.rsv[0] = 0;
-			req.perout.rsv[1] = 0;
-			req.perout.rsv[2] = 0;
-			req.perout.rsv[3] = 0;
+			zero_rsv_field(req.perout.rsv);
 		}
 		if (req.perout.index >= ops->n_per_out) {
 			err = -EINVAL;
@@ -258,7 +276,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			break;
 		}
 		if (extoff->n_samples > PTP_MAX_SAMPLES
-		    || extoff->rsv[0] || extoff->rsv[1] || extoff->rsv[2]) {
+		    || check_rsv_field(extoff->rsv)) {
 			err = -EINVAL;
 			break;
 		}

drivers/ptp/ptp_chardev.c long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg) · line 339

Quoted 7 lines
drivers/ptp/ptp_chardev.c +1 -3
@@ -318,17 +336,11 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			err = -EFAULT;
 			break;
 		}
-		if ((pd.rsv[0] || pd.rsv[1] || pd.rsv[2]
-				|| pd.rsv[3] || pd.rsv[4])
-			&& cmd == PTP_PIN_GETFUNC2) {
+		if (check_rsv_field(pd.rsv) && cmd == PTP_PIN_GETFUNC2) {

Not that it's a big deal, but I think this might read more clearly if this was "cmd == PTP_PIN_GETFUNC2 && check_rsv_field(pd.rsv)"

Thanks,
Jake

maintainer
#3 lore raw

Re: [Intel PMC TGPIO Driver 1/5] drivers/ptp: Add Enhanced handling of reserve fields

Quoted 3 lines · Jacob Keller

Not that it's a big deal, but I think this might read more clearly if this was "cmd == PTP_PIN_GETFUNC2 && check_rsv_field(pd.rsv)"

+1

#4 lore raw

Re: [Intel PMC TGPIO Driver 1/5] drivers/ptp: Add Enhanced handling of reserve fields

Quoted 6 lines · Richard Cochran
Quoted 3 lines · Jacob Keller

Not that it's a big deal, but I think this might read more clearly if this was "cmd == PTP_PIN_GETFUNC2 && check_rsv_field(pd.rsv)"

+1

Yes. Good point.

#5 lore raw

Re: [Intel PMC TGPIO Driver 1/5] drivers/ptp: Add Enhanced handling of reserve fields

In reply to [email protected]
Quoted 6 lines · On 12/11/2019 1:48 PM, [email protected] wrote:

From: Christopher Hall <[email protected]>

Add functions that parameterize checking and zeroing of reserve fields in ioctl arguments. Eliminates need to change this code when repurposing reserve fields.

One thing to note that may be problematic with this handling: a later usage of a reserved field must still be reported as zero for callers of the old ioctl variants. Reserved fields were only handled properly in the "2" variants of the ioctls.

This change might make it harder to notice and remember to ensure that previously unused fields get zero'd for the old ioctls.

Richard, thoughts?

Thanks,
Jake

maintainer
#6 lore raw

Re: [Intel PMC TGPIO Driver 1/5] drivers/ptp: Add Enhanced handling of reserve fields

In reply to [email protected]

drivers/ptp/ptp_chardev.c line 15

Quoted 9 lines · On Wed, Dec 11, 2019 at 01:48:48PM -0800, [email protected] wrote:
drivers/ptp/ptp_chardev.c +1 -0
index 9d72ab593f13..f9ad6df57fa5 100644
@@ -12,6 +12,7 @@
 #include <linux/timekeeping.h>
 
 #include <linux/nospec.h>
+#include <linux/string.h>

Please group these two includes with the others, above, in alphabetical order.

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 111

Quoted 8 lines
drivers/ptp/ptp_chardev.c +2 -0
 #include "ptp_private.h"
 
@@ -106,6 +107,28 @@ int ptp_open(struct posix_clock *pc, fmode_t fmode)
 	return 0;
 }
 
+/* Returns -1 if any reserved fields are non-zero */
+static inline int _check_rsv_field(unsigned int *field, size_t size)

How about _check_reserved_field() instead?

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 113

Quoted 2 lines
drivers/ptp/ptp_chardev.c +2 -0
+{
+	unsigned int *iter;

Ugh, 'ptr' please.

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 117

Quoted 4 lines
drivers/ptp/ptp_chardev.c +4 -0
+	int ret = 0;
+
+	for (iter = field; iter < field+size && ret == 0; ++iter)
+		ret = *iter == 0 ? 0 : -1;

Please use the "early out" pattern:

	for (ptr = field; ptr < field + size; ptr++) {
		if (*ptr) {
			return -1;
		}
	}
	return 0;
Note:  field + size
Note:  ptr++

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 121

Quoted 4 lines
drivers/ptp/ptp_chardev.c +4 -0
+
+	return ret;
+}
+#define check_rsv_field(field) _check_rsv_field(field, ARRAY_SIZE(field))

And check_reserved_field() here. No need to abbreviate.

Thanks,
Richard

#7 lore raw

Re: [Intel PMC TGPIO Driver 1/5] drivers/ptp: Add Enhanced handling of reserve fields

Hi Richard,

Thanks for the detailed review.

Quoted 8 lines · Richard Cochran
Quoted 7 lines · On Wed, Dec 11, 2019 at 01:48:48PM -0800, [email protected] wrote:
drivers/ptp/ptp_chardev.c +0 -0
index 9d72ab593f13..f9ad6df57fa5 100644
@@ -12,6 +12,7 @@
 #include <linux/timekeeping.h>
 
Quoted 5 lines

drivers/ptp/ptp_chardev.c line 15

Quoted 2 lines
drivers/ptp/ptp_chardev.c +1 -0
 #include <linux/nospec.h>
+#include <linux/string.h>

Please group these two includes with the others, above, in alphabetical order.

OK. Done.

Quoted 10 lines

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 111

Quoted 8 lines
drivers/ptp/ptp_chardev.c +2 -0
 #include "ptp_private.h"
 
@@ -106,6 +107,28 @@ int ptp_open(struct posix_clock *pc, fmode_t fmode)
 	return 0;
 }
 
+/* Returns -1 if any reserved fields are non-zero */
+static inline int _check_rsv_field(unsigned int *field, size_t size)

How about _check_reserved_field() instead?

No problem. Sounds good.

Quoted 19 lines

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 113

Quoted 2 lines
drivers/ptp/ptp_chardev.c +2 -0
+{
+	unsigned int *iter;

Ugh, 'ptr' please.

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 117

Quoted 4 lines
drivers/ptp/ptp_chardev.c +4 -0
+	int ret = 0;
+
+	for (iter = field; iter < field+size && ret == 0; ++iter)
+		ret = *iter == 0 ? 0 : -1;

Please use the "early out" pattern:

	for (ptr = field; ptr < field + size; ptr++) {
		if (*ptr) {
			return -1;
		}
	}
	return 0;
Quoted 2 lines
Note:  field + size
Note:  ptr++

OK for both of these.

Quoted 6 lines

drivers/ptp/ptp_chardev.c int ptp_open(struct posix_clock *pc, fmode_t fmode) · line 121

Quoted 4 lines
drivers/ptp/ptp_chardev.c +4 -0
+
+	return ret;
+}
+#define check_rsv_field(field) _check_rsv_field(field, ARRAY_SIZE(field))

And check_reserved_field() here. No need to abbreviate.

OK. Done.

Quoted 2 lines

Thanks,
Richard

Thanks,
Chris

#8 lore raw

PATCH 2/5 drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface

In reply to [email protected]

From: Christopher Hall <[email protected]>

The Intel PMC TGPIO controller logic is driven by ART which is not adjust- able. Rather than adjusting the clock frequency, an output frequency adjustment method is added that doesn't require restarting or resetting output.

Output frequency adjustment is achieved by adding a flag to be passed in the PEROUT2 ioctl argument. In this case, the driver should disregard the 'start' time field and adjust the hardware output frequency if periodic output is already "running".

For devices with an adjustable clock, the OFFSET_PRECISE* ioctl is used to compute the device clock offset with respect to the system clock. For non-adjustable clocks, the EVENT_COUNT_TSTAMP2 ioctl adds an analogous relation between output edges and elapsed time device time. This tuple is captured simultaneously in hardware and is used to precisely compute the *actual* average cumulative output frequency relative to the device clock. The actual average is used to adjust the output period to achieve the *desired* cumulative output frequency.

---
 drivers/ptp/ptp_chardev.c        | 26 ++++++++++++++++++++++++++
 include/linux/ptp_clock_kernel.h |  2 ++
 include/uapi/linux/ptp_clock.h   | 25 +++++++++++++++++++++++--
 3 files changed, 51 insertions(+), 2 deletions(-)
drivers/ptp/ptp_chardev.c +26 -0
index f9ad6df57fa5..04c51878723d 100644
@@ -134,6 +134,7 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
 	struct ptp_sys_offset_extended *extoff = NULL;
 	struct ptp_sys_offset_precise precise_offset;
+	struct ptp_event_count_tstamp counttstamp;
 	struct system_device_crosststamp xtstamp;
 	struct ptp_clock_info *ops = ptp->info;
 	struct ptp_sys_offset *sysoff = NULL;
@@ -218,6 +219,13 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 			req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
 			zero_rsv_field(req.perout.rsv);
 		}
+		/* These flags don't make sense together */
+		if (cmd == PTP_PEROUT_REQUEST2 &&
+		    req.perout.flags & PTP_PEROUT_FREQ_ADJ &&
+		    req.perout.flags & PTP_PEROUT_ONE_SHOT) {
+			err = -EINVAL;
+			break;
+		}
 		if (req.perout.index >= ops->n_per_out) {
 			err = -EINVAL;
 			break;
@@ -227,6 +235,24 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
 		err = ops->enable(ops, &req, enable);
 		break;
 
+	case PTP_EVENT_COUNT_TSTAMP2:
+		if (!ops->counttstamp)
+			return -ENOTSUPP;
+		if (copy_from_user(&counttstamp, (void __user *)arg,
+				   sizeof(counttstamp))) {
+			err = -EFAULT;
+			break;
+		}
+		if (check_rsv_field(counttstamp.rsv)) {
+			err = -EINVAL;
+			break;
+		}
+		err = ops->counttstamp(ops, &counttstamp);
+		if (!err && copy_to_user((void __user *)arg, &counttstamp,
+						sizeof(counttstamp)))
+			err = -EFAULT;
+		break;
+
 	case PTP_ENABLE_PPS:
 	case PTP_ENABLE_PPS2:
 		memset(&req, 0, sizeof(req));
include/linux/ptp_clock_kernel.h +2 -0
index 93cc4f1d444a..8223f6f656dd 100644
@@ -134,6 +134,8 @@ struct ptp_clock_info {
 			  struct ptp_system_timestamp *sts);
 	int (*getcrosststamp)(struct ptp_clock_info *ptp,
 			      struct system_device_crosststamp *cts);
+	int (*counttstamp)(struct ptp_clock_info *ptp,
+			   struct ptp_event_count_tstamp *count);
 	int (*settime64)(struct ptp_clock_info *p, const struct timespec64 *ts);
 	int (*enable)(struct ptp_clock_info *ptp,
 		      struct ptp_clock_request *request, int on);
include/uapi/linux/ptp_clock.h +23 -2
index 9dc9d0079e98..ecb4c4e49205 100644
@@ -34,6 +34,11 @@
 #define PTP_STRICT_FLAGS   (1<<3)
 #define PTP_EXTTS_EDGES    (PTP_RISING_EDGE | PTP_FALLING_EDGE)
 
+/*
+ * Bits of the ptp_pin_desc.flags field:
+ */
+#define PTP_PINDESC_EVTCNTVALID	(1<<0)
+
 /*
  * flag fields valid for the new PTP_EXTTS_REQUEST2 ioctl.
  */
@@ -54,11 +59,13 @@
  * Bits of the ptp_perout_request.flags field:
  */
 #define PTP_PEROUT_ONE_SHOT (1<<0)
+#define PTP_PEROUT_FREQ_ADJ (1<<1)
 
 /*
  * flag fields valid for the new PTP_PEROUT_REQUEST2 ioctl.
  */
-#define PTP_PEROUT_VALID_FLAGS	(PTP_PEROUT_ONE_SHOT)
+#define PTP_PEROUT_VALID_FLAGS	(PTP_PEROUT_ONE_SHOT |	\
+				 PTP_PEROUT_FREQ_ADJ)
 
 /*
  * No flags are valid for the original PTP_PEROUT_REQUEST ioctl
@@ -106,6 +113,14 @@ struct ptp_perout_request {
 	unsigned int rsv[4];          /* Reserved for future use. */
 };
 
+struct ptp_event_count_tstamp {
+	struct ptp_clock_time device_time;
+	unsigned long long event_count;
+	unsigned int index;
+	unsigned int flags;
+	unsigned int rsv[4];          /* Reserved for future use. */
+};
+
 #define PTP_MAX_SAMPLES 25 /* Maximum allowed offset measurement samples. */
 
 struct ptp_sys_offset {
@@ -164,10 +179,14 @@ struct ptp_pin_desc {
 	 * PTP_EXTTS_REQUEST and PTP_PEROUT_REQUEST ioctls.
 	 */
 	unsigned int chan;
+	/*
+	 * Per pin capability flag
+	 */
+	unsigned int flags;
 	/*
 	 * Reserved for future use.
 	 */
-	unsigned int rsv[5];
+	unsigned int rsv[4];
 };
 
 #define PTP_CLK_MAGIC '='
@@ -195,6 +214,8 @@ struct ptp_pin_desc {
 	_IOWR(PTP_CLK_MAGIC, 17, struct ptp_sys_offset_precise)
 #define PTP_SYS_OFFSET_EXTENDED2 \
 	_IOWR(PTP_CLK_MAGIC, 18, struct ptp_sys_offset_extended)
+#define PTP_EVENT_COUNT_TSTAMP2 \
+	_IOWR(PTP_CLK_MAGIC, 19, struct ptp_event_count_tstamp)
 
 struct ptp_extts_event {
 	struct ptp_clock_time t; /* Time event occured. */
Signature · 1 line
2.21.0
maintainer
#9 lore raw

Re: [Intel PMC TGPIO Driver 2/5] drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface

include/linux/ptp_clock_kernel.h struct ptp_clock_info { · line 138

Quoted 10 lines · On Wed, Dec 11, 2019 at 01:48:49PM -0800, [email protected] wrote:
include/linux/ptp_clock_kernel.h +2 -0
index 93cc4f1d444a..8223f6f656dd 100644
@@ -134,6 +134,8 @@ struct ptp_clock_info {
 			  struct ptp_system_timestamp *sts);
 	int (*getcrosststamp)(struct ptp_clock_info *ptp,
 			      struct system_device_crosststamp *cts);
+	int (*counttstamp)(struct ptp_clock_info *ptp,
+			   struct ptp_event_count_tstamp *count);

KernelDoc missing.

As tglx said, it is hard to guess what this will be used for. I would appreciate a fuller explanation of the new callback in the commit log message.

In general, please introduce a specific new API with an example of how it is used. In this series you have three new APIs,

[Intel PMC TGPIO Driver 2/5] drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface [Intel PMC TGPIO Driver 3/5] drivers/ptp: Add user-space input polling interface [Intel PMC TGPIO Driver 4/5] x86/tsc: Add TSC support functions to support ART driven Time-Aware GPIO

and then a largish driver using them all.

[Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

May I suggest an ordering more like:

[1/5] x86/tsc: Add TSC support functions to support ART... (with forward explanation of the use case)
[2/5] drivers/ptp: Add PMC Time-Aware GPIO Driver (without new bits)
[3/5] drivers/ptp: Add Enhanced handling of reserve fields (okay as is)
[4/5] drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface [5/5] implement ^^^ in the driver
[6/5] drivers/ptp: Add user-space input polling interface [7/5] implement ^^^ in the driver

include/linux/ptp_clock_kernel.h struct ptp_clock_info {

Quoted 4 lines
include/linux/ptp_clock_kernel.h +4 -0
+/*
+ * Bits of the ptp_pin_desc.flags field:
+ */
+#define PTP_PINDESC_EVTCNTVALID	(1<<0)

Is this somehow connected to ...

Quoted 2 lines

#define PTP_PEROUT_ONE_SHOT (1<<0)
+#define PTP_PEROUT_FREQ_ADJ (1<<1)

... this? If not, then they each deserve their own patch.

include/linux/ptp_clock_kernel.h struct ptp_pin_desc { · line 185

Quoted 8 lines
include/linux/ptp_clock_kernel.h +4 -0
@@ -164,10 +179,14 @@ struct ptp_pin_desc {
 	 * PTP_EXTTS_REQUEST and PTP_PEROUT_REQUEST ioctls.
 	 */
 	unsigned int chan;
+	/*
+	 * Per pin capability flag
+	 */
+	unsigned int flags;

Please use 'capabilities' instead of 'flags'.

include/linux/ptp_clock_kernel.h struct ptp_pin_desc { · line 187

Quoted 2 lines
include/linux/ptp_clock_kernel.h +2 -0
+#define PTP_EVENT_COUNT_TSTAMP2 \
+	_IOWR(PTP_CLK_MAGIC, 19, struct ptp_event_count_tstamp)

What is the connection between this, PTP_PINDESC_EVTCNTVALID, and PTP_PEROUT_FREQ?

Thanks,
Richard

#10 lore raw

Re: [Intel PMC TGPIO Driver 2/5] drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface

Hi Richard,

Quoted 11 lines · Richard Cochran

include/linux/ptp_clock_kernel.h struct ptp_clock_info { · line 138

Quoted 4 lines · On Wed, Dec 11, 2019 at 01:48:49PM -0800, [email protected] wrote:
include/linux/ptp_clock_kernel.h +2 -0
 
+	int (*counttstamp)(struct ptp_clock_info *ptp,
+			   struct ptp_event_count_tstamp *count);

KernelDoc missing.

As tglx said, it is hard to guess what this will be used for. I would appreciate a fuller explanation of the new callback in the commit log message.

Yes, to both of these above. More incremental patches as you point out below also helps here. I have replied to tglx and you in another thread.

Quoted 13 lines

In general, please introduce a specific new API with an example of how it is used. In this series you have three new APIs,

[Intel PMC TGPIO Driver 2/5] drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface [Intel PMC TGPIO Driver 3/5] drivers/ptp: Add user-space input polling interface [Intel PMC TGPIO Driver 4/5] x86/tsc: Add TSC support functions to support ART driven Time-Aware GPIO

and then a largish driver using them all.

[Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

May I suggest an ordering more like:

Quoted 7 lines

[1/5] x86/tsc: Add TSC support functions to support ART... (with forward explanation of the use case)
[2/5] drivers/ptp: Add PMC Time-Aware GPIO Driver (without new bits)
[3/5] drivers/ptp: Add Enhanced handling of reserve fields (okay as is)
[4/5] drivers/ptp: Add PEROUT2 ioctl frequency adjustment interface [5/5] implement ^^^ in the driver
[6/5] drivers/ptp: Add user-space input polling interface [7/5] implement ^^^ in the driver

This makes sense. Thanks for the detail here.

Quoted 10 lines

include/linux/ptp_clock_kernel.h struct ptp_pin_desc { · line 185

Quoted 8 lines
include/linux/ptp_clock_kernel.h +4 -0
@@ -164,10 +179,14 @@ struct ptp_pin_desc {
 	 * PTP_EXTTS_REQUEST and PTP_PEROUT_REQUEST ioctls.
 	 */
 	unsigned int chan;
+	/*
+	 * Per pin capability flag
+	 */
+	unsigned int flags;

Please use 'capabilities' instead of 'flags'.

Yes. Makes sense.

Quoted 2 lines

Thanks,
Richard

Thanks,
Christopher

#11 lore raw

PATCH 3/5 drivers/ptp: Add user-space input polling interface

In reply to [email protected]

From: Christopher Hall <[email protected]>

The Intel PMC Time-Aware GPIO controller doesn't implement interrupts to notify software that an input event has occurred. To solve this problem, implement a user-space polling interface allowing the application to check for input events. The API returns an event count and time. This interface (EVENT_COUNT_TSTAMP2) is *reused* from the output frequency adjustment API. The event count delta indicates that one or more events have occurred and how many events may have been missed.

For periodic input use cases, polling is fairly efficient. Using PPS input, as an example, polling 3x per second is more than sufficient to capture rising and falling edge timestamps. Generalizing, the poll period should be:

	poll_period = (nominal_input_period / (1 + freq_offset)) / 2

	Where freq_offset = <remote:local clock ratio> - 1;

A flag that indicates a pin isn't capable of generating interrupts is added. Software should not expect notification through the read() interface on pins marked as such. If all pins are marked as poll only, the read interface is marked 'defunct' and immediately returns an error because no interrupt would ever occur to exit the wait state.

---
 drivers/ptp/ptp_clock.c        | 13 +++++++++++++
 include/uapi/linux/ptp_clock.h |  1 +
 2 files changed, 14 insertions(+)
drivers/ptp/ptp_clock.c +13 -0
index b84f16bbd6f2..518dc911ec40 100644
@@ -190,6 +190,17 @@ static void ptp_aux_kworker(struct kthread_work *work)
 		kthread_queue_delayed_work(ptp->kworker, &ptp->aux_work, delay);
 }
 
+static bool check_for_readability(struct ptp_pin_desc *pin_desc, size_t size)
+{
+	int i;
+	unsigned int flags = PTP_PINDESC_INPUTPOLL;
+
+	for (i = 0; i < size && flags != 0; ++i)
+		flags &= pin_desc[i].flags;
+
+	return flags == 0;
+}
+
 /* public interface */
 
 struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
@@ -213,6 +224,8 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
 		goto no_slot;
 	}
 
+	ptp->defunct = !check_for_readability(info->pin_config, info->n_pins);
+
 	ptp->clock.ops = ptp_clock_ops;
 	ptp->info = info;
 	ptp->devid = MKDEV(major, index);
include/uapi/linux/ptp_clock.h +1 -0
index ecb4c4e49205..d5bd6504480c 100644
@@ -38,6 +38,7 @@
  * Bits of the ptp_pin_desc.flags field:
  */
 #define PTP_PINDESC_EVTCNTVALID	(1<<0)
+#define PTP_PINDESC_INPUTPOLL	(1<<1)
 
 /*
  * flag fields valid for the new PTP_EXTTS_REQUEST2 ioctl.
Signature · 1 line
2.21.0
maintainer
#12 lore raw

Re: [Intel PMC TGPIO Driver 3/5] drivers/ptp: Add user-space input polling interface

Quoted 9 lines · On Wed, Dec 11, 2019 at 01:48:50PM -0800, [email protected] wrote:

From: Christopher Hall <[email protected]>

The Intel PMC Time-Aware GPIO controller doesn't implement interrupts to notify software that an input event has occurred. To solve this problem, implement a user-space polling interface allowing the application to check for input events. The API returns an event count and time. This interface (EVENT_COUNT_TSTAMP2) is *reused* from the output frequency adjustment API. The event count delta indicates that one or more events have occurred and how many events may have been missed.

So I think this interface is truly horrible.

The ptp_pin_desc describes a pin's configuration WRT the PTP_PF_xxx and the specific EXTTS/PEROUT_REQUEST channel. I don't know exactly what you are trying to accomplish, but there has got to be a better way. Re-using the ptp_pin_desc for a polling interface is surely not the way forward.

Thanks,
Richard

#13 lore raw

PATCH 4/5 x86/tsc: Add TSC support functions to support ART driven Time-Aware GPIO

In reply to [email protected]

From: Christopher Hall <[email protected]>

The PMC Time-Aware GPIO (TGPIO) logic is driven by ART. Several support functions are required to translate ART to/from nanoseconds units which is required by the PHC clock interface. A function is also added to get the current value of ART/TSC using get_cycles() (RDTSC). This avoids multiple MMIO reads required to retrieve ART.

Note that rather than ART nanoseconds, TSC nanoseconds are used. These are related by TSC = ART * CPUID[15H].EBX/CPUID[15H].EAX + K. The value of K is the distinction between between the ART and TSC nanoseconds.

The advantage of using TSC nanoseconds to represent the device clock because it can easily be calculated in user-space using only CPUID[15H]. The value of ART in general can't be since K isn't necessarily known to the application.

---
 arch/x86/include/asm/tsc.h |   6 ++
 arch/x86/kernel/tsc.c      | 116 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 120 insertions(+), 2 deletions(-)
arch/x86/include/asm/tsc.h +6 -0
index 8a0c25c6bf09..f44c92fe3cd5 100644
@@ -32,6 +32,12 @@ static inline cycles_t get_cycles(void)
 
 extern struct system_counterval_t convert_art_to_tsc(u64 art);
 extern struct system_counterval_t convert_art_ns_to_tsc(u64 art_ns);
+extern struct timespec64 get_tsc_ns_now(struct system_counterval_t
+					*system_counter);
+extern u64 convert_tsc_ns_to_art(struct timespec64 *tsc_ns);
+extern u64 convert_tsc_ns_to_art_duration(struct timespec64 *tsc_ns);
+extern struct timespec64 convert_art_to_tsc_ns(u64 art);
+extern struct timespec64 convert_art_to_tsc_ns_duration(u64 art);
 
 extern void tsc_early_init(void);
 extern void tsc_init(void);
arch/x86/kernel/tsc.c +114 -2

134 lines not on this page · raw message

Signature · 1 line
2.21.0
#14 lore raw

PATCH 5/5 drivers/ptp: Add PMC Time-Aware GPIO Driver

In reply to [email protected]

From: Christopher Hall <[email protected]>

Add support for PMC Time-Aware GPIO (TGPIO) hardware that is present on upcoming Intel platforms. The hardware logic is driven by the ART clock. The current hardware has two GPIO pins. Input interrupts are not implemented in hardware.

The driver implements to the expanded PHC interface. Input requires use of the user-polling interface. Also, since the ART clock can't be adjusted, modulating the output frequency uses the edge timestamp interface (EVENT_COUNT_TSTAMP2) and the PEROUT2 ioctl output frequency adjustment interface.

Acknowledgment: Portions of the driver code were authored by Felipe Balbi <[email protected]>

---
 drivers/ptp/Kconfig               |  13 +
 drivers/ptp/Makefile              |   1 +
 drivers/ptp/ptp-intel-pmc-tgpio.c | 867 ++++++++++++++++++++++++++++++
 3 files changed, 881 insertions(+)
 create mode 100644 drivers/ptp/ptp-intel-pmc-tgpio.c
drivers/ptp/Kconfig +13 -0
index b0d1b8d264fa..974964d24947 100644
@@ -107,6 +107,19 @@ config PTP_1588_CLOCK_PCH
 	  To compile this driver as a module, choose M here: the module
 	  will be called ptp_pch.
 
+config PTP_INTEL_PMC_TGPIO
+	tristate "Intel PMC Timed GPIO"
+	depends on X86
+	depends on ACPI
+	depends on PTP_1588_CLOCK
+	help
+	  This driver adds support for Intel PMC Timed-Aware GPIO (TGPIO)
+	  Controller. The device clock used to drive TGPIO logic is the
+	  Always Running Timer (ART).
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called ptp-intel-pmc-tgpio.
+
 config PTP_1588_CLOCK_KVM
 	tristate "KVM virtual PTP clock"
 	depends on PTP_1588_CLOCK
drivers/ptp/Makefile +1 -0
index 69a06f86a450..3733ae240d4f 100644
@@ -7,6 +7,7 @@ ptp-y					:= ptp_clock.o ptp_chardev.o ptp_sysfs.o
 obj-$(CONFIG_PTP_1588_CLOCK)		+= ptp.o
 obj-$(CONFIG_PTP_1588_CLOCK_DTE)	+= ptp_dte.o
 obj-$(CONFIG_PTP_1588_CLOCK_IXP46X)	+= ptp_ixp46x.o
+obj-$(CONFIG_PTP_INTEL_PMC_TGPIO)	+= ptp-intel-pmc-tgpio.o
 obj-$(CONFIG_PTP_1588_CLOCK_PCH)	+= ptp_pch.o
 obj-$(CONFIG_PTP_1588_CLOCK_KVM)	+= ptp_kvm.o
 obj-$(CONFIG_PTP_1588_CLOCK_QORIQ)	+= ptp-qoriq.o
drivers/ptp/ptp-intel-pmc-tgpio.c new file +867 -0

867 lines not on this page · raw message

Signature · 1 line
2.21.0
maintainer
#15 lore raw

Re: [Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

drivers/ptp/ptp-intel-pmc-tgpio.c line 88

Quoted 2 lines · On Wed, Dec 11, 2019 at 01:48:52PM -0800, [email protected] wrote:
drivers/ptp/ptp-intel-pmc-tgpio.c new file +2 -0
+static inline u32 intel_pmc_tgpio_readl
+(struct intel_pmc_tgpio_t *tgpio, u32 offset, unsigned int index)

Coding style is off the deep end, here and in many following functions.

Thanks,
Richard

#16 lore raw

Re: [Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

In reply to [email protected]

Hi Christopher,

thanks for your patch!

Quoted 13 lines · On Fri, Jan 31, 2020 at 7:41 AM <[email protected]> wrote:

From: Christopher Hall <[email protected]>

Add support for PMC Time-Aware GPIO (TGPIO) hardware that is present on upcoming Intel platforms. The hardware logic is driven by the ART clock. The current hardware has two GPIO pins. Input interrupts are not implemented in hardware.

The driver implements to the expanded PHC interface. Input requires use of the user-polling interface. Also, since the ART clock can't be adjusted, modulating the output frequency uses the edge timestamp interface (EVENT_COUNT_TSTAMP2) and the PEROUT2 ioctl output frequency adjustment interface.

Quoted 4 lines

Acknowledgment: Portions of the driver code were authored by Felipe Balbi <[email protected]>

This driver becomes a big confusion for the GPIO maintainer...

Quoted 5 lines
drivers/ptp/Kconfig +5 -0
+config PTP_INTEL_PMC_TGPIO
+       tristate "Intel PMC Timed GPIO"
+       depends on X86
+       depends on ACPI
+       depends on PTP_1588_CLOCK

(...)

Quoted 1 line

+#include <linux/gpio.h>

Don't use this header in new code, use <linux/gpio/driver.h>

But it looks like you should just drop it because there is no GPIO of that generic type going on at all?

drivers/ptp/ptp-intel-pmc-tgpio.c line 41

Quoted 8 lines
drivers/ptp/ptp-intel-pmc-tgpio.c new file +8 -0
+/* Control Register */
+#define TGPIOCTL_EN                    BIT(0)
+#define TGPIOCTL_DIR                   BIT(1)
+#define TGPIOCTL_EP                    GENMASK(3, 2)
+#define TGPIOCTL_EP_RISING_EDGE                (0 << 2)
+#define TGPIOCTL_EP_FALLING_EDGE       (1 << 2)
+#define TGPIOCTL_EP_TOGGLE_EDGE                (2 << 2)
+#define TGPIOCTL_PM                    BIT(4)

OK this looks like some GPIO registers...

Then there is a bunch of PTP stuff I don't understand I suppose related to the precision time protocol.

Could you explain to a simple soul like me what is going on? Should I bother myself with this or is this "some other GPIO, not what you work on" or could it be that it's something I should review?

I get the impression that this so-called "general purpose I/O" isn't very general purpose at all, it seems to be very PTP-purpose rather, so this confusion needs to be explained in the commit message and possibly in the code as well.

What is it for really?

Yours,
Linus Walleij

#17 lore raw

Re: [Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

Quoted 4 lines · Linus Walleij

OK this looks like some GPIO registers...

Then there is a bunch of PTP stuff I don't understand I suppose related to the precision time protocol.

Hi Linus

I understand your confusion. The first time this was posted to netdev, i asked it to be renamed because it has very little to do with GPIO

https://lore.kernel.org/netdev/[email protected]/

Andrew

#18 lore raw

Re: [Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

Quoted 6 lines · Andrew Lunn
Quoted 4 lines · Linus Walleij

OK this looks like some GPIO registers...

Then there is a bunch of PTP stuff I don't understand I suppose related to the precision time protocol.

Quoted 6 lines

Hi Linus

I understand your confusion. The first time this was posted to netdev, i asked it to be renamed because it has very little to do with GPIO

https://lore.kernel.org/netdev/[email protected]/

And besides that I didn't see it in internal review list, so, it needs to be very carefully reviewed. I already saw some not good formatted and questionable code.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#19 lore raw

Re: [Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Andrew Lunn
Quoted 4 lines · Linus Walleij

OK this looks like some GPIO registers...

Then there is a bunch of PTP stuff I don't understand I suppose related to the precision time protocol.

Hi Linus

Quoted 8 lines
Quoted 4 lines

I understand your confusion. The first time this was posted to netdev, i asked it to be renamed because it has very little to do with GPIO

https://lore.kernel.org/netdev/[email protected]/

And besides that I didn't see it in internal review list, so, it needs to be very carefully reviewed. I already saw some not good formatted and questionable code.

Just to have some evidences.

The entire function
static const plat_acpi_resource *find_plat_acpi_resource (struct platform_device *pdev, int *n_pins)
brings a lot of questions.

MODULE_ALIAS("acpi*:INTC1021:*");
What is this?!

And so on...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#20 lore raw

Re: [Intel PMC TGPIO Driver 5/5] drivers/ptp: Add PMC Time-Aware GPIO Driver

In reply to Linus Walleij

Hi Linus,

Thanks for the review.

Quoted 15 lines · Linus Walleij

Hi Christopher,

thanks for your patch!

Quoted 9 lines · On Fri, Jan 31, 2020 at 7:41 AM <[email protected]> wrote:

From: Christopher Hall <[email protected]>

The driver implements to the expanded PHC interface. Input requires use of the user-polling interface. Also, since the ART clock can't be adjusted, modulating the output frequency uses the edge timestamp interface (EVENT_COUNT_TSTAMP2) and the PEROUT2 ioctl output frequency adjustment interface.

Quoted 6 lines
Quoted 4 lines

Acknowledgment: Portions of the driver code were authored by Felipe Balbi <[email protected]>

This driver becomes a big confusion for the GPIO maintainer...

I see your concern. TGPIO is Intel's internal name for the device, but there's no reason we can't use some other terminology in the context of the Linux kernel. How about removing the GP? We could refer to the device as "timed I/O". I think that is still fairly descriptive, but removes the confusion. Does this help the problem?

Quoted 5 lines

drivers/ptp/Kconfig config PTP_1588_CLOCK_PCH · line 114

Quoted 5 lines
drivers/ptp/Kconfig +5 -0
+config PTP_INTEL_PMC_TGPIO
+       tristate "Intel PMC Timed GPIO"
+       depends on X86
+       depends on ACPI
+       depends on PTP_1588_CLOCK
Quoted 7 lines

(...)

Quoted 1 line

+#include <linux/gpio.h>

Don't use this header in new code, use <linux/gpio/driver.h>

But it looks like you should just drop it because there is no GPIO of that generic type going on at all?

Yes. You're correct. Removed.

Quoted 11 lines

drivers/ptp/ptp-intel-pmc-tgpio.c line 41

Quoted 8 lines
drivers/ptp/ptp-intel-pmc-tgpio.c new file +8 -0
+/* Control Register */
+#define TGPIOCTL_EN                    BIT(0)
+#define TGPIOCTL_DIR                   BIT(1)
+#define TGPIOCTL_EP                    GENMASK(3, 2)
+#define TGPIOCTL_EP_RISING_EDGE                (0 << 2)
+#define TGPIOCTL_EP_FALLING_EDGE       (1 << 2)
+#define TGPIOCTL_EP_TOGGLE_EDGE                (2 << 2)
+#define TGPIOCTL_PM                    BIT(4)

OK this looks like some GPIO registers...

Quoted 7 lines

Then there is a bunch of PTP stuff I don't understand I suppose related to the precision time protocol.

Could you explain to a simple soul like me what is going on? Should I bother myself with this or is this "some other GPIO, not what you work on" or could it be that it's something I should review?

The Timed GPIO device has some GPIO-like features, but is mostly used to import/export a clock signal. It doesn't implement PWM or some other "GP" features like reading/setting pin state. I think you can safely ignore the feature.

Quoted 2 lines

I get the impression that this so-called "general purpose I/O" isn't very general purpose at all, it seems to be very PTP-purpose

Yes. It is missing many of general purpose features.

Quoted 4 lines

rather, so this confusion needs to be explained in the commit message and possibly in the code as well.

What is it for really?

For import/export system clock, primarily.

Quoted 2 lines

Yours,
Linus Walleij

Thanks,
Christopher

maintainer
#21 lore raw
In reply to [email protected]

Some process notes here:

On Wed, 11 Dec 2019 13:48:47 -0800, [email protected] wrote:

Please fix the date on your system / patches.

Quoted 2 lines

Acknowledgment: Portions of the driver code were authored by Felipe Balbi <[email protected]>

Strangely none of the patches carry his sign-off tho, neither have you CCed him?

Presumably this is going via the networking tree, so please tag the patches with [PATCH net-next] rather than the name of the driver. Subject should sufficiently serve as an indication of what the patches do.

The net-next networking tree is now closed and will reopen shortly after the merge window is over:

http://vger.kernel.org/~davem/net-next.html

If you post a next version before it's open to get reviews - please post it as [RFC net-next].

(also don't use "static inline" in C files, compiler will know)

#22 lore raw
In reply to [email protected]
Quoted 13 lines · [email protected] writes:

From: Christopher Hall <[email protected]>

Upcoming Intel platforms will have Time-Aware GPIO (TGPIO) hardware. The TGPIO logic is driven by the Always Running Timer (ART) that's related to TSC using CPUID[15H] (See Intel SDM Invariant Time-Keeping).

The ART frequency is not adjustable. In order, to implement output adjustments an additional edge-timestamp API is added, as well, as a periodic output frequency adjustment API. Togther, these implement equivalent functionality to the existing SYS_OFFSET_* and frequency adjustment APIs.

Quoted 5 lines

The TGPIO hardware doesn't implement interrupts. For TGPIO input, the output edge-timestamp API is re-used to implement a user-space polling interface. For periodic input (e.g. PPS) this is fairly efficient, requiring only a marginally faster poll rate than the input event frequency.

I really have a hard time to understand why this is implemented as part of PTP while you talk about PPS at the same time.

Proper information about why this approach was chosen and what that magic device is used for would be really helpful.

Thanks,

tglx

#23 lore raw

Thanks for reviewing.

Quoted 11 lines · Thomas Gleixner
Quoted 7 lines · [email protected] writes:

From: Christopher Hall <[email protected]>

The TGPIO hardware doesn't implement interrupts. For TGPIO input, the output edge-timestamp API is re-used to implement a user-space polling interface. For periodic input (e.g. PPS) this is fairly efficient, requiring only a marginally faster poll rate than the input event frequency.

I really have a hard time to understand why this is implemented as part of PTP while you talk about PPS at the same time.

We primarily need support for periodic input and output uses cases. Apologies for omitting the periodic output use case from the cover letter. While TGPIO isn't associated with a PTP timestamp clock, the PHC pin/clock interface fits the usage otherwise.

The PHC periodic output API is the closest fit for periodic output without creating a new API. The PHC interface can also register as a PPS source. I am, however, concerned in general about implementing PPS input in the driver because the hardware doesn't implement interrupts - requiring polling.

Quoted 2 lines

Proper information about why this approach was chosen and what that magic device is used for would be really helpful.

The customer requested usages are 1 kHz and 1 Hz for both input and output. Some higher level use cases are:
- using a GPS PPS signal to sync the system clock
- auditing timesync precision for financial services, especially high frequency trading (e.g. MiFID).

Apart from clock import/export applications, timestamping single I/O events are potentially valuable for industrial control applications (e.g. motor position sensing vs. time). As time sync precision requirements for these applications are tightened, standard GPIO timing precision will not be good enough.

Quoted 3 lines

Thanks,

tglx

Thanks,
Christopher

#24 lore raw

Christopher,

Quoted 9 lines · Christopher S. Hall
Quoted 8 lines · Thomas Gleixner
Quoted 6 lines · [email protected] writes:

The TGPIO hardware doesn't implement interrupts. For TGPIO input, the output edge-timestamp API is re-used to implement a user-space polling interface. For periodic input (e.g. PPS) this is fairly efficient, requiring only a marginally faster poll rate than the input event frequency.

Quoted 7 lines
Quoted 2 lines

I really have a hard time to understand why this is implemented as part of PTP while you talk about PPS at the same time.

We primarily need support for periodic input and output uses cases. Apologies for omitting the periodic output use case from the cover letter. While TGPIO isn't associated with a PTP timestamp clock, the PHC pin/clock interface fits the usage otherwise.

Which usage? PTP like usage? I really have a hard time to make the connection. PTP is as the name says a protocol to synchronize time across a network.

What you're having is a GPIO which has some magic timestamp clock which can be correlated back to ART/TSC, right?

So squeezing this into PTP makes as much sense as squeezing a camera/video/audio interface which provides or consumes timestamps into it.

Quoted 2 lines

The PHC periodic output API is the closest fit for periodic output without creating a new API.

Well, you avoid creating a new API or extending one which makes sense by abusing the PTP interface for something which is not even remotely connected to PTP.

Quoted 3 lines

The PHC interface can also register as a PPS source. I am, however, concerned in general about implementing PPS input in the driver because the hardware doesn't implement interrupts - requiring polling.

Really useful.

Quoted 6 lines
Quoted 2 lines

Proper information about why this approach was chosen and what that magic device is used for would be really helpful.

The customer requested usages are 1 kHz and 1 Hz for both input and output. Some higher level use cases are:
- using a GPS PPS signal to sync the system clock

That makes at least some sense. See below.

Quoted 2 lines

- auditing timesync precision for financial services, especially high frequency trading (e.g. MiFID).

A good reason to not support it at all. Aside of that I have no idea how that auditing is supposed to work. Just throwing a few buzzwords around is not giving much technical context.

Quoted 5 lines

Apart from clock import/export applications, timestamping single I/O events are potentially valuable for industrial control applications (e.g. motor position sensing vs. time). As time sync precision requirements for these applications are tightened, standard GPIO timing precision will not be good enough.

Well, coming from that industry I really doubt that you can do anything useful with it, but hey it's been 25 years since I stopped working on motor and motion controllers :)

Anyway, the device we are talking about is a GPIO device with inputs and outputs plus bells and whistels attached to it.

On the input side this provides a timestamp taken by the hardware when the input level changes, i.e. hardware based time stamping instead of software based interrupt arrival timestamping. Looks like an obvious extension to the GPIO subsystem.

How that timestamp is processed/converted and what an application can actually do with it is a secondary problem:

- PPS mode:

This can be implemented as an actual PPS driver which consumes the GPIO, does timer based polling and feeds the timestamp into the PPS subsystem. Might be not the most accurate solution, so I can see why you want to use the PTP interface for it, which provides the raw clocksource (ART/TSC) and the correlated monotonic/realtime timestamps. But then again this wants to be a PTP driver consuming the GPIO and the timestamp via timer based polling.

- GPIO sampling

That's totally disconnected from PPS/PTP and just provides a correlated clock monotonic timestamp to the application.

That covers your motor example :)

- Timesync validation:

-Enocluehowthatshouldworkatall

And of course you can use the GPIO input just as input without bells and whistels :)

Now you have the output side which again is a GPIO in the first place. But then it also has a secondary function which allows to create a periodic output with a magic correlation to the ART and some way to actually adjust the frequency. Neither of those two functions are in anyway relatable to PTP AFAICT.

The periodic, programmable and adjustable output is pretty much a PWM of some form and what you want to tell it is: Output a pulse at a given frequency. Due to the fact that the input clock of that thing is ART you can do the magic transformation from ART frequency to frequency adjusted clock monotonic in order to tweak the parameters so they actually end up generating your precise output frequency. Tell the driver which frequency you want and it retrieves the correlation information from the kernel and uses it to achieve a precise output frequency. Doesn't sound like rocket science and does not required new magic ioctls.

I might be missing something, but you surely can fill the blanks then.

Thanks,

tglx

#25 lore raw

Hi Thomas,

Thank you for your suggestions.

Quoted 20 lines · Thomas Gleixner

Christopher,

Quoted 9 lines · Christopher S. Hall
Quoted 8 lines · Thomas Gleixner
Quoted 6 lines · [email protected] writes:

The TGPIO hardware doesn't implement interrupts. For TGPIO input, the output edge-timestamp API is re-used to implement a user-space polling interface. For periodic input (e.g. PPS) this is fairly efficient, requiring only a marginally faster poll rate than the input event frequency.

Quoted 7 lines
Quoted 2 lines

I really have a hard time to understand why this is implemented as part of PTP while you talk about PPS at the same time.

We primarily need support for periodic input and output uses cases. Apologies for omitting the periodic output use case from the cover letter. While TGPIO isn't associated with a PTP timestamp clock, the PHC pin/clock interface fits the usage otherwise.

Quoted 6 lines

Which usage? PTP like usage? I really have a hard time to make the connection. PTP is as the name says a protocol to synchronize time across a network.

What you're having is a GPIO which has some magic timestamp clock which can be correlated back to ART/TSC, right?

Right.

Quoted 64 lines
Quoted 3 lines

The customer requested usages are 1 kHz and 1 Hz for both input and output. Some higher level use cases are:
- using a GPS PPS signal to sync the system clock

That makes at least some sense. See below.

Quoted 2 lines

- auditing timesync precision for financial services, especially high frequency trading (e.g. MiFID).

A good reason to not support it at all. Aside of that I have no idea how that auditing is supposed to work. Just throwing a few buzzwords around is not giving much technical context.

Quoted 5 lines

Apart from clock import/export applications, timestamping single I/O events are potentially valuable for industrial control applications (e.g. motor position sensing vs. time). As time sync precision requirements for these applications are tightened, standard GPIO timing precision will not be good enough.

Well, coming from that industry I really doubt that you can do anything useful with it, but hey it's been 25 years since I stopped working on motor and motion controllers :)

Anyway, the device we are talking about is a GPIO device with inputs and outputs plus bells and whistels attached to it.

On the input side this provides a timestamp taken by the hardware when the input level changes, i.e. hardware based time stamping instead of software based interrupt arrival timestamping. Looks like an obvious extension to the GPIO subsystem.

How that timestamp is processed/converted and what an application can actually do with it is a secondary problem:

- PPS mode:

This can be implemented as an actual PPS driver which consumes the GPIO, does timer based polling and feeds the timestamp into the PPS subsystem. Might be not the most accurate solution, so I can see why you want to use the PTP interface for it, which provides the raw clocksource (ART/TSC) and the correlated monotonic/realtime timestamps. But then again this wants to be a PTP driver consuming the GPIO and the timestamp via timer based polling.

- GPIO sampling

That's totally disconnected from PPS/PTP and just provides a correlated clock monotonic timestamp to the application.

That covers your motor example :)

- Timesync validation:

-Enocluehowthatshouldworkatall

And of course you can use the GPIO input just as input without bells and whistels :)

Now you have the output side which again is a GPIO in the first place. But then it also has a secondary function which allows to create a periodic output with a magic correlation to the ART and some way to actually adjust the frequency. Neither of those two functions are in anyway relatable to PTP AFAICT.

Quoted 9 lines

The periodic, programmable and adjustable output is pretty much a PWM of some form and what you want to tell it is: Output a pulse at a given frequency. Due to the fact that the input clock of that thing is ART you can do the magic transformation from ART frequency to frequency adjusted clock monotonic in order to tweak the parameters so they actually end up generating your precise output frequency. Tell the driver which frequency you want and it retrieves the correlation information from the kernel and uses it to achieve a precise output frequency. Doesn't sound like rocket science and does not required new magic ioctls.

This will have a few touch points in the kernel - PWM, GPIO, PPS. I'll work on an RFC patchset.

Quoted 5 lines

I might be missing something, but you surely can fill the blanks then.

Thanks,

tglx

Thanks,
Christopher

#26 lore raw
In reply to Thomas Gleixner
Quoted 7 lines · Thomas Gleixner
Quoted 5 lines · Christopher S. Hall

Apart from clock import/export applications, timestamping single I/O events are potentially valuable for industrial control applications (e.g. motor position sensing vs. time). As time sync precision requirements for these applications are tightened, standard GPIO timing precision will not be good enough.

If you are using (from userspace) the GPIO character device and open the events using e.g. tools/gpio/gpio-event-mon.c you get GPIO events to userspace.

This uses a threaded interrupt with an top half (fastpath) that timestamps it as the IRQ comes in using ktime_get_ns(). It's as good as we can get it with just software and IRQs (I think).

This uses a KFIFO to userspace, same approach as the IIO subsystem.

Quoted 7 lines

Anyway, the device we are talking about is a GPIO device with inputs and outputs plus bells and whistles attached to it.

On the input side this provides a timestamp taken by the hardware when the input level changes, i.e. hardware based time stamping instead of software based interrupt arrival timestamping. Looks like an obvious extension to the GPIO subsystem.

That looks like something I/we would want to support all the way to userspace so people can do their funny industrial stuff in some standard manner.

IIO has a config file in sysfs that lets them select the source of the timestamp like so (drivers/iio/industrialio-core.c):

s64 iio_get_time_ns(const struct iio_dev *indio_dev)

{
        struct timespec64 tp;

        switch (iio_device_get_clock(indio_dev)) {
        case CLOCK_REALTIME:
                return ktime_get_real_ns();
        case CLOCK_MONOTONIC:
                return ktime_get_ns();
        case CLOCK_MONOTONIC_RAW:
                return ktime_get_raw_ns();
        case CLOCK_REALTIME_COARSE:
                return ktime_to_ns(ktime_get_coarse_real());
        case CLOCK_MONOTONIC_COARSE:
                ktime_get_coarse_ts64(&tp);
                return timespec64_to_ns(&tp);
        case CLOCK_BOOTTIME:
                return ktime_get_boottime_ns();
        case CLOCK_TAI:
                return ktime_get_clocktai_ns();
        default:
                BUG();
        }
}

After discussion with Arnd we concluded the only timestamp that makes sense is ktime_get_ns(). So in GPIO we just use that, all the userspace I can think of certainly prefers monotonic time. (If tglx does not agree with that I stand corrected to whatever he says, I suppose.)

Anyway in GPIO we could also make it configurable for users who know what they are doing.

HW timestamps would be something more elaborate and nice CLOCK_HW_SPECIFIC or so. Some of the IIO sensors also have that, we just don't expose it as of now.

Yours,
Linus Walleij

maintainer
#27 lore raw
Quoted 4 lines · Linus Walleij

That looks like something I/we would want to support all the way to userspace so people can do their funny industrial stuff in some standard manner.

...

Quoted 3 lines

HW timestamps would be something more elaborate and nice CLOCK_HW_SPECIFIC or so. Some of the IIO sensors also have that, we just don't expose it as of now.

It is worth considering whether it makes sense to somehow unify gpio, iio, and the phc pin subsystems. In my view, a big chunk of work would be to have something like the "clock tree" for gpios and clock lines. This tree would describe the HW connectivity between (at least) MAC/PHY clocks and IOs, gpio controllers, audio/video codecs, and so on.

Also, there is that comedi thing in staging. If it has a chance ever to leave staging, then I think it would also benefit from integration into gpio/iio world.

Thanks,
Richard

#28 lore raw
In reply to Linus Walleij
Quoted 21 lines · Linus Walleij
Quoted 1 line · Thomas Gleixner

"Christopher S. Hall" <[email protected]> writes:

IIO has a config file in sysfs that lets them select the source of the timestamp like so (drivers/iio/industrialio-core.c):

s64 iio_get_time_ns(const struct iio_dev *indio_dev)

{
        struct timespec64 tp;

        switch (iio_device_get_clock(indio_dev)) {
        case CLOCK_REALTIME:
                return ktime_get_real_ns();
        case CLOCK_MONOTONIC:
                return ktime_get_ns();
        case CLOCK_MONOTONIC_RAW:
                return ktime_get_raw_ns();
        case CLOCK_REALTIME_COARSE:
                return ktime_to_ns(ktime_get_coarse_real());
        case CLOCK_MONOTONIC_COARSE:
                ktime_get_coarse_ts64(&tp);
                return timespec64_to_ns(&tp);
Quoted 8 lines
        case CLOCK_BOOTTIME:
                return ktime_get_boottime_ns();
        case CLOCK_TAI:
                return ktime_get_clocktai_ns();
        default:
                BUG();
        }
}

That's a nice example of overengineering :)

Quoted 5 lines

After discussion with Arnd we concluded the only timestamp that makes sense is ktime_get_ns(). So in GPIO we just use that, all the userspace I can think of certainly prefers monotonic time. (If tglx does not agree with that I stand corrected to whatever he says, I suppose.)

In general, CLOCK_MONOTONIC is what makes most sense.

The only other interesting clock which makes sense from an application POV is CLOCK_TAI which is becoming more popular in terms of network wide time coordination and TSN.

CLOCK_REALTIME is a pain to deal with due to leap seconds, daylight savings etc.

Quoted 6 lines

Anyway in GPIO we could also make it configurable for users who know what they are doing.

HW timestamps would be something more elaborate and nice CLOCK_HW_SPECIFIC or so. Some of the IIO sensors also have that, we just don't expose it as of now.

HW timestamps are just more accurate than the software timestamps which we have now and from a portability and interface POV they should just be converted converted / mapped to clock MONOTONIC or clock TAI. So your existing interface (maybe extended to TAI in the future) is just working, but more accurate.

Exposing the HW timestamp itself based on some random and potentially unknown clock might still be useful for some specialized applications, but that want's to be through a distinct interface so there is no chance to confuse it with something generally useful.

Thanks,

tglx

#29 lore raw
Quoted 31 lines · On Tue, 03 Mar 2020 16:24:03 +0100 Thomas Gleixner <[email protected]> wrote:
Quoted 21 lines · Linus Walleij
Quoted 1 line · Thomas Gleixner

"Christopher S. Hall" <[email protected]> writes:

IIO has a config file in sysfs that lets them select the source of the timestamp like so (drivers/iio/industrialio-core.c):

s64 iio_get_time_ns(const struct iio_dev *indio_dev)

{
        struct timespec64 tp;

        switch (iio_device_get_clock(indio_dev)) {
        case CLOCK_REALTIME:
                return ktime_get_real_ns();
        case CLOCK_MONOTONIC:
                return ktime_get_ns();
        case CLOCK_MONOTONIC_RAW:
                return ktime_get_raw_ns();
        case CLOCK_REALTIME_COARSE:
                return ktime_to_ns(ktime_get_coarse_real());
        case CLOCK_MONOTONIC_COARSE:
                ktime_get_coarse_ts64(&tp);
                return timespec64_to_ns(&tp);
Quoted 8 lines
        case CLOCK_BOOTTIME:
                return ktime_get_boottime_ns();
        case CLOCK_TAI:
                return ktime_get_clocktai_ns();
        default:
                BUG();
        }
}  
Quoted 1 line

That's a nice example of overengineering :)

Yeah. There was some ugly history behind that including some 'ancient' stupidity from me :( I certainly don't recommend anyone copies it.

We may have overcompensated for having an odd default by allowing lots of other odd choices.

Quoted 29 lines
Quoted 6 lines

After discussion with Arnd we concluded the only timestamp that makes sense is ktime_get_ns(). So in GPIO we just use that, all the userspace I can think of certainly prefers monotonic time. (If tglx does not agree with that I stand corrected to whatever he says, I suppose.)

In general, CLOCK_MONOTONIC is what makes most sense.

The only other interesting clock which makes sense from an application POV is CLOCK_TAI which is becoming more popular in terms of network wide time coordination and TSN.

CLOCK_REALTIME is a pain to deal with due to leap seconds, daylight savings etc.

Quoted 6 lines

Anyway in GPIO we could also make it configurable for users who know what they are doing.

HW timestamps would be something more elaborate and nice CLOCK_HW_SPECIFIC or so. Some of the IIO sensors also have that, we just don't expose it as of now.

HW timestamps are just more accurate than the software timestamps which we have now and from a portability and interface POV they should just be converted converted / mapped to clock MONOTONIC or clock TAI. So your existing interface (maybe extended to TAI in the future) is just working, but more accurate.

Quoted 4 lines

Exposing the HW timestamp itself based on some random and potentially unknown clock might still be useful for some specialized applications, but that want's to be through a distinct interface so there is no chance to confuse it with something generally useful.

Agreed, though it would be nice to actually have some hardware that supports sane synchronization between a hardware timestamp and
sensible clocks in the system. In IIO we have some nasty filtering in
some drivers to attempt to align hardware timestamps and deal with jitter in interrupt handling.

Good luck (or maybe you do have a rare sane system!)

Jonathan

Quoted 4 lines

Thanks,

tglx

maintainer
#30 lore raw
In reply to [email protected]
Quoted 5 lines · On Wed, Dec 11, 2019 at 01:48:47PM -0800, [email protected] wrote:

The ART frequency is not adjustable. In order, to implement output adjustments an additional edge-timestamp API is added, as well, as a periodic output frequency adjustment API. Togther, these implement equivalent functionality to the existing SYS_OFFSET_* and frequency adjustment APIs.

I don't see a reason for a custom, new API just for this device.

The TGPIO input clock, the ART, is a free running counter, but you want to support frequency adjustments. Use a timecounter cyclecounter pair.

Let the user dial a periodic output signal in the normal way.

Let the user change the frequency in the normal way, and during this call, adjust the counter values accordingly.

Thanks,
Richard

#31 lore raw
Quoted 9 lines · Richard Cochran
Quoted 5 lines · On Wed, Dec 11, 2019 at 01:48:47PM -0800, [email protected] wrote:

The ART frequency is not adjustable. In order, to implement output adjustments an additional edge-timestamp API is added, as well, as a periodic output frequency adjustment API. Togther, these implement equivalent functionality to the existing SYS_OFFSET_* and frequency adjustment APIs.

I don't see a reason for a custom, new API just for this device.

Quoted 9 lines

The TGPIO input clock, the ART, is a free running counter, but you want to support frequency adjustments. Use a timecounter cyclecounter pair.

Let the user dial a periodic output signal in the normal way.

Let the user change the frequency in the normal way, and during this call, adjust the counter values accordingly.

To add:

in order to program the pin output correctly, you may need to reverse the timecounter/cyclecounter calculations. I recall doing something similar in ixgbe for programming the correct output signal to match.

It may actually be worth adding helper functions to the timecounter system for doing those kind of reverse calculations for converting from a time value back into cycles.

Quoted 3 lines

Thanks,
Richard

#32 lore raw
In reply to Richard Cochran

Hi Richard,

Thanks for reviewing.

Quoted 7 lines · Richard Cochran
Quoted 5 lines · On Wed, Dec 11, 2019 at 01:48:47PM -0800, [email protected] wrote:

The ART frequency is not adjustable. In order, to implement output adjustments an additional edge-timestamp API is added, as well, as a periodic output frequency adjustment API. Togther, these implement equivalent functionality to the existing SYS_OFFSET_* and frequency adjustment APIs.

Quoted 5 lines

I don't see a reason for a custom, new API just for this device.

The TGPIO input clock, the ART, is a free running counter, but you want to support frequency adjustments. Use a timecounter cyclecounter pair.

I'm concerned about the complexity that the timecounter adds to the driver. Specifically, the complexity of dealing with any rate mismatches between the timecounter and the periodic output signal. The phase error between the output and timecounter needs to be zero.

My counter-proposal would be to use the real-time clock as the basis of the device clock. This is fairly simple because the relation between ART and the realtime clock is known. When output is enabled any phase error between the realtime clock and the periodic output signal is accumulated in the SYS_OFFSET result.

This leaves the PHC API behavior as it is currently and uses the frequency adjust API to adjust the output rate.

Quoted 4 lines

Let the user dial a periodic output signal in the normal way.

Let the user change the frequency in the normal way, and during this call, adjust the counter values accordingly.

Yes to both of the above.

Quoted 2 lines

Thanks,
Richard

Thanks,
Christopher

maintainer
#33 lore raw
Quoted 9 lines · Christopher S. Hall
Quoted 3 lines · Richard Cochran

The TGPIO input clock, the ART, is a free running counter, but you want to support frequency adjustments. Use a timecounter cyclecounter pair.

I'm concerned about the complexity that the timecounter adds to the driver. Specifically, the complexity of dealing with any rate mismatches between the timecounter and the periodic output signal. The phase error between the output and timecounter needs to be zero.

If I understood correctly, the device's outputs are generated from a non-adjustable counter. So, no matter what, you will have the problem of changing the pulse period in concert with the user changing the desired frequency.

Quoted 5 lines

My counter-proposal would be to use the real-time clock as the basis of the device clock. This is fairly simple because the relation between ART and the realtime clock is known. When output is enabled any phase error between the realtime clock and the periodic output signal is accumulated in the SYS_OFFSET result.

I don't understand how you intend to do this...

Quoted 9 lines

This leaves the PHC API behavior as it is currently and uses the frequency adjust API to adjust the output rate.

Quoted 4 lines

Let the user dial a periodic output signal in the normal way.

Let the user change the frequency in the normal way, and during this call, adjust the counter values accordingly.

Yes to both of the above.

So, why then do you need this?

+#define PTP_EVENT_COUNT_TSTAMP2 \
+ _IOWR(PTP_CLK_MAGIC, 19, struct ptp_event_count_tstamp)

If you can make the device work with the existing user space API,

	ioctl(fd, PTP_PEROUT_REQUEST2, ...);
	while (1) {
		clock_adjtimex(FD_TO_CLOCKID(fd), ...);
	}

that would be ideal. But I will push back on anything like the following.

	ioctl(fd, PTP_PEROUT_REQUEST2, ...);
	while (1) {
		clock_adjtimex(FD_TO_CLOCKID(fd), ...);
		ioctl(fd, PTP_EVENT_COUNT_TSTAMP, ...);
	}

But maybe I misunderstood?

Thanks,
Richard

#34 lore raw

Hi Richard,

Quoted 42 lines · Richard Cochran
Quoted 9 lines · Christopher S. Hall
Quoted 3 lines · Richard Cochran

The TGPIO input clock, the ART, is a free running counter, but you want to support frequency adjustments. Use a timecounter cyclecounter pair.

I'm concerned about the complexity that the timecounter adds to the driver. Specifically, the complexity of dealing with any rate mismatches between the timecounter and the periodic output signal. The phase error between the output and timecounter needs to be zero.

If I understood correctly, the device's outputs are generated from a non-adjustable counter. So, no matter what, you will have the problem of changing the pulse period in concert with the user changing the desired frequency.

Quoted 9 lines

This leaves the PHC API behavior as it is currently and uses the frequency adjust API to adjust the output rate.

Quoted 4 lines

Let the user dial a periodic output signal in the normal way.

Let the user change the frequency in the normal way, and during this call, adjust the counter values accordingly.

Yes to both of the above.

So, why then do you need this?

(quoted diff) +2 -0
+#define PTP_EVENT_COUNT_TSTAMP2 \
+       _IOWR(PTP_CLK_MAGIC, 19, struct ptp_event_count_tstamp)

If you can make the device work with the existing user space API,

	ioctl(fd, PTP_PEROUT_REQUEST2, ...);
	while (1) {
		clock_adjtimex(FD_TO_CLOCKID(fd), ...);
	}

that would be ideal. But I will push back on anything like the following.

Quoted 7 lines
	ioctl(fd, PTP_PEROUT_REQUEST2, ...);
	while (1) {
		clock_adjtimex(FD_TO_CLOCKID(fd), ...);
		ioctl(fd, PTP_EVENT_COUNT_TSTAMP, ...);
	}

But maybe I misunderstood?

Thank you for the feedback, but Thomas wants to see this as an extension of GPIO. I'll work on an RFC patch for that instead.

Quoted 2 lines

Thanks,
Richard

Thanks,
Christopher

#35 lore raw
In reply to [email protected]
Quoted 3 lines · On Fri, Jan 31, 2020 at 7:40 AM <[email protected]> wrote:

The TGPIO hardware doesn't implement interrupts. For TGPIO input, the output edge-timestamp API is re-used to implement a user-space polling interface.

It you modeled it reusing the GPIO subsystem (which I don't know if you can) you would get access to the gpiochip character device /dev/gpiochipN and be able to read timestamped events like the tool in tools/gpio/gpio-event-mon.c does.

That said I am still confused about what this driver does or what the purpose is.

GPIO pins in or out? Network coming in or going out using PTP?

What is the use case?

Yours,
Linus Walleij