Skip to contentSkip to conversation

dpll: zl3073x: do not block DPLL callbacks while PTP ops wait

PATCH New Ivan Vecera 1 participant 1 message Nov 20

Subsystems (4)
lore
Share & tools
lore

Ivan Vecera

author maintainer
#0 lore raw

PATCH 1/1 dpll: zl3073x: do not block DPLL callbacks while PTP ops wait

The PTP clock operations hold the channel lock while they wait for the
device: up to 1 s for a pending ToD write to be applied on the next 1 Hz edge, up to 1 s for a previous TIE write and up to 3 s for a previous output phase step. The DPLL callbacks and the periodic state check of the channel take the same lock, so they wait as well. The DPLL callbacks wait under the global dpll_lock, which stalls the requests for all DPLL devices in the system.

On a GNR-D system with a ZL30732, device-get requests for both DPLL channels and for a DPLL device of ice took 950 ms in median while settime() ran in a loop on the PTP clock of channel 0, and 2955 ms while adjtime() did output phase steps in NCO mode. With this change they take 2 ms in both cases.

Only the PTP clock operations use the ToD, TIE and phase step registers. They need the channel lock only for the channel mode and the list of output pins.

Serialize the PTP clock operations with a new per-channel ptp_lock and take the channel lock only to read the mode and the output pins and to issue a TIE write or a phase step. Wait for the previous TIE write or phase step before taking the channel lock, with tie_lock or phase_step_lock held, and check the mode again under it, so the write or the step is issued only in a mode that supports it. The lock order is ptp_lock, then tie_lock or phase_step_lock, then the channel lock.

Fixes 1db73ce01856 ("dpll: zl3073x: add PTP clock support")
---
 drivers/dpll/zl3073x/chan.c |  60 ++++++++++----
 drivers/dpll/zl3073x/chan.h |   2 +
 drivers/dpll/zl3073x/dpll.c | 153 +++++++++++++++++++++++++++++-------
 drivers/dpll/zl3073x/dpll.h |   5 +-
 4 files changed, 173 insertions(+), 47 deletions(-)
drivers/dpll/zl3073x/chan.c +44 -16
index ba4d303d41b47d..3d0388f28e7bcc 100644
@@ -460,6 +460,25 @@ int zl3073x_chan_df_offset_set(struct zl3073x_dev *zldev, u8 ch, s64 offset)
 	return rc;
 }
 
+/**
+ * zl3073x_chan_tie_ready_wait - wait for the previous TIE write to complete
+ * @zldev: pointer to zl3073x device
+ *
+ * The TIE write operation is shared by all channels, and the device can take
+ * up to one second to complete it.
+ *
+ * Context: Caller must hold zldev->tie_lock.
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_tie_ready_wait(struct zl3073x_dev *zldev)
+{
+	lockdep_assert_held(&zldev->tie_lock);
+
+	return zl3073x_poll_zero_u8(zldev, ZL_REG_DPLL_TIE_CTRL,
+				    ZL_DPLL_TIE_CTRL_OP,
+				    ZL_POLL_TIE_WR_TIMEOUT_US);
+}
+
 /**
  * zl3073x_chan_tie_write - adjust DPLL phase using TIE write
  * @zldev: pointer to zl3073x device
@@ -469,6 +488,8 @@ int zl3073x_chan_df_offset_set(struct zl3073x_dev *zldev, u8 ch, s64 offset)
  * Converts nanoseconds to TIE units (0.01 ps) and writes TIE data
  * to the specified channel.
  *
+ * Context: Caller must hold zldev->tie_lock and must have waited for the
+ * previous TIE write with zl3073x_chan_tie_ready_wait().
  * Return: 0 on success, <0 on error
  */
 int zl3073x_chan_tie_write(struct zl3073x_dev *zldev, u8 ch, s64 delta_ns)
@@ -476,14 +497,7 @@ int zl3073x_chan_tie_write(struct zl3073x_dev *zldev, u8 ch, s64 delta_ns)
 	s64 tie_data;
 	int rc;
 
-	guard(mutex)(&zldev->tie_lock);
-
-	/* Wait for any previous TIE operation to complete */
-	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_DPLL_TIE_CTRL,
-				  ZL_DPLL_TIE_CTRL_OP,
-				  ZL_POLL_TIE_WR_TIMEOUT_US);
-	if (rc)
-		return rc;
+	lockdep_assert_held(&zldev->tie_lock);
 
 	/* Convert ns to TIE units (0.01 ps = 10^-14 s) */
 	tie_data = delta_ns * 100000LL;
@@ -500,6 +514,25 @@ int zl3073x_chan_tie_write(struct zl3073x_dev *zldev, u8 ch, s64 delta_ns)
 				ZL_DPLL_TIE_CTRL_OP_WR);
 }
 
