1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Base Protocol
5 * Copyright (C) 2018 ARM Ltd.
8 #define pr_fmt(fmt) "SCMI Notifications BASE - " fmt
10 #include <linux/scmi_protocol.h>
15 #define SCMI_BASE_NUM_SOURCES 1
16 #define SCMI_BASE_MAX_CMD_ERR_COUNT 1024
18 enum scmi_base_protocol_cmd
{
19 BASE_DISCOVER_VENDOR
= 0x3,
20 BASE_DISCOVER_SUB_VENDOR
= 0x4,
21 BASE_DISCOVER_IMPLEMENT_VERSION
= 0x5,
22 BASE_DISCOVER_LIST_PROTOCOLS
= 0x6,
23 BASE_DISCOVER_AGENT
= 0x7,
24 BASE_NOTIFY_ERRORS
= 0x8,
25 BASE_SET_DEVICE_PERMISSIONS
= 0x9,
26 BASE_SET_PROTOCOL_PERMISSIONS
= 0xa,
27 BASE_RESET_AGENT_CONFIGURATION
= 0xb,
30 struct scmi_msg_resp_base_attributes
{
36 struct scmi_msg_base_error_notify
{
38 #define BASE_TP_NOTIFY_ALL BIT(0)
41 struct scmi_base_error_notify_payld
{
44 #define IS_FATAL_ERROR(x) ((x) & BIT(31))
45 #define ERROR_CMD_COUNT(x) FIELD_GET(GENMASK(9, 0), (x))
46 __le64 msg_reports
[SCMI_BASE_MAX_CMD_ERR_COUNT
];
50 * scmi_base_attributes_get() - gets the implementation details
51 * that are associated with the base protocol.
53 * @handle: SCMI entity handle
55 * Return: 0 on success, else appropriate SCMI error.
57 static int scmi_base_attributes_get(const struct scmi_handle
*handle
)
61 struct scmi_msg_resp_base_attributes
*attr_info
;
62 struct scmi_revision_info
*rev
= handle
->version
;
64 ret
= scmi_xfer_get_init(handle
, PROTOCOL_ATTRIBUTES
,
65 SCMI_PROTOCOL_BASE
, 0, sizeof(*attr_info
), &t
);
69 ret
= scmi_do_xfer(handle
, t
);
71 attr_info
= t
->rx
.buf
;
72 rev
->num_protocols
= attr_info
->num_protocols
;
73 rev
->num_agents
= attr_info
->num_agents
;
76 scmi_xfer_put(handle
, t
);
82 * scmi_base_vendor_id_get() - gets vendor/subvendor identifier ASCII string.
84 * @handle: SCMI entity handle
85 * @sub_vendor: specify true if sub-vendor ID is needed
87 * Return: 0 on success, else appropriate SCMI error.
90 scmi_base_vendor_id_get(const struct scmi_handle
*handle
, bool sub_vendor
)
96 struct scmi_revision_info
*rev
= handle
->version
;
99 cmd
= BASE_DISCOVER_SUB_VENDOR
;
100 vendor_id
= rev
->sub_vendor_id
;
101 size
= ARRAY_SIZE(rev
->sub_vendor_id
);
103 cmd
= BASE_DISCOVER_VENDOR
;
104 vendor_id
= rev
->vendor_id
;
105 size
= ARRAY_SIZE(rev
->vendor_id
);
108 ret
= scmi_xfer_get_init(handle
, cmd
, SCMI_PROTOCOL_BASE
, 0, size
, &t
);
112 ret
= scmi_do_xfer(handle
, t
);
114 memcpy(vendor_id
, t
->rx
.buf
, size
);
116 scmi_xfer_put(handle
, t
);
122 * scmi_base_implementation_version_get() - gets a vendor-specific
123 * implementation 32-bit version. The format of the version number is
126 * @handle: SCMI entity handle
128 * Return: 0 on success, else appropriate SCMI error.
131 scmi_base_implementation_version_get(const struct scmi_handle
*handle
)
136 struct scmi_revision_info
*rev
= handle
->version
;
138 ret
= scmi_xfer_get_init(handle
, BASE_DISCOVER_IMPLEMENT_VERSION
,
139 SCMI_PROTOCOL_BASE
, 0, sizeof(*impl_ver
), &t
);
143 ret
= scmi_do_xfer(handle
, t
);
145 impl_ver
= t
->rx
.buf
;
146 rev
->impl_ver
= le32_to_cpu(*impl_ver
);
149 scmi_xfer_put(handle
, t
);
155 * scmi_base_implementation_list_get() - gets the list of protocols it is
156 * OSPM is allowed to access
158 * @handle: SCMI entity handle
159 * @protocols_imp: pointer to hold the list of protocol identifiers
161 * Return: 0 on success, else appropriate SCMI error.
163 static int scmi_base_implementation_list_get(const struct scmi_handle
*handle
,
169 __le32
*num_skip
, *num_ret
;
170 u32 tot_num_ret
= 0, loop_num_ret
;
171 struct device
*dev
= handle
->dev
;
173 ret
= scmi_xfer_get_init(handle
, BASE_DISCOVER_LIST_PROTOCOLS
,
174 SCMI_PROTOCOL_BASE
, sizeof(*num_skip
), 0, &t
);
178 num_skip
= t
->tx
.buf
;
180 list
= t
->rx
.buf
+ sizeof(*num_ret
);
183 /* Set the number of protocols to be skipped/already read */
184 *num_skip
= cpu_to_le32(tot_num_ret
);
186 ret
= scmi_do_xfer(handle
, t
);
190 loop_num_ret
= le32_to_cpu(*num_ret
);
191 if (tot_num_ret
+ loop_num_ret
> MAX_PROTOCOLS_IMP
) {
192 dev_err(dev
, "No. of Protocol > MAX_PROTOCOLS_IMP");
196 for (loop
= 0; loop
< loop_num_ret
; loop
++)
197 protocols_imp
[tot_num_ret
+ loop
] = *(list
+ loop
);
199 tot_num_ret
+= loop_num_ret
;
201 scmi_reset_rx_to_maxsz(handle
, t
);
202 } while (loop_num_ret
);
204 scmi_xfer_put(handle
, t
);
210 * scmi_base_discover_agent_get() - discover the name of an agent
212 * @handle: SCMI entity handle
213 * @id: Agent identifier
214 * @name: Agent identifier ASCII string
216 * An agent id of 0 is reserved to identify the platform itself.
217 * Generally operating system is represented as "OSPM"
219 * Return: 0 on success, else appropriate SCMI error.
221 static int scmi_base_discover_agent_get(const struct scmi_handle
*handle
,
227 ret
= scmi_xfer_get_init(handle
, BASE_DISCOVER_AGENT
,
228 SCMI_PROTOCOL_BASE
, sizeof(__le32
),
229 SCMI_MAX_STR_SIZE
, &t
);
233 put_unaligned_le32(id
, t
->tx
.buf
);
235 ret
= scmi_do_xfer(handle
, t
);
237 strlcpy(name
, t
->rx
.buf
, SCMI_MAX_STR_SIZE
);
239 scmi_xfer_put(handle
, t
);
244 static int scmi_base_error_notify(const struct scmi_handle
*handle
, bool enable
)
247 u32 evt_cntl
= enable
? BASE_TP_NOTIFY_ALL
: 0;
249 struct scmi_msg_base_error_notify
*cfg
;
251 ret
= scmi_xfer_get_init(handle
, BASE_NOTIFY_ERRORS
,
252 SCMI_PROTOCOL_BASE
, sizeof(*cfg
), 0, &t
);
257 cfg
->event_control
= cpu_to_le32(evt_cntl
);
259 ret
= scmi_do_xfer(handle
, t
);
261 scmi_xfer_put(handle
, t
);
265 static int scmi_base_set_notify_enabled(const struct scmi_handle
*handle
,
266 u8 evt_id
, u32 src_id
, bool enable
)
270 ret
= scmi_base_error_notify(handle
, enable
);
272 pr_debug("FAIL_ENABLED - evt[%X] ret:%d\n", evt_id
, ret
);
277 static void *scmi_base_fill_custom_report(const struct scmi_handle
*handle
,
278 u8 evt_id
, ktime_t timestamp
,
279 const void *payld
, size_t payld_sz
,
280 void *report
, u32
*src_id
)
283 const struct scmi_base_error_notify_payld
*p
= payld
;
284 struct scmi_base_error_report
*r
= report
;
287 * BaseError notification payload is variable in size but
288 * up to a maximum length determined by the struct ponted by p.
289 * Instead payld_sz is the effective length of this notification
290 * payload so cannot be greater of the maximum allowed size as
293 if (evt_id
!= SCMI_EVENT_BASE_ERROR_EVENT
|| sizeof(*p
) < payld_sz
)
296 r
->timestamp
= timestamp
;
297 r
->agent_id
= le32_to_cpu(p
->agent_id
);
298 r
->fatal
= IS_FATAL_ERROR(le32_to_cpu(p
->error_status
));
299 r
->cmd_count
= ERROR_CMD_COUNT(le32_to_cpu(p
->error_status
));
300 for (i
= 0; i
< r
->cmd_count
; i
++)
301 r
->reports
[i
] = le64_to_cpu(p
->msg_reports
[i
]);
307 static const struct scmi_event base_events
[] = {
309 .id
= SCMI_EVENT_BASE_ERROR_EVENT
,
310 .max_payld_sz
= sizeof(struct scmi_base_error_notify_payld
),
311 .max_report_sz
= sizeof(struct scmi_base_error_report
) +
312 SCMI_BASE_MAX_CMD_ERR_COUNT
* sizeof(u64
),
316 static const struct scmi_event_ops base_event_ops
= {
317 .set_notify_enabled
= scmi_base_set_notify_enabled
,
318 .fill_custom_report
= scmi_base_fill_custom_report
,
321 int scmi_base_protocol_init(struct scmi_handle
*h
)
326 char name
[SCMI_MAX_STR_SIZE
];
327 const struct scmi_handle
*handle
= h
;
328 struct device
*dev
= handle
->dev
;
329 struct scmi_revision_info
*rev
= handle
->version
;
331 ret
= scmi_version_get(handle
, SCMI_PROTOCOL_BASE
, &version
);
335 prot_imp
= devm_kcalloc(dev
, MAX_PROTOCOLS_IMP
, sizeof(u8
), GFP_KERNEL
);
339 rev
->major_ver
= PROTOCOL_REV_MAJOR(version
),
340 rev
->minor_ver
= PROTOCOL_REV_MINOR(version
);
342 scmi_base_attributes_get(handle
);
343 scmi_base_vendor_id_get(handle
, false);
344 scmi_base_vendor_id_get(handle
, true);
345 scmi_base_implementation_version_get(handle
);
346 scmi_base_implementation_list_get(handle
, prot_imp
);
347 scmi_setup_protocol_implemented(handle
, prot_imp
);
349 dev_info(dev
, "SCMI Protocol v%d.%d '%s:%s' Firmware version 0x%x\n",
350 rev
->major_ver
, rev
->minor_ver
, rev
->vendor_id
,
351 rev
->sub_vendor_id
, rev
->impl_ver
);
352 dev_dbg(dev
, "Found %d protocol(s) %d agent(s)\n", rev
->num_protocols
,
355 scmi_register_protocol_events(handle
, SCMI_PROTOCOL_BASE
,
356 (4 * SCMI_PROTO_QUEUE_SZ
),
357 &base_event_ops
, base_events
,
358 ARRAY_SIZE(base_events
),
359 SCMI_BASE_NUM_SOURCES
);
361 for (id
= 0; id
< rev
->num_agents
; id
++) {
362 scmi_base_discover_agent_get(handle
, id
, name
);
363 dev_dbg(dev
, "Agent %d: %s\n", id
, name
);