]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/gpu/vga/vga_switcheroo.c
Merge remote-tracking branch 'mkp-scsi/4.5/scsi-fixes' into fixes
[mirror_ubuntu-zesty-kernel.git] / drivers / gpu / vga / vga_switcheroo.c
CommitLineData
6a9ee8af 1/*
a645654b
LW
2 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
3 *
6a9ee8af
DA
4 * Copyright (c) 2010 Red Hat Inc.
5 * Author : Dave Airlie <airlied@redhat.com>
6 *
a645654b 7 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
6a9ee8af 8 *
a645654b
LW
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:
6a9ee8af 15 *
a645654b
LW
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.
71309278 19 *
a645654b
LW
20 * 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.
71309278 28 *
6a9ee8af
DA
29 */
30
9b0be1eb
TR
31#define pr_fmt(fmt) "vga_switcheroo: " fmt
32
4127838c 33#include <linux/console.h>
6a9ee8af
DA
34#include <linux/debugfs.h>
35#include <linux/fb.h>
4127838c
LW
36#include <linux/fs.h>
37#include <linux/module.h>
6a9ee8af 38#include <linux/pci.h>
989561de 39#include <linux/pm_domain.h>
0d69704a 40#include <linux/pm_runtime.h>
4127838c
LW
41#include <linux/seq_file.h>
42#include <linux/uaccess.h>
2fbe8c7c 43#include <linux/vgaarb.h>
4127838c 44#include <linux/vga_switcheroo.h>
2fbe8c7c 45
a645654b
LW
46/**
47 * DOC: Overview
48 *
49 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
50 * These come in two flavors:
51 *
52 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
53 * * muxless: Dual GPUs but only one of them is connected to outputs.
54 * The other one is merely used to offload rendering, its results
55 * are copied over PCIe into the framebuffer. On Linux this is
56 * supported with DRI PRIME.
57 *
58 * Hybrid graphics started to appear in the late Naughties and were initially
59 * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
60 * A notable exception is the MacBook Pro which continues to use a mux.
61 * Muxes come with varying capabilities: Some switch only the panel, others
62 * can also switch external displays. Some switch all display pins at once
63 * while others can switch just the DDC lines. (To allow EDID probing
64 * for the inactive GPU.) Also, muxes are often used to cut power to the
65 * discrete GPU while it is not used.
66 *
3a848662 67 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
a645654b
LW
68 * clients. The mux is called the handler. Muxless machines also register a
69 * handler to control the power state of the discrete GPU, its ->switchto
70 * callback is a no-op for obvious reasons. The discrete GPU is often equipped
71 * with an HDA controller for the HDMI/DP audio signal, this will also
72 * register as a client so that vga_switcheroo can take care of the correct
73 * suspend/resume order when changing the discrete GPU's power state. In total
74 * there can thus be up to three clients: Two vga clients (GPUs) and one audio
75 * client (on the discrete GPU). The code is mostly prepared to support
76 * machines with more than two GPUs should they become available.
77 * The GPU to which the outputs are currently switched is called the
78 * active client in vga_switcheroo parlance. The GPU not in use is the
79 * inactive client.
80 */
81
82/**
83 * struct vga_switcheroo_client - registered client
84 * @pdev: client pci device
85 * @fb_info: framebuffer to which console is remapped on switching
86 * @pwr_state: current power state
87 * @ops: client callbacks
21c5ba8c
LW
88 * @id: client identifier. Determining the id requires the handler,
89 * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
90 * and later given their true id in vga_switcheroo_enable()
a645654b
LW
91 * @active: whether the outputs are currently switched to this client
92 * @driver_power_control: whether power state is controlled by the driver's
93 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
94 * interface is a no-op so as not to interfere with runtime pm
95 * @list: client list
96 *
97 * Registered client. A client can be either a GPU or an audio device on a GPU.
98 * For audio clients, the @fb_info, @active and @driver_power_control members
99 * are bogus.
100 */
6a9ee8af
DA
101struct vga_switcheroo_client {
102 struct pci_dev *pdev;
103 struct fb_info *fb_info;
203d027d 104 enum vga_switcheroo_state pwr_state;
26ec685f 105 const struct vga_switcheroo_client_ops *ops;
fa3e967f 106 enum vga_switcheroo_client_id id;
6a9ee8af 107 bool active;
0d69704a 108 bool driver_power_control;
79721e0a 109 struct list_head list;
6a9ee8af
DA
110};
111
a645654b
LW
112/*
113 * protects access to struct vgasr_priv
114 */
6a9ee8af
DA
115static DEFINE_MUTEX(vgasr_mutex);
116
a645654b
LW
117/**
118 * struct vgasr_priv - vga_switcheroo private data
119 * @active: whether vga_switcheroo is enabled.
120 * Prerequisite is the registration of two GPUs and a handler
121 * @delayed_switch_active: whether a delayed switch is pending
122 * @delayed_client_id: client to which a delayed switch is pending
123 * @debugfs_root: directory for vga_switcheroo debugfs interface
124 * @switch_file: file for vga_switcheroo debugfs interface
125 * @registered_clients: number of registered GPUs
126 * (counting only vga clients, not audio clients)
127 * @clients: list of registered clients
128 * @handler: registered handler
129 *
130 * vga_switcheroo private data. Currently only one vga_switcheroo instance
131 * per system is supported.
132 */
6a9ee8af 133struct vgasr_priv {
6a9ee8af
DA
134 bool active;
135 bool delayed_switch_active;
136 enum vga_switcheroo_client_id delayed_client_id;
137
138 struct dentry *debugfs_root;
139 struct dentry *switch_file;
140
141 int registered_clients;
79721e0a 142 struct list_head clients;
6a9ee8af 143
5d170139 144 const struct vga_switcheroo_handler *handler;
6a9ee8af
DA
145};
146
3e9e63db
TI
147#define ID_BIT_AUDIO 0x100
148#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
21c5ba8c
LW
149#define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
150 !client_is_audio(c))
3e9e63db
TI
151#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
152
6a9ee8af
DA
153static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
154static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
155
156/* only one switcheroo per system */
79721e0a
TI
157static struct vgasr_priv vgasr_priv = {
158 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
159};
6a9ee8af 160
36704c0c 161static bool vga_switcheroo_ready(void)
6a9ee8af 162{
36704c0c
SF
163 /* we're ready if we get two clients + handler */
164 return !vgasr_priv.active &&
165 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
6a9ee8af 166}
6a9ee8af
DA
167
168static void vga_switcheroo_enable(void)
169{
6a9ee8af 170 int ret;
79721e0a
TI
171 struct vga_switcheroo_client *client;
172
6a9ee8af 173 /* call the handler to init */
e99eac5e
SF
174 if (vgasr_priv.handler->init)
175 vgasr_priv.handler->init();
6a9ee8af 176
79721e0a 177 list_for_each_entry(client, &vgasr_priv.clients, list) {
21c5ba8c 178 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
3e9e63db 179 continue;
79721e0a 180 ret = vgasr_priv.handler->get_client_id(client->pdev);
6a9ee8af
DA
181 if (ret < 0)
182 return;
183
79721e0a 184 client->id = ret;
6a9ee8af
DA
185 }
186 vga_switcheroo_debugfs_init(&vgasr_priv);
187 vgasr_priv.active = true;
188}
189
a645654b
LW
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 */
5d170139 199int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler)
36704c0c
SF
200{
201 mutex_lock(&vgasr_mutex);
202 if (vgasr_priv.handler) {
203 mutex_unlock(&vgasr_mutex);
204 return -EINVAL;
205 }
206
207 vgasr_priv.handler = handler;
208 if (vga_switcheroo_ready()) {
9b0be1eb 209 pr_info("enabled\n");
36704c0c
SF
210 vga_switcheroo_enable();
211 }
212 mutex_unlock(&vgasr_mutex);
213 return 0;
214}
215EXPORT_SYMBOL(vga_switcheroo_register_handler);
216
a645654b
LW
217/**
218 * vga_switcheroo_unregister_handler() - unregister handler
219 *
220 * Unregister handler. Disable vga_switcheroo.
221 */
36704c0c
SF
222void vga_switcheroo_unregister_handler(void)
223{
224 mutex_lock(&vgasr_mutex);
225 vgasr_priv.handler = NULL;
226 if (vgasr_priv.active) {
9b0be1eb 227 pr_info("disabled\n");
36704c0c
SF
228 vga_switcheroo_debugfs_fini(&vgasr_priv);
229 vgasr_priv.active = false;
230 }
231 mutex_unlock(&vgasr_mutex);
232}
233EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
234
3e9e63db
TI
235static int register_client(struct pci_dev *pdev,
236 const struct vga_switcheroo_client_ops *ops,
fa3e967f
LW
237 enum vga_switcheroo_client_id id, bool active,
238 bool driver_power_control)
6a9ee8af 239{
79721e0a 240 struct vga_switcheroo_client *client;
6a9ee8af 241
79721e0a
TI
242 client = kzalloc(sizeof(*client), GFP_KERNEL);
243 if (!client)
244 return -ENOMEM;
245
246 client->pwr_state = VGA_SWITCHEROO_ON;
247 client->pdev = pdev;
26ec685f 248 client->ops = ops;
3e9e63db
TI
249 client->id = id;
250 client->active = active;
0d69704a 251 client->driver_power_control = driver_power_control;
6a9ee8af 252
79721e0a
TI
253 mutex_lock(&vgasr_mutex);
254 list_add_tail(&client->list, &vgasr_priv.clients);
3e9e63db
TI
255 if (client_is_vga(client))
256 vgasr_priv.registered_clients++;
6a9ee8af 257
36704c0c 258 if (vga_switcheroo_ready()) {
9b0be1eb 259 pr_info("enabled\n");
6a9ee8af
DA
260 vga_switcheroo_enable();
261 }
262 mutex_unlock(&vgasr_mutex);
263 return 0;
264}
3e9e63db 265
a645654b
LW
266/**
267 * vga_switcheroo_register_client - register vga client
268 * @pdev: client pci device
269 * @ops: client callbacks
270 * @driver_power_control: whether power state is controlled by the driver's
271 * runtime pm
272 *
273 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
274 * handler have already registered. The power state of the client is assumed
275 * to be ON.
276 *
277 * Return: 0 on success, -ENOMEM on memory allocation error.
278 */
3e9e63db 279int vga_switcheroo_register_client(struct pci_dev *pdev,
0d69704a
DA
280 const struct vga_switcheroo_client_ops *ops,
281 bool driver_power_control)
3e9e63db 282{
21c5ba8c 283 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
7491bfb4
TR
284 pdev == vga_default_device(),
285 driver_power_control);
3e9e63db 286}
6a9ee8af
DA
287EXPORT_SYMBOL(vga_switcheroo_register_client);
288
a645654b
LW
289/**
290 * vga_switcheroo_register_audio_client - register audio client
291 * @pdev: client pci device
292 * @ops: client callbacks
fa3e967f 293 * @id: client identifier
a645654b
LW
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 */
3e9e63db
TI
300int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
301 const struct vga_switcheroo_client_ops *ops,
fa3e967f 302 enum vga_switcheroo_client_id id)
3e9e63db 303{
21b45676 304 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
3e9e63db
TI
305}
306EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
307
79721e0a
TI
308static struct vga_switcheroo_client *
309find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
310{
311 struct vga_switcheroo_client *client;
7491bfb4 312
79721e0a
TI
313 list_for_each_entry(client, head, list)
314 if (client->pdev == pdev)
315 return client;
316 return NULL;
317}
318
319static struct vga_switcheroo_client *
fa3e967f
LW
320find_client_from_id(struct list_head *head,
321 enum vga_switcheroo_client_id client_id)
79721e0a
TI
322{
323 struct vga_switcheroo_client *client;
7491bfb4 324
79721e0a
TI
325 list_for_each_entry(client, head, list)
326 if (client->id == client_id)
327 return client;
328 return NULL;
329}
330
331static struct vga_switcheroo_client *
332find_active_client(struct list_head *head)
333{
334 struct vga_switcheroo_client *client;
7491bfb4 335
79721e0a 336 list_for_each_entry(client, head, list)
21b45676 337 if (client->active)
79721e0a
TI
338 return client;
339 return NULL;
340}
341
a645654b
LW
342/**
343 * vga_switcheroo_get_client_state() - obtain power state of a given client
344 * @pdev: client pci device
345 *
346 * Obtain power state of a given client as seen from vga_switcheroo.
347 * The function is only called from hda_intel.c.
348 *
349 * Return: Power state.
350 */
203d027d 351enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
c8e9cf7b
TI
352{
353 struct vga_switcheroo_client *client;
8f12a311 354 enum vga_switcheroo_state ret;
c8e9cf7b 355
8f12a311 356 mutex_lock(&vgasr_mutex);
c8e9cf7b
TI
357 client = find_client_from_pci(&vgasr_priv.clients, pdev);
358 if (!client)
8f12a311 359 ret = VGA_SWITCHEROO_NOT_FOUND;
8f12a311
LW
360 else
361 ret = client->pwr_state;
362 mutex_unlock(&vgasr_mutex);
363 return ret;
c8e9cf7b
TI
364}
365EXPORT_SYMBOL(vga_switcheroo_get_client_state);
366
a645654b
LW
367/**
368 * vga_switcheroo_unregister_client() - unregister client
369 * @pdev: client pci device
370 *
371 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
372 */
6a9ee8af
DA
373void vga_switcheroo_unregister_client(struct pci_dev *pdev)
374{
79721e0a 375 struct vga_switcheroo_client *client;
6a9ee8af
DA
376
377 mutex_lock(&vgasr_mutex);
79721e0a
TI
378 client = find_client_from_pci(&vgasr_priv.clients, pdev);
379 if (client) {
3e9e63db
TI
380 if (client_is_vga(client))
381 vgasr_priv.registered_clients--;
79721e0a
TI
382 list_del(&client->list);
383 kfree(client);
6a9ee8af 384 }
3e9e63db 385 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
9b0be1eb 386 pr_info("disabled\n");
3e9e63db
TI
387 vga_switcheroo_debugfs_fini(&vgasr_priv);
388 vgasr_priv.active = false;
389 }
6a9ee8af
DA
390 mutex_unlock(&vgasr_mutex);
391}
392EXPORT_SYMBOL(vga_switcheroo_unregister_client);
393
a645654b
LW
394/**
395 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
396 * @pdev: client pci device
397 * @info: framebuffer
398 *
399 * Set framebuffer of a given client. The console will be remapped to this
400 * on switching.
401 */
6a9ee8af
DA
402void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
403 struct fb_info *info)
404{
79721e0a 405 struct vga_switcheroo_client *client;
6a9ee8af
DA
406
407 mutex_lock(&vgasr_mutex);
79721e0a
TI
408 client = find_client_from_pci(&vgasr_priv.clients, pdev);
409 if (client)
410 client->fb_info = info;
6a9ee8af
DA
411 mutex_unlock(&vgasr_mutex);
412}
413EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
414
a645654b
LW
415/**
416 * DOC: Manual switching and manual power control
417 *
418 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
419 * can be read to retrieve the current vga_switcheroo state and commands
420 * can be written to it to change the state. The file appears as soon as
421 * two GPU drivers and one handler have registered with vga_switcheroo.
422 * The following commands are understood:
423 *
424 * * OFF: Power off the device not in use.
425 * * ON: Power on the device not in use.
426 * * IGD: Switch to the integrated graphics device.
427 * Power on the integrated GPU if necessary, power off the discrete GPU.
428 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
429 * have opened device files of the GPUs or the audio client. If the
430 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
431 * and /dev/snd/controlC1 to identify processes blocking the switch.
432 * * DIS: Switch to the discrete graphics device.
433 * * DIGD: Delayed switch to the integrated graphics device.
434 * This will perform the switch once the last user space process has
435 * closed the device files of the GPUs and the audio client.
436 * * DDIS: Delayed switch to the discrete graphics device.
437 * * MIGD: Mux-only switch to the integrated graphics device.
438 * Does not remap console or change the power state of either gpu.
439 * If the integrated GPU is currently off, the screen will turn black.
440 * If it is on, the screen will show whatever happens to be in VRAM.
441 * Either way, the user has to blindly enter the command to switch back.
442 * * MDIS: Mux-only switch to the discrete graphics device.
443 *
444 * For GPUs whose power state is controlled by the driver's runtime pm,
445 * the ON and OFF commands are a no-op (see next section).
446 *
447 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
448 * should not be used.
449 */
450
6a9ee8af
DA
451static int vga_switcheroo_show(struct seq_file *m, void *v)
452{
79721e0a
TI
453 struct vga_switcheroo_client *client;
454 int i = 0;
7491bfb4 455
6a9ee8af 456 mutex_lock(&vgasr_mutex);
79721e0a 457 list_for_each_entry(client, &vgasr_priv.clients, list) {
0d69704a 458 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
7491bfb4
TR
459 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
460 "IGD",
3e9e63db 461 client_is_vga(client) ? "" : "-Audio",
79721e0a 462 client->active ? '+' : ' ',
0d69704a 463 client->driver_power_control ? "Dyn" : "",
79721e0a
TI
464 client->pwr_state ? "Pwr" : "Off",
465 pci_name(client->pdev));
466 i++;
6a9ee8af
DA
467 }
468 mutex_unlock(&vgasr_mutex);
469 return 0;
470}
471
472static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
473{
474 return single_open(file, vga_switcheroo_show, NULL);
475}
476
477static int vga_switchon(struct vga_switcheroo_client *client)
478{
0d69704a
DA
479 if (client->driver_power_control)
480 return 0;
5cfb3c3a
DA
481 if (vgasr_priv.handler->power_state)
482 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
6a9ee8af 483 /* call the driver callback to turn on device */
26ec685f 484 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
6a9ee8af
DA
485 client->pwr_state = VGA_SWITCHEROO_ON;
486 return 0;
487}
488
489static int vga_switchoff(struct vga_switcheroo_client *client)
490{
0d69704a
DA
491 if (client->driver_power_control)
492 return 0;
6a9ee8af 493 /* call the driver callback to turn off device */
26ec685f 494 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
5cfb3c3a
DA
495 if (vgasr_priv.handler->power_state)
496 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
6a9ee8af
DA
497 client->pwr_state = VGA_SWITCHEROO_OFF;
498 return 0;
499}
500
fa3e967f
LW
501static void set_audio_state(enum vga_switcheroo_client_id id,
502 enum vga_switcheroo_state state)
3e9e63db
TI
503{
504 struct vga_switcheroo_client *client;
505
506 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
507 if (client && client->pwr_state != state) {
508 client->ops->set_gpu_state(client->pdev, state);
509 client->pwr_state = state;
510 }
511}
512
66b37c67
DA
513/* stage one happens before delay */
514static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
6a9ee8af 515{
79721e0a 516 struct vga_switcheroo_client *active;
6a9ee8af 517
79721e0a 518 active = find_active_client(&vgasr_priv.clients);
6a9ee8af
DA
519 if (!active)
520 return 0;
521
6a9ee8af
DA
522 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
523 vga_switchon(new_client);
524
2fbe8c7c 525 vga_set_default_device(new_client->pdev);
66b37c67
DA
526 return 0;
527}
528
529/* post delay */
530static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
531{
532 int ret;
79721e0a 533 struct vga_switcheroo_client *active;
66b37c67 534
79721e0a 535 active = find_active_client(&vgasr_priv.clients);
66b37c67
DA
536 if (!active)
537 return 0;
538
539 active->active = false;
6a9ee8af 540
c91c3fae
TI
541 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
542
6a9ee8af
DA
543 if (new_client->fb_info) {
544 struct fb_event event;
7491bfb4 545
054430e7 546 console_lock();
6a9ee8af
DA
547 event.info = new_client->fb_info;
548 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
054430e7 549 console_unlock();
6a9ee8af
DA
550 }
551
552 ret = vgasr_priv.handler->switchto(new_client->id);
553 if (ret)
554 return ret;
555
26ec685f
TI
556 if (new_client->ops->reprobe)
557 new_client->ops->reprobe(new_client->pdev);
8d608aa6 558
6a9ee8af
DA
559 if (active->pwr_state == VGA_SWITCHEROO_ON)
560 vga_switchoff(active);
561
c91c3fae
TI
562 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
563
6a9ee8af
DA
564 new_client->active = true;
565 return 0;
566}
567
79721e0a
TI
568static bool check_can_switch(void)
569{
570 struct vga_switcheroo_client *client;
571
572 list_for_each_entry(client, &vgasr_priv.clients, list) {
26ec685f 573 if (!client->ops->can_switch(client->pdev)) {
9b0be1eb 574 pr_err("client %x refused switch\n", client->id);
79721e0a
TI
575 return false;
576 }
577 }
578 return true;
579}
580
6a9ee8af
DA
581static ssize_t
582vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
583 size_t cnt, loff_t *ppos)
584{
585 char usercmd[64];
79721e0a 586 int ret;
6a9ee8af 587 bool delay = false, can_switch;
851ab954 588 bool just_mux = false;
fa3e967f 589 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
6a9ee8af
DA
590 struct vga_switcheroo_client *client = NULL;
591
592 if (cnt > 63)
593 cnt = 63;
594
595 if (copy_from_user(usercmd, ubuf, cnt))
596 return -EFAULT;
597
598 mutex_lock(&vgasr_mutex);
599
8c88e50b
JS
600 if (!vgasr_priv.active) {
601 cnt = -EINVAL;
602 goto out;
603 }
6a9ee8af
DA
604
605 /* pwr off the device not in use */
606 if (strncmp(usercmd, "OFF", 3) == 0) {
79721e0a 607 list_for_each_entry(client, &vgasr_priv.clients, list) {
c91c3fae 608 if (client->active || client_is_audio(client))
6a9ee8af 609 continue;
0d69704a
DA
610 if (client->driver_power_control)
611 continue;
c91c3fae 612 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
79721e0a
TI
613 if (client->pwr_state == VGA_SWITCHEROO_ON)
614 vga_switchoff(client);
6a9ee8af
DA
615 }
616 goto out;
617 }
618 /* pwr on the device not in use */
619 if (strncmp(usercmd, "ON", 2) == 0) {
79721e0a 620 list_for_each_entry(client, &vgasr_priv.clients, list) {
c91c3fae 621 if (client->active || client_is_audio(client))
6a9ee8af 622 continue;
0d69704a
DA
623 if (client->driver_power_control)
624 continue;
79721e0a
TI
625 if (client->pwr_state == VGA_SWITCHEROO_OFF)
626 vga_switchon(client);
c91c3fae 627 set_audio_state(client->id, VGA_SWITCHEROO_ON);
6a9ee8af
DA
628 }
629 goto out;
630 }
631
632 /* request a delayed switch - test can we switch now */
633 if (strncmp(usercmd, "DIGD", 4) == 0) {
634 client_id = VGA_SWITCHEROO_IGD;
635 delay = true;
636 }
637
638 if (strncmp(usercmd, "DDIS", 4) == 0) {
639 client_id = VGA_SWITCHEROO_DIS;
640 delay = true;
641 }
642
643 if (strncmp(usercmd, "IGD", 3) == 0)
644 client_id = VGA_SWITCHEROO_IGD;
645
646 if (strncmp(usercmd, "DIS", 3) == 0)
647 client_id = VGA_SWITCHEROO_DIS;
648
fea6f330 649 if (strncmp(usercmd, "MIGD", 4) == 0) {
851ab954
DA
650 just_mux = true;
651 client_id = VGA_SWITCHEROO_IGD;
652 }
fea6f330 653 if (strncmp(usercmd, "MDIS", 4) == 0) {
851ab954
DA
654 just_mux = true;
655 client_id = VGA_SWITCHEROO_DIS;
656 }
657
21c5ba8c 658 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
6a9ee8af 659 goto out;
79721e0a
TI
660 client = find_client_from_id(&vgasr_priv.clients, client_id);
661 if (!client)
662 goto out;
6a9ee8af
DA
663
664 vgasr_priv.delayed_switch_active = false;
851ab954
DA
665
666 if (just_mux) {
667 ret = vgasr_priv.handler->switchto(client_id);
668 goto out;
669 }
670
79721e0a 671 if (client->active)
a67b8887
FM
672 goto out;
673
6a9ee8af 674 /* okay we want a switch - test if devices are willing to switch */
79721e0a 675 can_switch = check_can_switch();
6a9ee8af
DA
676
677 if (can_switch == false && delay == false)
678 goto out;
679
79721e0a 680 if (can_switch) {
66b37c67
DA
681 ret = vga_switchto_stage1(client);
682 if (ret)
9b0be1eb 683 pr_err("switching failed stage 1 %d\n", ret);
66b37c67
DA
684
685 ret = vga_switchto_stage2(client);
6a9ee8af 686 if (ret)
9b0be1eb 687 pr_err("switching failed stage 2 %d\n", ret);
66b37c67 688
6a9ee8af 689 } else {
9b0be1eb 690 pr_info("setting delayed switch to client %d\n", client->id);
6a9ee8af
DA
691 vgasr_priv.delayed_switch_active = true;
692 vgasr_priv.delayed_client_id = client_id;
693
66b37c67
DA
694 ret = vga_switchto_stage1(client);
695 if (ret)
9b0be1eb 696 pr_err("delayed switching stage 1 failed %d\n", ret);
6a9ee8af
DA
697 }
698
699out:
700 mutex_unlock(&vgasr_mutex);
701 return cnt;
702}
703
704static const struct file_operations vga_switcheroo_debugfs_fops = {
705 .owner = THIS_MODULE,
706 .open = vga_switcheroo_debugfs_open,
707 .write = vga_switcheroo_debugfs_write,
708 .read = seq_read,
709 .llseek = seq_lseek,
710 .release = single_release,
711};
712
713static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
714{
798ae0f6
TR
715 debugfs_remove(priv->switch_file);
716 priv->switch_file = NULL;
717
718 debugfs_remove(priv->debugfs_root);
719 priv->debugfs_root = NULL;
6a9ee8af
DA
720}
721
722static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
723{
9b0be1eb
TR
724 static const char mp[] = "/sys/kernel/debug";
725
6a9ee8af
DA
726 /* already initialised */
727 if (priv->debugfs_root)
728 return 0;
729 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
730
731 if (!priv->debugfs_root) {
9b0be1eb 732 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
6a9ee8af
DA
733 goto fail;
734 }
735
736 priv->switch_file = debugfs_create_file("switch", 0644,
7491bfb4
TR
737 priv->debugfs_root, NULL,
738 &vga_switcheroo_debugfs_fops);
6a9ee8af 739 if (!priv->switch_file) {
9b0be1eb 740 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
6a9ee8af
DA
741 goto fail;
742 }
743 return 0;
744fail:
745 vga_switcheroo_debugfs_fini(priv);
746 return -1;
747}
748
a645654b
LW
749/**
750 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
751 *
752 * Process a delayed switch if one is pending. DRM drivers should call this
753 * from their ->lastclose callback.
754 *
755 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
756 * has unregistered in the meantime or if there are other clients blocking the
757 * switch. If the actual switch fails, an error is reported and 0 is returned.
758 */
6a9ee8af
DA
759int vga_switcheroo_process_delayed_switch(void)
760{
79721e0a 761 struct vga_switcheroo_client *client;
6a9ee8af
DA
762 int ret;
763 int err = -EINVAL;
764
765 mutex_lock(&vgasr_mutex);
766 if (!vgasr_priv.delayed_switch_active)
767 goto err;
768
9b0be1eb
TR
769 pr_info("processing delayed switch to %d\n",
770 vgasr_priv.delayed_client_id);
6a9ee8af 771
79721e0a
TI
772 client = find_client_from_id(&vgasr_priv.clients,
773 vgasr_priv.delayed_client_id);
774 if (!client || !check_can_switch())
6a9ee8af
DA
775 goto err;
776
66b37c67 777 ret = vga_switchto_stage2(client);
6a9ee8af 778 if (ret)
9b0be1eb 779 pr_err("delayed switching failed stage 2 %d\n", ret);
6a9ee8af
DA
780
781 vgasr_priv.delayed_switch_active = false;
782 err = 0;
783err:
784 mutex_unlock(&vgasr_mutex);
785 return err;
786}
787EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
0d69704a 788
a645654b
LW
789/**
790 * DOC: Driver power control
791 *
792 * In this mode of use, the discrete GPU automatically powers up and down at
793 * the discretion of the driver's runtime pm. On muxed machines, the user may
794 * still influence the muxer state by way of the debugfs interface, however
795 * the ON and OFF commands become a no-op for the discrete GPU.
796 *
797 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
798 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
799 * command line disables it.
800 *
801 * When the driver decides to power up or down, it notifies vga_switcheroo
802 * thereof so that it can (a) power the audio device on the GPU up or down,
803 * and (b) update its internal power state representation for the device.
804 * This is achieved by vga_switcheroo_set_dynamic_switch().
805 *
806 * After the GPU has been suspended, the handler needs to be called to cut
807 * power to the GPU. Likewise it needs to reinstate power before the GPU
808 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
809 * which augments the GPU's suspend/resume functions by the requisite
810 * calls to the handler.
811 *
812 * When the audio device resumes, the GPU needs to be woken. This is achieved
813 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
814 * audio device's resume function.
815 *
816 * On muxed machines, if the mux is initially switched to the discrete GPU,
817 * the user ends up with a black screen when the GPU powers down after boot.
818 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
819 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
820 */
821
7491bfb4
TR
822static void vga_switcheroo_power_switch(struct pci_dev *pdev,
823 enum vga_switcheroo_state state)
0d69704a
DA
824{
825 struct vga_switcheroo_client *client;
826
827 if (!vgasr_priv.handler->power_state)
828 return;
829
830 client = find_client_from_pci(&vgasr_priv.clients, pdev);
831 if (!client)
832 return;
833
834 if (!client->driver_power_control)
835 return;
836
837 vgasr_priv.handler->power_state(client->id, state);
838}
839
a645654b
LW
840/**
841 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
842 * @pdev: client pci device
843 * @dynamic: new power state
844 *
845 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
846 * When the driver decides to power up or down, it notifies vga_switcheroo
847 * thereof using this helper so that it can (a) power the audio device on
848 * the GPU up or down, and (b) update its internal power state representation
849 * for the device.
850 */
7491bfb4
TR
851void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
852 enum vga_switcheroo_state dynamic)
0d69704a
DA
853{
854 struct vga_switcheroo_client *client;
855
8f12a311 856 mutex_lock(&vgasr_mutex);
0d69704a 857 client = find_client_from_pci(&vgasr_priv.clients, pdev);
8f12a311
LW
858 if (!client || !client->driver_power_control) {
859 mutex_unlock(&vgasr_mutex);
0d69704a 860 return;
8f12a311 861 }
0d69704a
DA
862
863 client->pwr_state = dynamic;
864 set_audio_state(client->id, dynamic);
8f12a311 865 mutex_unlock(&vgasr_mutex);
0d69704a
DA
866}
867EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
868
869/* switcheroo power domain */
870static int vga_switcheroo_runtime_suspend(struct device *dev)
871{
872 struct pci_dev *pdev = to_pci_dev(dev);
873 int ret;
874
875 ret = dev->bus->pm->runtime_suspend(dev);
876 if (ret)
877 return ret;
8f12a311 878 mutex_lock(&vgasr_mutex);
f2bc5616
AD
879 if (vgasr_priv.handler->switchto)
880 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
0d69704a 881 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
8f12a311 882 mutex_unlock(&vgasr_mutex);
0d69704a
DA
883 return 0;
884}
885
886static int vga_switcheroo_runtime_resume(struct device *dev)
887{
888 struct pci_dev *pdev = to_pci_dev(dev);
889 int ret;
890
8f12a311 891 mutex_lock(&vgasr_mutex);
0d69704a 892 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
8f12a311 893 mutex_unlock(&vgasr_mutex);
0d69704a
DA
894 ret = dev->bus->pm->runtime_resume(dev);
895 if (ret)
896 return ret;
897
898 return 0;
899}
900
a645654b
LW
901/**
902 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
903 * @dev: vga client device
904 * @domain: power domain
905 *
906 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
907 * After the GPU has been suspended, the handler needs to be called to cut
908 * power to the GPU. Likewise it needs to reinstate power before the GPU
909 * can resume. To this end, this helper augments the suspend/resume functions
910 * by the requisite calls to the handler. It needs only be called on platforms
911 * where the power switch is separate to the device being powered down.
912 */
7491bfb4
TR
913int vga_switcheroo_init_domain_pm_ops(struct device *dev,
914 struct dev_pm_domain *domain)
0d69704a
DA
915{
916 /* copy over all the bus versions */
917 if (dev->bus && dev->bus->pm) {
918 domain->ops = *dev->bus->pm;
919 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
920 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
921
989561de 922 dev_pm_domain_set(dev, domain);
0d69704a
DA
923 return 0;
924 }
989561de 925 dev_pm_domain_set(dev, NULL);
0d69704a
DA
926 return -EINVAL;
927}
928EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
929
766a53d0
AD
930void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
931{
989561de 932 dev_pm_domain_set(dev, NULL);
766a53d0
AD
933}
934EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
935
0d69704a
DA
936static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
937{
938 struct pci_dev *pdev = to_pci_dev(dev);
8f12a311
LW
939 struct vga_switcheroo_client *client;
940 struct device *video_dev = NULL;
0d69704a 941 int ret;
0d69704a
DA
942
943 /* we need to check if we have to switch back on the video
944 device so the audio device can come back */
8f12a311 945 mutex_lock(&vgasr_mutex);
0d69704a 946 list_for_each_entry(client, &vgasr_priv.clients, list) {
7491bfb4
TR
947 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
948 client_is_vga(client)) {
8f12a311 949 video_dev = &client->pdev->dev;
0d69704a
DA
950 break;
951 }
952 }
8f12a311
LW
953 mutex_unlock(&vgasr_mutex);
954
955 if (video_dev) {
956 ret = pm_runtime_get_sync(video_dev);
957 if (ret && ret != 1)
958 return ret;
959 }
0d69704a
DA
960 ret = dev->bus->pm->runtime_resume(dev);
961
962 /* put the reference for the gpu */
8f12a311
LW
963 if (video_dev) {
964 pm_runtime_mark_last_busy(video_dev);
965 pm_runtime_put_autosuspend(video_dev);
0d69704a
DA
966 }
967 return ret;
968}
969
a645654b
LW
970/**
971 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
972 * power control
973 * @dev: audio client device
974 * @domain: power domain
975 *
976 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
977 * When the audio device resumes, the GPU needs to be woken. This helper
978 * augments the audio device's resume function to do that.
979 *
980 * Return: 0 on success, -EINVAL if no power management operations are
981 * defined for this device.
982 */
7491bfb4
TR
983int
984vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
985 struct dev_pm_domain *domain)
0d69704a
DA
986{
987 /* copy over all the bus versions */
988 if (dev->bus && dev->bus->pm) {
989 domain->ops = *dev->bus->pm;
7491bfb4
TR
990 domain->ops.runtime_resume =
991 vga_switcheroo_runtime_resume_hdmi_audio;
0d69704a 992
989561de 993 dev_pm_domain_set(dev, domain);
0d69704a
DA
994 return 0;
995 }
989561de 996 dev_pm_domain_set(dev, NULL);
0d69704a
DA
997 return -EINVAL;
998}
999EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);