[PATCH v2] virtio: Add support for no-reset virtio PCI PM

David Stevens posted 1 patch 2 years ago
drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
1 file changed, 33 insertions(+), 1 deletion(-)
[PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by David Stevens 2 years ago
If a virtio_pci_device supports native PCI power management and has the
No_Soft_Reset bit set, then skip resetting and reinitializing the device
when suspending and restoring the device. This allows system-wide low
power states like s2idle to be used in systems with stateful virtio
devices that can't simply be re-initialized (e.g. virtio-fs).

Signed-off-by: David Stevens <stevensd@chromium.org>
---
v1 -> v2:
 - Check the No_Soft_Reset bit

 drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
 1 file changed, 33 insertions(+), 1 deletion(-)

diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
index c2524a7207cf..3a95ecaf12dc 100644
--- a/drivers/virtio/virtio_pci_common.c
+++ b/drivers/virtio/virtio_pci_common.c
@@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
 	return virtio_device_restore(&vp_dev->vdev);
 }
 
+static bool vp_supports_pm_no_reset(struct device *dev)
+{
+	struct pci_dev *pci_dev = to_pci_dev(dev);
+	u16 pmcsr;
+
+	if (!pci_dev->pm_cap)
+		return false;
+
+	pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
+	if (PCI_POSSIBLE_ERROR(pmcsr)) {
+		dev_err(dev, "Unable to query pmcsr");
+		return false;
+	}
+
+	return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
+}
+
+static int virtio_pci_suspend(struct device *dev)
+{
+	return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
+}
+
+static int virtio_pci_resume(struct device *dev)
+{
+	return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
+}
+
 static const struct dev_pm_ops virtio_pci_pm_ops = {
-	SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
+	.suspend = virtio_pci_suspend,
+	.resume = virtio_pci_resume,
+	.freeze = virtio_pci_freeze,
+	.thaw = virtio_pci_restore,
+	.poweroff = virtio_pci_freeze,
+	.restore = virtio_pci_restore,
 };
 #endif
 
-- 
2.43.0.472.g3155946c3a-goog
Re: [PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by David Hildenbrand 2 years ago
On 08.12.23 08:07, David Stevens wrote:
> If a virtio_pci_device supports native PCI power management and has the
> No_Soft_Reset bit set, then skip resetting and reinitializing the device
> when suspending and restoring the device. This allows system-wide low
> power states like s2idle to be used in systems with stateful virtio
> devices that can't simply be re-initialized (e.g. virtio-fs).
> 
> Signed-off-by: David Stevens <stevensd@chromium.org>
> ---
> v1 -> v2:
>   - Check the No_Soft_Reset bit
> 
>   drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
>   1 file changed, 33 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
> index c2524a7207cf..3a95ecaf12dc 100644
> --- a/drivers/virtio/virtio_pci_common.c
> +++ b/drivers/virtio/virtio_pci_common.c
> @@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
>   	return virtio_device_restore(&vp_dev->vdev);
>   }
>   
> +static bool vp_supports_pm_no_reset(struct device *dev)
> +{
> +	struct pci_dev *pci_dev = to_pci_dev(dev);
> +	u16 pmcsr;
> +
> +	if (!pci_dev->pm_cap)
> +		return false;
> +
> +	pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
> +	if (PCI_POSSIBLE_ERROR(pmcsr)) {
> +		dev_err(dev, "Unable to query pmcsr");
> +		return false;
> +	}
> +
> +	return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
> +}
> +
> +static int virtio_pci_suspend(struct device *dev)
> +{
> +	return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
> +}
> +
> +static int virtio_pci_resume(struct device *dev)
> +{
> +	return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
> +}
> +
>   static const struct dev_pm_ops virtio_pci_pm_ops = {
> -	SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
> +	.suspend = virtio_pci_suspend,
> +	.resume = virtio_pci_resume,
> +	.freeze = virtio_pci_freeze,
> +	.thaw = virtio_pci_restore,
> +	.poweroff = virtio_pci_freeze,
> +	.restore = virtio_pci_restore,
>   };
>   #endif
>   

