xref: /kvmtool/virtio/pci.c (revision 3d5cefc2eb3e2dec92bad5bf46a87251027975a2)
1 #include "kvm/virtio-pci.h"
2 
3 #include "kvm/ioport.h"
4 #include "kvm/kvm.h"
5 #include "kvm/kvm-cpu.h"
6 #include "kvm/virtio-pci-dev.h"
7 #include "kvm/irq.h"
8 #include "kvm/virtio.h"
9 #include "kvm/ioeventfd.h"
10 #include "kvm/util.h"
11 
12 #include <sys/ioctl.h>
13 #include <linux/virtio_pci.h>
14 #include <assert.h>
15 #include <string.h>
16 
17 int virtio_pci__add_msix_route(struct virtio_pci *vpci, u32 vec)
18 {
19 	int gsi;
20 	struct msi_msg *msg;
21 
22 	if (vec == VIRTIO_MSI_NO_VECTOR)
23 		return -EINVAL;
24 
25 	msg = &vpci->msix_table[vec].msg;
26 	gsi = irq__add_msix_route(vpci->kvm, msg, vpci->dev_hdr.dev_num << 3);
27 	/*
28 	 * We don't need IRQ routing if we can use
29 	 * MSI injection via the KVM_SIGNAL_MSI ioctl.
30 	 */
31 	if (gsi == -ENXIO && vpci->features & VIRTIO_PCI_F_SIGNAL_MSI)
32 		return gsi;
33 
34 	if (gsi < 0)
35 		die("failed to configure MSIs");
36 
37 	return gsi;
38 }
39 
40 static void virtio_pci__del_msix_route(struct virtio_pci *vpci, u32 gsi)
41 {
42 	struct msi_msg msg = { 0 };
43 
44 	irq__update_msix_route(vpci->kvm, gsi, &msg);
45 }
46 
47 static void virtio_pci__ioevent_callback(struct kvm *kvm, void *param)
48 {
49 	struct virtio_pci_ioevent_param *ioeventfd = param;
50 	struct virtio_pci *vpci = ioeventfd->vdev->virtio;
51 
52 	ioeventfd->vdev->ops->notify_vq(kvm, vpci->dev, ioeventfd->vq);
53 }
54 
55 int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_device *vdev,
56 			       u32 vq)
57 {
58 	struct ioevent ioevent;
59 	struct virtio_pci *vpci = vdev->virtio;
60 	u32 mmio_addr = virtio_pci__mmio_addr(vpci);
61 	u16 port_addr = virtio_pci__port_addr(vpci);
62 	off_t offset = vpci->doorbell_offset;
63 	int r, flags = 0;
64 	int pio_fd, mmio_fd;
65 
66 	vpci->ioeventfds[vq] = (struct virtio_pci_ioevent_param) {
67 		.vdev		= vdev,
68 		.vq		= vq,
69 	};
70 
71 	ioevent = (struct ioevent) {
72 		.fn		= virtio_pci__ioevent_callback,
73 		.fn_ptr		= &vpci->ioeventfds[vq],
74 		.datamatch	= vq,
75 		.fn_kvm		= kvm,
76 	};
77 
78 	/*
79 	 * Vhost will poll the eventfd in host kernel side, otherwise we
80 	 * need to poll in userspace.
81 	 */
82 	if (!vdev->use_vhost)
83 		flags |= IOEVENTFD_FLAG_USER_POLL;
84 
85 	/* ioport */
86 	ioevent.io_addr	= port_addr + offset;
87 	ioevent.io_len	= sizeof(u16);
88 	ioevent.fd	= pio_fd = eventfd(0, 0);
89 	r = ioeventfd__add_event(&ioevent, flags | IOEVENTFD_FLAG_PIO);
90 	if (r)
91 		return r;
92 
93 	/* mmio */
94 	ioevent.io_addr	= mmio_addr + offset;
95 	ioevent.io_len	= sizeof(u16);
96 	ioevent.fd	= mmio_fd = eventfd(0, 0);
97 	r = ioeventfd__add_event(&ioevent, flags);
98 	if (r)
99 		goto free_ioport_evt;
100 
101 	if (vdev->ops->notify_vq_eventfd)
102 		vdev->ops->notify_vq_eventfd(kvm, vpci->dev, vq,
103 					     vdev->legacy ? pio_fd : mmio_fd);
104 	return 0;
105 
106 free_ioport_evt:
107 	ioeventfd__del_event(port_addr + offset, vq);
108 	return r;
109 }
110 
111 int virtio_pci_init_vq(struct kvm *kvm, struct virtio_device *vdev, int vq)
112 {
113 	int ret;
114 	struct virtio_pci *vpci = vdev->virtio;
115 
116 	ret = virtio_pci__init_ioeventfd(kvm, vdev, vq);
117 	if (ret) {
118 		pr_err("couldn't add ioeventfd for vq %d: %d", vq, ret);
119 		return ret;
120 	}
121 	return vdev->ops->init_vq(kvm, vpci->dev, vq);
122 }
123 
124 void virtio_pci_exit_vq(struct kvm *kvm, struct virtio_device *vdev, int vq)
125 {
126 	struct virtio_pci *vpci = vdev->virtio;
127 	u32 mmio_addr = virtio_pci__mmio_addr(vpci);
128 	u16 port_addr = virtio_pci__port_addr(vpci);
129 	off_t offset = vpci->doorbell_offset;
130 
131 	virtio_pci__del_msix_route(vpci, vpci->gsis[vq]);
132 	vpci->gsis[vq] = 0;
133 	vpci->vq_vector[vq] = VIRTIO_MSI_NO_VECTOR;
134 	ioeventfd__del_event(mmio_addr + offset, vq);
135 	ioeventfd__del_event(port_addr + offset, vq);
136 	virtio_exit_vq(kvm, vdev, vpci->dev, vq);
137 }
138 
139 static void update_msix_map(struct virtio_pci *vpci,
140 			    struct msix_table *msix_entry, u32 vecnum)
141 {
142 	u32 gsi, i;
143 
144 	/* Find the GSI number used for that vector */
145 	if (vecnum == vpci->config_vector) {
146 		gsi = vpci->config_gsi;
147 	} else {
148 		for (i = 0; i < VIRTIO_PCI_MAX_VQ; i++)
149 			if (vpci->vq_vector[i] == vecnum)
150 				break;
151 		if (i == VIRTIO_PCI_MAX_VQ)
152 			return;
153 		gsi = vpci->gsis[i];
154 	}
155 
156 	if (gsi == 0)
157 		return;
158 
159 	msix_entry = &msix_entry[vecnum];
160 	irq__update_msix_route(vpci->kvm, gsi, &msix_entry->msg);
161 }
162 
163 static void virtio_pci__msix_mmio_callback(struct kvm_cpu *vcpu,
164 					   u64 addr, u8 *data, u32 len,
165 					   u8 is_write, void *ptr)
166 {
167 	struct virtio_device *vdev = ptr;
168 	struct virtio_pci *vpci = vdev->virtio;
169 	struct msix_table *table;
170 	u32 msix_io_addr = virtio_pci__msix_io_addr(vpci);
171 	u32 pba_offset;
172 	int vecnum;
173 	size_t offset;
174 
175 	BUILD_BUG_ON(VIRTIO_NR_MSIX > (sizeof(vpci->msix_pba) * 8));
176 
177 	pba_offset = vpci->pci_hdr.msix.pba_offset & ~PCI_MSIX_TABLE_BIR;
178 	if (addr >= msix_io_addr + pba_offset) {
179 		/* Read access to PBA */
180 		if (is_write)
181 			return;
182 		offset = addr - (msix_io_addr + pba_offset);
183 		if ((offset + len) > sizeof (vpci->msix_pba))
184 			return;
185 		memcpy(data, (void *)&vpci->msix_pba + offset, len);
186 		return;
187 	}
188 
189 	table  = vpci->msix_table;
190 	offset = addr - msix_io_addr;
191 
192 	vecnum = offset / sizeof(struct msix_table);
193 	offset = offset % sizeof(struct msix_table);
194 
195 	if (!is_write) {
196 		memcpy(data, (void *)&table[vecnum] + offset, len);
197 		return;
198 	}
199 
200 	memcpy((void *)&table[vecnum] + offset, data, len);
201 
202 	/* Did we just update the address or payload? */
203 	if (offset < offsetof(struct msix_table, ctrl))
204 		update_msix_map(vpci, table, vecnum);
205 }
206 
207 static void virtio_pci__signal_msi(struct kvm *kvm, struct virtio_pci *vpci,
208 				   int vec)
209 {
210 	struct kvm_msi msi = {
211 		.address_lo = vpci->msix_table[vec].msg.address_lo,
212 		.address_hi = vpci->msix_table[vec].msg.address_hi,
213 		.data = vpci->msix_table[vec].msg.data,
214 	};
215 
216 	if (kvm->msix_needs_devid) {
217 		msi.flags = KVM_MSI_VALID_DEVID;
218 		msi.devid = vpci->dev_hdr.dev_num << 3;
219 	}
220 
221 	irq__signal_msi(kvm, &msi);
222 }
223 
224 int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq)
225 {
226 	struct virtio_pci *vpci = vdev->virtio;
227 	int tbl = vpci->vq_vector[vq];
228 
229 	if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) {
230 		if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) ||
231 		    vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) {
232 
233 			vpci->msix_pba |= 1 << tbl;
234 			return 0;
235 		}
236 
237 		if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI)
238 			virtio_pci__signal_msi(kvm, vpci, vpci->vq_vector[vq]);
239 		else
240 			kvm__irq_trigger(kvm, vpci->gsis[vq]);
241 	} else {
242 		vpci->isr = VIRTIO_IRQ_HIGH;
243 		kvm__irq_line(kvm, vpci->legacy_irq_line, VIRTIO_IRQ_HIGH);
244 	}
245 	return 0;
246 }
247 
248 int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev)
249 {
250 	struct virtio_pci *vpci = vdev->virtio;
251 	int tbl = vpci->config_vector;
252 
253 	if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) {
254 		if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) ||
255 		    vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) {
256 
257 			vpci->msix_pba |= 1 << tbl;
258 			return 0;
259 		}
260 
261 		if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI)
262 			virtio_pci__signal_msi(kvm, vpci, tbl);
263 		else
264 			kvm__irq_trigger(kvm, vpci->config_gsi);
265 	} else {
266 		vpci->isr = VIRTIO_PCI_ISR_CONFIG;
267 		kvm__irq_trigger(kvm, vpci->legacy_irq_line);
268 	}
269 
270 	return 0;
271 }
272 
273 static int virtio_pci__bar_activate(struct kvm *kvm,
274 				    struct pci_device_header *pci_hdr,
275 				    int bar_num, void *data)
276 {
277 	struct virtio_device *vdev = data;
278 	mmio_handler_fn mmio_fn;
279 	u32 bar_addr, bar_size;
280 	int r = -EINVAL;
281 
282 	if (vdev->legacy)
283 		mmio_fn = &virtio_pci_legacy__io_mmio_callback;
284 	else
285 		mmio_fn = &virtio_pci_modern__io_mmio_callback;
286 
287 	assert(bar_num <= 2);
288 
289 	bar_addr = pci__bar_address(pci_hdr, bar_num);
290 	bar_size = pci__bar_size(pci_hdr, bar_num);
291 
292 	switch (bar_num) {
293 	case 0:
294 		r = kvm__register_pio(kvm, bar_addr, bar_size, mmio_fn, vdev);
295 		break;
296 	case 1:
297 		r =  kvm__register_mmio(kvm, bar_addr, bar_size, false, mmio_fn,
298 					vdev);
299 		break;
300 	case 2:
301 		r =  kvm__register_mmio(kvm, bar_addr, bar_size, false,
302 					virtio_pci__msix_mmio_callback, vdev);
303 		break;
304 	}
305 
306 	return r;
307 }
308 
309 static int virtio_pci__bar_deactivate(struct kvm *kvm,
310 				      struct pci_device_header *pci_hdr,
311 				      int bar_num, void *data)
312 {
313 	u32 bar_addr;
314 	bool success;
315 	int r = -EINVAL;
316 
317 	assert(bar_num <= 2);
318 
319 	bar_addr = pci__bar_address(pci_hdr, bar_num);
320 
321 	switch (bar_num) {
322 	case 0:
323 		r = kvm__deregister_pio(kvm, bar_addr);
324 		break;
325 	case 1:
326 	case 2:
327 		success = kvm__deregister_mmio(kvm, bar_addr);
328 		/* kvm__deregister_mmio fails when the region is not found. */
329 		r = (success ? 0 : -ENOENT);
330 		break;
331 	}
332 
333 	return r;
334 }
335 
336 int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
337 		     int device_id, int subsys_id, int class)
338 {
339 	struct virtio_pci *vpci = vdev->virtio;
340 	u32 mmio_addr, msix_io_block;
341 	u16 port_addr;
342 	int r;
343 
344 	vpci->kvm = kvm;
345 	vpci->dev = dev;
346 
347 	BUILD_BUG_ON(!is_power_of_two(PCI_IO_SIZE));
348 
349 	port_addr = pci_get_io_port_block(PCI_IO_SIZE);
350 	mmio_addr = pci_get_mmio_block(PCI_IO_SIZE);
351 	msix_io_block = pci_get_mmio_block(VIRTIO_MSIX_BAR_SIZE);
352 
353 	vpci->pci_hdr = (struct pci_device_header) {
354 		.vendor_id		= cpu_to_le16(PCI_VENDOR_ID_REDHAT_QUMRANET),
355 		.device_id		= cpu_to_le16(device_id),
356 		.command		= PCI_COMMAND_IO | PCI_COMMAND_MEMORY,
357 		.header_type		= PCI_HEADER_TYPE_NORMAL,
358 		.revision_id		= vdev->legacy ? 0 : 1,
359 		.class[0]		= class & 0xff,
360 		.class[1]		= (class >> 8) & 0xff,
361 		.class[2]		= (class >> 16) & 0xff,
362 		.subsys_vendor_id	= cpu_to_le16(PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET),
363 		.subsys_id		= cpu_to_le16(subsys_id),
364 		.bar[0]			= cpu_to_le32(port_addr
365 							| PCI_BASE_ADDRESS_SPACE_IO),
366 		.bar[1]			= cpu_to_le32(mmio_addr
367 							| PCI_BASE_ADDRESS_SPACE_MEMORY),
368 		.bar[2]			= cpu_to_le32(msix_io_block
369 							| PCI_BASE_ADDRESS_SPACE_MEMORY),
370 		.status			= cpu_to_le16(PCI_STATUS_CAP_LIST),
371 		.capabilities		= PCI_CAP_OFF(&vpci->pci_hdr, msix),
372 		.bar_size[0]		= cpu_to_le32(PCI_IO_SIZE),
373 		.bar_size[1]		= cpu_to_le32(PCI_IO_SIZE),
374 		.bar_size[2]		= cpu_to_le32(VIRTIO_MSIX_BAR_SIZE),
375 	};
376 
377 	r = pci__register_bar_regions(kvm, &vpci->pci_hdr,
378 				      virtio_pci__bar_activate,
379 				      virtio_pci__bar_deactivate, vdev);
380 	if (r < 0)
381 		return r;
382 
383 	vpci->dev_hdr = (struct device_header) {
384 		.bus_type		= DEVICE_BUS_PCI,
385 		.data			= &vpci->pci_hdr,
386 	};
387 
388 	vpci->pci_hdr.msix.cap = PCI_CAP_ID_MSIX;
389 	vpci->pci_hdr.msix.next = 0;
390 	/*
391 	 * We at most have VIRTIO_NR_MSIX entries (VIRTIO_PCI_MAX_VQ
392 	 * entries for virt queue, VIRTIO_PCI_MAX_CONFIG entries for
393 	 * config).
394 	 *
395 	 * To quote the PCI spec:
396 	 *
397 	 * System software reads this field to determine the
398 	 * MSI-X Table Size N, which is encoded as N-1.
399 	 * For example, a returned value of "00000000011"
400 	 * indicates a table size of 4.
401 	 */
402 	vpci->pci_hdr.msix.ctrl = cpu_to_le16(VIRTIO_NR_MSIX - 1);
403 
404 	/* Both table and PBA are mapped to the same BAR (2) */
405 	vpci->pci_hdr.msix.table_offset = cpu_to_le32(2);
406 	vpci->pci_hdr.msix.pba_offset = cpu_to_le32(2 | VIRTIO_MSIX_TABLE_SIZE);
407 	vpci->config_vector = VIRTIO_MSI_NO_VECTOR;
408 	/* Initialize all vq vectors to NO_VECTOR */
409 	memset(vpci->vq_vector, 0xff, sizeof(vpci->vq_vector));
410 
411 	if (irq__can_signal_msi(kvm))
412 		vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI;
413 
414 	vpci->legacy_irq_line = pci__assign_irq(&vpci->pci_hdr);
415 
416 	r = device__register(&vpci->dev_hdr);
417 	if (r < 0)
418 		return r;
419 
420 	if (vdev->legacy)
421 		vpci->doorbell_offset = VIRTIO_PCI_QUEUE_NOTIFY;
422 	else
423 		return virtio_pci_modern_init(vdev);
424 
425 	return 0;
426 }
427 
428 int virtio_pci__reset(struct kvm *kvm, struct virtio_device *vdev)
429 {
430 	unsigned int vq;
431 	struct virtio_pci *vpci = vdev->virtio;
432 
433 	virtio_pci__del_msix_route(vpci, vpci->config_gsi);
434 	vpci->config_gsi = 0;
435 	vpci->config_vector = VIRTIO_MSI_NO_VECTOR;
436 
437 	for (vq = 0; vq < vdev->ops->get_vq_count(kvm, vpci->dev); vq++)
438 		virtio_pci_exit_vq(kvm, vdev, vq);
439 
440 	return 0;
441 }
442 
443 int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev)
444 {
445 	struct virtio_pci *vpci = vdev->virtio;
446 
447 	virtio_pci__reset(kvm, vdev);
448 	kvm__deregister_mmio(kvm, virtio_pci__mmio_addr(vpci));
449 	kvm__deregister_mmio(kvm, virtio_pci__msix_io_addr(vpci));
450 	kvm__deregister_pio(kvm, virtio_pci__port_addr(vpci));
451 
452 	return 0;
453 }
454