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