From patchwork Thu Apr 3 02:46:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qunqin Zhao X-Patchwork-Id: 14037088 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8581DA933; Thu, 3 Apr 2025 02:45:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648364; cv=none; b=PYQlnIwEvfda2psWuNL3Qz0plS/RDpTbjTsPi9IrcGr8ih0gDqvb1J9kXbx1GBmhI8IdjMpcu3XlXXNliMk7jQ4bxgYC9Mro8VdPAzP3+Zm9+AeH5u7ETFLzUEEvxcRU3H+1FTXnBx/u9HdTZSQO0GhD6XOb/MF3VrNlTGIJS2I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648364; c=relaxed/simple; bh=KZo4k/OilUzGSEC/W+xdo2D2pteNIARJrHv3Ko4qJ3o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fq0yWhZUXQCHwetrbazpd87WRcBKhPa3DvVC9OpRdlMhEaRutfAMR87/N4zMWTNXxZrGr7bvry4CI57hNDu/BhYi3MF/i1kRpAEKuCd+WuhkP1t/JAYhWU5o0xnTjdiuBcjLxqQ9868RWf4y6rW57+4hoPFrmALXlFBjo7BYCVk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.40.54.180]) by gateway (Coremail) with SMTP id _____8AxaeFm9u1nzcavAA--.52574S3; Thu, 03 Apr 2025 10:45:58 +0800 (CST) Received: from localhost.localdomain (unknown [10.40.54.180]) by front1 (Coremail) with SMTP id qMiowMBx3MRY9u1n8SFtAA--.62246S3; Thu, 03 Apr 2025 10:45:51 +0800 (CST) From: Qunqin Zhao To: lee@kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, peterhuewe@gmx.de, jarkko@kernel.org Cc: linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-crypto@vger.kernel.org, jgg@ziepe.ca, linux-integrity@vger.kernel.org, pmenzel@molgen.mpg.de, Qunqin Zhao , Yinggang Gu , Huacai Chen Subject: [PATCH v7 1/6] mfd: Add support for Loongson Security Module Date: Thu, 3 Apr 2025 10:46:40 +0800 Message-ID: <20250403024645.4427-2-zhaoqunqin@loongson.cn> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250403024645.4427-1-zhaoqunqin@loongson.cn> References: <20250403024645.4427-1-zhaoqunqin@loongson.cn> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: qMiowMBx3MRY9u1n8SFtAA--.62246S3 X-CM-SenderInfo: 52kd01pxqtx0o6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBj9fXoW3Zr1ruFWftFy8Jr1ktw4UWrX_yoW8GF15Ko WxZFs3X3W8Jw1xArW09r1rtrWfZF9Y9a4Yyw15Z397u3WUJF15KFWFgw13Ww13ZF4Fkr15 u34YqrW8uF43Crn5l-sFpf9Il3svdjkaLaAFLSUrUUUUnb8apTn2vfkv8UJUUUU8wcxFpf 9Il3svdxBIdaVrn0xqx4xG64xvF2IEw4CE5I8CrVC2j2Jv73VFW2AGmfu7bjvjm3AaLaJ3 UjIYCTnIWjp_UUUYW7kC6x804xWl14x267AKxVWUJVW8JwAFc2x0x2IEx4CE42xK8VAvwI 8IcIk0rVWrJVCq3wAFIxvE14AKwVWUXVWUAwA2ocxC64kIII0Yj41l84x0c7CEw4AK67xG Y2AK021l84ACjcxK6xIIjxv20xvE14v26ryj6F1UM28EF7xvwVC0I7IYx2IY6xkF7I0E14 v26r4j6F4UM28EF7xvwVC2z280aVAFwI0_Cr1j6rxdM28EF7xvwVC2z280aVCY1x0267AK xVWxJr0_GcWln4kS14v26r126r1DM2AIxVAIcxkEcVAq07x20xvEncxIr21l57IF6xkI12 xvs2x26I8E6xACxx1l5I8CrVACY4xI64kE6c02F40Ex7xfMcIj6xIIjxv20xvE14v26r1q 6rW5McIj6I8E87Iv67AKxVW8JVWxJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41lc7CjxVAaw2AFwI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1l4IxYO2xFxVAFwI0_JF0_Jw1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8Gjc xK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0 cI8IcVAFwI0_Gr0_Xr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0xvE42xK8V AvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E 14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU4Xo7DUUUU This driver supports Loongson Security Module, which provides the control for it's hardware encryption acceleration child devices. Co-developed-by: Yinggang Gu Signed-off-by: Yinggang Gu Signed-off-by: Qunqin Zhao Reviewed-by: Huacai Chen --- v7: Moved Kconfig entry between MFD_INTEL_M10_BMC_PMCI and MFD_QNAP_MCU. Renamed se_enable_int_locked() to se_enable_int(), then moved the lock out of se_disable_int(). "se_send_genl_cmd" ---> "se_send_cmd". "struct lsse_ch" ---> "struct se_channel". v6: Replace all "ls6000se" with "loongson" v5: Registered "ls6000se-rng" device. v3-v4: None v2: Removed "ls6000se-sdf" device, added "ls6000se-tpm" device. Passed dmamem size to SE firmware in se_init_hw() function. drivers/mfd/Kconfig | 10 + drivers/mfd/Makefile | 2 + drivers/mfd/loongson-se.c | 374 ++++++++++++++++++++++++++++++++ include/linux/mfd/loongson-se.h | 75 +++++++ 4 files changed, 461 insertions(+) create mode 100644 drivers/mfd/loongson-se.c create mode 100644 include/linux/mfd/loongson-se.h diff --git a/drivers/mfd/Kconfig b/drivers/mfd/Kconfig index 22b936310..66889f2a9 100644 --- a/drivers/mfd/Kconfig +++ b/drivers/mfd/Kconfig @@ -2369,6 +2369,16 @@ config MFD_INTEL_M10_BMC_PMCI additional drivers must be enabled in order to use the functionality of the device. +config MFD_LOONGSON_SE + tristate "Loongson Security Module Interface" + depends on LOONGARCH && ACPI + select MFD_CORE + help + The Loongson security module provides the control for hardware + encryption acceleration devices. Each device uses at least one + channel to interact with security module, and each channel may + have its own buffer provided by security module. + config MFD_QNAP_MCU tristate "QNAP microcontroller unit core driver" depends on SERIAL_DEV_BUS diff --git a/drivers/mfd/Makefile b/drivers/mfd/Makefile index 948cbdf42..fc50601ca 100644 --- a/drivers/mfd/Makefile +++ b/drivers/mfd/Makefile @@ -290,3 +290,5 @@ obj-$(CONFIG_MFD_RSMU_I2C) += rsmu_i2c.o rsmu_core.o obj-$(CONFIG_MFD_RSMU_SPI) += rsmu_spi.o rsmu_core.o obj-$(CONFIG_MFD_UPBOARD_FPGA) += upboard-fpga.o + +obj-$(CONFIG_MFD_LOONGSON_SE) += loongson-se.o diff --git a/drivers/mfd/loongson-se.c b/drivers/mfd/loongson-se.c new file mode 100644 index 000000000..4f30695de --- /dev/null +++ b/drivers/mfd/loongson-se.c @@ -0,0 +1,374 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* Copyright (C) 2025 Loongson Technology Corporation Limited */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * The Loongson Security Module provides the control for hardware + * encryption acceleration child devices. The SE framework is + * shown as follows: + * + * +------------+ + * | CPU | + * +------------+ + * ^ ^ + * DMA | | IRQ + * v v + * +-----------------------------------+ + * | Loongson Security Module | + * +-----------------------------------+ + * ^ ^ + * channel1 | channel2 | + * v v + * +-----------+ +----------+ + * | sub-dev1 | | sub-dev2 | ..... Max sub-dev31 + * +-----------+ +----------+ + * + * The CPU cannot directly communicate with SE's sub devices, + * but sends commands to SE, which processes the commands and + * sends them to the corresponding sub devices. + */ + +struct loongson_se { + void __iomem *base; + u32 version; + spinlock_t dev_lock; + struct completion cmd_completion; + + /* dma memory */ + void *mem_base; + int mem_map_pages; + unsigned long *mem_map; + + /* channel */ + struct mutex ch_init_lock; + struct se_channel chs[SE_CH_MAX]; +}; + +union se_request { + u32 info[8]; + struct se_cmd { + u32 cmd; + u32 info[7]; + } req; + struct se_res { + u32 cmd; + u32 cmd_ret; + u32 info[6]; + } res; +}; + +static inline u32 se_readl(struct loongson_se *se, u32 off) +{ + return readl(se->base + off); +} + +static inline void se_writel(struct loongson_se *se, u32 val, u32 off) +{ + writel(val, se->base + off); +} + +static void se_enable_int(struct loongson_se *se, u32 int_bit) +{ + u32 tmp; + + tmp = se_readl(se, SE_S2LINT_EN); + tmp |= int_bit; + se_writel(se, tmp, SE_S2LINT_EN); +} + +static void se_disable_int(struct loongson_se *se, u32 int_bit) +{ + u32 tmp; + + tmp = se_readl(se, SE_S2LINT_EN); + tmp &= ~(int_bit); + se_writel(se, tmp, SE_S2LINT_EN); +} + +static int se_poll(struct loongson_se *se, u32 int_bit) +{ + u32 status; + int err; + + spin_lock_irq(&se->dev_lock); + + se_enable_int(se, int_bit); + se_writel(se, int_bit, SE_L2SINT_SET); + err = readl_relaxed_poll_timeout_atomic(se->base + SE_L2SINT_STAT, status, + !(status & int_bit), 1, 10000); + + spin_unlock_irq(&se->dev_lock); + + return err; +} + +static int se_send_requeset(struct loongson_se *se, union se_request *req) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(req->info); i++) + se_writel(se, req->info[i], SE_DATA_S + i * 4); + + return se_poll(se, SE_INT_SETUP); +} + +/* + * Called by SE's child device driver. + * Send a request to the corresponding device. + */ +int se_send_ch_requeset(struct se_channel *ch) +{ + return se_poll(ch->se, ch->int_bit); +} +EXPORT_SYMBOL_GPL(se_send_ch_requeset); + +static int se_get_res(struct loongson_se *se, u32 cmd, union se_request *res) +{ + int i; + + for (i = 0; i < ARRAY_SIZE(res->info); i++) + res->info[i] = se_readl(se, SE_DATA_L + i * 4); + + if (res->res.cmd != cmd) + return -EFAULT; + + return 0; +} + +static int se_send_cmd(struct loongson_se *se, union se_request *req) +{ + int err; + + err = se_send_requeset(se, req); + if (err) + return err; + + if (!wait_for_completion_timeout(&se->cmd_completion, HZ)) + return -ETIME; + + return se_get_res(se, req->req.cmd, req); +} + +static int se_set_msg(struct se_channel *ch) +{ + struct loongson_se *se = ch->se; + union se_request req; + + req.req.cmd = SE_CMD_SETMSG; + req.req.info[0] = ch->id; + req.req.info[1] = ch->smsg - se->mem_base; + req.req.info[2] = ch->msg_size; + + return se_send_cmd(se, &req); +} + +static irqreturn_t se_irq(int irq, void *dev_id) +{ + struct loongson_se *se = (struct loongson_se *)dev_id; + struct se_channel *ch; + unsigned long flag; + u32 int_status; + int id; + + int_status = se_readl(se, SE_S2LINT_STAT); + + spin_lock_irqsave(&se->dev_lock, flag); + se_disable_int(se, int_status); + spin_unlock_irqrestore(&se->dev_lock, flag); + + if (int_status & SE_INT_SETUP) { + complete(&se->cmd_completion); + int_status &= ~SE_INT_SETUP; + se_writel(se, SE_INT_SETUP, SE_S2LINT_CL); + } + + while (int_status) { + id = __ffs(int_status); + + ch = &se->chs[id]; + if (ch->complete) + ch->complete(ch); + int_status &= ~BIT(id); + se_writel(se, BIT(id), SE_S2LINT_CL); + } + + return IRQ_HANDLED; +} + +static int se_init_hw(struct loongson_se *se, dma_addr_t addr, int size) +{ + union se_request req; + int err; + + /* Start engine */ + req.req.cmd = SE_CMD_START; + err = se_send_cmd(se, &req); + if (err) + return err; + + /* Get Version */ + req.req.cmd = SE_CMD_GETVER; + err = se_send_cmd(se, &req); + if (err) + return err; + se->version = req.res.info[0]; + + /* Setup dma memory */ + req.req.cmd = SE_CMD_SETBUF; + req.req.info[0] = addr & 0xffffffff; + req.req.info[1] = addr >> 32; + req.req.info[2] = size; + + return se_send_cmd(se, &req); +} + +/* + * se_init_ch() - Init the channel used by child device. + * + * Allocate dma memory as agreed upon with SE on SE probe, + * and register the callback function when the data processing + * in this channel is completed. + */ +struct se_channel *se_init_ch(struct device *dev, int id, int data_size, int msg_size, + void *priv, void (*complete)(struct se_channel *se_ch)) +{ + struct loongson_se *se = dev_get_drvdata(dev); + struct se_channel *ch; + int data_first, data_nr; + int msg_first, msg_nr; + + mutex_lock(&se->ch_init_lock); + + ch = &se->chs[id]; + ch->se = se; + ch->id = id; + ch->int_bit = BIT(id); + + data_nr = round_up(data_size, PAGE_SIZE) / PAGE_SIZE; + data_first = bitmap_find_next_zero_area(se->mem_map, se->mem_map_pages, + 0, data_nr, 0); + if (data_first >= se->mem_map_pages) { + ch = NULL; + goto out_unlock; + } + + bitmap_set(se->mem_map, data_first, data_nr); + ch->off = data_first * PAGE_SIZE; + ch->data_buffer = se->mem_base + ch->off; + ch->data_size = data_size; + + msg_nr = round_up(msg_size, PAGE_SIZE) / PAGE_SIZE; + msg_first = bitmap_find_next_zero_area(se->mem_map, se->mem_map_pages, + 0, msg_nr, 0); + if (msg_first >= se->mem_map_pages) { + ch = NULL; + goto out_unlock; + } + + bitmap_set(se->mem_map, msg_first, msg_nr); + ch->smsg = se->mem_base + msg_first * PAGE_SIZE; + ch->rmsg = ch->smsg + msg_size / 2; + ch->msg_size = msg_size; + ch->complete = complete; + ch->priv = priv; + ch->version = se->version; + + if (se_set_msg(ch)) + ch = NULL; + +out_unlock: + mutex_unlock(&se->ch_init_lock); + + return ch; +} +EXPORT_SYMBOL_GPL(se_init_ch); + +static const struct mfd_cell se_devs[] = { + { .name = "loongson-rng" }, + { .name = "loongson-tpm" }, +}; + +static int loongson_se_probe(struct platform_device *pdev) +{ + struct loongson_se *se; + struct device *dev = &pdev->dev; + int nr_irq, irq, err, size; + dma_addr_t paddr; + + se = devm_kmalloc(dev, sizeof(*se), GFP_KERNEL); + if (!se) + return -ENOMEM; + dev_set_drvdata(dev, se); + init_completion(&se->cmd_completion); + spin_lock_init(&se->dev_lock); + mutex_init(&se->ch_init_lock); + dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); + if (device_property_read_u32(dev, "dmam_size", &size)) + return -ENODEV; + size = roundup_pow_of_two(size); + se->mem_base = dmam_alloc_coherent(dev, size, &paddr, GFP_KERNEL); + if (!se->mem_base) + return -ENOMEM; + se->mem_map_pages = size / PAGE_SIZE; + se->mem_map = devm_bitmap_zalloc(dev, se->mem_map_pages, GFP_KERNEL); + if (!se->mem_map) + return -ENOMEM; + + se->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(se->base)) + return PTR_ERR(se->base); + + nr_irq = platform_irq_count(pdev); + if (nr_irq <= 0) + return -ENODEV; + while (nr_irq) { + irq = platform_get_irq(pdev, --nr_irq); + /* Use the same interrupt handler address. + * Determine which irq it is accroding + * SE_S2LINT_STAT register. + */ + err = devm_request_irq(dev, irq, se_irq, 0, "loongson-se", se); + if (err) + dev_err(dev, "failed to request irq: %d\n", irq); + } + + err = se_init_hw(se, paddr, size); + if (err) + return err; + + return devm_mfd_add_devices(dev, 0, se_devs, ARRAY_SIZE(se_devs), + NULL, 0, NULL); +} + +static const struct acpi_device_id loongson_se_acpi_match[] = { + {"LOON0011", 0}, + {} +}; +MODULE_DEVICE_TABLE(acpi, loongson_se_acpi_match); + +static struct platform_driver loongson_se_driver = { + .probe = loongson_se_probe, + .driver = { + .name = "loongson-se", + .acpi_match_table = loongson_se_acpi_match, + }, +}; +module_platform_driver(loongson_se_driver); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Yinggang Gu "); +MODULE_AUTHOR("Qunqin Zhao "); +MODULE_DESCRIPTION("Loongson Security Module driver"); diff --git a/include/linux/mfd/loongson-se.h b/include/linux/mfd/loongson-se.h new file mode 100644 index 000000000..f209b7043 --- /dev/null +++ b/include/linux/mfd/loongson-se.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* Copyright (C) 2025 Loongson Technology Corporation Limited */ + +#ifndef __LOONGSON_SE_H__ +#define __LOONGSON_SE_H__ + +#define SE_DATA_S 0x0 +#define SE_DATA_L 0x20 +#define SE_S2LINT_STAT 0x88 +#define SE_S2LINT_EN 0x8c +#define SE_S2LINT_SET 0x90 +#define SE_S2LINT_CL 0x94 +#define SE_L2SINT_STAT 0x98 +#define SE_L2SINT_EN 0x9c +#define SE_L2SINT_SET 0xa0 +#define SE_L2SINT_CL 0xa4 + +/* INT bit definition */ +#define SE_INT_SETUP BIT(0) +#define SE_INT_TPM BIT(5) + +#define SE_CMD_START 0x0 +#define SE_CMD_STOP 0x1 +#define SE_CMD_GETVER 0x2 +#define SE_CMD_SETBUF 0x3 +#define SE_CMD_SETMSG 0x4 + +#define SE_CMD_RNG 0x100 +#define SE_CMD_SM2_SIGN 0x200 +#define SE_CMD_SM2_VSIGN 0x201 +#define SE_CMD_SM3_DIGEST 0x300 +#define SE_CMD_SM3_UPDATE 0x301 +#define SE_CMD_SM3_FINISH 0x302 +#define SE_CMD_SM4_ECB_ENCRY 0x400 +#define SE_CMD_SM4_ECB_DECRY 0x401 +#define SE_CMD_SM4_CBC_ENCRY 0x402 +#define SE_CMD_SM4_CBC_DECRY 0x403 +#define SE_CMD_SM4_CTR 0x404 +#define SE_CMD_TPM 0x500 +#define SE_CMD_ZUC_INIT_READ 0x600 +#define SE_CMD_ZUC_READ 0x601 +#define SE_CMD_SDF 0x700 + +#define SE_CH_MAX 32 +#define SE_CH_RNG 1 +#define SE_CH_SM2 2 +#define SE_CH_SM3 3 +#define SE_CH_SM4 4 +#define SE_CH_TPM 5 +#define SE_CH_ZUC 6 +#define SE_CH_SDF 7 + +struct se_channel { + struct loongson_se *se; + void *priv; + u32 version; + u32 id; + u32 int_bit; + + void *smsg; + void *rmsg; + int msg_size; + + void *data_buffer; + int data_size; + u32 off; + + void (*complete)(struct se_channel *se_ch); +}; + +struct se_channel *se_init_ch(struct device *dev, int id, int data_size, int msg_size, + void *priv, void (*complete)(struct se_channel *se_ch)); +int se_send_ch_requeset(struct se_channel *ch); + +#endif From patchwork Thu Apr 3 02:46:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qunqin Zhao X-Patchwork-Id: 14037089 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4C82D24B29; Thu, 3 Apr 2025 02:46:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648364; cv=none; b=o6E69L9YoGdxQSgwWCw5AMlz77qgN0o7fzaikgUFvFMKqko3meK+SihO6RPKEEPEF+jFie61gkQ1oRtTv2NfUvyKBcwwLfJQs6uXoQFpkV0Xe6kV0Nvldi9q3N+D5cDmjRXX/CcZmG3t/NrSmKhrUW6uify76u+lmXIVxCJ7NUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648364; c=relaxed/simple; bh=d8J4Yo+YNaI+HBcojp3i+Q4OH9DvvQ0WqocwzhvQkjc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=S3EQKQ8a67Zet9VR3cwCjlJ/wpCRw0aKt5Ky+1rjsN4ZPHEmgvgIwwJ57Fpvq9QOv0CPn4TG733ADjdO7IakcOZ+Kmt63fsP7fQDv5hyFWvXIxADyLqWGOeNFmsRhb2L+UDmtidX192ddPRcAXTiTcbHpqCMWcy+eMwgsLrbrfo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.40.54.180]) by gateway (Coremail) with SMTP id _____8Cxqmpn9u1n1cavAA--.32373S3; Thu, 03 Apr 2025 10:45:59 +0800 (CST) Received: from localhost.localdomain (unknown [10.40.54.180]) by front1 (Coremail) with SMTP id qMiowMBx3MRY9u1n8SFtAA--.62246S4; Thu, 03 Apr 2025 10:45:57 +0800 (CST) From: Qunqin Zhao To: lee@kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, peterhuewe@gmx.de, jarkko@kernel.org Cc: linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-crypto@vger.kernel.org, jgg@ziepe.ca, linux-integrity@vger.kernel.org, pmenzel@molgen.mpg.de, Qunqin Zhao , Huacai Chen Subject: [PATCH v7 2/6] MAINTAINERS: Add entry for Loongson Security Module driver Date: Thu, 3 Apr 2025 10:46:41 +0800 Message-ID: <20250403024645.4427-3-zhaoqunqin@loongson.cn> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250403024645.4427-1-zhaoqunqin@loongson.cn> References: <20250403024645.4427-1-zhaoqunqin@loongson.cn> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: qMiowMBx3MRY9u1n8SFtAA--.62246S4 X-CM-SenderInfo: 52kd01pxqtx0o6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBj9xXoW7Xw18uryDZr4fAr1rWw1xWFX_yoWDuwb_Ca yxK3yxJr48JFn2k3y8uF1fJrWSg3yxXF1Uu3Z2yw1fZ34DtFy5ArykA3s7C3W3CrW5CFsx XayxGrs7Cr4xZosvyTuYvTs0mTUanT9S1TB71UUUUjDqnTZGkaVYY2UrUUUUj1kv1TuYvT s0mT0YCTnIWjqI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUI cSsGvfJTRUUUbfAYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20x vaj40_Wr0E3s1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW5JVW7JwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV W8JVWxJwA2z4x0Y4vEx4A2jsIE14v26F4UJVW0owA2z4x0Y4vEx4A2jsIEc7CjxVAFwI0_ Cr1j6rxdM2kKe7AKxVWUAVWUtwAS0I0E0xvYzxvE52x082IY62kv0487Mc804VCY07AIYI kI8VC2zVCFFI0UMc02F40EFcxC0VAKzVAqx4xG6I80ewAv7VC0I7IYx2IY67AKxVWUtVWr XwAv7VC2z280aVAFwI0_Gr0_Cr1lOx8S6xCaFVCjc4AY6r1j6r4UM4x0Y48IcxkI7VAKI4 8JMxkF7I0En4kS14v26r126r1DMxAIw28IcxkI7VAKI48JMxC20s026xCaFVCjc4AY6r1j 6r4UMxCIbckI1I0E14v26r126r1DMI8I3I0E5I8CrVAFwI0_Jr0_Jr4lx2IqxVCjr7xvwV AFwI0_JrI_JrWlx4CE17CEb7AF67AKxVWUtVW8ZwCIc40Y0x0EwIxGrwCI42IY6xIIjxv2 0xvE14v26ryj6F1UMIIF0xvE2Ix0cI8IcVCY1x0267AKxVW8JVWxJwCI42IY6xAIw20EY4 v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AK xVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU8_gA5UUUUU== This patch adds an entry for Loongson Security Module driver in the list of maintainers. Signed-off-by: Qunqin Zhao Reviewed-by: Huacai Chen --- v7: Added Huacai's tag v6: "ls6000se.c" --> "loongson_se.c" "ls6000se.h" --> "loongson_se.h" v5: None v4: Changed tile to "Add entry for ....". Lowcased "Maintainers" in commit message. MAINTAINERS | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index e46b60ccf..d2d8ae6a6 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13616,6 +13616,13 @@ S: Maintained F: Documentation/devicetree/bindings/i2c/loongson,ls2x-i2c.yaml F: drivers/i2c/busses/i2c-ls2x.c +LOONGSON SECURITY MODULE DRIVER +M: Qunqin Zhao +L: loongarch@lists.linux.dev +S: Maintained +F: drivers/mfd/loongson-se.c +F: include/linux/mfd/loongson-se.h + LOONGSON-2 SOC SERIES CLOCK DRIVER M: Yinbo Zhu L: linux-clk@vger.kernel.org From patchwork Thu Apr 3 02:46:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qunqin Zhao X-Patchwork-Id: 14037091 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3A9B416F0FE; Thu, 3 Apr 2025 02:46:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648366; cv=none; b=ZVIiZk0thxrL+/e2TwlTZte27fJR9KtG+7k2PpTb0egn2TFbYTCNSSmI8YmiYVbttQ90VJ7+TnJaH6hqQxtYPkXj5x8EciyC/g4Hr3R6lC03EH+D8BLGRCHExSBaPEDCIp3vzvmx1eEYg/jOEQAOFTcp7wJSHjiP0pDEsAXa0AY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648366; c=relaxed/simple; bh=tCjNVxL3l+t2SU7mdgmKzyw+JXSRqWQas8sTDWEDNIw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Z4eYBB2TU/nqFKooObaA2m0i/FzusDDt+3B1jc0SQCRigDPq+yZcWUHwK8jfY20AY9Cckcak6jEWiIAXBV1roZk4izERSmjDsGXYB0sMsobYbg+9U+tHtAsK8FZut6kgBF2go3CrmMxt0eznbn99Geh1IdJ5A0I6icg5SNTiYfM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.40.54.180]) by gateway (Coremail) with SMTP id _____8DxfWto9u1n3cavAA--.31087S3; Thu, 03 Apr 2025 10:46:00 +0800 (CST) Received: from localhost.localdomain (unknown [10.40.54.180]) by front1 (Coremail) with SMTP id qMiowMBx3MRY9u1n8SFtAA--.62246S5; Thu, 03 Apr 2025 10:45:59 +0800 (CST) From: Qunqin Zhao To: lee@kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, peterhuewe@gmx.de, jarkko@kernel.org Cc: linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-crypto@vger.kernel.org, jgg@ziepe.ca, linux-integrity@vger.kernel.org, pmenzel@molgen.mpg.de, Qunqin Zhao , Yinggang Gu , Huacai Chen Subject: [PATCH v7 3/6] crypto: loongson - add Loongson RNG driver support Date: Thu, 3 Apr 2025 10:46:42 +0800 Message-ID: <20250403024645.4427-4-zhaoqunqin@loongson.cn> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250403024645.4427-1-zhaoqunqin@loongson.cn> References: <20250403024645.4427-1-zhaoqunqin@loongson.cn> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: qMiowMBx3MRY9u1n8S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F7I0E14v26r4j6F4UMIIF0xvE42xK8V AvwI8IcIk0rVWUJVWUCwCI42IY6I8E87Iv67AKxVW8JVWxJwCI42IY6I8E87Iv6xkF7I0E 14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxU4Xo7DUUUU Loongson's Random Number Generator is found inside Loongson security engine. Co-developed-by: Yinggang Gu Signed-off-by: Yinggang Gu Signed-off-by: Qunqin Zhao Reviewed-by: Huacai Chen --- v7: Change the lsrng_ prefix to loongson_rng_ v6: Replace all "ls6000se" with "loongson" v2-v5: None drivers/crypto/Kconfig | 1 + drivers/crypto/Makefile | 1 + drivers/crypto/loongson/Kconfig | 6 + drivers/crypto/loongson/Makefile | 2 + drivers/crypto/loongson/loongson-rng.c | 190 +++++++++++++++++++++++++ 5 files changed, 200 insertions(+) create mode 100644 drivers/crypto/loongson/Kconfig create mode 100644 drivers/crypto/loongson/Makefile create mode 100644 drivers/crypto/loongson/loongson-rng.c diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig index 19ab145f9..567ed81b0 100644 --- a/drivers/crypto/Kconfig +++ b/drivers/crypto/Kconfig @@ -855,5 +855,6 @@ config CRYPTO_DEV_SA2UL source "drivers/crypto/aspeed/Kconfig" source "drivers/crypto/starfive/Kconfig" +source "drivers/crypto/loongson/Kconfig" endif # CRYPTO_HW diff --git a/drivers/crypto/Makefile b/drivers/crypto/Makefile index fef18ffdb..643c3710b 100644 --- a/drivers/crypto/Makefile +++ b/drivers/crypto/Makefile @@ -50,3 +50,4 @@ obj-y += hisilicon/ obj-$(CONFIG_CRYPTO_DEV_AMLOGIC_GXL) += amlogic/ obj-y += intel/ obj-y += starfive/ +obj-y += loongson/ diff --git a/drivers/crypto/loongson/Kconfig b/drivers/crypto/loongson/Kconfig new file mode 100644 index 000000000..4368701ad --- /dev/null +++ b/drivers/crypto/loongson/Kconfig @@ -0,0 +1,6 @@ +# SPDX-License-Identifier: GPL-2.0 +config CRYPTO_DEV_LOONGSON_RNG + tristate "Support for Loongson RNG Driver" + depends on MFD_LOONGSON_SE + help + Support for Loongson RNG Driver. diff --git a/drivers/crypto/loongson/Makefile b/drivers/crypto/loongson/Makefile new file mode 100644 index 000000000..b8b013c86 --- /dev/null +++ b/drivers/crypto/loongson/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +obj-$(CONFIG_CRYPTO_DEV_LOONGSON_RNG) += loongson-rng.o diff --git a/drivers/crypto/loongson/loongson-rng.c b/drivers/crypto/loongson/loongson-rng.c new file mode 100644 index 000000000..307014992 --- /dev/null +++ b/drivers/crypto/loongson/loongson-rng.c @@ -0,0 +1,190 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 HiSilicon Limited. */ +/* Copyright (c) 2025 Loongson Technology Corporation Limited. */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct loongson_rng_list { + struct mutex lock; + struct list_head list; + int is_init; +}; + +struct lsrng { + bool is_used; + struct se_channel *se_ch; + struct list_head list; + struct completion rng_completion; +}; + +struct loongson_rng_ctx { + struct lsrng *rng; +}; + +struct rng_msg { + u32 cmd; + union { + u32 len; + u32 ret; + } u; + u32 resved; + u32 out_off; + u32 pad[4]; +}; + +static atomic_t rng_active_devs; +static struct loongson_rng_list rng_devices; + +static void loongson_rng_complete(struct se_channel *ch) +{ + struct lsrng *rng = (struct lsrng *)ch->priv; + + complete(&rng->rng_completion); +} + +static int loongson_rng_generate(struct crypto_rng *tfm, const u8 *src, + unsigned int slen, u8 *dstn, unsigned int dlen) +{ + struct loongson_rng_ctx *ctx = crypto_rng_ctx(tfm); + struct lsrng *rng = ctx->rng; + struct rng_msg *msg; + int err, len; + + do { + len = min(dlen, PAGE_SIZE); + msg = rng->se_ch->smsg; + msg->u.len = len; + err = se_send_ch_requeset(rng->se_ch); + if (err) + return err; + + wait_for_completion_interruptible(&rng->rng_completion); + + msg = rng->se_ch->rmsg; + if (msg->u.ret) + return -EFAULT; + + memcpy(dstn, rng->se_ch->data_buffer, len); + dlen -= len; + dstn += len; + } while (dlen > 0); + + return 0; +} + +static int loongson_rng_init(struct crypto_tfm *tfm) +{ + struct loongson_rng_ctx *ctx = crypto_tfm_ctx(tfm); + struct lsrng *rng; + int ret = -EBUSY; + + mutex_lock(&rng_devices.lock); + list_for_each_entry(rng, &rng_devices.list, list) { + if (!rng->is_used) { + rng->is_used = true; + ctx->rng = rng; + ret = 0; + break; + } + } + mutex_unlock(&rng_devices.lock); + + return ret; +} + +static void loongson_rng_exit(struct crypto_tfm *tfm) +{ + struct loongson_rng_ctx *ctx = crypto_tfm_ctx(tfm); + + mutex_lock(&rng_devices.lock); + ctx->rng->is_used = false; + mutex_unlock(&rng_devices.lock); +} + +static int no_seed(struct crypto_rng *tfm, const u8 *seed, unsigned int slen) +{ + return 0; +} + +static struct rng_alg loongson_rng_alg = { + .generate = loongson_rng_generate, + .seed = no_seed, + .base = { + .cra_name = "stdrng", + .cra_driver_name = "loongson_stdrng", + .cra_priority = 300, + .cra_ctxsize = sizeof(struct loongson_rng_ctx), + .cra_module = THIS_MODULE, + .cra_init = loongson_rng_init, + .cra_exit = loongson_rng_exit, + }, +}; + +static void loongson_rng_add_to_list(struct lsrng *rng) +{ + mutex_lock(&rng_devices.lock); + list_add_tail(&rng->list, &rng_devices.list); + mutex_unlock(&rng_devices.lock); +} + +static int loongson_rng_probe(struct platform_device *pdev) +{ + struct rng_msg *msg; + struct lsrng *rng; + int ret; + + rng = devm_kzalloc(&pdev->dev, sizeof(*rng), GFP_KERNEL); + if (!rng) + return -ENOMEM; + + init_completion(&rng->rng_completion); + rng->se_ch = se_init_ch(pdev->dev.parent, SE_CH_RNG, PAGE_SIZE, + sizeof(struct rng_msg) * 2, rng, loongson_rng_complete); + if (!rng->se_ch) + return -ENODEV; + msg = rng->se_ch->smsg; + msg->cmd = SE_CMD_RNG; + msg->out_off = rng->se_ch->off; + + if (!rng_devices.is_init) { + ret = crypto_register_rng(&loongson_rng_alg); + if (ret) { + dev_err(&pdev->dev, "failed to register crypto(%d)\n", ret); + return ret; + } + INIT_LIST_HEAD(&rng_devices.list); + mutex_init(&rng_devices.lock); + rng_devices.is_init = true; + } + + loongson_rng_add_to_list(rng); + atomic_inc(&rng_active_devs); + + return 0; +} + +static struct platform_driver loongson_rng_driver = { + .probe = loongson_rng_probe, + .driver = { + .name = "loongson-rng", + }, +}; +module_platform_driver(loongson_rng_driver); + +MODULE_ALIAS("platform:loongson-rng"); +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Yinggang Gu "); +MODULE_AUTHOR("Qunqin Zhao "); +MODULE_DESCRIPTION("Loongson random number generator driver"); From patchwork Thu Apr 3 02:46:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qunqin Zhao X-Patchwork-Id: 14037092 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0F86E1891A9; Thu, 3 Apr 2025 02:46:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648387; cv=none; b=nIT76hr6+6DTnWrtDAWuWwhCDHmjmpbbzPWIpx4MGI/uuQ2eVAR2YAjhS88gIDaDl7hGuQh8HjiB67bKfRkwoEOS659pFXbq9wguD1HUxh20BIoK5N4l3mjofGhf+jTF1HrtRhA7wByeYjJQp0W6svwnQZROzgBhr9LkwquEqaw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648387; c=relaxed/simple; bh=aXlfkdvXljyBhKEhQfz9OeaoqiFMa4hMa3bcUYF5bZA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Cr6fxw1VaWujoCyZvyYAzRTwbwIuLZtjSqMqRAZFSjpqI3+GE9ZX9STQEStwzK2w+lan9ssqyFf4dy7MJw+AuEognd+KUtPNdR0e8vDijVi68wZMIdlmflvTjosoLMZYTHrZ9pWwE3i+iJXmQ5m4YoVldF3+ZE8y4YCxj46MiKk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.40.54.180]) by gateway (Coremail) with SMTP id _____8DxzOJ49u1nJcevAA--.30878S3; Thu, 03 Apr 2025 10:46:16 +0800 (CST) Received: from localhost.localdomain (unknown [10.40.54.180]) by front1 (Coremail) with SMTP id qMiowMBx3MRY9u1n8SFtAA--.62246S6; Thu, 03 Apr 2025 10:46:01 +0800 (CST) From: Qunqin Zhao To: lee@kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, peterhuewe@gmx.de, jarkko@kernel.org Cc: linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-crypto@vger.kernel.org, jgg@ziepe.ca, linux-integrity@vger.kernel.org, pmenzel@molgen.mpg.de, Qunqin Zhao , Huacai Chen Subject: [PATCH v7 4/6] MAINTAINERS: Add entry for Loongson RNG driver Date: Thu, 3 Apr 2025 10:46:43 +0800 Message-ID: <20250403024645.4427-5-zhaoqunqin@loongson.cn> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250403024645.4427-1-zhaoqunqin@loongson.cn> References: <20250403024645.4427-1-zhaoqunqin@loongson.cn> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: qMiowMBx3MRY9u1n8SFtAA--.62246S6 X-CM-SenderInfo: 52kd01pxqtx0o6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBj9xXoW7Xr1UZryUGrWxZFy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r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUVWrXDUUUU This patch adds an entry for Loongson RNG driver in the list of maintainers. Signed-off-by: Qunqin Zhao Reviewed-by: Huacai Chen --- v7: Added Huacai's tag v5-v6: None v4: Changed tile to "Add entry for ...". Lowcased "Maintainers" in commit message. MAINTAINERS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MAINTAINERS b/MAINTAINERS index d2d8ae6a6..814205642 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13602,6 +13602,12 @@ S: Maintained F: Documentation/devicetree/bindings/gpio/loongson,ls-gpio.yaml F: drivers/gpio/gpio-loongson-64bit.c +LOONGSON CRYPTO DRIVER +M: Qunqin Zhao +L: linux-crypto@vger.kernel.org +S: Maintained +F: drivers/crypto/loongson/ + LOONGSON-2 APB DMA DRIVER M: Binbin Zhou L: dmaengine@vger.kernel.org From patchwork Thu Apr 3 02:47:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qunqin Zhao X-Patchwork-Id: 14037093 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CA601194A73; Thu, 3 Apr 2025 02:46:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648416; cv=none; b=nJpkBtXHK5nIyOB7vrzFpRtEtmafVSfjwmFZ42Nxcs7Xw/TDSlsugf5HOIuhanHKyDkMedK24bVT2455HU0nEcPJxcoCoK4rp9fDRAfOF8BYnm5ta8In3v0isEvY0RXwpic8Fv74qlxLZRRgeNFu99/S0eHYKSuT8Kl0CfjP3P4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648416; c=relaxed/simple; bh=j96DN75Q9TvfZfL7/FAP9Tz80HzbJtXmCGTt4R+kQd4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=t3LhUY3buy2NEvfy8cOp3OTIpmbe7sR03bHtN6mBvqnyb9X3v/a45xmudEbuBa7smVZYSHovU3wNWoLoAapdPL7RkY8IH2X+UxR5jTdSBUYsH1Ex00T0a4Ob6v5dD8VTh3vgmU72YcybBkb4+C8oJX5LJYckgZhtWlcp4TQ5q1g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.40.54.180]) by gateway (Coremail) with SMTP id _____8AxHHKc9u1nWsevAA--.31022S3; Thu, 03 Apr 2025 10:46:52 +0800 (CST) Received: from localhost.localdomain (unknown [10.40.54.180]) by front1 (Coremail) with SMTP id qMiowMCx7xua9u1ndyJtAA--.61678S2; Thu, 03 Apr 2025 10:46:51 +0800 (CST) From: Qunqin Zhao To: lee@kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, peterhuewe@gmx.de, jarkko@kernel.org Cc: linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-crypto@vger.kernel.org, jgg@ziepe.ca, linux-integrity@vger.kernel.org, pmenzel@molgen.mpg.de, Qunqin Zhao , Yinggang Gu , Huacai Chen Subject: [PATCH v7 5/6] tpm: Add a driver for Loongson TPM device Date: Thu, 3 Apr 2025 10:47:49 +0800 Message-ID: <20250403024750.4448-1-zhaoqunqin@loongson.cn> X-Mailer: git-send-email 2.45.2 Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: qMiowMCx7xua9u1ndyJtAA--.61678S2 X-CM-SenderInfo: 52kd01pxqtx0o6or00hjv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F4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x02 67AKxVW8JVW8JrUvcSsGvfC2KfnxnUUI43ZEXa7IU0_WrPUUUUU== Loongson security engine supports random number generation, hash, symmetric encryption and asymmetric encryption. Based on these encryption functions, TPM2 have been implemented in the Loongson security engine firmware. This driver is responsible for copying data into the memory visible to the firmware and receiving data from the firmware. Co-developed-by: Yinggang Gu Signed-off-by: Yinggang Gu Signed-off-by: Qunqin Zhao Reviewed-by: Jarkko Sakkinen Reviewed-by: Huacai Chen --- v7: Moved Kconfig entry between TCG_IBMVTPM and TCG_XEN. Added Jarkko's tag(a little change, should be fine). v6: Replace all "ls6000se" with "loongson" Prefix all with tpm_loongson instead of tpm_lsse. Removed Jarkko's tag cause there are some changes in v6 v5: None v4: Prefix all with tpm_lsse instead of tpm. Removed MODULE_AUTHOR fields. v3: Added reminder about Loongson security engine to git log. drivers/char/tpm/Kconfig | 9 +++ drivers/char/tpm/Makefile | 1 + drivers/char/tpm/tpm_loongson.c | 103 ++++++++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 drivers/char/tpm/tpm_loongson.c diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig index 0fc9a510e..a5152e83a 100644 --- a/drivers/char/tpm/Kconfig +++ b/drivers/char/tpm/Kconfig @@ -189,6 +189,15 @@ config TCG_IBMVTPM will be accessible from within Linux. To compile this driver as a module, choose M here; the module will be called tpm_ibmvtpm. +config TCG_LOONGSON + tristate "Loongson TPM Interface" + depends on MFD_LOONGSON_SE + help + If you want to make Loongson TPM support available, say Yes and + it will be accessible from within Linux. To compile this + driver as a module, choose M here; the module will be called + tpm_loongson. + config TCG_XEN tristate "XEN TPM Interface" depends on TCG_TPM && XEN diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile index 9bb142c75..e84a2f7a9 100644 --- a/drivers/char/tpm/Makefile +++ b/drivers/char/tpm/Makefile @@ -44,3 +44,4 @@ obj-$(CONFIG_TCG_XEN) += xen-tpmfront.o obj-$(CONFIG_TCG_CRB) += tpm_crb.o obj-$(CONFIG_TCG_VTPM_PROXY) += tpm_vtpm_proxy.o obj-$(CONFIG_TCG_FTPM_TEE) += tpm_ftpm_tee.o +obj-$(CONFIG_TCG_LOONGSON) += tpm_loongson.o diff --git a/drivers/char/tpm/tpm_loongson.c b/drivers/char/tpm/tpm_loongson.c new file mode 100644 index 000000000..8b7da8cdd --- /dev/null +++ b/drivers/char/tpm/tpm_loongson.c @@ -0,0 +1,103 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2025 Loongson Technology Corporation Limited. */ + +#include +#include +#include +#include + +#include "tpm.h" + +struct tpm_loongson_msg { + u32 cmd; + u32 data_off; + u32 data_len; + u32 info[5]; +}; + +struct tpm_loongson_dev { + struct se_channel *se_ch; + struct completion tpm_loongson_completion; +}; + +static void tpm_loongson_complete(struct se_channel *ch) +{ + struct tpm_loongson_dev *td = ch->priv; + + complete(&td->tpm_loongson_completion); +} + +static int tpm_loongson_recv(struct tpm_chip *chip, u8 *buf, size_t count) +{ + struct tpm_loongson_dev *td = dev_get_drvdata(&chip->dev); + struct tpm_loongson_msg *rmsg; + int sig; + + sig = wait_for_completion_interruptible(&td->tpm_loongson_completion); + if (sig) + return sig; + + rmsg = td->se_ch->rmsg; + memcpy(buf, td->se_ch->data_buffer, rmsg->data_len); + + return rmsg->data_len; +} + +static int tpm_loongson_send(struct tpm_chip *chip, u8 *buf, size_t count) +{ + struct tpm_loongson_dev *td = dev_get_drvdata(&chip->dev); + struct tpm_loongson_msg *smsg = td->se_ch->smsg; + + memcpy(td->se_ch->data_buffer, buf, count); + smsg->data_len = count; + + return se_send_ch_requeset(td->se_ch); +} + +static const struct tpm_class_ops tpm_loongson_ops = { + .flags = TPM_OPS_AUTO_STARTUP, + .recv = tpm_loongson_recv, + .send = tpm_loongson_send, +}; + +static int tpm_loongson_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct tpm_loongson_msg *smsg; + struct tpm_loongson_dev *td; + struct tpm_chip *chip; + + td = devm_kzalloc(dev, sizeof(struct tpm_loongson_dev), GFP_KERNEL); + if (!td) + return -ENOMEM; + + init_completion(&td->tpm_loongson_completion); + td->se_ch = se_init_ch(dev->parent, SE_CH_TPM, PAGE_SIZE, + 2 * sizeof(struct tpm_loongson_msg), td, + tpm_loongson_complete); + if (!td->se_ch) + return -ENODEV; + smsg = td->se_ch->smsg; + smsg->cmd = SE_CMD_TPM; + smsg->data_off = td->se_ch->off; + + chip = tpmm_chip_alloc(dev, &tpm_loongson_ops); + if (IS_ERR(chip)) + return PTR_ERR(chip); + chip->flags = TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_IRQ; + dev_set_drvdata(&chip->dev, td); + + return tpm_chip_register(chip); +} + +static struct platform_driver tpm_loongson_driver = { + .probe = tpm_loongson_probe, + .driver = { + .name = "loongson-tpm", + }, +}; +module_platform_driver(tpm_loongson_driver); + +MODULE_ALIAS("platform:loongson-tpm"); +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Loongson TPM driver"); From patchwork Thu Apr 3 02:47:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qunqin Zhao X-Patchwork-Id: 14037094 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3B025189F39; Thu, 3 Apr 2025 02:47:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=114.242.206.163 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648437; cv=none; b=Z2dccdBYqlcLQkALVBjd7Vt/83+KSJuHSoOeN1/x/VVnQZXQfBTNLjPFz/bL9DgbweQh13p0Vn2EpB5/vP8QlslWoQnJTyl99K8kYt7UqEPHLGvMbz0CU0Wrp26qKlZD6hLnondlpbIRF/nznLEl6AcUc7pjozFYhaM/IRmBsT0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1743648437; c=relaxed/simple; bh=S3q9bb4U9e2oivwjcyGVae6pXFc0/ArY9TSeWTFYhXI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eDxwd36EJWQaNrNZ1qcN1LMOSkvecetWaDHx2kaxu7T2j69hfGcb75w2T18GV9SEo6PBJ0QR6Q1oTnBDGpz0B5ufcWZ124KkY1Z677Ti6kWEycAkqVXOFQVOI+Hh2zaLYPFeAomOiXuWuQ1rJukXGV+Eip/MXgUMPXEqe6hIz4I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn; spf=pass smtp.mailfrom=loongson.cn; arc=none smtp.client-ip=114.242.206.163 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=loongson.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=loongson.cn Received: from loongson.cn (unknown [10.40.54.180]) by gateway (Coremail) with SMTP id _____8AxlnCs9u1nesevAA--.30636S3; Thu, 03 Apr 2025 10:47:08 +0800 (CST) Received: from localhost.localdomain (unknown [10.40.54.180]) by front1 (Coremail) with SMTP id qMiowMCx7xua9u1ndyJtAA--.61678S3; Thu, 03 Apr 2025 10:46:52 +0800 (CST) From: Qunqin Zhao To: lee@kernel.org, herbert@gondor.apana.org.au, davem@davemloft.net, peterhuewe@gmx.de, jarkko@kernel.org Cc: linux-kernel@vger.kernel.org, loongarch@lists.linux.dev, linux-crypto@vger.kernel.org, jgg@ziepe.ca, linux-integrity@vger.kernel.org, pmenzel@molgen.mpg.de, Qunqin Zhao , Huacai Chen Subject: [PATCH v7 6/6] MAINTAINERS: Add tpm_loongson.c to LOONGSON CRYPTO DRIVER entry Date: Thu, 3 Apr 2025 10:47:50 +0800 Message-ID: <20250403024750.4448-2-zhaoqunqin@loongson.cn> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20250403024750.4448-1-zhaoqunqin@loongson.cn> References: <20250403024750.4448-1-zhaoqunqin@loongson.cn> Precedence: bulk X-Mailing-List: linux-integrity@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-CM-TRANSID: qMiowMCx7xua9u1ndyJtAA--.61678S3 X-CM-SenderInfo: 52kd01pxqtx0o6or00hjvr0hdfq/ X-Coremail-Antispam: 1Uk129KBj9xXoW7GFyftFyrAF48Jr1rCw1xJFc_yoW3Crc_Ca y7t3yxXr1kGFn2934FvF97Aw4YvrWrJ3Wxu3WUtw1fX34Utr98trnFyF48K3W3CrW5CFZx WayxGFn5Ar1avosvyTuYvTs0mTUanT9S1TB71UUUUjDqnTZGkaVYY2UrUUUUj1kv1TuYvT s0mT0YCTnIWjqI5I8CrVACY4xI64kE6c02F40Ex7xfYxn0WfASr-VFAUDa7-sFnT9fnUUI cSsGvfJTRUUUbSAYFVCjjxCrM7AC8VAFwI0_Jr0_Gr1l1xkIjI8I6I8E6xAIw20EY4v20x vaj40_Wr0E3s1l1IIY67AEw4v_Jrv_JF1l8cAvFVAK0II2c7xJM28CjxkF64kEwVA0rcxS w2x7M28EF7xvwVC0I7IYx2IY67AKxVW7JVWDJwA2z4x0Y4vE2Ix0cI8IcVCY1x0267AKxV WxJVW8Jr1l84ACjcxK6I8E87Iv67AKxVWxJr0_GcWl84ACjcxK6I8E87Iv6xkF7I0E14v2 6F4UJVW0owAaw2AFwI0_JF0_Jw1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2xF0c Ia020Ex4CE44I27wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_Wrv_ ZF1lYx0Ex4A2jsIE14v26F4j6r4UJwAm72CE4IkC6x0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64 vIr41lc7CjxVAaw2AFwI0_JF0_Jw1l42xK82IYc2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_ Jr0_Gr1l4IxYO2xFxVAFwI0_JF0_Jw1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s026x8Gjc xK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF0xvE2Ix0 cI8IcVAFwI0_Ar0_tr1lIxAIcVC0I7IYx2IY6xkF7I0E14v26F4j6r4UJwCI42IY6xAIw2 0EY4v20xvaj40_Jr0_JF4lIxAIcVC2z280aVAFwI0_Cr0_Gr1UMIIF0xvEx4A2jsIEc7Cj xVAFwI0_Gr0_Gr1UYxBIdaVFxhVjvjDU0xZFpf9x07jxxhdUUUUU= Changes to Loongson TPM driver would be best reviewed by the Loongson crypto driver maintainers. Signed-off-by: Qunqin Zhao Reviewed-by: Jarkko Sakkinen Reviewed-by: Huacai Chen --- v7: Added tag from Jarkko and Huacai v6: "tpm_lsse.c" -> "tpm_loongson" v4-v5: None MAINTAINERS | 1 + 1 file changed, 1 insertion(+) diff --git a/MAINTAINERS b/MAINTAINERS index 814205642..16fcd35ff 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -13606,6 +13606,7 @@ LOONGSON CRYPTO DRIVER M: Qunqin Zhao L: linux-crypto@vger.kernel.org S: Maintained +F: drivers/char/tpm/tpm_loongson.c F: drivers/crypto/loongson/ LOONGSON-2 APB DMA DRIVER