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 )
@@ -691,6 +693,27 @@ static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
691693 queue_work (pm_wq , & genpd -> power_off_work );
692694}
693695
696+ /**
697+ * genpd_must_defer - Check whether the genpd cannot be safely powered off.
698+ * @genpd: PM domain about to be powered down.
699+ * @one_dev_probing: True if we are being called from RPM callbacks on a device that
700+ * is probing, to allow poweroff if that device is the sole remaining consumer probing.
701+ *
702+ * Returns true if the @genpd has the GENPD_FLAG_DEFER_OFF flag and there
703+ * are any consumer devices which either do not exist yet (only represented
704+ * by fwlinks) or whose drivers have not probed yet.
705+ */
706+ static bool genpd_must_defer (struct generic_pm_domain * genpd , bool one_dev_probing )
707+ {
708+ if (genpd_is_defer_off (genpd ) && genpd -> has_provider ) {
709+ int absent = fw_devlink_count_absent_consumers (genpd -> provider );
710+
711+ if (absent > (one_dev_probing ? 1 : 0 ))
712+ return true;
713+ }
714+ return false;
715+ }
716+
694717/**
695718 * genpd_power_off - Remove power from a given PM domain.
696719 * @genpd: PM domain to power down.
@@ -704,7 +727,7 @@ static void genpd_queue_power_off_work(struct generic_pm_domain *genpd)
704727 * have been powered down, remove power from @genpd.
705728 */
706729static int genpd_power_off (struct generic_pm_domain * genpd , bool one_dev_on ,
707- unsigned int depth )
730+ bool one_dev_probing , unsigned int depth )
708731{
709732 struct pm_domain_data * pdd ;
710733 struct gpd_link * link ;
@@ -754,6 +777,14 @@ static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
754777 if (not_suspended > 1 || (not_suspended == 1 && !one_dev_on ))
755778 return - EBUSY ;
756779
780+ /*
781+ * Do not allow PM domain to be powered off if it is marked
782+ * as GENPD_FLAG_DEFER_OFF and there are consumer devices
783+ * which have not probed yet.
784+ */
785+ if (genpd_must_defer (genpd , one_dev_probing ))
786+ return - EBUSY ;
787+
757788 if (genpd -> gov && genpd -> gov -> power_down_ok ) {
758789 if (!genpd -> gov -> power_down_ok (& genpd -> domain ))
759790 return - EAGAIN ;
@@ -780,7 +811,7 @@ static int genpd_power_off(struct generic_pm_domain *genpd, bool one_dev_on,
780811 list_for_each_entry (link , & genpd -> child_links , child_node ) {
781812 genpd_sd_counter_dec (link -> parent );
782813 genpd_lock_nested (link -> parent , depth + 1 );
783- genpd_power_off (link -> parent , false, depth + 1 );
814+ genpd_power_off (link -> parent , false, false, depth + 1 );
784815 genpd_unlock (link -> parent );
785816 }
786817
@@ -838,7 +869,7 @@ static int genpd_power_on(struct generic_pm_domain *genpd, unsigned int depth)
838869 child_node ) {
839870 genpd_sd_counter_dec (link -> parent );
840871 genpd_lock_nested (link -> parent , depth + 1 );
841- genpd_power_off (link -> parent , false, depth + 1 );
872+ genpd_power_off (link -> parent , false, false, depth + 1 );
842873 genpd_unlock (link -> parent );
843874 }
844875
@@ -905,7 +936,7 @@ static void genpd_power_off_work_fn(struct work_struct *work)
905936 genpd = container_of (work , struct generic_pm_domain , power_off_work );
906937
907938 genpd_lock (genpd );
908- genpd_power_off (genpd , false, 0 );
939+ genpd_power_off (genpd , false, false, 0 );
909940 genpd_unlock (genpd );
910941}
911942
@@ -970,6 +1001,7 @@ static int genpd_runtime_suspend(struct device *dev)
9701001 struct generic_pm_domain_data * gpd_data = dev_gpd_data (dev );
9711002 struct gpd_timing_data * td = gpd_data -> td ;
9721003 bool runtime_pm = pm_runtime_enabled (dev );
1004+ bool probing = dev -> links .status != DL_DEV_DRIVER_BOUND ;
9731005 ktime_t time_start = 0 ;
9741006 s64 elapsed_ns ;
9751007 int ret ;
@@ -1024,7 +1056,7 @@ static int genpd_runtime_suspend(struct device *dev)
10241056 return 0 ;
10251057
10261058 genpd_lock (genpd );
1027- genpd_power_off (genpd , true, 0 );
1059+ genpd_power_off (genpd , true, probing , 0 );
10281060 gpd_data -> rpm_pstate = genpd_drop_performance_state (dev );
10291061 genpd_unlock (genpd );
10301062
@@ -1045,6 +1077,7 @@ static int genpd_runtime_resume(struct device *dev)
10451077 struct generic_pm_domain_data * gpd_data = dev_gpd_data (dev );
10461078 struct gpd_timing_data * td = gpd_data -> td ;
10471079 bool timed = td && pm_runtime_enabled (dev );
1080+ bool probing = dev -> links .status != DL_DEV_DRIVER_BOUND ;
10481081 ktime_t time_start = 0 ;
10491082 s64 elapsed_ns ;
10501083 int ret ;
@@ -1102,7 +1135,7 @@ static int genpd_runtime_resume(struct device *dev)
11021135err_poweroff :
11031136 if (!pm_runtime_is_irq_safe (dev ) || genpd_is_irq_safe (genpd )) {
11041137 genpd_lock (genpd );
1105- genpd_power_off (genpd , true, 0 );
1138+ genpd_power_off (genpd , true, probing , 0 );
11061139 gpd_data -> rpm_pstate = genpd_drop_performance_state (dev );
11071140 genpd_unlock (genpd );
11081141 }
@@ -1169,6 +1202,9 @@ static void genpd_sync_power_off(struct generic_pm_domain *genpd, bool use_lock,
11691202 || atomic_read (& genpd -> sd_count ) > 0 )
11701203 return ;
11711204
1205+ if (genpd_must_defer (genpd , false))
1206+ return ;
1207+
11721208 /* Check that the children are in their deepest (powered-off) state. */
11731209 list_for_each_entry (link , & genpd -> parent_links , parent_node ) {
11741210 struct generic_pm_domain * child = link -> child ;
@@ -2135,6 +2171,12 @@ int pm_genpd_init(struct generic_pm_domain *genpd,
21352171 return - EINVAL ;
21362172 }
21372173
2174+ /* Deferred-off power domains should be powered on at initialization. */
2175+ if (genpd_is_defer_off (genpd ) && !genpd_status_on (genpd )) {
2176+ pr_warn ("deferred-off PM domain %s is not on at init\n" , genpd -> name );
2177+ genpd -> flags &= ~GENPD_FLAG_DEFER_OFF ;
2178+ }
2179+
21382180 /* Multiple states but no governor doesn't make sense. */
21392181 if (!gov && genpd -> state_count > 1 )
21402182 pr_warn ("%s: no governor for states\n" , genpd -> name );
0 commit comments