commit cd8f863a804d9388b09771640110577dddafc695 Author: Stefan Demharter 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 #include #include +#include #include @@ -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; }