2 * Copyright (C) 2010-2011 thgreasi@gmail.com, jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 /* AMD id for the aticonfig */
29 #define _(str) gettext(str)
40 typedef int (*ADL_MAIN_CONTROL_CREATE)(ADL_MAIN_MALLOC_CALLBACK, int);
41 typedef int (*ADL_MAIN_CONTROL_DESTROY)();
42 typedef int (*ADL_ADAPTER_NUMBEROFADAPTERS_GET) (int *);
43 typedef int (*ADL_ADAPTER_ADAPTERINFO_GET) (LPAdapterInfo, int);
44 typedef int (*ADL_ADAPTER_ACTIVE_GET) (int, int*);
45 typedef int (*ADL_OVERDRIVE5_TEMPERATURE_GET) (int, int, ADLTemperature*);
46 typedef int (*ADL_OVERDRIVE5_FANSPEED_GET) (int, int, ADLFanSpeedValue*);
48 static ADL_MAIN_CONTROL_CREATE adl_main_control_create;
49 static ADL_MAIN_CONTROL_DESTROY adl_main_control_destroy;
50 static ADL_ADAPTER_NUMBEROFADAPTERS_GET adl_adapter_numberofadapters_get;
51 static ADL_ADAPTER_ADAPTERINFO_GET adl_adapter_adapterinfo_get;
52 static ADL_ADAPTER_ACTIVE_GET adl_adapter_active_get;
53 static ADL_OVERDRIVE5_TEMPERATURE_GET adl_overdrive5_temperature_get;
54 static ADL_OVERDRIVE5_FANSPEED_GET adl_overdrive5_fanspeed_get;
57 static int adl_main_control_done;
58 static int *active_amd_adapters;
60 /* Memory allocation function */
61 static void __stdcall *adl_main_memory_alloc(int isize)
63 void *lpbuffer = malloc(isize);
67 static void *getprocaddress(void *plibrary, const char * name)
69 return dlsym(plibrary, name);
73 Returns the temperature (Celcius) of an AMD/Ati GPU.
75 static double get_temp(struct psensor *sensor)
77 ADLTemperature temperature;
79 temperature.iSize = sizeof(ADLTemperature);
80 temperature.iTemperature = -273;
81 if (ADL_OK != adl_overdrive5_temperature_get(sensor->amd_id,
83 return UNKNOWN_DBL_VALUE;
85 return temperature.iTemperature/1000;
88 static double get_fanspeed(struct psensor *sensor)
90 ADLFanSpeedValue fanspeedvalue;
92 fanspeedvalue.iSize = sizeof(ADLFanSpeedValue);
93 fanspeedvalue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
94 fanspeedvalue.iFanSpeed = -1;
95 if (ADL_OK != adl_overdrive5_fanspeed_get(sensor->amd_id,
97 return UNKNOWN_DBL_VALUE;
99 return fanspeedvalue.iFanSpeed;
102 static struct psensor *create_sensor(int id, int values_len)
110 if (id & 1) {/* odd number ids represent fan sensors */
112 sprintf(name, "GPU%dfan", id);
113 sensor_type = SENSOR_TYPE_AMD_FAN;
114 } else {/* even number ids represent temperature sensors */
116 sprintf(name, "GPU%dtemp", id);
117 sensor_type = SENSOR_TYPE_AMD_TEMP;
120 sid = malloc(strlen("amd") + 1 + strlen(name) + 1);
121 sprintf(sid, "amd %s", name);
123 s = psensor_create(sid, strdup(name),
124 sensor_type, values_len);
126 s->amd_id = active_amd_adapters[id];
132 Returns the number of AMD/Ati GPU sensors (temperature and fan
135 Return 0 if no AMD/Ati gpus or cannot get information.
139 LPAdapterInfo lpadapterinfo = NULL;
140 int i, inumberadapters, inumberadaptersactive = 0;
141 int lpstatus, iadapterindex;
144 adl_main_control_done = 0;
145 active_amd_adapters = NULL;
146 hdll = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
150 _("ERROR: ADL library not found!\n"));
154 adl_main_control_create = (ADL_MAIN_CONTROL_CREATE)
155 getprocaddress(hdll, "ADL_Main_Control_Create");
156 adl_main_control_destroy = (ADL_MAIN_CONTROL_DESTROY)
157 getprocaddress(hdll, "ADL_Main_Control_Destroy");
158 adl_adapter_numberofadapters_get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET)
159 getprocaddress(hdll, "ADL_Adapter_NumberOfAdapters_Get");
160 adl_adapter_adapterinfo_get = (ADL_ADAPTER_ADAPTERINFO_GET)
161 getprocaddress(hdll, "ADL_Adapter_AdapterInfo_Get");
162 adl_adapter_active_get = (ADL_ADAPTER_ACTIVE_GET)
163 getprocaddress(hdll, "ADL_Adapter_Active_Get");
164 adl_overdrive5_temperature_get = (ADL_OVERDRIVE5_TEMPERATURE_GET)
165 getprocaddress(hdll, "ADL_Overdrive5_Temperature_Get");
166 adl_overdrive5_fanspeed_get = (ADL_OVERDRIVE5_FANSPEED_GET)
167 getprocaddress(hdll, "ADL_Overdrive5_FanSpeed_Get");
168 if (!adl_main_control_create ||
169 !adl_main_control_destroy ||
170 !adl_adapter_numberofadapters_get ||
171 !adl_adapter_adapterinfo_get ||
172 !adl_overdrive5_temperature_get ||
173 !adl_overdrive5_fanspeed_get) {
175 _("ERROR: ADL's API is missing!\n"));
179 /* Initialize ADL. The second parameter is 1, which means:
180 retrieve adapter information only for adapters that
181 are physically present and enabled in the system */
182 if (ADL_OK != adl_main_control_create(adl_main_memory_alloc, 1)) {
184 _("ERROR: ADL Initialization Error!\n"));
187 adl_main_control_done = 1;
189 /* Obtain the number of adapters for the system */
190 if (ADL_OK != adl_adapter_numberofadapters_get(&inumberadapters)) {
192 _("ERROR: Cannot get the number of adapters!\n"));
196 if (!inumberadapters)
199 lpadapterinfo = malloc(sizeof(AdapterInfo) * inumberadapters);
200 memset(lpadapterinfo, '\0', sizeof(AdapterInfo) * inumberadapters);
202 /* Get the AdapterInfo structure for all adapters in the system */
203 adl_adapter_adapterinfo_get(lpadapterinfo,
204 sizeof(AdapterInfo) * inumberadapters);
206 /* Repeat for all available adapters in the system */
207 for (i = 0; i < inumberadapters; i++) {
209 iadapterindex = lpadapterinfo[i].iAdapterIndex;
211 if (ADL_OK != adl_adapter_active_get(iadapterindex, &lpstatus))
213 if (lpstatus != ADL_TRUE)
214 /* count only if the adapter is active */
217 if (!active_amd_adapters) {
218 active_amd_adapters = (int *) malloc(sizeof(int));
219 inumberadaptersactive = 1;
221 ++inumberadaptersactive;
222 active_amd_adapters = (int *)realloc
223 (active_amd_adapters,
224 sizeof(int)*inumberadaptersactive);
226 active_amd_adapters[inumberadaptersactive-1] = iadapterindex;
231 /* Each Adapter has one GPU temperature sensor and one fan
233 return 2*inumberadaptersactive;
236 void amd_psensor_list_update(struct psensor **sensors)
238 struct psensor **ss, *s;
244 if (s->type == SENSOR_TYPE_AMD_TEMP)
245 psensor_set_current_value(s, get_temp(s));
246 else if (s->type == SENSOR_TYPE_AMD_FAN)
247 psensor_set_current_value(s, get_fanspeed(s));
254 amd_psensor_list_add(struct psensor **sensors, int values_len)
257 struct psensor **tmp, **ss, *s;
262 for (i = 0; i < n; i++) {
263 s = create_sensor(i, values_len);
265 tmp = psensor_list_add(ss, s);
279 if (adl_main_control_done)
280 adl_main_control_destroy();
284 if (active_amd_adapters) {
285 free(active_amd_adapters);
286 active_amd_adapters = NULL;