[PATCH v4 2/3] virtio-iommu: Add a granule property

Eric Auger posted 3 patches 9 months, 1 week ago
There is a newer version of this series
[PATCH v4 2/3] virtio-iommu: Add a granule property
Posted by Eric Auger 9 months, 1 week ago
This allows to choose which granule will be used by
default by the virtio-iommu. Current page size mask
default is qemu_target_page_mask so this translates
into a 4K granule.

Signed-off-by: Eric Auger <eric.auger@redhat.com>

---

v3 -> v4:
- granule_mode introduction moved to that patch
---
 include/hw/virtio/virtio-iommu.h |  1 +
 hw/virtio/virtio-iommu.c         | 27 ++++++++++++++++++++++++---
 qemu-options.hx                  |  3 +++
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/include/hw/virtio/virtio-iommu.h b/include/hw/virtio/virtio-iommu.h
index e22327548f..a3e5b35b1e 100644
--- a/include/hw/virtio/virtio-iommu.h
+++ b/include/hw/virtio/virtio-iommu.h
@@ -78,6 +78,7 @@ struct VirtIOIOMMU {
     Notifier machine_done;
     bool granule_frozen;
     uint8_t aw_bits;
+    GranuleMode granule_mode;
 };
 
 #endif
diff --git a/hw/virtio/virtio-iommu.c b/hw/virtio/virtio-iommu.c
index a9bdc03d12..0461b87ef2 100644
--- a/hw/virtio/virtio-iommu.c
+++ b/hw/virtio/virtio-iommu.c
@@ -1126,8 +1126,8 @@ static int virtio_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu_mr,
 }
 
 /*
- * The default mask (TARGET_PAGE_MASK) is the smallest supported guest granule,
- * for example 0xfffffffffffff000. When an assigned device has page size
+ * The default mask depends on the "granule" property. For example, with
+ * 4K granule, it is ~0xFFF. When an assigned device has page size
  * restrictions due to the hardware IOMMU configuration, apply this restriction
  * to the mask.
  */
@@ -1324,7 +1324,26 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
      * in vfio realize
      */
     s->config.bypass = s->boot_bypass;
-    s->config.page_size_mask = qemu_target_page_mask();
+
+    switch (s->granule_mode) {
+    case GRANULE_MODE_4K:
+        s->config.page_size_mask = ~0xFFF;
+        break;
+    case GRANULE_MODE_8K:
+        s->config.page_size_mask = ~0x1FFF;
+        break;
+    case GRANULE_MODE_16K:
+        s->config.page_size_mask = ~0x3FFF;
+        break;
+    case GRANULE_MODE_64K:
+        s->config.page_size_mask = ~0xFFFF;
+        break;
+    case GRANULE_MODE_HOST:
+        s->config.page_size_mask = qemu_real_host_page_mask();
+        break;
+    default:
+        error_setg(errp, "Unsupported granule mode");
+    }
     if (s->aw_bits < 32 || s->aw_bits > 64) {
         error_setg(errp, "aw-bits must be within [32,64]");
     }
@@ -1538,6 +1557,8 @@ static Property virtio_iommu_properties[] = {
                      TYPE_PCI_BUS, PCIBus *),
     DEFINE_PROP_BOOL("boot-bypass", VirtIOIOMMU, boot_bypass, true),
     DEFINE_PROP_UINT8("aw-bits", VirtIOIOMMU, aw_bits, 0),
+    DEFINE_PROP_GRANULE_MODE("granule", VirtIOIOMMU, granule_mode,
+                             GRANULE_MODE_4K),
     DEFINE_PROP_END_OF_LIST(),
 };
 
diff --git a/qemu-options.hx b/qemu-options.hx
index a98bc7bd60..8bc1e9e4aa 100644
--- a/qemu-options.hx
+++ b/qemu-options.hx
@@ -1179,6 +1179,9 @@ SRST
     ``aw-bits=val`` (val between 32 and 64, default depends on machine)
         This decides the address width of IOVA address space. It defaults
         to 39 bits on q35 machines and 48 bits on ARM virt machines.
+    ``granule=val`` (possible values are 4K, 8K, 16K, 64K and host)
+        This decides the default granule to be be exposed by the
+        virtio-iommu. If host, the granule matches the host page size.
 
 ERST
 
