Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 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 | #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änzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 41 | static int parse_one_line(struct amdgpu_device *dev, const char *line) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 42 | { |
| 43 | char *buf, *saveptr; |
| 44 | char *s_did; |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 45 | uint32_t did; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 46 | char *s_rid; |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 47 | uint32_t rid; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 48 | char *s_name; |
| 49 | char *endptr; |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 50 | int r = -EINVAL; |
| 51 | |
| 52 | /* ignore empty line and commented line */ |
| 53 | if (strlen(line) == 0 || line[0] == '#') |
| 54 | return -EAGAIN; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 55 | |
| 56 | buf = strdup(line); |
| 57 | if (!buf) |
| 58 | return -ENOMEM; |
| 59 | |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 60 | /* device id */ |
| 61 | s_did = strtok_r(buf, ",", &saveptr); |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 62 | if (!s_did) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 63 | goto out; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 64 | |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 65 | did = strtol(s_did, &endptr, 16); |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 66 | if (*endptr) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 67 | goto out; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 68 | |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 69 | if (did != dev->info.asic_id) { |
| 70 | r = -EAGAIN; |
| 71 | goto out; |
| 72 | } |
| 73 | |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 74 | /* revision id */ |
| 75 | s_rid = strtok_r(NULL, ",", &saveptr); |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 76 | if (!s_rid) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 77 | goto out; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 78 | |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 79 | rid = strtol(s_rid, &endptr, 16); |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 80 | if (*endptr) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 81 | goto out; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 82 | |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 83 | if (rid != dev->info.pci_rev_id) { |
| 84 | r = -EAGAIN; |
| 85 | goto out; |
| 86 | } |
| 87 | |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 88 | /* marketing name */ |
| 89 | s_name = strtok_r(NULL, ",", &saveptr); |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 90 | if (!s_name) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 91 | goto out; |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 92 | |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 93 | /* trim leading whitespaces or tabs */ |
| 94 | while (isblank(*s_name)) |
| 95 | s_name++; |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 96 | if (strlen(s_name) == 0) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 97 | goto out; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 98 | |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 99 | dev->marketing_name = strdup(s_name); |
| 100 | if (dev->marketing_name) |
Michel Dänzer | 5219809 | 2017-12-01 16:59:38 +0100 | [diff] [blame] | 101 | r = 0; |
| 102 | else |
| 103 | r = -ENOMEM; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 104 | |
| 105 | out: |
| 106 | free(buf); |
| 107 | |
| 108 | return r; |
| 109 | } |
| 110 | |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 111 | void amdgpu_parse_asic_ids(struct amdgpu_device *dev) |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 112 | { |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 113 | FILE *fp; |
| 114 | char *line = NULL; |
| 115 | size_t len = 0; |
| 116 | ssize_t n; |
| 117 | int line_num = 1; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 118 | 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änzer | 85c6b0b | 2017-11-30 18:28:01 +0100 | [diff] [blame] | 124 | return; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 127 | /* 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 Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 144 | /* trim trailing newline */ |
| 145 | if (line[n - 1] == '\n') |
| 146 | line[n - 1] = '\0'; |
| 147 | |
Michel Dänzer | f05a2b4 | 2017-11-30 18:52:06 +0100 | [diff] [blame] | 148 | r = parse_one_line(dev, line); |
| 149 | if (r != -EAGAIN) |
| 150 | break; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 151 | |
| 152 | line_num++; |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 153 | } |
| 154 | |
Michel Dänzer | 85c6b0b | 2017-11-30 18:28:01 +0100 | [diff] [blame] | 155 | if (r == -EINVAL) { |
| 156 | fprintf(stderr, "Invalid format: %s: line %d: %s\n", |
| 157 | AMDGPU_ASIC_ID_TABLE, line_num, line); |
Michel Dänzer | 57d3d4c | 2018-01-08 11:20:25 +0100 | [diff] [blame^] | 158 | } else if (r && r != -EAGAIN) { |
Michel Dänzer | 85c6b0b | 2017-11-30 18:28:01 +0100 | [diff] [blame] | 159 | fprintf(stderr, "%s: Cannot parse ASIC IDs: %s\n", |
| 160 | __func__, strerror(-r)); |
| 161 | } |
| 162 | |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 163 | free(line); |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 164 | fclose(fp); |
Xiaojie Yuan | 7e6bf88 | 2017-05-31 16:22:50 -0400 | [diff] [blame] | 165 | } |