[BACK]Return to cpufreq.c CVS log [TXT][DIR] Up to [local] / prex-old / dev / power

Annotation of prex-old/dev/power/cpufreq.c, Revision 1.1.1.1.2.1

1.1       nbrk        1: /*-
1.1.1.1.2.1! nbrk        2:  * Copyright (c) 2007-2008, Kohsuke Ohtani
1.1       nbrk        3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms, with or without
                      6:  * modification, are permitted provided that the following conditions
                      7:  * are met:
                      8:  * 1. Redistributions of source code must retain the above copyright
                      9:  *    notice, this list of conditions and the following disclaimer.
                     10:  * 2. Redistributions in binary form must reproduce the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer in the
                     12:  *    documentation and/or other materials provided with the distribution.
                     13:  * 3. Neither the name of the author nor the names of any co-contributors
                     14:  *    may be used to endorse or promote products derived from this software
                     15:  *    without specific prior written permission.
                     16:  *
                     17:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     18:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     19:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     20:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
                     21:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     22:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     23:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     24:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     25:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     26:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     27:  * SUCH DAMAGE.
                     28:  */
                     29:
                     30: /*
                     31:  * cpufreq.c - CPU frequency control
                     32:  */
                     33:
1.1.1.1.2.1! nbrk       34: /*
        !            35:  * Dynamic voltage scaling (DVS)
        !            36:  *
        !            37:  * DVS is widely used with mobile systems to save the processor
        !            38:  * power consumption, with minimum impact on performance.
        !            39:  * The basic idea is come from the fact the power consumption is
        !            40:  * proportional to V^2 x f, where V is voltage and f is frequency.
        !            41:  * Since processor does not always require the full performance,
        !            42:  * we can reduce power consumption by lowering voltage and frequeceny.
        !            43:  */
        !            44:
1.1       nbrk       45: #include <sys/ioctl.h>
                     46: #include <driver.h>
                     47: #include <pm.h>
1.1.1.1.2.1! nbrk       48: #include <cpu.h>
        !            49: #include <cpufreq.h>
1.1       nbrk       50:
                     51: /* #define DEBUG_CPUFREQ 1 */
                     52:
                     53: #ifdef DEBUG_CPUFREQ
1.1.1.1.2.1! nbrk       54: #define DPRINTF(a) printf a
1.1       nbrk       55: #else
1.1.1.1.2.1! nbrk       56: #define DPRINTF(a)
1.1       nbrk       57: #endif
                     58:
1.1.1.1.2.1! nbrk       59: /*
        !            60:  * DVS parameters
        !            61:  */
        !            62: #define INTERVAL_MSEC          50
        !            63: #define INTERVAL_TICK          msec_to_tick(INTERVAL_MSEC)
        !            64: #define WEIGHT                 20
        !            65:
1.1       nbrk       66: static int cpufreq_open(device_t dev, int mode);
1.1.1.1.2.1! nbrk       67: static int cpufreq_ioctl(device_t dev, u_long cmd, void *arg);
1.1       nbrk       68: static int cpufreq_close(device_t dev);
                     69: static int cpufreq_init(void);
                     70:
                     71: /*
                     72:  * Driver structure
                     73:  */
                     74: struct driver cpufreq_drv = {
                     75:        /* name */      "CPU Frequency Control",
                     76:        /* order */     3,              /* Must larger than pm driver */
                     77:        /* init */      cpufreq_init,
                     78: };
                     79:
1.1.1.1.2.1! nbrk       80: /*
        !            81:  * Device I/O table
        !            82:  */
1.1       nbrk       83: static struct devio cpufreq_io = {
                     84:        /* open */      cpufreq_open,
                     85:        /* close */     cpufreq_close,
                     86:        /* read */      NULL,
                     87:        /* write */     NULL,
                     88:        /* ioctl */     cpufreq_ioctl,
                     89:        /* event */     NULL,
                     90: };
                     91:
1.1.1.1.2.1! nbrk       92: static device_t cpufreq_dev;   /* Device object */
        !            93:
        !            94: static int cpufreq_policy;     /* Frequecy control policy */
        !            95:
        !            96: static struct dpc dvs_dpc;     /* DPC object */
        !            97:
        !            98: static int dvs_capable;                /* True if the system has dvs capability */
        !            99: static int dvs_enabled;                /* True if dvs is enabled */
        !           100:
        !           101: static int cur_speed;          /* Current CPU speed (%) */
        !           102: static int max_speed;          /* Maximum CPU speed (%) */
        !           103: static int min_speed;          /* Minimum CPU speed (%) */
        !           104:
        !           105: static int run_cycles;   /* The non-idle CPU cycles in the last interval */
        !           106: static int idle_cycles;          /* The idle CPU cycles in the last interval */
        !           107: static int excess_cycles; /* The cycles left over from the last interval */
        !           108:
        !           109: static int avg_workload;       /* Average workload */
        !           110: static int avg_deadline;       /* Average deadline */
        !           111:
        !           112: static u_long elapsed_ticks;
        !           113:
