cpufreq: add support for cpufreq governors

cpufreq: add support for cpufreq governors

Post by Dominik Brodowsk » Tue, 11 Feb 2003 22:50:37



This patch adds support for "cpufreq governors".

Most cpufreq drivers (in fact, all except one, longrun) or even most
cpu frequency scaling algorithms only offer the CPU to be set to one
frequency. In order to offer dynamic frequency scaling, the cpufreq
core must be able to tell these drivers of a "target frequency". So
these specific drivers will be transformed to offer a "->target"
call instead of the existing "->setpolicy" call. For "longrun", all
stays the same, though.

How to decide what frequency within the CPUfreq policy should be used?
That's done using "cpufreq governors". Two are already in this patch
-- they're the already existing "powersave" and "performance" which
set the frequency statically to the lowest or highest frequency,
respectively. At least two more such governors will be ready for
addition in the near future, but likely many more as there are various
different theories and models about dynamic frequency scaling
around. Using such a generic interface as cpufreq offers to scaling
governors, these can be tested extensively, and the best one can be
selected for each specific use.

Basically, it's the following flow graph:

CPU can be set to switch independetly    |         CPU can only be set
      within specific "limits"                 |       to specific frequencies

                                 "CPUfreq policy"
                consists of frequency limits (policy->{min,max})
                     and CPUfreq governor to be used
                         /                    \
                        /                      \
                       /                       the cpufreq governor decides
                      /                        (dynamically or statically)
                     /                         what target_freq to set within
                    /                          the limits of policy->{min,max}
                   /                                \
                  /                                  \
        Using the ->setpolicy call,           Using the ->target call,
            the limits and the                    the frequency closest
             "policy" is set.                   to target_freq is set.
                                                  It is assured that it
                                                  is within policy->{min,max}

 include/linux/cpufreq.h |   72 ++++++++-
 kernel/cpufreq.c        |  357 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 378 insertions(+), 51 deletions(-)

diff -ruN linux-original/include/linux/cpufreq.h linux/include/linux/cpufreq.h
--- linux-original/include/linux/cpufreq.h      2003-01-27 17:25:23.000000000 +0100
+++ linux/include/linux/cpufreq.h       2003-01-27 17:25:53.000000000 +0100
@@ -2,10 +2,10 @@
  *  linux/include/linux/cpufreq.h
  *
  *  Copyright (C) 2001 Russell King
- *            (C) 2002 Dominik Brodowski <li...@brodo.de>
+ *            (C) 2002 - 2003 Dominik Brodowski <li...@brodo.de>
  *            
  *
- * $Id: cpufreq.h,v 1.29 2002/11/11 15:35:47 db Exp $
+ * $Id: cpufreq.h,v 1.36 2003/01/20 17:31:48 db Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -20,6 +20,9 @@
 #include <linux/device.h>

+#define CPUFREQ_NAME_LEN 16
+
+
 /*********************************************************************
  *                     CPUFREQ NOTIFIER INTERFACE                    *
  *********************************************************************/
@@ -37,14 +40,17 @@

 #define CPUFREQ_POLICY_POWERSAVE        (1)
 #define CPUFREQ_POLICY_PERFORMANCE      (2)
+#define CPUFREQ_POLICY_GOVERNOR         (3)

 /* Frequency values here are CPU kHz so that hardware which doesn't run
  * with some frequencies can complain without having to guess what per
  * cent / per mille means.
- * Maximum transition latency is in nanoseconds - if it's unknown,
+ * Maximum transition latency is in microseconds - if it's unknown,
  * CPUFREQ_ETERNAL shall be used.
  */

+struct cpufreq_governor;
+
 #define CPUFREQ_ETERNAL (-1)
 struct cpufreq_cpuinfo {
        unsigned int            max_freq;
@@ -57,6 +63,7 @@
        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
         unsigned int            policy; /* see above */
+       struct cpufreq_governor *governor; /* see below */
        struct cpufreq_cpuinfo  cpuinfo;     /* see above */
        struct intf_data        intf;   /* interface data */
 };
@@ -104,25 +111,62 @@
        return carry + val;
 };

