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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/export.h>
23 #include <linux/idr.h>
25 #include <linux/io-64-nonatomic-hi-lo.h>
26 #include <linux/kernel.h>
27 #include <linux/ktime.h>
28 #include <linux/hashtable.h>
29 #include <linux/list.h>
30 #include <linux/module.h>
32 #include <linux/platform_device.h>
33 #include <linux/processor.h>
34 #include <linux/refcount.h>
35 #include <linux/slab.h>
36 #include <linux/xarray.h>
43 #define CREATE_TRACE_POINTS
44 #include <trace/events/scmi.h>
46 static DEFINE_IDA(scmi_id);
48 static DEFINE_XARRAY(scmi_protocols);
50 /* List of all SCMI devices active in system */
51 static LIST_HEAD(scmi_list);
52 /* Protection for the entire list */
53 static DEFINE_MUTEX(scmi_list_mutex);
54 /* Track the unique id for the transfers for debug & profiling purpose */
55 static atomic_t transfer_last_id;
57 static struct dentry *scmi_top_dentry;
60 * struct scmi_xfers_info - Structure to manage transfer information
62 * @xfer_alloc_table: Bitmap table for allocated messages.
63 * Index of this bitmap table is also used for message
64 * sequence identifier.
65 * @xfer_lock: Protection for message allocation
66 * @max_msg: Maximum number of messages that can be pending
67 * @free_xfers: A free list for available to use xfers. It is initialized with
68 * a number of xfers equal to the maximum allowed in-flight
70 * @pending_xfers: An hashtable, indexed by msg_hdr.seq, used to keep all the
71 * currently in-flight messages.
73 struct scmi_xfers_info {
74 unsigned long *xfer_alloc_table;
77 struct hlist_head free_xfers;
78 DECLARE_HASHTABLE(pending_xfers, SCMI_PENDING_XFERS_HT_ORDER_SZ);
82 * struct scmi_protocol_instance - Describe an initialized protocol instance.
83 * @handle: Reference to the SCMI handle associated to this protocol instance.
84 * @proto: A reference to the protocol descriptor.
85 * @gid: A reference for per-protocol devres management.
86 * @users: A refcount to track effective users of this protocol.
87 * @priv: Reference for optional protocol private data.
88 * @version: Protocol version supported by the platform as detected at runtime.
89 * @negotiated_version: When the platform supports a newer protocol version,
90 * the agent will try to negotiate with the platform the
91 * usage of the newest version known to it, since
92 * backward compatibility is NOT automatically assured.
93 * This field is NON-zero when a successful negotiation
95 * @ph: An embedded protocol handle that will be passed down to protocol
96 * initialization code to identify this instance.
98 * Each protocol is initialized independently once for each SCMI platform in
99 * which is defined by DT and implemented by the SCMI server fw.
101 struct scmi_protocol_instance {
102 const struct scmi_handle *handle;
103 const struct scmi_protocol *proto;
107 unsigned int version;
108 unsigned int negotiated_version;
109 struct scmi_protocol_handle ph;
112 #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
115 * struct scmi_debug_info - Debug common info
116 * @top_dentry: A reference to the top debugfs dentry
117 * @name: Name of this SCMI instance
118 * @type: Type of this SCMI instance
119 * @is_atomic: Flag to state if the transport of this instance is atomic
121 struct scmi_debug_info {
122 struct dentry *top_dentry;
129 * struct scmi_info - Structure representing a SCMI instance
131 * @id: A sequence number starting from zero identifying this instance
132 * @dev: Device pointer
133 * @desc: SoC description for this instance
134 * @version: SCMI revision information containing protocol version,
135 * implementation version and (sub-)vendor identification.
136 * @handle: Instance of SCMI handle to send to clients
137 * @tx_minfo: Universal Transmit Message management info
138 * @rx_minfo: Universal Receive Message management info
139 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
140 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
141 * @protocols: IDR for protocols' instance descriptors initialized for
142 * this SCMI instance: populated on protocol's first attempted
144 * @protocols_mtx: A mutex to protect protocols instances initialization.
145 * @protocols_imp: List of protocols implemented, currently maximum of
146 * scmi_revision_info.num_protocols elements allocated by the
148 * @active_protocols: IDR storing device_nodes for protocols actually defined
149 * in the DT and confirmed as implemented by fw.
150 * @atomic_threshold: Optional system wide DT-configured threshold, expressed
151 * in microseconds, for atomic operations.
152 * Only SCMI synchronous commands reported by the platform
153 * to have an execution latency lesser-equal to the threshold
154 * should be considered for atomic mode operation: such
155 * decision is finally left up to the SCMI drivers.
156 * @notify_priv: Pointer to private data structure specific to notifications.
158 * @users: Number of users of this instance
159 * @bus_nb: A notifier to listen for device bind/unbind on the scmi bus
160 * @dev_req_nb: A notifier to listen for device request/unrequest on the scmi
162 * @devreq_mtx: A mutex to serialize device creation for this SCMI instance
163 * @dbg: A pointer to debugfs related data (if any)
164 * @raw: An opaque reference handle used by SCMI Raw mode.
169 const struct scmi_desc *desc;
170 struct scmi_revision_info version;
171 struct scmi_handle handle;
172 struct scmi_xfers_info tx_minfo;
173 struct scmi_xfers_info rx_minfo;
176 struct idr protocols;
177 /* Ensure mutual exclusive access to protocols instance array */
178 struct mutex protocols_mtx;
180 struct idr active_protocols;
181 unsigned int atomic_threshold;
183 struct list_head node;
185 struct notifier_block bus_nb;
186 struct notifier_block dev_req_nb;
187 /* Serialize device creation process for this instance */
188 struct mutex devreq_mtx;
189 struct scmi_debug_info *dbg;
193 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
194 #define bus_nb_to_scmi_info(nb) container_of(nb, struct scmi_info, bus_nb)
195 #define req_nb_to_scmi_info(nb) container_of(nb, struct scmi_info, dev_req_nb)
198 scmi_vendor_protocol_signature(unsigned int protocol_id, char *vendor_id,
199 char *sub_vendor_id, u32 impl_ver)
202 unsigned long hash = 0;
204 /* vendor_id/sub_vendor_id guaranteed <= SCMI_SHORT_NAME_MAX_SIZE */
205 signature = kasprintf(GFP_KERNEL, "%02X|%s|%s|0x%08X", protocol_id,
206 vendor_id ?: "", sub_vendor_id ?: "", impl_ver);
212 hash = partial_name_hash(tolower(*p++), hash);
213 hash = end_name_hash(hash);
221 scmi_protocol_key_calculate(int protocol_id, char *vendor_id,
222 char *sub_vendor_id, u32 impl_ver)
224 if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
227 return scmi_vendor_protocol_signature(protocol_id, vendor_id,
228 sub_vendor_id, impl_ver);
231 static const struct scmi_protocol *
232 __scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
233 char *sub_vendor_id, u32 impl_ver)
236 struct scmi_protocol *proto = NULL;
238 key = scmi_protocol_key_calculate(protocol_id, vendor_id,
239 sub_vendor_id, impl_ver);
241 proto = xa_load(&scmi_protocols, key);
246 static const struct scmi_protocol *
247 scmi_vendor_protocol_lookup(int protocol_id, char *vendor_id,
248 char *sub_vendor_id, u32 impl_ver)
250 const struct scmi_protocol *proto = NULL;
252 /* Searching for closest match ...*/
253 proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
254 sub_vendor_id, impl_ver);
258 /* Any match just on vendor/sub_vendor ? */
260 proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
266 /* Any match just on the vendor ? */
268 proto = __scmi_vendor_protocol_lookup(protocol_id, vendor_id,
273 static const struct scmi_protocol *
274 scmi_protocol_get(int protocol_id, struct scmi_revision_info *version)
276 const struct scmi_protocol *proto = NULL;
278 if (protocol_id < SCMI_PROTOCOL_VENDOR_BASE)
279 proto = xa_load(&scmi_protocols, protocol_id);
281 proto = scmi_vendor_protocol_lookup(protocol_id,
283 version->sub_vendor_id,
285 if (!proto || !try_module_get(proto->owner)) {
286 pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id);
290 pr_debug("Found SCMI Protocol 0x%x\n", protocol_id);
292 if (protocol_id >= SCMI_PROTOCOL_VENDOR_BASE)
293 pr_info("Loaded SCMI Vendor Protocol 0x%x - %s %s %X\n",
294 protocol_id, proto->vendor_id ?: "",
295 proto->sub_vendor_id ?: "", proto->impl_ver);
300 static void scmi_protocol_put(const struct scmi_protocol *proto)
303 module_put(proto->owner);
306 static int scmi_vendor_protocol_check(const struct scmi_protocol *proto)
308 if (!proto->vendor_id) {
309 pr_err("missing vendor_id for protocol 0x%x\n", proto->id);
313 if (strlen(proto->vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
314 pr_err("malformed vendor_id for protocol 0x%x\n", proto->id);
318 if (proto->sub_vendor_id &&
319 strlen(proto->sub_vendor_id) >= SCMI_SHORT_NAME_MAX_SIZE) {
320 pr_err("malformed sub_vendor_id for protocol 0x%x\n",
328 int scmi_protocol_register(const struct scmi_protocol *proto)
334 pr_err("invalid protocol\n");
338 if (!proto->instance_init) {
339 pr_err("missing init for protocol 0x%x\n", proto->id);
343 if (proto->id >= SCMI_PROTOCOL_VENDOR_BASE &&
344 scmi_vendor_protocol_check(proto))
348 * Calculate a protocol key to register this protocol with the core;
349 * key value 0 is considered invalid.
351 key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
352 proto->sub_vendor_id,
357 ret = xa_insert(&scmi_protocols, key, (void *)proto, GFP_KERNEL);
359 pr_err("unable to allocate SCMI protocol slot for 0x%x - err %d\n",
364 pr_debug("Registered SCMI Protocol 0x%x\n", proto->id);
368 EXPORT_SYMBOL_GPL(scmi_protocol_register);
370 void scmi_protocol_unregister(const struct scmi_protocol *proto)
374 key = scmi_protocol_key_calculate(proto->id, proto->vendor_id,
375 proto->sub_vendor_id,
380 xa_erase(&scmi_protocols, key);
382 pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id);
384 EXPORT_SYMBOL_GPL(scmi_protocol_unregister);
387 * scmi_create_protocol_devices - Create devices for all pending requests for
388 * this SCMI instance.
390 * @np: The device node describing the protocol
391 * @info: The SCMI instance descriptor
392 * @prot_id: The protocol ID
393 * @name: The optional name of the device to be created: if not provided this
394 * call will lead to the creation of all the devices currently requested
395 * for the specified protocol.
397 static void scmi_create_protocol_devices(struct device_node *np,
398 struct scmi_info *info,
399 int prot_id, const char *name)
401 struct scmi_device *sdev;
403 mutex_lock(&info->devreq_mtx);
404 sdev = scmi_device_create(np, info->dev, prot_id, name);
407 "failed to create device for protocol 0x%X (%s)\n",
409 mutex_unlock(&info->devreq_mtx);
412 static void scmi_destroy_protocol_devices(struct scmi_info *info,
413 int prot_id, const char *name)
415 mutex_lock(&info->devreq_mtx);
416 scmi_device_destroy(info->dev, prot_id, name);
417 mutex_unlock(&info->devreq_mtx);
420 void scmi_notification_instance_data_set(const struct scmi_handle *handle,
423 struct scmi_info *info = handle_to_scmi_info(handle);
425 info->notify_priv = priv;
426 /* Ensure updated protocol private date are visible */
430 void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
432 struct scmi_info *info = handle_to_scmi_info(handle);
434 /* Ensure protocols_private_data has been updated */
436 return info->notify_priv;
440 * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
442 * @minfo: Pointer to Tx/Rx Message management info based on channel type
443 * @xfer: The xfer to act upon
445 * Pick the next unused monotonically increasing token and set it into
446 * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
447 * reuse of freshly completed or timed-out xfers, thus mitigating the risk
448 * of incorrect association of a late and expired xfer with a live in-flight
449 * transaction, both happening to re-use the same token identifier.
451 * Since platform is NOT required to answer our request in-order we should
452 * account for a few rare but possible scenarios:
454 * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
455 * using find_next_zero_bit() starting from candidate next_token bit
457 * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
458 * are plenty of free tokens at start, so try a second pass using
459 * find_next_zero_bit() and starting from 0.
467 * -----------+----------------------------------------------------------
468 * | | |X|X|X| | | | | | ... ... ... ... ... ... ... ... ... ... ...|X|X|
469 * ----------------------------------------------------------------------
473 * Out-of-order pending at start
474 * -----------------------------
476 * |- xfer_id picked, last_token fixed
477 * -----+----------------------------------------------------------------
478 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... ... ...|X| |
479 * ----------------------------------------------------------------------
484 * Out-of-order pending at end
485 * ---------------------------
487 * |- xfer_id picked, last_token fixed
488 * -----+----------------------------------------------------------------
489 * |X|X| | | | |X|X| ... ... ... ... ... ... ... ... ... ... |X|X|X||X|X|
490 * ----------------------------------------------------------------------
494 * Context: Assumes to be called with @xfer_lock already acquired.
496 * Return: 0 on Success or error
498 static int scmi_xfer_token_set(struct scmi_xfers_info *minfo,
499 struct scmi_xfer *xfer)
501 unsigned long xfer_id, next_token;
504 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1]
505 * using the pre-allocated transfer_id as a base.
506 * Note that the global transfer_id is shared across all message types
507 * so there could be holes in the allocated set of monotonic sequence
508 * numbers, but that is going to limit the effectiveness of the
509 * mitigation only in very rare limit conditions.
511 next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1));
513 /* Pick the next available xfer_id >= next_token */
514 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
515 MSG_TOKEN_MAX, next_token);
516 if (xfer_id == MSG_TOKEN_MAX) {
518 * After heavily out-of-order responses, there are no free
519 * tokens ahead, but only at start of xfer_alloc_table so
520 * try again from the beginning.
522 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table,
525 * Something is wrong if we got here since there can be a
526 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages
527 * but we have not found any free token [0, MSG_TOKEN_MAX - 1].
529 if (WARN_ON_ONCE(xfer_id == MSG_TOKEN_MAX))
533 /* Update +/- last_token accordingly if we skipped some hole */
534 if (xfer_id != next_token)
535 atomic_add((int)(xfer_id - next_token), &transfer_last_id);
537 xfer->hdr.seq = (u16)xfer_id;
543 * scmi_xfer_token_clear - Release the token
545 * @minfo: Pointer to Tx/Rx Message management info based on channel type
546 * @xfer: The xfer to act upon
548 static inline void scmi_xfer_token_clear(struct scmi_xfers_info *minfo,
549 struct scmi_xfer *xfer)
551 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
555 * scmi_xfer_inflight_register_unlocked - Register the xfer as in-flight
557 * @xfer: The xfer to register
558 * @minfo: Pointer to Tx/Rx Message management info based on channel type
560 * Note that this helper assumes that the xfer to be registered as in-flight
561 * had been built using an xfer sequence number which still corresponds to a
562 * free slot in the xfer_alloc_table.
564 * Context: Assumes to be called with @xfer_lock already acquired.
567 scmi_xfer_inflight_register_unlocked(struct scmi_xfer *xfer,
568 struct scmi_xfers_info *minfo)
571 set_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
572 hash_add(minfo->pending_xfers, &xfer->node, xfer->hdr.seq);
573 xfer->pending = true;
577 * scmi_xfer_inflight_register - Try to register an xfer as in-flight
579 * @xfer: The xfer to register
580 * @minfo: Pointer to Tx/Rx Message management info based on channel type
582 * Note that this helper does NOT assume anything about the sequence number
583 * that was baked into the provided xfer, so it checks at first if it can
584 * be mapped to a free slot and fails with an error if another xfer with the
585 * same sequence number is currently still registered as in-flight.
587 * Return: 0 on Success or -EBUSY if sequence number embedded in the xfer
588 * could not rbe mapped to a free slot in the xfer_alloc_table.
590 static int scmi_xfer_inflight_register(struct scmi_xfer *xfer,
591 struct scmi_xfers_info *minfo)
596 spin_lock_irqsave(&minfo->xfer_lock, flags);
597 if (!test_bit(xfer->hdr.seq, minfo->xfer_alloc_table))
598 scmi_xfer_inflight_register_unlocked(xfer, minfo);
601 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
607 * scmi_xfer_raw_inflight_register - An helper to register the given xfer as in
608 * flight on the TX channel, if possible.
610 * @handle: Pointer to SCMI entity handle
611 * @xfer: The xfer to register
613 * Return: 0 on Success, error otherwise
615 int scmi_xfer_raw_inflight_register(const struct scmi_handle *handle,
616 struct scmi_xfer *xfer)
618 struct scmi_info *info = handle_to_scmi_info(handle);
620 return scmi_xfer_inflight_register(xfer, &info->tx_minfo);
624 * scmi_xfer_pending_set - Pick a proper sequence number and mark the xfer
625 * as pending in-flight
627 * @xfer: The xfer to act upon
628 * @minfo: Pointer to Tx/Rx Message management info based on channel type
630 * Return: 0 on Success or error otherwise
632 static inline int scmi_xfer_pending_set(struct scmi_xfer *xfer,
633 struct scmi_xfers_info *minfo)
638 spin_lock_irqsave(&minfo->xfer_lock, flags);
639 /* Set a new monotonic token as the xfer sequence number */
640 ret = scmi_xfer_token_set(minfo, xfer);
642 scmi_xfer_inflight_register_unlocked(xfer, minfo);
643 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
649 * scmi_xfer_get() - Allocate one message
651 * @handle: Pointer to SCMI entity handle
652 * @minfo: Pointer to Tx/Rx Message management info based on channel type
654 * Helper function which is used by various message functions that are
655 * exposed to clients of this driver for allocating a message traffic event.
657 * Picks an xfer from the free list @free_xfers (if any available) and perform
658 * a basic initialization.
660 * Note that, at this point, still no sequence number is assigned to the
661 * allocated xfer, nor it is registered as a pending transaction.
663 * The successfully initialized xfer is refcounted.
665 * Context: Holds @xfer_lock while manipulating @free_xfers.
667 * Return: An initialized xfer if all went fine, else pointer error.
669 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
670 struct scmi_xfers_info *minfo)
673 struct scmi_xfer *xfer;
675 spin_lock_irqsave(&minfo->xfer_lock, flags);
676 if (hlist_empty(&minfo->free_xfers)) {
677 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
678 return ERR_PTR(-ENOMEM);
681 /* grab an xfer from the free_list */
682 xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node);
683 hlist_del_init(&xfer->node);
686 * Allocate transfer_id early so that can be used also as base for
687 * monotonic sequence number generation if needed.
689 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
691 refcount_set(&xfer->users, 1);
692 atomic_set(&xfer->busy, SCMI_XFER_FREE);
693 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
699 * scmi_xfer_raw_get - Helper to get a bare free xfer from the TX channel
701 * @handle: Pointer to SCMI entity handle
703 * Note that xfer is taken from the TX channel structures.
705 * Return: A valid xfer on Success, or an error-pointer otherwise
707 struct scmi_xfer *scmi_xfer_raw_get(const struct scmi_handle *handle)
709 struct scmi_xfer *xfer;
710 struct scmi_info *info = handle_to_scmi_info(handle);
712 xfer = scmi_xfer_get(handle, &info->tx_minfo);
714 xfer->flags |= SCMI_XFER_FLAG_IS_RAW;
720 * scmi_xfer_raw_channel_get - Helper to get a reference to the proper channel
721 * to use for a specific protocol_id Raw transaction.
723 * @handle: Pointer to SCMI entity handle
724 * @protocol_id: Identifier of the protocol
726 * Note that in a regular SCMI stack, usually, a protocol has to be defined in
727 * the DT to have an associated channel and be usable; but in Raw mode any
728 * protocol in range is allowed, re-using the Base channel, so as to enable
729 * fuzzing on any protocol without the need of a fully compiled DT.
731 * Return: A reference to the channel to use, or an ERR_PTR
733 struct scmi_chan_info *
734 scmi_xfer_raw_channel_get(const struct scmi_handle *handle, u8 protocol_id)
736 struct scmi_chan_info *cinfo;
737 struct scmi_info *info = handle_to_scmi_info(handle);
739 cinfo = idr_find(&info->tx_idr, protocol_id);
741 if (protocol_id == SCMI_PROTOCOL_BASE)
742 return ERR_PTR(-EINVAL);
743 /* Use Base channel for protocols not defined for DT */
744 cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE);
746 return ERR_PTR(-EINVAL);
747 dev_warn_once(handle->dev,
748 "Using Base channel for protocol 0x%X\n",
756 * __scmi_xfer_put() - Release a message
758 * @minfo: Pointer to Tx/Rx Message management info based on channel type
759 * @xfer: message that was reserved by scmi_xfer_get
761 * After refcount check, possibly release an xfer, clearing the token slot,
762 * removing xfer from @pending_xfers and putting it back into free_xfers.
764 * This holds a spinlock to maintain integrity of internal data structures.
767 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
771 spin_lock_irqsave(&minfo->xfer_lock, flags);
772 if (refcount_dec_and_test(&xfer->users)) {
774 scmi_xfer_token_clear(minfo, xfer);
775 hash_del(&xfer->node);
776 xfer->pending = false;
778 hlist_add_head(&xfer->node, &minfo->free_xfers);
780 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
784 * scmi_xfer_raw_put - Release an xfer that was taken by @scmi_xfer_raw_get
786 * @handle: Pointer to SCMI entity handle
787 * @xfer: A reference to the xfer to put
789 * Note that as with other xfer_put() handlers the xfer is really effectively
790 * released only if there are no more users on the system.
792 void scmi_xfer_raw_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
794 struct scmi_info *info = handle_to_scmi_info(handle);
796 xfer->flags &= ~SCMI_XFER_FLAG_IS_RAW;
797 xfer->flags &= ~SCMI_XFER_FLAG_CHAN_SET;
798 return __scmi_xfer_put(&info->tx_minfo, xfer);
802 * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
804 * @minfo: Pointer to Tx/Rx Message management info based on channel type
805 * @xfer_id: Token ID to lookup in @pending_xfers
807 * Refcounting is untouched.
809 * Context: Assumes to be called with @xfer_lock already acquired.
811 * Return: A valid xfer on Success or error otherwise
813 static struct scmi_xfer *
814 scmi_xfer_lookup_unlocked(struct scmi_xfers_info *minfo, u16 xfer_id)
816 struct scmi_xfer *xfer = NULL;
818 if (test_bit(xfer_id, minfo->xfer_alloc_table))
819 xfer = XFER_FIND(minfo->pending_xfers, xfer_id);
821 return xfer ?: ERR_PTR(-EINVAL);
825 * scmi_bad_message_trace - A helper to trace weird messages
827 * @cinfo: A reference to the channel descriptor on which the message was
829 * @msg_hdr: Message header to track
830 * @err: A specific error code used as a status value in traces.
832 * This helper can be used to trace any kind of weird, incomplete, unexpected,
833 * timed-out message that arrives and as such, can be traced only referring to
834 * the header content, since the payload is missing/unreliable.
836 void scmi_bad_message_trace(struct scmi_chan_info *cinfo, u32 msg_hdr,
837 enum scmi_bad_msg err)
840 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
842 switch (MSG_XTRACT_TYPE(msg_hdr)) {
843 case MSG_TYPE_COMMAND:
846 case MSG_TYPE_DELAYED_RESP:
849 case MSG_TYPE_NOTIFICATION:
857 trace_scmi_msg_dump(info->id, cinfo->id,
858 MSG_XTRACT_PROT_ID(msg_hdr),
859 MSG_XTRACT_ID(msg_hdr), tag,
860 MSG_XTRACT_TOKEN(msg_hdr), err, NULL, 0);
864 * scmi_msg_response_validate - Validate message type against state of related
867 * @cinfo: A reference to the channel descriptor.
868 * @msg_type: Message type to check
869 * @xfer: A reference to the xfer to validate against @msg_type
871 * This function checks if @msg_type is congruent with the current state of
872 * a pending @xfer; if an asynchronous delayed response is received before the
873 * related synchronous response (Out-of-Order Delayed Response) the missing
874 * synchronous response is assumed to be OK and completed, carrying on with the
875 * Delayed Response: this is done to address the case in which the underlying
876 * SCMI transport can deliver such out-of-order responses.
878 * Context: Assumes to be called with xfer->lock already acquired.
880 * Return: 0 on Success, error otherwise
882 static inline int scmi_msg_response_validate(struct scmi_chan_info *cinfo,
884 struct scmi_xfer *xfer)
887 * Even if a response was indeed expected on this slot at this point,
888 * a buggy platform could wrongly reply feeding us an unexpected
889 * delayed response we're not prepared to handle: bail-out safely
892 if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) {
894 "Delayed Response for %d not expected! Buggy F/W ?\n",
899 switch (xfer->state) {
900 case SCMI_XFER_SENT_OK:
901 if (msg_type == MSG_TYPE_DELAYED_RESP) {
903 * Delayed Response expected but delivered earlier.
904 * Assume message RESPONSE was OK and skip state.
906 xfer->hdr.status = SCMI_SUCCESS;
907 xfer->state = SCMI_XFER_RESP_OK;
908 complete(&xfer->done);
910 "Received valid OoO Delayed Response for %d\n",
914 case SCMI_XFER_RESP_OK:
915 if (msg_type != MSG_TYPE_DELAYED_RESP)
918 case SCMI_XFER_DRESP_OK:
919 /* No further message expected once in SCMI_XFER_DRESP_OK */
927 * scmi_xfer_state_update - Update xfer state
929 * @xfer: A reference to the xfer to update
930 * @msg_type: Type of message being processed.
932 * Note that this message is assumed to have been already successfully validated
933 * by @scmi_msg_response_validate(), so here we just update the state.
935 * Context: Assumes to be called on an xfer exclusively acquired using the
938 static inline void scmi_xfer_state_update(struct scmi_xfer *xfer, u8 msg_type)
940 xfer->hdr.type = msg_type;
942 /* Unknown command types were already discarded earlier */
943 if (xfer->hdr.type == MSG_TYPE_COMMAND)
944 xfer->state = SCMI_XFER_RESP_OK;
946 xfer->state = SCMI_XFER_DRESP_OK;
949 static bool scmi_xfer_acquired(struct scmi_xfer *xfer)
953 ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY);
955 return ret == SCMI_XFER_FREE;
959 * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
961 * @cinfo: A reference to the channel descriptor.
962 * @msg_hdr: A message header to use as lookup key
964 * When a valid xfer is found for the sequence number embedded in the provided
965 * msg_hdr, reference counting is properly updated and exclusive access to this
966 * xfer is granted till released with @scmi_xfer_command_release.
968 * Return: A valid @xfer on Success or error otherwise.
970 static inline struct scmi_xfer *
971 scmi_xfer_command_acquire(struct scmi_chan_info *cinfo, u32 msg_hdr)
975 struct scmi_xfer *xfer;
976 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
977 struct scmi_xfers_info *minfo = &info->tx_minfo;
978 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
979 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
981 /* Are we even expecting this? */
982 spin_lock_irqsave(&minfo->xfer_lock, flags);
983 xfer = scmi_xfer_lookup_unlocked(minfo, xfer_id);
986 "Message for %d type %d is not expected!\n",
988 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
990 scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNEXPECTED);
994 refcount_inc(&xfer->users);
995 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
997 spin_lock_irqsave(&xfer->lock, flags);
998 ret = scmi_msg_response_validate(cinfo, msg_type, xfer);
1000 * If a pending xfer was found which was also in a congruent state with
1001 * the received message, acquire exclusive access to it setting the busy
1003 * Spins only on the rare limit condition of concurrent reception of
1004 * RESP and DRESP for the same xfer.
1007 spin_until_cond(scmi_xfer_acquired(xfer));
1008 scmi_xfer_state_update(xfer, msg_type);
1010 spin_unlock_irqrestore(&xfer->lock, flags);
1014 "Invalid message type:%d for %d - HDR:0x%X state:%d\n",
1015 msg_type, xfer_id, msg_hdr, xfer->state);
1017 scmi_bad_message_trace(cinfo, msg_hdr, MSG_INVALID);
1019 /* On error the refcount incremented above has to be dropped */
1020 __scmi_xfer_put(minfo, xfer);
1021 xfer = ERR_PTR(-EINVAL);
1027 static inline void scmi_xfer_command_release(struct scmi_info *info,
1028 struct scmi_xfer *xfer)
1030 atomic_set(&xfer->busy, SCMI_XFER_FREE);
1031 __scmi_xfer_put(&info->tx_minfo, xfer);
1034 static inline void scmi_clear_channel(struct scmi_info *info,
1035 struct scmi_chan_info *cinfo)
1037 if (info->desc->ops->clear_channel)
1038 info->desc->ops->clear_channel(cinfo);
1041 static void scmi_handle_notification(struct scmi_chan_info *cinfo,
1042 u32 msg_hdr, void *priv)
1044 struct scmi_xfer *xfer;
1045 struct device *dev = cinfo->dev;
1046 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1047 struct scmi_xfers_info *minfo = &info->rx_minfo;
1050 ts = ktime_get_boottime();
1051 xfer = scmi_xfer_get(cinfo->handle, minfo);
1053 dev_err(dev, "failed to get free message slot (%ld)\n",
1056 scmi_bad_message_trace(cinfo, msg_hdr, MSG_NOMEM);
1058 scmi_clear_channel(info, cinfo);
1062 unpack_scmi_header(msg_hdr, &xfer->hdr);
1064 /* Ensure order between xfer->priv store and following ops */
1065 smp_store_mb(xfer->priv, priv);
1066 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
1069 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1070 xfer->hdr.id, "NOTI", xfer->hdr.seq,
1071 xfer->hdr.status, xfer->rx.buf, xfer->rx.len);
1073 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
1074 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
1076 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
1077 xfer->hdr.protocol_id, xfer->hdr.seq,
1078 MSG_TYPE_NOTIFICATION);
1080 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1081 xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr);
1082 scmi_raw_message_report(info->raw, xfer, SCMI_RAW_NOTIF_QUEUE,
1086 __scmi_xfer_put(minfo, xfer);
1088 scmi_clear_channel(info, cinfo);
1091 static void scmi_handle_response(struct scmi_chan_info *cinfo,
1092 u32 msg_hdr, void *priv)
1094 struct scmi_xfer *xfer;
1095 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1097 xfer = scmi_xfer_command_acquire(cinfo, msg_hdr);
1099 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
1100 scmi_raw_error_report(info->raw, cinfo, msg_hdr, priv);
1102 if (MSG_XTRACT_TYPE(msg_hdr) == MSG_TYPE_DELAYED_RESP)
1103 scmi_clear_channel(info, cinfo);
1107 /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
1108 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP)
1109 xfer->rx.len = info->desc->max_msg_size;
1112 /* Ensure order between xfer->priv store and following ops */
1113 smp_store_mb(xfer->priv, priv);
1114 info->desc->ops->fetch_response(cinfo, xfer);
1116 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1118 xfer->hdr.type == MSG_TYPE_DELAYED_RESP ?
1119 (!SCMI_XFER_IS_RAW(xfer) ? "DLYD" : "dlyd") :
1120 (!SCMI_XFER_IS_RAW(xfer) ? "RESP" : "resp"),
1121 xfer->hdr.seq, xfer->hdr.status,
1122 xfer->rx.buf, xfer->rx.len);
1124 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
1125 xfer->hdr.protocol_id, xfer->hdr.seq,
1128 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) {
1129 scmi_clear_channel(info, cinfo);
1130 complete(xfer->async_done);
1132 complete(&xfer->done);
1135 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1137 * When in polling mode avoid to queue the Raw xfer on the IRQ
1138 * RX path since it will be already queued at the end of the TX
1141 if (!xfer->hdr.poll_completion)
1142 scmi_raw_message_report(info->raw, xfer,
1143 SCMI_RAW_REPLY_QUEUE,
1147 scmi_xfer_command_release(info, xfer);
1151 * scmi_rx_callback() - callback for receiving messages
1153 * @cinfo: SCMI channel info
1154 * @msg_hdr: Message header
1155 * @priv: Transport specific private data.
1157 * Processes one received message to appropriate transfer information and
1158 * signals completion of the transfer.
1160 * NOTE: This function will be invoked in IRQ context, hence should be
1161 * as optimal as possible.
1163 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr, void *priv)
1165 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
1168 case MSG_TYPE_NOTIFICATION:
1169 scmi_handle_notification(cinfo, msg_hdr, priv);
1171 case MSG_TYPE_COMMAND:
1172 case MSG_TYPE_DELAYED_RESP:
1173 scmi_handle_response(cinfo, msg_hdr, priv);
1176 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
1177 scmi_bad_message_trace(cinfo, msg_hdr, MSG_UNKNOWN);
1183 * xfer_put() - Release a transmit message
1185 * @ph: Pointer to SCMI protocol handle
1186 * @xfer: message that was reserved by xfer_get_init
1188 static void xfer_put(const struct scmi_protocol_handle *ph,
1189 struct scmi_xfer *xfer)
1191 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1192 struct scmi_info *info = handle_to_scmi_info(pi->handle);
1194 __scmi_xfer_put(&info->tx_minfo, xfer);
1197 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
1198 struct scmi_xfer *xfer, ktime_t stop)
1200 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1203 * Poll also on xfer->done so that polling can be forcibly terminated
1204 * in case of out-of-order receptions of delayed responses
1206 return info->desc->ops->poll_done(cinfo, xfer) ||
1207 try_wait_for_completion(&xfer->done) ||
1208 ktime_after(ktime_get(), stop);
1211 static int scmi_wait_for_reply(struct device *dev, const struct scmi_desc *desc,
1212 struct scmi_chan_info *cinfo,
1213 struct scmi_xfer *xfer, unsigned int timeout_ms)
1217 if (xfer->hdr.poll_completion) {
1219 * Real polling is needed only if transport has NOT declared
1220 * itself to support synchronous commands replies.
1222 if (!desc->sync_cmds_completed_on_ret) {
1224 * Poll on xfer using transport provided .poll_done();
1225 * assumes no completion interrupt was available.
1227 ktime_t stop = ktime_add_ms(ktime_get(), timeout_ms);
1229 spin_until_cond(scmi_xfer_done_no_timeout(cinfo,
1231 if (ktime_after(ktime_get(), stop)) {
1233 "timed out in resp(caller: %pS) - polling\n",
1240 unsigned long flags;
1241 struct scmi_info *info =
1242 handle_to_scmi_info(cinfo->handle);
1245 * Do not fetch_response if an out-of-order delayed
1246 * response is being processed.
1248 spin_lock_irqsave(&xfer->lock, flags);
1249 if (xfer->state == SCMI_XFER_SENT_OK) {
1250 desc->ops->fetch_response(cinfo, xfer);
1251 xfer->state = SCMI_XFER_RESP_OK;
1253 spin_unlock_irqrestore(&xfer->lock, flags);
1255 /* Trace polled replies. */
1256 trace_scmi_msg_dump(info->id, cinfo->id,
1257 xfer->hdr.protocol_id, xfer->hdr.id,
1258 !SCMI_XFER_IS_RAW(xfer) ?
1260 xfer->hdr.seq, xfer->hdr.status,
1261 xfer->rx.buf, xfer->rx.len);
1263 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
1264 struct scmi_info *info =
1265 handle_to_scmi_info(cinfo->handle);
1267 scmi_raw_message_report(info->raw, xfer,
1268 SCMI_RAW_REPLY_QUEUE,
1273 /* And we wait for the response. */
1274 if (!wait_for_completion_timeout(&xfer->done,
1275 msecs_to_jiffies(timeout_ms))) {
1276 dev_err(dev, "timed out in resp(caller: %pS)\n",
1286 * scmi_wait_for_message_response - An helper to group all the possible ways of
1287 * waiting for a synchronous message response.
1289 * @cinfo: SCMI channel info
1290 * @xfer: Reference to the transfer being waited for.
1292 * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
1293 * configuration flags like xfer->hdr.poll_completion.
1295 * Return: 0 on Success, error otherwise.
1297 static int scmi_wait_for_message_response(struct scmi_chan_info *cinfo,
1298 struct scmi_xfer *xfer)
1300 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1301 struct device *dev = info->dev;
1303 trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id,
1304 xfer->hdr.protocol_id, xfer->hdr.seq,
1305 info->desc->max_rx_timeout_ms,
1306 xfer->hdr.poll_completion);
1308 return scmi_wait_for_reply(dev, info->desc, cinfo, xfer,
1309 info->desc->max_rx_timeout_ms);
1313 * scmi_xfer_raw_wait_for_message_response - An helper to wait for a message
1314 * reply to an xfer raw request on a specific channel for the required timeout.
1316 * @cinfo: SCMI channel info
1317 * @xfer: Reference to the transfer being waited for.
1318 * @timeout_ms: The maximum timeout in milliseconds
1320 * Return: 0 on Success, error otherwise.
1322 int scmi_xfer_raw_wait_for_message_response(struct scmi_chan_info *cinfo,
1323 struct scmi_xfer *xfer,
1324 unsigned int timeout_ms)
1327 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
1328 struct device *dev = info->dev;
1330 ret = scmi_wait_for_reply(dev, info->desc, cinfo, xfer, timeout_ms);
1332 dev_dbg(dev, "timed out in RAW response - HDR:%08X\n",
1333 pack_scmi_header(&xfer->hdr));
1339 * do_xfer() - Do one transfer
1341 * @ph: Pointer to SCMI protocol handle
1342 * @xfer: Transfer to initiate and wait for response
1344 * Return: -ETIMEDOUT in case of no response, if transmit error,
1345 * return corresponding error, else if all goes well,
1348 static int do_xfer(const struct scmi_protocol_handle *ph,
1349 struct scmi_xfer *xfer)
1352 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1353 struct scmi_info *info = handle_to_scmi_info(pi->handle);
1354 struct device *dev = info->dev;
1355 struct scmi_chan_info *cinfo;
1357 /* Check for polling request on custom command xfers at first */
1358 if (xfer->hdr.poll_completion &&
1359 !is_transport_polling_capable(info->desc)) {
1361 "Polling mode is not supported by transport.\n");
1365 cinfo = idr_find(&info->tx_idr, pi->proto->id);
1366 if (unlikely(!cinfo))
1369 /* True ONLY if also supported by transport. */
1370 if (is_polling_enabled(cinfo, info->desc))
1371 xfer->hdr.poll_completion = true;
1374 * Initialise protocol id now from protocol handle to avoid it being
1375 * overridden by mistake (or malice) by the protocol code mangling with
1376 * the scmi_xfer structure prior to this.
1378 xfer->hdr.protocol_id = pi->proto->id;
1379 reinit_completion(&xfer->done);
1381 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
1382 xfer->hdr.protocol_id, xfer->hdr.seq,
1383 xfer->hdr.poll_completion);
1385 /* Clear any stale status */
1386 xfer->hdr.status = SCMI_SUCCESS;
1387 xfer->state = SCMI_XFER_SENT_OK;
1389 * Even though spinlocking is not needed here since no race is possible
1390 * on xfer->state due to the monotonically increasing tokens allocation,
1391 * we must anyway ensure xfer->state initialization is not re-ordered
1392 * after the .send_message() to be sure that on the RX path an early
1393 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state.
1397 ret = info->desc->ops->send_message(cinfo, xfer);
1399 dev_dbg(dev, "Failed to send message %d\n", ret);
1403 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id,
1404 xfer->hdr.id, "CMND", xfer->hdr.seq,
1405 xfer->hdr.status, xfer->tx.buf, xfer->tx.len);
1407 ret = scmi_wait_for_message_response(cinfo, xfer);
1408 if (!ret && xfer->hdr.status)
1409 ret = scmi_to_linux_errno(xfer->hdr.status);
1411 if (info->desc->ops->mark_txdone)
1412 info->desc->ops->mark_txdone(cinfo, ret, xfer);
1414 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
1415 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
1420 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
1421 struct scmi_xfer *xfer)
1423 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1424 struct scmi_info *info = handle_to_scmi_info(pi->handle);
1426 xfer->rx.len = info->desc->max_msg_size;
1430 * do_xfer_with_response() - Do one transfer and wait until the delayed
1431 * response is received
1433 * @ph: Pointer to SCMI protocol handle
1434 * @xfer: Transfer to initiate and wait for response
1436 * Using asynchronous commands in atomic/polling mode should be avoided since
1437 * it could cause long busy-waiting here, so ignore polling for the delayed
1438 * response and WARN if it was requested for this command transaction since
1439 * upper layers should refrain from issuing such kind of requests.
1441 * The only other option would have been to refrain from using any asynchronous
1442 * command even if made available, when an atomic transport is detected, and
1443 * instead forcibly use the synchronous version (thing that can be easily
1444 * attained at the protocol layer), but this would also have led to longer
1445 * stalls of the channel for synchronous commands and possibly timeouts.
1446 * (in other words there is usually a good reason if a platform provides an
1447 * asynchronous version of a command and we should prefer to use it...just not
1448 * when using atomic/polling mode)
1450 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
1451 * return corresponding error, else if all goes well, return 0.
1453 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
1454 struct scmi_xfer *xfer)
1456 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
1457 DECLARE_COMPLETION_ONSTACK(async_response);
1459 xfer->async_done = &async_response;
1462 * Delayed responses should not be polled, so an async command should
1463 * not have been used when requiring an atomic/poll context; WARN and
1464 * perform instead a sleeping wait.
1465 * (Note Async + IgnoreDelayedResponses are sent via do_xfer)
1467 WARN_ON_ONCE(xfer->hdr.poll_completion);
1469 ret = do_xfer(ph, xfer);
1471 if (!wait_for_completion_timeout(xfer->async_done, timeout)) {
1473 "timed out in delayed resp(caller: %pS)\n",
1476 } else if (xfer->hdr.status) {
1477 ret = scmi_to_linux_errno(xfer->hdr.status);
1481 xfer->async_done = NULL;
1486 * xfer_get_init() - Allocate and initialise one message for transmit
1488 * @ph: Pointer to SCMI protocol handle
1489 * @msg_id: Message identifier
1490 * @tx_size: transmit message size
1491 * @rx_size: receive message size
1492 * @p: pointer to the allocated and initialised message
1494 * This function allocates the message using @scmi_xfer_get and
1495 * initialise the header.
1497 * Return: 0 if all went fine with @p pointing to message, else
1498 * corresponding error.
1500 static int xfer_get_init(const struct scmi_protocol_handle *ph,
1501 u8 msg_id, size_t tx_size, size_t rx_size,
1502 struct scmi_xfer **p)
1505 struct scmi_xfer *xfer;
1506 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1507 struct scmi_info *info = handle_to_scmi_info(pi->handle);
1508 struct scmi_xfers_info *minfo = &info->tx_minfo;
1509 struct device *dev = info->dev;
1511 /* Ensure we have sane transfer sizes */
1512 if (rx_size > info->desc->max_msg_size ||
1513 tx_size > info->desc->max_msg_size)
1516 xfer = scmi_xfer_get(pi->handle, minfo);
1518 ret = PTR_ERR(xfer);
1519 dev_err(dev, "failed to get free message slot(%d)\n", ret);
1523 /* Pick a sequence number and register this xfer as in-flight */
1524 ret = scmi_xfer_pending_set(xfer, minfo);
1526 dev_err(pi->handle->dev,
1527 "Failed to get monotonic token %d\n", ret);
1528 __scmi_xfer_put(minfo, xfer);
1532 xfer->tx.len = tx_size;
1533 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
1534 xfer->hdr.type = MSG_TYPE_COMMAND;
1535 xfer->hdr.id = msg_id;
1536 xfer->hdr.poll_completion = false;
1544 * version_get() - command to get the revision of the SCMI entity
1546 * @ph: Pointer to SCMI protocol handle
1547 * @version: Holds returned version of protocol.
1549 * Updates the SCMI information in the internal data structure.
1551 * Return: 0 if all went fine, else return appropriate error.
1553 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
1557 struct scmi_xfer *t;
1559 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
1563 ret = do_xfer(ph, t);
1565 rev_info = t->rx.buf;
1566 *version = le32_to_cpu(*rev_info);
1574 * scmi_set_protocol_priv - Set protocol specific data at init time
1576 * @ph: A reference to the protocol handle.
1577 * @priv: The private data to set.
1578 * @version: The detected protocol version for the core to register.
1580 * Return: 0 on Success
1582 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
1583 void *priv, u32 version)
1585 struct scmi_protocol_instance *pi = ph_to_pi(ph);
1588 pi->version = version;
1594 * scmi_get_protocol_priv - Set protocol specific data at init time
1596 * @ph: A reference to the protocol handle.
1598 * Return: Protocol private data if any was set.
1600 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
1602 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1607 static const struct scmi_xfer_ops xfer_ops = {
1608 .version_get = version_get,
1609 .xfer_get_init = xfer_get_init,
1610 .reset_rx_to_maxsz = reset_rx_to_maxsz,
1612 .do_xfer_with_response = do_xfer_with_response,
1613 .xfer_put = xfer_put,
1616 struct scmi_msg_resp_domain_name_get {
1618 u8 name[SCMI_MAX_STR_SIZE];
1622 * scmi_common_extended_name_get - Common helper to get extended resources name
1623 * @ph: A protocol handle reference.
1624 * @cmd_id: The specific command ID to use.
1625 * @res_id: The specific resource ID to use.
1626 * @flags: A pointer to specific flags to use, if any.
1627 * @name: A pointer to the preallocated area where the retrieved name will be
1628 * stored as a NULL terminated string.
1629 * @len: The len in bytes of the @name char array.
1631 * Return: 0 on Succcess
1633 static int scmi_common_extended_name_get(const struct scmi_protocol_handle *ph,
1634 u8 cmd_id, u32 res_id, u32 *flags,
1635 char *name, size_t len)
1639 struct scmi_xfer *t;
1640 struct scmi_msg_resp_domain_name_get *resp;
1642 txlen = !flags ? sizeof(res_id) : sizeof(res_id) + sizeof(*flags);
1643 ret = ph->xops->xfer_get_init(ph, cmd_id, txlen, sizeof(*resp), &t);
1647 put_unaligned_le32(res_id, t->tx.buf);
1649 put_unaligned_le32(*flags, t->tx.buf + sizeof(res_id));
1652 ret = ph->xops->do_xfer(ph, t);
1654 strscpy(name, resp->name, len);
1656 ph->xops->xfer_put(ph, t);
1660 "Failed to get extended name - id:%u (ret:%d). Using %s\n",
1666 * scmi_common_get_max_msg_size - Get maximum message size
1667 * @ph: A protocol handle reference.
1669 * Return: Maximum message size for the current protocol.
1671 static int scmi_common_get_max_msg_size(const struct scmi_protocol_handle *ph)
1673 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1674 struct scmi_info *info = handle_to_scmi_info(pi->handle);
1676 return info->desc->max_msg_size;
1680 * struct scmi_iterator - Iterator descriptor
1681 * @msg: A reference to the message TX buffer; filled by @prepare_message with
1682 * a proper custom command payload for each multi-part command request.
1683 * @resp: A reference to the response RX buffer; used by @update_state and
1684 * @process_response to parse the multi-part replies.
1685 * @t: A reference to the underlying xfer initialized and used transparently by
1686 * the iterator internal routines.
1687 * @ph: A reference to the associated protocol handle to be used.
1688 * @ops: A reference to the custom provided iterator operations.
1689 * @state: The current iterator state; used and updated in turn by the iterators
1690 * internal routines and by the caller-provided @scmi_iterator_ops.
1691 * @priv: A reference to optional private data as provided by the caller and
1692 * passed back to the @@scmi_iterator_ops.
1694 struct scmi_iterator {
1697 struct scmi_xfer *t;
1698 const struct scmi_protocol_handle *ph;
1699 struct scmi_iterator_ops *ops;
1700 struct scmi_iterator_state state;
1704 static void *scmi_iterator_init(const struct scmi_protocol_handle *ph,
1705 struct scmi_iterator_ops *ops,
1706 unsigned int max_resources, u8 msg_id,
1707 size_t tx_size, void *priv)
1710 struct scmi_iterator *i;
1712 i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL);
1714 return ERR_PTR(-ENOMEM);
1720 ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t);
1722 devm_kfree(ph->dev, i);
1723 return ERR_PTR(ret);
1726 i->state.max_resources = max_resources;
1727 i->msg = i->t->tx.buf;
1728 i->resp = i->t->rx.buf;
1733 static int scmi_iterator_run(void *iter)
1736 struct scmi_iterator_ops *iops;
1737 const struct scmi_protocol_handle *ph;
1738 struct scmi_iterator_state *st;
1739 struct scmi_iterator *i = iter;
1741 if (!i || !i->ops || !i->ph)
1749 iops->prepare_message(i->msg, st->desc_index, i->priv);
1750 ret = ph->xops->do_xfer(ph, i->t);
1754 st->rx_len = i->t->rx.len;
1755 ret = iops->update_state(st, i->resp, i->priv);
1759 if (st->num_returned > st->max_resources - st->desc_index) {
1761 "No. of resources can't exceed %d\n",
1767 for (st->loop_idx = 0; st->loop_idx < st->num_returned;
1769 ret = iops->process_response(ph, i->resp, st, i->priv);
1774 st->desc_index += st->num_returned;
1775 ph->xops->reset_rx_to_maxsz(ph, i->t);
1777 * check for both returned and remaining to avoid infinite
1778 * loop due to buggy firmware
1780 } while (st->num_returned && st->num_remaining);
1783 /* Finalize and destroy iterator */
1784 ph->xops->xfer_put(ph, i->t);
1785 devm_kfree(ph->dev, i);
1790 struct scmi_msg_get_fc_info {
1795 struct scmi_msg_resp_desc_fc {
1797 #define SUPPORTS_DOORBELL(x) ((x) & BIT(0))
1798 #define DOORBELL_REG_WIDTH(x) FIELD_GET(GENMASK(2, 1), (x))
1800 __le32 chan_addr_low;
1801 __le32 chan_addr_high;
1804 __le32 db_addr_high;
1805 __le32 db_set_lmask;
1806 __le32 db_set_hmask;
1807 __le32 db_preserve_lmask;
1808 __le32 db_preserve_hmask;
1812 scmi_common_fastchannel_init(const struct scmi_protocol_handle *ph,
1813 u8 describe_id, u32 message_id, u32 valid_size,
1814 u32 domain, void __iomem **p_addr,
1815 struct scmi_fc_db_info **p_db, u32 *rate_limit)
1822 struct scmi_xfer *t;
1823 struct scmi_fc_db_info *db = NULL;
1824 struct scmi_msg_get_fc_info *info;
1825 struct scmi_msg_resp_desc_fc *resp;
1826 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
1833 ret = ph->xops->xfer_get_init(ph, describe_id,
1834 sizeof(*info), sizeof(*resp), &t);
1839 info->domain = cpu_to_le32(domain);
1840 info->message_id = cpu_to_le32(message_id);
1843 * Bail out on error leaving fc_info addresses zeroed; this includes
1844 * the case in which the requested domain/message_id does NOT support
1845 * fastchannels at all.
1847 ret = ph->xops->do_xfer(ph, t);
1852 flags = le32_to_cpu(resp->attr);
1853 size = le32_to_cpu(resp->chan_size);
1854 if (size != valid_size) {
1860 *rate_limit = le32_to_cpu(resp->rate_limit) & GENMASK(19, 0);
1862 phys_addr = le32_to_cpu(resp->chan_addr_low);
1863 phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32;
1864 addr = devm_ioremap(ph->dev, phys_addr, size);
1866 ret = -EADDRNOTAVAIL;
1872 if (p_db && SUPPORTS_DOORBELL(flags)) {
1873 db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL);
1879 size = 1 << DOORBELL_REG_WIDTH(flags);
1880 phys_addr = le32_to_cpu(resp->db_addr_low);
1881 phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32;
1882 addr = devm_ioremap(ph->dev, phys_addr, size);
1884 ret = -EADDRNOTAVAIL;
1890 db->set = le32_to_cpu(resp->db_set_lmask);
1891 db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32;
1892 db->mask = le32_to_cpu(resp->db_preserve_lmask);
1893 db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32;
1898 ph->xops->xfer_put(ph, t);
1901 "Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n",
1902 pi->proto->id, message_id, domain);
1907 devm_kfree(ph->dev, db);
1913 ph->xops->xfer_put(ph, t);
1917 "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n",
1918 pi->proto->id, message_id, domain, ret);
1921 #define SCMI_PROTO_FC_RING_DB(w) \
1926 val = ioread##w(db->addr) & db->mask; \
1927 iowrite##w((u##w)db->set | val, db->addr); \
1930 static void scmi_common_fastchannel_db_ring(struct scmi_fc_db_info *db)
1932 if (!db || !db->addr)
1936 SCMI_PROTO_FC_RING_DB(8);
1937 else if (db->width == 2)
1938 SCMI_PROTO_FC_RING_DB(16);
1939 else if (db->width == 4)
1940 SCMI_PROTO_FC_RING_DB(32);
1941 else /* db->width == 8 */
1943 SCMI_PROTO_FC_RING_DB(64);
1949 val = ioread64_hi_lo(db->addr) & db->mask;
1950 iowrite64_hi_lo(db->set | val, db->addr);
1956 * scmi_protocol_msg_check - Check protocol message attributes
1958 * @ph: A reference to the protocol handle.
1959 * @message_id: The ID of the message to check.
1960 * @attributes: A parameter to optionally return the retrieved message
1961 * attributes, in case of Success.
1963 * An helper to check protocol message attributes for a specific protocol
1966 * Return: 0 on SUCCESS
1968 static int scmi_protocol_msg_check(const struct scmi_protocol_handle *ph,
1969 u32 message_id, u32 *attributes)
1972 struct scmi_xfer *t;
1974 ret = xfer_get_init(ph, PROTOCOL_MESSAGE_ATTRIBUTES,
1975 sizeof(__le32), 0, &t);
1979 put_unaligned_le32(message_id, t->tx.buf);
1980 ret = do_xfer(ph, t);
1981 if (!ret && attributes)
1982 *attributes = get_unaligned_le32(t->rx.buf);
1988 static const struct scmi_proto_helpers_ops helpers_ops = {
1989 .extended_name_get = scmi_common_extended_name_get,
1990 .get_max_msg_size = scmi_common_get_max_msg_size,
1991 .iter_response_init = scmi_iterator_init,
1992 .iter_response_run = scmi_iterator_run,
1993 .protocol_msg_check = scmi_protocol_msg_check,
1994 .fastchannel_init = scmi_common_fastchannel_init,
1995 .fastchannel_db_ring = scmi_common_fastchannel_db_ring,
1999 * scmi_revision_area_get - Retrieve version memory area.
2001 * @ph: A reference to the protocol handle.
2003 * A helper to grab the version memory area reference during SCMI Base protocol
2006 * Return: A reference to the version memory area associated to the SCMI
2007 * instance underlying this protocol handle.
2009 struct scmi_revision_info *
2010 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
2012 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
2014 return pi->handle->version;
2018 * scmi_protocol_version_negotiate - Negotiate protocol version
2020 * @ph: A reference to the protocol handle.
2022 * An helper to negotiate a protocol version different from the latest
2023 * advertised as supported from the platform: on Success backward
2024 * compatibility is assured by the platform.
2026 * Return: 0 on Success
2028 static int scmi_protocol_version_negotiate(struct scmi_protocol_handle *ph)
2031 struct scmi_xfer *t;
2032 struct scmi_protocol_instance *pi = ph_to_pi(ph);
2034 /* At first check if NEGOTIATE_PROTOCOL_VERSION is supported ... */
2035 ret = scmi_protocol_msg_check(ph, NEGOTIATE_PROTOCOL_VERSION, NULL);
2039 /* ... then attempt protocol version negotiation */
2040 ret = xfer_get_init(ph, NEGOTIATE_PROTOCOL_VERSION,
2041 sizeof(__le32), 0, &t);
2045 put_unaligned_le32(pi->proto->supported_version, t->tx.buf);
2046 ret = do_xfer(ph, t);
2048 pi->negotiated_version = pi->proto->supported_version;
2056 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
2057 * instance descriptor.
2058 * @info: The reference to the related SCMI instance.
2059 * @proto: The protocol descriptor.
2061 * Allocate a new protocol instance descriptor, using the provided @proto
2062 * description, against the specified SCMI instance @info, and initialize it;
2063 * all resources management is handled via a dedicated per-protocol devres
2066 * Context: Assumes to be called with @protocols_mtx already acquired.
2067 * Return: A reference to a freshly allocated and initialized protocol instance
2068 * or ERR_PTR on failure. On failure the @proto reference is at first
2069 * put using @scmi_protocol_put() before releasing all the devres group.
2071 static struct scmi_protocol_instance *
2072 scmi_alloc_init_protocol_instance(struct scmi_info *info,
2073 const struct scmi_protocol *proto)
2077 struct scmi_protocol_instance *pi;
2078 const struct scmi_handle *handle = &info->handle;
2080 /* Protocol specific devres group */
2081 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
2083 scmi_protocol_put(proto);
2087 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
2093 pi->handle = handle;
2094 pi->ph.dev = handle->dev;
2095 pi->ph.xops = &xfer_ops;
2096 pi->ph.hops = &helpers_ops;
2097 pi->ph.set_priv = scmi_set_protocol_priv;
2098 pi->ph.get_priv = scmi_get_protocol_priv;
2099 refcount_set(&pi->users, 1);
2100 /* proto->init is assured NON NULL by scmi_protocol_register */
2101 ret = pi->proto->instance_init(&pi->ph);
2105 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
2107 if (ret != proto->id)
2111 * Warn but ignore events registration errors since we do not want
2112 * to skip whole protocols if their notifications are messed up.
2114 if (pi->proto->events) {
2115 ret = scmi_register_protocol_events(handle, pi->proto->id,
2119 dev_warn(handle->dev,
2120 "Protocol:%X - Events Registration Failed - err:%d\n",
2121 pi->proto->id, ret);
2124 devres_close_group(handle->dev, pi->gid);
2125 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
2127 if (pi->version > proto->supported_version) {
2128 ret = scmi_protocol_version_negotiate(&pi->ph);
2130 dev_info(handle->dev,
2131 "Protocol 0x%X successfully negotiated version 0x%X\n",
2132 proto->id, pi->negotiated_version);
2134 dev_warn(handle->dev,
2135 "Detected UNSUPPORTED higher version 0x%X for protocol 0x%X.\n",
2136 pi->version, pi->proto->id);
2137 dev_warn(handle->dev,
2138 "Trying version 0x%X. Backward compatibility is NOT assured.\n",
2139 pi->proto->supported_version);
2146 /* Take care to put the protocol module's owner before releasing all */
2147 scmi_protocol_put(proto);
2148 devres_release_group(handle->dev, gid);
2150 return ERR_PTR(ret);
2154 * scmi_get_protocol_instance - Protocol initialization helper.
2155 * @handle: A reference to the SCMI platform instance.
2156 * @protocol_id: The protocol being requested.
2158 * In case the required protocol has never been requested before for this
2159 * instance, allocate and initialize all the needed structures while handling
2160 * resource allocation with a dedicated per-protocol devres subgroup.
2162 * Return: A reference to an initialized protocol instance or error on failure:
2163 * in particular returns -EPROBE_DEFER when the desired protocol could
2166 static struct scmi_protocol_instance * __must_check
2167 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
2169 struct scmi_protocol_instance *pi;
2170 struct scmi_info *info = handle_to_scmi_info(handle);
2172 mutex_lock(&info->protocols_mtx);
2173 pi = idr_find(&info->protocols, protocol_id);
2176 refcount_inc(&pi->users);
2178 const struct scmi_protocol *proto;
2180 /* Fails if protocol not registered on bus */
2181 proto = scmi_protocol_get(protocol_id, &info->version);
2183 pi = scmi_alloc_init_protocol_instance(info, proto);
2185 pi = ERR_PTR(-EPROBE_DEFER);
2187 mutex_unlock(&info->protocols_mtx);
2193 * scmi_protocol_acquire - Protocol acquire
2194 * @handle: A reference to the SCMI platform instance.
2195 * @protocol_id: The protocol being requested.
2197 * Register a new user for the requested protocol on the specified SCMI
2198 * platform instance, possibly triggering its initialization on first user.
2200 * Return: 0 if protocol was acquired successfully.
2202 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
2204 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
2208 * scmi_protocol_release - Protocol de-initialization helper.
2209 * @handle: A reference to the SCMI platform instance.
2210 * @protocol_id: The protocol being requested.
2212 * Remove one user for the specified protocol and triggers de-initialization
2213 * and resources de-allocation once the last user has gone.
2215 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
2217 struct scmi_info *info = handle_to_scmi_info(handle);
2218 struct scmi_protocol_instance *pi;
2220 mutex_lock(&info->protocols_mtx);
2221 pi = idr_find(&info->protocols, protocol_id);
2225 if (refcount_dec_and_test(&pi->users)) {
2226 void *gid = pi->gid;
2228 if (pi->proto->events)
2229 scmi_deregister_protocol_events(handle, protocol_id);
2231 if (pi->proto->instance_deinit)
2232 pi->proto->instance_deinit(&pi->ph);
2234 idr_remove(&info->protocols, protocol_id);
2236 scmi_protocol_put(pi->proto);
2238 devres_release_group(handle->dev, gid);
2239 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
2244 mutex_unlock(&info->protocols_mtx);
2247 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
2250 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
2251 struct scmi_info *info = handle_to_scmi_info(pi->handle);
2253 info->protocols_imp = prot_imp;
2257 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
2260 struct scmi_info *info = handle_to_scmi_info(handle);
2261 struct scmi_revision_info *rev = handle->version;
2263 if (!info->protocols_imp)
2266 for (i = 0; i < rev->num_protocols; i++)
2267 if (info->protocols_imp[i] == prot_id)
2272 struct scmi_protocol_devres {
2273 const struct scmi_handle *handle;
2277 static void scmi_devm_release_protocol(struct device *dev, void *res)
2279 struct scmi_protocol_devres *dres = res;
2281 scmi_protocol_release(dres->handle, dres->protocol_id);
2284 static struct scmi_protocol_instance __must_check *
2285 scmi_devres_protocol_instance_get(struct scmi_device *sdev, u8 protocol_id)
2287 struct scmi_protocol_instance *pi;
2288 struct scmi_protocol_devres *dres;
2290 dres = devres_alloc(scmi_devm_release_protocol,
2291 sizeof(*dres), GFP_KERNEL);
2293 return ERR_PTR(-ENOMEM);
2295 pi = scmi_get_protocol_instance(sdev->handle, protocol_id);
2301 dres->handle = sdev->handle;
2302 dres->protocol_id = protocol_id;
2303 devres_add(&sdev->dev, dres);
2309 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
2310 * @sdev: A reference to an scmi_device whose embedded struct device is to
2311 * be used for devres accounting.
2312 * @protocol_id: The protocol being requested.
2313 * @ph: A pointer reference used to pass back the associated protocol handle.
2315 * Get hold of a protocol accounting for its usage, eventually triggering its
2316 * initialization, and returning the protocol specific operations and related
2317 * protocol handle which will be used as first argument in most of the
2318 * protocols operations methods.
2319 * Being a devres based managed method, protocol hold will be automatically
2320 * released, and possibly de-initialized on last user, once the SCMI driver
2321 * owning the scmi_device is unbound from it.
2323 * Return: A reference to the requested protocol operations or error.
2324 * Must be checked for errors by caller.
2326 static const void __must_check *
2327 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
2328 struct scmi_protocol_handle **ph)
2330 struct scmi_protocol_instance *pi;
2333 return ERR_PTR(-EINVAL);
2335 pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
2341 return pi->proto->ops;
2345 * scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol
2346 * @sdev: A reference to an scmi_device whose embedded struct device is to
2347 * be used for devres accounting.
2348 * @protocol_id: The protocol being requested.
2350 * Get hold of a protocol accounting for its usage, possibly triggering its
2351 * initialization but without getting access to its protocol specific operations
2354 * Being a devres based managed method, protocol hold will be automatically
2355 * released, and possibly de-initialized on last user, once the SCMI driver
2356 * owning the scmi_device is unbound from it.
2358 * Return: 0 on SUCCESS
2360 static int __must_check scmi_devm_protocol_acquire(struct scmi_device *sdev,
2363 struct scmi_protocol_instance *pi;
2365 pi = scmi_devres_protocol_instance_get(sdev, protocol_id);
2372 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
2374 struct scmi_protocol_devres *dres = res;
2376 if (WARN_ON(!dres || !data))
2379 return dres->protocol_id == *((u8 *)data);
2383 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
2384 * @sdev: A reference to an scmi_device whose embedded struct device is to
2385 * be used for devres accounting.
2386 * @protocol_id: The protocol being requested.
2388 * Explicitly release a protocol hold previously obtained calling the above
2389 * @scmi_devm_protocol_get.
2391 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
2395 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
2396 scmi_devm_protocol_match, &protocol_id);
2401 * scmi_is_transport_atomic - Method to check if underlying transport for an
2402 * SCMI instance is configured as atomic.
2404 * @handle: A reference to the SCMI platform instance.
2405 * @atomic_threshold: An optional return value for the system wide currently
2406 * configured threshold for atomic operations.
2408 * Return: True if transport is configured as atomic
2410 static bool scmi_is_transport_atomic(const struct scmi_handle *handle,
2411 unsigned int *atomic_threshold)
2414 struct scmi_info *info = handle_to_scmi_info(handle);
2416 ret = info->desc->atomic_enabled &&
2417 is_transport_polling_capable(info->desc);
2418 if (ret && atomic_threshold)
2419 *atomic_threshold = info->atomic_threshold;
2425 * scmi_handle_get() - Get the SCMI handle for a device
2427 * @dev: pointer to device for which we want SCMI handle
2429 * NOTE: The function does not track individual clients of the framework
2430 * and is expected to be maintained by caller of SCMI protocol library.
2431 * scmi_handle_put must be balanced with successful scmi_handle_get
2433 * Return: pointer to handle if successful, NULL on error
2435 static struct scmi_handle *scmi_handle_get(struct device *dev)
2437 struct list_head *p;
2438 struct scmi_info *info;
2439 struct scmi_handle *handle = NULL;
2441 mutex_lock(&scmi_list_mutex);
2442 list_for_each(p, &scmi_list) {
2443 info = list_entry(p, struct scmi_info, node);
2444 if (dev->parent == info->dev) {
2446 handle = &info->handle;
2450 mutex_unlock(&scmi_list_mutex);
2456 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
2458 * @handle: handle acquired by scmi_handle_get
2460 * NOTE: The function does not track individual clients of the framework
2461 * and is expected to be maintained by caller of SCMI protocol library.
2462 * scmi_handle_put must be balanced with successful scmi_handle_get
2464 * Return: 0 is successfully released
2465 * if null was passed, it returns -EINVAL;
2467 static int scmi_handle_put(const struct scmi_handle *handle)
2469 struct scmi_info *info;
2474 info = handle_to_scmi_info(handle);
2475 mutex_lock(&scmi_list_mutex);
2476 if (!WARN_ON(!info->users))
2478 mutex_unlock(&scmi_list_mutex);
2483 static void scmi_device_link_add(struct device *consumer,
2484 struct device *supplier)
2486 struct device_link *link;
2488 link = device_link_add(consumer, supplier, DL_FLAG_AUTOREMOVE_CONSUMER);
2493 static void scmi_set_handle(struct scmi_device *scmi_dev)
2495 scmi_dev->handle = scmi_handle_get(&scmi_dev->dev);
2496 if (scmi_dev->handle)
2497 scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev);
2500 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
2501 struct scmi_xfers_info *info)
2504 struct scmi_xfer *xfer;
2505 struct device *dev = sinfo->dev;
2506 const struct scmi_desc *desc = sinfo->desc;
2508 /* Pre-allocated messages, no more than what hdr.seq can support */
2509 if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) {
2511 "Invalid maximum messages %d, not in range [1 - %lu]\n",
2512 info->max_msg, MSG_TOKEN_MAX);
2516 hash_init(info->pending_xfers);
2518 /* Allocate a bitmask sized to hold MSG_TOKEN_MAX tokens */
2519 info->xfer_alloc_table = devm_bitmap_zalloc(dev, MSG_TOKEN_MAX,
2521 if (!info->xfer_alloc_table)
2525 * Preallocate a number of xfers equal to max inflight messages,
2526 * pre-initialize the buffer pointer to pre-allocated buffers and
2527 * attach all of them to the free list
2529 INIT_HLIST_HEAD(&info->free_xfers);
2530 for (i = 0; i < info->max_msg; i++) {
2531 xfer = devm_kzalloc(dev, sizeof(*xfer), GFP_KERNEL);
2535 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
2540 xfer->tx.buf = xfer->rx.buf;
2541 init_completion(&xfer->done);
2542 spin_lock_init(&xfer->lock);
2544 /* Add initialized xfer to the free list */
2545 hlist_add_head(&xfer->node, &info->free_xfers);
2548 spin_lock_init(&info->xfer_lock);
2553 static int scmi_channels_max_msg_configure(struct scmi_info *sinfo)
2555 const struct scmi_desc *desc = sinfo->desc;
2557 if (!desc->ops->get_max_msg) {
2558 sinfo->tx_minfo.max_msg = desc->max_msg;
2559 sinfo->rx_minfo.max_msg = desc->max_msg;
2561 struct scmi_chan_info *base_cinfo;
2563 base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE);
2566 sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo);
2568 /* RX channel is optional so can be skipped */
2569 base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE);
2571 sinfo->rx_minfo.max_msg =
2572 desc->ops->get_max_msg(base_cinfo);
2578 static int scmi_xfer_info_init(struct scmi_info *sinfo)
2582 ret = scmi_channels_max_msg_configure(sinfo);
2586 ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
2587 if (!ret && !idr_is_empty(&sinfo->rx_idr))
2588 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
2593 static int scmi_chan_setup(struct scmi_info *info, struct device_node *of_node,
2594 int prot_id, bool tx)
2598 struct scmi_chan_info *cinfo;
2600 struct scmi_device *tdev = NULL;
2602 /* Transmit channel is first entry i.e. index 0 */
2604 idr = tx ? &info->tx_idr : &info->rx_idr;
2606 if (!info->desc->ops->chan_available(of_node, idx)) {
2607 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
2608 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
2613 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
2617 cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms;
2619 /* Create a unique name for this transport device */
2620 snprintf(name, 32, "__scmi_transport_device_%s_%02X",
2621 idx ? "rx" : "tx", prot_id);
2622 /* Create a uniquely named, dedicated transport device for this chan */
2623 tdev = scmi_device_create(of_node, info->dev, prot_id, name);
2626 "failed to create transport device (%s)\n", name);
2627 devm_kfree(info->dev, cinfo);
2630 of_node_get(of_node);
2632 cinfo->id = prot_id;
2633 cinfo->dev = &tdev->dev;
2634 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
2636 of_node_put(of_node);
2637 scmi_device_destroy(info->dev, prot_id, name);
2638 devm_kfree(info->dev, cinfo);
2642 if (tx && is_polling_required(cinfo, info->desc)) {
2643 if (is_transport_polling_capable(info->desc))
2644 dev_info(&tdev->dev,
2645 "Enabled polling mode TX channel - prot_id:%d\n",
2648 dev_warn(&tdev->dev,
2649 "Polling mode NOT supported by transport.\n");
2653 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
2654 if (ret != prot_id) {
2656 "unable to allocate SCMI idr slot err %d\n", ret);
2657 /* Destroy channel and device only if created by this call. */
2659 of_node_put(of_node);
2660 scmi_device_destroy(info->dev, prot_id, name);
2661 devm_kfree(info->dev, cinfo);
2666 cinfo->handle = &info->handle;
2671 scmi_txrx_setup(struct scmi_info *info, struct device_node *of_node,
2674 int ret = scmi_chan_setup(info, of_node, prot_id, true);
2677 /* Rx is optional, report only memory errors */
2678 ret = scmi_chan_setup(info, of_node, prot_id, false);
2679 if (ret && ret != -ENOMEM)
2685 "failed to setup channel for protocol:0x%X\n", prot_id);
2691 * scmi_channels_setup - Helper to initialize all required channels
2693 * @info: The SCMI instance descriptor.
2695 * Initialize all the channels found described in the DT against the underlying
2696 * configured transport using custom defined dedicated devices instead of
2697 * borrowing devices from the SCMI drivers; this way channels are initialized
2698 * upfront during core SCMI stack probing and are no more coupled with SCMI
2699 * devices used by SCMI drivers.
2701 * Note that, even though a pair of TX/RX channels is associated to each
2702 * protocol defined in the DT, a distinct freshly initialized channel is
2703 * created only if the DT node for the protocol at hand describes a dedicated
2704 * channel: in all the other cases the common BASE protocol channel is reused.
2706 * Return: 0 on Success
2708 static int scmi_channels_setup(struct scmi_info *info)
2711 struct device_node *child, *top_np = info->dev->of_node;
2713 /* Initialize a common generic channel at first */
2714 ret = scmi_txrx_setup(info, top_np, SCMI_PROTOCOL_BASE);
2718 for_each_available_child_of_node(top_np, child) {
2721 if (of_property_read_u32(child, "reg", &prot_id))
2724 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
2726 "Out of range protocol %d\n", prot_id);
2728 ret = scmi_txrx_setup(info, child, prot_id);
2738 static int scmi_chan_destroy(int id, void *p, void *idr)
2740 struct scmi_chan_info *cinfo = p;
2743 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
2744 struct scmi_device *sdev = to_scmi_dev(cinfo->dev);
2746 of_node_put(cinfo->dev->of_node);
2747 scmi_device_destroy(info->dev, id, sdev->name);
2751 idr_remove(idr, id);
2756 static void scmi_cleanup_channels(struct scmi_info *info, struct idr *idr)
2758 /* At first free all channels at the transport layer ... */
2759 idr_for_each(idr, info->desc->ops->chan_free, idr);
2761 /* ...then destroy all underlying devices */
2762 idr_for_each(idr, scmi_chan_destroy, idr);
2767 static void scmi_cleanup_txrx_channels(struct scmi_info *info)
2769 scmi_cleanup_channels(info, &info->tx_idr);
2771 scmi_cleanup_channels(info, &info->rx_idr);
2774 static int scmi_bus_notifier(struct notifier_block *nb,
2775 unsigned long action, void *data)
2777 struct scmi_info *info = bus_nb_to_scmi_info(nb);
2778 struct scmi_device *sdev = to_scmi_dev(data);
2780 /* Skip transport devices and devices of different SCMI instances */
2781 if (!strncmp(sdev->name, "__scmi_transport_device", 23) ||
2782 sdev->dev.parent != info->dev)
2786 case BUS_NOTIFY_BIND_DRIVER:
2787 /* setup handle now as the transport is ready */
2788 scmi_set_handle(sdev);
2790 case BUS_NOTIFY_UNBOUND_DRIVER:
2791 scmi_handle_put(sdev->handle);
2792 sdev->handle = NULL;
2798 dev_dbg(info->dev, "Device %s (%s) is now %s\n", dev_name(&sdev->dev),
2799 sdev->name, action == BUS_NOTIFY_BIND_DRIVER ?
2800 "about to be BOUND." : "UNBOUND.");
2805 static int scmi_device_request_notifier(struct notifier_block *nb,
2806 unsigned long action, void *data)
2808 struct device_node *np;
2809 struct scmi_device_id *id_table = data;
2810 struct scmi_info *info = req_nb_to_scmi_info(nb);
2812 np = idr_find(&info->active_protocols, id_table->protocol_id);
2816 dev_dbg(info->dev, "%sRequested device (%s) for protocol 0x%x\n",
2817 action == SCMI_BUS_NOTIFY_DEVICE_REQUEST ? "" : "UN-",
2818 id_table->name, id_table->protocol_id);
2821 case SCMI_BUS_NOTIFY_DEVICE_REQUEST:
2822 scmi_create_protocol_devices(np, info, id_table->protocol_id,
2825 case SCMI_BUS_NOTIFY_DEVICE_UNREQUEST:
2826 scmi_destroy_protocol_devices(info, id_table->protocol_id,
2836 static void scmi_debugfs_common_cleanup(void *d)
2838 struct scmi_debug_info *dbg = d;
2843 debugfs_remove_recursive(dbg->top_dentry);
2848 static struct scmi_debug_info *scmi_debugfs_common_setup(struct scmi_info *info)
2851 struct dentry *trans, *top_dentry;
2852 struct scmi_debug_info *dbg;
2853 const char *c_ptr = NULL;
2855 dbg = devm_kzalloc(info->dev, sizeof(*dbg), GFP_KERNEL);
2859 dbg->name = kstrdup(of_node_full_name(info->dev->of_node), GFP_KERNEL);
2861 devm_kfree(info->dev, dbg);
2865 of_property_read_string(info->dev->of_node, "compatible", &c_ptr);
2866 dbg->type = kstrdup(c_ptr, GFP_KERNEL);
2869 devm_kfree(info->dev, dbg);
2873 snprintf(top_dir, 16, "%d", info->id);
2874 top_dentry = debugfs_create_dir(top_dir, scmi_top_dentry);
2875 trans = debugfs_create_dir("transport", top_dentry);
2877 dbg->is_atomic = info->desc->atomic_enabled &&
2878 is_transport_polling_capable(info->desc);
2880 debugfs_create_str("instance_name", 0400, top_dentry,
2881 (char **)&dbg->name);
2883 debugfs_create_u32("atomic_threshold_us", 0400, top_dentry,
2884 &info->atomic_threshold);
2886 debugfs_create_str("type", 0400, trans, (char **)&dbg->type);
2888 debugfs_create_bool("is_atomic", 0400, trans, &dbg->is_atomic);
2890 debugfs_create_u32("max_rx_timeout_ms", 0400, trans,
2891 (u32 *)&info->desc->max_rx_timeout_ms);
2893 debugfs_create_u32("max_msg_size", 0400, trans,
2894 (u32 *)&info->desc->max_msg_size);
2896 debugfs_create_u32("tx_max_msg", 0400, trans,
2897 (u32 *)&info->tx_minfo.max_msg);
2899 debugfs_create_u32("rx_max_msg", 0400, trans,
2900 (u32 *)&info->rx_minfo.max_msg);
2902 dbg->top_dentry = top_dentry;
2904 if (devm_add_action_or_reset(info->dev,
2905 scmi_debugfs_common_cleanup, dbg)) {
2906 scmi_debugfs_common_cleanup(dbg);
2913 static int scmi_debugfs_raw_mode_setup(struct scmi_info *info)
2915 int id, num_chans = 0, ret = 0;
2916 struct scmi_chan_info *cinfo;
2917 u8 channels[SCMI_MAX_CHANNELS] = {};
2918 DECLARE_BITMAP(protos, SCMI_MAX_CHANNELS) = {};
2923 /* Enumerate all channels to collect their ids */
2924 idr_for_each_entry(&info->tx_idr, cinfo, id) {
2926 * Cannot happen, but be defensive.
2927 * Zero as num_chans is ok, warn and carry on.
2929 if (num_chans >= SCMI_MAX_CHANNELS || !cinfo) {
2931 "SCMI RAW - Error enumerating channels\n");
2935 if (!test_bit(cinfo->id, protos)) {
2936 channels[num_chans++] = cinfo->id;
2937 set_bit(cinfo->id, protos);
2941 info->raw = scmi_raw_mode_init(&info->handle, info->dbg->top_dentry,
2942 info->id, channels, num_chans,
2943 info->desc, info->tx_minfo.max_msg);
2944 if (IS_ERR(info->raw)) {
2945 dev_err(info->dev, "Failed to initialize SCMI RAW Mode !\n");
2946 ret = PTR_ERR(info->raw);
2953 static int scmi_probe(struct platform_device *pdev)
2956 char *err_str = "probe failure\n";
2957 struct scmi_handle *handle;
2958 const struct scmi_desc *desc;
2959 struct scmi_info *info;
2960 bool coex = IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT_COEX);
2961 struct device *dev = &pdev->dev;
2962 struct device_node *child, *np = dev->of_node;
2964 desc = of_device_get_match_data(dev);
2968 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
2972 info->id = ida_alloc_min(&scmi_id, 0, GFP_KERNEL);
2978 info->bus_nb.notifier_call = scmi_bus_notifier;
2979 info->dev_req_nb.notifier_call = scmi_device_request_notifier;
2980 INIT_LIST_HEAD(&info->node);
2981 idr_init(&info->protocols);
2982 mutex_init(&info->protocols_mtx);
2983 idr_init(&info->active_protocols);
2984 mutex_init(&info->devreq_mtx);
2986 platform_set_drvdata(pdev, info);
2987 idr_init(&info->tx_idr);
2988 idr_init(&info->rx_idr);
2990 handle = &info->handle;
2991 handle->dev = info->dev;
2992 handle->version = &info->version;
2993 handle->devm_protocol_acquire = scmi_devm_protocol_acquire;
2994 handle->devm_protocol_get = scmi_devm_protocol_get;
2995 handle->devm_protocol_put = scmi_devm_protocol_put;
2997 /* System wide atomic threshold for atomic ops .. if any */
2998 if (!of_property_read_u32(np, "atomic-threshold-us",
2999 &info->atomic_threshold))
3001 "SCMI System wide atomic threshold set to %d us\n",
3002 info->atomic_threshold);
3003 handle->is_transport_atomic = scmi_is_transport_atomic;
3005 if (desc->ops->link_supplier) {
3006 ret = desc->ops->link_supplier(dev);
3008 err_str = "transport not ready\n";
3013 /* Setup all channels described in the DT at first */
3014 ret = scmi_channels_setup(info);
3016 err_str = "failed to setup channels\n";
3020 ret = bus_register_notifier(&scmi_bus_type, &info->bus_nb);
3022 err_str = "failed to register bus notifier\n";
3023 goto clear_txrx_setup;
3026 ret = blocking_notifier_chain_register(&scmi_requested_devices_nh,
3029 err_str = "failed to register device notifier\n";
3030 goto clear_bus_notifier;
3033 ret = scmi_xfer_info_init(info);
3035 err_str = "failed to init xfers pool\n";
3036 goto clear_dev_req_notifier;
3039 if (scmi_top_dentry) {
3040 info->dbg = scmi_debugfs_common_setup(info);
3042 dev_warn(dev, "Failed to setup SCMI debugfs.\n");
3044 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT)) {
3045 ret = scmi_debugfs_raw_mode_setup(info);
3048 goto clear_dev_req_notifier;
3050 /* Bail out anyway when coex disabled. */
3054 /* Coex enabled, carry on in any case. */
3055 dev_info(dev, "SCMI RAW Mode COEX enabled !\n");
3059 if (scmi_notification_init(handle))
3060 dev_err(dev, "SCMI Notifications NOT available.\n");
3062 if (info->desc->atomic_enabled &&
3063 !is_transport_polling_capable(info->desc))
3065 "Transport is not polling capable. Atomic mode not supported.\n");
3068 * Trigger SCMI Base protocol initialization.
3069 * It's mandatory and won't be ever released/deinit until the
3070 * SCMI stack is shutdown/unloaded as a whole.
3072 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
3074 err_str = "unable to communicate with SCMI\n";
3076 dev_err(dev, "%s", err_str);
3079 goto notification_exit;
3082 mutex_lock(&scmi_list_mutex);
3083 list_add_tail(&info->node, &scmi_list);
3084 mutex_unlock(&scmi_list_mutex);
3086 for_each_available_child_of_node(np, child) {
3089 if (of_property_read_u32(child, "reg", &prot_id))
3092 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
3093 dev_err(dev, "Out of range protocol %d\n", prot_id);
3095 if (!scmi_is_protocol_implemented(handle, prot_id)) {
3096 dev_err(dev, "SCMI protocol %d not implemented\n",
3102 * Save this valid DT protocol descriptor amongst
3103 * @active_protocols for this SCMI instance/
3105 ret = idr_alloc(&info->active_protocols, child,
3106 prot_id, prot_id + 1, GFP_KERNEL);
3107 if (ret != prot_id) {
3108 dev_err(dev, "SCMI protocol %d already activated. Skip\n",
3114 scmi_create_protocol_devices(child, info, prot_id, NULL);
3120 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
3121 scmi_raw_mode_cleanup(info->raw);
3122 scmi_notification_exit(&info->handle);
3123 clear_dev_req_notifier:
3124 blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
3127 bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
3129 scmi_cleanup_txrx_channels(info);
3131 ida_free(&scmi_id, info->id);
3133 return dev_err_probe(dev, ret, "%s", err_str);
3136 static void scmi_remove(struct platform_device *pdev)
3139 struct scmi_info *info = platform_get_drvdata(pdev);
3140 struct device_node *child;
3142 if (IS_ENABLED(CONFIG_ARM_SCMI_RAW_MODE_SUPPORT))
3143 scmi_raw_mode_cleanup(info->raw);
3145 mutex_lock(&scmi_list_mutex);
3147 dev_warn(&pdev->dev,
3148 "Still active SCMI users will be forcibly unbound.\n");
3149 list_del(&info->node);
3150 mutex_unlock(&scmi_list_mutex);
3152 scmi_notification_exit(&info->handle);
3154 mutex_lock(&info->protocols_mtx);
3155 idr_destroy(&info->protocols);
3156 mutex_unlock(&info->protocols_mtx);
3158 idr_for_each_entry(&info->active_protocols, child, id)
3160 idr_destroy(&info->active_protocols);
3162 blocking_notifier_chain_unregister(&scmi_requested_devices_nh,
3164 bus_unregister_notifier(&scmi_bus_type, &info->bus_nb);
3166 /* Safe to free channels since no more users */
3167 scmi_cleanup_txrx_channels(info);
3169 ida_free(&scmi_id, info->id);
3172 static ssize_t protocol_version_show(struct device *dev,
3173 struct device_attribute *attr, char *buf)
3175 struct scmi_info *info = dev_get_drvdata(dev);
3177 return sprintf(buf, "%u.%u\n", info->version.major_ver,
3178 info->version.minor_ver);
3180 static DEVICE_ATTR_RO(protocol_version);
3182 static ssize_t firmware_version_show(struct device *dev,
3183 struct device_attribute *attr, char *buf)
3185 struct scmi_info *info = dev_get_drvdata(dev);
3187 return sprintf(buf, "0x%x\n", info->version.impl_ver);
3189 static DEVICE_ATTR_RO(firmware_version);
3191 static ssize_t vendor_id_show(struct device *dev,
3192 struct device_attribute *attr, char *buf)
3194 struct scmi_info *info = dev_get_drvdata(dev);
3196 return sprintf(buf, "%s\n", info->version.vendor_id);
3198 static DEVICE_ATTR_RO(vendor_id);
3200 static ssize_t sub_vendor_id_show(struct device *dev,
3201 struct device_attribute *attr, char *buf)
3203 struct scmi_info *info = dev_get_drvdata(dev);
3205 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
3207 static DEVICE_ATTR_RO(sub_vendor_id);
3209 static struct attribute *versions_attrs[] = {
3210 &dev_attr_firmware_version.attr,
3211 &dev_attr_protocol_version.attr,
3212 &dev_attr_vendor_id.attr,
3213 &dev_attr_sub_vendor_id.attr,
3216 ATTRIBUTE_GROUPS(versions);
3218 /* Each compatible listed below must have descriptor associated with it */
3219 static const struct of_device_id scmi_of_match[] = {
3220 #ifdef CONFIG_ARM_SCMI_TRANSPORT_MAILBOX
3221 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
3223 #ifdef CONFIG_ARM_SCMI_TRANSPORT_OPTEE
3224 { .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc },
3226 #ifdef CONFIG_ARM_SCMI_TRANSPORT_SMC
3227 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
3228 { .compatible = "arm,scmi-smc-param", .data = &scmi_smc_desc},
3229 { .compatible = "qcom,scmi-smc", .data = &scmi_smc_desc},
3231 #ifdef CONFIG_ARM_SCMI_TRANSPORT_VIRTIO
3232 { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
3237 MODULE_DEVICE_TABLE(of, scmi_of_match);
3239 static struct platform_driver scmi_driver = {
3242 .suppress_bind_attrs = true,
3243 .of_match_table = scmi_of_match,
3244 .dev_groups = versions_groups,
3246 .probe = scmi_probe,
3247 .remove_new = scmi_remove,
3251 * __scmi_transports_setup - Common helper to call transport-specific
3252 * .init/.exit code if provided.
3254 * @init: A flag to distinguish between init and exit.
3256 * Note that, if provided, we invoke .init/.exit functions for all the
3257 * transports currently compiled in.
3259 * Return: 0 on Success.
3261 static inline int __scmi_transports_setup(bool init)
3264 const struct of_device_id *trans;
3266 for (trans = scmi_of_match; trans->data; trans++) {
3267 const struct scmi_desc *tdesc = trans->data;
3269 if ((init && !tdesc->transport_init) ||
3270 (!init && !tdesc->transport_exit))
3274 ret = tdesc->transport_init();
3276 tdesc->transport_exit();
3279 pr_err("SCMI transport %s FAILED initialization!\n",
3288 static int __init scmi_transports_init(void)
3290 return __scmi_transports_setup(true);
3293 static void __exit scmi_transports_exit(void)
3295 __scmi_transports_setup(false);
3298 static struct dentry *scmi_debugfs_init(void)
3302 d = debugfs_create_dir("scmi", NULL);
3304 pr_err("Could NOT create SCMI top dentry.\n");
3311 static int __init scmi_driver_init(void)
3315 /* Bail out if no SCMI transport was configured */
3316 if (WARN_ON(!IS_ENABLED(CONFIG_ARM_SCMI_HAVE_TRANSPORT)))
3319 /* Initialize any compiled-in transport which provided an init/exit */
3320 ret = scmi_transports_init();
3324 if (IS_ENABLED(CONFIG_ARM_SCMI_NEED_DEBUGFS))
3325 scmi_top_dentry = scmi_debugfs_init();
3327 scmi_base_register();
3329 scmi_clock_register();
3330 scmi_perf_register();
3331 scmi_power_register();
3332 scmi_reset_register();
3333 scmi_sensors_register();
3334 scmi_voltage_register();
3335 scmi_system_register();
3336 scmi_powercap_register();
3337 scmi_pinctrl_register();
3339 return platform_driver_register(&scmi_driver);
3341 module_init(scmi_driver_init);
3343 static void __exit scmi_driver_exit(void)
3345 scmi_base_unregister();
3347 scmi_clock_unregister();
3348 scmi_perf_unregister();
3349 scmi_power_unregister();
3350 scmi_reset_unregister();
3351 scmi_sensors_unregister();
3352 scmi_voltage_unregister();
3353 scmi_system_unregister();
3354 scmi_powercap_unregister();
3355 scmi_pinctrl_unregister();
3357 scmi_transports_exit();
3359 platform_driver_unregister(&scmi_driver);
3361 debugfs_remove_recursive(scmi_top_dentry);
3363 module_exit(scmi_driver_exit);
3365 MODULE_ALIAS("platform:arm-scmi");
3367 MODULE_DESCRIPTION("ARM SCMI protocol driver");
3368 MODULE_LICENSE("GPL v2");