From patchwork Fri Oct 6 23:09:28 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 9990701 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 23A1060247 for ; Fri, 6 Oct 2017 23:09:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1421228C58 for ; Fri, 6 Oct 2017 23:09:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0877F28C7F; Fri, 6 Oct 2017 23:09:48 +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 99C0328C58 for ; Fri, 6 Oct 2017 23:09:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752845AbdJFXJp (ORCPT ); Fri, 6 Oct 2017 19:09:45 -0400 Received: from mail-sn1nam01on0084.outbound.protection.outlook.com ([104.47.32.84]:31662 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752842AbdJFXJn (ORCPT ); Fri, 6 Oct 2017 19:09:43 -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=eT0xkRQv+jViv45LsqWOb3xMrj35Ok0ypDydO8ZCvtI=; b=N2rnag5J44WjsVgOVJUwVnZqwncYYshtAr0a3g23mJX/UBvjJ8BoLGxCKHbgSvHGNgZ8XS3hk3B1bfy8/H0JfLS+veTjPwia1V5CIAAqMvw/L1tgf+RaEuLoH9Z3DZg1BAecJKs3LFcVQBiz/TGTGqEoxhee1oKbDPLz1uFNAAU= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=brijesh.singh@amd.com; Received: from ubuntu-010236106000.amd.com (165.204.78.1) by DM2PR12MB0156.namprd12.prod.outlook.com (2a01:111:e400:50ce::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7; Fri, 6 Oct 2017 23:09:39 +0000 From: Brijesh Singh To: bp@suse.de Cc: Brijesh Singh , Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Herbert Xu , Gary Hook , Tom Lendacky , linux-crypto@vger.kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [Part2 PATCH v5.1 10/31] crypto: ccp: Add Platform Security Processor (PSP) device support Date: Fri, 6 Oct 2017 18:09:28 -0500 Message-Id: <20171006230928.77740-1-brijesh.singh@amd.com> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20171004131412.13038-11-brijesh.singh@amd.com> References: <20171004131412.13038-11-brijesh.singh@amd.com> MIME-Version: 1.0 X-Originating-IP: [165.204.78.1] X-ClientProxiedBy: BN6PR16CA0011.namprd16.prod.outlook.com (2603:10b6:404:f5::21) To DM2PR12MB0156.namprd12.prod.outlook.com (2a01:111:e400:50ce::19) X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9272d9c7-c26c-4b27-c6dd-08d50d0f5252 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254152)(48565401081)(2017052603199)(201703131423075)(201703031133081)(201702281549075); SRVR:DM2PR12MB0156; X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0156; 3:SJcauQwYFMDUjMlCkZKMdQAzIg4a+NFIqDTNQQjgrrs6i4ZudoJkrUSJcNkhgHozxWM+YMUA/29O6sydufje16YF/iE4Q8damHJgiQI/wgwflQJH6ULtNio/IvwgTWWCGXLvJioyjWMJo0MvqeOJcO2DeQDYVSju15g8xw7wFK6/V0SXKSgfT4jTFWrUro2FijKBssR2llTZve2JdVQVyBAO2AKxhYc+ju2y0XvAed11VZMQvrntQrT4fLYuAzCI; 25:yCoJayOKm+6lmbNp3R9vUPP/EJQ/R934DO9URa9p/LV270+n0qLdo1mksuQmAn518NFb/pUSNk/sBiThnJj1Ub9dVHkwjJvwgwD9bthS/dYQeHnYvkEKtgpsMMnfATXHccWGJm6PM32o/JBB42L1Pifv3yaNYVE8dMsSXRjhK2gD4oiAVtNBfplh87tEvdO3KXiW0K1knNGVPqIuj28Sw0gmC6+SHl9W54eeumvdxIv2qA0Re2BjpD3e4sZ2wIZGs1vfingbyRb47ZgCor2UWQYbSAHlldq5qa8De+Ny4lucsV6X7dvbxuoQtlSkUqLf0bF5zfk5R0+BZEPudB2nZQ==; 31:GSPFuVQCYQNi7DIHYyh/5k9knYlfreugFb7RuxotAWtne4TBhc2Ns9ZVgJAM8VW/1hyeinQhl/bvruqdmxK4l6MKsgW5m79So41P0TXEF6DJRI6ZDVA7LwLUiTfcNrtLS4eUnqj6O4AuhKvXpvXlkw0z+f4J/7Ntdg1oSwInsXJLoRoYqpqIvcc7q6WMUZlqFxZYIjb0Nt3GusWWQfhxt77tgxuCme9bntNdce6baW8= X-MS-TrafficTypeDiagnostic: DM2PR12MB0156: X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0156; 20:GJIDtEcuuUhv8QPUS8jeOX6vTaO0ZP8xGfKMwNI2HEB/1p8LTsS8gGFiE2ZpEaTaLailg1Tgelxaz/c3wugyRPI9/hD1w58/aq223YQUHh/UcnlAqwfSj/gayWlKCeUSkcH4t+347pTyZDL11zXxsVfUX7DPbCXyKAW366mdt4g0DNvYEbJ9bsgp0Ff8dLAgFj6RG1qshvKaI71ign9lkuokuj8mb567qg8QFf4l3EFKFqb/zw2LskjxNLcRWigSXS2L3acm3wHOfVWXoHFrQ6aUlxS1zVf9nmveN27anA3vVgRdQ5tepX0Uxy2FE4xxihw7RjIYYROW4Qi0b/HDAvuTTcnGrdYTB4dGvnsW6Uvn8JeF/jqLP8R4TQMqz8JAFdq4/c3aJ4YvX/xntc76LvSqEeYIEeV6cDiLKR+/BJMJbByjHFTZH2aoHYBSeNxB6TdXUNLUN0Yct8sXAIUBtT6hMWA47BJF8kFjBslgdo58wirBVZdunY0vk/4cAJUF; 4:2YkAenc9bykF32FhP0xodpiZ6JeP3rev52n4ufd2z0pY3v6c4bGmEPuQVuWy6abxs7rMmI3HXMaIj/K3Gzo/dXwHj2ODYxqJWFx1xLWaShpgUfgMYQIJC1eMeQIArK51SCow9nCFvoJL0mA0yo2cvBeGOfHNS7ATY6fO+zbasVLRkoOhqUL04D5KjpboLZD53TykxRoDyP/EMyBBTSKWICRFSjt+XTBaOxt651fH0BMB78AJ5AyZD9vOHizLjhFx/Fh1TZnH/tzwCM0+JivQiKBd/UWmwZv2eb650GSxVdfT6CuEQofZ7Wega20l5GgSn0h190C0sAXqKiWFHr4h9sljydlU0K8oeS+FJT7XLQA= X-Exchange-Antispam-Report-Test: UriScan:(192374486261705)(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)(3002001)(10201501046)(100000703101)(100105400095)(93006095)(93001095)(6055026)(6041248)(20161123560025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(20161123562025)(20161123558100)(6072148)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:DM2PR12MB0156; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:DM2PR12MB0156; X-Forefront-PRVS: 0452022BE1 X-Forefront-Antispam-Report: SFV:NSPM; SFS:(10009020)(6009001)(346002)(376002)(189002)(199003)(305945005)(86362001)(316002)(97736004)(81156014)(2950100002)(23676002)(6916009)(81166006)(50466002)(3846002)(33646002)(6116002)(1076002)(54906003)(7736002)(2870700001)(2906002)(5660300001)(16526018)(8676002)(47776003)(53416004)(189998001)(6666003)(66066001)(36756003)(478600001)(15650500001)(106356001)(25786009)(50226002)(105586002)(76176999)(4326008)(68736007)(8936002)(53936002)(6486002)(2361001)(2351001)(101416001)(50986999)(2004002); DIR:OUT; SFP:1101; SCL:1; SRVR:DM2PR12MB0156; H:ubuntu-010236106000.amd.com; 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?MTtETTJQUjEyTUIwMTU2OzIzOmp0M3JpWVRtOXRvMWd6d2s4UXdSRHk1UUEw?= =?utf-8?B?bFRZbklzMVFkTStZS2ZZdkVjS1pyNHFlSTJDOHJUWlV4VUJzb2RrbHRKbkF1?= =?utf-8?B?L1hhVnB6bm5TdnpRb3o1MXUvZ1V4SjV3OFVSU3cwc0VuN0JwanN4UGpaQll1?= =?utf-8?B?ck5zTnFwUGx2VGFlK1lUZ3Mrd0N3N09nRjdPeWNDWlZwOVNWZUlyZkczSlZM?= =?utf-8?B?TWZzS0dGajQxK0pvSmJkUmlRVy81ek9LSlhxYUk1S3doMnlQa0dDdmZIblpw?= =?utf-8?B?aWNrZ3lqYktqMmhwOXU5TG5LNnp3ekloU2VHRzBtU2hpYTZ4Qzhhb1VEa0JH?= =?utf-8?B?U2kwRjRzUDBlandzWDNMOC9sY0RJY2lLbm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B2cgAkUaDZq09+VPWoG9xvWJt0ACF8mYUnK7WQReV4v8U7MeyZOy6fGquM/y8zBrI8CRf7b4pOYM952wxYpUGUGJFdFHVrhTTGQ4hbdOuvEzK9YwByhrDQJ58Yq3iBHRxOlpTUbS+4= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1; DM2PR12MB0156; 20:LECaB+QSBAGX5c82tCQYq4M8WvYRp9u+FAiOkWpuQcvJV2rtwS/wpA6ARnpvKA9O/kZgFS3f5lm98cA4Aio3pmarAl8mcNnOvcfOFyH9v8oQKPC9iYvV+4h2JNNNBg8HliuHvP1TG3uUVUyEfJaOrOV1JEHrwP85IH7TzR1kuJJ9QLGeOINVi4YujdNsGQ4JEvZpDP30bL4F5tvd1gwK+PBRgDBlYh0Ln0g+AInT13Ltjir2hIr0qQnM/UxcE/4j X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Oct 2017 23:09:39.0245 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM2PR12MB0156 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 The Platform Security Processor (PSP) is part of the AMD Secure Processor (AMD-SP) functionality. The PSP is a dedicated processor that provides support for key management commands in Secure Encrypted Virtualization (SEV) mode, along with software-based Trusted Execution Environment (TEE) to enable third-party trusted applications. Note that the key management functionality provided by the SEV firmware can be used outside of the kvm-amd driver hence it doesn't need to depend on CONFIG_KVM_AMD. 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 Improvements-by: Borislav Petkov Signed-off-by: Brijesh Singh Reviewed-by: Borislav Petkov --- Changes since v5: * move psp_entry in sp-pci.c (based on Boris improvement patch) drivers/crypto/ccp/Kconfig | 11 +++++ drivers/crypto/ccp/Makefile | 1 + drivers/crypto/ccp/psp-dev.c | 105 +++++++++++++++++++++++++++++++++++++++++++ drivers/crypto/ccp/psp-dev.h | 59 ++++++++++++++++++++++++ drivers/crypto/ccp/sp-dev.c | 26 +++++++++++ drivers/crypto/ccp/sp-dev.h | 26 ++++++++++- drivers/crypto/ccp/sp-pci.c | 52 +++++++++++++++++++++ 7 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 drivers/crypto/ccp/psp-dev.c create mode 100644 drivers/crypto/ccp/psp-dev.h diff --git a/drivers/crypto/ccp/Kconfig b/drivers/crypto/ccp/Kconfig index 9c84f9838931..b9dfae47aefd 100644 --- a/drivers/crypto/ccp/Kconfig +++ b/drivers/crypto/ccp/Kconfig @@ -33,3 +33,14 @@ config CRYPTO_DEV_CCP_CRYPTO Support for using the cryptographic API with the AMD Cryptographic Coprocessor. This module supports offload of SHA and AES algorithms. If you choose 'M' here, this module will be called ccp_crypto. + +config CRYPTO_DEV_SP_PSP + bool "Platform Security Processor (PSP) device" + default y + depends on CRYPTO_DEV_CCP_DD && X86_64 + help + Provide support for the AMD Platform Security Processor (PSP). + The PSP is a dedicated processor that provides support for key + management commands in Secure Encrypted Virtualization (SEV) mode, + along with software-based Trusted Execution Environment (TEE) to + enable third-party trusted applications. diff --git a/drivers/crypto/ccp/Makefile b/drivers/crypto/ccp/Makefile index 57f8debfcfb3..008bae7e26ec 100644 --- a/drivers/crypto/ccp/Makefile +++ b/drivers/crypto/ccp/Makefile @@ -7,6 +7,7 @@ ccp-$(CONFIG_CRYPTO_DEV_SP_CCP) += ccp-dev.o \ ccp-dmaengine.o \ ccp-debugfs.o ccp-$(CONFIG_PCI) += sp-pci.o +ccp-$(CONFIG_CRYPTO_DEV_SP_PSP) += psp-dev.o obj-$(CONFIG_CRYPTO_DEV_CCP_CRYPTO) += ccp-crypto.o ccp-crypto-objs := ccp-crypto-main.o \ diff --git a/drivers/crypto/ccp/psp-dev.c b/drivers/crypto/ccp/psp-dev.c new file mode 100644 index 000000000000..b5789f878560 --- /dev/null +++ b/drivers/crypto/ccp/psp-dev.c @@ -0,0 +1,105 @@ +/* + * AMD Platform Security Processor (PSP) interface + * + * 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. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sp-dev.h" +#include "psp-dev.h" + +static struct psp_device *psp_alloc_struct(struct sp_device *sp) +{ + struct device *dev = sp->dev; + struct psp_device *psp; + + psp = devm_kzalloc(dev, sizeof(*psp), GFP_KERNEL); + if (!psp) + return NULL; + + psp->dev = dev; + psp->sp = sp; + + snprintf(psp->name, sizeof(psp->name), "psp-%u", sp->ord); + + return psp; +} + +static irqreturn_t psp_irq_handler(int irq, void *data) +{ + return IRQ_HANDLED; +} + +int psp_dev_init(struct sp_device *sp) +{ + struct device *dev = sp->dev; + struct psp_device *psp; + int ret; + + ret = -ENOMEM; + psp = psp_alloc_struct(sp); + if (!psp) + goto e_err; + + sp->psp_data = psp; + + psp->vdata = (struct psp_vdata *)sp->dev_vdata->psp_vdata; + if (!psp->vdata) { + ret = -ENODEV; + dev_err(dev, "missing driver data\n"); + goto e_err; + } + + psp->io_regs = sp->io_map + psp->vdata->offset; + + /* Disable and clear interrupts until ready */ + iowrite32(0, psp->io_regs + PSP_P2CMSG_INTEN); + iowrite32(-1, psp->io_regs + PSP_P2CMSG_INTSTS); + + /* Request an irq */ + ret = sp_request_psp_irq(psp->sp, psp_irq_handler, psp->name, psp); + if (ret) { + dev_err(dev, "psp: unable to allocate an IRQ\n"); + goto e_err; + } + + if (sp->set_psp_master_device) + sp->set_psp_master_device(sp); + + /* Enable interrupt */ + iowrite32(-1, psp->io_regs + PSP_P2CMSG_INTEN); + + return 0; + +e_err: + sp->psp_data = NULL; + + dev_notice(dev, "psp initialization failed\n"); + + return ret; +} + +void psp_dev_destroy(struct sp_device *sp) +{ + struct psp_device *psp = sp->psp_data; + + sp_free_psp_irq(sp, psp); +} diff --git a/drivers/crypto/ccp/psp-dev.h b/drivers/crypto/ccp/psp-dev.h new file mode 100644 index 000000000000..55b7808367c3 --- /dev/null +++ b/drivers/crypto/ccp/psp-dev.h @@ -0,0 +1,59 @@ +/* + * AMD Platform Security Processor (PSP) interface driver + * + * Copyright (C) 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_DEV_H__ +#define __PSP_DEV_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sp-dev.h" + +#define PSP_P2CMSG_INTEN 0x0110 +#define PSP_P2CMSG_INTSTS 0x0114 + +#define PSP_C2PMSG_ATTR_0 0x0118 +#define PSP_C2PMSG_ATTR_1 0x011c +#define PSP_C2PMSG_ATTR_2 0x0120 +#define PSP_C2PMSG_ATTR_3 0x0124 +#define PSP_P2CMSG_ATTR_0 0x0128 + +#define PSP_CMDRESP_CMD_SHIFT 16 +#define PSP_CMDRESP_IOC BIT(0) +#define PSP_CMDRESP_RESP BIT(31) +#define PSP_CMDRESP_ERR_MASK 0xffff + +#define MAX_PSP_NAME_LEN 16 + +struct psp_device { + struct list_head entry; + + struct psp_vdata *vdata; + char name[MAX_PSP_NAME_LEN]; + + struct device *dev; + struct sp_device *sp; + + void __iomem *io_regs; +}; + +#endif /* __PSP_DEV_H */ diff --git a/drivers/crypto/ccp/sp-dev.c b/drivers/crypto/ccp/sp-dev.c index bef387c8abfd..cf101c039c8f 100644 --- a/drivers/crypto/ccp/sp-dev.c +++ b/drivers/crypto/ccp/sp-dev.c @@ -198,6 +198,8 @@ int sp_init(struct sp_device *sp) if (sp->dev_vdata->ccp_vdata) ccp_dev_init(sp); + if (sp->dev_vdata->psp_vdata) + psp_dev_init(sp); return 0; } @@ -206,6 +208,9 @@ void sp_destroy(struct sp_device *sp) if (sp->dev_vdata->ccp_vdata) ccp_dev_destroy(sp); + if (sp->dev_vdata->psp_vdata) + psp_dev_destroy(sp); + sp_del_device(sp); } @@ -237,6 +242,27 @@ int sp_resume(struct sp_device *sp) } #endif +struct sp_device *sp_get_psp_master_device(void) +{ + struct sp_device *i, *ret = NULL; + unsigned long flags; + + write_lock_irqsave(&sp_unit_lock, flags); + if (list_empty(&sp_units)) + goto unlock; + + list_for_each_entry(i, &sp_units, entry) { + if (i->psp_data) + break; + } + + if (i->get_psp_master_device) + ret = i->get_psp_master_device(); +unlock: + write_unlock_irqrestore(&sp_unit_lock, flags); + return ret; +} + static int __init sp_mod_init(void) { #ifdef CONFIG_X86 diff --git a/drivers/crypto/ccp/sp-dev.h b/drivers/crypto/ccp/sp-dev.h index 5ab486ade1ad..761dba176168 100644 --- a/drivers/crypto/ccp/sp-dev.h +++ b/drivers/crypto/ccp/sp-dev.h @@ -42,12 +42,19 @@ struct ccp_vdata { const unsigned int offset; const unsigned int rsamax; }; + +struct psp_vdata { + const unsigned int version; + const struct psp_actions *perform; + const unsigned int offset; +}; + /* Structure to hold SP device data */ struct sp_dev_vdata { const unsigned int bar; const struct ccp_vdata *ccp_vdata; - void *psp_vdata; + const struct psp_vdata *psp_vdata; }; struct sp_device { @@ -68,6 +75,10 @@ struct sp_device { /* DMA caching attribute support */ unsigned int axcache; + /* get and set master device */ + struct sp_device*(*get_psp_master_device)(void); + void (*set_psp_master_device)(struct sp_device *); + bool irq_registered; bool use_tasklet; @@ -103,6 +114,7 @@ void sp_free_ccp_irq(struct sp_device *sp, void *data); int sp_request_psp_irq(struct sp_device *sp, irq_handler_t handler, const char *name, void *data); void sp_free_psp_irq(struct sp_device *sp, void *data); +struct sp_device *sp_get_psp_master_device(void); #ifdef CONFIG_CRYPTO_DEV_SP_CCP @@ -130,4 +142,16 @@ static inline int ccp_dev_resume(struct sp_device *sp) } #endif /* CONFIG_CRYPTO_DEV_SP_CCP */ +#ifdef CONFIG_CRYPTO_DEV_SP_PSP + +int psp_dev_init(struct sp_device *sp); +void psp_dev_destroy(struct sp_device *sp); + +#else /* !CONFIG_CRYPTO_DEV_SP_PSP */ + +static inline int psp_dev_init(struct sp_device *sp) { return 0; } +static inline void psp_dev_destroy(struct sp_device *sp) { } + +#endif /* CONFIG_CRYPTO_DEV_SP_PSP */ + #endif diff --git a/drivers/crypto/ccp/sp-pci.c b/drivers/crypto/ccp/sp-pci.c index 9859aa683a28..f5f43c50698a 100644 --- a/drivers/crypto/ccp/sp-pci.c +++ b/drivers/crypto/ccp/sp-pci.c @@ -25,6 +25,7 @@ #include #include "ccp-dev.h" +#include "psp-dev.h" #define MSIX_VECTORS 2 @@ -32,6 +33,7 @@ struct sp_pci { int msix_count; struct msix_entry msix_entry[MSIX_VECTORS]; }; +static struct sp_device *sp_dev_master; static int sp_get_msix_irqs(struct sp_device *sp) { @@ -108,6 +110,45 @@ static void sp_free_irqs(struct sp_device *sp) sp->psp_irq = 0; } +static bool sp_pci_is_master(struct sp_device *sp) +{ + struct device *dev_cur, *dev_new; + struct pci_dev *pdev_cur, *pdev_new; + + dev_new = sp->dev; + dev_cur = sp_dev_master->dev; + + pdev_new = to_pci_dev(dev_new); + pdev_cur = to_pci_dev(dev_cur); + + if (pdev_new->bus->number < pdev_cur->bus->number) + return true; + + if (PCI_SLOT(pdev_new->devfn) < PCI_SLOT(pdev_cur->devfn)) + return true; + + if (PCI_FUNC(pdev_new->devfn) < PCI_FUNC(pdev_cur->devfn)) + return true; + + return false; +} + +static void psp_set_master(struct sp_device *sp) +{ + if (!sp_dev_master) { + sp_dev_master = sp; + return; + } + + if (sp_pci_is_master(sp)) + sp_dev_master = sp; +} + +static struct sp_device *psp_get_master(void) +{ + return sp_dev_master; +} + static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) { struct sp_device *sp; @@ -166,6 +207,8 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) goto e_err; pci_set_master(pdev); + sp->set_psp_master_device = psp_set_master; + sp->get_psp_master_device = psp_get_master; ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(48)); if (ret) { @@ -225,6 +268,12 @@ static int sp_pci_resume(struct pci_dev *pdev) } #endif +#ifdef CONFIG_CRYPTO_DEV_SP_PSP +static const struct psp_vdata psp_entry = { + .offset = 0x10500, +}; +#endif + static const struct sp_dev_vdata dev_vdata[] = { { .bar = 2, @@ -237,6 +286,9 @@ static const struct sp_dev_vdata dev_vdata[] = { #ifdef CONFIG_CRYPTO_DEV_SP_CCP .ccp_vdata = &ccpv5a, #endif +#ifdef CONFIG_CRYPTO_DEV_SP_PSP + .psp_vdata = &psp_entry +#endif }, { .bar = 2,