From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 073BF45C85; Tue, 5 Nov 2024 09:43:35 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id E19B14025D; Tue, 5 Nov 2024 09:43:34 +0100 (CET) Received: from zg8tmja5ljk3lje4ms43mwaa.icoremail.net (zg8tmja5ljk3lje4ms43mwaa.icoremail.net [209.97.181.73]) by mails.dpdk.org (Postfix) with ESMTP id BB669400EF for ; Tue, 5 Nov 2024 09:43:33 +0100 (CET) Received: from prodtpl.icoremail.net (unknown [10.12.1.20]) by hzbj-icmmx-7 (Coremail) with SMTP id AQAAfwBXTqmy2ilnWNyDAw--.40098S2; Tue, 05 Nov 2024 16:43:30 +0800 (CST) Received: from localhost.localdomain (unknown [218.76.62.144]) by mail (Coremail) with SMTP id AQAAfwBnvnqw2ilntm1HAA--.14828S3; Tue, 05 Nov 2024 16:43:28 +0800 (CST) From: liwencheng To: liwencheng@phytium.com.cn Cc: dev@dpdk.org Subject: [PATCH v2 3/3] usertools/dpdk-devbind: add platform device bind/unbind Date: Tue, 5 Nov 2024 08:43:21 +0000 Message-Id: <1730796201-1235443-1-git-send-email-liwencheng@phytium.com.cn> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1730455640-1084345-2-git-send-email-liwencheng@phytium.com.cn> References: <1730455640-1084345-2-git-send-email-liwencheng@phytium.com.cn> X-CM-TRANSID: AQAAfwBnvnqw2ilntm1HAA--.14828S3 X-CM-SenderInfo: polzv0xfkh0wo6sk53xlxphulrpou0/ Authentication-Results: hzbj-icmmx-7; spf=neutral smtp.mail=liwencheng @phytium.com.cn; X-Coremail-Antispam: 1Uk129KBjvJXoWxCF4UKFy3CF17Cw4kKw45Wrg_yoWrtw13pr 4rAaySkr17Kr9rKwnxZ3ZrCFWFkws2va45KrnrXw1j93s8ZrZ5trZ7tF1rZ3ZrJr1fZ3Z8 t3Z8WFWvka13ZF7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUj1kv1TuYvTs0mT0YCTnIWj DUYxn0WfASr-VFAU7a7-sFnT9fnUUIcSsGvfJ3UbIYCTnIWIevJa73UjIFyTuYvj4RJUUU UUUUU X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch adds functions for binding and unbinding platform devices, such as bind_platform_one and unbind_platform_one. Signed-off-by: liwencheng --- 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