Skip to contentSkip to conversation

ptp: add Alibaba CIPU PTP clock driver

PATCH Superseded Wen Gu 4 participants 8 messages Jun 27, 2025 – Jun 30, 2025

Subsystems (3)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Wen Gu

author
#0 lore raw

PATCH v2 1/1 ptp: add Alibaba CIPU PTP clock driver

This adds a driver for Alibaba CIPU PTP clock. The CIPU, an underlying infrastructure of Alibaba Cloud, synchronizes time with reference clocks continuously and provides PTP clocks for VMs and bare metals on cloud.

User space e.g. chrony, in VMs or bare metals can get the value of CIPU clock time through the ptp device exposed by this driver.

---
v2->v1:
- add Kconfig dependency on CONFIG_PCI to fix kernel test
robot's complaint.
v1 link: https://lore.kernel.org/netdev/[email protected]/

---
 MAINTAINERS            |   7 +
 drivers/ptp/Kconfig    |  13 +
 drivers/ptp/Makefile   |   1 +
 drivers/ptp/ptp_cipu.c | 958 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 979 insertions(+)
 create mode 100644 drivers/ptp/ptp_cipu.c
MAINTAINERS +7 -0
index bb9df569a3ff..ecd5c1d2808a 100644
@@ -786,6 +786,13 @@ S:	Maintained
 F:	Documentation/i2c/busses/i2c-ali1563.rst
 F:	drivers/i2c/busses/i2c-ali1563.c
 
