77#define pr_fmt (fmt ) "PM: " fmt
88
99#include <linux/delay.h>
10+ #include <linux/fwnode.h>
1011#include <linux/idr.h>
1112#include <linux/kernel.h>
1213#include <linux/io.h>
@@ -176,6 +177,7 @@ static const struct genpd_lock_ops genpd_raw_spin_ops = {
176177#define genpd_is_rpm_always_on (genpd ) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
177178#define genpd_is_opp_table_fw (genpd ) (genpd->flags & GENPD_FLAG_OPP_TABLE_FW)
178179#define genpd_is_dev_name_fw (genpd ) (genpd->flags & GENPD_FLAG_DEV_NAME_FW)
180+ #define genpd_is_defer_off (genpd ) (genpd->flags & GENPD_FLAG_DEFER_OFF)
179181
180182static inline bool irq_safe_dev_in_sleep_domain (struct device * dev ,
181183 const struct generic_pm_domain * genpd )
@@ -896,6 +898,27 @@ static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
896898 queue_work (pm_wq , & genpd -> power_off_work );
897899}
898900
901+ /**
902+ * genpd_must_defer - Check whether the genpd cannot be safely powered off.
903+ * @genpd: PM domain about to be powered down.
904+ * @one_dev_probing: True if we are being called from RPM callbacks on a device that
905+ * is probing, to allow poweroff if that device is the sole remaining consumer probing.
906+ *
907+ * Returns true if the @genpd has the GENPD_FLAG_DEFER_OFF flag and there
908+ * are any consumer devices which either do not exist yet (only represented
909+ * by fwlinks) or whose drivers have not probed yet.
910+ */
911+ static bool genpd_must_defer (struct generic_pm_domain * genpd , bool one_dev_probing )
912+ {
913+ if (genpd_is_defer_off (genpd ) && genpd -> has_provider ) {
914+ int absent = fw_devlink_count_absent_consumers (genpd -> provider );
915+
916+ if (absent > (one_dev_probing ? 1 : 0 ))
917+ return true;
918+ }
919+ return false;
920+ }
921+
899922/**
900923 * genpd_power_off - Remove power from a given PM domain.
901924 * @genpd: PM domain to power down.
@@ -909,7 +932,7 @@ static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
909932 * have been powered down, remove power from @genpd.
910933 */
911934static void genpd_power_off (struct generic_pm_domain * genpd , bool one_dev_on ,
912- unsigned int depth )
935+ bool one_dev_probing , unsigned int depth )
913936{
914937 struct pm_domain_data * pdd ;
915938 struct gpd_link * link ;
@@ -956,6 +979,14 @@ static void genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
956979 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on ))
957980 return ;
958981
982+ /*
983+ * Do not allow PM domain to be powered off if it is marked
984+ * as GENPD_FLAG_DEFER_OFF and there are consumer devices
985+ * which have not probed yet.
986+ */
987+ if (genpd_must_defer (genpd , one_dev_probing ))
988+ return ;
989+
959990 if (genpd -> gov && genpd -> gov -> power_down_ok ) {
960991 if (!genpd -> gov -> power_down_ok (& genpd -> domain ))
961992 return ;
@@ -981,7 +1012,7 @@ static void genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
9811012 list_for_each_entry (link , & genpd -> child_links , child_node ) {
9821013 genpd_sd_counter_dec (link -> parent );
9831014 genpd_lock_nested (link -> parent , depth + 1 );
984- genpd_power_off (link -> parent , false, depth + 1 );
1015+ genpd_power_off (link -> parent , false, false, depth + 1 );
9851016 genpd_unlock (link -> parent );
9861017 }
9871018}
@@ -1040,7 +1071,7 @@ static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
10401071 child_node ) {
10411072 genpd_sd_counter_dec (link -> parent );
10421073 genpd_lock_nested (link -> parent , depth + 1 );
1043- genpd_power_off (link -> parent , false, depth + 1 );
1074+ genpd_power_off (link -> parent , false, false, depth + 1 );
10441075 genpd_unlock (link -> parent );
10451076 }
10461077
@@ -1107,7 +1138,7 @@ static void genpd_power_off_work_fn(struct work_struct *work)
11071138 genpd = container_of (work , struct generic_pm_domain , power_off_work );
11081139
11091140 genpd_lock (genpd );
1110- genpd_power_off (genpd , false, 0 );
1141+ genpd_power_off (genpd , false, false, 0 );
11111142 genpd_unlock (genpd );
11121143}
11131144
@@ -1172,6 +1203,7 @@ static int genpd_runtime_suspend(struct device *dev)
11721203 struct generic_pm_domain_data * gpd_data = dev_gpd_data (dev );
11731204 struct gpd_timing_data * td = gpd_data -> td ;
11741205 bool runtime_pm = pm_runtime_enabled (dev );
1206+ bool probing = dev -> links .status != DL_DEV_DRIVER_BOUND ;
11751207 ktime_t time_start = 0 ;
11761208 s64 elapsed_ns ;
11771209 int ret ;
@@ -1226,7 +1258,7 @@ static int genpd_runtime_suspend(struct device *dev)
12261258 return 0 ;
12271259
12281260 genpd_lock (genpd );
1229- genpd_power_off (genpd , true, 0 );
1261+ genpd_power_off (genpd , true, probing , 0 );
12301262 gpd_data -> rpm_pstate = genpd_drop_performance_state (dev );
12311263 genpd_unlock (genpd );
12321264
@@ -1247,6 +1279,7 @@ static int genpd_runtime_resume(struct device *dev)
12471279 struct generic_pm_domain_data * gpd_data = dev_gpd_data (dev );
12481280 struct gpd_timing_data * td = gpd_data -> td ;
12491281 bool timed = td && pm_runtime_enabled (dev );
1282+ bool probing = dev -> links .status != DL_DEV_DRIVER_BOUND ;
12501283 ktime_t time_start = 0 ;
12511284 s64 elapsed_ns ;
12521285 int ret ;
@@ -1304,7 +1337,7 @@ static int genpd_runtime_resume(struct device *dev)
13041337err_poweroff :
13051338 if (!pm_runtime_is_irq_safe (dev ) || genpd_is_irq_safe (genpd )) {
13061339 genpd_lock (genpd );
1307- genpd_power_off (genpd , true, 0 );
1340+ genpd_power_off (genpd , true, probing , 0 );
13081341 gpd_data -> rpm_pstate = genpd_drop_performance_state (dev );
13091342 genpd_unlock (genpd );
13101343 }
@@ -1371,6 +1404,9 @@ static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
13711404 || atomic_read (& genpd -> sd_count ) > 0 )
13721405 return ;
13731406
1407+ if (genpd_must_defer (genpd , false))
1408+ return ;
1409+
13741410 /* Check that the children are in their deepest (powered-off) state. */
13751411 list_for_each_entry (link , & genpd -> parent_links , parent_node ) {
13761412 struct generic_pm_domain * child = link -> child ;
@@ -2374,6 +2410,12 @@ int pm_genpd_init(struct generic_pm_domain *genpd,
23742410 return - EINVAL ;
23752411 }
23762412
2413+ /* Deferred-off power domains should be powered on at initialization. */
2414+ if (genpd_is_defer_off (genpd ) && !genpd_status_on (genpd )) {
2415+ pr_warn ("deferred-off PM domain %s is not on at init\n" , genpd -> name );
2416+ genpd -> flags &= ~GENPD_FLAG_DEFER_OFF ;
2417+ }
2418+
23772419 /* Multiple states but no governor doesn't make sense. */
23782420 if (!gov && genpd -> state_count > 1 )
23792421 pr_warn ("%s: no governor for states\n" , genpd -> name );
0 commit comments