diff mbox

Restore mux and power states after resume from hibernation

Message ID 526E282E.5030309@gmx.net (mailing list archive)
State New, archived
Headers show

Commit Message

Stefan Demharter Oct. 28, 2013, 9:02 a.m. UTC
Hi all,

i've got a system with a muxed intel+ati card and had a problem with hibernation:
Vgaswitcheroo didn't restore the states of the graphics cards and the state of the mux after resume.

I have solved the issue with the attached patch.

Can you please review it?

Regards
Stefan

Comments

Jani Nikula Jan. 23, 2014, 11:46 a.m. UTC | #1
On Mon, 28 Oct 2013, Stefan Demharter <stefan.demharter@gmx.net> wrote:
> Hi all,
>
> i've got a system with a muxed intel+ati card and had a problem with hibernation:
> Vgaswitcheroo didn't restore the states of the graphics cards and the state of the mux after resume.
>
> I have solved the issue with the attached patch.
>
> Can you please review it?

I see that Daniel asked for the patch to be sent here in [1]. Thanks,
and sorry that it's been neglected!

I did not look at the patch either (not really my area of expertise) but
please consider sending the patch in proper patch format (with
Signed-off-by, see Documentation/SubmittingPatches), and do pay
attention to Documentation/CodingStyle (at least wrt comments and having
a newline after if (cond)). This might help you get the review.

Thanks,
Jani.


[1] https://bugs.freedesktop.org/show_bug.cgi?id=70863



