1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Reset Protocol
5 * Copyright (C) 2019-2021 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications RESET - " fmt
10 #include <linux/module.h>
11 #include <linux/scmi_protocol.h>
16 enum scmi_reset_protocol_cmd {
17 RESET_DOMAIN_ATTRIBUTES = 0x3,
22 #define NUM_RESET_DOMAIN_MASK 0xffff
23 #define RESET_NOTIFY_ENABLE BIT(0)
25 struct scmi_msg_resp_reset_domain_attributes {
27 #define SUPPORTS_ASYNC_RESET(x) ((x) & BIT(31))
28 #define SUPPORTS_NOTIFY_RESET(x) ((x) & BIT(30))
30 u8 name[SCMI_MAX_STR_SIZE];
33 struct scmi_msg_reset_domain_reset {
36 #define AUTONOMOUS_RESET BIT(0)
37 #define EXPLICIT_RESET_ASSERT BIT(1)
38 #define ASYNCHRONOUS_RESET BIT(2)
40 #define ARCH_COLD_RESET 0
43 struct scmi_msg_reset_notify {
46 #define RESET_TP_NOTIFY_ALL BIT(0)
49 struct scmi_reset_issued_notify_payld {
55 struct reset_dom_info {
59 char name[SCMI_MAX_STR_SIZE];
62 struct scmi_reset_info {
65 struct reset_dom_info *dom_info;
68 static int scmi_reset_attributes_get(const struct scmi_protocol_handle *ph,
69 struct scmi_reset_info *pi)
75 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
80 ret = ph->xops->do_xfer(ph, t);
82 attr = get_unaligned_le32(t->rx.buf);
83 pi->num_domains = attr & NUM_RESET_DOMAIN_MASK;
86 ph->xops->xfer_put(ph, t);
91 scmi_reset_domain_attributes_get(const struct scmi_protocol_handle *ph,
92 u32 domain, struct reset_dom_info *dom_info)
96 struct scmi_msg_resp_reset_domain_attributes *attr;
98 ret = ph->xops->xfer_get_init(ph, RESET_DOMAIN_ATTRIBUTES,
99 sizeof(domain), sizeof(*attr), &t);
103 put_unaligned_le32(domain, t->tx.buf);
106 ret = ph->xops->do_xfer(ph, t);
108 u32 attributes = le32_to_cpu(attr->attributes);
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);
118 ph->xops->xfer_put(ph, t);
122 static int scmi_reset_num_domains_get(const struct scmi_protocol_handle *ph)
124 struct scmi_reset_info *pi = ph->get_priv(ph);
126 return pi->num_domains;
129 static char *scmi_reset_name_get(const struct scmi_protocol_handle *ph,
132 struct scmi_reset_info *pi = ph->get_priv(ph);
134 struct reset_dom_info *dom = pi->dom_info + domain;
139 static int scmi_reset_latency_get(const struct scmi_protocol_handle *ph,
142 struct scmi_reset_info *pi = ph->get_priv(ph);
143 struct reset_dom_info *dom = pi->dom_info + domain;
145 return dom->latency_us;
148 static int scmi_domain_reset(const struct scmi_protocol_handle *ph, u32 domain,
149 u32 flags, u32 state)
153 struct scmi_msg_reset_domain_reset *dom;
154 struct scmi_reset_info *pi = ph->get_priv(ph);
155 struct reset_dom_info *rdom = pi->dom_info + domain;
157 if (rdom->async_reset)
158 flags |= ASYNCHRONOUS_RESET;
160 ret = ph->xops->xfer_get_init(ph, RESET, sizeof(*dom), 0, &t);
165 dom->domain_id = cpu_to_le32(domain);
166 dom->flags = cpu_to_le32(flags);
167 dom->reset_state = cpu_to_le32(state);
169 if (rdom->async_reset)
170 ret = ph->xops->do_xfer_with_response(ph, t);
172 ret = ph->xops->do_xfer(ph, t);
174 ph->xops->xfer_put(ph, t);
178 static int scmi_reset_domain_reset(const struct scmi_protocol_handle *ph,
181 return scmi_domain_reset(ph, domain, AUTONOMOUS_RESET,
186 scmi_reset_domain_assert(const struct scmi_protocol_handle *ph, u32 domain)
188 return scmi_domain_reset(ph, domain, EXPLICIT_RESET_ASSERT,
193 scmi_reset_domain_deassert(const struct scmi_protocol_handle *ph, u32 domain)
195 return scmi_domain_reset(ph, domain, 0, ARCH_COLD_RESET);
198 static const struct scmi_reset_proto_ops reset_proto_ops = {
199 .num_domains_get = scmi_reset_num_domains_get,
200 .name_get = scmi_reset_name_get,
201 .latency_get = scmi_reset_latency_get,
202 .reset = scmi_reset_domain_reset,
203 .assert = scmi_reset_domain_assert,
204 .deassert = scmi_reset_domain_deassert,
207 static int scmi_reset_notify(const struct scmi_protocol_handle *ph,
208 u32 domain_id, bool enable)
211 u32 evt_cntl = enable ? RESET_TP_NOTIFY_ALL : 0;
213 struct scmi_msg_reset_notify *cfg;
215 ret = ph->xops->xfer_get_init(ph, RESET_NOTIFY, sizeof(*cfg), 0, &t);
220 cfg->id = cpu_to_le32(domain_id);
221 cfg->event_control = cpu_to_le32(evt_cntl);
223 ret = ph->xops->do_xfer(ph, t);
225 ph->xops->xfer_put(ph, t);
229 static int scmi_reset_set_notify_enabled(const struct scmi_protocol_handle *ph,
230 u8 evt_id, u32 src_id, bool enable)
234 ret = scmi_reset_notify(ph, src_id, enable);
236 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
237 evt_id, src_id, ret);
243 scmi_reset_fill_custom_report(const struct scmi_protocol_handle *ph,
244 u8 evt_id, ktime_t timestamp,
245 const void *payld, size_t payld_sz,
246 void *report, u32 *src_id)
248 const struct scmi_reset_issued_notify_payld *p = payld;
249 struct scmi_reset_issued_report *r = report;
251 if (evt_id != SCMI_EVENT_RESET_ISSUED || sizeof(*p) != payld_sz)
254 r->timestamp = timestamp;
255 r->agent_id = le32_to_cpu(p->agent_id);
256 r->domain_id = le32_to_cpu(p->domain_id);
257 r->reset_state = le32_to_cpu(p->reset_state);
258 *src_id = r->domain_id;
263 static int scmi_reset_get_num_sources(const struct scmi_protocol_handle *ph)
265 struct scmi_reset_info *pinfo = ph->get_priv(ph);
270 return pinfo->num_domains;
273 static const struct scmi_event reset_events[] = {
275 .id = SCMI_EVENT_RESET_ISSUED,
276 .max_payld_sz = sizeof(struct scmi_reset_issued_notify_payld),
277 .max_report_sz = sizeof(struct scmi_reset_issued_report),
281 static const struct scmi_event_ops reset_event_ops = {
282 .get_num_sources = scmi_reset_get_num_sources,
283 .set_notify_enabled = scmi_reset_set_notify_enabled,
284 .fill_custom_report = scmi_reset_fill_custom_report,
287 static const struct scmi_protocol_events reset_protocol_events = {
288 .queue_sz = SCMI_PROTO_QUEUE_SZ,
289 .ops = &reset_event_ops,
290 .evts = reset_events,
291 .num_events = ARRAY_SIZE(reset_events),
294 static int scmi_reset_protocol_init(const struct scmi_protocol_handle *ph)
298 struct scmi_reset_info *pinfo;
300 ph->xops->version_get(ph, &version);
302 dev_dbg(ph->dev, "Reset Version %d.%d\n",
303 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
305 pinfo = devm_kzalloc(ph->dev, sizeof(*pinfo), GFP_KERNEL);
309 scmi_reset_attributes_get(ph, pinfo);
311 pinfo->dom_info = devm_kcalloc(ph->dev, pinfo->num_domains,
312 sizeof(*pinfo->dom_info), GFP_KERNEL);
313 if (!pinfo->dom_info)
316 for (domain = 0; domain < pinfo->num_domains; domain++) {
317 struct reset_dom_info *dom = pinfo->dom_info + domain;
319 scmi_reset_domain_attributes_get(ph, domain, dom);
322 pinfo->version = version;
323 return ph->set_priv(ph, pinfo);
326 static const struct scmi_protocol scmi_reset = {
327 .id = SCMI_PROTOCOL_RESET,
328 .owner = THIS_MODULE,
329 .instance_init = &scmi_reset_protocol_init,
330 .ops = &reset_proto_ops,
331 .events = &reset_protocol_events,
334 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(reset, scmi_reset)