2 * Copyright (C) 2010-2011 thgreasi@gmail.com
3 * Copyright (C) 2010-2012 jeanfi@gmail.com
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 /* AMD id for the aticonfig */
30 #define _(str) gettext(str)
41 typedef int (*ADL_MAIN_CONTROL_CREATE)(ADL_MAIN_MALLOC_CALLBACK, int);
42 typedef int (*ADL_MAIN_CONTROL_DESTROY)();
43 typedef int (*ADL_ADAPTER_NUMBEROFADAPTERS_GET) (int *);
44 typedef int (*ADL_ADAPTER_ADAPTERINFO_GET) (LPAdapterInfo, int);
45 typedef int (*ADL_ADAPTER_ACTIVE_GET) (int, int*);
46 typedef int (*ADL_OVERDRIVE5_TEMPERATURE_GET) (int, int, ADLTemperature*);
47 typedef int (*ADL_OVERDRIVE5_FANSPEED_GET) (int, int, ADLFanSpeedValue*);
49 static ADL_MAIN_CONTROL_CREATE adl_main_control_create;
50 static ADL_MAIN_CONTROL_DESTROY adl_main_control_destroy;
51 static ADL_ADAPTER_NUMBEROFADAPTERS_GET adl_adapter_numberofadapters_get;
52 static ADL_ADAPTER_ADAPTERINFO_GET adl_adapter_adapterinfo_get;
53 static ADL_ADAPTER_ACTIVE_GET adl_adapter_active_get;
54 static ADL_OVERDRIVE5_TEMPERATURE_GET adl_overdrive5_temperature_get;
55 static ADL_OVERDRIVE5_FANSPEED_GET adl_overdrive5_fanspeed_get;
58 static int adl_main_control_done;
59 static int *active_amd_adapters;
61 /* Memory allocation function */
62 static void __stdcall *adl_main_memory_alloc(int isize)
64 void *lpbuffer = malloc(isize);
68 static void *getprocaddress(void *plibrary, const char * name)
70 return dlsym(plibrary, name);
74 Returns the temperature (Celcius) of an AMD/Ati GPU.
76 static double get_temp(struct psensor *sensor)
78 ADLTemperature temperature;
80 temperature.iSize = sizeof(ADLTemperature);
81 temperature.iTemperature = -273;
82 if (ADL_OK != adl_overdrive5_temperature_get(sensor->amd_id,
84 return UNKNOWN_DBL_VALUE;
86 return temperature.iTemperature/1000;
89 static double get_fanspeed(struct psensor *sensor)
91 ADLFanSpeedValue fanspeedvalue;
93 fanspeedvalue.iSize = sizeof(ADLFanSpeedValue);
94 fanspeedvalue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
95 fanspeedvalue.iFanSpeed = -1;
96 if (ADL_OK != adl_overdrive5_fanspeed_get(sensor->amd_id,
98 return UNKNOWN_DBL_VALUE;
100 return fanspeedvalue.iFanSpeed;
103 static struct psensor *create_sensor(int id, int values_len)
111 if (id & 1) {/* odd number ids represent fan sensors */
113 sprintf(name, "GPU%dfan", id);
114 sensor_type = SENSOR_TYPE_AMD_FAN;
115 } else {/* even number ids represent temperature sensors */
117 sprintf(name, "GPU%dtemp", id);
118 sensor_type = SENSOR_TYPE_AMD_TEMP;
121 sid = malloc(strlen("amd") + 1 + strlen(name) + 1);
122 sprintf(sid, "amd %s", name);
124 s = psensor_create(sid, strdup(name),
125 sensor_type, values_len);
127 s->amd_id = active_amd_adapters[id];
133 Returns the number of AMD/Ati GPU sensors (temperature and fan
136 Return 0 if no AMD/Ati gpus or cannot get information.
140 LPAdapterInfo lpadapterinfo = NULL;
141 int i, inumberadapters, inumberadaptersactive = 0;
142 int lpstatus, iadapterindex;
145 adl_main_control_done = 0;
146 active_amd_adapters = NULL;
147 hdll = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
151 _("ERROR: ADL library not found!\n"));
155 adl_main_control_create = (ADL_MAIN_CONTROL_CREATE)
156 getprocaddress(hdll, "ADL_Main_Control_Create");
157 adl_main_control_destroy = (ADL_MAIN_CONTROL_DESTROY)
158 getprocaddress(hdll, "ADL_Main_Control_Destroy");
159 adl_adapter_numberofadapters_get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET)
160 getprocaddress(hdll, "ADL_Adapter_NumberOfAdapters_Get");
161 adl_adapter_adapterinfo_get = (ADL_ADAPTER_ADAPTERINFO_GET)
162 getprocaddress(hdll, "ADL_Adapter_AdapterInfo_Get");
163 adl_adapter_active_get = (ADL_ADAPTER_ACTIVE_GET)
164 getprocaddress(hdll, "ADL_Adapter_Active_Get");
165 adl_overdrive5_temperature_get = (ADL_OVERDRIVE5_TEMPERATURE_GET)
166 getprocaddress(hdll, "ADL_Overdrive5_Temperature_Get");
167 adl_overdrive5_fanspeed_get = (ADL_OVERDRIVE5_FANSPEED_GET)
168 getprocaddress(hdll, "ADL_Overdrive5_FanSpeed_Get");
169 if (!adl_main_control_create ||
170 !adl_main_control_destroy ||
171 !adl_adapter_numberofadapters_get ||
172 !adl_adapter_adapterinfo_get ||
173 !adl_overdrive5_temperature_get ||
174 !adl_overdrive5_fanspeed_get) {
176 _("ERROR: ADL's API is missing!\n"));
180 /* Initialize ADL. The second parameter is 1, which means:
181 retrieve adapter information only for adapters that
182 are physically present and enabled in the system */
183 if (ADL_OK != adl_main_control_create(adl_main_memory_alloc, 1)) {
185 _("ERROR: ADL Initialization Error!\n"));
188 adl_main_control_done = 1;
190 /* Obtain the number of adapters for the system */
191 if (ADL_OK != adl_adapter_numberofadapters_get(&inumberadapters)) {
193 _("ERROR: Cannot get the number of adapters!\n"));
197 if (!inumberadapters)
200 lpadapterinfo = malloc(sizeof(AdapterInfo) * inumberadapters);
201 memset(lpadapterinfo, '\0', sizeof(AdapterInfo) * inumberadapters);
203 /* Get the AdapterInfo structure for all adapters in the system */
204 adl_adapter_adapterinfo_get(lpadapterinfo,
205 sizeof(AdapterInfo) * inumberadapters);
207 /* Repeat for all available adapters in the system */
208 for (i = 0; i < inumberadapters; i++) {
210 iadapterindex = lpadapterinfo[i].iAdapterIndex;
212 if (ADL_OK != adl_adapter_active_get(iadapterindex, &lpstatus))
214 if (lpstatus != ADL_TRUE)
215 /* count only if the adapter is active */
218 if (!active_amd_adapters) {
219 active_amd_adapters = (int *) malloc(sizeof(int));
220 inumberadaptersactive = 1;
222 ++inumberadaptersactive;
223 active_amd_adapters = (int *)realloc
224 (active_amd_adapters,
225 sizeof(int)*inumberadaptersactive);
227 active_amd_adapters[inumberadaptersactive-1] = iadapterindex;
232 /* Each Adapter has one GPU temperature sensor and one fan
234 return 2*inumberadaptersactive;
237 void amd_psensor_list_update(struct psensor **sensors)
239 struct psensor **ss, *s;
245 if (s->type == SENSOR_TYPE_AMD_TEMP)
246 psensor_set_current_value(s, get_temp(s));
247 else if (s->type == SENSOR_TYPE_AMD_FAN)
248 psensor_set_current_value(s, get_fanspeed(s));
255 amd_psensor_list_add(struct psensor **sensors, int values_len)
258 struct psensor **tmp, **ss, *s;
263 for (i = 0; i < n; i++) {
264 s = create_sensor(i, values_len);
266 tmp = psensor_list_add(ss, s);
280 if (adl_main_control_done)
281 adl_main_control_destroy();
285 if (active_amd_adapters) {
286 free(active_amd_adapters);
287 active_amd_adapters = NULL;