Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2021 Mark Kettenis <kettenis@openbsd.org> |
| 4 | * (C) Copyright 2021 Copyright The Asahi Linux Contributors |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <mailbox.h> |
| 9 | #include <malloc.h> |
| 10 | |
| 11 | #include <asm/arch/rtkit.h> |
| 12 | #include <linux/apple-mailbox.h> |
| 13 | #include <linux/bitfield.h> |
| 14 | |
| 15 | #define APPLE_RTKIT_EP_MGMT 0 |
| 16 | #define APPLE_RTKIT_EP_CRASHLOG 1 |
| 17 | #define APPLE_RTKIT_EP_SYSLOG 2 |
| 18 | #define APPLE_RTKIT_EP_DEBUG 3 |
| 19 | #define APPLE_RTKIT_EP_IOREPORT 4 |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 20 | #define APPLE_RTKIT_EP_TRACEKIT 10 |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 21 | |
| 22 | /* Messages for management endpoint. */ |
| 23 | #define APPLE_RTKIT_MGMT_TYPE GENMASK(59, 52) |
| 24 | |
| 25 | #define APPLE_RTKIT_MGMT_PWR_STATE GENMASK(15, 0) |
| 26 | |
| 27 | #define APPLE_RTKIT_MGMT_HELLO 1 |
| 28 | #define APPLE_RTKIT_MGMT_HELLO_REPLY 2 |
| 29 | #define APPLE_RTKIT_MGMT_HELLO_MINVER GENMASK(15, 0) |
| 30 | #define APPLE_RTKIT_MGMT_HELLO_MAXVER GENMASK(31, 16) |
| 31 | |
| 32 | #define APPLE_RTKIT_MGMT_STARTEP 5 |
| 33 | #define APPLE_RTKIT_MGMT_STARTEP_EP GENMASK(39, 32) |
| 34 | #define APPLE_RTKIT_MGMT_STARTEP_FLAG BIT(1) |
| 35 | |
| 36 | #define APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE 6 |
| 37 | #define APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK 7 |
| 38 | |
| 39 | #define APPLE_RTKIT_MGMT_EPMAP 8 |
| 40 | #define APPLE_RTKIT_MGMT_EPMAP_LAST BIT(51) |
| 41 | #define APPLE_RTKIT_MGMT_EPMAP_BASE GENMASK(34, 32) |
| 42 | #define APPLE_RTKIT_MGMT_EPMAP_BITMAP GENMASK(31, 0) |
| 43 | |
| 44 | #define APPLE_RTKIT_MGMT_EPMAP_REPLY 8 |
| 45 | #define APPLE_RTKIT_MGMT_EPMAP_REPLY_MORE BIT(0) |
| 46 | |
| 47 | #define APPLE_RTKIT_MIN_SUPPORTED_VERSION 11 |
| 48 | #define APPLE_RTKIT_MAX_SUPPORTED_VERSION 12 |
| 49 | |
| 50 | /* Messages for internal endpoints. */ |
| 51 | #define APPLE_RTKIT_BUFFER_REQUEST 1 |
| 52 | #define APPLE_RTKIT_BUFFER_REQUEST_SIZE GENMASK(51, 44) |
| 53 | #define APPLE_RTKIT_BUFFER_REQUEST_IOVA GENMASK(41, 0) |
| 54 | |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 55 | struct apple_rtkit { |
| 56 | struct mbox_chan *chan; |
| 57 | void *cookie; |
| 58 | apple_rtkit_shmem_setup shmem_setup; |
| 59 | apple_rtkit_shmem_destroy shmem_destroy; |
| 60 | |
| 61 | struct apple_rtkit_buffer syslog_buffer; |
| 62 | struct apple_rtkit_buffer crashlog_buffer; |
| 63 | struct apple_rtkit_buffer ioreport_buffer; |
| 64 | }; |
| 65 | |
| 66 | struct apple_rtkit *apple_rtkit_init(struct mbox_chan *chan, void *cookie, |
| 67 | apple_rtkit_shmem_setup shmem_setup, |
| 68 | apple_rtkit_shmem_destroy shmem_destroy) |
| 69 | { |
| 70 | struct apple_rtkit *rtk; |
| 71 | |
| 72 | rtk = calloc(sizeof(*rtk), 1); |
| 73 | if (!rtk) |
| 74 | return NULL; |
| 75 | |
| 76 | rtk->chan = chan; |
| 77 | rtk->cookie = cookie; |
| 78 | rtk->shmem_setup = shmem_setup; |
| 79 | rtk->shmem_destroy = shmem_destroy; |
| 80 | |
| 81 | return rtk; |
| 82 | } |
| 83 | |
| 84 | void apple_rtkit_free(struct apple_rtkit *rtk) |
| 85 | { |
| 86 | if (rtk->shmem_destroy) { |
| 87 | if (rtk->syslog_buffer.buffer) |
| 88 | rtk->shmem_destroy(rtk->cookie, &rtk->syslog_buffer); |
| 89 | if (rtk->crashlog_buffer.buffer) |
| 90 | rtk->shmem_destroy(rtk->cookie, &rtk->crashlog_buffer); |
| 91 | if (rtk->ioreport_buffer.buffer) |
| 92 | rtk->shmem_destroy(rtk->cookie, &rtk->ioreport_buffer); |
| 93 | } |
| 94 | free(rtk); |
| 95 | } |
| 96 | |
| 97 | static int rtkit_handle_buf_req(struct apple_rtkit *rtk, int endpoint, struct apple_mbox_msg *msg) |
| 98 | { |
| 99 | struct apple_rtkit_buffer *buf; |
| 100 | size_t num_4kpages; |
| 101 | int ret; |
| 102 | |
| 103 | num_4kpages = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_SIZE, msg->msg0); |
| 104 | |
| 105 | if (num_4kpages == 0) { |
| 106 | printf("%s: unexpected request for buffer without size\n", __func__); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | switch (endpoint) { |
| 111 | case APPLE_RTKIT_EP_CRASHLOG: |
| 112 | buf = &rtk->crashlog_buffer; |
| 113 | break; |
| 114 | case APPLE_RTKIT_EP_SYSLOG: |
| 115 | buf = &rtk->syslog_buffer; |
| 116 | break; |
| 117 | case APPLE_RTKIT_EP_IOREPORT: |
| 118 | buf = &rtk->ioreport_buffer; |
| 119 | break; |
| 120 | default: |
| 121 | printf("%s: unexpected endpoint %d\n", __func__, endpoint); |
| 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | buf->dva = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_IOVA, msg->msg0); |
| 126 | buf->size = num_4kpages << 12; |
| 127 | buf->is_mapped = false; |
| 128 | |
| 129 | if (rtk->shmem_setup) { |
| 130 | ret = rtk->shmem_setup(rtk->cookie, buf); |
| 131 | if (ret < 0) { |
| 132 | printf("%s: shmen_setup failed for endpoint %d\n", __func__, |
| 133 | endpoint); |
| 134 | return ret; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | if (!buf->is_mapped) { |
| 139 | msg->msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_BUFFER_REQUEST) | |
| 140 | FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_SIZE, num_4kpages) | |
| 141 | FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_IOVA, buf->dva); |
| 142 | msg->msg1 = endpoint; |
| 143 | |
| 144 | return mbox_send(rtk->chan, msg); |
| 145 | } |
| 146 | |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | int apple_rtkit_boot(struct apple_rtkit *rtk) |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 151 | { |
| 152 | struct apple_mbox_msg msg; |
| 153 | int endpoints[256]; |
| 154 | int nendpoints = 0; |
| 155 | int endpoint; |
| 156 | int min_ver, max_ver, want_ver; |
| 157 | int msgtype, pwrstate; |
| 158 | u64 reply; |
| 159 | u32 bitmap, base; |
| 160 | int i, ret; |
| 161 | |
| 162 | /* Wakup the IOP. */ |
| 163 | msg.msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE) | |
| 164 | FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, APPLE_RTKIT_PWR_STATE_ON); |
| 165 | msg.msg1 = APPLE_RTKIT_EP_MGMT; |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 166 | ret = mbox_send(rtk->chan, &msg); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 167 | if (ret < 0) |
| 168 | return ret; |
| 169 | |
| 170 | /* Wait for protocol version negotiation message. */ |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 171 | ret = mbox_recv(rtk->chan, &msg, 10000); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | |
| 175 | endpoint = msg.msg1; |
| 176 | msgtype = FIELD_GET(APPLE_RTKIT_MGMT_TYPE, msg.msg0); |
| 177 | if (endpoint != APPLE_RTKIT_EP_MGMT) { |
| 178 | printf("%s: unexpected endpoint %d\n", __func__, endpoint); |
| 179 | return -EINVAL; |
| 180 | } |
| 181 | if (msgtype != APPLE_RTKIT_MGMT_HELLO) { |
| 182 | printf("%s: unexpected message type %d\n", __func__, msgtype); |
| 183 | return -EINVAL; |
| 184 | } |
| 185 | |
| 186 | min_ver = FIELD_GET(APPLE_RTKIT_MGMT_HELLO_MINVER, msg.msg0); |
| 187 | max_ver = FIELD_GET(APPLE_RTKIT_MGMT_HELLO_MAXVER, msg.msg0); |
| 188 | want_ver = min(APPLE_RTKIT_MAX_SUPPORTED_VERSION, max_ver); |
| 189 | |
| 190 | if (min_ver > APPLE_RTKIT_MAX_SUPPORTED_VERSION) { |
| 191 | printf("%s: firmware min version %d is too new\n", |
| 192 | __func__, min_ver); |
| 193 | return -ENOTSUPP; |
| 194 | } |
| 195 | |
| 196 | if (max_ver < APPLE_RTKIT_MIN_SUPPORTED_VERSION) { |
| 197 | printf("%s: firmware max version %d is too old\n", |
| 198 | __func__, max_ver); |
| 199 | return -ENOTSUPP; |
| 200 | } |
| 201 | |
| 202 | /* Ack version. */ |
| 203 | msg.msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_HELLO_REPLY) | |
| 204 | FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MINVER, want_ver) | |
| 205 | FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MAXVER, want_ver); |
| 206 | msg.msg1 = APPLE_RTKIT_EP_MGMT; |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 207 | ret = mbox_send(rtk->chan, &msg); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 208 | if (ret < 0) |
| 209 | return ret; |
| 210 | |
| 211 | wait_epmap: |
| 212 | /* Wait for endpoint map message. */ |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 213 | ret = mbox_recv(rtk->chan, &msg, 10000); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 214 | if (ret < 0) |
| 215 | return ret; |
| 216 | |
| 217 | endpoint = msg.msg1; |
| 218 | msgtype = FIELD_GET(APPLE_RTKIT_MGMT_TYPE, msg.msg0); |
| 219 | if (endpoint != APPLE_RTKIT_EP_MGMT) { |
| 220 | printf("%s: unexpected endpoint %d\n", __func__, endpoint); |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | if (msgtype != APPLE_RTKIT_MGMT_EPMAP) { |
| 224 | printf("%s: unexpected message type %d\n", __func__, msgtype); |
| 225 | return -EINVAL; |
| 226 | } |
| 227 | |
| 228 | bitmap = FIELD_GET(APPLE_RTKIT_MGMT_EPMAP_BITMAP, msg.msg0); |
| 229 | base = FIELD_GET(APPLE_RTKIT_MGMT_EPMAP_BASE, msg.msg0); |
| 230 | for (i = 0; i < 32; i++) { |
| 231 | if (bitmap & (1U << i)) |
| 232 | endpoints[nendpoints++] = base * 32 + i; |
| 233 | } |
| 234 | |
| 235 | /* Ack endpoint map. */ |
| 236 | reply = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_EPMAP_REPLY) | |
| 237 | FIELD_PREP(APPLE_RTKIT_MGMT_EPMAP_BASE, base); |
| 238 | if (msg.msg0 & APPLE_RTKIT_MGMT_EPMAP_LAST) |
| 239 | reply |= APPLE_RTKIT_MGMT_EPMAP_LAST; |
| 240 | else |
| 241 | reply |= APPLE_RTKIT_MGMT_EPMAP_REPLY_MORE; |
| 242 | msg.msg0 = reply; |
| 243 | msg.msg1 = APPLE_RTKIT_EP_MGMT; |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 244 | ret = mbox_send(rtk->chan, &msg); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 245 | if (ret < 0) |
| 246 | return ret; |
| 247 | |
| 248 | if (reply & APPLE_RTKIT_MGMT_EPMAP_REPLY_MORE) |
| 249 | goto wait_epmap; |
| 250 | |
| 251 | for (i = 0; i < nendpoints; i++) { |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 252 | /* Start only necessary endpoints. The syslog endpoint is |
| 253 | * particularly noisy and its message can't easily be handled |
| 254 | * within U-Boot. |
| 255 | */ |
| 256 | switch (endpoints[i]) { |
| 257 | case APPLE_RTKIT_EP_MGMT: |
| 258 | case APPLE_RTKIT_EP_SYSLOG: |
| 259 | case APPLE_RTKIT_EP_DEBUG: |
| 260 | case APPLE_RTKIT_EP_TRACEKIT: |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 261 | continue; |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 262 | default: |
| 263 | break; |
| 264 | } |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 265 | |
| 266 | /* Request endpoint. */ |
| 267 | msg.msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_STARTEP) | |
| 268 | FIELD_PREP(APPLE_RTKIT_MGMT_STARTEP_EP, endpoints[i]) | |
| 269 | APPLE_RTKIT_MGMT_STARTEP_FLAG; |
| 270 | msg.msg1 = APPLE_RTKIT_EP_MGMT; |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 271 | ret = mbox_send(rtk->chan, &msg); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 272 | if (ret < 0) |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | pwrstate = APPLE_RTKIT_PWR_STATE_SLEEP; |
| 277 | while (pwrstate != APPLE_RTKIT_PWR_STATE_ON) { |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 278 | ret = mbox_recv(rtk->chan, &msg, 100000); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 279 | if (ret < 0) |
| 280 | return ret; |
| 281 | |
| 282 | endpoint = msg.msg1; |
| 283 | msgtype = FIELD_GET(APPLE_RTKIT_MGMT_TYPE, msg.msg0); |
| 284 | |
| 285 | if (endpoint == APPLE_RTKIT_EP_CRASHLOG || |
| 286 | endpoint == APPLE_RTKIT_EP_SYSLOG || |
| 287 | endpoint == APPLE_RTKIT_EP_IOREPORT) { |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 288 | if (msgtype == APPLE_RTKIT_BUFFER_REQUEST) { |
| 289 | ret = rtkit_handle_buf_req(rtk, endpoint, &msg); |
| 290 | if (ret < 0) |
| 291 | return ret; |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 292 | continue; |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 295 | |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 296 | if (endpoint == APPLE_RTKIT_EP_IOREPORT) { |
| 297 | // these two messages have to be ack-ed for proper startup |
| 298 | if (msgtype == 0xc || msgtype == 0x8) { |
| 299 | ret = mbox_send(rtk->chan, &msg); |
| 300 | if (ret < 0) |
| 301 | return ret; |
| 302 | continue; |
| 303 | } |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | if (endpoint != APPLE_RTKIT_EP_MGMT) { |
| 307 | printf("%s: unexpected endpoint %d\n", __func__, endpoint); |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | if (msgtype != APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK) { |
| 311 | printf("%s: unexpected message type %d\n", __func__, msgtype); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
| 315 | pwrstate = FIELD_GET(APPLE_RTKIT_MGMT_PWR_STATE, msg.msg0); |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 321 | int apple_rtkit_shutdown(struct apple_rtkit *rtk, int pwrstate) |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 322 | { |
| 323 | struct apple_mbox_msg msg; |
| 324 | int ret; |
| 325 | |
| 326 | msg.msg0 = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE) | |
| 327 | FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, pwrstate); |
| 328 | msg.msg1 = APPLE_RTKIT_EP_MGMT; |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 329 | ret = mbox_send(rtk->chan, &msg); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 330 | if (ret < 0) |
| 331 | return ret; |
| 332 | |
Janne Grunau | e3a438f | 2022-06-14 09:09:07 +0200 | [diff] [blame] | 333 | ret = mbox_recv(rtk->chan, &msg, 100000); |
Mark Kettenis | 72d7301 | 2022-01-22 20:38:14 +0100 | [diff] [blame] | 334 | if (ret < 0) |
| 335 | return ret; |
| 336 | |
| 337 | return 0; |
| 338 | } |