-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0001-linux-v6.11-rc3-bao-io-dispatcher.patch
3222 lines (3215 loc) · 88.1 KB
/
0001-linux-v6.11-rc3-bao-io-dispatcher.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
diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documentation/userspace-api/ioctl/ioctl-number.rst
index e91c0376e..3116fe1ce 100644
--- a/Documentation/userspace-api/ioctl/ioctl-number.rst
+++ b/Documentation/userspace-api/ioctl/ioctl-number.rst
@@ -340,6 +340,7 @@ Code Seq# Include File Comments
<mailto:[email protected]>
0xA5 20-2F linux/surface_aggregator/dtx.h Microsoft Surface DTX driver
<mailto:[email protected]>
+0xA6 all uapi/linux/bao.h Bao hypervisor
0xAA 00-3F linux/uapi/linux/userfaultfd.h
0xAB 00-1F linux/nbd.h
0xAC 00-1F linux/raw.h
diff --git a/arch/arm/include/asm/bao.h b/arch/arm/include/asm/bao.h
new file mode 100644
index 000000000..957330d9d
--- /dev/null
+++ b/arch/arm/include/asm/bao.h
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hypercall for Bao Hypervisor on ARM
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ * João Peixoto <[email protected]>
+ */
+
+#ifndef __ASM_ARM_BAO_H
+#define __ASM_ARM_BAO_H
+
+#include <asm/bao.h>
+#include <linux/bao.h>
+#include <linux/arm-smccc.h>
+
+/**
+ * asm_bao_hypercall_remio() - Performs a Remote I/O Hypercall
+ * @remio_hc_id: VirtIO Hypercall ID
+ * @dm_id: Device Model ID
+ * @addr: Access address
+ * @op: Write, Read, Ask or Notify operation
+ * @value: Value to write or read
+ * @cpu_id: CPU ID
+ * @vcpu_id: VCPU ID
+ *
+ * @return: The VirtIO request structure
+ */
+static inline struct bao_virtio_request
+asm_bao_hypercall_remio(u64 remio_hc_id, u64 dm_id, u64 addr, u64 op,
+ u64 value, u64 cpu_id, u64 vcpu_id)
+{
+ register int x0 asm("r0") =
+ ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
+ ARM_SMCCC_OWNER_VENDOR_HYP, remio_hc_id);
+ register u32 x1 asm("r1") = dm_id;
+ register u32 x2 asm("r2") = addr;
+ register u32 x3 asm("r3") = op;
+ register u32 x4 asm("r4") = value;
+ register u32 x5 asm("r5") = cpu_id;
+ register u32 x6 asm("r6") = vcpu_id;
+
+ struct bao_virtio_request ret;
+
+ asm volatile("hvc 0\n\t"
+ : "=r"(x0), "=r"(x1), "=r"(x2), "=r"(x3), "=r"(x4),
+ "=r"(x5), "=r"(x6)
+ : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5),
+ "r"(x6)
+ : "memory");
+
+ ret.ret = x0;
+ ret.dm_id = dm_id;
+ ret.addr = x1;
+ ret.op = x2;
+ ret.value = x3;
+ ret.access_width = x4;
+ ret.cpu_id = x5;
+ ret.vcpu_id = x6;
+
+ return ret;
+}
+
+#endif /* __ASM_ARM_BAO_H */
diff --git a/arch/arm64/include/asm/bao.h b/arch/arm64/include/asm/bao.h
new file mode 100644
index 000000000..4a42c0bce
--- /dev/null
+++ b/arch/arm64/include/asm/bao.h
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hypercall for Bao Hypervisor on ARM64
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ * João Peixoto <[email protected]>
+ */
+
+#ifndef __ASM_ARM64_BAO_H
+#define __ASM_ARM64_BAO_H
+
+#include <asm/bao.h>
+#include <linux/bao.h>
+#include <linux/arm-smccc.h>
+
+/**
+ * asm_bao_hypercall_remio() - Performs a Remote I/O Hypercall
+ * @remio_hc_id: VirtIO Hypercall ID
+ * @dm_id: Device Model ID
+ * @addr: Access address
+ * @op: Write, Read, Ask or Notify operation
+ * @value: Value to write or read
+ * @cpu_id: CPU ID
+ * @vcpu_id: VCPU ID
+ *
+ * @return: The VirtIO request structure
+ */
+static inline struct bao_virtio_request
+asm_bao_hypercall_remio(u64 remio_hc_id, u64 dm_id, u64 addr, u64 op,
+ u64 value, u64 cpu_id, u64 vcpu_id)
+{
+ register int x0 asm("x0") =
+ ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, ARM_SMCCC_SMC_64,
+ ARM_SMCCC_OWNER_VENDOR_HYP, remio_hc_id);
+ register u64 x1 asm("x1") = dm_id;
+ register u64 x2 asm("x2") = addr;
+ register u64 x3 asm("x3") = op;
+ register u64 x4 asm("x4") = value;
+ register u64 x5 asm("x5") = cpu_id;
+ register u64 x6 asm("x6") = vcpu_id;
+
+ struct bao_virtio_request ret;
+
+ asm volatile("hvc 0\n\t"
+ : "=r"(x0), "=r"(x1), "=r"(x2), "=r"(x3), "=r"(x4),
+ "=r"(x5), "=r"(x6)
+ : "r"(x0), "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5),
+ "r"(x6)
+ : "memory");
+
+ ret.ret = x0;
+ ret.dm_id = dm_id;
+ ret.addr = x1;
+ ret.op = x2;
+ ret.value = x3;
+ ret.access_width = x4;
+ ret.cpu_id = x5;
+ ret.vcpu_id = x6;
+
+ return ret;
+}
+
+#endif /* __ASM_ARM64_BAO_H */
diff --git a/arch/riscv/include/asm/bao.h b/arch/riscv/include/asm/bao.h
new file mode 100644
index 000000000..f2492acc7
--- /dev/null
+++ b/arch/riscv/include/asm/bao.h
@@ -0,0 +1,63 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Hypercall for Bao Hypervisor on RISC-V
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ * João Peixoto <[email protected]>
+ */
+
+#ifndef __ASM_RISCV_BAO_H
+#define __ASM_RISCV_BAO_H
+
+#include <asm/sbi.h>
+#include <linux/bao.h>
+
+/**
+ * asm_bao_hypercall_remio() - Performs a Remote I/O Hypercall
+ * @remio_hc_id: VirtIO Hypercall ID
+ * @dm_id: Device Model ID
+ * @addr: Access address
+ * @op: Write, Read, Ask or Notify operation
+ * @value: Value to write or read
+ * @cpu_id: CPU ID
+ * @vcpu_id: VCPU ID
+ *
+ * @return: The VirtIO request structure
+ */
+static inline struct bao_virtio_request
+asm_bao_hypercall_remio(u64 remio_hc_id, u64 dm_id, u64 addr, u64 op,
+ u64 value, u64 cpu_id, u64 vcpu_id)
+{
+ struct bao_virtio_request ret;
+
+ register uintptr_t a0 asm("a0") = (uintptr_t)(dm_id);
+ register uintptr_t a1 asm("a1") = (uintptr_t)(addr);
+ register uintptr_t a2 asm("a2") = (uintptr_t)(op);
+ register uintptr_t a3 asm("a3") = (uintptr_t)(value);
+ register uintptr_t a4 asm("a4") = (uintptr_t)(cpu_id);
+ register uintptr_t a5 asm("a5") = (uintptr_t)(vcpu_id);
+ register uintptr_t a6 asm("a6") = (uintptr_t)(remio_hc_id);
+ register uintptr_t a7 asm("a7") = (uintptr_t)(0x08000ba0);
+
+ asm volatile("ecall"
+ : "+r"(a0), "+r"(a1), "+r"(a2), "+r"(a3), "+r"(a4),
+ "+r"(a5), "+r"(a6)
+ : "r"(a0), "r"(a1), "r"(a2), "r"(a3), "r"(a4), "r"(a5),
+ "r"(a6), "r"(a7)
+ : "memory");
+
+ ret.ret = a0;
+ ret.dm_id = dm_id;
+ ret.addr = a1;
+ ret.op = a2;
+ ret.value = a3;
+ ret.access_width = a4;
+ ret.cpu_id = a5;
+ ret.vcpu_id = a6;
+
+ return ret;
+}
+
+#endif /* __ASM_RISCV_BAO_H */
diff --git a/drivers/Kconfig b/drivers/Kconfig
index 7bdad836f..f6906c8f7 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -147,6 +147,8 @@ source "drivers/hv/Kconfig"
source "drivers/xen/Kconfig"
+source "drivers/bao/Kconfig"
+
source "drivers/greybus/Kconfig"
source "drivers/comedi/Kconfig"
diff --git a/drivers/Makefile b/drivers/Makefile
index fe9ceb0d2..986b0db67 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -160,6 +160,7 @@ obj-$(CONFIG_SOUNDWIRE) += soundwire/
obj-$(CONFIG_VIRT_DRIVERS) += virt/
obj-$(subst m,y,$(CONFIG_HYPERV)) += hv/
+obj-$(CONFIG_BAO_SHMEM) += bao/
obj-$(CONFIG_PM_DEVFREQ) += devfreq/
obj-$(CONFIG_EXTCON) += extcon/
obj-$(CONFIG_MEMORY) += memory/
diff --git a/drivers/bao/Kconfig b/drivers/bao/Kconfig
new file mode 100644
index 000000000..c99d163eb
--- /dev/null
+++ b/drivers/bao/Kconfig
@@ -0,0 +1,5 @@
+config BAO_SHMEM
+ tristate "Bao shared memory support"
+
+ help
+ This implements an interface to communicate with bao hosted guests.
\ No newline at end of file
diff --git a/drivers/bao/Makefile b/drivers/bao/Makefile
new file mode 100644
index 000000000..d35c18fd3
--- /dev/null
+++ b/drivers/bao/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_BAO_SHMEM) += bao.o
+bao-objs += bao-ipcshmem.o
\ No newline at end of file
diff --git a/drivers/bao/bao-ipcshmem.c b/drivers/bao/bao-ipcshmem.c
new file mode 100644
index 000000000..475ac5b1c
--- /dev/null
+++ b/drivers/bao/bao-ipcshmem.c
@@ -0,0 +1,332 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Bao Hypervisor IPC Through Shared-memory Sample Driver
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ * David Cerdeira and José Martins
+ */
+
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/uaccess.h>
+#include <linux/fs.h>
+#include <linux/io.h>
+#include <linux/ioport.h>
+#include <linux/interrupt.h>
+#include <linux/of.h>
+#include <asm/io.h>
+#include <linux/mutex.h>
+#include <linux/poll.h>
+#include <linux/platform_device.h>
+#include <linux/ioctl.h>
+#include <linux/cdev.h>
+#include <linux/device.h>
+#include <linux/spinlock.h>
+#include <linux/mutex.h>
+#include <linux/wait.h>
+#include <linux/mm.h>
+
+#if defined(CONFIG_ARM64) || defined(CONFIG_ARM)
+#include <linux/arm-smccc.h>
+#include <asm/memory.h>
+#elif CONFIG_RISCV
+#include <asm/sbi.h>
+#endif
+
+#define DEV_NAME "baoipc"
+#define MAX_DEVICES 16
+#define NAME_LEN 32
+
+static dev_t bao_ipcshmem_devt;
+struct class *cl;
+
+struct bao_ipcshmem
+{
+ struct cdev cdev;
+ struct device *dev;
+
+ int id;
+ char label[NAME_LEN];
+ void* read_base;
+ size_t read_size;
+ void* write_base;
+ size_t write_size;
+ void* physical_base;
+ size_t shmem_size;
+};
+
+#ifdef CONFIG_ARM64
+static uint64_t bao_ipcshmem_notify(struct bao_ipcshmem *dev) {
+ register uint64_t x0 asm("x0") = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,
+ ARM_SMCCC_SMC_64, ARM_SMCCC_OWNER_VENDOR_HYP, 1);
+ register uint64_t x1 asm("x1") = dev->id;
+ register uint64_t x2 asm("x2") = 0;
+
+ asm volatile(
+ "hvc 0\t\n"
+ : "=r"(x0)
+ : "r"(x0), "r"(x1), "r"(x2)
+ );
+
+ return x0;
+}
+#elif CONFIG_ARM
+static uint32_t bao_ipcshmem_notify(struct bao_ipcshmem *dev) {
+ register uint32_t r0 asm("r0") = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL,
+ ARM_SMCCC_SMC_32, ARM_SMCCC_OWNER_VENDOR_HYP, 1);
+ register uint32_t r1 asm("r1") = dev->id;
+ register uint32_t r2 asm("r2") = 0;
+
+ asm volatile(
+ "hvc #0\t\n"
+ : "=r"(r0)
+ : "r"(r0), "r"(r1), "r"(r2)
+ );
+
+ return r0;
+}
+#elif CONFIG_RISCV
+static uint64_t bao_ipcshmem_notify(struct bao_ipcshmem *dev) {
+
+ struct sbiret ret =
+ sbi_ecall(0x08000ba0, 1, dev->id, 0, 0, 0, 0, 0);
+
+ return ret.error;
+}
+#endif
+
+static int bao_ipcshmem_mmap_fops(struct file *filp, struct vm_area_struct *vma)
+{
+ struct bao_ipcshmem *bao = filp->private_data;
+
+ unsigned long vsize = vma->vm_end - vma->vm_start;
+
+ if (remap_pfn_range(vma, vma->vm_start,
+ (unsigned long)bao->physical_base >> PAGE_SHIFT, vsize,
+ vma->vm_page_prot)) {
+ return -EFAULT;
+ }
+
+ return 0;
+}
+
+static ssize_t bao_ipcshmem_read_fops(struct file *filp,
+ char *buf, size_t count, loff_t *ppos)
+{
+ struct bao_ipcshmem *bao_ipcshmem = filp->private_data;
+ unsigned long missing = 0;
+ size_t len = 0;
+
+ len = strnlen(bao_ipcshmem->read_base, bao_ipcshmem->read_size);
+
+ if (*ppos >= len) return 0;
+ if ((len - *ppos) < count) count = len - *ppos;
+
+ missing =
+ copy_to_user(buf, bao_ipcshmem->read_base + *ppos, count);
+ if(missing != 0) count = count - missing;
+ *ppos += count;
+
+ return count;
+}
+
+static ssize_t bao_ipcshmem_write_fops(struct file *filp,
+ const char *buf, size_t count, loff_t *ppos)
+{
+ struct bao_ipcshmem *bao_ipcshmem = filp->private_data;
+ unsigned long missing = 0;
+
+ if (*ppos >= bao_ipcshmem->write_size)
+ return 0;
+ if(count > bao_ipcshmem->write_size)
+ count = bao_ipcshmem->write_size;
+ if((*ppos + count) > bao_ipcshmem->write_size)
+ count = bao_ipcshmem->write_size - *ppos;
+
+ missing =
+ copy_from_user(bao_ipcshmem->write_base + *ppos, buf, count);
+ if (missing != 0) count = count - missing;
+ *ppos += count;
+
+ bao_ipcshmem_notify(bao_ipcshmem);
+
+ return count;
+}
+
+static int bao_ipcshmem_open_fops(struct inode *inode, struct file *filp)
+{
+ struct bao_ipcshmem *bao_ipcshmem = container_of(inode->i_cdev,
+ struct bao_ipcshmem, cdev);
+ filp->private_data = bao_ipcshmem;
+
+ kobject_get(&bao_ipcshmem->dev->kobj);
+
+ return 0;
+}
+
+static int bao_ipcshmem_release_fops(struct inode *inode, struct file *filp)
+{
+ struct bao_ipcshmem *bao_ipcshmem = container_of(inode->i_cdev,
+ struct bao_ipcshmem, cdev);
+ filp->private_data = NULL;
+
+ kobject_put(&bao_ipcshmem->dev->kobj);
+
+ return 0;
+}
+
+static struct file_operations bao_ipcshmem_fops = {
+ .owner = THIS_MODULE,
+ .read = bao_ipcshmem_read_fops,
+ .write = bao_ipcshmem_write_fops,
+ .mmap = bao_ipcshmem_mmap_fops,
+ .open = bao_ipcshmem_open_fops,
+ .release = bao_ipcshmem_release_fops
+};
+
+int bao_ipcshmem_register(struct platform_device *pdev)
+{
+ int ret = 0;
+ struct device *dev = &(pdev->dev);
+ struct device_node *np = dev->of_node;
+ struct module *owner = THIS_MODULE;
+ struct resource *r;
+ dev_t devt;
+ resource_size_t shmem_size;
+ u32 write_offset, read_offset, write_size, read_size;
+ bool rd_in_range, wr_in_range, disjoint;
+ void* shmem_base_addr = NULL;
+ int id = -1;
+ struct bao_ipcshmem *bao;
+
+ r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+ if(r == NULL)
+ return -EINVAL;
+ of_property_read_u32_index(np, "read-channel", 0, &read_offset);
+ of_property_read_u32_index(np, "read-channel", 1, &read_size);
+ of_property_read_u32_index(np, "write-channel", 0, &write_offset);
+ of_property_read_u32_index(np, "write-channel", 1, &write_size);
+
+ rd_in_range = (r->start + read_offset + read_size) < r->end;
+ wr_in_range = (r->start + write_offset + write_size) < r->end;
+ disjoint = ((read_offset + read_size) <= write_offset) ||
+ ((write_offset + write_size) <= read_offset);
+
+ if(!rd_in_range || !wr_in_range || !disjoint) {
+ dev_err(&pdev->dev,"invalid channel layout\n");
+ dev_err(&pdev->dev,"rd_in_range = %d, wr_in_range = %d, disjoint = %d\n",
+ rd_in_range, wr_in_range, disjoint);
+ return -EINVAL;
+ }
+
+ shmem_size = r->end - r->start + 1;
+ shmem_base_addr = memremap(r->start, shmem_size, MEMREMAP_WB);
+ if(shmem_base_addr == NULL)
+ return -ENOMEM;
+
+ of_property_read_u32(np, "id", &id);
+ if (id >= MAX_DEVICES) {
+ dev_err(&pdev->dev,"invalid id %d\n", id);
+ ret = -EINVAL;
+ goto err_unmap;
+ }
+
+ bao = devm_kzalloc(&pdev->dev, sizeof(struct bao_ipcshmem), GFP_KERNEL);
+ if(bao == NULL) {
+ ret = -ENOMEM;
+ goto err_unmap;
+ }
+ snprintf(bao->label, NAME_LEN, "%s%d", DEV_NAME, id);
+ bao->id = id;
+ bao->read_size = read_size;
+ bao->write_size = write_size;
+ bao->read_base = shmem_base_addr + read_offset;
+ bao->write_base = shmem_base_addr + write_offset;
+ bao->physical_base = (void *)r->start;
+ bao->shmem_size = shmem_size;
+
+ cdev_init(&bao->cdev, &bao_ipcshmem_fops);
+ bao->cdev.owner = owner;
+
+ devt = MKDEV(MAJOR(bao_ipcshmem_devt), id);
+ ret = cdev_add(&bao->cdev, devt, 1);
+ if (ret) {
+ goto err_unmap;
+ }
+
+ bao->dev = device_create(cl, &pdev->dev, devt, bao, bao->label);
+ if (IS_ERR(bao->dev)) {
+ ret = PTR_ERR(bao->dev);
+ goto err_cdev;
+ }
+ dev_set_drvdata(bao->dev, bao);
+
+ return 0;
+
+err_cdev:
+ cdev_del(&bao->cdev);
+err_unmap:
+ memunmap(shmem_base_addr);
+
+ dev_err(&pdev->dev,"failed initialization\n");
+ return ret;
+}
+
+static void bao_ipcshmem_unregister(struct platform_device *pdev)
+{
+ /* TODO */
+ return;
+}
+
+static const struct of_device_id of_bao_ipcshmem_match[] = {
+ {
+ .compatible = "bao,ipcshmem",
+ },
+ {/* sentinel */}};
+MODULE_DEVICE_TABLE(of, of_bao_ipcshmem_match);
+
+static struct platform_driver bao_ipcshmem_driver = {
+ .probe = bao_ipcshmem_register,
+ .remove = bao_ipcshmem_unregister,
+ .driver = {
+ .name = DEV_NAME,
+ .of_match_table = of_bao_ipcshmem_match,
+ },
+};
+
+static int __init bao_ipcshmem_init(void)
+{
+ int ret;
+
+ if ((cl = class_create(DEV_NAME)) == NULL) {
+ ret = -1;
+ pr_err("unable to class_create " DEV_NAME " device\n");
+ return ret;
+ }
+
+ ret = alloc_chrdev_region(&bao_ipcshmem_devt, 0, MAX_DEVICES, DEV_NAME);
+ if (ret < 0) {
+ pr_err("unable to alloc_chrdev_region " DEV_NAME " device\n");
+ return ret;
+ }
+
+ return platform_driver_register(&bao_ipcshmem_driver);
+}
+
+static void __exit bao_ipcshmem_exit(void)
+{
+ platform_driver_unregister(&bao_ipcshmem_driver);
+ unregister_chrdev(bao_ipcshmem_devt, DEV_NAME);
+ class_destroy(cl);
+}
+
+module_init(bao_ipcshmem_init);
+module_exit(bao_ipcshmem_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("David Cerdeira");
+MODULE_AUTHOR("José Martins");
+MODULE_DESCRIPTION("bao ipc through shared-memory sample driver");
\ No newline at end of file
diff --git a/drivers/virt/Kconfig b/drivers/virt/Kconfig
index d8c848cf0..33f9f4243 100644
--- a/drivers/virt/Kconfig
+++ b/drivers/virt/Kconfig
@@ -41,6 +41,8 @@ config FSL_HV_MANAGER
4) A kernel interface for receiving callbacks when a managed
partition shuts down.
+source "drivers/virt/bao/Kconfig"
+
source "drivers/virt/vboxguest/Kconfig"
source "drivers/virt/nitro_enclaves/Kconfig"
diff --git a/drivers/virt/Makefile b/drivers/virt/Makefile
index f29901bd7..f6733a98e 100644
--- a/drivers/virt/Makefile
+++ b/drivers/virt/Makefile
@@ -3,6 +3,7 @@
# Makefile for drivers that support virtualization
#
+obj-$(CONFIG_BAO_IO_DISPATCHER) += bao/
obj-$(CONFIG_FSL_HV_MANAGER) += fsl_hypervisor.o
obj-$(CONFIG_VMGENID) += vmgenid.o
obj-y += vboxguest/
diff --git a/drivers/virt/bao/Kconfig b/drivers/virt/bao/Kconfig
new file mode 100644
index 000000000..b7caa3786
--- /dev/null
+++ b/drivers/virt/bao/Kconfig
@@ -0,0 +1,47 @@
+# SPDX-License-Identifier: GPL-2.0
+config BAO_IO_DISPATCHER
+ tristate "Bao Hypervisor I/O Dispatcher"
+ help
+ The Bao I/O Dispatcher system is a kernel module that can be loaded
+ into the Linux kernel of the backend VMs. It is responsible for
+ establishing the connection between the Remote I/O system and the
+ Frontend Device Model, offering a unified API to support various
+ VirtIO backends.
+
+ To compile as a module, choose M, the module will be called
+ bao_io_dispatcher. If unsure, say N.
+
+choice
+ prompt "Bao Hypervisor I/O Dispatcher Mode"
+ default BAO_IO_DISPATCHER_INTERRUPT_MODE
+ depends on BAO_IO_DISPATCHER
+ help
+ Choose between interrupt mode and pooling mode.
+
+config BAO_IO_DISPATCHER_INTERRUPT_MODE
+ bool "Interrupt Mode"
+ help
+ Say Y here to enable the I/O Dispatcher System Interrupt Mode.
+
+ If you say N, the I/O Dispatcher System will dispatch the I/O requests
+ on a periodic form, resulting in a potential degradation of
+ performance.
+
+config BAO_IO_DISPATCHER_POOLING_MODE
+ bool "Pooling Mode"
+ help
+ Say Y here to enable the I/O Dispatcher System Pooling Mode.
+
+ If you say N, the I/O Dispatcher System will dispatch the I/O requests
+ though interrupts, resulting in a potential improvement of
+ performance.
+
+endchoice
+
+config BAO_IO_DISPATCHER_POOLING_INTERVAL
+ int "Interval in nanoseconds"
+ default 1000000
+ depends on BAO_IO_DISPATCHER
+ depends on BAO_IO_DISPATCHER_POOLING_MODE
+ help
+ Set the interval in nanoseconds for the pooling mode. Adjust this value according to your needs.
\ No newline at end of file
diff --git a/drivers/virt/bao/Makefile b/drivers/virt/bao/Makefile
new file mode 100644
index 000000000..caeef8fdd
--- /dev/null
+++ b/drivers/virt/bao/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0
+obj-$(CONFIG_BAO_IO_DISPATCHER) := bao.o
+bao-y := ioctls.o ioeventfd.o io_client.o io_dispatcher.o irqfd.o dm.o intc.o driver.o
\ No newline at end of file
diff --git a/drivers/virt/bao/bao_drv.h b/drivers/virt/bao/bao_drv.h
new file mode 100644
index 000000000..4f78965df
--- /dev/null
+++ b/drivers/virt/bao/bao_drv.h
@@ -0,0 +1,369 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Provides some definitions for the Bao Hypervisor modules
+ *
+ * Copyright (c) Bao Project and Contributors. All rights reserved.
+ *
+ * Authors:
+ * João Peixoto <[email protected]>
+ */
+
+#ifndef __BAO_DRV_H
+#define __BAO_DRV_H
+
+#include <linux/bao.h>
+#include <linux/types.h>
+#include <linux/spinlock.h>
+#include <linux/wait.h>
+#include <linux/mutex.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/interrupt.h>
+#include <linux/cdev.h>
+#include <linux/device.h>
+
+#define BAO_IOEVENTFD_FLAG_DATAMATCH (1 << 1)
+#define BAO_IOEVENTFD_FLAG_DEASSIGN (1 << 2)
+#define BAO_IRQFD_FLAG_DEASSIGN 1U
+
+#define BAO_IO_CLIENT_DESTROYING 0U
+
+#define BAO_DM_FLAG_DESTROYING 0U
+#define BAO_DM_FLAG_CLEARING_IOREQ 1U
+
+struct bao_dm;
+struct bao_io_client;
+
+typedef int (*bao_io_client_handler_t)(struct bao_io_client *client,
+ struct bao_virtio_request *req);
+
+/**
+ * Bao I/O client
+ * @name: Client name
+ * @dm: The DM that the client belongs to
+ * @list: List node for this bao_io_client
+ * @is_control: If this client is the control client
+ * @flags: Flags (BAO_IO_CLIENT_*)
+ * @virtio_requests: Array of all I/O requests that are free to process
+ * @virtio_requests_lock: Lock to protect virtio_requests list
+ * @range_list: I/O ranges
+ * @range_lock: Semaphore to protect range_list
+ * @handler: I/O requests handler of this client
+ * @thread: The thread which executes the handler
+ * @wq: The wait queue for the handler thread parking
+ * @priv: Data for the thread
+ */
+struct bao_io_client {
+ char name[BAO_NAME_MAX_LEN];
+ struct bao_dm *dm;
+ struct list_head list;
+ bool is_control;
+ unsigned long flags;
+ struct list_head virtio_requests;
+ struct mutex virtio_requests_lock;
+ struct list_head range_list;
+ struct rw_semaphore range_lock;
+ bao_io_client_handler_t handler;
+ struct task_struct *thread;
+ wait_queue_head_t wq;
+ void *priv;
+};
+
+/**
+ * Bao backend device model (DM)
+ * @list: Entry within global list of all DMs
+ * @info: DM information (id, shmem_addr, shmem_size, irq, fd)
+ * @shmem_base_addr: The base address of the shared memory (only used for unmapping purposes)
+ * @flags: Flags (BAO_IO_DISPATCHER_DM_*)
+ * @ioeventfds: List to link all bao_ioeventfd
+ * @ioeventfds_lock: Lock to protect ioeventfds list
+ * @ioeventfd_client: Ioevenfd client
+ * @irqfds: List to link all bao_irqfd
+ * @irqfds_lock: Lock to protect irqfds list
+ * @irqfd_server: Irqfd server
+ * @io_clients_lock: Semaphore to protect io_clients
+ * @io_clients: List to link all bao_io_client
+ * @control_client: Control client
+ */
+struct bao_dm {
+ struct list_head list;
+ struct bao_dm_info info;
+ void *shmem_base_addr;
+ unsigned long flags;
+ struct list_head ioeventfds;
+ struct mutex ioeventfds_lock;
+ struct bao_io_client *ioeventfd_client;
+ struct list_head irqfds;
+ struct mutex irqfds_lock;
+ struct workqueue_struct *irqfd_server;
+ struct rw_semaphore io_clients_lock;
+ struct list_head io_clients;
+ struct bao_io_client *control_client;
+};
+
+/**
+ * Bao I/O request range
+ * @list: List node for this range
+ * @start: The start address of the range
+ * @end: The end address of the range
+ *
+ */
+struct bao_io_range {
+ struct list_head list;
+ u64 start;
+ u64 end;
+};
+
+extern struct list_head bao_dm_list;
+extern rwlock_t bao_dm_list_lock;
+
+/************************************************************************************************************/
+/* Backend Device Model (DM) API */
+/************************************************************************************************************/
+
+/**
+ * Create the backend DM
+ * @info: The DM information (id, shmem_addr, shmem_size, irq, fd)
+ * @return dm on success, NULL on error
+ */
+struct bao_dm* bao_dm_create(struct bao_dm_info *info);
+
+/**
+ * Destroy the backend DM
+ * @dm: The DM to be destroyed
+ */
+void bao_dm_destroy(struct bao_dm *dm);
+
+/**
+ * Get the DM information
+ * @info: The DM information to be filled (id field contains the DM ID)
+ * @return true on success, false on error
+ */
+bool bao_dm_get_info(struct bao_dm_info *info);
+
+/**
+ * DM ioctls handler
+ * @filp: The open file pointer
+ * @cmd: The ioctl command
+ * @ioctl_param: The ioctl parameter
+ */
+long bao_dm_ioctl(struct file *filp, unsigned int cmd,
+ unsigned long ioctl_param);
+
+/************************************************************************************************************/
+/* I/O Clients API */
+/************************************************************************************************************/
+
+/**
+ * Create an I/O client
+ * @dm: The DM that this client belongs to
+ * @handler: The I/O client handler for the I/O requests
+ * @data: Private data for the handler
+ * @is_control: If it is the control client
+ * @name: The name of I/O client
+ */
+struct bao_io_client *
+bao_io_client_create(struct bao_dm *dm, bao_io_client_handler_t handler,
+ void *data, bool is_control, const char *name);
+
+/**
+ * Destroy the I/O clients of the DM
+ * @dm: The DM that the I/O clients belong to
+ */
+void bao_io_clients_destroy(struct bao_dm *dm);
+
+/**
+ * Attach the thread to the I/O client to wait for I/O requests
+ * @client: The I/O client to handle the I/O request
+ */
+int bao_io_client_attach(struct bao_io_client *client);
+
+/**
+ * Add an I/O range monitor into an I/O client
+ * @client: The I/O client that the range will be added
+ * @start: The start address of the range
+ * @end: The end address of the range
+ */
+int bao_io_client_range_add(struct bao_io_client *client, u64 start,
+ u64 end);
+
+/**
+ * Delete an I/O range monitor from an I/O client
+ * @client: The I/O client that the range will be deleted
+ * @start: The start address of the range
+ * @end: The end address of the range
+ */
+void bao_io_client_range_del(struct bao_io_client *client, u64 start,
+ u64 end);
+
+/**
+ * Retrieve the oldest I/O request from the I/O client
+ * @client: The I/O client
+ * @req: The virtio request to be retrieved
+ */
+int bao_io_client_request(struct bao_io_client *client,
+ struct bao_virtio_request *req);
+
+/**
+ * Push an I/O request into the I/O client request list
+ * @client: The I/O Client that the I/O request belongs to
+ * @req: The I/O request to be pushed
+ */
+void bao_io_client_push_request(struct bao_io_client *client,
+ struct bao_virtio_request *req);
+
+/**
+ * Pop an I/O request from the I/O client request list
+ * @client: The I/O client that the I/O request belongs to
+ * @return The I/O request
+ */
+struct bao_virtio_request bao_io_client_pop_request(struct bao_io_client *client);
+
+/**
+ * Find the I/O client that the I/O request belongs to
+ * @dm: The DM that the I/O request belongs to
+ * @req: The I/O request
+ * @return The I/O client that the I/O request belongs to, or NULL if there is no client
+ */
+struct bao_io_client *bao_io_client_find(struct bao_dm *dm, struct bao_virtio_request *req);
+
+/************************************************************************************************************/
+/* Ioeventfd Client API */
+/************************************************************************************************************/
+
+/**
+ * Initialize the Ioeventfd client
+ * @dm: The DM that the Ioeventfd client belongs to
+ */
+int bao_ioeventfd_client_init(struct bao_dm *dm);
+
+/**
+ * Destroy the Ioeventfd client
+ * @dm: The DM that the Ioeventfd client belongs to
+ */
+void bao_ioeventfd_client_destroy(struct bao_dm *dm);
+
+/**
+ * Configure the Ioeventfd client
+ * @dm: The DM that the Ioeventfd client belongs to
+ * @config: The ioeventfd configuration
+ */
+int bao_ioeventfd_client_config(struct bao_dm *dm,
+ struct bao_ioeventfd *config);
+
+/************************************************************************************************************/
+/* Irqfd Server API */
+/************************************************************************************************************/
+
+/**
+ * Initialize the Irqfd server
+ * @dm: The DM that the Irqfd server belongs to
+ */
+int bao_irqfd_server_init(struct bao_dm *dm);
+
+/**
+ * Destroy the Irqfd server
+ * @dm: The DM that the Irqfd server belongs to
+ */
+void bao_irqfd_server_destroy(struct bao_dm *dm);
+
+/**
+ * Configure the Irqfd server
+ * @dm: The DM that the Irqfd server belongs to
+ * @config: The irqfd configuration
+ */
+int bao_irqfd_server_config(struct bao_dm *dm, struct bao_irqfd *config);
+
+/************************************************************************************************************/
+/* I/O Dispatcher API */
+/************************************************************************************************************/
+
+/**
+ * Initialize the I/O Dispatcher
+ * @dm: The DM to be initialized on the I/O Dispatcher
+ */
+int bao_io_dispatcher_init(struct bao_dm *dm);
+
+/**
+ * Destroy the I/O Dispatcher
+ * @dm: The DM to be destroyed on the I/O Dispatcher
+ */
+void bao_io_dispatcher_destroy(struct bao_dm *dm);
+
+/**
+ * Setup the I/O Dispatcher
+ */
+int bao_io_dispatcher_setup(void);