Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1104679pxu; Wed, 6 Jan 2021 12:22:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRfFLSqDtPIbFZU6kfu/wm1zMaK/Weu7JuJlckQMVXoKmCDITMRME0aZtZp1JHFNqVH7Al X-Received: by 2002:aa7:dac5:: with SMTP id x5mr5249278eds.198.1609964576165; Wed, 06 Jan 2021 12:22:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609964576; cv=none; d=google.com; s=arc-20160816; b=SgF6UGq6WQzO5g8QBzpcYonnTB8w+07DZDzouPUludQIAfQdI4IAi8UJ19lvMU9Unj aVUdDHZH77SIwSaVFICwxJGuA3RvHILMpyuKE32RtpVKF5nLl3lJ0HGtRNBTtYs83aot aHkv3udoSh0QjjXdlhjuSg/IvVJlGh3krlfHaKagIII7dSlhrD6iJ71dZXRqFBOwpMJf VAO3q6h8d8QJu6wubvusJssHQjdnr+Nu34SOeCCfyiVT3OJSUxUJ7Pz0O0wSQ+kBl9y1 g2HRUFy/SVji5gRCk5f2zAHByxPVMHyx6DOPoThggy8N9iR2nxjoMGgfqmSBFMdOta5w FsaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=FSG2Yqw2tnbHbEbykJ5YCtZVHoCBV9iWQq9S6z+mQy0=; b=bmAQcvmJ1bb1Bidqp2a1aV0w7vkFPCEr1IAxATmi0WhnaVfvESbCSoNJcLTXPnY7Jv fhUnX3/SrD3nZH+L5jnpJJuMOoCo2Vm3/YCcYz6VNrr4A1wrr9ICTNhuXlybm1rpPtdw xYvxSI+bDfKKCgMr64eNWTE0VSTcv0OkJKBqUYDhOGl22o6YKYwAC6++O9LrynWXj4G4 J8rinworBZxQZ/SFY03z5EaHew/jpgIM45ni6rmQk0d8wKUMz22QWs+wZc5mi7WvHEOs YvhDNqmiyae3AHQjmwx4qZlcoVy9zAxGq4PgN7NKogv/9g/6/D9Q5B/LqTVhhL2F5WGf patw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x9si1407846eje.109.2021.01.06.12.22.32; Wed, 06 Jan 2021 12:22:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727788AbhAFUTG (ORCPT + 99 others); Wed, 6 Jan 2021 15:19:06 -0500 Received: from foss.arm.com ([217.140.110.172]:46940 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727766AbhAFUTE (ORCPT ); Wed, 6 Jan 2021 15:19:04 -0500 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6D218139F; Wed, 6 Jan 2021 12:18:26 -0800 (PST) Received: from e120937-lin.home (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2F9B23F719; Wed, 6 Jan 2021 12:18:24 -0800 (PST) From: Cristian Marussi To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: sudeep.holla@arm.com, lukasz.luba@arm.com, james.quinlan@broadcom.com, Jonathan.Cameron@Huawei.com, f.fainelli@gmail.com, etienne.carriere@linaro.org, thara.gopinath@linaro.org, vincent.guittot@linaro.org, souvik.chakravarty@arm.com, cristian.marussi@arm.com Subject: [PATCH v4 36/37] firmware: arm_scmi: add protocol modularization support Date: Wed, 6 Jan 2021 20:16:09 +0000 Message-Id: <20210106201610.26538-37-cristian.marussi@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210106201610.26538-1-cristian.marussi@arm.com> References: <20210106201610.26538-1-cristian.marussi@arm.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Extend SCMI protocols accounting mechanism to address possible module usage and add the support to possibly define new protocols as loadable modules. Keep Standard protocols built into the SCMI core. Signed-off-by: Cristian Marussi --- drivers/firmware/arm_scmi/base.c | 2 ++ drivers/firmware/arm_scmi/bus.c | 11 ++++++++++- drivers/firmware/arm_scmi/clock.c | 2 ++ drivers/firmware/arm_scmi/common.h | 4 ++++ drivers/firmware/arm_scmi/driver.c | 5 ++++- drivers/firmware/arm_scmi/perf.c | 2 ++ drivers/firmware/arm_scmi/power.c | 2 ++ drivers/firmware/arm_scmi/reset.c | 2 ++ drivers/firmware/arm_scmi/sensors.c | 2 ++ drivers/firmware/arm_scmi/system.c | 2 ++ include/linux/scmi_protocol.h | 12 ++++++++++++ 11 files changed, 44 insertions(+), 2 deletions(-) diff --git a/drivers/firmware/arm_scmi/base.c b/drivers/firmware/arm_scmi/base.c index f2d10c5a55ae..b70d741f8465 100644 --- a/drivers/firmware/arm_scmi/base.c +++ b/drivers/firmware/arm_scmi/base.c @@ -7,6 +7,7 @@ #define pr_fmt(fmt) "SCMI Notifications BASE - " fmt +#include #include #include "common.h" @@ -373,6 +374,7 @@ static int scmi_base_protocol_init(const struct scmi_protocol_handle *ph) static const struct scmi_protocol scmi_base = { .id = SCMI_PROTOCOL_BASE, + .owner = NULL, .init_instance = &scmi_base_protocol_init, .ops = NULL, .events = &base_protocol_events, diff --git a/drivers/firmware/arm_scmi/bus.c b/drivers/firmware/arm_scmi/bus.c index 9fbf618b9c4b..88e5057f4e85 100644 --- a/drivers/firmware/arm_scmi/bus.c +++ b/drivers/firmware/arm_scmi/bus.c @@ -56,7 +56,7 @@ const struct scmi_protocol *scmi_get_protocol(int protocol_id) const struct scmi_protocol *proto; proto = idr_find(&scmi_available_protocols, protocol_id); - if (!proto) { + if (!proto || !try_module_get(proto->owner)) { pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id); return NULL; } @@ -66,6 +66,15 @@ const struct scmi_protocol *scmi_get_protocol(int protocol_id) return proto; } +void scmi_put_protocol(int protocol_id) +{ + const struct scmi_protocol *proto; + + proto = idr_find(&scmi_available_protocols, protocol_id); + if (proto) + module_put(proto->owner); +} + static int scmi_dev_probe(struct device *dev) { struct scmi_driver *scmi_drv = to_scmi_driver(dev->driver); diff --git a/drivers/firmware/arm_scmi/clock.c b/drivers/firmware/arm_scmi/clock.c index a0a6593f464f..bd364d3a56d2 100644 --- a/drivers/firmware/arm_scmi/clock.c +++ b/drivers/firmware/arm_scmi/clock.c @@ -5,6 +5,7 @@ * Copyright (C) 2018-2020 ARM Ltd. */ +#include #include #include "common.h" @@ -367,6 +368,7 @@ static int scmi_clock_protocol_init(const struct scmi_protocol_handle *ph) static const struct scmi_protocol scmi_clock = { .id = SCMI_PROTOCOL_CLOCK, + .owner = THIS_MODULE, .init_instance = &scmi_clock_protocol_init, .ops = &clk_proto_ops, }; diff --git a/drivers/firmware/arm_scmi/common.h b/drivers/firmware/arm_scmi/common.h index 5fb64182610a..1e2046c61d43 100644 --- a/drivers/firmware/arm_scmi/common.h +++ b/drivers/firmware/arm_scmi/common.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -214,6 +215,7 @@ typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *); /** * struct scmi_protocol - Protocol descriptor * @id: Protocol ID. + * @owner: Module reference if any. * @init_instance: Mandatory protocol initialization function. * @deinit_instance: Optional protocol de-initialization function. * @ops: Optional reference to the operations provided by the protocol and @@ -222,6 +224,7 @@ typedef int (*scmi_prot_init_ph_fn_t)(const struct scmi_protocol_handle *); */ struct scmi_protocol { const u8 id; + struct module *owner; const scmi_prot_init_ph_fn_t init_instance; const scmi_prot_init_ph_fn_t deinit_instance; const void *ops; @@ -255,6 +258,7 @@ void __exit scmi_##name##_unregister(void) \ } const struct scmi_protocol *scmi_get_protocol(int protocol_id); +void scmi_put_protocol(int protocol_id); int scmi_acquire_protocol(const struct scmi_handle *handle, u8 protocol_id); void scmi_release_protocol(const struct scmi_handle *handle, u8 protocol_id); diff --git a/drivers/firmware/arm_scmi/driver.c b/drivers/firmware/arm_scmi/driver.c index a0a6a3d1d239..4b68952e49db 100644 --- a/drivers/firmware/arm_scmi/driver.c +++ b/drivers/firmware/arm_scmi/driver.c @@ -677,7 +677,7 @@ scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id) /* Fail if protocol not registered on bus */ proto = scmi_get_protocol(protocol_id); if (!proto) { - ret = -ENODEV; + ret = -EPROBE_DEFER; goto out; } @@ -722,6 +722,7 @@ scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id) return pi; clean: + scmi_put_protocol(protocol_id); devres_release_group(handle->dev, gid); out: mutex_unlock(&info->protocols_mtx); @@ -772,6 +773,8 @@ void scmi_release_protocol(const struct scmi_handle *handle, u8 protocol_id) idr_remove(&info->protocols, protocol_id); + scmi_put_protocol(protocol_id); + devres_release_group(handle->dev, gid); dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n", protocol_id); diff --git a/drivers/firmware/arm_scmi/perf.c b/drivers/firmware/arm_scmi/perf.c index c87a390463d9..f9146429789e 100644 --- a/drivers/firmware/arm_scmi/perf.c +++ b/drivers/firmware/arm_scmi/perf.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -909,6 +910,7 @@ static int scmi_perf_protocol_init(const struct scmi_protocol_handle *ph) static const struct scmi_protocol scmi_perf = { .id = SCMI_PROTOCOL_PERF, + .owner = THIS_MODULE, .init_instance = &scmi_perf_protocol_init, .ops = &perf_proto_ops, .events = &perf_protocol_events, diff --git a/drivers/firmware/arm_scmi/power.c b/drivers/firmware/arm_scmi/power.c index 3e453d9bcbc8..70d366ed37e6 100644 --- a/drivers/firmware/arm_scmi/power.c +++ b/drivers/firmware/arm_scmi/power.c @@ -7,6 +7,7 @@ #define pr_fmt(fmt) "SCMI Notifications POWER - " fmt +#include #include #include "common.h" @@ -312,6 +313,7 @@ static int scmi_power_protocol_init(const struct scmi_protocol_handle *ph) static const struct scmi_protocol scmi_power = { .id = SCMI_PROTOCOL_POWER, + .owner = THIS_MODULE, .init_instance = &scmi_power_protocol_init, .ops = &power_proto_ops, .events = &power_protocol_events, diff --git a/drivers/firmware/arm_scmi/reset.c b/drivers/firmware/arm_scmi/reset.c index bd28d4e9664d..b26686dd8526 100644 --- a/drivers/firmware/arm_scmi/reset.c +++ b/drivers/firmware/arm_scmi/reset.c @@ -7,6 +7,7 @@ #define pr_fmt(fmt) "SCMI Notifications RESET - " fmt +#include #include #include "common.h" @@ -324,6 +325,7 @@ static int scmi_reset_protocol_init(const struct scmi_protocol_handle *ph) static const struct scmi_protocol scmi_reset = { .id = SCMI_PROTOCOL_RESET, + .owner = THIS_MODULE, .init_instance = &scmi_reset_protocol_init, .ops = &reset_proto_ops, .events = &reset_protocol_events, diff --git a/drivers/firmware/arm_scmi/sensors.c b/drivers/firmware/arm_scmi/sensors.c index edc08d9f8daa..903fcdb57d4a 100644 --- a/drivers/firmware/arm_scmi/sensors.c +++ b/drivers/firmware/arm_scmi/sensors.c @@ -8,6 +8,7 @@ #define pr_fmt(fmt) "SCMI Notifications SENSOR - " fmt #include +#include #include #include "common.h" @@ -990,6 +991,7 @@ static int scmi_sensors_protocol_init(const struct scmi_protocol_handle *ph) static const struct scmi_protocol scmi_sensors = { .id = SCMI_PROTOCOL_SENSOR, + .owner = THIS_MODULE, .init_instance = &scmi_sensors_protocol_init, .ops = &sensor_proto_ops, .events = &sensor_protocol_events, diff --git a/drivers/firmware/arm_scmi/system.c b/drivers/firmware/arm_scmi/system.c index 97ad20a51d72..a68bad27688f 100644 --- a/drivers/firmware/arm_scmi/system.c +++ b/drivers/firmware/arm_scmi/system.c @@ -7,6 +7,7 @@ #define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt +#include #include #include "common.h" @@ -130,6 +131,7 @@ static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph) static const struct scmi_protocol scmi_system = { .id = SCMI_PROTOCOL_SYSTEM, + .owner = THIS_MODULE, .init_instance = &scmi_system_protocol_init, .ops = NULL, .events = &system_protocol_events, diff --git a/include/linux/scmi_protocol.h b/include/linux/scmi_protocol.h index c5d3f9b7de7c..13e81547c3d1 100644 --- a/include/linux/scmi_protocol.h +++ b/include/linux/scmi_protocol.h @@ -709,6 +709,18 @@ static inline void scmi_driver_unregister(struct scmi_driver *driver) {} #define module_scmi_driver(__scmi_driver) \ module_driver(__scmi_driver, scmi_register, scmi_unregister) +/** + * module_scmi_protocol() - Helper macro for registering a scmi protocol + * @__scmi_protocol: scmi_protocol structure + * + * Helper macro for scmi drivers to set up proper module init / exit + * functions. Replaces module_init() and module_exit() and keeps people from + * printing pointless things to the kernel log when their driver is loaded. + */ +#define module_scmi_protocol(__scmi_protocol) \ + module_driver(__scmi_protocol, \ + scmi_protocol_register, scmi_protocol_unregister) + struct scmi_protocol; int scmi_protocol_register(const struct scmi_protocol *proto); void scmi_protocol_unregister(const struct scmi_protocol *proto); -- 2.17.1