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> |
Alif Zakuan Yuslaimi | 6aa360d | 2025-02-18 16:35:03 +0800 | [diff] [blame^] | 4 | * Copyright (C) 2025 Altera Corporation <www.altera.com> |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 5 | * |
| 6 | */ |
| 7 | |
Alif Zakuan Yuslaimi | 6aa360d | 2025-02-18 16:35:03 +0800 | [diff] [blame^] | 8 | #include <cpu_func.h> |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 9 | #include <asm/ptrace.h> |
| 10 | #include <asm/system.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 11 | #include <linux/errno.h> |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 12 | #include <linux/intel-smc.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 13 | #include <linux/string.h> |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 14 | |
| 15 | int 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(®s, 0, sizeof(regs)); |
| 20 | regs.regs[0] = func_id; |
| 21 | |
| 22 | if (args) |
| 23 | memcpy(®s.regs[1], args, arg_len * sizeof(*args)); |
| 24 | |
| 25 | smc_call(®s); |
| 26 | |
| 27 | if (ret_arg) |
| 28 | memcpy(ret_arg, ®s.regs[1], ret_len * sizeof(*ret_arg)); |
| 29 | |
| 30 | return regs.regs[0]; |
| 31 | } |
| 32 | |
| 33 | int 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 Yuslaimi | 6aa360d | 2025-02-18 16:35:03 +0800 | [diff] [blame^] | 45 | |
| 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 Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 50 | args[5] = *resp_buf_len; |
Alif Zakuan Yuslaimi | 6aa360d | 2025-02-18 16:35:03 +0800 | [diff] [blame^] | 51 | flush_dcache_range((uintptr_t)resp_buf, (uintptr_t)resp_buf + *resp_buf_len); |
| 52 | } else { |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 53 | args[5] = 0; |
Alif Zakuan Yuslaimi | 6aa360d | 2025-02-18 16:35:03 +0800 | [diff] [blame^] | 54 | } |
Chee Hong Ang | b5ddb91 | 2020-12-24 18:21:00 +0800 | [diff] [blame] | 55 | |
| 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 Lim | 5474457 | 2021-03-25 14:07:45 +0800 | [diff] [blame] | 66 | |
| 67 | int 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 | } |