xref: /OK3568_Linux_fs/kernel/drivers/firmware/arm_scmi/system.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * System Control and Management Interface (SCMI) System Power Protocol
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2020 ARM Ltd.
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #define pr_fmt(fmt) "SCMI Notifications SYSTEM - " fmt
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/scmi_protocol.h>
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun #include "common.h"
14*4882a593Smuzhiyun #include "notify.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define SCMI_SYSTEM_NUM_SOURCES		1
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun enum scmi_system_protocol_cmd {
19*4882a593Smuzhiyun 	SYSTEM_POWER_STATE_NOTIFY = 0x5,
20*4882a593Smuzhiyun };
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun struct scmi_system_power_state_notify {
23*4882a593Smuzhiyun 	__le32 notify_enable;
24*4882a593Smuzhiyun };
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun struct scmi_system_power_state_notifier_payld {
27*4882a593Smuzhiyun 	__le32 agent_id;
28*4882a593Smuzhiyun 	__le32 flags;
29*4882a593Smuzhiyun 	__le32 system_state;
30*4882a593Smuzhiyun };
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun struct scmi_system_info {
33*4882a593Smuzhiyun 	u32 version;
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun 
scmi_system_request_notify(const struct scmi_protocol_handle * ph,bool enable)36*4882a593Smuzhiyun static int scmi_system_request_notify(const struct scmi_protocol_handle *ph,
37*4882a593Smuzhiyun 				      bool enable)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	int ret;
40*4882a593Smuzhiyun 	struct scmi_xfer *t;
41*4882a593Smuzhiyun 	struct scmi_system_power_state_notify *notify;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	ret = ph->xops->xfer_get_init(ph, SYSTEM_POWER_STATE_NOTIFY,
44*4882a593Smuzhiyun 				      sizeof(*notify), 0, &t);
45*4882a593Smuzhiyun 	if (ret)
46*4882a593Smuzhiyun 		return ret;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	notify = t->tx.buf;
49*4882a593Smuzhiyun 	notify->notify_enable = enable ? cpu_to_le32(BIT(0)) : 0;
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	ret = ph->xops->do_xfer(ph, t);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	ph->xops->xfer_put(ph, t);
54*4882a593Smuzhiyun 	return ret;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
scmi_system_set_notify_enabled(const struct scmi_protocol_handle * ph,u8 evt_id,u32 src_id,bool enable)57*4882a593Smuzhiyun static int scmi_system_set_notify_enabled(const struct scmi_protocol_handle *ph,
58*4882a593Smuzhiyun 					  u8 evt_id, u32 src_id, bool enable)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	int ret;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	ret = scmi_system_request_notify(ph, enable);
63*4882a593Smuzhiyun 	if (ret)
64*4882a593Smuzhiyun 		pr_debug("FAIL_ENABLE - evt[%X] - ret:%d\n", evt_id, ret);
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	return ret;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun static void *
scmi_system_fill_custom_report(const struct scmi_protocol_handle * ph,u8 evt_id,ktime_t timestamp,const void * payld,size_t payld_sz,void * report,u32 * src_id)70*4882a593Smuzhiyun scmi_system_fill_custom_report(const struct scmi_protocol_handle *ph,
71*4882a593Smuzhiyun 			       u8 evt_id, ktime_t timestamp,
72*4882a593Smuzhiyun 			       const void *payld, size_t payld_sz,
73*4882a593Smuzhiyun 			       void *report, u32 *src_id)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	const struct scmi_system_power_state_notifier_payld *p = payld;
76*4882a593Smuzhiyun 	struct scmi_system_power_state_notifier_report *r = report;
77*4882a593Smuzhiyun 
78*4882a593Smuzhiyun 	if (evt_id != SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER ||
79*4882a593Smuzhiyun 	    sizeof(*p) != payld_sz)
80*4882a593Smuzhiyun 		return NULL;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	r->timestamp = timestamp;
83*4882a593Smuzhiyun 	r->agent_id = le32_to_cpu(p->agent_id);
84*4882a593Smuzhiyun 	r->flags = le32_to_cpu(p->flags);
85*4882a593Smuzhiyun 	r->system_state = le32_to_cpu(p->system_state);
86*4882a593Smuzhiyun 	*src_id = 0;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	return r;
89*4882a593Smuzhiyun }
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun static const struct scmi_event system_events[] = {
92*4882a593Smuzhiyun 	{
93*4882a593Smuzhiyun 		.id = SCMI_EVENT_SYSTEM_POWER_STATE_NOTIFIER,
94*4882a593Smuzhiyun 		.max_payld_sz =
95*4882a593Smuzhiyun 			sizeof(struct scmi_system_power_state_notifier_payld),
96*4882a593Smuzhiyun 		.max_report_sz =
97*4882a593Smuzhiyun 			sizeof(struct scmi_system_power_state_notifier_report),
98*4882a593Smuzhiyun 	},
99*4882a593Smuzhiyun };
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun static const struct scmi_event_ops system_event_ops = {
102*4882a593Smuzhiyun 	.set_notify_enabled = scmi_system_set_notify_enabled,
103*4882a593Smuzhiyun 	.fill_custom_report = scmi_system_fill_custom_report,
104*4882a593Smuzhiyun };
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun static const struct scmi_protocol_events system_protocol_events = {
107*4882a593Smuzhiyun 	.queue_sz = SCMI_PROTO_QUEUE_SZ,
108*4882a593Smuzhiyun 	.ops = &system_event_ops,
109*4882a593Smuzhiyun 	.evts = system_events,
110*4882a593Smuzhiyun 	.num_events = ARRAY_SIZE(system_events),
111*4882a593Smuzhiyun 	.num_sources = SCMI_SYSTEM_NUM_SOURCES,
112*4882a593Smuzhiyun };
113*4882a593Smuzhiyun 
scmi_system_protocol_init(const struct scmi_protocol_handle * ph)114*4882a593Smuzhiyun static int scmi_system_protocol_init(const struct scmi_protocol_handle *ph)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	u32 version;
117*4882a593Smuzhiyun 	struct scmi_system_info *pinfo;
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	ph->xops->version_get(ph, &version);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	dev_dbg(ph->dev, "System Power Version %d.%d\n",
122*4882a593Smuzhiyun 		PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
125*4882a593Smuzhiyun 	if (!pinfo)
126*4882a593Smuzhiyun 		return -ENOMEM;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	pinfo->version = version;
129*4882a593Smuzhiyun 	return ph->set_priv(ph, pinfo);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun static const struct scmi_protocol scmi_system = {
133*4882a593Smuzhiyun 	.id = SCMI_PROTOCOL_SYSTEM,
134*4882a593Smuzhiyun 	.owner = THIS_MODULE,
135*4882a593Smuzhiyun 	.init_instance = &scmi_system_protocol_init,
136*4882a593Smuzhiyun 	.ops = NULL,
137*4882a593Smuzhiyun 	.events = &system_protocol_events,
138*4882a593Smuzhiyun };
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(system, scmi_system)
141