"...intel/soc/common/plat/intel/soc/common/socfpga_sip_svc.c" did not exist on "d57318b7c9584f1613c9223028ef8ea186d6d203"
socfpga_sip_svc.c 12.9 KB
Newer Older
1
/*
2
 * Copyright (c) 2019-2020, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
8
9
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <common/debug.h>
#include <common/runtime_svc.h>
10
#include <lib/mmio.h>
11
12
#include <tools_share/uuid.h>

13
#include "socfpga_mailbox.h"
14
#include "socfpga_reset_manager.h"
15
#include "socfpga_sip_svc.h"
16
17
18
19
20


/* Total buffer the driver can hold */
#define FPGA_CONFIG_BUFFER_SIZE 4

21
22
23
24
25
static int current_block, current_buffer;
static int read_block, max_blocks, is_partial_reconfig;
static uint32_t send_id, rcv_id;
static uint32_t bytes_per_block, blocks_submitted;

26
27
28
29
30
31

/*  SiP Service UUID */
DEFINE_SVC_UUID2(intl_svc_uid,
		0xa85273b0, 0xe85a, 0x4862, 0xa6, 0x2a,
		0xfa, 0x88, 0x88, 0x17, 0x68, 0x81);

32
static uint64_t socfpga_sip_handler(uint32_t smc_fid,
33
34
35
36
37
38
39
40
41
42
43
44
45
46
				   uint64_t x1,
				   uint64_t x2,
				   uint64_t x3,
				   uint64_t x4,
				   void *cookie,
				   void *handle,
				   uint64_t flags)
{
	ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid);
	SMC_RET1(handle, SMC_UNK);
}

struct fpga_config_info fpga_config_buffers[FPGA_CONFIG_BUFFER_SIZE];

Hadi Asyrafi's avatar
Hadi Asyrafi committed
47
static int intel_fpga_sdm_write_buffer(struct fpga_config_info *buffer)
48
{
49
	uint32_t args[3];
50
51

	while (max_blocks > 0 && buffer->size > buffer->size_written) {
Hadi Asyrafi's avatar
Hadi Asyrafi committed
52
53
54
		args[0] = (1<<8);
		args[1] = buffer->addr + buffer->size_written;
		if (buffer->size - buffer->size_written <= bytes_per_block) {
55
56
57
			args[2] = buffer->size - buffer->size_written;
			current_buffer++;
			current_buffer %= FPGA_CONFIG_BUFFER_SIZE;
Hadi Asyrafi's avatar
Hadi Asyrafi committed
58
		} else
59
			args[2] = bytes_per_block;
Hadi Asyrafi's avatar
Hadi Asyrafi committed
60
61

		buffer->size_written += args[2];
62
		mailbox_send_cmd_async(&send_id, MBOX_RECONFIG_DATA, args,
63
					3U, CMD_INDIRECT);
Hadi Asyrafi's avatar
Hadi Asyrafi committed
64
65

		buffer->subblocks_sent++;
66
67
		max_blocks--;
	}
Hadi Asyrafi's avatar
Hadi Asyrafi committed
68
69

	return !max_blocks;
70
71
72
73
}

static int intel_fpga_sdm_write_all(void)
{
Hadi Asyrafi's avatar
Hadi Asyrafi committed
74
75
76
77
	for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++)
		if (intel_fpga_sdm_write_buffer(
			&fpga_config_buffers[current_buffer]))
			break;
78
79
80
	return 0;
}

81
static uint32_t intel_mailbox_fpga_config_isdone(uint32_t query_type)
82
{
83
84
85
86
87
88
	uint32_t ret;

	if (query_type == 1)
		ret = intel_mailbox_get_config_status(MBOX_CONFIG_STATUS);
	else
		ret = intel_mailbox_get_config_status(MBOX_RECONFIG_STATUS);
Hadi Asyrafi's avatar
Hadi Asyrafi committed
89
90
91
92
93
94
95
96

	if (ret) {
		if (ret == MBOX_CFGSTAT_STATE_CONFIG)
			return INTEL_SIP_SMC_STATUS_BUSY;
		else
			return INTEL_SIP_SMC_STATUS_ERROR;
	}

97
98
99
100
101
102
	if (query_type != 1) {
		/* full reconfiguration */
		if (!is_partial_reconfig)
			socfpga_bridges_enable();	/* Enable bridge */
	}

Hadi Asyrafi's avatar
Hadi Asyrafi committed
103
	return INTEL_SIP_SMC_STATUS_OK;
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
}

