Use the uthread framework to initialize and scan USB buses in parallel
for better performance. Tested on two platforms:

1. arm64 QEMU on a somewhat contrived example (4 USB buses, each with
one audio device, one keyboard, one mouse and one tablet)

 $ make qemu_arm64_defconfig
 $ make -j$(nproc) CROSS_COMPILE="ccache aarch64-linux-gnu-"
 $ qemu-system-aarch64 -M virt -nographic -cpu max -bios u-boot.bin \
     $(for i in {1..4}; do echo -device qemu-xhci,id=xhci$i \
         -device\ usb-{audio,kbd,mouse,tablet},bus=xhci$i.0; \
     done)

2. i.MX93 EVK (imx93_11x11_evk_defconfig) with two USB hubs, each with
one webcam and one ethernet adapter, resulting in the following device
tree:

 USB device tree:
   1  Hub (480 Mb/s, 0mA)
   |  u-boot EHCI Host Controller
   |
   +-2  Hub (480 Mb/s, 100mA)
     |  GenesysLogic USB2.1 Hub
     |
     +-3  Vendor specific (480 Mb/s, 350mA)
     |    Realtek USB 10/100/1000 LAN 001000001
     |
     +-4   (480 Mb/s, 500mA)
           HD Pro Webcam C920 8F7CD51F

   1  Hub (480 Mb/s, 0mA)
   |  u-boot EHCI Host Controller
   |
   +-2  Hub (480 Mb/s, 100mA)
     |   USB 2.0 Hub
     |
     +-3  Vendor specific (480 Mb/s, 200mA)
     |    Realtek USB 10/100/1000 LAN 000001
     |
     +-4   (480 Mb/s, 500mA)
          Generic OnLan-CS30 201801010008

Note that i.MX was tested on top of the downstream repository [1] since
USB doesn't work in the upstream master branch.

[1] https://github.com/nxp-imx/uboot-imx/tree/lf-6.6.52-2.2.0
    commit 6c4545203d12 ("LF-13928 update key for capsule")

The time spent in usb_init() ("usb start" command) is reported on
the console. Here are the results:

        | CONFIG_UTHREAD=n | CONFIG_UTHREAD=y
--------+------------------+-----------------
QEMU    |          5628 ms |          2212 ms
i.MX93  |          4591 ms |          2441 ms

Signed-off-by: Jerome Forissier <jerome.foriss...@linaro.org>
---
 drivers/usb/host/usb-uclass.c | 89 ++++++++++++++++++++++++++++-------
 test/boot/bootdev.c           | 14 +++---
 test/boot/bootflow.c          |  3 +-
 3 files changed, 82 insertions(+), 24 deletions(-)

diff --git a/drivers/usb/host/usb-uclass.c b/drivers/usb/host/usb-uclass.c
index cc803241461..eedf4e1c3fb 100644
--- a/drivers/usb/host/usb-uclass.c
+++ b/drivers/usb/host/usb-uclass.c
@@ -9,6 +9,7 @@
 #define LOG_CATEGORY UCLASS_USB
 
 #include <bootdev.h>
+#include <uthread.h>
 #include <dm.h>
 #include <errno.h>
 #include <log.h>
@@ -17,6 +18,7 @@
 #include <dm/device-internal.h>
 #include <dm/lists.h>
 #include <dm/uclass-internal.h>
+#include <time.h>
 
 static bool asynch_allowed;
 
@@ -221,25 +223,18 @@ int usb_stop(void)
        return err;
 }
 
-static void usb_scan_bus(struct udevice *bus, bool recurse)
+static void _usb_scan_bus(void *arg)
 {
+       struct udevice *bus = (struct udevice *)arg;
        struct usb_bus_priv *priv;
        struct udevice *dev;
        int ret;
 
        priv = dev_get_uclass_priv(bus);
 
-       assert(recurse);        /* TODO: Support non-recusive */
-
-       printf("scanning bus %s for devices... ", bus->name);
-       debug("\n");
        ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
        if (ret)
-               printf("failed, error %d\n", ret);
-       else if (priv->next_addr == 0)
-               printf("No USB Device found\n");
-       else
-               printf("%d USB Device(s) found\n", priv->next_addr);
+               printf("Scanning bus %s failed, error %d\n", bus->name, ret);
 }
 
 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
@@ -289,12 +284,12 @@ static int usb_probe_companion(struct udevice *bus)
 
 static int controllers_initialized;
 
-static void usb_init_bus(struct udevice *bus)
+static void _usb_init_bus(void *arg)
 {
+       struct udevice *bus = (struct udevice *)arg;
        int ret;
 
        /* init low_level USB */
-       printf("Bus %s: ", bus->name);
 
        /*
         * For Sandbox, we need scan the device tree each time when we
@@ -309,20 +304,21 @@ static void usb_init_bus(struct udevice *bus)
            IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) {
                ret = dm_scan_fdt_dev(bus);
                if (ret) {
-                       printf("USB device scan from fdt failed (%d)", ret);
+                       printf("Bus %s: USB device scan from fdt failed (%d)\n",
+                              bus->name, ret);
                        return;
                }
        }
 
        ret = device_probe(bus);
        if (ret == -ENODEV) {   /* No such device. */
-               puts("Port not available.\n");
+               printf("Bus %s: Port not available.\n", bus->name);
                controllers_initialized++;
                return;
        }
 
        if (ret) {              /* Other error. */
-               printf("probe failed, error %d\n", ret);
+               printf("Bus %s: probe failed, error %d\n", bus->name, ret);
                return;
        }
 
@@ -334,8 +330,57 @@ static void usb_init_bus(struct udevice *bus)
        usb_started = true;
 }
 
