#include <linux/arm-smccc.h>
#include <linux/cc_platform.h>
#include <linux/kernel.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/smp.h>
#include <linux/tsm.h>
#include <linux/types.h>
#include <asm/rsi.h>
struct arm_cca_token_info {
void *challenge;
unsigned long challenge_size;
phys_addr_t granule;
unsigned long offset;
unsigned long result;
};
static void arm_cca_attestation_init(void *param)
{
struct arm_cca_token_info *info;
info = (struct arm_cca_token_info *)param;
info->result = rsi_attestation_token_init(info->challenge,
info->challenge_size);
}
static void arm_cca_attestation_continue(void *param)
{
unsigned long len;
unsigned long size;
struct arm_cca_token_info *info;
info = (struct arm_cca_token_info *)param;
size = RSI_GRANULE_SIZE - info->offset;
info->result = rsi_attestation_token_continue(info->granule,
info->offset, size, &len);
info->offset += len;
}
static int arm_cca_report_new(struct tsm_report *report, void *data)
{
int ret;
int cpu;
long max_size;
unsigned long token_size = 0;
struct arm_cca_token_info info;
void *buf;
u8 *token __free(kvfree) = NULL;
struct tsm_report_desc *desc = &report->desc;
if (desc->inblob_len < 32 || desc->inblob_len > 64)
return -EINVAL;
cpu = smp_processor_id();
info.challenge = desc->inblob;
info.challenge_size = desc->inblob_len;
ret = smp_call_function_single(cpu, arm_cca_attestation_init,
&info, true);
if (ret)
return ret;
max_size = info.result;
if (max_size <= 0)
return -EINVAL;
token = kvzalloc(max_size, GFP_KERNEL);
if (!token)
return -ENOMEM;
buf = alloc_pages_exact(RSI_GRANULE_SIZE, GFP_KERNEL);
if (!buf)
return -ENOMEM;
info.granule = (unsigned long)virt_to_phys(buf);
do {
info.offset = 0;
do {
ret = smp_call_function_single(cpu,
arm_cca_attestation_continue,
(void *)&info, true);
if (ret != 0) {
token_size = 0;
goto exit_free_granule_page;
}
} while (info.result == RSI_INCOMPLETE &&
info.offset < RSI_GRANULE_SIZE);
if (info.result != RSI_SUCCESS) {
ret = -ENXIO;
token_size = 0;
goto exit_free_granule_page;
}
if (WARN_ON(token_size + info.offset > max_size))
break;
memcpy(&token[token_size], buf, info.offset);
token_size += info.offset;
} while (info.result == RSI_INCOMPLETE);
report->outblob = no_free_ptr(token);
exit_free_granule_page:
report->outblob_len = token_size;
free_pages_exact(buf, RSI_GRANULE_SIZE);
return ret;
}
static const struct tsm_report_ops arm_cca_tsm_ops = {
.name = KBUILD_MODNAME,
.report_new = arm_cca_report_new,
};
static int __init arm_cca_guest_init(void)
{
int ret;
if (!is_realm_world())
return -ENODEV;
ret = tsm_report_register(&arm_cca_tsm_ops, NULL);
if (ret < 0)
pr_err("Error %d registering with TSM\n", ret);
return ret;
}
module_init(arm_cca_guest_init);
static void __exit arm_cca_guest_exit(void)
{
tsm_report_unregister(&arm_cca_tsm_ops);
}
module_exit(arm_cca_guest_exit);
static const struct platform_device_id arm_cca_match[] __maybe_unused = {
{ RSI_PDEV_NAME, 0},
{ }
};
MODULE_DEVICE_TABLE(platform, arm_cca_match);
MODULE_AUTHOR("Sami Mujawar <sami.mujawar@arm.com>");
MODULE_DESCRIPTION("Arm CCA Guest TSM Driver");
MODULE_LICENSE("GPL");