summaryrefslogtreecommitdiffstats
path: root/meta-agl-devel/meta-agl-jailhouse/recipes-kernel/linux/linux/0012-uio-Add-driver-for-inter-VM-shared-memory-device.patch
blob: f98670c3deb3cb39b489b055a572884f392bb409 (plain)
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
From 205cdad2dc9fc8a6a7204b2a71408b43085dd45f Mon Sep 17 00:00:00 2001
From: Jan Kiszka <jan.kiszka@siemens.com>
Date: Tue, 4 Jun 2019 18:40:25 +0200
Subject: [PATCH 12/32] uio: Add driver for inter-VM shared memory device

This adds a UIO driver the ivshmem device, found in QEMU and the
Jailhouse hypervisor. It exposes the MMIO register region and all shared
memory section to userspace. Interrupts are configured in one-shot mode
so that userspace needs to re-enable them after each event via the
Interrupt Control register. The driver registers all possible MSI-X
vectors, coalescing them into the single notifier UIO provides.

Note: Specification work for the interface is ongoing, so details may
still change.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 drivers/uio/Kconfig       |   7 ++
 drivers/uio/Makefile      |   1 +
 drivers/uio/uio_ivshmem.c | 241 ++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/pci_ids.h   |   1 +
 4 files changed, 250 insertions(+)
 create mode 100644 drivers/uio/uio_ivshmem.c

diff --git a/drivers/uio/Kconfig b/drivers/uio/Kconfig
index 202ee81cfc2b..a130500f46b8 100644
--- a/drivers/uio/Kconfig
+++ b/drivers/uio/Kconfig
@@ -165,4 +165,11 @@ config UIO_HV_GENERIC
 	  to network and storage devices from userspace.
 
 	  If you compile this as a module, it will be called uio_hv_generic.
+
+config UIO_IVSHMEM
+	tristate "Inter-VM Shared Memory driver"
+	depends on PCI
+	help
+	  Userspace I/O driver for the inter-VM shared memory PCI device
+	  as provided by QEMU and the Jailhouse hypervisor.
 endif
diff --git a/drivers/uio/Makefile b/drivers/uio/Makefile
index c285dd2a4539..3911fefb2a7e 100644
--- a/drivers/uio/Makefile
+++ b/drivers/uio/Makefile
@@ -11,3 +11,4 @@ obj-$(CONFIG_UIO_PRUSS)         += uio_pruss.o
 obj-$(CONFIG_UIO_MF624)         += uio_mf624.o
 obj-$(CONFIG_UIO_FSL_ELBC_GPCM)	+= uio_fsl_elbc_gpcm.o
 obj-$(CONFIG_UIO_HV_GENERIC)	+= uio_hv_generic.o
