[PATCH 2/4] drm/xe: Save and restore PCI state

2024-04-21 Thread Aravind Iddamsetty
Save and restore PCI states where ever needed.

Cc: Lucas De Marchi 

Reviewed-by: Rodrigo Vivi 
Signed-off-by: Aravind Iddamsetty 
---
 drivers/gpu/drm/xe/xe_device_types.h |  3 ++
 drivers/gpu/drm/xe/xe_pci.c  | 48 ++--
 drivers/gpu/drm/xe/xe_pci.h  |  4 ++-
 3 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h 
b/drivers/gpu/drm/xe/xe_device_types.h
index 8244b177a6a3..0a66555229e9 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -462,6 +462,9 @@ struct xe_device {
/** @needs_flr_on_fini: requests function-reset on fini */
bool needs_flr_on_fini;
 
+   /** @pci_state: PCI state of device */
+   struct pci_saved_state *pci_state;
+
/* private: */
 
 #if IS_ENABLED(CONFIG_DRM_XE_DISPLAY)
diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
index fb20c9828563..a62300990e19 100644
--- a/drivers/gpu/drm/xe/xe_pci.c
+++ b/drivers/gpu/drm/xe/xe_pci.c
@@ -393,6 +393,41 @@ MODULE_DEVICE_TABLE(pci, pciidlist);
 
 #undef INTEL_VGA_DEVICE
 
+static bool xe_save_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+
+   if (pci_save_state(pdev))
+   return false;
+
+   kfree(xe->pci_state);
+
+   xe->pci_state = pci_store_saved_state(pdev);
+   if (!xe->pci_state) {
+   drm_err(&xe->drm, "Failed to store PCI saved state\n");
+   return false;
+   }
+
+   return true;
+}
+
+void xe_load_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+   int ret;
+
+   if (!xe->pci_state)
+   return;
+
+   ret = pci_load_saved_state(pdev, xe->pci_state);
+   if (ret) {
+   drm_warn(&xe->drm, "Failed to load PCI state err:%d\n", ret);
+   return;
+   }
+
+   pci_restore_state(pdev);
+}
+
 /* is device_id present in comma separated list of ids */
 static bool device_id_in_list(u16 device_id, const char *devices, bool 
negative)
 {
@@ -698,6 +733,8 @@ static void xe_pci_remove(struct pci_dev *pdev)
 
xe_device_remove(xe);
xe_pm_runtime_fini(xe);
+
+   kfree(xe->pci_state);
pci_set_drvdata(pdev, NULL);
 }
 
@@ -798,6 +835,9 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
drm_dbg(&xe->drm, "d3cold: capable=%s\n",
str_yes_no(xe->d3cold.capable));
 
+   if (xe_save_pci_state(pdev))
+   pci_restore_state(pdev);
+
return 0;
 
 err_driver_cleanup:
@@ -849,7 +889,7 @@ static int xe_pci_suspend(struct device *dev)
 */
d3cold_toggle(pdev, D3COLD_ENABLE);
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
pci_disable_device(pdev);
 
return 0;
@@ -873,6 +913,8 @@ static int xe_pci_resume(struct device *dev)
 
pci_set_master(pdev);
 
+   xe_load_pci_state(pdev);
+
err = xe_pm_resume(pdev_to_xe_device(pdev));
if (err)
return err;
@@ -890,7 +932,7 @@ static int xe_pci_runtime_suspend(struct device *dev)
if (err)
return err;
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
 
