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