Skip to contentSkip to conversation

Apple RTKit improvements

PATCH In review Mark Kettenis 2 participants 8 messages Apr 20, 2025 – Apr 28, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Apple RTKit improvements

This is a collection of improvements for the Apple RTKit code that we have been carrying downstream for some time now.

Hector Martin (6):
arm: apple: rtkit: Add support for AP power & syslogs
arm: apple: rtkit: Add default buffer handlers
arm: apple: rtkit: Add a generic RTKit helper driver
arm: apple: rtkit: Add OSLog buffer support
arm: apple: rtkit: Add endpoint field to buffers
arm: apple: rtkit: Support allocating OSLog out of SRAM in helper

 arch/arm/include/asm/arch-apple/rtkit.h |   5 +
 arch/arm/mach-apple/Makefile            |   1 +
 arch/arm/mach-apple/rtkit.c             | 234 ++++++++++++++++++------
 arch/arm/mach-apple/rtkit_helper.c      | 145 +++++++++++++++
 4 files changed, 333 insertions(+), 52 deletions(-)
 create mode 100644 arch/arm/mach-apple/rtkit_helper.c
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/6 arm: apple: rtkit: Add support for AP power & syslogs

From: Hector Martin <[email protected]>

This is required for MTP to work properly

---
 arch/arm/include/asm/arch-apple/rtkit.h |   2 +
 arch/arm/mach-apple/rtkit.c             | 148 ++++++++++++++++++------
 2 files changed, 112 insertions(+), 38 deletions(-)
arch/arm/include/asm/arch-apple/rtkit.h +2 -0
index eff18ddb9d2..b795215feb7 100644
@@ -26,4 +26,6 @@ struct apple_rtkit *apple_rtkit_init(struct mbox_chan *chan, void *cookie,
 				     apple_rtkit_shmem_destroy shmem_destroy);
 void apple_rtkit_free(struct apple_rtkit *rtk);
 int apple_rtkit_boot(struct apple_rtkit *rtk);
+int apple_rtkit_set_ap_power(struct apple_rtkit *rtk, int pwrstate);
+int apple_rtkit_poll(struct apple_rtkit *rtk, ulong timeout);
 int apple_rtkit_shutdown(struct apple_rtkit *rtk, int pwrstate);
arch/arm/mach-apple/rtkit.c +110 -38

196 lines not on this page · raw message

Signature · 1 line
2.49.0
#2 lore raw

PATCH 2/6 arm: apple: rtkit: Add default buffer handlers

In reply to Mark Kettenis

From: Hector Martin <[email protected]>

For devices without specific buffer methods, just assume we can give them raw memory pointers when they request a buffer.

---
 arch/arm/mach-apple/rtkit.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
arch/arm/mach-apple/rtkit.c +15 -1
index 433ffb259ee..5e8e282ec0e 100644
@@ -11,6 +11,7 @@
 #include <linux/apple-mailbox.h>
 #include <linux/bitfield.h>
 #include <linux/errno.h>
+#include <linux/sizes.h>
 #include <linux/types.h>
 
 #define APPLE_RTKIT_EP_MGMT 0
@@ -97,6 +98,13 @@ void apple_rtkit_free(struct apple_rtkit *rtk)
 			rtk->shmem_destroy(rtk->cookie, &rtk->crashlog_buffer);
 		if (rtk->ioreport_buffer.buffer)
 			rtk->shmem_destroy(rtk->cookie, &rtk->ioreport_buffer);
+	} else {
+		if (rtk->syslog_buffer.buffer)
+			free(rtk->syslog_buffer.buffer);
+		if (rtk->crashlog_buffer.buffer)
+			free(rtk->crashlog_buffer.buffer);
+		if (rtk->ioreport_buffer.buffer)
+			free(rtk->ioreport_buffer.buffer);
 	}
 	free(rtk);
 }
@@ -131,7 +139,7 @@ static int rtkit_handle_buf_req(struct apple_rtkit *rtk, int endpoint, struct ap
 
 	buf->dva = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_IOVA, msg->msg0);
 	buf->size = num_4kpages << 12;
