summaryrefslogtreecommitdiff
path: root/amdgpu/amdgpu_asic_id.c
blob: e8218974524549763d534bd6de140b44cf6cb7e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
 * Copyright © 2017 Advanced Micro Devices, Inc.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "xf86drm.h"
#include "amdgpu_drm.h"
#include "amdgpu_internal.h"

static int parse_one_line(const char *line, struct amdgpu_asic_id *id)
{
	char *buf, *saveptr;
	char *s_did;
	char *s_rid;
	char *s_name;
	char *endptr;
	int r = 0;

	buf = strdup(line);
	if (!buf)
		return -ENOMEM;

	/* ignore empty line and commented line */
	if (strlen(line) == 0 || line[0] == '#') {
		r = -EAGAIN;
		goto out;
	}

	/* device id */
	s_did = strtok_r(buf, ",", &saveptr);
	if (!s_did) {
		r = -EINVAL;
		goto out;
	}

	id->did = strtol(s_did, &endptr, 16);
	if (*endptr) {
		r = -EINVAL;
		goto out;
	}

	/* revision id */
	s_rid = strtok_r(NULL, ",", &saveptr);
	if (!s_rid) {
		r = -EINVAL;
		goto out;
	}

	id->rid = strtol(s_rid, &endptr, 16);
	if (*endptr) {
		r = -EINVAL;
		goto out;
	}

	/* marketing name */
	s_name = strtok_r(NULL, ",", &saveptr);
	if (!s_name) {
		r = -EINVAL;
		goto out;
	}
	/* trim leading whitespaces or tabs */
	while (isblank(*s_name))
		s_name++;
	if (strlen(s_name) == 0) {
		r = -EINVAL;
		goto out;
	}

	id->marketing_name = strdup(s_name);
	if (id->marketing_name == NULL) {
		r = -EINVAL;
		goto out;
	}

out:
	free(buf);

	return r;
}

int amdgpu_parse_asic_ids(struct amdgpu_asic_id **p_asic_id_table)
{
	struct amdgpu_asic_id *asic_id_table;
	struct amdgpu_asic_id *id;
	FILE *fp;
	char *line = NULL;
	size_t len = 0;
	ssize_t n;
	int line_num = 1;
	size_t table_size = 0;
	size_t table_max_size = AMDGPU_ASIC_ID_TABLE_NUM_ENTRIES;
	int r = 0;

	fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
	if (!fp) {
		fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
			strerror(errno));
		return -EINVAL;
	}

	asic_id_table = calloc(table_max_size + 1,
			       sizeof(struct amdgpu_asic_id));
	if (!asic_id_table) {
		r = -ENOMEM;
		goto close;
	}

	/* 1st valid line is file version */
	while ((n = getline(&line, &len, fp)) != -1) {
		/* trim trailing newline */
		if (line[n - 1] == '\n')
			line[n - 1] = '\0';

		/* ignore empty line and commented line */
		if (strlen(line) == 0 || line[0] == '#') {
			line_num++;
			continue;
		}

		drmMsg("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);
		break;
	}

	while ((n = getline(&line, &len, fp)) != -1) {
		if (table_size > table_max_size) {
			/* double table size */
			table_max_size *= 2;
			id = realloc(asic_id_table, (table_max_size + 1) *
				     sizeof(struct amdgpu_asic_id));
			if (!id) {
				r = -ENOMEM;
				goto free;
			}
                        asic_id_table = id;
		}

		id = asic_id_table + table_size;

		/* trim trailing newline */
		if (line[n - 1] == '\n')
			line[n - 1] = '\0';

		r = parse_one_line(line, id);
		if (r) {
			if (r == -EAGAIN) {
				line_num++;
				continue;
			}
			fprintf(stderr, "Invalid format: %s: line %d: %s\n",
				AMDGPU_ASIC_ID_TABLE, line_num, line);
			goto free;
		}

		line_num++;
		table_size++;
	}

	if (table_size != table_max_size) {
		id = realloc(asic_id_table, (table_size + 1) *
			     sizeof(struct amdgpu_asic_id));
		if (!id) {
			r = -ENOMEM;
			goto free;
		}
		asic_id_table = id;
        }

	/* end of table */
	id = asic_id_table + table_size;
	memset(id, 0, sizeof(struct amdgpu_asic_id));

free:
	free(line);

	if (r && asic_id_table) {
		while (table_size--) {
			id = asic_id_table + table_size;
			free(id->marketing_name);
		}
		free(asic_id_table);
		asic_id_table = NULL;
	}
close:
	fclose(fp);

	*p_asic_id_table = asic_id_table;

	return r;
}