Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp373515pxb; Thu, 26 Aug 2021 05:21:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyyf4smaOXg4ibR+wR3QSXq4V6bOrDMzy3itf5iiwF7qLjKe504iacgeZeOiWJjarfqUZbq X-Received: by 2002:a17:907:1dcf:: with SMTP id og15mr3914188ejc.470.1629980503435; Thu, 26 Aug 2021 05:21:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629980503; cv=none; d=google.com; s=arc-20160816; b=RqlGfoImVJuU++unKZwSKvDWQfqHPafD4kHVBnkKw3Y+fHuWqBfrfQKm35gN3j0acr vfarIzpFm0QMvaoylBIGSNvQmISH4lMcqfFtE6VvakF8WDmrTiZ5ceGDnXDG74BJOMZ4 SYcCvjT6RPkG6Ulr8wuvIJhlY7AZ8cGySFYdLJWksrANxJA6uxWvIrIEKXhVkqlGEbdz WI2F5mNJH6aj5bE5U0QL9+42tAdD/HRCAhe73nlT0hp8QAVBAQy9mKROex7sAiaX/CzZ lu8e5qh9KsRspd4vpVWzu9Mep7Z/fdVPHr+sEa/fVQ2mqN8vv+ZJjNVxTOTn9pDUHom6 fXSA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=jwjkPLCUD2lvumFEMAsZ2sThwCTFsfYB699Fn3hl1X4=; b=pGtJ7wzplB9j7aUvut6Gb0kb5+rLFjzqqB3rgvutz/Sm5fFNjUvo4zRM5X0mNlFFgH knfo4ArKkfmbS4UPrM5rh99xDOPDjJluUQiHcxro4PyCCc+4/GbtTGELqGyUBydgSFSp ibxVAP9ETLi8Hd4Il5lQelfd3Pqdv0PQ7Jw7f7mXrawjKvfonn+q7luLIAcnVTEeOq8j PaSMSMYuGBfE82HBC7fDkaRlJGaf+HNmvMHxpkjUc6tDyycGHhXzkDZOQPj75mIr1GWV mAi5QaWVvqzepLyJRszseU7C0ACCJj1IGv4IFZQsEa1KQd/IyHh1bZotCjXp1MAgfzRA ry+w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p12si3551646ejy.328.2021.08.26.05.21.07; Thu, 26 Aug 2021 05:21:43 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234453AbhHZMTz (ORCPT + 99 others); Thu, 26 Aug 2021 08:19:55 -0400 Received: from 212.199.177.27.static.012.net.il ([212.199.177.27]:41648 "EHLO herzl.nuvoton.co.il" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S242578AbhHZMTy (ORCPT ); Thu, 26 Aug 2021 08:19:54 -0400 Received: from taln60.nuvoton.co.il (ntil-fw [212.199.177.25]) by herzl.nuvoton.co.il (8.13.8/8.13.8) with ESMTP id 17QBJURj025241; Thu, 26 Aug 2021 14:19:30 +0300 Received: by taln60.nuvoton.co.il (Postfix, from userid 10140) id 1A58063A1C; Thu, 26 Aug 2021 14:19:40 +0300 (IDT) From: amirmizi6@gmail.com To: Eyal.Cohen@nuvoton.com, jarkko.sakkinen@linux.intel.com, oshrialkoby85@gmail.com, alexander.steffen@infineon.com, robh+dt@kernel.org, mark.rutland@arm.com, peterhuewe@gmx.de, jgg@ziepe.ca, arnd@arndb.de, gregkh@linuxfoundation.org, benoit.houyere@st.com, eajames@linux.ibm.com, joel@jms.id.au Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-integrity@vger.kernel.org, oshri.alkoby@nuvoton.com, tmaimon77@gmail.com, gcwilson@us.ibm.com, kgoldman@us.ibm.com, Dan.Morav@nuvoton.com, oren.tanami@nuvoton.com, shmulik.hager@nuvoton.com, amir.mizinski@nuvoton.com, Amir Mizinski , Alexander Steffen Subject: [PATCH v13 1/7] tpm: Make read{16, 32}() and write32() in tpm_tis_phy_ops optional Date: Thu, 26 Aug 2021 14:19:02 +0300 Message-Id: <20210826111908.117278-2-amirmizi6@gmail.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20210826111908.117278-1-amirmizi6@gmail.com> References: <20210826111908.117278-1-amirmizi6@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Amir Mizinski Only tpm_tis can use memory-mapped I/O, which is truly mapped into the kernel's memory space. Therefore, using ioread16/ioread32/iowrite32 turns into a straightforward pointer dereference. Every other driver requires more complicated operations to read more than one byte at a time and will just fall back to read_bytes/write_bytes. Therefore, move this common code out of tpm_tis_spi and into tpm_tis_core so that it is used automatically when low-level drivers do not implement the specialized methods. Co-developed-by: Alexander Steffen Signed-off-by: Alexander Steffen Signed-off-by: Amir Mizinski Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen --- drivers/char/tpm/tpm_tis_core.h | 38 +++++++++++++++++++++++++++++++--- drivers/char/tpm/tpm_tis_spi.h | 4 ---- drivers/char/tpm/tpm_tis_spi_cr50.c | 3 --- drivers/char/tpm/tpm_tis_spi_main.c | 41 ------------------------------------- 4 files changed, 35 insertions(+), 51 deletions(-) diff --git a/drivers/char/tpm/tpm_tis_core.h b/drivers/char/tpm/tpm_tis_core.h index b2a3c6c..e8fad0e 100644 --- a/drivers/char/tpm/tpm_tis_core.h +++ b/drivers/char/tpm/tpm_tis_core.h @@ -124,13 +124,35 @@ static inline int tpm_tis_read8(struct tpm_tis_data *data, u32 addr, u8 *result) static inline int tpm_tis_read16(struct tpm_tis_data *data, u32 addr, u16 *result) { - return data->phy_ops->read16(data, addr, result); + __le16 result_le; + int rc; + + if (data->phy_ops->read16) + return data->phy_ops->read16(data, addr, result); + + rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), + (u8 *)&result_le); + if (!rc) + *result = le16_to_cpu(result_le); + + return rc; } static inline int tpm_tis_read32(struct tpm_tis_data *data, u32 addr, u32 *result) { - return data->phy_ops->read32(data, addr, result); + __le32 result_le; + int rc; + + if (data->phy_ops->read32) + return data->phy_ops->read32(data, addr, result); + + rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), + (u8 *)&result_le); + if (!rc) + *result = le32_to_cpu(result_le); + + return rc; } static inline int tpm_tis_write_bytes(struct tpm_tis_data *data, u32 addr, @@ -147,7 +169,17 @@ static inline int tpm_tis_write8(struct tpm_tis_data *data, u32 addr, u8 value) static inline int tpm_tis_write32(struct tpm_tis_data *data, u32 addr, u32 value) { - return data->phy_ops->write32(data, addr, value); + __le32 value_le; + int rc; + + if (data->phy_ops->write32) + return data->phy_ops->write32(data, addr, value); + + value_le = cpu_to_le32(value); + rc = data->phy_ops->write_bytes(data, addr, sizeof(u32), + (u8 *)&value_le); + + return rc; } static inline bool is_bsw(void) diff --git a/drivers/char/tpm/tpm_tis_spi.h b/drivers/char/tpm/tpm_tis_spi.h index bba7397..d0f66f6 100644 --- a/drivers/char/tpm/tpm_tis_spi.h +++ b/drivers/char/tpm/tpm_tis_spi.h @@ -31,10 +31,6 @@ extern int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy, extern int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len, u8 *in, const u8 *out); -extern int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result); -extern int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result); -extern int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value); - #ifdef CONFIG_TCG_TIS_SPI_CR50 extern int cr50_spi_probe(struct spi_device *spi); #else diff --git a/drivers/char/tpm/tpm_tis_spi_cr50.c b/drivers/char/tpm/tpm_tis_spi_cr50.c index ea759af..9af6f84 100644 --- a/drivers/char/tpm/tpm_tis_spi_cr50.c +++ b/drivers/char/tpm/tpm_tis_spi_cr50.c @@ -220,9 +220,6 @@ static int tpm_tis_spi_cr50_write_bytes(struct tpm_tis_data *data, u32 addr, static const struct tpm_tis_phy_ops tpm_spi_cr50_phy_ops = { .read_bytes = tpm_tis_spi_cr50_read_bytes, .write_bytes = tpm_tis_spi_cr50_write_bytes, - .read16 = tpm_tis_spi_read16, - .read32 = tpm_tis_spi_read32, - .write32 = tpm_tis_spi_write32, }; static void cr50_print_fw_version(struct tpm_tis_data *data) diff --git a/drivers/char/tpm/tpm_tis_spi_main.c b/drivers/char/tpm/tpm_tis_spi_main.c index 54584b4..942900c 100644 --- a/drivers/char/tpm/tpm_tis_spi_main.c +++ b/drivers/char/tpm/tpm_tis_spi_main.c @@ -152,44 +152,6 @@ static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, return tpm_tis_spi_transfer(data, addr, len, NULL, value); } -int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result) -{ - __le16 result_le; - int rc; - - rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), - (u8 *)&result_le); - if (!rc) - *result = le16_to_cpu(result_le); - - return rc; -} - -int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result) -{ - __le32 result_le; - int rc; - - rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), - (u8 *)&result_le); - if (!rc) - *result = le32_to_cpu(result_le); - - return rc; -} - -int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value) -{ - __le32 value_le; - int rc; - - value_le = cpu_to_le32(value); - rc = data->phy_ops->write_bytes(data, addr, sizeof(u32), - (u8 *)&value_le); - - return rc; -} - int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy, int irq, const struct tpm_tis_phy_ops *phy_ops) { @@ -205,9 +167,6 @@ int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy, static const struct tpm_tis_phy_ops tpm_spi_phy_ops = { .read_bytes = tpm_tis_spi_read_bytes, .write_bytes = tpm_tis_spi_write_bytes, - .read16 = tpm_tis_spi_read16, - .read32 = tpm_tis_spi_read32, - .write32 = tpm_tis_spi_write32, }; static int tpm_tis_spi_probe(struct spi_device *dev) -- 2.7.4