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 enum scmi_reset_protocol_cmd {
17 RESET_DOMAIN_ATTRIBUTES = 0x3,
20 RESET_DOMAIN_NAME_GET = 0x6,
23 #define NUM_RESET_DOMAIN_MASK 0xffff
24 #define RESET_NOTIFY_ENABLE BIT(0)
26 struct scmi_msg_resp_reset_domain_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))
32 u8 name[SCMI_SHORT_NAME_MAX_SIZE];
35 struct scmi_msg_reset_domain_reset {
38 #define AUTONOMOUS_RESET BIT(0)
39 #define EXPLICIT_RESET_ASSERT BIT(1)
40 #define ASYNCHRONOUS_RESET BIT(2)
42 #define ARCH_COLD_RESET 0
45 struct scmi_msg_reset_notify {
48 #define RESET_TP_NOTIFY_ALL BIT(0)
51 struct scmi_reset_issued_notify_payld {
57 struct reset_dom_info {
61 char name[SCMI_MAX_STR_SIZE];
64 struct scmi_reset_info {
67 struct reset_dom_info *dom_info;
70 static int scmi_reset_attributes_get(const struct scmi_protocol_handle *ph,
71 struct scmi_reset_info *pi)
77 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
82 ret = ph->xops->do_xfer(ph, t);
84 attr = get_unaligned_le32(t->rx.buf);
85 pi->num_domains = attr & NUM_RESET_DOMAIN_MASK;
88 ph->xops->xfer_put(ph, t);
93 scmi_reset_domain_attributes_get(const struct scmi_protocol_handle *ph,
94 u32 domain, struct reset_dom_info *dom_info,
100 struct scmi_msg_resp_reset_domain_attributes *attr;
102 ret = ph->xops->xfer_get_init(ph, RESET_DOMAIN_ATTRIBUTES,
103 sizeof(domain), sizeof(*attr), &t);
107 put_unaligned_le32(domain, t->tx.buf);
110 ret = ph->xops->do_xfer(ph, t);
112 attributes = le32_to_cpu(attr->attributes);
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);
122 ph->xops->xfer_put(ph, t);
125 * If supported overwrite short name with the extended one;
126 * on error just carry on and use already provided short name.
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);
136 static int scmi_reset_num_domains_get(const struct scmi_protocol_handle *ph)
138 struct scmi_reset_info *pi = ph->get_priv(ph);
140 return pi->num_domains;
144 scmi_reset_name_get(const struct scmi_protocol_handle *ph, u32 domain)
146 struct scmi_reset_info *pi = ph->get_priv(ph);
148 struct reset_dom_info *dom = pi->dom_info + domain;
153 static int scmi_reset_latency_get(const struct scmi_protocol_handle *ph,
156 struct scmi_reset_info *pi = ph->get_priv(ph);
157 struct reset_dom_info *dom = pi->dom_info + domain;
159 return dom->latency_us;
162 static int scmi_domain_reset(const struct scmi_protocol_handle *ph, u32 domain,
163 u32 flags, u32 state)
167 struct scmi_msg_reset_domain_reset *dom;
168 struct scmi_reset_info *pi = ph->get_priv(ph);
169 struct reset_dom_info *rdom = pi->dom_info + domain;
171 if (rdom->async_reset)
172 flags |= ASYNCHRONOUS_RESET;
174 ret = ph->xops->xfer_get_init(ph, RESET, sizeof(*dom), 0, &t);
179 dom->domain_id = cpu_to_le32(domain);
180 dom->flags = cpu_to_le32(flags);
181 dom->reset_state = cpu_to_le32(state);
183 if (rdom->async_reset)
184 ret = ph->xops->do_xfer_with_response(ph, t);
186 ret = ph->xops->do_xfer(ph, t);
188 ph->xops->xfer_put(ph, t);
192 static int scmi_reset_domain_reset(const struct scmi_protocol_handle *ph,
195 return scmi_domain_reset(ph, domain, AUTONOMOUS_RESET,
200 scmi_reset_domain_assert(const struct scmi_protocol_handle *ph, u32 domain)
202 return scmi_domain_reset(ph, domain, EXPLICIT_RESET_ASSERT,
207 scmi_reset_domain_deassert(const struct scmi_protocol_handle *ph, u32 domain)
209 return scmi_domain_reset(ph, domain, 0, ARCH_COLD_RESET);
212 static const struct scmi_reset_proto_ops reset_proto_ops = {
213 .num_domains_get = scmi_reset_num_domains_get,
214 .name_get = scmi_reset_name_get,
215 .latency_get = scmi_reset_latency_get,
216 .reset = scmi_reset_domain_reset,
217 .assert = scmi_reset_domain_assert,
218 .deassert = scmi_reset_domain_deassert,
221 static int scmi_reset_notify(const struct scmi_protocol_handle *ph,
222 u32 domain_id, bool enable)
225 u32 evt_cntl = enable ? RESET_TP_NOTIFY_ALL : 0;
227 struct scmi_msg_reset_notify *cfg;
229 ret = ph->xops->xfer_get_init(ph, RESET_NOTIFY, sizeof(*cfg), 0, &t);
234 cfg->id = cpu_to_le32(domain_id);
235 cfg->event_control = cpu_to_le32(evt_cntl);
237 ret = ph->xops->do_xfer(ph, t);
239 ph->xops->xfer_put(ph, t);
243 static int scmi_reset_set_notify_enabled(const struct scmi_protocol_handle *ph,
244 u8 evt_id, u32 src_id, bool enable)
248 ret = scmi_reset_notify(ph, src_id, enable);
250 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
251 evt_id, src_id, ret);
257 scmi_reset_fill_custom_report(const struct scmi_protocol_handle *ph,
258 u8 evt_id, ktime_t timestamp,
259 const void *payld, size_t payld_sz,
260 void *report, u32 *src_id)
262 const struct scmi_reset_issued_notify_payld *p = payld;
263 struct scmi_reset_issued_report *r = report;
265 if (evt_id != SCMI_EVENT_RESET_ISSUED || sizeof(*p) != payld_sz)
268 r->timestamp = timestamp;
269 r->agent_id = le32_to_cpu(p->agent_id);
270 r->domain_id = le32_to_cpu(p->domain_id);
271 r->reset_state = le32_to_cpu(p->reset_state);
272 *src_id = r->domain_id;
277 static int scmi_reset_get_num_sources(const struct scmi_protocol_handle *ph)
279 struct scmi_reset_info *pinfo = ph->get_priv(ph);
284 return pinfo->num_domains;
287 static const struct scmi_event reset_events[] = {
289 .id = SCMI_EVENT_RESET_ISSUED,
290 .max_payld_sz = sizeof(struct scmi_reset_issued_notify_payld),
291 .max_report_sz = sizeof(struct scmi_reset_issued_report),
295 static const struct scmi_event_ops reset_event_ops = {
296 .get_num_sources = scmi_reset_get_num_sources,
297 .set_notify_enabled = scmi_reset_set_notify_enabled,
298 .fill_custom_report = scmi_reset_fill_custom_report,
301 static const struct scmi_protocol_events reset_protocol_events = {
302 .queue_sz = SCMI_PROTO_QUEUE_SZ,
303 .ops = &reset_event_ops,
304 .evts = reset_events,
305 .num_events = ARRAY_SIZE(reset_events),
308 static int scmi_reset_protocol_init(const struct scmi_protocol_handle *ph)
312 struct scmi_reset_info *pinfo;
314 ret = ph->xops->version_get(ph, &version);
318 dev_dbg(ph->dev, "Reset Version %d.%d\n",
319 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
321 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
325 ret = scmi_reset_attributes_get(ph, pinfo);
329 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
330 sizeof(*pinfo->dom_info), GFP_KERNEL);
331 if (!pinfo->dom_info)
334 for (domain = 0; domain < pinfo->num_domains; domain++) {
335 struct reset_dom_info *dom = pinfo->dom_info + domain;
337 scmi_reset_domain_attributes_get(ph, domain, dom, version);
340 pinfo->version = version;
341 return ph->set_priv(ph, pinfo);
344 static const struct scmi_protocol scmi_reset = {
345 .id = SCMI_PROTOCOL_RESET,
346 .owner = THIS_MODULE,
347 .instance_init = &scmi_reset_protocol_init,
348 .ops = &reset_proto_ops,
349 .events = &reset_protocol_events,
352 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(reset, scmi_reset)