cpuidle.c 4.3 KB

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