From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=60.244.123.138 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796459650; b=nVQPBSYw8Aa+EusdKYgOGe1iFB9+Nio7W5STbX0WlVI5ZMptwLL9OBoq4C0NZHA14nOL sJQZNaMxfRoA3QHYlhDfsoR4TxTSkKyg3uvpuHgMy5ZQ0kaeXyC7PDV9mG4lriOviWUvd HKSorkFwYRTGJWoWZ9IAS4VoVMh/efCgJYxbrhzWlOdWWpnw4LOqe3QO+WGXoTU7RFjtL DLx/+GdN/DJWgkau2eNElgLRmMqrfXIU3yYCEd+5Rna3ehAKI8fh4W+YhuhJcnmD59c4r LB9tZSsrahjAUhwlPMbtCVdf5+hFWlCu86HuMLUQNsgOl5qdxzWbv7xVcbvmzhZnaKg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796459650; h=DMARC-Filter:X-UUID:DKIM-Signature:X-CID-P-RULE:X-CID-O-INFO: X-CID-META:X-CID-BVR:X-CID-BAS:X-CID-FACTOR:X-CID-RHF:X-UUID:Received: Received:Received:From:To:CC:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Transfer-Encoding:Content-Type:X-MTK; bh=m+I+LsU5wVqaA5RFfLxDs1XuFQukeQq9J4H4pyzLtWg=; b=HFLcOVKW6ZU9GJejyRiSg68Rx7GUBfVAHKGV9RFk02KEYDbsc2JdDll9mgtfVQQBGwgW dEdzB/Ht5lclg2T98qdDwMOOWP5buApBHOFXwr7BNei8UOwVjx1S5VgQ6w5kbRkxdsjJa p/rsFWyFNxT+R0M31kMu7TpacelbZGQzlRQv0gfCSlumtTPj/R+yN+vWovz1uEKissDO3 mKfUqzIWdGFXMcK+6WvsP5EtXhsf6Svi+SzPU75hBpI1MbMD8l1waUxHUkG5jMycWSgtI Y2/VV+GbCUr/FQ44LdouxiActRP83poDITvELgqetEQetw4L97JxRu0zSGakumVq8qw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=mediatek.com; spf=pass smtp.mailfrom=mediatek.com; dkim=pass header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256 header.s=dk header.b=XFKvhCOm; arc=none smtp.remote-ip=60.244.123.138 Received: from mailgw01.mediatek.com (unknown [60.244.123.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 648F4606A322; Sat, 5 Dec 2026 08:34:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 648F4606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=mediatek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=mediatek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=mediatek.com header.i=@mediatek.com header.a=rsa-sha256 header.s=dk header.b=XFKvhCOm X-UUID: 9d799200f08611f1b9cf3d4034dccee2-20261205 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mediatek.com; s=dk; h=Content-Type:Content-Transfer-Encoding:MIME-Version:Message-ID:Date:Subject:CC:To:From; bh=m+I+LsU5wVqaA5RFfLxDs1XuFQukeQq9J4H4pyzLtWg=; b=XFKvhCOmZOUAXWjequD07fkPuk7+2yewU8LTSmp8Yz+TToCFV7ffTSkfoYu7vXapADhcYXWJT/2KWON8LY9FkAkwizI5ZAH43Wzfdj1tASoFMQQ64Ved9jBMfKkb0dWF89d7ppCOzWURN2XxCRnhFaB/7jSfTxFHGo5shdQdIT4=; X-CID-P-RULE: Release_Ham X-CID-O-INFO: VERSION:1.3.22,REQID:43574797-3d22-412a-bfa8-48362c766de2,IP:0,U RL:0,TC:0,Content:-25,EDM:0,RT:0,SF:0,FILE:0,BULK:0,RULE:Release_Ham,ACTIO N:release,TS:-25 X-CID-META: VersionHash:d36851b,CLOUDID:6b63c631-f289-4656-bb36-27baf4489f11,B ulkID:nil,BulkQuantity:0,SF:102|836|865|888|898,TC:-5,Content:0|15|50|99,E DM:-3,IP:nil,URL:0,File:130,RT:0,Bulk:nil,QS:nil,BEC:nil,COL:0,OSI:0,OSA:0 ,AV:0,LES:1,SPR:NO,DKR:0,DKP:0,BRR:0,BRE:0,ARC:0 X-CID-BVR: 2,SSN|SDN X-CID-BAS: 2,SSN|SDN,0,_ X-CID-FACTOR: TF_CID_SPAM_SNR X-CID-RHF: D41D8CD98F00B204E9800998ECF8427E X-UUID: 9d799200f08611f1b9cf3d4034dccee2-20261205 Received: from mtkmbs09n1.mediatek.inc [(172.21.101.35)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 2067241073; Sat, 05 Dec 2026 16:33:58 +0800 Received: from mtkmbs13n1.mediatek.inc (172.21.101.193) by mtkmbs13n1.mediatek.inc (172.21.101.193) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.56; Sat, 5 Dec 2026 16:33:57 +0800 Received: from gcnsap21.gcn.mediatek.inc (10.17.81.22) by mtkmbs13n1.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.2.2562.56 via Frontend Transport; Sat, 5 Dec 2026 16:33:56 +0800 From: Chunfeng Yun To: Mika Westerberg CC: Andreas Noever , Konrad Dybcio , Sven Peter , Yehezkel Bernat , Matthias Brugger , Greg Kroah-Hartman , AngeloGioacchino Del Regno , , , , , Chunfeng Yun Subject: [PATCH v4 00/14] thunderbolt: Add platform NHI driver and modularize PCIe NHI Date: Sat, 5 Dec 2026 16:33:25 +0800 Message-ID: <20261205083342.4009278-1-chunfeng.yun@mediatek.com> X-Mailer: git-send-email 2.43.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-MTK: N 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