Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [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 | |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 14 | #include <linux/debugfs.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/completion.h> |
| 19 | #include <linux/cpuidle.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/ktime.h> |
| 23 | #include <linux/pm.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 24 | #include <linux/pm_qos.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 25 | #include <linux/smp.h> |
| 26 | #include <linux/suspend.h> |
| 27 | #include <linux/tick.h> |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 28 | #include <linux/delay.h> |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 29 | #include <linux/platform_device.h> |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 30 | #include <linux/of_platform.h> |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 31 | #include <linux/regulator/krait-regulator.h> |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 32 | #include <linux/cpu.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 33 | #include <mach/msm_iomap.h> |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 34 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | #include <mach/system.h> |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 36 | #include <mach/scm.h> |
| 37 | #include <mach/socinfo.h> |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 38 | #include <mach/msm-krait-l2-accessors.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | #include <asm/cacheflush.h> |
| 40 | #include <asm/hardware/gic.h> |
| 41 | #include <asm/pgtable.h> |
| 42 | #include <asm/pgalloc.h> |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 43 | #include <asm/outercache.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 44 | #ifdef CONFIG_VFP |
| 45 | #include <asm/vfp.h> |
| 46 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 47 | #include "acpuclock.h" |
| 48 | #include "clock.h" |
| 49 | #include "avs.h" |
Abhijeet Dharmapurikar | efaca4f | 2011-12-27 16:24:07 -0800 | [diff] [blame] | 50 | #include <mach/cpuidle.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 51 | #include "idle.h" |
Matt Wagantall | 7cca464 | 2012-02-01 16:43:24 -0800 | [diff] [blame] | 52 | #include "pm.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 53 | #include "scm-boot.h" |
| 54 | #include "spm.h" |
| 55 | #include "timer.h" |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 56 | #include "pm-boot.h" |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 57 | #include <mach/event_timer.h> |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 58 | #define CREATE_TRACE_POINTS |
| 59 | #include "trace_msm_low_power.h" |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 60 | #define SCM_L2_RETENTION (0x2) |
| 61 | #define SCM_CMD_TERMINATE_PC (0x2) |
| 62 | |
| 63 | #define GET_CPU_OF_ATTR(attr) \ |
| 64 | (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu) |
| 65 | |
| 66 | #define SCLK_HZ (32768) |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 67 | |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 68 | #define NUM_OF_COUNTERS 3 |
| 69 | #define MAX_BUF_SIZE 512 |
| 70 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 71 | static int msm_pm_debug_mask = 1; |
| 72 | module_param_named( |
| 73 | debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP |
| 74 | ); |
| 75 | |
| 76 | static int msm_pm_sleep_time_override; |
| 77 | module_param_named(sleep_time_override, |
| 78 | msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 79 | |
| 80 | enum { |
| 81 | MSM_PM_DEBUG_SUSPEND = BIT(0), |
| 82 | MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1), |
| 83 | MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2), |
| 84 | MSM_PM_DEBUG_CLOCK = BIT(3), |
| 85 | MSM_PM_DEBUG_RESET_VECTOR = BIT(4), |
Karthik Parsha | 6fb932d | 2012-01-24 18:04:12 -0800 | [diff] [blame] | 86 | MSM_PM_DEBUG_IDLE_CLK = BIT(5), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 87 | MSM_PM_DEBUG_IDLE = BIT(6), |
| 88 | MSM_PM_DEBUG_IDLE_LIMITS = BIT(7), |
| 89 | MSM_PM_DEBUG_HOTPLUG = BIT(8), |
| 90 | }; |
| 91 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 92 | enum { |
| 93 | MSM_PM_MODE_ATTR_SUSPEND, |
| 94 | MSM_PM_MODE_ATTR_IDLE, |
| 95 | MSM_PM_MODE_ATTR_NR, |
| 96 | }; |
| 97 | |
| 98 | static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = { |
| 99 | [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled", |
| 100 | [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled", |
| 101 | }; |
| 102 | |
| 103 | struct msm_pm_kobj_attribute { |
| 104 | unsigned int cpu; |
| 105 | struct kobj_attribute ka; |
| 106 | }; |
| 107 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 108 | struct msm_pm_sysfs_sleep_mode { |
| 109 | struct kobject *kobj; |
| 110 | struct attribute_group attr_group; |
| 111 | struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1]; |
| 112 | struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR]; |
| 113 | }; |
| 114 | |
| 115 | static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = { |
| 116 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse", |
| 117 | [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi", |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 118 | [MSM_PM_SLEEP_MODE_RETENTION] = "retention", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 119 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] = |
| 120 | "standalone_power_collapse", |
| 121 | }; |
| 122 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 123 | static struct hrtimer pm_hrtimer; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 124 | static struct msm_pm_sleep_ops pm_sleep_ops; |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 125 | static bool msm_pm_ldo_retention_enabled = true; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 126 | static bool msm_pm_use_sync_timer; |
| 127 | static struct msm_pm_cp15_save_data cp15_data; |
| 128 | static bool msm_pm_retention_calls_tz; |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 129 | static bool msm_no_ramp_down_pc; |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 130 | static struct msm_pm_sleep_status_data *msm_pm_slp_sts; |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 131 | static bool msm_pm_pc_reset_timer; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 132 | |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 133 | static int msm_pm_get_pc_mode(struct device_node *node, |
| 134 | const char *key, uint32_t *pc_mode_val) |
| 135 | { |
| 136 | struct pc_mode_of { |
| 137 | uint32_t mode; |
| 138 | char *mode_name; |
| 139 | }; |
| 140 | int i; |
| 141 | struct pc_mode_of pc_modes[] = { |
| 142 | {MSM_PM_PC_TZ_L2_INT, "tz_l2_int"}, |
| 143 | {MSM_PM_PC_NOTZ_L2_EXT, "no_tz_l2_ext"}, |
| 144 | {MSM_PM_PC_TZ_L2_EXT , "tz_l2_ext"} }; |
| 145 | int ret; |
| 146 | const char *pc_mode_str; |
| 147 | |
| 148 | ret = of_property_read_string(node, key, &pc_mode_str); |
| 149 | if (ret) { |
| 150 | pr_debug("%s: Cannot read %s,defaulting to 0", __func__, key); |
| 151 | pc_mode_val = MSM_PM_PC_TZ_L2_INT; |
| 152 | ret = 0; |
| 153 | } else { |
| 154 | ret = -EINVAL; |
| 155 | for (i = 0; i < ARRAY_SIZE(pc_modes); i++) { |
| 156 | if (!strncmp(pc_mode_str, pc_modes[i].mode_name, |
| 157 | strlen(pc_modes[i].mode_name))) { |
| 158 | *pc_mode_val = pc_modes[i].mode; |
| 159 | ret = 0; |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | return ret; |
| 165 | } |
| 166 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 167 | /* |
| 168 | * Write out the attribute. |
| 169 | */ |
| 170 | static ssize_t msm_pm_mode_attr_show( |
| 171 | struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 172 | { |
| 173 | int ret = -EINVAL; |
| 174 | int i; |
| 175 | |
| 176 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 177 | struct kernel_param kp; |
| 178 | unsigned int cpu; |
| 179 | struct msm_pm_platform_data *mode; |
| 180 | |
| 181 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 182 | continue; |
| 183 | |
| 184 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 185 | continue; |
| 186 | |
| 187 | cpu = GET_CPU_OF_ATTR(attr); |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 188 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 189 | |
| 190 | if (!strcmp(attr->attr.name, |
| 191 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 192 | u32 arg = mode->suspend_enabled; |
| 193 | kp.arg = &arg; |
| 194 | ret = param_get_ulong(buf, &kp); |
| 195 | } else if (!strcmp(attr->attr.name, |
| 196 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 197 | u32 arg = mode->idle_enabled; |
| 198 | kp.arg = &arg; |
| 199 | ret = param_get_ulong(buf, &kp); |
| 200 | } |
| 201 | |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | if (ret > 0) { |
Praveen Chidambaram | 2b0fdd0 | 2011-10-28 16:40:58 -0600 | [diff] [blame] | 206 | strlcat(buf, "\n", PAGE_SIZE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 207 | ret++; |
| 208 | } |
| 209 | |
| 210 | return ret; |
| 211 | } |
| 212 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 213 | static ssize_t msm_pm_mode_attr_store(struct kobject *kobj, |
| 214 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 215 | { |
| 216 | int ret = -EINVAL; |
| 217 | int i; |
| 218 | |
| 219 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 220 | struct kernel_param kp; |
| 221 | unsigned int cpu; |
| 222 | struct msm_pm_platform_data *mode; |
| 223 | |
| 224 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 225 | continue; |
| 226 | |
| 227 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 228 | continue; |
| 229 | |
| 230 | cpu = GET_CPU_OF_ATTR(attr); |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 231 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 232 | |
| 233 | if (!strcmp(attr->attr.name, |
| 234 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 235 | kp.arg = &mode->suspend_enabled; |
| 236 | ret = param_set_byte(buf, &kp); |
| 237 | } else if (!strcmp(attr->attr.name, |
| 238 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 239 | kp.arg = &mode->idle_enabled; |
| 240 | ret = param_set_byte(buf, &kp); |
| 241 | } |
| 242 | |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | return ret ? ret : count; |
| 247 | } |
| 248 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 249 | static int __devinit msm_pm_mode_sysfs_add_cpu( |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 250 | unsigned int cpu, struct kobject *modes_kobj) |
| 251 | { |
| 252 | char cpu_name[8]; |
| 253 | struct kobject *cpu_kobj; |
Praveen Chidambaram | 2b0fdd0 | 2011-10-28 16:40:58 -0600 | [diff] [blame] | 254 | struct msm_pm_sysfs_sleep_mode *mode = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 255 | int i, j, k; |
| 256 | int ret; |
| 257 | |
| 258 | snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu); |
| 259 | cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj); |
| 260 | if (!cpu_kobj) { |
| 261 | pr_err("%s: cannot create %s kobject\n", __func__, cpu_name); |
| 262 | ret = -ENOMEM; |
| 263 | goto mode_sysfs_add_cpu_exit; |
| 264 | } |
| 265 | |
| 266 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 267 | int idx = MSM_PM_MODE(cpu, i); |
| 268 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 269 | if ((!msm_pm_sleep_modes[idx].suspend_supported) |
| 270 | && (!msm_pm_sleep_modes[idx].idle_supported)) |
| 271 | continue; |
| 272 | |
| 273 | if (!msm_pm_sleep_mode_labels[i] || |
| 274 | !msm_pm_sleep_mode_labels[i][0]) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 275 | continue; |
| 276 | |
| 277 | mode = kzalloc(sizeof(*mode), GFP_KERNEL); |
| 278 | if (!mode) { |
| 279 | pr_err("%s: cannot allocate memory for attributes\n", |
| 280 | __func__); |
| 281 | ret = -ENOMEM; |
| 282 | goto mode_sysfs_add_cpu_exit; |
| 283 | } |
| 284 | |
| 285 | mode->kobj = kobject_create_and_add( |
| 286 | msm_pm_sleep_mode_labels[i], cpu_kobj); |
| 287 | if (!mode->kobj) { |
| 288 | pr_err("%s: cannot create kobject\n", __func__); |
| 289 | ret = -ENOMEM; |
| 290 | goto mode_sysfs_add_cpu_exit; |
| 291 | } |
| 292 | |
| 293 | for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) { |
| 294 | if ((k == MSM_PM_MODE_ATTR_IDLE) && |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 295 | !msm_pm_sleep_modes[idx].idle_supported) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 296 | continue; |
| 297 | if ((k == MSM_PM_MODE_ATTR_SUSPEND) && |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 298 | !msm_pm_sleep_modes[idx].suspend_supported) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | continue; |
Stephen Boyd | d2059c3 | 2012-07-03 15:11:15 -0700 | [diff] [blame] | 300 | sysfs_attr_init(&mode->kas[j].ka.attr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 301 | mode->kas[j].cpu = cpu; |
| 302 | mode->kas[j].ka.attr.mode = 0644; |
| 303 | mode->kas[j].ka.show = msm_pm_mode_attr_show; |
| 304 | mode->kas[j].ka.store = msm_pm_mode_attr_store; |
| 305 | mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k]; |
| 306 | mode->attrs[j] = &mode->kas[j].ka.attr; |
| 307 | j++; |
| 308 | } |
| 309 | mode->attrs[j] = NULL; |
| 310 | |
| 311 | mode->attr_group.attrs = mode->attrs; |
| 312 | ret = sysfs_create_group(mode->kobj, &mode->attr_group); |
| 313 | if (ret) { |
| 314 | pr_err("%s: cannot create kobject attribute group\n", |
| 315 | __func__); |
| 316 | goto mode_sysfs_add_cpu_exit; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | ret = 0; |
| 321 | |
| 322 | mode_sysfs_add_cpu_exit: |
Praveen Chidambaram | d5ac2d3 | 2011-10-24 14:30:27 -0600 | [diff] [blame] | 323 | if (ret) { |
Praveen Chidambaram | 2cfda63 | 2011-10-11 16:58:09 -0600 | [diff] [blame] | 324 | if (mode && mode->kobj) |
| 325 | kobject_del(mode->kobj); |
| 326 | kfree(mode); |
| 327 | } |
| 328 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 329 | return ret; |
| 330 | } |
| 331 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 332 | int __devinit msm_pm_mode_sysfs_add(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 333 | { |
| 334 | struct kobject *module_kobj; |
| 335 | struct kobject *modes_kobj; |
| 336 | unsigned int cpu; |
| 337 | int ret; |
| 338 | |
| 339 | module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 340 | if (!module_kobj) { |
| 341 | pr_err("%s: cannot find kobject for module %s\n", |
| 342 | __func__, KBUILD_MODNAME); |
| 343 | ret = -ENOENT; |
| 344 | goto mode_sysfs_add_exit; |
| 345 | } |
| 346 | |
| 347 | modes_kobj = kobject_create_and_add("modes", module_kobj); |
| 348 | if (!modes_kobj) { |
| 349 | pr_err("%s: cannot create modes kobject\n", __func__); |
| 350 | ret = -ENOMEM; |
| 351 | goto mode_sysfs_add_exit; |
| 352 | } |
| 353 | |
| 354 | for_each_possible_cpu(cpu) { |
| 355 | ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj); |
| 356 | if (ret) |
| 357 | goto mode_sysfs_add_exit; |
| 358 | } |
| 359 | |
| 360 | ret = 0; |
| 361 | |
| 362 | mode_sysfs_add_exit: |
| 363 | return ret; |
| 364 | } |
| 365 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 366 | /* |
| 367 | * Configure hardware registers in preparation for Apps power down. |
| 368 | */ |
| 369 | static void msm_pm_config_hw_before_power_down(void) |
| 370 | { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * Clear hardware registers after Apps powers up. |
| 376 | */ |
| 377 | static void msm_pm_config_hw_after_power_up(void) |
| 378 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | /* |
| 382 | * Configure hardware registers in preparation for SWFI. |
| 383 | */ |
| 384 | static void msm_pm_config_hw_before_swfi(void) |
| 385 | { |
| 386 | return; |
| 387 | } |
| 388 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 389 | /* |
| 390 | * Configure/Restore hardware registers in preparation for Retention. |
| 391 | */ |
| 392 | |
| 393 | static void msm_pm_config_hw_after_retention(void) |
| 394 | { |
| 395 | int ret; |
Abhijeet Dharmapurikar | e8f8385 | 2013-02-06 18:57:17 -0800 | [diff] [blame] | 396 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 397 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 398 | WARN_ON(ret); |
| 399 | } |
| 400 | |
| 401 | static void msm_pm_config_hw_before_retention(void) |
| 402 | { |
| 403 | return; |
| 404 | } |
| 405 | |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 406 | static void msm_pm_save_cpu_reg(void) |
| 407 | { |
| 408 | int i; |
| 409 | |
| 410 | /* Only on core0 */ |
| 411 | if (smp_processor_id()) |
| 412 | return; |
| 413 | |
| 414 | /** |
| 415 | * On some targets, L2 PC will turn off may reset the core |
| 416 | * configuration for the mux and the default may not make the core |
| 417 | * happy when it resumes. |
| 418 | * Save the active vdd, and set the core vdd to QSB max vdd, so that |
| 419 | * when the core resumes, it is capable of supporting the current QSB |
| 420 | * rate. Then restore the active vdd before switching the acpuclk rate. |
| 421 | */ |
| 422 | if (msm_pm_get_l2_flush_flag() == 1) { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 423 | cp15_data.active_vdd = msm_spm_get_vdd(0); |
| 424 | for (i = 0; i < cp15_data.reg_saved_state_size; i++) |
| 425 | cp15_data.reg_val[i] = |
| 426 | get_l2_indirect_reg( |
| 427 | cp15_data.reg_data[i]); |
| 428 | msm_spm_set_vdd(0, cp15_data.qsb_pc_vdd); |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | |
| 432 | static void msm_pm_restore_cpu_reg(void) |
| 433 | { |
| 434 | int i; |
| 435 | |
| 436 | /* Only on core0 */ |
| 437 | if (smp_processor_id()) |
| 438 | return; |
| 439 | |
| 440 | if (msm_pm_get_l2_flush_flag() == 1) { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 441 | for (i = 0; i < cp15_data.reg_saved_state_size; i++) |
| 442 | set_l2_indirect_reg( |
| 443 | cp15_data.reg_data[i], |
| 444 | cp15_data.reg_val[i]); |
| 445 | msm_spm_set_vdd(0, cp15_data.active_vdd); |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 446 | } |
| 447 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 448 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 449 | static void msm_pm_swfi(void) |
| 450 | { |
| 451 | msm_pm_config_hw_before_swfi(); |
| 452 | msm_arch_idle(); |
| 453 | } |
| 454 | |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 455 | static void msm_pm_retention(void) |
| 456 | { |
| 457 | int ret = 0; |
| 458 | |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 459 | msm_pm_config_hw_before_retention(); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 460 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_RETENTION, false); |
| 461 | WARN_ON(ret); |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 462 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 463 | if (msm_pm_retention_calls_tz) |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 464 | scm_call_atomic1(SCM_SVC_BOOT, SCM_CMD_TERMINATE_PC, |
| 465 | SCM_L2_RETENTION); |
| 466 | else |
| 467 | msm_arch_idle(); |
| 468 | |
| 469 | msm_pm_config_hw_after_retention(); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 470 | } |
| 471 | |
Stephen Boyd | b29750d | 2012-02-21 01:21:32 -0800 | [diff] [blame] | 472 | static bool __ref msm_pm_spm_power_collapse( |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 473 | unsigned int cpu, bool from_idle, bool notify_rpm) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | { |
| 475 | void *entry; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 476 | bool collapsed = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 477 | int ret; |
| 478 | |
| 479 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 480 | pr_info("CPU%u: %s: notify_rpm %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 481 | cpu, __func__, (int) notify_rpm); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 482 | |
| 483 | ret = msm_spm_set_low_power_mode( |
| 484 | MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm); |
| 485 | WARN_ON(ret); |
| 486 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 487 | entry = (!cpu || from_idle) ? |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 488 | msm_pm_collapse_exit : msm_secondary_startup; |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 489 | msm_pm_boot_config_before_pc(cpu, virt_to_phys(entry)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 490 | |
| 491 | if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask) |
| 492 | pr_info("CPU%u: %s: program vector to %p\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 493 | cpu, __func__, entry); |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 494 | if (from_idle && msm_pm_pc_reset_timer) |
| 495 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 496 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 497 | collapsed = msm_pm_collapse(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 498 | |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 499 | if (from_idle && msm_pm_pc_reset_timer) |
| 500 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu); |
| 501 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 502 | msm_pm_boot_config_after_pc(cpu); |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame] | 503 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 504 | if (collapsed) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 505 | cpu_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 506 | local_fiq_enable(); |
| 507 | } |
| 508 | |
| 509 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 510 | pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 511 | cpu, __func__, collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 512 | |
| 513 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 514 | WARN_ON(ret); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 515 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 518 | static bool msm_pm_power_collapse_standalone(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 519 | { |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 520 | unsigned int cpu = smp_processor_id(); |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 521 | unsigned int avsdscr; |
| 522 | unsigned int avscsr; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 523 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 524 | |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 525 | avsdscr = avs_get_avsdscr(); |
| 526 | avscsr = avs_get_avscsr(); |
| 527 | avs_set_avscsr(0); /* Disable AVS */ |
| 528 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 529 | collapsed = msm_pm_spm_power_collapse(cpu, from_idle, false); |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 530 | |
| 531 | avs_set_avsdscr(avsdscr); |
| 532 | avs_set_avscsr(avscsr); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 533 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 536 | static bool msm_pm_power_collapse(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 537 | { |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 538 | unsigned int cpu = smp_processor_id(); |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 539 | unsigned long saved_acpuclk_rate = 0; |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 540 | unsigned int avsdscr; |
| 541 | unsigned int avscsr; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 542 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 543 | |
| 544 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 545 | pr_info("CPU%u: %s: idle %d\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 546 | cpu, __func__, (int)from_idle); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 547 | |
| 548 | msm_pm_config_hw_before_power_down(); |
| 549 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 550 | pr_info("CPU%u: %s: pre power down\n", cpu, __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 551 | |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 552 | avsdscr = avs_get_avsdscr(); |
| 553 | avscsr = avs_get_avscsr(); |
| 554 | avs_set_avscsr(0); /* Disable AVS */ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 556 | if (cpu_online(cpu) && !msm_no_ramp_down_pc) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 557 | saved_acpuclk_rate = acpuclk_power_collapse(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 558 | |
| 559 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 560 | pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n", |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 561 | cpu, __func__, saved_acpuclk_rate); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 562 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 563 | if (cp15_data.save_cp15) |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 564 | msm_pm_save_cpu_reg(); |
| 565 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 566 | collapsed = msm_pm_spm_power_collapse(cpu, from_idle, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 567 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 568 | if (cp15_data.save_cp15) |
Praveen Chidambaram | c594a09 | 2012-09-18 19:48:29 -0600 | [diff] [blame] | 569 | msm_pm_restore_cpu_reg(); |
| 570 | |
Girish Mahadevan | 4e025d9 | 2012-02-29 13:26:51 -0700 | [diff] [blame] | 571 | if (cpu_online(cpu)) { |
| 572 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 573 | pr_info("CPU%u: %s: restore clock rate to %lu\n", |
| 574 | cpu, __func__, saved_acpuclk_rate); |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 575 | if (!msm_no_ramp_down_pc && |
| 576 | acpuclk_set_rate(cpu, saved_acpuclk_rate, SETRATE_PC) |
| 577 | < 0) |
Girish Mahadevan | 4e025d9 | 2012-02-29 13:26:51 -0700 | [diff] [blame] | 578 | pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n", |
| 579 | cpu, __func__, saved_acpuclk_rate); |
| 580 | } else { |
| 581 | unsigned int gic_dist_enabled; |
| 582 | unsigned int gic_dist_pending; |
| 583 | gic_dist_enabled = readl_relaxed( |
| 584 | MSM_QGIC_DIST_BASE + GIC_DIST_ENABLE_CLEAR); |
| 585 | gic_dist_pending = readl_relaxed( |
| 586 | MSM_QGIC_DIST_BASE + GIC_DIST_PENDING_SET); |
| 587 | mb(); |
| 588 | gic_dist_pending &= gic_dist_enabled; |
| 589 | |
| 590 | if (gic_dist_pending) |
| 591 | pr_err("CPU %d interrupted during hotplug.Pending int 0x%x\n", |
| 592 | cpu, gic_dist_pending); |
| 593 | } |
| 594 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 595 | |
Praveen Chidambaram | 5e61411 | 2012-11-08 17:53:34 -0700 | [diff] [blame] | 596 | avs_set_avsdscr(avsdscr); |
| 597 | avs_set_avscsr(avscsr); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 598 | msm_pm_config_hw_after_power_up(); |
| 599 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 600 | pr_info("CPU%u: %s: post power up\n", cpu, __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 601 | |
| 602 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 603 | pr_info("CPU%u: %s: return\n", cpu, __func__); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 604 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 605 | } |
| 606 | |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 607 | static int64_t msm_pm_timer_enter_idle(void) |
| 608 | { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 609 | if (msm_pm_use_sync_timer) |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 610 | return ktime_to_ns(tick_nohz_get_sleep_length()); |
| 611 | |
| 612 | return msm_timer_enter_idle(); |
| 613 | } |
| 614 | |
| 615 | static void msm_pm_timer_exit_idle(bool timer_halted) |
| 616 | { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 617 | if (msm_pm_use_sync_timer) |
Praveen Chidambaram | 192979f | 2012-04-25 18:30:23 -0600 | [diff] [blame] | 618 | return; |
| 619 | |
| 620 | msm_timer_exit_idle((int) timer_halted); |
| 621 | } |
| 622 | |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 623 | static int64_t msm_pm_timer_enter_suspend(int64_t *period) |
| 624 | { |
Anji Jonnala | c02367a | 2012-07-01 02:56:11 +0530 | [diff] [blame] | 625 | int64_t time = 0; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 626 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 627 | if (msm_pm_use_sync_timer) |
Mahesh Sivasubramanian | 0eaeb30 | 2013-03-14 14:56:32 -0600 | [diff] [blame] | 628 | return sched_clock(); |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 629 | |
| 630 | time = msm_timer_get_sclk_time(period); |
| 631 | if (!time) |
| 632 | pr_err("%s: Unable to read sclk.\n", __func__); |
| 633 | |
| 634 | return time; |
| 635 | } |
| 636 | |
| 637 | static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period) |
| 638 | { |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 639 | if (msm_pm_use_sync_timer) |
Mahesh Sivasubramanian | 0eaeb30 | 2013-03-14 14:56:32 -0600 | [diff] [blame] | 640 | return sched_clock() - time; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 641 | |
| 642 | if (time != 0) { |
| 643 | int64_t end_time = msm_timer_get_sclk_time(NULL); |
| 644 | if (end_time != 0) { |
| 645 | time = end_time - time; |
| 646 | if (time < 0) |
| 647 | time += period; |
| 648 | } else |
| 649 | time = 0; |
| 650 | } |
| 651 | |
| 652 | return time; |
| 653 | } |
| 654 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 655 | /** |
| 656 | * pm_hrtimer_cb() : Callback function for hrtimer created if the |
| 657 | * core needs to be awake to handle an event. |
| 658 | * @hrtimer : Pointer to hrtimer |
| 659 | */ |
| 660 | static enum hrtimer_restart pm_hrtimer_cb(struct hrtimer *hrtimer) |
| 661 | { |
| 662 | return HRTIMER_NORESTART; |
| 663 | } |
| 664 | |
| 665 | /** |
| 666 | * msm_pm_set_timer() : Set an hrtimer to wakeup the core in time |
| 667 | * to handle an event. |
| 668 | */ |
| 669 | static void msm_pm_set_timer(uint32_t modified_time_us) |
| 670 | { |
| 671 | u64 modified_time_ns = modified_time_us * NSEC_PER_USEC; |
| 672 | ktime_t modified_ktime = ns_to_ktime(modified_time_ns); |
| 673 | pm_hrtimer.function = pm_hrtimer_cb; |
| 674 | hrtimer_start(&pm_hrtimer, modified_ktime, HRTIMER_MODE_ABS); |
| 675 | } |
| 676 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 677 | /****************************************************************************** |
| 678 | * External Idle/Suspend Functions |
| 679 | *****************************************************************************/ |
| 680 | |
| 681 | void arch_idle(void) |
| 682 | { |
| 683 | return; |
| 684 | } |
| 685 | |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 686 | static inline void msm_pm_ftrace_lpm_enter(unsigned int cpu, |
| 687 | uint32_t latency, uint32_t sleep_us, |
| 688 | uint32_t wake_up, |
| 689 | enum msm_pm_sleep_mode mode) |
| 690 | { |
| 691 | switch (mode) { |
| 692 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 693 | trace_msm_pm_enter_wfi(cpu, latency, sleep_us, wake_up); |
| 694 | break; |
| 695 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 696 | trace_msm_pm_enter_spc(cpu, latency, sleep_us, wake_up); |
| 697 | break; |
| 698 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
| 699 | trace_msm_pm_enter_pc(cpu, latency, sleep_us, wake_up); |
| 700 | break; |
| 701 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 702 | trace_msm_pm_enter_ret(cpu, latency, sleep_us, wake_up); |
| 703 | break; |
| 704 | default: |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | static inline void msm_pm_ftrace_lpm_exit(unsigned int cpu, |
| 710 | enum msm_pm_sleep_mode mode, |
| 711 | bool success) |
| 712 | { |
| 713 | switch (mode) { |
| 714 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 715 | trace_msm_pm_exit_wfi(cpu, success); |
| 716 | break; |
| 717 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 718 | trace_msm_pm_exit_spc(cpu, success); |
| 719 | break; |
| 720 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
| 721 | trace_msm_pm_exit_pc(cpu, success); |
| 722 | break; |
| 723 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 724 | trace_msm_pm_exit_ret(cpu, success); |
| 725 | break; |
| 726 | default: |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 731 | static int msm_pm_idle_prepare(struct cpuidle_device *dev, |
| 732 | struct cpuidle_driver *drv, int index, |
| 733 | void **msm_pm_idle_rs_limits) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 734 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 735 | int i; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 736 | unsigned int power_usage = -1; |
Priyanka Mathur | 848bb4c | 2012-11-30 18:04:57 -0800 | [diff] [blame] | 737 | int ret = MSM_PM_SLEEP_MODE_NOT_SELECTED; |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 738 | uint32_t modified_time_us = 0; |
| 739 | struct msm_pm_time_params time_param; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 740 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 741 | time_param.latency_us = |
| 742 | (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
| 743 | time_param.sleep_us = |
| 744 | (uint32_t) (ktime_to_us(tick_nohz_get_sleep_length()) |
| 745 | & UINT_MAX); |
| 746 | time_param.modified_time_us = 0; |
| 747 | |
| 748 | if (!dev->cpu) |
| 749 | time_param.next_event_us = |
| 750 | (uint32_t) (ktime_to_us(get_next_event_time()) |
| 751 | & UINT_MAX); |
| 752 | else |
| 753 | time_param.next_event_us = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 754 | |
| 755 | for (i = 0; i < dev->state_count; i++) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 756 | struct cpuidle_state *state = &drv->states[i]; |
| 757 | struct cpuidle_state_usage *st_usage = &dev->states_usage[i]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 758 | enum msm_pm_sleep_mode mode; |
| 759 | bool allow; |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 760 | uint32_t power; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 761 | int idx; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 762 | void *rs_limits = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 763 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 764 | mode = (enum msm_pm_sleep_mode) cpuidle_get_statedata(st_usage); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 765 | idx = MSM_PM_MODE(dev->cpu, mode); |
| 766 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 767 | allow = msm_pm_sleep_modes[idx].idle_enabled && |
| 768 | msm_pm_sleep_modes[idx].idle_supported; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 769 | |
| 770 | switch (mode) { |
| 771 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 772 | if (num_online_cpus() > 1) |
Priyanka Mathur | 68a9bc5 | 2012-10-03 15:15:01 -0700 | [diff] [blame] | 773 | allow = false; |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 774 | break; |
Mahesh Sivasubramanian | ba7e01c | 2012-09-04 18:22:51 -0600 | [diff] [blame] | 775 | case MSM_PM_SLEEP_MODE_RETENTION: |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 776 | /* |
| 777 | * The Krait BHS regulator doesn't have enough head |
| 778 | * room to drive the retention voltage on LDO and so |
| 779 | * has disabled retention |
| 780 | */ |
| 781 | if (!msm_pm_ldo_retention_enabled) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 782 | allow = false; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 783 | |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 784 | if (msm_pm_retention_calls_tz && num_online_cpus() > 1) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 785 | allow = false; |
| 786 | break; |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 787 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 788 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 789 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 790 | default: |
| 791 | allow = false; |
| 792 | break; |
| 793 | } |
| 794 | |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 795 | if (!allow) |
| 796 | continue; |
| 797 | |
| 798 | if (pm_sleep_ops.lowest_limits) |
| 799 | rs_limits = pm_sleep_ops.lowest_limits(true, |
| 800 | mode, &time_param, &power); |
| 801 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 802 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 803 | pr_info("CPU%u:%s:%s, latency %uus, slp %uus, lim %p\n", |
| 804 | dev->cpu, __func__, state->desc, |
| 805 | time_param.latency_us, |
| 806 | time_param.sleep_us, rs_limits); |
| 807 | if (!rs_limits) |
| 808 | continue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 809 | |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 810 | if (power < power_usage) { |
| 811 | power_usage = power; |
| 812 | modified_time_us = time_param.modified_time_us; |
| 813 | ret = mode; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 814 | *msm_pm_idle_rs_limits = rs_limits; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 815 | } |
Mahesh Sivasubramanian | 87f579c | 2013-02-04 11:59:44 -0700 | [diff] [blame] | 816 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 817 | } |
| 818 | |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 819 | if (modified_time_us && !dev->cpu) |
| 820 | msm_pm_set_timer(modified_time_us); |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 821 | |
| 822 | msm_pm_ftrace_lpm_enter(dev->cpu, time_param.latency_us, |
| 823 | time_param.sleep_us, time_param.next_event_us, |
| 824 | ret); |
| 825 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 826 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 829 | enum msm_pm_sleep_mode msm_pm_idle_enter(struct cpuidle_device *dev, |
| 830 | struct cpuidle_driver *drv, int index) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 831 | { |
| 832 | int64_t time; |
Priyanka Mathur | 26b4a4b | 2012-11-05 13:45:45 -0800 | [diff] [blame] | 833 | bool collapsed = 1; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 834 | int exit_stat = -1; |
| 835 | enum msm_pm_sleep_mode sleep_mode; |
| 836 | void *msm_pm_idle_rs_limits = NULL; |
Mahesh Sivasubramanian | 0369876 | 2013-04-18 16:18:46 -0600 | [diff] [blame] | 837 | uint32_t sleep_delay = 1; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 838 | int ret = -ENODEV; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 839 | int notify_rpm = false; |
| 840 | bool timer_halted = false; |
| 841 | |
| 842 | sleep_mode = msm_pm_idle_prepare(dev, drv, index, |
| 843 | &msm_pm_idle_rs_limits); |
| 844 | |
| 845 | if (!msm_pm_idle_rs_limits) { |
| 846 | sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED; |
| 847 | goto cpuidle_enter_bail; |
| 848 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 849 | |
| 850 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 851 | pr_info("CPU%u: %s: mode %d\n", |
| 852 | smp_processor_id(), __func__, sleep_mode); |
| 853 | |
| 854 | time = ktime_to_ns(ktime_get()); |
| 855 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 856 | if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) { |
| 857 | notify_rpm = true; |
Mahesh Sivasubramanian | 0369876 | 2013-04-18 16:18:46 -0600 | [diff] [blame] | 858 | sleep_delay = (uint32_t)msm_pm_timer_enter_idle(); |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 859 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 860 | if (sleep_delay == 0) /* 0 would mean infinite time */ |
| 861 | sleep_delay = 1; |
| 862 | } |
| 863 | |
| 864 | if (pm_sleep_ops.enter_sleep) |
| 865 | ret = pm_sleep_ops.enter_sleep(sleep_delay, |
| 866 | msm_pm_idle_rs_limits, true, notify_rpm); |
| 867 | if (ret) |
| 868 | goto cpuidle_enter_bail; |
| 869 | |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 870 | switch (sleep_mode) { |
| 871 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 872 | msm_pm_swfi(); |
| 873 | exit_stat = MSM_PM_STAT_IDLE_WFI; |
| 874 | break; |
| 875 | |
| 876 | case MSM_PM_SLEEP_MODE_RETENTION: |
| 877 | msm_pm_retention(); |
| 878 | exit_stat = MSM_PM_STAT_RETENTION; |
| 879 | break; |
| 880 | |
| 881 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 882 | collapsed = msm_pm_power_collapse_standalone(true); |
| 883 | exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE; |
| 884 | break; |
| 885 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 886 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 887 | if (MSM_PM_DEBUG_IDLE_CLK & msm_pm_debug_mask) |
| 888 | clock_debug_print_enabled(); |
Karthik Parsha | 6fb932d | 2012-01-24 18:04:12 -0800 | [diff] [blame] | 889 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 890 | collapsed = msm_pm_power_collapse(true); |
| 891 | timer_halted = true; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 892 | |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 893 | exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE; |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 894 | msm_pm_timer_exit_idle(timer_halted); |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 895 | break; |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 896 | |
| 897 | case MSM_PM_SLEEP_MODE_NOT_SELECTED: |
| 898 | goto cpuidle_enter_bail; |
| 899 | break; |
| 900 | |
| 901 | default: |
| 902 | __WARN(); |
| 903 | goto cpuidle_enter_bail; |
| 904 | break; |
| 905 | } |
| 906 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 907 | if (pm_sleep_ops.exit_sleep) |
| 908 | pm_sleep_ops.exit_sleep(msm_pm_idle_rs_limits, true, |
| 909 | notify_rpm, collapsed); |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 910 | |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 911 | time = ktime_to_ns(ktime_get()) - time; |
| 912 | msm_pm_ftrace_lpm_exit(smp_processor_id(), sleep_mode, collapsed); |
| 913 | if (exit_stat >= 0) |
| 914 | msm_pm_add_stat(exit_stat, time); |
Mahesh Sivasubramanian | 95a48e7 | 2013-02-15 14:23:26 -0700 | [diff] [blame] | 915 | do_div(time, 1000); |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 916 | dev->last_residency = (int) time; |
| 917 | return sleep_mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 918 | |
| 919 | cpuidle_enter_bail: |
Priyanka Mathur | 92fe575 | 2013-01-17 10:58:04 -0800 | [diff] [blame] | 920 | dev->last_residency = 0; |
| 921 | if (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE) |
| 922 | msm_pm_timer_exit_idle(timer_halted); |
| 923 | sleep_mode = MSM_PM_SLEEP_MODE_NOT_SELECTED; |
| 924 | return sleep_mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 927 | int msm_pm_wait_cpu_shutdown(unsigned int cpu) |
| 928 | { |
| 929 | int timeout = 10; |
| 930 | |
| 931 | if (!msm_pm_slp_sts) |
| 932 | return 0; |
| 933 | if (!msm_pm_slp_sts[cpu].base_addr) |
| 934 | return 0; |
| 935 | while (timeout--) { |
| 936 | /* |
| 937 | * Check for the SPM of the core being hotplugged to set |
| 938 | * its sleep state.The SPM sleep state indicates that the |
| 939 | * core has been power collapsed. |
| 940 | */ |
| 941 | int acc_sts = __raw_readl(msm_pm_slp_sts[cpu].base_addr); |
| 942 | |
| 943 | if (acc_sts & msm_pm_slp_sts[cpu].mask) |
| 944 | return 0; |
Mahesh Sivasubramanian | 65187f9 | 2013-03-19 15:14:06 -0600 | [diff] [blame] | 945 | udelay(100); |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | pr_info("%s(): Timed out waiting for CPU %u SPM to enter sleep state", |
| 949 | __func__, cpu); |
| 950 | return -EBUSY; |
| 951 | } |
| 952 | |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 953 | void msm_pm_cpu_enter_lowpower(unsigned int cpu) |
| 954 | { |
| 955 | int i; |
| 956 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 957 | |
| 958 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 959 | struct msm_pm_platform_data *mode; |
| 960 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 961 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(cpu, i)]; |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 962 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 963 | } |
| 964 | |
| 965 | if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask) |
| 966 | pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__); |
| 967 | |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 968 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 969 | msm_pm_power_collapse(false); |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 970 | else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 971 | msm_pm_power_collapse_standalone(false); |
Matt Wagantall | 5375aa7 | 2012-07-02 19:59:51 -0700 | [diff] [blame] | 972 | else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 973 | msm_pm_retention(); |
Stephen Boyd | bda7427 | 2012-08-09 14:01:27 -0700 | [diff] [blame] | 974 | else |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 975 | msm_pm_swfi(); |
Mahesh Sivasubramanian | d23add1 | 2011-11-18 14:30:11 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 978 | static void msm_pm_ack_retention_disable(void *data) |
| 979 | { |
| 980 | /* |
| 981 | * This is a NULL function to ensure that the core has woken up |
| 982 | * and is safe to disable retention. |
| 983 | */ |
| 984 | } |
| 985 | /** |
| 986 | * msm_pm_enable_retention() - Disable/Enable retention on all cores |
| 987 | * @enable: Enable/Disable retention |
| 988 | * |
| 989 | */ |
| 990 | void msm_pm_enable_retention(bool enable) |
| 991 | { |
Matt Wagantall | d06f5c2 | 2013-03-14 19:31:14 -0700 | [diff] [blame] | 992 | if (enable == msm_pm_ldo_retention_enabled) |
| 993 | return; |
| 994 | |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 995 | msm_pm_ldo_retention_enabled = enable; |
| 996 | /* |
| 997 | * If retention is being disabled, wakeup all online core to ensure |
| 998 | * that it isn't executing retention. Offlined cores need not be woken |
| 999 | * up as they enter the deepest sleep mode, namely RPM assited power |
| 1000 | * collapse |
| 1001 | */ |
Mahesh Sivasubramanian | 96f67d1 | 2013-03-14 22:44:42 -0600 | [diff] [blame] | 1002 | if (!enable) { |
| 1003 | preempt_disable(); |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1004 | smp_call_function_many(cpu_online_mask, |
| 1005 | msm_pm_ack_retention_disable, |
| 1006 | NULL, true); |
Mahesh Sivasubramanian | 96f67d1 | 2013-03-14 22:44:42 -0600 | [diff] [blame] | 1007 | preempt_enable(); |
| 1008 | |
| 1009 | |
| 1010 | } |
Mahesh Sivasubramanian | 1b8601b | 2012-12-20 14:11:23 -0700 | [diff] [blame] | 1011 | } |
| 1012 | EXPORT_SYMBOL(msm_pm_enable_retention); |
| 1013 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1014 | static int msm_pm_enter(suspend_state_t state) |
| 1015 | { |
| 1016 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 1017 | int i; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1018 | int64_t period = 0; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1019 | int64_t time = msm_pm_timer_enter_suspend(&period); |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 1020 | struct msm_pm_time_params time_param; |
| 1021 | |
| 1022 | time_param.latency_us = -1; |
| 1023 | time_param.sleep_us = -1; |
| 1024 | time_param.next_event_us = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1025 | |
| 1026 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1027 | pr_info("%s\n", __func__); |
| 1028 | |
| 1029 | if (smp_processor_id()) { |
| 1030 | __WARN(); |
| 1031 | goto enter_exit; |
| 1032 | } |
| 1033 | |
| 1034 | |
| 1035 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 1036 | struct msm_pm_platform_data *mode; |
| 1037 | |
Praveen Chidambaram | 42da9d2 | 2012-03-30 12:16:34 -0600 | [diff] [blame] | 1038 | mode = &msm_pm_sleep_modes[MSM_PM_MODE(0, i)]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1039 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 1040 | } |
| 1041 | |
| 1042 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1043 | void *rs_limits = NULL; |
| 1044 | int ret = -ENODEV; |
| 1045 | uint32_t power; |
Mahesh Sivasubramanian | 0369876 | 2013-04-18 16:18:46 -0600 | [diff] [blame] | 1046 | uint32_t msm_pm_max_sleep_time = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1047 | |
| 1048 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1049 | pr_info("%s: power collapse\n", __func__); |
| 1050 | |
| 1051 | clock_debug_print_enabled(); |
| 1052 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1053 | if (msm_pm_sleep_time_override > 0) { |
| 1054 | int64_t ns = NSEC_PER_SEC * |
| 1055 | (int64_t) msm_pm_sleep_time_override; |
Mahesh Sivasubramanian | 0369876 | 2013-04-18 16:18:46 -0600 | [diff] [blame] | 1056 | do_div(ns, NSEC_PER_SEC / SCLK_HZ); |
| 1057 | msm_pm_max_sleep_time = (uint32_t) ns; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1058 | } |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1059 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1060 | if (pm_sleep_ops.lowest_limits) |
| 1061 | rs_limits = pm_sleep_ops.lowest_limits(false, |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1062 | MSM_PM_SLEEP_MODE_POWER_COLLAPSE, &time_param, &power); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1063 | |
| 1064 | if (rs_limits) { |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1065 | if (pm_sleep_ops.enter_sleep) |
| 1066 | ret = pm_sleep_ops.enter_sleep( |
| 1067 | msm_pm_max_sleep_time, |
| 1068 | rs_limits, false, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1069 | if (!ret) { |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 1070 | int collapsed = msm_pm_power_collapse(false); |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1071 | if (pm_sleep_ops.exit_sleep) { |
| 1072 | pm_sleep_ops.exit_sleep(rs_limits, |
| 1073 | false, true, collapsed); |
| 1074 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1075 | } |
| 1076 | } else { |
| 1077 | pr_err("%s: cannot find the lowest power limit\n", |
| 1078 | __func__); |
| 1079 | } |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1080 | time = msm_pm_timer_exit_suspend(time, period); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1081 | msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1082 | } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) { |
| 1083 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1084 | pr_info("%s: standalone power collapse\n", __func__); |
| 1085 | msm_pm_power_collapse_standalone(false); |
Praveen Chidambaram | d3d844d | 2012-04-24 09:47:38 -0600 | [diff] [blame] | 1086 | } else if (allow[MSM_PM_SLEEP_MODE_RETENTION]) { |
| 1087 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1088 | pr_info("%s: retention\n", __func__); |
| 1089 | msm_pm_retention(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1090 | } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) { |
| 1091 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1092 | pr_info("%s: swfi\n", __func__); |
| 1093 | msm_pm_swfi(); |
| 1094 | } |
| 1095 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1096 | enter_exit: |
| 1097 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1098 | pr_info("%s: return\n", __func__); |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
Mahesh Sivasubramanian | 6d06e3a | 2012-05-16 13:41:07 -0600 | [diff] [blame] | 1103 | void msm_pm_set_sleep_ops(struct msm_pm_sleep_ops *ops) |
| 1104 | { |
| 1105 | if (ops) |
| 1106 | pm_sleep_ops = *ops; |
| 1107 | } |
| 1108 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1109 | static const struct platform_suspend_ops msm_pm_ops = { |
| 1110 | .enter = msm_pm_enter, |
| 1111 | .valid = suspend_valid_only_mem, |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1112 | }; |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 1113 | static int __devinit msm_cpu_status_probe(struct platform_device *pdev) |
| 1114 | { |
| 1115 | struct msm_pm_sleep_status_data *pdata; |
| 1116 | char *key; |
| 1117 | u32 cpu; |
| 1118 | |
| 1119 | if (!pdev) |
| 1120 | return -EFAULT; |
| 1121 | |
| 1122 | msm_pm_slp_sts = |
| 1123 | kzalloc(sizeof(*msm_pm_slp_sts) * num_possible_cpus(), |
| 1124 | GFP_KERNEL); |
| 1125 | |
| 1126 | if (!msm_pm_slp_sts) |
| 1127 | return -ENOMEM; |
| 1128 | |
| 1129 | if (pdev->dev.of_node) { |
| 1130 | struct resource *res; |
| 1131 | u32 offset; |
| 1132 | int rc; |
| 1133 | u32 mask; |
| 1134 | |
| 1135 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1136 | if (!res) |
| 1137 | goto fail_free_mem; |
| 1138 | |
| 1139 | key = "qcom,cpu-alias-addr"; |
| 1140 | rc = of_property_read_u32(pdev->dev.of_node, key, &offset); |
| 1141 | |
| 1142 | if (rc) |
| 1143 | goto fail_free_mem; |
| 1144 | |
| 1145 | key = "qcom,sleep-status-mask"; |
| 1146 | rc = of_property_read_u32(pdev->dev.of_node, key, |
| 1147 | &mask); |
| 1148 | if (rc) |
| 1149 | goto fail_free_mem; |
| 1150 | |
| 1151 | for_each_possible_cpu(cpu) { |
| 1152 | msm_pm_slp_sts[cpu].base_addr = |
| 1153 | ioremap(res->start + cpu * offset, |
| 1154 | resource_size(res)); |
| 1155 | msm_pm_slp_sts[cpu].mask = mask; |
| 1156 | |
| 1157 | if (!msm_pm_slp_sts[cpu].base_addr) |
| 1158 | goto failed_of_node; |
| 1159 | } |
| 1160 | |
| 1161 | } else { |
| 1162 | pdata = pdev->dev.platform_data; |
| 1163 | if (!pdev->dev.platform_data) |
| 1164 | goto fail_free_mem; |
| 1165 | |
| 1166 | for_each_possible_cpu(cpu) { |
| 1167 | msm_pm_slp_sts[cpu].base_addr = |
| 1168 | pdata->base_addr + cpu * pdata->cpu_offset; |
| 1169 | msm_pm_slp_sts[cpu].mask = pdata->mask; |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | return 0; |
| 1174 | |
| 1175 | failed_of_node: |
| 1176 | pr_info("%s(): Failed to key=%s\n", __func__, key); |
| 1177 | for_each_possible_cpu(cpu) { |
| 1178 | if (msm_pm_slp_sts[cpu].base_addr) |
| 1179 | iounmap(msm_pm_slp_sts[cpu].base_addr); |
| 1180 | } |
| 1181 | fail_free_mem: |
| 1182 | kfree(msm_pm_slp_sts); |
| 1183 | return -EINVAL; |
| 1184 | |
| 1185 | }; |
| 1186 | |
| 1187 | static struct of_device_id msm_slp_sts_match_tbl[] = { |
| 1188 | {.compatible = "qcom,cpu-sleep-status"}, |
| 1189 | {}, |
| 1190 | }; |
| 1191 | |
| 1192 | static struct platform_driver msm_cpu_status_driver = { |
| 1193 | .probe = msm_cpu_status_probe, |
| 1194 | .driver = { |
| 1195 | .name = "cpu_slp_status", |
| 1196 | .owner = THIS_MODULE, |
| 1197 | .of_match_table = msm_slp_sts_match_tbl, |
| 1198 | }, |
| 1199 | }; |
Mahesh Sivasubramanian | cb39662 | 2012-03-14 14:50:37 -0600 | [diff] [blame] | 1200 | |
Praveen Chidambaram | ede45d0 | 2013-02-20 17:45:21 -0700 | [diff] [blame] | 1201 | static int __init msm_pm_setup_saved_state(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1202 | { |
| 1203 | pgd_t *pc_pgd; |
| 1204 | pmd_t *pmd; |
| 1205 | unsigned long pmdval; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1206 | unsigned long exit_phys; |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1207 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1208 | /* Page table for cores to come back up safely. */ |
| 1209 | pc_pgd = pgd_alloc(&init_mm); |
| 1210 | if (!pc_pgd) |
| 1211 | return -ENOMEM; |
| 1212 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1213 | exit_phys = virt_to_phys(msm_pm_collapse_exit); |
| 1214 | |
| 1215 | pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys),exit_phys), |
| 1216 | exit_phys); |
| 1217 | pmdval = (exit_phys & PGDIR_MASK) | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1218 | PMD_TYPE_SECT | PMD_SECT_AP_WRITE; |
| 1219 | pmd[0] = __pmd(pmdval); |
| 1220 | pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1))); |
| 1221 | |
Steve Muckle | fcece05 | 2012-02-18 20:09:58 -0800 | [diff] [blame] | 1222 | msm_saved_state_phys = |
| 1223 | allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE * |
| 1224 | num_possible_cpus(), 4); |
| 1225 | if (!msm_saved_state_phys) |
| 1226 | return -ENOMEM; |
| 1227 | msm_saved_state = ioremap_nocache(msm_saved_state_phys, |
| 1228 | CPU_SAVED_STATE_SIZE * |
| 1229 | num_possible_cpus()); |
| 1230 | if (!msm_saved_state) |
| 1231 | return -ENOMEM; |
| 1232 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1233 | /* It is remotely possible that the code in msm_pm_collapse_exit() |
| 1234 | * which turns on the MMU with this mapping is in the |
| 1235 | * next even-numbered megabyte beyond the |
| 1236 | * start of msm_pm_collapse_exit(). |
| 1237 | * Map this megabyte in as well. |
| 1238 | */ |
| 1239 | pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1))); |
| 1240 | flush_pmd_entry(pmd); |
| 1241 | msm_pm_pc_pgd = virt_to_phys(pc_pgd); |
Steve Muckle | 730ad7a | 2012-02-21 15:26:37 -0800 | [diff] [blame] | 1242 | clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd), |
| 1243 | virt_to_phys(&msm_pm_pc_pgd)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1244 | |
Praveen Chidambaram | ede45d0 | 2013-02-20 17:45:21 -0700 | [diff] [blame] | 1245 | return 0; |
| 1246 | } |
| 1247 | core_initcall(msm_pm_setup_saved_state); |
| 1248 | |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1249 | static void setup_broadcast_timer(void *arg) |
| 1250 | { |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1251 | int cpu = smp_processor_id(); |
| 1252 | |
Mahesh Sivasubramanian | 738eaf6 | 2013-04-30 12:42:59 -0600 | [diff] [blame] | 1253 | clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu); |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1254 | } |
| 1255 | |
| 1256 | static int setup_broadcast_cpuhp_notify(struct notifier_block *n, |
| 1257 | unsigned long action, void *hcpu) |
| 1258 | { |
Mahesh Sivasubramanian | 738eaf6 | 2013-04-30 12:42:59 -0600 | [diff] [blame] | 1259 | int cpu = (unsigned long)hcpu; |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1260 | |
| 1261 | switch (action & ~CPU_TASKS_FROZEN) { |
| 1262 | case CPU_ONLINE: |
Mahesh Sivasubramanian | 738eaf6 | 2013-04-30 12:42:59 -0600 | [diff] [blame] | 1263 | smp_call_function_single(cpu, setup_broadcast_timer, NULL, 1); |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1264 | break; |
| 1265 | } |
| 1266 | |
| 1267 | return NOTIFY_OK; |
| 1268 | } |
| 1269 | |
| 1270 | static struct notifier_block setup_broadcast_notifier = { |
| 1271 | .notifier_call = setup_broadcast_cpuhp_notify, |
| 1272 | }; |
| 1273 | |
Praveen Chidambaram | ede45d0 | 2013-02-20 17:45:21 -0700 | [diff] [blame] | 1274 | static int __init msm_pm_init(void) |
| 1275 | { |
| 1276 | enum msm_pm_time_stats_id enable_stats[] = { |
| 1277 | MSM_PM_STAT_IDLE_WFI, |
| 1278 | MSM_PM_STAT_RETENTION, |
| 1279 | MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE, |
| 1280 | MSM_PM_STAT_IDLE_POWER_COLLAPSE, |
| 1281 | MSM_PM_STAT_SUSPEND, |
| 1282 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1283 | msm_pm_mode_sysfs_add(); |
Praveen Chidambaram | 3895bde | 2012-05-14 19:42:40 +0530 | [diff] [blame] | 1284 | msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1285 | suspend_set_ops(&msm_pm_ops); |
Girish Mahadevan | dc318fd | 2012-08-17 16:48:05 -0600 | [diff] [blame] | 1286 | hrtimer_init(&pm_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1287 | msm_cpuidle_init(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1288 | |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1289 | if (msm_pm_pc_reset_timer) { |
Mahesh Sivasubramanian | 738eaf6 | 2013-04-30 12:42:59 -0600 | [diff] [blame] | 1290 | on_each_cpu(setup_broadcast_timer, NULL, 1); |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1291 | register_cpu_notifier(&setup_broadcast_notifier); |
| 1292 | } |
| 1293 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1294 | return 0; |
| 1295 | } |
| 1296 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1297 | static void __devinit msm_pm_set_flush_fn(uint32_t pc_mode) |
| 1298 | { |
| 1299 | msm_pm_disable_l2_fn = NULL; |
| 1300 | msm_pm_enable_l2_fn = NULL; |
| 1301 | msm_pm_flush_l2_fn = outer_flush_all; |
| 1302 | |
| 1303 | if (pc_mode == MSM_PM_PC_NOTZ_L2_EXT) { |
| 1304 | msm_pm_disable_l2_fn = outer_disable; |
| 1305 | msm_pm_enable_l2_fn = outer_resume; |
| 1306 | } |
| 1307 | } |
| 1308 | |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1309 | struct msm_pc_debug_counters_buffer { |
| 1310 | void __iomem *reg; |
| 1311 | u32 len; |
| 1312 | char buf[MAX_BUF_SIZE]; |
| 1313 | }; |
| 1314 | |
| 1315 | static inline u32 msm_pc_debug_counters_read_register( |
| 1316 | void __iomem *reg, int index , int offset) |
| 1317 | { |
| 1318 | return readl_relaxed(reg + (index * 4 + offset) * 4); |
| 1319 | } |
| 1320 | |
| 1321 | static char *counter_name[] = { |
| 1322 | "PC Entry Counter", |
| 1323 | "Warmboot Entry Counter", |
| 1324 | "PC Bailout Counter" |
| 1325 | }; |
| 1326 | |
| 1327 | static int msm_pc_debug_counters_copy( |
| 1328 | struct msm_pc_debug_counters_buffer *data) |
| 1329 | { |
| 1330 | int j; |
| 1331 | u32 stat; |
| 1332 | unsigned int cpu; |
| 1333 | |
| 1334 | for_each_possible_cpu(cpu) { |
| 1335 | data->len += scnprintf(data->buf + data->len, |
| 1336 | sizeof(data->buf)-data->len, |
| 1337 | "CPU%d\n", cpu); |
| 1338 | |
| 1339 | for (j = 0; j < NUM_OF_COUNTERS; j++) { |
| 1340 | stat = msm_pc_debug_counters_read_register( |
| 1341 | data->reg, cpu, j); |
| 1342 | data->len += scnprintf(data->buf + data->len, |
| 1343 | sizeof(data->buf)-data->len, |
| 1344 | "\t%s : %d\n", counter_name[j], |
| 1345 | stat); |
| 1346 | } |
| 1347 | |
| 1348 | } |
| 1349 | |
| 1350 | return data->len; |
| 1351 | } |
| 1352 | |
| 1353 | static int msm_pc_debug_counters_file_read(struct file *file, |
| 1354 | char __user *bufu, size_t count, loff_t *ppos) |
| 1355 | { |
| 1356 | struct msm_pc_debug_counters_buffer *data; |
| 1357 | |
| 1358 | data = file->private_data; |
| 1359 | |
| 1360 | if (!data) |
| 1361 | return -EINVAL; |
| 1362 | |
Mahesh Sivasubramanian | 6900fdc | 2013-04-18 14:02:52 -0600 | [diff] [blame] | 1363 | if (!bufu) |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1364 | return -EINVAL; |
| 1365 | |
| 1366 | if (!access_ok(VERIFY_WRITE, bufu, count)) |
| 1367 | return -EFAULT; |
| 1368 | |
| 1369 | if (*ppos >= data->len && data->len == 0) |
| 1370 | data->len = msm_pc_debug_counters_copy(data); |
| 1371 | |
| 1372 | return simple_read_from_buffer(bufu, count, ppos, |
| 1373 | data->buf, data->len); |
| 1374 | } |
| 1375 | |
| 1376 | static int msm_pc_debug_counters_file_open(struct inode *inode, |
| 1377 | struct file *file) |
| 1378 | { |
| 1379 | struct msm_pc_debug_counters_buffer *buf; |
| 1380 | void __iomem *msm_pc_debug_counters_reg; |
| 1381 | |
| 1382 | msm_pc_debug_counters_reg = inode->i_private; |
| 1383 | |
| 1384 | if (!msm_pc_debug_counters_reg) |
| 1385 | return -EINVAL; |
| 1386 | |
| 1387 | file->private_data = kzalloc( |
| 1388 | sizeof(struct msm_pc_debug_counters_buffer), GFP_KERNEL); |
| 1389 | |
| 1390 | if (!file->private_data) { |
| 1391 | pr_err("%s: ERROR kmalloc failed to allocate %d bytes\n", |
| 1392 | __func__, sizeof(struct msm_pc_debug_counters_buffer)); |
| 1393 | |
| 1394 | return -ENOMEM; |
| 1395 | } |
| 1396 | |
| 1397 | buf = file->private_data; |
| 1398 | buf->reg = msm_pc_debug_counters_reg; |
| 1399 | |
| 1400 | return 0; |
| 1401 | } |
| 1402 | |
| 1403 | static int msm_pc_debug_counters_file_close(struct inode *inode, |
| 1404 | struct file *file) |
| 1405 | { |
| 1406 | kfree(file->private_data); |
| 1407 | return 0; |
| 1408 | } |
| 1409 | |
| 1410 | static const struct file_operations msm_pc_debug_counters_fops = { |
| 1411 | .open = msm_pc_debug_counters_file_open, |
| 1412 | .read = msm_pc_debug_counters_file_read, |
| 1413 | .release = msm_pc_debug_counters_file_close, |
| 1414 | .llseek = no_llseek, |
| 1415 | }; |
| 1416 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1417 | static int __devinit msm_pm_8x60_probe(struct platform_device *pdev) |
| 1418 | { |
| 1419 | char *key = NULL; |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1420 | struct dentry *dent = NULL; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1421 | struct resource *res = NULL; |
| 1422 | int i ; |
| 1423 | struct msm_pm_init_data_type pdata_local; |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1424 | int ret = 0; |
| 1425 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1426 | memset(&pdata_local, 0, sizeof(struct msm_pm_init_data_type)); |
| 1427 | |
| 1428 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1429 | if (res) { |
| 1430 | msm_pc_debug_counters_phys = res->start; |
| 1431 | WARN_ON(resource_size(res) < SZ_64); |
| 1432 | msm_pc_debug_counters = devm_ioremap(&pdev->dev, res->start, |
| 1433 | resource_size(res)); |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1434 | if (msm_pc_debug_counters) |
| 1435 | for (i = 0; i < resource_size(res)/4; i++) |
| 1436 | __raw_writel(0, msm_pc_debug_counters + i * 4); |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1437 | |
| 1438 | } |
| 1439 | |
| 1440 | if (!msm_pc_debug_counters) { |
| 1441 | msm_pc_debug_counters = 0; |
| 1442 | msm_pc_debug_counters_phys = 0; |
Priyanka Mathur | 13bdad1 | 2013-01-28 15:52:56 -0800 | [diff] [blame] | 1443 | } else { |
| 1444 | dent = debugfs_create_file("pc_debug_counter", S_IRUGO, NULL, |
| 1445 | msm_pc_debug_counters, |
| 1446 | &msm_pc_debug_counters_fops); |
| 1447 | if (!dent) |
| 1448 | pr_err("%s: ERROR debugfs_create_file failed\n", |
| 1449 | __func__); |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1452 | if (!pdev->dev.of_node) { |
| 1453 | struct msm_pm_init_data_type *d = pdev->dev.platform_data; |
| 1454 | |
| 1455 | if (!d) |
| 1456 | goto pm_8x60_probe_done; |
| 1457 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1458 | memcpy(&pdata_local, d, sizeof(struct msm_pm_init_data_type)); |
| 1459 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1460 | } else { |
| 1461 | key = "qcom,pc-mode"; |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 1462 | ret = msm_pm_get_pc_mode(pdev->dev.of_node, |
| 1463 | key, |
| 1464 | &pdata_local.pc_mode); |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1465 | if (ret) { |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 1466 | pr_debug("%s: Error reading key %s", |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1467 | __func__, key); |
Archana Sathyakumar | 2b91dc8 | 2013-02-01 17:38:23 -0700 | [diff] [blame] | 1468 | return -EINVAL; |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1469 | } |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1470 | |
| 1471 | key = "qcom,use-sync-timer"; |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1472 | pdata_local.use_sync_timer = |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1473 | of_property_read_bool(pdev->dev.of_node, key); |
Praveen Chidambaram | 4b8df03 | 2013-01-18 16:21:16 -0700 | [diff] [blame] | 1474 | |
| 1475 | key = "qcom,saw-turns-off-pll"; |
| 1476 | msm_no_ramp_down_pc = of_property_read_bool(pdev->dev.of_node, |
| 1477 | key); |
Mahesh Sivasubramanian | b7bda88 | 2013-04-08 09:55:48 -0600 | [diff] [blame] | 1478 | |
| 1479 | key = "qcom,pc-resets-timer"; |
| 1480 | msm_pm_pc_reset_timer = of_property_read_bool( |
| 1481 | pdev->dev.of_node, key); |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1482 | } |
| 1483 | |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1484 | if (pdata_local.cp15_data.reg_data && |
| 1485 | pdata_local.cp15_data.reg_saved_state_size > 0) { |
| 1486 | cp15_data.reg_data = kzalloc(sizeof(uint32_t) * |
| 1487 | pdata_local.cp15_data.reg_saved_state_size, |
| 1488 | GFP_KERNEL); |
| 1489 | if (!cp15_data.reg_data) |
| 1490 | return -ENOMEM; |
| 1491 | |
| 1492 | cp15_data.reg_val = kzalloc(sizeof(uint32_t) * |
| 1493 | pdata_local.cp15_data.reg_saved_state_size, |
| 1494 | GFP_KERNEL); |
| 1495 | if (cp15_data.reg_val) |
| 1496 | return -ENOMEM; |
| 1497 | |
| 1498 | memcpy(cp15_data.reg_data, pdata_local.cp15_data.reg_data, |
| 1499 | pdata_local.cp15_data.reg_saved_state_size * |
| 1500 | sizeof(uint32_t)); |
| 1501 | } |
| 1502 | |
| 1503 | msm_pm_set_flush_fn(pdata_local.pc_mode); |
| 1504 | msm_pm_use_sync_timer = pdata_local.use_sync_timer; |
| 1505 | msm_pm_retention_calls_tz = pdata_local.retention_calls_tz; |
| 1506 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1507 | pm_8x60_probe_done: |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1508 | msm_pm_init(); |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 1509 | if (pdev->dev.of_node) |
| 1510 | of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev); |
| 1511 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1512 | return ret; |
| 1513 | } |
| 1514 | |
| 1515 | static struct of_device_id msm_pm_8x60_table[] = { |
| 1516 | {.compatible = "qcom,pm-8x60"}, |
| 1517 | {}, |
| 1518 | }; |
| 1519 | |
| 1520 | static struct platform_driver msm_pm_8x60_driver = { |
| 1521 | .probe = msm_pm_8x60_probe, |
| 1522 | .driver = { |
| 1523 | .name = "pm-8x60", |
| 1524 | .owner = THIS_MODULE, |
| 1525 | .of_match_table = msm_pm_8x60_table, |
| 1526 | }, |
| 1527 | }; |
| 1528 | |
| 1529 | static int __init msm_pm_8x60_init(void) |
| 1530 | { |
Anji Jonnala | 02dac8d | 2013-03-06 21:31:04 +0530 | [diff] [blame] | 1531 | int rc; |
| 1532 | |
| 1533 | rc = platform_driver_register(&msm_cpu_status_driver); |
| 1534 | |
| 1535 | if (rc) { |
| 1536 | pr_err("%s(): failed to register driver %s\n", __func__, |
| 1537 | msm_cpu_status_driver.driver.name); |
| 1538 | return rc; |
| 1539 | } |
| 1540 | |
Girish Mahadevan | 5594499 | 2012-10-26 11:03:07 -0600 | [diff] [blame] | 1541 | return platform_driver_register(&msm_pm_8x60_driver); |
| 1542 | } |
Praveen Chidambaram | f27a515 | 2013-02-01 11:44:53 -0700 | [diff] [blame] | 1543 | device_initcall(msm_pm_8x60_init); |