blob: 8482d219c2b9be6b782eb40e3a2cb56a5b8f9201 [file] [log] [blame]
Soby Mathewea26bad2016-11-14 12:25:45 +00001/*
Samarth Parikh59cfa132017-11-23 14:23:21 +05302 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
Soby Mathewea26bad2016-11-14 12:25:45 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch_helpers.h>
8#include <assert.h>
9#include <debug.h>
10#include "scmi.h"
11#include "scmi_private.h"
12
13/*
14 * Private helper function to get exclusive access to SCMI channel.
15 */
16void scmi_get_channel(scmi_channel_t *ch)
17{
18 assert(ch->lock);
19 bakery_lock_get(ch->lock);
20
21 /* Make sure any previous command has finished */
22 assert(SCMI_IS_CHANNEL_FREE(
23 ((mailbox_mem_t *)(ch->info->scmi_mbx_mem))->status));
24}
25
26/*
27 * Private helper function to transfer ownership of channel from AP to SCP.
28 */
29void scmi_send_sync_command(scmi_channel_t *ch)
30{
31 mailbox_mem_t *mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
32
33 SCMI_MARK_CHANNEL_BUSY(mbx_mem->status);
34
35 /*
36 * Ensure that any write to the SCMI payload area is seen by SCP before
37 * we write to the doorbell register. If these 2 writes were reordered
38 * by the CPU then SCP would read stale payload data
39 */
40 dmbst();
41
Samarth Parikh59cfa132017-11-23 14:23:21 +053042 ch->info->ring_doorbell(ch->info);
Soby Mathewea26bad2016-11-14 12:25:45 +000043 /*
44 * Ensure that the write to the doorbell register is ordered prior to
45 * checking whether the channel is free.
46 */
47 dmbsy();
48
49 /* Wait for channel to be free */
50 while (!SCMI_IS_CHANNEL_FREE(mbx_mem->status))
51 ;
52
53 /*
54 * Ensure that any read to the SCMI payload area is done after reading
55 * mailbox status. If these 2 reads were reordered then the CPU would
56 * read invalid payload data
57 */
58 dmbld();
59}
60
61/*
62 * Private helper function to release exclusive access to SCMI channel.
63 */
64void scmi_put_channel(scmi_channel_t *ch)
65{
66 /* Make sure any previous command has finished */
67 assert(SCMI_IS_CHANNEL_FREE(
68 ((mailbox_mem_t *)(ch->info->scmi_mbx_mem))->status));
69
70 assert(ch->lock);
71 bakery_lock_release(ch->lock);
72}
73
74/*
75 * API to query the SCMI protocol version.
76 */
77int scmi_proto_version(void *p, uint32_t proto_id, uint32_t *version)
78{
79 mailbox_mem_t *mbx_mem;
80 int token = 0, ret;
81 scmi_channel_t *ch = (scmi_channel_t *)p;
82
83 validate_scmi_channel(ch);
84
85 scmi_get_channel(ch);
86
87 mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
88 mbx_mem->msg_header = SCMI_MSG_CREATE(proto_id, SCMI_PROTO_VERSION_MSG,
89 token);
90 mbx_mem->len = SCMI_PROTO_VERSION_MSG_LEN;
91 mbx_mem->flags = SCMI_FLAG_RESP_POLL;
92
93 scmi_send_sync_command(ch);
94
95 /* Get the return values */
96 SCMI_PAYLOAD_RET_VAL2(mbx_mem->payload, ret, *version);
97 assert(mbx_mem->len == SCMI_PROTO_VERSION_RESP_LEN);
98 assert(token == SCMI_MSG_GET_TOKEN(mbx_mem->msg_header));
99
100 scmi_put_channel(ch);
101
102 return ret;
103}
104
105/*
106 * API to query the protocol message attributes for a SCMI protocol.
107 */
108int scmi_proto_msg_attr(void *p, uint32_t proto_id,
109 uint32_t command_id, uint32_t *attr)
110{
111 mailbox_mem_t *mbx_mem;
112 int token = 0, ret;
113 scmi_channel_t *ch = (scmi_channel_t *)p;
114
115 validate_scmi_channel(ch);
116
117 scmi_get_channel(ch);
118
119 mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
120 mbx_mem->msg_header = SCMI_MSG_CREATE(proto_id,
121 SCMI_PROTO_MSG_ATTR_MSG, token);
122 mbx_mem->len = SCMI_PROTO_MSG_ATTR_MSG_LEN;
123 mbx_mem->flags = SCMI_FLAG_RESP_POLL;
124 SCMI_PAYLOAD_ARG1(mbx_mem->payload, command_id);
125
126 scmi_send_sync_command(ch);
127
128 /* Get the return values */
129 SCMI_PAYLOAD_RET_VAL2(mbx_mem->payload, ret, *attr);
130 assert(mbx_mem->len == SCMI_PROTO_MSG_ATTR_RESP_LEN);
131 assert(token == SCMI_MSG_GET_TOKEN(mbx_mem->msg_header));
132
133 scmi_put_channel(ch);
134
135 return ret;
136}
137
138/*
139 * SCMI Driver initialization API. Returns initialized channel on success
140 * or NULL on error. The return type is an opaque void pointer.
141 */
142void *scmi_init(scmi_channel_t *ch)
143{
144 uint32_t version;
145 int ret;
146
147 assert(ch && ch->info);
148 assert(ch->info->db_reg_addr);
149 assert(ch->info->db_modify_mask);
150 assert(ch->info->db_preserve_mask);
Samarth Parikh59cfa132017-11-23 14:23:21 +0530151 assert(ch->info->ring_doorbell != NULL);
Soby Mathewea26bad2016-11-14 12:25:45 +0000152
153 assert(ch->lock);
154
155 bakery_lock_init(ch->lock);
156
157 ch->is_initialized = 1;
158
159 ret = scmi_proto_version(ch, SCMI_PWR_DMN_PROTO_ID, &version);
160 if (ret != SCMI_E_SUCCESS) {
161 WARN("SCMI power domain protocol version message failed");
162 goto error;
163 }
164
165 if (!is_scmi_version_compatible(SCMI_PWR_DMN_PROTO_VER, version)) {
166 WARN("SCMI power domain protocol version 0x%x incompatible with driver version 0x%x",
167 version, SCMI_PWR_DMN_PROTO_VER);
168 goto error;
169 }
170
171 VERBOSE("SCMI power domain protocol version 0x%x detected\n", version);
172
173 ret = scmi_proto_version(ch, SCMI_SYS_PWR_PROTO_ID, &version);
174 if ((ret != SCMI_E_SUCCESS)) {
175 WARN("SCMI system power protocol version message failed");
176 goto error;
177 }
178
179 if (!is_scmi_version_compatible(SCMI_SYS_PWR_PROTO_VER, version)) {
180 WARN("SCMI system power management protocol version 0x%x incompatible with driver version 0x%x",
181 version, SCMI_SYS_PWR_PROTO_VER);
182 goto error;
183 }
184
185 VERBOSE("SCMI system power management protocol version 0x%x detected\n",
186 version);
187
188 INFO("SCMI driver initialized\n");
189
190 return (void *)ch;
191
192error:
193 ch->is_initialized = 0;
194 return NULL;
195}