+ALIBABA CIPU PTP DRIVER
+M:	Wen Gu <[email protected]>
+M:	Xuan Zhuo <[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/ptp/ptp_cipu.c
+
 ALIBABA ELASTIC RDMA DRIVER
 M:	Cheng Xu <[email protected]>
 M:	Kai Shen <[email protected]>
drivers/ptp/Kconfig +13 -0
index 204278eb215e..5811e569a077 100644
@@ -252,4 +252,17 @@ config PTP_S390
 	  driver provides the raw clock value without the delta to
 	  userspace. That way userspace programs like chrony could steer
 	  the kernel clock.
+
+config PTP_1588_CLOCK_CIPU
+	tristate "Alibaba CIPU PTP clock"
+	depends on PTP_1588_CLOCK
+	depends on 64BIT
+	depends on PCI
+	help
+	  This driver adds support for using Alibaba CIPU clock device
+	  as a PTP clock. This is only useful in Alibaba Cloud CIPU
+	  infrastructure.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called ptp_cipu.
 endmenu
drivers/ptp/Makefile +1 -0
index 25f846fe48c9..a168254d3c35 100644
@@ -23,3 +23,4 @@ obj-$(CONFIG_PTP_1588_CLOCK_VMW)	+= ptp_vmw.o
 obj-$(CONFIG_PTP_1588_CLOCK_OCP)	+= ptp_ocp.o
 obj-$(CONFIG_PTP_DFL_TOD)		+= ptp_dfl_tod.o
 obj-$(CONFIG_PTP_S390)			+= ptp_s390.o
+obj-$(CONFIG_PTP_1588_CLOCK_CIPU)	+= ptp_cipu.o
drivers/ptp/ptp_cipu.c new file +958 -0

958 lines not on this page · raw message

Signature · 1 line
2.43.5
#1 lore raw
Quoted 24 lines
(quoted diff) +22 -0
+#define DRV_VER_MAJOR		1
+#define DRV_VER_MINOR		3
+#define DRV_VER_SUBMINOR	0
 
+#define LINUX_UPSTREAM		0x1F
 
+static int ptp_cipu_set_drv_version(struct ptp_cipu_ctx *ptp_ctx)
+{
+	struct ptp_cipu_regs *regs = &ptp_ctx->regs;
+	int version, patchlevel, sublevel;
+	u32 env_ver, drv_ver;
+	int rc;
+
+	if (sscanf(utsname()->release, "%u.%u.%u",
+		   &version, &patchlevel, &sublevel) != 3)
+		return -EINVAL;
+	sublevel = sublevel < 0xFF ? sublevel : 0xFF;
+
+	env_ver = (LINUX_UPSTREAM << 27) | (version << 16) |
+		  (patchlevel << 8) | sublevel;
+
+	rc = cipu_iowrite32_and_check(ptp_ctx->reg_addr +
+				      PTP_CIPU_REG(env_ver),
+				      env_ver, &regs->env_ver);

Review

Quoted 10 lines
(quoted diff) +10 -0
+	if (rc)
+		return rc;
+
+	drv_ver = (DRV_TYPE << 24) | (DRV_VER_MAJOR << 16) |
+		  (DRV_VER_MINOR << 8) | DRV_VER_SUBMINOR;
+
+	return cipu_iowrite32_and_check(ptp_ctx->reg_addr +
+					PTP_CIPU_REG(drv_ver), drv_ver,
+					&regs->drv_ver);
+}

Please could you explain what this is doing.

Andrew

author
#2 lore raw
Quoted 37 lines · Andrew Lunn
Quoted 24 lines
(quoted diff) +22 -0
+#define DRV_VER_MAJOR		1
+#define DRV_VER_MINOR		3
+#define DRV_VER_SUBMINOR	0
 
+#define LINUX_UPSTREAM		0x1F
 
+static int ptp_cipu_set_drv_version(struct ptp_cipu_ctx *ptp_ctx)
+{
+	struct ptp_cipu_regs *regs = &ptp_ctx->regs;
+	int version, patchlevel, sublevel;
+	u32 env_ver, drv_ver;
+	int rc;
+
+	if (sscanf(utsname()->release, "%u.%u.%u",
+		   &version, &patchlevel, &sublevel) != 3)
+		return -EINVAL;
+	sublevel = sublevel < 0xFF ? sublevel : 0xFF;
+
+	env_ver = (LINUX_UPSTREAM << 27) | (version << 16) |
+		  (patchlevel << 8) | sublevel;
+
+	rc = cipu_iowrite32_and_check(ptp_ctx->reg_addr +
+				      PTP_CIPU_REG(env_ver),
+				      env_ver, &regs->env_ver);

Review

Quoted 10 lines
(quoted diff) +10 -0
+	if (rc)
+		return rc;
+
+	drv_ver = (DRV_TYPE << 24) | (DRV_VER_MAJOR << 16) |
+		  (DRV_VER_MINOR << 8) | DRV_VER_SUBMINOR;
+
+	return cipu_iowrite32_and_check(ptp_ctx->reg_addr +
+					PTP_CIPU_REG(drv_ver), drv_ver,
+					&regs->drv_ver);
+}

Please could you explain what this is doing.

Quoted 1 line

Andrew

Sure.

This provides informations to cloud ptp device, so that device can decide whether to allow the initialization to continue.

It is expected to avoid loading a version that is found to have serious defects after it is released.

Thanks,

#3 lore raw
In reply to Wen Gu

drivers/ptp/ptp_cipu.c line 121

Quoted 9 lines
drivers/ptp/ptp_cipu.c new file +9 -0
+#define PTP_CIPU_LOG_SUB(dev, level, type, event, fmt, ...) \
+({ \
+	static DEFINE_RATELIMIT_STATE(_rs, \
+				      DEFAULT_RATELIMIT_INTERVAL, \
+				      DEFAULT_RATELIMIT_BURST); \
+	if (__ratelimit(&_rs)) \
+		dev_printk(level, dev, "[%02x:%02x]: " fmt, \
+			   type, event, ##__VA_ARGS__); \
+})

Please don't use such wrappers. Just use dev_dbg_ratelimited() etc.

drivers/ptp/ptp_cipu.c line 171

Quoted 9 lines
drivers/ptp/ptp_cipu.c new file +9 -0
+static int cipu_iowrite8_and_check(void __iomem *addr,
+				   u8 value, u8 *res)
+{
+	iowrite8(value, addr);
+	if (value != ioread8(addr))
+		return -EIO;
+	*res = value;
+	return 0;
+}

This probably needs a comment. I assume the hardware is broken and sometimes writes don't work? You should state that.

Quoted 33 lines
drivers/ptp/ptp_cipu.c new file +33 -0
+static void ptp_cipu_print_dev_events(struct ptp_cipu_ctx *ptp_ctx,
+				      int event)
+{
+	struct device *dev = &ptp_ctx->pdev->dev;
+	int type = PTP_CIPU_EVT_TYPE_DEV;
+
+	switch (event) {
+	case PTP_CIPU_EVT_H_CLK_ABN:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
+				 "Atomic Clock Error Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_CLK_ABN_REC:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "Atomic Clock Error Recovered\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_MT:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
+				 "Maintenance Exception Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_MT_REC:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "Maintenance Exception Recovered\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_MT_TOUT:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "Maintenance Exception Failed to Recover "
+				 "within %d us\n", ptp_ctx->regs.mt_tout_us);
+		break;
+	case PTP_CIPU_EVT_H_DEV_BUSY:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
+				 "PHC Busy Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_BUSY_REC:

drivers/ptp/ptp_cipu.c line 225

Quoted 10 lines
drivers/ptp/ptp_cipu.c new file +10 -0
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "PHC Busy Recovered\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_ERR:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
+				 "PHC Error Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_ERR_REC:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "PHC Error Recovered\n");

Are these fatal? Or can the device still be used after these errors occur?

Quoted 11 lines
drivers/ptp/ptp_cipu.c new file +11 -0
+static int ptp_cipu_enable(struct ptp_clock_info *info,
+			   struct ptp_clock_request *request, int on)
+{
+	return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_settime(struct ptp_clock_info *p,
+			    const struct timespec64 *ts)
+{
+	return -EOPNOTSUPP;
+}

drivers/ptp/ptp_cipu.c line 516

Quoted 10 lines
drivers/ptp/ptp_cipu.c new file +10 -0
+
+static int ptp_cipu_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
+{
+	return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_adjtime(struct ptp_clock_info *ptp, s64 delta)
+{
+	return -EOPNOTSUPP;
+}

I've not looked at the core. Are these actually required? Or if they are missing, does the core default to -EOPNOTSUPP?

Quoted 12 lines
drivers/ptp/ptp_cipu.c new file +12 -0
+static ssize_t register_snapshot_show(struct device *dev,
+				      struct device_attribute *attr, char *buf)
+{
+	struct ptp_cipu_ctx *ctx = pci_get_drvdata(to_pci_dev(dev));
+	struct ptp_cipu_regs *regs = &ctx->regs;
+
+	return sysfs_emit(buf, "%s 0x%x %s 0x%x %s 0x%x %s 0x%x "
+			  "%s 0x%x %s 0x%x %s 0x%x %s 0x%x %s 0x%x "
+			  "%s 0x%x %s 0x%x %s 0x%x\n",
+			  "device_features", regs->dev_feat,
+			  "guest_features", regs->gst_feat,
+			  "driver_version", regs->drv_ver,

drivers/ptp/ptp_cipu.c line 665

Quoted 10 lines
drivers/ptp/ptp_cipu.c new file +10 -0
+			  "environment_version", regs->env_ver,
+			  "device_status", regs->dev_stat,
+			  "sync_status", regs->sync_stat,
+			  "time_precision(ns)", regs->tm_prec_ns,
+			  "epoch_base(years)", regs->epo_base_yr,
+			  "leap_second(s)", regs->leap_sec,
+			  "max_latency(ns)", regs->max_lat_ns,
+			  "maintenance_timeout(us)", regs->mt_tout_us,
+			  "offset_threshold(us)", regs->thresh_us);
+}

Is this debug? Maybe it should be placed in debugfs, rather than sysfs.

Andrew

#4 lore raw
Quoted 16 lines · Andrew Lunn
Quoted 16 lines
drivers/ptp/ptp_cipu.c new file +16 -0
+static int ptp_cipu_enable(struct ptp_clock_info *info,
+			   struct ptp_clock_request *request, int on)
+{
+	return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_settime(struct ptp_clock_info *p,
+			    const struct timespec64 *ts)
+{
+	return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
+{
+	return -EOPNOTSUPP;
+}
Quoted 9 lines

drivers/ptp/ptp_cipu.c line 516

Quoted 5 lines
drivers/ptp/ptp_cipu.c new file +5 -0
+
+static int ptp_cipu_adjtime(struct ptp_clock_info *ptp, s64 delta)
+{
+	return -EOPNOTSUPP;
+}

I've not looked at the core. Are these actually required? Or if they are missing, does the core default to -EOPNOTSUPP?

I was going to say that these are not needed because posix clocks do check if callbacks are assigned and return -EOPNOTSUPP if they are not. That's why ptp_clock_* functions do call these callbacks without checks.

author
#5 lore raw
Quoted 28 lines · Vadim Fedorenko
Quoted 16 lines · Andrew Lunn
Quoted 16 lines
drivers/ptp/ptp_cipu.c new file +16 -0
+static int ptp_cipu_enable(struct ptp_clock_info *info,
+               struct ptp_clock_request *request, int on)
+{
+    return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_settime(struct ptp_clock_info *p,
+                const struct timespec64 *ts)
+{
+    return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
+{
+    return -EOPNOTSUPP;
+}
Quoted 9 lines

drivers/ptp/ptp_cipu.c line 516

Quoted 5 lines
drivers/ptp/ptp_cipu.c new file +5 -0
+
+static int ptp_cipu_adjtime(struct ptp_clock_info *ptp, s64 delta)
+{
+    return -EOPNOTSUPP;
+}

I've not looked at the core. Are these actually required? Or if they are missing, does the core default to -EOPNOTSUPP?

Quoted 3 lines

I was going to say that these are not needed because posix clocks do check if callbacks are assigned and return -EOPNOTSUPP if they are not. That's why ptp_clock_* functions do call these callbacks without checks.

Hi Vadim, do you mean posix clock functions like this:

e.g. posix-clock.c:

static int pc_clock_settime(clockid_t id, const struct timespec64 *ts) {
<...>

	if (cd.clk->ops.clock_settime)
		err = cd.clk->ops.clock_settime(cd.clk, ts);
	else
		err = -EOPNOTSUPP;

<...>
}

In ptp_clock.c, ops.clock_settime() is assigned to ptp_clock_settime(), and it will call ptp->info->settime64() without checks. So I think these 'return -EOPNOTSUPP' functions are needed. Did I miss something?

Thanks!

maintainer
#6 lore raw
Quoted 3 lines · Wen Gu

In ptp_clock.c, ops.clock_settime() is assigned to ptp_clock_settime(), and it will call ptp->info->settime64() without checks. So I think these 'return -EOPNOTSUPP' functions are needed. Did I miss something?

Right, for the essential clock related callbacks, stubs are expected by the ptp class layer.

Some of the newer, non-essential clock methods do indeed check the function pointer, simply to accommodate existing drivers.

Thanks,
Richard

author
#7 lore raw
In reply to Andrew Lunn
Quoted 12 lines · Andrew Lunn

drivers/ptp/ptp_cipu.c line 121

Quoted 9 lines
drivers/ptp/ptp_cipu.c new file +9 -0
+#define PTP_CIPU_LOG_SUB(dev, level, type, event, fmt, ...) \
+({ \
+	static DEFINE_RATELIMIT_STATE(_rs, \
+				      DEFAULT_RATELIMIT_INTERVAL, \
+				      DEFAULT_RATELIMIT_BURST); \
+	if (__ratelimit(&_rs)) \
+		dev_printk(level, dev, "[%02x:%02x]: " fmt, \
+			   type, event, ##__VA_ARGS__); \
+})

Please don't use such wrappers. Just use dev_dbg_ratelimited() etc.

Agree. This was for compatibility with older kernels, I should change it to new helpers.. Will fix in next version. Thanks!

Quoted 4 lines
Quoted 4 lines
drivers/ptp/ptp_cipu.c new file +4 -0
+static int cipu_iowrite8_and_check(void __iomem *addr,
+				   u8 value, u8 *res)
+{
+	iowrite8(value, addr);
Quoted 9 lines

drivers/ptp/ptp_cipu.c line 171

Quoted 5 lines
drivers/ptp/ptp_cipu.c new file +5 -0
+	if (value != ioread8(addr))
+		return -EIO;
+	*res = value;
+	return 0;
+}

This probably needs a comment. I assume the hardware is broken and sometimes writes don't work? You should state that.

Yes, If the cloud device thinks the written value is not what it expected, the write will fail. I will add a comment about that, thanks.

Quoted 38 lines
Quoted 38 lines
drivers/ptp/ptp_cipu.c new file +38 -0
+static void ptp_cipu_print_dev_events(struct ptp_cipu_ctx *ptp_ctx,
+				      int event)
+{
+	struct device *dev = &ptp_ctx->pdev->dev;
+	int type = PTP_CIPU_EVT_TYPE_DEV;
+
+	switch (event) {
+	case PTP_CIPU_EVT_H_CLK_ABN:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
+				 "Atomic Clock Error Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_CLK_ABN_REC:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "Atomic Clock Error Recovered\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_MT:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
+				 "Maintenance Exception Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_MT_REC:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "Maintenance Exception Recovered\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_MT_TOUT:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "Maintenance Exception Failed to Recover "
+				 "within %d us\n", ptp_ctx->regs.mt_tout_us);
+		break;
+	case PTP_CIPU_EVT_H_DEV_BUSY:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
+				 "PHC Busy Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_BUSY_REC:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "PHC Busy Recovered\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_ERR:
+		PTP_CIPU_LOG_SUB(dev, KERN_ERR, type, event,
Quoted 8 lines

drivers/ptp/ptp_cipu.c line 225

Quoted 5 lines
drivers/ptp/ptp_cipu.c new file +5 -0
+				 "PHC Error Detected\n");
+		break;
+	case PTP_CIPU_EVT_H_DEV_ERR_REC:
+		PTP_CIPU_LOG_SUB(dev, KERN_INFO, type, event,
+				 "PHC Error Recovered\n");

Are these fatal? Or can the device still be used after these errors occur?

The clock can't work as expected if these events happened.

The gettime operation will get an invalid timestamp whose PTP_CIPU_M_TS_ABN bit is set and return -EIO.

Quoted 17 lines
Quoted 17 lines
drivers/ptp/ptp_cipu.c new file +16 -0
+static int ptp_cipu_enable(struct ptp_clock_info *info,
+			   struct ptp_clock_request *request, int on)
+{
+	return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_settime(struct ptp_clock_info *p,
+			    const struct timespec64 *ts)
+{
+	return -EOPNOTSUPP;
+}
+
+static int ptp_cipu_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
+{
+	return -EOPNOTSUPP;
+}
Quoted 9 lines

drivers/ptp/ptp_cipu.c line 516

Quoted 5 lines
drivers/ptp/ptp_cipu.c new file +5 -0
+
+static int ptp_cipu_adjtime(struct ptp_clock_info *ptp, s64 delta)
+{
+	return -EOPNOTSUPP;
+}

I've not looked at the core. Are these actually required? Or if they are missing, does the core default to -EOPNOTSUPP?

See reply to Vadim :)

Quoted 17 lines
Quoted 17 lines
drivers/ptp/ptp_cipu.c new file +17 -0
+static ssize_t register_snapshot_show(struct device *dev,
+				      struct device_attribute *attr, char *buf)
+{
+	struct ptp_cipu_ctx *ctx = pci_get_drvdata(to_pci_dev(dev));
+	struct ptp_cipu_regs *regs = &ctx->regs;
+
+	return sysfs_emit(buf, "%s 0x%x %s 0x%x %s 0x%x %s 0x%x "
+			  "%s 0x%x %s 0x%x %s 0x%x %s 0x%x %s 0x%x "
+			  "%s 0x%x %s 0x%x %s 0x%x\n",
+			  "device_features", regs->dev_feat,
+			  "guest_features", regs->gst_feat,
+			  "driver_version", regs->drv_ver,
+			  "environment_version", regs->env_ver,
+			  "device_status", regs->dev_stat,
+			  "sync_status", regs->sync_stat,
+			  "time_precision(ns)", regs->tm_prec_ns,
+			  "epoch_base(years)", regs->epo_base_yr,
Quoted 8 lines

drivers/ptp/ptp_cipu.c line 665

Quoted 5 lines
drivers/ptp/ptp_cipu.c new file +5 -0
+			  "leap_second(s)", regs->leap_sec,
+			  "max_latency(ns)", regs->max_lat_ns,
+			  "maintenance_timeout(us)", regs->mt_tout_us,
+			  "offset_threshold(us)", regs->thresh_us);
+}

Is this debug? Maybe it should be placed in debugfs, rather than sysfs.

These are considered attributes of the CIPU ptp device, so I perfer to put them in sysfs.

But I found sysfs prefers only one value per file [1]. The format here may need to be improved.

[1] https://docs.kernel.org/filesystems/sysfs.html

Thank you for these comments!

Wen Gu

Quoted 2 lines

Andrew