From patchwork Fri Apr 2 16:36:14 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: 12181315 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 B5BFEC433B4 for ; Fri, 2 Apr 2021 16:38:24 +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 21C9161106 for ; Fri, 2 Apr 2021 16:38:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 21C9161106 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=XfKJfTVhjooxSm7c9/M0r7WcE gr6YundypxPUdPPSAorh9nijY01cd88x/ztRpPdIh84piWXG3QZbzbcO889Qr+TZY2gRK5fZJThXL LNXD+KoEoORWtrcm73xKcTfxP96bsa9UpY9+CaC+p6NmUjR6bjk12KdjU1coBcXvhtQdiVHfoQDe6 JfPwKvh5AyaPnaU6ZeCxhDcWFM7wPWJYxi/yo+61wthUaixgqeNNkM9BQrefdO7H7rV8MdYfsa/Ue AwaSyGkOeSePAovBcf1EebicluwviBk9txYHGJRhP0UGyJdZUdTCpTw7+JxzUGq8RK11t0KHgSrs7 z9/qvPmpQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lSMmk-00DOpV-Vw; Fri, 02 Apr 2021 16:36:39 +0000 Received: from mail-pj1-x1032.google.com ([2607:f8b0:4864:20::1032]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lSMme-00DOoS-LB for linux-arm-kernel@lists.infradead.org; Fri, 02 Apr 2021 16:36:34 +0000 Received: by mail-pj1-x1032.google.com with SMTP id s21so2939347pjq.1 for ; Fri, 02 Apr 2021 09:36:32 -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=WVKXH08lo+mqN07StvGegOHxdQZcUNV35cDwhDJQtll/QCwKno68Z5a4fmHQh4fb70 xURek2O+oiRCGeR0GqS1Eu69+P8owxykMTeKYDtYwrtCcjHhsfGwMP7/F9rvJQNnxEUf xW8vsbWaSDcAbc6olGPv4WGAbHDuXI68qP4ZeFnAdzrz00FcCIxPFJMtHF0s6DO9OiqM MN4hbGdfIo8mJYs60Cvp2v4zB3qxjCrfaYc3Tew9M84QSe6hGjNvaAP2/dcFrLU1G7Ty B0ObJDx0KXRVEw71Rv0FI2tYHsJGoXsA5vsdQmI9Uo7Mtjt9hMFSb0jbLtbuTXKISvUz kPDA== 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=WkXpVBgXw7DF8u3nQ4U3pMFOQ5ZOX7/W4MfeT/ID3dGlg/bbBnviqVn5C5jxJ4D5kZ hsgTyL0aB3Ba6219pkeqBWE8TerpQztpnZRlG5M3NXkxC+bPA+JaPKqv4H5lnXDhlpRI 6UqbTqOTaP146f7N+bqNF9SSLLRxGdWg10Luei/faxi2aZ1jOAc8RRn0ylcY4PkeOP30 jwvRDORwbiRL3Ww37Ni7+Ys6lDFcjO3w0NABMtYehoz7HddLVdq4t9Cqao3hDbIauXme b+y+Ezt62h+MrH7BrQoQAilxKfTX3UV+NH1Xjjf96TFvxh+lZXU086OOVwUq/0Fu/Lsq 9pBQ== X-Gm-Message-State: AOAM5303PD7FdteXjb2Sb1+iQP57AqxYTos4rZGddMEkN/wpy5ghqQ52 JT3eF6Dis3Gx660wYYixF7w= X-Google-Smtp-Source: ABdhPJzh8UlXUoWIyn2bKWSqPS0Vwhytdxdj2cY+JYBqKtpf+XkvAEs2/n1PGBMIQ1touT07P10OEQ== X-Received: by 2002:a17:902:7585:b029:e6:cc10:61fe with SMTP id j5-20020a1709027585b02900e6cc1061femr13379285pll.23.1617381391211; Fri, 02 Apr 2021 09:36:31 -0700 (PDT) Received: from syed ([223.225.109.149]) by smtp.gmail.com with ESMTPSA id x22sm8356885pfa.24.2021.04.02.09.36.24 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Apr 2021 09:36:30 -0700 (PDT) Date: Fri, 2 Apr 2021 22:06:14 +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: [RESEND PATCH v4 1/3] gpiolib: Introduce the for_each_set_nbits macro Message-ID: <12102d94258fc6bd5c01800b7cea680fc51d8b93.1617380819.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_173632_844344_62B22DDE X-CRM114-Status: GOOD ( 23.24 ) 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 16:36:48 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: 12181325 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 BC3E8C433ED for ; Fri, 2 Apr 2021 16:38:43 +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 5CC6961155 for ; Fri, 2 Apr 2021 16:38:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5CC6961155 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=ojm4VVfEnxcbP36IWsokh5G+f TvGAu84cIXvQTRi10weftpWi4VoNbZYqqxrHFPyeLFBoo5HQDN0To2EVel/zPcgRpttrirRMMNH9l 7Mvm+NaqmXbYTQoL42+MrTcAfb7mjV95OZQpv+jAOyfd4XgAN/Ot5kOunFw+LHPL0u/4vtFP6eHRV Y/b+NgCQmF2Dk50Pt3/+j9n6u4Gs7XubYPXYzvUeOT0+833ha8T0XJUDrPFMHbCiUz+KN+e0U2eAK Bg6bU6bpZzsXTLwSS4xX/d5dP25CTf5A0Uq1QHVYzjxnnfq8kavT23DtpXYllAADj0APO8AT+5OT1 YQVLEXI/Q==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lSMnH-00DOwC-CV; Fri, 02 Apr 2021 16:37:11 +0000 Received: from mail-pl1-x633.google.com ([2607:f8b0:4864:20::633]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lSMnC-00DOul-Ec for linux-arm-kernel@lists.infradead.org; Fri, 02 Apr 2021 16:37:08 +0000 Received: by mail-pl1-x633.google.com with SMTP id e14so2748475plj.2 for ; Fri, 02 Apr 2021 09:37:06 -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=rniiW33V+k97vWR8A63gBXFhFoayK+VhrSPyN1QPh9Q05cqI9iknScGDIOHhzW37Q3 E9Yeo9XdchviKP8uebuKKVt6GzcbpKPZ325d3bjBTCpPdvbGtgZPVghMQNxYDEW/wsAr JRtIPcxsd9klpDnHOdoxgmSv2jYQEJaSa1txLdmw3oyj+gLMyJojW8ltMvoKHWB4yjIb WzuZMPiVA6tDbreMXiSoHzko92sLTCW3oKyE3A59LT2zBCBH3dItDq8zS5ZH+kitDXle JYAx/1dLyBGTl0mP/EPhQbsrcx6tl14JBk2PPq636maGraNQ/llDWmpeDKMswZ/xCTqU 5zKw== 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=SEOhnLnZ9lHmTAcKAPv8NIS4O6FwQORnesefDl3yC+PR8Vtxj/xY710qUDtsZzAVKv WseD/vAXeqjqU8qOZ18zJkYolAdyjiMSz59bpEUcsm2ufkWBfm3HSD3CFgQ48MrZRnKo A1dWy55shvDhqIdA7oH3RAKmJ7Mk8DUDx3j5Hz1X1WZ+RR1Zv8p23AxYIBgeDPzjAR0o ZA94ulA0JsCY6Dy6BUcaALbo9DdvJ4VpH+UY+zdutS6OpaB5Ri+4sHelaeKsaibkkfq0 tCpp+tP03MVN2okn4umIqCzxyJ6uTNbdC9HB0oJAavdMxtcPsbbb+nB2uVNBHeKAlC0O dOuA== X-Gm-Message-State: AOAM533mJSy6n5apWXNtlcyIDBzFQ7qYwDbdCVvuvTuqoCvCf9/IyDa8 Qx2iA6jJMxCTloCPNq2BPvYKFGbOhhvyWA== X-Google-Smtp-Source: ABdhPJz1n7/WaQ0+zG3NLK93leEDb2jZHXIhU7HWtvH1pbg2UjdBAdvabxAjuykg5CqXPqw5r+36CA== X-Received: by 2002:a17:90a:a613:: with SMTP id c19mr14848896pjq.141.1617381425020; Fri, 02 Apr 2021 09:37:05 -0700 (PDT) Received: from syed ([223.225.109.149]) by smtp.gmail.com with ESMTPSA id 23sm8698123pgo.53.2021.04.02.09.36.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Apr 2021 09:37:04 -0700 (PDT) Date: Fri, 2 Apr 2021 22:06:48 +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: [RESEND PATCH v4 2/3] gpio: thunderx: Utilize for_each_set_nbits macro Message-ID: <3a9bd0a593798538bec54d8b5ceab20a73d7eaa1.1617380819.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_173706_706696_91AC37DD X-CRM114-Status: GOOD ( 15.58 ) 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 16:37:36 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: 12181327 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 CA7B0C433ED for ; Fri, 2 Apr 2021 16:39:33 +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 5FC1961155 for ; Fri, 2 Apr 2021 16:39:33 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5FC1961155 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=Wsb/Eq/OOfJoFkwil+uV1ol/tgsPeht2iB81CmU30d0=; b=OWGGQCusA6rJw0Ii4gcfydMUN HoAjIJTiOkNKwfRPrD2lsBkEdXE/TcqEIZ3RwGVFES+yabfrvSjkqqbkZWiy13IWgXBO5emZXWjjW lZCZNRJ0uyvziLpaXsJliWoo5yLCI52jhzlrjymEBMtuVfkOunE0oUNWajeOb049Was8h2UTEl3zl veEdFK+E/uw+9CL5QDoC1JcfJjOkcdWDpQHmlTSsud7u705YMahguNHymw/A2oeCT6snb+hTK9qD2 KOX2aLfqtvhSXPbHKnZpK0TvbYvTYUj6dskaYeWMn34rdV+fGLfu0C7WX0XUwwgxGvJgU3T6C311b 8T7S1sOjw==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lSMo2-00DP5F-Tm; Fri, 02 Apr 2021 16:37:59 +0000 Received: from mail-pj1-x1036.google.com ([2607:f8b0:4864:20::1036]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lSMnx-00DP3n-OA for linux-arm-kernel@lists.infradead.org; Fri, 02 Apr 2021 16:37:55 +0000 Received: by mail-pj1-x1036.google.com with SMTP id kk2-20020a17090b4a02b02900c777aa746fso2795034pjb.3 for ; Fri, 02 Apr 2021 09:37:53 -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=y8eBCCmlCiBAb49e1Y44XUdLGF33+F00lNn7YXoNQhY=; b=KqSpFXxoAFj8zHVyLFT5IkMX4zwL3c8hnpc9JsakNm1CZF9IM3UGDbEnWaMcb2mMmo Q/MWeiiRAzvfoWInmPS1blTIipL/ScTf4jC244bntda/aGUjxfJ0EpO0Ny1zzhooAcaH mq43vWBksxacKe8s7/I16HSHsuDik6MLfmRa+qTlZOcdYSy9QY1kAxDpoJSu/Neml0XY jBYsELkh3XAYBRveHCc4Yjy7zJwYieQmgKALjbSO/8YH3Tc68y1xL8r2iPqUwDUehANo HWMksLHa8GKauJpUdqdqWDsxmcX4u/dWfKgDiMhlk6h19f+Ojcaii/OLE24awJXjNcdL ktmA== 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=y8eBCCmlCiBAb49e1Y44XUdLGF33+F00lNn7YXoNQhY=; b=KK15g/Hp3PBLWNpSO7WG92k7oJEq5gXmet+V+QFGmYlwBWQULyJvEjDwCSQ9jz7wyR Acm4huEcoMzm+DzJWlJCDdrhhS7YzDSHZsqm8/AFe0xaeTZE62Nn+f279zfjYYiQ/yZd LaJVFrp/ii72P4Fdb10VT3MR/2w/1Z6MkvJGt4TAbe2kTn51GsKubwW7GUjtuza5qtz2 XvYAqEJbnDN6N1lmqMhGa9IawGwdaQGGyB0WI80YnTqHl3zJiPS0qbgzmzEsrMhrkXGQ +3EIrCyE7EKoULsmdyKiOu06jmiB9oo2wlOkpNEl2yxNdE2hdW7SAYHSKoVTChAzVTrp cblg== X-Gm-Message-State: AOAM530qLpBqhqRMb5UCSGYgkunNVTe2xxCBMvJ72t8W/5f2uRZdn5c3 ZLp23aMx5fozGqInAvgLiow= X-Google-Smtp-Source: ABdhPJyGQMF+MUGjKiku6ifz1SggVMS5kG1l6WlgiEaxFGfXZfiER05E3luxOPCRSpCAxle6KdWV1w== X-Received: by 2002:a17:902:7b90:b029:e6:f01d:9db2 with SMTP id w16-20020a1709027b90b02900e6f01d9db2mr13415174pll.69.1617381472160; Fri, 02 Apr 2021 09:37:52 -0700 (PDT) Received: from syed ([223.225.109.149]) by smtp.gmail.com with ESMTPSA id g10sm8352570pfh.212.2021.04.02.09.37.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 02 Apr 2021 09:37:51 -0700 (PDT) Date: Fri, 2 Apr 2021 22:07:36 +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: [RESEND PATCH v4 3/3] gpio: xilinx: Utilize generic bitmap_get_value and _set_value 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-20210402_173753_813190_C59D1711 X-CRM114-Status: GOOD ( 19.63 ) 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 | 52 +++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/drivers/gpio/gpio-xilinx.c b/drivers/gpio/gpio-xilinx.c index b411d3156e0b..512198250b02 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,35 +162,34 @@ 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); + u32 *state = chip->gpio_state; + unsigned int *width = chip->gpio_width; + DECLARE_BITMAP(old, 64); + DECLARE_BITMAP(new, 64); + DECLARE_BITMAP(changed, 64); - /* 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); - } - } + spin_lock_irqsave(&chip->gpio_lock, flags); - xgpio_writereg(chip->regs + XGPIO_DATA_OFFSET + - index * XGPIO_CHANNEL_OFFSET, chip->gpio_state[index]); + /* Copy initial value of state bits into 'old' contiguously */ + 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); }