summaryrefslogtreecommitdiff
path: root/chip/mt_scp/ipi.c
blob: 05bbed890b3d4e0b14191f8ec6479b40b42df71b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* Copyright 2018 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Inter-Processor Communication (IPC) and Inter-Processor Interrupt (IPI)
 *
 * IPC is a communication bridge between AP and SCP.  AP/SCP sends an IPC
 * interrupt to SCP/AP to inform to collect the commmunication mesesages in the
 * shared buffer.
 *
 * There are 4 IPCs in the current architecture, from IPC0 to IPC3.  The
 * priority of IPC is proportional to its IPC index. IPC3 has the highest
 * priority and IPC0 has the lowest one.
 *
 * IPC0 may contain zero or more IPIs.  Each IPI represents a task or a service,
 * e.g. host command, or video encoding.  IPIs are recognized by IPI ID, which
 * should sync across AP and SCP.  Shared buffer should designated which IPI
 * ID it talks to.
 *
 * Currently, we don't have IPC handlers for IPC1, IPC2, and IPC3.
 */

#include "console.h"
#include "hooks.h"
#include "host_command.h"
#include "ipi_chip.h"
#include "system.h"
#include "task.h"
#include "util.h"

#define CPRINTF(format, args...) cprintf(CC_IPI, format, ##args)
#define CPRINTS(format, args...) cprints(CC_IPI, format, ##args)

#define IPI_MAX_REQUEST_SIZE CONFIG_IPC_SHARED_OBJ_BUF_SIZE
#define IPI_MAX_RESPONSE_SIZE CONFIG_IPC_SHARED_OBJ_BUF_SIZE

static struct mutex ipi_lock;
/* IPC0 shared objects, including send object and receive object. */
static struct ipc_shared_obj *const scp_send_obj =
	(struct ipc_shared_obj *)CONFIG_IPC_SHARED_OBJ_ADDR;
static struct ipc_shared_obj *const scp_recv_obj =
	(struct ipc_shared_obj *)(CONFIG_IPC_SHARED_OBJ_ADDR +
				  sizeof(struct ipc_shared_obj));
#ifdef HAS_TASK_HOSTCMD
static struct host_packet ipi_packet;
#endif

/* Check if SCP to AP IPI is in use. */
static inline int is_ipi_busy(void)
{
	return SCP_HOST_INT & IPC_SCP2HOST_BIT;
}

/* If IPI is declared as a wake-up source, wake AP up. */
static inline void try_to_wakeup_ap(int32_t id)
{
	if (*ipi_wakeup_table[id])
		SCP_SPM_INT = SPM_INT_A2SPM;
}

/* Send data from SCP to AP. */
int ipi_send(int32_t id, const void *buf, uint32_t len, int wait)
{
	/*
	 * TODO(b:117917141): Evaluate if we can remove this once we have the
	 * video/camera feature code base.
	 */
	if (wait && in_interrupt_context())
		/* Prevent from infinity wait when be in ISR context. */
		return EC_ERROR_BUSY;

	if (len > sizeof(scp_send_obj->buffer))
		return EC_ERROR_INVAL;

	task_disable_irq(SCP_IRQ_IPC0);
	mutex_lock(&ipi_lock);

	/* Check if there is already an IPI pending in AP. */
	if (is_ipi_busy()) {
		/*
		 * If the following conditions meet,
		 *   1) There is an IPI pending in AP.
		 *   2) The incoming IPI is a wakeup IPI.
		 * then it assumes that AP is in suspend state.
		 * Send a AP wakeup request to SPM.
		 *
		 * The incoming IPI will be checked if it's a wakeup source.
		 */
		try_to_wakeup_ap(id);

		mutex_unlock(&ipi_lock);
		task_enable_irq(SCP_IRQ_IPC0);

		return EC_ERROR_BUSY;
	}


	scp_send_obj->id = id;
	scp_send_obj->len = len;
	memcpy(scp_send_obj->buffer, buf, len);

	/* Send IPI to AP: interrutp AP to receive IPI messages. */
	try_to_wakeup_ap(id);
	SCP_HOST_INT = IPC_SCP2HOST_BIT;

	while (wait && is_ipi_busy())
		;

	mutex_unlock(&ipi_lock);
	task_enable_irq(SCP_IRQ_IPC0);

	return EC_SUCCESS;
}

static void ipi_handler(void)
{
	if (scp_recv_obj->id >= IPI_COUNT) {
		CPRINTS("#ERR IPI %d", scp_recv_obj->id);
		return;
	}

	/*
	 * Pass the buffer to handler. Each handler should be in charge of
	 * the buffer copying/reading before returning from handler.
	 */
	ipi_handler_table[scp_recv_obj->id](
		scp_recv_obj->id, scp_recv_obj->buffer, scp_recv_obj->len);
}

