blob: 62459c093842225da1706e1ace3e2da7b5ae44b4 [file] [log] [blame]
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -04001/*
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#ifdef HAVE_CONFIG_H
26#include "config.h"
27#endif
28
29#include <ctype.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <stdint.h>
33#include <string.h>
34#include <unistd.h>
35#include <errno.h>
36
37#include "xf86drm.h"
38#include "amdgpu_drm.h"
39#include "amdgpu_internal.h"
40
Michel Dänzerf05a2b42017-11-30 18:52:06 +010041static int parse_one_line(struct amdgpu_device *dev, const char *line)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040042{
43 char *buf, *saveptr;
44 char *s_did;
Michel Dänzerf05a2b42017-11-30 18:52:06 +010045 uint32_t did;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040046 char *s_rid;
Michel Dänzerf05a2b42017-11-30 18:52:06 +010047 uint32_t rid;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040048 char *s_name;
49 char *endptr;
Michel Dänzer52198092017-12-01 16:59:38 +010050 int r = -EINVAL;
51
52 /* ignore empty line and commented line */
53 if (strlen(line) == 0 || line[0] == '#')
54 return -EAGAIN;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040055
56 buf = strdup(line);
57 if (!buf)
58 return -ENOMEM;
59
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040060 /* device id */
61 s_did = strtok_r(buf, ",", &saveptr);
Michel Dänzer52198092017-12-01 16:59:38 +010062 if (!s_did)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040063 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040064
Michel Dänzerf05a2b42017-11-30 18:52:06 +010065 did = strtol(s_did, &endptr, 16);
Michel Dänzer52198092017-12-01 16:59:38 +010066 if (*endptr)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040067 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040068
Michel Dänzerf05a2b42017-11-30 18:52:06 +010069 if (did != dev->info.asic_id) {
70 r = -EAGAIN;
71 goto out;
72 }
73
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040074 /* revision id */
75 s_rid = strtok_r(NULL, ",", &saveptr);
Michel Dänzer52198092017-12-01 16:59:38 +010076 if (!s_rid)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040077 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040078
Michel Dänzerf05a2b42017-11-30 18:52:06 +010079 rid = strtol(s_rid, &endptr, 16);
Michel Dänzer52198092017-12-01 16:59:38 +010080 if (*endptr)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040081 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040082
Michel Dänzerf05a2b42017-11-30 18:52:06 +010083 if (rid != dev->info.pci_rev_id) {
84 r = -EAGAIN;
85 goto out;
86 }
87
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040088 /* marketing name */
89 s_name = strtok_r(NULL, ",", &saveptr);
Michel Dänzer52198092017-12-01 16:59:38 +010090 if (!s_name)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040091 goto out;
Michel Dänzer52198092017-12-01 16:59:38 +010092
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040093 /* trim leading whitespaces or tabs */
94 while (isblank(*s_name))
95 s_name++;
Michel Dänzer52198092017-12-01 16:59:38 +010096 if (strlen(s_name) == 0)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040097 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040098
Michel Dänzerf05a2b42017-11-30 18:52:06 +010099 dev->marketing_name = strdup(s_name);
100 if (dev->marketing_name)
Michel Dänzer52198092017-12-01 16:59:38 +0100101 r = 0;
102 else
103 r = -ENOMEM;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400104
105out:
106 free(buf);
107
108 return r;
109}
110
Michel Dänzerf05a2b42017-11-30 18:52:06 +0100111void amdgpu_parse_asic_ids(struct amdgpu_device *dev)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400112{
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400113 FILE *fp;
114 char *line = NULL;
115 size_t len = 0;
116 ssize_t n;
117 int line_num = 1;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400118 int r = 0;
119
120 fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
121 if (!fp) {
122 fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
123 strerror(errno));
Michel Dänzer85c6b0b2017-11-30 18:28:01 +0100124 return;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400125 }
126
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400127 /* 1st valid line is file version */
128 while ((n = getline(&line, &len, fp)) != -1) {
129 /* trim trailing newline */
130 if (line[n - 1] == '\n')
131 line[n - 1] = '\0';
132
133 /* ignore empty line and commented line */
134 if (strlen(line) == 0 || line[0] == '#') {
135 line_num++;
136 continue;
137 }
138
139 drmMsg("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);
140 break;
141 }
142
143 while ((n = getline(&line, &len, fp)) != -1) {
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400144 /* trim trailing newline */
145 if (line[n - 1] == '\n')
146 line[n - 1] = '\0';
147
Michel Dänzerf05a2b42017-11-30 18:52:06 +0100148 r = parse_one_line(dev, line);
149 if (r != -EAGAIN)
150 break;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400151
152 line_num++;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400153 }
154
Michel Dänzer85c6b0b2017-11-30 18:28:01 +0100155 if (r == -EINVAL) {
156 fprintf(stderr, "Invalid format: %s: line %d: %s\n",
157 AMDGPU_ASIC_ID_TABLE, line_num, line);
Michel Dänzer57d3d4c2018-01-08 11:20:25 +0100158 } else if (r && r != -EAGAIN) {
Michel Dänzer85c6b0b2017-11-30 18:28:01 +0100159 fprintf(stderr, "%s: Cannot parse ASIC IDs: %s\n",
160 __func__, strerror(-r));
161 }
162
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400163 free(line);
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400164 fclose(fp);
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400165}