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