[PATCH libdrm v2] drm: add fallback function to get device name

Wang, Kevin(Yang) Kevin1.Wang at amd.com
Mon Jun 24 11:48:37 UTC 2019


1.optimize code logic.
2.if not found device name in amdgpu.ids,
will return a fallback device name "deviceid:revisionid", eg:69a0:01

Change-Id: I0d0c95a402533a74978681309680cb73d6a36e60
Signed-off-by: Kevin Wang <kevin1.wang at amd.com>
---
 amdgpu/amdgpu_asic_id.c  | 134 ++++++++-------------------------------
 amdgpu/amdgpu_device.c   |   5 +-
 amdgpu/amdgpu_internal.h |   1 +
 3 files changed, 33 insertions(+), 107 deletions(-)

diff --git a/amdgpu/amdgpu_asic_id.c b/amdgpu/amdgpu_asic_id.c
index a5007ffc80a7..6ecb9e573403 100644
--- a/amdgpu/amdgpu_asic_id.c
+++ b/amdgpu/amdgpu_asic_id.c
@@ -34,128 +34,50 @@
 #include "amdgpu_drm.h"
 #include "amdgpu_internal.h"
 
-static int parse_one_line(struct amdgpu_device *dev, const char *line)
+char* amdgpu_get_device_name(uint32_t devid, uint32_t revid)
 {
-	char *buf, *saveptr;
-	char *s_did;
-	uint32_t did;
-	char *s_rid;
-	uint32_t rid;
-	char *s_name;
-	char *endptr;
-	int r = -EINVAL;
 
-	/* ignore empty line and commented line */
-	if (strlen(line) == 0 || line[0] == '#')
-		return -EAGAIN;
-
-	buf = strdup(line);
-	if (!buf)
-		return -ENOMEM;
-
-	/* device id */
-	s_did = strtok_r(buf, ",", &saveptr);
-	if (!s_did)
-		goto out;
-
-	did = strtol(s_did, &endptr, 16);
-	if (*endptr)
-		goto out;
-
-	if (did != dev->info.asic_id) {
-		r = -EAGAIN;
-		goto out;
-	}
-
-	/* revision id */
-	s_rid = strtok_r(NULL, ",", &saveptr);
-	if (!s_rid)
-		goto out;
-
-	rid = strtol(s_rid, &endptr, 16);
-	if (*endptr)
-		goto out;
-
-	if (rid != dev->info.pci_rev_id) {
-		r = -EAGAIN;
-		goto out;
-	}
-
-	/* marketing name */
-	s_name = strtok_r(NULL, ",", &saveptr);
-	if (!s_name)
-		goto out;
-
-	/* trim leading whitespaces or tabs */
-	while (isblank(*s_name))
-		s_name++;
-	if (strlen(s_name) == 0)
-		goto out;
-
-	dev->marketing_name = strdup(s_name);
-	if (dev->marketing_name)
-		r = 0;
-	else
-		r = -ENOMEM;
-
-out:
-	free(buf);
-
-	return r;
-}
-
-void amdgpu_parse_asic_ids(struct amdgpu_device *dev)
-{
-	FILE *fp;
-	char *line = NULL;
-	size_t len = 0;
-	ssize_t n;
-	int line_num = 1;
-	int r = 0;
+	char *name = NULL;
+	FILE *fp = NULL;
+	char buf[100];
+	int a, b, c, len = 0;
+	uint32_t vid, rid;
 
 	fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
 	if (!fp) {
 		fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
 			strerror(errno));
-		return;
+		goto fallback;
 	}
 
-	/* 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);
+	while (fgets(buf, sizeof(buf), fp)) {
+		if (buf[0] == '#') continue;
 		break;
 	}
 
-	while ((n = getline(&line, &len, fp)) != -1) {
-		/* trim trailing newline */
-		if (line[n - 1] == '\n')
-			line[n - 1] = '\0';
+	if (fscanf(fp, "%d.%d.%d", &a, &b, &c) != 3)
+		goto fallback;
 
-		r = parse_one_line(dev, line);
-		if (r != -EAGAIN)
-			break;
+	/* check ids verison: should be 1.0.0 */
+	if (a != 1 || b != 0 || c != 0)
+		goto fallback;
 
-		line_num++;
+	while(!feof(fp) && fscanf(fp, "%4x, %2x, %[^,'\n']", &vid, &rid, buf) == 3) {
+		if (vid == devid && rid == revid) {
+			name = strdup(buf);
+			break;
+		}
 	}
 
-	if (r == -EINVAL) {
-		fprintf(stderr, "Invalid format: %s: line %d: %s\n",
-			AMDGPU_ASIC_ID_TABLE, line_num, line);
-	} else if (r && r != -EAGAIN) {
-		fprintf(stderr, "%s: Cannot parse ASIC IDs: %s\n",
-			__func__, strerror(-r));
+fallback:
+	if (name == NULL) {
+		memset(buf, 0, sizeof(buf));
+		len = snprintf(buf, sizeof(buf), "%04x:%02x", devid, revid);
+		buf[len] = '\0';
+		name = strdup(buf);
 	}
+	if (fp)
+		fclose(fp);
 
-	free(line);
-	fclose(fp);
+	return name;
 }
diff --git a/amdgpu/amdgpu_device.c b/amdgpu/amdgpu_device.c
index 8e74393a5518..4291c6628346 100644
--- a/amdgpu/amdgpu_device.c
+++ b/amdgpu/amdgpu_device.c
@@ -260,7 +260,10 @@ drm_public int amdgpu_device_initialize(int fd,
 	amdgpu_vamgr_init(&dev->vamgr_high, start, max,
 			  dev->dev_info.virtual_address_alignment);
 
-	amdgpu_parse_asic_ids(dev);
+	/* amdgpu_get_device_name(vid, rid) */
+	dev->marketing_name = amdgpu_get_device_name(dev->info.asic_id,
+						     dev->info.pci_rev_id);
+
 
 	*major_version = dev->major_version;
 	*minor_version = dev->minor_version;
diff --git a/amdgpu/amdgpu_internal.h b/amdgpu/amdgpu_internal.h
index b614b4acb81f..510abecd0042 100644
--- a/amdgpu/amdgpu_internal.h
+++ b/amdgpu/amdgpu_internal.h
@@ -142,6 +142,7 @@ drm_private void amdgpu_vamgr_init(struct amdgpu_bo_va_mgr *mgr, uint64_t start,
 drm_private void amdgpu_vamgr_deinit(struct amdgpu_bo_va_mgr *mgr);
 
 drm_private void amdgpu_parse_asic_ids(struct amdgpu_device *dev);
+drm_private char* amdgpu_get_device_name(uint32_t devid, uint32_t revid);
 
 drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
 
-- 
2.22.0



More information about the amd-gfx mailing list