The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Mika Westerberg <mika.westerberg@linux.intel.com>
To: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
Cc: Vinod Koul <vkoul@kernel.org>,
	Neil Armstrong <neil.armstrong@linaro.org>,
	Manivannan Sadhasivam <mani@kernel.org>,
	Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konradybcio@kernel.org>,
	Abel Vesa <abelvesa@kernel.org>, Stephen Boyd <sboyd@kernel.org>,
	Brian Masney <bmasney+clk@redhat.com>,
	Jerome Brunet <jbrunet+clk@baylibre.com>,
	Andreas Noever <andreas.noever@gmail.com>,
	Mika Westerberg <westeri@kernel.org>,
	Yehezkel Bernat <YehezkelShB@gmail.com>,
	Mario Limonciello <mario.limonciello@amd.com>,
	Philipp Zabel <p.zabel@pengutronix.de>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	linux-arm-msm@vger.kernel.org, linux-phy@lists.infradead.org,
	linux-clk@vger.kernel.org, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, devicetree@vger.kernel.org,
	usb4-upstream@oss.qualcomm.com
Subject: Re: [PATCH RFC DNM 13/15] thunderbolt: Introduce Qualcomm USB4 router driver
Date: Tue, 1 Dec 2026 09:36:18 +0100	[thread overview]
Message-ID: <20261201083618.GV953667@black.igk.intel.com> (raw)
In-Reply-To: <20261124-topic-next-20261116-fsm-mvp-v1-13-a9b45c69cd38@oss.qualcomm.com>

Hi,

On Tue, Nov 24, 2026 at 09:01:42PM +0100, Konrad Dybcio wrote:
> Introduce a driver for the in-house Qualcomm USB4 router driver.
> It consists of resource enablement, uC firmware loading and
> communication, Type-C infrastructure and power state handling.
> 
> Unlike many other implementations, the power state of both the router
> and the native protocol controllers has to be manually coordinated,
> which much of this driver ends up doing.
> 
> Signed-off-by: Konrad Dybcio <konrad.dybcio@oss.qualcomm.com>
> ---
>  drivers/thunderbolt/Kconfig   |   13 +
>  drivers/thunderbolt/Makefile  |    3 +
>  drivers/thunderbolt/eeprom.c  |   17 -
>  drivers/thunderbolt/qcom.c    | 2299 +++++++++++++++++++++++++++++++++++++++++
>  drivers/thunderbolt/qcom.h    |  251 +++++
>  drivers/thunderbolt/tb.h      |   20 +
>  drivers/thunderbolt/tb_regs.h |    3 +
>  7 files changed, 2589 insertions(+), 17 deletions(-)
> 
> diff --git a/drivers/thunderbolt/Kconfig b/drivers/thunderbolt/Kconfig
> index 294b3227a54582536433cbb391057b99bb9df352..94654d3e41137b630fc4723adc4949d8df8b0617 100644
> --- a/drivers/thunderbolt/Kconfig
> +++ b/drivers/thunderbolt/Kconfig
> @@ -64,6 +64,19 @@ config USB4_DMA_TEST
>  	  To compile this driver a module, choose M here. The module will be
>  	  called thunderbolt_dma_test.
>  
> +config USB4_QCOM
> +	tristate "Qualcomm USB4/Thunderbolt Support"
> +	depends on USB4
> +	depends on OF
> +	depends on ARCH_QCOM || COMPILE_TEST
> +	depends on COMMON_CLK && PM
> +	help
> +	  Say Y here to add support for the USB4 and Thunderbolt host
> +	  routers found on Qualcomm SoCs.
> +
> +	  To compile the driver as a module, choose M here. The module
> +	  will be called thunderbolt_qcom.
> +
>  config USB4_STREAM
>  	tristate "Stream data over Thunderbolt/USB4 cable"
>  	depends on USB4_CONFIGFS
> diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile
> index beb054c3126b1445d26b4c14de039d23367a5100..8f56afd7d6b47e95324adbd95f50692ca7b3582d 100644
> --- a/drivers/thunderbolt/Makefile
> +++ b/drivers/thunderbolt/Makefile
> @@ -11,6 +11,9 @@ thunderbolt-$(CONFIG_USB4_CONFIGFS) += configfs.o
>  thunderbolt-${CONFIG_USB4_KUNIT_TEST} += test.o
>  CFLAGS_test.o += $(DISABLE_STRUCTLEAK_PLUGIN)
>  
> +thunderbolt_qcom-objs := qcom.o
> +obj-${CONFIG_USB4_QCOM} += thunderbolt_qcom.o
> +
>  thunderbolt_dma_test-${CONFIG_USB4_DMA_TEST} += dma_test.o
>  obj-$(CONFIG_USB4_DMA_TEST) += thunderbolt_dma_test.o
>  
> diff --git a/drivers/thunderbolt/eeprom.c b/drivers/thunderbolt/eeprom.c
> index 2acfe83928b6d8cc95520479f90b595d7137b829..74be3295a0a241f17c833d143d0674e18ffb9bb0 100644
> --- a/drivers/thunderbolt/eeprom.c
> +++ b/drivers/thunderbolt/eeprom.c
> @@ -197,23 +197,6 @@ static int tb_eeprom_read_n(struct tb_switch *sw, u16 offset, u8 *val,
>  	return tb_eeprom_active(sw, false);
>  }
>  
> -static u8 tb_crc8(u8 *data, int len)
> -{
> -	int i, j;
> -	u8 val = 0xff;
> -	for (i = 0; i < len; i++) {
> -		val ^= data[i];
> -		for (j = 0; j < 8; j++)
> -			val = (val << 1) ^ ((val & 0x80) ? 7 : 0);
> -	}
> -	return val;
> -}
> -
> -static u32 tb_crc32(void *data, size_t len)
> -{
> -	return ~crc32c(~0, data, len);
> -}

