Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: "Jesse.zhang@amd.com" <jesse.zhang@amd.com>
To: <igt-dev@lists.freedesktop.org>
Cc: Vitaly Prosyak <vitaly.prosyak@amd.com>,
	Alex Deucher <alexander.deucher@amd.com>,
	Christian Koenig <christian.koenig@amd.com>,
	"Jesse.zhang@amd.com" <Jesse.zhang@amd.com>,
	Jesse Zhang <jesse.zhang@amd.com>
Subject: [PATCH i-g-t v2 2/3] lib/amdgpu: Extract PCI device address from file descriptor
Date: Fri, 3 Jan 2025 16:17:33 +0800	[thread overview]
Message-ID: <20250103081734.2338675-2-jesse.zhang@amd.com> (raw)
In-Reply-To: <20250103081734.2338675-1-jesse.zhang@amd.com>

From: "Jesse.zhang@amd.com" <Jesse.zhang@amd.com>

Get the pci domain, device and function number from the amdgpu device's fd.

Cc: Vitaly Prosyak <vitaly.prosyak@amd.com>
Cc: Christian Koenig <christian.koenig@amd.com>
Cc: Alexander Deucher <alexander.deucher@amd.com>

Suggest-by: Vitaly Prosyak <vitaly.prosyak@amd.com>
Signed-off-by: Jesse Zhang  <jesse.zhang@amd.com>
---
 lib/amdgpu/amd_ip_blocks.c | 64 ++++++++++++++++++++++++++++++++++++++
 lib/amdgpu/amd_ip_blocks.h | 17 ++++++++++
 2 files changed, 81 insertions(+)

diff --git a/lib/amdgpu/amd_ip_blocks.c b/lib/amdgpu/amd_ip_blocks.c
index cc4163bfc..05e6e2622 100644
--- a/lib/amdgpu/amd_ip_blocks.c
+++ b/lib/amdgpu/amd_ip_blocks.c
@@ -1047,3 +1047,67 @@ is_reset_enable(enum amd_ip_block_type ip_type, uint32_t reset_type)
 
         return enable;
 }
+
+/**
+ * get_pci_addr_from_fd - Extracts the PCI device address from a file descriptor.
+ * @fd: The file descriptor to extract the address from.
+ * @pci: Pointer to a pci_addr struct to store the extracted address.
+ *
+ * Returns 0 on success, or a negative error code on failure.
+ */
+int get_pci_addr_from_fd(int fd, struct pci_addr *pci)
+{
+    char path[80];
+    struct stat st;
+    char link[20], pci_path[256];
+    char *buf;
+    int len, sysfs;
+    int ret;
+
+    // Check if the file descriptor is a character device and can be accessed
+    if (fstat(fd, &st) < 0 || !S_ISCHR(st.st_mode))
+        return -1;
+
+    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d",
+             major(st.st_rdev), minor(st.st_rdev));
+
+    // Check if the sysfs path exists
+    if (access(path, F_OK) < 0)
+        return -1;
+
+    // Open the sysfs directory
+    sysfs = open(path, O_RDONLY);
+    if (sysfs < 0) {
+        perror("open");
+        return -1;
+    }
+
+    // Read the "device" link from the sysfs directory
+    snprintf(link, sizeof(link), "device");
+    len = readlinkat(sysfs, link, pci_path, sizeof(pci_path) - 1);
+    if (len == -1) {
+        close(sysfs);
+        return -ENOENT;
+    }
+    close(sysfs);
+ // Null-terminate the extracted path
+    pci_path[len] = '\0';
+
+    // Find the last occurrence of '/' in the extracted path
+    buf = strrchr(pci_path, '/');
+    if (!buf)
+        return -ENOENT;
+
+    // Extract the PCI device address from the path using sscanf
+    ret = sscanf(buf, "/%4x:%2x:%2x.%2x",
+                     &pci->domain, &pci->bus,
+                     &pci->device, &pci->function);
+
+    if (ret != 4) {
+        printf("Unable to extract PCI device address from '%s'\n", buf);
+        return -ENOENT;
+    }
+
+    return 0;
+}
+
diff --git a/lib/amdgpu/amd_ip_blocks.h b/lib/amdgpu/amd_ip_blocks.h
index 337ef3c25..dc4d87151 100644
--- a/lib/amdgpu/amd_ip_blocks.h
+++ b/lib/amdgpu/amd_ip_blocks.h
@@ -8,6 +8,13 @@
 #define AMD_IP_BLOCKS_H
 
 #include <amdgpu_drm.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/sysmacros.h>
 
 #include "amd_registers.h"
 #include "amd_family.h"
@@ -183,6 +190,13 @@ struct chip_info {
 	amdgpu_device_handle dev;
 };
 
+struct pci_addr {
+    unsigned int domain;
+    unsigned int bus;
+    unsigned int device;
+    unsigned int function;
+};
+
 extern  struct amdgpu_ip_blocks_device amdgpu_ips;
 extern const struct chip_info  *g_pChip;
 int
@@ -220,4 +234,7 @@ asic_rings_readness(amdgpu_device_handle device_handle, uint32_t mask, bool arr[
 
 bool
 is_reset_enable(enum amd_ip_block_type ip_type, uint32_t reset_type);
+
+int
+get_pci_addr_from_fd(int fd, struct pci_addr *pci);
 #endif
-- 
2.25.1


  reply	other threads:[~2025-01-03  8:17 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-03  8:17 [PATCH i-g-t v2 1/3] lib/amdgpu: enhance wait memory helper Jesse.zhang@amd.com
2025-01-03  8:17 ` Jesse.zhang@amd.com [this message]
2025-01-03  8:17 ` [PATCH i-g-t v2 3/3] lib/amdpgu: fix the hard code when shedule ring Jesse.zhang@amd.com
2025-01-03  9:54 ` ✓ Xe.CI.BAT: success for series starting with [i-g-t,v2,1/3] lib/amdgpu: enhance wait memory helper Patchwork
2025-01-03 10:11 ` ✗ i915.CI.BAT: failure " Patchwork
2025-01-03 12:16 ` ✗ Xe.CI.Full: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250103081734.2338675-2-jesse.zhang@amd.com \
    --to=jesse.zhang@amd.com \
    --cc=alexander.deucher@amd.com \
    --cc=christian.koenig@amd.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=vitaly.prosyak@amd.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox