2 * vga_switcheroo.c -- vga_switcheroo driver for DragonFly
4 * Adapted from linux v4.8: linux-src/drivers/gpu/vga/vga_switcheroo.c
9 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
11 * Copyright (c) 2010 Red Hat Inc.
12 * Author : Dave Airlie <airlied@redhat.com>
14 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
38 #include <sys/param.h>
39 #include <sys/kernel.h>
42 #include <sys/malloc.h>
43 #include <sys/module.h>
45 #include <sys/types.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
52 #define pr_fmt(fmt) "vga_switcheroo: " fmt
56 #include <linux/module.h>
57 #include <linux/pci.h>
58 #include <linux/pci_ids.h>
59 #include <linux/mutex.h>
60 #include <linux/list.h>
61 #include <linux/export.h>
63 #define VGA_SWITCHEROO
64 #include <linux/vga_switcheroo.h>
69 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
70 * These come in two flavors:
72 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
73 * * muxless: Dual GPUs but only one of them is connected to outputs.
74 * The other one is merely used to offload rendering, its results
75 * are copied over PCIe into the framebuffer. On Linux this is
76 * supported with DRI PRIME.
78 * Hybrid graphics started to appear in the late Naughties and were initially
79 * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
80 * A notable exception is the MacBook Pro which continues to use a mux.
81 * Muxes come with varying capabilities: Some switch only the panel, others
82 * can also switch external displays. Some switch all display pins at once
83 * while others can switch just the DDC lines. (To allow EDID probing
84 * for the inactive GPU.) Also, muxes are often used to cut power to the
85 * discrete GPU while it is not used.
87 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
88 * clients. The mux is called the handler. Muxless machines also register a
89 * handler to control the power state of the discrete GPU, its ->switchto
90 * callback is a no-op for obvious reasons. The discrete GPU is often equipped
91 * with an HDA controller for the HDMI/DP audio signal, this will also
92 * register as a client so that vga_switcheroo can take care of the correct
93 * suspend/resume order when changing the discrete GPU's power state. In total
94 * there can thus be up to three clients: Two vga clients (GPUs) and one audio
95 * client (on the discrete GPU). The code is mostly prepared to support
96 * machines with more than two GPUs should they become available.
98 * The GPU to which the outputs are currently switched is called the
99 * active client in vga_switcheroo parlance. The GPU not in use is the
100 * inactive client. When the inactive client's DRM driver is loaded,
101 * it will be unable to probe the panel's EDID and hence depends on
102 * VBIOS to provide its display modes. If the VBIOS modes are bogus or
103 * if there is no VBIOS at all (which is common on the MacBook Pro),
104 * a client may alternatively request that the DDC lines are temporarily
105 * switched to it, provided that the handler supports this. Switching
106 * only the DDC lines and not the entire output avoids unnecessary
111 * struct vga_switcheroo_client - registered client
112 * @pdev: client pci device
113 * @fb_info: framebuffer to which console is remapped on switching
114 * @pwr_state: current power state
115 * @ops: client callbacks
116 * @id: client identifier. Determining the id requires the handler,
117 * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
118 * and later given their true id in vga_switcheroo_enable()
119 * @active: whether the outputs are currently switched to this client
120 * @driver_power_control: whether power state is controlled by the driver's
121 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
122 * interface is a no-op so as not to interfere with runtime pm
125 * Registered client. A client can be either a GPU or an audio device on a GPU.
126 * For audio clients, the @fb_info, @active and @driver_power_control members
129 struct vga_switcheroo_client {
130 struct pci_dev *pdev;
131 struct fb_info *fb_info;
132 enum vga_switcheroo_state pwr_state;
133 const struct vga_switcheroo_client_ops *ops;
134 enum vga_switcheroo_client_id id;
136 bool driver_power_control;
137 struct list_head list;
141 * protects access to struct vgasr_priv
143 static DEFINE_MUTEX(vgasr_mutex);
146 * struct vgasr_priv - vga_switcheroo private data
147 * @active: whether vga_switcheroo is enabled.
148 * Prerequisite is the registration of two GPUs and a handler
149 * @delayed_switch_active: whether a delayed switch is pending
150 * @delayed_client_id: client to which a delayed switch is pending
151 * @debugfs_root: directory for vga_switcheroo debugfs interface
152 * @switch_file: file for vga_switcheroo debugfs interface
153 * @registered_clients: number of registered GPUs
154 * (counting only vga clients, not audio clients)
155 * @clients: list of registered clients
156 * @handler: registered handler
157 * @handler_flags: flags of registered handler
158 * @mux_hw_lock: protects mux state
159 * (in particular while DDC lines are temporarily switched)
160 * @old_ddc_owner: client to which DDC lines will be switched back on unlock
162 * vga_switcheroo private data. Currently only one vga_switcheroo instance
163 * per system is supported.
167 bool delayed_switch_active;
168 enum vga_switcheroo_client_id delayed_client_id;
170 struct dentry *debugfs_root;
171 struct dentry *switch_file;
173 int registered_clients;
174 struct list_head clients;
176 const struct vga_switcheroo_handler *handler;
177 enum vga_switcheroo_handler_flags_t handler_flags;
178 struct lock mux_hw_lk;
182 #define ID_BIT_AUDIO 0x100
183 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
184 #define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
186 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
189 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
190 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
193 /* only one switcheroo per system */
194 static struct vgasr_priv *vgasr_priv;
197 vga_switcheroo_ready(void)
199 /* we're ready if we get two clients + handler */
200 return !vgasr_priv->active &&
201 vgasr_priv->registered_clients == 2 && vgasr_priv->handler;
205 vga_switcheroo_enable(void)
208 struct vga_switcheroo_client *client;
210 /* call the handler to init */
211 if (vgasr_priv->handler->init)
212 vgasr_priv->handler->init();
214 list_for_each_entry(client, &vgasr_priv->clients, list) {
215 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
217 ret = vgasr_priv->handler->get_client_id(client->pdev);
224 //vga_switcheroo_debugfs_init(&vgasr_priv);
225 vgasr_priv->active = true;
229 * vga_switcheroo_register_handler() - register handler
230 * @handler: handler callbacks
231 * @handler_flags: handler flags
233 * Register handler. Enable vga_switcheroo if two vga clients have already
236 * Return: 0 on success, -EINVAL if a handler was already registered.
239 vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
240 enum vga_switcheroo_handler_flags_t handler_flags)
242 mutex_lock(&vgasr_mutex);
243 if (vgasr_priv->handler) {
244 mutex_unlock(&vgasr_mutex);
248 vgasr_priv->handler = handler;
249 vgasr_priv->handler_flags = handler_flags;
250 if (vga_switcheroo_ready()) {
251 pr_info("enabled\n");
252 vga_switcheroo_enable();
254 mutex_unlock(&vgasr_mutex);
257 EXPORT_SYMBOL(vga_switcheroo_register_handler);
260 * vga_switcheroo_unregister_handler() - unregister handler
262 * Unregister handler. Disable vga_switcheroo.
265 vga_switcheroo_unregister_handler(void)
267 mutex_lock(&vgasr_mutex);
268 mutex_lock(&vgasr_priv->mux_hw_lk);
269 vgasr_priv->handler_flags = 0;
270 vgasr_priv->handler = NULL;
271 if (vgasr_priv->active) {
272 pr_info("disabled\n");
274 //vga_switcheroo_debugfs_fini(&vgasr_priv);
275 vgasr_priv->active = false;
277 mutex_unlock(&vgasr_priv->mux_hw_lk);
278 mutex_unlock(&vgasr_mutex);
280 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
283 * vga_switcheroo_handler_flags() - obtain handler flags
285 * Helper for clients to obtain the handler flags bitmask.
287 * Return: Handler flags. A value of 0 means that no handler is registered
288 * or that the handler has no special capabilities.
290 enum vga_switcheroo_handler_flags_t
291 vga_switcheroo_handler_flags(void)
293 return vgasr_priv->handler_flags;
295 EXPORT_SYMBOL(vga_switcheroo_handler_flags);
297 MALLOC_DECLARE(M_VGA_SWITCHEROO_CLIENT);
298 MALLOC_DEFINE(M_VGA_SWITCHEROO_CLIENT, "vga_switcheroo_client", "vga_switcheroo client data");
301 register_client(struct pci_dev *pdev,
302 const struct vga_switcheroo_client_ops *ops,
303 enum vga_switcheroo_client_id id, bool active,
304 bool driver_power_control)
306 struct vga_switcheroo_client *client;
308 client = kmalloc(sizeof(*client), M_VGA_SWITCHEROO_CLIENT, M_WAITOK | M_ZERO);
312 client->pwr_state = VGA_SWITCHEROO_ON;
316 client->active = active;
317 client->driver_power_control = driver_power_control;
319 mutex_lock(&vgasr_mutex);
320 list_add_tail(&client->list, &vgasr_priv->clients);
321 if (client_is_vga(client))
322 vgasr_priv->registered_clients++;
324 if (vga_switcheroo_ready()) {
325 pr_info("enabled\n");
326 vga_switcheroo_enable();
328 mutex_unlock(&vgasr_mutex);
333 * vga_switcheroo_register_client - register vga client
334 * @pdev: client pci device
335 * @ops: client callbacks
336 * @driver_power_control: whether power state is controlled by the driver's
339 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
340 * handler have already registered. The power state of the client is assumed
343 * Return: 0 on success, -ENOMEM on memory allocation error.
346 vga_switcheroo_register_client(struct pci_dev *pdev,
347 const struct vga_switcheroo_client_ops *ops,
348 bool driver_power_control)
351 int unit, default_vgapci_unit;
355 * We need to find out if video device pci_dev is
356 * the active (default) video device: will determine
357 * if the pci_dev unit is the default_vgapci_unit
359 unit = device_get_unit(pdev->dev.bsddev);
361 len = sizeof(default_vgapci_unit);
362 error = kernel_sysctlbyname("hw.pci.default_vgapci_unit", &default_vgapci_unit, &len, NULL, 0, NULL);
364 pr_err("kernel_sysctlbyname: error %d\n", error);
368 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
369 unit == default_vgapci_unit,
370 driver_power_control);
372 EXPORT_SYMBOL(vga_switcheroo_register_client);
375 * vga_switcheroo_register_audio_client - register audio client
376 * @pdev: client pci device
377 * @ops: client callbacks
378 * @id: client identifier
380 * Register audio client (audio device on a GPU). The power state of the
381 * client is assumed to be ON.
383 * Return: 0 on success, -ENOMEM on memory allocation error.
386 vga_switcheroo_register_audio_client(struct pci_dev *pdev,
387 const struct vga_switcheroo_client_ops *ops,
388 enum vga_switcheroo_client_id id)
390 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
392 EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
394 static struct vga_switcheroo_client *
395 find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
397 struct vga_switcheroo_client *client;
399 list_for_each_entry(client, head, list)
400 if (client->pdev == pdev)
405 static struct vga_switcheroo_client *
406 find_client_from_id(struct list_head *head,
407 enum vga_switcheroo_client_id client_id)
409 struct vga_switcheroo_client *client;
411 list_for_each_entry(client, head, list)
412 if (client->id == client_id)
417 static struct vga_switcheroo_client *
418 find_active_client(struct list_head *head)
420 struct vga_switcheroo_client *client;
422 list_for_each_entry(client, head, list)
429 * vga_switcheroo_client_probe_defer() - whether to defer probing a given client
430 * @pdev: client pci device
432 * Determine whether any prerequisites are not fulfilled to probe a given
433 * client. Drivers shall invoke this early on in their ->probe callback
434 * and return %-EPROBE_DEFER if it evaluates to %true. Thou shalt not
435 * register the client ere thou hast called this.
437 * Return: %true if probing should be deferred, otherwise %false.
440 bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev)
442 if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
444 * apple-gmux is needed on pre-retina MacBook Pro
445 * to probe the panel if pdev is the inactive GPU.
447 if (apple_gmux_present() && pdev != vga_default_device() &&
448 !vgasr_priv.handler_flags)
454 EXPORT_SYMBOL(vga_switcheroo_client_probe_defer);
458 * vga_switcheroo_get_client_state() - obtain power state of a given client
459 * @pdev: client pci device
461 * Obtain power state of a given client as seen from vga_switcheroo.
462 * The function is only called from hda_intel.c.
464 * Return: Power state.
466 enum vga_switcheroo_state
467 vga_switcheroo_get_client_state(struct pci_dev *pdev)
469 struct vga_switcheroo_client *client;
470 enum vga_switcheroo_state ret;
472 mutex_lock(&vgasr_mutex);
473 client = find_client_from_pci(&vgasr_priv->clients, pdev);
475 ret = VGA_SWITCHEROO_NOT_FOUND;
477 ret = client->pwr_state;
478 mutex_unlock(&vgasr_mutex);
481 EXPORT_SYMBOL(vga_switcheroo_get_client_state);
484 * vga_switcheroo_unregister_client() - unregister client
485 * @pdev: client pci device
487 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
490 vga_switcheroo_unregister_client(struct pci_dev *pdev)
492 struct vga_switcheroo_client *client;
494 mutex_lock(&vgasr_mutex);
495 client = find_client_from_pci(&vgasr_priv->clients, pdev);
497 if (client_is_vga(client))
498 vgasr_priv->registered_clients--;
499 list_del(&client->list);
500 kfree(client, M_VGA_SWITCHEROO_CLIENT);
502 if (vgasr_priv->active && vgasr_priv->registered_clients < 2) {
503 pr_info("disabled\n");
505 //vga_switcheroo_debugfs_fini(&vgasr_priv);
506 vgasr_priv->active = false;
508 mutex_unlock(&vgasr_mutex);
510 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
513 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
514 * @pdev: client pci device
517 * Set framebuffer of a given client. The console will be remapped to this
521 vga_switcheroo_client_fb_set(struct pci_dev *pdev,
522 struct fb_info *info)
524 struct vga_switcheroo_client *client;
526 mutex_lock(&vgasr_mutex);
527 client = find_client_from_pci(&vgasr_priv->clients, pdev);
529 client->fb_info = info;
530 mutex_unlock(&vgasr_mutex);
532 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
535 * vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client
536 * @pdev: client pci device
538 * Temporarily switch DDC lines to the client identified by @pdev
539 * (but leave the outputs otherwise switched to where they are).
540 * This allows the inactive client to probe EDID. The DDC lines must
541 * afterwards be switched back by calling vga_switcheroo_unlock_ddc(),
542 * even if this function returns an error.
544 * Return: Previous DDC owner on success or a negative int on error.
545 * Specifically, %-ENODEV if no handler has registered or if the handler
546 * does not support switching the DDC lines. Also, a negative value
547 * returned by the handler is propagated back to the caller.
548 * The return value has merely an informational purpose for any caller
549 * which might be interested in it. It is acceptable to ignore the return
550 * value and simply rely on the result of the subsequent EDID probe,
551 * which will be %NULL if DDC switching failed.
554 vga_switcheroo_lock_ddc(struct pci_dev *pdev)
556 enum vga_switcheroo_client_id id;
558 mutex_lock(&vgasr_priv->mux_hw_lk);
559 if (!vgasr_priv->handler || !vgasr_priv->handler->switch_ddc) {
560 vgasr_priv->old_ddc_owner = -ENODEV;
564 id = vgasr_priv->handler->get_client_id(pdev);
565 vgasr_priv->old_ddc_owner = vgasr_priv->handler->switch_ddc(id);
566 return vgasr_priv->old_ddc_owner;
568 EXPORT_SYMBOL(vga_switcheroo_lock_ddc);
571 * vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner
572 * @pdev: client pci device
574 * Switch DDC lines back to the previous owner after calling
575 * vga_switcheroo_lock_ddc(). This must be called even if
576 * vga_switcheroo_lock_ddc() returned an error.
578 * Return: Previous DDC owner on success (i.e. the client identifier of @pdev)
579 * or a negative int on error.
580 * Specifically, %-ENODEV if no handler has registered or if the handler
581 * does not support switching the DDC lines. Also, a negative value
582 * returned by the handler is propagated back to the caller.
583 * Finally, invoking this function without calling vga_switcheroo_lock_ddc()
584 * first is not allowed and will result in %-EINVAL.
587 vga_switcheroo_unlock_ddc(struct pci_dev *pdev)
589 enum vga_switcheroo_client_id id;
590 int ret = vgasr_priv->old_ddc_owner;
592 if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv->mux_hw_lk)))
595 if (vgasr_priv->old_ddc_owner >= 0) {
596 id = vgasr_priv->handler->get_client_id(pdev);
597 if (vgasr_priv->old_ddc_owner != id)
598 ret = vgasr_priv->handler->switch_ddc(
599 vgasr_priv->old_ddc_owner);
601 mutex_unlock(&vgasr_priv->mux_hw_lk);
604 EXPORT_SYMBOL(vga_switcheroo_unlock_ddc);
607 * DOC: Manual switching and manual power control
609 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
610 * can be read to retrieve the current vga_switcheroo state and commands
611 * can be written to it to change the state. The file appears as soon as
612 * two GPU drivers and one handler have registered with vga_switcheroo.
613 * The following commands are understood:
615 * * OFF: Power off the device not in use.
616 * * ON: Power on the device not in use.
617 * * IGD: Switch to the integrated graphics device.
618 * Power on the integrated GPU if necessary, power off the discrete GPU.
619 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
620 * have opened device files of the GPUs or the audio client. If the
621 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
622 * and /dev/snd/controlC1 to identify processes blocking the switch.
623 * * DIS: Switch to the discrete graphics device.
624 * * DIGD: Delayed switch to the integrated graphics device.
625 * This will perform the switch once the last user space process has
626 * closed the device files of the GPUs and the audio client.
627 * * DDIS: Delayed switch to the discrete graphics device.
628 * * MIGD: Mux-only switch to the integrated graphics device.
629 * Does not remap console or change the power state of either gpu.
630 * If the integrated GPU is currently off, the screen will turn black.
631 * If it is on, the screen will show whatever happens to be in VRAM.
632 * Either way, the user has to blindly enter the command to switch back.
633 * * MDIS: Mux-only switch to the discrete graphics device.
635 * For GPUs whose power state is controlled by the driver's runtime pm,
636 * the ON and OFF commands are a no-op (see next section).
638 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
639 * should not be used.
642 #if 0 /* debugfs 1 */
644 vga_switcheroo_show(struct seq_file *m, void *v)
646 struct vga_switcheroo_client *client;
649 mutex_lock(&vgasr_mutex);
650 list_for_each_entry(client, &vgasr_priv.clients, list) {
651 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
652 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
654 client_is_vga(client) ? "" : "-Audio",
655 client->active ? '+' : ' ',
656 client->driver_power_control ? "Dyn" : "",
657 client->pwr_state ? "Pwr" : "Off",
658 pci_name(client->pdev));
661 mutex_unlock(&vgasr_mutex);
666 vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
668 return single_open(file, vga_switcheroo_show, NULL);
670 #endif /* debugfs 1 */
673 vga_switchon(struct vga_switcheroo_client *client)
675 if (client->driver_power_control)
677 if (vgasr_priv->handler->power_state)
678 vgasr_priv->handler->power_state(client->id, VGA_SWITCHEROO_ON);
679 /* call the driver callback to turn on device */
680 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
681 client->pwr_state = VGA_SWITCHEROO_ON;
686 vga_switchoff(struct vga_switcheroo_client *client)
688 if (client->driver_power_control)
690 /* call the driver callback to turn off device */
691 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
692 if (vgasr_priv->handler->power_state)
693 vgasr_priv->handler->power_state(client->id, VGA_SWITCHEROO_OFF);
694 client->pwr_state = VGA_SWITCHEROO_OFF;
699 set_audio_state(enum vga_switcheroo_client_id id,
700 enum vga_switcheroo_state state)
702 struct vga_switcheroo_client *client;
704 client = find_client_from_id(&vgasr_priv->clients, id | ID_BIT_AUDIO);
705 if (client && client->pwr_state != state) {
706 client->ops->set_gpu_state(client->pdev, state);
707 client->pwr_state = state;
711 /* stage one happens before delay */
713 vga_switchto_stage1(struct vga_switcheroo_client *new_client)
715 struct vga_switcheroo_client *active;
717 active = find_active_client(&vgasr_priv->clients);
721 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
722 vga_switchon(new_client);
725 //vga_set_default_device(new_client->pdev);
732 vga_switchto_stage2(struct vga_switcheroo_client *new_client)
735 struct vga_switcheroo_client *active;
737 active = find_active_client(&vgasr_priv->clients);
741 active->active = false;
743 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
745 /* XXX CRITICAL TODO: set new fb
747 if (new_client->fb_info) {
748 struct fb_event event;
751 event.info = new_client->fb_info;
752 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
757 mutex_lock(&vgasr_priv->mux_hw_lk);
758 ret = vgasr_priv->handler->switchto(new_client->id);
759 mutex_unlock(&vgasr_priv->mux_hw_lk);
763 if (new_client->ops->reprobe)
764 new_client->ops->reprobe(new_client->pdev);
766 if (active->pwr_state == VGA_SWITCHEROO_ON)
767 vga_switchoff(active);
769 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
771 new_client->active = true;
776 check_can_switch(void)
778 struct vga_switcheroo_client *client;
780 list_for_each_entry(client, &vgasr_priv->clients, list) {
781 if (!client->ops->can_switch(client->pdev)) {
782 pr_err("client %x refused switch\n", client->id);
789 #if 0 /* debugfs 2 */
791 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
792 size_t cnt, loff_t *ppos)
796 bool delay = false, can_switch;
797 bool just_mux = false;
798 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
799 struct vga_switcheroo_client *client = NULL;
804 if (copy_from_user(usercmd, ubuf, cnt))
807 mutex_lock(&vgasr_mutex);
809 if (!vgasr_priv.active) {
814 /* pwr off the device not in use */
815 if (strncmp(usercmd, "OFF", 3) == 0) {
816 list_for_each_entry(client, &vgasr_priv.clients, list) {
817 if (client->active || client_is_audio(client))
819 if (client->driver_power_control)
821 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
822 if (client->pwr_state == VGA_SWITCHEROO_ON)
823 vga_switchoff(client);
827 /* pwr on the device not in use */
828 if (strncmp(usercmd, "ON", 2) == 0) {
829 list_for_each_entry(client, &vgasr_priv.clients, list) {
830 if (client->active || client_is_audio(client))
832 if (client->driver_power_control)
834 if (client->pwr_state == VGA_SWITCHEROO_OFF)
835 vga_switchon(client);
836 set_audio_state(client->id, VGA_SWITCHEROO_ON);
841 /* request a delayed switch - test can we switch now */
842 if (strncmp(usercmd, "DIGD", 4) == 0) {
843 client_id = VGA_SWITCHEROO_IGD;
847 if (strncmp(usercmd, "DDIS", 4) == 0) {
848 client_id = VGA_SWITCHEROO_DIS;
852 if (strncmp(usercmd, "IGD", 3) == 0)
853 client_id = VGA_SWITCHEROO_IGD;
855 if (strncmp(usercmd, "DIS", 3) == 0)
856 client_id = VGA_SWITCHEROO_DIS;
858 if (strncmp(usercmd, "MIGD", 4) == 0) {
860 client_id = VGA_SWITCHEROO_IGD;
862 if (strncmp(usercmd, "MDIS", 4) == 0) {
864 client_id = VGA_SWITCHEROO_DIS;
867 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
869 client = find_client_from_id(&vgasr_priv.clients, client_id);
873 vgasr_priv.delayed_switch_active = false;
876 mutex_lock(&vgasr_priv.mux_hw_lock);
877 ret = vgasr_priv.handler->switchto(client_id);
878 mutex_unlock(&vgasr_priv.mux_hw_lock);
885 /* okay we want a switch - test if devices are willing to switch */
886 can_switch = check_can_switch();
888 if (can_switch == false && delay == false)
892 ret = vga_switchto_stage1(client);
894 pr_err("switching failed stage 1 %d\n", ret);
896 ret = vga_switchto_stage2(client);
898 pr_err("switching failed stage 2 %d\n", ret);
901 pr_info("setting delayed switch to client %d\n", client->id);
902 vgasr_priv.delayed_switch_active = true;
903 vgasr_priv.delayed_client_id = client_id;
905 ret = vga_switchto_stage1(client);
907 pr_err("delayed switching stage 1 failed %d\n", ret);
911 mutex_unlock(&vgasr_mutex);
915 #if 0 /* linux specific */
916 static const struct file_operations vga_switcheroo_debugfs_fops = {
917 .owner = THIS_MODULE,
918 .open = vga_switcheroo_debugfs_open,
919 .write = vga_switcheroo_debugfs_write,
922 .release = single_release,
927 vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
929 debugfs_remove(priv->switch_file);
930 priv->switch_file = NULL;
932 debugfs_remove(priv->debugfs_root);
933 priv->debugfs_root = NULL;
937 vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
939 static const char mp[] = "/sys/kernel/debug";
941 /* already initialised */
942 if (priv->debugfs_root)
944 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
946 if (!priv->debugfs_root) {
947 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
951 priv->switch_file = debugfs_create_file("switch", 0644,
952 priv->debugfs_root, NULL,
953 &vga_switcheroo_debugfs_fops);
954 if (!priv->switch_file) {
955 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
960 vga_switcheroo_debugfs_fini(priv);
963 #endif /* debugfs 2 */
966 * Device infrastructure
968 #define BUFFERSIZE 255
970 static d_open_t vga_switcheroo_open;
971 static d_close_t vga_switcheroo_close;
972 static d_read_t vga_switcheroo_read;
973 static d_write_t vga_switcheroo_write;
975 static struct dev_ops vga_switcheroo_ops = {
976 { "vga_switcheroo", 0, 0 },
977 .d_open = vga_switcheroo_open,
978 .d_close = vga_switcheroo_close,
979 .d_read = vga_switcheroo_read,
980 .d_write = vga_switcheroo_write,
983 struct s_vga_switcheroo {
984 char msg[BUFFERSIZE + 1];
988 static cdev_t vga_switcheroo_dev;
989 static struct s_vga_switcheroo *vga_switcheroo_buf;
991 MALLOC_DECLARE(M_VGA_SWITCHEROO_BUF);
992 MALLOC_DEFINE(M_VGA_SWITCHEROO_BUF, "vga_switcheroo_buf", "buffer for vga_switcheroo");
993 MALLOC_DECLARE(M_VGA_SWITCHEROO_VGASR_PRIV);
994 MALLOC_DEFINE(M_VGA_SWITCHEROO_VGASR_PRIV, "vga_switcheroo_vgasr_priv", "private data of vga_switcheroo");
997 vga_switcheroo_handler(struct module *m __unused, int what, void *arg __unused)
1002 case MOD_LOAD: /* kldload */
1003 vga_switcheroo_dev = make_dev(
1004 &vga_switcheroo_ops,
1010 reference_dev(vga_switcheroo_dev);
1014 vga_switcheroo_buf = kmalloc(sizeof(*vga_switcheroo_buf), M_VGA_SWITCHEROO_BUF, M_WAITOK | M_ZERO);
1017 vgasr_priv = kmalloc(sizeof(*vgasr_priv), M_VGA_SWITCHEROO_VGASR_PRIV, M_WAITOK | M_ZERO);
1018 vgasr_priv->clients = (struct list_head) { &(vgasr_priv->clients), &(vgasr_priv->clients) };
1019 lockinit(&vgasr_priv->mux_hw_lk, "mux_hw_lk", 0, LK_CANRECURSE);
1020 kprintf("vga_switcheroo device loaded\n");
1025 destroy_dev(vga_switcheroo_dev);
1026 kfree(vga_switcheroo_buf, M_VGA_SWITCHEROO_BUF);
1027 kfree(vgasr_priv, M_VGA_SWITCHEROO_VGASR_PRIV);
1028 kprintf("vga_switcheroo device unloaded\n");
1040 vga_switcheroo_open(struct dev_open_args *ap)
1048 vga_switcheroo_close(struct dev_close_args *ap)
1054 vga_switcheroo_read(struct dev_read_args *ap)
1056 struct vga_switcheroo_client *client;
1059 mutex_lock(&vgasr_mutex);
1060 list_for_each_entry(client, &vgasr_priv->clients, list) {
1061 uprintf("%d:%s%s:%c:%s%s:%s\n", i,
1062 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
1063 client_is_vga(client) ? "" : "-Audio",
1064 client->active ? '+' : ' ',
1065 client->driver_power_control ? "Dyn" : "",
1066 client->pwr_state ? "Pwr" : "Off",
1067 device_get_nameunit(client->pdev->dev.bsddev));
1070 mutex_unlock(&vgasr_mutex);
1075 vga_switcheroo_write(struct dev_write_args *ap)
1077 struct uio *uio = ap->a_uio;
1084 bool just_mux = false;
1085 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
1086 struct vga_switcheroo_client *client = NULL;
1089 * We either write from the beginning or are appending -- do
1090 * not allow random access.
1092 if (uio->uio_offset != 0 && (uio->uio_offset != vga_switcheroo_buf->len))
1095 /* This is a new message, reset length */
1096 if (uio->uio_offset == 0)
1097 vga_switcheroo_buf->len = 0;
1099 /* Copy the string in from user memory to kernel memory */
1100 amt = MIN(uio->uio_resid, (BUFFERSIZE - vga_switcheroo_buf->len));
1103 error = uiomove(vga_switcheroo_buf->msg + uio->uio_offset, amt, uio);
1105 vga_switcheroo_buf->len = uio->uio_offset;
1106 vga_switcheroo_buf->msg[vga_switcheroo_buf->len] = 0;
1109 uprintf("Write failed: bad address!\n");
1111 mutex_lock(&vgasr_mutex);
1113 if (!vgasr_priv->active) {
1119 * START PROCESSING COMMANDS
1122 /* pwr off the device not in use */
1123 if (strncmp(vga_switcheroo_buf->msg, "OFF", 3) == 0) {
1124 list_for_each_entry(client, &vgasr_priv->clients, list) {
1125 if (client->active || client_is_audio(client))
1127 if (client->driver_power_control)
1129 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
1130 if (client->pwr_state == VGA_SWITCHEROO_ON)
1131 vga_switchoff(client);
1136 /* pwr on the device not in use */
1137 if (strncmp(vga_switcheroo_buf->msg, "ON", 2) == 0) {
1138 list_for_each_entry(client, &vgasr_priv->clients, list) {
1139 if (client->active || client_is_audio(client))
1141 if (client->driver_power_control)
1143 if (client->pwr_state == VGA_SWITCHEROO_OFF)
1144 vga_switchon(client);
1145 set_audio_state(client->id, VGA_SWITCHEROO_ON);
1150 /* request a delayed switch - test can we switch now */
1151 if (strncmp(vga_switcheroo_buf->msg, "DIGD", 4) == 0) {
1152 client_id = VGA_SWITCHEROO_IGD;
1156 if (strncmp(vga_switcheroo_buf->msg, "DDIS", 4) == 0) {
1157 client_id = VGA_SWITCHEROO_DIS;
1161 if (strncmp(vga_switcheroo_buf->msg, "IGD", 3) == 0)
1162 client_id = VGA_SWITCHEROO_IGD;
1164 if (strncmp(vga_switcheroo_buf->msg, "DIS", 3) == 0)
1165 client_id = VGA_SWITCHEROO_DIS;
1167 if (strncmp(vga_switcheroo_buf->msg, "MIGD", 4) == 0) {
1169 client_id = VGA_SWITCHEROO_IGD;
1172 if (strncmp(vga_switcheroo_buf->msg, "MDIS", 4) == 0) {
1174 client_id = VGA_SWITCHEROO_DIS;
1177 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
1179 client = find_client_from_id(&vgasr_priv->clients, client_id);
1183 vgasr_priv->delayed_switch_active = false;
1186 mutex_lock(&vgasr_priv->mux_hw_lk);
1187 ret = vgasr_priv->handler->switchto(client_id);
1188 mutex_unlock(&vgasr_priv->mux_hw_lk);
1195 /* okay we want a switch - test if devices are willing to switch */
1196 can_switch = check_can_switch();
1198 if (can_switch == false && delay == false)
1202 ret = vga_switchto_stage1(client);
1204 pr_err("switching failed stage 1 %d\n", ret);
1206 ret = vga_switchto_stage2(client);
1208 pr_err("switching failed stage 2 %d\n", ret);
1211 pr_info("setting delayed switch to client %d\n", client->id);
1212 vgasr_priv->delayed_switch_active = true;
1213 vgasr_priv->delayed_client_id = client_id;
1215 ret = vga_switchto_stage1(client);
1217 pr_err("delayed switching stage 1 failed %d\n", ret);
1221 mutex_unlock(&vgasr_mutex);
1225 #ifdef __DragonFly__
1227 vga_switcheroo_force_migd(void)
1229 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_IGD;
1232 mutex_lock(&vgasr_priv->mux_hw_lk);
1233 ret = vgasr_priv->handler->switchto(client_id);
1234 mutex_unlock(&vgasr_priv->mux_hw_lk);
1236 pr_err("failed to switch gmux to IGD\n");
1242 MODULE_VERSION(vga_switcheroo, 1);
1243 DEV_MODULE(vga_switcheroo, vga_switcheroo_handler, NULL);
1246 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
1248 * Process a delayed switch if one is pending. DRM drivers should call this
1249 * from their ->lastclose callback.
1251 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
1252 * has unregistered in the meantime or if there are other clients blocking the
1253 * switch. If the actual switch fails, an error is reported and 0 is returned.
1256 vga_switcheroo_process_delayed_switch(void)
1258 struct vga_switcheroo_client *client;
1262 mutex_lock(&vgasr_mutex);
1263 if (!vgasr_priv->delayed_switch_active)
1266 pr_info("processing delayed switch to %d\n",
1267 vgasr_priv->delayed_client_id);
1269 client = find_client_from_id(&vgasr_priv->clients,
1270 vgasr_priv->delayed_client_id);
1271 if (!client || !check_can_switch())
1274 ret = vga_switchto_stage2(client);
1276 pr_err("delayed switching failed stage 2 %d\n", ret);
1278 vgasr_priv->delayed_switch_active = false;
1281 mutex_unlock(&vgasr_mutex);
1284 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
1287 * DOC: Driver power control
1289 * In this mode of use, the discrete GPU automatically powers up and down at
1290 * the discretion of the driver's runtime pm. On muxed machines, the user may
1291 * still influence the muxer state by way of the debugfs interface, however
1292 * the ON and OFF commands become a no-op for the discrete GPU.
1294 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
1295 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
1296 * command line disables it.
1298 * When the driver decides to power up or down, it notifies vga_switcheroo
1299 * thereof so that it can (a) power the audio device on the GPU up or down,
1300 * and (b) update its internal power state representation for the device.
1301 * This is achieved by vga_switcheroo_set_dynamic_switch().
1303 * After the GPU has been suspended, the handler needs to be called to cut
1304 * power to the GPU. Likewise it needs to reinstate power before the GPU
1305 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
1306 * which augments the GPU's suspend/resume functions by the requisite
1307 * calls to the handler.
1309 * When the audio device resumes, the GPU needs to be woken. This is achieved
1310 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
1311 * audio device's resume function.
1313 * On muxed machines, if the mux is initially switched to the discrete GPU,
1314 * the user ends up with a black screen when the GPU powers down after boot.
1315 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
1316 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
1319 #if 0 /* used in: runtime_suspend/resume() */
1321 vga_switcheroo_power_switch(struct pci_dev *pdev,
1322 enum vga_switcheroo_state state)
1324 struct vga_switcheroo_client *client;
1326 if (!vgasr_priv->handler->power_state)
1329 client = find_client_from_pci(&vgasr_priv->clients, pdev);
1333 if (!client->driver_power_control)
1336 vgasr_priv->handler->power_state(client->id, state);
1338 #endif /* used in: runtime_suspend/resume() */
1341 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
1342 * @pdev: client pci device
1343 * @dynamic: new power state
1345 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1346 * When the driver decides to power up or down, it notifies vga_switcheroo
1347 * thereof using this helper so that it can (a) power the audio device on
1348 * the GPU up or down, and (b) update its internal power state representation
1351 #if 0 /* TODO: no dynamic_switch yet */
1353 vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
1354 enum vga_switcheroo_state dynamic)
1356 struct vga_switcheroo_client *client;
1358 mutex_lock(&vgasr_mutex);
1359 client = find_client_from_pci(&vgasr_priv.clients, pdev);
1360 if (!client || !client->driver_power_control) {
1361 mutex_unlock(&vgasr_mutex);
1365 client->pwr_state = dynamic;
1366 set_audio_state(client->id, dynamic);
1367 mutex_unlock(&vgasr_mutex);
1369 EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
1371 /* switcheroo power domain */
1373 vga_switcheroo_runtime_suspend(struct device *dev)
1375 struct pci_dev *pdev = to_pci_dev(dev);
1378 ret = dev->bus->pm->runtime_suspend(dev);
1381 mutex_lock(&vgasr_mutex);
1382 if (vgasr_priv.handler->switchto) {
1383 mutex_lock(&vgasr_priv.mux_hw_lock);
1384 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
1385 mutex_unlock(&vgasr_priv.mux_hw_lock);
1387 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
1388 mutex_unlock(&vgasr_mutex);
1393 vga_switcheroo_runtime_resume(struct device *dev)
1395 struct pci_dev *pdev = to_pci_dev(dev);
1398 mutex_lock(&vgasr_mutex);
1399 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
1400 mutex_unlock(&vgasr_mutex);
1401 ret = dev->bus->pm->runtime_resume(dev);
1407 #endif /* TODO: no dynamic_switch yet */
1410 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
1411 * @dev: vga client device
1412 * @domain: power domain
1414 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1415 * After the GPU has been suspended, the handler needs to be called to cut
1416 * power to the GPU. Likewise it needs to reinstate power before the GPU
1417 * can resume. To this end, this helper augments the suspend/resume functions
1418 * by the requisite calls to the handler. It needs only be called on platforms
1419 * where the power switch is separate to the device being powered down.
1421 #if 0 /* TODO: no pm_ops */
1423 vga_switcheroo_init_domain_pm_ops(struct device *dev,
1424 struct dev_pm_domain *domain)
1426 /* copy over all the bus versions */
1427 if (dev->bus && dev->bus->pm) {
1428 domain->ops = *dev->bus->pm;
1429 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
1430 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
1432 dev_pm_domain_set(dev, domain);
1435 dev_pm_domain_set(dev, NULL);
1438 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
1441 vga_switcheroo_fini_domain_pm_ops(struct device *dev)
1443 dev_pm_domain_set(dev, NULL);
1445 EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
1448 vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
1450 struct pci_dev *pdev = to_pci_dev(dev);
1451 struct vga_switcheroo_client *client;
1452 struct device *video_dev = NULL;
1455 /* we need to check if we have to switch back on the video
1456 device so the audio device can come back */
1457 mutex_lock(&vgasr_mutex);
1458 list_for_each_entry(client, &vgasr_priv.clients, list) {
1459 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
1460 client_is_vga(client)) {
1461 video_dev = &client->pdev->dev;
1465 mutex_unlock(&vgasr_mutex);
1468 ret = pm_runtime_get_sync(video_dev);
1469 if (ret && ret != 1)
1472 ret = dev->bus->pm->runtime_resume(dev);
1474 /* put the reference for the gpu */
1476 pm_runtime_mark_last_busy(video_dev);
1477 pm_runtime_put_autosuspend(video_dev);
1481 #endif /* TODO: no pm_ops */
1484 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
1486 * @dev: audio client device
1487 * @domain: power domain
1489 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1490 * When the audio device resumes, the GPU needs to be woken. This helper
1491 * augments the audio device's resume function to do that.
1493 * Return: 0 on success, -EINVAL if no power management operations are
1494 * defined for this device.
1496 #if 0 /* TODO: no pm */
1498 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
1499 struct dev_pm_domain *domain)
1501 /* copy over all the bus versions */
1502 if (dev->bus && dev->bus->pm) {
1503 domain->ops = *dev->bus->pm;
1504 domain->ops.runtime_resume =
1505 vga_switcheroo_runtime_resume_hdmi_audio;
1507 dev_pm_domain_set(dev, domain);
1510 dev_pm_domain_set(dev, NULL);
1513 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);
1514 #endif /* TODO: no pm */