diff mbox series

[v3,06/35] acpi: Support generation of interrupt descriptor

Message ID 20200614025523.40183-3-sjg@chromium.org
State Superseded
Headers show
Series dm: Add programmatic generation of ACPI tables (part B) | expand

Commit Message

Simon Glass June 14, 2020, 2:54 a.m. UTC
Add a function to write an interrupt descriptor to the generated ACPI
code.

Signed-off-by: Simon Glass <sjg at chromium.org>
Reviewed-by: Wolfgang Wallner <wolfgang.wallner at br-automation.com>
---

(no changes since v1)

 include/acpi/acpi_device.h |  15 +++++
 lib/acpi/acpi_device.c     | 118 +++++++++++++++++++++++++++++++++++++
 test/dm/acpigen.c          |  31 ++++++++++
 3 files changed, 164 insertions(+)

Comments

Bin Meng June 28, 2020, 9:55 a.m. UTC | #1
Hi Simon,

On Sun, Jun 14, 2020 at 10:55 AM Simon Glass <sjg at chromium.org> wrote:
>
> Add a function to write an interrupt descriptor to the generated ACPI
> code.
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> Reviewed-by: Wolfgang Wallner <wolfgang.wallner at br-automation.com>
> ---
>
> (no changes since v1)
>
>  include/acpi/acpi_device.h |  15 +++++
>  lib/acpi/acpi_device.c     | 118 +++++++++++++++++++++++++++++++++++++
>  test/dm/acpigen.c          |  31 ++++++++++
>  3 files changed, 164 insertions(+)
>
> diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
> index 24895de0da..4f87cd003a 100644
> --- a/include/acpi/acpi_device.h
> +++ b/include/acpi/acpi_device.h
> @@ -11,6 +11,8 @@
>
>  #include <linux/bitops.h>
>
> +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 0 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..94f37b8d73 100644
> --- a/lib/acpi/acpi_device.c
> +++ b/lib/acpi/acpi_device.c
> @@ -8,8 +8,10 @@
>
>  #include <common.h>
>  #include <dm.h>
> +#include <irq.h>
>  #include <log.h>
>  #include <acpi/acpi_device.h>
> +#include <acpi/acpigen.h>
>  #include <dm/acpi.h>
>
>  /**
> @@ -86,3 +88,119 @@ enum acpi_dev_status acpi_device_status(const struct udevice *dev)
>  {
>         return ACPI_DSTATUS_ALL_ON;
>  }
> +
> +/**
> + * acpi_device_write_zero_len() - Write a placeholder word value
> + *
> + * @return pointer to the zero word (for fixing up later)

This function name seems to be generic. Are all lengths in various
ACPI objects following the same 2 bytes convention to represent
length?

Per the ACPI spec, this seems to only apply to Large Resource Data
Type, so we should probably rename this API to reflect this.

> + */
> +static void *acpi_device_write_zero_len(struct acpi_ctx *ctx)
> +{
> +       u8 *p = acpigen_get_current(ctx);
> +
> +       acpigen_emit_word(ctx, 0);
> +
> +       return p;
> +}
> +
> +/**
> + * acpi_device_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 acpi_device_write_zero_len().
> + *
> + * @ptr: Word to update
> + * @start: Start address to count from to calculated the length
> + */
> +static void acpi_device_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;
> +}
> +
> +/**
> + * acpi_device_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_fill_from_len() passing @ptr + 2 as @start
> + *
> + * @ptr: Word to update.
> + */
> +static void acpi_device_fill_len(struct acpi_ctx *ctx, void *ptr)
> +{
> +       acpi_device_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 = acpi_device_write_zero_len(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) */
> +       acpi_device_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 c353408021..4ee5c9cfe2 100644
> --- a/test/dm/acpigen.c
> +++ b/test/dm/acpigen.c
> @@ -8,8 +8,10 @@
>
>  #include <common.h>
>  #include <dm.h>
> +#include <irq.h>
>  #include <malloc.h>
>  #include <acpi/acpigen.h>
> +#include <acpi/acpi_device.h>
>  #include <asm/unaligned.h>
>  #include <dm/acpi.h>
>  #include <dm/test.h>
> @@ -67,3 +69,32 @@ 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));
> +
> +       ut_assertok(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);
> --

Regards,
Bin
diff mbox series

Patch

diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
index 24895de0da..4f87cd003a 100644
--- a/include/acpi/acpi_device.h
+++ b/include/acpi/acpi_device.h
@@ -11,6 +11,8 @@ 
 
 #include <linux/bitops.h>
 
+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 0 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..94f37b8d73 100644
--- a/lib/acpi/acpi_device.c
+++ b/lib/acpi/acpi_device.c
@@ -8,8 +8,10 @@ 
 
 #include <common.h>
 #include <dm.h>
+#include <irq.h>
 #include <log.h>
 #include <acpi/acpi_device.h>
+#include <acpi/acpigen.h>
 #include <dm/acpi.h>
 
 /**
@@ -86,3 +88,119 @@  enum acpi_dev_status acpi_device_status(const struct udevice *dev)
 {
 	return ACPI_DSTATUS_ALL_ON;
 }
+
+/**
+ * acpi_device_write_zero_len() - Write a placeholder word value
+ *
+ * @return pointer to the zero word (for fixing up later)
+ */
+static void *acpi_device_write_zero_len(struct acpi_ctx *ctx)
+{
+	u8 *p = acpigen_get_current(ctx);
+
+	acpigen_emit_word(ctx, 0);
+
+	return p;
+}
+
+/**
+ * acpi_device_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 acpi_device_write_zero_len().
+ *
+ * @ptr: Word to update
+ * @start: Start address to count from to calculated the length
+ */
+static void acpi_device_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;
+}
+
+/**
+ * acpi_device_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_fill_from_len() passing @ptr + 2 as @start
+ *
+ * @ptr: Word to update.
+ */
+static void acpi_device_fill_len(struct acpi_ctx *ctx, void *ptr)
+{
+	acpi_device_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 = acpi_device_write_zero_len(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) */
+	acpi_device_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 c353408021..4ee5c9cfe2 100644
--- a/test/dm/acpigen.c
+++ b/test/dm/acpigen.c
@@ -8,8 +8,10 @@ 
 
 #include <common.h>
 #include <dm.h>
+#include <irq.h>
 #include <malloc.h>
 #include <acpi/acpigen.h>
+#include <acpi/acpi_device.h>
 #include <asm/unaligned.h>
 #include <dm/acpi.h>
 #include <dm/test.h>
@@ -67,3 +69,32 @@  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));
+
+	ut_assertok(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);