WIP FPC-III support
[linux/fpc-iii.git] / drivers / firmware / arm_scmi / reset.c
bloba981a22cfe8912b4ae68bbadc18f275ada8c514f
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * System Control and Management Interface (SCMI) Reset Protocol
5 * Copyright (C) 2019 ARM Ltd.
6 */
8 #define pr_fmt(fmt) "SCMI Notifications RESET - " fmt
10 #include <linux/scmi_protocol.h>
12 #include "common.h"
13 #include "notify.h"
15 enum scmi_reset_protocol_cmd {
16 RESET_DOMAIN_ATTRIBUTES = 0x3,
17 RESET = 0x4,
18 RESET_NOTIFY = 0x5,
21 #define NUM_RESET_DOMAIN_MASK 0xffff
22 #define RESET_NOTIFY_ENABLE BIT(0)
24 struct scmi_msg_resp_reset_domain_attributes {
25 __le32 attributes;
26 #define SUPPORTS_ASYNC_RESET(x) ((x) & BIT(31))
27 #define SUPPORTS_NOTIFY_RESET(x) ((x) & BIT(30))
28 __le32 latency;
29 u8 name[SCMI_MAX_STR_SIZE];
32 struct scmi_msg_reset_domain_reset {
33 __le32 domain_id;
34 __le32 flags;
35 #define AUTONOMOUS_RESET BIT(0)
36 #define EXPLICIT_RESET_ASSERT BIT(1)
37 #define ASYNCHRONOUS_RESET BIT(2)
38 __le32 reset_state;
39 #define ARCH_COLD_RESET 0
42 struct scmi_msg_reset_notify {
43 __le32 id;
44 __le32 event_control;
45 #define RESET_TP_NOTIFY_ALL BIT(0)
48 struct scmi_reset_issued_notify_payld {
49 __le32 agent_id;
50 __le32 domain_id;
51 __le32 reset_state;
54 struct reset_dom_info {
55 bool async_reset;
56 bool reset_notify;
57 u32 latency_us;
58 char name[SCMI_MAX_STR_SIZE];
61 struct scmi_reset_info {
62 u32 version;
63 int num_domains;
64 struct reset_dom_info *dom_info;
67 static int scmi_reset_attributes_get(const struct scmi_handle *handle,
68 struct scmi_reset_info *pi)
70 int ret;
71 struct scmi_xfer *t;
72 u32 attr;
74 ret = scmi_xfer_get_init(handle, PROTOCOL_ATTRIBUTES,
75 SCMI_PROTOCOL_RESET, 0, sizeof(attr), &t);
76 if (ret)
77 return ret;
79 ret = scmi_do_xfer(handle, t);
80 if (!ret) {
81 attr = get_unaligned_le32(t->rx.buf);
82 pi->num_domains = attr & NUM_RESET_DOMAIN_MASK;
85 scmi_xfer_put(handle, t);
86 return ret;
89 static int
90 scmi_reset_domain_attributes_get(const struct scmi_handle *handle, u32 domain,
91 struct reset_dom_info *dom_info)
93 int ret;
94 struct scmi_xfer *t;
95 struct scmi_msg_resp_reset_domain_attributes *attr;
97 ret = scmi_xfer_get_init(handle, RESET_DOMAIN_ATTRIBUTES,
98 SCMI_PROTOCOL_RESET, sizeof(domain),
99 sizeof(*attr), &t);
100 if (ret)
101 return ret;
103 put_unaligned_le32(domain, t->tx.buf);
104 attr = t->rx.buf;
106 ret = scmi_do_xfer(handle, t);
107 if (!ret) {
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 scmi_xfer_put(handle, t);
119 return ret;
122 static int scmi_reset_num_domains_get(const struct scmi_handle *handle)
124 struct scmi_reset_info *pi = handle->reset_priv;
126 return pi->num_domains;
129 static char *scmi_reset_name_get(const struct scmi_handle *handle, u32 domain)
131 struct scmi_reset_info *pi = handle->reset_priv;
132 struct reset_dom_info *dom = pi->dom_info + domain;
134 return dom->name;
137 static int scmi_reset_latency_get(const struct scmi_handle *handle, u32 domain)
139 struct scmi_reset_info *pi = handle->reset_priv;
140 struct reset_dom_info *dom = pi->dom_info + domain;
142 return dom->latency_us;
145 static int scmi_domain_reset(const struct scmi_handle *handle, u32 domain,
146 u32 flags, u32 state)
148 int ret;
149 struct scmi_xfer *t;
150 struct scmi_msg_reset_domain_reset *dom;
151 struct scmi_reset_info *pi = handle->reset_priv;
152 struct reset_dom_info *rdom = pi->dom_info + domain;
154 if (rdom->async_reset)
155 flags |= ASYNCHRONOUS_RESET;
157 ret = scmi_xfer_get_init(handle, RESET, SCMI_PROTOCOL_RESET,
158 sizeof(*dom), 0, &t);
159 if (ret)
160 return ret;
162 dom = t->tx.buf;
163 dom->domain_id = cpu_to_le32(domain);
164 dom->flags = cpu_to_le32(flags);
165 dom->reset_state = cpu_to_le32(state);
167 if (rdom->async_reset)
168 ret = scmi_do_xfer_with_response(handle, t);
169 else
170 ret = scmi_do_xfer(handle, t);
172 scmi_xfer_put(handle, t);
173 return ret;
176 static int scmi_reset_domain_reset(const struct scmi_handle *handle, u32 domain)
178 return scmi_domain_reset(handle, domain, AUTONOMOUS_RESET,
179 ARCH_COLD_RESET);
182 static int
183 scmi_reset_domain_assert(const struct scmi_handle *handle, u32 domain)
185 return scmi_domain_reset(handle, domain, EXPLICIT_RESET_ASSERT,
186 ARCH_COLD_RESET);
189 static int
190 scmi_reset_domain_deassert(const struct scmi_handle *handle, u32 domain)
192 return scmi_domain_reset(handle, domain, 0, ARCH_COLD_RESET);
195 static const struct scmi_reset_ops reset_ops = {
196 .num_domains_get = scmi_reset_num_domains_get,
197 .name_get = scmi_reset_name_get,
198 .latency_get = scmi_reset_latency_get,
199 .reset = scmi_reset_domain_reset,
200 .assert = scmi_reset_domain_assert,
201 .deassert = scmi_reset_domain_deassert,
204 static int scmi_reset_notify(const struct scmi_handle *handle, u32 domain_id,
205 bool enable)
207 int ret;
208 u32 evt_cntl = enable ? RESET_TP_NOTIFY_ALL : 0;
209 struct scmi_xfer *t;
210 struct scmi_msg_reset_notify *cfg;
212 ret = scmi_xfer_get_init(handle, RESET_NOTIFY,
213 SCMI_PROTOCOL_RESET, sizeof(*cfg), 0, &t);
214 if (ret)
215 return ret;
217 cfg = t->tx.buf;
218 cfg->id = cpu_to_le32(domain_id);
219 cfg->event_control = cpu_to_le32(evt_cntl);
221 ret = scmi_do_xfer(handle, t);
223 scmi_xfer_put(handle, t);
224 return ret;
227 static int scmi_reset_set_notify_enabled(const struct scmi_handle *handle,
228 u8 evt_id, u32 src_id, bool enable)
230 int ret;
232 ret = scmi_reset_notify(handle, src_id, enable);
233 if (ret)
234 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
235 evt_id, src_id, ret);
237 return ret;
240 static void *scmi_reset_fill_custom_report(const struct scmi_handle *handle,
241 u8 evt_id, ktime_t timestamp,
242 const void *payld, size_t payld_sz,
243 void *report, u32 *src_id)
245 const struct scmi_reset_issued_notify_payld *p = payld;
246 struct scmi_reset_issued_report *r = report;
248 if (evt_id != SCMI_EVENT_RESET_ISSUED || sizeof(*p) != payld_sz)
249 return NULL;
251 r->timestamp = timestamp;
252 r->agent_id = le32_to_cpu(p->agent_id);
253 r->domain_id = le32_to_cpu(p->domain_id);
254 r->reset_state = le32_to_cpu(p->reset_state);
255 *src_id = r->domain_id;
257 return r;
260 static const struct scmi_event reset_events[] = {
262 .id = SCMI_EVENT_RESET_ISSUED,
263 .max_payld_sz = sizeof(struct scmi_reset_issued_notify_payld),
264 .max_report_sz = sizeof(struct scmi_reset_issued_report),
268 static const struct scmi_event_ops reset_event_ops = {
269 .set_notify_enabled = scmi_reset_set_notify_enabled,
270 .fill_custom_report = scmi_reset_fill_custom_report,
273 static int scmi_reset_protocol_init(struct scmi_handle *handle)
275 int domain;
276 u32 version;
277 struct scmi_reset_info *pinfo;
279 scmi_version_get(handle, SCMI_PROTOCOL_RESET, &version);
281 dev_dbg(handle->dev, "Reset Version %d.%d\n",
282 PROTOCOL_REV_MAJOR(version), PROTOCOL_REV_MINOR(version));
284 pinfo = devm_kzalloc(handle->dev, sizeof(*pinfo), GFP_KERNEL);
285 if (!pinfo)
286 return -ENOMEM;
288 scmi_reset_attributes_get(handle, pinfo);
290 pinfo->dom_info = devm_kcalloc(handle->dev, pinfo->num_domains,
291 sizeof(*pinfo->dom_info), GFP_KERNEL);
292 if (!pinfo->dom_info)
293 return -ENOMEM;
295 for (domain = 0; domain < pinfo->num_domains; domain++) {
296 struct reset_dom_info *dom = pinfo->dom_info + domain;
298 scmi_reset_domain_attributes_get(handle, domain, dom);
301 scmi_register_protocol_events(handle,
302 SCMI_PROTOCOL_RESET, SCMI_PROTO_QUEUE_SZ,
303 &reset_event_ops, reset_events,
304 ARRAY_SIZE(reset_events),
305 pinfo->num_domains);
307 pinfo->version = version;
308 handle->reset_ops = &reset_ops;
309 handle->reset_priv = pinfo;
311 return 0;
314 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(SCMI_PROTOCOL_RESET, reset)