Am I correct with my assumption that this will make s2idle work with virtio-mem-pci as well?

Right now, all suspending is disabled, but really only s4/hibernate is problematic.

[root@vm-0 ~]# echo "s2idle" > /sys/power/mem_sleep
[root@vm-0 ~]# echo "mem" > /sys/power/state
[  101.822991] PM: suspend entry (s2idle)
[  101.828978] Filesystems sync: 0.004 seconds
[  101.831618] Freezing user space processes
[  101.834569] Freezing user space processes completed (elapsed 0.001 seconds)
[  101.836915] OOM killer disabled.
[  101.838072] Freezing remaining freezable tasks
[  101.841054] Freezing remaining freezable tasks completed (elapsed 0.001 seconds)
[  101.843538] printk: Suspending console(s) (use no_console_suspend to debug)
[  101.957676] virtio_mem virtio0: save/restore not supported.
[  101.957683] virtio-pci 0000:00:02.0: PM: pci_pm_suspend(): virtio_pci_freeze+0x0/0x50 returns -1
[  101.957702] virtio-pci 0000:00:02.0: PM: dpm_run_callback(): pci_pm_suspend+0x0/0x170 returns -1
[  101.957718] virtio-pci 0000:00:02.0: PM: failed to suspend async: error -1


-- 
Cheers,

David / dhildenb
Re: [PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by David Stevens 2 years ago
On Thu, Dec 14, 2023 at 6:47 PM David Hildenbrand <david@redhat.com> wrote:
>
> On 08.12.23 08:07, David Stevens wrote:
> > If a virtio_pci_device supports native PCI power management and has the
> > No_Soft_Reset bit set, then skip resetting and reinitializing the device
> > when suspending and restoring the device. This allows system-wide low
> > power states like s2idle to be used in systems with stateful virtio
> > devices that can't simply be re-initialized (e.g. virtio-fs).
> >
> > Signed-off-by: David Stevens <stevensd@chromium.org>
> > ---
> > v1 -> v2:
> >   - Check the No_Soft_Reset bit
> >
> >   drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
> >   1 file changed, 33 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
> > index c2524a7207cf..3a95ecaf12dc 100644
> > --- a/drivers/virtio/virtio_pci_common.c
> > +++ b/drivers/virtio/virtio_pci_common.c
> > @@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
> >       return virtio_device_restore(&vp_dev->vdev);
> >   }
> >
> > +static bool vp_supports_pm_no_reset(struct device *dev)
> > +{
> > +     struct pci_dev *pci_dev = to_pci_dev(dev);
> > +     u16 pmcsr;
> > +
> > +     if (!pci_dev->pm_cap)
> > +             return false;
> > +
> > +     pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
> > +     if (PCI_POSSIBLE_ERROR(pmcsr)) {
> > +             dev_err(dev, "Unable to query pmcsr");
> > +             return false;
> > +     }
> > +
> > +     return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
> > +}
> > +
> > +static int virtio_pci_suspend(struct device *dev)
> > +{
> > +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
> > +}
> > +
> > +static int virtio_pci_resume(struct device *dev)
> > +{
> > +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
> > +}
> > +
> >   static const struct dev_pm_ops virtio_pci_pm_ops = {
> > -     SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
> > +     .suspend = virtio_pci_suspend,
> > +     .resume = virtio_pci_resume,
> > +     .freeze = virtio_pci_freeze,
> > +     .thaw = virtio_pci_restore,
> > +     .poweroff = virtio_pci_freeze,
> > +     .restore = virtio_pci_restore,
> >   };
> >   #endif
> >
>
> Am I correct with my assumption that this will make s2idle work with virtio-mem-pci as well?
>
> Right now, all suspending is disabled, but really only s4/hibernate is problematic.
>
> [root@vm-0 ~]# echo "s2idle" > /sys/power/mem_sleep
> [root@vm-0 ~]# echo "mem" > /sys/power/state
> [  101.822991] PM: suspend entry (s2idle)
> [  101.828978] Filesystems sync: 0.004 seconds
> [  101.831618] Freezing user space processes
> [  101.834569] Freezing user space processes completed (elapsed 0.001 seconds)
> [  101.836915] OOM killer disabled.
> [  101.838072] Freezing remaining freezable tasks
> [  101.841054] Freezing remaining freezable tasks completed (elapsed 0.001 seconds)
> [  101.843538] printk: Suspending console(s) (use no_console_suspend to debug)
> [  101.957676] virtio_mem virtio0: save/restore not supported.
> [  101.957683] virtio-pci 0000:00:02.0: PM: pci_pm_suspend(): virtio_pci_freeze+0x0/0x50 returns -1
> [  101.957702] virtio-pci 0000:00:02.0: PM: dpm_run_callback(): pci_pm_suspend+0x0/0x170 returns -1
> [  101.957718] virtio-pci 0000:00:02.0: PM: failed to suspend async: error -1

