summaryrefslogtreecommitdiff
path: root/arch/arm/mach-socfpga/smc_api.c
blob: b212a94b32127826197923b95e93db83dfb5f3fc (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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2020 Intel Corporation <www.intel.com>
 * Copyright (C) 2025 Altera Corporation <www.altera.com>
 *
 */

#include <cpu_func.h>
#include <asm/ptrace.h>
#include <asm/system.h>
#include <linux/errno.h>
#include <linux/intel-smc.h>
#include <linux/string.h>

int invoke_smc(u32 func_id, u64 *args, int arg_len, u64 *ret_arg, int ret_len)
{
	struct pt_regs regs;

	memset(&regs, 0, sizeof(regs));
	regs.regs[0] = func_id;

	if (args)
		memcpy(&regs.regs[1], args, arg_len * sizeof(*args));

	smc_call(&regs);

	if (ret_arg)
		memcpy(ret_arg, &regs.regs[1], ret_len * sizeof(*ret_arg));

	return regs.regs[0];
}

int smc_send_mailbox(u32 cmd, u32 len, u32 *arg, u8 urgent, u32 *resp_buf_len,
		     u32 *resp_buf)
{
	int ret;
	u64 args[6];
	u64 resp[3];

	args[0] = cmd;
	args[1] = (u64)arg;
	args[2] = len;
	args[3] = urgent;
	args[4] = (u64)resp_buf;

	if (arg && len > 0)
		flush_dcache_range((uintptr_t)arg, (uintptr_t)arg + len);

	if (resp_buf && resp_buf_len && *resp_buf_len > 0) {
		args[5] = *resp_buf_len;
		flush_dcache_range((uintptr_t)resp_buf, (uintptr_t)resp_buf + *resp_buf_len);
	} else {
		args[5] = 0;
	}

	ret = invoke_smc(INTEL_SIP_SMC_MBOX_SEND_CMD, args, ARRAY_SIZE(args),
			 resp, ARRAY_SIZE(resp));

	if (ret == INTEL_SIP_SMC_STATUS_OK && resp_buf && resp_buf_len) {
		if (!resp[0])
			*resp_buf_len = resp[1];
	}

	return (int)resp[0];
}

int smc_get_usercode(u32 *usercode)
{
	int ret;
	u64 resp;

	if (!usercode)
		return -EINVAL;

	ret = invoke_smc(INTEL_SIP_SMC_GET_USERCODE, NULL, 0,
			 &resp, 1);

	if (ret == INTEL_SIP_SMC_STATUS_OK)
		*usercode = (u32)resp;

	return ret;
}