Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/platform_device.h> |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 19 | #include <linux/mutex.h> |
| 20 | #include <linux/cpu.h> |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 21 | #include <linux/of.h> |
| 22 | #include <mach/mpm.h> |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 23 | #include "pm.h" |
Mahesh Sivasubramanian | 11dad77 | 2012-07-13 14:00:01 -0600 | [diff] [blame] | 24 | #include "rpm-notifier.h" |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 25 | #include "spm.h" |
| 26 | #include "idle.h" |
Mahesh Sivasubramanian | fae923f | 2012-10-08 16:22:51 -0600 | [diff] [blame] | 27 | |
| 28 | enum { |
| 29 | MSM_LPM_LVL_DBG_SUSPEND_LIMITS = BIT(0), |
| 30 | MSM_LPM_LVL_DBG_IDLE_LIMITS = BIT(1), |
| 31 | }; |
| 32 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 33 | enum { |
| 34 | MSM_SCM_L2_ON = 0, |
| 35 | MSM_SCM_L2_OFF = 1, |
| 36 | MSM_SCM_L2_GDHS = 3, |
| 37 | }; |
| 38 | |
| 39 | struct msm_rpmrs_level { |
| 40 | enum msm_pm_sleep_mode sleep_mode; |
| 41 | uint32_t l2_cache; |
| 42 | bool available; |
| 43 | uint32_t latency_us; |
| 44 | uint32_t steady_state_power; |
| 45 | uint32_t energy_overhead; |
| 46 | uint32_t time_overhead_us; |
| 47 | }; |
| 48 | |
| 49 | struct lpm_lookup_table { |
| 50 | uint32_t modes; |
| 51 | const char *mode_name; |
| 52 | }; |
| 53 | |
| 54 | static void msm_lpm_level_update(void); |
| 55 | |
| 56 | static int msm_lpm_cpu_callback(struct notifier_block *cpu_nb, |
| 57 | unsigned long action, void *hcpu); |
| 58 | |
| 59 | static struct notifier_block __refdata msm_lpm_cpu_nblk = { |
| 60 | .notifier_call = msm_lpm_cpu_callback, |
| 61 | }; |
| 62 | |
| 63 | static uint32_t allowed_l2_mode; |
| 64 | static uint32_t sysfs_dbg_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
| 65 | static uint32_t default_l2_mode; |
| 66 | |
| 67 | static bool no_l2_saw; |
| 68 | |
| 69 | static ssize_t msm_lpm_levels_attr_show( |
| 70 | struct kobject *kobj, struct kobj_attribute *attr, char *buf); |
| 71 | static ssize_t msm_lpm_levels_attr_store(struct kobject *kobj, |
| 72 | struct kobj_attribute *attr, const char *buf, size_t count); |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 73 | |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 74 | #define ADJUST_LATENCY(x) \ |
| 75 | ((x == MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE) ?\ |
| 76 | (num_online_cpus()) / 2 : 0) |
| 77 | |
Mahesh Sivasubramanian | fae923f | 2012-10-08 16:22:51 -0600 | [diff] [blame] | 78 | static int msm_lpm_lvl_dbg_msk; |
| 79 | |
| 80 | module_param_named( |
| 81 | debug_mask, msm_lpm_lvl_dbg_msk, int, S_IRUGO | S_IWUSR | S_IWGRP |
| 82 | ); |
| 83 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 84 | static struct msm_rpmrs_level *msm_lpm_levels; |
| 85 | static int msm_lpm_level_count; |
| 86 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 87 | static struct kobj_attribute lpm_l2_kattr = __ATTR(l2, S_IRUGO|S_IWUSR,\ |
| 88 | msm_lpm_levels_attr_show, msm_lpm_levels_attr_store); |
| 89 | |
| 90 | static struct attribute *lpm_levels_attr[] = { |
| 91 | &lpm_l2_kattr.attr, |
| 92 | NULL, |
| 93 | }; |
| 94 | |
| 95 | static struct attribute_group lpm_levels_attr_grp = { |
| 96 | .attrs = lpm_levels_attr, |
| 97 | }; |
| 98 | |
| 99 | /* SYSFS */ |
| 100 | static ssize_t msm_lpm_levels_attr_show( |
| 101 | struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 102 | { |
| 103 | struct kernel_param kp; |
| 104 | int rc; |
| 105 | |
| 106 | kp.arg = &sysfs_dbg_l2_mode; |
| 107 | |
| 108 | rc = param_get_uint(buf, &kp); |
| 109 | |
| 110 | if (rc > 0) { |
| 111 | strlcat(buf, "\n", PAGE_SIZE); |
| 112 | rc++; |
| 113 | } |
| 114 | |
| 115 | return rc; |
| 116 | } |
| 117 | |
| 118 | static ssize_t msm_lpm_levels_attr_store(struct kobject *kobj, |
| 119 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 120 | { |
| 121 | struct kernel_param kp; |
| 122 | unsigned int temp; |
| 123 | int rc; |
| 124 | |
| 125 | kp.arg = &temp; |
| 126 | rc = param_set_uint(buf, &kp); |
| 127 | if (rc) |
| 128 | return rc; |
| 129 | |
| 130 | sysfs_dbg_l2_mode = temp; |
| 131 | msm_lpm_level_update(); |
| 132 | |
| 133 | return count; |
| 134 | } |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 135 | |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 136 | static int msm_pm_get_sleep_mode_value(struct device_node *node, |
| 137 | const char *key, uint32_t *sleep_mode_val) |
| 138 | { |
| 139 | int i; |
| 140 | struct lpm_lookup_table { |
| 141 | uint32_t modes; |
| 142 | const char *mode_name; |
| 143 | }; |
| 144 | struct lpm_lookup_table pm_sm_lookup[] = { |
| 145 | {MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT, |
| 146 | "wfi"}, |
| 147 | {MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT, |
| 148 | "ramp_down_and_wfi"}, |
| 149 | {MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE, |
| 150 | "standalone_pc"}, |
| 151 | {MSM_PM_SLEEP_MODE_POWER_COLLAPSE, |
| 152 | "pc"}, |
| 153 | {MSM_PM_SLEEP_MODE_RETENTION, |
| 154 | "retention"}, |
| 155 | {MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND, |
| 156 | "pc_suspend"}, |
| 157 | {MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN, |
| 158 | "pc_no_xo_shutdown"} |
| 159 | }; |
| 160 | int ret; |
| 161 | const char *mode_name; |
| 162 | |
| 163 | ret = of_property_read_string(node, key, &mode_name); |
| 164 | if (!ret) { |
| 165 | ret = -EINVAL; |
| 166 | for (i = 0; i < ARRAY_SIZE(pm_sm_lookup); i++) { |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 167 | if (!strcmp(mode_name, pm_sm_lookup[i].mode_name)) { |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 168 | *sleep_mode_val = pm_sm_lookup[i].modes; |
| 169 | ret = 0; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | return ret; |
| 175 | } |
| 176 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 177 | static int msm_lpm_set_l2_mode(int sleep_mode) |
| 178 | { |
| 179 | int lpm = sleep_mode; |
| 180 | int rc = 0; |
| 181 | |
| 182 | if (no_l2_saw) |
| 183 | goto bail_set_l2_mode; |
| 184 | |
| 185 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_ON); |
| 186 | |
| 187 | switch (sleep_mode) { |
| 188 | case MSM_SPM_L2_MODE_POWER_COLLAPSE: |
| 189 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_OFF); |
| 190 | break; |
| 191 | case MSM_SPM_L2_MODE_GDHS: |
| 192 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_GDHS); |
| 193 | break; |
| 194 | case MSM_SPM_L2_MODE_RETENTION: |
| 195 | case MSM_SPM_L2_MODE_DISABLED: |
| 196 | break; |
| 197 | default: |
| 198 | lpm = MSM_SPM_L2_MODE_DISABLED; |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | rc = msm_spm_l2_set_low_power_mode(lpm, true); |
| 203 | |
| 204 | if (rc) { |
| 205 | if (rc == -ENXIO) |
| 206 | WARN_ON_ONCE(1); |
| 207 | else |
| 208 | pr_err("%s: Failed to set L2 low power mode %d, ERR %d", |
| 209 | __func__, lpm, rc); |
| 210 | } |
| 211 | |
| 212 | bail_set_l2_mode: |
| 213 | return rc; |
| 214 | } |
| 215 | |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 216 | static void msm_lpm_level_update(void) |
| 217 | { |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 218 | int lpm_level; |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 219 | struct msm_rpmrs_level *level = NULL; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 220 | uint32_t max_l2_mode; |
| 221 | static DEFINE_MUTEX(lpm_lock); |
| 222 | |
| 223 | mutex_lock(&lpm_lock); |
| 224 | |
| 225 | max_l2_mode = min(allowed_l2_mode, sysfs_dbg_l2_mode); |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 226 | |
| 227 | for (lpm_level = 0; lpm_level < msm_lpm_level_count; lpm_level++) { |
| 228 | level = &msm_lpm_levels[lpm_level]; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 229 | level->available = !(level->l2_cache > max_l2_mode); |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 230 | } |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 231 | mutex_unlock(&lpm_lock); |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | int msm_lpm_enter_sleep(uint32_t sclk_count, void *limits, |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 235 | bool from_idle, bool notify_rpm) |
| 236 | { |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 237 | int ret = 0; |
Mahesh Sivasubramanian | 9b0f412 | 2013-02-15 14:20:52 -0700 | [diff] [blame] | 238 | int debug_mask; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 239 | uint32_t l2 = *(uint32_t *)limits; |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 240 | |
Mahesh Sivasubramanian | 9b0f412 | 2013-02-15 14:20:52 -0700 | [diff] [blame] | 241 | if (from_idle) |
| 242 | debug_mask = msm_lpm_lvl_dbg_msk & |
Priyanka Mathur | 86cfc76 | 2013-01-10 17:03:04 -0800 | [diff] [blame] | 243 | MSM_LPM_LVL_DBG_IDLE_LIMITS; |
Mahesh Sivasubramanian | 9b0f412 | 2013-02-15 14:20:52 -0700 | [diff] [blame] | 244 | else |
| 245 | debug_mask = msm_lpm_lvl_dbg_msk & |
Priyanka Mathur | 86cfc76 | 2013-01-10 17:03:04 -0800 | [diff] [blame] | 246 | MSM_LPM_LVL_DBG_SUSPEND_LIMITS; |
Mahesh Sivasubramanian | 9b0f412 | 2013-02-15 14:20:52 -0700 | [diff] [blame] | 247 | |
| 248 | if (debug_mask) |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 249 | pr_info("%s(): l2:%d", __func__, l2); |
Mahesh Sivasubramanian | 9b0f412 | 2013-02-15 14:20:52 -0700 | [diff] [blame] | 250 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 251 | ret = msm_lpm_set_l2_mode(l2); |
| 252 | |
Mahesh Sivasubramanian | 0558d4b | 2012-10-12 18:05:28 -0600 | [diff] [blame] | 253 | if (ret) { |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 254 | if (ret == -ENXIO) |
| 255 | ret = 0; |
| 256 | else { |
| 257 | pr_warn("%s(): Failed to set L2 SPM Mode %d", |
| 258 | __func__, l2); |
| 259 | goto bail; |
| 260 | } |
Mahesh Sivasubramanian | 0558d4b | 2012-10-12 18:05:28 -0600 | [diff] [blame] | 261 | } |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 262 | |
Priyanka Mathur | 86cfc76 | 2013-01-10 17:03:04 -0800 | [diff] [blame] | 263 | if (notify_rpm) { |
| 264 | ret = msm_rpm_enter_sleep(debug_mask); |
| 265 | if (ret) { |
| 266 | pr_warn("%s(): RPM failed to enter sleep err:%d\n", |
| 267 | __func__, ret); |
| 268 | goto bail; |
| 269 | } |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 270 | |
| 271 | msm_mpm_enter_sleep(sclk_count, from_idle); |
Priyanka Mathur | 86cfc76 | 2013-01-10 17:03:04 -0800 | [diff] [blame] | 272 | } |
Mahesh Sivasubramanian | 11dad77 | 2012-07-13 14:00:01 -0600 | [diff] [blame] | 273 | bail: |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 274 | return ret; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 275 | } |
| 276 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 277 | static void msm_lpm_exit_sleep(void *limits, bool from_idle, |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 278 | bool notify_rpm, bool collapsed) |
| 279 | { |
Mahesh Sivasubramanian | 0558d4b | 2012-10-12 18:05:28 -0600 | [diff] [blame] | 280 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 281 | msm_lpm_set_l2_mode(default_l2_mode); |
| 282 | |
| 283 | if (notify_rpm) { |
| 284 | msm_mpm_exit_sleep(from_idle); |
Priyanka Mathur | 86cfc76 | 2013-01-10 17:03:04 -0800 | [diff] [blame] | 285 | msm_rpm_exit_sleep(); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 286 | } |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 287 | } |
| 288 | |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 289 | void msm_lpm_show_resources(void) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 290 | { |
| 291 | /* TODO */ |
| 292 | return; |
| 293 | } |
| 294 | |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 295 | s32 msm_cpuidle_get_deep_idle_latency(void) |
| 296 | { |
| 297 | int i; |
| 298 | struct msm_rpmrs_level *level = msm_lpm_levels, *best = level; |
| 299 | |
| 300 | if (!level) |
| 301 | return 0; |
| 302 | |
| 303 | for (i = 0; i < msm_lpm_level_count; i++, level++) { |
| 304 | if (!level->available) |
| 305 | continue; |
| 306 | if (level->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE) |
| 307 | continue; |
| 308 | /* Pick the first power collapse mode by default */ |
| 309 | if (best->sleep_mode != MSM_PM_SLEEP_MODE_POWER_COLLAPSE) |
| 310 | best = level; |
| 311 | /* Find the lowest latency for power collapse */ |
| 312 | if (level->latency_us < best->latency_us) |
| 313 | best = level; |
| 314 | } |
| 315 | return best->latency_us - 1; |
| 316 | } |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 317 | |
| 318 | static int msm_lpm_cpu_callback(struct notifier_block *cpu_nb, |
| 319 | unsigned long action, void *hcpu) |
Mahesh Sivasubramanian | f1ddf04 | 2013-01-08 14:03:32 -0700 | [diff] [blame] | 320 | { |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 321 | switch (action) { |
| 322 | case CPU_UP_PREPARE: |
| 323 | case CPU_UP_PREPARE_FROZEN: |
| 324 | allowed_l2_mode = default_l2_mode; |
| 325 | msm_lpm_level_update(); |
| 326 | break; |
| 327 | case CPU_DEAD_FROZEN: |
| 328 | case CPU_DEAD: |
| 329 | case CPU_UP_CANCELED: |
| 330 | case CPU_UP_CANCELED_FROZEN: |
| 331 | if (num_online_cpus() == 1) |
| 332 | allowed_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
| 333 | msm_lpm_level_update(); |
| 334 | break; |
| 335 | } |
| 336 | return NOTIFY_OK; |
Mahesh Sivasubramanian | f1ddf04 | 2013-01-08 14:03:32 -0700 | [diff] [blame] | 337 | } |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 338 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 339 | static void *msm_lpm_lowest_limits(bool from_idle, |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 340 | enum msm_pm_sleep_mode sleep_mode, |
| 341 | struct msm_pm_time_params *time_param, uint32_t *power) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 342 | { |
| 343 | unsigned int cpu = smp_processor_id(); |
| 344 | struct msm_rpmrs_level *best_level = NULL; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 345 | uint32_t best_level_pwr = 0; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 346 | uint32_t pwr; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 347 | int i; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 348 | bool modify_event_timer; |
| 349 | uint32_t next_wakeup_us = time_param->sleep_us; |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 350 | uint32_t lvl_latency_us = 0; |
| 351 | uint32_t lvl_overhead_us = 0; |
| 352 | uint32_t lvl_overhead_energy = 0; |
Mahesh Sivasubramanian | f1ddf04 | 2013-01-08 14:03:32 -0700 | [diff] [blame] | 353 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 354 | if (!msm_lpm_levels) |
| 355 | return NULL; |
| 356 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 357 | for (i = 0; i < msm_lpm_level_count; i++) { |
| 358 | struct msm_rpmrs_level *level = &msm_lpm_levels[i]; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 359 | |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 360 | modify_event_timer = false; |
| 361 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 362 | if (!level->available) |
| 363 | continue; |
| 364 | |
| 365 | if (sleep_mode != level->sleep_mode) |
| 366 | continue; |
| 367 | |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 368 | lvl_latency_us = |
| 369 | level->latency_us + (level->latency_us * |
| 370 | ADJUST_LATENCY(sleep_mode)); |
| 371 | |
| 372 | lvl_overhead_us = |
| 373 | level->time_overhead_us + (level->time_overhead_us * |
| 374 | ADJUST_LATENCY(sleep_mode)); |
| 375 | |
| 376 | lvl_overhead_energy = |
| 377 | level->energy_overhead + level->energy_overhead * |
| 378 | ADJUST_LATENCY(sleep_mode); |
| 379 | |
| 380 | if (time_param->latency_us < lvl_latency_us) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 381 | continue; |
| 382 | |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 383 | if (time_param->next_event_us && |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 384 | time_param->next_event_us < lvl_latency_us) |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 385 | continue; |
| 386 | |
| 387 | if (time_param->next_event_us) { |
| 388 | if ((time_param->next_event_us < time_param->sleep_us) |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 389 | || ((time_param->next_event_us - lvl_latency_us) < |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 390 | time_param->sleep_us)) { |
| 391 | modify_event_timer = true; |
| 392 | next_wakeup_us = time_param->next_event_us - |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 393 | lvl_latency_us; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 397 | if (next_wakeup_us <= lvl_overhead_us) |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 398 | continue; |
| 399 | |
Mahesh Sivasubramanian | 9063a29 | 2012-11-09 09:15:30 -0700 | [diff] [blame] | 400 | if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == sleep_mode) |
| 401 | || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == sleep_mode)) |
| 402 | if (!cpu && msm_rpm_waiting_for_ack()) |
| 403 | break; |
| 404 | |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 405 | if (next_wakeup_us <= 1) { |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 406 | pwr = lvl_overhead_energy; |
| 407 | } else if (next_wakeup_us <= lvl_overhead_us) { |
| 408 | pwr = lvl_overhead_energy / next_wakeup_us; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 409 | } else if ((next_wakeup_us >> 10) |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 410 | > lvl_overhead_us) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 411 | pwr = level->steady_state_power; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 412 | } else { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 413 | pwr = level->steady_state_power; |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 414 | pwr -= (lvl_overhead_us * |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 415 | level->steady_state_power) / |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 416 | next_wakeup_us; |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 417 | pwr += lvl_overhead_energy / next_wakeup_us; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 418 | } |
| 419 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 420 | if (!best_level || (best_level_pwr >= pwr)) { |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 421 | best_level = level; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 422 | best_level_pwr = pwr; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 423 | if (power) |
| 424 | *power = pwr; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 425 | if (modify_event_timer && |
| 426 | (sleep_mode != |
| 427 | MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT)) |
| 428 | time_param->modified_time_us = |
| 429 | time_param->next_event_us - |
Girish Mahadevan | e6b1dd4 | 2013-05-22 11:59:08 -0600 | [diff] [blame] | 430 | lvl_latency_us; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 431 | else |
| 432 | time_param->modified_time_us = 0; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 436 | return best_level ? &best_level->l2_cache : NULL; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 437 | } |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 438 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 439 | static struct msm_pm_sleep_ops msm_lpm_ops = { |
| 440 | .lowest_limits = msm_lpm_lowest_limits, |
| 441 | .enter_sleep = msm_lpm_enter_sleep, |
| 442 | .exit_sleep = msm_lpm_exit_sleep, |
| 443 | }; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 444 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 445 | static int msm_lpm_get_l2_cache_value(struct device_node *node, |
| 446 | char *key, uint32_t *l2_val) |
| 447 | { |
| 448 | int i; |
| 449 | struct lpm_lookup_table l2_mode_lookup[] = { |
| 450 | {MSM_SPM_L2_MODE_POWER_COLLAPSE, "l2_cache_pc"}, |
| 451 | {MSM_SPM_L2_MODE_GDHS, "l2_cache_gdhs"}, |
| 452 | {MSM_SPM_L2_MODE_RETENTION, "l2_cache_retention"}, |
| 453 | {MSM_SPM_L2_MODE_DISABLED, "l2_cache_active"} |
| 454 | }; |
| 455 | const char *l2_str; |
| 456 | int ret; |
| 457 | |
| 458 | ret = of_property_read_string(node, key, &l2_str); |
| 459 | if (!ret) { |
| 460 | ret = -EINVAL; |
| 461 | for (i = 0; i < ARRAY_SIZE(l2_mode_lookup); i++) { |
| 462 | if (!strcmp(l2_str, l2_mode_lookup[i].mode_name)) { |
| 463 | *l2_val = l2_mode_lookup[i].modes; |
| 464 | ret = 0; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | return ret; |
| 470 | } |
| 471 | |
| 472 | static int __devinit msm_lpm_levels_sysfs_add(void) |
| 473 | { |
| 474 | struct kobject *module_kobj = NULL; |
| 475 | struct kobject *low_power_kobj = NULL; |
| 476 | int rc = 0; |
| 477 | |
| 478 | module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 479 | if (!module_kobj) { |
| 480 | pr_err("%s: cannot find kobject for module %s\n", |
| 481 | __func__, KBUILD_MODNAME); |
| 482 | rc = -ENOENT; |
| 483 | goto resource_sysfs_add_exit; |
| 484 | } |
| 485 | |
| 486 | low_power_kobj = kobject_create_and_add( |
| 487 | "enable_low_power", module_kobj); |
| 488 | if (!low_power_kobj) { |
| 489 | pr_err("%s: cannot create kobject\n", __func__); |
| 490 | rc = -ENOMEM; |
| 491 | goto resource_sysfs_add_exit; |
| 492 | } |
| 493 | |
| 494 | rc = sysfs_create_group(low_power_kobj, &lpm_levels_attr_grp); |
| 495 | resource_sysfs_add_exit: |
| 496 | if (rc) { |
| 497 | if (low_power_kobj) { |
| 498 | sysfs_remove_group(low_power_kobj, |
| 499 | &lpm_levels_attr_grp); |
| 500 | kobject_del(low_power_kobj); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | return rc; |
| 505 | } |
| 506 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 507 | static int __devinit msm_lpm_levels_probe(struct platform_device *pdev) |
| 508 | { |
| 509 | struct msm_rpmrs_level *levels = NULL; |
| 510 | struct msm_rpmrs_level *level = NULL; |
| 511 | struct device_node *node = NULL; |
| 512 | char *key = NULL; |
| 513 | uint32_t val = 0; |
| 514 | int ret = 0; |
| 515 | uint32_t num_levels = 0; |
| 516 | int idx = 0; |
| 517 | |
| 518 | for_each_child_of_node(pdev->dev.of_node, node) |
| 519 | num_levels++; |
| 520 | |
| 521 | levels = kzalloc(num_levels * sizeof(struct msm_rpmrs_level), |
| 522 | GFP_KERNEL); |
| 523 | if (!levels) |
| 524 | return -ENOMEM; |
| 525 | |
| 526 | for_each_child_of_node(pdev->dev.of_node, node) { |
| 527 | level = &levels[idx++]; |
| 528 | level->available = false; |
| 529 | |
| 530 | key = "qcom,mode"; |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 531 | ret = msm_pm_get_sleep_mode_value(node, key, &val); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 532 | if (ret) |
| 533 | goto fail; |
| 534 | level->sleep_mode = val; |
| 535 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 536 | key = "qcom,l2"; |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 537 | ret = msm_lpm_get_l2_cache_value(node, key, &val); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 538 | if (ret) |
| 539 | goto fail; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 540 | level->l2_cache = val; |
Mahesh Sivasubramanian | b71ce09 | 2013-01-08 13:44:23 -0700 | [diff] [blame] | 541 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 542 | key = "qcom,latency-us"; |
| 543 | ret = of_property_read_u32(node, key, &val); |
| 544 | if (ret) |
| 545 | goto fail; |
| 546 | level->latency_us = val; |
| 547 | |
| 548 | key = "qcom,ss-power"; |
| 549 | ret = of_property_read_u32(node, key, &val); |
| 550 | if (ret) |
| 551 | goto fail; |
| 552 | level->steady_state_power = val; |
| 553 | |
| 554 | key = "qcom,energy-overhead"; |
| 555 | ret = of_property_read_u32(node, key, &val); |
| 556 | if (ret) |
| 557 | goto fail; |
| 558 | level->energy_overhead = val; |
| 559 | |
| 560 | key = "qcom,time-overhead"; |
| 561 | ret = of_property_read_u32(node, key, &val); |
| 562 | if (ret) |
| 563 | goto fail; |
| 564 | level->time_overhead_us = val; |
| 565 | |
| 566 | level->available = true; |
| 567 | } |
| 568 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 569 | node = pdev->dev.of_node; |
| 570 | key = "qcom,no-l2-saw"; |
| 571 | no_l2_saw = of_property_read_bool(node, key); |
| 572 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 573 | msm_lpm_levels = levels; |
| 574 | msm_lpm_level_count = idx; |
| 575 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 576 | if (num_online_cpus() == 1) |
| 577 | allowed_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 578 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 579 | /* Do the following two steps only if L2 SAW is present */ |
| 580 | if (!no_l2_saw) { |
| 581 | key = "qcom,default-l2-state"; |
| 582 | if (msm_lpm_get_l2_cache_value(node, key, &default_l2_mode)) |
| 583 | goto fail; |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 584 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 585 | if (msm_lpm_levels_sysfs_add()) |
| 586 | goto fail; |
| 587 | register_hotcpu_notifier(&msm_lpm_cpu_nblk); |
| 588 | msm_pm_set_l2_flush_flag(0); |
| 589 | } else { |
| 590 | msm_pm_set_l2_flush_flag(1); |
| 591 | default_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
| 592 | } |
| 593 | |
| 594 | msm_lpm_level_update(); |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 595 | msm_pm_set_sleep_ops(&msm_lpm_ops); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 596 | return 0; |
| 597 | fail: |
| 598 | pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key); |
| 599 | kfree(levels); |
| 600 | return -EFAULT; |
| 601 | } |
| 602 | |
| 603 | static struct of_device_id msm_lpm_levels_match_table[] = { |
| 604 | {.compatible = "qcom,lpm-levels"}, |
| 605 | {}, |
| 606 | }; |
| 607 | |
| 608 | static struct platform_driver msm_lpm_levels_driver = { |
| 609 | .probe = msm_lpm_levels_probe, |
| 610 | .driver = { |
| 611 | .name = "lpm-levels", |
| 612 | .owner = THIS_MODULE, |
| 613 | .of_match_table = msm_lpm_levels_match_table, |
| 614 | }, |
| 615 | }; |
| 616 | |
| 617 | static int __init msm_lpm_levels_module_init(void) |
| 618 | { |
| 619 | return platform_driver_register(&msm_lpm_levels_driver); |
| 620 | } |
| 621 | late_initcall(msm_lpm_levels_module_init); |