blob: ec749fb56129062bfb9fb4226c5934d8eeef394e [file] [log] [blame]
Soby Mathewea26bad2016-11-14 12:25:45 +00001/*
Ambroise Vincenta88a35d2019-02-14 09:48:21 +00002 * Copyright (c) 2017-2019, ARM Limited and Contributors. All rights reserved.
Soby Mathewea26bad2016-11-14 12:25:45 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Soby Mathewea26bad2016-11-14 12:25:45 +00007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <arch_helpers.h>
10#include <common/debug.h>
Antonio Nino Diazc30db5b2019-01-23 20:37:32 +000011#include <drivers/arm/css/scmi.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
Soby Mathewea26bad2016-11-14 12:25:45 +000013#include "scmi_private.h"
14
Roberto Vargas00996942017-11-13 13:41:58 +000015#if HW_ASSISTED_COHERENCY
16#define scmi_lock_init(lock)
17#define scmi_lock_get(lock) spin_lock(lock)
18#define scmi_lock_release(lock) spin_unlock(lock)
19#else
20#define scmi_lock_init(lock) bakery_lock_init(lock)
21#define scmi_lock_get(lock) bakery_lock_get(lock)
22#define scmi_lock_release(lock) bakery_lock_release(lock)
23#endif
24
25
Soby Mathewea26bad2016-11-14 12:25:45 +000026/*
27 * Private helper function to get exclusive access to SCMI channel.
28 */
29void scmi_get_channel(scmi_channel_t *ch)
30{
31 assert(ch->lock);
Roberto Vargas00996942017-11-13 13:41:58 +000032 scmi_lock_get(ch->lock);
Soby Mathewea26bad2016-11-14 12:25:45 +000033
34 /* Make sure any previous command has finished */
35 assert(SCMI_IS_CHANNEL_FREE(
36 ((mailbox_mem_t *)(ch->info->scmi_mbx_mem))->status));
37}
38
39/*
40 * Private helper function to transfer ownership of channel from AP to SCP.
41 */
42void scmi_send_sync_command(scmi_channel_t *ch)
43{
44 mailbox_mem_t *mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
45
46 SCMI_MARK_CHANNEL_BUSY(mbx_mem->status);
47
48 /*
49 * Ensure that any write to the SCMI payload area is seen by SCP before
50 * we write to the doorbell register. If these 2 writes were reordered
51 * by the CPU then SCP would read stale payload data
52 */
53 dmbst();
54
Samarth Parikh59cfa132017-11-23 14:23:21 +053055 ch->info->ring_doorbell(ch->info);
Soby Mathewea26bad2016-11-14 12:25:45 +000056 /*
57 * Ensure that the write to the doorbell register is ordered prior to
58 * checking whether the channel is free.
59 */
60 dmbsy();
61
62 /* Wait for channel to be free */
63 while (!SCMI_IS_CHANNEL_FREE(mbx_mem->status))
64 ;
65
66 /*
67 * Ensure that any read to the SCMI payload area is done after reading
68 * mailbox status. If these 2 reads were reordered then the CPU would
69 * read invalid payload data
70 */
71 dmbld();
72}
73
74/*
75 * Private helper function to release exclusive access to SCMI channel.
76 */
77void scmi_put_channel(scmi_channel_t *ch)
78{
79 /* Make sure any previous command has finished */
80 assert(SCMI_IS_CHANNEL_FREE(
81 ((mailbox_mem_t *)(ch->info->scmi_mbx_mem))->status));
82
83 assert(ch->lock);
Roberto Vargas00996942017-11-13 13:41:58 +000084 scmi_lock_release(ch->lock);
Soby Mathewea26bad2016-11-14 12:25:45 +000085}
86
87/*
88 * API to query the SCMI protocol version.
89 */
90int scmi_proto_version(void *p, uint32_t proto_id, uint32_t *version)
91{
92 mailbox_mem_t *mbx_mem;
Ambroise Vincenta88a35d2019-02-14 09:48:21 +000093 unsigned int token = 0;
94 int ret;
Soby Mathewea26bad2016-11-14 12:25:45 +000095 scmi_channel_t *ch = (scmi_channel_t *)p;
96
97 validate_scmi_channel(ch);
98
99 scmi_get_channel(ch);
100
101 mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
102 mbx_mem->msg_header = SCMI_MSG_CREATE(proto_id, SCMI_PROTO_VERSION_MSG,
103 token);
104 mbx_mem->len = SCMI_PROTO_VERSION_MSG_LEN;
105 mbx_mem->flags = SCMI_FLAG_RESP_POLL;
106
107 scmi_send_sync_command(ch);
108
109 /* Get the return values */
110 SCMI_PAYLOAD_RET_VAL2(mbx_mem->payload, ret, *version);
111 assert(mbx_mem->len == SCMI_PROTO_VERSION_RESP_LEN);
112 assert(token == SCMI_MSG_GET_TOKEN(mbx_mem->msg_header));
113
114 scmi_put_channel(ch);
115
116 return ret;
117}
118
119/*
120 * API to query the protocol message attributes for a SCMI protocol.
121 */
122int scmi_proto_msg_attr(void *p, uint32_t proto_id,
123 uint32_t command_id, uint32_t *attr)
124{
125 mailbox_mem_t *mbx_mem;
Ambroise Vincenta88a35d2019-02-14 09:48:21 +0000126 unsigned int token = 0;
127 int ret;
Soby Mathewea26bad2016-11-14 12:25:45 +0000128 scmi_channel_t *ch = (scmi_channel_t *)p;
129
130 validate_scmi_channel(ch);
131
132 scmi_get_channel(ch);
133
134 mbx_mem = (mailbox_mem_t *)(ch->info->scmi_mbx_mem);
135 mbx_mem->msg_header = SCMI_MSG_CREATE(proto_id,
136 SCMI_PROTO_MSG_ATTR_MSG, token);
137 mbx_mem->len = SCMI_PROTO_MSG_ATTR_MSG_LEN;
138 mbx_mem->flags = SCMI_FLAG_RESP_POLL;
139 SCMI_PAYLOAD_ARG1(mbx_mem->payload, command_id);
140
141 scmi_send_sync_command(ch);
142
143 /* Get the return values */
144 SCMI_PAYLOAD_RET_VAL2(mbx_mem->payload, ret, *attr);
145 assert(mbx_mem->len == SCMI_PROTO_MSG_ATTR_RESP_LEN);
146 assert(token == SCMI_MSG_GET_TOKEN(mbx_mem->msg_header));
147
148 scmi_put_channel(ch);
149
150 return ret;
151}
152
153/*
154 * SCMI Driver initialization API. Returns initialized channel on success
155 * or NULL on error. The return type is an opaque void pointer.
156 */
157void *scmi_init(scmi_channel_t *ch)
158{
159 uint32_t version;
160 int ret;
161
162 assert(ch && ch->info);
163 assert(ch->info->db_reg_addr);
164 assert(ch->info->db_modify_mask);
165 assert(ch->info->db_preserve_mask);
Samarth Parikh59cfa132017-11-23 14:23:21 +0530166 assert(ch->info->ring_doorbell != NULL);
Soby Mathewea26bad2016-11-14 12:25:45 +0000167
168 assert(ch->lock);
169
Roberto Vargas00996942017-11-13 13:41:58 +0000170 scmi_lock_init(ch->lock);
Soby Mathewea26bad2016-11-14 12:25:45 +0000171
172 ch->is_initialized = 1;
173
174 ret = scmi_proto_version(ch, SCMI_PWR_DMN_PROTO_ID, &version);
175 if (ret != SCMI_E_SUCCESS) {
Heyi Guo9e284992021-06-08 19:59:53 +0800176 WARN("SCMI power domain protocol version message failed\n");
Soby Mathewea26bad2016-11-14 12:25:45 +0000177 goto error;
178 }
179
180 if (!is_scmi_version_compatible(SCMI_PWR_DMN_PROTO_VER, version)) {
Heyi Guo9e284992021-06-08 19:59:53 +0800181 WARN("SCMI power domain protocol version 0x%x incompatible with driver version 0x%x\n",
Soby Mathewea26bad2016-11-14 12:25:45 +0000182 version, SCMI_PWR_DMN_PROTO_VER);
183 goto error;
184 }
185
186 VERBOSE("SCMI power domain protocol version 0x%x detected\n", version);
187
188 ret = scmi_proto_version(ch, SCMI_SYS_PWR_PROTO_ID, &version);
189 if ((ret != SCMI_E_SUCCESS)) {
Heyi Guo9e284992021-06-08 19:59:53 +0800190 WARN("SCMI system power protocol version message failed\n");
Soby Mathewea26bad2016-11-14 12:25:45 +0000191 goto error;
192 }
193
194 if (!is_scmi_version_compatible(SCMI_SYS_PWR_PROTO_VER, version)) {
Heyi Guo9e284992021-06-08 19:59:53 +0800195 WARN("SCMI system power management protocol version 0x%x incompatible with driver version 0x%x\n",
Soby Mathewea26bad2016-11-14 12:25:45 +0000196 version, SCMI_SYS_PWR_PROTO_VER);
197 goto error;
198 }
199
200 VERBOSE("SCMI system power management protocol version 0x%x detected\n",
201 version);
202
203 INFO("SCMI driver initialized\n");
204
205 return (void *)ch;
206
207error:
208 ch->is_initialized = 0;
209 return NULL;
210}