>
> Regards
> Stefan
> commit cd8f863a804d9388b09771640110577dddafc695
> Author: Stefan Demharter <stefan.demharter@gmx.net>
> Date:   Thu Oct 24 00:06:09 2013 +0200
>
>     vgaswitcheroo: restore mux and power states after resume from hibernation.
>
> diff --git a/drivers/gpu/vga/vga_switcheroo.c b/drivers/gpu/vga/vga_switcheroo.c
> index ec0ae2d..4731f02 100644
> --- a/drivers/gpu/vga/vga_switcheroo.c
> +++ b/drivers/gpu/vga/vga_switcheroo.c
> @@ -28,6 +28,7 @@
>  #include <linux/console.h>
>  #include <linux/vga_switcheroo.h>
>  #include <linux/pm_runtime.h>
> +#include <linux/suspend.h>
>  
>  #include <linux/vgaarb.h>
>  
> @@ -72,6 +73,46 @@ static struct vgasr_priv vgasr_priv = {
>  	.clients = LIST_HEAD_INIT(vgasr_priv.clients),
>  };
>  
> +/* save state of mux to restore it upon hibernation resume */
> +// Assume initial state after boot: mux is set to IGD
> +#define MUX_POWER_ON_STATE VGA_SWITCHEROO_IGD
> +static int mux_id = MUX_POWER_ON_STATE;
> +
> +static int vga_switchoff(struct vga_switcheroo_client *client);
> +
> +static int vga_resume(struct notifier_block *nb,
> +                                unsigned long val, void *ign)
> +{
> +	int ret = 0;
> +	struct vga_switcheroo_client *client;
> +	pr_info("vga_switcheroo: vga_resume %d %d %d\n", (int)val, PM_POST_HIBERNATION, mux_id);
> +	if (val != PM_POST_HIBERNATION) return 0;
> +	if (mux_id != VGA_SWITCHEROO_IGD) {
> +		mutex_lock(&vgasr_mutex);
> +		pr_info("vga_switcheroo: restoring switch to %d after hibernate\n", mux_id);
> +		// Restore mux state
> +		if (mux_id != MUX_POWER_ON_STATE)
> +			ret = vgasr_priv.handler->switchto(mux_id);
> +		// Restore power state
> +		// Assumes all gpus have power after boot
> +		list_for_each_entry(client, &vgasr_priv.clients, list) {
> +			if (!client->pwr_state) {
> +				// Device was powered off, so we redo that...
> +				pr_info("vga_switcheroo: restoring power off of device %d after hibernate\n", client->id);
> +				vga_switchoff(client);
> +			}
> +		}
> +		mutex_unlock(&vgasr_mutex);
> +		if (ret) pr_warn("vga_switcheroo: failed\n");
> +	}
> +	return 0;
> +}
> +
> +static struct notifier_block vga_pm_nb = {
> +	.notifier_call = vga_resume,
> +	.priority = 0,
> +};
> +
>  static bool vga_switcheroo_ready(void)
>  {
>  	/* we're ready if we get two clients + handler */
> @@ -83,6 +124,7 @@ static void vga_switcheroo_enable(void)
>  {
>  	int ret;
>  	struct vga_switcheroo_client *client;
> +	register_pm_notifier(&vga_pm_nb);
>  
>  	/* call the handler to init */
>  	if (vgasr_priv.handler->init)
> @@ -119,14 +161,21 @@ int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
>  }
>  EXPORT_SYMBOL(vga_switcheroo_register_handler);
>  
> +// vgasr_mutex has to be locked from caller
> +void vga_switcheroo_disable(void)
> +{
> +	unregister_pm_notifier(&vga_pm_nb);
> +	pr_info("vga_switcheroo: disabled\n");
> +	vga_switcheroo_debugfs_fini(&vgasr_priv);
> +	vgasr_priv.active = false;
> +}
> +
>  void vga_switcheroo_unregister_handler(void)
>  {
>  	mutex_lock(&vgasr_mutex);
>  	vgasr_priv.handler = NULL;
>  	if (vgasr_priv.active) {
> -		pr_info("vga_switcheroo: disabled\n");
> -		vga_switcheroo_debugfs_fini(&vgasr_priv);
> -		vgasr_priv.active = false;
> +		vga_switcheroo_disable();
>  	}
>  	mutex_unlock(&vgasr_mutex);
>  }
> @@ -235,9 +284,7 @@ void vga_switcheroo_unregister_client(struct pci_dev *pdev)
>  		kfree(client);
>  	}
>  	if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
> -		printk(KERN_INFO "vga_switcheroo: disabled\n");
> -		vga_switcheroo_debugfs_fini(&vgasr_priv);
> -		vgasr_priv.active = false;
> +		vga_switcheroo_disable();
>  	}
>  	mutex_unlock(&vgasr_mutex);
>  }
> @@ -262,12 +309,13 @@ static int vga_switcheroo_show(struct seq_file *m, void *v)
>  	int i = 0;
>  	mutex_lock(&vgasr_mutex);
>  	list_for_each_entry(client, &vgasr_priv.clients, list) {
> -		seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
> +		seq_printf(m, "%d:%s%s:%c:%s%s:%s:%s\n", i,
>  			   client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
>  			   client_is_vga(client) ? "" : "-Audio",
>  			   client->active ? '+' : ' ',
>  			   client->driver_power_control ? "Dyn" : "",
>  			   client->pwr_state ? "Pwr" : "Off",
> +			   client_id(client) == mux_id ? "mux" : "",
>  			   pci_name(client->pdev));
>  		i++;
>  	}
> @@ -304,6 +352,15 @@ static int vga_switchoff(struct vga_switcheroo_client *client)
>  	return 0;
>  }
>  
> +static int vga_switch_mux_to(int client_id)
> +{
> +	int ret = vgasr_priv.handler->switchto(client_id);
> +	if (ret == 0) {
> +		mux_id = client_id;
> +	}
> +	return ret;
> +}
> +
>  static void set_audio_state(int id, int state)
>  {
>  	struct vga_switcheroo_client *client;
> @@ -353,7 +410,7 @@ static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
>  		console_unlock();
>  	}
>  
> -	ret = vgasr_priv.handler->switchto(new_client->id);
> +	ret = vga_switch_mux_to(new_client->id);
>  	if (ret)
>  		return ret;
>  
> @@ -468,7 +525,7 @@ vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
>  	vgasr_priv.delayed_switch_active = false;
>  
>  	if (just_mux) {
> -		ret = vgasr_priv.handler->switchto(client_id);
> +		ret = vga_switch_mux_to(client_id);
>  		goto out;
>  	}
>  
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-devel
diff mbox

Patch

commit cd8f863a804d9388b09771640110577dddafc695
Author: Stefan Demharter <stefan.demharter@gmx.net>
Date:   Thu Oct 24 00:06:09 2013 +0200

    vgaswitcheroo: restore mux and power states after resume from hibernation.

diff --git a/drivers/gpu/vga/vga_switcheroo.c b/drivers/gpu/vga/vga_switcheroo.c
index ec0ae2d..4731f02 100644
--- a/drivers/gpu/vga/vga_switcheroo.c
+++ b/drivers/gpu/vga/vga_switcheroo.c
@@ -28,6 +28,7 @@ 
 #include <linux/console.h>
 #include <linux/vga_switcheroo.h>
 #include <linux/pm_runtime.h>
+#include <linux/suspend.h>
 
 #include <linux/vgaarb.h>
 
@@ -72,6 +73,46 @@  static struct vgasr_priv vgasr_priv = {
 	.clients = LIST_HEAD_INIT(vgasr_priv.clients),
 };
 
