From nobody Sun May 19 04:55:59 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B988B19BBA; Tue, 23 Apr 2024 14:57:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713884240; cv=none; b=GsH/7LCMlAHavcSSe5ZzRDa/GEn4DTrCW6nopuH34dAHasASqxQOtr4MxCacfK586TsaaOcX+aM1XCC7Dir5L/22f6XzINZDFppn7NpTNLMqbUHh/bxbLtiU5icWfEIxWWo4V6hinzgl7SofvGpQdv7EXPmXI93T+UxGQ0SxO+k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713884240; c=relaxed/simple; bh=9Z6TIVtIHsBGYS50SZqMPeE1FnH5U/FD9sZNT2L8PVE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HmNYNhLFbEgEUI4kakpVqgnrvew173AUlI0CA8l8NSJLXpH7zIxafkd+iYS/ZheoQlV8cHzsNdypQ8+lyVqOjZg01j/4joeu8xO7hbLAZ9OmkUzmFqP5QNKaA45m2XadwAtMuKs6ciebT0aCOpJ2Xs5OPWMum4eDS88Qzl4XRes= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=ZaESqS0d; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="ZaESqS0d" Received: by mail.gandi.net (Postfix) with ESMTPA id 09C4D2000D; Tue, 23 Apr 2024 14:57:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1713884236; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hGsiCOFN4nE0bxasvx4D4akJDLOLdXoX82elYUQl1HU=; b=ZaESqS0dsjp6EwjLf378LtSFa3TE4kfgV8Fs504anjVVXifQYL5rCQ1lvO9j+XILzYWjCM QNtOSZ+L3IHpKFgHLEyRBnkEAMk55ndleX816MIlg2km5g4yy84+jU2dVH1VONB+9zS0LL kmbwbh/EWE9xw6HiiJQk0LNAardpsDY/dl78Rnj4XzqHABw+wQ0/4zf5dnHw+9sPq3ouTj j893ZSegzz1hbFGiqGdnGjp3Jc14sCB4c6+0djNdkXy/1UA8iTZBp+MySYuSOGbzMi3OB8 h+XgOXxYAblRyVBvI+Zgcecsu/iZwK6loypiGbFYhlUd7QdqeT59lDUBOzsOhQ== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Bjorn Helgaas , Lizhi Hou , Rob Herring Cc: Max Zhen , Sonal Santan , Stefano Stabellini , Jonathan Cameron , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH v4 1/2] driver core: Introduce device_{add,remove}_of_node() Date: Tue, 23 Apr 2024 16:56:58 +0200 Message-ID: <20240423145703.604489-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240423145703.604489-1-herve.codina@bootlin.com> References: <20240423145703.604489-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: herve.codina@bootlin.com Content-Type: text/plain; charset="utf-8" An of_node can be set to a device using device_set_node(). This function cannot prevent any of_node and/or fwnode overwrites. When adding an of_node on an already present device, the following operations need to be done: - Attach the of_node if no of_node were already attached - Attach the of_node as a fwnode if no fwnode were already attached This is the purpose of device_add_of_node(). device_remove_of_node() reverts the operations done by device_add_of_node(). Cc: stable@vger.kernel.org Signed-off-by: Herve Codina --- drivers/base/core.c | 52 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 ++ 2 files changed, 54 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 521757408fc0..098ae335cb0a 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5127,6 +5127,58 @@ void set_secondary_fwnode(struct device *dev, struct= fwnode_handle *fwnode) } EXPORT_SYMBOL_GPL(set_secondary_fwnode); =20 +/** + * device_remove_of_node - Remove an of_node from a device + * @dev: device whose device-tree node is being removed + */ +void device_remove_of_node(struct device *dev) +{ + dev =3D get_device(dev); + if (!dev) + return; + + if (!dev->of_node) + goto end; + + if (dev->fwnode =3D=3D of_fwnode_handle(dev->of_node)) + dev->fwnode =3D NULL; + + of_node_put(dev->of_node); + dev->of_node =3D NULL; + +end: + put_device(dev); +} +EXPORT_SYMBOL_GPL(device_remove_of_node); + +/** + * device_add_of_node - Add an of_node to an existing device + * @dev: device whose device-tree node is being added + * @of_node: of_node to add + */ +void device_add_of_node(struct device *dev, struct device_node *of_node) +{ + if (!of_node) + return; + + dev =3D get_device(dev); + if (!dev) + return; + + if (WARN(dev->of_node, "%s: Cannot replace node %pOF with %pOF\n", + dev_name(dev), dev->of_node, of_node)) + goto end; + + dev->of_node =3D of_node_get(of_node); + + if (!dev->fwnode) + dev->fwnode =3D of_fwnode_handle(of_node); + +end: + put_device(dev); +} +EXPORT_SYMBOL_GPL(device_add_of_node); + /** * device_set_of_node_from_dev - reuse device-tree node of another device * @dev: device whose device-tree node is being set diff --git a/include/linux/device.h b/include/linux/device.h index 97c4b046c09d..1795121dee9a 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1127,6 +1127,8 @@ int device_offline(struct device *dev); int device_online(struct device *dev); void set_primary_fwnode(struct device *dev, struct fwnode_handle *fwnode); void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode= ); +void device_add_of_node(struct device *dev, struct device_node *of_node); +void device_remove_of_node(struct device *dev); void device_set_of_node_from_dev(struct device *dev, const struct device *= dev2); void device_set_node(struct device *dev, struct fwnode_handle *fwnode); =20 --=20 2.44.0 From nobody Sun May 19 04:55:59 2024 Received: from relay7-d.mail.gandi.net (relay7-d.mail.gandi.net [217.70.183.200]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C63A713B7AB; Tue, 23 Apr 2024 14:57:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.200 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713884242; cv=none; b=iVIEVTYBNpG0u2OCuF5UP9IybSgtEgHM+/QCaSXByLgCaYavH6GYZE1lldlUpF1VVsokJB73Jy8faFIYlqAJ5EQ38JJiI02YBz9KNJ8jkmxdGu4PaSML6C2IEQzcW7fPNtNvPR/9T+9NfFYpMSeljbob5VyiZYmwt8mcCgx7ie8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713884242; c=relaxed/simple; bh=6Sg7pfkwoCdHyxPYWfO/0nEXEYlUdqzDZq+0lCLAjXs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O/sSr13mOT6VvBNgIKDB4BnfCyFYfqHa+6CeP84lFY3J67XgMnKYfcymrXBqJgL5gv6iMuJZsUmw/OqFIgMWQs4ZOtQUl0lsVPeZKXKwBcFeaqKiKs/XA8yztmId4frMI7xGVRJkopbHI08ynNJolyL/Pa37J5FRFdcpw/xkaZU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=MeezJZrF; arc=none smtp.client-ip=217.70.183.200 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="MeezJZrF" Received: by mail.gandi.net (Postfix) with ESMTPA id 045542000F; Tue, 23 Apr 2024 14:57:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1713884237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=DJkxtHqhgZHsEW6tFHhsuGDRyY1D69IDz218tw2fS/g=; b=MeezJZrFFkR3jPaPfy3EAOhU/EaTtkTNzT6dlO6ziw0NB/w+j2zvYx3B6HKfGWQIA2jX3L kM0xRVdCv/L3FYNkwuupfif6z8wY6B5RAacA2kLL6pU4OC2AbSXmvb/CjO+xeH1H1wc/4d 6PIc7zMMFnmQdznnU+isuuzSzlDcuNU6gC9/8p6A3zba+A7OqA0GGmNRdQDDbWbH1Xvkyc 7WcyBvlX4RCYKkjK7x991gFa6T4+vvoyBFPSpV1dExU9at8etWLvhvDVLteQAK9Pbl25sN dfTL94v2X92SbClMEM3OPuTWp4Ci7MpmJhjPtz2sUWy/h1O5CG+C/fQJDM54CA== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Bjorn Helgaas , Lizhi Hou , Rob Herring Cc: Max Zhen , Sonal Santan , Stefano Stabellini , Jonathan Cameron , =?UTF-8?q?Krzysztof=20Wilczy=C5=84ski?= , linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH v4 2/2] PCI: of: Attach created of_node to existing device Date: Tue, 23 Apr 2024 16:56:59 +0200 Message-ID: <20240423145703.604489-3-herve.codina@bootlin.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240423145703.604489-1-herve.codina@bootlin.com> References: <20240423145703.604489-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-GND-Sasl: herve.codina@bootlin.com Content-Type: text/plain; charset="utf-8" The commit 407d1a51921e ("PCI: Create device tree node for bridge") creates of_node for PCI devices. During the insertion handling of these new DT nodes done by of_platform, new devices (struct device) are created. For each PCI devices a struct device is already present (created and handled by the PCI core). Having a second struct device to represent the exact same PCI device is not correct. On the of_node creation: - tell the of_platform that there is no need to create a device for this node (OF_POPULATED flag), - link this newly created of_node to the already present device, - tell fwnode that the device attached to this of_node is ready using fwnode_dev_initialized(). On the of_node removal, revert the operations. Fixes: 407d1a51921e ("PCI: Create device tree node for bridge") Cc: stable@vger.kernel.org Signed-off-by: Herve Codina --- drivers/pci/of.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/drivers/pci/of.c b/drivers/pci/of.c index 51e3dd0ea5ab..5afd2731e876 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -615,7 +615,8 @@ void of_pci_remove_node(struct pci_dev *pdev) np =3D pci_device_to_OF_node(pdev); if (!np || !of_node_check_flag(np, OF_DYNAMIC)) return; - pdev->dev.of_node =3D NULL; + + device_remove_of_node(&pdev->dev); =20 of_changeset_revert(np->data); of_changeset_destroy(np->data); @@ -668,12 +669,22 @@ void of_pci_make_dev_node(struct pci_dev *pdev) if (ret) goto out_free_node; =20 + /* + * This of_node will be added to an existing device. + * Avoid any device creation and use the existing device + */ + of_node_set_flag(np, OF_POPULATED); + np->fwnode.dev =3D &pdev->dev; + fwnode_dev_initialized(&np->fwnode, true); + ret =3D of_changeset_apply(cset); if (ret) goto out_free_node; =20 np->data =3D cset; - pdev->dev.of_node =3D np; + + /* Add the of_node to the existing device */ + device_add_of_node(&pdev->dev, np); kfree(name); =20 return; --=20 2.44.0