-	buf->is_mapped = false;
+	buf->is_mapped = !!buf->dva;
 
 	if (rtk->shmem_setup) {
 		ret = rtk->shmem_setup(rtk->cookie, buf);
@@ -140,6 +148,12 @@ static int rtkit_handle_buf_req(struct apple_rtkit *rtk, int endpoint, struct ap
 			       endpoint);
 			return ret;
 		}
+	} else if (!buf->is_mapped){
+		buf->buffer = memalign(SZ_16K, ALIGN(buf->size, SZ_16K));
+		if (!buf->buffer)
+			return -ENOMEM;
+
+		buf->dva = (u64)buf->buffer;
 	}
 
 	if (!buf->is_mapped) {
Signature · 1 line
2.49.0
#3 lore raw

PATCH 3/6 arm: apple: rtkit: Add a generic RTKit helper driver

In reply to Mark Kettenis

From: Hector Martin <[email protected]>

This driver handles the MTP ASC coprocessor, which does not need any special handling on the RTKit side and communicates out-of-band.

---
 arch/arm/include/asm/arch-apple/rtkit.h |  2 +
 arch/arm/mach-apple/Makefile            |  1 +
 arch/arm/mach-apple/rtkit_helper.c      | 97 +++++++++++++++++++++++++
 3 files changed, 100 insertions(+)
 create mode 100644 arch/arm/mach-apple/rtkit_helper.c
arch/arm/include/asm/arch-apple/rtkit.h +2 -0
index b795215feb7..1b6c6ccd821 100644
@@ -29,3 +29,5 @@ int apple_rtkit_boot(struct apple_rtkit *rtk);
 int apple_rtkit_set_ap_power(struct apple_rtkit *rtk, int pwrstate);
 int apple_rtkit_poll(struct apple_rtkit *rtk, ulong timeout);
 int apple_rtkit_shutdown(struct apple_rtkit *rtk, int pwrstate);
+
+int apple_rtkit_helper_poll(struct udevice *dev, ulong timeout);
arch/arm/mach-apple/Makefile +1 -0
index 50b465b9473..d79a3a69592 100644
@@ -3,4 +3,5 @@
 obj-y += board.o
 obj-y += lowlevel_init.o
 obj-y += rtkit.o
+obj-$(CONFIG_APPLE_MTP_KEYB) += rtkit_helper.o
 obj-$(CONFIG_NVME_APPLE) += sart.o
arch/arm/mach-apple/rtkit_helper.c new file +97 -0

97 lines not on this page · raw message

Signature · 1 line
2.49.0
#4 lore raw

PATCH 4/6 arm: apple: rtkit: Add OSLog buffer support

In reply to Mark Kettenis

From: Hector Martin <[email protected]>

This will work for u-boot itself, but needs a special workaround in the MTP driver for Linux handoff to work.

---
 arch/arm/mach-apple/rtkit.c | 69 ++++++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 13 deletions(-)
arch/arm/mach-apple/rtkit.c +56 -13
index 5e8e282ec0e..d294ee09350 100644
@@ -19,6 +19,7 @@
 #define APPLE_RTKIT_EP_SYSLOG 2
 #define APPLE_RTKIT_EP_DEBUG 3
 #define APPLE_RTKIT_EP_IOREPORT 4
+#define APPLE_RTKIT_EP_OSLOG 8
 #define APPLE_RTKIT_EP_TRACEKIT 10
 
 /* Messages for management endpoint. */
@@ -47,6 +48,11 @@
 #define APPLE_RTKIT_MGMT_EPMAP_REPLY 8
 #define APPLE_RTKIT_MGMT_EPMAP_REPLY_MORE BIT(0)
 
+#define APPLE_RTKIT_OSLOG_TYPE GENMASK_ULL(63, 56)
+#define APPLE_RTKIT_OSLOG_BUFFER_REQUEST 1
+#define APPLE_RTKIT_OSLOG_SIZE GENMASK_ULL(55, 36)
+#define APPLE_RTKIT_OSLOG_IOVA GENMASK_ULL(35, 0)
+
 #define APPLE_RTKIT_MIN_SUPPORTED_VERSION 11
 #define APPLE_RTKIT_MAX_SUPPORTED_VERSION 12
 
@@ -66,6 +72,7 @@ struct apple_rtkit {
 	struct apple_rtkit_buffer syslog_buffer;
 	struct apple_rtkit_buffer crashlog_buffer;
 	struct apple_rtkit_buffer ioreport_buffer;
+	struct apple_rtkit_buffer oslog_buffer;
 
 	int iop_pwr;
 	int ap_pwr;
@@ -98,6 +105,8 @@ void apple_rtkit_free(struct apple_rtkit *rtk)
 			rtk->shmem_destroy(rtk->cookie, &rtk->crashlog_buffer);
 		if (rtk->ioreport_buffer.buffer)
 			rtk->shmem_destroy(rtk->cookie, &rtk->ioreport_buffer);
+		if (rtk->oslog_buffer.buffer)
+			rtk->shmem_destroy(rtk->cookie, &rtk->oslog_buffer);
 	} else {
 		if (rtk->syslog_buffer.buffer)
 			free(rtk->syslog_buffer.buffer);
@@ -105,6 +114,8 @@ void apple_rtkit_free(struct apple_rtkit *rtk)
 			free(rtk->crashlog_buffer.buffer);
 		if (rtk->ioreport_buffer.buffer)
 			free(rtk->ioreport_buffer.buffer);
+		if (rtk->oslog_buffer.buffer)
+			free(rtk->oslog_buffer.buffer);
 	}
 	free(rtk);
 }
@@ -112,16 +123,8 @@ void apple_rtkit_free(struct apple_rtkit *rtk)
 static int rtkit_handle_buf_req(struct apple_rtkit *rtk, int endpoint, struct apple_mbox_msg *msg)
 {
 	struct apple_rtkit_buffer *buf;
-	size_t num_4kpages;
 	int ret;
 
-	num_4kpages = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_SIZE, msg->msg0);
-
-	if (num_4kpages == 0) {
-		printf("%s: unexpected request for buffer without size\n", __func__);
-		return -1;
-	}
-
 	switch (endpoint) {
 	case APPLE_RTKIT_EP_CRASHLOG:
 		buf = &rtk->crashlog_buffer;
@@ -132,13 +135,31 @@ static int rtkit_handle_buf_req(struct apple_rtkit *rtk, int endpoint, struct ap
 	case APPLE_RTKIT_EP_IOREPORT:
 		buf = &rtk->ioreport_buffer;
 		break;
+	case APPLE_RTKIT_EP_OSLOG:
+		buf = &rtk->oslog_buffer;
+		break;
 	default:
 		printf("%s: unexpected endpoint %d\n", __func__, endpoint);
 		return -1;
 	}
 
+	switch (endpoint) {
+	case APPLE_RTKIT_EP_OSLOG:
+		buf->size = FIELD_GET(APPLE_RTKIT_OSLOG_SIZE, msg->msg0);
+		buf->dva = FIELD_GET(APPLE_RTKIT_OSLOG_IOVA, msg->msg0 << 12);
+		break;
+	default:
+		buf->size = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_SIZE, msg->msg0) << 12;
+		buf->dva = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_IOVA, msg->msg0);
+		break;
+	}
+
+	if (buf->size == 0) {
+		printf("%s: unexpected request for buffer without size\n", __func__);
+		return -1;
+	}
+
 	buf->dva = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_IOVA, msg->msg0);
-	buf->size = num_4kpages << 12;
 	buf->is_mapped = !!buf->dva;
 
 	if (rtk->shmem_setup) {
@@ -157,10 +178,18 @@ static int rtkit_handle_buf_req(struct apple_rtkit *rtk, int endpoint, struct ap
 	}
 
 	if (!buf->is_mapped) {
-		msg->msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_BUFFER_REQUEST) |
-				FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_SIZE, num_4kpages) |
-				FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_IOVA, buf->dva);
-		msg->msg1 = endpoint;
+		/* oslog uses different fields */
+		if (endpoint == APPLE_RTKIT_EP_OSLOG) {
+			msg->msg0 = FIELD_PREP(APPLE_RTKIT_OSLOG_TYPE,
+					       APPLE_RTKIT_OSLOG_BUFFER_REQUEST);
+			msg->msg0 |= FIELD_PREP(APPLE_RTKIT_OSLOG_SIZE, buf->size);
+			msg->msg0 |= FIELD_PREP(APPLE_RTKIT_OSLOG_IOVA, buf->dva >> 12);
+		} else {
+			msg->msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE,
+					   APPLE_RTKIT_BUFFER_REQUEST);
+			msg->msg0 |= FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_SIZE, buf->size >> 12);
+			msg->msg0 |= FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_IOVA, buf->dva);
+		}
 
 		return mbox_send(rtk->chan, msg);
 	}
@@ -193,6 +222,20 @@ int apple_rtkit_poll(struct apple_rtkit *rtk, ulong timeout)
 		}
 	}
 
