1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments System Control Interface Protocol Driver
5 * Copyright (C) 2015-2022 Texas Instruments Incorporated - https://www.ti.com/
9 #define pr_fmt(fmt) "%s: " fmt, __func__
11 #include <linux/bitmap.h>
12 #include <linux/debugfs.h>
13 #include <linux/export.h>
15 #include <linux/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/mailbox_client.h>
18 #include <linux/module.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
23 #include <linux/semaphore.h>
24 #include <linux/slab.h>
25 #include <linux/soc/ti/ti-msgmgr.h>
26 #include <linux/soc/ti/ti_sci_protocol.h>
27 #include <linux/reboot.h>
31 /* List of all TI SCI devices active in system */
32 static LIST_HEAD(ti_sci_list);
33 /* Protection for the entire list */
34 static DEFINE_MUTEX(ti_sci_list_mutex);
37 * struct ti_sci_xfer - Structure representing a message flow
38 * @tx_message: Transmit message
39 * @rx_len: Receive message length
40 * @xfer_buf: Preallocated buffer to store receive message
41 * Since we work with request-ACK protocol, we can
42 * reuse the same buffer for the rx path as we
43 * use for the tx path.
44 * @done: completion event
47 struct ti_msgmgr_message tx_message;
50 struct completion done;
54 * struct ti_sci_xfers_info - Structure to manage transfer information
55 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
57 * @xfer_block: Preallocated Message array
58 * @xfer_alloc_table: Bitmap table for allocated messages.
59 * Index of this bitmap table is also used for message
60 * sequence identifier.
61 * @xfer_lock: Protection for message allocation
63 struct ti_sci_xfers_info {
64 struct semaphore sem_xfer_count;
65 struct ti_sci_xfer *xfer_block;
66 unsigned long *xfer_alloc_table;
67 /* protect transfer allocation */
72 * struct ti_sci_desc - Description of SoC integration
73 * @default_host_id: Host identifier representing the compute entity
74 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
75 * @max_msgs: Maximum number of messages that can be pending
76 * simultaneously in the system
77 * @max_msg_size: Maximum size of data per message that can be handled.
81 int max_rx_timeout_ms;
87 * struct ti_sci_info - Structure representing a TI SCI instance
88 * @dev: Device pointer
89 * @desc: SoC description for this instance
90 * @d: Debugfs file entry
91 * @debug_region: Memory region where the debug message are available
92 * @debug_region_size: Debug region size
93 * @debug_buffer: Buffer allocated to copy debug messages.
94 * @handle: Instance of TI SCI handle to send to clients.
96 * @chan_tx: Transmit mailbox channel
97 * @chan_rx: Receive mailbox channel
98 * @minfo: Message info
101 * @users: Number of users of this instance
105 const struct ti_sci_desc *desc;
107 void __iomem *debug_region;
109 size_t debug_region_size;
110 struct ti_sci_handle handle;
111 struct mbox_client cl;
112 struct mbox_chan *chan_tx;
113 struct mbox_chan *chan_rx;
114 struct ti_sci_xfers_info minfo;
115 struct list_head node;
117 /* protected by ti_sci_list_mutex */
121 #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
122 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
124 #ifdef CONFIG_DEBUG_FS
127 * ti_sci_debug_show() - Helper to dump the debug log
128 * @s: sequence file pointer
133 static int ti_sci_debug_show(struct seq_file *s, void *unused)
135 struct ti_sci_info *info = s->private;
137 memcpy_fromio(info->debug_buffer, info->debug_region,
138 info->debug_region_size);
140 * We don't trust firmware to leave NULL terminated last byte (hence
141 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
142 * specific data format for debug messages, We just present the data
143 * in the buffer as is - we expect the messages to be self explanatory.
145 seq_puts(s, info->debug_buffer);
149 /* Provide the log file operations interface*/
150 DEFINE_SHOW_ATTRIBUTE(ti_sci_debug);
153 * ti_sci_debugfs_create() - Create log debug file
154 * @pdev: platform device pointer
155 * @info: Pointer to SCI entity information
157 * Return: 0 if all went fine, else corresponding error.
159 static int ti_sci_debugfs_create(struct platform_device *pdev,
160 struct ti_sci_info *info)
162 struct device *dev = &pdev->dev;
163 struct resource *res;
166 /* Debug region is optional */
167 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
169 info->debug_region = devm_ioremap_resource(dev, res);
170 if (IS_ERR(info->debug_region))
172 info->debug_region_size = resource_size(res);
174 info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
175 sizeof(char), GFP_KERNEL);
176 if (!info->debug_buffer)
178 /* Setup NULL termination */
179 info->debug_buffer[info->debug_region_size] = 0;
181 snprintf(debug_name, sizeof(debug_name), "ti_sci_debug@%s",
183 info->d = debugfs_create_file(debug_name, 0444, NULL, info,
186 return PTR_ERR(info->d);
188 dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
189 info->debug_region, info->debug_region_size, res);
193 #else /* CONFIG_DEBUG_FS */
194 static inline int ti_sci_debugfs_create(struct platform_device *dev,
195 struct ti_sci_info *info)
200 static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
201 struct ti_sci_info *info)
204 #endif /* CONFIG_DEBUG_FS */
207 * ti_sci_dump_header_dbg() - Helper to dump a message header.
208 * @dev: Device pointer corresponding to the SCI entity
209 * @hdr: pointer to header.
211 static inline void ti_sci_dump_header_dbg(struct device *dev,
212 struct ti_sci_msg_hdr *hdr)
214 dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
215 hdr->type, hdr->host, hdr->seq, hdr->flags);
219 * ti_sci_rx_callback() - mailbox client callback for receive messages
220 * @cl: client pointer
221 * @m: mailbox message
223 * Processes one received message to appropriate transfer information and
224 * signals completion of the transfer.
226 * NOTE: This function will be invoked in IRQ context, hence should be
227 * as optimal as possible.
229 static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
231 struct ti_sci_info *info = cl_to_ti_sci_info(cl);
232 struct device *dev = info->dev;
233 struct ti_sci_xfers_info *minfo = &info->minfo;
234 struct ti_msgmgr_message *mbox_msg = m;
235 struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
236 struct ti_sci_xfer *xfer;
242 * Are we even expecting this?
243 * NOTE: barriers were implicit in locks used for modifying the bitmap
245 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
246 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
250 xfer = &minfo->xfer_block[xfer_id];
252 /* Is the message of valid length? */
253 if (mbox_msg->len > info->desc->max_msg_size) {
254 dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
255 mbox_msg->len, info->desc->max_msg_size);
256 ti_sci_dump_header_dbg(dev, hdr);
259 if (mbox_msg->len < xfer->rx_len) {
260 dev_err(dev, "Recv xfer %zu < expected %d length\n",
261 mbox_msg->len, xfer->rx_len);
262 ti_sci_dump_header_dbg(dev, hdr);
266 ti_sci_dump_header_dbg(dev, hdr);
267 /* Take a copy to the rx buffer.. */
268 memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
269 complete(&xfer->done);
273 * ti_sci_get_one_xfer() - Allocate one message
274 * @info: Pointer to SCI entity information
275 * @msg_type: Message type
276 * @msg_flags: Flag to set for the message
277 * @tx_message_size: transmit message size
278 * @rx_message_size: receive message size
280 * Helper function which is used by various command functions that are
281 * exposed to clients of this driver for allocating a message traffic event.
283 * This function can sleep depending on pending requests already in the system
284 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
285 * of internal data structures.
287 * Return: 0 if all went fine, else corresponding error.
289 static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
290 u16 msg_type, u32 msg_flags,
291 size_t tx_message_size,
292 size_t rx_message_size)
294 struct ti_sci_xfers_info *minfo = &info->minfo;
295 struct ti_sci_xfer *xfer;
296 struct ti_sci_msg_hdr *hdr;
298 unsigned long bit_pos;
303 /* Ensure we have sane transfer sizes */
304 if (rx_message_size > info->desc->max_msg_size ||
305 tx_message_size > info->desc->max_msg_size ||
306 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
307 return ERR_PTR(-ERANGE);
310 * Ensure we have only controlled number of pending messages.
311 * Ideally, we might just have to wait a single message, be
312 * conservative and wait 5 times that..
314 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
315 ret = down_timeout(&minfo->sem_xfer_count, timeout);
319 /* Keep the locked section as small as possible */
320 spin_lock_irqsave(&minfo->xfer_lock, flags);
321 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
322 info->desc->max_msgs);
323 set_bit(bit_pos, minfo->xfer_alloc_table);
324 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
327 * We already ensured in probe that we can have max messages that can
328 * fit in hdr.seq - NOTE: this improves access latencies
329 * to predictable O(1) access, BUT, it opens us to risk if
330 * remote misbehaves with corrupted message sequence responses.
331 * If that happens, we are going to be messed up anyways..
333 xfer_id = (u8)bit_pos;
335 xfer = &minfo->xfer_block[xfer_id];
337 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
338 xfer->tx_message.len = tx_message_size;
339 xfer->tx_message.chan_rx = info->chan_rx;
340 xfer->tx_message.timeout_rx_ms = info->desc->max_rx_timeout_ms;
341 xfer->rx_len = (u8)rx_message_size;
343 reinit_completion(&xfer->done);
346 hdr->type = msg_type;
347 hdr->host = info->host_id;
348 hdr->flags = msg_flags;
354 * ti_sci_put_one_xfer() - Release a message
355 * @minfo: transfer info pointer
356 * @xfer: message that was reserved by ti_sci_get_one_xfer
358 * This holds a spinlock to maintain integrity of internal data structures.
360 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
361 struct ti_sci_xfer *xfer)
364 struct ti_sci_msg_hdr *hdr;
367 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
371 * Keep the locked section as small as possible
372 * NOTE: we might escape with smp_mb and no lock here..
373 * but just be conservative and symmetric.
375 spin_lock_irqsave(&minfo->xfer_lock, flags);
376 clear_bit(xfer_id, minfo->xfer_alloc_table);
377 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
379 /* Increment the count for the next user to get through */
380 up(&minfo->sem_xfer_count);
384 * ti_sci_do_xfer() - Do one transfer
385 * @info: Pointer to SCI entity information
386 * @xfer: Transfer to initiate and wait for response
388 * Return: -ETIMEDOUT in case of no response, if transmit error,
389 * return corresponding error, else if all goes well,
392 static inline int ti_sci_do_xfer(struct ti_sci_info *info,
393 struct ti_sci_xfer *xfer)
397 struct device *dev = info->dev;
398 bool done_state = true;
400 ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
406 if (system_state <= SYSTEM_RUNNING) {
407 /* And we wait for the response. */
408 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
409 if (!wait_for_completion_timeout(&xfer->done, timeout))
413 * If we are !running, we cannot use wait_for_completion_timeout
414 * during noirq phase, so we must manually poll the completion.
416 ret = read_poll_timeout_atomic(try_wait_for_completion, done_state,
418 info->desc->max_rx_timeout_ms * 1000,
422 if (ret == -ETIMEDOUT)
423 dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
427 * NOTE: we might prefer not to need the mailbox ticker to manage the
428 * transfer queueing since the protocol layer queues things by itself.
429 * Unfortunately, we have to kick the mailbox framework after we have
430 * received our message.
432 mbox_client_txdone(info->chan_tx, ret);
438 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
439 * @info: Pointer to SCI entity information
441 * Updates the SCI information in the internal data structure.
443 * Return: 0 if all went fine, else return appropriate error.
445 static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
447 struct device *dev = info->dev;
448 struct ti_sci_handle *handle = &info->handle;
449 struct ti_sci_version_info *ver = &handle->version;
450 struct ti_sci_msg_resp_version *rev_info;
451 struct ti_sci_xfer *xfer;
454 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
455 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
456 sizeof(struct ti_sci_msg_hdr),
460 dev_err(dev, "Message alloc failed(%d)\n", ret);
464 rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
466 ret = ti_sci_do_xfer(info, xfer);
468 dev_err(dev, "Mbox send fail %d\n", ret);
472 ver->abi_major = rev_info->abi_major;
473 ver->abi_minor = rev_info->abi_minor;
474 ver->firmware_revision = rev_info->firmware_revision;
475 strscpy(ver->firmware_description, rev_info->firmware_description,
476 sizeof(ver->firmware_description));
479 ti_sci_put_one_xfer(&info->minfo, xfer);
484 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
485 * @r: pointer to response buffer
487 * Return: true if the response was an ACK, else returns false.
489 static inline bool ti_sci_is_response_ack(void *r)
491 struct ti_sci_msg_hdr *hdr = r;
493 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
497 * ti_sci_set_device_state() - Set device state helper
498 * @handle: pointer to TI SCI handle
499 * @id: Device identifier
500 * @flags: flags to setup for the device
501 * @state: State to move the device to
503 * Return: 0 if all went well, else returns appropriate error value.
505 static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
506 u32 id, u32 flags, u8 state)
508 struct ti_sci_info *info;
509 struct ti_sci_msg_req_set_device_state *req;
510 struct ti_sci_msg_hdr *resp;
511 struct ti_sci_xfer *xfer;
516 return PTR_ERR(handle);
520 info = handle_to_ti_sci_info(handle);
523 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
524 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
525 sizeof(*req), sizeof(*resp));
528 dev_err(dev, "Message alloc failed(%d)\n", ret);
531 req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
535 ret = ti_sci_do_xfer(info, xfer);
537 dev_err(dev, "Mbox send fail %d\n", ret);
541 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
543 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
546 ti_sci_put_one_xfer(&info->minfo, xfer);
552 * ti_sci_get_device_state() - Get device state helper
553 * @handle: Handle to the device
554 * @id: Device Identifier
555 * @clcnt: Pointer to Context Loss Count
556 * @resets: pointer to resets
557 * @p_state: pointer to p_state
558 * @c_state: pointer to c_state
560 * Return: 0 if all went fine, else return appropriate error.
562 static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
563 u32 id, u32 *clcnt, u32 *resets,
564 u8 *p_state, u8 *c_state)
566 struct ti_sci_info *info;
567 struct ti_sci_msg_req_get_device_state *req;
568 struct ti_sci_msg_resp_get_device_state *resp;
569 struct ti_sci_xfer *xfer;
574 return PTR_ERR(handle);
578 if (!clcnt && !resets && !p_state && !c_state)
581 info = handle_to_ti_sci_info(handle);
584 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
585 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
586 sizeof(*req), sizeof(*resp));
589 dev_err(dev, "Message alloc failed(%d)\n", ret);
592 req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
595 ret = ti_sci_do_xfer(info, xfer);
597 dev_err(dev, "Mbox send fail %d\n", ret);
601 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
602 if (!ti_sci_is_response_ack(resp)) {
608 *clcnt = resp->context_loss_count;
610 *resets = resp->resets;
612 *p_state = resp->programmed_state;
614 *c_state = resp->current_state;
616 ti_sci_put_one_xfer(&info->minfo, xfer);
622 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
623 * that can be shared with other hosts.
624 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
625 * @id: Device Identifier
627 * Request for the device - NOTE: the client MUST maintain integrity of
628 * usage count by balancing get_device with put_device. No refcounting is
629 * managed by driver for that purpose.
631 * Return: 0 if all went fine, else return appropriate error.
633 static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
635 return ti_sci_set_device_state(handle, id, 0,
636 MSG_DEVICE_SW_STATE_ON);
640 * ti_sci_cmd_get_device_exclusive() - command to request for device managed by
641 * TISCI that is exclusively owned by the
643 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
644 * @id: Device Identifier
646 * Request for the device - NOTE: the client MUST maintain integrity of
647 * usage count by balancing get_device with put_device. No refcounting is
648 * managed by driver for that purpose.
650 * Return: 0 if all went fine, else return appropriate error.
652 static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
655 return ti_sci_set_device_state(handle, id,
656 MSG_FLAG_DEVICE_EXCLUSIVE,
657 MSG_DEVICE_SW_STATE_ON);
661 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
662 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
663 * @id: Device Identifier
665 * Request for the device - NOTE: the client MUST maintain integrity of
666 * usage count by balancing get_device with put_device. No refcounting is
667 * managed by driver for that purpose.
669 * Return: 0 if all went fine, else return appropriate error.
671 static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
673 return ti_sci_set_device_state(handle, id, 0,
674 MSG_DEVICE_SW_STATE_RETENTION);
678 * ti_sci_cmd_idle_device_exclusive() - Command to idle a device managed by
679 * TISCI that is exclusively owned by
681 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
682 * @id: Device Identifier
684 * Request for the device - NOTE: the client MUST maintain integrity of
685 * usage count by balancing get_device with put_device. No refcounting is
686 * managed by driver for that purpose.
688 * Return: 0 if all went fine, else return appropriate error.
690 static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
693 return ti_sci_set_device_state(handle, id,
694 MSG_FLAG_DEVICE_EXCLUSIVE,
695 MSG_DEVICE_SW_STATE_RETENTION);
699 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
700 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
701 * @id: Device Identifier
703 * Request for the device - NOTE: the client MUST maintain integrity of
704 * usage count by balancing get_device with put_device. No refcounting is
705 * managed by driver for that purpose.
707 * Return: 0 if all went fine, else return appropriate error.
709 static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
711 return ti_sci_set_device_state(handle, id,
712 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
716 * ti_sci_cmd_dev_is_valid() - Is the device valid
717 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
718 * @id: Device Identifier
720 * Return: 0 if all went fine and the device ID is valid, else return
723 static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
727 /* check the device state which will also tell us if the ID is valid */
728 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
732 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
733 * @handle: Pointer to TISCI handle
734 * @id: Device Identifier
735 * @count: Pointer to Context Loss counter to populate
737 * Return: 0 if all went fine, else return appropriate error.
739 static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
742 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
746 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
747 * @handle: Pointer to TISCI handle
748 * @id: Device Identifier
749 * @r_state: true if requested to be idle
751 * Return: 0 if all went fine, else return appropriate error.
753 static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
762 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
766 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
772 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
773 * @handle: Pointer to TISCI handle
774 * @id: Device Identifier
775 * @r_state: true if requested to be stopped
776 * @curr_state: true if currently stopped.
778 * Return: 0 if all went fine, else return appropriate error.
780 static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
781 bool *r_state, bool *curr_state)
786 if (!r_state && !curr_state)
790 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
795 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
797 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
803 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
804 * @handle: Pointer to TISCI handle
805 * @id: Device Identifier
806 * @r_state: true if requested to be ON
807 * @curr_state: true if currently ON and active
809 * Return: 0 if all went fine, else return appropriate error.
811 static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
812 bool *r_state, bool *curr_state)
817 if (!r_state && !curr_state)
821 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
826 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
828 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
834 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
835 * @handle: Pointer to TISCI handle
836 * @id: Device Identifier
837 * @curr_state: true if currently transitioning.
839 * Return: 0 if all went fine, else return appropriate error.
841 static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
850 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
854 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
860 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
862 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
863 * @id: Device Identifier
864 * @reset_state: Device specific reset bit field
866 * Return: 0 if all went fine, else return appropriate error.
868 static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
869 u32 id, u32 reset_state)
871 struct ti_sci_info *info;
872 struct ti_sci_msg_req_set_device_resets *req;
873 struct ti_sci_msg_hdr *resp;
874 struct ti_sci_xfer *xfer;
879 return PTR_ERR(handle);
883 info = handle_to_ti_sci_info(handle);
886 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
887 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
888 sizeof(*req), sizeof(*resp));
891 dev_err(dev, "Message alloc failed(%d)\n", ret);
894 req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
896 req->resets = reset_state;
898 ret = ti_sci_do_xfer(info, xfer);
900 dev_err(dev, "Mbox send fail %d\n", ret);
904 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
906 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
909 ti_sci_put_one_xfer(&info->minfo, xfer);
915 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
917 * @handle: Pointer to TISCI handle
918 * @id: Device Identifier
919 * @reset_state: Pointer to reset state to populate
921 * Return: 0 if all went fine, else return appropriate error.
923 static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
924 u32 id, u32 *reset_state)
926 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
931 * ti_sci_set_clock_state() - Set clock state helper
932 * @handle: pointer to TI SCI handle
933 * @dev_id: Device identifier this request is for
934 * @clk_id: Clock identifier for the device for this request.
935 * Each device has it's own set of clock inputs. This indexes
936 * which clock input to modify.
937 * @flags: Header flags as needed
938 * @state: State to request for the clock.
940 * Return: 0 if all went well, else returns appropriate error value.
942 static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
943 u32 dev_id, u32 clk_id,
946 struct ti_sci_info *info;
947 struct ti_sci_msg_req_set_clock_state *req;
948 struct ti_sci_msg_hdr *resp;
949 struct ti_sci_xfer *xfer;
954 return PTR_ERR(handle);
958 info = handle_to_ti_sci_info(handle);
961 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
962 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
963 sizeof(*req), sizeof(*resp));
966 dev_err(dev, "Message alloc failed(%d)\n", ret);
969 req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
970 req->dev_id = dev_id;
972 req->clk_id = clk_id;
975 req->clk_id_32 = clk_id;
977 req->request_state = state;
979 ret = ti_sci_do_xfer(info, xfer);
981 dev_err(dev, "Mbox send fail %d\n", ret);
985 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
987 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
990 ti_sci_put_one_xfer(&info->minfo, xfer);
996 * ti_sci_cmd_get_clock_state() - Get clock state helper
997 * @handle: pointer to TI SCI handle
998 * @dev_id: Device identifier this request is for
999 * @clk_id: Clock identifier for the device for this request.
1000 * Each device has it's own set of clock inputs. This indexes
1001 * which clock input to modify.
1002 * @programmed_state: State requested for clock to move to
1003 * @current_state: State that the clock is currently in
1005 * Return: 0 if all went well, else returns appropriate error value.
1007 static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
1008 u32 dev_id, u32 clk_id,
1009 u8 *programmed_state, u8 *current_state)
1011 struct ti_sci_info *info;
1012 struct ti_sci_msg_req_get_clock_state *req;
1013 struct ti_sci_msg_resp_get_clock_state *resp;
1014 struct ti_sci_xfer *xfer;
1019 return PTR_ERR(handle);
1023 if (!programmed_state && !current_state)
1026 info = handle_to_ti_sci_info(handle);
1029 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1030 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1031 sizeof(*req), sizeof(*resp));
1033 ret = PTR_ERR(xfer);
1034 dev_err(dev, "Message alloc failed(%d)\n", ret);
1037 req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
1038 req->dev_id = dev_id;
1040 req->clk_id = clk_id;
1043 req->clk_id_32 = clk_id;
1046 ret = ti_sci_do_xfer(info, xfer);
1048 dev_err(dev, "Mbox send fail %d\n", ret);
1052 resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
1054 if (!ti_sci_is_response_ack(resp)) {
1059 if (programmed_state)
1060 *programmed_state = resp->programmed_state;
1062 *current_state = resp->current_state;
1065 ti_sci_put_one_xfer(&info->minfo, xfer);
1071 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1072 * @handle: pointer to TI SCI handle
1073 * @dev_id: Device identifier this request is for
1074 * @clk_id: Clock identifier for the device for this request.
1075 * Each device has it's own set of clock inputs. This indexes
1076 * which clock input to modify.
1077 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1078 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1079 * @enable_input_term: 'true' if input termination is desired, else 'false'
1081 * Return: 0 if all went well, else returns appropriate error value.
1083 static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1084 u32 clk_id, bool needs_ssc,
1085 bool can_change_freq, bool enable_input_term)
1089 flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1090 flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1091 flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1093 return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1094 MSG_CLOCK_SW_STATE_REQ);
1098 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1099 * @handle: pointer to TI SCI handle
1100 * @dev_id: Device identifier this request is for
1101 * @clk_id: Clock identifier for the device for this request.
1102 * Each device has it's own set of clock inputs. This indexes
1103 * which clock input to modify.
1105 * NOTE: This clock must have been requested by get_clock previously.
1107 * Return: 0 if all went well, else returns appropriate error value.
1109 static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1110 u32 dev_id, u32 clk_id)
1112 return ti_sci_set_clock_state(handle, dev_id, clk_id,
1113 MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
1114 MSG_CLOCK_SW_STATE_UNREQ);
1118 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1119 * @handle: pointer to TI SCI handle
1120 * @dev_id: Device identifier this request is for
1121 * @clk_id: Clock identifier for the device for this request.
1122 * Each device has it's own set of clock inputs. This indexes
1123 * which clock input to modify.
1125 * NOTE: This clock must have been requested by get_clock previously.
1127 * Return: 0 if all went well, else returns appropriate error value.
1129 static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1130 u32 dev_id, u32 clk_id)
1132 return ti_sci_set_clock_state(handle, dev_id, clk_id,
1133 MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
1134 MSG_CLOCK_SW_STATE_AUTO);
1138 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1139 * @handle: pointer to TI SCI handle
1140 * @dev_id: Device identifier this request is for
1141 * @clk_id: Clock identifier for the device for this request.
1142 * Each device has it's own set of clock inputs. This indexes
1143 * which clock input to modify.
1144 * @req_state: state indicating if the clock is auto managed
1146 * Return: 0 if all went well, else returns appropriate error value.
1148 static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1149 u32 dev_id, u32 clk_id, bool *req_state)
1157 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1161 *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1166 * ti_sci_cmd_clk_is_on() - Is the clock ON
1167 * @handle: pointer to TI SCI handle
1168 * @dev_id: Device identifier this request is for
1169 * @clk_id: Clock identifier for the device for this request.
1170 * Each device has it's own set of clock inputs. This indexes
1171 * which clock input to modify.
1172 * @req_state: state indicating if the clock is managed by us and enabled
1173 * @curr_state: state indicating if the clock is ready for operation
1175 * Return: 0 if all went well, else returns appropriate error value.
1177 static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1178 u32 clk_id, bool *req_state, bool *curr_state)
1180 u8 c_state = 0, r_state = 0;
1183 if (!req_state && !curr_state)
1186 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1187 &r_state, &c_state);
1192 *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1194 *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1199 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1200 * @handle: pointer to TI SCI handle
1201 * @dev_id: Device identifier this request is for
1202 * @clk_id: Clock identifier for the device for this request.
1203 * Each device has it's own set of clock inputs. This indexes
1204 * which clock input to modify.
1205 * @req_state: state indicating if the clock is managed by us and disabled
1206 * @curr_state: state indicating if the clock is NOT ready for operation
1208 * Return: 0 if all went well, else returns appropriate error value.
1210 static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1211 u32 clk_id, bool *req_state, bool *curr_state)
1213 u8 c_state = 0, r_state = 0;
1216 if (!req_state && !curr_state)
1219 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1220 &r_state, &c_state);
1225 *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1227 *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1232 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1233 * @handle: pointer to TI SCI handle
1234 * @dev_id: Device identifier this request is for
1235 * @clk_id: Clock identifier for the device for this request.
1236 * Each device has it's own set of clock inputs. This indexes
1237 * which clock input to modify.
1238 * @parent_id: Parent clock identifier to set
1240 * Return: 0 if all went well, else returns appropriate error value.
1242 static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1243 u32 dev_id, u32 clk_id, u32 parent_id)
1245 struct ti_sci_info *info;
1246 struct ti_sci_msg_req_set_clock_parent *req;
1247 struct ti_sci_msg_hdr *resp;
1248 struct ti_sci_xfer *xfer;
1253 return PTR_ERR(handle);
1257 info = handle_to_ti_sci_info(handle);
1260 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1261 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1262 sizeof(*req), sizeof(*resp));
1264 ret = PTR_ERR(xfer);
1265 dev_err(dev, "Message alloc failed(%d)\n", ret);
1268 req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
1269 req->dev_id = dev_id;
1271 req->clk_id = clk_id;
1274 req->clk_id_32 = clk_id;
1276 if (parent_id < 255) {
1277 req->parent_id = parent_id;
1279 req->parent_id = 255;
1280 req->parent_id_32 = parent_id;
1283 ret = ti_sci_do_xfer(info, xfer);
1285 dev_err(dev, "Mbox send fail %d\n", ret);
1289 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1291 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1294 ti_sci_put_one_xfer(&info->minfo, xfer);
1300 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1301 * @handle: pointer to TI SCI handle
1302 * @dev_id: Device identifier this request is for
1303 * @clk_id: Clock identifier for the device for this request.
1304 * Each device has it's own set of clock inputs. This indexes
1305 * which clock input to modify.
1306 * @parent_id: Current clock parent
1308 * Return: 0 if all went well, else returns appropriate error value.
1310 static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1311 u32 dev_id, u32 clk_id, u32 *parent_id)
1313 struct ti_sci_info *info;
1314 struct ti_sci_msg_req_get_clock_parent *req;
1315 struct ti_sci_msg_resp_get_clock_parent *resp;
1316 struct ti_sci_xfer *xfer;
1321 return PTR_ERR(handle);
1322 if (!handle || !parent_id)
1325 info = handle_to_ti_sci_info(handle);
1328 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1329 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1330 sizeof(*req), sizeof(*resp));
1332 ret = PTR_ERR(xfer);
1333 dev_err(dev, "Message alloc failed(%d)\n", ret);
1336 req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
1337 req->dev_id = dev_id;
1339 req->clk_id = clk_id;
1342 req->clk_id_32 = clk_id;
1345 ret = ti_sci_do_xfer(info, xfer);
1347 dev_err(dev, "Mbox send fail %d\n", ret);
1351 resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
1353 if (!ti_sci_is_response_ack(resp)) {
1356 if (resp->parent_id < 255)
1357 *parent_id = resp->parent_id;
1359 *parent_id = resp->parent_id_32;
1363 ti_sci_put_one_xfer(&info->minfo, xfer);
1369 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1370 * @handle: pointer to TI SCI handle
1371 * @dev_id: Device identifier this request is for
1372 * @clk_id: Clock identifier for the device for this request.
1373 * Each device has it's own set of clock inputs. This indexes
1374 * which clock input to modify.
1375 * @num_parents: Returns he number of parents to the current clock.
1377 * Return: 0 if all went well, else returns appropriate error value.
1379 static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1380 u32 dev_id, u32 clk_id,
1383 struct ti_sci_info *info;
1384 struct ti_sci_msg_req_get_clock_num_parents *req;
1385 struct ti_sci_msg_resp_get_clock_num_parents *resp;
1386 struct ti_sci_xfer *xfer;
1391 return PTR_ERR(handle);
1392 if (!handle || !num_parents)
1395 info = handle_to_ti_sci_info(handle);
1398 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1399 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1400 sizeof(*req), sizeof(*resp));
1402 ret = PTR_ERR(xfer);
1403 dev_err(dev, "Message alloc failed(%d)\n", ret);
1406 req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
1407 req->dev_id = dev_id;
1409 req->clk_id = clk_id;
1412 req->clk_id_32 = clk_id;
1415 ret = ti_sci_do_xfer(info, xfer);
1417 dev_err(dev, "Mbox send fail %d\n", ret);
1421 resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
1423 if (!ti_sci_is_response_ack(resp)) {
1426 if (resp->num_parents < 255)
1427 *num_parents = resp->num_parents;
1429 *num_parents = resp->num_parents_32;
1433 ti_sci_put_one_xfer(&info->minfo, xfer);
1439 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1440 * @handle: pointer to TI SCI handle
1441 * @dev_id: Device identifier this request is for
1442 * @clk_id: Clock identifier for the device for this request.
1443 * Each device has it's own set of clock inputs. This indexes
1444 * which clock input to modify.
1445 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1446 * allowable programmed frequency and does not account for clock
1447 * tolerances and jitter.
1448 * @target_freq: The target clock frequency in Hz. A frequency will be
1449 * processed as close to this target frequency as possible.
1450 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1451 * allowable programmed frequency and does not account for clock
1452 * tolerances and jitter.
1453 * @match_freq: Frequency match in Hz response.
1455 * Return: 0 if all went well, else returns appropriate error value.
1457 static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1458 u32 dev_id, u32 clk_id, u64 min_freq,
1459 u64 target_freq, u64 max_freq,
1462 struct ti_sci_info *info;
1463 struct ti_sci_msg_req_query_clock_freq *req;
1464 struct ti_sci_msg_resp_query_clock_freq *resp;
1465 struct ti_sci_xfer *xfer;
1470 return PTR_ERR(handle);
1471 if (!handle || !match_freq)
1474 info = handle_to_ti_sci_info(handle);
1477 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1478 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1479 sizeof(*req), sizeof(*resp));
1481 ret = PTR_ERR(xfer);
1482 dev_err(dev, "Message alloc failed(%d)\n", ret);
1485 req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
1486 req->dev_id = dev_id;
1488 req->clk_id = clk_id;
1491 req->clk_id_32 = clk_id;
1493 req->min_freq_hz = min_freq;
1494 req->target_freq_hz = target_freq;
1495 req->max_freq_hz = max_freq;
1497 ret = ti_sci_do_xfer(info, xfer);
1499 dev_err(dev, "Mbox send fail %d\n", ret);
1503 resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
1505 if (!ti_sci_is_response_ack(resp))
1508 *match_freq = resp->freq_hz;
1511 ti_sci_put_one_xfer(&info->minfo, xfer);
1517 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1518 * @handle: pointer to TI SCI handle
1519 * @dev_id: Device identifier this request is for
1520 * @clk_id: Clock identifier for the device for this request.
1521 * Each device has it's own set of clock inputs. This indexes
1522 * which clock input to modify.
1523 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1524 * allowable programmed frequency and does not account for clock
1525 * tolerances and jitter.
1526 * @target_freq: The target clock frequency in Hz. A frequency will be
1527 * processed as close to this target frequency as possible.
1528 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1529 * allowable programmed frequency and does not account for clock
1530 * tolerances and jitter.
1532 * Return: 0 if all went well, else returns appropriate error value.
1534 static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1535 u32 dev_id, u32 clk_id, u64 min_freq,
1536 u64 target_freq, u64 max_freq)
1538 struct ti_sci_info *info;
1539 struct ti_sci_msg_req_set_clock_freq *req;
1540 struct ti_sci_msg_hdr *resp;
1541 struct ti_sci_xfer *xfer;
1546 return PTR_ERR(handle);
1550 info = handle_to_ti_sci_info(handle);
1553 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1554 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1555 sizeof(*req), sizeof(*resp));
1557 ret = PTR_ERR(xfer);
1558 dev_err(dev, "Message alloc failed(%d)\n", ret);
1561 req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
1562 req->dev_id = dev_id;
1564 req->clk_id = clk_id;
1567 req->clk_id_32 = clk_id;
1569 req->min_freq_hz = min_freq;
1570 req->target_freq_hz = target_freq;
1571 req->max_freq_hz = max_freq;
1573 ret = ti_sci_do_xfer(info, xfer);
1575 dev_err(dev, "Mbox send fail %d\n", ret);
1579 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1581 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1584 ti_sci_put_one_xfer(&info->minfo, xfer);
1590 * ti_sci_cmd_clk_get_freq() - Get current frequency
1591 * @handle: pointer to TI SCI handle
1592 * @dev_id: Device identifier this request is for
1593 * @clk_id: Clock identifier for the device for this request.
1594 * Each device has it's own set of clock inputs. This indexes
1595 * which clock input to modify.
1596 * @freq: Currently frequency in Hz
1598 * Return: 0 if all went well, else returns appropriate error value.
1600 static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1601 u32 dev_id, u32 clk_id, u64 *freq)
1603 struct ti_sci_info *info;
1604 struct ti_sci_msg_req_get_clock_freq *req;
1605 struct ti_sci_msg_resp_get_clock_freq *resp;
1606 struct ti_sci_xfer *xfer;
1611 return PTR_ERR(handle);
1612 if (!handle || !freq)
1615 info = handle_to_ti_sci_info(handle);
1618 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1619 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1620 sizeof(*req), sizeof(*resp));
1622 ret = PTR_ERR(xfer);
1623 dev_err(dev, "Message alloc failed(%d)\n", ret);
1626 req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
1627 req->dev_id = dev_id;
1629 req->clk_id = clk_id;
1632 req->clk_id_32 = clk_id;
1635 ret = ti_sci_do_xfer(info, xfer);
1637 dev_err(dev, "Mbox send fail %d\n", ret);
1641 resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
1643 if (!ti_sci_is_response_ack(resp))
1646 *freq = resp->freq_hz;
1649 ti_sci_put_one_xfer(&info->minfo, xfer);
1654 static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1656 struct ti_sci_info *info;
1657 struct ti_sci_msg_req_reboot *req;
1658 struct ti_sci_msg_hdr *resp;
1659 struct ti_sci_xfer *xfer;
1664 return PTR_ERR(handle);
1668 info = handle_to_ti_sci_info(handle);
1671 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1672 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1673 sizeof(*req), sizeof(*resp));
1675 ret = PTR_ERR(xfer);
1676 dev_err(dev, "Message alloc failed(%d)\n", ret);
1679 req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1681 ret = ti_sci_do_xfer(info, xfer);
1683 dev_err(dev, "Mbox send fail %d\n", ret);
1687 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1689 if (!ti_sci_is_response_ack(resp))
1695 ti_sci_put_one_xfer(&info->minfo, xfer);
1701 * ti_sci_get_resource_range - Helper to get a range of resources assigned
1702 * to a host. Resource is uniquely identified by
1704 * @handle: Pointer to TISCI handle.
1705 * @dev_id: TISCI device ID.
1706 * @subtype: Resource assignment subtype that is being requested
1707 * from the given device.
1708 * @s_host: Host processor ID to which the resources are allocated
1709 * @desc: Pointer to ti_sci_resource_desc to be updated with the
1710 * resource range start index and number of resources
1712 * Return: 0 if all went fine, else return appropriate error.
1714 static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
1715 u32 dev_id, u8 subtype, u8 s_host,
1716 struct ti_sci_resource_desc *desc)
1718 struct ti_sci_msg_resp_get_resource_range *resp;
1719 struct ti_sci_msg_req_get_resource_range *req;
1720 struct ti_sci_xfer *xfer;
1721 struct ti_sci_info *info;
1726 return PTR_ERR(handle);
1727 if (!handle || !desc)
1730 info = handle_to_ti_sci_info(handle);
1733 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
1734 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1735 sizeof(*req), sizeof(*resp));
1737 ret = PTR_ERR(xfer);
1738 dev_err(dev, "Message alloc failed(%d)\n", ret);
1742 req = (struct ti_sci_msg_req_get_resource_range *)xfer->xfer_buf;
1743 req->secondary_host = s_host;
1744 req->type = dev_id & MSG_RM_RESOURCE_TYPE_MASK;
1745 req->subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
1747 ret = ti_sci_do_xfer(info, xfer);
1749 dev_err(dev, "Mbox send fail %d\n", ret);
1753 resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->xfer_buf;
1755 if (!ti_sci_is_response_ack(resp)) {
1757 } else if (!resp->range_num && !resp->range_num_sec) {
1758 /* Neither of the two resource range is valid */
1761 desc->start = resp->range_start;
1762 desc->num = resp->range_num;
1763 desc->start_sec = resp->range_start_sec;
1764 desc->num_sec = resp->range_num_sec;
1768 ti_sci_put_one_xfer(&info->minfo, xfer);
1774 * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
1775 * that is same as ti sci interface host.
1776 * @handle: Pointer to TISCI handle.
1777 * @dev_id: TISCI device ID.
1778 * @subtype: Resource assignment subtype that is being requested
1779 * from the given device.
1780 * @desc: Pointer to ti_sci_resource_desc to be updated with the
1781 * resource range start index and number of resources
1783 * Return: 0 if all went fine, else return appropriate error.
1785 static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
1786 u32 dev_id, u8 subtype,
1787 struct ti_sci_resource_desc *desc)
1789 return ti_sci_get_resource_range(handle, dev_id, subtype,
1790 TI_SCI_IRQ_SECONDARY_HOST_INVALID,
1795 * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
1796 * assigned to a specified host.
1797 * @handle: Pointer to TISCI handle.
1798 * @dev_id: TISCI device ID.
1799 * @subtype: Resource assignment subtype that is being requested
1800 * from the given device.
1801 * @s_host: Host processor ID to which the resources are allocated
1802 * @desc: Pointer to ti_sci_resource_desc to be updated with the
1803 * resource range start index and number of resources
1805 * Return: 0 if all went fine, else return appropriate error.
1808 int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
1809 u32 dev_id, u8 subtype, u8 s_host,
1810 struct ti_sci_resource_desc *desc)
1812 return ti_sci_get_resource_range(handle, dev_id, subtype, s_host, desc);
1816 * ti_sci_manage_irq() - Helper api to configure/release the irq route between
1817 * the requested source and destination
1818 * @handle: Pointer to TISCI handle.
1819 * @valid_params: Bit fields defining the validity of certain params
1820 * @src_id: Device ID of the IRQ source
1821 * @src_index: IRQ source index within the source device
1822 * @dst_id: Device ID of the IRQ destination
1823 * @dst_host_irq: IRQ number of the destination device
1824 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1825 * @vint: Virtual interrupt to be used within the IA
1826 * @global_event: Global event number to be used for the requesting event
1827 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1828 * @s_host: Secondary host ID to which the irq/event is being
1830 * @type: Request type irq set or release.
1832 * Return: 0 if all went fine, else return appropriate error.
1834 static int ti_sci_manage_irq(const struct ti_sci_handle *handle,
1835 u32 valid_params, u16 src_id, u16 src_index,
1836 u16 dst_id, u16 dst_host_irq, u16 ia_id, u16 vint,
1837 u16 global_event, u8 vint_status_bit, u8 s_host,
1840 struct ti_sci_msg_req_manage_irq *req;
1841 struct ti_sci_msg_hdr *resp;
1842 struct ti_sci_xfer *xfer;
1843 struct ti_sci_info *info;
1848 return PTR_ERR(handle);
1852 info = handle_to_ti_sci_info(handle);
1855 xfer = ti_sci_get_one_xfer(info, type, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1856 sizeof(*req), sizeof(*resp));
1858 ret = PTR_ERR(xfer);
1859 dev_err(dev, "Message alloc failed(%d)\n", ret);
1862 req = (struct ti_sci_msg_req_manage_irq *)xfer->xfer_buf;
1863 req->valid_params = valid_params;
1864 req->src_id = src_id;
1865 req->src_index = src_index;
1866 req->dst_id = dst_id;
1867 req->dst_host_irq = dst_host_irq;
1870 req->global_event = global_event;
1871 req->vint_status_bit = vint_status_bit;
1872 req->secondary_host = s_host;
1874 ret = ti_sci_do_xfer(info, xfer);
1876 dev_err(dev, "Mbox send fail %d\n", ret);
1880 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1882 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1885 ti_sci_put_one_xfer(&info->minfo, xfer);
1891 * ti_sci_set_irq() - Helper api to configure the irq route between the
1892 * requested source and destination
1893 * @handle: Pointer to TISCI handle.
1894 * @valid_params: Bit fields defining the validity of certain params
1895 * @src_id: Device ID of the IRQ source
1896 * @src_index: IRQ source index within the source device
1897 * @dst_id: Device ID of the IRQ destination
1898 * @dst_host_irq: IRQ number of the destination device
1899 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1900 * @vint: Virtual interrupt to be used within the IA
1901 * @global_event: Global event number to be used for the requesting event
1902 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1903 * @s_host: Secondary host ID to which the irq/event is being
1906 * Return: 0 if all went fine, else return appropriate error.
1908 static int ti_sci_set_irq(const struct ti_sci_handle *handle, u32 valid_params,
1909 u16 src_id, u16 src_index, u16 dst_id,
1910 u16 dst_host_irq, u16 ia_id, u16 vint,
1911 u16 global_event, u8 vint_status_bit, u8 s_host)
1913 pr_debug("%s: IRQ set with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1914 __func__, valid_params, src_id, src_index,
1915 dst_id, dst_host_irq, ia_id, vint, global_event,
1918 return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1919 dst_id, dst_host_irq, ia_id, vint,
1920 global_event, vint_status_bit, s_host,
1921 TI_SCI_MSG_SET_IRQ);
1925 * ti_sci_free_irq() - Helper api to free the irq route between the
1926 * requested source and destination
1927 * @handle: Pointer to TISCI handle.
1928 * @valid_params: Bit fields defining the validity of certain params
1929 * @src_id: Device ID of the IRQ source
1930 * @src_index: IRQ source index within the source device
1931 * @dst_id: Device ID of the IRQ destination
1932 * @dst_host_irq: IRQ number of the destination device
1933 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1934 * @vint: Virtual interrupt to be used within the IA
1935 * @global_event: Global event number to be used for the requesting event
1936 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1937 * @s_host: Secondary host ID to which the irq/event is being
1940 * Return: 0 if all went fine, else return appropriate error.
1942 static int ti_sci_free_irq(const struct ti_sci_handle *handle, u32 valid_params,
1943 u16 src_id, u16 src_index, u16 dst_id,
1944 u16 dst_host_irq, u16 ia_id, u16 vint,
1945 u16 global_event, u8 vint_status_bit, u8 s_host)
1947 pr_debug("%s: IRQ release with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1948 __func__, valid_params, src_id, src_index,
1949 dst_id, dst_host_irq, ia_id, vint, global_event,
1952 return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1953 dst_id, dst_host_irq, ia_id, vint,
1954 global_event, vint_status_bit, s_host,
1955 TI_SCI_MSG_FREE_IRQ);
1959 * ti_sci_cmd_set_irq() - Configure a host irq route between the requested
1960 * source and destination.
1961 * @handle: Pointer to TISCI handle.
1962 * @src_id: Device ID of the IRQ source
1963 * @src_index: IRQ source index within the source device
1964 * @dst_id: Device ID of the IRQ destination
1965 * @dst_host_irq: IRQ number of the destination device
1967 * Return: 0 if all went fine, else return appropriate error.
1969 static int ti_sci_cmd_set_irq(const struct ti_sci_handle *handle, u16 src_id,
1970 u16 src_index, u16 dst_id, u16 dst_host_irq)
1972 u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
1974 return ti_sci_set_irq(handle, valid_params, src_id, src_index, dst_id,
1975 dst_host_irq, 0, 0, 0, 0, 0);
1979 * ti_sci_cmd_set_event_map() - Configure an event based irq route between the
1980 * requested source and Interrupt Aggregator.
1981 * @handle: Pointer to TISCI handle.
1982 * @src_id: Device ID of the IRQ source
1983 * @src_index: IRQ source index within the source device
1984 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1985 * @vint: Virtual interrupt to be used within the IA
1986 * @global_event: Global event number to be used for the requesting event
1987 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1989 * Return: 0 if all went fine, else return appropriate error.
1991 static int ti_sci_cmd_set_event_map(const struct ti_sci_handle *handle,
1992 u16 src_id, u16 src_index, u16 ia_id,
1993 u16 vint, u16 global_event,
1996 u32 valid_params = MSG_FLAG_IA_ID_VALID | MSG_FLAG_VINT_VALID |
1997 MSG_FLAG_GLB_EVNT_VALID |
1998 MSG_FLAG_VINT_STS_BIT_VALID;
2000 return ti_sci_set_irq(handle, valid_params, src_id, src_index, 0, 0,
2001 ia_id, vint, global_event, vint_status_bit, 0);
2005 * ti_sci_cmd_free_irq() - Free a host irq route between the between the
2006 * requested source and destination.
2007 * @handle: Pointer to TISCI handle.
2008 * @src_id: Device ID of the IRQ source
2009 * @src_index: IRQ source index within the source device
2010 * @dst_id: Device ID of the IRQ destination
2011 * @dst_host_irq: IRQ number of the destination device
2013 * Return: 0 if all went fine, else return appropriate error.
2015 static int ti_sci_cmd_free_irq(const struct ti_sci_handle *handle, u16 src_id,
2016 u16 src_index, u16 dst_id, u16 dst_host_irq)
2018 u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
2020 return ti_sci_free_irq(handle, valid_params, src_id, src_index, dst_id,
2021 dst_host_irq, 0, 0, 0, 0, 0);
2025 * ti_sci_cmd_free_event_map() - Free an event map between the requested source
2026 * and Interrupt Aggregator.
2027 * @handle: Pointer to TISCI handle.
2028 * @src_id: Device ID of the IRQ source
2029 * @src_index: IRQ source index within the source device
2030 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
2031 * @vint: Virtual interrupt to be used within the IA
2032 * @global_event: Global event number to be used for the requesting event
2033 * @vint_status_bit: Virtual interrupt status bit to be used for the event
2035 * Return: 0 if all went fine, else return appropriate error.
2037 static int ti_sci_cmd_free_event_map(const struct ti_sci_handle *handle,
2038 u16 src_id, u16 src_index, u16 ia_id,
2039 u16 vint, u16 global_event,
2042 u32 valid_params = MSG_FLAG_IA_ID_VALID |
2043 MSG_FLAG_VINT_VALID | MSG_FLAG_GLB_EVNT_VALID |
2044 MSG_FLAG_VINT_STS_BIT_VALID;
2046 return ti_sci_free_irq(handle, valid_params, src_id, src_index, 0, 0,
2047 ia_id, vint, global_event, vint_status_bit, 0);
2051 * ti_sci_cmd_rm_ring_cfg() - Configure a NAVSS ring
2052 * @handle: Pointer to TI SCI handle.
2053 * @params: Pointer to ti_sci_msg_rm_ring_cfg ring config structure
2055 * Return: 0 if all went well, else returns appropriate error value.
2057 * See @ti_sci_msg_rm_ring_cfg and @ti_sci_msg_rm_ring_cfg_req for
2060 static int ti_sci_cmd_rm_ring_cfg(const struct ti_sci_handle *handle,
2061 const struct ti_sci_msg_rm_ring_cfg *params)
2063 struct ti_sci_msg_rm_ring_cfg_req *req;
2064 struct ti_sci_msg_hdr *resp;
2065 struct ti_sci_xfer *xfer;
2066 struct ti_sci_info *info;
2070 if (IS_ERR_OR_NULL(handle))
2073 info = handle_to_ti_sci_info(handle);
2076 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
2077 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2078 sizeof(*req), sizeof(*resp));
2080 ret = PTR_ERR(xfer);
2081 dev_err(dev, "RM_RA:Message config failed(%d)\n", ret);
2084 req = (struct ti_sci_msg_rm_ring_cfg_req *)xfer->xfer_buf;
2085 req->valid_params = params->valid_params;
2086 req->nav_id = params->nav_id;
2087 req->index = params->index;
2088 req->addr_lo = params->addr_lo;
2089 req->addr_hi = params->addr_hi;
2090 req->count = params->count;
2091 req->mode = params->mode;
2092 req->size = params->size;
2093 req->order_id = params->order_id;
2094 req->virtid = params->virtid;
2095 req->asel = params->asel;
2097 ret = ti_sci_do_xfer(info, xfer);
2099 dev_err(dev, "RM_RA:Mbox config send fail %d\n", ret);
2103 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2104 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2107 ti_sci_put_one_xfer(&info->minfo, xfer);
2108 dev_dbg(dev, "RM_RA:config ring %u ret:%d\n", params->index, ret);
2113 * ti_sci_cmd_rm_psil_pair() - Pair PSI-L source to destination thread
2114 * @handle: Pointer to TI SCI handle.
2115 * @nav_id: Device ID of Navigator Subsystem which should be used for
2117 * @src_thread: Source PSI-L thread ID
2118 * @dst_thread: Destination PSI-L thread ID
2120 * Return: 0 if all went well, else returns appropriate error value.
2122 static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
2123 u32 nav_id, u32 src_thread, u32 dst_thread)
2125 struct ti_sci_msg_psil_pair *req;
2126 struct ti_sci_msg_hdr *resp;
2127 struct ti_sci_xfer *xfer;
2128 struct ti_sci_info *info;
2133 return PTR_ERR(handle);
2137 info = handle_to_ti_sci_info(handle);
2140 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
2141 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2142 sizeof(*req), sizeof(*resp));
2144 ret = PTR_ERR(xfer);
2145 dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2148 req = (struct ti_sci_msg_psil_pair *)xfer->xfer_buf;
2149 req->nav_id = nav_id;
2150 req->src_thread = src_thread;
2151 req->dst_thread = dst_thread;
2153 ret = ti_sci_do_xfer(info, xfer);
2155 dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2159 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2160 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2163 ti_sci_put_one_xfer(&info->minfo, xfer);
2169 * ti_sci_cmd_rm_psil_unpair() - Unpair PSI-L source from destination thread
2170 * @handle: Pointer to TI SCI handle.
2171 * @nav_id: Device ID of Navigator Subsystem which should be used for
2173 * @src_thread: Source PSI-L thread ID
2174 * @dst_thread: Destination PSI-L thread ID
2176 * Return: 0 if all went well, else returns appropriate error value.
2178 static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
2179 u32 nav_id, u32 src_thread, u32 dst_thread)
2181 struct ti_sci_msg_psil_unpair *req;
2182 struct ti_sci_msg_hdr *resp;
2183 struct ti_sci_xfer *xfer;
2184 struct ti_sci_info *info;
2189 return PTR_ERR(handle);
2193 info = handle_to_ti_sci_info(handle);
2196 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
2197 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2198 sizeof(*req), sizeof(*resp));
2200 ret = PTR_ERR(xfer);
2201 dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2204 req = (struct ti_sci_msg_psil_unpair *)xfer->xfer_buf;
2205 req->nav_id = nav_id;
2206 req->src_thread = src_thread;
2207 req->dst_thread = dst_thread;
2209 ret = ti_sci_do_xfer(info, xfer);
2211 dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2215 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2216 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2219 ti_sci_put_one_xfer(&info->minfo, xfer);
2225 * ti_sci_cmd_rm_udmap_tx_ch_cfg() - Configure a UDMAP TX channel
2226 * @handle: Pointer to TI SCI handle.
2227 * @params: Pointer to ti_sci_msg_rm_udmap_tx_ch_cfg TX channel config
2230 * Return: 0 if all went well, else returns appropriate error value.
2232 * See @ti_sci_msg_rm_udmap_tx_ch_cfg and @ti_sci_msg_rm_udmap_tx_ch_cfg_req for
2235 static int ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle *handle,
2236 const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
2238 struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *req;
2239 struct ti_sci_msg_hdr *resp;
2240 struct ti_sci_xfer *xfer;
2241 struct ti_sci_info *info;
2245 if (IS_ERR_OR_NULL(handle))
2248 info = handle_to_ti_sci_info(handle);
2251 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
2252 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2253 sizeof(*req), sizeof(*resp));
2255 ret = PTR_ERR(xfer);
2256 dev_err(dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
2259 req = (struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *)xfer->xfer_buf;
2260 req->valid_params = params->valid_params;
2261 req->nav_id = params->nav_id;
2262 req->index = params->index;
2263 req->tx_pause_on_err = params->tx_pause_on_err;
2264 req->tx_filt_einfo = params->tx_filt_einfo;
2265 req->tx_filt_pswords = params->tx_filt_pswords;
2266 req->tx_atype = params->tx_atype;
2267 req->tx_chan_type = params->tx_chan_type;
2268 req->tx_supr_tdpkt = params->tx_supr_tdpkt;
2269 req->tx_fetch_size = params->tx_fetch_size;
2270 req->tx_credit_count = params->tx_credit_count;
2271 req->txcq_qnum = params->txcq_qnum;
2272 req->tx_priority = params->tx_priority;
2273 req->tx_qos = params->tx_qos;
2274 req->tx_orderid = params->tx_orderid;
2275 req->fdepth = params->fdepth;
2276 req->tx_sched_priority = params->tx_sched_priority;
2277 req->tx_burst_size = params->tx_burst_size;
2278 req->tx_tdtype = params->tx_tdtype;
2279 req->extended_ch_type = params->extended_ch_type;
2281 ret = ti_sci_do_xfer(info, xfer);
2283 dev_err(dev, "Mbox send TX_CH_CFG fail %d\n", ret);
2287 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2288 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2291 ti_sci_put_one_xfer(&info->minfo, xfer);
2292 dev_dbg(dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
2297 * ti_sci_cmd_rm_udmap_rx_ch_cfg() - Configure a UDMAP RX channel
2298 * @handle: Pointer to TI SCI handle.
2299 * @params: Pointer to ti_sci_msg_rm_udmap_rx_ch_cfg RX channel config
2302 * Return: 0 if all went well, else returns appropriate error value.
2304 * See @ti_sci_msg_rm_udmap_rx_ch_cfg and @ti_sci_msg_rm_udmap_rx_ch_cfg_req for
2307 static int ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle *handle,
2308 const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
2310 struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *req;
2311 struct ti_sci_msg_hdr *resp;
2312 struct ti_sci_xfer *xfer;
2313 struct ti_sci_info *info;
2317 if (IS_ERR_OR_NULL(handle))
2320 info = handle_to_ti_sci_info(handle);
2323 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
2324 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2325 sizeof(*req), sizeof(*resp));
2327 ret = PTR_ERR(xfer);
2328 dev_err(dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
2331 req = (struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *)xfer->xfer_buf;
2332 req->valid_params = params->valid_params;
2333 req->nav_id = params->nav_id;
2334 req->index = params->index;
2335 req->rx_fetch_size = params->rx_fetch_size;
2336 req->rxcq_qnum = params->rxcq_qnum;
2337 req->rx_priority = params->rx_priority;
2338 req->rx_qos = params->rx_qos;
2339 req->rx_orderid = params->rx_orderid;
2340 req->rx_sched_priority = params->rx_sched_priority;
2341 req->flowid_start = params->flowid_start;
2342 req->flowid_cnt = params->flowid_cnt;
2343 req->rx_pause_on_err = params->rx_pause_on_err;
2344 req->rx_atype = params->rx_atype;
2345 req->rx_chan_type = params->rx_chan_type;
2346 req->rx_ignore_short = params->rx_ignore_short;
2347 req->rx_ignore_long = params->rx_ignore_long;
2348 req->rx_burst_size = params->rx_burst_size;
2350 ret = ti_sci_do_xfer(info, xfer);
2352 dev_err(dev, "Mbox send RX_CH_CFG fail %d\n", ret);
2356 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2357 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2360 ti_sci_put_one_xfer(&info->minfo, xfer);
2361 dev_dbg(dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
2366 * ti_sci_cmd_rm_udmap_rx_flow_cfg() - Configure UDMAP RX FLOW
2367 * @handle: Pointer to TI SCI handle.
2368 * @params: Pointer to ti_sci_msg_rm_udmap_flow_cfg RX FLOW config
2371 * Return: 0 if all went well, else returns appropriate error value.
2373 * See @ti_sci_msg_rm_udmap_flow_cfg and @ti_sci_msg_rm_udmap_flow_cfg_req for
2376 static int ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle *handle,
2377 const struct ti_sci_msg_rm_udmap_flow_cfg *params)
2379 struct ti_sci_msg_rm_udmap_flow_cfg_req *req;
2380 struct ti_sci_msg_hdr *resp;
2381 struct ti_sci_xfer *xfer;
2382 struct ti_sci_info *info;
2386 if (IS_ERR_OR_NULL(handle))
2389 info = handle_to_ti_sci_info(handle);
2392 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
2393 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2394 sizeof(*req), sizeof(*resp));
2396 ret = PTR_ERR(xfer);
2397 dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)\n", ret);
2400 req = (struct ti_sci_msg_rm_udmap_flow_cfg_req *)xfer->xfer_buf;
2401 req->valid_params = params->valid_params;
2402 req->nav_id = params->nav_id;
2403 req->flow_index = params->flow_index;
2404 req->rx_einfo_present = params->rx_einfo_present;
2405 req->rx_psinfo_present = params->rx_psinfo_present;
2406 req->rx_error_handling = params->rx_error_handling;
2407 req->rx_desc_type = params->rx_desc_type;
2408 req->rx_sop_offset = params->rx_sop_offset;
2409 req->rx_dest_qnum = params->rx_dest_qnum;
2410 req->rx_src_tag_hi = params->rx_src_tag_hi;
2411 req->rx_src_tag_lo = params->rx_src_tag_lo;
2412 req->rx_dest_tag_hi = params->rx_dest_tag_hi;
2413 req->rx_dest_tag_lo = params->rx_dest_tag_lo;
2414 req->rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
2415 req->rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
2416 req->rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
2417 req->rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
2418 req->rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
2419 req->rx_fdq1_qnum = params->rx_fdq1_qnum;
2420 req->rx_fdq2_qnum = params->rx_fdq2_qnum;
2421 req->rx_fdq3_qnum = params->rx_fdq3_qnum;
2422 req->rx_ps_location = params->rx_ps_location;
2424 ret = ti_sci_do_xfer(info, xfer);
2426 dev_err(dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
2430 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2431 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2434 ti_sci_put_one_xfer(&info->minfo, xfer);
2435 dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
2440 * ti_sci_cmd_proc_request() - Command to request a physical processor control
2441 * @handle: Pointer to TI SCI handle
2442 * @proc_id: Processor ID this request is for
2444 * Return: 0 if all went well, else returns appropriate error value.
2446 static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
2449 struct ti_sci_msg_req_proc_request *req;
2450 struct ti_sci_msg_hdr *resp;
2451 struct ti_sci_info *info;
2452 struct ti_sci_xfer *xfer;
2459 return PTR_ERR(handle);
2461 info = handle_to_ti_sci_info(handle);
2464 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_REQUEST,
2465 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2466 sizeof(*req), sizeof(*resp));
2468 ret = PTR_ERR(xfer);
2469 dev_err(dev, "Message alloc failed(%d)\n", ret);
2472 req = (struct ti_sci_msg_req_proc_request *)xfer->xfer_buf;
2473 req->processor_id = proc_id;
2475 ret = ti_sci_do_xfer(info, xfer);
2477 dev_err(dev, "Mbox send fail %d\n", ret);
2481 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2483 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2486 ti_sci_put_one_xfer(&info->minfo, xfer);
2492 * ti_sci_cmd_proc_release() - Command to release a physical processor control
2493 * @handle: Pointer to TI SCI handle
2494 * @proc_id: Processor ID this request is for
2496 * Return: 0 if all went well, else returns appropriate error value.
2498 static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
2501 struct ti_sci_msg_req_proc_release *req;
2502 struct ti_sci_msg_hdr *resp;
2503 struct ti_sci_info *info;
2504 struct ti_sci_xfer *xfer;
2511 return PTR_ERR(handle);
2513 info = handle_to_ti_sci_info(handle);
2516 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_RELEASE,
2517 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2518 sizeof(*req), sizeof(*resp));
2520 ret = PTR_ERR(xfer);
2521 dev_err(dev, "Message alloc failed(%d)\n", ret);
2524 req = (struct ti_sci_msg_req_proc_release *)xfer->xfer_buf;
2525 req->processor_id = proc_id;
2527 ret = ti_sci_do_xfer(info, xfer);
2529 dev_err(dev, "Mbox send fail %d\n", ret);
2533 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2535 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2538 ti_sci_put_one_xfer(&info->minfo, xfer);
2544 * ti_sci_cmd_proc_handover() - Command to handover a physical processor
2545 * control to a host in the processor's access
2547 * @handle: Pointer to TI SCI handle
2548 * @proc_id: Processor ID this request is for
2549 * @host_id: Host ID to get the control of the processor
2551 * Return: 0 if all went well, else returns appropriate error value.
2553 static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
2554 u8 proc_id, u8 host_id)
2556 struct ti_sci_msg_req_proc_handover *req;
2557 struct ti_sci_msg_hdr *resp;
2558 struct ti_sci_info *info;
2559 struct ti_sci_xfer *xfer;
2566 return PTR_ERR(handle);
2568 info = handle_to_ti_sci_info(handle);
2571 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_HANDOVER,
2572 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2573 sizeof(*req), sizeof(*resp));
2575 ret = PTR_ERR(xfer);
2576 dev_err(dev, "Message alloc failed(%d)\n", ret);
2579 req = (struct ti_sci_msg_req_proc_handover *)xfer->xfer_buf;
2580 req->processor_id = proc_id;
2581 req->host_id = host_id;
2583 ret = ti_sci_do_xfer(info, xfer);
2585 dev_err(dev, "Mbox send fail %d\n", ret);
2589 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2591 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2594 ti_sci_put_one_xfer(&info->minfo, xfer);
2600 * ti_sci_cmd_proc_set_config() - Command to set the processor boot
2601 * configuration flags
2602 * @handle: Pointer to TI SCI handle
2603 * @proc_id: Processor ID this request is for
2604 * @bootvector: Processor Boot vector (start address)
2605 * @config_flags_set: Configuration flags to be set
2606 * @config_flags_clear: Configuration flags to be cleared.
2608 * Return: 0 if all went well, else returns appropriate error value.
2610 static int ti_sci_cmd_proc_set_config(const struct ti_sci_handle *handle,
2611 u8 proc_id, u64 bootvector,
2612 u32 config_flags_set,
2613 u32 config_flags_clear)
2615 struct ti_sci_msg_req_set_config *req;
2616 struct ti_sci_msg_hdr *resp;
2617 struct ti_sci_info *info;
2618 struct ti_sci_xfer *xfer;
2625 return PTR_ERR(handle);
2627 info = handle_to_ti_sci_info(handle);
2630 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CONFIG,
2631 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2632 sizeof(*req), sizeof(*resp));
2634 ret = PTR_ERR(xfer);
2635 dev_err(dev, "Message alloc failed(%d)\n", ret);
2638 req = (struct ti_sci_msg_req_set_config *)xfer->xfer_buf;
2639 req->processor_id = proc_id;
2640 req->bootvector_low = bootvector & TI_SCI_ADDR_LOW_MASK;
2641 req->bootvector_high = (bootvector & TI_SCI_ADDR_HIGH_MASK) >>
2642 TI_SCI_ADDR_HIGH_SHIFT;
2643 req->config_flags_set = config_flags_set;
2644 req->config_flags_clear = config_flags_clear;
2646 ret = ti_sci_do_xfer(info, xfer);
2648 dev_err(dev, "Mbox send fail %d\n", ret);
2652 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2654 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2657 ti_sci_put_one_xfer(&info->minfo, xfer);
2663 * ti_sci_cmd_proc_set_control() - Command to set the processor boot
2665 * @handle: Pointer to TI SCI handle
2666 * @proc_id: Processor ID this request is for
2667 * @control_flags_set: Control flags to be set
2668 * @control_flags_clear: Control flags to be cleared
2670 * Return: 0 if all went well, else returns appropriate error value.
2672 static int ti_sci_cmd_proc_set_control(const struct ti_sci_handle *handle,
2673 u8 proc_id, u32 control_flags_set,
2674 u32 control_flags_clear)
2676 struct ti_sci_msg_req_set_ctrl *req;
2677 struct ti_sci_msg_hdr *resp;
2678 struct ti_sci_info *info;
2679 struct ti_sci_xfer *xfer;
2686 return PTR_ERR(handle);
2688 info = handle_to_ti_sci_info(handle);
2691 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CTRL,
2692 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2693 sizeof(*req), sizeof(*resp));
2695 ret = PTR_ERR(xfer);
2696 dev_err(dev, "Message alloc failed(%d)\n", ret);
2699 req = (struct ti_sci_msg_req_set_ctrl *)xfer->xfer_buf;
2700 req->processor_id = proc_id;
2701 req->control_flags_set = control_flags_set;
2702 req->control_flags_clear = control_flags_clear;
2704 ret = ti_sci_do_xfer(info, xfer);
2706 dev_err(dev, "Mbox send fail %d\n", ret);
2710 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2712 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2715 ti_sci_put_one_xfer(&info->minfo, xfer);
2721 * ti_sci_cmd_proc_get_status() - Command to get the processor boot status
2722 * @handle: Pointer to TI SCI handle
2723 * @proc_id: Processor ID this request is for
2724 * @bv: Processor Boot vector (start address)
2725 * @cfg_flags: Processor specific configuration flags
2726 * @ctrl_flags: Processor specific control flags
2727 * @sts_flags: Processor specific status flags
2729 * Return: 0 if all went well, else returns appropriate error value.
2731 static int ti_sci_cmd_proc_get_status(const struct ti_sci_handle *handle,
2732 u8 proc_id, u64 *bv, u32 *cfg_flags,
2733 u32 *ctrl_flags, u32 *sts_flags)
2735 struct ti_sci_msg_resp_get_status *resp;
2736 struct ti_sci_msg_req_get_status *req;
2737 struct ti_sci_info *info;
2738 struct ti_sci_xfer *xfer;
2745 return PTR_ERR(handle);
2747 info = handle_to_ti_sci_info(handle);
2750 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_STATUS,
2751 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2752 sizeof(*req), sizeof(*resp));
2754 ret = PTR_ERR(xfer);
2755 dev_err(dev, "Message alloc failed(%d)\n", ret);
2758 req = (struct ti_sci_msg_req_get_status *)xfer->xfer_buf;
2759 req->processor_id = proc_id;
2761 ret = ti_sci_do_xfer(info, xfer);
2763 dev_err(dev, "Mbox send fail %d\n", ret);
2767 resp = (struct ti_sci_msg_resp_get_status *)xfer->tx_message.buf;
2769 if (!ti_sci_is_response_ack(resp)) {
2772 *bv = (resp->bootvector_low & TI_SCI_ADDR_LOW_MASK) |
2773 (((u64)resp->bootvector_high << TI_SCI_ADDR_HIGH_SHIFT) &
2774 TI_SCI_ADDR_HIGH_MASK);
2775 *cfg_flags = resp->config_flags;
2776 *ctrl_flags = resp->control_flags;
2777 *sts_flags = resp->status_flags;
2781 ti_sci_put_one_xfer(&info->minfo, xfer);
2787 * ti_sci_setup_ops() - Setup the operations structures
2788 * @info: pointer to TISCI pointer
2790 static void ti_sci_setup_ops(struct ti_sci_info *info)
2792 struct ti_sci_ops *ops = &info->handle.ops;
2793 struct ti_sci_core_ops *core_ops = &ops->core_ops;
2794 struct ti_sci_dev_ops *dops = &ops->dev_ops;
2795 struct ti_sci_clk_ops *cops = &ops->clk_ops;
2796 struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
2797 struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
2798 struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
2799 struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
2800 struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
2801 struct ti_sci_proc_ops *pops = &ops->proc_ops;
2803 core_ops->reboot_device = ti_sci_cmd_core_reboot;
2805 dops->get_device = ti_sci_cmd_get_device;
2806 dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
2807 dops->idle_device = ti_sci_cmd_idle_device;
2808 dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
2809 dops->put_device = ti_sci_cmd_put_device;
2811 dops->is_valid = ti_sci_cmd_dev_is_valid;
2812 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
2813 dops->is_idle = ti_sci_cmd_dev_is_idle;
2814 dops->is_stop = ti_sci_cmd_dev_is_stop;
2815 dops->is_on = ti_sci_cmd_dev_is_on;
2816 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
2817 dops->set_device_resets = ti_sci_cmd_set_device_resets;
2818 dops->get_device_resets = ti_sci_cmd_get_device_resets;
2820 cops->get_clock = ti_sci_cmd_get_clock;
2821 cops->idle_clock = ti_sci_cmd_idle_clock;
2822 cops->put_clock = ti_sci_cmd_put_clock;
2823 cops->is_auto = ti_sci_cmd_clk_is_auto;
2824 cops->is_on = ti_sci_cmd_clk_is_on;
2825 cops->is_off = ti_sci_cmd_clk_is_off;
2827 cops->set_parent = ti_sci_cmd_clk_set_parent;
2828 cops->get_parent = ti_sci_cmd_clk_get_parent;
2829 cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
2831 cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
2832 cops->set_freq = ti_sci_cmd_clk_set_freq;
2833 cops->get_freq = ti_sci_cmd_clk_get_freq;
2835 rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
2836 rm_core_ops->get_range_from_shost =
2837 ti_sci_cmd_get_resource_range_from_shost;
2839 iops->set_irq = ti_sci_cmd_set_irq;
2840 iops->set_event_map = ti_sci_cmd_set_event_map;
2841 iops->free_irq = ti_sci_cmd_free_irq;
2842 iops->free_event_map = ti_sci_cmd_free_event_map;
2844 rops->set_cfg = ti_sci_cmd_rm_ring_cfg;
2846 psilops->pair = ti_sci_cmd_rm_psil_pair;
2847 psilops->unpair = ti_sci_cmd_rm_psil_unpair;
2849 udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
2850 udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
2851 udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
2853 pops->request = ti_sci_cmd_proc_request;
2854 pops->release = ti_sci_cmd_proc_release;
2855 pops->handover = ti_sci_cmd_proc_handover;
2856 pops->set_config = ti_sci_cmd_proc_set_config;
2857 pops->set_control = ti_sci_cmd_proc_set_control;
2858 pops->get_status = ti_sci_cmd_proc_get_status;
2862 * ti_sci_get_handle() - Get the TI SCI handle for a device
2863 * @dev: Pointer to device for which we want SCI handle
2865 * NOTE: The function does not track individual clients of the framework
2866 * and is expected to be maintained by caller of TI SCI protocol library.
2867 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2868 * Return: pointer to handle if successful, else:
2869 * -EPROBE_DEFER if the instance is not ready
2870 * -ENODEV if the required node handler is missing
2871 * -EINVAL if invalid conditions are encountered.
2873 const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
2875 struct device_node *ti_sci_np;
2876 struct ti_sci_handle *handle = NULL;
2877 struct ti_sci_info *info;
2880 pr_err("I need a device pointer\n");
2881 return ERR_PTR(-EINVAL);
2883 ti_sci_np = of_get_parent(dev->of_node);
2885 dev_err(dev, "No OF information\n");
2886 return ERR_PTR(-EINVAL);
2889 mutex_lock(&ti_sci_list_mutex);
2890 list_for_each_entry(info, &ti_sci_list, node) {
2891 if (ti_sci_np == info->dev->of_node) {
2892 handle = &info->handle;
2897 mutex_unlock(&ti_sci_list_mutex);
2898 of_node_put(ti_sci_np);
2901 return ERR_PTR(-EPROBE_DEFER);
2905 EXPORT_SYMBOL_GPL(ti_sci_get_handle);
2908 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
2909 * @handle: Handle acquired by ti_sci_get_handle
2911 * NOTE: The function does not track individual clients of the framework
2912 * and is expected to be maintained by caller of TI SCI protocol library.
2913 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2915 * Return: 0 is successfully released
2916 * if an error pointer was passed, it returns the error value back,
2917 * if null was passed, it returns -EINVAL;
2919 int ti_sci_put_handle(const struct ti_sci_handle *handle)
2921 struct ti_sci_info *info;
2924 return PTR_ERR(handle);
2928 info = handle_to_ti_sci_info(handle);
2929 mutex_lock(&ti_sci_list_mutex);
2930 if (!WARN_ON(!info->users))
2932 mutex_unlock(&ti_sci_list_mutex);
2936 EXPORT_SYMBOL_GPL(ti_sci_put_handle);
2938 static void devm_ti_sci_release(struct device *dev, void *res)
2940 const struct ti_sci_handle **ptr = res;
2941 const struct ti_sci_handle *handle = *ptr;
2944 ret = ti_sci_put_handle(handle);
2946 dev_err(dev, "failed to put handle %d\n", ret);
2950 * devm_ti_sci_get_handle() - Managed get handle
2951 * @dev: device for which we want SCI handle for.
2953 * NOTE: This releases the handle once the device resources are
2954 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
2955 * The function does not track individual clients of the framework
2956 * and is expected to be maintained by caller of TI SCI protocol library.
2958 * Return: 0 if all went fine, else corresponding error.
2960 const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
2962 const struct ti_sci_handle **ptr;
2963 const struct ti_sci_handle *handle;
2965 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
2967 return ERR_PTR(-ENOMEM);
2968 handle = ti_sci_get_handle(dev);
2970 if (!IS_ERR(handle)) {
2972 devres_add(dev, ptr);
2979 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
2982 * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
2984 * @property: property name containing phandle on TISCI node
2986 * NOTE: The function does not track individual clients of the framework
2987 * and is expected to be maintained by caller of TI SCI protocol library.
2988 * ti_sci_put_handle must be balanced with successful ti_sci_get_by_phandle
2989 * Return: pointer to handle if successful, else:
2990 * -EPROBE_DEFER if the instance is not ready
2991 * -ENODEV if the required node handler is missing
2992 * -EINVAL if invalid conditions are encountered.
2994 const struct ti_sci_handle *ti_sci_get_by_phandle(struct device_node *np,
2995 const char *property)
2997 struct ti_sci_handle *handle = NULL;
2998 struct device_node *ti_sci_np;
2999 struct ti_sci_info *info;
3002 pr_err("I need a device pointer\n");
3003 return ERR_PTR(-EINVAL);
3006 ti_sci_np = of_parse_phandle(np, property, 0);
3008 return ERR_PTR(-ENODEV);
3010 mutex_lock(&ti_sci_list_mutex);
3011 list_for_each_entry(info, &ti_sci_list, node) {
3012 if (ti_sci_np == info->dev->of_node) {
3013 handle = &info->handle;
3018 mutex_unlock(&ti_sci_list_mutex);
3019 of_node_put(ti_sci_np);
3022 return ERR_PTR(-EPROBE_DEFER);
3026 EXPORT_SYMBOL_GPL(ti_sci_get_by_phandle);
3029 * devm_ti_sci_get_by_phandle() - Managed get handle using phandle
3030 * @dev: Device pointer requesting TISCI handle
3031 * @property: property name containing phandle on TISCI node
3033 * NOTE: This releases the handle once the device resources are
3034 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
3035 * The function does not track individual clients of the framework
3036 * and is expected to be maintained by caller of TI SCI protocol library.
3038 * Return: 0 if all went fine, else corresponding error.
3040 const struct ti_sci_handle *devm_ti_sci_get_by_phandle(struct device *dev,
3041 const char *property)
3043 const struct ti_sci_handle *handle;
3044 const struct ti_sci_handle **ptr;
3046 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
3048 return ERR_PTR(-ENOMEM);
3049 handle = ti_sci_get_by_phandle(dev_of_node(dev), property);
3051 if (!IS_ERR(handle)) {
3053 devres_add(dev, ptr);
3060 EXPORT_SYMBOL_GPL(devm_ti_sci_get_by_phandle);
3063 * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
3064 * @res: Pointer to the TISCI resource
3066 * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
3068 u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
3070 unsigned long flags;
3073 raw_spin_lock_irqsave(&res->lock, flags);
3074 for (set = 0; set < res->sets; set++) {
3075 struct ti_sci_resource_desc *desc = &res->desc[set];
3076 int res_count = desc->num + desc->num_sec;
3078 free_bit = find_first_zero_bit(desc->res_map, res_count);
3079 if (free_bit != res_count) {
3080 __set_bit(free_bit, desc->res_map);
3081 raw_spin_unlock_irqrestore(&res->lock, flags);
3083 if (desc->num && free_bit < desc->num)
3084 return desc->start + free_bit;
3086 return desc->start_sec + free_bit;
3089 raw_spin_unlock_irqrestore(&res->lock, flags);
3091 return TI_SCI_RESOURCE_NULL;
3093 EXPORT_SYMBOL_GPL(ti_sci_get_free_resource);
3096 * ti_sci_release_resource() - Release a resource from TISCI resource.
3097 * @res: Pointer to the TISCI resource
3098 * @id: Resource id to be released.
3100 void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
3102 unsigned long flags;
3105 raw_spin_lock_irqsave(&res->lock, flags);
3106 for (set = 0; set < res->sets; set++) {
3107 struct ti_sci_resource_desc *desc = &res->desc[set];
3109 if (desc->num && desc->start <= id &&
3110 (desc->start + desc->num) > id)
3111 __clear_bit(id - desc->start, desc->res_map);
3112 else if (desc->num_sec && desc->start_sec <= id &&
3113 (desc->start_sec + desc->num_sec) > id)
3114 __clear_bit(id - desc->start_sec, desc->res_map);
3116 raw_spin_unlock_irqrestore(&res->lock, flags);
3118 EXPORT_SYMBOL_GPL(ti_sci_release_resource);
3121 * ti_sci_get_num_resources() - Get the number of resources in TISCI resource
3122 * @res: Pointer to the TISCI resource
3124 * Return: Total number of available resources.
3126 u32 ti_sci_get_num_resources(struct ti_sci_resource *res)
3130 for (set = 0; set < res->sets; set++)
3131 count += res->desc[set].num + res->desc[set].num_sec;
3135 EXPORT_SYMBOL_GPL(ti_sci_get_num_resources);
3138 * devm_ti_sci_get_resource_sets() - Get a TISCI resources assigned to a device
3139 * @handle: TISCI handle
3140 * @dev: Device pointer to which the resource is assigned
3141 * @dev_id: TISCI device id to which the resource is assigned
3142 * @sub_types: Array of sub_types assigned corresponding to device
3143 * @sets: Number of sub_types
3145 * Return: Pointer to ti_sci_resource if all went well else appropriate
3148 static struct ti_sci_resource *
3149 devm_ti_sci_get_resource_sets(const struct ti_sci_handle *handle,
3150 struct device *dev, u32 dev_id, u32 *sub_types,
3153 struct ti_sci_resource *res;
3154 bool valid_set = false;
3155 int i, ret, res_count;
3157 res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
3159 return ERR_PTR(-ENOMEM);
3162 res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
3165 return ERR_PTR(-ENOMEM);
3167 for (i = 0; i < res->sets; i++) {
3168 ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
3172 dev_dbg(dev, "dev = %d subtype %d not allocated for this host\n",
3173 dev_id, sub_types[i]);
3174 memset(&res->desc[i], 0, sizeof(res->desc[i]));
3178 dev_dbg(dev, "dev/sub_type: %d/%d, start/num: %d/%d | %d/%d\n",
3179 dev_id, sub_types[i], res->desc[i].start,
3180 res->desc[i].num, res->desc[i].start_sec,
3181 res->desc[i].num_sec);
3184 res_count = res->desc[i].num + res->desc[i].num_sec;
3185 res->desc[i].res_map = devm_bitmap_zalloc(dev, res_count,
3187 if (!res->desc[i].res_map)
3188 return ERR_PTR(-ENOMEM);
3190 raw_spin_lock_init(&res->lock);
3195 return ERR_PTR(-EINVAL);
3199 * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
3200 * @handle: TISCI handle
3201 * @dev: Device pointer to which the resource is assigned
3202 * @dev_id: TISCI device id to which the resource is assigned
3203 * @of_prop: property name by which the resource are represented
3205 * Return: Pointer to ti_sci_resource if all went well else appropriate
3208 struct ti_sci_resource *
3209 devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
3210 struct device *dev, u32 dev_id, char *of_prop)
3212 struct ti_sci_resource *res;
3216 sets = of_property_count_elems_of_size(dev_of_node(dev), of_prop,
3219 dev_err(dev, "%s resource type ids not available\n", of_prop);
3220 return ERR_PTR(sets);
3223 sub_types = kcalloc(sets, sizeof(*sub_types), GFP_KERNEL);
3225 return ERR_PTR(-ENOMEM);
3227 of_property_read_u32_array(dev_of_node(dev), of_prop, sub_types, sets);
3228 res = devm_ti_sci_get_resource_sets(handle, dev, dev_id, sub_types,
3234 EXPORT_SYMBOL_GPL(devm_ti_sci_get_of_resource);
3237 * devm_ti_sci_get_resource() - Get a resource range assigned to the device
3238 * @handle: TISCI handle
3239 * @dev: Device pointer to which the resource is assigned
3240 * @dev_id: TISCI device id to which the resource is assigned
3241 * @sub_type: TISCI resource subytpe representing the resource.
3243 * Return: Pointer to ti_sci_resource if all went well else appropriate
3246 struct ti_sci_resource *
3247 devm_ti_sci_get_resource(const struct ti_sci_handle *handle, struct device *dev,
3248 u32 dev_id, u32 sub_type)
3250 return devm_ti_sci_get_resource_sets(handle, dev, dev_id, &sub_type, 1);
3252 EXPORT_SYMBOL_GPL(devm_ti_sci_get_resource);
3254 static int tisci_reboot_handler(struct sys_off_data *data)
3256 struct ti_sci_info *info = data->cb_data;
3257 const struct ti_sci_handle *handle = &info->handle;
3259 ti_sci_cmd_core_reboot(handle);
3261 /* call fail OR pass, we should not be here in the first place */
3265 /* Description for K2G */
3266 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
3267 .default_host_id = 2,
3268 /* Conservative duration */
3269 .max_rx_timeout_ms = 1000,
3270 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3275 /* Description for AM654 */
3276 static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
3277 .default_host_id = 12,
3278 /* Conservative duration */
3279 .max_rx_timeout_ms = 10000,
3280 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3285 static const struct of_device_id ti_sci_of_match[] = {
3286 {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
3287 {.compatible = "ti,am654-sci", .data = &ti_sci_pmmc_am654_desc},
3290 MODULE_DEVICE_TABLE(of, ti_sci_of_match);
3292 static int ti_sci_probe(struct platform_device *pdev)
3294 struct device *dev = &pdev->dev;
3295 const struct ti_sci_desc *desc;
3296 struct ti_sci_xfer *xfer;
3297 struct ti_sci_info *info = NULL;
3298 struct ti_sci_xfers_info *minfo;
3299 struct mbox_client *cl;
3304 desc = device_get_match_data(dev);
3306 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3312 ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
3313 /* if the property is not present in DT, use a default from desc */
3315 info->host_id = info->desc->default_host_id;
3318 dev_warn(dev, "Host ID 0 is reserved for firmware\n");
3319 info->host_id = info->desc->default_host_id;
3321 info->host_id = h_id;
3325 INIT_LIST_HEAD(&info->node);
3326 minfo = &info->minfo;
3329 * Pre-allocate messages
3330 * NEVER allocate more than what we can indicate in hdr.seq
3331 * if we have data description bug, force a fix..
3333 if (WARN_ON(desc->max_msgs >=
3334 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
3337 minfo->xfer_block = devm_kcalloc(dev,
3339 sizeof(*minfo->xfer_block),
3341 if (!minfo->xfer_block)
3344 minfo->xfer_alloc_table = devm_bitmap_zalloc(dev,
3347 if (!minfo->xfer_alloc_table)
3350 /* Pre-initialize the buffer pointer to pre-allocated buffers */
3351 for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
3352 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
3354 if (!xfer->xfer_buf)
3357 xfer->tx_message.buf = xfer->xfer_buf;
3358 init_completion(&xfer->done);
3361 ret = ti_sci_debugfs_create(pdev, info);
3363 dev_warn(dev, "Failed to create debug file\n");
3365 platform_set_drvdata(pdev, info);
3369 cl->tx_block = false;
3370 cl->rx_callback = ti_sci_rx_callback;
3371 cl->knows_txdone = true;
3373 spin_lock_init(&minfo->xfer_lock);
3374 sema_init(&minfo->sem_xfer_count, desc->max_msgs);
3376 info->chan_rx = mbox_request_channel_byname(cl, "rx");
3377 if (IS_ERR(info->chan_rx)) {
3378 ret = PTR_ERR(info->chan_rx);
3382 info->chan_tx = mbox_request_channel_byname(cl, "tx");
3383 if (IS_ERR(info->chan_tx)) {
3384 ret = PTR_ERR(info->chan_tx);
3387 ret = ti_sci_cmd_get_revision(info);
3389 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
3393 ti_sci_setup_ops(info);
3395 ret = devm_register_restart_handler(dev, tisci_reboot_handler, info);
3397 dev_err(dev, "reboot registration fail(%d)\n", ret);
3401 dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
3402 info->handle.version.abi_major, info->handle.version.abi_minor,
3403 info->handle.version.firmware_revision,
3404 info->handle.version.firmware_description);
3406 mutex_lock(&ti_sci_list_mutex);
3407 list_add_tail(&info->node, &ti_sci_list);
3408 mutex_unlock(&ti_sci_list_mutex);
3410 return of_platform_populate(dev->of_node, NULL, NULL, dev);
3412 if (!IS_ERR(info->chan_tx))
3413 mbox_free_channel(info->chan_tx);
3414 if (!IS_ERR(info->chan_rx))
3415 mbox_free_channel(info->chan_rx);
3416 debugfs_remove(info->d);
3420 static struct platform_driver ti_sci_driver = {
3421 .probe = ti_sci_probe,
3424 .of_match_table = of_match_ptr(ti_sci_of_match),
3425 .suppress_bind_attrs = true,
3428 module_platform_driver(ti_sci_driver);
3430 MODULE_LICENSE("GPL v2");
3431 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
3432 MODULE_AUTHOR("Nishanth Menon");
3433 MODULE_ALIAS("platform:ti-sci");