1.1       nbrk      114:
                    115: /*
1.1.1.1.2.1! nbrk      116:  * Predict CPU speed.
        !           117:  *
        !           118:  * DVS Algorithm: Weiser Style
        !           119:  *
        !           120:  *  If the utilization prediction x is high (over 70%), increase the
        !           121:  *  speed by 20% of the maximum speed. If the utilization prediction
        !           122:  *  is low (under 50%), decrease the speed by (60 - x)% of the
        !           123:  *  maximum speed.
        !           124:  *
        !           125:  *  excess_cycles is defined as the number of uncompleted run cycles
        !           126:  *  from the last interval. For example, if we find 70% activity
        !           127:  *  when runnig at full speed, and their processor speed was set to
        !           128:  *  50% during that interval, excess_cycles is set to 20%. This
        !           129:  *  value (20%) is used to calculate the processor speed in the next
        !           130:  *  interval.
        !           131:  *
        !           132:  * Refernce:
        !           133:  *   M.Weiser, B.Welch, A.Demers, and S.Shenker,
        !           134:  *   "Scheduling for Reduced CPU Energy", In Proceedings of the
        !           135:  *   1st Symposium on Operating Systems Design and Implementation,
        !           136:  *   pages 13-23, November 1994.
1.1       nbrk      137:  */
1.1.1.1.2.1! nbrk      138: static int
        !           139: predict_cpu_speed(int speed)
        !           140: {
        !           141:        int next_excess;
        !           142:        int run_percent;
        !           143:        int newspeed = speed;
        !           144:
        !           145:        run_cycles += excess_cycles;
        !           146:        run_percent = (run_cycles * 100) / (idle_cycles + run_cycles);
        !           147:
        !           148:        next_excess = run_cycles - speed * (run_cycles + idle_cycles) / 100;
        !           149:        if (next_excess < 0)
        !           150:                next_excess = 0;
        !           151:
        !           152:        if (excess_cycles > idle_cycles)
        !           153:                newspeed = 100;
        !           154:        else if (run_percent > 70)
        !           155:                newspeed = speed + 20;
        !           156:        else if (run_percent < 50)
        !           157:                newspeed = speed - (60 - run_percent);
        !           158:
        !           159:        if (newspeed > max_speed)
        !           160:                newspeed = max_speed;
        !           161:        if (newspeed < min_speed)
        !           162:                newspeed = min_speed;
        !           163:
        !           164:        DPRINTF(("DVS: run_percent=%d next_excess=%d newspeed=%d\n\n",
        !           165:                 run_percent, next_excess, newspeed));
        !           166:
        !           167:        excess_cycles = next_excess;
        !           168:        return newspeed;
        !           169: }
        !           170:
        !           171: /*
        !           172:  * Predict max CPU speed.
        !           173:  *
        !           174:  * DVS Algorithm: AVG<3>
        !           175:  *
        !           176:  *  Computes an exponentially moving average of the previous intervals.
        !           177:  *  <wight> is the relative weighting of past intervals relative to
        !           178:  *  the current interval.
        !           179:  *
        !           180:  *   predict = (weight x current + past) / (weight + 1)
        !           181:  *
        !           182:  * Refernce:
        !           183:  *   K.Govil, E.Chan, H.Wasserman,
        !           184:  *   "Comparing Algorithm for Dynamic Speed-Setting of a Low-Power CPU".
        !           185:  *   Proc. 1st Int'l Conference on Mobile Computing and Networking,
        !           186:  *   Nov 1995.
        !           187:  */
        !           188: static int
        !           189: predict_max_speed(int speed)
        !           190: {
        !           191:        int new_workload;
        !           192:        int new_deadline;
        !           193:        int newspeed;
        !           194:
        !           195:        new_workload = run_cycles * speed;
        !           196:        new_deadline = (run_cycles + idle_cycles) * speed;
        !           197:
        !           198:        avg_workload = (avg_workload * WEIGHT + new_workload) / (WEIGHT + 1);
        !           199:        avg_deadline = (avg_deadline * WEIGHT + new_deadline) / (WEIGHT + 1);
        !           200:
        !           201:        newspeed = avg_workload * 100 / avg_deadline;
        !           202:
        !           203:        DPRINTF(("DVS: new_workload=%u new_deadline=%u\n",
        !           204:                 new_workload, new_deadline));
        !           205:        DPRINTF(("DVS: avg_workload=%u avg_deadline=%u\n",
        !           206:                 avg_workload, avg_deadline));
        !           207:        return newspeed;
        !           208: }
        !           209:
        !           210: /*
        !           211:  * DPC routine to set CPU speed.
        !           212:  *
        !           213:  * This is kicked by dvs_tick() if it needed.
        !           214:  */
        !           215: static void
        !           216: dpc_adjust_speed(void *arg)
        !           217: {
        !           218:        int newspeed = (int)arg;
        !           219:
        !           220:        DPRINTF(("DVS: new speed=%d\n", newspeed));
        !           221:        cpu_setperf(newspeed);
        !           222:        cur_speed = cpu_getperf();
        !           223: }
        !           224:
        !           225: /*
        !           226:  * Timer hook routine called by tick handler.
        !           227:  */
        !           228: static void
        !           229: dvs_tick(int idle)
        !           230: {
        !           231:        int newspeed;
        !           232:
        !           233:        elapsed_ticks++;
        !           234:        if (idle)
        !           235:                idle_cycles++;
        !           236:        else
        !           237:                run_cycles++;
        !           238:
        !           239:        if (elapsed_ticks < INTERVAL_TICK)
        !           240:                return;
        !           241:
        !           242:        /* Predict max CPU speed */
        !           243:        max_speed = predict_max_speed(cur_speed);
        !           244:
        !           245:        DPRINTF(("DVS: run_cycles=%d idle_cycles=%d cur_speed=%d "
        !           246:                 "max_speed=%d\n",
        !           247:                 run_cycles, idle_cycles, cur_speed, max_speed));
        !           248:        /*
        !           249:         * Predict next CPU speed
        !           250:         */
        !           251:        newspeed = predict_cpu_speed(cur_speed);
        !           252:        if (newspeed != cur_speed) {
        !           253:                sched_dpc(&dvs_dpc, &dpc_adjust_speed, (void *)newspeed);
        !           254:        }
        !           255:        run_cycles = 0;
        !           256:        idle_cycles = 0;
        !           257:        elapsed_ticks = 0;
        !           258: }
        !           259:
        !           260: /*
        !           261:  * Enable DVS operation
        !           262:  */
        !           263: static void
        !           264: dvs_enable(void)
        !           265: {
        !           266:
        !           267:        if (!dvs_capable)
        !           268:                return;
        !           269:
        !           270:        run_cycles = 0;
        !           271:        idle_cycles = 0;
        !           272:        elapsed_ticks = 0;
        !           273:
        !           274:        max_speed = 100;        /* max 100% */
        !           275:        min_speed = 5;          /* min   5% */
        !           276:        cur_speed = cpu_getperf();
        !           277:
        !           278:        timer_hook(dvs_tick);
        !           279:        dvs_enabled = 1;
        !           280: }
        !           281:
        !           282: /*
        !           283:  * Disable DVS operation
        !           284:  */
        !           285: static void
        !           286: dvs_disable(void)
        !           287: {
        !           288:
        !           289:        if (!dvs_capable)
        !           290:                return;
        !           291:
        !           292:        timer_hook(NULL);
        !           293:
        !           294:        /* Set CPU speed to 100% */
        !           295:        cpu_setperf(100);
        !           296:        dvs_enabled = 0;
        !           297: }
