Skip to contentSkip to conversation

amdgpu: move asic id table to a separate file

PATCH Superseded Samuel Li 2 participants 5 messages May 11, 2017 – May 16, 2017 4 messages not in the archive

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Samuel Li

author
#0 lore raw

PATCH v2 1/1 amdgpu: move asic id table to a separate file

From: Xiaojie Yuan <[email protected]>

v2: fix an off by one error and leading white spaces

Change-Id I12216da14910f5e2b0970bc1fafc2a20b0ef1ba9
---
 amdgpu/Makefile.am       |   2 +
 amdgpu/Makefile.sources  |   2 +-
 amdgpu/amdgpu_asic_id.c  | 198 +++++++++++++++++++++++++++++++++++++++++++++++
 amdgpu/amdgpu_asic_id.h  | 165 ---------------------------------------
 amdgpu/amdgpu_device.c   |  28 +++++--
 amdgpu/amdgpu_internal.h |  10 +++
 6 files changed, 232 insertions(+), 173 deletions(-)
 create mode 100644 amdgpu/amdgpu_asic_id.c
 delete mode 100644 amdgpu/amdgpu_asic_id.h
amdgpu/Makefile.am +2 -0
index cf7bc1b..ecf9e82 100644
@@ -30,6 +30,8 @@ AM_CFLAGS = \
 	$(PTHREADSTUBS_CFLAGS) \
 	-I$(top_srcdir)/include/drm
 
+AM_CPPFLAGS = -DAMDGPU_ASIC_ID_TABLE=\"${datadir}/libdrm/amdgpu.ids\"
+
 libdrm_amdgpu_la_LTLIBRARIES = libdrm_amdgpu.la
 libdrm_amdgpu_ladir = $(libdir)
 libdrm_amdgpu_la_LDFLAGS = -version-number 1:0:0 -no-undefined
amdgpu/Makefile.sources +1 -1
index 487b9e0..bc3abaa 100644
@@ -1,5 +1,5 @@
 LIBDRM_AMDGPU_FILES := \
-	amdgpu_asic_id.h \
+	amdgpu_asic_id.c \
 	amdgpu_bo.c \
 	amdgpu_cs.c \
 	amdgpu_device.c \
amdgpu/amdgpu_asic_id.c new file +198 -0

198 lines not on this page · raw message

amdgpu/amdgpu_asic_id.h deleted +0 -165

165 lines not on this page · raw message

amdgpu/amdgpu_device.c +21 -7
index f473d2d..9d08744 100644
@@ -44,7 +44,6 @@
 #include "amdgpu_internal.h"
 #include "util_hash_table.h"
 #include "util_math.h"
-#include "amdgpu_asic_id.h"
 
 #define PTR_TO_UINT(x) ((unsigned)((intptr_t)(x)))
 #define UINT_TO_PTR(x) ((void *)((intptr_t)(x)))
@@ -131,6 +130,7 @@ static int amdgpu_get_auth(int fd, int *auth)
 
 static void amdgpu_device_free_internal(amdgpu_device_handle dev)
 {
+	const struct amdgpu_asic_id *id;
 	amdgpu_vamgr_deinit(&dev->vamgr_32);
 	amdgpu_vamgr_deinit(&dev->vamgr);
 	util_hash_table_destroy(dev->bo_flink_names);
@@ -140,6 +140,13 @@ static void amdgpu_device_free_internal(amdgpu_device_handle dev)
 	close(dev->fd);
 	if ((dev->flink_fd >= 0) && (dev->fd != dev->flink_fd))
 		close(dev->flink_fd);
+	if (dev->asic_ids) {
+		for (id = dev->asic_ids; id->did; id++) {
+			if (id->marketing_name !=  NULL)
+				free(id->marketing_name);
+		}
+		free(dev->asic_ids);
+	}
 	free(dev);
 }
 
