From patchwork Fri Feb 12 13:20:20 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Syed Nayyar Waris X-Patchwork-Id: 12085293 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.2 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C34FAC433DB for ; Fri, 12 Feb 2021 13:22:02 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7698C64E57 for ; Fri, 12 Feb 2021 13:22:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7698C64E57 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=NkdpLuabyNPlinjdRUAVw+TsEaGSAAEDPYiZxK8R/JI=; b=rCpr52cWr77Y/ZPoU94i7td6w RzqsNhpgMt6K/FgmpgDvqHwa8amyxETTq7nI27KG20qlpm1EwXQO1rvU98HWDxmStOj8paKIh05E/ gES+xSHLPhMiKK/DZP8DBh7fqeNh4ULQHKefQekpuAEAsl0LMQS3E51QgIcQhKx9FHz4Qg+JRLWOM zWgUySP4cWcBBXyvvJmlG3l3OeshKHcEXw0LciMQG0JNVX4yBz+D0QG35/v7C+78QMGM0tAcEAq0a cQpmKxcmhUgtBh4NYjfLyHIKWdpnH8KH55ZF18m6iih3XJjl3+UmBPFsoDQ6jQTC7cAfMEpvcE3kt xIwWu2d4g==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lAYNK-0002Zw-GC; Fri, 12 Feb 2021 13:20:46 +0000 Received: from mail-pf1-x434.google.com ([2607:f8b0:4864:20::434]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1lAYNH-0002Yd-MQ for linux-arm-kernel@lists.infradead.org; Fri, 12 Feb 2021 13:20:44 +0000 Received: by mail-pf1-x434.google.com with SMTP id 189so5814621pfy.6 for ; Fri, 12 Feb 2021 05:20:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=cTQEqXBmnHB2EgizLse4tydi3im1uOQRvgvnrkN47J8=; b=c2m4BrFm6utQ8jG49ztsui33dIHmVYha/smSZZp6CdGbN7x9X8NAVvf3UAdn+H12Jf Bo/Kqyr9ttnxE0XM3TbkYTsU7JNjWd8TT3R+7hMNaxPGffGQATTAcoXgDnFLcimQO7zY 5zYQ3qsfhBtXcTRcs/ESmGyiydTjxZNKzG1rhh8VRoq6a9MfdkeUx7v83NhyFm9bCpyP lQcCy3UGYiCOgJAdMM9+YD2A0uiCRiHzXj9VwF/Kxu2cwhAqmwPxhWLLb1/FXUlCdi6T 9W6s4mawVTzuZtj2f/spx5VzYgK99wKtjIGGWARfmKrjt57/SpbfdLQ3W+Bj4lS1ife7 4wcg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=cTQEqXBmnHB2EgizLse4tydi3im1uOQRvgvnrkN47J8=; b=sLHLpiz129yLH7KdVMsdsOHee1aiS6SryFjxckSitYdltL0ITY6+O9onfzIN5Tim5o gHEOYrs2KFn5/TVoqW0QtCkASwRbaz+tw+oaK7s0TPNsVRY+hzIFGKqks0yX16qjTzzT Jh9uiF68sVpZCXvHO2+HBzEZehB5Loz+V+qmEsJ5btE+xW2WMuBzkLSoig0BrHbtgDC6 8YtG8gxS5F2t9PYi4dzGa5pxVmwDylWVGWccWiNXNoi9ZfJGWBxus/MNPCmGS6I+bLm2 1Ad3//AhOKrzBTqVH9Matbmk1ncfLB9lXJ454tsFrtr3f+jYsQ+a1DVvV87UO5zA5mzz qpvg== X-Gm-Message-State: AOAM530mnesuahLdhxYSZ2EnzJ1uG+wZjxEGsWtBjCOh+dAi+pI8CriS o4nQrjxmrfSL25q2+jz7skE= X-Google-Smtp-Source: ABdhPJwwF+8ia0xQ32smGqMNWm3lJv4V7czNffAMHLs+siAu92CyuitiArgz+CfLeEAsLSE+LL7sZA== X-Received: by 2002:a65:6688:: with SMTP id b8mr3133522pgw.158.1613136041576; Fri, 12 Feb 2021 05:20:41 -0800 (PST) Received: from syed.domain.name ([103.201.127.1]) by smtp.gmail.com with ESMTPSA id w188sm7287592pfw.177.2021.02.12.05.20.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Feb 2021 05:20:41 -0800 (PST) Date: Fri, 12 Feb 2021 18:50:20 +0530 From: Syed Nayyar Waris To: bgolaszewski@baylibre.com Subject: [PATCH v2 1/3] gpiolib: Introduce the for_each_set_clump macro Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210212_082043_793691_CE725069 X-CRM114-Status: GOOD ( 23.80 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, amit.kucheria@verdurent.com, arnd@arndb.de, yamada.masahiro@socionext.com, linux-kernel@vger.kernel.org, linus.walleij@linaro.org, daniel.lezcano@linaro.org, vilhelm.gray@gmail.com, michal.simek@xilinx.com, bgolaszewski@baylibre.com, rrichter@marvell.com, linux-gpio@vger.kernel.org, linux-pm@vger.kernel.org, akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, rui.zhang@intel.com, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This macro iterates for each group of bits (clump) with set bits, within a bitmap memory region. For each iteration, "start" is set to the bit offset of the found clump, while the respective clump value is stored to the location pointed by "clump". Additionally, the bitmap_get_value() and bitmap_set_value() functions are introduced to respectively get and set a value of n-bits in a bitmap memory region. The n-bits can have any size from 1 to BITS_PER_LONG. size less than 1 or more than BITS_PER_LONG causes undefined behaviour. Moreover, during setting value of n-bit in bitmap, if a situation arise that the width of next n-bit is exceeding the word boundary, then it will divide itself such that some portion of it is stored in that word, while the remaining portion is stored in the next higher word. Similar situation occurs while retrieving the value from bitmap. Cc: Linus Walleij Cc: Bartosz Gołaszewski Cc: Arnd Bergmann Cc: William Breathitt Gray Cc: Andy Shevchenko Signed-off-by: Syed Nayyar Waris Acked-by: William Breathitt Gray --- drivers/gpio/gpiolib.c | 90 ++++++++++++++++++++++++++++++++++++++++++ drivers/gpio/gpiolib.h | 28 +++++++++++++ 2 files changed, 118 insertions(+) diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index b02cc2abd3b6..282ae599c143 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -4342,6 +4342,96 @@ static int gpiolib_seq_show(struct seq_file *s, void *v) return 0; } +/** + * bitmap_get_value - get a value of n-bits from the memory region + * @map: address to the bitmap memory region + * @start: bit offset of the n-bit value + * @nbits: size of value in bits (must be between 1 and BITS_PER_LONG inclusive). + * + * Returns value of nbits located at the @start bit offset within the @map + * memory region. + */ +unsigned long bitmap_get_value(const unsigned long *map, + unsigned long start, + unsigned long nbits) +{ + const size_t index = BIT_WORD(start); + const unsigned long offset = start % BITS_PER_LONG; + const unsigned long ceiling = round_up(start + 1, BITS_PER_LONG); + const unsigned long space = ceiling - start; + unsigned long value_low, value_high; + + if (space >= nbits) + return (map[index] >> offset) & GENMASK(nbits - 1, 0); + else { + value_low = map[index] & BITMAP_FIRST_WORD_MASK(start); + value_high = map[index + 1] & BITMAP_LAST_WORD_MASK(start + nbits); + return (value_low >> offset) | (value_high << space); + } +} +EXPORT_SYMBOL_GPL(bitmap_get_value); + +/** + * bitmap_set_value - set value within a memory region + * @map: address to the bitmap memory region + * @nbits: size of map in bits + * @value: value of clump + * @value_width: size of value in bits (must be between 1 and BITS_PER_LONG inclusive) + * @start: bit offset of the value + */ +void bitmap_set_value(unsigned long *map, unsigned long nbits, + unsigned long value, unsigned long value_width, + unsigned long start) +{ + const unsigned long index = BIT_WORD(start); + const unsigned long length = BIT_WORD(nbits); + const unsigned long offset = start % BITS_PER_LONG; + const unsigned long ceiling = round_up(start + 1, BITS_PER_LONG); + const unsigned long space = ceiling - start; + + value &= GENMASK(value_width - 1, 0); + + if (space >= value_width) { + map[index] &= ~(GENMASK(value_width - 1, 0) << offset); + map[index] |= value << offset; + } else { + map[index + 0] &= ~BITMAP_FIRST_WORD_MASK(start); + map[index + 0] |= value << offset; + + if (index + 1 >= length) + return; + + map[index + 1] &= ~BITMAP_LAST_WORD_MASK(start + value_width); + map[index + 1] |= value >> space; + } +} +EXPORT_SYMBOL_GPL(bitmap_set_value); + +/** + * find_next_clump - find next clump with set bits in a memory region + * @clump: location to store copy of found clump + * @addr: address to base the search on + * @size: bitmap size in number of bits + * @offset: bit offset at which to start searching + * @clump_size: clump size in bits + * + * Returns the bit offset for the next set clump; the found clump value is + * copied to the location pointed by @clump. If no bits are set, returns @size. + */ +unsigned long find_next_clump(unsigned long *clump, const unsigned long *addr, + unsigned long size, unsigned long offset, + unsigned long clump_size) +{ + offset = find_next_bit(addr, size, offset); + if (offset == size) + return size; + + offset = rounddown(offset, clump_size); + *clump = bitmap_get_value(addr, offset, clump_size); + return offset; +} +EXPORT_SYMBOL_GPL(find_next_clump); + static const struct seq_operations gpiolib_sops = { .start = gpiolib_seq_start, .next = gpiolib_seq_next, diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 30bc3f80f83e..41c6b24d9842 100644 --- a/drivers/gpio/gpiolib.h +++ b/drivers/gpio/gpiolib.h @@ -141,6 +141,34 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce); int gpiod_hog(struct gpio_desc *desc, const char *name, unsigned long lflags, enum gpiod_flags dflags); +unsigned long bitmap_get_value(const unsigned long *map, + unsigned long start, + unsigned long nbits); + +void bitmap_set_value(unsigned long *map, unsigned long nbits, + unsigned long value, unsigned long value_width, + unsigned long start); + +unsigned long find_next_clump(unsigned long *clump, const unsigned long *addr, + unsigned long size, unsigned long offset, + unsigned long clump_size); + +#define find_first_clump(clump, bits, size, clump_size) \ + find_next_clump((clump), (bits), (size), 0, (clump_size)) + +/** + * for_each_set_clump - iterate over bitmap for each clump with set bits + * @start: bit offset to start search and to store the current iteration offset + * @clump: location to store copy of current 8-bit clump + * @bits: bitmap address to base the search on + * @size: bitmap size in number of bits + * @clump_size: clump size in bits + */ +#define for_each_set_clump(start, clump, bits, size, clump_size) \ + for ((start) = find_first_clump(&(clump), (bits), (size), (clump_size)); \ + (start) < (size); \ + (start) = find_next_clump(&(clump), (bits), (size), (start) + (clump_size), (clump_size))) + /* * Return the GPIO number of the passed descriptor relative to its chip */ From patchwork Fri Feb 12 13:21:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Syed Nayyar Waris X-Patchwork-Id: 12085303 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.2 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6CDBC433DB for ; Fri, 12 Feb 2021 13:22:37 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7527264E02 for ; Fri, 12 Feb 2021 13:22:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7527264E02 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Be5NEsBJUhp1nOCtGMCXErMnQNpD5+z1opHklFY/kM8=; b=zwF0FXkqMgVCYtO4JM1QCMroq 1yvvikGdPdqMsEswi+BiIZGUyAyz0Jy1SD9LoITtLq1pS3rc9P37hlQdcADFjEb8R7VTpw0RQJq7n BqNZfLbkTKgbLjhMerMkZPaLxEucX33XdYBhHj4TGxtWn0diCqmh6qj3uY+umOdI/Higk2zxpAcrt AS85uFru3VO1So5M05sHIJgzfyCvbfTOobvt5asIb6D8cD1lK3N3iJb2hJS7xb0RqSuklUhROKfqw DMNR2zfZktMP+bw/eeldpjax6zW9xrm4zsYtqNsnfOqJTWJa5naOnfi/oo66PiT8CPyk3Z3pmVwcj a/hTX4H4A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lAYO3-0002sc-QG; Fri, 12 Feb 2021 13:21:31 +0000 Received: from mail-pj1-x102d.google.com ([2607:f8b0:4864:20::102d]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1lAYO1-0002qG-Bu for linux-arm-kernel@lists.infradead.org; Fri, 12 Feb 2021 13:21:30 +0000 Received: by mail-pj1-x102d.google.com with SMTP id z9so426209pjl.5 for ; Fri, 12 Feb 2021 05:21:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=O/3BA4PWnT2f4RUSGMom+2MnQBkrppTk3RLQijEPW1A=; b=CI5X+Hq+gKpTnweohO4hVWy0QNcS0e/Z1p6h3GGl3SDgUIcmrH2thRXHitu/3yBVx1 w0zKYSoEBxImHtT4h6U8ZqGw+wpkHluw+jlR2oQOBriYBB+9hWC0qYn7bLqyDrVgFrdW C/JXAg+GSJbInwC+1w1spbqz/5Wkhz1PpLj/qbyk9FpFMT92P4FX+reJHzxbGcTv5Ody sgchSZy9OPkf5000p+X/B0o3dSv26eYoR8gfKDUhazlCpzUd58kTi1xRW/5CVOyixkK5 XI1jr+a0XSySYLcHyRCiBrTJ/fmys0THSFrrzIibUfdMyhgOV5k313KrZQSxjhK4P9+t +EMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=O/3BA4PWnT2f4RUSGMom+2MnQBkrppTk3RLQijEPW1A=; b=r8UcL/v5D0lJWT+4TF/2Yh8vc9pP6r2v8/+7rKkTP7i6K12gH8LeC6l+4b/Hro3TFl eWjbNrksFZ35omhWjlmDnBM7SOew6m3Snw/1/Q8Q/N9PPjWf7LQzilKNisK7gqjPAhZD ewgiKJkox3FT4PxNEu/0HqrCEzUhc/Tm6b/3XTXJVxquaHaFbzdnotx97KKPDXauoJCM UoVcuv9dGxHB9IPz1Psloql2yMpiE5I4e0QRNjEFV+m31QS0H1W24zTlAs0I+yJ+iAfd 8spXaAaLaiavC778GWCc69XGmA2MWgnCaKM0pNG7xJP9edqlumUwlnjGKQn8FftgeP0b C69w== X-Gm-Message-State: AOAM531hFWp6cE0FGTvH8MxwIvSzUZCdsXcweEj57fKmOL10Sx4y8zOg SRUxvZaDGKal1vMuVtRlapY= X-Google-Smtp-Source: ABdhPJw9tWvL+iS8ZZKqxM1FZEUFcyB0qqhDeIW1+FeHi3hFhHdeu5m2XqoqQDDmxiVE6lGaF6fFaQ== X-Received: by 2002:a17:90a:8c87:: with SMTP id b7mr2807646pjo.158.1613136086127; Fri, 12 Feb 2021 05:21:26 -0800 (PST) Received: from syed.domain.name ([103.201.127.1]) by smtp.gmail.com with ESMTPSA id 71sm9403499pgh.55.2021.02.12.05.21.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Feb 2021 05:21:25 -0800 (PST) Date: Fri, 12 Feb 2021 18:51:04 +0530 From: Syed Nayyar Waris To: bgolaszewski@baylibre.com Subject: [PATCH v2 2/3] gpio: thunderx: Utilize for_each_set_clump macro Message-ID: <3fc5bd83322c94eb2a4f48677f6d762bf81d0652.1613134924.git.syednwaris@gmail.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210212_082129_458163_CDA23F7F X-CRM114-Status: GOOD ( 15.21 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, amit.kucheria@verdurent.com, arnd@arndb.de, yamada.masahiro@socionext.com, linux-kernel@vger.kernel.org, linus.walleij@linaro.org, daniel.lezcano@linaro.org, vilhelm.gray@gmail.com, michal.simek@xilinx.com, bgolaszewski@baylibre.com, rrichter@marvell.com, linux-gpio@vger.kernel.org, linux-pm@vger.kernel.org, akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, rui.zhang@intel.com, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This patch reimplements the thunderx_gpio_set_multiple function in drivers/gpio/gpio-thunderx.c to use the new for_each_set_clump macro. Instead of looping for each bank in thunderx_gpio_set_multiple function, now we can skip bank which is not set and save cycles. Cc: William Breathitt Gray Cc: Robert Richter Cc: Bartosz Golaszewski Signed-off-by: Syed Nayyar Waris Acked-by: William Breathitt Gray --- drivers/gpio/gpio-thunderx.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/gpio/gpio-thunderx.c b/drivers/gpio/gpio-thunderx.c index 9f66deab46ea..0398b2d2af4b 100644 --- a/drivers/gpio/gpio-thunderx.c +++ b/drivers/gpio/gpio-thunderx.c @@ -16,7 +16,7 @@ #include #include #include - +#include "gpiolib.h" #define GPIO_RX_DAT 0x0 #define GPIO_TX_SET 0x8 @@ -275,12 +275,15 @@ static void thunderx_gpio_set_multiple(struct gpio_chip *chip, unsigned long *bits) { int bank; - u64 set_bits, clear_bits; + unsigned long set_bits, clear_bits, gpio_mask; + unsigned long offset; + struct thunderx_gpio *txgpio = gpiochip_get_data(chip); - for (bank = 0; bank <= chip->ngpio / 64; bank++) { - set_bits = bits[bank] & mask[bank]; - clear_bits = ~bits[bank] & mask[bank]; + for_each_set_clump(offset, gpio_mask, mask, chip->ngpio, 64) { + bank = offset / 64; + set_bits = bits[bank] & gpio_mask; + clear_bits = ~bits[bank] & gpio_mask; writeq(set_bits, txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_TX_SET); writeq(clear_bits, txgpio->register_base + (bank * GPIO_2ND_BANK) + GPIO_TX_CLR); } From patchwork Fri Feb 12 13:22:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Syed Nayyar Waris X-Patchwork-Id: 12085305 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.2 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 86C51C433DB for ; Fri, 12 Feb 2021 13:23:29 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3C92B60295 for ; Fri, 12 Feb 2021 13:23:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3C92B60295 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=zNHEmrLX7c0kFQ7f4y7wKD443F645Ap6oyrDVsaMJ4Y=; b=jA99BpI6QGFl6rk/825wOkQ8A SAujMlxk0sBJash8CPaOG1sMtkwOcPayyOh/uhk8SQAUGmmSrzP+IJCIexPg5vgaTsbFQ13zT7xnc AiO9MZeXkrnjnu2a/N/awzhViMAxhwmd68wZKhmmfzDGQg4R2SyMMa0GJQQKDV6yC9C4Nnl5APJZ+ 9sMMMY6OH44kSJfdTpk9Z0bONFZal/6utmgBqlYGDHC9EeHoUjAhvC6I2l2lKihbma0Z66TVmrPB7 1I1Q7mgws6GWvNH5y7PUAGAvqTnZ/6njYnouv5OQPEGzYMm+7A3wLIKJNjrmVtqH0yyv66CN8kBGg 6HjiM+Y0w==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lAYOr-0003DU-Se; Fri, 12 Feb 2021 13:22:21 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1lAYOn-0003Av-Gh for linux-arm-kernel@lists.infradead.org; Fri, 12 Feb 2021 13:22:18 +0000 Received: by mail-pj1-x1036.google.com with SMTP id lw17so384964pjb.0 for ; Fri, 12 Feb 2021 05:22:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=LlK4oOejkGmVCANiTlUnm8yrZASNqtveudOgthnjqjI=; b=iQSwM5JwXDKLM+rqGqJmII6cCxrZTr/MDyQJcOwWPdUIGET0SX5Dx13XxnWQoAB9ce Yl/C+Dxd75UvNlHjdqdFOvI3d49JZhiBywK3vO9N83sZOlwQU3mLGNoVFiFIMtzr/GHe vlII8OXJn+L2qWA7GV2gpHFjmDN84VXCKXAujllae/ClEjYEuu4ABe9ijORyyc99KJel iAWaJs2XjitDtSwsr1UsCh2gWMmCdk6GpV6sJLpKNDrjJ4LDnhm17LeQGe+6VHoprmpd qB/jEHYVAzgUPn1QREHwoiEfhRO/e2Is8Hpj3lwdSFIJdo12N8xnMPq0/3IQYvMeamib W4HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=LlK4oOejkGmVCANiTlUnm8yrZASNqtveudOgthnjqjI=; b=O+Z9IkgdspSfBe7LYZ3+Sn4iZNRHZAn7TRf0JkbKKTkJlIPnUZSJyUFUtgTF3kELE4 45PfAbwpceIHcJz38tHiLsX4x6Vx7pb0WZ/ppA/oy6LutGjXcbt3xs8F/90OBh0EWL7M lwuguzUARz7AZNBShhMbKgJCtt7rh9nFugDxb4SRdEUQ7IfqAfvZwksJc5oHdQj7nUeG rDjI0uCOhyafs6prfc4XRuRsESEvKu5puhQOlDd4gXrhP4NM+Z9G1CBFWdiQ7RuZe0tV X874Zzwtm8IkfzJ9JXOL9dExofG8wr0qZJxTA+sxAZkUWKNWWDcTp+OcFXR0H6LpAP1S Lc9A== X-Gm-Message-State: AOAM533YrHcfa8V1SToabed5bKRM2wdwm5KCqX29AB1MyOmOis8GFGrK s4vzP8LuWVuXp0eUkkZhhO0= X-Google-Smtp-Source: ABdhPJyV9t8X6mKuwfE8e/aZK1KZFuuyTqGY4IAiBevhX62jo7ymr64zGlqJd1+zqtCK1Yew5apvoQ== X-Received: by 2002:a17:90a:6549:: with SMTP id f9mr2754627pjs.17.1613136135243; Fri, 12 Feb 2021 05:22:15 -0800 (PST) Received: from syed.domain.name ([103.201.127.1]) by smtp.gmail.com with ESMTPSA id q43sm5591021pjq.25.2021.02.12.05.22.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Feb 2021 05:22:15 -0800 (PST) Date: Fri, 12 Feb 2021 18:52:00 +0530 From: Syed Nayyar Waris To: bgolaszewski@baylibre.com Subject: [PATCH v2 3/3] gpio: xilinx: Utilize generic bitmap_get_value and _set_value Message-ID: <1b1f706b60e4c571c4f17d53ac640e8bd8384856.1613134924.git.syednwaris@gmail.com> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210212_082217_615655_6F136014 X-CRM114-Status: GOOD ( 17.75 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arch@vger.kernel.org, amit.kucheria@verdurent.com, arnd@arndb.de, yamada.masahiro@socionext.com, linux-kernel@vger.kernel.org, linus.walleij@linaro.org, daniel.lezcano@linaro.org, vilhelm.gray@gmail.com, michal.simek@xilinx.com, bgolaszewski@baylibre.com, rrichter@marvell.com, linux-gpio@vger.kernel.org, linux-pm@vger.kernel.org, akpm@linux-foundation.org, andriy.shevchenko@linux.intel.com, rui.zhang@intel.com, linux-arm-kernel@lists.infradead.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org This patch reimplements the xgpio_set_multiple() function in drivers/gpio/gpio-xilinx.c to use the new generic functions: bitmap_get_value() and bitmap_set_value(). The code is now simpler to read and understand. Moreover, instead of looping for each bit in xgpio_set_multiple() function, now we can check each channel at a time and save cycles. Cc: William Breathitt Gray Cc: Bartosz Golaszewski Cc: Michal Simek Signed-off-by: Syed Nayyar Waris Acked-by: William Breathitt Gray --- drivers/gpio/gpio-xilinx.c | 63 +++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/drivers/gpio/gpio-xilinx.c b/drivers/gpio/gpio-xilinx.c index be539381fd82..8445e69cf37b 100644 --- a/drivers/gpio/gpio-xilinx.c +++ b/drivers/gpio/gpio-xilinx.c @@ -15,6 +15,7 @@ #include #include #include +#include "gpiolib.h" /* Register Offset Definitions */ #define XGPIO_DATA_OFFSET (0x0) /* Data register */ @@ -141,37 +142,37 @@ static void xgpio_set_multiple(struct gpio_chip *gc, unsigned long *mask, { unsigned long flags; struct xgpio_instance *chip = gpiochip_get_data(gc); - int index = xgpio_index(chip, 0); - int offset, i; - - spin_lock_irqsave(&chip->gpio_lock[index], flags); - - /* Write to GPIO signals */ - for (i = 0; i < gc->ngpio; i++) { - if (*mask == 0) - break; - /* Once finished with an index write it out to the register */ - if (index != xgpio_index(chip, i)) { - xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET + - index * XGPIO_CHANNEL_OFFSET, - chip->gpio_state[index]); - spin_unlock_irqrestore(&chip->gpio_lock[index], flags); - index = xgpio_index(chip, i); - spin_lock_irqsave(&chip->gpio_lock[index], flags); - } - if (__test_and_clear_bit(i, mask)) { - offset = xgpio_offset(chip, i); - if (test_bit(i, bits)) - chip->gpio_state[index] |= BIT(offset); - else - chip->gpio_state[index] &= ~BIT(offset); - } - } - - xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET + - index * XGPIO_CHANNEL_OFFSET, chip->gpio_state[index]); - - spin_unlock_irqrestore(&chip->gpio_lock[index], flags); + u32 *const state = chip->gpio_state; + unsigned int *const width = chip->gpio_width; + + DECLARE_BITMAP(old, 64); + DECLARE_BITMAP(new, 64); + DECLARE_BITMAP(changed, 64); + + spin_lock_irqsave(&chip->gpio_lock[0], flags); + spin_lock(&chip->gpio_lock[1]); + + bitmap_set_value(old, 64, state[0], width[0], 0); + bitmap_set_value(old, 64, state[1], width[1], width[0]); + bitmap_replace(new, old, bits, mask, gc->ngpio); + + bitmap_set_value(old, 64, state[0], 32, 0); + bitmap_set_value(old, 64, state[1], 32, 32); + state[0] = bitmap_get_value(new, 0, width[0]); + state[1] = bitmap_get_value(new, width[0], width[1]); + bitmap_set_value(new, 64, state[0], 32, 0); + bitmap_set_value(new, 64, state[1], 32, 32); + bitmap_xor(changed, old, new, 64); + + if (((u32 *)changed)[0]) + xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET, + state[0]); + if (((u32 *)changed)[1]) + xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET + + XGPIO_CHANNEL_OFFSET, state[1]); + + spin_unlock(&chip->gpio_lock[1]); + spin_unlock_irqrestore(&chip->gpio_lock[0], flags); } /**