Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4472314ioa; Wed, 27 Apr 2022 04:45:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz0Qd8p/74ObZXUkLFuRkTAkSk4/FCyJdozd6Cl2cTZZ9ZMXmZWDeYpD8TpQm4BWmGL18Jc X-Received: by 2002:a62:be14:0:b0:505:a43b:cf6e with SMTP id l20-20020a62be14000000b00505a43bcf6emr29465411pff.33.1651059906163; Wed, 27 Apr 2022 04:45:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651059906; cv=none; d=google.com; s=arc-20160816; b=a/iixtTtrbVGEDnJuBpsH0wz2KleJV7TUy0YEI5tau/GZRMIwuc3suBG/KLpJmA39v WQhAsLeHsORJ4yQx1LMS8n4GiEn/FNmRQR3xsTAAcil/BBmDjf9pbVvg6YSpSHCCDErm lmdvmlSYn7nxFcgNXJoG1IUH+s4U2blhbVxzEDDRj6samz03YVPDHhNqklX9YiSyx9nQ wnZd9t5ob9Xsuy9ZB7gTY3maxYK4Ju2tewWejVtsInXiRWkMxeRuK5AIXkXRz3Fat42m xKGe32TX9UXUoLsQmmQEFAR/x210jFoZ2tZhOUDSutzOdp2OBRpYawz5WOqcpybZ61yl 89Hg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uCL2E5kCBWUm8Rv1Y0Fyb1TZVoxBKQdT85eHkMBS2Gk=; b=Yp1sycqqIeA8VhkHiR7NwDpkwNY/6+1eGAEGOw3HR9UEhmzfSWPNzEOhWklvLoi+0w m3d+7vqdxCypztCxPDkBPhyLyKwl9Q70ZPUzSBRH7jYTKN4tTAVQCTk3fhhhKngcVcmh UeCgiv5jGc4sWHIGXWCqhQMb1ZylA1mzqzrE9IBbBncRcGd66rGRzMkAFTYndB5IPqDD W9VQsyfgDE2GgiEAyjmovQ+qoDdP9dp/Fi0+M0ihVWB9zF1clxDEPb7EUN0n5rFCNy0R IeHt1A6//hbTymmVPakPm+/S2ayppKmzL9aAhGcwpm8jWY/Vs9a6ULV1lEmwlS+u94B3 2nbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=K3CtJnVk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id nu7-20020a17090b1b0700b001d9bee7dd25si4484599pjb.101.2022.04.27.04.45.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 04:45:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=K3CtJnVk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id ACCFE3AB9A4; Wed, 27 Apr 2022 04:03:09 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232555AbiD0LFa (ORCPT + 99 others); Wed, 27 Apr 2022 07:05:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38030 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232666AbiD0LE7 (ORCPT ); Wed, 27 Apr 2022 07:04:59 -0400 Received: from mslow1.mail.gandi.net (mslow1.mail.gandi.net [217.70.178.240]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 976A55535A; Wed, 27 Apr 2022 03:54:55 -0700 (PDT) Received: from relay1-d.mail.gandi.net (unknown [217.70.183.193]) by mslow1.mail.gandi.net (Postfix) with ESMTP id 0C2BFC56EA; Wed, 27 Apr 2022 09:48:19 +0000 (UTC) Received: (Authenticated sender: clement.leger@bootlin.com) by mail.gandi.net (Postfix) with ESMTPSA id 2AD19240004; Wed, 27 Apr 2022 09:46:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1651052811; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uCL2E5kCBWUm8Rv1Y0Fyb1TZVoxBKQdT85eHkMBS2Gk=; b=K3CtJnVkdGWY/rajy2k4wBoDtHaTL8VcDQi3rR/3mSlToPj3Kv/u1ngd2sMhS0g40eehUc twIXRW6OhVa1QtltSABUu7vFxAa9aus2+7EgzEDSQ9a3WEWZoeGdjnd9eWGI3wy0m7NFiX MbRSA6uTj+xTd5cJ/e+IB7v0bYl1MaeALEE5jjyFWuFm4vxV6Kh1M7RmDrr52vSa5SGrIH q7lONIS0c9DqDlYSgNPlNrY2MVV4BCpETqWvScYWfic568JrdialNNqU2CjN82a9q+8vo1 yUutaeTH6CMIOK1Q534FsGNCmV4xYzlCElcqw7HzlEy42FZGPua8eBqezKXxKw== From: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= To: Rob Herring , Frank Rowand , Pantelis Antoniou , Bjorn Helgaas Cc: =?UTF-8?q?Cl=C3=A9ment=20L=C3=A9ger?= , Allan Nielsen , Horatiu Vultur , Steen Hegelund , Thomas Petazzoni , Alexandre Belloni , Mark Brown , Andy Shevchenko , Jakub Kicinski , Hans de Goede , Andrew Lunn , devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-pci@vger.kernel.org, Rob Herring Subject: [PATCH 2/3] PCI: of: create DT nodes for PCI devices if they do not exists Date: Wed, 27 Apr 2022 11:45:01 +0200 Message-Id: <20220427094502.456111-3-clement.leger@bootlin.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220427094502.456111-1-clement.leger@bootlin.com> References: <20220427094502.456111-1-clement.leger@bootlin.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In order to apply overlays to PCI device nodes, the nodes must first exist. This commit add support to populate a skeleton tree for PCI bus and devices. These nodes can then be used by drivers to apply overlays. Co-developed-by: Rob Herring Signed-off-by: Rob Herring Signed-off-by: Clément Léger --- drivers/pci/of.c | 184 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 184 insertions(+) diff --git a/drivers/pci/of.c b/drivers/pci/of.c index cb2e8351c2cc..f2325708726e 100644 --- a/drivers/pci/of.c +++ b/drivers/pci/of.c @@ -16,12 +16,194 @@ #include "pci.h" #ifdef CONFIG_PCI +static int of_pci_add_property(struct of_changeset *ocs, struct device_node *np, + const char *name, const void *value, int length) +{ + struct property *prop; + int ret = -ENOMEM; + + prop = kzalloc(sizeof(*prop), GFP_KERNEL); + if (!prop) + return -ENOMEM; + + prop->name = kstrdup(name, GFP_KERNEL); + if (!prop->name) + goto out_err; + + if (value) { + prop->value = kmemdup(value, length, GFP_KERNEL); + if (!prop->value) + goto out_err; + } else { + /* + * Even if the property has no value, it must be set to a + * non-null value since of_get_property() is used to check + * some values that might or not have a values (ranges for + * instance). Moreover, when the node is released, prop->value + * is kfreed so the memory must come from kmalloc. + */ + prop->value = kmalloc(1, GFP_KERNEL); + if (!prop->value) + goto out_err; + } + + of_property_set_flag(prop, OF_DYNAMIC); + + prop->length = length; + + ret = of_changeset_add_property(ocs, np, prop); + if (!ret) + return 0; + +out_err: + kfree(prop->value); + kfree(prop->name); + kfree(prop); + + return ret; +} + +static struct device_node *of_pci_make_node(void) +{ + struct device_node *node; + + node = kzalloc(sizeof(*node), GFP_KERNEL); + if (!node) + return NULL; + + of_node_set_flag(node, OF_DYNAMIC); + of_node_set_flag(node, OF_DETACHED); + of_node_init(node); + + return node; +} + +static int of_pci_add_cells_props(struct device_node *node, + struct of_changeset *cs, int n_addr_cells, + int n_size_cells) +{ + __be32 val; + int ret; + + ret = of_pci_add_property(cs, node, "ranges", NULL, 0); + if (ret) + return ret; + + val = __cpu_to_be32(n_addr_cells); + ret = of_pci_add_property(cs, node, "#address-cells", &val, + sizeof(__be32)); + if (ret) + return ret; + + val = __cpu_to_be32(n_size_cells); + return of_pci_add_property(cs, node, "#size-cells", &val, + sizeof(__be32)); +} + +static int of_pci_add_pci_bus_props(struct device_node *node, + struct of_changeset *cs) +{ + int ret; + + ret = of_pci_add_property(cs, node, "device_type", "pci", + strlen("pci") + 1); + if (ret) + return ret; + + return of_pci_add_cells_props(node, cs, 3, 2); +} + +static void of_pci_make_dev_node(struct pci_dev *dev) +{ + static struct of_changeset cs; + const char *pci_type = "dev"; + struct device_node *node; + __be32 val[5] = {0}; + int ret; + + node = of_pci_make_node(); + if (!node) + return; + + node->parent = dev->bus->dev.of_node; + of_changeset_init(&cs); + + if (pci_is_bridge(dev)) { + ret = of_pci_add_pci_bus_props(node, &cs); + if (ret) + goto changeset_destroy; + pci_type = "pci"; + } + + node->full_name = kasprintf(GFP_KERNEL, "%s@%x,%x", pci_type, + PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn)); + + val[0] = __cpu_to_be32(dev->devfn << 8); + val[4] = __cpu_to_be32(SZ_4K); + ret = of_pci_add_property(&cs, node, "reg", val, 5 * sizeof(__be32)); + if (ret) + goto changeset_destroy; + + ret = of_changeset_attach_node(&cs, node); + if (ret) + goto changeset_destroy; + + ret = of_changeset_apply(&cs); + if (ret) + goto changeset_destroy; + + dev->dev.of_node = node; + + return; + +changeset_destroy: + of_changeset_destroy(&cs); + kfree(node); +} + +static struct device_node *of_pci_create_root_bus_node(struct pci_bus *bus) +{ + static struct of_changeset cs; + struct device_node *node; + int ret; + + node = of_pci_make_node(); + if (!node) + return NULL; + + node->full_name = "pci"; + node->parent = of_root; + + of_changeset_init(&cs); + ret = of_pci_add_pci_bus_props(node, &cs); + if (ret) + goto changeset_destroy; + + ret = of_changeset_attach_node(&cs, node); + if (ret) + goto changeset_destroy; + + ret = of_changeset_apply(&cs); + if (ret) + goto changeset_destroy; + + return node; + +changeset_destroy: + of_changeset_destroy(&cs); + kfree(node); + + return NULL; +} + void pci_set_of_node(struct pci_dev *dev) { if (!dev->bus->dev.of_node) return; dev->dev.of_node = of_pci_find_child_device(dev->bus->dev.of_node, dev->devfn); + if (!dev->dev.of_node) + of_pci_make_dev_node(dev); if (dev->dev.of_node) dev->dev.fwnode = &dev->dev.of_node->fwnode; } @@ -39,6 +221,8 @@ void pci_set_bus_of_node(struct pci_bus *bus) if (bus->self == NULL) { node = pcibios_get_phb_of_node(bus); + if (!node) + node = of_pci_create_root_bus_node(bus); } else { node = of_node_get(bus->self->dev.of_node); if (node && of_property_read_bool(node, "external-facing")) -- 2.34.1