1 // SPDX-License-Identifier: GPL-2.0
3 * System Control and Management Interface (SCMI) Message Protocol driver
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
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.
14 * Copyright (C) 2018-2021 ARM Ltd.
17 #include <linux/bitmap.h>
18 #include <linux/device.h>
19 #include <linux/export.h>
20 #include <linux/idr.h>
22 #include <linux/io-64-nonatomic-hi-lo.h>
23 #include <linux/kernel.h>
24 #include <linux/ktime.h>
25 #include <linux/hashtable.h>
26 #include <linux/list.h>
27 #include <linux/module.h>
28 #include <linux/of_address.h>
29 #include <linux/of_device.h>
30 #include <linux/processor.h>
31 #include <linux/refcount.h>
32 #include <linux/slab.h>
37 #define CREATE_TRACE_POINTS
38 #include <trace/events/scmi.h>
40 enum scmi_error_codes {
41 SCMI_SUCCESS = 0, /* Success */
42 SCMI_ERR_SUPPORT = -1, /* Not supported */
43 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
44 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
45 SCMI_ERR_ENTRY = -4, /* Not found */
46 SCMI_ERR_RANGE = -5, /* Value out of range */
47 SCMI_ERR_BUSY = -6, /* Device busy */
48 SCMI_ERR_COMMS = -7, /* Communication Error */
49 SCMI_ERR_GENERIC = -8, /* Generic Error */
50 SCMI_ERR_HARDWARE = -9, /* Hardware Error */
51 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
54 /* List of all SCMI devices active in system */
55 static LIST_HEAD(scmi_list);
56 /* Protection for the entire list */
57 static DEFINE_MUTEX(scmi_list_mutex);
58 /* Track the unique id for the transfers for debug & profiling purpose */
59 static atomic_t transfer_last_id;
61 static DEFINE_IDR(scmi_requested_devices);
62 static DEFINE_MUTEX(scmi_requested_devices_mtx);
64 /* Track globally the creation of SCMI SystemPower related devices */
65 static bool scmi_syspower_registered;
66 /* Protect access to scmi_syspower_registered */
67 static DEFINE_MUTEX(scmi_syspower_mtx);
69 struct scmi_requested_dev {
70 const struct scmi_device_id *id_table;
71 struct list_head node;
75 * struct scmi_xfers_info - Structure to manage transfer information
77 * @xfer_alloc_table: Bitmap table for allocated messages.
78 * Index of this bitmap table is also used for message
79 * sequence identifier.
80 * @xfer_lock: Protection for message allocation
81 * @max_msg: Maximum number of messages that can be pending
82 * @free_xfers: A free list for available to use xfers. It is initialized with
83 * a number of xfers equal to the maximum allowed in-flight
85 * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the
86 * currently in-flight messages.
88 struct scmi_xfers_info {
89 unsigned long *xfer_alloc_table;
92 struct hlist_head free_xfers;
93 DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
97 * struct scmi_protocol_instance - Describe an initialized protocol instance.
98 * @handle: Reference to the SCMI handle associated to this protocol instance.
99 * @proto: A reference to the protocol descriptor.
100 * @gid: A reference for per-protocol devres management.
101 * @users: A refcount to track effective users of this protocol.
102 * @priv: Reference for optional protocol private data.
103 * @ph: An embedded protocol handle that will be passed down to protocol
104 * initialization code to identify this instance.
106 * Each protocol is initialized independently once for each SCMI platform in
107 * which is defined by DT and implemented by the SCMI server fw.
109 struct scmi_protocol_instance {
110 const struct scmi_handle *handle;
111 const struct scmi_protocol *proto;
115 struct scmi_protocol_handle ph;
118 #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
121 * struct scmi_info - Structure representing a SCMI instance
123 * @dev: Device pointer
124 * @desc: SoC description for this instance
125 * @version: SCMI revision information containing protocol version,
126 * implementation version and (sub-)vendor identification.
127 * @handle: Instance of SCMI handle to send to clients
128 * @tx_minfo: Universal Transmit Message management info
129 * @rx_minfo: Universal Receive Message management info
130 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
131 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
132 * @protocols: IDR for protocols' instance descriptors initialized for
133 * this SCMI instance: populated on protocol's first attempted
135 * @protocols_mtx: A mutex to protect protocols instances initialization.
136 * @protocols_imp: List of protocols implemented, currently maximum of
137 * scmi_revision_info.num_protocols elements allocated by the
139 * @active_protocols: IDR storing device_nodes for protocols actually defined
140 * in the DT and confirmed as implemented by fw.
141 * @atomic_threshold: Optional system wide DT-configured threshold, expressed
142 * in microseconds, for atomic operations.
143 * Only SCMI synchronous commands reported by the platform
144 * to have an execution latency lesser-equal to the threshold
145 * should be considered for atomic mode operation: such
146 * decision is finally left up to the SCMI drivers.
147 * @notify_priv: Pointer to private data structure specific to notifications.
149 * @users: Number of users of this instance
153 const struct scmi_desc *desc;
154 struct scmi_revision_info version;
155 struct scmi_handle handle;
156 struct scmi_xfers_info tx_minfo;
157 struct scmi_xfers_info rx_minfo;
160 struct idr protocols;
161 /* Ensure mutual exclusive access to protocols instance array */
162 struct mutex protocols_mtx;
164 struct idr active_protocols;
165 unsigned int atomic_threshold;
167 struct list_head node;
171 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
173 static const int scmi_linux_errmap[] = {
174 /* better than switch case as long as return value is continuous */
175 0, /* SCMI_SUCCESS */
176 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
177 -EINVAL, /* SCMI_ERR_PARAM */
178 -EACCES, /* SCMI_ERR_ACCESS */
179 -ENOENT, /* SCMI_ERR_ENTRY */
180 -ERANGE, /* SCMI_ERR_RANGE */
181 -EBUSY, /* SCMI_ERR_BUSY */
182 -ECOMM, /* SCMI_ERR_COMMS */
183 -EIO, /* SCMI_ERR_GENERIC */
184 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
185 -EPROTO, /* SCMI_ERR_PROTOCOL */
188 static inline int scmi_to_linux_errno(int errno)
190 int err_idx = -errno;
192 if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
193 return scmi_linux_errmap[err_idx];
197 void scmi_notification_instance_data_set(const struct scmi_handle *handle,
200 struct scmi_info *info = handle_to_scmi_info(handle);
202 info->notify_priv = priv;
203 /* Ensure updated protocol private date are visible */
207 void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
209 struct scmi_info *info = handle_to_scmi_info(handle);
211 /* Ensure protocols_private_data has been updated */
213 return info->notify_priv;
217 * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
219 * @minfo: Pointer to Tx/Rx Message management info based on channel type
220 * @xfer: The xfer to act upon
222 * Pick the next unused monotonically increasing token and set it into
223 * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
224 * reuse of freshly completed or timed-out xfers, thus mitigating the risk
225 * of incorrect association of a late and expired xfer with a live in-flight
226 * transaction, both happening to re-use the same token identifier.
228 * Since platform is NOT required to answer our request in-order we should
229 * account for a few rare but possible scenarios:
231 * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
232 * using find_next_zero_bit() starting from candidate next_token bit
234 * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
235 * are plenty of free tokens at start, so try a second pass using
236 * find_next_zero_bit() and starting from 0.
244 * -----------+----------------------------------------------------------
245 * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
246 * ----------------------------------------------------------------------
250 * Out-of-order pending at start
251 * -----------------------------
253 * |- xfer_id picked, last_token fixed
254 * -----+----------------------------------------------------------------
255 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
256 * ----------------------------------------------------------------------
261 * Out-of-order pending at end
262 * ---------------------------
264 * |- xfer_id picked, last_token fixed
265 * -----+----------------------------------------------------------------
266 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
267 * ----------------------------------------------------------------------
271 * Context: Assumes to be called with @xfer_lock already acquired.
273 * Return: 0 on Success or error
275 static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
276 struct scmi_xfer *xfer)
278 unsigned long xfer_id, next_token;
281 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
282 * using the pre-allocated transfer_id as a base.
283 * Note that the global transfer_id is shared across all message types
284 * so there could be holes in the allocated set of monotonic sequence
285 * numbers, but that is going to limit the effectiveness of the
286 * mitigation only in very rare limit conditions.
288 next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
290 /* Pick the next available xfer_id >= next_token */
291 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
292 MSG_TOKEN_MAX, next_token);
293 if (xfer_id == MSG_TOKEN_MAX) {
295 * After heavily out-of-order responses, there are no free
296 * tokens ahead, but only at start of xfer_alloc_table so
297 * try again from the beginning.
299 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
302 * Something is wrong if we got here since there can be a
303 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
304 * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
306 if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
310 /* Update +/- last_token accordingly if we skipped some hole */
311 if (xfer_id != next_token)
312 atomic_add((int)(xfer_id - next_token), &transfer_last_id);
315 set_bit(xfer_id, minfo->xfer_alloc_table);
316 xfer->hdr.seq = (u16)xfer_id;
322 * scmi_xfer_token_clear - Release the token
324 * @minfo: Pointer to Tx/Rx Message management info based on channel type
325 * @xfer: The xfer to act upon
327 static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
328 struct scmi_xfer *xfer)
330 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
334 * scmi_xfer_get() - Allocate one message
336 * @handle: Pointer to SCMI entity handle
337 * @minfo: Pointer to Tx/Rx Message management info based on channel type
338 * @set_pending: If true a monotonic token is picked and the xfer is added to
339 * the pending hash table.
341 * Helper function which is used by various message functions that are
342 * exposed to clients of this driver for allocating a message traffic event.
344 * Picks an xfer from the free list @free_xfers (if any available) and, if
345 * required, sets a monotonically increasing token and stores the inflight xfer
346 * into the @pending_xfers hashtable for later retrieval.
348 * The successfully initialized xfer is refcounted.
350 * Context: Holds @xfer_lock while manipulating @xfer_alloc_table and
353 * Return: 0 if all went fine, else corresponding error.
355 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
356 struct scmi_xfers_info *minfo,
361 struct scmi_xfer *xfer;
363 spin_lock_irqsave(&minfo->xfer_lock, flags);
364 if (hlist_empty(&minfo->free_xfers)) {
365 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
366 return ERR_PTR(-ENOMEM);
369 /* grab an xfer from the free_list */
370 xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
371 hlist_del_init(&xfer->node);
374 * Allocate transfer_id early so that can be used also as base for
375 * monotonic sequence number generation if needed.
377 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
380 /* Pick and set monotonic token */
381 ret = scmi_xfer_token_set(minfo, xfer);
383 hash_add(minfo->pending_xfers, &xfer->node,
385 xfer->pending = true;
388 "Failed to get monotonic token %d\n", ret);
389 hlist_add_head(&xfer->node, &minfo->free_xfers);
395 refcount_set(&xfer->users, 1);
396 atomic_set(&xfer->busy, SCMI_XFER_FREE);
398 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
404 * __scmi_xfer_put() - Release a message
406 * @minfo: Pointer to Tx/Rx Message management info based on channel type
407 * @xfer: message that was reserved by scmi_xfer_get
409 * After refcount check, possibly release an xfer, clearing the token slot,
410 * removing xfer from @pending_xfers and putting it back into free_xfers.
412 * This holds a spinlock to maintain integrity of internal data structures.
415 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
419 spin_lock_irqsave(&minfo->xfer_lock, flags);
420 if (refcount_dec_and_test(&xfer->users)) {
422 scmi_xfer_token_clear(minfo, xfer);
423 hash_del(&xfer->node);
424 xfer->pending = false;
426 hlist_add_head(&xfer->node, &minfo->free_xfers);
428 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
432 * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
434 * @minfo: Pointer to Tx/Rx Message management info based on channel type
435 * @xfer_id: Token ID to lookup in @pending_xfers
437 * Refcounting is untouched.
439 * Context: Assumes to be called with @xfer_lock already acquired.
441 * Return: A valid xfer on Success or error otherwise
443 static struct scmi_xfer *
444 scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
446 struct scmi_xfer *xfer = NULL;
448 if (test_bit(xfer_id, minfo->xfer_alloc_table))
449 xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
451 return xfer ?: ERR_PTR(-EINVAL);
455 * scmi_msg_response_validate - Validate message type against state of related
458 * @cinfo: A reference to the channel descriptor.
459 * @msg_type: Message type to check
460 * @xfer: A reference to the xfer to validate against @msg_type
462 * This function checks if @msg_type is congruent with the current state of
463 * a pending @xfer; if an asynchronous delayed response is received before the
464 * related synchronous response (Out-of-Order Delayed Response) the missing
465 * synchronous response is assumed to be OK and completed, carrying on with the
466 * Delayed Response: this is done to address the case in which the underlying
467 * SCMI transport can deliver such out-of-order responses.
469 * Context: Assumes to be called with xfer->lock already acquired.
471 * Return: 0 on Success, error otherwise
473 static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
475 struct scmi_xfer *xfer)
478 * Even if a response was indeed expected on this slot at this point,
479 * a buggy platform could wrongly reply feeding us an unexpected
480 * delayed response we're not prepared to handle: bail-out safely
483 if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
485 "Delayed Response for %d not expected! Buggy F/W ?\n",
490 switch (xfer->state) {
491 case SCMI_XFER_SENT_OK:
492 if (msg_type == MSG_TYPE_DELAYED_RESP) {
494 * Delayed Response expected but delivered earlier.
495 * Assume message RESPONSE was OK and skip state.
497 xfer->hdr.status = SCMI_SUCCESS;
498 xfer->state = SCMI_XFER_RESP_OK;
499 complete(&xfer->done);
501 "Received valid OoO Delayed Response for %d\n",
505 case SCMI_XFER_RESP_OK:
506 if (msg_type != MSG_TYPE_DELAYED_RESP)
509 case SCMI_XFER_DRESP_OK:
510 /* No further message expected once in SCMI_XFER_DRESP_OK */
518 * scmi_xfer_state_update - Update xfer state
520 * @xfer: A reference to the xfer to update
521 * @msg_type: Type of message being processed.
523 * Note that this message is assumed to have been already successfully validated
524 * by @scmi_msg_response_validate(), so here we just update the state.
526 * Context: Assumes to be called on an xfer exclusively acquired using the
529 static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
531 xfer->hdr.type = msg_type;
533 /* Unknown command types were already discarded earlier */
534 if (xfer->hdr.type == MSG_TYPE_COMMAND)
535 xfer->state = SCMI_XFER_RESP_OK;
537 xfer->state = SCMI_XFER_DRESP_OK;
540 static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
544 ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
546 return ret == SCMI_XFER_FREE;
550 * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
552 * @cinfo: A reference to the channel descriptor.
553 * @msg_hdr: A message header to use as lookup key
555 * When a valid xfer is found for the sequence number embedded in the provided
556 * msg_hdr, reference counting is properly updated and exclusive access to this
557 * xfer is granted till released with @scmi_xfer_command_release.
559 * Return: A valid @xfer on Success or error otherwise.
561 static inline struct scmi_xfer *
562 scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
566 struct scmi_xfer *xfer;
567 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
568 struct scmi_xfers_info *minfo = &info->tx_minfo;
569 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
570 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
572 /* Are we even expecting this? */
573 spin_lock_irqsave(&minfo->xfer_lock, flags);
574 xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
577 "Message for %d type %d is not expected!\n",
579 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
582 refcount_inc(&xfer->users);
583 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
585 spin_lock_irqsave(&xfer->lock, flags);
586 ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
588 * If a pending xfer was found which was also in a congruent state with
589 * the received message, acquire exclusive access to it setting the busy
591 * Spins only on the rare limit condition of concurrent reception of
592 * RESP and DRESP for the same xfer.
595 spin_until_cond(scmi_xfer_acquired(xfer));
596 scmi_xfer_state_update(xfer, msg_type);
598 spin_unlock_irqrestore(&xfer->lock, flags);
602 "Invalid message type:%d for %d - HDR:0x%X state:%d\n",
603 msg_type, xfer_id, msg_hdr, xfer->state);
604 /* On error the refcount incremented above has to be dropped */
605 __scmi_xfer_put(minfo, xfer);
606 xfer = ERR_PTR(-EINVAL);
612 static inline void scmi_xfer_command_release(struct scmi_info *info,
613 struct scmi_xfer *xfer)
615 atomic_set(&xfer->busy, SCMI_XFER_FREE);
616 __scmi_xfer_put(&info->tx_minfo, xfer);
619 static inline void scmi_clear_channel(struct scmi_info *info,
620 struct scmi_chan_info *cinfo)
622 if (info->desc->ops->clear_channel)
623 info->desc->ops->clear_channel(cinfo);
626 static inline bool is_polling_required(struct scmi_chan_info *cinfo,
627 struct scmi_info *info)
629 return cinfo->no_completion_irq || info->desc->force_polling;
632 static inline bool is_transport_polling_capable(struct scmi_info *info)
634 return info->desc->ops->poll_done ||
635 info->desc->sync_cmds_completed_on_ret;
638 static inline bool is_polling_enabled(struct scmi_chan_info *cinfo,
639 struct scmi_info *info)
641 return is_polling_required(cinfo, info) &&
642 is_transport_polling_capable(info);
645 static void scmi_handle_notification(struct scmi_chan_info *cinfo,
646 u32 msg_hdr, void *priv)
648 struct scmi_xfer *xfer;
649 struct device *dev = cinfo->dev;
650 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
651 struct scmi_xfers_info *minfo = &info->rx_minfo;
654 ts = ktime_get_boottime();
655 xfer = scmi_xfer_get(cinfo->handle, minfo, false);
657 dev_err(dev, "failed to get free message slot (%ld)\n",
659 scmi_clear_channel(info, cinfo);
663 unpack_scmi_header(msg_hdr, &xfer->hdr);
665 /* Ensure order between xfer->priv store and following ops */
666 smp_store_mb(xfer->priv, priv);
667 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
670 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id, "NOTI",
671 xfer->hdr.seq, xfer->hdr.status,
672 xfer->rx.buf, xfer->rx.len);
674 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
675 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
677 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
678 xfer->hdr.protocol_id, xfer->hdr.seq,
679 MSG_TYPE_NOTIFICATION);
681 __scmi_xfer_put(minfo, xfer);
683 scmi_clear_channel(info, cinfo);
686 static void scmi_handle_response(struct scmi_chan_info *cinfo,
687 u32 msg_hdr, void *priv)
689 struct scmi_xfer *xfer;
690 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
692 xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
694 if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP)
695 scmi_clear_channel(info, cinfo);
699 /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
700 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
701 xfer->rx.len = info->desc->max_msg_size;
704 /* Ensure order between xfer->priv store and following ops */
705 smp_store_mb(xfer->priv, priv);
706 info->desc->ops->fetch_response(cinfo, xfer);
708 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id,
709 xfer->hdr.type == MSG_TYPE_DELAYED_RESP ?
711 xfer->hdr.seq, xfer->hdr.status,
712 xfer->rx.buf, xfer->rx.len);
714 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
715 xfer->hdr.protocol_id, xfer->hdr.seq,
718 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
719 scmi_clear_channel(info, cinfo);
720 complete(xfer->async_done);
722 complete(&xfer->done);
725 scmi_xfer_command_release(info, xfer);
729 * scmi_rx_callback() - callback for receiving messages
731 * @cinfo: SCMI channel info
732 * @msg_hdr: Message header
733 * @priv: Transport specific private data.
735 * Processes one received message to appropriate transfer information and
736 * signals completion of the transfer.
738 * NOTE: This function will be invoked in IRQ context, hence should be
739 * as optimal as possible.
741 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv)
743 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
746 case MSG_TYPE_NOTIFICATION:
747 scmi_handle_notification(cinfo, msg_hdr, priv);
749 case MSG_TYPE_COMMAND:
750 case MSG_TYPE_DELAYED_RESP:
751 scmi_handle_response(cinfo, msg_hdr, priv);
754 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
760 * xfer_put() - Release a transmit message
762 * @ph: Pointer to SCMI protocol handle
763 * @xfer: message that was reserved by xfer_get_init
765 static void xfer_put(const struct scmi_protocol_handle *ph,
766 struct scmi_xfer *xfer)
768 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
769 struct scmi_info *info = handle_to_scmi_info(pi->handle);
771 __scmi_xfer_put(&info->tx_minfo, xfer);
774 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
775 struct scmi_xfer *xfer, ktime_t stop)
777 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
780 * Poll also on xfer->done so that polling can be forcibly terminated
781 * in case of out-of-order receptions of delayed responses
783 return info->desc->ops->poll_done(cinfo, xfer) ||
784 try_wait_for_completion(&xfer->done) ||
785 ktime_after(ktime_get(), stop);
789 * scmi_wait_for_message_response - An helper to group all the possible ways of
790 * waiting for a synchronous message response.
792 * @cinfo: SCMI channel info
793 * @xfer: Reference to the transfer being waited for.
795 * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
796 * configuration flags like xfer->hdr.poll_completion.
798 * Return: 0 on Success, error otherwise.
800 static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo,
801 struct scmi_xfer *xfer)
803 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
804 struct device *dev = info->dev;
805 int ret = 0, timeout_ms = info->desc->max_rx_timeout_ms;
807 trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id,
808 xfer->hdr.protocol_id, xfer->hdr.seq,
810 xfer->hdr.poll_completion);
812 if (xfer->hdr.poll_completion) {
814 * Real polling is needed only if transport has NOT declared
815 * itself to support synchronous commands replies.
817 if (!info->desc->sync_cmds_completed_on_ret) {
819 * Poll on xfer using transport provided .poll_done();
820 * assumes no completion interrupt was available.
822 ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms);
824 spin_until_cond(scmi_xfer_done_no_timeout(cinfo,
826 if (ktime_after(ktime_get(), stop)) {
828 "timed out in resp(caller: %pS) - polling\n",
838 * Do not fetch_response if an out-of-order delayed
839 * response is being processed.
841 spin_lock_irqsave(&xfer->lock, flags);
842 if (xfer->state == SCMI_XFER_SENT_OK) {
843 info->desc->ops->fetch_response(cinfo, xfer);
844 xfer->state = SCMI_XFER_RESP_OK;
846 spin_unlock_irqrestore(&xfer->lock, flags);
848 /* Trace polled replies. */
849 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id,
851 xfer->hdr.seq, xfer->hdr.status,
852 xfer->rx.buf, xfer->rx.len);
855 /* And we wait for the response. */
856 if (!wait_for_completion_timeout(&xfer->done,
857 msecs_to_jiffies(timeout_ms))) {
858 dev_err(dev, "timed out in resp(caller: %pS)\n",
868 * do_xfer() - Do one transfer
870 * @ph: Pointer to SCMI protocol handle
871 * @xfer: Transfer to initiate and wait for response
873 * Return: -ETIMEDOUT in case of no response, if transmit error,
874 * return corresponding error, else if all goes well,
877 static int do_xfer(const struct scmi_protocol_handle *ph,
878 struct scmi_xfer *xfer)
881 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
882 struct scmi_info *info = handle_to_scmi_info(pi->handle);
883 struct device *dev = info->dev;
884 struct scmi_chan_info *cinfo;
886 /* Check for polling request on custom command xfers at first */
887 if (xfer->hdr.poll_completion && !is_transport_polling_capable(info)) {
889 "Polling mode is not supported by transport.\n");
893 cinfo = idr_find(&info->tx_idr, pi->proto->id);
894 if (unlikely(!cinfo))
897 /* True ONLY if also supported by transport. */
898 if (is_polling_enabled(cinfo, info))
899 xfer->hdr.poll_completion = true;
902 * Initialise protocol id now from protocol handle to avoid it being
903 * overridden by mistake (or malice) by the protocol code mangling with
904 * the scmi_xfer structure prior to this.
906 xfer->hdr.protocol_id = pi->proto->id;
907 reinit_completion(&xfer->done);
909 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
910 xfer->hdr.protocol_id, xfer->hdr.seq,
911 xfer->hdr.poll_completion);
913 xfer->state = SCMI_XFER_SENT_OK;
915 * Even though spinlocking is not needed here since no race is possible
916 * on xfer->state due to the monotonically increasing tokens allocation,
917 * we must anyway ensure xfer->state initialization is not re-ordered
918 * after the .send_message() to be sure that on the RX path an early
919 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
923 ret = info->desc->ops->send_message(cinfo, xfer);
925 dev_dbg(dev, "Failed to send message %d\n", ret);
929 trace_scmi_msg_dump(xfer->hdr.protocol_id, xfer->hdr.id, "CMND",
930 xfer->hdr.seq, xfer->hdr.status,
931 xfer->tx.buf, xfer->tx.len);
933 ret = scmi_wait_for_message_response(cinfo, xfer);
934 if (!ret && xfer->hdr.status)
935 ret = scmi_to_linux_errno(xfer->hdr.status);
937 if (info->desc->ops->mark_txdone)
938 info->desc->ops->mark_txdone(cinfo, ret, xfer);
940 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
941 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
946 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
947 struct scmi_xfer *xfer)
949 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
950 struct scmi_info *info = handle_to_scmi_info(pi->handle);
952 xfer->rx.len = info->desc->max_msg_size;
955 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
958 * do_xfer_with_response() - Do one transfer and wait until the delayed
959 * response is received
961 * @ph: Pointer to SCMI protocol handle
962 * @xfer: Transfer to initiate and wait for response
964 * Using asynchronous commands in atomic/polling mode should be avoided since
965 * it could cause long busy-waiting here, so ignore polling for the delayed
966 * response and WARN if it was requested for this command transaction since
967 * upper layers should refrain from issuing such kind of requests.
969 * The only other option would have been to refrain from using any asynchronous
970 * command even if made available, when an atomic transport is detected, and
971 * instead forcibly use the synchronous version (thing that can be easily
972 * attained at the protocol layer), but this would also have led to longer
973 * stalls of the channel for synchronous commands and possibly timeouts.
974 * (in other words there is usually a good reason if a platform provides an
975 * asynchronous version of a command and we should prefer to use it...just not
976 * when using atomic/polling mode)
978 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
979 * return corresponding error, else if all goes well, return 0.
981 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
982 struct scmi_xfer *xfer)
984 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
985 DECLARE_COMPLETION_ONSTACK(async_response);
987 xfer->async_done = &async_response;
990 * Delayed responses should not be polled, so an async command should
991 * not have been used when requiring an atomic/poll context; WARN and
992 * perform instead a sleeping wait.
993 * (Note Async + IgnoreDelayedResponses are sent via do_xfer)
995 WARN_ON_ONCE(xfer->hdr.poll_completion);
997 ret = do_xfer(ph, xfer);
999 if (!wait_for_completion_timeout(xfer->async_done, timeout)) {
1001 "timed out in delayed resp(caller: %pS)\n",
1004 } else if (xfer->hdr.status) {
1005 ret = scmi_to_linux_errno(xfer->hdr.status);
1009 xfer->async_done = NULL;
1014 * xfer_get_init() - Allocate and initialise one message for transmit
1016 * @ph: Pointer to SCMI protocol handle
1017 * @msg_id: Message identifier
1018 * @tx_size: transmit message size
1019 * @rx_size: receive message size
1020 * @p: pointer to the allocated and initialised message
1022 * This function allocates the message using @scmi_xfer_get and
1023 * initialise the header.
1025 * Return: 0 if all went fine with @p pointing to message, else
1026 * corresponding error.
1028 static int xfer_get_init(const struct scmi_protocol_handle *ph,
1029 u8 msg_id, size_t tx_size, size_t rx_size,
1030 struct scmi_xfer **p)
1033 struct scmi_xfer *xfer;
1034 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1035 struct scmi_info *info = handle_to_scmi_info(pi->handle);
1036 struct scmi_xfers_info *minfo = &info->tx_minfo;
1037 struct device *dev = info->dev;
1039 /* Ensure we have sane transfer sizes */
1040 if (rx_size > info->desc->max_msg_size ||
1041 tx_size > info->desc->max_msg_size)
1044 xfer = scmi_xfer_get(pi->handle, minfo, true);
1046 ret = PTR_ERR(xfer);
1047 dev_err(dev, "failed to get free message slot(%d)\n", ret);
1051 xfer->tx.len = tx_size;
1052 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
1053 xfer->hdr.type = MSG_TYPE_COMMAND;
1054 xfer->hdr.id = msg_id;
1055 xfer->hdr.poll_completion = false;
1063 * version_get() - command to get the revision of the SCMI entity
1065 * @ph: Pointer to SCMI protocol handle
1066 * @version: Holds returned version of protocol.
1068 * Updates the SCMI information in the internal data structure.
1070 * Return: 0 if all went fine, else return appropriate error.
1072 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
1076 struct scmi_xfer *t;
1078 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
1082 ret = do_xfer(ph, t);
1084 rev_info = t->rx.buf;
1085 *version = le32_to_cpu(*rev_info);
1093 * scmi_set_protocol_priv - Set protocol specific data at init time
1095 * @ph: A reference to the protocol handle.
1096 * @priv: The private data to set.
1098 * Return: 0 on Success
1100 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
1103 struct scmi_protocol_instance *pi = ph_to_pi(ph);
1111 * scmi_get_protocol_priv - Set protocol specific data at init time
1113 * @ph: A reference to the protocol handle.
1115 * Return: Protocol private data if any was set.
1117 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
1119 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1124 static const struct scmi_xfer_ops xfer_ops = {
1125 .version_get = version_get,
1126 .xfer_get_init = xfer_get_init,
1127 .reset_rx_to_maxsz = reset_rx_to_maxsz,
1129 .do_xfer_with_response = do_xfer_with_response,
1130 .xfer_put = xfer_put,
1133 struct scmi_msg_resp_domain_name_get {
1135 u8 name[SCMI_MAX_STR_SIZE];
1139 * scmi_common_extended_name_get - Common helper to get extended resources name
1140 * @ph: A protocol handle reference.
1141 * @cmd_id: The specific command ID to use.
1142 * @res_id: The specific resource ID to use.
1143 * @name: A pointer to the preallocated area where the retrieved name will be
1144 * stored as a NULL terminated string.
1145 * @len: The len in bytes of the @name char array.
1147 * Return: 0 on Succcess
1149 static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph,
1150 u8 cmd_id, u32 res_id, char *name,
1154 struct scmi_xfer *t;
1155 struct scmi_msg_resp_domain_name_get *resp;
1157 ret = ph->xops->xfer_get_init(ph, cmd_id, sizeof(res_id),
1162 put_unaligned_le32(res_id, t->tx.buf);
1165 ret = ph->xops->do_xfer(ph, t);
1167 strscpy(name, resp->name, len);
1169 ph->xops->xfer_put(ph, t);
1173 "Failed to get extended name - id:%u (ret:%d). Using %s\n",
1179 * struct scmi_iterator - Iterator descriptor
1180 * @msg: A reference to the message TX buffer; filled by @prepare_message with
1181 * a proper custom command payload for each multi-part command request.
1182 * @resp: A reference to the response RX buffer; used by @update_state and
1183 * @process_response to parse the multi-part replies.
1184 * @t: A reference to the underlying xfer initialized and used transparently by
1185 * the iterator internal routines.
1186 * @ph: A reference to the associated protocol handle to be used.
1187 * @ops: A reference to the custom provided iterator operations.
1188 * @state: The current iterator state; used and updated in turn by the iterators
1189 * internal routines and by the caller-provided @scmi_iterator_ops.
1190 * @priv: A reference to optional private data as provided by the caller and
1191 * passed back to the @@scmi_iterator_ops.
1193 struct scmi_iterator {
1196 struct scmi_xfer *t;
1197 const struct scmi_protocol_handle *ph;
1198 struct scmi_iterator_ops *ops;
1199 struct scmi_iterator_state state;
1203 static void *scmi_iterator_init(const struct scmi_protocol_handle *ph,
1204 struct scmi_iterator_ops *ops,
1205 unsigned int max_resources, u8 msg_id,
1206 size_t tx_size, void *priv)
1209 struct scmi_iterator *i;
1211 i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL);
1213 return ERR_PTR(-ENOMEM);
1219 ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t);
1221 devm_kfree(ph->dev, i);
1222 return ERR_PTR(ret);
1225 i->state.max_resources = max_resources;
1226 i->msg = i->t->tx.buf;
1227 i->resp = i->t->rx.buf;
1232 static int scmi_iterator_run(void *iter)
1235 struct scmi_iterator_ops *iops;
1236 const struct scmi_protocol_handle *ph;
1237 struct scmi_iterator_state *st;
1238 struct scmi_iterator *i = iter;
1240 if (!i || !i->ops || !i->ph)
1248 iops->prepare_message(i->msg, st->desc_index, i->priv);
1249 ret = ph->xops->do_xfer(ph, i->t);
1253 st->rx_len = i->t->rx.len;
1254 ret = iops->update_state(st, i->resp, i->priv);
1258 if (st->num_returned > st->max_resources - st->desc_index) {
1260 "No. of resources can't exceed %d\n",
1266 for (st->loop_idx = 0; st->loop_idx < st->num_returned;
1268 ret = iops->process_response(ph, i->resp, st, i->priv);
1273 st->desc_index += st->num_returned;
1274 ph->xops->reset_rx_to_maxsz(ph, i->t);
1276 * check for both returned and remaining to avoid infinite
1277 * loop due to buggy firmware
1279 } while (st->num_returned && st->num_remaining);
1282 /* Finalize and destroy iterator */
1283 ph->xops->xfer_put(ph, i->t);
1284 devm_kfree(ph->dev, i);
1289 struct scmi_msg_get_fc_info {
1294 struct scmi_msg_resp_desc_fc {
1296 #define SUPPORTS_DOORBELL(x) ((x) & BIT(0))
1297 #define DOORBELL_REG_WIDTH(x) FIELD_GET(GENMASK(2, 1), (x))
1299 __le32 chan_addr_low;
1300 __le32 chan_addr_high;
1303 __le32 db_addr_high;
1304 __le32 db_set_lmask;
1305 __le32 db_set_hmask;
1306 __le32 db_preserve_lmask;
1307 __le32 db_preserve_hmask;
1311 scmi_common_fastchannel_init(const struct scmi_protocol_handle *ph,
1312 u8 describe_id, u32 message_id, u32 valid_size,
1313 u32 domain, void __iomem **p_addr,
1314 struct scmi_fc_db_info **p_db)
1321 struct scmi_xfer *t;
1322 struct scmi_fc_db_info *db = NULL;
1323 struct scmi_msg_get_fc_info *info;
1324 struct scmi_msg_resp_desc_fc *resp;
1325 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1332 ret = ph->xops->xfer_get_init(ph, describe_id,
1333 sizeof(*info), sizeof(*resp), &t);
1338 info->domain = cpu_to_le32(domain);
1339 info->message_id = cpu_to_le32(message_id);
1342 * Bail out on error leaving fc_info addresses zeroed; this includes
1343 * the case in which the requested domain/message_id does NOT support
1344 * fastchannels at all.
1346 ret = ph->xops->do_xfer(ph, t);
1351 flags = le32_to_cpu(resp->attr);
1352 size = le32_to_cpu(resp->chan_size);
1353 if (size != valid_size) {
1358 phys_addr = le32_to_cpu(resp->chan_addr_low);
1359 phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32;
1360 addr = devm_ioremap(ph->dev, phys_addr, size);
1362 ret = -EADDRNOTAVAIL;
1368 if (p_db && SUPPORTS_DOORBELL(flags)) {
1369 db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL);
1375 size = 1 << DOORBELL_REG_WIDTH(flags);
1376 phys_addr = le32_to_cpu(resp->db_addr_low);
1377 phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32;
1378 addr = devm_ioremap(ph->dev, phys_addr, size);
1380 ret = -EADDRNOTAVAIL;
1386 db->set = le32_to_cpu(resp->db_set_lmask);
1387 db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32;
1388 db->mask = le32_to_cpu(resp->db_preserve_lmask);
1389 db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32;
1394 ph->xops->xfer_put(ph, t);
1397 "Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n",
1398 pi->proto->id, message_id, domain);
1403 devm_kfree(ph->dev, db);
1409 ph->xops->xfer_put(ph, t);
1413 "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n",
1414 pi->proto->id, message_id, domain, ret);
1417 #define SCMI_PROTO_FC_RING_DB(w) \
1422 val = ioread##w(db->addr) & db->mask; \
1423 iowrite##w((u##w)db->set | val, db->addr); \
1426 static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db)
1428 if (!db || !db->addr)
1432 SCMI_PROTO_FC_RING_DB(8);
1433 else if (db->width == 2)
1434 SCMI_PROTO_FC_RING_DB(16);
1435 else if (db->width == 4)
1436 SCMI_PROTO_FC_RING_DB(32);
1437 else /* db->width == 8 */
1439 SCMI_PROTO_FC_RING_DB(64);
1445 val = ioread64_hi_lo(db->addr) & db->mask;
1446 iowrite64_hi_lo(db->set | val, db->addr);
1451 static const struct scmi_proto_helpers_ops helpers_ops = {
1452 .extended_name_get = scmi_common_extended_name_get,
1453 .iter_response_init = scmi_iterator_init,
1454 .iter_response_run = scmi_iterator_run,
1455 .fastchannel_init = scmi_common_fastchannel_init,
1456 .fastchannel_db_ring = scmi_common_fastchannel_db_ring,
1460 * scmi_revision_area_get - Retrieve version memory area.
1462 * @ph: A reference to the protocol handle.
1464 * A helper to grab the version memory area reference during SCMI Base protocol
1467 * Return: A reference to the version memory area associated to the SCMI
1468 * instance underlying this protocol handle.
1470 struct scmi_revision_info *
1471 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
1473 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1475 return pi->handle->version;
1479 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
1480 * instance descriptor.
1481 * @info: The reference to the related SCMI instance.
1482 * @proto: The protocol descriptor.
1484 * Allocate a new protocol instance descriptor, using the provided @proto
1485 * description, against the specified SCMI instance @info, and initialize it;
1486 * all resources management is handled via a dedicated per-protocol devres
1489 * Context: Assumes to be called with @protocols_mtx already acquired.
1490 * Return: A reference to a freshly allocated and initialized protocol instance
1491 * or ERR_PTR on failure. On failure the @proto reference is at first
1492 * put using @scmi_protocol_put() before releasing all the devres group.
1494 static struct scmi_protocol_instance *
1495 scmi_alloc_init_protocol_instance(struct scmi_info *info,
1496 const struct scmi_protocol *proto)
1500 struct scmi_protocol_instance *pi;
1501 const struct scmi_handle *handle = &info->handle;
1503 /* Protocol specific devres group */
1504 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
1506 scmi_protocol_put(proto->id);
1510 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
1516 pi->handle = handle;
1517 pi->ph.dev = handle->dev;
1518 pi->ph.xops = &xfer_ops;
1519 pi->ph.hops = &helpers_ops;
1520 pi->ph.set_priv = scmi_set_protocol_priv;
1521 pi->ph.get_priv = scmi_get_protocol_priv;
1522 refcount_set(&pi->users, 1);
1523 /* proto->init is assured NON NULL by scmi_protocol_register */
1524 ret = pi->proto->instance_init(&pi->ph);
1528 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
1530 if (ret != proto->id)
1534 * Warn but ignore events registration errors since we do not want
1535 * to skip whole protocols if their notifications are messed up.
1537 if (pi->proto->events) {
1538 ret = scmi_register_protocol_events(handle, pi->proto->id,
1542 dev_warn(handle->dev,
1543 "Protocol:%X - Events Registration Failed - err:%d\n",
1544 pi->proto->id, ret);
1547 devres_close_group(handle->dev, pi->gid);
1548 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
1553 /* Take care to put the protocol module's owner before releasing all */
1554 scmi_protocol_put(proto->id);
1555 devres_release_group(handle->dev, gid);
1557 return ERR_PTR(ret);
1561 * scmi_get_protocol_instance - Protocol initialization helper.
1562 * @handle: A reference to the SCMI platform instance.
1563 * @protocol_id: The protocol being requested.
1565 * In case the required protocol has never been requested before for this
1566 * instance, allocate and initialize all the needed structures while handling
1567 * resource allocation with a dedicated per-protocol devres subgroup.
1569 * Return: A reference to an initialized protocol instance or error on failure:
1570 * in particular returns -EPROBE_DEFER when the desired protocol could
1573 static struct scmi_protocol_instance * __must_check
1574 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
1576 struct scmi_protocol_instance *pi;
1577 struct scmi_info *info = handle_to_scmi_info(handle);
1579 mutex_lock(&info->protocols_mtx);
1580 pi = idr_find(&info->protocols, protocol_id);
1583 refcount_inc(&pi->users);
1585 const struct scmi_protocol *proto;
1587 /* Fails if protocol not registered on bus */
1588 proto = scmi_protocol_get(protocol_id);
1590 pi = scmi_alloc_init_protocol_instance(info, proto);
1592 pi = ERR_PTR(-EPROBE_DEFER);
1594 mutex_unlock(&info->protocols_mtx);
1600 * scmi_protocol_acquire - Protocol acquire
1601 * @handle: A reference to the SCMI platform instance.
1602 * @protocol_id: The protocol being requested.
1604 * Register a new user for the requested protocol on the specified SCMI
1605 * platform instance, possibly triggering its initialization on first user.
1607 * Return: 0 if protocol was acquired successfully.
1609 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
1611 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
1615 * scmi_protocol_release - Protocol de-initialization helper.
1616 * @handle: A reference to the SCMI platform instance.
1617 * @protocol_id: The protocol being requested.
1619 * Remove one user for the specified protocol and triggers de-initialization
1620 * and resources de-allocation once the last user has gone.
1622 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
1624 struct scmi_info *info = handle_to_scmi_info(handle);
1625 struct scmi_protocol_instance *pi;
1627 mutex_lock(&info->protocols_mtx);
1628 pi = idr_find(&info->protocols, protocol_id);
1632 if (refcount_dec_and_test(&pi->users)) {
1633 void *gid = pi->gid;
1635 if (pi->proto->events)
1636 scmi_deregister_protocol_events(handle, protocol_id);
1638 if (pi->proto->instance_deinit)
1639 pi->proto->instance_deinit(&pi->ph);
1641 idr_remove(&info->protocols, protocol_id);
1643 scmi_protocol_put(protocol_id);
1645 devres_release_group(handle->dev, gid);
1646 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
1651 mutex_unlock(&info->protocols_mtx);
1654 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
1657 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1658 struct scmi_info *info = handle_to_scmi_info(pi->handle);
1660 info->protocols_imp = prot_imp;
1664 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
1667 struct scmi_info *info = handle_to_scmi_info(handle);
1668 struct scmi_revision_info *rev = handle->version;
1670 if (!info->protocols_imp)
1673 for (i = 0; i < rev->num_protocols; i++)
1674 if (info->protocols_imp[i] == prot_id)
1679 struct scmi_protocol_devres {
1680 const struct scmi_handle *handle;
1684 static void scmi_devm_release_protocol(struct device *dev, void *res)
1686 struct scmi_protocol_devres *dres = res;
1688 scmi_protocol_release(dres->handle, dres->protocol_id);
1691 static struct scmi_protocol_instance __must_check *
1692 scmi_devres_protocol_instance_get(struct scmi_device *sdev, u8 protocol_id)
1694 struct scmi_protocol_instance *pi;
1695 struct scmi_protocol_devres *dres;
1697 dres = devres_alloc(scmi_devm_release_protocol,
1698 sizeof(*dres), GFP_KERNEL);
1700 return ERR_PTR(-ENOMEM);
1702 pi = scmi_get_protocol_instance(sdev->handle, protocol_id);
1708 dres->handle = sdev->handle;
1709 dres->protocol_id = protocol_id;
1710 devres_add(&sdev->dev, dres);
1716 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
1717 * @sdev: A reference to an scmi_device whose embedded struct device is to
1718 * be used for devres accounting.
1719 * @protocol_id: The protocol being requested.
1720 * @ph: A pointer reference used to pass back the associated protocol handle.
1722 * Get hold of a protocol accounting for its usage, eventually triggering its
1723 * initialization, and returning the protocol specific operations and related
1724 * protocol handle which will be used as first argument in most of the
1725 * protocols operations methods.
1726 * Being a devres based managed method, protocol hold will be automatically
1727 * released, and possibly de-initialized on last user, once the SCMI driver
1728 * owning the scmi_device is unbound from it.
1730 * Return: A reference to the requested protocol operations or error.
1731 * Must be checked for errors by caller.
1733 static const void __must_check *
1734 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
1735 struct scmi_protocol_handle **ph)
1737 struct scmi_protocol_instance *pi;
1740 return ERR_PTR(-EINVAL);
1742 pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
1748 return pi->proto->ops;
1752 * scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol
1753 * @sdev: A reference to an scmi_device whose embedded struct device is to
1754 * be used for devres accounting.
1755 * @protocol_id: The protocol being requested.
1757 * Get hold of a protocol accounting for its usage, possibly triggering its
1758 * initialization but without getting access to its protocol specific operations
1761 * Being a devres based managed method, protocol hold will be automatically
1762 * released, and possibly de-initialized on last user, once the SCMI driver
1763 * owning the scmi_device is unbound from it.
1765 * Return: 0 on SUCCESS
1767 static int __must_check scmi_devm_protocol_acquire(struct scmi_device *sdev,
1770 struct scmi_protocol_instance *pi;
1772 pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
1779 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
1781 struct scmi_protocol_devres *dres = res;
1783 if (WARN_ON(!dres || !data))
1786 return dres->protocol_id == *((u8 *)data);
1790 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
1791 * @sdev: A reference to an scmi_device whose embedded struct device is to
1792 * be used for devres accounting.
1793 * @protocol_id: The protocol being requested.
1795 * Explicitly release a protocol hold previously obtained calling the above
1796 * @scmi_devm_protocol_get.
1798 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
1802 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
1803 scmi_devm_protocol_match, &protocol_id);
1808 * scmi_is_transport_atomic - Method to check if underlying transport for an
1809 * SCMI instance is configured as atomic.
1811 * @handle: A reference to the SCMI platform instance.
1812 * @atomic_threshold: An optional return value for the system wide currently
1813 * configured threshold for atomic operations.
1815 * Return: True if transport is configured as atomic
1817 static bool scmi_is_transport_atomic(const struct scmi_handle *handle,
1818 unsigned int *atomic_threshold)
1821 struct scmi_info *info = handle_to_scmi_info(handle);
1823 ret = info->desc->atomic_enabled && is_transport_polling_capable(info);
1824 if (ret && atomic_threshold)
1825 *atomic_threshold = info->atomic_threshold;
1831 struct scmi_handle *scmi_handle_get_from_info_unlocked(struct scmi_info *info)
1834 return &info->handle;
1838 * scmi_handle_get() - Get the SCMI handle for a device
1840 * @dev: pointer to device for which we want SCMI handle
1842 * NOTE: The function does not track individual clients of the framework
1843 * and is expected to be maintained by caller of SCMI protocol library.
1844 * scmi_handle_put must be balanced with successful scmi_handle_get
1846 * Return: pointer to handle if successful, NULL on error
1848 struct scmi_handle *scmi_handle_get(struct device *dev)
1850 struct list_head *p;
1851 struct scmi_info *info;
1852 struct scmi_handle *handle = NULL;
1854 mutex_lock(&scmi_list_mutex);
1855 list_for_each(p, &scmi_list) {
1856 info = list_entry(p, struct scmi_info, node);
1857 if (dev->parent == info->dev) {
1858 handle = scmi_handle_get_from_info_unlocked(info);
1862 mutex_unlock(&scmi_list_mutex);
1868 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
1870 * @handle: handle acquired by scmi_handle_get
1872 * NOTE: The function does not track individual clients of the framework
1873 * and is expected to be maintained by caller of SCMI protocol library.
1874 * scmi_handle_put must be balanced with successful scmi_handle_get
1876 * Return: 0 is successfully released
1877 * if null was passed, it returns -EINVAL;
1879 int scmi_handle_put(const struct scmi_handle *handle)
1881 struct scmi_info *info;
1886 info = handle_to_scmi_info(handle);
1887 mutex_lock(&scmi_list_mutex);
1888 if (!WARN_ON(!info->users))
1890 mutex_unlock(&scmi_list_mutex);
1895 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
1896 struct scmi_xfers_info *info)
1899 struct scmi_xfer *xfer;
1900 struct device *dev = sinfo->dev;
1901 const struct scmi_desc *desc = sinfo->desc;
1903 /* Pre-allocated messages, no more than what hdr.seq can support */
1904 if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
1906 "Invalid maximum messages %d, not in range [1 - %lu]\n",
1907 info->max_msg, MSG_TOKEN_MAX);
1911 hash_init(info->pending_xfers);
1913 /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
1914 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(MSG_TOKEN_MAX),
1915 sizeof(long), GFP_KERNEL);
1916 if (!info->xfer_alloc_table)
1920 * Preallocate a number of xfers equal to max inflight messages,
1921 * pre-initialize the buffer pointer to pre-allocated buffers and
1922 * attach all of them to the free list
1924 INIT_HLIST_HEAD(&info->free_xfers);
1925 for (i = 0; i < info->max_msg; i++) {
1926 xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
1930 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
1935 xfer->tx.buf = xfer->rx.buf;
1936 init_completion(&xfer->done);
1937 spin_lock_init(&xfer->lock);
1939 /* Add initialized xfer to the free list */
1940 hlist_add_head(&xfer->node, &info->free_xfers);
1943 spin_lock_init(&info->xfer_lock);
1948 static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
1950 const struct scmi_desc *desc = sinfo->desc;
1952 if (!desc->ops->get_max_msg) {
1953 sinfo->tx_minfo.max_msg = desc->max_msg;
1954 sinfo->rx_minfo.max_msg = desc->max_msg;
1956 struct scmi_chan_info *base_cinfo;
1958 base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
1961 sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
1963 /* RX channel is optional so can be skipped */
1964 base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
1966 sinfo->rx_minfo.max_msg =
1967 desc->ops->get_max_msg(base_cinfo);
1973 static int scmi_xfer_info_init(struct scmi_info *sinfo)
1977 ret = scmi_channels_max_msg_configure(sinfo);
1981 ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
1982 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1983 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1988 static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1989 int prot_id, bool tx)
1992 struct scmi_chan_info *cinfo;
1995 /* Transmit channel is first entry i.e. index 0 */
1997 idr = tx ? &info->tx_idr : &info->rx_idr;
1999 /* check if already allocated, used for multiple device per protocol */
2000 cinfo = idr_find(idr, prot_id);
2004 if (!info->desc->ops->chan_available(dev, idx)) {
2005 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
2006 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
2011 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
2017 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
2021 if (tx && is_polling_required(cinfo, info)) {
2022 if (is_transport_polling_capable(info))
2024 "Enabled polling mode TX channel - prot_id:%d\n",
2028 "Polling mode NOT supported by transport.\n");
2032 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
2033 if (ret != prot_id) {
2034 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
2038 cinfo->handle = &info->handle;
2043 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
2045 int ret = scmi_chan_setup(info, dev, prot_id, true);
2047 if (!ret) /* Rx is optional, hence no error check */
2048 scmi_chan_setup(info, dev, prot_id, false);
2054 * scmi_get_protocol_device - Helper to get/create an SCMI device.
2056 * @np: A device node representing a valid active protocols for the referred
2058 * @info: The referred SCMI instance for which we are getting/creating this
2060 * @prot_id: The protocol ID.
2061 * @name: The device name.
2063 * Referring to the specific SCMI instance identified by @info, this helper
2064 * takes care to return a properly initialized device matching the requested
2065 * @proto_id and @name: if device was still not existent it is created as a
2066 * child of the specified SCMI instance @info and its transport properly
2067 * initialized as usual.
2069 * Return: A properly initialized scmi device, NULL otherwise.
2071 static inline struct scmi_device *
2072 scmi_get_protocol_device(struct device_node *np, struct scmi_info *info,
2073 int prot_id, const char *name)
2075 struct scmi_device *sdev;
2077 /* Already created for this parent SCMI instance ? */
2078 sdev = scmi_child_dev_find(info->dev, prot_id, name);
2082 mutex_lock(&scmi_syspower_mtx);
2083 if (prot_id == SCMI_PROTOCOL_SYSTEM && scmi_syspower_registered) {
2085 "SCMI SystemPower protocol device must be unique !\n");
2086 mutex_unlock(&scmi_syspower_mtx);
2091 pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id);
2093 sdev = scmi_device_create(np, info->dev, prot_id, name);
2095 dev_err(info->dev, "failed to create %d protocol device\n",
2097 mutex_unlock(&scmi_syspower_mtx);
2102 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
2103 dev_err(&sdev->dev, "failed to setup transport\n");
2104 scmi_device_destroy(sdev);
2105 mutex_unlock(&scmi_syspower_mtx);
2110 if (prot_id == SCMI_PROTOCOL_SYSTEM)
2111 scmi_syspower_registered = true;
2113 mutex_unlock(&scmi_syspower_mtx);
2119 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
2120 int prot_id, const char *name)
2122 struct scmi_device *sdev;
2124 sdev = scmi_get_protocol_device(np, info, prot_id, name);
2128 /* setup handle now as the transport is ready */
2129 scmi_set_handle(sdev);
2133 * scmi_create_protocol_devices - Create devices for all pending requests for
2134 * this SCMI instance.
2136 * @np: The device node describing the protocol
2137 * @info: The SCMI instance descriptor
2138 * @prot_id: The protocol ID
2140 * All devices previously requested for this instance (if any) are found and
2141 * created by scanning the proper @&scmi_requested_devices entry.
2143 static void scmi_create_protocol_devices(struct device_node *np,
2144 struct scmi_info *info, int prot_id)
2146 struct list_head *phead;
2148 mutex_lock(&scmi_requested_devices_mtx);
2149 phead = idr_find(&scmi_requested_devices, prot_id);
2151 struct scmi_requested_dev *rdev;
2153 list_for_each_entry(rdev, phead, node)
2154 scmi_create_protocol_device(np, info, prot_id,
2155 rdev->id_table->name);
2157 mutex_unlock(&scmi_requested_devices_mtx);
2161 * scmi_protocol_device_request - Helper to request a device
2163 * @id_table: A protocol/name pair descriptor for the device to be created.
2165 * This helper let an SCMI driver request specific devices identified by the
2166 * @id_table to be created for each active SCMI instance.
2168 * The requested device name MUST NOT be already existent for any protocol;
2169 * at first the freshly requested @id_table is annotated in the IDR table
2170 * @scmi_requested_devices, then a matching device is created for each already
2171 * active SCMI instance. (if any)
2173 * This way the requested device is created straight-away for all the already
2174 * initialized(probed) SCMI instances (handles) and it remains also annotated
2175 * as pending creation if the requesting SCMI driver was loaded before some
2176 * SCMI instance and related transports were available: when such late instance
2177 * is probed, its probe will take care to scan the list of pending requested
2178 * devices and create those on its own (see @scmi_create_protocol_devices and
2179 * its enclosing loop)
2181 * Return: 0 on Success
2183 int scmi_protocol_device_request(const struct scmi_device_id *id_table)
2186 unsigned int id = 0;
2187 struct list_head *head, *phead = NULL;
2188 struct scmi_requested_dev *rdev;
2189 struct scmi_info *info;
2191 pr_debug("Requesting SCMI device (%s) for protocol %x\n",
2192 id_table->name, id_table->protocol_id);
2195 * Search for the matching protocol rdev list and then search
2196 * of any existent equally named device...fails if any duplicate found.
2198 mutex_lock(&scmi_requested_devices_mtx);
2199 idr_for_each_entry(&scmi_requested_devices, head, id) {
2201 /* A list found registered in the IDR is never empty */
2202 rdev = list_first_entry(head, struct scmi_requested_dev,
2204 if (rdev->id_table->protocol_id ==
2205 id_table->protocol_id)
2208 list_for_each_entry(rdev, head, node) {
2209 if (!strcmp(rdev->id_table->name, id_table->name)) {
2210 pr_err("Ignoring duplicate request [%d] %s\n",
2211 rdev->id_table->protocol_id,
2212 rdev->id_table->name);
2220 * No duplicate found for requested id_table, so let's create a new
2221 * requested device entry for this new valid request.
2223 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
2228 rdev->id_table = id_table;
2231 * Append the new requested device table descriptor to the head of the
2232 * related protocol list, eventually creating such head if not already
2236 phead = kzalloc(sizeof(*phead), GFP_KERNEL);
2242 INIT_LIST_HEAD(phead);
2244 ret = idr_alloc(&scmi_requested_devices, (void *)phead,
2245 id_table->protocol_id,
2246 id_table->protocol_id + 1, GFP_KERNEL);
2247 if (ret != id_table->protocol_id) {
2248 pr_err("Failed to save SCMI device - ret:%d\n", ret);
2256 list_add(&rdev->node, phead);
2259 * Now effectively create and initialize the requested device for every
2260 * already initialized SCMI instance which has registered the requested
2261 * protocol as a valid active one: i.e. defined in DT and supported by
2262 * current platform FW.
2264 mutex_lock(&scmi_list_mutex);
2265 list_for_each_entry(info, &scmi_list, node) {
2266 struct device_node *child;
2268 child = idr_find(&info->active_protocols,
2269 id_table->protocol_id);
2271 struct scmi_device *sdev;
2273 sdev = scmi_get_protocol_device(child, info,
2274 id_table->protocol_id,
2276 /* Set handle if not already set: device existed */
2277 if (sdev && !sdev->handle)
2279 scmi_handle_get_from_info_unlocked(info);
2282 "Failed. SCMI protocol %d not active.\n",
2283 id_table->protocol_id);
2286 mutex_unlock(&scmi_list_mutex);
2289 mutex_unlock(&scmi_requested_devices_mtx);
2295 * scmi_protocol_device_unrequest - Helper to unrequest a device
2297 * @id_table: A protocol/name pair descriptor for the device to be unrequested.
2299 * An helper to let an SCMI driver release its request about devices; note that
2300 * devices are created and initialized once the first SCMI driver request them
2301 * but they destroyed only on SCMI core unloading/unbinding.
2303 * The current SCMI transport layer uses such devices as internal references and
2304 * as such they could be shared as same transport between multiple drivers so
2305 * that cannot be safely destroyed till the whole SCMI stack is removed.
2306 * (unless adding further burden of refcounting.)
2308 void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
2310 struct list_head *phead;
2312 pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
2313 id_table->name, id_table->protocol_id);
2315 mutex_lock(&scmi_requested_devices_mtx);
2316 phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
2318 struct scmi_requested_dev *victim, *tmp;
2320 list_for_each_entry_safe(victim, tmp, phead, node) {
2321 if (!strcmp(victim->id_table->name, id_table->name)) {
2322 list_del(&victim->node);
2328 if (list_empty(phead)) {
2329 idr_remove(&scmi_requested_devices,
2330 id_table->protocol_id);
2334 mutex_unlock(&scmi_requested_devices_mtx);
2337 static int scmi_cleanup_txrx_channels(struct scmi_info *info)
2340 struct idr *idr = &info->tx_idr;
2342 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
2343 idr_destroy(&info->tx_idr);
2345 idr = &info->rx_idr;
2346 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
2347 idr_destroy(&info->rx_idr);
2352 static int scmi_probe(struct platform_device *pdev)
2355 struct scmi_handle *handle;
2356 const struct scmi_desc *desc;
2357 struct scmi_info *info;
2358 struct device *dev = &pdev->dev;
2359 struct device_node *child, *np = dev->of_node;
2361 desc = of_device_get_match_data(dev);
2365 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
2371 INIT_LIST_HEAD(&info->node);
2372 idr_init(&info->protocols);
2373 mutex_init(&info->protocols_mtx);
2374 idr_init(&info->active_protocols);
2376 platform_set_drvdata(pdev, info);
2377 idr_init(&info->tx_idr);
2378 idr_init(&info->rx_idr);
2380 handle = &info->handle;
2381 handle->dev = info->dev;
2382 handle->version = &info->version;
2383 handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
2384 handle->devm_protocol_get = scmi_devm_protocol_get;
2385 handle->devm_protocol_put = scmi_devm_protocol_put;
2387 /* System wide atomic threshold for atomic ops .. if any */
2388 if (!of_property_read_u32(np, "atomic-threshold-us",
2389 &info->atomic_threshold))
2391 "SCMI System wide atomic threshold set to %d us\n",
2392 info->atomic_threshold);
2393 handle->is_transport_atomic = scmi_is_transport_atomic;
2395 if (desc->ops->link_supplier) {
2396 ret = desc->ops->link_supplier(dev);
2401 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
2405 ret = scmi_xfer_info_init(info);
2407 goto clear_txrx_setup;
2409 if (scmi_notification_init(handle))
2410 dev_err(dev, "SCMI Notifications NOT available.\n");
2412 if (info->desc->atomic_enabled && !is_transport_polling_capable(info))
2414 "Transport is not polling capable. Atomic mode not supported.\n");
2417 * Trigger SCMI Base protocol initialization.
2418 * It's mandatory and won't be ever released/deinit until the
2419 * SCMI stack is shutdown/unloaded as a whole.
2421 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
2423 dev_err(dev, "unable to communicate with SCMI\n");
2424 goto notification_exit;
2427 mutex_lock(&scmi_list_mutex);
2428 list_add_tail(&info->node, &scmi_list);
2429 mutex_unlock(&scmi_list_mutex);
2431 for_each_available_child_of_node(np, child) {
2434 if (of_property_read_u32(child, "reg", &prot_id))
2437 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
2438 dev_err(dev, "Out of range protocol %d\n", prot_id);
2440 if (!scmi_is_protocol_implemented(handle, prot_id)) {
2441 dev_err(dev, "SCMI protocol %d not implemented\n",
2447 * Save this valid DT protocol descriptor amongst
2448 * @active_protocols for this SCMI instance/
2450 ret = idr_alloc(&info->active_protocols, child,
2451 prot_id, prot_id + 1, GFP_KERNEL);
2452 if (ret != prot_id) {
2453 dev_err(dev, "SCMI protocol %d already activated. Skip\n",
2459 scmi_create_protocol_devices(child, info, prot_id);
2465 scmi_notification_exit(&info->handle);
2467 scmi_cleanup_txrx_channels(info);
2471 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
2473 idr_remove(idr, id);
2476 static int scmi_remove(struct platform_device *pdev)
2479 struct scmi_info *info = platform_get_drvdata(pdev);
2480 struct device_node *child;
2482 mutex_lock(&scmi_list_mutex);
2486 list_del(&info->node);
2487 mutex_unlock(&scmi_list_mutex);
2492 scmi_notification_exit(&info->handle);
2494 mutex_lock(&info->protocols_mtx);
2495 idr_destroy(&info->protocols);
2496 mutex_unlock(&info->protocols_mtx);
2498 idr_for_each_entry(&info->active_protocols, child, id)
2500 idr_destroy(&info->active_protocols);
2502 /* Safe to free channels since no more users */
2503 return scmi_cleanup_txrx_channels(info);
2506 static ssize_t protocol_version_show(struct device *dev,
2507 struct device_attribute *attr, char *buf)
2509 struct scmi_info *info = dev_get_drvdata(dev);
2511 return sprintf(buf, "%u.%u\n", info->version.major_ver,
2512 info->version.minor_ver);
2514 static DEVICE_ATTR_RO(protocol_version);
2516 static ssize_t firmware_version_show(struct device *dev,
2517 struct device_attribute *attr, char *buf)
2519 struct scmi_info *info = dev_get_drvdata(dev);
2521 return sprintf(buf, "0x%x\n", info->version.impl_ver);
2523 static DEVICE_ATTR_RO(firmware_version);
2525 static ssize_t vendor_id_show(struct device *dev,
2526 struct device_attribute *attr, char *buf)
2528 struct scmi_info *info = dev_get_drvdata(dev);
2530 return sprintf(buf, "%s\n", info->version.vendor_id);
2532 static DEVICE_ATTR_RO(vendor_id);
2534 static ssize_t sub_vendor_id_show(struct device *dev,
2535 struct device_attribute *attr, char *buf)
2537 struct scmi_info *info = dev_get_drvdata(dev);
2539 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
2541 static DEVICE_ATTR_RO(sub_vendor_id);
2543 static struct attribute *versions_attrs[] = {
2544 &dev_attr_firmware_version.attr,
2545 &dev_attr_protocol_version.attr,
2546 &dev_attr_vendor_id.attr,
2547 &dev_attr_sub_vendor_id.attr,
2550 ATTRIBUTE_GROUPS(versions);
2552 /* Each compatible listed below must have descriptor associated with it */
2553 static const struct of_device_id scmi_of_match[] = {
2554 #ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
2555 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
2557 #ifdef CONFIG_ARM_SCMI_TRANSPORT_OPTEE
2558 { .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc },
2560 #ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
2561 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
2563 #ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
2564 { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
2569 MODULE_DEVICE_TABLE(of, scmi_of_match);
2571 static struct platform_driver scmi_driver = {
2574 .of_match_table = scmi_of_match,
2575 .dev_groups = versions_groups,
2577 .probe = scmi_probe,
2578 .remove = scmi_remove,
2582 * __scmi_transports_setup - Common helper to call transport-specific
2583 * .init/.exit code if provided.
2585 * @init: A flag to distinguish between init and exit.
2587 * Note that, if provided, we invoke .init/.exit functions for all the
2588 * transports currently compiled in.
2590 * Return: 0 on Success.
2592 static inline int __scmi_transports_setup(bool init)
2595 const struct of_device_id *trans;
2597 for (trans = scmi_of_match; trans->data; trans++) {
2598 const struct scmi_desc *tdesc = trans->data;
2600 if ((init && !tdesc->transport_init) ||
2601 (!init && !tdesc->transport_exit))
2605 ret = tdesc->transport_init();
2607 tdesc->transport_exit();
2610 pr_err("SCMI transport %s FAILED initialization!\n",
2619 static int __init scmi_transports_init(void)
2621 return __scmi_transports_setup(true);
2624 static void __exit scmi_transports_exit(void)
2626 __scmi_transports_setup(false);
2629 static int __init scmi_driver_init(void)
2633 /* Bail out if no SCMI transport was configured */
2634 if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
2639 /* Initialize any compiled-in transport which provided an init/exit */
2640 ret = scmi_transports_init();
2644 scmi_base_register();
2646 scmi_clock_register();
2647 scmi_perf_register();
2648 scmi_power_register();
2649 scmi_reset_register();
2650 scmi_sensors_register();
2651 scmi_voltage_register();
2652 scmi_system_register();
2653 scmi_powercap_register();
2655 return platform_driver_register(&scmi_driver);
2657 subsys_initcall(scmi_driver_init);
2659 static void __exit scmi_driver_exit(void)
2661 scmi_base_unregister();
2663 scmi_clock_unregister();
2664 scmi_perf_unregister();
2665 scmi_power_unregister();
2666 scmi_reset_unregister();
2667 scmi_sensors_unregister();
2668 scmi_voltage_unregister();
2669 scmi_system_unregister();
2670 scmi_powercap_unregister();
2674 scmi_transports_exit();
2676 platform_driver_unregister(&scmi_driver);
2678 module_exit(scmi_driver_exit);
2680 MODULE_ALIAS("platform:arm-scmi");
2681 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
2682 MODULE_DESCRIPTION("ARM SCMI protocol driver");
2683 MODULE_LICENSE("GPL v2");