linux/drivers/cpufreq/freq_table.c
<<
>>
Prefs
   1/*
   2 * linux/drivers/cpufreq/freq_table.c
   3 *
   4 * Copyright (C) 2002 - 2003 Dominik Brodowski
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 as
   8 * published by the Free Software Foundation.
   9 *
  10 */
  11
  12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13
  14#include <linux/cpufreq.h>
  15#include <linux/module.h>
  16
  17/*********************************************************************
  18 *                     FREQUENCY TABLE HELPERS                       *
  19 *********************************************************************/
  20
  21int cpufreq_frequency_table_cpuinfo(struct cpufreq_policy *policy,
  22                                    struct cpufreq_frequency_table *table)
  23{
  24        unsigned int min_freq = ~0;
  25        unsigned int max_freq = 0;
  26        unsigned int i;
  27
  28        for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  29                unsigned int freq = table[i].frequency;
  30                if (freq == CPUFREQ_ENTRY_INVALID) {
  31                        pr_debug("table entry %u is invalid, skipping\n", i);
  32
  33                        continue;
  34                }
  35                pr_debug("table entry %u: %u kHz, %u driver_data\n",
  36                                        i, freq, table[i].driver_data);
  37                if (freq < min_freq)
  38                        min_freq = freq;
  39                if (freq > max_freq)
  40                        max_freq = freq;
  41        }
  42
  43        policy->min = policy->cpuinfo.min_freq = min_freq;
  44        policy->max = policy->cpuinfo.max_freq = max_freq;
  45
  46        if (policy->min == ~0)
  47                return -EINVAL;
  48        else
  49                return 0;
  50}
  51EXPORT_SYMBOL_GPL(cpufreq_frequency_table_cpuinfo);
  52
  53
  54int cpufreq_frequency_table_verify(struct cpufreq_policy *policy,
  55                                   struct cpufreq_frequency_table *table)
  56{
  57        unsigned int next_larger = ~0;
  58        unsigned int i;
  59        unsigned int count = 0;
  60
  61        pr_debug("request for verification of policy (%u - %u kHz) for cpu %u\n",
  62                                        policy->min, policy->max, policy->cpu);
  63
  64        cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  65                                     policy->cpuinfo.max_freq);
  66
  67        for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
  68                unsigned int freq = table[i].frequency;
  69                if (freq == CPUFREQ_ENTRY_INVALID)
  70                        continue;
  71                if ((freq >= policy->min) && (freq <= policy->max))
  72                        count++;
  73                else if ((next_larger > freq) && (freq > policy->max))
  74                        next_larger = freq;
  75        }
  76
  77        if (!count)
  78                policy->max = next_larger;
  79
  80        cpufreq_verify_within_limits(policy, policy->cpuinfo.min_freq,
  81                                     policy->cpuinfo.max_freq);
  82
  83        pr_debug("verification lead to (%u - %u kHz) for cpu %u\n",
  84                                policy->min, policy->max, policy->cpu);
  85
  86        return 0;
  87}
  88EXPORT_SYMBOL_GPL(cpufreq_frequency_table_verify);
  89
  90
  91int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
  92                                   struct cpufreq_frequency_table *table,
  93                                   unsigned int target_freq,
  94                                   unsigned int relation,
  95                                   unsigned int *index)
  96{
  97        struct cpufreq_frequency_table optimal = {
  98                .driver_data = ~0,
  99                .frequency = 0,
 100        };
 101        struct cpufreq_frequency_table suboptimal = {
 102                .driver_data = ~0,
 103                .frequency = 0,
 104        };
 105        unsigned int i;
 106
 107        pr_debug("request for target %u kHz (relation: %u) for cpu %u\n",
 108                                        target_freq, relation, policy->cpu);
 109
 110        switch (relation) {
 111        case CPUFREQ_RELATION_H:
 112                suboptimal.frequency = ~0;
 113                break;
 114        case CPUFREQ_RELATION_L:
 115                optimal.frequency = ~0;
 116                break;
 117        }
 118
 119        for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
 120                unsigned int freq = table[i].frequency;
 121                if (freq == CPUFREQ_ENTRY_INVALID)
 122                        continue;
 123                if ((freq < policy->min) || (freq > policy->max))
 124                        continue;
 125                switch (relation) {
 126                case CPUFREQ_RELATION_H:
 127                        if (freq <= target_freq) {
 128                                if (freq >= optimal.frequency) {
 129                                        optimal.frequency = freq;
 130                                        optimal.driver_data = i;
 131                                }
 132                        } else {
 133                                if (freq <= suboptimal.frequency) {
 134                                        suboptimal.frequency = freq;
 135                                        suboptimal.driver_data = i;
 136                                }
 137                        }
 138                        break;
 139                case CPUFREQ_RELATION_L:
 140                        if (freq >= target_freq) {
 141                                if (freq <= optimal.frequency) {
 142                                        optimal.frequency = freq;
 143                                        optimal.driver_data = i;
 144                                }
 145                        } else {
 146                                if (freq >= suboptimal.frequency) {
 147                                        suboptimal.frequency = freq;
 148                                        suboptimal.driver_data = i;
 149                                }
 150                        }
 151                        break;
 152                }
 153        }
 154        if (optimal.driver_data > i) {
 155                if (suboptimal.driver_data > i)
 156                        return -EINVAL;
 157                *index = suboptimal.driver_data;
 158        } else
 159                *index = optimal.driver_data;
 160
 161        pr_debug("target is %u (%u kHz, %u)\n", *index, table[*index].frequency,
 162                table[*index].driver_data);
 163
 164        return 0;
 165}
 166EXPORT_SYMBOL_GPL(cpufreq_frequency_table_target);
 167
 168static DEFINE_PER_CPU(struct cpufreq_frequency_table *, cpufreq_show_table);
 169/**
 170 * show_available_freqs - show available frequencies for the specified CPU
 171 */
 172static ssize_t show_available_freqs(struct cpufreq_policy *policy, char *buf)
 173{
 174        unsigned int i = 0;
 175        unsigned int cpu = policy->cpu;
 176        ssize_t count = 0;
 177        struct cpufreq_frequency_table *table;
 178
 179        if (!per_cpu(cpufreq_show_table, cpu))
 180                return -ENODEV;
 181
 182        table = per_cpu(cpufreq_show_table, cpu);
 183
 184        for (i = 0; (table[i].frequency != CPUFREQ_TABLE_END); i++) {
 185                if (table[i].frequency == CPUFREQ_ENTRY_INVALID)
 186                        continue;
 187                count += sprintf(&buf[count], "%d ", table[i].frequency);
 188        }
 189        count += sprintf(&buf[count], "\n");
 190
 191        return count;
 192
 193}
 194
 195struct freq_attr cpufreq_freq_attr_scaling_available_freqs = {
 196        .attr = { .name = "scaling_available_frequencies",
 197                  .mode = 0444,
 198                },
 199        .show = show_available_freqs,
 200};
 201EXPORT_SYMBOL_GPL(cpufreq_freq_attr_scaling_available_freqs);
 202
 203/*
 204 * if you use these, you must assure that the frequency table is valid
 205 * all the time between get_attr and put_attr!
 206 */
 207void cpufreq_frequency_table_get_attr(struct cpufreq_frequency_table *table,
 208                                      unsigned int cpu)
 209{
 210        pr_debug("setting show_table for cpu %u to %p\n", cpu, table);
 211        per_cpu(cpufreq_show_table, cpu) = table;
 212}
 213EXPORT_SYMBOL_GPL(cpufreq_frequency_table_get_attr);
 214
 215void cpufreq_frequency_table_put_attr(unsigned int cpu)
 216{
 217        pr_debug("clearing show_table for cpu %u\n", cpu);
 218        per_cpu(cpufreq_show_table, cpu) = NULL;
 219}
 220EXPORT_SYMBOL_GPL(cpufreq_frequency_table_put_attr);
 221
 222void cpufreq_frequency_table_update_policy_cpu(struct cpufreq_policy *policy)
 223{
 224        pr_debug("Updating show_table for new_cpu %u from last_cpu %u\n",
 225                        policy->cpu, policy->last_cpu);
 226        per_cpu(cpufreq_show_table, policy->cpu) = per_cpu(cpufreq_show_table,
 227                        policy->last_cpu);
 228        per_cpu(cpufreq_show_table, policy->last_cpu) = NULL;
 229}
 230
 231struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
 232{
 233        return per_cpu(cpufreq_show_table, cpu);
 234}
 235EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
 236
 237MODULE_AUTHOR("Dominik Brodowski <linux@brodo.de>");
 238MODULE_DESCRIPTION("CPUfreq frequency table helpers");
 239MODULE_LICENSE("GPL");
 240
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.