blob: ebaa0b8fa170061803ea32ff8a7174bba1fea470 [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>
4 *
5 */
6
Chee Hong Angb5ddb912020-12-24 18:21:00 +08007#include <asm/ptrace.h>
8#include <asm/system.h>
Tom Rinic77f48c2024-04-30 07:35:34 -06009#include <linux/errno.h>
Chee Hong Angb5ddb912020-12-24 18:21:00 +080010#include <linux/intel-smc.h>
Tom Rinic77f48c2024-04-30 07:35:34 -060011#include <linux/string.h>
Chee Hong Angb5ddb912020-12-24 18:21:00 +080012
13int invoke_smc(u32 func_id, u64 *args, int arg_len, u64 *ret_arg, int ret_len)
14{
15 struct pt_regs regs;
16
17 memset(&regs, 0, sizeof(regs));
18 regs.regs[0] = func_id;
19
20 if (args)
21 memcpy(&regs.regs[1], args, arg_len * sizeof(*args));
22
23 smc_call(&regs);
24
25 if (ret_arg)
26 memcpy(ret_arg, &regs.regs[1], ret_len * sizeof(*ret_arg));
27
28 return regs.regs[0];
29}
30
31int smc_send_mailbox(u32 cmd, u32 len, u32 *arg, u8 urgent, u32 *resp_buf_len,
32 u32 *resp_buf)
33{
34 int ret;
35 u64 args[6];
36 u64 resp[3];
37
38 args[0] = cmd;
39 args[1] = (u64)arg;
40 args[2] = len;
41 args[3] = urgent;
42 args[4] = (u64)resp_buf;
43 if (resp_buf_len)
44 args[5] = *resp_buf_len;
45 else
46 args[5] = 0;
47
48 ret = invoke_smc(INTEL_SIP_SMC_MBOX_SEND_CMD, args, ARRAY_SIZE(args),
49 resp, ARRAY_SIZE(resp));
50
51 if (ret == INTEL_SIP_SMC_STATUS_OK && resp_buf && resp_buf_len) {
52 if (!resp[0])
53 *resp_buf_len = resp[1];
54 }
55
56 return (int)resp[0];
57}
Siew Chin Lim54744572021-03-25 14:07:45 +080058
59int smc_get_usercode(u32 *usercode)
60{
61 int ret;
62 u64 resp;
63
64 if (!usercode)
65 return -EINVAL;
66
67 ret = invoke_smc(INTEL_SIP_SMC_GET_USERCODE, NULL, 0,
68 &resp, 1);
69
70 if (ret == INTEL_SIP_SMC_STATUS_OK)
71 *usercode = (u32)resp;
72
73 return ret;
74}