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 ARM Ltd.
17 #include <linux/bitmap.h>
18 #include <linux/export.h>
20 #include <linux/kernel.h>
21 #include <linux/ktime.h>
22 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_device.h>
25 #include <linux/processor.h>
26 #include <linux/slab.h>
31 #define CREATE_TRACE_POINTS
32 #include <trace/events/scmi.h>
34 enum scmi_error_codes {
35 SCMI_SUCCESS = 0, /* Success */
36 SCMI_ERR_SUPPORT = -1, /* Not supported */
37 SCMI_ERR_PARAMS = -2, /* Invalid Parameters */
38 SCMI_ERR_ACCESS = -3, /* Invalid access/permission denied */
39 SCMI_ERR_ENTRY = -4, /* Not found */
40 SCMI_ERR_RANGE = -5, /* Value out of range */
41 SCMI_ERR_BUSY = -6, /* Device busy */
42 SCMI_ERR_COMMS = -7, /* Communication Error */
43 SCMI_ERR_GENERIC = -8, /* Generic Error */
44 SCMI_ERR_HARDWARE = -9, /* Hardware Error */
45 SCMI_ERR_PROTOCOL = -10,/* Protocol Error */
49 /* List of all SCMI devices active in system */
50 static LIST_HEAD(scmi_list);
51 /* Protection for the entire list */
52 static DEFINE_MUTEX(scmi_list_mutex);
53 /* Track the unique id for the transfers for debug & profiling purpose */
54 static atomic_t transfer_last_id;
57 * struct scmi_xfers_info - Structure to manage transfer information
59 * @xfer_block: Preallocated Message array
60 * @xfer_alloc_table: Bitmap table for allocated messages.
61 * Index of this bitmap table is also used for message
62 * sequence identifier.
63 * @xfer_lock: Protection for message allocation
65 struct scmi_xfers_info {
66 struct scmi_xfer *xfer_block;
67 unsigned long *xfer_alloc_table;
72 * struct scmi_info - Structure representing a SCMI instance
74 * @dev: Device pointer
75 * @desc: SoC description for this instance
76 * @version: SCMI revision information containing protocol version,
77 * implementation version and (sub-)vendor identification.
78 * @handle: Instance of SCMI handle to send to clients
79 * @tx_minfo: Universal Transmit Message management info
80 * @rx_minfo: Universal Receive Message management info
81 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
82 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
83 * @protocols_imp: List of protocols implemented, currently maximum of
84 * MAX_PROTOCOLS_IMP elements allocated by the base protocol
86 * @users: Number of users of this instance
90 const struct scmi_desc *desc;
91 struct scmi_revision_info version;
92 struct scmi_handle handle;
93 struct scmi_xfers_info tx_minfo;
94 struct scmi_xfers_info rx_minfo;
98 struct list_head node;
102 #define handle_to_scmi_info(h) container_of(h, struct scmi_info, handle)
104 static const int scmi_linux_errmap[] = {
105 /* better than switch case as long as return value is continuous */
106 0, /* SCMI_SUCCESS */
107 -EOPNOTSUPP, /* SCMI_ERR_SUPPORT */
108 -EINVAL, /* SCMI_ERR_PARAM */
109 -EACCES, /* SCMI_ERR_ACCESS */
110 -ENOENT, /* SCMI_ERR_ENTRY */
111 -ERANGE, /* SCMI_ERR_RANGE */
112 -EBUSY, /* SCMI_ERR_BUSY */
113 -ECOMM, /* SCMI_ERR_COMMS */
114 -EIO, /* SCMI_ERR_GENERIC */
115 -EREMOTEIO, /* SCMI_ERR_HARDWARE */
116 -EPROTO, /* SCMI_ERR_PROTOCOL */
119 static inline int scmi_to_linux_errno(int errno)
121 if (errno < SCMI_SUCCESS && errno > SCMI_ERR_MAX)
122 return scmi_linux_errmap[-errno];
127 * scmi_dump_header_dbg() - Helper to dump a message header.
129 * @dev: Device pointer corresponding to the SCMI entity
130 * @hdr: pointer to header.
132 static inline void scmi_dump_header_dbg(struct device *dev,
133 struct scmi_msg_hdr *hdr)
135 dev_dbg(dev, "Message ID: %x Sequence ID: %x Protocol: %x\n",
136 hdr->id, hdr->seq, hdr->protocol_id);
140 * scmi_xfer_get() - Allocate one message
142 * @handle: Pointer to SCMI entity handle
143 * @minfo: Pointer to Tx/Rx Message management info based on channel type
145 * Helper function which is used by various message functions that are
146 * exposed to clients of this driver for allocating a message traffic event.
148 * This function can sleep depending on pending requests already in the system
149 * for the SCMI entity. Further, this also holds a spinlock to maintain
150 * integrity of internal data structures.
152 * Return: 0 if all went fine, else corresponding error.
154 static struct scmi_xfer *scmi_xfer_get(const struct scmi_handle *handle,
155 struct scmi_xfers_info *minfo)
158 struct scmi_xfer *xfer;
159 unsigned long flags, bit_pos;
160 struct scmi_info *info = handle_to_scmi_info(handle);
162 /* Keep the locked section as small as possible */
163 spin_lock_irqsave(&minfo->xfer_lock, flags);
164 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
165 info->desc->max_msg);
166 if (bit_pos == info->desc->max_msg) {
167 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
168 return ERR_PTR(-ENOMEM);
170 set_bit(bit_pos, minfo->xfer_alloc_table);
171 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
175 xfer = &minfo->xfer_block[xfer_id];
176 xfer->hdr.seq = xfer_id;
177 reinit_completion(&xfer->done);
178 xfer->transfer_id = atomic_inc_return(&transfer_last_id);
184 * __scmi_xfer_put() - Release a message
186 * @minfo: Pointer to Tx/Rx Message management info based on channel type
187 * @xfer: message that was reserved by scmi_xfer_get
189 * This holds a spinlock to maintain integrity of internal data structures.
192 __scmi_xfer_put(struct scmi_xfers_info *minfo, struct scmi_xfer *xfer)
197 * Keep the locked section as small as possible
198 * NOTE: we might escape with smp_mb and no lock here..
199 * but just be conservative and symmetric.
201 spin_lock_irqsave(&minfo->xfer_lock, flags);
202 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table);
203 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
206 static void scmi_handle_notification(struct scmi_chan_info *cinfo, u32 msg_hdr)
208 struct scmi_xfer *xfer;
209 struct device *dev = cinfo->dev;
210 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
211 struct scmi_xfers_info *minfo = &info->rx_minfo;
214 ts = ktime_get_boottime();
215 xfer = scmi_xfer_get(cinfo->handle, minfo);
217 dev_err(dev, "failed to get free message slot (%ld)\n",
219 info->desc->ops->clear_channel(cinfo);
223 unpack_scmi_header(msg_hdr, &xfer->hdr);
224 scmi_dump_header_dbg(dev, &xfer->hdr);
225 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size,
227 scmi_notify(cinfo->handle, xfer->hdr.protocol_id,
228 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts);
230 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
231 xfer->hdr.protocol_id, xfer->hdr.seq,
232 MSG_TYPE_NOTIFICATION);
234 __scmi_xfer_put(minfo, xfer);
236 info->desc->ops->clear_channel(cinfo);
239 static void scmi_handle_response(struct scmi_chan_info *cinfo,
240 u16 xfer_id, u8 msg_type)
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->tx_minfo;
247 /* Are we even expecting this? */
248 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
249 dev_err(dev, "message for %d is not expected!\n", xfer_id);
250 info->desc->ops->clear_channel(cinfo);
254 xfer = &minfo->xfer_block[xfer_id];
256 * Even if a response was indeed expected on this slot at this point,
257 * a buggy platform could wrongly reply feeding us an unexpected
258 * delayed response we're not prepared to handle: bail-out safely
261 if (unlikely(msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done)) {
263 "Delayed Response for %d not expected! Buggy F/W ?\n",
265 info->desc->ops->clear_channel(cinfo);
266 /* It was unexpected, so nobody will clear the xfer if not us */
267 __scmi_xfer_put(minfo, xfer);
271 scmi_dump_header_dbg(dev, &xfer->hdr);
273 info->desc->ops->fetch_response(cinfo, xfer);
275 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id,
276 xfer->hdr.protocol_id, xfer->hdr.seq,
279 if (msg_type == MSG_TYPE_DELAYED_RESP) {
280 info->desc->ops->clear_channel(cinfo);
281 complete(xfer->async_done);
283 complete(&xfer->done);
288 * scmi_rx_callback() - callback for receiving messages
290 * @cinfo: SCMI channel info
291 * @msg_hdr: Message header
293 * Processes one received message to appropriate transfer information and
294 * signals completion of the transfer.
296 * NOTE: This function will be invoked in IRQ context, hence should be
297 * as optimal as possible.
299 void scmi_rx_callback(struct scmi_chan_info *cinfo, u32 msg_hdr)
301 u16 xfer_id = MSG_XTRACT_TOKEN(msg_hdr);
302 u8 msg_type = MSG_XTRACT_TYPE(msg_hdr);
305 case MSG_TYPE_NOTIFICATION:
306 scmi_handle_notification(cinfo, msg_hdr);
308 case MSG_TYPE_COMMAND:
309 case MSG_TYPE_DELAYED_RESP:
310 scmi_handle_response(cinfo, xfer_id, msg_type);
313 WARN_ONCE(1, "received unknown msg_type:%d\n", msg_type);
319 * scmi_xfer_put() - Release a transmit message
321 * @handle: Pointer to SCMI entity handle
322 * @xfer: message that was reserved by scmi_xfer_get
324 void scmi_xfer_put(const struct scmi_handle *handle, struct scmi_xfer *xfer)
326 struct scmi_info *info = handle_to_scmi_info(handle);
328 __scmi_xfer_put(&info->tx_minfo, xfer);
331 #define SCMI_MAX_POLL_TO_NS (100 * NSEC_PER_USEC)
333 static bool scmi_xfer_done_no_timeout(struct scmi_chan_info *cinfo,
334 struct scmi_xfer *xfer, ktime_t stop)
336 struct scmi_info *info = handle_to_scmi_info(cinfo->handle);
338 return info->desc->ops->poll_done(cinfo, xfer) ||
339 ktime_after(ktime_get(), stop);
343 * scmi_do_xfer() - Do one transfer
345 * @handle: Pointer to SCMI entity handle
346 * @xfer: Transfer to initiate and wait for response
348 * Return: -ETIMEDOUT in case of no response, if transmit error,
349 * return corresponding error, else if all goes well,
352 int scmi_do_xfer(const struct scmi_handle *handle, struct scmi_xfer *xfer)
356 struct scmi_info *info = handle_to_scmi_info(handle);
357 struct device *dev = info->dev;
358 struct scmi_chan_info *cinfo;
360 cinfo = idr_find(&info->tx_idr, xfer->hdr.protocol_id);
361 if (unlikely(!cinfo))
364 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id,
365 xfer->hdr.protocol_id, xfer->hdr.seq,
366 xfer->hdr.poll_completion);
368 ret = info->desc->ops->send_message(cinfo, xfer);
370 dev_dbg(dev, "Failed to send message %d\n", ret);
374 if (xfer->hdr.poll_completion) {
375 ktime_t stop = ktime_add_ns(ktime_get(), SCMI_MAX_POLL_TO_NS);
377 spin_until_cond(scmi_xfer_done_no_timeout(cinfo, xfer, stop));
379 if (ktime_before(ktime_get(), stop))
380 info->desc->ops->fetch_response(cinfo, xfer);
384 /* And we wait for the response. */
385 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
386 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
387 dev_err(dev, "timed out in resp(caller: %pS)\n",
393 if (!ret && xfer->hdr.status)
394 ret = scmi_to_linux_errno(xfer->hdr.status);
396 if (info->desc->ops->mark_txdone)
397 info->desc->ops->mark_txdone(cinfo, ret);
399 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id,
400 xfer->hdr.protocol_id, xfer->hdr.seq, ret);
405 void scmi_reset_rx_to_maxsz(const struct scmi_handle *handle,
406 struct scmi_xfer *xfer)
408 struct scmi_info *info = handle_to_scmi_info(handle);
410 xfer->rx.len = info->desc->max_msg_size;
413 #define SCMI_MAX_RESPONSE_TIMEOUT (2 * MSEC_PER_SEC)
416 * scmi_do_xfer_with_response() - Do one transfer and wait until the delayed
417 * response is received
419 * @handle: Pointer to SCMI entity handle
420 * @xfer: Transfer to initiate and wait for response
422 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
423 * return corresponding error, else if all goes well, return 0.
425 int scmi_do_xfer_with_response(const struct scmi_handle *handle,
426 struct scmi_xfer *xfer)
428 int ret, timeout = msecs_to_jiffies(SCMI_MAX_RESPONSE_TIMEOUT);
429 DECLARE_COMPLETION_ONSTACK(async_response);
431 xfer->async_done = &async_response;
433 ret = scmi_do_xfer(handle, xfer);
434 if (!ret && !wait_for_completion_timeout(xfer->async_done, timeout))
437 xfer->async_done = NULL;
442 * scmi_xfer_get_init() - Allocate and initialise one message for transmit
444 * @handle: Pointer to SCMI entity handle
445 * @msg_id: Message identifier
446 * @prot_id: Protocol identifier for the message
447 * @tx_size: transmit message size
448 * @rx_size: receive message size
449 * @p: pointer to the allocated and initialised message
451 * This function allocates the message using @scmi_xfer_get and
452 * initialise the header.
454 * Return: 0 if all went fine with @p pointing to message, else
455 * corresponding error.
457 int scmi_xfer_get_init(const struct scmi_handle *handle, u8 msg_id, u8 prot_id,
458 size_t tx_size, size_t rx_size, struct scmi_xfer **p)
461 struct scmi_xfer *xfer;
462 struct scmi_info *info = handle_to_scmi_info(handle);
463 struct scmi_xfers_info *minfo = &info->tx_minfo;
464 struct device *dev = info->dev;
466 /* Ensure we have sane transfer sizes */
467 if (rx_size > info->desc->max_msg_size ||
468 tx_size > info->desc->max_msg_size)
471 xfer = scmi_xfer_get(handle, minfo);
474 dev_err(dev, "failed to get free message slot(%d)\n", ret);
478 xfer->tx.len = tx_size;
479 xfer->rx.len = rx_size ? : info->desc->max_msg_size;
480 xfer->hdr.id = msg_id;
481 xfer->hdr.protocol_id = prot_id;
482 xfer->hdr.poll_completion = false;
490 * scmi_version_get() - command to get the revision of the SCMI entity
492 * @handle: Pointer to SCMI entity handle
493 * @protocol: Protocol identifier for the message
494 * @version: Holds returned version of protocol.
496 * Updates the SCMI information in the internal data structure.
498 * Return: 0 if all went fine, else return appropriate error.
500 int scmi_version_get(const struct scmi_handle *handle, u8 protocol,
507 ret = scmi_xfer_get_init(handle, PROTOCOL_VERSION, protocol, 0,
508 sizeof(*version), &t);
512 ret = scmi_do_xfer(handle, t);
514 rev_info = t->rx.buf;
515 *version = le32_to_cpu(*rev_info);
518 scmi_xfer_put(handle, t);
522 void scmi_setup_protocol_implemented(const struct scmi_handle *handle,
525 struct scmi_info *info = handle_to_scmi_info(handle);
527 info->protocols_imp = prot_imp;
531 scmi_is_protocol_implemented(const struct scmi_handle *handle, u8 prot_id)
534 struct scmi_info *info = handle_to_scmi_info(handle);
536 if (!info->protocols_imp)
539 for (i = 0; i < MAX_PROTOCOLS_IMP; i++)
540 if (info->protocols_imp[i] == prot_id)
546 * scmi_handle_get() - Get the SCMI handle for a device
548 * @dev: pointer to device for which we want SCMI handle
550 * NOTE: The function does not track individual clients of the framework
551 * and is expected to be maintained by caller of SCMI protocol library.
552 * scmi_handle_put must be balanced with successful scmi_handle_get
554 * Return: pointer to handle if successful, NULL on error
556 struct scmi_handle *scmi_handle_get(struct device *dev)
559 struct scmi_info *info;
560 struct scmi_handle *handle = NULL;
562 mutex_lock(&scmi_list_mutex);
563 list_for_each(p, &scmi_list) {
564 info = list_entry(p, struct scmi_info, node);
565 if (dev->parent == info->dev) {
566 handle = &info->handle;
571 mutex_unlock(&scmi_list_mutex);
577 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
579 * @handle: handle acquired by scmi_handle_get
581 * NOTE: The function does not track individual clients of the framework
582 * and is expected to be maintained by caller of SCMI protocol library.
583 * scmi_handle_put must be balanced with successful scmi_handle_get
585 * Return: 0 is successfully released
586 * if null was passed, it returns -EINVAL;
588 int scmi_handle_put(const struct scmi_handle *handle)
590 struct scmi_info *info;
595 info = handle_to_scmi_info(handle);
596 mutex_lock(&scmi_list_mutex);
597 if (!WARN_ON(!info->users))
599 mutex_unlock(&scmi_list_mutex);
604 static int __scmi_xfer_info_init(struct scmi_info *sinfo,
605 struct scmi_xfers_info *info)
608 struct scmi_xfer *xfer;
609 struct device *dev = sinfo->dev;
610 const struct scmi_desc *desc = sinfo->desc;
612 /* Pre-allocated messages, no more than what hdr.seq can support */
613 if (WARN_ON(desc->max_msg >= MSG_TOKEN_MAX)) {
614 dev_err(dev, "Maximum message of %d exceeds supported %ld\n",
615 desc->max_msg, MSG_TOKEN_MAX);
619 info->xfer_block = devm_kcalloc(dev, desc->max_msg,
620 sizeof(*info->xfer_block), GFP_KERNEL);
621 if (!info->xfer_block)
624 info->xfer_alloc_table = devm_kcalloc(dev, BITS_TO_LONGS(desc->max_msg),
625 sizeof(long), GFP_KERNEL);
626 if (!info->xfer_alloc_table)
629 /* Pre-initialize the buffer pointer to pre-allocated buffers */
630 for (i = 0, xfer = info->xfer_block; i < desc->max_msg; i++, xfer++) {
631 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size,
636 xfer->tx.buf = xfer->rx.buf;
637 init_completion(&xfer->done);
640 spin_lock_init(&info->xfer_lock);
645 static int scmi_xfer_info_init(struct scmi_info *sinfo)
647 int ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo);
649 if (!ret && idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE))
650 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo);
655 static int scmi_chan_setup(struct scmi_info *info, struct device *dev,
656 int prot_id, bool tx)
659 struct scmi_chan_info *cinfo;
662 /* Transmit channel is first entry i.e. index 0 */
664 idr = tx ? &info->tx_idr : &info->rx_idr;
666 /* check if already allocated, used for multiple device per protocol */
667 cinfo = idr_find(idr, prot_id);
671 if (!info->desc->ops->chan_available(dev, idx)) {
672 cinfo = idr_find(idr, SCMI_PROTOCOL_BASE);
673 if (unlikely(!cinfo)) /* Possible only if platform has no Rx */
678 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL);
684 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx);
689 ret = idr_alloc(idr, cinfo, prot_id, prot_id + 1, GFP_KERNEL);
690 if (ret != prot_id) {
691 dev_err(dev, "unable to allocate SCMI idr slot err %d\n", ret);
695 cinfo->handle = &info->handle;
700 scmi_txrx_setup(struct scmi_info *info, struct device *dev, int prot_id)
702 int ret = scmi_chan_setup(info, dev, prot_id, true);
704 if (!ret) /* Rx is optional, hence no error check */
705 scmi_chan_setup(info, dev, prot_id, false);
711 scmi_create_protocol_device(struct device_node *np, struct scmi_info *info,
712 int prot_id, const char *name)
714 struct scmi_device *sdev;
716 sdev = scmi_device_create(np, info->dev, prot_id, name);
718 dev_err(info->dev, "failed to create %d protocol device\n",
723 if (scmi_txrx_setup(info, &sdev->dev, prot_id)) {
724 dev_err(&sdev->dev, "failed to setup transport\n");
725 scmi_device_destroy(sdev);
729 /* setup handle now as the transport is ready */
730 scmi_set_handle(sdev);
733 #define MAX_SCMI_DEV_PER_PROTOCOL 2
734 struct scmi_prot_devnames {
736 char *names[MAX_SCMI_DEV_PER_PROTOCOL];
739 static struct scmi_prot_devnames devnames[] = {
740 { SCMI_PROTOCOL_POWER, { "genpd" },},
741 { SCMI_PROTOCOL_SYSTEM, { "syspower" },},
742 { SCMI_PROTOCOL_PERF, { "cpufreq" },},
743 { SCMI_PROTOCOL_CLOCK, { "clocks" },},
744 { SCMI_PROTOCOL_SENSOR, { "hwmon" },},
745 { SCMI_PROTOCOL_RESET, { "reset" },},
749 scmi_create_protocol_devices(struct device_node *np, struct scmi_info *info,
754 for (loop = 0; loop < ARRAY_SIZE(devnames); loop++) {
755 if (devnames[loop].protocol_id != prot_id)
758 for (cnt = 0; cnt < ARRAY_SIZE(devnames[loop].names); cnt++) {
759 const char *name = devnames[loop].names[cnt];
762 scmi_create_protocol_device(np, info, prot_id,
768 static int scmi_probe(struct platform_device *pdev)
771 struct scmi_handle *handle;
772 const struct scmi_desc *desc;
773 struct scmi_info *info;
774 struct device *dev = &pdev->dev;
775 struct device_node *child, *np = dev->of_node;
777 desc = of_device_get_match_data(dev);
781 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
787 INIT_LIST_HEAD(&info->node);
789 platform_set_drvdata(pdev, info);
790 idr_init(&info->tx_idr);
791 idr_init(&info->rx_idr);
793 handle = &info->handle;
794 handle->dev = info->dev;
795 handle->version = &info->version;
797 ret = scmi_txrx_setup(info, dev, SCMI_PROTOCOL_BASE);
801 ret = scmi_xfer_info_init(info);
805 if (scmi_notification_init(handle))
806 dev_err(dev, "SCMI Notifications NOT available.\n");
808 ret = scmi_base_protocol_init(handle);
810 dev_err(dev, "unable to communicate with SCMI(%d)\n", ret);
814 mutex_lock(&scmi_list_mutex);
815 list_add_tail(&info->node, &scmi_list);
816 mutex_unlock(&scmi_list_mutex);
818 for_each_available_child_of_node(np, child) {
821 if (of_property_read_u32(child, "reg", &prot_id))
824 if (!FIELD_FIT(MSG_PROTOCOL_ID_MASK, prot_id))
825 dev_err(dev, "Out of range protocol %d\n", prot_id);
827 if (!scmi_is_protocol_implemented(handle, prot_id)) {
828 dev_err(dev, "SCMI protocol %d not implemented\n",
833 scmi_create_protocol_devices(child, info, prot_id);
839 void scmi_free_channel(struct scmi_chan_info *cinfo, struct idr *idr, int id)
844 static int scmi_remove(struct platform_device *pdev)
847 struct scmi_info *info = platform_get_drvdata(pdev);
848 struct idr *idr = &info->tx_idr;
850 scmi_notification_exit(&info->handle);
852 mutex_lock(&scmi_list_mutex);
856 list_del(&info->node);
857 mutex_unlock(&scmi_list_mutex);
862 /* Safe to free channels since no more users */
863 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
864 idr_destroy(&info->tx_idr);
867 ret = idr_for_each(idr, info->desc->ops->chan_free, idr);
868 idr_destroy(&info->rx_idr);
873 static ssize_t protocol_version_show(struct device *dev,
874 struct device_attribute *attr, char *buf)
876 struct scmi_info *info = dev_get_drvdata(dev);
878 return sprintf(buf, "%u.%u\n", info->version.major_ver,
879 info->version.minor_ver);
881 static DEVICE_ATTR_RO(protocol_version);
883 static ssize_t firmware_version_show(struct device *dev,
884 struct device_attribute *attr, char *buf)
886 struct scmi_info *info = dev_get_drvdata(dev);
888 return sprintf(buf, "0x%x\n", info->version.impl_ver);
890 static DEVICE_ATTR_RO(firmware_version);
892 static ssize_t vendor_id_show(struct device *dev,
893 struct device_attribute *attr, char *buf)
895 struct scmi_info *info = dev_get_drvdata(dev);
897 return sprintf(buf, "%s\n", info->version.vendor_id);
899 static DEVICE_ATTR_RO(vendor_id);
901 static ssize_t sub_vendor_id_show(struct device *dev,
902 struct device_attribute *attr, char *buf)
904 struct scmi_info *info = dev_get_drvdata(dev);
906 return sprintf(buf, "%s\n", info->version.sub_vendor_id);
908 static DEVICE_ATTR_RO(sub_vendor_id);
910 static struct attribute *versions_attrs[] = {
911 &dev_attr_firmware_version.attr,
912 &dev_attr_protocol_version.attr,
913 &dev_attr_vendor_id.attr,
914 &dev_attr_sub_vendor_id.attr,
917 ATTRIBUTE_GROUPS(versions);
919 /* Each compatible listed below must have descriptor associated with it */
920 static const struct of_device_id scmi_of_match[] = {
921 { .compatible = "arm,scmi", .data = &scmi_mailbox_desc },
922 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
923 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
928 MODULE_DEVICE_TABLE(of, scmi_of_match);
930 static struct platform_driver scmi_driver = {
933 .of_match_table = scmi_of_match,
934 .dev_groups = versions_groups,
937 .remove = scmi_remove,
940 static int __init scmi_driver_init(void)
944 scmi_clock_register();
945 scmi_perf_register();
946 scmi_power_register();
947 scmi_reset_register();
948 scmi_sensors_register();
949 scmi_system_register();
951 return platform_driver_register(&scmi_driver);
953 subsys_initcall(scmi_driver_init);
955 static void __exit scmi_driver_exit(void)
959 scmi_clock_unregister();
960 scmi_perf_unregister();
961 scmi_power_unregister();
962 scmi_reset_unregister();
963 scmi_sensors_unregister();
964 scmi_system_unregister();
966 platform_driver_unregister(&scmi_driver);
968 module_exit(scmi_driver_exit);
970 MODULE_ALIAS("platform: arm-scmi");
971 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
972 MODULE_DESCRIPTION("ARM SCMI protocol driver");
973 MODULE_LICENSE("GPL v2");