From patchwork Mon May 7 15:35:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: William Breathitt Gray X-Patchwork-Id: 909815 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=linux-gpio-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="dl5XHRTS"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 40fmt96cwKz9s47 for ; Tue, 8 May 2018 01:36:01 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752099AbeEGPf2 (ORCPT ); Mon, 7 May 2018 11:35:28 -0400 Received: from mail-yw0-f195.google.com ([209.85.161.195]:41638 "EHLO mail-yw0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751913AbeEGPf0 (ORCPT ); Mon, 7 May 2018 11:35:26 -0400 Received: by mail-yw0-f195.google.com with SMTP id q125-v6so4632079ywc.8; Mon, 07 May 2018 08:35:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SYqjrGM4rdL/qFsrD90ydFOcJqXW8BhXuyIbZcJ9vN4=; b=dl5XHRTSk763x0Er3MpJ0bsfT4QAUtHTZL0Fo3WwquJ6bP6E3gFT84fzD15I0WlxcI MFCOdZCz96ZnXaGmvI1xRTJh1WBYt6j3zvdNAENDiurUlxn82hrtvWUOVNg0jGTVYGY+ ElJkXA32SenzSzfx4ZGSmXVmP7nPneOvyYcfdq5xS35UVzAmv4v8C3TuD0DBbThtU+o2 rE23loNOOjdJc51sTkw6BBfuDW/gSL9yvVkDYRSmy9Qq92aFFgqxstm5Q2HQ38I7MGp0 scTocAuDnBIRVUkRYWqG1fgrQbPRD/6kQZ1F4RQJFfQmbQAe0bo0lgeLunvWI1BIR8g9 s28A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SYqjrGM4rdL/qFsrD90ydFOcJqXW8BhXuyIbZcJ9vN4=; b=OqFMuvr5JasQj7cYsd/o9oOdZ9/5qL9MH8myqRdUoOMqtniTK5GP061kgw4R1Hz7p3 Yu7xnDxzaiUgEpfImv4Ye+zJU/otZ5IDXGAeHNGAhDVtu5OXQ7b6bf0L4HMwj6R7obd9 OaEZCC6re3YWlc8JPeKsT80ylcgj9vALnJng3XW83KGWzCW2OADg6A/Y6bkKzxb9VeEB HqN3CKsvEWU1pqEEUZn7eAO6+J2X7u80yLfdelvgMODMoj3Xd4IzAqviaii5a/IH/++a 5DeNKqos1eSd+52j4zDqcZJGlx36cIdYar42RJSJigmIkrP3Rbhaplu/EFDzAPTsrs3H /y4A== X-Gm-Message-State: ALQs6tCX5VjzHxZKZBIqvKiTdsXtswljdGDmMD+MnqbWezs2oXtdw4QJ hHzqNY1rNn/CXJoA+8EnGdGSoA== X-Google-Smtp-Source: AB8JxZrYpgm/cMH3R1joLtBlKeS7Xelo+N83k64C2VDFVlHMacWpDVeeb9SZHoB1UVmi2bTICKy9hw== X-Received: by 2002:a0d:d995:: with SMTP id b143-v6mr21012122ywe.220.1525707325888; Mon, 07 May 2018 08:35:25 -0700 (PDT) Received: from localhost ([72.188.97.40]) by smtp.gmail.com with ESMTPSA id g2-v6sm9912190ywa.63.2018.05.07.08.35.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 07 May 2018 08:35:25 -0700 (PDT) From: William Breathitt Gray To: linus.walleij@linaro.org Cc: linux-gpio@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org, William Breathitt Gray , Arnd Bergmann Subject: [PATCH 1/7] bitops: Introduce the for_each_set_port_word macro Date: Mon, 7 May 2018 11:35:18 -0400 Message-Id: <7b704b1feb2f2cdb6c3a3061fa5f5d804954d15d.1525704095.git.vilhelm.gray@gmail.com> X-Mailer: git-send-email 2.17.0 In-Reply-To: References: Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org This macro iterates for each group of bits (port word) with set bits, within a bitmap memory region. For each iteration, "port_word" is set to the found port word index, "word_index" is set to the word index of the bitmap containing the found port word, and "word_offset" is set to the bit offset of the found port word within the respective bitmap word. Cc: Arnd Bergmann Signed-off-by: William Breathitt Gray --- include/asm-generic/bitops/find.h | 24 ++++++++++++++++++++++++ include/linux/bitops.h | 9 +++++++++ lib/find_bit.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/include/asm-generic/bitops/find.h b/include/asm-generic/bitops/find.h index 8a1ee10014de..b24f3f92642b 100644 --- a/include/asm-generic/bitops/find.h +++ b/include/asm-generic/bitops/find.h @@ -80,4 +80,28 @@ extern unsigned long find_first_zero_bit(const unsigned long *addr, #endif /* CONFIG_GENERIC_FIND_FIRST_BIT */ +/** + * find_next_port_word - find next port word with set bits in a memory region + * @word_index: location to store bitmap word index of found port word + * @word_offset: bits offset of the found port word in respective bitmap word + * @bits: address to base the search on + * @size: bitmap size in number of ports + * @offset: port word index to start searching at + * @port_size: port word size in bits + * + * Returns the port index for the next port word with set bits; the respective + * bitmap word index is stored at the location pointed by @word_index, and the + * bits offset of the found port word within the respective bitmap word is + * stored at the location pointed by @word_offset. If no bits are set, returns + * @size. + */ +size_t find_next_port_word(size_t *const word_index, + unsigned int *const word_offset, + const unsigned long *const bits, const size_t size, + const size_t offset, const unsigned int port_size); + +#define find_first_port_word(word_index, word_offset, bits, size, port_size) \ + find_next_port_word((word_index), (word_offset), (bits), (size), 0, \ + (port_size)) + #endif /*_ASM_GENERIC_BITOPS_FIND_H_ */ diff --git a/include/linux/bitops.h b/include/linux/bitops.h index 4cac4e1a72ff..c3b7caf4ad2d 100644 --- a/include/linux/bitops.h +++ b/include/linux/bitops.h @@ -59,6 +59,15 @@ extern unsigned long __sw_hweight64(__u64 w); (bit) < (size); \ (bit) = find_next_zero_bit((addr), (size), (bit) + 1)) +#define for_each_set_port_word(port_word, word_index, word_offset, bits, size, \ + port_size) \ + for ((port_word) = find_first_port_word(&(word_index), &(word_offset), \ + (bits), (size), (port_size)); \ + (port_word) < (size); \ + (port_word) = find_next_port_word(&(word_index), &(word_offset), \ + (bits), (size), (port_word) + 1, \ + (port_size))) + static inline int get_bitmask_order(unsigned int count) { int order; diff --git a/lib/find_bit.c b/lib/find_bit.c index ee3df93ba69a..628c94d63d6b 100644 --- a/lib/find_bit.c +++ b/lib/find_bit.c @@ -20,6 +20,7 @@ #include #include #include +#include #if !defined(find_next_bit) || !defined(find_next_zero_bit) || \ !defined(find_next_and_bit) @@ -218,3 +219,30 @@ EXPORT_SYMBOL(find_next_bit_le); #endif #endif /* __BIG_ENDIAN */ + +size_t find_next_port_word(size_t *const word_index, + unsigned int *const word_offset, + const unsigned long *const bits, const size_t size, + const size_t offset, const unsigned int port_size) +{ + size_t i; + unsigned int bits_offset; + unsigned long word_mask; + const unsigned long port_mask = GENMASK(port_size - 1, 0); + + for (i = offset; i < size; i++) { + bits_offset = i * port_size; + + *word_index = BIT_WORD(bits_offset); + *word_offset = bits_offset % BITS_PER_LONG; + + word_mask = bits[*word_index] & (port_mask << *word_offset); + if (!word_mask) + continue; + + return i; + } + + return size; +} +EXPORT_SYMBOL(find_next_port_word);