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 */
52 /* List of all SCMI devices active in system */
53 static LIST_HEAD(scmi_list);
54 /* Protection for the entire list */
55 static DEFINE_MUTEX(scmi_list_mutex);
56 /* Track the unique id for the transfers for debug & profiling purpose */
57 static atomic_t transfer_last_id;
59 static DEFINE_IDR(scmi_requested_devices);
60 static DEFINE_MUTEX(scmi_requested_devices_mtx);
62 struct scmi_requested_dev {
63 const struct scmi_device_id *id_table;
64 struct list_head node;
68 * struct scmi_xfers_info - Structure to manage transfer information
70 * @xfer_block: Preallocated Message array
71 * @xfer_alloc_table: Bitmap table for allocated messages.
72 * Index of this bitmap table is also used for message
73 * sequence identifier.
74 * @xfer_lock: Protection for message allocation
76 struct scmi_xfers_info {
77 struct scmi_xfer *xfer_block;
78 unsigned long *xfer_alloc_table;
83 * struct scmi_protocol_instance - Describe an initialized protocol instance.
84 * @handle: Reference to the SCMI handle associated to this protocol instance.
85 * @proto: A reference to the protocol descriptor.
86 * @gid: A reference for per-protocol devres management.
87 * @users: A refcount to track effective users of this protocol.
88 * @priv: Reference for optional protocol private data.
89 * @ph: An embedded protocol handle that will be passed down to protocol
90 * initialization code to identify this instance.
92 * Each protocol is initialized independently once for each SCMI platform in
93 * which is defined by DT and implemented by the SCMI server fw.
95 struct scmi_protocol_instance {
96 const struct scmi_handle *handle;
97 const struct scmi_protocol *proto;
101 struct scmi_protocol_handle ph;
104 #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
107 * struct scmi_info - Structure representing a SCMI instance
109 * @dev: Device pointer
110 * @desc: SoC description for this instance
111 * @version: SCMI revision information containing protocol version,
112 * implementation version and (sub-)vendor identification.
113 * @handle: Instance of SCMI handle to send to clients
114 * @tx_minfo: Universal Transmit Message management info
115 * @rx_minfo: Universal Receive Message management info
116 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
117 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
118 * @protocols: IDR for protocols' instance descriptors initialized for
119 * this SCMI instance: populated on protocol's first attempted
121 * @protocols_mtx: A mutex to protect protocols instances initialization.
122 * @protocols_imp: List of protocols implemented, currently maximum of
123 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
124 * @active_protocols: IDR storing device_nodes for protocols actually defined
125 * in the DT and confirmed as implemented by fw.
126 * @notify_priv: Pointer to private data structure specific to notifications.
128 * @users: Number of users of this instance
132 const struct scmi_desc *desc;
133 struct scmi_revision_info version;
134 struct scmi_handle handle;
135 struct scmi_xfers_info tx_minfo;
136 struct scmi_xfers_info rx_minfo;
139 struct idr protocols;
140 /* Ensure mutual exclusive access to protocols instance array */
141 struct mutex protocols_mtx;
143 struct idr active_protocols;
145 struct list_head node;
149 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
151 static const int scmi_linux_errmap[] = {
152 /* better than switch case as long as return value is continuous */
153 0, /* SCMI_SUCCESS */
154 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
155 -EINVAL, /* SCMI_ERR_PARAM */
156 -EACCES, /* SCMI_ERR_ACCESS */
157 -ENOENT, /* SCMI_ERR_ENTRY */
158 -ERANGE, /* SCMI_ERR_RANGE */
159 -EBUSY, /* SCMI_ERR_BUSY */
160 -ECOMM, /* SCMI_ERR_COMMS */
161 -EIO, /* SCMI_ERR_GENERIC */
162 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
163 -EPROTO, /* SCMI_ERR_PROTOCOL */
166 static inline int scmi_to_linux_errno(int errno)
168 int err_idx = -errno;
170 if (err_idx >= SCMI_SUCCESS && err_idx < ARRAY_SIZE(scmi_linux_errmap))
171 return scmi_linux_errmap[err_idx];
176 * scmi_dump_header_dbg() - Helper to dump a message header.
178 * @dev: Device pointer corresponding to the SCMI entity
179 * @hdr: pointer to header.
181 static inline void scmi_dump_header_dbg(struct device *dev,
182 struct scmi_msg_hdr *hdr)
184 dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
185 hdr->id, hdr->seq, hdr->protocol_id);
188 void scmi_notification_instance_data_set(const struct scmi_handle *handle,
191 struct scmi_info *info = handle_to_scmi_info(handle);
193 info->notify_priv = priv;
194 /* Ensure updated protocol private date are visible */
198 void *scmi_notification_instance_data_get(const struct scmi_handle *handle)
200 struct scmi_info *info = handle_to_scmi_info(handle);
202 /* Ensure protocols_private_data has been updated */
204 return info->notify_priv;
208 * scmi_xfer_get() - Allocate one message
210 * @handle: Pointer to SCMI entity handle
211 * @minfo: Pointer to Tx/Rx Message management info based on channel type
213 * Helper function which is used by various message functions that are
214 * exposed to clients of this driver for allocating a message traffic event.
216 * This function can sleep depending on pending requests already in the system
217 * for the SCMI entity. Further, this also holds a spinlock to maintain
218 * integrity of internal data structures.
220 * Return: 0 if all went fine, else corresponding error.
222 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
223 struct scmi_xfers_info *minfo)
226 struct scmi_xfer *xfer;
227 unsigned long flags, bit_pos;
228 struct scmi_info *info = handle_to_scmi_info(handle);
230 /* Keep the locked section as small as possible */
231 spin_lock_irqsave(&minfo->xfer_lock, flags);
232 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
233 info->desc->max_msg);
234 if (bit_pos == info->desc->max_msg) {
235 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
236 return ERR_PTR(-ENOMEM);
238 set_bit(bit_pos, minfo->xfer_alloc_table);
239 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
243 xfer = &minfo->xfer_block[xfer_id];
244 xfer->hdr.seq = xfer_id;
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 /* rx.len could be shrunk in the sync do_xfer, so reset to maxsz */
339 if (msg_type == MSG_TYPE_DELAYED_RESP)
340 xfer->rx.len = info->desc->max_msg_size;
342 scmi_dump_header_dbg(dev, &xfer->hdr);
344 info->desc->ops->fetch_response(cinfo, xfer);
346 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
347 xfer->hdr.protocol_id, xfer->hdr.seq,
350 if (msg_type == MSG_TYPE_DELAYED_RESP) {
351 info->desc->ops->clear_channel(cinfo);
352 complete(xfer->async_done);
354 complete(&xfer->done);
359 * scmi_rx_callback() - callback for receiving messages
361 * @cinfo: SCMI channel info
362 * @msg_hdr: Message header
364 * Processes one received message to appropriate transfer information and
365 * signals completion of the transfer.
367 * NOTE: This function will be invoked in IRQ context, hence should be
368 * as optimal as possible.
370 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr)
372 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
373 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
376 case MSG_TYPE_NOTIFICATION:
377 scmi_handle_notification(cinfo, msg_hdr);
379 case MSG_TYPE_COMMAND:
380 case MSG_TYPE_DELAYED_RESP:
381 scmi_handle_response(cinfo, xfer_id, msg_type);
384 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
390 * xfer_put() - Release a transmit message
392 * @ph: Pointer to SCMI protocol handle
393 * @xfer: message that was reserved by scmi_xfer_get
395 static void xfer_put(const struct scmi_protocol_handle *ph,
396 struct scmi_xfer *xfer)
398 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
399 struct scmi_info *info = handle_to_scmi_info(pi->handle);
401 __scmi_xfer_put(&info->tx_minfo, xfer);
404 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
406 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
407 struct scmi_xfer *xfer, ktime_t stop)
409 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
411 return info->desc->ops->poll_done(cinfo, xfer) ||
412 ktime_after(ktime_get(), stop);
416 * do_xfer() - Do one transfer
418 * @ph: Pointer to SCMI protocol handle
419 * @xfer: Transfer to initiate and wait for response
421 * Return: -ETIMEDOUT in case of no response, if transmit error,
422 * return corresponding error, else if all goes well,
425 static int do_xfer(const struct scmi_protocol_handle *ph,
426 struct scmi_xfer *xfer)
430 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
431 struct scmi_info *info = handle_to_scmi_info(pi->handle);
432 struct device *dev = info->dev;
433 struct scmi_chan_info *cinfo;
436 * Initialise protocol id now from protocol handle to avoid it being
437 * overridden by mistake (or malice) by the protocol code mangling with
438 * the scmi_xfer structure prior to this.
440 xfer->hdr.protocol_id = pi->proto->id;
441 reinit_completion(&xfer->done);
443 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
444 if (unlikely(!cinfo))
447 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
448 xfer->hdr.protocol_id, xfer->hdr.seq,
449 xfer->hdr.poll_completion);
451 ret = info->desc->ops->send_message(cinfo, xfer);
453 dev_dbg(dev, "Failed to send message %d\n", ret);
457 if (xfer->hdr.poll_completion) {
458 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
460 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
462 if (ktime_before(ktime_get(), stop))
463 info->desc->ops->fetch_response(cinfo, xfer);
467 /* And we wait for the response. */
468 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
469 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
470 dev_err(dev, "timed out in resp(caller: %pS)\n",
476 if (!ret && xfer->hdr.status)
477 ret = scmi_to_linux_errno(xfer->hdr.status);
479 if (info->desc->ops->mark_txdone)
480 info->desc->ops->mark_txdone(cinfo, ret);
482 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
483 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
488 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
489 struct scmi_xfer *xfer)
491 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
492 struct scmi_info *info = handle_to_scmi_info(pi->handle);
494 xfer->rx.len = info->desc->max_msg_size;
497 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
500 * do_xfer_with_response() - Do one transfer and wait until the delayed
501 * response is received
503 * @ph: Pointer to SCMI protocol handle
504 * @xfer: Transfer to initiate and wait for response
506 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
507 * return corresponding error, else if all goes well, return 0.
509 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
510 struct scmi_xfer *xfer)
512 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
513 DECLARE_COMPLETION_ONSTACK(async_response);
515 xfer->async_done = &async_response;
517 ret = do_xfer(ph, xfer);
519 if (!wait_for_completion_timeout(xfer->async_done, timeout))
521 else if (xfer->hdr.status)
522 ret = scmi_to_linux_errno(xfer->hdr.status);
525 xfer->async_done = NULL;
530 * xfer_get_init() - Allocate and initialise one message for transmit
532 * @ph: Pointer to SCMI protocol handle
533 * @msg_id: Message identifier
534 * @tx_size: transmit message size
535 * @rx_size: receive message size
536 * @p: pointer to the allocated and initialised message
538 * This function allocates the message using @scmi_xfer_get and
539 * initialise the header.
541 * Return: 0 if all went fine with @p pointing to message, else
542 * corresponding error.
544 static int xfer_get_init(const struct scmi_protocol_handle *ph,
545 u8 msg_id, size_t tx_size, size_t rx_size,
546 struct scmi_xfer **p)
549 struct scmi_xfer *xfer;
550 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
551 struct scmi_info *info = handle_to_scmi_info(pi->handle);
552 struct scmi_xfers_info *minfo = &info->tx_minfo;
553 struct device *dev = info->dev;
555 /* Ensure we have sane transfer sizes */
556 if (rx_size > info->desc->max_msg_size ||
557 tx_size > info->desc->max_msg_size)
560 xfer = scmi_xfer_get(pi->handle, minfo);
563 dev_err(dev, "failed to get free message slot(%d)\n", ret);
567 xfer->tx.len = tx_size;
568 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
569 xfer->hdr.id = msg_id;
570 xfer->hdr.poll_completion = false;
578 * version_get() - command to get the revision of the SCMI entity
580 * @ph: Pointer to SCMI protocol handle
581 * @version: Holds returned version of protocol.
583 * Updates the SCMI information in the internal data structure.
585 * Return: 0 if all went fine, else return appropriate error.
587 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
593 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
597 ret = do_xfer(ph, t);
599 rev_info = t->rx.buf;
600 *version = le32_to_cpu(*rev_info);
608 * scmi_set_protocol_priv - Set protocol specific data at init time
610 * @ph: A reference to the protocol handle.
611 * @priv: The private data to set.
613 * Return: 0 on Success
615 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
618 struct scmi_protocol_instance *pi = ph_to_pi(ph);
626 * scmi_get_protocol_priv - Set protocol specific data at init time
628 * @ph: A reference to the protocol handle.
630 * Return: Protocol private data if any was set.
632 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
634 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
639 static const struct scmi_xfer_ops xfer_ops = {
640 .version_get = version_get,
641 .xfer_get_init = xfer_get_init,
642 .reset_rx_to_maxsz = reset_rx_to_maxsz,
644 .do_xfer_with_response = do_xfer_with_response,
645 .xfer_put = xfer_put,
649 * scmi_revision_area_get - Retrieve version memory area.
651 * @ph: A reference to the protocol handle.
653 * A helper to grab the version memory area reference during SCMI Base protocol
656 * Return: A reference to the version memory area associated to the SCMI
657 * instance underlying this protocol handle.
659 struct scmi_revision_info *
660 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
662 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
664 return pi->handle->version;
668 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
669 * instance descriptor.
670 * @info: The reference to the related SCMI instance.
671 * @proto: The protocol descriptor.
673 * Allocate a new protocol instance descriptor, using the provided @proto
674 * description, against the specified SCMI instance @info, and initialize it;
675 * all resources management is handled via a dedicated per-protocol devres
678 * Context: Assumes to be called with @protocols_mtx already acquired.
679 * Return: A reference to a freshly allocated and initialized protocol instance
680 * or ERR_PTR on failure. On failure the @proto reference is at first
681 * put using @scmi_protocol_put() before releasing all the devres group.
683 static struct scmi_protocol_instance *
684 scmi_alloc_init_protocol_instance(struct scmi_info *info,
685 const struct scmi_protocol *proto)
689 struct scmi_protocol_instance *pi;
690 const struct scmi_handle *handle = &info->handle;
692 /* Protocol specific devres group */
693 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
695 scmi_protocol_put(proto->id);
699 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
706 pi->ph.dev = handle->dev;
707 pi->ph.xops = &xfer_ops;
708 pi->ph.set_priv = scmi_set_protocol_priv;
709 pi->ph.get_priv = scmi_get_protocol_priv;
710 refcount_set(&pi->users, 1);
711 /* proto->init is assured NON NULL by scmi_protocol_register */
712 ret = pi->proto->instance_init(&pi->ph);
716 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
718 if (ret != proto->id)
722 * Warn but ignore events registration errors since we do not want
723 * to skip whole protocols if their notifications are messed up.
725 if (pi->proto->events) {
726 ret = scmi_register_protocol_events(handle, pi->proto->id,
730 dev_warn(handle->dev,
731 "Protocol:%X - Events Registration Failed - err:%d\n",
735 devres_close_group(handle->dev, pi->gid);
736 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
741 /* Take care to put the protocol module's owner before releasing all */
742 scmi_protocol_put(proto->id);
743 devres_release_group(handle->dev, gid);
749 * scmi_get_protocol_instance - Protocol initialization helper.
750 * @handle: A reference to the SCMI platform instance.
751 * @protocol_id: The protocol being requested.
753 * In case the required protocol has never been requested before for this
754 * instance, allocate and initialize all the needed structures while handling
755 * resource allocation with a dedicated per-protocol devres subgroup.
757 * Return: A reference to an initialized protocol instance or error on failure:
758 * in particular returns -EPROBE_DEFER when the desired protocol could
761 static struct scmi_protocol_instance * __must_check
762 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
764 struct scmi_protocol_instance *pi;
765 struct scmi_info *info = handle_to_scmi_info(handle);
767 mutex_lock(&info->protocols_mtx);
768 pi = idr_find(&info->protocols, protocol_id);
771 refcount_inc(&pi->users);
773 const struct scmi_protocol *proto;
775 /* Fails if protocol not registered on bus */
776 proto = scmi_protocol_get(protocol_id);
778 pi = scmi_alloc_init_protocol_instance(info, proto);
780 pi = ERR_PTR(-EPROBE_DEFER);
782 mutex_unlock(&info->protocols_mtx);
788 * scmi_protocol_acquire - Protocol acquire
789 * @handle: A reference to the SCMI platform instance.
790 * @protocol_id: The protocol being requested.
792 * Register a new user for the requested protocol on the specified SCMI
793 * platform instance, possibly triggering its initialization on first user.
795 * Return: 0 if protocol was acquired successfully.
797 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
799 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
803 * scmi_protocol_release - Protocol de-initialization helper.
804 * @handle: A reference to the SCMI platform instance.
805 * @protocol_id: The protocol being requested.
807 * Remove one user for the specified protocol and triggers de-initialization
808 * and resources de-allocation once the last user has gone.
810 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
812 struct scmi_info *info = handle_to_scmi_info(handle);
813 struct scmi_protocol_instance *pi;
815 mutex_lock(&info->protocols_mtx);
816 pi = idr_find(&info->protocols, protocol_id);
820 if (refcount_dec_and_test(&pi->users)) {
823 if (pi->proto->events)
824 scmi_deregister_protocol_events(handle, protocol_id);
826 if (pi->proto->instance_deinit)
827 pi->proto->instance_deinit(&pi->ph);
829 idr_remove(&info->protocols, protocol_id);
831 scmi_protocol_put(protocol_id);
833 devres_release_group(handle->dev, gid);
834 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
839 mutex_unlock(&info->protocols_mtx);
842 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
845 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
846 struct scmi_info *info = handle_to_scmi_info(pi->handle);
848 info->protocols_imp = prot_imp;
852 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
855 struct scmi_info *info = handle_to_scmi_info(handle);
857 if (!info->protocols_imp)
860 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
861 if (info->protocols_imp[i] == prot_id)
866 struct scmi_protocol_devres {
867 const struct scmi_handle *handle;
871 static void scmi_devm_release_protocol(struct device *dev, void *res)
873 struct scmi_protocol_devres *dres = res;
875 scmi_protocol_release(dres->handle, dres->protocol_id);
879 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
880 * @sdev: A reference to an scmi_device whose embedded struct device is to
881 * be used for devres accounting.
882 * @protocol_id: The protocol being requested.
883 * @ph: A pointer reference used to pass back the associated protocol handle.
885 * Get hold of a protocol accounting for its usage, eventually triggering its
886 * initialization, and returning the protocol specific operations and related
887 * protocol handle which will be used as first argument in most of the
888 * protocols operations methods.
889 * Being a devres based managed method, protocol hold will be automatically
890 * released, and possibly de-initialized on last user, once the SCMI driver
891 * owning the scmi_device is unbound from it.
893 * Return: A reference to the requested protocol operations or error.
894 * Must be checked for errors by caller.
896 static const void __must_check *
897 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
898 struct scmi_protocol_handle **ph)
900 struct scmi_protocol_instance *pi;
901 struct scmi_protocol_devres *dres;
902 struct scmi_handle *handle = sdev->handle;
905 return ERR_PTR(-EINVAL);
907 dres = devres_alloc(scmi_devm_release_protocol,
908 sizeof(*dres), GFP_KERNEL);
910 return ERR_PTR(-ENOMEM);
912 pi = scmi_get_protocol_instance(handle, protocol_id);
918 dres->handle = handle;
919 dres->protocol_id = protocol_id;
920 devres_add(&sdev->dev, dres);
924 return pi->proto->ops;
927 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
929 struct scmi_protocol_devres *dres = res;
931 if (WARN_ON(!dres || !data))
934 return dres->protocol_id == *((u8 *)data);
938 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
939 * @sdev: A reference to an scmi_device whose embedded struct device is to
940 * be used for devres accounting.
941 * @protocol_id: The protocol being requested.
943 * Explicitly release a protocol hold previously obtained calling the above
944 * @scmi_devm_protocol_get.
946 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
950 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
951 scmi_devm_protocol_match, &protocol_id);
956 struct scmi_handle *scmi_handle_get_from_info_unlocked(struct scmi_info *info)
959 return &info->handle;
963 * scmi_handle_get() - Get the SCMI handle for a device
965 * @dev: pointer to device for which we want SCMI handle
967 * NOTE: The function does not track individual clients of the framework
968 * and is expected to be maintained by caller of SCMI protocol library.
969 * scmi_handle_put must be balanced with successful scmi_handle_get
971 * Return: pointer to handle if successful, NULL on error
973 struct scmi_handle *scmi_handle_get(struct device *dev)
976 struct scmi_info *info;
977 struct scmi_handle *handle = NULL;
979 mutex_lock(&scmi_list_mutex);
980 list_for_each(p, &scmi_list) {
981 info = list_entry(p, struct scmi_info, node);
982 if (dev->parent == info->dev) {
983 handle = scmi_handle_get_from_info_unlocked(info);
987 mutex_unlock(&scmi_list_mutex);
993 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
995 * @handle: handle acquired by scmi_handle_get
997 * NOTE: The function does not track individual clients of the framework
998 * and is expected to be maintained by caller of SCMI protocol library.
999 * scmi_handle_put must be balanced with successful scmi_handle_get
1001 * Return: 0 is successfully released
1002 * if null was passed, it returns -EINVAL;
1004 int scmi_handle_put(const struct scmi_handle *handle)
1006 struct scmi_info *info;
1011 info = handle_to_scmi_info(handle);
1012 mutex_lock(&scmi_list_mutex);
1013 if (!WARN_ON(!info->users))
1015 mutex_unlock(&scmi_list_mutex);
1020 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
1021 struct scmi_xfers_info *info)
1024 struct scmi_xfer *xfer;
1025 struct device *dev = sinfo->dev;
1026 const struct scmi_desc *desc = sinfo->desc;
1028 /* Pre-allocated messages, no more than what hdr.seq can support */
1029 if (WARN_ON(!desc->max_msg || desc->max_msg > MSG_TOKEN_MAX)) {
1031 "Invalid maximum messages %d, not in range [1 - %lu]\n",
1032 desc->max_msg, MSG_TOKEN_MAX);
1036 info->xfer_block = devm_kcalloc(dev, desc->max_msg,
1037 sizeof(*info->xfer_block), GFP_KERNEL);
1038 if (!info->xfer_block)
1041 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
1042 sizeof(long), GFP_KERNEL);
1043 if (!info->xfer_alloc_table)
1046 /* Pre-initialize the buffer pointer to pre-allocated buffers */
1047 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
1048 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
1053 xfer->tx.buf = xfer->rx.buf;
1054 init_completion(&xfer->done);
1057 spin_lock_init(&info->xfer_lock);
1062 static int scmi_xfer_info_init(struct scmi_info *sinfo)
1064 int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
1066 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1067 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1072 static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1073 int prot_id, bool tx)
1076 struct scmi_chan_info *cinfo;
1079 /* Transmit channel is first entry i.e. index 0 */
1081 idr = tx ? &info->tx_idr : &info->rx_idr;
1083 /* check if already allocated, used for multiple device per protocol */
1084 cinfo = idr_find(idr, prot_id);
1088 if (!info->desc->ops->chan_available(dev, idx)) {
1089 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
1090 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
1095 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
1101 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
1106 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
1107 if (ret != prot_id) {
1108 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
1112 cinfo->handle = &info->handle;
1117 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
1119 int ret = scmi_chan_setup(info, dev, prot_id, true);
1121 if (!ret) /* Rx is optional, hence no error check */
1122 scmi_chan_setup(info, dev, prot_id, false);
1128 * scmi_get_protocol_device - Helper to get/create an SCMI device.
1130 * @np: A device node representing a valid active protocols for the referred
1132 * @info: The referred SCMI instance for which we are getting/creating this
1134 * @prot_id: The protocol ID.
1135 * @name: The device name.
1137 * Referring to the specific SCMI instance identified by @info, this helper
1138 * takes care to return a properly initialized device matching the requested
1139 * @proto_id and @name: if device was still not existent it is created as a
1140 * child of the specified SCMI instance @info and its transport properly
1141 * initialized as usual.
1143 * Return: A properly initialized scmi device, NULL otherwise.
1145 static inline struct scmi_device *
1146 scmi_get_protocol_device(struct device_node *np, struct scmi_info *info,
1147 int prot_id, const char *name)
1149 struct scmi_device *sdev;
1151 /* Already created for this parent SCMI instance ? */
1152 sdev = scmi_child_dev_find(info->dev, prot_id, name);
1156 pr_debug("Creating SCMI device (%s) for protocol %x\n", name, prot_id);
1158 sdev = scmi_device_create(np, info->dev, prot_id, name);
1160 dev_err(info->dev, "failed to create %d protocol device\n",
1165 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
1166 dev_err(&sdev->dev, "failed to setup transport\n");
1167 scmi_device_destroy(sdev);
1175 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
1176 int prot_id, const char *name)
1178 struct scmi_device *sdev;
1180 sdev = scmi_get_protocol_device(np, info, prot_id, name);
1184 /* setup handle now as the transport is ready */
1185 scmi_set_handle(sdev);
1189 * scmi_create_protocol_devices - Create devices for all pending requests for
1190 * this SCMI instance.
1192 * @np: The device node describing the protocol
1193 * @info: The SCMI instance descriptor
1194 * @prot_id: The protocol ID
1196 * All devices previously requested for this instance (if any) are found and
1197 * created by scanning the proper @&scmi_requested_devices entry.
1199 static void scmi_create_protocol_devices(struct device_node *np,
1200 struct scmi_info *info, int prot_id)
1202 struct list_head *phead;
1204 mutex_lock(&scmi_requested_devices_mtx);
1205 phead = idr_find(&scmi_requested_devices, prot_id);
1207 struct scmi_requested_dev *rdev;
1209 list_for_each_entry(rdev, phead, node)
1210 scmi_create_protocol_device(np, info, prot_id,
1211 rdev->id_table->name);
1213 mutex_unlock(&scmi_requested_devices_mtx);
1217 * scmi_protocol_device_request - Helper to request a device
1219 * @id_table: A protocol/name pair descriptor for the device to be created.
1221 * This helper let an SCMI driver request specific devices identified by the
1222 * @id_table to be created for each active SCMI instance.
1224 * The requested device name MUST NOT be already existent for any protocol;
1225 * at first the freshly requested @id_table is annotated in the IDR table
1226 * @scmi_requested_devices, then a matching device is created for each already
1227 * active SCMI instance. (if any)
1229 * This way the requested device is created straight-away for all the already
1230 * initialized(probed) SCMI instances (handles) and it remains also annotated
1231 * as pending creation if the requesting SCMI driver was loaded before some
1232 * SCMI instance and related transports were available: when such late instance
1233 * is probed, its probe will take care to scan the list of pending requested
1234 * devices and create those on its own (see @scmi_create_protocol_devices and
1235 * its enclosing loop)
1237 * Return: 0 on Success
1239 int scmi_protocol_device_request(const struct scmi_device_id *id_table)
1242 unsigned int id = 0;
1243 struct list_head *head, *phead = NULL;
1244 struct scmi_requested_dev *rdev;
1245 struct scmi_info *info;
1247 pr_debug("Requesting SCMI device (%s) for protocol %x\n",
1248 id_table->name, id_table->protocol_id);
1251 * Search for the matching protocol rdev list and then search
1252 * of any existent equally named device...fails if any duplicate found.
1254 mutex_lock(&scmi_requested_devices_mtx);
1255 idr_for_each_entry(&scmi_requested_devices, head, id) {
1257 /* A list found registered in the IDR is never empty */
1258 rdev = list_first_entry(head, struct scmi_requested_dev,
1260 if (rdev->id_table->protocol_id ==
1261 id_table->protocol_id)
1264 list_for_each_entry(rdev, head, node) {
1265 if (!strcmp(rdev->id_table->name, id_table->name)) {
1266 pr_err("Ignoring duplicate request [%d] %s\n",
1267 rdev->id_table->protocol_id,
1268 rdev->id_table->name);
1276 * No duplicate found for requested id_table, so let's create a new
1277 * requested device entry for this new valid request.
1279 rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
1284 rdev->id_table = id_table;
1287 * Append the new requested device table descriptor to the head of the
1288 * related protocol list, eventually creating such head if not already
1292 phead = kzalloc(sizeof(*phead), GFP_KERNEL);
1298 INIT_LIST_HEAD(phead);
1300 ret = idr_alloc(&scmi_requested_devices, (void *)phead,
1301 id_table->protocol_id,
1302 id_table->protocol_id + 1, GFP_KERNEL);
1303 if (ret != id_table->protocol_id) {
1304 pr_err("Failed to save SCMI device - ret:%d\n", ret);
1312 list_add(&rdev->node, phead);
1315 * Now effectively create and initialize the requested device for every
1316 * already initialized SCMI instance which has registered the requested
1317 * protocol as a valid active one: i.e. defined in DT and supported by
1318 * current platform FW.
1320 mutex_lock(&scmi_list_mutex);
1321 list_for_each_entry(info, &scmi_list, node) {
1322 struct device_node *child;
1324 child = idr_find(&info->active_protocols,
1325 id_table->protocol_id);
1327 struct scmi_device *sdev;
1329 sdev = scmi_get_protocol_device(child, info,
1330 id_table->protocol_id,
1332 /* Set handle if not already set: device existed */
1333 if (sdev && !sdev->handle)
1335 scmi_handle_get_from_info_unlocked(info);
1338 "Failed. SCMI protocol %d not active.\n",
1339 id_table->protocol_id);
1342 mutex_unlock(&scmi_list_mutex);
1345 mutex_unlock(&scmi_requested_devices_mtx);
1351 * scmi_protocol_device_unrequest - Helper to unrequest a device
1353 * @id_table: A protocol/name pair descriptor for the device to be unrequested.
1355 * An helper to let an SCMI driver release its request about devices; note that
1356 * devices are created and initialized once the first SCMI driver request them
1357 * but they destroyed only on SCMI core unloading/unbinding.
1359 * The current SCMI transport layer uses such devices as internal references and
1360 * as such they could be shared as same transport between multiple drivers so
1361 * that cannot be safely destroyed till the whole SCMI stack is removed.
1362 * (unless adding further burden of refcounting.)
1364 void scmi_protocol_device_unrequest(const struct scmi_device_id *id_table)
1366 struct list_head *phead;
1368 pr_debug("Unrequesting SCMI device (%s) for protocol %x\n",
1369 id_table->name, id_table->protocol_id);
1371 mutex_lock(&scmi_requested_devices_mtx);
1372 phead = idr_find(&scmi_requested_devices, id_table->protocol_id);
1374 struct scmi_requested_dev *victim, *tmp;
1376 list_for_each_entry_safe(victim, tmp, phead, node) {
1377 if (!strcmp(victim->id_table->name, id_table->name)) {
1378 list_del(&victim->node);
1384 if (list_empty(phead)) {
1385 idr_remove(&scmi_requested_devices,
1386 id_table->protocol_id);
1390 mutex_unlock(&scmi_requested_devices_mtx);
1393 static int scmi_probe(struct platform_device *pdev)
1396 struct scmi_handle *handle;
1397 const struct scmi_desc *desc;
1398 struct scmi_info *info;
1399 struct device *dev = &pdev->dev;
1400 struct device_node *child, *np = dev->of_node;
1402 desc = of_device_get_match_data(dev);
1406 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1412 INIT_LIST_HEAD(&info->node);
1413 idr_init(&info->protocols);
1414 mutex_init(&info->protocols_mtx);
1415 idr_init(&info->active_protocols);
1417 platform_set_drvdata(pdev, info);
1418 idr_init(&info->tx_idr);
1419 idr_init(&info->rx_idr);
1421 handle = &info->handle;
1422 handle->dev = info->dev;
1423 handle->version = &info->version;
1424 handle->devm_protocol_get = scmi_devm_protocol_get;
1425 handle->devm_protocol_put = scmi_devm_protocol_put;
1427 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
1431 ret = scmi_xfer_info_init(info);
1435 if (scmi_notification_init(handle))
1436 dev_err(dev, "SCMI Notifications NOT available.\n");
1439 * Trigger SCMI Base protocol initialization.
1440 * It's mandatory and won't be ever released/deinit until the
1441 * SCMI stack is shutdown/unloaded as a whole.
1443 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
1445 dev_err(dev, "unable to communicate with SCMI\n");
1449 mutex_lock(&scmi_list_mutex);
1450 list_add_tail(&info->node, &scmi_list);
1451 mutex_unlock(&scmi_list_mutex);
1453 for_each_available_child_of_node(np, child) {
1456 if (of_property_read_u32(child, "reg", &prot_id))
1459 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
1460 dev_err(dev, "Out of range protocol %d\n", prot_id);
1462 if (!scmi_is_protocol_implemented(handle, prot_id)) {
1463 dev_err(dev, "SCMI protocol %d not implemented\n",
1469 * Save this valid DT protocol descriptor amongst
1470 * @active_protocols for this SCMI instance/
1472 ret = idr_alloc(&info->active_protocols, child,
1473 prot_id, prot_id + 1, GFP_KERNEL);
1474 if (ret != prot_id) {
1475 dev_err(dev, "SCMI protocol %d already activated. Skip\n",
1481 scmi_create_protocol_devices(child, info, prot_id);
1487 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
1489 idr_remove(idr, id);
1492 static int scmi_remove(struct platform_device *pdev)
1495 struct scmi_info *info = platform_get_drvdata(pdev);
1496 struct idr *idr = &info->tx_idr;
1497 struct device_node *child;
1499 mutex_lock(&scmi_list_mutex);
1503 list_del(&info->node);
1504 mutex_unlock(&scmi_list_mutex);
1509 scmi_notification_exit(&info->handle);
1511 mutex_lock(&info->protocols_mtx);
1512 idr_destroy(&info->protocols);
1513 mutex_unlock(&info->protocols_mtx);
1515 idr_for_each_entry(&info->active_protocols, child, id)
1517 idr_destroy(&info->active_protocols);
1519 /* Safe to free channels since no more users */
1520 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1521 idr_destroy(&info->tx_idr);
1523 idr = &info->rx_idr;
1524 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1525 idr_destroy(&info->rx_idr);
1530 static ssize_t protocol_version_show(struct device *dev,
1531 struct device_attribute *attr, char *buf)
1533 struct scmi_info *info = dev_get_drvdata(dev);
1535 return sprintf(buf, "%u.%u\n", info->version.major_ver,
1536 info->version.minor_ver);
1538 static DEVICE_ATTR_RO(protocol_version);
1540 static ssize_t firmware_version_show(struct device *dev,
1541 struct device_attribute *attr, char *buf)
1543 struct scmi_info *info = dev_get_drvdata(dev);
1545 return sprintf(buf, "0x%x\n", info->version.impl_ver);
1547 static DEVICE_ATTR_RO(firmware_version);
1549 static ssize_t vendor_id_show(struct device *dev,
1550 struct device_attribute *attr, char *buf)
1552 struct scmi_info *info = dev_get_drvdata(dev);
1554 return sprintf(buf, "%s\n", info->version.vendor_id);
1556 static DEVICE_ATTR_RO(vendor_id);
1558 static ssize_t sub_vendor_id_show(struct device *dev,
1559 struct device_attribute *attr, char *buf)
1561 struct scmi_info *info = dev_get_drvdata(dev);
1563 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
1565 static DEVICE_ATTR_RO(sub_vendor_id);
1567 static struct attribute *versions_attrs[] = {
1568 &dev_attr_firmware_version.attr,
1569 &dev_attr_protocol_version.attr,
1570 &dev_attr_vendor_id.attr,
1571 &dev_attr_sub_vendor_id.attr,
1574 ATTRIBUTE_GROUPS(versions);
1576 /* Each compatible listed below must have descriptor associated with it */
1577 static const struct of_device_id scmi_of_match[] = {
1578 #ifdef CONFIG_MAILBOX
1579 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
1581 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
1582 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
1587 MODULE_DEVICE_TABLE(of, scmi_of_match);
1589 static struct platform_driver scmi_driver = {
1592 .of_match_table = scmi_of_match,
1593 .dev_groups = versions_groups,
1595 .probe = scmi_probe,
1596 .remove = scmi_remove,
1599 static int __init scmi_driver_init(void)
1603 scmi_base_register();
1605 scmi_clock_register();
1606 scmi_perf_register();
1607 scmi_power_register();
1608 scmi_reset_register();
1609 scmi_sensors_register();
1610 scmi_voltage_register();
1611 scmi_system_register();
1613 return platform_driver_register(&scmi_driver);
1615 subsys_initcall(scmi_driver_init);
1617 static void __exit scmi_driver_exit(void)
1619 scmi_base_unregister();
1621 scmi_clock_unregister();
1622 scmi_perf_unregister();
1623 scmi_power_unregister();
1624 scmi_reset_unregister();
1625 scmi_sensors_unregister();
1626 scmi_voltage_unregister();
1627 scmi_system_unregister();
1631 platform_driver_unregister(&scmi_driver);
1633 module_exit(scmi_driver_exit);
1635 MODULE_ALIAS("platform: arm-scmi");
1636 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1637 MODULE_DESCRIPTION("ARM SCMI protocol driver");
1638 MODULE_LICENSE("GPL v2");