From patchwork Mon May 23 16:30:21 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Carlo Caione X-Patchwork-Id: 9132145 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 678F3607D3 for ; Mon, 23 May 2016 16:33:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5CB8A28238 for ; Mon, 23 May 2016 16:33:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 515422823D; Mon, 23 May 2016 16:33:38 +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 A2B022823A for ; Mon, 23 May 2016 16:33:37 +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 1b4sm9-0003qm-G3; Mon, 23 May 2016 16:32:17 +0000 Received: from mail-wm0-x243.google.com ([2a00:1450:400c:c09::243]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1b4slx-0003c7-B8; Mon, 23 May 2016 16:32:08 +0000 Received: by mail-wm0-x243.google.com with SMTP id f75so10410538wmf.2; Mon, 23 May 2016 09:31:46 -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=I1xvFKUQBXkm9vHt7p2G7gOq7EPAvTJK8MJWT/4lSmQ=; b=VEUVM3tNgLSyryBvDuA9LiKqRplltAaA8cB9aZkViJJUs0bF4tuPYOS6iL31pcHkRq XPnPqeUDe/35O/0rvf4lyVz8wUVp3bdR/eyJVbFIzumyfldojqU6a9EzDyClKlsr+6Co Chne3mR+tSR2CtLIwdeWvsAQ1Wc6swc2jkmQojWCdBtsbLxxOsI2DUTUrBMf9YeZ5ea6 as0b+cCE3U5oxOejA/Fza2tTWVll9y/jUwLisG9fqBggILgYXoKfK8nuHLHgbYhEDQ+X Lq4j2GowgQMN2LDTMXdrwLjBhm1D4nDaKNhDl4XcqbRSexXONPGFzKvay9AZvEKx1rMD q+Eg== 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=I1xvFKUQBXkm9vHt7p2G7gOq7EPAvTJK8MJWT/4lSmQ=; b=X37b/QVus9RnAhoMCjZb6xKpku0y2JWSFrwEgepLiIWktptvKTuzSUBqTzkjQCZftN 6fiTUKDtYKqQRPd+b6mig/7lqAw9+tcO1jQOSBGb9bFdUQRWfAMfD8nOkxB3E4JwfUrw 86cIF38zXQMVPIuajf5I/MPZoRci9dpTxf35m3/S763HU/0tM4ojQmcB0IsFCVTWTkWd W0EqrKyRAqZrMWuEoJRJvtfhzxeN82czAdKZ/DtsplZGVV5UHxsIgB+sTk0avwztiQhd 5nJEn8MD8xsoso6IP3+HbqbIDDNA4h63Yj28ua/iKql/5RYxBuItWp7Q8vzK0NuZ4bB8 b1yg== X-Gm-Message-State: AOPr4FWzvCcdaA1KFHeg0ai4TVyK50N7cG+aJFAOqiLZKvzGnF5Vok4sbNOUw2/Bg0XSjw== X-Received: by 10.194.97.16 with SMTP id dw16mr20122104wjb.114.1464021104924; Mon, 23 May 2016 09:31:44 -0700 (PDT) Received: from localhost.localdomain (2-238-57-164.ip242.fastwebnet.it. [2.238.57.164]) by smtp.gmail.com with ESMTPSA id o129sm14483647wmb.17.2016.05.23.09.31.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 23 May 2016 09:31:44 -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, afaerber@suse.de, arnd@arndb.de, jens.wiklander@linaro.org, mark.rutland@arm.com, robh+dt@kernel.org, matthias.bgg@gmail.com Subject: [PATCH v3 1/4] firmware: Amlogic: Add secure monitor driver Date: Mon, 23 May 2016 18:30:21 +0200 Message-Id: <1464021024-29380-2-git-send-email-carlo@caione.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1464021024-29380-1-git-send-email-carlo@caione.org> References: <1464021024-29380-1-git-send-email-carlo@caione.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20160523_093205_727707_EA05CFAC X-CRM114-Status: GOOD ( 21.16 ) 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 | 179 ++++++++++++++++++++++++++++++++ include/linux/firmware/meson/meson_sm.h | 38 +++++++ 6 files changed, 228 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..445bc7f --- /dev/null +++ b/drivers/firmware/meson/meson_sm.c @@ -0,0 +1,179 @@ +/* + * 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 + +#define SM_MEM_SIZE 0x1000 + +struct meson_sm_data { + u32 cmd; + u32 arg0; + u32 arg1; + u32 arg2; + u32 arg3; + u32 arg4; + u32 ret; +}; + +static void __meson_sm_call(void *info) +{ + struct meson_sm_data *data = info; + struct arm_smccc_res res; + + arm_smccc_smc(data->cmd, + data->arg0, data->arg1, data->arg2, + data->arg3, data->arg4, 0, 0, &res); + data->ret = res.a0; +} + +u32 meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2, + u32 arg3, u32 arg4) +{ + struct meson_sm_data data; + + data.cmd = cmd; + data.arg0 = arg0; + data.arg1 = arg1; + data.arg2 = arg2; + data.arg3 = arg3; + data.arg4 = arg4; + data.ret = 0; + + __meson_sm_call(&data); + + return data.ret; +} +EXPORT_SYMBOL(meson_sm_call); + +u32 meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer, + u32 cmd, u32 arg0, u32 arg1, u32 arg2, + u32 arg3, u32 arg4) +{ + u32 size; + + if (!fw) + return -EINVAL; + + size = meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4); + + if (!size || size > SM_MEM_SIZE) + return -EINVAL; + + memcpy(buffer, fw->sm_sharemem_out_base, size); + return size; +} +EXPORT_SYMBOL(meson_sm_call_read); + +u32 meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer, + unsigned int b_size, u32 cmd, u32 arg0, + u32 arg1, u32 arg2, u32 arg3, u32 arg4) +{ + u32 size; + + if (!fw) + return -EINVAL; + + if (b_size > SM_MEM_SIZE) + return -EINVAL; + + memcpy(fw->sm_sharemem_in_base, buffer, b_size); + + size = meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4); + + if (!size) + return -EINVAL; + + return size; +} +EXPORT_SYMBOL(meson_sm_call_write); + +static int meson_sm_probe(struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct meson_sm_firmware *fw; + u32 sm_phy_in_base, sm_phy_out_base; + int cmd_in, cmd_out; + + fw = devm_kzalloc(&pdev->dev, sizeof(*fw), GFP_KERNEL); + if (!fw) + return -ENOMEM; + + fw->dev = &pdev->dev; + + if (of_property_read_u32(np, "amlogic,sm-cmd-input-base", &cmd_in)) + return -EINVAL; + + if (of_property_read_u32(np, "amlogic,sm-cmd-output-base", &cmd_out)) + return -EINVAL; + + sm_phy_in_base = meson_sm_call(cmd_in, 0, 0, 0, 0, 0); + fw->sm_sharemem_in_base = ioremap_cache(sm_phy_in_base, SM_MEM_SIZE); + if (!fw->sm_sharemem_in_base) + return -EINVAL; + + sm_phy_out_base = meson_sm_call(cmd_out, 0, 0, 0, 0, 0); + fw->sm_sharemem_out_base = ioremap_cache(sm_phy_out_base, SM_MEM_SIZE); + if (!fw->sm_sharemem_out_base) { + iounmap(fw->sm_sharemem_in_base); + return -EINVAL; + } + + platform_set_drvdata(pdev, fw); + + return 0; +} + +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-sm" }, + { /* sentinel */}, +}; +MODULE_DEVICE_TABLE(of, meson_sm_ids); + +static struct platform_driver meson_sm_platform_driver = { + .probe = meson_sm_probe, + .driver = { + .name = "secmon", + .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..7b6b524 --- /dev/null +++ b/include/linux/firmware/meson/meson_sm.h @@ -0,0 +1,38 @@ +/* + * 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_H_ +#define _MESON_SM_H_ + +#include +#include + +struct meson_sm_firmware { + struct device *dev; + void __iomem *sm_sharemem_in_base; + void __iomem *sm_sharemem_out_base; +}; + +u32 meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2, + u32 arg3, u32 arg4); + +u32 meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer, + unsigned int b_size, u32 cmd, u32 arg0, + u32 arg1, u32 arg2, u32 arg3, u32 arg4); + +u32 meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer, + u32 cmd, 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_H_ */