acpigen: Support writing a length
It is convenient to write a length value for preceding a block of data. Of course the length is not known or is hard to calculate a priori. So add a way to mark the start on a stack, so the length can be updated when known. Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-by: Wolfgang Wallner <wolfgang.wallner@br-automation.com> Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
This commit is contained in:
parent
70e5e67a4d
commit
7e148f2ed3
@ -14,6 +14,9 @@
|
||||
|
||||
struct acpi_ctx;
|
||||
|
||||
/* Top 4 bits of the value used to indicate a three-byte length value */
|
||||
#define ACPI_PKG_LEN_3_BYTES 0x80
|
||||
|
||||
/**
|
||||
* acpigen_get_current() - Get the current ACPI code output pointer
|
||||
*
|
||||
@ -65,4 +68,42 @@ void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size);
|
||||
*/
|
||||
void acpigen_emit_string(struct acpi_ctx *ctx, const char *str);
|
||||
|
||||
/**
|
||||
* acpigen_write_len_f() - Write a 'forward' length placeholder
|
||||
*
|
||||
* This adds space for a length value in the ACPI stream and pushes the current
|
||||
* position (before the length) on the stack. After calling this you can write
|
||||
* some data and then call acpigen_pop_len() to update the length value.
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* acpigen_write_len_f() ------\
|
||||
* acpigen_write...() |
|
||||
* acpigen_write...() |
|
||||
* acpigen_write_len_f() --\ |
|
||||
* acpigen_write...() | |
|
||||
* acpigen_write...() | |
|
||||
* acpigen_pop_len() ------/ |
|
||||
* acpigen_write...() |
|
||||
* acpigen_pop_len() ----------/
|
||||
*
|
||||
* See ACPI 6.3 section 20.2.4 Package Length Encoding
|
||||
*
|
||||
* This implementation always uses a 3-byte packet length for simplicity. It
|
||||
* could be adjusted to support other lengths.
|
||||
*
|
||||
* @ctx: ACPI context pointer
|
||||
*/
|
||||
void acpigen_write_len_f(struct acpi_ctx *ctx);
|
||||
|
||||
/**
|
||||
* acpigen_pop_len() - Update the previously stacked length placeholder
|
||||
*
|
||||
* Call this after the data for the block has been written. It updates the
|
||||
* top length value in the stack and pops it off.
|
||||
*
|
||||
* @ctx: ACPI context pointer
|
||||
*/
|
||||
void acpigen_pop_len(struct acpi_ctx *ctx);
|
||||
|
||||
#endif
|
||||
|
@ -16,12 +16,15 @@
|
||||
#define ACPI_OPS_PTR(_ptr)
|
||||
#endif
|
||||
|
||||
/* Length of an ACPI name string, excluding nul terminator */
|
||||
/* Length of an ACPI name string, excluding null terminator */
|
||||
#define ACPI_NAME_LEN 4
|
||||
|
||||
/* Length of an ACPI name string including nul terminator */
|
||||
#define ACPI_NAME_MAX (ACPI_NAME_LEN + 1)
|
||||
|
||||
/* Number of nested objects supported */
|
||||
#define ACPIGEN_LENSTACK_SIZE 10
|
||||
|
||||
#if !defined(__ACPI__)
|
||||
|
||||
/**
|
||||
@ -35,6 +38,8 @@
|
||||
* adding a new table. The RSDP holds pointers to the RSDT and XSDT.
|
||||
* @rsdt: Pointer to the Root System Description Table
|
||||
* @xsdt: Pointer to the Extended System Description Table
|
||||
* @len_stack: Stack of 'length' words to fix up later
|
||||
* @ltop: Points to current top of stack (0 = empty)
|
||||
*/
|
||||
struct acpi_ctx {
|
||||
void *base;
|
||||
@ -42,6 +47,8 @@ struct acpi_ctx {
|
||||
struct acpi_rsdp *rsdp;
|
||||
struct acpi_rsdt *rsdt;
|
||||
struct acpi_xsdt *xsdt;
|
||||
char *len_stack[ACPIGEN_LENSTACK_SIZE];
|
||||
int ltop;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -10,6 +10,7 @@
|
||||
|
||||
#include <common.h>
|
||||
#include <dm.h>
|
||||
#include <log.h>
|
||||
#include <acpi/acpigen.h>
|
||||
#include <dm/acpi.h>
|
||||
|
||||
@ -38,6 +39,38 @@ void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
|
||||
acpigen_emit_byte(ctx, (data >> 24) & 0xff);
|
||||
}
|
||||
|
||||
/*
|
||||
* Maximum length for an ACPI object generated by this code,
|
||||
*
|
||||
* If you need to change this, change acpigen_write_len_f(ctx) and
|
||||
* acpigen_pop_len(ctx)
|
||||
*/
|
||||
#define ACPIGEN_MAXLEN 0xfffff
|
||||
|
||||
void acpigen_write_len_f(struct acpi_ctx *ctx)
|
||||
{
|
||||
assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
|
||||
ctx->len_stack[ctx->ltop++] = ctx->current;
|
||||
acpigen_emit_byte(ctx, 0);
|
||||
acpigen_emit_byte(ctx, 0);
|
||||
acpigen_emit_byte(ctx, 0);
|
||||
}
|
||||
|
||||
void acpigen_pop_len(struct acpi_ctx *ctx)
|
||||
{
|
||||
int len;
|
||||
char *p;
|
||||
|
||||
assert(ctx->ltop > 0);
|
||||
p = ctx->len_stack[--ctx->ltop];
|
||||
len = ctx->current - (void *)p;
|
||||
assert(len <= ACPIGEN_MAXLEN);
|
||||
/* generate store length for 0xfffff max */
|
||||
p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
|
||||
p[1] = len >> 4 & 0xff;
|
||||
p[2] = len >> 12 & 0xff;
|
||||
}
|
||||
|
||||
void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
|
||||
{
|
||||
int i;
|
||||
|
@ -25,7 +25,7 @@
|
||||
#define TEST_STRING "frogmore"
|
||||
#define TEST_STREAM2 "\xfa\xde"
|
||||
|
||||
static int alloc_context(struct acpi_ctx **ctxp)
|
||||
static int alloc_context_size(struct acpi_ctx **ctxp, int size)
|
||||
{
|
||||
struct acpi_ctx *ctx;
|
||||
|
||||
@ -33,17 +33,23 @@ static int alloc_context(struct acpi_ctx **ctxp)
|
||||
ctx = malloc(sizeof(*ctx));
|
||||
if (!ctx)
|
||||
return -ENOMEM;
|
||||
ctx->base = malloc(ACPI_CONTEXT_SIZE);
|
||||
ctx->base = malloc(size);
|
||||
if (!ctx->base) {
|
||||
free(ctx);
|
||||
return -ENOMEM;
|
||||
}
|
||||
ctx->ltop = 0;
|
||||
ctx->current = ctx->base;
|
||||
*ctxp = ctx;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int alloc_context(struct acpi_ctx **ctxp)
|
||||
{
|
||||
return alloc_context_size(ctxp, ACPI_CONTEXT_SIZE);
|
||||
}
|
||||
|
||||
static void free_context(struct acpi_ctx **ctxp)
|
||||
{
|
||||
free((*ctxp)->base);
|
||||
@ -344,3 +350,57 @@ static int dm_test_acpi_spi(struct unit_test_state *uts)
|
||||
return 0;
|
||||
}
|
||||
DM_TEST(dm_test_acpi_spi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
|
||||
|
||||
/**
|
||||
* get_length() - decode a three-byte length field
|
||||
*
|
||||
* @ptr: Length encoded as per ACPI
|
||||
* @return decoded length, or -EINVAL on error
|
||||
*/
|
||||
static int get_length(u8 *ptr)
|
||||
{
|
||||
if (!(*ptr & 0x80))
|
||||
return -EINVAL;
|
||||
|
||||
return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12;
|
||||
}
|
||||
|
||||
/* Test emitting a length */
|
||||
static int dm_test_acpi_len(struct unit_test_state *uts)
|
||||
{
|
||||
const int size = 0xc0000;
|
||||
struct acpi_ctx *ctx;
|
||||
u8 *ptr;
|
||||
int i;
|
||||
|
||||
ut_assertok(alloc_context_size(&ctx, size));
|
||||
|
||||
ptr = acpigen_get_current(ctx);
|
||||
|
||||
/* Write a byte and a 3-byte length */
|
||||
acpigen_write_len_f(ctx);
|
||||
acpigen_emit_byte(ctx, 0x23);
|
||||
acpigen_pop_len(ctx);
|
||||
ut_asserteq(1 + 3, get_length(ptr));
|
||||
|
||||
/* Write 200 bytes so we need two length bytes */
|
||||
ptr = ctx->current;
|
||||
acpigen_write_len_f(ctx);
|
||||
for (i = 0; i < 200; i++)
|
||||
acpigen_emit_byte(ctx, 0x23);
|
||||
acpigen_pop_len(ctx);
|
||||
ut_asserteq(200 + 3, get_length(ptr));
|
||||
|
||||
/* Write 40KB so we need three length bytes */
|
||||
ptr = ctx->current;
|
||||
acpigen_write_len_f(ctx);
|
||||
for (i = 0; i < 40000; i++)
|
||||
acpigen_emit_byte(ctx, 0x23);
|
||||
acpigen_pop_len(ctx);
|
||||
ut_asserteq(40000 + 3, get_length(ptr));
|
||||
|
||||
free_context(&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
DM_TEST(dm_test_acpi_len, 0);
|
||||
|
Loading…
Reference in New Issue
Block a user