From patchwork Mon Oct 28 09:02:38 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Demharter X-Patchwork-Id: 3101191 Return-Path: X-Original-To: patchwork-dri-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0B8249F431 for ; Mon, 28 Oct 2013 09:36:02 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id D6569202C7 for ; Mon, 28 Oct 2013 09:36:00 +0000 (UTC) Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) by mail.kernel.org (Postfix) with ESMTP id 741B720250 for ; Mon, 28 Oct 2013 09:35:59 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 64730E6641 for ; Mon, 28 Oct 2013 02:35:59 -0700 (PDT) X-Original-To: dri-devel@lists.freedesktop.org Delivered-To: dri-devel@lists.freedesktop.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.20]) by gabe.freedesktop.org (Postfix) with ESMTP id 4DFBBE70F2 for ; Mon, 28 Oct 2013 02:02:45 -0700 (PDT) Received: from [10.21.40.126] ([212.227.66.36]) by mail.gmx.com (mrgmx001) with ESMTPSA (Nemesis) id 0M4WuC-1VtdgF0way-00yfjt for ; Mon, 28 Oct 2013 10:02:43 +0100 Message-ID: <526E282E.5030309@gmx.net> Date: Mon, 28 Oct 2013 10:02:38 +0100 From: Stefan Demharter User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: dri-devel@lists.freedesktop.org Subject: [PATCH] Restore mux and power states after resume from hibernation X-Provags-ID: V03:K0:Pmxv7Ft51cp4cXMpT4zmEH13xCM1jaM+Q5z5XmvejHof7MsSGv6 YbLJBY91O3Y8Xw48veM3u22Ps8cjkDj9LPRmxNHRODw/zkbZ3tNTD+qKVQ5pNPj/whpbjsu S4fvrYLHUEfUMCja8q82sCdpTwWkv6p8AvWH9Y6osSZn1CqvDm5dSDAU824mTmH6CGJzFXI zOc12EF9PvRSfrKDyB6KQ== X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org Errors-To: dri-devel-bounces+patchwork-dri-devel=patchwork.kernel.org@lists.freedesktop.org X-Spam-Status: No, score=-4.7 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_MED, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 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; }