if (xe->d3cold.allowed) {
d3cold_toggle(pdev, D3COLD_ENABLE);
@@ -915,7 +957,7 @@ static int xe_pci_runtime_resume(struct device *dev)
if (err)
return err;
 
-   pci_restore_state(pdev);
+   xe_load_pci_state(pdev);
 
if (xe->d3cold.allowed) {
err = pci_enable_device(pdev);
diff --git a/drivers/gpu/drm/xe/xe_pci.h b/drivers/gpu/drm/xe/xe_pci.h
index 611c1209b14c..73b90a430d1f 100644
--- a/drivers/gpu/drm/xe/xe_pci.h
+++ b/drivers/gpu/drm/xe/xe_pci.h
@@ -6,7 +6,9 @@
 #ifndef _XE_PCI_H_
 #define _XE_PCI_H_
 
+struct pci_dev;
+
 int xe_register_pci_driver(void);
 void xe_unregister_pci_driver(void);
-
+void xe_load_pci_state(struct pci_dev *pdev);
 #endif
-- 
2.25.1



[PATCH 2/4] drm/xe: Save and restore PCI state

2024-04-19 Thread Aravind Iddamsetty
Save and restore PCI states where ever needed.

Cc: Lucas De Marchi 

Reviewed-by: Rodrigo Vivi 
Signed-off-by: Aravind Iddamsetty 
---
 drivers/gpu/drm/xe/xe_device_types.h |  3 ++
 drivers/gpu/drm/xe/xe_pci.c  | 48 ++--
 drivers/gpu/drm/xe/xe_pci.h  |  4 ++-
 3 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h 
b/drivers/gpu/drm/xe/xe_device_types.h
index 8244b177a6a3..0a66555229e9 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -462,6 +462,9 @@ struct xe_device {
/** @needs_flr_on_fini: requests function-reset on fini */
bool needs_flr_on_fini;
 
+   /** @pci_state: PCI state of device */
+   struct pci_saved_state *pci_state;
+
/* private: */
 
 #if IS_ENABLED(CONFIG_DRM_XE_DISPLAY)
diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
index 65ea44672cab..fd45f235b35c 100644
--- a/drivers/gpu/drm/xe/xe_pci.c
+++ b/drivers/gpu/drm/xe/xe_pci.c
@@ -391,6 +391,41 @@ MODULE_DEVICE_TABLE(pci, pciidlist);
 
 #undef INTEL_VGA_DEVICE
 
+static bool xe_save_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+
+   if (pci_save_state(pdev))
+   return false;
+
+   kfree(xe->pci_state);
+
+   xe->pci_state = pci_store_saved_state(pdev);
+   if (!xe->pci_state) {
+   drm_err(&xe->drm, "Failed to store PCI saved state\n");
+   return false;
+   }
+
+   return true;
+}
+
+void xe_load_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+   int ret;
+
+   if (!xe->pci_state)
+   return;
+
+   ret = pci_load_saved_state(pdev, xe->pci_state);
+   if (ret) {
+   drm_warn(&xe->drm, "Failed to load PCI state err:%d\n", ret);
+   return;
+   }
+
+   pci_restore_state(pdev);
+}
+
 /* is device_id present in comma separated list of ids */
 static bool device_id_in_list(u16 device_id, const char *devices, bool 
negative)
 {
@@ -696,6 +731,8 @@ static void xe_pci_remove(struct pci_dev *pdev)
 
xe_device_remove(xe);
xe_pm_runtime_fini(xe);
+
+   kfree(xe->pci_state);
pci_set_drvdata(pdev, NULL);
 }
 
@@ -796,6 +833,9 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
drm_dbg(&xe->drm, "d3cold: capable=%s\n",
str_yes_no(xe->d3cold.capable));
 
+   if (xe_save_pci_state(pdev))
+   pci_restore_state(pdev);
+
return 0;
 
 err_driver_cleanup:
@@ -847,7 +887,7 @@ static int xe_pci_suspend(struct device *dev)
 */
d3cold_toggle(pdev, D3COLD_ENABLE);
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
pci_disable_device(pdev);
 
return 0;
@@ -871,6 +911,8 @@ static int xe_pci_resume(struct device *dev)
 
pci_set_master(pdev);
 
+   xe_load_pci_state(pdev);
+
err = xe_pm_resume(pdev_to_xe_device(pdev));
if (err)
return err;
@@ -888,7 +930,7 @@ static int xe_pci_runtime_suspend(struct device *dev)
if (err)
return err;
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
 
if (xe->d3cold.allowed) {
d3cold_toggle(pdev, D3COLD_ENABLE);
@@ -913,7 +955,7 @@ static int xe_pci_runtime_resume(struct device *dev)
if (err)
return err;
 
-   pci_restore_state(pdev);
+   xe_load_pci_state(pdev);
 
if (xe->d3cold.allowed) {
err = pci_enable_device(pdev);
diff --git a/drivers/gpu/drm/xe/xe_pci.h b/drivers/gpu/drm/xe/xe_pci.h
index 611c1209b14c..73b90a430d1f 100644
--- a/drivers/gpu/drm/xe/xe_pci.h
+++ b/drivers/gpu/drm/xe/xe_pci.h
@@ -6,7 +6,9 @@
 #ifndef _XE_PCI_H_
 #define _XE_PCI_H_
 
+struct pci_dev;
+
 int xe_register_pci_driver(void);
 void xe_unregister_pci_driver(void);
-
+void xe_load_pci_state(struct pci_dev *pdev);
 #endif
-- 
2.25.1



[PATCH 2/4] drm/xe: Save and restore PCI state

2024-04-17 Thread Aravind Iddamsetty
Save and restore PCI states where ever needed.

Cc: Lucas De Marchi 

Reviewed-by: Rodrigo Vivi 
Signed-off-by: Aravind Iddamsetty 
---
 drivers/gpu/drm/xe/xe_device_types.h |  3 ++
 drivers/gpu/drm/xe/xe_pci.c  | 48 ++--
 drivers/gpu/drm/xe/xe_pci.h  |  4 ++-
 3 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h 
b/drivers/gpu/drm/xe/xe_device_types.h
index 8244b177a6a3..0a66555229e9 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -462,6 +462,9 @@ struct xe_device {
/** @needs_flr_on_fini: requests function-reset on fini */
bool needs_flr_on_fini;
 
+   /** @pci_state: PCI state of device */
+   struct pci_saved_state *pci_state;
+
/* private: */
 
 #if IS_ENABLED(CONFIG_DRM_XE_DISPLAY)
diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
index fa2cc80a08a3..f9957cd16356 100644
--- a/drivers/gpu/drm/xe/xe_pci.c
+++ b/drivers/gpu/drm/xe/xe_pci.c
@@ -391,6 +391,41 @@ MODULE_DEVICE_TABLE(pci, pciidlist);
 
 #undef INTEL_VGA_DEVICE
 
+static bool xe_save_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+
+   if (pci_save_state(pdev))
+   return false;
+
+   kfree(xe->pci_state);
+
+   xe->pci_state = pci_store_saved_state(pdev);
+   if (!xe->pci_state) {
+   drm_err(&xe->drm, "Failed to store PCI saved state\n");
+   return false;
+   }
+
+   return true;
+}
+
+void xe_load_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+   int ret;
+
+   if (!xe->pci_state)
+   return;
+
+   ret = pci_load_saved_state(pdev, xe->pci_state);
+   if (ret) {
+   drm_warn(&xe->drm, "Failed to load PCI state err:%d\n", ret);
+   return;
+   }
+
+   pci_restore_state(pdev);
+}
+
 /* is device_id present in comma separated list of ids */
 static bool device_id_in_list(u16 device_id, const char *devices, bool 
negative)
 {
@@ -696,6 +731,8 @@ static void xe_pci_remove(struct pci_dev *pdev)
 
xe_device_remove(xe);
xe_pm_runtime_fini(xe);
+
+   kfree(xe->pci_state);
pci_set_drvdata(pdev, NULL);
 }
 
@@ -792,6 +829,9 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
drm_dbg(&xe->drm, "d3cold: capable=%s\n",
str_yes_no(xe->d3cold.capable));
 
+   if (xe_save_pci_state(pdev))
+   pci_restore_state(pdev);
+
return 0;
 }
 
@@ -839,7 +879,7 @@ static int xe_pci_suspend(struct device *dev)
 */
d3cold_toggle(pdev, D3COLD_ENABLE);
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
pci_disable_device(pdev);
 
return 0;
@@ -863,6 +903,8 @@ static int xe_pci_resume(struct device *dev)
 
pci_set_master(pdev);
 
+   xe_load_pci_state(pdev);
+
err = xe_pm_resume(pdev_to_xe_device(pdev));
if (err)
return err;
@@ -880,7 +922,7 @@ static int xe_pci_runtime_suspend(struct device *dev)
if (err)
return err;
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
 
if (xe->d3cold.allowed) {
d3cold_toggle(pdev, D3COLD_ENABLE);
@@ -905,7 +947,7 @@ static int xe_pci_runtime_resume(struct device *dev)
if (err)
return err;
 
-   pci_restore_state(pdev);
+   xe_load_pci_state(pdev);
 
if (xe->d3cold.allowed) {
err = pci_enable_device(pdev);
diff --git a/drivers/gpu/drm/xe/xe_pci.h b/drivers/gpu/drm/xe/xe_pci.h
index 611c1209b14c..73b90a430d1f 100644
--- a/drivers/gpu/drm/xe/xe_pci.h
+++ b/drivers/gpu/drm/xe/xe_pci.h
@@ -6,7 +6,9 @@
 #ifndef _XE_PCI_H_
 #define _XE_PCI_H_
 
+struct pci_dev;
+
 int xe_register_pci_driver(void);
 void xe_unregister_pci_driver(void);
-
+void xe_load_pci_state(struct pci_dev *pdev);
 #endif
-- 
2.25.1



[PATCH 2/4] drm/xe: Save and restore PCI state

2024-04-02 Thread Aravind Iddamsetty
Save and restore PCI states where ever needed.

Cc: Lucas De Marchi 
Signed-off-by: Aravind Iddamsetty 
---
 drivers/gpu/drm/xe/xe_device_types.h |  3 ++
 drivers/gpu/drm/xe/xe_pci.c  | 48 ++--
 drivers/gpu/drm/xe/xe_pci.h  |  4 ++-
 3 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/drivers/gpu/drm/xe/xe_device_types.h 
b/drivers/gpu/drm/xe/xe_device_types.h
index 1df3dcc17d75..3bfde4b59284 100644
--- a/drivers/gpu/drm/xe/xe_device_types.h
+++ b/drivers/gpu/drm/xe/xe_device_types.h
@@ -455,6 +455,9 @@ struct xe_device {
/** @needs_flr_on_fini: requests function-reset on fini */
bool needs_flr_on_fini;
 
+   /** @pci_state: PCI state of device */
+   struct pci_saved_state *pci_state;
+
/* private: */
 
 #if IS_ENABLED(CONFIG_DRM_XE_DISPLAY)
diff --git a/drivers/gpu/drm/xe/xe_pci.c b/drivers/gpu/drm/xe/xe_pci.c
index c401d4890386..e9e10f8d5f2b 100644
--- a/drivers/gpu/drm/xe/xe_pci.c
+++ b/drivers/gpu/drm/xe/xe_pci.c
@@ -383,6 +383,41 @@ MODULE_DEVICE_TABLE(pci, pciidlist);
 
 #undef INTEL_VGA_DEVICE
 
+static bool xe_save_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+
+   if (pci_save_state(pdev))
+   return false;
+
+   kfree(xe->pci_state);
+
+   xe->pci_state = pci_store_saved_state(pdev);
+   if (!xe->pci_state) {
+   drm_err(&xe->drm, "Failed to store PCI saved state\n");
+   return false;
+   }
+
+   return true;
+}
+
+void xe_load_pci_state(struct pci_dev *pdev)
+{
+   struct xe_device *xe = pci_get_drvdata(pdev);
+   int ret;
+
+   if (!xe->pci_state)
+   return;
+
+   ret = pci_load_saved_state(pdev, xe->pci_state);
+   if (ret) {
+   drm_warn(&xe->drm, "Failed to load PCI state err:%d\n", ret);
+   return;
+   }
+
+   pci_restore_state(pdev);
+}
+
 /* is device_id present in comma separated list of ids */
 static bool device_id_in_list(u16 device_id, const char *devices, bool 
negative)
 {
@@ -688,6 +723,8 @@ static void xe_pci_remove(struct pci_dev *pdev)
 
xe_device_remove(xe);
xe_pm_runtime_fini(xe);
+
+   kfree(xe->pci_state);
pci_set_drvdata(pdev, NULL);
 }
 
@@ -786,6 +823,9 @@ static int xe_pci_probe(struct pci_dev *pdev, const struct 
pci_device_id *ent)
drm_dbg(&xe->drm, "d3cold: capable=%s\n",
str_yes_no(xe->d3cold.capable));
 
+   if (xe_save_pci_state(pdev))
+   pci_restore_state(pdev);
+
return 0;
 }
 
@@ -833,7 +873,7 @@ static int xe_pci_suspend(struct device *dev)
 */
d3cold_toggle(pdev, D3COLD_ENABLE);
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
pci_disable_device(pdev);
 
return 0;
@@ -857,6 +897,8 @@ static int xe_pci_resume(struct device *dev)
 
pci_set_master(pdev);
 
+   xe_load_pci_state(pdev);
+
err = xe_pm_resume(pdev_to_xe_device(pdev));
if (err)
return err;
@@ -874,7 +916,7 @@ static int xe_pci_runtime_suspend(struct device *dev)
if (err)
return err;
 
-   pci_save_state(pdev);
+   xe_save_pci_state(pdev);
 
if (xe->d3cold.allowed) {
d3cold_toggle(pdev, D3COLD_ENABLE);
@@ -899,7 +941,7 @@ static int xe_pci_runtime_resume(struct device *dev)
if (err)
return err;
 
-   pci_restore_state(pdev);
+   xe_load_pci_state(pdev);
 
if (xe->d3cold.allowed) {
err = pci_enable_device(pdev);
diff --git a/drivers/gpu/drm/xe/xe_pci.h b/drivers/gpu/drm/xe/xe_pci.h
index 611c1209b14c..73b90a430d1f 100644
--- a/drivers/gpu/drm/xe/xe_pci.h
+++ b/drivers/gpu/drm/xe/xe_pci.h
@@ -6,7 +6,9 @@
 #ifndef _XE_PCI_H_
 #define _XE_PCI_H_
 
+struct pci_dev;
+
 int xe_register_pci_driver(void);
 void xe_unregister_pci_driver(void);
-
+void xe_load_pci_state(struct pci_dev *pdev);
 #endif
-- 
2.25.1