From patchwork Tue Mar 28 20:15:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Rob Herring (Arm)" X-Patchwork-Id: 13191521 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3E5B7C77B60 for ; Tue, 28 Mar 2023 20:17:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=dKOWCW3AXIwjIMP0S9XWGAEHTfb6UhYQPI26Gf7/Rho=; b=E0Wno3o9YAW0/I qDj+HAPA6mWB9+kISIymgDw9BV+4bLCrOqWHbuqoZE8IDZ/Kbgk944Eq8XpbtzsOizk4zryJYJpdm V9VjJlxSNLXJIW+W4jizwtw77R0xa+NhIVMjTg7iGfGkYIpEzF9RA8ZTWDvgsOihAVC5obw3T6dAf APP2KZQBvRDmJk6MFG70UheWosTXk8FBOUk0GOy30bbrZvwFA/hb3AVOuFqm+gOjGZn6UYlH4s+E6 6hLNUHKgm0qvIWh1yTFDeGV9Q3+IAO/bGFm7OtQL8T5HtDMygeAGaVV/L7ljol4boMXHmR+QNLlt2 2X3TT3tlX9P1CfPyTIjQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phFk5-00Fl6Z-0j; Tue, 28 Mar 2023 20:16:29 +0000 Received: from mail-oa1-f48.google.com ([209.85.160.48]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phFjs-00Fl12-2a for linux-arm-kernel@lists.infradead.org; Tue, 28 Mar 2023 20:16:18 +0000 Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-17aaa51a911so14014643fac.5 for ; Tue, 28 Mar 2023 13:16:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680034573; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WffwCkUrO2QFOwkwd1OwIoPn40+WgwuocFCo25EFRP4=; b=jKUbVtv2e1KFUdsK8llruNRK0FEew27NRweaiWJva336aVXzQNkUp52CqFIqYFISeV PJ6pX+KIUNPkN/AXrdFbVPI87HpEY3qSNrhOpR6yauMMlamfT0f3H68d8QmKP4USREn/ Qbj9hqT8tasTtIYT5wIk7ikqcjxR6mCfXrhMoTj81mEXADpXaVl6La4TD9P4qOFI2wLu 5Y3LGO5oO0Pe91fy0/ELKwXwuXpvD4FiHfeo/a3HFR69RUHJZOge5DRWmKz/0PfZkWDO komZzwTGiK1D7G6gk1gQjlsWDMJnK9YLw/WUkicBJw9BLePIOelFJu44UzNcgJ3cgKZw rMvw== X-Gm-Message-State: AAQBX9d68q9DL9lYQM5lSdvx+Lo6Hu7pFXm0Vxire0BijzBTy+ZUFiHW p234C5tW2GUnWcs/ktb2rQ== X-Google-Smtp-Source: AK7set9O95zUO5XPRjY3del1fKAHhbZzd1ayFyAZFIuT0LhS8F1687vY4wmshaSQMXVNhTAnL+nVzg== X-Received: by 2002:a05:6870:e40d:b0:169:dcba:1ec9 with SMTP id n13-20020a056870e40d00b00169dcba1ec9mr11894631oag.41.1680034573156; Tue, 28 Mar 2023 13:16:13 -0700 (PDT) Received: from robh_at_kernel.org (66-90-144-107.dyn.grandenetworks.net. [66.90.144.107]) by smtp.gmail.com with ESMTPSA id m9-20020a9d6ac9000000b0069f153bb37asm8467384otq.62.2023.03.28.13.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 13:16:12 -0700 (PDT) Received: (nullmailer pid 3993719 invoked by uid 1000); Tue, 28 Mar 2023 20:16:10 -0000 From: Rob Herring Date: Tue, 28 Mar 2023 15:15:57 -0500 Subject: [PATCH 2/5] of/address: Add of_range_to_resource() helper MIME-Version: 1.0 Message-Id: <20230328-dt-address-helpers-v1-2-e2456c3e77ab@kernel.org> References: <20230328-dt-address-helpers-v1-0-e2456c3e77ab@kernel.org> In-Reply-To: <20230328-dt-address-helpers-v1-0-e2456c3e77ab@kernel.org> To: Michael Ellerman , Nicholas Piggin , Christophe Leroy , Stuart Yoder , Laurentiu Tudor , Benjamin Herrenschmidt Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linuxppc-dev@lists.ozlabs.org X-Mailer: b4 0.13-dev X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230328_131616_833449_C7CAABEA X-CRM114-Status: GOOD ( 22.84 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org A few users need to convert a specific "ranges" entry into a struct resource. Add a helper to similar to of_address_to_resource(). The existing of_pci_range_to_resource() helper isn't really PCI specific, so it can be used with the CONFIG_PCI check dropped. Signed-off-by: Rob Herring --- drivers/of/address.c | 31 ++++++++++++++++++++++++++++--- drivers/of/unittest.c | 16 +++++++++++++++- include/linux/of_address.h | 8 ++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/drivers/of/address.c b/drivers/of/address.c index 4c0b169ef9bf..b79f005834fc 100644 --- a/drivers/of/address.c +++ b/drivers/of/address.c @@ -229,9 +229,6 @@ int of_pci_range_to_resource(struct of_pci_range *range, res->parent = res->child = res->sibling = NULL; res->name = np->full_name; - if (!IS_ENABLED(CONFIG_PCI)) - return -ENOSYS; - if (res->flags & IORESOURCE_IO) { unsigned long port; err = pci_register_io_range(&np->fwnode, range->cpu_addr, @@ -263,6 +260,34 @@ int of_pci_range_to_resource(struct of_pci_range *range, } EXPORT_SYMBOL(of_pci_range_to_resource); +/* + * of_range_to_resource - Create a resource from a ranges entry + * @np: device node where the range belongs to + * @index: the 'ranges' index to convert to a resource + * @res: pointer to a valid resource that will be updated to + * reflect the values contained in the range. + * + * Returns ENOENT if the entry is not found or EINVAL if the range cannot be + * converted to resource. + */ +int of_range_to_resource(struct device_node *np, int index, struct resource *res) +{ + int ret, i = 0; + struct of_range_parser parser; + struct of_range range; + + ret = of_range_parser_init(&parser, np); + if (ret) + return ret; + + for_each_of_range(&parser, &range) + if (i++ == index) + return of_pci_range_to_resource(&range, np, res); + + return -ENOENT; +} +EXPORT_SYMBOL(of_range_to_resource); + /* * ISA bus specific translator */ diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c index 1a45df1f354a..945288d5672f 100644 --- a/drivers/of/unittest.c +++ b/drivers/of/unittest.c @@ -1013,7 +1013,8 @@ static void __init of_unittest_bus_ranges(void) struct device_node *np; struct of_range range; struct of_range_parser parser; - int i = 0; + struct resource res; + int ret, i = 0; np = of_find_node_by_path("/testcase-data/address-tests"); if (!np) { @@ -1026,6 +1027,19 @@ static void __init of_unittest_bus_ranges(void) return; } + ret = of_range_to_resource(np, 1, &res); + unittest(!ret, "of_range_to_resource returned error (%d) node %pOF\n", + ret, np); + unittest(resource_type(&res) == IORESOURCE_MEM, + "of_range_to_resource wrong resource type on node %pOF res=%pR\n", + np, &res); + unittest(res.start == 0xd0000000, + "of_range_to_resource wrong resource start address on node %pOF res=%pR\n", + np, &res); + unittest(resource_size(&res) == 0x20000000, + "of_range_to_resource wrong resource start address on node %pOF res=%pR\n", + np, &res); + /* * Get the "ranges" from the device tree */ diff --git a/include/linux/of_address.h b/include/linux/of_address.h index 376671594746..1d005439f026 100644 --- a/include/linux/of_address.h +++ b/include/linux/of_address.h @@ -68,6 +68,8 @@ extern int of_pci_address_to_resource(struct device_node *dev, int bar, extern int of_pci_range_to_resource(struct of_pci_range *range, struct device_node *np, struct resource *res); +extern int of_range_to_resource(struct device_node *np, int index, + struct resource *res); extern bool of_dma_is_coherent(struct device_node *np); #else /* CONFIG_OF_ADDRESS */ static inline void __iomem *of_io_request_and_map(struct device_node *device, @@ -120,6 +122,12 @@ static inline int of_pci_range_to_resource(struct of_pci_range *range, return -ENOSYS; } +static inline int of_range_to_resource(struct device_node *np, int index, + struct resource *res) +{ + return -ENOSYS; +} + static inline bool of_dma_is_coherent(struct device_node *np) { return false;