virtio-mailbox-test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/debugfs.h>
  3. #include <linux/err.h>
  4. #include <linux/io.h>
  5. #include <linux/kernel.h>
  6. #include <linux/mailbox_client.h>
  7. #include <linux/module.h>
  8. #include <linux/moduleparam.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/virtio_mailbox.h>
  15. static bool tx_block = true;
  16. module_param(tx_block, bool, 0660);
  17. static bool debug = false;
  18. module_param(debug, bool, 0660);
  19. static char *chan_name = "vchan0";
  20. module_param(chan_name, charp, 0);
  21. static int MBOX_MAX_MSG_LEN = VIRTIO_MAILBOX_MSG_SIZE_DEFAULT;
  22. module_param(MBOX_MAX_MSG_LEN, int, 0660);
  23. static struct dentry *root_debugfs_dir;
  24. struct mbox_client_device {
  25. struct device *dev;
  26. void __iomem *tx_mmio;
  27. void __iomem *rx_mmio;
  28. struct mbox_chan *channel;
  29. char *rx_buffer;
  30. char *message;
  31. spinlock_t lock;
  32. };
  33. static ssize_t mbox_client_message_write(struct file *filp,
  34. const char __user *userbuf,
  35. size_t count, loff_t *ppos)
  36. {
  37. struct mbox_client_device *tdev = filp->private_data;
  38. void *data;
  39. int ret;
  40. if (!tdev->channel) {
  41. dev_err(tdev->dev, "Channel cannot do Tx\n");
  42. return -EINVAL;
  43. }
  44. if (count > MBOX_MAX_MSG_LEN)
  45. count = MBOX_MAX_MSG_LEN;
  46. tdev->message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
  47. if (!tdev->message)
  48. return -ENOMEM;
  49. ret = copy_from_user(tdev->message, userbuf, count);
  50. if (ret) {
  51. ret = -EFAULT;
  52. goto out;
  53. }
  54. data = tdev->message;
  55. if (debug)
  56. print_hex_dump(KERN_INFO, "Xmit: ", DUMP_PREFIX_NONE, 16, 1,
  57. tdev->message, MBOX_MAX_MSG_LEN, true);
  58. ret = mbox_send_message(tdev->channel, data);
  59. if (ret < 0)
  60. dev_err(tdev->dev, "Failed to send message via mailbox(%d)\n", ret);
  61. out:
  62. kfree(tdev->message);
  63. return ret < 0 ? ret : count;
  64. }
  65. static ssize_t mbox_client_message_read(struct file *filp,
  66. char __user *userbuf,
  67. size_t count, loff_t *ppos)
  68. {
  69. struct mbox_client_device *tdev = filp->private_data;
  70. unsigned long flags;
  71. if (debug)
  72. print_hex_dump(KERN_INFO, "Read: ", DUMP_PREFIX_NONE, 16, 1,
  73. tdev->rx_buffer, MBOX_MAX_MSG_LEN, true);
  74. spin_lock_irqsave(&tdev->lock, flags);
  75. memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
  76. spin_unlock_irqrestore(&tdev->lock, flags);
  77. return MBOX_MAX_MSG_LEN;
  78. }
  79. static const struct file_operations mbox_client_message_ops = {
  80. .write = mbox_client_message_write,
  81. .read = mbox_client_message_read,
  82. .open = simple_open,
  83. .llseek = generic_file_llseek,
  84. };
  85. static int index_names = 0;
  86. static bool debugfs_dir_created = false;
  87. static const char* file_names[] = {"mbox-client0", "mbox-client1"};
  88. static int mbox_client_add_debugfs(struct platform_device *pdev,
  89. struct mbox_client_device *tdev)
  90. {
  91. if (!debugfs_initialized())
  92. return 0;
  93. if (index_names > 2) {
  94. dev_err(&pdev->dev, "Max device index is 2\n");
  95. return 0;
  96. }
  97. if (!debugfs_dir_created) {
  98. root_debugfs_dir = debugfs_create_dir("mailbox",NULL);
  99. if (!root_debugfs_dir) {
  100. dev_err(&pdev->dev,
  101. "Failed to create mailbox debugfs\n");
  102. return -EINVAL;
  103. }
  104. debugfs_dir_created = true;
  105. }
  106. debugfs_create_file(file_names[index_names], 0600, root_debugfs_dir,
  107. tdev, &mbox_client_message_ops);
  108. index_names++;
  109. return 0;
  110. }
  111. static void mbox_client_receive_message(struct mbox_client *client,
  112. void *message)
  113. {
  114. struct mbox_client_device *tdev = dev_get_drvdata(client->dev);
  115. char *data = message;
  116. spin_lock(&tdev->lock);
  117. memcpy(tdev->rx_buffer, data, MBOX_MAX_MSG_LEN);
  118. spin_unlock(&tdev->lock);
  119. if (debug)
  120. print_hex_dump(KERN_INFO, "Recv: ", DUMP_PREFIX_NONE, 16, 1,
  121. tdev->rx_buffer, MBOX_MAX_MSG_LEN, true);
  122. }
  123. static void mbox_client_tx_done(struct mbox_client *cl, void *msg, int ret)
  124. {
  125. //printk("tx_done\n");
  126. }
  127. static struct mbox_chan * mbox_client_request_channel(struct platform_device *pdev,
  128. const char *name)
  129. {
  130. struct mbox_client *client;
  131. struct mbox_chan *channel;
  132. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  133. if (!client)
  134. return ERR_PTR(-ENOMEM);
  135. client->dev = &pdev->dev;
  136. client->tx_block = tx_block;
  137. client->knows_txdone = false;
  138. client->tx_tout = 1000;
  139. client->tx_done = mbox_client_tx_done;
  140. client->rx_callback = mbox_client_receive_message;
  141. channel = mbox_request_channel_byname(client, name);
  142. if (IS_ERR(channel)) {
  143. devm_kfree(&pdev->dev, client);
  144. dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
  145. return NULL;
  146. }
  147. return channel;
  148. }
  149. static int mbox_client_probe(struct platform_device *pdev)
  150. {
  151. struct mbox_client_device *tdev;
  152. int ret;
  153. tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
  154. if (!tdev)
  155. return -ENOMEM;
  156. tdev->channel = mbox_client_request_channel(pdev, chan_name);
  157. if (!tdev->channel) {
  158. dev_err(&pdev->dev, "Request channel failed\n");
  159. return -EPROBE_DEFER;
  160. }
  161. tdev->dev = &pdev->dev;
  162. platform_set_drvdata(pdev, tdev);
  163. spin_lock_init(&tdev->lock);
  164. tdev->rx_buffer = devm_kzalloc(&pdev->dev,
  165. MBOX_MAX_MSG_LEN, GFP_KERNEL);
  166. if (!tdev->rx_buffer)
  167. return -ENOMEM;
  168. ret = mbox_client_add_debugfs(pdev, tdev);
  169. if (ret)
  170. return ret;
  171. dev_info(&pdev->dev, "Successfully registered\n");
  172. return 0;
  173. }
  174. static int mbox_client_remove(struct platform_device *pdev)
  175. {
  176. struct mbox_client_device *tdev = platform_get_drvdata(pdev);
  177. debugfs_remove_recursive(root_debugfs_dir);
  178. if (tdev->channel)
  179. mbox_free_channel(tdev->channel);
  180. return 0;
  181. }
  182. static const struct of_device_id mbox_client_match[] = {
  183. { .compatible = "thead,vmbox-client" },
  184. {},
  185. };
  186. static struct platform_driver mbox_client_driver = {
  187. .driver = {
  188. .name = "vmbox-client",
  189. .of_match_table = mbox_client_match,
  190. },
  191. .probe = mbox_client_probe,
  192. .remove = mbox_client_remove,
  193. };
  194. module_platform_driver(mbox_client_driver);
  195. MODULE_DESCRIPTION("Virtio Mailbox Client test driver");
  196. MODULE_AUTHOR("Xianting Tian <xianting.tian@linux.alibaba.com>");
  197. MODULE_LICENSE("GPL");