-- 
2.41.0
Re: [PATCH v4 2/3] virtio-iommu: Add a granule property
Posted by Philippe Mathieu-Daudé 9 months, 1 week ago
On 23/2/24 08:27, Eric Auger wrote:
> This allows to choose which granule will be used by
> default by the virtio-iommu. Current page size mask
> default is qemu_target_page_mask so this translates
> into a 4K granule.
> 
> Signed-off-by: Eric Auger <eric.auger@redhat.com>
> 
> ---
> 
> v3 -> v4:
> - granule_mode introduction moved to that patch
> ---
>   include/hw/virtio/virtio-iommu.h |  1 +
>   hw/virtio/virtio-iommu.c         | 27 ++++++++++++++++++++++++---
>   qemu-options.hx                  |  3 +++
>   3 files changed, 28 insertions(+), 3 deletions(-)


> @@ -1324,7 +1324,26 @@ static void virtio_iommu_device_realize(DeviceState *dev, Error **errp)
>        * in vfio realize
>        */
>       s->config.bypass = s->boot_bypass;
> -    s->config.page_size_mask = qemu_target_page_mask();
> +
> +    switch (s->granule_mode) {
> +    case GRANULE_MODE_4K:
> +        s->config.page_size_mask = ~0xFFF;

Alternatively:

           s->config.page_size_mask = -(4 * KiB);

> +        break;
> +    case GRANULE_MODE_8K:
> +        s->config.page_size_mask = ~0x1FFF;

           s->config.page_size_mask = -(8 * KiB);

> +        break;
> +    case GRANULE_MODE_16K:
> +        s->config.page_size_mask = ~0x3FFF;

...

> +        break;
> +    case GRANULE_MODE_64K:
> +        s->config.page_size_mask = ~0xFFFF;
> +        break;
> +    case GRANULE_MODE_HOST:
> +        s->config.page_size_mask = qemu_real_host_page_mask();
> +        break;
> +    default:
> +        error_setg(errp, "Unsupported granule mode");
> +    }
Re: [PATCH v4 2/3] virtio-iommu: Add a granule property
Posted by Eric Auger 9 months ago

On 2/23/24 08:57, Philippe Mathieu-Daudé wrote:
> On 23/2/24 08:27, Eric Auger wrote:
>> This allows to choose which granule will be used by
>> default by the virtio-iommu. Current page size mask
>> default is qemu_target_page_mask so this translates
>> into a 4K granule.
>>
>> Signed-off-by: Eric Auger <eric.auger@redhat.com>
>>
>> ---
>>
>> v3 -> v4:
>> - granule_mode introduction moved to that patch
>> ---
>>   include/hw/virtio/virtio-iommu.h |  1 +
>>   hw/virtio/virtio-iommu.c         | 27 ++++++++++++++++++++++++---
>>   qemu-options.hx                  |  3 +++
>>   3 files changed, 28 insertions(+), 3 deletions(-)
>
>
>> @@ -1324,7 +1324,26 @@ static void
>> virtio_iommu_device_realize(DeviceState *dev, Error **errp)
>>        * in vfio realize
>>        */
>>       s->config.bypass = s->boot_bypass;
>> -    s->config.page_size_mask = qemu_target_page_mask();
>> +
>> +    switch (s->granule_mode) {
>> +    case GRANULE_MODE_4K:
>> +        s->config.page_size_mask = ~0xFFF;
>
> Alternatively:
>
>           s->config.page_size_mask = -(4 * KiB);
Yep that's more readable.

Thanks

Eric
>
>> +        break;
>> +    case GRANULE_MODE_8K:
>> +        s->config.page_size_mask = ~0x1FFF;
>
>           s->config.page_size_mask = -(8 * KiB);
>
>> +        break;
>> +    case GRANULE_MODE_16K:
>> +        s->config.page_size_mask = ~0x3FFF;
>
> ...
>
>> +        break;
>> +    case GRANULE_MODE_64K:
>> +        s->config.page_size_mask = ~0xFFFF;
>> +        break;
>> +    case GRANULE_MODE_HOST:
>> +        s->config.page_size_mask = qemu_real_host_page_mask();
>> +        break;
>> +    default:
>> +        error_setg(errp, "Unsupported granule mode");
>> +    }
>