aboutsummaryrefslogtreecommitdiff
path: root/cmd/scmi.c
blob: cfbca63e1644e387dd80e1a445301eff39ec68b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// SPDX-License-Identifier: GPL-2.0+
/*
 *  SCMI (System Control and Management Interface) utility command
 *
 *  Copyright (c) 2023 Linaro Limited
 *		Author: AKASHI Takahiro
 */

#include <command.h>
#include <exports.h>
#include <scmi_agent.h>
#include <scmi_agent-uclass.h>
#include <stdlib.h>
#include <asm/types.h>
#include <dm/device.h>
#include <dm/uclass.h> /* uclass_get_device */
#include <linux/bitfield.h>
#include <linux/bitops.h>

struct {
	enum scmi_std_protocol id;
	const char *name;
} protocol_name[] = {
	{SCMI_PROTOCOL_ID_BASE, "Base"},
	{SCMI_PROTOCOL_ID_POWER_DOMAIN, "Power domain management"},
	{SCMI_PROTOCOL_ID_SYSTEM, "System power management"},
	{SCMI_PROTOCOL_ID_PERF, "Performance domain management"},
	{SCMI_PROTOCOL_ID_CLOCK, "Clock management"},
	{SCMI_PROTOCOL_ID_SENSOR, "Sensor management"},
	{SCMI_PROTOCOL_ID_RESET_DOMAIN, "Reset domain management"},
	{SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN, "Voltage domain management"},
};

/**
 * get_agent() - get SCMI agent device
 *
 * Return:	Pointer to SCMI agent device on success, NULL on failure
 */
static struct udevice *get_agent(void)
{
	struct udevice *agent;

	if (uclass_get_device(UCLASS_SCMI_AGENT, 0, &agent)) {
		printf("Cannot find any SCMI agent\n");
		return NULL;
	}

	return agent;
}

/**
 * get_base_proto() - get SCMI base protocol device
 * @agent:	SCMI agent device
 *
 * Return:	Pointer to SCMI base protocol device on success,
 *		NULL on failure
 */
static struct udevice *get_base_proto(struct udevice *agent)
{
	struct udevice *base_proto;

	if (!agent) {
		agent = get_agent();
		if (!agent)
			return NULL;
	}

	base_proto = scmi_get_protocol(agent, SCMI_PROTOCOL_ID_BASE);
	if (!base_proto) {
		printf("SCMI base protocol not found\n");
		return NULL;
	}

	return base_proto;
}

/**
 * get_proto_name() - get the name of SCMI protocol
 *
 * @id:		SCMI Protocol ID
 *
 * Get the printable name of the protocol, @id
 *
 * Return:	Name string on success, NULL on failure
 */
static const char *get_proto_name(enum scmi_std_protocol id)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(protocol_name); i++)
		if (id == protocol_name[i].id)
			return protocol_name[i].name;

	return NULL;
}

/**
 * do_scmi_info() - get the information of SCMI services
 *
 * @cmdtp:	Command table
 * @flag:	Command flag
 * @argc:	Number of arguments
 * @argv:	Argument array
 *
 * Get the information of SCMI services using various interfaces
 * provided by the Base protocol.
 *
 * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
 */