void ipi_inform_ap(void)
{
	struct scp_run_t scp_run;
	int ret;
#ifdef CONFIG_RPMSG_NAME_SERVICE
	struct rpmsg_ns_msg ns_msg;
#endif

	scp_run.signaled = 1;
	strncpy(scp_run.fw_ver, system_get_version(SYSTEM_IMAGE_RW),
		SCP_FW_VERSION_LEN);
	scp_run.dec_capability = 0;
	scp_run.enc_capability = 0;

	ret = ipi_send(IPI_SCP_INIT, (void *)&scp_run, sizeof(scp_run), 1);

	if (ret)
		ccprintf("Failed to send initialization IPC messages.\n");

#ifdef CONFIG_RPMSG_NAME_SERVICE
	ns_msg.id = IPI_HOST_COMMAND;
	strncpy(ns_msg.name, "cros-ec-rpmsg", RPMSG_NAME_SIZE);
	ret = ipi_send(IPI_NS_SERVICE, &ns_msg, sizeof(ns_msg), 1);
	if (ret)
		ccprintf("Failed to announce host command channel.\n");
#endif
}

#ifdef HAS_TASK_HOSTCMD
static void ipi_send_response_packet(struct host_packet *pkt)
{
	int ret;

	ret = ipi_send(IPI_HOST_COMMAND, pkt->response, pkt->response_size, 0);
	if (ret)
		CPRINTS("#ERR IPI HOSTCMD %d", ret);
}

static void ipi_hostcmd_handler(int32_t id, void *buf, uint32_t len)
{
	uint8_t *in_msg = buf;
	struct ec_host_request *r = (struct ec_host_request *)in_msg;
	int i;

	if (in_msg[0] != EC_HOST_REQUEST_VERSION) {
		CPRINTS("ERROR: Protocol V2 is not supported!");
		CPRINTF("in_msg=[");
		for (i = 0; i < len; i++)
			CPRINTF("%02x ", in_msg[i]);
		CPRINTF("]\n");
		return;
	}

	/* Protocol version 3 */

	ipi_packet.send_response = ipi_send_response_packet;

	/*
	 * Just assign the buffer to request, host_packet_receive
	 * handles the buffer copy.
	 */
	ipi_packet.request = (void *)r;
	ipi_packet.request_temp = NULL;
	ipi_packet.request_max = IPI_MAX_REQUEST_SIZE;
	ipi_packet.request_size = host_request_expected_size(r);

	ipi_packet.response = scp_send_obj->buffer;
	/* Reserve space for the preamble and trailing byte */
	ipi_packet.response_max = IPI_MAX_RESPONSE_SIZE;
	ipi_packet.response_size = 0;

	ipi_packet.driver_result = EC_RES_SUCCESS;

	host_packet_receive(&ipi_packet);
}
DECLARE_IPI(IPI_HOST_COMMAND, ipi_hostcmd_handler, 0);

/*
 * Get protocol information
 */
static int ipi_get_protocol_info(struct host_cmd_handler_args *args)
{
	struct ec_response_get_protocol_info *r = args->response;

	memset(r, 0, sizeof(*r));
	r->protocol_versions |= (1 << 3);
	r->max_request_packet_size = IPI_MAX_REQUEST_SIZE;
	r->max_response_packet_size = IPI_MAX_RESPONSE_SIZE;

	args->response_size = sizeof(*r);

	return EC_SUCCESS;
}
DECLARE_HOST_COMMAND(EC_CMD_GET_PROTOCOL_INFO, ipi_get_protocol_info,
		     EC_VER_MASK(0));
#endif

static void ipi_enable_ipc0_deferred(void)
{
	/* Clear IPC0 IRQs. */
	SCP_GIPC_IN = SCP_GPIC_IN_CLEAR_ALL;

	/* All tasks are up, we can safely enable IPC0 IRQ now. */
	SCP_INTC_IRQ_ENABLE |= IPC0_IRQ_EN;
	task_enable_irq(SCP_IRQ_IPC0);

	/* Inform AP that SCP is inited.  */
	ipi_inform_ap();

	CPRINTS("ipi init");
}
DECLARE_DEFERRED(ipi_enable_ipc0_deferred);

/* Initialize IPI. */
static void ipi_init(void)
{
	/* Clear send share buffer. */
	memset(scp_send_obj, 0, sizeof(struct ipc_shared_obj));

	/* Enable IRQ after all tasks are up.  */
	hook_call_deferred(&ipi_enable_ipc0_deferred_data, 0);
}
DECLARE_HOOK(HOOK_INIT, ipi_init, HOOK_PRIO_DEFAULT);

void ipc_handler(void)
{
	/* TODO(b/117917141): We only support IPC_ID(0) for now. */
	if (SCP_GIPC_IN & SCP_GIPC_IN_CLEAR_IPCN(0))
		ipi_handler();

	SCP_GIPC_IN = SCP_GPIC_IN_CLEAR_ALL;
}
DECLARE_IRQ(SCP_IRQ_IPC0, ipc_handler, 4);