mailbox.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // SPDX-License-Identifier: GPL-2.0-only OR MIT
  2. /*
  3. * Apple mailbox driver
  4. *
  5. * Copyright The Asahi Linux Contributors
  6. *
  7. * This driver adds support for two mailbox variants (called ASC and M3 by
  8. * Apple) found in Apple SoCs such as the M1. It consists of two FIFOs used to
  9. * exchange 64+32 bit messages between the main CPU and a co-processor.
  10. * Various coprocessors implement different IPC protocols based on these simple
  11. * messages and shared memory buffers.
  12. *
  13. * Both the main CPU and the co-processor see the same set of registers but
  14. * the first FIFO (A2I) is always used to transfer messages from the application
  15. * processor (us) to the I/O processor and the second one (I2A) for the
  16. * other direction.
  17. */
  18. #include <linux/bitfield.h>
  19. #include <linux/bits.h>
  20. #include <linux/delay.h>
  21. #include <linux/device.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/io.h>
  24. #include <linux/iopoll.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_platform.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/types.h>
  32. #include "mailbox.h"
  33. #define APPLE_ASC_MBOX_CONTROL_FULL BIT(16)
  34. #define APPLE_ASC_MBOX_CONTROL_EMPTY BIT(17)
  35. #define APPLE_ASC_MBOX_A2I_CONTROL 0x110
  36. #define APPLE_ASC_MBOX_A2I_SEND0 0x800
  37. #define APPLE_ASC_MBOX_A2I_SEND1 0x808
  38. #define APPLE_ASC_MBOX_A2I_RECV0 0x810
  39. #define APPLE_ASC_MBOX_A2I_RECV1 0x818
  40. #define APPLE_ASC_MBOX_I2A_CONTROL 0x114
  41. #define APPLE_ASC_MBOX_I2A_SEND0 0x820
  42. #define APPLE_ASC_MBOX_I2A_SEND1 0x828
  43. #define APPLE_ASC_MBOX_I2A_RECV0 0x830
  44. #define APPLE_ASC_MBOX_I2A_RECV1 0x838
  45. #define APPLE_M3_MBOX_CONTROL_FULL BIT(16)
  46. #define APPLE_M3_MBOX_CONTROL_EMPTY BIT(17)
  47. #define APPLE_M3_MBOX_A2I_CONTROL 0x50
  48. #define APPLE_M3_MBOX_A2I_SEND0 0x60
  49. #define APPLE_M3_MBOX_A2I_SEND1 0x68
  50. #define APPLE_M3_MBOX_A2I_RECV0 0x70
  51. #define APPLE_M3_MBOX_A2I_RECV1 0x78
  52. #define APPLE_M3_MBOX_I2A_CONTROL 0x80
  53. #define APPLE_M3_MBOX_I2A_SEND0 0x90
  54. #define APPLE_M3_MBOX_I2A_SEND1 0x98
  55. #define APPLE_M3_MBOX_I2A_RECV0 0xa0
  56. #define APPLE_M3_MBOX_I2A_RECV1 0xa8
  57. #define APPLE_M3_MBOX_IRQ_ENABLE 0x48
  58. #define APPLE_M3_MBOX_IRQ_ACK 0x4c
  59. #define APPLE_M3_MBOX_IRQ_A2I_EMPTY BIT(0)
  60. #define APPLE_M3_MBOX_IRQ_A2I_NOT_EMPTY BIT(1)
  61. #define APPLE_M3_MBOX_IRQ_I2A_EMPTY BIT(2)
  62. #define APPLE_M3_MBOX_IRQ_I2A_NOT_EMPTY BIT(3)
  63. #define APPLE_MBOX_MSG1_OUTCNT GENMASK(56, 52)
  64. #define APPLE_MBOX_MSG1_INCNT GENMASK(51, 48)
  65. #define APPLE_MBOX_MSG1_OUTPTR GENMASK(47, 44)
  66. #define APPLE_MBOX_MSG1_INPTR GENMASK(43, 40)
  67. #define APPLE_MBOX_MSG1_MSG GENMASK(31, 0)
  68. #define APPLE_MBOX_TX_TIMEOUT 500
  69. struct apple_mbox_hw {
  70. unsigned int control_full;
  71. unsigned int control_empty;
  72. unsigned int a2i_control;
  73. unsigned int a2i_send0;
  74. unsigned int a2i_send1;
  75. unsigned int i2a_control;
  76. unsigned int i2a_recv0;
  77. unsigned int i2a_recv1;
  78. bool has_irq_controls;
  79. unsigned int irq_enable;
  80. unsigned int irq_ack;
  81. unsigned int irq_bit_recv_not_empty;
  82. unsigned int irq_bit_send_empty;
  83. };
  84. int apple_mbox_send(struct apple_mbox *mbox, const struct apple_mbox_msg msg,
  85. bool atomic)
  86. {
  87. unsigned long flags;
  88. int ret;
  89. u32 mbox_ctrl;
  90. long t;
  91. spin_lock_irqsave(&mbox->tx_lock, flags);
  92. mbox_ctrl = readl_relaxed(mbox->regs + mbox->hw->a2i_control);
  93. while (mbox_ctrl & mbox->hw->control_full) {
  94. if (atomic) {
  95. ret = readl_poll_timeout_atomic(
  96. mbox->regs + mbox->hw->a2i_control, mbox_ctrl,
  97. !(mbox_ctrl & mbox->hw->control_full), 100,
  98. APPLE_MBOX_TX_TIMEOUT * 1000);
  99. if (ret) {
  100. spin_unlock_irqrestore(&mbox->tx_lock, flags);
  101. return ret;
  102. }
  103. break;
  104. }
  105. /*
  106. * The interrupt is level triggered and will keep firing as long as the
  107. * FIFO is empty. It will also keep firing if the FIFO was empty
  108. * at any point in the past until it has been acknowledged at the
  109. * mailbox level. By acknowledging it here we can ensure that we will
  110. * only get the interrupt once the FIFO has been cleared again.
  111. * If the FIFO is already empty before the ack it will fire again
  112. * immediately after the ack.
  113. */
  114. if (mbox->hw->has_irq_controls) {
  115. writel_relaxed(mbox->hw->irq_bit_send_empty,
  116. mbox->regs + mbox->hw->irq_ack);
  117. }
  118. enable_irq(mbox->irq_send_empty);
  119. reinit_completion(&mbox->tx_empty);
  120. spin_unlock_irqrestore(&mbox->tx_lock, flags);
  121. t = wait_for_completion_interruptible_timeout(
  122. &mbox->tx_empty,
  123. msecs_to_jiffies(APPLE_MBOX_TX_TIMEOUT));
  124. if (t < 0)
  125. return t;
  126. else if (t == 0)
  127. return -ETIMEDOUT;
  128. spin_lock_irqsave(&mbox->tx_lock, flags);
  129. mbox_ctrl = readl_relaxed(mbox->regs + mbox->hw->a2i_control);
  130. }
  131. writeq_relaxed(msg.msg0, mbox->regs + mbox->hw->a2i_send0);
  132. writeq_relaxed(FIELD_PREP(APPLE_MBOX_MSG1_MSG, msg.msg1),
  133. mbox->regs + mbox->hw->a2i_send1);
  134. spin_unlock_irqrestore(&mbox->tx_lock, flags);
  135. return 0;
  136. }
  137. EXPORT_SYMBOL(apple_mbox_send);
  138. static irqreturn_t apple_mbox_send_empty_irq(int irq, void *data)
  139. {
  140. struct apple_mbox *mbox = data;
  141. /*
  142. * We don't need to acknowledge the interrupt at the mailbox level
  143. * here even if supported by the hardware. It will keep firing but that
  144. * doesn't matter since it's disabled at the main interrupt controller.
  145. * apple_mbox_send will acknowledge it before enabling
  146. * it at the main controller again.
  147. */
  148. spin_lock(&mbox->tx_lock);
  149. disable_irq_nosync(mbox->irq_send_empty);
  150. complete(&mbox->tx_empty);
  151. spin_unlock(&mbox->tx_lock);
  152. return IRQ_HANDLED;
  153. }
  154. static int apple_mbox_poll_locked(struct apple_mbox *mbox)
  155. {
  156. struct apple_mbox_msg msg;
  157. int ret = 0;
  158. u32 mbox_ctrl = readl_relaxed(mbox->regs + mbox->hw->i2a_control);
  159. while (!(mbox_ctrl & mbox->hw->control_empty)) {
  160. msg.msg0 = readq_relaxed(mbox->regs + mbox->hw->i2a_recv0);
  161. msg.msg1 = FIELD_GET(
  162. APPLE_MBOX_MSG1_MSG,
  163. readq_relaxed(mbox->regs + mbox->hw->i2a_recv1));
  164. mbox->rx(mbox, msg, mbox->cookie);
  165. ret++;
  166. mbox_ctrl = readl_relaxed(mbox->regs + mbox->hw->i2a_control);
  167. }
  168. /*
  169. * The interrupt will keep firing even if there are no more messages
  170. * unless we also acknowledge it at the mailbox level here.
  171. * There's no race if a message comes in between the check in the while
  172. * loop above and the ack below: If a new messages arrives inbetween
  173. * those two the interrupt will just fire again immediately after the
  174. * ack since it's level triggered.
  175. */
  176. if (mbox->hw->has_irq_controls) {
  177. writel_relaxed(mbox->hw->irq_bit_recv_not_empty,
  178. mbox->regs + mbox->hw->irq_ack);
  179. }
  180. return ret;
  181. }
  182. static irqreturn_t apple_mbox_recv_irq(int irq, void *data)
  183. {
  184. struct apple_mbox *mbox = data;
  185. spin_lock(&mbox->rx_lock);
  186. apple_mbox_poll_locked(mbox);
  187. spin_unlock(&mbox->rx_lock);
  188. return IRQ_HANDLED;
  189. }
  190. int apple_mbox_poll(struct apple_mbox *mbox)
  191. {
  192. unsigned long flags;
  193. int ret;
  194. spin_lock_irqsave(&mbox->rx_lock, flags);
  195. ret = apple_mbox_poll_locked(mbox);
  196. spin_unlock_irqrestore(&mbox->rx_lock, flags);
  197. return ret;
  198. }
  199. EXPORT_SYMBOL(apple_mbox_poll);
  200. int apple_mbox_start(struct apple_mbox *mbox)
  201. {
  202. int ret;
  203. if (mbox->active)
  204. return 0;
  205. ret = pm_runtime_resume_and_get(mbox->dev);
  206. if (ret)
  207. return ret;
  208. /*
  209. * Only some variants of this mailbox HW provide interrupt control
  210. * at the mailbox level. We therefore need to handle enabling/disabling
  211. * interrupts at the main interrupt controller anyway for hardware that
  212. * doesn't. Just always keep the interrupts we care about enabled at
  213. * the mailbox level so that both hardware revisions behave almost
  214. * the same.
  215. */
  216. if (mbox->hw->has_irq_controls) {
  217. writel_relaxed(mbox->hw->irq_bit_recv_not_empty |
  218. mbox->hw->irq_bit_send_empty,
  219. mbox->regs + mbox->hw->irq_enable);
  220. }
  221. enable_irq(mbox->irq_recv_not_empty);
  222. mbox->active = true;
  223. return 0;
  224. }
  225. EXPORT_SYMBOL(apple_mbox_start);
  226. void apple_mbox_stop(struct apple_mbox *mbox)
  227. {
  228. if (!mbox->active)
  229. return;
  230. mbox->active = false;
  231. disable_irq(mbox->irq_recv_not_empty);
  232. pm_runtime_mark_last_busy(mbox->dev);
  233. pm_runtime_put_autosuspend(mbox->dev);
  234. }
  235. EXPORT_SYMBOL(apple_mbox_stop);
  236. struct apple_mbox *apple_mbox_get(struct device *dev, int index)
  237. {
  238. struct of_phandle_args args;
  239. struct platform_device *pdev;
  240. struct apple_mbox *mbox;
  241. int ret;
  242. ret = of_parse_phandle_with_args(dev->of_node, "mboxes", "#mbox-cells",
  243. index, &args);
  244. if (ret || !args.np)
  245. return ERR_PTR(ret);
  246. pdev = of_find_device_by_node(args.np);
  247. of_node_put(args.np);
  248. if (!pdev)
  249. return ERR_PTR(-EPROBE_DEFER);
  250. mbox = platform_get_drvdata(pdev);
  251. if (!mbox)
  252. return ERR_PTR(-EPROBE_DEFER);
  253. if (!device_link_add(dev, &pdev->dev, DL_FLAG_AUTOREMOVE_CONSUMER))
  254. return ERR_PTR(-ENODEV);
  255. return mbox;
  256. }
  257. EXPORT_SYMBOL(apple_mbox_get);
  258. struct apple_mbox *apple_mbox_get_byname(struct device *dev, const char *name)
  259. {
  260. int index;
  261. index = of_property_match_string(dev->of_node, "mbox-names", name);
  262. if (index < 0)
  263. return ERR_PTR(index);
  264. return apple_mbox_get(dev, index);
  265. }
  266. EXPORT_SYMBOL(apple_mbox_get_byname);
  267. static int apple_mbox_probe(struct platform_device *pdev)
  268. {
  269. int ret;
  270. char *irqname;
  271. struct apple_mbox *mbox;
  272. struct device *dev = &pdev->dev;
  273. mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
  274. if (!mbox)
  275. return -ENOMEM;
  276. mbox->dev = &pdev->dev;
  277. mbox->hw = of_device_get_match_data(dev);
  278. if (!mbox->hw)
  279. return -EINVAL;
  280. mbox->regs = devm_platform_ioremap_resource(pdev, 0);
  281. if (IS_ERR(mbox->regs))
  282. return PTR_ERR(mbox->regs);
  283. mbox->irq_recv_not_empty =
  284. platform_get_irq_byname(pdev, "recv-not-empty");
  285. if (mbox->irq_recv_not_empty < 0)
  286. return -ENODEV;
  287. mbox->irq_send_empty = platform_get_irq_byname(pdev, "send-empty");
  288. if (mbox->irq_send_empty < 0)
  289. return -ENODEV;
  290. spin_lock_init(&mbox->rx_lock);
  291. spin_lock_init(&mbox->tx_lock);
  292. init_completion(&mbox->tx_empty);
  293. irqname = devm_kasprintf(dev, GFP_KERNEL, "%s-recv", dev_name(dev));
  294. if (!irqname)
  295. return -ENOMEM;
  296. ret = devm_request_irq(dev, mbox->irq_recv_not_empty,
  297. apple_mbox_recv_irq,
  298. IRQF_NO_AUTOEN | IRQF_NO_SUSPEND, irqname, mbox);
  299. if (ret)
  300. return ret;
  301. irqname = devm_kasprintf(dev, GFP_KERNEL, "%s-send", dev_name(dev));
  302. if (!irqname)
  303. return -ENOMEM;
  304. ret = devm_request_irq(dev, mbox->irq_send_empty,
  305. apple_mbox_send_empty_irq,
  306. IRQF_NO_AUTOEN | IRQF_NO_SUSPEND, irqname, mbox);
  307. if (ret)
  308. return ret;
  309. ret = devm_pm_runtime_enable(dev);
  310. if (ret)
  311. return ret;
  312. platform_set_drvdata(pdev, mbox);
  313. return 0;
  314. }
  315. static const struct apple_mbox_hw apple_mbox_asc_hw = {
  316. .control_full = APPLE_ASC_MBOX_CONTROL_FULL,
  317. .control_empty = APPLE_ASC_MBOX_CONTROL_EMPTY,
  318. .a2i_control = APPLE_ASC_MBOX_A2I_CONTROL,
  319. .a2i_send0 = APPLE_ASC_MBOX_A2I_SEND0,
  320. .a2i_send1 = APPLE_ASC_MBOX_A2I_SEND1,
  321. .i2a_control = APPLE_ASC_MBOX_I2A_CONTROL,
  322. .i2a_recv0 = APPLE_ASC_MBOX_I2A_RECV0,
  323. .i2a_recv1 = APPLE_ASC_MBOX_I2A_RECV1,
  324. .has_irq_controls = false,
  325. };
  326. static const struct apple_mbox_hw apple_mbox_m3_hw = {
  327. .control_full = APPLE_M3_MBOX_CONTROL_FULL,
  328. .control_empty = APPLE_M3_MBOX_CONTROL_EMPTY,
  329. .a2i_control = APPLE_M3_MBOX_A2I_CONTROL,
  330. .a2i_send0 = APPLE_M3_MBOX_A2I_SEND0,
  331. .a2i_send1 = APPLE_M3_MBOX_A2I_SEND1,
  332. .i2a_control = APPLE_M3_MBOX_I2A_CONTROL,
  333. .i2a_recv0 = APPLE_M3_MBOX_I2A_RECV0,
  334. .i2a_recv1 = APPLE_M3_MBOX_I2A_RECV1,
  335. .has_irq_controls = true,
  336. .irq_enable = APPLE_M3_MBOX_IRQ_ENABLE,
  337. .irq_ack = APPLE_M3_MBOX_IRQ_ACK,
  338. .irq_bit_recv_not_empty = APPLE_M3_MBOX_IRQ_I2A_NOT_EMPTY,
  339. .irq_bit_send_empty = APPLE_M3_MBOX_IRQ_A2I_EMPTY,
  340. };
  341. static const struct of_device_id apple_mbox_of_match[] = {
  342. { .compatible = "apple,asc-mailbox-v4", .data = &apple_mbox_asc_hw },
  343. { .compatible = "apple,m3-mailbox-v2", .data = &apple_mbox_m3_hw },
  344. {}
  345. };
  346. MODULE_DEVICE_TABLE(of, apple_mbox_of_match);
  347. static struct platform_driver apple_mbox_driver = {
  348. .driver = {
  349. .name = "apple-mailbox",
  350. .of_match_table = apple_mbox_of_match,
  351. },
  352. .probe = apple_mbox_probe,
  353. };
  354. module_platform_driver(apple_mbox_driver);
  355. MODULE_LICENSE("Dual MIT/GPL");
  356. MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
  357. MODULE_DESCRIPTION("Apple Mailbox driver");