1 // Copyright 2017 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <zircon/device/ioctl.h>
8 #include <zircon/device/ioctl-wrapper.h>
9 #include <ddk/metadata.h>
10 #include <ddk/protocol/scpi.h>
11 
12 // Unique numbers to represent correct metadata.
13 // 0x564f4c00 is the string representation of VOL.
14 #define VOLTAGE_DUTY_CYCLE_METADATA          (0x564f4c00 | DEVICE_METADATA_PRIVATE)
15 
16 // 0x54485200 is the string representation of THR.
17 #define THERMAL_CONFIG_METADATA              (0x54485200 | DEVICE_METADATA_PRIVATE)
18 
19 #define MAX_TRIP_POINTS                      16
20 #define MAX_DVFS_DOMAINS                     2
21 #define MAX_VOLTAGE_TABLE                    31
22 // temperature units are in 10th of a degree kelvin
23 typedef struct {
24     // state is a bitmask
25     uint32_t state;
26     // trip points for below states are defined below
27 #define THERMAL_STATE_NORMAL                 0
28 #define THERMAL_STATE_TRIP_VIOLATION         1
29 #define BIG_CLUSTER_POWER_DOMAIN             0
30 #define LITTLE_CLUSTER_POWER_DOMAIN          1
31 
32     // the sensor temperature at which the system should activate
33     // passive cooling policy
34     uint32_t passive_temp;
35 
36     // the sensor temperature at which the system should perform
37     // critical shutdown
38     uint32_t critical_temp;
39 
40     // number of trip points supported
41     uint32_t max_trip_count;
42 
43     // the currently active trip point
44     uint32_t active_trip[9];
45 } thermal_info_t;
46 
47 typedef struct {
48     uint32_t up_temp;
49     uint32_t down_temp;
50     int32_t fan_level;
51     int32_t big_cluster_dvfs_opp;
52     int32_t little_cluster_dvfs_opp;
53     int32_t gpu_clk_freq_source;
54 } thermal_temperature_info_t;
55 
56 typedef struct {
57     // active cooling support
58     bool active_cooling;
59 
60     // passive cooling support
61     bool passive_cooling;
62 
63     // gpu throttling support
64     bool gpu_throttling;
65 
66     // number of trip points
67     uint32_t num_trip_points;
68 
69     // big-little architecture
70     bool big_little;
71 
72     // critical temperature
73     uint32_t critical_temp;
74 
75     // trip point information
76     thermal_temperature_info_t trip_point_info[MAX_TRIP_POINTS];
77 
78     // opps information
79     scpi_opp_t opps[MAX_DVFS_DOMAINS];
80 } thermal_device_info_t;
81 
82 typedef struct {
83     uint32_t id;
84     uint32_t temp;
85 } trip_point_t;
86 
87 typedef struct {
88     uint16_t op_idx;
89     uint32_t power_domain;
90 } dvfs_info_t;
91 
92 typedef struct {
93     uint32_t microvolt;
94     uint32_t duty_cycle;
95 } voltage_table_t;
96 
97 typedef struct {
98     scpi_opp_entry_t opps[MAX_TRIP_POINTS];
99     voltage_table_t voltage_table[MAX_VOLTAGE_TABLE];
100 } opp_info_t;
101 
102 // Get thermal info
103 #define IOCTL_THERMAL_GET_INFO \
104     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 1)
105 
106 // Sets a trip point. When the sensor reaches the trip point temperature
107 // the device will notify on an event.
108 #define IOCTL_THERMAL_SET_TRIP \
109     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 2)
110 
111 // Get an event to get trip point notifications on. ZX_USER_SIGNAL_0 is changed
112 // when either trip point is reached. It is deasserted when the state is read
113 // via IOCTL_THERMAL_GET_INFO.
114 #define IOCTL_THERMAL_GET_STATE_CHANGE_EVENT \
115     IOCTL(IOCTL_KIND_GET_HANDLE, IOCTL_FAMILY_THERMAL, 3)
116 
117 // Get a port to get trip point notification packets.
118 #define IOCTL_THERMAL_GET_STATE_CHANGE_PORT \
119     IOCTL(IOCTL_KIND_GET_HANDLE, IOCTL_FAMILY_THERMAL, 4)
120 
121 #define IOCTL_THERMAL_GET_DEVICE_INFO \
122     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 5)
123 
124 #define IOCTL_THERMAL_SET_FAN_LEVEL \
125     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 6)
126 
127 #define IOCTL_THERMAL_SET_DVFS_OPP \
128     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 7)
129 
130 #define IOCTL_THERMAL_GET_TEMPERATURE \
131     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 8)
132 
133 #define IOCTL_THERMAL_GET_DVFS_INFO \
134     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 9)
135 
136 #define IOCTL_THERMAL_GET_DVFS_OPP \
137     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 10)
138 
139 #define IOCTL_THERMAL_GET_FAN_LEVEL \
140     IOCTL(IOCTL_KIND_DEFAULT, IOCTL_FAMILY_THERMAL, 11)
141 
142 // ssize_t ioctl_thermal_get_info(int fd, thermal_info_t* out)
143 IOCTL_WRAPPER_OUT(ioctl_thermal_get_info, IOCTL_THERMAL_GET_INFO, thermal_info_t);
144 
145 // ssize_t ioctl_thermal_set_trip(int fd, uint32_t temp)
146 IOCTL_WRAPPER_IN(ioctl_thermal_set_trip, IOCTL_THERMAL_SET_TRIP, trip_point_t);
147 
148 // ssize_t ioctl_thermal_get_state_change_event(int fd, zx_handle_t* out)
149 IOCTL_WRAPPER_OUT(ioctl_thermal_get_state_change_event,
150                   IOCTL_THERMAL_GET_STATE_CHANGE_EVENT,
151                   zx_handle_t);
152 
153 // ssize_t ioctl_thermal_get_state_change_port(int fd, zx_handle_t* out)
154 IOCTL_WRAPPER_OUT(ioctl_thermal_get_state_change_port,
155                   IOCTL_THERMAL_GET_STATE_CHANGE_PORT,
156                   zx_handle_t);
157 
158 // ssize_t ioctl_thermal_get_device_info(int fd, thermal_info_t* out)
159 IOCTL_WRAPPER_OUT(ioctl_thermal_get_device_info,
160                  IOCTL_THERMAL_GET_DEVICE_INFO, thermal_device_info_t);
161 
162 // ssize_t ioctl_thermal_set_fan_level(int fd, uint32_t fan_level)
163 IOCTL_WRAPPER_IN(ioctl_thermal_set_fan_level, IOCTL_THERMAL_SET_FAN_LEVEL, uint32_t);
164 
165 // ssize_t ioctl_thermal_set_dvfs_opp(int fd, dvfs_info_t* info)
166 IOCTL_WRAPPER_IN(ioctl_thermal_set_dvfs_opp,
167                  IOCTL_THERMAL_SET_DVFS_OPP, dvfs_info_t);
168 
169 // ssize_t ioctl_thermal_get_temperature(int fd, uint32_t* temp)
170 IOCTL_WRAPPER_OUT(ioctl_thermal_get_temperature, IOCTL_THERMAL_GET_TEMPERATURE, uint32_t);
171 
172 // ssize_t ioctl_thermal_get_dvfs_info(int fd, uint32_t* power_domain, scpi_opp_t* opp)
173 IOCTL_WRAPPER_INOUT(ioctl_thermal_get_dvfs_info, IOCTL_THERMAL_GET_DVFS_INFO,
174                     uint32_t, scpi_opp_t);
175 
176 // ssize_t ioctl_thermal_get_dvfs_opp(int fd, uint32_t* power_domain, uint32_t* opp)
177 IOCTL_WRAPPER_INOUT(ioctl_thermal_get_dvfs_opp, IOCTL_THERMAL_GET_DVFS_OPP,
178                     uint32_t, uint32_t);
179 
180 // ssize_t ioctl_thermal_get_fan_level(int fd, uint32_t* fan_level)
181 IOCTL_WRAPPER_OUT(ioctl_thermal_get_fan_level, IOCTL_THERMAL_GET_FAN_LEVEL, uint32_t);
182 
183 
184