-
Notifications
You must be signed in to change notification settings - Fork 0
/
0001-ivshmem-driver.patch
775 lines (771 loc) · 24 KB
/
0001-ivshmem-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
From a39e97196a7da7d5885e738388066d4fabb3d871 Mon Sep 17 00:00:00 2001
From: Jaroslaw Kurowski <[email protected]>
Date: Wed, 17 Jul 2024 12:19:48 +0400
Subject: ivshmem driver
---
drivers/char/Kconfig | 2 +
drivers/char/Makefile | 1 +
drivers/char/ivshmem/Kconfig | 6 +
drivers/char/ivshmem/Makefile | 1 +
drivers/char/ivshmem/kvm_ivshmem.c | 678 +++++++++++++++++++++++++++++
drivers/char/ivshmem/kvm_ivshmem.h | 25 ++
6 files changed, 713 insertions(+)
create mode 100644 drivers/char/ivshmem/Kconfig
create mode 100644 drivers/char/ivshmem/Makefile
create mode 100644 drivers/char/ivshmem/kvm_ivshmem.c
create mode 100644 drivers/char/ivshmem/kvm_ivshmem.h
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
index 801d6c83f..e93d4265a 100644
--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -421,4 +421,6 @@ config ADI
and SSM (Silicon Secured Memory). Intended consumers of this
driver include crash and makedumpfile.
+source "drivers/char/ivshmem/Kconfig"
+
endmenu
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index c5f532e41..fc17a75f3 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -44,3 +44,4 @@ obj-$(CONFIG_PS3_FLASH) += ps3flash.o
obj-$(CONFIG_XILLYBUS_CLASS) += xillybus/
obj-$(CONFIG_POWERNV_OP_PANEL) += powernv-op-panel.o
obj-$(CONFIG_ADI) += adi.o
+obj-y += ivshmem/
diff --git a/drivers/char/ivshmem/Kconfig b/drivers/char/ivshmem/Kconfig
new file mode 100644
index 000000000..f0f75200c
--- /dev/null
+++ b/drivers/char/ivshmem/Kconfig
@@ -0,0 +1,6 @@
+# Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
+# SPDX-License-Identifier: Apache-2.0
+
+config KVM_IVSHMEM_VM_COUNT
+ int "Application VMs count"
+ default 5
diff --git a/drivers/char/ivshmem/Makefile b/drivers/char/ivshmem/Makefile
new file mode 100644
index 000000000..0a850b05e
--- /dev/null
+++ b/drivers/char/ivshmem/Makefile
@@ -0,0 +1 @@
+obj-y += kvm_ivshmem.o
diff --git a/drivers/char/ivshmem/kvm_ivshmem.c b/drivers/char/ivshmem/kvm_ivshmem.c
new file mode 100644
index 000000000..96ca0c71c
--- /dev/null
+++ b/drivers/char/ivshmem/kvm_ivshmem.c
@@ -0,0 +1,678 @@
+/* drivers/char/kvm_ivshmem.c - driver for KVM Inter-VM shared memory PCI device
+
+* Copyright 2022-2024 TII (SSRC) and the Ghaf contributors
+* SPDX-License-Identifier: Apache-2.0
+*
+* Copyright 2009 Cam Macdonell <[email protected]>
+*
+* Based on cirrusfb.c and 8139cp.c:
+* Copyright 1999-2001 Jeff Garzik
+* Copyright 2001-2004 Jeff Garzik
+*
+*/
+
+#include "kvm_ivshmem.h"
+#include <linux/fcntl.h>
+#include <linux/file.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>
+#include <linux/kernel.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/pci.h>
+#include <linux/poll.h>
+#include <linux/proc_fs.h>
+#include <linux/uio.h>
+#include <linux/version.h>
+
+#ifndef CONFIG_KVM_IVSHMEM_VM_COUNT
+#warning CONFIG_KVM_IVSHMEM_VM_COUNT not defined. Assuming 5.
+#define CONFIG_KVM_IVSHMEM_VM_COUNT (5)
+#endif
+
+DEFINE_SPINLOCK(rawhide_irq_lock);
+#define VM_COUNT (CONFIG_KVM_IVSHMEM_VM_COUNT)
+#define VECTORS_COUNT (2 * VM_COUNT)
+
+#undef DEBUG
+#ifdef DEBUG
+#define KVM_IVSHMEM_DPRINTK(fmt, ...) \
+ do { \
+ printk(KERN_INFO "KVM_IVSHMEM: " fmt "\n", ##__VA_ARGS__); \
+ } while (0)
+#else
+#define KVM_IVSHMEM_DPRINTK(fmt, ...) \
+ {}
+#endif
+
+enum {
+ /* KVM Inter-VM shared memory device register offsets */
+ IntrMask = 0x00, /* Interrupt Mask */
+ IntrStatus = 0x04, /* Interrupt Status */
+ IVPosition = 0x08, /* VM ID */
+ Doorbell = 0x0c, /* Doorbell */
+};
+
+typedef struct kvm_ivshmem_device {
+ void __iomem *regs;
+
+ void *base_addr;
+
+ uint64_t regaddr;
+ unsigned int reg_size;
+
+ uint64_t ioaddr;
+ unsigned int ioaddr_size;
+ unsigned int irq;
+
+ struct pci_dev *dev;
+ char (*msix_names)[256];
+ struct msix_entry *msix_entries;
+ int nvectors;
+} kvm_ivshmem_device;
+
+static int irq_incoming_data[VM_COUNT];
+static int irq_ack[VM_COUNT];
+static int local_resource_count[VM_COUNT];
+static int peer_resource_count[VM_COUNT];
+static wait_queue_head_t local_data_ready_wait_queue[VM_COUNT];
+static wait_queue_head_t peer_data_ready_wait_queue[VM_COUNT];
+
+static kvm_ivshmem_device kvm_ivshmem_dev;
+
+static long kvm_ivshmem_ioctl(struct file *, unsigned int, unsigned long);
+static int kvm_ivshmem_mmap(struct file *, struct vm_area_struct *);
+static int kvm_ivshmem_open(struct inode *, struct file *);
+static int kvm_ivshmem_release(struct inode *, struct file *);
+static ssize_t kvm_ivshmem_read(struct file *, char *, size_t, loff_t *);
+static ssize_t kvm_ivshmem_write(struct file *, const char *, size_t, loff_t *);
+static loff_t kvm_ivshmem_lseek(struct file *filp, loff_t offset, int origin);
+static unsigned kvm_ivshmem_poll(struct file *filp,
+ struct poll_table_struct *wait);
+static const struct file_operations kvm_ivshmem_ops = {
+ .owner = THIS_MODULE,
+ .open = kvm_ivshmem_open,
+ .mmap = kvm_ivshmem_mmap,
+ .read = kvm_ivshmem_read,
+ .unlocked_ioctl = kvm_ivshmem_ioctl,
+ .write = kvm_ivshmem_write,
+ .llseek = kvm_ivshmem_lseek,
+ .release = kvm_ivshmem_release,
+ .poll = kvm_ivshmem_poll,
+};
+
+static struct pci_device_id kvm_ivshmem_id_table[] = {
+ {0x1af4, 0x1110, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
+ {0},
+};
+
+static struct miscdevice kvm_ivshmem_misc_dev = {
+ .minor = MISC_DYNAMIC_MINOR,
+ .name = "ivshmem",
+ .fops = &kvm_ivshmem_ops,
+};
+
+static uint64_t flataddr = 0x0;
+module_param_named(flataddr, flataddr, ullong, S_IRUGO);
+
+// TODO: debug
+static int in_counter = 0, out_counter = 0;
+
+MODULE_DEVICE_TABLE(pci, kvm_ivshmem_id_table);
+
+static void kvm_ivshmem_remove_device(struct pci_dev *pdev);
+static int kvm_ivshmem_probe_device(struct pci_dev *pdev,
+ const struct pci_device_id *ent);
+
+static struct pci_driver kvm_ivshmem_pci_driver = {
+ .name = "kvm-shmem",
+ .id_table = kvm_ivshmem_id_table,
+ .probe = kvm_ivshmem_probe_device,
+ .remove = kvm_ivshmem_remove_device,
+};
+
+static long kvm_ivshmem_ioctl(struct file *filp, unsigned int cmd,
+ unsigned long arg) {
+
+
+ int rv = 0;
+ unsigned long flags;
+ uint32_t msg;
+ struct ioctl_data ioctl_data;
+
+ KVM_IVSHMEM_DPRINTK("%ld ioctl: cmd=0x%x args is 0x%lx",
+ (unsigned long int)filp->private_data, cmd, arg);
+ if ((unsigned long int)filp->private_data >= VM_COUNT &&
+ cmd != SHMEM_IOCSETINSTANCENO) {
+ printk(KERN_ERR "KVM_IVSHMEM: ioctl: invalid instance id %ld > VM_COUNT=%d",
+ (unsigned long int)filp->private_data, VM_COUNT);
+ return -EINVAL;
+ }
+ switch (cmd) {
+ case SHMEM_IOCIVPOSN:
+ msg = readl(kvm_ivshmem_dev.regs + IVPosition);
+ KVM_IVSHMEM_DPRINTK("%ld my vmid is 0x%08x",
+ (unsigned long int)filp->private_data, msg);
+ rv = copy_to_user((void __user *)arg, &msg, sizeof(msg));
+ break;
+
+ case SHMEM_IOCDORBELL:
+ unsigned int vec;
+
+ if (copy_from_user(&ioctl_data, (void __user *)arg, sizeof(ioctl_data))) {
+ printk(KERN_ERR
+ "KVM_IVSHMEM: SHMEM_IOCDORBELL: %ld invalid argument 0x%lx",
+ (unsigned long int)filp->private_data, arg);
+ rv = -EINVAL;
+ break;
+ }
+
+ vec = ioctl_data.int_no & 0xffff;
+#ifdef DEBUG_IOCTL
+ KVM_IVSHMEM_DPRINTK("%ld ioctl cmd=%d fd=%d len=%d int_no=0x%x",
+ (unsigned long int)filp->private_data, ioctl_data.cmd,
+ ioctl_data.fd, ioctl_data.len, ioctl_data.int_no);
+#endif
+ KVM_IVSHMEM_DPRINTK("%ld ringing doorbell id=0x%x on vector 0x%x",
+ (unsigned long int)filp->private_data,
+ (ioctl_data.int_no >> 16), vec);
+ spin_lock_irqsave(&rawhide_irq_lock, flags);
+ if (vec & LOCAL_RESOURCE_READY_INT_VEC) {
+ local_resource_count[(unsigned long int)filp->private_data] = 0;
+ } else {
+ peer_resource_count[(unsigned long int)filp->private_data] = 0;
+ }
+ spin_unlock_irqrestore(&rawhide_irq_lock, flags);
+ writel(ioctl_data.int_no, kvm_ivshmem_dev.regs + Doorbell);
+ break;
+
+ case SHMEM_IOCSET:
+ spin_lock_irqsave(&rawhide_irq_lock, flags);
+ if ((arg >> 8) == LOCAL_RESOURCE_READY_INT_VEC)
+ local_resource_count[(unsigned long int)filp->private_data] = arg & 0xff;
+ else if ((arg >> 8) == PEER_RESOURCE_CONSUMED_INT_VEC)
+ peer_resource_count[(unsigned long int)filp->private_data] = arg & 0xff;
+ else {
+ rv = -EINVAL;
+ printk(KERN_ERR "KVM_IVSHMEM: SHMEM_IOCSET: invalid arg %ld", arg);
+ }
+ spin_unlock_irqrestore(&rawhide_irq_lock, flags);
+ break;
+
+ case SHMEM_IOCSETINSTANCENO:
+ spin_lock_irqsave(&rawhide_irq_lock, flags);
+ if (arg >= VM_COUNT) {
+ printk(KERN_ERR "KVM_IVSHMEM: ioctl: invalid instance id %ld", arg);
+ rv = -EINVAL;
+ goto unlock;
+ }
+ filp->private_data = (void *)arg;
+ printk(KERN_INFO
+ "KVM_IVSHMEM: SHMEM_IOCSETINSTANCENO: set instance id 0x%lx",
+ arg);
+
+ init_waitqueue_head(&local_data_ready_wait_queue[arg]);
+ init_waitqueue_head(&peer_data_ready_wait_queue[arg]);
+ local_resource_count[arg] = 1;
+ peer_resource_count[arg] = 0;
+ unlock:
+ spin_unlock_irqrestore(&rawhide_irq_lock, flags);
+ break;
+
+ case SHMEM_IOCNOP:
+ unsigned int tmp;
+
+ if (copy_from_user(&tmp, (void __user *)arg, sizeof(tmp))) {
+ printk(KERN_ERR "KVM_IVSHMEM: SHMEM_IOCWLOCAL: %ld invalid argument",
+ (unsigned long int)filp->private_data);
+ }
+ KVM_IVSHMEM_DPRINTK(
+ "%ld %x local %d counter=%d: peer:%d counter=%d",
+ (unsigned long int)filp->private_data, tmp,
+ local_resource_count[(unsigned long int)filp->private_data], in_counter,
+ peer_resource_count[(unsigned long int)filp->private_data],
+ out_counter);
+
+ tmp = ((unsigned)out_counter) << 16 | (unsigned)(in_counter & 0xffff);
+ rv = copy_to_user((void __user *)arg, &tmp, sizeof(tmp));
+ break;
+
+ default:
+ KVM_IVSHMEM_DPRINTK("%ld bad ioctl (0x%08x)",
+ (unsigned long int)filp->private_data, cmd);
+ return -EINVAL;
+ }
+
+ return rv;
+}
+
+static unsigned kvm_ivshmem_poll(struct file *filp,
+ struct poll_table_struct *wait) {
+ __poll_t mask = 0;
+ __poll_t req_events = poll_requested_events(wait);
+
+ if (req_events & EPOLLIN) {
+ poll_wait(
+ filp,
+ &peer_data_ready_wait_queue[(unsigned long int)filp->private_data],
+ wait);
+
+ if (peer_resource_count[(unsigned long int)filp->private_data]) {
+ KVM_IVSHMEM_DPRINTK(
+ "%ld poll: in: peer_resource_count=%d",
+ (unsigned long int)filp->private_data,
+ peer_resource_count[(unsigned long int)filp->private_data]);
+ mask |= (POLLIN | POLLRDNORM);
+ }
+ }
+
+ if (req_events & EPOLLOUT) {
+ poll_wait(
+ filp,
+ &local_data_ready_wait_queue[(unsigned long int)filp->private_data],
+ wait);
+
+ if (local_resource_count[(unsigned long int)filp->private_data]) {
+ KVM_IVSHMEM_DPRINTK(
+ "%ld poll: out: local_resource_count=%d",
+ (unsigned long int)filp->private_data,
+ local_resource_count[(unsigned long int)filp->private_data]);
+
+ mask |= (POLLOUT | POLLWRNORM);
+ }
+ }
+
+#ifdef DEBUG
+ if (!mask) {
+ printk(KERN_ERR "KVM_IVSHMEM: %ld poll: timeout: query for events 0x%x",
+ (unsigned long int)filp->private_data, req_events);
+ }
+#endif
+ return mask;
+}
+
+static ssize_t kvm_ivshmem_read(struct file *filp, char *buffer, size_t len,
+ loff_t *poffset) {
+
+ int bytes_read = 0;
+ unsigned long offset;
+
+ offset = *poffset;
+
+ if (!kvm_ivshmem_dev.base_addr) {
+ printk(KERN_ERR "KVM_IVSHMEM: cannot read from ioaddr (NULL)");
+ return 0;
+ }
+
+ if (len > kvm_ivshmem_dev.ioaddr_size - offset) {
+ len = kvm_ivshmem_dev.ioaddr_size - offset;
+ }
+
+ if (len == 0)
+ return 0;
+
+ bytes_read = copy_to_user(buffer, (void*) kvm_ivshmem_dev.base_addr + offset, len);
+ if (bytes_read > 0) {
+ return -EFAULT;
+ }
+
+ *poffset += len;
+ return len;
+}
+
+static loff_t kvm_ivshmem_lseek(struct file *filp, loff_t offset, int origin) {
+
+ loff_t retval = -1;
+
+ switch (origin) {
+ case SEEK_CUR:
+ offset += filp->f_pos;
+ __attribute__((__fallthrough__));
+ case SEEK_SET:
+ retval = offset;
+ if (offset > kvm_ivshmem_dev.ioaddr_size) {
+ offset = kvm_ivshmem_dev.ioaddr_size;
+ }
+ filp->f_pos = offset;
+ break;
+ case SEEK_END:
+ retval = kvm_ivshmem_dev.ioaddr_size;
+ filp->f_pos = kvm_ivshmem_dev.ioaddr_size;
+ }
+
+ return retval;
+}
+
+static ssize_t kvm_ivshmem_write(struct file *filp, const char *buffer,
+ size_t len, loff_t *poffset) {
+
+ int bytes_written = 0;
+ unsigned long offset;
+
+ offset = *poffset;
+
+ KVM_IVSHMEM_DPRINTK("%ld KVM_IVSHMEM: trying to write",
+ (unsigned long int)filp->private_data);
+ if (!kvm_ivshmem_dev.base_addr) {
+ printk(KERN_ERR "KVM_IVSHMEM: %ld cannot write to ioaddr (NULL)",
+ (unsigned long int)filp->private_data);
+ return 0;
+ }
+
+ if (len > kvm_ivshmem_dev.ioaddr_size - offset) {
+ len = kvm_ivshmem_dev.ioaddr_size - offset;
+ }
+
+ KVM_IVSHMEM_DPRINTK("%ld KVM_IVSHMEM: len is %u",
+ (unsigned long int)filp->private_data, (unsigned)len);
+ if (len == 0)
+ return 0;
+
+ bytes_written =
+ copy_from_user((void*) kvm_ivshmem_dev.base_addr + offset, buffer, len);
+ if (bytes_written > 0) {
+ return -EFAULT;
+ }
+
+ KVM_IVSHMEM_DPRINTK("%ld KVM_IVSHMEM: wrote %u bytes at offset %lu",
+ (unsigned long int)filp->private_data, (unsigned)len,
+ offset);
+ *poffset += len;
+ return len;
+}
+
+static irqreturn_t kvm_ivshmem_interrupt(int irq, void *dev_instance) {
+ struct kvm_ivshmem_device *dev = dev_instance;
+ int i;
+
+ if (unlikely(dev == NULL)) {
+ KVM_IVSHMEM_DPRINTK("return IRQ_NONE");
+ return IRQ_NONE;
+ }
+
+ KVM_IVSHMEM_DPRINTK("irq %d", irq);
+ for (i = 0; i < VM_COUNT; i++) {
+ if (irq == irq_incoming_data[i]) {
+ out_counter++;
+ KVM_IVSHMEM_DPRINTK("%d wake up peer_data_ready_wait_queue count=%d", i,
+ out_counter);
+ if (peer_resource_count[i]) {
+ KVM_IVSHMEM_DPRINTK("%d WARNING: peer_resource_count>0!: %d", i,
+ peer_resource_count[i]);
+ }
+ spin_lock(&rawhide_irq_lock);
+ peer_resource_count[i] = 1;
+ spin_unlock(&rawhide_irq_lock);
+ wake_up_interruptible(&peer_data_ready_wait_queue[i]);
+ return IRQ_HANDLED;
+ }
+ if (irq == irq_ack[i]) {
+ in_counter++;
+ KVM_IVSHMEM_DPRINTK("%d wake up local_data_ready_wait_queue count=%d", i,
+ in_counter);
+ if (local_resource_count[i]) {
+ KVM_IVSHMEM_DPRINTK("%d WARNING: local_resource_count>0!: %d", i,
+ local_resource_count[i]);
+ }
+ spin_lock(&rawhide_irq_lock);
+ local_resource_count[i] = 1;
+ spin_unlock(&rawhide_irq_lock);
+ wake_up_interruptible(&local_data_ready_wait_queue[i]);
+ return IRQ_HANDLED;
+ }
+ }
+
+ printk(KERN_ERR "KVM_IVSHMEM: irq %d not handled", irq);
+ return IRQ_NONE;
+}
+
+static int request_msix_vectors(struct kvm_ivshmem_device *ivs_info,
+ int nvectors) {
+ int i, n, err;
+ const char *name = "ivshmem";
+
+ KVM_IVSHMEM_DPRINTK("KVM_IVSHMEM: devname is %s", name);
+ ivs_info->nvectors = nvectors;
+
+ ivs_info->msix_entries =
+ kmalloc(nvectors * sizeof *ivs_info->msix_entries, GFP_KERNEL);
+ ivs_info->msix_names =
+ kmalloc(nvectors * sizeof *ivs_info->msix_names, GFP_KERNEL);
+
+ for (i = 0; i < nvectors; i++)
+ ivs_info->msix_entries[i].entry = i;
+
+ n = pci_alloc_irq_vectors(ivs_info->dev, nvectors, nvectors, PCI_IRQ_MSIX);
+ if (n < 0) {
+ printk(KERN_ERR "KVM_IVSHMEM: pci_alloc_irq_vectors i=%d: error %d", i, n);
+ return n;
+ }
+
+ for (i = 0; i < nvectors; i++) {
+
+ snprintf(ivs_info->msix_names[i], sizeof *ivs_info->msix_names, "%s-config",
+ name);
+
+ n = pci_irq_vector(ivs_info->dev, i);
+ err = request_irq(n, kvm_ivshmem_interrupt, IRQF_SHARED,
+ ivs_info->msix_names[i], ivs_info);
+
+ if (err) {
+ printk(KERN_ERR "KVM_IVSHMEM: couldn't allocate irq for msi-x entry %d "
+ "with vector %d",
+ i, n);
+ return -ENOSPC;
+ } else {
+ printk(KERN_INFO "KVM_IVSHMEM: allocated irq #%d", n);
+ }
+ // vector 1 is used for data sending
+ if (i & LOCAL_RESOURCE_READY_INT_VEC) {
+ irq_incoming_data[i >> 1] = n;
+ KVM_IVSHMEM_DPRINTK("Using interrupt #%d for incoming data for vm %d", n,
+ i >> 1);
+ // vector 0 is used for for sending acknowledgments
+ } else {
+ irq_ack[i >> 1] = n;
+ KVM_IVSHMEM_DPRINTK("Using interrupt #%d for ACKs for vm %d", n, i >> 1);
+ }
+ }
+
+ pci_set_master(ivs_info->dev);
+ return 0;
+}
+
+static int kvm_ivshmem_probe_device(struct pci_dev *pdev,
+ const struct pci_device_id *ent) {
+
+ int result;
+
+ KVM_IVSHMEM_DPRINTK("Probing for KVM_IVSHMEM Device");
+
+ result = pci_enable_device(pdev);
+ if (result) {
+ printk(KERN_ERR "KVM_IVSHMEM: Cannot probe KVM_IVSHMEM device %s: error %d",
+ pci_name(pdev), result);
+ return result;
+ }
+
+ result = pci_request_regions(pdev, "kvm_ivshmem");
+ if (result < 0) {
+ printk(KERN_ERR "KVM_IVSHMEM: cannot request regions");
+ goto pci_disable;
+ } else
+ printk(KERN_ERR "KVM_IVSHMEM: pci_request_regions(): result is %d", result);
+
+ kvm_ivshmem_dev.ioaddr = pci_resource_start(pdev, 2);
+ kvm_ivshmem_dev.ioaddr_size = pci_resource_len(pdev, 2);
+
+ if (flataddr) {
+ kvm_ivshmem_dev.base_addr = memremap(flataddr, kvm_ivshmem_dev.ioaddr_size, MEMREMAP_WB);
+ printk(KERN_ERR "KVM_IVSHMEM: using flat memory 0x%llx mapped to %p", flataddr,
+ kvm_ivshmem_dev.base_addr);
+ }
+ else {
+ kvm_ivshmem_dev.base_addr = pci_iomap(pdev, 2, 0);
+ printk(KERN_INFO "KVM_IVSHMEM: using PCI iomap base = 0x%p", kvm_ivshmem_dev.base_addr);
+ }
+
+ if (!kvm_ivshmem_dev.base_addr) {
+ printk(KERN_ERR "KVM_IVSHMEM: cannot map region size %d",
+ kvm_ivshmem_dev.ioaddr_size);
+ goto pci_release;
+ }
+
+ printk(KERN_INFO "KVM_IVSHMEM: ioaddr = 0x%llx ioaddr_size = 0x%x base_addr = %p flataddr = 0x%llx",
+ kvm_ivshmem_dev.ioaddr, kvm_ivshmem_dev.ioaddr_size, kvm_ivshmem_dev.base_addr, flataddr);
+
+ /* Clear the the shared memory*/
+ memset_io(kvm_ivshmem_dev.base_addr, kvm_ivshmem_dev.ioaddr_size, 0);
+
+ kvm_ivshmem_dev.regaddr = pci_resource_start(pdev, 0);
+ kvm_ivshmem_dev.reg_size = pci_resource_len(pdev, 0);
+ kvm_ivshmem_dev.regs = pci_iomap(pdev, 0, 0x100);
+
+ kvm_ivshmem_dev.dev = pdev;
+
+ if (!kvm_ivshmem_dev.regs) {
+ printk(KERN_ERR "KVM_IVSHMEM: cannot ioremap registers of size %d",
+ kvm_ivshmem_dev.reg_size);
+ goto reg_release;
+ }
+
+ if (request_msix_vectors(&kvm_ivshmem_dev, VECTORS_COUNT) != 0) {
+ printk(KERN_ERR
+ "KVM_IVSHMEM: Check ivshmem and qemu configured interrupts number");
+ goto reg_release;
+ } else {
+ printk(KERN_INFO "KVM_IVSHMEM: MSI-X enabled");
+ }
+
+ /* set all masks to on */
+ writel(0xffffffff, kvm_ivshmem_dev.regs + IntrMask);
+
+ return 0;
+
+reg_release:
+if (!flataddr)
+ pci_iounmap(pdev, kvm_ivshmem_dev.base_addr);
+pci_release:
+ pci_release_regions(pdev);
+ if (flataddr)
+ memunmap(kvm_ivshmem_dev.base_addr);
+pci_disable:
+ pci_disable_device(pdev);
+ return -EBUSY;
+}
+
+static void kvm_ivshmem_remove_device(struct pci_dev *pdev) {
+ int i, n;
+
+ printk(KERN_INFO "KVM_IVSHMEM: Unregister kvm_ivshmem device.");
+ for (i = 0; i < VECTORS_COUNT; i++) {
+ n = pci_irq_vector(pdev, i);
+ KVM_IVSHMEM_DPRINTK("Freeing irq# %d", n);
+ disable_irq(n);
+ free_irq(n, &kvm_ivshmem_dev);
+ }
+ pci_free_irq_vectors(pdev);
+ pci_iounmap(pdev, kvm_ivshmem_dev.regs);
+ pci_iounmap(pdev, kvm_ivshmem_dev.base_addr);
+ pci_release_regions(pdev);
+ pci_disable_device(pdev);
+}
+
+static void __exit kvm_ivshmem_cleanup_module(void) {
+ pci_unregister_driver(&kvm_ivshmem_pci_driver);
+ misc_deregister(&kvm_ivshmem_misc_dev);
+}
+
+static int __init kvm_ivshmem_init_module(void) {
+
+ int err = -ENOMEM, i;
+
+ /* Register device node ops. */
+ err = misc_register(&kvm_ivshmem_misc_dev);
+ if (err < 0) {
+ printk(KERN_ERR "KVM_IVSHMEM: Unable to register kvm_ivshmem_misc device");
+ return err;
+ }
+ KVM_IVSHMEM_DPRINTK("Registered the /dev/%s device ",
+ kvm_ivshmem_misc_dev.name);
+
+ err = pci_register_driver(&kvm_ivshmem_pci_driver);
+ if (err < 0) {
+ goto error;
+ }
+
+ for (i = 0; i < VM_COUNT; i++) {
+ init_waitqueue_head(&local_data_ready_wait_queue[i]);
+ init_waitqueue_head(&peer_data_ready_wait_queue[i]);
+ local_resource_count[i] = 1;
+ peer_resource_count[i] = 0;
+ }
+ return 0;
+
+error:
+ misc_deregister(&kvm_ivshmem_misc_dev);
+ return err;
+}
+
+static int kvm_ivshmem_open(struct inode *inode, struct file *filp) {
+ printk(KERN_INFO "KVM_IVSHMEM: Opening kvm_ivshmem device. Using memory @ %p", kvm_ivshmem_dev.base_addr);
+ filp->private_data = (void *)(unsigned long)-1;
+ return 0;
+}
+
+static int kvm_ivshmem_release(struct inode *inode, struct file *filp) {
+ return 0;
+}
+
+static int kvm_ivshmem_mmap(struct file *filp, struct vm_area_struct *vma) {
+
+ unsigned long len;
+ unsigned long off;
+ uint64_t start;
+
+ off = vma->vm_pgoff << PAGE_SHIFT;
+ start = flataddr? flataddr:(uint64_t)kvm_ivshmem_dev.ioaddr;
+
+ len = PAGE_ALIGN((start & ~PAGE_MASK) + kvm_ivshmem_dev.ioaddr_size);
+ start &= PAGE_MASK;
+
+ printk(KERN_INFO
+ "KVM_IVSHMEM: mmap: vma->vm_start=0x%lx vma->vm_end=0x%lx off=0x%lx",
+ vma->vm_start, vma->vm_end, off);
+ printk(
+ KERN_INFO
+ "KVM_IVSHMEM: mmap: vma->vm_end - vma->vm_start + off=0x%lx > len=0x%lx",
+ (vma->vm_end - vma->vm_start + off), len);
+
+ if ((vma->vm_end - vma->vm_start + off) > len)
+ return -EINVAL;
+
+ off += start;
+ vma->vm_pgoff = off >> PAGE_SHIFT;
+ #if LINUX_VERSION_CODE >= KERNEL_VERSION(6,3,0)
+ vm_flags_mod(vma, VM_SHARED, 0);
+ #else
+ vma->vm_flags |= VM_SHARED;
+ #endif
+
+ if (remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
+ vma->vm_end - vma->vm_start, vma->vm_page_prot)) {
+ KVM_IVSHMEM_DPRINTK("%ld mmap failed",
+ (unsigned long int)filp->private_data);
+ return -ENXIO;
+ }
+
+ return 0;
+}
+
+module_init(kvm_ivshmem_init_module);
+module_exit(kvm_ivshmem_cleanup_module);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Cam Macdonell <[email protected]>");
+MODULE_DESCRIPTION("KVM inter-VM shared memory module");
+MODULE_VERSION("1.0");
diff --git a/drivers/char/ivshmem/kvm_ivshmem.h b/drivers/char/ivshmem/kvm_ivshmem.h
new file mode 100644
index 000000000..aacd7f73e
--- /dev/null
+++ b/drivers/char/ivshmem/kvm_ivshmem.h
@@ -0,0 +1,25 @@
+/* Copyright 2022-2023 TII (SSRC) and the Ghaf contributors
+ SPDX-License-Identifier: Apache-2.0
+*/
+
+#define PEER_RESOURCE_CONSUMED_INT_VEC (0)
+#define LOCAL_RESOURCE_READY_INT_VEC (1)
+
+#define SHMEM_IOC_MAGIC 's'
+
+#define SHMEM_IOCWLOCAL _IOR(SHMEM_IOC_MAGIC, 1, int)
+#define SHMEM_IOCWPEER _IOR(SHMEM_IOC_MAGIC, 2, int)
+#define SHMEM_IOCIVPOSN _IOW(SHMEM_IOC_MAGIC, 3, int)
+#define SHMEM_IOCSETINSTANCENO _IOR(SHMEM_IOC_MAGIC, 4, int)
+#define SHMEM_IOCSET _IOR(SHMEM_IOC_MAGIC, 5, int)
+#define SHMEM_IOCDORBELL _IOR(SHMEM_IOC_MAGIC, 6, int)
+#define SHMEM_IOCNOP _IOR(SHMEM_IOC_MAGIC, 7, int)
+
+// #define DEBUG_IOCTL
+struct ioctl_data {
+ int peer_vm_id;
+ unsigned int int_no;
+ int fd;
+ int cmd;
+ int len;
+};
--
2.45.2