mem_encrypt.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Memory Encryption Support Common Code
  4. *
  5. * Copyright (C) 2016 Advanced Micro Devices, Inc.
  6. *
  7. * Author: Tom Lendacky <[email protected]>
  8. */
  9. #include <linux/dma-direct.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/swiotlb.h>
  12. #include <linux/cc_platform.h>
  13. #include <linux/mem_encrypt.h>
  14. /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */
  15. bool force_dma_unencrypted(struct device *dev)
  16. {
  17. /*
  18. * For SEV, all DMA must be to unencrypted addresses.
  19. */
  20. if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
  21. return true;
  22. /*
  23. * For SME, all DMA must be to unencrypted addresses if the
  24. * device does not support DMA to addresses that include the
  25. * encryption mask.
  26. */
  27. if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
  28. u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
  29. u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
  30. dev->bus_dma_limit);
  31. if (dma_dev_mask <= dma_enc_mask)
  32. return true;
  33. }
  34. return false;
  35. }
  36. static void print_mem_encrypt_feature_info(void)
  37. {
  38. pr_info("Memory Encryption Features active:");
  39. if (cpu_feature_enabled(X86_FEATURE_TDX_GUEST)) {
  40. pr_cont(" Intel TDX\n");
  41. return;
  42. }
  43. pr_cont(" AMD");
  44. /* Secure Memory Encryption */
  45. if (cc_platform_has(CC_ATTR_HOST_MEM_ENCRYPT)) {
  46. /*
  47. * SME is mutually exclusive with any of the SEV
  48. * features below.
  49. */
  50. pr_cont(" SME\n");
  51. return;
  52. }
  53. /* Secure Encrypted Virtualization */
  54. if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
  55. pr_cont(" SEV");
  56. /* Encrypted Register State */
  57. if (cc_platform_has(CC_ATTR_GUEST_STATE_ENCRYPT))
  58. pr_cont(" SEV-ES");
  59. /* Secure Nested Paging */
  60. if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP))
  61. pr_cont(" SEV-SNP");
  62. pr_cont("\n");
  63. }
  64. /* Architecture __weak replacement functions */
  65. void __init mem_encrypt_init(void)
  66. {
  67. if (!cc_platform_has(CC_ATTR_MEM_ENCRYPT))
  68. return;
  69. /* Call into SWIOTLB to update the SWIOTLB DMA buffers */
  70. swiotlb_update_mem_attributes();
  71. print_mem_encrypt_feature_info();
  72. }