blob: b406b3f78f4678142a5ac76c1c45f580d7ba7bd3 [file] [log] [blame]
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * System Control and Management Interface (SCMI) Message Protocol driver
4 *
5 * SCMI Message Protocol is used between the System Control Processor(SCP)
6 * and the Application Processors(AP). The Message Handling Unit(MHU)
7 * provides a mechanism for inter-processor communication between SCP's
8 * Cortex M3 and AP.
9 *
10 * SCP offers control and management of the core/cluster power states,
11 * various power domain DVFS including the core/cluster, certain system
12 * clocks configuration, thermal sensors and many others.
13 *
Cristian Marussi48dc16e2021-03-16 12:48:26 +000014 * Copyright (C) 2018-2021 ARM Ltd.
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010015 */
16
17#include <linux/bitmap.h>
Cristian Marussi23934ef2021-03-16 12:48:28 +000018#include <linux/device.h>
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010019#include <linux/export.h>
Cristian Marussi48dc16e2021-03-16 12:48:26 +000020#include <linux/idr.h>
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010021#include <linux/io.h>
22#include <linux/kernel.h>
Sudeep Hollad4c37512017-07-20 14:39:57 +010023#include <linux/ktime.h>
Cristian Marussi9ca5a182021-08-03 14:10:13 +010024#include <linux/hashtable.h>
Cristian Marussid4f9ddd2021-03-16 12:49:03 +000025#include <linux/list.h>
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010026#include <linux/module.h>
27#include <linux/of_address.h>
28#include <linux/of_device.h>
Sudeep Hollad4c37512017-07-20 14:39:57 +010029#include <linux/processor.h>
Cristian Marussi48dc16e2021-03-16 12:48:26 +000030#include <linux/refcount.h>
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010031#include <linux/slab.h>
32
33#include "common.h"
Cristian Marussi6b8a6912020-07-01 16:53:43 +010034#include "notify.h"
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010035
Lukasz Luba729d3532019-12-17 13:43:45 +000036#define CREATE_TRACE_POINTS
37#include <trace/events/scmi.h>
38
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010039enum scmi_error_codes {
40 SCMI_SUCCESS = 0, /* Success */
41 SCMI_ERR_SUPPORT = -1, /* Not supported */
42 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
43 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
44 SCMI_ERR_ENTRY = -4, /* Not found */
45 SCMI_ERR_RANGE = -5, /* Value out of range */
46 SCMI_ERR_BUSY = -6, /* Device busy */
47 SCMI_ERR_COMMS = -7, /* Communication Error */
48 SCMI_ERR_GENERIC = -8, /* Generic Error */
49 SCMI_ERR_HARDWARE = -9, /* Hardware Error */
50 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010051};
52
Sudeep Holla1baf47c2018-05-09 17:52:06 +010053/* List of all SCMI devices active in system */
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010054static LIST_HEAD(scmi_list);
55/* Protection for the entire list */
56static DEFINE_MUTEX(scmi_list_mutex);
Lukasz Luba729d3532019-12-17 13:43:45 +000057/* Track the unique id for the transfers for debug & profiling purpose */
58static atomic_t transfer_last_id;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010059
Cristian Marussid4f9ddd2021-03-16 12:49:03 +000060static DEFINE_IDR(scmi_requested_devices);
61static DEFINE_MUTEX(scmi_requested_devices_mtx);
62
63struct scmi_requested_dev {
64 const struct scmi_device_id *id_table;
65 struct list_head node;
66};
67
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010068/**
69 * struct scmi_xfers_info - Structure to manage transfer information
70 *
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010071 * @xfer_alloc_table: Bitmap table for allocated messages.
72 * Index of this bitmap table is also used for message
73 * sequence identifier.
74 * @xfer_lock: Protection for message allocation
Igor Skalkinc92c3e32021-08-03 14:10:19 +010075 * @max_msg: Maximum number of messages that can be pending
Cristian Marussi9ca5a182021-08-03 14:10:13 +010076 * @free_xfers: A free list for available to use xfers. It is initialized with
77 * a number of xfers equal to the maximum allowed in-flight
78 * messages.
79 * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the
80 * currently in-flight messages.
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010081 */
82struct scmi_xfers_info {
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010083 unsigned long *xfer_alloc_table;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010084 spinlock_t xfer_lock;
Igor Skalkinc92c3e32021-08-03 14:10:19 +010085 int max_msg;
Cristian Marussi9ca5a182021-08-03 14:10:13 +010086 struct hlist_head free_xfers;
87 DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +010088};
89
90/**
Cristian Marussi48dc16e2021-03-16 12:48:26 +000091 * struct scmi_protocol_instance - Describe an initialized protocol instance.
Cristian Marussid7b6cc52021-03-16 12:48:27 +000092 * @handle: Reference to the SCMI handle associated to this protocol instance.
Cristian Marussi48dc16e2021-03-16 12:48:26 +000093 * @proto: A reference to the protocol descriptor.
94 * @gid: A reference for per-protocol devres management.
95 * @users: A refcount to track effective users of this protocol.
Cristian Marussid7b6cc52021-03-16 12:48:27 +000096 * @priv: Reference for optional protocol private data.
97 * @ph: An embedded protocol handle that will be passed down to protocol
98 * initialization code to identify this instance.
Cristian Marussi48dc16e2021-03-16 12:48:26 +000099 *
100 * Each protocol is initialized independently once for each SCMI platform in
101 * which is defined by DT and implemented by the SCMI server fw.
102 */
103struct scmi_protocol_instance {
Cristian Marussid7b6cc52021-03-16 12:48:27 +0000104 const struct scmi_handle *handle;
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000105 const struct scmi_protocol *proto;
106 void *gid;
107 refcount_t users;
Cristian Marussid7b6cc52021-03-16 12:48:27 +0000108 void *priv;
109 struct scmi_protocol_handle ph;
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000110};
111
Cristian Marussid7b6cc52021-03-16 12:48:27 +0000112#define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
113
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000114/**
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100115 * struct scmi_info - Structure representing a SCMI instance
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100116 *
117 * @dev: Device pointer
118 * @desc: SoC description for this instance
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100119 * @version: SCMI revision information containing protocol version,
120 * implementation version and (sub-)vendor identification.
Viresh Kumar71af05a2020-01-31 10:58:11 +0530121 * @handle: Instance of SCMI handle to send to clients
Sudeep Holla38c927f2019-07-08 09:40:48 +0100122 * @tx_minfo: Universal Transmit Message management info
Sudeep Holla4ebd8f62020-03-27 14:34:26 +0000123 * @rx_minfo: Universal Receive Message management info
Sudeep Holla3748daf2019-07-08 09:40:44 +0100124 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
Sudeep Holla46cc7c22019-07-08 09:40:46 +0100125 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000126 * @protocols: IDR for protocols' instance descriptors initialized for
127 * this SCMI instance: populated on protocol's first attempted
128 * usage.
129 * @protocols_mtx: A mutex to protect protocols instances initialization.
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100130 * @protocols_imp: List of protocols implemented, currently maximum of
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100131 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
Cristian Marussid4f9ddd2021-03-16 12:49:03 +0000132 * @active_protocols: IDR storing device_nodes for protocols actually defined
133 * in the DT and confirmed as implemented by fw.
Cristian Marussia02d7c92021-03-16 12:49:00 +0000134 * @notify_priv: Pointer to private data structure specific to notifications.
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100135 * @node: List head
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100136 * @users: Number of users of this instance
137 */
138struct scmi_info {
139 struct device *dev;
140 const struct scmi_desc *desc;
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100141 struct scmi_revision_info version;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100142 struct scmi_handle handle;
Sudeep Holla38c927f2019-07-08 09:40:48 +0100143 struct scmi_xfers_info tx_minfo;
Sudeep Holla4ebd8f62020-03-27 14:34:26 +0000144 struct scmi_xfers_info rx_minfo;
Sudeep Holla907b6d12017-07-31 15:43:27 +0100145 struct idr tx_idr;
Sudeep Holla46cc7c22019-07-08 09:40:46 +0100146 struct idr rx_idr;
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000147 struct idr protocols;
148 /* Ensure mutual exclusive access to protocols instance array */
149 struct mutex protocols_mtx;
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100150 u8 *protocols_imp;
Cristian Marussid4f9ddd2021-03-16 12:49:03 +0000151 struct idr active_protocols;
Cristian Marussia02d7c92021-03-16 12:49:00 +0000152 void *notify_priv;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100153 struct list_head node;
154 int users;
155};
156
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100157#define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
158
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100159static const int scmi_linux_errmap[] = {
160 /* better than switch case as long as return value is continuous */
161 0, /* SCMI_SUCCESS */
162 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
163 -EINVAL, /* SCMI_ERR_PARAM */
164 -EACCES, /* SCMI_ERR_ACCESS */
165 -ENOENT, /* SCMI_ERR_ENTRY */
166 -ERANGE, /* SCMI_ERR_RANGE */
167 -EBUSY, /* SCMI_ERR_BUSY */
168 -ECOMM, /* SCMI_ERR_COMMS */
169 -EIO, /* SCMI_ERR_GENERIC */
170 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
171 -EPROTO, /* SCMI_ERR_PROTOCOL */
172};
173
174static inline int scmi_to_linux_errno(int errno)
175{
Sudeep Holla7a691f12021-07-07 14:50:28 +0100176 int err_idx = -errno;
177
178 if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
179 return scmi_linux_errmap[err_idx];
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100180 return -EIO;
181}
182
Cristian Marussia02d7c92021-03-16 12:49:00 +0000183void scmi_notification_instance_data_set(const struct scmi_handle *handle,
184 void *priv)
185{
186 struct scmi_info *info = handle_to_scmi_info(handle);
187
188 info->notify_priv = priv;
189 /* Ensure updated protocol private date are visible */
190 smp_wmb();
191}
192
193void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
194{
195 struct scmi_info *info = handle_to_scmi_info(handle);
196
197 /* Ensure protocols_private_data has been updated */
198 smp_rmb();
199 return info->notify_priv;
200}
201
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100202/**
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100203 * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
204 *
205 * @minfo: Pointer to Tx/Rx Message management info based on channel type
206 * @xfer: The xfer to act upon
207 *
208 * Pick the next unused monotonically increasing token and set it into
209 * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
210 * reuse of freshly completed or timed-out xfers, thus mitigating the risk
211 * of incorrect association of a late and expired xfer with a live in-flight
212 * transaction, both happening to re-use the same token identifier.
213 *
214 * Since platform is NOT required to answer our request in-order we should
215 * account for a few rare but possible scenarios:
216 *
217 * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
218 * using find_next_zero_bit() starting from candidate next_token bit
219 *
220 * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
221 * are plenty of free tokens at start, so try a second pass using
222 * find_next_zero_bit() and starting from 0.
223 *
224 * X = used in-flight
225 *
226 * Normal
227 * ------
228 *
229 * |- xfer_id picked
230 * -----------+----------------------------------------------------------
231 * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
232 * ----------------------------------------------------------------------
233 * ^
234 * |- next_token
235 *
236 * Out-of-order pending at start
237 * -----------------------------
238 *
239 * |- xfer_id picked, last_token fixed
240 * -----+----------------------------------------------------------------
241 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
242 * ----------------------------------------------------------------------
243 * ^
244 * |- next_token
245 *
246 *
247 * Out-of-order pending at end
248 * ---------------------------
249 *
250 * |- xfer_id picked, last_token fixed
251 * -----+----------------------------------------------------------------
252 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
253 * ----------------------------------------------------------------------
254 * ^
255 * |- next_token
256 *
257 * Context: Assumes to be called with @xfer_lock already acquired.
258 *
259 * Return: 0 on Success or error
260 */
261static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
262 struct scmi_xfer *xfer)
263{
264 unsigned long xfer_id, next_token;
265
266 /*
267 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
268 * using the pre-allocated transfer_id as a base.
269 * Note that the global transfer_id is shared across all message types
270 * so there could be holes in the allocated set of monotonic sequence
271 * numbers, but that is going to limit the effectiveness of the
272 * mitigation only in very rare limit conditions.
273 */
274 next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
275
276 /* Pick the next available xfer_id >= next_token */
277 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
278 MSG_TOKEN_MAX, next_token);
279 if (xfer_id == MSG_TOKEN_MAX) {
280 /*
281 * After heavily out-of-order responses, there are no free
282 * tokens ahead, but only at start of xfer_alloc_table so
283 * try again from the beginning.
284 */
285 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
286 MSG_TOKEN_MAX, 0);
287 /*
288 * Something is wrong if we got here since there can be a
289 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
290 * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
291 */
292 if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
293 return -ENOMEM;
294 }
295
296 /* Update +/- last_token accordingly if we skipped some hole */
297 if (xfer_id != next_token)
298 atomic_add((int)(xfer_id - next_token), &transfer_last_id);
299
300 /* Set in-flight */
301 set_bit(xfer_id, minfo->xfer_alloc_table);
302 xfer->hdr.seq = (u16)xfer_id;
303
304 return 0;
305}
306
307/**
308 * scmi_xfer_token_clear - Release the token
309 *
310 * @minfo: Pointer to Tx/Rx Message management info based on channel type
311 * @xfer: The xfer to act upon
312 */
313static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
314 struct scmi_xfer *xfer)
315{
316 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
317}
318
319/**
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100320 * scmi_xfer_get() - Allocate one message
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100321 *
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100322 * @handle: Pointer to SCMI entity handle
Sudeep Holla38c927f2019-07-08 09:40:48 +0100323 * @minfo: Pointer to Tx/Rx Message management info based on channel type
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100324 * @set_pending: If true a monotonic token is picked and the xfer is added to
325 * the pending hash table.
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100326 *
Sudeep Holla5b65af82019-07-08 09:40:40 +0100327 * Helper function which is used by various message functions that are
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100328 * exposed to clients of this driver for allocating a message traffic event.
329 *
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100330 * Picks an xfer from the free list @free_xfers (if any available) and, if
331 * required, sets a monotonically increasing token and stores the inflight xfer
332 * into the @pending_xfers hashtable for later retrieval.
333 *
334 * The successfully initialized xfer is refcounted.
335 *
336 * Context: Holds @xfer_lock while manipulating @xfer_alloc_table and
337 * @free_xfers.
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100338 *
339 * Return: 0 if all went fine, else corresponding error.
340 */
Sudeep Holla38c927f2019-07-08 09:40:48 +0100341static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100342 struct scmi_xfers_info *minfo,
343 bool set_pending)
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100344{
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100345 int ret;
346 unsigned long flags;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100347 struct scmi_xfer *xfer;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100348
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100349 spin_lock_irqsave(&minfo->xfer_lock, flags);
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100350 if (hlist_empty(&minfo->free_xfers)) {
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100351 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
352 return ERR_PTR(-ENOMEM);
353 }
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100354
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100355 /* grab an xfer from the free_list */
356 xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
357 hlist_del_init(&xfer->node);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100358
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100359 /*
360 * Allocate transfer_id early so that can be used also as base for
361 * monotonic sequence number generation if needed.
362 */
Lukasz Luba729d3532019-12-17 13:43:45 +0000363 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100364
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100365 if (set_pending) {
366 /* Pick and set monotonic token */
367 ret = scmi_xfer_token_set(minfo, xfer);
368 if (!ret) {
369 hash_add(minfo->pending_xfers, &xfer->node,
370 xfer->hdr.seq);
371 xfer->pending = true;
372 } else {
373 dev_err(handle->dev,
374 "Failed to get monotonic token %d\n", ret);
375 hlist_add_head(&xfer->node, &minfo->free_xfers);
376 xfer = ERR_PTR(ret);
377 }
378 }
Cristian Marussied7c04c2021-08-03 14:10:14 +0100379
380 if (!IS_ERR(xfer)) {
381 refcount_set(&xfer->users, 1);
382 atomic_set(&xfer->busy, SCMI_XFER_FREE);
383 }
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100384 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
385
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100386 return xfer;
387}
388
389/**
Sudeep Holla38c927f2019-07-08 09:40:48 +0100390 * __scmi_xfer_put() - Release a message
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100391 *
Sudeep Holla38c927f2019-07-08 09:40:48 +0100392 * @minfo: Pointer to Tx/Rx Message management info based on channel type
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100393 * @xfer: message that was reserved by scmi_xfer_get
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100394 *
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100395 * After refcount check, possibly release an xfer, clearing the token slot,
396 * removing xfer from @pending_xfers and putting it back into free_xfers.
397 *
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100398 * This holds a spinlock to maintain integrity of internal data structures.
399 */
Sudeep Holla38c927f2019-07-08 09:40:48 +0100400static void
401__scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100402{
403 unsigned long flags;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100404
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100405 spin_lock_irqsave(&minfo->xfer_lock, flags);
Cristian Marussied7c04c2021-08-03 14:10:14 +0100406 if (refcount_dec_and_test(&xfer->users)) {
407 if (xfer->pending) {
408 scmi_xfer_token_clear(minfo, xfer);
409 hash_del(&xfer->node);
410 xfer->pending = false;
411 }
412 hlist_add_head(&xfer->node, &minfo->free_xfers);
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100413 }
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100414 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
415}
416
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100417/**
418 * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
419 *
420 * @minfo: Pointer to Tx/Rx Message management info based on channel type
421 * @xfer_id: Token ID to lookup in @pending_xfers
422 *
423 * Refcounting is untouched.
424 *
425 * Context: Assumes to be called with @xfer_lock already acquired.
426 *
427 * Return: A valid xfer on Success or error otherwise
428 */
429static struct scmi_xfer *
430scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
431{
432 struct scmi_xfer *xfer = NULL;
433
434 if (test_bit(xfer_id, minfo->xfer_alloc_table))
435 xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
436
437 return xfer ?: ERR_PTR(-EINVAL);
438}
439
Cristian Marussied7c04c2021-08-03 14:10:14 +0100440/**
441 * scmi_msg_response_validate - Validate message type against state of related
442 * xfer
443 *
444 * @cinfo: A reference to the channel descriptor.
445 * @msg_type: Message type to check
446 * @xfer: A reference to the xfer to validate against @msg_type
447 *
448 * This function checks if @msg_type is congruent with the current state of
449 * a pending @xfer; if an asynchronous delayed response is received before the
450 * related synchronous response (Out-of-Order Delayed Response) the missing
451 * synchronous response is assumed to be OK and completed, carrying on with the
452 * Delayed Response: this is done to address the case in which the underlying
453 * SCMI transport can deliver such out-of-order responses.
454 *
455 * Context: Assumes to be called with xfer->lock already acquired.
456 *
457 * Return: 0 on Success, error otherwise
458 */
459static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
460 u8 msg_type,
461 struct scmi_xfer *xfer)
462{
463 /*
464 * Even if a response was indeed expected on this slot at this point,
465 * a buggy platform could wrongly reply feeding us an unexpected
466 * delayed response we're not prepared to handle: bail-out safely
467 * blaming firmware.
468 */
469 if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
470 dev_err(cinfo->dev,
471 "Delayed Response for %d not expected! Buggy F/W ?\n",
472 xfer->hdr.seq);
473 return -EINVAL;
474 }
475
476 switch (xfer->state) {
477 case SCMI_XFER_SENT_OK:
478 if (msg_type == MSG_TYPE_DELAYED_RESP) {
479 /*
480 * Delayed Response expected but delivered earlier.
481 * Assume message RESPONSE was OK and skip state.
482 */
483 xfer->hdr.status = SCMI_SUCCESS;
484 xfer->state = SCMI_XFER_RESP_OK;
485 complete(&xfer->done);
486 dev_warn(cinfo->dev,
487 "Received valid OoO Delayed Response for %d\n",
488 xfer->hdr.seq);
489 }
490 break;
491 case SCMI_XFER_RESP_OK:
492 if (msg_type != MSG_TYPE_DELAYED_RESP)
493 return -EINVAL;
494 break;
495 case SCMI_XFER_DRESP_OK:
496 /* No further message expected once in SCMI_XFER_DRESP_OK */
497 return -EINVAL;
498 }
499
500 return 0;
501}
502
503/**
504 * scmi_xfer_state_update - Update xfer state
505 *
506 * @xfer: A reference to the xfer to update
507 * @msg_type: Type of message being processed.
508 *
509 * Note that this message is assumed to have been already successfully validated
510 * by @scmi_msg_response_validate(), so here we just update the state.
511 *
512 * Context: Assumes to be called on an xfer exclusively acquired using the
513 * busy flag.
514 */
515static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
516{
517 xfer->hdr.type = msg_type;
518
519 /* Unknown command types were already discarded earlier */
520 if (xfer->hdr.type == MSG_TYPE_COMMAND)
521 xfer->state = SCMI_XFER_RESP_OK;
522 else
523 xfer->state = SCMI_XFER_DRESP_OK;
524}
525
526static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
527{
528 int ret;
529
530 ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
531
532 return ret == SCMI_XFER_FREE;
533}
534
535/**
536 * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
537 *
538 * @cinfo: A reference to the channel descriptor.
539 * @msg_hdr: A message header to use as lookup key
540 *
541 * When a valid xfer is found for the sequence number embedded in the provided
542 * msg_hdr, reference counting is properly updated and exclusive access to this
543 * xfer is granted till released with @scmi_xfer_command_release.
544 *
545 * Return: A valid @xfer on Success or error otherwise.
546 */
547static inline struct scmi_xfer *
548scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
549{
550 int ret;
551 unsigned long flags;
552 struct scmi_xfer *xfer;
553 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
554 struct scmi_xfers_info *minfo = &info->tx_minfo;
555 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
556 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
557
558 /* Are we even expecting this? */
559 spin_lock_irqsave(&minfo->xfer_lock, flags);
560 xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
561 if (IS_ERR(xfer)) {
562 dev_err(cinfo->dev,
563 "Message for %d type %d is not expected!\n",
564 xfer_id, msg_type);
565 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
566 return xfer;
567 }
568 refcount_inc(&xfer->users);
569 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
570
571 spin_lock_irqsave(&xfer->lock, flags);
572 ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
573 /*
574 * If a pending xfer was found which was also in a congruent state with
575 * the received message, acquire exclusive access to it setting the busy
576 * flag.
577 * Spins only on the rare limit condition of concurrent reception of
578 * RESP and DRESP for the same xfer.
579 */
580 if (!ret) {
581 spin_until_cond(scmi_xfer_acquired(xfer));
582 scmi_xfer_state_update(xfer, msg_type);
583 }
584 spin_unlock_irqrestore(&xfer->lock, flags);
585
586 if (ret) {
587 dev_err(cinfo->dev,
588 "Invalid message type:%d for %d - HDR:0x%X state:%d\n",
589 msg_type, xfer_id, msg_hdr, xfer->state);
590 /* On error the refcount incremented above has to be dropped */
591 __scmi_xfer_put(minfo, xfer);
592 xfer = ERR_PTR(-EINVAL);
593 }
594
595 return xfer;
596}
597
598static inline void scmi_xfer_command_release(struct scmi_info *info,
599 struct scmi_xfer *xfer)
600{
601 atomic_set(&xfer->busy, SCMI_XFER_FREE);
602 __scmi_xfer_put(&info->tx_minfo, xfer);
603}
604
Cristian Marussie9b21c92021-08-03 14:10:15 +0100605static inline void scmi_clear_channel(struct scmi_info *info,
606 struct scmi_chan_info *cinfo)
607{
608 if (info->desc->ops->clear_channel)
609 info->desc->ops->clear_channel(cinfo);
610}
611
Cristian Marussi13fba872021-08-03 14:10:23 +0100612static void scmi_handle_notification(struct scmi_chan_info *cinfo,
613 u32 msg_hdr, void *priv)
Sudeep Holla2747a962019-07-08 09:40:42 +0100614{
Sudeep Holla4d098522020-03-27 14:34:29 +0000615 struct scmi_xfer *xfer;
616 struct device *dev = cinfo->dev;
617 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
618 struct scmi_xfers_info *minfo = &info->rx_minfo;
Cristian Marussi72a5eb9d2020-07-10 14:39:19 +0100619 ktime_t ts;
Sudeep Holla4d098522020-03-27 14:34:29 +0000620
Cristian Marussi72a5eb9d2020-07-10 14:39:19 +0100621 ts = ktime_get_boottime();
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100622 xfer = scmi_xfer_get(cinfo->handle, minfo, false);
Sudeep Holla4d098522020-03-27 14:34:29 +0000623 if (IS_ERR(xfer)) {
624 dev_err(dev, "failed to get free message slot (%ld)\n",
625 PTR_ERR(xfer));
Cristian Marussie9b21c92021-08-03 14:10:15 +0100626 scmi_clear_channel(info, cinfo);
Sudeep Holla4d098522020-03-27 14:34:29 +0000627 return;
628 }
629
630 unpack_scmi_header(msg_hdr, &xfer->hdr);
Cristian Marussi13fba872021-08-03 14:10:23 +0100631 if (priv)
632 xfer->priv = priv;
Sudeep Holla4d098522020-03-27 14:34:29 +0000633 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
634 xfer);
Cristian Marussi6b8a6912020-07-01 16:53:43 +0100635 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
636 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
Sudeep Holla4d098522020-03-27 14:34:29 +0000637
638 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
639 xfer->hdr.protocol_id, xfer->hdr.seq,
640 MSG_TYPE_NOTIFICATION);
641
642 __scmi_xfer_put(minfo, xfer);
643
Cristian Marussie9b21c92021-08-03 14:10:15 +0100644 scmi_clear_channel(info, cinfo);
Sudeep Holla4d098522020-03-27 14:34:29 +0000645}
646
Cristian Marussi13fba872021-08-03 14:10:23 +0100647static void scmi_handle_response(struct scmi_chan_info *cinfo,
648 u32 msg_hdr, void *priv)
Sudeep Holla4d098522020-03-27 14:34:29 +0000649{
650 struct scmi_xfer *xfer;
Sudeep Holla2747a962019-07-08 09:40:42 +0100651 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
Sudeep Holla2747a962019-07-08 09:40:42 +0100652
Cristian Marussied7c04c2021-08-03 14:10:14 +0100653 xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100654 if (IS_ERR(xfer)) {
Cristian Marussie9b21c92021-08-03 14:10:15 +0100655 scmi_clear_channel(info, cinfo);
Sudeep Holla2747a962019-07-08 09:40:42 +0100656 return;
657 }
658
Cristian Marussi0cb7af42021-06-01 11:24:17 +0100659 /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
Cristian Marussied7c04c2021-08-03 14:10:14 +0100660 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
Cristian Marussi0cb7af42021-06-01 11:24:17 +0100661 xfer->rx.len = info->desc->max_msg_size;
662
Cristian Marussi13fba872021-08-03 14:10:23 +0100663 if (priv)
664 xfer->priv = priv;
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530665 info->desc->ops->fetch_response(cinfo, xfer);
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100666
Lukasz Luba729d3532019-12-17 13:43:45 +0000667 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
668 xfer->hdr.protocol_id, xfer->hdr.seq,
Cristian Marussied7c04c2021-08-03 14:10:14 +0100669 xfer->hdr.type);
Lukasz Luba729d3532019-12-17 13:43:45 +0000670
Cristian Marussied7c04c2021-08-03 14:10:14 +0100671 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
Cristian Marussie9b21c92021-08-03 14:10:15 +0100672 scmi_clear_channel(info, cinfo);
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100673 complete(xfer->async_done);
Cristian Marussid04fb2b2020-04-20 16:23:14 +0100674 } else {
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100675 complete(&xfer->done);
Cristian Marussid04fb2b2020-04-20 16:23:14 +0100676 }
Cristian Marussied7c04c2021-08-03 14:10:14 +0100677
678 scmi_xfer_command_release(info, xfer);
Sudeep Holla2747a962019-07-08 09:40:42 +0100679}
680
Sudeep Holla38c927f2019-07-08 09:40:48 +0100681/**
Sudeep Holla4d098522020-03-27 14:34:29 +0000682 * scmi_rx_callback() - callback for receiving messages
683 *
684 * @cinfo: SCMI channel info
685 * @msg_hdr: Message header
Cristian Marussi13fba872021-08-03 14:10:23 +0100686 * @priv: Transport specific private data.
Sudeep Holla4d098522020-03-27 14:34:29 +0000687 *
688 * Processes one received message to appropriate transfer information and
689 * signals completion of the transfer.
690 *
691 * NOTE: This function will be invoked in IRQ context, hence should be
692 * as optimal as possible.
693 */
Cristian Marussi13fba872021-08-03 14:10:23 +0100694void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv)
Sudeep Holla4d098522020-03-27 14:34:29 +0000695{
Sudeep Holla4d098522020-03-27 14:34:29 +0000696 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
697
698 switch (msg_type) {
699 case MSG_TYPE_NOTIFICATION:
Cristian Marussi13fba872021-08-03 14:10:23 +0100700 scmi_handle_notification(cinfo, msg_hdr, priv);
Sudeep Holla4d098522020-03-27 14:34:29 +0000701 break;
702 case MSG_TYPE_COMMAND:
703 case MSG_TYPE_DELAYED_RESP:
Cristian Marussi13fba872021-08-03 14:10:23 +0100704 scmi_handle_response(cinfo, msg_hdr, priv);
Sudeep Holla4d098522020-03-27 14:34:29 +0000705 break;
706 default:
707 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
708 break;
709 }
710}
711
Cristian Marussia4a20b02021-03-16 12:48:33 +0000712/**
713 * xfer_put() - Release a transmit message
714 *
715 * @ph: Pointer to SCMI protocol handle
Cristian Marussied7c04c2021-08-03 14:10:14 +0100716 * @xfer: message that was reserved by xfer_get_init
Cristian Marussia4a20b02021-03-16 12:48:33 +0000717 */
718static void xfer_put(const struct scmi_protocol_handle *ph,
719 struct scmi_xfer *xfer)
720{
721 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
722 struct scmi_info *info = handle_to_scmi_info(pi->handle);
Sudeep Holla38c927f2019-07-08 09:40:48 +0100723
724 __scmi_xfer_put(&info->tx_minfo, xfer);
725}
726
Sudeep Hollad4c37512017-07-20 14:39:57 +0100727#define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
728
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530729static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
Sudeep Hollad4c37512017-07-20 14:39:57 +0100730 struct scmi_xfer *xfer, ktime_t stop)
731{
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530732 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
Sudeep Hollad4c37512017-07-20 14:39:57 +0100733
Cristian Marussied7c04c2021-08-03 14:10:14 +0100734 /*
735 * Poll also on xfer->done so that polling can be forcibly terminated
736 * in case of out-of-order receptions of delayed responses
737 */
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530738 return info->desc->ops->poll_done(cinfo, xfer) ||
Cristian Marussied7c04c2021-08-03 14:10:14 +0100739 try_wait_for_completion(&xfer->done) ||
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530740 ktime_after(ktime_get(), stop);
Sudeep Hollad4c37512017-07-20 14:39:57 +0100741}
742
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100743/**
Cristian Marussia4a20b02021-03-16 12:48:33 +0000744 * do_xfer() - Do one transfer
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100745 *
Cristian Marussia4a20b02021-03-16 12:48:33 +0000746 * @ph: Pointer to SCMI protocol handle
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100747 * @xfer: Transfer to initiate and wait for response
748 *
749 * Return: -ETIMEDOUT in case of no response, if transmit error,
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100750 * return corresponding error, else if all goes well,
751 * return 0.
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100752 */
Cristian Marussia4a20b02021-03-16 12:48:33 +0000753static int do_xfer(const struct scmi_protocol_handle *ph,
754 struct scmi_xfer *xfer)
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100755{
756 int ret;
757 int timeout;
Cristian Marussia4a20b02021-03-16 12:48:33 +0000758 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
759 struct scmi_info *info = handle_to_scmi_info(pi->handle);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100760 struct device *dev = info->dev;
Sudeep Holla907b6d12017-07-31 15:43:27 +0100761 struct scmi_chan_info *cinfo;
762
Cristian Marussi2930abc2021-08-03 14:10:16 +0100763 if (xfer->hdr.poll_completion && !info->desc->ops->poll_done) {
764 dev_warn_once(dev,
765 "Polling mode is not supported by transport.\n");
766 return -EINVAL;
767 }
768
Cristian Marussia4a20b02021-03-16 12:48:33 +0000769 /*
Sudeep Holla61832b32021-06-08 15:01:40 +0100770 * Initialise protocol id now from protocol handle to avoid it being
Cristian Marussia4a20b02021-03-16 12:48:33 +0000771 * overridden by mistake (or malice) by the protocol code mangling with
Sudeep Holla61832b32021-06-08 15:01:40 +0100772 * the scmi_xfer structure prior to this.
Cristian Marussia4a20b02021-03-16 12:48:33 +0000773 */
774 xfer->hdr.protocol_id = pi->proto->id;
Cristian Marussie30d91d2021-06-06 23:12:24 +0100775 reinit_completion(&xfer->done);
Cristian Marussia4a20b02021-03-16 12:48:33 +0000776
Sudeep Holla907b6d12017-07-31 15:43:27 +0100777 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
778 if (unlikely(!cinfo))
779 return -EINVAL;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100780
Lukasz Luba729d3532019-12-17 13:43:45 +0000781 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
782 xfer->hdr.protocol_id, xfer->hdr.seq,
783 xfer->hdr.poll_completion);
784
Cristian Marussied7c04c2021-08-03 14:10:14 +0100785 xfer->state = SCMI_XFER_SENT_OK;
786 /*
787 * Even though spinlocking is not needed here since no race is possible
788 * on xfer->state due to the monotonically increasing tokens allocation,
789 * we must anyway ensure xfer->state initialization is not re-ordered
790 * after the .send_message() to be sure that on the RX path an early
791 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
792 */
793 smp_mb();
794
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530795 ret = info->desc->ops->send_message(cinfo, xfer);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100796 if (ret < 0) {
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530797 dev_dbg(dev, "Failed to send message %d\n", ret);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100798 return ret;
799 }
800
Sudeep Hollad4c37512017-07-20 14:39:57 +0100801 if (xfer->hdr.poll_completion) {
802 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
803
Sudeep Hollafbc4d812017-07-31 15:25:32 +0100804 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
Cristian Marussied7c04c2021-08-03 14:10:14 +0100805 if (ktime_before(ktime_get(), stop)) {
806 unsigned long flags;
807
808 /*
809 * Do not fetch_response if an out-of-order delayed
810 * response is being processed.
811 */
812 spin_lock_irqsave(&xfer->lock, flags);
813 if (xfer->state == SCMI_XFER_SENT_OK) {
814 info->desc->ops->fetch_response(cinfo, xfer);
815 xfer->state = SCMI_XFER_RESP_OK;
816 }
817 spin_unlock_irqrestore(&xfer->lock, flags);
818 } else {
Sudeep Hollad4c37512017-07-20 14:39:57 +0100819 ret = -ETIMEDOUT;
Cristian Marussied7c04c2021-08-03 14:10:14 +0100820 }
Sudeep Hollad4c37512017-07-20 14:39:57 +0100821 } else {
822 /* And we wait for the response. */
823 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
824 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530825 dev_err(dev, "timed out in resp(caller: %pS)\n",
Sudeep Hollad4c37512017-07-20 14:39:57 +0100826 (void *)_RET_IP_);
827 ret = -ETIMEDOUT;
828 }
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100829 }
Sudeep Hollad4c37512017-07-20 14:39:57 +0100830
831 if (!ret && xfer->hdr.status)
832 ret = scmi_to_linux_errno(xfer->hdr.status);
833
Viresh Kumar5c8a47a2020-01-31 10:58:13 +0530834 if (info->desc->ops->mark_txdone)
835 info->desc->ops->mark_txdone(cinfo, ret);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100836
Lukasz Luba729d3532019-12-17 13:43:45 +0000837 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
Sudeep Hollabad0d732020-06-09 14:45:03 +0100838 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
Lukasz Luba729d3532019-12-17 13:43:45 +0000839
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100840 return ret;
841}
842
Cristian Marussia4a20b02021-03-16 12:48:33 +0000843static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
844 struct scmi_xfer *xfer)
845{
846 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
847 struct scmi_info *info = handle_to_scmi_info(pi->handle);
848
849 xfer->rx.len = info->desc->max_msg_size;
850}
851
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100852#define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
853
854/**
Cristian Marussia4a20b02021-03-16 12:48:33 +0000855 * do_xfer_with_response() - Do one transfer and wait until the delayed
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100856 * response is received
857 *
Cristian Marussia4a20b02021-03-16 12:48:33 +0000858 * @ph: Pointer to SCMI protocol handle
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100859 * @xfer: Transfer to initiate and wait for response
860 *
861 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
862 * return corresponding error, else if all goes well, return 0.
863 */
Cristian Marussia4a20b02021-03-16 12:48:33 +0000864static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
865 struct scmi_xfer *xfer)
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100866{
867 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
868 DECLARE_COMPLETION_ONSTACK(async_response);
869
870 xfer->async_done = &async_response;
871
Cristian Marussia4a20b02021-03-16 12:48:33 +0000872 ret = do_xfer(ph, xfer);
Cristian Marussif1748b12021-06-08 11:30:56 +0100873 if (!ret) {
874 if (!wait_for_completion_timeout(xfer->async_done, timeout))
875 ret = -ETIMEDOUT;
876 else if (xfer->hdr.status)
877 ret = scmi_to_linux_errno(xfer->hdr.status);
878 }
Sudeep Holla58ecdf02019-07-08 09:40:54 +0100879
880 xfer->async_done = NULL;
881 return ret;
882}
883
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100884/**
Cristian Marussia4a20b02021-03-16 12:48:33 +0000885 * xfer_get_init() - Allocate and initialise one message for transmit
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100886 *
Cristian Marussia4a20b02021-03-16 12:48:33 +0000887 * @ph: Pointer to SCMI protocol handle
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100888 * @msg_id: Message identifier
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100889 * @tx_size: transmit message size
890 * @rx_size: receive message size
891 * @p: pointer to the allocated and initialised message
892 *
Sudeep Holla14e297b2018-05-09 17:52:06 +0100893 * This function allocates the message using @scmi_xfer_get and
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100894 * initialise the header.
895 *
896 * Return: 0 if all went fine with @p pointing to message, else
897 * corresponding error.
898 */
Cristian Marussia4a20b02021-03-16 12:48:33 +0000899static int xfer_get_init(const struct scmi_protocol_handle *ph,
900 u8 msg_id, size_t tx_size, size_t rx_size,
901 struct scmi_xfer **p)
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100902{
903 int ret;
904 struct scmi_xfer *xfer;
Cristian Marussia4a20b02021-03-16 12:48:33 +0000905 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
906 struct scmi_info *info = handle_to_scmi_info(pi->handle);
Sudeep Holla38c927f2019-07-08 09:40:48 +0100907 struct scmi_xfers_info *minfo = &info->tx_minfo;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100908 struct device *dev = info->dev;
909
910 /* Ensure we have sane transfer sizes */
911 if (rx_size > info->desc->max_msg_size ||
912 tx_size > info->desc->max_msg_size)
913 return -ERANGE;
914
Cristian Marussi9ca5a182021-08-03 14:10:13 +0100915 xfer = scmi_xfer_get(pi->handle, minfo, true);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100916 if (IS_ERR(xfer)) {
917 ret = PTR_ERR(xfer);
918 dev_err(dev, "failed to get free message slot(%d)\n", ret);
919 return ret;
920 }
921
922 xfer->tx.len = tx_size;
923 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
Cristian Marussi63b282f2021-08-03 14:10:10 +0100924 xfer->hdr.type = MSG_TYPE_COMMAND;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100925 xfer->hdr.id = msg_id;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100926 xfer->hdr.poll_completion = false;
927
928 *p = xfer;
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100929
Sudeep Hollaaa4f8862017-03-28 11:36:07 +0100930 return 0;
931}
932
933/**
Cristian Marussia4a20b02021-03-16 12:48:33 +0000934 * version_get() - command to get the revision of the SCMI entity
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100935 *
Cristian Marussia4a20b02021-03-16 12:48:33 +0000936 * @ph: Pointer to SCMI protocol handle
Sudeep Holla1baf47c2018-05-09 17:52:06 +0100937 * @version: Holds returned version of protocol.
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100938 *
939 * Updates the SCMI information in the internal data structure.
940 *
941 * Return: 0 if all went fine, else return appropriate error.
942 */
Cristian Marussia4a20b02021-03-16 12:48:33 +0000943static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100944{
945 int ret;
946 __le32 *rev_info;
947 struct scmi_xfer *t;
948
Cristian Marussia4a20b02021-03-16 12:48:33 +0000949 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100950 if (ret)
951 return ret;
952
Cristian Marussia4a20b02021-03-16 12:48:33 +0000953 ret = do_xfer(ph, t);
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100954 if (!ret) {
955 rev_info = t->rx.buf;
956 *version = le32_to_cpu(*rev_info);
957 }
958
Cristian Marussia4a20b02021-03-16 12:48:33 +0000959 xfer_put(ph, t);
Sudeep Hollab6f20ff2017-06-06 11:16:15 +0100960 return ret;
961}
962
Cristian Marussi48dc16e2021-03-16 12:48:26 +0000963/**
Cristian Marussid7b6cc52021-03-16 12:48:27 +0000964 * scmi_set_protocol_priv - Set protocol specific data at init time
965 *
966 * @ph: A reference to the protocol handle.
967 * @priv: The private data to set.
968 *
969 * Return: 0 on Success
970 */
971static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
972 void *priv)
973{
974 struct scmi_protocol_instance *pi = ph_to_pi(ph);
975
976 pi->priv = priv;
977
978 return 0;
979}
980
981/**
982 * scmi_get_protocol_priv - Set protocol specific data at init time
983 *
984 * @ph: A reference to the protocol handle.
985 *
986 * Return: Protocol private data if any was set.
987 */
988static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
989{
990 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
991
992 return pi->priv;
993}
994
Cristian Marussia4a20b02021-03-16 12:48:33 +0000995static const struct scmi_xfer_ops xfer_ops = {
996 .version_get = version_get,
997 .xfer_get_init = xfer_get_init,
998 .reset_rx_to_maxsz = reset_rx_to_maxsz,
999 .do_xfer = do_xfer,
1000 .do_xfer_with_response = do_xfer_with_response,
1001 .xfer_put = xfer_put,
1002};
1003
Cristian Marussid7b6cc52021-03-16 12:48:27 +00001004/**
Cristian Marussi3d5d6e82021-03-16 12:48:34 +00001005 * scmi_revision_area_get - Retrieve version memory area.
1006 *
1007 * @ph: A reference to the protocol handle.
1008 *
1009 * A helper to grab the version memory area reference during SCMI Base protocol
1010 * initialization.
1011 *
1012 * Return: A reference to the version memory area associated to the SCMI
1013 * instance underlying this protocol handle.
1014 */
1015struct scmi_revision_info *
1016scmi_revision_area_get(const struct scmi_protocol_handle *ph)
1017{
1018 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1019
1020 return pi->handle->version;
1021}
1022
1023/**
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001024 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
1025 * instance descriptor.
1026 * @info: The reference to the related SCMI instance.
1027 * @proto: The protocol descriptor.
1028 *
1029 * Allocate a new protocol instance descriptor, using the provided @proto
1030 * description, against the specified SCMI instance @info, and initialize it;
1031 * all resources management is handled via a dedicated per-protocol devres
1032 * group.
1033 *
1034 * Context: Assumes to be called with @protocols_mtx already acquired.
1035 * Return: A reference to a freshly allocated and initialized protocol instance
Cristian Marussif5800e02021-03-16 12:49:02 +00001036 * or ERR_PTR on failure. On failure the @proto reference is at first
1037 * put using @scmi_protocol_put() before releasing all the devres group.
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001038 */
1039static struct scmi_protocol_instance *
1040scmi_alloc_init_protocol_instance(struct scmi_info *info,
1041 const struct scmi_protocol *proto)
1042{
1043 int ret = -ENOMEM;
1044 void *gid;
1045 struct scmi_protocol_instance *pi;
Cristian Marussif0e73ce2021-03-16 12:48:56 +00001046 const struct scmi_handle *handle = &info->handle;
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001047
1048 /* Protocol specific devres group */
1049 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
Cristian Marussif5800e02021-03-16 12:49:02 +00001050 if (!gid) {
1051 scmi_protocol_put(proto->id);
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001052 goto out;
Cristian Marussif5800e02021-03-16 12:49:02 +00001053 }
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001054
1055 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
1056 if (!pi)
1057 goto clean;
1058
1059 pi->gid = gid;
1060 pi->proto = proto;
Cristian Marussid7b6cc52021-03-16 12:48:27 +00001061 pi->handle = handle;
1062 pi->ph.dev = handle->dev;
Cristian Marussia4a20b02021-03-16 12:48:33 +00001063 pi->ph.xops = &xfer_ops;
Cristian Marussid7b6cc52021-03-16 12:48:27 +00001064 pi->ph.set_priv = scmi_set_protocol_priv;
1065 pi->ph.get_priv = scmi_get_protocol_priv;
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001066 refcount_set(&pi->users, 1);
1067 /* proto->init is assured NON NULL by scmi_protocol_register */
Cristian Marussia4a20b02021-03-16 12:48:33 +00001068 ret = pi->proto->instance_init(&pi->ph);
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001069 if (ret)
1070 goto clean;
1071
1072 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
1073 GFP_KERNEL);
1074 if (ret != proto->id)
1075 goto clean;
1076
Cristian Marussi533c7092021-03-16 12:48:31 +00001077 /*
1078 * Warn but ignore events registration errors since we do not want
1079 * to skip whole protocols if their notifications are messed up.
1080 */
1081 if (pi->proto->events) {
1082 ret = scmi_register_protocol_events(handle, pi->proto->id,
Cristian Marussib9f7fd92021-03-16 12:48:32 +00001083 &pi->ph,
Cristian Marussi533c7092021-03-16 12:48:31 +00001084 pi->proto->events);
1085 if (ret)
1086 dev_warn(handle->dev,
1087 "Protocol:%X - Events Registration Failed - err:%d\n",
1088 pi->proto->id, ret);
1089 }
1090
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001091 devres_close_group(handle->dev, pi->gid);
1092 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
1093
1094 return pi;
1095
1096clean:
Cristian Marussif5800e02021-03-16 12:49:02 +00001097 /* Take care to put the protocol module's owner before releasing all */
1098 scmi_protocol_put(proto->id);
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001099 devres_release_group(handle->dev, gid);
1100out:
1101 return ERR_PTR(ret);
1102}
1103
1104/**
1105 * scmi_get_protocol_instance - Protocol initialization helper.
1106 * @handle: A reference to the SCMI platform instance.
1107 * @protocol_id: The protocol being requested.
1108 *
1109 * In case the required protocol has never been requested before for this
1110 * instance, allocate and initialize all the needed structures while handling
1111 * resource allocation with a dedicated per-protocol devres subgroup.
1112 *
Cristian Marussif5800e02021-03-16 12:49:02 +00001113 * Return: A reference to an initialized protocol instance or error on failure:
1114 * in particular returns -EPROBE_DEFER when the desired protocol could
1115 * NOT be found.
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001116 */
1117static struct scmi_protocol_instance * __must_check
Cristian Marussif0e73ce2021-03-16 12:48:56 +00001118scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001119{
1120 struct scmi_protocol_instance *pi;
1121 struct scmi_info *info = handle_to_scmi_info(handle);
1122
1123 mutex_lock(&info->protocols_mtx);
1124 pi = idr_find(&info->protocols, protocol_id);
1125
1126 if (pi) {
1127 refcount_inc(&pi->users);
1128 } else {
1129 const struct scmi_protocol *proto;
1130
1131 /* Fails if protocol not registered on bus */
1132 proto = scmi_protocol_get(protocol_id);
1133 if (proto)
1134 pi = scmi_alloc_init_protocol_instance(info, proto);
1135 else
Cristian Marussif5800e02021-03-16 12:49:02 +00001136 pi = ERR_PTR(-EPROBE_DEFER);
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001137 }
1138 mutex_unlock(&info->protocols_mtx);
1139
1140 return pi;
1141}
1142
1143/**
1144 * scmi_protocol_acquire - Protocol acquire
1145 * @handle: A reference to the SCMI platform instance.
1146 * @protocol_id: The protocol being requested.
1147 *
1148 * Register a new user for the requested protocol on the specified SCMI
1149 * platform instance, possibly triggering its initialization on first user.
1150 *
1151 * Return: 0 if protocol was acquired successfully.
1152 */
Cristian Marussif0e73ce2021-03-16 12:48:56 +00001153int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001154{
1155 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
1156}
1157
1158/**
1159 * scmi_protocol_release - Protocol de-initialization helper.
1160 * @handle: A reference to the SCMI platform instance.
1161 * @protocol_id: The protocol being requested.
1162 *
1163 * Remove one user for the specified protocol and triggers de-initialization
1164 * and resources de-allocation once the last user has gone.
1165 */
Cristian Marussif0e73ce2021-03-16 12:48:56 +00001166void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001167{
1168 struct scmi_info *info = handle_to_scmi_info(handle);
1169 struct scmi_protocol_instance *pi;
1170
1171 mutex_lock(&info->protocols_mtx);
1172 pi = idr_find(&info->protocols, protocol_id);
1173 if (WARN_ON(!pi))
1174 goto out;
1175
1176 if (refcount_dec_and_test(&pi->users)) {
1177 void *gid = pi->gid;
1178
Cristian Marussi533c7092021-03-16 12:48:31 +00001179 if (pi->proto->events)
1180 scmi_deregister_protocol_events(handle, protocol_id);
1181
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001182 if (pi->proto->instance_deinit)
Cristian Marussia4a20b02021-03-16 12:48:33 +00001183 pi->proto->instance_deinit(&pi->ph);
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001184
1185 idr_remove(&info->protocols, protocol_id);
1186
Cristian Marussif5800e02021-03-16 12:49:02 +00001187 scmi_protocol_put(protocol_id);
1188
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001189 devres_release_group(handle->dev, gid);
1190 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
1191 protocol_id);
1192 }
1193
1194out:
1195 mutex_unlock(&info->protocols_mtx);
1196}
1197
Cristian Marussi8d3581c2021-03-16 12:48:35 +00001198void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
Sudeep Hollab6f20ff2017-06-06 11:16:15 +01001199 u8 *prot_imp)
1200{
Cristian Marussi8d3581c2021-03-16 12:48:35 +00001201 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1202 struct scmi_info *info = handle_to_scmi_info(pi->handle);
Sudeep Hollab6f20ff2017-06-06 11:16:15 +01001203
1204 info->protocols_imp = prot_imp;
1205}
1206
Sudeep Hollabc400812017-06-06 11:39:08 +01001207static bool
1208scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
1209{
1210 int i;
1211 struct scmi_info *info = handle_to_scmi_info(handle);
1212
1213 if (!info->protocols_imp)
1214 return false;
1215
1216 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
1217 if (info->protocols_imp[i] == prot_id)
1218 return true;
1219 return false;
1220}
1221
Cristian Marussi23934ef2021-03-16 12:48:28 +00001222struct scmi_protocol_devres {
Cristian Marussif0e73ce2021-03-16 12:48:56 +00001223 const struct scmi_handle *handle;
Cristian Marussi23934ef2021-03-16 12:48:28 +00001224 u8 protocol_id;
1225};
1226
1227static void scmi_devm_release_protocol(struct device *dev, void *res)
1228{
1229 struct scmi_protocol_devres *dres = res;
1230
1231 scmi_protocol_release(dres->handle, dres->protocol_id);
1232}
1233
1234/**
1235 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
1236 * @sdev: A reference to an scmi_device whose embedded struct device is to
1237 * be used for devres accounting.
1238 * @protocol_id: The protocol being requested.
1239 * @ph: A pointer reference used to pass back the associated protocol handle.
1240 *
1241 * Get hold of a protocol accounting for its usage, eventually triggering its
1242 * initialization, and returning the protocol specific operations and related
1243 * protocol handle which will be used as first argument in most of the
1244 * protocols operations methods.
1245 * Being a devres based managed method, protocol hold will be automatically
1246 * released, and possibly de-initialized on last user, once the SCMI driver
1247 * owning the scmi_device is unbound from it.
1248 *
1249 * Return: A reference to the requested protocol operations or error.
1250 * Must be checked for errors by caller.
1251 */
1252static const void __must_check *
1253scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
1254 struct scmi_protocol_handle **ph)
1255{
1256 struct scmi_protocol_instance *pi;
1257 struct scmi_protocol_devres *dres;
1258 struct scmi_handle *handle = sdev->handle;
1259
1260 if (!ph)
1261 return ERR_PTR(-EINVAL);
1262
1263 dres = devres_alloc(scmi_devm_release_protocol,
1264 sizeof(*dres), GFP_KERNEL);
1265 if (!dres)
1266 return ERR_PTR(-ENOMEM);
1267
1268 pi = scmi_get_protocol_instance(handle, protocol_id);
1269 if (IS_ERR(pi)) {
1270 devres_free(dres);
1271 return pi;
1272 }
1273
1274 dres->handle = handle;
1275 dres->protocol_id = protocol_id;
1276 devres_add(&sdev->dev, dres);
1277
1278 *ph = &pi->ph;
1279
1280 return pi->proto->ops;
1281}
1282
1283static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
1284{
1285 struct scmi_protocol_devres *dres = res;
1286
1287 if (WARN_ON(!dres || !data))
1288 return 0;
1289
1290 return dres->protocol_id == *((u8 *)data);
1291}
1292
1293/**
1294 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
1295 * @sdev: A reference to an scmi_device whose embedded struct device is to
1296 * be used for devres accounting.
1297 * @protocol_id: The protocol being requested.
1298 *
1299 * Explicitly release a protocol hold previously obtained calling the above
1300 * @scmi_devm_protocol_get.
1301 */
1302static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
1303{
1304 int ret;
1305
1306 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
1307 scmi_devm_protocol_match, &protocol_id);
1308 WARN_ON(ret);
1309}
1310
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001311static inline
1312struct scmi_handle *scmi_handle_get_from_info_unlocked(struct scmi_info *info)
1313{
1314 info->users++;
1315 return &info->handle;
1316}
1317
Sudeep Hollab6f20ff2017-06-06 11:16:15 +01001318/**
Sudeep Holla14e297b2018-05-09 17:52:06 +01001319 * scmi_handle_get() - Get the SCMI handle for a device
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001320 *
1321 * @dev: pointer to device for which we want SCMI handle
1322 *
1323 * NOTE: The function does not track individual clients of the framework
Sudeep Holla1baf47c2018-05-09 17:52:06 +01001324 * and is expected to be maintained by caller of SCMI protocol library.
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001325 * scmi_handle_put must be balanced with successful scmi_handle_get
1326 *
1327 * Return: pointer to handle if successful, NULL on error
1328 */
1329struct scmi_handle *scmi_handle_get(struct device *dev)
1330{
1331 struct list_head *p;
1332 struct scmi_info *info;
1333 struct scmi_handle *handle = NULL;
1334
1335 mutex_lock(&scmi_list_mutex);
1336 list_for_each(p, &scmi_list) {
1337 info = list_entry(p, struct scmi_info, node);
1338 if (dev->parent == info->dev) {
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001339 handle = scmi_handle_get_from_info_unlocked(info);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001340 break;
1341 }
1342 }
1343 mutex_unlock(&scmi_list_mutex);
1344
1345 return handle;
1346}
1347
1348/**
1349 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
1350 *
1351 * @handle: handle acquired by scmi_handle_get
1352 *
1353 * NOTE: The function does not track individual clients of the framework
Sudeep Holla1baf47c2018-05-09 17:52:06 +01001354 * and is expected to be maintained by caller of SCMI protocol library.
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001355 * scmi_handle_put must be balanced with successful scmi_handle_get
1356 *
1357 * Return: 0 is successfully released
1358 * if null was passed, it returns -EINVAL;
1359 */
1360int scmi_handle_put(const struct scmi_handle *handle)
1361{
1362 struct scmi_info *info;
1363
1364 if (!handle)
1365 return -EINVAL;
1366
1367 info = handle_to_scmi_info(handle);
1368 mutex_lock(&scmi_list_mutex);
1369 if (!WARN_ON(!info->users))
1370 info->users--;
1371 mutex_unlock(&scmi_list_mutex);
1372
1373 return 0;
1374}
1375
Sudeep Holla4ebd8f62020-03-27 14:34:26 +00001376static int __scmi_xfer_info_init(struct scmi_info *sinfo,
1377 struct scmi_xfers_info *info)
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001378{
1379 int i;
1380 struct scmi_xfer *xfer;
1381 struct device *dev = sinfo->dev;
1382 const struct scmi_desc *desc = sinfo->desc;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001383
1384 /* Pre-allocated messages, no more than what hdr.seq can support */
Igor Skalkinc92c3e32021-08-03 14:10:19 +01001385 if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
Cristian Marussibdb87422021-07-12 15:18:18 +01001386 dev_err(dev,
1387 "Invalid maximum messages %d, not in range [1 - %lu]\n",
Igor Skalkinc92c3e32021-08-03 14:10:19 +01001388 info->max_msg, MSG_TOKEN_MAX);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001389 return -EINVAL;
1390 }
1391
Cristian Marussi9ca5a182021-08-03 14:10:13 +01001392 hash_init(info->pending_xfers);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001393
Cristian Marussi9ca5a182021-08-03 14:10:13 +01001394 /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
1395 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(MSG_TOKEN_MAX),
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001396 sizeof(long), GFP_KERNEL);
1397 if (!info->xfer_alloc_table)
1398 return -ENOMEM;
1399
Cristian Marussi9ca5a182021-08-03 14:10:13 +01001400 /*
1401 * Preallocate a number of xfers equal to max inflight messages,
1402 * pre-initialize the buffer pointer to pre-allocated buffers and
1403 * attach all of them to the free list
1404 */
1405 INIT_HLIST_HEAD(&info->free_xfers);
Igor Skalkinc92c3e32021-08-03 14:10:19 +01001406 for (i = 0; i < info->max_msg; i++) {
Cristian Marussi9ca5a182021-08-03 14:10:13 +01001407 xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
1408 if (!xfer)
1409 return -ENOMEM;
1410
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001411 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
1412 GFP_KERNEL);
1413 if (!xfer->rx.buf)
1414 return -ENOMEM;
1415
1416 xfer->tx.buf = xfer->rx.buf;
1417 init_completion(&xfer->done);
Cristian Marussied7c04c2021-08-03 14:10:14 +01001418 spin_lock_init(&xfer->lock);
Cristian Marussi9ca5a182021-08-03 14:10:13 +01001419
1420 /* Add initialized xfer to the free list */
1421 hlist_add_head(&xfer->node, &info->free_xfers);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001422 }
1423
1424 spin_lock_init(&info->xfer_lock);
1425
1426 return 0;
1427}
1428
Igor Skalkinc92c3e32021-08-03 14:10:19 +01001429static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
1430{
1431 const struct scmi_desc *desc = sinfo->desc;
1432
1433 if (!desc->ops->get_max_msg) {
1434 sinfo->tx_minfo.max_msg = desc->max_msg;
1435 sinfo->rx_minfo.max_msg = desc->max_msg;
1436 } else {
1437 struct scmi_chan_info *base_cinfo;
1438
1439 base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
1440 if (!base_cinfo)
1441 return -EINVAL;
1442 sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
1443
1444 /* RX channel is optional so can be skipped */
1445 base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
1446 if (base_cinfo)
1447 sinfo->rx_minfo.max_msg =
1448 desc->ops->get_max_msg(base_cinfo);
1449 }
1450
1451 return 0;
1452}
1453
Sudeep Holla4ebd8f62020-03-27 14:34:26 +00001454static int scmi_xfer_info_init(struct scmi_info *sinfo)
1455{
Igor Skalkinc92c3e32021-08-03 14:10:19 +01001456 int ret;
Sudeep Holla4ebd8f62020-03-27 14:34:26 +00001457
Igor Skalkinc92c3e32021-08-03 14:10:19 +01001458 ret = scmi_channels_max_msg_configure(sinfo);
1459 if (ret)
1460 return ret;
1461
1462 ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
Sudeep Holla4ebd8f62020-03-27 14:34:26 +00001463 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1464 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1465
1466 return ret;
1467}
1468
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301469static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1470 int prot_id, bool tx)
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001471{
Sudeep Holla3748daf2019-07-08 09:40:44 +01001472 int ret, idx;
Sudeep Hollafbc4d812017-07-31 15:25:32 +01001473 struct scmi_chan_info *cinfo;
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001474 struct idr *idr;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001475
Sudeep Holla3748daf2019-07-08 09:40:44 +01001476 /* Transmit channel is first entry i.e. index 0 */
1477 idx = tx ? 0 : 1;
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001478 idr = tx ? &info->tx_idr : &info->rx_idr;
Sudeep Holla3748daf2019-07-08 09:40:44 +01001479
Sudeep Holla11040882019-11-06 11:32:05 +00001480 /* check if already allocated, used for multiple device per protocol */
1481 cinfo = idr_find(idr, prot_id);
1482 if (cinfo)
1483 return 0;
1484
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301485 if (!info->desc->ops->chan_available(dev, idx)) {
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001486 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
1487 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
1488 return -EINVAL;
Sudeep Holla907b6d12017-07-31 15:43:27 +01001489 goto idr_alloc;
1490 }
1491
Sudeep Hollafbc4d812017-07-31 15:25:32 +01001492 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
1493 if (!cinfo)
1494 return -ENOMEM;
1495
Sudeep Hollafbc4d812017-07-31 15:25:32 +01001496 cinfo->dev = dev;
1497
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301498 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
1499 if (ret)
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001500 return ret;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001501
Sudeep Holla907b6d12017-07-31 15:43:27 +01001502idr_alloc:
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001503 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
Sudeep Holla907b6d12017-07-31 15:43:27 +01001504 if (ret != prot_id) {
1505 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
1506 return ret;
1507 }
1508
1509 cinfo->handle = &info->handle;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001510 return 0;
1511}
1512
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001513static inline int
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301514scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001515{
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301516 int ret = scmi_chan_setup(info, dev, prot_id, true);
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001517
1518 if (!ret) /* Rx is optional, hence no error check */
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301519 scmi_chan_setup(info, dev, prot_id, false);
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001520
1521 return ret;
1522}
1523
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001524/**
1525 * scmi_get_protocol_device - Helper to get/create an SCMI device.
1526 *
1527 * @np: A device node representing a valid active protocols for the referred
1528 * SCMI instance.
1529 * @info: The referred SCMI instance for which we are getting/creating this
1530 * device.
1531 * @prot_id: The protocol ID.
1532 * @name: The device name.
1533 *
1534 * Referring to the specific SCMI instance identified by @info, this helper
1535 * takes care to return a properly initialized device matching the requested
1536 * @proto_id and @name: if device was still not existent it is created as a
1537 * child of the specified SCMI instance @info and its transport properly
1538 * initialized as usual.
Cristian Marussib98cf552021-07-12 15:35:04 +01001539 *
1540 * Return: A properly initialized scmi device, NULL otherwise.
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001541 */
1542static inline struct scmi_device *
1543scmi_get_protocol_device(struct device_node *np, struct scmi_info *info,
1544 int prot_id, const char *name)
1545{
1546 struct scmi_device *sdev;
1547
1548 /* Already created for this parent SCMI instance ? */
1549 sdev = scmi_child_dev_find(info->dev, prot_id, name);
1550 if (sdev)
1551 return sdev;
1552
1553 pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id);
1554
1555 sdev = scmi_device_create(np, info->dev, prot_id, name);
1556 if (!sdev) {
1557 dev_err(info->dev, "failed to create %d protocol device\n",
1558 prot_id);
1559 return NULL;
1560 }
1561
1562 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
1563 dev_err(&sdev->dev, "failed to setup transport\n");
1564 scmi_device_destroy(sdev);
1565 return NULL;
1566 }
1567
1568 return sdev;
1569}
1570
Sudeep Hollabc400812017-06-06 11:39:08 +01001571static inline void
1572scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
Sudeep Hollaee7a9c92018-12-21 18:08:08 +00001573 int prot_id, const char *name)
Sudeep Hollabc400812017-06-06 11:39:08 +01001574{
1575 struct scmi_device *sdev;
1576
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001577 sdev = scmi_get_protocol_device(np, info, prot_id, name);
1578 if (!sdev)
Sudeep Hollabc400812017-06-06 11:39:08 +01001579 return;
Sudeep Holla907b6d12017-07-31 15:43:27 +01001580
Sudeep Hollabc400812017-06-06 11:39:08 +01001581 /* setup handle now as the transport is ready */
1582 scmi_set_handle(sdev);
1583}
1584
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001585/**
1586 * scmi_create_protocol_devices - Create devices for all pending requests for
1587 * this SCMI instance.
1588 *
1589 * @np: The device node describing the protocol
1590 * @info: The SCMI instance descriptor
1591 * @prot_id: The protocol ID
1592 *
1593 * All devices previously requested for this instance (if any) are found and
1594 * created by scanning the proper @&scmi_requested_devices entry.
1595 */
1596static void scmi_create_protocol_devices(struct device_node *np,
1597 struct scmi_info *info, int prot_id)
Sudeep Holla9c5c4632019-11-06 15:17:26 +00001598{
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001599 struct list_head *phead;
Sudeep Holla9c5c4632019-11-06 15:17:26 +00001600
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001601 mutex_lock(&scmi_requested_devices_mtx);
1602 phead = idr_find(&scmi_requested_devices, prot_id);
1603 if (phead) {
1604 struct scmi_requested_dev *rdev;
Sudeep Holla9c5c4632019-11-06 15:17:26 +00001605
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001606 list_for_each_entry(rdev, phead, node)
1607 scmi_create_protocol_device(np, info, prot_id,
1608 rdev->id_table->name);
1609 }
1610 mutex_unlock(&scmi_requested_devices_mtx);
1611}
Sudeep Holla9c5c4632019-11-06 15:17:26 +00001612
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001613/**
1614 * scmi_protocol_device_request - Helper to request a device
1615 *
1616 * @id_table: A protocol/name pair descriptor for the device to be created.
1617 *
1618 * This helper let an SCMI driver request specific devices identified by the
1619 * @id_table to be created for each active SCMI instance.
1620 *
1621 * The requested device name MUST NOT be already existent for any protocol;
1622 * at first the freshly requested @id_table is annotated in the IDR table
1623 * @scmi_requested_devices, then a matching device is created for each already
1624 * active SCMI instance. (if any)
1625 *
1626 * This way the requested device is created straight-away for all the already
1627 * initialized(probed) SCMI instances (handles) and it remains also annotated
1628 * as pending creation if the requesting SCMI driver was loaded before some
1629 * SCMI instance and related transports were available: when such late instance
1630 * is probed, its probe will take care to scan the list of pending requested
1631 * devices and create those on its own (see @scmi_create_protocol_devices and
1632 * its enclosing loop)
1633 *
1634 * Return: 0 on Success
1635 */
1636int scmi_protocol_device_request(const struct scmi_device_id *id_table)
1637{
1638 int ret = 0;
1639 unsigned int id = 0;
1640 struct list_head *head, *phead = NULL;
1641 struct scmi_requested_dev *rdev;
1642 struct scmi_info *info;
1643
1644 pr_debug("Requesting SCMI device (%s) for protocol %x\n",
1645 id_table->name, id_table->protocol_id);
1646
1647 /*
1648 * Search for the matching protocol rdev list and then search
1649 * of any existent equally named device...fails if any duplicate found.
1650 */
1651 mutex_lock(&scmi_requested_devices_mtx);
1652 idr_for_each_entry(&scmi_requested_devices, head, id) {
1653 if (!phead) {
1654 /* A list found registered in the IDR is never empty */
1655 rdev = list_first_entry(head, struct scmi_requested_dev,
1656 node);
1657 if (rdev->id_table->protocol_id ==
1658 id_table->protocol_id)
1659 phead = head;
1660 }
1661 list_for_each_entry(rdev, head, node) {
1662 if (!strcmp(rdev->id_table->name, id_table->name)) {
1663 pr_err("Ignoring duplicate request [%d] %s\n",
1664 rdev->id_table->protocol_id,
1665 rdev->id_table->name);
1666 ret = -EINVAL;
1667 goto out;
1668 }
Sudeep Holla9c5c4632019-11-06 15:17:26 +00001669 }
1670 }
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001671
1672 /*
1673 * No duplicate found for requested id_table, so let's create a new
1674 * requested device entry for this new valid request.
1675 */
1676 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1677 if (!rdev) {
1678 ret = -ENOMEM;
1679 goto out;
1680 }
1681 rdev->id_table = id_table;
1682
1683 /*
1684 * Append the new requested device table descriptor to the head of the
1685 * related protocol list, eventually creating such head if not already
1686 * there.
1687 */
1688 if (!phead) {
1689 phead = kzalloc(sizeof(*phead), GFP_KERNEL);
1690 if (!phead) {
1691 kfree(rdev);
1692 ret = -ENOMEM;
1693 goto out;
1694 }
1695 INIT_LIST_HEAD(phead);
1696
1697 ret = idr_alloc(&scmi_requested_devices, (void *)phead,
1698 id_table->protocol_id,
1699 id_table->protocol_id + 1, GFP_KERNEL);
1700 if (ret != id_table->protocol_id) {
1701 pr_err("Failed to save SCMI device - ret:%d\n", ret);
1702 kfree(rdev);
1703 kfree(phead);
1704 ret = -EINVAL;
1705 goto out;
1706 }
1707 ret = 0;
1708 }
1709 list_add(&rdev->node, phead);
1710
1711 /*
1712 * Now effectively create and initialize the requested device for every
1713 * already initialized SCMI instance which has registered the requested
1714 * protocol as a valid active one: i.e. defined in DT and supported by
1715 * current platform FW.
1716 */
1717 mutex_lock(&scmi_list_mutex);
1718 list_for_each_entry(info, &scmi_list, node) {
1719 struct device_node *child;
1720
1721 child = idr_find(&info->active_protocols,
1722 id_table->protocol_id);
1723 if (child) {
1724 struct scmi_device *sdev;
1725
1726 sdev = scmi_get_protocol_device(child, info,
1727 id_table->protocol_id,
1728 id_table->name);
1729 /* Set handle if not already set: device existed */
1730 if (sdev && !sdev->handle)
1731 sdev->handle =
1732 scmi_handle_get_from_info_unlocked(info);
1733 } else {
1734 dev_err(info->dev,
1735 "Failed. SCMI protocol %d not active.\n",
1736 id_table->protocol_id);
1737 }
1738 }
1739 mutex_unlock(&scmi_list_mutex);
1740
1741out:
1742 mutex_unlock(&scmi_requested_devices_mtx);
1743
1744 return ret;
1745}
1746
1747/**
1748 * scmi_protocol_device_unrequest - Helper to unrequest a device
1749 *
1750 * @id_table: A protocol/name pair descriptor for the device to be unrequested.
1751 *
1752 * An helper to let an SCMI driver release its request about devices; note that
1753 * devices are created and initialized once the first SCMI driver request them
1754 * but they destroyed only on SCMI core unloading/unbinding.
1755 *
1756 * The current SCMI transport layer uses such devices as internal references and
1757 * as such they could be shared as same transport between multiple drivers so
1758 * that cannot be safely destroyed till the whole SCMI stack is removed.
1759 * (unless adding further burden of refcounting.)
1760 */
1761void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
1762{
1763 struct list_head *phead;
1764
1765 pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
1766 id_table->name, id_table->protocol_id);
1767
1768 mutex_lock(&scmi_requested_devices_mtx);
1769 phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
1770 if (phead) {
1771 struct scmi_requested_dev *victim, *tmp;
1772
1773 list_for_each_entry_safe(victim, tmp, phead, node) {
1774 if (!strcmp(victim->id_table->name, id_table->name)) {
1775 list_del(&victim->node);
1776 kfree(victim);
1777 break;
1778 }
1779 }
1780
1781 if (list_empty(phead)) {
1782 idr_remove(&scmi_requested_devices,
1783 id_table->protocol_id);
1784 kfree(phead);
1785 }
1786 }
1787 mutex_unlock(&scmi_requested_devices_mtx);
Sudeep Holla9c5c4632019-11-06 15:17:26 +00001788}
1789
Rishabh Bhatnagar1e7cbfa2021-08-04 14:19:59 -07001790static int scmi_cleanup_txrx_channels(struct scmi_info *info)
1791{
1792 int ret;
1793 struct idr *idr = &info->tx_idr;
1794
1795 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1796 idr_destroy(&info->tx_idr);
1797
1798 idr = &info->rx_idr;
1799 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1800 idr_destroy(&info->rx_idr);
1801
1802 return ret;
1803}
1804
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001805static int scmi_probe(struct platform_device *pdev)
1806{
1807 int ret;
1808 struct scmi_handle *handle;
1809 const struct scmi_desc *desc;
1810 struct scmi_info *info;
1811 struct device *dev = &pdev->dev;
Sudeep Hollabc400812017-06-06 11:39:08 +01001812 struct device_node *child, *np = dev->of_node;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001813
Aditya Pakkid9350f22019-03-22 16:55:03 -05001814 desc = of_device_get_match_data(dev);
1815 if (!desc)
1816 return -EINVAL;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001817
1818 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1819 if (!info)
1820 return -ENOMEM;
1821
1822 info->dev = dev;
1823 info->desc = desc;
1824 INIT_LIST_HEAD(&info->node);
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001825 idr_init(&info->protocols);
1826 mutex_init(&info->protocols_mtx);
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001827 idr_init(&info->active_protocols);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001828
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001829 platform_set_drvdata(pdev, info);
Sudeep Holla907b6d12017-07-31 15:43:27 +01001830 idr_init(&info->tx_idr);
Sudeep Holla46cc7c22019-07-08 09:40:46 +01001831 idr_init(&info->rx_idr);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001832
1833 handle = &info->handle;
1834 handle->dev = info->dev;
Sudeep Hollab6f20ff2017-06-06 11:16:15 +01001835 handle->version = &info->version;
Cristian Marussi23934ef2021-03-16 12:48:28 +00001836 handle->devm_protocol_get = scmi_devm_protocol_get;
1837 handle->devm_protocol_put = scmi_devm_protocol_put;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001838
Peter Hilber78852812021-08-03 14:10:21 +01001839 if (desc->ops->link_supplier) {
1840 ret = desc->ops->link_supplier(dev);
1841 if (ret)
1842 return ret;
1843 }
1844
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301845 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001846 if (ret)
1847 return ret;
1848
Sudeep Holla4ebd8f62020-03-27 14:34:26 +00001849 ret = scmi_xfer_info_init(info);
1850 if (ret)
Rishabh Bhatnagar1e7cbfa2021-08-04 14:19:59 -07001851 goto clear_txrx_setup;
Sudeep Holla4ebd8f62020-03-27 14:34:26 +00001852
Cristian Marussi6b8a6912020-07-01 16:53:43 +01001853 if (scmi_notification_init(handle))
1854 dev_err(dev, "SCMI Notifications NOT available.\n");
1855
Cristian Marussi8d3581c2021-03-16 12:48:35 +00001856 /*
1857 * Trigger SCMI Base protocol initialization.
1858 * It's mandatory and won't be ever released/deinit until the
1859 * SCMI stack is shutdown/unloaded as a whole.
1860 */
1861 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
Sudeep Hollab6f20ff2017-06-06 11:16:15 +01001862 if (ret) {
Cristian Marussi8d3581c2021-03-16 12:48:35 +00001863 dev_err(dev, "unable to communicate with SCMI\n");
Rishabh Bhatnagar1e7cbfa2021-08-04 14:19:59 -07001864 goto notification_exit;
Sudeep Hollab6f20ff2017-06-06 11:16:15 +01001865 }
1866
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001867 mutex_lock(&scmi_list_mutex);
1868 list_add_tail(&info->node, &scmi_list);
1869 mutex_unlock(&scmi_list_mutex);
1870
Sudeep Hollabc400812017-06-06 11:39:08 +01001871 for_each_available_child_of_node(np, child) {
1872 u32 prot_id;
1873
1874 if (of_property_read_u32(child, "reg", &prot_id))
1875 continue;
1876
Sudeep Holla354b2e32018-05-09 17:52:06 +01001877 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
1878 dev_err(dev, "Out of range protocol %d\n", prot_id);
Sudeep Hollabc400812017-06-06 11:39:08 +01001879
1880 if (!scmi_is_protocol_implemented(handle, prot_id)) {
1881 dev_err(dev, "SCMI protocol %d not implemented\n",
1882 prot_id);
1883 continue;
1884 }
1885
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001886 /*
1887 * Save this valid DT protocol descriptor amongst
1888 * @active_protocols for this SCMI instance/
1889 */
1890 ret = idr_alloc(&info->active_protocols, child,
1891 prot_id, prot_id + 1, GFP_KERNEL);
1892 if (ret != prot_id) {
1893 dev_err(dev, "SCMI protocol %d already activated. Skip\n",
1894 prot_id);
1895 continue;
1896 }
1897
1898 of_node_get(child);
Sudeep Holla9c5c4632019-11-06 15:17:26 +00001899 scmi_create_protocol_devices(child, info, prot_id);
Sudeep Hollabc400812017-06-06 11:39:08 +01001900 }
1901
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001902 return 0;
Rishabh Bhatnagar1e7cbfa2021-08-04 14:19:59 -07001903
1904notification_exit:
1905 scmi_notification_exit(&info->handle);
1906clear_txrx_setup:
1907 scmi_cleanup_txrx_channels(info);
1908 return ret;
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01001909}
1910
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301911void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
Sudeep Holla2747a962019-07-08 09:40:42 +01001912{
Sudeep Holla2747a962019-07-08 09:40:42 +01001913 idr_remove(idr, id);
Sudeep Holla2747a962019-07-08 09:40:42 +01001914}
1915
1916static int scmi_remove(struct platform_device *pdev)
1917{
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001918 int ret = 0, id;
Sudeep Holla2747a962019-07-08 09:40:42 +01001919 struct scmi_info *info = platform_get_drvdata(pdev);
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001920 struct device_node *child;
Sudeep Holla2747a962019-07-08 09:40:42 +01001921
1922 mutex_lock(&scmi_list_mutex);
1923 if (info->users)
1924 ret = -EBUSY;
1925 else
1926 list_del(&info->node);
1927 mutex_unlock(&scmi_list_mutex);
1928
1929 if (ret)
1930 return ret;
1931
Cristian Marussia90b6542021-01-12 19:13:26 +00001932 scmi_notification_exit(&info->handle);
1933
Cristian Marussi48dc16e2021-03-16 12:48:26 +00001934 mutex_lock(&info->protocols_mtx);
1935 idr_destroy(&info->protocols);
1936 mutex_unlock(&info->protocols_mtx);
1937
Cristian Marussid4f9ddd2021-03-16 12:49:03 +00001938 idr_for_each_entry(&info->active_protocols, child, id)
1939 of_node_put(child);
1940 idr_destroy(&info->active_protocols);
1941
Sudeep Holla2747a962019-07-08 09:40:42 +01001942 /* Safe to free channels since no more users */
Rishabh Bhatnagar1e7cbfa2021-08-04 14:19:59 -07001943 return scmi_cleanup_txrx_channels(info);
Sudeep Holla2747a962019-07-08 09:40:42 +01001944}
1945
Sudeep Holla4605e222019-10-22 10:09:01 +01001946static ssize_t protocol_version_show(struct device *dev,
1947 struct device_attribute *attr, char *buf)
1948{
1949 struct scmi_info *info = dev_get_drvdata(dev);
1950
1951 return sprintf(buf, "%u.%u\n", info->version.major_ver,
1952 info->version.minor_ver);
1953}
1954static DEVICE_ATTR_RO(protocol_version);
1955
1956static ssize_t firmware_version_show(struct device *dev,
1957 struct device_attribute *attr, char *buf)
1958{
1959 struct scmi_info *info = dev_get_drvdata(dev);
1960
1961 return sprintf(buf, "0x%x\n", info->version.impl_ver);
1962}
1963static DEVICE_ATTR_RO(firmware_version);
1964
1965static ssize_t vendor_id_show(struct device *dev,
1966 struct device_attribute *attr, char *buf)
1967{
1968 struct scmi_info *info = dev_get_drvdata(dev);
1969
1970 return sprintf(buf, "%s\n", info->version.vendor_id);
1971}
1972static DEVICE_ATTR_RO(vendor_id);
1973
1974static ssize_t sub_vendor_id_show(struct device *dev,
1975 struct device_attribute *attr, char *buf)
1976{
1977 struct scmi_info *info = dev_get_drvdata(dev);
1978
1979 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
1980}
1981static DEVICE_ATTR_RO(sub_vendor_id);
1982
1983static struct attribute *versions_attrs[] = {
1984 &dev_attr_firmware_version.attr,
1985 &dev_attr_protocol_version.attr,
1986 &dev_attr_vendor_id.attr,
1987 &dev_attr_sub_vendor_id.attr,
1988 NULL,
1989};
1990ATTRIBUTE_GROUPS(versions);
1991
Sudeep Holla2747a962019-07-08 09:40:42 +01001992/* Each compatible listed below must have descriptor associated with it */
1993static const struct of_device_id scmi_of_match[] = {
Cristian Marussie8419c22021-08-03 14:10:17 +01001994#ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
Viresh Kumar5c8a47a2020-01-31 10:58:13 +05301995 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
Sudeep Hollaab7766b2021-06-03 08:26:31 +01001996#endif
Cristian Marussie8419c22021-08-03 14:10:17 +01001997#ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
Peng Fan1dc65582020-03-08 21:24:39 +08001998 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
1999#endif
Igor Skalkin46abe132021-08-03 14:10:24 +01002000#ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
2001 { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
2002#endif
Sudeep Holla2747a962019-07-08 09:40:42 +01002003 { /* Sentinel */ },
2004};
2005
2006MODULE_DEVICE_TABLE(of, scmi_of_match);
2007
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01002008static struct platform_driver scmi_driver = {
2009 .driver = {
2010 .name = "arm-scmi",
2011 .of_match_table = scmi_of_match,
Sudeep Holla4605e222019-10-22 10:09:01 +01002012 .dev_groups = versions_groups,
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01002013 },
2014 .probe = scmi_probe,
2015 .remove = scmi_remove,
2016};
2017
Cristian Marussiceac2572021-08-03 14:10:12 +01002018/**
2019 * __scmi_transports_setup - Common helper to call transport-specific
2020 * .init/.exit code if provided.
2021 *
2022 * @init: A flag to distinguish between init and exit.
2023 *
2024 * Note that, if provided, we invoke .init/.exit functions for all the
2025 * transports currently compiled in.
2026 *
2027 * Return: 0 on Success.
2028 */
2029static inline int __scmi_transports_setup(bool init)
2030{
2031 int ret = 0;
2032 const struct of_device_id *trans;
2033
2034 for (trans = scmi_of_match; trans->data; trans++) {
2035 const struct scmi_desc *tdesc = trans->data;
2036
2037 if ((init && !tdesc->transport_init) ||
2038 (!init && !tdesc->transport_exit))
2039 continue;
2040
2041 if (init)
2042 ret = tdesc->transport_init();
2043 else
2044 tdesc->transport_exit();
2045
2046 if (ret) {
2047 pr_err("SCMI transport %s FAILED initialization!\n",
2048 trans->compatible);
2049 break;
2050 }
2051 }
2052
2053 return ret;
2054}
2055
2056static int __init scmi_transports_init(void)
2057{
2058 return __scmi_transports_setup(true);
2059}
2060
2061static void __exit scmi_transports_exit(void)
2062{
2063 __scmi_transports_setup(false);
2064}
2065
Sudeep Holla5a2f0a02020-09-07 12:00:04 +01002066static int __init scmi_driver_init(void)
2067{
Cristian Marussiceac2572021-08-03 14:10:12 +01002068 int ret;
2069
Cristian Marussic0397c82021-08-09 10:22:45 +01002070 /* Bail out if no SCMI transport was configured */
2071 if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
2072 return -EINVAL;
Sudeep Holla5a2f0a02020-09-07 12:00:04 +01002073
Cristian Marussic0397c82021-08-09 10:22:45 +01002074 scmi_bus_init();
Cristian Marussie8419c22021-08-03 14:10:17 +01002075
Cristian Marussiceac2572021-08-03 14:10:12 +01002076 /* Initialize any compiled-in transport which provided an init/exit */
2077 ret = scmi_transports_init();
2078 if (ret)
2079 return ret;
2080
Cristian Marussi48dc16e2021-03-16 12:48:26 +00002081 scmi_base_register();
2082
Sudeep Holla1eaf18e2020-09-07 12:06:01 +01002083 scmi_clock_register();
2084 scmi_perf_register();
2085 scmi_power_register();
2086 scmi_reset_register();
2087 scmi_sensors_register();
Cristian Marussi2add5ca2020-11-19 19:10:47 +00002088 scmi_voltage_register();
Sudeep Holla1eaf18e2020-09-07 12:06:01 +01002089 scmi_system_register();
2090
Sudeep Holla5a2f0a02020-09-07 12:00:04 +01002091 return platform_driver_register(&scmi_driver);
2092}
Sudeep Holla1eaf18e2020-09-07 12:06:01 +01002093subsys_initcall(scmi_driver_init);
Sudeep Holla5a2f0a02020-09-07 12:00:04 +01002094
2095static void __exit scmi_driver_exit(void)
2096{
Cristian Marussi48dc16e2021-03-16 12:48:26 +00002097 scmi_base_unregister();
Sudeep Holla5a2f0a02020-09-07 12:00:04 +01002098
Sudeep Holla1eaf18e2020-09-07 12:06:01 +01002099 scmi_clock_unregister();
2100 scmi_perf_unregister();
2101 scmi_power_unregister();
2102 scmi_reset_unregister();
2103 scmi_sensors_unregister();
Cristian Marussi2add5ca2020-11-19 19:10:47 +00002104 scmi_voltage_unregister();
Sudeep Holla1eaf18e2020-09-07 12:06:01 +01002105 scmi_system_unregister();
2106
Cristian Marussi48dc16e2021-03-16 12:48:26 +00002107 scmi_bus_exit();
2108
Cristian Marussiceac2572021-08-03 14:10:12 +01002109 scmi_transports_exit();
2110
Sudeep Holla5a2f0a02020-09-07 12:00:04 +01002111 platform_driver_unregister(&scmi_driver);
2112}
2113module_exit(scmi_driver_exit);
Sudeep Hollaaa4f8862017-03-28 11:36:07 +01002114
2115MODULE_ALIAS("platform: arm-scmi");
2116MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
2117MODULE_DESCRIPTION("ARM SCMI protocol driver");
2118MODULE_LICENSE("GPL v2");