Mahesh Sivasubramanian | 208b0d1 | 2014-01-08 12:43:19 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2012-2014, 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> |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 22 | #include <linux/hrtimer.h> |
| 23 | #include <linux/ktime.h> |
| 24 | #include <linux/tick.h> |
| 25 | #include <linux/suspend.h> |
| 26 | #include <linux/pm_qos.h> |
| 27 | #include <linux/of_platform.h> |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 28 | #include <mach/mpm.h> |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 29 | #include <mach/cpuidle.h> |
| 30 | #include <mach/event_timer.h> |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 31 | #include "pm.h" |
Mahesh Sivasubramanian | 11dad77 | 2012-07-13 14:00:01 -0600 | [diff] [blame] | 32 | #include "rpm-notifier.h" |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 33 | #include "spm.h" |
| 34 | #include "idle.h" |
Mahesh Sivasubramanian | fae923f | 2012-10-08 16:22:51 -0600 | [diff] [blame] | 35 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 36 | #define SCLK_HZ (32768) |
| 37 | |
Mahesh Sivasubramanian | fae923f | 2012-10-08 16:22:51 -0600 | [diff] [blame] | 38 | enum { |
| 39 | MSM_LPM_LVL_DBG_SUSPEND_LIMITS = BIT(0), |
| 40 | MSM_LPM_LVL_DBG_IDLE_LIMITS = BIT(1), |
| 41 | }; |
| 42 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 43 | struct power_params { |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 44 | uint32_t latency_us; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 45 | uint32_t ss_power; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 46 | uint32_t energy_overhead; |
| 47 | uint32_t time_overhead_us; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 48 | uint32_t target_residency_us; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 49 | }; |
| 50 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 51 | struct lpm_cpu_level { |
| 52 | const char *name; |
| 53 | enum msm_pm_sleep_mode mode; |
| 54 | struct power_params pwr; |
| 55 | bool use_bc_timer; |
| 56 | bool sync; |
| 57 | }; |
| 58 | |
| 59 | struct lpm_system_level { |
| 60 | const char *name; |
| 61 | uint32_t l2_mode; |
| 62 | struct power_params pwr; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 63 | enum msm_pm_sleep_mode min_cpu_mode; |
| 64 | int num_cpu_votes; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 65 | bool notify_rpm; |
| 66 | bool available; |
| 67 | bool sync; |
| 68 | }; |
| 69 | |
| 70 | struct lpm_system_state { |
| 71 | struct lpm_cpu_level *cpu_level; |
| 72 | int num_cpu_levels; |
| 73 | struct lpm_system_level *system_level; |
| 74 | int num_system_levels; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 75 | enum msm_pm_sleep_mode sync_cpu_mode; |
| 76 | int last_entered_cluster_index; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 77 | bool allow_synched_levels; |
| 78 | bool no_l2_saw; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 79 | struct spinlock sync_lock; |
| 80 | int num_cores_in_sync; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static struct lpm_system_state sys_state; |
| 84 | static bool suspend_in_progress; |
| 85 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 86 | struct lpm_lookup_table { |
| 87 | uint32_t modes; |
| 88 | const char *mode_name; |
| 89 | }; |
| 90 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 91 | static void lpm_system_level_update(void); |
| 92 | static void setup_broadcast_timer(void *arg); |
| 93 | static int lpm_cpu_callback(struct notifier_block *cpu_nb, |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 94 | unsigned long action, void *hcpu); |
| 95 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 96 | static struct notifier_block __refdata lpm_cpu_nblk = { |
| 97 | .notifier_call = lpm_cpu_callback, |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static uint32_t allowed_l2_mode; |
| 101 | static uint32_t sysfs_dbg_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
| 102 | static uint32_t default_l2_mode; |
| 103 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 104 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 105 | static ssize_t lpm_levels_attr_show( |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 106 | struct kobject *kobj, struct kobj_attribute *attr, char *buf); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 107 | static ssize_t lpm_levels_attr_store(struct kobject *kobj, |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 108 | struct kobj_attribute *attr, const char *buf, size_t count); |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 109 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 110 | |
| 111 | static int lpm_lvl_dbg_msk; |
Mahesh Sivasubramanian | fae923f | 2012-10-08 16:22:51 -0600 | [diff] [blame] | 112 | |
| 113 | module_param_named( |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 114 | debug_mask, lpm_lvl_dbg_msk, int, S_IRUGO | S_IWUSR | S_IWGRP |
Mahesh Sivasubramanian | fae923f | 2012-10-08 16:22:51 -0600 | [diff] [blame] | 115 | ); |
| 116 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 117 | static bool menu_select; |
| 118 | module_param_named( |
| 119 | menu_select, menu_select, bool, S_IRUGO | S_IWUSR | S_IWGRP |
| 120 | ); |
| 121 | |
| 122 | static int msm_pm_sleep_time_override; |
| 123 | module_param_named(sleep_time_override, |
| 124 | msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 125 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 126 | static int num_powered_cores; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 127 | static struct hrtimer lpm_hrtimer; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 128 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 129 | static struct kobj_attribute lpm_l2_kattr = __ATTR(l2, S_IRUGO|S_IWUSR,\ |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 130 | lpm_levels_attr_show, lpm_levels_attr_store); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 131 | |
| 132 | static struct attribute *lpm_levels_attr[] = { |
| 133 | &lpm_l2_kattr.attr, |
| 134 | NULL, |
| 135 | }; |
| 136 | |
| 137 | static struct attribute_group lpm_levels_attr_grp = { |
| 138 | .attrs = lpm_levels_attr, |
| 139 | }; |
| 140 | |
| 141 | /* SYSFS */ |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 142 | static ssize_t lpm_levels_attr_show( |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 143 | struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 144 | { |
| 145 | struct kernel_param kp; |
| 146 | int rc; |
| 147 | |
| 148 | kp.arg = &sysfs_dbg_l2_mode; |
| 149 | |
| 150 | rc = param_get_uint(buf, &kp); |
| 151 | |
| 152 | if (rc > 0) { |
| 153 | strlcat(buf, "\n", PAGE_SIZE); |
| 154 | rc++; |
| 155 | } |
| 156 | |
| 157 | return rc; |
| 158 | } |
| 159 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 160 | static ssize_t lpm_levels_attr_store(struct kobject *kobj, |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 161 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 162 | { |
| 163 | struct kernel_param kp; |
| 164 | unsigned int temp; |
| 165 | int rc; |
| 166 | |
| 167 | kp.arg = &temp; |
| 168 | rc = param_set_uint(buf, &kp); |
| 169 | if (rc) |
| 170 | return rc; |
| 171 | |
| 172 | sysfs_dbg_l2_mode = temp; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 173 | lpm_system_level_update(); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 174 | |
| 175 | return count; |
| 176 | } |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 177 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 178 | static int msm_pm_get_sleep_mode_value(const char *mode_name) |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 179 | { |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 180 | struct lpm_lookup_table pm_sm_lookup[] = { |
| 181 | {MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT, |
| 182 | "wfi"}, |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 183 | {MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE, |
| 184 | "standalone_pc"}, |
| 185 | {MSM_PM_SLEEP_MODE_POWER_COLLAPSE, |
| 186 | "pc"}, |
| 187 | {MSM_PM_SLEEP_MODE_RETENTION, |
| 188 | "retention"}, |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 189 | }; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 190 | int i; |
| 191 | int ret = -EINVAL; |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 192 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 193 | for (i = 0; i < ARRAY_SIZE(pm_sm_lookup); i++) { |
| 194 | if (!strcmp(mode_name, pm_sm_lookup[i].mode_name)) { |
| 195 | ret = pm_sm_lookup[i].modes; |
| 196 | break; |
Archana Sathyakumar | e6a3510 | 2013-01-31 16:18:49 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | return ret; |
| 200 | } |
| 201 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 202 | static int lpm_set_l2_mode(struct lpm_system_state *system_state, |
| 203 | int sleep_mode) |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 204 | { |
| 205 | int lpm = sleep_mode; |
| 206 | int rc = 0; |
| 207 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 208 | if (system_state->no_l2_saw) |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 209 | goto bail_set_l2_mode; |
| 210 | |
| 211 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_ON); |
| 212 | |
| 213 | switch (sleep_mode) { |
| 214 | case MSM_SPM_L2_MODE_POWER_COLLAPSE: |
| 215 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_OFF); |
| 216 | break; |
| 217 | case MSM_SPM_L2_MODE_GDHS: |
| 218 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_GDHS); |
| 219 | break; |
Murali Nalajala | 82123b0 | 2013-10-04 16:00:19 +0530 | [diff] [blame] | 220 | case MSM_SPM_L2_MODE_PC_NO_RPM: |
| 221 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_OFF); |
| 222 | break; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 223 | case MSM_SPM_L2_MODE_RETENTION: |
| 224 | case MSM_SPM_L2_MODE_DISABLED: |
| 225 | break; |
| 226 | default: |
| 227 | lpm = MSM_SPM_L2_MODE_DISABLED; |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | rc = msm_spm_l2_set_low_power_mode(lpm, true); |
| 232 | |
| 233 | if (rc) { |
| 234 | if (rc == -ENXIO) |
| 235 | WARN_ON_ONCE(1); |
| 236 | else |
| 237 | pr_err("%s: Failed to set L2 low power mode %d, ERR %d", |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 238 | __func__, lpm, rc); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 239 | } |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 240 | bail_set_l2_mode: |
| 241 | return rc; |
| 242 | } |
| 243 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 244 | static void lpm_system_level_update(void) |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 245 | { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 246 | int i; |
| 247 | struct lpm_system_level *l = NULL; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 248 | uint32_t max_l2_mode; |
| 249 | static DEFINE_MUTEX(lpm_lock); |
| 250 | |
| 251 | mutex_lock(&lpm_lock); |
| 252 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 253 | if (num_powered_cores == 1) |
| 254 | allowed_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
| 255 | else if (sys_state.allow_synched_levels) |
| 256 | allowed_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
| 257 | else |
| 258 | allowed_l2_mode = default_l2_mode; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 259 | max_l2_mode = min(allowed_l2_mode, sysfs_dbg_l2_mode); |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 260 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 261 | for (i = 0; i < sys_state.num_system_levels; i++) { |
| 262 | l = &sys_state.system_level[i]; |
| 263 | l->available = !(l->l2_mode > max_l2_mode); |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 264 | } |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 265 | mutex_unlock(&lpm_lock); |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 266 | } |
| 267 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 268 | static int lpm_system_mode_select( |
| 269 | struct lpm_system_state *system_state, |
| 270 | uint32_t sleep_us, bool from_idle) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 271 | { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 272 | int best_level = -1; |
| 273 | int i; |
Murali Nalajala | 15090a2 | 2014-01-22 15:09:09 +0530 | [diff] [blame] | 274 | uint32_t best_level_pwr = ~0U; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 275 | uint32_t pwr; |
| 276 | uint32_t latency_us = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
| 277 | |
| 278 | if (!system_state->system_level) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | for (i = 0; i < system_state->num_system_levels; i++) { |
| 282 | struct lpm_system_level *system_level = |
| 283 | &system_state->system_level[i]; |
| 284 | struct power_params *pwr_param = &system_level->pwr; |
| 285 | |
| 286 | if (!system_level->available) |
| 287 | continue; |
| 288 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 289 | if (system_level->sync && |
| 290 | system_level->num_cpu_votes != num_powered_cores) |
| 291 | continue; |
| 292 | |
Archana Sathyakumar | 41e303a | 2013-12-13 17:37:55 -0700 | [diff] [blame] | 293 | if (latency_us < pwr_param->latency_us && from_idle) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 294 | continue; |
| 295 | |
| 296 | if (sleep_us < pwr_param->time_overhead_us) |
| 297 | continue; |
| 298 | |
| 299 | /* |
| 300 | * After the suspend prepare notifications its possible |
| 301 | * for the CPU to enter a system sleep mode. But MPM would have |
| 302 | * already requested a XO clock based on the wakeup irqs. To |
| 303 | * prevent suspend votes from being overriden by idle irqs, MPM |
| 304 | * doesn't send an updated MPM vote after suspend_prepare |
| 305 | * callback. |
| 306 | * To ensure that XO sleep vote isn't used if and when the |
| 307 | * device enters idle PC after suspend prepare callback, |
| 308 | * disallow any low power modes that notifies RPM after suspend |
| 309 | * prepare function is called |
| 310 | */ |
| 311 | if (suspend_in_progress && system_level->notify_rpm && |
| 312 | from_idle) |
| 313 | continue; |
| 314 | |
| 315 | if ((sleep_us >> 10) > pwr_param->time_overhead_us) { |
| 316 | pwr = pwr_param->ss_power; |
| 317 | } else { |
| 318 | pwr = pwr_param->ss_power; |
| 319 | pwr -= (pwr_param->time_overhead_us |
| 320 | * pwr_param->ss_power) / sleep_us; |
| 321 | pwr += pwr_param->energy_overhead / sleep_us; |
| 322 | } |
| 323 | |
| 324 | if (best_level_pwr >= pwr) { |
| 325 | best_level = i; |
| 326 | best_level_pwr = pwr; |
| 327 | } |
| 328 | } |
| 329 | return best_level; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | static void lpm_system_prepare(struct lpm_system_state *system_state, |
| 333 | int index, bool from_idle) |
| 334 | { |
| 335 | struct lpm_system_level *lvl; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 336 | struct clock_event_device *bc = tick_get_broadcast_device()->evtdev; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 337 | uint32_t sclk; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 338 | int64_t us = (~0ULL); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 339 | int dbg_mask; |
| 340 | int ret; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 341 | const struct cpumask *nextcpu; |
Girish Mahadevan | 40abbe1 | 2012-04-25 14:58:13 -0600 | [diff] [blame] | 342 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 343 | spin_lock(&system_state->sync_lock); |
Mahesh Sivasubramanian | 208b0d1 | 2014-01-08 12:43:19 -0700 | [diff] [blame] | 344 | if (index < 0 || |
| 345 | num_powered_cores != system_state->num_cores_in_sync) { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 346 | spin_unlock(&system_state->sync_lock); |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | if (from_idle) { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 351 | dbg_mask = lpm_lvl_dbg_msk & MSM_LPM_LVL_DBG_IDLE_LIMITS; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 352 | us = ktime_to_us(ktime_sub(bc->next_event, ktime_get())); |
| 353 | nextcpu = bc->cpumask; |
| 354 | } else { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 355 | dbg_mask = lpm_lvl_dbg_msk & MSM_LPM_LVL_DBG_SUSPEND_LIMITS; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 356 | nextcpu = cpumask_of(smp_processor_id()); |
| 357 | } |
Mahesh Sivasubramanian | 9b0f412 | 2013-02-15 14:20:52 -0700 | [diff] [blame] | 358 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 359 | lvl = &system_state->system_level[index]; |
Mahesh Sivasubramanian | 9b0f412 | 2013-02-15 14:20:52 -0700 | [diff] [blame] | 360 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 361 | ret = lpm_set_l2_mode(system_state, lvl->l2_mode); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 362 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 363 | if (ret && ret != -ENXIO) { |
| 364 | pr_warn("%s(): Cannot set L2 Mode %d, ret:%d\n", |
| 365 | __func__, lvl->l2_mode, ret); |
| 366 | goto bail_system_sleep; |
| 367 | } |
| 368 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 369 | if (lvl->notify_rpm) { |
| 370 | ret = msm_rpm_enter_sleep(dbg_mask, nextcpu); |
| 371 | if (ret) { |
| 372 | pr_err("rpm_enter_sleep() failed with rc = %d\n", ret); |
| 373 | goto bail_system_sleep; |
| 374 | } |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 375 | |
Mahesh Sivasubramanian | fc2eb5b | 2013-11-21 18:30:06 -0700 | [diff] [blame] | 376 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 377 | if (!from_idle) |
| 378 | us = USEC_PER_SEC * msm_pm_sleep_time_override; |
| 379 | |
| 380 | do_div(us, USEC_PER_SEC/SCLK_HZ); |
| 381 | sclk = (uint32_t)us; |
| 382 | msm_mpm_enter_sleep(sclk, from_idle, nextcpu); |
Mahesh Sivasubramanian | 0558d4b | 2012-10-12 18:05:28 -0600 | [diff] [blame] | 383 | } |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 384 | system_state->last_entered_cluster_index = index; |
| 385 | spin_unlock(&system_state->sync_lock); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 386 | return; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 387 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 388 | bail_system_sleep: |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 389 | if (default_l2_mode != system_state->system_level[index].l2_mode) |
| 390 | lpm_set_l2_mode(system_state, default_l2_mode); |
| 391 | spin_unlock(&system_state->sync_lock); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | static void lpm_system_unprepare(struct lpm_system_state *system_state, |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 395 | int cpu_index, bool from_idle) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 396 | { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 397 | int index, i; |
| 398 | struct lpm_cpu_level *cpu_level = &system_state->cpu_level[cpu_index]; |
| 399 | bool first_core_up; |
| 400 | |
| 401 | if (cpu_level->mode < system_state->sync_cpu_mode) |
| 402 | return; |
| 403 | |
| 404 | spin_lock(&system_state->sync_lock); |
| 405 | |
| 406 | first_core_up = (system_state->num_cores_in_sync == num_powered_cores); |
| 407 | |
| 408 | system_state->num_cores_in_sync--; |
| 409 | |
| 410 | if (!system_state->system_level) |
| 411 | goto unlock_and_return; |
| 412 | |
| 413 | index = system_state->last_entered_cluster_index; |
| 414 | |
| 415 | for (i = 0; i < system_state->num_system_levels; i++) { |
| 416 | struct lpm_system_level *system_lvl |
| 417 | = &system_state->system_level[i]; |
| 418 | if (cpu_level->mode >= system_lvl->min_cpu_mode) |
| 419 | system_lvl->num_cpu_votes--; |
| 420 | } |
| 421 | |
Mahesh Sivasubramanian | 208b0d1 | 2014-01-08 12:43:19 -0700 | [diff] [blame] | 422 | if (!first_core_up || index < 0) |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 423 | goto unlock_and_return; |
| 424 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 425 | if (default_l2_mode != system_state->system_level[index].l2_mode) |
| 426 | lpm_set_l2_mode(system_state, default_l2_mode); |
| 427 | |
| 428 | if (system_state->system_level[index].notify_rpm) { |
| 429 | msm_rpm_exit_sleep(); |
| 430 | msm_mpm_exit_sleep(from_idle); |
| 431 | } |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 432 | unlock_and_return: |
Mahesh Sivasubramanian | 208b0d1 | 2014-01-08 12:43:19 -0700 | [diff] [blame] | 433 | system_state->last_entered_cluster_index = -1; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 434 | spin_unlock(&system_state->sync_lock); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 435 | } |
| 436 | |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 437 | s32 msm_cpuidle_get_deep_idle_latency(void) |
| 438 | { |
| 439 | int i; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 440 | struct lpm_cpu_level *level = sys_state.cpu_level; |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 441 | |
| 442 | if (!level) |
| 443 | return 0; |
| 444 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 445 | for (i = 0; i < sys_state.num_cpu_levels; i++, level++) { |
| 446 | if (level->mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) |
| 447 | break; |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 448 | } |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 449 | |
| 450 | if (i == sys_state.num_cpu_levels) |
| 451 | return 0; |
| 452 | else |
| 453 | return level->pwr.latency_us; |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 454 | } |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 455 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 456 | static int lpm_cpu_callback(struct notifier_block *cpu_nb, |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 457 | unsigned long action, void *hcpu) |
Mahesh Sivasubramanian | f1ddf04 | 2013-01-08 14:03:32 -0700 | [diff] [blame] | 458 | { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 459 | switch (action & ~CPU_TASKS_FROZEN) { |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 460 | case CPU_UP_PREPARE: |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 461 | ++num_powered_cores; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 462 | lpm_system_level_update(); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 463 | break; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 464 | case CPU_DEAD: |
| 465 | case CPU_UP_CANCELED: |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 466 | num_powered_cores = num_online_cpus(); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 467 | lpm_system_level_update(); |
| 468 | break; |
| 469 | case CPU_ONLINE: |
| 470 | smp_call_function_single((unsigned long)hcpu, |
| 471 | setup_broadcast_timer, (void *)true, 1); |
| 472 | break; |
| 473 | default: |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 474 | break; |
| 475 | } |
| 476 | return NOTIFY_OK; |
Mahesh Sivasubramanian | f1ddf04 | 2013-01-08 14:03:32 -0700 | [diff] [blame] | 477 | } |
Stephen Boyd | 3f4bac2 | 2012-05-30 10:03:13 -0700 | [diff] [blame] | 478 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 479 | static enum hrtimer_restart lpm_hrtimer_cb(struct hrtimer *h) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 480 | { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 481 | return HRTIMER_NORESTART; |
| 482 | } |
| 483 | |
| 484 | static void msm_pm_set_timer(uint32_t modified_time_us) |
| 485 | { |
| 486 | u64 modified_time_ns = modified_time_us * NSEC_PER_USEC; |
| 487 | ktime_t modified_ktime = ns_to_ktime(modified_time_ns); |
| 488 | lpm_hrtimer.function = lpm_hrtimer_cb; |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 489 | hrtimer_start(&lpm_hrtimer, modified_ktime, HRTIMER_MODE_REL_PINNED); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 490 | } |
| 491 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 492 | static noinline int lpm_cpu_power_select(struct cpuidle_device *dev, int *index) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 493 | { |
| 494 | int best_level = -1; |
Murali Nalajala | 15090a2 | 2014-01-22 15:09:09 +0530 | [diff] [blame] | 495 | uint32_t best_level_pwr = ~0U; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 496 | uint32_t latency_us = pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
| 497 | uint32_t sleep_us = |
| 498 | (uint32_t)(ktime_to_us(tick_nohz_get_sleep_length())); |
| 499 | uint32_t modified_time_us = 0; |
| 500 | uint32_t next_event_us = 0; |
| 501 | uint32_t power; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 502 | int i; |
Mahesh Sivasubramanian | f1ddf04 | 2013-01-08 14:03:32 -0700 | [diff] [blame] | 503 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 504 | if (!sys_state.cpu_level) |
| 505 | return -EINVAL; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 506 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 507 | if (!dev->cpu) |
| 508 | next_event_us = (uint32_t)(ktime_to_us(get_next_event_time())); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 509 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 510 | for (i = 0; i < sys_state.num_cpu_levels; i++) { |
| 511 | struct lpm_cpu_level *level = &sys_state.cpu_level[i]; |
| 512 | struct power_params *pwr = &level->pwr; |
| 513 | uint32_t next_wakeup_us = sleep_us; |
| 514 | enum msm_pm_sleep_mode mode = level->mode; |
| 515 | bool allow; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 516 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 517 | if (level->sync && num_online_cpus() > 1 |
| 518 | && !sys_state.allow_synched_levels) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 519 | continue; |
| 520 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 521 | allow = msm_cpu_pm_check_mode(dev->cpu, mode, true); |
| 522 | |
| 523 | if (!allow) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 524 | continue; |
| 525 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 526 | if (latency_us < pwr->latency_us) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 527 | continue; |
| 528 | |
Mahesh Sivasubramanian | ea1c594 | 2014-01-31 13:57:00 -0700 | [diff] [blame] | 529 | if (next_event_us) { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 530 | if (next_event_us < pwr->latency_us) |
| 531 | continue; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 532 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 533 | if (((next_event_us - pwr->latency_us) < sleep_us) |
| 534 | || (next_event_us < sleep_us)) { |
| 535 | next_wakeup_us = next_event_us |
| 536 | - pwr->latency_us; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 537 | } |
Mahesh Sivasubramanian | ea1c594 | 2014-01-31 13:57:00 -0700 | [diff] [blame] | 538 | } |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 539 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 540 | if (next_wakeup_us <= pwr->time_overhead_us) |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 541 | continue; |
| 542 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 543 | if ((MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE == mode) |
| 544 | || (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode)) |
| 545 | if (!dev->cpu && msm_rpm_waiting_for_ack()) |
Mahesh Sivasubramanian | 9063a29 | 2012-11-09 09:15:30 -0700 | [diff] [blame] | 546 | break; |
| 547 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 548 | if ((next_wakeup_us >> 10) > pwr->latency_us) { |
| 549 | power = pwr->ss_power; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 550 | } else { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 551 | power = pwr->ss_power; |
| 552 | power -= (pwr->latency_us * pwr->ss_power) |
| 553 | / next_wakeup_us; |
| 554 | power += pwr->energy_overhead / next_wakeup_us; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 555 | } |
| 556 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 557 | if (best_level_pwr >= power) { |
| 558 | best_level = i; |
| 559 | best_level_pwr = power; |
| 560 | if (next_event_us < sleep_us && |
| 561 | (mode != MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT)) |
| 562 | modified_time_us = next_event_us |
| 563 | - pwr->latency_us; |
Girish Mahadevan | a351c0b | 2013-02-22 11:06:00 -0700 | [diff] [blame] | 564 | else |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 565 | modified_time_us = 0; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 569 | if (modified_time_us && !dev->cpu) |
| 570 | msm_pm_set_timer(modified_time_us); |
| 571 | |
| 572 | return best_level; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 573 | } |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 574 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 575 | static int lpm_get_l2_cache_value(const char *l2_str) |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 576 | { |
| 577 | int i; |
| 578 | struct lpm_lookup_table l2_mode_lookup[] = { |
| 579 | {MSM_SPM_L2_MODE_POWER_COLLAPSE, "l2_cache_pc"}, |
Murali Nalajala | 82123b0 | 2013-10-04 16:00:19 +0530 | [diff] [blame] | 580 | {MSM_SPM_L2_MODE_PC_NO_RPM, "l2_cache_pc_no_rpm"}, |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 581 | {MSM_SPM_L2_MODE_GDHS, "l2_cache_gdhs"}, |
| 582 | {MSM_SPM_L2_MODE_RETENTION, "l2_cache_retention"}, |
| 583 | {MSM_SPM_L2_MODE_DISABLED, "l2_cache_active"} |
| 584 | }; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 585 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 586 | for (i = 0; i < ARRAY_SIZE(l2_mode_lookup); i++) |
| 587 | if (!strcmp(l2_str, l2_mode_lookup[i].mode_name)) |
| 588 | return l2_mode_lookup[i].modes; |
| 589 | return -EINVAL; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 590 | } |
| 591 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 592 | static int lpm_levels_sysfs_add(void) |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 593 | { |
| 594 | struct kobject *module_kobj = NULL; |
| 595 | struct kobject *low_power_kobj = NULL; |
| 596 | int rc = 0; |
| 597 | |
| 598 | module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 599 | if (!module_kobj) { |
| 600 | pr_err("%s: cannot find kobject for module %s\n", |
| 601 | __func__, KBUILD_MODNAME); |
| 602 | rc = -ENOENT; |
| 603 | goto resource_sysfs_add_exit; |
| 604 | } |
| 605 | |
| 606 | low_power_kobj = kobject_create_and_add( |
| 607 | "enable_low_power", module_kobj); |
| 608 | if (!low_power_kobj) { |
| 609 | pr_err("%s: cannot create kobject\n", __func__); |
| 610 | rc = -ENOMEM; |
| 611 | goto resource_sysfs_add_exit; |
| 612 | } |
| 613 | |
| 614 | rc = sysfs_create_group(low_power_kobj, &lpm_levels_attr_grp); |
| 615 | resource_sysfs_add_exit: |
| 616 | if (rc) { |
| 617 | if (low_power_kobj) { |
| 618 | sysfs_remove_group(low_power_kobj, |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 619 | &lpm_levels_attr_grp); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 620 | kobject_del(low_power_kobj); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | return rc; |
| 625 | } |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 626 | static int lpm_cpu_menu_select(struct cpuidle_device *dev, int *index) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 627 | { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 628 | int j; |
| 629 | |
| 630 | for (; *index >= 0; (*index)--) { |
| 631 | int mode = 0; |
| 632 | bool allow = false; |
| 633 | |
| 634 | allow = msm_cpu_pm_check_mode(dev->cpu, mode, true); |
| 635 | |
| 636 | if (!allow) |
| 637 | continue; |
| 638 | |
| 639 | for (j = sys_state.num_cpu_levels; j >= 0; j--) { |
| 640 | struct lpm_cpu_level *l = &sys_state.cpu_level[j]; |
| 641 | if (mode == l->mode) |
| 642 | return j; |
| 643 | } |
| 644 | } |
| 645 | return -EPERM; |
| 646 | } |
| 647 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 648 | static inline void lpm_cpu_prepare(struct lpm_system_state *system_state, |
| 649 | int cpu_index, bool from_idle) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 650 | { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 651 | struct lpm_cpu_level *cpu_level = &system_state->cpu_level[cpu_index]; |
| 652 | unsigned int cpu = smp_processor_id(); |
| 653 | |
| 654 | /* Use broadcast timer for aggregating sleep mode within a cluster. |
| 655 | * A broadcast timer could be used because of harware restriction or |
| 656 | * to ensure that we BC timer is used incase a cpu mode could trigger |
| 657 | * a cluster level sleep |
| 658 | */ |
| 659 | if (from_idle && (cpu_level->use_bc_timer || |
| 660 | (cpu_level->mode >= system_state->sync_cpu_mode))) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 661 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); |
| 662 | } |
| 663 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 664 | static inline void lpm_cpu_unprepare(struct lpm_system_state *system_state, |
| 665 | int cpu_index, bool from_idle) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 666 | { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 667 | struct lpm_cpu_level *cpu_level = &system_state->cpu_level[cpu_index]; |
| 668 | unsigned int cpu = smp_processor_id(); |
| 669 | |
| 670 | if (from_idle && (cpu_level->use_bc_timer || |
| 671 | (cpu_level->mode >= system_state->sync_cpu_mode))) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 672 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu); |
| 673 | } |
| 674 | |
| 675 | static int lpm_system_select(struct lpm_system_state *system_state, |
| 676 | int cpu_index, bool from_idle) |
| 677 | { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 678 | uint64_t us = (~0ULL); |
| 679 | struct clock_event_device *ed; |
| 680 | struct lpm_cpu_level *cpu_level = &system_state->cpu_level[cpu_index]; |
| 681 | int i; |
| 682 | bool last_core_down; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 683 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 684 | if (cpu_level->mode < system_state->sync_cpu_mode) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 685 | return -EINVAL; |
| 686 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 687 | spin_lock(&system_state->sync_lock); |
| 688 | |
| 689 | last_core_down = |
| 690 | (++system_state->num_cores_in_sync == num_powered_cores); |
| 691 | |
| 692 | if (!system_state->system_level) { |
| 693 | spin_unlock(&system_state->sync_lock); |
| 694 | return -EINVAL; |
| 695 | } |
| 696 | |
| 697 | for (i = 0; i < system_state->num_system_levels; i++) { |
| 698 | struct lpm_system_level *system_lvl = |
| 699 | &system_state->system_level[i]; |
| 700 | if (cpu_level->mode >= system_lvl->min_cpu_mode) |
| 701 | system_lvl->num_cpu_votes++; |
| 702 | } |
| 703 | spin_unlock(&system_state->sync_lock); |
| 704 | |
| 705 | if (!last_core_down) |
| 706 | return -EBUSY; |
| 707 | |
| 708 | ed = tick_get_broadcast_device()->evtdev; |
| 709 | if (!ed) |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 710 | return -EINVAL; |
| 711 | |
| 712 | if (from_idle) |
| 713 | us = ktime_to_us(ktime_sub(ed->next_event, ktime_get())); |
| 714 | else |
| 715 | us = (~0ULL); |
| 716 | |
| 717 | return lpm_system_mode_select(system_state, (uint32_t)(us), from_idle); |
| 718 | } |
| 719 | |
| 720 | static void lpm_enter_low_power(struct lpm_system_state *system_state, |
| 721 | int cpu_index, bool from_idle) |
| 722 | { |
| 723 | int idx; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 724 | struct lpm_cpu_level *cpu_level = &system_state->cpu_level[cpu_index]; |
| 725 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 726 | lpm_cpu_prepare(system_state, cpu_index, from_idle); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 727 | |
| 728 | idx = lpm_system_select(system_state, cpu_index, from_idle); |
| 729 | |
Mahesh Sivasubramanian | 208b0d1 | 2014-01-08 12:43:19 -0700 | [diff] [blame] | 730 | lpm_system_prepare(system_state, idx, from_idle); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 731 | |
| 732 | msm_cpu_pm_enter_sleep(cpu_level->mode, from_idle); |
| 733 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 734 | lpm_system_unprepare(system_state, cpu_index, from_idle); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 735 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 736 | lpm_cpu_unprepare(system_state, cpu_index, from_idle); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static int lpm_cpuidle_enter(struct cpuidle_device *dev, |
| 740 | struct cpuidle_driver *drv, int index) |
| 741 | { |
| 742 | int64_t time = ktime_to_ns(ktime_get()); |
| 743 | int idx; |
| 744 | |
| 745 | idx = menu_select ? lpm_cpu_menu_select(dev, &index) : |
| 746 | lpm_cpu_power_select(dev, &index); |
| 747 | if (idx < 0) { |
| 748 | local_irq_enable(); |
| 749 | return -EPERM; |
| 750 | } |
| 751 | |
| 752 | lpm_enter_low_power(&sys_state, idx, true); |
| 753 | |
| 754 | time = ktime_to_ns(ktime_get()) - time; |
| 755 | do_div(time, 1000); |
| 756 | dev->last_residency = (int)time; |
| 757 | local_irq_enable(); |
Anil kumar mamidala | d6962bd | 2014-02-06 14:27:50 +0530 | [diff] [blame] | 758 | return idx; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | static int lpm_suspend_enter(suspend_state_t state) |
| 762 | { |
| 763 | int i; |
| 764 | |
| 765 | for (i = sys_state.num_cpu_levels - 1; i >= 0; i--) { |
| 766 | bool allow = msm_cpu_pm_check_mode(smp_processor_id(), |
| 767 | sys_state.cpu_level[i].mode, false); |
| 768 | if (allow) |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | if (i < 0) |
| 773 | return -EINVAL; |
| 774 | |
| 775 | lpm_enter_low_power(&sys_state, i, false); |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | static int lpm_suspend_prepare(void) |
| 781 | { |
| 782 | suspend_in_progress = true; |
| 783 | msm_mpm_suspend_prepare(); |
| 784 | return 0; |
| 785 | } |
| 786 | |
| 787 | static void lpm_suspend_wake(void) |
| 788 | { |
| 789 | msm_mpm_suspend_wake(); |
| 790 | suspend_in_progress = false; |
| 791 | } |
| 792 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 793 | static struct platform_device lpm_dev = { |
| 794 | .name = "msm_pm", |
| 795 | .id = -1, |
| 796 | }; |
| 797 | |
| 798 | static const struct platform_suspend_ops lpm_suspend_ops = { |
| 799 | .enter = lpm_suspend_enter, |
| 800 | .valid = suspend_valid_only_mem, |
| 801 | .prepare_late = lpm_suspend_prepare, |
| 802 | .wake = lpm_suspend_wake, |
| 803 | }; |
| 804 | |
| 805 | static void setup_broadcast_timer(void *arg) |
| 806 | { |
| 807 | unsigned long reason = (unsigned long)arg; |
| 808 | int cpu = smp_processor_id(); |
| 809 | |
| 810 | reason = reason ? |
| 811 | CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF; |
| 812 | |
| 813 | clockevents_notify(reason, &cpu); |
| 814 | } |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 815 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 816 | static struct cpuidle_driver msm_cpuidle_driver = { |
| 817 | .name = "msm_idle", |
| 818 | .owner = THIS_MODULE, |
| 819 | }; |
| 820 | |
| 821 | static void lpm_cpuidle_init(void) |
| 822 | { |
| 823 | int i = 0; |
| 824 | int state_count = 0; |
| 825 | |
| 826 | if (!sys_state.cpu_level) |
| 827 | return; |
| 828 | BUG_ON(sys_state.num_cpu_levels > CPUIDLE_STATE_MAX); |
| 829 | |
| 830 | for (i = 0; i < sys_state.num_cpu_levels; i++) { |
| 831 | struct cpuidle_state *st = &msm_cpuidle_driver.states[i]; |
| 832 | struct lpm_cpu_level *cpu_level = &sys_state.cpu_level[i]; |
| 833 | snprintf(st->name, CPUIDLE_NAME_LEN, "C%u\n", i); |
| 834 | snprintf(st->desc, CPUIDLE_DESC_LEN, cpu_level->name); |
| 835 | st->flags = 0; |
| 836 | st->exit_latency = cpu_level->pwr.latency_us; |
| 837 | st->power_usage = cpu_level->pwr.ss_power; |
| 838 | st->target_residency = 0; |
| 839 | st->enter = lpm_cpuidle_enter; |
| 840 | state_count++; |
| 841 | } |
| 842 | msm_cpuidle_driver.state_count = state_count; |
| 843 | msm_cpuidle_driver.safe_state_index = 0; |
| 844 | |
| 845 | if (cpuidle_register(&msm_cpuidle_driver, NULL)) |
| 846 | pr_err("%s(): Failed to register CPUIDLE device\n", __func__); |
| 847 | } |
| 848 | |
| 849 | static int lpm_parse_power_params(struct device_node *node, |
| 850 | struct power_params *pwr) |
| 851 | { |
| 852 | char *key; |
| 853 | int ret; |
| 854 | |
| 855 | key = "qcom,latency-us"; |
| 856 | ret = of_property_read_u32(node, key, &pwr->latency_us); |
| 857 | if (ret) |
| 858 | goto fail; |
| 859 | |
| 860 | key = "qcom,ss-power"; |
| 861 | ret = of_property_read_u32(node, key, &pwr->ss_power); |
| 862 | if (ret) |
| 863 | goto fail; |
| 864 | |
| 865 | key = "qcom,energy-overhead"; |
| 866 | ret = of_property_read_u32(node, key, &pwr->energy_overhead); |
| 867 | if (ret) |
| 868 | goto fail; |
| 869 | |
| 870 | key = "qcom,time-overhead"; |
| 871 | ret = of_property_read_u32(node, key, &pwr->time_overhead_us); |
| 872 | if (ret) |
| 873 | goto fail; |
| 874 | fail: |
| 875 | if (ret) |
| 876 | pr_err("%s(): Error reading %s\n", __func__, key); |
| 877 | return ret; |
| 878 | } |
| 879 | |
| 880 | static int lpm_cpu_probe(struct platform_device *pdev) |
| 881 | { |
| 882 | struct lpm_cpu_level *level = NULL, *l; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 883 | struct device_node *node = NULL; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 884 | int num_levels = 0; |
| 885 | char *key; |
| 886 | int ret; |
| 887 | |
| 888 | for_each_child_of_node(pdev->dev.of_node, node) |
| 889 | num_levels++; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 890 | |
| 891 | level = kzalloc(num_levels * sizeof(struct lpm_cpu_level), |
| 892 | GFP_KERNEL); |
| 893 | |
| 894 | if (!level) |
| 895 | return -ENOMEM; |
| 896 | |
| 897 | l = &level[0]; |
| 898 | for_each_child_of_node(pdev->dev.of_node, node) { |
| 899 | |
| 900 | key = "qcom,mode"; |
| 901 | ret = of_property_read_string(node, key, &l->name); |
| 902 | |
| 903 | if (ret) { |
| 904 | pr_err("%s(): Cannot read cpu mode%s\n", __func__, key); |
| 905 | goto fail; |
| 906 | } |
| 907 | |
| 908 | l->mode = msm_pm_get_sleep_mode_value(l->name); |
| 909 | |
| 910 | if (l->mode < 0) { |
| 911 | pr_err("%s():Cannot parse cpu mode:%s\n", __func__, |
| 912 | l->name); |
| 913 | goto fail; |
| 914 | } |
| 915 | |
| 916 | if (l->mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) |
| 917 | l->sync = true; |
| 918 | |
| 919 | key = "qcom,use-broadcast-timer"; |
| 920 | l->use_bc_timer = of_property_read_bool(node, key); |
| 921 | |
| 922 | ret = lpm_parse_power_params(node, &l->pwr); |
| 923 | if (ret) { |
| 924 | pr_err("%s(): cannot Parse power params\n", __func__); |
| 925 | goto fail; |
| 926 | } |
| 927 | l++; |
| 928 | } |
| 929 | sys_state.cpu_level = level; |
| 930 | sys_state.num_cpu_levels = num_levels; |
| 931 | return ret; |
| 932 | fail: |
| 933 | kfree(level); |
| 934 | return ret; |
| 935 | } |
| 936 | |
| 937 | static int lpm_system_probe(struct platform_device *pdev) |
| 938 | { |
| 939 | struct lpm_system_level *level = NULL, *l; |
| 940 | int num_levels = 0; |
| 941 | struct device_node *node; |
| 942 | char *key; |
| 943 | int ret; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 944 | |
| 945 | for_each_child_of_node(pdev->dev.of_node, node) |
| 946 | num_levels++; |
| 947 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 948 | level = kzalloc(num_levels * sizeof(struct lpm_system_level), |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 949 | GFP_KERNEL); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 950 | |
| 951 | if (!level) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 952 | return -ENOMEM; |
| 953 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 954 | l = &level[0]; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 955 | for_each_child_of_node(pdev->dev.of_node, node) { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 956 | |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 957 | key = "qcom,l2"; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 958 | ret = of_property_read_string(node, key, &l->name); |
| 959 | if (ret) { |
| 960 | pr_err("%s(): Failed to read L2 mode\n", __func__); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 961 | goto fail; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 962 | } |
Mahesh Sivasubramanian | b71ce09 | 2013-01-08 13:44:23 -0700 | [diff] [blame] | 963 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 964 | l->l2_mode = lpm_get_l2_cache_value(l->name); |
| 965 | |
| 966 | if (l->l2_mode < 0) { |
| 967 | pr_err("%s(): Failed to read l2 cache mode\n", |
| 968 | __func__); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 969 | goto fail; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 970 | } |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 971 | |
Anil kumar mamidala | a39a335 | 2014-02-06 20:42:01 +0530 | [diff] [blame] | 972 | key = "qcom,send-rpm-sleep-set"; |
| 973 | l->notify_rpm = of_property_read_bool(node, key); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 974 | |
| 975 | if (l->l2_mode >= MSM_SPM_L2_MODE_GDHS) |
| 976 | l->sync = true; |
| 977 | |
| 978 | ret = lpm_parse_power_params(node, &l->pwr); |
| 979 | if (ret) { |
| 980 | pr_err("%s(): Failed to parse power params\n", |
| 981 | __func__); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 982 | goto fail; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 983 | } |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 984 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 985 | key = "qcom,sync-cpus"; |
| 986 | l->sync = of_property_read_bool(node, key); |
| 987 | |
| 988 | if (l->sync) { |
| 989 | const char *name; |
| 990 | |
| 991 | key = "qcom,min-cpu-mode"; |
| 992 | ret = of_property_read_string(node, key, &name); |
| 993 | if (ret) { |
| 994 | pr_err("%s(): Required key %snot found\n", |
| 995 | __func__, name); |
| 996 | goto fail; |
| 997 | } |
| 998 | |
| 999 | l->min_cpu_mode = msm_pm_get_sleep_mode_value(name); |
| 1000 | |
| 1001 | if (l->min_cpu_mode < 0) { |
| 1002 | pr_err("%s(): Cannot parse cpu mode:%s\n", |
| 1003 | __func__, name); |
| 1004 | goto fail; |
| 1005 | } |
| 1006 | |
| 1007 | if (l->min_cpu_mode < sys_state.sync_cpu_mode) |
| 1008 | sys_state.sync_cpu_mode = l->min_cpu_mode; |
| 1009 | } |
| 1010 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1011 | l++; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1012 | } |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1013 | sys_state.system_level = level; |
| 1014 | sys_state.num_system_levels = num_levels; |
Mahesh Sivasubramanian | 208b0d1 | 2014-01-08 12:43:19 -0700 | [diff] [blame] | 1015 | sys_state.last_entered_cluster_index = -1; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1016 | return ret; |
| 1017 | fail: |
| 1018 | kfree(level); |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | static int lpm_probe(struct platform_device *pdev) |
| 1023 | { |
| 1024 | struct device_node *node = NULL; |
| 1025 | char *key = NULL; |
| 1026 | int ret; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1027 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1028 | node = pdev->dev.of_node; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1029 | |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 1030 | key = "qcom,allow-synced-levels"; |
| 1031 | sys_state.allow_synched_levels = of_property_read_bool(node, key); |
| 1032 | |
| 1033 | key = "qcom,no-l2-saw"; |
| 1034 | sys_state.no_l2_saw = of_property_read_bool(node, key); |
| 1035 | |
| 1036 | sys_state.sync_cpu_mode = MSM_PM_SLEEP_MODE_NR; |
| 1037 | spin_lock_init(&sys_state.sync_lock); |
| 1038 | sys_state.num_cores_in_sync = 0; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1039 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1040 | ret = of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); |
| 1041 | |
| 1042 | if (ret) |
| 1043 | goto fail; |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 1044 | |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1045 | /* Do the following two steps only if L2 SAW is present */ |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 1046 | num_powered_cores = num_online_cpus(); |
| 1047 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1048 | if (!sys_state.no_l2_saw) { |
| 1049 | int ret; |
| 1050 | const char *l2; |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1051 | key = "qcom,default-l2-state"; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1052 | ret = of_property_read_string(node, key, &l2); |
| 1053 | if (ret) { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 1054 | pr_err("%s(): Failed to read default L2 mode\n", |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1055 | __func__); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1056 | goto fail; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1057 | } |
Priyanka Mathur | 3abfd44 | 2013-01-11 12:58:51 -0800 | [diff] [blame] | 1058 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1059 | default_l2_mode = lpm_get_l2_cache_value(l2); |
| 1060 | if (default_l2_mode < 0) { |
| 1061 | pr_err("%s(): Unable to parse default L2 mode\n", |
| 1062 | __func__); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1063 | goto fail; |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | if (lpm_levels_sysfs_add()) |
| 1067 | goto fail; |
| 1068 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_ON); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1069 | } else { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1070 | msm_pm_set_l2_flush_flag(MSM_SCM_L2_OFF); |
Girish Mahadevan | c45b4c7 | 2013-04-24 14:07:11 -0600 | [diff] [blame] | 1071 | default_l2_mode = MSM_SPM_L2_MODE_POWER_COLLAPSE; |
| 1072 | } |
| 1073 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1074 | get_cpu(); |
| 1075 | on_each_cpu(setup_broadcast_timer, (void *)true, 1); |
| 1076 | put_cpu(); |
| 1077 | |
| 1078 | register_hotcpu_notifier(&lpm_cpu_nblk); |
| 1079 | |
| 1080 | lpm_system_level_update(); |
| 1081 | platform_device_register(&lpm_dev); |
| 1082 | suspend_set_ops(&lpm_suspend_ops); |
| 1083 | hrtimer_init(&lpm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1084 | lpm_cpuidle_init(); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1085 | return 0; |
| 1086 | fail: |
| 1087 | pr_err("%s: Error in name %s key %s\n", __func__, node->full_name, key); |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1088 | return -EFAULT; |
| 1089 | } |
| 1090 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1091 | static struct of_device_id cpu_modes_mtch_tbl[] = { |
| 1092 | {.compatible = "qcom,cpu-modes"}, |
| 1093 | {}, |
| 1094 | }; |
| 1095 | |
| 1096 | static struct platform_driver cpu_modes_driver = { |
| 1097 | .probe = lpm_cpu_probe, |
| 1098 | .driver = { |
| 1099 | .name = "cpu-modes", |
| 1100 | .owner = THIS_MODULE, |
| 1101 | .of_match_table = cpu_modes_mtch_tbl, |
| 1102 | }, |
| 1103 | }; |
| 1104 | |
| 1105 | static struct of_device_id system_modes_mtch_tbl[] = { |
| 1106 | {.compatible = "qcom,system-modes"}, |
| 1107 | {}, |
| 1108 | }; |
| 1109 | |
| 1110 | static struct platform_driver system_modes_driver = { |
| 1111 | .probe = lpm_system_probe, |
| 1112 | .driver = { |
| 1113 | .name = "system-modes", |
| 1114 | .owner = THIS_MODULE, |
| 1115 | .of_match_table = system_modes_mtch_tbl, |
| 1116 | }, |
| 1117 | }; |
| 1118 | |
| 1119 | static struct of_device_id lpm_levels_match_table[] = { |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1120 | {.compatible = "qcom,lpm-levels"}, |
| 1121 | {}, |
| 1122 | }; |
| 1123 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1124 | static struct platform_driver lpm_levels_driver = { |
| 1125 | .probe = lpm_probe, |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1126 | .driver = { |
| 1127 | .name = "lpm-levels", |
| 1128 | .owner = THIS_MODULE, |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1129 | .of_match_table = lpm_levels_match_table, |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1130 | }, |
| 1131 | }; |
| 1132 | |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1133 | static int __init lpm_levels_module_init(void) |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1134 | { |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1135 | int rc; |
| 1136 | rc = platform_driver_register(&cpu_modes_driver); |
| 1137 | if (rc) { |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 1138 | pr_err("Error registering %s\n", cpu_modes_driver.driver.name); |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1139 | goto fail; |
| 1140 | } |
| 1141 | |
| 1142 | rc = platform_driver_register(&system_modes_driver); |
| 1143 | if (rc) { |
| 1144 | platform_driver_unregister(&cpu_modes_driver); |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 1145 | pr_err("Error registering %s\n", |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1146 | system_modes_driver.driver.name); |
| 1147 | goto fail; |
| 1148 | } |
| 1149 | |
| 1150 | rc = platform_driver_register(&lpm_levels_driver); |
| 1151 | if (rc) { |
| 1152 | platform_driver_unregister(&cpu_modes_driver); |
| 1153 | platform_driver_unregister(&system_modes_driver); |
Mahesh Sivasubramanian | 65c6236 | 2013-05-08 14:30:06 -0600 | [diff] [blame] | 1154 | pr_err("Error registering %s\n", |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1155 | lpm_levels_driver.driver.name); |
| 1156 | } |
| 1157 | fail: |
| 1158 | return rc; |
Praveen Chidambaram | 85b7b28 | 2012-04-16 13:45:15 -0600 | [diff] [blame] | 1159 | } |
Mahesh Sivasubramanian | dab0ec3 | 2013-10-24 15:09:31 -0600 | [diff] [blame] | 1160 | late_initcall(lpm_levels_module_init); |