107005b64SAneesh Kumar K.V (Arm) // SPDX-License-Identifier: GPL-2.0-only
207005b64SAneesh Kumar K.V (Arm) /*
307005b64SAneesh Kumar K.V (Arm) * Copyright (C) 2023 ARM Ltd.
407005b64SAneesh Kumar K.V (Arm) */
507005b64SAneesh Kumar K.V (Arm)
607005b64SAneesh Kumar K.V (Arm) #include <linux/arm-rsi-cmds.h>
7*0d5be76bSAneesh Kumar K.V (Arm) #include <linux/arm-smccc-bus.h>
807005b64SAneesh Kumar K.V (Arm) #include <linux/arm-smccc-rsi.h>
907005b64SAneesh Kumar K.V (Arm) #include <linux/cc_platform.h>
1007005b64SAneesh Kumar K.V (Arm) #include <linux/kernel.h>
1107005b64SAneesh Kumar K.V (Arm) #include <linux/device-id/platform.h>
1207005b64SAneesh Kumar K.V (Arm) #include <linux/module.h>
1307005b64SAneesh Kumar K.V (Arm) #include <linux/smp.h>
1407005b64SAneesh Kumar K.V (Arm) #include <linux/tsm.h>
1507005b64SAneesh Kumar K.V (Arm) #include <linux/types.h>
1607005b64SAneesh Kumar K.V (Arm)
1707005b64SAneesh Kumar K.V (Arm) /**
1807005b64SAneesh Kumar K.V (Arm) * struct arm_cca_token_info - a descriptor for the token buffer.
1907005b64SAneesh Kumar K.V (Arm) * @granule: PA of the granule to which the token will be written
2007005b64SAneesh Kumar K.V (Arm) * @offset: Offset within granule to start of buffer in bytes
2107005b64SAneesh Kumar K.V (Arm) */
2207005b64SAneesh Kumar K.V (Arm) struct arm_cca_token_info {
2307005b64SAneesh Kumar K.V (Arm) phys_addr_t granule;
2407005b64SAneesh Kumar K.V (Arm) unsigned long offset;
2507005b64SAneesh Kumar K.V (Arm) };
2607005b64SAneesh Kumar K.V (Arm)
2707005b64SAneesh Kumar K.V (Arm) /**
2807005b64SAneesh Kumar K.V (Arm) * arm_cca_attestation_continue - Retrieve the attestation token data.
2907005b64SAneesh Kumar K.V (Arm) *
3007005b64SAneesh Kumar K.V (Arm) * @info: pointer to the arm_cca_token_info
3107005b64SAneesh Kumar K.V (Arm) *
3207005b64SAneesh Kumar K.V (Arm) * Attestation token generation is a long running operation and therefore
3307005b64SAneesh Kumar K.V (Arm) * the token data may not be retrieved in a single call. Moreover, the
3407005b64SAneesh Kumar K.V (Arm) * token retrieval operation must be requested on the same CPU on which the
3507005b64SAneesh Kumar K.V (Arm) * attestation token generation was initialised.
3607005b64SAneesh Kumar K.V (Arm) * This helper function must therefore be executed on the same CPU multiple
3707005b64SAneesh Kumar K.V (Arm) * times until the entire token data is retrieved.
3807005b64SAneesh Kumar K.V (Arm) */
3907005b64SAneesh Kumar K.V (Arm) static unsigned long
arm_cca_attestation_continue(struct arm_cca_token_info * info)4007005b64SAneesh Kumar K.V (Arm) arm_cca_attestation_continue(struct arm_cca_token_info *info)
4107005b64SAneesh Kumar K.V (Arm) {
4207005b64SAneesh Kumar K.V (Arm) unsigned long ret;
4307005b64SAneesh Kumar K.V (Arm) unsigned long len;
4407005b64SAneesh Kumar K.V (Arm) unsigned long size;
4507005b64SAneesh Kumar K.V (Arm)
4607005b64SAneesh Kumar K.V (Arm) size = RSI_GRANULE_SIZE - info->offset;
4707005b64SAneesh Kumar K.V (Arm) ret = rsi_attestation_token_continue(info->granule, info->offset, size,
4807005b64SAneesh Kumar K.V (Arm) &len);
4907005b64SAneesh Kumar K.V (Arm) info->offset += len;
5007005b64SAneesh Kumar K.V (Arm) return ret;
5107005b64SAneesh Kumar K.V (Arm) }
5207005b64SAneesh Kumar K.V (Arm)
5307005b64SAneesh Kumar K.V (Arm) /**
5407005b64SAneesh Kumar K.V (Arm) * arm_cca_report_new - Generate a new attestation token.
5507005b64SAneesh Kumar K.V (Arm) *
5607005b64SAneesh Kumar K.V (Arm) * @report: pointer to the TSM report context information.
5707005b64SAneesh Kumar K.V (Arm) * @data: pointer to the context specific data for this module.
5807005b64SAneesh Kumar K.V (Arm) *
5907005b64SAneesh Kumar K.V (Arm) * Initialise the attestation token generation using the challenge data
6007005b64SAneesh Kumar K.V (Arm) * passed in the TSM descriptor. Allocate memory for the attestation token
6107005b64SAneesh Kumar K.V (Arm) * and retrieve the attestation token on the same CPU on which the
6207005b64SAneesh Kumar K.V (Arm) * attestation token generation was initialised.
6307005b64SAneesh Kumar K.V (Arm) *
6407005b64SAneesh Kumar K.V (Arm) * The challenge data must be at least 32 bytes and no more than 64 bytes. If
6507005b64SAneesh Kumar K.V (Arm) * less than 64 bytes are provided it will be zero padded to 64 bytes.
6607005b64SAneesh Kumar K.V (Arm) *
6707005b64SAneesh Kumar K.V (Arm) * Return:
6807005b64SAneesh Kumar K.V (Arm) * * %0 - Attestation token generated successfully.
6907005b64SAneesh Kumar K.V (Arm) * * %-EINVAL - A parameter was not valid.
7007005b64SAneesh Kumar K.V (Arm) * * %-ENOMEM - Out of memory.
7107005b64SAneesh Kumar K.V (Arm) * * %-EFAULT - Failed to get IPA for memory page(s).
7207005b64SAneesh Kumar K.V (Arm) */
arm_cca_report_new(struct tsm_report * report,void * data)7307005b64SAneesh Kumar K.V (Arm) static int arm_cca_report_new(struct tsm_report *report, void *data)
7407005b64SAneesh Kumar K.V (Arm) {
7507005b64SAneesh Kumar K.V (Arm) int ret = 0;
7607005b64SAneesh Kumar K.V (Arm) unsigned long rsi_result;
7707005b64SAneesh Kumar K.V (Arm) long max_size;
7807005b64SAneesh Kumar K.V (Arm) unsigned long token_size = 0;
7907005b64SAneesh Kumar K.V (Arm) struct arm_cca_token_info info;
8007005b64SAneesh Kumar K.V (Arm) void *buf;
8107005b64SAneesh Kumar K.V (Arm) u8 *token __free(kvfree) = NULL;
8207005b64SAneesh Kumar K.V (Arm) struct tsm_report_desc *desc = &report->desc;
8307005b64SAneesh Kumar K.V (Arm)
8407005b64SAneesh Kumar K.V (Arm) if (desc->inblob_len < 32 || desc->inblob_len > 64)
8507005b64SAneesh Kumar K.V (Arm) return -EINVAL;
8607005b64SAneesh Kumar K.V (Arm)
8707005b64SAneesh Kumar K.V (Arm) /*
8807005b64SAneesh Kumar K.V (Arm) * The attestation token 'init' and 'continue' calls must be
8907005b64SAneesh Kumar K.V (Arm) * performed on the same CPU, so disable CPU migration around
9007005b64SAneesh Kumar K.V (Arm) * those operations.
9107005b64SAneesh Kumar K.V (Arm) */
9207005b64SAneesh Kumar K.V (Arm) migrate_disable();
9307005b64SAneesh Kumar K.V (Arm)
9407005b64SAneesh Kumar K.V (Arm) max_size = rsi_attestation_token_init(desc->inblob, desc->inblob_len);
9507005b64SAneesh Kumar K.V (Arm) if (max_size <= 0) {
9607005b64SAneesh Kumar K.V (Arm) ret = -EINVAL;
9707005b64SAneesh Kumar K.V (Arm) goto exit_migrate_enable;
9807005b64SAneesh Kumar K.V (Arm) }
9907005b64SAneesh Kumar K.V (Arm)
10007005b64SAneesh Kumar K.V (Arm) /* Allocate outblob */
10107005b64SAneesh Kumar K.V (Arm) token = kvzalloc(max_size, GFP_KERNEL);
10207005b64SAneesh Kumar K.V (Arm) if (!token) {
10307005b64SAneesh Kumar K.V (Arm) ret = -ENOMEM;
10407005b64SAneesh Kumar K.V (Arm) goto exit_migrate_enable;
10507005b64SAneesh Kumar K.V (Arm) }
10607005b64SAneesh Kumar K.V (Arm)
10707005b64SAneesh Kumar K.V (Arm) /*
10807005b64SAneesh Kumar K.V (Arm) * Since the outblob may not be physically contiguous, use a page
10907005b64SAneesh Kumar K.V (Arm) * to bounce the buffer from RMM.
11007005b64SAneesh Kumar K.V (Arm) */
11107005b64SAneesh Kumar K.V (Arm) buf = alloc_pages_exact(RSI_GRANULE_SIZE, GFP_KERNEL);
11207005b64SAneesh Kumar K.V (Arm) if (!buf) {
11307005b64SAneesh Kumar K.V (Arm) ret = -ENOMEM;
11407005b64SAneesh Kumar K.V (Arm) goto exit_migrate_enable;
11507005b64SAneesh Kumar K.V (Arm) }
11607005b64SAneesh Kumar K.V (Arm)
11707005b64SAneesh Kumar K.V (Arm) /* Get the PA of the memory page(s) that were allocated */
11807005b64SAneesh Kumar K.V (Arm) info.granule = (unsigned long)virt_to_phys(buf);
11907005b64SAneesh Kumar K.V (Arm)
12007005b64SAneesh Kumar K.V (Arm) /* Loop until the token is ready or there is an error */
12107005b64SAneesh Kumar K.V (Arm) do {
12207005b64SAneesh Kumar K.V (Arm) /* Retrieve one RSI_GRANULE_SIZE data per loop iteration */
12307005b64SAneesh Kumar K.V (Arm) info.offset = 0;
12407005b64SAneesh Kumar K.V (Arm) do {
12507005b64SAneesh Kumar K.V (Arm) /*
12607005b64SAneesh Kumar K.V (Arm) * Retrieve a sub-granule chunk of data per loop
12707005b64SAneesh Kumar K.V (Arm) * iteration.
12807005b64SAneesh Kumar K.V (Arm) */
12907005b64SAneesh Kumar K.V (Arm) rsi_result = arm_cca_attestation_continue(&info);
13007005b64SAneesh Kumar K.V (Arm) } while (rsi_result == RSI_INCOMPLETE &&
13107005b64SAneesh Kumar K.V (Arm) info.offset < RSI_GRANULE_SIZE);
13207005b64SAneesh Kumar K.V (Arm)
13307005b64SAneesh Kumar K.V (Arm) /* Break out in case of failure */
13407005b64SAneesh Kumar K.V (Arm) if (rsi_result != RSI_SUCCESS && rsi_result != RSI_INCOMPLETE) {
13507005b64SAneesh Kumar K.V (Arm) ret = -ENXIO;
13607005b64SAneesh Kumar K.V (Arm) token_size = 0;
13707005b64SAneesh Kumar K.V (Arm) goto exit_free_granule_page;
13807005b64SAneesh Kumar K.V (Arm) }
13907005b64SAneesh Kumar K.V (Arm)
14007005b64SAneesh Kumar K.V (Arm) /*
14107005b64SAneesh Kumar K.V (Arm) * Copy the retrieved token data from the granule
14207005b64SAneesh Kumar K.V (Arm) * to the token buffer, ensuring that the RMM doesn't
14307005b64SAneesh Kumar K.V (Arm) * overflow the buffer.
14407005b64SAneesh Kumar K.V (Arm) */
14507005b64SAneesh Kumar K.V (Arm) if (WARN_ON(token_size + info.offset > max_size))
14607005b64SAneesh Kumar K.V (Arm) break;
14707005b64SAneesh Kumar K.V (Arm) memcpy(&token[token_size], buf, info.offset);
14807005b64SAneesh Kumar K.V (Arm) token_size += info.offset;
14907005b64SAneesh Kumar K.V (Arm) } while (rsi_result == RSI_INCOMPLETE);
15007005b64SAneesh Kumar K.V (Arm)
15107005b64SAneesh Kumar K.V (Arm) report->outblob = no_free_ptr(token);
15207005b64SAneesh Kumar K.V (Arm) exit_free_granule_page:
15307005b64SAneesh Kumar K.V (Arm) report->outblob_len = token_size;
15407005b64SAneesh Kumar K.V (Arm) free_pages_exact(buf, RSI_GRANULE_SIZE);
15507005b64SAneesh Kumar K.V (Arm) exit_migrate_enable:
15607005b64SAneesh Kumar K.V (Arm) migrate_enable();
15707005b64SAneesh Kumar K.V (Arm) return ret;
15807005b64SAneesh Kumar K.V (Arm) }
15907005b64SAneesh Kumar K.V (Arm)
16007005b64SAneesh Kumar K.V (Arm) static const struct tsm_report_ops arm_cca_tsm_report_ops = {
16107005b64SAneesh Kumar K.V (Arm) .name = KBUILD_MODNAME,
16207005b64SAneesh Kumar K.V (Arm) .report_new = arm_cca_report_new,
16307005b64SAneesh Kumar K.V (Arm) };
16407005b64SAneesh Kumar K.V (Arm)
cca_tsm_probe(struct arm_smccc_device * sdev)165*0d5be76bSAneesh Kumar K.V (Arm) static int cca_tsm_probe(struct arm_smccc_device *sdev)
16607005b64SAneesh Kumar K.V (Arm) {
16707005b64SAneesh Kumar K.V (Arm) int ret;
16807005b64SAneesh Kumar K.V (Arm)
16907005b64SAneesh Kumar K.V (Arm) if (!is_realm_world())
17007005b64SAneesh Kumar K.V (Arm) return -ENODEV;
17107005b64SAneesh Kumar K.V (Arm)
17207005b64SAneesh Kumar K.V (Arm) ret = tsm_report_register(&arm_cca_tsm_report_ops, NULL);
17307005b64SAneesh Kumar K.V (Arm) if (ret < 0)
174*0d5be76bSAneesh Kumar K.V (Arm) return dev_err_probe(&sdev->dev,
175*0d5be76bSAneesh Kumar K.V (Arm) ret, "Error registering with TSM\n");
17607005b64SAneesh Kumar K.V (Arm)
177*0d5be76bSAneesh Kumar K.V (Arm) return 0;
17807005b64SAneesh Kumar K.V (Arm) }
17907005b64SAneesh Kumar K.V (Arm)
cca_tsm_remove(struct arm_smccc_device * sdev)180*0d5be76bSAneesh Kumar K.V (Arm) static void cca_tsm_remove(struct arm_smccc_device *sdev)
18107005b64SAneesh Kumar K.V (Arm) {
18207005b64SAneesh Kumar K.V (Arm) tsm_report_unregister(&arm_cca_tsm_report_ops);
18307005b64SAneesh Kumar K.V (Arm) }
18407005b64SAneesh Kumar K.V (Arm)
185*0d5be76bSAneesh Kumar K.V (Arm) static const struct arm_smccc_device_id cca_tsm_id_table[] = {
186*0d5be76bSAneesh Kumar K.V (Arm) { .func_id = SMC_RSI_ABI_VERSION },
18707005b64SAneesh Kumar K.V (Arm) {}
18807005b64SAneesh Kumar K.V (Arm) };
189*0d5be76bSAneesh Kumar K.V (Arm) MODULE_DEVICE_TABLE(arm_smccc, cca_tsm_id_table);
19007005b64SAneesh Kumar K.V (Arm)
191*0d5be76bSAneesh Kumar K.V (Arm) static struct arm_smccc_driver cca_tsm_driver = {
192*0d5be76bSAneesh Kumar K.V (Arm) .name = KBUILD_MODNAME,
193*0d5be76bSAneesh Kumar K.V (Arm) .probe = cca_tsm_probe,
194*0d5be76bSAneesh Kumar K.V (Arm) .remove = cca_tsm_remove,
195*0d5be76bSAneesh Kumar K.V (Arm) .id_table = cca_tsm_id_table,
196*0d5be76bSAneesh Kumar K.V (Arm) };
197*0d5be76bSAneesh Kumar K.V (Arm) module_arm_smccc_driver(cca_tsm_driver);
19807005b64SAneesh Kumar K.V (Arm) MODULE_AUTHOR("Sami Mujawar <sami.mujawar@arm.com>");
19907005b64SAneesh Kumar K.V (Arm) MODULE_DESCRIPTION("Arm CCA Guest TSM Driver");
20007005b64SAneesh Kumar K.V (Arm) MODULE_LICENSE("GPL");
201