From nobody Sun Nov 24 14:27:55 2024 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 F1E401C5793; Mon, 4 Nov 2024 17:20:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740813; cv=none; b=qB1uaULEZv0dACNBAftN+Z3c0yfxyeVJf7J4P8IcAW2G4m55d+dqXzTTuVfvUTfENDoIhQXGtfuh/0SwC13J295cjkQ83qNOBoj7JmyD0t4sPFhFTidsSOVXEhndLHIfCvR5pqNAPEC8ZurW7JZ9IQTeI0CgSUY4cQD0+0tjnuM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740813; c=relaxed/simple; bh=hiS6n9pheg1VmpHKj++eyaK6UcsY7Peml+xl4mdMBQA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hHnFaL6GAaf/StoZJuZQPYQfBQwvgZ1uuBHYUrZX6ZlUbbE6aiX7a6Py98Ho6t/mjCf6foUzhQcQzW6cV6yMUxE9hjQSU4lXk/jXr+kWccbaeJ0o1JJu4/3ylIco8bnjrMJ8dHp30XAR975ZSmPhcGV3b5i+lmkmD5dtRft0iAA= 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=gwe7fO99; arc=none smtp.client-ip=217.70.183.201 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="gwe7fO99" Received: by mail.gandi.net (Postfix) with ESMTPA id 8FE281BF20B; Mon, 4 Nov 2024 17:20:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1730740809; 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=xXuggkRMR/uQkcFh5AavFOlwv1DgC3rb6WoS70TIrr8=; b=gwe7fO991rjyA85LZ0ZKf8j1ola4vp/LSTDZGWfeHhi7UE2VzKCUenKz/AMVBUh6bPXZAA yNP8Mq4MAiZrWA54Jx0VN7rklMJ2kgKj0/5QbTtfZMroO9SD5WVPETK6N1DIC0OmVEpy7h g433FByH+AIOPf0XUthCb6l2PAAMNuJBRh7J1ZAsfhwKQU6I/Z+ZwnPwlqOQKXBL1BUb+c xwlmj+jNuMTX09X/v5KnPX9WmnM1Q4rQxJqer0WzDF9fzRuRJAuJsk9cklSEG+tpdX0Xsm 5zDyLyX5gVjRpWeKtOlAel2TDLVpceM8wAMJsZmlpv7sh0rRvDMD3Nx0IUeM1Q== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Saravana Kannan , Bjorn Helgaas , Lizhi Hou Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH 1/6] driver core: Introduce device_{add,remove}_of_node() Date: Mon, 4 Nov 2024 18:19:55 +0100 Message-ID: <20241104172001.165640-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241104172001.165640-1-herve.codina@bootlin.com> References: <20241104172001.165640-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 24c572031403..0aa63371f55d 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5118,6 +5118,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 b4bde8d22697..e3aa25ce1f90 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1146,6 +1146,8 @@ 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_set_node(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); =20 static inline struct device_node *dev_of_node(struct device *dev) --=20 2.46.2 From nobody Sun Nov 24 14:27:55 2024 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 ADEEE1B5ED0; Mon, 4 Nov 2024 17:20:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740814; cv=none; b=AwMJvHMnPwmwadzNq3a5EoYEBBRSkEi1ki0WcwyikHiyrDkoOmAxFeHS+cpyy+WzhhjQQ7Li+U80EuW0Z4imHYXQ9ns6mJu4WF3194oe2rXeGYJUreT1X3dNVPKzhghlDJLpNdy5moAbsa5VEQvPOU1dSMAV1n2rrpaydiDn0FE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740814; c=relaxed/simple; bh=2bOktKmAPEkqsKGZ1qCsKyyYcjkM1ngNPOsvVPIF9n4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QIjQE2S8giV7WeSwESkXcdcUmpdy6xiIsxALXx2eWsj4TONbSeppWVdIMiOASXR6n/Nz3M+eFDb40ZWyROxrFETpFCM+lqnV0BDSF1QHG+/1u+JjkXhsrBOrGuiitKVnXY3iGd92jzDX/omRh/PP/Z2Eh10LnxT/lU/3wD9j1Ng= 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=ehKBhHGg; arc=none smtp.client-ip=217.70.183.201 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="ehKBhHGg" Received: by mail.gandi.net (Postfix) with ESMTPA id 5C53D1BF20A; Mon, 4 Nov 2024 17:20:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1730740810; 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=TampoxF0nOUV9sc0duplKSebun+rgGbOYYjIN6TWt9s=; b=ehKBhHGgUKFq3vt6C4/iO/53Gx8bo3UUhFOZ/2c6SomNmHdbl+noVYLCd7pxyE6tOdznEV frWzFwjWP71PYG09TN56wKqEB7IPan/7r/Tas6cj2ADkkNNj8SBqnOxWVfbPNv7/gmWD3/ 8Od4JtVUutFZsEgKNPH0XOqU8quJDx3SZP75MMrzaq6n1TDy7ciG2Mbpbwv5Bljlz2Zxor JB/A01pUiTL8jDENVhJeQpGjXoy6Y18LbZHQTN5r+00dk9xkGJW6IOt/xJsRTdMo1ZWseM SwSNbK57hJMDgKNKjo2k0nc3/A0FrN7t2XYfoyGlYFwvRqr5bw4x2z5uW/CcGg== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Saravana Kannan , Bjorn Helgaas , Lizhi Hou Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina , stable@vger.kernel.org Subject: [PATCH 2/6] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device Date: Mon, 4 Nov 2024 18:19:56 +0100 Message-ID: <20241104172001.165640-3-herve.codina@bootlin.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241104172001.165640-1-herve.codina@bootlin.com> References: <20241104172001.165640-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. The newly created of_node is attached to an existing device. This is done setting directly pdev->dev.of_node in the code. Even if pdev->dev.of_node cannot be previously set, this doesn't handle the fwnode field of the struct device. Indeed, this field needs to be set if it hasn't already been set. device_{add,remove}_of_node() have been introduced to handle this case. Use them instead of the direct setting. Fixes: 407d1a51921e ("PCI: Create device tree node for bridge") Cc: stable@vger.kernel.org Signed-off-by: Herve Codina --- drivers/pci/of.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/pci/of.c b/drivers/pci/of.c index dacea3fc5128..141ffbb1b3e6 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -655,8 +655,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; =20 + device_remove_of_node(&pdev->dev); of_changeset_revert(np->data); of_changeset_destroy(np->data); of_node_put(np); @@ -713,7 +713,7 @@ void of_pci_make_dev_node(struct pci_dev *pdev) goto out_free_node; =20 np->data =3D cset; - pdev->dev.of_node =3D np; + device_add_of_node(&pdev->dev, np); kfree(name); =20 return; --=20 2.46.2 From nobody Sun Nov 24 14:27:55 2024 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 8BF941C3022; Mon, 4 Nov 2024 17:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740814; cv=none; b=rsv9nub3IGyErC6ruwXqEZa2RGSqcOPzymbyKEp7jUjnLkQYM9ESxjdbVXKc4/HHD/WUkUDXTOWyXON846VzaQTpxikGXSH7qDZaqp/x/T1EfOyGGtfBC9q7Jqt0r2/g3QH2mmXiynGrqXRDsU5Tx+mTnaDAVZkTZ2OlOoQnXVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740814; c=relaxed/simple; bh=KbQcH2DUBO4XBFA/lTskG3ADO76dFxPNoXmfqDECx2E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ieRt6JPJEhUgZjrPPI42GQL6+a+yhjmKJlNHSpHz1sfIjWxh/WGTkWwCnxeKR/UEGPNAu3UfoBEGCNp/aKc9UE+tnl344CMqMuQQrNyxCWFyDMUgBMaHAwVdQmIQTOsaHY+UeVW2dYHfLm3/I8XFGd7/XD3sqKmDhkBFOnQ8wZ8= 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=YFHH0zB+; arc=none smtp.client-ip=217.70.183.201 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="YFHH0zB+" Received: by mail.gandi.net (Postfix) with ESMTPA id 2DF521BF20D; Mon, 4 Nov 2024 17:20:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1730740810; 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=OWUWbgIcwwkYpx2sGFB36arAHxZpqg+uUR07E7hW3jI=; b=YFHH0zB+9neL9D2bKrNNSRX1QuwwiP7zTHk3eKzEEYuJbJGgwDuZrIumR3vEWyk1jW/YpX RLBLT6JeQMiWijyaAsx7PssSbT9Yxv0pIcCGL0hpruoyW+8slKDiiV5ul4BFoXx4ap/GUH s79XcApRhqPQ7fg2oAy2/85oRJr8SCvmv6cpiRSAxwgX1EupQZBQUxBSSyy0cgRAxAPQBX VH/2NtC9GxfezhZdrjDtDaHemWEuhQrMQka3XZ9eHPglGBetK2Wr0LUvMUFA7Swl/2dtH/ CVm+QSQn8M86efcmRN51O31fiblxBZlDgRn0bSMNwdYCw6iLkEBtjO6xbl8aaA== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Saravana Kannan , Bjorn Helgaas , Lizhi Hou Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina Subject: [PATCH 3/6] PCI: of_property: Add support for NULL pdev in of_pci_set_address() Date: Mon, 4 Nov 2024 18:19:57 +0100 Message-ID: <20241104172001.165640-4-herve.codina@bootlin.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241104172001.165640-1-herve.codina@bootlin.com> References: <20241104172001.165640-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 pdev (pointer to a struct pci_dev) parameter of of_pci_set_address() cannot be NULL. In order to reuse of_pci_set_address() when creating the PCI root bus node, this function needs to support a NULL pdev parameter. Indeed, in the case of the PCI root bus node creation, no pdev are available and of_pci_set_address() will be used with the bridge windows. Allow to call of_pci_set_address() with a NULL pdev. Signed-off-by: Herve Codina --- drivers/pci/of_property.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index 5a0b98e69795..59cc5c851fc3 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -54,9 +54,13 @@ enum of_pci_prop_compatible { static void of_pci_set_address(struct pci_dev *pdev, u32 *prop, u64 addr, u32 reg_num, u32 flags, bool reloc) { - prop[0] =3D FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) | - FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) | - FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn)); + if (pdev) + prop[0] =3D FIELD_PREP(OF_PCI_ADDR_FIELD_BUS, pdev->bus->number) | + FIELD_PREP(OF_PCI_ADDR_FIELD_DEV, PCI_SLOT(pdev->devfn)) | + FIELD_PREP(OF_PCI_ADDR_FIELD_FUNC, PCI_FUNC(pdev->devfn)); + else + prop[0] =3D 0; + prop[0] |=3D flags | reg_num; if (!reloc) { prop[0] |=3D OF_PCI_ADDR_FIELD_NONRELOC; --=20 2.46.2 From nobody Sun Nov 24 14:27:55 2024 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 407531C8787; Mon, 4 Nov 2024 17:20:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740815; cv=none; b=UEChm9b8gUF+pH72j9mmadlb9fLphMH3wpIIq2FRnnGadrz5Nmg6pZfqjVkLgTzfAFTmXqck8pGhYJ+Y3trSHG5ElydYe9uyIF+EINdOcrF0IabeiltunvN+P4f2HHTlUw84mYeQCaH2+q++8eBjQmeLIoKnyzv790u3eA9KP/s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740815; c=relaxed/simple; bh=fYwE5EjMh19lhQDnSaq4yYULUe6QFJR/LjTL/pR+GRg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sqBITnYjL9Gqtyhi62jrRvbKRn8CyVWlewBc/Y1tBB/Op0EpdWIMA1W95TDBU9JTnE01eQApTr0tCC34g3JPm/kjyKF4y1/SMmZJSciQjxHWPYrYRvuJ9CPmarO6AnqGHVAFl7NQBqOW2PB/3fR8VIOfNaOBY9oTcKiFyBupabs= 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=MiAzBprF; arc=none smtp.client-ip=217.70.183.201 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="MiAzBprF" Received: by mail.gandi.net (Postfix) with ESMTPA id D990C1BF20F; Mon, 4 Nov 2024 17:20:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1730740811; 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=GxzxY5tO2wGGtnAYK1ZdH8OX9CY4/fDjyHiH50aTJak=; b=MiAzBprFjt0Jff5Wi7WHw8BQA+IWWG5WQZCClSV8WnU6BWNDZ1GmjvdMYnf3U8M/KLp7Ug o/T3GEpQc3gHHAJG/g+j7ms1RaT98lF+odoIE5lvLY2CDWjEL8PYPQrOz1iGkBohRKmL6Z 4j6ZWRf67rBtNH6YmYlR2gAcSVg1vqeBYhanKvSpolKs+nKf0BmR9kV+tM9rClfgrhsebg zwuba+xbDLKMgcpS+jLDEFLPj2OhsnqS1Gv35doH1VxjOY0S5UWNVQya/twiEvO+lQ49GT HqQwoEkqiVYW+gSP2LuKeCdxdUWK4YYg70J+Zd+pmsGPkSKS8ZR56DhHKnBnog== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Saravana Kannan , Bjorn Helgaas , Lizhi Hou Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina Subject: [PATCH 4/6] PCI: of_property: Constify parameter in of_pci_get_addr_flags() Date: Mon, 4 Nov 2024 18:19:58 +0100 Message-ID: <20241104172001.165640-5-herve.codina@bootlin.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241104172001.165640-1-herve.codina@bootlin.com> References: <20241104172001.165640-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 res parameter has no reason to be a pointer to an un-const struct resource. Indeed, struct resource is not supposed to be modified by the function. Constify the res parameter. Signed-off-by: Herve Codina --- drivers/pci/of_property.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index 59cc5c851fc3..e56159cc48e8 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -69,7 +69,7 @@ static void of_pci_set_address(struct pci_dev *pdev, u32 = *prop, u64 addr, } } =20 -static int of_pci_get_addr_flags(struct resource *res, u32 *flags) +static int of_pci_get_addr_flags(const struct resource *res, u32 *flags) { u32 ss; =20 --=20 2.46.2 From nobody Sun Nov 24 14:27:55 2024 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 CA6D91C9ED1; Mon, 4 Nov 2024 17:20:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740815; cv=none; b=dTPbnZ2SBs9Ubz3J6wfvvrO59eGlEYPz3L/WRVd4jnqh/V31jzaBJawILyOnJp/KS2vbAj0bXJm4jQz31mm9He91TXTJok2xXDBwYIK4CsYGK9lTUPUSKn3HB6J1zrlIAm1HxPmWa6xRmD/tJk98c7brmzmUo2tcP6wcTXcakuE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740815; c=relaxed/simple; bh=PCVCnfxn3uvwj+ZCYt1qF5vPcAytJQEgBonodYhLbUI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Barc+sjMqa10OsRwXPToioaMXgbwgQDOlxBgUiIatj3iJWtCrnN/HLFART1GeHrM/XtTvZ9ZRzsGXjiC7frZXjeX7rH1fNQtJ4+bxRx1rqi/1TUbc/5lMYLduBIvCvQIql8CoPi4xkxtkOvXHTIu/mjclVkkUHwqC5Tn2r1WPu8= 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=lfPu+9Qv; arc=none smtp.client-ip=217.70.183.201 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="lfPu+9Qv" Received: by mail.gandi.net (Postfix) with ESMTPA id 9BD2B1BF212; Mon, 4 Nov 2024 17:20:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1730740812; 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=oACsH740MGISvwOaAMqyJDhEnvLSJA2AqYvobBxaexc=; b=lfPu+9Qvt4oDo0U1topeTIBJjp+29fgu/1qA/x8rNDAmNpgPgIbZ3pK15e1Jb6aSAcXOXl K5s1KhEZflsD8hTK0bciHagWhft6QNY3XX60IidTO48IQz1Dr3LoACBvJUMnTDcTClf5/T v1bR93Yme2BD5xq9rCJNGxLjLckwKQ3Xv0DLMXcZ8YLrG31Ts4THxpASEgMxCuTadKqnIz fwj6fm+jXYgW17a8X4k3+B2MN2WHCqZjuBEupS5FWbXAtWBWF9gDHmdMcINCuMKggboAm5 xed9teY7g0rVIYicBisDzOziSw87/b+dwk7ZVDYhUi7CS5/x6bmNJ2kq5WAhkQ== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Saravana Kannan , Bjorn Helgaas , Lizhi Hou Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina Subject: [PATCH 5/6] of: Use the standards compliant default address cells value for x86 Date: Mon, 4 Nov 2024 18:19:59 +0100 Message-ID: <20241104172001.165640-6-herve.codina@bootlin.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241104172001.165640-1-herve.codina@bootlin.com> References: <20241104172001.165640-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 default address cells value is 1. According to the devicetree specification and the OpenFirmware standard (IEEE 1275-1994) this default value should be 2. The device tree compiler already use 2 as default value and the powerpc PROM code also use 2 as default value. Modern implementation should have the #address-cells property set and should not rely on this default value. On x86, an empty root device-tree node is created but as the hardware is not described by a device-tree passed by the bootloader, this root device-tree remains empty and so the #address-cells default value is used. In preparation of the support for device-tree overlay on PCI devices feature on x86 (i.e. the creation of the PCI root bus device-tree node), this default value needs to be updated. Indeed, on x86_64, addresses are on 64bits and the upper part of an address is needed for correct address translations. On x86_32 having the default value updated does not lead to issues while the uppert part of a 64bits address is zero. Changing the default value for all architectures may break device-tree compatibility. Indeed, existing dts file without the #address-cells property set in the root node will not be compatible with this modification. Restrict the modification to the x86 architecture. Instead of having 1 for this default value, be consistent with standards and set the default address cells value to 2 in case of x86. Signed-off-by: Herve Codina --- drivers/of/of_private.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/of/of_private.h b/drivers/of/of_private.h index 04aa2a91f851..d8353f04af0a 100644 --- a/drivers/of/of_private.h +++ b/drivers/of/of_private.h @@ -29,7 +29,7 @@ struct alias_prop { char stem[]; }; =20 -#if defined(CONFIG_SPARC) +#if defined(CONFIG_SPARC) || defined(CONFIG_X86) #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 2 #else #define OF_ROOT_NODE_ADDR_CELLS_DEFAULT 1 --=20 2.46.2 From nobody Sun Nov 24 14:27:55 2024 Received: from relay8-d.mail.gandi.net (relay8-d.mail.gandi.net [217.70.183.201]) (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 8A2D31CACFD; Mon, 4 Nov 2024 17:20:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740817; cv=none; b=IvrUMAO8O7qrD7NoaMj1nmZhLWDccaIQKqGFofod6akbqiW0Laa1WFCsnY+KlaYcpCC4tFoR3C5HlhjfCsMLAWLWPLHlnrzdbHxYOfENaUXYIDq1cDwlL5XSBlnM/ipIQjsmwWBMmVUjbgCOYuEu0NiJcWj6GhwGsZphUJzVzHs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730740817; c=relaxed/simple; bh=NwMSCaI0b8RGS3jn4oHDmv6MAzYS4vPG0txsV1EJP0k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=F7OnkHYKRjnVVAX37NR+4pwBma3hk3nV4+kI8hd+ulQwPlwWIdigATLCwREQmXaV7x7T7V7+8eSDYllCNcfz4SYT0u8DvnjbrgBQHz8ZOrNstG1SIQV/x35sNI+SP1sPpTdV+wMpnAHqlf+x803dp7m02CpN5poy9QEp8XpOgOA= 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=Xia/Y4cV; arc=none smtp.client-ip=217.70.183.201 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="Xia/Y4cV" Received: by mail.gandi.net (Postfix) with ESMTPA id 5DA2A1BF203; Mon, 4 Nov 2024 17:20:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1730740813; 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=DoVk53M0b7TJweV9F3/GrYxUnWVI2icOOAP6maodiQs=; b=Xia/Y4cV7NLKY2ys9yYBiAIkmGNoNlbfZrPkGqxueCmeRM1q6O4edlLLTMnc+/mt04kxzc 3DLnzhv8q6o/G+Vc5iREjobFBPdMkS+Fq2LQExo4krrh1QeJoXLTZhEk0v53MYHk22s5MZ xD1VXL8wmASpEf0UWTFxuwUK6lQCDIyn4k+86gRTIbTnnqKKZCjFPZz08rJU+bUUtdTveD QGZxXfhDQV750YyItpEj6BkGV9iiEG2nJ9OLoYhGIXKoQQu7CeJDlHbbKJDJus+QCDIf3m b0YNRTT6CJ/luecHv9bmdRx7xAgR5cKyOjZOgcPh58S38ALjIR6SJ1In7p5JuA== From: Herve Codina To: Greg Kroah-Hartman , "Rafael J. Wysocki" , Rob Herring , Saravana Kannan , Bjorn Helgaas , Lizhi Hou Cc: linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-pci@vger.kernel.org, Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Herve Codina Subject: [PATCH 6/6] PCI: of: Create device-tree root bus node Date: Mon, 4 Nov 2024 18:20:00 +0100 Message-ID: <20241104172001.165640-7-herve.codina@bootlin.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241104172001.165640-1-herve.codina@bootlin.com> References: <20241104172001.165640-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" PCI devices device-tree nodes can be already created. This was introduced by commit 407d1a51921e ("PCI: Create device tree node for bridge"). In order to have device-tree nodes related to PCI devices attached on their PCI root bus, a root bus device-tree node is needed. This root bus node will be used as the parent node of the first level devices scanned on the bus. On non device-tree based system (such as ACPI), a device-tree node for the PCI root bus does not exist. Indeed, this component is not described in a device-tree used at boot. The device-tree PCI root bus node creation needs to be done at runtime. This is done in the same way as for the creation of the PCI device nodes. I.e. node and properties are created based on computed information done by the PCI core. Signed-off-by: Herve Codina --- drivers/pci/of.c | 85 +++++++++++++++++++++++++++++++- drivers/pci/of_property.c | 101 ++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 6 +++ drivers/pci/probe.c | 2 + drivers/pci/remove.c | 2 + 5 files changed, 195 insertions(+), 1 deletion(-) diff --git a/drivers/pci/of.c b/drivers/pci/of.c index 141ffbb1b3e6..46733a293c3f 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -726,7 +726,90 @@ void of_pci_make_dev_node(struct pci_dev *pdev) out_free_name: kfree(name); } -#endif + +void of_pci_remove_root_bus_node(struct pci_bus *bus) +{ + struct device_node *np; + + np =3D pci_bus_to_OF_node(bus); + if (!np || !of_node_check_flag(np, OF_DYNAMIC)) + return; + + device_remove_of_node(&bus->dev); + of_changeset_revert(np->data); + of_changeset_destroy(np->data); + of_node_put(np); +} + +void of_pci_make_root_bus_node(struct pci_bus *bus) +{ + struct device_node *np =3D NULL; + struct of_changeset *cset; + const char *name; + int ret; + + /* + * If there is already a device tree node linked to this device, + * return immediately. + */ + if (pci_bus_to_OF_node(bus)) + return; + + /* Check if there is a DT root node to attach this created node */ + if (!of_root) { + pr_err("of_root node is NULL, cannot create PCI root bus node"); + return; + } + + name =3D kasprintf(GFP_KERNEL, "pci-root@%x,%x", pci_domain_nr(bus), + bus->number); + if (!name) + return; + + cset =3D kmalloc(sizeof(*cset), GFP_KERNEL); + if (!cset) + goto out_free_name; + of_changeset_init(cset); + + np =3D of_changeset_create_node(cset, of_root, name); + if (!np) + goto out_destroy_cset; + + ret =3D of_pci_add_root_bus_properties(bus, cset, np); + if (ret) + goto out_free_node; + + /* + * This of_node will be added to an existing device. The of_node parent + * is the root OF node and so this node will be handled by the platform + * bus. Avoid any new device creation. + */ + of_node_set_flag(np, OF_POPULATED); + np->fwnode.dev =3D &bus->dev; + fwnode_dev_initialized(&np->fwnode, true); + + ret =3D of_changeset_apply(cset); + if (ret) + goto out_free_node; + + np->data =3D cset; + + /* Add the of_node to the existing device */ + device_add_of_node(&bus->dev, np); + kfree(name); + + return; + +out_free_node: + of_node_put(np); +out_destroy_cset: + of_changeset_destroy(cset); + kfree(cset); +out_free_name: + kfree(name); +} + +#endif /* CONFIG_PCI_DYNAMIC_OF_NODES */ =20 #endif /* CONFIG_PCI */ =20 diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index e56159cc48e8..527fc51565f3 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -394,3 +394,104 @@ int of_pci_add_properties(struct pci_dev *pdev, struc= t of_changeset *ocs, =20 return 0; } + +static bool of_pci_is_range_resource(const struct resource *res, u32 *flag= s) +{ + if (!(resource_type(res) & IORESOURCE_MEM) && + !(resource_type(res) & IORESOURCE_MEM_64)) + return false; + + if (of_pci_get_addr_flags(res, flags)) + return false; + + return true; +} + +static int of_pci_root_bus_prop_ranges(struct pci_bus *bus, + struct of_changeset *ocs, + struct device_node *np) +{ + struct pci_host_bridge *bridge =3D to_pci_host_bridge(bus->bridge); + struct resource_entry *window; + unsigned int ranges_sz =3D 0; + unsigned int n_range =3D 0; + struct resource *res; + int n_addr_cells; + u32 *ranges; + u64 val64; + u32 flags; + int ret; + + n_addr_cells =3D of_n_addr_cells(np); + if (n_addr_cells <=3D 0 || n_addr_cells > 2) + return -EINVAL; + + resource_list_for_each_entry(window, &bridge->windows) { + res =3D window->res; + if (!of_pci_is_range_resource(res, &flags)) + continue; + n_range++; + } + + if (!n_range) + return 0; + + ranges =3D kcalloc(n_range, + (OF_PCI_ADDRESS_CELLS + OF_PCI_SIZE_CELLS + + n_addr_cells) * sizeof(*ranges), + GFP_KERNEL); + if (!ranges) + return -ENOMEM; + + resource_list_for_each_entry(window, &bridge->windows) { + res =3D window->res; + if (!of_pci_is_range_resource(res, &flags)) + continue; + + /* PCI bus address */ + val64 =3D res->start; + of_pci_set_address(NULL, &ranges[ranges_sz], val64, 0, flags, false); + ranges_sz +=3D OF_PCI_ADDRESS_CELLS; + + /* Host bus address */ + if (n_addr_cells =3D=3D 2) + ranges[ranges_sz++] =3D upper_32_bits(val64); + ranges[ranges_sz++] =3D lower_32_bits(val64); + + /* Size */ + val64 =3D resource_size(res); + ranges[ranges_sz] =3D upper_32_bits(val64); + ranges[ranges_sz + 1] =3D lower_32_bits(val64); + ranges_sz +=3D OF_PCI_SIZE_CELLS; + } + + ret =3D of_changeset_add_prop_u32_array(ocs, np, "ranges", ranges, ranges= _sz); + kfree(ranges); + return ret; +} + +int of_pci_add_root_bus_properties(struct pci_bus *bus, struct of_changese= t *ocs, + struct device_node *np) +{ + int ret; + + ret =3D of_changeset_add_prop_string(ocs, np, "device_type", "pci"); + if (ret) + return ret; + + ret =3D of_changeset_add_prop_u32(ocs, np, "#address-cells", + OF_PCI_ADDRESS_CELLS); + if (ret) + return ret; + + ret =3D of_changeset_add_prop_u32(ocs, np, "#size-cells", + OF_PCI_SIZE_CELLS); + if (ret) + return ret; + + ret =3D of_pci_root_bus_prop_ranges(bus, ocs, np); + if (ret) + return ret; + + return 0; +} diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 14d00ce45bfa..56e450807d5d 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -802,9 +802,15 @@ void of_pci_make_dev_node(struct pci_dev *pdev); void of_pci_remove_node(struct pci_dev *pdev); int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs, struct device_node *np); +void of_pci_make_root_bus_node(struct pci_bus *bus); +void of_pci_remove_root_bus_node(struct pci_bus *bus); +int of_pci_add_root_bus_properties(struct pci_bus *bus, struct of_changese= t *ocs, + struct device_node *np); #else static inline void of_pci_make_dev_node(struct pci_dev *pdev) { } static inline void of_pci_remove_node(struct pci_dev *pdev) { } +static inline void of_pci_make_root_bus_node(struct pci_bus *bus) { } +static inline void of_pci_remove_root_bus_node(struct pci_bus *bus) { } #endif =20 #ifdef CONFIG_PCIEAER diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index 4f68414c3086..063780bec45e 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -1049,6 +1049,8 @@ static int pci_register_host_bridge(struct pci_host_b= ridge *bridge) dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr); } =20 + of_pci_make_root_bus_node(bus); + down_write(&pci_bus_sem); list_add_tail(&bus->node, &pci_root_buses); up_write(&pci_bus_sem); diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c index e4ce1145aa3e..80cbe02f66b2 100644 --- a/drivers/pci/remove.c +++ b/drivers/pci/remove.c @@ -160,6 +160,8 @@ void pci_stop_root_bus(struct pci_bus *bus) &bus->devices, bus_list) pci_stop_bus_device(child); =20 + of_pci_remove_root_bus_node(bus); + /* stop the host bridge */ device_release_driver(&host_bridge->dev); } --=20 2.46.2