ptp: add ability to configure duty cycle for periodic output

There are external event timestampers (PHCs with support for
PTP_EXTTS_REQUEST) that timestamp both event edges.

When those edges are very close (such as in the case of a short pulse),
there is a chance that the collected timestamp might be of the rising,
or of the falling edge, we never know.

There are also PHCs capable of generating periodic output with a
configurable duty cycle. This is good news, because we can space the
rising and falling edge out enough in time, that the risks to overrun
the 1-entry timestamp FIFO of the extts PHC are lower (example: the
perout PHC can be configured for a period of 1 second, and an "on" time
of 0.5 seconds, resulting in a duty cycle of 50%).

A flag is introduced for signaling that an on time is present in the
perout request structure, for preserving compatibility. Logically
speaking, the duty cycle cannot exceed 100% and the PTP core checks for
this.

PHC drivers that don't support this flag emit a periodic output of an
unspecified duty cycle, same as before.

The duty cycle is encoded as an "on" time, similar to the "start" and
"period" times, and reuses the reserved space while preserving overall
binary layout.

Pahole reported before:

struct ptp_perout_request {
        struct ptp_clock_time start;                     /*     0    16 */
        struct ptp_clock_time period;                    /*    16    16 */
        unsigned int               index;                /*    32     4 */
        unsigned int               flags;                /*    36     4 */
        unsigned int               rsv[4];               /*    40    16 */

        /* size: 56, cachelines: 1, members: 5 */
        /* last cacheline: 56 bytes */
};

And now:

struct ptp_perout_request {
        struct ptp_clock_time start;                     /*     0    16 */
        struct ptp_clock_time period;                    /*    16    16 */
        unsigned int               index;                /*    32     4 */
        unsigned int               flags;                /*    36     4 */
        union {
                struct ptp_clock_time on;                /*    40    16 */
                unsigned int       rsv[4];               /*    40    16 */
        };                                               /*    40    16 */

        /* size: 56, cachelines: 1, members: 5 */
        /* last cacheline: 56 bytes */
};

Signed-off-by: Vladimir Oltean <olteanv@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Vladimir Oltean 2020-07-17 01:45:29 +03:00 committed by David S. Miller
parent eba75c587e
commit f65b71aa25
2 changed files with 41 additions and 9 deletions

View File

@ -191,12 +191,33 @@ long ptp_ioctl(struct posix_clock *pc, unsigned int cmd, unsigned long arg)
err = -EFAULT; err = -EFAULT;
break; break;
} }
if (((req.perout.flags & ~PTP_PEROUT_VALID_FLAGS) || if (cmd == PTP_PEROUT_REQUEST2) {
req.perout.rsv[0] || req.perout.rsv[1] || struct ptp_perout_request *perout = &req.perout;
req.perout.rsv[2] || req.perout.rsv[3]) &&
cmd == PTP_PEROUT_REQUEST2) { if (perout->flags & ~PTP_PEROUT_VALID_FLAGS) {
err = -EINVAL; err = -EINVAL;
break; break;
}
/*
* The "on" field has undefined meaning if
* PTP_PEROUT_DUTY_CYCLE isn't set, we must still treat
* it as reserved, which must be set to zero.
*/
if (!(perout->flags & PTP_PEROUT_DUTY_CYCLE) &&
(perout->rsv[0] || perout->rsv[1] ||
perout->rsv[2] || perout->rsv[3])) {
err = -EINVAL;
break;
}
if (perout->flags & PTP_PEROUT_DUTY_CYCLE) {
/* The duty cycle must be subunitary. */
if (perout->on.sec > perout->period.sec ||
(perout->on.sec == perout->period.sec &&
perout->on.nsec > perout->period.nsec)) {
err = -ERANGE;
break;
}
}
} else if (cmd == PTP_PEROUT_REQUEST) { } else if (cmd == PTP_PEROUT_REQUEST) {
req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS; req.perout.flags &= PTP_PEROUT_V1_VALID_FLAGS;
req.perout.rsv[0] = 0; req.perout.rsv[0] = 0;

View File

@ -53,12 +53,14 @@
/* /*
* Bits of the ptp_perout_request.flags field: * Bits of the ptp_perout_request.flags field:
*/ */
#define PTP_PEROUT_ONE_SHOT (1<<0) #define PTP_PEROUT_ONE_SHOT (1<<0)
#define PTP_PEROUT_DUTY_CYCLE (1<<1)
/* /*
* flag fields valid for the new PTP_PEROUT_REQUEST2 ioctl. * 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_DUTY_CYCLE)
/* /*
* No flags are valid for the original PTP_PEROUT_REQUEST ioctl * No flags are valid for the original PTP_PEROUT_REQUEST ioctl
@ -105,7 +107,16 @@ struct ptp_perout_request {
struct ptp_clock_time period; /* Desired period, zero means disable. */ struct ptp_clock_time period; /* Desired period, zero means disable. */
unsigned int index; /* Which channel to configure. */ unsigned int index; /* Which channel to configure. */
unsigned int flags; unsigned int flags;
unsigned int rsv[4]; /* Reserved for future use. */ union {
/*
* The "on" time of the signal.
* Must be lower than the period.
* Valid only if (flags & PTP_PEROUT_DUTY_CYCLE) is set.
*/
struct ptp_clock_time on;
/* Reserved for future use. */
unsigned int rsv[4];
};
}; };
#define PTP_MAX_SAMPLES 25 /* Maximum allowed offset measurement samples. */ #define PTP_MAX_SAMPLES 25 /* Maximum allowed offset measurement samples. */