[PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI

From: Chunfeng Yun

Date: Sat Dec 05 2026 - 03:34:29 EST


Historically, the USB4 / Thunderbolt subsystem has had a hard dependency
on PCI, bundling the core framework, the PCIe-attached Native Host
Interface (NHI) driver (pci.c), and the Intel Connection Manager (icm.c)
into a single module (thunderbolt.ko).

Modern ARM64 SoCs (such as MediaTek MT8901 / MT8992) integrate the USB4
host router and NHI directly as a memory-mapped platform device described
via ACPI ("ACPI0015"). Furthermore, embedded and SoC-based platforms
often lack PCIe-attached host routers and do not require Intel ICM
support, making a mandatory PCI dependency and bundled PCIe code
undesirable.

This series:
1. Fixes an initialization race and error unwind issue in nhi_probe().
2. Refactors and encapsulates internal subsystem interfaces (interrupt
handling, router device type checks, bus walks, and teardown).
3. Decouples PCIe NHI and Intel ICM support from the core framework into
an opt-in module (thunderbolt_pci.ko).
4. Makes struct tb_nhi_ops optional for simple drivers.
5. Introduces a standalone platform NHI driver (thunderbolt_platform.ko).
6. Improves link training timeouts and disconnect handling under dynamic
runtime conditions.

Overview of Changes
===================

1. Standalone Core Bugfix (Patch 1)
- Fixes initialization order and error unwind paths in nhi_probe().
Initializes &nhi->lock and configures the DMA mask before enabling
interrupts to prevent race conditions with early interrupts, and
consolidates post-interrupt failure paths to invoke nhi_shutdown().

2. Subsystem Interface Encapsulation (Patches 2-8)
In preparation for modularizing PCIe NHI support, several internal
abstractions are cleaned up to avoid leaking core internals across
module boundaries:
- Patch 2 renames core ring and NHI symbols to use standard tb_/tb_ring_
prefixes.
- Patch 3 internalizes non-MSI-X interrupt request and work handling
into nhi.c, allowing nhi_isr(), nhi_interrupt_work(), and
nhi_disable_interrupts() to become static, and drops the
.init_interrupts callback from struct tb_nhi_ops.
- Patch 4 encapsulates router type checking within switch.c by moving
tb_to_switch() out-of-line and introducing tb_switch_has_children(),
keeping struct device_type tb_switch_type private to switch.c.
- Patch 5 encapsulates the bus iteration for runtime resume completion
into domain.c as tb_domain_complete_rpm(), keeping tb_bus_type
private to domain.c.
- Patch 6 consolidates domain and NHI teardown into tb_nhi_remove(),
making nhi_shutdown() static and eliminating the need to export
tb_domain_remove() and nhi_shutdown().
- Patch 7 moves Intel ICM mailbox helpers (nhi_mailbox_cmd/mode) into
icm.c as static functions.
- Patch 8 decouples ICM probing from the NHI core by introducing an
optional .select_cm() hook in struct tb_nhi_ops.

3. Modularizing PCIe NHI & Making Ops Optional (Patches 9-10)
- Patch 9 drops the hard "depends on PCI" from CONFIG_USB4, adds
CONFIG_USB4_PCI (defaulting to USB4) to build PCIe NHI and ICM as
thunderbolt_pci.ko, and exports the minimal set of remaining APIs.
Thanks to patches 2-8, internal structures like tb_bus_type and
tb_switch_type do not need to be exported.
- Patch 10 makes struct tb_nhi_ops completely optional by introducing
the TB_NHI_HAS_OP() helper macro and dropping the mandatory
"if (!nhi->ops)" check in tb_nhi_probe(), allowing platform drivers
to operate without any ops table.

4. Platform NHI Driver (Patch 11)
- Introduces CONFIG_USB4_PLATFORM (thunderbolt_platform.ko), adding
support for memory-mapped USB4 NHIs via ACPI ("ACPI0015").
- Supports an optional dedicated wakeup interrupt from an always-on
power domain (registered via dev_pm_set_dedicated_wake_irq()),
necessary for SoCs where the primary NHI power domain is gated
during system sleep.

5. Link Training and Disconnect Improvements (Patches 12-14)
- Patch 12 increases the port link training timeout in
tb_wait_for_port() from 1s to 2s to accommodate Phase 5 equalization
and retimer propagation delays across multi-retimer topologies
during wakeups.
- Patch 13 decouples TB_PORT_UNPLUGGED wait time from the overall 2s
link-up timeout, capping it to 400ms so genuine disconnects are
handled promptly.
- Patch 14 drops an unnecessary WARN_ON() in tb_disconnect_pci() when
userspace deauthorizes a router after a tunnel was already torn down.

Differences since v3
====================
- Re-ordered the nhi_probe() error handling and race condition fix to
Patch 1 as a standalone bugfix with a Fixes: tag.
- Decomposed the previous monolithic modularization patch into discrete
preparatory encapsulation patches (Patches 2-8):
* Internalized primary interrupt request and work into nhi.c.
* Encapsulated router type checks (tb_to_switch / tb_switch_has_children)
in switch.c.
* Encapsulated rpm_complete bus walk in domain.c.
* Consolidated teardown into tb_nhi_remove().
* Decoupled ICM mailbox helpers and probe logic.
- Avoided exporting tb_bus_type, tb_switch_type, tb_domain_remove, and
nhi_shutdown in the PCIe modularization patch.
- Added TB_NHI_HAS_OP() and made struct tb_nhi_ops optional (Patch 10).
- In the platform driver (Patch 11):
* Wrapped wake_irq in a local driver structure (struct tb_nhi_platform)
instead of modifying include/linux/thunderbolt.h.
* Corrected wake IRQ registration order by setting wakeup capability
before dev_pm_set_dedicated_wake_irq().
* Added cleanup for sysfs wakeup nodes on error and device removal.

Testing
=======
This series has been tested on MediaTek ARM64 SoC platforms with an
integrated platform USB4 NHI, verifying:
- Cold boot, device discovery, and tunnel allocation (PCIe, USB3).
- System/runtime suspend and wake-on-connect/disconnect/remote wakeup.
- Link training across wakeups with dual-retimer topologies.
- Concurrent sysfs deauthorization during disconnect events (verifying
absence of WARN_ON splats).
- Modular builds with CONFIG_USB4=m, CONFIG_USB4_PCI=m, and
CONFIG_USB4_PLATFORM=m, as well as built-in configurations.

Chunfeng Yun (13):
thunderbolt: Fix error handling and unwind paths in nhi_probe()
thunderbolt: Prefix NHI core symbols with tb_ and tb_ring_
thunderbolt: Internalize non-MSI-X interrupt request and work in NHI
core
thunderbolt: Encapsulate router device type checks in switch.c
thunderbolt: Move rpm_complete bus walk to domain.c
thunderbolt: Consolidate domain and NHI teardown into tb_nhi_remove()
thunderbolt: Move ICM mailbox functions to icm.c
thunderbolt: Decouple ICM probe from NHI core via .select_cm hook
thunderbolt: Make nhi->ops optional
thunderbolt: Add platform NHI driver
thunderbolt: Increase the lane adapter link-up timeout value
thunderbolt: Decouple the waiting time of TB_PORT_UNPLUGGED and
TB_PORT_UP
thunderbolt: Drop WARN_ON() in tb_disconnect_pci()

Konrad Dybcio (1):
thunderbolt: Make PCIe NHI support opt-in

drivers/thunderbolt/Kconfig | 22 +++-
drivers/thunderbolt/Makefile | 10 +-
drivers/thunderbolt/ctl.c | 6 +-
drivers/thunderbolt/domain.c | 37 +++++-
drivers/thunderbolt/icm.c | 76 ++++++++++---
drivers/thunderbolt/nhi.c | 200 +++++++++++++++------------------
drivers/thunderbolt/nhi.h | 20 ++--
drivers/thunderbolt/pci.c | 124 +++++++-------------
drivers/thunderbolt/platform.c | 155 +++++++++++++++++++++++++
drivers/thunderbolt/switch.c | 63 +++++++++--
drivers/thunderbolt/tb.c | 4 +-
drivers/thunderbolt/tb.h | 12 +-
drivers/thunderbolt/tunnel.c | 1 +
drivers/thunderbolt/xdomain.c | 5 +
include/linux/thunderbolt.h | 2 +
15 files changed, 490 insertions(+), 247 deletions(-)
create mode 100644 drivers/thunderbolt/platform.c

--
2.45.2