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