Maybe make this separate preparatory patch?

> -
>  #define TB_DROM_DATA_START		13
>  #define TB_DROM_HEADER_SIZE		22
>  #define USB4_DROM_HEADER_SIZE		16
> diff --git a/drivers/thunderbolt/qcom.c b/drivers/thunderbolt/qcom.c
> new file mode 100644
> index 0000000000000000000000000000000000000000..721feeccd7364e21bb1e2ecb2d97967bee5bc5cf
> --- /dev/null
> +++ b/drivers/thunderbolt/qcom.c
> @@ -0,0 +1,2299 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
> + */
> +
> +#include <linux/align.h>
> +#include <linux/clk.h>
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/devm-helpers.h>
> +#include <linux/errno.h>
> +#include <linux/firmware.h>
> +#include <linux/interrupt.h>
> +#include <linux/iopoll.h>
> +#include <linux/mfd/syscon.h>
> +#include <linux/module.h>
> +#include <linux/overflow.h>
> +#include <linux/phy/phy.h>
> +#include <linux/pinctrl/consumer.h>
> +#include <linux/platform_device.h>
> +#include <linux/pm_domain.h>
> +#include <linux/pm_opp.h>
> +#include <linux/pm_runtime.h>
> +#include <linux/pm_wakeirq.h>
> +#include <linux/property.h>
> +#include <linux/regmap.h>
> +#include <linux/reset.h>
> +#include <linux/slab.h>
> +#include <linux/soc/qcom/smem.h>
> +#include <linux/unaligned.h>
> +#include <linux/units.h>
> +#include <linux/usb/pd.h>
> +#include <linux/usb/typec_altmode.h>
> +#include <linux/usb/typec_mux.h>
> +#include <linux/usb/typec_tbt.h>
> +#include <linux/usb/typec.h>
> +#include <linux/workqueue.h>

Are you sure you need all of these?

> +
> +#include "nhi.h"
> +#include "nhi_regs.h"
> +#include "qcom.h"
> +#include "tb.h"
> +
> +/*
> + * The router's operational ability is directly tied to the mode and power state of the QMPPHY
> + * (and thus to cable presence). The router (transitively) sources its clocks from the PHY,
> + * although it may operate in reduced capacity even when it's off (by muxing its inputs to the
> + * 19.2 MHz XO clock source). That 'reduced mode' allows for register access, but not TBT linkup.

TB or Thunderbolt

Ditto everywhere

> + *
> + * At probe time, that mechanism is used to bootstrap the router regardless of the plug state
> + * (since forcefully switching the PHY to USB4 mode would disrupt a USB3/DP link on that port).
> + * Then, upon a USB4/TBT3 typec_mux event the PHY is reconfigured, the clocks are reconfigured to
> + * follow, the uC is brought online and a BEGIN_HSE command is sent to the router, which can take
> + * a couple seconds to complete.
> + *
> + * Upon unplug, the router has to be immediately torn down (including the clocks scaffolding and
> + * the PHY) to unblock the port for a potentially soon-to-be-plugged USB3/DP device.
> + *
> + * Suspend/resume sequences require a handshake with the uC, as that influences the state of some
> + * clocks and failing to follow that ends up in a hard crash of the entire platform.
> + *
> + * This is modeled as an explicit state machine (enum qcusb4_hw_state), with all transitions
> + * serialized by state_lock:
> + *
> + *   OFF --probe--> IDLE <--reconcile--> LINK
> + *                    \                   /
> + *                     +---> ASLEEP <----+   (runtime/system sleep, uC handshake)
> + *
> + * The Type-C callbacks record what state they want (->desired_state) and kick the reconcile work,
> + * which runs the IDLE <-> LINK transitions. Cable removal is LINK -> IDLE, which cycles the domain
> + * and the router through a full sleep (TBT layer _SLP first, then the uC handshake) to hand the PHY
> + * back, then cold-restarts the router on XO (a sleep exit would have the FW go for the PHY it no
> + * longer has), so the TBT core sees a regular unplug. The PHY must never be taken away outside of
> + * that handshake. For the same reason, sleep entered from IDLE is exited through a cold restart, as
> + * the cable may have been plugged in meanwhile. Two gates protect against unclocked access from
> + * outside the state machine: uc_online for FW commands and mmio_ok for the PCIe reset controller.
> + */
> +
> +/* ROUTER_CS registers */
> +#define ROUTER_CS_CS_1			0x4
> +#define ROUTER_CS_CS_5			0x14

