| 1 |
/* |
| 2 |
* Copyright © 2017 Advanced Micro Devices, Inc. |
| 3 |
* All Rights Reserved. |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 19 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
*/ |
| 24 |
|
| 25 |
// secure_getenv requires _GNU_SOURCE |
| 26 |
#ifndef _GNU_SOURCE |
| 27 |
#define _GNU_SOURCE |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <ctype.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <stdint.h> |
| 34 |
#include <string.h> |
| 35 |
#include <unistd.h> |
| 36 |
#include <errno.h> |
| 37 |
|
| 38 |
#include "xf86drm.h" |
| 39 |
#include "amdgpu_drm.h" |
| 40 |
#include "amdgpu_internal.h" |
| 41 |
|
| 42 |
static int parse_one_line(struct amdgpu_device *dev, const char *line) |
| 43 |
{ |
| 44 |
char *buf, *saveptr; |
| 45 |
char *s_did; |
| 46 |
uint32_t did; |
| 47 |
char *s_rid; |
| 48 |
uint32_t rid; |
| 49 |
char *s_name; |
| 50 |
char *endptr; |
| 51 |
int r = -EINVAL; |
| 52 |
|
| 53 |
/* ignore empty line and commented line */ |
| 54 |
if (strlen(line) == 0 || line[0] == '#') |
| 55 |
return -EAGAIN; |
| 56 |
|
| 57 |
buf = strdup(line); |
| 58 |
if (!buf) |
| 59 |
return -ENOMEM; |
| 60 |
|
| 61 |
/* device id */ |
| 62 |
s_did = strtok_r(buf, ",", &saveptr); |
| 63 |
if (!s_did) |
| 64 |
goto out; |
| 65 |
|
| 66 |
did = strtol(s_did, &endptr, 16); |
| 67 |
if (*endptr) |
| 68 |
goto out; |
| 69 |
|
| 70 |
if (did != dev->info.asic_id) { |
| 71 |
r = -EAGAIN; |
| 72 |
goto out; |
| 73 |
} |
| 74 |
|
| 75 |
/* revision id */ |
| 76 |
s_rid = strtok_r(NULL, ",", &saveptr); |
| 77 |
if (!s_rid) |
| 78 |
goto out; |
| 79 |
|
| 80 |
rid = strtol(s_rid, &endptr, 16); |
| 81 |
if (*endptr) |
| 82 |
goto out; |
| 83 |
|
| 84 |
if (rid != dev->info.pci_rev_id) { |
| 85 |
r = -EAGAIN; |
| 86 |
goto out; |
| 87 |
} |
| 88 |
|
| 89 |
/* marketing name */ |
| 90 |
s_name = strtok_r(NULL, ",", &saveptr); |
| 91 |
if (!s_name) |
| 92 |
goto out; |
| 93 |
|
| 94 |
/* trim leading whitespaces or tabs */ |
| 95 |
while (isblank(*s_name)) |
| 96 |
s_name++; |
| 97 |
if (strlen(s_name) == 0) |
| 98 |
goto out; |
| 99 |
|
| 100 |
dev->marketing_name = strdup(s_name); |
| 101 |
if (dev->marketing_name) |
| 102 |
r = 0; |
| 103 |
else |
| 104 |
r = -ENOMEM; |
| 105 |
|
| 106 |
out: |
| 107 |
free(buf); |
| 108 |
|
| 109 |
return r; |
| 110 |
} |
| 111 |
|
| 112 |
static void amdgpu_parse_proc_cpuinfo(struct amdgpu_device *dev) |
| 113 |
{ |
| 114 |
const char *search_key = "model name"; |
| 115 |
const char *radeon_key = "Radeon"; |
| 116 |
char *line = NULL; |
| 117 |
size_t len = 0; |
| 118 |
FILE *fp; |
| 119 |
|
| 120 |
fp = fopen("/proc/cpuinfo", "r"); |
| 121 |
if (fp == NULL) { |
| 122 |
fprintf(stderr, "%s\n", strerror(errno)); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
while (getline(&line, &len, fp) != -1) { |
| 127 |
char *saveptr; |
| 128 |
char *value; |
| 129 |
|
| 130 |
if (strncmp(line, search_key, strlen(search_key))) |
| 131 |
continue; |
| 132 |
|
| 133 |
/* check for parts that have both CPU and GPU information */ |
| 134 |
value = strstr(line, radeon_key); |
| 135 |
|
| 136 |
/* get content after the first colon */ |
| 137 |
if (value == NULL) { |
| 138 |
value = strstr(line, ":"); |
| 139 |
if (value == NULL) |
| 140 |
continue; |
| 141 |
value++; |
| 142 |
} |
| 143 |
|
| 144 |
/* strip whitespace */ |
| 145 |
while (*value == ' ' || *value == '\t') |
| 146 |
value++; |
| 147 |
saveptr = strchr(value, '\n'); |
| 148 |
if (saveptr) |
| 149 |
*saveptr = '\0'; |
| 150 |
|
| 151 |
/* Add AMD to the new string if it's missing from slicing/dicing */ |
| 152 |
if (strncmp(value, "AMD", 3) != 0) { |
| 153 |
char *tmp = malloc(strlen(value) + 5); |
| 154 |
|
| 155 |
if (!tmp) |
| 156 |
break; |
| 157 |
sprintf(tmp, "AMD %s", value); |
| 158 |
dev->marketing_name = tmp; |
| 159 |
} else |
| 160 |
dev->marketing_name = strdup(value); |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
free(line); |
| 165 |
fclose(fp); |
| 166 |
} |
| 167 |
|
| 168 |
#if HAVE_SECURE_GETENV |
| 169 |
static char *join_path(const char *dir, const char *file) { |
| 170 |
size_t dir_len = strlen(dir); |
| 171 |
size_t file_len = strlen(file); |
| 172 |
char *full_path = NULL; |
| 173 |
|
| 174 |
int need_slash = ((dir_len > 0) && (dir[dir_len - 1] != '/')); |
| 175 |
size_t total_len = dir_len + (need_slash ? 1 : 0) + file_len + 1; // +1 for null terminator |
| 176 |
|
| 177 |
if (dir_len == 0) { |
| 178 |
return strdup(file); |
| 179 |
} |
| 180 |
|
| 181 |
full_path = malloc(total_len); |
| 182 |
if (!full_path) { |
| 183 |
return NULL; // Memory allocation failed |
| 184 |
} |
| 185 |
|
| 186 |
strcpy(full_path, dir); |
| 187 |
if (need_slash) { |
| 188 |
full_path[dir_len] = '/'; |
| 189 |
dir_len++; |
| 190 |
} |
| 191 |
strcpy(full_path + dir_len, file); |
| 192 |
|
| 193 |
return full_path; |
| 194 |
} |
| 195 |
|
| 196 |
static char **split_env_var(const char *env_var_content) |
| 197 |
{ |
| 198 |
char **ret = NULL; |
| 199 |
char *dup_env_val; |
| 200 |
int elements = 1; |
| 201 |
int index = 1; |
| 202 |
|
| 203 |
if (!env_var_content || env_var_content[0] == '\0') |
| 204 |
return NULL; |
| 205 |
|
| 206 |
for(char *p = (char *)env_var_content; *p; p++) { |
| 207 |
if (*p == ':') |
| 208 |
elements++; |
| 209 |
} |
| 210 |
|
| 211 |
dup_env_val = strdup(env_var_content); |
| 212 |
if (!dup_env_val) { |
| 213 |
return NULL; |
| 214 |
} |
| 215 |
ret = malloc(sizeof(char *) * (elements + 1)); |
| 216 |
ret[0] = dup_env_val; |
| 217 |
for(char *p = (char *)dup_env_val; *p; p++) { |
| 218 |
if (*p == ':') { |
| 219 |
*p = 0; |
| 220 |
ret[index++] = p + 1; |
| 221 |
} |
| 222 |
} |
| 223 |
ret[index] = NULL; // ensure that the last element in the array is NULL |
| 224 |
return ret; |
| 225 |
} |
| 226 |
|
| 227 |
static void split_env_var_free(char **split_var) |
| 228 |
{ |
| 229 |
if (split_var) { |
| 230 |
// remember that the first element also points to the whole duplicated string, |
| 231 |
// which was modified in place by replacing ':' with '\0' characters |
| 232 |
free(split_var[0]); |
| 233 |
free(split_var); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
static char *find_asic_id_table(void) |
| 238 |
{ |
| 239 |
// first check the paths in AMDGPU_ASIC_ID_TABLE_PATHS environment variable |
| 240 |
const char *amdgpu_asic_id_table_paths = secure_getenv("AMDGPU_ASIC_ID_TABLE_PATHS"); |
| 241 |
const char *file_name = NULL; |
| 242 |
char *found_path = NULL; |
| 243 |
char **paths = NULL; |
| 244 |
|
| 245 |
if (!amdgpu_asic_id_table_paths) |
| 246 |
return NULL; |
| 247 |
|
| 248 |
// extract the file name from AMDGPU_ASIC_ID_TABLE |
| 249 |
file_name = strrchr(AMDGPU_ASIC_ID_TABLE, '/'); |
| 250 |
if (!file_name) |
| 251 |
return NULL; |
| 252 |
file_name++; // skip the '/' |
| 253 |
|
| 254 |
paths = split_env_var(amdgpu_asic_id_table_paths); |
| 255 |
if (!paths) |
| 256 |
return NULL; |
| 257 |
|
| 258 |
// for each path, join with file_name and check if it exists |
| 259 |
for (int i = 0; paths[i] != NULL; i++) { |
| 260 |
char *full_path = join_path(paths[i], file_name); |
| 261 |
if (!full_path) { |
| 262 |
continue; |
| 263 |
} |
| 264 |
if (access(full_path, R_OK) == 0) { |
| 265 |
found_path = full_path; |
| 266 |
break; |
| 267 |
} |
| 268 |
} |
| 269 |
split_env_var_free(paths); |
| 270 |
return found_path; |
| 271 |
} |
| 272 |
#endif |
| 273 |
|
| 274 |
void amdgpu_parse_asic_ids(struct amdgpu_device *dev) |
| 275 |
{ |
| 276 |
FILE *fp; |
| 277 |
char *line = NULL; |
| 278 |
size_t len = 0; |
| 279 |
ssize_t n; |
| 280 |
int line_num = 1; |
| 281 |
int r = 0; |
| 282 |
|
| 283 |
char *amdgpu_asic_id_table_path = NULL; |
| 284 |
#if HAVE_SECURE_GETENV |
| 285 |
// if this system lacks secure_getenv(), don't allow extra paths |
| 286 |
// for security reasons. |
| 287 |
amdgpu_asic_id_table_path = find_asic_id_table(); |
| 288 |
#endif |
| 289 |
// if not found, use the default AMDGPU_ASIC_ID_TABLE path |
| 290 |
if (!amdgpu_asic_id_table_path) |
| 291 |
amdgpu_asic_id_table_path = strdup(AMDGPU_ASIC_ID_TABLE); |
| 292 |
|
| 293 |
fp = fopen(amdgpu_asic_id_table_path, "r"); |
| 294 |
if (!fp) { |
| 295 |
fprintf(stderr, "%s: %s\n", amdgpu_asic_id_table_path, |
| 296 |
strerror(errno)); |
| 297 |
goto get_cpu; |
| 298 |
} |
| 299 |
|
| 300 |
/* 1st valid line is file version */ |
| 301 |
while ((n = getline(&line, &len, fp)) != -1) { |
| 302 |
/* trim trailing newline */ |
| 303 |
if (line[n - 1] == '\n') |
| 304 |
line[n - 1] = '\0'; |
| 305 |
|
| 306 |
/* ignore empty line and commented line */ |
| 307 |
if (strlen(line) == 0 || line[0] == '#') { |
| 308 |
line_num++; |
| 309 |
continue; |
| 310 |
} |
| 311 |
|
| 312 |
drmMsg("%s version: %s\n", amdgpu_asic_id_table_path, line); |
| 313 |
break; |
| 314 |
} |
| 315 |
|
| 316 |
while ((n = getline(&line, &len, fp)) != -1) { |
| 317 |
/* trim trailing newline */ |
| 318 |
if (line[n - 1] == '\n') |
| 319 |
line[n - 1] = '\0'; |
| 320 |
|
| 321 |
r = parse_one_line(dev, line); |
| 322 |
if (r != -EAGAIN) |
| 323 |
break; |
| 324 |
|
| 325 |
line_num++; |
| 326 |
} |
| 327 |
|
| 328 |
if (r == -EINVAL) { |
| 329 |
fprintf(stderr, "Invalid format: %s: line %d: %s\n", |
| 330 |
amdgpu_asic_id_table_path, line_num, line); |
| 331 |
} else if (r && r != -EAGAIN) { |
| 332 |
fprintf(stderr, "%s: Cannot parse ASIC IDs: %s\n", |
| 333 |
__func__, strerror(-r)); |
| 334 |
} |
| 335 |
|
| 336 |
free(line); |
| 337 |
fclose(fp); |
| 338 |
|
| 339 |
get_cpu: |
| 340 |
free(amdgpu_asic_id_table_path); |
| 341 |
if (dev->info.ids_flags & AMDGPU_IDS_FLAGS_FUSION && |
| 342 |
dev->marketing_name == NULL) { |
| 343 |
amdgpu_parse_proc_cpuinfo(dev); |
| 344 |
} |
| 345 |
} |