cpuidle.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2012 Linaro : Daniel Lezcano <daniel.lezcano@linaro.org> (IBM)
  3. *
  4. * Based on the work of Rickard Andersson <rickard.andersson@stericsson.com>
  5. * and Jonas Aaberg <jonas.aberg@stericsson.com>.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/cpuidle.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/atomic.h>
  15. #include <linux/smp.h>
  16. #include <linux/mfd/dbx500-prcmu.h>
  17. #include <linux/platform_data/arm-ux500-pm.h>
  18. #include <asm/cpuidle.h>
  19. #include <asm/proc-fns.h>
  20. #include "db8500-regs.h"
  21. static atomic_t master = ATOMIC_INIT(0);
  22. static DEFINE_SPINLOCK(master_lock);
  23. static inline int ux500_enter_idle(struct cpuidle_device *dev,
  24. struct cpuidle_driver *drv, int index)
  25. {
  26. int this_cpu = smp_processor_id();
  27. bool recouple = false;
  28. if (atomic_inc_return(&master) == num_online_cpus()) {
  29. /* With this lock, we prevent the other cpu to exit and enter
  30. * this function again and become the master */
  31. if (!spin_trylock(&master_lock))
  32. goto wfi;
  33. /* decouple the gic from the A9 cores */
  34. if (prcmu_gic_decouple()) {
  35. spin_unlock(&master_lock);
  36. goto out;
  37. }
  38. /* If an error occur, we will have to recouple the gic
  39. * manually */
  40. recouple = true;
  41. /* At this state, as the gic is decoupled, if the other
  42. * cpu is in WFI, we have the guarantee it won't be wake
  43. * up, so we can safely go to retention */
  44. if (!prcmu_is_cpu_in_wfi(this_cpu ? 0 : 1))
  45. goto out;
  46. /* The prcmu will be in charge of watching the interrupts
  47. * and wake up the cpus */
  48. if (prcmu_copy_gic_settings())
  49. goto out;
  50. /* Check in the meantime an interrupt did
  51. * not occur on the gic ... */
  52. if (prcmu_gic_pending_irq())
  53. goto out;
  54. /* ... and the prcmu */
  55. if (prcmu_pending_irq())
  56. goto out;
  57. /* Go to the retention state, the prcmu will wait for the
  58. * cpu to go WFI and this is what happens after exiting this
  59. * 'master' critical section */
  60. if (prcmu_set_power_state(PRCMU_AP_IDLE, true, true))
  61. goto out;
  62. /* When we switch to retention, the prcmu is in charge
  63. * of recoupling the gic automatically */
  64. recouple = false;
  65. spin_unlock(&master_lock);
  66. }
  67. wfi:
  68. cpu_do_idle();
  69. out:
  70. atomic_dec(&master);
  71. if (recouple) {
  72. prcmu_gic_recouple();
  73. spin_unlock(&master_lock);
  74. }
  75. return index;
  76. }
  77. static struct cpuidle_driver ux500_idle_driver = {
  78. .name = "ux500_idle",
  79. .owner = THIS_MODULE,
  80. .states = {
  81. ARM_CPUIDLE_WFI_STATE,
  82. {
  83. .enter = ux500_enter_idle,
  84. .exit_latency = 70,
  85. .target_residency = 260,
  86. .flags = CPUIDLE_FLAG_TIME_VALID |
  87. CPUIDLE_FLAG_TIMER_STOP,
  88. .name = "ApIdle",
  89. .desc = "ARM Retention",
  90. },
  91. },
  92. .safe_state_index = 0,
  93. .state_count = 2,
  94. };
  95. int __init ux500_idle_init(void)
  96. {
  97. /* Configure wake up reasons */
  98. prcmu_enable_wakeups(PRCMU_WAKEUP(ARM) | PRCMU_WAKEUP(RTC) |
  99. PRCMU_WAKEUP(ABB));
  100. return cpuidle_register(&ux500_idle_driver, NULL);
  101. }
  102. device_initcall(ux500_idle_init);