xref: /optee_os/core/drivers/scmi-msg/entry.c (revision 5c34a982f7e3cc1b802c20a166d892b8f5d35f18)
1 // SPDX-License-Identifier: BSD-3-Clause
2 /*
3  * Copyright (c) 2015-2019, Arm Limited and Contributors. All rights reserved.
4  * Copyright (c) 2019-2022, Linaro Limited
5  */
6 #include <assert.h>
7 #include <drivers/scmi-msg.h>
8 #include <drivers/scmi.h>
9 #include <kernel/spinlock.h>
10 #include <string.h>
11 #include <trace.h>
12 
13 #include "base.h"
14 #include "clock.h"
15 #include "common.h"
16 #include "reset_domain.h"
17 #include "voltage_domain.h"
18 
19 /* SMP protection on channel->busy field */
20 static unsigned int smt_channels_lock;
21 
22 /* If channel is not busy, set busy and return true, otherwise return false */
23 bool scmi_msg_claim_channel(struct scmi_msg_channel *channel)
24 {
25 	uint32_t exceptions = cpu_spin_lock_xsave(&smt_channels_lock);
26 	bool channel_is_busy = channel->busy;
27 
28 	if (!channel_is_busy)
29 		channel->busy = true;
30 
31 	cpu_spin_unlock_xrestore(&smt_channels_lock, exceptions);
32 
33 	return !channel_is_busy;
34 }
35 
36 void scmi_msg_release_channel(struct scmi_msg_channel *channel)
37 {
38 	channel->busy = false;
39 }
40 
41 void scmi_status_response(struct scmi_msg *msg, int32_t status)
42 {
43 	assert(msg->out && msg->out_size >= sizeof(int32_t));
44 
45 	memcpy(msg->out, &status, sizeof(int32_t));
46 	msg->out_size_out = sizeof(int32_t);
47 }
48 
49 void scmi_write_response(struct scmi_msg *msg, void *payload, size_t size)
50 {
51 	/*
52 	 * Output payload shall be at least the size of the status
53 	 * Output buffer shall be at least be the size of the status
54 	 * Output paylaod shall fit in output buffer
55 	 */
56 	assert(payload && size >= sizeof(int32_t) && size <= msg->out_size &&
57 	       msg->out && msg->out_size >= sizeof(int32_t));
58 
59 	memcpy(msg->out, payload, size);
60 	msg->out_size_out = size;
61 }
62 
63 void scmi_process_message(struct scmi_msg *msg)
64 {
65 	scmi_msg_handler_t handler = NULL;
66 
67 	switch (msg->protocol_id) {
68 	case SCMI_PROTOCOL_ID_BASE:
69 		handler = scmi_msg_get_base_handler(msg);
70 		break;
71 	case SCMI_PROTOCOL_ID_CLOCK:
72 		handler = scmi_msg_get_clock_handler(msg);
73 		break;
74 	case SCMI_PROTOCOL_ID_RESET_DOMAIN:
75 		handler = scmi_msg_get_rd_handler(msg);
76 		break;
77 	case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
78 		handler = scmi_msg_get_voltd_handler(msg);
79 		break;
80 	default:
81 		break;
82 	}
83 
84 	if (handler) {
85 		handler(msg);
86 		return;
87 	}
88 
89 	DMSG("Channel %u Protocol %#x Message %#x: not supported",
90 	     msg->channel_id, msg->protocol_id, msg->message_id);
91 
92 	scmi_status_response(msg, SCMI_NOT_SUPPORTED);
93 }
94