blob: f986ee8919f0392dd1866036f45f75e938c8300f [file] [log] [blame]
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * System Control and Management Interface (SCMI) Base Protocol
4 *
5 * Copyright (C) 2018 ARM Ltd.
6 */
7
8#include "common.h"
9
10enum scmi_base_protocol_cmd {
11 BASE_DISCOVER_VENDOR = 0x3,
12 BASE_DISCOVER_SUB_VENDOR = 0x4,
13 BASE_DISCOVER_IMPLEMENT_VERSION = 0x5,
14 BASE_DISCOVER_LIST_PROTOCOLS = 0x6,
15 BASE_DISCOVER_AGENT = 0x7,
16 BASE_NOTIFY_ERRORS = 0x8,
17};
18
19struct scmi_msg_resp_base_attributes {
20 u8 num_protocols;
21 u8 num_agents;
22 __le16 reserved;
23};
24
25/**
26 * scmi_base_attributes_get() - gets the implementation details
27 * that are associated with the base protocol.
28 *
29 * @handle: SCMI entity handle
30 *
31 * Return: 0 on success, else appropriate SCMI error.
32 */
33static int scmi_base_attributes_get(const struct scmi_handle *handle)
34{
35 int ret;
36 struct scmi_xfer *t;
37 struct scmi_msg_resp_base_attributes *attr_info;
38 struct scmi_revision_info *rev = handle->version;
39
40 ret = scmi_xfer_get_init(handle, PROTOCOL_ATTRIBUTES,
41 SCMI_PROTOCOL_BASE, 0, sizeof(*attr_info), &t);
42 if (ret)
43 return ret;
44
45 ret = scmi_do_xfer(handle, t);
46 if (!ret) {
47 attr_info = t->rx.buf;
48 rev->num_protocols = attr_info->num_protocols;
49 rev->num_agents = attr_info->num_agents;
50 }
51
52 scmi_xfer_put(handle, t);
53
54 return ret;
55}
56
57/**
58 * scmi_base_vendor_id_get() - gets vendor/subvendor identifier ASCII string.
59 *
60 * @handle: SCMI entity handle
61 * @sub_vendor: specify true if sub-vendor ID is needed
62 *
63 * Return: 0 on success, else appropriate SCMI error.
64 */
65static int
66scmi_base_vendor_id_get(const struct scmi_handle *handle, bool sub_vendor)
67{
68 u8 cmd;
69 int ret, size;
70 char *vendor_id;
71 struct scmi_xfer *t;
72 struct scmi_revision_info *rev = handle->version;
73
74 if (sub_vendor) {
75 cmd = BASE_DISCOVER_SUB_VENDOR;
76 vendor_id = rev->sub_vendor_id;
77 size = ARRAY_SIZE(rev->sub_vendor_id);
78 } else {
79 cmd = BASE_DISCOVER_VENDOR;
80 vendor_id = rev->vendor_id;
81 size = ARRAY_SIZE(rev->vendor_id);
82 }
83
84 ret = scmi_xfer_get_init(handle, cmd, SCMI_PROTOCOL_BASE, 0, size, &t);
85 if (ret)
86 return ret;
87
88 ret = scmi_do_xfer(handle, t);
89 if (!ret)
90 memcpy(vendor_id, t->rx.buf, size);
91
92 scmi_xfer_put(handle, t);
93
94 return ret;
95}
96
97/**
98 * scmi_base_implementation_version_get() - gets a vendor-specific
99 * implementation 32-bit version. The format of the version number is
100 * vendor-specific
101 *
102 * @handle: SCMI entity handle
103 *
104 * Return: 0 on success, else appropriate SCMI error.
105 */
106static int
107scmi_base_implementation_version_get(const struct scmi_handle *handle)
108{
109 int ret;
110 __le32 *impl_ver;
111 struct scmi_xfer *t;
112 struct scmi_revision_info *rev = handle->version;
113
114 ret = scmi_xfer_get_init(handle, BASE_DISCOVER_IMPLEMENT_VERSION,
115 SCMI_PROTOCOL_BASE, 0, sizeof(*impl_ver), &t);
116 if (ret)
117 return ret;
118
119 ret = scmi_do_xfer(handle, t);
120 if (!ret) {
121 impl_ver = t->rx.buf;
122 rev->impl_ver = le32_to_cpu(*impl_ver);
123 }
124
125 scmi_xfer_put(handle, t);
126
127 return ret;
128}
129
130/**
131 * scmi_base_implementation_list_get() - gets the list of protocols it is
132 * OSPM is allowed to access
133 *
134 * @handle: SCMI entity handle
135 * @protocols_imp: pointer to hold the list of protocol identifiers
136 *
137 * Return: 0 on success, else appropriate SCMI error.
138 */
139static int scmi_base_implementation_list_get(const struct scmi_handle *handle,
140 u8 *protocols_imp)
141{
142 u8 *list;
143 int ret, loop;
144 struct scmi_xfer *t;
145 __le32 *num_skip, *num_ret;
146 u32 tot_num_ret = 0, loop_num_ret;
147 struct device *dev = handle->dev;
148
149 ret = scmi_xfer_get_init(handle, BASE_DISCOVER_LIST_PROTOCOLS,
150 SCMI_PROTOCOL_BASE, sizeof(*num_skip), 0, &t);
151 if (ret)
152 return ret;
153
154 num_skip = t->tx.buf;
155 num_ret = t->rx.buf;
156 list = t->rx.buf + sizeof(*num_ret);
157
158 do {
159 /* Set the number of protocols to be skipped/already read */
160 *num_skip = cpu_to_le32(tot_num_ret);
161
162 ret = scmi_do_xfer(handle, t);
163 if (ret)
164 break;
165
166 loop_num_ret = le32_to_cpu(*num_ret);
167 if (tot_num_ret + loop_num_ret > MAX_PROTOCOLS_IMP) {
168 dev_err(dev, "No. of Protocol > MAX_PROTOCOLS_IMP");
169 break;
170 }
171
172 for (loop = 0; loop < loop_num_ret; loop++)
173 protocols_imp[tot_num_ret + loop] = *(list + loop);
174
175 tot_num_ret += loop_num_ret;
Olivier Deprez0e641232021-09-23 10:07:05 +0200176
177 scmi_reset_rx_to_maxsz(handle, t);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000178 } while (loop_num_ret);
179
180 scmi_xfer_put(handle, t);
181
182 return ret;
183}
184
185/**
186 * scmi_base_discover_agent_get() - discover the name of an agent
187 *
188 * @handle: SCMI entity handle
189 * @id: Agent identifier
190 * @name: Agent identifier ASCII string
191 *
192 * An agent id of 0 is reserved to identify the platform itself.
193 * Generally operating system is represented as "OSPM"
194 *
195 * Return: 0 on success, else appropriate SCMI error.
196 */
197static int scmi_base_discover_agent_get(const struct scmi_handle *handle,
198 int id, char *name)
199{
200 int ret;
201 struct scmi_xfer *t;
202
203 ret = scmi_xfer_get_init(handle, BASE_DISCOVER_AGENT,
204 SCMI_PROTOCOL_BASE, sizeof(__le32),
205 SCMI_MAX_STR_SIZE, &t);
206 if (ret)
207 return ret;
208
David Brazdil0f672f62019-12-10 10:32:29 +0000209 put_unaligned_le32(id, t->tx.buf);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000210
211 ret = scmi_do_xfer(handle, t);
212 if (!ret)
David Brazdil0f672f62019-12-10 10:32:29 +0000213 strlcpy(name, t->rx.buf, SCMI_MAX_STR_SIZE);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000214
215 scmi_xfer_put(handle, t);
216
217 return ret;
218}
219
220int scmi_base_protocol_init(struct scmi_handle *h)
221{
222 int id, ret;
223 u8 *prot_imp;
224 u32 version;
225 char name[SCMI_MAX_STR_SIZE];
226 const struct scmi_handle *handle = h;
227 struct device *dev = handle->dev;
228 struct scmi_revision_info *rev = handle->version;
229
230 ret = scmi_version_get(handle, SCMI_PROTOCOL_BASE, &version);
231 if (ret)
232 return ret;
233
234 prot_imp = devm_kcalloc(dev, MAX_PROTOCOLS_IMP, sizeof(u8), GFP_KERNEL);
235 if (!prot_imp)
236 return -ENOMEM;
237
238 rev->major_ver = PROTOCOL_REV_MAJOR(version),
239 rev->minor_ver = PROTOCOL_REV_MINOR(version);
240
241 scmi_base_attributes_get(handle);
242 scmi_base_vendor_id_get(handle, false);
243 scmi_base_vendor_id_get(handle, true);
244 scmi_base_implementation_version_get(handle);
245 scmi_base_implementation_list_get(handle, prot_imp);
246 scmi_setup_protocol_implemented(handle, prot_imp);
247
248 dev_info(dev, "SCMI Protocol v%d.%d '%s:%s' Firmware version 0x%x\n",
249 rev->major_ver, rev->minor_ver, rev->vendor_id,
250 rev->sub_vendor_id, rev->impl_ver);
251 dev_dbg(dev, "Found %d protocol(s) %d agent(s)\n", rev->num_protocols,
252 rev->num_agents);
253
254 for (id = 0; id < rev->num_agents; id++) {
255 scmi_base_discover_agent_get(handle, id, name);
256 dev_dbg(dev, "Agent %d: %s\n", id, name);
257 }
258
259 return 0;
260}