<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"><!-- P {margin-top:0;margin-bottom:0;} --></style>
</head>
<body dir="ltr">
<div id="divtagdefaultwrapper" style="font-size: 12pt; color: rgb(0, 0, 0); font-family: Calibri, Helvetica, sans-serif, EmojiFont, "Apple Color Emoji", "Segoe UI Emoji", NotoColorEmoji, "Segoe UI Symbol", "Android Emoji", EmojiSymbols;" dir="ltr">
<p style="margin-top:0;margin-bottom:0">changes history:</p>
<p style="margin-top:0;margin-bottom:0">v1: 1. <span style="font-size: 12pt;">Simplify the old code logic,</span></p>
<p style="margin-top:0;margin-bottom:0"><span style="font-size: 12pt;"> 2. Provides a backup function when the name is not found in the amdgpu.ids file</span></p>
<p style="margin-top:0;margin-bottom:0">v2: 1. when open ids file fail, not ids version or ids version not matched, it will generate a device name by device id and revision id. eg:69a0:01</p>
<p style="margin-top:0;margin-bottom:0"> 2. fix miss a close file <span style="font-size: 12pt;">operation.</span></p>
<p style="margin-top:0;margin-bottom:0"><br>
</p>
<p style="margin-top:0;margin-bottom:0">BR,</p>
<p style="margin-top:0;margin-bottom:0">Kevin</p>
<div style="color: rgb(0, 0, 0);">
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Wang, Kevin(Yang) <kevin1.wang@amd.com><br>
<b>Sent:</b> Monday, June 24, 2019 7:48 PM<br>
<b>To:</b> amd-gfx@lists.freedesktop.org<br>
<b>Cc:</b> Daenzer, Michel; Wang, Kevin(Yang)<br>
<b>Subject:</b> [PATCH libdrm v2] drm: add fallback function to get device name</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">1.optimize code logic.<br>
2.if not found device name in amdgpu.ids,<br>
will return a fallback device name "deviceid:revisionid", eg:69a0:01<br>
<br>
Change-Id: I0d0c95a402533a74978681309680cb73d6a36e60<br>
Signed-off-by: Kevin Wang <kevin1.wang@amd.com><br>
---<br>
amdgpu/amdgpu_asic_id.c | 134 ++++++++-------------------------------<br>
amdgpu/amdgpu_device.c | 5 +-<br>
amdgpu/amdgpu_internal.h | 1 +<br>
3 files changed, 33 insertions(+), 107 deletions(-)<br>
<br>
diff --git a/amdgpu/amdgpu_asic_id.c b/amdgpu/amdgpu_asic_id.c<br>
index a5007ffc80a7..6ecb9e573403 100644<br>
--- a/amdgpu/amdgpu_asic_id.c<br>
+++ b/amdgpu/amdgpu_asic_id.c<br>
@@ -34,128 +34,50 @@<br>
#include "amdgpu_drm.h"<br>
#include "amdgpu_internal.h"<br>
<br>
-static int parse_one_line(struct amdgpu_device *dev, const char *line)<br>
+char* amdgpu_get_device_name(uint32_t devid, uint32_t revid)<br>
{<br>
- char *buf, *saveptr;<br>
- char *s_did;<br>
- uint32_t did;<br>
- char *s_rid;<br>
- uint32_t rid;<br>
- char *s_name;<br>
- char *endptr;<br>
- int r = -EINVAL;<br>
<br>
- /* ignore empty line and commented line */<br>
- if (strlen(line) == 0 || line[0] == '#')<br>
- return -EAGAIN;<br>
-<br>
- buf = strdup(line);<br>
- if (!buf)<br>
- return -ENOMEM;<br>
-<br>
- /* device id */<br>
- s_did = strtok_r(buf, ",", &saveptr);<br>
- if (!s_did)<br>
- goto out;<br>
-<br>
- did = strtol(s_did, &endptr, 16);<br>
- if (*endptr)<br>
- goto out;<br>
-<br>
- if (did != dev->info.asic_id) {<br>
- r = -EAGAIN;<br>
- goto out;<br>
- }<br>
-<br>
- /* revision id */<br>
- s_rid = strtok_r(NULL, ",", &saveptr);<br>
- if (!s_rid)<br>
- goto out;<br>
-<br>
- rid = strtol(s_rid, &endptr, 16);<br>
- if (*endptr)<br>
- goto out;<br>
-<br>
- if (rid != dev->info.pci_rev_id) {<br>
- r = -EAGAIN;<br>
- goto out;<br>
- }<br>
-<br>
- /* marketing name */<br>
- s_name = strtok_r(NULL, ",", &saveptr);<br>
- if (!s_name)<br>
- goto out;<br>
-<br>
- /* trim leading whitespaces or tabs */<br>
- while (isblank(*s_name))<br>
- s_name++;<br>
- if (strlen(s_name) == 0)<br>
- goto out;<br>
-<br>
- dev->marketing_name = strdup(s_name);<br>
- if (dev->marketing_name)<br>
- r = 0;<br>
- else<br>
- r = -ENOMEM;<br>
-<br>
-out:<br>
- free(buf);<br>
-<br>
- return r;<br>
-}<br>
-<br>
-void amdgpu_parse_asic_ids(struct amdgpu_device *dev)<br>
-{<br>
- FILE *fp;<br>
- char *line = NULL;<br>
- size_t len = 0;<br>
- ssize_t n;<br>
- int line_num = 1;<br>
- int r = 0;<br>
+ char *name = NULL;<br>
+ FILE *fp = NULL;<br>
+ char buf[100];<br>
+ int a, b, c, len = 0;<br>
+ uint32_t vid, rid;<br>
<br>
fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");<br>
if (!fp) {<br>
fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,<br>
strerror(errno));<br>
- return;<br>
+ goto fallback;<br>
}<br>
<br>
- /* 1st valid line is file version */<br>
- while ((n = getline(&line, &len, fp)) != -1) {<br>
- /* trim trailing newline */<br>
- if (line[n - 1] == '\n')<br>
- line[n - 1] = '\0';<br>
-<br>
- /* ignore empty line and commented line */<br>
- if (strlen(line) == 0 || line[0] == '#') {<br>
- line_num++;<br>
- continue;<br>
- }<br>
-<br>
- drmMsg("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);<br>
+ while (fgets(buf, sizeof(buf), fp)) {<br>
+ if (buf[0] == '#') continue;<br>
break;<br>
}<br>
<br>
- while ((n = getline(&line, &len, fp)) != -1) {<br>
- /* trim trailing newline */<br>
- if (line[n - 1] == '\n')<br>
- line[n - 1] = '\0';<br>
+ if (fscanf(fp, "%d.%d.%d", &a, &b, &c) != 3)<br>
+ goto fallback;<br>
<br>
- r = parse_one_line(dev, line);<br>
- if (r != -EAGAIN)<br>
- break;<br>
+ /* check ids verison: should be 1.0.0 */<br>
+ if (a != 1 || b != 0 || c != 0)<br>
+ goto fallback;<br>
<br>
- line_num++;<br>
+ while(!feof(fp) && fscanf(fp, "%4x, %2x, %[^,'\n']", &vid, &rid, buf) == 3) {<br>
+ if (vid == devid && rid == revid) {<br>
+ name = strdup(buf);<br>
+ break;<br>
+ }<br>
}<br>
<br>
- if (r == -EINVAL) {<br>
- fprintf(stderr, "Invalid format: %s: line %d: %s\n",<br>
- AMDGPU_ASIC_ID_TABLE, line_num, line);<br>
- } else if (r && r != -EAGAIN) {<br>
- fprintf(stderr, "%s: Cannot parse ASIC IDs: %s\n",<br>
- __func__, strerror(-r));<br>
+fallback:<br>
+ if (name == NULL) {<br>
+ memset(buf, 0, sizeof(buf));<br>
+ len = snprintf(buf, sizeof(buf), "%04x:%02x", devid, revid);<br>
+ buf[len] = '\0';<br>
+ name = strdup(buf);<br>
}<br>
+ if (fp)<br>
+ fclose(fp);<br>
<br>
- free(line);<br>
- fclose(fp);<br>
+ return name;<br>
}<br>
diff --git a/amdgpu/amdgpu_device.c b/amdgpu/amdgpu_device.c<br>
index 8e74393a5518..4291c6628346 100644<br>
--- a/amdgpu/amdgpu_device.c<br>
+++ b/amdgpu/amdgpu_device.c<br>
@@ -260,7 +260,10 @@ drm_public int amdgpu_device_initialize(int fd,<br>
amdgpu_vamgr_init(&dev->vamgr_high, start, max,<br>
dev->dev_info.virtual_address_alignment);<br>
<br>
- amdgpu_parse_asic_ids(dev);<br>
+ /* amdgpu_get_device_name(vid, rid) */<br>
+ dev->marketing_name = amdgpu_get_device_name(dev->info.asic_id,<br>
+ dev->info.pci_rev_id);<br>
+<br>
<br>
*major_version = dev->major_version;<br>
*minor_version = dev->minor_version;<br>
diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h<br>
index b614b4acb81f..510abecd0042 100644<br>
--- a/amdgpu/amdgpu_internal.h<br>
+++ b/amdgpu/amdgpu_internal.h<br>
@@ -142,6 +142,7 @@ drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,<br>
drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);<br>
<br>
drm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);<br>
+drm_private char* amdgpu_get_device_name(uint32_t devid, uint32_t revid);<br>
<br>
drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);<br>
<br>
-- <br>
2.22.0<br>
<br>
</div>
</span></font></div>
</div>
</div>
</body>
</html>