1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Reset Protocol
5 * Copyright (C) 2019 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications RESET - " fmt
10 #include <linux/scmi_protocol.h>
15 enum scmi_reset_protocol_cmd
{
16 RESET_DOMAIN_ATTRIBUTES
= 0x3,
21 #define NUM_RESET_DOMAIN_MASK 0xffff
22 #define RESET_NOTIFY_ENABLE BIT(0)
24 struct scmi_msg_resp_reset_domain_attributes
{
26 #define SUPPORTS_ASYNC_RESET(x) ((x) & BIT(31))
27 #define SUPPORTS_NOTIFY_RESET(x) ((x) & BIT(30))
29 u8 name
[SCMI_MAX_STR_SIZE
];
32 struct scmi_msg_reset_domain_reset
{
35 #define AUTONOMOUS_RESET BIT(0)
36 #define EXPLICIT_RESET_ASSERT BIT(1)
37 #define ASYNCHRONOUS_RESET BIT(2)
39 #define ARCH_COLD_RESET 0
42 struct scmi_msg_reset_notify
{
45 #define RESET_TP_NOTIFY_ALL BIT(0)
48 struct scmi_reset_issued_notify_payld
{
54 struct reset_dom_info
{
58 char name
[SCMI_MAX_STR_SIZE
];
61 struct scmi_reset_info
{
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
)
74 ret
= scmi_xfer_get_init(handle
, PROTOCOL_ATTRIBUTES
,
75 SCMI_PROTOCOL_RESET
, 0, sizeof(attr
), &t
);
79 ret
= scmi_do_xfer(handle
, t
);
81 attr
= get_unaligned_le32(t
->rx
.buf
);
82 pi
->num_domains
= attr
& NUM_RESET_DOMAIN_MASK
;
85 scmi_xfer_put(handle
, t
);
90 scmi_reset_domain_attributes_get(const struct scmi_handle
*handle
, u32 domain
,
91 struct reset_dom_info
*dom_info
)
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
),
103 put_unaligned_le32(domain
, t
->tx
.buf
);
106 ret
= scmi_do_xfer(handle
, 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 scmi_xfer_put(handle
, t
);
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
;
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
)
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
);
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
);
170 ret
= scmi_do_xfer(handle
, t
);
172 scmi_xfer_put(handle
, t
);
176 static int scmi_reset_domain_reset(const struct scmi_handle
*handle
, u32 domain
)
178 return scmi_domain_reset(handle
, domain
, AUTONOMOUS_RESET
,
183 scmi_reset_domain_assert(const struct scmi_handle
*handle
, u32 domain
)
185 return scmi_domain_reset(handle
, domain
, EXPLICIT_RESET_ASSERT
,
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
,
208 u32 evt_cntl
= enable
? RESET_TP_NOTIFY_ALL
: 0;
210 struct scmi_msg_reset_notify
*cfg
;
212 ret
= scmi_xfer_get_init(handle
, RESET_NOTIFY
,
213 SCMI_PROTOCOL_RESET
, sizeof(*cfg
), 0, &t
);
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
);
227 static int scmi_reset_set_notify_enabled(const struct scmi_handle
*handle
,
228 u8 evt_id
, u32 src_id
, bool enable
)
232 ret
= scmi_reset_notify(handle
, src_id
, enable
);
234 pr_debug("FAIL_ENABLED - evt[%X] dom[%d] - ret:%d\n",
235 evt_id
, src_id
, 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
)
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
;
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
)
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
);
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
)
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
),
307 pinfo
->version
= version
;
308 handle
->reset_ops
= &reset_ops
;
309 handle
->reset_priv
= pinfo
;
314 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(SCMI_PROTOCOL_RESET
, reset
)