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 X-Spam-Level: X-Spam-Status: No, score=-10.7 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 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 20348C433E0 for ; Sun, 14 Feb 2021 06:46:20 +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 BD868601FA for ; Sun, 14 Feb 2021 06:46:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BD868601FA 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-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-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=p2VuAFgZayyMLbydAqUy4t+FkPx6AhCapTCcXRXnOd4=; b=GgGToY1sIRUi8FxuHyomXB/DG +FJsVa525XGu/6P6PfCDo9pvuVwalKUtNSjmqhgbQYGx5KGgaLan4zP9AczHIHIXkMGyxI8S9Zs8a H4rNbjS00TdX9MExkOmmh8aC6TenNxw80BRCIpLNvk3T2wVtyTgKGlTdeR5e8SmjQo3NXh5j/6OKh FFANWl4fNPUje1YsWK/WNxCV1b/QiQQ5UceNx4DqSMfiQt2rE//1p0mS1QwAHakX6dax2PuEfqiK5 P7EIsE/j3qs7al0BxSiGyZ9ZnPTo3BPz4NAk4/mWKQiKtJxlBIR2AcDfkkZSmv85ew4gzgdQ7g+eQ TPE7atmfQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1lBB8n-0001Y9-Ud; Sun, 14 Feb 2021 06:44:21 +0000 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1lBB8k-0001XP-Sf for linux-arm-kernel@lists.infradead.org; Sun, 14 Feb 2021 06:44:19 +0000 Received: by mail-pl1-x62b.google.com with SMTP id d15so1984536plh.4 for ; Sat, 13 Feb 2021 22:44: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; bh=Ox7P6Ft4Ae/j+8CvGaBiB8+ZHoryy/fndeBtAp254PA=; b=qBv+tcbiowvYapz/SeUwnAYB6zvJEsWUvQylhvVUkz5NIVuHV5ZmDy05VOYSdpIWRd TSxkZgJSjaiVflDWodvRm+vjYY2HL+TxXCMiNApFomJIZb2J+Dr23uMf/qX7rQWns4O2 i1hSInATgbKsIs8ZPswsT38u+fnPodt0SpdV2Ueo3/TL66YBZoUU7e9eUZGpYRdWA4W1 V8Sc6GOqNojMrzdeXccDSYyHbDESMXFekiyku4odyaOAYB6xQHglWfLW1AppHn/m+FMO 6DMsId8UV0fQEDOa1I+CWwvOlAwa+o12KUgEoOTc6dEqvGkLd6NAJA1x2NdwhAlyHZzy Wl4A== 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; bh=Ox7P6Ft4Ae/j+8CvGaBiB8+ZHoryy/fndeBtAp254PA=; b=CRWIqb9wgyhQouUOrF5jUpv8ShNivnjcoBcyPAV2U7sFsNNoaXp32Zd2YO0VF8BEw8 1huNMJZ6cqnKsg+yWems3bP5qztXYGGxWiCbYwFlqdhD2Umrt5MbG1vWZZ9l3aYnKemC pQMTPyY/qkUJsHUY14gUavqHy0DVRvrPItZNRU30XvnwcDeRsTI9Z8na2Ype7hm7eyIk fHWMy4DUCxJX6Lyxd9+HP77+T1VOJvD78+P6bZjY1pWL/mX/vrXq8LISr9zqt23OlXrN /nfia4eEXOvgKKcob7cOCgKZnKglrsXdPSIQzeTTi+tyh/0ZzLxUXKLfhZCVE1TwoAgk tcLw== X-Gm-Message-State: AOAM532kJLxKruSFeE/3z1E0wbCcg9TMib6j5OrmPJA9D4GF7YA+nQ7H uqUxo4Nfd9+UFOmQwr99tlc= X-Google-Smtp-Source: ABdhPJwQ+jMyex95c511IArWunpnYm+FgNfk2TK5ARZRICuM/+i5aZcHBsAg+yksW8yVR1ZZbn+Gyg== X-Received: by 2002:a17:903:2306:b029:de:18e9:f439 with SMTP id d6-20020a1709032306b02900de18e9f439mr9759653plh.38.1613285055310; Sat, 13 Feb 2021 22:44:15 -0800 (PST) Received: from shinobu ([156.146.35.76]) by smtp.gmail.com with ESMTPSA id b17sm13570674pfb.75.2021.02.13.22.44.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 13 Feb 2021 22:44:14 -0800 (PST) Date: Sun, 14 Feb 2021 15:44:07 +0900 From: William Breathitt Gray To: Syed Nayyar Waris Subject: Re: [PATCH v2 1/3] gpiolib: Introduce the for_each_set_clump macro Message-ID: References: MIME-Version: 1.0 In-Reply-To: X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210214_014418_992338_22C4A31F X-CRM114-Status: GOOD ( 34.86 ) 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, linus.walleij@linaro.org, daniel.lezcano@linaro.org, michal.simek@xilinx.com, linux-kernel@vger.kernel.org, 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 Content-Type: multipart/mixed; boundary="===============5479068378954118544==" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org --===============5479068378954118544== Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="2zWgD9gnv5AxmUo7" Content-Disposition: inline --2zWgD9gnv5AxmUo7 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Fri, Feb 12, 2021 at 06:50:20PM +0530, Syed Nayyar Waris wrote: > 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. >=20 > Cc: Linus Walleij > Cc: Bartosz Go=C5=82aszewski > Cc: Arnd Bergmann > Cc: William Breathitt Gray > Cc: Andy Shevchenko > Signed-off-by: Syed Nayyar Waris I think it would be good to retire the "clump" nomenclature and instead call this macro for_each_set_nbits in order to match the existing convention used by the bitmap functions. But I don't feel strongly enough to nitpick on that, so this version looks good to me as is. Acked-by: William Breathitt Gray > --- > drivers/gpio/gpiolib.c | 90 ++++++++++++++++++++++++++++++++++++++++++ > drivers/gpio/gpiolib.h | 28 +++++++++++++ > 2 files changed, 118 insertions(+) >=20 > 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, vo= id *v) > return 0; > } > =20 > +/** > + * 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 in= clusive). > + * > + * Returns value of nbits located at the @start bit offset within the @m= ap > + * memory region. > + */ > +unsigned long bitmap_get_value(const unsigned long *map, > + unsigned long start, > + unsigned long nbits) > +{ > + const size_t index =3D BIT_WORD(start); > + const unsigned long offset =3D start % BITS_PER_LONG; > + const unsigned long ceiling =3D round_up(start + 1, BITS_PER_LONG); > + const unsigned long space =3D ceiling - start; > + unsigned long value_low, value_high; > + > + if (space >=3D nbits) > + return (map[index] >> offset) & GENMASK(nbits - 1, 0); > + else { > + value_low =3D map[index] & BITMAP_FIRST_WORD_MASK(start); > + value_high =3D 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_L= ONG 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 =3D BIT_WORD(start); > + const unsigned long length =3D BIT_WORD(nbits); > + const unsigned long offset =3D start % BITS_PER_LONG; > + const unsigned long ceiling =3D round_up(start + 1, BITS_PER_LONG); > + const unsigned long space =3D ceiling - start; > + > + value &=3D GENMASK(value_width - 1, 0); > + > + if (space >=3D value_width) { > + map[index] &=3D ~(GENMASK(value_width - 1, 0) << offset); > + map[index] |=3D value << offset; > + } else { > + map[index + 0] &=3D ~BITMAP_FIRST_WORD_MASK(start); > + map[index + 0] |=3D value << offset; > + > + if (index + 1 >=3D length) > + return; > + > + map[index + 1] &=3D ~BITMAP_LAST_WORD_MASK(start + value_width); > + map[index + 1] |=3D 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 =3D find_next_bit(addr, size, offset); > + if (offset =3D=3D size) > + return size; > + > + offset =3D rounddown(offset, clump_size); > + *clump =3D bitmap_get_value(addr, offset, clump_size); > + return offset; > +} > +EXPORT_SYMBOL_GPL(find_next_clump); > + > static const struct seq_operations gpiolib_sops =3D { > .start =3D gpiolib_seq_start, > .next =3D 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); > =20 > +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) =3D find_first_clump(&(clump), (bits), (size), (clump_size= )); \ > + (start) < (size); \ > + (start) =3D find_next_clump(&(clump), (bits), (size), (start) + (c= lump_size), (clump_size))) > + > /* > * Return the GPIO number of the passed descriptor relative to its chip > */ > --=20 > 2.29.0 >=20 --2zWgD9gnv5AxmUo7 Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEk5I4PDJ2w1cDf/bghvpINdm7VJIFAmAoxqsACgkQhvpINdm7 VJKYxxAArGoTAiMfiMp0c/4bQYLA/idauyYXF7ssxggh9Y65sgKLeqVoZMadHROX BKtQ7lX9Ul+Xb/mRNlXhUWxGv3X23sN1ORDM1b1rxxmbhyTpZ2SwM6zBdkLcjJYK 4PqARL05Fg7jon9vbBwLXhGTtV8vl/b19gNTUnlOstxMw5eGabBn1p4y1RLnVIH1 u/tua5GDG9KgGbEjaEsT4xrq7TpZxgdLzKQ0BBgyTb+Vm0b/8KJce93HVu+1Ma05 UAK6kecOMSPw13aN6o2WV/lai1l/LAl3gJ2FfygzlT0+lHGOrZQhJh/HgVAASqZY JuivDcPIeSrbRoxUeJjqk866jc/eV9mKu1Nc2KbXQ5wQZ4y3gO5Wamir8PM7k7uv lFyTVOQYY3xN9EAvKWIjFubcjreNdnHZVIfsj73W/pDG04GnkG0/y4lLn+ygzjE9 NdlNG5LPdDz8VYYEivI0/ZsELIyJ053rLI1Wb9FpXu9Cr9MDSdcNzSJIZwlmLnDN qxK+W4dShmW2Ting8gyvsa4sTxgEQu5GI/hroQvwhMl0XlnRDVp7SbUQqYFt6/M0 gscZfGaGB+xO140EGs5Kr9z0P6CNGBc2sXPy+CinPYmczaCL/O+cIHzI85/jat8S flGNmtjOcRPFqPx78vMnGV4TueNvJwT4FVzulrqyuvhwOdOSmbU= =XFCt -----END PGP SIGNATURE----- --2zWgD9gnv5AxmUo7-- --===============5479068378954118544== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel --===============5479068378954118544==--