+/*********************************************************************
+ *                          CPUFREQ GOVERNORS                        *
+ *********************************************************************/
+
+#define CPUFREQ_GOV_START  1
+#define CPUFREQ_GOV_STOP   2
+#define CPUFREQ_GOV_LIMITS 3
+
+struct cpufreq_governor {
+       char                    name[CPUFREQ_NAME_LEN];
+       int     (*governor)     (struct cpufreq_policy *policy,
+                                unsigned int event);
+       struct list_head        governor_list;
+       struct module           *owner;
+};
+
+/* pass a target to the cpufreq driver
+ * _l : (cpufreq_driver_sem is not held)
+ */
+inline int cpufreq_driver_target(struct cpufreq_policy *policy,
+                                unsigned int target_freq,
+                                unsigned int relation);
+
+inline int cpufreq_driver_target_l(struct cpufreq_policy *policy,
+                                  unsigned int target_freq,
+                                  unsigned int relation);
+
+/* pass an event to the cpufreq governor */
+int cpufreq_governor_l(unsigned int cpu, unsigned int event);
+
+int cpufreq_register_governor(struct cpufreq_governor *governor);
+void cpufreq_unregister_governor(struct cpufreq_governor *governor);

 /*********************************************************************
  *                      CPUFREQ DRIVER INTERFACE                     *
  *********************************************************************/

-#define CPUFREQ_NAME_LEN 16
+#define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
+#define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */

 struct cpufreq_driver {
        /* needed by all drivers */
-       int     (*verify)       (struct cpufreq_policy *policy);
-       int     (*setpolicy)    (struct cpufreq_policy *policy);
-       struct cpufreq_policy   *policy;
-       char                    name[CPUFREQ_NAME_LEN];
+       int     (*verify)       (struct cpufreq_policy *policy);
+       struct cpufreq_policy   *policy;
+       char                    name[CPUFREQ_NAME_LEN];
+       /* define one out of two */
+       int     (*setpolicy)    (struct cpufreq_policy *policy);
+       int     (*target)       (struct cpufreq_policy *policy,
+                                unsigned int target_freq,
+                                unsigned int relation);
        /* optional, for the moment */
-       int     (*init)        (struct cpufreq_policy *policy);
-       int     (*exit)        (struct cpufreq_policy *policy);
+       int     (*init)         (struct cpufreq_policy *policy);
+       int     (*exit)         (struct cpufreq_policy *policy);
        /* 2.4. compatible API */
 #ifdef CONFIG_CPU_FREQ_24_API
-       unsigned int            cpu_cur_freq[NR_CPUS];
+       unsigned int            cpu_cur_freq[NR_CPUS];
 #endif
 };

@@ -276,4 +320,10 @@
                                      struct cpufreq_frequency_table *table,
                                      unsigned int *index);

+int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
+                                  struct cpufreq_frequency_table *table,
+                                  unsigned int target_freq,
+                                  unsigned int relation,
+                                  unsigned int *index);
+
 #endif /* _LINUX_CPUFREQ_H */
diff -ruN linux-original/kernel/cpufreq.c linux/kernel/cpufreq.c
--- linux-original/kernel/cpufreq.c     2003-01-27 17:25:27.000000000 +0100
+++ linux/kernel/cpufreq.c      2003-01-27 17:43:41.000000000 +0100
@@ -2,9 +2,9 @@
  *  linux/kernel/cpufreq.c
  *
  *  Copyright (C) 2001 Russell King
- *            (C) 2002 Dominik Brodowski <li...@brodo.de>
+ *            (C) 2002 - 2003 Dominik Brodowski <li...@brodo.de>
  *
- *  $Id: cpufreq.c,v 1.50 2002/11/11 15:35:48 db Exp $
+ *  $Id: cpufreq.c,v 1.59 2003/01/20 17:31:48 db Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -34,7 +34,6 @@
 #include <linux/sysctl.h>
 #endif

-
 /**
  * The "cpufreq driver" - the arch- or hardware-dependend low
  * level driver of CPUFreq support, and its locking mutex.
@@ -67,6 +66,9 @@
 static unsigned int     cpu_cur_freq[NR_CPUS];
 #endif

+LIST_HEAD(cpufreq_governor_list);
+
+static int cpufreq_governor(unsigned int cpu, unsigned int event);

 /*********************************************************************
  *                          SYSFS INTERFACE                          *
@@ -75,16 +77,31 @@
 /**
  * cpufreq_parse_governor - parse a governor string
  */
