From patchwork Mon Aug 22 23:26:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9294757 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B6B3A60B16 for ; Mon, 22 Aug 2016 23:50:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A81CC286EC for ; Mon, 22 Aug 2016 23:50:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 936BC286FE; Mon, 22 Aug 2016 23:50:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2F0FE28796 for ; Mon, 22 Aug 2016 23:50:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756635AbcHVXt6 (ORCPT ); Mon, 22 Aug 2016 19:49:58 -0400 Received: from mail-by2nam03on0048.outbound.protection.outlook.com ([104.47.42.48]:45664 "EHLO NAM03-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1754221AbcHVXtv (ORCPT ); Mon, 22 Aug 2016 19:49:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector1-amd-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=UOzWchusHEn5uWIENSDRhjAxDiTzZyJyfHxT8YlcMuo=; b=n/4RJIhj2wFBQFb74Lyrw8xGukXeItCY6RPCGoDC5hfnNTNZUG3DFRnEQWgn3DwgoKi87kNQk+0aHK3NoqCmoHiOQSLuE/FlAO+jWNFZXkibe4BE+4hc5MhmVZ2/XILAdwGLM5svfY1xWtY9u24zIwjrBVfRJM/oUS/cvyynMno= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from [127.0.1.1] (165.204.77.1) by BY2PR12MB0663.namprd12.prod.outlook.com (10.163.113.152) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA_P384) id 15.1.557.21; Mon, 22 Aug 2016 23:26:05 +0000 Subject: [RFC PATCH v1 12/28] x86: DMA support for SEV memory encryption From: Brijesh Singh To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Date: Mon, 22 Aug 2016 19:26:02 -0400 Message-ID: <147190836254.9523.17071309814378405604.stgit@brijesh-build-machine> In-Reply-To: <147190820782.9523.4967724730957229273.stgit@brijesh-build-machine> References: <147190820782.9523.4967724730957229273.stgit@brijesh-build-machine> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: BY2PR21CA0003.namprd21.prod.outlook.com (10.162.74.141) To BY2PR12MB0663.namprd12.prod.outlook.com (10.163.113.152) X-MS-Office365-Filtering-Correlation-Id: 82708357-803b-4255-2c4e-08d3cae3b196 X-Microsoft-Exchange-Diagnostics: 1; BY2PR12MB0663; 2:fF0CV2VnB3rYQqnFkoTbaNsI9Drl/giuZpE3h/B6KXPKblP+wsX/H0jDCMveExqQ3+MiaHs2uR/Cm/qIAvyIZA3/sYmjGpga105Dox9jf4F445kkbIWVzSKlPZX10zGNmNHzsDyXS4evileAY7QfEAtIcUs8albo2WcMh4y/nDyzXbPNpuxE5/J1bvkfjYHG; 3:NMKRa8BZ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r/R5KQvlRfm1vRcibpq9v8Q2n X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6040176)(601004)(2401047)(5005006)(8121501046)(3002001)(10201501046)(6055026); SRVR:BY2PR12MB0663; BCL:0; PCL:0; RULEID:; SRVR:BY2PR12MB0663; X-Forefront-PRVS: 00429279BA X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(4630300001)(6009001)(6049001)(7916002)(189002)(199003)(33646002)(7416002)(92566002)(50466002)(77096005)(42186005)(7406005)(230700001)(19580395003)(101416001)(83506001)(19580405001)(2950100001)(9686002)(76176999)(54356999)(81156014)(4001350100001)(586003)(106356001)(50986999)(86362001)(47776003)(81166006)(305945005)(105586002)(97736004)(5001770100001)(7846002)(229853001)(23676002)(189998001)(8676002)(2906002)(7736002)(3846002)(2201001)(6116002)(33716001)(66066001)(103116003)(107886002)(68736007)(5660300001)(921003)(217873001)(83996005)(2101003)(1121003); DIR:OUT; SFP:1101; SCL:1; SRVR:BY2PR12MB0663; H:[127.0.1.1]; FPR:; SPF:None; PTR:InfoNoRecords; MX:1; A:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtCWTJQUjEyTUIwNjYzOzIzOjRTdTBxSlpzSVQ5TEVQZmNBWUJtQTE4cE84?= =?utf-8?B?eGtQSURYK05KUE82RDhTdkRvODRiZzZhVnkzY0IvT3hNVVZtSytkcWh4ZlEv?= =?utf-8?B?cFJ5YzVZMG9yT1dqYklHN3Jvd1hhWEk2bUh1SHlaWjVnQUxkckNEam5qNUVE?= =?utf-8?B?ZkF6YWdBeFhLaDdnaTc1WXFMSEYvSTM4dmYrYXdZbnAzbmxJblY3aEVEVy9l?= =?utf-8?B?RURCS0pRQUhIRnRTNHludHg3TFlZRXJyQ2sxM3c5MkpYNWFGSXc0ZmlLYVlI?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470nVrFYEZH6uQ7fej3wFgwFU/WYeCM+VRX+v6I6Wmfd1mSfGb7ewJPQGBSXamdkrcLCS8qUjTh X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Aug 2016 23:26:05.2679 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY2PR12MB0663 X-OriginatorOrg: amd.com Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Tom Lendacky DMA access to memory mapped as encrypted while SEV is active can not be encrypted during device write or decrypted during device read. In order for DMA to properly work when SEV is active, the swiotlb bounce buffers must be used. Signed-off-by: Tom Lendacky --- arch/x86/mm/mem_encrypt.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe linux-crypto" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 1154353..ce6e3ea 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -173,8 +173,52 @@ void __init sme_early_init(void) /* Update the protection map with memory encryption mask */ for (i = 0; i < ARRAY_SIZE(protection_map); i++) protection_map[i] = __pgprot(pgprot_val(protection_map[i]) | sme_me_mask); + + if (sev_active) + swiotlb_force = 1; } +static void *sme_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle, + gfp_t gfp, unsigned long attrs) +{ + void *vaddr; + + vaddr = x86_swiotlb_alloc_coherent(dev, size, dma_handle, gfp, attrs); + if (!vaddr) + return NULL; + + /* Clear the SME encryption bit for DMA use */ + sme_set_mem_dec(vaddr, size); + + /* Remove the encryption bit from the DMA address */ + *dma_handle &= ~sme_me_mask; + + return vaddr; +} + +static void sme_free(struct device *dev, size_t size, void *vaddr, + dma_addr_t dma_handle, unsigned long attrs) +{ + /* Set the SME encryption bit for re-use as encrypted */ + sme_set_mem_enc(vaddr, size); + + x86_swiotlb_free_coherent(dev, size, vaddr, dma_handle, attrs); +} + +static struct dma_map_ops sme_dma_ops = { + .alloc = sme_alloc, + .free = sme_free, + .map_page = swiotlb_map_page, + .unmap_page = swiotlb_unmap_page, + .map_sg = swiotlb_map_sg_attrs, + .unmap_sg = swiotlb_unmap_sg_attrs, + .sync_single_for_cpu = swiotlb_sync_single_for_cpu, + .sync_single_for_device = swiotlb_sync_single_for_device, + .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu, + .sync_sg_for_device = swiotlb_sync_sg_for_device, + .mapping_error = swiotlb_dma_mapping_error, +}; + /* Architecture __weak replacement functions */ void __init mem_encrypt_init(void) { @@ -184,6 +228,10 @@ void __init mem_encrypt_init(void) /* Make SWIOTLB use an unencrypted DMA area */ swiotlb_clear_encryption(); + /* Use SEV DMA operations if SEV is active */ + if (sev_active) + dma_ops = &sme_dma_ops; + pr_info("memory encryption active\n"); }