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