xref: /kvmtool/virtio/pci.c (revision 7f542b0f39bd6ef93d323a5ac27ca677606c337b)
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(u64 addr, u8 *data, u32 len,
244 					   u8 is_write, void *ptr)
245 {
246 	struct virtio_pci *vpci = ptr;
247 	void *table;
248 	u32 offset;
249 
250 	if (addr > vpci->msix_io_block + PCI_IO_SIZE) {
251 		table	= &vpci->msix_pba;
252 		offset	= vpci->msix_io_block + PCI_IO_SIZE;
253 	} else {
254 		table	= &vpci->msix_table;
255 		offset	= vpci->msix_io_block;
256 	}
257 
258 	if (is_write)
259 		memcpy(table + addr - offset, data, len);
260 	else
261 		memcpy(data, table + addr - offset, len);
262 }
263 
264 static void virtio_pci__signal_msi(struct kvm *kvm, struct virtio_pci *vpci, int vec)
265 {
266 	struct kvm_msi msi = {
267 		.address_lo = vpci->msix_table[vec].msg.address_lo,
268 		.address_hi = vpci->msix_table[vec].msg.address_hi,
269 		.data = vpci->msix_table[vec].msg.data,
270 	};
271 
272 	ioctl(kvm->vm_fd, KVM_SIGNAL_MSI, &msi);
273 }
274 
275 int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq)
276 {
277 	struct virtio_pci *vpci = vdev->virtio;
278 	int tbl = vpci->vq_vector[vq];
279 
280 	if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) {
281 		if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) ||
282 		    vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) {
283 
284 			vpci->msix_pba |= 1 << tbl;
285 			return 0;
286 		}
287 
288 		if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI)
289 			virtio_pci__signal_msi(kvm, vpci, vpci->vq_vector[vq]);
290 		else
291 			kvm__irq_trigger(kvm, vpci->gsis[vq]);
292 	} else {
293 		vpci->isr = VIRTIO_IRQ_HIGH;
294 		kvm__irq_trigger(kvm, vpci->pci_hdr.irq_line);
295 	}
296 	return 0;
297 }
298 
299 int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev)
300 {
301 	struct virtio_pci *vpci = vdev->virtio;
302 	int tbl = vpci->config_vector;
303 
304 	if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) {
305 		if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) ||
306 		    vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) {
307 
308 			vpci->msix_pba |= 1 << tbl;
309 			return 0;
310 		}
311 
312 		if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI)
313 			virtio_pci__signal_msi(kvm, vpci, tbl);
314 		else
315 			kvm__irq_trigger(kvm, vpci->config_gsi);
316 	} else {
317 		vpci->isr = VIRTIO_PCI_ISR_CONFIG;
318 		kvm__irq_trigger(kvm, vpci->pci_hdr.irq_line);
319 	}
320 
321 	return 0;
322 }
323 
324 static void virtio_pci__io_mmio_callback(u64 addr, u8 *data, u32 len,
325 					 u8 is_write, void *ptr)
326 {
327 	struct virtio_pci *vpci = ptr;
328 	int direction = is_write ? KVM_EXIT_IO_OUT : KVM_EXIT_IO_IN;
329 	u16 port = vpci->port_addr + (addr & (IOPORT_SIZE - 1));
330 
331 	kvm__emulate_io(vpci->kvm, port, data, direction, len, 1);
332 }
333 
334 int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev,
335 		     int device_id, int subsys_id, int class)
336 {
337 	struct virtio_pci *vpci = vdev->virtio;
338 	int r;
339 
340 	vpci->kvm = kvm;
341 	vpci->dev = dev;
342 
343 	r = ioport__register(kvm, IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vdev);
344 	if (r < 0)
345 		return r;
346 	vpci->port_addr = (u16)r;
347 
348 	vpci->mmio_addr = pci_get_io_space_block(IOPORT_SIZE);
349 	r = kvm__register_mmio(kvm, vpci->mmio_addr, IOPORT_SIZE, false,
350 			       virtio_pci__io_mmio_callback, vpci);
351 	if (r < 0)
352 		goto free_ioport;
353 
354 	vpci->msix_io_block = pci_get_io_space_block(PCI_IO_SIZE * 2);
355 	r = kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE * 2, false,
356 			       virtio_pci__msix_mmio_callback, vpci);
357 	if (r < 0)
358 		goto free_mmio;
359 
360 	vpci->pci_hdr = (struct pci_device_header) {
361 		.vendor_id		= cpu_to_le16(PCI_VENDOR_ID_REDHAT_QUMRANET),
362 		.device_id		= cpu_to_le16(device_id),
363 		.header_type		= PCI_HEADER_TYPE_NORMAL,
364 		.revision_id		= 0,
365 		.class[0]		= class & 0xff,
366 		.class[1]		= (class >> 8) & 0xff,
367 		.class[2]		= (class >> 16) & 0xff,
368 		.subsys_vendor_id	= cpu_to_le16(PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET),
369 		.subsys_id		= cpu_to_le16(subsys_id),
370 		.bar[0]			= cpu_to_le32(vpci->mmio_addr
371 							| PCI_BASE_ADDRESS_SPACE_MEMORY),
372 		.bar[1]			= cpu_to_le32(vpci->port_addr
373 							| PCI_BASE_ADDRESS_SPACE_IO),
374 		.bar[2]			= cpu_to_le32(vpci->msix_io_block
375 							| PCI_BASE_ADDRESS_SPACE_MEMORY),
376 		.status			= cpu_to_le16(PCI_STATUS_CAP_LIST),
377 		.capabilities		= (void *)&vpci->pci_hdr.msix - (void *)&vpci->pci_hdr,
378 		.bar_size[0]		= IOPORT_SIZE,
379 		.bar_size[1]		= IOPORT_SIZE,
380 		.bar_size[2]		= PCI_IO_SIZE * 2,
381 	};
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_PCI_MAX_VQ entries for virt queue,
392 	 * VIRTIO_PCI_MAX_CONFIG entries for config.
393 	 *
394 	 * To quote the PCI spec:
395 	 *
396 	 * System software reads this field to determine the
397 	 * MSI-X Table Size N, which is encoded as N-1.
398 	 * For example, a returned value of "00000000011"
399 	 * indicates a table size of 4.
400 	 */
401 	vpci->pci_hdr.msix.ctrl = cpu_to_le16(VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG - 1);
402 
403 	/* Both table and PBA are mapped to the same BAR (2) */
404 	vpci->pci_hdr.msix.table_offset = cpu_to_le32(2);
405 	vpci->pci_hdr.msix.pba_offset = cpu_to_le32(2 | PCI_IO_SIZE);
406 	vpci->config_vector = 0;
407 
408 	if (kvm__supports_extension(kvm, KVM_CAP_SIGNAL_MSI))
409 		vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI;
410 
411 	vpci->pci_hdr.irq_pin	= 1;
412 	vpci->pci_hdr.irq_line	= irq__register_device();
413 	r = device__register(&vpci->dev_hdr);
414 	if (r < 0)
415 		goto free_msix_mmio;
416 
417 	return 0;
418 
419 free_msix_mmio:
420 	kvm__deregister_mmio(kvm, vpci->msix_io_block);
421 free_mmio:
422 	kvm__deregister_mmio(kvm, vpci->mmio_addr);
423 free_ioport:
424 	ioport__unregister(kvm, vpci->port_addr);
425 	return r;
426 }
427 
428 int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev)
429 {
430 	struct virtio_pci *vpci = vdev->virtio;
431 	int i;
432 
433 	kvm__deregister_mmio(kvm, vpci->mmio_addr);
434 	kvm__deregister_mmio(kvm, vpci->msix_io_block);
435 	ioport__unregister(kvm, vpci->port_addr);
436 
437 	for (i = 0; i < VIRTIO_PCI_MAX_VQ; i++) {
438 		ioeventfd__del_event(vpci->port_addr + VIRTIO_PCI_QUEUE_NOTIFY, i);
439 		ioeventfd__del_event(vpci->mmio_addr + VIRTIO_PCI_QUEUE_NOTIFY, i);
440 	}
441 
442 	return 0;
443 }
444