cpuidle-ux500.c 3.0 KB

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