rsi_cmds.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2023 ARM Ltd.
  4. */
  5. #ifndef __ASM_RSI_CMDS_H
  6. #define __ASM_RSI_CMDS_H
  7. #include <linux/arm-smccc.h>
  8. #include <linux/string.h>
  9. #include <asm/memory.h>
  10. #include <asm/rsi_smc.h>
  11. #define RSI_GRANULE_SHIFT 12
  12. #define RSI_GRANULE_SIZE (_AC(1, UL) << RSI_GRANULE_SHIFT)
  13. enum ripas {
  14. RSI_RIPAS_EMPTY = 0,
  15. RSI_RIPAS_RAM = 1,
  16. RSI_RIPAS_DESTROYED = 2,
  17. RSI_RIPAS_DEV = 3,
  18. };
  19. static inline unsigned long rsi_request_version(unsigned long req,
  20. unsigned long *out_lower,
  21. unsigned long *out_higher)
  22. {
  23. struct arm_smccc_res res;
  24. arm_smccc_smc(SMC_RSI_ABI_VERSION, req, 0, 0, 0, 0, 0, 0, &res);
  25. if (out_lower)
  26. *out_lower = res.a1;
  27. if (out_higher)
  28. *out_higher = res.a2;
  29. return res.a0;
  30. }
  31. static inline unsigned long rsi_get_realm_config(struct realm_config *cfg)
  32. {
  33. struct arm_smccc_res res;
  34. arm_smccc_smc(SMC_RSI_REALM_CONFIG, virt_to_phys(cfg),
  35. 0, 0, 0, 0, 0, 0, &res);
  36. return res.a0;
  37. }
  38. static inline unsigned long rsi_ipa_state_get(phys_addr_t start,
  39. phys_addr_t end,
  40. enum ripas *state,
  41. phys_addr_t *top)
  42. {
  43. struct arm_smccc_res res;
  44. arm_smccc_smc(SMC_RSI_IPA_STATE_GET,
  45. start, end, 0, 0, 0, 0, 0,
  46. &res);
  47. if (res.a0 == RSI_SUCCESS) {
  48. if (top)
  49. *top = res.a1;
  50. if (state)
  51. *state = res.a2;
  52. }
  53. return res.a0;
  54. }
  55. static inline long rsi_set_addr_range_state(phys_addr_t start,
  56. phys_addr_t end,
  57. enum ripas state,
  58. unsigned long flags,
  59. phys_addr_t *top)
  60. {
  61. struct arm_smccc_res res;
  62. arm_smccc_smc(SMC_RSI_IPA_STATE_SET, start, end, state,
  63. flags, 0, 0, 0, &res);
  64. if (top)
  65. *top = res.a1;
  66. if (res.a2 != RSI_ACCEPT)
  67. return -EPERM;
  68. return res.a0;
  69. }
  70. /**
  71. * rsi_attestation_token_init - Initialise the operation to retrieve an
  72. * attestation token.
  73. *
  74. * @challenge: The challenge data to be used in the attestation token
  75. * generation.
  76. * @size: Size of the challenge data in bytes.
  77. *
  78. * Initialises the attestation token generation and returns an upper bound
  79. * on the attestation token size that can be used to allocate an adequate
  80. * buffer. The caller is expected to subsequently call
  81. * rsi_attestation_token_continue() to retrieve the attestation token data on
  82. * the same CPU.
  83. *
  84. * Returns:
  85. * On success, returns the upper limit of the attestation report size.
  86. * Otherwise, -EINVAL
  87. */
  88. static inline long
  89. rsi_attestation_token_init(const u8 *challenge, unsigned long size)
  90. {
  91. struct arm_smccc_1_2_regs regs = { 0 };
  92. /* The challenge must be at least 32bytes and at most 64bytes */
  93. if (!challenge || size < 32 || size > 64)
  94. return -EINVAL;
  95. regs.a0 = SMC_RSI_ATTESTATION_TOKEN_INIT;
  96. memcpy(&regs.a1, challenge, size);
  97. arm_smccc_1_2_smc(&regs, &regs);
  98. if (regs.a0 == RSI_SUCCESS)
  99. return regs.a1;
  100. return -EINVAL;
  101. }
  102. /**
  103. * rsi_attestation_token_continue - Continue the operation to retrieve an
  104. * attestation token.
  105. *
  106. * @granule: {I}PA of the Granule to which the token will be written.
  107. * @offset: Offset within Granule to start of buffer in bytes.
  108. * @size: The size of the buffer.
  109. * @len: The number of bytes written to the buffer.
  110. *
  111. * Retrieves up to a RSI_GRANULE_SIZE worth of token data per call. The caller
  112. * is expected to call rsi_attestation_token_init() before calling this
  113. * function to retrieve the attestation token.
  114. *
  115. * Return:
  116. * * %RSI_SUCCESS - Attestation token retrieved successfully.
  117. * * %RSI_INCOMPLETE - Token generation is not complete.
  118. * * %RSI_ERROR_INPUT - A parameter was not valid.
  119. * * %RSI_ERROR_STATE - Attestation not in progress.
  120. */
  121. static inline unsigned long rsi_attestation_token_continue(phys_addr_t granule,
  122. unsigned long offset,
  123. unsigned long size,
  124. unsigned long *len)
  125. {
  126. struct arm_smccc_res res;
  127. arm_smccc_1_1_invoke(SMC_RSI_ATTESTATION_TOKEN_CONTINUE,
  128. granule, offset, size, 0, &res);
  129. if (len)
  130. *len = res.a1;
  131. return res.a0;
  132. }
  133. #endif /* __ASM_RSI_CMDS_H */