Skip to content

Commit 6ec4ff2

Browse files
niedzwiecki-dawidnashif
authored andcommitted
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 <[email protected]>
1 parent d1948dc commit 6ec4ff2

File tree

6 files changed

+298
-0
lines changed

6 files changed

+298
-0
lines changed

include/drivers/espi_emul.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ struct emul_espi_driver_api {
126126
int espi_emul_register(const struct device *dev, const char *name,
127127
struct espi_emul *emul);
128128

129+
/**
130+
* Sets the eSPI virtual wire on the host side, which will
131+
* trigger a proper event(and callbacks) on the emulated eSPI controller
132+
*
133+
* @param espi_dev eSPI emulation controller device
134+
* @param vw The signal to be set.
135+
* @param level The level of the signal to be set.
136+
*
137+
* @retval 0 If successful.
138+
* @retval -EIO General input / output error.
139+
*/
140+
int emul_espi_host_send_vw(const struct device *espi_dev,
141+
enum espi_vwire_signal vw, uint8_t level);
142+
143+
/**
144+
* Perform port80 write on the emulated host side, which will
145+
* trigger a proper event(and callbacks) on the emulated eSPI controller
146+
*
147+
* @param espi_dev eSPI emulation controller device
148+
* @param data The date to be written.
149+
*
150+
* @retval 0 If successful.
151+
* @retval -EIO General input / output error.
152+
*/
153+
int emul_espi_host_port80_write(const struct device *espi_dev, uint32_t data);
154+
129155
#ifdef __cplusplus
130156
}
131157
#endif

subsys/emul/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ zephyr_library_sources_ifdef(CONFIG_EMUL_BMI160 emul_bmi160.c)
99

1010
add_subdirectory(i2c)
1111
add_subdirectory(spi)
12+
add_subdirectory(espi)

subsys/emul/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@ config EMUL_BMI160
4747

4848
source "subsys/emul/i2c/Kconfig"
4949
source "subsys/emul/spi/Kconfig"
50+
source "subsys/emul/espi/Kconfig"
5051

5152
endif

subsys/emul/espi/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
zephyr_library_sources_ifdef(CONFIG_EMUL_ESPI_HOST emul_espi_host.c)

subsys/emul/espi/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Configuration options for eSPI emulators
2+
3+
# Copyright 2020 Google LLC
4+
# SPDX-License-Identifier: Apache-2.0
5+
6+
config EMUL_ESPI_HOST
7+
bool "Emulate an eSPI host"
8+
help
9+
This is an emulator of the generic eSPI host. The emulator supports
10+
basic host operations - virtual wires and writing to port 80. It can be
11+
extended.