QEMU's virtio-pci devices don't advertise no_soft_reset, so this patch
won't affect vanilla QEMU. But if you add PCI_PM_CTRL_NO_SOFT_RESET to
the capability, then it should work. I'm working with crosvm, which
doesn't have virtio-mem implemented, but this patch makes virtio-fs
work with no extra kernel changes.

-David
Re: [PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by David Hildenbrand 1 year, 12 months ago
On 15.12.23 02:00, David Stevens wrote:
> On Thu, Dec 14, 2023 at 6:47 PM David Hildenbrand <david@redhat.com> wrote:
>>
>> On 08.12.23 08:07, David Stevens wrote:
>>> If a virtio_pci_device supports native PCI power management and has the
>>> No_Soft_Reset bit set, then skip resetting and reinitializing the device
>>> when suspending and restoring the device. This allows system-wide low
>>> power states like s2idle to be used in systems with stateful virtio
>>> devices that can't simply be re-initialized (e.g. virtio-fs).
>>>
>>> Signed-off-by: David Stevens <stevensd@chromium.org>
>>> ---
>>> v1 -> v2:
>>>    - Check the No_Soft_Reset bit
>>>
>>>    drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
>>>    1 file changed, 33 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
>>> index c2524a7207cf..3a95ecaf12dc 100644
>>> --- a/drivers/virtio/virtio_pci_common.c
>>> +++ b/drivers/virtio/virtio_pci_common.c
>>> @@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
>>>        return virtio_device_restore(&vp_dev->vdev);
>>>    }
>>>
>>> +static bool vp_supports_pm_no_reset(struct device *dev)
>>> +{
>>> +     struct pci_dev *pci_dev = to_pci_dev(dev);
>>> +     u16 pmcsr;
>>> +
>>> +     if (!pci_dev->pm_cap)
>>> +             return false;
>>> +
>>> +     pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
>>> +     if (PCI_POSSIBLE_ERROR(pmcsr)) {
>>> +             dev_err(dev, "Unable to query pmcsr");
>>> +             return false;
>>> +     }
>>> +
>>> +     return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
>>> +}
>>> +
>>> +static int virtio_pci_suspend(struct device *dev)
>>> +{
>>> +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
>>> +}
>>> +
>>> +static int virtio_pci_resume(struct device *dev)
>>> +{
>>> +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
>>> +}
>>> +
>>>    static const struct dev_pm_ops virtio_pci_pm_ops = {
>>> -     SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
>>> +     .suspend = virtio_pci_suspend,
>>> +     .resume = virtio_pci_resume,
>>> +     .freeze = virtio_pci_freeze,
>>> +     .thaw = virtio_pci_restore,
>>> +     .poweroff = virtio_pci_freeze,
>>> +     .restore = virtio_pci_restore,
>>>    };
>>>    #endif
>>>
>>
>> Am I correct with my assumption that this will make s2idle work with virtio-mem-pci as well?
>>
>> Right now, all suspending is disabled, but really only s4/hibernate is problematic.
>>
>> [root@vm-0 ~]# echo "s2idle" > /sys/power/mem_sleep
>> [root@vm-0 ~]# echo "mem" > /sys/power/state
>> [  101.822991] PM: suspend entry (s2idle)
>> [  101.828978] Filesystems sync: 0.004 seconds
>> [  101.831618] Freezing user space processes
>> [  101.834569] Freezing user space processes completed (elapsed 0.001 seconds)
>> [  101.836915] OOM killer disabled.
>> [  101.838072] Freezing remaining freezable tasks
>> [  101.841054] Freezing remaining freezable tasks completed (elapsed 0.001 seconds)
>> [  101.843538] printk: Suspending console(s) (use no_console_suspend to debug)
>> [  101.957676] virtio_mem virtio0: save/restore not supported.
>> [  101.957683] virtio-pci 0000:00:02.0: PM: pci_pm_suspend(): virtio_pci_freeze+0x0/0x50 returns -1
>> [  101.957702] virtio-pci 0000:00:02.0: PM: dpm_run_callback(): pci_pm_suspend+0x0/0x170 returns -1
>> [  101.957718] virtio-pci 0000:00:02.0: PM: failed to suspend async: error -1
> 
> QEMU's virtio-pci devices don't advertise no_soft_reset, so this patch
> won't affect vanilla QEMU. But if you add PCI_PM_CTRL_NO_SOFT_RESET to
> the capability, then it should work. I'm working with crosvm, which
> doesn't have virtio-mem implemented, but this patch makes virtio-fs
> work with no extra kernel changes.

