Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp5289292yba; Wed, 8 May 2019 10:44:38 -0700 (PDT) X-Google-Smtp-Source: APXvYqzOSUjqko4W2fefYRCd5FwzmJaK4xdo4hk91PfQs2Fh0/i41TYkGBJr7YL8T8QLkpoPWNWu X-Received: by 2002:a17:902:8507:: with SMTP id bj7mr27817877plb.214.1557337478171; Wed, 08 May 2019 10:44:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557337478; cv=none; d=google.com; s=arc-20160816; b=RvJX7pj7wHFGLG4gB+tuUV9kjfDsITmyEFsQJn1qVGtvhKl/4XZeosQbTkFfIA8DXs og3UPrFRXxWnv0h05QtxSu8L+fYem1GQ9M13MLB02QV6xwbQ9WcqGpcL9ZXGF1j0rQZk x4mvy7cOASGlGmdUjPQtq+lULCBsSuIlDswUn97qXsFVQPiaba2d8pxm951RuQIPRfnS X2IvWki8bsjna09bBMxXKPumGT4MOigMGyLFm2XjfhUqj36AOxPDOd2CfPfG8shoPPK3 3rEAIHeoHjKiIOHwRNbt/40cqgsEIULT6n5/hF+aVegHhJs2lNqS8KqtbZ2Y6WqxPb// Cy7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=WDFDEcXg+f/4xOnB0EiSvj+2Wa4G0QK/3j1Q+39oVvk=; b=JaFUt/x3ZWBdfuCHEQzbslDw7Y+xX+PAy55NwnrES3vw2puG80+wzdqdjag9TXiUno dr2nLf7d7GzNpQ8OVgOlQxs5d1AchqgvvtETZjmtUDpf4T11jcRgNX3WL8LT3gaXewg7 doDfVZEhND4Yx5pDg5fG02QbSU2ZH3/wYKBy5L8WA83jZ1VgryHxPJmSEwJlUU7TUr+2 iwfXqvDZ3HpNAuJzfJj9AveelzEITDcsPAoFfJzPCbKitIoN3xqMkJgIB6szF0HI1elA 46fHvy0k/7Tlyrq/3aiE3VO42yAK70pAw2IbGYsTS37WDowSLl5CRw8DUwd4K8ycPIOv +BeA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bh2si8472247plb.430.2019.05.08.10.44.22; Wed, 08 May 2019 10:44:38 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727423AbfEHOri (ORCPT + 99 others); Wed, 8 May 2019 10:47:38 -0400 Received: from mga03.intel.com ([134.134.136.65]:59536 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728453AbfEHOov (ORCPT ); Wed, 8 May 2019 10:44:51 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 08 May 2019 07:44:51 -0700 X-ExtLoop1: 1 Received: from black.fi.intel.com ([10.237.72.28]) by orsmga005.jf.intel.com with ESMTP; 08 May 2019 07:44:46 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 51F061123; Wed, 8 May 2019 17:44:31 +0300 (EEST) From: "Kirill A. Shutemov" To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: [PATCH, RFC 52/62] x86/mm: introduce common code for mem encryption Date: Wed, 8 May 2019 17:44:12 +0300 Message-Id: <20190508144422.13171-53-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190508144422.13171-1-kirill.shutemov@linux.intel.com> References: <20190508144422.13171-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jacob Pan Both Intel MKTME and AMD SME have needs to support DMA address translation with encryption related bits. Common functions are introduced in this patch to keep DMA generic code abstracted. Signed-off-by: Jacob Pan Signed-off-by: Kirill A. Shutemov --- arch/x86/Kconfig | 4 ++++ arch/x86/mm/Makefile | 1 + arch/x86/mm/mem_encrypt_common.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 arch/x86/mm/mem_encrypt_common.c diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 62cfb381fee3..ce9642e2c31b 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -1505,11 +1505,15 @@ config X86_CPA_STATISTICS config ARCH_HAS_MEM_ENCRYPT def_bool y +config X86_MEM_ENCRYPT_COMMON + def_bool n + config AMD_MEM_ENCRYPT bool "AMD Secure Memory Encryption (SME) support" depends on X86_64 && CPU_SUP_AMD select DYNAMIC_PHYSICAL_MASK select ARCH_USE_MEMREMAP_PROT + select X86_MEM_ENCRYPT_COMMON ---help--- Say yes to enable support for the encryption of system memory. This requires an AMD processor that supports Secure Memory diff --git a/arch/x86/mm/Makefile b/arch/x86/mm/Makefile index 4ebee899c363..89dddbc01b1b 100644 --- a/arch/x86/mm/Makefile +++ b/arch/x86/mm/Makefile @@ -55,3 +55,4 @@ obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_identity.o obj-$(CONFIG_AMD_MEM_ENCRYPT) += mem_encrypt_boot.o obj-$(CONFIG_X86_INTEL_MKTME) += mktme.o +obj-$(CONFIG_X86_MEM_ENCRYPT_COMMON) += mem_encrypt_common.o diff --git a/arch/x86/mm/mem_encrypt_common.c b/arch/x86/mm/mem_encrypt_common.c new file mode 100644 index 000000000000..2adee65eec46 --- /dev/null +++ b/arch/x86/mm/mem_encrypt_common.c @@ -0,0 +1,28 @@ +#include +#include +#include + +/* + * Encryption bits need to be set and cleared for both Intel MKTME and + * AMD SME when converting between DMA address and physical address. + */ +dma_addr_t __mem_encrypt_dma_set(dma_addr_t daddr, phys_addr_t paddr) +{ + unsigned long keyid; + + if (sme_active()) + return __sme_set(daddr); + keyid = page_keyid(pfn_to_page(__phys_to_pfn(paddr))); + + return (daddr & ~mktme_keyid_mask) | (keyid << mktme_keyid_shift); +} +EXPORT_SYMBOL_GPL(__mem_encrypt_dma_set); + +phys_addr_t __mem_encrypt_dma_clear(phys_addr_t paddr) +{ + if (sme_active()) + return __sme_clr(paddr); + + return paddr & ~mktme_keyid_mask; +} +EXPORT_SYMBOL_GPL(__mem_encrypt_dma_clear); -- 2.20.1