This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit fe156a40e3012407768d24263676348610c7d4b6
Author: Tiago Medicci Serrano <[email protected]>
AuthorDate: Mon Oct 9 17:15:18 2023 -0300

    wireless/bluetooth: Add option to set the HCI TX thread affinity
    
    By enabling the config `CONFIG_BLUETOOTH_TXCMD_PINNED_TO_CORE` and
    setting the value of `CONFIG_BLUETOOTH_TXCMD_CORE`, it's possible
    to pin the HCI TX thread to a specific core on a SMP-enabled setup.
    This is necessary for devices that require that the function that
    sends data (`bt_send`) to be called from a specific core.
---
 wireless/bluetooth/Kconfig      | 19 +++++++++++++++++++
 wireless/bluetooth/bt_hcicore.c | 27 +++++++++++++++++++++++++--
 2 files changed, 44 insertions(+), 2 deletions(-)

diff --git a/wireless/bluetooth/Kconfig b/wireless/bluetooth/Kconfig
index d5c60c2dc5..fbe17788af 100644
--- a/wireless/bluetooth/Kconfig
+++ b/wireless/bluetooth/Kconfig
@@ -193,6 +193,25 @@ config BLUETOOTH_TXCONN_NMSGS
        int "Tx connection thread mqueue size"
        default 16
 
+config BLUETOOTH_TXCMD_PINNED_TO_CORE
+       bool "Pin Tx command thread to specific core"
+       depends on SMP
+       default n
+       ---help---
+               This option enables us to set the affinity of the Tx command 
thread
+               to make it run on a specific core.
+
+if BLUETOOTH_TXCMD_PINNED_TO_CORE
+
+config BLUETOOTH_TXCMD_CORE
+       int "Tx command thread CPU core"
+       default 1
+       range 1 SMP_NCPUS
+       ---help---
+               Select the core to pin the Tx command thread.
+
+endif # BLUETOOTH_TXCMD_PINNED_TO_CORE
+
 endmenu # Kernel Thread Configuration
 
 config BLUETOOTH_SMP_SELFTEST
diff --git a/wireless/bluetooth/bt_hcicore.c b/wireless/bluetooth/bt_hcicore.c
index 1d95f0ea1b..d7e192247e 100644
--- a/wireless/bluetooth/bt_hcicore.c
+++ b/wireless/bluetooth/bt_hcicore.c
@@ -1499,6 +1499,10 @@ static void cmd_queue_deinit(void)
 static void cmd_queue_init(void)
 {
   int ret;
+#ifdef CONFIG_BLUETOOTH_TXCMD_PINNED_TO_CORE
+  cpu_set_t cpuset;
+#endif
+  int pid;
 
   /* When there is a command to be sent to the Bluetooth driver, it queued on
    * the Tx queue and received by logic on the Tx kernel thread.
@@ -1512,10 +1516,29 @@ static void cmd_queue_init(void)
 
   g_btdev.ncmd = 1;
   g_btdev.tx_status = OK;
-  ret = kthread_create("BT HCI Tx", CONFIG_BLUETOOTH_TXCMD_PRIORITY,
+
+#ifdef CONFIG_BLUETOOTH_TXCMD_PINNED_TO_CORE
+  sched_lock();
+#endif
+
+  pid = kthread_create("BT HCI Tx", CONFIG_BLUETOOTH_TXCMD_PRIORITY,
                        CONFIG_BLUETOOTH_TXCMD_STACKSIZE,
                        hci_tx_kthread, NULL);
-  DEBUGASSERT(ret > 0);
+  DEBUGASSERT(pid > 0);
+
+#ifdef CONFIG_BLUETOOTH_TXCMD_PINNED_TO_CORE
+  CPU_ZERO(&cpuset);
+  CPU_SET((CONFIG_BLUETOOTH_TXCMD_CORE - 1), &cpuset);
+  ret = nxsched_set_affinity(pid, sizeof(cpuset), &cpuset);
+  if (ret)
+    {
+      wlerr("Failed to set affinity error=%d\n", ret);
+      DEBUGPANIC();
+    }
+
+  sched_unlock();
+#endif
+
   UNUSED(ret);
 }
 

Reply via email to