blob: 5f962bfcb43c731c54ad8a6d4c54b564a9acd6f8 [file] [log] [blame]
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001/*
Lukas Wunnera6456542015-08-23 15:18:55 +02002 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
3 *
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10004 * Copyright (c) 2010 Red Hat Inc.
5 * Author : Dave Airlie <airlied@redhat.com>
6 *
Lukas Wunnera6456542015-08-23 15:18:55 +02007 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10008 *
Lukas Wunnera6456542015-08-23 15:18:55 +02009 * 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:
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100015 *
Lukas Wunnera6456542015-08-23 15:18:55 +020016 * 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.
Thierry Reding71309272015-08-12 16:32:09 +020019 *
Lukas Wunnera6456542015-08-23 15:18:55 +020020 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
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.
Thierry Reding71309272015-08-12 16:32:09 +020028 *
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100029 */
30
Thierry Reding9b0be1e2015-08-12 16:32:10 +020031#define pr_fmt(fmt) "vga_switcheroo: " fmt
32
Lukas Wunnerb00e5332016-05-31 11:13:27 +020033#include <linux/apple-gmux.h>
Lukas Wunner41278382015-09-05 13:40:23 +020034#include <linux/console.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100035#include <linux/debugfs.h>
36#include <linux/fb.h>
Lukas Wunner41278382015-09-05 13:40:23 +020037#include <linux/fs.h>
38#include <linux/module.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100039#include <linux/pci.h>
Tomeu Vizoso989561d2016-01-07 16:46:13 +010040#include <linux/pm_domain.h>
Dave Airlie0d697042012-09-10 12:28:36 +100041#include <linux/pm_runtime.h>
Lukas Wunner41278382015-09-05 13:40:23 +020042#include <linux/seq_file.h>
43#include <linux/uaccess.h>
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040044#include <linux/vgaarb.h>
Lukas Wunner41278382015-09-05 13:40:23 +020045#include <linux/vga_switcheroo.h>
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040046
Lukas Wunnera6456542015-08-23 15:18:55 +020047/**
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.
Lukas Wunnerfb001df2016-07-11 10:22:33 +020055 * 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.
Lukas Wunnera6456542015-08-23 15:18:55 +020058 *
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 *
Lukas Wunner3a848662016-01-02 18:47:17 +010068 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
Lukas Wunnera6456542015-08-23 15:18:55 +020069 * 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.
Lukas Wunnere4cb81d2016-01-11 20:09:20 +010078 *
Lukas Wunnera6456542015-08-23 15:18:55 +020079 * The GPU to which the outputs are currently switched is called the
80 * active client in vga_switcheroo parlance. The GPU not in use is the
Lukas Wunnere4cb81d2016-01-11 20:09:20 +010081 * inactive client. When the inactive client's DRM driver is loaded,
82 * it will be unable to probe the panel's EDID and hence depends on
83 * VBIOS to provide its display modes. If the VBIOS modes are bogus or
84 * if there is no VBIOS at all (which is common on the MacBook Pro),
85 * a client may alternatively request that the DDC lines are temporarily
86 * switched to it, provided that the handler supports this. Switching
87 * only the DDC lines and not the entire output avoids unnecessary
88 * flickering.
Lukas Wunnera6456542015-08-23 15:18:55 +020089 */
90
91/**
92 * struct vga_switcheroo_client - registered client
93 * @pdev: client pci device
94 * @fb_info: framebuffer to which console is remapped on switching
95 * @pwr_state: current power state
96 * @ops: client callbacks
Lukas Wunner21c5ba82015-08-28 13:30:32 +020097 * @id: client identifier. Determining the id requires the handler,
98 * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
99 * and later given their true id in vga_switcheroo_enable()
Lukas Wunnera6456542015-08-23 15:18:55 +0200100 * @active: whether the outputs are currently switched to this client
101 * @driver_power_control: whether power state is controlled by the driver's
102 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
103 * interface is a no-op so as not to interfere with runtime pm
104 * @list: client list
105 *
106 * Registered client. A client can be either a GPU or an audio device on a GPU.
107 * For audio clients, the @fb_info, @active and @driver_power_control members
108 * are bogus.
109 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000110struct vga_switcheroo_client {
111 struct pci_dev *pdev;
112 struct fb_info *fb_info;
Lukas Wunner203d0272015-08-28 11:56:26 +0200113 enum vga_switcheroo_state pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200114 const struct vga_switcheroo_client_ops *ops;
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200115 enum vga_switcheroo_client_id id;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000116 bool active;
Dave Airlie0d697042012-09-10 12:28:36 +1000117 bool driver_power_control;
Takashi Iwai79721e02012-04-26 12:55:59 +0200118 struct list_head list;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000119};
120
Lukas Wunnera6456542015-08-23 15:18:55 +0200121/*
122 * protects access to struct vgasr_priv
123 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000124static DEFINE_MUTEX(vgasr_mutex);
125
Lukas Wunnera6456542015-08-23 15:18:55 +0200126/**
127 * struct vgasr_priv - vga_switcheroo private data
128 * @active: whether vga_switcheroo is enabled.
129 * Prerequisite is the registration of two GPUs and a handler
130 * @delayed_switch_active: whether a delayed switch is pending
131 * @delayed_client_id: client to which a delayed switch is pending
132 * @debugfs_root: directory for vga_switcheroo debugfs interface
133 * @switch_file: file for vga_switcheroo debugfs interface
134 * @registered_clients: number of registered GPUs
135 * (counting only vga clients, not audio clients)
136 * @clients: list of registered clients
137 * @handler: registered handler
Lukas Wunner156d7d42016-01-11 20:09:20 +0100138 * @handler_flags: flags of registered handler
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100139 * @mux_hw_lock: protects mux state
140 * (in particular while DDC lines are temporarily switched)
141 * @old_ddc_owner: client to which DDC lines will be switched back on unlock
Lukas Wunnera6456542015-08-23 15:18:55 +0200142 *
143 * vga_switcheroo private data. Currently only one vga_switcheroo instance
144 * per system is supported.
145 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000146struct vgasr_priv {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000147 bool active;
148 bool delayed_switch_active;
149 enum vga_switcheroo_client_id delayed_client_id;
150
151 struct dentry *debugfs_root;
152 struct dentry *switch_file;
153
154 int registered_clients;
Takashi Iwai79721e02012-04-26 12:55:59 +0200155 struct list_head clients;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000156
Lukas Wunner5d170132015-10-18 13:05:40 +0200157 const struct vga_switcheroo_handler *handler;
Lukas Wunner156d7d42016-01-11 20:09:20 +0100158 enum vga_switcheroo_handler_flags_t handler_flags;
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100159 struct mutex mux_hw_lock;
160 int old_ddc_owner;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000161};
162
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200163#define ID_BIT_AUDIO 0x100
164#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200165#define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
166 !client_is_audio(c))
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200167#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
168
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000169static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
170static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
171
172/* only one switcheroo per system */
Takashi Iwai79721e02012-04-26 12:55:59 +0200173static struct vgasr_priv vgasr_priv = {
174 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100175 .mux_hw_lock = __MUTEX_INITIALIZER(vgasr_priv.mux_hw_lock),
Takashi Iwai79721e02012-04-26 12:55:59 +0200176};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000177
Seth Forshee36704c02012-08-17 11:17:03 -0500178static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000179{
Seth Forshee36704c02012-08-17 11:17:03 -0500180 /* we're ready if we get two clients + handler */
181 return !vgasr_priv.active &&
182 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000183}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000184
185static void vga_switcheroo_enable(void)
186{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000187 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200188 struct vga_switcheroo_client *client;
189
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000190 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -0500191 if (vgasr_priv.handler->init)
192 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000193
Takashi Iwai79721e02012-04-26 12:55:59 +0200194 list_for_each_entry(client, &vgasr_priv.clients, list) {
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200195 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200196 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200197 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000198 if (ret < 0)
199 return;
200
Takashi Iwai79721e02012-04-26 12:55:59 +0200201 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000202 }
203 vga_switcheroo_debugfs_init(&vgasr_priv);
204 vgasr_priv.active = true;
205}
206
Lukas Wunnera6456542015-08-23 15:18:55 +0200207/**
208 * vga_switcheroo_register_handler() - register handler
209 * @handler: handler callbacks
Lukas Wunner156d7d42016-01-11 20:09:20 +0100210 * @handler_flags: handler flags
Lukas Wunnera6456542015-08-23 15:18:55 +0200211 *
212 * Register handler. Enable vga_switcheroo if two vga clients have already
213 * registered.
214 *
215 * Return: 0 on success, -EINVAL if a handler was already registered.
216 */
Lukas Wunner156d7d42016-01-11 20:09:20 +0100217int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
218 enum vga_switcheroo_handler_flags_t handler_flags)
Seth Forshee36704c02012-08-17 11:17:03 -0500219{
220 mutex_lock(&vgasr_mutex);
221 if (vgasr_priv.handler) {
222 mutex_unlock(&vgasr_mutex);
223 return -EINVAL;
224 }
225
226 vgasr_priv.handler = handler;
Lukas Wunner156d7d42016-01-11 20:09:20 +0100227 vgasr_priv.handler_flags = handler_flags;
Seth Forshee36704c02012-08-17 11:17:03 -0500228 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200229 pr_info("enabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500230 vga_switcheroo_enable();
231 }
232 mutex_unlock(&vgasr_mutex);
233 return 0;
234}
235EXPORT_SYMBOL(vga_switcheroo_register_handler);
236
Lukas Wunnera6456542015-08-23 15:18:55 +0200237/**
238 * vga_switcheroo_unregister_handler() - unregister handler
239 *
240 * Unregister handler. Disable vga_switcheroo.
241 */
Seth Forshee36704c02012-08-17 11:17:03 -0500242void vga_switcheroo_unregister_handler(void)
243{
244 mutex_lock(&vgasr_mutex);
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100245 mutex_lock(&vgasr_priv.mux_hw_lock);
Lukas Wunner156d7d42016-01-11 20:09:20 +0100246 vgasr_priv.handler_flags = 0;
Seth Forshee36704c02012-08-17 11:17:03 -0500247 vgasr_priv.handler = NULL;
248 if (vgasr_priv.active) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200249 pr_info("disabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500250 vga_switcheroo_debugfs_fini(&vgasr_priv);
251 vgasr_priv.active = false;
252 }
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100253 mutex_unlock(&vgasr_priv.mux_hw_lock);
Seth Forshee36704c02012-08-17 11:17:03 -0500254 mutex_unlock(&vgasr_mutex);
255}
256EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
257
Lukas Wunner156d7d42016-01-11 20:09:20 +0100258/**
259 * vga_switcheroo_handler_flags() - obtain handler flags
260 *
261 * Helper for clients to obtain the handler flags bitmask.
262 *
263 * Return: Handler flags. A value of 0 means that no handler is registered
264 * or that the handler has no special capabilities.
265 */
266enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void)
267{
268 return vgasr_priv.handler_flags;
269}
270EXPORT_SYMBOL(vga_switcheroo_handler_flags);
271
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200272static int register_client(struct pci_dev *pdev,
273 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200274 enum vga_switcheroo_client_id id, bool active,
275 bool driver_power_control)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000276{
Takashi Iwai79721e02012-04-26 12:55:59 +0200277 struct vga_switcheroo_client *client;
278
279 client = kzalloc(sizeof(*client), GFP_KERNEL);
280 if (!client)
281 return -ENOMEM;
282
283 client->pwr_state = VGA_SWITCHEROO_ON;
284 client->pdev = pdev;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200285 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200286 client->id = id;
287 client->active = active;
Dave Airlie0d697042012-09-10 12:28:36 +1000288 client->driver_power_control = driver_power_control;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000289
290 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200291 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200292 if (client_is_vga(client))
293 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000294
Seth Forshee36704c02012-08-17 11:17:03 -0500295 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200296 pr_info("enabled\n");
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000297 vga_switcheroo_enable();
298 }
299 mutex_unlock(&vgasr_mutex);
300 return 0;
301}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200302
Lukas Wunnera6456542015-08-23 15:18:55 +0200303/**
304 * vga_switcheroo_register_client - register vga client
305 * @pdev: client pci device
306 * @ops: client callbacks
307 * @driver_power_control: whether power state is controlled by the driver's
308 * runtime pm
309 *
310 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
311 * handler have already registered. The power state of the client is assumed
Lukas Wunnerb00e5332016-05-31 11:13:27 +0200312 * to be ON. Beforehand, vga_switcheroo_client_probe_defer() shall be called
313 * to ensure that all prerequisites are met.
Lukas Wunnera6456542015-08-23 15:18:55 +0200314 *
315 * Return: 0 on success, -ENOMEM on memory allocation error.
316 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200317int vga_switcheroo_register_client(struct pci_dev *pdev,
Dave Airlie0d697042012-09-10 12:28:36 +1000318 const struct vga_switcheroo_client_ops *ops,
319 bool driver_power_control)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200320{
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200321 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200322 pdev == vga_default_device(),
323 driver_power_control);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200324}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000325EXPORT_SYMBOL(vga_switcheroo_register_client);
326
Lukas Wunnera6456542015-08-23 15:18:55 +0200327/**
328 * vga_switcheroo_register_audio_client - register audio client
329 * @pdev: client pci device
330 * @ops: client callbacks
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200331 * @id: client identifier
Lukas Wunnera6456542015-08-23 15:18:55 +0200332 *
333 * Register audio client (audio device on a GPU). The power state of the
Lukas Wunnera3459182016-05-31 11:13:27 +0200334 * client is assumed to be ON. Beforehand, vga_switcheroo_client_probe_defer()
335 * shall be called to ensure that all prerequisites are met.
Lukas Wunnera6456542015-08-23 15:18:55 +0200336 *
337 * Return: 0 on success, -ENOMEM on memory allocation error.
338 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200339int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
340 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200341 enum vga_switcheroo_client_id id)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200342{
Lukas Wunner21b45672015-08-27 16:43:43 +0200343 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200344}
345EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
346
Takashi Iwai79721e02012-04-26 12:55:59 +0200347static struct vga_switcheroo_client *
348find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
349{
350 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200351
Takashi Iwai79721e02012-04-26 12:55:59 +0200352 list_for_each_entry(client, head, list)
353 if (client->pdev == pdev)
354 return client;
355 return NULL;
356}
357
358static struct vga_switcheroo_client *
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200359find_client_from_id(struct list_head *head,
360 enum vga_switcheroo_client_id client_id)
Takashi Iwai79721e02012-04-26 12:55:59 +0200361{
362 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200363
Takashi Iwai79721e02012-04-26 12:55:59 +0200364 list_for_each_entry(client, head, list)
365 if (client->id == client_id)
366 return client;
367 return NULL;
368}
369
370static struct vga_switcheroo_client *
371find_active_client(struct list_head *head)
372{
373 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200374
Takashi Iwai79721e02012-04-26 12:55:59 +0200375 list_for_each_entry(client, head, list)
Lukas Wunner21b45672015-08-27 16:43:43 +0200376 if (client->active)
Takashi Iwai79721e02012-04-26 12:55:59 +0200377 return client;
378 return NULL;
379}
380
Lukas Wunnera6456542015-08-23 15:18:55 +0200381/**
Lukas Wunnerb00e5332016-05-31 11:13:27 +0200382 * vga_switcheroo_client_probe_defer() - whether to defer probing a given client
383 * @pdev: client pci device
384 *
385 * Determine whether any prerequisites are not fulfilled to probe a given
386 * client. Drivers shall invoke this early on in their ->probe callback
387 * and return %-EPROBE_DEFER if it evaluates to %true. Thou shalt not
388 * register the client ere thou hast called this.
389 *
390 * Return: %true if probing should be deferred, otherwise %false.
391 */
392bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev)
393{
Lukas Wunnera3459182016-05-31 11:13:27 +0200394 if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
395 /*
396 * apple-gmux is needed on pre-retina MacBook Pro
397 * to probe the panel if pdev is the inactive GPU.
398 */
399 if (apple_gmux_present() && pdev != vga_default_device() &&
400 !vgasr_priv.handler_flags)
401 return true;
402 }
Lukas Wunnerb00e5332016-05-31 11:13:27 +0200403
404 return false;
405}
406EXPORT_SYMBOL(vga_switcheroo_client_probe_defer);
407
408/**
Lukas Wunnera6456542015-08-23 15:18:55 +0200409 * vga_switcheroo_get_client_state() - obtain power state of a given client
410 * @pdev: client pci device
411 *
412 * Obtain power state of a given client as seen from vga_switcheroo.
413 * The function is only called from hda_intel.c.
414 *
415 * Return: Power state.
416 */
Lukas Wunner203d0272015-08-28 11:56:26 +0200417enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200418{
419 struct vga_switcheroo_client *client;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200420 enum vga_switcheroo_state ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200421
Lukas Wunner8f12a312015-08-23 23:23:02 +0200422 mutex_lock(&vgasr_mutex);
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200423 client = find_client_from_pci(&vgasr_priv.clients, pdev);
424 if (!client)
Lukas Wunner8f12a312015-08-23 23:23:02 +0200425 ret = VGA_SWITCHEROO_NOT_FOUND;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200426 else
427 ret = client->pwr_state;
428 mutex_unlock(&vgasr_mutex);
429 return ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200430}
431EXPORT_SYMBOL(vga_switcheroo_get_client_state);
432
Lukas Wunnera6456542015-08-23 15:18:55 +0200433/**
434 * vga_switcheroo_unregister_client() - unregister client
435 * @pdev: client pci device
436 *
437 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
438 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000439void vga_switcheroo_unregister_client(struct pci_dev *pdev)
440{
Takashi Iwai79721e02012-04-26 12:55:59 +0200441 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000442
443 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200444 client = find_client_from_pci(&vgasr_priv.clients, pdev);
445 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200446 if (client_is_vga(client))
447 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200448 list_del(&client->list);
449 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000450 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200451 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200452 pr_info("disabled\n");
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200453 vga_switcheroo_debugfs_fini(&vgasr_priv);
454 vgasr_priv.active = false;
455 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000456 mutex_unlock(&vgasr_mutex);
457}
458EXPORT_SYMBOL(vga_switcheroo_unregister_client);
459
Lukas Wunnera6456542015-08-23 15:18:55 +0200460/**
461 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
462 * @pdev: client pci device
463 * @info: framebuffer
464 *
465 * Set framebuffer of a given client. The console will be remapped to this
466 * on switching.
467 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000468void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
469 struct fb_info *info)
470{
Takashi Iwai79721e02012-04-26 12:55:59 +0200471 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000472
473 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200474 client = find_client_from_pci(&vgasr_priv.clients, pdev);
475 if (client)
476 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000477 mutex_unlock(&vgasr_mutex);
478}
479EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
480
Lukas Wunnera6456542015-08-23 15:18:55 +0200481/**
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100482 * vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client
483 * @pdev: client pci device
484 *
485 * Temporarily switch DDC lines to the client identified by @pdev
486 * (but leave the outputs otherwise switched to where they are).
487 * This allows the inactive client to probe EDID. The DDC lines must
488 * afterwards be switched back by calling vga_switcheroo_unlock_ddc(),
489 * even if this function returns an error.
490 *
491 * Return: Previous DDC owner on success or a negative int on error.
492 * Specifically, %-ENODEV if no handler has registered or if the handler
493 * does not support switching the DDC lines. Also, a negative value
494 * returned by the handler is propagated back to the caller.
495 * The return value has merely an informational purpose for any caller
496 * which might be interested in it. It is acceptable to ignore the return
497 * value and simply rely on the result of the subsequent EDID probe,
498 * which will be %NULL if DDC switching failed.
499 */
500int vga_switcheroo_lock_ddc(struct pci_dev *pdev)
501{
502 enum vga_switcheroo_client_id id;
503
504 mutex_lock(&vgasr_priv.mux_hw_lock);
505 if (!vgasr_priv.handler || !vgasr_priv.handler->switch_ddc) {
506 vgasr_priv.old_ddc_owner = -ENODEV;
507 return -ENODEV;
508 }
509
510 id = vgasr_priv.handler->get_client_id(pdev);
511 vgasr_priv.old_ddc_owner = vgasr_priv.handler->switch_ddc(id);
512 return vgasr_priv.old_ddc_owner;
513}
514EXPORT_SYMBOL(vga_switcheroo_lock_ddc);
515
516/**
517 * vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner
518 * @pdev: client pci device
519 *
520 * Switch DDC lines back to the previous owner after calling
521 * vga_switcheroo_lock_ddc(). This must be called even if
522 * vga_switcheroo_lock_ddc() returned an error.
523 *
524 * Return: Previous DDC owner on success (i.e. the client identifier of @pdev)
525 * or a negative int on error.
526 * Specifically, %-ENODEV if no handler has registered or if the handler
527 * does not support switching the DDC lines. Also, a negative value
528 * returned by the handler is propagated back to the caller.
529 * Finally, invoking this function without calling vga_switcheroo_lock_ddc()
530 * first is not allowed and will result in %-EINVAL.
531 */
532int vga_switcheroo_unlock_ddc(struct pci_dev *pdev)
533{
534 enum vga_switcheroo_client_id id;
535 int ret = vgasr_priv.old_ddc_owner;
536
537 if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv.mux_hw_lock)))
538 return -EINVAL;
539
540 if (vgasr_priv.old_ddc_owner >= 0) {
541 id = vgasr_priv.handler->get_client_id(pdev);
542 if (vgasr_priv.old_ddc_owner != id)
543 ret = vgasr_priv.handler->switch_ddc(
544 vgasr_priv.old_ddc_owner);
545 }
546 mutex_unlock(&vgasr_priv.mux_hw_lock);
547 return ret;
548}
549EXPORT_SYMBOL(vga_switcheroo_unlock_ddc);
550
551/**
Lukas Wunnera6456542015-08-23 15:18:55 +0200552 * DOC: Manual switching and manual power control
553 *
554 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
555 * can be read to retrieve the current vga_switcheroo state and commands
556 * can be written to it to change the state. The file appears as soon as
557 * two GPU drivers and one handler have registered with vga_switcheroo.
558 * The following commands are understood:
559 *
560 * * OFF: Power off the device not in use.
561 * * ON: Power on the device not in use.
562 * * IGD: Switch to the integrated graphics device.
Lukas Wunnerfb001df2016-07-11 10:22:33 +0200563 * Power on the integrated GPU if necessary, power off the discrete GPU.
564 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
565 * have opened device files of the GPUs or the audio client. If the
566 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
567 * and /dev/snd/controlC1 to identify processes blocking the switch.
Lukas Wunnera6456542015-08-23 15:18:55 +0200568 * * DIS: Switch to the discrete graphics device.
569 * * DIGD: Delayed switch to the integrated graphics device.
Lukas Wunnerfb001df2016-07-11 10:22:33 +0200570 * This will perform the switch once the last user space process has
571 * closed the device files of the GPUs and the audio client.
Lukas Wunnera6456542015-08-23 15:18:55 +0200572 * * DDIS: Delayed switch to the discrete graphics device.
573 * * MIGD: Mux-only switch to the integrated graphics device.
Lukas Wunnerfb001df2016-07-11 10:22:33 +0200574 * Does not remap console or change the power state of either gpu.
575 * If the integrated GPU is currently off, the screen will turn black.
576 * If it is on, the screen will show whatever happens to be in VRAM.
577 * Either way, the user has to blindly enter the command to switch back.
Lukas Wunnera6456542015-08-23 15:18:55 +0200578 * * MDIS: Mux-only switch to the discrete graphics device.
579 *
580 * For GPUs whose power state is controlled by the driver's runtime pm,
581 * the ON and OFF commands are a no-op (see next section).
582 *
583 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
584 * should not be used.
585 */
586
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000587static int vga_switcheroo_show(struct seq_file *m, void *v)
588{
Takashi Iwai79721e02012-04-26 12:55:59 +0200589 struct vga_switcheroo_client *client;
590 int i = 0;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200591
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000592 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200593 list_for_each_entry(client, &vgasr_priv.clients, list) {
Dave Airlie0d697042012-09-10 12:28:36 +1000594 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200595 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
596 "IGD",
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200597 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200598 client->active ? '+' : ' ',
Dave Airlie0d697042012-09-10 12:28:36 +1000599 client->driver_power_control ? "Dyn" : "",
Takashi Iwai79721e02012-04-26 12:55:59 +0200600 client->pwr_state ? "Pwr" : "Off",
601 pci_name(client->pdev));
602 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000603 }
604 mutex_unlock(&vgasr_mutex);
605 return 0;
606}
607
608static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
609{
610 return single_open(file, vga_switcheroo_show, NULL);
611}
612
613static int vga_switchon(struct vga_switcheroo_client *client)
614{
Dave Airlie0d697042012-09-10 12:28:36 +1000615 if (client->driver_power_control)
616 return 0;
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000617 if (vgasr_priv.handler->power_state)
618 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000619 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200620 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000621 client->pwr_state = VGA_SWITCHEROO_ON;
622 return 0;
623}
624
625static int vga_switchoff(struct vga_switcheroo_client *client)
626{
Dave Airlie0d697042012-09-10 12:28:36 +1000627 if (client->driver_power_control)
628 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000629 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200630 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000631 if (vgasr_priv.handler->power_state)
632 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000633 client->pwr_state = VGA_SWITCHEROO_OFF;
634 return 0;
635}
636
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200637static void set_audio_state(enum vga_switcheroo_client_id id,
638 enum vga_switcheroo_state state)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200639{
640 struct vga_switcheroo_client *client;
641
642 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
643 if (client && client->pwr_state != state) {
644 client->ops->set_gpu_state(client->pdev, state);
645 client->pwr_state = state;
646 }
647}
648
Dave Airlie66b37c62010-12-07 14:24:25 +1000649/* stage one happens before delay */
650static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000651{
Takashi Iwai79721e02012-04-26 12:55:59 +0200652 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000653
Takashi Iwai79721e02012-04-26 12:55:59 +0200654 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000655 if (!active)
656 return 0;
657
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000658 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
659 vga_switchon(new_client);
660
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400661 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000662 return 0;
663}
664
665/* post delay */
666static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
667{
668 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200669 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000670
Takashi Iwai79721e02012-04-26 12:55:59 +0200671 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000672 if (!active)
673 return 0;
674
675 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000676
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200677 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
678
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000679 if (new_client->fb_info) {
680 struct fb_event event;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200681
Dave Airlie054430e2013-01-25 11:38:56 +1000682 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000683 event.info = new_client->fb_info;
684 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie054430e2013-01-25 11:38:56 +1000685 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000686 }
687
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100688 mutex_lock(&vgasr_priv.mux_hw_lock);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000689 ret = vgasr_priv.handler->switchto(new_client->id);
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100690 mutex_unlock(&vgasr_priv.mux_hw_lock);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000691 if (ret)
692 return ret;
693
Takashi Iwai26ec6852012-05-11 07:51:17 +0200694 if (new_client->ops->reprobe)
695 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000696
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000697 if (active->pwr_state == VGA_SWITCHEROO_ON)
698 vga_switchoff(active);
699
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200700 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
701
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000702 new_client->active = true;
703 return 0;
704}
705
Takashi Iwai79721e02012-04-26 12:55:59 +0200706static bool check_can_switch(void)
707{
708 struct vga_switcheroo_client *client;
709
710 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200711 if (!client->ops->can_switch(client->pdev)) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200712 pr_err("client %x refused switch\n", client->id);
Takashi Iwai79721e02012-04-26 12:55:59 +0200713 return false;
714 }
715 }
716 return true;
717}
718
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000719static ssize_t
720vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
721 size_t cnt, loff_t *ppos)
722{
723 char usercmd[64];
Takashi Iwai79721e02012-04-26 12:55:59 +0200724 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000725 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000726 bool just_mux = false;
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200727 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000728 struct vga_switcheroo_client *client = NULL;
729
730 if (cnt > 63)
731 cnt = 63;
732
733 if (copy_from_user(usercmd, ubuf, cnt))
734 return -EFAULT;
735
736 mutex_lock(&vgasr_mutex);
737
Jiri Slaby8c88e502010-04-27 14:11:03 -0700738 if (!vgasr_priv.active) {
739 cnt = -EINVAL;
740 goto out;
741 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000742
743 /* pwr off the device not in use */
744 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200745 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200746 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000747 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000748 if (client->driver_power_control)
749 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200750 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200751 if (client->pwr_state == VGA_SWITCHEROO_ON)
752 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000753 }
754 goto out;
755 }
756 /* pwr on the device not in use */
757 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200758 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200759 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000760 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000761 if (client->driver_power_control)
762 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200763 if (client->pwr_state == VGA_SWITCHEROO_OFF)
764 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200765 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000766 }
767 goto out;
768 }
769
770 /* request a delayed switch - test can we switch now */
771 if (strncmp(usercmd, "DIGD", 4) == 0) {
772 client_id = VGA_SWITCHEROO_IGD;
773 delay = true;
774 }
775
776 if (strncmp(usercmd, "DDIS", 4) == 0) {
777 client_id = VGA_SWITCHEROO_DIS;
778 delay = true;
779 }
780
781 if (strncmp(usercmd, "IGD", 3) == 0)
782 client_id = VGA_SWITCHEROO_IGD;
783
784 if (strncmp(usercmd, "DIS", 3) == 0)
785 client_id = VGA_SWITCHEROO_DIS;
786
Dan Carpenterfea6f332011-01-07 08:12:27 +0300787 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000788 just_mux = true;
789 client_id = VGA_SWITCHEROO_IGD;
790 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300791 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000792 just_mux = true;
793 client_id = VGA_SWITCHEROO_DIS;
794 }
795
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200796 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000797 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200798 client = find_client_from_id(&vgasr_priv.clients, client_id);
799 if (!client)
800 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000801
802 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000803
804 if (just_mux) {
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100805 mutex_lock(&vgasr_priv.mux_hw_lock);
Dave Airlie851ab952010-12-06 12:35:52 +1000806 ret = vgasr_priv.handler->switchto(client_id);
Lukas Wunnere4cb81d2016-01-11 20:09:20 +0100807 mutex_unlock(&vgasr_priv.mux_hw_lock);
Dave Airlie851ab952010-12-06 12:35:52 +1000808 goto out;
809 }
810
Takashi Iwai79721e02012-04-26 12:55:59 +0200811 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200812 goto out;
813
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000814 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200815 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000816
817 if (can_switch == false && delay == false)
818 goto out;
819
Takashi Iwai79721e02012-04-26 12:55:59 +0200820 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000821 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000822 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200823 pr_err("switching failed stage 1 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000824
825 ret = vga_switchto_stage2(client);
826 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200827 pr_err("switching failed stage 2 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000828
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000829 } else {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200830 pr_info("setting delayed switch to client %d\n", client->id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000831 vgasr_priv.delayed_switch_active = true;
832 vgasr_priv.delayed_client_id = client_id;
833
Dave Airlie66b37c62010-12-07 14:24:25 +1000834 ret = vga_switchto_stage1(client);
835 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200836 pr_err("delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000837 }
838
839out:
840 mutex_unlock(&vgasr_mutex);
841 return cnt;
842}
843
844static const struct file_operations vga_switcheroo_debugfs_fops = {
845 .owner = THIS_MODULE,
846 .open = vga_switcheroo_debugfs_open,
847 .write = vga_switcheroo_debugfs_write,
848 .read = seq_read,
849 .llseek = seq_lseek,
850 .release = single_release,
851};
852
853static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
854{
Thierry Reding798ae0f2015-08-12 16:32:12 +0200855 debugfs_remove(priv->switch_file);
856 priv->switch_file = NULL;
857
858 debugfs_remove(priv->debugfs_root);
859 priv->debugfs_root = NULL;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000860}
861
862static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
863{
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200864 static const char mp[] = "/sys/kernel/debug";
865
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000866 /* already initialised */
867 if (priv->debugfs_root)
868 return 0;
869 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
870
871 if (!priv->debugfs_root) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200872 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000873 goto fail;
874 }
875
876 priv->switch_file = debugfs_create_file("switch", 0644,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200877 priv->debugfs_root, NULL,
878 &vga_switcheroo_debugfs_fops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000879 if (!priv->switch_file) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200880 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000881 goto fail;
882 }
883 return 0;
884fail:
885 vga_switcheroo_debugfs_fini(priv);
886 return -1;
887}
888
Lukas Wunnera6456542015-08-23 15:18:55 +0200889/**
890 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
891 *
892 * Process a delayed switch if one is pending. DRM drivers should call this
893 * from their ->lastclose callback.
894 *
895 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
896 * has unregistered in the meantime or if there are other clients blocking the
897 * switch. If the actual switch fails, an error is reported and 0 is returned.
898 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000899int vga_switcheroo_process_delayed_switch(void)
900{
Takashi Iwai79721e02012-04-26 12:55:59 +0200901 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000902 int ret;
903 int err = -EINVAL;
904
905 mutex_lock(&vgasr_mutex);
906 if (!vgasr_priv.delayed_switch_active)
907 goto err;
908
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200909 pr_info("processing delayed switch to %d\n",
910 vgasr_priv.delayed_client_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000911
Takashi Iwai79721e02012-04-26 12:55:59 +0200912 client = find_client_from_id(&vgasr_priv.clients,
913 vgasr_priv.delayed_client_id);
914 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000915 goto err;
916
Dave Airlie66b37c62010-12-07 14:24:25 +1000917 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000918 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200919 pr_err("delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000920
921 vgasr_priv.delayed_switch_active = false;
922 err = 0;
923err:
924 mutex_unlock(&vgasr_mutex);
925 return err;
926}
927EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
Dave Airlie0d697042012-09-10 12:28:36 +1000928
Lukas Wunnera6456542015-08-23 15:18:55 +0200929/**
930 * DOC: Driver power control
931 *
932 * In this mode of use, the discrete GPU automatically powers up and down at
933 * the discretion of the driver's runtime pm. On muxed machines, the user may
934 * still influence the muxer state by way of the debugfs interface, however
935 * the ON and OFF commands become a no-op for the discrete GPU.
936 *
937 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
938 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
939 * command line disables it.
940 *
941 * When the driver decides to power up or down, it notifies vga_switcheroo
942 * thereof so that it can (a) power the audio device on the GPU up or down,
943 * and (b) update its internal power state representation for the device.
944 * This is achieved by vga_switcheroo_set_dynamic_switch().
945 *
946 * After the GPU has been suspended, the handler needs to be called to cut
947 * power to the GPU. Likewise it needs to reinstate power before the GPU
948 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
949 * which augments the GPU's suspend/resume functions by the requisite
950 * calls to the handler.
951 *
952 * When the audio device resumes, the GPU needs to be woken. This is achieved
953 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
954 * audio device's resume function.
955 *
956 * On muxed machines, if the mux is initially switched to the discrete GPU,
957 * the user ends up with a black screen when the GPU powers down after boot.
958 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
959 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
960 */
961
Thierry Reding7491bfb2015-08-12 16:32:11 +0200962static void vga_switcheroo_power_switch(struct pci_dev *pdev,
963 enum vga_switcheroo_state state)
Dave Airlie0d697042012-09-10 12:28:36 +1000964{
965 struct vga_switcheroo_client *client;
966
967 if (!vgasr_priv.handler->power_state)
968 return;
969
970 client = find_client_from_pci(&vgasr_priv.clients, pdev);
971 if (!client)
972 return;
973
974 if (!client->driver_power_control)
975 return;
976
977 vgasr_priv.handler->power_state(client->id, state);
978}
979
Lukas Wunnera6456542015-08-23 15:18:55 +0200980/**
981 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
982 * @pdev: client pci device
983 * @dynamic: new power state
984 *
985 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
986 * When the driver decides to power up or down, it notifies vga_switcheroo
987 * thereof using this helper so that it can (a) power the audio device on
988 * the GPU up or down, and (b) update its internal power state representation
989 * for the device.
990 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200991void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
992 enum vga_switcheroo_state dynamic)
Dave Airlie0d697042012-09-10 12:28:36 +1000993{
994 struct vga_switcheroo_client *client;
995
Lukas Wunner8f12a312015-08-23 23:23:02 +0200996 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000997 client = find_client_from_pci(&vgasr_priv.clients, pdev);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200998 if (!client || !client->driver_power_control) {
999 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +10001000 return;
Lukas Wunner8f12a312015-08-23 23:23:02 +02001001 }
Dave Airlie0d697042012-09-10 12:28:36 +10001002
1003 client->pwr_state = dynamic;
1004 set_audio_state(client->id, dynamic);
Lukas Wunner8f12a312015-08-23 23:23:02 +02001005 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +10001006}
1007EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
1008
1009/* switcheroo power domain */
1010static int vga_switcheroo_runtime_suspend(struct device *dev)
1011{
1012 struct pci_dev *pdev = to_pci_dev(dev);
1013 int ret;
1014
1015 ret = dev->bus->pm->runtime_suspend(dev);
1016 if (ret)
1017 return ret;
Lukas Wunner8f12a312015-08-23 23:23:02 +02001018 mutex_lock(&vgasr_mutex);
Lukas Wunnere4cb81d2016-01-11 20:09:20 +01001019 if (vgasr_priv.handler->switchto) {
1020 mutex_lock(&vgasr_priv.mux_hw_lock);
Alex Deucherf2bc561612014-05-19 14:04:43 -04001021 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
Lukas Wunnere4cb81d2016-01-11 20:09:20 +01001022 mutex_unlock(&vgasr_priv.mux_hw_lock);
1023 }
Dave Airlie0d697042012-09-10 12:28:36 +10001024 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
Lukas Wunner8f12a312015-08-23 23:23:02 +02001025 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +10001026 return 0;
1027}
1028
1029static int vga_switcheroo_runtime_resume(struct device *dev)
1030{
1031 struct pci_dev *pdev = to_pci_dev(dev);
1032 int ret;
1033
Lukas Wunner8f12a312015-08-23 23:23:02 +02001034 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +10001035 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
Lukas Wunner8f12a312015-08-23 23:23:02 +02001036 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +10001037 ret = dev->bus->pm->runtime_resume(dev);
1038 if (ret)
1039 return ret;
1040
1041 return 0;
1042}
1043
Lukas Wunnera6456542015-08-23 15:18:55 +02001044/**
1045 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
1046 * @dev: vga client device
1047 * @domain: power domain
1048 *
1049 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1050 * After the GPU has been suspended, the handler needs to be called to cut
1051 * power to the GPU. Likewise it needs to reinstate power before the GPU
1052 * can resume. To this end, this helper augments the suspend/resume functions
1053 * by the requisite calls to the handler. It needs only be called on platforms
1054 * where the power switch is separate to the device being powered down.
1055 */
Thierry Reding7491bfb2015-08-12 16:32:11 +02001056int vga_switcheroo_init_domain_pm_ops(struct device *dev,
1057 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +10001058{
1059 /* copy over all the bus versions */
1060 if (dev->bus && dev->bus->pm) {
1061 domain->ops = *dev->bus->pm;
1062 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
1063 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
1064
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001065 dev_pm_domain_set(dev, domain);
Dave Airlie0d697042012-09-10 12:28:36 +10001066 return 0;
1067 }
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001068 dev_pm_domain_set(dev, NULL);
Dave Airlie0d697042012-09-10 12:28:36 +10001069 return -EINVAL;
1070}
1071EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
1072
Alex Deucher766a53d2014-09-12 17:51:29 -04001073void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
1074{
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001075 dev_pm_domain_set(dev, NULL);
Alex Deucher766a53d2014-09-12 17:51:29 -04001076}
1077EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
1078
Dave Airlie0d697042012-09-10 12:28:36 +10001079static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
1080{
1081 struct pci_dev *pdev = to_pci_dev(dev);
Lukas Wunner8f12a312015-08-23 23:23:02 +02001082 struct vga_switcheroo_client *client;
1083 struct device *video_dev = NULL;
Dave Airlie0d697042012-09-10 12:28:36 +10001084 int ret;
Dave Airlie0d697042012-09-10 12:28:36 +10001085
1086 /* we need to check if we have to switch back on the video
1087 device so the audio device can come back */
Lukas Wunner8f12a312015-08-23 23:23:02 +02001088 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +10001089 list_for_each_entry(client, &vgasr_priv.clients, list) {
Thierry Reding7491bfb2015-08-12 16:32:11 +02001090 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
1091 client_is_vga(client)) {
Lukas Wunner8f12a312015-08-23 23:23:02 +02001092 video_dev = &client->pdev->dev;
Dave Airlie0d697042012-09-10 12:28:36 +10001093 break;
1094 }
1095 }
Lukas Wunner8f12a312015-08-23 23:23:02 +02001096 mutex_unlock(&vgasr_mutex);
1097
1098 if (video_dev) {
1099 ret = pm_runtime_get_sync(video_dev);
1100 if (ret && ret != 1)
1101 return ret;
1102 }
Dave Airlie0d697042012-09-10 12:28:36 +10001103 ret = dev->bus->pm->runtime_resume(dev);
1104
1105 /* put the reference for the gpu */
Lukas Wunner8f12a312015-08-23 23:23:02 +02001106 if (video_dev) {
1107 pm_runtime_mark_last_busy(video_dev);
1108 pm_runtime_put_autosuspend(video_dev);
Dave Airlie0d697042012-09-10 12:28:36 +10001109 }
1110 return ret;
1111}
1112
Lukas Wunnera6456542015-08-23 15:18:55 +02001113/**
1114 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
1115 * power control
1116 * @dev: audio client device
1117 * @domain: power domain
1118 *
1119 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1120 * When the audio device resumes, the GPU needs to be woken. This helper
1121 * augments the audio device's resume function to do that.
1122 *
1123 * Return: 0 on success, -EINVAL if no power management operations are
1124 * defined for this device.
1125 */
Thierry Reding7491bfb2015-08-12 16:32:11 +02001126int
1127vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
1128 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +10001129{
1130 /* copy over all the bus versions */
1131 if (dev->bus && dev->bus->pm) {
1132 domain->ops = *dev->bus->pm;
Thierry Reding7491bfb2015-08-12 16:32:11 +02001133 domain->ops.runtime_resume =
1134 vga_switcheroo_runtime_resume_hdmi_audio;
Dave Airlie0d697042012-09-10 12:28:36 +10001135
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001136 dev_pm_domain_set(dev, domain);
Dave Airlie0d697042012-09-10 12:28:36 +10001137 return 0;
1138 }
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001139 dev_pm_domain_set(dev, NULL);
Dave Airlie0d697042012-09-10 12:28:36 +10001140 return -EINVAL;
1141}
1142EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);