From patchwork Tue Sep 19 20:46:05 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9960207 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 A6D9C6038F for ; Tue, 19 Sep 2017 20:47:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 987F426E3A for ; Tue, 19 Sep 2017 20:47:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8CF0926E96; Tue, 19 Sep 2017 20:47:29 +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.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_HI autolearn=ham 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 AAFBF27F88 for ; Tue, 19 Sep 2017 20:47:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751832AbdISUrK (ORCPT ); Tue, 19 Sep 2017 16:47:10 -0400 Received: from mail-by2nam01on0056.outbound.protection.outlook.com ([104.47.34.56]:55572 "EHLO NAM01-BY2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751660AbdISUrC (ORCPT ); Tue, 19 Sep 2017 16:47:02 -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=WrXPNU6C7TZQsp1ERK/GXhMrxmuFRoIfyHMPLh5oU/k=; b=ak0rXIT3kyElZd0RyY23dwg+oUUrTnYWEi4x4vgYKbe8A1nwjWcYSqhcjsG5POANjt5+h6kZ9z5OHJn8CdpXRAzRA4SUpAjKXZZy/E+3PNBj4bbOQ2wEBq0ym/cZu3FmMKms2zOvR7tja82FPbb9AX4up2c1a/GKAYyaY2MROf8= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from ubuntu-010236106000.amd.com (165.204.78.1) by SN1PR12MB0158.namprd12.prod.outlook.com (10.162.3.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.56.11; Tue, 19 Sep 2017 20:46:53 +0000 From: Brijesh Singh To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Brijesh Singh , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Borislav Petkov , Herbert Xu , Gary Hook , Tom Lendacky , linux-crypto@vger.kernel.org Subject: [Part2 PATCH v4 07/29] crypto: ccp: Add Secure Encrypted Virtualization (SEV) command support Date: Tue, 19 Sep 2017 15:46:05 -0500 Message-Id: <20170919204627.3875-8-brijesh.singh@amd.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20170919204627.3875-1-brijesh.singh@amd.com> References: <20170919204627.3875-1-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.78.1] X-ClientProxiedBy: CY4PR04CA0071.namprd04.prod.outlook.com (10.171.243.164) To SN1PR12MB0158.namprd12.prod.outlook.com (10.162.3.145) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9b4fd423-aaaa-4381-6d61-08d4ff9f8fd0 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(300000500095)(300135000095)(300000501095)(300135300095)(22001)(300000502095)(300135100095)(2017030254152)(48565401081)(300000503095)(300135400095)(2017052603199)(201703131423075)(201703031133081)(201702281549075)(300000504095)(300135200095)(300000505095)(300135600095)(300000506095)(300135500095); SRVR:SN1PR12MB0158; X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0158; 3:asY3zsUmrF7kicezSdgVwVS+/7c5VyiWLOnT+pqx5G7NQQQ9s4VyL9WQ1FmjFqM3+tQ+AStMeB1f3sbLDOAekQan3iDLDDgvwNlSmD3GnvJ2ZwABnSepB16O2+hLG9SgS3WcIWMXZ0vvP1rXq4zRBTCbg2NVfFdtBY4Cea2CYALj+CpiKRK+9J+/KjsewqN4/M7NIP1c6k8WD/9YRW45zTZ0ZdM5ReiwSKUgDoxdEDBO+EN07gWxIkt91B6pI5f6; 25:Y49k5YUp0NU98jhKyW/8lO5C7ZWVyjw0OP5D5jaDJRXa4z9jlwOZKoNNaNCrSMo3RNyQwfeO5P+ayR7AQwqnELOdeG80/KPrafA+ccoM0O88SEF/WA3yqaJ0i9WqfaWyLWQ7HDocDKR41iAc/xnyOZPIlSZPvTsBGWhjv7ycIP6wqyEw3vkjvJHhfsQdq63S1JQKlqWxSBU5zJzDMckFlWo4ybPH7JLEqq05F57dbEbAHtdekIj47ik67pWnaLn/o5nywyS+hfMyTT11RNbilOnS0TGw+4BxoS3ECayC1r9vUe9JeQe7Lgm9JA7/BpUE7sib6z02bnUKXOwCTXIUug==; 31:a8HbDj7HSDiEF0KFAPSSx8SJbw4H0bbpe3nTT7CRXSo2nODrFD0mArCVmliFv3BaFwO6L4gTzgNiHriwHkHd0RmGEbcZq+DOCccqZe1LAm+XR8gPxcb31IYl1JnKJ5I2o0ovomyuB6NvqjlXBfRPXhaHGrMjor/UDEVHhfriZVrQUB6m4sSVxjB8cJAYX+23gaC4xEhoEVquDOlcpICmH4Fc0rHQHNlHbY2yhKWgWPk= X-MS-TrafficTypeDiagnostic: SN1PR12MB0158: X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0158; 20:Fh8JXbsM1M97nu9VaExiMqUl7cvAzDzXAukddipmrZOE8rbPgmTsJXRxVqkWzYQhnva0qbGODb656fxYft6C/w5Gi3I44uq1uuTTJUDbCZtHysjaEH6iR+IwPdwiinnUgQ/sWsx8MsAqSQeHvffVhbEUgxJOLq5I7fTblJWG//jo+9qhn9aSDHEX53qDHafGmY/kaneIYZT8gu+Eo4CmHMejb8/J48ALFYUDGaZdJRcVB1wbZRvLyBV0qUS/Vwroe8AEnoYPsJItHsC9I/AsgZu+JhBmhnO3YHhk+KWuVruq58AdzKg1f+iTxQYXqPKUcWx/kqHwLHcwAx6v4dWkiWI3cipPqgjhLoXgKecVM+e7Vx5dBSRZRa/X0uiSiLVbVTsqZ+zh9Ogt6w0vw36Efk/ygxfXig8D9mrca4jFvp6Rq83jJeJed14RQ9UJ9rL2XTJitPEMqPSGheXM7FVKCtUHnLX1ipJyUJ0U/Qx+pQhc00D09h1fQRxUE3PRTudx; 4:LN3Z83myJEeAd/liVYue00bxD3MNEeQr3mbr19qtU3sADsvyCi4sPBdOPWEHz4oUiBMPbaGP7AqJ9NnJrlTyQGy06QTwkkFxhNVb6C74ySkeFHwCisY5YhQcBaPuMO/C5U3KYTykaZBkDDwWbbLngi+b1mPn6CAfKEJjJliRqlLfJ1j/6qF0/zt1Dp2QDDpe0rCOBeYo7WgnDeYxgD0HsNinPH3YPy5SMwkcn6zxQ/hzenFeAEnksg1XYkMWjRuP30J+xAzpUwF6oOHdBvidpnHCRne+8yM6ECfcpKecGeKyf2gy0Pw/jxMYuwJaifeY X-Exchange-Antispam-Report-Test: UriScan:(9452136761055)(767451399110); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(2401047)(8121501046)(5005006)(93006095)(93001095)(100000703101)(100105400095)(3002001)(10201501046)(6055026)(6041248)(20161123560025)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123555025)(20161123562025)(20161123564025)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:SN1PR12MB0158; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:SN1PR12MB0158; X-Forefront-PRVS: 04359FAD81 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(346002)(376002)(39860400002)(199003)(189002)(86362001)(50466002)(8936002)(97736004)(6116002)(3846002)(81156014)(8676002)(50226002)(2950100002)(81166006)(316002)(478600001)(16526017)(6666003)(966005)(7736002)(66066001)(305945005)(47776003)(6486002)(6306002)(189998001)(53416004)(25786009)(101416001)(2870700001)(50986999)(76176999)(53936002)(2906002)(68736007)(36756003)(106356001)(1076002)(4326008)(5660300001)(53946003)(105586002)(33646002)(23676002)(54906003)(2004002)(134885004); DIR:OUT; SFP:1101; SCL:1; SRVR:SN1PR12MB0158; H:ubuntu-010236106000.amd.com; FPR:; SPF:None; PTR:InfoNoRecords; A:1; MX:1; LANG:en; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtTTjFQUjEyTUIwMTU4OzIzOmhoSmRpNVBxajZOaEd6M0lnaWNIbkFDRGpZ?= =?utf-8?B?MlhCalAzc3prd2h3OElQSHFWQ1JobnNLeEVFMWM5VWxHTmRkREhERXVYMElN?= =?utf-8?B?TXBqTStmTWxkODlib1d4ZTVhby9tTWxjcHhqWnFqYnZWbHRHa0xJQkdDM2VI?= =?utf-8?B?WFV6TFIzMHZYNGFRVDN6SXBZNzRFQWxmdWhNUHNnaTNkZHl4TlE5WHl4NVc4?= =?utf-8?B?Y012N1JEM25mZ2Frd1dRT0xncjVjQlVUZnRGMHFxRnBoclpKVjhlNUNQMlBR?= =?utf-8?B?cU5IaU5LcG5KdGpXMGthVVd5NU0vVWx1MlMxM09ONkJLNnZMSERhVEM2RVdv?= =?utf-8?B?S21SRWdaYW9reDQ3bjdZbTBFNVVRVDdWM2RQTU54RVVZcyswS0g0cVg1cUlu?= =?utf-8?B?cWVDMUR3dUdiMHh4QlJTNlpvTkQvcFNrdzRXLzR3ekRCSVZJSytFckNnUE1Z?= =?utf-8?B?aCt5bHduZDZhdjlLU2ZtOGtzNUh2aWNnRlNtemtWeWtoM2NIMXp1R0Z5dmVh?= =?utf-8?B?WEk1WE16ZjZWTGg5ZlFIZzkxSFZ2ZVFsNmsrOEM2TU9zWHIyemVWOFRvQW1o?= =?utf-8?B?Nlp3Z2RwVGo4S0ovcFkyTVZuK2RLb2x3dXo3RXVmL1EvVk52QUw0L1N3bVVU?= =?utf-8?B?UEkwQ0srYlh0SWdYN1FReTh4TlR0V096Y3FzWFZJTHVzSzlWczZwNk5LbEx0?= =?utf-8?B?VDlNSzUySmxHRDFVL3RLRkhTRDA2LzR4QVBmTDYwK3docTBTQTJFSHp6UHFV?= =?utf-8?B?emlOdTFLYkUxMk5PZVRSN1QyaWVmS1ZGcUNzMGpkV3UzaXRqTTlseFdPSUtO?= =?utf-8?B?TnlLalJQeUE0NXdrdjdvZXZPVWppeEdpUlJvd0dzR0hqZWNwKzIyQWM5bTQz?= =?utf-8?B?WERQZ05FN0krZVFsZE5CMEIva0YrZGhhcEtIaXl4czlxOEt4ZTZCMWhaMS9Y?= =?utf-8?B?dklzK0VFSm9vczkydlkvTzVVSnNvSHlnTFdScHE2WmJkTi9Kak1KeE9QR0Ji?= =?utf-8?B?R0lzYmpyOWlWNDNiUWdJU3A4R2dvU1Bod0ZPYVFSTEVqcSttN1A4aGMwYUls?= =?utf-8?B?Z3UrSnRYSnhWM2ZRSlpqSmpreHJLdjVNcWl1UEF6bGJKZmxkcjdjbW16Wjc2?= =?utf-8?B?ZVhZaWkwUTEyVStjajlkZE5DSlpmbkpNVVU3czZDeXcydXRaZU45L0cxVjQx?= =?utf-8?B?VFBBQUxTZW5LNDhMK3pvdFhXMytuT0JuZUdwSVNMUGRGbE5yaXpXVWRtWVZ0?= =?utf-8?B?MTJuRE13U1VVbm14TWcyRGo3Y3k2M3Y3MFNkTUNQNWNxL3dkRXcyZm9xaWNC?= =?utf-8?B?MGNMZVl4elNXd2QxSjlUU3cybHNFQllNTHNZcXY4L0hjS1Y4MEdhbUo4MXBG?= =?utf-8?B?SzVtanRHbFltdmIvR1kwckJFcXE3WkV2RXpadVJXVnVGblRFSUQ3SDV1Wmwv?= =?utf-8?B?TFZYQmkrbjNJSTMzVHNqb3FNM3Q0TGFRUTZvSmNQa3dHR2tORUYzTXkreFRn?= =?utf-8?B?emxlNkl2ZlNhWEpIMTNGalhjeFd2S1BnUDR0NUhGYnJCM0RZbmlIaG0vWGtJ?= =?utf-8?B?TnVSVkt3ZWdTZkJpV0NXMWFuUlBxbFE9PQ==?= X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0158; 6:LrjiIsLJpc8doGAhAiX7ERaSl8ZXI4E475ogd5NZir6Q3gls//lKvTvzM/hDdDWk1mvanf4SkUiXKQUi0j2k3ZrWjgdBLPtUyeuA45CJ/KaKCkH5qnjfHhKwbemq2IpnZH/2ajf1SWBmV/39pyYoMHJkl57HRlouyEANx5R6LowGqgvMEXzLw4bvrKHhhVpH+rfYkt4oJ3N1vSXUufhApg0vMNqPVwiElrF56S0tRAeLwnfhyRAHnQzPTnCfC23utkA/km2tqWepTfsv2n/lOfbaNHV8d7a480X9qmkLedGVIHlf122GccbRqkvT7H4CGM4KqrmDVxYTwbIHS0j0aw==; 5:008jxxm9vR4a6OJ/GmZm7Qej3KIGzX4b/dpGC1sZVzW0ap9MVhxNg7sm4e59Q4q7MKwagvmyCi7HRsfMOgsB91dI54caCKy3ZaVljikzcanVU89DpKPRik5QqM2d5oYP/GqBpJqp6v9YJy8RknL2Ww==; 24:qJTVRcck7Mm9ig6uHMaSABIH/TZNLOlhBExPA2iyZ6q2W0/w0wb+UKSxeLvl9L8IQLmrKtt+y/SwugFTmBoj30Nyq+oDYFPCN3n7/R//3S4=; 7:DTJsc4CN/oWmKFJ7S0oK6B0HNzYdP5ozyF0BkmmuvFsBoulzBa47B/Bwbyx8gwu5e4fbSnR2rp+JgxUjN4kKpyapMEVwBKO3elXalk1/4D/OP0j23EL0/vVcxVsZHz+HjwyBbMCaxUE7I7epHbVbWo92j8uxN936XyLO9UPpkW5jVbZLoJhRLV/P2sQXu0kGt+bKezLKhuMzD0zSohBLpAwWSOosW+7beHm13gGw0Ac= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; SN1PR12MB0158; 20:dvaUkkhR95k4nlFU7aIPMVRKttfS1PtjC2Ll4Q9JGCSRXcfo8d78Hg/FyaY158Ec4LX9zYZTt3zq26xVfD8gsZJyGlpuhModpcuy/mBCdruy47j7hJ1KfJSb429lXSU02luQAGXyS9GpBNnh0TjBEgLzcYwcuByoAF9uH8KCpu2heN6svCXetqQUFAG2noXs9bGpOoY0ts3cdDjBhy0xc7AzzkulxtCnaZwRntZKKVLGFn/4tNliqV53DAa3gkqr X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2017 20:46:53.6268 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB0158 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 AMDs new Secure Encrypted Virtualization (SEV) feature allows the memory contents of a virtual machines to be transparently encrypted with a key unique to the guest VM. The programming and management of the encryption keys are handled by the AMD Secure Processor (AMD-SP), which exposes the commands for these tasks. The complete spec is available at: http://support.amd.com/TechDocs/55766_SEV-KM%20API_Specification.pdf Extend the AMD-SP driver to provide the following support: - an in-kernel APIs to communicate with a SEV firmware. The APIs can be used by the hypervisor to create encryption context for the SEV guests. - an userspace IOCTL to manage the platform certificates Cc: Paolo Bonzini Cc: "Radim Krčmář" Cc: Borislav Petkov Cc: Herbert Xu Cc: Gary Hook Cc: Tom Lendacky Cc: linux-crypto@vger.kernel.org Cc: kvm@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/psp-dev.c | 743 ++++++++++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/psp-dev.h | 17 + include/linux/psp-sev.h | 171 ++++++++++ include/uapi/linux/psp-sev.h | 114 +++++++ 4 files changed, 1044 insertions(+), 1 deletion(-) create mode 100644 include/uapi/linux/psp-sev.h diff --git a/drivers/crypto/ccp/psp-dev.c b/drivers/crypto/ccp/psp-dev.c index e60e53272e71..c74e383666f8 100644 --- a/drivers/crypto/ccp/psp-dev.c +++ b/drivers/crypto/ccp/psp-dev.c @@ -23,9 +23,20 @@ #include #include +#include + #include "sp-dev.h" #include "psp-dev.h" +#define DEVICE_NAME "sev" + +static unsigned int sev_poll; +module_param(sev_poll, uint, 0444); +MODULE_PARM_DESC(sev_poll, "Poll for sev command completion - any non-zero value"); + +DEFINE_MUTEX(sev_cmd_mutex); +static bool sev_fops_registered; + const struct psp_vdata psp_entry = { .offset = 0x10500, }; @@ -49,9 +60,732 @@ static struct psp_device *psp_alloc_struct(struct sp_device *sp) irqreturn_t psp_irq_handler(int irq, void *data) { + unsigned int status; + struct psp_device *psp = data; + + /* read the interrupt status */ + status = ioread32(psp->io_regs + PSP_P2CMSG_INTSTS); + + /* check if its command completion */ + if (status & (1 << PSP_CMD_COMPLETE_REG)) { + int reg; + + /* check if its SEV command completion */ + reg = ioread32(psp->io_regs + PSP_CMDRESP); + if (reg & PSP_CMDRESP_RESP) { + psp->sev_int_rcvd = 1; + wake_up(&psp->sev_int_queue); + } + } + + /* clear the interrupt status by writing 1 */ + iowrite32(status, psp->io_regs + PSP_P2CMSG_INTSTS); + return IRQ_HANDLED; } +static struct psp_device *psp_get_master_device(void) +{ + struct sp_device *sp = sp_get_psp_master_device(); + + return sp ? sp->psp_data : NULL; +} + +static int sev_wait_cmd_poll(struct psp_device *psp, unsigned int timeout, + unsigned int *reg) +{ + int wait = timeout * 10; /* 100ms sleep => timeout * 10 */ + + while (--wait) { + msleep(100); + + *reg = ioread32(psp->io_regs + PSP_CMDRESP); + if (*reg & PSP_CMDRESP_RESP) + break; + } + + if (!wait) { + dev_err(psp->dev, "sev command timed out\n"); + return -ETIMEDOUT; + } + + return 0; +} + +static int sev_wait_cmd_ioc(struct psp_device *psp, unsigned int *reg) +{ + psp->sev_int_rcvd = 0; + + wait_event(psp->sev_int_queue, psp->sev_int_rcvd); + *reg = ioread32(psp->io_regs + PSP_CMDRESP); + + return 0; +} + +static int sev_wait_cmd(struct psp_device *psp, unsigned int *reg) +{ + return (*reg & PSP_CMDRESP_IOC) ? sev_wait_cmd_ioc(psp, reg) + : sev_wait_cmd_poll(psp, 10, reg); +} + +static int sev_cmd_buffer_len(int cmd) +{ + switch (cmd) { + case SEV_CMD_INIT: + return sizeof(struct sev_data_init); + case SEV_CMD_PLATFORM_STATUS: + return sizeof(struct sev_data_status); + case SEV_CMD_PEK_CSR: + return sizeof(struct sev_data_pek_csr); + case SEV_CMD_PEK_CERT_IMPORT: + return sizeof(struct sev_data_pek_cert_import); + case SEV_CMD_PDH_CERT_EXPORT: + return sizeof(struct sev_data_pdh_cert_export); + case SEV_CMD_LAUNCH_START: + return sizeof(struct sev_data_launch_start); + case SEV_CMD_LAUNCH_UPDATE_DATA: + return sizeof(struct sev_data_launch_update_data); + case SEV_CMD_LAUNCH_UPDATE_VMSA: + return sizeof(struct sev_data_launch_update_vmsa); + case SEV_CMD_LAUNCH_FINISH: + return sizeof(struct sev_data_launch_finish); + case SEV_CMD_LAUNCH_UPDATE_SECRET: + return sizeof(struct sev_data_launch_secret); + case SEV_CMD_LAUNCH_MEASURE: + return sizeof(struct sev_data_launch_measure); + case SEV_CMD_ACTIVATE: + return sizeof(struct sev_data_activate); + case SEV_CMD_DEACTIVATE: + return sizeof(struct sev_data_deactivate); + case SEV_CMD_DECOMMISSION: + return sizeof(struct sev_data_decommission); + case SEV_CMD_GUEST_STATUS: + return sizeof(struct sev_data_guest_status); + case SEV_CMD_DBG_DECRYPT: + case SEV_CMD_DBG_ENCRYPT: + return sizeof(struct sev_data_dbg); + case SEV_CMD_SEND_START: + return sizeof(struct sev_data_send_start); + case SEV_CMD_SEND_UPDATE_DATA: + return sizeof(struct sev_data_send_update_data); + case SEV_CMD_SEND_UPDATE_VMSA: + return sizeof(struct sev_data_send_update_vmsa); + case SEV_CMD_SEND_FINISH: + return sizeof(struct sev_data_send_finish); + case SEV_CMD_RECEIVE_START: + return sizeof(struct sev_data_receive_start); + case SEV_CMD_RECEIVE_UPDATE_DATA: + return sizeof(struct sev_data_receive_update_data); + case SEV_CMD_RECEIVE_UPDATE_VMSA: + return sizeof(struct sev_data_receive_update_vmsa); + case SEV_CMD_RECEIVE_FINISH: + return sizeof(struct sev_data_receive_finish); + default: + return 0; + } + + return 0; +} + +static int sev_handle_cmd(int cmd, void *data, int *psp_ret) +{ + struct psp_device *psp = psp_get_master_device(); + unsigned int phys_lsb, phys_msb; + unsigned int reg, ret; + + if (!psp) + return -ENODEV; + + /* Set the physical address for the PSP */ + phys_lsb = data ? lower_32_bits(__psp_pa(data)) : 0; + phys_msb = data ? upper_32_bits(__psp_pa(data)) : 0; + + dev_dbg(psp->dev, "sev command id %#x buffer 0x%08x%08x\n", + cmd, phys_msb, phys_lsb); + print_hex_dump_debug("(in): ", DUMP_PREFIX_OFFSET, 16, 2, data, + sev_cmd_buffer_len(cmd), false); + + /* Only one command at a time... */ + mutex_lock(&sev_cmd_mutex); + + iowrite32(phys_lsb, psp->io_regs + PSP_CMDBUFF_ADDR_LO); + iowrite32(phys_msb, psp->io_regs + PSP_CMDBUFF_ADDR_HI); + + reg = cmd; + reg <<= PSP_CMDRESP_CMD_SHIFT; + reg |= sev_poll ? 0 : PSP_CMDRESP_IOC; + iowrite32(reg, psp->io_regs + PSP_CMDRESP); + + ret = sev_wait_cmd(psp, ®); + if (ret) + goto unlock; + + if (psp_ret) + *psp_ret = reg & PSP_CMDRESP_ERR_MASK; + + if (reg & PSP_CMDRESP_ERR_MASK) { + dev_dbg(psp->dev, "sev command %#x failed (%#010x)\n", + cmd, reg & PSP_CMDRESP_ERR_MASK); + ret = -EIO; + } + +unlock: + mutex_unlock(&sev_cmd_mutex); + print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, + sev_cmd_buffer_len(cmd), false); + return ret; +} + +static int sev_platform_get_state(int *state, int *error) +{ + struct sev_data_status *data; + int ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + ret = sev_handle_cmd(SEV_CMD_PLATFORM_STATUS, data, error); + if (!ret) + *state = data->state; + + kfree(data); + return ret; +} + +static int sev_firmware_init(int *error) +{ + struct sev_data_init *data; + int rc; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + rc = sev_handle_cmd(SEV_CMD_INIT, data, error); + + kfree(data); + return rc; +} + +static inline int sev_ioctl_factory_reset(struct sev_issue_cmd *argp) +{ + return sev_handle_cmd(SEV_CMD_FACTORY_RESET, 0, &argp->error); +} + +static int sev_ioctl_platform_status(struct sev_issue_cmd *argp) +{ + struct sev_data_status *data; + int ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + ret = sev_handle_cmd(SEV_CMD_PLATFORM_STATUS, data, &argp->error); + if (ret) + goto e_free; + + if (copy_to_user((void __user *)(uintptr_t) argp->data, + data, sizeof(*data))) { + ret = -EFAULT; + goto e_free; + } +e_free: + kfree(data); + return ret; +} + +static void *copy_user_blob(u64 __user uaddr, u32 len) +{ + void *data; + + if (!uaddr || !len) + return ERR_PTR(-EINVAL); + + /* verify that blob length does not exceed our limit */ + if (len > SEV_FW_BLOB_MAX_SIZE) + return ERR_PTR(-EINVAL); + + data = kmalloc(len, GFP_KERNEL); + if (IS_ERR(data)) + return ERR_PTR(-ENOMEM); + + if (copy_from_user(data, (void __user *)(uintptr_t)uaddr, len)) + goto e_free; + + return data; +e_free: + kfree(data); + return ERR_PTR(-EFAULT); +} + +static int sev_ioctl_pek_csr(struct sev_issue_cmd *argp) +{ + struct sev_user_data_pek_csr input; + struct sev_data_pek_csr *data; + int do_shutdown = 0; + int ret, state; + void *blob; + + if (copy_from_user(&input, (void __user *)(uintptr_t)argp->data, + sizeof(struct sev_user_data_pek_csr))) + return -EFAULT; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + + /* copy the PEK certificate blob from userspace */ + blob = NULL; + if (input.address) { + blob = copy_user_blob(input.address, input.length); + if (IS_ERR(blob)) { + ret = PTR_ERR(blob); + goto e_free; + } + + data->address = __psp_pa(blob); + data->len = input.length; + } + + ret = sev_platform_get_state(&state, &argp->error); + if (ret) + goto e_free_blob; + + /* + * PEK_CERT command can be issued only when we are in INIT state. + * if current state is WORKING then reject it, if state is UNINIT + * then transition the platform to INIT state before issuing the + * command. + */ + if (state == SEV_STATE_WORKING) { + ret = -EBUSY; + goto e_free_blob; + } else if (state == SEV_STATE_UNINIT) { + ret = sev_firmware_init(&argp->error); + if (ret) + goto e_free_blob; + do_shutdown = 1; + } + + ret = sev_handle_cmd(SEV_CMD_PEK_CSR, data, &argp->error); + + input.length = data->len; + + if (copy_to_user((void __user *)(uintptr_t)argp->data, &input, + sizeof(struct sev_user_data_pek_csr))) + ret = -EFAULT; + + /* transition the plaform into INIT state */ + if (do_shutdown) + sev_handle_cmd(SEV_CMD_SHUTDOWN, 0, NULL); + +e_free_blob: + kfree(blob); +e_free: + kfree(data); + return ret; +} + +static int sev_ioctl_pdh_gen(struct sev_issue_cmd *argp) +{ + int ret, state, do_shutdown = 0; + + /* + * PDH_GEN command can be issued when platform is in INIT or WORKING + * state. If we are in UNINIT state then transition into INIT state + * before issuing the command. + */ + ret = sev_platform_get_state(&state, &argp->error); + if (ret) + return ret; + + if (state == SEV_STATE_UNINIT) { + /* transition the plaform into INIT state */ + ret = sev_firmware_init(&argp->error); + if (ret) + return ret; + do_shutdown = 1; + } + + ret = sev_handle_cmd(SEV_CMD_PDH_GEN, 0, &argp->error); + + if (do_shutdown) + sev_handle_cmd(SEV_CMD_SHUTDOWN, 0, NULL); + + return ret; +} + +static int sev_ioctl_pek_gen(struct sev_issue_cmd *argp) +{ + int do_shutdown = 0; + int ret, state; + + /* + * PEK_GEN command can be issued only when firmware is in INIT state. + * If firmware is in UNINIT state then we transition it into INIT state + * and issue the command. + */ + ret = sev_platform_get_state(&state, &argp->error); + if (ret) + return ret; + + if (state == SEV_STATE_WORKING) { + return -EBUSY; + } else if (state == SEV_STATE_UNINIT) { + /* transition the plaform into INIT state */ + ret = sev_firmware_init(&argp->error); + if (ret) + return ret; + + do_shutdown = 1; + } + + ret = sev_handle_cmd(SEV_CMD_PEK_GEN, 0, &argp->error); + + if (do_shutdown) + sev_handle_cmd(SEV_CMD_SHUTDOWN, 0, NULL); + + return ret; +} + +static int sev_ioctl_pek_cert_import(struct sev_issue_cmd *argp) +{ + struct sev_user_data_pek_cert_import input; + struct sev_data_pek_cert_import *data; + int ret, state, do_shutdown = 0; + void *pek_blob, *oca_blob; + + if (copy_from_user(&input, (void __user *)(uintptr_t) argp->data, + sizeof(struct sev_user_data_pek_cert_import))) + return -EFAULT; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + /* copy PEK certificate blobs from userspace */ + pek_blob = copy_user_blob(input.pek_cert_address, input.pek_cert_len); + if (IS_ERR(pek_blob)) { + ret = PTR_ERR(pek_blob); + goto e_free; + } + + data->pek_cert_address = __psp_pa(pek_blob); + data->pek_cert_len = input.pek_cert_len; + + /* copy PEK certificate blobs from userspace */ + oca_blob = copy_user_blob(input.oca_cert_address, input.oca_cert_len); + if (IS_ERR(oca_blob)) { + ret = PTR_ERR(oca_blob); + goto e_free_pek; + } + + data->oca_cert_address = __psp_pa(oca_blob); + data->oca_cert_len = input.oca_cert_len; + + ret = sev_platform_get_state(&state, &argp->error); + if (ret) + goto e_free_oca; + + /* + * PEK_CERT_IMPORT command can be issued only when platform is in INIT + * state. If we are in UNINIT state then transition into INIT state + * before issuing the command. + */ + if (state == SEV_STATE_WORKING) { + ret = -EBUSY; + goto e_free_oca; + } else if (state == SEV_STATE_UNINIT) { + /* transition platform init INIT state */ + ret = sev_firmware_init(&argp->error); + if (ret) + goto e_free_oca; + do_shutdown = 1; + } + + ret = sev_handle_cmd(SEV_CMD_PEK_CERT_IMPORT, data, &argp->error); + + if (do_shutdown) + sev_handle_cmd(SEV_CMD_SHUTDOWN, 0, NULL); +e_free_oca: + kfree(oca_blob); +e_free_pek: + kfree(pek_blob); +e_free: + kfree(data); + return ret; +} + +static int sev_ioctl_pdh_cert_export(struct sev_issue_cmd *argp) +{ + struct sev_user_data_pdh_cert_export input; + struct sev_data_pdh_cert_export *data; + int ret, state, need_shutdown = 0; + void *pdh_blob, *cert_blob; + + if (copy_from_user(&input, (void __user *)(uintptr_t)argp->data, + sizeof(struct sev_user_data_pdh_cert_export))) + return -EFAULT; + + data = kzalloc(sizeof(*data), GFP_KERNEL); + if (!data) + return -ENOMEM; + + pdh_blob = NULL; + if (input.pdh_cert_address) { + if (input.pdh_cert_len > SEV_FW_BLOB_MAX_SIZE) { + ret = -EINVAL; + goto e_free; + } + + pdh_blob = kmalloc(input.pdh_cert_len, GFP_KERNEL); + if (!pdh_blob) { + ret = -ENOMEM; + goto e_free; + } + + data->pdh_cert_address = __psp_pa(pdh_blob); + data->pdh_cert_len = input.pdh_cert_len; + } + + cert_blob = NULL; + if (input.cert_chain_address) { + if (input.cert_chain_len > SEV_FW_BLOB_MAX_SIZE) { + ret = -EINVAL; + goto e_free_pdh; + } + + cert_blob = kmalloc(input.cert_chain_len, GFP_KERNEL); + if (!cert_blob) { + ret = -ENOMEM; + goto e_free_pdh; + } + + data->cert_chain_address = __psp_pa(cert_blob); + data->cert_chain_len = input.cert_chain_len; + } + + ret = sev_platform_get_state(&state, &argp->error); + if (ret) + goto e_free_cert; + + /* + * CERT_EXPORT command can be issued in INIT or WORKING state. + * If we are in UNINIT state then transition into INIT state and + * shutdown before exiting. But if platform is in WORKING state + * then EXPORT the certificate but do not shutdown the platform. + */ + if (state == SEV_STATE_UNINIT) { + ret = sev_firmware_init(&argp->error); + if (ret) + goto e_free_cert; + + need_shutdown = 1; + } + + ret = sev_handle_cmd(SEV_CMD_PDH_CERT_EXPORT, data, &argp->error); + + input.cert_chain_len = data->cert_chain_len; + input.pdh_cert_len = data->pdh_cert_len; + + /* copy certificate length to userspace */ + if (copy_to_user((void __user *)(uintptr_t)argp->data, &input, + sizeof(struct sev_user_data_pdh_cert_export))) + ret = -EFAULT; + + if (ret) + goto e_shutdown; + + /* copy PDH certificate to userspace */ + if (pdh_blob && + copy_to_user((void __user *)(uintptr_t)input.pdh_cert_address, + pdh_blob, input.pdh_cert_len)) { + ret = -EFAULT; + goto e_shutdown; + } + + /* copy certificate chain to userspace */ + if (cert_blob && + copy_to_user((void __user *)(uintptr_t)input.cert_chain_address, + cert_blob, input.cert_chain_len)) { + ret = -EFAULT; + goto e_shutdown; + } + +e_shutdown: + if (need_shutdown) + sev_handle_cmd(SEV_CMD_SHUTDOWN, 0, NULL); +e_free_cert: + kfree(cert_blob); +e_free_pdh: + kfree(pdh_blob); +e_free: + kfree(data); + return ret; +} + +static long sev_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) +{ + void __user *argp = (void __user *)arg; + struct sev_issue_cmd input; + int ret = -EFAULT; + + if (ioctl != SEV_ISSUE_CMD) + return -EINVAL; + + if (copy_from_user(&input, argp, sizeof(struct sev_issue_cmd))) + return -EFAULT; + + if (input.cmd > SEV_CMD_MAX) + return -EINVAL; + + switch (input.cmd) { + + case SEV_USER_CMD_FACTORY_RESET: { + ret = sev_ioctl_factory_reset(&input); + break; + } + case SEV_USER_CMD_PLATFORM_STATUS: { + ret = sev_ioctl_platform_status(&input); + break; + } + case SEV_USER_CMD_PEK_GEN: { + ret = sev_ioctl_pek_gen(&input); + break; + } + case SEV_USER_CMD_PDH_GEN: { + ret = sev_ioctl_pdh_gen(&input); + break; + } + case SEV_USER_CMD_PEK_CSR: { + ret = sev_ioctl_pek_csr(&input); + break; + } + case SEV_USER_CMD_PEK_CERT_IMPORT: { + ret = sev_ioctl_pek_cert_import(&input); + break; + } + case SEV_USER_CMD_PDH_CERT_EXPORT: { + ret = sev_ioctl_pdh_cert_export(&input); + break; + } + default: + ret = -EINVAL; + break; + } + + if (copy_to_user(argp, &input, sizeof(struct sev_issue_cmd))) + ret = -EFAULT; + + return ret; +} + + +const struct file_operations sev_fops = { + .owner = THIS_MODULE, + .unlocked_ioctl = sev_ioctl, +}; + +int sev_platform_init(struct sev_data_init *data, int *error) +{ + return sev_handle_cmd(SEV_CMD_INIT, data, error); +} +EXPORT_SYMBOL_GPL(sev_platform_init); + +int sev_platform_shutdown(int *error) +{ + return sev_handle_cmd(SEV_CMD_SHUTDOWN, 0, error); +} +EXPORT_SYMBOL_GPL(sev_platform_shutdown); + +int sev_platform_status(struct sev_data_status *data, int *error) +{ + return sev_handle_cmd(SEV_CMD_PLATFORM_STATUS, data, error); +} +EXPORT_SYMBOL_GPL(sev_platform_status); + +int sev_issue_cmd_external_user(struct file *filep, unsigned int cmd, + void *data, int *error) +{ + if (!filep || filep->f_op != &sev_fops) + return -EBADF; + + return sev_handle_cmd(cmd, data, error); +} +EXPORT_SYMBOL_GPL(sev_issue_cmd_external_user); + +int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) +{ + return sev_handle_cmd(SEV_CMD_DEACTIVATE, data, error); +} +EXPORT_SYMBOL_GPL(sev_guest_deactivate); + +int sev_guest_activate(struct sev_data_activate *data, int *error) +{ + return sev_handle_cmd(SEV_CMD_ACTIVATE, data, error); +} +EXPORT_SYMBOL_GPL(sev_guest_activate); + +int sev_guest_decommission(struct sev_data_decommission *data, int *error) +{ + return sev_handle_cmd(SEV_CMD_DECOMMISSION, data, error); +} +EXPORT_SYMBOL_GPL(sev_guest_decommission); + +int sev_guest_df_flush(int *error) +{ + return sev_handle_cmd(SEV_CMD_DF_FLUSH, 0, error); +} +EXPORT_SYMBOL_GPL(sev_guest_df_flush); + +static int sev_ops_init(struct psp_device *psp) +{ + int ret = 0; + struct miscdevice *misc = &psp->sev_misc; + + /* + * SEV feature support can be detected on the multiple devices but the + * SEV FW commands must be issued on the master. During probe time we + * do not know the master hence we create /dev/sev on the first device + * probe. sev_handle_cmd() finds the right master device to send the + * command. + */ + if (!sev_fops_registered) { + misc->minor = MISC_DYNAMIC_MINOR; + misc->name = DEVICE_NAME; + misc->fops = &sev_fops; + + ret = misc_register(misc); + if (!ret) { + sev_fops_registered = true; + psp->has_sev_fops = true; + init_waitqueue_head(&psp->sev_int_queue); + } + } + + return ret; +} + +static int sev_init(struct psp_device *psp) +{ + /* Check if device supports SEV feature */ + if (!(ioread32(psp->io_regs + PSP_FEATURE_REG) & 1)) { + dev_dbg(psp->dev, "device does not support SEV\n"); + return 1; + } + + return sev_ops_init(psp); +} + +static void sev_exit(struct psp_device *psp) +{ + if (psp->has_sev_fops) + misc_deregister(&psp->sev_misc); +} + int psp_dev_init(struct sp_device *sp) { struct device *dev = sp->dev; @@ -87,14 +821,20 @@ int psp_dev_init(struct sp_device *sp) sp_set_psp_master(sp); + ret = sev_init(psp); + if (ret) + goto e_irq; + /* Enable interrupt */ dev_dbg(dev, "Enabling interrupts ...\n"); - iowrite32(7, psp->io_regs + PSP_P2CMSG_INTEN); + iowrite32(-1, psp->io_regs + PSP_P2CMSG_INTEN); dev_notice(dev, "psp enabled\n"); return 0; +e_irq: + sp_free_psp_irq(psp->sp, psp); e_err: sp->psp_data = NULL; @@ -107,5 +847,6 @@ void psp_dev_destroy(struct sp_device *sp) { struct psp_device *psp = sp->psp_data; + sev_exit(psp); sp_free_psp_irq(sp, psp); } diff --git a/drivers/crypto/ccp/psp-dev.h b/drivers/crypto/ccp/psp-dev.h index d53970ef5960..51d3cd966eed 100644 --- a/drivers/crypto/ccp/psp-dev.h +++ b/drivers/crypto/ccp/psp-dev.h @@ -25,9 +25,21 @@ #include #include #include +#include +#include #include "sp-dev.h" +#define PSP_C2PMSG(_num) ((_num) << 2) +#define PSP_CMDRESP PSP_C2PMSG(32) +#define PSP_CMDBUFF_ADDR_LO PSP_C2PMSG(56) +#define PSP_CMDBUFF_ADDR_HI PSP_C2PMSG(57) +#define PSP_FEATURE_REG PSP_C2PMSG(63) + +#define PSP_P2CMSG(_num) (_num << 2) +#define PSP_CMD_COMPLETE_REG 1 +#define PSP_CMD_COMPLETE PSP_P2CMSG(PSP_CMD_COMPLETE_REG) + #define PSP_P2CMSG_INTEN 0x0110 #define PSP_P2CMSG_INTSTS 0x0114 @@ -54,6 +66,11 @@ struct psp_device { struct sp_device *sp; void __iomem *io_regs; + + unsigned int sev_int_rcvd; + wait_queue_head_t sev_int_queue; + bool has_sev_fops; + struct miscdevice sev_misc; }; extern const struct psp_vdata psp_entry; diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index d40709b65b18..70ba2dcdba04 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -509,4 +509,175 @@ struct __attribute__((__packed__)) sev_data_dbg { u32 len; /* In */ }; +#if defined(CONFIG_CRYPTO_DEV_SP_PSP) + +/** + * sev_platform_init - perform SEV INIT command + * + * @init: sev_data_init structure to be processed + * @error: SEV command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + */ +int sev_platform_init(struct sev_data_init *init, int *error); + +/** + * sev_platform_shutdown - perform SEV SHUTDOWN command + * + * @error: SEV command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + */ +int sev_platform_shutdown(int *error); + +/** + * sev_platform_status - perform SEV PLATFORM_STATUS command + * + * @init: sev_data_status structure to be processed + * @error: SEV command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + */ +int sev_platform_status(struct sev_data_status *status, int *error); +/** + * sev_issue_cmd_external_user - issue SEV command by other driver with a file handle. + * + * The function can be used by other drivers to issue a SEV command on + * behalf by userspace. The caller must pass a valid SEV file descriptor + * so that we know that caller has access to SEV device. + * + * @filep - SEV device file pointer + * @cmd - command to issue + * @data - command buffer + * @error: SEV command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + * -%EINVAL if the SEV file descriptor is not valid + */ +int sev_issue_cmd_external_user(struct file *filep, unsigned int id, + void *data, int *error); + +/** + * sev_guest_deactivate - perform SEV DEACTIVATE command + * + * @deactivate: sev_data_deactivate structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_deactivate(struct sev_data_deactivate *data, int *error); + +/** + * sev_guest_activate - perform SEV ACTIVATE command + * + * @activate: sev_data_activate structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_activate(struct sev_data_activate *data, int *error); + +/** + * sev_guest_df_flush - perform SEV DF_FLUSH command + * + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_df_flush(int *error); + +/** + * sev_guest_decommission - perform SEV DECOMMISSION command + * + * @decommission: sev_data_decommission structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int sev_guest_decommission(struct sev_data_decommission *data, int *error); + +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ + +static inline int sev_platform_status(struct sev_data_status *status, + int *error) +{ + return -ENODEV; +} + +static inline int sev_platform_init(struct sev_data_init *init, int *error) +{ + return -ENODEV; +} + +static inline int sev_platform_shutdown(int *error) { return -ENODEV; } + + +static inline int sev_issue_cmd_external_user(int fd, unsigned int id, + void *data, int *error) +{ + return -ENODEV; +} + +static inline int sev_guest_deactivate(struct sev_data_deactivate *data, + int *error) +{ + return -ENODEV; +} + +static inline int sev_guest_decommission(struct sev_data_decommission *data, + int *error) +{ + return -ENODEV; +} + +static inline int sev_guest_activate(struct sev_data_activate *data, + int *error) +{ + return -ENODEV; +} + +static inline int sev_guest_df_flush(int *error) { return -ENODEV; } + +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ + #endif /* __PSP_SEV_H__ */ diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h new file mode 100644 index 000000000000..885d9940786a --- /dev/null +++ b/include/uapi/linux/psp-sev.h @@ -0,0 +1,114 @@ + +/* + * Userspace interface for AMD Secure Encrypted Virtualization (SEV) + * + * Copyright (C) 2016-2017 Advanced Micro Devices, Inc. + * + * Author: Brijesh Singh + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#ifndef __PSP_SEV_USER_H__ +#define __PSP_SEV_USER_H__ + +#include + +/** + * SEV platform commands + */ +enum { + SEV_USER_CMD_FACTORY_RESET = 0, + SEV_USER_CMD_PLATFORM_STATUS, + SEV_USER_CMD_PEK_GEN, + SEV_USER_CMD_PEK_CSR, + SEV_USER_CMD_PDH_GEN, + SEV_USER_CMD_PDH_CERT_EXPORT, + SEV_USER_CMD_PEK_CERT_IMPORT, + + SEV_USER_CMD_MAX, +}; + +/** + * struct sev_user_data_status - PLATFORM_STATUS command parameters + * + * @major: major API version + * @minor: minor API version + * @state: platform state + * @owner: self-owned or externally owned + * @config: platform config flags + * @build: firmware build id for API version + * @guest_count: number of active guests + */ +struct sev_user_data_status { + __u8 api_major; /* Out */ + __u8 api_minor; /* Out */ + __u8 state; /* Out */ + __u8 owner : 1; /* Out */ + __u8 reserved1 : 7; + __u32 config : 1; /* Out */ + __u32 reserved2 : 23; + __u32 build : 8; /* Out */ + __u32 guest_count; /* Out */ +} __attribute__ ((__packed__)); + +/** + * struct sev_user_data_pek_csr - PEK_CSR command parameters + * + * @address: PEK certificate chain + * @length: length of certificate + */ +struct sev_user_data_pek_csr { + __u64 address; /* In */ + __u32 length; /* In/Out */ +}; + +/** + * struct sev_user_data_cert_import - PEK_CERT_IMPORT command parameters + * + * @pek_address: PEK certificate chain + * @pek_len: length of PEK certificate + * @oca_address: OCA certificate chain + * @oca_len: length of OCA certificate + */ +struct sev_user_data_pek_cert_import { + __u64 pek_cert_address; /* In */ + __u32 pek_cert_len; /* In */ + __u64 oca_cert_address; /* In */ + __u32 oca_cert_len; /* In */ +}; + +/** + * struct sev_user_data_pdh_cert_export - PDH_CERT_EXPORT command parameters + * + * @pdh_address: PDH certificate address + * @pdh_len: length of PDH certificate + * @cert_chain_address: PDH certificate chain + * @cert_chain_len: length of PDH certificate chain + */ +struct sev_user_data_pdh_cert_export { + __u64 pdh_cert_address; /* In */ + __u32 pdh_cert_len; /* In/Out */ + __u64 cert_chain_address; /* In */ + __u32 cert_chain_len; /* In/Out */ +}; + +/** + * struct sev_issue_cmd - SEV ioctl parameters + * + * @cmd: SEV commands to execute + * @opaque: pointer to the command structure + * @error: SEV FW return code on failure + */ +struct sev_issue_cmd { + __u32 cmd; /* In */ + __u64 data; /* In */ + __u32 error; /* Out */ +}; + +#define SEV_IOC_TYPE 'S' +#define SEV_ISSUE_CMD _IOWR(SEV_IOC_TYPE, 0x0, struct sev_issue_cmd) + +#endif /* __PSP_USER_SEV_H */