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 if (vdev->ops->notify_status) 214 vdev->ops->notify_status(kvm, vpci->dev, vpci->status); 215 break; 216 default: 217 ret = virtio_pci__specific_io_out(kvm, vdev, port, data, size, offset); 218 break; 219 }; 220 221 return ret; 222 } 223 224 static struct ioport_operations virtio_pci__io_ops = { 225 .io_in = virtio_pci__io_in, 226 .io_out = virtio_pci__io_out, 227 }; 228 229 static void virtio_pci__mmio_callback(u64 addr, u8 *data, u32 len, u8 is_write, void *ptr) 230 { 231 struct virtio_pci *vpci = ptr; 232 void *table; 233 u32 offset; 234 235 if (addr > vpci->msix_io_block + PCI_IO_SIZE) { 236 table = &vpci->msix_pba; 237 offset = vpci->msix_io_block + PCI_IO_SIZE; 238 } else { 239 table = &vpci->msix_table; 240 offset = vpci->msix_io_block; 241 } 242 243 if (is_write) 244 memcpy(table + addr - offset, data, len); 245 else 246 memcpy(data, table + addr - offset, len); 247 } 248 249 static void virtio_pci__signal_msi(struct kvm *kvm, struct virtio_pci *vpci, int vec) 250 { 251 struct kvm_msi msi = { 252 .address_lo = vpci->msix_table[vec].msg.address_lo, 253 .address_hi = vpci->msix_table[vec].msg.address_hi, 254 .data = vpci->msix_table[vec].msg.data, 255 }; 256 257 ioctl(kvm->vm_fd, KVM_SIGNAL_MSI, &msi); 258 } 259 260 int virtio_pci__signal_vq(struct kvm *kvm, struct virtio_device *vdev, u32 vq) 261 { 262 struct virtio_pci *vpci = vdev->virtio; 263 int tbl = vpci->vq_vector[vq]; 264 265 if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) { 266 if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) || 267 vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) { 268 269 vpci->msix_pba |= 1 << tbl; 270 return 0; 271 } 272 273 if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI) 274 virtio_pci__signal_msi(kvm, vpci, vpci->vq_vector[vq]); 275 else 276 kvm__irq_trigger(kvm, vpci->gsis[vq]); 277 } else { 278 vpci->isr = VIRTIO_IRQ_HIGH; 279 kvm__irq_trigger(kvm, vpci->pci_hdr.irq_line); 280 } 281 return 0; 282 } 283 284 int virtio_pci__signal_config(struct kvm *kvm, struct virtio_device *vdev) 285 { 286 struct virtio_pci *vpci = vdev->virtio; 287 int tbl = vpci->config_vector; 288 289 if (virtio_pci__msix_enabled(vpci) && tbl != VIRTIO_MSI_NO_VECTOR) { 290 if (vpci->pci_hdr.msix.ctrl & cpu_to_le16(PCI_MSIX_FLAGS_MASKALL) || 291 vpci->msix_table[tbl].ctrl & cpu_to_le16(PCI_MSIX_ENTRY_CTRL_MASKBIT)) { 292 293 vpci->msix_pba |= 1 << tbl; 294 return 0; 295 } 296 297 if (vpci->features & VIRTIO_PCI_F_SIGNAL_MSI) 298 virtio_pci__signal_msi(kvm, vpci, tbl); 299 else 300 kvm__irq_trigger(kvm, vpci->config_gsi); 301 } else { 302 vpci->isr = VIRTIO_PCI_ISR_CONFIG; 303 kvm__irq_trigger(kvm, vpci->pci_hdr.irq_line); 304 } 305 306 return 0; 307 } 308 309 int virtio_pci__init(struct kvm *kvm, void *dev, struct virtio_device *vdev, 310 int device_id, int subsys_id, int class) 311 { 312 struct virtio_pci *vpci = vdev->virtio; 313 u8 pin, line; 314 int r; 315 316 vpci->dev = dev; 317 vpci->msix_io_block = pci_get_io_space_block(PCI_IO_SIZE * 2); 318 319 r = ioport__register(kvm, IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vdev); 320 if (r < 0) 321 return r; 322 323 vpci->base_addr = (u16)r; 324 r = kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE, false, 325 virtio_pci__mmio_callback, vpci); 326 if (r < 0) 327 goto free_ioport; 328 329 vpci->pci_hdr = (struct pci_device_header) { 330 .vendor_id = cpu_to_le16(PCI_VENDOR_ID_REDHAT_QUMRANET), 331 .device_id = cpu_to_le16(device_id), 332 .header_type = PCI_HEADER_TYPE_NORMAL, 333 .revision_id = 0, 334 .class[0] = class & 0xff, 335 .class[1] = (class >> 8) & 0xff, 336 .class[2] = (class >> 16) & 0xff, 337 .subsys_vendor_id = cpu_to_le16(PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET), 338 .subsys_id = cpu_to_le16(subsys_id), 339 .bar[0] = cpu_to_le32(vpci->base_addr 340 | PCI_BASE_ADDRESS_SPACE_IO), 341 .bar[1] = cpu_to_le32(vpci->msix_io_block 342 | PCI_BASE_ADDRESS_SPACE_MEMORY), 343 .status = cpu_to_le16(PCI_STATUS_CAP_LIST), 344 .capabilities = (void *)&vpci->pci_hdr.msix - (void *)&vpci->pci_hdr, 345 .bar_size[0] = IOPORT_SIZE, 346 .bar_size[1] = PCI_IO_SIZE, 347 .bar_size[3] = PCI_IO_SIZE, 348 }; 349 350 vpci->dev_hdr = (struct device_header) { 351 .bus_type = DEVICE_BUS_PCI, 352 .data = &vpci->pci_hdr, 353 }; 354 355 vpci->pci_hdr.msix.cap = PCI_CAP_ID_MSIX; 356 vpci->pci_hdr.msix.next = 0; 357 /* 358 * We at most have VIRTIO_PCI_MAX_VQ entries for virt queue, 359 * VIRTIO_PCI_MAX_CONFIG entries for config. 360 * 361 * To quote the PCI spec: 362 * 363 * System software reads this field to determine the 364 * MSI-X Table Size N, which is encoded as N-1. 365 * For example, a returned value of "00000000011" 366 * indicates a table size of 4. 367 */ 368 vpci->pci_hdr.msix.ctrl = cpu_to_le16(VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG - 1); 369 370 /* 371 * Both table and PBA could be mapped on the same BAR, but for now 372 * we're not in short of BARs 373 */ 374 vpci->pci_hdr.msix.table_offset = cpu_to_le32(1); /* Use BAR 1 */ 375 vpci->pci_hdr.msix.pba_offset = cpu_to_le32(1 | PCI_IO_SIZE); /* Use BAR 3 */ 376 vpci->config_vector = 0; 377 378 r = irq__register_device(subsys_id, &pin, &line); 379 if (r < 0) 380 goto free_mmio; 381 382 if (kvm__supports_extension(kvm, KVM_CAP_SIGNAL_MSI)) 383 vpci->features |= VIRTIO_PCI_F_SIGNAL_MSI; 384 385 vpci->pci_hdr.irq_pin = pin; 386 vpci->pci_hdr.irq_line = line; 387 r = device__register(&vpci->dev_hdr); 388 if (r < 0) 389 goto free_ioport; 390 391 return 0; 392 393 free_mmio: 394 kvm__deregister_mmio(kvm, vpci->msix_io_block); 395 free_ioport: 396 ioport__unregister(kvm, vpci->base_addr); 397 return r; 398 } 399 400 int virtio_pci__exit(struct kvm *kvm, struct virtio_device *vdev) 401 { 402 struct virtio_pci *vpci = vdev->virtio; 403 int i; 404 405 kvm__deregister_mmio(kvm, vpci->msix_io_block); 406 ioport__unregister(kvm, vpci->base_addr); 407 408 for (i = 0; i < VIRTIO_PCI_MAX_VQ; i++) 409 ioeventfd__del_event(vpci->base_addr + VIRTIO_PCI_QUEUE_NOTIFY, i); 410 411 return 0; 412 } 413