1745e3ed8SKirill A. Shutemov // SPDX-License-Identifier: GPL-2.0-only
2745e3ed8SKirill A. Shutemov
3745e3ed8SKirill A. Shutemov #include "error.h"
43fd1239aSKirill A. Shutemov #include "misc.h"
575d090fdSKirill A. Shutemov #include "tdx.h"
66c321179STom Lendacky #include "sev.h"
775d090fdSKirill A. Shutemov #include <asm/shared/tdx.h>
875d090fdSKirill A. Shutemov
975d090fdSKirill A. Shutemov /*
1075d090fdSKirill A. Shutemov * accept_memory() and process_unaccepted_memory() called from EFI stub which
11*54aa699eSBjorn Helgaas * runs before decompressor and its early_tdx_detect().
1275d090fdSKirill A. Shutemov *
1375d090fdSKirill A. Shutemov * Enumerate TDX directly from the early users.
1475d090fdSKirill A. Shutemov */
early_is_tdx_guest(void)1575d090fdSKirill A. Shutemov static bool early_is_tdx_guest(void)
1675d090fdSKirill A. Shutemov {
1775d090fdSKirill A. Shutemov static bool once;
1875d090fdSKirill A. Shutemov static bool is_tdx;
1975d090fdSKirill A. Shutemov
2075d090fdSKirill A. Shutemov if (!IS_ENABLED(CONFIG_INTEL_TDX_GUEST))
2175d090fdSKirill A. Shutemov return false;
2275d090fdSKirill A. Shutemov
2375d090fdSKirill A. Shutemov if (!once) {
2475d090fdSKirill A. Shutemov u32 eax, sig[3];
2575d090fdSKirill A. Shutemov
2675d090fdSKirill A. Shutemov cpuid_count(TDX_CPUID_LEAF_ID, 0, &eax,
2775d090fdSKirill A. Shutemov &sig[0], &sig[2], &sig[1]);
2875d090fdSKirill A. Shutemov is_tdx = !memcmp(TDX_IDENT, sig, sizeof(sig));
2975d090fdSKirill A. Shutemov once = true;
3075d090fdSKirill A. Shutemov }
3175d090fdSKirill A. Shutemov
3275d090fdSKirill A. Shutemov return is_tdx;
3375d090fdSKirill A. Shutemov }
34745e3ed8SKirill A. Shutemov
arch_accept_memory(phys_addr_t start,phys_addr_t end)35745e3ed8SKirill A. Shutemov void arch_accept_memory(phys_addr_t start, phys_addr_t end)
36745e3ed8SKirill A. Shutemov {
37745e3ed8SKirill A. Shutemov /* Platform-specific memory-acceptance call goes here */
3875d090fdSKirill A. Shutemov if (early_is_tdx_guest()) {
3975d090fdSKirill A. Shutemov if (!tdx_accept_memory(start, end))
4075d090fdSKirill A. Shutemov panic("TDX: Failed to accept memory\n");
416c321179STom Lendacky } else if (sev_snp_enabled()) {
426c321179STom Lendacky snp_accept_memory(start, end);
4375d090fdSKirill A. Shutemov } else {
4475d090fdSKirill A. Shutemov error("Cannot accept memory: unknown platform\n");
4575d090fdSKirill A. Shutemov }
46745e3ed8SKirill A. Shutemov }
473fd1239aSKirill A. Shutemov
init_unaccepted_memory(void)483fd1239aSKirill A. Shutemov bool init_unaccepted_memory(void)
493fd1239aSKirill A. Shutemov {
503fd1239aSKirill A. Shutemov guid_t guid = LINUX_EFI_UNACCEPTED_MEM_TABLE_GUID;
513fd1239aSKirill A. Shutemov struct efi_unaccepted_memory *table;
523fd1239aSKirill A. Shutemov unsigned long cfg_table_pa;
533fd1239aSKirill A. Shutemov unsigned int cfg_table_len;
543fd1239aSKirill A. Shutemov enum efi_type et;
553fd1239aSKirill A. Shutemov int ret;
563fd1239aSKirill A. Shutemov
57d55d5bc5SArd Biesheuvel et = efi_get_type(boot_params_ptr);
583fd1239aSKirill A. Shutemov if (et == EFI_TYPE_NONE)
593fd1239aSKirill A. Shutemov return false;
603fd1239aSKirill A. Shutemov
61d55d5bc5SArd Biesheuvel ret = efi_get_conf_table(boot_params_ptr, &cfg_table_pa, &cfg_table_len);
623fd1239aSKirill A. Shutemov if (ret) {
633fd1239aSKirill A. Shutemov warn("EFI config table not found.");
643fd1239aSKirill A. Shutemov return false;
653fd1239aSKirill A. Shutemov }
663fd1239aSKirill A. Shutemov
67d55d5bc5SArd Biesheuvel table = (void *)efi_find_vendor_table(boot_params_ptr, cfg_table_pa,
683fd1239aSKirill A. Shutemov cfg_table_len, guid);
693fd1239aSKirill A. Shutemov if (!table)
703fd1239aSKirill A. Shutemov return false;
713fd1239aSKirill A. Shutemov
723fd1239aSKirill A. Shutemov if (table->version != 1)
733fd1239aSKirill A. Shutemov error("Unknown version of unaccepted memory table\n");
743fd1239aSKirill A. Shutemov
753fd1239aSKirill A. Shutemov /*
763fd1239aSKirill A. Shutemov * In many cases unaccepted_table is already set by EFI stub, but it
773fd1239aSKirill A. Shutemov * has to be initialized again to cover cases when the table is not
783fd1239aSKirill A. Shutemov * allocated by EFI stub or EFI stub copied the kernel image with
793fd1239aSKirill A. Shutemov * efi_relocate_kernel() before the variable is set.
803fd1239aSKirill A. Shutemov *
813fd1239aSKirill A. Shutemov * It must be initialized before the first usage of accept_memory().
823fd1239aSKirill A. Shutemov */
833fd1239aSKirill A. Shutemov unaccepted_table = table;
843fd1239aSKirill A. Shutemov
853fd1239aSKirill A. Shutemov return true;
863fd1239aSKirill A. Shutemov }
87