+/**
+ * zl3073x_chan_phase_step_ready_wait - wait for the previous phase step
+ * @zldev: pointer to zl3073x device
+ *
+ * The output phase step operation is shared by all channels, and the device
+ * can take seconds to complete it.
+ *
+ * Context: Caller must hold zldev->phase_step_lock.
+ * Return: 0 on success, <0 on error
+ */
+int zl3073x_chan_phase_step_ready_wait(struct zl3073x_dev *zldev)
+{
+	lockdep_assert_held(&zldev->phase_step_lock);
+
+	return zl3073x_poll_zero_u8(zldev, ZL_REG_OUTPUT_PHASE_STEP_CTRL,
+				    ZL_OUTPUT_PHASE_STEP_CTRL_OP,
+				    ZL_POLL_PHASE_STEP_TIMEOUT_US);
+}
+
 /**
  * zl3073x_chan_phase_step - execute one output phase step operation
  * @zldev: pointer to zl3073x device
@@ -511,6 +544,8 @@ int zl3073x_chan_tie_write(struct zl3073x_dev *zldev, u8 ch, s64 delta_ns)
  * All masked outputs must use synthesizers of the same frequency since
  * the step value is in synthesizer clock cycles.
  *
+ * Context: Caller must hold zldev->phase_step_lock and must have waited for
+ * the previous phase step with zl3073x_chan_phase_step_ready_wait().
  * Return: 0 on success, <0 on error
  */
 int zl3073x_chan_phase_step(struct zl3073x_dev *zldev, u8 ch,
@@ -520,14 +555,7 @@ int zl3073x_chan_phase_step(struct zl3073x_dev *zldev, u8 ch,
 	u8 ctrl;
 	int rc;
 
-	guard(mutex)(&zldev->phase_step_lock);
-
-	/* Wait for any previous phase step operation to complete */
-	rc = zl3073x_poll_zero_u8(zldev, ZL_REG_OUTPUT_PHASE_STEP_CTRL,
-				  ZL_OUTPUT_PHASE_STEP_CTRL_OP,
-				  ZL_POLL_PHASE_STEP_TIMEOUT_US);
-	if (rc)
-		return rc;
+	lockdep_assert_held(&zldev->phase_step_lock);
 
 	rc = zl3073x_write_u32(zldev, ZL_REG_OUTPUT_PHASE_STEP_DATA,
 			       step_cycles);
drivers/dpll/zl3073x/chan.h +2 -0
index 99c3b7088f678f..55932e950e7e6e 100644
@@ -52,11 +52,13 @@ int zl3073x_chan_tod_write(struct zl3073x_dev *zldev, u8 ch,
 			   struct timespec64 ts);
 int zl3073x_chan_tod_adjust(struct zl3073x_dev *zldev, u8 ch,
 			    struct timespec64 delta);
+int zl3073x_chan_phase_step_ready_wait(struct zl3073x_dev *zldev);
 int zl3073x_chan_phase_step(struct zl3073x_dev *zldev, u8 ch,
 			    u16 out_mask, s32 step_cycles, bool tod_step);
 
 int zl3073x_chan_df_offset_set(struct zl3073x_dev *zldev, u8 ch, s64 offset);
 
+int zl3073x_chan_tie_ready_wait(struct zl3073x_dev *zldev);
 int zl3073x_chan_tie_write(struct zl3073x_dev *zldev, u8 ch, s64 delta_ns);
 
 /**
drivers/dpll/zl3073x/dpll.c +123 -30

266 lines not on this page · raw message

drivers/dpll/zl3073x/dpll.h +4 -1
index 993221dc63249d..eda37239ef1e3a 100644
@@ -19,10 +19,12 @@
  * @ops: DPLL device operations for this instance
  * @dpll_dev: pointer to registered DPLL device
  * @tracker: tracking object for the acquired reference
- * @lock: per-DPLL mutex serializing all operations
+ * @lock: per-DPLL mutex serializing DPLL operations and channel state access
  * @type: DPLL type (PPS or EEC)
  * @lock_status: last saved DPLL lock status
  * @pins: list of pins
+ * @ptp_lock: serializes PTP clock operations, taken before @lock, which they
+ *	      hold only while they use the channel state
  * @ptp_info: PTP clock info
  * @ptp_clock: registered PTP clock (or NULL)
  */
@@ -39,6 +41,7 @@ struct zl3073x_dpll {
 	enum dpll_type			type;
 	enum dpll_lock_status		lock_status;
 	struct list_head		pins;
+	struct mutex			ptp_lock;
 	struct ptp_clock_info		ptp_info;
 	struct ptp_clock		*ptp_clock;
 };

base-commit: 4d67cf2fd2352f130c42494a933ba9158b925c65

Signature · 1 line
2.56.1