lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <322d4564-b188-64b9-13b3-7a91d3887643@loongson.cn>
Date: Sat, 26 Jul 2025 15:05:17 +0800
From: Qunqin Zhao <zhaoqunqin@...ngson.cn>
To: Stefano Garzarella <sgarzare@...hat.com>
Cc: lee@...nel.org, herbert@...dor.apana.org.au, jarkko@...nel.org,
 linux-kernel@...r.kernel.org, loongarch@...ts.linux.dev,
 davem@...emloft.net, linux-crypto@...r.kernel.org, peterhuewe@....de,
 jgg@...pe.ca, linux-integrity@...r.kernel.org,
 Yinggang Gu <guyinggang@...ngson.cn>, Huacai Chen <chenhuacai@...ngson.cn>
Subject: Re: [PATCH v12 3/4] tpm: Add a driver for Loongson TPM device


在 2025/7/7 下午5:48, Stefano Garzarella 写道:
> On Sat, Jul 05, 2025 at 03:20:44PM +0800, Qunqin Zhao wrote:
>> 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 <guyinggang@...ngson.cn>
>> Signed-off-by: Yinggang Gu <guyinggang@...ngson.cn>
>> Signed-off-by: Qunqin Zhao <zhaoqunqin@...ngson.cn>
>> Reviewed-by: Huacai Chen <chenhuacai@...ngson.cn>
>> ---
>> drivers/char/tpm/Kconfig        |  9 ++++
>> drivers/char/tpm/Makefile       |  1 +
>> drivers/char/tpm/tpm_loongson.c | 84 +++++++++++++++++++++++++++++++++
>> 3 files changed, 94 insertions(+)
>> create mode 100644 drivers/char/tpm/tpm_loongson.c
>
> TPM_CHIP_FLAG_SYNC support is now merged in linux-tpmdd/next tree, so 
> IMHO this driver can also set it and implement a synchronous send() in 
> this way (untested):
>
> diff --git a/drivers/char/tpm/tpm_loongson.c 
> b/drivers/char/tpm/tpm_loongson.c

Thank you very much, but I prefer send()+recv().

Qunqin.

