blob: b212a94b32127826197923b95e93db83dfb5f3fc [file] [log] [blame]
Chee Hong Angb5ddb912020-12-24 18:21:00 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Intel Corporation <www.intel.com>
Alif Zakuan Yuslaimi6aa360d2025-02-18 16:35:03 +08004 * Copyright (C) 2025 Altera Corporation <www.altera.com>
Chee Hong Angb5ddb912020-12-24 18:21:00 +08005 *
6 */
7
Alif Zakuan Yuslaimi6aa360d2025-02-18 16:35:03 +08008#include <cpu_func.h>
Chee Hong Angb5ddb912020-12-24 18:21:00 +08009#include <asm/ptrace.h>
10#include <asm/system.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060011#include <linux/errno.h>
Chee Hong Angb5ddb912020-12-24 18:21:00 +080012#include <linux/intel-smc.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060013#include <linux/string.h>
Chee Hong Angb5ddb912020-12-24 18:21:00 +080014
15int invoke_smc(u32 func_id, u64 *args, int arg_len, u64 *ret_arg, int ret_len)
16{
17 struct pt_regs regs;
18
19 memset(&regs, 0, sizeof(regs));
20 regs.regs[0] = func_id;
21
22 if (args)
23 memcpy(&regs.regs[1], args, arg_len * sizeof(*args));
24
25 smc_call(&regs);
26
27 if (ret_arg)
28 memcpy(ret_arg, &regs.regs[1], ret_len * sizeof(*ret_arg));
29
30 return regs.regs[0];
31}
32
33int smc_send_mailbox(u32 cmd, u32 len, u32 *arg, u8 urgent, u32 *resp_buf_len,
34 u32 *resp_buf)
35{
36 int ret;
37 u64 args[6];
38 u64 resp[3];
39
40 args[0] = cmd;
41 args[1] = (u64)arg;
42 args[2] = len;
43 args[3] = urgent;
44 args[4] = (u64)resp_buf;
Alif Zakuan Yuslaimi6aa360d2025-02-18 16:35:03 +080045
46 if (arg && len > 0)
47 flush_dcache_range((uintptr_t)arg, (uintptr_t)arg + len);
48
49 if (resp_buf && resp_buf_len && *resp_buf_len > 0) {
Chee Hong Angb5ddb912020-12-24 18:21:00 +080050 args[5] = *resp_buf_len;
Alif Zakuan Yuslaimi6aa360d2025-02-18 16:35:03 +080051 flush_dcache_range((uintptr_t)resp_buf, (uintptr_t)resp_buf + *resp_buf_len);
52 } else {
Chee Hong Angb5ddb912020-12-24 18:21:00 +080053 args[5] = 0;
Alif Zakuan Yuslaimi6aa360d2025-02-18 16:35:03 +080054 }
Chee Hong Angb5ddb912020-12-24 18:21:00 +080055
56 ret = invoke_smc(INTEL_SIP_SMC_MBOX_SEND_CMD, args, ARRAY_SIZE(args),
57 resp, ARRAY_SIZE(resp));
58
59 if (ret == INTEL_SIP_SMC_STATUS_OK && resp_buf && resp_buf_len) {
60 if (!resp[0])
61 *resp_buf_len = resp[1];
62 }
63
64 return (int)resp[0];
65}
Siew Chin Lim54744572021-03-25 14:07:45 +080066
67int smc_get_usercode(u32 *usercode)
68{
69 int ret;
70 u64 resp;
71
72 if (!usercode)
73 return -EINVAL;
74
75 ret = invoke_smc(INTEL_SIP_SMC_GET_USERCODE, NULL, 0,
76 &resp, 1);
77
78 if (ret == INTEL_SIP_SMC_STATUS_OK)
79 *usercode = (u32)resp;
80
81 return ret;
82}