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