DPDK patches and discussions
 help / color / mirror / Atom feed
From: liwencheng <liwencheng@phytium.com.cn>
To: liwencheng@phytium.com.cn
Cc: dev@dpdk.org
Subject: [PATCH v1 2/2] /usertools/dpdk-devbind:add the binding and unbinding of platform device
Date: Fri,  1 Nov 2024 10:07:20 +0000	[thread overview]
Message-ID: <1730455640-1084345-2-git-send-email-liwencheng@phytium.com.cn> (raw)
In-Reply-To: <1730455640-1084345-1-git-send-email-liwencheng@phytium.com.cn>

This patch mainly adds functions for binding and unbinding platform devices, such as bind_platform_one and unbind_platform_one.

Signed-off-by: liwencheng <liwencheng@phytium.com.cn>
---
 usertools/dpdk-devbind.py | 101 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 99 insertions(+), 2 deletions(-)

diff --git a/usertools/dpdk-devbind.py b/usertools/dpdk-devbind.py
index 80c35f9..4b65b55 100755
--- a/usertools/dpdk-devbind.py
+++ b/usertools/dpdk-devbind.py
@@ -147,10 +147,32 @@ def module_is_loaded(module):
     return module in loaded_modules
 
 
+def get_platform_devices():
+    global platform_devices
+
+    platform_device_path = "/sys/bus/platform/devices/"
+    platform_devices = os.listdir(platform_device_path)
+
+
+def devices_are_platform(devs):
+    all_devices_are_platform = True
+
+    get_platform_devices()
+    for d in devs:
+        if d not in platform_devices:
+            all_devices_are_platform = False
+            break
+
+    return all_devices_are_platform
+
+
 def check_modules():
     '''Checks that igb_uio is loaded'''
     global dpdk_drivers
 
+    if devices_are_platform(args):
+        return
+
     # list of supported modules
     mods = [{"Name": driver, "Found": False} for driver in dpdk_drivers]
 
@@ -321,11 +343,38 @@ def dev_id_from_dev_name(dev_name):
     for d in devices.keys():
         if dev_name in devices[d]["Interface"].split(","):
             return devices[d]["Slot"]
+
+    # Check if it is a platform device
+    if dev_name in platform_devices:
+        return dev_name
+
     # if nothing else matches - error
     raise ValueError("Unknown device: %s. "
                      "Please specify device in \"bus:slot.func\" format" % dev_name)
 
 
+def unbind_platform_one(dev_name):
+    filename = "/sys/bus/platform/devices/%s/driver" % dev_name
+
+    if exists(filename):
+        try:
+            f = open(os.path.join(filename, "unbind"), "w")
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot open %s: %s" %
+                     (dev_name, os.path.join(filename, "unbind"), err))
+        f.write(dev_name)
+        f.close()
+        filename = "/sys/bus/platform/devices/%s/driver_override" % dev_name
+        try:
+            f = open(filename, "w")
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot open %s: %s" %
+                     (dev_name, filename, err))
+        f.write("")
+        f.close()
+        print("Successfully unbind platform device %s" % dev_name)
+
+
 def unbind_one(dev_id, force):
     '''Unbind the device identified by "dev_id" from its current driver'''
     dev = devices[dev_id]
@@ -351,6 +400,48 @@ def unbind_one(dev_id, force):
     f.close()
 
 
+def bind_platform_one(dev_name, driver):
+    filename = "/sys/bus/platform/drivers/%s" % driver
+
+    if not exists(filename):
+        print("The driver %s is not loaded" % driver)
+        return
+    # unbind any existing drivers we don't want
+    filename = "/sys/bus/platform/devices/%s/driver" % dev_name
+    if exists(filename):
+        unbind_platform_one(dev_name)
+    # driver_override can be used to specify the driver
+    filename = "/sys/bus/platform/devices/%s/driver_override" % dev_name
+    if exists(filename):
+        try:
+            f = open(filename, "w")
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot open %s: %s"
+                     % (dev_name, filename, err))
+        try:
+            f.write(driver)
+            f.close()
+        except OSError as err:
+            sys.exit("Error: unbind failed for %s - Cannot write %s: %s"
+                     % (dev_name, filename, err))
+    # do the bind by writing to /sys
+    filename = "/sys/bus/platform/drivers/%s/bind" % driver
+    try:
+        f = open(filename, "w")
+    except OSError as err:
+        print("Error: bind failed for %s - Cannot open %s: %s"
+              % (dev_name, filename, err), file=sys.stderr)
+        return
+    try:
+        f.write(dev_name)
+        f.close()
+    except OSError as err:
+        print("Error: bind failed for %s - Cannot bind to driver %s: %s"
+              % (dev_name, driver, err), file=sys.stderr)
+        return
+    print("Successfully bind platform device %s to driver %s" % (dev_name, driver))
+
+
 def bind_one(dev_id, driver, force):
     '''Bind the device given by "dev_id" to the driver "driver". If the device
     is already bound to a different driver, it will be unbound first'''
@@ -475,7 +566,10 @@ def unbind_all(dev_list, force=False):
         sys.exit(1)
 
     for d in dev_list:
-        unbind_one(d, force)
+        if d in platform_devices:
+            unbind_platform_one(d)
+        else:
+            unbind_one(d, force)
 
 
 def has_iommu():
@@ -537,7 +631,10 @@ def bind_all(dev_list, driver, force=False):
         check_noiommu_mode()
 
     for d in dev_list:
-        bind_one(d, driver, force)
+        if d in platform_devices:
+            bind_platform_one(d, driver)
+        else:
+            bind_one(d, driver, force)
 
     # For kernels < 3.15 when binding devices to a generic driver
     # (i.e. one that doesn't have a PCI ID table) using new_id, some devices
-- 
2.7.4


      reply	other threads:[~2024-11-01 10:07 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-01 10:07 [PATCH v1 1/2] net/macb: add new driver liwencheng
2024-11-01 10:07 ` liwencheng [this message]

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=1730455640-1084345-2-git-send-email-liwencheng@phytium.com.cn \
    --to=liwencheng@phytium.com.cn \
    --cc=dev@dpdk.org \
    /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;
as well as URLs for NNTP newsgroup(s).