From patchwork Tue Sep 26 03:12:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Masahiro Yamada X-Patchwork-Id: 114211 Delivered-To: patch@linaro.org Received: by 10.140.106.117 with SMTP id d108csp3335536qgf; Mon, 25 Sep 2017 20:12:53 -0700 (PDT) X-Google-Smtp-Source: AOwi7QB6BRQKpJ+G4tlLB+YUMvROd61gwty3B6HkageI8VWyvZeFUVAGOVWzV32egMi727eGFFrl X-Received: by 10.80.206.14 with SMTP id y14mr10054651edi.94.1506395573649; Mon, 25 Sep 2017 20:12:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1506395573; cv=none; d=google.com; s=arc-20160816; b=H4mGxxXcUt7L1Lyf1PBnXAeW+K4w3WGpoRj0pCWSejwTqYsXwKxATbms0cBC6jnZZ8 BfjWL+9IKb0qiMwbdxlhCEFPOStJRQQPfTDixn8tpiG1FtnAAy3VqVcRtdozRW1jwEK4 JwXsShuR9RFLiKDHuKOyLIB16g/t54C6eePcsrwepPP3H++0Jt1HStpTqpEu8n+gZ1yD lOsNzw63GxQiYVlP2g3mrwnxQBxVQXZc485k8lDpG9ujiEG0SpXobFjYzYAtrfDZcdrH LaIMFAJTq99ly6xoARSpiKpjGKE4owGx+//EC+FWgQJoELLLK1ZNmYBZ2bP5h6eEMPRB Ibbw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:message-id:date:to:from :dkim-signature:dkim-filter:arc-authentication-results; bh=80YqlkCL19UiolN8OZTEPbWc+Qe/I39vEtYbrQ1l0RQ=; b=U5xyRXPow1U9sEEK0uDiE+7fbLitGRqqBR6mGq/1tBRH9NOHeORsoDKBLSO+vsjRqI RR3VvvkMP3xKzxRGPPoBxO5vzf/8slmhkBRh2BZ03VZyiht/dqPIZ8wsOcCbq3XipgCZ TQLHG4oFtuLzSpOfWdztV/nmijT+fDjggg5JpTm0yvZCyG9nMjnOZSQ+T92M1lpBvaNC V8+E9vzJGiou4extgQPXk9glMjkAxdgv727iqqwVIx8EfemoX8p7WiDpIW2ZJ63iD7Ne LFW6OUtuUX6zHL7lXMqO29YMnUu6LfGpGlN+1SSP133Po3F+9OR9KGpDXGSPpnnlks6t avlw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com header.s=dec2015msa header.b=1p6/3TlU; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id o6si1145318eda.375.2017.09.25.20.12.53; Mon, 25 Sep 2017 20:12:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@nifty.com header.s=dec2015msa header.b=1p6/3TlU; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by lists.denx.de (Postfix, from userid 105) id E7451C21DD9; Tue, 26 Sep 2017 03:12:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 40BD8C21CA5; Tue, 26 Sep 2017 03:12:47 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 85C9DC21CA5; Tue, 26 Sep 2017 03:12:45 +0000 (UTC) Received: from conuserg-10.nifty.com (conuserg-10.nifty.com [210.131.2.77]) by lists.denx.de (Postfix) with ESMTPS id A29F1C21C71 for ; Tue, 26 Sep 2017 03:12:43 +0000 (UTC) Received: from pug.e01.socionext.com (p14092-ipngnfx01kyoto.kyoto.ocn.ne.jp [153.142.97.92]) (authenticated) by conuserg-10.nifty.com with ESMTP id v8Q3C5sI021445; Tue, 26 Sep 2017 12:12:05 +0900 DKIM-Filter: OpenDKIM Filter v2.10.3 conuserg-10.nifty.com v8Q3C5sI021445 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nifty.com; s=dec2015msa; t=1506395526; bh=C+ET4f56fAr2VRWUMwWKTCuekq7zzjOG/mAhhHvyzeU=; h=From:To:Cc:Subject:Date:From; b=1p6/3TlUW8L6IUqaoKeTDgHC3r7NVtzq6304bTmY+NcG6rdgGiLeYZlMhK2qlswM9 dUL2y+6emr6YyARz6+HCDp3rfSaow4dI2mt/OXC+iTtF0HYayt7XJ2FtuFE3Qbz+1a K3CB9IncqVKewkZETWV8D2VB8oY7T/PZHTKDhqkqbcnlC+m9pse18ue5tIC1nghZ+U Q9QuJLwRDf3CRHQF+v/qzJ6B7bcJzBkhx069HDhNXrPk8mOZiDshvnL+a53fmaGWjg Wme5IKtHmO+joNDF7OXUxPS+wdj0aetRTzS2WZSlXD4NSLeXNuLiGi+3LgXonDIJvH UFdHBhiYtqnlA== X-Nifty-SrcIP: [153.142.97.92] From: Masahiro Yamada To: u-boot@lists.denx.de Date: Tue, 26 Sep 2017 12:12:02 +0900 Message-Id: <1506395522-20004-1-git-send-email-yamada.masahiro@socionext.com> X-Mailer: git-send-email 2.7.4 Cc: Tom Rini Subject: [U-Boot] [PATCH] linux/bitfield.h: import from Linux 4.13 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" Copied from Linux 4.13. Commit log of 3e9b3112ec74 of Linux explains well why this header is useful. Signed-off-by: Masahiro Yamada Reviewed-by: Simon Glass --- In order to use this header, http://patchwork.ozlabs.org/patch/814471/ must be applied. is highly dependent on include/linux/bitfield.h | 106 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 include/linux/bitfield.h diff --git a/include/linux/bitfield.h b/include/linux/bitfield.h new file mode 100644 index 0000000..8b9d6ff --- /dev/null +++ b/include/linux/bitfield.h @@ -0,0 +1,106 @@ +/* + * Copyright (C) 2014 Felix Fietkau + * Copyright (C) 2004 - 2009 Ivo van Doorn + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _LINUX_BITFIELD_H +#define _LINUX_BITFIELD_H + +#include + +/* + * Bitfield access macros + * + * FIELD_{GET,PREP} macros take as first parameter shifted mask + * from which they extract the base mask and shift amount. + * Mask must be a compilation time constant. + * + * Example: + * + * #define REG_FIELD_A GENMASK(6, 0) + * #define REG_FIELD_B BIT(7) + * #define REG_FIELD_C GENMASK(15, 8) + * #define REG_FIELD_D GENMASK(31, 16) + * + * Get: + * a = FIELD_GET(REG_FIELD_A, reg); + * b = FIELD_GET(REG_FIELD_B, reg); + * + * Set: + * reg = FIELD_PREP(REG_FIELD_A, 1) | + * FIELD_PREP(REG_FIELD_B, 0) | + * FIELD_PREP(REG_FIELD_C, c) | + * FIELD_PREP(REG_FIELD_D, 0x40); + * + * Modify: + * reg &= ~REG_FIELD_C; + * reg |= FIELD_PREP(REG_FIELD_C, c); + */ + +#define __bf_shf(x) (__builtin_ffsll(x) - 1) + +#define __BF_FIELD_CHECK(_mask, _reg, _val, _pfx) \ + ({ \ + BUILD_BUG_ON_MSG(!__builtin_constant_p(_mask), \ + _pfx "mask is not constant"); \ + BUILD_BUG_ON_MSG(!(_mask), _pfx "mask is zero"); \ + BUILD_BUG_ON_MSG(__builtin_constant_p(_val) ? \ + ~((_mask) >> __bf_shf(_mask)) & (_val) : 0, \ + _pfx "value too large for the field"); \ + BUILD_BUG_ON_MSG((_mask) > (typeof(_reg))~0ull, \ + _pfx "type of reg too small for mask"); \ + __BUILD_BUG_ON_NOT_POWER_OF_2((_mask) + \ + (1ULL << __bf_shf(_mask))); \ + }) + +/** + * FIELD_FIT() - check if value fits in the field + * @_mask: shifted mask defining the field's length and position + * @_val: value to test against the field + * + * Return: true if @_val can fit inside @_mask, false if @_val is too big. + */ +#define FIELD_FIT(_mask, _val) \ + ({ \ + __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_FIT: "); \ + !((((typeof(_mask))_val) << __bf_shf(_mask)) & ~(_mask)); \ + }) + +/** + * FIELD_PREP() - prepare a bitfield element + * @_mask: shifted mask defining the field's length and position + * @_val: value to put in the field + * + * FIELD_PREP() masks and shifts up the value. The result should + * be combined with other fields of the bitfield using logical OR. + */ +#define FIELD_PREP(_mask, _val) \ + ({ \ + __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: "); \ + ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \ + }) + +/** + * FIELD_GET() - extract a bitfield element + * @_mask: shifted mask defining the field's length and position + * @_reg: 32bit value of entire bitfield + * + * FIELD_GET() extracts the field specified by @_mask from the + * bitfield passed in as @_reg by masking and shifting it down. + */ +#define FIELD_GET(_mask, _reg) \ + ({ \ + __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: "); \ + (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \ + }) + +#endif