blob: a5007ffc80a74e517b6a85b49a7c0ffd6fb5ccb8 [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
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040025#include <ctype.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <stdint.h>
29#include <string.h>
30#include <unistd.h>
31#include <errno.h>
32
33#include "xf86drm.h"
34#include "amdgpu_drm.h"
35#include "amdgpu_internal.h"
36
Michel Dänzerf05a2b42017-11-30 18:52:06 +010037static int parse_one_line(struct amdgpu_device *dev, const char *line)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040038{
39 char *buf, *saveptr;
40 char *s_did;
Michel Dänzerf05a2b42017-11-30 18:52:06 +010041 uint32_t did;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040042 char *s_rid;
Michel Dänzerf05a2b42017-11-30 18:52:06 +010043 uint32_t rid;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040044 char *s_name;
45 char *endptr;
Michel Dänzer52198092017-12-01 16:59:38 +010046 int r = -EINVAL;
47
48 /* ignore empty line and commented line */
49 if (strlen(line) == 0 || line[0] == '#')
50 return -EAGAIN;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040051
52 buf = strdup(line);
53 if (!buf)
54 return -ENOMEM;
55
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040056 /* device id */
57 s_did = strtok_r(buf, ",", &saveptr);
Michel Dänzer52198092017-12-01 16:59:38 +010058 if (!s_did)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040059 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040060
Michel Dänzerf05a2b42017-11-30 18:52:06 +010061 did = strtol(s_did, &endptr, 16);
Michel Dänzer52198092017-12-01 16:59:38 +010062 if (*endptr)
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 if (did != dev->info.asic_id) {
66 r = -EAGAIN;
67 goto out;
68 }
69
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040070 /* revision id */
71 s_rid = strtok_r(NULL, ",", &saveptr);
Michel Dänzer52198092017-12-01 16:59:38 +010072 if (!s_rid)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040073 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040074
Michel Dänzerf05a2b42017-11-30 18:52:06 +010075 rid = strtol(s_rid, &endptr, 16);
Michel Dänzer52198092017-12-01 16:59:38 +010076 if (*endptr)
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 if (rid != dev->info.pci_rev_id) {
80 r = -EAGAIN;
81 goto out;
82 }
83
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040084 /* marketing name */
85 s_name = strtok_r(NULL, ",", &saveptr);
Michel Dänzer52198092017-12-01 16:59:38 +010086 if (!s_name)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040087 goto out;
Michel Dänzer52198092017-12-01 16:59:38 +010088
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040089 /* trim leading whitespaces or tabs */
90 while (isblank(*s_name))
91 s_name++;
Michel Dänzer52198092017-12-01 16:59:38 +010092 if (strlen(s_name) == 0)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040093 goto out;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -040094
Michel Dänzerf05a2b42017-11-30 18:52:06 +010095 dev->marketing_name = strdup(s_name);
96 if (dev->marketing_name)
Michel Dänzer52198092017-12-01 16:59:38 +010097 r = 0;
98 else
99 r = -ENOMEM;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400100
101out:
102 free(buf);
103
104 return r;
105}
106
Michel Dänzerf05a2b42017-11-30 18:52:06 +0100107void amdgpu_parse_asic_ids(struct amdgpu_device *dev)
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400108{
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400109 FILE *fp;
110 char *line = NULL;
111 size_t len = 0;
112 ssize_t n;
113 int line_num = 1;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400114 int r = 0;
115
116 fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
117 if (!fp) {
118 fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
119 strerror(errno));
Michel Dänzer85c6b0b2017-11-30 18:28:01 +0100120 return;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400121 }
122
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400123 /* 1st valid line is file version */
124 while ((n = getline(&line, &len, fp)) != -1) {
125 /* trim trailing newline */
126 if (line[n - 1] == '\n')
127 line[n - 1] = '\0';
128
129 /* ignore empty line and commented line */
130 if (strlen(line) == 0 || line[0] == '#') {
131 line_num++;
132 continue;
133 }
134
135 drmMsg("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);
136 break;
137 }
138
139 while ((n = getline(&line, &len, fp)) != -1) {
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400140 /* trim trailing newline */
141 if (line[n - 1] == '\n')
142 line[n - 1] = '\0';
143
Michel Dänzerf05a2b42017-11-30 18:52:06 +0100144 r = parse_one_line(dev, line);
145 if (r != -EAGAIN)
146 break;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400147
148 line_num++;
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400149 }
150
Michel Dänzer85c6b0b2017-11-30 18:28:01 +0100151 if (r == -EINVAL) {
152 fprintf(stderr, "Invalid format: %s: line %d: %s\n",
153 AMDGPU_ASIC_ID_TABLE, line_num, line);
Michel Dänzer57d3d4c2018-01-08 11:20:25 +0100154 } else if (r && r != -EAGAIN) {
Michel Dänzer85c6b0b2017-11-30 18:28:01 +0100155 fprintf(stderr, "%s: Cannot parse ASIC IDs: %s\n",
156 __func__, strerror(-r));
157 }
158
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400159 free(line);
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400160 fclose(fp);
Xiaojie Yuan7e6bf882017-05-31 16:22:50 -0400161}