1.1       nbrk      298:
                    299: static int
                    300: cpufreq_open(device_t dev, int mode)
                    301: {
                    302:
                    303:        return 0;
                    304: }
                    305:
                    306: static int
                    307: cpufreq_close(device_t dev)
                    308: {
                    309:
                    310:        return 0;
                    311: }
                    312:
                    313: static int
1.1.1.1.2.1! nbrk      314: cpufreq_ioctl(device_t dev, u_long cmd, void *arg)
1.1       nbrk      315: {
                    316:
                    317:        return 0;
                    318: }
                    319:
                    320: void
                    321: cpufreq_setpolicy(int policy)
                    322: {
                    323:
                    324:        switch (cpufreq_policy) {
                    325:        case CPUFREQ_ONDEMAND:
                    326:                if (policy == PM_POWERSAVE)
                    327:                        dvs_enable();
                    328:                else
                    329:                        dvs_disable();
                    330:
                    331:                break;
                    332:        case CPUFREQ_MAXSPEED:
                    333:                break;
                    334:        case CPUFREQ_MINSPEED:
                    335:                break;
                    336:        }
                    337: }
                    338:
                    339: static int
                    340: cpufreq_init(void)
                    341: {
                    342:        int policy;
                    343:
1.1.1.1.2.1! nbrk      344:        if (cpu_initperf())
        !           345:                return -1;
        !           346:
1.1       nbrk      347:        /* Create device object */
                    348:        cpufreq_dev = device_create(&cpufreq_io, "cpufreq", DF_CHR);
                    349:        ASSERT(cpufreq_dev);
                    350:
1.1.1.1.2.1! nbrk      351:        dvs_capable = 1;
1.1       nbrk      352:
                    353:        policy = pm_getpolicy();
                    354:        if (policy == PM_POWERSAVE)
                    355:                dvs_enable();
                    356:        return 0;
                    357: }
                    358:

CVSweb