CS_CS? And we do have ROUTER_CS alread in tb_regs.h. If these are not the
same perhaps prefix them QCOM_ or so to avoid confusion?

> +
> +#define is_usb4v2(q)			((q)->match_data->entries[(q)->instance_idx].usb4v2_capable)

Maybe inline function instead to make this somewhat more readable.

> +
> +/* LANEn_CS registers */
> +#define UC_RAM_DROM_OFFSET_V1		0xdf20
> +#define UC_RAM_DROM_OFFSET_V2		0xff20
> +#define UC_RAM_DROM_OFFSET(q)		(is_usb4v2((q)) ? \
> +					 UC_RAM_DROM_OFFSET_V2 : UC_RAM_DROM_OFFSET_V1)
> +
> +#define LANEn_CS_CS_1(q)		(is_usb4v2(q) ? 0x58 : 0x50)
> +#define LANEn_CS_CS_1_ADAPTER_STATE	GENMASK(29, 26)
> +#define LANEn_CS_CS_18(q)		(is_usb4v2(q) ? 0xac : 0xa4)
> +#define LANEn_CS_CS_19(q)		(is_usb4v2(q) ? 0xb0 : 0xa8)

Here also depending on where these are used but maybe more readable is to
have a separate function or block.

> +
> +/* PORT_GROUP registers */
> +#define PORT_GROUP_LANE_CFG		0x0
> +#define LANE_CFG_CLx_SUPPORT(q)		(is_usb4v2(q) ? GENMASK(21, 19) : GENMASK(23, 21))
> +#define PORT_GROUP_FCM_2_CFG		0x64
> + #define FCM_2_CFG_DIS_ANY_HOPID	BIT(6)

I prefer not to indent these. Let's try to be consistent across.

> +
> +/* SIDEBAND registers */
> +#define SIDEBAND_TX_CTRL		0xc
> + #define SB_TX_CTRL_TX_ENABLE		BIT(31)
> +
> +/* UC_PER registers */
> +#define UC_PER_CONTROL			0x1000
> + #define PER_CONTROL_UC_ENABLE		BIT(0)
> +#define UC_PER_SW_TRIGGERS_UC_IRQ	0x100c
> +#define UC_PER_FW_INT_STATUS_SET	0x1010
> +#define UC_PER_FW_INT_CLR		0x1018
> + #define PER_FW_INT_CLR_CLR		BIT(0)
> +#define UC_PER_FW_INT_MASK		0x101c
> + #define PER_FW_INT_MASK_FW_INT		BIT(0)
> +
> +/* CFG registers */
> +#define CFG_SYS_CLK_CGC			0x0
> + #define SYS_CLK_CGC_SYS_CLK_EN_USBAP	BIT(7)
> +
> +/* USBAP_CONFIG registers */
> +#define USBAP_CFG_SYNC_RST_USB3		0x0
> + #define RST_USB3_RST			BIT(0)
> +#define USBAP_CFG_USB4_MODE		0x4
> + #define USB4_MODE_USB4_MODE		BIT(0)
> +#define USBAP_CFG_RXTERM_BACKDOOR_CTRL	0x10
> +#define USBAP_CFG_RXTERM_BACKDOOR_STS	0x14
> + #define RXTERM_RX_TERM			BIT(0)
> +#define USBAP_CFG_PHYSTATUS_EN		0x18
> + #define PHYSTATUS_EN_EN		BIT(0)
> +
> +/* PCIEAP_CONFIG registers */
> +#define PCIEAP_CONFIG_SYNC_RST		0x0
> + #define PCIEAP_SYNC_RST_PCIE		BIT(0)
> +#define PCIEAP_CONFIG_IRQ_STATUS	0x0c
> + #define PCIEAP_IRQ_PE_DEASSERT		BIT(3)
> + #define PCIEAP_IRQ_PE_ASSERT		BIT(2)
> +#define PCIEAP_CONFIG_IRQ_MASK		0x10
> +#define PCIEAP_CONFIG_IRQ_CLEAR		0x14
> +
> +/* PCIEAP (USB4 v2 router) registers */
> +#define PCIEAP_TX_CFG			0x0
> + #define PCIEAP_TX_CFG_EN_FLUSH_MISSING_SOP	BIT(22)
> +
> +/* ROUTER_CONFIG registers */
> +#define ROUTER_CONFIG_CS_6		0x18
> + #define CS_6_ROUTER_READY		BIT(24)
> +#define ROUTER_CONFIG_CS_7		0x1c
> +#define ROUTER_CONFIG_CS_8		0x20

