Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2012 Linaro : Daniel Lezcano <daniel.lezcano@linaro.org> (IBM)
0004  *
0005  * Based on the work of Rickard Andersson <rickard.andersson@stericsson.com>
0006  * and Jonas Aaberg <jonas.aberg@stericsson.com>.
0007  */
0008 
0009 #include <linux/init.h>
0010 #include <linux/cpuidle.h>
0011 #include <linux/spinlock.h>
0012 #include <linux/atomic.h>
0013 #include <linux/smp.h>
0014 #include <linux/mfd/dbx500-prcmu.h>
0015 #include <linux/platform_data/arm-ux500-pm.h>
0016 #include <linux/platform_device.h>
0017 
0018 #include <asm/cpuidle.h>
0019 
0020 static atomic_t master = ATOMIC_INIT(0);
0021 static DEFINE_SPINLOCK(master_lock);
0022 
0023 static inline int ux500_enter_idle(struct cpuidle_device *dev,
0024                    struct cpuidle_driver *drv, int index)
0025 {
0026     int this_cpu = smp_processor_id();
0027     bool recouple = false;
0028 
0029     if (atomic_inc_return(&master) == num_online_cpus()) {
0030 
0031         /* With this lock, we prevent the other cpu to exit and enter
0032          * this function again and become the master */
0033         if (!spin_trylock(&master_lock))
0034             goto wfi;
0035 
0036         /* decouple the gic from the A9 cores */
0037         if (prcmu_gic_decouple()) {
0038             spin_unlock(&master_lock);
0039             goto out;
0040         }
0041 
0042         /* If an error occur, we will have to recouple the gic
0043          * manually */
0044         recouple = true;
0045 
0046         /* At this state, as the gic is decoupled, if the other
0047          * cpu is in WFI, we have the guarantee it won't be wake
0048          * up, so we can safely go to retention */
0049         if (!prcmu_is_cpu_in_wfi(this_cpu ? 0 : 1))
0050             goto out;
0051 
0052         /* The prcmu will be in charge of watching the interrupts
0053          * and wake up the cpus */
0054         if (prcmu_copy_gic_settings())
0055             goto out;
0056 
0057         /* Check in the meantime an interrupt did
0058          * not occur on the gic ... */
0059         if (prcmu_gic_pending_irq())
0060             goto out;
0061 
0062         /* ... and the prcmu */
0063         if (prcmu_pending_irq())
0064             goto out;
0065 
0066         /* Go to the retention state, the prcmu will wait for the
0067          * cpu to go WFI and this is what happens after exiting this
0068          * 'master' critical section */
0069         if (prcmu_set_power_state(PRCMU_AP_IDLE, true, true))
0070             goto out;
0071 
0072         /* When we switch to retention, the prcmu is in charge
0073          * of recoupling the gic automatically */
0074         recouple = false;
0075 
0076         spin_unlock(&master_lock);
0077     }
0078 wfi:
0079     cpu_do_idle();
0080 out:
0081     atomic_dec(&master);
0082 
0083     if (recouple) {
0084         prcmu_gic_recouple();
0085         spin_unlock(&master_lock);
0086     }
0087 
0088     return index;
0089 }
0090 
0091 static struct cpuidle_driver ux500_idle_driver = {
0092     .name = "ux500_idle",
0093     .owner = THIS_MODULE,
0094     .states = {
0095         ARM_CPUIDLE_WFI_STATE,
0096         {
0097             .enter        = ux500_enter_idle,
0098             .exit_latency     = 70,
0099             .target_residency = 260,
0100             .flags        = CPUIDLE_FLAG_TIMER_STOP,
0101             .name         = "ApIdle",
0102             .desc         = "ARM Retention",
0103         },
0104     },
0105     .safe_state_index = 0,
0106     .state_count = 2,
0107 };
0108 
0109 static int dbx500_cpuidle_probe(struct platform_device *pdev)
0110 {
0111     /* Configure wake up reasons */
0112     prcmu_enable_wakeups(PRCMU_WAKEUP(ARM) | PRCMU_WAKEUP(RTC) |
0113                  PRCMU_WAKEUP(ABB));
0114 
0115     return cpuidle_register(&ux500_idle_driver, NULL);
0116 }
0117 
0118 static struct platform_driver dbx500_cpuidle_plat_driver = {
0119     .driver = {
0120         .name = "db8500-cpuidle",
0121     },
0122     .probe = dbx500_cpuidle_probe,
0123 };
0124 builtin_platform_driver(dbx500_cpuidle_plat_driver);