@@ -267,6 +274,11 @@ int amdgpu_device_initialize(int fd,
 	amdgpu_vamgr_init(&dev->vamgr_32, start, max,
 			  dev->dev_info.virtual_address_alignment);
 
+	r = amdgpu_parse_asic_ids(&dev->asic_ids);
+	if (r)
+		fprintf(stderr, "%s: Can not parse asic ids, 0x%x.",
+			__func__, r);
+
 	*major_version = dev->major_version;
 	*minor_version = dev->minor_version;
 	*device_handle = dev;
@@ -297,13 +309,15 @@ int amdgpu_device_deinitialize(amdgpu_device_handle dev)
 
 const char *amdgpu_get_marketing_name(amdgpu_device_handle dev)
 {
-	const struct amdgpu_asic_id_table_t *t = amdgpu_asic_id_table;
+	const struct amdgpu_asic_id *id;
+
+	if (!dev->asic_ids)
+		return NULL;
 
-	while (t->did) {
-		if ((t->did == dev->info.asic_id) &&
-		    (t->rid == dev->info.pci_rev_id))
-			return t->marketing_name;
-		t++;
+	for (id = dev->asic_ids; id->did; id++) {
+		if ((id->did == dev->info.asic_id) &&
+				(id->rid == dev->info.pci_rev_id))
+			return id->marketing_name;
 	}
 
 	return NULL;
amdgpu/amdgpu_internal.h +10 -0
index cf119a5..e68246b 100644
@@ -69,6 +69,12 @@ struct amdgpu_va {
 	struct amdgpu_bo_va_mgr *vamgr;
 };
 
+struct amdgpu_asic_id {
+	uint32_t did;
+	uint32_t rid;
+	char *marketing_name;
+};
+
 struct amdgpu_device {
 	atomic_t refcount;
 	int fd;
@@ -76,6 +82,8 @@ struct amdgpu_device {
 	unsigned major_version;
 	unsigned minor_version;
 
+	/** Lookup table of asic device id, revision id and marketing name */
+	struct amdgpu_asic_id *asic_ids;
 	/** List of buffer handles. Protected by bo_table_mutex. */
 	struct util_hash_table *bo_handles;
 	/** List of buffer GEM flink names. Protected by bo_table_mutex. */
@@ -149,6 +157,8 @@ amdgpu_vamgr_find_va(struct amdgpu_bo_va_mgr *mgr, uint64_t size,
 drm_private void
 amdgpu_vamgr_free_va(struct amdgpu_bo_va_mgr *mgr, uint64_t va, uint64_t size);
 
+drm_private int amdgpu_parse_asic_ids(struct amdgpu_asic_id **asic_ids);
+
 drm_private int amdgpu_query_gpu_info_init(amdgpu_device_handle dev);
 
 drm_private uint64_t amdgpu_cs_calculate_timeout(uint64_t timeout);
Signature · 6 lines
2.7.4

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx
#2 lore raw
In reply to a message not in the archive
Quoted 23 lines · Samuel Li

From: Xiaojie Yuan <[email protected]>

v2: fix an off by one error and leading white spaces

Change-Id I12216da14910f5e2b0970bc1fafc2a20b0ef1ba9
---
 amdgpu/Makefile.am       |   2 +
 amdgpu/Makefile.sources  |   2 +-
 amdgpu/amdgpu_asic_id.c  | 198 +++++++++++++++++++++++++++++++++++++++++++++++
 amdgpu/amdgpu_asic_id.h  | 165 ---------------------------------------
 amdgpu/amdgpu_device.c   |  28 +++++--
 amdgpu/amdgpu_internal.h |  10 +++
 6 files changed, 232 insertions(+), 173 deletions(-)
 create mode 100644 amdgpu/amdgpu_asic_id.c
 delete mode 100644 amdgpu/amdgpu_asic_id.h
amdgpu/Makefile.am +0 -0
index cf7bc1b..ecf9e82 100644

amdgpu/Makefile.am AM_CFLAGS = \ · line 33

Quoted 4 lines
amdgpu/Makefile.am +1 -0
@@ -30,6 +30,8 @@ AM_CFLAGS = \
        $(PTHREADSTUBS_CFLAGS) \
        -I$(top_srcdir)/include/drm
 
+AM_CPPFLAGS = -DAMDGPU_ASIC_ID_TABLE=\"${datadir}/libdrm/amdgpu.ids\"

If you add this here, you should add the ids file itself and make libdrm install it too...

Quoted 70 lines
amdgpu/Makefile.am +1 -0
+
 libdrm_amdgpu_la_LTLIBRARIES = libdrm_amdgpu.la
 libdrm_amdgpu_ladir = $(libdir)
 libdrm_amdgpu_la_LDFLAGS = -version-number 1:0:0 -no-undefined
amdgpu/Makefile.sources +1 -1
index 487b9e0..bc3abaa 100644
@@ -1,5 +1,5 @@
 LIBDRM_AMDGPU_FILES := \
-       amdgpu_asic_id.h \
+       amdgpu_asic_id.c \
        amdgpu_bo.c \
        amdgpu_cs.c \
        amdgpu_device.c \
amdgpu/amdgpu_asic_id.c new file +49 -0
new file mode 100644
index 0000000..067f38c
@@ -0,0 +1,198 @@
+/*
+ * Copyright © 2017 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "amdgpu_drm.h"
+#include "amdgpu_internal.h"
+
+static int parse_one_line(const char *line, struct amdgpu_asic_id *id)
+{
+       char *buf;
+       char *s_did;
+       char *s_rid;
+       char *s_name;
+       char *endptr;
+       int r = 0;
+
+       buf = strdup(line);
+       if (!buf)

amdgpu/amdgpu_asic_id.c line 59

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               return -ENOMEM;
+
+       /* ignore empty line and commented line */
+       if (strlen(line) == 0 || line[0] == '#') {
+               r = -EAGAIN;
+               goto out;
+       }
+
+       /* device id */
+       s_did = strtok(buf, ",");

You can't use strtok() in a library. Any other thread may call strtok() anytime too and screw you up.

Quoted 40 lines
amdgpu/amdgpu_asic_id.c new file +40 -0
+       if (!s_did) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->did = strtol(s_did, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* revision id */
+       s_rid = strtok(NULL, ",");
+       if (!s_rid) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->rid = strtol(s_rid, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* marketing name */
+       s_name = strtok(NULL, ",");
+       if (!s_name) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->marketing_name = strdup(s_name);
+       if (id->marketing_name == NULL) {
+               r = -EINVAL;
+               goto out;
+       }
+
+out:
+       free(buf);
+

amdgpu/amdgpu_asic_id.c line 109

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+       return r;
+}
+
+int amdgpu_parse_asic_ids(struct amdgpu_asic_id **p_asic_id_table)
+{
+       struct amdgpu_asic_id *asic_id_table;
+       struct amdgpu_asic_id *id;
+       FILE *fp;
+       char *line = NULL;
+       size_t len;

The manpage says len must be 0 before the first getline() call.

Quoted 15 lines
amdgpu/amdgpu_asic_id.c new file +15 -0
+       ssize_t n;
+       int line_num = 1;
+       size_t table_size = 0;
+       size_t table_max_size = 256;
+       int r = 0;
+
+       fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
+       if (!fp) {
+               fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
+                               strerror(errno));
+               return -EINVAL;
+       }
+
+       asic_id_table = calloc(table_max_size, sizeof(struct amdgpu_asic_id));
+       if (!asic_id_table) {

amdgpu/amdgpu_asic_id.c line 134

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               r = -ENOMEM;
+               goto close;
+       }
+
+       /* 1st line is file version */
+       if ((n = getline(&line, &len, fp)) != -1) {
+               /* trim trailing newline */
+               if (line[n - 1] == '\n')
+                       line[n - 1] = '\0';
+               printf("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);

debug leftover?

Gražvydas
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

author
#4 lore raw
In reply to a message not in the archive
Quoted 1 line

If you add this here, you should add the ids file itself and make libdrm install it too...

? Here the ids file is separate from libdrm. It is passed during compilation so that libdrm knows where to get it.

Quoted 1 line

You can't use strtok() in a library. Any other thread may call strtok() anytime too and screw you up.

Right, better to call strtok_r() instead.

Quoted 1 line

The manpage says len must be 0 before the first getline() call.

Right, that shall be safer.

Quoted 2 lines
Quoted 1 line

+ printf("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);

debug leftover?

This is to print out the ids file version.

Thanks,
Sam

-----Original Message-----
From: Grazvydas Ignotas [mailto:[email protected]]
Sent: Friday, May 12, 2017 8:16 AM
To: Li, Samuel <[email protected]>
Cc: [email protected]; Yuan, Xiaojie <[email protected]>
Subject: Re: [PATCH libdrm v2 1/1] amdgpu: move asic id table to a separate file

Quoted 24 lines · Samuel Li

From: Xiaojie Yuan <[email protected]>

v2: fix an off by one error and leading white spaces

Change-Id I12216da14910f5e2b0970bc1fafc2a20b0ef1ba9
---
 amdgpu/Makefile.am       |   2 +
 amdgpu/Makefile.sources  |   2 +-
 amdgpu/amdgpu_asic_id.c  | 198 
+++++++++++++++++++++++++++++++++++++++++++++++
 amdgpu/amdgpu_asic_id.h  | 165 ---------------------------------------
 amdgpu/amdgpu_device.c   |  28 +++++--
 amdgpu/amdgpu_internal.h |  10 +++
 6 files changed, 232 insertions(+), 173 deletions(-)  create mode 

100644 amdgpu/amdgpu_asic_id.c delete mode 100644 amdgpu/amdgpu_asic_id.h

amdgpu/Makefile.am index renamed from amdgpu/Makefile.am +0 -0

cf7bc1b..ecf9e82 100644

amdgpu/Makefile.am AM_CFLAGS = \ · line 33

Quoted 4 lines
amdgpu/Makefile.am +1 -0
@@ -30,6 +30,8 @@ AM_CFLAGS = \
        $(PTHREADSTUBS_CFLAGS) \
        -I$(top_srcdir)/include/drm
 
+AM_CPPFLAGS = -DAMDGPU_ASIC_ID_TABLE=\"${datadir}/libdrm/amdgpu.ids\"

If you add this here, you should add the ids file itself and make libdrm install it too...

Quoted 80 lines
amdgpu/Makefile.am +1 -0
+
 libdrm_amdgpu_la_LTLIBRARIES = libdrm_amdgpu.la  libdrm_amdgpu_ladir 

= $(libdir) libdrm_amdgpu_la_LDFLAGS = -version-number 1:0:0 -no-undefined diff --git a/amdgpu/Makefile.sources b/amdgpu/Makefile.sources index 487b9e0..bc3abaa 100644

amdgpu/Makefile.sources +1 -1
@@ -1,5 +1,5 @@
 LIBDRM_AMDGPU_FILES := \
-       amdgpu_asic_id.h \
+       amdgpu_asic_id.c \
        amdgpu_bo.c \
        amdgpu_cs.c \
        amdgpu_device.c \
amdgpu/amdgpu_asic_id.c new renamed from amdgpu/amdgpu_asic_id.c +0 -0

file mode 100644 index 0000000..067f38c

amdgpu/amdgpu_asic_id.c new file +61 -0
@@ -0,0 +1,198 @@
+/*
+ * Copyright © 2017 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person 
+obtaining a
+ * copy of this software and associated documentation files (the 
+"Software"),
+ * to deal in the Software without restriction, including without 
+limitation
+ * the rights to use, copy, modify, merge, publish, distribute, 
+sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom 
+the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be 
+included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
+EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
+MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT 
+SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, 
+DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
+OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 
+OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "amdgpu_drm.h"
+#include "amdgpu_internal.h"
+
+static int parse_one_line(const char *line, struct amdgpu_asic_id 
+*id) {
+       char *buf;
+       char *s_did;
+       char *s_rid;
+       char *s_name;
+       char *endptr;
+       int r = 0;
+
+       buf = strdup(line);
+       if (!buf)

amdgpu/amdgpu_asic_id.c line 71

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               return -ENOMEM;
+
+       /* ignore empty line and commented line */
+       if (strlen(line) == 0 || line[0] == '#') {
+               r = -EAGAIN;
+               goto out;
+       }
+
+       /* device id */
+       s_did = strtok(buf, ",");

You can't use strtok() in a library. Any other thread may call strtok() anytime too and screw you up.

Quoted 39 lines
amdgpu/amdgpu_asic_id.c new file +39 -0
+       if (!s_did) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->did = strtol(s_did, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* revision id */
+       s_rid = strtok(NULL, ",");
+       if (!s_rid) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->rid = strtol(s_rid, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* marketing name */
+       s_name = strtok(NULL, ",");
+       if (!s_name) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->marketing_name = strdup(s_name);
+       if (id->marketing_name == NULL) {
+               r = -EINVAL;
+               goto out;
+       }
+
+out:
+       free(buf);

amdgpu/amdgpu_asic_id.c line 109

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+
+       return r;
+}
+
+int amdgpu_parse_asic_ids(struct amdgpu_asic_id **p_asic_id_table) {
+       struct amdgpu_asic_id *asic_id_table;
+       struct amdgpu_asic_id *id;
+       FILE *fp;
+       char *line = NULL;
+       size_t len;

The manpage says len must be 0 before the first getline() call.

Quoted 16 lines
amdgpu/amdgpu_asic_id.c new file +16 -0
+       ssize_t n;
+       int line_num = 1;
+       size_t table_size = 0;
+       size_t table_max_size = 256;
+       int r = 0;
+
+       fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
+       if (!fp) {
+               fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
+                               strerror(errno));
+               return -EINVAL;
+       }
+
+       asic_id_table = calloc(table_max_size, sizeof(struct amdgpu_asic_id));
+       if (!asic_id_table) {
+               r = -ENOMEM;

amdgpu/amdgpu_asic_id.c

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               goto close;
+       }
+
+       /* 1st line is file version */
+       if ((n = getline(&line, &len, fp)) != -1) {
+               /* trim trailing newline */
+               if (line[n - 1] == '\n')
+                       line[n - 1] = '\0';
+               printf("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, 
+ line);

debug leftover?

Gražvydas
_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

#6 lore raw
In reply to a message not in the archive
Quoted 2 lines · Samuel
Quoted 1 line

If you add this here, you should add the ids file itself and make libdrm install it too...

? Here the ids file is separate from libdrm. It is passed during compilation so that libdrm knows where to get it.

OK then, how will users that compile the OSS stack know where to get that file (or that they need some new file at all) after your patch is applied?

Quoted 3 lines
Quoted 2 lines
Quoted 1 line

+ printf("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, line);

debug leftover?

This is to print out the ids file version.

Libraries can't print to stdout as it will break all programs that parse the output, apitrace is one such example.

Quoted 218 lines

Thanks,
Sam

-----Original Message-----
From: Grazvydas Ignotas [mailto:[email protected]]
Sent: Friday, May 12, 2017 8:16 AM
To: Li, Samuel <[email protected]>
Cc: [email protected]; Yuan, Xiaojie <[email protected]>
Subject: Re: [PATCH libdrm v2 1/1] amdgpu: move asic id table to a separate file

Quoted 24 lines · Samuel Li

From: Xiaojie Yuan <[email protected]>

v2: fix an off by one error and leading white spaces

Change-Id I12216da14910f5e2b0970bc1fafc2a20b0ef1ba9
---
 amdgpu/Makefile.am       |   2 +
 amdgpu/Makefile.sources  |   2 +-
 amdgpu/amdgpu_asic_id.c  | 198
+++++++++++++++++++++++++++++++++++++++++++++++
 amdgpu/amdgpu_asic_id.h  | 165 ---------------------------------------
 amdgpu/amdgpu_device.c   |  28 +++++--
 amdgpu/amdgpu_internal.h |  10 +++
 6 files changed, 232 insertions(+), 173 deletions(-)  create mode

100644 amdgpu/amdgpu_asic_id.c delete mode 100644 amdgpu/amdgpu_asic_id.h

amdgpu/Makefile.am index renamed from amdgpu/Makefile.am +0 -0

cf7bc1b..ecf9e82 100644

amdgpu/Makefile.am AM_CFLAGS = \ · line 33

Quoted 4 lines
amdgpu/Makefile.am +1 -0
@@ -30,6 +30,8 @@ AM_CFLAGS = \
        $(PTHREADSTUBS_CFLAGS) \
        -I$(top_srcdir)/include/drm
 
+AM_CPPFLAGS = -DAMDGPU_ASIC_ID_TABLE=\"${datadir}/libdrm/amdgpu.ids\"

If you add this here, you should add the ids file itself and make libdrm install it too...

Quoted 80 lines
amdgpu/Makefile.am +1 -0
+
 libdrm_amdgpu_la_LTLIBRARIES = libdrm_amdgpu.la  libdrm_amdgpu_ladir

= $(libdir) libdrm_amdgpu_la_LDFLAGS = -version-number 1:0:0 -no-undefined diff --git a/amdgpu/Makefile.sources b/amdgpu/Makefile.sources index 487b9e0..bc3abaa 100644

amdgpu/Makefile.sources +1 -1
@@ -1,5 +1,5 @@
 LIBDRM_AMDGPU_FILES := \
-       amdgpu_asic_id.h \
+       amdgpu_asic_id.c \
        amdgpu_bo.c \
        amdgpu_cs.c \
        amdgpu_device.c \
amdgpu/amdgpu_asic_id.c new renamed from amdgpu/amdgpu_asic_id.c +0 -0

file mode 100644 index 0000000..067f38c

amdgpu/amdgpu_asic_id.c new file +61 -0
@@ -0,0 +1,198 @@
+/*
+ * Copyright © 2017 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+obtaining a
+ * copy of this software and associated documentation files (the
+"Software"),
+ * to deal in the Software without restriction, including without
+limitation
+ * the rights to use, copy, modify, merge, publish, distribute,
+sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom
+the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT
+SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM,
+DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
+OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "amdgpu_drm.h"
+#include "amdgpu_internal.h"
+
+static int parse_one_line(const char *line, struct amdgpu_asic_id
+*id) {
+       char *buf;
+       char *s_did;
+       char *s_rid;
+       char *s_name;
+       char *endptr;
+       int r = 0;
+
+       buf = strdup(line);
+       if (!buf)

amdgpu/amdgpu_asic_id.c line 71

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               return -ENOMEM;
+
+       /* ignore empty line and commented line */
+       if (strlen(line) == 0 || line[0] == '#') {
+               r = -EAGAIN;
+               goto out;
+       }
+
+       /* device id */
+       s_did = strtok(buf, ",");

You can't use strtok() in a library. Any other thread may call strtok() anytime too and screw you up.

Quoted 39 lines
amdgpu/amdgpu_asic_id.c new file +39 -0
+       if (!s_did) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->did = strtol(s_did, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* revision id */
+       s_rid = strtok(NULL, ",");
+       if (!s_rid) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->rid = strtol(s_rid, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* marketing name */
+       s_name = strtok(NULL, ",");
+       if (!s_name) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->marketing_name = strdup(s_name);
+       if (id->marketing_name == NULL) {
+               r = -EINVAL;
+               goto out;
+       }
+
+out:
+       free(buf);

amdgpu/amdgpu_asic_id.c line 109

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+
+       return r;
+}
+
+int amdgpu_parse_asic_ids(struct amdgpu_asic_id **p_asic_id_table) {
+       struct amdgpu_asic_id *asic_id_table;
+       struct amdgpu_asic_id *id;
+       FILE *fp;
+       char *line = NULL;
+       size_t len;

The manpage says len must be 0 before the first getline() call.

Quoted 16 lines
amdgpu/amdgpu_asic_id.c new file +16 -0
+       ssize_t n;
+       int line_num = 1;
+       size_t table_size = 0;
+       size_t table_max_size = 256;
+       int r = 0;
+
+       fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
+       if (!fp) {
+               fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
+                               strerror(errno));
+               return -EINVAL;
+       }
+
+       asic_id_table = calloc(table_max_size, sizeof(struct amdgpu_asic_id));
+       if (!asic_id_table) {
+               r = -ENOMEM;

amdgpu/amdgpu_asic_id.c

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               goto close;
+       }
+
+       /* 1st line is file version */
+       if ((n = getline(&line, &len, fp)) != -1) {
+               /* trim trailing newline */
+               if (line[n - 1] == '\n')
+                       line[n - 1] = '\0';
+               printf("%s version: %s\n", AMDGPU_ASIC_ID_TABLE,
+ line);

debug leftover?

Quoted 1 line

Gražvydas

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx

author
#8 lore raw
In reply to a message not in the archive
Quoted 1 line

OK then, how will users that compile the OSS stack know where to get that file (or that they need some new file at all) after your patch is applied?

There is a discussion regarding whether the ids file shall be separate upstream. This question can be answer after that ... previously we were thinking a fixed path /usr/share/libdrm.

Quoted 1 line

Libraries can't print to stdout as it will break all programs that parse the output, apitrace is one such example.

That sounds a good practice.

Sam

-----Original Message-----
From: Grazvydas Ignotas [mailto:[email protected]]
Sent: Sunday, May 14, 2017 1:46 PM
To: Li, Samuel <[email protected]>
Cc: [email protected]; Yuan, Xiaojie <[email protected]>
Subject: Re: [PATCH libdrm v2 1/1] amdgpu: move asic id table to a separate file

Quoted 2 lines · Samuel
Quoted 1 line

If you add this here, you should add the ids file itself and make libdrm install it too...

? Here the ids file is separate from libdrm. It is passed during compilation so that libdrm knows where to get it.

OK then, how will users that compile the OSS stack know where to get that file (or that they need some new file at all) after your patch is applied?

Quoted 4 lines
Quoted 3 lines
Quoted 2 lines

+ printf("%s version: %s\n", AMDGPU_ASIC_ID_TABLE,
+ line);

debug leftover?

This is to print out the ids file version.

Libraries can't print to stdout as it will break all programs that parse the output, apitrace is one such example.

Quoted 220 lines

Thanks,
Sam

-----Original Message-----
From: Grazvydas Ignotas [mailto:[email protected]]
Sent: Friday, May 12, 2017 8:16 AM
To: Li, Samuel <[email protected]>
Cc: [email protected]; Yuan, Xiaojie <[email protected]>
Subject: Re: [PATCH libdrm v2 1/1] amdgpu: move asic id table to a separate file

Quoted 24 lines · Samuel Li

From: Xiaojie Yuan <[email protected]>

v2: fix an off by one error and leading white spaces

Change-Id I12216da14910f5e2b0970bc1fafc2a20b0ef1ba9
---
 amdgpu/Makefile.am       |   2 +
 amdgpu/Makefile.sources  |   2 +-
 amdgpu/amdgpu_asic_id.c  | 198
+++++++++++++++++++++++++++++++++++++++++++++++
 amdgpu/amdgpu_asic_id.h  | 165 ---------------------------------------
 amdgpu/amdgpu_device.c   |  28 +++++--
 amdgpu/amdgpu_internal.h |  10 +++
 6 files changed, 232 insertions(+), 173 deletions(-)  create mode

100644 amdgpu/amdgpu_asic_id.c delete mode 100644 amdgpu/amdgpu_asic_id.h

amdgpu/Makefile.am index renamed from amdgpu/Makefile.am +0 -0

cf7bc1b..ecf9e82 100644

amdgpu/Makefile.am AM_CFLAGS = \ · line 33

Quoted 4 lines
amdgpu/Makefile.am +1 -0
@@ -30,6 +30,8 @@ AM_CFLAGS = \
        $(PTHREADSTUBS_CFLAGS) \
        -I$(top_srcdir)/include/drm
 
+AM_CPPFLAGS = -DAMDGPU_ASIC_ID_TABLE=\"${datadir}/libdrm/amdgpu.ids\"

If you add this here, you should add the ids file itself and make libdrm install it too...

Quoted 80 lines
amdgpu/Makefile.am +1 -0
+
 libdrm_amdgpu_la_LTLIBRARIES = libdrm_amdgpu.la  libdrm_amdgpu_ladir 

= $(libdir) libdrm_amdgpu_la_LDFLAGS = -version-number 1:0:0 -no-undefined diff --git a/amdgpu/Makefile.sources b/amdgpu/Makefile.sources index 487b9e0..bc3abaa 100644

amdgpu/Makefile.sources +1 -1
@@ -1,5 +1,5 @@
 LIBDRM_AMDGPU_FILES := \
-       amdgpu_asic_id.h \
+       amdgpu_asic_id.c \
        amdgpu_bo.c \
        amdgpu_cs.c \
        amdgpu_device.c \
amdgpu/amdgpu_asic_id.c new renamed from amdgpu/amdgpu_asic_id.c +0 -0

file mode 100644 index 0000000..067f38c

amdgpu/amdgpu_asic_id.c new file +61 -0
@@ -0,0 +1,198 @@
+/*
+ * Copyright © 2017 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person 
+obtaining a
+ * copy of this software and associated documentation files (the 
+"Software"),
+ * to deal in the Software without restriction, including without 
+limitation
+ * the rights to use, copy, modify, merge, publish, distribute, 
+sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom 
+the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be 
+included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
+EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
+MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO 
+EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, 
+DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
+OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
+USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "amdgpu_drm.h"
+#include "amdgpu_internal.h"
+
+static int parse_one_line(const char *line, struct amdgpu_asic_id
+*id) {
+       char *buf;
+       char *s_did;
+       char *s_rid;
+       char *s_name;
+       char *endptr;
+       int r = 0;
+
+       buf = strdup(line);
+       if (!buf)

amdgpu/amdgpu_asic_id.c line 71

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               return -ENOMEM;
+
+       /* ignore empty line and commented line */
+       if (strlen(line) == 0 || line[0] == '#') {
+               r = -EAGAIN;
+               goto out;
+       }
+
+       /* device id */
+       s_did = strtok(buf, ",");

You can't use strtok() in a library. Any other thread may call strtok() anytime too and screw you up.

Quoted 39 lines
amdgpu/amdgpu_asic_id.c new file +39 -0
+       if (!s_did) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->did = strtol(s_did, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* revision id */
+       s_rid = strtok(NULL, ",");
+       if (!s_rid) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->rid = strtol(s_rid, &endptr, 16);
+       if (*endptr) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       /* marketing name */
+       s_name = strtok(NULL, ",");
+       if (!s_name) {
+               r = -EINVAL;
+               goto out;
+       }
+
+       id->marketing_name = strdup(s_name);
+       if (id->marketing_name == NULL) {
+               r = -EINVAL;
+               goto out;
+       }
+
+out:
+       free(buf);

amdgpu/amdgpu_asic_id.c line 120

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+
+       return r;
+}
+
+int amdgpu_parse_asic_ids(struct amdgpu_asic_id **p_asic_id_table) {
+       struct amdgpu_asic_id *asic_id_table;
+       struct amdgpu_asic_id *id;
+       FILE *fp;
+       char *line = NULL;
+       size_t len;

The manpage says len must be 0 before the first getline() call.

Quoted 16 lines
amdgpu/amdgpu_asic_id.c new file +16 -0
+       ssize_t n;
+       int line_num = 1;
+       size_t table_size = 0;
+       size_t table_max_size = 256;
+       int r = 0;
+
+       fp = fopen(AMDGPU_ASIC_ID_TABLE, "r");
+       if (!fp) {
+               fprintf(stderr, "%s: %s\n", AMDGPU_ASIC_ID_TABLE,
+                               strerror(errno));
+               return -EINVAL;
+       }
+
+       asic_id_table = calloc(table_max_size, sizeof(struct amdgpu_asic_id));
+       if (!asic_id_table) {
+               r = -ENOMEM;

amdgpu/amdgpu_asic_id.c line 146

Quoted 10 lines
amdgpu/amdgpu_asic_id.c new file +10 -0
+               goto close;
+       }
+
+       /* 1st line is file version */
+       if ((n = getline(&line, &len, fp)) != -1) {
+               /* trim trailing newline */
+               if (line[n - 1] == '\n')
+                       line[n - 1] = '\0';
+               printf("%s version: %s\n", AMDGPU_ASIC_ID_TABLE, 
+ line);

debug leftover?

Quoted 1 line

Gražvydas

_______________________________________________
amd-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/amd-gfx