0001
0002
0003
0004
0005
0006
0007 #include <linux/kernel.h>
0008 #include <linux/pm_domain.h>
0009 #include <linux/pm_qos.h>
0010 #include <linux/hrtimer.h>
0011 #include <linux/cpuidle.h>
0012 #include <linux/cpumask.h>
0013 #include <linux/ktime.h>
0014
0015 static int dev_update_qos_constraint(struct device *dev, void *data)
0016 {
0017 s64 *constraint_ns_p = data;
0018 s64 constraint_ns;
0019
0020 if (dev->power.subsys_data && dev->power.subsys_data->domain_data) {
0021 struct gpd_timing_data *td = dev_gpd_data(dev)->td;
0022
0023
0024
0025
0026
0027
0028
0029
0030 constraint_ns = td ? td->effective_constraint_ns :
0031 PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
0032 } else {
0033
0034
0035
0036
0037
0038
0039 constraint_ns = dev_pm_qos_read_value(dev, DEV_PM_QOS_RESUME_LATENCY);
0040 constraint_ns *= NSEC_PER_USEC;
0041 }
0042
0043 if (constraint_ns < *constraint_ns_p)
0044 *constraint_ns_p = constraint_ns;
0045
0046 return 0;
0047 }
0048
0049
0050
0051
0052
0053 static bool default_suspend_ok(struct device *dev)
0054 {
0055 struct gpd_timing_data *td = dev_gpd_data(dev)->td;
0056 unsigned long flags;
0057 s64 constraint_ns;
0058
0059 dev_dbg(dev, "%s()\n", __func__);
0060
0061 spin_lock_irqsave(&dev->power.lock, flags);
0062
0063 if (!td->constraint_changed) {
0064 bool ret = td->cached_suspend_ok;
0065
0066 spin_unlock_irqrestore(&dev->power.lock, flags);
0067 return ret;
0068 }
0069 td->constraint_changed = false;
0070 td->cached_suspend_ok = false;
0071 td->effective_constraint_ns = 0;
0072 constraint_ns = __dev_pm_qos_resume_latency(dev);
0073
0074 spin_unlock_irqrestore(&dev->power.lock, flags);
0075
0076 if (constraint_ns == 0)
0077 return false;
0078
0079 constraint_ns *= NSEC_PER_USEC;
0080
0081
0082
0083
0084
0085 if (!dev->power.ignore_children)
0086 device_for_each_child(dev, &constraint_ns,
0087 dev_update_qos_constraint);
0088
0089 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS) {
0090
0091 td->effective_constraint_ns = PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS;
0092 td->cached_suspend_ok = true;
0093 } else if (constraint_ns == 0) {
0094
0095
0096
0097
0098
0099
0100 return false;
0101 } else {
0102 constraint_ns -= td->suspend_latency_ns +
0103 td->resume_latency_ns;
0104
0105
0106
0107
0108
0109 if (constraint_ns <= 0)
0110 return false;
0111
0112 td->effective_constraint_ns = constraint_ns;
0113 td->cached_suspend_ok = true;
0114 }
0115
0116
0117
0118
0119
0120 return td->cached_suspend_ok;
0121 }
0122
0123 static void update_domain_next_wakeup(struct generic_pm_domain *genpd, ktime_t now)
0124 {
0125 ktime_t domain_wakeup = KTIME_MAX;
0126 ktime_t next_wakeup;
0127 struct pm_domain_data *pdd;
0128 struct gpd_link *link;
0129
0130 if (!(genpd->flags & GENPD_FLAG_MIN_RESIDENCY))
0131 return;
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
0142 next_wakeup = to_gpd_data(pdd)->td->next_wakeup;
0143 if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
0144 if (ktime_before(next_wakeup, domain_wakeup))
0145 domain_wakeup = next_wakeup;
0146 }
0147
0148 list_for_each_entry(link, &genpd->parent_links, parent_node) {
0149 struct genpd_governor_data *cgd = link->child->gd;
0150
0151 next_wakeup = cgd ? cgd->next_wakeup : KTIME_MAX;
0152 if (next_wakeup != KTIME_MAX && !ktime_before(next_wakeup, now))
0153 if (ktime_before(next_wakeup, domain_wakeup))
0154 domain_wakeup = next_wakeup;
0155 }
0156
0157 genpd->gd->next_wakeup = domain_wakeup;
0158 }
0159
0160 static bool next_wakeup_allows_state(struct generic_pm_domain *genpd,
0161 unsigned int state, ktime_t now)
0162 {
0163 ktime_t domain_wakeup = genpd->gd->next_wakeup;
0164 s64 idle_time_ns, min_sleep_ns;
0165
0166 min_sleep_ns = genpd->states[state].power_off_latency_ns +
0167 genpd->states[state].residency_ns;
0168
0169 idle_time_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
0170
0171 return idle_time_ns >= min_sleep_ns;
0172 }
0173
0174 static bool __default_power_down_ok(struct dev_pm_domain *pd,
0175 unsigned int state)
0176 {
0177 struct generic_pm_domain *genpd = pd_to_genpd(pd);
0178 struct gpd_link *link;
0179 struct pm_domain_data *pdd;
0180 s64 min_off_time_ns;
0181 s64 off_on_time_ns;
0182
0183 off_on_time_ns = genpd->states[state].power_off_latency_ns +
0184 genpd->states[state].power_on_latency_ns;
0185
0186 min_off_time_ns = -1;
0187
0188
0189
0190
0191
0192 list_for_each_entry(link, &genpd->parent_links, parent_node) {
0193 struct genpd_governor_data *cgd = link->child->gd;
0194
0195 s64 sd_max_off_ns = cgd ? cgd->max_off_time_ns : -1;
0196
0197 if (sd_max_off_ns < 0)
0198 continue;
0199
0200
0201
0202
0203
0204
0205 if (sd_max_off_ns <= off_on_time_ns)
0206 return false;
0207
0208 if (min_off_time_ns > sd_max_off_ns || min_off_time_ns < 0)
0209 min_off_time_ns = sd_max_off_ns;
0210 }
0211
0212
0213
0214
0215 list_for_each_entry(pdd, &genpd->dev_list, list_node) {
0216 struct gpd_timing_data *td;
0217 s64 constraint_ns;
0218
0219
0220
0221
0222
0223
0224 td = to_gpd_data(pdd)->td;
0225 constraint_ns = td->effective_constraint_ns;
0226
0227
0228
0229
0230 if (constraint_ns == PM_QOS_RESUME_LATENCY_NO_CONSTRAINT_NS)
0231 continue;
0232
0233 if (constraint_ns <= off_on_time_ns)
0234 return false;
0235
0236 if (min_off_time_ns > constraint_ns || min_off_time_ns < 0)
0237 min_off_time_ns = constraint_ns;
0238 }
0239
0240
0241
0242
0243
0244
0245 if (min_off_time_ns < 0)
0246 return true;
0247
0248
0249
0250
0251
0252
0253 genpd->gd->max_off_time_ns = min_off_time_ns -
0254 genpd->states[state].power_on_latency_ns;
0255 return true;
0256 }
0257
0258
0259
0260
0261
0262
0263
0264
0265 static bool _default_power_down_ok(struct dev_pm_domain *pd, ktime_t now)
0266 {
0267 struct generic_pm_domain *genpd = pd_to_genpd(pd);
0268 struct genpd_governor_data *gd = genpd->gd;
0269 int state_idx = genpd->state_count - 1;
0270 struct gpd_link *link;
0271
0272
0273
0274
0275
0276
0277
0278 update_domain_next_wakeup(genpd, now);
0279 if ((genpd->flags & GENPD_FLAG_MIN_RESIDENCY) && (gd->next_wakeup != KTIME_MAX)) {
0280
0281 while (state_idx >= 0) {
0282 if (next_wakeup_allows_state(genpd, state_idx, now)) {
0283 gd->max_off_time_changed = true;
0284 break;
0285 }
0286 state_idx--;
0287 }
0288
0289 if (state_idx < 0) {
0290 state_idx = 0;
0291 gd->cached_power_down_ok = false;
0292 goto done;
0293 }
0294 }
0295
0296 if (!gd->max_off_time_changed) {
0297 genpd->state_idx = gd->cached_power_down_state_idx;
0298 return gd->cached_power_down_ok;
0299 }
0300
0301
0302
0303
0304
0305
0306
0307 list_for_each_entry(link, &genpd->child_links, child_node) {
0308 struct genpd_governor_data *pgd = link->parent->gd;
0309
0310 if (pgd)
0311 pgd->max_off_time_changed = true;
0312 }
0313
0314 gd->max_off_time_ns = -1;
0315 gd->max_off_time_changed = false;
0316 gd->cached_power_down_ok = true;
0317
0318
0319
0320
0321
0322 while (!__default_power_down_ok(pd, state_idx)) {
0323 if (state_idx == 0) {
0324 gd->cached_power_down_ok = false;
0325 break;
0326 }
0327 state_idx--;
0328 }
0329
0330 done:
0331 genpd->state_idx = state_idx;
0332 gd->cached_power_down_state_idx = genpd->state_idx;
0333 return gd->cached_power_down_ok;
0334 }
0335
0336 static bool default_power_down_ok(struct dev_pm_domain *pd)
0337 {
0338 return _default_power_down_ok(pd, ktime_get());
0339 }
0340
0341 #ifdef CONFIG_CPU_IDLE
0342 static bool cpu_power_down_ok(struct dev_pm_domain *pd)
0343 {
0344 struct generic_pm_domain *genpd = pd_to_genpd(pd);
0345 struct cpuidle_device *dev;
0346 ktime_t domain_wakeup, next_hrtimer;
0347 ktime_t now = ktime_get();
0348 s64 idle_duration_ns;
0349 int cpu, i;
0350
0351
0352 if (!_default_power_down_ok(pd, now))
0353 return false;
0354
0355 if (!(genpd->flags & GENPD_FLAG_CPU_DOMAIN))
0356 return true;
0357
0358
0359
0360
0361
0362
0363 domain_wakeup = ktime_set(KTIME_SEC_MAX, 0);
0364 for_each_cpu_and(cpu, genpd->cpus, cpu_online_mask) {
0365 dev = per_cpu(cpuidle_devices, cpu);
0366 if (dev) {
0367 next_hrtimer = READ_ONCE(dev->next_hrtimer);
0368 if (ktime_before(next_hrtimer, domain_wakeup))
0369 domain_wakeup = next_hrtimer;
0370 }
0371 }
0372
0373
0374 idle_duration_ns = ktime_to_ns(ktime_sub(domain_wakeup, now));
0375 if (idle_duration_ns <= 0)
0376 return false;
0377
0378
0379
0380
0381
0382
0383 i = genpd->state_idx;
0384 do {
0385 if (idle_duration_ns >= (genpd->states[i].residency_ns +
0386 genpd->states[i].power_off_latency_ns)) {
0387 genpd->state_idx = i;
0388 return true;
0389 }
0390 } while (--i >= 0);
0391
0392 return false;
0393 }
0394
0395 struct dev_power_governor pm_domain_cpu_gov = {
0396 .suspend_ok = default_suspend_ok,
0397 .power_down_ok = cpu_power_down_ok,
0398 };
0399 #endif
0400
0401 struct dev_power_governor simple_qos_governor = {
0402 .suspend_ok = default_suspend_ok,
0403 .power_down_ok = default_power_down_ok,
0404 };
0405
0406
0407
0408
0409 struct dev_power_governor pm_domain_always_on_gov = {
0410 .suspend_ok = default_suspend_ok,
0411 };