1 /*  RetroArch - A frontend for libretro.
2  *  Copyright (C) 2021 - David Guillen Fandos
3  *
4  *  RetroArch is free software: you can redistribute it and/or modify it under the terms
5  *  of the GNU General Public License as published by the Free Software Found-
6  *  ation, either version 3 of the License, or (at your option) any later version.
7  *
8  *  RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10  *  PURPOSE.  See the GNU General Public License for more details.
11  *
12  *  You should have received a copy of the GNU General Public License along with RetroArch.
13  *  If not, see <http://www.gnu.org/licenses/>.
14  */
15 
16 #ifndef _MISC_CPUFREQ_H
17 #define _MISC_CPUFREQ_H
18 
19 #include <stdint.h>
20 
21 RETRO_BEGIN_DECLS
22 
23 #define MAX_GOV_STRLEN   32
24 
25 /* Events from Frontend to the driver to drive policies */
26 enum cpu_scaling_event
27 {
28    CPUSCALING_EVENT_FOCUS_CORE,
29    CPUSCALING_EVENT_FOCUS_MENU,
30    CPUSCALING_EVENT_FOCUS_SCREENSAVER
31 };
32 
33 /* Scaling mode selected by the user */
34 enum cpu_scaling_mode
35 {
36    CPUSCALING_MANAGED_PERFORMANCE = 0, /* Performance while running core     */
37    CPUSCALING_MANAGED_PER_CONTEXT,     /* Policies for core, menu, etc.      */
38    CPUSCALING_MAX_PERFORMANCE,         /* Performance (Max Freq)             */
39    CPUSCALING_MIN_POWER,               /* Use Powersave governor             */
40    CPUSCALING_BALANCED,                /* Uses schedutil/ondemand            */
41    CPUSCALING_MANUAL                   /* Can manually tweak stuff           */
42 };
43 
44 typedef struct cpu_scaling_opts
45 {
46    /* Max/Min frequencies */
47    uint32_t min_freq, max_freq;
48    /* Options for CPUSCALING_POLICY_PER_CONTEXT */
49    char main_policy[MAX_GOV_STRLEN];
50    char menu_policy[MAX_GOV_STRLEN];
51 } cpu_scaling_opts_t;
52 
53 typedef struct cpu_scaling_driver
54 {
55    /* Policy number in the sysfs tree */
56    unsigned int policy_id;
57    /* Which CPUs this scaling driver will affect */
58    char *affected_cpus;
59    /* Governor and available governors */
60    char *scaling_governor;
61    struct string_list *available_governors;
62    /* Current frequency (value might be slightly old) */
63    uint32_t current_frequency;
64    /* Max and min frequencies, for the hardware and policy */
65    uint32_t min_cpu_freq, max_cpu_freq;
66    uint32_t min_policy_freq, max_policy_freq;
67    /* Available frequencies table (can be NULL), ends with zero */
68    uint32_t *available_freqs;
69 } cpu_scaling_driver_t;
70 
71 /* Safely free all memory used by the driver */
72 void cpu_scaling_driver_free(void);
73 
74 /* Signal the initialization */
75 void cpu_scaling_driver_init(void);
76 
77 /* Get a list of the available cpu scaling drivers */
78 cpu_scaling_driver_t **get_cpu_scaling_drivers(bool can_update);
79 
80 /* Set max and min policy cpu frequency */
81 bool set_cpu_scaling_min_frequency(
82    cpu_scaling_driver_t *driver, uint32_t min_freq);
83 bool set_cpu_scaling_max_frequency(
84    cpu_scaling_driver_t *driver, uint32_t max_freq);
85 
86 /* Calculate next/previous frequencies */
87 uint32_t get_cpu_scaling_next_frequency(cpu_scaling_driver_t *driver,
88    uint32_t freq, int step);
89 uint32_t get_cpu_scaling_next_frequency_limit(uint32_t freq, int step);
90 
91 /* Set the scaling governor for this scaling driver */
92 bool set_cpu_scaling_governor(cpu_scaling_driver_t *driver,
93       const char* governor);
94 
95 /* Signal certain events that are of interest of this driver */
96 void set_cpu_scaling_signal(enum cpu_scaling_event);
97 
98 /* Set the base cpufreq policy mode */
99 void set_cpu_scaling_mode(enum cpu_scaling_mode mode,
100                           const cpu_scaling_opts_t *opts);
101 
102 /* Get the base cpufreq policy mode */
103 enum cpu_scaling_mode get_cpu_scaling_mode(cpu_scaling_opts_t *opts);
104 
105 RETRO_END_DECLS
106 
107 #endif
108 
109