From patchwork Tue Jul 7 19:11:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1324673 X-Patchwork-Delegate: bmeng.cn@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=fb90PVOZ; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4B1XHZ11dbz9sSJ for ; Wed, 8 Jul 2020 05:16:58 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 77A1A821BC; Tue, 7 Jul 2020 21:13:19 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="fb90PVOZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7849982165; Tue, 7 Jul 2020 21:12:57 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-io1-xd44.google.com (mail-io1-xd44.google.com [IPv6:2607:f8b0:4864:20::d44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 79AFD820DB for ; Tue, 7 Jul 2020 21:12:54 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-io1-xd44.google.com with SMTP id i4so44320524iov.11 for ; Tue, 07 Jul 2020 12:12:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ocbfQyb62ZHDSM/zfy1NdomrLjIes6wY/KUR5AKqr1A=; b=fb90PVOZMrxYiat1M5XQANbSXKWG62hoXp6p23gQtQIZ2woQ6XyipwLtn9pD92D+/L e8DFKum1HHJ3MSvsxyz0LUpVvpJtPTi9QN1RI8klzweV3/T7Fd4sG4w+Idl0gJxjJkEA SCq5eJ3z/DOJjP8WOkVjaKMEtEi2itrUe8OCA= 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:mime-version:content-transfer-encoding; bh=ocbfQyb62ZHDSM/zfy1NdomrLjIes6wY/KUR5AKqr1A=; b=HiAzr4GCvCGtz0++I2Ey8Jf7ahDAMjV721h3xvtEet1GzMyRcdJNYjMYaYSBh9lusY qaTXm/DyVQpLR2yaFSP0OLd5DQdjYpt8AFbBF6UmHQ1hRYI5p0wBJphu2oyVqVtWMYJK loirwv8V/b8VBvJ1vFVNDP/s7TmOb1b+nPcU05QjXmC5Eru33TyD6RclqmgBNJNYVhfc RhtHkrkKhfat/l1zX7DdpsI7m0vFzZ1YYH9YtIAynXj805EWCfZUqpNxz/h9U8tLYOoc EZ3Zzyiko03CH8W3d2LWg+j2/rQyfRiV0UEw/fSMvCCNwHZk8FS/tI5d/tejSlMS19wF JatA== X-Gm-Message-State: AOAM533ois8DuI7fiDIFQlQMG3HgxusRJmlzPW5/NDsIq7jN+3q3LRKH b+Id0do6QhuY7cTfiqdprNNuzmTsPbE5HQ== X-Google-Smtp-Source: ABdhPJxdxHh9Ta9HiLFTcNJXCbDAyEYzMSK8ifQrb15Lw/mMDXbjDM4oj47WoDh1a7u6GQAD9b273g== X-Received: by 2002:a5e:8b44:: with SMTP id z4mr10433846iom.123.1594149173203; Tue, 07 Jul 2020 12:12:53 -0700 (PDT) Received: from localhost.localdomain (c-73-14-175-90.hsd1.co.comcast.net. [73.14.175.90]) by smtp.gmail.com with ESMTPSA id 186sm9584320iow.26.2020.07.07.12.12.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2020 12:12:52 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Andy Shevchenko , Bin Meng , Wolfgang Wallner , Simon Glass Subject: [PATCH v4 15/35] acpi: Support writing an integer Date: Tue, 7 Jul 2020 13:11:52 -0600 Message-Id: <20200707191212.2542638-7-sjg@chromium.org> X-Mailer: git-send-email 2.27.0.383.g050319c2ae-goog In-Reply-To: <20200707191212.2542638-1-sjg@chromium.org> References: <20200707191212.2542638-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean ACPI supports storing integers in various ways. Add a function to handle this. Signed-off-by: Simon Glass Reviewed-by: Wolfgang Wallner Reviewed-by: Bin Meng --- Changes in v4: - Use 'writing' instead of emitting in test comment include/acpi/acpigen.h | 17 ++++++++++++++ lib/acpi/acpigen.c | 51 ++++++++++++++++++++++++++++++++++++++++++ test/dm/acpigen.c | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/include/acpi/acpigen.h b/include/acpi/acpigen.h index c1a6bc263c..9e52ec589f 100644 --- a/include/acpi/acpigen.h +++ b/include/acpi/acpigen.h @@ -19,6 +19,12 @@ struct acpi_ctx; /* ACPI Op/Prefix codes */ enum { + ZERO_OP = 0x00, + ONE_OP = 0x01, + BYTE_PREFIX = 0x0a, + WORD_PREFIX = 0x0b, + DWORD_PREFIX = 0x0c, + QWORD_PREFIX = 0x0e, PACKAGE_OP = 0x12, }; @@ -138,4 +144,15 @@ void acpigen_pop_len(struct acpi_ctx *ctx); */ char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el); +/** + * acpigen_write_integer() - Write an integer + * + * This writes an operation (BYTE_OP, WORD_OP, DWORD_OP, QWORD_OP depending on + * the integer size) and an integer value. Note that WORD means 16 bits in ACPI. + * + * @ctx: ACPI context pointer + * @data: Integer to write + */ +void acpigen_write_integer(struct acpi_ctx *ctx, u64 data); + #endif diff --git a/lib/acpi/acpigen.c b/lib/acpi/acpigen.c index 9043c2bc72..27d4eab7a6 100644 --- a/lib/acpi/acpigen.c +++ b/lib/acpi/acpigen.c @@ -83,6 +83,57 @@ char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el) return p; } +void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data) +{ + acpigen_emit_byte(ctx, BYTE_PREFIX); + acpigen_emit_byte(ctx, data & 0xff); +} + +void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data) +{ + acpigen_emit_byte(ctx, WORD_PREFIX); + acpigen_emit_word(ctx, data); +} + +void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data) +{ + acpigen_emit_byte(ctx, DWORD_PREFIX); + acpigen_emit_dword(ctx, data); +} + +void acpigen_write_qword(struct acpi_ctx *ctx, u64 data) +{ + acpigen_emit_byte(ctx, QWORD_PREFIX); + acpigen_emit_dword(ctx, data & 0xffffffff); + acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff); +} + +void acpigen_write_zero(struct acpi_ctx *ctx) +{ + acpigen_emit_byte(ctx, ZERO_OP); +} + +void acpigen_write_one(struct acpi_ctx *ctx) +{ + acpigen_emit_byte(ctx, ONE_OP); +} + +void acpigen_write_integer(struct acpi_ctx *ctx, u64 data) +{ + if (data == 0) + acpigen_write_zero(ctx); + else if (data == 1) + acpigen_write_one(ctx); + else if (data <= 0xff) + acpigen_write_byte(ctx, (unsigned char)data); + else if (data <= 0xffff) + acpigen_write_word(ctx, (unsigned int)data); + else if (data <= 0xffffffff) + acpigen_write_dword(ctx, (unsigned int)data); + else + acpigen_write_qword(ctx, data); +} + void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size) { int i; diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c index aaffc6a4cf..187001d2fc 100644 --- a/test/dm/acpigen.c +++ b/test/dm/acpigen.c @@ -25,6 +25,11 @@ #define TEST_STRING "frogmore" #define TEST_STREAM2 "\xfa\xde" +#define TEST_INT8 0x7d +#define TEST_INT16 0x2345 +#define TEST_INT32 0x12345678 +#define TEST_INT64 0x4567890123456 + static int alloc_context_size(struct acpi_ctx **ctxp, int size) { struct acpi_ctx *ctx; @@ -431,3 +436,44 @@ static int dm_test_acpi_package(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_acpi_package, 0); + +/* Test writing an integer */ +static int dm_test_acpi_integer(struct unit_test_state *uts) +{ + struct acpi_ctx *ctx; + u8 *ptr; + + ut_assertok(alloc_context(&ctx)); + + ptr = acpigen_get_current(ctx); + + acpigen_write_integer(ctx, 0); + acpigen_write_integer(ctx, 1); + acpigen_write_integer(ctx, TEST_INT8); + acpigen_write_integer(ctx, TEST_INT16); + acpigen_write_integer(ctx, TEST_INT32); + acpigen_write_integer(ctx, TEST_INT64); + + ut_asserteq(6 + 1 + 2 + 4 + 8, acpigen_get_current(ctx) - ptr); + + ut_asserteq(ZERO_OP, ptr[0]); + + ut_asserteq(ONE_OP, ptr[1]); + + ut_asserteq(BYTE_PREFIX, ptr[2]); + ut_asserteq(TEST_INT8, ptr[3]); + + ut_asserteq(WORD_PREFIX, ptr[4]); + ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 5))); + + ut_asserteq(DWORD_PREFIX, ptr[7]); + ut_asserteq(TEST_INT32, get_unaligned((u32 *)(ptr + 8))); + + ut_asserteq(QWORD_PREFIX, ptr[12]); + ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 13))); + + free_context(&ctx); + + return 0; +} +DM_TEST(dm_test_acpi_integer, 0);