[PATCH v1 22/43] x86: Add support for building up an NHLT structure

Wolfgang Wallner wolfgang.wallner at br-automation.com
Thu Jul 2 10:11:00 CEST 2020


Hi Simon,

I dont know NHLT well enough to actually review the code, but I did compare
the files in this patch to the version in coreboot. Most of the changes are
obvious (coding style, spelling, ...), but some things are not, see the remarks
below.

-----"Simon Glass" <sjg at chromium.org> schrieb: -----
> Betreff: [PATCH v1 22/43] x86: Add support for building up an NHLT structure
> 
> The Intel Non-High-Definition-Audio Link Table (NHLT) table describes the
> audio codecs and connections in a system. Various devices can contribute
> information to produce the table.
> 
> Add functions to allow adding to the structure that is eventually written
> to the ACPI tables. Also add the device-tree bindings.
> 
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
> 
> Changes in v1:
> - Add a new patch to support building up an NHLT structure
> 
>  arch/x86/include/asm/acpi_nhlt.h | 314 ++++++++++++++++++++
>  arch/x86/lib/Makefile            |   1 +
>  arch/x86/lib/acpi_nhlt.c         | 482 +++++++++++++++++++++++++++++++
>  3 files changed, 797 insertions(+)
>  create mode 100644 arch/x86/include/asm/acpi_nhlt.h
>  create mode 100644 arch/x86/lib/acpi_nhlt.c
> 
> diff --git a/arch/x86/include/asm/acpi_nhlt.h b/arch/x86/include/asm/acpi_nhlt.h
> new file mode 100644
> index 0000000000..4d2573d5ff
> --- /dev/null
> +++ b/arch/x86/include/asm/acpi_nhlt.h
> @@ -0,0 +1,314 @@
> +/* SPDX-License-Identifier: GPL-2.0+ */
> +/*
> + * Copyright 2020 Google LLC
> + *
> + * Modified from coreboot nhlt.h
> + */
> +
> +#ifndef _NHLT_H_
> +#define _NHLT_H_
> +
> +struct acpi_ctx;
> +struct nhlt;
> +struct nhlt_endpoint;
> +struct nhlt_format;
> +struct nhlt_format_config;
> +
> +/*
> + * Non HD Audio ACPI support. This table is typically used for Intel Smart
> + * Sound Technology DSP. It provides a way to encode opaque settings in
> + * the ACPI tables.
> + *
> + * While the structure fields of the NHLT structs are exposed below
> + * the SoC/chipset code should be the only other user manipulating the
> + * fields directly aside from the library itself.
> + *
> + * The NHLT table consists of endpoints which in turn contain different
> + * supporting stream formats. Each endpoint may contain a device specific
> + * configuration payload as well as each stream format.
> + *
> + * Most code should use the SoC variants of the functions because
> + * there is required logic needed to be performed by the SoC. The SoC
> + * code should be abstracting the inner details of these functions that
> + * specically apply to NHLT objects for that SoC.
> + *
> + * An example sequence:
> + *
> + * nhlt = nhlt_init()
> + * ep = nhlt_add_endpoint()
> + * nhlt_endpoint_append_config(ep)
> + * nhlt_endpoint_add_formats(ep)
> + * nhlt_soc_serialise()
> + */
> +
> +/* Obtain an nhlt object for adding endpoints. Returns NULL on error. */
> +struct nhlt *nhlt_init(void);
> +
> +/* Return the size of the NHLT table including ACPI header. */
> +size_t nhlt_current_size(struct nhlt *nhlt);
> +
> +/*
> + * Helper functions for adding NHLT devices utilizing an nhlt_endp_descriptor
> + * to drive the logic.
> + */
> +
> +struct nhlt_endp_descriptor {
> +	/* NHLT endpoint types. */
> +	int link;
> +	int device;
> +	int direction;
> +	u16 vid;
> +	u16 did;
> +	/* Optional endpoint specific configuration data. */
> +	const void *cfg;
> +	size_t cfg_size;
> +	/* Formats supported for endpoint. */
> +	const struct nhlt_format_config *formats;
> +	size_t num_formats;
> +};
> +
> +/*
> + * Add the number of endpoints described by each descriptor. The virtual bus
> + * id for each descriptor is the default value of 0.
> + * Returns < 0 on error, 0 on success.
> + */
> +int nhlt_add_endpoints(struct nhlt *nhlt,
> +		       const struct nhlt_endp_descriptor *epds,
> +		       size_t num_epds);
> +
> +/*
> + * Add the number of endpoints associated with a single NHLT SSP instance id.
> + * Each endpoint described in the endpoint descriptor array uses the provided
> + * virtual bus id. Returns < 0 on error, 0 on success.
> + */
> +int nhlt_add_ssp_endpoints(struct nhlt *nhlt, int virtual_bus_id,
> +			   const struct nhlt_endp_descriptor *epds,
> +			   size_t num_epds);
> +
> +/*
> + * Add endpoint to NHLT object. Returns NULL on error.
> + *
> + * generic nhlt_add_endpoint() is called by the SoC code to provide
> + * the specific assumptions/uses for NHLT for that platform. All fields
> + * are the NHLT enumerations found within this header file.
> + */
> +struct nhlt_endpoint *nhlt_add_endpoint(struct nhlt *nhlt, int link_type,
> +					int device_type, int dir,
> +					u16 vid, u16 did);
> +
> +/*
> + * Append blob of configuration to the endpoint proper. Returns 0 on
> + * success, < 0 on error. A copy of the configuration is made so any
> + * resources pointed to by config can be freed after the call.
> + */
> +int nhlt_endpoint_append_config(struct nhlt_endpoint *endpoint,
> +				const void *config, size_t config_sz);
> +
> +/* Add a format type to the provided endpoint. Returns NULL on error. */
> +struct nhlt_format *nhlt_add_format(struct nhlt_endpoint *endpoint,
> +				    int num_channels, int sample_freq_khz,
> +				    int container_bits_per_sample,
> +				    int valid_bits_per_sample,
> +				    u32 speaker_mask);
> +
> +/*
> + * Append blob of configuration to the format proper. Returns 0 on
> + * success, < 0 on error. A copy of the configuration is made so any
> + * resources pointed to by config can be freed after the call.
> + */
> +int nhlt_format_append_config(struct nhlt_format *format, const void *config,
> +			      size_t config_sz);
> +
> +/*
> + * Add num_formats described by formats to the endpoint. This function
> + * effectively wraps nhlt_add_format() and nhlt_format_config() using the
> + * data found in each nhlt_format_config object. Returns 0 on success, < 0
> + * on error.
> + */
> +int nhlt_endpoint_add_formats(struct nhlt_endpoint *endpoint,
> +			      const struct nhlt_format_config *formats,
> +			      size_t num_formats);
> +
> +/*
> + * Increment the instance id for a given link type. This function is
> + * used for marking a device being completely added to the NHLT object.
> + * Subsequent endpoints added to the nhlt object with the same link type
> + * will use incremented instance id.
> + */
> +void nhlt_next_instance(struct nhlt *nhlt, int link_type);
> +
> +/*
> + * Serialize NHLT object to ACPI table. Take in the beginning address of where
> + * the table will reside and return the address of the next ACPI table. On
> + * error 0 will be returned. The NHLT object is no longer valid after this
> + * function is called.
> + */
> +uintptr_t nhlt_serialise(struct nhlt *nhlt, uintptr_t acpi_addr);

