cpuidle.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #include "id.h"
  22. static atomic_t master = ATOMIC_INIT(0);
  23. static DEFINE_SPINLOCK(master_lock);
  24. static inline int ux500_enter_idle(struct cpuidle_device *dev,
  25. struct cpuidle_driver *drv, int index)
  26. {
  27. int this_cpu = smp_processor_id();
  28. bool recouple = false;
  29. if (atomic_inc_return(&master) == num_online_cpus()) {
  30. /* With this lock, we prevent the other cpu to exit and enter
  31. * this function again and become the master */
  32. if (!spin_trylock(&master_lock))
  33. goto wfi;
  34. /* decouple the gic from the A9 cores */
  35. if (prcmu_gic_decouple()) {
  36. spin_unlock(&master_lock);
  37. goto out;
  38. }
  39. /* If an error occur, we will have to recouple the gic
  40. * manually */
  41. recouple = true;
  42. /* At this state, as the gic is decoupled, if the other
  43. * cpu is in WFI, we have the guarantee it won't be wake
  44. * up, so we can safely go to retention */
  45. if (!prcmu_is_cpu_in_wfi(this_cpu ? 0 : 1))
  46. goto out;
  47. /* The prcmu will be in charge of watching the interrupts
  48. * and wake up the cpus */
  49. if (prcmu_copy_gic_settings())
  50. goto out;
  51. /* Check in the meantime an interrupt did
  52. * not occur on the gic ... */
  53. if (prcmu_gic_pending_irq())
  54. goto out;
  55. /* ... and the prcmu */
  56. if (prcmu_pending_irq())
  57. goto out;
  58. /* Go to the retention state, the prcmu will wait for the
  59. * cpu to go WFI and this is what happens after exiting this
  60. * 'master' critical section */
  61. if (prcmu_set_power_state(PRCMU_AP_IDLE, true, true))
  62. goto out;
  63. /* When we switch to retention, the prcmu is in charge
  64. * of recoupling the gic automatically */
  65. recouple = false;
  66. spin_unlock(&master_lock);
  67. }
  68. wfi:
  69. cpu_do_idle();
  70. out:
  71. atomic_dec(&master);
  72. if (recouple) {
  73. prcmu_gic_recouple();
  74. spin_unlock(&master_lock);
  75. }
  76. return index;
  77. }
  78. static struct cpuidle_driver ux500_idle_driver = {
  79. .name = "ux500_idle",
  80. .owner = THIS_MODULE,
  81. .states = {
  82. ARM_CPUIDLE_WFI_STATE,
  83. {
  84. .enter = ux500_enter_idle,
  85. .exit_latency = 70,
  86. .target_residency = 260,
  87. .flags = CPUIDLE_FLAG_TIME_VALID |
  88. CPUIDLE_FLAG_TIMER_STOP,
  89. .name = "ApIdle",
  90. .desc = "ARM Retention",
  91. },
  92. },
  93. .safe_state_index = 0,
  94. .state_count = 2,
  95. };
  96. int __init ux500_idle_init(void)
  97. {
  98. if (!(cpu_is_u8500_family() || cpu_is_ux540_family()))
  99. return -ENODEV;
  100. /* Configure wake up reasons */
  101. prcmu_enable_wakeups(PRCMU_WAKEUP(ARM) | PRCMU_WAKEUP(RTC) |
  102. PRCMU_WAKEUP(ABB));
  103. return cpuidle_register(&ux500_idle_driver, NULL);
  104. }
  105. device_initcall(ux500_idle_init);