From ab6e724ab4d9ec71f1089f82b357c145203dd8ba Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Wed, 28 Jul 2021 21:40:13 -0600 Subject: [PATCH] emul: espi: Add ACPI Shared Memory functions Add the bare minimum to set and access the ACPI shared memory via the eSPI emulator. Signed-off-by: Yuval Peress --- drivers/espi/espi_emul.c | 45 +++++++++++++++++++ include/drivers/espi_emul.h | 27 ++++++++++- subsys/emul/espi/Kconfig | 12 +++++ subsys/emul/espi/emul_espi_host.c | 25 +++++++++++ tests/drivers/espi/CMakeLists.txt | 10 +++++ .../drivers/espi/boards/native_posix.overlay | 14 ++++++ tests/drivers/espi/prj.conf | 11 +++++ tests/drivers/espi/src/test_acpi.c | 38 ++++++++++++++++ tests/drivers/espi/testcase.yaml | 11 +++++ 9 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 tests/drivers/espi/CMakeLists.txt create mode 100644 tests/drivers/espi/boards/native_posix.overlay create mode 100644 tests/drivers/espi/prj.conf create mode 100644 tests/drivers/espi/src/test_acpi.c create mode 100644 tests/drivers/espi/testcase.yaml diff --git a/drivers/espi/espi_emul.c b/drivers/espi/espi_emul.c index ddcb613865e..ccd797b0a6f 100644 --- a/drivers/espi/espi_emul.c +++ b/drivers/espi/espi_emul.c @@ -87,6 +87,49 @@ static bool espi_emul_get_channel_status(const struct device *dev, enum espi_cha return (data->cfg.channel_caps & ch); } +static int espi_emul_read_lpc_request(const struct device *dev, + enum lpc_peripheral_opcode op, + uint32_t *data) +{ + const struct emul_espi_device_api *api; + struct espi_emul *emul; + struct espi_emul_data *emul_data = dev->data; + + if (!(emul_data->cfg.channel_caps & ESPI_CHANNEL_VWIRE)) { + LOG_ERR("bad channel vwire"); + return -EINVAL; + } + + emul = espi_emul_find(dev, EMUL_ESPI_HOST_CHIPSEL); + if (!emul) { + LOG_ERR("espi_emul not found"); + return -ENOTSUP; + } + + __ASSERT_NO_MSG(emul->api); + api = emul->api; + + switch (op) { +#ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION + case EACPI_GET_SHARED_MEMORY: + __ASSERT_NO_MSG(api->get_acpi_shm); + *data = (uint32_t)api->get_acpi_shm(emul); + break; +#endif + default: + return -EINVAL; + } + return 0; +} + +static int espi_emul_write_lpc_request(const struct device *dev, enum lpc_peripheral_opcode op, + uint32_t *data) +{ + ARG_UNUSED(dev); + + return -EINVAL; +} + static int espi_emul_send_vwire(const struct device *dev, enum espi_vwire_signal vw, uint8_t level) { const struct emul_espi_device_api *api; @@ -172,6 +215,8 @@ static struct emul_espi_driver_api emul_espi_driver_api = { .espi_api = { .config = espi_emul_config, .get_channel_status = espi_emul_get_channel_status, + .read_lpc_request = espi_emul_read_lpc_request, + .write_lpc_request = espi_emul_write_lpc_request, .send_vwire = espi_emul_send_vwire, .receive_vwire = espi_emul_receive_vwire, .manage_callback = espi_emul_manage_callback diff --git a/include/drivers/espi_emul.h b/include/drivers/espi_emul.h index 07481f10e44..d4679191321 100644 --- a/include/drivers/espi_emul.h +++ b/include/drivers/espi_emul.h @@ -61,6 +61,17 @@ typedef int (*emul_espi_api_get_vw)(struct espi_emul *emul, enum espi_vwire_signal vw, uint8_t *level); +#ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION +/** + * Get the ACPI shared memory address owned by the emulator. + * + * @param emul Emulator instance. + * + * @retval The address of the memory. + */ +typedef uintptr_t (*emul_espi_api_get_acpi_shm)(struct espi_emul *emul); +#endif + /** * Find an emulator present on a eSPI bus * @@ -88,11 +99,13 @@ typedef struct espi_emul *(*emul_find_emul)(const struct device *dev, typedef int (*emul_trigger_event)(const struct device *dev, struct espi_event *evt); - /** Definition of the eSPI device emulator API */ struct emul_espi_device_api { emul_espi_api_set_vw set_vw; emul_espi_api_get_vw get_vw; +#ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION + emul_espi_api_get_acpi_shm get_acpi_shm; +#endif }; /** Node in a linked list of emulators for eSPI devices */ @@ -152,6 +165,18 @@ int emul_espi_host_send_vw(const struct device *espi_dev, */ int emul_espi_host_port80_write(const struct device *espi_dev, uint32_t data); +#ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION +/** + * Get the host device's ACPI shared memory start address. The size of the region is + * CONFIG_EMUL_ESPI_HOST_ACPI_SHM_REGION_SIZE. + * + * @param espi_dev eSPI emulation controller device. + * @return Address of the start of the ACPI shared memory. + */ +uintptr_t emul_espi_host_get_acpi_shm(const struct device *espi_dev); +#endif + + #ifdef __cplusplus } #endif diff --git a/subsys/emul/espi/Kconfig b/subsys/emul/espi/Kconfig index 1e9ed31edfe..1c75d1bf30a 100644 --- a/subsys/emul/espi/Kconfig +++ b/subsys/emul/espi/Kconfig @@ -9,3 +9,15 @@ config EMUL_ESPI_HOST This is an emulator of the generic eSPI host. The emulator supports basic host operations - virtual wires and writing to port 80. It can be extended. + +if EMUL_ESPI_HOST + +config EMUL_ESPI_HOST_ACPI_SHM_REGION_SIZE + int "Host I/O peripheral port size for shared memory in emulator" + depends on ESPI_PERIPHERAL_ACPI_SHM_REGION + default 256 + help + This is the port size used to mimic the Host and EC communication + over the shared memory region which returns the ACPI response data. + +endif # EMUL_ESPI_HOST diff --git a/subsys/emul/espi/emul_espi_host.c b/subsys/emul/espi/emul_espi_host.c index fd0cb16e905..f659fcdabe2 100644 --- a/subsys/emul/espi/emul_espi_host.c +++ b/subsys/emul/espi/emul_espi_host.c @@ -73,6 +73,10 @@ struct espi_host_emul_data { /** Virtual Wires states, for one slave only. * With multi-slaves config, the states should be saved per slave */ struct vw_data vw_state[NUMBER_OF_VWIRES]; +#ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION + /** ACPI Shared memory. */ + uint8_t shm_acpi_mmap[CONFIG_EMUL_ESPI_HOST_ACPI_SHM_REGION_SIZE]; +#endif }; /** Static configuration for the emulator */ @@ -217,10 +221,31 @@ int emul_espi_host_port80_write(const struct device *espi_dev, uint32_t data) return 0; } +#ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION +static uintptr_t emul_espi_dev_get_acpi_shm(struct espi_emul *emul) +{ + struct espi_host_emul_data *data = + CONTAINER_OF(emul, struct espi_host_emul_data, emul); + + return (uintptr_t)data->shm_acpi_mmap; +} + +uintptr_t emul_espi_host_get_acpi_shm(const struct device *espi_dev) +{ + uint32_t shm; + int rc = espi_read_lpc_request(espi_dev, EACPI_GET_SHARED_MEMORY, &shm); + + __ASSERT_NO_MSG(rc == 0); + + return (uintptr_t) shm; +} +#endif + /* Device instantiation */ static struct emul_espi_device_api ap_emul_api = { .set_vw = emul_host_set_vw, .get_vw = emul_host_get_vw, + .get_acpi_shm = emul_espi_dev_get_acpi_shm, }; /** diff --git a/tests/drivers/espi/CMakeLists.txt b/tests/drivers/espi/CMakeLists.txt new file mode 100644 index 00000000000..87d61c4caf0 --- /dev/null +++ b/tests/drivers/espi/CMakeLists.txt @@ -0,0 +1,10 @@ +# Copyright 2021 Google LLC +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(espi) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/drivers/espi/boards/native_posix.overlay b/tests/drivers/espi/boards/native_posix.overlay new file mode 100644 index 00000000000..425d68f693c --- /dev/null +++ b/tests/drivers/espi/boards/native_posix.overlay @@ -0,0 +1,14 @@ +/* + * Copyright 2021 Google LLC + * + * SPDX-License-Identifier: Apache-2.0 + */ + +&espi0 { + espi_host: espi-host@0 { + status = "okay"; + compatible = "zephyr,espi-emul-espi-host"; + reg = <0x0>; + label = "ESPI_HOST"; + }; +}; diff --git a/tests/drivers/espi/prj.conf b/tests/drivers/espi/prj.conf new file mode 100644 index 00000000000..b909c03f0c3 --- /dev/null +++ b/tests/drivers/espi/prj.conf @@ -0,0 +1,11 @@ +# Copyright 2021 Google LLC +# SPDX-License-Identifier: Apache-2.0 + +CONFIG_ZTEST=y +CONFIG_ESPI=y +CONFIG_EMUL=y +CONFIG_ESPI_EMUL=y +CONFIG_EMUL_ESPI_HOST=y + +CONFIG_ESPI_PERIPHERAL_CHANNEL=y +CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION=y diff --git a/tests/drivers/espi/src/test_acpi.c b/tests/drivers/espi/src/test_acpi.c new file mode 100644 index 00000000000..b2ccdaba615 --- /dev/null +++ b/tests/drivers/espi/src/test_acpi.c @@ -0,0 +1,38 @@ +/* + * Copyright 2021 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include +#include + +static void test_acpi_shared_memory(void) +{ + const struct device *espi_dev = device_get_binding(DT_LABEL(DT_NODELABEL(espi0))); + struct espi_cfg cfg = { + .channel_caps = ESPI_CHANNEL_VWIRE | ESPI_CHANNEL_PERIPHERAL, + }; + uintptr_t host_shm, peripheral_shm; + + zassert_not_null(espi_dev, NULL); + + zassert_ok(espi_config(espi_dev, &cfg), NULL); + + host_shm = emul_espi_host_get_acpi_shm(espi_dev); + zassert_not_equal(host_shm, 0, NULL); + + zassert_ok(espi_read_lpc_request(espi_dev, EACPI_GET_SHARED_MEMORY, + (uint32_t *)&peripheral_shm), + NULL); + + zassert_equal(host_shm, peripheral_shm, NULL); +} + +ztest_test_suite(acpi, ztest_unit_test(test_acpi_shared_memory)); + +void test_main(void) +{ + ztest_run_test_suite(acpi); +} diff --git a/tests/drivers/espi/testcase.yaml b/tests/drivers/espi/testcase.yaml new file mode 100644 index 00000000000..949b69c2033 --- /dev/null +++ b/tests/drivers/espi/testcase.yaml @@ -0,0 +1,11 @@ +# Copyright 2021 Google LLC +# SPDX-License-Identifier: Apache-2.0 + +tests: + drivers.espi.acpi: + tags: drivers espi + filter: dt_compat_enabled("zephyr,espi-emul-controller") + harness: ztest + platform_allow: native_posix + integration_platforms: + - native_posix