Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * DT idle states parsing code.
0004  *
0005  * Copyright (C) 2014 ARM Ltd.
0006  * Author: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
0007  */
0008 
0009 #define pr_fmt(fmt) "DT idle-states: " fmt
0010 
0011 #include <linux/cpuidle.h>
0012 #include <linux/cpumask.h>
0013 #include <linux/errno.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/of.h>
0017 #include <linux/of_device.h>
0018 
0019 #include "dt_idle_states.h"
0020 
0021 static int init_state_node(struct cpuidle_state *idle_state,
0022                const struct of_device_id *match_id,
0023                struct device_node *state_node)
0024 {
0025     int err;
0026     const char *desc;
0027 
0028     /*
0029      * CPUidle drivers are expected to initialize the const void *data
0030      * pointer of the passed in struct of_device_id array to the idle
0031      * state enter function.
0032      */
0033     idle_state->enter = match_id->data;
0034     /*
0035      * Since this is not a "coupled" state, it's safe to assume interrupts
0036      * won't be enabled when it exits allowing the tick to be frozen
0037      * safely. So enter() can be also enter_s2idle() callback.
0038      */
0039     idle_state->enter_s2idle = match_id->data;
0040 
0041     err = of_property_read_u32(state_node, "wakeup-latency-us",
0042                    &idle_state->exit_latency);
0043     if (err) {
0044         u32 entry_latency, exit_latency;
0045 
0046         err = of_property_read_u32(state_node, "entry-latency-us",
0047                        &entry_latency);
0048         if (err) {
0049             pr_debug(" * %pOF missing entry-latency-us property\n",
0050                  state_node);
0051             return -EINVAL;
0052         }
0053 
0054         err = of_property_read_u32(state_node, "exit-latency-us",
0055                        &exit_latency);
0056         if (err) {
0057             pr_debug(" * %pOF missing exit-latency-us property\n",
0058                  state_node);
0059             return -EINVAL;
0060         }
0061         /*
0062          * If wakeup-latency-us is missing, default to entry+exit
0063          * latencies as defined in idle states bindings
0064          */
0065         idle_state->exit_latency = entry_latency + exit_latency;
0066     }
0067 
0068     err = of_property_read_u32(state_node, "min-residency-us",
0069                    &idle_state->target_residency);
0070     if (err) {
0071         pr_debug(" * %pOF missing min-residency-us property\n",
0072                  state_node);
0073         return -EINVAL;
0074     }
0075 
0076     err = of_property_read_string(state_node, "idle-state-name", &desc);
0077     if (err)
0078         desc = state_node->name;
0079 
0080     idle_state->flags = 0;
0081     if (of_property_read_bool(state_node, "local-timer-stop"))
0082         idle_state->flags |= CPUIDLE_FLAG_TIMER_STOP;
0083     /*
0084      * TODO:
0085      *  replace with kstrdup and pointer assignment when name
0086      *  and desc become string pointers
0087      */
0088     strncpy(idle_state->name, state_node->name, CPUIDLE_NAME_LEN - 1);
0089     strncpy(idle_state->desc, desc, CPUIDLE_DESC_LEN - 1);
0090     return 0;
0091 }
0092 
0093 /*
0094  * Check that the idle state is uniform across all CPUs in the CPUidle driver
0095  * cpumask
0096  */
0097 static bool idle_state_valid(struct device_node *state_node, unsigned int idx,
0098                  const cpumask_t *cpumask)
0099 {
0100     int cpu;
0101     struct device_node *cpu_node, *curr_state_node;
0102     bool valid = true;
0103 
0104     /*
0105      * Compare idle state phandles for index idx on all CPUs in the
0106      * CPUidle driver cpumask. Start from next logical cpu following
0107      * cpumask_first(cpumask) since that's the CPU state_node was
0108      * retrieved from. If a mismatch is found bail out straight
0109      * away since we certainly hit a firmware misconfiguration.
0110      */
0111     for (cpu = cpumask_next(cpumask_first(cpumask), cpumask);
0112          cpu < nr_cpu_ids; cpu = cpumask_next(cpu, cpumask)) {
0113         cpu_node = of_cpu_device_node_get(cpu);
0114         curr_state_node = of_get_cpu_state_node(cpu_node, idx);
0115         if (state_node != curr_state_node)
0116             valid = false;
0117 
0118         of_node_put(curr_state_node);
0119         of_node_put(cpu_node);
0120         if (!valid)
0121             break;
0122     }
0123 
0124     return valid;
0125 }
0126 
0127 /**
0128  * dt_init_idle_driver() - Parse the DT idle states and initialize the
0129  *             idle driver states array
0130  * @drv:      Pointer to CPU idle driver to be initialized
0131  * @matches:      Array of of_device_id match structures to search in for
0132  *        compatible idle state nodes. The data pointer for each valid
0133  *        struct of_device_id entry in the matches array must point to
0134  *        a function with the following signature, that corresponds to
0135  *        the CPUidle state enter function signature:
0136  *
0137  *        int (*)(struct cpuidle_device *dev,
0138  *            struct cpuidle_driver *drv,
0139  *            int index);
0140  *
0141  * @start_idx:    First idle state index to be initialized
0142  *
0143  * If DT idle states are detected and are valid the state count and states
0144  * array entries in the cpuidle driver are initialized accordingly starting
0145  * from index start_idx.
0146  *
0147  * Return: number of valid DT idle states parsed, <0 on failure
0148  */
0149 int dt_init_idle_driver(struct cpuidle_driver *drv,
0150             const struct of_device_id *matches,
0151             unsigned int start_idx)
0152 {
0153     struct cpuidle_state *idle_state;
0154     struct device_node *state_node, *cpu_node;
0155     const struct of_device_id *match_id;
0156     int i, err = 0;
0157     const cpumask_t *cpumask;
0158     unsigned int state_idx = start_idx;
0159 
0160     if (state_idx >= CPUIDLE_STATE_MAX)
0161         return -EINVAL;
0162     /*
0163      * We get the idle states for the first logical cpu in the
0164      * driver mask (or cpu_possible_mask if the driver cpumask is not set)
0165      * and we check through idle_state_valid() if they are uniform
0166      * across CPUs, otherwise we hit a firmware misconfiguration.
0167      */
0168     cpumask = drv->cpumask ? : cpu_possible_mask;
0169     cpu_node = of_cpu_device_node_get(cpumask_first(cpumask));
0170 
0171     for (i = 0; ; i++) {
0172         state_node = of_get_cpu_state_node(cpu_node, i);
0173         if (!state_node)
0174             break;
0175 
0176         match_id = of_match_node(matches, state_node);
0177         if (!match_id) {
0178             err = -ENODEV;
0179             break;
0180         }
0181 
0182         if (!of_device_is_available(state_node)) {
0183             of_node_put(state_node);
0184             continue;
0185         }
0186 
0187         if (!idle_state_valid(state_node, i, cpumask)) {
0188             pr_warn("%pOF idle state not valid, bailing out\n",
0189                 state_node);
0190             err = -EINVAL;
0191             break;
0192         }
0193 
0194         if (state_idx == CPUIDLE_STATE_MAX) {
0195             pr_warn("State index reached static CPU idle driver states array size\n");
0196             break;
0197         }
0198 
0199         idle_state = &drv->states[state_idx++];
0200         err = init_state_node(idle_state, match_id, state_node);
0201         if (err) {
0202             pr_err("Parsing idle state node %pOF failed with err %d\n",
0203                    state_node, err);
0204             err = -EINVAL;
0205             break;
0206         }
0207         of_node_put(state_node);
0208     }
0209 
0210     of_node_put(state_node);
0211     of_node_put(cpu_node);
0212     if (err)
0213         return err;
0214     /*
0215      * Update the driver state count only if some valid DT idle states
0216      * were detected
0217      */
0218     if (i)
0219         drv->state_count = state_idx;
0220 
0221     /*
0222      * Return the number of present and valid DT idle states, which can
0223      * also be 0 on platforms with missing DT idle states or legacy DT
0224      * configuration predating the DT idle states bindings.
0225      */
0226     return i;
0227 }
0228 EXPORT_SYMBOL_GPL(dt_init_idle_driver);