2010-02-01 13:38:10 +08:00
|
|
|
/*
|
2015-08-23 21:18:55 +08:00
|
|
|
* vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
|
|
|
|
*
|
2010-02-01 13:38:10 +08:00
|
|
|
* Copyright (c) 2010 Red Hat Inc.
|
|
|
|
* Author : Dave Airlie <airlied@redhat.com>
|
|
|
|
*
|
2015-08-23 21:18:55 +08:00
|
|
|
* Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
|
2010-02-01 13:38:10 +08:00
|
|
|
*
|
2015-08-23 21:18:55 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2010-02-01 13:38:10 +08:00
|
|
|
*
|
2015-08-23 21:18:55 +08:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
2015-08-12 22:32:09 +08:00
|
|
|
*
|
2015-08-23 21:18:55 +08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
2015-08-12 22:32:09 +08:00
|
|
|
*
|
2010-02-01 13:38:10 +08:00
|
|
|
*/
|
|
|
|
|
2015-08-12 22:32:10 +08:00
|
|
|
#define pr_fmt(fmt) "vga_switcheroo: " fmt
|
|
|
|
|
2016-05-31 17:13:27 +08:00
|
|
|
#include <linux/apple-gmux.h>
|
2015-09-05 19:40:23 +08:00
|
|
|
#include <linux/console.h>
|
2010-02-01 13:38:10 +08:00
|
|
|
#include <linux/debugfs.h>
|
|
|
|
#include <linux/fb.h>
|
2015-09-05 19:40:23 +08:00
|
|
|
#include <linux/fs.h>
|
2019-05-28 17:03:00 +08:00
|
|
|
#include <linux/fbcon.h>
|
2015-09-05 19:40:23 +08:00
|
|
|
#include <linux/module.h>
|
2010-02-01 13:38:10 +08:00
|
|
|
#include <linux/pci.h>
|
2016-01-07 23:46:13 +08:00
|
|
|
#include <linux/pm_domain.h>
|
2012-09-10 10:28:36 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
2015-09-05 19:40:23 +08:00
|
|
|
#include <linux/seq_file.h>
|
|
|
|
#include <linux/uaccess.h>
|
2012-04-17 04:26:03 +08:00
|
|
|
#include <linux/vgaarb.h>
|
2015-09-05 19:40:23 +08:00
|
|
|
#include <linux/vga_switcheroo.h>
|
2012-04-17 04:26:03 +08:00
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* DOC: Overview
|
|
|
|
*
|
|
|
|
* vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
|
|
|
|
* These come in two flavors:
|
|
|
|
*
|
|
|
|
* * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
|
|
|
|
* * muxless: Dual GPUs but only one of them is connected to outputs.
|
2016-07-11 16:22:33 +08:00
|
|
|
* The other one is merely used to offload rendering, its results
|
|
|
|
* are copied over PCIe into the framebuffer. On Linux this is
|
|
|
|
* supported with DRI PRIME.
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* Hybrid graphics started to appear in the late Naughties and were initially
|
|
|
|
* all muxed. Newer laptops moved to a muxless architecture for cost reasons.
|
|
|
|
* A notable exception is the MacBook Pro which continues to use a mux.
|
|
|
|
* Muxes come with varying capabilities: Some switch only the panel, others
|
|
|
|
* can also switch external displays. Some switch all display pins at once
|
|
|
|
* while others can switch just the DDC lines. (To allow EDID probing
|
|
|
|
* for the inactive GPU.) Also, muxes are often used to cut power to the
|
|
|
|
* discrete GPU while it is not used.
|
|
|
|
*
|
2016-01-03 01:47:17 +08:00
|
|
|
* DRM drivers register GPUs with vga_switcheroo, these are henceforth called
|
2015-08-23 21:18:55 +08:00
|
|
|
* clients. The mux is called the handler. Muxless machines also register a
|
|
|
|
* handler to control the power state of the discrete GPU, its ->switchto
|
|
|
|
* callback is a no-op for obvious reasons. The discrete GPU is often equipped
|
|
|
|
* with an HDA controller for the HDMI/DP audio signal, this will also
|
|
|
|
* register as a client so that vga_switcheroo can take care of the correct
|
|
|
|
* suspend/resume order when changing the discrete GPU's power state. In total
|
|
|
|
* there can thus be up to three clients: Two vga clients (GPUs) and one audio
|
|
|
|
* client (on the discrete GPU). The code is mostly prepared to support
|
|
|
|
* machines with more than two GPUs should they become available.
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
*
|
2015-08-23 21:18:55 +08:00
|
|
|
* The GPU to which the outputs are currently switched is called the
|
|
|
|
* active client in vga_switcheroo parlance. The GPU not in use is the
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
* inactive client. When the inactive client's DRM driver is loaded,
|
|
|
|
* it will be unable to probe the panel's EDID and hence depends on
|
|
|
|
* VBIOS to provide its display modes. If the VBIOS modes are bogus or
|
|
|
|
* if there is no VBIOS at all (which is common on the MacBook Pro),
|
|
|
|
* a client may alternatively request that the DDC lines are temporarily
|
|
|
|
* switched to it, provided that the handler supports this. Switching
|
|
|
|
* only the DDC lines and not the entire output avoids unnecessary
|
|
|
|
* flickering.
|
2015-08-23 21:18:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct vga_switcheroo_client - registered client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @fb_info: framebuffer to which console is remapped on switching
|
2018-03-03 17:53:24 +08:00
|
|
|
* @pwr_state: current power state if manual power control is used.
|
|
|
|
* For driver power control, call vga_switcheroo_pwr_state().
|
2015-08-23 21:18:55 +08:00
|
|
|
* @ops: client callbacks
|
2015-08-28 19:30:32 +08:00
|
|
|
* @id: client identifier. Determining the id requires the handler,
|
2017-03-01 02:59:52 +08:00
|
|
|
* so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
|
|
|
|
* and later given their true id in vga_switcheroo_enable()
|
2015-08-23 21:18:55 +08:00
|
|
|
* @active: whether the outputs are currently switched to this client
|
|
|
|
* @driver_power_control: whether power state is controlled by the driver's
|
2017-03-01 02:59:52 +08:00
|
|
|
* runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
|
|
|
|
* interface is a no-op so as not to interfere with runtime pm
|
2015-08-23 21:18:55 +08:00
|
|
|
* @list: client list
|
2018-07-17 16:20:50 +08:00
|
|
|
* @vga_dev: pci device, indicate which GPU is bound to current audio client
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* Registered client. A client can be either a GPU or an audio device on a GPU.
|
2018-07-17 16:20:50 +08:00
|
|
|
* For audio clients, the @fb_info and @active members are bogus. For GPU
|
|
|
|
* clients, the @vga_dev is bogus.
|
2015-08-23 21:18:55 +08:00
|
|
|
*/
|
2010-02-01 13:38:10 +08:00
|
|
|
struct vga_switcheroo_client {
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
struct fb_info *fb_info;
|
2015-08-28 17:56:26 +08:00
|
|
|
enum vga_switcheroo_state pwr_state;
|
2012-05-11 13:51:17 +08:00
|
|
|
const struct vga_switcheroo_client_ops *ops;
|
2015-08-28 18:54:07 +08:00
|
|
|
enum vga_switcheroo_client_id id;
|
2010-02-01 13:38:10 +08:00
|
|
|
bool active;
|
2012-09-10 10:28:36 +08:00
|
|
|
bool driver_power_control;
|
2012-04-26 18:55:59 +08:00
|
|
|
struct list_head list;
|
2018-07-17 16:20:50 +08:00
|
|
|
struct pci_dev *vga_dev;
|
2010-02-01 13:38:10 +08:00
|
|
|
};
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/*
|
|
|
|
* protects access to struct vgasr_priv
|
|
|
|
*/
|
2010-02-01 13:38:10 +08:00
|
|
|
static DEFINE_MUTEX(vgasr_mutex);
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* struct vgasr_priv - vga_switcheroo private data
|
|
|
|
* @active: whether vga_switcheroo is enabled.
|
2017-03-01 02:59:52 +08:00
|
|
|
* Prerequisite is the registration of two GPUs and a handler
|
2015-08-23 21:18:55 +08:00
|
|
|
* @delayed_switch_active: whether a delayed switch is pending
|
|
|
|
* @delayed_client_id: client to which a delayed switch is pending
|
|
|
|
* @debugfs_root: directory for vga_switcheroo debugfs interface
|
|
|
|
* @registered_clients: number of registered GPUs
|
2017-03-01 02:59:52 +08:00
|
|
|
* (counting only vga clients, not audio clients)
|
2015-08-23 21:18:55 +08:00
|
|
|
* @clients: list of registered clients
|
|
|
|
* @handler: registered handler
|
2016-01-12 03:09:20 +08:00
|
|
|
* @handler_flags: flags of registered handler
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
* @mux_hw_lock: protects mux state
|
|
|
|
* (in particular while DDC lines are temporarily switched)
|
|
|
|
* @old_ddc_owner: client to which DDC lines will be switched back on unlock
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* vga_switcheroo private data. Currently only one vga_switcheroo instance
|
|
|
|
* per system is supported.
|
|
|
|
*/
|
2010-02-01 13:38:10 +08:00
|
|
|
struct vgasr_priv {
|
|
|
|
bool active;
|
|
|
|
bool delayed_switch_active;
|
|
|
|
enum vga_switcheroo_client_id delayed_client_id;
|
|
|
|
|
|
|
|
struct dentry *debugfs_root;
|
|
|
|
|
|
|
|
int registered_clients;
|
2012-04-26 18:55:59 +08:00
|
|
|
struct list_head clients;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2015-10-18 19:05:40 +08:00
|
|
|
const struct vga_switcheroo_handler *handler;
|
2016-01-12 03:09:20 +08:00
|
|
|
enum vga_switcheroo_handler_flags_t handler_flags;
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
struct mutex mux_hw_lock;
|
|
|
|
int old_ddc_owner;
|
2010-02-01 13:38:10 +08:00
|
|
|
};
|
|
|
|
|
2012-04-26 20:29:48 +08:00
|
|
|
#define ID_BIT_AUDIO 0x100
|
2018-07-17 16:20:50 +08:00
|
|
|
#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
|
|
|
|
#define client_is_vga(c) (!client_is_audio(c))
|
2012-04-26 20:29:48 +08:00
|
|
|
#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
|
|
|
|
|
2019-06-13 19:44:55 +08:00
|
|
|
static void vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
|
2010-02-01 13:38:10 +08:00
|
|
|
static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
|
|
|
|
|
|
|
|
/* only one switcheroo per system */
|
2012-04-26 18:55:59 +08:00
|
|
|
static struct vgasr_priv vgasr_priv = {
|
|
|
|
.clients = LIST_HEAD_INIT(vgasr_priv.clients),
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
.mux_hw_lock = __MUTEX_INITIALIZER(vgasr_priv.mux_hw_lock),
|
2012-04-26 18:55:59 +08:00
|
|
|
};
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2012-08-18 00:17:03 +08:00
|
|
|
static bool vga_switcheroo_ready(void)
|
2010-02-01 13:38:10 +08:00
|
|
|
{
|
2012-08-18 00:17:03 +08:00
|
|
|
/* we're ready if we get two clients + handler */
|
|
|
|
return !vgasr_priv.active &&
|
|
|
|
vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vga_switcheroo_enable(void)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
/* call the handler to init */
|
2012-08-18 00:17:02 +08:00
|
|
|
if (vgasr_priv.handler->init)
|
|
|
|
vgasr_priv.handler->init();
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2018-07-17 16:20:50 +08:00
|
|
|
if (!client_is_vga(client) ||
|
|
|
|
client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID)
|
2012-04-26 20:29:48 +08:00
|
|
|
continue;
|
2018-07-17 16:20:50 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
ret = vgasr_priv.handler->get_client_id(client->pdev);
|
2010-02-01 13:38:10 +08:00
|
|
|
if (ret < 0)
|
|
|
|
return;
|
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
client->id = ret;
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
2018-07-17 16:20:50 +08:00
|
|
|
|
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
|
|
|
if (!client_is_audio(client) ||
|
|
|
|
client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = vgasr_priv.handler->get_client_id(client->vga_dev);
|
|
|
|
if (ret < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client->id = ret | ID_BIT_AUDIO;
|
ALSA: hda - Enable runtime PM only for discrete GPU
The recent change of vga_switcheroo allowed the runtime PM for
HD-audio on AMD GPUs, but this also resulted in a regression. When
the HD-audio controller driver gets runtime-suspended, HD-audio link
is turned off, and the hotplug notification is ignored. This leads to
the inconsistent audio state (the connection isn't notified and ELD is
ignored).
The best fix would be to implement the proper ELD notification via the
audio component, but it's still not ready. As a quick workaround,
this patch adds the check of runtime_idle and allows the runtime
suspend only when the vga_switcheroo is bound with discrete GPU.
That is, a system with a single GPU and APU would be again without
runtime PM to keep the HD-audio link for the hotplug notification and
ELD read out.
Also, the codec->auto_runtime_pm flag is set only for the discrete GPU
at the time GPU gets bound via vga_switcheroo (i.e. only dGPU is
forcibly runtime-PM enabled), so that APU can still get the ELD
notification.
For identifying which GPU is bound, a new vga_switcheroo client
callback, gpu_bound, is implemented. The vga_switcheroo simply calls
this when GPU is bound, and tells whether it's dGPU or APU.
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=200945
Fixes: 07f4f97d7b4b ("vga_switcheroo: Use device link for HDA controller")
Reported-by: Jian-Hong Pan <jian-hong@endlessm.com>
Tested-by: Jian-Hong Pan <jian-hong@endlessm.com>
Acked-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Takashi Iwai <tiwai@suse.de>
2018-09-10 22:20:25 +08:00
|
|
|
if (client->ops->gpu_bound)
|
|
|
|
client->ops->gpu_bound(client->pdev, ret);
|
2018-07-17 16:20:50 +08:00
|
|
|
}
|
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
vga_switcheroo_debugfs_init(&vgasr_priv);
|
|
|
|
vgasr_priv.active = true;
|
|
|
|
}
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_register_handler() - register handler
|
|
|
|
* @handler: handler callbacks
|
2016-01-12 03:09:20 +08:00
|
|
|
* @handler_flags: handler flags
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* Register handler. Enable vga_switcheroo if two vga clients have already
|
|
|
|
* registered.
|
|
|
|
*
|
|
|
|
* Return: 0 on success, -EINVAL if a handler was already registered.
|
|
|
|
*/
|
2017-03-01 02:59:52 +08:00
|
|
|
int vga_switcheroo_register_handler(
|
|
|
|
const struct vga_switcheroo_handler *handler,
|
|
|
|
enum vga_switcheroo_handler_flags_t handler_flags)
|
2012-08-18 00:17:03 +08:00
|
|
|
{
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
if (vgasr_priv.handler) {
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
vgasr_priv.handler = handler;
|
2016-01-12 03:09:20 +08:00
|
|
|
vgasr_priv.handler_flags = handler_flags;
|
2012-08-18 00:17:03 +08:00
|
|
|
if (vga_switcheroo_ready()) {
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_info("enabled\n");
|
2012-08-18 00:17:03 +08:00
|
|
|
vga_switcheroo_enable();
|
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_register_handler);
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_unregister_handler() - unregister handler
|
|
|
|
*
|
|
|
|
* Unregister handler. Disable vga_switcheroo.
|
|
|
|
*/
|
2012-08-18 00:17:03 +08:00
|
|
|
void vga_switcheroo_unregister_handler(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&vgasr_mutex);
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
mutex_lock(&vgasr_priv.mux_hw_lock);
|
2016-01-12 03:09:20 +08:00
|
|
|
vgasr_priv.handler_flags = 0;
|
2012-08-18 00:17:03 +08:00
|
|
|
vgasr_priv.handler = NULL;
|
|
|
|
if (vgasr_priv.active) {
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_info("disabled\n");
|
2012-08-18 00:17:03 +08:00
|
|
|
vga_switcheroo_debugfs_fini(&vgasr_priv);
|
|
|
|
vgasr_priv.active = false;
|
|
|
|
}
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
mutex_unlock(&vgasr_priv.mux_hw_lock);
|
2012-08-18 00:17:03 +08:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
|
|
|
|
|
2016-01-12 03:09:20 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_handler_flags() - obtain handler flags
|
|
|
|
*
|
|
|
|
* Helper for clients to obtain the handler flags bitmask.
|
|
|
|
*
|
|
|
|
* Return: Handler flags. A value of 0 means that no handler is registered
|
|
|
|
* or that the handler has no special capabilities.
|
|
|
|
*/
|
|
|
|
enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void)
|
|
|
|
{
|
|
|
|
return vgasr_priv.handler_flags;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_handler_flags);
|
|
|
|
|
2012-04-26 20:29:48 +08:00
|
|
|
static int register_client(struct pci_dev *pdev,
|
|
|
|
const struct vga_switcheroo_client_ops *ops,
|
2018-07-17 16:20:50 +08:00
|
|
|
enum vga_switcheroo_client_id id,
|
|
|
|
struct pci_dev *vga_dev,
|
|
|
|
bool active,
|
2015-08-28 18:54:07 +08:00
|
|
|
bool driver_power_control)
|
2010-02-01 13:38:10 +08:00
|
|
|
{
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
client = kzalloc(sizeof(*client), GFP_KERNEL);
|
|
|
|
if (!client)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
client->pwr_state = VGA_SWITCHEROO_ON;
|
|
|
|
client->pdev = pdev;
|
2012-05-11 13:51:17 +08:00
|
|
|
client->ops = ops;
|
2012-04-26 20:29:48 +08:00
|
|
|
client->id = id;
|
|
|
|
client->active = active;
|
2012-09-10 10:28:36 +08:00
|
|
|
client->driver_power_control = driver_power_control;
|
2018-07-17 16:20:50 +08:00
|
|
|
client->vga_dev = vga_dev;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
list_add_tail(&client->list, &vgasr_priv.clients);
|
2012-04-26 20:29:48 +08:00
|
|
|
if (client_is_vga(client))
|
|
|
|
vgasr_priv.registered_clients++;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2012-08-18 00:17:03 +08:00
|
|
|
if (vga_switcheroo_ready()) {
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_info("enabled\n");
|
2010-02-01 13:38:10 +08:00
|
|
|
vga_switcheroo_enable();
|
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-26 20:29:48 +08:00
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_register_client - register vga client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @ops: client callbacks
|
|
|
|
* @driver_power_control: whether power state is controlled by the driver's
|
2017-03-01 02:59:52 +08:00
|
|
|
* runtime pm
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* Register vga client (GPU). Enable vga_switcheroo if another GPU and a
|
|
|
|
* handler have already registered. The power state of the client is assumed
|
2016-05-31 17:13:27 +08:00
|
|
|
* to be ON. Beforehand, vga_switcheroo_client_probe_defer() shall be called
|
|
|
|
* to ensure that all prerequisites are met.
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* Return: 0 on success, -ENOMEM on memory allocation error.
|
|
|
|
*/
|
2012-04-26 20:29:48 +08:00
|
|
|
int vga_switcheroo_register_client(struct pci_dev *pdev,
|
2012-09-10 10:28:36 +08:00
|
|
|
const struct vga_switcheroo_client_ops *ops,
|
|
|
|
bool driver_power_control)
|
2012-04-26 20:29:48 +08:00
|
|
|
{
|
2018-07-17 16:20:50 +08:00
|
|
|
return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID, NULL,
|
2015-08-12 22:32:11 +08:00
|
|
|
pdev == vga_default_device(),
|
|
|
|
driver_power_control);
|
2012-04-26 20:29:48 +08:00
|
|
|
}
|
2010-02-01 13:38:10 +08:00
|
|
|
EXPORT_SYMBOL(vga_switcheroo_register_client);
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_register_audio_client - register audio client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @ops: client callbacks
|
2018-07-17 16:20:50 +08:00
|
|
|
* @vga_dev: pci device which is bound to current audio client
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
vga_switcheroo: Use device link for HDA controller
Back in 2013, runtime PM for GPUs with integrated HDA controller was
introduced with commits 0d69704ae348 ("gpu/vga_switcheroo: add driver
control power feature. (v3)") and 246efa4a072f ("snd/hda: add runtime
suspend/resume on optimus support (v4)").
Briefly, the idea was that the HDA controller is forced on and off in
unison with the GPU.
The original code is mostly still in place even though it was never a
100% perfect solution: E.g. on access to the HDA controller, the GPU
is powered up via vga_switcheroo_runtime_resume_hdmi_audio() but there
are no provisions to keep it resumed until access to the HDA controller
has ceased: The GPU autosuspends after 5 seconds, rendering the HDA
controller inaccessible.
Additionally, a kludge is required when hda_intel.c probes: It has to
check whether the GPU is powered down (check_hdmi_disabled()) and defer
probing if so.
However in the meantime (in v4.10) the driver core has gained a feature
called device links which promises to solve such issues in a clean way:
It allows us to declare a dependency from the HDA controller (consumer)
to the GPU (supplier). The PM core then automagically ensures that the
GPU is runtime resumed as long as the HDA controller's ->probe hook is
executed and whenever the HDA controller is accessed.
By default, the HDA controller has a dependency on its parent, a PCIe
Root Port. Adding a device link creates another dependency on its
sibling:
PCIe Root Port
^ ^
| |
| |
HDA ===> GPU
The device link is not only used for runtime PM, it also guarantees that
on system sleep, the HDA controller suspends before the GPU and resumes
after the GPU, and on system shutdown the HDA controller's ->shutdown
hook is executed before the one of the GPU. It is a complete solution.
Using this functionality is as simple as calling device_link_add(),
which results in a dmesg entry like this:
pci 0000:01:00.1: Linked as a consumer to 0000:01:00.0
The code for the GPU-governed audio power management can thus be removed
(except where it's still needed for legacy manual power control).
The device link is added in a PCI quirk rather than in hda_intel.c.
It is therefore legal for the GPU to runtime suspend to D3cold even if
the HDA controller is not bound to a driver or if CONFIG_SND_HDA_INTEL
is not enabled, for accesses to the HDA controller will cause the GPU to
wake up regardless if they're occurring outside of hda_intel.c (think
config space readout via sysfs).
Contrary to the previous implementation, the HDA controller's power
state is now self-governed, rather than GPU-governed, whereas the GPU's
power state is no longer fully self-governed. (The HDA controller needs
to runtime suspend before the GPU can.)
It is thus crucial that runtime PM is always activated on the HDA
controller even if CONFIG_SND_HDA_POWER_SAVE_DEFAULT is set to 0 (which
is the default), lest the GPU stays awake. This is achieved by setting
the auto_runtime_pm flag on every codec and the AZX_DCAPS_PM_RUNTIME
flag on the HDA controller.
A side effect is that power consumption might be reduced if the GPU is
in use but the HDA controller is not, because the HDA controller is now
allowed to go to D3hot. Before, it was forced to stay in D0 as long as
the GPU was in use. (There is no reduction in power consumption on my
Nvidia GK107, but there might be on other chips.)
The code paths for legacy manual power control are adjusted such that
runtime PM is disabled during power off, thereby preventing the PM core
from resuming the HDA controller.
Note that the device link is not only added on vga_switcheroo capable
systems, but for *any* GPU with integrated HDA controller. The idea is
that the HDA controller streams audio via connectors located on the GPU,
so the GPU needs to be on for the HDA controller to do anything useful.
This commit implicitly fixes an unbalanced runtime PM ref upon unbind of
hda_intel.c: On ->probe, a runtime PM ref was previously released under
the condition "azx_has_pm_runtime(chip) || hda->use_vga_switcheroo", but
on ->remove a runtime PM ref was only acquired under the first of those
conditions. Thus, binding and unbinding the driver twice on a
vga_switcheroo capable system caused the runtime PM refcount to drop
below zero. The issue is resolved because the AZX_DCAPS_PM_RUNTIME flag
is now always set if use_vga_switcheroo is true.
For more information on device links please refer to:
https://www.kernel.org/doc/html/latest/driver-api/device_link.html
Documentation/driver-api/device_link.rst
Cc: Dave Airlie <airlied@redhat.com>
Cc: Ben Skeggs <bskeggs@redhat.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
Tested-by: Kai Heng Feng <kai.heng.feng@canonical.com> # AMD PowerXpress
Tested-by: Mike Lothian <mike@fireburn.co.uk> # AMD PowerXpress
Tested-by: Denis Lisov <dennis.lissov@gmail.com> # Nvidia Optimus
Tested-by: Peter Wu <peter@lekensteyn.nl> # Nvidia Optimus
Tested-by: Lukas Wunner <lukas@wunner.de> # MacBook Pro
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Link: https://patchwork.freedesktop.org/patch/msgid/51bd38360ff502a8c42b1ebf4405ee1d3f27118d.1520068884.git.lukas@wunner.de
2018-03-03 17:53:24 +08:00
|
|
|
* Register audio client (audio device on a GPU). The client is assumed
|
|
|
|
* to use runtime PM. Beforehand, vga_switcheroo_client_probe_defer()
|
2016-05-31 17:13:27 +08:00
|
|
|
* shall be called to ensure that all prerequisites are met.
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
2018-07-17 16:20:50 +08:00
|
|
|
* Return: 0 on success, -ENOMEM on memory allocation error, -EINVAL on getting
|
|
|
|
* client id error.
|
2015-08-23 21:18:55 +08:00
|
|
|
*/
|
2012-04-26 20:29:48 +08:00
|
|
|
int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
|
2017-03-01 02:59:52 +08:00
|
|
|
const struct vga_switcheroo_client_ops *ops,
|
2018-07-17 16:20:50 +08:00
|
|
|
struct pci_dev *vga_dev)
|
2012-04-26 20:29:48 +08:00
|
|
|
{
|
2018-07-17 16:20:50 +08:00
|
|
|
enum vga_switcheroo_client_id id = VGA_SWITCHEROO_UNKNOWN_ID;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if vga_switcheroo has enabled, that mean two GPU clients and also
|
|
|
|
* handler are registered. Get audio client id from bound GPU client
|
|
|
|
* id directly, otherwise, set it as VGA_SWITCHEROO_UNKNOWN_ID,
|
|
|
|
* it will set to correct id in later when vga_switcheroo_enable()
|
|
|
|
* is called.
|
|
|
|
*/
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
if (vgasr_priv.active) {
|
|
|
|
id = vgasr_priv.handler->get_client_id(vga_dev);
|
|
|
|
if (id < 0) {
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2018-11-05 18:52:50 +08:00
|
|
|
/* notify if GPU has been already bound */
|
|
|
|
if (ops->gpu_bound)
|
|
|
|
ops->gpu_bound(pdev, id);
|
2018-07-17 16:20:50 +08:00
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
|
|
|
|
return register_client(pdev, ops, id | ID_BIT_AUDIO, vga_dev,
|
|
|
|
false, true);
|
2012-04-26 20:29:48 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
|
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
static struct vga_switcheroo_client *
|
|
|
|
find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-12 22:32:11 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
list_for_each_entry(client, head, list)
|
|
|
|
if (client->pdev == pdev)
|
|
|
|
return client;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vga_switcheroo_client *
|
2015-08-28 18:54:07 +08:00
|
|
|
find_client_from_id(struct list_head *head,
|
|
|
|
enum vga_switcheroo_client_id client_id)
|
2012-04-26 18:55:59 +08:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-12 22:32:11 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
list_for_each_entry(client, head, list)
|
|
|
|
if (client->id == client_id)
|
|
|
|
return client;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct vga_switcheroo_client *
|
|
|
|
find_active_client(struct list_head *head)
|
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-12 22:32:11 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
list_for_each_entry(client, head, list)
|
2015-08-27 22:43:43 +08:00
|
|
|
if (client->active)
|
2012-04-26 18:55:59 +08:00
|
|
|
return client;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-05-31 17:13:27 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_client_probe_defer() - whether to defer probing a given client
|
|
|
|
* @pdev: client pci device
|
|
|
|
*
|
|
|
|
* Determine whether any prerequisites are not fulfilled to probe a given
|
|
|
|
* client. Drivers shall invoke this early on in their ->probe callback
|
|
|
|
* and return %-EPROBE_DEFER if it evaluates to %true. Thou shalt not
|
|
|
|
* register the client ere thou hast called this.
|
|
|
|
*
|
|
|
|
* Return: %true if probing should be deferred, otherwise %false.
|
|
|
|
*/
|
|
|
|
bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev)
|
|
|
|
{
|
2016-05-31 17:13:27 +08:00
|
|
|
if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
|
|
|
|
/*
|
|
|
|
* apple-gmux is needed on pre-retina MacBook Pro
|
|
|
|
* to probe the panel if pdev is the inactive GPU.
|
|
|
|
*/
|
|
|
|
if (apple_gmux_present() && pdev != vga_default_device() &&
|
|
|
|
!vgasr_priv.handler_flags)
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-31 17:13:27 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_client_probe_defer);
|
|
|
|
|
2018-03-03 17:53:24 +08:00
|
|
|
static enum vga_switcheroo_state
|
|
|
|
vga_switcheroo_pwr_state(struct vga_switcheroo_client *client)
|
|
|
|
{
|
|
|
|
if (client->driver_power_control)
|
|
|
|
if (pm_runtime_enabled(&client->pdev->dev) &&
|
|
|
|
pm_runtime_active(&client->pdev->dev))
|
|
|
|
return VGA_SWITCHEROO_ON;
|
|
|
|
else
|
|
|
|
return VGA_SWITCHEROO_OFF;
|
|
|
|
else
|
|
|
|
return client->pwr_state;
|
|
|
|
}
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_get_client_state() - obtain power state of a given client
|
|
|
|
* @pdev: client pci device
|
|
|
|
*
|
|
|
|
* Obtain power state of a given client as seen from vga_switcheroo.
|
|
|
|
* The function is only called from hda_intel.c.
|
|
|
|
*
|
|
|
|
* Return: Power state.
|
|
|
|
*/
|
2015-08-28 17:56:26 +08:00
|
|
|
enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
|
2012-06-07 18:15:15 +08:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
2015-08-24 05:23:02 +08:00
|
|
|
enum vga_switcheroo_state ret;
|
2012-06-07 18:15:15 +08:00
|
|
|
|
2015-08-24 05:23:02 +08:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-06-07 18:15:15 +08:00
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (!client)
|
2015-08-24 05:23:02 +08:00
|
|
|
ret = VGA_SWITCHEROO_NOT_FOUND;
|
|
|
|
else
|
2018-03-03 17:53:24 +08:00
|
|
|
ret = vga_switcheroo_pwr_state(client);
|
2015-08-24 05:23:02 +08:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return ret;
|
2012-06-07 18:15:15 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_get_client_state);
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_unregister_client() - unregister client
|
|
|
|
* @pdev: client pci device
|
|
|
|
*
|
|
|
|
* Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
|
|
|
|
*/
|
2010-02-01 13:38:10 +08:00
|
|
|
void vga_switcheroo_unregister_client(struct pci_dev *pdev)
|
|
|
|
{
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-04-26 18:55:59 +08:00
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (client) {
|
2012-04-26 20:29:48 +08:00
|
|
|
if (client_is_vga(client))
|
|
|
|
vgasr_priv.registered_clients--;
|
2012-04-26 18:55:59 +08:00
|
|
|
list_del(&client->list);
|
|
|
|
kfree(client);
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
2012-04-26 20:29:48 +08:00
|
|
|
if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_info("disabled\n");
|
2012-04-26 20:29:48 +08:00
|
|
|
vga_switcheroo_debugfs_fini(&vgasr_priv);
|
|
|
|
vgasr_priv.active = false;
|
|
|
|
}
|
2010-02-01 13:38:10 +08:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_unregister_client);
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_client_fb_set() - set framebuffer of a given client
|
|
|
|
* @pdev: client pci device
|
|
|
|
* @info: framebuffer
|
|
|
|
*
|
|
|
|
* Set framebuffer of a given client. The console will be remapped to this
|
|
|
|
* on switching.
|
|
|
|
*/
|
2010-02-01 13:38:10 +08:00
|
|
|
void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
|
|
|
|
struct fb_info *info)
|
|
|
|
{
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-04-26 18:55:59 +08:00
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (client)
|
|
|
|
client->fb_info = info;
|
2010-02-01 13:38:10 +08:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
|
|
|
|
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client
|
|
|
|
* @pdev: client pci device
|
|
|
|
*
|
|
|
|
* Temporarily switch DDC lines to the client identified by @pdev
|
|
|
|
* (but leave the outputs otherwise switched to where they are).
|
|
|
|
* This allows the inactive client to probe EDID. The DDC lines must
|
|
|
|
* afterwards be switched back by calling vga_switcheroo_unlock_ddc(),
|
|
|
|
* even if this function returns an error.
|
|
|
|
*
|
|
|
|
* Return: Previous DDC owner on success or a negative int on error.
|
|
|
|
* Specifically, %-ENODEV if no handler has registered or if the handler
|
|
|
|
* does not support switching the DDC lines. Also, a negative value
|
|
|
|
* returned by the handler is propagated back to the caller.
|
|
|
|
* The return value has merely an informational purpose for any caller
|
|
|
|
* which might be interested in it. It is acceptable to ignore the return
|
|
|
|
* value and simply rely on the result of the subsequent EDID probe,
|
|
|
|
* which will be %NULL if DDC switching failed.
|
|
|
|
*/
|
|
|
|
int vga_switcheroo_lock_ddc(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
enum vga_switcheroo_client_id id;
|
|
|
|
|
|
|
|
mutex_lock(&vgasr_priv.mux_hw_lock);
|
|
|
|
if (!vgasr_priv.handler || !vgasr_priv.handler->switch_ddc) {
|
|
|
|
vgasr_priv.old_ddc_owner = -ENODEV;
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
id = vgasr_priv.handler->get_client_id(pdev);
|
|
|
|
vgasr_priv.old_ddc_owner = vgasr_priv.handler->switch_ddc(id);
|
|
|
|
return vgasr_priv.old_ddc_owner;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_lock_ddc);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner
|
|
|
|
* @pdev: client pci device
|
|
|
|
*
|
|
|
|
* Switch DDC lines back to the previous owner after calling
|
|
|
|
* vga_switcheroo_lock_ddc(). This must be called even if
|
|
|
|
* vga_switcheroo_lock_ddc() returned an error.
|
|
|
|
*
|
|
|
|
* Return: Previous DDC owner on success (i.e. the client identifier of @pdev)
|
|
|
|
* or a negative int on error.
|
|
|
|
* Specifically, %-ENODEV if no handler has registered or if the handler
|
|
|
|
* does not support switching the DDC lines. Also, a negative value
|
|
|
|
* returned by the handler is propagated back to the caller.
|
|
|
|
* Finally, invoking this function without calling vga_switcheroo_lock_ddc()
|
|
|
|
* first is not allowed and will result in %-EINVAL.
|
|
|
|
*/
|
|
|
|
int vga_switcheroo_unlock_ddc(struct pci_dev *pdev)
|
|
|
|
{
|
|
|
|
enum vga_switcheroo_client_id id;
|
|
|
|
int ret = vgasr_priv.old_ddc_owner;
|
|
|
|
|
|
|
|
if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv.mux_hw_lock)))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (vgasr_priv.old_ddc_owner >= 0) {
|
|
|
|
id = vgasr_priv.handler->get_client_id(pdev);
|
|
|
|
if (vgasr_priv.old_ddc_owner != id)
|
|
|
|
ret = vgasr_priv.handler->switch_ddc(
|
|
|
|
vgasr_priv.old_ddc_owner);
|
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_priv.mux_hw_lock);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_unlock_ddc);
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* DOC: Manual switching and manual power control
|
|
|
|
*
|
|
|
|
* In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
|
|
|
|
* can be read to retrieve the current vga_switcheroo state and commands
|
|
|
|
* can be written to it to change the state. The file appears as soon as
|
|
|
|
* two GPU drivers and one handler have registered with vga_switcheroo.
|
|
|
|
* The following commands are understood:
|
|
|
|
*
|
|
|
|
* * OFF: Power off the device not in use.
|
|
|
|
* * ON: Power on the device not in use.
|
|
|
|
* * IGD: Switch to the integrated graphics device.
|
2016-07-11 16:22:33 +08:00
|
|
|
* Power on the integrated GPU if necessary, power off the discrete GPU.
|
|
|
|
* Prerequisite is that no user space processes (e.g. Xorg, alsactl)
|
|
|
|
* have opened device files of the GPUs or the audio client. If the
|
|
|
|
* switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
|
|
|
|
* and /dev/snd/controlC1 to identify processes blocking the switch.
|
2015-08-23 21:18:55 +08:00
|
|
|
* * DIS: Switch to the discrete graphics device.
|
|
|
|
* * DIGD: Delayed switch to the integrated graphics device.
|
2016-07-11 16:22:33 +08:00
|
|
|
* This will perform the switch once the last user space process has
|
|
|
|
* closed the device files of the GPUs and the audio client.
|
2015-08-23 21:18:55 +08:00
|
|
|
* * DDIS: Delayed switch to the discrete graphics device.
|
|
|
|
* * MIGD: Mux-only switch to the integrated graphics device.
|
2016-07-11 16:22:33 +08:00
|
|
|
* Does not remap console or change the power state of either gpu.
|
|
|
|
* If the integrated GPU is currently off, the screen will turn black.
|
|
|
|
* If it is on, the screen will show whatever happens to be in VRAM.
|
|
|
|
* Either way, the user has to blindly enter the command to switch back.
|
2015-08-23 21:18:55 +08:00
|
|
|
* * MDIS: Mux-only switch to the discrete graphics device.
|
|
|
|
*
|
|
|
|
* For GPUs whose power state is controlled by the driver's runtime pm,
|
|
|
|
* the ON and OFF commands are a no-op (see next section).
|
|
|
|
*
|
|
|
|
* For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
|
|
|
|
* should not be used.
|
|
|
|
*/
|
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
static int vga_switcheroo_show(struct seq_file *m, void *v)
|
|
|
|
{
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
int i = 0;
|
2015-08-12 22:32:11 +08:00
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-04-26 18:55:59 +08:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-09-10 10:28:36 +08:00
|
|
|
seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
|
2015-08-12 22:32:11 +08:00
|
|
|
client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
|
|
|
|
"IGD",
|
2012-04-26 20:29:48 +08:00
|
|
|
client_is_vga(client) ? "" : "-Audio",
|
2012-04-26 18:55:59 +08:00
|
|
|
client->active ? '+' : ' ',
|
2012-09-10 10:28:36 +08:00
|
|
|
client->driver_power_control ? "Dyn" : "",
|
2018-03-03 17:53:24 +08:00
|
|
|
vga_switcheroo_pwr_state(client) ? "Pwr" : "Off",
|
2012-04-26 18:55:59 +08:00
|
|
|
pci_name(client->pdev));
|
|
|
|
i++;
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
|
|
|
|
{
|
|
|
|
return single_open(file, vga_switcheroo_show, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switchon(struct vga_switcheroo_client *client)
|
|
|
|
{
|
2012-09-10 10:28:36 +08:00
|
|
|
if (client->driver_power_control)
|
|
|
|
return 0;
|
2010-12-06 10:31:50 +08:00
|
|
|
if (vgasr_priv.handler->power_state)
|
|
|
|
vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
|
2010-02-01 13:38:10 +08:00
|
|
|
/* call the driver callback to turn on device */
|
2012-05-11 13:51:17 +08:00
|
|
|
client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
|
2010-02-01 13:38:10 +08:00
|
|
|
client->pwr_state = VGA_SWITCHEROO_ON;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switchoff(struct vga_switcheroo_client *client)
|
|
|
|
{
|
2012-09-10 10:28:36 +08:00
|
|
|
if (client->driver_power_control)
|
|
|
|
return 0;
|
2010-02-01 13:38:10 +08:00
|
|
|
/* call the driver callback to turn off device */
|
2012-05-11 13:51:17 +08:00
|
|
|
client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
|
2010-12-06 10:31:50 +08:00
|
|
|
if (vgasr_priv.handler->power_state)
|
|
|
|
vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
|
2010-02-01 13:38:10 +08:00
|
|
|
client->pwr_state = VGA_SWITCHEROO_OFF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-28 18:54:07 +08:00
|
|
|
static void set_audio_state(enum vga_switcheroo_client_id id,
|
|
|
|
enum vga_switcheroo_state state)
|
2012-04-26 20:29:48 +08:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
|
|
|
client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
|
vga_switcheroo: Use device link for HDA controller
Back in 2013, runtime PM for GPUs with integrated HDA controller was
introduced with commits 0d69704ae348 ("gpu/vga_switcheroo: add driver
control power feature. (v3)") and 246efa4a072f ("snd/hda: add runtime
suspend/resume on optimus support (v4)").
Briefly, the idea was that the HDA controller is forced on and off in
unison with the GPU.
The original code is mostly still in place even though it was never a
100% perfect solution: E.g. on access to the HDA controller, the GPU
is powered up via vga_switcheroo_runtime_resume_hdmi_audio() but there
are no provisions to keep it resumed until access to the HDA controller
has ceased: The GPU autosuspends after 5 seconds, rendering the HDA
controller inaccessible.
Additionally, a kludge is required when hda_intel.c probes: It has to
check whether the GPU is powered down (check_hdmi_disabled()) and defer
probing if so.
However in the meantime (in v4.10) the driver core has gained a feature
called device links which promises to solve such issues in a clean way:
It allows us to declare a dependency from the HDA controller (consumer)
to the GPU (supplier). The PM core then automagically ensures that the
GPU is runtime resumed as long as the HDA controller's ->probe hook is
executed and whenever the HDA controller is accessed.
By default, the HDA controller has a dependency on its parent, a PCIe
Root Port. Adding a device link creates another dependency on its
sibling:
PCIe Root Port
^ ^
| |
| |
HDA ===> GPU
The device link is not only used for runtime PM, it also guarantees that
on system sleep, the HDA controller suspends before the GPU and resumes
after the GPU, and on system shutdown the HDA controller's ->shutdown
hook is executed before the one of the GPU. It is a complete solution.
Using this functionality is as simple as calling device_link_add(),
which results in a dmesg entry like this:
pci 0000:01:00.1: Linked as a consumer to 0000:01:00.0
The code for the GPU-governed audio power management can thus be removed
(except where it's still needed for legacy manual power control).
The device link is added in a PCI quirk rather than in hda_intel.c.
It is therefore legal for the GPU to runtime suspend to D3cold even if
the HDA controller is not bound to a driver or if CONFIG_SND_HDA_INTEL
is not enabled, for accesses to the HDA controller will cause the GPU to
wake up regardless if they're occurring outside of hda_intel.c (think
config space readout via sysfs).
Contrary to the previous implementation, the HDA controller's power
state is now self-governed, rather than GPU-governed, whereas the GPU's
power state is no longer fully self-governed. (The HDA controller needs
to runtime suspend before the GPU can.)
It is thus crucial that runtime PM is always activated on the HDA
controller even if CONFIG_SND_HDA_POWER_SAVE_DEFAULT is set to 0 (which
is the default), lest the GPU stays awake. This is achieved by setting
the auto_runtime_pm flag on every codec and the AZX_DCAPS_PM_RUNTIME
flag on the HDA controller.
A side effect is that power consumption might be reduced if the GPU is
in use but the HDA controller is not, because the HDA controller is now
allowed to go to D3hot. Before, it was forced to stay in D0 as long as
the GPU was in use. (There is no reduction in power consumption on my
Nvidia GK107, but there might be on other chips.)
The code paths for legacy manual power control are adjusted such that
runtime PM is disabled during power off, thereby preventing the PM core
from resuming the HDA controller.
Note that the device link is not only added on vga_switcheroo capable
systems, but for *any* GPU with integrated HDA controller. The idea is
that the HDA controller streams audio via connectors located on the GPU,
so the GPU needs to be on for the HDA controller to do anything useful.
This commit implicitly fixes an unbalanced runtime PM ref upon unbind of
hda_intel.c: On ->probe, a runtime PM ref was previously released under
the condition "azx_has_pm_runtime(chip) || hda->use_vga_switcheroo", but
on ->remove a runtime PM ref was only acquired under the first of those
conditions. Thus, binding and unbinding the driver twice on a
vga_switcheroo capable system caused the runtime PM refcount to drop
below zero. The issue is resolved because the AZX_DCAPS_PM_RUNTIME flag
is now always set if use_vga_switcheroo is true.
For more information on device links please refer to:
https://www.kernel.org/doc/html/latest/driver-api/device_link.html
Documentation/driver-api/device_link.rst
Cc: Dave Airlie <airlied@redhat.com>
Cc: Ben Skeggs <bskeggs@redhat.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
Tested-by: Kai Heng Feng <kai.heng.feng@canonical.com> # AMD PowerXpress
Tested-by: Mike Lothian <mike@fireburn.co.uk> # AMD PowerXpress
Tested-by: Denis Lisov <dennis.lissov@gmail.com> # Nvidia Optimus
Tested-by: Peter Wu <peter@lekensteyn.nl> # Nvidia Optimus
Tested-by: Lukas Wunner <lukas@wunner.de> # MacBook Pro
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Link: https://patchwork.freedesktop.org/patch/msgid/51bd38360ff502a8c42b1ebf4405ee1d3f27118d.1520068884.git.lukas@wunner.de
2018-03-03 17:53:24 +08:00
|
|
|
if (client)
|
2012-04-26 20:29:48 +08:00
|
|
|
client->ops->set_gpu_state(client->pdev, state);
|
|
|
|
}
|
|
|
|
|
2010-12-07 12:24:25 +08:00
|
|
|
/* stage one happens before delay */
|
|
|
|
static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
|
2010-02-01 13:38:10 +08:00
|
|
|
{
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *active;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
active = find_active_client(&vgasr_priv.clients);
|
2010-02-01 13:38:10 +08:00
|
|
|
if (!active)
|
|
|
|
return 0;
|
|
|
|
|
2018-03-03 17:53:24 +08:00
|
|
|
if (vga_switcheroo_pwr_state(new_client) == VGA_SWITCHEROO_OFF)
|
2010-02-01 13:38:10 +08:00
|
|
|
vga_switchon(new_client);
|
|
|
|
|
2012-04-17 04:26:03 +08:00
|
|
|
vga_set_default_device(new_client->pdev);
|
2010-12-07 12:24:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* post delay */
|
|
|
|
static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
|
|
|
|
{
|
|
|
|
int ret;
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *active;
|
2010-12-07 12:24:25 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
active = find_active_client(&vgasr_priv.clients);
|
2010-12-07 12:24:25 +08:00
|
|
|
if (!active)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
active->active = false;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2018-03-03 17:53:24 +08:00
|
|
|
/* let HDA controller autosuspend if GPU uses driver power control */
|
|
|
|
if (!active->driver_power_control)
|
|
|
|
set_audio_state(active->id, VGA_SWITCHEROO_OFF);
|
2012-06-09 14:46:42 +08:00
|
|
|
|
2019-05-28 17:03:00 +08:00
|
|
|
if (new_client->fb_info)
|
|
|
|
fbcon_remap_all(new_client->fb_info);
|
2010-02-01 13:38:10 +08:00
|
|
|
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
mutex_lock(&vgasr_priv.mux_hw_lock);
|
2010-02-01 13:38:10 +08:00
|
|
|
ret = vgasr_priv.handler->switchto(new_client->id);
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
mutex_unlock(&vgasr_priv.mux_hw_lock);
|
2010-02-01 13:38:10 +08:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2012-05-11 13:51:17 +08:00
|
|
|
if (new_client->ops->reprobe)
|
|
|
|
new_client->ops->reprobe(new_client->pdev);
|
2010-12-07 06:57:57 +08:00
|
|
|
|
2018-03-03 17:53:24 +08:00
|
|
|
if (vga_switcheroo_pwr_state(active) == VGA_SWITCHEROO_ON)
|
2010-02-01 13:38:10 +08:00
|
|
|
vga_switchoff(active);
|
|
|
|
|
2018-03-03 17:53:24 +08:00
|
|
|
/* let HDA controller autoresume if GPU uses driver power control */
|
|
|
|
if (!new_client->driver_power_control)
|
|
|
|
set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
|
2012-06-09 14:46:42 +08:00
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
new_client->active = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
static bool check_can_switch(void)
|
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-05-11 13:51:17 +08:00
|
|
|
if (!client->ops->can_switch(client->pdev)) {
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_err("client %x refused switch\n", client->id);
|
2012-04-26 18:55:59 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
static ssize_t
|
|
|
|
vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
|
|
|
|
size_t cnt, loff_t *ppos)
|
|
|
|
{
|
|
|
|
char usercmd[64];
|
2012-04-26 18:55:59 +08:00
|
|
|
int ret;
|
2010-02-01 13:38:10 +08:00
|
|
|
bool delay = false, can_switch;
|
2010-12-06 10:35:52 +08:00
|
|
|
bool just_mux = false;
|
2015-08-28 18:54:07 +08:00
|
|
|
enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
|
2010-02-01 13:38:10 +08:00
|
|
|
struct vga_switcheroo_client *client = NULL;
|
|
|
|
|
|
|
|
if (cnt > 63)
|
|
|
|
cnt = 63;
|
|
|
|
|
|
|
|
if (copy_from_user(usercmd, ubuf, cnt))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
|
2010-04-28 05:11:03 +08:00
|
|
|
if (!vgasr_priv.active) {
|
|
|
|
cnt = -EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2010-02-01 13:38:10 +08:00
|
|
|
|
|
|
|
/* pwr off the device not in use */
|
|
|
|
if (strncmp(usercmd, "OFF", 3) == 0) {
|
2012-04-26 18:55:59 +08:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-06-09 14:46:42 +08:00
|
|
|
if (client->active || client_is_audio(client))
|
2010-02-01 13:38:10 +08:00
|
|
|
continue;
|
2012-09-10 10:28:36 +08:00
|
|
|
if (client->driver_power_control)
|
|
|
|
continue;
|
2012-06-09 14:46:42 +08:00
|
|
|
set_audio_state(client->id, VGA_SWITCHEROO_OFF);
|
2012-04-26 18:55:59 +08:00
|
|
|
if (client->pwr_state == VGA_SWITCHEROO_ON)
|
|
|
|
vga_switchoff(client);
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* pwr on the device not in use */
|
|
|
|
if (strncmp(usercmd, "ON", 2) == 0) {
|
2012-04-26 18:55:59 +08:00
|
|
|
list_for_each_entry(client, &vgasr_priv.clients, list) {
|
2012-06-09 14:46:42 +08:00
|
|
|
if (client->active || client_is_audio(client))
|
2010-02-01 13:38:10 +08:00
|
|
|
continue;
|
2012-09-10 10:28:36 +08:00
|
|
|
if (client->driver_power_control)
|
|
|
|
continue;
|
2012-04-26 18:55:59 +08:00
|
|
|
if (client->pwr_state == VGA_SWITCHEROO_OFF)
|
|
|
|
vga_switchon(client);
|
2012-06-09 14:46:42 +08:00
|
|
|
set_audio_state(client->id, VGA_SWITCHEROO_ON);
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* request a delayed switch - test can we switch now */
|
|
|
|
if (strncmp(usercmd, "DIGD", 4) == 0) {
|
|
|
|
client_id = VGA_SWITCHEROO_IGD;
|
|
|
|
delay = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(usercmd, "DDIS", 4) == 0) {
|
|
|
|
client_id = VGA_SWITCHEROO_DIS;
|
|
|
|
delay = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strncmp(usercmd, "IGD", 3) == 0)
|
|
|
|
client_id = VGA_SWITCHEROO_IGD;
|
|
|
|
|
|
|
|
if (strncmp(usercmd, "DIS", 3) == 0)
|
|
|
|
client_id = VGA_SWITCHEROO_DIS;
|
|
|
|
|
2011-01-07 13:12:27 +08:00
|
|
|
if (strncmp(usercmd, "MIGD", 4) == 0) {
|
2010-12-06 10:35:52 +08:00
|
|
|
just_mux = true;
|
|
|
|
client_id = VGA_SWITCHEROO_IGD;
|
|
|
|
}
|
2011-01-07 13:12:27 +08:00
|
|
|
if (strncmp(usercmd, "MDIS", 4) == 0) {
|
2010-12-06 10:35:52 +08:00
|
|
|
just_mux = true;
|
|
|
|
client_id = VGA_SWITCHEROO_DIS;
|
|
|
|
}
|
|
|
|
|
2015-08-28 19:30:32 +08:00
|
|
|
if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
|
2010-02-01 13:38:10 +08:00
|
|
|
goto out;
|
2012-04-26 18:55:59 +08:00
|
|
|
client = find_client_from_id(&vgasr_priv.clients, client_id);
|
|
|
|
if (!client)
|
|
|
|
goto out;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
|
|
|
vgasr_priv.delayed_switch_active = false;
|
2010-12-06 10:35:52 +08:00
|
|
|
|
|
|
|
if (just_mux) {
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
mutex_lock(&vgasr_priv.mux_hw_lock);
|
2010-12-06 10:35:52 +08:00
|
|
|
ret = vgasr_priv.handler->switchto(client_id);
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
mutex_unlock(&vgasr_priv.mux_hw_lock);
|
2010-12-06 10:35:52 +08:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
if (client->active)
|
2011-05-15 22:32:50 +08:00
|
|
|
goto out;
|
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
/* okay we want a switch - test if devices are willing to switch */
|
2012-04-26 18:55:59 +08:00
|
|
|
can_switch = check_can_switch();
|
2010-02-01 13:38:10 +08:00
|
|
|
|
|
|
|
if (can_switch == false && delay == false)
|
|
|
|
goto out;
|
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
if (can_switch) {
|
2010-12-07 12:24:25 +08:00
|
|
|
ret = vga_switchto_stage1(client);
|
|
|
|
if (ret)
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_err("switching failed stage 1 %d\n", ret);
|
2010-12-07 12:24:25 +08:00
|
|
|
|
|
|
|
ret = vga_switchto_stage2(client);
|
2010-02-01 13:38:10 +08:00
|
|
|
if (ret)
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_err("switching failed stage 2 %d\n", ret);
|
2010-12-07 12:24:25 +08:00
|
|
|
|
2010-02-01 13:38:10 +08:00
|
|
|
} else {
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_info("setting delayed switch to client %d\n", client->id);
|
2010-02-01 13:38:10 +08:00
|
|
|
vgasr_priv.delayed_switch_active = true;
|
|
|
|
vgasr_priv.delayed_client_id = client_id;
|
|
|
|
|
2010-12-07 12:24:25 +08:00
|
|
|
ret = vga_switchto_stage1(client);
|
|
|
|
if (ret)
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_err("delayed switching stage 1 failed %d\n", ret);
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct file_operations vga_switcheroo_debugfs_fops = {
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
.open = vga_switcheroo_debugfs_open,
|
|
|
|
.write = vga_switcheroo_debugfs_write,
|
|
|
|
.read = seq_read,
|
|
|
|
.llseek = seq_lseek,
|
|
|
|
.release = single_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
|
|
|
|
{
|
2019-06-13 19:44:55 +08:00
|
|
|
debugfs_remove_recursive(priv->debugfs_root);
|
2015-08-12 22:32:12 +08:00
|
|
|
priv->debugfs_root = NULL;
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 19:44:55 +08:00
|
|
|
static void vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
|
2010-02-01 13:38:10 +08:00
|
|
|
{
|
|
|
|
/* already initialised */
|
|
|
|
if (priv->debugfs_root)
|
2019-06-13 19:44:55 +08:00
|
|
|
return;
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2019-06-13 19:44:55 +08:00
|
|
|
priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2019-06-13 19:44:55 +08:00
|
|
|
debugfs_create_file("switch", 0644, priv->debugfs_root, NULL,
|
|
|
|
&vga_switcheroo_debugfs_fops);
|
2010-02-01 13:38:10 +08:00
|
|
|
}
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_process_delayed_switch() - helper for delayed switching
|
|
|
|
*
|
2024-08-12 16:28:22 +08:00
|
|
|
* Process a delayed switch if one is pending.
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
|
|
|
|
* has unregistered in the meantime or if there are other clients blocking the
|
|
|
|
* switch. If the actual switch fails, an error is reported and 0 is returned.
|
|
|
|
*/
|
2010-02-01 13:38:10 +08:00
|
|
|
int vga_switcheroo_process_delayed_switch(void)
|
|
|
|
{
|
2012-04-26 18:55:59 +08:00
|
|
|
struct vga_switcheroo_client *client;
|
2010-02-01 13:38:10 +08:00
|
|
|
int ret;
|
|
|
|
int err = -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&vgasr_mutex);
|
|
|
|
if (!vgasr_priv.delayed_switch_active)
|
|
|
|
goto err;
|
|
|
|
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_info("processing delayed switch to %d\n",
|
|
|
|
vgasr_priv.delayed_client_id);
|
2010-02-01 13:38:10 +08:00
|
|
|
|
2012-04-26 18:55:59 +08:00
|
|
|
client = find_client_from_id(&vgasr_priv.clients,
|
|
|
|
vgasr_priv.delayed_client_id);
|
|
|
|
if (!client || !check_can_switch())
|
2010-02-01 13:38:10 +08:00
|
|
|
goto err;
|
|
|
|
|
2010-12-07 12:24:25 +08:00
|
|
|
ret = vga_switchto_stage2(client);
|
2010-02-01 13:38:10 +08:00
|
|
|
if (ret)
|
2015-08-12 22:32:10 +08:00
|
|
|
pr_err("delayed switching failed stage 2 %d\n", ret);
|
2010-02-01 13:38:10 +08:00
|
|
|
|
|
|
|
vgasr_priv.delayed_switch_active = false;
|
|
|
|
err = 0;
|
|
|
|
err:
|
|
|
|
mutex_unlock(&vgasr_mutex);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
|
2012-09-10 10:28:36 +08:00
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* DOC: Driver power control
|
|
|
|
*
|
|
|
|
* In this mode of use, the discrete GPU automatically powers up and down at
|
|
|
|
* the discretion of the driver's runtime pm. On muxed machines, the user may
|
|
|
|
* still influence the muxer state by way of the debugfs interface, however
|
|
|
|
* the ON and OFF commands become a no-op for the discrete GPU.
|
|
|
|
*
|
|
|
|
* This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
|
|
|
|
* Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
|
|
|
|
* command line disables it.
|
|
|
|
*
|
|
|
|
* After the GPU has been suspended, the handler needs to be called to cut
|
|
|
|
* power to the GPU. Likewise it needs to reinstate power before the GPU
|
|
|
|
* can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
|
|
|
|
* which augments the GPU's suspend/resume functions by the requisite
|
|
|
|
* calls to the handler.
|
|
|
|
*
|
|
|
|
* When the audio device resumes, the GPU needs to be woken. This is achieved
|
vga_switcheroo: Use device link for HDA controller
Back in 2013, runtime PM for GPUs with integrated HDA controller was
introduced with commits 0d69704ae348 ("gpu/vga_switcheroo: add driver
control power feature. (v3)") and 246efa4a072f ("snd/hda: add runtime
suspend/resume on optimus support (v4)").
Briefly, the idea was that the HDA controller is forced on and off in
unison with the GPU.
The original code is mostly still in place even though it was never a
100% perfect solution: E.g. on access to the HDA controller, the GPU
is powered up via vga_switcheroo_runtime_resume_hdmi_audio() but there
are no provisions to keep it resumed until access to the HDA controller
has ceased: The GPU autosuspends after 5 seconds, rendering the HDA
controller inaccessible.
Additionally, a kludge is required when hda_intel.c probes: It has to
check whether the GPU is powered down (check_hdmi_disabled()) and defer
probing if so.
However in the meantime (in v4.10) the driver core has gained a feature
called device links which promises to solve such issues in a clean way:
It allows us to declare a dependency from the HDA controller (consumer)
to the GPU (supplier). The PM core then automagically ensures that the
GPU is runtime resumed as long as the HDA controller's ->probe hook is
executed and whenever the HDA controller is accessed.
By default, the HDA controller has a dependency on its parent, a PCIe
Root Port. Adding a device link creates another dependency on its
sibling:
PCIe Root Port
^ ^
| |
| |
HDA ===> GPU
The device link is not only used for runtime PM, it also guarantees that
on system sleep, the HDA controller suspends before the GPU and resumes
after the GPU, and on system shutdown the HDA controller's ->shutdown
hook is executed before the one of the GPU. It is a complete solution.
Using this functionality is as simple as calling device_link_add(),
which results in a dmesg entry like this:
pci 0000:01:00.1: Linked as a consumer to 0000:01:00.0
The code for the GPU-governed audio power management can thus be removed
(except where it's still needed for legacy manual power control).
The device link is added in a PCI quirk rather than in hda_intel.c.
It is therefore legal for the GPU to runtime suspend to D3cold even if
the HDA controller is not bound to a driver or if CONFIG_SND_HDA_INTEL
is not enabled, for accesses to the HDA controller will cause the GPU to
wake up regardless if they're occurring outside of hda_intel.c (think
config space readout via sysfs).
Contrary to the previous implementation, the HDA controller's power
state is now self-governed, rather than GPU-governed, whereas the GPU's
power state is no longer fully self-governed. (The HDA controller needs
to runtime suspend before the GPU can.)
It is thus crucial that runtime PM is always activated on the HDA
controller even if CONFIG_SND_HDA_POWER_SAVE_DEFAULT is set to 0 (which
is the default), lest the GPU stays awake. This is achieved by setting
the auto_runtime_pm flag on every codec and the AZX_DCAPS_PM_RUNTIME
flag on the HDA controller.
A side effect is that power consumption might be reduced if the GPU is
in use but the HDA controller is not, because the HDA controller is now
allowed to go to D3hot. Before, it was forced to stay in D0 as long as
the GPU was in use. (There is no reduction in power consumption on my
Nvidia GK107, but there might be on other chips.)
The code paths for legacy manual power control are adjusted such that
runtime PM is disabled during power off, thereby preventing the PM core
from resuming the HDA controller.
Note that the device link is not only added on vga_switcheroo capable
systems, but for *any* GPU with integrated HDA controller. The idea is
that the HDA controller streams audio via connectors located on the GPU,
so the GPU needs to be on for the HDA controller to do anything useful.
This commit implicitly fixes an unbalanced runtime PM ref upon unbind of
hda_intel.c: On ->probe, a runtime PM ref was previously released under
the condition "azx_has_pm_runtime(chip) || hda->use_vga_switcheroo", but
on ->remove a runtime PM ref was only acquired under the first of those
conditions. Thus, binding and unbinding the driver twice on a
vga_switcheroo capable system caused the runtime PM refcount to drop
below zero. The issue is resolved because the AZX_DCAPS_PM_RUNTIME flag
is now always set if use_vga_switcheroo is true.
For more information on device links please refer to:
https://www.kernel.org/doc/html/latest/driver-api/device_link.html
Documentation/driver-api/device_link.rst
Cc: Dave Airlie <airlied@redhat.com>
Cc: Ben Skeggs <bskeggs@redhat.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Reviewed-by: Takashi Iwai <tiwai@suse.de>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
Tested-by: Kai Heng Feng <kai.heng.feng@canonical.com> # AMD PowerXpress
Tested-by: Mike Lothian <mike@fireburn.co.uk> # AMD PowerXpress
Tested-by: Denis Lisov <dennis.lissov@gmail.com> # Nvidia Optimus
Tested-by: Peter Wu <peter@lekensteyn.nl> # Nvidia Optimus
Tested-by: Lukas Wunner <lukas@wunner.de> # MacBook Pro
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Link: https://patchwork.freedesktop.org/patch/msgid/51bd38360ff502a8c42b1ebf4405ee1d3f27118d.1520068884.git.lukas@wunner.de
2018-03-03 17:53:24 +08:00
|
|
|
* by a PCI quirk which calls device_link_add() to declare a dependency on the
|
|
|
|
* GPU. That way, the GPU is kept awake whenever and as long as the audio
|
|
|
|
* device is in use.
|
2015-08-23 21:18:55 +08:00
|
|
|
*
|
|
|
|
* On muxed machines, if the mux is initially switched to the discrete GPU,
|
|
|
|
* the user ends up with a black screen when the GPU powers down after boot.
|
|
|
|
* As a workaround, the mux is forced to the integrated GPU on runtime suspend,
|
|
|
|
* cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
|
|
|
|
*/
|
|
|
|
|
2015-08-12 22:32:11 +08:00
|
|
|
static void vga_switcheroo_power_switch(struct pci_dev *pdev,
|
|
|
|
enum vga_switcheroo_state state)
|
2012-09-10 10:28:36 +08:00
|
|
|
{
|
|
|
|
struct vga_switcheroo_client *client;
|
|
|
|
|
|
|
|
if (!vgasr_priv.handler->power_state)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client = find_client_from_pci(&vgasr_priv.clients, pdev);
|
|
|
|
if (!client)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!client->driver_power_control)
|
|
|
|
return;
|
|
|
|
|
|
|
|
vgasr_priv.handler->power_state(client->id, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* switcheroo power domain */
|
|
|
|
static int vga_switcheroo_runtime_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = dev->bus->pm->runtime_suspend(dev);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2015-08-24 05:23:02 +08:00
|
|
|
mutex_lock(&vgasr_mutex);
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
if (vgasr_priv.handler->switchto) {
|
|
|
|
mutex_lock(&vgasr_priv.mux_hw_lock);
|
2014-05-20 02:04:43 +08:00
|
|
|
vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
|
vga_switcheroo: Add support for switching only the DDC
Originally by Seth Forshee <seth.forshee@canonical.com>, 2012-10-04:
During graphics driver initialization it's useful to be able to mux
only the DDC to the inactive client in order to read the EDID. Add
a switch_ddc callback to allow capable handlers to provide this
functionality, and add vga_switcheroo_switch_ddc() to allow DRM
to mux only the DDC.
Modified by Dave Airlie <airlied@gmail.com>, 2012-12-22:
I can't figure out why I didn't like this, but I rewrote this [...]
to lock/unlock the ddc lines [...]. I think I'd prefer something
like that otherwise the interface got really ugly.
Modified by Lukas Wunner <lukas@wunner.de>, 2015-04 - 2015-10:
Change semantics of ->switch_ddc handler callback to return previous
DDC owner. Original version tried to determine previous DDC owner
with find_active_client() but this fails if the inactive client
registers before the active client.
Don't lock vgasr_mutex in _lock_ddc() / _unlock_ddc(), it can cause
deadlocks because (a) during switch (with vgasr_mutex already held),
GPU is woken and probes its outputs, tries to re-acquire vgasr_mutex
to lock DDC lines; (b) Likewise during switch, GPU is suspended and
calls cancel_delayed_work_sync() to stop output polling, if poll
task is running at this moment we may wait forever for it to finish.
Instead, lock mux_hw_lock when unregistering the handler because
the only reason why we'd want to lock vgasr_mutex in _lock_ddc() /
_unlock_ddc() is to block the handler from disappearing while DDC
lines are switched.
Also acquire mux_hw_lock in stage2 to avoid race condition where
reading the EDID and switching happens simultaneously. Likewise on
MIGD / MDIS commands and on runtime suspend.
v2.1: Overhaul locking, squash commits (Daniel Vetter)
v2.2: Readability improvements (Thierry Reding)
v2.3: Overhaul locking once more
v2.4: Retain semantics of ->switchto handler callback to switch all
pins, including DDC (Daniel Vetter)
v5: Rename ddc_lock to mux_hw_lock: Since we acquire this both
when calling ->switch_ddc and ->switchto, it protects not just
access to the DDC lines but to the mux in general. This is in
line with the DRM convention to use low-level locks to avoid
concurrent hw access (e.g. i2c, dp_aux) which are often called
hw_lock (Daniel Vetter)
Bugzilla: https://bugzilla.kernel.org/show_bug.cgi?id=88861
Bugzilla: https://bugs.freedesktop.org/show_bug.cgi?id=61115
Tested-by: Lukas Wunner <lukas@wunner.de>
[MBP 9,1 2012 intel IVB + nvidia GK107 pre-retina 15"]
Cc: Seth Forshee <seth.forshee@canonical.com>
Cc: Dave Airlie <airlied@gmail.com>
Signed-off-by: Lukas Wunner <lukas@wunner.de>
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Link: http://patchwork.freedesktop.org/patch/msgid/e81ae9722b84c5ed591805fee3ea6dbf5dc6c4b3.1452525860.git.lukas@wunner.de
2016-01-12 03:09:20 +08:00
|
|
|
mutex_unlock(&vgasr_priv.mux_hw_lock);
|
|
|
|
}
|
2018-03-03 17:53:24 +08:00
|
|
|
pci_bus_set_current_state(pdev->bus, PCI_D3cold);
|
2012-09-10 10:28:36 +08:00
|
|
|
vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
|
2015-08-24 05:23:02 +08:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
2012-09-10 10:28:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int vga_switcheroo_runtime_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev = to_pci_dev(dev);
|
|
|
|
|
2015-08-24 05:23:02 +08:00
|
|
|
mutex_lock(&vgasr_mutex);
|
2012-09-10 10:28:36 +08:00
|
|
|
vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
|
2015-08-24 05:23:02 +08:00
|
|
|
mutex_unlock(&vgasr_mutex);
|
2020-11-25 17:07:33 +08:00
|
|
|
pci_resume_bus(pdev->bus);
|
2020-09-21 21:11:10 +08:00
|
|
|
return dev->bus->pm->runtime_resume(dev);
|
2012-09-10 10:28:36 +08:00
|
|
|
}
|
|
|
|
|
2015-08-23 21:18:55 +08:00
|
|
|
/**
|
|
|
|
* vga_switcheroo_init_domain_pm_ops() - helper for driver power control
|
|
|
|
* @dev: vga client device
|
|
|
|
* @domain: power domain
|
|
|
|
*
|
|
|
|
* Helper for GPUs whose power state is controlled by the driver's runtime pm.
|
|
|
|
* After the GPU has been suspended, the handler needs to be called to cut
|
|
|
|
* power to the GPU. Likewise it needs to reinstate power before the GPU
|
|
|
|
* can resume. To this end, this helper augments the suspend/resume functions
|
|
|
|
* by the requisite calls to the handler. It needs only be called on platforms
|
|
|
|
* where the power switch is separate to the device being powered down.
|
|
|
|
*/
|
2015-08-12 22:32:11 +08:00
|
|
|
int vga_switcheroo_init_domain_pm_ops(struct device *dev,
|
|
|
|
struct dev_pm_domain *domain)
|
2012-09-10 10:28:36 +08:00
|
|
|
{
|
|
|
|
/* copy over all the bus versions */
|
|
|
|
if (dev->bus && dev->bus->pm) {
|
|
|
|
domain->ops = *dev->bus->pm;
|
|
|
|
domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
|
|
|
|
domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
|
|
|
|
|
2016-01-07 23:46:13 +08:00
|
|
|
dev_pm_domain_set(dev, domain);
|
2012-09-10 10:28:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-07 23:46:13 +08:00
|
|
|
dev_pm_domain_set(dev, NULL);
|
2012-09-10 10:28:36 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
|
|
|
|
|
2014-09-13 05:51:29 +08:00
|
|
|
void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
|
|
|
|
{
|
2016-01-07 23:46:13 +08:00
|
|
|
dev_pm_domain_set(dev, NULL);
|
2014-09-13 05:51:29 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
|