static int mark_last_buffer_xfer_completed(uint32_t *buffer_addr_completed)
{
	int i;

	for (i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) {
		if (fpga_config_buffers[i].block_number == current_block) {
			fpga_config_buffers[i].subblocks_sent--;
			if (fpga_config_buffers[i].subblocks_sent == 0
			&& fpga_config_buffers[i].size <=
			fpga_config_buffers[i].size_written) {
				fpga_config_buffers[i].write_requested = 0;
				current_block++;
				*buffer_addr_completed =
					fpga_config_buffers[i].addr;
				return 0;
			}
		}
	}

	return -1;
}

128
static int intel_fpga_config_completed_write(uint32_t *completed_addr,
129
					uint32_t *count, uint32_t *job_id)
130
131
132
133
134
135
136
{
	uint32_t status = INTEL_SIP_SMC_STATUS_OK;
	*count = 0;
	int resp_len = 0;
	uint32_t resp[5];
	int all_completed = 1;

137
	while (*count < 3) {
138

139
		resp_len = mailbox_read_response(job_id,
140
				resp, ARRAY_SIZE(resp));
141

142
143
		if (resp_len < 0)
			break;
144
145

		max_blocks++;
146

147
148
149
150
151
152
153
154
155
156
		if (mark_last_buffer_xfer_completed(
			&completed_addr[*count]) == 0)
			*count = *count + 1;
		else
			break;
	}

	if (*count <= 0) {
		if (resp_len != MBOX_NO_RESPONSE &&
			resp_len != MBOX_TIMEOUT && resp_len != 0) {
157
			mailbox_clear_response();
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
			return INTEL_SIP_SMC_STATUS_ERROR;
		}

		*count = 0;
	}

	intel_fpga_sdm_write_all();

	if (*count > 0)
		status = INTEL_SIP_SMC_STATUS_OK;
	else if (*count == 0)
		status = INTEL_SIP_SMC_STATUS_BUSY;

	for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) {
		if (fpga_config_buffers[i].write_requested != 0) {
			all_completed = 0;
			break;
		}
	}

	if (all_completed == 1)
		return INTEL_SIP_SMC_STATUS_OK;

	return status;
}

184
static int intel_fpga_config_start(uint32_t config_type)
185
186
187
188
{
	uint32_t response[3];
	int status = 0;

189
190
	is_partial_reconfig = config_type;

191
192
	mailbox_clear_response();

193
	mailbox_send_cmd(1U, MBOX_CMD_CANCEL, NULL, 0U, CMD_CASUAL, NULL, 0U);
194

195
	status = mailbox_send_cmd(1U, MBOX_RECONFIG, NULL, 0U, CMD_CASUAL,
196
			response, ARRAY_SIZE(response));
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

	if (status < 0)
		return status;

	max_blocks = response[0];
	bytes_per_block = response[1];

	for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) {
		fpga_config_buffers[i].size = 0;
		fpga_config_buffers[i].size_written = 0;
		fpga_config_buffers[i].addr = 0;
		fpga_config_buffers[i].write_requested = 0;
		fpga_config_buffers[i].block_number = 0;
		fpga_config_buffers[i].subblocks_sent = 0;
	}

	blocks_submitted = 0;
	current_block = 0;
215
	read_block = 0;
216
217
	current_buffer = 0;

218
219
220
221
222
223
	/* full reconfiguration */
	if (!is_partial_reconfig) {
		/* Disable bridge */
		socfpga_bridges_disable();
	}

224
225
226
	return 0;
}

Hadi Asyrafi's avatar
Hadi Asyrafi committed
227
228
229
230
231
232
233
234
static bool is_fpga_config_buffer_full(void)
{
	for (int i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++)
		if (!fpga_config_buffers[i].write_requested)
			return false;
	return true;
}

235
bool is_address_in_ddr_range(uint64_t addr, uint64_t size)
Hadi Asyrafi's avatar
Hadi Asyrafi committed
236
{
237
238
	if (size > (UINT64_MAX - addr))
		return false;
239
	if (addr < BL31_LIMIT)
240
241
242
		return false;
	if (addr + size > DRAM_BASE + DRAM_SIZE)
		return false;
Hadi Asyrafi's avatar
Hadi Asyrafi committed
243

244
	return true;
Hadi Asyrafi's avatar
Hadi Asyrafi committed
245
}
246

