From patchwork Fri Apr 2 10:11:24 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: 12180747 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,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 E7D0AC433ED for ; Fri, 2 Apr 2021 10:22:14 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 0AC7D61057 for ; Fri, 2 Apr 2021 10:22:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0AC7D61057 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=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc: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=91TDRjRZ8C+At/CO1D6rLw/0c5lHFS5EJeCvhWui8UI=; b=iWQEPpLdAPEbl62f60WbPKlkM 5byfPCEkIQMPtKlxKZnQFtvAujr/VQSRG6yaAeoYunz1n2MBMiv/Dp5o4SPZjq2nsKoR3Y1NUmIIt hzh8X5lmLaQrSMD4CmKbeFIcrvshk7my/Suon8RUVLM334IMprRw6dVg+9TVGlmoP077i9zx9HHnr 76/iSIT4/9DM+y+8WIipjM4nn0JZFWDneX+iT+nPgdQPCSaJnviAuhQvvvVqFqp7rq4WxJRDX8kkn g8sTMGxXH6M7WdU09fbPLpmTnnlruYPZ33QtVDtAC6EYjKq6Qpw7r1odklKkdv2tEnLIVOxfdIwxF AiqCt0F8A==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lSGtx-00Cj1K-4d; Fri, 02 Apr 2021 10:19:43 +0000 Received: from mail-pf1-x42e.google.com ([2607:f8b0:4864:20::42e]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lSGmG-00Chbp-1B for linux-arm-kernel@lists.infradead.org; Fri, 02 Apr 2021 10:11:57 +0000 Received: by mail-pf1-x42e.google.com with SMTP id c17so3323162pfn.6 for ; Fri, 02 Apr 2021 03:11:42 -0700 (PDT) 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=uINt+NN92c/qqfcZODFiV431lREP2XSc10cIb3wY4cc=; b=l/fmTCDn13mLbCThcvC6o8U9EAGLpx5ASG68btJFTg+HocJJ0kP0nOt6taopeYMDB3 RdKOdDANC483leCBktWTrtX1VNOtbddTw1Oi+7qB/LczPzyHH4RBeSbVxqIdL16Tc4yt /RJpofJnpA5wgO3NecEYKlQi+jWHONp/cNM4AblrELIXeb4riFUc//slAfbiNU4BoLqN UajRyBO5G1qXQYOoXE0xA2NTyzHS9CsLRDy8OP8dmz8ddEFH+4PtD4HtYbdM3EKpzwFl ms1Tl3FWYdxTQmNUXoamORAZ8npktqBXeMVQTOASVOWAQBeUVyxYXgNAuZVWLZ4cho+T yxig== 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=uINt+NN92c/qqfcZODFiV431lREP2XSc10cIb3wY4cc=; b=M3SYzm1RWy+KHVZQ19gg6GT9aK6HV+QSmUFX+0+vs8Ce1BDukZce69EXjQPMw6LuNK rG78HUlkM7wBjdrP+2A2q/UmyDClxi4/jbZxMq2ghHi2FjnU97eo4s4o3Yk+iKsPdYQO 7IrIaqVInj2BZGFjR8eVjCKAr01LycvJAPZz958Tj6J+JSEq/6RO/RUGQeINZV7OvV41 /DORlBcUeLKa+ZHWUM64q7YL2g6u6sce5IBNAUteg8oEbkqpGTzngBxiiEVJxA1L0HtS bMdvN5numHdt2hqVDiiUjKkx5gfwzGGE+tpMrI5JGzftwh8YjfX/Su34tM7Aq9I6SBJu 8K2A== X-Gm-Message-State: AOAM532v0R00P05adtNoVGqj6zkZyiWlnYBPccneZ6goS/67eLBgHSAp VWtC61CRvZEZYKgUAIp+T2Q= X-Google-Smtp-Source: ABdhPJwYOkp3h/5YYuq7wFH6wtT41RcO8LQl85nhoCS0ohhE7PHiPkpZ9rNp9iCbGWqgyHPiyvbWzA== X-Received: by 2002:a62:6883:0:b029:220:4426:449c with SMTP id d125-20020a6268830000b02902204426449cmr11368828pfc.14.1617358301687; Fri, 02 Apr 2021 03:11:41 -0700 (PDT) Received: from syed ([223.225.111.29]) by smtp.gmail.com with ESMTPSA id v13sm7326337pfu.54.2021.04.02.03.11.35 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Apr 2021 03:11:41 -0700 (PDT) Date: Fri, 2 Apr 2021 15:41:24 +0530 From: Syed Nayyar Waris To: bgolaszewski@baylibre.com Cc: andriy.shevchenko@linux.intel.com, vilhelm.gray@gmail.com, michal.simek@xilinx.com, arnd@arndb.de, rrichter@marvell.com, linus.walleij@linaro.org, bgolaszewski@baylibre.com, yamada.masahiro@socionext.com, akpm@linux-foundation.org, rui.zhang@intel.com, daniel.lezcano@linaro.org, amit.kucheria@verdurent.com, linux-arch@vger.kernel.org, linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org Subject: [PATCH v4 1/3] gpiolib: Introduce the for_each_set_nbits macro Message-ID: <12102d94258fc6bd5c01800b7cea680fc51d8b93.1617357235.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-20210402_111144_400797_A9ABDBCA X-CRM114-Status: GOOD ( 23.28 ) 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 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: 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 1427c1be749b..5576d1465c81 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -150,6 +150,96 @@ struct gpio_desc *gpiochip_get_desc(struct gpio_chip *gc, } EXPORT_SYMBOL_GPL(gpiochip_get_desc); +/** + * 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); + /** * desc_to_gpio - convert a GPIO descriptor to the integer namespace * @desc: GPIO descriptor diff --git a/drivers/gpio/gpiolib.h b/drivers/gpio/gpiolib.h index 30bc3f80f83e..af79784dfce3 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_nbits - 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_nbits(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 Apr 2 10:12:06 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: 12180749 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,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 72D72C433ED for ; Fri, 2 Apr 2021 10:24:09 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 5EAC8610E7 for ; Fri, 2 Apr 2021 10:24:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5EAC8610E7 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=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc: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=NzUwajGbAmDIMMXTcVDQHa9tlOxFWTn1yiuXA+/pc9I=; b=FrCSGDLJKZ0R8goTPRl8hG0vc gXsvTCiBDLVzcUlG511ZYcArcyfK2CrV72448Da3QxljDsx+VjpdHTER/qbbubgsg7nBGluTR5DF/ mt8IOPSUxyD7P8Ws7LYbYnjrMottXK11WOct1+EzlHWVE5XpfsMfWMVuHQpl0otqzoff1vS06wMpD Sdw+x/PxFaQKdl1qiDw8bV9lQcHXU8k0Pc6QXKuoXSgKQeoDNDg8M4/LMN6dIOESECKNPNdNU3Oy8 hAcczU64CFgD234KuTirHLNggHbtsU4A0hXiVvZbBmhBZMwFoMF/NV/Rbl+9CMC/C2SA1Z6kwDDG7 aFL02om2Q==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lSGvb-00CjIU-Bo; Fri, 02 Apr 2021 10:21:24 +0000 Received: from mail-pf1-x42d.google.com ([2607:f8b0:4864:20::42d]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lSGmu-00Chmg-IG for linux-arm-kernel@lists.infradead.org; Fri, 02 Apr 2021 10:12:30 +0000 Received: by mail-pf1-x42d.google.com with SMTP id c204so3334257pfc.4 for ; Fri, 02 Apr 2021 03:12:23 -0700 (PDT) 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=pjJTs/trNbCfk/Wd5AeG0sctmtT3vopPWM58p/JWi08=; b=X0pl6oiDGAB8riJRc8TdXBwVXf3oZNvOJwhTa2W7pA6M5M9hyfmX2bdbKbwz3pblYH IYZg697aXp3DpAZFoRpEwUwnG4Ag90KNmwmeUStf6J9wmarh7MIONtvf1++OG7p/D7JQ vHyoAHEujja+kgxFzY7F8rpzz/L+xoNeoT4okBQ8ARmhePe0AW0pahn3rGDhiHWW1FTW 7ArCZ4+c3aZn6tz5iajWiKbQC0bX/UUDgt8X8asCIgEhk7QA4Fapo+SASiuOZj+hAU79 E/RLY4srtg9kbXu4wHonooy57C3VgAQVj3IZLjA1ySSBcQRCXQ5L/hgzGa6aWDUkWF8Q glGA== 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=pjJTs/trNbCfk/Wd5AeG0sctmtT3vopPWM58p/JWi08=; b=tUbeaJaH6xMoqref7dyHsB0kBHVXyxNDI5JmxKDpqbAK/SbxQWPYZwcwYjy8zh5ykl IImV77oFc50w5IDMSNYCVSwkJLNfshn3vNK27d5lk51ULU799NNVl3JGUNKEoL8LCV+L eOqP5SafHZ/7I2uoZf8YL68R2V1AFoYs5R7Eu3AOgE9dos4EsuFRozumqndvujGXvljL p/QI1Cg72yN/2JKR+Ye5HCGoAek0i5Wge8M/UzrU5mryPT+giNVSRapfl3SPc3tmAN6r DmhxOsU8W3g+qoNyLnBE021HUNE4FsYuKLLsS2p71lirpP4sGc3hVQ4CPzMJc7PsfKli u8fA== X-Gm-Message-State: AOAM531sBgMFl0jqY4LdynaBGPMlmhNsTj/oFNYHks0GaCvzboB6ijMC JcipPQtDGZ/m79Pr8rOD9HE= X-Google-Smtp-Source: ABdhPJw8RJb7DEr9GjqHs0QTLVJduTr4bs7vTfx3FKXKyLa3wkOCb2XxgyvGtFl04jQnovTx3jM9Cw== X-Received: by 2002:a63:f542:: with SMTP id e2mr11371560pgk.338.1617358342446; Fri, 02 Apr 2021 03:12:22 -0700 (PDT) Received: from syed ([223.225.111.29]) by smtp.gmail.com with ESMTPSA id b84sm8051524pfb.162.2021.04.02.03.12.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Apr 2021 03:12:22 -0700 (PDT) Date: Fri, 2 Apr 2021 15:42:06 +0530 From: Syed Nayyar Waris To: bgolaszewski@baylibre.com Cc: andriy.shevchenko@linux.intel.com, vilhelm.gray@gmail.com, michal.simek@xilinx.com, arnd@arndb.de, rrichter@marvell.com, linus.walleij@linaro.org, bgolaszewski@baylibre.com, yamada.masahiro@socionext.com, akpm@linux-foundation.org, rui.zhang@intel.com, daniel.lezcano@linaro.org, amit.kucheria@verdurent.com, linux-arch@vger.kernel.org, linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org Subject: [PATCH v4 2/3] gpio: thunderx: Utilize for_each_set_nbits macro Message-ID: <3a9bd0a593798538bec54d8b5ceab20a73d7eaa1.1617357235.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-20210402_111225_841218_39C020FE X-CRM114-Status: GOOD ( 15.59 ) 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 This patch reimplements the thunderx_gpio_set_multiple function in drivers/gpio/gpio-thunderx.c to use the new for_each_set_nbits 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: 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..4349e7393a1d 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_nbits(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 Apr 2 10:12:40 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: 12180751 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,USER_AGENT_SANE_1 autolearn=unavailable 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 8CFCFC433ED for ; Fri, 2 Apr 2021 10:26:05 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (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 C4ED2610E7 for ; Fri, 2 Apr 2021 10:26:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C4ED2610E7 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=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc: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=7DE0KC8gh8ZblUYSNFQs6MJMdvorRZvygnqhSdEWpsA=; b=JWouY7mJ3PKbP+w+Df8dItLYQ BRoe4VLJqjqO+9tw7G7IL0dMC6KevKlHkVRPmprFU401dW1RsmGS6FF4aIdnhs+U4POpHDHTglfGN d2WMta84tqCdbTH80TvR8/LW+IfJD2EPDmh7EVrVCAVQcuVI3x4v+zqd0LCxu1X9PEzIpVyHBlUx8 S6SdS2g4ks/lGWbS/huiqU2eZEZ4q+juUXjIxGJYbtRbD0/qh5ngtfNgq4HHF0kO80eEKZWOzbAdl q3P3O78gcApz1y4ok1hw675Cmv2Py8QnciRc0vUaxN/V9c6XMjhV1w/v5s8DsLKZMGSSgQ7K3kKd4 aTq9Jfm6Q==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lSGxc-00CjZx-AM; Fri, 02 Apr 2021 10:23:29 +0000 Received: from mail-pl1-x635.google.com ([2607:f8b0:4864:20::635]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lSGnV-00ChzJ-Mu for linux-arm-kernel@lists.infradead.org; Fri, 02 Apr 2021 10:13:11 +0000 Received: by mail-pl1-x635.google.com with SMTP id v8so2323313plz.10 for ; Fri, 02 Apr 2021 03:12:59 -0700 (PDT) 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=elvUwGrM418CXLIilNL/fwznJq0Mnf7UxWCgH8HJ9kg=; b=bxdBUcsc7T9mo2Lnd8uMQDUsshtUfkbRETbQsHPYB42RrNZ/m6ZOlhgozDLroTZYrN eON3MNfx2OHeuXOJ9Y0YosiPoGZ4eTRY0qPUvRXgcO2YAm64TkPJGRpj/ZP+IcFsKeHD hdYliySd+aDZPAVFw13NX0eJV4O8AR8edPEiM56usn6f5njihh4NOYdzH/AQprkH5VfK +9oDbjJEVymVdL03W10jK2IQtcd3wRhPVZdHwqbzwhbSE7wcJFaamH2iaB+U8agkvHuU SA94JWSWRiQKj+r0Hp1lRaU7pRxo1w36l3v6UFmsUfDBaeMO9Afp/l10N45s+Un3G9bX NZiQ== 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=elvUwGrM418CXLIilNL/fwznJq0Mnf7UxWCgH8HJ9kg=; b=m+j9O0ElR4GuosAFy3anrbKeUI2tVJsRQWzYAGsfN0AmriMeyOZTs3GfCN8eZGnr0K vEq64efMe0fKbq7sh9oeIC28xKWSUBcWXLW7Z9I2exKIYctBVbe2Lj8F+e08q4eK+c+p X5/qG5iz/1C5aJSLmlEY4pVhEvrt4RBJI/3Hw2fWWe3RXhpMn/IevFnvowrZQkk91YNj qNmOPzJLXzET3b1uQffp5A8biewvG97sK0UK1/3ntCwxCuV+fE6aPcoHZTDds4VlI7vU lcOVLvFeDQ3XvMPX+9ur3xr2aswitbV3kRKhpqxcnnn3Oee0XWadUzOrJ3FLz2BHVaUI jeeg== X-Gm-Message-State: AOAM533/ZSzVNwDDdwVtCO+qFy4LmAUfvz5K8GaYjdIbdvjBo8Y4TQN3 G07I83b/EaWhEER9RoYq3xo= X-Google-Smtp-Source: ABdhPJylA1WmAAVPuY/l/zHcqiwsL0HAoW7BKAkydfxcDxaVE/yXGSiIi6gPAbUvQDDTz+K36uZDUg== X-Received: by 2002:a17:902:b602:b029:e6:cabb:10b9 with SMTP id b2-20020a170902b602b02900e6cabb10b9mr11967011pls.47.1617358378281; Fri, 02 Apr 2021 03:12:58 -0700 (PDT) Received: from syed ([223.225.111.29]) by smtp.gmail.com with ESMTPSA id a18sm8419755pfa.18.2021.04.02.03.12.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Apr 2021 03:12:58 -0700 (PDT) Date: Fri, 2 Apr 2021 15:42:40 +0530 From: Syed Nayyar Waris To: bgolaszewski@baylibre.com Cc: andriy.shevchenko@linux.intel.com, vilhelm.gray@gmail.com, michal.simek@xilinx.com, arnd@arndb.de, rrichter@marvell.com, linus.walleij@linaro.org, bgolaszewski@baylibre.com, yamada.masahiro@socionext.com, akpm@linux-foundation.org, rui.zhang@intel.com, daniel.lezcano@linaro.org, amit.kucheria@verdurent.com, linux-arch@vger.kernel.org, linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org Subject: [PATCH v4 3/3] gpio: xilinx: Utilize generic bitmap_get_value and _set_value Message-ID: <00d085d4068be651c58a61564926d4f3d495ab80.1617357235.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-20210402_111302_881397_43CA83FC X-CRM114-Status: GOOD ( 19.53 ) 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 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: Bartosz Golaszewski Cc: Michal Simek Signed-off-by: Syed Nayyar Waris Acked-by: William Breathitt Gray --- drivers/gpio/gpio-xilinx.c | 60 +++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/drivers/gpio/gpio-xilinx.c b/drivers/gpio/gpio-xilinx.c index b411d3156e0b..e0ad3a81f216 100644 --- a/drivers/gpio/gpio-xilinx.c +++ b/drivers/gpio/gpio-xilinx.c @@ -18,6 +18,7 @@ #include #include #include +#include "gpiolib.h" /* Register Offset Definitions */ #define XGPIO_DATA_OFFSET (0x0) /* Data register */ @@ -161,37 +162,36 @@ 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, 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, flags); - index = xgpio_index(chip, i); - spin_lock_irqsave(&chip->gpio_lock, 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, flags); + u32 *state = chip->gpio_state; + unsigned int *width = chip->gpio_width; + DECLARE_BITMAP(old, 64); + DECLARE_BITMAP(new, 64); + DECLARE_BITMAP(changed, 64); + + spin_lock_irqsave(&chip->gpio_lock, flags); + + /* Copy initial value of state bits into 'old' bit-wise */ + bitmap_set_value(old, 64, state[0], width[0], 0); + bitmap_set_value(old, 64, state[1], width[1], width[0]); + /* Copy value from 'old' into 'new' with mask applied */ + bitmap_replace(new, old, bits, mask, gc->ngpio); + + bitmap_from_arr32(old, state, 64); + /* Update 'state' */ + state[0] = bitmap_get_value(new, 0, width[0]); + state[1] = bitmap_get_value(new, width[0], width[1]); + bitmap_from_arr32(new, state, 64); + /* XOR operation sets only changed bits */ + 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_irqrestore(&chip->gpio_lock, flags); } /**