If VIRTIO_F_SHM_PAGE_SIZE has been negotiated and cfg_type is
VIRTIO_PCI_CAP_SHARED_MEMORY_CFG, the driver must read the page_shift
field, derive the supported page size from it, and honor it when
requesting the map of memory into the shared memory region to the
device.

Extend virtio_pci_cap to hold that field, and use it to feed
virtio_shm_region with the corresponding page size.

Reviewed-by: Dmitry Osipenko <dmitry.osipe...@collabora.com>
Signed-off-by: Sergio Lopez <s...@redhat.com>
---
 drivers/virtio/virtio_mmio.c       |  2 ++
 drivers/virtio/virtio_pci_modern.c | 21 +++++++++++++++++----
 include/linux/virtio_config.h      |  1 +
 include/uapi/linux/virtio_pci.h    |  3 ++-
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/drivers/virtio/virtio_mmio.c b/drivers/virtio/virtio_mmio.c
index 
5d78c2d572abfcfe2b84cdd82df622320fe97d5d..1f594b626d7a7734e8ec58766737a118c26bad94
 100644
--- a/drivers/virtio/virtio_mmio.c
+++ b/drivers/virtio/virtio_mmio.c
@@ -560,6 +560,8 @@ static bool vm_get_shm_region(struct virtio_device *vdev,
 
        region->addr = addr;
 
+       region->page_size = 4096;
+
        return true;
 }
 
diff --git a/drivers/virtio/virtio_pci_modern.c 
b/drivers/virtio/virtio_pci_modern.c
index 
104ba08f82d4a6268240bbad15385dd44b3a71d6..562a8e1c2bfe6876cffabe26f02cd61ad7fea2cd
 100644
--- a/drivers/virtio/virtio_pci_modern.c
+++ b/drivers/virtio/virtio_pci_modern.c
@@ -770,14 +770,15 @@ static void del_vq(struct virtio_pci_vq_info *info)
        vring_del_virtqueue(vq);
 }
 
-static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
-                                  u8 *bar, u64 *offset, u64 *len)
+static int virtio_pci_find_shm_cap(struct virtio_device *vdev, struct pci_dev 
*dev,
+                                  u8 required_id, u8 *bar, u64 *offset, u64 
*len,
+                                  u32 *page_size)
 {
        int pos;
 
        for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
             pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
-               u8 type, cap_len, id, res_bar;
+               u8 type, cap_len, id, res_bar, page_shift = 0;
                u32 tmp32;
                u64 res_offset, res_length;
 
@@ -808,6 +809,15 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 
required_id,
                 * Looks good.
                 */
 
+               /* Read the page shift if supported. The page_shift variable is
+                * initialized to zero above, so if this feature isn't 
supported it
+                * will result in a page_size of 4096, a default safe value.
+                */
+               if (__virtio_test_bit(vdev, VIRTIO_F_SHM_PAGE_SIZE)) {
+                       pci_read_config_byte(dev, pos + offsetof(struct 
virtio_pci_cap,
+                                                                page_shift), 
&page_shift);
+               }
+
                /* Read the lower 32bit of length and offset */
                pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
                                                          offset), &tmp32);
@@ -829,6 +839,7 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 
required_id,
                *bar = res_bar;
                *offset = res_offset;
                *len = res_length;
+               *page_size = 1 << (page_shift + 12);
 
                return pos;
        }
@@ -841,11 +852,12 @@ static bool vp_get_shm_region(struct virtio_device *vdev,
        struct virtio_pci_device *vp_dev = to_vp_device(vdev);
        struct pci_dev *pci_dev = vp_dev->pci_dev;
        u8 bar;
+       u32 page_size;
        u64 offset, len;
        phys_addr_t phys_addr;
        size_t bar_len;
 
-       if (!virtio_pci_find_shm_cap(pci_dev, id, &bar, &offset, &len))
+       if (!virtio_pci_find_shm_cap(vdev, pci_dev, id, &bar, &offset, &len, 
&page_size))
                return false;
 
        phys_addr = pci_resource_start(pci_dev, bar);
@@ -865,6 +877,7 @@ static bool vp_get_shm_region(struct virtio_device *vdev,
 
        region->len = len;
        region->addr = (u64) phys_addr + offset;
+       region->page_size = page_size;
 
        return true;
 }
diff --git a/include/linux/virtio_config.h b/include/linux/virtio_config.h
index 
169c7d367facb36dcabf9596068580ea8b8516c7..c1b2ce71ea55e81978e18db05494deab193fa4fb
 100644
--- a/include/linux/virtio_config.h
+++ b/include/linux/virtio_config.h
@@ -14,6 +14,7 @@ struct irq_affinity;
 struct virtio_shm_region {
        u64 addr;
        u64 len;
+       u32 page_size;
 };
 
 typedef void vq_callback_t(struct virtqueue *);
diff --git a/include/uapi/linux/virtio_pci.h b/include/uapi/linux/virtio_pci.h
index 
8549d4571257142ac6c9dad5c01369923791a85a..1a76df52b4eccf548df78a8ee7d3a04591f55522
 100644
--- a/include/uapi/linux/virtio_pci.h
+++ b/include/uapi/linux/virtio_pci.h
@@ -127,7 +127,8 @@ struct virtio_pci_cap {
        __u8 cfg_type;          /* Identifies the structure. */
        __u8 bar;               /* Where to find it. */
        __u8 id;                /* Multiple capabilities of the same type */
-       __u8 padding[2];        /* Pad to full dword. */
+       __u8 page_shift;        /* Page shift for 
VIRTIO_PCI_CAP_SHARED_MEMORY_CFG. */
+       __u8 padding[1];        /* Pad to full dword. */
        __le32 offset;          /* Offset within bar. */
        __le32 length;          /* Length of the structure, in bytes. */
 };

-- 
2.48.1


Reply via email to