1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * System Control and Management Interface (SCMI) Reset Protocol
4  *
5  * Copyright (C) 2019-2021 ARM Ltd.
6  */
7 
8 #define pr_fmt(fmt) "SCMI Notifications RESET - " fmt
9 
10 #include <linux/module.h>
11 #include <linux/scmi_protocol.h>
12 
13 #include "common.h"
14 #include "notify.h"
15 
16 enum scmi_reset_protocol_cmd {
17 	RESET_DOMAIN_ATTRIBUTES = 0x3,
18 	RESET = 0x4,
19 	RESET_NOTIFY = 0x5,
20 };
21 
22 #define NUM_RESET_DOMAIN_MASK	0xffff
23 #define RESET_NOTIFY_ENABLE	BIT(0)
24 
25 struct scmi_msg_resp_reset_domain_attributes {
26 	__le32 attributes;
27 #define SUPPORTS_ASYNC_RESET(x)		((x) & BIT(31))
28 #define SUPPORTS_NOTIFY_RESET(x)	((x) & BIT(30))
29 	__le32 latency;
30 	    u8 name[SCMI_MAX_STR_SIZE];
31 };
32 
33 struct scmi_msg_reset_domain_reset {
34 	__le32 domain_id;
35 	__le32 flags;
36 #define AUTONOMOUS_RESET	BIT(0)
37 #define EXPLICIT_RESET_ASSERT	BIT(1)
38 #define ASYNCHRONOUS_RESET	BIT(2)
39 	__le32 reset_state;
40 #define ARCH_COLD_RESET		0
41 };
42 
43 struct scmi_msg_reset_notify {
44 	__le32 id;
45 	__le32 event_control;
46 #define RESET_TP_NOTIFY_ALL	BIT(0)
47 };
48 
49 struct scmi_reset_issued_notify_payld {
50 	__le32 agent_id;
51 	__le32 domain_id;
52 	__le32 reset_state;
53 };
54 
55 struct reset_dom_info {
56 	bool async_reset;
57 	bool reset_notify;
58 	u32 latency_us;
59 	char name[SCMI_MAX_STR_SIZE];
60 };
61 
62 struct scmi_reset_info {
63 	u32 version;
64 	int num_domains;
65 	struct reset_dom_info *dom_info;
66 };
67 
68 static int scmi_reset_attributes_get(const struct scmi_protocol_handle *ph,
69 				     struct scmi_reset_info *pi)
70 {
71 	int ret;
72 	struct scmi_xfer *t;
73 	u32 attr;
74 
75 	ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
76 				      0, sizeof(attr), &t);
77 	if (ret)
78 		return ret;
79 
80 	ret = ph->xops->do_xfer(ph, t);
81 	if (!ret) {
82 		attr = get_unaligned_le32(t->rx.buf);
83 		pi->num_domains = attr & NUM_RESET_DOMAIN_MASK;
84 	}
85 
86 	ph->xops->xfer_put(ph, t);
87 	return ret;
88 }
89 
90 static int
91 scmi_reset_domain_attributes_get(const struct scmi_protocol_handle *ph,
92 				 u32 domain, struct reset_dom_info *dom_info)
93 {
94 	int ret;
95 	struct scmi_xfer *t;
96 	struct scmi_msg_resp_reset_domain_attributes *attr;
97 
98 	ret = ph->xops->xfer_get_init(ph, RESET_DOMAIN_ATTRIBUTES,
99 				      sizeof(domain), sizeof(*attr), &t);
100 	if (ret)
101 		return ret;
102 
103 	put_unaligned_le32(domain, t->tx.buf);
104 	attr = t->rx.buf;
105 
106 	ret = ph->xops->do_xfer(ph, t);
107 	if (!ret) {
108 		u32 attributes = le32_to_cpu(attr->attributes);
109 
110 		dom_info->async_reset = SUPPORTS_ASYNC_RESET(attributes);
111 		dom_info->reset_notify = SUPPORTS_NOTIFY_RESET(attributes);
112 		dom_info->latency_us = le32_to_cpu(attr->latency);
113 		if (dom_info->latency_us == U32_MAX)
114 			dom_info->latency_us = 0;
115 		strlcpy(dom_info->name, attr->name, SCMI_MAX_STR_SIZE);
116 	}
117 
118 	ph->xops->xfer_put(ph, t);
119 	return ret;
120 }
121 
122 static int scmi_reset_num_domains_get(const struct scmi_protocol_handle *ph)
123 {
124 	struct scmi_reset_info *pi = ph->get_priv(ph);
125 
126 	return pi->num_domains;
127 }
128 
129 static char *scmi_reset_name_get(const struct scmi_protocol_handle *ph,
130 				 u32 domain)
131 {
132 	struct scmi_reset_info *pi = ph->get_priv(ph);
133 
134 	struct reset_dom_info *dom = pi->dom_info + domain;
135 
136 	return dom->name;
137 }
138 
139 static int scmi_reset_latency_get(const struct scmi_protocol_handle *ph,
140 				  u32 domain)
141 {
142 	struct scmi_reset_info *pi = ph->get_priv(ph);
143 	struct reset_dom_info *dom = pi->dom_info + domain;
144 
145 	return dom->latency_us;
146 }
147 
148 static int scmi_domain_reset(const struct scmi_protocol_handle *ph, u32 domain,
149 			     u32 flags, u32 state)
150 {
151 	int ret;
152 	struct scmi_xfer *t;
153 	struct scmi_msg_reset_domain_reset *dom;
154 	struct scmi_reset_info *pi = ph->get_priv(ph);
155 	struct reset_dom_info *rdom;
156 
157 	if (domain >= pi->num_domains)
158 		return -EINVAL;
159 
160 	rdom = pi->dom_info + domain;
161 	if (rdom->async_reset)
162 		flags |= ASYNCHRONOUS_RESET;
163 
164 	ret = ph->xops->xfer_get_init(ph, RESET, sizeof(*dom), 0, &t);
165 	if (ret)
166 		return ret;
167 
168 	dom = t->tx.buf;
169 	dom->domain_id = cpu_to_le32(domain);
170 	dom->flags = cpu_to_le32(flags);
171 	dom->reset_state = cpu_to_le32(state);
172 
173 	if (rdom->async_reset)
174 		ret = ph->xops->do_xfer_with_response(ph, t);
175 	else
176 		ret = ph->xops->do_xfer(ph, t);
177 
178 	ph->xops->xfer_put(ph, t);
179 	return ret;
180 }
181 
182 static int scmi_reset_domain_reset(const struct scmi_protocol_handle *ph,
183 				   u32 domain)
184 {
185 	return scmi_domain_reset(ph, domain, AUTONOMOUS_RESET,
186 				 ARCH_COLD_RESET);
187 }
188 
189 static int
190 scmi_reset_domain_assert(const struct scmi_protocol_handle *ph, u32 domain)
191 {
192 	return scmi_domain_reset(ph, domain, EXPLICIT_RESET_ASSERT,
193 				 ARCH_COLD_RESET);
194 }
195 
196 static int
197 scmi_reset_domain_deassert(const struct scmi_protocol_handle *ph, u32 domain)
198 {
199 	return scmi_domain_reset(ph, domain, 0, ARCH_COLD_RESET);
200 }
201 
202 static const struct scmi_reset_proto_ops reset_proto_ops = {
203 	.num_domains_get = scmi_reset_num_domains_get,
204 	.name_get = scmi_reset_name_get,
205 	.latency_get = scmi_reset_latency_get,
206 	.reset = scmi_reset_domain_reset,
207 	.assert = scmi_reset_domain_assert,
208 	.deassert = scmi_reset_domain_deassert,
209 };
210 
211 static int scmi_reset_notify(const struct scmi_protocol_handle *ph,
212 			     u32 domain_id, bool enable)
213 {
214 	int ret;
215 	u32 evt_cntl = enable ? RESET_TP_NOTIFY_ALL : 0;
216 	struct scmi_xfer *t;
217 	struct scmi_msg_reset_notify *cfg;
218 
219 	ret = ph->xops->xfer_get_init(ph, RESET_NOTIFY, sizeof(*cfg), 0, &t);
220 	if (ret)
221 		return ret;
222 
223 	cfg = t->tx.buf;
224 	cfg->id = cpu_to_le32(domain_id);
225 	cfg->event_control = cpu_to_le32(evt_cntl);
226 
227 	ret = ph->xops->do_xfer(ph, t);
228 
229 	ph->xops->xfer_put(ph, t);
230 	return ret;
231 }
232 
233 static int scmi_reset_set_notify_enabled(const struct scmi_protocol_handle *ph,
234 					 u8 evt_id, u32 src_id, bool enable)
235 {
236 	int ret;
237 
238 	ret = scmi_reset_notify(ph, src_id, enable);
239 	if (ret)
240 		pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
241 			 evt_id, src_id, ret);
242 
243 	return ret;
244 }
245 
246 static void *
247 scmi_reset_fill_custom_report(const struct scmi_protocol_handle *ph,
248 			      u8 evt_id, ktime_t timestamp,
249 			      const void *payld, size_t payld_sz,
250 			      void *report, u32 *src_id)
251 {
252 	const struct scmi_reset_issued_notify_payld *p = payld;
253 	struct scmi_reset_issued_report *r = report;
254 
255 	if (evt_id != SCMI_EVENT_RESET_ISSUED || sizeof(*p) != payld_sz)
256 		return NULL;
257 
258 	r->timestamp = timestamp;
259 	r->agent_id = le32_to_cpu(p->agent_id);
260 	r->domain_id = le32_to_cpu(p->domain_id);
261 	r->reset_state = le32_to_cpu(p->reset_state);
262 	*src_id = r->domain_id;
263 
264 	return r;
265 }
266 
267 static int scmi_reset_get_num_sources(const struct scmi_protocol_handle *ph)
268 {
269 	struct scmi_reset_info *pinfo = ph->get_priv(ph);
270 
271 	if (!pinfo)
272 		return -EINVAL;
273 
274 	return pinfo->num_domains;
275 }
276 
277 static const struct scmi_event reset_events[] = {
278 	{
279 		.id = SCMI_EVENT_RESET_ISSUED,
280 		.max_payld_sz = sizeof(struct scmi_reset_issued_notify_payld),
281 		.max_report_sz = sizeof(struct scmi_reset_issued_report),
282 	},
283 };
284 
285 static const struct scmi_event_ops reset_event_ops = {
286 	.get_num_sources = scmi_reset_get_num_sources,
287 	.set_notify_enabled = scmi_reset_set_notify_enabled,
288 	.fill_custom_report = scmi_reset_fill_custom_report,
289 };
290 
291 static const struct scmi_protocol_events reset_protocol_events = {
292 	.queue_sz = SCMI_PROTO_QUEUE_SZ,
293 	.ops = &reset_event_ops,
294 	.evts = reset_events,
295 	.num_events = ARRAY_SIZE(reset_events),
296 };
297 
298 static int scmi_reset_protocol_init(const struct scmi_protocol_handle *ph)
299 {
300 	int domain;
301 	u32 version;
302 	struct scmi_reset_info *pinfo;
303 
304 	ph->xops->version_get(ph, &version);
305 
306 	dev_dbg(ph->dev, "Reset Version %d.%d\n",
307 		PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
308 
309 	pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
310 	if (!pinfo)
311 		return -ENOMEM;
312 
313 	scmi_reset_attributes_get(ph, pinfo);
314 
315 	pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
316 				       sizeof(*pinfo->dom_info), GFP_KERNEL);
317 	if (!pinfo->dom_info)
318 		return -ENOMEM;
319 
320 	for (domain = 0; domain < pinfo->num_domains; domain++) {
321 		struct reset_dom_info *dom = pinfo->dom_info + domain;
322 
323 		scmi_reset_domain_attributes_get(ph, domain, dom);
324 	}
325 
326 	pinfo->version = version;
327 	return ph->set_priv(ph, pinfo);
328 }
329 
330 static const struct scmi_protocol scmi_reset = {
331 	.id = SCMI_PROTOCOL_RESET,
332 	.owner = THIS_MODULE,
333 	.instance_init = &scmi_reset_protocol_init,
334 	.ops = &reset_proto_ops,
335 	.events = &reset_protocol_events,
336 };
337 
338 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(reset, scmi_reset)
339