aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/gpu/vga
diff options
context:
space:
mode:
authorLukas Wunner <lukas@wunner.de>2015-08-23 09:18:55 -0400
committerDaniel Vetter <daniel.vetter@ffwll.ch>2015-09-22 04:37:26 -0400
commita645654b817feba05e5156345325d19fc85ebc9f (patch)
tree6c2dce73efb5fe34039f7cf5500a0615fd28aa68 /drivers/gpu/vga
parent28cc504e8d52248962f5b485bdc65f539e3fe21d (diff)
vga_switcheroo: Document _ALL_ the things!
This adds an "Overview" DOC section plus two DOC sections for the modes of use ("Manual switching and manual power control" and "Driver power control"). Also included is kernel-doc for all public functions, structs and enums. Signed-off-by: Lukas Wunner <lukas@wunner.de> Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Diffstat (limited to 'drivers/gpu/vga')
-rw-r--r--drivers/gpu/vga/vga_switcheroo.c285
1 files changed, 271 insertions, 14 deletions
diff --git a/drivers/gpu/vga/vga_switcheroo.c b/drivers/gpu/vga/vga_switcheroo.c
index 21060668fd25..b19a72f7ac7c 100644
--- a/drivers/gpu/vga/vga_switcheroo.c
+++ b/drivers/gpu/vga/vga_switcheroo.c
@@ -1,20 +1,31 @@
1/* 1/*
2 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
3 *
2 * Copyright (c) 2010 Red Hat Inc. 4 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com> 5 * Author : Dave Airlie <airlied@redhat.com>
4 * 6 *
7 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
5 * 8 *
6 * Licensed under GPLv2 9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
7 * 15 *
8 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs 16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
9 * 19 *
10 * Switcher interface - methods require for ATPX and DCM 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11 * - switchto - this throws the output MUX switch 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 * - discrete_set_power - sets the power state for the discrete card 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS
27 * IN THE SOFTWARE.
13 * 28 *
14 * GPU driver interface
15 * - set_gpu_state - this should do the equiv of s/r for the card
16 * - this should *not* set the discrete power state
17 * - switch_check - check if the device is in a position to switch now
18 */ 29 */
19 30
20#define pr_fmt(fmt) "vga_switcheroo: " fmt 31#define pr_fmt(fmt) "vga_switcheroo: " fmt
@@ -33,6 +44,61 @@
33 44
34#include <linux/vgaarb.h> 45#include <linux/vgaarb.h>
35 46
47/**
48 * DOC: Overview
49 *
50 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
51 * These come in two flavors:
52 *
53 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
54 * * muxless: Dual GPUs but only one of them is connected to outputs.
55 * The other one is merely used to offload rendering, its results
56 * are copied over PCIe into the framebuffer. On Linux this is
57 * supported with DRI PRIME.
58 *
59 * Hybrid graphics started to appear in the late Naughties and were initially
60 * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
61 * A notable exception is the MacBook Pro which continues to use a mux.
62 * Muxes come with varying capabilities: Some switch only the panel, others
63 * can also switch external displays. Some switch all display pins at once
64 * while others can switch just the DDC lines. (To allow EDID probing
65 * for the inactive GPU.) Also, muxes are often used to cut power to the
66 * discrete GPU while it is not used.
67 *
68 * DRM drivers register GPUs with vga_switcheroo, these are heretoforth called
69 * clients. The mux is called the handler. Muxless machines also register a
70 * handler to control the power state of the discrete GPU, its ->switchto
71 * callback is a no-op for obvious reasons. The discrete GPU is often equipped
72 * with an HDA controller for the HDMI/DP audio signal, this will also
73 * register as a client so that vga_switcheroo can take care of the correct
74 * suspend/resume order when changing the discrete GPU's power state. In total
75 * there can thus be up to three clients: Two vga clients (GPUs) and one audio
76 * client (on the discrete GPU). The code is mostly prepared to support
77 * machines with more than two GPUs should they become available.
78 * The GPU to which the outputs are currently switched is called the
79 * active client in vga_switcheroo parlance. The GPU not in use is the
80 * inactive client.
81 */
82
83/**
84 * struct vga_switcheroo_client - registered client
85 * @pdev: client pci device
86 * @fb_info: framebuffer to which console is remapped on switching
87 * @pwr_state: current power state
88 * @ops: client callbacks
89 * @id: client identifier, see enum vga_switcheroo_client_id.
90 * Determining the id requires the handler, so GPUs are initially
91 * assigned -1 and later given their true id in vga_switcheroo_enable()
92 * @active: whether the outputs are currently switched to this client
93 * @driver_power_control: whether power state is controlled by the driver's
94 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
95 * interface is a no-op so as not to interfere with runtime pm
96 * @list: client list
97 *
98 * Registered client. A client can be either a GPU or an audio device on a GPU.
99 * For audio clients, the @fb_info, @active and @driver_power_control members
100 * are bogus.
101 */
36struct vga_switcheroo_client { 102struct vga_switcheroo_client {
37 struct pci_dev *pdev; 103 struct pci_dev *pdev;
38 struct fb_info *fb_info; 104 struct fb_info *fb_info;
@@ -44,10 +110,28 @@ struct vga_switcheroo_client {
44 struct list_head list; 110 struct list_head list;
45}; 111};
46 112
113/*
114 * protects access to struct vgasr_priv
115 */
47static DEFINE_MUTEX(vgasr_mutex); 116static DEFINE_MUTEX(vgasr_mutex);
48 117
118/**
119 * struct vgasr_priv - vga_switcheroo private data
120 * @active: whether vga_switcheroo is enabled.
121 * Prerequisite is the registration of two GPUs and a handler
122 * @delayed_switch_active: whether a delayed switch is pending
123 * @delayed_client_id: client to which a delayed switch is pending
124 * @debugfs_root: directory for vga_switcheroo debugfs interface
125 * @switch_file: file for vga_switcheroo debugfs interface
126 * @registered_clients: number of registered GPUs
127 * (counting only vga clients, not audio clients)
128 * @clients: list of registered clients
129 * @handler: registered handler
130 *
131 * vga_switcheroo private data. Currently only one vga_switcheroo instance
132 * per system is supported.
133 */
49struct vgasr_priv { 134struct vgasr_priv {
50
51 bool active; 135 bool active;
52 bool delayed_switch_active; 136 bool delayed_switch_active;
53 enum vga_switcheroo_client_id delayed_client_id; 137 enum vga_switcheroo_client_id delayed_client_id;
@@ -103,6 +187,15 @@ static void vga_switcheroo_enable(void)
103 vgasr_priv.active = true; 187 vgasr_priv.active = true;
104} 188}
105 189
190/**
191 * vga_switcheroo_register_handler() - register handler
192 * @handler: handler callbacks
193 *
194 * Register handler. Enable vga_switcheroo if two vga clients have already
195 * registered.
196 *
197 * Return: 0 on success, -EINVAL if a handler was already registered.
198 */
106int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler) 199int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
107{ 200{
108 mutex_lock(&vgasr_mutex); 201 mutex_lock(&vgasr_mutex);
@@ -121,6 +214,11 @@ int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
121} 214}
122EXPORT_SYMBOL(vga_switcheroo_register_handler); 215EXPORT_SYMBOL(vga_switcheroo_register_handler);
123 216
217/**
218 * vga_switcheroo_unregister_handler() - unregister handler
219 *
220 * Unregister handler. Disable vga_switcheroo.
221 */
124void vga_switcheroo_unregister_handler(void) 222void vga_switcheroo_unregister_handler(void)
125{ 223{
126 mutex_lock(&vgasr_mutex); 224 mutex_lock(&vgasr_mutex);
@@ -164,6 +262,19 @@ static int register_client(struct pci_dev *pdev,
164 return 0; 262 return 0;
165} 263}
166 264
265/**
266 * vga_switcheroo_register_client - register vga client
267 * @pdev: client pci device
268 * @ops: client callbacks
269 * @driver_power_control: whether power state is controlled by the driver's
270 * runtime pm
271 *
272 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
273 * handler have already registered. The power state of the client is assumed
274 * to be ON.
275 *
276 * Return: 0 on success, -ENOMEM on memory allocation error.
277 */
167int vga_switcheroo_register_client(struct pci_dev *pdev, 278int vga_switcheroo_register_client(struct pci_dev *pdev,
168 const struct vga_switcheroo_client_ops *ops, 279 const struct vga_switcheroo_client_ops *ops,
169 bool driver_power_control) 280 bool driver_power_control)
@@ -174,6 +285,18 @@ int vga_switcheroo_register_client(struct pci_dev *pdev,
174} 285}
175EXPORT_SYMBOL(vga_switcheroo_register_client); 286EXPORT_SYMBOL(vga_switcheroo_register_client);
176 287
288/**
289 * vga_switcheroo_register_audio_client - register audio client
290 * @pdev: client pci device
291 * @ops: client callbacks
292 * @id: client identifier, see enum vga_switcheroo_client_id
293 * @active: whether the audio device is fully initialized
294 *
295 * Register audio client (audio device on a GPU). The power state of the
296 * client is assumed to be ON.
297 *
298 * Return: 0 on success, -ENOMEM on memory allocation error.
299 */
177int vga_switcheroo_register_audio_client(struct pci_dev *pdev, 300int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
178 const struct vga_switcheroo_client_ops *ops, 301 const struct vga_switcheroo_client_ops *ops,
179 int id, bool active) 302 int id, bool active)
@@ -215,6 +338,15 @@ find_active_client(struct list_head *head)
215 return NULL; 338 return NULL;
216} 339}
217 340
341/**
342 * vga_switcheroo_get_client_state() - obtain power state of a given client
343 * @pdev: client pci device
344 *
345 * Obtain power state of a given client as seen from vga_switcheroo.
346 * The function is only called from hda_intel.c.
347 *
348 * Return: Power state.
349 */
218int vga_switcheroo_get_client_state(struct pci_dev *pdev) 350int vga_switcheroo_get_client_state(struct pci_dev *pdev)
219{ 351{
220 struct vga_switcheroo_client *client; 352 struct vga_switcheroo_client *client;
@@ -228,6 +360,12 @@ int vga_switcheroo_get_client_state(struct pci_dev *pdev)
228} 360}
229EXPORT_SYMBOL(vga_switcheroo_get_client_state); 361EXPORT_SYMBOL(vga_switcheroo_get_client_state);
230 362
363/**
364 * vga_switcheroo_unregister_client() - unregister client
365 * @pdev: client pci device
366 *
367 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
368 */
231void vga_switcheroo_unregister_client(struct pci_dev *pdev) 369void vga_switcheroo_unregister_client(struct pci_dev *pdev)
232{ 370{
233 struct vga_switcheroo_client *client; 371 struct vga_switcheroo_client *client;
@@ -249,6 +387,14 @@ void vga_switcheroo_unregister_client(struct pci_dev *pdev)
249} 387}
250EXPORT_SYMBOL(vga_switcheroo_unregister_client); 388EXPORT_SYMBOL(vga_switcheroo_unregister_client);
251 389
390/**
391 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
392 * @pdev: client pci device
393 * @info: framebuffer
394 *
395 * Set framebuffer of a given client. The console will be remapped to this
396 * on switching.
397 */
252void vga_switcheroo_client_fb_set(struct pci_dev *pdev, 398void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
253 struct fb_info *info) 399 struct fb_info *info)
254{ 400{
@@ -262,6 +408,42 @@ void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
262} 408}
263EXPORT_SYMBOL(vga_switcheroo_client_fb_set); 409EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
264 410
411/**
412 * DOC: Manual switching and manual power control
413 *
414 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
415 * can be read to retrieve the current vga_switcheroo state and commands
416 * can be written to it to change the state. The file appears as soon as
417 * two GPU drivers and one handler have registered with vga_switcheroo.
418 * The following commands are understood:
419 *
420 * * OFF: Power off the device not in use.
421 * * ON: Power on the device not in use.
422 * * IGD: Switch to the integrated graphics device.
423 * Power on the integrated GPU if necessary, power off the discrete GPU.
424 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
425 * have opened device files of the GPUs or the audio client. If the
426 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
427 * and /dev/snd/controlC1 to identify processes blocking the switch.
428 * * DIS: Switch to the discrete graphics device.
429 * * DIGD: Delayed switch to the integrated graphics device.
430 * This will perform the switch once the last user space process has
431 * closed the device files of the GPUs and the audio client.
432 * * DDIS: Delayed switch to the discrete graphics device.
433 * * MIGD: Mux-only switch to the integrated graphics device.
434 * Does not remap console or change the power state of either gpu.
435 * If the integrated GPU is currently off, the screen will turn black.
436 * If it is on, the screen will show whatever happens to be in VRAM.
437 * Either way, the user has to blindly enter the command to switch back.
438 * * MDIS: Mux-only switch to the discrete graphics device.
439 *
440 * For GPUs whose power state is controlled by the driver's runtime pm,
441 * the ON and OFF commands are a no-op (see next section).
442 *
443 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
444 * should not be used.
445 */
446
265static int vga_switcheroo_show(struct seq_file *m, void *v) 447static int vga_switcheroo_show(struct seq_file *m, void *v)
266{ 448{
267 struct vga_switcheroo_client *client; 449 struct vga_switcheroo_client *client;
@@ -559,6 +741,16 @@ fail:
559 return -1; 741 return -1;
560} 742}
561 743
744/**
745 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
746 *
747 * Process a delayed switch if one is pending. DRM drivers should call this
748 * from their ->lastclose callback.
749 *
750 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
751 * has unregistered in the meantime or if there are other clients blocking the
752 * switch. If the actual switch fails, an error is reported and 0 is returned.
753 */
562int vga_switcheroo_process_delayed_switch(void) 754int vga_switcheroo_process_delayed_switch(void)
563{ 755{
564 struct vga_switcheroo_client *client; 756 struct vga_switcheroo_client *client;
@@ -589,6 +781,39 @@ err:
589} 781}
590EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch); 782EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
591 783
784/**
785 * DOC: Driver power control
786 *
787 * In this mode of use, the discrete GPU automatically powers up and down at
788 * the discretion of the driver's runtime pm. On muxed machines, the user may
789 * still influence the muxer state by way of the debugfs interface, however
790 * the ON and OFF commands become a no-op for the discrete GPU.
791 *
792 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
793 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
794 * command line disables it.
795 *
796 * When the driver decides to power up or down, it notifies vga_switcheroo
797 * thereof so that it can (a) power the audio device on the GPU up or down,
798 * and (b) update its internal power state representation for the device.
799 * This is achieved by vga_switcheroo_set_dynamic_switch().
800 *
801 * After the GPU has been suspended, the handler needs to be called to cut
802 * power to the GPU. Likewise it needs to reinstate power before the GPU
803 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
804 * which augments the GPU's suspend/resume functions by the requisite
805 * calls to the handler.
806 *
807 * When the audio device resumes, the GPU needs to be woken. This is achieved
808 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
809 * audio device's resume function.
810 *
811 * On muxed machines, if the mux is initially switched to the discrete GPU,
812 * the user ends up with a black screen when the GPU powers down after boot.
813 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
814 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
815 */
816
592static void vga_switcheroo_power_switch(struct pci_dev *pdev, 817static void vga_switcheroo_power_switch(struct pci_dev *pdev,
593 enum vga_switcheroo_state state) 818 enum vga_switcheroo_state state)
594{ 819{
@@ -607,8 +832,17 @@ static void vga_switcheroo_power_switch(struct pci_dev *pdev,
607 vgasr_priv.handler->power_state(client->id, state); 832 vgasr_priv.handler->power_state(client->id, state);
608} 833}
609 834
610/* force a PCI device to a certain state - mainly to turn off audio clients */ 835/**
611 836 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
837 * @pdev: client pci device
838 * @dynamic: new power state
839 *
840 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
841 * When the driver decides to power up or down, it notifies vga_switcheroo
842 * thereof using this helper so that it can (a) power the audio device on
843 * the GPU up or down, and (b) update its internal power state representation
844 * for the device.
845 */
612void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, 846void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
613 enum vga_switcheroo_state dynamic) 847 enum vga_switcheroo_state dynamic)
614{ 848{
@@ -654,8 +888,18 @@ static int vga_switcheroo_runtime_resume(struct device *dev)
654 return 0; 888 return 0;
655} 889}
656 890
657/* this version is for the case where the power switch is separate 891/**
658 to the device being powered down. */ 892 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
893 * @dev: vga client device
894 * @domain: power domain
895 *
896 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
897 * After the GPU has been suspended, the handler needs to be called to cut
898 * power to the GPU. Likewise it needs to reinstate power before the GPU
899 * can resume. To this end, this helper augments the suspend/resume functions
900 * by the requisite calls to the handler. It needs only be called on platforms
901 * where the power switch is separate to the device being powered down.
902 */
659int vga_switcheroo_init_domain_pm_ops(struct device *dev, 903int vga_switcheroo_init_domain_pm_ops(struct device *dev,
660 struct dev_pm_domain *domain) 904 struct dev_pm_domain *domain)
661{ 905{
@@ -709,6 +953,19 @@ static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
709 return ret; 953 return ret;
710} 954}
711 955
956/**
957 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
958 * power control
959 * @dev: audio client device
960 * @domain: power domain
961 *
962 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
963 * When the audio device resumes, the GPU needs to be woken. This helper
964 * augments the audio device's resume function to do that.
965 *
966 * Return: 0 on success, -EINVAL if no power management operations are
967 * defined for this device.
968 */
712int 969int
713vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, 970vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
714 struct dev_pm_domain *domain) 971 struct dev_pm_domain *domain)