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 <zephyr/logging/log.h>
14 LOG_MODULE_REGISTER(espi_host);
15 
16 #include <zephyr/device.h>
17 #include <zephyr/drivers/emul.h>
18 #include <zephyr/drivers/espi.h>
19 #include <zephyr/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_CONTROLLER_TO_TARGET or ESPI_TARGET_TO_CONTROLLER
29 	 */
30 	uint8_t dir;
31 };
32 
33 /** Declare the default state of virtual wires */
34 const static struct vw_data vw_state_default[] = {
35 	{ ESPI_VWIRE_SIGNAL_SLP_S3, 0, ESPI_CONTROLLER_TO_TARGET },
36 	{ ESPI_VWIRE_SIGNAL_SLP_S4, 0, ESPI_CONTROLLER_TO_TARGET },
37 	{ ESPI_VWIRE_SIGNAL_SLP_S5, 0, ESPI_CONTROLLER_TO_TARGET },
38 	{ ESPI_VWIRE_SIGNAL_SUS_STAT, 0, ESPI_CONTROLLER_TO_TARGET },
39 	{ ESPI_VWIRE_SIGNAL_PLTRST, 0, ESPI_CONTROLLER_TO_TARGET },
40 	{ ESPI_VWIRE_SIGNAL_OOB_RST_WARN, 0, ESPI_CONTROLLER_TO_TARGET },
41 	{ ESPI_VWIRE_SIGNAL_OOB_RST_ACK, 0, ESPI_TARGET_TO_CONTROLLER },
42 	{ ESPI_VWIRE_SIGNAL_WAKE, 0, ESPI_TARGET_TO_CONTROLLER },
43 	{ ESPI_VWIRE_SIGNAL_PME, 0, ESPI_TARGET_TO_CONTROLLER },
44 	{ ESPI_VWIRE_SIGNAL_TARGET_BOOT_DONE, 0, ESPI_TARGET_TO_CONTROLLER },
45 	{ ESPI_VWIRE_SIGNAL_ERR_FATAL, 0, ESPI_TARGET_TO_CONTROLLER },
46 	{ ESPI_VWIRE_SIGNAL_ERR_NON_FATAL, 0, ESPI_TARGET_TO_CONTROLLER },
47 	{ ESPI_VWIRE_SIGNAL_TARGET_BOOT_STS, 0, ESPI_TARGET_TO_CONTROLLER },
48 	{ ESPI_VWIRE_SIGNAL_SCI, 0, ESPI_TARGET_TO_CONTROLLER },
49 	{ ESPI_VWIRE_SIGNAL_SMI, 0, ESPI_TARGET_TO_CONTROLLER },
50 	{ ESPI_VWIRE_SIGNAL_RST_CPU_INIT, 0, ESPI_TARGET_TO_CONTROLLER },
51 	{ ESPI_VWIRE_SIGNAL_HOST_RST_ACK, 0, ESPI_TARGET_TO_CONTROLLER },
52 	{ ESPI_VWIRE_SIGNAL_HOST_RST_WARN, 0, ESPI_CONTROLLER_TO_TARGET },
53 	{ ESPI_VWIRE_SIGNAL_SUS_ACK, 0, ESPI_TARGET_TO_CONTROLLER },
54 	{ ESPI_VWIRE_SIGNAL_DNX_ACK, 0, ESPI_TARGET_TO_CONTROLLER },
55 	{ ESPI_VWIRE_SIGNAL_SUS_WARN, 0, ESPI_CONTROLLER_TO_TARGET },
56 	{ ESPI_VWIRE_SIGNAL_SUS_PWRDN_ACK, 0, ESPI_CONTROLLER_TO_TARGET },
57 	{ ESPI_VWIRE_SIGNAL_SLP_A, 0, ESPI_CONTROLLER_TO_TARGET },
58 	{ ESPI_VWIRE_SIGNAL_SLP_LAN, 0, ESPI_CONTROLLER_TO_TARGET },
59 	{ ESPI_VWIRE_SIGNAL_SLP_WLAN, 0, ESPI_CONTROLLER_TO_TARGET },
60 	{ ESPI_VWIRE_SIGNAL_HOST_C10, 0, ESPI_CONTROLLER_TO_TARGET },
61 	{ ESPI_VWIRE_SIGNAL_DNX_WARN, 0, ESPI_CONTROLLER_TO_TARGET },
62 };
63 
64 #define NUMBER_OF_VWIRES ARRAY_SIZE(vw_state_default)
65 
66 /** Run-time data used by the emulator */
67 struct espi_host_emul_data {
68 	/** eSPI emulator detail */
69 	struct espi_emul emul;
70 	/** eSPI controller device */
71 	const struct device *espi;
72 	/** Virtual Wires states, for one slave only.
73 	 *  With multi-slaves config, the states should be saved per slave */
74 	struct vw_data vw_state[NUMBER_OF_VWIRES];
75 #ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION
76 	/** ACPI Shared memory. */
77 	uint8_t shm_acpi_mmap[CONFIG_EMUL_ESPI_HOST_ACPI_SHM_REGION_SIZE];
78 #endif
79 };
80 
81 /** Static configuration for the emulator */
82 struct espi_host_emul_cfg {
83 	/** Label of the emulated AP*/
84 	const char *label;
85 	/* eSPI chip-select of the emulated device */
86 	uint16_t chipsel;
87 };
88 
89 /**
90  * Initialize the state of virtual wires to default based on
91  * the vw_state_default array.
92  *
93  * @param data Host emulator data with the vwire array
94  */
emul_host_init_vw_state(struct espi_host_emul_data * data)95 static void emul_host_init_vw_state(struct espi_host_emul_data *data)
96 {
97 	unsigned i;
98 
99 	for (i = 0; i < NUMBER_OF_VWIRES; i++) {
100 		data->vw_state[i] = vw_state_default[i];
101 	}
102 	return;
103 }
104 
105 /**
106  * Find a virtual wire in the array placed in the host data.
107  *
108  * @param data Host emulator data with the vwire array
109  * @param vw Virtual wire signal to be found
110  * @return index in the array
111  * @return -1 if not found
112  */
emul_host_find_index(struct espi_host_emul_data * data,enum espi_vwire_signal vw)113 static int emul_host_find_index(struct espi_host_emul_data *data,
114 				enum espi_vwire_signal vw)
115 {
116 	int idx;
117 
118 	for (idx = 0; idx < NUMBER_OF_VWIRES; idx++) {
119 		if (data->vw_state[idx].sig == vw) {
120 			return idx;
121 		}
122 	}
123 
124 	return -1;
125 }
126 
emul_host_set_vw(const struct emul * target,enum espi_vwire_signal vw,uint8_t level)127 static int emul_host_set_vw(const struct emul *target,
128 			    enum espi_vwire_signal vw, uint8_t level)
129 {
130 	struct espi_host_emul_data *data = target->data;
131 	int idx;
132 
133 	idx = emul_host_find_index(data, vw);
134 
135 	if (idx < 0 || data->vw_state[idx].dir != ESPI_TARGET_TO_CONTROLLER) {
136 		LOG_ERR("%s: invalid vw: %d", __func__, vw);
137 		return -EPERM;
138 	}
139 
140 	data->vw_state[idx].level = level;
141 
142 	return 0;
143 }
144 
emul_host_get_vw(const struct emul * target,enum espi_vwire_signal vw,uint8_t * level)145 static int emul_host_get_vw(const struct emul *target,
146 			    enum espi_vwire_signal vw, uint8_t *level)
147 {
148 	struct espi_host_emul_data *data = target->data;
149 	int idx;
150 
151 	idx = emul_host_find_index(data, vw);
152 
153 	if (idx < 0 || data->vw_state[idx].dir != ESPI_CONTROLLER_TO_TARGET) {
154 		LOG_ERR("%s: invalid vw: %d", __func__, vw);
155 		return -EPERM;
156 	}
157 
158 	*level = data->vw_state[idx].level;
159 
160 	return 0;
161 }
162 
emul_espi_host_send_vw(const struct device * espi_dev,enum espi_vwire_signal vw,uint8_t level)163 int emul_espi_host_send_vw(const struct device *espi_dev, enum espi_vwire_signal vw,
164 			   uint8_t level)
165 {
166 	struct espi_emul *emul_espi;
167 	struct espi_event evt;
168 	struct espi_host_emul_data *data_host;
169 	struct emul_espi_driver_api *api;
170 	int idx;
171 
172 	api = (struct emul_espi_driver_api *)espi_dev->api;
173 
174 	__ASSERT_NO_MSG(api);
175 	__ASSERT_NO_MSG(api->trigger_event);
176 	__ASSERT_NO_MSG(api->find_emul);
177 
178 	emul_espi = api->find_emul(espi_dev, EMUL_ESPI_HOST_CHIPSEL);
179 	data_host = emul_espi->target->data;
180 
181 	idx = emul_host_find_index(data_host, vw);
182 	if (idx < 0 || data_host->vw_state[idx].dir != ESPI_CONTROLLER_TO_TARGET) {
183 		LOG_ERR("%s: invalid vw: %d", __func__, vw);
184 		return -EPERM;
185 	}
186 
187 	data_host->vw_state[idx].level = level;
188 
189 	evt.evt_type = ESPI_BUS_EVENT_VWIRE_RECEIVED;
190 	evt.evt_details = vw;
191 	evt.evt_data = level;
192 
193 	api->trigger_event(espi_dev, &evt);
194 
195 	return 0;
196 }
197 
emul_espi_host_port80_write(const struct device * espi_dev,uint32_t data)198 int emul_espi_host_port80_write(const struct device *espi_dev, uint32_t data)
199 {
200 	struct espi_event evt;
201 	struct emul_espi_driver_api *api;
202 
203 	api = (struct emul_espi_driver_api *)espi_dev->api;
204 
205 	__ASSERT_NO_MSG(api);
206 	__ASSERT_NO_MSG(api->trigger_event);
207 
208 	evt.evt_type = ESPI_BUS_PERIPHERAL_NOTIFICATION;
209 	evt.evt_details = ESPI_PERIPHERAL_DEBUG_PORT80;
210 	evt.evt_data = data;
211 
212 	api->trigger_event(espi_dev, &evt);
213 
214 	return 0;
215 }
216 
217 #ifdef CONFIG_ESPI_PERIPHERAL_ACPI_SHM_REGION
emul_espi_dev_get_acpi_shm(const struct emul * target)218 static uintptr_t emul_espi_dev_get_acpi_shm(const struct emul *target)
219 {
220 	struct espi_host_emul_data *data = target->data;
221 
222 	return (uintptr_t)data->shm_acpi_mmap;
223 }
224 
emul_espi_host_get_acpi_shm(const struct device * espi_dev)225 uintptr_t emul_espi_host_get_acpi_shm(const struct device *espi_dev)
226 {
227 	uint32_t shm;
228 	int rc = espi_read_lpc_request(espi_dev, EACPI_GET_SHARED_MEMORY, &shm);
229 
230 	__ASSERT_NO_MSG(rc == 0);
231 
232 	return (uintptr_t) shm;
233 }
234 #endif
235 
236 /* Device instantiation */
237 static struct emul_espi_device_api ap_emul_api = {
238 	.set_vw = emul_host_set_vw,
239 	.get_vw = emul_host_get_vw,
240 	.get_acpi_shm = emul_espi_dev_get_acpi_shm,
241 };
242 
243 /**
244  * Set up a new eSPI host emulator
245  *
246  * @param emul Emulation information
247  * @param bus Device to emulated eSPI controller
248  * @return 0 indicating success (always)
249  */
emul_host_init(const struct emul * emul,const struct device * bus)250 static int emul_host_init(const struct emul *emul, const struct device *bus)
251 {
252 	struct espi_host_emul_data *data = emul->data;
253 
254 	ARG_UNUSED(bus);
255 
256 	emul_host_init_vw_state(data);
257 
258 	return 0;
259 }
260 
261 #define HOST_EMUL(n)                                                                               \
262 	static struct espi_host_emul_data espi_host_emul_data_##n;                                 \
263 	static const struct espi_host_emul_cfg espi_host_emul_cfg_##n = {                          \
264 		.chipsel = DT_INST_REG_ADDR(n),                                                    \
265 	};                                                                                         \
266 	EMUL_DT_INST_DEFINE(n, emul_host_init, &espi_host_emul_data_##n, &espi_host_emul_cfg_##n,  \
267 			    &ap_emul_api, NULL)
268 
269 DT_INST_FOREACH_STATUS_OKAY(HOST_EMUL)
270