1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
5 * Copyright (C) 2021 The Asahi Linux Contributors
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.
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
19 #include <linux/apple-mailbox.h>
20 #include <linux/device.h>
21 #include <linux/gfp.h>
22 #include <linux/interrupt.h>
24 #include <linux/mailbox_controller.h>
25 #include <linux/module.h>
27 #include <linux/platform_device.h>
28 #include <linux/types.h>
30 #define APPLE_ASC_MBOX_CONTROL_FULL BIT(16)
31 #define APPLE_ASC_MBOX_CONTROL_EMPTY BIT(17)
33 #define APPLE_ASC_MBOX_A2I_CONTROL 0x110
34 #define APPLE_ASC_MBOX_A2I_SEND0 0x800
35 #define APPLE_ASC_MBOX_A2I_SEND1 0x808
36 #define APPLE_ASC_MBOX_A2I_RECV0 0x810
37 #define APPLE_ASC_MBOX_A2I_RECV1 0x818
39 #define APPLE_ASC_MBOX_I2A_CONTROL 0x114
40 #define APPLE_ASC_MBOX_I2A_SEND0 0x820
41 #define APPLE_ASC_MBOX_I2A_SEND1 0x828
42 #define APPLE_ASC_MBOX_I2A_RECV0 0x830
43 #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)
48 #define APPLE_M3_MBOX_A2I_CONTROL 0x50
49 #define APPLE_M3_MBOX_A2I_SEND0 0x60
50 #define APPLE_M3_MBOX_A2I_SEND1 0x68
51 #define APPLE_M3_MBOX_A2I_RECV0 0x70
52 #define APPLE_M3_MBOX_A2I_RECV1 0x78
54 #define APPLE_M3_MBOX_I2A_CONTROL 0x80
55 #define APPLE_M3_MBOX_I2A_SEND0 0x90
56 #define APPLE_M3_MBOX_I2A_SEND1 0x98
57 #define APPLE_M3_MBOX_I2A_RECV0 0xa0
58 #define APPLE_M3_MBOX_I2A_RECV1 0xa8
60 #define APPLE_M3_MBOX_IRQ_ENABLE 0x48
61 #define APPLE_M3_MBOX_IRQ_ACK 0x4c
62 #define APPLE_M3_MBOX_IRQ_A2I_EMPTY BIT(0)
63 #define APPLE_M3_MBOX_IRQ_A2I_NOT_EMPTY BIT(1)
64 #define APPLE_M3_MBOX_IRQ_I2A_EMPTY BIT(2)
65 #define APPLE_M3_MBOX_IRQ_I2A_NOT_EMPTY BIT(3)
67 #define APPLE_MBOX_MSG1_OUTCNT GENMASK(56, 52)
68 #define APPLE_MBOX_MSG1_INCNT GENMASK(51, 48)
69 #define APPLE_MBOX_MSG1_OUTPTR GENMASK(47, 44)
70 #define APPLE_MBOX_MSG1_INPTR GENMASK(43, 40)
71 #define APPLE_MBOX_MSG1_MSG GENMASK(31, 0)
73 struct apple_mbox_hw {
74 unsigned int control_full;
75 unsigned int control_empty;
77 unsigned int a2i_control;
78 unsigned int a2i_send0;
79 unsigned int a2i_send1;
81 unsigned int i2a_control;
82 unsigned int i2a_recv0;
83 unsigned int i2a_recv1;
85 bool has_irq_controls;
86 unsigned int irq_enable;
88 unsigned int irq_bit_recv_not_empty;
89 unsigned int irq_bit_send_empty;
94 const struct apple_mbox_hw *hw;
96 int irq_recv_not_empty;
99 struct mbox_chan chan;
102 struct mbox_controller controller;
105 static const struct of_device_id apple_mbox_of_match[];
107 static bool apple_mbox_hw_can_send(struct apple_mbox *apple_mbox)
110 readl_relaxed(apple_mbox->regs + apple_mbox->hw->a2i_control);
112 return !(mbox_ctrl & apple_mbox->hw->control_full);
115 static int apple_mbox_hw_send(struct apple_mbox *apple_mbox,
116 struct apple_mbox_msg *msg)
118 if (!apple_mbox_hw_can_send(apple_mbox))
121 dev_dbg(apple_mbox->dev, "> TX %016llx %08x\n", msg->msg0, msg->msg1);
123 writeq_relaxed(msg->msg0, apple_mbox->regs + apple_mbox->hw->a2i_send0);
124 writeq_relaxed(FIELD_PREP(APPLE_MBOX_MSG1_MSG, msg->msg1),
125 apple_mbox->regs + apple_mbox->hw->a2i_send1);
130 static bool apple_mbox_hw_can_recv(struct apple_mbox *apple_mbox)
133 readl_relaxed(apple_mbox->regs + apple_mbox->hw->i2a_control);
135 return !(mbox_ctrl & apple_mbox->hw->control_empty);
138 static int apple_mbox_hw_recv(struct apple_mbox *apple_mbox,
139 struct apple_mbox_msg *msg)
141 if (!apple_mbox_hw_can_recv(apple_mbox))
144 msg->msg0 = readq_relaxed(apple_mbox->regs + apple_mbox->hw->i2a_recv0);
145 msg->msg1 = FIELD_GET(
147 readq_relaxed(apple_mbox->regs + apple_mbox->hw->i2a_recv1));
149 dev_dbg(apple_mbox->dev, "< RX %016llx %08x\n", msg->msg0, msg->msg1);
154 static int apple_mbox_chan_send_data(struct mbox_chan *chan, void *data)
156 struct apple_mbox *apple_mbox = chan->con_priv;
157 struct apple_mbox_msg *msg = data;
160 ret = apple_mbox_hw_send(apple_mbox, msg);
165 * The interrupt is level triggered and will keep firing as long as the
166 * FIFO is empty. It will also keep firing if the FIFO was empty
167 * at any point in the past until it has been acknowledged at the
168 * mailbox level. By acknowledging it here we can ensure that we will
169 * only get the interrupt once the FIFO has been cleared again.
170 * If the FIFO is already empty before the ack it will fire again
171 * immediately after the ack.
173 if (apple_mbox->hw->has_irq_controls) {
174 writel_relaxed(apple_mbox->hw->irq_bit_send_empty,
175 apple_mbox->regs + apple_mbox->hw->irq_ack);
177 enable_irq(apple_mbox->irq_send_empty);
182 static irqreturn_t apple_mbox_send_empty_irq(int irq, void *data)
184 struct apple_mbox *apple_mbox = data;
187 * We don't need to acknowledge the interrupt at the mailbox level
188 * here even if supported by the hardware. It will keep firing but that
189 * doesn't matter since it's disabled at the main interrupt controller.
190 * apple_mbox_chan_send_data will acknowledge it before enabling
191 * it at the main controller again.
193 disable_irq_nosync(apple_mbox->irq_send_empty);
194 mbox_chan_txdone(&apple_mbox->chan, 0);
198 static irqreturn_t apple_mbox_recv_irq(int irq, void *data)
200 struct apple_mbox *apple_mbox = data;
201 struct apple_mbox_msg msg;
203 while (apple_mbox_hw_recv(apple_mbox, &msg) == 0)
204 mbox_chan_received_data(&apple_mbox->chan, (void *)&msg);
207 * The interrupt will keep firing even if there are no more messages
208 * unless we also acknowledge it at the mailbox level here.
209 * There's no race if a message comes in between the check in the while
210 * loop above and the ack below: If a new messages arrives inbetween
211 * those two the interrupt will just fire again immediately after the
212 * ack since it's level triggered.
214 if (apple_mbox->hw->has_irq_controls) {
215 writel_relaxed(apple_mbox->hw->irq_bit_recv_not_empty,
216 apple_mbox->regs + apple_mbox->hw->irq_ack);
222 static int apple_mbox_chan_startup(struct mbox_chan *chan)
224 struct apple_mbox *apple_mbox = chan->con_priv;
227 * Only some variants of this mailbox HW provide interrupt control
228 * at the mailbox level. We therefore need to handle enabling/disabling
229 * interrupts at the main interrupt controller anyway for hardware that
230 * doesn't. Just always keep the interrupts we care about enabled at
231 * the mailbox level so that both hardware revisions behave almost
234 if (apple_mbox->hw->has_irq_controls) {
235 writel_relaxed(apple_mbox->hw->irq_bit_recv_not_empty |
236 apple_mbox->hw->irq_bit_send_empty,
237 apple_mbox->regs + apple_mbox->hw->irq_enable);
240 enable_irq(apple_mbox->irq_recv_not_empty);
244 static void apple_mbox_chan_shutdown(struct mbox_chan *chan)
246 struct apple_mbox *apple_mbox = chan->con_priv;
248 disable_irq(apple_mbox->irq_recv_not_empty);
251 static const struct mbox_chan_ops apple_mbox_ops = {
252 .send_data = apple_mbox_chan_send_data,
253 .startup = apple_mbox_chan_startup,
254 .shutdown = apple_mbox_chan_shutdown,
257 static struct mbox_chan *apple_mbox_of_xlate(struct mbox_controller *mbox,
258 const struct of_phandle_args *args)
260 if (args->args_count != 0)
261 return ERR_PTR(-EINVAL);
263 return &mbox->chans[0];
266 static int apple_mbox_probe(struct platform_device *pdev)
269 const struct of_device_id *match;
271 struct apple_mbox *mbox;
272 struct device *dev = &pdev->dev;
274 match = of_match_node(apple_mbox_of_match, pdev->dev.of_node);
280 mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
283 platform_set_drvdata(pdev, mbox);
286 mbox->regs = devm_platform_ioremap_resource(pdev, 0);
287 if (IS_ERR(mbox->regs))
288 return PTR_ERR(mbox->regs);
290 mbox->hw = match->data;
291 mbox->irq_recv_not_empty =
292 platform_get_irq_byname(pdev, "recv-not-empty");
293 if (mbox->irq_recv_not_empty < 0)
296 mbox->irq_send_empty = platform_get_irq_byname(pdev, "send-empty");
297 if (mbox->irq_send_empty < 0)
300 mbox->controller.dev = mbox->dev;
301 mbox->controller.num_chans = 1;
302 mbox->controller.chans = &mbox->chan;
303 mbox->controller.ops = &apple_mbox_ops;
304 mbox->controller.txdone_irq = true;
305 mbox->controller.of_xlate = apple_mbox_of_xlate;
306 mbox->chan.con_priv = mbox;
308 irqname = devm_kasprintf(dev, GFP_KERNEL, "%s-recv", dev_name(dev));
312 ret = devm_request_threaded_irq(dev, mbox->irq_recv_not_empty, NULL,
314 IRQF_NO_AUTOEN | IRQF_ONESHOT, irqname,
319 irqname = devm_kasprintf(dev, GFP_KERNEL, "%s-send", dev_name(dev));
323 ret = devm_request_irq(dev, mbox->irq_send_empty,
324 apple_mbox_send_empty_irq, IRQF_NO_AUTOEN,
329 return devm_mbox_controller_register(dev, &mbox->controller);
332 static const struct apple_mbox_hw apple_mbox_asc_hw = {
333 .control_full = APPLE_ASC_MBOX_CONTROL_FULL,
334 .control_empty = APPLE_ASC_MBOX_CONTROL_EMPTY,
336 .a2i_control = APPLE_ASC_MBOX_A2I_CONTROL,
337 .a2i_send0 = APPLE_ASC_MBOX_A2I_SEND0,
338 .a2i_send1 = APPLE_ASC_MBOX_A2I_SEND1,
340 .i2a_control = APPLE_ASC_MBOX_I2A_CONTROL,
341 .i2a_recv0 = APPLE_ASC_MBOX_I2A_RECV0,
342 .i2a_recv1 = APPLE_ASC_MBOX_I2A_RECV1,
344 .has_irq_controls = false,
347 static const struct apple_mbox_hw apple_mbox_m3_hw = {
348 .control_full = APPLE_M3_MBOX_CONTROL_FULL,
349 .control_empty = APPLE_M3_MBOX_CONTROL_EMPTY,
351 .a2i_control = APPLE_M3_MBOX_A2I_CONTROL,
352 .a2i_send0 = APPLE_M3_MBOX_A2I_SEND0,
353 .a2i_send1 = APPLE_M3_MBOX_A2I_SEND1,
355 .i2a_control = APPLE_M3_MBOX_I2A_CONTROL,
356 .i2a_recv0 = APPLE_M3_MBOX_I2A_RECV0,
357 .i2a_recv1 = APPLE_M3_MBOX_I2A_RECV1,
359 .has_irq_controls = true,
360 .irq_enable = APPLE_M3_MBOX_IRQ_ENABLE,
361 .irq_ack = APPLE_M3_MBOX_IRQ_ACK,
362 .irq_bit_recv_not_empty = APPLE_M3_MBOX_IRQ_I2A_NOT_EMPTY,
363 .irq_bit_send_empty = APPLE_M3_MBOX_IRQ_A2I_EMPTY,
366 static const struct of_device_id apple_mbox_of_match[] = {
367 { .compatible = "apple,asc-mailbox-v4", .data = &apple_mbox_asc_hw },
368 { .compatible = "apple,m3-mailbox-v2", .data = &apple_mbox_m3_hw },
371 MODULE_DEVICE_TABLE(of, apple_mbox_of_match);
373 static struct platform_driver apple_mbox_driver = {
375 .name = "apple-mailbox",
376 .of_match_table = apple_mbox_of_match,
378 .probe = apple_mbox_probe,
380 module_platform_driver(apple_mbox_driver);
382 MODULE_LICENSE("Dual MIT/GPL");
383 MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
384 MODULE_DESCRIPTION("Apple Mailbox driver");