Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

virtio_pci: Use the DMA API if enabled

This switches to vring_create_virtqueue, simplifying the driver and
adding DMA API support.

This fixes virtio-pci on platforms and busses that have IOMMUs. This
will break the experimental QEMU Q35 IOMMU support until QEMU is
fixed. In exchange, it fixes physical virtio hardware as well as
virtio-pci running under Xen.

Signed-off-by: Andy Lutomirski <luto@kernel.org>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

authored by

Andy Lutomirski and committed by
Michael S. Tsirkin
7a5589b2 b4211138

+33 -76
-6
drivers/virtio/virtio_pci_common.h
··· 35 35 /* the actual virtqueue */ 36 36 struct virtqueue *vq; 37 37 38 - /* the number of entries in the queue */ 39 - int num; 40 - 41 - /* the virtual address of the ring queue */ 42 - void *queue; 43 - 44 38 /* the list node for the virtqueues list */ 45 39 struct list_head node; 46 40
+17 -25
drivers/virtio/virtio_pci_legacy.c
··· 119 119 u16 msix_vec) 120 120 { 121 121 struct virtqueue *vq; 122 - unsigned long size; 123 122 u16 num; 124 123 int err; 125 124 ··· 130 131 if (!num || ioread32(vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN)) 131 132 return ERR_PTR(-ENOENT); 132 133 133 - info->num = num; 134 134 info->msix_vector = msix_vec; 135 135 136 - size = PAGE_ALIGN(vring_size(num, VIRTIO_PCI_VRING_ALIGN)); 137 - info->queue = alloc_pages_exact(size, GFP_KERNEL|__GFP_ZERO); 138 - if (info->queue == NULL) 136 + /* create the vring */ 137 + vq = vring_create_virtqueue(index, num, 138 + VIRTIO_PCI_VRING_ALIGN, &vp_dev->vdev, 139 + true, false, vp_notify, callback, name); 140 + if (!vq) 139 141 return ERR_PTR(-ENOMEM); 140 142 141 143 /* activate the queue */ 142 - iowrite32(virt_to_phys(info->queue) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT, 144 + iowrite32(virtqueue_get_desc_addr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT, 143 145 vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); 144 - 145 - /* create the vring */ 146 - vq = vring_new_virtqueue(index, info->num, 147 - VIRTIO_PCI_VRING_ALIGN, &vp_dev->vdev, 148 - true, info->queue, vp_notify, callback, name); 149 - if (!vq) { 150 - err = -ENOMEM; 151 - goto out_activate_queue; 152 - } 153 146 154 147 vq->priv = (void __force *)vp_dev->ioaddr + VIRTIO_PCI_QUEUE_NOTIFY; 155 148 ··· 150 159 msix_vec = ioread16(vp_dev->ioaddr + VIRTIO_MSI_QUEUE_VECTOR); 151 160 if (msix_vec == VIRTIO_MSI_NO_VECTOR) { 152 161 err = -EBUSY; 153 - goto out_assign; 162 + goto out_deactivate; 154 163 } 155 164 } 156 165 157 166 return vq; 158 167 159 - out_assign: 160 - vring_del_virtqueue(vq); 161 - out_activate_queue: 168 + out_deactivate: 162 169 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); 163 - free_pages_exact(info->queue, size); 170 + vring_del_virtqueue(vq); 164 171 return ERR_PTR(err); 165 172 } 166 173 ··· 166 177 { 167 178 struct virtqueue *vq = info->vq; 168 179 struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev); 169 - unsigned long size; 170 180 171 181 iowrite16(vq->index, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_SEL); 172 182 ··· 176 188 ioread8(vp_dev->ioaddr + VIRTIO_PCI_ISR); 177 189 } 178 190 179 - vring_del_virtqueue(vq); 180 - 181 191 /* Select and deactivate the queue */ 182 192 iowrite32(0, vp_dev->ioaddr + VIRTIO_PCI_QUEUE_PFN); 183 193 184 - size = PAGE_ALIGN(vring_size(info->num, VIRTIO_PCI_VRING_ALIGN)); 185 - free_pages_exact(info->queue, size); 194 + vring_del_virtqueue(vq); 186 195 } 187 196 188 197 static const struct virtio_config_ops virtio_pci_config_ops = { ··· 211 226 VIRTIO_PCI_ABI_VERSION, pci_dev->revision); 212 227 return -ENODEV; 213 228 } 229 + 230 + rc = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64)); 231 + if (rc) 232 + rc = dma_set_mask_and_coherent(&pci_dev->dev, 233 + DMA_BIT_MASK(32)); 234 + if (rc) 235 + dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n"); 214 236 215 237 rc = pci_request_region(pci_dev, 0, "virtio-pci-legacy"); 216 238 if (rc)
+16 -45
drivers/virtio/virtio_pci_modern.c
··· 287 287 return vp_ioread16(&vp_dev->common->msix_config); 288 288 } 289 289 290 - static size_t vring_pci_size(u16 num) 291 - { 292 - /* We only need a cacheline separation. */ 293 - return PAGE_ALIGN(vring_size(num, SMP_CACHE_BYTES)); 294 - } 295 - 296 - static void *alloc_virtqueue_pages(int *num) 297 - { 298 - void *pages; 299 - 300 - /* TODO: allocate each queue chunk individually */ 301 - for (; *num && vring_pci_size(*num) > PAGE_SIZE; *num /= 2) { 302 - pages = alloc_pages_exact(vring_pci_size(*num), 303 - GFP_KERNEL|__GFP_ZERO|__GFP_NOWARN); 304 - if (pages) 305 - return pages; 306 - } 307 - 308 - if (!*num) 309 - return NULL; 310 - 311 - /* Try to get a single page. You are my only hope! */ 312 - return alloc_pages_exact(vring_pci_size(*num), GFP_KERNEL|__GFP_ZERO); 313 - } 314 - 315 290 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev, 316 291 struct virtio_pci_vq_info *info, 317 292 unsigned index, ··· 318 343 /* get offset of notification word for this vq */ 319 344 off = vp_ioread16(&cfg->queue_notify_off); 320 345 321 - info->num = num; 322 346 info->msix_vector = msix_vec; 323 347 324 - info->queue = alloc_virtqueue_pages(&info->num); 325 - if (info->queue == NULL) 348 + /* create the vring */ 349 + vq = vring_create_virtqueue(index, num, 350 + SMP_CACHE_BYTES, &vp_dev->vdev, 351 + true, true, vp_notify, callback, name); 352 + if (!vq) 326 353 return ERR_PTR(-ENOMEM); 327 354 328 - /* create the vring */ 329 - vq = vring_new_virtqueue(index, info->num, 330 - SMP_CACHE_BYTES, &vp_dev->vdev, 331 - true, info->queue, vp_notify, callback, name); 332 - if (!vq) { 333 - err = -ENOMEM; 334 - goto err_new_queue; 335 - } 336 - 337 355 /* activate the queue */ 338 - vp_iowrite16(num, &cfg->queue_size); 339 - vp_iowrite64_twopart(virt_to_phys(info->queue), 356 + vp_iowrite16(virtqueue_get_vring_size(vq), &cfg->queue_size); 357 + vp_iowrite64_twopart(virtqueue_get_desc_addr(vq), 340 358 &cfg->queue_desc_lo, &cfg->queue_desc_hi); 341 - vp_iowrite64_twopart(virt_to_phys(virtqueue_get_avail(vq)), 359 + vp_iowrite64_twopart(virtqueue_get_avail_addr(vq), 342 360 &cfg->queue_avail_lo, &cfg->queue_avail_hi); 343 - vp_iowrite64_twopart(virt_to_phys(virtqueue_get_used(vq)), 361 + vp_iowrite64_twopart(virtqueue_get_used_addr(vq), 344 362 &cfg->queue_used_lo, &cfg->queue_used_hi); 345 363 346 364 if (vp_dev->notify_base) { ··· 378 410 pci_iounmap(vp_dev->pci_dev, (void __iomem __force *)vq->priv); 379 411 err_map_notify: 380 412 vring_del_virtqueue(vq); 381 - err_new_queue: 382 - free_pages_exact(info->queue, vring_pci_size(info->num)); 383 413 return ERR_PTR(err); 384 414 } 385 415 ··· 422 456 pci_iounmap(vp_dev->pci_dev, (void __force __iomem *)vq->priv); 423 457 424 458 vring_del_virtqueue(vq); 425 - 426 - free_pages_exact(info->queue, vring_pci_size(info->num)); 427 459 } 428 460 429 461 static const struct virtio_config_ops virtio_pci_config_nodev_ops = { ··· 604 640 common, isr, notify); 605 641 return -EINVAL; 606 642 } 643 + 644 + err = dma_set_mask_and_coherent(&pci_dev->dev, DMA_BIT_MASK(64)); 645 + if (err) 646 + err = dma_set_mask_and_coherent(&pci_dev->dev, 647 + DMA_BIT_MASK(32)); 648 + if (err) 649 + dev_warn(&pci_dev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n"); 607 650 608 651 /* Device capability is only mandatory for devices that have 609 652 * device-specific configuration.