+obj-$(CONFIG_UIO_IVSHMEM)	+= uio_ivshmem.o
diff --git a/drivers/uio/uio_ivshmem.c b/drivers/uio/uio_ivshmem.c
new file mode 100644
index 000000000000..0c16d428c6ed
--- /dev/null
+++ b/drivers/uio/uio_ivshmem.c
@@ -0,0 +1,241 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * UIO driver for Inter-VM shared memory PCI device
+ *
+ * Copyright (c) Siemens AG, 2019
+ *
+ * Authors:
+ *  Jan Kiszka <jan.kiszka@siemens.com>
+ */
+
+#include <linux/ivshmem.h>
+#include <linux/module.h>
+#include <linux/pci.h>
+#include <linux/uio_driver.h>
+
+#define DRV_NAME "uio_ivshmem"
+
+struct ivshm_dev {
+	struct uio_info info;
+	struct pci_dev *pdev;
+	struct ivshm_regs __iomem *regs;
+	int vectors;
+};
+
+static irqreturn_t ivshm_irq_handler(int irq, void *dev_id)
+{
+	struct ivshm_dev *ivshm_dev = (struct ivshm_dev *)dev_id;
+
+	/* nothing else to do, we configured one-shot interrupt mode */
+	uio_event_notify(&ivshm_dev->info);
+
+	return IRQ_HANDLED;
+}
+
+static u64 get_config_qword(struct pci_dev *pdev, unsigned int pos)
+{
+	u32 lo, hi;
+
+	pci_read_config_dword(pdev, pos, &lo);
+	pci_read_config_dword(pdev, pos + 4, &hi);
+	return lo | ((u64)hi << 32);
+}
+
+static int ivshm_release(struct uio_info *info, struct inode *inode)
+{
+	struct ivshm_dev *ivshm_dev =
+		container_of(info, struct ivshm_dev, info);
+
+	writel(0, &ivshm_dev->regs->state);
+	return 0;
+}
+
+static int ivshm_probe(struct pci_dev *pdev, const struct pci_device_id *id)
+{
+	resource_size_t rw_section_sz, output_section_sz;
+	struct ivshm_dev *ivshm_dev;
+	phys_addr_t section_addr;
+	int err, vendor_cap, i;
+	unsigned int cap_pos;
+	struct uio_mem *mem;
+	char *device_name;
+	u32 dword;
+
+	ivshm_dev = devm_kzalloc(&pdev->dev, sizeof(struct ivshm_dev),
+				 GFP_KERNEL);
+	if (!ivshm_dev)
+		return -ENOMEM;
+
+	err = pcim_enable_device(pdev);
+	if (err)
+		return err;
+
+	device_name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s[%s]", DRV_NAME,
+				     dev_name(&pdev->dev));
+	if (!device_name)
+		return -ENOMEM;
+
+	ivshm_dev->info.name = device_name;
+	ivshm_dev->info.version = "1";
+	ivshm_dev->info.release = ivshm_release;
+
+	err = pcim_iomap_regions(pdev, BIT(0), device_name);
+	if (err)
+		return err;
+	ivshm_dev->regs = pcim_iomap_table(pdev)[0];
+
+	mem = &ivshm_dev->info.mem[0];
+
+	mem->name = "registers";
+	mem->addr = pci_resource_start(pdev, 0);
+	if (!mem->addr)
+		return -ENODEV;
+	mem->size = pci_resource_len(pdev, 0);
+	mem->memtype = UIO_MEM_PHYS;
+
+	vendor_cap = pci_find_capability(pdev, PCI_CAP_ID_VNDR);
+	if (vendor_cap < 0)
+		return -ENODEV;
+
+	if (pci_resource_len(pdev, 2) > 0) {
+		section_addr = pci_resource_start(pdev, 2);
+	} else {
+		cap_pos = vendor_cap + IVSHM_CFG_ADDRESS;
+		section_addr = get_config_qword(pdev, cap_pos);
+	}
+
+	mem++;
+	mem->name = "state_table";
+	mem->addr = section_addr;
+	cap_pos = vendor_cap + IVSHM_CFG_STATE_TAB_SZ;
+	pci_read_config_dword(pdev, cap_pos, &dword);
+	mem->size = dword;
+	mem->memtype = UIO_MEM_IOVA;
+	mem->readonly = true;
+	if (!devm_request_mem_region(&pdev->dev, mem->addr, mem->size,
+				     device_name))
+		return -EBUSY;
+	dev_info(&pdev->dev, "%s at %pa, size %pa\n", mem->name, &mem->addr,
+		 &mem->size);
+
+	cap_pos = vendor_cap + IVSHM_CFG_RW_SECTION_SZ;
+	rw_section_sz = get_config_qword(pdev, cap_pos);
+	if (rw_section_sz > 0) {
+		section_addr += mem->size;
+
+		mem++;
+		mem->name = "rw_section";
+		mem->addr = section_addr;
+		mem->size = rw_section_sz;
+		mem->memtype = UIO_MEM_IOVA;
+		if (!devm_request_mem_region(&pdev->dev, mem->addr, mem->size,
+					     device_name))
+			return -EBUSY;
+		dev_info(&pdev->dev, "%s at %pa, size %pa\n", mem->name,
+			 &mem->addr, &mem->size);
+	}
+
+	cap_pos = vendor_cap + IVSHM_CFG_OUTPUT_SECTION_SZ;
+	output_section_sz = get_config_qword(pdev, cap_pos);
+	if (output_section_sz > 0) {
+		section_addr += mem->size;
+
+		mem++;
+		mem->name = "input_sections";
+		mem->addr = section_addr;
+		mem->size =
+			readl(&ivshm_dev->regs->max_peers) * output_section_sz;
+		mem->memtype = UIO_MEM_IOVA;
+		mem->readonly = true;
+		if (!devm_request_mem_region(&pdev->dev, mem->addr, mem->size,
+					     device_name))
+			return -EBUSY;
+		dev_info(&pdev->dev, "%s at %pa, size %pa\n", mem->name,
+			 &mem->addr, &mem->size);
+
+		mem++;
+		mem->name = "output_section";
+		mem->addr = section_addr +
+			readl(&ivshm_dev->regs->id) * output_section_sz;
+		mem->size = output_section_sz;
+		mem->memtype = UIO_MEM_IOVA;
+		dev_info(&pdev->dev, "%s at %pa, size %pa\n", mem->name,
+			 &mem->addr, &mem->size);
+	}
+
+	pci_write_config_byte(pdev, vendor_cap + IVSHM_CFG_PRIV_CNTL,
+			      IVSHM_PRIV_CNTL_ONESHOT_INT);
+
+	/*
+	 * Grab all vectors although we can only coalesce them into a single
+	 * notifier. This avoids missing any event.
+	 */
+	ivshm_dev->vectors = pci_msix_vec_count(pdev);
+	if (ivshm_dev->vectors < 0)
+		ivshm_dev->vectors = 1;
+
+	err = pci_alloc_irq_vectors(pdev, ivshm_dev->vectors,
+				    ivshm_dev->vectors,
+				    PCI_IRQ_LEGACY | PCI_IRQ_MSIX);
+	if (err < 0)
+		return err;
+
+	for (i = 0; i < ivshm_dev->vectors; i++) {
+		err = request_irq(pci_irq_vector(pdev, i), ivshm_irq_handler,
+				  IRQF_SHARED, ivshm_dev->info.name, ivshm_dev);
+		if (err)
+			goto error;
+	}
+
+	ivshm_dev->info.irq = UIO_IRQ_CUSTOM;
+
+	err = uio_register_device(&pdev->dev, &ivshm_dev->info);
+	if (err)
+		goto error;
+
+	pci_set_master(pdev);
+
+	pci_set_drvdata(pdev, ivshm_dev);
+
+	return 0;
+
+error:
+	while (--i > 0)
+		free_irq(pci_irq_vector(pdev, i), ivshm_dev);
+	pci_free_irq_vectors(pdev);
+	return err;
+}
+
+static void ivshm_remove(struct pci_dev *pdev)
+{
+	struct ivshm_dev *ivshm_dev = pci_get_drvdata(pdev);
+	int i;
+
+	writel(0, &ivshm_dev->regs->int_control);
+	pci_clear_master(pdev);
+
+	uio_unregister_device(&ivshm_dev->info);
+
+	for (i = 0; i < ivshm_dev->vectors; i++)
+		free_irq(pci_irq_vector(pdev, i), ivshm_dev);
+
+	pci_free_irq_vectors(pdev);
+}
+
+static const struct pci_device_id ivshm_device_id_table[] = {
+	{ PCI_DEVICE(PCI_VENDOR_ID_SIEMENS, PCI_DEVICE_ID_IVSHMEM),
+	  (PCI_CLASS_OTHERS << 16) | IVSHM_PROTO_UNDEFINED, 0xffffff },
+	{ 0 }
+};
+MODULE_DEVICE_TABLE(pci, ivshm_device_id_table);
+
+static struct pci_driver uio_ivshm_driver = {
+	.name = DRV_NAME,
+	.id_table = ivshm_device_id_table,
+	.probe = ivshm_probe,
+	.remove = ivshm_remove,
+};
+module_pci_driver(uio_ivshm_driver);
+
+MODULE_AUTHOR("Jan Kiszka <jan.kiszka@siemens.com>");
+MODULE_LICENSE("GPL v2");
diff --git a/include/linux/pci_ids.h b/include/linux/pci_ids.h
index 21a572469a4e..e450458c8ba8 100644
--- a/include/linux/pci_ids.h
+++ b/include/linux/pci_ids.h
@@ -1477,6 +1477,7 @@
 
 #define PCI_VENDOR_ID_SIEMENS           0x110A
 #define PCI_DEVICE_ID_SIEMENS_DSCC4     0x2102
+#define PCI_DEVICE_ID_IVSHMEM		0x4106
 
 #define PCI_VENDOR_ID_VORTEX		0x1119
 #define PCI_DEVICE_ID_VORTEX_GDT60x0	0x0000
-- 
2.11.0