1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Reset Protocol
5 * Copyright (C) 2019-2022 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications RESET - " fmt
10 #include <linux/module.h>
11 #include <linux/scmi_protocol.h>
13 #include "protocols.h"
16 /* Updated only after ALL the mandatory features for that version are merged */
17 #define SCMI_PROTOCOL_SUPPORTED_VERSION 0x30000
19 enum scmi_reset_protocol_cmd {
20 RESET_DOMAIN_ATTRIBUTES = 0x3,
23 RESET_DOMAIN_NAME_GET = 0x6,
26 #define NUM_RESET_DOMAIN_MASK 0xffff
27 #define RESET_NOTIFY_ENABLE BIT(0)
29 struct scmi_msg_resp_reset_domain_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))
35 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
38 struct scmi_msg_reset_domain_reset {
41 #define AUTONOMOUS_RESET BIT(0)
42 #define EXPLICIT_RESET_ASSERT BIT(1)
43 #define ASYNCHRONOUS_RESET BIT(2)
45 #define ARCH_COLD_RESET 0
48 struct scmi_msg_reset_notify {
51 #define RESET_TP_NOTIFY_ALL BIT(0)
54 struct scmi_reset_issued_notify_payld {
60 struct reset_dom_info {
64 char name[SCMI_MAX_STR_SIZE];
67 struct scmi_reset_info {
70 struct reset_dom_info *dom_info;
73 static int scmi_reset_attributes_get(const struct scmi_protocol_handle *ph,
74 struct scmi_reset_info *pi)
80 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
85 ret = ph->xops->do_xfer(ph, t);
87 attr = get_unaligned_le32(t->rx.buf);
88 pi->num_domains = attr & NUM_RESET_DOMAIN_MASK;
91 ph->xops->xfer_put(ph, t);
96 scmi_reset_domain_attributes_get(const struct scmi_protocol_handle *ph,
97 u32 domain, struct reset_dom_info *dom_info,
103 struct scmi_msg_resp_reset_domain_attributes *attr;
105 ret = ph->xops->xfer_get_init(ph, RESET_DOMAIN_ATTRIBUTES,
106 sizeof(domain), sizeof(*attr), &t);
110 put_unaligned_le32(domain, t->tx.buf);
113 ret = ph->xops->do_xfer(ph, t);
115 attributes = le32_to_cpu(attr->attributes);
117 dom_info->async_reset = SUPPORTS_ASYNC_RESET(attributes);
118 dom_info->reset_notify = SUPPORTS_NOTIFY_RESET(attributes);
119 dom_info->latency_us = le32_to_cpu(attr->latency);
120 if (dom_info->latency_us == U32_MAX)
121 dom_info->latency_us = 0;
122 strscpy(dom_info->name, attr->name, SCMI_SHORT_NAME_MAX_SIZE);
125 ph->xops->xfer_put(ph, t);
128 * If supported overwrite short name with the extended one;
129 * on error just carry on and use already provided short name.
131 if (!ret && PROTOCOL_REV_MAJOR(version) >= 0x3 &&
132 SUPPORTS_EXTENDED_NAMES(attributes))
133 ph->hops->extended_name_get(ph, RESET_DOMAIN_NAME_GET, domain,
134 NULL, dom_info->name,
140 static int scmi_reset_num_domains_get(const struct scmi_protocol_handle *ph)
142 struct scmi_reset_info *pi = ph->get_priv(ph);
144 return pi->num_domains;
148 scmi_reset_name_get(const struct scmi_protocol_handle *ph, u32 domain)
150 struct scmi_reset_info *pi = ph->get_priv(ph);
152 struct reset_dom_info *dom = pi->dom_info + domain;
157 static int scmi_reset_latency_get(const struct scmi_protocol_handle *ph,
160 struct scmi_reset_info *pi = ph->get_priv(ph);
161 struct reset_dom_info *dom = pi->dom_info + domain;
163 return dom->latency_us;
166 static int scmi_domain_reset(const struct scmi_protocol_handle *ph, u32 domain,
167 u32 flags, u32 state)
171 struct scmi_msg_reset_domain_reset *dom;
172 struct scmi_reset_info *pi = ph->get_priv(ph);
173 struct reset_dom_info *rdom;
175 if (domain >= pi->num_domains)
178 rdom = pi->dom_info + domain;
179 if (rdom->async_reset && flags & AUTONOMOUS_RESET)
180 flags |= ASYNCHRONOUS_RESET;
182 ret = ph->xops->xfer_get_init(ph, RESET, sizeof(*dom), 0, &t);
187 dom->domain_id = cpu_to_le32(domain);
188 dom->flags = cpu_to_le32(flags);
189 dom->reset_state = cpu_to_le32(state);
191 if (flags & ASYNCHRONOUS_RESET)
192 ret = ph->xops->do_xfer_with_response(ph, t);
194 ret = ph->xops->do_xfer(ph, t);
196 ph->xops->xfer_put(ph, t);
200 static int scmi_reset_domain_reset(const struct scmi_protocol_handle *ph,
203 return scmi_domain_reset(ph, domain, AUTONOMOUS_RESET,
208 scmi_reset_domain_assert(const struct scmi_protocol_handle *ph, u32 domain)
210 return scmi_domain_reset(ph, domain, EXPLICIT_RESET_ASSERT,
215 scmi_reset_domain_deassert(const struct scmi_protocol_handle *ph, u32 domain)
217 return scmi_domain_reset(ph, domain, 0, ARCH_COLD_RESET);
220 static const struct scmi_reset_proto_ops reset_proto_ops = {
221 .num_domains_get = scmi_reset_num_domains_get,
222 .name_get = scmi_reset_name_get,
223 .latency_get = scmi_reset_latency_get,
224 .reset = scmi_reset_domain_reset,
225 .assert = scmi_reset_domain_assert,
226 .deassert = scmi_reset_domain_deassert,
229 static int scmi_reset_notify(const struct scmi_protocol_handle *ph,
230 u32 domain_id, bool enable)
233 u32 evt_cntl = enable ? RESET_TP_NOTIFY_ALL : 0;
235 struct scmi_msg_reset_notify *cfg;
237 ret = ph->xops->xfer_get_init(ph, RESET_NOTIFY, sizeof(*cfg), 0, &t);
242 cfg->id = cpu_to_le32(domain_id);
243 cfg->event_control = cpu_to_le32(evt_cntl);
245 ret = ph->xops->do_xfer(ph, t);
247 ph->xops->xfer_put(ph, t);
251 static int scmi_reset_set_notify_enabled(const struct scmi_protocol_handle *ph,
252 u8 evt_id, u32 src_id, bool enable)
256 ret = scmi_reset_notify(ph, src_id, enable);
258 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
259 evt_id, src_id, ret);
265 scmi_reset_fill_custom_report(const struct scmi_protocol_handle *ph,
266 u8 evt_id, ktime_t timestamp,
267 const void *payld, size_t payld_sz,
268 void *report, u32 *src_id)
270 const struct scmi_reset_issued_notify_payld *p = payld;
271 struct scmi_reset_issued_report *r = report;
273 if (evt_id != SCMI_EVENT_RESET_ISSUED || sizeof(*p) != payld_sz)
276 r->timestamp = timestamp;
277 r->agent_id = le32_to_cpu(p->agent_id);
278 r->domain_id = le32_to_cpu(p->domain_id);
279 r->reset_state = le32_to_cpu(p->reset_state);
280 *src_id = r->domain_id;
285 static int scmi_reset_get_num_sources(const struct scmi_protocol_handle *ph)
287 struct scmi_reset_info *pinfo = ph->get_priv(ph);
292 return pinfo->num_domains;
295 static const struct scmi_event reset_events[] = {
297 .id = SCMI_EVENT_RESET_ISSUED,
298 .max_payld_sz = sizeof(struct scmi_reset_issued_notify_payld),
299 .max_report_sz = sizeof(struct scmi_reset_issued_report),
303 static const struct scmi_event_ops reset_event_ops = {
304 .get_num_sources = scmi_reset_get_num_sources,
305 .set_notify_enabled = scmi_reset_set_notify_enabled,
306 .fill_custom_report = scmi_reset_fill_custom_report,
309 static const struct scmi_protocol_events reset_protocol_events = {
310 .queue_sz = SCMI_PROTO_QUEUE_SZ,
311 .ops = &reset_event_ops,
312 .evts = reset_events,
313 .num_events = ARRAY_SIZE(reset_events),
316 static int scmi_reset_protocol_init(const struct scmi_protocol_handle *ph)
320 struct scmi_reset_info *pinfo;
322 ret = ph->xops->version_get(ph, &version);
326 dev_dbg(ph->dev, "Reset Version %d.%d\n",
327 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
329 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
333 ret = scmi_reset_attributes_get(ph, pinfo);
337 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
338 sizeof(*pinfo->dom_info), GFP_KERNEL);
339 if (!pinfo->dom_info)
342 for (domain = 0; domain < pinfo->num_domains; domain++) {
343 struct reset_dom_info *dom = pinfo->dom_info + domain;
345 scmi_reset_domain_attributes_get(ph, domain, dom, version);
348 pinfo->version = version;
349 return ph->set_priv(ph, pinfo, version);
352 static const struct scmi_protocol scmi_reset = {
353 .id = SCMI_PROTOCOL_RESET,
354 .owner = THIS_MODULE,
355 .instance_init = &scmi_reset_protocol_init,
356 .ops = &reset_proto_ops,
357 .events = &reset_protocol_events,
358 .supported_version = SCMI_PROTOCOL_SUPPORTED_VERSION,
361 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(reset, scmi_reset)