2 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
4 * Copyright (c) 2010 Red Hat Inc.
5 * Author : Dave Airlie <airlied@redhat.com>
7 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
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:
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
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
31 #define pr_fmt(fmt) "vga_switcheroo: " fmt
33 #include <linux/console.h>
34 #include <linux/debugfs.h>
37 #include <linux/module.h>
38 #include <linux/pci.h>
39 #include <linux/pm_domain.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/seq_file.h>
42 #include <linux/uaccess.h>
43 #include <linux/vgaarb.h>
44 #include <linux/vga_switcheroo.h>
49 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
50 * These come in two flavors:
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.
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.
67 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
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
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
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()
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
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
101 struct vga_switcheroo_client {
102 struct pci_dev *pdev;
103 struct fb_info *fb_info;
104 enum vga_switcheroo_state pwr_state;
105 const struct vga_switcheroo_client_ops *ops;
106 enum vga_switcheroo_client_id id;
108 bool driver_power_control;
109 struct list_head list;
113 * protects access to struct vgasr_priv
115 static DEFINE_MUTEX(vgasr_mutex);
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
130 * vga_switcheroo private data. Currently only one vga_switcheroo instance
131 * per system is supported.
135 bool delayed_switch_active;
136 enum vga_switcheroo_client_id delayed_client_id;
138 struct dentry *debugfs_root;
139 struct dentry *switch_file;
141 int registered_clients;
142 struct list_head clients;
144 const struct vga_switcheroo_handler *handler;
147 #define ID_BIT_AUDIO 0x100
148 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
149 #define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
151 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
153 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
154 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
156 /* only one switcheroo per system */
157 static struct vgasr_priv vgasr_priv = {
158 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
161 static bool vga_switcheroo_ready(void)
163 /* we're ready if we get two clients + handler */
164 return !vgasr_priv.active &&
165 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
168 static void vga_switcheroo_enable(void)
171 struct vga_switcheroo_client *client;
173 /* call the handler to init */
174 if (vgasr_priv.handler->init)
175 vgasr_priv.handler->init();
177 list_for_each_entry(client, &vgasr_priv.clients, list) {
178 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
180 ret = vgasr_priv.handler->get_client_id(client->pdev);
186 vga_switcheroo_debugfs_init(&vgasr_priv);
187 vgasr_priv.active = true;
191 * vga_switcheroo_register_handler() - register handler
192 * @handler: handler callbacks
194 * Register handler. Enable vga_switcheroo if two vga clients have already
197 * Return: 0 on success, -EINVAL if a handler was already registered.
199 int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler)
201 mutex_lock(&vgasr_mutex);
202 if (vgasr_priv.handler) {
203 mutex_unlock(&vgasr_mutex);
207 vgasr_priv.handler = handler;
208 if (vga_switcheroo_ready()) {
209 pr_info("enabled\n");
210 vga_switcheroo_enable();
212 mutex_unlock(&vgasr_mutex);
215 EXPORT_SYMBOL(vga_switcheroo_register_handler);
218 * vga_switcheroo_unregister_handler() - unregister handler
220 * Unregister handler. Disable vga_switcheroo.
222 void vga_switcheroo_unregister_handler(void)
224 mutex_lock(&vgasr_mutex);
225 vgasr_priv.handler = NULL;
226 if (vgasr_priv.active) {
227 pr_info("disabled\n");
228 vga_switcheroo_debugfs_fini(&vgasr_priv);
229 vgasr_priv.active = false;
231 mutex_unlock(&vgasr_mutex);
233 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
235 static int register_client(struct pci_dev *pdev,
236 const struct vga_switcheroo_client_ops *ops,
237 enum vga_switcheroo_client_id id, bool active,
238 bool driver_power_control)
240 struct vga_switcheroo_client *client;
242 client = kzalloc(sizeof(*client), GFP_KERNEL);
246 client->pwr_state = VGA_SWITCHEROO_ON;
250 client->active = active;
251 client->driver_power_control = driver_power_control;
253 mutex_lock(&vgasr_mutex);
254 list_add_tail(&client->list, &vgasr_priv.clients);
255 if (client_is_vga(client))
256 vgasr_priv.registered_clients++;
258 if (vga_switcheroo_ready()) {
259 pr_info("enabled\n");
260 vga_switcheroo_enable();
262 mutex_unlock(&vgasr_mutex);
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
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
277 * Return: 0 on success, -ENOMEM on memory allocation error.
279 int vga_switcheroo_register_client(struct pci_dev *pdev,
280 const struct vga_switcheroo_client_ops *ops,
281 bool driver_power_control)
283 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
284 pdev == vga_default_device(),
285 driver_power_control);
287 EXPORT_SYMBOL(vga_switcheroo_register_client);
290 * vga_switcheroo_register_audio_client - register audio client
291 * @pdev: client pci device
292 * @ops: client callbacks
293 * @id: client identifier
295 * Register audio client (audio device on a GPU). The power state of the
296 * client is assumed to be ON.
298 * Return: 0 on success, -ENOMEM on memory allocation error.
300 int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
301 const struct vga_switcheroo_client_ops *ops,
302 enum vga_switcheroo_client_id id)
304 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
306 EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
308 static struct vga_switcheroo_client *
309 find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
311 struct vga_switcheroo_client *client;
313 list_for_each_entry(client, head, list)
314 if (client->pdev == pdev)
319 static struct vga_switcheroo_client *
320 find_client_from_id(struct list_head *head,
321 enum vga_switcheroo_client_id client_id)
323 struct vga_switcheroo_client *client;
325 list_for_each_entry(client, head, list)
326 if (client->id == client_id)
331 static struct vga_switcheroo_client *
332 find_active_client(struct list_head *head)
334 struct vga_switcheroo_client *client;
336 list_for_each_entry(client, head, list)
343 * vga_switcheroo_get_client_state() - obtain power state of a given client
344 * @pdev: client pci device
346 * Obtain power state of a given client as seen from vga_switcheroo.
347 * The function is only called from hda_intel.c.
349 * Return: Power state.
351 enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
353 struct vga_switcheroo_client *client;
354 enum vga_switcheroo_state ret;
356 mutex_lock(&vgasr_mutex);
357 client = find_client_from_pci(&vgasr_priv.clients, pdev);
359 ret = VGA_SWITCHEROO_NOT_FOUND;
361 ret = client->pwr_state;
362 mutex_unlock(&vgasr_mutex);
365 EXPORT_SYMBOL(vga_switcheroo_get_client_state);
368 * vga_switcheroo_unregister_client() - unregister client
369 * @pdev: client pci device
371 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
373 void vga_switcheroo_unregister_client(struct pci_dev *pdev)
375 struct vga_switcheroo_client *client;
377 mutex_lock(&vgasr_mutex);
378 client = find_client_from_pci(&vgasr_priv.clients, pdev);
380 if (client_is_vga(client))
381 vgasr_priv.registered_clients--;
382 list_del(&client->list);
385 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
386 pr_info("disabled\n");
387 vga_switcheroo_debugfs_fini(&vgasr_priv);
388 vgasr_priv.active = false;
390 mutex_unlock(&vgasr_mutex);
392 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
395 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
396 * @pdev: client pci device
399 * Set framebuffer of a given client. The console will be remapped to this
402 void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
403 struct fb_info *info)
405 struct vga_switcheroo_client *client;
407 mutex_lock(&vgasr_mutex);
408 client = find_client_from_pci(&vgasr_priv.clients, pdev);
410 client->fb_info = info;
411 mutex_unlock(&vgasr_mutex);
413 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
416 * DOC: Manual switching and manual power control
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:
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.
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).
447 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
448 * should not be used.
451 static int vga_switcheroo_show(struct seq_file *m, void *v)
453 struct vga_switcheroo_client *client;
456 mutex_lock(&vgasr_mutex);
457 list_for_each_entry(client, &vgasr_priv.clients, list) {
458 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
459 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
461 client_is_vga(client) ? "" : "-Audio",
462 client->active ? '+' : ' ',
463 client->driver_power_control ? "Dyn" : "",
464 client->pwr_state ? "Pwr" : "Off",
465 pci_name(client->pdev));
468 mutex_unlock(&vgasr_mutex);
472 static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
474 return single_open(file, vga_switcheroo_show, NULL);
477 static int vga_switchon(struct vga_switcheroo_client *client)
479 if (client->driver_power_control)
481 if (vgasr_priv.handler->power_state)
482 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
483 /* call the driver callback to turn on device */
484 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
485 client->pwr_state = VGA_SWITCHEROO_ON;
489 static int vga_switchoff(struct vga_switcheroo_client *client)
491 if (client->driver_power_control)
493 /* call the driver callback to turn off device */
494 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
495 if (vgasr_priv.handler->power_state)
496 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
497 client->pwr_state = VGA_SWITCHEROO_OFF;
501 static void set_audio_state(enum vga_switcheroo_client_id id,
502 enum vga_switcheroo_state state)
504 struct vga_switcheroo_client *client;
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;
513 /* stage one happens before delay */
514 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
516 struct vga_switcheroo_client *active;
518 active = find_active_client(&vgasr_priv.clients);
522 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
523 vga_switchon(new_client);
525 vga_set_default_device(new_client->pdev);
530 static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
533 struct vga_switcheroo_client *active;
535 active = find_active_client(&vgasr_priv.clients);
539 active->active = false;
541 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
543 if (new_client->fb_info) {
544 struct fb_event event;
547 event.info = new_client->fb_info;
548 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
552 ret = vgasr_priv.handler->switchto(new_client->id);
556 if (new_client->ops->reprobe)
557 new_client->ops->reprobe(new_client->pdev);
559 if (active->pwr_state == VGA_SWITCHEROO_ON)
560 vga_switchoff(active);
562 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
564 new_client->active = true;
568 static bool check_can_switch(void)
570 struct vga_switcheroo_client *client;
572 list_for_each_entry(client, &vgasr_priv.clients, list) {
573 if (!client->ops->can_switch(client->pdev)) {
574 pr_err("client %x refused switch\n", client->id);
582 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
583 size_t cnt, loff_t *ppos)
587 bool delay = false, can_switch;
588 bool just_mux = false;
589 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
590 struct vga_switcheroo_client *client = NULL;
595 if (copy_from_user(usercmd, ubuf, cnt))
598 mutex_lock(&vgasr_mutex);
600 if (!vgasr_priv.active) {
605 /* pwr off the device not in use */
606 if (strncmp(usercmd, "OFF", 3) == 0) {
607 list_for_each_entry(client, &vgasr_priv.clients, list) {
608 if (client->active || client_is_audio(client))
610 if (client->driver_power_control)
612 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
613 if (client->pwr_state == VGA_SWITCHEROO_ON)
614 vga_switchoff(client);
618 /* pwr on the device not in use */
619 if (strncmp(usercmd, "ON", 2) == 0) {
620 list_for_each_entry(client, &vgasr_priv.clients, list) {
621 if (client->active || client_is_audio(client))
623 if (client->driver_power_control)
625 if (client->pwr_state == VGA_SWITCHEROO_OFF)
626 vga_switchon(client);
627 set_audio_state(client->id, VGA_SWITCHEROO_ON);
632 /* request a delayed switch - test can we switch now */
633 if (strncmp(usercmd, "DIGD", 4) == 0) {
634 client_id = VGA_SWITCHEROO_IGD;
638 if (strncmp(usercmd, "DDIS", 4) == 0) {
639 client_id = VGA_SWITCHEROO_DIS;
643 if (strncmp(usercmd, "IGD", 3) == 0)
644 client_id = VGA_SWITCHEROO_IGD;
646 if (strncmp(usercmd, "DIS", 3) == 0)
647 client_id = VGA_SWITCHEROO_DIS;
649 if (strncmp(usercmd, "MIGD", 4) == 0) {
651 client_id = VGA_SWITCHEROO_IGD;
653 if (strncmp(usercmd, "MDIS", 4) == 0) {
655 client_id = VGA_SWITCHEROO_DIS;
658 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
660 client = find_client_from_id(&vgasr_priv.clients, client_id);
664 vgasr_priv.delayed_switch_active = false;
667 ret = vgasr_priv.handler->switchto(client_id);
674 /* okay we want a switch - test if devices are willing to switch */
675 can_switch = check_can_switch();
677 if (can_switch == false && delay == false)
681 ret = vga_switchto_stage1(client);
683 pr_err("switching failed stage 1 %d\n", ret);
685 ret = vga_switchto_stage2(client);
687 pr_err("switching failed stage 2 %d\n", ret);
690 pr_info("setting delayed switch to client %d\n", client->id);
691 vgasr_priv.delayed_switch_active = true;
692 vgasr_priv.delayed_client_id = client_id;
694 ret = vga_switchto_stage1(client);
696 pr_err("delayed switching stage 1 failed %d\n", ret);
700 mutex_unlock(&vgasr_mutex);
704 static const struct file_operations vga_switcheroo_debugfs_fops = {
705 .owner = THIS_MODULE,
706 .open = vga_switcheroo_debugfs_open,
707 .write = vga_switcheroo_debugfs_write,
710 .release = single_release,
713 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
715 debugfs_remove(priv->switch_file);
716 priv->switch_file = NULL;
718 debugfs_remove(priv->debugfs_root);
719 priv->debugfs_root = NULL;
722 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
724 static const char mp[] = "/sys/kernel/debug";
726 /* already initialised */
727 if (priv->debugfs_root)
729 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
731 if (!priv->debugfs_root) {
732 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
736 priv->switch_file = debugfs_create_file("switch", 0644,
737 priv->debugfs_root, NULL,
738 &vga_switcheroo_debugfs_fops);
739 if (!priv->switch_file) {
740 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
745 vga_switcheroo_debugfs_fini(priv);
750 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
752 * Process a delayed switch if one is pending. DRM drivers should call this
753 * from their ->lastclose callback.
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.
759 int vga_switcheroo_process_delayed_switch(void)
761 struct vga_switcheroo_client *client;
765 mutex_lock(&vgasr_mutex);
766 if (!vgasr_priv.delayed_switch_active)
769 pr_info("processing delayed switch to %d\n",
770 vgasr_priv.delayed_client_id);
772 client = find_client_from_id(&vgasr_priv.clients,
773 vgasr_priv.delayed_client_id);
774 if (!client || !check_can_switch())
777 ret = vga_switchto_stage2(client);
779 pr_err("delayed switching failed stage 2 %d\n", ret);
781 vgasr_priv.delayed_switch_active = false;
784 mutex_unlock(&vgasr_mutex);
787 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
790 * DOC: Driver power control
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.
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.
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().
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.
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.
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
822 static void vga_switcheroo_power_switch(struct pci_dev *pdev,
823 enum vga_switcheroo_state state)
825 struct vga_switcheroo_client *client;
827 if (!vgasr_priv.handler->power_state)
830 client = find_client_from_pci(&vgasr_priv.clients, pdev);
834 if (!client->driver_power_control)
837 vgasr_priv.handler->power_state(client->id, state);
841 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
842 * @pdev: client pci device
843 * @dynamic: new power state
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
851 void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
852 enum vga_switcheroo_state dynamic)
854 struct vga_switcheroo_client *client;
856 mutex_lock(&vgasr_mutex);
857 client = find_client_from_pci(&vgasr_priv.clients, pdev);
858 if (!client || !client->driver_power_control) {
859 mutex_unlock(&vgasr_mutex);
863 client->pwr_state = dynamic;
864 set_audio_state(client->id, dynamic);
865 mutex_unlock(&vgasr_mutex);
867 EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
869 /* switcheroo power domain */
870 static int vga_switcheroo_runtime_suspend(struct device *dev)
872 struct pci_dev *pdev = to_pci_dev(dev);
875 ret = dev->bus->pm->runtime_suspend(dev);
878 mutex_lock(&vgasr_mutex);
879 if (vgasr_priv.handler->switchto)
880 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
881 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
882 mutex_unlock(&vgasr_mutex);
886 static int vga_switcheroo_runtime_resume(struct device *dev)
888 struct pci_dev *pdev = to_pci_dev(dev);
891 mutex_lock(&vgasr_mutex);
892 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
893 mutex_unlock(&vgasr_mutex);
894 ret = dev->bus->pm->runtime_resume(dev);
902 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
903 * @dev: vga client device
904 * @domain: power domain
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.
913 int vga_switcheroo_init_domain_pm_ops(struct device *dev,
914 struct dev_pm_domain *domain)
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;
922 dev_pm_domain_set(dev, domain);
925 dev_pm_domain_set(dev, NULL);
928 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
930 void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
932 dev_pm_domain_set(dev, NULL);
934 EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
936 static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
938 struct pci_dev *pdev = to_pci_dev(dev);
939 struct vga_switcheroo_client *client;
940 struct device *video_dev = NULL;
943 /* we need to check if we have to switch back on the video
944 device so the audio device can come back */
945 mutex_lock(&vgasr_mutex);
946 list_for_each_entry(client, &vgasr_priv.clients, list) {
947 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
948 client_is_vga(client)) {
949 video_dev = &client->pdev->dev;
953 mutex_unlock(&vgasr_mutex);
956 ret = pm_runtime_get_sync(video_dev);
960 ret = dev->bus->pm->runtime_resume(dev);
962 /* put the reference for the gpu */
964 pm_runtime_mark_last_busy(video_dev);
965 pm_runtime_put_autosuspend(video_dev);
971 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
973 * @dev: audio client device
974 * @domain: power domain
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.
980 * Return: 0 on success, -EINVAL if no power management operations are
981 * defined for this device.
984 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
985 struct dev_pm_domain *domain)
987 /* copy over all the bus versions */
988 if (dev->bus && dev->bus->pm) {
989 domain->ops = *dev->bus->pm;
990 domain->ops.runtime_resume =
991 vga_switcheroo_runtime_resume_hdmi_audio;
993 dev_pm_domain_set(dev, domain);
996 dev_pm_domain_set(dev, NULL);
999 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);