-static int cpufreq_parse_governor (char *str_governor, unsigned int *governor)
+static int cpufreq_parse_governor (char *str_governor, unsigned int *policy, struct cpufreq_governor **governor)
 {
-       if (!strnicmp(str_governor, "performance", 11)) {
-               *governor = CPUFREQ_POLICY_PERFORMANCE;
+       if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
+               *policy = CPUFREQ_POLICY_PERFORMANCE;
                return 0;
-       } else if (!strnicmp(str_governor, "powersave", 9)) {
-               *governor = CPUFREQ_POLICY_POWERSAVE;
+       } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
+               *policy = CPUFREQ_POLICY_POWERSAVE;
                return 0;
-       } else
-               return -EINVAL;
+       } else  {
+               struct cpufreq_governor *t;
+               down(&cpufreq_driver_sem);
+               if (!cpufreq_driver || !cpufreq_driver->target)
+                       goto out;
+               list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
+                       if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
+                               *governor = t;
+                               *policy = CPUFREQ_POLICY_GOVERNOR;
+                               up(&cpufreq_driver_sem);
+                               return 0;
+                       }
+               }
+       out:
+               up(&cpufreq_driver_sem);
+       }
+       return -EINVAL;
 }

@@ -171,6 +188,8 @@
 static ssize_t show_scaling_governor (struct device *dev, char *buf)
 {
        unsigned int value = 0;
+       char value2[CPUFREQ_NAME_LEN];
+

        if (!dev)
                return 0;
@@ -178,6 +197,8 @@
        down(&cpufreq_driver_sem);
        if (cpufreq_driver)
                value = cpufreq_driver->policy[to_cpu_nr(dev)].policy;
+       if (value == CPUFREQ_POLICY_GOVERNOR)
+               strncpy(value2, cpufreq_driver->policy[to_cpu_nr(dev)].governor->name, CPUFREQ_NAME_LEN);
        up(&cpufreq_driver_sem);

        switch (value) {
@@ -185,6 +206,8 @@
                return sprintf(buf, "powersave\n");
        case CPUFREQ_POLICY_PERFORMANCE:
                return sprintf(buf, "performance\n");
+       case
...

read more »

 
 
 

cpufreq: add support for cpufreq governors

Post by Dominik Brodowsk » Thu, 13 Feb 2003 23:20:09


This patch adds support for "cpufreq governors".

Most cpufreq drivers (in fact, all except one, longrun) or even most
cpu frequency scaling algorithms only offer the CPU to be set to one
frequency. In order to offer dynamic frequency scaling, the cpufreq
core must be able to tell these drivers of a "target frequency". So
these specific drivers will be transformed to offer a "->target"
call instead of the existing "->setpolicy" call. For "longrun", all
stays the same, though.

How to decide what frequency within the CPUfreq policy should be used?
That's done using "cpufreq governors". Two are already in this patch
-- they're the already existing "powersave" and "performance" which
set the frequency statically to the lowest or highest frequency,
respectively. At least two more such governors will be ready for
addition in the near future, but likely many more as there are various
different theories and models about dynamic frequency scaling
around. Using such a generic interface as cpufreq offers to scaling
governors, these can be tested extensively, and the best one can be
selected for each specific use.

Basically, it's the following flow graph:

CPU can be set to switch independetly    |         CPU can only be set
      within specific "limits"                 |       to specific frequencies

                                 "CPUfreq policy"
                consists of frequency limits (policy->{min,max})
                     and CPUfreq governor to be used
                         /                    \
                        /                      \
                       /                       the cpufreq governor decides
                      /                        (dynamically or statically)
                     /                         what target_freq to set within
                    /                          the limits of policy->{min,max}
                   /                                \
                  /                                  \
        Using the ->setpolicy call,           Using the ->target call,
            the limits and the                    the frequency closest
             "policy" is set.                   to target_freq is set.
                                                  It is assured that it
                                                  is within policy->{min,max}

 include/linux/cpufreq.h |   72 ++++++++-
 kernel/cpufreq.c        |  357 ++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 378 insertions(+), 51 deletions(-)

diff -ruN linux-original/include/linux/cpufreq.h linux/include/linux/cpufreq.h
--- linux-original/include/linux/cpufreq.h      2003-01-27 17:25:23.000000000 +0100
+++ linux/include/linux/cpufreq.h       2003-01-27 17:25:53.000000000 +0100
@@ -2,10 +2,10 @@
  *  linux/include/linux/cpufreq.h
  *
  *  Copyright (C) 2001 Russell King
- *            (C) 2002 Dominik Brodowski <li...@brodo.de>
+ *            (C) 2002 - 2003 Dominik Brodowski <li...@brodo.de>
  *            
  *
- * $Id: cpufreq.h,v 1.29 2002/11/11 15:35:47 db Exp $
+ * $Id: cpufreq.h,v 1.36 2003/01/20 17:31:48 db Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -20,6 +20,9 @@
 #include <linux/device.h>

+#define CPUFREQ_NAME_LEN 16
+
+
 /*********************************************************************
  *                     CPUFREQ NOTIFIER INTERFACE                    *
  *********************************************************************/
@@ -37,14 +40,17 @@

 #define CPUFREQ_POLICY_POWERSAVE        (1)
 #define CPUFREQ_POLICY_PERFORMANCE      (2)
+#define CPUFREQ_POLICY_GOVERNOR         (3)

 /* Frequency values here are CPU kHz so that hardware which doesn't run
  * with some frequencies can complain without having to guess what per
  * cent / per mille means.
- * Maximum transition latency is in nanoseconds - if it's unknown,
+ * Maximum transition latency is in microseconds - if it's unknown,
  * CPUFREQ_ETERNAL shall be used.
  */

+struct cpufreq_governor;
+
 #define CPUFREQ_ETERNAL (-1)
 struct cpufreq_cpuinfo {
        unsigned int            max_freq;
@@ -57,6 +63,7 @@
        unsigned int            min;    /* in kHz */
        unsigned int            max;    /* in kHz */
         unsigned int            policy; /* see above */
+       struct cpufreq_governor *governor; /* see below */
        struct cpufreq_cpuinfo  cpuinfo;     /* see above */
        struct intf_data        intf;   /* interface data */
 };
@@ -104,25 +111,62 @@
        return carry + val;
 };

+/*********************************************************************
+ *                          CPUFREQ GOVERNORS                        *
+ *********************************************************************/
+
+#define CPUFREQ_GOV_START  1
+#define CPUFREQ_GOV_STOP   2
+#define CPUFREQ_GOV_LIMITS 3
+
+struct cpufreq_governor {
+       char                    name[CPUFREQ_NAME_LEN];
+       int     (*governor)     (struct cpufreq_policy *policy,
+                                unsigned int event);
+       struct list_head        governor_list;
+       struct module           *owner;
+};
+
+/* pass a target to the cpufreq driver
+ * _l : (cpufreq_driver_sem is not held)
+ */
+inline int cpufreq_driver_target(struct cpufreq_policy *policy,
+                                unsigned int target_freq,
+                                unsigned int relation);
+
+inline int cpufreq_driver_target_l(struct cpufreq_policy *policy,
+                                  unsigned int target_freq,
+                                  unsigned int relation);
+
+/* pass an event to the cpufreq governor */
+int cpufreq_governor_l(unsigned int cpu, unsigned int event);
+
+int cpufreq_register_governor(struct cpufreq_governor *governor);
+void cpufreq_unregister_governor(struct cpufreq_governor *governor);

 /*********************************************************************
  *                      CPUFREQ DRIVER INTERFACE                     *
  *********************************************************************/

-#define CPUFREQ_NAME_LEN 16
+#define CPUFREQ_RELATION_L 0  /* lowest frequency at or above target */
+#define CPUFREQ_RELATION_H 1  /* highest frequency below or at target */

 struct cpufreq_driver {
        /* needed by all drivers */
-       int     (*verify)       (struct cpufreq_policy *policy);
-       int     (*setpolicy)    (struct cpufreq_policy *policy);
-       struct cpufreq_policy   *policy;
-       char                    name[CPUFREQ_NAME_LEN];
+       int     (*verify)       (struct cpufreq_policy *policy);
+       struct cpufreq_policy   *policy;
+       char                    name[CPUFREQ_NAME_LEN];
+       /* define one out of two */
+       int     (*setpolicy)    (struct cpufreq_policy *policy);
+       int     (*target)       (struct cpufreq_policy *policy,
+                                unsigned int target_freq,
+                                unsigned int relation);
        /* optional, for the moment */
-       int     (*init)        (struct cpufreq_policy *policy);
-       int     (*exit)        (struct cpufreq_policy *policy);
+       int     (*init)         (struct cpufreq_policy *policy);
+       int     (*exit)         (struct cpufreq_policy *policy);
        /* 2.4. compatible API */
 #ifdef CONFIG_CPU_FREQ_24_API
-       unsigned int            cpu_cur_freq[NR_CPUS];
+       unsigned int            cpu_cur_freq[NR_CPUS];
 #endif
 };

@@ -276,4 +320,10 @@
                                      struct cpufreq_frequency_table *table,
                                      unsigned int *index);

