Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * tboot.c: main implementation of helper functions used by kernel for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *          runtime support of Intel(R) Trusted Execution Technology
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (c) 2006-2009, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/intel-iommu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init_task.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/dmar.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/pfn.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/tboot.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <asm/realmode.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <asm/bootparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <asm/pgalloc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <asm/swiotlb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <asm/fixmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <asm/proto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <asm/e820/api.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include "../realmode/rm/wakeup.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) /* Global pointer to shared data; NULL means no measured launch. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static struct tboot *tboot __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) /* timeout for APs (in secs) to enter wait-for-SIPI state during shutdown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define AP_WAIT_TIMEOUT		1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) #undef pr_fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define pr_fmt(fmt)	"tboot: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) static u8 tboot_uuid[16] __initdata = TBOOT_UUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) bool tboot_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	return tboot != NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) void __init tboot_probe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	/* Look for valid page-aligned address for shared page. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	if (!boot_params.tboot_addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * also verify that it is mapped as we expect it before calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 * set_fixmap(), to reduce chance of garbage value causing crash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!e820__mapped_any(boot_params.tboot_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			     boot_params.tboot_addr, E820_TYPE_RESERVED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		pr_warn("non-0 tboot_addr but it is not of type E820_TYPE_RESERVED\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	/* Map and check for tboot UUID. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	set_fixmap(FIX_TBOOT_BASE, boot_params.tboot_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	tboot = (struct tboot *)fix_to_virt(FIX_TBOOT_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	if (memcmp(&tboot_uuid, &tboot->uuid, sizeof(tboot->uuid))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		pr_warn("tboot at 0x%llx is invalid\n", boot_params.tboot_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		tboot = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (tboot->version < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		pr_warn("tboot version is invalid: %u\n", tboot->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		tboot = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	pr_info("found shared page at phys addr 0x%llx:\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 		boot_params.tboot_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	pr_debug("version: %d\n", tboot->version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	pr_debug("log_addr: 0x%08x\n", tboot->log_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	pr_debug("shutdown_entry: 0x%x\n", tboot->shutdown_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	pr_debug("tboot_base: 0x%08x\n", tboot->tboot_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	pr_debug("tboot_size: 0x%x\n", tboot->tboot_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static pgd_t *tboot_pg_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) static struct mm_struct tboot_mm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	.mm_rb          = RB_ROOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	.pgd            = swapper_pg_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	.mm_users       = ATOMIC_INIT(2),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	.mm_count       = ATOMIC_INIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	.write_protect_seq = SEQCNT_ZERO(tboot_mm.write_protect_seq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	MMAP_LOCK_INITIALIZER(init_mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	.page_table_lock =  __SPIN_LOCK_UNLOCKED(init_mm.page_table_lock),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	.mmlist         = LIST_HEAD_INIT(init_mm.mmlist),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static inline void switch_to_tboot_pt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	write_cr3(virt_to_phys(tboot_pg_dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static int map_tboot_page(unsigned long vaddr, unsigned long pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 			  pgprot_t prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	pgd_t *pgd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	p4d_t *p4d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	pud_t *pud;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	pmd_t *pmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	pte_t *pte;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	pgd = pgd_offset(&tboot_mm, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	p4d = p4d_alloc(&tboot_mm, pgd, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (!p4d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	pud = pud_alloc(&tboot_mm, p4d, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (!pud)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	pmd = pmd_alloc(&tboot_mm, pud, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (!pmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	pte = pte_alloc_map(&tboot_mm, pmd, vaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (!pte)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	set_pte_at(&tboot_mm, vaddr, pte, pfn_pte(pfn, prot));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	pte_unmap(pte);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 * PTI poisons low addresses in the kernel page tables in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 * name of making them unusable for userspace.  To execute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * code at such a low address, the poison must be cleared.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 * Note: 'pgd' actually gets set in p4d_alloc() _or_
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 * pud_alloc() depending on 4/5-level paging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	pgd->pgd &= ~_PAGE_NX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) static int map_tboot_pages(unsigned long vaddr, unsigned long start_pfn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			   unsigned long nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* Reuse the original kernel mapping */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	tboot_pg_dir = pgd_alloc(&tboot_mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (!tboot_pg_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	for (; nr > 0; nr--, vaddr += PAGE_SIZE, start_pfn++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		if (map_tboot_page(vaddr, start_pfn, PAGE_KERNEL_EXEC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void tboot_create_trampoline(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	u32 map_base, map_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	/* Create identity map for tboot shutdown code. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	map_base = PFN_DOWN(tboot->tboot_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	map_size = PFN_UP(tboot->tboot_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	if (map_tboot_pages(map_base << PAGE_SHIFT, map_base, map_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		panic("tboot: Error mapping tboot pages (mfns) @ 0x%x, 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		      map_base, map_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) #ifdef CONFIG_ACPI_SLEEP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static void add_mac_region(phys_addr_t start, unsigned long size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	struct tboot_mac_region *mr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	phys_addr_t end = start + size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (tboot->num_mac_regions >= MAX_TB_MAC_REGIONS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		panic("tboot: Too many MAC regions\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	if (start && size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		mr = &tboot->mac_regions[tboot->num_mac_regions++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		mr->start = round_down(start, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		mr->size  = round_up(end, PAGE_SIZE) - mr->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int tboot_setup_sleep(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	tboot->num_mac_regions = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	for (i = 0; i < e820_table->nr_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		if ((e820_table->entries[i].type != E820_TYPE_RAM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		 && (e820_table->entries[i].type != E820_TYPE_RESERVED_KERN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		add_mac_region(e820_table->entries[i].addr, e820_table->entries[i].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	tboot->acpi_sinfo.kernel_s3_resume_vector =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		real_mode_header->wakeup_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #else /* no CONFIG_ACPI_SLEEP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int tboot_setup_sleep(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	/* S3 shutdown requested, but S3 not supported by the kernel... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) void tboot_shutdown(u32 shutdown_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	void (*shutdown)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (!tboot_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	 * if we're being called before the 1:1 mapping is set up then just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	 * return and let the normal shutdown happen; this should only be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	 * due to very early panic()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	if (!tboot_pg_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	/* if this is S3 then set regions to MAC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (shutdown_type == TB_SHUTDOWN_S3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		if (tboot_setup_sleep())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	tboot->shutdown_type = shutdown_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	switch_to_tboot_pt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	shutdown = (void(*)(void))(unsigned long)tboot->shutdown_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	/* should not reach here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	while (1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		halt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static void tboot_copy_fadt(const struct acpi_table_fadt *fadt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #define TB_COPY_GAS(tbg, g)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	tbg.space_id     = g.space_id;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	tbg.bit_width    = g.bit_width;		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	tbg.bit_offset   = g.bit_offset;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	tbg.access_width = g.access_width;	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	tbg.address      = g.address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	TB_COPY_GAS(tboot->acpi_sinfo.pm1a_cnt_blk, fadt->xpm1a_control_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	TB_COPY_GAS(tboot->acpi_sinfo.pm1b_cnt_blk, fadt->xpm1b_control_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	TB_COPY_GAS(tboot->acpi_sinfo.pm1a_evt_blk, fadt->xpm1a_event_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	TB_COPY_GAS(tboot->acpi_sinfo.pm1b_evt_blk, fadt->xpm1b_event_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	 * We need phys addr of waking vector, but can't use virt_to_phys() on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	 * &acpi_gbl_FACS because it is ioremap'ed, so calc from FACS phys
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	 * addr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	tboot->acpi_sinfo.wakeup_vector = fadt->facs +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		offsetof(struct acpi_table_facs, firmware_waking_vector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int tboot_sleep(u8 sleep_state, u32 pm1a_control, u32 pm1b_control)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	static u32 acpi_shutdown_map[ACPI_S_STATE_COUNT] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		/* S0,1,2: */ -1, -1, -1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		/* S3: */ TB_SHUTDOWN_S3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		/* S4: */ TB_SHUTDOWN_S4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		/* S5: */ TB_SHUTDOWN_S5 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	if (!tboot_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	tboot_copy_fadt(&acpi_gbl_FADT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	tboot->acpi_sinfo.pm1a_cnt_val = pm1a_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	tboot->acpi_sinfo.pm1b_cnt_val = pm1b_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	/* we always use the 32b wakeup vector */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	tboot->acpi_sinfo.vector_width = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (sleep_state >= ACPI_S_STATE_COUNT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	    acpi_shutdown_map[sleep_state] == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		pr_warn("unsupported sleep state 0x%x\n", sleep_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	tboot_shutdown(acpi_shutdown_map[sleep_state]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static int tboot_extended_sleep(u8 sleep_state, u32 val_a, u32 val_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (!tboot_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	pr_warn("tboot is not able to suspend on platforms with reduced hardware sleep (ACPIv5)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static atomic_t ap_wfs_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static int tboot_wait_for_aps(int num_aps)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	unsigned long timeout;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	timeout = AP_WAIT_TIMEOUT*HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	while (atomic_read((atomic_t *)&tboot->num_in_wfs) != num_aps &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	       timeout) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		mdelay(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		timeout--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (timeout)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		pr_warn("tboot wait for APs timeout\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	return !(atomic_read((atomic_t *)&tboot->num_in_wfs) == num_aps);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) static int tboot_dying_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	atomic_inc(&ap_wfs_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (num_online_cpus() == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		if (tboot_wait_for_aps(atomic_read(&ap_wfs_count)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) #define TBOOT_LOG_UUID	{ 0x26, 0x25, 0x19, 0xc0, 0x30, 0x6b, 0xb4, 0x4d, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			  0x4c, 0x84, 0xa3, 0xe9, 0x53, 0xb8, 0x81, 0x74 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) #define TBOOT_SERIAL_LOG_ADDR	0x60000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) #define TBOOT_SERIAL_LOG_SIZE	0x08000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) #define LOG_MAX_SIZE_OFF	16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) #define LOG_BUF_OFF		24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static uint8_t tboot_log_uuid[16] = TBOOT_LOG_UUID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static ssize_t tboot_log_read(struct file *file, char __user *user_buf, size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	void __iomem *log_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	u8 log_uuid[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	u32 max_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	void *kbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	int ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	log_base = ioremap(TBOOT_SERIAL_LOG_ADDR, TBOOT_SERIAL_LOG_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (!log_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	memcpy_fromio(log_uuid, log_base, sizeof(log_uuid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if (memcmp(&tboot_log_uuid, log_uuid, sizeof(log_uuid)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		goto err_iounmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	max_size = readl(log_base + LOG_MAX_SIZE_OFF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (*ppos >= max_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		goto err_iounmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	if (*ppos + count > max_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 		count = max_size - *ppos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	kbuf = kmalloc(count, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	if (!kbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		goto err_iounmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	memcpy_fromio(kbuf, log_base + LOG_BUF_OFF + *ppos, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (copy_to_user(user_buf, kbuf, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		goto err_kfree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	*ppos += count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) err_kfree:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	kfree(kbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) err_iounmap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	iounmap(log_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static const struct file_operations tboot_log_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	.read	= tboot_log_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	.llseek	= default_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) #endif /* CONFIG_DEBUG_FS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) static __init int tboot_late_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	if (!tboot_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	tboot_create_trampoline();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	atomic_set(&ap_wfs_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	cpuhp_setup_state(CPUHP_AP_X86_TBOOT_DYING, "x86/tboot:dying", NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 			  tboot_dying_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) #ifdef CONFIG_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	debugfs_create_file("tboot_log", S_IRUSR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 			arch_debugfs_dir, NULL, &tboot_log_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	acpi_os_set_prepare_sleep(&tboot_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	acpi_os_set_prepare_extended_sleep(&tboot_extended_sleep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) late_initcall(tboot_late_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  * TXT configuration registers (offsets from TXT_{PUB, PRIV}_CONFIG_REGS_BASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) #define TXT_PUB_CONFIG_REGS_BASE       0xfed30000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) #define TXT_PRIV_CONFIG_REGS_BASE      0xfed20000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) /* # pages for each config regs space - used by fixmap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) #define NR_TXT_CONFIG_PAGES     ((TXT_PUB_CONFIG_REGS_BASE -                \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 				  TXT_PRIV_CONFIG_REGS_BASE) >> PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) /* offsets from pub/priv config space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) #define TXTCR_HEAP_BASE             0x0300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) #define TXTCR_HEAP_SIZE             0x0308
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) #define SHA1_SIZE      20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) struct sha1_hash {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	u8 hash[SHA1_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct sinit_mle_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	u32               version;             /* currently 6 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	struct sha1_hash  bios_acm_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	u32               edx_senter_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	u64               mseg_valid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	struct sha1_hash  sinit_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct sha1_hash  mle_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	struct sha1_hash  stm_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct sha1_hash  lcp_policy_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	u32               lcp_policy_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	u32               rlp_wakeup_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	u32               reserved;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	u32               num_mdrs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	u32               mdrs_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	u32               num_vtd_dmars;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	u32               vtd_dmars_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) } __packed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) struct acpi_table_header *tboot_get_dmar_table(struct acpi_table_header *dmar_tbl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	void *heap_base, *heap_ptr, *config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	if (!tboot_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 		return dmar_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	 * ACPI tables may not be DMA protected by tboot, so use DMAR copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	 * SINIT saved in SinitMleData in TXT heap (which is DMA protected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	/* map config space in order to get heap addr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	config = ioremap(TXT_PUB_CONFIG_REGS_BASE, NR_TXT_CONFIG_PAGES *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 			 PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (!config)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	/* now map TXT heap */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	heap_base = ioremap(*(u64 *)(config + TXTCR_HEAP_BASE),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 			    *(u64 *)(config + TXTCR_HEAP_SIZE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	iounmap(config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	if (!heap_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	/* walk heap to SinitMleData */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	/* skip BiosData */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	heap_ptr = heap_base + *(u64 *)heap_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	/* skip OsMleData */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	heap_ptr += *(u64 *)heap_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	/* skip OsSinitData */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	heap_ptr += *(u64 *)heap_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	/* now points to SinitMleDataSize; set to SinitMleData */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	heap_ptr += sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	/* get addr of DMAR table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	dmar_tbl = (struct acpi_table_header *)(heap_ptr +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		   ((struct sinit_mle_data *)heap_ptr)->vtd_dmars_off -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		   sizeof(u64));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	/* don't unmap heap because dmar.c needs access to this */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	return dmar_tbl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) int tboot_force_iommu(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	if (!tboot_enabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (no_iommu || dmar_disabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		pr_warn("Forcing Intel-IOMMU to enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	dmar_disabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	no_iommu = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }