Skip to content

Commit 9bd1d9a

Browse files
committed
soc: apple: Add RTKit IPC library
Apple SoCs such as the M1 come with multiple embedded co-processors running proprietary firmware. Communication with those is established over a simple mailbox using the RTKit IPC protocol. This cannot be implemented inside the mailbox subsystem since on top of communication over channels we also need support for starting, hibernating and resetting these co-processors. We also need to handle shared memory allocations differently depending on the co-processor and don't want to split that across multiple drivers. Reviewed-by: Arnd Bergmann <arnd@arndb.de> Signed-off-by: Sven Peter <sven@svenpeter.dev>
1 parent cbb0f00 commit 9bd1d9a

File tree

6 files changed

+1345
-0
lines changed

6 files changed

+1345
-0
lines changed

drivers/soc/apple/Kconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ config APPLE_PMGR_PWRSTATE
1717
controls for SoC devices. This driver manages them through the
1818
generic power domain framework, and also provides reset support.
1919

20+
config APPLE_RTKIT
21+
tristate "Apple RTKit co-processor IPC protocol"
22+
depends on MAILBOX
23+
depends on ARCH_APPLE || COMPILE_TEST
24+
default ARCH_APPLE
25+
help
26+
Apple SoCs such as the M1 come with various co-processors running
27+
their proprietary RTKit operating system. This option enables support
28+
for the protocol library used to communicate with those. It is used
29+
by various client drivers.
30+
31+
Say 'y' here if you have an Apple SoC.
32+
2033
endmenu
2134

2235
endif

drivers/soc/apple/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# SPDX-License-Identifier: GPL-2.0-only
22
obj-$(CONFIG_APPLE_PMGR_PWRSTATE) += apple-pmgr-pwrstate.o
3+
4+
obj-$(CONFIG_APPLE_RTKIT) += apple-rtkit.o
5+
apple-rtkit-y = rtkit.o rtkit-crashlog.o

drivers/soc/apple/rtkit-crashlog.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// SPDX-License-Identifier: GPL-2.0-only OR MIT
2+
/*
3+
* Apple RTKit IPC library
4+
* Copyright (C) The Asahi Linux Contributors
5+
*/
6+
#include "rtkit-internal.h"
7+
8+
#define FOURCC(a, b, c, d) \
9+
(((u32)(a) << 24) | ((u32)(b) << 16) | ((u32)(c) << 8) | ((u32)(d)))
10+
11+
#define APPLE_RTKIT_CRASHLOG_HEADER FOURCC('C', 'L', 'H', 'E')
12+
#define APPLE_RTKIT_CRASHLOG_STR FOURCC('C', 's', 't', 'r')
13+
#define APPLE_RTKIT_CRASHLOG_VERSION FOURCC('C', 'v', 'e', 'r')
14+
#define APPLE_RTKIT_CRASHLOG_MBOX FOURCC('C', 'm', 'b', 'x')
15+
#define APPLE_RTKIT_CRASHLOG_TIME FOURCC('C', 't', 'i', 'm')
16+
17+
struct apple_rtkit_crashlog_header {
18+
u32 fourcc;
19+
u32 version;
20+
u32 size;
21+
u32 flags;
22+
u8 _unk[16];
23+
};
24+
static_assert(sizeof(struct apple_rtkit_crashlog_header) == 0x20);
25+
26+
struct apple_rtkit_crashlog_mbox_entry {
27+
u64 msg0;
28+
u64 msg1;
29+
u32 timestamp;
30+
u8 _unk[4];
31+
};
32+
static_assert(sizeof(struct apple_rtkit_crashlog_mbox_entry) == 0x18);
33+
34+
static void apple_rtkit_crashlog_dump_str(struct apple_rtkit *rtk, u8 *bfr,
35+
size_t size)
36+
{
37+
u32 idx;
38+
u8 *ptr, *end;
39+
40+
memcpy(&idx, bfr, 4);
41+
42+
ptr = bfr + 4;
43+
end = bfr + size;
44+
while (ptr < end) {
45+
u8 *newline = memchr(ptr, '\n', end - ptr);
46+
47+
if (newline) {
48+
u8 tmp = *newline;
49+
*newline = '\0';
50+
dev_warn(rtk->dev, "RTKit: Message (id=%x): %s\n", idx,
51+
ptr);
52+
*newline = tmp;
53+
ptr = newline + 1;
54+
} else {
55+
dev_warn(rtk->dev, "RTKit: Message (id=%x): %s", idx,
56+
ptr);
57+
break;
58+
}
59+
}
60+
}
61+
62+
static void apple_rtkit_crashlog_dump_version(struct apple_rtkit *rtk, u8 *bfr,
63+
size_t size)
64+
{
65+
dev_warn(rtk->dev, "RTKit: Version: %s", bfr + 16);
66+
}
67+
68+
static void apple_rtkit_crashlog_dump_time(struct apple_rtkit *rtk, u8 *bfr,
69+
size_t size)
70+
{
71+
u64 crash_time;
72+
73+
memcpy(&crash_time, bfr, 8);
74+
dev_warn(rtk->dev, "RTKit: Crash time: %lld", crash_time);
75+
}
76+
77+
static void apple_rtkit_crashlog_dump_mailbox(struct apple_rtkit *rtk, u8 *bfr,
78+
size_t size)
79+
{
80+
u32 type, index, i;
81+
size_t n_messages;
82+
struct apple_rtkit_crashlog_mbox_entry entry;
83+
84+
memcpy(&type, bfr + 16, 4);
85+
memcpy(&index, bfr + 24, 4);
86+
n_messages = (size - 28) / sizeof(entry);
87+
88+
dev_warn(rtk->dev, "RTKit: Mailbox history (type = %d, index = %d)",
89+
type, index);
90+
for (i = 0; i < n_messages; ++i) {
91+
memcpy(&entry, bfr + 28 + i * sizeof(entry), sizeof(entry));
92+
dev_warn(rtk->dev, "RTKit: #%03d@%08x: %016llx %016llx", i,
93+
entry.timestamp, entry.msg0, entry.msg1);
94+
}
95+
}
96+
97+
void apple_rtkit_crashlog_dump(struct apple_rtkit *rtk, u8 *bfr, size_t size)
98+
{
99+
size_t offset;
100+
u32 section_fourcc, section_size;
101+
struct apple_rtkit_crashlog_header header;
102+
103+
memcpy(&header, bfr, sizeof(header));
104+
if (header.fourcc != APPLE_RTKIT_CRASHLOG_HEADER) {
105+
dev_warn(rtk->dev, "RTKit: Expected crashlog header but got %x",
106+
header.fourcc);
107+
return;
108+
}
109+
110+
if (header.size > size) {
111+
dev_warn(rtk->dev, "RTKit: Crashlog size (%x) is too large",
112+
header.size);
113+
return;
114+
}
115+
116+
size = header.size;
117+
offset = sizeof(header);
118+
119+
while (offset < size) {
120+
memcpy(&section_fourcc, bfr + offset, 4);
121+
memcpy(&section_size, bfr + offset + 12, 4);
122+
123+
switch (section_fourcc) {
124+
case APPLE_RTKIT_CRASHLOG_HEADER:
125+
dev_dbg(rtk->dev, "RTKit: End of crashlog reached");
126+
return;
127+
case APPLE_RTKIT_CRASHLOG_STR:
128+
apple_rtkit_crashlog_dump_str(rtk, bfr + offset + 16,
129+
section_size);
130+
break;
131+
case APPLE_RTKIT_CRASHLOG_VERSION:
132+
apple_rtkit_crashlog_dump_version(
133+
rtk, bfr + offset + 16, section_size);
134+
break;
135+
case APPLE_RTKIT_CRASHLOG_MBOX:
136+
apple_rtkit_crashlog_dump_mailbox(
137+
rtk, bfr + offset + 16, section_size);
138+
break;
139+
case APPLE_RTKIT_CRASHLOG_TIME:
140+
apple_rtkit_crashlog_dump_time(rtk, bfr + offset + 16,
141+
section_size);
142+
break;
143+
default:
144+
dev_warn(rtk->dev,
145+
"RTKit: Unknown crashlog section: %x",
146+
section_fourcc);
147+
}
148+
149+
offset += section_size;
150+
}
151+
152+
dev_warn(rtk->dev,
153+
"RTKit: End of crashlog reached but no footer present");
154+
}

drivers/soc/apple/rtkit-internal.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
2+
/*
3+
* Apple RTKit IPC library
4+
* Copyright (C) The Asahi Linux Contributors
5+
*/
6+
7+
#ifndef _APPLE_RTKIT_INTERAL_H
8+
#define _APPLE_RTKIT_INTERAL_H
9+
10+
#include <linux/apple-mailbox.h>
11+
#include <linux/bitfield.h>
12+
#include <linux/bitmap.h>
13+
#include <linux/completion.h>
14+
#include <linux/dma-mapping.h>
15+
#include <linux/io.h>
16+
#include <linux/kernel.h>
17+
#include <linux/mailbox_client.h>
18+
#include <linux/module.h>
19+
#include <linux/slab.h>
20+
#include <linux/soc/apple/rtkit.h>
21+
#include <linux/workqueue.h>
22+
23+
#define APPLE_RTKIT_APP_ENDPOINT_START 0x20
24+
#define APPLE_RTKIT_MAX_ENDPOINTS 0x100
25+
26+
struct apple_rtkit {
27+
void *cookie;
28+
const struct apple_rtkit_ops *ops;
29+
struct device *dev;
30+
31+
const char *mbox_name;
32+
int mbox_idx;
33+
struct mbox_client mbox_cl;
34+
struct mbox_chan *mbox_chan;
35+
36+
struct completion epmap_completion;
37+
struct completion iop_pwr_ack_completion;
38+
struct completion ap_pwr_ack_completion;
39+
40+
int boot_result;
41+
int version;
42+
43+
unsigned int iop_power_state;
44+
unsigned int ap_power_state;
45+
bool crashed;
46+
47+
DECLARE_BITMAP(endpoints, APPLE_RTKIT_MAX_ENDPOINTS);
48+
49+
struct apple_rtkit_shmem ioreport_buffer;
50+
struct apple_rtkit_shmem crashlog_buffer;
51+
52+
struct apple_rtkit_shmem syslog_buffer;
53+
char *syslog_msg_buffer;
54+
size_t syslog_n_entries;
55+
size_t syslog_msg_size;
56+
57+
struct workqueue_struct *wq;
58+
};
59+
60+
void apple_rtkit_crashlog_dump(struct apple_rtkit *rtk, u8 *bfr, size_t size);
61+
62+
#endif

0 commit comments

Comments
 (0)