rtkit.c (57b79ac9f43dc71fc8b55af51d1c9f469cb7a0de) | rtkit.c (bb538effdc71397f2d08bb98df6b326d3b5b2333) |
---|---|
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#include "rtkit-internal.h" 8 --- 58 unchanged lines hidden (view full) --- 67 68#define APPLE_RTKIT_OSLOG_TYPE GENMASK_ULL(63, 56) 69#define APPLE_RTKIT_OSLOG_INIT 1 70#define APPLE_RTKIT_OSLOG_ACK 3 71 72#define APPLE_RTKIT_MIN_SUPPORTED_VERSION 11 73#define APPLE_RTKIT_MAX_SUPPORTED_VERSION 12 74 | 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#include "rtkit-internal.h" 8 --- 58 unchanged lines hidden (view full) --- 67 68#define APPLE_RTKIT_OSLOG_TYPE GENMASK_ULL(63, 56) 69#define APPLE_RTKIT_OSLOG_INIT 1 70#define APPLE_RTKIT_OSLOG_ACK 3 71 72#define APPLE_RTKIT_MIN_SUPPORTED_VERSION 11 73#define APPLE_RTKIT_MAX_SUPPORTED_VERSION 12 74 |
75struct apple_rtkit_msg { 76 struct completion *completion; 77 struct apple_mbox_msg mbox_msg; 78}; 79 | |
80struct apple_rtkit_rx_work { 81 struct apple_rtkit *rtk; 82 u8 ep; 83 u64 msg; 84 struct work_struct work; 85}; 86 87bool apple_rtkit_is_running(struct apple_rtkit *rtk) --- 457 unchanged lines hidden (view full) --- 545 dev_warn(rtk->dev, 546 "RTKit: message to unknown endpoint %02x: %llx\n", 547 rtk_work->ep, rtk_work->msg); 548 } 549 550 kfree(rtk_work); 551} 552 | 75struct apple_rtkit_rx_work { 76 struct apple_rtkit *rtk; 77 u8 ep; 78 u64 msg; 79 struct work_struct work; 80}; 81 82bool apple_rtkit_is_running(struct apple_rtkit *rtk) --- 457 unchanged lines hidden (view full) --- 540 dev_warn(rtk->dev, 541 "RTKit: message to unknown endpoint %02x: %llx\n", 542 rtk_work->ep, rtk_work->msg); 543 } 544 545 kfree(rtk_work); 546} 547 |
553static void apple_rtkit_rx(struct mbox_client *cl, void *mssg) | 548static void apple_rtkit_rx(struct apple_mbox *mbox, struct apple_mbox_msg msg, 549 void *cookie) |
554{ | 550{ |
555 struct apple_rtkit *rtk = container_of(cl, struct apple_rtkit, mbox_cl); 556 struct apple_mbox_msg *msg = mssg; | 551 struct apple_rtkit *rtk = cookie; |
557 struct apple_rtkit_rx_work *work; | 552 struct apple_rtkit_rx_work *work; |
558 u8 ep = msg->msg1; | 553 u8 ep = msg.msg1; |
559 560 /* 561 * The message was read from a MMIO FIFO and we have to make 562 * sure all reads from buffers sent with that message happen 563 * afterwards. 564 */ 565 dma_rmb(); 566 567 if (!test_bit(ep, rtk->endpoints)) 568 dev_warn(rtk->dev, 569 "RTKit: Message to undiscovered endpoint 0x%02x\n", 570 ep); 571 572 if (ep >= APPLE_RTKIT_APP_ENDPOINT_START && 573 rtk->ops->recv_message_early && | 554 555 /* 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 558 * afterwards. 559 */ 560 dma_rmb(); 561 562 if (!test_bit(ep, rtk->endpoints)) 563 dev_warn(rtk->dev, 564 "RTKit: Message to undiscovered endpoint 0x%02x\n", 565 ep); 566 567 if (ep >= APPLE_RTKIT_APP_ENDPOINT_START && 568 rtk->ops->recv_message_early && |
574 rtk->ops->recv_message_early(rtk->cookie, ep, msg->msg0)) | 569 rtk->ops->recv_message_early(rtk->cookie, ep, msg.msg0)) |
575 return; 576 577 work = kzalloc(sizeof(*work), GFP_ATOMIC); 578 if (!work) 579 return; 580 581 work->rtk = rtk; 582 work->ep = ep; | 570 return; 571 572 work = kzalloc(sizeof(*work), GFP_ATOMIC); 573 if (!work) 574 return; 575 576 work->rtk = rtk; 577 work->ep = ep; |
583 work->msg = msg->msg0; | 578 work->msg = msg.msg0; |
584 INIT_WORK(&work->work, apple_rtkit_rx_work); 585 queue_work(rtk->wq, &work->work); 586} 587 | 579 INIT_WORK(&work->work, apple_rtkit_rx_work); 580 queue_work(rtk->wq, &work->work); 581} 582 |
588static void apple_rtkit_tx_done(struct mbox_client *cl, void *mssg, int r) 589{ 590 struct apple_rtkit_msg *msg = 591 container_of(mssg, struct apple_rtkit_msg, mbox_msg); 592 593 if (r == -ETIME) 594 return; 595 596 if (msg->completion) 597 complete(msg->completion); 598 kfree(msg); 599} 600 | |
601int apple_rtkit_send_message(struct apple_rtkit *rtk, u8 ep, u64 message, 602 struct completion *completion, bool atomic) 603{ | 583int apple_rtkit_send_message(struct apple_rtkit *rtk, u8 ep, u64 message, 584 struct completion *completion, bool atomic) 585{ |
604 struct apple_rtkit_msg *msg; 605 int ret; 606 gfp_t flags; | 586 struct apple_mbox_msg msg = { 587 .msg0 = message, 588 .msg1 = ep, 589 }; |
607 608 if (rtk->crashed) 609 return -EINVAL; 610 if (ep >= APPLE_RTKIT_APP_ENDPOINT_START && 611 !apple_rtkit_is_running(rtk)) 612 return -EINVAL; 613 | 590 591 if (rtk->crashed) 592 return -EINVAL; 593 if (ep >= APPLE_RTKIT_APP_ENDPOINT_START && 594 !apple_rtkit_is_running(rtk)) 595 return -EINVAL; 596 |
614 if (atomic) 615 flags = GFP_ATOMIC; 616 else 617 flags = GFP_KERNEL; 618 619 msg = kzalloc(sizeof(*msg), flags); 620 if (!msg) 621 return -ENOMEM; 622 623 msg->mbox_msg.msg0 = message; 624 msg->mbox_msg.msg1 = ep; 625 msg->completion = completion; 626 | |
627 /* 628 * The message will be sent with a MMIO write. We need the barrier 629 * here to ensure any previous writes to buffers are visible to the 630 * device before that MMIO write happens. 631 */ 632 dma_wmb(); 633 | 597 /* 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. 601 */ 602 dma_wmb(); 603 |
634 ret = mbox_send_message(rtk->mbox_chan, &msg->mbox_msg); 635 if (ret < 0) { 636 kfree(msg); 637 return ret; 638 } 639 640 return 0; | 604 return apple_mbox_send(rtk->mbox, msg, atomic); |
641} 642EXPORT_SYMBOL_GPL(apple_rtkit_send_message); 643 644int apple_rtkit_poll(struct apple_rtkit *rtk) 645{ | 605} 606EXPORT_SYMBOL_GPL(apple_rtkit_send_message); 607 608int apple_rtkit_poll(struct apple_rtkit *rtk) 609{ |
646 return mbox_client_peek_data(rtk->mbox_chan); | 610 return apple_mbox_poll(rtk->mbox); |
647} 648EXPORT_SYMBOL_GPL(apple_rtkit_poll); 649 650int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) 651{ 652 u64 msg; 653 654 if (!test_bit(endpoint, rtk->endpoints)) --- 5 unchanged lines hidden (view full) --- 660 msg = FIELD_PREP(APPLE_RTKIT_MGMT_STARTEP_EP, endpoint); 661 msg |= APPLE_RTKIT_MGMT_STARTEP_FLAG; 662 apple_rtkit_management_send(rtk, APPLE_RTKIT_MGMT_STARTEP, msg); 663 664 return 0; 665} 666EXPORT_SYMBOL_GPL(apple_rtkit_start_ep); 667 | 611} 612EXPORT_SYMBOL_GPL(apple_rtkit_poll); 613 614int apple_rtkit_start_ep(struct apple_rtkit *rtk, u8 endpoint) 615{ 616 u64 msg; 617 618 if (!test_bit(endpoint, rtk->endpoints)) --- 5 unchanged lines hidden (view full) --- 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); 627 628 return 0; 629} 630EXPORT_SYMBOL_GPL(apple_rtkit_start_ep); 631 |
668static int apple_rtkit_request_mbox_chan(struct apple_rtkit *rtk) 669{ 670 if (rtk->mbox_name) 671 rtk->mbox_chan = mbox_request_channel_byname(&rtk->mbox_cl, 672 rtk->mbox_name); 673 else 674 rtk->mbox_chan = 675 mbox_request_channel(&rtk->mbox_cl, rtk->mbox_idx); 676 677 if (IS_ERR(rtk->mbox_chan)) 678 return PTR_ERR(rtk->mbox_chan); 679 return 0; 680} 681 | |
682struct apple_rtkit *apple_rtkit_init(struct device *dev, void *cookie, 683 const char *mbox_name, int mbox_idx, 684 const struct apple_rtkit_ops *ops) 685{ 686 struct apple_rtkit *rtk; 687 int ret; 688 689 if (!ops) --- 9 unchanged lines hidden (view full) --- 699 700 init_completion(&rtk->epmap_completion); 701 init_completion(&rtk->iop_pwr_ack_completion); 702 init_completion(&rtk->ap_pwr_ack_completion); 703 704 bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS); 705 set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints); 706 | 632struct 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) 635{ 636 struct apple_rtkit *rtk; 637 int ret; 638 639 if (!ops) --- 9 unchanged lines hidden (view full) --- 649 650 init_completion(&rtk->epmap_completion); 651 init_completion(&rtk->iop_pwr_ack_completion); 652 init_completion(&rtk->ap_pwr_ack_completion); 653 654 bitmap_zero(rtk->endpoints, APPLE_RTKIT_MAX_ENDPOINTS); 655 set_bit(APPLE_RTKIT_EP_MGMT, rtk->endpoints); 656 |
707 rtk->mbox_name = mbox_name; 708 rtk->mbox_idx = mbox_idx; 709 rtk->mbox_cl.dev = dev; 710 rtk->mbox_cl.tx_block = false; 711 rtk->mbox_cl.knows_txdone = false; 712 rtk->mbox_cl.rx_callback = &apple_rtkit_rx; 713 rtk->mbox_cl.tx_done = &apple_rtkit_tx_done; | 657 if (mbox_name) 658 rtk->mbox = apple_mbox_get_byname(dev, mbox_name); 659 else 660 rtk->mbox = apple_mbox_get(dev, mbox_idx); |
714 | 661 |
662 if (IS_ERR(rtk->mbox)) { 663 ret = PTR_ERR(rtk->mbox); 664 goto free_rtk; 665 } 666 667 rtk->mbox->rx = apple_rtkit_rx; 668 rtk->mbox->cookie = rtk; 669 |
|
715 rtk->wq = alloc_ordered_workqueue("rtkit-%s", WQ_MEM_RECLAIM, 716 dev_name(rtk->dev)); 717 if (!rtk->wq) { 718 ret = -ENOMEM; 719 goto free_rtk; 720 } 721 | 670 rtk->wq = alloc_ordered_workqueue("rtkit-%s", WQ_MEM_RECLAIM, 671 dev_name(rtk->dev)); 672 if (!rtk->wq) { 673 ret = -ENOMEM; 674 goto free_rtk; 675 } 676 |
722 ret = apple_rtkit_request_mbox_chan(rtk); | 677 ret = apple_mbox_start(rtk->mbox); |
723 if (ret) 724 goto destroy_wq; 725 726 return rtk; 727 728destroy_wq: 729 destroy_workqueue(rtk->wq); 730free_rtk: --- 14 unchanged lines hidden (view full) --- 745 return -ETIME; 746 else 747 return 0; 748} 749 750int apple_rtkit_reinit(struct apple_rtkit *rtk) 751{ 752 /* make sure we don't handle any messages while reinitializing */ | 678 if (ret) 679 goto destroy_wq; 680 681 return rtk; 682 683destroy_wq: 684 destroy_workqueue(rtk->wq); 685free_rtk: --- 14 unchanged lines hidden (view full) --- 700 return -ETIME; 701 else 702 return 0; 703} 704 705int apple_rtkit_reinit(struct apple_rtkit *rtk) 706{ 707 /* make sure we don't handle any messages while reinitializing */ |
753 mbox_free_channel(rtk->mbox_chan); | 708 apple_mbox_stop(rtk->mbox); |
754 flush_workqueue(rtk->wq); 755 756 apple_rtkit_free_buffer(rtk, &rtk->ioreport_buffer); 757 apple_rtkit_free_buffer(rtk, &rtk->crashlog_buffer); 758 apple_rtkit_free_buffer(rtk, &rtk->syslog_buffer); 759 760 kfree(rtk->syslog_msg_buffer); 761 --- 7 unchanged lines hidden (view full) --- 769 reinit_completion(&rtk->epmap_completion); 770 reinit_completion(&rtk->iop_pwr_ack_completion); 771 reinit_completion(&rtk->ap_pwr_ack_completion); 772 773 rtk->crashed = false; 774 rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_OFF; 775 rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_OFF; 776 | 709 flush_workqueue(rtk->wq); 710 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); 714 715 kfree(rtk->syslog_msg_buffer); 716 --- 7 unchanged lines hidden (view full) --- 724 reinit_completion(&rtk->epmap_completion); 725 reinit_completion(&rtk->iop_pwr_ack_completion); 726 reinit_completion(&rtk->ap_pwr_ack_completion); 727 728 rtk->crashed = false; 729 rtk->iop_power_state = APPLE_RTKIT_PWR_STATE_OFF; 730 rtk->ap_power_state = APPLE_RTKIT_PWR_STATE_OFF; 731 |
777 return apple_rtkit_request_mbox_chan(rtk); | 732 return apple_mbox_start(rtk->mbox); |
778} 779EXPORT_SYMBOL_GPL(apple_rtkit_reinit); 780 781static int apple_rtkit_set_ap_power_state(struct apple_rtkit *rtk, 782 unsigned int state) 783{ 784 u64 msg; 785 int ret; --- 139 unchanged lines hidden (view full) --- 925 msg); 926 927 return apple_rtkit_boot(rtk); 928} 929EXPORT_SYMBOL_GPL(apple_rtkit_wake); 930 931void apple_rtkit_free(struct apple_rtkit *rtk) 932{ | 733} 734EXPORT_SYMBOL_GPL(apple_rtkit_reinit); 735 736static int apple_rtkit_set_ap_power_state(struct apple_rtkit *rtk, 737 unsigned int state) 738{ 739 u64 msg; 740 int ret; --- 139 unchanged lines hidden (view full) --- 880 msg); 881 882 return apple_rtkit_boot(rtk); 883} 884EXPORT_SYMBOL_GPL(apple_rtkit_wake); 885 886void apple_rtkit_free(struct apple_rtkit *rtk) 887{ |
933 mbox_free_channel(rtk->mbox_chan); | 888 apple_mbox_stop(rtk->mbox); |
934 destroy_workqueue(rtk->wq); 935 936 apple_rtkit_free_buffer(rtk, &rtk->ioreport_buffer); 937 apple_rtkit_free_buffer(rtk, &rtk->crashlog_buffer); 938 apple_rtkit_free_buffer(rtk, &rtk->syslog_buffer); 939 940 kfree(rtk->syslog_msg_buffer); 941 kfree(rtk); --- 30 unchanged lines hidden --- | 889 destroy_workqueue(rtk->wq); 890 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); 894 895 kfree(rtk->syslog_msg_buffer); 896 kfree(rtk); --- 30 unchanged lines hidden --- |