Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Virtio PCI driver - legacy device support
0004  *
0005  * This module allows virtio devices to be used over a virtual PCI device.
0006  * This can be used with QEMU based VMMs like KVM or Xen.
0007  *
0008  * Copyright IBM Corp. 2007
0009  * Copyright Red Hat, Inc. 2014
0010  *
0011  * Authors:
0012  *  Anthony Liguori  <aliguori@us.ibm.com>
0013  *  Rusty Russell <rusty@rustcorp.com.au>
0014  *  Michael S. Tsirkin <mst@redhat.com>
0015  */
0016 
0017 #include "linux/virtio_pci_legacy.h"
0018 #include "virtio_pci_common.h"
0019 
0020 /* virtio config->get_features() implementation */
0021 static u64 vp_get_features(struct virtio_device *vdev)
0022 {
0023     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0024 
0025     /* When someone needs more than 32 feature bits, we'll need to
0026      * steal a bit to indicate that the rest are somewhere else. */
0027     return vp_legacy_get_features(&vp_dev->ldev);
0028 }
0029 
0030 /* virtio config->finalize_features() implementation */
0031 static int vp_finalize_features(struct virtio_device *vdev)
0032 {
0033     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0034 
0035     /* Give virtio_ring a chance to accept features. */
0036     vring_transport_features(vdev);
0037 
0038     /* Make sure we don't have any features > 32 bits! */
0039     BUG_ON((u32)vdev->features != vdev->features);
0040 
0041     /* We only support 32 feature bits. */
0042     vp_legacy_set_features(&vp_dev->ldev, vdev->features);
0043 
0044     return 0;
0045 }
0046 
0047 /* virtio config->get() implementation */
0048 static void vp_get(struct virtio_device *vdev, unsigned int offset,
0049            void *buf, unsigned int len)
0050 {
0051     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0052     void __iomem *ioaddr = vp_dev->ldev.ioaddr +
0053             VIRTIO_PCI_CONFIG_OFF(vp_dev->msix_enabled) +
0054             offset;
0055     u8 *ptr = buf;
0056     int i;
0057 
0058     for (i = 0; i < len; i++)
0059         ptr[i] = ioread8(ioaddr + i);
0060 }
0061 
0062 /* the config->set() implementation.  it's symmetric to the config->get()
0063  * implementation */
0064 static void vp_set(struct virtio_device *vdev, unsigned int offset,
0065            const void *buf, unsigned int len)
0066 {
0067     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0068     void __iomem *ioaddr = vp_dev->ldev.ioaddr +
0069             VIRTIO_PCI_CONFIG_OFF(vp_dev->msix_enabled) +
0070             offset;
0071     const u8 *ptr = buf;
0072     int i;
0073 
0074     for (i = 0; i < len; i++)
0075         iowrite8(ptr[i], ioaddr + i);
0076 }
0077 
0078 /* config->{get,set}_status() implementations */
0079 static u8 vp_get_status(struct virtio_device *vdev)
0080 {
0081     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0082     return vp_legacy_get_status(&vp_dev->ldev);
0083 }
0084 
0085 static void vp_set_status(struct virtio_device *vdev, u8 status)
0086 {
0087     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0088     /* We should never be setting status to 0. */
0089     BUG_ON(status == 0);
0090     vp_legacy_set_status(&vp_dev->ldev, status);
0091 }
0092 
0093 static void vp_reset(struct virtio_device *vdev)
0094 {
0095     struct virtio_pci_device *vp_dev = to_vp_device(vdev);
0096     /* 0 status means a reset. */
0097     vp_legacy_set_status(&vp_dev->ldev, 0);
0098     /* Flush out the status write, and flush in device writes,
0099      * including MSi-X interrupts, if any. */
0100     vp_legacy_get_status(&vp_dev->ldev);
0101     /* Flush pending VQ/configuration callbacks. */
0102     vp_synchronize_vectors(vdev);
0103 }
0104 
0105 static u16 vp_config_vector(struct virtio_pci_device *vp_dev, u16 vector)
0106 {
0107     return vp_legacy_config_vector(&vp_dev->ldev, vector);
0108 }
0109 
0110 static struct virtqueue *setup_vq(struct virtio_pci_device *vp_dev,
0111                   struct virtio_pci_vq_info *info,
0112                   unsigned int index,
0113                   void (*callback)(struct virtqueue *vq),
0114                   const char *name,
0115                   bool ctx,
0116                   u16 msix_vec)
0117 {
0118     struct virtqueue *vq;
0119     u16 num;
0120     int err;
0121     u64 q_pfn;
0122 
0123     /* Check if queue is either not available or already active. */
0124     num = vp_legacy_get_queue_size(&vp_dev->ldev, index);
0125     if (!num || vp_legacy_get_queue_enable(&vp_dev->ldev, index))
0126         return ERR_PTR(-ENOENT);
0127 
0128     info->msix_vector = msix_vec;
0129 
0130     /* create the vring */
0131     vq = vring_create_virtqueue(index, num,
0132                     VIRTIO_PCI_VRING_ALIGN, &vp_dev->vdev,
0133                     true, false, ctx,
0134                     vp_notify, callback, name);
0135     if (!vq)
0136         return ERR_PTR(-ENOMEM);
0137 
0138     vq->num_max = num;
0139 
0140     q_pfn = virtqueue_get_desc_addr(vq) >> VIRTIO_PCI_QUEUE_ADDR_SHIFT;
0141     if (q_pfn >> 32) {
0142         dev_err(&vp_dev->pci_dev->dev,
0143             "platform bug: legacy virtio-pci must not be used with RAM above 0x%llxGB\n",
0144             0x1ULL << (32 + PAGE_SHIFT - 30));
0145         err = -E2BIG;
0146         goto out_del_vq;
0147     }
0148 
0149     /* activate the queue */
0150     vp_legacy_set_queue_address(&vp_dev->ldev, index, q_pfn);
0151 
0152     vq->priv = (void __force *)vp_dev->ldev.ioaddr + VIRTIO_PCI_QUEUE_NOTIFY;
0153 
0154     if (msix_vec != VIRTIO_MSI_NO_VECTOR) {
0155         msix_vec = vp_legacy_queue_vector(&vp_dev->ldev, index, msix_vec);
0156         if (msix_vec == VIRTIO_MSI_NO_VECTOR) {
0157             err = -EBUSY;
0158             goto out_deactivate;
0159         }
0160     }
0161 
0162     return vq;
0163 
0164 out_deactivate:
0165     vp_legacy_set_queue_address(&vp_dev->ldev, index, 0);
0166 out_del_vq:
0167     vring_del_virtqueue(vq);
0168     return ERR_PTR(err);
0169 }
0170 
0171 static void del_vq(struct virtio_pci_vq_info *info)
0172 {
0173     struct virtqueue *vq = info->vq;
0174     struct virtio_pci_device *vp_dev = to_vp_device(vq->vdev);
0175 
0176     if (vp_dev->msix_enabled) {
0177         vp_legacy_queue_vector(&vp_dev->ldev, vq->index,
0178                 VIRTIO_MSI_NO_VECTOR);
0179         /* Flush the write out to device */
0180         ioread8(vp_dev->ldev.ioaddr + VIRTIO_PCI_ISR);
0181     }
0182 
0183     /* Select and deactivate the queue */
0184     vp_legacy_set_queue_address(&vp_dev->ldev, vq->index, 0);
0185 
0186     vring_del_virtqueue(vq);
0187 }
0188 
0189 static const struct virtio_config_ops virtio_pci_config_ops = {
0190     .get        = vp_get,
0191     .set        = vp_set,
0192     .get_status = vp_get_status,
0193     .set_status = vp_set_status,
0194     .reset      = vp_reset,
0195     .find_vqs   = vp_find_vqs,
0196     .del_vqs    = vp_del_vqs,
0197     .synchronize_cbs = vp_synchronize_vectors,
0198     .get_features   = vp_get_features,
0199     .finalize_features = vp_finalize_features,
0200     .bus_name   = vp_bus_name,
0201     .set_vq_affinity = vp_set_vq_affinity,
0202     .get_vq_affinity = vp_get_vq_affinity,
0203 };
0204 
0205 /* the PCI probing function */
0206 int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev)
0207 {
0208     struct virtio_pci_legacy_device *ldev = &vp_dev->ldev;
0209     struct pci_dev *pci_dev = vp_dev->pci_dev;
0210     int rc;
0211 
0212     ldev->pci_dev = pci_dev;
0213 
0214     rc = vp_legacy_probe(ldev);
0215     if (rc)
0216         return rc;
0217 
0218     vp_dev->isr = ldev->isr;
0219     vp_dev->vdev.id = ldev->id;
0220 
0221     vp_dev->vdev.config = &virtio_pci_config_ops;
0222 
0223     vp_dev->config_vector = vp_config_vector;
0224     vp_dev->setup_vq = setup_vq;
0225     vp_dev->del_vq = del_vq;
0226 
0227     return 0;
0228 }
0229 
0230 void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev)
0231 {
0232     struct virtio_pci_legacy_device *ldev = &vp_dev->ldev;
0233 
0234     vp_legacy_remove(ldev);
0235 }