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/module.h>
25 #include <linux/of_address.h>
26 #include <linux/of_device.h>
27 #include <linux/processor.h>
28 #include <linux/refcount.h>
29 #include <linux/slab.h>
34 #define CREATE_TRACE_POINTS
35 #include <trace/events/scmi.h>
37 enum scmi_error_codes {
38 SCMI_SUCCESS = 0, /* Success */
39 SCMI_ERR_SUPPORT = -1, /* Not supported */
40 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
41 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
42 SCMI_ERR_ENTRY = -4, /* Not found */
43 SCMI_ERR_RANGE = -5, /* Value out of range */
44 SCMI_ERR_BUSY = -6, /* Device busy */
45 SCMI_ERR_COMMS = -7, /* Communication Error */
46 SCMI_ERR_GENERIC = -8, /* Generic Error */
47 SCMI_ERR_HARDWARE = -9, /* Hardware Error */
48 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;
60 * struct scmi_xfers_info - Structure to manage transfer information
62 * @xfer_block: Preallocated Message array
63 * @xfer_alloc_table: Bitmap table for allocated messages.
64 * Index of this bitmap table is also used for message
65 * sequence identifier.
66 * @xfer_lock: Protection for message allocation
68 struct scmi_xfers_info {
69 struct scmi_xfer *xfer_block;
70 unsigned long *xfer_alloc_table;
75 * struct scmi_protocol_instance - Describe an initialized protocol instance.
76 * @handle: Reference to the SCMI handle associated to this protocol instance.
77 * @proto: A reference to the protocol descriptor.
78 * @gid: A reference for per-protocol devres management.
79 * @users: A refcount to track effective users of this protocol.
80 * @priv: Reference for optional protocol private data.
81 * @ph: An embedded protocol handle that will be passed down to protocol
82 * initialization code to identify this instance.
84 * Each protocol is initialized independently once for each SCMI platform in
85 * which is defined by DT and implemented by the SCMI server fw.
87 struct scmi_protocol_instance {
88 const struct scmi_handle *handle;
89 const struct scmi_protocol *proto;
93 struct scmi_protocol_handle ph;
96 #define ph_to_pi(h) container_of(h, struct scmi_protocol_instance, ph)
99 * struct scmi_info - Structure representing a SCMI instance
101 * @dev: Device pointer
102 * @desc: SoC description for this instance
103 * @version: SCMI revision information containing protocol version,
104 * implementation version and (sub-)vendor identification.
105 * @handle: Instance of SCMI handle to send to clients
106 * @tx_minfo: Universal Transmit Message management info
107 * @rx_minfo: Universal Receive Message management info
108 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
109 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
110 * @protocols: IDR for protocols' instance descriptors initialized for
111 * this SCMI instance: populated on protocol's first attempted
113 * @protocols_mtx: A mutex to protect protocols instances initialization.
114 * @protocols_imp: List of protocols implemented, currently maximum of
115 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
117 * @users: Number of users of this instance
121 const struct scmi_desc *desc;
122 struct scmi_revision_info version;
123 struct scmi_handle handle;
124 struct scmi_xfers_info tx_minfo;
125 struct scmi_xfers_info rx_minfo;
128 struct idr protocols;
129 /* Ensure mutual exclusive access to protocols instance array */
130 struct mutex protocols_mtx;
132 struct list_head node;
136 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
138 static const int scmi_linux_errmap[] = {
139 /* better than switch case as long as return value is continuous */
140 0, /* SCMI_SUCCESS */
141 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
142 -EINVAL, /* SCMI_ERR_PARAM */
143 -EACCES, /* SCMI_ERR_ACCESS */
144 -ENOENT, /* SCMI_ERR_ENTRY */
145 -ERANGE, /* SCMI_ERR_RANGE */
146 -EBUSY, /* SCMI_ERR_BUSY */
147 -ECOMM, /* SCMI_ERR_COMMS */
148 -EIO, /* SCMI_ERR_GENERIC */
149 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
150 -EPROTO, /* SCMI_ERR_PROTOCOL */
153 static inline int scmi_to_linux_errno(int errno)
155 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
156 return scmi_linux_errmap[-errno];
161 * scmi_dump_header_dbg() - Helper to dump a message header.
163 * @dev: Device pointer corresponding to the SCMI entity
164 * @hdr: pointer to header.
166 static inline void scmi_dump_header_dbg(struct device *dev,
167 struct scmi_msg_hdr *hdr)
169 dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
170 hdr->id, hdr->seq, hdr->protocol_id);
174 * scmi_xfer_get() - Allocate one message
176 * @handle: Pointer to SCMI entity handle
177 * @minfo: Pointer to Tx/Rx Message management info based on channel type
179 * Helper function which is used by various message functions that are
180 * exposed to clients of this driver for allocating a message traffic event.
182 * This function can sleep depending on pending requests already in the system
183 * for the SCMI entity. Further, this also holds a spinlock to maintain
184 * integrity of internal data structures.
186 * Return: 0 if all went fine, else corresponding error.
188 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
189 struct scmi_xfers_info *minfo)
192 struct scmi_xfer *xfer;
193 unsigned long flags, bit_pos;
194 struct scmi_info *info = handle_to_scmi_info(handle);
196 /* Keep the locked section as small as possible */
197 spin_lock_irqsave(&minfo->xfer_lock, flags);
198 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
199 info->desc->max_msg);
200 if (bit_pos == info->desc->max_msg) {
201 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
202 return ERR_PTR(-ENOMEM);
204 set_bit(bit_pos, minfo->xfer_alloc_table);
205 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
209 xfer = &minfo->xfer_block[xfer_id];
210 xfer->hdr.seq = xfer_id;
211 reinit_completion(&xfer->done);
212 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
218 * __scmi_xfer_put() - Release a message
220 * @minfo: Pointer to Tx/Rx Message management info based on channel type
221 * @xfer: message that was reserved by scmi_xfer_get
223 * This holds a spinlock to maintain integrity of internal data structures.
226 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
231 * Keep the locked section as small as possible
232 * NOTE: we might escape with smp_mb and no lock here..
233 * but just be conservative and symmetric.
235 spin_lock_irqsave(&minfo->xfer_lock, flags);
236 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
237 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
240 static void scmi_handle_notification(struct scmi_chan_info *cinfo, u32 msg_hdr)
242 struct scmi_xfer *xfer;
243 struct device *dev = cinfo->dev;
244 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
245 struct scmi_xfers_info *minfo = &info->rx_minfo;
248 ts = ktime_get_boottime();
249 xfer = scmi_xfer_get(cinfo->handle, minfo);
251 dev_err(dev, "failed to get free message slot (%ld)\n",
253 info->desc->ops->clear_channel(cinfo);
257 unpack_scmi_header(msg_hdr, &xfer->hdr);
258 scmi_dump_header_dbg(dev, &xfer->hdr);
259 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
261 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
262 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
264 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
265 xfer->hdr.protocol_id, xfer->hdr.seq,
266 MSG_TYPE_NOTIFICATION);
268 __scmi_xfer_put(minfo, xfer);
270 info->desc->ops->clear_channel(cinfo);
273 static void scmi_handle_response(struct scmi_chan_info *cinfo,
274 u16 xfer_id, u8 msg_type)
276 struct scmi_xfer *xfer;
277 struct device *dev = cinfo->dev;
278 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
279 struct scmi_xfers_info *minfo = &info->tx_minfo;
281 /* Are we even expecting this? */
282 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
283 dev_err(dev, "message for %d is not expected!\n", xfer_id);
284 info->desc->ops->clear_channel(cinfo);
288 xfer = &minfo->xfer_block[xfer_id];
290 * Even if a response was indeed expected on this slot at this point,
291 * a buggy platform could wrongly reply feeding us an unexpected
292 * delayed response we're not prepared to handle: bail-out safely
295 if (unlikely(msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done)) {
297 "Delayed Response for %d not expected! Buggy F/W ?\n",
299 info->desc->ops->clear_channel(cinfo);
300 /* It was unexpected, so nobody will clear the xfer if not us */
301 __scmi_xfer_put(minfo, xfer);
305 scmi_dump_header_dbg(dev, &xfer->hdr);
307 info->desc->ops->fetch_response(cinfo, xfer);
309 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
310 xfer->hdr.protocol_id, xfer->hdr.seq,
313 if (msg_type == MSG_TYPE_DELAYED_RESP) {
314 info->desc->ops->clear_channel(cinfo);
315 complete(xfer->async_done);
317 complete(&xfer->done);
322 * scmi_rx_callback() - callback for receiving messages
324 * @cinfo: SCMI channel info
325 * @msg_hdr: Message header
327 * Processes one received message to appropriate transfer information and
328 * signals completion of the transfer.
330 * NOTE: This function will be invoked in IRQ context, hence should be
331 * as optimal as possible.
333 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr)
335 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
336 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
339 case MSG_TYPE_NOTIFICATION:
340 scmi_handle_notification(cinfo, msg_hdr);
342 case MSG_TYPE_COMMAND:
343 case MSG_TYPE_DELAYED_RESP:
344 scmi_handle_response(cinfo, xfer_id, msg_type);
347 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
353 * xfer_put() - Release a transmit message
355 * @ph: Pointer to SCMI protocol handle
356 * @xfer: message that was reserved by scmi_xfer_get
358 static void xfer_put(const struct scmi_protocol_handle *ph,
359 struct scmi_xfer *xfer)
361 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
362 struct scmi_info *info = handle_to_scmi_info(pi->handle);
364 __scmi_xfer_put(&info->tx_minfo, xfer);
367 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
369 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
370 struct scmi_xfer *xfer, ktime_t stop)
372 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
374 return info->desc->ops->poll_done(cinfo, xfer) ||
375 ktime_after(ktime_get(), stop);
379 * do_xfer() - Do one transfer
381 * @ph: Pointer to SCMI protocol handle
382 * @xfer: Transfer to initiate and wait for response
384 * Return: -ETIMEDOUT in case of no response, if transmit error,
385 * return corresponding error, else if all goes well,
388 static int do_xfer(const struct scmi_protocol_handle *ph,
389 struct scmi_xfer *xfer)
393 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
394 struct scmi_info *info = handle_to_scmi_info(pi->handle);
395 struct device *dev = info->dev;
396 struct scmi_chan_info *cinfo;
399 * Re-instate protocol id here from protocol handle so that cannot be
400 * overridden by mistake (or malice) by the protocol code mangling with
401 * the scmi_xfer structure.
403 xfer->hdr.protocol_id = pi->proto->id;
405 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
406 if (unlikely(!cinfo))
409 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
410 xfer->hdr.protocol_id, xfer->hdr.seq,
411 xfer->hdr.poll_completion);
413 ret = info->desc->ops->send_message(cinfo, xfer);
415 dev_dbg(dev, "Failed to send message %d\n", ret);
419 if (xfer->hdr.poll_completion) {
420 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
422 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
424 if (ktime_before(ktime_get(), stop))
425 info->desc->ops->fetch_response(cinfo, xfer);
429 /* And we wait for the response. */
430 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
431 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
432 dev_err(dev, "timed out in resp(caller: %pS)\n",
438 if (!ret && xfer->hdr.status)
439 ret = scmi_to_linux_errno(xfer->hdr.status);
441 if (info->desc->ops->mark_txdone)
442 info->desc->ops->mark_txdone(cinfo, ret);
444 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
445 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
450 static void reset_rx_to_maxsz(const struct scmi_protocol_handle *ph,
451 struct scmi_xfer *xfer)
453 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
454 struct scmi_info *info = handle_to_scmi_info(pi->handle);
456 xfer->rx.len = info->desc->max_msg_size;
459 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
462 * do_xfer_with_response() - Do one transfer and wait until the delayed
463 * response is received
465 * @ph: Pointer to SCMI protocol handle
466 * @xfer: Transfer to initiate and wait for response
468 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
469 * return corresponding error, else if all goes well, return 0.
471 static int do_xfer_with_response(const struct scmi_protocol_handle *ph,
472 struct scmi_xfer *xfer)
474 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
475 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
476 DECLARE_COMPLETION_ONSTACK(async_response);
478 xfer->hdr.protocol_id = pi->proto->id;
480 xfer->async_done = &async_response;
482 ret = do_xfer(ph, xfer);
483 if (!ret && !wait_for_completion_timeout(xfer->async_done, timeout))
486 xfer->async_done = NULL;
491 * xfer_get_init() - Allocate and initialise one message for transmit
493 * @ph: Pointer to SCMI protocol handle
494 * @msg_id: Message identifier
495 * @tx_size: transmit message size
496 * @rx_size: receive message size
497 * @p: pointer to the allocated and initialised message
499 * This function allocates the message using @scmi_xfer_get and
500 * initialise the header.
502 * Return: 0 if all went fine with @p pointing to message, else
503 * corresponding error.
505 static int xfer_get_init(const struct scmi_protocol_handle *ph,
506 u8 msg_id, size_t tx_size, size_t rx_size,
507 struct scmi_xfer **p)
510 struct scmi_xfer *xfer;
511 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
512 struct scmi_info *info = handle_to_scmi_info(pi->handle);
513 struct scmi_xfers_info *minfo = &info->tx_minfo;
514 struct device *dev = info->dev;
516 /* Ensure we have sane transfer sizes */
517 if (rx_size > info->desc->max_msg_size ||
518 tx_size > info->desc->max_msg_size)
521 xfer = scmi_xfer_get(pi->handle, minfo);
524 dev_err(dev, "failed to get free message slot(%d)\n", ret);
528 xfer->tx.len = tx_size;
529 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
530 xfer->hdr.id = msg_id;
531 xfer->hdr.protocol_id = pi->proto->id;
532 xfer->hdr.poll_completion = false;
540 * version_get() - command to get the revision of the SCMI entity
542 * @ph: Pointer to SCMI protocol handle
543 * @version: Holds returned version of protocol.
545 * Updates the SCMI information in the internal data structure.
547 * Return: 0 if all went fine, else return appropriate error.
549 static int version_get(const struct scmi_protocol_handle *ph, u32 *version)
555 ret = xfer_get_init(ph, PROTOCOL_VERSION, 0, sizeof(*version), &t);
559 ret = do_xfer(ph, t);
561 rev_info = t->rx.buf;
562 *version = le32_to_cpu(*rev_info);
570 * scmi_set_protocol_priv - Set protocol specific data at init time
572 * @ph: A reference to the protocol handle.
573 * @priv: The private data to set.
575 * Return: 0 on Success
577 static int scmi_set_protocol_priv(const struct scmi_protocol_handle *ph,
580 struct scmi_protocol_instance *pi = ph_to_pi(ph);
588 * scmi_get_protocol_priv - Set protocol specific data at init time
590 * @ph: A reference to the protocol handle.
592 * Return: Protocol private data if any was set.
594 static void *scmi_get_protocol_priv(const struct scmi_protocol_handle *ph)
596 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
601 static const struct scmi_xfer_ops xfer_ops = {
602 .version_get = version_get,
603 .xfer_get_init = xfer_get_init,
604 .reset_rx_to_maxsz = reset_rx_to_maxsz,
606 .do_xfer_with_response = do_xfer_with_response,
607 .xfer_put = xfer_put,
611 * scmi_revision_area_get - Retrieve version memory area.
613 * @ph: A reference to the protocol handle.
615 * A helper to grab the version memory area reference during SCMI Base protocol
618 * Return: A reference to the version memory area associated to the SCMI
619 * instance underlying this protocol handle.
621 struct scmi_revision_info *
622 scmi_revision_area_get(const struct scmi_protocol_handle *ph)
624 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
626 return pi->handle->version;
630 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
631 * instance descriptor.
632 * @info: The reference to the related SCMI instance.
633 * @proto: The protocol descriptor.
635 * Allocate a new protocol instance descriptor, using the provided @proto
636 * description, against the specified SCMI instance @info, and initialize it;
637 * all resources management is handled via a dedicated per-protocol devres
640 * Context: Assumes to be called with @protocols_mtx already acquired.
641 * Return: A reference to a freshly allocated and initialized protocol instance
642 * or ERR_PTR on failure.
644 static struct scmi_protocol_instance *
645 scmi_alloc_init_protocol_instance(struct scmi_info *info,
646 const struct scmi_protocol *proto)
650 struct scmi_protocol_instance *pi;
651 const struct scmi_handle *handle = &info->handle;
653 /* Protocol specific devres group */
654 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL);
658 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL);
665 pi->ph.dev = handle->dev;
666 pi->ph.xops = &xfer_ops;
667 pi->ph.set_priv = scmi_set_protocol_priv;
668 pi->ph.get_priv = scmi_get_protocol_priv;
669 refcount_set(&pi->users, 1);
670 /* proto->init is assured NON NULL by scmi_protocol_register */
671 ret = pi->proto->instance_init(&pi->ph);
675 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1,
677 if (ret != proto->id)
681 * Warn but ignore events registration errors since we do not want
682 * to skip whole protocols if their notifications are messed up.
684 if (pi->proto->events) {
685 ret = scmi_register_protocol_events(handle, pi->proto->id,
689 dev_warn(handle->dev,
690 "Protocol:%X - Events Registration Failed - err:%d\n",
694 devres_close_group(handle->dev, pi->gid);
695 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id);
700 devres_release_group(handle->dev, gid);
706 * scmi_get_protocol_instance - Protocol initialization helper.
707 * @handle: A reference to the SCMI platform instance.
708 * @protocol_id: The protocol being requested.
710 * In case the required protocol has never been requested before for this
711 * instance, allocate and initialize all the needed structures while handling
712 * resource allocation with a dedicated per-protocol devres subgroup.
714 * Return: A reference to an initialized protocol instance or error on failure.
716 static struct scmi_protocol_instance * __must_check
717 scmi_get_protocol_instance(const struct scmi_handle *handle, u8 protocol_id)
719 struct scmi_protocol_instance *pi;
720 struct scmi_info *info = handle_to_scmi_info(handle);
722 mutex_lock(&info->protocols_mtx);
723 pi = idr_find(&info->protocols, protocol_id);
726 refcount_inc(&pi->users);
728 const struct scmi_protocol *proto;
730 /* Fails if protocol not registered on bus */
731 proto = scmi_protocol_get(protocol_id);
733 pi = scmi_alloc_init_protocol_instance(info, proto);
735 pi = ERR_PTR(-ENODEV);
737 mutex_unlock(&info->protocols_mtx);
743 * scmi_protocol_acquire - Protocol acquire
744 * @handle: A reference to the SCMI platform instance.
745 * @protocol_id: The protocol being requested.
747 * Register a new user for the requested protocol on the specified SCMI
748 * platform instance, possibly triggering its initialization on first user.
750 * Return: 0 if protocol was acquired successfully.
752 int scmi_protocol_acquire(const struct scmi_handle *handle, u8 protocol_id)
754 return PTR_ERR_OR_ZERO(scmi_get_protocol_instance(handle, protocol_id));
758 * scmi_protocol_release - Protocol de-initialization helper.
759 * @handle: A reference to the SCMI platform instance.
760 * @protocol_id: The protocol being requested.
762 * Remove one user for the specified protocol and triggers de-initialization
763 * and resources de-allocation once the last user has gone.
765 void scmi_protocol_release(const struct scmi_handle *handle, u8 protocol_id)
767 struct scmi_info *info = handle_to_scmi_info(handle);
768 struct scmi_protocol_instance *pi;
770 mutex_lock(&info->protocols_mtx);
771 pi = idr_find(&info->protocols, protocol_id);
775 if (refcount_dec_and_test(&pi->users)) {
778 if (pi->proto->events)
779 scmi_deregister_protocol_events(handle, protocol_id);
781 if (pi->proto->instance_deinit)
782 pi->proto->instance_deinit(&pi->ph);
784 idr_remove(&info->protocols, protocol_id);
786 devres_release_group(handle->dev, gid);
787 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n",
792 mutex_unlock(&info->protocols_mtx);
795 void scmi_setup_protocol_implemented(const struct scmi_protocol_handle *ph,
798 const struct scmi_protocol_instance *pi = ph_to_pi(ph);
799 struct scmi_info *info = handle_to_scmi_info(pi->handle);
801 info->protocols_imp = prot_imp;
805 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
808 struct scmi_info *info = handle_to_scmi_info(handle);
810 if (!info->protocols_imp)
813 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
814 if (info->protocols_imp[i] == prot_id)
819 struct scmi_protocol_devres {
820 const struct scmi_handle *handle;
824 static void scmi_devm_release_protocol(struct device *dev, void *res)
826 struct scmi_protocol_devres *dres = res;
828 scmi_protocol_release(dres->handle, dres->protocol_id);
832 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
833 * @sdev: A reference to an scmi_device whose embedded struct device is to
834 * be used for devres accounting.
835 * @protocol_id: The protocol being requested.
836 * @ph: A pointer reference used to pass back the associated protocol handle.
838 * Get hold of a protocol accounting for its usage, eventually triggering its
839 * initialization, and returning the protocol specific operations and related
840 * protocol handle which will be used as first argument in most of the
841 * protocols operations methods.
842 * Being a devres based managed method, protocol hold will be automatically
843 * released, and possibly de-initialized on last user, once the SCMI driver
844 * owning the scmi_device is unbound from it.
846 * Return: A reference to the requested protocol operations or error.
847 * Must be checked for errors by caller.
849 static const void __must_check *
850 scmi_devm_protocol_get(struct scmi_device *sdev, u8 protocol_id,
851 struct scmi_protocol_handle **ph)
853 struct scmi_protocol_instance *pi;
854 struct scmi_protocol_devres *dres;
855 struct scmi_handle *handle = sdev->handle;
858 return ERR_PTR(-EINVAL);
860 dres = devres_alloc(scmi_devm_release_protocol,
861 sizeof(*dres), GFP_KERNEL);
863 return ERR_PTR(-ENOMEM);
865 pi = scmi_get_protocol_instance(handle, protocol_id);
871 dres->handle = handle;
872 dres->protocol_id = protocol_id;
873 devres_add(&sdev->dev, dres);
877 return pi->proto->ops;
880 static int scmi_devm_protocol_match(struct device *dev, void *res, void *data)
882 struct scmi_protocol_devres *dres = res;
884 if (WARN_ON(!dres || !data))
887 return dres->protocol_id == *((u8 *)data);
891 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
892 * @sdev: A reference to an scmi_device whose embedded struct device is to
893 * be used for devres accounting.
894 * @protocol_id: The protocol being requested.
896 * Explicitly release a protocol hold previously obtained calling the above
897 * @scmi_devm_protocol_get.
899 static void scmi_devm_protocol_put(struct scmi_device *sdev, u8 protocol_id)
903 ret = devres_release(&sdev->dev, scmi_devm_release_protocol,
904 scmi_devm_protocol_match, &protocol_id);
909 * scmi_handle_get() - Get the SCMI handle for a device
911 * @dev: pointer to device for which we want SCMI handle
913 * NOTE: The function does not track individual clients of the framework
914 * and is expected to be maintained by caller of SCMI protocol library.
915 * scmi_handle_put must be balanced with successful scmi_handle_get
917 * Return: pointer to handle if successful, NULL on error
919 struct scmi_handle *scmi_handle_get(struct device *dev)
922 struct scmi_info *info;
923 struct scmi_handle *handle = NULL;
925 mutex_lock(&scmi_list_mutex);
926 list_for_each(p, &scmi_list) {
927 info = list_entry(p, struct scmi_info, node);
928 if (dev->parent == info->dev) {
929 handle = &info->handle;
934 mutex_unlock(&scmi_list_mutex);
940 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
942 * @handle: handle acquired by scmi_handle_get
944 * NOTE: The function does not track individual clients of the framework
945 * and is expected to be maintained by caller of SCMI protocol library.
946 * scmi_handle_put must be balanced with successful scmi_handle_get
948 * Return: 0 is successfully released
949 * if null was passed, it returns -EINVAL;
951 int scmi_handle_put(const struct scmi_handle *handle)
953 struct scmi_info *info;
958 info = handle_to_scmi_info(handle);
959 mutex_lock(&scmi_list_mutex);
960 if (!WARN_ON(!info->users))
962 mutex_unlock(&scmi_list_mutex);
967 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
968 struct scmi_xfers_info *info)
971 struct scmi_xfer *xfer;
972 struct device *dev = sinfo->dev;
973 const struct scmi_desc *desc = sinfo->desc;
975 /* Pre-allocated messages, no more than what hdr.seq can support */
976 if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
977 dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
978 desc->max_msg, MSG_TOKEN_MAX);
982 info->xfer_block = devm_kcalloc(dev, desc->max_msg,
983 sizeof(*info->xfer_block), GFP_KERNEL);
984 if (!info->xfer_block)
987 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
988 sizeof(long), GFP_KERNEL);
989 if (!info->xfer_alloc_table)
992 /* Pre-initialize the buffer pointer to pre-allocated buffers */
993 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
994 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
999 xfer->tx.buf = xfer->rx.buf;
1000 init_completion(&xfer->done);
1003 spin_lock_init(&info->xfer_lock);
1008 static int scmi_xfer_info_init(struct scmi_info *sinfo)
1010 int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
1012 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
1013 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
1018 static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
1019 int prot_id, bool tx)
1022 struct scmi_chan_info *cinfo;
1025 /* Transmit channel is first entry i.e. index 0 */
1027 idr = tx ? &info->tx_idr : &info->rx_idr;
1029 /* check if already allocated, used for multiple device per protocol */
1030 cinfo = idr_find(idr, prot_id);
1034 if (!info->desc->ops->chan_available(dev, idx)) {
1035 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
1036 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
1041 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
1047 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
1052 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
1053 if (ret != prot_id) {
1054 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
1058 cinfo->handle = &info->handle;
1063 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
1065 int ret = scmi_chan_setup(info, dev, prot_id, true);
1067 if (!ret) /* Rx is optional, hence no error check */
1068 scmi_chan_setup(info, dev, prot_id, false);
1074 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
1075 int prot_id, const char *name)
1077 struct scmi_device *sdev;
1079 sdev = scmi_device_create(np, info->dev, prot_id, name);
1081 dev_err(info->dev, "failed to create %d protocol device\n",
1086 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
1087 dev_err(&sdev->dev, "failed to setup transport\n");
1088 scmi_device_destroy(sdev);
1092 /* setup handle now as the transport is ready */
1093 scmi_set_handle(sdev);
1096 #define MAX_SCMI_DEV_PER_PROTOCOL 2
1097 struct scmi_prot_devnames {
1099 char *names[MAX_SCMI_DEV_PER_PROTOCOL];
1102 static struct scmi_prot_devnames devnames[] = {
1103 { SCMI_PROTOCOL_POWER, { "genpd" },},
1104 { SCMI_PROTOCOL_SYSTEM, { "syspower" },},
1105 { SCMI_PROTOCOL_PERF, { "cpufreq" },},
1106 { SCMI_PROTOCOL_CLOCK, { "clocks" },},
1107 { SCMI_PROTOCOL_SENSOR, { "hwmon", "iiodev" },},
1108 { SCMI_PROTOCOL_RESET, { "reset" },},
1109 { SCMI_PROTOCOL_VOLTAGE, { "regulator" },},
1113 scmi_create_protocol_devices(struct device_node *np, struct scmi_info *info,
1118 for (loop = 0; loop < ARRAY_SIZE(devnames); loop++) {
1119 if (devnames[loop].protocol_id != prot_id)
1122 for (cnt = 0; cnt < ARRAY_SIZE(devnames[loop].names); cnt++) {
1123 const char *name = devnames[loop].names[cnt];
1126 scmi_create_protocol_device(np, info, prot_id,
1132 static int scmi_probe(struct platform_device *pdev)
1135 struct scmi_handle *handle;
1136 const struct scmi_desc *desc;
1137 struct scmi_info *info;
1138 struct device *dev = &pdev->dev;
1139 struct device_node *child, *np = dev->of_node;
1141 desc = of_device_get_match_data(dev);
1145 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1151 INIT_LIST_HEAD(&info->node);
1152 idr_init(&info->protocols);
1153 mutex_init(&info->protocols_mtx);
1155 platform_set_drvdata(pdev, info);
1156 idr_init(&info->tx_idr);
1157 idr_init(&info->rx_idr);
1159 handle = &info->handle;
1160 handle->dev = info->dev;
1161 handle->version = &info->version;
1162 handle->devm_protocol_get = scmi_devm_protocol_get;
1163 handle->devm_protocol_put = scmi_devm_protocol_put;
1165 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
1169 ret = scmi_xfer_info_init(info);
1173 if (scmi_notification_init(handle))
1174 dev_err(dev, "SCMI Notifications NOT available.\n");
1177 * Trigger SCMI Base protocol initialization.
1178 * It's mandatory and won't be ever released/deinit until the
1179 * SCMI stack is shutdown/unloaded as a whole.
1181 ret = scmi_protocol_acquire(handle, SCMI_PROTOCOL_BASE);
1183 dev_err(dev, "unable to communicate with SCMI\n");
1187 mutex_lock(&scmi_list_mutex);
1188 list_add_tail(&info->node, &scmi_list);
1189 mutex_unlock(&scmi_list_mutex);
1191 for_each_available_child_of_node(np, child) {
1194 if (of_property_read_u32(child, "reg", &prot_id))
1197 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
1198 dev_err(dev, "Out of range protocol %d\n", prot_id);
1200 if (!scmi_is_protocol_implemented(handle, prot_id)) {
1201 dev_err(dev, "SCMI protocol %d not implemented\n",
1206 scmi_create_protocol_devices(child, info, prot_id);
1212 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
1214 idr_remove(idr, id);
1217 static int scmi_remove(struct platform_device *pdev)
1220 struct scmi_info *info = platform_get_drvdata(pdev);
1221 struct idr *idr = &info->tx_idr;
1223 mutex_lock(&scmi_list_mutex);
1227 list_del(&info->node);
1228 mutex_unlock(&scmi_list_mutex);
1233 scmi_notification_exit(&info->handle);
1235 mutex_lock(&info->protocols_mtx);
1236 idr_destroy(&info->protocols);
1237 mutex_unlock(&info->protocols_mtx);
1239 /* Safe to free channels since no more users */
1240 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1241 idr_destroy(&info->tx_idr);
1243 idr = &info->rx_idr;
1244 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
1245 idr_destroy(&info->rx_idr);
1250 static ssize_t protocol_version_show(struct device *dev,
1251 struct device_attribute *attr, char *buf)
1253 struct scmi_info *info = dev_get_drvdata(dev);
1255 return sprintf(buf, "%u.%u\n", info->version.major_ver,
1256 info->version.minor_ver);
1258 static DEVICE_ATTR_RO(protocol_version);
1260 static ssize_t firmware_version_show(struct device *dev,
1261 struct device_attribute *attr, char *buf)
1263 struct scmi_info *info = dev_get_drvdata(dev);
1265 return sprintf(buf, "0x%x\n", info->version.impl_ver);
1267 static DEVICE_ATTR_RO(firmware_version);
1269 static ssize_t vendor_id_show(struct device *dev,
1270 struct device_attribute *attr, char *buf)
1272 struct scmi_info *info = dev_get_drvdata(dev);
1274 return sprintf(buf, "%s\n", info->version.vendor_id);
1276 static DEVICE_ATTR_RO(vendor_id);
1278 static ssize_t sub_vendor_id_show(struct device *dev,
1279 struct device_attribute *attr, char *buf)
1281 struct scmi_info *info = dev_get_drvdata(dev);
1283 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
1285 static DEVICE_ATTR_RO(sub_vendor_id);
1287 static struct attribute *versions_attrs[] = {
1288 &dev_attr_firmware_version.attr,
1289 &dev_attr_protocol_version.attr,
1290 &dev_attr_vendor_id.attr,
1291 &dev_attr_sub_vendor_id.attr,
1294 ATTRIBUTE_GROUPS(versions);
1296 /* Each compatible listed below must have descriptor associated with it */
1297 static const struct of_device_id scmi_of_match[] = {
1298 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
1299 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
1300 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
1305 MODULE_DEVICE_TABLE(of, scmi_of_match);
1307 static struct platform_driver scmi_driver = {
1310 .of_match_table = scmi_of_match,
1311 .dev_groups = versions_groups,
1313 .probe = scmi_probe,
1314 .remove = scmi_remove,
1317 static int __init scmi_driver_init(void)
1321 scmi_base_register();
1323 scmi_clock_register();
1324 scmi_perf_register();
1325 scmi_power_register();
1326 scmi_reset_register();
1327 scmi_sensors_register();
1328 scmi_voltage_register();
1329 scmi_system_register();
1331 return platform_driver_register(&scmi_driver);
1333 subsys_initcall(scmi_driver_init);
1335 static void __exit scmi_driver_exit(void)
1337 scmi_base_unregister();
1339 scmi_clock_unregister();
1340 scmi_perf_unregister();
1341 scmi_power_unregister();
1342 scmi_reset_unregister();
1343 scmi_sensors_unregister();
1344 scmi_voltage_unregister();
1345 scmi_system_unregister();
1349 platform_driver_unregister(&scmi_driver);
1351 module_exit(scmi_driver_exit);
1353 MODULE_ALIAS("platform: arm-scmi");
1354 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1355 MODULE_DESCRIPTION("ARM SCMI protocol driver");
1356 MODULE_LICENSE("GPL v2");