77#define pr_fmt (fmt ) "PM: " fmt
88
99#include <linux/delay.h>
10+ #include <linux/fwnode.h>
1011#include <linux/kernel.h>
1112#include <linux/io.h>
1213#include <linux/platform_device.h>
@@ -129,6 +130,7 @@ static const struct genpd_lock_ops genpd_spin_ops = {
129130#define genpd_is_cpu_domain (genpd ) (genpd->flags & GENPD_FLAG_CPU_DOMAIN)
130131#define genpd_is_rpm_always_on (genpd ) (genpd->flags & GENPD_FLAG_RPM_ALWAYS_ON)
131132#define genpd_is_opp_table_fw (genpd ) (genpd->flags & GENPD_FLAG_OPP_TABLE_FW)
133+ #define genpd_is_defer_off (genpd ) (genpd->flags & GENPD_FLAG_DEFER_OFF)
132134
133135static inline bool irq_safe_dev_in_sleep_domain (struct device * dev ,
134136 const struct generic_pm_domain * genpd )
@@ -763,6 +765,27 @@ static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
763765 queue_work (pm_wq , & genpd -> power_off_work );
764766}
765767
768+ /**
769+ * genpd_must_defer - Check whether the genpd cannot be safely powered off.
770+ * @genpd: PM domain about to be powered down.
771+ * @one_dev_probing: True if we are being called from RPM callbacks on a device that
772+ * is probing, to allow poweroff if that device is the sole remaining consumer probing.
773+ *
774+ * Returns true if the @genpd has the GENPD_FLAG_DEFER_OFF flag and there
775+ * are any consumer devices which either do not exist yet (only represented
776+ * by fwlinks) or whose drivers have not probed yet.
777+ */
778+ static bool genpd_must_defer (struct generic_pm_domain * genpd , bool one_dev_probing )
779+ {
780+ if (genpd_is_defer_off (genpd ) && genpd -> has_provider ) {
781+ int absent = fw_devlink_count_absent_consumers (genpd -> provider );
782+
783+ if (absent > (one_dev_probing ? 1 : 0 ))
784+ return true;
785+ }
786+ return false;
787+ }
788+
766789/**
767790 * genpd_power_off - Remove power from a given PM domain.
768791 * @genpd: PM domain to power down.
@@ -776,7 +799,7 @@ static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
776799 * have been powered down, remove power from @genpd.
777800 */
778801static int genpd_power_off (struct generic_pm_domain * genpd , bool one_dev_on ,
779- unsigned int depth )
802+ bool one_dev_probing , unsigned int depth )
780803{
781804 struct pm_domain_data * pdd ;
782805 struct gpd_link * link ;
@@ -826,6 +849,14 @@ static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
826849 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on ))
827850 return - EBUSY ;
828851
852+ /*
853+ * Do not allow PM domain to be powered off if it is marked
854+ * as GENPD_FLAG_DEFER_OFF and there are consumer devices
855+ * which have not probed yet.
856+ */
857+ if (genpd_must_defer (genpd , one_dev_probing ))
858+ return - EBUSY ;
859+
829860 if (genpd -> gov && genpd -> gov -> power_down_ok ) {
830861 if (!genpd -> gov -> power_down_ok (& genpd -> domain ))
831862 return - EAGAIN ;
@@ -852,7 +883,7 @@ static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
852883 list_for_each_entry (link , & genpd -> child_links , child_node ) {
853884 genpd_sd_counter_dec (link -> parent );
854885 genpd_lock_nested (link -> parent , depth + 1 );
855- genpd_power_off (link -> parent , false, depth + 1 );
886+ genpd_power_off (link -> parent , false, false, depth + 1 );
856887 genpd_unlock (link -> parent );
857888 }
858889
@@ -910,7 +941,7 @@ static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
910941 child_node ) {
911942 genpd_sd_counter_dec (link -> parent );
912943 genpd_lock_nested (link -> parent , depth + 1 );
913- genpd_power_off (link -> parent , false, depth + 1 );
944+ genpd_power_off (link -> parent , false, false, depth + 1 );
914945 genpd_unlock (link -> parent );
915946 }
916947
@@ -977,7 +1008,7 @@ static void genpd_power_off_work_fn(struct work_struct *work)
9771008 genpd = container_of (work , struct generic_pm_domain , power_off_work );
9781009
9791010 genpd_lock (genpd );
980- genpd_power_off (genpd , false, 0 );
1011+ genpd_power_off (genpd , false, false, 0 );
9811012 genpd_unlock (genpd );
9821013}
9831014
@@ -1042,6 +1073,7 @@ static int genpd_runtime_suspend(struct device *dev)
10421073 struct generic_pm_domain_data * gpd_data = dev_gpd_data (dev );
10431074 struct gpd_timing_data * td = gpd_data -> td ;
10441075 bool runtime_pm = pm_runtime_enabled (dev );
1076+ bool probing = dev -> links .status != DL_DEV_DRIVER_BOUND ;
10451077 ktime_t time_start = 0 ;
10461078 s64 elapsed_ns ;
10471079 int ret ;
@@ -1096,7 +1128,7 @@ static int genpd_runtime_suspend(struct device *dev)
10961128 return 0 ;
10971129
10981130 genpd_lock (genpd );
1099- genpd_power_off (genpd , true, 0 );
1131+ genpd_power_off (genpd , true, probing , 0 );
11001132 gpd_data -> rpm_pstate = genpd_drop_performance_state (dev );
11011133 genpd_unlock (genpd );
11021134
@@ -1117,6 +1149,7 @@ static int genpd_runtime_resume(struct device *dev)
11171149 struct generic_pm_domain_data * gpd_data = dev_gpd_data (dev );
11181150 struct gpd_timing_data * td = gpd_data -> td ;
11191151 bool timed = td && pm_runtime_enabled (dev );
1152+ bool probing = dev -> links .status != DL_DEV_DRIVER_BOUND ;
11201153 ktime_t time_start = 0 ;
11211154 s64 elapsed_ns ;
11221155 int ret ;
@@ -1174,7 +1207,7 @@ static int genpd_runtime_resume(struct device *dev)
11741207err_poweroff :
11751208 if (!pm_runtime_is_irq_safe (dev ) || genpd_is_irq_safe (genpd )) {
11761209 genpd_lock (genpd );
1177- genpd_power_off (genpd , true, 0 );
1210+ genpd_power_off (genpd , true, probing , 0 );
11781211 gpd_data -> rpm_pstate = genpd_drop_performance_state (dev );
11791212 genpd_unlock (genpd );
11801213 }
@@ -1241,6 +1274,9 @@ static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
12411274 || atomic_read (& genpd -> sd_count ) > 0 )
12421275 return ;
12431276
1277+ if (genpd_must_defer (genpd , false))
1278+ return ;
1279+
12441280 /* Check that the children are in their deepest (powered-off) state. */
12451281 list_for_each_entry (link , & genpd -> parent_links , parent_node ) {
12461282 struct generic_pm_domain * child = link -> child ;
@@ -2210,6 +2246,12 @@ int pm_genpd_init(struct generic_pm_domain *genpd,
22102246 return - EINVAL ;
22112247 }
22122248
2249+ /* Deferred-off power domains should be powered on at initialization. */
2250+ if (genpd_is_defer_off (genpd ) && !genpd_status_on (genpd )) {
2251+ pr_warn ("deferred-off PM domain %s is not on at init\n" , genpd -> name );
2252+ genpd -> flags &= ~GENPD_FLAG_DEFER_OFF ;
2253+ }
2254+
22132255 /* Multiple states but no governor doesn't make sense. */
22142256 if (!gov && genpd -> state_count > 1 )
22152257 pr_warn ("%s: no governor for states\n" , genpd -> name );
0 commit comments