1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
3 * Apple RTKit IPC library
4 * Copyright (C) The Asahi Linux Contributors
7 #include "rtkit-internal.h"
10 APPLE_RTKIT_PWR_STATE_OFF = 0x00, /* power off, cannot be restarted */
11 APPLE_RTKIT_PWR_STATE_SLEEP = 0x01, /* sleeping, can be restarted */
12 APPLE_RTKIT_PWR_STATE_IDLE = 0x201, /* sleeping, retain state */
13 APPLE_RTKIT_PWR_STATE_QUIESCED = 0x10, /* running but no communication */
14 APPLE_RTKIT_PWR_STATE_ON = 0x20, /* normal operating state */
18 APPLE_RTKIT_EP_MGMT = 0,
19 APPLE_RTKIT_EP_CRASHLOG = 1,
20 APPLE_RTKIT_EP_SYSLOG = 2,
21 APPLE_RTKIT_EP_DEBUG = 3,
22 APPLE_RTKIT_EP_IOREPORT = 4,
23 APPLE_RTKIT_EP_OSLOG = 8,
26 #define APPLE_RTKIT_MGMT_TYPE GENMASK_ULL(59, 52)
29 APPLE_RTKIT_MGMT_HELLO = 1,
30 APPLE_RTKIT_MGMT_HELLO_REPLY = 2,
31 APPLE_RTKIT_MGMT_STARTEP = 5,
32 APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE = 6,
33 APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK = 7,
34 APPLE_RTKIT_MGMT_EPMAP = 8,
35 APPLE_RTKIT_MGMT_EPMAP_REPLY = 8,
36 APPLE_RTKIT_MGMT_SET_AP_PWR_STATE = 0xb,
37 APPLE_RTKIT_MGMT_SET_AP_PWR_STATE_ACK = 0xb,
40 #define APPLE_RTKIT_MGMT_HELLO_MINVER GENMASK_ULL(15, 0)
41 #define APPLE_RTKIT_MGMT_HELLO_MAXVER GENMASK_ULL(31, 16)
43 #define APPLE_RTKIT_MGMT_EPMAP_LAST BIT_ULL(51)
44 #define APPLE_RTKIT_MGMT_EPMAP_BASE GENMASK_ULL(34, 32)
45 #define APPLE_RTKIT_MGMT_EPMAP_BITMAP GENMASK_ULL(31, 0)
47 #define APPLE_RTKIT_MGMT_EPMAP_REPLY_MORE BIT_ULL(0)
49 #define APPLE_RTKIT_MGMT_STARTEP_EP GENMASK_ULL(39, 32)
50 #define APPLE_RTKIT_MGMT_STARTEP_FLAG BIT_ULL(1)
52 #define APPLE_RTKIT_MGMT_PWR_STATE GENMASK_ULL(15, 0)
54 #define APPLE_RTKIT_CRASHLOG_CRASH 1
56 #define APPLE_RTKIT_BUFFER_REQUEST 1
57 #define APPLE_RTKIT_BUFFER_REQUEST_SIZE GENMASK_ULL(51, 44)
58 #define APPLE_RTKIT_BUFFER_REQUEST_IOVA GENMASK_ULL(43, 0)
60 #define APPLE_RTKIT_SYSLOG_TYPE GENMASK_ULL(59, 52)
62 #define APPLE_RTKIT_SYSLOG_LOG 5
64 #define APPLE_RTKIT_SYSLOG_INIT 8
65 #define APPLE_RTKIT_SYSLOG_N_ENTRIES GENMASK_ULL(7, 0)
66 #define APPLE_RTKIT_SYSLOG_MSG_SIZE GENMASK_ULL(31, 24)
68 #define APPLE_RTKIT_OSLOG_TYPE GENMASK_ULL(63, 56)
69 #define APPLE_RTKIT_OSLOG_INIT 1
70 #define APPLE_RTKIT_OSLOG_ACK 3
72 #define APPLE_RTKIT_MIN_SUPPORTED_VERSION 11
73 #define APPLE_RTKIT_MAX_SUPPORTED_VERSION 12
75 struct apple_rtkit_rx_work {
76 struct apple_rtkit *rtk;
79 struct work_struct work;
82 bool apple_rtkit_is_running(struct apple_rtkit *rtk)
86 if ((rtk->iop_power_state & 0xff) != APPLE_RTKIT_PWR_STATE_ON)
88 if ((rtk->ap_power_state & 0xff) != APPLE_RTKIT_PWR_STATE_ON)
92 EXPORT_SYMBOL_GPL(apple_rtkit_is_running);
94 bool apple_rtkit_is_crashed(struct apple_rtkit *rtk)
98 EXPORT_SYMBOL_GPL(apple_rtkit_is_crashed);
100 static void apple_rtkit_management_send(struct apple_rtkit *rtk, u8 type,
103 msg &= ~APPLE_RTKIT_MGMT_TYPE;
104 msg |= FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, type);
105 apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_MGMT, msg, NULL, false);
108 static void apple_rtkit_management_rx_hello(struct apple_rtkit *rtk, u64 msg)
112 int min_ver = FIELD_GET(APPLE_RTKIT_MGMT_HELLO_MINVER, msg);
113 int max_ver = FIELD_GET(APPLE_RTKIT_MGMT_HELLO_MAXVER, msg);
114 int want_ver = min(APPLE_RTKIT_MAX_SUPPORTED_VERSION, max_ver);
116 dev_dbg(rtk->dev, "RTKit: Min ver %d, max ver %d\n", min_ver, max_ver);
118 if (min_ver > APPLE_RTKIT_MAX_SUPPORTED_VERSION) {
119 dev_err(rtk->dev, "RTKit: Firmware min version %d is too new\n",
124 if (max_ver < APPLE_RTKIT_MIN_SUPPORTED_VERSION) {
125 dev_err(rtk->dev, "RTKit: Firmware max version %d is too old\n",
130 dev_info(rtk->dev, "RTKit: Initializing (protocol version %d)\n",
132 rtk->version = want_ver;
134 reply = FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MINVER, want_ver);
135 reply |= FIELD_PREP(APPLE_RTKIT_MGMT_HELLO_MAXVER, want_ver);
136 apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_HELLO_REPLY, reply);
141 rtk->boot_result = -EINVAL;
142 complete_all(&rtk->epmap_completion);
145 static void apple_rtkit_management_rx_epmap(struct apple_rtkit *rtk, u64 msg)
149 unsigned long bitmap = FIELD_GET(APPLE_RTKIT_MGMT_EPMAP_BITMAP, msg);
150 u32 base = FIELD_GET(APPLE_RTKIT_MGMT_EPMAP_BASE, msg);
153 "RTKit: received endpoint bitmap 0x%lx with base 0x%x\n",
156 for_each_set_bit(i, &bitmap, 32) {
158 dev_dbg(rtk->dev, "RTKit: Discovered endpoint 0x%02x\n", ep);
159 set_bit(ep, rtk->endpoints);
162 reply = FIELD_PREP(APPLE_RTKIT_MGMT_EPMAP_BASE, base);
163 if (msg & APPLE_RTKIT_MGMT_EPMAP_LAST)
164 reply |= APPLE_RTKIT_MGMT_EPMAP_LAST;
166 reply |= APPLE_RTKIT_MGMT_EPMAP_REPLY_MORE;
168 apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_EPMAP_REPLY, reply);
170 if (!(msg & APPLE_RTKIT_MGMT_EPMAP_LAST))
173 for_each_set_bit(ep, rtk->endpoints, APPLE_RTKIT_APP_ENDPOINT_START) {
175 /* the management endpoint is started by default */
176 case APPLE_RTKIT_EP_MGMT:
179 /* without starting these RTKit refuses to boot */
180 case APPLE_RTKIT_EP_SYSLOG:
181 case APPLE_RTKIT_EP_CRASHLOG:
182 case APPLE_RTKIT_EP_DEBUG:
183 case APPLE_RTKIT_EP_IOREPORT:
184 case APPLE_RTKIT_EP_OSLOG:
186 "RTKit: Starting system endpoint 0x%02x\n", ep);
187 apple_rtkit_start_ep(rtk, ep);
192 "RTKit: Unknown system endpoint: 0x%02x\n",
197 rtk->boot_result = 0;
198 complete_all(&rtk->epmap_completion);
201 static void apple_rtkit_management_rx_iop_pwr_ack(struct apple_rtkit *rtk,
204 unsigned int new_state = FIELD_GET(APPLE_RTKIT_MGMT_PWR_STATE, msg);
206 dev_dbg(rtk->dev, "RTKit: IOP power state transition: 0x%x -> 0x%x\n",
207 rtk->iop_power_state, new_state);
208 rtk->iop_power_state = new_state;
210 complete_all(&rtk->iop_pwr_ack_completion);
213 static void apple_rtkit_management_rx_ap_pwr_ack(struct apple_rtkit *rtk,
216 unsigned int new_state = FIELD_GET(APPLE_RTKIT_MGMT_PWR_STATE, msg);
218 dev_dbg(rtk->dev, "RTKit: AP power state transition: 0x%x -> 0x%x\n",
219 rtk->ap_power_state, new_state);
220 rtk->ap_power_state = new_state;
222 complete_all(&rtk->ap_pwr_ack_completion);
225 static void apple_rtkit_management_rx(struct apple_rtkit *rtk, u64 msg)
227 u8 type = FIELD_GET(APPLE_RTKIT_MGMT_TYPE, msg);
230 case APPLE_RTKIT_MGMT_HELLO:
231 apple_rtkit_management_rx_hello(rtk, msg);
233 case APPLE_RTKIT_MGMT_EPMAP:
234 apple_rtkit_management_rx_epmap(rtk, msg);
236 case APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE_ACK:
237 apple_rtkit_management_rx_iop_pwr_ack(rtk, msg);
239 case APPLE_RTKIT_MGMT_SET_AP_PWR_STATE_ACK:
240 apple_rtkit_management_rx_ap_pwr_ack(rtk, msg);
245 "RTKit: unknown management message: 0x%llx (type: 0x%02x)\n",
250 static int apple_rtkit_common_rx_get_buffer(struct apple_rtkit *rtk,
251 struct apple_rtkit_shmem *buffer,
254 size_t n_4kpages = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_SIZE, msg);
258 buffer->buffer = NULL;
259 buffer->iomem = NULL;
260 buffer->is_mapped = false;
261 buffer->iova = FIELD_GET(APPLE_RTKIT_BUFFER_REQUEST_IOVA, msg);
262 buffer->size = n_4kpages << 12;
264 dev_dbg(rtk->dev, "RTKit: buffer request for 0x%zx bytes at %pad\n",
265 buffer->size, &buffer->iova);
268 (!rtk->ops->shmem_setup || !rtk->ops->shmem_destroy)) {
273 if (rtk->ops->shmem_setup) {
274 err = rtk->ops->shmem_setup(rtk->cookie, buffer);
278 buffer->buffer = dma_alloc_coherent(rtk->dev, buffer->size,
279 &buffer->iova, GFP_KERNEL);
280 if (!buffer->buffer) {
286 if (!buffer->is_mapped) {
287 reply = FIELD_PREP(APPLE_RTKIT_SYSLOG_TYPE,
288 APPLE_RTKIT_BUFFER_REQUEST);
289 reply |= FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_SIZE, n_4kpages);
290 reply |= FIELD_PREP(APPLE_RTKIT_BUFFER_REQUEST_IOVA,
292 apple_rtkit_send_message(rtk, ep, reply, NULL, false);
298 buffer->buffer = NULL;
299 buffer->iomem = NULL;
302 buffer->is_mapped = false;
306 static void apple_rtkit_free_buffer(struct apple_rtkit *rtk,
307 struct apple_rtkit_shmem *bfr)
312 if (rtk->ops->shmem_destroy)
313 rtk->ops->shmem_destroy(rtk->cookie, bfr);
314 else if (bfr->buffer)
315 dma_free_coherent(rtk->dev, bfr->size, bfr->buffer, bfr->iova);
321 bfr->is_mapped = false;
324 static void apple_rtkit_memcpy(struct apple_rtkit *rtk, void *dst,
325 struct apple_rtkit_shmem *bfr, size_t offset,
329 memcpy_fromio(dst, bfr->iomem + offset, len);
331 memcpy(dst, bfr->buffer + offset, len);
334 static void apple_rtkit_crashlog_rx(struct apple_rtkit *rtk, u64 msg)
336 u8 type = FIELD_GET(APPLE_RTKIT_SYSLOG_TYPE, msg);
339 if (type != APPLE_RTKIT_CRASHLOG_CRASH) {
340 dev_warn(rtk->dev, "RTKit: Unknown crashlog message: %llx\n",
345 if (!rtk->crashlog_buffer.size) {
346 apple_rtkit_common_rx_get_buffer(rtk, &rtk->crashlog_buffer,
347 APPLE_RTKIT_EP_CRASHLOG, msg);
351 dev_err(rtk->dev, "RTKit: co-processor has crashed\n");
354 * create a shadow copy here to make sure the co-processor isn't able
355 * to change the log while we're dumping it. this also ensures
356 * the buffer is in normal memory and not iomem for e.g. the SMC
358 bfr = kzalloc(rtk->crashlog_buffer.size, GFP_KERNEL);
360 apple_rtkit_memcpy(rtk, bfr, &rtk->crashlog_buffer, 0,
361 rtk->crashlog_buffer.size);
362 apple_rtkit_crashlog_dump(rtk, bfr, rtk->crashlog_buffer.size);
366 "RTKit: Couldn't allocate crashlog shadow buffer\n");
370 if (rtk->ops->crashed)
371 rtk->ops->crashed(rtk->cookie);
374 static void apple_rtkit_ioreport_rx(struct apple_rtkit *rtk, u64 msg)
376 u8 type = FIELD_GET(APPLE_RTKIT_SYSLOG_TYPE, msg);
379 case APPLE_RTKIT_BUFFER_REQUEST:
380 apple_rtkit_common_rx_get_buffer(rtk, &rtk->ioreport_buffer,
381 APPLE_RTKIT_EP_IOREPORT, msg);
383 /* unknown, must be ACKed or the co-processor will hang */
386 apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_IOREPORT, msg,
390 dev_warn(rtk->dev, "RTKit: Unknown ioreport message: %llx\n",
395 static void apple_rtkit_syslog_rx_init(struct apple_rtkit *rtk, u64 msg)
397 rtk->syslog_n_entries = FIELD_GET(APPLE_RTKIT_SYSLOG_N_ENTRIES, msg);
398 rtk->syslog_msg_size = FIELD_GET(APPLE_RTKIT_SYSLOG_MSG_SIZE, msg);
400 rtk->syslog_msg_buffer = kzalloc(rtk->syslog_msg_size, GFP_KERNEL);
403 "RTKit: syslog initialized: entries: %zd, msg_size: %zd\n",
404 rtk->syslog_n_entries, rtk->syslog_msg_size);
407 static bool should_crop_syslog_char(char c)
409 return c == '\n' || c == '\r' || c == ' ' || c == '\0';
412 static void apple_rtkit_syslog_rx_log(struct apple_rtkit *rtk, u64 msg)
415 char log_context[24];
416 size_t entry_size = 0x20 + rtk->syslog_msg_size;
419 if (!rtk->syslog_msg_buffer) {
422 "RTKit: received syslog message but no syslog_msg_buffer\n");
425 if (!rtk->syslog_buffer.size) {
428 "RTKit: received syslog message but syslog_buffer.size is zero\n");
431 if (!rtk->syslog_buffer.buffer && !rtk->syslog_buffer.iomem) {
434 "RTKit: received syslog message but no syslog_buffer.buffer or syslog_buffer.iomem\n");
437 if (idx > rtk->syslog_n_entries) {
438 dev_warn(rtk->dev, "RTKit: syslog index %d out of range\n",
443 apple_rtkit_memcpy(rtk, log_context, &rtk->syslog_buffer,
444 idx * entry_size + 8, sizeof(log_context));
445 apple_rtkit_memcpy(rtk, rtk->syslog_msg_buffer, &rtk->syslog_buffer,
446 idx * entry_size + 8 + sizeof(log_context),
447 rtk->syslog_msg_size);
449 log_context[sizeof(log_context) - 1] = 0;
451 msglen = rtk->syslog_msg_size - 1;
453 should_crop_syslog_char(rtk->syslog_msg_buffer[msglen - 1]))
456 rtk->syslog_msg_buffer[msglen] = 0;
457 dev_info(rtk->dev, "RTKit: syslog message: %s: %s\n", log_context,
458 rtk->syslog_msg_buffer);
461 apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_SYSLOG, msg, NULL, false);
464 static void apple_rtkit_syslog_rx(struct apple_rtkit *rtk, u64 msg)
466 u8 type = FIELD_GET(APPLE_RTKIT_SYSLOG_TYPE, msg);
469 case APPLE_RTKIT_BUFFER_REQUEST:
470 apple_rtkit_common_rx_get_buffer(rtk, &rtk->syslog_buffer,
471 APPLE_RTKIT_EP_SYSLOG, msg);
473 case APPLE_RTKIT_SYSLOG_INIT:
474 apple_rtkit_syslog_rx_init(rtk, msg);
476 case APPLE_RTKIT_SYSLOG_LOG:
477 apple_rtkit_syslog_rx_log(rtk, msg);
480 dev_warn(rtk->dev, "RTKit: Unknown syslog message: %llx\n",
485 static void apple_rtkit_oslog_rx_init(struct apple_rtkit *rtk, u64 msg)
489 dev_dbg(rtk->dev, "RTKit: oslog init: msg: 0x%llx\n", msg);
490 ack = FIELD_PREP(APPLE_RTKIT_OSLOG_TYPE, APPLE_RTKIT_OSLOG_ACK);
491 apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_OSLOG, ack, NULL, false);
494 static void apple_rtkit_oslog_rx(struct apple_rtkit *rtk, u64 msg)
496 u8 type = FIELD_GET(APPLE_RTKIT_OSLOG_TYPE, msg);
499 case APPLE_RTKIT_OSLOG_INIT:
500 apple_rtkit_oslog_rx_init(rtk, msg);
503 dev_warn(rtk->dev, "RTKit: Unknown oslog message: %llx\n", msg);
507 static void apple_rtkit_rx_work(struct work_struct *work)
509 struct apple_rtkit_rx_work *rtk_work =
510 container_of(work, struct apple_rtkit_rx_work, work);
511 struct apple_rtkit *rtk = rtk_work->rtk;
513 switch (rtk_work->ep) {
514 case APPLE_RTKIT_EP_MGMT:
515 apple_rtkit_management_rx(rtk, rtk_work->msg);
517 case APPLE_RTKIT_EP_CRASHLOG:
518 apple_rtkit_crashlog_rx(rtk, rtk_work->msg);
520 case APPLE_RTKIT_EP_SYSLOG:
521 apple_rtkit_syslog_rx(rtk, rtk_work->msg);
523 case APPLE_RTKIT_EP_IOREPORT:
524 apple_rtkit_ioreport_rx(rtk, rtk_work->msg);
526 case APPLE_RTKIT_EP_OSLOG:
527 apple_rtkit_oslog_rx(rtk, rtk_work->msg);
529 case APPLE_RTKIT_APP_ENDPOINT_START ... 0xff:
530 if (rtk->ops->recv_message)
531 rtk->ops->recv_message(rtk->cookie, rtk_work->ep,
536 "Received unexpected message to EP%02d: %llx\n",
537 rtk_work->ep, rtk_work->msg);
541 "RTKit: message to unknown endpoint %02x: %llx\n",
542 rtk_work->ep, rtk_work->msg);
548 static void apple_rtkit_rx(struct apple_mbox *mbox, struct apple_mbox_msg msg,
551 struct apple_rtkit *rtk = cookie;
552 struct apple_rtkit_rx_work *work;
556 * The message was read from a MMIO FIFO and we have to make
557 * sure all reads from buffers sent with that message happen
562 if (!test_bit(ep, rtk->endpoints))
564 "RTKit: Message to undiscovered endpoint 0x%02x\n",
567 if (ep >= APPLE_RTKIT_APP_ENDPOINT_START &&
568 rtk->ops->recv_message_early &&
569 rtk->ops->recv_message_early(rtk->cookie, ep, msg.msg0))
572 work = kzalloc(sizeof(*work), GFP_ATOMIC);
578 work->msg = msg.msg0;
579 INIT_WORK(&work->work, apple_rtkit_rx_work);
580 queue_work(rtk->wq, &work->work);
583 int apple_rtkit_send_message(struct apple_rtkit *rtk, u8 ep, u64 message,
584 struct completion *completion, bool atomic)
586 struct apple_mbox_msg msg = {
593 if (ep >= APPLE_RTKIT_APP_ENDPOINT_START &&
594 !apple_rtkit_is_running(rtk))
598 * The message will be sent with a MMIO write. We need the barrier
599 * here to ensure any previous writes to buffers are visible to the
600 * device before that MMIO write happens.
604 return apple_mbox_send(rtk->mbox, msg, atomic);
606 EXPORT_SYMBOL_GPL(apple_rtkit_send_message);
608 int apple_rtkit_poll(struct apple_rtkit *rtk)
610 return apple_mbox_poll(rtk->mbox);
612 EXPORT_SYMBOL_GPL(apple_rtkit_poll);
614 int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint)
618 if (!test_bit(endpoint, rtk->endpoints))
620 if (endpoint >= APPLE_RTKIT_APP_ENDPOINT_START &&
621 !apple_rtkit_is_running(rtk))
624 msg = FIELD_PREP(APPLE_RTKIT_MGMT_STARTEP_EP, endpoint);
625 msg |= APPLE_RTKIT_MGMT_STARTEP_FLAG;
626 apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_STARTEP, msg);
630 EXPORT_SYMBOL_GPL(apple_rtkit_start_ep);
632 struct apple_rtkit *apple_rtkit_init(struct device *dev, void *cookie,
633 const char *mbox_name, int mbox_idx,
634 const struct apple_rtkit_ops *ops)
636 struct apple_rtkit *rtk;
640 return ERR_PTR(-EINVAL);
642 rtk = kzalloc(sizeof(*rtk), GFP_KERNEL);
644 return ERR_PTR(-ENOMEM);
647 rtk->cookie = cookie;
650 init_completion(&rtk->epmap_completion);
651 init_completion(&rtk->iop_pwr_ack_completion);
652 init_completion(&rtk->ap_pwr_ack_completion);
654 bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS);
655 set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints);
658 rtk->mbox = apple_mbox_get_byname(dev, mbox_name);
660 rtk->mbox = apple_mbox_get(dev, mbox_idx);
662 if (IS_ERR(rtk->mbox)) {
663 ret = PTR_ERR(rtk->mbox);
667 rtk->mbox->rx = apple_rtkit_rx;
668 rtk->mbox->cookie = rtk;
670 rtk->wq = alloc_ordered_workqueue("rtkit-%s", WQ_MEM_RECLAIM,
677 ret = apple_mbox_start(rtk->mbox);
684 destroy_workqueue(rtk->wq);
689 EXPORT_SYMBOL_GPL(apple_rtkit_init);
691 static int apple_rtkit_wait_for_completion(struct completion *c)
695 t = wait_for_completion_interruptible_timeout(c,
696 msecs_to_jiffies(1000));
705 int apple_rtkit_reinit(struct apple_rtkit *rtk)
707 /* make sure we don't handle any messages while reinitializing */
708 apple_mbox_stop(rtk->mbox);
709 flush_workqueue(rtk->wq);
711 apple_rtkit_free_buffer(rtk, &rtk->ioreport_buffer);
712 apple_rtkit_free_buffer(rtk, &rtk->crashlog_buffer);
713 apple_rtkit_free_buffer(rtk, &rtk->syslog_buffer);
715 kfree(rtk->syslog_msg_buffer);
717 rtk->syslog_msg_buffer = NULL;
718 rtk->syslog_n_entries = 0;
719 rtk->syslog_msg_size = 0;
721 bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS);
722 set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints);
724 reinit_completion(&rtk->epmap_completion);
725 reinit_completion(&rtk->iop_pwr_ack_completion);
726 reinit_completion(&rtk->ap_pwr_ack_completion);
728 rtk->crashed = false;
729 rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_OFF;
730 rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_OFF;
732 return apple_mbox_start(rtk->mbox);
734 EXPORT_SYMBOL_GPL(apple_rtkit_reinit);
736 static int apple_rtkit_set_ap_power_state(struct apple_rtkit *rtk,
742 reinit_completion(&rtk->ap_pwr_ack_completion);
744 msg = FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, state);
745 apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_SET_AP_PWR_STATE,
748 ret = apple_rtkit_wait_for_completion(&rtk->ap_pwr_ack_completion);
752 if (rtk->ap_power_state != state)
757 static int apple_rtkit_set_iop_power_state(struct apple_rtkit *rtk,
763 reinit_completion(&rtk->iop_pwr_ack_completion);
765 msg = FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, state);
766 apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE,
769 ret = apple_rtkit_wait_for_completion(&rtk->iop_pwr_ack_completion);
773 if (rtk->iop_power_state != state)
778 int apple_rtkit_boot(struct apple_rtkit *rtk)
782 if (apple_rtkit_is_running(rtk))
787 dev_dbg(rtk->dev, "RTKit: waiting for boot to finish\n");
788 ret = apple_rtkit_wait_for_completion(&rtk->epmap_completion);
791 if (rtk->boot_result)
792 return rtk->boot_result;
794 dev_dbg(rtk->dev, "RTKit: waiting for IOP power state ACK\n");
795 ret = apple_rtkit_wait_for_completion(&rtk->iop_pwr_ack_completion);
799 return apple_rtkit_set_ap_power_state(rtk, APPLE_RTKIT_PWR_STATE_ON);
801 EXPORT_SYMBOL_GPL(apple_rtkit_boot);
803 int apple_rtkit_shutdown(struct apple_rtkit *rtk)
807 /* if OFF is used here the co-processor will not wake up again */
808 ret = apple_rtkit_set_ap_power_state(rtk,
809 APPLE_RTKIT_PWR_STATE_QUIESCED);
813 ret = apple_rtkit_set_iop_power_state(rtk, APPLE_RTKIT_PWR_STATE_SLEEP);
817 return apple_rtkit_reinit(rtk);
819 EXPORT_SYMBOL_GPL(apple_rtkit_shutdown);
821 int apple_rtkit_idle(struct apple_rtkit *rtk)
825 /* if OFF is used here the co-processor will not wake up again */
826 ret = apple_rtkit_set_ap_power_state(rtk,
827 APPLE_RTKIT_PWR_STATE_IDLE);
831 ret = apple_rtkit_set_iop_power_state(rtk, APPLE_RTKIT_PWR_STATE_IDLE);
835 rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_IDLE;
836 rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_IDLE;
839 EXPORT_SYMBOL_GPL(apple_rtkit_idle);
841 int apple_rtkit_quiesce(struct apple_rtkit *rtk)
845 ret = apple_rtkit_set_ap_power_state(rtk,
846 APPLE_RTKIT_PWR_STATE_QUIESCED);
850 ret = apple_rtkit_set_iop_power_state(rtk,
851 APPLE_RTKIT_PWR_STATE_QUIESCED);
855 ret = apple_rtkit_reinit(rtk);
859 rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_QUIESCED;
860 rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_QUIESCED;
863 EXPORT_SYMBOL_GPL(apple_rtkit_quiesce);
865 int apple_rtkit_wake(struct apple_rtkit *rtk)
869 if (apple_rtkit_is_running(rtk))
872 reinit_completion(&rtk->iop_pwr_ack_completion);
875 * Use open-coded apple_rtkit_set_iop_power_state since apple_rtkit_boot
876 * will wait for the completion anyway.
878 msg = FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, APPLE_RTKIT_PWR_STATE_ON);
879 apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE,
882 return apple_rtkit_boot(rtk);
884 EXPORT_SYMBOL_GPL(apple_rtkit_wake);
886 void apple_rtkit_free(struct apple_rtkit *rtk)
888 apple_mbox_stop(rtk->mbox);
889 destroy_workqueue(rtk->wq);
891 apple_rtkit_free_buffer(rtk, &rtk->ioreport_buffer);
892 apple_rtkit_free_buffer(rtk, &rtk->crashlog_buffer);
893 apple_rtkit_free_buffer(rtk, &rtk->syslog_buffer);
895 kfree(rtk->syslog_msg_buffer);
898 EXPORT_SYMBOL_GPL(apple_rtkit_free);
900 static void apple_rtkit_free_wrapper(void *data)
902 apple_rtkit_free(data);
905 struct apple_rtkit *devm_apple_rtkit_init(struct device *dev, void *cookie,
906 const char *mbox_name, int mbox_idx,
907 const struct apple_rtkit_ops *ops)
909 struct apple_rtkit *rtk;
912 rtk = apple_rtkit_init(dev, cookie, mbox_name, mbox_idx, ops);
916 ret = devm_add_action_or_reset(dev, apple_rtkit_free_wrapper, rtk);
922 EXPORT_SYMBOL_GPL(devm_apple_rtkit_init);
924 MODULE_LICENSE("Dual MIT/GPL");
926 MODULE_DESCRIPTION("Apple RTKit driver");