static int do_scmi_info(struct cmd_tbl *cmdtp, int flag, int argc,
			char * const argv[])
{
	struct udevice *agent, *base_proto;
	u32 agent_id, num_protocols;
	u8 *agent_name, *protocols;
	int i, ret;

	if (argc != 1)
		return CMD_RET_USAGE;

	agent = get_agent();
	if (!agent)
		return CMD_RET_FAILURE;
	base_proto = get_base_proto(agent);
	if (!base_proto)
		return CMD_RET_FAILURE;

	printf("SCMI device: %s\n", agent->name);
	printf("  protocol version: 0x%x\n", scmi_version(agent));
	printf("  # of agents: %d\n", scmi_num_agents(agent));
	for (i = 0; i < scmi_num_agents(agent); i++) {
		ret = scmi_base_discover_agent(base_proto, i, &agent_id,
					       &agent_name);
		if (ret) {
			if (ret != -EOPNOTSUPP)
				printf("base_discover_agent() failed for id: %d (%d)\n",
				       i, ret);
			break;
		}
		printf("    %c%2d: %s\n", i == scmi_agent_id(agent) ? '>' : ' ',
		       i, agent_name);
		free(agent_name);
	}
	printf("  # of protocols: %d\n", scmi_num_protocols(agent));
	num_protocols = scmi_num_protocols(agent);
	protocols = scmi_protocols(agent);
	if (protocols)
		for (i = 0; i < num_protocols; i++)
			printf("      %s\n", get_proto_name(protocols[i]));
	printf("  vendor: %s\n", scmi_vendor(agent));
	printf("  sub vendor: %s\n", scmi_sub_vendor(agent));
	printf("  impl version: 0x%x\n", scmi_impl_version(agent));

	return CMD_RET_SUCCESS;
}

/**
 * do_scmi_set_dev() - set access permission to device
 *
 * @cmdtp:	Command table
 * @flag:	Command flag
 * @argc:	Number of arguments
 * @argv:	Argument array
 *
 * Set access permission to device with SCMI_BASE_SET_DEVICE_PERMISSIONS
 *
 * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
 */