Also ROUTER_CONFIG_CS and ROUTER_CS_CS? Its fine if these match the
datasheet but if you invented them then I suggest rethink the naming so
that it's not that confusing ;-)

> +
> +struct qcom_usb4_fw_event {
> +	u8 opcode;
> +	u8 params[];
> +} __packed;

Please check if you really need the __packed.

> +struct qcom_usb4_fw_events {
> +	__le16 num_entries;
> +	__le16 entry_size;
> +	__le32 next_entry_index;
> +	__le16 num_unread_entries; /* This only guarantees num_unread >= num_entries */
> +	u8 res[6];
> +	struct qcom_usb4_fw_event entries[];
> +} __packed;
> +
> +#define FW_EV_BUF_SIZE			SZ_8K
> +
> +enum qcom_usb4_fw_cmd {
> +	FW_CMD_BEGIN_HSE = 0x1,
> +
> +	FW_CMD_ENABLE_PHY_PRESETS = 0x2,
> +#define PHY_PRESETS_DEFAULT (BIT(0) | BIT(1) | BIT(5) | BIT(6) | BIT(10) | BIT(11))

Also perhaps move these outside of the enum?

And while there maybe kernel-doc?

> +
> +	FW_CMD_LIMIT_LOGLEVEL = 0x3,
> +#define LOG_LEVEL_ALL			0
> +#define LOG_LEVEL_WARN			1
> +#define LOG_LEVEL_ERR			2
> +#define LOG_LEVEL_NONE			3
> +
> +	/* Only makes sense to call this after FW_MSG_SLEEP_READY */
> +	FW_CMD_COMPLETE_SLEEP = 0x4,
> +
> +	/* Reconfigure the internals to restore the link (or handle an in-sleep disconnect) */
> +	FW_CMD_WAKEUP = 0x5,
> +#define WAKEUP_REASON_DEVICE		0
> +#define WAKEUP_REASON_CBL_DIS		1
> +#define WAKEUP_REASON_CBL_CON		2
> +#define WAKEUP_REASON_SYSTEM		3
> +
> +	/* The router alone can't differentiate between physical and logical disconnects */
> +	FW_CMD_PHYS_DISCONNECT = 0x6,
> +
> +	FW_CMD_DEPRECATED	= 0x7,

This is not used anywhere so drop it (also ditto similar if there are).

> +
> +	/* USB4v2 introduced more tuning presets */
> +	FW_CMD_ENABLE_PHY_PRESETS_MSB = 0x8,
> +
> +	/* Notify the uC that we're done with the Host Router Reset */
> +	FW_CMD_HRR_DONE = 0x9,
> +
> +	FW_CMD_WA_DONE = 0xa,
> +#define WA_DONE_USBAP_RXTERM		1
> +};
> +
> +/* Must be called with the mux clocks enabled */
> +static int qcusb4_select_rx_clock_source(struct qcom_usb4 *qcusb4,
> +					 bool fast_clock)

qcom_usb4_?

