summaryrefslogtreecommitdiffstats
path: root/drivers/arm/mhu/mhu_wrapper_v2_x.c
blob: 60de1d38c89e122c01fce3b08e6e7a5037b45e67 (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 * Copyright (c) 2022, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <drivers/arm/mhu.h>

#include "mhu_v2_x.h"

#define MHU_NOTIFY_VALUE	(1234u)

/*
 * MHU devices for host:
 * HSE: Host to Secure Enclave (sender device)
 * SEH: Secure Enclave to Host (receiver device)
 */
struct mhu_v2_x_dev_t MHU1_HSE_DEV = {0, MHU_V2_X_SENDER_FRAME};
struct mhu_v2_x_dev_t MHU1_SEH_DEV = {0, MHU_V2_X_RECEIVER_FRAME};

static enum mhu_error_t error_mapping_to_mhu_error_t(enum mhu_v2_x_error_t err)
{
	switch (err) {
	case MHU_V_2_X_ERR_NONE:
		return MHU_ERR_NONE;
	case MHU_V_2_X_ERR_NOT_INIT:
		return MHU_ERR_NOT_INIT;
	case MHU_V_2_X_ERR_ALREADY_INIT:
		return MHU_ERR_ALREADY_INIT;
	case MHU_V_2_X_ERR_UNSUPPORTED_VERSION:
		return MHU_ERR_UNSUPPORTED_VERSION;
	case MHU_V_2_X_ERR_INVALID_ARG:
		return MHU_ERR_INVALID_ARG;
	case MHU_V_2_X_ERR_GENERAL:
		return MHU_ERR_GENERAL;
	default:
		return MHU_ERR_GENERAL;
	}
}

static enum mhu_v2_x_error_t signal_and_wait_for_clear(void)
{
	enum mhu_v2_x_error_t err;
	struct mhu_v2_x_dev_t *dev = &MHU1_HSE_DEV;
	uint32_t val = MHU_NOTIFY_VALUE;
	/* Using the last channel for notifications */
	uint32_t channel_notify = mhu_v2_x_get_num_channel_implemented(dev) - 1;

	err = mhu_v2_x_channel_send(dev, channel_notify, val);
	if (err != MHU_V_2_X_ERR_NONE) {
		return err;
	}

	do {
		err = mhu_v2_x_channel_poll(dev, channel_notify, &val);
		if (err != MHU_V_2_X_ERR_NONE) {
			break;
		}
	} while (val != 0);

	return err;
}

static enum mhu_v2_x_error_t wait_for_signal(void)
{
	enum mhu_v2_x_error_t err;
	struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
	uint32_t val = 0;
	/* Using the last channel for notifications */
	uint32_t channel_notify = mhu_v2_x_get_num_channel_implemented(dev) - 1;

	do {
		err = mhu_v2_x_channel_receive(dev, channel_notify, &val);
		if (err != MHU_V_2_X_ERR_NONE) {
			break;
		}
	} while (val != MHU_NOTIFY_VALUE);

	return err;
}

static enum mhu_v2_x_error_t clear_and_wait_for_next_signal(void)
{
	enum mhu_v2_x_error_t err;
	struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
	uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);
	uint32_t i;

	/* Clear all channels */
	for (i = 0; i < num_channels; ++i) {
		err = mhu_v2_x_channel_clear(dev, i);
		if (err != MHU_V_2_X_ERR_NONE) {
			return err;
		}
	}

	return wait_for_signal();
}

enum mhu_error_t mhu_init_sender(uintptr_t mhu_sender_base)
{
	enum mhu_v2_x_error_t err;

	assert(mhu_sender_base != (uintptr_t)NULL);

	MHU1_HSE_DEV.base = mhu_sender_base;

	err = mhu_v2_x_driver_init(&MHU1_HSE_DEV, MHU_REV_READ_FROM_HW);
	return error_mapping_to_mhu_error_t(err);
}

enum mhu_error_t mhu_init_receiver(uintptr_t mhu_receiver_base)
{
	enum mhu_v2_x_error_t err;
	uint32_t num_channels, i;

	assert(mhu_receiver_base != (uintptr_t)NULL);

	MHU1_SEH_DEV.base = mhu_receiver_base;

	err = mhu_v2_x_driver_init(&MHU1_SEH_DEV, MHU_REV_READ_FROM_HW);
	if (err != MHU_V_2_X_ERR_NONE) {
		return error_mapping_to_mhu_error_t(err);
	}

	num_channels = mhu_v2_x_get_num_channel_implemented(&MHU1_SEH_DEV);

	/* Mask all channels except the notifying channel */
	for (i = 0; i < (num_channels - 1); ++i) {
		err = mhu_v2_x_channel_mask_set(&MHU1_SEH_DEV, i, UINT32_MAX);
		if (err != MHU_V_2_X_ERR_NONE) {
			return error_mapping_to_mhu_error_t(err);
		}
	}

	/* The last channel is used for notifications */
	err = mhu_v2_x_channel_mask_clear(
		&MHU1_SEH_DEV, (num_channels - 1), UINT32_MAX);
	return error_mapping_to_mhu_error_t(err);
}