+int cpufreq_frequency_table_target(struct cpufreq_policy *policy,
+                                  struct cpufreq_frequency_table *table,
+                                  unsigned int target_freq,
+                                  unsigned int relation,
+                                  unsigned int *index);
+
 #endif /* _LINUX_CPUFREQ_H */
diff -ruN linux-original/kernel/cpufreq.c linux/kernel/cpufreq.c
--- linux-original/kernel/cpufreq.c     2003-01-27 17:25:27.000000000 +0100
+++ linux/kernel/cpufreq.c      2003-01-27 17:43:41.000000000 +0100
@@ -2,9 +2,9 @@
  *  linux/kernel/cpufreq.c
  *
  *  Copyright (C) 2001 Russell King
- *            (C) 2002 Dominik Brodowski <li...@brodo.de>
+ *            (C) 2002 - 2003 Dominik Brodowski <li...@brodo.de>
  *
- *  $Id: cpufreq.c,v 1.50 2002/11/11 15:35:48 db Exp $
+ *  $Id: cpufreq.c,v 1.59 2003/01/20 17:31:48 db Exp $
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License version 2 as
@@ -34,7 +34,6 @@
 #include <linux/sysctl.h>
 #endif

-
 /**
  * The "cpufreq driver" - the arch- or hardware-dependend low
  * level driver of CPUFreq support, and its locking mutex.
@@ -67,6 +66,9 @@
 static unsigned int     cpu_cur_freq[NR_CPUS];
 #endif

+LIST_HEAD(cpufreq_governor_list);
+
+static int cpufreq_governor(unsigned int cpu, unsigned int event);

 /*********************************************************************
  *                          SYSFS INTERFACE                          *
@@ -75,16 +77,31 @@
 /**
  * cpufreq_parse_governor - parse a governor string
  */