> +{
> +	struct clk *rx0 = qcusb4->mux_clks[MUX_CLK_PHY_RX0].clk;
> +	struct clk *rx1 = qcusb4->mux_clks[MUX_CLK_PHY_RX1].clk;
> +	unsigned long rate = fast_clock ? ULONG_MAX : 19200000;
> +	int ret;
> +
> +	clk_disable_unprepare(rx0);
> +	clk_disable_unprepare(rx1);
> +
> +	/* No error unwinding on purpose, failure here is borderline impossible and catastrophic */
> +	ret = clk_set_rate(rx0, rate);
> +	if (ret) {
> +		dev_err(qcusb4->nhi.dev, "Failed to select RX0 clock source: %d\n", ret);

"failed .."

To be consistent with the rest of the driver.

Ditto everywhere.

> +		return ret;
> +	}
> +
> +	ret = clk_set_rate(rx1, rate);
> +	if (ret) {
> +		dev_err(qcusb4->nhi.dev, "Failed to select RX1 clock source: %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = clk_prepare_enable(rx0);
> +	if (ret) {
> +		dev_err(qcusb4->nhi.dev, "Failed to re-enable the RX0 clock: %d\n", ret);
> +		return ret;
> +	}
> +
> +	ret = clk_prepare_enable(rx1);
> +	if (ret)
> +		dev_err(qcusb4->nhi.dev, "Failed to re-enable the RX1 clock: %d\n", ret);

It's fine if these fail that they are set to prepared and configured?

> +
> +	return ret;
> +}
> +
> +static int __qcusb4_fw_cmd(struct qcom_usb4 *qcusb4, enum qcom_usb4_fw_cmd msg, u32 args)

__qcom_usb4_?

> +{
> +	u32 cmd;
> +	u32 val;
> +
> +	lockdep_assert_held(&qcusb4->uc_cmd_lock);
> +
> +	/* The uC is either down, or about to be */
> +	if (!qcusb4->uc_online) {
> +		dev_warn_ratelimited(qcusb4->nhi.dev,
> +				     "Dropping cmd=0x%x arg=0x%x, the uC is not running\n",
> +				     msg, args);

Does this happen how often? Is ratelimited needed? Maybe this can be debug
instead or drop completely?

> +		return -EAGAIN;
> +	}
> +
> +	pm_runtime_mark_last_busy(qcusb4->nhi.dev);
> +
> +	cmd = FIELD_PREP(GENMASK(31, 8), args) |
> +	      FIELD_PREP(GENMASK(7, 0), msg);
> +
> +	writel(cmd, qcusb4->uc_per + UC_PER_SW_TRIGGERS_UC_IRQ);
> +
> +	if (readl_poll_timeout(qcusb4->uc_per + UC_PER_SW_TRIGGERS_UC_IRQ,
> +			       val, !val, 10, 10 * USEC_PER_MSEC)) {

Maybe #define for the timeout?

> +		dev_err(qcusb4->nhi.dev, "Firmware failed to clear cmd 0x%08x\n", cmd);
> +		return -ETIMEDOUT;
> +	}
> +
> +	return 0;
> +}
> +
> +static int qcusb4_fw_cmd(struct qcom_usb4 *qcusb4, enum qcom_usb4_fw_cmd msg, u32 args)
> +{
> +	guard(mutex)(&qcusb4->uc_cmd_lock);
> +
> +	return __qcusb4_fw_cmd(qcusb4, msg, args);
> +}
> +
> +/*
> + * Send a command that the FW acks with an event. The completion is re-armed under uc_cmd_lock,
> + * so that a stale ack from an earlier command can't satisfy the wait.
> + */
> +static int qcusb4_fw_cmd_wait(struct qcom_usb4 *qcusb4, enum qcom_usb4_fw_cmd msg, u32 args,
> +			      struct completion *done, unsigned int timeout_ms)
> +{
> +	int ret;
> +
> +	scoped_guard(mutex, &qcusb4->uc_cmd_lock) {

		int ret;

> +		reinit_completion(done);
> +

Drop this empty line.

> +		ret = __qcusb4_fw_cmd(qcusb4, msg, args);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	if (!wait_for_completion_timeout(done, msecs_to_jiffies(timeout_ms)))
> +		return -ETIMEDOUT;
> +
> +	return 0;
> +}
> +
> +enum qcom_usb4_fw_msg {
> +	FW_MSG_MESSAGE = 0x1,
> +	FW_MSG_ERR = 0x2,
> +	FW_MSG_TEST_RES = 0x3,
> +	/* Fires after a successful handling of FW_CMD_COMPLETE_SLEEP */

You can use kernel-doc as well to document these.

> +	FW_MSG_SLEEP_DONE = 0x4,
> +	/* Fires after a packet with opcode = USB4_SB_OPCODE_ROUTER_OFFLINE is handled */
> +	FW_MSG_EXIT_OFFLINE = 0x5,
> +	/* Fires after any sort of (incl. logical) disconnect has been detected */
> +	FW_MSG_DISCONNECT = 0x6,
> +	/* Fires after a successful handling of ROUTER_CS_5.SLP becoming set */
> +	FW_MSG_SLEEP_READY = 0x7,
> +	/* "A specific software workaround must be applied immediately" */
> +	FW_MSG_TRIGGER = 0x8,
> +#define WA_TRIGGER_USBAP_RXTERM		3
> +
> +	/* "found hotplug with HopId > 1" */
> +	FW_MSG_CHAINED_HOTPLUG = 0x9,
> +	/* Fires after a HRR request arrives, needs to be acked with FW_CMD_HRR_DONE */
> +	FW_MSG_HOST_RESET = 0xa,
> +	FW_MSG_HSE_STATS = 0xb,
> +	FW_MSG_FOM_STATS = 0xc,
> +};

Move this to the top of the file.

> +
> +static void qcom_usb4_fw_clear_irq(struct qcom_usb4 *qcusb4)
> +{
> +	/* Clear the irq - this resets the unread message counter on the FW side */
> +	writel(PER_FW_INT_CLR_CLR, qcusb4->uc_per + UC_PER_FW_INT_CLR);
> +	readl(qcusb4->uc_per + UC_PER_FW_INT_CLR);
> +}
> +
> +static bool qcom_usb4_fw_event_ring_valid(struct qcom_usb4 *qcusb4, u32 data_off,
> +					  u16 total_entries, u16 entry_size, u32 *ring_size)
> +{
> +	u32 entries_size;
> +
> +	if (!total_entries || entry_size < sizeof(struct qcom_usb4_fw_event))
> +		return false;
> +
> +	if (check_mul_overflow((u32)total_entries, (u32)entry_size, &entries_size))

You really need the casts here and below?

> +		return false;
> +
> +	if (check_add_overflow((u32)sizeof(struct qcom_usb4_fw_events),
> +			       entries_size, ring_size))
> +		return false;

Functions that modify the passed parameters should do so only when they
actually succeed.

> +
> +	if (*ring_size > FW_EV_BUF_SIZE)
> +		return false;
> +
> +	if (data_off > qcusb4->uc_ram_size - *ring_size)
> +		return false;
> +
> +	return true;
> +}
> +
> +/*
> + * The FW exposes a ring buffer with no 'event processing done' acking (i.e. the event may be gone
> + * before we process it).

I think you should wrap these.

> + *
> + * Accessing that data happens by directly reading the uC's RAM at a given offset.
> + * The uC is clocked by the SYS clock (part of qcusb4->mux_clks), so care must be taken to disable
> + * this IRQ, to prevent system crashes.
> + */
> +static irqreturn_t qcom_usb4_fw_irq_handler(int irq, void *data)
> +{
> +	struct qcom_usb4 *qcusb4 = data;
> +	u16 total_entries;
> +	u16 entry_size;
> +	u32 ring_size;
> +	u32 data_off;
> +
> +	if (unlikely(!qcusb4->fw_ev_rb_base)) {

Drop the unlikely.

It reads better actually if you do:

	if (qcusb4->fw_ev_rb_base)
		return IRQ_WAKE_THREAD;

	data_off = readl(qcusb4->uc_per + UC_PER_FW_INT_STATUS_SET);
	...

> +		data_off = readl(qcusb4->uc_per + UC_PER_FW_INT_STATUS_SET);
> +		if (!IS_ALIGNED(data_off, sizeof(u32)) ||
> +		    data_off > qcusb4->uc_ram_size - sizeof(struct qcom_usb4_fw_events)) {
> +			dev_err_ratelimited(qcusb4->nhi.dev,
> +					    "Invalid FW event ring offset 0x%x\n",
> +					    data_off);
> +			qcom_usb4_fw_clear_irq(qcusb4);
> +			return IRQ_HANDLED;
> +		}
> +
> +		total_entries = lower_16_bits(readl(qcusb4->uc_ram + data_off));
> +		entry_size = upper_16_bits(readl(qcusb4->uc_ram + data_off));
> +
> +		if (!qcom_usb4_fw_event_ring_valid(qcusb4, data_off, total_entries,
> +						   entry_size, &ring_size)) {
> +			dev_err_ratelimited(qcusb4->nhi.dev,
> +					    "Invalid FW event ring: off=0x%x entries=%u size=%u\n",
> +					    data_off, total_entries, entry_size);
> +			qcom_usb4_fw_clear_irq(qcusb4);
> +			return IRQ_HANDLED;
> +		}
> +
> +		qcusb4->fw_ev_rb_base = qcusb4->uc_ram + data_off;
> +		qcusb4->fw_ev_total_entry_num = total_entries;
> +		qcusb4->fw_ev_entry_size = entry_size;
> +		qcusb4->fw_ev_ring_size = ring_size;
> +		qcusb4->next_fw_ev_idx = 0;
> +	}
> +
> +	return IRQ_WAKE_THREAD;
> +}
> +
> +static int qcusb4_usbap_rxterm_wa(struct qcom_usb4 *qcusb4)
> +{
> +	struct device *dev = qcusb4->nhi.dev;
> +	u32 val;
> +	int ret;
> +
> +	val = readl(qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN);
> +	writel(val & ~PHYSTATUS_EN_EN, qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN);
> +
> +	val = readl(qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3);
> +	writel(val | RST_USB3_RST, qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3);
> +
> +	ret = reset_control_assert(qcusb4->usb_pipe_reset);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "Couldn't assert usb_pipe reset\n");
> +
> +	usleep_range(100, 150);
> +
> +	val = readl(qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN);
> +	writel(val | PHYSTATUS_EN_EN, qcusb4->usb_cfg + USBAP_CFG_PHYSTATUS_EN);
> +
> +	val = readl(qcusb4->cfg + CFG_SYS_CLK_CGC);
> +	writel(val & ~SYS_CLK_CGC_SYS_CLK_EN_USBAP, qcusb4->cfg + CFG_SYS_CLK_CGC);
> +
> +	ret = reset_control_deassert(qcusb4->usb_pipe_reset);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "Couldn't deassert usb_pipe reset\n");
> +
> +	usleep_range(100, 150);
> +
> +	val = readl(qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3);
> +	writel(val & ~RST_USB3_RST, qcusb4->usb_cfg + USBAP_CFG_SYNC_RST_USB3);
> +
> +	val = readl(qcusb4->usb_cfg + USBAP_CFG_RXTERM_BACKDOOR_CTRL);
> +	writel(val | RXTERM_RX_TERM, qcusb4->usb_cfg + USBAP_CFG_RXTERM_BACKDOOR_CTRL);
> +
> +	return qcusb4_fw_cmd(qcusb4, FW_CMD_WA_DONE, WA_DONE_USBAP_RXTERM);
> +}
> +
> +static irqreturn_t qcom_usb4_fw_irq_thread(int irq, void *data)
> +{
> +	struct qcom_usb4 *qcusb4 = data;
> +	struct qcom_usb4_fw_events *fw_events = qcusb4->fw_events_buf;
> +	struct device *dev = qcusb4->nhi.dev;
> +	u32 entry_size = qcusb4->fw_ev_entry_size;
> +	u16 num_new_entries, num_to_end;
> +	void __iomem *entries;
> +	u32 next_entry_index;
> +
> +	if (!qcusb4->fw_ev_rb_base || !qcusb4->fw_ev_ring_size) {

How this can happen BTW?

> +		qcom_usb4_fw_clear_irq(qcusb4);
> +		return IRQ_HANDLED;
> +	}
> +
> +	/*
> +	 * Clear before reading the write index. Otherwise an event written in between gets its IRQ
> +	 * cleared, but not read, and sits in the ring until something else fires the IRQ.

Wrap these too. Ditto everywhere.

> +	 */
> +	qcom_usb4_fw_clear_irq(qcusb4);
> +
> +	next_entry_index = readl(qcusb4->fw_ev_rb_base +
> +				 offsetof(struct qcom_usb4_fw_events, next_entry_index));

Would this read better if it is called "index"? Ditto for the other long
names where it makes things more readable.

> +	if (next_entry_index >= qcusb4->fw_ev_total_entry_num) {
> +		dev_err_ratelimited(dev, "Invalid FW event index %u/%u\n",
> +				    next_entry_index, qcusb4->fw_ev_total_entry_num);

Same question about the ratelimit here. Seems it is common to get invalid
events from the FW?

> +		return IRQ_HANDLED;
> +	}
> +
> +	/* Check for ringbuffer rollover */
> +	if (next_entry_index < qcusb4->next_fw_ev_idx)
> +		num_new_entries = qcusb4->fw_ev_total_entry_num - qcusb4->next_fw_ev_idx
> +				  + next_entry_index;
> +	else
> +		num_new_entries = next_entry_index - qcusb4->next_fw_ev_idx;
> +
> +	/* Grab the header and the new entries (wrapping around) from the uC's RAM */
> +	memcpy_fromio(fw_events, qcusb4->fw_ev_rb_base, sizeof(*fw_events));
> +	entries = qcusb4->fw_ev_rb_base + offsetof(struct qcom_usb4_fw_events, entries);
> +	num_to_end = min_t(u16, num_new_entries,
> +			   qcusb4->fw_ev_total_entry_num - qcusb4->next_fw_ev_idx);
> +	memcpy_fromio((void *)fw_events->entries + entry_size * qcusb4->next_fw_ev_idx,
> +		      entries + entry_size * qcusb4->next_fw_ev_idx, entry_size * num_to_end);
> +	memcpy_fromio(fw_events->entries, entries, entry_size * (num_new_entries - num_to_end));
> +	/* Make sure we get the events before proceeding */
> +	mb();
> +
> +	if (le16_to_cpu(fw_events->num_unread_entries) >= qcusb4->fw_ev_total_entry_num)
> +		dev_warn_ratelimited(dev, "FW event ring overflowed, events were lost\n");
> +
> +	for (int i = 0; i < num_new_entries; i++) {
> +		struct qcom_usb4_fw_event *ev;
> +		u16 payload_size = qcusb4->fw_ev_entry_size - sizeof(*ev);

Reverse-christmas tree where possible.

> +
> +		ev = (void *)fw_events->entries +
> +		     (qcusb4->fw_ev_entry_size * qcusb4->next_fw_ev_idx++);

This is hard to parse and easy to get wrong so perhaps split it to a couple
of statements. Also check if you need the cast here.

> +
> +		switch (ev->opcode) {
> +		case FW_MSG_MESSAGE:
> +			dev_warn(qcusb4->nhi.dev, "FW MSG: %.*s\n", payload_size, ev->params);
> +			break;
> +		case FW_MSG_ERR:
> +			if (payload_size < 3) {
> +				dev_err(dev, "Malformed FW error event\n");
> +				break;
> +			}
> +
> +			u8 error = ev->params[0];
> +			u16 extradata = (ev->params[2] << 8) | ev->params[1];

Please then make this a block with {
}

> +
> +			dev_err(qcusb4->nhi.dev, "Received FW error: %u (data: 0x%x)\n",
> +				error, extradata);
> +			break;
> +		case FW_MSG_SLEEP_DONE:
> +			complete(&qcusb4->sleep_acked);
> +			dev_err(qcusb4->nhi.dev, "FW Sleep done\n");

dev_err()?

> +			break;
> +		case FW_MSG_EXIT_OFFLINE:
> +			dev_err(qcusb4->nhi.dev, "FW exit offline\n");

???

Okay I stoped here for now. As you say this is RFC so not yet production
quality ;-) Thanks for posting, though. I now understand better the
challenges "more OS control" poses. But we are getting there.. :)

  parent reply	other threads:[~2026-12-01  8:36 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 20:01 [RFC DNM PATCH 00/15] Qualcomm USB4 host router Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 01/15] phy: qualcomm: qmp-combo: Add Glymur USB4v1 PHY sequences Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 02/15] phy: qualcomm: qmp-combo: Add USB4 power_on/off to de/assert USB4 clamps Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 03/15] soc: qcom: smem: Add and export qcom_smem_get_serial_no() Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 04/15] soc: qcom: pmic_glink_altmode: Enable TBT/USB4 negotiation Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 05/15] clk: qcom: gcc-glymur: Don't poll the status bit of some USB4 clocks Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 06/15] thunderbolt: nhi: Clear ring interrupts when not using MSI-X Konrad Dybcio
