cpuidle.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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/clockchips.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/atomic.h>
  16. #include <linux/smp.h>
  17. #include <linux/mfd/dbx500-prcmu.h>
  18. #include <linux/platform_data/arm-ux500-pm.h>
  19. #include <asm/cpuidle.h>
  20. #include <asm/proc-fns.h>
  21. #include "db8500-regs.h"
  22. static atomic_t master = ATOMIC_INIT(0);
  23. static DEFINE_SPINLOCK(master_lock);
  24. static DEFINE_PER_CPU(struct cpuidle_device, ux500_cpuidle_device);
  25. static inline int ux500_enter_idle(struct cpuidle_device *dev,
  26. struct cpuidle_driver *drv, int index)
  27. {
  28. int this_cpu = smp_processor_id();
  29. bool recouple = false;
  30. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &this_cpu);
  31. if (atomic_inc_return(&master) == num_online_cpus()) {
  32. /* With this lock, we prevent the other cpu to exit and enter
  33. * this function again and become the master */
  34. if (!spin_trylock(&master_lock))
  35. goto wfi;
  36. /* decouple the gic from the A9 cores */
  37. if (prcmu_gic_decouple()) {
  38. spin_unlock(&master_lock);
  39. goto out;
  40. }
  41. /* If an error occur, we will have to recouple the gic
  42. * manually */
  43. recouple = true;
  44. /* At this state, as the gic is decoupled, if the other
  45. * cpu is in WFI, we have the guarantee it won't be wake
  46. * up, so we can safely go to retention */
  47. if (!prcmu_is_cpu_in_wfi(this_cpu ? 0 : 1))
  48. goto out;
  49. /* The prcmu will be in charge of watching the interrupts
  50. * and wake up the cpus */
  51. if (prcmu_copy_gic_settings())
  52. goto out;
  53. /* Check in the meantime an interrupt did
  54. * not occur on the gic ... */
  55. if (prcmu_gic_pending_irq())
  56. goto out;
  57. /* ... and the prcmu */
  58. if (prcmu_pending_irq())
  59. goto out;
  60. /* Go to the retention state, the prcmu will wait for the
  61. * cpu to go WFI and this is what happens after exiting this
  62. * 'master' critical section */
  63. if (prcmu_set_power_state(PRCMU_AP_IDLE, true, true))
  64. goto out;
  65. /* When we switch to retention, the prcmu is in charge
  66. * of recoupling the gic automatically */
  67. recouple = false;
  68. spin_unlock(&master_lock);
  69. }
  70. wfi:
  71. cpu_do_idle();
  72. out:
  73. atomic_dec(&master);
  74. if (recouple) {
  75. prcmu_gic_recouple();
  76. spin_unlock(&master_lock);
  77. }
  78. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &this_cpu);
  79. return index;
  80. }
  81. static struct cpuidle_driver ux500_idle_driver = {
  82. .name = "ux500_idle",
  83. .owner = THIS_MODULE,
  84. .en_core_tk_irqen = 1,
  85. .states = {
  86. ARM_CPUIDLE_WFI_STATE,
  87. {
  88. .enter = ux500_enter_idle,
  89. .exit_latency = 70,
  90. .target_residency = 260,
  91. .flags = CPUIDLE_FLAG_TIME_VALID,
  92. .name = "ApIdle",
  93. .desc = "ARM Retention",
  94. },
  95. },
  96. .safe_state_index = 0,
  97. .state_count = 2,
  98. };
  99. /*
  100. * For each cpu, setup the broadcast timer because we will
  101. * need to migrate the timers for the states >= ApIdle.
  102. */
  103. static void ux500_setup_broadcast_timer(void *arg)
  104. {
  105. int cpu = smp_processor_id();
  106. clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ON, &cpu);
  107. }
  108. int __init ux500_idle_init(void)
  109. {
  110. int ret, cpu;
  111. struct cpuidle_device *device;
  112. /* Configure wake up reasons */
  113. prcmu_enable_wakeups(PRCMU_WAKEUP(ARM) | PRCMU_WAKEUP(RTC) |
  114. PRCMU_WAKEUP(ABB));
  115. /*
  116. * Configure the timer broadcast for each cpu, that must
  117. * be done from the cpu context, so we use a smp cross
  118. * call with 'on_each_cpu'.
  119. */
  120. on_each_cpu(ux500_setup_broadcast_timer, NULL, 1);
  121. ret = cpuidle_register_driver(&ux500_idle_driver);
  122. if (ret) {
  123. printk(KERN_ERR "failed to register ux500 idle driver\n");
  124. return ret;
  125. }
  126. for_each_online_cpu(cpu) {
  127. device = &per_cpu(ux500_cpuidle_device, cpu);
  128. device->cpu = cpu;
  129. ret = cpuidle_register_device(device);
  130. if (ret) {
  131. printk(KERN_ERR "Failed to register cpuidle "
  132. "device for cpu%d\n", cpu);
  133. goto out_unregister;
  134. }
  135. }
  136. out:
  137. return ret;
  138. out_unregister:
  139. for_each_online_cpu(cpu) {
  140. device = &per_cpu(ux500_cpuidle_device, cpu);
  141. cpuidle_unregister_device(device);
  142. }
  143. cpuidle_unregister_driver(&ux500_idle_driver);
  144. goto out;
  145. }
  146. device_initcall(ux500_idle_init);