-static int cpufreq_parse_governor (char *str_governor, unsigned int *governor)
+static int cpufreq_parse_governor (char *str_governor, unsigned int *policy, struct cpufreq_governor **governor)
 {
-       if (!strnicmp(str_governor, "performance", 11)) {
-               *governor = CPUFREQ_POLICY_PERFORMANCE;
+       if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
+               *policy = CPUFREQ_POLICY_PERFORMANCE;
                return 0;
-       } else if (!strnicmp(str_governor, "powersave", 9)) {
-               *governor = CPUFREQ_POLICY_POWERSAVE;
+       } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
+               *policy = CPUFREQ_POLICY_POWERSAVE;
                return 0;
-       } else
-               return -EINVAL;
+       } else  {
+               struct cpufreq_governor *t;
+               down(&cpufreq_driver_sem);
+               if (!cpufreq_driver || !cpufreq_driver->target)
+                       goto out;
+               list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
+                       if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
+                               *governor = t;
+                               *policy = CPUFREQ_POLICY_GOVERNOR;
+                               up(&cpufreq_driver_sem);
+                               return 0;
+                       }
+               }
+       out:
+               up(&cpufreq_driver_sem);
+       }
+       return -EINVAL;
 }

@@ -171,6 +188,8 @@
 static ssize_t show_scaling_governor (struct device *dev, char *buf)
 {
        unsigned int value = 0;
+       char value2[CPUFREQ_NAME_LEN];
+

        if (!dev)
                return 0;
@@ -178,6 +197,8 @@
        down(&cpufreq_driver_sem);
        if (cpufreq_driver)
                value = cpufreq_driver->policy[to_cpu_nr(dev)].policy;
+       if (value == CPUFREQ_POLICY_GOVERNOR)
+               strncpy(value2, cpufreq_driver->policy[to_cpu_nr(dev)].governor->name, CPUFREQ_NAME_LEN);
        up(&cpufreq_driver_sem);

        switch (value) {
@@ -185,6 +206,8 @@
                return sprintf(buf, "powersave\n");
        case CPUFREQ_POLICY_PERFORMANCE:
                return sprintf(buf, "performance\n");
+       case
...

read more »