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