From patchwork Fri Jan 10 08:21:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 13933956 X-Patchwork-Delegate: bhelgaas@google.com Received: from mslow1.mail.gandi.net (mslow1.mail.gandi.net [217.70.178.240]) (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 9847D207E04; Fri, 10 Jan 2025 08:26:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.178.240 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497611; cv=none; b=ONbKlRxTYEDY2pliZzWwYLvrXLBlCLEx6abAnSh+ZsVEJ+1QXd4xAHtsIz1+niuRcd+hCezqp1wnuCB8zkNz55GHj9NkUE6nQ6VZCySRyCFpgtv0798lOQol6WRhZQLYjUk2phG3u2oqlEQ8863hihKMCuVdKr2M5m7lj4gxE7g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497611; c=relaxed/simple; bh=5GTb6DZp06ovx0Ux9JTq0ypq0dxJ8Dwhha83bPb7smc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Piz11DnCipK5RVDWf9VAucCxZ7anWOGkYFPvFy1XMNXZBTsxn5yVDwlPNISFXerrVdM/iUadfp2x8siI8fyYZsJQ4r13qSS6EUPrlSewWQn26xPsXtyqoR+b8UKNfmd7ngAMiN6lM0xW7tQXgapaMb02kSBQHZatRmabfEFmxZI= 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=h3XFPOgw; arc=none smtp.client-ip=217.70.178.240 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="h3XFPOgw" Received: from relay7-d.mail.gandi.net (unknown [IPv6:2001:4b98:dc4:8::227]) by mslow1.mail.gandi.net (Postfix) with ESMTP id 8BBA9C47FA; Fri, 10 Jan 2025 08:21:57 +0000 (UTC) Received: by mail.gandi.net (Postfix) with ESMTPA id B3F2720008; Fri, 10 Jan 2025 08:21:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1736497315; 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=FNgXov3cni7kQDkWTh8wpdAuS8om6z2Gf6DhN7iBDVw=; b=h3XFPOgwu585jLlhhhV6jvs1PiDpZBBnQQguWwCxi7qZkRf+OEaP03KUGFlM1Lkzc0F31p MEPBp+vKiiGSTgppFNCrHEFV9v9WwfBpHpp8/i1eANOEhH/3ZVHpzV+2dyzYRFzr4Y5B6j WVK8fiJGt5K+DZjVUyBxnK0v1O6bFL4gMnT5haK6hI1fCB4dfXFoib7MR4mRhX/Nud+ML6 5zLJmQs31kxM90tQHwNpzXX0lCFfxok7oHM/R4ueCHZ9l/KPZqZB0WHmd+28nB7/XmfVWS +3AxnqQRFX/Vzhe2G9VNNqLrv31ouYAFtkJBunrJG7K2bqpMIUQliV3AjPwgAg== 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 v6 1/5] driver core: Introduce device_{add,remove}_of_node() Date: Fri, 10 Jan 2025 09:21:37 +0100 Message-ID: <20250110082143.917590-2-herve.codina@bootlin.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250110082143.917590-1-herve.codina@bootlin.com> References: <20250110082143.917590-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com 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(). Signed-off-by: Herve Codina --- drivers/base/core.c | 61 ++++++++++++++++++++++++++++++++++++++++++ include/linux/device.h | 2 ++ 2 files changed, 63 insertions(+) diff --git a/drivers/base/core.c b/drivers/base/core.c index 8b056306f04e..b32bff9ed18d 100644 --- a/drivers/base/core.c +++ b/drivers/base/core.c @@ -5216,6 +5216,67 @@ void set_secondary_fwnode(struct device *dev, struct fwnode_handle *fwnode) } EXPORT_SYMBOL_GPL(set_secondary_fwnode); +/** + * 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 = get_device(dev); + if (!dev) + return; + + if (!dev->of_node) + goto end; + + if (dev->fwnode == of_fwnode_handle(dev->of_node)) + dev->fwnode = NULL; + + of_node_put(dev->of_node); + dev->of_node = 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 + * + * Return: 0 on success or error code on failure. + */ +int device_add_of_node(struct device *dev, struct device_node *of_node) +{ + int ret; + + if (!of_node) + return -EINVAL; + + dev = get_device(dev); + if (!dev) + return -EINVAL; + + if (dev->of_node) { + dev_err(dev, "Cannot replace node %pOF with %pOF\n", + dev->of_node, of_node); + ret = -EBUSY; + goto end; + } + + dev->of_node = of_node_get(of_node); + + if (!dev->fwnode) + dev->fwnode = of_fwnode_handle(of_node); + + ret = 0; +end: + put_device(dev); + return ret; +} +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 667cb6db9019..ac1515f05e5d 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -1149,6 +1149,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); +int 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); static inline struct device_node *dev_of_node(struct device *dev) From patchwork Fri Jan 10 08:21:38 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 13933955 X-Patchwork-Delegate: bhelgaas@google.com Received: from mslow1.mail.gandi.net (mslow1.mail.gandi.net [217.70.178.240]) (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 984E4207E07; Fri, 10 Jan 2025 08:26:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.178.240 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497611; cv=none; b=ajXJ9Gf+hW3TD4aFYhmhs7LeDcY7LJ9JV2Qzw8RqYybhLlmQFkIre56Y9V5HyGfj++yILc3Cd0bBsil+bnLQGc9xJypn6cNzXpBKYtvmUlAB8GXrxw1yBm6Emu74mi5beSQ2rJ59ypNLDYx+kv/nydo2M1s8fF7mX/vZLB9vajc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497611; c=relaxed/simple; bh=hvN6WMFdEMTej8FUIz+FisYVwp6XWKotOXXluIZYQ1w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qcV6p10E+ikEVxf1HuWfAcaHuO/1UpPdc3Y6PV7Ei8EhydwV6CsJra1YL8p5v378KtXJKszMNVpvkKK1TV2StdR6OdNXoqj23kNOV9LRXHCyNY+bWWtr9qWEZYy3oQqnspa1/CyX3jRFJdnuipn0fQ2v3K54KtYNg6bM6GazQR4= 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=PVj9gx4J; arc=none smtp.client-ip=217.70.178.240 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="PVj9gx4J" Received: from relay7-d.mail.gandi.net (unknown [IPv6:2001:4b98:dc4:8::227]) by mslow1.mail.gandi.net (Postfix) with ESMTP id 44DFBC47FB; Fri, 10 Jan 2025 08:21:58 +0000 (UTC) Received: by mail.gandi.net (Postfix) with ESMTPA id 8511E20004; Fri, 10 Jan 2025 08:21:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1736497316; 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=2gGktN26Hugtn1FgaDpGqq+iIoze2rydGcl15ctXuo0=; b=PVj9gx4JRMmzegDcF5CuMSzqnSWs1R0c+U3BDSvbwsI8tlk7wnOKMmi1ma4+rjufN/20VG bLh2OjSMLHjc9lE9WRYh063LlCGHhuOrxurrmUoI3IJxcGHENI3r2JQT1r4f0qGC0UUgYM 8pdHLYfIHcHuJBOb1KpxrKEUGxtNM9+eJMV7KfGr0bPCAEq5lHxn+q4vyqrbsYDKJdTlAn 3yQCbEickbXrA9Utrz1kEf/Gd/VxIPQCbMq30MouXxvn8e6uCWtRYwlKWV9rBdXH+yyq1Q /uemdRCOUrf6P6y8xxvMJmbipUFQCPxueOFUXL5l+fbLGmaBYFpRduCCH58u/Q== 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 v6 2/5] PCI: of: Use device_{add,remove}_of_node() to attach of_node to existing device Date: Fri, 10 Jan 2025 09:21:38 +0100 Message-ID: <20250110082143.917590-3-herve.codina@bootlin.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250110082143.917590-1-herve.codina@bootlin.com> References: <20250110082143.917590-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com 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. Signed-off-by: Herve Codina --- drivers/pci/of.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/drivers/pci/of.c b/drivers/pci/of.c index 52f770bcc481..e6a39f0361da 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 = pci_device_to_OF_node(pdev); if (!np || !of_node_check_flag(np, OF_DYNAMIC)) return; - pdev->dev.of_node = NULL; + device_remove_of_node(&pdev->dev); of_changeset_revert(np->data); of_changeset_destroy(np->data); of_node_put(np); @@ -713,11 +713,18 @@ void of_pci_make_dev_node(struct pci_dev *pdev) goto out_free_node; np->data = cset; - pdev->dev.of_node = np; + + ret = device_add_of_node(&pdev->dev, np); + if (ret) + goto out_revert_cset; + kfree(name); return; +out_revert_cset: + np->data = NULL; + of_changeset_revert(cset); out_free_node: of_node_put(np); out_destroy_cset: From patchwork Fri Jan 10 08:21:39 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 13933933 X-Patchwork-Delegate: bhelgaas@google.com 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 7F92E8F54; Fri, 10 Jan 2025 08:21:58 +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=1736497320; cv=none; b=FmcQmGpvZden+FFSugpHCfIdSH+H1d/T/asGrf6Ku8F1RG0fZ5ehWv4H5eU9Hs3kE5gSnatUnX2N7ZkQrcXmqS+ZGJ3k0CctNwpmvKuVY0WLyywc0BpxCjzN5bVcxxgP7NBxG/eHL5FVsDP4ZeUgfWU5jH+L1lxCff3+GYoC6zA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497320; c=relaxed/simple; bh=P+Y9UraIZhvdw5UcVwbrlExRQcniBwLNbPoCHyp5MBw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RUG8LcfeTVC8l2xqBxRK/8gKf+/NDvs6hc2mYpOkpLjoT/i2zwSlyRXfcxgeftGBey9NwtyfiDjjiIBiC1peNBwoLDgd9GN3OquFuBZOOkBbaxoSXtTB472+w0ie1ZfyuMwdgk1HBBE+bpeRcjdlUwzwQFgz0btwfrJm2jcfm1c= 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=h9ZBQiTN; 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="h9ZBQiTN" Received: by mail.gandi.net (Postfix) with ESMTPA id 4DFE82000A; Fri, 10 Jan 2025 08:21:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1736497316; 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=l3exH2D4DHGEwR8WFnd1f/VkSRJ5bE65THJQbo6YCV8=; b=h9ZBQiTNYYJsPvzribTbTE8aKxVKfnMqB2Nbe2VV278x4sLXz4AQrOljNHZZRuoMPFrGQR fNLJ99jxv11q260FnjrWXp4S9CJO+V3vKfuZvi8NG2Bv+T4iuHhYFOA7/4TVRVhwJi8VBN p9t+J0GFfTdICwpRNSyahDpt9J+8Qwm/zT52G18z0Jv459Feu9yDc75xiL9BbzHlR8utL5 yw5MsGyaJkl1z2iJaZF4S2851G61t1X32jHO+QVeljHie/UJrCABfaNlVJ1745GLOkAyRO +mhXmHPQIhHVtXaHa0SJYwy5lKpNvap8AFBec4KyWQKMQM19Yp/LeIGEWiG3JA== 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 v6 3/5] PCI: of_property: Add support for NULL pdev in of_pci_set_address() Date: Fri, 10 Jan 2025 09:21:39 +0100 Message-ID: <20250110082143.917590-4-herve.codina@bootlin.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250110082143.917590-1-herve.codina@bootlin.com> References: <20250110082143.917590-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com 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 886c236e5de6..8aff9ca1f222 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] = 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] = 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] = 0; + prop[0] |= flags | reg_num; if (!reloc) { prop[0] |= OF_PCI_ADDR_FIELD_NONRELOC; From patchwork Fri Jan 10 08:21:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 13933934 X-Patchwork-Delegate: bhelgaas@google.com 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 5F3AA1F9428; Fri, 10 Jan 2025 08:21:59 +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=1736497321; cv=none; b=OIHqh3qcet0070vSdqx6aiZD8kcI4NugIxCLNzSP553I0pW4ckd0/eOx5SwjCiZjfTjCtEy+jMZF8hU1TeNBfJJQj/67vCdLnzOaSkQyx99XSUwr26KHUs18/XWQZF4eOABkYgzv1GtSjb7BaJPQaAKc7I3xHQpItQWEAyUayys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497321; c=relaxed/simple; bh=K6YrYOW3EAHUwaREXrvn/g6td90GW/YjSqcHRbukuFQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gpeMl1340fwa2+4T5B949fQI/pdR/LrAf0fjLQBr+stfugfPNbqlErw7dG1vYlT386AXczS8GkQZJuPan2MplI0BdSB5XR8F/H2sGqRdckZM1q29kZRmB8txpvCUPhhRMLnUKDQvkGycjMANlahRndAc1XRGnUXEGsVOR0oxeG8= 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=Nwl7RyOW; 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="Nwl7RyOW" Received: by mail.gandi.net (Postfix) with ESMTPA id 117E02000B; Fri, 10 Jan 2025 08:21:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1736497317; 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=c7RB7w8J19TlctLPPgGOEfCfHWIGmPuKJMrM/M8OuQ4=; b=Nwl7RyOWbILIZ/6xy/XhmOL+1Ljo/fWhMvJE85735o9w78iKwXjjGdkEbw8MfyuXAwwHHo f0AAPO88ioDknn48VKcJq4C/asbMWasIZOMugKSXgXn7HM60chydvRXr/axfWkk8MQ85Js gMl3ITu3WeltT7xNSTC/Ltx7pwo6T0yLFqTGhXMIIvlHjFwHKyEjt/b4MwijX9+S1qUxLQ 0dtjBWauQZwgQnf/dJ9PhnQhC8W06IwC60dOX8/cvQSW3zZiMdrU8y3k5E/Xicjd2gzgRw Sv31xdO4LaptoDWdR66BObKJ+MO2fpIT8NZFGgnevfx0kgWgNlG1jd5mMoInHA== 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 v6 4/5] PCI: of_property: Constify parameter in of_pci_get_addr_flags() Date: Fri, 10 Jan 2025 09:21:40 +0100 Message-ID: <20250110082143.917590-5-herve.codina@bootlin.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250110082143.917590-1-herve.codina@bootlin.com> References: <20250110082143.917590-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com 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 8aff9ca1f222..400c4c2e434d 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, } } -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; From patchwork Fri Jan 10 08:21:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herve Codina X-Patchwork-Id: 13933935 X-Patchwork-Delegate: bhelgaas@google.com 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 0BB91205E01; Fri, 10 Jan 2025 08:21:59 +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=1736497322; cv=none; b=ST2R9tJ2FphUDubFRBBI7jQkJGvrgQZFzmNzQRpKJQbl0WvHLpOzKrIaEDBPhg0uX2aFdrJFWrVzxOyv1fuqLxIiQcZF8DZ7QHsA42W0q8e4cxM4TYXxGNvwSV0VY/mOxIPOkzqQLyQ6pOSBFvI0Bq7q/xqDeR+/xWDrjuLQKes= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736497322; c=relaxed/simple; bh=ZTPQQiYo5FcdZZNPo/ud/Ogz2M8NyBLhNnyA5ApBSM8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=b3/uN8Zisk3FrrAYhRJ+cIUaLpHbl4xXikA9DXh2vk8mn0NooQfdDp9lIBOFr4lXfRH7y18XAcIgFyz0sPHcV/vYkTUyaaMhShV1PUvuVIkyEuH4am2o9+UZKv1od0yhpIisDyUqAytMOXpxAwcT6hCGvxMOXxJJ2ctDCy3kGwo= 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=GrhaUQ1Z; 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="GrhaUQ1Z" Received: by mail.gandi.net (Postfix) with ESMTPA id D28C92000D; Fri, 10 Jan 2025 08:21:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1736497318; 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=6MnfR/Awinh9heHET9F76+N2AeumaMbCYnlUO2ZbjvY=; b=GrhaUQ1ZyRMNR2ZYYpNXACGJpVncVjIi4S6Y2shTS6JjBv6yyWLR8SBuGD8wM2mxh5LjLu FDyZE+BjRdoMB/RKF/29sEerrfIcWATB81dcVaTSi0AWPfRFckndaTb4xtIPA1YObJInp/ Cd1MLjTeV31DDiXt9YvmT2nGo8LCiBrKfX+j1y/8NYBspTSuMnS7ushaOHgOVxD0YSClaE Dc4Vd12gk2+0ZEek0Y/VMkm+Xkh5EPg2MB+MbAByRPSRMiFfjp0QqE5ldjDH5jNZl80O+x a1RkIJhmRYEtQpqyPISz/+I1gzOr2CqvjYsfdyYT3VhcjGItvta0EW5TlraVVQ== 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 v6 5/5] PCI: of: Create device-tree PCI host bridge node Date: Fri, 10 Jan 2025 09:21:41 +0100 Message-ID: <20250110082143.917590-6-herve.codina@bootlin.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: <20250110082143.917590-1-herve.codina@bootlin.com> References: <20250110082143.917590-1-herve.codina@bootlin.com> Precedence: bulk X-Mailing-List: linux-pci@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-GND-Sasl: herve.codina@bootlin.com 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 (the PCI bus handled by the PCI host bridge), a PCI 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 device-tree based systems, this PCI root bus device tree node is set to the node of the related PCI host bridge. The PCI host bridge node is available in the device-tree used to describe the hardware passed at boot. On non device-tree based system (such as ACPI), a device-tree node for the PCI host bridge or for the root bus does not exist. Indeed, the PCI host bridge is not described in a device-tree used at boot simply because no device-tree are passed at boot. The device-tree PCI host bridge 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. Also, as is done on device-tree based systems, this PCI host bridge node is used for the PCI root bus. Signed-off-by: Herve Codina --- drivers/pci/of.c | 104 +++++++++++++++++++++++++++++++++++++- drivers/pci/of_property.c | 102 +++++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 6 +++ drivers/pci/probe.c | 2 + drivers/pci/remove.c | 2 + 5 files changed, 215 insertions(+), 1 deletion(-) diff --git a/drivers/pci/of.c b/drivers/pci/of.c index e6a39f0361da..ac2ae64a6eac 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -733,7 +733,109 @@ void of_pci_make_dev_node(struct pci_dev *pdev) out_free_name: kfree(name); } -#endif + +void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge) +{ + struct device_node *np; + + np = pci_bus_to_OF_node(bridge->bus); + if (!np || !of_node_check_flag(np, OF_DYNAMIC)) + return; + + device_remove_of_node(&bridge->bus->dev); + device_remove_of_node(&bridge->dev); + of_changeset_revert(np->data); + of_changeset_destroy(np->data); + of_node_put(np); +} + +void of_pci_make_host_bridge_node(struct pci_host_bridge *bridge) +{ + struct device_node *np = NULL; + struct of_changeset *cset; + const char *name; + int ret; + + /* + * If there is already a device-tree node linked to the PCI bus handled + * by this bridge (i.e. the PCI root bus), nothing to do. + */ + if (pci_bus_to_OF_node(bridge->bus)) + return; + + /* The root bus has no node. Check that the host bridge has no node too */ + if (bridge->dev.of_node) { + dev_err(&bridge->dev, "PCI host bridge of_node already set"); + return; + } + + /* Check if there is a DT root node to attach the created node */ + if (!of_root) { + pr_err("of_root node is NULL, cannot create PCI host bridge node\n"); + return; + } + + name = kasprintf(GFP_KERNEL, "pci@%x,%x", pci_domain_nr(bridge->bus), + bridge->bus->number); + if (!name) + return; + + cset = kmalloc(sizeof(*cset), GFP_KERNEL); + if (!cset) + goto out_free_name; + of_changeset_init(cset); + + np = of_changeset_create_node(cset, of_root, name); + if (!np) + goto out_destroy_cset; + + ret = of_pci_add_host_bridge_properties(bridge, 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 = &bridge->dev; + fwnode_dev_initialized(&np->fwnode, true); + + ret = of_changeset_apply(cset); + if (ret) + goto out_free_node; + + np->data = cset; + + /* Add the of_node to host bridge and the root bus */ + ret = device_add_of_node(&bridge->dev, np); + if (ret) + goto out_revert_cset; + + ret = device_add_of_node(&bridge->bus->dev, np); + if (ret) + goto out_remove_bridge_dev_of_node; + + kfree(name); + + return; + +out_remove_bridge_dev_of_node: + device_remove_of_node(&bridge->dev); +out_revert_cset: + np->data = NULL; + of_changeset_revert(cset); +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 */ /** * of_pci_supply_present() - Check if the power supply is present for the PCI diff --git a/drivers/pci/of_property.c b/drivers/pci/of_property.c index 400c4c2e434d..b03baff651ee 100644 --- a/drivers/pci/of_property.c +++ b/drivers/pci/of_property.c @@ -394,3 +394,105 @@ int of_pci_add_properties(struct pci_dev *pdev, struct of_changeset *ocs, return 0; } + +static bool of_pci_is_range_resource(const struct resource *res, u32 *flags) +{ + 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_host_bridge_prop_ranges(struct pci_host_bridge *bridge, + struct of_changeset *ocs, + struct device_node *np) +{ + struct resource_entry *window; + unsigned int ranges_sz = 0; + unsigned int n_range = 0; + struct resource *res; + int n_addr_cells; + u32 *ranges; + u64 val64; + u32 flags; + int ret; + + n_addr_cells = of_n_addr_cells(np); + if (n_addr_cells <= 0 || n_addr_cells > 2) + return -EINVAL; + + resource_list_for_each_entry(window, &bridge->windows) { + res = window->res; + if (!of_pci_is_range_resource(res, &flags)) + continue; + n_range++; + } + + if (!n_range) + return 0; + + ranges = 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 = window->res; + if (!of_pci_is_range_resource(res, &flags)) + continue; + + /* PCI bus address */ + val64 = res->start; + of_pci_set_address(NULL, &ranges[ranges_sz], val64 - window->offset, + 0, flags, false); + ranges_sz += OF_PCI_ADDRESS_CELLS; + + /* Host bus address */ + if (n_addr_cells == 2) + ranges[ranges_sz++] = upper_32_bits(val64); + ranges[ranges_sz++] = lower_32_bits(val64); + + /* Size */ + val64 = resource_size(res); + ranges[ranges_sz] = upper_32_bits(val64); + ranges[ranges_sz + 1] = lower_32_bits(val64); + ranges_sz += OF_PCI_SIZE_CELLS; + } + + ret = of_changeset_add_prop_u32_array(ocs, np, "ranges", ranges, ranges_sz); + kfree(ranges); + return ret; +} + +int of_pci_add_host_bridge_properties(struct pci_host_bridge *bridge, + struct of_changeset *ocs, + struct device_node *np) +{ + int ret; + + ret = of_changeset_add_prop_string(ocs, np, "device_type", "pci"); + if (ret) + return ret; + + ret = of_changeset_add_prop_u32(ocs, np, "#address-cells", + OF_PCI_ADDRESS_CELLS); + if (ret) + return ret; + + ret = of_changeset_add_prop_u32(ocs, np, "#size-cells", + OF_PCI_SIZE_CELLS); + if (ret) + return ret; + + ret = of_pci_host_bridge_prop_ranges(bridge, ocs, np); + if (ret) + return ret; + + return 0; +} diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h index 2e40fc63ba31..0cdb2b3daea8 100644 --- a/drivers/pci/pci.h +++ b/drivers/pci/pci.h @@ -871,9 +871,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_host_bridge_node(struct pci_host_bridge *bridge); +void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge); +int of_pci_add_host_bridge_properties(struct pci_host_bridge *bridge, struct of_changeset *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_host_bridge_node(struct pci_host_bridge *bridge) { } +static inline void of_pci_remove_host_bridge_node(struct pci_host_bridge *bridge) { } #endif #ifdef CONFIG_PCIEAER diff --git a/drivers/pci/probe.c b/drivers/pci/probe.c index 2e81ab0f5a25..629287f6b3d9 100644 --- a/drivers/pci/probe.c +++ b/drivers/pci/probe.c @@ -1051,6 +1051,8 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge) dev_info(&bus->dev, "root bus resource %pR%s\n", res, addr); } + of_pci_make_host_bridge_node(bridge); + 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 efc37fcb73e2..9f7df2b20183 100644 --- a/drivers/pci/remove.c +++ b/drivers/pci/remove.c @@ -163,6 +163,8 @@ void pci_stop_root_bus(struct pci_bus *bus) &bus->devices, bus_list) pci_stop_bus_device(child); + of_pci_remove_host_bridge_node(host_bridge); + /* stop the host bridge */ device_release_driver(&host_bridge->dev); }