+	if (endpoint == APPLE_RTKIT_EP_OSLOG) {
+		msgtype = FIELD_GET(APPLE_RTKIT_OSLOG_TYPE, msg.msg0);
+
+		if (msgtype == APPLE_RTKIT_OSLOG_BUFFER_REQUEST) {
+			ret = rtkit_handle_buf_req(rtk, endpoint, &msg);
+			if (ret < 0)
+				return ret;
+			return 0;
+		} else {
+			/* Ignore */
+			return 0;
+		}
+	}
+
 	if (endpoint == APPLE_RTKIT_EP_IOREPORT) {
 		// these two messages have to be ack-ed for proper startup
 		if (msgtype == 0xc || msgtype == 0x8) {
Signature · 1 line
2.49.0
#5 lore raw

PATCH 5/6 arm: apple: rtkit: Add endpoint field to buffers

In reply to Mark Kettenis

From: Hector Martin <[email protected]>

To be used for special-case oslog support in rtkit-helper.

---
 arch/arm/include/asm/arch-apple/rtkit.h | 1 +
 arch/arm/mach-apple/rtkit.c             | 1 +
 2 files changed, 2 insertions(+)
arch/arm/include/asm/arch-apple/rtkit.h +1 -0
index 1b6c6ccd821..4b11e2a72dc 100644
@@ -12,6 +12,7 @@ struct apple_rtkit_buffer {
 	u64 dva;
 	size_t size;
 	bool is_mapped;
+	int endpoint;
 };
 
 typedef int (*apple_rtkit_shmem_setup)(void *cookie,
arch/arm/mach-apple/rtkit.c +1 -0
index d294ee09350..f3561543a35 100644
@@ -161,6 +161,7 @@ static int rtkit_handle_buf_req(struct apple_rtkit *rtk, int endpoint, struct ap
 
 	buf->dva = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_IOVA, msg->msg0);
 	buf->is_mapped = !!buf->dva;
+	buf->endpoint = endpoint;
 
 	if (rtk->shmem_setup) {
 		ret = rtk->shmem_setup(rtk->cookie, buf);
Signature · 1 line
2.49.0
#6 lore raw

PATCH 6/6 arm: apple: rtkit: Support allocating OSLog out of SRAM in helper

In reply to Mark Kettenis

From: Hector Martin <[email protected]>

The new OSLog region in MTP (firmware 13.3+) persists on handoff to Linux. To avoid having to come up with some weird DART handoff or DAPF tricks, let's just steal some of the coprocessor's dedicated SRAM. This keeps it happy and Linux doesn't need any special handoff then.

---
 arch/arm/mach-apple/rtkit_helper.c | 50 +++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)
arch/arm/mach-apple/rtkit_helper.c +49 -1
index 3b755008195..b7d60e15700 100644
@@ -16,12 +16,60 @@
 #define REG_CPU_CTRL		0x0044
 #define  REG_CPU_CTRL_RUN	BIT(4)
 
+#define APPLE_RTKIT_EP_OSLOG 8
+
 struct rtkit_helper_priv {
 	void *asc;		/* ASC registers */
 	struct mbox_chan chan;
 	struct apple_rtkit *rtk;
+	bool sram_stolen;
 };
 
+static int shmem_setup(void *cookie, struct apple_rtkit_buffer *buf) {
+	struct udevice *dev = cookie;
+	struct rtkit_helper_priv *priv = dev_get_priv(dev);
+
+	if (!buf->is_mapped) {
+		/*
+		 * Special case: The OSLog buffer on MTP persists on Linux handoff.
+		 * Steal some SRAM instead of putting this in DRAM, so we don't
+		 * have to hand off DART/DAPF mappings.
+		 */
+		if (buf->endpoint == APPLE_RTKIT_EP_OSLOG) {
+			if (priv->sram_stolen) {
+				printf("%s: Tried to map more than one OSLog buffer out of SRAM\n",
+				       __func__);
+			} else {
+				fdt_size_t size;
+				fdt_addr_t addr;
+
+				addr = dev_read_addr_size_name(dev, "sram", &size);
+
+				if (addr != FDT_ADDR_T_NONE) {
+					buf->dva = ALIGN_DOWN(addr + size - buf->size, SZ_16K);
+					priv->sram_stolen = true;
+
+					return 0;
+				} else {
+					printf("%s: No SRAM, falling back to DRAM\n", __func__);
+				}
+			}
+		}
+
+		buf->buffer = memalign(SZ_16K, ALIGN(buf->size, SZ_16K));
+		if (!buf->buffer)
+			return -ENOMEM;
+
+		buf->dva = (u64)buf->buffer;
+	}
+	return 0;
+}
+
+static void shmem_destroy(void *cookie, struct apple_rtkit_buffer *buf) {
+	if (buf->buffer)
+		free(buf->buffer);
+}
+
 static int rtkit_helper_probe(struct udevice *dev)
 {
 	struct rtkit_helper_priv *priv = dev_get_priv(dev);
@@ -39,7 +87,7 @@ static int rtkit_helper_probe(struct udevice *dev)
 	ctrl = readl(priv->asc + REG_CPU_CTRL);
 	writel(ctrl | REG_CPU_CTRL_RUN, priv->asc + REG_CPU_CTRL);
 
-	priv->rtk = apple_rtkit_init(&priv->chan, priv, NULL, NULL);
+	priv->rtk = apple_rtkit_init(&priv->chan, dev, shmem_setup, shmem_destroy);
 	if (!priv->rtk)
 		return -ENOMEM;
 
Signature · 1 line
2.49.0
#7 lore raw
In reply to Mark Kettenis
Quoted 11 lines · Mark Kettenis

This is a collection of improvements for the Apple RTKit code that we have been carrying downstream for some time now.

Hector Martin (6):
arm: apple: rtkit: Add support for AP power & syslogs
arm: apple: rtkit: Add default buffer handlers
arm: apple: rtkit: Add a generic RTKit helper driver
arm: apple: rtkit: Add OSLog buffer support
arm: apple: rtkit: Add endpoint field to buffers
arm: apple: rtkit: Support allocating OSLog out of SRAM in helper

Quoted 1 line

[...]

Applied to u-boot/master, thanks!

[1/6] arm: apple: rtkit: Add support for AP power & syslogs

      commit: 72bc04b8744c8b2756cbf115a88bc48fbde57c6b
[2/6] arm: apple: rtkit: Add default buffer handlers
      commit: 89cb15788a16f2568083d984c547ff41c56a2d68
[3/6] arm: apple: rtkit: Add a generic RTKit helper driver
      commit: 7bac7f5f012f4990ac28116839043cf588499e3a
[4/6] arm: apple: rtkit: Add OSLog buffer support
      commit: 082789a4f7a1a7bac46ef75f98acf93ee8bfe837
[5/6] arm: apple: rtkit: Add endpoint field to buffers
      commit: fe593cc8ed9b9616eca9d094be1b1557824dcea4
[6/6] arm: apple: rtkit: Support allocating OSLog out of SRAM in helper
      commit: 4a55e7f0311df145662831576bbe0ed2113227ce
Signature · 1 line
Tom