From 6ec4ff22e3841b118cdbea0ccc336b47e895183b Mon Sep 17 00:00:00 2001 From: Dawid Niedzwiecki Date: Wed, 16 Dec 2020 15:08:59 +0100 Subject: [PATCH] emul: Add an emulator for the eSPI Host This emulator pretends a generic eSPI Host. It supports basic virtual wires and port80 operations. There are functions to trigger actions on the host side e.g. for setting a virtual wire from the host to the eSPI slave, use emul_espi_host_send_vw. It will prepare data and set a proper event on the slave side which will trigger callback (if there is any). Signed-off-by: Dawid Niedzwiecki --- include/drivers/espi_emul.h | 26 +++ subsys/emul/CMakeLists.txt | 1 + subsys/emul/Kconfig | 1 + subsys/emul/espi/CMakeLists.txt | 3 + subsys/emul/espi/Kconfig | 11 ++ subsys/emul/espi/emul_espi_host.c | 256 ++++++++++++++++++++++++++++++ 6 files changed, 298 insertions(+) create mode 100644 subsys/emul/espi/CMakeLists.txt create mode 100644 subsys/emul/espi/Kconfig create mode 100644 subsys/emul/espi/emul_espi_host.c diff --git a/include/drivers/espi_emul.h b/include/drivers/espi_emul.h index 3153a29b13..07481f10e4 100644 --- a/include/drivers/espi_emul.h +++ b/include/drivers/espi_emul.h @@ -126,6 +126,32 @@ struct emul_espi_driver_api { int espi_emul_register(const struct device *dev, const char *name, struct espi_emul *emul); +/** + * Sets the eSPI virtual wire on the host side, which will + * trigger a proper event(and callbacks) on the emulated eSPI controller + * + * @param espi_dev eSPI emulation controller device + * @param vw The signal to be set. + * @param level The level of the signal to be set. + * + * @retval 0 If successful. + * @retval -EIO General input / output error. + */ +int emul_espi_host_send_vw(const struct device *espi_dev, + enum espi_vwire_signal vw, uint8_t level); + +/** + * Perform port80 write on the emulated host side, which will + * trigger a proper event(and callbacks) on the emulated eSPI controller + * + * @param espi_dev eSPI emulation controller device + * @param data The date to be written. + * + * @retval 0 If successful. + * @retval -EIO General input / output error. + */ +int emul_espi_host_port80_write(const struct device *espi_dev, uint32_t data); + #ifdef __cplusplus } #endif diff --git a/subsys/emul/CMakeLists.txt b/subsys/emul/CMakeLists.txt index c759ea9a07..0eb5ae3cf6 100644 --- a/subsys/emul/CMakeLists.txt +++ b/subsys/emul/CMakeLists.txt @@ -9,3 +9,4 @@ zephyr_library_sources_ifdef(CONFIG_EMUL_BMI160 emul_bmi160.c) add_subdirectory(i2c) add_subdirectory(spi) +add_subdirectory(espi) diff --git a/subsys/emul/Kconfig b/subsys/emul/Kconfig index 7cc3fd89d6..7cf4f778fd 100644 --- a/subsys/emul/Kconfig +++ b/subsys/emul/Kconfig @@ -47,5 +47,6 @@ config EMUL_BMI160 source "subsys/emul/i2c/Kconfig" source "subsys/emul/spi/Kconfig" +source "subsys/emul/espi/Kconfig" endif diff --git a/subsys/emul/espi/CMakeLists.txt b/subsys/emul/espi/CMakeLists.txt new file mode 100644 index 0000000000..1425949df4 --- /dev/null +++ b/subsys/emul/espi/CMakeLists.txt @@ -0,0 +1,3 @@ +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library_sources_ifdef(CONFIG_EMUL_ESPI_HOST emul_espi_host.c) diff --git a/subsys/emul/espi/Kconfig b/subsys/emul/espi/Kconfig new file mode 100644 index 0000000000..1e9ed31edf --- /dev/null +++ b/subsys/emul/espi/Kconfig @@ -0,0 +1,11 @@ +# Configuration options for eSPI emulators + +# Copyright 2020 Google LLC +# SPDX-License-Identifier: Apache-2.0 + +config EMUL_ESPI_HOST + bool "Emulate an eSPI host" + help + 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. diff --git a/subsys/emul/espi/emul_espi_host.c b/subsys/emul/espi/emul_espi_host.c new file mode 100644 index 0000000000..33a161c753 --- /dev/null +++ b/subsys/emul/espi/emul_espi_host.c @@ -0,0 +1,256 @@ +/* + * Copyright 2020 Google LLC + * + * SPDX-License-Identifier: Apache-2.0 + * + * Emulator for the generic eSPI Host. This supports basic + * host operations. + */ + +#define DT_DRV_COMPAT zephyr_espi_emul_espi_host + +#define LOG_LEVEL CONFIG_ESPI_LOG_LEVEL +#include +LOG_MODULE_REGISTER(espi_host); + +#include +#include +#include +#include + +/** Data about the virtual wire */ +struct vw_data { + /* Virtual wire signal */ + enum espi_vwire_signal sig; + /* The level(state) of the virtual wire */ + uint8_t level; + /* The direction of the virtual wire. Possible values: + * ESPI_MASTER_TO_SLAVE or ESPI_SLAVE_TO_MASTER */ + uint8_t dir; +}; + +/** Declare the default state of virtual wires */ +const static struct vw_data vw_state_default[] = { + { ESPI_VWIRE_SIGNAL_SLP_S3, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SLP_S4, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SLP_S5, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SUS_STAT, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_PLTRST, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_OOB_RST_WARN, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_OOB_RST_ACK, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_WAKE, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_PME, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_SLV_BOOT_DONE, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_ERR_FATAL, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_ERR_NON_FATAL, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_SLV_BOOT_STS, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_SCI, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_SMI, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_RST_CPU_INIT, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_HOST_RST_ACK, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_HOST_RST_WARN, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SUS_ACK, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_DNX_ACK, 0, ESPI_SLAVE_TO_MASTER }, + { ESPI_VWIRE_SIGNAL_SUS_WARN, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SUS_PWRDN_ACK, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SLP_A, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SLP_LAN, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_SLP_WLAN, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_HOST_C10, 0, ESPI_MASTER_TO_SLAVE }, + { ESPI_VWIRE_SIGNAL_DNX_WARN, 0, ESPI_MASTER_TO_SLAVE }, +}; + +#define NUMBER_OF_VWIRES ARRAY_SIZE(vw_state_default) + +/** Run-time data used by the emulator */ +struct espi_host_emul_data { + /** eSPI emulator detail */ + struct espi_emul emul; + /** eSPI controller device */ + const struct device *espi; + /** Configuration information */ + const struct espi_host_emul_cfg *cfg; + /** 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]; +}; + +/** Static configuration for the emulator */ +struct espi_host_emul_cfg { + /** Label of the eSPI bus this emulator connects to */ + const char *espi_label; + /** Label of the emulated AP*/ + const char *label; + /** Pointer to run-time data */ + struct espi_host_emul_data *data; + /* eSPI chip-select of the emulated device */ + uint16_t chipsel; +}; + +/** + * Initialize the state of virtual wires to default based on + * the vw_state_default array. + * + * @param data Host emulator data with the vwire array + */ +static void emul_host_init_vw_state(struct espi_host_emul_data *data) +{ + unsigned i; + + for (i = 0; i < NUMBER_OF_VWIRES; i++) { + data->vw_state[i] = vw_state_default[i]; + } + return; +} + +/** + * Find a virtual wire in the array placed in the host data. + * + * @param data Host emulator data with the vwire array + * @param vw Virtual wire signal to be found + * @return index in the array + * @return -1 if not found + */ +static int emul_host_find_index(struct espi_host_emul_data *data, + enum espi_vwire_signal vw) +{ + int idx; + + for (idx = 0; idx < NUMBER_OF_VWIRES; idx++) { + if (data->vw_state[idx].sig == vw) { + return idx; + } + } + + return -1; +} + +static int emul_host_set_vw(struct espi_emul *emul, enum espi_vwire_signal vw, + uint8_t level) +{ + struct espi_host_emul_data *data; + int idx; + + data = CONTAINER_OF(emul, struct espi_host_emul_data, emul); + idx = emul_host_find_index(data, vw); + + if (idx < 0 || data->vw_state[idx].dir != ESPI_SLAVE_TO_MASTER) { + LOG_ERR("%s: invalid vw: %d", __func__, vw); + return -EPERM; + } + + data->vw_state[idx].level = level; + + return 0; +} + +static int emul_host_get_vw(struct espi_emul *emul, enum espi_vwire_signal vw, + uint8_t *level) +{ + struct espi_host_emul_data *data; + int idx; + + data = CONTAINER_OF(emul, struct espi_host_emul_data, emul); + idx = emul_host_find_index(data, vw); + + if (idx < 0 || data->vw_state[idx].dir != ESPI_MASTER_TO_SLAVE) { + LOG_ERR("%s: invalid vw: %d", __func__, vw); + return -EPERM; + } + + *level = data->vw_state[idx].level; + + return 0; +} + +int emul_espi_host_send_vw(const struct device *espi_dev, enum espi_vwire_signal vw, + uint8_t level) +{ + struct espi_emul *emul_espi; + struct espi_event evt; + struct espi_host_emul_data *data_host; + struct emul_espi_driver_api *api; + int idx; + + api = (struct emul_espi_driver_api *)espi_dev->api; + + __ASSERT_NO_MSG(api); + __ASSERT_NO_MSG(api->trigger_event); + __ASSERT_NO_MSG(api->find_emul); + + emul_espi = api->find_emul(espi_dev, EMUL_ESPI_HOST_CHIPSEL); + data_host = CONTAINER_OF(emul_espi, struct espi_host_emul_data, emul); + + idx = emul_host_find_index(data_host, vw); + if (idx < 0 || data_host->vw_state[idx].dir != ESPI_MASTER_TO_SLAVE) { + LOG_ERR("%s: invalid vw: %d", __func__, vw); + return -EPERM; + } + + data_host->vw_state[idx].level = level; + + evt.evt_type = ESPI_BUS_EVENT_VWIRE_RECEIVED; + evt.evt_details = vw; + evt.evt_data = level; + + api->trigger_event(espi_dev, &evt); + + return 0; +} + +int emul_espi_host_port80_write(const struct device *espi_dev, uint32_t data) +{ + struct espi_event evt; + struct emul_espi_driver_api *api; + + api = (struct emul_espi_driver_api *)espi_dev->api; + + __ASSERT_NO_MSG(api); + __ASSERT_NO_MSG(api->trigger_event); + + evt.evt_type = ESPI_BUS_PERIPHERAL_NOTIFICATION; + evt.evt_details = ESPI_PERIPHERAL_DEBUG_PORT80; + evt.evt_data = data; + + api->trigger_event(espi_dev, &evt); + + return 0; +} + +/* Device instantiation */ +static struct emul_espi_device_api ap_emul_api = { + .set_vw = emul_host_set_vw, + .get_vw = emul_host_get_vw, +}; + +/** + * Set up a new eSPI host emulator + * + * @param emul Emulation information + * @param bus Device to emulated eSPI controller + * @return 0 indicating success (always) + */ +static int emul_host_init(const struct emul *emul, const struct device *bus) +{ + const struct espi_host_emul_cfg *cfg = emul->cfg; + struct espi_host_emul_data *data = cfg->data; + + data->emul.api = &ap_emul_api; + data->emul.chipsel = cfg->chipsel; + data->espi = bus; + data->cfg = cfg; + emul_host_init_vw_state(data); + + return espi_emul_register(bus, emul->dev_label, &data->emul); +} + +#define HOST_EMUL(n) \ + static struct espi_host_emul_data espi_host_emul_data_##n; \ + static const struct espi_host_emul_cfg espi_host_emul_cfg_##n = { \ + .espi_label = DT_INST_BUS_LABEL(n), \ + .data = &espi_host_emul_data_##n, \ + .chipsel = DT_INST_REG_ADDR(n), \ + }; \ + EMUL_DEFINE(emul_host_init, DT_DRV_INST(n), &espi_host_emul_cfg_##n) + +DT_INST_FOREACH_STATUS_OKAY(HOST_EMUL)