Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/completion.h> |
| 18 | #include <linux/cpuidle.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/ktime.h> |
| 22 | #include <linux/pm.h> |
| 23 | #include <linux/pm_qos_params.h> |
| 24 | #include <linux/proc_fs.h> |
| 25 | #include <linux/smp.h> |
| 26 | #include <linux/suspend.h> |
| 27 | #include <linux/tick.h> |
| 28 | #include <linux/uaccess.h> |
| 29 | #include <linux/wakelock.h> |
| 30 | #include <mach/msm_iomap.h> |
| 31 | #include <mach/system.h> |
| 32 | #include <asm/cacheflush.h> |
| 33 | #include <asm/hardware/gic.h> |
| 34 | #include <asm/pgtable.h> |
| 35 | #include <asm/pgalloc.h> |
| 36 | #ifdef CONFIG_VFP |
| 37 | #include <asm/vfp.h> |
| 38 | #endif |
| 39 | |
| 40 | #include "acpuclock.h" |
| 41 | #include "clock.h" |
| 42 | #include "avs.h" |
| 43 | #include "cpuidle.h" |
| 44 | #include "idle.h" |
| 45 | #include "pm.h" |
| 46 | #include "rpm_resources.h" |
| 47 | #include "scm-boot.h" |
| 48 | #include "spm.h" |
| 49 | #include "timer.h" |
Pratik Patel | e577179 | 2011-09-17 18:33:54 -0700 | [diff] [blame] | 50 | #include "qdss.h" |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame^] | 51 | #include "pm-boot.h" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 52 | |
| 53 | /****************************************************************************** |
| 54 | * Debug Definitions |
| 55 | *****************************************************************************/ |
| 56 | |
| 57 | enum { |
| 58 | MSM_PM_DEBUG_SUSPEND = BIT(0), |
| 59 | MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1), |
| 60 | MSM_PM_DEBUG_SUSPEND_LIMITS = BIT(2), |
| 61 | MSM_PM_DEBUG_CLOCK = BIT(3), |
| 62 | MSM_PM_DEBUG_RESET_VECTOR = BIT(4), |
| 63 | MSM_PM_DEBUG_IDLE = BIT(6), |
| 64 | MSM_PM_DEBUG_IDLE_LIMITS = BIT(7), |
| 65 | MSM_PM_DEBUG_HOTPLUG = BIT(8), |
| 66 | }; |
| 67 | |
| 68 | static int msm_pm_debug_mask = 1; |
| 69 | module_param_named( |
| 70 | debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP |
| 71 | ); |
| 72 | |
| 73 | |
| 74 | /****************************************************************************** |
| 75 | * Sleep Modes and Parameters |
| 76 | *****************************************************************************/ |
| 77 | |
| 78 | static struct msm_pm_platform_data *msm_pm_modes; |
| 79 | static int rpm_cpu0_wakeup_irq; |
| 80 | |
| 81 | void __init msm_pm_set_platform_data( |
| 82 | struct msm_pm_platform_data *data, int count) |
| 83 | { |
| 84 | BUG_ON(MSM_PM_SLEEP_MODE_NR * num_possible_cpus() > count); |
| 85 | msm_pm_modes = data; |
| 86 | } |
| 87 | |
| 88 | void __init msm_pm_set_rpm_wakeup_irq(unsigned int irq) |
| 89 | { |
| 90 | rpm_cpu0_wakeup_irq = irq; |
| 91 | } |
| 92 | |
| 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 | |
| 109 | #define GET_CPU_OF_ATTR(attr) \ |
| 110 | (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu) |
| 111 | |
| 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", |
| 122 | [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] = |
| 123 | "standalone_power_collapse", |
| 124 | }; |
| 125 | |
| 126 | /* |
| 127 | * Write out the attribute. |
| 128 | */ |
| 129 | static ssize_t msm_pm_mode_attr_show( |
| 130 | struct kobject *kobj, struct kobj_attribute *attr, char *buf) |
| 131 | { |
| 132 | int ret = -EINVAL; |
| 133 | int i; |
| 134 | |
| 135 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 136 | struct kernel_param kp; |
| 137 | unsigned int cpu; |
| 138 | struct msm_pm_platform_data *mode; |
| 139 | |
| 140 | if (msm_pm_sleep_mode_labels[i] == NULL) |
| 141 | continue; |
| 142 | |
| 143 | if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i])) |
| 144 | continue; |
| 145 | |
| 146 | cpu = GET_CPU_OF_ATTR(attr); |
| 147 | mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)]; |
| 148 | |
| 149 | if (!strcmp(attr->attr.name, |
| 150 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 151 | u32 arg = mode->suspend_enabled; |
| 152 | kp.arg = &arg; |
| 153 | ret = param_get_ulong(buf, &kp); |
| 154 | } else if (!strcmp(attr->attr.name, |
| 155 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 156 | u32 arg = mode->idle_enabled; |
| 157 | kp.arg = &arg; |
| 158 | ret = param_get_ulong(buf, &kp); |
| 159 | } |
| 160 | |
| 161 | break; |
| 162 | } |
| 163 | |
| 164 | if (ret > 0) { |
| 165 | strcat(buf, "\n"); |
| 166 | ret++; |
| 167 | } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Read in the new attribute value. |
| 174 | */ |
| 175 | static ssize_t msm_pm_mode_attr_store(struct kobject *kobj, |
| 176 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 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); |
| 193 | mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)]; |
| 194 | |
| 195 | if (!strcmp(attr->attr.name, |
| 196 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) { |
| 197 | kp.arg = &mode->suspend_enabled; |
| 198 | ret = param_set_byte(buf, &kp); |
| 199 | } else if (!strcmp(attr->attr.name, |
| 200 | msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) { |
| 201 | kp.arg = &mode->idle_enabled; |
| 202 | ret = param_set_byte(buf, &kp); |
| 203 | } |
| 204 | |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | return ret ? ret : count; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Add sysfs entries for one cpu. |
| 213 | */ |
| 214 | static int __init msm_pm_mode_sysfs_add_cpu( |
| 215 | unsigned int cpu, struct kobject *modes_kobj) |
| 216 | { |
| 217 | char cpu_name[8]; |
| 218 | struct kobject *cpu_kobj; |
| 219 | struct msm_pm_sysfs_sleep_mode *mode; |
| 220 | int i, j, k; |
| 221 | int ret; |
| 222 | |
| 223 | snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu); |
| 224 | cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj); |
| 225 | if (!cpu_kobj) { |
| 226 | pr_err("%s: cannot create %s kobject\n", __func__, cpu_name); |
| 227 | ret = -ENOMEM; |
| 228 | goto mode_sysfs_add_cpu_exit; |
| 229 | } |
| 230 | |
| 231 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 232 | int idx = MSM_PM_MODE(cpu, i); |
| 233 | |
| 234 | if ((!msm_pm_modes[idx].suspend_supported) |
| 235 | && (!msm_pm_modes[idx].idle_supported)) |
| 236 | continue; |
| 237 | |
| 238 | mode = kzalloc(sizeof(*mode), GFP_KERNEL); |
| 239 | if (!mode) { |
| 240 | pr_err("%s: cannot allocate memory for attributes\n", |
| 241 | __func__); |
| 242 | ret = -ENOMEM; |
| 243 | goto mode_sysfs_add_cpu_exit; |
| 244 | } |
| 245 | |
| 246 | mode->kobj = kobject_create_and_add( |
| 247 | msm_pm_sleep_mode_labels[i], cpu_kobj); |
| 248 | if (!mode->kobj) { |
| 249 | pr_err("%s: cannot create kobject\n", __func__); |
| 250 | ret = -ENOMEM; |
| 251 | goto mode_sysfs_add_cpu_exit; |
| 252 | } |
| 253 | |
| 254 | for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) { |
| 255 | if ((k == MSM_PM_MODE_ATTR_IDLE) && |
| 256 | !msm_pm_modes[idx].idle_supported) |
| 257 | continue; |
| 258 | if ((k == MSM_PM_MODE_ATTR_SUSPEND) && |
| 259 | !msm_pm_modes[idx].suspend_supported) |
| 260 | continue; |
| 261 | mode->kas[j].cpu = cpu; |
| 262 | mode->kas[j].ka.attr.mode = 0644; |
| 263 | mode->kas[j].ka.show = msm_pm_mode_attr_show; |
| 264 | mode->kas[j].ka.store = msm_pm_mode_attr_store; |
| 265 | mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k]; |
| 266 | mode->attrs[j] = &mode->kas[j].ka.attr; |
| 267 | j++; |
| 268 | } |
| 269 | mode->attrs[j] = NULL; |
| 270 | |
| 271 | mode->attr_group.attrs = mode->attrs; |
| 272 | ret = sysfs_create_group(mode->kobj, &mode->attr_group); |
| 273 | if (ret) { |
| 274 | pr_err("%s: cannot create kobject attribute group\n", |
| 275 | __func__); |
| 276 | goto mode_sysfs_add_cpu_exit; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | ret = 0; |
| 281 | |
| 282 | mode_sysfs_add_cpu_exit: |
Praveen Chidambaram | d5ac2d3 | 2011-10-24 14:30:27 -0600 | [diff] [blame] | 283 | if (ret) { |
Praveen Chidambaram | 2cfda63 | 2011-10-11 16:58:09 -0600 | [diff] [blame] | 284 | if (mode && mode->kobj) |
| 285 | kobject_del(mode->kobj); |
| 286 | kfree(mode); |
| 287 | } |
| 288 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Add sysfs entries for the sleep modes. |
| 294 | */ |
| 295 | static int __init msm_pm_mode_sysfs_add(void) |
| 296 | { |
| 297 | struct kobject *module_kobj; |
| 298 | struct kobject *modes_kobj; |
| 299 | unsigned int cpu; |
| 300 | int ret; |
| 301 | |
| 302 | module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME); |
| 303 | if (!module_kobj) { |
| 304 | pr_err("%s: cannot find kobject for module %s\n", |
| 305 | __func__, KBUILD_MODNAME); |
| 306 | ret = -ENOENT; |
| 307 | goto mode_sysfs_add_exit; |
| 308 | } |
| 309 | |
| 310 | modes_kobj = kobject_create_and_add("modes", module_kobj); |
| 311 | if (!modes_kobj) { |
| 312 | pr_err("%s: cannot create modes kobject\n", __func__); |
| 313 | ret = -ENOMEM; |
| 314 | goto mode_sysfs_add_exit; |
| 315 | } |
| 316 | |
| 317 | for_each_possible_cpu(cpu) { |
| 318 | ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj); |
| 319 | if (ret) |
| 320 | goto mode_sysfs_add_exit; |
| 321 | } |
| 322 | |
| 323 | ret = 0; |
| 324 | |
| 325 | mode_sysfs_add_exit: |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | /****************************************************************************** |
| 330 | * CONFIG_MSM_IDLE_STATS |
| 331 | *****************************************************************************/ |
| 332 | |
| 333 | #ifdef CONFIG_MSM_IDLE_STATS |
| 334 | enum msm_pm_time_stats_id { |
| 335 | MSM_PM_STAT_REQUESTED_IDLE, |
| 336 | MSM_PM_STAT_IDLE_WFI, |
| 337 | MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE, |
| 338 | MSM_PM_STAT_IDLE_POWER_COLLAPSE, |
| 339 | MSM_PM_STAT_SUSPEND, |
| 340 | MSM_PM_STAT_COUNT |
| 341 | }; |
| 342 | |
| 343 | struct msm_pm_time_stats { |
| 344 | const char *name; |
| 345 | int64_t first_bucket_time; |
| 346 | int bucket[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT]; |
| 347 | int64_t min_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT]; |
| 348 | int64_t max_time[CONFIG_MSM_IDLE_STATS_BUCKET_COUNT]; |
| 349 | int count; |
| 350 | int64_t total_time; |
| 351 | }; |
| 352 | |
| 353 | struct msm_pm_cpu_time_stats { |
| 354 | struct msm_pm_time_stats stats[MSM_PM_STAT_COUNT]; |
| 355 | }; |
| 356 | |
| 357 | static DEFINE_SPINLOCK(msm_pm_stats_lock); |
| 358 | static DEFINE_PER_CPU_SHARED_ALIGNED( |
| 359 | struct msm_pm_cpu_time_stats, msm_pm_stats); |
| 360 | |
| 361 | /* |
| 362 | * Add the given time data to the statistics collection. |
| 363 | */ |
| 364 | static void msm_pm_add_stat(enum msm_pm_time_stats_id id, int64_t t) |
| 365 | { |
| 366 | unsigned long flags; |
| 367 | struct msm_pm_time_stats *stats; |
| 368 | int64_t bt; |
| 369 | int i; |
| 370 | |
| 371 | spin_lock_irqsave(&msm_pm_stats_lock, flags); |
| 372 | stats = __get_cpu_var(msm_pm_stats).stats; |
| 373 | |
| 374 | stats[id].total_time += t; |
| 375 | stats[id].count++; |
| 376 | |
| 377 | bt = t; |
| 378 | do_div(bt, stats[id].first_bucket_time); |
| 379 | |
| 380 | if (bt < 1ULL << (CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT * |
| 381 | (CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1))) |
| 382 | i = DIV_ROUND_UP(fls((uint32_t)bt), |
| 383 | CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT); |
| 384 | else |
| 385 | i = CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; |
| 386 | |
| 387 | stats[id].bucket[i]++; |
| 388 | |
| 389 | if (t < stats[id].min_time[i] || !stats[id].max_time[i]) |
| 390 | stats[id].min_time[i] = t; |
| 391 | if (t > stats[id].max_time[i]) |
| 392 | stats[id].max_time[i] = t; |
| 393 | |
| 394 | spin_unlock_irqrestore(&msm_pm_stats_lock, flags); |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Helper function of snprintf where buf is auto-incremented, size is auto- |
| 399 | * decremented, and there is no return value. |
| 400 | * |
| 401 | * NOTE: buf and size must be l-values (e.g. variables) |
| 402 | */ |
| 403 | #define SNPRINTF(buf, size, format, ...) \ |
| 404 | do { \ |
| 405 | if (size > 0) { \ |
| 406 | int ret; \ |
| 407 | ret = snprintf(buf, size, format, ## __VA_ARGS__); \ |
| 408 | if (ret > size) { \ |
| 409 | buf += size; \ |
| 410 | size = 0; \ |
| 411 | } else { \ |
| 412 | buf += ret; \ |
| 413 | size -= ret; \ |
| 414 | } \ |
| 415 | } \ |
| 416 | } while (0) |
| 417 | |
| 418 | /* |
| 419 | * Write out the power management statistics. |
| 420 | */ |
| 421 | static int msm_pm_read_proc |
| 422 | (char *page, char **start, off_t off, int count, int *eof, void *data) |
| 423 | { |
| 424 | unsigned int cpu = off / MSM_PM_STAT_COUNT; |
| 425 | int id = off % MSM_PM_STAT_COUNT; |
| 426 | char *p = page; |
| 427 | |
| 428 | if (count < 1024) { |
| 429 | *start = (char *) 0; |
| 430 | *eof = 0; |
| 431 | return 0; |
| 432 | } |
| 433 | |
| 434 | if (cpu < num_possible_cpus()) { |
| 435 | unsigned long flags; |
| 436 | struct msm_pm_time_stats *stats; |
| 437 | int i; |
| 438 | int64_t bucket_time; |
| 439 | int64_t s; |
| 440 | uint32_t ns; |
| 441 | |
| 442 | spin_lock_irqsave(&msm_pm_stats_lock, flags); |
| 443 | stats = per_cpu(msm_pm_stats, cpu).stats; |
| 444 | |
| 445 | s = stats[id].total_time; |
| 446 | ns = do_div(s, NSEC_PER_SEC); |
| 447 | SNPRINTF(p, count, |
| 448 | "[cpu %u] %s:\n" |
| 449 | " count: %7d\n" |
| 450 | " total_time: %lld.%09u\n", |
| 451 | cpu, stats[id].name, |
| 452 | stats[id].count, |
| 453 | s, ns); |
| 454 | |
| 455 | bucket_time = stats[id].first_bucket_time; |
| 456 | for (i = 0; i < CONFIG_MSM_IDLE_STATS_BUCKET_COUNT - 1; i++) { |
| 457 | s = bucket_time; |
| 458 | ns = do_div(s, NSEC_PER_SEC); |
| 459 | SNPRINTF(p, count, |
| 460 | " <%6lld.%09u: %7d (%lld-%lld)\n", |
| 461 | s, ns, stats[id].bucket[i], |
| 462 | stats[id].min_time[i], |
| 463 | stats[id].max_time[i]); |
| 464 | |
| 465 | bucket_time <<= CONFIG_MSM_IDLE_STATS_BUCKET_SHIFT; |
| 466 | } |
| 467 | |
| 468 | SNPRINTF(p, count, " >=%6lld.%09u: %7d (%lld-%lld)\n", |
| 469 | s, ns, stats[id].bucket[i], |
| 470 | stats[id].min_time[i], |
| 471 | stats[id].max_time[i]); |
| 472 | |
| 473 | *start = (char *) 1; |
| 474 | *eof = (off + 1 >= MSM_PM_STAT_COUNT * num_possible_cpus()); |
| 475 | |
| 476 | spin_unlock_irqrestore(&msm_pm_stats_lock, flags); |
| 477 | } |
| 478 | |
| 479 | return p - page; |
| 480 | } |
| 481 | #undef SNPRINTF |
| 482 | |
| 483 | #define MSM_PM_STATS_RESET "reset" |
| 484 | |
| 485 | /* |
| 486 | * Reset the power management statistics values. |
| 487 | */ |
| 488 | static int msm_pm_write_proc(struct file *file, const char __user *buffer, |
| 489 | unsigned long count, void *data) |
| 490 | { |
| 491 | char buf[sizeof(MSM_PM_STATS_RESET)]; |
| 492 | int ret; |
| 493 | unsigned long flags; |
| 494 | unsigned int cpu; |
| 495 | |
| 496 | if (count < strlen(MSM_PM_STATS_RESET)) { |
| 497 | ret = -EINVAL; |
| 498 | goto write_proc_failed; |
| 499 | } |
| 500 | |
| 501 | if (copy_from_user(buf, buffer, strlen(MSM_PM_STATS_RESET))) { |
| 502 | ret = -EFAULT; |
| 503 | goto write_proc_failed; |
| 504 | } |
| 505 | |
| 506 | if (memcmp(buf, MSM_PM_STATS_RESET, strlen(MSM_PM_STATS_RESET))) { |
| 507 | ret = -EINVAL; |
| 508 | goto write_proc_failed; |
| 509 | } |
| 510 | |
| 511 | spin_lock_irqsave(&msm_pm_stats_lock, flags); |
| 512 | for_each_possible_cpu(cpu) { |
| 513 | struct msm_pm_time_stats *stats; |
| 514 | int i; |
| 515 | |
| 516 | stats = per_cpu(msm_pm_stats, cpu).stats; |
| 517 | for (i = 0; i < MSM_PM_STAT_COUNT; i++) { |
| 518 | memset(stats[i].bucket, |
| 519 | 0, sizeof(stats[i].bucket)); |
| 520 | memset(stats[i].min_time, |
| 521 | 0, sizeof(stats[i].min_time)); |
| 522 | memset(stats[i].max_time, |
| 523 | 0, sizeof(stats[i].max_time)); |
| 524 | stats[i].count = 0; |
| 525 | stats[i].total_time = 0; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | spin_unlock_irqrestore(&msm_pm_stats_lock, flags); |
| 530 | return count; |
| 531 | |
| 532 | write_proc_failed: |
| 533 | return ret; |
| 534 | } |
| 535 | #undef MSM_PM_STATS_RESET |
| 536 | #endif /* CONFIG_MSM_IDLE_STATS */ |
| 537 | |
| 538 | |
| 539 | /****************************************************************************** |
| 540 | * Configure Hardware before/after Low Power Mode |
| 541 | *****************************************************************************/ |
| 542 | |
| 543 | /* |
| 544 | * Configure hardware registers in preparation for Apps power down. |
| 545 | */ |
| 546 | static void msm_pm_config_hw_before_power_down(void) |
| 547 | { |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | /* |
| 552 | * Clear hardware registers after Apps powers up. |
| 553 | */ |
| 554 | static void msm_pm_config_hw_after_power_up(void) |
| 555 | { |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Configure hardware registers in preparation for SWFI. |
| 561 | */ |
| 562 | static void msm_pm_config_hw_before_swfi(void) |
| 563 | { |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | |
| 568 | /****************************************************************************** |
| 569 | * Suspend Max Sleep Time |
| 570 | *****************************************************************************/ |
| 571 | |
| 572 | #ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE |
| 573 | static int msm_pm_sleep_time_override; |
| 574 | module_param_named(sleep_time_override, |
| 575 | msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 576 | #endif |
| 577 | |
| 578 | #define SCLK_HZ (32768) |
| 579 | #define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000) |
| 580 | |
| 581 | static uint32_t msm_pm_max_sleep_time; |
| 582 | |
| 583 | /* |
| 584 | * Convert time from nanoseconds to slow clock ticks, then cap it to the |
| 585 | * specified limit |
| 586 | */ |
| 587 | static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit) |
| 588 | { |
| 589 | do_div(time_ns, NSEC_PER_SEC / SCLK_HZ); |
| 590 | return (time_ns > limit) ? limit : time_ns; |
| 591 | } |
| 592 | |
| 593 | /* |
| 594 | * Set the sleep time for suspend. 0 means infinite sleep time. |
| 595 | */ |
| 596 | void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns) |
| 597 | { |
| 598 | if (max_sleep_time_ns == 0) { |
| 599 | msm_pm_max_sleep_time = 0; |
| 600 | } else { |
| 601 | msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time( |
| 602 | max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT); |
| 603 | |
| 604 | if (msm_pm_max_sleep_time == 0) |
| 605 | msm_pm_max_sleep_time = 1; |
| 606 | } |
| 607 | |
| 608 | if (msm_pm_debug_mask & MSM_PM_DEBUG_SUSPEND) |
| 609 | pr_info("%s: Requested %lld ns Giving %u sclk ticks\n", |
| 610 | __func__, max_sleep_time_ns, msm_pm_max_sleep_time); |
| 611 | } |
| 612 | EXPORT_SYMBOL(msm_pm_set_max_sleep_time); |
| 613 | |
| 614 | |
| 615 | /****************************************************************************** |
| 616 | * |
| 617 | *****************************************************************************/ |
| 618 | |
| 619 | struct msm_pm_device { |
| 620 | unsigned int cpu; |
| 621 | #ifdef CONFIG_HOTPLUG_CPU |
| 622 | struct completion cpu_killed; |
| 623 | unsigned int warm_boot; |
| 624 | #endif |
| 625 | }; |
| 626 | |
| 627 | static DEFINE_PER_CPU_SHARED_ALIGNED(struct msm_pm_device, msm_pm_devices); |
| 628 | static struct msm_rpmrs_limits *msm_pm_idle_rs_limits; |
| 629 | |
| 630 | static void msm_pm_swfi(void) |
| 631 | { |
| 632 | msm_pm_config_hw_before_swfi(); |
| 633 | msm_arch_idle(); |
| 634 | } |
| 635 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 636 | static bool msm_pm_spm_power_collapse( |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | struct msm_pm_device *dev, bool from_idle, bool notify_rpm) |
| 638 | { |
| 639 | void *entry; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 640 | bool collapsed = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 641 | int ret; |
| 642 | |
| 643 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 644 | pr_info("CPU%u: %s: notify_rpm %d\n", |
| 645 | dev->cpu, __func__, (int) notify_rpm); |
| 646 | |
| 647 | ret = msm_spm_set_low_power_mode( |
| 648 | MSM_SPM_MODE_POWER_COLLAPSE, notify_rpm); |
| 649 | WARN_ON(ret); |
| 650 | |
| 651 | entry = (!dev->cpu || from_idle) ? |
| 652 | msm_pm_collapse_exit : msm_secondary_startup; |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame^] | 653 | msm_pm_boot_config_before_pc(dev->cpu, virt_to_phys(entry)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 654 | |
| 655 | if (MSM_PM_DEBUG_RESET_VECTOR & msm_pm_debug_mask) |
| 656 | pr_info("CPU%u: %s: program vector to %p\n", |
| 657 | dev->cpu, __func__, entry); |
| 658 | |
| 659 | #ifdef CONFIG_VFP |
| 660 | vfp_flush_context(); |
| 661 | #endif |
| 662 | |
| 663 | collapsed = msm_pm_collapse(); |
| 664 | |
Maheshkumar Sivasubramanian | 8ccc16e | 2011-10-25 15:59:57 -0600 | [diff] [blame^] | 665 | msm_pm_boot_config_after_pc(dev->cpu); |
| 666 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 667 | if (collapsed) { |
| 668 | #ifdef CONFIG_VFP |
| 669 | vfp_reinit(); |
| 670 | #endif |
| 671 | cpu_init(); |
| 672 | writel(0xF0, MSM_QGIC_CPU_BASE + GIC_CPU_PRIMASK); |
| 673 | writel(1, MSM_QGIC_CPU_BASE + GIC_CPU_CTRL); |
| 674 | local_fiq_enable(); |
| 675 | } |
| 676 | |
| 677 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 678 | pr_info("CPU%u: %s: msm_pm_collapse returned, collapsed %d\n", |
| 679 | dev->cpu, __func__, collapsed); |
| 680 | |
| 681 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 682 | WARN_ON(ret); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 683 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 686 | static bool msm_pm_power_collapse_standalone(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 687 | { |
| 688 | struct msm_pm_device *dev = &__get_cpu_var(msm_pm_devices); |
| 689 | unsigned int avsdscr_setting; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 690 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 691 | |
| 692 | avsdscr_setting = avs_get_avsdscr(); |
| 693 | avs_disable(); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 694 | collapsed = msm_pm_spm_power_collapse(dev, from_idle, false); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 695 | avs_reset_delays(avsdscr_setting); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 696 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 699 | static bool msm_pm_power_collapse(bool from_idle) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 700 | { |
| 701 | struct msm_pm_device *dev = &__get_cpu_var(msm_pm_devices); |
| 702 | unsigned long saved_acpuclk_rate; |
| 703 | unsigned int avsdscr_setting; |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 704 | bool collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 705 | |
| 706 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 707 | pr_info("CPU%u: %s: idle %d\n", |
| 708 | dev->cpu, __func__, (int)from_idle); |
| 709 | |
| 710 | msm_pm_config_hw_before_power_down(); |
| 711 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 712 | pr_info("CPU%u: %s: pre power down\n", dev->cpu, __func__); |
| 713 | |
| 714 | avsdscr_setting = avs_get_avsdscr(); |
| 715 | avs_disable(); |
| 716 | |
| 717 | if (cpu_online(dev->cpu)) |
| 718 | saved_acpuclk_rate = acpuclk_power_collapse(); |
| 719 | else |
| 720 | saved_acpuclk_rate = 0; |
| 721 | |
| 722 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 723 | pr_info("CPU%u: %s: change clock rate (old rate = %lu)\n", |
| 724 | dev->cpu, __func__, saved_acpuclk_rate); |
| 725 | |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 726 | collapsed = msm_pm_spm_power_collapse(dev, from_idle, true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 727 | |
| 728 | if (MSM_PM_DEBUG_CLOCK & msm_pm_debug_mask) |
| 729 | pr_info("CPU%u: %s: restore clock rate to %lu\n", |
| 730 | dev->cpu, __func__, saved_acpuclk_rate); |
| 731 | if (acpuclk_set_rate(dev->cpu, saved_acpuclk_rate, SETRATE_PC) < 0) |
| 732 | pr_err("CPU%u: %s: failed to restore clock rate(%lu)\n", |
| 733 | dev->cpu, __func__, saved_acpuclk_rate); |
| 734 | |
| 735 | avs_reset_delays(avsdscr_setting); |
| 736 | msm_pm_config_hw_after_power_up(); |
| 737 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 738 | pr_info("CPU%u: %s: post power up\n", dev->cpu, __func__); |
| 739 | |
| 740 | if (MSM_PM_DEBUG_POWER_COLLAPSE & msm_pm_debug_mask) |
| 741 | pr_info("CPU%u: %s: return\n", dev->cpu, __func__); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 742 | return collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | static irqreturn_t msm_pm_rpm_wakeup_interrupt(int irq, void *dev_id) |
| 746 | { |
| 747 | if (dev_id != &msm_pm_rpm_wakeup_interrupt) |
| 748 | return IRQ_NONE; |
| 749 | |
| 750 | return IRQ_HANDLED; |
| 751 | } |
| 752 | |
| 753 | |
| 754 | /****************************************************************************** |
| 755 | * External Idle/Suspend Functions |
| 756 | *****************************************************************************/ |
| 757 | |
| 758 | void arch_idle(void) |
| 759 | { |
| 760 | return; |
| 761 | } |
| 762 | |
| 763 | int msm_pm_idle_prepare(struct cpuidle_device *dev) |
| 764 | { |
| 765 | uint32_t latency_us; |
| 766 | uint32_t sleep_us; |
| 767 | int i; |
| 768 | |
| 769 | latency_us = (uint32_t) pm_qos_request(PM_QOS_CPU_DMA_LATENCY); |
| 770 | sleep_us = (uint32_t) ktime_to_ns(tick_nohz_get_sleep_length()); |
| 771 | sleep_us = DIV_ROUND_UP(sleep_us, 1000); |
| 772 | |
| 773 | for (i = 0; i < dev->state_count; i++) { |
| 774 | struct cpuidle_state *state = &dev->states[i]; |
| 775 | enum msm_pm_sleep_mode mode; |
| 776 | bool allow; |
| 777 | struct msm_rpmrs_limits *rs_limits = NULL; |
| 778 | int idx; |
| 779 | |
| 780 | mode = (enum msm_pm_sleep_mode) state->driver_data; |
| 781 | idx = MSM_PM_MODE(dev->cpu, mode); |
| 782 | |
| 783 | allow = msm_pm_modes[idx].idle_enabled && |
| 784 | msm_pm_modes[idx].idle_supported; |
| 785 | |
| 786 | switch (mode) { |
| 787 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: |
| 788 | if (!allow) |
| 789 | break; |
| 790 | |
| 791 | if (num_online_cpus() > 1) { |
| 792 | allow = false; |
| 793 | break; |
| 794 | } |
| 795 | #ifdef CONFIG_HAS_WAKELOCK |
| 796 | if (has_wake_lock(WAKE_LOCK_IDLE)) { |
| 797 | allow = false; |
| 798 | break; |
| 799 | } |
| 800 | #endif |
| 801 | /* fall through */ |
| 802 | |
| 803 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 804 | if (!allow) |
| 805 | break; |
| 806 | |
| 807 | if (!dev->cpu && |
| 808 | msm_rpm_local_request_is_outstanding()) { |
| 809 | allow = false; |
| 810 | break; |
| 811 | } |
| 812 | /* fall through */ |
| 813 | |
| 814 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 815 | if (!allow) |
| 816 | break; |
| 817 | |
| 818 | rs_limits = msm_rpmrs_lowest_limits(true, |
| 819 | mode, latency_us, sleep_us); |
| 820 | |
| 821 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 822 | pr_info("CPU%u: %s: %s, latency %uus, " |
| 823 | "sleep %uus, limit %p\n", |
| 824 | dev->cpu, __func__, state->desc, |
| 825 | latency_us, sleep_us, rs_limits); |
| 826 | |
| 827 | if ((MSM_PM_DEBUG_IDLE_LIMITS & msm_pm_debug_mask) && |
| 828 | rs_limits) |
| 829 | pr_info("CPU%u: %s: limit %p: " |
| 830 | "pxo %d, l2_cache %d, " |
| 831 | "vdd_mem %d, vdd_dig %d\n", |
| 832 | dev->cpu, __func__, rs_limits, |
| 833 | rs_limits->pxo, |
| 834 | rs_limits->l2_cache, |
| 835 | rs_limits->vdd_mem, |
| 836 | rs_limits->vdd_dig); |
| 837 | |
| 838 | if (!rs_limits) |
| 839 | allow = false; |
| 840 | break; |
| 841 | |
| 842 | default: |
| 843 | allow = false; |
| 844 | break; |
| 845 | } |
| 846 | |
| 847 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 848 | pr_info("CPU%u: %s: allow %s: %d\n", |
| 849 | dev->cpu, __func__, state->desc, (int)allow); |
| 850 | |
| 851 | if (allow) { |
| 852 | state->flags &= ~CPUIDLE_FLAG_IGNORE; |
| 853 | state->target_residency = 0; |
| 854 | state->exit_latency = 0; |
| 855 | state->power_usage = rs_limits->power[dev->cpu]; |
| 856 | |
| 857 | if (MSM_PM_SLEEP_MODE_POWER_COLLAPSE == mode) |
| 858 | msm_pm_idle_rs_limits = rs_limits; |
| 859 | } else { |
| 860 | state->flags |= CPUIDLE_FLAG_IGNORE; |
| 861 | } |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | int msm_pm_idle_enter(enum msm_pm_sleep_mode sleep_mode) |
| 868 | { |
| 869 | int64_t time; |
| 870 | #ifdef CONFIG_MSM_IDLE_STATS |
| 871 | int exit_stat; |
| 872 | #endif |
| 873 | |
| 874 | if (MSM_PM_DEBUG_IDLE & msm_pm_debug_mask) |
| 875 | pr_info("CPU%u: %s: mode %d\n", |
| 876 | smp_processor_id(), __func__, sleep_mode); |
| 877 | |
| 878 | time = ktime_to_ns(ktime_get()); |
| 879 | |
| 880 | switch (sleep_mode) { |
| 881 | case MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT: |
| 882 | msm_pm_swfi(); |
| 883 | #ifdef CONFIG_MSM_IDLE_STATS |
| 884 | exit_stat = MSM_PM_STAT_IDLE_WFI; |
| 885 | #endif |
| 886 | break; |
| 887 | |
| 888 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE: |
| 889 | msm_pm_power_collapse_standalone(true); |
| 890 | #ifdef CONFIG_MSM_IDLE_STATS |
| 891 | exit_stat = MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE; |
| 892 | #endif |
| 893 | break; |
| 894 | |
| 895 | case MSM_PM_SLEEP_MODE_POWER_COLLAPSE: { |
| 896 | int64_t timer_expiration = msm_timer_enter_idle(); |
| 897 | bool timer_halted = false; |
| 898 | uint32_t sleep_delay; |
| 899 | int ret; |
| 900 | int notify_rpm = |
| 901 | (sleep_mode == MSM_PM_SLEEP_MODE_POWER_COLLAPSE); |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 902 | int collapsed; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 903 | |
| 904 | sleep_delay = (uint32_t) msm_pm_convert_and_cap_time( |
| 905 | timer_expiration, MSM_PM_SLEEP_TICK_LIMIT); |
| 906 | if (sleep_delay == 0) /* 0 would mean infinite time */ |
| 907 | sleep_delay = 1; |
| 908 | |
| 909 | ret = msm_rpmrs_enter_sleep( |
| 910 | sleep_delay, msm_pm_idle_rs_limits, true, notify_rpm); |
| 911 | if (!ret) { |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 912 | collapsed = msm_pm_power_collapse(true); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 913 | timer_halted = true; |
| 914 | |
| 915 | msm_rpmrs_exit_sleep(msm_pm_idle_rs_limits, true, |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 916 | notify_rpm, collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | msm_timer_exit_idle((int) timer_halted); |
| 920 | #ifdef CONFIG_MSM_IDLE_STATS |
| 921 | exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE; |
| 922 | #endif |
| 923 | break; |
| 924 | } |
| 925 | |
| 926 | default: |
| 927 | __WARN(); |
| 928 | goto cpuidle_enter_bail; |
| 929 | } |
| 930 | |
| 931 | time = ktime_to_ns(ktime_get()) - time; |
| 932 | #ifdef CONFIG_MSM_IDLE_STATS |
| 933 | msm_pm_add_stat(exit_stat, time); |
| 934 | #endif |
| 935 | |
| 936 | do_div(time, 1000); |
| 937 | return (int) time; |
| 938 | |
| 939 | cpuidle_enter_bail: |
| 940 | return 0; |
| 941 | } |
| 942 | |
| 943 | static int msm_pm_enter(suspend_state_t state) |
| 944 | { |
| 945 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 946 | int i; |
| 947 | |
| 948 | #ifdef CONFIG_MSM_IDLE_STATS |
| 949 | int64_t period = 0; |
| 950 | int64_t time = msm_timer_get_sclk_time(&period); |
| 951 | #endif |
| 952 | |
| 953 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 954 | pr_info("%s\n", __func__); |
| 955 | |
| 956 | if (smp_processor_id()) { |
| 957 | __WARN(); |
| 958 | goto enter_exit; |
| 959 | } |
| 960 | |
| 961 | |
| 962 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 963 | struct msm_pm_platform_data *mode; |
| 964 | |
| 965 | mode = &msm_pm_modes[MSM_PM_MODE(0, i)]; |
| 966 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 967 | } |
| 968 | |
| 969 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) { |
| 970 | struct msm_rpmrs_limits *rs_limits; |
| 971 | int ret; |
| 972 | |
| 973 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 974 | pr_info("%s: power collapse\n", __func__); |
| 975 | |
| 976 | clock_debug_print_enabled(); |
| 977 | |
| 978 | #ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE |
| 979 | if (msm_pm_sleep_time_override > 0) { |
| 980 | int64_t ns = NSEC_PER_SEC * |
| 981 | (int64_t) msm_pm_sleep_time_override; |
| 982 | msm_pm_set_max_sleep_time(ns); |
| 983 | msm_pm_sleep_time_override = 0; |
| 984 | } |
| 985 | #endif /* CONFIG_MSM_SLEEP_TIME_OVERRIDE */ |
| 986 | |
| 987 | if (MSM_PM_DEBUG_SUSPEND_LIMITS & msm_pm_debug_mask) |
| 988 | msm_rpmrs_show_resources(); |
| 989 | |
| 990 | rs_limits = msm_rpmrs_lowest_limits(false, |
| 991 | MSM_PM_SLEEP_MODE_POWER_COLLAPSE, -1, -1); |
| 992 | |
| 993 | if ((MSM_PM_DEBUG_SUSPEND_LIMITS & msm_pm_debug_mask) && |
| 994 | rs_limits) |
| 995 | pr_info("%s: limit %p: pxo %d, l2_cache %d, " |
| 996 | "vdd_mem %d, vdd_dig %d\n", |
| 997 | __func__, rs_limits, |
| 998 | rs_limits->pxo, rs_limits->l2_cache, |
| 999 | rs_limits->vdd_mem, rs_limits->vdd_dig); |
| 1000 | |
| 1001 | if (rs_limits) { |
| 1002 | ret = msm_rpmrs_enter_sleep( |
| 1003 | msm_pm_max_sleep_time, rs_limits, false, true); |
| 1004 | if (!ret) { |
Maheshkumar Sivasubramanian | dd93ecf | 2011-09-15 19:39:14 -0600 | [diff] [blame] | 1005 | int collapsed = msm_pm_power_collapse(false); |
| 1006 | msm_rpmrs_exit_sleep(rs_limits, false, true, |
| 1007 | collapsed); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1008 | } |
| 1009 | } else { |
| 1010 | pr_err("%s: cannot find the lowest power limit\n", |
| 1011 | __func__); |
| 1012 | } |
| 1013 | |
| 1014 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1015 | if (time != 0) { |
| 1016 | int64_t end_time = msm_timer_get_sclk_time(NULL); |
| 1017 | if (end_time != 0) { |
| 1018 | time = end_time - time; |
| 1019 | if (time < 0) |
| 1020 | time += period; |
| 1021 | } else |
| 1022 | time = 0; |
| 1023 | } |
| 1024 | |
| 1025 | msm_pm_add_stat(MSM_PM_STAT_SUSPEND, time); |
| 1026 | #endif /* CONFIG_MSM_IDLE_STATS */ |
| 1027 | } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) { |
| 1028 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1029 | pr_info("%s: standalone power collapse\n", __func__); |
| 1030 | msm_pm_power_collapse_standalone(false); |
| 1031 | } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) { |
| 1032 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1033 | pr_info("%s: swfi\n", __func__); |
| 1034 | msm_pm_swfi(); |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | enter_exit: |
| 1039 | if (MSM_PM_DEBUG_SUSPEND & msm_pm_debug_mask) |
| 1040 | pr_info("%s: return\n", __func__); |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static struct platform_suspend_ops msm_pm_ops = { |
| 1046 | .enter = msm_pm_enter, |
| 1047 | .valid = suspend_valid_only_mem, |
| 1048 | }; |
| 1049 | |
| 1050 | #ifdef CONFIG_HOTPLUG_CPU |
| 1051 | int platform_cpu_disable(unsigned int cpu) |
| 1052 | { |
| 1053 | return cpu == 0 ? -EPERM : 0; |
| 1054 | } |
| 1055 | |
| 1056 | int platform_cpu_kill(unsigned int cpu) |
| 1057 | { |
| 1058 | struct completion *killed = &per_cpu(msm_pm_devices, cpu).cpu_killed; |
| 1059 | return wait_for_completion_timeout(killed, HZ * 5); |
| 1060 | } |
| 1061 | |
| 1062 | void platform_cpu_die(unsigned int cpu) |
| 1063 | { |
| 1064 | bool allow[MSM_PM_SLEEP_MODE_NR]; |
| 1065 | int i; |
| 1066 | |
| 1067 | if (unlikely(cpu != smp_processor_id())) { |
| 1068 | pr_crit("%s: running on %u, should be %u\n", |
| 1069 | __func__, smp_processor_id(), cpu); |
| 1070 | BUG(); |
| 1071 | } |
| 1072 | |
| 1073 | for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) { |
| 1074 | struct msm_pm_platform_data *mode; |
| 1075 | |
| 1076 | mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)]; |
| 1077 | allow[i] = mode->suspend_supported && mode->suspend_enabled; |
| 1078 | } |
| 1079 | |
| 1080 | if (MSM_PM_DEBUG_HOTPLUG & msm_pm_debug_mask) |
| 1081 | pr_notice("CPU%u: %s: shutting down cpu\n", cpu, __func__); |
| 1082 | complete(&__get_cpu_var(msm_pm_devices).cpu_killed); |
| 1083 | |
| 1084 | flush_cache_all(); |
| 1085 | |
| 1086 | for (;;) { |
| 1087 | if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE]) |
| 1088 | msm_pm_power_collapse(false); |
| 1089 | else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) |
| 1090 | msm_pm_power_collapse_standalone(false); |
| 1091 | else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) |
| 1092 | msm_pm_swfi(); |
| 1093 | |
| 1094 | if (pen_release == cpu) { |
| 1095 | /* OK, proper wakeup, we're done */ |
| 1096 | break; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | pen_release = -1; |
| 1101 | pr_notice("CPU%u: %s: normal wakeup\n", cpu, __func__); |
| 1102 | } |
| 1103 | |
| 1104 | int msm_pm_platform_secondary_init(unsigned int cpu) |
| 1105 | { |
| 1106 | int ret; |
| 1107 | struct msm_pm_device *dev = &__get_cpu_var(msm_pm_devices); |
| 1108 | |
| 1109 | if (!dev->warm_boot) { |
| 1110 | dev->warm_boot = 1; |
| 1111 | return 0; |
| 1112 | } |
Pratik Patel | e577179 | 2011-09-17 18:33:54 -0700 | [diff] [blame] | 1113 | etm_restore_reg_check(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1114 | #ifdef CONFIG_VFP |
| 1115 | vfp_reinit(); |
| 1116 | #endif |
| 1117 | ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false); |
| 1118 | |
| 1119 | return ret; |
| 1120 | } |
| 1121 | #endif /* CONFIG_HOTPLUG_CPU */ |
| 1122 | |
| 1123 | /****************************************************************************** |
| 1124 | * Initialization routine |
| 1125 | *****************************************************************************/ |
| 1126 | |
| 1127 | static int __init msm_pm_init(void) |
| 1128 | { |
| 1129 | pgd_t *pc_pgd; |
| 1130 | pmd_t *pmd; |
| 1131 | unsigned long pmdval; |
| 1132 | unsigned int cpu; |
| 1133 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1134 | struct proc_dir_entry *d_entry; |
| 1135 | #endif |
| 1136 | int ret; |
| 1137 | |
| 1138 | /* Page table for cores to come back up safely. */ |
| 1139 | pc_pgd = pgd_alloc(&init_mm); |
| 1140 | if (!pc_pgd) |
| 1141 | return -ENOMEM; |
| 1142 | |
| 1143 | pmd = pmd_offset(pc_pgd + |
| 1144 | pgd_index(virt_to_phys(msm_pm_collapse_exit)), |
| 1145 | virt_to_phys(msm_pm_collapse_exit)); |
| 1146 | pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) | |
| 1147 | PMD_TYPE_SECT | PMD_SECT_AP_WRITE; |
| 1148 | pmd[0] = __pmd(pmdval); |
| 1149 | pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1))); |
| 1150 | |
| 1151 | /* It is remotely possible that the code in msm_pm_collapse_exit() |
| 1152 | * which turns on the MMU with this mapping is in the |
| 1153 | * next even-numbered megabyte beyond the |
| 1154 | * start of msm_pm_collapse_exit(). |
| 1155 | * Map this megabyte in as well. |
| 1156 | */ |
| 1157 | pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1))); |
| 1158 | flush_pmd_entry(pmd); |
| 1159 | msm_pm_pc_pgd = virt_to_phys(pc_pgd); |
| 1160 | |
| 1161 | ret = request_irq(rpm_cpu0_wakeup_irq, |
| 1162 | msm_pm_rpm_wakeup_interrupt, IRQF_TRIGGER_RISING, |
| 1163 | "pm_drv", msm_pm_rpm_wakeup_interrupt); |
| 1164 | if (ret) { |
| 1165 | pr_err("%s: failed to request irq %u: %d\n", |
| 1166 | __func__, rpm_cpu0_wakeup_irq, ret); |
| 1167 | return ret; |
| 1168 | } |
| 1169 | |
| 1170 | ret = irq_set_irq_wake(rpm_cpu0_wakeup_irq, 1); |
| 1171 | if (ret) { |
| 1172 | pr_err("%s: failed to set wakeup irq %u: %d\n", |
| 1173 | __func__, rpm_cpu0_wakeup_irq, ret); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | for_each_possible_cpu(cpu) { |
| 1178 | struct msm_pm_device *dev = &per_cpu(msm_pm_devices, cpu); |
| 1179 | |
| 1180 | dev->cpu = cpu; |
| 1181 | #ifdef CONFIG_HOTPLUG_CPU |
| 1182 | init_completion(&dev->cpu_killed); |
| 1183 | #endif |
| 1184 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1185 | |
| 1186 | #ifdef CONFIG_MSM_IDLE_STATS |
| 1187 | for_each_possible_cpu(cpu) { |
| 1188 | struct msm_pm_time_stats *stats = |
| 1189 | per_cpu(msm_pm_stats, cpu).stats; |
| 1190 | |
| 1191 | stats[MSM_PM_STAT_REQUESTED_IDLE].name = "idle-request"; |
| 1192 | stats[MSM_PM_STAT_REQUESTED_IDLE].first_bucket_time = |
| 1193 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1194 | |
| 1195 | stats[MSM_PM_STAT_IDLE_WFI].name = "idle-wfi"; |
| 1196 | stats[MSM_PM_STAT_IDLE_WFI].first_bucket_time = |
| 1197 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1198 | |
| 1199 | stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE].name = |
| 1200 | "idle-standalone-power-collapse"; |
| 1201 | stats[MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE]. |
| 1202 | first_bucket_time = CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1203 | |
| 1204 | stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].name = |
| 1205 | "idle-power-collapse"; |
| 1206 | stats[MSM_PM_STAT_IDLE_POWER_COLLAPSE].first_bucket_time = |
| 1207 | CONFIG_MSM_IDLE_STATS_FIRST_BUCKET; |
| 1208 | |
| 1209 | stats[MSM_PM_STAT_SUSPEND].name = "suspend"; |
| 1210 | stats[MSM_PM_STAT_SUSPEND].first_bucket_time = |
| 1211 | CONFIG_MSM_SUSPEND_STATS_FIRST_BUCKET; |
| 1212 | } |
| 1213 | |
| 1214 | d_entry = create_proc_entry("msm_pm_stats", |
| 1215 | S_IRUGO | S_IWUSR | S_IWGRP, NULL); |
| 1216 | if (d_entry) { |
| 1217 | d_entry->read_proc = msm_pm_read_proc; |
| 1218 | d_entry->write_proc = msm_pm_write_proc; |
| 1219 | d_entry->data = NULL; |
| 1220 | } |
| 1221 | #endif /* CONFIG_MSM_IDLE_STATS */ |
| 1222 | |
| 1223 | msm_pm_mode_sysfs_add(); |
| 1224 | msm_spm_allow_x_cpu_set_vdd(false); |
| 1225 | |
| 1226 | suspend_set_ops(&msm_pm_ops); |
| 1227 | msm_cpuidle_init(); |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | late_initcall(msm_pm_init); |