xref: /kvmtool/virtio/pci.c (revision 9b7359101f9f6f15eee493865678051c507e900c)
1 #include "kvm/virtio-pci.h"
2 
3 #include "kvm/ioport.h"
4 #include "kvm/kvm.h"
5 #include "kvm/virtio-pci-dev.h"
6 #include "kvm/irq.h"
7 #include "kvm/virtio.h"
8 #include "kvm/ioeventfd.h"
9 
10 #include <sys/ioctl.h>
11 #include <linux/virtio_pci.h>
12 #include <linux/byteorder.h>
13 #include <string.h>
14 
15 static void virtio_pci__ioevent_callback(struct kvm *kvm, void *param)
16 {
17 	struct virtio_pci_ioevent_param *ioeventfd = param;
18 	struct virtio_pci *vpci = ioeventfd->vdev->virtio;
19 
20 	ioeventfd->vdev->ops->notify_vq(kvm, vpci->dev, ioeventfd->vq);
21 }
22 
23 static int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_device *vdev, u32 vq)
24 {
25 	struct ioevent ioevent;
26 	struct virtio_pci *vpci = vdev->virtio;
27 	int i, r, flags = IOEVENTFD_FLAG_PIO;
28 	int fds[2];
29 
30 	vpci->ioeventfds[vq] = (struct virtio_pci_ioevent_param) {
31 		.vdev		= vdev,
32 		.vq		= vq,
33 	};
34 
35 	ioevent = (struct ioevent) {
36 		.fn		= virtio_pci__ioevent_callback,
37 		.fn_ptr		= &vpci->ioeventfds[vq],
38 		.datamatch	= vq,
39 		.fn_kvm		= kvm,
40 	};
41 
42 	/*
43 	 * Vhost will poll the eventfd in host kernel side, otherwise we
44 	 * need to poll in userspace.
45 	 */
46 	if (!vdev->use_vhost)
47 		flags |= IOEVENTFD_FLAG_USER_POLL;
48 
49 	/* ioport */
50 	ioevent.io_addr	= vpci->port_addr + VIRTIO_PCI_QUEUE_NOTIFY;
51 	ioevent.io_len	= sizeof(u16);
52 	ioevent.fd	= fds[0] = eventfd(0, 0);
53 	r = ioeventfd__add_event(&ioevent, flags);
54 	if (r)
55 		return r;
56 
57 	/* mmio */
58 	ioevent.io_addr	= vpci->mmio_addr + VIRTIO_PCI_QUEUE_NOTIFY;
59 	ioevent.io_len	= sizeof(u32);
60 	ioevent.fd	= fds[1] = eventfd(0, 0);
61 	r = ioeventfd__add_event(&ioevent, flags);
62 	if (r)
63 		goto free_ioport_evt;
64 
65 	if (vdev->ops->notify_vq_eventfd)
66 		for (i = 0; i < 2; ++i)
67 			vdev->ops->notify_vq_eventfd(kvm, vpci->dev, vq,
68 						     fds[i]);
69 	return 0;
70 
71 free_ioport_evt:
72 	ioeventfd__del_event(vpci->port_addr + VIRTIO_PCI_QUEUE_NOTIFY, vq);
73 	return r;
74 }
75 
76 static inline bool virtio_pci__msix_enabled(struct virtio_pci *vpci)
77 {
78 	return vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_ENABLE);
79 }
80 
81 static bool virtio_pci__specific_io_in(struct kvm *kvm, struct virtio_device *vdev, u16 port,
82 					void *data, int size, int offset)
83 {
84 	u32 config_offset;
85 	struct virtio_pci *vpci = vdev->virtio;
86 	int type = virtio__get_dev_specific_field(offset - 20,
87 							virtio_pci__msix_enabled(vpci),
88 							&config_offset);
89 	if (type == VIRTIO_PCI_O_MSIX) {
90 		switch (offset) {
91 		case VIRTIO_MSI_CONFIG_VECTOR:
92 			ioport__write16(data, vpci->config_vector);
93 			break;
94 		case VIRTIO_MSI_QUEUE_VECTOR:
95 			ioport__write16(data, vpci->vq_vector[vpci->queue_selector]);
96 			break;
97 		};
98 
99 		return true;
100 	} else if (type == VIRTIO_PCI_O_CONFIG) {
101 		u8 cfg;
102 
103 		cfg = vdev->ops->get_config(kvm, vpci->dev)[config_offset];
104 		ioport__write8(data, cfg);
105 		return true;
106 	}
107 
108 	return false;
109 }
110 
111 static bool virtio_pci__io_in(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
112 {
113 	unsigned long offset;
114 	bool ret = true;
115 	struct virtio_device *vdev;
116 	struct virtio_pci *vpci;
117 	u32 val;
118 
119 	vdev = ioport->priv;
120 	vpci = vdev->virtio;
121 	offset = port - vpci->port_addr;
122 
123 	switch (offset) {
124 	case VIRTIO_PCI_HOST_FEATURES:
125 		val = vdev->ops->get_host_features(kvm, vpci->dev);
126 		ioport__write32(data, val);
127 		break;
128 	case VIRTIO_PCI_QUEUE_PFN:
129 		val = vdev->ops->get_pfn_vq(kvm, vpci->dev, vpci->queue_selector);
130 		ioport__write32(data, val);
131 		break;
132 	case VIRTIO_PCI_QUEUE_NUM:
133 		val = vdev->ops->get_size_vq(kvm, vpci->dev, vpci->queue_selector);
134 		ioport__write16(data, val);
135 		break;
136 	case VIRTIO_PCI_STATUS:
137 		ioport__write8(data, vpci->status);
138 		break;
139 	case VIRTIO_PCI_ISR:
140 		ioport__write8(data, vpci->isr);
141 		kvm__irq_line(kvm, vpci->pci_hdr.irq_line, VIRTIO_IRQ_LOW);
142 		vpci->isr = VIRTIO_IRQ_LOW;
143 		break;
144 	default:
145 		ret = virtio_pci__specific_io_in(kvm, vdev, port, data, size, offset);
146 		break;
147 	};
148 
149 	return ret;
150 }
151 
152 static bool virtio_pci__specific_io_out(struct kvm *kvm, struct virtio_device *vdev, u16 port,
153 					void *data, int size, int offset)
154 {
155 	struct virtio_pci *vpci = vdev->virtio;
156 	u32 config_offset, gsi, vec;
157 	int type = virtio__get_dev_specific_field(offset - 20, virtio_pci__msix_enabled(vpci),
158 							&config_offset);
159 	if (type == VIRTIO_PCI_O_MSIX) {
160 		switch (offset) {
161 		case VIRTIO_MSI_CONFIG_VECTOR:
162 			vec = vpci->config_vector = ioport__read16(data);
163 			if (vec == VIRTIO_MSI_NO_VECTOR)
164 				break;
165 
166 			gsi = irq__add_msix_route(kvm, &vpci->msix_table[vec].msg);
167 
168 			vpci->config_gsi = gsi;
169 			break;
170 		case VIRTIO_MSI_QUEUE_VECTOR:
171 			vec = vpci->vq_vector[vpci->queue_selector] = ioport__read16(data);
172 
173 			if (vec == VIRTIO_MSI_NO_VECTOR)
174 				break;
175 
176 			gsi = irq__add_msix_route(kvm, &vpci->msix_table[vec].msg);
177 			vpci->gsis[vpci->queue_selector] = gsi;
178 			if (vdev->ops->notify_vq_gsi)
179 				vdev->ops->notify_vq_gsi(kvm, vpci->dev,
180 							vpci->queue_selector, gsi);
181 			break;
182 		};
183 
184 		return true;
185 	} else if (type == VIRTIO_PCI_O_CONFIG) {
186 		vdev->ops->get_config(kvm, vpci->dev)[config_offset] = *(u8 *)data;
187 
188 		return true;
189 	}
190 
191 	return false;
192 }
193 
194 static bool virtio_pci__io_out(struct ioport *ioport, struct kvm *kvm, u16 port, void *data, int size)
195 {
196 	unsigned long offset;
197 	bool ret = true;
198 	struct virtio_device *vdev;
199 	struct virtio_pci *vpci;
200 	u32 val;
201 
202 	vdev = ioport->priv;
203 	vpci = vdev->virtio;
204 	offset = port - vpci->port_addr;
205 
206 	switch (offset) {
207 	case VIRTIO_PCI_GUEST_FEATURES:
208 		val = ioport__read32(data);
209 		vdev->ops->set_guest_features(kvm, vpci->dev, val);
210 		break;
211 	case VIRTIO_PCI_QUEUE_PFN:
212 		val = ioport__read32(data);
213 		virtio_pci__init_ioeventfd(kvm, vdev, vpci->queue_selector);
214 		vdev->ops->init_vq(kvm, vpci->dev, vpci->queue_selector,
215 				   1 << VIRTIO_PCI_QUEUE_ADDR_SHIFT,
216 				   VIRTIO_PCI_VRING_ALIGN, val);
217 		break;
218 	case VIRTIO_PCI_QUEUE_SEL:
219 		vpci->queue_selector = ioport__read16(data);
220 		break;
221 	case VIRTIO_PCI_QUEUE_NOTIFY:
222 		val = ioport__read16(data);
223 		vdev->ops->notify_vq(kvm, vpci->dev, val);
224 		break;
225 	case VIRTIO_PCI_STATUS:
226 		vpci->status = ioport__read8(data);
227 		if (vdev->ops->notify_status)
228 			vdev->ops->notify_status(kvm, vpci->dev, vpci->status);
229 		break;
230 	default:
231 		ret = virtio_pci__specific_io_out(kvm, vdev, port, data, size, offset);
232 		break;
233 	};
234 
235 	return ret;
236 }
237 
238 static struct ioport_operations virtio_pci__io_ops = {
239 	.io_in	= virtio_pci__io_in,
240 	.io_out	= virtio_pci__io_out,
241 };
242 
243 static void virtio_pci__msix_mmio_callback(struct kvm_cpu *vcpu,
244 					   u64 addr, u8 *data, u32 len,
245 					   u8 is_write, void *ptr)
246 {
247 	struct virtio_pci *vpci = ptr;
248 	void *table;
249 	u32 offset;
250 
251 	if (addr > vpci->msix_io_block + PCI_IO_SIZE) {
252 		table	= &vpci->msix_pba;
253 		offset	= vpci->msix_io_block + PCI_IO_SIZE;
254 	} else {
255 		table	= &vpci->msix_table;
256 		offset	= vpci->msix_io_block;
257 	}
258 
259 	if (is_write)
260 		memcpy(table + addr - offset, data, len);
261 	else
262 		memcpy(data, table + addr - offset, len);
263 }
264 
265 static void virtio_pci__signal_msi(struct kvm *kvm, struct virtio_pci *vpci, int vec)
266 {
267 	struct kvm_msi msi = {
268 		.address_lo = vpci->msix_table[vec].msg.address_lo,
269 		.address_hi = vpci->msix_table[vec].msg.address_hi,
270 		.data = vpci->msix_table[vec].msg.data,
271 	};
272 
273 	ioctl(kvm->vm_fd, KVM_SIGNAL_MSI, &msi);
274 }
275 
276 int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq)
277 {
278 	struct virtio_pci *vpci = vdev->virtio;
279 	int tbl = vpci->vq_vector[vq];
280 
281 	if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) {
282 		if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) ||
283 		    vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) {
284 
285 			vpci->msix_pba |= 1 << tbl;
286 			return 0;
287 		}
288 
289 		if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI)
290 			virtio_pci__signal_msi(kvm, vpci, vpci->vq_vector[vq]);
291 		else
292 			kvm__irq_trigger(kvm, vpci->gsis[vq]);
293 	} else {
294 		vpci->isr = VIRTIO_IRQ_HIGH;
295 		kvm__irq_trigger(kvm, vpci->pci_hdr.irq_line);
296 	}
297 	return 0;
298 }
299 
300 int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev)
301 {
302 	struct virtio_pci *vpci = vdev->virtio;
303 	int tbl = vpci->config_vector;
304 
305 	if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) {
306 		if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) ||
307 		    vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) {
308 
309 			vpci->msix_pba |= 1 << tbl;
310 			return 0;
311 		}
312 
313 		if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI)
314 			virtio_pci__signal_msi(kvm, vpci, tbl);
315 		else
316 			kvm__irq_trigger(kvm, vpci->config_gsi);
317 	} else {
318 		vpci->isr = VIRTIO_PCI_ISR_CONFIG;
319 		kvm__irq_trigger(kvm, vpci->pci_hdr.irq_line);
320 	}
321 
322 	return 0;
323 }
324 
325 static void virtio_pci__io_mmio_callback(struct kvm_cpu *vcpu,
326 					 u64 addr, u8 *data, u32 len,
327 					 u8 is_write, void *ptr)
328 {
329 	struct virtio_pci *vpci = ptr;
330 	int direction = is_write ? KVM_EXIT_IO_OUT : KVM_EXIT_IO_IN;
331 	u16 port = vpci->port_addr + (addr & (IOPORT_SIZE - 1));
332 
333 	kvm__emulate_io(vpci->kvm, port, data, direction, len, 1);
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 	int r;
341 
342 	vpci->kvm = kvm;
343 	vpci->dev = dev;
344 
345 	r = ioport__register(kvm, IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vdev);
346 	if (r < 0)
347 		return r;
348 	vpci->port_addr = (u16)r;
349 
350 	vpci->mmio_addr = pci_get_io_space_block(IOPORT_SIZE);
351 	r = kvm__register_mmio(kvm, vpci->mmio_addr, IOPORT_SIZE, false,
352 			       virtio_pci__io_mmio_callback, vpci);
353 	if (r < 0)
354 		goto free_ioport;
355 
356 	vpci->msix_io_block = pci_get_io_space_block(PCI_IO_SIZE * 2);
357 	r = kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE * 2, false,
358 			       virtio_pci__msix_mmio_callback, vpci);
359 	if (r < 0)
360 		goto free_mmio;
361 
362 	vpci->pci_hdr = (struct pci_device_header) {
363 		.vendor_id		= cpu_to_le16(PCI_VENDOR_ID_REDHAT_QUMRANET),
364 		.device_id		= cpu_to_le16(device_id),
365 		.command		= PCI_COMMAND_IO | PCI_COMMAND_MEMORY,
366 		.header_type		= PCI_HEADER_TYPE_NORMAL,
367 		.revision_id		= 0,
368 		.class[0]		= class & 0xff,
369 		.class[1]		= (class >> 8) & 0xff,
370 		.class[2]		= (class >> 16) & 0xff,
371 		.subsys_vendor_id	= cpu_to_le16(PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET),
372 		.subsys_id		= cpu_to_le16(subsys_id),
373 		.bar[0]			= cpu_to_le32(vpci->mmio_addr
374 							| PCI_BASE_ADDRESS_SPACE_MEMORY),
375 		.bar[1]			= cpu_to_le32(vpci->port_addr
376 							| PCI_BASE_ADDRESS_SPACE_IO),
377 		.bar[2]			= cpu_to_le32(vpci->msix_io_block
378 							| PCI_BASE_ADDRESS_SPACE_MEMORY),
379 		.status			= cpu_to_le16(PCI_STATUS_CAP_LIST),
380 		.capabilities		= (void *)&vpci->pci_hdr.msix - (void *)&vpci->pci_hdr,
381 		.bar_size[0]		= IOPORT_SIZE,
382 		.bar_size[1]		= IOPORT_SIZE,
383 		.bar_size[2]		= PCI_IO_SIZE * 2,
384 	};
385 
386 	vpci->dev_hdr = (struct device_header) {
387 		.bus_type		= DEVICE_BUS_PCI,
388 		.data			= &vpci->pci_hdr,
389 	};
390 
391 	vpci->pci_hdr.msix.cap = PCI_CAP_ID_MSIX;
392 	vpci->pci_hdr.msix.next = 0;
393 	/*
394 	 * We at most have VIRTIO_PCI_MAX_VQ entries for virt queue,
395 	 * VIRTIO_PCI_MAX_CONFIG entries for config.
396 	 *
397 	 * To quote the PCI spec:
398 	 *
399 	 * System software reads this field to determine the
400 	 * MSI-X Table Size N, which is encoded as N-1.
401 	 * For example, a returned value of "00000000011"
402 	 * indicates a table size of 4.
403 	 */
404 	vpci->pci_hdr.msix.ctrl = cpu_to_le16(VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG - 1);
405 
406 	/* Both table and PBA are mapped to the same BAR (2) */
407 	vpci->pci_hdr.msix.table_offset = cpu_to_le32(2);
408 	vpci->pci_hdr.msix.pba_offset = cpu_to_le32(2 | PCI_IO_SIZE);
409 	vpci->config_vector = 0;
410 
411 	if (kvm__supports_extension(kvm, KVM_CAP_SIGNAL_MSI))
412 		vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI;
413 
414 	r = device__register(&vpci->dev_hdr);
415 	if (r < 0)
416 		goto free_msix_mmio;
417 
418 	return 0;
419 
420 free_msix_mmio:
421 	kvm__deregister_mmio(kvm, vpci->msix_io_block);
422 free_mmio:
423 	kvm__deregister_mmio(kvm, vpci->mmio_addr);
424 free_ioport:
425 	ioport__unregister(kvm, vpci->port_addr);
426 	return r;
427 }
428 
429 int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev)
430 {
431 	struct virtio_pci *vpci = vdev->virtio;
432 	int i;
433 
434 	kvm__deregister_mmio(kvm, vpci->mmio_addr);
435 	kvm__deregister_mmio(kvm, vpci->msix_io_block);
436 	ioport__unregister(kvm, vpci->port_addr);
437 
438 	for (i = 0; i < VIRTIO_PCI_MAX_VQ; i++) {
439 		ioeventfd__del_event(vpci->port_addr + VIRTIO_PCI_QUEUE_NOTIFY, i);
440 		ioeventfd__del_event(vpci->mmio_addr + VIRTIO_PCI_QUEUE_NOTIFY, i);
441 	}
442 
443 	return 0;
444 }
445