CPUFreq scaling governor interface for Linux/Android hosted devices.

For a usage example see http://gerrit.rockbox.org/r/#/c/1074/

Change-Id: I1d61e0eba6552a9b5d6e15a2e3169435b2f7079d
This commit is contained in:
Udo Schläpfer 2015-01-21 20:35:03 +01:00 committed by Gerrit Rockbox
parent 6d3dc8fce0
commit a312ca1c50
3 changed files with 210 additions and 0 deletions

View file

@ -12,6 +12,7 @@ powermgmt.c
#ifdef __linux__
target/hosted/cpuinfo-linux.c
target/hosted/cpufreq-linux.c
#endif
#if !defined(SAMSUNG_YPR0) || defined(SIMULATOR) /* uses as3514 rtc */

View file

@ -0,0 +1,151 @@
/***************************************************************************
* __________ __ ___
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2015 by Udo Schläpfer
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "cpuinfo-linux.h"
#include "debug.h"
#include "cpufreq-linux.h"
static FILE* open_read(const char* file_name)
{
FILE *f = fopen(file_name, "r");
if(f == NULL)
{
DEBUGF("ERROR %s: Can not open %s for reading.", __func__, file_name);
}
return f;
}
void cpufreq_available_governors(char* governors, int governors_size, int cpu)
{
if(governors_size <= 0)
{
DEBUGF("ERROR %s: Invalid governors_size: %d.", __func__, governors_size);
return;
}
if(cpu < 0)
{
DEBUGF("ERROR %s: Invalid cpu: %d.", __func__, cpu);
return;
}
char available_governors_interface[70];
snprintf(available_governors_interface,
sizeof(available_governors_interface),
"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_available_governors",
cpu);
FILE *f = open_read(available_governors_interface);
if(f == NULL)
{
return;
}
if(fgets(governors, governors_size, f) == NULL)
{
DEBUGF("ERROR %s: Read failed for %s.", __func__, available_governors_interface);
fclose(f);
return;
}
DEBUGF("DEBUG %s: Available governors for cpu %d: %s.", __func__, cpu, governors);
fclose(f);
}
static FILE* open_write(const char* file_name)
{
FILE *f = fopen(file_name, "w");
if(f == NULL)
{
DEBUGF("ERROR %s: Can not open %s for writing.", __func__, file_name);
}
return f;
}
static void set_governor_for_cpu(const char* governor, int cpu)
{
DEBUGF("DEBUG %s: governor: %s, cpu: %d.", __func__, governor, cpu);
char scaling_governor_interface[64];
snprintf(scaling_governor_interface,
sizeof(scaling_governor_interface),
"/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
cpu);
FILE *f = open_write(scaling_governor_interface);
if(f == NULL)
{
return;
}
if(fprintf(f, "%s", governor) < 1)
{
DEBUGF("ERROR %s: Write failed for %s.", __func__, scaling_governor_interface);
}
fclose(f);
}
void cpufreq_set_governor(const char* governor, int cpu)
{
if(governor == NULL)
{
DEBUGF("ERROR %s: Invalid governor.", __func__);
return;
}
if(cpu < CPUFREQ_ALL_CPUS)
{
DEBUGF("ERROR %s: Invalid cpu: %d.", __func__, cpu);
return;
}
DEBUGF("DEBUG %s: governor: %s, cpu: %d.", __func__, governor, cpu);
if(cpu == CPUFREQ_ALL_CPUS)
{
int max_cpu = cpucount_linux();
for(int count = 0; count < max_cpu; ++count)
{
set_governor_for_cpu(governor, count);
}
}
else
{
set_governor_for_cpu(governor, cpu);
}
}

View file

@ -0,0 +1,58 @@
/***************************************************************************
* __________ __ ___
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
*
* Copyright (C) 2015 by Udo Schläpfer
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
*
* Interface for the Linux CPUFreq kernel modul.
* See https://www.kernel.org/doc/Documentation/cpu-freq/
*
****************************************************************************/
#ifndef __CPUFREQ_LINUX_H__
#define __CPUFREQ_LINUX_H__
#include <stdbool.h>
/*
Get the available governors for cpu.
governors Preallocated string for the available governors. On succes this will look like
"performance ondemand powersave", each word beeing a governor that can be used with
cpufreq_set_governor.
governors_size Size of governors.
cpu The cpu, starting with 0, for which to get the available governors. CPUFREQ_ALL_CPUS is not
supported, you must supply a cpu. See cpucount_linux in cpuinfo-linux.h.
*/
void cpufreq_available_governors(char* governors, int governors_size, int cpu);
/*
Set the cpufreq governor for cpu.
governor The governor to set. This must be one of the available governors returned by
cpufreq_available_governors.
cpu The cpu, starting with 0, for which to set the governor. CPUFREQ_ALL_CPUS to set
the governor for all cpus. See cpucount_linux in cpuinfo-linux.h.
*/
void cpufreq_set_governor(const char* governor, int cpu);
static const int CPUFREQ_ALL_CPUS = -1;
#endif