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/kernel.h>
23 #include <linux/ktime.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_device.h>
28 #include <linux/processor.h>
29 #include <linux/refcount.h>
30 #include <linux/slab.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/scmi.h>
38 enum scmi_error_codes {
39 SCMI_SUCCESS = 0, /* Success */
40 SCMI_ERR_SUPPORT = -1, /* Not supported */
41 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
42 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
43 SCMI_ERR_ENTRY = -4, /* Not found */
44 SCMI_ERR_RANGE = -5, /* Value out of range */
45 SCMI_ERR_BUSY = -6, /* Device busy */
46 SCMI_ERR_COMMS = -7, /* Communication Error */
47 SCMI_ERR_GENERIC = -8, /* Generic Error */
48 SCMI_ERR_HARDWARE = -9, /* Hardware Error */
49 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
53 /* List of all SCMI devices active in system */
54 static LIST_HEAD(scmi_list);
55 /* Protection for the entire list */
56 static DEFINE_MUTEX(scmi_list_mutex);
57 /* Track the unique id for the transfers for debug & profiling purpose */
58 static atomic_t transfer_last_id;
60 static DEFINE_IDR(scmi_requested_devices);
61 static DEFINE_MUTEX(scmi_requested_devices_mtx);
63 struct scmi_requested_dev {
64 const struct scmi_device_id *id_table;
65 struct list_head node;
69 * struct scmi_xfers_info - Structure to manage transfer information
71 * @xfer_block: Preallocated Message array
72 * @xfer_alloc_table: Bitmap table for allocated messages.
73 * Index of this bitmap table is also used for message
74 * sequence identifier.
75 * @xfer_lock: Protection for message allocation
77 struct scmi_xfers_info {
78 struct scmi_xfer *xfer_block;
79 unsigned long *xfer_alloc_table;
84 * struct scmi_protocol_instance - Describe an initialized protocol instance.
85 * @handle: Reference to the SCMI handle associated to this protocol instance.
86 * @proto: A reference to the protocol descriptor.
87 * @gid: A reference for per-protocol devres management.
88 * @users: A refcount to track effective users of this protocol.
89 * @priv: Reference for optional protocol private data.
90 * @ph: An embedded protocol handle that will be passed down to protocol
91 * initialization code to identify this instance.
93 * Each protocol is initialized independently once for each SCMI platform in
94 * which is defined by DT and implemented by the SCMI server fw.
96 struct scmi_protocol_instance {
97 const struct scmi_handle *handle;
98 const struct scmi_protocol *proto;
102 struct scmi_protocol_handle ph;
105 #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
108 * struct scmi_info - Structure representing a SCMI instance
110 * @dev: Device pointer
111 * @desc: SoC description for this instance
112 * @version: SCMI revision information containing protocol version,
113 * implementation version and (sub-)vendor identification.
114 * @handle: Instance of SCMI handle to send to clients
115 * @tx_minfo: Universal Transmit Message management info
116 * @rx_minfo: Universal Receive Message management info
117 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
118 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
119 * @protocols: IDR for protocols' instance descriptors initialized for
120 * this SCMI instance: populated on protocol's first attempted
122 * @protocols_mtx: A mutex to protect protocols instances initialization.
123 * @protocols_imp: List of protocols implemented, currently maximum of
124 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
125 * @active_protocols: IDR storing device_nodes for protocols actually defined
126 * in the DT and confirmed as implemented by fw.
127 * @notify_priv: Pointer to private data structure specific to notifications.
129 * @users: Number of users of this instance
133 const struct scmi_desc *desc;
134 struct scmi_revision_info version;
135 struct scmi_handle handle;
136 struct scmi_xfers_info tx_minfo;
137 struct scmi_xfers_info rx_minfo;
140 struct idr protocols;
141 /* Ensure mutual exclusive access to protocols instance array */
142 struct mutex protocols_mtx;
144 struct idr active_protocols;
146 struct list_head node;
150 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
152 static const int scmi_linux_errmap[] = {
153 /* better than switch case as long as return value is continuous */
154 0, /* SCMI_SUCCESS */
155 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
156 -EINVAL, /* SCMI_ERR_PARAM */
157 -EACCES, /* SCMI_ERR_ACCESS */
158 -ENOENT, /* SCMI_ERR_ENTRY */
159 -ERANGE, /* SCMI_ERR_RANGE */
160 -EBUSY, /* SCMI_ERR_BUSY */
161 -ECOMM, /* SCMI_ERR_COMMS */
162 -EIO, /* SCMI_ERR_GENERIC */
163 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
164 -EPROTO, /* SCMI_ERR_PROTOCOL */
167 static inline int scmi_to_linux_errno(int errno)
169 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
170 return scmi_linux_errmap[-errno];
175 * scmi_dump_header_dbg() - Helper to dump a message header.
177 * @dev: Device pointer corresponding to the SCMI entity
178 * @hdr: pointer to header.
180 static inline void scmi_dump_header_dbg(struct device *dev,
181 struct scmi_msg_hdr *hdr)
183 dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
184 hdr->id, hdr->seq, hdr->protocol_id);
187 void scmi_notification_instance_data_set(const struct scmi_handle *handle,
190 struct scmi_info *info = handle_to_scmi_info(handle);
192 info->notify_priv = priv;
193 /* Ensure updated protocol private date are visible */
197 void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
199 struct scmi_info *info = handle_to_scmi_info(handle);
201 /* Ensure protocols_private_data has been updated */
203 return info->notify_priv;
207 * scmi_xfer_get() - Allocate one message
209 * @handle: Pointer to SCMI entity handle
210 * @minfo: Pointer to Tx/Rx Message management info based on channel type
212 * Helper function which is used by various message functions that are
213 * exposed to clients of this driver for allocating a message traffic event.
215 * This function can sleep depending on pending requests already in the system
216 * for the SCMI entity. Further, this also holds a spinlock to maintain
217 * integrity of internal data structures.
219 * Return: 0 if all went fine, else corresponding error.
221 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
222 struct scmi_xfers_info *minfo)
225 struct scmi_xfer *xfer;
226 unsigned long flags, bit_pos;
227 struct scmi_info *info = handle_to_scmi_info(handle);
229 /* Keep the locked section as small as possible */
230 spin_lock_irqsave(&minfo->xfer_lock, flags);
231 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
232 info->desc->max_msg);
233 if (bit_pos == info->desc->max_msg) {
234 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
235 return ERR_PTR(-ENOMEM);
237 set_bit(bit_pos, minfo->xfer_alloc_table);
238 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
242 xfer = &minfo->xfer_block[xfer_id];
243 xfer->hdr.seq = xfer_id;
244 reinit_completion(&xfer->done);
245 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
251 * __scmi_xfer_put() - Release a message
253 * @minfo: Pointer to Tx/Rx Message management info based on channel type
254 * @xfer: message that was reserved by scmi_xfer_get
256 * This holds a spinlock to maintain integrity of internal data structures.
259 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
264 * Keep the locked section as small as possible
265 * NOTE: we might escape with smp_mb and no lock here..
266 * but just be conservative and symmetric.
268 spin_lock_irqsave(&minfo->xfer_lock, flags);
269 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
270 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
273 static void scmi_handle_notification(struct scmi_chan_info *cinfo, u32 msg_hdr)
275 struct scmi_xfer *xfer;
276 struct device *dev = cinfo->dev;
277 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
278 struct scmi_xfers_info *minfo = &info->rx_minfo;
281 ts = ktime_get_boottime();
282 xfer = scmi_xfer_get(cinfo->handle, minfo);
284 dev_err(dev, "failed to get free message slot (%ld)\n",
286 info->desc->ops->clear_channel(cinfo);
290 unpack_scmi_header(msg_hdr, &xfer->hdr);
291 scmi_dump_header_dbg(dev, &xfer->hdr);
292 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
294 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
295 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
297 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
298 xfer->hdr.protocol_id, xfer->hdr.seq,
299 MSG_TYPE_NOTIFICATION);
301 __scmi_xfer_put(minfo, xfer);
303 info->desc->ops->clear_channel(cinfo);
306 static void scmi_handle_response(struct scmi_chan_info *cinfo,
307 u16 xfer_id, u8 msg_type)
309 struct scmi_xfer *xfer;
310 struct device *dev = cinfo->dev;
311 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
312 struct scmi_xfers_info *minfo = &info->tx_minfo;
314 /* Are we even expecting this? */
315 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
316 dev_err(dev, "message for %d is not expected!\n", xfer_id);
317 info->desc->ops->clear_channel(cinfo);
321 xfer = &minfo->xfer_block[xfer_id];
323 * Even if a response was indeed expected on this slot at this point,
324 * a buggy platform could wrongly reply feeding us an unexpected
325 * delayed response we're not prepared to handle: bail-out safely
328 if (unlikely(msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done)) {
330 "Delayed Response for %d not expected! Buggy F/W ?\n",
332 info->desc->ops->clear_channel(cinfo);
333 /* It was unexpected, so nobody will clear the xfer if not us */
334 __scmi_xfer_put(minfo, xfer);
338 scmi_dump_header_dbg(dev, &xfer->hdr);
340 info->desc->ops->fetch_response(cinfo, xfer);
342 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
343 xfer->hdr.protocol_id, xfer->hdr.seq,
346 if (msg_type == MSG_TYPE_DELAYED_RESP) {
347 info->desc->ops->clear_channel(cinfo);
348 complete(xfer->async_done);
350 complete(&xfer->done);
355 * scmi_rx_callback() - callback for receiving messages
357 * @cinfo: SCMI channel info
358 * @msg_hdr: Message header
360 * Processes one received message to appropriate transfer information and
361 * signals completion of the transfer.
363 * NOTE: This function will be invoked in IRQ context, hence should be
364 * as optimal as possible.
366 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr)
368 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
369 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
372 case MSG_TYPE_NOTIFICATION:
373 scmi_handle_notification(cinfo, msg_hdr);
375 case MSG_TYPE_COMMAND:
376 case MSG_TYPE_DELAYED_RESP:
377 scmi_handle_response(cinfo, xfer_id, msg_type);
380 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
386 * xfer_put() - Release a transmit message
388 * @ph: Pointer to SCMI protocol handle
389 * @xfer: message that was reserved by scmi_xfer_get
391 static void xfer_put(const struct scmi_protocol_handle *ph,
392 struct scmi_xfer *xfer)
394 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
395 struct scmi_info *info = handle_to_scmi_info(pi->handle);
397 __scmi_xfer_put(&info->tx_minfo, xfer);
400 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
402 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
403 struct scmi_xfer *xfer, ktime_t stop)
405 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
407 return info->desc->ops->poll_done(cinfo, xfer) ||
408 ktime_after(ktime_get(), stop);
412 * do_xfer() - Do one transfer
414 * @ph: Pointer to SCMI protocol handle
415 * @xfer: Transfer to initiate and wait for response
417 * Return: -ETIMEDOUT in case of no response, if transmit error,
418 * return corresponding error, else if all goes well,
421 static int do_xfer(const struct scmi_protocol_handle *ph,
422 struct scmi_xfer *xfer)
426 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
427 struct scmi_info *info = handle_to_scmi_info(pi->handle);
428 struct device *dev = info->dev;
429 struct scmi_chan_info *cinfo;
432 * Re-instate protocol id here from protocol handle so that cannot be
433 * overridden by mistake (or malice) by the protocol code mangling with
434 * the scmi_xfer structure.
436 xfer->hdr.protocol_id = pi->proto->id;
438 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
439 if (unlikely(!cinfo))
442 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
443 xfer->hdr.protocol_id, xfer->hdr.seq,
444 xfer->hdr.poll_completion);
446 ret = info->desc->ops->send_message(cinfo, xfer);
448 dev_dbg(dev, "Failed to send message %d\n", ret);
452 if (xfer->hdr.poll_completion) {
453 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
455 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
457 if (ktime_before(ktime_get(), stop))
458 info->desc->ops->fetch_response(cinfo, xfer);
462 /* And we wait for the response. */
463 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
464 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
465 dev_err(dev, "timed out in resp(caller: %pS)\n",
471 if (!ret && xfer->hdr.status)
472 ret = scmi_to_linux_errno(xfer->hdr.status);
474 if (info->desc->ops->mark_txdone)
475 info->desc->ops->mark_txdone(cinfo, ret);
477 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
478 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
483 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
484 struct scmi_xfer *xfer)
486 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
487 struct scmi_info *info = handle_to_scmi_info(pi->handle);
489 xfer->rx.len = info->desc->max_msg_size;
492 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
495 * do_xfer_with_response() - Do one transfer and wait until the delayed
496 * response is received
498 * @ph: Pointer to SCMI protocol handle
499 * @xfer: Transfer to initiate and wait for response
501 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
502 * return corresponding error, else if all goes well, return 0.
504 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
505 struct scmi_xfer *xfer)
507 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
508 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
509 DECLARE_COMPLETION_ONSTACK(async_response);
511 xfer->hdr.protocol_id = pi->proto->id;
513 xfer->async_done = &async_response;
515 ret = do_xfer(ph, xfer);
516 if (!ret && !wait_for_completion_timeout(xfer->async_done, timeout))
519 xfer->async_done = NULL;
524 * xfer_get_init() - Allocate and initialise one message for transmit
526 * @ph: Pointer to SCMI protocol handle
527 * @msg_id: Message identifier
528 * @tx_size: transmit message size
529 * @rx_size: receive message size
530 * @p: pointer to the allocated and initialised message
532 * This function allocates the message using @scmi_xfer_get and
533 * initialise the header.
535 * Return: 0 if all went fine with @p pointing to message, else
536 * corresponding error.
538 static int xfer_get_init(const struct scmi_protocol_handle *ph,
539 u8 msg_id, size_t tx_size, size_t rx_size,
540 struct scmi_xfer **p)
543 struct scmi_xfer *xfer;
544 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
545 struct scmi_info *info = handle_to_scmi_info(pi->handle);
546 struct scmi_xfers_info *minfo = &info->tx_minfo;
547 struct device *dev = info->dev;
549 /* Ensure we have sane transfer sizes */
550 if (rx_size > info->desc->max_msg_size ||
551 tx_size > info->desc->max_msg_size)
554 xfer = scmi_xfer_get(pi->handle, minfo);
557 dev_err(dev, "failed to get free message slot(%d)\n", ret);
561 xfer->tx.len = tx_size;
562 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
563 xfer->hdr.id = msg_id;
564 xfer->hdr.protocol_id = pi->proto->id;
565 xfer->hdr.poll_completion = false;
573 * version_get() - command to get the revision of the SCMI entity
575 * @ph: Pointer to SCMI protocol handle
576 * @version: Holds returned version of protocol.
578 * Updates the SCMI information in the internal data structure.
580 * Return: 0 if all went fine, else return appropriate error.
582 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
588 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
592 ret = do_xfer(ph, t);
594 rev_info = t->rx.buf;
595 *version = le32_to_cpu(*rev_info);
603 * scmi_set_protocol_priv - Set protocol specific data at init time
605 * @ph: A reference to the protocol handle.
606 * @priv: The private data to set.
608 * Return: 0 on Success
610 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
613 struct scmi_protocol_instance *pi = ph_to_pi(ph);
621 * scmi_get_protocol_priv - Set protocol specific data at init time
623 * @ph: A reference to the protocol handle.
625 * Return: Protocol private data if any was set.
627 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
629 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
634 static const struct scmi_xfer_ops xfer_ops = {
635 .version_get = version_get,
636 .xfer_get_init = xfer_get_init,
637 .reset_rx_to_maxsz = reset_rx_to_maxsz,
639 .do_xfer_with_response = do_xfer_with_response,
640 .xfer_put = xfer_put,
644 * scmi_revision_area_get - Retrieve version memory area.
646 * @ph: A reference to the protocol handle.
648 * A helper to grab the version memory area reference during SCMI Base protocol
651 * Return: A reference to the version memory area associated to the SCMI
652 * instance underlying this protocol handle.
654 struct scmi_revision_info *
655 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
657 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
659 return pi->handle->version;
663 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
664 * instance descriptor.
665 * @info: The reference to the related SCMI instance.
666 * @proto: The protocol descriptor.
668 * Allocate a new protocol instance descriptor, using the provided @proto
669 * description, against the specified SCMI instance @info, and initialize it;
670 * all resources management is handled via a dedicated per-protocol devres
673 * Context: Assumes to be called with @protocols_mtx already acquired.
674 * Return: A reference to a freshly allocated and initialized protocol instance
675 * or ERR_PTR on failure. On failure the @proto reference is at first
676 * put using @scmi_protocol_put() before releasing all the devres group.
678 static struct scmi_protocol_instance *
679 scmi_alloc_init_protocol_instance(struct scmi_info *info,
680 const struct scmi_protocol *proto)
684 struct scmi_protocol_instance *pi;
685 const struct scmi_handle *handle = &info->handle;
687 /* Protocol specific devres group */
688 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
690 scmi_protocol_put(proto->id);
694 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
701 pi->ph.dev = handle->dev;
702 pi->ph.xops = &xfer_ops;
703 pi->ph.set_priv = scmi_set_protocol_priv;
704 pi->ph.get_priv = scmi_get_protocol_priv;
705 refcount_set(&pi->users, 1);
706 /* proto->init is assured NON NULL by scmi_protocol_register */
707 ret = pi->proto->instance_init(&pi->ph);
711 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
713 if (ret != proto->id)
717 * Warn but ignore events registration errors since we do not want
718 * to skip whole protocols if their notifications are messed up.
720 if (pi->proto->events) {
721 ret = scmi_register_protocol_events(handle, pi->proto->id,
725 dev_warn(handle->dev,
726 "Protocol:%X - Events Registration Failed - err:%d\n",
730 devres_close_group(handle->dev, pi->gid);
731 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
736 /* Take care to put the protocol module's owner before releasing all */
737 scmi_protocol_put(proto->id);
738 devres_release_group(handle->dev, gid);
744 * scmi_get_protocol_instance - Protocol initialization helper.
745 * @handle: A reference to the SCMI platform instance.
746 * @protocol_id: The protocol being requested.
748 * In case the required protocol has never been requested before for this
749 * instance, allocate and initialize all the needed structures while handling
750 * resource allocation with a dedicated per-protocol devres subgroup.
752 * Return: A reference to an initialized protocol instance or error on failure:
753 * in particular returns -EPROBE_DEFER when the desired protocol could
756 static struct scmi_protocol_instance * __must_check
757 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
759 struct scmi_protocol_instance *pi;
760 struct scmi_info *info = handle_to_scmi_info(handle);
762 mutex_lock(&info->protocols_mtx);
763 pi = idr_find(&info->protocols, protocol_id);
766 refcount_inc(&pi->users);
768 const struct scmi_protocol *proto;
770 /* Fails if protocol not registered on bus */
771 proto = scmi_protocol_get(protocol_id);
773 pi = scmi_alloc_init_protocol_instance(info, proto);
775 pi = ERR_PTR(-EPROBE_DEFER);
777 mutex_unlock(&info->protocols_mtx);
783 * scmi_protocol_acquire - Protocol acquire
784 * @handle: A reference to the SCMI platform instance.
785 * @protocol_id: The protocol being requested.
787 * Register a new user for the requested protocol on the specified SCMI
788 * platform instance, possibly triggering its initialization on first user.
790 * Return: 0 if protocol was acquired successfully.
792 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
794 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
798 * scmi_protocol_release - Protocol de-initialization helper.
799 * @handle: A reference to the SCMI platform instance.
800 * @protocol_id: The protocol being requested.
802 * Remove one user for the specified protocol and triggers de-initialization
803 * and resources de-allocation once the last user has gone.
805 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
807 struct scmi_info *info = handle_to_scmi_info(handle);
808 struct scmi_protocol_instance *pi;
810 mutex_lock(&info->protocols_mtx);
811 pi = idr_find(&info->protocols, protocol_id);
815 if (refcount_dec_and_test(&pi->users)) {
818 if (pi->proto->events)
819 scmi_deregister_protocol_events(handle, protocol_id);
821 if (pi->proto->instance_deinit)
822 pi->proto->instance_deinit(&pi->ph);
824 idr_remove(&info->protocols, protocol_id);
826 scmi_protocol_put(protocol_id);
828 devres_release_group(handle->dev, gid);
829 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
834 mutex_unlock(&info->protocols_mtx);
837 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
840 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
841 struct scmi_info *info = handle_to_scmi_info(pi->handle);
843 info->protocols_imp = prot_imp;
847 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
850 struct scmi_info *info = handle_to_scmi_info(handle);
852 if (!info->protocols_imp)
855 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
856 if (info->protocols_imp[i] == prot_id)
861 struct scmi_protocol_devres {
862 const struct scmi_handle *handle;
866 static void scmi_devm_release_protocol(struct device *dev, void *res)
868 struct scmi_protocol_devres *dres = res;
870 scmi_protocol_release(dres->handle, dres->protocol_id);
874 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
875 * @sdev: A reference to an scmi_device whose embedded struct device is to
876 * be used for devres accounting.
877 * @protocol_id: The protocol being requested.
878 * @ph: A pointer reference used to pass back the associated protocol handle.
880 * Get hold of a protocol accounting for its usage, eventually triggering its
881 * initialization, and returning the protocol specific operations and related
882 * protocol handle which will be used as first argument in most of the
883 * protocols operations methods.
884 * Being a devres based managed method, protocol hold will be automatically
885 * released, and possibly de-initialized on last user, once the SCMI driver
886 * owning the scmi_device is unbound from it.
888 * Return: A reference to the requested protocol operations or error.
889 * Must be checked for errors by caller.
891 static const void __must_check *
892 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
893 struct scmi_protocol_handle **ph)
895 struct scmi_protocol_instance *pi;
896 struct scmi_protocol_devres *dres;
897 struct scmi_handle *handle = sdev->handle;
900 return ERR_PTR(-EINVAL);
902 dres = devres_alloc(scmi_devm_release_protocol,
903 sizeof(*dres), GFP_KERNEL);
905 return ERR_PTR(-ENOMEM);
907 pi = scmi_get_protocol_instance(handle, protocol_id);
913 dres->handle = handle;
914 dres->protocol_id = protocol_id;
915 devres_add(&sdev->dev, dres);
919 return pi->proto->ops;
922 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
924 struct scmi_protocol_devres *dres = res;
926 if (WARN_ON(!dres || !data))
929 return dres->protocol_id == *((u8 *)data);
933 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
934 * @sdev: A reference to an scmi_device whose embedded struct device is to
935 * be used for devres accounting.
936 * @protocol_id: The protocol being requested.
938 * Explicitly release a protocol hold previously obtained calling the above
939 * @scmi_devm_protocol_get.
941 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
945 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
946 scmi_devm_protocol_match, &protocol_id);
951 struct scmi_handle *scmi_handle_get_from_info_unlocked(struct scmi_info *info)
954 return &info->handle;
958 * scmi_handle_get() - Get the SCMI handle for a device
960 * @dev: pointer to device for which we want SCMI handle
962 * NOTE: The function does not track individual clients of the framework
963 * and is expected to be maintained by caller of SCMI protocol library.
964 * scmi_handle_put must be balanced with successful scmi_handle_get
966 * Return: pointer to handle if successful, NULL on error
968 struct scmi_handle *scmi_handle_get(struct device *dev)
971 struct scmi_info *info;
972 struct scmi_handle *handle = NULL;
974 mutex_lock(&scmi_list_mutex);
975 list_for_each(p, &scmi_list) {
976 info = list_entry(p, struct scmi_info, node);
977 if (dev->parent == info->dev) {
978 handle = scmi_handle_get_from_info_unlocked(info);
982 mutex_unlock(&scmi_list_mutex);
988 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
990 * @handle: handle acquired by scmi_handle_get
992 * NOTE: The function does not track individual clients of the framework
993 * and is expected to be maintained by caller of SCMI protocol library.
994 * scmi_handle_put must be balanced with successful scmi_handle_get
996 * Return: 0 is successfully released
997 * if null was passed, it returns -EINVAL;
999 int scmi_handle_put(const struct scmi_handle *handle)
1001 struct scmi_info *info;
1006 info = handle_to_scmi_info(handle);
1007 mutex_lock(&scmi_list_mutex);
1008 if (!WARN_ON(!info->users))
1010 mutex_unlock(&scmi_list_mutex);
1015 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
1016 struct scmi_xfers_info *info)
1019 struct scmi_xfer *xfer;
1020 struct device *dev = sinfo->dev;
1021 const struct scmi_desc *desc = sinfo->desc;
1023 /* Pre-allocated messages, no more than what hdr.seq can support */
1024 if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
1025 dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
1026 desc->max_msg, MSG_TOKEN_MAX);
1030 info->xfer_block = devm_kcalloc(dev, desc->max_msg,
1031 sizeof(*info->xfer_block), GFP_KERNEL);
1032 if (!info->xfer_block)
1035 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
1036 sizeof(long), GFP_KERNEL);
1037 if (!info->xfer_alloc_table)
1040 /* Pre-initialize the buffer pointer to pre-allocated buffers */
1041 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
1042 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
1047 xfer->tx.buf = xfer->rx.buf;
1048 init_completion(&xfer->done);
1051 spin_lock_init(&info->xfer_lock);
1056 static int scmi_xfer_info_init(struct scmi_info *sinfo)
1058 int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
1060 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1061 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1066 static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1067 int prot_id, bool tx)
1070 struct scmi_chan_info *cinfo;
1073 /* Transmit channel is first entry i.e. index 0 */
1075 idr = tx ? &info->tx_idr : &info->rx_idr;
1077 /* check if already allocated, used for multiple device per protocol */
1078 cinfo = idr_find(idr, prot_id);
1082 if (!info->desc->ops->chan_available(dev, idx)) {
1083 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
1084 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
1089 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
1095 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
1100 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
1101 if (ret != prot_id) {
1102 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
1106 cinfo->handle = &info->handle;
1111 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
1113 int ret = scmi_chan_setup(info, dev, prot_id, true);
1115 if (!ret) /* Rx is optional, hence no error check */
1116 scmi_chan_setup(info, dev, prot_id, false);
1122 * scmi_get_protocol_device - Helper to get/create an SCMI device.
1124 * @np: A device node representing a valid active protocols for the referred
1126 * @info: The referred SCMI instance for which we are getting/creating this
1128 * @prot_id: The protocol ID.
1129 * @name: The device name.
1131 * Referring to the specific SCMI instance identified by @info, this helper
1132 * takes care to return a properly initialized device matching the requested
1133 * @proto_id and @name: if device was still not existent it is created as a
1134 * child of the specified SCMI instance @info and its transport properly
1135 * initialized as usual.
1137 static inline struct scmi_device *
1138 scmi_get_protocol_device(struct device_node *np, struct scmi_info *info,
1139 int prot_id, const char *name)
1141 struct scmi_device *sdev;
1143 /* Already created for this parent SCMI instance ? */
1144 sdev = scmi_child_dev_find(info->dev, prot_id, name);
1148 pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id);
1150 sdev = scmi_device_create(np, info->dev, prot_id, name);
1152 dev_err(info->dev, "failed to create %d protocol device\n",
1157 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
1158 dev_err(&sdev->dev, "failed to setup transport\n");
1159 scmi_device_destroy(sdev);
1167 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
1168 int prot_id, const char *name)
1170 struct scmi_device *sdev;
1172 sdev = scmi_get_protocol_device(np, info, prot_id, name);
1176 /* setup handle now as the transport is ready */
1177 scmi_set_handle(sdev);
1181 * scmi_create_protocol_devices - Create devices for all pending requests for
1182 * this SCMI instance.
1184 * @np: The device node describing the protocol
1185 * @info: The SCMI instance descriptor
1186 * @prot_id: The protocol ID
1188 * All devices previously requested for this instance (if any) are found and
1189 * created by scanning the proper @&scmi_requested_devices entry.
1191 static void scmi_create_protocol_devices(struct device_node *np,
1192 struct scmi_info *info, int prot_id)
1194 struct list_head *phead;
1196 mutex_lock(&scmi_requested_devices_mtx);
1197 phead = idr_find(&scmi_requested_devices, prot_id);
1199 struct scmi_requested_dev *rdev;
1201 list_for_each_entry(rdev, phead, node)
1202 scmi_create_protocol_device(np, info, prot_id,
1203 rdev->id_table->name);
1205 mutex_unlock(&scmi_requested_devices_mtx);
1209 * scmi_protocol_device_request - Helper to request a device
1211 * @id_table: A protocol/name pair descriptor for the device to be created.
1213 * This helper let an SCMI driver request specific devices identified by the
1214 * @id_table to be created for each active SCMI instance.
1216 * The requested device name MUST NOT be already existent for any protocol;
1217 * at first the freshly requested @id_table is annotated in the IDR table
1218 * @scmi_requested_devices, then a matching device is created for each already
1219 * active SCMI instance. (if any)
1221 * This way the requested device is created straight-away for all the already
1222 * initialized(probed) SCMI instances (handles) and it remains also annotated
1223 * as pending creation if the requesting SCMI driver was loaded before some
1224 * SCMI instance and related transports were available: when such late instance
1225 * is probed, its probe will take care to scan the list of pending requested
1226 * devices and create those on its own (see @scmi_create_protocol_devices and
1227 * its enclosing loop)
1229 * Return: 0 on Success
1231 int scmi_protocol_device_request(const struct scmi_device_id *id_table)
1234 unsigned int id = 0;
1235 struct list_head *head, *phead = NULL;
1236 struct scmi_requested_dev *rdev;
1237 struct scmi_info *info;
1239 pr_debug("Requesting SCMI device (%s) for protocol %x\n",
1240 id_table->name, id_table->protocol_id);
1243 * Search for the matching protocol rdev list and then search
1244 * of any existent equally named device...fails if any duplicate found.
1246 mutex_lock(&scmi_requested_devices_mtx);
1247 idr_for_each_entry(&scmi_requested_devices, head, id) {
1249 /* A list found registered in the IDR is never empty */
1250 rdev = list_first_entry(head, struct scmi_requested_dev,
1252 if (rdev->id_table->protocol_id ==
1253 id_table->protocol_id)
1256 list_for_each_entry(rdev, head, node) {
1257 if (!strcmp(rdev->id_table->name, id_table->name)) {
1258 pr_err("Ignoring duplicate request [%d] %s\n",
1259 rdev->id_table->protocol_id,
1260 rdev->id_table->name);
1268 * No duplicate found for requested id_table, so let's create a new
1269 * requested device entry for this new valid request.
1271 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1276 rdev->id_table = id_table;
1279 * Append the new requested device table descriptor to the head of the
1280 * related protocol list, eventually creating such head if not already
1284 phead = kzalloc(sizeof(*phead), GFP_KERNEL);
1290 INIT_LIST_HEAD(phead);
1292 ret = idr_alloc(&scmi_requested_devices, (void *)phead,
1293 id_table->protocol_id,
1294 id_table->protocol_id + 1, GFP_KERNEL);
1295 if (ret != id_table->protocol_id) {
1296 pr_err("Failed to save SCMI device - ret:%d\n", ret);
1304 list_add(&rdev->node, phead);
1307 * Now effectively create and initialize the requested device for every
1308 * already initialized SCMI instance which has registered the requested
1309 * protocol as a valid active one: i.e. defined in DT and supported by
1310 * current platform FW.
1312 mutex_lock(&scmi_list_mutex);
1313 list_for_each_entry(info, &scmi_list, node) {
1314 struct device_node *child;
1316 child = idr_find(&info->active_protocols,
1317 id_table->protocol_id);
1319 struct scmi_device *sdev;
1321 sdev = scmi_get_protocol_device(child, info,
1322 id_table->protocol_id,
1324 /* Set handle if not already set: device existed */
1325 if (sdev && !sdev->handle)
1327 scmi_handle_get_from_info_unlocked(info);
1330 "Failed. SCMI protocol %d not active.\n",
1331 id_table->protocol_id);
1334 mutex_unlock(&scmi_list_mutex);
1337 mutex_unlock(&scmi_requested_devices_mtx);
1343 * scmi_protocol_device_unrequest - Helper to unrequest a device
1345 * @id_table: A protocol/name pair descriptor for the device to be unrequested.
1347 * An helper to let an SCMI driver release its request about devices; note that
1348 * devices are created and initialized once the first SCMI driver request them
1349 * but they destroyed only on SCMI core unloading/unbinding.
1351 * The current SCMI transport layer uses such devices as internal references and
1352 * as such they could be shared as same transport between multiple drivers so
1353 * that cannot be safely destroyed till the whole SCMI stack is removed.
1354 * (unless adding further burden of refcounting.)
1356 void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
1358 struct list_head *phead;
1360 pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
1361 id_table->name, id_table->protocol_id);
1363 mutex_lock(&scmi_requested_devices_mtx);
1364 phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
1366 struct scmi_requested_dev *victim, *tmp;
1368 list_for_each_entry_safe(victim, tmp, phead, node) {
1369 if (!strcmp(victim->id_table->name, id_table->name)) {
1370 list_del(&victim->node);
1376 if (list_empty(phead)) {
1377 idr_remove(&scmi_requested_devices,
1378 id_table->protocol_id);
1382 mutex_unlock(&scmi_requested_devices_mtx);
1385 static int scmi_probe(struct platform_device *pdev)
1388 struct scmi_handle *handle;
1389 const struct scmi_desc *desc;
1390 struct scmi_info *info;
1391 struct device *dev = &pdev->dev;
1392 struct device_node *child, *np = dev->of_node;
1394 desc = of_device_get_match_data(dev);
1398 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1404 INIT_LIST_HEAD(&info->node);
1405 idr_init(&info->protocols);
1406 mutex_init(&info->protocols_mtx);
1407 idr_init(&info->active_protocols);
1409 platform_set_drvdata(pdev, info);
1410 idr_init(&info->tx_idr);
1411 idr_init(&info->rx_idr);
1413 handle = &info->handle;
1414 handle->dev = info->dev;
1415 handle->version = &info->version;
1416 handle->devm_protocol_get = scmi_devm_protocol_get;
1417 handle->devm_protocol_put = scmi_devm_protocol_put;
1419 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
1423 ret = scmi_xfer_info_init(info);
1427 if (scmi_notification_init(handle))
1428 dev_err(dev, "SCMI Notifications NOT available.\n");
1431 * Trigger SCMI Base protocol initialization.
1432 * It's mandatory and won't be ever released/deinit until the
1433 * SCMI stack is shutdown/unloaded as a whole.
1435 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
1437 dev_err(dev, "unable to communicate with SCMI\n");
1441 mutex_lock(&scmi_list_mutex);
1442 list_add_tail(&info->node, &scmi_list);
1443 mutex_unlock(&scmi_list_mutex);
1445 for_each_available_child_of_node(np, child) {
1448 if (of_property_read_u32(child, "reg", &prot_id))
1451 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
1452 dev_err(dev, "Out of range protocol %d\n", prot_id);
1454 if (!scmi_is_protocol_implemented(handle, prot_id)) {
1455 dev_err(dev, "SCMI protocol %d not implemented\n",
1461 * Save this valid DT protocol descriptor amongst
1462 * @active_protocols for this SCMI instance/
1464 ret = idr_alloc(&info->active_protocols, child,
1465 prot_id, prot_id + 1, GFP_KERNEL);
1466 if (ret != prot_id) {
1467 dev_err(dev, "SCMI protocol %d already activated. Skip\n",
1473 scmi_create_protocol_devices(child, info, prot_id);
1479 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
1481 idr_remove(idr, id);
1484 static int scmi_remove(struct platform_device *pdev)
1487 struct scmi_info *info = platform_get_drvdata(pdev);
1488 struct idr *idr = &info->tx_idr;
1489 struct device_node *child;
1491 mutex_lock(&scmi_list_mutex);
1495 list_del(&info->node);
1496 mutex_unlock(&scmi_list_mutex);
1501 scmi_notification_exit(&info->handle);
1503 mutex_lock(&info->protocols_mtx);
1504 idr_destroy(&info->protocols);
1505 mutex_unlock(&info->protocols_mtx);
1507 idr_for_each_entry(&info->active_protocols, child, id)
1509 idr_destroy(&info->active_protocols);
1511 /* Safe to free channels since no more users */
1512 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1513 idr_destroy(&info->tx_idr);
1515 idr = &info->rx_idr;
1516 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1517 idr_destroy(&info->rx_idr);
1522 static ssize_t protocol_version_show(struct device *dev,
1523 struct device_attribute *attr, char *buf)
1525 struct scmi_info *info = dev_get_drvdata(dev);
1527 return sprintf(buf, "%u.%u\n", info->version.major_ver,
1528 info->version.minor_ver);
1530 static DEVICE_ATTR_RO(protocol_version);
1532 static ssize_t firmware_version_show(struct device *dev,
1533 struct device_attribute *attr, char *buf)
1535 struct scmi_info *info = dev_get_drvdata(dev);
1537 return sprintf(buf, "0x%x\n", info->version.impl_ver);
1539 static DEVICE_ATTR_RO(firmware_version);
1541 static ssize_t vendor_id_show(struct device *dev,
1542 struct device_attribute *attr, char *buf)
1544 struct scmi_info *info = dev_get_drvdata(dev);
1546 return sprintf(buf, "%s\n", info->version.vendor_id);
1548 static DEVICE_ATTR_RO(vendor_id);
1550 static ssize_t sub_vendor_id_show(struct device *dev,
1551 struct device_attribute *attr, char *buf)
1553 struct scmi_info *info = dev_get_drvdata(dev);
1555 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
1557 static DEVICE_ATTR_RO(sub_vendor_id);
1559 static struct attribute *versions_attrs[] = {
1560 &dev_attr_firmware_version.attr,
1561 &dev_attr_protocol_version.attr,
1562 &dev_attr_vendor_id.attr,
1563 &dev_attr_sub_vendor_id.attr,
1566 ATTRIBUTE_GROUPS(versions);
1568 /* Each compatible listed below must have descriptor associated with it */
1569 static const struct of_device_id scmi_of_match[] = {
1570 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
1571 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
1572 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
1577 MODULE_DEVICE_TABLE(of, scmi_of_match);
1579 static struct platform_driver scmi_driver = {
1582 .of_match_table = scmi_of_match,
1583 .dev_groups = versions_groups,
1585 .probe = scmi_probe,
1586 .remove = scmi_remove,
1589 static int __init scmi_driver_init(void)
1593 scmi_base_register();
1595 scmi_clock_register();
1596 scmi_perf_register();
1597 scmi_power_register();
1598 scmi_reset_register();
1599 scmi_sensors_register();
1600 scmi_voltage_register();
1601 scmi_system_register();
1603 return platform_driver_register(&scmi_driver);
1605 subsys_initcall(scmi_driver_init);
1607 static void __exit scmi_driver_exit(void)
1609 scmi_base_unregister();
1611 scmi_clock_unregister();
1612 scmi_perf_unregister();
1613 scmi_power_unregister();
1614 scmi_reset_unregister();
1615 scmi_sensors_unregister();
1616 scmi_voltage_unregister();
1617 scmi_system_unregister();
1621 platform_driver_unregister(&scmi_driver);
1623 module_exit(scmi_driver_exit);
1625 MODULE_ALIAS("platform: arm-scmi");
1626 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1627 MODULE_DESCRIPTION("ARM SCMI protocol driver");
1628 MODULE_LICENSE("GPL v2");