2026-12-01  7:16   ` Mika Westerberg
2026-11-24 20:01 ` [PATCH RFC DNM 07/15] thunderbolt: nhi: Process ring completions in the threaded IRQ Konrad Dybcio
2026-12-01  7:33   ` Mika Westerberg
2026-11-24 20:01 ` [PATCH RFC DNM 08/15] thunderbolt: nhi: Add pre_switch_suspend hook Konrad Dybcio
2026-12-01  7:41   ` Mika Westerberg
2026-11-24 20:01 ` [PATCH RFC DNM 09/15] thunderbolt: nhi: Resume the domain if the NHI suspend op fails Konrad Dybcio
2026-12-01  7:44   ` Mika Westerberg
2026-11-24 20:01 ` [PATCH RFC DNM 10/15] thunderbolt: nhi: Resume a runtime suspended NHI for system suspend Konrad Dybcio
2026-12-01  7:49   ` Mika Westerberg
2026-11-24 20:01 ` [PATCH RFC DNM 11/15] thunderbolt: Raise TB_TIMEOUT to 500 ms Konrad Dybcio
2026-11-24 20:01 ` [PATCH HACK RFC DNM 12/15] thunderbolt: Refuse to activate DP/USB3/PCIe tunnels Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 13/15] thunderbolt: Introduce Qualcomm USB4 router driver Konrad Dybcio
2026-11-28 13:54   ` Troels Vognbjerg
2026-12-01  8:36   ` Mika Westerberg [this message]
2026-11-24 20:01 ` [PATCH RFC DNM 14/15] arm64: dts: qcom: glymur: Add USB4 topology Konrad Dybcio
2026-11-24 20:01 ` [PATCH RFC DNM 15/15] arm64: dts: qcom: glymur-crd: Enable the tertiary Type-C Konrad Dybcio

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=20261201083618.GV953667@black.igk.intel.com \
    --to=mika.westerberg@linux.intel.com \
    --cc=YehezkelShB@gmail.com \
    --cc=abelvesa@kernel.org \
    --cc=andersson@kernel.org \
    --cc=andreas.noever@gmail.com \
    --cc=bmasney+clk@redhat.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=jbrunet+clk@baylibre.com \
    --cc=konrad.dybcio@oss.qualcomm.com \
    --cc=konradybcio@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-phy@lists.infradead.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=mani@kernel.org \
    --cc=mario.limonciello@amd.com \
    --cc=neil.armstrong@linaro.org \
    --cc=p.zabel@pengutronix.de \
    --cc=robh@kernel.org \
    --cc=sboyd@kernel.org \
    --cc=usb4-upstream@oss.qualcomm.com \
    --cc=vkoul@kernel.org \
    --cc=westeri@kernel.org \
    /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