cc_platform.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Confidential Computing Platform Capability checks
  4. *
  5. * Copyright (C) 2021 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <thomas.lendacky@amd.com>
  8. */
  9. #include <linux/export.h>
  10. #include <linux/cc_platform.h>
  11. #include <linux/mem_encrypt.h>
  12. #include <asm/processor.h>
  13. static bool __maybe_unused intel_cc_platform_has(enum cc_attr attr)
  14. {
  15. #ifdef CONFIG_INTEL_TDX_GUEST
  16. return false;
  17. #else
  18. return false;
  19. #endif
  20. }
  21. /*
  22. * SME and SEV are very similar but they are not the same, so there are
  23. * times that the kernel will need to distinguish between SME and SEV. The
  24. * cc_platform_has() function is used for this. When a distinction isn't
  25. * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
  26. *
  27. * The trampoline code is a good example for this requirement. Before
  28. * paging is activated, SME will access all memory as decrypted, but SEV
  29. * will access all memory as encrypted. So, when APs are being brought
  30. * up under SME the trampoline area cannot be encrypted, whereas under SEV
  31. * the trampoline area must be encrypted.
  32. */
  33. static bool amd_cc_platform_has(enum cc_attr attr)
  34. {
  35. #ifdef CONFIG_AMD_MEM_ENCRYPT
  36. switch (attr) {
  37. case CC_ATTR_MEM_ENCRYPT:
  38. return sme_me_mask;
  39. case CC_ATTR_HOST_MEM_ENCRYPT:
  40. return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
  41. case CC_ATTR_GUEST_MEM_ENCRYPT:
  42. return sev_status & MSR_AMD64_SEV_ENABLED;
  43. case CC_ATTR_GUEST_STATE_ENCRYPT:
  44. return sev_status & MSR_AMD64_SEV_ES_ENABLED;
  45. default:
  46. return false;
  47. }
  48. #else
  49. return false;
  50. #endif
  51. }
  52. bool cc_platform_has(enum cc_attr attr)
  53. {
  54. if (sme_me_mask)
  55. return amd_cc_platform_has(attr);
  56. return false;
  57. }
  58. EXPORT_SYMBOL_GPL(cc_platform_has);