Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020 Intel Corporation <www.intel.com> |
| 4 | * |
| 5 | */ |
| 6 | |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 7 | #include <asm/ptrace.h> |
| 8 | #include <asm/system.h> |
Tom Rini | c77f48c | 2024-04-30 07:35:34 -0600 | [diff] [blame^] | 9 | #include <linux/errno.h> |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 10 | #include <linux/intel-smc.h> |
Tom Rini | c77f48c | 2024-04-30 07:35:34 -0600 | [diff] [blame^] | 11 | #include <linux/string.h> |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 12 | |
| 13 | int 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(®s, 0, sizeof(regs)); |
| 18 | regs.regs[0] = func_id; |
| 19 | |
| 20 | if (args) |
| 21 | memcpy(®s.regs[1], args, arg_len * sizeof(*args)); |
| 22 | |
| 23 | smc_call(®s); |
| 24 | |
| 25 | if (ret_arg) |
| 26 | memcpy(ret_arg, ®s.regs[1], ret_len * sizeof(*ret_arg)); |
| 27 | |
| 28 | return regs.regs[0]; |
| 29 | } |
| 30 | |
| 31 | int 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 Lim | 5474457 | 2021-03-25 14:07:45 +0800 | [diff] [blame] | 58 | |
| 59 | int 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 | } |