From patchwork Tue May 5 07:33:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin GAIGNARD X-Patchwork-Id: 11528341 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 771D314C0 for ; Tue, 5 May 2020 07:34:54 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 4F91C20663 for ; Tue, 5 May 2020 07:34:54 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="r9Zi43QU"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=st.com header.i=@st.com header.b="1OT7YvsW" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4F91C20663 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=st.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=v3Zh6XfpBlCzqz1c5luEwiZPxUm3PYMyePb4A8mG6GU=; b=r9Zi43QUzKm1TO i4Ax+jLffQOYuSsY0PX8DubS4eW7/J8PRdD+H0IpdhU6SFgEx9XEKDngMxaMbshikPrzffsRigFG1 TjO/qmwfyZqFEt0mbpGGqtxAGDTuxn+DYUhUMKwzJFUV6jhHGYFWbNfKLdEZA9S+/6eZzh/iSmPEX 6wMvniosHJ/QmKt2VFo2uMYS7RUD8HzVsCgURoEIVsx/yEtNsEZAe0N88IJMoL1/fmkbBQVS9UYMH qWyxj2NpHvBPR7ekFuwrvVBrXKMOo0aYqGi6TxlhdyiR93FWOfG3E34pGDT/G5jxig9tn/2vixWsC loprfhXrsLMGeuNVe7+A==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1jVs6N-0004v9-VT; Tue, 05 May 2020 07:34:52 +0000 Received: from mx08-00178001.pphosted.com ([91.207.212.93] helo=mx07-00178001.pphosted.com) by bombadil.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1jVs5B-0003h7-DF for linux-arm-kernel@lists.infradead.org; Tue, 05 May 2020 07:33:40 +0000 Received: from pps.filterd (m0046661.ppops.net [127.0.0.1]) by mx07-00178001.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0457RbWX021776; Tue, 5 May 2020 09:33:27 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=st.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=STMicroelectronics; bh=RDeGUX1kaDX+4Hwp31TbURyze55b7oE/AwyPuMTSu6o=; b=1OT7YvsWKZAlV/D55kLNeqw5oNNK3w4Xi3WB1cz8MArOKqv4TdxnVbWpV8BquGucCA5+ /F8+nHl2APxJyMvrimqk+fmgkX9ZclkoZRGbTtBNzl9pHY2t7WxqXazDibBeICPH1JZC AkWq+iXBbzYS+ZZ7SyghMSUJvNnqiLGPxPSn2z6V+nRbyDOj4uDPSFnVQ7z/0msYqSK/ +SNXrH0X2RbmJ033uUA6hKefTLuq299rS96In9JcxCCN5JzAesnFV76UF93wriSqr4jh r1cJOf1J1PgxJaDjIU3DiqSaXvcsZolKbCShMIwHAQbO3zBqeAoIrRafk+GiUtWdmris UQ== Received: from beta.dmz-eu.st.com (beta.dmz-eu.st.com [164.129.1.35]) by mx07-00178001.pphosted.com with ESMTP id 30ryrj6f4j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 05 May 2020 09:33:27 +0200 Received: from euls16034.sgp.st.com (euls16034.sgp.st.com [10.75.44.20]) by beta.dmz-eu.st.com (STMicroelectronics) with ESMTP id 5507A10002A; Tue, 5 May 2020 09:33:27 +0200 (CEST) Received: from Webmail-eu.st.com (sfhdag3node3.st.com [10.75.127.9]) by euls16034.sgp.st.com (STMicroelectronics) with ESMTP id 4904D2A4927; Tue, 5 May 2020 09:33:27 +0200 (CEST) Received: from localhost (10.75.127.49) by SFHDAG3NODE3.st.com (10.75.127.9) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Tue, 5 May 2020 09:33:26 +0200 From: Benjamin Gaignard To: , , , , , Subject: [PATCH v3 2/5] bus: stm32: Introduce firewall controller helpers Date: Tue, 5 May 2020 09:33:05 +0200 Message-ID: <20200505073308.22914-3-benjamin.gaignard@st.com> X-Mailer: git-send-email 2.15.0 In-Reply-To: <20200505073308.22914-1-benjamin.gaignard@st.com> References: <20200505073308.22914-1-benjamin.gaignard@st.com> MIME-Version: 1.0 X-Originating-IP: [10.75.127.49] X-ClientProxiedBy: SFHDAG2NODE3.st.com (10.75.127.6) To SFHDAG3NODE3.st.com (10.75.127.9) X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.138, 18.0.676 definitions=2020-05-05_04:2020-05-04, 2020-05-05 signatures=0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20200505_003337_753095_2CC80228 X-CRM114-Status: GOOD ( 25.28 ) X-Spam-Score: -0.9 (/) X-Spam-Report: SpamAssassin version 3.4.4 on bombadil.infradead.org summary: Content analysis details: (-0.9 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at https://www.dnswl.org/, low trust [91.207.212.93 listed in list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Benjamin Gaignard , devicetree@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org The goal of these helpers are to offer an interface for the hardware blocks controlling bus accesses rights. Bus firewall controllers are typically used to control if a hardware block can perform read or write operations on bus. Smarter firewall controllers could be able to define accesses rights per hardware blocks to control where they can read or write. Firewall controller configurations are provided in device node, parsed by the helpers and send to the driver to apply them. Each controller may need different number and type of inputs to configure the firewall so device-tree properties size have to be define by using "#firewall-cells". Firewall configurations properties have to be named "firewall-X" on device node. "firewall-names" keyword can also be used to give a name to a specific configuration. Example of device-tree: ctrl0: firewall@0 { #firewall-cells = <2>; }; foo: foo@0 { firewall-names = "default", "setting1"; firewall-0 = <&ctrl0 1 2>; firewall-1 = <&ctrl0 3 4>; }; Configurations could be applied with functions like firewall_set_config_by_index() or firewall_set_config_by_name(). firewall_set_default_config() function will apply the configuration named "default" (if existing) or the configuration with index 0 (i.e. firewall-0). Drivers could register/unregister themselves be calling firewall_register/firewall_unregister functions. Signed-off-by: Benjamin Gaignard --- drivers/bus/Kconfig | 2 + drivers/bus/Makefile | 2 + drivers/bus/stm32/Kconfig | 3 + drivers/bus/stm32/Makefile | 1 + drivers/bus/stm32/firewall.c | 266 +++++++++++++++++++++++++++++++++++++++++++ drivers/bus/stm32/firewall.h | 75 ++++++++++++ 6 files changed, 349 insertions(+) create mode 100644 drivers/bus/stm32/Kconfig create mode 100644 drivers/bus/stm32/Makefile create mode 100644 drivers/bus/stm32/firewall.c create mode 100644 drivers/bus/stm32/firewall.h diff --git a/drivers/bus/Kconfig b/drivers/bus/Kconfig index 6d4e4497b59b..843b356322d9 100644 --- a/drivers/bus/Kconfig +++ b/drivers/bus/Kconfig @@ -203,4 +203,6 @@ config DA8XX_MSTPRI source "drivers/bus/fsl-mc/Kconfig" source "drivers/bus/mhi/Kconfig" +source "drivers/bus/stm32/Kconfig" + endmenu diff --git a/drivers/bus/Makefile b/drivers/bus/Makefile index 05f32cd694a4..5e0e34b10235 100644 --- a/drivers/bus/Makefile +++ b/drivers/bus/Makefile @@ -37,3 +37,5 @@ obj-$(CONFIG_DA8XX_MSTPRI) += da8xx-mstpri.o # MHI obj-$(CONFIG_MHI_BUS) += mhi/ + +obj-$(CONFIG_MACH_STM32MP157) += stm32/ \ No newline at end of file diff --git a/drivers/bus/stm32/Kconfig b/drivers/bus/stm32/Kconfig new file mode 100644 index 000000000000..57221e833e2d --- /dev/null +++ b/drivers/bus/stm32/Kconfig @@ -0,0 +1,3 @@ +config FIREWALL_CONTROLLERS + bool "Support of bus firewall controllers" + depends on OF diff --git a/drivers/bus/stm32/Makefile b/drivers/bus/stm32/Makefile new file mode 100644 index 000000000000..eb6b978d6450 --- /dev/null +++ b/drivers/bus/stm32/Makefile @@ -0,0 +1 @@ +obj-$(CONFIG_FIREWALL_CONTROLLERS) += firewall.o diff --git a/drivers/bus/stm32/firewall.c b/drivers/bus/stm32/firewall.c new file mode 100644 index 000000000000..95f716cf926f --- /dev/null +++ b/drivers/bus/stm32/firewall.c @@ -0,0 +1,266 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) STMicroelectronics 2020 - All Rights Reserved + * Author: Benjamin Gaignard for STMicroelectronics. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "firewall.h" + +/* Mutex taken to protect firewall_list */ +static DEFINE_MUTEX(firewall_list_mutex); + +/* Global list of firewall control devices */ +static LIST_HEAD(firewall_list); + +struct firewall_ctrl { + struct list_head list; + struct firewall_ops *ops; + struct device_node *np; + void *priv; +}; + +static struct firewall_ctrl *get_firewallctrl_from_node(struct device_node *np) +{ + struct firewall_ctrl *ctrl; + + mutex_lock(&firewall_list_mutex); + + list_for_each_entry(ctrl, &firewall_list, list) { + if (ctrl->np == np) { + mutex_unlock(&firewall_list_mutex); + return ctrl; + } + } + + mutex_unlock(&firewall_list_mutex); + + return NULL; +} + +/** + * firewall_dt_has_default + * + * Check if the device node provide firewall configuration + * + * @np: device node with possible firewall configuration + * + * Return: true is firewall-0 property exist in the device node + */ +static bool firewall_dt_has_default(struct device_node *np) +{ + struct property *prop; + int size; + + if (!np) + return false; + + prop = of_find_property(np, "firewall-0", &size); + + return prop ? true : false; +} + +/** + * firewall_set_config_by_index + * + * Set a firewall controller configuration based on given index. + * + * @np: device node with firewall configuration to apply. + * @index: the index of the configuration in device node. + * + * Return: 0 if OK, -EPROBE_DEFER if waiting for firewall controller to be + * registered or negative value on other errors. + */ +int firewall_set_config_by_index(struct device_node *np, int index) +{ + char *propname; + int configs, i, err = 0; + + if (!np) + return 0; + + propname = kasprintf(GFP_KERNEL, "firewall-%d", index); + configs = of_count_phandle_with_args(np, propname, "#firewall-cells"); + if (configs < 0) { + err = -EINVAL; + goto error; + } + + for (i = 0; i < configs; i++) { + struct firewall_ctrl *ctrl; + struct of_phandle_args args; + + err = of_parse_phandle_with_args(np, propname, + "#firewall-cells", + i, &args); + if (err) + goto error; + + /* Test if the controller is (or will be) available */ + if (!of_device_is_available(args.np)) { + of_node_put(args.np); + continue; + } + + ctrl = get_firewallctrl_from_node(args.np); + of_node_put(args.np); + + /* Controller is not yet registered */ + if (!ctrl) { + err = -EPROBE_DEFER; + goto error; + } + + err = ctrl->ops->set_config(ctrl->priv, &args); + if (err) + goto error; + } + +error: + kfree(propname); + return err; +} +EXPORT_SYMBOL_GPL(firewall_set_config_by_index); + +/** + * firewall_set_config_by_name + * + * Set a firwall controller configuration based on given name. + * + * @np: device node with firewall configuration to apply. + * @name: the name of the configuration in device node. + * + * Return: 0 if OK, -EPROBE_DEFER if waiting for firewall controller to be + * registered or negative value on other errors. + */ +int firewall_set_config_by_name(struct device_node *np, char *name) +{ + const char *configname; + int count, i; + + count = of_property_count_strings(np, "firewall-names"); + for (i = 0; i < count; i++) { + int err; + + err = of_property_read_string_index(np, + "firewall-names", + i, &configname); + if (err) + return err; + + if (strcmp(name, configname)) + continue; + + return firewall_set_config_by_index(np, i); + } + + return -EINVAL; +} +EXPORT_SYMBOL_GPL(firewall_set_config_by_name); + +/** + * firewall_set_default_config + * + * Set the default configuration for device. + * First try to apply configuration named "default", if it fails + * or doesn't exist, try to apply firewall-0 configuration. + * + * @np: device node with firewall configuration to apply. + * + * Return: 0 if OK, -EPROBE_DEFER if waiting for firewall controller to be + * registered or negative value on other errors. + */ +int firewall_set_default_config(struct device_node *np) +{ + int ret; + + /* Nothing to do if device node doesn't contain at least + * one configuration + */ + if (!firewall_dt_has_default(np)) + return 0; + + ret = firewall_set_config_by_name(np, "default"); + if (!ret || (ret == -EPROBE_DEFER)) + return ret; + + return firewall_set_config_by_index(np, 0); +} +EXPORT_SYMBOL_GPL(firewall_set_default_config); + +/** + * firewall_register + * + * Register a firewall controller. + * + * @np: node implementing firewall controller. + * @ops: firewall controller operations. + * @priv: caller private data. + * + * Return: 0 if OK or negative value on error. + */ +int firewall_register(struct device_node *np, + struct firewall_ops *ops, + void *priv) +{ + struct firewall_ctrl *ctrl; + + if (!np || !ops || !ops->set_config) + return -EINVAL; + + ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL); + if (!ctrl) + return -ENOMEM; + + INIT_LIST_HEAD(&ctrl->list); + + ctrl->np = np; + ctrl->ops = ops; + ctrl->priv = priv; + + mutex_lock(&firewall_list_mutex); + list_add_tail(&ctrl->list, &firewall_list); + mutex_unlock(&firewall_list_mutex); + + return 0; +} +EXPORT_SYMBOL_GPL(firewall_register); + +/** + * firewall_unregister + * + * Unregister a firewall controller. + * + * @np: node implementing firewall controller. + */ +void firewall_unregister(struct device_node *np) +{ + struct firewall_ctrl *ctrl; + + ctrl = get_firewallctrl_from_node(np); + if (!ctrl) + return; + + mutex_lock(&firewall_list_mutex); + list_del(&ctrl->list); + mutex_unlock(&firewall_list_mutex); + + kfree(ctrl); +} +EXPORT_SYMBOL_GPL(firewall_unregister); + +static int __init firewall_init(void) +{ + pr_info("initialized bus firewall controller subsystem\n"); + return 0; +} + +/* Init early since drivers really need to configure firewall early */ +core_initcall(firewall_init); diff --git a/drivers/bus/stm32/firewall.h b/drivers/bus/stm32/firewall.h new file mode 100644 index 000000000000..d6fd720a229e --- /dev/null +++ b/drivers/bus/stm32/firewall.h @@ -0,0 +1,75 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) STMicroelectronics 2020 - All Rights Reserved + * Author: Benjamin Gaignard for STMicroelectronics. + */ + +#ifndef _FIREWALL_H_ +#define _FIREWALL_H_ + +#include +#include + +/** + * struct firewall_ops + * + * Firewall controller operations structure to be filled by drivers. + */ +struct firewall_ops { + /** + * @set_config: + * + * Driver callback to set a firewall configuration on a controller. + * Configuration arguments are provided in out_args parameter. + * + * Return: 0 on success, a negative error code on failure. + */ + int (*set_config)(void *priv, struct of_phandle_args *out_args); +}; + +#ifdef CONFIG_FIREWALL_CONTROLLERS + +int firewall_set_config_by_index(struct device_node *np, int index); +int firewall_set_config_by_name(struct device_node *np, char *name); +int firewall_set_default_config(struct device_node *np); + +int firewall_register(struct device_node *np, + struct firewall_ops *ops, + void *priv); + +void firewall_unregister(struct device_node *np); + +#else + +static inline int firewall_set_config_by_index(struct device_node *np, + int index) +{ + return 0; +} + +static inline int firewall_set_config_by_name(struct device_node *np, + char *name) +{ + return 0; +} + +static inline int firewall_set_default_config(struct device_node *np) +{ + return 0; +} + +static inline int firewall_register(struct device_node *np, + struct firewall_ops *ops, + void *priv) +{ + return 0; +} + +static inline void firewall_unregister(struct device_node *np) +{ + /* Empty */ +} + +#endif + +#endif /* _FIREWALL_H_ */