+/* save state of mux to restore it upon hibernation resume */
+// Assume initial state after boot: mux is set to IGD
+#define MUX_POWER_ON_STATE VGA_SWITCHEROO_IGD
+static int mux_id = MUX_POWER_ON_STATE;
+
+static int vga_switchoff(struct vga_switcheroo_client *client);
+
+static int vga_resume(struct notifier_block *nb,
+                                unsigned long val, void *ign)
+{
+	int ret = 0;
+	struct vga_switcheroo_client *client;
+	pr_info("vga_switcheroo: vga_resume %d %d %d\n", (int)val, PM_POST_HIBERNATION, mux_id);
+	if (val != PM_POST_HIBERNATION) return 0;
+	if (mux_id != VGA_SWITCHEROO_IGD) {
+		mutex_lock(&vgasr_mutex);
+		pr_info("vga_switcheroo: restoring switch to %d after hibernate\n", mux_id);
+		// Restore mux state
+		if (mux_id != MUX_POWER_ON_STATE)
+			ret = vgasr_priv.handler->switchto(mux_id);
+		// Restore power state
+		// Assumes all gpus have power after boot
+		list_for_each_entry(client, &vgasr_priv.clients, list) {
+			if (!client->pwr_state) {
+				// Device was powered off, so we redo that...
+				pr_info("vga_switcheroo: restoring power off of device %d after hibernate\n", client->id);
+				vga_switchoff(client);
+			}
+		}
+		mutex_unlock(&vgasr_mutex);
+		if (ret) pr_warn("vga_switcheroo: failed\n");
+	}
+	return 0;
+}
+
+static struct notifier_block vga_pm_nb = {
+	.notifier_call = vga_resume,
+	.priority = 0,
+};
+
 static bool vga_switcheroo_ready(void)
 {
 	/* we're ready if we get two clients + handler */
@@ -83,6 +124,7 @@  static void vga_switcheroo_enable(void)
 {
 	int ret;
 	struct vga_switcheroo_client *client;
+	register_pm_notifier(&vga_pm_nb);
 
 	/* call the handler to init */
 	if (vgasr_priv.handler->init)
@@ -119,14 +161,21 @@  int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
 }
 EXPORT_SYMBOL(vga_switcheroo_register_handler);
 
+// vgasr_mutex has to be locked from caller
+void vga_switcheroo_disable(void)
+{
+	unregister_pm_notifier(&vga_pm_nb);
+	pr_info("vga_switcheroo: disabled\n");
+	vga_switcheroo_debugfs_fini(&vgasr_priv);
+	vgasr_priv.active = false;
+}
+
 void vga_switcheroo_unregister_handler(void)
 {
 	mutex_lock(&vgasr_mutex);
 	vgasr_priv.handler = NULL;
 	if (vgasr_priv.active) {
-		pr_info("vga_switcheroo: disabled\n");
-		vga_switcheroo_debugfs_fini(&vgasr_priv);
-		vgasr_priv.active = false;
+		vga_switcheroo_disable();
 	}
 	mutex_unlock(&vgasr_mutex);
 }
@@ -235,9 +284,7 @@  void vga_switcheroo_unregister_client(struct pci_dev *pdev)
 		kfree(client);
 	}
 	if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
-		printk(KERN_INFO "vga_switcheroo: disabled\n");
-		vga_switcheroo_debugfs_fini(&vgasr_priv);
-		vgasr_priv.active = false;
+		vga_switcheroo_disable();
 	}
 	mutex_unlock(&vgasr_mutex);
 }
@@ -262,12 +309,13 @@  static int vga_switcheroo_show(struct seq_file *m, void *v)
 	int i = 0;
 	mutex_lock(&vgasr_mutex);
 	list_for_each_entry(client, &vgasr_priv.clients, list) {
-		seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
+		seq_printf(m, "%d:%s%s:%c:%s%s:%s:%s\n", i,
 			   client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
 			   client_is_vga(client) ? "" : "-Audio",
 			   client->active ? '+' : ' ',
 			   client->driver_power_control ? "Dyn" : "",
 			   client->pwr_state ? "Pwr" : "Off",
+			   client_id(client) == mux_id ? "mux" : "",
 			   pci_name(client->pdev));
 		i++;
 	}
@@ -304,6 +352,15 @@  static int vga_switchoff(struct vga_switcheroo_client *client)
 	return 0;
 }
 
+static int vga_switch_mux_to(int client_id)
+{
+	int ret = vgasr_priv.handler->switchto(client_id);
+	if (ret == 0) {
+		mux_id = client_id;
+	}
+	return ret;
+}
+
 static void set_audio_state(int id, int state)
 {
 	struct vga_switcheroo_client *client;
@@ -353,7 +410,7 @@  static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
 		console_unlock();
 	}
 
-	ret = vgasr_priv.handler->switchto(new_client->id);
+	ret = vga_switch_mux_to(new_client->id);
 	if (ret)
 		return ret;
 
@@ -468,7 +525,7 @@  vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
 	vgasr_priv.delayed_switch_active = false;
 
 	if (just_mux) {
-		ret = vgasr_priv.handler->switchto(client_id);
+		ret = vga_switch_mux_to(client_id);
 		goto out;
 	}