summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/cpufreq-linux.c
blob: 2083f00f26f8d0d7d2ec1e4be3cb87c8b3aefb54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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, "re");
    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 & 0xFFFF);

    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, "we");
    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);
    }
}