// SPDX-License-Identifier: GPL-2.0/* * Texas Instruments System Control Interface Protocol Driver * * Copyright (C) 2015-2025 Texas Instruments Incorporated - https://www.ti.com/ * Nishanth Menon */ #define pr_fmt(fmt) "%s: " fmt, __func__ #include <linux/bitmap.h>#include <linux/clk.h>#include <linux/cpu.h>#include <linux/debugfs.h>#include <linux/export.h>#include <linux/hashtable.h>#include <linux/io.h>#include <linux/iopoll.h>#include <linux/kernel.h>#include <linux/mailbox_client.h>#include <linux/module.h>#include <linux/mutex.h>#include <linux/of.h>#include <linux/of_platform.h>#include <linux/platform_device.h>#include <linux/pm_qos.h>#include <linux/property.h>#include <linux/semaphore.h>#include <linux/slab.h>#include <linux/soc/ti/ti-msgmgr.h>#include <linux/soc/ti/ti_sci_protocol.h>#include <linux/suspend.h>#include <linux/sys_soc.h>#include <linux/reboot.h> #include "ti_sci.h" /* List of all TI SCI devices active in system */static LIST_HEAD(ti_sci_list);/* Protection for the entire list */static DEFINE_MUTEX(ti_sci_list_mutex); /** * struct ti_sci_xfer - Structure representing a message flow * @tx_message: Transmit message * @rx_len: Receive message length * @xfer_buf: Preallocated buffer to store receive message * Since we work with request-ACK protocol, we can * reuse the same buffer for the rx path as we * use for the tx path. * @done: completion event */struct ti_sci_xfer { struct ti_msgmgr_message tx_message; u8 rx_len; u8 *xfer_buf; struct completion done;}; /** * struct ti_sci_xfers_info - Structure to manage transfer information * @sem_xfer_count: Counting Semaphore for managing max simultaneous * Messages. * @xfer_block: Preallocated Message array * @xfer_alloc_table: Bitmap table for allocated messages. * Index of this bitmap table is also used for message * sequence identifier. * @xfer_lock: Protection for message allocation */struct ti_sci_xfers_info { struct semaphore sem_xfer_count; struct ti_sci_xfer *xfer_block; unsigned long *xfer_alloc_table; /* protect transfer allocation */ spinlock_t xfer_lock;}; /** * struct ti_sci_desc - Description of SoC integration * @default_host_id: Host identifier representing the compute entity * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds) * @max_msgs: Maximum number of messages that can be pending * simultaneously in the system * @max_msg_size: Maximum size of data per message that can be handled. */struct ti_sci_desc { u8 default_host_id; int max_rx_timeout_ms; int max_msgs; int max_msg_size;}; /** * struct ti_sci_irq - Description of allocated irqs * @node: Link to hash table * @desc: Description of the irq */struct ti_sci_irq { struct hlist_node node; struct ti_sci_msg_req_manage_irq desc;}; /** * struct ti_sci_info - Structure representing a TI SCI instance * @dev: Device pointer * @desc: SoC description for this instance * @d: Debugfs file entry * @debug_region: Memory region where the debug message are available * @debug_region_size: Debug region size * @debug_buffer: Buffer allocated to copy debug messages. * @handle: Instance of TI SCI handle to send to clients. * @cl: Mailbox Client * @chan_tx: Transmit mailbox channel * @chan_rx: Receive mailbox channel * @minfo: Message info * @node: list head * @irqs: List of allocated irqs * @irq_lock: Protection for irq hash list * @host_id: Host ID * @fw_caps: FW/SoC low power capabilities * @users: Number of users of this instance */struct ti_sci_info { struct device *dev; const struct ti_sci_desc *desc; struct dentry *d; void __iomem *debug_region; char *debug_buffer; size_t debug_region_size; struct ti_sci_handle handle; struct mbox_client cl; struct mbox_chan *chan_tx; struct mbox_chan *chan_rx; struct ti_sci_xfers_info minfo; struct list_head node; DECLARE_HASHTABLE(irqs, 8); struct mutex irq_lock; u8 host_id; u64 fw_caps; /* protected by ti_sci_list_mutex */ int users;}; #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)#define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle) #ifdef CONFIG_DEBUG_FS /** * ti_sci_debug_show() - Helper to dump the debug log * @s: sequence file pointer * @unused: unused. * * Return: 0 */static int ti_sci_debug_show(struct seq_file *s, void *unused){ struct ti_sci_info *info = s->private; memcpy_fromio(info->debug_buffer, info->debug_region, info->debug_region_size); /* * We don't trust firmware to leave NULL terminated last byte (hence * we have allocated 1 extra 0 byte). Since we cannot guarantee any * specific data format for debug messages, We just present the data * in the buffer as is - we expect the messages to be self explanatory. */ seq_puts(s, info->debug_buffer); return 0;} /* Provide the log file operations interface*/DEFINE_SHOW_ATTRIBUTE(ti_sci_debug); /** * ti_sci_debugfs_create() - Create log debug file * @pdev: platform device pointer * @info: Pointer to SCI entity information * * Return: 0 if all went fine, else corresponding error. */static int ti_sci_debugfs_create(struct platform_device *pdev, struct ti_sci_info *info){ struct device *dev = &pdev->dev; struct resource *res; char debug_name[50]; /* Debug region is optional */ res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "debug_messages"); info->debug_region = devm_ioremap_resource(dev, res); if (IS_ERR(info->debug_region)) return 0; info->debug_region_size = resource_size(res); info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1, sizeof(char), GFP_KERNEL); if (!info->debug_buffer) return -ENOMEM; /* Setup NULL termination */ info->debug_buffer[info->debug_region_size] = 0; snprintf(debug_name, sizeof(debug_name), "ti_sci_debug@%s", dev_name(dev)); info->d = debugfs_create_file(debug_name, 0444, NULL, info, &ti_sci_debug_fops); if (IS_ERR(info->d)) return PTR_ERR(info->d); dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n", info->debug_region, info->debug_region_size, res); return 0;} #else /* CONFIG_DEBUG_FS */static inline int ti_sci_debugfs_create(struct platform_device *dev, struct ti_sci_info *info){ return 0;} static inline void ti_sci_debugfs_destroy(struct platform_device *dev, struct ti_sci_info *info){}#endif /* CONFIG_DEBUG_FS */ /** * ti_sci_dump_header_dbg() - Helper to dump a message header. * @dev: Device pointer corresponding to the SCI entity * @hdr: pointer to header. */static inline void ti_sci_dump_header_dbg(struct device *dev, struct ti_sci_msg_hdr *hdr){ dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n", hdr->type, hdr->host, hdr->seq, hdr->flags);} /** * ti_sci_rx_callback() - mailbox client callback for receive messages * @cl: client pointer * @m: mailbox message * * Processes one received message to appropriate transfer information and * signals completion of the transfer. * * NOTE: This function will be invoked in IRQ context, hence should be * as optimal as possible. */static void ti_sci_rx_callback(struct mbox_client *cl, void *m){ struct ti_sci_info *info = cl_to_ti_sci_info(cl); struct device *dev = info->dev; struct ti_sci_xfers_info *minfo = &info->minfo; struct ti_msgmgr_message *mbox_msg = m; struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf; struct ti_sci_xfer *xfer; u8 xfer_id; xfer_id = hdr->seq; /* * Are we even expecting this? * NOTE: barriers were implicit in locks used for modifying the bitmap */ if (!test_bit(xfer_id, minfo->xfer_alloc_table)) { dev_err(dev, "Message for %d is not expected!\n", xfer_id); return; } xfer = &minfo->xfer_block[xfer_id]; /* Is the message of valid length? */ if (mbox_msg->len > info->desc->max_msg_size) { dev_err(dev, "Unable to handle %zu xfer(max %d)\n", mbox_msg->len, info->desc->max_msg_size); ti_sci_dump_header_dbg(dev, hdr); return; } if (mbox_msg->len < xfer->rx_len) { dev_err(dev, "Recv xfer %zu < expected %d length\n", mbox_msg->len, xfer->rx_len); ti_sci_dump_header_dbg(dev, hdr); return; } ti_sci_dump_header_dbg(dev, hdr); /* Take a copy to the rx buffer.. */ memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len); complete(&xfer->done);} /** * ti_sci_get_one_xfer() - Allocate one message * @info: Pointer to SCI entity information * @msg_type: Message type * @msg_flags: Flag to set for the message * @tx_message_size: transmit message size * @rx_message_size: receive message size * * Helper function which is used by various command functions that are * exposed to clients of this driver for allocating a message traffic event. * * This function can sleep depending on pending requests already in the system * for the SCI entity. Further, this also holds a spinlock to maintain integrity * of internal data structures. * * Return: 0 if all went fine, else corresponding error. */static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info, u16 msg_type, u32 msg_flags, size_t tx_message_size, size_t rx_message_size){ struct ti_sci_xfers_info *minfo = &info->minfo; struct ti_sci_xfer *xfer; struct ti_sci_msg_hdr *hdr; unsigned long flags; unsigned long bit_pos; u8 xfer_id; int ret; int timeout; /* Ensure we have sane transfer sizes */ if (rx_message_size > info->desc->max_msg_size || tx_message_size > info->desc->max_msg_size || rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr)) return ERR_PTR(-ERANGE); /* * Ensure we have only controlled number of pending messages. * Ideally, we might just have to wait a single message, be * conservative and wait 5 times that.. */ timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5; ret = down_timeout(&minfo->sem_xfer_count, timeout); if (ret < 0) return ERR_PTR(ret); /* Keep the locked section as small as possible */ spin_lock_irqsave(&minfo->xfer_lock, flags); bit_pos = find_first_zero_bit(minfo->xfer_alloc_table, info->desc->max_msgs); set_bit(bit_pos, minfo->xfer_alloc_table); spin_unlock_irqrestore(&minfo->xfer_lock, flags); /* * We already ensured in probe that we can have max messages that can * fit in hdr.seq - NOTE: this improves access latencies * to predictable O(1) access, BUT, it opens us to risk if * remote misbehaves with corrupted message sequence responses. * If that happens, we are going to be messed up anyways.. */ xfer_id = (u8)bit_pos; xfer = &minfo->xfer_block[xfer_id]; hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; xfer->tx_message.len = tx_message_size; xfer->tx_message.chan_rx = info->chan_rx; xfer->tx_message.timeout_rx_ms = info->desc->max_rx_timeout_ms; xfer->rx_len = (u8)rx_message_size; reinit_completion(&xfer->done); hdr->seq = xfer_id; hdr->type = msg_type; hdr->host = info->host_id; hdr->flags = msg_flags; return xfer;} /** * ti_sci_put_one_xfer() - Release a message * @minfo: transfer info pointer * @xfer: message that was reserved by ti_sci_get_one_xfer * * This holds a spinlock to maintain integrity of internal data structures. */static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo, struct ti_sci_xfer *xfer){ unsigned long flags; struct ti_sci_msg_hdr *hdr; u8 xfer_id; hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; xfer_id = hdr->seq; /* * Keep the locked section as small as possible * NOTE: we might escape with smp_mb and no lock here.. * but just be conservative and symmetric. */ spin_lock_irqsave(&minfo->xfer_lock, flags); clear_bit(xfer_id, minfo->xfer_alloc_table); spin_unlock_irqrestore(&minfo->xfer_lock, flags); /* Increment the count for the next user to get through */ up(&minfo->sem_xfer_count);} /** * ti_sci_do_xfer() - Do one transfer * @info: Pointer to SCI entity information * @xfer: Transfer to initiate and wait for response * * Return: -ETIMEDOUT in case of no response, if transmit error, * return corresponding error, else if all goes well, * return 0. */static inline int ti_sci_do_xfer(struct ti_sci_info *info, struct ti_sci_xfer *xfer){ struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; bool response_expected = !!(hdr->flags & (TI_SCI_FLAG_REQ_ACK_ON_PROCESSED | TI_SCI_FLAG_REQ_ACK_ON_RECEIVED)); int ret; int timeout; struct device *dev = info->dev; bool done_state = true; ret = mbox_send_message(info->chan_tx, &xfer->tx_message); if (ret < 0) return ret; ret = 0; if (response_expected && system_state <= SYSTEM_RUNNING) { /* And we wait for the response. */ timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms); if (!wait_for_completion_timeout(&xfer->done, timeout)) ret = -ETIMEDOUT; } else if (response_expected) { /* * If we are !running, we cannot use wait_for_completion_timeout * during noirq phase, so we must manually poll the completion. */ ret = read_poll_timeout_atomic(try_wait_for_completion, done_state, done_state, 1, info->desc->max_rx_timeout_ms * 1000, false, &xfer->done); } if (ret == -ETIMEDOUT) dev_err(dev, "Mbox timedout in resp(caller: %pS)\n", (void *)_RET_IP_); /* * NOTE: we might prefer not to need the mailbox ticker to manage the * transfer queueing since the protocol layer queues things by itself. * Unfortunately, we have to kick the mailbox framework after we have * received our message. */ mbox_client_txdone(info->chan_tx, ret); return ret;} /** * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity * @info: Pointer to SCI entity information * * Updates the SCI information in the internal data structure. * * Return: 0 if all went fine, else return appropriate error. */static int ti_sci_cmd_get_revision(struct ti_sci_info *info){ struct device *dev = info->dev; struct ti_sci_handle *handle = &info->handle; struct ti_sci_version_info *ver = &handle->version; struct ti_sci_msg_resp_version *rev_info; struct ti_sci_xfer *xfer; int ret; xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, sizeof(struct ti_sci_msg_hdr), sizeof(*rev_info)); if (IS_ERR(xfer)) { ret = PTR_ERR(xfer); dev_err(dev, "Message alloc failed(%d)\n", ret); return ret; } rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf; ret = ti_sci_do_xfer(info, xfer); if (ret) { dev_err(dev, "Mbox send fail %d\n", ret); goto fail; } ver->abi_major = rev_info->abi_major; ver->abi_minor = rev_info->abi_minor; ver->firmware_revision = rev_info->firmware_revision;Lines 1–500 of 4259. Load more
// SPDX-License-Identifier: GPL-2.0 /* * Texas Instruments System Control Interface Protocol Driver * * Copyright (C) 2015-2025 Texas Instruments Incorporated - https://www.ti.com/ * Nishanth Menon */ #define pr_fmt(fmt) "%s: " fmt, __func__ #include <linux/bitmap.h> #include <linux/clk.h> #include <linux/cpu.h> #include <linux/debugfs.h> #include <linux/export.h> #include <linux/hashtable.h> #include <linux/io.h> #include <linux/iopoll.h> #include <linux/kernel.h> #include <linux/mailbox_client.h> #include <linux/module.h> #include <linux/mutex.h> #include <linux/of.h> #include <linux/of_platform.h> #include <linux/platform_device.h> #include <linux/pm_qos.h> #include <linux/property.h> #include <linux/semaphore.h> #include <linux/slab.h> #include <linux/soc/ti/ti-msgmgr.h> #include <linux/soc/ti/ti_sci_protocol.h> #include <linux/suspend.h> #include <linux/sys_soc.h> #include <linux/reboot.h> #include "ti_sci.h" /* List of all TI SCI devices active in system */ static LIST_HEAD(ti_sci_list); /* Protection for the entire list */ static DEFINE_MUTEX(ti_sci_list_mutex); /** * struct ti_sci_xfer - Structure representing a message flow * @tx_message: Transmit message * @rx_len: Receive message length * @xfer_buf: Preallocated buffer to store receive message * Since we work with request-ACK protocol, we can * reuse the same buffer for the rx path as we * use for the tx path. * @done: completion event */ struct ti_sci_xfer { struct ti_msgmgr_message tx_message; u8 rx_len; u8 *xfer_buf; struct completion done; }; /** * struct ti_sci_xfers_info - Structure to manage transfer information * @sem_xfer_count: Counting Semaphore for managing max simultaneous * Messages. * @xfer_block: Preallocated Message array * @xfer_alloc_table: Bitmap table for allocated messages. * Index of this bitmap table is also used for message * sequence identifier. * @xfer_lock: Protection for message allocation */ struct ti_sci_xfers_info { struct semaphore sem_xfer_count; struct ti_sci_xfer *xfer_block; unsigned long *xfer_alloc_table; /* protect transfer allocation */ spinlock_t xfer_lock; }; /** * struct ti_sci_desc - Description of SoC integration * @default_host_id: Host identifier representing the compute entity * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds) * @max_msgs: Maximum number of messages that can be pending * simultaneously in the system * @max_msg_size: Maximum size of data per message that can be handled. */ struct ti_sci_desc { u8 default_host_id; int max_rx_timeout_ms; int max_msgs; int max_msg_size; }; /** * struct ti_sci_irq - Description of allocated irqs * @node: Link to hash table * @desc: Description of the irq */ struct ti_sci_irq { struct hlist_node node; struct ti_sci_msg_req_manage_irq desc; }; /** * struct ti_sci_info - Structure representing a TI SCI instance * @dev: Device pointer * @desc: SoC description for this instance * @d: Debugfs file entry * @debug_region: Memory region where the debug message are available * @debug_region_size: Debug region size * @debug_buffer: Buffer allocated to copy debug messages. * @handle: Instance of TI SCI handle to send to clients. * @cl: Mailbox Client * @chan_tx: Transmit mailbox channel * @chan_rx: Receive mailbox channel * @minfo: Message info * @node: list head * @irqs: List of allocated irqs * @irq_lock: Protection for irq hash list * @host_id: Host ID * @fw_caps: FW/SoC low power capabilities * @users: Number of users of this instance */ struct ti_sci_info { struct device *dev; const struct ti_sci_desc *desc; struct dentry *d; void __iomem *debug_region; char *debug_buffer; size_t debug_region_size; struct ti_sci_handle handle; struct mbox_client cl; struct mbox_chan *chan_tx; struct mbox_chan *chan_rx; struct ti_sci_xfers_info minfo; struct list_head node; DECLARE_HASHTABLE(irqs, 8); struct mutex irq_lock; u8 host_id; u64 fw_caps; /* protected by ti_sci_list_mutex */ int users; }; #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl) #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle) #ifdef CONFIG_DEBUG_FS /** * ti_sci_debug_show() - Helper to dump the debug log * @s: sequence file pointer * @unused: unused. * * Return: 0 */ static int ti_sci_debug_show(struct seq_file *s, void *unused) { struct ti_sci_info *info = s->private; memcpy_fromio(info->debug_buffer, info->debug_region, info->debug_region_size); /* * We don't trust firmware to leave NULL terminated last byte (hence * we have allocated 1 extra 0 byte). Since we cannot guarantee any * specific data format for debug messages, We just present the data * in the buffer as is - we expect the messages to be self explanatory. */ seq_puts(s, info->debug_buffer); return 0; } /* Provide the log file operations interface*/ DEFINE_SHOW_ATTRIBUTE(ti_sci_debug); /** * ti_sci_debugfs_create() - Create log debug file * @pdev: platform device pointer * @info: Pointer to SCI entity information * * Return: 0 if all went fine, else corresponding error. */ static int ti_sci_debugfs_create(struct platform_device *pdev, struct ti_sci_info *info) { struct device *dev = &pdev->dev; struct resource *res; char debug_name[50]; /* Debug region is optional */ res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "debug_messages"); info->debug_region = devm_ioremap_resource(dev, res); if (IS_ERR(info->debug_region)) return 0; info->debug_region_size = resource_size(res); info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1, sizeof(char), GFP_KERNEL); if (!info->debug_buffer) return -ENOMEM; /* Setup NULL termination */ info->debug_buffer[info->debug_region_size] = 0; snprintf(debug_name, sizeof(debug_name), "ti_sci_debug@%s", dev_name(dev)); info->d = debugfs_create_file(debug_name, 0444, NULL, info, &ti_sci_debug_fops); if (IS_ERR(info->d)) return PTR_ERR(info->d); dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n", info->debug_region, info->debug_region_size, res); return 0; } #else /* CONFIG_DEBUG_FS */ static inline int ti_sci_debugfs_create(struct platform_device *dev, struct ti_sci_info *info) { return 0; } static inline void ti_sci_debugfs_destroy(struct platform_device *dev, struct ti_sci_info *info) { } #endif /* CONFIG_DEBUG_FS */ /** * ti_sci_dump_header_dbg() - Helper to dump a message header. * @dev: Device pointer corresponding to the SCI entity * @hdr: pointer to header. */ static inline void ti_sci_dump_header_dbg(struct device *dev, struct ti_sci_msg_hdr *hdr) { dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n", hdr->type, hdr->host, hdr->seq, hdr->flags); } /** * ti_sci_rx_callback() - mailbox client callback for receive messages * @cl: client pointer * @m: mailbox message * * Processes one received message to appropriate transfer information and * signals completion of the transfer. * * NOTE: This function will be invoked in IRQ context, hence should be * as optimal as possible. */ static void ti_sci_rx_callback(struct mbox_client *cl, void *m) { struct ti_sci_info *info = cl_to_ti_sci_info(cl); struct device *dev = info->dev; struct ti_sci_xfers_info *minfo = &info->minfo; struct ti_msgmgr_message *mbox_msg = m; struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf; struct ti_sci_xfer *xfer; u8 xfer_id; xfer_id = hdr->seq; /* * Are we even expecting this? * NOTE: barriers were implicit in locks used for modifying the bitmap */ if (!test_bit(xfer_id, minfo->xfer_alloc_table)) { dev_err(dev, "Message for %d is not expected!\n", xfer_id); return; } xfer = &minfo->xfer_block[xfer_id]; /* Is the message of valid length? */ if (mbox_msg->len > info->desc->max_msg_size) { dev_err(dev, "Unable to handle %zu xfer(max %d)\n", mbox_msg->len, info->desc->max_msg_size); ti_sci_dump_header_dbg(dev, hdr); return; } if (mbox_msg->len < xfer->rx_len) { dev_err(dev, "Recv xfer %zu < expected %d length\n", mbox_msg->len, xfer->rx_len); ti_sci_dump_header_dbg(dev, hdr); return; } ti_sci_dump_header_dbg(dev, hdr); /* Take a copy to the rx buffer.. */ memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len); complete(&xfer->done); } /** * ti_sci_get_one_xfer() - Allocate one message * @info: Pointer to SCI entity information * @msg_type: Message type * @msg_flags: Flag to set for the message * @tx_message_size: transmit message size * @rx_message_size: receive message size * * Helper function which is used by various command functions that are * exposed to clients of this driver for allocating a message traffic event. * * This function can sleep depending on pending requests already in the system * for the SCI entity. Further, this also holds a spinlock to maintain integrity * of internal data structures. * * Return: 0 if all went fine, else corresponding error. */ static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info, u16 msg_type, u32 msg_flags, size_t tx_message_size, size_t rx_message_size) { struct ti_sci_xfers_info *minfo = &info->minfo; struct ti_sci_xfer *xfer; struct ti_sci_msg_hdr *hdr; unsigned long flags; unsigned long bit_pos; u8 xfer_id; int ret; int timeout; /* Ensure we have sane transfer sizes */ if (rx_message_size > info->desc->max_msg_size || tx_message_size > info->desc->max_msg_size || rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr)) return ERR_PTR(-ERANGE); /* * Ensure we have only controlled number of pending messages. * Ideally, we might just have to wait a single message, be * conservative and wait 5 times that.. */ timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5; ret = down_timeout(&minfo->sem_xfer_count, timeout); if (ret < 0) return ERR_PTR(ret); /* Keep the locked section as small as possible */ spin_lock_irqsave(&minfo->xfer_lock, flags); bit_pos = find_first_zero_bit(minfo->xfer_alloc_table, info->desc->max_msgs); set_bit(bit_pos, minfo->xfer_alloc_table); spin_unlock_irqrestore(&minfo->xfer_lock, flags); /* * We already ensured in probe that we can have max messages that can * fit in hdr.seq - NOTE: this improves access latencies * to predictable O(1) access, BUT, it opens us to risk if * remote misbehaves with corrupted message sequence responses. * If that happens, we are going to be messed up anyways.. */ xfer_id = (u8)bit_pos; xfer = &minfo->xfer_block[xfer_id]; hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; xfer->tx_message.len = tx_message_size; xfer->tx_message.chan_rx = info->chan_rx; xfer->tx_message.timeout_rx_ms = info->desc->max_rx_timeout_ms; xfer->rx_len = (u8)rx_message_size; reinit_completion(&xfer->done); hdr->seq = xfer_id; hdr->type = msg_type; hdr->host = info->host_id; hdr->flags = msg_flags; return xfer; } /** * ti_sci_put_one_xfer() - Release a message * @minfo: transfer info pointer * @xfer: message that was reserved by ti_sci_get_one_xfer * * This holds a spinlock to maintain integrity of internal data structures. */ static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo, struct ti_sci_xfer *xfer) { unsigned long flags; struct ti_sci_msg_hdr *hdr; u8 xfer_id; hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; xfer_id = hdr->seq; /* * Keep the locked section as small as possible * NOTE: we might escape with smp_mb and no lock here.. * but just be conservative and symmetric. */ spin_lock_irqsave(&minfo->xfer_lock, flags); clear_bit(xfer_id, minfo->xfer_alloc_table); spin_unlock_irqrestore(&minfo->xfer_lock, flags); /* Increment the count for the next user to get through */ up(&minfo->sem_xfer_count); } /** * ti_sci_do_xfer() - Do one transfer * @info: Pointer to SCI entity information * @xfer: Transfer to initiate and wait for response * * Return: -ETIMEDOUT in case of no response, if transmit error, * return corresponding error, else if all goes well, * return 0. */ static inline int ti_sci_do_xfer(struct ti_sci_info *info, struct ti_sci_xfer *xfer) { struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf; bool response_expected = !!(hdr->flags & (TI_SCI_FLAG_REQ_ACK_ON_PROCESSED | TI_SCI_FLAG_REQ_ACK_ON_RECEIVED)); int ret; int timeout; struct device *dev = info->dev; bool done_state = true; ret = mbox_send_message(info->chan_tx, &xfer->tx_message); if (ret < 0) return ret; ret = 0; if (response_expected && system_state <= SYSTEM_RUNNING) { /* And we wait for the response. */ timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms); if (!wait_for_completion_timeout(&xfer->done, timeout)) ret = -ETIMEDOUT; } else if (response_expected) { /* * If we are !running, we cannot use wait_for_completion_timeout * during noirq phase, so we must manually poll the completion. */ ret = read_poll_timeout_atomic(try_wait_for_completion, done_state, done_state, 1, info->desc->max_rx_timeout_ms * 1000, false, &xfer->done); } if (ret == -ETIMEDOUT) dev_err(dev, "Mbox timedout in resp(caller: %pS)\n", (void *)_RET_IP_); /* * NOTE: we might prefer not to need the mailbox ticker to manage the * transfer queueing since the protocol layer queues things by itself. * Unfortunately, we have to kick the mailbox framework after we have * received our message. */ mbox_client_txdone(info->chan_tx, ret); return ret; } /** * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity * @info: Pointer to SCI entity information * * Updates the SCI information in the internal data structure. * * Return: 0 if all went fine, else return appropriate error. */ static int ti_sci_cmd_get_revision(struct ti_sci_info *info) { struct device *dev = info->dev; struct ti_sci_handle *handle = &info->handle; struct ti_sci_version_info *ver = &handle->version; struct ti_sci_msg_resp_version *rev_info; struct ti_sci_xfer *xfer; int ret; xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED, sizeof(struct ti_sci_msg_hdr), sizeof(*rev_info)); if (IS_ERR(xfer)) { ret = PTR_ERR(xfer); dev_err(dev, "Message alloc failed(%d)\n", ret); return ret; } rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf; ret = ti_sci_do_xfer(info, xfer); if (ret) { dev_err(dev, "Mbox send fail %d\n", ret); goto fail; } ver->abi_major = rev_info->abi_major; ver->abi_minor = rev_info->abi_minor; ver->firmware_revision = rev_info->firmware_revision;