Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

soc: apple: mailbox: Add ASC/M3 mailbox driver

This new driver is based on the existing apple-mailbox driver, but
replaces the usage of the mailbox subsystem with directly exported
symbols.

As part of this refactor, this adds support for using the hardware FIFOs
(not supported in mailbox) and implicitly fixes a bunch of bugs caused
by bad interactions with the mailbox subsystem. It also adds runtime-PM
support.

The new config symbol is APPLE_MBOX, while the module name remains
identical ("apple-mailbox"). The configs are mutually exclusive in
Kconfig, to avoid conflicts.

Acked-by: Eric Curtin <ecurtin@redhat.com>
Acked-by: Neal Gompa <neal@gompa.dev>
Acked-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Signed-off-by: Hector Martin <marcan@marcan.st>

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