Linux PCI subsystem development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Manivannan Sadhasivam" <manivannan.sadhasivam@oss.qualcomm.com>
Cc: linux-pci@vger.kernel.org, devicetree@vger.kernel.org,
	robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 3/4] PCI: qcom: Recalibrate the PHY on temperature drift
Date: Wed, 25 Nov 2026 10:45:08 +0000	[thread overview]
Message-ID: <sashiko-outbox-233155@kernel.org> (raw)
In-Reply-To: <20261125-glymur-thermal-fix-v1-3-b93a9feae156@oss.qualcomm.com>

Thank you for your contribution! Sashiko AI review found 8 potential
issues to consider:
- [Medium] PCI: qcom: qcom_pcie_recalib_work() checks target speed
  instead of link state
- [Medium] PCI: qcom: qcom_pcie_recalib_work() corrupts TLS and updates
  ref temp on error
- [Medium] PCI: qcom: qcom_pcie_suspend_noirq() leaves recalib_work
  disarmed on error
- [Medium] PCI: qcom: qcom_pcie_recalib_init() error unwind races with
  active global IRQ
- [Medium] PCI: qcom: qcom_pcie_recalib_work() races with Root Port
  suspend and D3hot
- [Medium] PCI: qcom: stale recalibration reference temp after resume
  and port reset
- [Low] PCI: qcom: missing CONFIG_PCIEPORTBUS dependency for
  pcie_set_target_speed()
- [Low] PCI: qcom: remove unused linux/atomic.h header inclusion
--

commit 2b6d79564efc85fd7cf5340b7e493d9126e31fcc
Author: Manivannan Sadhasivam <manivannan.sadhasivam@oss.qualcomm.com>

PCI: qcom: Recalibrate the PHY on temperature drift

This commit adds periodic thermal zone polling to recalibrate the PCIe PHY on
temperature drift by stepping the Root Port target link speed down to 2.5 GT/s
and restoring it.

> diff --git a/drivers/pci/controller/dwc/pcie-qcom.c b/drivers/pci/controller/dwc/pcie-qcom.c
> index d903f10211c23..fb11718cea39b 100644
> --- a/drivers/pci/controller/dwc/pcie-qcom.c
> +++ b/drivers/pci/controller/dwc/pcie-qcom.c
> @@ -8,6 +8,7 @@
>   * Author: Stanimir Varbanov <svarbanov@mm-sol.com>
>   */
>  
> +#include <linux/atomic.h>

[Severity: Low]
linux/atomic.h does not appear to be used in this file.

[ ... ]

