2 * Copyright (C) 2010-2011 thgreasi@gmail.com, jeanfi@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_adapters;
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), strdup("ATI GPU"),
124 sensor_type, values_len);
126 s->amd_id = active_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;
143 adl_main_control_done = 0;
144 active_adapters = NULL;
145 hdll = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
148 log_err(_("AMD: cannot found ADL library."));
152 adl_main_control_create = (ADL_MAIN_CONTROL_CREATE)
153 getprocaddress(hdll, "ADL_Main_Control_Create");
154 adl_main_control_destroy = (ADL_MAIN_CONTROL_DESTROY)
155 getprocaddress(hdll, "ADL_Main_Control_Destroy");
156 adl_adapter_numberofadapters_get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET)
157 getprocaddress(hdll, "ADL_Adapter_NumberOfAdapters_Get");
158 adl_adapter_adapterinfo_get = (ADL_ADAPTER_ADAPTERINFO_GET)
159 getprocaddress(hdll, "ADL_Adapter_AdapterInfo_Get");
160 adl_adapter_active_get = (ADL_ADAPTER_ACTIVE_GET)
161 getprocaddress(hdll, "ADL_Adapter_Active_Get");
162 adl_overdrive5_temperature_get = (ADL_OVERDRIVE5_TEMPERATURE_GET)
163 getprocaddress(hdll, "ADL_Overdrive5_Temperature_Get");
164 adl_overdrive5_fanspeed_get = (ADL_OVERDRIVE5_FANSPEED_GET)
165 getprocaddress(hdll, "ADL_Overdrive5_FanSpeed_Get");
166 if (!adl_main_control_create ||
167 !adl_main_control_destroy ||
168 !adl_adapter_numberofadapters_get ||
169 !adl_adapter_adapterinfo_get ||
170 !adl_overdrive5_temperature_get ||
171 !adl_overdrive5_fanspeed_get) {
172 log_err(_("AMD: missing ADL's API."));
176 if (ADL_OK != adl_main_control_create(adl_main_memory_alloc, 1)) {
177 log_err(_("AMD: failed to initialize ADL."));
180 adl_main_control_done = 1;
182 if (ADL_OK != adl_adapter_numberofadapters_get(&inumberadapters)) {
183 log_err(_("AMD: cannot get the number of adapters."));
187 if (!inumberadapters)
190 lpadapterinfo = malloc(sizeof(AdapterInfo) * inumberadapters);
191 memset(lpadapterinfo, '\0', sizeof(AdapterInfo) * inumberadapters);
193 adl_adapter_adapterinfo_get(lpadapterinfo,
194 sizeof(AdapterInfo) * inumberadapters);
196 for (i = 0; i < inumberadapters; i++) {
198 iadapterindex = lpadapterinfo[i].iAdapterIndex;
200 if (ADL_OK != adl_adapter_active_get(iadapterindex, &lpstatus))
202 if (lpstatus != ADL_TRUE)
205 if (!active_adapters) {
206 active_adapters = (int *) malloc(sizeof(int));
207 inumberadaptersactive = 1;
209 ++inumberadaptersactive;
210 active_adapters = (int *)realloc
212 sizeof(int)*inumberadaptersactive);
214 if (!active_adapters)
217 active_adapters[inumberadaptersactive-1] = iadapterindex;
222 /* Each Adapter has one temperature sensor and one fan */
223 return 2*inumberadaptersactive;
226 void amd_psensor_list_update(struct psensor **sensors)
228 struct psensor **ss, *s;
234 if (s->type == SENSOR_TYPE_AMD_TEMP)
235 psensor_set_current_value(s, get_temp(s));
236 else if (s->type == SENSOR_TYPE_AMD_FAN)
237 psensor_set_current_value(s, get_fanspeed(s));
244 amd_psensor_list_add(struct psensor **sensors, int values_len)
247 struct psensor **tmp, **ss, *s;
252 for (i = 0; i < n; i++) {
253 s = create_sensor(i, values_len);
255 tmp = psensor_list_add(ss, s);
269 if (adl_main_control_done)
270 adl_main_control_destroy();
274 if (active_adapters) {
275 free(active_adapters);
276 active_adapters = NULL;