From mboxrd@z Thu Jan 1 00:00:00 1970 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 B2A28C76196 for ; Tue, 28 Mar 2023 20:17:14 +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=gMbSHuhph+cMdnK/qNwWaMVv56FbKML1ttcsZAri1Z8=; b=CXAx2O7UqNEEZ4 kkE2elpgw/rknvDiECIRfbez57lKoErHjZUyy/JhfcVU7gByeIWwYC32hj8/zwgnvZwTUlCV0v4ed 8A1HCXZ+cmb9THBhH9hHZd0CR44tp68WBT57UWstVshcGfhIu0DQKlGHFsEHm9cL5TplErvbQaSI0 oYr2bLt8MHO85tIItQONcU8k6dPjm6k25KfhHOeYykrr3SwLDhwCZLz3JvMNo+a0VJhIhWUil0Pp4 O4q48DLVh0tCpl7l+Mw9cbb6fM2t6RvY+4JCw/F6ws5HuEUXKpj4aVjq91gNS7YClq1wk2HkxhzFX Lpygw1Qg2R1IeyK+Vt5A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1phFk4-00Fl6A-0g; Tue, 28 Mar 2023 20:16:28 +0000 Received: from mail-oo1-f53.google.com ([209.85.161.53]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1phFjs-00Fl18-1h for linux-arm-kernel@lists.infradead.org; Tue, 28 Mar 2023 20:16:17 +0000 Received: by mail-oo1-f53.google.com with SMTP id g21-20020a4ad855000000b0053e563c2f72so886451oov.6 for ; Tue, 28 Mar 2023 13:16:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680034574; 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=wq9MleJbVtyTb3QrV3Xlq0I3I3gqQWPoavFTMvWxBWU=; b=lnJitPzVf3NgW7K6xDMVMPxbT+Yn1WklDFWbi62D/OEyPzCRlX6/fKkntEEvPcSc8p 90JuhvVLAaZ+X+5d+3NnyaZvaTzHTzC0CGfJwVkvc3TwEyzfzRyOEujbCWXAzgWqkTqT oe0IaRCpUITikBnwlgsXTQLWyKXHCOKTWWJGWGwNyDHnd85TWs8ggiuCHDb2XFURl1Yn eHbsxjXbEkV/5g9v53nRwD6F4ljMFKHj6AXWbieqwv6f+jEflzWx7HaPHo7iZ2bXx9Ju jSDhE46cEjKPgP8DjVJtFKlRKfg23Vz0EqcvYSsjZAwiJVnkjE4aOpN2PAjmfm6mRkC7 Q5Uw== X-Gm-Message-State: AO0yUKXcG8Pe20H3jXRaXzZyDq+Yw8ugSbztLfUTV8EbMXfAEF8XjgD+ ZUKaOiGsE87e6chlZbHDTg== X-Google-Smtp-Source: AK7set9De6XnycHJIQgLEMSB8BvrDZtnx3/knyjsjlWaCorjvDtfznD+X/H4ZPT08mbWWvY4Eor3wg== X-Received: by 2002:a4a:4f83:0:b0:53b:4b94:45bf with SMTP id c125-20020a4a4f83000000b0053b4b9445bfmr8745156oob.3.1680034574395; Tue, 28 Mar 2023 13:16:14 -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 c192-20020a4a4fc9000000b0053b88b03e24sm7136185oob.18.2023.03.28.13.16.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 13:16:13 -0700 (PDT) Received: (nullmailer pid 3993734 invoked by uid 1000); Tue, 28 Mar 2023 20:16:10 -0000 From: Rob Herring Date: Tue, 28 Mar 2023 15:15:59 -0500 Subject: [PATCH 4/5] of/address: Add of_range_count() helper MIME-Version: 1.0 Message-Id: <20230328-dt-address-helpers-v1-4-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_563386_53B5C550 X-CRM114-Status: GOOD ( 17.05 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Some users need a count of the number of ranges entries before iterating over the entries. Typically this is for allocating some data structure based on the size. Add a helper, of_range_count(), to get the count. The helper must be called with an struct of_range_parser initialized by of_range_parser_init(). Signed-off-by: Rob Herring --- drivers/of/unittest.c | 7 ++++++- include/linux/of_address.h | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/drivers/of/unittest.c b/drivers/of/unittest.c index 29066ecbed47..eaeb58065acc 100644 --- a/drivers/of/unittest.c +++ b/drivers/of/unittest.c @@ -1014,7 +1014,7 @@ static void __init of_unittest_bus_ranges(void) struct of_range range; struct of_range_parser parser; struct resource res; - int ret, i = 0; + int ret, count, i = 0; np = of_find_node_by_path("/testcase-data/address-tests"); if (!np) { @@ -1040,6 +1040,11 @@ static void __init of_unittest_bus_ranges(void) "of_range_to_resource wrong resource start address on node %pOF res=%pR\n", np, &res); + count = of_range_count(&parser); + unittest(count == 2, + "of_range_count wrong size on node %pOF count=%d\n", + np, count); + /* * Get the "ranges" from the device tree */ diff --git a/include/linux/of_address.h b/include/linux/of_address.h index 1d005439f026..5292f62c1baa 100644 --- a/include/linux/of_address.h +++ b/include/linux/of_address.h @@ -35,6 +35,22 @@ struct of_pci_range { for (; of_pci_range_parser_one(parser, range);) #define for_each_of_range for_each_of_pci_range +/* + * of_range_count - Get the number of "ranges" or "dma-ranges" entries + * @parser: Parser state initialized by of_range_parser_init() + * + * Returns the number of entries or 0 if none. + * + * Note that calling this within or after the for_each_of_range() iterator will + * be inaccurate giving the number of entries remaining. + */ +static inline int of_range_count(const struct of_range_parser *parser) +{ + if (!parser || !parser->node || !parser->range || parser->range == parser->end) + return 0; + return (parser->end - parser->range) / (parser->na + parser->pna + parser->ns); +} + /* Translate a DMA address from device space to CPU space */ extern u64 of_translate_dma_address(struct device_node *dev, const __be32 *in_addr); -- 2.39.2 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel