firmware: arm_scmi: Use new SCMI full message tracing
[linux-block.git] / drivers / firmware / arm_scmi / driver.c
CommitLineData
aa4f886f
SH
1// 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 *
48dc16e2 14 * Copyright (C) 2018-2021 ARM Ltd.
aa4f886f
SH
15 */
16
17#include <linux/bitmap.h>
23934efe 18#include <linux/device.h>
aa4f886f 19#include <linux/export.h>
48dc16e2 20#include <linux/idr.h>
aa4f886f
SH
21#include <linux/io.h>
22#include <linux/kernel.h>
d4c3751a 23#include <linux/ktime.h>
9ca5a183 24#include <linux/hashtable.h>
d4f9dddd 25#include <linux/list.h>
aa4f886f
SH
26#include <linux/module.h>
27#include <linux/of_address.h>
28#include <linux/of_device.h>
d4c3751a 29#include <linux/processor.h>
48dc16e2 30#include <linux/refcount.h>
aa4f886f
SH
31#include <linux/slab.h>
32
33#include "common.h"
6b8a6913 34#include "notify.h"
aa4f886f 35
729d3530
LL
36#define CREATE_TRACE_POINTS
37#include <trace/events/scmi.h>
38
aa4f886f
SH
39enum 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 */
aa4f886f
SH
51};
52
1baf47c2 53/* List of all SCMI devices active in system */
aa4f886f
SH
54static LIST_HEAD(scmi_list);
55/* Protection for the entire list */
56static DEFINE_MUTEX(scmi_list_mutex);
729d3530
LL
57/* Track the unique id for the transfers for debug & profiling purpose */
58static atomic_t transfer_last_id;
aa4f886f 59
d4f9dddd
CM
60static 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
aa4f886f
SH
68/**
69 * struct scmi_xfers_info - Structure to manage transfer information
70 *
aa4f886f
SH
71 * @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
c92c3e38 75 * @max_msg: Maximum number of messages that can be pending
9ca5a183
CM
76 * @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.
aa4f886f
SH
81 */
82struct scmi_xfers_info {
aa4f886f 83 unsigned long *xfer_alloc_table;
aa4f886f 84 spinlock_t xfer_lock;
c92c3e38 85 int max_msg;
9ca5a183
CM
86 struct hlist_head free_xfers;
87 DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
aa4f886f
SH
88};
89
48dc16e2
CM
90/**
91 * struct scmi_protocol_instance - Describe an initialized protocol instance.
d7b6cc56 92 * @handle: Reference to the SCMI handle associated to this protocol instance.
48dc16e2
CM
93 * @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.
d7b6cc56
CM
96 * @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.
48dc16e2
CM
99 *
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 {
d7b6cc56 104 const struct scmi_handle *handle;
48dc16e2
CM
105 const struct scmi_protocol *proto;
106 void *gid;
107 refcount_t users;
d7b6cc56
CM
108 void *priv;
109 struct scmi_protocol_handle ph;
48dc16e2
CM
110};
111
d7b6cc56
CM
112#define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
113
aa4f886f 114/**
1baf47c2 115 * struct scmi_info - Structure representing a SCMI instance
aa4f886f
SH
116 *
117 * @dev: Device pointer
118 * @desc: SoC description for this instance
b6f20ff8
SH
119 * @version: SCMI revision information containing protocol version,
120 * implementation version and (sub-)vendor identification.
71af05a7 121 * @handle: Instance of SCMI handle to send to clients
38c927fb 122 * @tx_minfo: Universal Transmit Message management info
4ebd8f6d 123 * @rx_minfo: Universal Receive Message management info
3748daf7 124 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
46cc7c28 125 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
48dc16e2
CM
126 * @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.
1baf47c2 130 * @protocols_imp: List of protocols implemented, currently maximum of
776b6c8a
CM
131 * scmi_revision_info.num_protocols elements allocated by the
132 * base protocol
d4f9dddd
CM
133 * @active_protocols: IDR storing device_nodes for protocols actually defined
134 * in the DT and confirmed as implemented by fw.
05976c5f
CM
135 * @atomic_threshold: Optional system wide DT-configured threshold, expressed
136 * in microseconds, for atomic operations.
137 * Only SCMI synchronous commands reported by the platform
138 * to have an execution latency lesser-equal to the threshold
139 * should be considered for atomic mode operation: such
140 * decision is finally left up to the SCMI drivers.
a02d7c93 141 * @notify_priv: Pointer to private data structure specific to notifications.
1baf47c2 142 * @node: List head
aa4f886f
SH
143 * @users: Number of users of this instance
144 */
145struct scmi_info {
146 struct device *dev;
147 const struct scmi_desc *desc;
b6f20ff8 148 struct scmi_revision_info version;
aa4f886f 149 struct scmi_handle handle;
38c927fb 150 struct scmi_xfers_info tx_minfo;
4ebd8f6d 151 struct scmi_xfers_info rx_minfo;
907b6d14 152 struct idr tx_idr;
46cc7c28 153 struct idr rx_idr;
48dc16e2
CM
154 struct idr protocols;
155 /* Ensure mutual exclusive access to protocols instance array */
156 struct mutex protocols_mtx;
b6f20ff8 157 u8 *protocols_imp;
d4f9dddd 158 struct idr active_protocols;
05976c5f 159 unsigned int atomic_threshold;
a02d7c93 160 void *notify_priv;
aa4f886f
SH
161 struct list_head node;
162 int users;
163};
164
aa4f886f
SH
165#define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
166
aa4f886f
SH
167static const int scmi_linux_errmap[] = {
168 /* better than switch case as long as return value is continuous */
169 0, /* SCMI_SUCCESS */
170 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
171 -EINVAL, /* SCMI_ERR_PARAM */
172 -EACCES, /* SCMI_ERR_ACCESS */
173 -ENOENT, /* SCMI_ERR_ENTRY */
174 -ERANGE, /* SCMI_ERR_RANGE */
175 -EBUSY, /* SCMI_ERR_BUSY */
176 -ECOMM, /* SCMI_ERR_COMMS */
177 -EIO, /* SCMI_ERR_GENERIC */
178 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
179 -EPROTO, /* SCMI_ERR_PROTOCOL */
180};
181
182static inline int scmi_to_linux_errno(int errno)
183{
7a691f16
SH
184 int err_idx = -errno;
185
186 if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
187 return scmi_linux_errmap[err_idx];
aa4f886f
SH
188 return -EIO;
189}
190
a02d7c93
CM
191void scmi_notification_instance_data_set(const struct scmi_handle *handle,
192 void *priv)
193{
194 struct scmi_info *info = handle_to_scmi_info(handle);
195
196 info->notify_priv = priv;
197 /* Ensure updated protocol private date are visible */
198 smp_wmb();
199}
200
201void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
202{
203 struct scmi_info *info = handle_to_scmi_info(handle);
204
205 /* Ensure protocols_private_data has been updated */
206 smp_rmb();
207 return info->notify_priv;
208}
209
9ca5a183
CM
210/**
211 * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
212 *
213 * @minfo: Pointer to Tx/Rx Message management info based on channel type
214 * @xfer: The xfer to act upon
215 *
216 * Pick the next unused monotonically increasing token and set it into
217 * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
218 * reuse of freshly completed or timed-out xfers, thus mitigating the risk
219 * of incorrect association of a late and expired xfer with a live in-flight
220 * transaction, both happening to re-use the same token identifier.
221 *
222 * Since platform is NOT required to answer our request in-order we should
223 * account for a few rare but possible scenarios:
224 *
225 * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
226 * using find_next_zero_bit() starting from candidate next_token bit
227 *
228 * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
229 * are plenty of free tokens at start, so try a second pass using
230 * find_next_zero_bit() and starting from 0.
231 *
232 * X = used in-flight
233 *
234 * Normal
235 * ------
236 *
237 * |- xfer_id picked
238 * -----------+----------------------------------------------------------
239 * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
240 * ----------------------------------------------------------------------
241 * ^
242 * |- next_token
243 *
244 * Out-of-order pending at start
245 * -----------------------------
246 *
247 * |- xfer_id picked, last_token fixed
248 * -----+----------------------------------------------------------------
249 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
250 * ----------------------------------------------------------------------
251 * ^
252 * |- next_token
253 *
254 *
255 * Out-of-order pending at end
256 * ---------------------------
257 *
258 * |- xfer_id picked, last_token fixed
259 * -----+----------------------------------------------------------------
260 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
261 * ----------------------------------------------------------------------
262 * ^
263 * |- next_token
264 *
265 * Context: Assumes to be called with @xfer_lock already acquired.
266 *
267 * Return: 0 on Success or error
268 */
269static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
270 struct scmi_xfer *xfer)
271{
272 unsigned long xfer_id, next_token;
273
274 /*
275 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
276 * using the pre-allocated transfer_id as a base.
277 * Note that the global transfer_id is shared across all message types
278 * so there could be holes in the allocated set of monotonic sequence
279 * numbers, but that is going to limit the effectiveness of the
280 * mitigation only in very rare limit conditions.
281 */
282 next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
283
284 /* Pick the next available xfer_id >= next_token */
285 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
286 MSG_TOKEN_MAX, next_token);
287 if (xfer_id == MSG_TOKEN_MAX) {
288 /*
289 * After heavily out-of-order responses, there are no free
290 * tokens ahead, but only at start of xfer_alloc_table so
291 * try again from the beginning.
292 */
293 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
294 MSG_TOKEN_MAX, 0);
295 /*
296 * Something is wrong if we got here since there can be a
297 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
298 * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
299 */
300 if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
301 return -ENOMEM;
302 }
303
304 /* Update +/- last_token accordingly if we skipped some hole */
305 if (xfer_id != next_token)
306 atomic_add((int)(xfer_id - next_token), &transfer_last_id);
307
308 /* Set in-flight */
309 set_bit(xfer_id, minfo->xfer_alloc_table);
310 xfer->hdr.seq = (u16)xfer_id;
311
312 return 0;
313}
314
315/**
316 * scmi_xfer_token_clear - Release the token
317 *
318 * @minfo: Pointer to Tx/Rx Message management info based on channel type
319 * @xfer: The xfer to act upon
320 */
321static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
322 struct scmi_xfer *xfer)
323{
324 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
325}
326
aa4f886f 327/**
1baf47c2 328 * scmi_xfer_get() - Allocate one message
aa4f886f 329 *
1baf47c2 330 * @handle: Pointer to SCMI entity handle
38c927fb 331 * @minfo: Pointer to Tx/Rx Message management info based on channel type
9ca5a183
CM
332 * @set_pending: If true a monotonic token is picked and the xfer is added to
333 * the pending hash table.
aa4f886f 334 *
5b65af8f 335 * Helper function which is used by various message functions that are
aa4f886f
SH
336 * exposed to clients of this driver for allocating a message traffic event.
337 *
9ca5a183
CM
338 * Picks an xfer from the free list @free_xfers (if any available) and, if
339 * required, sets a monotonically increasing token and stores the inflight xfer
340 * into the @pending_xfers hashtable for later retrieval.
341 *
342 * The successfully initialized xfer is refcounted.
343 *
344 * Context: Holds @xfer_lock while manipulating @xfer_alloc_table and
345 * @free_xfers.
aa4f886f
SH
346 *
347 * Return: 0 if all went fine, else corresponding error.
348 */
38c927fb 349static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
9ca5a183
CM
350 struct scmi_xfers_info *minfo,
351 bool set_pending)
aa4f886f 352{
9ca5a183
CM
353 int ret;
354 unsigned long flags;
aa4f886f 355 struct scmi_xfer *xfer;
aa4f886f 356
aa4f886f 357 spin_lock_irqsave(&minfo->xfer_lock, flags);
9ca5a183 358 if (hlist_empty(&minfo->free_xfers)) {
aa4f886f
SH
359 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
360 return ERR_PTR(-ENOMEM);
361 }
aa4f886f 362
9ca5a183
CM
363 /* grab an xfer from the free_list */
364 xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
365 hlist_del_init(&xfer->node);
aa4f886f 366
9ca5a183
CM
367 /*
368 * Allocate transfer_id early so that can be used also as base for
369 * monotonic sequence number generation if needed.
370 */
729d3530 371 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
aa4f886f 372
9ca5a183
CM
373 if (set_pending) {
374 /* Pick and set monotonic token */
375 ret = scmi_xfer_token_set(minfo, xfer);
376 if (!ret) {
377 hash_add(minfo->pending_xfers, &xfer->node,
378 xfer->hdr.seq);
379 xfer->pending = true;
380 } else {
381 dev_err(handle->dev,
382 "Failed to get monotonic token %d\n", ret);
383 hlist_add_head(&xfer->node, &minfo->free_xfers);
384 xfer = ERR_PTR(ret);
385 }
386 }
ed7c04c1
CM
387
388 if (!IS_ERR(xfer)) {
389 refcount_set(&xfer->users, 1);
390 atomic_set(&xfer->busy, SCMI_XFER_FREE);
391 }
9ca5a183
CM
392 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
393
aa4f886f
SH
394 return xfer;
395}
396
397/**
38c927fb 398 * __scmi_xfer_put() - Release a message
aa4f886f 399 *
38c927fb 400 * @minfo: Pointer to Tx/Rx Message management info based on channel type
1baf47c2 401 * @xfer: message that was reserved by scmi_xfer_get
aa4f886f 402 *
9ca5a183
CM
403 * After refcount check, possibly release an xfer, clearing the token slot,
404 * removing xfer from @pending_xfers and putting it back into free_xfers.
405 *
aa4f886f
SH
406 * This holds a spinlock to maintain integrity of internal data structures.
407 */
38c927fb
SH
408static void
409__scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
aa4f886f
SH
410{
411 unsigned long flags;
aa4f886f 412
aa4f886f 413 spin_lock_irqsave(&minfo->xfer_lock, flags);
ed7c04c1
CM
414 if (refcount_dec_and_test(&xfer->users)) {
415 if (xfer->pending) {
416 scmi_xfer_token_clear(minfo, xfer);
417 hash_del(&xfer->node);
418 xfer->pending = false;
419 }
420 hlist_add_head(&xfer->node, &minfo->free_xfers);
9ca5a183 421 }
aa4f886f
SH
422 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
423}
424
9ca5a183
CM
425/**
426 * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
427 *
428 * @minfo: Pointer to Tx/Rx Message management info based on channel type
429 * @xfer_id: Token ID to lookup in @pending_xfers
430 *
431 * Refcounting is untouched.
432 *
433 * Context: Assumes to be called with @xfer_lock already acquired.
434 *
435 * Return: A valid xfer on Success or error otherwise
436 */
437static struct scmi_xfer *
438scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
439{
440 struct scmi_xfer *xfer = NULL;
441
442 if (test_bit(xfer_id, minfo->xfer_alloc_table))
443 xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
444
445 return xfer ?: ERR_PTR(-EINVAL);
446}
447
ed7c04c1
CM
448/**
449 * scmi_msg_response_validate - Validate message type against state of related
450 * xfer
451 *
452 * @cinfo: A reference to the channel descriptor.
453 * @msg_type: Message type to check
454 * @xfer: A reference to the xfer to validate against @msg_type
455 *
456 * This function checks if @msg_type is congruent with the current state of
457 * a pending @xfer; if an asynchronous delayed response is received before the
458 * related synchronous response (Out-of-Order Delayed Response) the missing
459 * synchronous response is assumed to be OK and completed, carrying on with the
460 * Delayed Response: this is done to address the case in which the underlying
461 * SCMI transport can deliver such out-of-order responses.
462 *
463 * Context: Assumes to be called with xfer->lock already acquired.
464 *
465 * Return: 0 on Success, error otherwise
466 */
467static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
468 u8 msg_type,
469 struct scmi_xfer *xfer)
470{
471 /*
472 * Even if a response was indeed expected on this slot at this point,
473 * a buggy platform could wrongly reply feeding us an unexpected
474 * delayed response we're not prepared to handle: bail-out safely
475 * blaming firmware.
476 */
477 if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
478 dev_err(cinfo->dev,
479 "Delayed Response for %d not expected! Buggy F/W ?\n",
480 xfer->hdr.seq);
481 return -EINVAL;
482 }
483
484 switch (xfer->state) {
485 case SCMI_XFER_SENT_OK:
486 if (msg_type == MSG_TYPE_DELAYED_RESP) {
487 /*
488 * Delayed Response expected but delivered earlier.
489 * Assume message RESPONSE was OK and skip state.
490 */
491 xfer->hdr.status = SCMI_SUCCESS;
492 xfer->state = SCMI_XFER_RESP_OK;
493 complete(&xfer->done);
494 dev_warn(cinfo->dev,
495 "Received valid OoO Delayed Response for %d\n",
496 xfer->hdr.seq);
497 }
498 break;
499 case SCMI_XFER_RESP_OK:
500 if (msg_type != MSG_TYPE_DELAYED_RESP)
501 return -EINVAL;
502 break;
503 case SCMI_XFER_DRESP_OK:
504 /* No further message expected once in SCMI_XFER_DRESP_OK */
505 return -EINVAL;
506 }
507
508 return 0;
509}
510
511/**
512 * scmi_xfer_state_update - Update xfer state
513 *
514 * @xfer: A reference to the xfer to update
515 * @msg_type: Type of message being processed.
516 *
517 * Note that this message is assumed to have been already successfully validated
518 * by @scmi_msg_response_validate(), so here we just update the state.
519 *
520 * Context: Assumes to be called on an xfer exclusively acquired using the
521 * busy flag.
522 */
523static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
524{
525 xfer->hdr.type = msg_type;
526
527 /* Unknown command types were already discarded earlier */
528 if (xfer->hdr.type == MSG_TYPE_COMMAND)
529 xfer->state = SCMI_XFER_RESP_OK;
530 else
531 xfer->state = SCMI_XFER_DRESP_OK;
532}
533
534static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
535{
536 int ret;
537
538 ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
539
540 return ret == SCMI_XFER_FREE;
541}
542
543/**
544 * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
545 *
546 * @cinfo: A reference to the channel descriptor.
547 * @msg_hdr: A message header to use as lookup key
548 *
549 * When a valid xfer is found for the sequence number embedded in the provided
550 * msg_hdr, reference counting is properly updated and exclusive access to this
551 * xfer is granted till released with @scmi_xfer_command_release.
552 *
553 * Return: A valid @xfer on Success or error otherwise.
554 */
555static inline struct scmi_xfer *
556scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
557{
558 int ret;
559 unsigned long flags;
560 struct scmi_xfer *xfer;
561 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
562 struct scmi_xfers_info *minfo = &info->tx_minfo;
563 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
564 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
565
566 /* Are we even expecting this? */
567 spin_lock_irqsave(&minfo->xfer_lock, flags);
568 xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
569 if (IS_ERR(xfer)) {
570 dev_err(cinfo->dev,
571 "Message for %d type %d is not expected!\n",
572 xfer_id, msg_type);
573 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
574 return xfer;
575 }
576 refcount_inc(&xfer->users);
577 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
578
579 spin_lock_irqsave(&xfer->lock, flags);
580 ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
581 /*
582 * If a pending xfer was found which was also in a congruent state with
583 * the received message, acquire exclusive access to it setting the busy
584 * flag.
585 * Spins only on the rare limit condition of concurrent reception of
586 * RESP and DRESP for the same xfer.
587 */
588 if (!ret) {
589 spin_until_cond(scmi_xfer_acquired(xfer));
590 scmi_xfer_state_update(xfer, msg_type);
591 }
592 spin_unlock_irqrestore(&xfer->lock, flags);
593
594 if (ret) {
595 dev_err(cinfo->dev,
596 "Invalid message type:%d for %d - HDR:0x%X state:%d\n",
597 msg_type, xfer_id, msg_hdr, xfer->state);
598 /* On error the refcount incremented above has to be dropped */
599 __scmi_xfer_put(minfo, xfer);
600 xfer = ERR_PTR(-EINVAL);
601 }
602
603 return xfer;
604}
605
606static inline void scmi_xfer_command_release(struct scmi_info *info,
607 struct scmi_xfer *xfer)
608{
609 atomic_set(&xfer->busy, SCMI_XFER_FREE);
610 __scmi_xfer_put(&info->tx_minfo, xfer);
611}
612
e9b21c96
CM
613static inline void scmi_clear_channel(struct scmi_info *info,
614 struct scmi_chan_info *cinfo)
615{
616 if (info->desc->ops->clear_channel)
617 info->desc->ops->clear_channel(cinfo);
618}
619
a690b7e6
CM
620static inline bool is_polling_required(struct scmi_chan_info *cinfo,
621 struct scmi_info *info)
622{
623 return cinfo->no_completion_irq || info->desc->force_polling;
624}
625
626static inline bool is_transport_polling_capable(struct scmi_info *info)
627{
31d2f803
CM
628 return info->desc->ops->poll_done ||
629 info->desc->sync_cmds_completed_on_ret;
a690b7e6
CM
630}
631
632static inline bool is_polling_enabled(struct scmi_chan_info *cinfo,
633 struct scmi_info *info)
634{
635 return is_polling_required(cinfo, info) &&
636 is_transport_polling_capable(info);
637}
638
13fba878
CM
639static void scmi_handle_notification(struct scmi_chan_info *cinfo,
640 u32 msg_hdr, void *priv)
2747a967 641{
5c8a47a5 642 struct scmi_xfer *xfer;
4d09852b
SH
643 struct device *dev = cinfo->dev;
644 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
645 struct scmi_xfers_info *minfo = &info->rx_minfo;
72a5eb9d 646 ktime_t ts;
4d09852b 647
72a5eb9d 648 ts = ktime_get_boottime();
9ca5a183 649 xfer = scmi_xfer_get(cinfo->handle, minfo, false);
4d09852b
SH
650 if (IS_ERR(xfer)) {
651 dev_err(dev, "failed to get free message slot (%ld)\n",
652 PTR_ERR(xfer));
e9b21c96 653 scmi_clear_channel(info, cinfo);
4d09852b
SH
654 return;
655 }
656
657 unpack_scmi_header(msg_hdr, &xfer->hdr);
13fba878 658 if (priv)
5a3b7185
CM
659 /* Ensure order between xfer->priv store and following ops */
660 smp_store_mb(xfer->priv, priv);
4d09852b
SH
661 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
662 xfer);
b60e0886
CM
663
664 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id, "NOTI",
665 xfer->hdr.seq, xfer->hdr.status,
666 xfer->rx.buf, xfer->rx.len);
667
6b8a6913
CM
668 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
669 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
4d09852b
SH
670
671 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
672 xfer->hdr.protocol_id, xfer->hdr.seq,
673 MSG_TYPE_NOTIFICATION);
58ecdf03 674
4d09852b
SH
675 __scmi_xfer_put(minfo, xfer);
676
e9b21c96 677 scmi_clear_channel(info, cinfo);
4d09852b
SH
678}
679
13fba878
CM
680static void scmi_handle_response(struct scmi_chan_info *cinfo,
681 u32 msg_hdr, void *priv)
4d09852b
SH
682{
683 struct scmi_xfer *xfer;
4d09852b 684 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
2747a967 685
ed7c04c1 686 xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
9ca5a183 687 if (IS_ERR(xfer)) {
98f0d68f
CM
688 if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP)
689 scmi_clear_channel(info, cinfo);
c5bceb98
CM
690 return;
691 }
2747a967 692
0cb7af47 693 /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
ed7c04c1 694 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
0cb7af47
CM
695 xfer->rx.len = info->desc->max_msg_size;
696
13fba878 697 if (priv)
5a3b7185
CM
698 /* Ensure order between xfer->priv store and following ops */
699 smp_store_mb(xfer->priv, priv);
5c8a47a5 700 info->desc->ops->fetch_response(cinfo, xfer);
58ecdf03 701
b60e0886
CM
702 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id,
703 xfer->hdr.type == MSG_TYPE_DELAYED_RESP ?
704 "DLYD" : "RESP",
705 xfer->hdr.seq, xfer->hdr.status,
706 xfer->rx.buf, xfer->rx.len);
707
729d3530
LL
708 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
709 xfer->hdr.protocol_id, xfer->hdr.seq,
ed7c04c1 710 xfer->hdr.type);
729d3530 711
ed7c04c1 712 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
e9b21c96 713 scmi_clear_channel(info, cinfo);
58ecdf03 714 complete(xfer->async_done);
d04fb2b2 715 } else {
58ecdf03 716 complete(&xfer->done);
d04fb2b2 717 }
ed7c04c1
CM
718
719 scmi_xfer_command_release(info, xfer);
2747a967
SH
720}
721
4d09852b
SH
722/**
723 * scmi_rx_callback() - callback for receiving messages
724 *
725 * @cinfo: SCMI channel info
726 * @msg_hdr: Message header
13fba878 727 * @priv: Transport specific private data.
4d09852b
SH
728 *
729 * Processes one received message to appropriate transfer information and
730 * signals completion of the transfer.
731 *
732 * NOTE: This function will be invoked in IRQ context, hence should be
733 * as optimal as possible.
734 */
13fba878 735void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv)
4d09852b 736{
4d09852b
SH
737 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
738
739 switch (msg_type) {
740 case MSG_TYPE_NOTIFICATION:
13fba878 741 scmi_handle_notification(cinfo, msg_hdr, priv);
4d09852b
SH
742 break;
743 case MSG_TYPE_COMMAND:
744 case MSG_TYPE_DELAYED_RESP:
13fba878 745 scmi_handle_response(cinfo, msg_hdr, priv);
4d09852b
SH
746 break;
747 default:
748 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
749 break;
750 }
751}
752
38c927fb 753/**
a4a20b09 754 * xfer_put() - Release a transmit message
38c927fb 755 *
a4a20b09 756 * @ph: Pointer to SCMI protocol handle
ed7c04c1 757 * @xfer: message that was reserved by xfer_get_init
38c927fb 758 */
a4a20b09
CM
759static void xfer_put(const struct scmi_protocol_handle *ph,
760 struct scmi_xfer *xfer)
38c927fb 761{
a4a20b09
CM
762 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
763 struct scmi_info *info = handle_to_scmi_info(pi->handle);
38c927fb
SH
764
765 __scmi_xfer_put(&info->tx_minfo, xfer);
766}
767
5c8a47a5 768static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
d4c3751a
SH
769 struct scmi_xfer *xfer, ktime_t stop)
770{
5c8a47a5 771 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
d4c3751a 772
ed7c04c1
CM
773 /*
774 * Poll also on xfer->done so that polling can be forcibly terminated
775 * in case of out-of-order receptions of delayed responses
776 */
5c8a47a5 777 return info->desc->ops->poll_done(cinfo, xfer) ||
ed7c04c1 778 try_wait_for_completion(&xfer->done) ||
5c8a47a5 779 ktime_after(ktime_get(), stop);
d4c3751a
SH
780}
781
5a731aeb
CM
782/**
783 * scmi_wait_for_message_response - An helper to group all the possible ways of
784 * waiting for a synchronous message response.
785 *
786 * @cinfo: SCMI channel info
787 * @xfer: Reference to the transfer being waited for.
788 *
789 * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
790 * configuration flags like xfer->hdr.poll_completion.
791 *
792 * Return: 0 on Success, error otherwise.
793 */
794static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo,
795 struct scmi_xfer *xfer)
796{
797 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
798 struct device *dev = info->dev;
799 int ret = 0, timeout_ms = info->desc->max_rx_timeout_ms;
800
f872af09
CM
801 trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id,
802 xfer->hdr.protocol_id, xfer->hdr.seq,
803 timeout_ms,
804 xfer->hdr.poll_completion);
805
5a731aeb 806 if (xfer->hdr.poll_completion) {
31d2f803
CM
807 /*
808 * Real polling is needed only if transport has NOT declared
809 * itself to support synchronous commands replies.
810 */
811 if (!info->desc->sync_cmds_completed_on_ret) {
812 /*
813 * Poll on xfer using transport provided .poll_done();
814 * assumes no completion interrupt was available.
815 */
816 ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms);
817
818 spin_until_cond(scmi_xfer_done_no_timeout(cinfo,
819 xfer, stop));
820 if (ktime_after(ktime_get(), stop)) {
821 dev_err(dev,
822 "timed out in resp(caller: %pS) - polling\n",
823 (void *)_RET_IP_);
824 ret = -ETIMEDOUT;
825 }
826 }
5a731aeb 827
31d2f803 828 if (!ret) {
5a731aeb
CM
829 unsigned long flags;
830
831 /*
832 * Do not fetch_response if an out-of-order delayed
833 * response is being processed.
834 */
835 spin_lock_irqsave(&xfer->lock, flags);
836 if (xfer->state == SCMI_XFER_SENT_OK) {
837 info->desc->ops->fetch_response(cinfo, xfer);
838 xfer->state = SCMI_XFER_RESP_OK;
839 }
840 spin_unlock_irqrestore(&xfer->lock, flags);
b60e0886
CM
841
842 /* Trace polled replies. */
843 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id,
844 "RESP",
845 xfer->hdr.seq, xfer->hdr.status,
846 xfer->rx.buf, xfer->rx.len);
5a731aeb
CM
847 }
848 } else {
849 /* And we wait for the response. */
850 if (!wait_for_completion_timeout(&xfer->done,
851 msecs_to_jiffies(timeout_ms))) {
852 dev_err(dev, "timed out in resp(caller: %pS)\n",
853 (void *)_RET_IP_);
854 ret = -ETIMEDOUT;
855 }
856 }
857
858 return ret;
859}
860
aa4f886f 861/**
a4a20b09 862 * do_xfer() - Do one transfer
aa4f886f 863 *
a4a20b09 864 * @ph: Pointer to SCMI protocol handle
aa4f886f
SH
865 * @xfer: Transfer to initiate and wait for response
866 *
867 * Return: -ETIMEDOUT in case of no response, if transmit error,
1baf47c2
SH
868 * return corresponding error, else if all goes well,
869 * return 0.
aa4f886f 870 */
a4a20b09
CM
871static int do_xfer(const struct scmi_protocol_handle *ph,
872 struct scmi_xfer *xfer)
aa4f886f
SH
873{
874 int ret;
a4a20b09
CM
875 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
876 struct scmi_info *info = handle_to_scmi_info(pi->handle);
aa4f886f 877 struct device *dev = info->dev;
907b6d14
SH
878 struct scmi_chan_info *cinfo;
879
a690b7e6 880 /* Check for polling request on custom command xfers at first */
31d2f803 881 if (xfer->hdr.poll_completion && !is_transport_polling_capable(info)) {
2930abcf
CM
882 dev_warn_once(dev,
883 "Polling mode is not supported by transport.\n");
884 return -EINVAL;
885 }
886
d211ddeb
CM
887 cinfo = idr_find(&info->tx_idr, pi->proto->id);
888 if (unlikely(!cinfo))
889 return -EINVAL;
890
a690b7e6
CM
891 /* True ONLY if also supported by transport. */
892 if (is_polling_enabled(cinfo, info))
893 xfer->hdr.poll_completion = true;
894
a4a20b09 895 /*
61832b35 896 * Initialise protocol id now from protocol handle to avoid it being
a4a20b09 897 * overridden by mistake (or malice) by the protocol code mangling with
61832b35 898 * the scmi_xfer structure prior to this.
a4a20b09
CM
899 */
900 xfer->hdr.protocol_id = pi->proto->id;
e30d91d4 901 reinit_completion(&xfer->done);
a4a20b09 902
729d3530
LL
903 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
904 xfer->hdr.protocol_id, xfer->hdr.seq,
905 xfer->hdr.poll_completion);
906
ed7c04c1
CM
907 xfer->state = SCMI_XFER_SENT_OK;
908 /*
909 * Even though spinlocking is not needed here since no race is possible
910 * on xfer->state due to the monotonically increasing tokens allocation,
911 * we must anyway ensure xfer->state initialization is not re-ordered
912 * after the .send_message() to be sure that on the RX path an early
913 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
914 */
915 smp_mb();
916
5c8a47a5 917 ret = info->desc->ops->send_message(cinfo, xfer);
aa4f886f 918 if (ret < 0) {
5c8a47a5 919 dev_dbg(dev, "Failed to send message %d\n", ret);
aa4f886f
SH
920 return ret;
921 }
922
b60e0886
CM
923 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id, "CMND",
924 xfer->hdr.seq, xfer->hdr.status,
925 xfer->tx.buf, xfer->tx.len);
926
5a731aeb 927 ret = scmi_wait_for_message_response(cinfo, xfer);
d4c3751a
SH
928 if (!ret && xfer->hdr.status)
929 ret = scmi_to_linux_errno(xfer->hdr.status);
930
5c8a47a5 931 if (info->desc->ops->mark_txdone)
94d0cd1d 932 info->desc->ops->mark_txdone(cinfo, ret, xfer);
aa4f886f 933
729d3530 934 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
bad0d73b 935 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
729d3530 936
aa4f886f
SH
937 return ret;
938}
939
a4a20b09
CM
940static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
941 struct scmi_xfer *xfer)
942{
943 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
944 struct scmi_info *info = handle_to_scmi_info(pi->handle);
945
946 xfer->rx.len = info->desc->max_msg_size;
947}
948
58ecdf03
SH
949#define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
950
951/**
a4a20b09 952 * do_xfer_with_response() - Do one transfer and wait until the delayed
58ecdf03
SH
953 * response is received
954 *
a4a20b09 955 * @ph: Pointer to SCMI protocol handle
58ecdf03
SH
956 * @xfer: Transfer to initiate and wait for response
957 *
69255e74
CM
958 * Using asynchronous commands in atomic/polling mode should be avoided since
959 * it could cause long busy-waiting here, so ignore polling for the delayed
960 * response and WARN if it was requested for this command transaction since
961 * upper layers should refrain from issuing such kind of requests.
962 *
963 * The only other option would have been to refrain from using any asynchronous
964 * command even if made available, when an atomic transport is detected, and
965 * instead forcibly use the synchronous version (thing that can be easily
966 * attained at the protocol layer), but this would also have led to longer
967 * stalls of the channel for synchronous commands and possibly timeouts.
968 * (in other words there is usually a good reason if a platform provides an
969 * asynchronous version of a command and we should prefer to use it...just not
970 * when using atomic/polling mode)
971 *
58ecdf03
SH
972 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
973 * return corresponding error, else if all goes well, return 0.
974 */
a4a20b09
CM
975static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
976 struct scmi_xfer *xfer)
58ecdf03
SH
977{
978 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
979 DECLARE_COMPLETION_ONSTACK(async_response);
980
981 xfer->async_done = &async_response;
982
69255e74
CM
983 /*
984 * Delayed responses should not be polled, so an async command should
985 * not have been used when requiring an atomic/poll context; WARN and
986 * perform instead a sleeping wait.
987 * (Note Async + IgnoreDelayedResponses are sent via do_xfer)
988 */
989 WARN_ON_ONCE(xfer->hdr.poll_completion);
990
a4a20b09 991 ret = do_xfer(ph, xfer);
f1748b1e 992 if (!ret) {
69255e74
CM
993 if (!wait_for_completion_timeout(xfer->async_done, timeout)) {
994 dev_err(ph->dev,
995 "timed out in delayed resp(caller: %pS)\n",
996 (void *)_RET_IP_);
f1748b1e 997 ret = -ETIMEDOUT;
69255e74 998 } else if (xfer->hdr.status) {
f1748b1e 999 ret = scmi_to_linux_errno(xfer->hdr.status);
69255e74 1000 }
f1748b1e 1001 }
58ecdf03
SH
1002
1003 xfer->async_done = NULL;
1004 return ret;
1005}
1006
aa4f886f 1007/**
a4a20b09 1008 * xfer_get_init() - Allocate and initialise one message for transmit
aa4f886f 1009 *
a4a20b09 1010 * @ph: Pointer to SCMI protocol handle
aa4f886f 1011 * @msg_id: Message identifier
aa4f886f
SH
1012 * @tx_size: transmit message size
1013 * @rx_size: receive message size
1014 * @p: pointer to the allocated and initialised message
1015 *
14e297b3 1016 * This function allocates the message using @scmi_xfer_get and
aa4f886f
SH
1017 * initialise the header.
1018 *
1019 * Return: 0 if all went fine with @p pointing to message, else
1020 * corresponding error.
1021 */
a4a20b09
CM
1022static int xfer_get_init(const struct scmi_protocol_handle *ph,
1023 u8 msg_id, size_t tx_size, size_t rx_size,
1024 struct scmi_xfer **p)
aa4f886f
SH
1025{
1026 int ret;
1027 struct scmi_xfer *xfer;
a4a20b09
CM
1028 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1029 struct scmi_info *info = handle_to_scmi_info(pi->handle);
38c927fb 1030 struct scmi_xfers_info *minfo = &info->tx_minfo;
aa4f886f
SH
1031 struct device *dev = info->dev;
1032
1033 /* Ensure we have sane transfer sizes */
1034 if (rx_size > info->desc->max_msg_size ||
1035 tx_size > info->desc->max_msg_size)
1036 return -ERANGE;
1037
9ca5a183 1038 xfer = scmi_xfer_get(pi->handle, minfo, true);
aa4f886f
SH
1039 if (IS_ERR(xfer)) {
1040 ret = PTR_ERR(xfer);
1041 dev_err(dev, "failed to get free message slot(%d)\n", ret);
1042 return ret;
1043 }
1044
1045 xfer->tx.len = tx_size;
1046 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
63b282f1 1047 xfer->hdr.type = MSG_TYPE_COMMAND;
aa4f886f 1048 xfer->hdr.id = msg_id;
aa4f886f
SH
1049 xfer->hdr.poll_completion = false;
1050
1051 *p = xfer;
1baf47c2 1052
aa4f886f
SH
1053 return 0;
1054}
1055
b6f20ff8 1056/**
a4a20b09 1057 * version_get() - command to get the revision of the SCMI entity
b6f20ff8 1058 *
a4a20b09 1059 * @ph: Pointer to SCMI protocol handle
1baf47c2 1060 * @version: Holds returned version of protocol.
b6f20ff8
SH
1061 *
1062 * Updates the SCMI information in the internal data structure.
1063 *
1064 * Return: 0 if all went fine, else return appropriate error.
1065 */
a4a20b09 1066static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
b6f20ff8
SH
1067{
1068 int ret;
1069 __le32 *rev_info;
1070 struct scmi_xfer *t;
1071
a4a20b09 1072 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
b6f20ff8
SH
1073 if (ret)
1074 return ret;
1075
a4a20b09 1076 ret = do_xfer(ph, t);
b6f20ff8
SH
1077 if (!ret) {
1078 rev_info = t->rx.buf;
1079 *version = le32_to_cpu(*rev_info);
1080 }
1081
a4a20b09 1082 xfer_put(ph, t);
b6f20ff8
SH
1083 return ret;
1084}
1085
d7b6cc56
CM
1086/**
1087 * scmi_set_protocol_priv - Set protocol specific data at init time
1088 *
1089 * @ph: A reference to the protocol handle.
1090 * @priv: The private data to set.
1091 *
1092 * Return: 0 on Success
1093 */
1094static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
1095 void *priv)
1096{
1097 struct scmi_protocol_instance *pi = ph_to_pi(ph);
1098
1099 pi->priv = priv;
1100
1101 return 0;
1102}
1103
1104/**
1105 * scmi_get_protocol_priv - Set protocol specific data at init time
1106 *
1107 * @ph: A reference to the protocol handle.
1108 *
1109 * Return: Protocol private data if any was set.
1110 */
1111static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
1112{
1113 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1114
1115 return pi->priv;
1116}
1117
a4a20b09
CM
1118static const struct scmi_xfer_ops xfer_ops = {
1119 .version_get = version_get,
1120 .xfer_get_init = xfer_get_init,
1121 .reset_rx_to_maxsz = reset_rx_to_maxsz,
1122 .do_xfer = do_xfer,
1123 .do_xfer_with_response = do_xfer_with_response,
1124 .xfer_put = xfer_put,
1125};
1126
5c873d12
CM
1127struct scmi_msg_resp_domain_name_get {
1128 __le32 flags;
1129 u8 name[SCMI_MAX_STR_SIZE];
1130};
1131
1132/**
1133 * scmi_common_extended_name_get - Common helper to get extended resources name
1134 * @ph: A protocol handle reference.
1135 * @cmd_id: The specific command ID to use.
1136 * @res_id: The specific resource ID to use.
1137 * @name: A pointer to the preallocated area where the retrieved name will be
1138 * stored as a NULL terminated string.
1139 * @len: The len in bytes of the @name char array.
1140 *
1141 * Return: 0 on Succcess
1142 */
1143static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph,
1144 u8 cmd_id, u32 res_id, char *name,
1145 size_t len)
1146{
1147 int ret;
1148 struct scmi_xfer *t;
1149 struct scmi_msg_resp_domain_name_get *resp;
1150
1151 ret = ph->xops->xfer_get_init(ph, cmd_id, sizeof(res_id),
1152 sizeof(*resp), &t);
1153 if (ret)
1154 goto out;
1155
1156 put_unaligned_le32(res_id, t->tx.buf);
1157 resp = t->rx.buf;
1158
1159 ret = ph->xops->do_xfer(ph, t);
1160 if (!ret)
1161 strscpy(name, resp->name, len);
1162
1163 ph->xops->xfer_put(ph, t);
1164out:
1165 if (ret)
1166 dev_warn(ph->dev,
1167 "Failed to get extended name - id:%u (ret:%d). Using %s\n",
1168 res_id, ret, name);
1169 return ret;
1170}
1171
36b6ea0f
CM
1172/**
1173 * struct scmi_iterator - Iterator descriptor
1174 * @msg: A reference to the message TX buffer; filled by @prepare_message with
1175 * a proper custom command payload for each multi-part command request.
1176 * @resp: A reference to the response RX buffer; used by @update_state and
1177 * @process_response to parse the multi-part replies.
1178 * @t: A reference to the underlying xfer initialized and used transparently by
1179 * the iterator internal routines.
1180 * @ph: A reference to the associated protocol handle to be used.
1181 * @ops: A reference to the custom provided iterator operations.
1182 * @state: The current iterator state; used and updated in turn by the iterators
1183 * internal routines and by the caller-provided @scmi_iterator_ops.
1184 * @priv: A reference to optional private data as provided by the caller and
1185 * passed back to the @@scmi_iterator_ops.
1186 */
1187struct scmi_iterator {
1188 void *msg;
1189 void *resp;
1190 struct scmi_xfer *t;
1191 const struct scmi_protocol_handle *ph;
1192 struct scmi_iterator_ops *ops;
1193 struct scmi_iterator_state state;
1194 void *priv;
1195};
1196
1197static void *scmi_iterator_init(const struct scmi_protocol_handle *ph,
1198 struct scmi_iterator_ops *ops,
1199 unsigned int max_resources, u8 msg_id,
1200 size_t tx_size, void *priv)
1201{
1202 int ret;
1203 struct scmi_iterator *i;
1204
1205 i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL);
1206 if (!i)
1207 return ERR_PTR(-ENOMEM);
1208
1209 i->ph = ph;
1210 i->ops = ops;
1211 i->priv = priv;
1212
1213 ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t);
1214 if (ret) {
1215 devm_kfree(ph->dev, i);
1216 return ERR_PTR(ret);
1217 }
1218
1219 i->state.max_resources = max_resources;
1220 i->msg = i->t->tx.buf;
1221 i->resp = i->t->rx.buf;
1222
1223 return i;
1224}
1225
1226static int scmi_iterator_run(void *iter)
1227{
1228 int ret = -EINVAL;
c7f8852d
CM
1229 struct scmi_iterator_ops *iops;
1230 const struct scmi_protocol_handle *ph;
1231 struct scmi_iterator_state *st;
36b6ea0f 1232 struct scmi_iterator *i = iter;
36b6ea0f 1233
c7f8852d 1234 if (!i || !i->ops || !i->ph)
36b6ea0f
CM
1235 return ret;
1236
c7f8852d
CM
1237 iops = i->ops;
1238 ph = i->ph;
1239 st = &i->state;
1240
36b6ea0f
CM
1241 do {
1242 iops->prepare_message(i->msg, st->desc_index, i->priv);
c7f8852d 1243 ret = ph->xops->do_xfer(ph, i->t);
36b6ea0f
CM
1244 if (ret)
1245 break;
1246
754f04ca 1247 st->rx_len = i->t->rx.len;
36b6ea0f
CM
1248 ret = iops->update_state(st, i->resp, i->priv);
1249 if (ret)
1250 break;
1251
1252 if (st->num_returned > st->max_resources - st->desc_index) {
1253 dev_err(ph->dev,
1254 "No. of resources can't exceed %d\n",
1255 st->max_resources);
1256 ret = -EINVAL;
1257 break;
1258 }
1259
1260 for (st->loop_idx = 0; st->loop_idx < st->num_returned;
1261 st->loop_idx++) {
1262 ret = iops->process_response(ph, i->resp, st, i->priv);
1263 if (ret)
1264 goto out;
1265 }
1266
1267 st->desc_index += st->num_returned;
c7f8852d 1268 ph->xops->reset_rx_to_maxsz(ph, i->t);
36b6ea0f
CM
1269 /*
1270 * check for both returned and remaining to avoid infinite
1271 * loop due to buggy firmware
1272 */
1273 } while (st->num_returned && st->num_remaining);
1274
1275out:
1276 /* Finalize and destroy iterator */
c7f8852d 1277 ph->xops->xfer_put(ph, i->t);
36b6ea0f
CM
1278 devm_kfree(ph->dev, i);
1279
1280 return ret;
1281}
1282
5c873d12
CM
1283static const struct scmi_proto_helpers_ops helpers_ops = {
1284 .extended_name_get = scmi_common_extended_name_get,
36b6ea0f
CM
1285 .iter_response_init = scmi_iterator_init,
1286 .iter_response_run = scmi_iterator_run,
5c873d12
CM
1287};
1288
3d5d6e84
CM
1289/**
1290 * scmi_revision_area_get - Retrieve version memory area.
1291 *
1292 * @ph: A reference to the protocol handle.
1293 *
1294 * A helper to grab the version memory area reference during SCMI Base protocol
1295 * initialization.
1296 *
1297 * Return: A reference to the version memory area associated to the SCMI
1298 * instance underlying this protocol handle.
1299 */
1300struct scmi_revision_info *
1301scmi_revision_area_get(const struct scmi_protocol_handle *ph)
1302{
1303 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1304
1305 return pi->handle->version;
1306}
1307
48dc16e2
CM
1308/**
1309 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
1310 * instance descriptor.
1311 * @info: The reference to the related SCMI instance.
1312 * @proto: The protocol descriptor.
1313 *
1314 * Allocate a new protocol instance descriptor, using the provided @proto
1315 * description, against the specified SCMI instance @info, and initialize it;
1316 * all resources management is handled via a dedicated per-protocol devres
1317 * group.
1318 *
1319 * Context: Assumes to be called with @protocols_mtx already acquired.
1320 * Return: A reference to a freshly allocated and initialized protocol instance
f5800e0b
CM
1321 * or ERR_PTR on failure. On failure the @proto reference is at first
1322 * put using @scmi_protocol_put() before releasing all the devres group.
48dc16e2
CM
1323 */
1324static struct scmi_protocol_instance *
1325scmi_alloc_init_protocol_instance(struct scmi_info *info,
1326 const struct scmi_protocol *proto)
1327{
1328 int ret = -ENOMEM;
1329 void *gid;
1330 struct scmi_protocol_instance *pi;
f0e73cee 1331 const struct scmi_handle *handle = &info->handle;
48dc16e2
CM
1332
1333 /* Protocol specific devres group */
1334 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
f5800e0b
CM
1335 if (!gid) {
1336 scmi_protocol_put(proto->id);
48dc16e2 1337 goto out;
f5800e0b 1338 }
48dc16e2
CM
1339
1340 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
1341 if (!pi)
1342 goto clean;
1343
1344 pi->gid = gid;
1345 pi->proto = proto;
d7b6cc56
CM
1346 pi->handle = handle;
1347 pi->ph.dev = handle->dev;
a4a20b09 1348 pi->ph.xops = &xfer_ops;
5c873d12 1349 pi->ph.hops = &helpers_ops;
d7b6cc56
CM
1350 pi->ph.set_priv = scmi_set_protocol_priv;
1351 pi->ph.get_priv = scmi_get_protocol_priv;
48dc16e2
CM
1352 refcount_set(&pi->users, 1);
1353 /* proto->init is assured NON NULL by scmi_protocol_register */
a4a20b09 1354 ret = pi->proto->instance_init(&pi->ph);
48dc16e2
CM
1355 if (ret)
1356 goto clean;
1357
1358 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
1359 GFP_KERNEL);
1360 if (ret != proto->id)
1361 goto clean;
1362
533c7095
CM
1363 /*
1364 * Warn but ignore events registration errors since we do not want
1365 * to skip whole protocols if their notifications are messed up.
1366 */
1367 if (pi->proto->events) {
1368 ret = scmi_register_protocol_events(handle, pi->proto->id,
b9f7fd90 1369 &pi->ph,
533c7095
CM
1370 pi->proto->events);
1371 if (ret)
1372 dev_warn(handle->dev,
1373 "Protocol:%X - Events Registration Failed - err:%d\n",
1374 pi->proto->id, ret);
1375 }
1376
48dc16e2
CM
1377 devres_close_group(handle->dev, pi->gid);
1378 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
1379
1380 return pi;
1381
1382clean:
f5800e0b
CM
1383 /* Take care to put the protocol module's owner before releasing all */
1384 scmi_protocol_put(proto->id);
48dc16e2
CM
1385 devres_release_group(handle->dev, gid);
1386out:
1387 return ERR_PTR(ret);
1388}
1389
1390/**
1391 * scmi_get_protocol_instance - Protocol initialization helper.
1392 * @handle: A reference to the SCMI platform instance.
1393 * @protocol_id: The protocol being requested.
1394 *
1395 * In case the required protocol has never been requested before for this
1396 * instance, allocate and initialize all the needed structures while handling
1397 * resource allocation with a dedicated per-protocol devres subgroup.
1398 *
f5800e0b
CM
1399 * Return: A reference to an initialized protocol instance or error on failure:
1400 * in particular returns -EPROBE_DEFER when the desired protocol could
1401 * NOT be found.
48dc16e2
CM
1402 */
1403static struct scmi_protocol_instance * __must_check
f0e73cee 1404scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
48dc16e2
CM
1405{
1406 struct scmi_protocol_instance *pi;
1407 struct scmi_info *info = handle_to_scmi_info(handle);
1408
1409 mutex_lock(&info->protocols_mtx);
1410 pi = idr_find(&info->protocols, protocol_id);
1411
1412 if (pi) {
1413 refcount_inc(&pi->users);
1414 } else {
1415 const struct scmi_protocol *proto;
1416
1417 /* Fails if protocol not registered on bus */
1418 proto = scmi_protocol_get(protocol_id);
1419 if (proto)
1420 pi = scmi_alloc_init_protocol_instance(info, proto);
1421 else
f5800e0b 1422 pi = ERR_PTR(-EPROBE_DEFER);
48dc16e2
CM
1423 }
1424 mutex_unlock(&info->protocols_mtx);
1425
1426 return pi;
1427}
1428
1429/**
1430 * scmi_protocol_acquire - Protocol acquire
1431 * @handle: A reference to the SCMI platform instance.
1432 * @protocol_id: The protocol being requested.
1433 *
1434 * Register a new user for the requested protocol on the specified SCMI
1435 * platform instance, possibly triggering its initialization on first user.
1436 *
1437 * Return: 0 if protocol was acquired successfully.
1438 */
f0e73cee 1439int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
48dc16e2
CM
1440{
1441 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
1442}
1443
1444/**
1445 * scmi_protocol_release - Protocol de-initialization helper.
1446 * @handle: A reference to the SCMI platform instance.
1447 * @protocol_id: The protocol being requested.
1448 *
1449 * Remove one user for the specified protocol and triggers de-initialization
1450 * and resources de-allocation once the last user has gone.
1451 */
f0e73cee 1452void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
48dc16e2
CM
1453{
1454 struct scmi_info *info = handle_to_scmi_info(handle);
1455 struct scmi_protocol_instance *pi;
1456
1457 mutex_lock(&info->protocols_mtx);
1458 pi = idr_find(&info->protocols, protocol_id);
1459 if (WARN_ON(!pi))
1460 goto out;
1461
1462 if (refcount_dec_and_test(&pi->users)) {
1463 void *gid = pi->gid;
1464
533c7095
CM
1465 if (pi->proto->events)
1466 scmi_deregister_protocol_events(handle, protocol_id);
1467
48dc16e2 1468 if (pi->proto->instance_deinit)
a4a20b09 1469 pi->proto->instance_deinit(&pi->ph);
48dc16e2
CM
1470
1471 idr_remove(&info->protocols, protocol_id);
1472
f5800e0b
CM
1473 scmi_protocol_put(protocol_id);
1474
48dc16e2
CM
1475 devres_release_group(handle->dev, gid);
1476 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
1477 protocol_id);
1478 }
1479
1480out:
1481 mutex_unlock(&info->protocols_mtx);
1482}
1483
8d3581c2 1484void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
b6f20ff8
SH
1485 u8 *prot_imp)
1486{
8d3581c2
CM
1487 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1488 struct scmi_info *info = handle_to_scmi_info(pi->handle);
b6f20ff8
SH
1489
1490 info->protocols_imp = prot_imp;
1491}
1492
bc40081d
SH
1493static bool
1494scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
1495{
1496 int i;
1497 struct scmi_info *info = handle_to_scmi_info(handle);
776b6c8a 1498 struct scmi_revision_info *rev = handle->version;
bc40081d
SH
1499
1500 if (!info->protocols_imp)
1501 return false;
1502
776b6c8a 1503 for (i = 0; i < rev->num_protocols; i++)
bc40081d
SH
1504 if (info->protocols_imp[i] == prot_id)
1505 return true;
1506 return false;
1507}
1508
23934efe 1509struct scmi_protocol_devres {
f0e73cee 1510 const struct scmi_handle *handle;
23934efe
CM
1511 u8 protocol_id;
1512};
1513
1514static void scmi_devm_release_protocol(struct device *dev, void *res)
1515{
1516 struct scmi_protocol_devres *dres = res;
1517
1518 scmi_protocol_release(dres->handle, dres->protocol_id);
1519}
1520
1521/**
1522 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
1523 * @sdev: A reference to an scmi_device whose embedded struct device is to
1524 * be used for devres accounting.
1525 * @protocol_id: The protocol being requested.
1526 * @ph: A pointer reference used to pass back the associated protocol handle.
1527 *
1528 * Get hold of a protocol accounting for its usage, eventually triggering its
1529 * initialization, and returning the protocol specific operations and related
1530 * protocol handle which will be used as first argument in most of the
1531 * protocols operations methods.
1532 * Being a devres based managed method, protocol hold will be automatically
1533 * released, and possibly de-initialized on last user, once the SCMI driver
1534 * owning the scmi_device is unbound from it.
1535 *
1536 * Return: A reference to the requested protocol operations or error.
1537 * Must be checked for errors by caller.
1538 */
1539static const void __must_check *
1540scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
1541 struct scmi_protocol_handle **ph)
1542{
1543 struct scmi_protocol_instance *pi;
1544 struct scmi_protocol_devres *dres;
1545 struct scmi_handle *handle = sdev->handle;
1546
1547 if (!ph)
1548 return ERR_PTR(-EINVAL);
1549
1550 dres = devres_alloc(scmi_devm_release_protocol,
1551 sizeof(*dres), GFP_KERNEL);
1552 if (!dres)
1553 return ERR_PTR(-ENOMEM);
1554
1555 pi = scmi_get_protocol_instance(handle, protocol_id);
1556 if (IS_ERR(pi)) {
1557 devres_free(dres);
1558 return pi;
1559 }
1560
1561 dres->handle = handle;
1562 dres->protocol_id = protocol_id;
1563 devres_add(&sdev->dev, dres);
1564
1565 *ph = &pi->ph;
1566
1567 return pi->proto->ops;
1568}
1569
1570static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
1571{
1572 struct scmi_protocol_devres *dres = res;
1573
1574 if (WARN_ON(!dres || !data))
1575 return 0;
1576
1577 return dres->protocol_id == *((u8 *)data);
1578}
1579
1580/**
1581 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
1582 * @sdev: A reference to an scmi_device whose embedded struct device is to
1583 * be used for devres accounting.
1584 * @protocol_id: The protocol being requested.
1585 *
1586 * Explicitly release a protocol hold previously obtained calling the above
1587 * @scmi_devm_protocol_get.
1588 */
1589static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
1590{
1591 int ret;
1592
1593 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
1594 scmi_devm_protocol_match, &protocol_id);
1595 WARN_ON(ret);
1596}
1597
69255e74
CM
1598/**
1599 * scmi_is_transport_atomic - Method to check if underlying transport for an
1600 * SCMI instance is configured as atomic.
1601 *
1602 * @handle: A reference to the SCMI platform instance.
05976c5f
CM
1603 * @atomic_threshold: An optional return value for the system wide currently
1604 * configured threshold for atomic operations.
69255e74
CM
1605 *
1606 * Return: True if transport is configured as atomic
1607 */
05976c5f
CM
1608static bool scmi_is_transport_atomic(const struct scmi_handle *handle,
1609 unsigned int *atomic_threshold)
69255e74 1610{
05976c5f 1611 bool ret;
69255e74
CM
1612 struct scmi_info *info = handle_to_scmi_info(handle);
1613
05976c5f
CM
1614 ret = info->desc->atomic_enabled && is_transport_polling_capable(info);
1615 if (ret && atomic_threshold)
1616 *atomic_threshold = info->atomic_threshold;
1617
1618 return ret;
69255e74
CM
1619}
1620
d4f9dddd
CM
1621static inline
1622struct scmi_handle *scmi_handle_get_from_info_unlocked(struct scmi_info *info)
1623{
1624 info->users++;
1625 return &info->handle;
1626}
1627
aa4f886f 1628/**
14e297b3 1629 * scmi_handle_get() - Get the SCMI handle for a device
aa4f886f
SH
1630 *
1631 * @dev: pointer to device for which we want SCMI handle
1632 *
1633 * NOTE: The function does not track individual clients of the framework
1baf47c2 1634 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
1635 * scmi_handle_put must be balanced with successful scmi_handle_get
1636 *
1637 * Return: pointer to handle if successful, NULL on error
1638 */
1639struct scmi_handle *scmi_handle_get(struct device *dev)
1640{
1641 struct list_head *p;
1642 struct scmi_info *info;
1643 struct scmi_handle *handle = NULL;
1644
1645 mutex_lock(&scmi_list_mutex);
1646 list_for_each(p, &scmi_list) {
1647 info = list_entry(p, struct scmi_info, node);
1648 if (dev->parent == info->dev) {
d4f9dddd 1649 handle = scmi_handle_get_from_info_unlocked(info);
aa4f886f
SH
1650 break;
1651 }
1652 }
1653 mutex_unlock(&scmi_list_mutex);
1654
1655 return handle;
1656}
1657
1658/**
1659 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
1660 *
1661 * @handle: handle acquired by scmi_handle_get
1662 *
1663 * NOTE: The function does not track individual clients of the framework
1baf47c2 1664 * and is expected to be maintained by caller of SCMI protocol library.
aa4f886f
SH
1665 * scmi_handle_put must be balanced with successful scmi_handle_get
1666 *
1667 * Return: 0 is successfully released
1668 * if null was passed, it returns -EINVAL;
1669 */
1670int scmi_handle_put(const struct scmi_handle *handle)
1671{
1672 struct scmi_info *info;
1673
1674 if (!handle)
1675 return -EINVAL;
1676
1677 info = handle_to_scmi_info(handle);
1678 mutex_lock(&scmi_list_mutex);
1679 if (!WARN_ON(!info->users))
1680 info->users--;
1681 mutex_unlock(&scmi_list_mutex);
1682
1683 return 0;
1684}
1685
4ebd8f6d
SH
1686static int __scmi_xfer_info_init(struct scmi_info *sinfo,
1687 struct scmi_xfers_info *info)
aa4f886f
SH
1688{
1689 int i;
1690 struct scmi_xfer *xfer;
1691 struct device *dev = sinfo->dev;
1692 const struct scmi_desc *desc = sinfo->desc;
aa4f886f
SH
1693
1694 /* Pre-allocated messages, no more than what hdr.seq can support */
c92c3e38 1695 if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
bdb8742d
CM
1696 dev_err(dev,
1697 "Invalid maximum messages %d, not in range [1 - %lu]\n",
c92c3e38 1698 info->max_msg, MSG_TOKEN_MAX);
aa4f886f
SH
1699 return -EINVAL;
1700 }
1701
9ca5a183 1702 hash_init(info->pending_xfers);
aa4f886f 1703
9ca5a183
CM
1704 /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
1705 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(MSG_TOKEN_MAX),
aa4f886f
SH
1706 sizeof(long), GFP_KERNEL);
1707 if (!info->xfer_alloc_table)
1708 return -ENOMEM;
1709
9ca5a183
CM
1710 /*
1711 * Preallocate a number of xfers equal to max inflight messages,
1712 * pre-initialize the buffer pointer to pre-allocated buffers and
1713 * attach all of them to the free list
1714 */
1715 INIT_HLIST_HEAD(&info->free_xfers);
c92c3e38 1716 for (i = 0; i < info->max_msg; i++) {
9ca5a183
CM
1717 xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
1718 if (!xfer)
1719 return -ENOMEM;
1720
aa4f886f
SH
1721 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
1722 GFP_KERNEL);
1723 if (!xfer->rx.buf)
1724 return -ENOMEM;
1725
1726 xfer->tx.buf = xfer->rx.buf;
1727 init_completion(&xfer->done);
ed7c04c1 1728 spin_lock_init(&xfer->lock);
9ca5a183
CM
1729
1730 /* Add initialized xfer to the free list */
1731 hlist_add_head(&xfer->node, &info->free_xfers);
aa4f886f
SH
1732 }
1733
1734 spin_lock_init(&info->xfer_lock);
1735
1736 return 0;
1737}
1738
c92c3e38
IS
1739static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
1740{
1741 const struct scmi_desc *desc = sinfo->desc;
1742
1743 if (!desc->ops->get_max_msg) {
1744 sinfo->tx_minfo.max_msg = desc->max_msg;
1745 sinfo->rx_minfo.max_msg = desc->max_msg;
1746 } else {
1747 struct scmi_chan_info *base_cinfo;
1748
1749 base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
1750 if (!base_cinfo)
1751 return -EINVAL;
1752 sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
1753
1754 /* RX channel is optional so can be skipped */
1755 base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
1756 if (base_cinfo)
1757 sinfo->rx_minfo.max_msg =
1758 desc->ops->get_max_msg(base_cinfo);
1759 }
1760
1761 return 0;
1762}
1763
4ebd8f6d
SH
1764static int scmi_xfer_info_init(struct scmi_info *sinfo)
1765{
c92c3e38
IS
1766 int ret;
1767
1768 ret = scmi_channels_max_msg_configure(sinfo);
1769 if (ret)
1770 return ret;
4ebd8f6d 1771
c92c3e38 1772 ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
4ebd8f6d
SH
1773 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1774 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1775
1776 return ret;
1777}
1778
5c8a47a5
VK
1779static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1780 int prot_id, bool tx)
aa4f886f 1781{
3748daf7 1782 int ret, idx;
fbc4d81a 1783 struct scmi_chan_info *cinfo;
46cc7c28 1784 struct idr *idr;
3748daf7
SH
1785
1786 /* Transmit channel is first entry i.e. index 0 */
1787 idx = tx ? 0 : 1;
46cc7c28 1788 idr = tx ? &info->tx_idr : &info->rx_idr;
aa4f886f 1789
11040889
SH
1790 /* check if already allocated, used for multiple device per protocol */
1791 cinfo = idr_find(idr, prot_id);
1792 if (cinfo)
1793 return 0;
1794
5c8a47a5 1795 if (!info->desc->ops->chan_available(dev, idx)) {
46cc7c28
SH
1796 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
1797 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
1798 return -EINVAL;
907b6d14
SH
1799 goto idr_alloc;
1800 }
1801
fbc4d81a
SH
1802 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
1803 if (!cinfo)
1804 return -ENOMEM;
1805
fbc4d81a
SH
1806 cinfo->dev = dev;
1807
5c8a47a5
VK
1808 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
1809 if (ret)
aa4f886f 1810 return ret;
aa4f886f 1811
a690b7e6
CM
1812 if (tx && is_polling_required(cinfo, info)) {
1813 if (is_transport_polling_capable(info))
1814 dev_info(dev,
1815 "Enabled polling mode TX channel - prot_id:%d\n",
1816 prot_id);
1817 else
1818 dev_warn(dev,
1819 "Polling mode NOT supported by transport.\n");
1820 }
1821
907b6d14 1822idr_alloc:
46cc7c28 1823 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
907b6d14
SH
1824 if (ret != prot_id) {
1825 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
1826 return ret;
1827 }
1828
1829 cinfo->handle = &info->handle;
aa4f886f
SH
1830 return 0;
1831}
1832
46cc7c28 1833static inline int
5c8a47a5 1834scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
46cc7c28 1835{
5c8a47a5 1836 int ret = scmi_chan_setup(info, dev, prot_id, true);
46cc7c28
SH
1837
1838 if (!ret) /* Rx is optional, hence no error check */
5c8a47a5 1839 scmi_chan_setup(info, dev, prot_id, false);
46cc7c28
SH
1840
1841 return ret;
1842}
1843
d4f9dddd
CM
1844/**
1845 * scmi_get_protocol_device - Helper to get/create an SCMI device.
1846 *
1847 * @np: A device node representing a valid active protocols for the referred
1848 * SCMI instance.
1849 * @info: The referred SCMI instance for which we are getting/creating this
1850 * device.
1851 * @prot_id: The protocol ID.
1852 * @name: The device name.
1853 *
1854 * Referring to the specific SCMI instance identified by @info, this helper
1855 * takes care to return a properly initialized device matching the requested
1856 * @proto_id and @name: if device was still not existent it is created as a
1857 * child of the specified SCMI instance @info and its transport properly
1858 * initialized as usual.
b98cf55e
CM
1859 *
1860 * Return: A properly initialized scmi device, NULL otherwise.
d4f9dddd
CM
1861 */
1862static inline struct scmi_device *
1863scmi_get_protocol_device(struct device_node *np, struct scmi_info *info,
1864 int prot_id, const char *name)
bc40081d
SH
1865{
1866 struct scmi_device *sdev;
1867
d4f9dddd
CM
1868 /* Already created for this parent SCMI instance ? */
1869 sdev = scmi_child_dev_find(info->dev, prot_id, name);
1870 if (sdev)
1871 return sdev;
1872
1873 pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id);
1874
ee7a9c9f 1875 sdev = scmi_device_create(np, info->dev, prot_id, name);
bc40081d
SH
1876 if (!sdev) {
1877 dev_err(info->dev, "failed to create %d protocol device\n",
1878 prot_id);
d4f9dddd 1879 return NULL;
bc40081d
SH
1880 }
1881
5c8a47a5 1882 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
907b6d14
SH
1883 dev_err(&sdev->dev, "failed to setup transport\n");
1884 scmi_device_destroy(sdev);
d4f9dddd 1885 return NULL;
907b6d14
SH
1886 }
1887
d4f9dddd
CM
1888 return sdev;
1889}
1890
1891static inline void
1892scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
1893 int prot_id, const char *name)
1894{
1895 struct scmi_device *sdev;
1896
1897 sdev = scmi_get_protocol_device(np, info, prot_id, name);
1898 if (!sdev)
1899 return;
1900
bc40081d
SH
1901 /* setup handle now as the transport is ready */
1902 scmi_set_handle(sdev);
1903}
1904
d4f9dddd
CM
1905/**
1906 * scmi_create_protocol_devices - Create devices for all pending requests for
1907 * this SCMI instance.
1908 *
1909 * @np: The device node describing the protocol
1910 * @info: The SCMI instance descriptor
1911 * @prot_id: The protocol ID
1912 *
1913 * All devices previously requested for this instance (if any) are found and
1914 * created by scanning the proper @&scmi_requested_devices entry.
1915 */
1916static void scmi_create_protocol_devices(struct device_node *np,
1917 struct scmi_info *info, int prot_id)
1918{
1919 struct list_head *phead;
9c5c463f 1920
d4f9dddd
CM
1921 mutex_lock(&scmi_requested_devices_mtx);
1922 phead = idr_find(&scmi_requested_devices, prot_id);
1923 if (phead) {
1924 struct scmi_requested_dev *rdev;
9c5c463f 1925
d4f9dddd
CM
1926 list_for_each_entry(rdev, phead, node)
1927 scmi_create_protocol_device(np, info, prot_id,
1928 rdev->id_table->name);
1929 }
1930 mutex_unlock(&scmi_requested_devices_mtx);
1931}
1932
1933/**
1934 * scmi_protocol_device_request - Helper to request a device
1935 *
1936 * @id_table: A protocol/name pair descriptor for the device to be created.
1937 *
1938 * This helper let an SCMI driver request specific devices identified by the
1939 * @id_table to be created for each active SCMI instance.
1940 *
1941 * The requested device name MUST NOT be already existent for any protocol;
1942 * at first the freshly requested @id_table is annotated in the IDR table
1943 * @scmi_requested_devices, then a matching device is created for each already
1944 * active SCMI instance. (if any)
1945 *
1946 * This way the requested device is created straight-away for all the already
1947 * initialized(probed) SCMI instances (handles) and it remains also annotated
1948 * as pending creation if the requesting SCMI driver was loaded before some
1949 * SCMI instance and related transports were available: when such late instance
1950 * is probed, its probe will take care to scan the list of pending requested
1951 * devices and create those on its own (see @scmi_create_protocol_devices and
1952 * its enclosing loop)
1953 *
1954 * Return: 0 on Success
1955 */
1956int scmi_protocol_device_request(const struct scmi_device_id *id_table)
9c5c463f 1957{
d4f9dddd
CM
1958 int ret = 0;
1959 unsigned int id = 0;
1960 struct list_head *head, *phead = NULL;
1961 struct scmi_requested_dev *rdev;
1962 struct scmi_info *info;
9c5c463f 1963
d4f9dddd
CM
1964 pr_debug("Requesting SCMI device (%s) for protocol %x\n",
1965 id_table->name, id_table->protocol_id);
1966
1967 /*
1968 * Search for the matching protocol rdev list and then search
1969 * of any existent equally named device...fails if any duplicate found.
1970 */
1971 mutex_lock(&scmi_requested_devices_mtx);
1972 idr_for_each_entry(&scmi_requested_devices, head, id) {
1973 if (!phead) {
1974 /* A list found registered in the IDR is never empty */
1975 rdev = list_first_entry(head, struct scmi_requested_dev,
1976 node);
1977 if (rdev->id_table->protocol_id ==
1978 id_table->protocol_id)
1979 phead = head;
1980 }
1981 list_for_each_entry(rdev, head, node) {
1982 if (!strcmp(rdev->id_table->name, id_table->name)) {
1983 pr_err("Ignoring duplicate request [%d] %s\n",
1984 rdev->id_table->protocol_id,
1985 rdev->id_table->name);
1986 ret = -EINVAL;
1987 goto out;
1988 }
1989 }
1990 }
1991
1992 /*
1993 * No duplicate found for requested id_table, so let's create a new
1994 * requested device entry for this new valid request.
1995 */
1996 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1997 if (!rdev) {
1998 ret = -ENOMEM;
1999 goto out;
2000 }
2001 rdev->id_table = id_table;
2002
2003 /*
2004 * Append the new requested device table descriptor to the head of the
2005 * related protocol list, eventually creating such head if not already
2006 * there.
2007 */
2008 if (!phead) {
2009 phead = kzalloc(sizeof(*phead), GFP_KERNEL);
2010 if (!phead) {
2011 kfree(rdev);
2012 ret = -ENOMEM;
2013 goto out;
2014 }
2015 INIT_LIST_HEAD(phead);
2016
2017 ret = idr_alloc(&scmi_requested_devices, (void *)phead,
2018 id_table->protocol_id,
2019 id_table->protocol_id + 1, GFP_KERNEL);
2020 if (ret != id_table->protocol_id) {
2021 pr_err("Failed to save SCMI device - ret:%d\n", ret);
2022 kfree(rdev);
2023 kfree(phead);
2024 ret = -EINVAL;
2025 goto out;
2026 }
2027 ret = 0;
2028 }
2029 list_add(&rdev->node, phead);
2030
2031 /*
2032 * Now effectively create and initialize the requested device for every
2033 * already initialized SCMI instance which has registered the requested
2034 * protocol as a valid active one: i.e. defined in DT and supported by
2035 * current platform FW.
2036 */
2037 mutex_lock(&scmi_list_mutex);
2038 list_for_each_entry(info, &scmi_list, node) {
2039 struct device_node *child;
2040
2041 child = idr_find(&info->active_protocols,
2042 id_table->protocol_id);
2043 if (child) {
2044 struct scmi_device *sdev;
2045
2046 sdev = scmi_get_protocol_device(child, info,
2047 id_table->protocol_id,
2048 id_table->name);
2049 /* Set handle if not already set: device existed */
2050 if (sdev && !sdev->handle)
2051 sdev->handle =
2052 scmi_handle_get_from_info_unlocked(info);
2053 } else {
2054 dev_err(info->dev,
2055 "Failed. SCMI protocol %d not active.\n",
2056 id_table->protocol_id);
2057 }
2058 }
2059 mutex_unlock(&scmi_list_mutex);
2060
2061out:
2062 mutex_unlock(&scmi_requested_devices_mtx);
9c5c463f 2063
d4f9dddd
CM
2064 return ret;
2065}
9c5c463f 2066
d4f9dddd
CM
2067/**
2068 * scmi_protocol_device_unrequest - Helper to unrequest a device
2069 *
2070 * @id_table: A protocol/name pair descriptor for the device to be unrequested.
2071 *
2072 * An helper to let an SCMI driver release its request about devices; note that
2073 * devices are created and initialized once the first SCMI driver request them
2074 * but they destroyed only on SCMI core unloading/unbinding.
2075 *
2076 * The current SCMI transport layer uses such devices as internal references and
2077 * as such they could be shared as same transport between multiple drivers so
2078 * that cannot be safely destroyed till the whole SCMI stack is removed.
2079 * (unless adding further burden of refcounting.)
2080 */
2081void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
2082{
2083 struct list_head *phead;
2084
2085 pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
2086 id_table->name, id_table->protocol_id);
2087
2088 mutex_lock(&scmi_requested_devices_mtx);
2089 phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
2090 if (phead) {
2091 struct scmi_requested_dev *victim, *tmp;
2092
2093 list_for_each_entry_safe(victim, tmp, phead, node) {
2094 if (!strcmp(victim->id_table->name, id_table->name)) {
2095 list_del(&victim->node);
2096 kfree(victim);
2097 break;
2098 }
2099 }
2100
2101 if (list_empty(phead)) {
2102 idr_remove(&scmi_requested_devices,
2103 id_table->protocol_id);
2104 kfree(phead);
9c5c463f
SH
2105 }
2106 }
d4f9dddd 2107 mutex_unlock(&scmi_requested_devices_mtx);
9c5c463f
SH
2108}
2109
1e7cbfaa
RB
2110static int scmi_cleanup_txrx_channels(struct scmi_info *info)
2111{
2112 int ret;
2113 struct idr *idr = &info->tx_idr;
2114
2115 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
2116 idr_destroy(&info->tx_idr);
2117
2118 idr = &info->rx_idr;
2119 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
2120 idr_destroy(&info->rx_idr);
2121
2122 return ret;
2123}
2124
aa4f886f
SH
2125static int scmi_probe(struct platform_device *pdev)
2126{
2127 int ret;
2128 struct scmi_handle *handle;
2129 const struct scmi_desc *desc;
2130 struct scmi_info *info;
2131 struct device *dev = &pdev->dev;
bc40081d 2132 struct device_node *child, *np = dev->of_node;
aa4f886f 2133
d9350f21
AP
2134 desc = of_device_get_match_data(dev);
2135 if (!desc)
2136 return -EINVAL;
aa4f886f
SH
2137
2138 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
2139 if (!info)
2140 return -ENOMEM;
2141
2142 info->dev = dev;
2143 info->desc = desc;
2144 INIT_LIST_HEAD(&info->node);
48dc16e2
CM
2145 idr_init(&info->protocols);
2146 mutex_init(&info->protocols_mtx);
d4f9dddd 2147 idr_init(&info->active_protocols);
aa4f886f 2148
aa4f886f 2149 platform_set_drvdata(pdev, info);
907b6d14 2150 idr_init(&info->tx_idr);
46cc7c28 2151 idr_init(&info->rx_idr);
aa4f886f
SH
2152
2153 handle = &info->handle;
2154 handle->dev = info->dev;
b6f20ff8 2155 handle->version = &info->version;
23934efe
CM
2156 handle->devm_protocol_get = scmi_devm_protocol_get;
2157 handle->devm_protocol_put = scmi_devm_protocol_put;
aa4f886f 2158
05976c5f
CM
2159 /* System wide atomic threshold for atomic ops .. if any */
2160 if (!of_property_read_u32(np, "atomic-threshold-us",
2161 &info->atomic_threshold))
2162 dev_info(dev,
2163 "SCMI System wide atomic threshold set to %d us\n",
2164 info->atomic_threshold);
69255e74 2165 handle->is_transport_atomic = scmi_is_transport_atomic;
aa4f886f 2166
78852812
PH
2167 if (desc->ops->link_supplier) {
2168 ret = desc->ops->link_supplier(dev);
2169 if (ret)
2170 return ret;
2171 }
2172
5c8a47a5 2173 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
aa4f886f
SH
2174 if (ret)
2175 return ret;
2176
4ebd8f6d
SH
2177 ret = scmi_xfer_info_init(info);
2178 if (ret)
1e7cbfaa 2179 goto clear_txrx_setup;
4ebd8f6d 2180
6b8a6913
CM
2181 if (scmi_notification_init(handle))
2182 dev_err(dev, "SCMI Notifications NOT available.\n");
2183
69255e74
CM
2184 if (info->desc->atomic_enabled && !is_transport_polling_capable(info))
2185 dev_err(dev,
2186 "Transport is not polling capable. Atomic mode not supported.\n");
2187
8d3581c2
CM
2188 /*
2189 * Trigger SCMI Base protocol initialization.
2190 * It's mandatory and won't be ever released/deinit until the
2191 * SCMI stack is shutdown/unloaded as a whole.
2192 */
2193 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
b6f20ff8 2194 if (ret) {
8d3581c2 2195 dev_err(dev, "unable to communicate with SCMI\n");
1e7cbfaa 2196 goto notification_exit;
b6f20ff8
SH
2197 }
2198
aa4f886f
SH
2199 mutex_lock(&scmi_list_mutex);
2200 list_add_tail(&info->node, &scmi_list);
2201 mutex_unlock(&scmi_list_mutex);
2202
bc40081d
SH
2203 for_each_available_child_of_node(np, child) {
2204 u32 prot_id;
2205
2206 if (of_property_read_u32(child, "reg", &prot_id))
2207 continue;
2208
354b2e36
SH
2209 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
2210 dev_err(dev, "Out of range protocol %d\n", prot_id);
bc40081d
SH
2211
2212 if (!scmi_is_protocol_implemented(handle, prot_id)) {
2213 dev_err(dev, "SCMI protocol %d not implemented\n",
2214 prot_id);
2215 continue;
2216 }
2217
d4f9dddd
CM
2218 /*
2219 * Save this valid DT protocol descriptor amongst
2220 * @active_protocols for this SCMI instance/
2221 */
2222 ret = idr_alloc(&info->active_protocols, child,
2223 prot_id, prot_id + 1, GFP_KERNEL);
2224 if (ret != prot_id) {
2225 dev_err(dev, "SCMI protocol %d already activated. Skip\n",
2226 prot_id);
2227 continue;
2228 }
2229
2230 of_node_get(child);
9c5c463f 2231 scmi_create_protocol_devices(child, info, prot_id);
bc40081d
SH
2232 }
2233
aa4f886f 2234 return 0;
1e7cbfaa
RB
2235
2236notification_exit:
2237 scmi_notification_exit(&info->handle);
2238clear_txrx_setup:
2239 scmi_cleanup_txrx_channels(info);
2240 return ret;
aa4f886f
SH
2241}
2242
5c8a47a5 2243void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
2747a967 2244{
2747a967 2245 idr_remove(idr, id);
2747a967
SH
2246}
2247
2248static int scmi_remove(struct platform_device *pdev)
2249{
d4f9dddd 2250 int ret = 0, id;
2747a967 2251 struct scmi_info *info = platform_get_drvdata(pdev);
d4f9dddd 2252 struct device_node *child;
2747a967
SH
2253
2254 mutex_lock(&scmi_list_mutex);
2255 if (info->users)
2256 ret = -EBUSY;
2257 else
2258 list_del(&info->node);
2259 mutex_unlock(&scmi_list_mutex);
2260
2261 if (ret)
2262 return ret;
2263
a90b6543
CM
2264 scmi_notification_exit(&info->handle);
2265
48dc16e2
CM
2266 mutex_lock(&info->protocols_mtx);
2267 idr_destroy(&info->protocols);
2268 mutex_unlock(&info->protocols_mtx);
2269
d4f9dddd
CM
2270 idr_for_each_entry(&info->active_protocols, child, id)
2271 of_node_put(child);
2272 idr_destroy(&info->active_protocols);
2273
2747a967 2274 /* Safe to free channels since no more users */
1e7cbfaa 2275 return scmi_cleanup_txrx_channels(info);
2747a967
SH
2276}
2277
4605e224
SH
2278static ssize_t protocol_version_show(struct device *dev,
2279 struct device_attribute *attr, char *buf)
2280{
2281 struct scmi_info *info = dev_get_drvdata(dev);
2282
2283 return sprintf(buf, "%u.%u\n", info->version.major_ver,
2284 info->version.minor_ver);
2285}
2286static DEVICE_ATTR_RO(protocol_version);
2287
2288static ssize_t firmware_version_show(struct device *dev,
2289 struct device_attribute *attr, char *buf)
2290{
2291 struct scmi_info *info = dev_get_drvdata(dev);
2292
2293 return sprintf(buf, "0x%x\n", info->version.impl_ver);
2294}
2295static DEVICE_ATTR_RO(firmware_version);
2296
2297static ssize_t vendor_id_show(struct device *dev,
2298 struct device_attribute *attr, char *buf)
2299{
2300 struct scmi_info *info = dev_get_drvdata(dev);
2301
2302 return sprintf(buf, "%s\n", info->version.vendor_id);
2303}
2304static DEVICE_ATTR_RO(vendor_id);
2305
2306static ssize_t sub_vendor_id_show(struct device *dev,
2307 struct device_attribute *attr, char *buf)
2308{
2309 struct scmi_info *info = dev_get_drvdata(dev);
2310
2311 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
2312}
2313static DEVICE_ATTR_RO(sub_vendor_id);
2314
2315static struct attribute *versions_attrs[] = {
2316 &dev_attr_firmware_version.attr,
2317 &dev_attr_protocol_version.attr,
2318 &dev_attr_vendor_id.attr,
2319 &dev_attr_sub_vendor_id.attr,
2320 NULL,
2321};
2322ATTRIBUTE_GROUPS(versions);
2323
2747a967
SH
2324/* Each compatible listed below must have descriptor associated with it */
2325static const struct of_device_id scmi_of_match[] = {
e8419c24 2326#ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
5c8a47a5 2327 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
ab7766b7 2328#endif
5f90f189
EC
2329#ifdef CONFIG_ARM_SCMI_TRANSPORT_OPTEE
2330 { .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc },
2331#endif
e8419c24 2332#ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
1dc65580 2333 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
46abe13b
IS
2334#endif
2335#ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
2336 { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
1dc65580 2337#endif
2747a967
SH
2338 { /* Sentinel */ },
2339};
2340
2341MODULE_DEVICE_TABLE(of, scmi_of_match);
2342
aa4f886f
SH
2343static struct platform_driver scmi_driver = {
2344 .driver = {
2345 .name = "arm-scmi",
2346 .of_match_table = scmi_of_match,
4605e224 2347 .dev_groups = versions_groups,
aa4f886f
SH
2348 },
2349 .probe = scmi_probe,
2350 .remove = scmi_remove,
2351};
2352
ceac257d
CM
2353/**
2354 * __scmi_transports_setup - Common helper to call transport-specific
2355 * .init/.exit code if provided.
2356 *
2357 * @init: A flag to distinguish between init and exit.
2358 *
2359 * Note that, if provided, we invoke .init/.exit functions for all the
2360 * transports currently compiled in.
2361 *
2362 * Return: 0 on Success.
2363 */
2364static inline int __scmi_transports_setup(bool init)
2365{
2366 int ret = 0;
2367 const struct of_device_id *trans;
2368
2369 for (trans = scmi_of_match; trans->data; trans++) {
2370 const struct scmi_desc *tdesc = trans->data;
2371
2372 if ((init && !tdesc->transport_init) ||
2373 (!init && !tdesc->transport_exit))
2374 continue;
2375
2376 if (init)
2377 ret = tdesc->transport_init();
2378 else
2379 tdesc->transport_exit();
2380
2381 if (ret) {
2382 pr_err("SCMI transport %s FAILED initialization!\n",
2383 trans->compatible);
2384 break;
2385 }
2386 }
2387
2388 return ret;
2389}
2390
2391static int __init scmi_transports_init(void)
2392{
2393 return __scmi_transports_setup(true);
2394}
2395
2396static void __exit scmi_transports_exit(void)
2397{
2398 __scmi_transports_setup(false);
2399}
2400
5a2f0a0b
SH
2401static int __init scmi_driver_init(void)
2402{
ceac257d
CM
2403 int ret;
2404
c0397c85
CM
2405 /* Bail out if no SCMI transport was configured */
2406 if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
2407 return -EINVAL;
5a2f0a0b 2408
c0397c85 2409 scmi_bus_init();
e8419c24 2410
ceac257d
CM
2411 /* Initialize any compiled-in transport which provided an init/exit */
2412 ret = scmi_transports_init();
2413 if (ret)
2414 return ret;
2415
48dc16e2
CM
2416 scmi_base_register();
2417
1eaf18e3
SH
2418 scmi_clock_register();
2419 scmi_perf_register();
2420 scmi_power_register();
2421 scmi_reset_register();
2422 scmi_sensors_register();
2add5cac 2423 scmi_voltage_register();
1eaf18e3
SH
2424 scmi_system_register();
2425
5a2f0a0b
SH
2426 return platform_driver_register(&scmi_driver);
2427}
1eaf18e3 2428subsys_initcall(scmi_driver_init);
5a2f0a0b
SH
2429
2430static void __exit scmi_driver_exit(void)
2431{
48dc16e2 2432 scmi_base_unregister();
5a2f0a0b 2433
1eaf18e3
SH
2434 scmi_clock_unregister();
2435 scmi_perf_unregister();
2436 scmi_power_unregister();
2437 scmi_reset_unregister();
2438 scmi_sensors_unregister();
2add5cac 2439 scmi_voltage_unregister();
1eaf18e3
SH
2440 scmi_system_unregister();
2441
48dc16e2
CM
2442 scmi_bus_exit();
2443
ceac257d
CM
2444 scmi_transports_exit();
2445
5a2f0a0b
SH
2446 platform_driver_unregister(&scmi_driver);
2447}
2448module_exit(scmi_driver_exit);
aa4f886f 2449
1ba603f5 2450MODULE_ALIAS("platform:arm-scmi");
aa4f886f
SH
2451MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
2452MODULE_DESCRIPTION("ARM SCMI protocol driver");
2453MODULE_LICENSE("GPL v2");