aboutsummaryrefslogtreecommitdiff
path: root/drivers/scmi-msg/power_domain.c
blob: c4e1289a245faaa64d901895fa958b43d6ff33b8 (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
// SPDX-License-Identifier: BSD-3-Clause
/*
 * Copyright (c) 2015-2020, Arm Limited and Contributors. All rights reserved.
 * Copyright (c) 2019-2020, Linaro Limited
 */
#include <cdefs.h>
#include <string.h>

#include <drivers/scmi-msg.h>
#include <drivers/scmi.h>
#include <lib/utils_def.h>

#include "common.h"

#pragma weak plat_scmi_pd_count
#pragma weak plat_scmi_pd_get_name
#pragma weak plat_scmi_pd_get_state
#pragma weak plat_scmi_pd_set_state
#pragma weak plat_scmi_pd_statistics
#pragma weak plat_scmi_pd_get_attributes

static bool message_id_is_supported(size_t message_id);

size_t plat_scmi_pd_count(unsigned int agent_id __unused)
{
	return 0U;
}

const char *plat_scmi_pd_get_name(unsigned int agent_id __unused,
				  unsigned int pd_id __unused)
{
	return NULL;
}

unsigned int plat_scmi_pd_statistics(unsigned int agent_id __unused,
				     unsigned long *pd_id __unused)
{
	return 0U;
}

unsigned int plat_scmi_pd_get_attributes(unsigned int agent_id __unused,
					 unsigned int pd_id __unused)
{
	return 0U;
}

unsigned int plat_scmi_pd_get_state(unsigned int agent_id __unused,
				    unsigned int pd_id __unused)
{
	return 0U;
}

int32_t plat_scmi_pd_set_state(unsigned int agent_id __unused,
			       unsigned int flags __unused,
			       unsigned int pd_id __unused,
			       unsigned int state __unused)
{
	return SCMI_NOT_SUPPORTED;
}

static void report_version(struct scmi_msg *msg)
{
	struct scmi_protocol_version_p2a return_values = {
		.status = SCMI_SUCCESS,
		.version = SCMI_PROTOCOL_VERSION_PD,
	};

	if (msg->in_size != 0) {
		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
		return;
	}

	scmi_write_response(msg, &return_values, sizeof(return_values));
}

static void report_attributes(struct scmi_msg *msg)
{
	unsigned long addr = 0UL;
	unsigned int len;

	struct scmi_protocol_attributes_p2a_pd return_values = {
		.status = SCMI_SUCCESS,
	};

	if (msg->in_size != 0) {
		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
		return;
	}

	return_values.attributes = plat_scmi_pd_count(msg->agent_id);
	len = plat_scmi_pd_statistics(msg->agent_id, &addr);
	if (len != 0U) {
		return_values.statistics_addr_low = (unsigned int)addr;
		return_values.statistics_addr_high = (uint32_t)(addr >> 32);
		return_values.statistics_len = len;
	}

	scmi_write_response(msg, &return_values, sizeof(return_values));
}

static void report_message_attributes(struct scmi_msg *msg)
{
	struct scmi_protocol_message_attributes_a2p *in_args = (void *)msg->in;
	struct scmi_protocol_message_attributes_p2a return_values = {
		.status = SCMI_SUCCESS,
		/* For this protocol, attributes shall be zero */
		.attributes = 0U,
	};

	if (msg->in_size != sizeof(*in_args)) {
		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
		return;
	}

	if (!message_id_is_supported(in_args->message_id)) {
		scmi_status_response(msg, SCMI_NOT_FOUND);
		return;
	}

	scmi_write_response(msg, &return_values, sizeof(return_values));
}

static void scmi_pd_attributes(struct scmi_msg *msg)
{
	const struct scmi_pd_attributes_a2p *in_args = (void *)msg->in;
	struct scmi_pd_attributes_p2a return_values = {
		.status = SCMI_SUCCESS,
	};
	const char *name = NULL;
	unsigned int pd_id = 0U;

	if (msg->in_size != sizeof(*in_args)) {
		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
		return;
	}

	pd_id = SPECULATION_SAFE_VALUE(in_args->pd_id);

	if (pd_id >= plat_scmi_pd_count(msg->agent_id)) {
		scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
		return;
	}

	name = plat_scmi_pd_get_name(msg->agent_id, pd_id);
	if (name == NULL) {
		scmi_status_response(msg, SCMI_NOT_FOUND);
		return;
	}

	COPY_NAME_IDENTIFIER(return_values.pd_name, name);

	return_values.attributes = plat_scmi_pd_get_attributes(msg->agent_id, pd_id);

	scmi_write_response(msg, &return_values, sizeof(return_values));
}

static void scmi_pd_state_get(struct scmi_msg *msg)
{
	const struct scmi_pd_state_get_a2p *in_args = (void *)msg->in;
	unsigned int state = 0U;
	struct scmi_pd_state_get_p2a return_values = {
		.status = SCMI_SUCCESS,
	};
	unsigned int pd_id = 0U;

	if (msg->in_size != sizeof(*in_args)) {
		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
		return;
	}

	pd_id = SPECULATION_SAFE_VALUE(in_args->pd_id);

	if (pd_id >= plat_scmi_pd_count(msg->agent_id)) {
		scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
		return;
	}

	state = plat_scmi_pd_get_state(msg->agent_id, pd_id);

	return_values.power_state = state;

	scmi_write_response(msg, &return_values, sizeof(return_values));
}

static void scmi_pd_state_set(struct scmi_msg *msg)
{
	const struct scmi_pd_state_set_a2p *in_args = (void *)msg->in;
	unsigned int flags = 0U;
	int32_t status = 0;
	unsigned int pd_id = 0U;
	unsigned int state = 0U;

	if (msg->in_size != sizeof(*in_args)) {
		scmi_status_response(msg, SCMI_PROTOCOL_ERROR);
		return;
	}

	pd_id = SPECULATION_SAFE_VALUE(in_args->pd_id);

	if (pd_id >= plat_scmi_pd_count(msg->agent_id)) {
		scmi_status_response(msg, SCMI_INVALID_PARAMETERS);
		return;
	}

	flags = SPECULATION_SAFE_VALUE(in_args->flags);
	state = SPECULATION_SAFE_VALUE(in_args->power_state);

	status = plat_scmi_pd_set_state(msg->agent_id, flags, pd_id, state);

	scmi_status_response(msg, status);
}

static const scmi_msg_handler_t scmi_pd_handler_table[] = {
	[SCMI_PROTOCOL_VERSION] = report_version,
	[SCMI_PROTOCOL_ATTRIBUTES] = report_attributes,
	[SCMI_PROTOCOL_MESSAGE_ATTRIBUTES] = report_message_attributes,
	[SCMI_PD_ATTRIBUTES] = scmi_pd_attributes,
	[SCMI_PD_STATE_SET] = scmi_pd_state_set,
	[SCMI_PD_STATE_GET] = scmi_pd_state_get,
};

static bool message_id_is_supported(size_t message_id)
{
	return (message_id < ARRAY_SIZE(scmi_pd_handler_table)) &&
	       (scmi_pd_handler_table[message_id] != NULL);
}

scmi_msg_handler_t scmi_msg_get_pd_handler(struct scmi_msg *msg)
{
	const size_t array_size = ARRAY_SIZE(scmi_pd_handler_table);
	unsigned int message_id = SPECULATION_SAFE_VALUE(msg->message_id);

	if (message_id >= array_size) {
		VERBOSE("pd handle not found %u", msg->message_id);
		return NULL;
	}

	return scmi_pd_handler_table[message_id];
}