2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
8 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
10 * Switcher interface - methods require for ATPX and DCM
11 * - switchto - this throws the output MUX switch
12 * - discrete_set_power - sets the power state for the discrete card
14 * GPU driver interface
15 * - set_gpu_state - this should do the equiv of s/r for the card
16 * - this should *not* set the discrete power state
17 * - switch_check - check if the device is in a position to switch now
20 #define pr_fmt(fmt) "vga_switcheroo: " fmt
22 #include <linux/module.h>
23 #include <linux/seq_file.h>
24 #include <linux/uaccess.h>
26 #include <linux/debugfs.h>
29 #include <linux/pci.h>
30 #include <linux/console.h>
31 #include <linux/vga_switcheroo.h>
32 #include <linux/pm_runtime.h>
34 #include <linux/vgaarb.h>
36 struct vga_switcheroo_client {
38 struct fb_info *fb_info;
40 const struct vga_switcheroo_client_ops *ops;
43 bool driver_power_control;
44 struct list_head list;
47 static DEFINE_MUTEX(vgasr_mutex);
52 bool delayed_switch_active;
53 enum vga_switcheroo_client_id delayed_client_id;
55 struct dentry *debugfs_root;
56 struct dentry *switch_file;
58 int registered_clients;
59 struct list_head clients;
61 struct vga_switcheroo_handler *handler;
64 #define ID_BIT_AUDIO 0x100
65 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
66 #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
67 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
69 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
70 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
72 /* only one switcheroo per system */
73 static struct vgasr_priv vgasr_priv = {
74 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
77 static bool vga_switcheroo_ready(void)
79 /* we're ready if we get two clients + handler */
80 return !vgasr_priv.active &&
81 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
84 static void vga_switcheroo_enable(void)
87 struct vga_switcheroo_client *client;
89 /* call the handler to init */
90 if (vgasr_priv.handler->init)
91 vgasr_priv.handler->init();
93 list_for_each_entry(client, &vgasr_priv.clients, list) {
96 ret = vgasr_priv.handler->get_client_id(client->pdev);
102 vga_switcheroo_debugfs_init(&vgasr_priv);
103 vgasr_priv.active = true;
106 int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
108 mutex_lock(&vgasr_mutex);
109 if (vgasr_priv.handler) {
110 mutex_unlock(&vgasr_mutex);
114 vgasr_priv.handler = handler;
115 if (vga_switcheroo_ready()) {
116 pr_info("enabled\n");
117 vga_switcheroo_enable();
119 mutex_unlock(&vgasr_mutex);
122 EXPORT_SYMBOL(vga_switcheroo_register_handler);
124 void vga_switcheroo_unregister_handler(void)
126 mutex_lock(&vgasr_mutex);
127 vgasr_priv.handler = NULL;
128 if (vgasr_priv.active) {
129 pr_info("disabled\n");
130 vga_switcheroo_debugfs_fini(&vgasr_priv);
131 vgasr_priv.active = false;
133 mutex_unlock(&vgasr_mutex);
135 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
137 static int register_client(struct pci_dev *pdev,
138 const struct vga_switcheroo_client_ops *ops,
139 int id, bool active, bool driver_power_control)
141 struct vga_switcheroo_client *client;
143 client = kzalloc(sizeof(*client), GFP_KERNEL);
147 client->pwr_state = VGA_SWITCHEROO_ON;
151 client->active = active;
152 client->driver_power_control = driver_power_control;
154 mutex_lock(&vgasr_mutex);
155 list_add_tail(&client->list, &vgasr_priv.clients);
156 if (client_is_vga(client))
157 vgasr_priv.registered_clients++;
159 if (vga_switcheroo_ready()) {
160 pr_info("enabled\n");
161 vga_switcheroo_enable();
163 mutex_unlock(&vgasr_mutex);
167 int vga_switcheroo_register_client(struct pci_dev *pdev,
168 const struct vga_switcheroo_client_ops *ops,
169 bool driver_power_control)
171 return register_client(pdev, ops, -1,
172 pdev == vga_default_device(),
173 driver_power_control);
175 EXPORT_SYMBOL(vga_switcheroo_register_client);
177 int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
178 const struct vga_switcheroo_client_ops *ops,
181 return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false);
183 EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
185 static struct vga_switcheroo_client *
186 find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
188 struct vga_switcheroo_client *client;
190 list_for_each_entry(client, head, list)
191 if (client->pdev == pdev)
196 static struct vga_switcheroo_client *
197 find_client_from_id(struct list_head *head, int client_id)
199 struct vga_switcheroo_client *client;
201 list_for_each_entry(client, head, list)
202 if (client->id == client_id)
207 static struct vga_switcheroo_client *
208 find_active_client(struct list_head *head)
210 struct vga_switcheroo_client *client;
212 list_for_each_entry(client, head, list)
213 if (client->active && client_is_vga(client))
218 int vga_switcheroo_get_client_state(struct pci_dev *pdev)
220 struct vga_switcheroo_client *client;
222 client = find_client_from_pci(&vgasr_priv.clients, pdev);
224 return VGA_SWITCHEROO_NOT_FOUND;
225 if (!vgasr_priv.active)
226 return VGA_SWITCHEROO_INIT;
227 return client->pwr_state;
229 EXPORT_SYMBOL(vga_switcheroo_get_client_state);
231 void vga_switcheroo_unregister_client(struct pci_dev *pdev)
233 struct vga_switcheroo_client *client;
235 mutex_lock(&vgasr_mutex);
236 client = find_client_from_pci(&vgasr_priv.clients, pdev);
238 if (client_is_vga(client))
239 vgasr_priv.registered_clients--;
240 list_del(&client->list);
243 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
244 pr_info("disabled\n");
245 vga_switcheroo_debugfs_fini(&vgasr_priv);
246 vgasr_priv.active = false;
248 mutex_unlock(&vgasr_mutex);
250 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
252 void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
253 struct fb_info *info)
255 struct vga_switcheroo_client *client;
257 mutex_lock(&vgasr_mutex);
258 client = find_client_from_pci(&vgasr_priv.clients, pdev);
260 client->fb_info = info;
261 mutex_unlock(&vgasr_mutex);
263 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
265 static int vga_switcheroo_show(struct seq_file *m, void *v)
267 struct vga_switcheroo_client *client;
270 mutex_lock(&vgasr_mutex);
271 list_for_each_entry(client, &vgasr_priv.clients, list) {
272 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
273 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
275 client_is_vga(client) ? "" : "-Audio",
276 client->active ? '+' : ' ',
277 client->driver_power_control ? "Dyn" : "",
278 client->pwr_state ? "Pwr" : "Off",
279 pci_name(client->pdev));
282 mutex_unlock(&vgasr_mutex);
286 static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
288 return single_open(file, vga_switcheroo_show, NULL);
291 static int vga_switchon(struct vga_switcheroo_client *client)
293 if (client->driver_power_control)
295 if (vgasr_priv.handler->power_state)
296 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
297 /* call the driver callback to turn on device */
298 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
299 client->pwr_state = VGA_SWITCHEROO_ON;
303 static int vga_switchoff(struct vga_switcheroo_client *client)
305 if (client->driver_power_control)
307 /* call the driver callback to turn off device */
308 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
309 if (vgasr_priv.handler->power_state)
310 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
311 client->pwr_state = VGA_SWITCHEROO_OFF;
315 static void set_audio_state(int id, int state)
317 struct vga_switcheroo_client *client;
319 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
320 if (client && client->pwr_state != state) {
321 client->ops->set_gpu_state(client->pdev, state);
322 client->pwr_state = state;
326 /* stage one happens before delay */
327 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
329 struct vga_switcheroo_client *active;
331 active = find_active_client(&vgasr_priv.clients);
335 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
336 vga_switchon(new_client);
338 vga_set_default_device(new_client->pdev);
343 static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
346 struct vga_switcheroo_client *active;
348 active = find_active_client(&vgasr_priv.clients);
352 active->active = false;
354 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
356 if (new_client->fb_info) {
357 struct fb_event event;
360 event.info = new_client->fb_info;
361 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
365 ret = vgasr_priv.handler->switchto(new_client->id);
369 if (new_client->ops->reprobe)
370 new_client->ops->reprobe(new_client->pdev);
372 if (active->pwr_state == VGA_SWITCHEROO_ON)
373 vga_switchoff(active);
375 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
377 new_client->active = true;
381 static bool check_can_switch(void)
383 struct vga_switcheroo_client *client;
385 list_for_each_entry(client, &vgasr_priv.clients, list) {
386 if (!client->ops->can_switch(client->pdev)) {
387 pr_err("client %x refused switch\n", client->id);
395 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
396 size_t cnt, loff_t *ppos)
400 bool delay = false, can_switch;
401 bool just_mux = false;
403 struct vga_switcheroo_client *client = NULL;
408 if (copy_from_user(usercmd, ubuf, cnt))
411 mutex_lock(&vgasr_mutex);
413 if (!vgasr_priv.active) {
418 /* pwr off the device not in use */
419 if (strncmp(usercmd, "OFF", 3) == 0) {
420 list_for_each_entry(client, &vgasr_priv.clients, list) {
421 if (client->active || client_is_audio(client))
423 if (client->driver_power_control)
425 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
426 if (client->pwr_state == VGA_SWITCHEROO_ON)
427 vga_switchoff(client);
431 /* pwr on the device not in use */
432 if (strncmp(usercmd, "ON", 2) == 0) {
433 list_for_each_entry(client, &vgasr_priv.clients, list) {
434 if (client->active || client_is_audio(client))
436 if (client->driver_power_control)
438 if (client->pwr_state == VGA_SWITCHEROO_OFF)
439 vga_switchon(client);
440 set_audio_state(client->id, VGA_SWITCHEROO_ON);
445 /* request a delayed switch - test can we switch now */
446 if (strncmp(usercmd, "DIGD", 4) == 0) {
447 client_id = VGA_SWITCHEROO_IGD;
451 if (strncmp(usercmd, "DDIS", 4) == 0) {
452 client_id = VGA_SWITCHEROO_DIS;
456 if (strncmp(usercmd, "IGD", 3) == 0)
457 client_id = VGA_SWITCHEROO_IGD;
459 if (strncmp(usercmd, "DIS", 3) == 0)
460 client_id = VGA_SWITCHEROO_DIS;
462 if (strncmp(usercmd, "MIGD", 4) == 0) {
464 client_id = VGA_SWITCHEROO_IGD;
466 if (strncmp(usercmd, "MDIS", 4) == 0) {
468 client_id = VGA_SWITCHEROO_DIS;
473 client = find_client_from_id(&vgasr_priv.clients, client_id);
477 vgasr_priv.delayed_switch_active = false;
480 ret = vgasr_priv.handler->switchto(client_id);
487 /* okay we want a switch - test if devices are willing to switch */
488 can_switch = check_can_switch();
490 if (can_switch == false && delay == false)
494 ret = vga_switchto_stage1(client);
496 pr_err("switching failed stage 1 %d\n", ret);
498 ret = vga_switchto_stage2(client);
500 pr_err("switching failed stage 2 %d\n", ret);
503 pr_info("setting delayed switch to client %d\n", client->id);
504 vgasr_priv.delayed_switch_active = true;
505 vgasr_priv.delayed_client_id = client_id;
507 ret = vga_switchto_stage1(client);
509 pr_err("delayed switching stage 1 failed %d\n", ret);
513 mutex_unlock(&vgasr_mutex);
517 static const struct file_operations vga_switcheroo_debugfs_fops = {
518 .owner = THIS_MODULE,
519 .open = vga_switcheroo_debugfs_open,
520 .write = vga_switcheroo_debugfs_write,
523 .release = single_release,
526 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
528 debugfs_remove(priv->switch_file);
529 priv->switch_file = NULL;
531 debugfs_remove(priv->debugfs_root);
532 priv->debugfs_root = NULL;
535 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
537 static const char mp[] = "/sys/kernel/debug";
539 /* already initialised */
540 if (priv->debugfs_root)
542 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
544 if (!priv->debugfs_root) {
545 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
549 priv->switch_file = debugfs_create_file("switch", 0644,
550 priv->debugfs_root, NULL,
551 &vga_switcheroo_debugfs_fops);
552 if (!priv->switch_file) {
553 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
558 vga_switcheroo_debugfs_fini(priv);
562 int vga_switcheroo_process_delayed_switch(void)
564 struct vga_switcheroo_client *client;
568 mutex_lock(&vgasr_mutex);
569 if (!vgasr_priv.delayed_switch_active)
572 pr_info("processing delayed switch to %d\n",
573 vgasr_priv.delayed_client_id);
575 client = find_client_from_id(&vgasr_priv.clients,
576 vgasr_priv.delayed_client_id);
577 if (!client || !check_can_switch())
580 ret = vga_switchto_stage2(client);
582 pr_err("delayed switching failed stage 2 %d\n", ret);
584 vgasr_priv.delayed_switch_active = false;
587 mutex_unlock(&vgasr_mutex);
590 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
592 static void vga_switcheroo_power_switch(struct pci_dev *pdev,
593 enum vga_switcheroo_state state)
595 struct vga_switcheroo_client *client;
597 if (!vgasr_priv.handler->power_state)
600 client = find_client_from_pci(&vgasr_priv.clients, pdev);
604 if (!client->driver_power_control)
607 vgasr_priv.handler->power_state(client->id, state);
610 /* force a PCI device to a certain state - mainly to turn off audio clients */
612 void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
613 enum vga_switcheroo_state dynamic)
615 struct vga_switcheroo_client *client;
617 client = find_client_from_pci(&vgasr_priv.clients, pdev);
621 if (!client->driver_power_control)
624 client->pwr_state = dynamic;
625 set_audio_state(client->id, dynamic);
627 EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
629 /* switcheroo power domain */
630 static int vga_switcheroo_runtime_suspend(struct device *dev)
632 struct pci_dev *pdev = to_pci_dev(dev);
635 ret = dev->bus->pm->runtime_suspend(dev);
638 if (vgasr_priv.handler->switchto)
639 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
640 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
644 static int vga_switcheroo_runtime_resume(struct device *dev)
646 struct pci_dev *pdev = to_pci_dev(dev);
649 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
650 ret = dev->bus->pm->runtime_resume(dev);
657 /* this version is for the case where the power switch is separate
658 to the device being powered down. */
659 int vga_switcheroo_init_domain_pm_ops(struct device *dev,
660 struct dev_pm_domain *domain)
662 /* copy over all the bus versions */
663 if (dev->bus && dev->bus->pm) {
664 domain->ops = *dev->bus->pm;
665 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
666 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
668 dev->pm_domain = domain;
671 dev->pm_domain = NULL;
674 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
676 void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
678 dev->pm_domain = NULL;
680 EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
682 static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
684 struct pci_dev *pdev = to_pci_dev(dev);
686 struct vga_switcheroo_client *client, *found = NULL;
688 /* we need to check if we have to switch back on the video
689 device so the audio device can come back */
690 list_for_each_entry(client, &vgasr_priv.clients, list) {
691 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
692 client_is_vga(client)) {
694 ret = pm_runtime_get_sync(&client->pdev->dev);
702 ret = dev->bus->pm->runtime_resume(dev);
704 /* put the reference for the gpu */
706 pm_runtime_mark_last_busy(&found->pdev->dev);
707 pm_runtime_put_autosuspend(&found->pdev->dev);
713 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
714 struct dev_pm_domain *domain)
716 /* copy over all the bus versions */
717 if (dev->bus && dev->bus->pm) {
718 domain->ops = *dev->bus->pm;
719 domain->ops.runtime_resume =
720 vga_switcheroo_runtime_resume_hdmi_audio;
722 dev->pm_domain = domain;
725 dev->pm_domain = NULL;
728 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);