Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2020, Linaro Limited |
| 4 | */ |
| 5 | |
Patrick Delaunay | 5dd84d4 | 2021-02-24 11:19:44 +0100 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_SCMI_AGENT |
| 7 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <malloc.h> |
| 11 | #include <scmi_agent.h> |
| 12 | #include <scmi_agent-uclass.h> |
| 13 | #include <scmi_protocols.h> |
| 14 | #include <asm/io.h> |
| 15 | #include <asm/scmi_test.h> |
| 16 | #include <dm/device_compat.h> |
| 17 | |
| 18 | /* |
| 19 | * The sandbox SCMI agent driver simulates to some extend a SCMI message |
| 20 | * processing. It simulates few of the SCMI services for some of the |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 21 | * SCMI protocols embedded in U-Boot. Currently: |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 22 | * - SCMI clock protocol emulates an agent exposing 2 clocks |
| 23 | * - SCMI reset protocol emulates an agent exposing a reset controller |
| 24 | * - SCMI voltage domain protocol emulates an agent exposing 2 regulators |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 25 | * |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 26 | * As per DT bindings, the device node name shall be scmi. |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 27 | * |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 28 | * All clocks and regulators are default disabled and reset controller down. |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 29 | * |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 30 | * This driver exports sandbox_scmi_service_ctx() for the test sequence to |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 31 | * get the state of the simulated services (clock state, rate, ...) and |
| 32 | * check back-end device state reflects the request send through the |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 33 | * various uclass devices, as clocks and reset controllers. |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 34 | */ |
| 35 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 36 | static struct sandbox_scmi_clk scmi_clk[] = { |
Etienne Carriere | bf1f132 | 2022-02-21 09:22:41 +0100 | [diff] [blame] | 37 | { .rate = 333 }, |
| 38 | { .rate = 200 }, |
| 39 | { .rate = 1000 }, |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 40 | }; |
| 41 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 42 | static struct sandbox_scmi_reset scmi_reset[] = { |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 43 | { .id = 3 }, |
| 44 | }; |
| 45 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 46 | static struct sandbox_scmi_voltd scmi_voltd[] = { |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 47 | { .id = 0, .voltage_uv = 3300000 }, |
| 48 | { .id = 1, .voltage_uv = 1800000 }, |
| 49 | }; |
| 50 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 51 | static struct sandbox_scmi_service sandbox_scmi_service_state; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 52 | |
| 53 | struct sandbox_scmi_service *sandbox_scmi_service_ctx(void) |
| 54 | { |
| 55 | return &sandbox_scmi_service_state; |
| 56 | } |
| 57 | |
| 58 | static void debug_print_agent_state(struct udevice *dev, char *str) |
| 59 | { |
| 60 | struct sandbox_scmi_agent *agent = dev_get_priv(dev); |
| 61 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 62 | dev_dbg(dev, "Dump sandbox_scmi_agent: %s\n", str); |
| 63 | dev_dbg(dev, " scmi_clk (%zu): %d/%ld, %d/%ld, %d/%ld, ...\n", |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 64 | agent->clk_count, |
| 65 | agent->clk_count ? agent->clk[0].enabled : -1, |
| 66 | agent->clk_count ? agent->clk[0].rate : -1, |
| 67 | agent->clk_count > 1 ? agent->clk[1].enabled : -1, |
| 68 | agent->clk_count > 1 ? agent->clk[1].rate : -1, |
| 69 | agent->clk_count > 2 ? agent->clk[2].enabled : -1, |
| 70 | agent->clk_count > 2 ? agent->clk[2].rate : -1); |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 71 | dev_dbg(dev, " scmi_reset (%zu): %d, %d, ...\n", |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 72 | agent->reset_count, |
| 73 | agent->reset_count ? agent->reset[0].asserted : -1, |
| 74 | agent->reset_count > 1 ? agent->reset[1].asserted : -1); |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 75 | dev_dbg(dev, " scmi_voltd (%zu): %u/%d, %u/%d, ...\n", |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 76 | agent->voltd_count, |
| 77 | agent->voltd_count ? agent->voltd[0].enabled : -1, |
| 78 | agent->voltd_count ? agent->voltd[0].voltage_uv : -1, |
| 79 | agent->voltd_count ? agent->voltd[1].enabled : -1, |
| 80 | agent->voltd_count ? agent->voltd[1].voltage_uv : -1); |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 83 | static struct sandbox_scmi_clk *get_scmi_clk_state(uint clock_id) |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 84 | { |
Etienne Carriere | bf1f132 | 2022-02-21 09:22:41 +0100 | [diff] [blame] | 85 | if (clock_id < ARRAY_SIZE(scmi_clk)) |
| 86 | return scmi_clk + clock_id; |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 87 | |
| 88 | return NULL; |
| 89 | } |
| 90 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 91 | static struct sandbox_scmi_reset *get_scmi_reset_state(uint reset_id) |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 92 | { |
| 93 | size_t n; |
| 94 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 95 | for (n = 0; n < ARRAY_SIZE(scmi_reset); n++) |
| 96 | if (scmi_reset[n].id == reset_id) |
| 97 | return scmi_reset + n; |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 98 | |
| 99 | return NULL; |
| 100 | } |
| 101 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 102 | static struct sandbox_scmi_voltd *get_scmi_voltd_state(uint domain_id) |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 103 | { |
| 104 | size_t n; |
| 105 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 106 | for (n = 0; n < ARRAY_SIZE(scmi_voltd); n++) |
| 107 | if (scmi_voltd[n].id == domain_id) |
| 108 | return scmi_voltd + n; |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 109 | |
| 110 | return NULL; |
| 111 | } |
| 112 | |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 113 | /* |
| 114 | * Sandbox SCMI agent ops |
| 115 | */ |
| 116 | |
Etienne Carriere | 4c4ec90 | 2022-02-21 09:22:42 +0100 | [diff] [blame] | 117 | static int sandbox_scmi_clock_protocol_attribs(struct udevice *dev, |
| 118 | struct scmi_msg *msg) |
| 119 | { |
| 120 | struct scmi_clk_protocol_attr_out *out = NULL; |
| 121 | |
| 122 | if (!msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 123 | return -EINVAL; |
| 124 | |
| 125 | out = (struct scmi_clk_protocol_attr_out *)msg->out_msg; |
| 126 | out->attributes = ARRAY_SIZE(scmi_clk); |
| 127 | out->status = SCMI_SUCCESS; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | static int sandbox_scmi_clock_attribs(struct udevice *dev, struct scmi_msg *msg) |
| 133 | { |
| 134 | struct scmi_clk_attribute_in *in = NULL; |
| 135 | struct scmi_clk_attribute_out *out = NULL; |
| 136 | struct sandbox_scmi_clk *clk_state = NULL; |
| 137 | int ret; |
| 138 | |
| 139 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 140 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | in = (struct scmi_clk_attribute_in *)msg->in_msg; |
| 144 | out = (struct scmi_clk_attribute_out *)msg->out_msg; |
| 145 | |
| 146 | clk_state = get_scmi_clk_state(in->clock_id); |
| 147 | if (!clk_state) { |
| 148 | dev_err(dev, "Unexpected clock ID %u\n", in->clock_id); |
| 149 | |
| 150 | out->status = SCMI_NOT_FOUND; |
| 151 | } else { |
| 152 | memset(out, 0, sizeof(*out)); |
| 153 | |
| 154 | if (clk_state->enabled) |
| 155 | out->attributes = 1; |
| 156 | |
| 157 | ret = snprintf(out->clock_name, sizeof(out->clock_name), |
| 158 | "clk%u", in->clock_id); |
| 159 | assert(ret > 0 && ret < sizeof(out->clock_name)); |
| 160 | |
| 161 | out->status = SCMI_SUCCESS; |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 166 | static int sandbox_scmi_clock_rate_set(struct udevice *dev, |
| 167 | struct scmi_msg *msg) |
| 168 | { |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 169 | struct scmi_clk_rate_set_in *in = NULL; |
| 170 | struct scmi_clk_rate_set_out *out = NULL; |
| 171 | struct sandbox_scmi_clk *clk_state = NULL; |
| 172 | |
| 173 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 174 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 175 | return -EINVAL; |
| 176 | |
| 177 | in = (struct scmi_clk_rate_set_in *)msg->in_msg; |
| 178 | out = (struct scmi_clk_rate_set_out *)msg->out_msg; |
| 179 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 180 | clk_state = get_scmi_clk_state(in->clock_id); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 181 | if (!clk_state) { |
| 182 | dev_err(dev, "Unexpected clock ID %u\n", in->clock_id); |
| 183 | |
| 184 | out->status = SCMI_NOT_FOUND; |
| 185 | } else { |
| 186 | u64 rate = ((u64)in->rate_msb << 32) + in->rate_lsb; |
| 187 | |
| 188 | clk_state->rate = (ulong)rate; |
| 189 | |
| 190 | out->status = SCMI_SUCCESS; |
| 191 | } |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | static int sandbox_scmi_clock_rate_get(struct udevice *dev, |
| 197 | struct scmi_msg *msg) |
| 198 | { |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 199 | struct scmi_clk_rate_get_in *in = NULL; |
| 200 | struct scmi_clk_rate_get_out *out = NULL; |
| 201 | struct sandbox_scmi_clk *clk_state = NULL; |
| 202 | |
| 203 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 204 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 205 | return -EINVAL; |
| 206 | |
| 207 | in = (struct scmi_clk_rate_get_in *)msg->in_msg; |
| 208 | out = (struct scmi_clk_rate_get_out *)msg->out_msg; |
| 209 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 210 | clk_state = get_scmi_clk_state(in->clock_id); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 211 | if (!clk_state) { |
| 212 | dev_err(dev, "Unexpected clock ID %u\n", in->clock_id); |
| 213 | |
| 214 | out->status = SCMI_NOT_FOUND; |
| 215 | } else { |
| 216 | out->rate_msb = (u32)((u64)clk_state->rate >> 32); |
| 217 | out->rate_lsb = (u32)clk_state->rate; |
| 218 | |
| 219 | out->status = SCMI_SUCCESS; |
| 220 | } |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | static int sandbox_scmi_clock_gate(struct udevice *dev, struct scmi_msg *msg) |
| 226 | { |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 227 | struct scmi_clk_state_in *in = NULL; |
| 228 | struct scmi_clk_state_out *out = NULL; |
| 229 | struct sandbox_scmi_clk *clk_state = NULL; |
| 230 | |
| 231 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 232 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 233 | return -EINVAL; |
| 234 | |
| 235 | in = (struct scmi_clk_state_in *)msg->in_msg; |
| 236 | out = (struct scmi_clk_state_out *)msg->out_msg; |
| 237 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 238 | clk_state = get_scmi_clk_state(in->clock_id); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 239 | if (!clk_state) { |
| 240 | dev_err(dev, "Unexpected clock ID %u\n", in->clock_id); |
| 241 | |
| 242 | out->status = SCMI_NOT_FOUND; |
| 243 | } else if (in->attributes > 1) { |
| 244 | out->status = SCMI_PROTOCOL_ERROR; |
| 245 | } else { |
| 246 | clk_state->enabled = in->attributes; |
| 247 | |
| 248 | out->status = SCMI_SUCCESS; |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 254 | static int sandbox_scmi_rd_attribs(struct udevice *dev, struct scmi_msg *msg) |
| 255 | { |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 256 | struct scmi_rd_attr_in *in = NULL; |
| 257 | struct scmi_rd_attr_out *out = NULL; |
| 258 | struct sandbox_scmi_reset *reset_state = NULL; |
| 259 | |
| 260 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 261 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 262 | return -EINVAL; |
| 263 | |
| 264 | in = (struct scmi_rd_attr_in *)msg->in_msg; |
| 265 | out = (struct scmi_rd_attr_out *)msg->out_msg; |
| 266 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 267 | reset_state = get_scmi_reset_state(in->domain_id); |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 268 | if (!reset_state) { |
| 269 | dev_err(dev, "Unexpected reset domain ID %u\n", in->domain_id); |
| 270 | |
| 271 | out->status = SCMI_NOT_FOUND; |
| 272 | } else { |
| 273 | memset(out, 0, sizeof(*out)); |
| 274 | snprintf(out->name, sizeof(out->name), "rd%u", in->domain_id); |
| 275 | |
| 276 | out->status = SCMI_SUCCESS; |
| 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int sandbox_scmi_rd_reset(struct udevice *dev, struct scmi_msg *msg) |
| 283 | { |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 284 | struct scmi_rd_reset_in *in = NULL; |
| 285 | struct scmi_rd_reset_out *out = NULL; |
| 286 | struct sandbox_scmi_reset *reset_state = NULL; |
| 287 | |
| 288 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 289 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | in = (struct scmi_rd_reset_in *)msg->in_msg; |
| 293 | out = (struct scmi_rd_reset_out *)msg->out_msg; |
| 294 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 295 | reset_state = get_scmi_reset_state(in->domain_id); |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 296 | if (!reset_state) { |
| 297 | dev_err(dev, "Unexpected reset domain ID %u\n", in->domain_id); |
| 298 | |
| 299 | out->status = SCMI_NOT_FOUND; |
| 300 | } else if (in->reset_state > 1) { |
| 301 | dev_err(dev, "Invalid reset domain input attribute value\n"); |
| 302 | |
| 303 | out->status = SCMI_INVALID_PARAMETERS; |
| 304 | } else { |
| 305 | if (in->flags & SCMI_RD_RESET_FLAG_CYCLE) { |
| 306 | if (in->flags & SCMI_RD_RESET_FLAG_ASYNC) { |
| 307 | out->status = SCMI_NOT_SUPPORTED; |
| 308 | } else { |
| 309 | /* Ends deasserted whatever current state */ |
| 310 | reset_state->asserted = false; |
| 311 | out->status = SCMI_SUCCESS; |
| 312 | } |
| 313 | } else { |
| 314 | reset_state->asserted = in->flags & |
| 315 | SCMI_RD_RESET_FLAG_ASSERT; |
| 316 | |
| 317 | out->status = SCMI_SUCCESS; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 324 | static int sandbox_scmi_voltd_attribs(struct udevice *dev, struct scmi_msg *msg) |
| 325 | { |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 326 | struct scmi_voltd_attr_in *in = NULL; |
| 327 | struct scmi_voltd_attr_out *out = NULL; |
| 328 | struct sandbox_scmi_voltd *voltd_state = NULL; |
| 329 | |
| 330 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 331 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 332 | return -EINVAL; |
| 333 | |
| 334 | in = (struct scmi_voltd_attr_in *)msg->in_msg; |
| 335 | out = (struct scmi_voltd_attr_out *)msg->out_msg; |
| 336 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 337 | voltd_state = get_scmi_voltd_state(in->domain_id); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 338 | if (!voltd_state) { |
| 339 | dev_err(dev, "Unexpected domain ID %u\n", in->domain_id); |
| 340 | |
| 341 | out->status = SCMI_NOT_FOUND; |
| 342 | } else { |
| 343 | memset(out, 0, sizeof(*out)); |
| 344 | snprintf(out->name, sizeof(out->name), "regu%u", in->domain_id); |
| 345 | |
| 346 | out->status = SCMI_SUCCESS; |
| 347 | } |
| 348 | |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | static int sandbox_scmi_voltd_config_set(struct udevice *dev, |
| 353 | struct scmi_msg *msg) |
| 354 | { |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 355 | struct scmi_voltd_config_set_in *in = NULL; |
| 356 | struct scmi_voltd_config_set_out *out = NULL; |
| 357 | struct sandbox_scmi_voltd *voltd_state = NULL; |
| 358 | |
| 359 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 360 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 361 | return -EINVAL; |
| 362 | |
| 363 | in = (struct scmi_voltd_config_set_in *)msg->in_msg; |
| 364 | out = (struct scmi_voltd_config_set_out *)msg->out_msg; |
| 365 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 366 | voltd_state = get_scmi_voltd_state(in->domain_id); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 367 | if (!voltd_state) { |
| 368 | dev_err(dev, "Unexpected domain ID %u\n", in->domain_id); |
| 369 | |
| 370 | out->status = SCMI_NOT_FOUND; |
| 371 | } else if (in->config & ~SCMI_VOLTD_CONFIG_MASK) { |
| 372 | dev_err(dev, "Invalid config value 0x%x\n", in->config); |
| 373 | |
| 374 | out->status = SCMI_INVALID_PARAMETERS; |
| 375 | } else if (in->config != SCMI_VOLTD_CONFIG_ON && |
| 376 | in->config != SCMI_VOLTD_CONFIG_OFF) { |
| 377 | dev_err(dev, "Unexpected custom value 0x%x\n", in->config); |
| 378 | |
| 379 | out->status = SCMI_INVALID_PARAMETERS; |
| 380 | } else { |
| 381 | voltd_state->enabled = in->config == SCMI_VOLTD_CONFIG_ON; |
| 382 | out->status = SCMI_SUCCESS; |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | static int sandbox_scmi_voltd_config_get(struct udevice *dev, |
| 389 | struct scmi_msg *msg) |
| 390 | { |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 391 | struct scmi_voltd_config_get_in *in = NULL; |
| 392 | struct scmi_voltd_config_get_out *out = NULL; |
| 393 | struct sandbox_scmi_voltd *voltd_state = NULL; |
| 394 | |
| 395 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 396 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 397 | return -EINVAL; |
| 398 | |
| 399 | in = (struct scmi_voltd_config_get_in *)msg->in_msg; |
| 400 | out = (struct scmi_voltd_config_get_out *)msg->out_msg; |
| 401 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 402 | voltd_state = get_scmi_voltd_state(in->domain_id); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 403 | if (!voltd_state) { |
| 404 | dev_err(dev, "Unexpected domain ID %u\n", in->domain_id); |
| 405 | |
| 406 | out->status = SCMI_NOT_FOUND; |
| 407 | } else { |
| 408 | if (voltd_state->enabled) |
| 409 | out->config = SCMI_VOLTD_CONFIG_ON; |
| 410 | else |
| 411 | out->config = SCMI_VOLTD_CONFIG_OFF; |
| 412 | |
| 413 | out->status = SCMI_SUCCESS; |
| 414 | } |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | static int sandbox_scmi_voltd_level_set(struct udevice *dev, |
| 420 | struct scmi_msg *msg) |
| 421 | { |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 422 | struct scmi_voltd_level_set_in *in = NULL; |
| 423 | struct scmi_voltd_level_set_out *out = NULL; |
| 424 | struct sandbox_scmi_voltd *voltd_state = NULL; |
| 425 | |
| 426 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 427 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 428 | return -EINVAL; |
| 429 | |
| 430 | in = (struct scmi_voltd_level_set_in *)msg->in_msg; |
| 431 | out = (struct scmi_voltd_level_set_out *)msg->out_msg; |
| 432 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 433 | voltd_state = get_scmi_voltd_state(in->domain_id); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 434 | if (!voltd_state) { |
| 435 | dev_err(dev, "Unexpected domain ID %u\n", in->domain_id); |
| 436 | |
| 437 | out->status = SCMI_NOT_FOUND; |
| 438 | } else { |
| 439 | voltd_state->voltage_uv = in->voltage_level; |
| 440 | out->status = SCMI_SUCCESS; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static int sandbox_scmi_voltd_level_get(struct udevice *dev, |
| 447 | struct scmi_msg *msg) |
| 448 | { |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 449 | struct scmi_voltd_level_get_in *in = NULL; |
| 450 | struct scmi_voltd_level_get_out *out = NULL; |
| 451 | struct sandbox_scmi_voltd *voltd_state = NULL; |
| 452 | |
| 453 | if (!msg->in_msg || msg->in_msg_sz < sizeof(*in) || |
| 454 | !msg->out_msg || msg->out_msg_sz < sizeof(*out)) |
| 455 | return -EINVAL; |
| 456 | |
| 457 | in = (struct scmi_voltd_level_get_in *)msg->in_msg; |
| 458 | out = (struct scmi_voltd_level_get_out *)msg->out_msg; |
| 459 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 460 | voltd_state = get_scmi_voltd_state(in->domain_id); |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 461 | if (!voltd_state) { |
| 462 | dev_err(dev, "Unexpected domain ID %u\n", in->domain_id); |
| 463 | |
| 464 | out->status = SCMI_NOT_FOUND; |
| 465 | } else { |
| 466 | out->voltage_level = voltd_state->voltage_uv; |
| 467 | out->status = SCMI_SUCCESS; |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 473 | static int sandbox_scmi_test_process_msg(struct udevice *dev, |
| 474 | struct scmi_msg *msg) |
| 475 | { |
| 476 | switch (msg->protocol_id) { |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 477 | case SCMI_PROTOCOL_ID_CLOCK: |
| 478 | switch (msg->message_id) { |
Etienne Carriere | 4c4ec90 | 2022-02-21 09:22:42 +0100 | [diff] [blame] | 479 | case SCMI_PROTOCOL_ATTRIBUTES: |
| 480 | return sandbox_scmi_clock_protocol_attribs(dev, msg); |
| 481 | case SCMI_CLOCK_ATTRIBUTES: |
| 482 | return sandbox_scmi_clock_attribs(dev, msg); |
Etienne Carriere | 2d94c08fa | 2020-09-09 18:44:05 +0200 | [diff] [blame] | 483 | case SCMI_CLOCK_RATE_SET: |
| 484 | return sandbox_scmi_clock_rate_set(dev, msg); |
| 485 | case SCMI_CLOCK_RATE_GET: |
| 486 | return sandbox_scmi_clock_rate_get(dev, msg); |
| 487 | case SCMI_CLOCK_CONFIG_SET: |
| 488 | return sandbox_scmi_clock_gate(dev, msg); |
| 489 | default: |
| 490 | break; |
| 491 | } |
| 492 | break; |
Etienne Carriere | 8b9b689 | 2020-09-09 18:44:07 +0200 | [diff] [blame] | 493 | case SCMI_PROTOCOL_ID_RESET_DOMAIN: |
| 494 | switch (msg->message_id) { |
| 495 | case SCMI_RESET_DOMAIN_ATTRIBUTES: |
| 496 | return sandbox_scmi_rd_attribs(dev, msg); |
| 497 | case SCMI_RESET_DOMAIN_RESET: |
| 498 | return sandbox_scmi_rd_reset(dev, msg); |
| 499 | default: |
| 500 | break; |
| 501 | } |
| 502 | break; |
Etienne Carriere | b8f15cd | 2021-03-08 22:38:07 +0100 | [diff] [blame] | 503 | case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN: |
| 504 | switch (msg->message_id) { |
| 505 | case SCMI_VOLTAGE_DOMAIN_ATTRIBUTES: |
| 506 | return sandbox_scmi_voltd_attribs(dev, msg); |
| 507 | case SCMI_VOLTAGE_DOMAIN_CONFIG_SET: |
| 508 | return sandbox_scmi_voltd_config_set(dev, msg); |
| 509 | case SCMI_VOLTAGE_DOMAIN_CONFIG_GET: |
| 510 | return sandbox_scmi_voltd_config_get(dev, msg); |
| 511 | case SCMI_VOLTAGE_DOMAIN_LEVEL_SET: |
| 512 | return sandbox_scmi_voltd_level_set(dev, msg); |
| 513 | case SCMI_VOLTAGE_DOMAIN_LEVEL_GET: |
| 514 | return sandbox_scmi_voltd_level_get(dev, msg); |
| 515 | default: |
| 516 | break; |
| 517 | } |
| 518 | break; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 519 | case SCMI_PROTOCOL_ID_BASE: |
| 520 | case SCMI_PROTOCOL_ID_POWER_DOMAIN: |
| 521 | case SCMI_PROTOCOL_ID_SYSTEM: |
| 522 | case SCMI_PROTOCOL_ID_PERF: |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 523 | case SCMI_PROTOCOL_ID_SENSOR: |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 524 | *(u32 *)msg->out_msg = SCMI_NOT_SUPPORTED; |
| 525 | return 0; |
| 526 | default: |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | dev_err(dev, "%s(%s): Unhandled protocol_id %#x/message_id %#x\n", |
| 531 | __func__, dev->name, msg->protocol_id, msg->message_id); |
| 532 | |
| 533 | if (msg->out_msg_sz < sizeof(u32)) |
| 534 | return -EINVAL; |
| 535 | |
| 536 | /* Intentionnaly report unhandled IDs through the SCMI return code */ |
| 537 | *(u32 *)msg->out_msg = SCMI_PROTOCOL_ERROR; |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static int sandbox_scmi_test_remove(struct udevice *dev) |
| 542 | { |
| 543 | struct sandbox_scmi_agent *agent = dev_get_priv(dev); |
| 544 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 545 | if (agent != sandbox_scmi_service_ctx()->agent) |
| 546 | return -EINVAL; |
| 547 | |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 548 | debug_print_agent_state(dev, "removed"); |
| 549 | |
| 550 | /* We only need to dereference the agent in the context */ |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 551 | sandbox_scmi_service_ctx()->agent = NULL; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | static int sandbox_scmi_test_probe(struct udevice *dev) |
| 557 | { |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 558 | struct sandbox_scmi_agent *agent = dev_get_priv(dev); |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 559 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 560 | if (sandbox_scmi_service_ctx()->agent) |
| 561 | return -EINVAL; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 562 | |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 563 | *agent = (struct sandbox_scmi_agent){ |
| 564 | .clk = scmi_clk, |
| 565 | .clk_count = ARRAY_SIZE(scmi_clk), |
| 566 | .reset = scmi_reset, |
| 567 | .reset_count = ARRAY_SIZE(scmi_reset), |
| 568 | .voltd = scmi_voltd, |
| 569 | .voltd_count = ARRAY_SIZE(scmi_voltd), |
| 570 | }; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 571 | |
| 572 | debug_print_agent_state(dev, "probed"); |
| 573 | |
| 574 | /* Save reference for tests purpose */ |
Etienne Carriere | 09665cb | 2022-02-21 09:22:39 +0100 | [diff] [blame] | 575 | sandbox_scmi_service_ctx()->agent = agent; |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 576 | |
| 577 | return 0; |
| 578 | }; |
| 579 | |
| 580 | static const struct udevice_id sandbox_scmi_test_ids[] = { |
| 581 | { .compatible = "sandbox,scmi-agent" }, |
| 582 | { } |
| 583 | }; |
| 584 | |
| 585 | struct scmi_agent_ops sandbox_scmi_test_ops = { |
| 586 | .process_msg = sandbox_scmi_test_process_msg, |
| 587 | }; |
| 588 | |
| 589 | U_BOOT_DRIVER(sandbox_scmi_agent) = { |
| 590 | .name = "sandbox-scmi_agent", |
| 591 | .id = UCLASS_SCMI_AGENT, |
| 592 | .of_match = sandbox_scmi_test_ids, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 593 | .priv_auto = sizeof(struct sandbox_scmi_agent), |
Etienne Carriere | 02fd126 | 2020-09-09 18:44:00 +0200 | [diff] [blame] | 594 | .probe = sandbox_scmi_test_probe, |
| 595 | .remove = sandbox_scmi_test_remove, |
| 596 | .ops = &sandbox_scmi_test_ops, |
| 597 | }; |