static int do_scmi_set_dev(struct cmd_tbl *cmdtp, int flag, int argc,
			   char * const argv[])
{
	u32 agent_id, device_id, flags, attributes;
	char *end;
	struct udevice *base_proto;
	int ret;

	if (argc != 4)
		return CMD_RET_USAGE;

	agent_id = simple_strtoul(argv[1], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	device_id = simple_strtoul(argv[2], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	flags = simple_strtoul(argv[3], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	base_proto = get_base_proto(NULL);
	if (!base_proto)
		return CMD_RET_FAILURE;

	ret = scmi_base_protocol_message_attrs(base_proto,
					       SCMI_BASE_SET_DEVICE_PERMISSIONS,
					       &attributes);
	if (ret) {
		printf("This operation is not supported\n");
		return CMD_RET_FAILURE;
	}

	ret = scmi_base_set_device_permissions(base_proto, agent_id,
					       device_id, flags);
	if (ret) {
		printf("%s access to device:%u failed (%d)\n",
		       flags ? "Allowing" : "Denying", device_id, ret);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

/**
 * do_scmi_set_proto() - set protocol permission to device
 *
 * @cmdtp:	Command table
 * @flag:	Command flag
 * @argc:	Number of arguments
 * @argv:	Argument array
 *
 * Set protocol permission to device with SCMI_BASE_SET_PROTOCOL_PERMISSIONS
 *
 * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
 */
static int do_scmi_set_proto(struct cmd_tbl *cmdtp, int flag, int argc,
			     char * const argv[])
{
	u32 agent_id, device_id, protocol_id, flags, attributes;
	char *end;
	struct udevice *base_proto;
	int ret;

	if (argc != 5)
		return CMD_RET_USAGE;

	agent_id = simple_strtoul(argv[1], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	device_id = simple_strtoul(argv[2], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	protocol_id = simple_strtoul(argv[3], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	flags = simple_strtoul(argv[4], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	base_proto = get_base_proto(NULL);
	if (!base_proto)
		return CMD_RET_FAILURE;

	ret = scmi_base_protocol_message_attrs(base_proto,
					       SCMI_BASE_SET_PROTOCOL_PERMISSIONS,
					       &attributes);
	if (ret) {
		printf("This operation is not supported\n");
		return CMD_RET_FAILURE;
	}

	ret = scmi_base_set_protocol_permissions(base_proto, agent_id,
						 device_id, protocol_id,
						 flags);
	if (ret) {
		printf("%s access to protocol:0x%x on device:%u failed (%d)\n",
		       flags ? "Allowing" : "Denying", protocol_id, device_id,
		       ret);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

/**
 * do_scmi_reset() - reset platform resource settings
 *
 * @cmdtp:	Command table
 * @flag:	Command flag
 * @argc:	Number of arguments
 * @argv:	Argument array
 *
 * Reset platform resource settings with BASE_RESET_AGENT_CONFIGURATION
 *
 * Return:	CMD_RET_SUCCESS on success, CMD_RET_RET_FAILURE on failure
 */
static int do_scmi_reset(struct cmd_tbl *cmdtp, int flag, int argc,
			 char * const argv[])
{
	u32 agent_id, flags, attributes;
	char *end;
	struct udevice *base_proto;
	int ret;

	if (argc != 3)
		return CMD_RET_USAGE;

	agent_id = simple_strtoul(argv[1], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	flags = simple_strtoul(argv[2], &end, 16);
	if (*end != '\0')
		return CMD_RET_USAGE;

	base_proto = get_base_proto(NULL);
	if (!base_proto)
		return CMD_RET_FAILURE;

	ret = scmi_base_protocol_message_attrs(base_proto,
					       SCMI_BASE_RESET_AGENT_CONFIGURATION,
					       &attributes);
	if (ret) {
		printf("Reset is not supported\n");
		return CMD_RET_FAILURE;
	}

	ret = scmi_base_reset_agent_configuration(base_proto, agent_id, flags);
	if (ret) {
		printf("Reset failed (%d)\n", ret);
		return CMD_RET_FAILURE;
	}

	return CMD_RET_SUCCESS;
}

static struct cmd_tbl cmd_scmi_sub[] = {
	U_BOOT_CMD_MKENT(info, CONFIG_SYS_MAXARGS, 1,
			 do_scmi_info, "", ""),
	U_BOOT_CMD_MKENT(perm_dev, CONFIG_SYS_MAXARGS, 1,
			 do_scmi_set_dev, "", ""),
	U_BOOT_CMD_MKENT(perm_proto, CONFIG_SYS_MAXARGS, 1,
			 do_scmi_set_proto, "", ""),
	U_BOOT_CMD_MKENT(reset, CONFIG_SYS_MAXARGS, 1,
			 do_scmi_reset, "", ""),
};

/**
 * do_scmi() - SCMI utility
 *
 * @cmdtp:	Command table
 * @flag:	Command flag
 * @argc:	Number of arguments
 * @argv:	Argument array
 *
 * Provide user interfaces to SCMI protocols.
 *
 * Return:	CMD_RET_SUCCESS on success,
 *		CMD_RET_USAGE or CMD_RET_RET_FAILURE on failure
 */
static int do_scmi(struct cmd_tbl *cmdtp, int flag,
		   int argc, char *const argv[])
{
	struct cmd_tbl *cp;

	if (argc < 2)
		return CMD_RET_USAGE;

	argc--; argv++;

	cp = find_cmd_tbl(argv[0], cmd_scmi_sub, ARRAY_SIZE(cmd_scmi_sub));
	if (!cp)
		return CMD_RET_USAGE;

	return cp->cmd(cmdtp, flag, argc, argv);
}

U_BOOT_LONGHELP(scmi,
	" - SCMI utility\n"
	" info - get the info of SCMI services\n"
	" perm_dev <agent-id in hex> <device-id in hex> <flags in hex>\n"
	"   - set access permission to device\n"
	" perm_proto <agent-id in hex> <device-id in hex> <protocol-id in hex> <flags in hex>\n"
	"   - set protocol permission to device\n"
	" reset <agent-id in hex> <flags in hex>\n"
	"   - reset platform resource settings\n");

U_BOOT_CMD(scmi, CONFIG_SYS_MAXARGS, 0, do_scmi, "SCMI utility",
	   scmi_help_text);