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 #include "kvm/virtio-trans.h" 10 11 #include <linux/virtio_pci.h> 12 #include <linux/byteorder.h> 13 #include <string.h> 14 15 struct virtio_trans_ops *virtio_pci__get_trans_ops(void) 16 { 17 static struct virtio_trans_ops virtio_pci_trans = (struct virtio_trans_ops) { 18 .signal_vq = virtio_pci__signal_vq, 19 .signal_config = virtio_pci__signal_config, 20 .init = virtio_pci__init, 21 }; 22 return &virtio_pci_trans; 23 }; 24 25 static void virtio_pci__ioevent_callback(struct kvm *kvm, void *param) 26 { 27 struct virtio_pci_ioevent_param *ioeventfd = param; 28 struct virtio_pci *vpci = ioeventfd->vtrans->virtio; 29 30 ioeventfd->vtrans->virtio_ops->notify_vq(kvm, vpci->dev, ioeventfd->vq); 31 } 32 33 static int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_trans *vtrans, u32 vq) 34 { 35 struct ioevent ioevent; 36 struct virtio_pci *vpci = vtrans->virtio; 37 38 vpci->ioeventfds[vq] = (struct virtio_pci_ioevent_param) { 39 .vtrans = vtrans, 40 .vq = vq, 41 }; 42 43 ioevent = (struct ioevent) { 44 .io_addr = vpci->base_addr + VIRTIO_PCI_QUEUE_NOTIFY, 45 .io_len = sizeof(u16), 46 .fn = virtio_pci__ioevent_callback, 47 .fn_ptr = &vpci->ioeventfds[vq], 48 .datamatch = vq, 49 .fn_kvm = kvm, 50 .fd = eventfd(0, 0), 51 }; 52 53 ioeventfd__add_event(&ioevent); 54 55 if (vtrans->virtio_ops->notify_vq_eventfd) 56 vtrans->virtio_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_trans *vtrans, u16 port, 67 void *data, int size, int offset) 68 { 69 u32 config_offset; 70 struct virtio_pci *vpci = vtrans->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 = vtrans->virtio_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_trans *vtrans; 101 struct virtio_pci *vpci; 102 u32 val; 103 104 vtrans = ioport->priv; 105 vpci = vtrans->virtio; 106 offset = port - vpci->base_addr; 107 108 switch (offset) { 109 case VIRTIO_PCI_HOST_FEATURES: 110 val = vtrans->virtio_ops->get_host_features(kvm, vpci->dev); 111 ioport__write32(data, val); 112 break; 113 case VIRTIO_PCI_QUEUE_PFN: 114 val = vtrans->virtio_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 = vtrans->virtio_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, vtrans, 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_trans *vtrans, u16 port, 138 void *data, int size, int offset) 139 { 140 struct virtio_pci *vpci = vtrans->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 (vtrans->virtio_ops->notify_vq_gsi) 159 vtrans->virtio_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 vtrans->virtio_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_trans *vtrans; 179 struct virtio_pci *vpci; 180 u32 val; 181 182 vtrans = ioport->priv; 183 vpci = vtrans->virtio; 184 offset = port - vpci->base_addr; 185 186 switch (offset) { 187 case VIRTIO_PCI_GUEST_FEATURES: 188 val = ioport__read32(data); 189 vtrans->virtio_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, vtrans, vpci->queue_selector); 194 vtrans->virtio_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 vtrans->virtio_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, vtrans, 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 callback_mmio_table(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_trans *vtrans, u32 vq) 240 { 241 struct virtio_pci *vpci = vtrans->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_trans *vtrans) 261 { 262 struct virtio_pci *vpci = vtrans->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, struct virtio_trans *vtrans, void *dev, 283 int device_id, int subsys_id, int class) 284 { 285 struct virtio_pci *vpci = vtrans->virtio; 286 u8 pin, line, ndev; 287 288 vpci->dev = dev; 289 vpci->msix_io_block = pci_get_io_space_block(PCI_IO_SIZE * 2); 290 291 vpci->base_addr = ioport__register(IOPORT_EMPTY, &virtio_pci__io_ops, IOPORT_SIZE, vtrans); 292 kvm__register_mmio(kvm, vpci->msix_io_block, PCI_IO_SIZE, false, callback_mmio_table, vpci); 293 294 vpci->pci_hdr = (struct pci_device_header) { 295 .vendor_id = cpu_to_le16(PCI_VENDOR_ID_REDHAT_QUMRANET), 296 .device_id = cpu_to_le16(device_id), 297 .header_type = PCI_HEADER_TYPE_NORMAL, 298 .revision_id = 0, 299 .class[0] = class & 0xff, 300 .class[1] = (class >> 8) & 0xff, 301 .class[2] = (class >> 16) & 0xff, 302 .subsys_vendor_id = cpu_to_le16(PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET), 303 .subsys_id = cpu_to_le16(subsys_id), 304 .bar[0] = cpu_to_le32(vpci->base_addr 305 | PCI_BASE_ADDRESS_SPACE_IO), 306 .bar[1] = cpu_to_le32(vpci->msix_io_block 307 | PCI_BASE_ADDRESS_SPACE_MEMORY), 308 .status = cpu_to_le16(PCI_STATUS_CAP_LIST), 309 .capabilities = (void *)&vpci->pci_hdr.msix - (void *)&vpci->pci_hdr, 310 .bar_size[0] = IOPORT_SIZE, 311 .bar_size[1] = PCI_IO_SIZE, 312 .bar_size[3] = PCI_IO_SIZE, 313 }; 314 315 vpci->pci_hdr.msix.cap = PCI_CAP_ID_MSIX; 316 vpci->pci_hdr.msix.next = 0; 317 /* 318 * We at most have VIRTIO_PCI_MAX_VQ entries for virt queue, 319 * VIRTIO_PCI_MAX_CONFIG entries for config. 320 * 321 * To quote the PCI spec: 322 * 323 * System software reads this field to determine the 324 * MSI-X Table Size N, which is encoded as N-1. 325 * For example, a returned value of "00000000011" 326 * indicates a table size of 4. 327 */ 328 vpci->pci_hdr.msix.ctrl = cpu_to_le16(VIRTIO_PCI_MAX_VQ + VIRTIO_PCI_MAX_CONFIG - 1); 329 330 /* 331 * Both table and PBA could be mapped on the same BAR, but for now 332 * we're not in short of BARs 333 */ 334 vpci->pci_hdr.msix.table_offset = cpu_to_le32(1); /* Use BAR 1 */ 335 vpci->pci_hdr.msix.pba_offset = cpu_to_le32(1 | PCI_IO_SIZE); /* Use BAR 3 */ 336 vpci->config_vector = 0; 337 338 if (irq__register_device(subsys_id, &ndev, &pin, &line) < 0) 339 return -1; 340 341 vpci->pci_hdr.irq_pin = pin; 342 vpci->pci_hdr.irq_line = line; 343 pci__register(&vpci->pci_hdr, ndev); 344 345 return 0; 346 } 347