[PATCH v2 02/35] acpi: Add a function to get a device path and scope

Bin Meng bmeng.cn at gmail.com
Sun May 17 16:54:57 CEST 2020


On Mon, May 11, 2020 at 4:34 AM Simon Glass <sjg at chromium.org> wrote:
>
> Add a function to build up the ACPI path for a device and another for its
> scope.
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> Reviewed-by: Wolfgang Wallner <wolfgang.wallner at br-automation.com>
> ---
>
> Changes in v2: None
> Changes in v1:
> - Split into more patches for review
> - Add tests
> - Rebase on top of common.h series
>
>  arch/sandbox/dts/test.dts  |  3 ++
>  include/acpi/acpi_device.h | 44 ++++++++++++++++++
>  lib/acpi/Makefile          |  1 +
>  lib/acpi/acpi_device.c     | 83 +++++++++++++++++++++++++++++++++
>  test/dm/acpi.c             | 95 ++++++++++++++++++++++++++++++++------
>  5 files changed, 213 insertions(+), 13 deletions(-)
>  create mode 100644 include/acpi/acpi_device.h
>  create mode 100644 lib/acpi/acpi_device.c
>
> diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts
> index 15cd2330a3..b802c1c909 100644
> --- a/arch/sandbox/dts/test.dts
> +++ b/arch/sandbox/dts/test.dts
> @@ -253,6 +253,9 @@
>
>         acpi-test {
>                 compatible = "denx,u-boot-acpi-test";
> +               child {
> +                       compatible = "denx,u-boot-acpi-test";
> +               };
>         };
>
>         acpi-test2 {
> diff --git a/include/acpi/acpi_device.h b/include/acpi/acpi_device.h
> new file mode 100644
> index 0000000000..37a675f101
> --- /dev/null
> +++ b/include/acpi/acpi_device.h
> @@ -0,0 +1,44 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +/*
> + * Generation of tables for particular device types
> + *
> + * Copyright 2019 Google LLC
> + * Mostly taken from coreboot file of the same name
> + */
> +
> +#ifndef __ACPI_DEVICE_H
> +#define __ACPI_DEVICE_H
> +
> +struct udevice;
> +
> +/* Length of a full path to an ACPI device */
> +#define ACPI_PATH_MAX          30
> +
> +/**
> + * acpi_device_path() - Get the full path to an ACPI device
> + *
> + * This gets the full path in the form XXXX.YYYY.ZZZZ where XXXX is the root
> + * and ZZZZ is the device. All parent devices are added to the path.
> + *
> + * @dev: Device to check
> + * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
> + * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
> + * @return 0 if OK, -ve on error
> + */
> +int acpi_device_path(const struct udevice *dev, char *buf, int maxlen);
> +
> +/**
> + * acpi_device_scope() - Get the scope of an ACPI device
> + *
> + * This gets the scope which is the full path of the parent device, as per
> + * acpi_device_path().
> + *
> + * @dev: Device to check
> + * @buf: Buffer to place the path in (should be ACPI_PATH_MAX long)
> + * @maxlen: Size of buffer (typically ACPI_PATH_MAX)
> + * @return 0 if OK, -EINVAL if the device has no parent, other -ve on other
> + *     error
> + */
> +int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen);
> +
> +#endif
> diff --git a/lib/acpi/Makefile b/lib/acpi/Makefile
> index 660491ef71..caae6c01bd 100644
> --- a/lib/acpi/Makefile
> +++ b/lib/acpi/Makefile
> @@ -1,4 +1,5 @@
>  # SPDX-License-Identifier: GPL-2.0+
>  #
>
> +obj-y += acpi_device.o
>  obj-y += acpi_table.o
> diff --git a/lib/acpi/acpi_device.c b/lib/acpi/acpi_device.c
> new file mode 100644
> index 0000000000..f9af2343c1
> --- /dev/null
> +++ b/lib/acpi/acpi_device.c
> @@ -0,0 +1,83 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Generation of tables for particular device types
> + *
> + * Copyright 2019 Google LLC
> + * Mostly taken from coreboot file of the same name
> + */
> +
> +#include <common.h>
> +#include <dm.h>
> +#include <log.h>
> +#include <acpi/acpi_device.h>
> +#include <dm/acpi.h>
> +
> +/**
> + * acpi_device_path_fill() - Find the root device and build a path from there
> + *
> + * This recursively reaches back to the root device and progressively adds path
> + * elements until the device is reached.
> + *
> + * @dev: Device to return path of
> + * @buf: Buffer to hold the path
> + * @buf_len: Length of buffer
> + * @cur: Current position in the buffer
> + * @return new position in buffer after adding @dev, or -ve on error
> + */
> +static int acpi_device_path_fill(const struct udevice *dev, char *buf,
> +                                size_t buf_len, int cur)
> +{
> +       char name[ACPI_NAME_MAX];
> +       int next = 0;
> +       int ret;
> +
> +       ret = acpi_get_name(dev, name);
> +       if (ret)
> +               return ret;
> +
> +       /*
> +        * Make sure this name segment will fit, including the path segment
> +        * separator and possible NUL terminator, if this is the last segment.

typo: NULL

> +        */
> +       if (cur + strlen(name) + 2 > buf_len)
> +               return -ENOSPC;
> +
> +       /* Walk up the tree to the root device */
> +       if (dev_get_parent(dev)) {
> +               next = acpi_device_path_fill(dev_get_parent(dev), buf, buf_len,
> +                                            cur);
> +               if (next < 0)
> +                       return next;
> +       }
> +
> +       /* Fill in the path from the root device */
> +       next += snprintf(buf + next, buf_len - next, "%s%s",
> +                        dev_get_parent(dev) && *name ? "." : "", name);
> +
> +       return next;
> +}
> +

[snip]

Reviewed-by: Bin Meng <bmeng.cn at gmail.com>


More information about the U-Boot mailing list