1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Base Protocol
5 * Copyright (C) 2018-2021 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications BASE - " fmt
10 #include <linux/module.h>
11 #include <linux/scmi_protocol.h>
16 #define SCMI_BASE_NUM_SOURCES 1
17 #define SCMI_BASE_MAX_CMD_ERR_COUNT 1024
19 enum scmi_base_protocol_cmd {
20 BASE_DISCOVER_VENDOR = 0x3,
21 BASE_DISCOVER_SUB_VENDOR = 0x4,
22 BASE_DISCOVER_IMPLEMENT_VERSION = 0x5,
23 BASE_DISCOVER_LIST_PROTOCOLS = 0x6,
24 BASE_DISCOVER_AGENT = 0x7,
25 BASE_NOTIFY_ERRORS = 0x8,
26 BASE_SET_DEVICE_PERMISSIONS = 0x9,
27 BASE_SET_PROTOCOL_PERMISSIONS = 0xa,
28 BASE_RESET_AGENT_CONFIGURATION = 0xb,
31 struct scmi_msg_resp_base_attributes {
37 struct scmi_msg_base_error_notify {
39 #define BASE_TP_NOTIFY_ALL BIT(0)
42 struct scmi_base_error_notify_payld {
45 #define IS_FATAL_ERROR(x) ((x) & BIT(31))
46 #define ERROR_CMD_COUNT(x) FIELD_GET(GENMASK(9, 0), (x))
47 __le64 msg_reports[SCMI_BASE_MAX_CMD_ERR_COUNT];
51 * scmi_base_attributes_get() - gets the implementation details
52 * that are associated with the base protocol.
54 * @ph: SCMI protocol handle
56 * Return: 0 on success, else appropriate SCMI error.
58 static int scmi_base_attributes_get(const struct scmi_protocol_handle *ph)
62 struct scmi_msg_resp_base_attributes *attr_info;
63 struct scmi_revision_info *rev = ph->get_priv(ph);
65 ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES,
66 0, sizeof(*attr_info), &t);
70 ret = ph->xops->do_xfer(ph, t);
72 attr_info = t->rx.buf;
73 rev->num_protocols = attr_info->num_protocols;
74 rev->num_agents = attr_info->num_agents;
77 ph->xops->xfer_put(ph, t);
83 * scmi_base_vendor_id_get() - gets vendor/subvendor identifier ASCII string.
85 * @ph: SCMI protocol handle
86 * @sub_vendor: specify true if sub-vendor ID is needed
88 * Return: 0 on success, else appropriate SCMI error.
91 scmi_base_vendor_id_get(const struct scmi_protocol_handle *ph, bool sub_vendor)
97 struct scmi_revision_info *rev = ph->get_priv(ph);
101 cmd = BASE_DISCOVER_SUB_VENDOR;
102 vendor_id = rev->sub_vendor_id;
103 size = ARRAY_SIZE(rev->sub_vendor_id);
105 cmd = BASE_DISCOVER_VENDOR;
106 vendor_id = rev->vendor_id;
107 size = ARRAY_SIZE(rev->vendor_id);
110 ret = ph->xops->xfer_get_init(ph, cmd, 0, size, &t);
114 ret = ph->xops->do_xfer(ph, t);
116 memcpy(vendor_id, t->rx.buf, size);
118 ph->xops->xfer_put(ph, t);
124 * scmi_base_implementation_version_get() - gets a vendor-specific
125 * implementation 32-bit version. The format of the version number is
128 * @ph: SCMI protocol handle
130 * Return: 0 on success, else appropriate SCMI error.
133 scmi_base_implementation_version_get(const struct scmi_protocol_handle *ph)
138 struct scmi_revision_info *rev = ph->get_priv(ph);
140 ret = ph->xops->xfer_get_init(ph, BASE_DISCOVER_IMPLEMENT_VERSION,
141 0, sizeof(*impl_ver), &t);
145 ret = ph->xops->do_xfer(ph, t);
147 impl_ver = t->rx.buf;
148 rev->impl_ver = le32_to_cpu(*impl_ver);
151 ph->xops->xfer_put(ph, t);
157 * scmi_base_implementation_list_get() - gets the list of protocols it is
158 * OSPM is allowed to access
160 * @ph: SCMI protocol handle
161 * @protocols_imp: pointer to hold the list of protocol identifiers
163 * Return: 0 on success, else appropriate SCMI error.
166 scmi_base_implementation_list_get(const struct scmi_protocol_handle *ph,
172 __le32 *num_skip, *num_ret;
173 u32 tot_num_ret = 0, loop_num_ret;
174 struct device *dev = ph->dev;
176 ret = ph->xops->xfer_get_init(ph, BASE_DISCOVER_LIST_PROTOCOLS,
177 sizeof(*num_skip), 0, &t);
181 num_skip = t->tx.buf;
183 list = t->rx.buf + sizeof(*num_ret);
186 /* Set the number of protocols to be skipped/already read */
187 *num_skip = cpu_to_le32(tot_num_ret);
189 ret = ph->xops->do_xfer(ph, t);
193 loop_num_ret = le32_to_cpu(*num_ret);
194 if (tot_num_ret + loop_num_ret > MAX_PROTOCOLS_IMP) {
195 dev_err(dev, "No. of Protocol > MAX_PROTOCOLS_IMP");
199 for (loop = 0; loop < loop_num_ret; loop++)
200 protocols_imp[tot_num_ret + loop] = *(list + loop);
202 tot_num_ret += loop_num_ret;
204 ph->xops->reset_rx_to_maxsz(ph, t);
205 } while (loop_num_ret);
207 ph->xops->xfer_put(ph, t);
213 * scmi_base_discover_agent_get() - discover the name of an agent
215 * @ph: SCMI protocol handle
216 * @id: Agent identifier
217 * @name: Agent identifier ASCII string
219 * An agent id of 0 is reserved to identify the platform itself.
220 * Generally operating system is represented as "OSPM"
222 * Return: 0 on success, else appropriate SCMI error.
224 static int scmi_base_discover_agent_get(const struct scmi_protocol_handle *ph,
230 ret = ph->xops->xfer_get_init(ph, BASE_DISCOVER_AGENT,
231 sizeof(__le32), SCMI_MAX_STR_SIZE, &t);
235 put_unaligned_le32(id, t->tx.buf);
237 ret = ph->xops->do_xfer(ph, t);
239 strlcpy(name, t->rx.buf, SCMI_MAX_STR_SIZE);
241 ph->xops->xfer_put(ph, t);
246 static int scmi_base_error_notify(const struct scmi_protocol_handle *ph,
250 u32 evt_cntl = enable ? BASE_TP_NOTIFY_ALL : 0;
252 struct scmi_msg_base_error_notify *cfg;
254 ret = ph->xops->xfer_get_init(ph, BASE_NOTIFY_ERRORS,
255 sizeof(*cfg), 0, &t);
260 cfg->event_control = cpu_to_le32(evt_cntl);
262 ret = ph->xops->do_xfer(ph, t);
264 ph->xops->xfer_put(ph, t);
268 static int scmi_base_set_notify_enabled(const struct scmi_protocol_handle *ph,
269 u8 evt_id, u32 src_id, bool enable)
273 ret = scmi_base_error_notify(ph, enable);
275 pr_debug("FAIL_ENABLED - evt[%X] ret:%d\n", evt_id, ret);
280 static void *scmi_base_fill_custom_report(const struct scmi_protocol_handle *ph,
281 u8 evt_id, ktime_t timestamp,
282 const void *payld, size_t payld_sz,
283 void *report, u32 *src_id)
286 const struct scmi_base_error_notify_payld *p = payld;
287 struct scmi_base_error_report *r = report;
290 * BaseError notification payload is variable in size but
291 * up to a maximum length determined by the struct ponted by p.
292 * Instead payld_sz is the effective length of this notification
293 * payload so cannot be greater of the maximum allowed size as
296 if (evt_id != SCMI_EVENT_BASE_ERROR_EVENT || sizeof(*p) < payld_sz)
299 r->timestamp = timestamp;
300 r->agent_id = le32_to_cpu(p->agent_id);
301 r->fatal = IS_FATAL_ERROR(le32_to_cpu(p->error_status));
302 r->cmd_count = ERROR_CMD_COUNT(le32_to_cpu(p->error_status));
303 for (i = 0; i < r->cmd_count; i++)
304 r->reports[i] = le64_to_cpu(p->msg_reports[i]);
310 static const struct scmi_event base_events[] = {
312 .id = SCMI_EVENT_BASE_ERROR_EVENT,
313 .max_payld_sz = sizeof(struct scmi_base_error_notify_payld),
314 .max_report_sz = sizeof(struct scmi_base_error_report) +
315 SCMI_BASE_MAX_CMD_ERR_COUNT * sizeof(u64),
319 static const struct scmi_event_ops base_event_ops = {
320 .set_notify_enabled = scmi_base_set_notify_enabled,
321 .fill_custom_report = scmi_base_fill_custom_report,
324 static const struct scmi_protocol_events base_protocol_events = {
325 .queue_sz = 4 * SCMI_PROTO_QUEUE_SZ,
326 .ops = &base_event_ops,
328 .num_events = ARRAY_SIZE(base_events),
329 .num_sources = SCMI_BASE_NUM_SOURCES,
332 static int scmi_base_protocol_init(const struct scmi_protocol_handle *ph)
337 char name[SCMI_MAX_STR_SIZE];
338 struct device *dev = ph->dev;
339 struct scmi_revision_info *rev = scmi_revision_area_get(ph);
341 ret = ph->xops->version_get(ph, &version);
345 prot_imp = devm_kcalloc(dev, MAX_PROTOCOLS_IMP, sizeof(u8), GFP_KERNEL);
349 rev->major_ver = PROTOCOL_REV_MAJOR(version),
350 rev->minor_ver = PROTOCOL_REV_MINOR(version);
351 ph->set_priv(ph, rev);
353 scmi_base_attributes_get(ph);
354 scmi_base_vendor_id_get(ph, false);
355 scmi_base_vendor_id_get(ph, true);
356 scmi_base_implementation_version_get(ph);
357 scmi_base_implementation_list_get(ph, prot_imp);
359 scmi_setup_protocol_implemented(ph, prot_imp);
361 dev_info(dev, "SCMI Protocol v%d.%d '%s:%s' Firmware version 0x%x\n",
362 rev->major_ver, rev->minor_ver, rev->vendor_id,
363 rev->sub_vendor_id, rev->impl_ver);
364 dev_dbg(dev, "Found %d protocol(s) %d agent(s)\n", rev->num_protocols,
367 for (id = 0; id < rev->num_agents; id++) {
368 scmi_base_discover_agent_get(ph, id, name);
369 dev_dbg(dev, "Agent %d: %s\n", id, name);
375 static const struct scmi_protocol scmi_base = {
376 .id = SCMI_PROTOCOL_BASE,
378 .instance_init = &scmi_base_protocol_init,
380 .events = &base_protocol_events,
383 DEFINE_SCMI_PROTOCOL_REGISTER_UNREGISTER(base, scmi_base)