subsys/emul/espi/emul_espi_host.c

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Emulator for the generic eSPI Host. This supports basic
7+
* host operations.
8+
*/
9+
10+
#define DT_DRV_COMPAT zephyr_espi_emul_espi_host
11+
12+
#define LOG_LEVEL CONFIG_ESPI_LOG_LEVEL
13+
#include <logging/log.h>
14+
LOG_MODULE_REGISTER(espi_host);
15+
16+
#include <device.h>
17+
#include <emul.h>
18+
#include <drivers/espi.h>
19+
#include <drivers/espi_emul.h>
20+
21+
/** Data about the virtual wire */
22+
struct vw_data {
23+
/* Virtual wire signal */
24+
enum espi_vwire_signal sig;
25+
/* The level(state) of the virtual wire */
26+
uint8_t level;
27+
/* The direction of the virtual wire. Possible values:
28+
* ESPI_MASTER_TO_SLAVE or ESPI_SLAVE_TO_MASTER */
29+
uint8_t dir;
30+
};
31+
32+
/** Declare the default state of virtual wires */
33+
const static struct vw_data vw_state_default[] = {
34+
{ ESPI_VWIRE_SIGNAL_SLP_S3, 0, ESPI_MASTER_TO_SLAVE },
35+
{ ESPI_VWIRE_SIGNAL_SLP_S4, 0, ESPI_MASTER_TO_SLAVE },
36+
{ ESPI_VWIRE_SIGNAL_SLP_S5, 0, ESPI_MASTER_TO_SLAVE },
37+
{ ESPI_VWIRE_SIGNAL_SUS_STAT, 0, ESPI_MASTER_TO_SLAVE },
38+
{ ESPI_VWIRE_SIGNAL_PLTRST, 0, ESPI_MASTER_TO_SLAVE },
39+
{ ESPI_VWIRE_SIGNAL_OOB_RST_WARN, 0, ESPI_MASTER_TO_SLAVE },
40+
{ ESPI_VWIRE_SIGNAL_OOB_RST_ACK, 0, ESPI_SLAVE_TO_MASTER },
41+
{ ESPI_VWIRE_SIGNAL_WAKE, 0, ESPI_SLAVE_TO_MASTER },
42+
{ ESPI_VWIRE_SIGNAL_PME, 0, ESPI_SLAVE_TO_MASTER },
43+
{ ESPI_VWIRE_SIGNAL_SLV_BOOT_DONE, 0, ESPI_SLAVE_TO_MASTER },
44+
{ ESPI_VWIRE_SIGNAL_ERR_FATAL, 0, ESPI_SLAVE_TO_MASTER },
45+
{ ESPI_VWIRE_SIGNAL_ERR_NON_FATAL, 0, ESPI_SLAVE_TO_MASTER },
46+
{ ESPI_VWIRE_SIGNAL_SLV_BOOT_STS, 0, ESPI_SLAVE_TO_MASTER },
47+
{ ESPI_VWIRE_SIGNAL_SCI, 0, ESPI_SLAVE_TO_MASTER },
48+
{ ESPI_VWIRE_SIGNAL_SMI, 0, ESPI_SLAVE_TO_MASTER },
49+
{ ESPI_VWIRE_SIGNAL_RST_CPU_INIT, 0, ESPI_SLAVE_TO_MASTER },
50+
{ ESPI_VWIRE_SIGNAL_HOST_RST_ACK, 0, ESPI_SLAVE_TO_MASTER },
51+
{ ESPI_VWIRE_SIGNAL_HOST_RST_WARN, 0, ESPI_MASTER_TO_SLAVE },
52+
{ ESPI_VWIRE_SIGNAL_SUS_ACK, 0, ESPI_SLAVE_TO_MASTER },
53+
{ ESPI_VWIRE_SIGNAL_DNX_ACK, 0, ESPI_SLAVE_TO_MASTER },
54+
{ ESPI_VWIRE_SIGNAL_SUS_WARN, 0, ESPI_MASTER_TO_SLAVE },
55+
{ ESPI_VWIRE_SIGNAL_SUS_PWRDN_ACK, 0, ESPI_MASTER_TO_SLAVE },
56+
{ ESPI_VWIRE_SIGNAL_SLP_A, 0, ESPI_MASTER_TO_SLAVE },
57+
{ ESPI_VWIRE_SIGNAL_SLP_LAN, 0, ESPI_MASTER_TO_SLAVE },
58+
{ ESPI_VWIRE_SIGNAL_SLP_WLAN, 0, ESPI_MASTER_TO_SLAVE },
59+
{ ESPI_VWIRE_SIGNAL_HOST_C10, 0, ESPI_MASTER_TO_SLAVE },
60+
{ ESPI_VWIRE_SIGNAL_DNX_WARN, 0, ESPI_MASTER_TO_SLAVE },
61+
};
62+
63+
#define NUMBER_OF_VWIRES ARRAY_SIZE(vw_state_default)
64+
65+
/** Run-time data used by the emulator */
66+
struct espi_host_emul_data {
67+
/** eSPI emulator detail */
68+
struct espi_emul emul;
69+
/** eSPI controller device */
70+
const struct device *espi;
71+
/** Configuration information */
72+
const struct espi_host_emul_cfg *cfg;
73+
/** Virtual Wires states, for one slave only.
74+
* With multi-slaves config, the states should be saved per slave */
75+
struct vw_data vw_state[NUMBER_OF_VWIRES];
76+
};
77+
78+
/** Static configuration for the emulator */
79+
struct espi_host_emul_cfg {
80+
/** Label of the eSPI bus this emulator connects to */
81+
const char *espi_label;
82+
/** Label of the emulated AP*/
83+
const char *label;
84+
/** Pointer to run-time data */
85+
struct espi_host_emul_data *data;
86+
/* eSPI chip-select of the emulated device */
87+
uint16_t chipsel;
88+
};
89+
90+
/**
91+
* Initialize the state of virtual wires to default based on
92+
* the vw_state_default array.
93+
*
94+
* @param data Host emulator data with the vwire array
95+
*/
96+
static void emul_host_init_vw_state(struct espi_host_emul_data *data)
97+
{
98+
unsigned i;
99+
100+
for (i = 0; i < NUMBER_OF_VWIRES; i++) {
101+
data->vw_state[i] = vw_state_default[i];
102+
}
103+
return;
104+
}
105+
106+
/**
107+
* Find a virtual wire in the array placed in the host data.
108+
*
109+
* @param data Host emulator data with the vwire array
110+
* @param vw Virtual wire signal to be found
111+
* @return index in the array
112+
* @return -1 if not found
113+
*/
114+
static int emul_host_find_index(struct espi_host_emul_data *data,
115+
enum espi_vwire_signal vw)
116+
{
117+
int idx;
118+
119+
for (idx = 0; idx < NUMBER_OF_VWIRES; idx++) {
120+
if (data->vw_state[idx].sig == vw) {
121+
return idx;
122+
}
123+
}
124+
125+
return -1;
126+
}
127+
128+
static int emul_host_set_vw(struct espi_emul *emul, enum espi_vwire_signal vw,
129+
uint8_t level)
130+
{
131+
struct espi_host_emul_data *data;
132+
int idx;
133+
134+
data = CONTAINER_OF(emul, struct espi_host_emul_data, emul);
135+
idx = emul_host_find_index(data, vw);
136+
137+
if (idx < 0 || data->vw_state[idx].dir != ESPI_SLAVE_TO_MASTER) {
138+
LOG_ERR("%s: invalid vw: %d", __func__, vw);
139+
return -EPERM;
140+
}
141+
142+
data->vw_state[idx].level = level;
143+
144+
return 0;
145+
}
146+
147+
static int emul_host_get_vw(struct espi_emul *emul, enum espi_vwire_signal vw,
148+
uint8_t *level)
149+
{
150+
struct espi_host_emul_data *data;
151+
int idx;
152+
153+
data = CONTAINER_OF(emul, struct espi_host_emul_data, emul);
154+
idx = emul_host_find_index(data, vw);
155+
156+
if (idx < 0 || data->vw_state[idx].dir != ESPI_MASTER_TO_SLAVE) {
157+
LOG_ERR("%s: invalid vw: %d", __func__, vw);
158+
return -EPERM;
159+
}
160+
161+
*level = data->vw_state[idx].level;
162+
163+
return 0;
164+
}
165+
166+
int emul_espi_host_send_vw(const struct device *espi_dev, enum espi_vwire_signal vw,
167+
uint8_t level)
168+
{
169+
struct espi_emul *emul_espi;
170+
struct espi_event evt;
171+
struct espi_host_emul_data *data_host;
172+
struct emul_espi_driver_api *api;
173+
int idx;
174+
175+
api = (struct emul_espi_driver_api *)espi_dev->api;
176+
177+
__ASSERT_NO_MSG(api);
178+
__ASSERT_NO_MSG(api->trigger_event);
179+
__ASSERT_NO_MSG(api->find_emul);
180+
181+
emul_espi = api->find_emul(espi_dev, EMUL_ESPI_HOST_CHIPSEL);
182+
data_host = CONTAINER_OF(emul_espi, struct espi_host_emul_data, emul);
183+
184+
idx = emul_host_find_index(data_host, vw);
185+
if (idx < 0 || data_host->vw_state[idx].dir != ESPI_MASTER_TO_SLAVE) {
186+
LOG_ERR("%s: invalid vw: %d", __func__, vw);
187+
return -EPERM;
188+
}
189+
190+
data_host->vw_state[idx].level = level;
191+
192+
evt.evt_type = ESPI_BUS_EVENT_VWIRE_RECEIVED;
193+
evt.evt_details = vw;
194+
evt.evt_data = level;
195+
196+
api->trigger_event(espi_dev, &evt);
197+
198+
return 0;
199+
}
200+
201+
int emul_espi_host_port80_write(const struct device *espi_dev, uint32_t data)
202+
{
203+
struct espi_event evt;
204+
struct emul_espi_driver_api *api;
205+
206+
api = (struct emul_espi_driver_api *)espi_dev->api;
207+
208+
__ASSERT_NO_MSG(api);
209+
__ASSERT_NO_MSG(api->trigger_event);
210+
211+
evt.evt_type = ESPI_BUS_PERIPHERAL_NOTIFICATION;
212+
evt.evt_details = ESPI_PERIPHERAL_DEBUG_PORT80;
213+
evt.evt_data = data;
214+
215+
api->trigger_event(espi_dev, &evt);
216+
217+
return 0;
218+
}
219+
220+
/* Device instantiation */
221+
static struct emul_espi_device_api ap_emul_api = {
222+
.set_vw = emul_host_set_vw,
223+
.get_vw = emul_host_get_vw,
224+
};
225+
226+
/**
227+
* Set up a new eSPI host emulator
228+
*
229+
* @param emul Emulation information
230+
* @param bus Device to emulated eSPI controller
231+
* @return 0 indicating success (always)
232+
*/
233+
static int emul_host_init(const struct emul *emul, const struct device *bus)
234+
{
235+
const struct espi_host_emul_cfg *cfg = emul->cfg;
236+
struct espi_host_emul_data *data = cfg->data;
237+
238+
data->emul.api = &ap_emul_api;
239+
data->emul.chipsel = cfg->chipsel;
240+
data->espi = bus;
241+
data->cfg = cfg;
242+
emul_host_init_vw_state(data);
243+
244+
return espi_emul_register(bus, emul->dev_label, &data->emul);
245+
}
246+
247+
#define HOST_EMUL(n) \
248+
static struct espi_host_emul_data espi_host_emul_data_##n; \
249+
static const struct espi_host_emul_cfg espi_host_emul_cfg_##n = { \
250+
.espi_label = DT_INST_BUS_LABEL(n), \
251+
.data = &espi_host_emul_data_##n, \
252+
.chipsel = DT_INST_REG_ADDR(n), \
253+
}; \
254+
EMUL_DEFINE(emul_host_init, DT_DRV_INST(n), &espi_host_emul_cfg_##n)
255+
256+
DT_INST_FOREACH_STATUS_OKAY(HOST_EMUL)

0 commit comments

Comments
 (0)