From patchwork Tue Jul 7 19:11:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1324662 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=gRvj1iBb; 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 4B1XF528zFz9s1x for ; Wed, 8 Jul 2020 05:14:49 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 10B4A82181; Tue, 7 Jul 2020 21:13:02 +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="gRvj1iBb"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id BEAEE81D39; Tue, 7 Jul 2020 21:12:49 +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-il1-x141.google.com (mail-il1-x141.google.com [IPv6:2607:f8b0:4864:20::141]) (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 A7AC281C26 for ; Tue, 7 Jul 2020 21:12:44 +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-il1-x141.google.com with SMTP id r12so29768057ilh.4 for ; Tue, 07 Jul 2020 12:12:44 -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=e+5ToC6w5un/sunzO3MBsYo72ZOuHd6AADKJc948Xaw=; b=gRvj1iBbJTbtO4Vw4oHX/3e2YZtvoykxjHoSSD21AjOuyZxZbV2H7hzznr2HHWqkrR SC1AwUc1aoQ0AeKsCPWvVrvZXmo/oE8eMRcXGlwOsms4REiqnWNgLHNBVZkC4BNmI8Yd nCP643Xg0MMUeQYO6hmf73//EtuYyHk2jSJG0= 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=e+5ToC6w5un/sunzO3MBsYo72ZOuHd6AADKJc948Xaw=; b=c6WTQrALYf6/i+g8P44A2WqlR3xI5U3hOC5oSVMZJv2fLj4m5WoEci4npu9dlFSzsH zME44Ozrjf/EH4TOCasDAwihGPUEuZHS2Ig3f5+N1TWO5USVsdzEmG4mafZoqjw1ZKpN lepNuMThzKOBXl5scx3CO4ZLU+LSnN0uqbdMA6zsF/PbHyJr5upIPFc1wkpzzBlauPzO HSTXt7d8V/sBSOMY41SbIMoHywec1QhnQukQtLDAapY8OESWOw3s1cz80B6yKaHIfFKs bjIdE6uFoiyBFCjnzhAgMe/sc1YALee3RnfQ+ag8WKrHsFMH8tEOguOz37F8jRbQfxKJ l24A== X-Gm-Message-State: AOAM532UJVx6pnx/4/77OWRvfghD7+rOvo6yPE2DmNHedUMyTBrYGCks 428/pQ1Jm/g8hMyy1nQL4rgzvgkqkp/Oxg== X-Google-Smtp-Source: ABdhPJzV1gdnb23P1vAPq5n1Ejq3Fb63NZ0RNR+Bqi1r0rad06AHq3cgJfyqY7zO9RximsSw1RyZTw== X-Received: by 2002:a92:c00d:: with SMTP id q13mr33335043ild.222.1594149163200; Tue, 07 Jul 2020 12:12:43 -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.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2020 12:12:42 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Andy Shevchenko , Bin Meng , Wolfgang Wallner , Simon Glass Subject: [PATCH v4 06/35] acpi: Support generation of interrupt descriptor Date: Tue, 7 Jul 2020 13:11:43 -0600 Message-Id: <20200707191212.2542638-3-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 Add a function to write an interrupt descriptor to the generated ACPI code. Signed-off-by: Simon Glass Reviewed-by: Wolfgang Wallner Reviewed-by: Bin Meng --- Changes in v4: - Rename the length-writing functions to indicate they are for large resources include/acpi/acpi_device.h | 15 +++++ lib/acpi/acpi_device.c | 119 +++++++++++++++++++++++++++++++++++++ test/dm/acpigen.c | 32 ++++++++++ 3 files changed, 166 insertions(+) diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h index 24895de0da..e72fede54c 100644 --- a/include/acpi/acpi_device.h +++ b/include/acpi/acpi_device.h @@ -11,6 +11,8 @@ #include +struct acpi_ctx; +struct irq; struct udevice; /* ACPI descriptor values for common descriptors: SERIAL_BUS means I2C */ @@ -128,4 +130,17 @@ int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen); */ enum acpi_dev_status acpi_device_status(const struct udevice *dev); +/** + * acpi_device_write_interrupt_irq() - Write an interrupt descriptor + * + * This writes an ACPI interrupt descriptor for the given interrupt, converting + * fields as needed. + * + * @ctx: ACPI context pointer + * @req_irq: Interrupt to output + * @return IRQ pin number if OK, -ve on error + */ +int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx, + const struct irq *req_irq); + #endif diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c index 60f4fd8cd5..d854a45cbc 100644 --- a/lib/acpi/acpi_device.c +++ b/lib/acpi/acpi_device.c @@ -8,8 +8,10 @@ #include #include +#include #include #include +#include #include /** @@ -86,3 +88,120 @@ enum acpi_dev_status acpi_device_status(const struct udevice *dev) { return ACPI_DSTATUS_ALL_ON; } + +/** + * largeres_write_len_f() - Write a placeholder word value + * + * Write a forward length for a large resource (2 bytes) + * + * @return pointer to the zero word (for fixing up later) + */ +static void *largeres_write_len_f(struct acpi_ctx *ctx) +{ + u8 *p = acpigen_get_current(ctx); + + acpigen_emit_word(ctx, 0); + + return p; +} + +/** + * largeres_fill_from_len() - Fill in a length value + * + * This calculated the number of bytes since the provided @start and writes it + * to @ptr, which was previous returned by largeres_write_len_f(). + * + * @ptr: Word to update + * @start: Start address to count from to calculated the length + */ +static void largeres_fill_from_len(struct acpi_ctx *ctx, char *ptr, u8 *start) +{ + u16 len = acpigen_get_current(ctx) - start; + + ptr[0] = len & 0xff; + ptr[1] = (len >> 8) & 0xff; +} + +/** + * largeres_fill_len() - Fill in a length value, excluding the length itself + * + * Fill in the length field with the value calculated from after the 16bit + * field to acpigen current. This is useful since the length value does not + * include the length field itself. + * + * This calls acpi_device_largeres_fill_len() passing @ptr + 2 as @start + * + * @ptr: Word to update. + */ +static void largeres_fill_len(struct acpi_ctx *ctx, void *ptr) +{ + largeres_fill_from_len(ctx, ptr, ptr + sizeof(u16)); +} + +/* ACPI 6.3 section 6.4.3.6: Extended Interrupt Descriptor */ +static int acpi_device_write_interrupt(struct acpi_ctx *ctx, + const struct acpi_irq *irq) +{ + void *desc_length; + u8 flags; + + if (!irq->pin) + return -ENOENT; + + /* This is supported by GpioInt() but not Interrupt() */ + if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH) + return -EINVAL; + + /* Byte 0: Descriptor Type */ + acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_INTERRUPT); + + /* Byte 1-2: Length (filled in later) */ + desc_length = largeres_write_len_f(ctx); + + /* + * Byte 3: Flags + * [7:5]: Reserved + * [4]: Wake (0=NO_WAKE 1=WAKE) + * [3]: Sharing (0=EXCLUSIVE 1=SHARED) + * [2]: Polarity (0=HIGH 1=LOW) + * [1]: Mode (0=LEVEL 1=EDGE) + * [0]: Resource (0=PRODUCER 1=CONSUMER) + */ + flags = BIT(0); /* ResourceConsumer */ + if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED) + flags |= BIT(1); + if (irq->polarity == ACPI_IRQ_ACTIVE_LOW) + flags |= BIT(2); + if (irq->shared == ACPI_IRQ_SHARED) + flags |= BIT(3); + if (irq->wake == ACPI_IRQ_WAKE) + flags |= BIT(4); + acpigen_emit_byte(ctx, flags); + + /* Byte 4: Interrupt Table Entry Count */ + acpigen_emit_byte(ctx, 1); + + /* Byte 5-8: Interrupt Number */ + acpigen_emit_dword(ctx, irq->pin); + + /* Fill in Descriptor Length (account for len word) */ + largeres_fill_len(ctx, desc_length); + + return 0; +} + +int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx, + const struct irq *req_irq) +{ + struct acpi_irq irq; + int ret; + + ret = irq_get_acpi(req_irq, &irq); + if (ret) + return log_msg_ret("get", ret); + ret = acpi_device_write_interrupt(ctx, &irq); + if (ret) + return log_msg_ret("write", ret); + + return 0; +} diff --git a/test/dm/acpigen.c b/test/dm/acpigen.c index 9ff9b68532..a4adfbfdf8 100644 --- a/test/dm/acpigen.c +++ b/test/dm/acpigen.c @@ -8,8 +8,10 @@ #include #include +#include #include #include +#include #include #include #include @@ -70,3 +72,33 @@ static int dm_test_acpi_emit_simple(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_acpi_emit_simple, 0); + +/* Test emitting an interrupt descriptor */ +static int dm_test_acpi_interrupt(struct unit_test_state *uts) +{ + struct acpi_ctx *ctx; + struct udevice *dev; + struct irq irq; + u8 *ptr; + + ut_assertok(alloc_context(&ctx)); + + ptr = acpigen_get_current(ctx); + + ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); + ut_assertok(irq_get_by_index(dev, 0, &irq)); + + /* See a-test, property interrupts-extended in the device tree */ + ut_asserteq(3, acpi_device_write_interrupt_irq(ctx, &irq)); + ut_asserteq(9, acpigen_get_current(ctx) - ptr); + ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]); + ut_asserteq(6, get_unaligned((u16 *)(ptr + 1))); + ut_asserteq(0x19, ptr[3]); + ut_asserteq(1, ptr[4]); + ut_asserteq(3, get_unaligned((u32 *)(ptr + 5))); + + free_context(&ctx); + + return 0; +} +DM_TEST(dm_test_acpi_interrupt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);