-
Notifications
You must be signed in to change notification settings - Fork 11
/
1001-Add-apple-bce-driver.patch
5855 lines (5852 loc) · 202 KB
/
1001-Add-apple-bce-driver.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 54a49b19e01908431a429bd136dacd30c3db0d02 Mon Sep 17 00:00:00 2001
From: Aditya Garg <[email protected]>
Date: Thu, 11 Jul 2024 10:59:04 +0000
Subject: [PATCH] Add apple-bce driver
---
drivers/staging/apple-bce/Makefile | 28 +
drivers/staging/apple-bce/apple_bce.c | 445 ++++++++++
drivers/staging/apple-bce/apple_bce.h | 38 +
drivers/staging/apple-bce/audio/audio.c | 711 ++++++++++++++++
drivers/staging/apple-bce/audio/audio.h | 125 +++
drivers/staging/apple-bce/audio/description.h | 42 +
drivers/staging/apple-bce/audio/pcm.c | 308 +++++++
drivers/staging/apple-bce/audio/pcm.h | 16 +
drivers/staging/apple-bce/audio/protocol.c | 347 ++++++++
drivers/staging/apple-bce/audio/protocol.h | 147 ++++
.../staging/apple-bce/audio/protocol_bce.c | 226 ++++++
.../staging/apple-bce/audio/protocol_bce.h | 72 ++
drivers/staging/apple-bce/mailbox.c | 151 ++++
drivers/staging/apple-bce/mailbox.h | 53 ++
drivers/staging/apple-bce/queue.c | 390 +++++++++
drivers/staging/apple-bce/queue.h | 177 ++++
drivers/staging/apple-bce/queue_dma.c | 220 +++++
drivers/staging/apple-bce/queue_dma.h | 50 ++
drivers/staging/apple-bce/vhci/command.h | 204 +++++
drivers/staging/apple-bce/vhci/queue.c | 268 +++++++
drivers/staging/apple-bce/vhci/queue.h | 76 ++
drivers/staging/apple-bce/vhci/transfer.c | 661 +++++++++++++++
drivers/staging/apple-bce/vhci/transfer.h | 73 ++
drivers/staging/apple-bce/vhci/vhci.c | 759 ++++++++++++++++++
drivers/staging/apple-bce/vhci/vhci.h | 52 ++
25 files changed, 5639 insertions(+)
create mode 100644 drivers/staging/apple-bce/Makefile
create mode 100644 drivers/staging/apple-bce/apple_bce.c
create mode 100644 drivers/staging/apple-bce/apple_bce.h
create mode 100644 drivers/staging/apple-bce/audio/audio.c
create mode 100644 drivers/staging/apple-bce/audio/audio.h
create mode 100644 drivers/staging/apple-bce/audio/description.h
create mode 100644 drivers/staging/apple-bce/audio/pcm.c
create mode 100644 drivers/staging/apple-bce/audio/pcm.h
create mode 100644 drivers/staging/apple-bce/audio/protocol.c
create mode 100644 drivers/staging/apple-bce/audio/protocol.h
create mode 100644 drivers/staging/apple-bce/audio/protocol_bce.c
create mode 100644 drivers/staging/apple-bce/audio/protocol_bce.h
create mode 100644 drivers/staging/apple-bce/mailbox.c
create mode 100644 drivers/staging/apple-bce/mailbox.h
create mode 100644 drivers/staging/apple-bce/queue.c
create mode 100644 drivers/staging/apple-bce/queue.h
create mode 100644 drivers/staging/apple-bce/queue_dma.c
create mode 100644 drivers/staging/apple-bce/queue_dma.h
create mode 100644 drivers/staging/apple-bce/vhci/command.h
create mode 100644 drivers/staging/apple-bce/vhci/queue.c
create mode 100644 drivers/staging/apple-bce/vhci/queue.h
create mode 100644 drivers/staging/apple-bce/vhci/transfer.c
create mode 100644 drivers/staging/apple-bce/vhci/transfer.h
create mode 100644 drivers/staging/apple-bce/vhci/vhci.c
create mode 100644 drivers/staging/apple-bce/vhci/vhci.h
diff --git a/drivers/staging/apple-bce/Makefile b/drivers/staging/apple-bce/Makefile
new file mode 100644
index 000000000..a6a656f06
--- /dev/null
+++ b/drivers/staging/apple-bce/Makefile
@@ -0,0 +1,28 @@
+modname := apple-bce
+obj-m += $(modname).o
+
+apple-bce-objs := apple_bce.o mailbox.o queue.o queue_dma.o vhci/vhci.o vhci/queue.o vhci/transfer.o audio/audio.o audio/protocol.o audio/protocol_bce.o audio/pcm.o
+
+MY_CFLAGS += -DWITHOUT_NVME_PATCH
+#MY_CFLAGS += -g -DDEBUG
+ccflags-y += ${MY_CFLAGS}
+CC += ${MY_CFLAGS}
+
+KVERSION := $(KERNELRELEASE)
+ifeq ($(origin KERNELRELEASE), undefined)
+KVERSION := $(shell uname -r)
+endif
+
+KDIR := /lib/modules/$(KVERSION)/build
+PWD := $(shell pwd)
+
+.PHONY: all
+
+all:
+ $(MAKE) -C $(KDIR) M=$(PWD) modules
+
+clean:
+ $(MAKE) -C $(KDIR) M=$(PWD) clean
+
+install:
+ $(MAKE) -C $(KDIR) M=$(PWD) modules_install
diff --git a/drivers/staging/apple-bce/apple_bce.c b/drivers/staging/apple-bce/apple_bce.c
new file mode 100644
index 000000000..4fd2415d7
--- /dev/null
+++ b/drivers/staging/apple-bce/apple_bce.c
@@ -0,0 +1,445 @@
+#include "apple_bce.h"
+#include <linux/module.h>
+#include <linux/crc32.h>
+#include "audio/audio.h"
+#include <linux/version.h>
+
+static dev_t bce_chrdev;
+static struct class *bce_class;
+
+struct apple_bce_device *global_bce;
+
+static int bce_create_command_queues(struct apple_bce_device *bce);
+static void bce_free_command_queues(struct apple_bce_device *bce);
+static irqreturn_t bce_handle_mb_irq(int irq, void *dev);
+static irqreturn_t bce_handle_dma_irq(int irq, void *dev);
+static int bce_fw_version_handshake(struct apple_bce_device *bce);
+static int bce_register_command_queue(struct apple_bce_device *bce, struct bce_queue_memcfg *cfg, int is_sq);
+
+static int apple_bce_probe(struct pci_dev *dev, const struct pci_device_id *id)
+{
+ struct apple_bce_device *bce = NULL;
+ int status = 0;
+ int nvec;
+
+ pr_info("apple-bce: capturing our device\n");
+
+ if (pci_enable_device(dev))
+ return -ENODEV;
+ if (pci_request_regions(dev, "apple-bce")) {
+ status = -ENODEV;
+ goto fail;
+ }
+ pci_set_master(dev);
+ nvec = pci_alloc_irq_vectors(dev, 1, 8, PCI_IRQ_MSI);
+ if (nvec < 5) {
+ status = -EINVAL;
+ goto fail;
+ }
+
+ bce = kzalloc(sizeof(struct apple_bce_device), GFP_KERNEL);
+ if (!bce) {
+ status = -ENOMEM;
+ goto fail;
+ }
+
+ bce->pci = dev;
+ pci_set_drvdata(dev, bce);
+
+ bce->devt = bce_chrdev;
+ bce->dev = device_create(bce_class, &dev->dev, bce->devt, NULL, "apple-bce");
+ if (IS_ERR_OR_NULL(bce->dev)) {
+ status = PTR_ERR(bce_class);
+ goto fail;
+ }
+
+ bce->reg_mem_mb = pci_iomap(dev, 4, 0);
+ bce->reg_mem_dma = pci_iomap(dev, 2, 0);
+
+ if (IS_ERR_OR_NULL(bce->reg_mem_mb) || IS_ERR_OR_NULL(bce->reg_mem_dma)) {
+ dev_warn(&dev->dev, "apple-bce: Failed to pci_iomap required regions\n");
+ goto fail;
+ }
+
+ bce_mailbox_init(&bce->mbox, bce->reg_mem_mb);
+ bce_timestamp_init(&bce->timestamp, bce->reg_mem_mb);
+
+ spin_lock_init(&bce->queues_lock);
+ ida_init(&bce->queue_ida);
+
+ if ((status = pci_request_irq(dev, 0, bce_handle_mb_irq, NULL, dev, "bce_mbox")))
+ goto fail;
+ if ((status = pci_request_irq(dev, 4, NULL, bce_handle_dma_irq, dev, "bce_dma")))
+ goto fail_interrupt_0;
+
+ if ((status = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(37)))) {
+ dev_warn(&dev->dev, "dma: Setting mask failed\n");
+ goto fail_interrupt;
+ }
+
+ /* Gets the function 0's interface. This is needed because Apple only accepts DMA on our function if function 0
+ is a bus master, so we need to work around this. */
+ bce->pci0 = pci_get_slot(dev->bus, PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
+#ifndef WITHOUT_NVME_PATCH
+ if ((status = pci_enable_device_mem(bce->pci0))) {
+ dev_warn(&dev->dev, "apple-bce: failed to enable function 0\n");
+ goto fail_dev0;
+ }
+#endif
+ pci_set_master(bce->pci0);
+
+ bce_timestamp_start(&bce->timestamp, true);
+
+ if ((status = bce_fw_version_handshake(bce)))
+ goto fail_ts;
+ pr_info("apple-bce: handshake done\n");
+
+ if ((status = bce_create_command_queues(bce))) {
+ pr_info("apple-bce: Creating command queues failed\n");
+ goto fail_ts;
+ }
+
+ global_bce = bce;
+
+ bce_vhci_create(bce, &bce->vhci);
+
+ return 0;
+
+fail_ts:
+ bce_timestamp_stop(&bce->timestamp);
+#ifndef WITHOUT_NVME_PATCH
+ pci_disable_device(bce->pci0);
+fail_dev0:
+#endif
+ pci_dev_put(bce->pci0);
+fail_interrupt:
+ pci_free_irq(dev, 4, dev);
+fail_interrupt_0:
+ pci_free_irq(dev, 0, dev);
+fail:
+ if (bce && bce->dev) {
+ device_destroy(bce_class, bce->devt);
+
+ if (!IS_ERR_OR_NULL(bce->reg_mem_mb))
+ pci_iounmap(dev, bce->reg_mem_mb);
+ if (!IS_ERR_OR_NULL(bce->reg_mem_dma))
+ pci_iounmap(dev, bce->reg_mem_dma);
+
+ kfree(bce);
+ }
+
+ pci_free_irq_vectors(dev);
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+
+ if (!status)
+ status = -EINVAL;
+ return status;
+}
+
+static int bce_create_command_queues(struct apple_bce_device *bce)
+{
+ int status;
+ struct bce_queue_memcfg *cfg;
+
+ bce->cmd_cq = bce_alloc_cq(bce, 0, 0x20);
+ bce->cmd_cmdq = bce_alloc_cmdq(bce, 1, 0x20);
+ if (bce->cmd_cq == NULL || bce->cmd_cmdq == NULL) {
+ status = -ENOMEM;
+ goto err;
+ }
+ bce->queues[0] = (struct bce_queue *) bce->cmd_cq;
+ bce->queues[1] = (struct bce_queue *) bce->cmd_cmdq->sq;
+
+ cfg = kzalloc(sizeof(struct bce_queue_memcfg), GFP_KERNEL);
+ if (!cfg) {
+ status = -ENOMEM;
+ goto err;
+ }
+ bce_get_cq_memcfg(bce->cmd_cq, cfg);
+ if ((status = bce_register_command_queue(bce, cfg, false)))
+ goto err;
+ bce_get_sq_memcfg(bce->cmd_cmdq->sq, bce->cmd_cq, cfg);
+ if ((status = bce_register_command_queue(bce, cfg, true)))
+ goto err;
+ kfree(cfg);
+
+ return 0;
+
+err:
+ if (bce->cmd_cq)
+ bce_free_cq(bce, bce->cmd_cq);
+ if (bce->cmd_cmdq)
+ bce_free_cmdq(bce, bce->cmd_cmdq);
+ return status;
+}
+
+static void bce_free_command_queues(struct apple_bce_device *bce)
+{
+ bce_free_cq(bce, bce->cmd_cq);
+ bce_free_cmdq(bce, bce->cmd_cmdq);
+ bce->cmd_cq = NULL;
+ bce->queues[0] = NULL;
+}
+
+static irqreturn_t bce_handle_mb_irq(int irq, void *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(dev);
+ bce_mailbox_handle_interrupt(&bce->mbox);
+ return IRQ_HANDLED;
+}
+
+static irqreturn_t bce_handle_dma_irq(int irq, void *dev)
+{
+ int i;
+ struct apple_bce_device *bce = pci_get_drvdata(dev);
+ spin_lock(&bce->queues_lock);
+ for (i = 0; i < BCE_MAX_QUEUE_COUNT; i++)
+ if (bce->queues[i] && bce->queues[i]->type == BCE_QUEUE_CQ)
+ bce_handle_cq_completions(bce, (struct bce_queue_cq *) bce->queues[i]);
+ spin_unlock(&bce->queues_lock);
+ return IRQ_HANDLED;
+}
+
+static int bce_fw_version_handshake(struct apple_bce_device *bce)
+{
+ u64 result;
+ int status;
+
+ if ((status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_SET_FW_PROTOCOL_VERSION, BC_PROTOCOL_VERSION),
+ &result)))
+ return status;
+ if (BCE_MB_TYPE(result) != BCE_MB_SET_FW_PROTOCOL_VERSION ||
+ BCE_MB_VALUE(result) != BC_PROTOCOL_VERSION) {
+ pr_err("apple-bce: FW version handshake failed %x:%llx\n", BCE_MB_TYPE(result), BCE_MB_VALUE(result));
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int bce_register_command_queue(struct apple_bce_device *bce, struct bce_queue_memcfg *cfg, int is_sq)
+{
+ int status;
+ int cmd_type;
+ u64 result;
+ // OS X uses an bidirectional direction, but that's not really needed
+ dma_addr_t a = dma_map_single(&bce->pci->dev, cfg, sizeof(struct bce_queue_memcfg), DMA_TO_DEVICE);
+ if (dma_mapping_error(&bce->pci->dev, a))
+ return -ENOMEM;
+ cmd_type = is_sq ? BCE_MB_REGISTER_COMMAND_SQ : BCE_MB_REGISTER_COMMAND_CQ;
+ status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(cmd_type, a), &result);
+ dma_unmap_single(&bce->pci->dev, a, sizeof(struct bce_queue_memcfg), DMA_TO_DEVICE);
+ if (status)
+ return status;
+ if (BCE_MB_TYPE(result) != BCE_MB_REGISTER_COMMAND_QUEUE_REPLY)
+ return -EINVAL;
+ return 0;
+}
+
+static void apple_bce_remove(struct pci_dev *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(dev);
+ bce->is_being_removed = true;
+
+ bce_vhci_destroy(&bce->vhci);
+
+ bce_timestamp_stop(&bce->timestamp);
+#ifndef WITHOUT_NVME_PATCH
+ pci_disable_device(bce->pci0);
+#endif
+ pci_dev_put(bce->pci0);
+ pci_free_irq(dev, 0, dev);
+ pci_free_irq(dev, 4, dev);
+ bce_free_command_queues(bce);
+ pci_iounmap(dev, bce->reg_mem_mb);
+ pci_iounmap(dev, bce->reg_mem_dma);
+ device_destroy(bce_class, bce->devt);
+ pci_free_irq_vectors(dev);
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+ kfree(bce);
+}
+
+static int bce_save_state_and_sleep(struct apple_bce_device *bce)
+{
+ int attempt, status = 0;
+ u64 resp;
+ dma_addr_t dma_addr;
+ void *dma_ptr = NULL;
+ size_t size = max(PAGE_SIZE, 4096UL);
+
+ for (attempt = 0; attempt < 5; ++attempt) {
+ pr_debug("apple-bce: suspend: attempt %i, buffer size %li\n", attempt, size);
+ dma_ptr = dma_alloc_coherent(&bce->pci->dev, size, &dma_addr, GFP_KERNEL);
+ if (!dma_ptr) {
+ pr_err("apple-bce: suspend failed (data alloc failed)\n");
+ break;
+ }
+ BUG_ON((dma_addr % 4096) != 0);
+ status = bce_mailbox_send(&bce->mbox,
+ BCE_MB_MSG(BCE_MB_SAVE_STATE_AND_SLEEP, (dma_addr & ~(4096LLU - 1)) | (size / 4096)), &resp);
+ if (status) {
+ pr_err("apple-bce: suspend failed (mailbox send)\n");
+ break;
+ }
+ if (BCE_MB_TYPE(resp) == BCE_MB_SAVE_RESTORE_STATE_COMPLETE) {
+ bce->saved_data_dma_addr = dma_addr;
+ bce->saved_data_dma_ptr = dma_ptr;
+ bce->saved_data_dma_size = size;
+ return 0;
+ } else if (BCE_MB_TYPE(resp) == BCE_MB_SAVE_STATE_AND_SLEEP_FAILURE) {
+ dma_free_coherent(&bce->pci->dev, size, dma_ptr, dma_addr);
+ /* The 0x10ff magic value was extracted from Apple's driver */
+ size = (BCE_MB_VALUE(resp) + 0x10ff) & ~(4096LLU - 1);
+ pr_debug("apple-bce: suspend: device requested a larger buffer (%li)\n", size);
+ continue;
+ } else {
+ pr_err("apple-bce: suspend failed (invalid device response)\n");
+ status = -EINVAL;
+ break;
+ }
+ }
+ if (dma_ptr)
+ dma_free_coherent(&bce->pci->dev, size, dma_ptr, dma_addr);
+ if (!status)
+ return bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_SLEEP_NO_STATE, 0), &resp);
+ return status;
+}
+
+static int bce_restore_state_and_wake(struct apple_bce_device *bce)
+{
+ int status;
+ u64 resp;
+ if (!bce->saved_data_dma_ptr) {
+ if ((status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_RESTORE_NO_STATE, 0), &resp))) {
+ pr_err("apple-bce: resume with no state failed (mailbox send)\n");
+ return status;
+ }
+ if (BCE_MB_TYPE(resp) != BCE_MB_RESTORE_NO_STATE) {
+ pr_err("apple-bce: resume with no state failed (invalid device response)\n");
+ return -EINVAL;
+ }
+ return 0;
+ }
+
+ if ((status = bce_mailbox_send(&bce->mbox, BCE_MB_MSG(BCE_MB_RESTORE_STATE_AND_WAKE,
+ (bce->saved_data_dma_addr & ~(4096LLU - 1)) | (bce->saved_data_dma_size / 4096)), &resp))) {
+ pr_err("apple-bce: resume with state failed (mailbox send)\n");
+ goto finish_with_state;
+ }
+ if (BCE_MB_TYPE(resp) != BCE_MB_SAVE_RESTORE_STATE_COMPLETE) {
+ pr_err("apple-bce: resume with state failed (invalid device response)\n");
+ status = -EINVAL;
+ goto finish_with_state;
+ }
+
+finish_with_state:
+ dma_free_coherent(&bce->pci->dev, bce->saved_data_dma_size, bce->saved_data_dma_ptr, bce->saved_data_dma_addr);
+ bce->saved_data_dma_ptr = NULL;
+ return status;
+}
+
+static int apple_bce_suspend(struct device *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(to_pci_dev(dev));
+ int status;
+
+ bce_timestamp_stop(&bce->timestamp);
+
+ if ((status = bce_save_state_and_sleep(bce)))
+ return status;
+
+ return 0;
+}
+
+static int apple_bce_resume(struct device *dev)
+{
+ struct apple_bce_device *bce = pci_get_drvdata(to_pci_dev(dev));
+ int status;
+
+ pci_set_master(bce->pci);
+ pci_set_master(bce->pci0);
+
+ if ((status = bce_restore_state_and_wake(bce)))
+ return status;
+
+ bce_timestamp_start(&bce->timestamp, false);
+
+ return 0;
+}
+
+static struct pci_device_id apple_bce_ids[ ] = {
+ { PCI_DEVICE(PCI_VENDOR_ID_APPLE, 0x1801) },
+ { 0, },
+};
+
+MODULE_DEVICE_TABLE(pci, apple_bce_ids);
+
+struct dev_pm_ops apple_bce_pci_driver_pm = {
+ .suspend = apple_bce_suspend,
+ .resume = apple_bce_resume
+};
+struct pci_driver apple_bce_pci_driver = {
+ .name = "apple-bce",
+ .id_table = apple_bce_ids,
+ .probe = apple_bce_probe,
+ .remove = apple_bce_remove,
+ .driver = {
+ .pm = &apple_bce_pci_driver_pm
+ }
+};
+
+
+static int __init apple_bce_module_init(void)
+{
+ int result;
+ if ((result = alloc_chrdev_region(&bce_chrdev, 0, 1, "apple-bce")))
+ goto fail_chrdev;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(6,4,0)
+ bce_class = class_create(THIS_MODULE, "apple-bce");
+#else
+ bce_class = class_create("apple-bce");
+#endif
+ if (IS_ERR(bce_class)) {
+ result = PTR_ERR(bce_class);
+ goto fail_class;
+ }
+ if ((result = bce_vhci_module_init())) {
+ pr_err("apple-bce: bce-vhci init failed");
+ goto fail_class;
+ }
+
+ result = pci_register_driver(&apple_bce_pci_driver);
+ if (result)
+ goto fail_drv;
+
+ aaudio_module_init();
+
+ return 0;
+
+fail_drv:
+ pci_unregister_driver(&apple_bce_pci_driver);
+fail_class:
+ class_destroy(bce_class);
+fail_chrdev:
+ unregister_chrdev_region(bce_chrdev, 1);
+ if (!result)
+ result = -EINVAL;
+ return result;
+}
+static void __exit apple_bce_module_exit(void)
+{
+ pci_unregister_driver(&apple_bce_pci_driver);
+
+ aaudio_module_exit();
+ bce_vhci_module_exit();
+ class_destroy(bce_class);
+ unregister_chrdev_region(bce_chrdev, 1);
+}
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("MrARM");
+MODULE_DESCRIPTION("Apple BCE Driver");
+MODULE_VERSION("0.01");
+module_init(apple_bce_module_init);
+module_exit(apple_bce_module_exit);
diff --git a/drivers/staging/apple-bce/apple_bce.h b/drivers/staging/apple-bce/apple_bce.h
new file mode 100644
index 000000000..f13ab8d57
--- /dev/null
+++ b/drivers/staging/apple-bce/apple_bce.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include "mailbox.h"
+#include "queue.h"
+#include "vhci/vhci.h"
+
+#define BC_PROTOCOL_VERSION 0x20001
+#define BCE_MAX_QUEUE_COUNT 0x100
+
+#define BCE_QUEUE_USER_MIN 2
+#define BCE_QUEUE_USER_MAX (BCE_MAX_QUEUE_COUNT - 1)
+
+struct apple_bce_device {
+ struct pci_dev *pci, *pci0;
+ dev_t devt;
+ struct device *dev;
+ void __iomem *reg_mem_mb;
+ void __iomem *reg_mem_dma;
+ struct bce_mailbox mbox;
+ struct bce_timestamp timestamp;
+ struct bce_queue *queues[BCE_MAX_QUEUE_COUNT];
+ struct spinlock queues_lock;
+ struct ida queue_ida;
+ struct bce_queue_cq *cmd_cq;
+ struct bce_queue_cmdq *cmd_cmdq;
+ struct bce_queue_sq *int_sq_list[BCE_MAX_QUEUE_COUNT];
+ bool is_being_removed;
+
+ dma_addr_t saved_data_dma_addr;
+ void *saved_data_dma_ptr;
+ size_t saved_data_dma_size;
+
+ struct bce_vhci vhci;
+};
+
+extern struct apple_bce_device *global_bce;
\ No newline at end of file
diff --git a/drivers/staging/apple-bce/audio/audio.c b/drivers/staging/apple-bce/audio/audio.c
new file mode 100644
index 000000000..bd16ddd16
--- /dev/null
+++ b/drivers/staging/apple-bce/audio/audio.c
@@ -0,0 +1,711 @@
+#include <linux/pci.h>
+#include <linux/spinlock.h>
+#include <linux/module.h>
+#include <linux/random.h>
+#include <sound/core.h>
+#include <sound/initval.h>
+#include <sound/pcm.h>
+#include <sound/jack.h>
+#include "audio.h"
+#include "pcm.h"
+#include <linux/version.h>
+
+static int aaudio_alsa_index = SNDRV_DEFAULT_IDX1;
+static char *aaudio_alsa_id = SNDRV_DEFAULT_STR1;
+
+static dev_t aaudio_chrdev;
+static struct class *aaudio_class;
+
+static int aaudio_init_cmd(struct aaudio_device *a);
+static int aaudio_init_bs(struct aaudio_device *a);
+static void aaudio_init_dev(struct aaudio_device *a, aaudio_device_id_t dev_id);
+static void aaudio_free_dev(struct aaudio_subdevice *sdev);
+
+static int aaudio_probe(struct pci_dev *dev, const struct pci_device_id *id)
+{
+ struct aaudio_device *aaudio = NULL;
+ struct aaudio_subdevice *sdev = NULL;
+ int status = 0;
+ u32 cfg;
+
+ pr_info("aaudio: capturing our device\n");
+
+ if (pci_enable_device(dev))
+ return -ENODEV;
+ if (pci_request_regions(dev, "aaudio")) {
+ status = -ENODEV;
+ goto fail;
+ }
+ pci_set_master(dev);
+
+ aaudio = kzalloc(sizeof(struct aaudio_device), GFP_KERNEL);
+ if (!aaudio) {
+ status = -ENOMEM;
+ goto fail;
+ }
+
+ aaudio->bce = global_bce;
+ if (!aaudio->bce) {
+ dev_warn(&dev->dev, "aaudio: No BCE available\n");
+ status = -EINVAL;
+ goto fail;
+ }
+
+ aaudio->pci = dev;
+ pci_set_drvdata(dev, aaudio);
+
+ aaudio->devt = aaudio_chrdev;
+ aaudio->dev = device_create(aaudio_class, &dev->dev, aaudio->devt, NULL, "aaudio");
+ if (IS_ERR_OR_NULL(aaudio->dev)) {
+ status = PTR_ERR(aaudio_class);
+ goto fail;
+ }
+ device_link_add(aaudio->dev, aaudio->bce->dev, DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER);
+
+ init_completion(&aaudio->remote_alive);
+ INIT_LIST_HEAD(&aaudio->subdevice_list);
+
+ /* Init: set an unknown flag in the bitset */
+ if (pci_read_config_dword(dev, 4, &cfg))
+ dev_warn(&dev->dev, "aaudio: pci_read_config_dword fail\n");
+ if (pci_write_config_dword(dev, 4, cfg | 6u))
+ dev_warn(&dev->dev, "aaudio: pci_write_config_dword fail\n");
+
+ dev_info(aaudio->dev, "aaudio: bs len = %llx\n", pci_resource_len(dev, 0));
+ aaudio->reg_mem_bs_dma = pci_resource_start(dev, 0);
+ aaudio->reg_mem_bs = pci_iomap(dev, 0, 0);
+ aaudio->reg_mem_cfg = pci_iomap(dev, 4, 0);
+
+ aaudio->reg_mem_gpr = (u32 __iomem *) ((u8 __iomem *) aaudio->reg_mem_cfg + 0xC000);
+
+ if (IS_ERR_OR_NULL(aaudio->reg_mem_bs) || IS_ERR_OR_NULL(aaudio->reg_mem_cfg)) {
+ dev_warn(&dev->dev, "aaudio: Failed to pci_iomap required regions\n");
+ goto fail;
+ }
+
+ if (aaudio_bce_init(aaudio)) {
+ dev_warn(&dev->dev, "aaudio: Failed to init BCE command transport\n");
+ goto fail;
+ }
+
+ if (snd_card_new(aaudio->dev, aaudio_alsa_index, aaudio_alsa_id, THIS_MODULE, 0, &aaudio->card)) {
+ dev_err(&dev->dev, "aaudio: Failed to create ALSA card\n");
+ goto fail;
+ }
+
+ strcpy(aaudio->card->shortname, "Apple T2 Audio");
+ strcpy(aaudio->card->longname, "Apple T2 Audio");
+ strcpy(aaudio->card->mixername, "Apple T2 Audio");
+ /* Dynamic alsa ids start at 100 */
+ aaudio->next_alsa_id = 100;
+
+ if (aaudio_init_cmd(aaudio)) {
+ dev_err(&dev->dev, "aaudio: Failed to initialize over BCE\n");
+ goto fail_snd;
+ }
+
+ if (aaudio_init_bs(aaudio)) {
+ dev_err(&dev->dev, "aaudio: Failed to initialize BufferStruct\n");
+ goto fail_snd;
+ }
+
+ if ((status = aaudio_cmd_set_remote_access(aaudio, AAUDIO_REMOTE_ACCESS_ON))) {
+ dev_err(&dev->dev, "Failed to set remote access\n");
+ return status;
+ }
+
+ if (snd_card_register(aaudio->card)) {
+ dev_err(&dev->dev, "aaudio: Failed to register ALSA sound device\n");
+ goto fail_snd;
+ }
+
+ list_for_each_entry(sdev, &aaudio->subdevice_list, list) {
+ struct aaudio_buffer_struct_device *dev = &aaudio->bs->devices[sdev->buf_id];
+
+ if (sdev->out_stream_cnt == 1 && !strcmp(dev->name, "Speaker")) {
+ struct snd_pcm_hardware *hw = sdev->out_streams[0].alsa_hw_desc;
+
+ snprintf(aaudio->card->driver, sizeof(aaudio->card->driver) / sizeof(char), "AppleT2x%d", hw->channels_min);
+ }
+ }
+
+ return 0;
+
+fail_snd:
+ snd_card_free(aaudio->card);
+fail:
+ if (aaudio && aaudio->dev)
+ device_destroy(aaudio_class, aaudio->devt);
+ kfree(aaudio);
+
+ if (!IS_ERR_OR_NULL(aaudio->reg_mem_bs))
+ pci_iounmap(dev, aaudio->reg_mem_bs);
+ if (!IS_ERR_OR_NULL(aaudio->reg_mem_cfg))
+ pci_iounmap(dev, aaudio->reg_mem_cfg);
+
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+
+ if (!status)
+ status = -EINVAL;
+ return status;
+}
+
+
+
+static void aaudio_remove(struct pci_dev *dev)
+{
+ struct aaudio_subdevice *sdev;
+ struct aaudio_device *aaudio = pci_get_drvdata(dev);
+
+ snd_card_free(aaudio->card);
+ while (!list_empty(&aaudio->subdevice_list)) {
+ sdev = list_first_entry(&aaudio->subdevice_list, struct aaudio_subdevice, list);
+ list_del(&sdev->list);
+ aaudio_free_dev(sdev);
+ }
+ pci_iounmap(dev, aaudio->reg_mem_bs);
+ pci_iounmap(dev, aaudio->reg_mem_cfg);
+ device_destroy(aaudio_class, aaudio->devt);
+ pci_free_irq_vectors(dev);
+ pci_release_regions(dev);
+ pci_disable_device(dev);
+ kfree(aaudio);
+}
+
+static int aaudio_suspend(struct device *dev)
+{
+ struct aaudio_device *aaudio = pci_get_drvdata(to_pci_dev(dev));
+
+ if (aaudio_cmd_set_remote_access(aaudio, AAUDIO_REMOTE_ACCESS_OFF))
+ dev_warn(aaudio->dev, "Failed to reset remote access\n");
+
+ pci_disable_device(aaudio->pci);
+ return 0;
+}
+
+static int aaudio_resume(struct device *dev)
+{
+ int status;
+ struct aaudio_device *aaudio = pci_get_drvdata(to_pci_dev(dev));
+
+ if ((status = pci_enable_device(aaudio->pci)))
+ return status;
+ pci_set_master(aaudio->pci);
+
+ if ((status = aaudio_cmd_set_remote_access(aaudio, AAUDIO_REMOTE_ACCESS_ON))) {
+ dev_err(aaudio->dev, "Failed to set remote access\n");
+ return status;
+ }
+
+ return 0;
+}
+
+static int aaudio_init_cmd(struct aaudio_device *a)
+{
+ int status;
+ struct aaudio_send_ctx sctx;
+ struct aaudio_msg buf;
+ u64 dev_cnt, dev_i;
+ aaudio_device_id_t *dev_l;
+
+ if ((status = aaudio_send(a, &sctx, 500,
+ aaudio_msg_write_alive_notification, 1, 3))) {
+ dev_err(a->dev, "Sending alive notification failed\n");
+ return status;
+ }
+
+ if (wait_for_completion_timeout(&a->remote_alive, msecs_to_jiffies(500)) == 0) {
+ dev_err(a->dev, "Timed out waiting for remote\n");
+ return -ETIMEDOUT;
+ }
+ dev_info(a->dev, "Continuing init\n");
+
+ buf = aaudio_reply_alloc();
+ if ((status = aaudio_cmd_get_device_list(a, &buf, &dev_l, &dev_cnt))) {
+ dev_err(a->dev, "Failed to get device list\n");
+ aaudio_reply_free(&buf);
+ return status;
+ }
+ for (dev_i = 0; dev_i < dev_cnt; ++dev_i)
+ aaudio_init_dev(a, dev_l[dev_i]);
+ aaudio_reply_free(&buf);
+
+ return 0;
+}
+
+static void aaudio_init_stream_info(struct aaudio_subdevice *sdev, struct aaudio_stream *strm);
+static void aaudio_handle_jack_connection_change(struct aaudio_subdevice *sdev);
+
+static void aaudio_init_dev(struct aaudio_device *a, aaudio_device_id_t dev_id)
+{
+ struct aaudio_subdevice *sdev;
+ struct aaudio_msg buf = aaudio_reply_alloc();
+ u64 uid_len, stream_cnt, i;
+ aaudio_object_id_t *stream_list;
+ char *uid;
+
+ sdev = kzalloc(sizeof(struct aaudio_subdevice), GFP_KERNEL);
+
+ if (aaudio_cmd_get_property(a, &buf, dev_id, dev_id, AAUDIO_PROP(AAUDIO_PROP_SCOPE_GLOBAL, AAUDIO_PROP_UID, 0),
+ NULL, 0, (void **) &uid, &uid_len) || uid_len > AAUDIO_DEVICE_MAX_UID_LEN) {
+ dev_err(a->dev, "Failed to get device uid for device %llx\n", dev_id);
+ goto fail;
+ }
+ dev_info(a->dev, "Remote device %llx %.*s\n", dev_id, (int) uid_len, uid);
+
+ sdev->a = a;
+ INIT_LIST_HEAD(&sdev->list);
+ sdev->dev_id = dev_id;
+ sdev->buf_id = AAUDIO_BUFFER_ID_NONE;
+ strncpy(sdev->uid, uid, uid_len);
+ sdev->uid[uid_len + 1] = '\0';
+
+ if (aaudio_cmd_get_primitive_property(a, dev_id, dev_id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_INPUT, AAUDIO_PROP_LATENCY, 0), NULL, 0, &sdev->in_latency, sizeof(u32)))
+ dev_warn(a->dev, "Failed to query device input latency\n");
+ if (aaudio_cmd_get_primitive_property(a, dev_id, dev_id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_OUTPUT, AAUDIO_PROP_LATENCY, 0), NULL, 0, &sdev->out_latency, sizeof(u32)))
+ dev_warn(a->dev, "Failed to query device output latency\n");
+
+ if (aaudio_cmd_get_input_stream_list(a, &buf, dev_id, &stream_list, &stream_cnt)) {
+ dev_err(a->dev, "Failed to get input stream list for device %llx\n", dev_id);
+ goto fail;
+ }
+ if (stream_cnt > AAUDIO_DEIVCE_MAX_INPUT_STREAMS) {
+ dev_warn(a->dev, "Device %s input stream count %llu is larger than the supported count of %u\n",
+ sdev->uid, stream_cnt, AAUDIO_DEIVCE_MAX_INPUT_STREAMS);
+ stream_cnt = AAUDIO_DEIVCE_MAX_INPUT_STREAMS;
+ }
+ sdev->in_stream_cnt = stream_cnt;
+ for (i = 0; i < stream_cnt; i++) {
+ sdev->in_streams[i].id = stream_list[i];
+ sdev->in_streams[i].buffer_cnt = 0;
+ aaudio_init_stream_info(sdev, &sdev->in_streams[i]);
+ sdev->in_streams[i].latency += sdev->in_latency;
+ }
+
+ if (aaudio_cmd_get_output_stream_list(a, &buf, dev_id, &stream_list, &stream_cnt)) {
+ dev_err(a->dev, "Failed to get output stream list for device %llx\n", dev_id);
+ goto fail;
+ }
+ if (stream_cnt > AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS) {
+ dev_warn(a->dev, "Device %s input stream count %llu is larger than the supported count of %u\n",
+ sdev->uid, stream_cnt, AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS);
+ stream_cnt = AAUDIO_DEIVCE_MAX_OUTPUT_STREAMS;
+ }
+ sdev->out_stream_cnt = stream_cnt;
+ for (i = 0; i < stream_cnt; i++) {
+ sdev->out_streams[i].id = stream_list[i];
+ sdev->out_streams[i].buffer_cnt = 0;
+ aaudio_init_stream_info(sdev, &sdev->out_streams[i]);
+ sdev->out_streams[i].latency += sdev->in_latency;
+ }
+
+ if (sdev->is_pcm)
+ aaudio_create_pcm(sdev);
+ /* Headphone Jack status */
+ if (!strcmp(sdev->uid, "Codec Output")) {
+ if (snd_jack_new(a->card, sdev->uid, SND_JACK_HEADPHONE, &sdev->jack, true, false))
+ dev_warn(a->dev, "Failed to create an attached jack for %s\n", sdev->uid);
+ aaudio_cmd_property_listener(a, sdev->dev_id, sdev->dev_id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_OUTPUT, AAUDIO_PROP_JACK_PLUGGED, 0));
+ aaudio_handle_jack_connection_change(sdev);
+ }
+
+ aaudio_reply_free(&buf);
+
+ list_add_tail(&sdev->list, &a->subdevice_list);
+ return;
+
+fail:
+ aaudio_reply_free(&buf);
+ kfree(sdev);
+}
+
+static void aaudio_init_stream_info(struct aaudio_subdevice *sdev, struct aaudio_stream *strm)
+{
+ if (aaudio_cmd_get_primitive_property(sdev->a, sdev->dev_id, strm->id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_GLOBAL, AAUDIO_PROP_PHYS_FORMAT, 0), NULL, 0,
+ &strm->desc, sizeof(strm->desc)))
+ dev_warn(sdev->a->dev, "Failed to query stream descriptor\n");
+ if (aaudio_cmd_get_primitive_property(sdev->a, sdev->dev_id, strm->id,
+ AAUDIO_PROP(AAUDIO_PROP_SCOPE_GLOBAL, AAUDIO_PROP_LATENCY, 0), NULL, 0, &strm->latency, sizeof(u32)))
+ dev_warn(sdev->a->dev, "Failed to query stream latency\n");
+ if (strm->desc.format_id == AAUDIO_FORMAT_LPCM)
+ sdev->is_pcm = true;
+}
+
+static void aaudio_free_dev(struct aaudio_subdevice *sdev)
+{
+ size_t i;
+ for (i = 0; i < sdev->in_stream_cnt; i++) {
+ if (sdev->in_streams[i].alsa_hw_desc)
+ kfree(sdev->in_streams[i].alsa_hw_desc);
+ if (sdev->in_streams[i].buffers)
+ kfree(sdev->in_streams[i].buffers);
+ }
+ for (i = 0; i < sdev->out_stream_cnt; i++) {
+ if (sdev->out_streams[i].alsa_hw_desc)
+ kfree(sdev->out_streams[i].alsa_hw_desc);
+ if (sdev->out_streams[i].buffers)
+ kfree(sdev->out_streams[i].buffers);
+ }
+ kfree(sdev);
+}
+
+static struct aaudio_subdevice *aaudio_find_dev_by_dev_id(struct aaudio_device *a, aaudio_device_id_t dev_id)
+{
+ struct aaudio_subdevice *sdev;
+ list_for_each_entry(sdev, &a->subdevice_list, list) {
+ if (dev_id == sdev->dev_id)
+ return sdev;
+ }
+ return NULL;
+}
+
+static struct aaudio_subdevice *aaudio_find_dev_by_uid(struct aaudio_device *a, const char *uid)
+{
+ struct aaudio_subdevice *sdev;
+ list_for_each_entry(sdev, &a->subdevice_list, list) {
+ if (!strcmp(uid, sdev->uid))
+ return sdev;
+ }
+ return NULL;
+}
+
+static void aaudio_init_bs_stream(struct aaudio_device *a, struct aaudio_stream *strm,
+ struct aaudio_buffer_struct_stream *bs_strm);
+static void aaudio_init_bs_stream_host(struct aaudio_device *a, struct aaudio_stream *strm,
+ struct aaudio_buffer_struct_stream *bs_strm);
+
+static int aaudio_init_bs(struct aaudio_device *a)
+{
+ int i, j;
+ struct aaudio_buffer_struct_device *dev;
+ struct aaudio_subdevice *sdev;
+ u32 ver, sig, bs_base;
+
+ ver = ioread32(&a->reg_mem_gpr[0]);
+ if (ver < 3) {
+ dev_err(a->dev, "aaudio: Bad GPR version (%u)", ver);
+ return -EINVAL;
+ }
+ sig = ioread32(&a->reg_mem_gpr[1]);
+ if (sig != AAUDIO_SIG) {
+ dev_err(a->dev, "aaudio: Bad GPR sig (%x)", sig);
+ return -EINVAL;
+ }
+ bs_base = ioread32(&a->reg_mem_gpr[2]);
+ a->bs = (struct aaudio_buffer_struct *) ((u8 *) a->reg_mem_bs + bs_base);
+ if (a->bs->signature != AAUDIO_SIG) {
+ dev_err(a->dev, "aaudio: Bad BufferStruct sig (%x)", a->bs->signature);
+ return -EINVAL;
+ }
+ dev_info(a->dev, "aaudio: BufferStruct ver = %i\n", a->bs->version);
+ dev_info(a->dev, "aaudio: Num devices = %i\n", a->bs->num_devices);