247
static uint32_t intel_fpga_config_write(uint64_t mem, uint64_t size)
248
{
Hadi Asyrafi's avatar
Hadi Asyrafi committed
249
	int i;
250

Hadi Asyrafi's avatar
Hadi Asyrafi committed
251
	intel_fpga_sdm_write_all();
252

253
	if (!is_address_in_ddr_range(mem, size) ||
Hadi Asyrafi's avatar
Hadi Asyrafi committed
254
255
		is_fpga_config_buffer_full())
		return INTEL_SIP_SMC_STATUS_REJECTED;
256
257

	for (i = 0; i < FPGA_CONFIG_BUFFER_SIZE; i++) {
Hadi Asyrafi's avatar
Hadi Asyrafi committed
258
259
260
261
262
263
264
265
		int j = (i + current_buffer) % FPGA_CONFIG_BUFFER_SIZE;

		if (!fpga_config_buffers[j].write_requested) {
			fpga_config_buffers[j].addr = mem;
			fpga_config_buffers[j].size = size;
			fpga_config_buffers[j].size_written = 0;
			fpga_config_buffers[j].write_requested = 1;
			fpga_config_buffers[j].block_number =
266
				blocks_submitted++;
Hadi Asyrafi's avatar
Hadi Asyrafi committed
267
			fpga_config_buffers[j].subblocks_sent = 0;
268
269
270
271
			break;
		}
	}

Hadi Asyrafi's avatar
Hadi Asyrafi committed
272
273
	if (is_fpga_config_buffer_full())
		return INTEL_SIP_SMC_STATUS_BUSY;
274

Hadi Asyrafi's avatar
Hadi Asyrafi committed
275
	return INTEL_SIP_SMC_STATUS_OK;
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
static int is_out_of_sec_range(uint64_t reg_addr)
{
	switch (reg_addr) {
	case(0xF8011100):	/* ECCCTRL1 */
	case(0xF8011104):	/* ECCCTRL2 */
	case(0xF8011110):	/* ERRINTEN */
	case(0xF8011114):	/* ERRINTENS */
	case(0xF8011118):	/* ERRINTENR */
	case(0xF801111C):	/* INTMODE */
	case(0xF8011120):	/* INTSTAT */
	case(0xF8011124):	/* DIAGINTTEST */
	case(0xF801112C):	/* DERRADDRA */
	case(0xFFD12028):	/* SDMMCGRP_CTRL */
	case(0xFFD12044):	/* EMAC0 */
	case(0xFFD12048):	/* EMAC1 */
	case(0xFFD1204C):	/* EMAC2 */
	case(0xFFD12090):	/* ECC_INT_MASK_VALUE */
	case(0xFFD12094):	/* ECC_INT_MASK_SET */
	case(0xFFD12098):	/* ECC_INT_MASK_CLEAR */
	case(0xFFD1209C):	/* ECC_INTSTATUS_SERR */
	case(0xFFD120A0):	/* ECC_INTSTATUS_DERR */
	case(0xFFD120C0):	/* NOC_TIMEOUT */
	case(0xFFD120C4):	/* NOC_IDLEREQ_SET */
	case(0xFFD120C8):	/* NOC_IDLEREQ_CLR */
	case(0xFFD120D0):	/* NOC_IDLEACK */
	case(0xFFD120D4):	/* NOC_IDLESTATUS */
	case(0xFFD12200):	/* BOOT_SCRATCH_COLD0 */
	case(0xFFD12204):	/* BOOT_SCRATCH_COLD1 */
	case(0xFFD12220):	/* BOOT_SCRATCH_COLD8 */
	case(0xFFD12224):	/* BOOT_SCRATCH_COLD9 */
		return 0;

	default:
		break;
	}

	return -1;
}

/* Secure register access */
uint32_t intel_secure_reg_read(uint64_t reg_addr, uint32_t *retval)
{
	if (is_out_of_sec_range(reg_addr))
		return INTEL_SIP_SMC_STATUS_ERROR;

	*retval = mmio_read_32(reg_addr);

	return INTEL_SIP_SMC_STATUS_OK;
}

uint32_t intel_secure_reg_write(uint64_t reg_addr, uint32_t val,
				uint32_t *retval)
{
	if (is_out_of_sec_range(reg_addr))
		return INTEL_SIP_SMC_STATUS_ERROR;

	mmio_write_32(reg_addr, val);

	return intel_secure_reg_read(reg_addr, retval);
}

uint32_t intel_secure_reg_update(uint64_t reg_addr, uint32_t mask,
				 uint32_t val, uint32_t *retval)
{
	if (!intel_secure_reg_read(reg_addr, retval)) {
		*retval &= ~mask;
		*retval |= val;
		return intel_secure_reg_write(reg_addr, *retval, retval);
	}

	return INTEL_SIP_SMC_STATUS_ERROR;
}

351
352
353
/* Intel Remote System Update (RSU) services */
uint64_t intel_rsu_update_address;

354
static uint32_t intel_rsu_status(uint64_t *respbuf, unsigned int respbuf_sz)
355
356
{
	if (mailbox_rsu_status((uint32_t *)respbuf, respbuf_sz) < 0)
357
		return INTEL_SIP_SMC_RSU_ERROR;
358
359
360
361
362
363
364
365
366
367

	return INTEL_SIP_SMC_STATUS_OK;
}

static uint32_t intel_rsu_update(uint64_t update_address)
{
	intel_rsu_update_address = update_address;
	return INTEL_SIP_SMC_STATUS_OK;
}

368
static uint32_t intel_rsu_notify(uint32_t execution_stage)
369
{
370
	if (mailbox_hps_stage_notify(execution_stage) < 0)
371
		return INTEL_SIP_SMC_RSU_ERROR;
372
373
374
375
376
377
378
379

	return INTEL_SIP_SMC_STATUS_OK;
}

static uint32_t intel_rsu_retry_counter(uint32_t *respbuf, uint32_t respbuf_sz,
					uint32_t *ret_stat)
{
	if (mailbox_rsu_status((uint32_t *)respbuf, respbuf_sz) < 0)
380
		return INTEL_SIP_SMC_RSU_ERROR;
381
382
383
384
385

	*ret_stat = respbuf[8];
	return INTEL_SIP_SMC_STATUS_OK;
}

386
/* Mailbox services */
387
388
389
static uint32_t intel_mbox_send_cmd(uint32_t cmd, uint32_t *args, uint32_t len,
				    uint32_t urgent, uint32_t *response,
				    uint32_t resp_len, int *mbox_status,
390
391
				    int *len_in_resp)
{
392
393
394
395
396
397
	*len_in_resp = 0;
	*mbox_status = 0;

	if (!is_address_in_ddr_range((uint64_t)args, sizeof(uint32_t) * len))
		return INTEL_SIP_SMC_STATUS_REJECTED;

398
399
400
401
402
403
404
405
406
407
408
409
410
	int status = mailbox_send_cmd(MBOX_JOB_ID, cmd, args, len, urgent,
				      response, resp_len);

	if (status < 0) {
		*mbox_status = -status;
		return INTEL_SIP_SMC_STATUS_ERROR;
	}

	*mbox_status = 0;
	*len_in_resp = status;
	return INTEL_SIP_SMC_STATUS_OK;
}

411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * This function is responsible for handling all SiP calls from the NS world
 */

uintptr_t sip_smc_handler(uint32_t smc_fid,
			 u_register_t x1,
			 u_register_t x2,
			 u_register_t x3,
			 u_register_t x4,
			 void *cookie,
			 void *handle,
			 u_register_t flags)
{
424
	uint32_t retval = 0;
425
426
	uint32_t status = INTEL_SIP_SMC_STATUS_OK;
	uint32_t completed_addr[3];
427
	uint64_t rsu_respbuf[9];
428
429
	u_register_t x5, x6;
	int mbox_status, len_in_resp;
430

431

432
433
434
435
	switch (smc_fid) {
	case SIP_SVC_UID:
		/* Return UID to the caller */
		SMC_UUID_RET(handle, intl_svc_uid);
436

437
	case INTEL_SIP_SMC_FPGA_CONFIG_ISDONE:
438
		status = intel_mailbox_fpga_config_isdone(x1);
439
		SMC_RET4(handle, status, 0, 0, 0);
440

441
442
443
444
445
	case INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM:
		SMC_RET3(handle, INTEL_SIP_SMC_STATUS_OK,
			INTEL_SIP_SMC_FPGA_CONFIG_ADDR,
			INTEL_SIP_SMC_FPGA_CONFIG_SIZE -
				INTEL_SIP_SMC_FPGA_CONFIG_ADDR);
446

447
448
449
	case INTEL_SIP_SMC_FPGA_CONFIG_START:
		status = intel_fpga_config_start(x1);
		SMC_RET4(handle, status, 0, 0, 0);
450

451
452
453
	case INTEL_SIP_SMC_FPGA_CONFIG_WRITE:
		status = intel_fpga_config_write(x1, x2);
		SMC_RET4(handle, status, 0, 0, 0);
454

455
456
	case INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE:
		status = intel_fpga_config_completed_write(completed_addr,
457
458
							&retval, &rcv_id);
		switch (retval) {
459
460
461
		case 1:
			SMC_RET4(handle, INTEL_SIP_SMC_STATUS_OK,
				completed_addr[0], 0, 0);
462

463
464
465
466
		case 2:
			SMC_RET4(handle, INTEL_SIP_SMC_STATUS_OK,
				completed_addr[0],
				completed_addr[1], 0);
467

468
469
470
471
472
		case 3:
			SMC_RET4(handle, INTEL_SIP_SMC_STATUS_OK,
				completed_addr[0],
				completed_addr[1],
				completed_addr[2]);
473

474
475
		case 0:
			SMC_RET4(handle, status, 0, 0, 0);
476

477
		default:
478
			mailbox_clear_response();
479
480
			SMC_RET1(handle, INTEL_SIP_SMC_STATUS_ERROR);
		}
481
482

	case INTEL_SIP_SMC_REG_READ:
483
484
		status = intel_secure_reg_read(x1, &retval);
		SMC_RET3(handle, status, retval, x1);
485
486

	case INTEL_SIP_SMC_REG_WRITE:
487
488
		status = intel_secure_reg_write(x1, (uint32_t)x2, &retval);
		SMC_RET3(handle, status, retval, x1);
489
490
491

	case INTEL_SIP_SMC_REG_UPDATE:
		status = intel_secure_reg_update(x1, (uint32_t)x2,
492
493
						 (uint32_t)x3, &retval);
		SMC_RET3(handle, status, retval, x1);
494

495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
	case INTEL_SIP_SMC_RSU_STATUS:
		status = intel_rsu_status(rsu_respbuf,
					ARRAY_SIZE(rsu_respbuf));
		if (status) {
			SMC_RET1(handle, status);
		} else {
			SMC_RET4(handle, rsu_respbuf[0], rsu_respbuf[1],
					rsu_respbuf[2], rsu_respbuf[3]);
		}

	case INTEL_SIP_SMC_RSU_UPDATE:
		status = intel_rsu_update(x1);
		SMC_RET1(handle, status);

	case INTEL_SIP_SMC_RSU_NOTIFY:
		status = intel_rsu_notify(x1);
		SMC_RET1(handle, status);

	case INTEL_SIP_SMC_RSU_RETRY_COUNTER:
		status = intel_rsu_retry_counter((uint32_t *)rsu_respbuf,
515
						ARRAY_SIZE(rsu_respbuf), &retval);
516
517
518
		if (status) {
			SMC_RET1(handle, status);
		} else {
519
			SMC_RET2(handle, status, retval);
520
521
		}

522
523
524
	case INTEL_SIP_SMC_MBOX_SEND_CMD:
		x5 = SMC_GET_GP(handle, CTX_GPREG_X5);
		x6 = SMC_GET_GP(handle, CTX_GPREG_X6);
525
		status = intel_mbox_send_cmd(x1, (uint32_t *)x2, x3, x4,
526
527
528
529
					     (uint32_t *)x5, x6, &mbox_status,
					     &len_in_resp);
		SMC_RET4(handle, status, mbox_status, x5, len_in_resp);

530
531
532
533
534
535
536
	default:
		return socfpga_sip_handler(smc_fid, x1, x2, x3, x4,
			cookie, handle, flags);
	}
}

DECLARE_RT_SVC(
537
	socfpga_sip_svc,
538
539
540
541
542
543
544
545
	OEN_SIP_START,
	OEN_SIP_END,
	SMC_TYPE_FAST,
	NULL,
	sip_smc_handler
);

DECLARE_RT_SVC(
546
	socfpga_sip_svc_std,
547
548
549
550
551
552
	OEN_SIP_START,
	OEN_SIP_END,
	SMC_TYPE_YIELD,
	NULL,
	sip_smc_handler
);