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