mailbox-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2015 ST Microelectronics
  4. *
  5. * Author: Lee Jones <lee.jones@linaro.org>
  6. */
  7. #include <linux/debugfs.h>
  8. #include <linux/err.h>
  9. #include <linux/fs.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/mailbox_client.h>
  13. #include <linux/module.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/sched/signal.h>
  22. #define MBOX_MAX_SIG_LEN 8
  23. #define MBOX_MAX_MSG_LEN 128
  24. #define MBOX_BYTES_PER_LINE 16
  25. #define MBOX_HEXDUMP_LINE_LEN ((MBOX_BYTES_PER_LINE * 4) + 2)
  26. #define MBOX_HEXDUMP_MAX_LEN (MBOX_HEXDUMP_LINE_LEN * \
  27. (MBOX_MAX_MSG_LEN / MBOX_BYTES_PER_LINE))
  28. static bool mbox_data_ready;
  29. struct mbox_test_device {
  30. struct device *dev;
  31. void __iomem *tx_mmio;
  32. void __iomem *rx_mmio;
  33. struct mbox_chan *tx_channel;
  34. struct mbox_chan *rx_channel;
  35. char *rx_buffer;
  36. char *signal;
  37. char *message;
  38. spinlock_t lock;
  39. struct mutex mutex;
  40. wait_queue_head_t waitq;
  41. struct fasync_struct *async_queue;
  42. struct dentry *root_debugfs_dir;
  43. };
  44. static ssize_t mbox_test_signal_write(struct file *filp,
  45. const char __user *userbuf,
  46. size_t count, loff_t *ppos)
  47. {
  48. struct mbox_test_device *tdev = filp->private_data;
  49. if (!tdev->tx_channel) {
  50. dev_err(tdev->dev, "Channel cannot do Tx\n");
  51. return -EINVAL;
  52. }
  53. if (count > MBOX_MAX_SIG_LEN) {
  54. dev_err(tdev->dev,
  55. "Signal length %zd greater than max allowed %d\n",
  56. count, MBOX_MAX_SIG_LEN);
  57. return -EINVAL;
  58. }
  59. /* Only allocate memory if we need to */
  60. if (!tdev->signal) {
  61. tdev->signal = kzalloc(MBOX_MAX_SIG_LEN, GFP_KERNEL);
  62. if (!tdev->signal)
  63. return -ENOMEM;
  64. }
  65. if (copy_from_user(tdev->signal, userbuf, count)) {
  66. kfree(tdev->signal);
  67. tdev->signal = NULL;
  68. return -EFAULT;
  69. }
  70. return count;
  71. }
  72. static const struct file_operations mbox_test_signal_ops = {
  73. .write = mbox_test_signal_write,
  74. .open = simple_open,
  75. .llseek = generic_file_llseek,
  76. };
  77. static int mbox_test_message_fasync(int fd, struct file *filp, int on)
  78. {
  79. struct mbox_test_device *tdev = filp->private_data;
  80. return fasync_helper(fd, filp, on, &tdev->async_queue);
  81. }
  82. static ssize_t mbox_test_message_write(struct file *filp,
  83. const char __user *userbuf,
  84. size_t count, loff_t *ppos)
  85. {
  86. struct mbox_test_device *tdev = filp->private_data;
  87. char *message;
  88. void *data;
  89. int ret;
  90. if (!tdev->tx_channel) {
  91. dev_err(tdev->dev, "Channel cannot do Tx\n");
  92. return -EINVAL;
  93. }
  94. if (count > MBOX_MAX_MSG_LEN) {
  95. dev_err(tdev->dev,
  96. "Message length %zd greater than max allowed %d\n",
  97. count, MBOX_MAX_MSG_LEN);
  98. return -EINVAL;
  99. }
  100. message = kzalloc(MBOX_MAX_MSG_LEN, GFP_KERNEL);
  101. if (!message)
  102. return -ENOMEM;
  103. mutex_lock(&tdev->mutex);
  104. tdev->message = message;
  105. ret = copy_from_user(tdev->message, userbuf, count);
  106. if (ret) {
  107. ret = -EFAULT;
  108. goto out;
  109. }
  110. /*
  111. * A separate signal is only of use if there is
  112. * MMIO to subsequently pass the message through
  113. */
  114. if (tdev->tx_mmio && tdev->signal) {
  115. print_hex_dump_bytes("Client: Sending: Signal: ", DUMP_PREFIX_ADDRESS,
  116. tdev->signal, MBOX_MAX_SIG_LEN);
  117. data = tdev->signal;
  118. } else
  119. data = tdev->message;
  120. print_hex_dump_bytes("Client: Sending: Message: ", DUMP_PREFIX_ADDRESS,
  121. tdev->message, MBOX_MAX_MSG_LEN);
  122. ret = mbox_send_message(tdev->tx_channel, data);
  123. if (ret < 0)
  124. dev_err(tdev->dev, "Failed to send message via mailbox\n");
  125. out:
  126. kfree(tdev->signal);
  127. kfree(tdev->message);
  128. tdev->signal = NULL;
  129. mutex_unlock(&tdev->mutex);
  130. return ret < 0 ? ret : count;
  131. }
  132. static bool mbox_test_message_data_ready(struct mbox_test_device *tdev)
  133. {
  134. bool data_ready;
  135. unsigned long flags;
  136. spin_lock_irqsave(&tdev->lock, flags);
  137. data_ready = mbox_data_ready;
  138. spin_unlock_irqrestore(&tdev->lock, flags);
  139. return data_ready;
  140. }
  141. static ssize_t mbox_test_message_read(struct file *filp, char __user *userbuf,
  142. size_t count, loff_t *ppos)
  143. {
  144. struct mbox_test_device *tdev = filp->private_data;
  145. unsigned long flags;
  146. char *touser, *ptr;
  147. int l = 0;
  148. int ret;
  149. DECLARE_WAITQUEUE(wait, current);
  150. touser = kzalloc(MBOX_HEXDUMP_MAX_LEN + 1, GFP_KERNEL);
  151. if (!touser)
  152. return -ENOMEM;
  153. if (!tdev->rx_channel) {
  154. ret = snprintf(touser, 20, "<NO RX CAPABILITY>\n");
  155. ret = simple_read_from_buffer(userbuf, count, ppos,
  156. touser, ret);
  157. goto kfree_err;
  158. }
  159. add_wait_queue(&tdev->waitq, &wait);
  160. do {
  161. __set_current_state(TASK_INTERRUPTIBLE);
  162. if (mbox_test_message_data_ready(tdev))
  163. break;
  164. if (filp->f_flags & O_NONBLOCK) {
  165. ret = -EAGAIN;
  166. goto waitq_err;
  167. }
  168. if (signal_pending(current)) {
  169. ret = -ERESTARTSYS;
  170. goto waitq_err;
  171. }
  172. schedule();
  173. } while (1);
  174. spin_lock_irqsave(&tdev->lock, flags);
  175. ptr = tdev->rx_buffer;
  176. while (l < MBOX_HEXDUMP_MAX_LEN) {
  177. hex_dump_to_buffer(ptr,
  178. MBOX_BYTES_PER_LINE,
  179. MBOX_BYTES_PER_LINE, 1, touser + l,
  180. MBOX_HEXDUMP_LINE_LEN, true);
  181. ptr += MBOX_BYTES_PER_LINE;
  182. l += MBOX_HEXDUMP_LINE_LEN;
  183. *(touser + (l - 1)) = '\n';
  184. }
  185. *(touser + l) = '\0';
  186. memset(tdev->rx_buffer, 0, MBOX_MAX_MSG_LEN);
  187. mbox_data_ready = false;
  188. spin_unlock_irqrestore(&tdev->lock, flags);
  189. ret = simple_read_from_buffer(userbuf, count, ppos, touser, MBOX_HEXDUMP_MAX_LEN);
  190. waitq_err:
  191. __set_current_state(TASK_RUNNING);
  192. remove_wait_queue(&tdev->waitq, &wait);
  193. kfree_err:
  194. kfree(touser);
  195. return ret;
  196. }
  197. static __poll_t
  198. mbox_test_message_poll(struct file *filp, struct poll_table_struct *wait)
  199. {
  200. struct mbox_test_device *tdev = filp->private_data;
  201. poll_wait(filp, &tdev->waitq, wait);
  202. if (mbox_test_message_data_ready(tdev))
  203. return EPOLLIN | EPOLLRDNORM;
  204. return 0;
  205. }
  206. static const struct file_operations mbox_test_message_ops = {
  207. .write = mbox_test_message_write,
  208. .read = mbox_test_message_read,
  209. .fasync = mbox_test_message_fasync,
  210. .poll = mbox_test_message_poll,
  211. .open = simple_open,
  212. .llseek = generic_file_llseek,
  213. };
  214. static int mbox_test_add_debugfs(struct platform_device *pdev,
  215. struct mbox_test_device *tdev)
  216. {
  217. if (!debugfs_initialized())
  218. return 0;
  219. tdev->root_debugfs_dir = debugfs_create_dir(dev_name(&pdev->dev), NULL);
  220. if (!tdev->root_debugfs_dir) {
  221. dev_err(&pdev->dev, "Failed to create Mailbox debugfs\n");
  222. return -EINVAL;
  223. }
  224. debugfs_create_file("message", 0600, tdev->root_debugfs_dir,
  225. tdev, &mbox_test_message_ops);
  226. debugfs_create_file("signal", 0200, tdev->root_debugfs_dir,
  227. tdev, &mbox_test_signal_ops);
  228. return 0;
  229. }
  230. static void mbox_test_receive_message(struct mbox_client *client, void *message)
  231. {
  232. struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
  233. unsigned long flags;
  234. spin_lock_irqsave(&tdev->lock, flags);
  235. if (tdev->rx_mmio) {
  236. memcpy_fromio(tdev->rx_buffer, tdev->rx_mmio, MBOX_MAX_MSG_LEN);
  237. print_hex_dump_bytes("Client: Received [MMIO]: ", DUMP_PREFIX_ADDRESS,
  238. tdev->rx_buffer, MBOX_MAX_MSG_LEN);
  239. } else if (message) {
  240. print_hex_dump_bytes("Client: Received [API]: ", DUMP_PREFIX_ADDRESS,
  241. message, MBOX_MAX_MSG_LEN);
  242. memcpy(tdev->rx_buffer, message, MBOX_MAX_MSG_LEN);
  243. }
  244. mbox_data_ready = true;
  245. spin_unlock_irqrestore(&tdev->lock, flags);
  246. wake_up_interruptible(&tdev->waitq);
  247. kill_fasync(&tdev->async_queue, SIGIO, POLL_IN);
  248. }
  249. static void mbox_test_prepare_message(struct mbox_client *client, void *message)
  250. {
  251. struct mbox_test_device *tdev = dev_get_drvdata(client->dev);
  252. if (tdev->tx_mmio) {
  253. if (tdev->signal)
  254. memcpy_toio(tdev->tx_mmio, tdev->message, MBOX_MAX_MSG_LEN);
  255. else
  256. memcpy_toio(tdev->tx_mmio, message, MBOX_MAX_MSG_LEN);
  257. }
  258. }
  259. static void mbox_test_message_sent(struct mbox_client *client,
  260. void *message, int r)
  261. {
  262. if (r)
  263. dev_warn(client->dev,
  264. "Client: Message could not be sent: %d\n", r);
  265. else
  266. dev_info(client->dev,
  267. "Client: Message sent\n");
  268. }
  269. static struct mbox_chan *
  270. mbox_test_request_channel(struct platform_device *pdev, const char *name)
  271. {
  272. struct mbox_client *client;
  273. struct mbox_chan *channel;
  274. client = devm_kzalloc(&pdev->dev, sizeof(*client), GFP_KERNEL);
  275. if (!client)
  276. return ERR_PTR(-ENOMEM);
  277. client->dev = &pdev->dev;
  278. client->rx_callback = mbox_test_receive_message;
  279. client->tx_prepare = mbox_test_prepare_message;
  280. client->tx_done = mbox_test_message_sent;
  281. client->tx_block = true;
  282. client->knows_txdone = false;
  283. client->tx_tout = 500;
  284. channel = mbox_request_channel_byname(client, name);
  285. if (IS_ERR(channel)) {
  286. dev_warn(&pdev->dev, "Failed to request %s channel\n", name);
  287. return NULL;
  288. }
  289. return channel;
  290. }
  291. static int mbox_test_probe(struct platform_device *pdev)
  292. {
  293. struct mbox_test_device *tdev;
  294. struct resource *res;
  295. resource_size_t size;
  296. int ret;
  297. tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL);
  298. if (!tdev)
  299. return -ENOMEM;
  300. /* It's okay for MMIO to be NULL */
  301. tdev->tx_mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  302. if (PTR_ERR(tdev->tx_mmio) == -EBUSY) {
  303. /* if reserved area in SRAM, try just ioremap */
  304. size = resource_size(res);
  305. tdev->tx_mmio = devm_ioremap(&pdev->dev, res->start, size);
  306. } else if (IS_ERR(tdev->tx_mmio)) {
  307. tdev->tx_mmio = NULL;
  308. }
  309. /* If specified, second reg entry is Rx MMIO */
  310. tdev->rx_mmio = devm_platform_get_and_ioremap_resource(pdev, 1, &res);
  311. if (PTR_ERR(tdev->rx_mmio) == -EBUSY) {
  312. size = resource_size(res);
  313. tdev->rx_mmio = devm_ioremap(&pdev->dev, res->start, size);
  314. } else if (IS_ERR(tdev->rx_mmio)) {
  315. tdev->rx_mmio = tdev->tx_mmio;
  316. }
  317. tdev->tx_channel = mbox_test_request_channel(pdev, "tx");
  318. tdev->rx_channel = mbox_test_request_channel(pdev, "rx");
  319. if (IS_ERR_OR_NULL(tdev->tx_channel) && IS_ERR_OR_NULL(tdev->rx_channel))
  320. return -EPROBE_DEFER;
  321. /* If Rx is not specified but has Rx MMIO, then Rx = Tx */
  322. if (!tdev->rx_channel && (tdev->rx_mmio != tdev->tx_mmio))
  323. tdev->rx_channel = tdev->tx_channel;
  324. tdev->dev = &pdev->dev;
  325. platform_set_drvdata(pdev, tdev);
  326. spin_lock_init(&tdev->lock);
  327. mutex_init(&tdev->mutex);
  328. if (tdev->rx_channel) {
  329. tdev->rx_buffer = devm_kzalloc(&pdev->dev,
  330. MBOX_MAX_MSG_LEN, GFP_KERNEL);
  331. if (!tdev->rx_buffer)
  332. return -ENOMEM;
  333. }
  334. ret = mbox_test_add_debugfs(pdev, tdev);
  335. if (ret)
  336. return ret;
  337. init_waitqueue_head(&tdev->waitq);
  338. dev_info(&pdev->dev, "Successfully registered\n");
  339. return 0;
  340. }
  341. static void mbox_test_remove(struct platform_device *pdev)
  342. {
  343. struct mbox_test_device *tdev = platform_get_drvdata(pdev);
  344. debugfs_remove_recursive(tdev->root_debugfs_dir);
  345. if (tdev->tx_channel)
  346. mbox_free_channel(tdev->tx_channel);
  347. if (tdev->rx_channel)
  348. mbox_free_channel(tdev->rx_channel);
  349. }
  350. static const struct of_device_id mbox_test_match[] = {
  351. { .compatible = "mailbox-test" },
  352. {},
  353. };
  354. MODULE_DEVICE_TABLE(of, mbox_test_match);
  355. static struct platform_driver mbox_test_driver = {
  356. .driver = {
  357. .name = "mailbox_test",
  358. .of_match_table = mbox_test_match,
  359. },
  360. .probe = mbox_test_probe,
  361. .remove_new = mbox_test_remove,
  362. };
  363. module_platform_driver(mbox_test_driver);
  364. MODULE_DESCRIPTION("Generic Mailbox Testing Facility");
  365. MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org");
  366. MODULE_LICENSE("GPL v2");