Thanks for the explanation, makes sense to me.

-- 
Cheers,

David / dhildenb

Re: [PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by Michael S. Tsirkin 2 years ago
On Fri, Dec 08, 2023 at 04:07:54PM +0900, David Stevens wrote:
> If a virtio_pci_device supports native PCI power management and has the
> No_Soft_Reset bit set, then skip resetting and reinitializing the device
> when suspending and restoring the device. This allows system-wide low
> power states like s2idle to be used in systems with stateful virtio
> devices that can't simply be re-initialized (e.g. virtio-fs).
> 
> Signed-off-by: David Stevens <stevensd@chromium.org>

tagged, thanks!
I'm still debating with myself whether we can classify this
as a bugfix ... better not risk it I guess.

> ---
> v1 -> v2:
>  - Check the No_Soft_Reset bit
> 
>  drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
>  1 file changed, 33 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
> index c2524a7207cf..3a95ecaf12dc 100644
> --- a/drivers/virtio/virtio_pci_common.c
> +++ b/drivers/virtio/virtio_pci_common.c
> @@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
>  	return virtio_device_restore(&vp_dev->vdev);
>  }
>  
> +static bool vp_supports_pm_no_reset(struct device *dev)
> +{
> +	struct pci_dev *pci_dev = to_pci_dev(dev);
> +	u16 pmcsr;
> +
> +	if (!pci_dev->pm_cap)
> +		return false;
> +
> +	pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
> +	if (PCI_POSSIBLE_ERROR(pmcsr)) {
> +		dev_err(dev, "Unable to query pmcsr");
> +		return false;
> +	}
> +
> +	return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
> +}
> +
> +static int virtio_pci_suspend(struct device *dev)
> +{
> +	return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
> +}
> +
> +static int virtio_pci_resume(struct device *dev)
> +{
> +	return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
> +}
> +
>  static const struct dev_pm_ops virtio_pci_pm_ops = {
> -	SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
> +	.suspend = virtio_pci_suspend,
> +	.resume = virtio_pci_resume,
> +	.freeze = virtio_pci_freeze,
> +	.thaw = virtio_pci_restore,
> +	.poweroff = virtio_pci_freeze,
> +	.restore = virtio_pci_restore,
>  };
>  #endif
>  
> -- 
> 2.43.0.472.g3155946c3a-goog
Re: [PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by Jason Wang 2 years ago
On Tue, Dec 12, 2023 at 12:37 AM Michael S. Tsirkin <mst@redhat.com> wrote:
>
> On Fri, Dec 08, 2023 at 04:07:54PM +0900, David Stevens wrote:
> > If a virtio_pci_device supports native PCI power management and has the
> > No_Soft_Reset bit set, then skip resetting and reinitializing the device
> > when suspending and restoring the device. This allows system-wide low
> > power states like s2idle to be used in systems with stateful virtio
> > devices that can't simply be re-initialized (e.g. virtio-fs).
> >
> > Signed-off-by: David Stevens <stevensd@chromium.org>
>
> tagged, thanks!
> I'm still debating with myself whether we can classify this
> as a bugfix ... better not risk it I guess.

It might be suitable if there's a hypervisor that advertises
no_soft_reset (but it seems Qemu doesn't).

Thanks

>
> > ---
> > v1 -> v2:
> >  - Check the No_Soft_Reset bit
> >
> >  drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
> >  1 file changed, 33 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
> > index c2524a7207cf..3a95ecaf12dc 100644
> > --- a/drivers/virtio/virtio_pci_common.c
> > +++ b/drivers/virtio/virtio_pci_common.c
> > @@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
> >       return virtio_device_restore(&vp_dev->vdev);
> >  }
> >
> > +static bool vp_supports_pm_no_reset(struct device *dev)
> > +{
> > +     struct pci_dev *pci_dev = to_pci_dev(dev);
> > +     u16 pmcsr;
> > +
> > +     if (!pci_dev->pm_cap)
> > +             return false;
> > +
> > +     pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
> > +     if (PCI_POSSIBLE_ERROR(pmcsr)) {
> > +             dev_err(dev, "Unable to query pmcsr");
> > +             return false;
> > +     }
> > +
> > +     return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
> > +}
> > +
> > +static int virtio_pci_suspend(struct device *dev)
> > +{
> > +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
> > +}
> > +
> > +static int virtio_pci_resume(struct device *dev)
> > +{
> > +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
> > +}
> > +
> >  static const struct dev_pm_ops virtio_pci_pm_ops = {
> > -     SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
> > +     .suspend = virtio_pci_suspend,
> > +     .resume = virtio_pci_resume,
> > +     .freeze = virtio_pci_freeze,
> > +     .thaw = virtio_pci_restore,
> > +     .poweroff = virtio_pci_freeze,
> > +     .restore = virtio_pci_restore,
> >  };
> >  #endif
> >
> > --
> > 2.43.0.472.g3155946c3a-goog
>
Re: [PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by Michael S. Tsirkin 2 years ago
On Tue, Dec 12, 2023 at 11:01:53AM +0800, Jason Wang wrote:
> On Tue, Dec 12, 2023 at 12:37 AM Michael S. Tsirkin <mst@redhat.com> wrote:
> >
> > On Fri, Dec 08, 2023 at 04:07:54PM +0900, David Stevens wrote:
> > > If a virtio_pci_device supports native PCI power management and has the
> > > No_Soft_Reset bit set, then skip resetting and reinitializing the device
> > > when suspending and restoring the device. This allows system-wide low
> > > power states like s2idle to be used in systems with stateful virtio
> > > devices that can't simply be re-initialized (e.g. virtio-fs).
> > >
> > > Signed-off-by: David Stevens <stevensd@chromium.org>
> >
> > tagged, thanks!
> > I'm still debating with myself whether we can classify this
> > as a bugfix ... better not risk it I guess.
> 
> It might be suitable if there's a hypervisor that advertises
> no_soft_reset (but it seems Qemu doesn't).
> 
> Thanks

Yea... so a bugfix but no rush to merge it I think.

> >
> > > ---
> > > v1 -> v2:
> > >  - Check the No_Soft_Reset bit
> > >
> > >  drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
> > >  1 file changed, 33 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
> > > index c2524a7207cf..3a95ecaf12dc 100644
> > > --- a/drivers/virtio/virtio_pci_common.c
> > > +++ b/drivers/virtio/virtio_pci_common.c
> > > @@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
> > >       return virtio_device_restore(&vp_dev->vdev);
> > >  }
> > >
> > > +static bool vp_supports_pm_no_reset(struct device *dev)
> > > +{
> > > +     struct pci_dev *pci_dev = to_pci_dev(dev);
> > > +     u16 pmcsr;
> > > +
> > > +     if (!pci_dev->pm_cap)
> > > +             return false;
> > > +
> > > +     pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
> > > +     if (PCI_POSSIBLE_ERROR(pmcsr)) {
> > > +             dev_err(dev, "Unable to query pmcsr");
> > > +             return false;
> > > +     }
> > > +
> > > +     return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
> > > +}
> > > +
> > > +static int virtio_pci_suspend(struct device *dev)
> > > +{
> > > +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
> > > +}
> > > +
> > > +static int virtio_pci_resume(struct device *dev)
> > > +{
> > > +     return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
> > > +}
> > > +
> > >  static const struct dev_pm_ops virtio_pci_pm_ops = {
> > > -     SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
> > > +     .suspend = virtio_pci_suspend,
> > > +     .resume = virtio_pci_resume,
> > > +     .freeze = virtio_pci_freeze,
> > > +     .thaw = virtio_pci_restore,
> > > +     .poweroff = virtio_pci_freeze,
> > > +     .restore = virtio_pci_restore,
> > >  };
> > >  #endif
> > >
> > > --
> > > 2.43.0.472.g3155946c3a-goog
> >

Re: [PATCH v2] virtio: Add support for no-reset virtio PCI PM
Posted by Jason Wang 2 years ago
On Fri, Dec 8, 2023 at 3:09 PM David Stevens <stevensd@chromium.org> wrote:
>
> If a virtio_pci_device supports native PCI power management and has the
> No_Soft_Reset bit set, then skip resetting and reinitializing the device
> when suspending and restoring the device. This allows system-wide low
> power states like s2idle to be used in systems with stateful virtio
> devices that can't simply be re-initialized (e.g. virtio-fs).
>
> Signed-off-by: David Stevens <stevensd@chromium.org>

Acked-by: Jason Wang <jasowang@redhat.com>

Thanks


> ---
> v1 -> v2:
>  - Check the No_Soft_Reset bit
>
>  drivers/virtio/virtio_pci_common.c | 34 +++++++++++++++++++++++++++++-
>  1 file changed, 33 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/virtio/virtio_pci_common.c b/drivers/virtio/virtio_pci_common.c
> index c2524a7207cf..3a95ecaf12dc 100644
> --- a/drivers/virtio/virtio_pci_common.c
> +++ b/drivers/virtio/virtio_pci_common.c
> @@ -492,8 +492,40 @@ static int virtio_pci_restore(struct device *dev)
>         return virtio_device_restore(&vp_dev->vdev);
>  }
>
> +static bool vp_supports_pm_no_reset(struct device *dev)
> +{
> +       struct pci_dev *pci_dev = to_pci_dev(dev);
> +       u16 pmcsr;
> +
> +       if (!pci_dev->pm_cap)
> +               return false;
> +
> +       pci_read_config_word(pci_dev, pci_dev->pm_cap + PCI_PM_CTRL, &pmcsr);
> +       if (PCI_POSSIBLE_ERROR(pmcsr)) {
> +               dev_err(dev, "Unable to query pmcsr");
> +               return false;
> +       }
> +
> +       return pmcsr & PCI_PM_CTRL_NO_SOFT_RESET;
> +}
> +
> +static int virtio_pci_suspend(struct device *dev)
> +{
> +       return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_freeze(dev);
> +}
> +
> +static int virtio_pci_resume(struct device *dev)
> +{
> +       return vp_supports_pm_no_reset(dev) ? 0 : virtio_pci_restore(dev);
> +}
> +
>  static const struct dev_pm_ops virtio_pci_pm_ops = {
> -       SET_SYSTEM_SLEEP_PM_OPS(virtio_pci_freeze, virtio_pci_restore)
> +       .suspend = virtio_pci_suspend,
> +       .resume = virtio_pci_resume,
> +       .freeze = virtio_pci_freeze,
> +       .thaw = virtio_pci_restore,
> +       .poweroff = virtio_pci_freeze,
> +       .restore = virtio_pci_restore,
>  };
>  #endif
>
> --
> 2.43.0.472.g3155946c3a-goog
>