1 /*
2  * Arm SCP/MCP Software
3  * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef THERMAL_MGMT_H
9 #define THERMAL_MGMT_H
10 
11 #include <mod_scmi_perf.h>
12 #include <mod_sensor.h>
13 #include <mod_thermal_mgmt.h>
14 
15 #include <fwk_core.h>
16 #include <fwk_event.h>
17 #include <fwk_id.h>
18 #include <fwk_log.h>
19 #include <fwk_mm.h>
20 #include <fwk_module.h>
21 #include <fwk_status.h>
22 
23 #include <stdint.h>
24 #include <stdlib.h>
25 
26 #define THERMAL_HAS_ASYNC_SENSORS 1
27 
28 enum mod_thermal_mgmt_event_idx {
29     MOD_THERMAL_EVENT_IDX_READ_TEMP,
30 
31     MOD_THERMAL_EVENT_IDX_COUNT,
32 };
33 
34 struct mod_thermal_mgmt_actor_ctx {
35     /* Thermal actor configuration */
36     struct mod_thermal_mgmt_actor_config *config;
37 
38     /* When the power allocated for this actor is less than what it requested */
39     uint32_t power_deficit;
40 
41     /* What the demand power is for this actor */
42     uint32_t demand_power;
43 
44     /* The power granted to an actor */
45     uint32_t granted_power;
46 
47     /* The excess of power (initially) granted vs the demand power */
48     uint32_t spare_power;
49 
50     /* Activity factor API */
51     struct mod_thermal_mgmt_activity_factor_api *activity_api;
52 };
53 
54 struct mod_thermal_mgmt_dev_ctx {
55     /* Thermal device configuration */
56     struct mod_thermal_mgmt_dev_config *config;
57 
58     /* Identifier of the thermal device */
59     fwk_id_t id;
60 
61     /* Tick counter for the slow loop */
62     unsigned int tick_counter;
63 
64     /* Current temperature */
65     uint32_t cur_temp;
66 
67     /* Does the PI loop need update */
68     bool control_needs_update;
69 
70     /* Sensor API */
71     const struct mod_sensor_api *sensor_api;
72 
73     /* Driver API */
74     struct mod_thermal_mgmt_driver_api *driver_api;
75 
76     /* The total power that can be re-distributed */
77     uint32_t tot_spare_power;
78 
79     /* The total power that actors could still take */
80     uint32_t tot_power_deficit;
81 
82     /* The total power carried over to the next fast-loop */
83     uint32_t carry_over_power;
84 
85     /*
86      * The power as: SUM(weight_actor * demand_power_actor)
87      * Use to distribute power according to actors' weight.
88      */
89     uint32_t tot_weighted_demand_power;
90 
91     /* Allocatable power calculated in the control loop */
92     uint32_t thermal_allocatable_power;
93 
94     /* Total power budget */
95     uint32_t allocatable_power;
96 
97     /* Integral (accumulated) error */
98     int32_t integral_error;
99 
100     /* Table of thermal actors */
101     struct mod_thermal_mgmt_actor_ctx *actor_ctx_table;
102 
103     /* Sensor data */
104     struct mod_sensor_data sensor_data;
105 
106     /* Thermal protection API */
107     struct mod_thermal_mgmt_protection_api *thermal_protection_api;
108 };
109 
110 struct mod_thermal_mgmt_ctx {
111     /* Table of thermal domains */
112     struct mod_thermal_mgmt_dev_ctx *dev_ctx_table;
113 
114     /* Number of thermal domains */
115     unsigned int dev_ctx_count;
116 };
117 
118 /* Helper functions */
119 struct mod_thermal_mgmt_dev_ctx *get_dev_ctx(fwk_id_t dom);
120 
121 struct mod_thermal_mgmt_actor_ctx *get_actor_ctx(
122     struct mod_thermal_mgmt_dev_ctx *dev_ctx,
123     unsigned int actor);
124 
125 /* Power allocation */
126 void distribute_power(
127     fwk_id_t id,
128     uint32_t *perf_request,
129     uint32_t *perf_limit);
130 
131 #endif /* THERMAL_MGMT_H */
132