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