/*
 * Public function. See mhu.h
 *
 * The basic steps of transferring a message:
 * 1.	Initiate MHU transfer.
 * 2.	Send over the size of the payload on Channel 1. It is the very first
 *	4 Bytes of the transfer. Continue with Channel 2.
 * 3.	Send over the payload, writing the channels one after the other
 *	(4 Bytes each). The last available channel is reserved for controlling
 *	the transfer.
 *	When the last channel is reached or no more data is left, STOP.
 * 4.	Notify the receiver using the last channel and wait for acknowledge.
 *	If there is still data to transfer, jump to step 3. Otherwise, proceed.
 * 5.	Close MHU transfer.
 *
 */
enum mhu_error_t mhu_send_data(const uint8_t *send_buffer, size_t size)
{
	enum mhu_v2_x_error_t err;
	struct mhu_v2_x_dev_t *dev = &MHU1_HSE_DEV;
	uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);
	uint32_t chan = 0;
	uint32_t i;
	uint32_t *p;

	/* For simplicity, require the send_buffer to be 4-byte aligned */
	if ((uintptr_t)send_buffer & 0x3U) {
		return MHU_ERR_INVALID_ARG;
	}

	err = mhu_v2_x_initiate_transfer(dev);
	if (err != MHU_V_2_X_ERR_NONE) {
		return error_mapping_to_mhu_error_t(err);
	}

	/* First send over the size of the actual message */
	err = mhu_v2_x_channel_send(dev, chan, (uint32_t)size);
	if (err != MHU_V_2_X_ERR_NONE) {
		return error_mapping_to_mhu_error_t(err);
	}
	chan++;

	p = (uint32_t *)send_buffer;
	for (i = 0; i < size; i += 4) {
		err = mhu_v2_x_channel_send(dev, chan, *p++);
		if (err != MHU_V_2_X_ERR_NONE) {
			return error_mapping_to_mhu_error_t(err);
		}
		if (++chan == (num_channels - 1)) {
			err = signal_and_wait_for_clear();
			if (err != MHU_V_2_X_ERR_NONE) {
				return error_mapping_to_mhu_error_t(err);
			}
			chan = 0;
		}
	}

	/* Signal the end of transfer.
	 *   It's not required to send a signal when the message was
	 *   perfectly-aligned (num_channels - 1 channels were used in the last
	 *   round) preventing it from signaling twice at the end of transfer.
	 */
	if (chan != 0) {
		err = signal_and_wait_for_clear();
		if (err != MHU_V_2_X_ERR_NONE) {
			return error_mapping_to_mhu_error_t(err);
		}
	}

	err = mhu_v2_x_close_transfer(dev);
	return error_mapping_to_mhu_error_t(err);
}

/*
 * Public function. See mhu.h
 *
 * The basic steps of receiving a message:
 * 1.	Read the size of the payload from Channel 1. It is the very first
 *	4 Bytes of the transfer. Continue with Channel 2.
 * 2.	Receive the payload, read the channels one after the other
 *	(4 Bytes each). The last available channel is reserved for controlling
 *	the transfer.
 *	When the last channel is reached clear all the channels
 *	(also sending an acknowledge on the last channel).
 * 3.	If there is still data to receive wait for a notification on the last
 *	channel and jump to step 2 as soon as it arrived. Otherwise, proceed.
 * 4.	End of transfer.
 *
 */
enum mhu_error_t mhu_receive_data(uint8_t *receive_buffer, size_t *size)
{
	enum mhu_v2_x_error_t err;
	struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
	uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);
	uint32_t chan = 0;
	uint32_t message_len;
	uint32_t i;
	uint32_t *p;

	/* For simplicity, require:
	 * - the receive_buffer to be 4-byte aligned,
	 * - the buffer size to be a multiple of 4.
	 */
	if (((uintptr_t)receive_buffer & 0x3U) || (*size & 0x3U)) {
		return MHU_ERR_INVALID_ARG;
	}

	/* Busy wait for incoming reply */
	err = wait_for_signal();
	if (err != MHU_V_2_X_ERR_NONE) {
		return error_mapping_to_mhu_error_t(err);
	}

	/* The first word is the length of the actual message */
	err = mhu_v2_x_channel_receive(dev, chan, &message_len);
	if (err != MHU_V_2_X_ERR_NONE) {
		return error_mapping_to_mhu_error_t(err);
	}
	chan++;

	if (message_len > *size) {
		/* Message buffer too small */
		*size = message_len;
		return MHU_ERR_BUFFER_TOO_SMALL;
	}

	p = (uint32_t *)receive_buffer;
	for (i = 0; i < message_len; i += 4) {
		err = mhu_v2_x_channel_receive(dev, chan, p++);
		if (err != MHU_V_2_X_ERR_NONE) {
			return error_mapping_to_mhu_error_t(err);
		}

		/* Only wait for next transfer if there is still missing data */
		if (++chan == (num_channels - 1) && (message_len - i) > 4) {
			/* Busy wait for next transfer */
			err = clear_and_wait_for_next_signal();
			if (err != MHU_V_2_X_ERR_NONE) {
				return error_mapping_to_mhu_error_t(err);
			}
			chan = 0;
		}
	}

	/* Clear all channels */
	for (i = 0; i < num_channels; ++i) {
		err = mhu_v2_x_channel_clear(dev, i);
		if (err != MHU_V_2_X_ERR_NONE) {
			return error_mapping_to_mhu_error_t(err);
		}
	}

	*size = message_len;

	return MHU_ERR_NONE;
}

size_t mhu_get_max_message_size(void)
{
	struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
	uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);

	assert(num_channels != 0);

	return num_channels * sizeof(uint32_t);
}