Lines Matching +full:protocol +full:- +full:id
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)
7 * provides a mechanism for inter-processor communication between SCP's
14 * Copyright (C) 2018-2021 ARM Ltd.
25 #include <linux/io-64-nonatomic-hi-lo.h>
54 /* Track the unique id for the transfers for debug & profiling purpose */
60 * struct scmi_xfers_info - Structure to manage transfer information
68 * a number of xfers equal to the maximum allowed in-flight
71 * currently in-flight messages.
82 * struct scmi_protocol_instance - Describe an initialized protocol instance.
83 * @handle: Reference to the SCMI handle associated to this protocol instance.
84 * @proto: A reference to the protocol descriptor.
85 * @gid: A reference for per-protocol devres management.
86 * @users: A refcount to track effective users of this protocol.
87 * @priv: Reference for optional protocol private data.
88 * @ph: An embedded protocol handle that will be passed down to protocol
91 * Each protocol is initialized independently once for each SCMI platform in
106 * struct scmi_debug_info - Debug common info
120 * struct scmi_info - Structure representing a SCMI instance
122 * @id: A sequence number starting from zero identifying this instance
125 * @version: SCMI revision information containing protocol version,
126 * implementation version and (sub-)vendor identification.
130 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
131 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
133 * this SCMI instance: populated on protocol's first attempted
138 * base protocol
141 * @atomic_threshold: Optional system wide DT-configured threshold, expressed
144 * to have an execution latency lesser-equal to the threshold
158 int id; member
193 if (!proto || !try_module_get(proto->owner)) { in scmi_protocol_get()
194 pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id); in scmi_protocol_get()
198 pr_debug("Found SCMI Protocol 0x%x\n", protocol_id); in scmi_protocol_get()
209 module_put(proto->owner); in scmi_protocol_put()
217 pr_err("invalid protocol\n"); in scmi_protocol_register()
218 return -EINVAL; in scmi_protocol_register()
221 if (!proto->instance_init) { in scmi_protocol_register()
222 pr_err("missing init for protocol 0x%x\n", proto->id); in scmi_protocol_register()
223 return -EINVAL; in scmi_protocol_register()
228 proto->id, proto->id + 1, GFP_ATOMIC); in scmi_protocol_register()
230 if (ret != proto->id) { in scmi_protocol_register()
231 pr_err("unable to allocate SCMI idr slot for 0x%x - err %d\n", in scmi_protocol_register()
232 proto->id, ret); in scmi_protocol_register()
236 pr_debug("Registered SCMI Protocol 0x%x\n", proto->id); in scmi_protocol_register()
245 idr_remove(&scmi_protocols, proto->id); in scmi_protocol_unregister()
248 pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id); in scmi_protocol_unregister()
253 * scmi_create_protocol_devices - Create devices for all pending requests for
256 * @np: The device node describing the protocol
258 * @prot_id: The protocol ID
261 * for the specified protocol.
269 mutex_lock(&info->devreq_mtx); in scmi_create_protocol_devices()
270 sdev = scmi_device_create(np, info->dev, prot_id, name); in scmi_create_protocol_devices()
272 dev_err(info->dev, in scmi_create_protocol_devices()
273 "failed to create device for protocol 0x%X (%s)\n", in scmi_create_protocol_devices()
275 mutex_unlock(&info->devreq_mtx); in scmi_create_protocol_devices()
281 mutex_lock(&info->devreq_mtx); in scmi_destroy_protocol_devices()
282 scmi_device_destroy(info->dev, prot_id, name); in scmi_destroy_protocol_devices()
283 mutex_unlock(&info->devreq_mtx); in scmi_destroy_protocol_devices()
291 info->notify_priv = priv; in scmi_notification_instance_data_set()
292 /* Ensure updated protocol private date are visible */ in scmi_notification_instance_data_set()
302 return info->notify_priv; in scmi_notification_instance_data_get()
306 * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
312 * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
313 * reuse of freshly completed or timed-out xfers, thus mitigating the risk
314 * of incorrect association of a late and expired xfer with a live in-flight
315 * transaction, both happening to re-use the same token identifier.
317 * Since platform is NOT required to answer our request in-order we should
320 * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
323 * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
327 * X = used in-flight
330 * ------
332 * |- xfer_id picked
333 * -----------+----------------------------------------------------------
335 * ----------------------------------------------------------------------
337 * |- next_token
339 * Out-of-order pending at start
340 * -----------------------------
342 * |- xfer_id picked, last_token fixed
343 * -----+----------------------------------------------------------------
345 * ----------------------------------------------------------------------
347 * |- next_token
350 * Out-of-order pending at end
351 * ---------------------------
353 * |- xfer_id picked, last_token fixed
354 * -----+----------------------------------------------------------------
356 * ----------------------------------------------------------------------
358 * |- next_token
370 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1] in scmi_xfer_token_set()
371 * using the pre-allocated transfer_id as a base. in scmi_xfer_token_set()
377 next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1)); in scmi_xfer_token_set()
380 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, in scmi_xfer_token_set()
384 * After heavily out-of-order responses, there are no free in scmi_xfer_token_set()
388 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, in scmi_xfer_token_set()
392 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages in scmi_xfer_token_set()
393 * but we have not found any free token [0, MSG_TOKEN_MAX - 1]. in scmi_xfer_token_set()
396 return -ENOMEM; in scmi_xfer_token_set()
399 /* Update +/- last_token accordingly if we skipped some hole */ in scmi_xfer_token_set()
401 atomic_add((int)(xfer_id - next_token), &transfer_last_id); in scmi_xfer_token_set()
403 xfer->hdr.seq = (u16)xfer_id; in scmi_xfer_token_set()
409 * scmi_xfer_token_clear - Release the token
417 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table); in scmi_xfer_token_clear()
421 * scmi_xfer_inflight_register_unlocked - Register the xfer as in-flight
426 * Note that this helper assumes that the xfer to be registered as in-flight
436 /* Set in-flight */ in scmi_xfer_inflight_register_unlocked()
437 set_bit(xfer->hdr.seq, minfo->xfer_alloc_table); in scmi_xfer_inflight_register_unlocked()
438 hash_add(minfo->pending_xfers, &xfer->node, xfer->hdr.seq); in scmi_xfer_inflight_register_unlocked()
439 xfer->pending = true; in scmi_xfer_inflight_register_unlocked()
443 * scmi_xfer_inflight_register - Try to register an xfer as in-flight
451 * same sequence number is currently still registered as in-flight.
453 * Return: 0 on Success or -EBUSY if sequence number embedded in the xfer
462 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_inflight_register()
463 if (!test_bit(xfer->hdr.seq, minfo->xfer_alloc_table)) in scmi_xfer_inflight_register()
466 ret = -EBUSY; in scmi_xfer_inflight_register()
467 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_inflight_register()
473 * scmi_xfer_raw_inflight_register - An helper to register the given xfer as in
486 return scmi_xfer_inflight_register(xfer, &info->tx_minfo); in scmi_xfer_raw_inflight_register()
490 * scmi_xfer_pending_set - Pick a proper sequence number and mark the xfer
491 * as pending in-flight
504 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_pending_set()
509 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_pending_set()
515 * scmi_xfer_get() - Allocate one message
541 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_get()
542 if (hlist_empty(&minfo->free_xfers)) { in scmi_xfer_get()
543 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_get()
544 return ERR_PTR(-ENOMEM); in scmi_xfer_get()
548 xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node); in scmi_xfer_get()
549 hlist_del_init(&xfer->node); in scmi_xfer_get()
555 xfer->transfer_id = atomic_inc_return(&transfer_last_id); in scmi_xfer_get()
557 refcount_set(&xfer->users, 1); in scmi_xfer_get()
558 atomic_set(&xfer->busy, SCMI_XFER_FREE); in scmi_xfer_get()
559 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_get()
565 * scmi_xfer_raw_get - Helper to get a bare free xfer from the TX channel
571 * Return: A valid xfer on Success, or an error-pointer otherwise
578 xfer = scmi_xfer_get(handle, &info->tx_minfo); in scmi_xfer_raw_get()
580 xfer->flags |= SCMI_XFER_FLAG_IS_RAW; in scmi_xfer_raw_get()
586 * scmi_xfer_raw_channel_get - Helper to get a reference to the proper channel
590 * @protocol_id: Identifier of the protocol
592 * Note that in a regular SCMI stack, usually, a protocol has to be defined in
594 * protocol in range is allowed, re-using the Base channel, so as to enable
595 * fuzzing on any protocol without the need of a fully compiled DT.
605 cinfo = idr_find(&info->tx_idr, protocol_id); in scmi_xfer_raw_channel_get()
608 return ERR_PTR(-EINVAL); in scmi_xfer_raw_channel_get()
610 cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE); in scmi_xfer_raw_channel_get()
612 return ERR_PTR(-EINVAL); in scmi_xfer_raw_channel_get()
613 dev_warn_once(handle->dev, in scmi_xfer_raw_channel_get()
614 "Using Base channel for protocol 0x%X\n", in scmi_xfer_raw_channel_get()
622 * __scmi_xfer_put() - Release a message
637 spin_lock_irqsave(&minfo->xfer_lock, flags); in __scmi_xfer_put()
638 if (refcount_dec_and_test(&xfer->users)) { in __scmi_xfer_put()
639 if (xfer->pending) { in __scmi_xfer_put()
641 hash_del(&xfer->node); in __scmi_xfer_put()
642 xfer->pending = false; in __scmi_xfer_put()
644 hlist_add_head(&xfer->node, &minfo->free_xfers); in __scmi_xfer_put()
646 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in __scmi_xfer_put()
650 * scmi_xfer_raw_put - Release an xfer that was taken by @scmi_xfer_raw_get
662 xfer->flags &= ~SCMI_XFER_FLAG_IS_RAW; in scmi_xfer_raw_put()
663 xfer->flags &= ~SCMI_XFER_FLAG_CHAN_SET; in scmi_xfer_raw_put()
664 return __scmi_xfer_put(&info->tx_minfo, xfer); in scmi_xfer_raw_put()
668 * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
671 * @xfer_id: Token ID to lookup in @pending_xfers
684 if (test_bit(xfer_id, minfo->xfer_alloc_table)) in scmi_xfer_lookup_unlocked()
685 xfer = XFER_FIND(minfo->pending_xfers, xfer_id); in scmi_xfer_lookup_unlocked()
687 return xfer ?: ERR_PTR(-EINVAL); in scmi_xfer_lookup_unlocked()
691 * scmi_msg_response_validate - Validate message type against state of related
700 * related synchronous response (Out-of-Order Delayed Response) the missing
703 * SCMI transport can deliver such out-of-order responses.
705 * Context: Assumes to be called with xfer->lock already acquired.
716 * delayed response we're not prepared to handle: bail-out safely in scmi_msg_response_validate()
719 if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) { in scmi_msg_response_validate()
720 dev_err(cinfo->dev, in scmi_msg_response_validate()
722 xfer->hdr.seq); in scmi_msg_response_validate()
723 return -EINVAL; in scmi_msg_response_validate()
726 switch (xfer->state) { in scmi_msg_response_validate()
733 xfer->hdr.status = SCMI_SUCCESS; in scmi_msg_response_validate()
734 xfer->state = SCMI_XFER_RESP_OK; in scmi_msg_response_validate()
735 complete(&xfer->done); in scmi_msg_response_validate()
736 dev_warn(cinfo->dev, in scmi_msg_response_validate()
738 xfer->hdr.seq); in scmi_msg_response_validate()
743 return -EINVAL; in scmi_msg_response_validate()
747 return -EINVAL; in scmi_msg_response_validate()
754 * scmi_xfer_state_update - Update xfer state
767 xfer->hdr.type = msg_type; in scmi_xfer_state_update()
770 if (xfer->hdr.type == MSG_TYPE_COMMAND) in scmi_xfer_state_update()
771 xfer->state = SCMI_XFER_RESP_OK; in scmi_xfer_state_update()
773 xfer->state = SCMI_XFER_DRESP_OK; in scmi_xfer_state_update()
780 ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY); in scmi_xfer_acquired()
786 * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
803 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_xfer_command_acquire()
804 struct scmi_xfers_info *minfo = &info->tx_minfo; in scmi_xfer_command_acquire()
809 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_command_acquire()
812 dev_err(cinfo->dev, in scmi_xfer_command_acquire()
815 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_command_acquire()
818 refcount_inc(&xfer->users); in scmi_xfer_command_acquire()
819 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_command_acquire()
821 spin_lock_irqsave(&xfer->lock, flags); in scmi_xfer_command_acquire()
834 spin_unlock_irqrestore(&xfer->lock, flags); in scmi_xfer_command_acquire()
837 dev_err(cinfo->dev, in scmi_xfer_command_acquire()
838 "Invalid message type:%d for %d - HDR:0x%X state:%d\n", in scmi_xfer_command_acquire()
839 msg_type, xfer_id, msg_hdr, xfer->state); in scmi_xfer_command_acquire()
842 xfer = ERR_PTR(-EINVAL); in scmi_xfer_command_acquire()
851 atomic_set(&xfer->busy, SCMI_XFER_FREE); in scmi_xfer_command_release()
852 __scmi_xfer_put(&info->tx_minfo, xfer); in scmi_xfer_command_release()
858 if (info->desc->ops->clear_channel) in scmi_clear_channel()
859 info->desc->ops->clear_channel(cinfo); in scmi_clear_channel()
866 struct device *dev = cinfo->dev; in scmi_handle_notification()
867 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_handle_notification()
868 struct scmi_xfers_info *minfo = &info->rx_minfo; in scmi_handle_notification()
872 xfer = scmi_xfer_get(cinfo->handle, minfo); in scmi_handle_notification()
880 unpack_scmi_header(msg_hdr, &xfer->hdr); in scmi_handle_notification()
882 /* Ensure order between xfer->priv store and following ops */ in scmi_handle_notification()
883 smp_store_mb(xfer->priv, priv); in scmi_handle_notification()
884 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size, in scmi_handle_notification()
887 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, in scmi_handle_notification()
888 xfer->hdr.id, "NOTI", xfer->hdr.seq, in scmi_handle_notification()
889 xfer->hdr.status, xfer->rx.buf, xfer->rx.len); in scmi_handle_notification()
891 scmi_notify(cinfo->handle, xfer->hdr.protocol_id, in scmi_handle_notification()
892 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts); in scmi_handle_notification()
894 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, in scmi_handle_notification()
895 xfer->hdr.protocol_id, xfer->hdr.seq, in scmi_handle_notification()
899 xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr); in scmi_handle_notification()
900 scmi_raw_message_report(info->raw, xfer, SCMI_RAW_NOTIF_QUEUE, in scmi_handle_notification()
901 cinfo->id); in scmi_handle_notification()
913 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_handle_response()
918 scmi_raw_error_report(info->raw, cinfo, msg_hdr, priv); in scmi_handle_response()
926 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) in scmi_handle_response()
927 xfer->rx.len = info->desc->max_msg_size; in scmi_handle_response()
930 /* Ensure order between xfer->priv store and following ops */ in scmi_handle_response()
931 smp_store_mb(xfer->priv, priv); in scmi_handle_response()
932 info->desc->ops->fetch_response(cinfo, xfer); in scmi_handle_response()
934 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, in scmi_handle_response()
935 xfer->hdr.id, in scmi_handle_response()
936 xfer->hdr.type == MSG_TYPE_DELAYED_RESP ? in scmi_handle_response()
939 xfer->hdr.seq, xfer->hdr.status, in scmi_handle_response()
940 xfer->rx.buf, xfer->rx.len); in scmi_handle_response()
942 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, in scmi_handle_response()
943 xfer->hdr.protocol_id, xfer->hdr.seq, in scmi_handle_response()
944 xfer->hdr.type); in scmi_handle_response()
946 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) { in scmi_handle_response()
948 complete(xfer->async_done); in scmi_handle_response()
950 complete(&xfer->done); in scmi_handle_response()
959 if (!xfer->hdr.poll_completion) in scmi_handle_response()
960 scmi_raw_message_report(info->raw, xfer, in scmi_handle_response()
962 cinfo->id); in scmi_handle_response()
969 * scmi_rx_callback() - callback for receiving messages
1000 * xfer_put() - Release a transmit message
1002 * @ph: Pointer to SCMI protocol handle
1009 struct scmi_info *info = handle_to_scmi_info(pi->handle); in xfer_put()
1011 __scmi_xfer_put(&info->tx_minfo, xfer); in xfer_put()
1017 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_xfer_done_no_timeout()
1020 * Poll also on xfer->done so that polling can be forcibly terminated in scmi_xfer_done_no_timeout()
1021 * in case of out-of-order receptions of delayed responses in scmi_xfer_done_no_timeout()
1023 return info->desc->ops->poll_done(cinfo, xfer) || in scmi_xfer_done_no_timeout()
1024 try_wait_for_completion(&xfer->done) || in scmi_xfer_done_no_timeout()
1034 if (xfer->hdr.poll_completion) { in scmi_wait_for_reply()
1039 if (!desc->sync_cmds_completed_on_ret) { in scmi_wait_for_reply()
1050 "timed out in resp(caller: %pS) - polling\n", in scmi_wait_for_reply()
1052 ret = -ETIMEDOUT; in scmi_wait_for_reply()
1059 handle_to_scmi_info(cinfo->handle); in scmi_wait_for_reply()
1062 * Do not fetch_response if an out-of-order delayed in scmi_wait_for_reply()
1065 spin_lock_irqsave(&xfer->lock, flags); in scmi_wait_for_reply()
1066 if (xfer->state == SCMI_XFER_SENT_OK) { in scmi_wait_for_reply()
1067 desc->ops->fetch_response(cinfo, xfer); in scmi_wait_for_reply()
1068 xfer->state = SCMI_XFER_RESP_OK; in scmi_wait_for_reply()
1070 spin_unlock_irqrestore(&xfer->lock, flags); in scmi_wait_for_reply()
1073 trace_scmi_msg_dump(info->id, cinfo->id, in scmi_wait_for_reply()
1074 xfer->hdr.protocol_id, xfer->hdr.id, in scmi_wait_for_reply()
1077 xfer->hdr.seq, xfer->hdr.status, in scmi_wait_for_reply()
1078 xfer->rx.buf, xfer->rx.len); in scmi_wait_for_reply()
1082 handle_to_scmi_info(cinfo->handle); in scmi_wait_for_reply()
1084 scmi_raw_message_report(info->raw, xfer, in scmi_wait_for_reply()
1086 cinfo->id); in scmi_wait_for_reply()
1091 if (!wait_for_completion_timeout(&xfer->done, in scmi_wait_for_reply()
1095 ret = -ETIMEDOUT; in scmi_wait_for_reply()
1103 * scmi_wait_for_message_response - An helper to group all the possible ways of
1109 * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
1110 * configuration flags like xfer->hdr.poll_completion.
1117 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_wait_for_message_response()
1118 struct device *dev = info->dev; in scmi_wait_for_message_response()
1120 trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id, in scmi_wait_for_message_response()
1121 xfer->hdr.protocol_id, xfer->hdr.seq, in scmi_wait_for_message_response()
1122 info->desc->max_rx_timeout_ms, in scmi_wait_for_message_response()
1123 xfer->hdr.poll_completion); in scmi_wait_for_message_response()
1125 return scmi_wait_for_reply(dev, info->desc, cinfo, xfer, in scmi_wait_for_message_response()
1126 info->desc->max_rx_timeout_ms); in scmi_wait_for_message_response()
1130 * scmi_xfer_raw_wait_for_message_response - An helper to wait for a message
1144 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_xfer_raw_wait_for_message_response()
1145 struct device *dev = info->dev; in scmi_xfer_raw_wait_for_message_response()
1147 ret = scmi_wait_for_reply(dev, info->desc, cinfo, xfer, timeout_ms); in scmi_xfer_raw_wait_for_message_response()
1149 dev_dbg(dev, "timed out in RAW response - HDR:%08X\n", in scmi_xfer_raw_wait_for_message_response()
1150 pack_scmi_header(&xfer->hdr)); in scmi_xfer_raw_wait_for_message_response()
1156 * do_xfer() - Do one transfer
1158 * @ph: Pointer to SCMI protocol handle
1161 * Return: -ETIMEDOUT in case of no response, if transmit error,
1170 struct scmi_info *info = handle_to_scmi_info(pi->handle); in do_xfer()
1171 struct device *dev = info->dev; in do_xfer()
1175 if (xfer->hdr.poll_completion && in do_xfer()
1176 !is_transport_polling_capable(info->desc)) { in do_xfer()
1179 return -EINVAL; in do_xfer()
1182 cinfo = idr_find(&info->tx_idr, pi->proto->id); in do_xfer()
1184 return -EINVAL; in do_xfer()
1187 if (is_polling_enabled(cinfo, info->desc)) in do_xfer()
1188 xfer->hdr.poll_completion = true; in do_xfer()
1191 * Initialise protocol id now from protocol handle to avoid it being in do_xfer()
1192 * overridden by mistake (or malice) by the protocol code mangling with in do_xfer()
1195 xfer->hdr.protocol_id = pi->proto->id; in do_xfer()
1196 reinit_completion(&xfer->done); in do_xfer()
1198 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id, in do_xfer()
1199 xfer->hdr.protocol_id, xfer->hdr.seq, in do_xfer()
1200 xfer->hdr.poll_completion); in do_xfer()
1203 xfer->hdr.status = SCMI_SUCCESS; in do_xfer()
1204 xfer->state = SCMI_XFER_SENT_OK; in do_xfer()
1207 * on xfer->state due to the monotonically increasing tokens allocation, in do_xfer()
1208 * we must anyway ensure xfer->state initialization is not re-ordered in do_xfer()
1210 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state. in do_xfer()
1214 ret = info->desc->ops->send_message(cinfo, xfer); in do_xfer()
1220 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, in do_xfer()
1221 xfer->hdr.id, "CMND", xfer->hdr.seq, in do_xfer()
1222 xfer->hdr.status, xfer->tx.buf, xfer->tx.len); in do_xfer()
1225 if (!ret && xfer->hdr.status) in do_xfer()
1226 ret = scmi_to_linux_errno(xfer->hdr.status); in do_xfer()
1228 if (info->desc->ops->mark_txdone) in do_xfer()
1229 info->desc->ops->mark_txdone(cinfo, ret, xfer); in do_xfer()
1231 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id, in do_xfer()
1232 xfer->hdr.protocol_id, xfer->hdr.seq, ret); in do_xfer()
1241 struct scmi_info *info = handle_to_scmi_info(pi->handle); in reset_rx_to_maxsz()
1243 xfer->rx.len = info->desc->max_msg_size; in reset_rx_to_maxsz()
1247 * do_xfer_with_response() - Do one transfer and wait until the delayed
1250 * @ph: Pointer to SCMI protocol handle
1254 * it could cause long busy-waiting here, so ignore polling for the delayed
1261 * attained at the protocol layer), but this would also have led to longer
1267 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
1276 xfer->async_done = &async_response; in do_xfer_with_response()
1284 WARN_ON_ONCE(xfer->hdr.poll_completion); in do_xfer_with_response()
1288 if (!wait_for_completion_timeout(xfer->async_done, timeout)) { in do_xfer_with_response()
1289 dev_err(ph->dev, in do_xfer_with_response()
1292 ret = -ETIMEDOUT; in do_xfer_with_response()
1293 } else if (xfer->hdr.status) { in do_xfer_with_response()
1294 ret = scmi_to_linux_errno(xfer->hdr.status); in do_xfer_with_response()
1298 xfer->async_done = NULL; in do_xfer_with_response()
1303 * xfer_get_init() - Allocate and initialise one message for transmit
1305 * @ph: Pointer to SCMI protocol handle
1324 struct scmi_info *info = handle_to_scmi_info(pi->handle); in xfer_get_init()
1325 struct scmi_xfers_info *minfo = &info->tx_minfo; in xfer_get_init()
1326 struct device *dev = info->dev; in xfer_get_init()
1329 if (rx_size > info->desc->max_msg_size || in xfer_get_init()
1330 tx_size > info->desc->max_msg_size) in xfer_get_init()
1331 return -ERANGE; in xfer_get_init()
1333 xfer = scmi_xfer_get(pi->handle, minfo); in xfer_get_init()
1340 /* Pick a sequence number and register this xfer as in-flight */ in xfer_get_init()
1343 dev_err(pi->handle->dev, in xfer_get_init()
1349 xfer->tx.len = tx_size; in xfer_get_init()
1350 xfer->rx.len = rx_size ? : info->desc->max_msg_size; in xfer_get_init()
1351 xfer->hdr.type = MSG_TYPE_COMMAND; in xfer_get_init()
1352 xfer->hdr.id = msg_id; in xfer_get_init()
1353 xfer->hdr.poll_completion = false; in xfer_get_init()
1361 * version_get() - command to get the revision of the SCMI entity
1363 * @ph: Pointer to SCMI protocol handle
1364 * @version: Holds returned version of protocol.
1382 rev_info = t->rx.buf; in version_get()
1391 * scmi_set_protocol_priv - Set protocol specific data at init time
1393 * @ph: A reference to the protocol handle.
1403 pi->priv = priv; in scmi_set_protocol_priv()
1409 * scmi_get_protocol_priv - Set protocol specific data at init time
1411 * @ph: A reference to the protocol handle.
1413 * Return: Protocol private data if any was set.
1419 return pi->priv; in scmi_get_protocol_priv()
1437 * scmi_common_extended_name_get - Common helper to get extended resources name
1438 * @ph: A protocol handle reference.
1439 * @cmd_id: The specific command ID to use.
1440 * @res_id: The specific resource ID to use.
1455 ret = ph->xops->xfer_get_init(ph, cmd_id, sizeof(res_id), in scmi_common_extended_name_get()
1460 put_unaligned_le32(res_id, t->tx.buf); in scmi_common_extended_name_get()
1461 resp = t->rx.buf; in scmi_common_extended_name_get()
1463 ret = ph->xops->do_xfer(ph, t); in scmi_common_extended_name_get()
1465 strscpy(name, resp->name, len); in scmi_common_extended_name_get()
1467 ph->xops->xfer_put(ph, t); in scmi_common_extended_name_get()
1470 dev_warn(ph->dev, in scmi_common_extended_name_get()
1471 "Failed to get extended name - id:%u (ret:%d). Using %s\n", in scmi_common_extended_name_get()
1477 * struct scmi_iterator - Iterator descriptor
1479 * a proper custom command payload for each multi-part command request.
1481 * @process_response to parse the multi-part replies.
1484 * @ph: A reference to the associated protocol handle to be used.
1487 * internal routines and by the caller-provided @scmi_iterator_ops.
1509 i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL); in scmi_iterator_init()
1511 return ERR_PTR(-ENOMEM); in scmi_iterator_init()
1513 i->ph = ph; in scmi_iterator_init()
1514 i->ops = ops; in scmi_iterator_init()
1515 i->priv = priv; in scmi_iterator_init()
1517 ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t); in scmi_iterator_init()
1519 devm_kfree(ph->dev, i); in scmi_iterator_init()
1523 i->state.max_resources = max_resources; in scmi_iterator_init()
1524 i->msg = i->t->tx.buf; in scmi_iterator_init()
1525 i->resp = i->t->rx.buf; in scmi_iterator_init()
1532 int ret = -EINVAL; in scmi_iterator_run()
1538 if (!i || !i->ops || !i->ph) in scmi_iterator_run()
1541 iops = i->ops; in scmi_iterator_run()
1542 ph = i->ph; in scmi_iterator_run()
1543 st = &i->state; in scmi_iterator_run()
1546 iops->prepare_message(i->msg, st->desc_index, i->priv); in scmi_iterator_run()
1547 ret = ph->xops->do_xfer(ph, i->t); in scmi_iterator_run()
1551 st->rx_len = i->t->rx.len; in scmi_iterator_run()
1552 ret = iops->update_state(st, i->resp, i->priv); in scmi_iterator_run()
1556 if (st->num_returned > st->max_resources - st->desc_index) { in scmi_iterator_run()
1557 dev_err(ph->dev, in scmi_iterator_run()
1559 st->max_resources); in scmi_iterator_run()
1560 ret = -EINVAL; in scmi_iterator_run()
1564 for (st->loop_idx = 0; st->loop_idx < st->num_returned; in scmi_iterator_run()
1565 st->loop_idx++) { in scmi_iterator_run()
1566 ret = iops->process_response(ph, i->resp, st, i->priv); in scmi_iterator_run()
1571 st->desc_index += st->num_returned; in scmi_iterator_run()
1572 ph->xops->reset_rx_to_maxsz(ph, i->t); in scmi_iterator_run()
1577 } while (st->num_returned && st->num_remaining); in scmi_iterator_run()
1581 ph->xops->xfer_put(ph, i->t); in scmi_iterator_run()
1582 devm_kfree(ph->dev, i); in scmi_iterator_run()
1626 ret = -EINVAL; in scmi_common_fastchannel_init()
1630 ret = ph->xops->xfer_get_init(ph, describe_id, in scmi_common_fastchannel_init()
1635 info = t->tx.buf; in scmi_common_fastchannel_init()
1636 info->domain = cpu_to_le32(domain); in scmi_common_fastchannel_init()
1637 info->message_id = cpu_to_le32(message_id); in scmi_common_fastchannel_init()
1644 ret = ph->xops->do_xfer(ph, t); in scmi_common_fastchannel_init()
1648 resp = t->rx.buf; in scmi_common_fastchannel_init()
1649 flags = le32_to_cpu(resp->attr); in scmi_common_fastchannel_init()
1650 size = le32_to_cpu(resp->chan_size); in scmi_common_fastchannel_init()
1652 ret = -EINVAL; in scmi_common_fastchannel_init()
1656 phys_addr = le32_to_cpu(resp->chan_addr_low); in scmi_common_fastchannel_init()
1657 phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32; in scmi_common_fastchannel_init()
1658 addr = devm_ioremap(ph->dev, phys_addr, size); in scmi_common_fastchannel_init()
1660 ret = -EADDRNOTAVAIL; in scmi_common_fastchannel_init()
1667 db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL); in scmi_common_fastchannel_init()
1669 ret = -ENOMEM; in scmi_common_fastchannel_init()
1674 phys_addr = le32_to_cpu(resp->db_addr_low); in scmi_common_fastchannel_init()
1675 phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32; in scmi_common_fastchannel_init()
1676 addr = devm_ioremap(ph->dev, phys_addr, size); in scmi_common_fastchannel_init()
1678 ret = -EADDRNOTAVAIL; in scmi_common_fastchannel_init()
1682 db->addr = addr; in scmi_common_fastchannel_init()
1683 db->width = size; in scmi_common_fastchannel_init()
1684 db->set = le32_to_cpu(resp->db_set_lmask); in scmi_common_fastchannel_init()
1685 db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32; in scmi_common_fastchannel_init()
1686 db->mask = le32_to_cpu(resp->db_preserve_lmask); in scmi_common_fastchannel_init()
1687 db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32; in scmi_common_fastchannel_init()
1692 ph->xops->xfer_put(ph, t); in scmi_common_fastchannel_init()
1694 dev_dbg(ph->dev, in scmi_common_fastchannel_init()
1695 "Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n", in scmi_common_fastchannel_init()
1696 pi->proto->id, message_id, domain); in scmi_common_fastchannel_init()
1701 devm_kfree(ph->dev, db); in scmi_common_fastchannel_init()
1707 ph->xops->xfer_put(ph, t); in scmi_common_fastchannel_init()
1710 dev_warn(ph->dev, in scmi_common_fastchannel_init()
1711 "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n", in scmi_common_fastchannel_init()
1712 pi->proto->id, message_id, domain, ret); in scmi_common_fastchannel_init()
1719 if (db->mask) \
1720 val = ioread##w(db->addr) & db->mask; \
1721 iowrite##w((u##w)db->set | val, db->addr); \
1726 if (!db || !db->addr) in scmi_common_fastchannel_db_ring()
1729 if (db->width == 1) in scmi_common_fastchannel_db_ring()
1731 else if (db->width == 2) in scmi_common_fastchannel_db_ring()
1733 else if (db->width == 4) in scmi_common_fastchannel_db_ring()
1735 else /* db->width == 8 */ in scmi_common_fastchannel_db_ring()
1742 if (db->mask) in scmi_common_fastchannel_db_ring()
1743 val = ioread64_hi_lo(db->addr) & db->mask; in scmi_common_fastchannel_db_ring()
1744 iowrite64_hi_lo(db->set | val, db->addr); in scmi_common_fastchannel_db_ring()
1758 * scmi_revision_area_get - Retrieve version memory area.
1760 * @ph: A reference to the protocol handle.
1762 * A helper to grab the version memory area reference during SCMI Base protocol
1766 * instance underlying this protocol handle.
1773 return pi->handle->version; in scmi_revision_area_get()
1777 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
1780 * @proto: The protocol descriptor.
1782 * Allocate a new protocol instance descriptor, using the provided @proto
1784 * all resources management is handled via a dedicated per-protocol devres
1788 * Return: A reference to a freshly allocated and initialized protocol instance
1796 int ret = -ENOMEM; in scmi_alloc_init_protocol_instance()
1799 const struct scmi_handle *handle = &info->handle; in scmi_alloc_init_protocol_instance()
1801 /* Protocol specific devres group */ in scmi_alloc_init_protocol_instance()
1802 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL); in scmi_alloc_init_protocol_instance()
1804 scmi_protocol_put(proto->id); in scmi_alloc_init_protocol_instance()
1808 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL); in scmi_alloc_init_protocol_instance()
1812 pi->gid = gid; in scmi_alloc_init_protocol_instance()
1813 pi->proto = proto; in scmi_alloc_init_protocol_instance()
1814 pi->handle = handle; in scmi_alloc_init_protocol_instance()
1815 pi->ph.dev = handle->dev; in scmi_alloc_init_protocol_instance()
1816 pi->ph.xops = &xfer_ops; in scmi_alloc_init_protocol_instance()
1817 pi->ph.hops = &helpers_ops; in scmi_alloc_init_protocol_instance()
1818 pi->ph.set_priv = scmi_set_protocol_priv; in scmi_alloc_init_protocol_instance()
1819 pi->ph.get_priv = scmi_get_protocol_priv; in scmi_alloc_init_protocol_instance()
1820 refcount_set(&pi->users, 1); in scmi_alloc_init_protocol_instance()
1821 /* proto->init is assured NON NULL by scmi_protocol_register */ in scmi_alloc_init_protocol_instance()
1822 ret = pi->proto->instance_init(&pi->ph); in scmi_alloc_init_protocol_instance()
1826 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1, in scmi_alloc_init_protocol_instance()
1828 if (ret != proto->id) in scmi_alloc_init_protocol_instance()
1835 if (pi->proto->events) { in scmi_alloc_init_protocol_instance()
1836 ret = scmi_register_protocol_events(handle, pi->proto->id, in scmi_alloc_init_protocol_instance()
1837 &pi->ph, in scmi_alloc_init_protocol_instance()
1838 pi->proto->events); in scmi_alloc_init_protocol_instance()
1840 dev_warn(handle->dev, in scmi_alloc_init_protocol_instance()
1841 "Protocol:%X - Events Registration Failed - err:%d\n", in scmi_alloc_init_protocol_instance()
1842 pi->proto->id, ret); in scmi_alloc_init_protocol_instance()
1845 devres_close_group(handle->dev, pi->gid); in scmi_alloc_init_protocol_instance()
1846 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id); in scmi_alloc_init_protocol_instance()
1851 /* Take care to put the protocol module's owner before releasing all */ in scmi_alloc_init_protocol_instance()
1852 scmi_protocol_put(proto->id); in scmi_alloc_init_protocol_instance()
1853 devres_release_group(handle->dev, gid); in scmi_alloc_init_protocol_instance()
1859 * scmi_get_protocol_instance - Protocol initialization helper.
1861 * @protocol_id: The protocol being requested.
1863 * In case the required protocol has never been requested before for this
1865 * resource allocation with a dedicated per-protocol devres subgroup.
1867 * Return: A reference to an initialized protocol instance or error on failure:
1868 * in particular returns -EPROBE_DEFER when the desired protocol could
1877 mutex_lock(&info->protocols_mtx); in scmi_get_protocol_instance()
1878 pi = idr_find(&info->protocols, protocol_id); in scmi_get_protocol_instance()
1881 refcount_inc(&pi->users); in scmi_get_protocol_instance()
1885 /* Fails if protocol not registered on bus */ in scmi_get_protocol_instance()
1890 pi = ERR_PTR(-EPROBE_DEFER); in scmi_get_protocol_instance()
1892 mutex_unlock(&info->protocols_mtx); in scmi_get_protocol_instance()
1898 * scmi_protocol_acquire - Protocol acquire
1900 * @protocol_id: The protocol being requested.
1902 * Register a new user for the requested protocol on the specified SCMI
1905 * Return: 0 if protocol was acquired successfully.
1913 * scmi_protocol_release - Protocol de-initialization helper.
1915 * @protocol_id: The protocol being requested.
1917 * Remove one user for the specified protocol and triggers de-initialization
1918 * and resources de-allocation once the last user has gone.
1925 mutex_lock(&info->protocols_mtx); in scmi_protocol_release()
1926 pi = idr_find(&info->protocols, protocol_id); in scmi_protocol_release()
1930 if (refcount_dec_and_test(&pi->users)) { in scmi_protocol_release()
1931 void *gid = pi->gid; in scmi_protocol_release()
1933 if (pi->proto->events) in scmi_protocol_release()
1936 if (pi->proto->instance_deinit) in scmi_protocol_release()
1937 pi->proto->instance_deinit(&pi->ph); in scmi_protocol_release()
1939 idr_remove(&info->protocols, protocol_id); in scmi_protocol_release()
1943 devres_release_group(handle->dev, gid); in scmi_protocol_release()
1944 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n", in scmi_protocol_release()
1949 mutex_unlock(&info->protocols_mtx); in scmi_protocol_release()
1956 struct scmi_info *info = handle_to_scmi_info(pi->handle); in scmi_setup_protocol_implemented()
1958 info->protocols_imp = prot_imp; in scmi_setup_protocol_implemented()
1966 struct scmi_revision_info *rev = handle->version; in scmi_is_protocol_implemented()
1968 if (!info->protocols_imp) in scmi_is_protocol_implemented()
1971 for (i = 0; i < rev->num_protocols; i++) in scmi_is_protocol_implemented()
1972 if (info->protocols_imp[i] == prot_id) in scmi_is_protocol_implemented()
1986 scmi_protocol_release(dres->handle, dres->protocol_id); in scmi_devm_release_protocol()
1998 return ERR_PTR(-ENOMEM); in scmi_devres_protocol_instance_get()
2000 pi = scmi_get_protocol_instance(sdev->handle, protocol_id); in scmi_devres_protocol_instance_get()
2006 dres->handle = sdev->handle; in scmi_devres_protocol_instance_get()
2007 dres->protocol_id = protocol_id; in scmi_devres_protocol_instance_get()
2008 devres_add(&sdev->dev, dres); in scmi_devres_protocol_instance_get()
2014 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
2017 * @protocol_id: The protocol being requested.
2018 * @ph: A pointer reference used to pass back the associated protocol handle.
2020 * Get hold of a protocol accounting for its usage, eventually triggering its
2021 * initialization, and returning the protocol specific operations and related
2022 * protocol handle which will be used as first argument in most of the
2024 * Being a devres based managed method, protocol hold will be automatically
2025 * released, and possibly de-initialized on last user, once the SCMI driver
2028 * Return: A reference to the requested protocol operations or error.
2038 return ERR_PTR(-EINVAL); in scmi_devm_protocol_get()
2044 *ph = &pi->ph; in scmi_devm_protocol_get()
2046 return pi->proto->ops; in scmi_devm_protocol_get()
2050 * scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol
2053 * @protocol_id: The protocol being requested.
2055 * Get hold of a protocol accounting for its usage, possibly triggering its
2056 * initialization but without getting access to its protocol specific operations
2059 * Being a devres based managed method, protocol hold will be automatically
2060 * released, and possibly de-initialized on last user, once the SCMI driver
2084 return dres->protocol_id == *((u8 *)data); in scmi_devm_protocol_match()
2088 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
2091 * @protocol_id: The protocol being requested.
2093 * Explicitly release a protocol hold previously obtained calling the above
2100 ret = devres_release(&sdev->dev, scmi_devm_release_protocol, in scmi_devm_protocol_put()
2106 * scmi_is_transport_atomic - Method to check if underlying transport for an
2121 ret = info->desc->atomic_enabled && in scmi_is_transport_atomic()
2122 is_transport_polling_capable(info->desc); in scmi_is_transport_atomic()
2124 *atomic_threshold = info->atomic_threshold; in scmi_is_transport_atomic()
2130 * scmi_handle_get() - Get the SCMI handle for a device
2135 * and is expected to be maintained by caller of SCMI protocol library.
2149 if (dev->parent == info->dev) { in scmi_handle_get()
2150 info->users++; in scmi_handle_get()
2151 handle = &info->handle; in scmi_handle_get()
2161 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
2166 * and is expected to be maintained by caller of SCMI protocol library.
2170 * if null was passed, it returns -EINVAL;
2177 return -EINVAL; in scmi_handle_put()
2181 if (!WARN_ON(!info->users)) in scmi_handle_put()
2182 info->users--; in scmi_handle_put()
2200 scmi_dev->handle = scmi_handle_get(&scmi_dev->dev); in scmi_set_handle()
2201 if (scmi_dev->handle) in scmi_set_handle()
2202 scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev); in scmi_set_handle()
2210 struct device *dev = sinfo->dev; in __scmi_xfer_info_init()
2211 const struct scmi_desc *desc = sinfo->desc; in __scmi_xfer_info_init()
2213 /* Pre-allocated messages, no more than what hdr.seq can support */ in __scmi_xfer_info_init()
2214 if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) { in __scmi_xfer_info_init()
2216 "Invalid maximum messages %d, not in range [1 - %lu]\n", in __scmi_xfer_info_init()
2217 info->max_msg, MSG_TOKEN_MAX); in __scmi_xfer_info_init()
2218 return -EINVAL; in __scmi_xfer_info_init()
2221 hash_init(info->pending_xfers); in __scmi_xfer_info_init()
2224 info->xfer_alloc_table = devm_bitmap_zalloc(dev, MSG_TOKEN_MAX, in __scmi_xfer_info_init()
2226 if (!info->xfer_alloc_table) in __scmi_xfer_info_init()
2227 return -ENOMEM; in __scmi_xfer_info_init()
2231 * pre-initialize the buffer pointer to pre-allocated buffers and in __scmi_xfer_info_init()
2234 INIT_HLIST_HEAD(&info->free_xfers); in __scmi_xfer_info_init()
2235 for (i = 0; i < info->max_msg; i++) { in __scmi_xfer_info_init()
2238 return -ENOMEM; in __scmi_xfer_info_init()
2240 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size, in __scmi_xfer_info_init()
2242 if (!xfer->rx.buf) in __scmi_xfer_info_init()
2243 return -ENOMEM; in __scmi_xfer_info_init()
2245 xfer->tx.buf = xfer->rx.buf; in __scmi_xfer_info_init()
2246 init_completion(&xfer->done); in __scmi_xfer_info_init()
2247 spin_lock_init(&xfer->lock); in __scmi_xfer_info_init()
2250 hlist_add_head(&xfer->node, &info->free_xfers); in __scmi_xfer_info_init()
2253 spin_lock_init(&info->xfer_lock); in __scmi_xfer_info_init()
2260 const struct scmi_desc *desc = sinfo->desc; in scmi_channels_max_msg_configure()
2262 if (!desc->ops->get_max_msg) { in scmi_channels_max_msg_configure()
2263 sinfo->tx_minfo.max_msg = desc->max_msg; in scmi_channels_max_msg_configure()
2264 sinfo->rx_minfo.max_msg = desc->max_msg; in scmi_channels_max_msg_configure()
2268 base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE); in scmi_channels_max_msg_configure()
2270 return -EINVAL; in scmi_channels_max_msg_configure()
2271 sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo); in scmi_channels_max_msg_configure()
2274 base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE); in scmi_channels_max_msg_configure()
2276 sinfo->rx_minfo.max_msg = in scmi_channels_max_msg_configure()
2277 desc->ops->get_max_msg(base_cinfo); in scmi_channels_max_msg_configure()
2291 ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo); in scmi_xfer_info_init()
2292 if (!ret && !idr_is_empty(&sinfo->rx_idr)) in scmi_xfer_info_init()
2293 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo); in scmi_xfer_info_init()
2309 idr = tx ? &info->tx_idr : &info->rx_idr; in scmi_chan_setup()
2311 if (!info->desc->ops->chan_available(of_node, idx)) { in scmi_chan_setup()
2314 return -EINVAL; in scmi_chan_setup()
2318 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL); in scmi_chan_setup()
2320 return -ENOMEM; in scmi_chan_setup()
2322 cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms; in scmi_chan_setup()
2328 tdev = scmi_device_create(of_node, info->dev, prot_id, name); in scmi_chan_setup()
2330 dev_err(info->dev, in scmi_chan_setup()
2332 devm_kfree(info->dev, cinfo); in scmi_chan_setup()
2333 return -EINVAL; in scmi_chan_setup()
2337 cinfo->id = prot_id; in scmi_chan_setup()
2338 cinfo->dev = &tdev->dev; in scmi_chan_setup()
2339 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx); in scmi_chan_setup()
2342 scmi_device_destroy(info->dev, prot_id, name); in scmi_chan_setup()
2343 devm_kfree(info->dev, cinfo); in scmi_chan_setup()
2347 if (tx && is_polling_required(cinfo, info->desc)) { in scmi_chan_setup()
2348 if (is_transport_polling_capable(info->desc)) in scmi_chan_setup()
2349 dev_info(&tdev->dev, in scmi_chan_setup()
2350 "Enabled polling mode TX channel - prot_id:%d\n", in scmi_chan_setup()
2353 dev_warn(&tdev->dev, in scmi_chan_setup()
2360 dev_err(info->dev, in scmi_chan_setup()
2365 scmi_device_destroy(info->dev, prot_id, name); in scmi_chan_setup()
2366 devm_kfree(info->dev, cinfo); in scmi_chan_setup()
2371 cinfo->handle = &info->handle; in scmi_chan_setup()
2384 if (ret && ret != -ENOMEM) in scmi_txrx_setup()
2392 * scmi_channels_setup - Helper to initialize all required channels
2403 * protocol defined in the DT, a distinct freshly initialized channel is
2404 * created only if the DT node for the protocol at hand describes a dedicated
2405 * channel: in all the other cases the common BASE protocol channel is reused.
2412 struct device_node *child, *top_np = info->dev->of_node; in scmi_channels_setup()
2426 dev_err(info->dev, in scmi_channels_setup()
2427 "Out of range protocol %d\n", prot_id); in scmi_channels_setup()
2439 static int scmi_chan_destroy(int id, void *p, void *idr) in scmi_chan_destroy() argument
2443 if (cinfo->dev) { in scmi_chan_destroy()
2444 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_chan_destroy()
2445 struct scmi_device *sdev = to_scmi_dev(cinfo->dev); in scmi_chan_destroy()
2447 of_node_put(cinfo->dev->of_node); in scmi_chan_destroy()
2448 scmi_device_destroy(info->dev, id, sdev->name); in scmi_chan_destroy()
2449 cinfo->dev = NULL; in scmi_chan_destroy()
2452 idr_remove(idr, id); in scmi_chan_destroy()
2460 idr_for_each(idr, info->desc->ops->chan_free, idr); in scmi_cleanup_channels()
2470 scmi_cleanup_channels(info, &info->tx_idr); in scmi_cleanup_txrx_channels()
2472 scmi_cleanup_channels(info, &info->rx_idr); in scmi_cleanup_txrx_channels()
2482 if (!strncmp(sdev->name, "__scmi_transport_device", 23) || in scmi_bus_notifier()
2483 sdev->dev.parent != info->dev) in scmi_bus_notifier()
2492 scmi_handle_put(sdev->handle); in scmi_bus_notifier()
2493 sdev->handle = NULL; in scmi_bus_notifier()
2499 dev_dbg(info->dev, "Device %s (%s) is now %s\n", dev_name(&sdev->dev), in scmi_bus_notifier()
2500 sdev->name, action == BUS_NOTIFY_BIND_DRIVER ? in scmi_bus_notifier()
2513 np = idr_find(&info->active_protocols, id_table->protocol_id); in scmi_device_request_notifier()
2517 dev_dbg(info->dev, "%sRequested device (%s) for protocol 0x%x\n", in scmi_device_request_notifier()
2518 action == SCMI_BUS_NOTIFY_DEVICE_REQUEST ? "" : "UN-", in scmi_device_request_notifier()
2519 id_table->name, id_table->protocol_id); in scmi_device_request_notifier()
2523 scmi_create_protocol_devices(np, info, id_table->protocol_id, in scmi_device_request_notifier()
2524 id_table->name); in scmi_device_request_notifier()
2527 scmi_destroy_protocol_devices(info, id_table->protocol_id, in scmi_device_request_notifier()
2528 id_table->name); in scmi_device_request_notifier()
2544 debugfs_remove_recursive(dbg->top_dentry); in scmi_debugfs_common_cleanup()
2545 kfree(dbg->name); in scmi_debugfs_common_cleanup()
2546 kfree(dbg->type); in scmi_debugfs_common_cleanup()
2556 dbg = devm_kzalloc(info->dev, sizeof(*dbg), GFP_KERNEL); in scmi_debugfs_common_setup()
2560 dbg->name = kstrdup(of_node_full_name(info->dev->of_node), GFP_KERNEL); in scmi_debugfs_common_setup()
2561 if (!dbg->name) { in scmi_debugfs_common_setup()
2562 devm_kfree(info->dev, dbg); in scmi_debugfs_common_setup()
2566 of_property_read_string(info->dev->of_node, "compatible", &c_ptr); in scmi_debugfs_common_setup()
2567 dbg->type = kstrdup(c_ptr, GFP_KERNEL); in scmi_debugfs_common_setup()
2568 if (!dbg->type) { in scmi_debugfs_common_setup()
2569 kfree(dbg->name); in scmi_debugfs_common_setup()
2570 devm_kfree(info->dev, dbg); in scmi_debugfs_common_setup()
2574 snprintf(top_dir, 16, "%d", info->id); in scmi_debugfs_common_setup()
2578 dbg->is_atomic = info->desc->atomic_enabled && in scmi_debugfs_common_setup()
2579 is_transport_polling_capable(info->desc); in scmi_debugfs_common_setup()
2582 (char **)&dbg->name); in scmi_debugfs_common_setup()
2585 &info->atomic_threshold); in scmi_debugfs_common_setup()
2587 debugfs_create_str("type", 0400, trans, (char **)&dbg->type); in scmi_debugfs_common_setup()
2589 debugfs_create_bool("is_atomic", 0400, trans, &dbg->is_atomic); in scmi_debugfs_common_setup()
2592 (u32 *)&info->desc->max_rx_timeout_ms); in scmi_debugfs_common_setup()
2595 (u32 *)&info->desc->max_msg_size); in scmi_debugfs_common_setup()
2598 (u32 *)&info->tx_minfo.max_msg); in scmi_debugfs_common_setup()
2601 (u32 *)&info->rx_minfo.max_msg); in scmi_debugfs_common_setup()
2603 dbg->top_dentry = top_dentry; in scmi_debugfs_common_setup()
2605 if (devm_add_action_or_reset(info->dev, in scmi_debugfs_common_setup()
2616 int id, num_chans = 0, ret = 0; in scmi_debugfs_raw_mode_setup() local
2621 if (!info->dbg) in scmi_debugfs_raw_mode_setup()
2622 return -EINVAL; in scmi_debugfs_raw_mode_setup()
2625 idr_for_each_entry(&info->tx_idr, cinfo, id) { in scmi_debugfs_raw_mode_setup()
2631 dev_warn(info->dev, in scmi_debugfs_raw_mode_setup()
2632 "SCMI RAW - Error enumerating channels\n"); in scmi_debugfs_raw_mode_setup()
2636 if (!test_bit(cinfo->id, protos)) { in scmi_debugfs_raw_mode_setup()
2637 channels[num_chans++] = cinfo->id; in scmi_debugfs_raw_mode_setup()
2638 set_bit(cinfo->id, protos); in scmi_debugfs_raw_mode_setup()
2642 info->raw = scmi_raw_mode_init(&info->handle, info->dbg->top_dentry, in scmi_debugfs_raw_mode_setup()
2643 info->id, channels, num_chans, in scmi_debugfs_raw_mode_setup()
2644 info->desc, info->tx_minfo.max_msg); in scmi_debugfs_raw_mode_setup()
2645 if (IS_ERR(info->raw)) { in scmi_debugfs_raw_mode_setup()
2646 dev_err(info->dev, "Failed to initialize SCMI RAW Mode !\n"); in scmi_debugfs_raw_mode_setup()
2647 ret = PTR_ERR(info->raw); in scmi_debugfs_raw_mode_setup()
2648 info->raw = NULL; in scmi_debugfs_raw_mode_setup()
2661 struct device *dev = &pdev->dev; in scmi_probe()
2662 struct device_node *child, *np = dev->of_node; in scmi_probe()
2666 return -EINVAL; in scmi_probe()
2670 return -ENOMEM; in scmi_probe()
2672 info->id = ida_alloc_min(&scmi_id, 0, GFP_KERNEL); in scmi_probe()
2673 if (info->id < 0) in scmi_probe()
2674 return info->id; in scmi_probe()
2676 info->dev = dev; in scmi_probe()
2677 info->desc = desc; in scmi_probe()
2678 info->bus_nb.notifier_call = scmi_bus_notifier; in scmi_probe()
2679 info->dev_req_nb.notifier_call = scmi_device_request_notifier; in scmi_probe()
2680 INIT_LIST_HEAD(&info->node); in scmi_probe()
2681 idr_init(&info->protocols); in scmi_probe()
2682 mutex_init(&info->protocols_mtx); in scmi_probe()
2683 idr_init(&info->active_protocols); in scmi_probe()
2684 mutex_init(&info->devreq_mtx); in scmi_probe()
2687 idr_init(&info->tx_idr); in scmi_probe()
2688 idr_init(&info->rx_idr); in scmi_probe()
2690 handle = &info->handle; in scmi_probe()
2691 handle->dev = info->dev; in scmi_probe()
2692 handle->version = &info->version; in scmi_probe()
2693 handle->devm_protocol_acquire = scmi_devm_protocol_acquire; in scmi_probe()
2694 handle->devm_protocol_get = scmi_devm_protocol_get; in scmi_probe()
2695 handle->devm_protocol_put = scmi_devm_protocol_put; in scmi_probe()
2698 if (!of_property_read_u32(np, "atomic-threshold-us", in scmi_probe()
2699 &info->atomic_threshold)) in scmi_probe()
2702 info->atomic_threshold); in scmi_probe()
2703 handle->is_transport_atomic = scmi_is_transport_atomic; in scmi_probe()
2705 if (desc->ops->link_supplier) { in scmi_probe()
2706 ret = desc->ops->link_supplier(dev); in scmi_probe()
2716 ret = bus_register_notifier(&scmi_bus_type, &info->bus_nb); in scmi_probe()
2721 &info->dev_req_nb); in scmi_probe()
2730 info->dbg = scmi_debugfs_common_setup(info); in scmi_probe()
2731 if (!info->dbg) in scmi_probe()
2752 if (info->desc->atomic_enabled && in scmi_probe()
2753 !is_transport_polling_capable(info->desc)) in scmi_probe()
2758 * Trigger SCMI Base protocol initialization. in scmi_probe()
2771 list_add_tail(&info->node, &scmi_list); in scmi_probe()
2781 dev_err(dev, "Out of range protocol %d\n", prot_id); in scmi_probe()
2784 dev_err(dev, "SCMI protocol %d not implemented\n", in scmi_probe()
2790 * Save this valid DT protocol descriptor amongst in scmi_probe()
2793 ret = idr_alloc(&info->active_protocols, child, in scmi_probe()
2796 dev_err(dev, "SCMI protocol %d already activated. Skip\n", in scmi_probe()
2809 scmi_raw_mode_cleanup(info->raw); in scmi_probe()
2810 scmi_notification_exit(&info->handle); in scmi_probe()
2813 &info->dev_req_nb); in scmi_probe()
2815 bus_unregister_notifier(&scmi_bus_type, &info->bus_nb); in scmi_probe()
2819 ida_free(&scmi_id, info->id); in scmi_probe()
2825 int id; in scmi_remove() local
2830 scmi_raw_mode_cleanup(info->raw); in scmi_remove()
2833 if (info->users) in scmi_remove()
2834 dev_warn(&pdev->dev, in scmi_remove()
2836 list_del(&info->node); in scmi_remove()
2839 scmi_notification_exit(&info->handle); in scmi_remove()
2841 mutex_lock(&info->protocols_mtx); in scmi_remove()
2842 idr_destroy(&info->protocols); in scmi_remove()
2843 mutex_unlock(&info->protocols_mtx); in scmi_remove()
2845 idr_for_each_entry(&info->active_protocols, child, id) in scmi_remove()
2847 idr_destroy(&info->active_protocols); in scmi_remove()
2850 &info->dev_req_nb); in scmi_remove()
2851 bus_unregister_notifier(&scmi_bus_type, &info->bus_nb); in scmi_remove()
2856 ida_free(&scmi_id, info->id); in scmi_remove()
2866 return sprintf(buf, "%u.%u\n", info->version.major_ver, in protocol_version_show()
2867 info->version.minor_ver); in protocol_version_show()
2876 return sprintf(buf, "0x%x\n", info->version.impl_ver); in firmware_version_show()
2885 return sprintf(buf, "%s\n", info->version.vendor_id); in vendor_id_show()
2894 return sprintf(buf, "%s\n", info->version.sub_vendor_id); in sub_vendor_id_show()
2913 { .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc },
2916 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
2917 { .compatible = "arm,scmi-smc-param", .data = &scmi_smc_desc},
2920 { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
2929 .name = "arm-scmi",
2939 * __scmi_transports_setup - Common helper to call transport-specific
2954 for (trans = scmi_of_match; trans->data; trans++) { in __scmi_transports_setup()
2955 const struct scmi_desc *tdesc = trans->data; in __scmi_transports_setup()
2957 if ((init && !tdesc->transport_init) || in __scmi_transports_setup()
2958 (!init && !tdesc->transport_exit)) in __scmi_transports_setup()
2962 ret = tdesc->transport_init(); in __scmi_transports_setup()
2964 tdesc->transport_exit(); in __scmi_transports_setup()
2968 trans->compatible); in __scmi_transports_setup()
3005 return -EINVAL; in scmi_driver_init()
3007 /* Initialize any compiled-in transport which provided an init/exit */ in scmi_driver_init()
3051 MODULE_ALIAS("platform:arm-scmi");
3053 MODULE_DESCRIPTION("ARM SCMI protocol driver");