@@ -49,6 +49,9 @@ static LIST_HEAD(wait_for_suppliers);
4949static DEFINE_MUTEX (wfs_lock );
5050static LIST_HEAD (deferred_sync );
5151static unsigned int defer_sync_state_count = 1 ;
52+ static unsigned int defer_fw_devlink_count ;
53+ static DEFINE_MUTEX (defer_fw_devlink_lock );
54+ static bool fw_devlink_is_permissive (void );
5255
5356#ifdef CONFIG_SRCU
5457static DEFINE_MUTEX (device_links_lock );
@@ -529,7 +532,7 @@ static void device_link_add_missing_supplier_links(void)
529532 int ret = fwnode_call_int_op (dev -> fwnode , add_links , dev );
530533 if (!ret )
531534 list_del_init (& dev -> links .needs_suppliers );
532- else if (ret != - ENODEV )
535+ else if (ret != - ENODEV || fw_devlink_is_permissive () )
533536 dev -> links .need_for_probe = false;
534537 }
535538 mutex_unlock (& wfs_lock );
@@ -643,9 +646,17 @@ static void device_links_missing_supplier(struct device *dev)
643646{
644647 struct device_link * link ;
645648
646- list_for_each_entry (link , & dev -> links .suppliers , c_node )
647- if (link -> status == DL_STATE_CONSUMER_PROBE )
649+ list_for_each_entry (link , & dev -> links .suppliers , c_node ) {
650+ if (link -> status != DL_STATE_CONSUMER_PROBE )
651+ continue ;
652+
653+ if (link -> supplier -> links .status == DL_DEV_DRIVER_BOUND ) {
648654 WRITE_ONCE (link -> status , DL_STATE_AVAILABLE );
655+ } else {
656+ WARN_ON (!(link -> flags & DL_FLAG_SYNC_STATE_ONLY ));
657+ WRITE_ONCE (link -> status , DL_STATE_DORMANT );
658+ }
659+ }
649660}
650661
651662/**
@@ -684,11 +695,11 @@ int device_links_check_suppliers(struct device *dev)
684695 device_links_write_lock ();
685696
686697 list_for_each_entry (link , & dev -> links .suppliers , c_node ) {
687- if (!(link -> flags & DL_FLAG_MANAGED ) ||
688- link -> flags & DL_FLAG_SYNC_STATE_ONLY )
698+ if (!(link -> flags & DL_FLAG_MANAGED ))
689699 continue ;
690700
691- if (link -> status != DL_STATE_AVAILABLE ) {
701+ if (link -> status != DL_STATE_AVAILABLE &&
702+ !(link -> flags & DL_FLAG_SYNC_STATE_ONLY )) {
692703 device_links_missing_supplier (dev );
693704 ret = - EPROBE_DEFER ;
694705 break ;
@@ -949,11 +960,21 @@ static void __device_links_no_driver(struct device *dev)
949960 if (!(link -> flags & DL_FLAG_MANAGED ))
950961 continue ;
951962
952- if (link -> flags & DL_FLAG_AUTOREMOVE_CONSUMER )
963+ if (link -> flags & DL_FLAG_AUTOREMOVE_CONSUMER ) {
953964 device_link_drop_managed (link );
954- else if (link -> status == DL_STATE_CONSUMER_PROBE ||
955- link -> status == DL_STATE_ACTIVE )
965+ continue ;
966+ }
967+
968+ if (link -> status != DL_STATE_CONSUMER_PROBE &&
969+ link -> status != DL_STATE_ACTIVE )
970+ continue ;
971+
972+ if (link -> supplier -> links .status == DL_DEV_DRIVER_BOUND ) {
956973 WRITE_ONCE (link -> status , DL_STATE_AVAILABLE );
974+ } else {
975+ WARN_ON (!(link -> flags & DL_FLAG_SYNC_STATE_ONLY ));
976+ WRITE_ONCE (link -> status , DL_STATE_DORMANT );
977+ }
957978 }
958979
959980 dev -> links .status = DL_DEV_NO_DRIVER ;
@@ -1162,6 +1183,150 @@ static void device_links_purge(struct device *dev)
11621183 device_links_write_unlock ();
11631184}
11641185
1186+ static u32 fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY ;
1187+ static int __init fw_devlink_setup (char * arg )
1188+ {
1189+ if (!arg )
1190+ return - EINVAL ;
1191+
1192+ if (strcmp (arg , "off" ) == 0 ) {
1193+ fw_devlink_flags = 0 ;
1194+ } else if (strcmp (arg , "permissive" ) == 0 ) {
1195+ fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY ;
1196+ } else if (strcmp (arg , "on" ) == 0 ) {
1197+ fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER ;
1198+ } else if (strcmp (arg , "rpm" ) == 0 ) {
1199+ fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
1200+ DL_FLAG_PM_RUNTIME ;
1201+ }
1202+ return 0 ;
1203+ }
1204+ early_param ("fw_devlink" , fw_devlink_setup );
1205+
1206+ u32 fw_devlink_get_flags (void )
1207+ {
1208+ return fw_devlink_flags ;
1209+ }
1210+
1211+ static bool fw_devlink_is_permissive (void )
1212+ {
1213+ return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY ;
1214+ }
1215+
1216+ static void fw_devlink_link_device (struct device * dev )
1217+ {
1218+ int fw_ret ;
1219+
1220+ if (!fw_devlink_flags )
1221+ return ;
1222+
1223+ mutex_lock (& defer_fw_devlink_lock );
1224+ if (!defer_fw_devlink_count )
1225+ device_link_add_missing_supplier_links ();
1226+
1227+ /*
1228+ * The device's fwnode not having add_links() doesn't affect if other
1229+ * consumers can find this device as a supplier. So, this check is
1230+ * intentionally placed after device_link_add_missing_supplier_links().
1231+ */
1232+ if (!fwnode_has_op (dev -> fwnode , add_links ))
1233+ goto out ;
1234+
1235+ /*
1236+ * If fw_devlink is being deferred, assume all devices have mandatory
1237+ * suppliers they need to link to later. Then, when the fw_devlink is
1238+ * resumed, all these devices will get a chance to try and link to any
1239+ * suppliers they have.
1240+ */
1241+ if (!defer_fw_devlink_count ) {
1242+ fw_ret = fwnode_call_int_op (dev -> fwnode , add_links , dev );
1243+ if (fw_ret == - ENODEV && fw_devlink_is_permissive ())
1244+ fw_ret = - EAGAIN ;
1245+ } else {
1246+ fw_ret = - ENODEV ;
1247+ }
1248+
1249+ if (fw_ret == - ENODEV )
1250+ device_link_wait_for_mandatory_supplier (dev );
1251+ else if (fw_ret )
1252+ device_link_wait_for_optional_supplier (dev );
1253+
1254+ out :
1255+ mutex_unlock (& defer_fw_devlink_lock );
1256+ }
1257+
1258+ /**
1259+ * fw_devlink_pause - Pause parsing of fwnode to create device links
1260+ *
1261+ * Calling this function defers any fwnode parsing to create device links until
1262+ * fw_devlink_resume() is called. Both these functions are ref counted and the
1263+ * caller needs to match the calls.
1264+ *
1265+ * While fw_devlink is paused:
1266+ * - Any device that is added won't have its fwnode parsed to create device
1267+ * links.
1268+ * - The probe of the device will also be deferred during this period.
1269+ * - Any devices that were already added, but waiting for suppliers won't be
1270+ * able to link to newly added devices.
1271+ *
1272+ * Once fw_devlink_resume():
1273+ * - All the fwnodes that was not parsed will be parsed.
1274+ * - All the devices that were deferred probing will be reattempted if they
1275+ * aren't waiting for any more suppliers.
1276+ *
1277+ * This pair of functions, is mainly meant to optimize the parsing of fwnodes
1278+ * when a lot of devices that need to link to each other are added in a short
1279+ * interval of time. For example, adding all the top level devices in a system.
1280+ *
1281+ * For example, if N devices are added and:
1282+ * - All the consumers are added before their suppliers
1283+ * - All the suppliers of the N devices are part of the N devices
1284+ *
1285+ * Then:
1286+ *
1287+ * - With the use of fw_devlink_pause() and fw_devlink_resume(), each device
1288+ * will only need one parsing of its fwnode because it is guaranteed to find
1289+ * all the supplier devices already registered and ready to link to. It won't
1290+ * have to do another pass later to find one or more suppliers it couldn't
1291+ * find in the first parse of the fwnode. So, we'll only need O(N) fwnode
1292+ * parses.
1293+ *
1294+ * - Without the use of fw_devlink_pause() and fw_devlink_resume(), we would
1295+ * end up doing O(N^2) parses of fwnodes because every device that's added is
1296+ * guaranteed to trigger a parse of the fwnode of every device added before
1297+ * it. This O(N^2) parse is made worse by the fact that when a fwnode of a
1298+ * device is parsed, all it descendant devices might need to have their
1299+ * fwnodes parsed too (even if the devices themselves aren't added).
1300+ */
1301+ void fw_devlink_pause (void )
1302+ {
1303+ mutex_lock (& defer_fw_devlink_lock );
1304+ defer_fw_devlink_count ++ ;
1305+ mutex_unlock (& defer_fw_devlink_lock );
1306+ }
1307+
1308+ /** fw_devlink_resume - Resume parsing of fwnode to create device links
1309+ *
1310+ * This function is used in conjunction with fw_devlink_pause() and is ref
1311+ * counted. See documentation for fw_devlink_pause() for more details.
1312+ */
1313+ void fw_devlink_resume (void )
1314+ {
1315+ mutex_lock (& defer_fw_devlink_lock );
1316+ if (!defer_fw_devlink_count ) {
1317+ WARN (true, "Unmatched fw_devlink pause/resume!" );
1318+ goto out ;
1319+ }
1320+
1321+ defer_fw_devlink_count -- ;
1322+ if (defer_fw_devlink_count )
1323+ goto out ;
1324+
1325+ device_link_add_missing_supplier_links ();
1326+ driver_deferred_probe_force_trigger ();
1327+ out :
1328+ mutex_unlock (& defer_fw_devlink_lock );
1329+ }
11651330/* Device links support end. */
11661331
11671332int (* platform_notify )(struct device * dev ) = NULL ;
@@ -2364,36 +2529,6 @@ static int device_private_init(struct device *dev)
23642529 return 0 ;
23652530}
23662531
2367- static u32 fw_devlink_flags ;
2368- static int __init fw_devlink_setup (char * arg )
2369- {
2370- if (!arg )
2371- return - EINVAL ;
2372-
2373- if (strcmp (arg , "off" ) == 0 ) {
2374- fw_devlink_flags = 0 ;
2375- } else if (strcmp (arg , "permissive" ) == 0 ) {
2376- fw_devlink_flags = DL_FLAG_SYNC_STATE_ONLY ;
2377- } else if (strcmp (arg , "on" ) == 0 ) {
2378- fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER ;
2379- } else if (strcmp (arg , "rpm" ) == 0 ) {
2380- fw_devlink_flags = DL_FLAG_AUTOPROBE_CONSUMER |
2381- DL_FLAG_PM_RUNTIME ;
2382- }
2383- return 0 ;
2384- }
2385- early_param ("fw_devlink" , fw_devlink_setup );
2386-
2387- u32 fw_devlink_get_flags (void )
2388- {
2389- return fw_devlink_flags ;
2390- }
2391-
2392- static bool fw_devlink_is_permissive (void )
2393- {
2394- return fw_devlink_flags == DL_FLAG_SYNC_STATE_ONLY ;
2395- }
2396-
23972532/**
23982533 * device_add - add device to device hierarchy.
23992534 * @dev: device.
@@ -2426,9 +2561,8 @@ int device_add(struct device *dev)
24262561 struct device * parent ;
24272562 struct kobject * kobj ;
24282563 struct class_interface * class_intf ;
2429- int error = - EINVAL , fw_ret ;
2564+ int error = - EINVAL ;
24302565 struct kobject * glue_dir = NULL ;
2431- bool is_fwnode_dev = false;
24322566
24332567 dev = get_device (dev );
24342568 if (!dev )
@@ -2526,11 +2660,6 @@ int device_add(struct device *dev)
25262660
25272661 kobject_uevent (& dev -> kobj , KOBJ_ADD );
25282662
2529- if (dev -> fwnode && !dev -> fwnode -> dev ) {
2530- dev -> fwnode -> dev = dev ;
2531- is_fwnode_dev = true;
2532- }
2533-
25342663 /*
25352664 * Check if any of the other devices (consumers) have been waiting for
25362665 * this device (supplier) to be added so that they can create a device
@@ -2539,19 +2668,13 @@ int device_add(struct device *dev)
25392668 * This needs to happen after device_pm_add() because device_link_add()
25402669 * requires the supplier be registered before it's called.
25412670 *
2542- * But this also needs to happe before bus_probe_device() to make sure
2671+ * But this also needs to happen before bus_probe_device() to make sure
25432672 * waiting consumers can link to it before the driver is bound to the
25442673 * device and the driver sync_state callback is called for this device.
25452674 */
2546- device_link_add_missing_supplier_links ();
2547-
2548- if (fw_devlink_flags && is_fwnode_dev &&
2549- fwnode_has_op (dev -> fwnode , add_links )) {
2550- fw_ret = fwnode_call_int_op (dev -> fwnode , add_links , dev );
2551- if (fw_ret == - ENODEV && !fw_devlink_is_permissive ())
2552- device_link_wait_for_mandatory_supplier (dev );
2553- else if (fw_ret )
2554- device_link_wait_for_optional_supplier (dev );
2675+ if (dev -> fwnode && !dev -> fwnode -> dev ) {
2676+ dev -> fwnode -> dev = dev ;
2677+ fw_devlink_link_device (dev );
25552678 }
25562679
25572680 bus_probe_device (dev );
0 commit comments