[PATCH v4 09/14] thunderbolt: Make PCIe NHI support opt-in
From: Chunfeng Yun
Date: Sat Dec 05 2026 - 03:34:36 EST
From: Konrad Dybcio <konrad.dybcio@xxxxxxxxxxxxxxxx>
Currently, the USB4/Thunderbolt subsystem directly depends on PCI,
tightly bundling the core framework with the PCIe-attached Native Host
Interface (NHI) driver (pci.c) and the Intel Connection Manager (icm.c)
into a single module (thunderbolt.o).
With support for platform/SoC-attached USB4 host controllers coming in
(such as on ARM64 systems), the core USB4 framework should no longer
have a hard dependency on PCI. Furthermore, embedded or SoC systems
without PCIe-attached host routers should be able to omit PCIe NHI
support to reduce kernel footprint.
Decouple the PCIe NHI and ICM drivers from the core framework:
1. Kconfig & Makefile:
- Remove "depends on PCI" from CONFIG_USB4.
- Introduce CONFIG_USB4_PCI to build PCIe NHI and ICM support as
a separate module (thunderbolt_pci.ko).
2. Driver Registration:
- Make the core thunderbolt module standalone by converting
tb_domain_init/exit() to postcore_initcall() and module_exit().
- Register the PCIe driver in thunderbolt_pci via module_pci_driver().
3. Symbol Exports:
- Export necessary APIs required by pci and platform drivers.
Signed-off-by: Konrad Dybcio <konrad.dybcio@xxxxxxxxxxxxxxxx>
Signed-off-by: Chunfeng Yun <chunfeng.yun@xxxxxxxxxxxx>
---
v4:
* split .select_cm() change into new patch 08/14 in the series
* drop tb_bus_type, tb_switch_type, tb_domain_remove and nhi_shutdown exports
* add tb_domain_complete_rpm, tb_to_switch, tb_switch_has_children and tb_nhi_remove exports
v3:
Added 'default USB4' to CONFIG_USB4_PCI in Kconfig;
Exported tb_xdomain_remove symbol immediately after its definition;
v2:
cherry-pick the patch of Konrad to fix the issues of this series, and
also do some changes:
* Renamed CONFIG_USB4_PCIE and thunderbolt_pcie.ko to CONFIG_USB4_PCI
and thunderbolt_pci.ko.
* Updated exported NHI symbols to standard tb_ / tb_ring_ prefixes
(e.g., ring_msix → tb_ring_msix, nhi_msi → tb_nhi_isr).
* Eliminated exports for nhi_mailbox_* and nhi_interrupt_work by
keeping them internal/static (handled by preparatory patches in v2).
* Added missing trailing semicolon to module_pci_driver(nhi_driver);.
* Expanded the commit message;
* Fix cherry-pick conflict caused by .reset_interface();
---
drivers/thunderbolt/Kconfig | 12 +++++++++++-
drivers/thunderbolt/Makefile | 7 +++++--
drivers/thunderbolt/ctl.c | 6 +++++-
drivers/thunderbolt/domain.c | 13 +++++++++++--
drivers/thunderbolt/nhi.c | 4 ++++
drivers/thunderbolt/pci.c | 27 ++-------------------------
drivers/thunderbolt/switch.c | 10 ++++++++++
drivers/thunderbolt/tb.h | 2 --
drivers/thunderbolt/tunnel.c | 1 +
drivers/thunderbolt/xdomain.c | 5 +++++
10 files changed, 54 insertions(+), 33 deletions(-)
diff --git a/drivers/thunderbolt/Kconfig b/drivers/thunderbolt/Kconfig
index 294b3227a545..80960555a364 100644
--- a/drivers/thunderbolt/Kconfig
+++ b/drivers/thunderbolt/Kconfig
@@ -1,7 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
menuconfig USB4
tristate "Unified support for USB4 and Thunderbolt"
- depends on PCI
select APPLE_PROPERTIES if EFI_STUB && X86
select CRC32
select CRYPTO_LIB_SHA256
@@ -18,6 +17,17 @@ menuconfig USB4
if USB4
+config USB4_PCI
+ tristate "Support for PCIe-attached USB4 and Thunderbolt controllers"
+ depends on PCI
+ default USB4
+ help
+ Say Y here to add support for the USB4 and Thunderbolt host
+ routers attached over the PCIe bus, commonly found on x86 PCs.
+
+ To compile this driver a module, choose M here. The module will be
+ called thunderbolt_pci.
+
config USB4_CONFIGFS
def_tristate USB4
depends on CONFIGFS_FS && !(USB4=y && CONFIGFS_FS=m)
diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile
index beb054c3126b..a563bf2b0345 100644
--- a/drivers/thunderbolt/Makefile
+++ b/drivers/thunderbolt/Makefile
@@ -1,10 +1,13 @@
# SPDX-License-Identifier: GPL-2.0-only
ccflags-y := -I$(src)
obj-${CONFIG_USB4} := thunderbolt.o
-thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o pci.o path.o tunnel.o eeprom.o
-thunderbolt-objs += domain.o dma_port.o icm.o property.o xdomain.o lc.o tmu.o usb4.o
+thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o tunnel.o eeprom.o
+thunderbolt-objs += domain.o dma_port.o property.o xdomain.o lc.o tmu.o usb4.o
thunderbolt-objs += usb4_port.o nvm.o retimer.o quirks.o clx.o
+obj-${CONFIG_USB4_PCI} += thunderbolt_pci.o
+thunderbolt_pci-y := pci.o icm.o
+
thunderbolt-${CONFIG_ACPI} += acpi.o
thunderbolt-$(CONFIG_DEBUG_FS) += debugfs.o
thunderbolt-$(CONFIG_USB4_CONFIGFS) += configfs.o
diff --git a/drivers/thunderbolt/ctl.c b/drivers/thunderbolt/ctl.c
index 965988b18608..96717a0a94b1 100644
--- a/drivers/thunderbolt/ctl.c
+++ b/drivers/thunderbolt/ctl.c
@@ -9,7 +9,6 @@
#include <linux/crc32.h>
#include <linux/delay.h>
#include <linux/slab.h>
-#include <linux/pci.h>
#include <linux/dmapool.h>
#include <linux/workqueue.h>
@@ -96,6 +95,7 @@ struct tb_cfg_request *tb_cfg_request_alloc(void)
return req;
}
+EXPORT_SYMBOL_GPL(tb_cfg_request_alloc);
/**
* tb_cfg_request_get() - Increase refcount of a request
@@ -128,6 +128,7 @@ void tb_cfg_request_put(struct tb_cfg_request *req)
kref_put(&req->kref, tb_cfg_request_destroy);
mutex_unlock(&tb_cfg_request_lock);
}
+EXPORT_SYMBOL_GPL(tb_cfg_request_put);
static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
struct tb_cfg_request *req)
@@ -575,6 +576,7 @@ int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
tb_cfg_request_dequeue(req);
return ret;
}
+EXPORT_SYMBOL_GPL(tb_cfg_request);
/**
* tb_cfg_request_cancel() - Cancel a control request
@@ -634,6 +636,7 @@ struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
return req->result;
}
+EXPORT_SYMBOL_GPL(tb_cfg_request_sync);
/* public interface, alloc/start/stop/free */
@@ -1009,6 +1012,7 @@ struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
memcpy(buffer, &reply.data, 4 * length);
return res;
}
+EXPORT_SYMBOL_GPL(tb_cfg_read_raw);
/**
* tb_cfg_write_raw() - write from buffer into config space
diff --git a/drivers/thunderbolt/domain.c b/drivers/thunderbolt/domain.c
index 075e0f8af1e8..6c01768a2d54 100644
--- a/drivers/thunderbolt/domain.c
+++ b/drivers/thunderbolt/domain.c
@@ -421,6 +421,7 @@ struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize
return NULL;
}
+EXPORT_SYMBOL_GPL(tb_domain_alloc);
/**
* tb_domain_add() - Add domain to the system
@@ -880,6 +881,7 @@ int tb_domain_unregister_unplugged_xdomains(struct tb *tb)
return ctx.n;
}
+EXPORT_SYMBOL_GPL(tb_domain_unregister_unplugged_xdomains);
static int complete_rpm(struct device *dev, void *data)
{
@@ -904,8 +906,9 @@ void tb_domain_complete_rpm(struct tb *tb, struct tb_switch *sw)
complete_rpm(&sw->dev, tb);
bus_for_each_dev(&tb_bus_type, &sw->dev, tb, complete_rpm);
}
+EXPORT_SYMBOL_GPL(tb_domain_complete_rpm);
-int tb_domain_init(void)
+static int __init tb_domain_init(void)
{
int ret;
@@ -931,7 +934,7 @@ int tb_domain_init(void)
return ret;
}
-void tb_domain_exit(void)
+static void __exit tb_domain_exit(void)
{
bus_unregister(&tb_bus_type);
ida_destroy(&tb_domain_ida);
@@ -941,3 +944,9 @@ void tb_domain_exit(void)
tb_debugfs_exit();
tb_configfs_exit();
}
+
+postcore_initcall(tb_domain_init);
+module_exit(tb_domain_exit);
+
+MODULE_DESCRIPTION("Thunderbolt/USB4 core driver");
+MODULE_LICENSE("GPL");
diff --git a/drivers/thunderbolt/nhi.c b/drivers/thunderbolt/nhi.c
index 384493d80839..a2d5a5b13b9b 100644
--- a/drivers/thunderbolt/nhi.c
+++ b/drivers/thunderbolt/nhi.c
@@ -546,6 +546,7 @@ irqreturn_t tb_ring_msix(int irq, void *data)
return IRQ_HANDLED;
}
+EXPORT_SYMBOL_GPL(tb_ring_msix);
static int nhi_alloc_hop(struct tb_nhi *nhi, struct tb_ring *ring)
{
@@ -1341,6 +1342,7 @@ int tb_nhi_probe(struct tb_nhi *nhi)
nhi_shutdown(nhi);
return res;
}
+EXPORT_SYMBOL_GPL(tb_nhi_probe);
/**
* tb_nhi_remove() - Remove and release NHI instance and its domain
@@ -1370,6 +1372,7 @@ void tb_nhi_remove(struct tb_nhi *nhi, bool reset)
wait_for_completion(&nhi->domain_released);
nhi_shutdown(nhi);
}
+EXPORT_SYMBOL_GPL(tb_nhi_remove);
/*
* The tunneled pci bridges are siblings of us. Use resume_noirq to reenable
@@ -1392,3 +1395,4 @@ const struct dev_pm_ops tb_nhi_pm_ops = {
.runtime_suspend = nhi_runtime_suspend,
.runtime_resume = nhi_runtime_resume,
};
+EXPORT_SYMBOL_GPL(tb_nhi_pm_ops);
diff --git a/drivers/thunderbolt/pci.c b/drivers/thunderbolt/pci.c
index 379677362b40..7e68831df449 100644
--- a/drivers/thunderbolt/pci.c
+++ b/drivers/thunderbolt/pci.c
@@ -690,7 +690,7 @@ static struct pci_device_id nhi_ids[] = {
};
MODULE_DEVICE_TABLE(pci, nhi_ids);
-MODULE_DESCRIPTION("Thunderbolt/USB4 core driver");
+MODULE_DESCRIPTION("PCIe-attached Thunderbolt/USB4 host interface driver");
MODULE_LICENSE("GPL");
static struct pci_driver nhi_driver = {
@@ -701,27 +701,4 @@ static struct pci_driver nhi_driver = {
.shutdown = nhi_pci_shutdown,
.driver.pm = &tb_nhi_pm_ops,
};
-
-static int __init nhi_init(void)
-{
- int ret;
-
- ret = tb_domain_init();
- if (ret)
- return ret;
-
- ret = pci_register_driver(&nhi_driver);
- if (ret)
- tb_domain_exit();
-
- return ret;
-}
-
-static void __exit nhi_unload(void)
-{
- pci_unregister_driver(&nhi_driver);
- tb_domain_exit();
-}
-
-rootfs_initcall(nhi_init);
-module_exit(nhi_unload);
+module_pci_driver(nhi_driver);
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c
index 0388c20c574a..63ffc48738c9 100644
--- a/drivers/thunderbolt/switch.c
+++ b/drivers/thunderbolt/switch.c
@@ -2410,6 +2410,7 @@ struct tb_switch *tb_to_switch(const struct device *dev)
return NULL;
}
+EXPORT_SYMBOL_GPL(tb_to_switch);
static int tb_switch_get_generation(struct tb_switch *sw)
{
@@ -2585,6 +2586,7 @@ struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
return ERR_PTR(ret);
}
+EXPORT_SYMBOL_GPL(tb_switch_alloc);
/**
* tb_switch_alloc_safe_mode() - allocate a switch that is in safe mode
@@ -2624,6 +2626,7 @@ tb_switch_alloc_safe_mode(struct tb *tb, struct device *parent, u64 route)
return sw;
}
+EXPORT_SYMBOL_GPL(tb_switch_alloc_safe_mode);
/**
* tb_switch_configure() - Uploads configuration to the switch
@@ -3452,6 +3455,7 @@ int tb_switch_add(struct tb_switch *sw)
return ret;
}
+EXPORT_SYMBOL_GPL(tb_switch_add);
/**
* tb_switch_remove() - Remove and release a switch
@@ -3497,6 +3501,7 @@ void tb_switch_remove(struct tb_switch *sw)
dev_info(&sw->dev, "device disconnected\n");
device_unregister(&sw->dev);
}
+EXPORT_SYMBOL_GPL(tb_switch_remove);
/**
* tb_sw_set_unplugged() - set is_unplugged on switch and downstream switches
@@ -3842,6 +3847,7 @@ struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link, u8 depth)
return NULL;
}
+EXPORT_SYMBOL_GPL(tb_switch_find_by_link_depth);
/**
* tb_switch_find_by_uuid() - Find switch by UUID
@@ -3868,6 +3874,7 @@ struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid)
return NULL;
}
+EXPORT_SYMBOL_GPL(tb_switch_find_by_uuid);
/**
* tb_switch_find_by_route() - Find switch by route string
@@ -3897,6 +3904,7 @@ struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route)
return NULL;
}
+EXPORT_SYMBOL_GPL(tb_switch_find_by_route);
/**
* tb_switch_find_port() - return the first port of @type on @sw or NULL
@@ -3917,6 +3925,7 @@ struct tb_port *tb_switch_find_port(struct tb_switch *sw,
return NULL;
}
+EXPORT_SYMBOL_GPL(tb_switch_find_port);
static int check_for_device(struct device *dev, void *data)
{
@@ -3936,6 +3945,7 @@ bool tb_switch_has_children(struct tb_switch *sw)
ret = device_for_each_child(&sw->dev, NULL, check_for_device);
return ret > 0;
}
+EXPORT_SYMBOL_GPL(tb_switch_has_children);
/*
* Can be used for read/write a specified PCIe bridge for any Thunderbolt 3
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index d6c188922ca1..5516e0340610 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -768,8 +768,6 @@ extern const struct device_type tb_retimer_type;
extern const struct device_type tb_switch_type;
extern const struct device_type usb4_port_device_type;
-int tb_domain_init(void);
-void tb_domain_exit(void);
int tb_xdomain_init(void);
void tb_xdomain_exit(void);
diff --git a/drivers/thunderbolt/tunnel.c b/drivers/thunderbolt/tunnel.c
index 2b911372001c..c92f181f181d 100644
--- a/drivers/thunderbolt/tunnel.c
+++ b/drivers/thunderbolt/tunnel.c
@@ -274,6 +274,7 @@ void tb_tunnel_event(struct tb *tb, enum tb_tunnel_event event,
kfree(envp[1]);
kfree(envp[0]);
}
+EXPORT_SYMBOL_GPL(tb_tunnel_event);
static inline void tb_tunnel_set_active(struct tb_tunnel *tunnel, bool active)
{
diff --git a/drivers/thunderbolt/xdomain.c b/drivers/thunderbolt/xdomain.c
index b187a60663f5..8ebe6cc18f60 100644
--- a/drivers/thunderbolt/xdomain.c
+++ b/drivers/thunderbolt/xdomain.c
@@ -86,6 +86,7 @@ bool tb_is_xdomain_enabled(void)
{
return tb_xdomain_enabled && tb_acpi_is_xdomain_allowed();
}
+EXPORT_SYMBOL_GPL(tb_is_xdomain_enabled);
static bool tb_xdomain_match(const struct tb_cfg_request *req,
const struct ctl_pkg *pkg)
@@ -2196,6 +2197,7 @@ struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
return NULL;
}
+EXPORT_SYMBOL_GPL(tb_xdomain_alloc);
/**
* tb_xdomain_add() - Add XDomain to the bus
@@ -2211,6 +2213,7 @@ void tb_xdomain_add(struct tb_xdomain *xd)
/* Start exchanging properties with the other host */
start_handshake(xd);
}
+EXPORT_SYMBOL_GPL(tb_xdomain_add);
static int unregister_service(struct device *dev, void *data)
{
@@ -2252,6 +2255,7 @@ void tb_xdomain_remove(struct tb_xdomain *xd)
put_device(&xd->dev);
}
}
+EXPORT_SYMBOL_GPL(tb_xdomain_remove);
/**
* tb_xdomain_unregister() - Unregister XDomain
@@ -2593,6 +2597,7 @@ struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
xd = switch_find_xdomain(tb->root_switch, &lookup);
return tb_xdomain_get(xd);
}
+EXPORT_SYMBOL_GPL(tb_xdomain_find_by_link_depth);
/**
* tb_xdomain_find_by_route() - Find an XDomain by route string
--
2.45.2