2 * Texas Instruments System Control Interface Protocol Driver
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #define pr_fmt(fmt) "%s: " fmt, __func__
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 #include <linux/export.h>
23 #include <linux/kernel.h>
24 #include <linux/mailbox_client.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/semaphore.h>
28 #include <linux/slab.h>
29 #include <linux/soc/ti/ti-msgmgr.h>
30 #include <linux/soc/ti/ti_sci_protocol.h>
34 /* List of all TI SCI devices active in system */
35 static LIST_HEAD(ti_sci_list);
36 /* Protection for the entire list */
37 static DEFINE_MUTEX(ti_sci_list_mutex);
40 * struct ti_sci_xfer - Structure representing a message flow
41 * @tx_message: Transmit message
42 * @rx_len: Receive message length
43 * @xfer_buf: Preallocated buffer to store receive message
44 * Since we work with request-ACK protocol, we can
45 * reuse the same buffer for the rx path as we
46 * use for the tx path.
47 * @done: completion event
50 struct ti_msgmgr_message tx_message;
53 struct completion done;
57 * struct ti_sci_xfers_info - Structure to manage transfer information
58 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
60 * @xfer_block: Preallocated Message array
61 * @xfer_alloc_table: Bitmap table for allocated messages.
62 * Index of this bitmap table is also used for message
63 * sequence identifier.
64 * @xfer_lock: Protection for message allocation
66 struct ti_sci_xfers_info {
67 struct semaphore sem_xfer_count;
68 struct ti_sci_xfer *xfer_block;
69 unsigned long *xfer_alloc_table;
70 /* protect transfer allocation */
75 * struct ti_sci_desc - Description of SoC integration
76 * @host_id: Host identifier representing the compute entity
77 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
78 * @max_msgs: Maximum number of messages that can be pending
79 * simultaneously in the system
80 * @max_msg_size: Maximum size of data per message that can be handled.
84 int max_rx_timeout_ms;
90 * struct ti_sci_info - Structure representing a TI SCI instance
91 * @dev: Device pointer
92 * @desc: SoC description for this instance
93 * @d: Debugfs file entry
94 * @debug_region: Memory region where the debug message are available
95 * @debug_region_size: Debug region size
96 * @debug_buffer: Buffer allocated to copy debug messages.
97 * @handle: Instance of TI SCI handle to send to clients.
99 * @chan_tx: Transmit mailbox channel
100 * @chan_rx: Receive mailbox channel
101 * @minfo: Message info
103 * @users: Number of users of this instance
107 const struct ti_sci_desc *desc;
109 void __iomem *debug_region;
111 size_t debug_region_size;
112 struct ti_sci_handle handle;
113 struct mbox_client cl;
114 struct mbox_chan *chan_tx;
115 struct mbox_chan *chan_rx;
116 struct ti_sci_xfers_info minfo;
117 struct list_head node;
118 /* protected by ti_sci_list_mutex */
122 #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
123 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
125 #ifdef CONFIG_DEBUG_FS
128 * ti_sci_debug_show() - Helper to dump the debug log
129 * @s: sequence file pointer
134 static int ti_sci_debug_show(struct seq_file *s, void *unused)
136 struct ti_sci_info *info = s->private;
138 memcpy_fromio(info->debug_buffer, info->debug_region,
139 info->debug_region_size);
141 * We don't trust firmware to leave NULL terminated last byte (hence
142 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
143 * specific data format for debug messages, We just present the data
144 * in the buffer as is - we expect the messages to be self explanatory.
146 seq_puts(s, info->debug_buffer);
151 * ti_sci_debug_open() - debug file open
152 * @inode: inode pointer
153 * @file: file pointer
155 * Return: result of single_open
157 static int ti_sci_debug_open(struct inode *inode, struct file *file)
159 return single_open(file, ti_sci_debug_show, inode->i_private);
162 /* log file operations */
163 static const struct file_operations ti_sci_debug_fops = {
164 .open = ti_sci_debug_open,
167 .release = single_release,
171 * ti_sci_debugfs_create() - Create log debug file
172 * @pdev: platform device pointer
173 * @info: Pointer to SCI entity information
175 * Return: 0 if all went fine, else corresponding error.
177 static int ti_sci_debugfs_create(struct platform_device *pdev,
178 struct ti_sci_info *info)
180 struct device *dev = &pdev->dev;
181 struct resource *res;
182 char debug_name[50] = "ti_sci_debug@";
184 /* Debug region is optional */
185 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
187 info->debug_region = devm_ioremap_resource(dev, res);
188 if (IS_ERR(info->debug_region))
190 info->debug_region_size = resource_size(res);
192 info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
193 sizeof(char), GFP_KERNEL);
194 if (!info->debug_buffer)
196 /* Setup NULL termination */
197 info->debug_buffer[info->debug_region_size] = 0;
199 info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
201 0444, NULL, info, &ti_sci_debug_fops);
203 return PTR_ERR(info->d);
205 dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
206 info->debug_region, info->debug_region_size, res);
211 * ti_sci_debugfs_destroy() - clean up log debug file
212 * @pdev: platform device pointer
213 * @info: Pointer to SCI entity information
215 static void ti_sci_debugfs_destroy(struct platform_device *pdev,
216 struct ti_sci_info *info)
218 if (IS_ERR(info->debug_region))
221 debugfs_remove(info->d);
223 #else /* CONFIG_DEBUG_FS */
224 static inline int ti_sci_debugfs_create(struct platform_device *dev,
225 struct ti_sci_info *info)
230 static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
231 struct ti_sci_info *info)
234 #endif /* CONFIG_DEBUG_FS */
237 * ti_sci_dump_header_dbg() - Helper to dump a message header.
238 * @dev: Device pointer corresponding to the SCI entity
239 * @hdr: pointer to header.
241 static inline void ti_sci_dump_header_dbg(struct device *dev,
242 struct ti_sci_msg_hdr *hdr)
244 dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
245 hdr->type, hdr->host, hdr->seq, hdr->flags);
249 * ti_sci_rx_callback() - mailbox client callback for receive messages
250 * @cl: client pointer
251 * @m: mailbox message
253 * Processes one received message to appropriate transfer information and
254 * signals completion of the transfer.
256 * NOTE: This function will be invoked in IRQ context, hence should be
257 * as optimal as possible.
259 static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
261 struct ti_sci_info *info = cl_to_ti_sci_info(cl);
262 struct device *dev = info->dev;
263 struct ti_sci_xfers_info *minfo = &info->minfo;
264 struct ti_msgmgr_message *mbox_msg = m;
265 struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
266 struct ti_sci_xfer *xfer;
272 * Are we even expecting this?
273 * NOTE: barriers were implicit in locks used for modifying the bitmap
275 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
276 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
280 xfer = &minfo->xfer_block[xfer_id];
282 /* Is the message of valid length? */
283 if (mbox_msg->len > info->desc->max_msg_size) {
284 dev_err(dev, "Unable to handle %d xfer(max %d)\n",
285 mbox_msg->len, info->desc->max_msg_size);
286 ti_sci_dump_header_dbg(dev, hdr);
289 if (mbox_msg->len < xfer->rx_len) {
290 dev_err(dev, "Recv xfer %d < expected %d length\n",
291 mbox_msg->len, xfer->rx_len);
292 ti_sci_dump_header_dbg(dev, hdr);
296 ti_sci_dump_header_dbg(dev, hdr);
297 /* Take a copy to the rx buffer.. */
298 memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
299 complete(&xfer->done);
303 * ti_sci_get_one_xfer() - Allocate one message
304 * @info: Pointer to SCI entity information
305 * @msg_type: Message type
306 * @msg_flags: Flag to set for the message
307 * @tx_message_size: transmit message size
308 * @rx_message_size: receive message size
310 * Helper function which is used by various command functions that are
311 * exposed to clients of this driver for allocating a message traffic event.
313 * This function can sleep depending on pending requests already in the system
314 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
315 * of internal data structures.
317 * Return: 0 if all went fine, else corresponding error.
319 static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
320 u16 msg_type, u32 msg_flags,
321 size_t tx_message_size,
322 size_t rx_message_size)
324 struct ti_sci_xfers_info *minfo = &info->minfo;
325 struct ti_sci_xfer *xfer;
326 struct ti_sci_msg_hdr *hdr;
328 unsigned long bit_pos;
333 /* Ensure we have sane transfer sizes */
334 if (rx_message_size > info->desc->max_msg_size ||
335 tx_message_size > info->desc->max_msg_size ||
336 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
337 return ERR_PTR(-ERANGE);
340 * Ensure we have only controlled number of pending messages.
341 * Ideally, we might just have to wait a single message, be
342 * conservative and wait 5 times that..
344 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
345 ret = down_timeout(&minfo->sem_xfer_count, timeout);
349 /* Keep the locked section as small as possible */
350 spin_lock_irqsave(&minfo->xfer_lock, flags);
351 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
352 info->desc->max_msgs);
353 set_bit(bit_pos, minfo->xfer_alloc_table);
354 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
357 * We already ensured in probe that we can have max messages that can
358 * fit in hdr.seq - NOTE: this improves access latencies
359 * to predictable O(1) access, BUT, it opens us to risk if
360 * remote misbehaves with corrupted message sequence responses.
361 * If that happens, we are going to be messed up anyways..
363 xfer_id = (u8)bit_pos;
365 xfer = &minfo->xfer_block[xfer_id];
367 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
368 xfer->tx_message.len = tx_message_size;
369 xfer->rx_len = (u8)rx_message_size;
371 reinit_completion(&xfer->done);
374 hdr->type = msg_type;
375 hdr->host = info->desc->host_id;
376 hdr->flags = msg_flags;
382 * ti_sci_put_one_xfer() - Release a message
383 * @minfo: transfer info pointer
384 * @xfer: message that was reserved by ti_sci_get_one_xfer
386 * This holds a spinlock to maintain integrity of internal data structures.
388 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
389 struct ti_sci_xfer *xfer)
392 struct ti_sci_msg_hdr *hdr;
395 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
399 * Keep the locked section as small as possible
400 * NOTE: we might escape with smp_mb and no lock here..
401 * but just be conservative and symmetric.
403 spin_lock_irqsave(&minfo->xfer_lock, flags);
404 clear_bit(xfer_id, minfo->xfer_alloc_table);
405 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
407 /* Increment the count for the next user to get through */
408 up(&minfo->sem_xfer_count);
412 * ti_sci_do_xfer() - Do one transfer
413 * @info: Pointer to SCI entity information
414 * @xfer: Transfer to initiate and wait for response
416 * Return: -ETIMEDOUT in case of no response, if transmit error,
417 * return corresponding error, else if all goes well,
420 static inline int ti_sci_do_xfer(struct ti_sci_info *info,
421 struct ti_sci_xfer *xfer)
425 struct device *dev = info->dev;
427 ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
433 /* And we wait for the response. */
434 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
435 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
436 dev_err(dev, "Mbox timedout in resp(caller: %pF)\n",
441 * NOTE: we might prefer not to need the mailbox ticker to manage the
442 * transfer queueing since the protocol layer queues things by itself.
443 * Unfortunately, we have to kick the mailbox framework after we have
444 * received our message.
446 mbox_client_txdone(info->chan_tx, ret);
452 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
453 * @info: Pointer to SCI entity information
455 * Updates the SCI information in the internal data structure.
457 * Return: 0 if all went fine, else return appropriate error.
459 static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
461 struct device *dev = info->dev;
462 struct ti_sci_handle *handle = &info->handle;
463 struct ti_sci_version_info *ver = &handle->version;
464 struct ti_sci_msg_resp_version *rev_info;
465 struct ti_sci_xfer *xfer;
468 /* No need to setup flags since it is expected to respond */
469 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
470 0x0, sizeof(struct ti_sci_msg_hdr),
474 dev_err(dev, "Message alloc failed(%d)\n", ret);
478 rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
480 ret = ti_sci_do_xfer(info, xfer);
482 dev_err(dev, "Mbox send fail %d\n", ret);
486 ver->abi_major = rev_info->abi_major;
487 ver->abi_minor = rev_info->abi_minor;
488 ver->firmware_revision = rev_info->firmware_revision;
489 strncpy(ver->firmware_description, rev_info->firmware_description,
490 sizeof(ver->firmware_description));
493 ti_sci_put_one_xfer(&info->minfo, xfer);
498 * ti_sci_get_handle() - Get the TI SCI handle for a device
499 * @dev: Pointer to device for which we want SCI handle
501 * NOTE: The function does not track individual clients of the framework
502 * and is expected to be maintained by caller of TI SCI protocol library.
503 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
504 * Return: pointer to handle if successful, else:
505 * -EPROBE_DEFER if the instance is not ready
506 * -ENODEV if the required node handler is missing
507 * -EINVAL if invalid conditions are encountered.
509 const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
511 struct device_node *ti_sci_np;
513 struct ti_sci_handle *handle = NULL;
514 struct ti_sci_info *info;
517 pr_err("I need a device pointer\n");
518 return ERR_PTR(-EINVAL);
520 ti_sci_np = of_get_parent(dev->of_node);
522 dev_err(dev, "No OF information\n");
523 return ERR_PTR(-EINVAL);
526 mutex_lock(&ti_sci_list_mutex);
527 list_for_each(p, &ti_sci_list) {
528 info = list_entry(p, struct ti_sci_info, node);
529 if (ti_sci_np == info->dev->of_node) {
530 handle = &info->handle;
535 mutex_unlock(&ti_sci_list_mutex);
536 of_node_put(ti_sci_np);
539 return ERR_PTR(-EPROBE_DEFER);
543 EXPORT_SYMBOL_GPL(ti_sci_get_handle);
546 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
547 * @handle: Handle acquired by ti_sci_get_handle
549 * NOTE: The function does not track individual clients of the framework
550 * and is expected to be maintained by caller of TI SCI protocol library.
551 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
553 * Return: 0 is successfully released
554 * if an error pointer was passed, it returns the error value back,
555 * if null was passed, it returns -EINVAL;
557 int ti_sci_put_handle(const struct ti_sci_handle *handle)
559 struct ti_sci_info *info;
562 return PTR_ERR(handle);
566 info = handle_to_ti_sci_info(handle);
567 mutex_lock(&ti_sci_list_mutex);
568 if (!WARN_ON(!info->users))
570 mutex_unlock(&ti_sci_list_mutex);
574 EXPORT_SYMBOL_GPL(ti_sci_put_handle);
576 static void devm_ti_sci_release(struct device *dev, void *res)
578 const struct ti_sci_handle **ptr = res;
579 const struct ti_sci_handle *handle = *ptr;
582 ret = ti_sci_put_handle(handle);
584 dev_err(dev, "failed to put handle %d\n", ret);
588 * devm_ti_sci_get_handle() - Managed get handle
589 * @dev: device for which we want SCI handle for.
591 * NOTE: This releases the handle once the device resources are
592 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
593 * The function does not track individual clients of the framework
594 * and is expected to be maintained by caller of TI SCI protocol library.
596 * Return: 0 if all went fine, else corresponding error.
598 const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
600 const struct ti_sci_handle **ptr;
601 const struct ti_sci_handle *handle;
603 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
605 return ERR_PTR(-ENOMEM);
606 handle = ti_sci_get_handle(dev);
608 if (!IS_ERR(handle)) {
610 devres_add(dev, ptr);
617 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
619 /* Description for K2G */
620 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
622 /* Conservative duration */
623 .max_rx_timeout_ms = 1000,
624 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
629 static const struct of_device_id ti_sci_of_match[] = {
630 {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
633 MODULE_DEVICE_TABLE(of, ti_sci_of_match);
635 static int ti_sci_probe(struct platform_device *pdev)
637 struct device *dev = &pdev->dev;
638 const struct of_device_id *of_id;
639 const struct ti_sci_desc *desc;
640 struct ti_sci_xfer *xfer;
641 struct ti_sci_info *info = NULL;
642 struct ti_sci_xfers_info *minfo;
643 struct mbox_client *cl;
647 of_id = of_match_device(ti_sci_of_match, dev);
649 dev_err(dev, "OF data missing\n");
654 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
660 INIT_LIST_HEAD(&info->node);
661 minfo = &info->minfo;
664 * Pre-allocate messages
665 * NEVER allocate more than what we can indicate in hdr.seq
666 * if we have data description bug, force a fix..
668 if (WARN_ON(desc->max_msgs >=
669 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
672 minfo->xfer_block = devm_kcalloc(dev,
674 sizeof(*minfo->xfer_block),
676 if (!minfo->xfer_block)
679 minfo->xfer_alloc_table = devm_kzalloc(dev,
680 BITS_TO_LONGS(desc->max_msgs)
681 * sizeof(unsigned long),
683 if (!minfo->xfer_alloc_table)
685 bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
687 /* Pre-initialize the buffer pointer to pre-allocated buffers */
688 for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
689 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
694 xfer->tx_message.buf = xfer->xfer_buf;
695 init_completion(&xfer->done);
698 ret = ti_sci_debugfs_create(pdev, info);
700 dev_warn(dev, "Failed to create debug file\n");
702 platform_set_drvdata(pdev, info);
706 cl->tx_block = false;
707 cl->rx_callback = ti_sci_rx_callback;
708 cl->knows_txdone = true;
710 spin_lock_init(&minfo->xfer_lock);
711 sema_init(&minfo->sem_xfer_count, desc->max_msgs);
713 info->chan_rx = mbox_request_channel_byname(cl, "rx");
714 if (IS_ERR(info->chan_rx)) {
715 ret = PTR_ERR(info->chan_rx);
719 info->chan_tx = mbox_request_channel_byname(cl, "tx");
720 if (IS_ERR(info->chan_tx)) {
721 ret = PTR_ERR(info->chan_tx);
724 ret = ti_sci_cmd_get_revision(info);
726 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
730 dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
731 info->handle.version.abi_major, info->handle.version.abi_minor,
732 info->handle.version.firmware_revision,
733 info->handle.version.firmware_description);
735 mutex_lock(&ti_sci_list_mutex);
736 list_add_tail(&info->node, &ti_sci_list);
737 mutex_unlock(&ti_sci_list_mutex);
739 return of_platform_populate(dev->of_node, NULL, NULL, dev);
741 if (!IS_ERR(info->chan_tx))
742 mbox_free_channel(info->chan_tx);
743 if (!IS_ERR(info->chan_rx))
744 mbox_free_channel(info->chan_rx);
745 debugfs_remove(info->d);
749 static int ti_sci_remove(struct platform_device *pdev)
751 struct ti_sci_info *info;
752 struct device *dev = &pdev->dev;
755 of_platform_depopulate(dev);
757 info = platform_get_drvdata(pdev);
759 mutex_lock(&ti_sci_list_mutex);
763 list_del(&info->node);
764 mutex_unlock(&ti_sci_list_mutex);
767 ti_sci_debugfs_destroy(pdev, info);
769 /* Safe to free channels since no more users */
770 mbox_free_channel(info->chan_tx);
771 mbox_free_channel(info->chan_rx);
777 static struct platform_driver ti_sci_driver = {
778 .probe = ti_sci_probe,
779 .remove = ti_sci_remove,
782 .of_match_table = of_match_ptr(ti_sci_of_match),
785 module_platform_driver(ti_sci_driver);
787 MODULE_LICENSE("GPL v2");
788 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
789 MODULE_AUTHOR("Nishanth Menon");
790 MODULE_ALIAS("platform:ti-sci");