> @@ -1839,6 +1858,197 @@ static void qcom_pcie_icc_opp_update(struct qcom_pcie *pcie)
>  	}
>  }
>  
> +static int qcom_pcie_recalib_update_ref_temp(struct qcom_pcie *pcie)
> +{
> +	unsigned int i;
> +	int temp, ret;
> +
> +	for (i = 0; i < pcie->recalib_num_zones; i++) {
> +		struct qcom_pcie_recalib_zone *zone = &pcie->recalib_zones[i];
> +
> +		ret = thermal_zone_get_temp(zone->tz, &temp);
> +		if (ret)
> +			return ret;
> +
> +		zone->ref = temp;
> +	}
> +
> +	return 0;
> +}
> +
> +#define QCOM_PCIE_RECALIB_INTERVAL_MS	5000
> +
> +static void qcom_pcie_recalib_arm_worker(struct qcom_pcie *pcie)
> +{
> +	if (!pcie->recalib_wq)
> +		return;
> +
> +	queue_delayed_work(pcie->recalib_wq, &pcie->recalib_work,
> +			   msecs_to_jiffies(QCOM_PCIE_RECALIB_INTERVAL_MS));
> +}
> +
> +static void qcom_pcie_recalib_work(struct work_struct *work)
> +{
> +	struct qcom_pcie *pcie = container_of(work, struct qcom_pcie,
> +					      recalib_work.work);
> +	struct dw_pcie_rp *pp = &pcie->pci->pp;
> +	struct device *dev = pcie->pci->dev;
> +	struct pci_dev *port __free(pci_dev_put) = pci_get_slot(pp->bridge->bus, PCI_DEVFN(0, 0));
> +	enum pci_bus_speed speed;
> +	bool drifted = false;
> +	unsigned int i;
> +	u16 lnkctl2;
> +	int temp, ret;
> +
> +	if (!port || !port->subordinate)
> +		goto rearm;
> +
> +	scoped_guard(mutex, &pcie->reset_lock) {
> +		for (i = 0; i < pcie->recalib_num_zones; i++) {
> +			struct qcom_pcie_recalib_zone *zone = &pcie->recalib_zones[i];
> +
> +			ret = thermal_zone_get_temp(zone->tz, &temp);
> +			if (ret) {
> +				dev_err(dev, "Failed to read %s: %d\n",
> +					thermal_zone_device_type(zone->tz), ret);
> +				continue;
> +			}
> +
> +			if (abs(temp - zone->ref) >= zone->drift)
> +				drifted = true;
> +		}
> +
> +		/* No zone has drifted far enough to need a recalibration */
> +		if (!drifted)
> +			break;
> +
> +		ret = pcie_capability_read_word(port, PCI_EXP_LNKCTL2, &lnkctl2);
> +		if (ret != PCIBIOS_SUCCESSFUL)
> +			break;
> +
> +		speed = PCIE_LNKCTL2_TLS2SPEED(lnkctl2);
> +		if (!pcie_valid_speed(speed))
> +			break;
> +
> +		/*
> +		 * Equalization only runs at 8.0 GT/s and above, so a slower
> +		 * link has nothing to recalibrate.
> +		 */
> +		if (speed >= PCIE_SPEED_8_0GT) {

[Severity: Medium]
Can this trigger link retraining when no endpoint is attached, when the link
is down, or when the negotiated link speed is below 8.0 GT/s?

pci_host_probe() enumerates the Root Port and allocates port->subordinate even
when dw_pcie_wait_for_link() fails with -ENODEV, and dw_pcie_setup_rc()
initializes PCI_EXP_LNKCTL2_TLS to the controller's maximum supported speed.
Because this checks the configured target speed in PCI_EXP_LNKCTL2 rather than
whether the link is active and currently operating at 8.0 GT/s or above
(port->subordinate->cur_bus_speed), temperature drift will attempt speed
changes and retrain the link on disconnected slots or Gen 1/Gen 2 endpoints.
In addition, if a Gen 3+ link has downtrained to 2.5 GT/s while TLS remains
>= 8.0 GT/s, pcie_set_target_speed(port, PCIE_SPEED_2_5GT, true) returns 0
early without updating PCI_EXP_LNKCTL2_TLS because bus->cur_bus_speed is
already 2.5 GT/s.

> +			ret = pcie_set_target_speed(port, PCIE_SPEED_2_5GT, true);
> +			if (ret)
> +				dev_err(dev, "Failed to step down the link speed: %d\n", ret);
> +
> +			/*
> +			 * The device below may not be able to train to the
> +			 * original Target Link Speed, which is reported as
> +			 * -EAGAIN even though the speed was restored.
> +			 */
> +			ret = pcie_set_target_speed(port, speed, true);
> +			if (ret && ret != -EAGAIN)
> +				dev_err(dev, "Failed to restore the link speed: %d\n", ret);
> +		}
> +
> +		/*
> +		 * Update the reference temperature of every zone and not just
> +		 * of the one that drifted, so all of them measure their next
> +		 * drift from the same point in time.
> +		 */
> +		ret = qcom_pcie_recalib_update_ref_temp(pcie);
> +		if (ret)
> +			dev_err(dev, "Failed to update the reference temperature: %d\n", ret);
> +	}

[Severity: Medium]
If stepping down to 2.5 GT/s fails in pcie_retrain_link() after
pcie_bwctrl_change_speed() has written 2.5 GT/s to PCI_EXP_LNKCTL2_TLS, can
PCI_EXP_LNKCTL2_TLS be left stuck at 2.5 GT/s?

When the step-down retrain fails, port->subordinate->cur_bus_speed remains at
the original speed, so the subsequent restore call hits the early return in
pcie_set_target_speed():

drivers/pci/pcie/bwctrl.c:pcie_set_target_speed() {
    ...
	if (bus && bus->cur_bus_speed == speed_req)
		return 0;
    ...
}

That skips writing the original target speed back to PCI_EXP_LNKCTL2, leaving
TLS at 2.5 GT/s so future recalibrations are skipped and any later retrain
drops the link to 2.5 GT/s.  Also, qcom_pcie_recalib_update_ref_temp() runs
even when pcie_set_target_speed() fails, advancing zone->ref as if
recalibration succeeded.

> +
> +rearm:
> +	qcom_pcie_recalib_arm_worker(pcie);
> +}
> +
> +#define QCOM_PCIE_RECALIB_ZONE_CELLS	2
> +
> +static int qcom_pcie_recalib_get_zones(struct qcom_pcie *pcie)
> +{
> +	const char *prop = "qcom,pcie-recalibration-thermal-zones";
> +	struct device *dev = pcie->pci->dev;
> +	struct of_phandle_args args;
> +	unsigned int i, num_zones;
> +	int ret;
> +
> +	ret = of_count_phandle_with_args(dev->of_node, prop, NULL);
> +	if (ret == -ENOENT)
> +		return 0;
> +	if (ret < 0)
> +		return ret;
> +
> +	if (!IS_ENABLED(CONFIG_THERMAL_OF))
> +		return dev_err_probe(dev, -ENODEV,
> +				     "%s requires CONFIG_THERMAL_OF\n", prop);

[Severity: Low]
Should this or Kconfig also check for CONFIG_PCIEPORTBUS?  When
CONFIG_PCIEPORTBUS is disabled, pcie_set_target_speed() is a stub returning
-EOPNOTSUPP, so qcom_pcie_recalib_work() will fail and log an error on every
temperature drift.

[ ... ]

> +static int qcom_pcie_recalib_init(struct qcom_pcie *pcie)
> +{
> +	struct device *dev = pcie->pci->dev;
> +	int ret;
> +
> +	if (!pcie->recalib_num_zones)
> +		return 0;
> +
> +	pcie->recalib_wq = alloc_workqueue("pcie-recalib/%s",
> +					   WQ_UNBOUND | WQ_HIGHPRI | WQ_MEM_RECLAIM,
> +					   0, dev_name(dev));

[Severity: Medium]
Can qcom_pcie_recalib_work() race with Root Port runtime PM or system suspend
before qcom_pcie_suspend_noirq() cancels it?

Because the platform device is the parent of the PCI host bridge, the Root
Port and downstream devices run pci_pm_suspend_noirq() (saving PCIe state and
entering PCI_D3hot) before qcom_pcie_suspend_noirq() runs, and runtime PM can
also put the Root Port into PCI_D3hot:

CPU0                                  kworker
pci_pm_suspend_noirq(&port->dev)      qcom_pcie_recalib_work()
                                        pcie_set_target_speed(..., 2.5GT)
  pci_save_state(port)
    pci_save_pcie_state(port)
      /* saves 2.5 GT/s in LNKCTL2 */
  pci_prepare_to_sleep(port)
    /* transitions port to D3hot */
                                        pcie_set_target_speed(..., speed)
                                          /* retrains link in D3hot */
qcom_pcie_suspend_noirq(dev)
  cancel_delayed_work_sync(...)

On resume, pci_restore_pcie_state() will restore the saved 2.5 GT/s Target
Link Speed and permanently downgrade the link, or the worker can access
suspended thermal sensors and retrain the link while the port is in PCI_D3hot.

[ ... ]

> @@ -2430,6 +2644,10 @@ static int qcom_pcie_probe(struct platform_device *pdev)
>  
>  	qcom_pcie_icc_opp_update(pcie);
>  
> +	ret = qcom_pcie_recalib_init(pcie);
> +	if (ret)
> +		goto err_host_deinit;

[Severity: Medium]
What happens if qcom_pcie_recalib_init() fails after the global IRQ has been
requested and PARF_INT_ALL_LINK_DOWN unmasked?

Jumping to err_host_deinit calls dw_pcie_host_deinit(), which frees
pp->bridge->bus, stops the link, and disables clocks via
qcom_pcie_host_deinit() while the devm-managed qcom_pcie_global_irq_thread()
is still active:

CPU0                                  <irq/qcom_pcie_global_irq_thread>
qcom_pcie_probe()
  qcom_pcie_recalib_init() -> err
  dw_pcie_host_deinit(pp)
    pci_remove_root_bus(...)
      pp->bridge->bus = NULL
    dw_pcie_stop_link(pci)
    qcom_pcie_host_deinit(pp)
                                      writel_relaxed(..., pcie->parf + ...)
                                      for_each_pci_bridge(port,
                                                          pp->bridge->bus)
                                        <-- NULL pointer dereference

In addition, qcom_pcie_host_deinit() only calls
pci_pwrctrl_power_off_devices() and does not destroy the pwrctrl devices
created in qcom_pcie_host_init(), leaking them on probe failure.

>  	if (pcie->mhi)
>  		qcom_pcie_init_debugfs(pcie);
>  
> @@ -2464,6 +2682,10 @@ static int qcom_pcie_suspend_noirq(struct device *dev)
>  	if (!pcie)
>  		return 0;
>  
> +	/* Recalibration touches config space, so it must not run past here */
> +	if (pcie->recalib_wq)
> +		cancel_delayed_work_sync(&pcie->recalib_work);
> +
>  	ret = dw_pcie_suspend_noirq(pcie->pci);
>  	if (ret)
>  		return ret;

[Severity: Medium]
If dw_pcie_suspend_noirq() or the subsequent icc_set_bw() call fails in
qcom_pcie_suspend_noirq(), does recalib_work stay permanently cancelled?

When suspend_noirq returns an error, the PM core does not mark the device as
noirq-suspended and skips qcom_pcie_resume_noirq() during rollback, so
qcom_pcie_recalib_arm_worker() is never called again.

> @@ -2575,6 +2797,8 @@ static int qcom_pcie_resume_noirq(struct device *dev)
>  
>  	qcom_pcie_icc_opp_update(pcie);
>  
> +	qcom_pcie_recalib_arm_worker(pcie);

[Severity: Medium]
Should qcom_pcie_recalib_update_ref_temp() be called when the PHY is
re-initialized and retrained in qcom_pcie_resume_noirq() (when
pcie->pci->suspended is true) and in qcom_pcie_reset_root_port()?

In both paths, qcom_pcie_host_init() and dw_pcie_start_link() power-cycle the
PHY and train the link at the current temperature, but zone->ref is left at
the pre-suspend or pre-reset temperature.  If the temperature shifted by less
than zone->drift while suspended and then drifts further in the opposite
direction after resume, the PHY can drift by up to nearly twice zone->drift
from its post-resume calibration temperature without triggering recalibration.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261125-glymur-thermal-fix-v1-0-b93a9feae156@oss.qualcomm.com?part=3

  reply	other threads:[~2026-11-25 10:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 10:17 [PATCH 0/4] PCI: qcom: Recalibrate the PHY on temperature drift Manivannan Sadhasivam via B4 Relay
2026-11-25 10:17 ` [PATCH 1/4] thermal/of: Add thermal_zone_get_zone_by_node() API Manivannan Sadhasivam via B4 Relay
2026-11-25 10:17 ` [PATCH 2/4] dt-bindings: PCI: qcom,pcie-x1e80100: Add qcom,pcie-recalibration-thermal-zones Manivannan Sadhasivam via B4 Relay
2026-11-25 10:17 ` [PATCH 3/4] PCI: qcom: Recalibrate the PHY on temperature drift Manivannan Sadhasivam via B4 Relay
2026-11-25 10:45   ` sashiko-bot [this message]
2026-11-25 10:17 ` [PATCH 4/4] arm64: dts: qcom: glymur: Describe the PCIe thermal inputs Manivannan Sadhasivam via B4 Relay
2026-11-25 10:33   ` Konrad Dybcio
2026-11-25 10:45 ` [PATCH 0/4] PCI: qcom: Recalibrate the PHY on temperature drift sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-233155@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=manivannan.sadhasivam@oss.qualcomm.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox