Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2012 Linaro Ltd.
0004  */
0005 
0006 #include <linux/cpuidle.h>
0007 #include <linux/of.h>
0008 #include <linux/of_device.h>
0009 #include <asm/cpuidle.h>
0010 
0011 extern struct of_cpuidle_method __cpuidle_method_of_table[];
0012 
0013 static const struct of_cpuidle_method __cpuidle_method_of_table_sentinel
0014     __used __section("__cpuidle_method_of_table_end");
0015 
0016 static struct cpuidle_ops cpuidle_ops[NR_CPUS] __ro_after_init;
0017 
0018 /**
0019  * arm_cpuidle_simple_enter() - a wrapper to cpu_do_idle()
0020  * @dev: not used
0021  * @drv: not used
0022  * @index: not used
0023  *
0024  * A trivial wrapper to allow the cpu_do_idle function to be assigned as a
0025  * cpuidle callback by matching the function signature.
0026  *
0027  * Returns the index passed as parameter
0028  */
0029 int arm_cpuidle_simple_enter(struct cpuidle_device *dev,
0030         struct cpuidle_driver *drv, int index)
0031 {
0032     cpu_do_idle();
0033 
0034     return index;
0035 }
0036 
0037 /**
0038  * arm_cpuidle_suspend() - function to enter low power idle states
0039  * @index: an integer used as an identifier for the low level PM callbacks
0040  *
0041  * This function calls the underlying arch specific low level PM code as
0042  * registered at the init time.
0043  *
0044  * Returns the result of the suspend callback.
0045  */
0046 int arm_cpuidle_suspend(int index)
0047 {
0048     int cpu = smp_processor_id();
0049 
0050     return cpuidle_ops[cpu].suspend(index);
0051 }
0052 
0053 /**
0054  * arm_cpuidle_get_ops() - find a registered cpuidle_ops by name
0055  * @method: the method name
0056  *
0057  * Search in the __cpuidle_method_of_table array the cpuidle ops matching the
0058  * method name.
0059  *
0060  * Returns a struct cpuidle_ops pointer, NULL if not found.
0061  */
0062 static const struct cpuidle_ops *__init arm_cpuidle_get_ops(const char *method)
0063 {
0064     struct of_cpuidle_method *m = __cpuidle_method_of_table;
0065 
0066     for (; m->method; m++)
0067         if (!strcmp(m->method, method))
0068             return m->ops;
0069 
0070     return NULL;
0071 }
0072 
0073 /**
0074  * arm_cpuidle_read_ops() - Initialize the cpuidle ops with the device tree
0075  * @dn: a pointer to a struct device node corresponding to a cpu node
0076  * @cpu: the cpu identifier
0077  *
0078  * Get the method name defined in the 'enable-method' property, retrieve the
0079  * associated cpuidle_ops and do a struct copy. This copy is needed because all
0080  * cpuidle_ops are tagged __initconst and will be unloaded after the init
0081  * process.
0082  *
0083  * Return 0 on sucess, -ENOENT if no 'enable-method' is defined, -EOPNOTSUPP if
0084  * no cpuidle_ops is registered for the 'enable-method', or if either init or
0085  * suspend callback isn't defined.
0086  */
0087 static int __init arm_cpuidle_read_ops(struct device_node *dn, int cpu)
0088 {
0089     const char *enable_method;
0090     const struct cpuidle_ops *ops;
0091 
0092     enable_method = of_get_property(dn, "enable-method", NULL);
0093     if (!enable_method)
0094         return -ENOENT;
0095 
0096     ops = arm_cpuidle_get_ops(enable_method);
0097     if (!ops) {
0098         pr_warn("%pOF: unsupported enable-method property: %s\n",
0099             dn, enable_method);
0100         return -EOPNOTSUPP;
0101     }
0102 
0103     if (!ops->init || !ops->suspend) {
0104         pr_warn("cpuidle_ops '%s': no init or suspend callback\n",
0105             enable_method);
0106         return -EOPNOTSUPP;
0107     }
0108 
0109     cpuidle_ops[cpu] = *ops; /* structure copy */
0110 
0111     pr_notice("cpuidle: enable-method property '%s'"
0112           " found operations\n", enable_method);
0113 
0114     return 0;
0115 }
0116 
0117 /**
0118  * arm_cpuidle_init() - Initialize cpuidle_ops for a specific cpu
0119  * @cpu: the cpu to be initialized
0120  *
0121  * Initialize the cpuidle ops with the device for the cpu and then call
0122  * the cpu's idle initialization callback. This may fail if the underlying HW
0123  * is not operational.
0124  *
0125  * Returns:
0126  *  0 on success,
0127  *  -ENODEV if it fails to find the cpu node in the device tree,
0128  *  -EOPNOTSUPP if it does not find a registered and valid cpuidle_ops for
0129  *  this cpu,
0130  *  -ENOENT if it fails to find an 'enable-method' property,
0131  *  -ENXIO if the HW reports a failure or a misconfiguration,
0132  *  -ENOMEM if the HW report an memory allocation failure 
0133  */
0134 int __init arm_cpuidle_init(int cpu)
0135 {
0136     struct device_node *cpu_node = of_cpu_device_node_get(cpu);
0137     int ret;
0138 
0139     if (!cpu_node)
0140         return -ENODEV;
0141 
0142     ret = arm_cpuidle_read_ops(cpu_node, cpu);
0143     if (!ret)
0144         ret = cpuidle_ops[cpu].init(cpu_node, cpu);
0145 
0146     of_node_put(cpu_node);
0147 
0148     return ret;
0149 }