From patchwork Thu Jun 2 14:16:02 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Caione X-Patchwork-Id: 9150069 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 6286860751 for ; Thu, 2 Jun 2016 14:18:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5175828047 for ; Thu, 2 Jun 2016 14:18:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 46095282E8; Thu, 2 Jun 2016 14:18:31 +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=-4.1 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_MED, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 95BCD28047 for ; Thu, 2 Jun 2016 14:18:30 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1b8TQm-0007i7-IZ; Thu, 02 Jun 2016 14:17:04 +0000 Received: from mail-pf0-x243.google.com ([2607:f8b0:400e:c00::243]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b8TQP-0007Af-Qz; Thu, 02 Jun 2016 14:16:49 +0000 Received: by mail-pf0-x243.google.com with SMTP id b124so8213149pfb.0; Thu, 02 Jun 2016 07:16:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=S6KF3p28CZ8fijNbA6W5Iqg6GUdd1ygdu9h6doyPdKs=; b=jBRuHG1CR43YvDJdLOvoOKQdctvXx76KCFG75KGEP2hLePvnoUkd3srpGXOur6umjG 6oPWg1VJdEQf7OjZ+OwjIhyCvcDVnG2IErRkGCgTsrTaro70OoKctFVNB7qEZiBOFW/F eveWM4IQhW5Eqv7qyc70fI/F1+fxB74DfsPwe/lxDdVVC+rczNA1y5qMcPdY1/ZTBcSx CcQ3u9s9wbF2Zf+OW/yWRSg18fM3IV1K56HruQYGzI6pRUSdQPrVqra7aCjTW1tEEyrw gXCXHpNM8QUI9k2p4r7FuiRgsKYvM7YA+LJQMq9WRT8r7LLWbrXOg2f0C53UwNXGP3OG xZMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references; bh=S6KF3p28CZ8fijNbA6W5Iqg6GUdd1ygdu9h6doyPdKs=; b=H3q8UnNJTmodOvU+6JoCXLyrvOqiuvOsxw8xTuTUMjGAVJ8xZHdV9Ii6Xd18q5P+o2 cQYmCTa6pTarZUZkaePQJJnXvFcMdonP7+JWJ0NM2DzxMXFWQ6mII0pmjsgtvGpdEfl+ HYs4wmkDdNCgdpwFOL1iHD3SwS0QOiVtSqP1zawOteKGTe5YoxWsj5tw3P1D3B1uzD8p Hx7jX5W3Tsq8ffDCRrJQJZUzgk4aDb/zQS+9rUzki/YnUUxU5U2VYusKAs1aJxRAoUIS lHUt6YMdaEhfqilKDoPkIKAB1Dfhtt/4H8RxizywvFOlEl/7NabMq6P2xDTyApPmjH+V Y0uA== X-Gm-Message-State: ALyK8tIZ0Rwu5kUXklRCDtv3UiBg6zRTFuysOvLlp3y8CGU6a/z6sOduRkyuLBNnnDCuEg== X-Received: by 10.98.10.19 with SMTP id s19mr4832298pfi.71.1464876980815; Thu, 02 Jun 2016 07:16:20 -0700 (PDT) Received: from mephisto.endlessm-sf.com (2-238-57-164.ip242.fastwebnet.it. [2.238.57.164]) by smtp.gmail.com with ESMTPSA id t77sm1292347pfa.71.2016.06.02.07.16.16 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 02 Jun 2016 07:16:19 -0700 (PDT) From: Carlo Caione To: linux-arm-kernel@lists.infradead.org, linux-amlogic@lists.infradead.org, linux@endlessm.com, devicetree@vger.kernel.org, khilman@baylibre.com, mark.rutland@arm.com, robh+dt@kernel.org, matthias.bgg@gmail.com, narmstrong@baylibre.com, bjdooks@googlemail.com Subject: [PATCH v6 1/3] firmware: Amlogic: Add secure monitor driver Date: Thu, 2 Jun 2016 16:16:02 +0200 Message-Id: <1464876964-13001-2-git-send-email-carlo@caione.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1464876964-13001-1-git-send-email-carlo@caione.org> References: <1464876964-13001-1-git-send-email-carlo@caione.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160602_071642_137316_BD16904D X-CRM114-Status: GOOD ( 23.53 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Carlo Caione MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP From: Carlo Caione Introduce a driver to provide calls into secure monitor mode. In the Amlogic SoCs these calls are used for multiple reasons: access to NVMEM, set USB boot, enable JTAG, etc... Signed-off-by: Carlo Caione --- drivers/firmware/Kconfig | 1 + drivers/firmware/Makefile | 1 + drivers/firmware/meson/Kconfig | 8 + drivers/firmware/meson/Makefile | 1 + drivers/firmware/meson/meson_sm.c | 283 ++++++++++++++++++++++++++++++++ include/linux/firmware/meson/meson_sm.h | 32 ++++ 6 files changed, 326 insertions(+) create mode 100644 drivers/firmware/meson/Kconfig create mode 100644 drivers/firmware/meson/Makefile create mode 100644 drivers/firmware/meson/meson_sm.c create mode 100644 include/linux/firmware/meson/meson_sm.h diff --git a/drivers/firmware/Kconfig b/drivers/firmware/Kconfig index 6664f11..686e395 100644 --- a/drivers/firmware/Kconfig +++ b/drivers/firmware/Kconfig @@ -199,5 +199,6 @@ config HAVE_ARM_SMCCC source "drivers/firmware/broadcom/Kconfig" source "drivers/firmware/google/Kconfig" source "drivers/firmware/efi/Kconfig" +source "drivers/firmware/meson/Kconfig" endmenu diff --git a/drivers/firmware/Makefile b/drivers/firmware/Makefile index 474bada..fc4bb09 100644 --- a/drivers/firmware/Makefile +++ b/drivers/firmware/Makefile @@ -21,6 +21,7 @@ obj-$(CONFIG_QCOM_SCM_32) += qcom_scm-32.o CFLAGS_qcom_scm-32.o :=$(call as-instr,.arch armv7-a\n.arch_extension sec,-DREQUIRES_SEC=1) -march=armv7-a obj-y += broadcom/ +obj-y += meson/ obj-$(CONFIG_GOOGLE_FIRMWARE) += google/ obj-$(CONFIG_EFI) += efi/ obj-$(CONFIG_UEFI_CPER) += efi/ diff --git a/drivers/firmware/meson/Kconfig b/drivers/firmware/meson/Kconfig new file mode 100644 index 0000000..fff11a3 --- /dev/null +++ b/drivers/firmware/meson/Kconfig @@ -0,0 +1,8 @@ +# +# Amlogic Secure Monitor driver +# +config MESON_SM + bool + default ARCH_MESON + help + Say y here to enable the Amlogic secure monitor driver diff --git a/drivers/firmware/meson/Makefile b/drivers/firmware/meson/Makefile new file mode 100644 index 0000000..9ab3884 --- /dev/null +++ b/drivers/firmware/meson/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_MESON_SM) += meson_sm.o diff --git a/drivers/firmware/meson/meson_sm.c b/drivers/firmware/meson/meson_sm.c new file mode 100644 index 0000000..97f10c0 --- /dev/null +++ b/drivers/firmware/meson/meson_sm.c @@ -0,0 +1,283 @@ +/* + * Amlogic Secure Monitor driver + * + * Copyright (C) 2016 Endless Mobile, Inc. + * Author: Carlo Caione + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct meson_sm_cmd { + unsigned int index; + u32 smc_id; +}; +#define CMD(d, s) { .index = (d), .smc_id = (s), } + +struct meson_sm_chip { + unsigned int shmem_size; + u32 cmd_shmem_in_base; + u32 cmd_shmem_out_base; + struct meson_sm_cmd cmd[]; +}; + +struct meson_sm_firmware { + struct device *dev; + const struct meson_sm_chip *chip; + void __iomem *sm_shmem_in_base; + void __iomem *sm_shmem_out_base; +}; + +struct meson_sm_chip gxbb_chip = { + .shmem_size = 0x1000, + .cmd_shmem_in_base = 0x82000020, + .cmd_shmem_out_base = 0x82000021, + .cmd = { + CMD(SM_EFUSE_READ, 0x82000030), + CMD(SM_EFUSE_WRITE, 0x82000031), + CMD(SM_EFUSE_USER_MAX, 0x82000033), + { /* sentinel */ }, + }, +}; + +static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip, unsigned int cmd_index) +{ + unsigned int i; + + for (i = 0; chip->cmd[i].smc_id; i++) + if (chip->cmd[i].index == cmd_index) + return chip->cmd[i].smc_id; + + return 0; +} + +static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4) +{ + struct arm_smccc_res res; + + arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res); + return res.a0; +} + +static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size) +{ + u32 sm_phy_base; + + sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0); + if (!sm_phy_base) + return 0; + + return ioremap_cache(sm_phy_base, size); +} + +/** + * meson_sm_call - generic SMC32 call to the secure-monitor + * + * @fw: Meson secure-monitor firmware pointer + * @cmd_index: Index of the SMC32 function ID + * @ret: Returned value + * @arg0: SMC32 Argument 0 + * @arg1: SMC32 Argument 1 + * @arg2: SMC32 Argument 2 + * @arg3: SMC32 Argument 3 + * @arg4: SMC32 Argument 4 + * + * Return: 0 on success, a negative value on error + */ +int meson_sm_call(struct meson_sm_firmware *fw, unsigned int cmd_index, + u32 *ret, u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4) +{ + u32 cmd, lret; + + if (!fw) + return -EINVAL; + + cmd = meson_sm_get_cmd(fw->chip, cmd_index); + if (!cmd) + return -EINVAL; + + lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4); + + if (ret) + *ret = lret; + + return 0; +} +EXPORT_SYMBOL(meson_sm_call); + +/** + * meson_sm_call_read - retrieve data from secure-monitor + * + * @fw: Meson secure-monitor firmware pointer + * @buffer: Buffer to store the retrieved data + * @cmd_index: Index of the SMC32 function ID + * @arg0: SMC32 Argument 0 + * @arg1: SMC32 Argument 1 + * @arg2: SMC32 Argument 2 + * @arg3: SMC32 Argument 3 + * @arg4: SMC32 Argument 4 + * + * Return: size of read data on success, a negative value on error + */ +int meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer, + unsigned int cmd_index, u32 arg0, u32 arg1, + u32 arg2, u32 arg3, u32 arg4) +{ + u32 size; + + if (!fw->chip->cmd_shmem_out_base) + return -EINVAL; + + if (meson_sm_call(fw, cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0) + return -EINVAL; + + if (!size || size > fw->chip->shmem_size) + return -EINVAL; + + if (buffer) + memcpy(buffer, fw->sm_shmem_out_base, size); + + return size; +} +EXPORT_SYMBOL(meson_sm_call_read); + +/** + * meson_sm_call_write - send data to secure-monitor + * + * @fw: Meson secure-monitor firmware pointer + * @buffer: Buffer containing data to send + * @b_size: Size of the data to send + * @cmd_index: Index of the SMC32 function ID + * @arg0: SMC32 Argument 0 + * @arg1: SMC32 Argument 1 + * @arg2: SMC32 Argument 2 + * @arg3: SMC32 Argument 3 + * @arg4: SMC32 Argument 4 + * + * Return: size of sent data on success, a negative value on error + */ +int meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer, + unsigned int b_size, unsigned int cmd_index, + u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4) +{ + u32 size; + + if (b_size > fw->chip->shmem_size) + return -EINVAL; + + if (!fw->chip->cmd_shmem_in_base) + return -EINVAL; + + memcpy(fw->sm_shmem_in_base, buffer, b_size); + + if (meson_sm_call(fw, cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0) + return -EINVAL; + + if (!size) + return -EINVAL; + + return size; +} +EXPORT_SYMBOL(meson_sm_call_write); + +static int meson_sm_probe(struct platform_device *pdev) +{ + const struct meson_sm_chip *chip; + struct meson_sm_firmware *fw; + + fw = devm_kzalloc(&pdev->dev, sizeof(*fw), GFP_KERNEL); + if (!fw) + return -ENOMEM; + + fw->dev = &pdev->dev; + + chip = of_device_get_match_data(fw->dev); + if (!chip) { + dev_err(fw->dev, "Unable to setup secure-monitor data\n"); + return -EINVAL; + } + + if (chip->cmd_shmem_in_base) { + fw->sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base, + chip->shmem_size); + if (WARN_ON(!fw->sm_shmem_in_base)) + goto out; + } + + if (chip->cmd_shmem_out_base) { + fw->sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base, + chip->shmem_size); + if (WARN_ON(!fw->sm_shmem_out_base)) + goto out; + } + + fw->chip = chip; + platform_set_drvdata(pdev, fw); + + dev_info(fw->dev, "secure-monitor enabled\n"); + + return 0; + +out: + if (fw->sm_shmem_in_base) + iounmap(fw->sm_shmem_in_base); + + return -EINVAL; +} + +/** + * meson_sm_get_fw - get Meson firmware struct + * + * @firmware_node: Device node of the secure-monitor + * + * Return: Meson secure-monitor firmware struct on success, NULL on error + */ +struct meson_sm_firmware *meson_sm_get_fw(struct device_node *firmware_node) +{ + struct platform_device *pdev = of_find_device_by_node(firmware_node); + + /* + * Returns NULL is the firmware device is not ready. + */ + if (!pdev) + return NULL; + + return platform_get_drvdata(pdev); +} +EXPORT_SYMBOL(meson_sm_get_fw); + +static const struct of_device_id meson_sm_ids[] = { + { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip }, + { /* sentinel */}, +}; +MODULE_DEVICE_TABLE(of, meson_sm_ids); + +static struct platform_driver meson_sm_platform_driver = { + .probe = meson_sm_probe, + .driver = { + .name = "secure-monitor", + .of_match_table = meson_sm_ids, + }, +}; +module_platform_driver(meson_sm_platform_driver); + +MODULE_AUTHOR("Carlo Caione "); +MODULE_DESCRIPTION("Amlogic secure monitor driver"); +MODULE_LICENSE("GPL"); diff --git a/include/linux/firmware/meson/meson_sm.h b/include/linux/firmware/meson/meson_sm.h new file mode 100644 index 0000000..863b231 --- /dev/null +++ b/include/linux/firmware/meson/meson_sm.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2016 Endless Mobile, Inc. + * Author: Carlo Caione + * + * 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. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _MESON_SM_FW_H_ +#define _MESON_SM_FW_H_ + +#define SM_EFUSE_READ 0 +#define SM_EFUSE_WRITE 1 +#define SM_EFUSE_USER_MAX 2 + +struct meson_sm_firmware; + +int meson_sm_call(struct meson_sm_firmware *fw, unsigned int cmd_index, + u32 *ret, u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4); +int meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer, + unsigned int b_size, unsigned int cmd_index, + u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4); +int meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer, + unsigned int cmd_index, u32 arg0, u32 arg1, + u32 arg2, u32 arg3, u32 arg4); +struct meson_sm_firmware *meson_sm_get_fw(struct device_node *firmware_node); + +#endif /* _MESON_SM_FW_H_ */