> index a4ec23639911..0e8eb8cee13c 100644
> --- a/drivers/char/tpm/tpm_loongson.c
> +++ b/drivers/char/tpm/tpm_loongson.c
> @@ -15,36 +15,35 @@ struct tpm_loongson_cmd {
>      u32 pad[5];
>  };
>
> -static int tpm_loongson_recv(struct tpm_chip *chip, u8 *buf, size_t 
> count)
> +static int tpm_loongson_send(struct tpm_chip *chip, u8 *buf, size_t 
> bufsiz,
> +                 size_t cmd_len)
>  {
>      struct loongson_se_engine *tpm_engine = dev_get_drvdata(&chip->dev);
> -    struct tpm_loongson_cmd *cmd_ret = tpm_engine->command_ret;
> -
> -    if (cmd_ret->data_len > count)
> -        return -EIO;
> +    struct tpm_loongson_cmd *cmd = tpm_engine->command;
> +    struct tpm_loongson_cmd *cmd_ret;
> +    int ret;
>
> -    memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len);
> +    if (cmd_len > tpm_engine->buffer_size)
> +        return -E2BIG;
>
> -    return cmd_ret->data_len;
> -}
> +    cmd->data_len = cmd_len;
> +    memcpy(tpm_engine->data_buffer, buf, cmd_len);
>
> -static int tpm_loongson_send(struct tpm_chip *chip, u8 *buf, size_t 
> count)
> -{
> -    struct loongson_se_engine *tpm_engine = dev_get_drvdata(&chip->dev);
> -    struct tpm_loongson_cmd *cmd = tpm_engine->command;
> +    ret = loongson_se_send_engine_cmd(tpm_engine);
> +    if (ret)
> +        return ret;
>
> -    if (count > tpm_engine->buffer_size)
> -        return -E2BIG;
> +    cmd_ret = tpm_engine->command_ret;
> +    if (cmd_ret->data_len > bufsiz)
> +        return -EIO;
>
> -    cmd->data_len = count;
> -    memcpy(tpm_engine->data_buffer, buf, count);
> +    memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len);
>
> -    return loongson_se_send_engine_cmd(tpm_engine);
> +    return cmd_ret->data_len;
>  }
>
>  static const struct tpm_class_ops tpm_loongson_ops = {
>      .flags = TPM_OPS_AUTO_STARTUP,
> -    .recv = tpm_loongson_recv,
>      .send = tpm_loongson_send,
>  };
>
> @@ -65,7 +64,7 @@ static int tpm_loongson_probe(struct platform_device 
> *pdev)
>      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;
> +    chip->flags = TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_SYNC;
>      dev_set_drvdata(&chip->dev, tpm_engine);
>
>      return tpm_chip_register(chip);
>
> Thanks,
> Stefano
>
>>
>> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
>> index dddd702b2..ba3924eb1 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 9de1b3ea3..5b5cdc0d3 100644
>> --- a/drivers/char/tpm/Makefile
>> +++ b/drivers/char/tpm/Makefile
>> @@ -46,3 +46,4 @@ obj-$(CONFIG_TCG_ARM_CRB_FFA) += tpm_crb_ffa.o
>> obj-$(CONFIG_TCG_VTPM_PROXY) += tpm_vtpm_proxy.o
>> obj-$(CONFIG_TCG_FTPM_TEE) += tpm_ftpm_tee.o
>> obj-$(CONFIG_TCG_SVSM) += tpm_svsm.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..a4ec23639
>> --- /dev/null
>> +++ b/drivers/char/tpm/tpm_loongson.c
>> @@ -0,0 +1,84 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/* Copyright (c) 2025 Loongson Technology Corporation Limited. */
>> +
>> +#include <linux/device.h>
>> +#include <linux/mfd/loongson-se.h>
>> +#include <linux/platform_device.h>
>> +#include <linux/wait.h>
>> +
>> +#include "tpm.h"
>> +
>> +struct tpm_loongson_cmd {
>> +    u32 cmd_id;
>> +    u32 data_off;
>> +    u32 data_len;
>> +    u32 pad[5];
>> +};
>> +
>> +static int tpm_loongson_recv(struct tpm_chip *chip, u8 *buf, size_t 
>> count)
>> +{
>> +    struct loongson_se_engine *tpm_engine = 
>> dev_get_drvdata(&chip->dev);
>> +    struct tpm_loongson_cmd *cmd_ret = tpm_engine->command_ret;
>> +
>> +    if (cmd_ret->data_len > count)
>> +        return -EIO;
>> +
>> +    memcpy(buf, tpm_engine->data_buffer, cmd_ret->data_len);
>> +
>> +    return cmd_ret->data_len;
>> +}
>> +
>> +static int tpm_loongson_send(struct tpm_chip *chip, u8 *buf, size_t 
>> count)
>> +{
>> +    struct loongson_se_engine *tpm_engine = 
>> dev_get_drvdata(&chip->dev);
>> +    struct tpm_loongson_cmd *cmd = tpm_engine->command;
>> +
>> +    if (count > tpm_engine->buffer_size)
>> +        return -E2BIG;
>> +
>> +    cmd->data_len = count;
>> +    memcpy(tpm_engine->data_buffer, buf, count);
>> +
>> +    return loongson_se_send_engine_cmd(tpm_engine);
>> +}
>> +
>> +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 loongson_se_engine *tpm_engine;
>> +    struct device *dev = &pdev->dev;
>> +    struct tpm_loongson_cmd *cmd;
>> +    struct tpm_chip *chip;
>> +
>> +    tpm_engine = loongson_se_init_engine(dev->parent, SE_ENGINE_TPM);
>> +    if (!tpm_engine)
>> +        return -ENODEV;
>> +    cmd = tpm_engine->command;
>> +    cmd->cmd_id = SE_CMD_TPM;
>> +    cmd->data_off = tpm_engine->buffer_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, tpm_engine);
>> +
>> +    return tpm_chip_register(chip);
>> +}
>> +
>> +static struct platform_driver tpm_loongson = {
>> +    .probe   = tpm_loongson_probe,
>> +    .driver  = {
>> +        .name  = "tpm_loongson",
>> +    },
>> +};
>> +module_platform_driver(tpm_loongson);
>> +
>> +MODULE_ALIAS("platform:tpm_loongson");
>> +MODULE_LICENSE("GPL");
>> +MODULE_DESCRIPTION("Loongson TPM driver");
>> -- 
>> 2.45.2
>>
>>


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