The implementation for this functions seems to be missing in this patch.
In coreboot it looks like this:

uintptr_t nhlt_serialize(struct nhlt *nhlt, uintptr_t acpi_addr)
{
	return nhlt_serialize_oem_overrides(nhlt, acpi_addr, NULL, NULL, 0);
}

> +
> +/*
> + * Serialize NHLT object to ACPI table. Take in the beginning address of where
> + * the table will reside oem_id and oem_table_id and return the address of the
> + * next ACPI table. On error 0 will be returned. The NHLT object is no longer
> + * valid after this function is called.
> + */
> +int nhlt_serialise_oem_overrides(struct acpi_ctx *ctx, struct nhlt *nhlt,
> +				 const char *oem_id, const char *oem_table_id,
> +				 u32 oem_revision);
> +
> +int nhlt_setup(struct nhlt *nhlt, ofnode node);
> +
> +/* Link and device types. */
> +enum {
> +	NHLT_LINK_HDA,
> +	NHLT_LINK_DSP,
> +	NHLT_LINK_PDM,
> +	NHLT_LINK_SSP,
> +	NHLT_MAX_LINK_TYPES,
> +};
> +
> +enum {
> +	NHLT_SSP_DEV_BT, /* Bluetooth */
> +	NHLT_SSP_DEV_MODEM,
> +	NHLT_SSP_DEV_FM,
> +	NHLT_SSP_DEV_RESERVED,
> +	NHLT_SSP_DEV_I2S = 4,
> +};
> +
> +enum {
> +	NHLT_PDM_DEV,
> +};
> +
> +/* Endpoint direction. */
> +enum {
> +	NHLT_DIR_RENDER,
> +	NHLT_DIR_CAPTURE,
> +	NHLT_DIR_BIDIRECTIONAL,
> +};
> +
> +/*
> + * Channel mask for an endpoint. While they are prefixed with 'SPEAKER' the
> + * channel masks are also used for capture devices
> + */
> +enum {
> +	SPEAKER_FRONT_LEFT = 1 << 0,

Nit: BIT(0) ?

> +	SPEAKER_FRONT_RIGHT = 1 << 1,
> +	SPEAKER_FRONT_CENTER = 1 << 2,
> +	SPEAKER_LOW_FREQUENCY = 1 << 3,
> +	SPEAKER_BACK_LEFT = 1 << 4,
> +	SPEAKER_BACK_RIGHT = 1 << 5,
> +	SPEAKER_FRONT_LEFT_OF_CENTER = 1 << 6,
> +	SPEAKER_FRONT_RIGHT_OF_CENTER = 1 << 7,
> +	SPEAKER_BACK_CENTER = 1 << 8,
> +	SPEAKER_SIDE_LEFT = 1 << 9,
> +	SPEAKER_SIDE_RIGHT = 1 << 10,
> +	SPEAKER_TOP_CENTER = 1 << 11,
> +	SPEAKER_TOP_FRONT_LEFT = 1 << 12,
> +	SPEAKER_TOP_FRONT_CENTER = 1 << 13,
> +	SPEAKER_TOP_FRONT_RIGHT = 1 << 14,
> +	SPEAKER_TOP_BACK_LEFT = 1 << 15,
> +	SPEAKER_TOP_BACK_CENTER = 1 << 16,
> +	SPEAKER_TOP_BACK_RIGHT = 1 << 17,
> +};
> +
> +/*
> + * Supporting structures. Only SoC/chipset and the library code directly should
> + * be manipulating these structures
> + */
> +struct sub_format {
> +	u32 data1;
> +	u16 data2;
> +	u16 data3;
> +	u8 data4[8];
> +};
> +
> +struct nhlt_specific_config {
> +	u32 size;
> +	void *capabilities;
> +};
> +
> +struct nhlt_waveform {
> +	u16 tag;
> +	u16 num_channels;
> +	u32 samples_per_second;
> +	u32 bytes_per_second;
> +	u16 block_align;
> +	u16 bits_per_sample;
> +	u16 extra_size;
> +	u16 valid_bits_per_sample;
> +	u32 channel_mask;
> +	struct sub_format sub_format;
> +};
> +
> +struct nhlt_format {
> +	struct nhlt_waveform waveform;
> +	struct nhlt_specific_config config;
> +};
> +
> +/*
> + * This struct is used by nhlt_endpoint_add_formats() for easily adding
> + * waveform formats with associated settings file.
> + */
> +struct nhlt_format_config {
> +	int num_channels;
> +	int sample_freq_khz;
> +	int container_bits_per_sample;
> +	int valid_bits_per_sample;
> +	u32 speaker_mask;
> +	const char *settings_file;
> +};
> +
> +/* Arbitrary max number of formats per endpoint. */
> +#define MAX_FORMATS 2
> +struct nhlt_endpoint {
> +	u32 length;
> +	u8 link_type;
> +	u8 instance_id;
> +	u16 vendor_id;
> +	u16 device_id;
> +	u16 revision_id;
> +	u32 subsystem_id;
> +	u8 device_type;
> +	u8 direction;
> +	u8 virtual_bus_id;
> +	struct nhlt_specific_config config;
> +	u8 num_formats;
> +	struct nhlt_format formats[MAX_FORMATS];
> +};
> +
> +#define MAX_ENDPOINTS 8
> +struct nhlt {
> +	u32 subsystem_id;
> +	u8 num_endpoints;
> +	struct nhlt_endpoint endpoints[MAX_ENDPOINTS];
> +	u8 current_instance_id[NHLT_MAX_LINK_TYPES];
> +};
> +
> +struct nhlt_tdm_config {
> +	u8 virtual_slot;
> +	u8 config_type;
> +};
> +
> +enum {
> +	NHLT_TDM_BASIC,
> +	NHLT_TDM_MIC_ARRAY,
> +};
> +
> +struct nhlt_dmic_array_config {
> +	struct nhlt_tdm_config tdm_config;
> +	u8 array_type;
> +};
> +
> +/*
> + * Microphone array definitions may be found here:
> + * https://msdn.microsoft.com/en-us/library/windows/hardware/dn613960%28v=vs.85%29.aspx
> + */
> +enum {
> +	NHLT_MIC_ARRAY_2CH_SMALL = 0xa,
> +	NHLT_MIC_ARRAY_2CH_BIG = 0xb,
> +	NHLT_MIC_ARRAY_4CH_1ST_GEOM = 0xc,
> +	NHLT_MIC_ARRAY_4CH_L_SHAPED = 0xd,
> +	NHLT_MIC_ARRAY_4CH_2ND_GEOM = 0xe,
> +	NHLT_MIC_ARRAY_VENDOR_DEFINED = 0xf,
> +};
> +
> +#endif
> diff --git a/arch/x86/lib/Makefile b/arch/x86/lib/Makefile
> index 5cd4587480..4f3e9ad6b6 100644
> --- a/arch/x86/lib/Makefile
> +++ b/arch/x86/lib/Makefile
> @@ -23,6 +23,7 @@ obj-y	+= interrupts.o
>  obj-y	+= lpc-uclass.o
>  obj-y	+= mpspec.o
>  obj-$(CONFIG_ENABLE_MRC_CACHE) += mrccache.o
> +obj-$(CONFIG_$(SPL_TPL_)ACPIGEN) += acpi_nhlt.o
>  obj-y	+= northbridge-uclass.o
>  obj-$(CONFIG_I8259_PIC) += i8259.o
>  obj-$(CONFIG_I8254_TIMER) += i8254.o
> diff --git a/arch/x86/lib/acpi_nhlt.c b/arch/x86/lib/acpi_nhlt.c
> new file mode 100644
> index 0000000000..c64dd9c008
> --- /dev/null
> +++ b/arch/x86/lib/acpi_nhlt.c
> @@ -0,0 +1,482 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright 2020 Google LLC
> + *
> + * Modified from coreboot nhlt.c
> + */
> +
> +#define LOG_CATEGORY	LOGC_ACPI
> +
> +#include <common.h>
> +#include <binman.h>
> +#include <dm.h>
> +#include <log.h>
> +#include <malloc.h>
> +#include <tables_csum.h>
> +#include <acpi/acpi_table.h>
> +#include <asm/acpi_nhlt.h>
> +#include <asm/unaligned.h>
> +#include <dm/acpi.h>
> +
> +#define NHLT_RID		1
> +#define NHLT_SSID		1
> +#define WAVEFORMAT_TAG		0xfffe
> +#define DEFAULT_VIRTUAL_BUS_ID	0
> +
> +static const struct sub_format pcm_subformat = {
> +	.data1 = 0x00000001,
> +	.data2 = 0x0000,
> +	.data3 = 0x0010,
> +	.data4 = { 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71 },
> +};
> +
> +struct nhlt *nhlt_init(void)
> +{
> +	struct nhlt *nhlt;
> +
> +	nhlt = malloc(sizeof(*nhlt));
> +
> +	if (!nhlt)
> +		return NULL;
> +
> +	memset(nhlt, 0, sizeof(*nhlt));
> +	nhlt->subsystem_id = NHLT_SSID;
> +
> +	return nhlt;
> +}
> +
> +struct nhlt_endpoint *nhlt_add_endpoint(struct nhlt *nhlt, int link_type,
> +					int device_type, int dir,
> +					u16 vid, u16 did)
> +{
> +	struct nhlt_endpoint *endp;
> +
> +	if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
> +		return NULL;
> +
> +	if (nhlt->num_endpoints >= MAX_ENDPOINTS)
> +		return NULL;
> +
> +	endp = &nhlt->endpoints[nhlt->num_endpoints];
> +
> +	endp->link_type = link_type;
> +	endp->instance_id = nhlt->current_instance_id[link_type];
> +	endp->vendor_id = vid;
> +	endp->device_id = did;
> +	endp->revision_id = NHLT_RID;
> +	endp->subsystem_id = nhlt->subsystem_id;
> +	endp->device_type = device_type;
> +	endp->direction = dir;
> +	endp->virtual_bus_id = DEFAULT_VIRTUAL_BUS_ID;
> +
> +	nhlt->num_endpoints++;
> +
> +	return endp;
> +}
> +
> +static int append_specific_config(struct nhlt_specific_config *spec_cfg,
> +				  const void *config, size_t config_sz)
> +{
> +	size_t new_sz;
> +	void *new_cfg;
> +

In coreboot this function starts with a check of config and config_sz:

if (config == NULL || config_sz == 0)
	return 0;

Why is this check left out in this implementation?

> +	new_sz = spec_cfg->size + config_sz;
> +	new_cfg = malloc(new_sz);
> +	if (!new_cfg)
> +		return -ENOMEM;
> +
> +	/* Append new config */
> +	memcpy(new_cfg, spec_cfg->capabilities, spec_cfg->size);
> +	memcpy(new_cfg + spec_cfg->size, config, config_sz);
> +
> +	free(spec_cfg->capabilities);
> +
> +	/* Update with new config data */
> +	spec_cfg->size = new_sz;
> +	spec_cfg->capabilities = new_cfg;
> +
> +	return 0;
> +}
> +
> +int nhlt_endpoint_append_config(struct nhlt_endpoint *endp, const void *config,
> +				size_t config_sz)
> +{
> +	return append_specific_config(&endp->config, config, config_sz);
> +}
> +
> +struct nhlt_format *nhlt_add_format(struct nhlt_endpoint *endp,
> +				    int num_channels, int sample_freq_khz,
> +				    int container_bits_per_sample,
> +				    int valid_bits_per_sample,
> +				    uint32_t speaker_mask)
> +{
> +	struct nhlt_format *fmt;
> +	struct nhlt_waveform *wave;
> +
> +	if (endp->num_formats >= MAX_FORMATS)
> +		return NULL;
> +
> +	fmt = &endp->formats[endp->num_formats];
> +	wave = &fmt->waveform;
> +
> +	wave->tag = WAVEFORMAT_TAG;
> +	wave->num_channels = num_channels;
> +	wave->samples_per_second = sample_freq_khz * 1000;
> +	wave->bits_per_sample = container_bits_per_sample;
> +	wave->extra_size = sizeof(wave->valid_bits_per_sample);
> +	wave->extra_size += sizeof(wave->channel_mask);
> +	wave->extra_size += sizeof(wave->sub_format);
> +	wave->valid_bits_per_sample = valid_bits_per_sample;
> +	wave->channel_mask = speaker_mask;
> +	memcpy(&wave->sub_format, &pcm_subformat, sizeof(wave->sub_format));
> +
> +	/* Calculate the dervied fields */
> +	wave->block_align = wave->num_channels * wave->bits_per_sample / 8;
> +	wave->bytes_per_second = wave->block_align * wave->samples_per_second;
> +
> +	endp->num_formats++;
> +
> +	return fmt;
> +}
> +
> +int nhlt_format_append_config(struct nhlt_format *fmt, const void *config,
> +			      size_t config_sz)
> +{
> +	return append_specific_config(&fmt->config, config, config_sz);
> +}
> +
> +int nhlt_endpoint_add_formats(struct nhlt_endpoint *endp,
> +			      const struct nhlt_format_config *formats,
> +			      size_t num_formats)
> +{
> +	ofnode node;
> +	size_t i;
> +
> +	node = binman_section_find_node("private-files");
> +
> +	for (i = 0; i < num_formats; i++) {
> +		const struct nhlt_format_config *cfg = &formats[i];
> +		struct nhlt_format *fmt;
> +		void *data;
> +		int size;
> +		int ret;
> +
> +		fmt = nhlt_add_format(endp, cfg->num_channels,
> +				      cfg->sample_freq_khz,
> +				      cfg->container_bits_per_sample,
> +				      cfg->valid_bits_per_sample,
> +				      cfg->speaker_mask);
> +		if (!fmt)
> +			return -ENOSPC;
> +
> +		if (!cfg->settings_file)
> +			continue;
> +
> +		ret = binman_entry_map(node, cfg->settings_file, &data, &size);
> +		if (ret) {
> +			log_warning("Failed to find settings file %s\n",
> +				    cfg->settings_file);
> +			return log_msg_ret("settings", ret);
> +		}
> +
> +		ret = nhlt_format_append_config(fmt, data, size);
> +		if (ret)
> +			return log_msg_ret("append", ret);
> +	}
> +
> +	return 0;
> +}
> +
> +void nhlt_next_instance(struct nhlt *nhlt, int link_type)
> +{
> +	if (link_type < NHLT_LINK_HDA || link_type >= NHLT_MAX_LINK_TYPES)
> +		return;
> +
> +	nhlt->current_instance_id[link_type]++;
> +}
> +
> +static size_t calc_specific_config_size(struct nhlt_specific_config *cfg)
> +{
> +	return sizeof(cfg->size) + cfg->size;
> +}
> +
> +static size_t calc_format_size(struct nhlt_format *fmt)
> +{
> +	size_t sz = 0;
> +
> +	/* Wave format first */
> +	sz += sizeof(fmt->waveform.tag);
> +	sz += sizeof(fmt->waveform.num_channels);
> +	sz += sizeof(fmt->waveform.samples_per_second);
> +	sz += sizeof(fmt->waveform.bytes_per_second);
> +	sz += sizeof(fmt->waveform.block_align);
> +	sz += sizeof(fmt->waveform.bits_per_sample);
> +	sz += sizeof(fmt->waveform.extra_size);
> +	sz += sizeof(fmt->waveform.valid_bits_per_sample);
> +	sz += sizeof(fmt->waveform.channel_mask);
> +	sz += sizeof(fmt->waveform.sub_format);
> +
> +	sz += calc_specific_config_size(&fmt->config);
> +
> +	return sz;
> +}
> +
> +static size_t calc_endpoint_size(struct nhlt_endpoint *endp)
> +{
> +	int i;
> +	size_t sz = 0;
> +
> +	sz += sizeof(endp->length) + sizeof(endp->link_type);
> +	sz += sizeof(endp->instance_id) + sizeof(endp->vendor_id);
> +	sz += sizeof(endp->device_id) + sizeof(endp->revision_id);
> +	sz += sizeof(endp->subsystem_id) + sizeof(endp->device_type);
> +	sz += sizeof(endp->direction) + sizeof(endp->virtual_bus_id);
> +	sz += calc_specific_config_size(&endp->config);
> +	sz += sizeof(endp->num_formats);
> +
> +	for (i = 0; i < endp->num_formats; i++)
> +		sz += calc_format_size(&endp->formats[i]);
> +
> +	/* Adjust endpoint length to reflect current configuration */
> +	endp->length = sz;
> +
> +	return sz;
> +}
> +
> +static size_t calc_endpoints_size(struct nhlt *nhlt)
> +{
> +	size_t sz = 0;
> +	int i;
> +
> +	for (i = 0; i < nhlt->num_endpoints; i++)
> +		sz += calc_endpoint_size(&nhlt->endpoints[i]);
> +
> +	return sz;
> +}
> +
> +static size_t calc_size(struct nhlt *nhlt)
> +{
> +	return sizeof(nhlt->num_endpoints) + calc_endpoints_size(nhlt);
> +}
> +
> +size_t nhlt_current_size(struct nhlt *nhlt)
> +{
> +	return calc_size(nhlt) + sizeof(struct acpi_table_header);
> +}
> +
> +static void nhlt_free_resources(struct nhlt *nhlt)
> +{
> +	int i, j;
> +
> +	/* Free all specific configs */
> +	for (i = 0; i < nhlt->num_endpoints; i++) {
> +		struct nhlt_endpoint *endp = &nhlt->endpoints[i];
> +
> +		free(endp->config.capabilities);
> +		for (j = 0; j < endp->num_formats; j++) {
> +			struct nhlt_format *fmt = &endp->formats[j];
> +
> +			free(fmt->config.capabilities);
> +		}
> +	}
> +
> +	/* Free nhlt object proper */
> +	free(nhlt);
> +}
> +
> +struct cursor {
> +	u8 *buf;
> +};
> +
> +static void ser8(struct cursor *cur, uint val)
> +{
> +	*cur->buf = val;
> +	cur->buf += sizeof(val);
> +}
> +
> +static void ser16(struct cursor *cur, uint val)
> +{
> +	put_unaligned_le16(val, cur->buf);
> +	cur->buf += sizeof(val);
> +}
> +
> +static void ser32(struct cursor *cur, uint val)
> +{
> +	put_unaligned_le32(val, cur->buf);
> +	cur->buf += sizeof(val);
> +}
> +
> +static void serblob(struct cursor *cur, void *from, size_t sz)
> +{
> +	memcpy(cur->buf, from, sz);
> +	cur->buf += sz;
> +}
> +
> +static void serialise_specific_config(struct nhlt_specific_config *cfg,
> +				      struct cursor *cur)
> +{
> +	ser32(cur, cfg->size);
> +	serblob(cur, cfg->capabilities, cfg->size);
> +}
> +
> +static void serialise_waveform(struct nhlt_waveform *wave, struct cursor *cur)
> +{
> +	ser16(cur, wave->tag);
> +	ser16(cur, wave->num_channels);
> +	ser32(cur, wave->samples_per_second);
> +	ser32(cur, wave->bytes_per_second);
> +	ser16(cur, wave->block_align);
> +	ser16(cur, wave->bits_per_sample);
> +	ser16(cur, wave->extra_size);
> +	ser16(cur, wave->valid_bits_per_sample);
> +	ser32(cur, wave->channel_mask);
> +	ser32(cur, wave->sub_format.data1);
> +	ser16(cur, wave->sub_format.data2);
> +	ser16(cur, wave->sub_format.data3);
> +	serblob(cur, wave->sub_format.data4, sizeof(wave->sub_format.data4));
> +}
> +
> +static void serialise_format(struct nhlt_format *fmt, struct cursor *cur)
> +{
> +	serialise_waveform(&fmt->waveform, cur);
> +	serialise_specific_config(&fmt->config, cur);
> +}
> +
> +static void serialise_endpoint(struct nhlt_endpoint *endp, struct cursor *cur)
> +{
> +	int i;
> +
> +	ser32(cur, endp->length);
> +	ser8(cur, endp->link_type);
> +	ser8(cur, endp->instance_id);
> +	ser16(cur, endp->vendor_id);
> +	ser16(cur, endp->device_id);
> +	ser16(cur, endp->revision_id);
> +	ser32(cur, endp->subsystem_id);
> +	ser8(cur, endp->device_type);
> +	ser8(cur, endp->direction);
> +	ser8(cur, endp->virtual_bus_id);
> +	serialise_specific_config(&endp->config, cur);
> +	ser8(cur, endp->num_formats);
> +
> +	for (i = 0; i < endp->num_formats; i++)
> +		serialise_format(&endp->formats[i], cur);
> +}
> +
> +static void nhlt_serialise_endpoints(struct nhlt *nhlt, struct cursor *cur)
> +{
> +	int i;
> +
> +	ser8(cur, nhlt->num_endpoints);
> +
> +	for (i = 0; i < nhlt->num_endpoints; i++)
> +		serialise_endpoint(&nhlt->endpoints[i], cur);
> +}
> +
> +int nhlt_serialise_oem_overrides(struct acpi_ctx *ctx, struct nhlt *nhlt,
> +				 const char *oem_id, const char *oem_table_id,
> +				 uint32_t oem_revision)
> +{
> +	struct cursor cur;
> +	struct acpi_table_header *header;
> +	size_t sz;
> +	size_t oem_id_len;
> +	size_t oem_table_id_len;
> +	int ret;
> +
> +	log_info("ACPI:    * NHLT\n");
> +	sz = nhlt_current_size(nhlt);
> +
> +	/* Create header */
> +	header = (void *)ctx->current;
> +	memset(header, '\0', sizeof(struct acpi_table_header));
> +	acpi_fill_header(header, "NHLT");
> +	header->length = sz;
> +	header->revision = acpi_get_table_revision(ACPITAB_NHLT);
> +
> +	if (oem_id) {
> +		oem_id_len = min((int)strlen(oem_id), 6);
> +		memcpy(header->oem_id, oem_id, oem_id_len);
> +	}
> +	if (oem_table_id) {
> +		oem_table_id_len = min((int)strlen(oem_table_id), 8);
> +		memcpy(header->oem_table_id, oem_table_id, oem_table_id_len);
> +	}
> +	header->oem_revision = oem_revision;
> +
> +	cur.buf = (void *)(header + 1);
> +	nhlt_serialise_endpoints(nhlt, &cur);
> +
> +	header->checksum = table_compute_checksum(header, sz);
> +	nhlt_free_resources(nhlt);
> +
> +	ret = acpi_add_table(ctx, ctx->current);
> +	if (ret)
> +		return log_msg_ret("add", ret);
> +	acpi_inc_align(ctx, sz);
> +
> +	return 0;
> +}
> +
> +static int _nhlt_add_single_endpoint(struct nhlt *nhlt, int virtual_bus_id,
> +				     const struct nhlt_endp_descriptor *epd)
> +{
> +	struct nhlt_endpoint *endp;
> +	int ret;
> +
> +	endp = nhlt_add_endpoint(nhlt, epd->link, epd->device, epd->direction,
> +				 epd->vid, epd->did);
> +	if (!endp)
> +		return -EINVAL;
> +
> +	endp->virtual_bus_id = virtual_bus_id;
> +
> +	ret = nhlt_endpoint_append_config(endp, epd->cfg, epd->cfg_size);
> +	if (ret)
> +		return ret;
> +
> +	ret = nhlt_endpoint_add_formats(endp, epd->formats, epd->num_formats);
> +	if (ret)
> +		return log_msg_ret("formats", ret);
> +
> +	return 0;
> +}
> +
> +static int _nhlt_add_endpoints(struct nhlt *nhlt, int virtual_bus_id,
> +			       const struct nhlt_endp_descriptor *epds,
> +			       size_t num_epds)
> +{
> +	size_t i;
> +	int ret;
> +
> +	for (i = 0; i < num_epds; i++) {
> +		ret = _nhlt_add_single_endpoint(nhlt, virtual_bus_id, &epds[i]);
> +		if (ret)
> +			return log_ret(ret);
> +	}
> +
> +	return 0;
> +}
> +
> +int nhlt_add_endpoints(struct nhlt *nhlt,
> +		       const struct nhlt_endp_descriptor *epds, size_t num_epds)
> +{
> +	int ret;
> +
> +	ret = _nhlt_add_endpoints(nhlt, DEFAULT_VIRTUAL_BUS_ID, epds, num_epds);
> +
> +	return ret;
> +}
> +
> +int nhlt_add_ssp_endpoints(struct nhlt *nhlt, int virtual_bus_id,
> +			   const struct nhlt_endp_descriptor *epds,
> +			   size_t num_epds)
> +{
> +	int ret;
> +
> +	ret = _nhlt_add_endpoints(nhlt, virtual_bus_id, epds, num_epds);
> +	if (!ret)
> +		nhlt_next_instance(nhlt, NHLT_LINK_SSP);
> +
> +	return ret;
> +}
> -- 
> 2.27.0.290.gba653c62da-goog

regards, Wolfgang


More information about the U-Boot mailing list