+#if CONFIG_IS_ENABLED(UTHREAD)
+extern int udelay_yield;
+#endif
+static int nthr;
+
+static void usb_init_bus(struct udevice *bus)
+{
+       if (!uthread_create(_usb_init_bus, (void *)bus, 0))
+               nthr++;
+}
+
+static void usb_scan_bus(struct udevice *bus, bool recurse)
+{
+       if (!uthread_create(_usb_scan_bus, (void *)bus, 0))
+               nthr++;
+}
+
+static void usb_report_devices(struct uclass *uc)
+{
+       struct usb_bus_priv *priv;
+       struct udevice *bus;
+
+       uclass_foreach_dev(bus, uc) {
+               if (!device_active(bus))
+                       continue;
+               priv = dev_get_uclass_priv(bus);
+               printf("Bus %s: ", bus->name);
+               if (priv->next_addr == 0)
+                       printf("No USB Device found\n");
+               else
+                       printf("%d USB Device(s) found\n", priv->next_addr);
+       }
+}
+
+static void run_threads(void)
+{
+#if CONFIG_IS_ENABLED(UTHREAD)
+       if (!nthr)
+               return;
+       udelay_yield = 0xCAFEDECA;
+       while (uthread_schedule())
+               /* Nothing */;
+       udelay_yield = 0;
+       uthread_free_all();
+       nthr = 0;
+#endif
+}
+
 int usb_init(void)
 {
+       unsigned long t0 = timer_get_us();
        struct usb_uclass_priv *uc_priv;
        struct usb_bus_priv *priv;
        struct udevice *bus;
@@ -354,6 +399,9 @@ int usb_init(void)
                usb_init_bus(bus);
        }
 
+       if (CONFIG_IS_ENABLED(UTHREAD))
+               run_threads();
+
        /*
         * lowlevel init done, now scan the bus for devices i.e. search HUBs
         * and configure them, first scan primary controllers.
@@ -367,6 +415,9 @@ int usb_init(void)
                        usb_scan_bus(bus, true);
        }
 
+       if (CONFIG_IS_ENABLED(UTHREAD))
+               run_threads();
+
        /*
         * Now that the primary controllers have been scanned and have handed
         * over any devices they do not understand to their companions, scan
@@ -383,7 +434,10 @@ int usb_init(void)
                }
        }
 
-       debug("scan end\n");
+       if (CONFIG_IS_ENABLED(UTHREAD))
+               run_threads();
+
+       usb_report_devices(uc);
 
        /* Remove any devices that were not found on this scan */
        remove_inactive_children(uc, bus);
@@ -397,6 +451,9 @@ int usb_init(void)
        if (controllers_initialized == 0)
                printf("No USB controllers found\n");
 
+       debug("USB initialized in %ld ms\n",
+              (timer_get_us() - t0) / 1000);
+
        return usb_started ? 0 : -ENOENT;
 }
 
diff --git a/test/boot/bootdev.c b/test/boot/bootdev.c
index 8c44afd9297..956320f1daa 100644
--- a/test/boot/bootdev.c
+++ b/test/boot/bootdev.c
@@ -393,8 +393,8 @@ static int bootdev_test_hunter(struct unit_test_state *uts)
        ut_assert_console_end();
 
        ut_assertok(bootdev_hunt("usb1", false));
-       ut_assert_nextline(
-               "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) 
found");
+       ut_assert_skip_to_line(
+               "Bus usb@1: 5 USB Device(s) found");
        ut_assert_console_end();
 
        /* USB is 7th in the list, so bit 8 */
@@ -449,8 +449,8 @@ static int bootdev_test_cmd_hunt(struct unit_test_state 
*uts)
        ut_assert_nextline("scanning bus for devices...");
        ut_assert_skip_to_line("Hunting with: spi_flash");
        ut_assert_nextline("Hunting with: usb");
-       ut_assert_nextline(
-               "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) 
found");
+       ut_assert_skip_to_line(
+               "Bus usb@1: 5 USB Device(s) found");
        ut_assert_nextline("Hunting with: virtio");
        ut_assert_console_end();
 
@@ -551,8 +551,8 @@ static int bootdev_test_hunt_prio(struct unit_test_state 
*uts)
        ut_assertok(bootdev_hunt_prio(BOOTDEVP_5_SCAN_SLOW, true));
        ut_assert_nextline("Hunting with: ide");
        ut_assert_nextline("Hunting with: usb");
-       ut_assert_nextline(
-               "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) 
found");
+       ut_assert_skip_to_line(
+               "Bus usb@1: 5 USB Device(s) found");
        ut_assert_console_end();
 
        return 0;
@@ -604,7 +604,7 @@ static int bootdev_test_hunt_label(struct unit_test_state 
*uts)
        ut_assertnonnull(dev);
        ut_asserteq_str("usb_mass_storage.lun0.bootdev", dev->name);
        ut_asserteq(BOOTFLOW_METHF_SINGLE_UCLASS, mflags);
-       ut_assert_nextlinen("Bus usb@1: scanning bus usb@1");
+       ut_assert_nextline("Bus usb@1: 5 USB Device(s) found");
        ut_assert_console_end();
 
        return 0;
diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c
index a8735c1c23d..67ee1777d01 100644
--- a/test/boot/bootflow.c
+++ b/test/boot/bootflow.c
@@ -1289,8 +1289,9 @@ static int bootflow_efi(struct unit_test_state *uts)
        bootstd_reset_usb();
 
        ut_assertok(run_command("bootflow scan", 0));
+
        ut_assert_skip_to_line(
-               "Bus usb@1: scanning bus usb@1 for devices... 5 USB Device(s) 
found");
+               "Bus usb@1: 5 USB Device(s) found");
 
        ut_assertok(run_command("bootflow list", 0));
 
-- 
2.43.0

Reply via email to