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 * @version: Protocol version supported by the platform as detected at runtime.
89 * @ph: An embedded protocol handle that will be passed down to protocol
92 * Each protocol is initialized independently once for each SCMI platform in
108 * struct scmi_debug_info - Debug common info
122 * struct scmi_info - Structure representing a SCMI instance
124 * @id: A sequence number starting from zero identifying this instance
127 * @version: SCMI revision information containing protocol version,
128 * implementation version and (sub-)vendor identification.
132 * @tx_idr: IDR object to map protocol id to Tx channel info pointer
133 * @rx_idr: IDR object to map protocol id to Rx channel info pointer
135 * this SCMI instance: populated on protocol's first attempted
140 * base protocol
143 * @atomic_threshold: Optional system wide DT-configured threshold, expressed
146 * to have an execution latency lesser-equal to the threshold
160 int id; member
195 if (!proto || !try_module_get(proto->owner)) { in scmi_protocol_get()
196 pr_warn("SCMI Protocol 0x%x not found!\n", protocol_id); in scmi_protocol_get()
200 pr_debug("Found SCMI Protocol 0x%x\n", protocol_id); in scmi_protocol_get()
211 module_put(proto->owner); in scmi_protocol_put()
219 pr_err("invalid protocol\n"); in scmi_protocol_register()
220 return -EINVAL; in scmi_protocol_register()
223 if (!proto->instance_init) { in scmi_protocol_register()
224 pr_err("missing init for protocol 0x%x\n", proto->id); in scmi_protocol_register()
225 return -EINVAL; in scmi_protocol_register()
230 proto->id, proto->id + 1, GFP_ATOMIC); in scmi_protocol_register()
232 if (ret != proto->id) { in scmi_protocol_register()
233 pr_err("unable to allocate SCMI idr slot for 0x%x - err %d\n", in scmi_protocol_register()
234 proto->id, ret); in scmi_protocol_register()
238 pr_debug("Registered SCMI Protocol 0x%x\n", proto->id); in scmi_protocol_register()
247 idr_remove(&scmi_protocols, proto->id); in scmi_protocol_unregister()
250 pr_debug("Unregistered SCMI Protocol 0x%x\n", proto->id); in scmi_protocol_unregister()
255 * scmi_create_protocol_devices - Create devices for all pending requests for
258 * @np: The device node describing the protocol
260 * @prot_id: The protocol ID
263 * for the specified protocol.
271 mutex_lock(&info->devreq_mtx); in scmi_create_protocol_devices()
272 sdev = scmi_device_create(np, info->dev, prot_id, name); in scmi_create_protocol_devices()
274 dev_err(info->dev, in scmi_create_protocol_devices()
275 "failed to create device for protocol 0x%X (%s)\n", in scmi_create_protocol_devices()
277 mutex_unlock(&info->devreq_mtx); in scmi_create_protocol_devices()
283 mutex_lock(&info->devreq_mtx); in scmi_destroy_protocol_devices()
284 scmi_device_destroy(info->dev, prot_id, name); in scmi_destroy_protocol_devices()
285 mutex_unlock(&info->devreq_mtx); in scmi_destroy_protocol_devices()
293 info->notify_priv = priv; in scmi_notification_instance_data_set()
294 /* Ensure updated protocol private date are visible */ in scmi_notification_instance_data_set()
304 return info->notify_priv; in scmi_notification_instance_data_get()
308 * scmi_xfer_token_set - Reserve and set new token for the xfer at hand
314 * xfer->hdr.seq: picking a monotonically increasing value avoids immediate
315 * reuse of freshly completed or timed-out xfers, thus mitigating the risk
316 * of incorrect association of a late and expired xfer with a live in-flight
317 * transaction, both happening to re-use the same token identifier.
319 * Since platform is NOT required to answer our request in-order we should
322 * - exactly 'next_token' may be NOT available so pick xfer_id >= next_token
325 * - all tokens ahead upto (MSG_TOKEN_ID_MASK - 1) are used in-flight but we
329 * X = used in-flight
332 * ------
334 * |- xfer_id picked
335 * -----------+----------------------------------------------------------
337 * ----------------------------------------------------------------------
339 * |- next_token
341 * Out-of-order pending at start
342 * -----------------------------
344 * |- xfer_id picked, last_token fixed
345 * -----+----------------------------------------------------------------
347 * ----------------------------------------------------------------------
349 * |- next_token
352 * Out-of-order pending at end
353 * ---------------------------
355 * |- xfer_id picked, last_token fixed
356 * -----+----------------------------------------------------------------
358 * ----------------------------------------------------------------------
360 * |- next_token
372 * Pick a candidate monotonic token in range [0, MSG_TOKEN_MAX - 1] in scmi_xfer_token_set()
373 * using the pre-allocated transfer_id as a base. in scmi_xfer_token_set()
379 next_token = (xfer->transfer_id & (MSG_TOKEN_MAX - 1)); in scmi_xfer_token_set()
382 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, in scmi_xfer_token_set()
386 * After heavily out-of-order responses, there are no free in scmi_xfer_token_set()
390 xfer_id = find_next_zero_bit(minfo->xfer_alloc_table, in scmi_xfer_token_set()
394 * maximum number of (MSG_TOKEN_MAX - 1) in-flight messages in scmi_xfer_token_set()
395 * but we have not found any free token [0, MSG_TOKEN_MAX - 1]. in scmi_xfer_token_set()
398 return -ENOMEM; in scmi_xfer_token_set()
401 /* Update +/- last_token accordingly if we skipped some hole */ in scmi_xfer_token_set()
403 atomic_add((int)(xfer_id - next_token), &transfer_last_id); in scmi_xfer_token_set()
405 xfer->hdr.seq = (u16)xfer_id; in scmi_xfer_token_set()
411 * scmi_xfer_token_clear - Release the token
419 clear_bit(xfer->hdr.seq, minfo->xfer_alloc_table); in scmi_xfer_token_clear()
423 * scmi_xfer_inflight_register_unlocked - Register the xfer as in-flight
428 * Note that this helper assumes that the xfer to be registered as in-flight
438 /* Set in-flight */ in scmi_xfer_inflight_register_unlocked()
439 set_bit(xfer->hdr.seq, minfo->xfer_alloc_table); in scmi_xfer_inflight_register_unlocked()
440 hash_add(minfo->pending_xfers, &xfer->node, xfer->hdr.seq); in scmi_xfer_inflight_register_unlocked()
441 xfer->pending = true; in scmi_xfer_inflight_register_unlocked()
445 * scmi_xfer_inflight_register - Try to register an xfer as in-flight
453 * same sequence number is currently still registered as in-flight.
455 * Return: 0 on Success or -EBUSY if sequence number embedded in the xfer
464 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_inflight_register()
465 if (!test_bit(xfer->hdr.seq, minfo->xfer_alloc_table)) in scmi_xfer_inflight_register()
468 ret = -EBUSY; in scmi_xfer_inflight_register()
469 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_inflight_register()
475 * scmi_xfer_raw_inflight_register - An helper to register the given xfer as in
488 return scmi_xfer_inflight_register(xfer, &info->tx_minfo); in scmi_xfer_raw_inflight_register()
492 * scmi_xfer_pending_set - Pick a proper sequence number and mark the xfer
493 * as pending in-flight
506 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_pending_set()
511 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_pending_set()
517 * scmi_xfer_get() - Allocate one message
543 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_get()
544 if (hlist_empty(&minfo->free_xfers)) { in scmi_xfer_get()
545 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_get()
546 return ERR_PTR(-ENOMEM); in scmi_xfer_get()
550 xfer = hlist_entry(minfo->free_xfers.first, struct scmi_xfer, node); in scmi_xfer_get()
551 hlist_del_init(&xfer->node); in scmi_xfer_get()
557 xfer->transfer_id = atomic_inc_return(&transfer_last_id); in scmi_xfer_get()
559 refcount_set(&xfer->users, 1); in scmi_xfer_get()
560 atomic_set(&xfer->busy, SCMI_XFER_FREE); in scmi_xfer_get()
561 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_get()
567 * scmi_xfer_raw_get - Helper to get a bare free xfer from the TX channel
573 * Return: A valid xfer on Success, or an error-pointer otherwise
580 xfer = scmi_xfer_get(handle, &info->tx_minfo); in scmi_xfer_raw_get()
582 xfer->flags |= SCMI_XFER_FLAG_IS_RAW; in scmi_xfer_raw_get()
588 * scmi_xfer_raw_channel_get - Helper to get a reference to the proper channel
592 * @protocol_id: Identifier of the protocol
594 * Note that in a regular SCMI stack, usually, a protocol has to be defined in
596 * protocol in range is allowed, re-using the Base channel, so as to enable
597 * fuzzing on any protocol without the need of a fully compiled DT.
607 cinfo = idr_find(&info->tx_idr, protocol_id); in scmi_xfer_raw_channel_get()
610 return ERR_PTR(-EINVAL); in scmi_xfer_raw_channel_get()
612 cinfo = idr_find(&info->tx_idr, SCMI_PROTOCOL_BASE); in scmi_xfer_raw_channel_get()
614 return ERR_PTR(-EINVAL); in scmi_xfer_raw_channel_get()
615 dev_warn_once(handle->dev, in scmi_xfer_raw_channel_get()
616 "Using Base channel for protocol 0x%X\n", in scmi_xfer_raw_channel_get()
624 * __scmi_xfer_put() - Release a message
639 spin_lock_irqsave(&minfo->xfer_lock, flags); in __scmi_xfer_put()
640 if (refcount_dec_and_test(&xfer->users)) { in __scmi_xfer_put()
641 if (xfer->pending) { in __scmi_xfer_put()
643 hash_del(&xfer->node); in __scmi_xfer_put()
644 xfer->pending = false; in __scmi_xfer_put()
646 hlist_add_head(&xfer->node, &minfo->free_xfers); in __scmi_xfer_put()
648 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in __scmi_xfer_put()
652 * scmi_xfer_raw_put - Release an xfer that was taken by @scmi_xfer_raw_get
664 xfer->flags &= ~SCMI_XFER_FLAG_IS_RAW; in scmi_xfer_raw_put()
665 xfer->flags &= ~SCMI_XFER_FLAG_CHAN_SET; in scmi_xfer_raw_put()
666 return __scmi_xfer_put(&info->tx_minfo, xfer); in scmi_xfer_raw_put()
670 * scmi_xfer_lookup_unlocked - Helper to lookup an xfer_id
673 * @xfer_id: Token ID to lookup in @pending_xfers
686 if (test_bit(xfer_id, minfo->xfer_alloc_table)) in scmi_xfer_lookup_unlocked()
687 xfer = XFER_FIND(minfo->pending_xfers, xfer_id); in scmi_xfer_lookup_unlocked()
689 return xfer ?: ERR_PTR(-EINVAL); in scmi_xfer_lookup_unlocked()
693 * scmi_msg_response_validate - Validate message type against state of related
702 * related synchronous response (Out-of-Order Delayed Response) the missing
705 * SCMI transport can deliver such out-of-order responses.
707 * Context: Assumes to be called with xfer->lock already acquired.
718 * delayed response we're not prepared to handle: bail-out safely in scmi_msg_response_validate()
721 if (msg_type == MSG_TYPE_DELAYED_RESP && !xfer->async_done) { in scmi_msg_response_validate()
722 dev_err(cinfo->dev, in scmi_msg_response_validate()
724 xfer->hdr.seq); in scmi_msg_response_validate()
725 return -EINVAL; in scmi_msg_response_validate()
728 switch (xfer->state) { in scmi_msg_response_validate()
735 xfer->hdr.status = SCMI_SUCCESS; in scmi_msg_response_validate()
736 xfer->state = SCMI_XFER_RESP_OK; in scmi_msg_response_validate()
737 complete(&xfer->done); in scmi_msg_response_validate()
738 dev_warn(cinfo->dev, in scmi_msg_response_validate()
740 xfer->hdr.seq); in scmi_msg_response_validate()
745 return -EINVAL; in scmi_msg_response_validate()
749 return -EINVAL; in scmi_msg_response_validate()
756 * scmi_xfer_state_update - Update xfer state
769 xfer->hdr.type = msg_type; in scmi_xfer_state_update()
772 if (xfer->hdr.type == MSG_TYPE_COMMAND) in scmi_xfer_state_update()
773 xfer->state = SCMI_XFER_RESP_OK; in scmi_xfer_state_update()
775 xfer->state = SCMI_XFER_DRESP_OK; in scmi_xfer_state_update()
782 ret = atomic_cmpxchg(&xfer->busy, SCMI_XFER_FREE, SCMI_XFER_BUSY); in scmi_xfer_acquired()
788 * scmi_xfer_command_acquire - Helper to lookup and acquire a command xfer
805 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_xfer_command_acquire()
806 struct scmi_xfers_info *minfo = &info->tx_minfo; in scmi_xfer_command_acquire()
811 spin_lock_irqsave(&minfo->xfer_lock, flags); in scmi_xfer_command_acquire()
814 dev_err(cinfo->dev, in scmi_xfer_command_acquire()
817 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_command_acquire()
820 refcount_inc(&xfer->users); in scmi_xfer_command_acquire()
821 spin_unlock_irqrestore(&minfo->xfer_lock, flags); in scmi_xfer_command_acquire()
823 spin_lock_irqsave(&xfer->lock, flags); in scmi_xfer_command_acquire()
836 spin_unlock_irqrestore(&xfer->lock, flags); in scmi_xfer_command_acquire()
839 dev_err(cinfo->dev, in scmi_xfer_command_acquire()
840 "Invalid message type:%d for %d - HDR:0x%X state:%d\n", in scmi_xfer_command_acquire()
841 msg_type, xfer_id, msg_hdr, xfer->state); in scmi_xfer_command_acquire()
844 xfer = ERR_PTR(-EINVAL); in scmi_xfer_command_acquire()
853 atomic_set(&xfer->busy, SCMI_XFER_FREE); in scmi_xfer_command_release()
854 __scmi_xfer_put(&info->tx_minfo, xfer); in scmi_xfer_command_release()
860 if (info->desc->ops->clear_channel) in scmi_clear_channel()
861 info->desc->ops->clear_channel(cinfo); in scmi_clear_channel()
868 struct device *dev = cinfo->dev; in scmi_handle_notification()
869 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_handle_notification()
870 struct scmi_xfers_info *minfo = &info->rx_minfo; in scmi_handle_notification()
874 xfer = scmi_xfer_get(cinfo->handle, minfo); in scmi_handle_notification()
882 unpack_scmi_header(msg_hdr, &xfer->hdr); in scmi_handle_notification()
884 /* Ensure order between xfer->priv store and following ops */ in scmi_handle_notification()
885 smp_store_mb(xfer->priv, priv); in scmi_handle_notification()
886 info->desc->ops->fetch_notification(cinfo, info->desc->max_msg_size, in scmi_handle_notification()
889 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, in scmi_handle_notification()
890 xfer->hdr.id, "NOTI", xfer->hdr.seq, in scmi_handle_notification()
891 xfer->hdr.status, xfer->rx.buf, xfer->rx.len); in scmi_handle_notification()
893 scmi_notify(cinfo->handle, xfer->hdr.protocol_id, in scmi_handle_notification()
894 xfer->hdr.id, xfer->rx.buf, xfer->rx.len, ts); in scmi_handle_notification()
896 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, in scmi_handle_notification()
897 xfer->hdr.protocol_id, xfer->hdr.seq, in scmi_handle_notification()
901 xfer->hdr.seq = MSG_XTRACT_TOKEN(msg_hdr); in scmi_handle_notification()
902 scmi_raw_message_report(info->raw, xfer, SCMI_RAW_NOTIF_QUEUE, in scmi_handle_notification()
903 cinfo->id); in scmi_handle_notification()
915 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_handle_response()
920 scmi_raw_error_report(info->raw, cinfo, msg_hdr, priv); in scmi_handle_response()
928 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) in scmi_handle_response()
929 xfer->rx.len = info->desc->max_msg_size; in scmi_handle_response()
932 /* Ensure order between xfer->priv store and following ops */ in scmi_handle_response()
933 smp_store_mb(xfer->priv, priv); in scmi_handle_response()
934 info->desc->ops->fetch_response(cinfo, xfer); in scmi_handle_response()
936 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, in scmi_handle_response()
937 xfer->hdr.id, in scmi_handle_response()
938 xfer->hdr.type == MSG_TYPE_DELAYED_RESP ? in scmi_handle_response()
941 xfer->hdr.seq, xfer->hdr.status, in scmi_handle_response()
942 xfer->rx.buf, xfer->rx.len); in scmi_handle_response()
944 trace_scmi_rx_done(xfer->transfer_id, xfer->hdr.id, in scmi_handle_response()
945 xfer->hdr.protocol_id, xfer->hdr.seq, in scmi_handle_response()
946 xfer->hdr.type); in scmi_handle_response()
948 if (xfer->hdr.type == MSG_TYPE_DELAYED_RESP) { in scmi_handle_response()
950 complete(xfer->async_done); in scmi_handle_response()
952 complete(&xfer->done); in scmi_handle_response()
961 if (!xfer->hdr.poll_completion) in scmi_handle_response()
962 scmi_raw_message_report(info->raw, xfer, in scmi_handle_response()
964 cinfo->id); in scmi_handle_response()
971 * scmi_rx_callback() - callback for receiving messages
1002 * xfer_put() - Release a transmit message
1004 * @ph: Pointer to SCMI protocol handle
1011 struct scmi_info *info = handle_to_scmi_info(pi->handle); in xfer_put()
1013 __scmi_xfer_put(&info->tx_minfo, xfer); in xfer_put()
1019 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_xfer_done_no_timeout()
1022 * Poll also on xfer->done so that polling can be forcibly terminated in scmi_xfer_done_no_timeout()
1023 * in case of out-of-order receptions of delayed responses in scmi_xfer_done_no_timeout()
1025 return info->desc->ops->poll_done(cinfo, xfer) || in scmi_xfer_done_no_timeout()
1026 try_wait_for_completion(&xfer->done) || in scmi_xfer_done_no_timeout()
1036 if (xfer->hdr.poll_completion) { in scmi_wait_for_reply()
1041 if (!desc->sync_cmds_completed_on_ret) { in scmi_wait_for_reply()
1052 "timed out in resp(caller: %pS) - polling\n", in scmi_wait_for_reply()
1054 ret = -ETIMEDOUT; in scmi_wait_for_reply()
1061 handle_to_scmi_info(cinfo->handle); in scmi_wait_for_reply()
1064 * Do not fetch_response if an out-of-order delayed in scmi_wait_for_reply()
1067 spin_lock_irqsave(&xfer->lock, flags); in scmi_wait_for_reply()
1068 if (xfer->state == SCMI_XFER_SENT_OK) { in scmi_wait_for_reply()
1069 desc->ops->fetch_response(cinfo, xfer); in scmi_wait_for_reply()
1070 xfer->state = SCMI_XFER_RESP_OK; in scmi_wait_for_reply()
1072 spin_unlock_irqrestore(&xfer->lock, flags); in scmi_wait_for_reply()
1075 trace_scmi_msg_dump(info->id, cinfo->id, in scmi_wait_for_reply()
1076 xfer->hdr.protocol_id, xfer->hdr.id, in scmi_wait_for_reply()
1079 xfer->hdr.seq, xfer->hdr.status, in scmi_wait_for_reply()
1080 xfer->rx.buf, xfer->rx.len); in scmi_wait_for_reply()
1084 handle_to_scmi_info(cinfo->handle); in scmi_wait_for_reply()
1086 scmi_raw_message_report(info->raw, xfer, in scmi_wait_for_reply()
1088 cinfo->id); in scmi_wait_for_reply()
1093 if (!wait_for_completion_timeout(&xfer->done, in scmi_wait_for_reply()
1097 ret = -ETIMEDOUT; in scmi_wait_for_reply()
1105 * scmi_wait_for_message_response - An helper to group all the possible ways of
1111 * Chooses waiting strategy (sleep-waiting vs busy-waiting) depending on
1112 * configuration flags like xfer->hdr.poll_completion.
1119 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_wait_for_message_response()
1120 struct device *dev = info->dev; in scmi_wait_for_message_response()
1122 trace_scmi_xfer_response_wait(xfer->transfer_id, xfer->hdr.id, in scmi_wait_for_message_response()
1123 xfer->hdr.protocol_id, xfer->hdr.seq, in scmi_wait_for_message_response()
1124 info->desc->max_rx_timeout_ms, in scmi_wait_for_message_response()
1125 xfer->hdr.poll_completion); in scmi_wait_for_message_response()
1127 return scmi_wait_for_reply(dev, info->desc, cinfo, xfer, in scmi_wait_for_message_response()
1128 info->desc->max_rx_timeout_ms); in scmi_wait_for_message_response()
1132 * scmi_xfer_raw_wait_for_message_response - An helper to wait for a message
1146 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_xfer_raw_wait_for_message_response()
1147 struct device *dev = info->dev; in scmi_xfer_raw_wait_for_message_response()
1149 ret = scmi_wait_for_reply(dev, info->desc, cinfo, xfer, timeout_ms); in scmi_xfer_raw_wait_for_message_response()
1151 dev_dbg(dev, "timed out in RAW response - HDR:%08X\n", in scmi_xfer_raw_wait_for_message_response()
1152 pack_scmi_header(&xfer->hdr)); in scmi_xfer_raw_wait_for_message_response()
1158 * do_xfer() - Do one transfer
1160 * @ph: Pointer to SCMI protocol handle
1163 * Return: -ETIMEDOUT in case of no response, if transmit error,
1172 struct scmi_info *info = handle_to_scmi_info(pi->handle); in do_xfer()
1173 struct device *dev = info->dev; in do_xfer()
1177 if (xfer->hdr.poll_completion && in do_xfer()
1178 !is_transport_polling_capable(info->desc)) { in do_xfer()
1181 return -EINVAL; in do_xfer()
1184 cinfo = idr_find(&info->tx_idr, pi->proto->id); in do_xfer()
1186 return -EINVAL; in do_xfer()
1189 if (is_polling_enabled(cinfo, info->desc)) in do_xfer()
1190 xfer->hdr.poll_completion = true; in do_xfer()
1193 * Initialise protocol id now from protocol handle to avoid it being in do_xfer()
1194 * overridden by mistake (or malice) by the protocol code mangling with in do_xfer()
1197 xfer->hdr.protocol_id = pi->proto->id; in do_xfer()
1198 reinit_completion(&xfer->done); in do_xfer()
1200 trace_scmi_xfer_begin(xfer->transfer_id, xfer->hdr.id, in do_xfer()
1201 xfer->hdr.protocol_id, xfer->hdr.seq, in do_xfer()
1202 xfer->hdr.poll_completion); in do_xfer()
1205 xfer->hdr.status = SCMI_SUCCESS; in do_xfer()
1206 xfer->state = SCMI_XFER_SENT_OK; in do_xfer()
1209 * on xfer->state due to the monotonically increasing tokens allocation, in do_xfer()
1210 * we must anyway ensure xfer->state initialization is not re-ordered in do_xfer()
1212 * ISR calling scmi_rx_callback() cannot see an old stale xfer->state. in do_xfer()
1216 ret = info->desc->ops->send_message(cinfo, xfer); in do_xfer()
1222 trace_scmi_msg_dump(info->id, cinfo->id, xfer->hdr.protocol_id, in do_xfer()
1223 xfer->hdr.id, "CMND", xfer->hdr.seq, in do_xfer()
1224 xfer->hdr.status, xfer->tx.buf, xfer->tx.len); in do_xfer()
1227 if (!ret && xfer->hdr.status) in do_xfer()
1228 ret = scmi_to_linux_errno(xfer->hdr.status); in do_xfer()
1230 if (info->desc->ops->mark_txdone) in do_xfer()
1231 info->desc->ops->mark_txdone(cinfo, ret, xfer); in do_xfer()
1233 trace_scmi_xfer_end(xfer->transfer_id, xfer->hdr.id, in do_xfer()
1234 xfer->hdr.protocol_id, xfer->hdr.seq, ret); in do_xfer()
1243 struct scmi_info *info = handle_to_scmi_info(pi->handle); in reset_rx_to_maxsz()
1245 xfer->rx.len = info->desc->max_msg_size; in reset_rx_to_maxsz()
1249 * do_xfer_with_response() - Do one transfer and wait until the delayed
1252 * @ph: Pointer to SCMI protocol handle
1256 * it could cause long busy-waiting here, so ignore polling for the delayed
1263 * attained at the protocol layer), but this would also have led to longer
1269 * Return: -ETIMEDOUT in case of no delayed response, if transmit error,
1278 xfer->async_done = &async_response; in do_xfer_with_response()
1286 WARN_ON_ONCE(xfer->hdr.poll_completion); in do_xfer_with_response()
1290 if (!wait_for_completion_timeout(xfer->async_done, timeout)) { in do_xfer_with_response()
1291 dev_err(ph->dev, in do_xfer_with_response()
1294 ret = -ETIMEDOUT; in do_xfer_with_response()
1295 } else if (xfer->hdr.status) { in do_xfer_with_response()
1296 ret = scmi_to_linux_errno(xfer->hdr.status); in do_xfer_with_response()
1300 xfer->async_done = NULL; in do_xfer_with_response()
1305 * xfer_get_init() - Allocate and initialise one message for transmit
1307 * @ph: Pointer to SCMI protocol handle
1326 struct scmi_info *info = handle_to_scmi_info(pi->handle); in xfer_get_init()
1327 struct scmi_xfers_info *minfo = &info->tx_minfo; in xfer_get_init()
1328 struct device *dev = info->dev; in xfer_get_init()
1331 if (rx_size > info->desc->max_msg_size || in xfer_get_init()
1332 tx_size > info->desc->max_msg_size) in xfer_get_init()
1333 return -ERANGE; in xfer_get_init()
1335 xfer = scmi_xfer_get(pi->handle, minfo); in xfer_get_init()
1342 /* Pick a sequence number and register this xfer as in-flight */ in xfer_get_init()
1345 dev_err(pi->handle->dev, in xfer_get_init()
1351 xfer->tx.len = tx_size; in xfer_get_init()
1352 xfer->rx.len = rx_size ? : info->desc->max_msg_size; in xfer_get_init()
1353 xfer->hdr.type = MSG_TYPE_COMMAND; in xfer_get_init()
1354 xfer->hdr.id = msg_id; in xfer_get_init()
1355 xfer->hdr.poll_completion = false; in xfer_get_init()
1363 * version_get() - command to get the revision of the SCMI entity
1365 * @ph: Pointer to SCMI protocol handle
1366 * @version: Holds returned version of protocol.
1384 rev_info = t->rx.buf; in version_get()
1393 * scmi_set_protocol_priv - Set protocol specific data at init time
1395 * @ph: A reference to the protocol handle.
1397 * @version: The detected protocol version for the core to register.
1406 pi->priv = priv; in scmi_set_protocol_priv()
1407 pi->version = version; in scmi_set_protocol_priv()
1413 * scmi_get_protocol_priv - Set protocol specific data at init time
1415 * @ph: A reference to the protocol handle.
1417 * Return: Protocol private data if any was set.
1423 return pi->priv; in scmi_get_protocol_priv()
1441 * scmi_common_extended_name_get - Common helper to get extended resources name
1442 * @ph: A protocol handle reference.
1443 * @cmd_id: The specific command ID to use.
1444 * @res_id: The specific resource ID to use.
1462 ret = ph->xops->xfer_get_init(ph, cmd_id, txlen, sizeof(*resp), &t); in scmi_common_extended_name_get()
1466 put_unaligned_le32(res_id, t->tx.buf); in scmi_common_extended_name_get()
1468 put_unaligned_le32(*flags, t->tx.buf + sizeof(res_id)); in scmi_common_extended_name_get()
1469 resp = t->rx.buf; in scmi_common_extended_name_get()
1471 ret = ph->xops->do_xfer(ph, t); in scmi_common_extended_name_get()
1473 strscpy(name, resp->name, len); in scmi_common_extended_name_get()
1475 ph->xops->xfer_put(ph, t); in scmi_common_extended_name_get()
1478 dev_warn(ph->dev, in scmi_common_extended_name_get()
1479 "Failed to get extended name - id:%u (ret:%d). Using %s\n", in scmi_common_extended_name_get()
1485 * struct scmi_iterator - Iterator descriptor
1487 * a proper custom command payload for each multi-part command request.
1489 * @process_response to parse the multi-part replies.
1492 * @ph: A reference to the associated protocol handle to be used.
1495 * internal routines and by the caller-provided @scmi_iterator_ops.
1517 i = devm_kzalloc(ph->dev, sizeof(*i), GFP_KERNEL); in scmi_iterator_init()
1519 return ERR_PTR(-ENOMEM); in scmi_iterator_init()
1521 i->ph = ph; in scmi_iterator_init()
1522 i->ops = ops; in scmi_iterator_init()
1523 i->priv = priv; in scmi_iterator_init()
1525 ret = ph->xops->xfer_get_init(ph, msg_id, tx_size, 0, &i->t); in scmi_iterator_init()
1527 devm_kfree(ph->dev, i); in scmi_iterator_init()
1531 i->state.max_resources = max_resources; in scmi_iterator_init()
1532 i->msg = i->t->tx.buf; in scmi_iterator_init()
1533 i->resp = i->t->rx.buf; in scmi_iterator_init()
1540 int ret = -EINVAL; in scmi_iterator_run()
1546 if (!i || !i->ops || !i->ph) in scmi_iterator_run()
1549 iops = i->ops; in scmi_iterator_run()
1550 ph = i->ph; in scmi_iterator_run()
1551 st = &i->state; in scmi_iterator_run()
1554 iops->prepare_message(i->msg, st->desc_index, i->priv); in scmi_iterator_run()
1555 ret = ph->xops->do_xfer(ph, i->t); in scmi_iterator_run()
1559 st->rx_len = i->t->rx.len; in scmi_iterator_run()
1560 ret = iops->update_state(st, i->resp, i->priv); in scmi_iterator_run()
1564 if (st->num_returned > st->max_resources - st->desc_index) { in scmi_iterator_run()
1565 dev_err(ph->dev, in scmi_iterator_run()
1567 st->max_resources); in scmi_iterator_run()
1568 ret = -EINVAL; in scmi_iterator_run()
1572 for (st->loop_idx = 0; st->loop_idx < st->num_returned; in scmi_iterator_run()
1573 st->loop_idx++) { in scmi_iterator_run()
1574 ret = iops->process_response(ph, i->resp, st, i->priv); in scmi_iterator_run()
1579 st->desc_index += st->num_returned; in scmi_iterator_run()
1580 ph->xops->reset_rx_to_maxsz(ph, i->t); in scmi_iterator_run()
1585 } while (st->num_returned && st->num_remaining); in scmi_iterator_run()
1589 ph->xops->xfer_put(ph, i->t); in scmi_iterator_run()
1590 devm_kfree(ph->dev, i); in scmi_iterator_run()
1634 ret = -EINVAL; in scmi_common_fastchannel_init()
1638 ret = ph->xops->xfer_get_init(ph, describe_id, in scmi_common_fastchannel_init()
1643 info = t->tx.buf; in scmi_common_fastchannel_init()
1644 info->domain = cpu_to_le32(domain); in scmi_common_fastchannel_init()
1645 info->message_id = cpu_to_le32(message_id); in scmi_common_fastchannel_init()
1652 ret = ph->xops->do_xfer(ph, t); in scmi_common_fastchannel_init()
1656 resp = t->rx.buf; in scmi_common_fastchannel_init()
1657 flags = le32_to_cpu(resp->attr); in scmi_common_fastchannel_init()
1658 size = le32_to_cpu(resp->chan_size); in scmi_common_fastchannel_init()
1660 ret = -EINVAL; in scmi_common_fastchannel_init()
1664 phys_addr = le32_to_cpu(resp->chan_addr_low); in scmi_common_fastchannel_init()
1665 phys_addr |= (u64)le32_to_cpu(resp->chan_addr_high) << 32; in scmi_common_fastchannel_init()
1666 addr = devm_ioremap(ph->dev, phys_addr, size); in scmi_common_fastchannel_init()
1668 ret = -EADDRNOTAVAIL; in scmi_common_fastchannel_init()
1675 db = devm_kzalloc(ph->dev, sizeof(*db), GFP_KERNEL); in scmi_common_fastchannel_init()
1677 ret = -ENOMEM; in scmi_common_fastchannel_init()
1682 phys_addr = le32_to_cpu(resp->db_addr_low); in scmi_common_fastchannel_init()
1683 phys_addr |= (u64)le32_to_cpu(resp->db_addr_high) << 32; in scmi_common_fastchannel_init()
1684 addr = devm_ioremap(ph->dev, phys_addr, size); in scmi_common_fastchannel_init()
1686 ret = -EADDRNOTAVAIL; in scmi_common_fastchannel_init()
1690 db->addr = addr; in scmi_common_fastchannel_init()
1691 db->width = size; in scmi_common_fastchannel_init()
1692 db->set = le32_to_cpu(resp->db_set_lmask); in scmi_common_fastchannel_init()
1693 db->set |= (u64)le32_to_cpu(resp->db_set_hmask) << 32; in scmi_common_fastchannel_init()
1694 db->mask = le32_to_cpu(resp->db_preserve_lmask); in scmi_common_fastchannel_init()
1695 db->mask |= (u64)le32_to_cpu(resp->db_preserve_hmask) << 32; in scmi_common_fastchannel_init()
1700 ph->xops->xfer_put(ph, t); in scmi_common_fastchannel_init()
1702 dev_dbg(ph->dev, in scmi_common_fastchannel_init()
1703 "Using valid FC for protocol %X [MSG_ID:%u / RES_ID:%u]\n", in scmi_common_fastchannel_init()
1704 pi->proto->id, message_id, domain); in scmi_common_fastchannel_init()
1709 devm_kfree(ph->dev, db); in scmi_common_fastchannel_init()
1715 ph->xops->xfer_put(ph, t); in scmi_common_fastchannel_init()
1718 dev_warn(ph->dev, in scmi_common_fastchannel_init()
1719 "Failed to get FC for protocol %X [MSG_ID:%u / RES_ID:%u] - ret:%d. Using regular messaging.\n", in scmi_common_fastchannel_init()
1720 pi->proto->id, message_id, domain, ret); in scmi_common_fastchannel_init()
1727 if (db->mask) \
1728 val = ioread##w(db->addr) & db->mask; \
1729 iowrite##w((u##w)db->set | val, db->addr); \
1734 if (!db || !db->addr) in scmi_common_fastchannel_db_ring()
1737 if (db->width == 1) in scmi_common_fastchannel_db_ring()
1739 else if (db->width == 2) in scmi_common_fastchannel_db_ring()
1741 else if (db->width == 4) in scmi_common_fastchannel_db_ring()
1743 else /* db->width == 8 */ in scmi_common_fastchannel_db_ring()
1750 if (db->mask) in scmi_common_fastchannel_db_ring()
1751 val = ioread64_hi_lo(db->addr) & db->mask; in scmi_common_fastchannel_db_ring()
1752 iowrite64_hi_lo(db->set | val, db->addr); in scmi_common_fastchannel_db_ring()
1766 * scmi_revision_area_get - Retrieve version memory area.
1768 * @ph: A reference to the protocol handle.
1770 * A helper to grab the version memory area reference during SCMI Base protocol
1774 * instance underlying this protocol handle.
1781 return pi->handle->version; in scmi_revision_area_get()
1785 * scmi_alloc_init_protocol_instance - Allocate and initialize a protocol
1788 * @proto: The protocol descriptor.
1790 * Allocate a new protocol instance descriptor, using the provided @proto
1792 * all resources management is handled via a dedicated per-protocol devres
1796 * Return: A reference to a freshly allocated and initialized protocol instance
1804 int ret = -ENOMEM; in scmi_alloc_init_protocol_instance()
1807 const struct scmi_handle *handle = &info->handle; in scmi_alloc_init_protocol_instance()
1809 /* Protocol specific devres group */ in scmi_alloc_init_protocol_instance()
1810 gid = devres_open_group(handle->dev, NULL, GFP_KERNEL); in scmi_alloc_init_protocol_instance()
1812 scmi_protocol_put(proto->id); in scmi_alloc_init_protocol_instance()
1816 pi = devm_kzalloc(handle->dev, sizeof(*pi), GFP_KERNEL); in scmi_alloc_init_protocol_instance()
1820 pi->gid = gid; in scmi_alloc_init_protocol_instance()
1821 pi->proto = proto; in scmi_alloc_init_protocol_instance()
1822 pi->handle = handle; in scmi_alloc_init_protocol_instance()
1823 pi->ph.dev = handle->dev; in scmi_alloc_init_protocol_instance()
1824 pi->ph.xops = &xfer_ops; in scmi_alloc_init_protocol_instance()
1825 pi->ph.hops = &helpers_ops; in scmi_alloc_init_protocol_instance()
1826 pi->ph.set_priv = scmi_set_protocol_priv; in scmi_alloc_init_protocol_instance()
1827 pi->ph.get_priv = scmi_get_protocol_priv; in scmi_alloc_init_protocol_instance()
1828 refcount_set(&pi->users, 1); in scmi_alloc_init_protocol_instance()
1829 /* proto->init is assured NON NULL by scmi_protocol_register */ in scmi_alloc_init_protocol_instance()
1830 ret = pi->proto->instance_init(&pi->ph); in scmi_alloc_init_protocol_instance()
1834 ret = idr_alloc(&info->protocols, pi, proto->id, proto->id + 1, in scmi_alloc_init_protocol_instance()
1836 if (ret != proto->id) in scmi_alloc_init_protocol_instance()
1843 if (pi->proto->events) { in scmi_alloc_init_protocol_instance()
1844 ret = scmi_register_protocol_events(handle, pi->proto->id, in scmi_alloc_init_protocol_instance()
1845 &pi->ph, in scmi_alloc_init_protocol_instance()
1846 pi->proto->events); in scmi_alloc_init_protocol_instance()
1848 dev_warn(handle->dev, in scmi_alloc_init_protocol_instance()
1849 "Protocol:%X - Events Registration Failed - err:%d\n", in scmi_alloc_init_protocol_instance()
1850 pi->proto->id, ret); in scmi_alloc_init_protocol_instance()
1853 devres_close_group(handle->dev, pi->gid); in scmi_alloc_init_protocol_instance()
1854 dev_dbg(handle->dev, "Initialized protocol: 0x%X\n", pi->proto->id); in scmi_alloc_init_protocol_instance()
1856 if (pi->version > proto->supported_version) in scmi_alloc_init_protocol_instance()
1857 dev_warn(handle->dev, in scmi_alloc_init_protocol_instance()
1858 "Detected UNSUPPORTED higher version 0x%X for protocol 0x%X." in scmi_alloc_init_protocol_instance()
1860 pi->version, pi->proto->id); in scmi_alloc_init_protocol_instance()
1865 /* Take care to put the protocol module's owner before releasing all */ in scmi_alloc_init_protocol_instance()
1866 scmi_protocol_put(proto->id); in scmi_alloc_init_protocol_instance()
1867 devres_release_group(handle->dev, gid); in scmi_alloc_init_protocol_instance()
1873 * scmi_get_protocol_instance - Protocol initialization helper.
1875 * @protocol_id: The protocol being requested.
1877 * In case the required protocol has never been requested before for this
1879 * resource allocation with a dedicated per-protocol devres subgroup.
1881 * Return: A reference to an initialized protocol instance or error on failure:
1882 * in particular returns -EPROBE_DEFER when the desired protocol could
1891 mutex_lock(&info->protocols_mtx); in scmi_get_protocol_instance()
1892 pi = idr_find(&info->protocols, protocol_id); in scmi_get_protocol_instance()
1895 refcount_inc(&pi->users); in scmi_get_protocol_instance()
1899 /* Fails if protocol not registered on bus */ in scmi_get_protocol_instance()
1904 pi = ERR_PTR(-EPROBE_DEFER); in scmi_get_protocol_instance()
1906 mutex_unlock(&info->protocols_mtx); in scmi_get_protocol_instance()
1912 * scmi_protocol_acquire - Protocol acquire
1914 * @protocol_id: The protocol being requested.
1916 * Register a new user for the requested protocol on the specified SCMI
1919 * Return: 0 if protocol was acquired successfully.
1927 * scmi_protocol_release - Protocol de-initialization helper.
1929 * @protocol_id: The protocol being requested.
1931 * Remove one user for the specified protocol and triggers de-initialization
1932 * and resources de-allocation once the last user has gone.
1939 mutex_lock(&info->protocols_mtx); in scmi_protocol_release()
1940 pi = idr_find(&info->protocols, protocol_id); in scmi_protocol_release()
1944 if (refcount_dec_and_test(&pi->users)) { in scmi_protocol_release()
1945 void *gid = pi->gid; in scmi_protocol_release()
1947 if (pi->proto->events) in scmi_protocol_release()
1950 if (pi->proto->instance_deinit) in scmi_protocol_release()
1951 pi->proto->instance_deinit(&pi->ph); in scmi_protocol_release()
1953 idr_remove(&info->protocols, protocol_id); in scmi_protocol_release()
1957 devres_release_group(handle->dev, gid); in scmi_protocol_release()
1958 dev_dbg(handle->dev, "De-Initialized protocol: 0x%X\n", in scmi_protocol_release()
1963 mutex_unlock(&info->protocols_mtx); in scmi_protocol_release()
1970 struct scmi_info *info = handle_to_scmi_info(pi->handle); in scmi_setup_protocol_implemented()
1972 info->protocols_imp = prot_imp; in scmi_setup_protocol_implemented()
1980 struct scmi_revision_info *rev = handle->version; in scmi_is_protocol_implemented()
1982 if (!info->protocols_imp) in scmi_is_protocol_implemented()
1985 for (i = 0; i < rev->num_protocols; i++) in scmi_is_protocol_implemented()
1986 if (info->protocols_imp[i] == prot_id) in scmi_is_protocol_implemented()
2000 scmi_protocol_release(dres->handle, dres->protocol_id); in scmi_devm_release_protocol()
2012 return ERR_PTR(-ENOMEM); in scmi_devres_protocol_instance_get()
2014 pi = scmi_get_protocol_instance(sdev->handle, protocol_id); in scmi_devres_protocol_instance_get()
2020 dres->handle = sdev->handle; in scmi_devres_protocol_instance_get()
2021 dres->protocol_id = protocol_id; in scmi_devres_protocol_instance_get()
2022 devres_add(&sdev->dev, dres); in scmi_devres_protocol_instance_get()
2028 * scmi_devm_protocol_get - Devres managed get protocol operations and handle
2031 * @protocol_id: The protocol being requested.
2032 * @ph: A pointer reference used to pass back the associated protocol handle.
2034 * Get hold of a protocol accounting for its usage, eventually triggering its
2035 * initialization, and returning the protocol specific operations and related
2036 * protocol handle which will be used as first argument in most of the
2038 * Being a devres based managed method, protocol hold will be automatically
2039 * released, and possibly de-initialized on last user, once the SCMI driver
2042 * Return: A reference to the requested protocol operations or error.
2052 return ERR_PTR(-EINVAL); in scmi_devm_protocol_get()
2058 *ph = &pi->ph; in scmi_devm_protocol_get()
2060 return pi->proto->ops; in scmi_devm_protocol_get()
2064 * scmi_devm_protocol_acquire - Devres managed helper to get hold of a protocol
2067 * @protocol_id: The protocol being requested.
2069 * Get hold of a protocol accounting for its usage, possibly triggering its
2070 * initialization but without getting access to its protocol specific operations
2073 * Being a devres based managed method, protocol hold will be automatically
2074 * released, and possibly de-initialized on last user, once the SCMI driver
2098 return dres->protocol_id == *((u8 *)data); in scmi_devm_protocol_match()
2102 * scmi_devm_protocol_put - Devres managed put protocol operations and handle
2105 * @protocol_id: The protocol being requested.
2107 * Explicitly release a protocol hold previously obtained calling the above
2114 ret = devres_release(&sdev->dev, scmi_devm_release_protocol, in scmi_devm_protocol_put()
2120 * scmi_is_transport_atomic - Method to check if underlying transport for an
2135 ret = info->desc->atomic_enabled && in scmi_is_transport_atomic()
2136 is_transport_polling_capable(info->desc); in scmi_is_transport_atomic()
2138 *atomic_threshold = info->atomic_threshold; in scmi_is_transport_atomic()
2144 * scmi_handle_get() - Get the SCMI handle for a device
2149 * and is expected to be maintained by caller of SCMI protocol library.
2163 if (dev->parent == info->dev) { in scmi_handle_get()
2164 info->users++; in scmi_handle_get()
2165 handle = &info->handle; in scmi_handle_get()
2175 * scmi_handle_put() - Release the handle acquired by scmi_handle_get
2180 * and is expected to be maintained by caller of SCMI protocol library.
2184 * if null was passed, it returns -EINVAL;
2191 return -EINVAL; in scmi_handle_put()
2195 if (!WARN_ON(!info->users)) in scmi_handle_put()
2196 info->users--; in scmi_handle_put()
2214 scmi_dev->handle = scmi_handle_get(&scmi_dev->dev); in scmi_set_handle()
2215 if (scmi_dev->handle) in scmi_set_handle()
2216 scmi_device_link_add(&scmi_dev->dev, scmi_dev->handle->dev); in scmi_set_handle()
2224 struct device *dev = sinfo->dev; in __scmi_xfer_info_init()
2225 const struct scmi_desc *desc = sinfo->desc; in __scmi_xfer_info_init()
2227 /* Pre-allocated messages, no more than what hdr.seq can support */ in __scmi_xfer_info_init()
2228 if (WARN_ON(!info->max_msg || info->max_msg > MSG_TOKEN_MAX)) { in __scmi_xfer_info_init()
2230 "Invalid maximum messages %d, not in range [1 - %lu]\n", in __scmi_xfer_info_init()
2231 info->max_msg, MSG_TOKEN_MAX); in __scmi_xfer_info_init()
2232 return -EINVAL; in __scmi_xfer_info_init()
2235 hash_init(info->pending_xfers); in __scmi_xfer_info_init()
2238 info->xfer_alloc_table = devm_bitmap_zalloc(dev, MSG_TOKEN_MAX, in __scmi_xfer_info_init()
2240 if (!info->xfer_alloc_table) in __scmi_xfer_info_init()
2241 return -ENOMEM; in __scmi_xfer_info_init()
2245 * pre-initialize the buffer pointer to pre-allocated buffers and in __scmi_xfer_info_init()
2248 INIT_HLIST_HEAD(&info->free_xfers); in __scmi_xfer_info_init()
2249 for (i = 0; i < info->max_msg; i++) { in __scmi_xfer_info_init()
2252 return -ENOMEM; in __scmi_xfer_info_init()
2254 xfer->rx.buf = devm_kcalloc(dev, sizeof(u8), desc->max_msg_size, in __scmi_xfer_info_init()
2256 if (!xfer->rx.buf) in __scmi_xfer_info_init()
2257 return -ENOMEM; in __scmi_xfer_info_init()
2259 xfer->tx.buf = xfer->rx.buf; in __scmi_xfer_info_init()
2260 init_completion(&xfer->done); in __scmi_xfer_info_init()
2261 spin_lock_init(&xfer->lock); in __scmi_xfer_info_init()
2264 hlist_add_head(&xfer->node, &info->free_xfers); in __scmi_xfer_info_init()
2267 spin_lock_init(&info->xfer_lock); in __scmi_xfer_info_init()
2274 const struct scmi_desc *desc = sinfo->desc; in scmi_channels_max_msg_configure()
2276 if (!desc->ops->get_max_msg) { in scmi_channels_max_msg_configure()
2277 sinfo->tx_minfo.max_msg = desc->max_msg; in scmi_channels_max_msg_configure()
2278 sinfo->rx_minfo.max_msg = desc->max_msg; in scmi_channels_max_msg_configure()
2282 base_cinfo = idr_find(&sinfo->tx_idr, SCMI_PROTOCOL_BASE); in scmi_channels_max_msg_configure()
2284 return -EINVAL; in scmi_channels_max_msg_configure()
2285 sinfo->tx_minfo.max_msg = desc->ops->get_max_msg(base_cinfo); in scmi_channels_max_msg_configure()
2288 base_cinfo = idr_find(&sinfo->rx_idr, SCMI_PROTOCOL_BASE); in scmi_channels_max_msg_configure()
2290 sinfo->rx_minfo.max_msg = in scmi_channels_max_msg_configure()
2291 desc->ops->get_max_msg(base_cinfo); in scmi_channels_max_msg_configure()
2305 ret = __scmi_xfer_info_init(sinfo, &sinfo->tx_minfo); in scmi_xfer_info_init()
2306 if (!ret && !idr_is_empty(&sinfo->rx_idr)) in scmi_xfer_info_init()
2307 ret = __scmi_xfer_info_init(sinfo, &sinfo->rx_minfo); in scmi_xfer_info_init()
2323 idr = tx ? &info->tx_idr : &info->rx_idr; in scmi_chan_setup()
2325 if (!info->desc->ops->chan_available(of_node, idx)) { in scmi_chan_setup()
2328 return -EINVAL; in scmi_chan_setup()
2332 cinfo = devm_kzalloc(info->dev, sizeof(*cinfo), GFP_KERNEL); in scmi_chan_setup()
2334 return -ENOMEM; in scmi_chan_setup()
2336 cinfo->rx_timeout_ms = info->desc->max_rx_timeout_ms; in scmi_chan_setup()
2342 tdev = scmi_device_create(of_node, info->dev, prot_id, name); in scmi_chan_setup()
2344 dev_err(info->dev, in scmi_chan_setup()
2346 devm_kfree(info->dev, cinfo); in scmi_chan_setup()
2347 return -EINVAL; in scmi_chan_setup()
2351 cinfo->id = prot_id; in scmi_chan_setup()
2352 cinfo->dev = &tdev->dev; in scmi_chan_setup()
2353 ret = info->desc->ops->chan_setup(cinfo, info->dev, tx); in scmi_chan_setup()
2356 scmi_device_destroy(info->dev, prot_id, name); in scmi_chan_setup()
2357 devm_kfree(info->dev, cinfo); in scmi_chan_setup()
2361 if (tx && is_polling_required(cinfo, info->desc)) { in scmi_chan_setup()
2362 if (is_transport_polling_capable(info->desc)) in scmi_chan_setup()
2363 dev_info(&tdev->dev, in scmi_chan_setup()
2364 "Enabled polling mode TX channel - prot_id:%d\n", in scmi_chan_setup()
2367 dev_warn(&tdev->dev, in scmi_chan_setup()
2374 dev_err(info->dev, in scmi_chan_setup()
2379 scmi_device_destroy(info->dev, prot_id, name); in scmi_chan_setup()
2380 devm_kfree(info->dev, cinfo); in scmi_chan_setup()
2385 cinfo->handle = &info->handle; in scmi_chan_setup()
2398 if (ret && ret != -ENOMEM) in scmi_txrx_setup()
2406 * scmi_channels_setup - Helper to initialize all required channels
2417 * protocol defined in the DT, a distinct freshly initialized channel is
2418 * created only if the DT node for the protocol at hand describes a dedicated
2419 * channel: in all the other cases the common BASE protocol channel is reused.
2426 struct device_node *child, *top_np = info->dev->of_node; in scmi_channels_setup()
2440 dev_err(info->dev, in scmi_channels_setup()
2441 "Out of range protocol %d\n", prot_id); in scmi_channels_setup()
2453 static int scmi_chan_destroy(int id, void *p, void *idr) in scmi_chan_destroy() argument
2457 if (cinfo->dev) { in scmi_chan_destroy()
2458 struct scmi_info *info = handle_to_scmi_info(cinfo->handle); in scmi_chan_destroy()
2459 struct scmi_device *sdev = to_scmi_dev(cinfo->dev); in scmi_chan_destroy()
2461 of_node_put(cinfo->dev->of_node); in scmi_chan_destroy()
2462 scmi_device_destroy(info->dev, id, sdev->name); in scmi_chan_destroy()
2463 cinfo->dev = NULL; in scmi_chan_destroy()
2466 idr_remove(idr, id); in scmi_chan_destroy()
2474 idr_for_each(idr, info->desc->ops->chan_free, idr); in scmi_cleanup_channels()
2484 scmi_cleanup_channels(info, &info->tx_idr); in scmi_cleanup_txrx_channels()
2486 scmi_cleanup_channels(info, &info->rx_idr); in scmi_cleanup_txrx_channels()
2496 if (!strncmp(sdev->name, "__scmi_transport_device", 23) || in scmi_bus_notifier()
2497 sdev->dev.parent != info->dev) in scmi_bus_notifier()
2506 scmi_handle_put(sdev->handle); in scmi_bus_notifier()
2507 sdev->handle = NULL; in scmi_bus_notifier()
2513 dev_dbg(info->dev, "Device %s (%s) is now %s\n", dev_name(&sdev->dev), in scmi_bus_notifier()
2514 sdev->name, action == BUS_NOTIFY_BIND_DRIVER ? in scmi_bus_notifier()
2527 np = idr_find(&info->active_protocols, id_table->protocol_id); in scmi_device_request_notifier()
2531 dev_dbg(info->dev, "%sRequested device (%s) for protocol 0x%x\n", in scmi_device_request_notifier()
2532 action == SCMI_BUS_NOTIFY_DEVICE_REQUEST ? "" : "UN-", in scmi_device_request_notifier()
2533 id_table->name, id_table->protocol_id); in scmi_device_request_notifier()
2537 scmi_create_protocol_devices(np, info, id_table->protocol_id, in scmi_device_request_notifier()
2538 id_table->name); in scmi_device_request_notifier()
2541 scmi_destroy_protocol_devices(info, id_table->protocol_id, in scmi_device_request_notifier()
2542 id_table->name); in scmi_device_request_notifier()
2558 debugfs_remove_recursive(dbg->top_dentry); in scmi_debugfs_common_cleanup()
2559 kfree(dbg->name); in scmi_debugfs_common_cleanup()
2560 kfree(dbg->type); in scmi_debugfs_common_cleanup()
2570 dbg = devm_kzalloc(info->dev, sizeof(*dbg), GFP_KERNEL); in scmi_debugfs_common_setup()
2574 dbg->name = kstrdup(of_node_full_name(info->dev->of_node), GFP_KERNEL); in scmi_debugfs_common_setup()
2575 if (!dbg->name) { in scmi_debugfs_common_setup()
2576 devm_kfree(info->dev, dbg); in scmi_debugfs_common_setup()
2580 of_property_read_string(info->dev->of_node, "compatible", &c_ptr); in scmi_debugfs_common_setup()
2581 dbg->type = kstrdup(c_ptr, GFP_KERNEL); in scmi_debugfs_common_setup()
2582 if (!dbg->type) { in scmi_debugfs_common_setup()
2583 kfree(dbg->name); in scmi_debugfs_common_setup()
2584 devm_kfree(info->dev, dbg); in scmi_debugfs_common_setup()
2588 snprintf(top_dir, 16, "%d", info->id); in scmi_debugfs_common_setup()
2592 dbg->is_atomic = info->desc->atomic_enabled && in scmi_debugfs_common_setup()
2593 is_transport_polling_capable(info->desc); in scmi_debugfs_common_setup()
2596 (char **)&dbg->name); in scmi_debugfs_common_setup()
2599 &info->atomic_threshold); in scmi_debugfs_common_setup()
2601 debugfs_create_str("type", 0400, trans, (char **)&dbg->type); in scmi_debugfs_common_setup()
2603 debugfs_create_bool("is_atomic", 0400, trans, &dbg->is_atomic); in scmi_debugfs_common_setup()
2606 (u32 *)&info->desc->max_rx_timeout_ms); in scmi_debugfs_common_setup()
2609 (u32 *)&info->desc->max_msg_size); in scmi_debugfs_common_setup()
2612 (u32 *)&info->tx_minfo.max_msg); in scmi_debugfs_common_setup()
2615 (u32 *)&info->rx_minfo.max_msg); in scmi_debugfs_common_setup()
2617 dbg->top_dentry = top_dentry; in scmi_debugfs_common_setup()
2619 if (devm_add_action_or_reset(info->dev, in scmi_debugfs_common_setup()
2630 int id, num_chans = 0, ret = 0; in scmi_debugfs_raw_mode_setup() local
2635 if (!info->dbg) in scmi_debugfs_raw_mode_setup()
2636 return -EINVAL; in scmi_debugfs_raw_mode_setup()
2639 idr_for_each_entry(&info->tx_idr, cinfo, id) { in scmi_debugfs_raw_mode_setup()
2645 dev_warn(info->dev, in scmi_debugfs_raw_mode_setup()
2646 "SCMI RAW - Error enumerating channels\n"); in scmi_debugfs_raw_mode_setup()
2650 if (!test_bit(cinfo->id, protos)) { in scmi_debugfs_raw_mode_setup()
2651 channels[num_chans++] = cinfo->id; in scmi_debugfs_raw_mode_setup()
2652 set_bit(cinfo->id, protos); in scmi_debugfs_raw_mode_setup()
2656 info->raw = scmi_raw_mode_init(&info->handle, info->dbg->top_dentry, in scmi_debugfs_raw_mode_setup()
2657 info->id, channels, num_chans, in scmi_debugfs_raw_mode_setup()
2658 info->desc, info->tx_minfo.max_msg); in scmi_debugfs_raw_mode_setup()
2659 if (IS_ERR(info->raw)) { in scmi_debugfs_raw_mode_setup()
2660 dev_err(info->dev, "Failed to initialize SCMI RAW Mode !\n"); in scmi_debugfs_raw_mode_setup()
2661 ret = PTR_ERR(info->raw); in scmi_debugfs_raw_mode_setup()
2662 info->raw = NULL; in scmi_debugfs_raw_mode_setup()
2675 struct device *dev = &pdev->dev; in scmi_probe()
2676 struct device_node *child, *np = dev->of_node; in scmi_probe()
2680 return -EINVAL; in scmi_probe()
2684 return -ENOMEM; in scmi_probe()
2686 info->id = ida_alloc_min(&scmi_id, 0, GFP_KERNEL); in scmi_probe()
2687 if (info->id < 0) in scmi_probe()
2688 return info->id; in scmi_probe()
2690 info->dev = dev; in scmi_probe()
2691 info->desc = desc; in scmi_probe()
2692 info->bus_nb.notifier_call = scmi_bus_notifier; in scmi_probe()
2693 info->dev_req_nb.notifier_call = scmi_device_request_notifier; in scmi_probe()
2694 INIT_LIST_HEAD(&info->node); in scmi_probe()
2695 idr_init(&info->protocols); in scmi_probe()
2696 mutex_init(&info->protocols_mtx); in scmi_probe()
2697 idr_init(&info->active_protocols); in scmi_probe()
2698 mutex_init(&info->devreq_mtx); in scmi_probe()
2701 idr_init(&info->tx_idr); in scmi_probe()
2702 idr_init(&info->rx_idr); in scmi_probe()
2704 handle = &info->handle; in scmi_probe()
2705 handle->dev = info->dev; in scmi_probe()
2706 handle->version = &info->version; in scmi_probe()
2707 handle->devm_protocol_acquire = scmi_devm_protocol_acquire; in scmi_probe()
2708 handle->devm_protocol_get = scmi_devm_protocol_get; in scmi_probe()
2709 handle->devm_protocol_put = scmi_devm_protocol_put; in scmi_probe()
2712 if (!of_property_read_u32(np, "atomic-threshold-us", in scmi_probe()
2713 &info->atomic_threshold)) in scmi_probe()
2716 info->atomic_threshold); in scmi_probe()
2717 handle->is_transport_atomic = scmi_is_transport_atomic; in scmi_probe()
2719 if (desc->ops->link_supplier) { in scmi_probe()
2720 ret = desc->ops->link_supplier(dev); in scmi_probe()
2730 ret = bus_register_notifier(&scmi_bus_type, &info->bus_nb); in scmi_probe()
2735 &info->dev_req_nb); in scmi_probe()
2744 info->dbg = scmi_debugfs_common_setup(info); in scmi_probe()
2745 if (!info->dbg) in scmi_probe()
2766 if (info->desc->atomic_enabled && in scmi_probe()
2767 !is_transport_polling_capable(info->desc)) in scmi_probe()
2772 * Trigger SCMI Base protocol initialization. in scmi_probe()
2785 list_add_tail(&info->node, &scmi_list); in scmi_probe()
2795 dev_err(dev, "Out of range protocol %d\n", prot_id); in scmi_probe()
2798 dev_err(dev, "SCMI protocol %d not implemented\n", in scmi_probe()
2804 * Save this valid DT protocol descriptor amongst in scmi_probe()
2807 ret = idr_alloc(&info->active_protocols, child, in scmi_probe()
2810 dev_err(dev, "SCMI protocol %d already activated. Skip\n", in scmi_probe()
2823 scmi_raw_mode_cleanup(info->raw); in scmi_probe()
2824 scmi_notification_exit(&info->handle); in scmi_probe()
2827 &info->dev_req_nb); in scmi_probe()
2829 bus_unregister_notifier(&scmi_bus_type, &info->bus_nb); in scmi_probe()
2833 ida_free(&scmi_id, info->id); in scmi_probe()
2839 int id; in scmi_remove() local
2844 scmi_raw_mode_cleanup(info->raw); in scmi_remove()
2847 if (info->users) in scmi_remove()
2848 dev_warn(&pdev->dev, in scmi_remove()
2850 list_del(&info->node); in scmi_remove()
2853 scmi_notification_exit(&info->handle); in scmi_remove()
2855 mutex_lock(&info->protocols_mtx); in scmi_remove()
2856 idr_destroy(&info->protocols); in scmi_remove()
2857 mutex_unlock(&info->protocols_mtx); in scmi_remove()
2859 idr_for_each_entry(&info->active_protocols, child, id) in scmi_remove()
2861 idr_destroy(&info->active_protocols); in scmi_remove()
2864 &info->dev_req_nb); in scmi_remove()
2865 bus_unregister_notifier(&scmi_bus_type, &info->bus_nb); in scmi_remove()
2870 ida_free(&scmi_id, info->id); in scmi_remove()
2878 return sprintf(buf, "%u.%u\n", info->version.major_ver, in protocol_version_show()
2879 info->version.minor_ver); in protocol_version_show()
2888 return sprintf(buf, "0x%x\n", info->version.impl_ver); in firmware_version_show()
2897 return sprintf(buf, "%s\n", info->version.vendor_id); in vendor_id_show()
2906 return sprintf(buf, "%s\n", info->version.sub_vendor_id); in sub_vendor_id_show()
2925 { .compatible = "linaro,scmi-optee", .data = &scmi_optee_desc },
2928 { .compatible = "arm,scmi-smc", .data = &scmi_smc_desc},
2929 { .compatible = "arm,scmi-smc-param", .data = &scmi_smc_desc},
2930 { .compatible = "qcom,scmi-smc", .data = &scmi_smc_desc},
2933 { .compatible = "arm,scmi-virtio", .data = &scmi_virtio_desc},
2942 .name = "arm-scmi",
2952 * __scmi_transports_setup - Common helper to call transport-specific
2967 for (trans = scmi_of_match; trans->data; trans++) { in __scmi_transports_setup()
2968 const struct scmi_desc *tdesc = trans->data; in __scmi_transports_setup()
2970 if ((init && !tdesc->transport_init) || in __scmi_transports_setup()
2971 (!init && !tdesc->transport_exit)) in __scmi_transports_setup()
2975 ret = tdesc->transport_init(); in __scmi_transports_setup()
2977 tdesc->transport_exit(); in __scmi_transports_setup()
2981 trans->compatible); in __scmi_transports_setup()
3018 return -EINVAL; in scmi_driver_init()
3020 /* Initialize any compiled-in transport which provided an init/exit */ in scmi_driver_init()
3064 MODULE_ALIAS("platform:arm-scmi");
3066 MODULE_DESCRIPTION("ARM SCMI protocol driver");