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
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 #include <linux/module.h>
21 #include <linux/seq_file.h>
22 #include <linux/uaccess.h>
24 #include <linux/debugfs.h>
27 #include <linux/pci.h>
28 #include <linux/console.h>
29 #include <linux/vga_switcheroo.h>
30 #include <linux/pm_runtime.h>
32 #include <linux/vgaarb.h>
34 struct vga_switcheroo_client
{
36 struct fb_info
*fb_info
;
38 const struct vga_switcheroo_client_ops
*ops
;
41 bool driver_power_control
;
42 struct list_head list
;
45 static DEFINE_MUTEX(vgasr_mutex
);
50 bool delayed_switch_active
;
51 enum vga_switcheroo_client_id delayed_client_id
;
53 struct dentry
*debugfs_root
;
54 struct dentry
*switch_file
;
56 int registered_clients
;
57 struct list_head clients
;
59 struct vga_switcheroo_handler
*handler
;
62 #define ID_BIT_AUDIO 0x100
63 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
64 #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
65 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
67 static int vga_switcheroo_debugfs_init(struct vgasr_priv
*priv
);
68 static void vga_switcheroo_debugfs_fini(struct vgasr_priv
*priv
);
70 /* only one switcheroo per system */
71 static struct vgasr_priv vgasr_priv
= {
72 .clients
= LIST_HEAD_INIT(vgasr_priv
.clients
),
75 static bool vga_switcheroo_ready(void)
77 /* we're ready if we get two clients + handler */
78 return !vgasr_priv
.active
&&
79 vgasr_priv
.registered_clients
== 2 && vgasr_priv
.handler
;
82 static void vga_switcheroo_enable(void)
85 struct vga_switcheroo_client
*client
;
87 /* call the handler to init */
88 if (vgasr_priv
.handler
->init
)
89 vgasr_priv
.handler
->init();
91 list_for_each_entry(client
, &vgasr_priv
.clients
, list
) {
94 ret
= vgasr_priv
.handler
->get_client_id(client
->pdev
);
100 vga_switcheroo_debugfs_init(&vgasr_priv
);
101 vgasr_priv
.active
= true;
104 int vga_switcheroo_register_handler(struct vga_switcheroo_handler
*handler
)
106 mutex_lock(&vgasr_mutex
);
107 if (vgasr_priv
.handler
) {
108 mutex_unlock(&vgasr_mutex
);
112 vgasr_priv
.handler
= handler
;
113 if (vga_switcheroo_ready()) {
114 printk(KERN_INFO
"vga_switcheroo: enabled\n");
115 vga_switcheroo_enable();
117 mutex_unlock(&vgasr_mutex
);
120 EXPORT_SYMBOL(vga_switcheroo_register_handler
);
122 void vga_switcheroo_unregister_handler(void)
124 mutex_lock(&vgasr_mutex
);
125 vgasr_priv
.handler
= NULL
;
126 if (vgasr_priv
.active
) {
127 pr_info("vga_switcheroo: disabled\n");
128 vga_switcheroo_debugfs_fini(&vgasr_priv
);
129 vgasr_priv
.active
= false;
131 mutex_unlock(&vgasr_mutex
);
133 EXPORT_SYMBOL(vga_switcheroo_unregister_handler
);
135 static int register_client(struct pci_dev
*pdev
,
136 const struct vga_switcheroo_client_ops
*ops
,
137 int id
, bool active
, bool driver_power_control
)
139 struct vga_switcheroo_client
*client
;
141 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
145 client
->pwr_state
= VGA_SWITCHEROO_ON
;
149 client
->active
= active
;
150 client
->driver_power_control
= driver_power_control
;
152 mutex_lock(&vgasr_mutex
);
153 list_add_tail(&client
->list
, &vgasr_priv
.clients
);
154 if (client_is_vga(client
))
155 vgasr_priv
.registered_clients
++;
157 if (vga_switcheroo_ready()) {
158 printk(KERN_INFO
"vga_switcheroo: enabled\n");
159 vga_switcheroo_enable();
161 mutex_unlock(&vgasr_mutex
);
165 int vga_switcheroo_register_client(struct pci_dev
*pdev
,
166 const struct vga_switcheroo_client_ops
*ops
,
167 bool driver_power_control
)
169 return register_client(pdev
, ops
, -1,
170 pdev
== vga_default_device(), driver_power_control
);
172 EXPORT_SYMBOL(vga_switcheroo_register_client
);
174 int vga_switcheroo_register_audio_client(struct pci_dev
*pdev
,
175 const struct vga_switcheroo_client_ops
*ops
,
178 return register_client(pdev
, ops
, id
| ID_BIT_AUDIO
, active
, false);
180 EXPORT_SYMBOL(vga_switcheroo_register_audio_client
);
182 static struct vga_switcheroo_client
*
183 find_client_from_pci(struct list_head
*head
, struct pci_dev
*pdev
)
185 struct vga_switcheroo_client
*client
;
186 list_for_each_entry(client
, head
, list
)
187 if (client
->pdev
== pdev
)
192 static struct vga_switcheroo_client
*
193 find_client_from_id(struct list_head
*head
, int client_id
)
195 struct vga_switcheroo_client
*client
;
196 list_for_each_entry(client
, head
, list
)
197 if (client
->id
== client_id
)
202 static struct vga_switcheroo_client
*
203 find_active_client(struct list_head
*head
)
205 struct vga_switcheroo_client
*client
;
206 list_for_each_entry(client
, head
, list
)
207 if (client
->active
&& client_is_vga(client
))
212 int vga_switcheroo_get_client_state(struct pci_dev
*pdev
)
214 struct vga_switcheroo_client
*client
;
216 client
= find_client_from_pci(&vgasr_priv
.clients
, pdev
);
218 return VGA_SWITCHEROO_NOT_FOUND
;
219 if (!vgasr_priv
.active
)
220 return VGA_SWITCHEROO_INIT
;
221 return client
->pwr_state
;
223 EXPORT_SYMBOL(vga_switcheroo_get_client_state
);
225 void vga_switcheroo_unregister_client(struct pci_dev
*pdev
)
227 struct vga_switcheroo_client
*client
;
229 mutex_lock(&vgasr_mutex
);
230 client
= find_client_from_pci(&vgasr_priv
.clients
, pdev
);
232 if (client_is_vga(client
))
233 vgasr_priv
.registered_clients
--;
234 list_del(&client
->list
);
237 if (vgasr_priv
.active
&& vgasr_priv
.registered_clients
< 2) {
238 printk(KERN_INFO
"vga_switcheroo: disabled\n");
239 vga_switcheroo_debugfs_fini(&vgasr_priv
);
240 vgasr_priv
.active
= false;
242 mutex_unlock(&vgasr_mutex
);
244 EXPORT_SYMBOL(vga_switcheroo_unregister_client
);
246 void vga_switcheroo_client_fb_set(struct pci_dev
*pdev
,
247 struct fb_info
*info
)
249 struct vga_switcheroo_client
*client
;
251 mutex_lock(&vgasr_mutex
);
252 client
= find_client_from_pci(&vgasr_priv
.clients
, pdev
);
254 client
->fb_info
= info
;
255 mutex_unlock(&vgasr_mutex
);
257 EXPORT_SYMBOL(vga_switcheroo_client_fb_set
);
259 static int vga_switcheroo_show(struct seq_file
*m
, void *v
)
261 struct vga_switcheroo_client
*client
;
263 mutex_lock(&vgasr_mutex
);
264 list_for_each_entry(client
, &vgasr_priv
.clients
, list
) {
265 seq_printf(m
, "%d:%s%s:%c:%s%s:%s\n", i
,
266 client_id(client
) == VGA_SWITCHEROO_DIS
? "DIS" : "IGD",
267 client_is_vga(client
) ? "" : "-Audio",
268 client
->active
? '+' : ' ',
269 client
->driver_power_control
? "Dyn" : "",
270 client
->pwr_state
? "Pwr" : "Off",
271 pci_name(client
->pdev
));
274 mutex_unlock(&vgasr_mutex
);
278 static int vga_switcheroo_debugfs_open(struct inode
*inode
, struct file
*file
)
280 return single_open(file
, vga_switcheroo_show
, NULL
);
283 static int vga_switchon(struct vga_switcheroo_client
*client
)
285 if (client
->driver_power_control
)
287 if (vgasr_priv
.handler
->power_state
)
288 vgasr_priv
.handler
->power_state(client
->id
, VGA_SWITCHEROO_ON
);
289 /* call the driver callback to turn on device */
290 client
->ops
->set_gpu_state(client
->pdev
, VGA_SWITCHEROO_ON
);
291 client
->pwr_state
= VGA_SWITCHEROO_ON
;
295 static int vga_switchoff(struct vga_switcheroo_client
*client
)
297 if (client
->driver_power_control
)
299 /* call the driver callback to turn off device */
300 client
->ops
->set_gpu_state(client
->pdev
, VGA_SWITCHEROO_OFF
);
301 if (vgasr_priv
.handler
->power_state
)
302 vgasr_priv
.handler
->power_state(client
->id
, VGA_SWITCHEROO_OFF
);
303 client
->pwr_state
= VGA_SWITCHEROO_OFF
;
307 static void set_audio_state(int id
, int state
)
309 struct vga_switcheroo_client
*client
;
311 client
= find_client_from_id(&vgasr_priv
.clients
, id
| ID_BIT_AUDIO
);
312 if (client
&& client
->pwr_state
!= state
) {
313 client
->ops
->set_gpu_state(client
->pdev
, state
);
314 client
->pwr_state
= state
;
318 /* stage one happens before delay */
319 static int vga_switchto_stage1(struct vga_switcheroo_client
*new_client
)
321 struct vga_switcheroo_client
*active
;
323 active
= find_active_client(&vgasr_priv
.clients
);
327 if (new_client
->pwr_state
== VGA_SWITCHEROO_OFF
)
328 vga_switchon(new_client
);
330 vga_set_default_device(new_client
->pdev
);
335 static int vga_switchto_stage2(struct vga_switcheroo_client
*new_client
)
338 struct vga_switcheroo_client
*active
;
340 active
= find_active_client(&vgasr_priv
.clients
);
344 active
->active
= false;
346 set_audio_state(active
->id
, VGA_SWITCHEROO_OFF
);
348 if (new_client
->fb_info
) {
349 struct fb_event event
;
351 event
.info
= new_client
->fb_info
;
352 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE
, &event
);
356 ret
= vgasr_priv
.handler
->switchto(new_client
->id
);
360 if (new_client
->ops
->reprobe
)
361 new_client
->ops
->reprobe(new_client
->pdev
);
363 if (active
->pwr_state
== VGA_SWITCHEROO_ON
)
364 vga_switchoff(active
);
366 set_audio_state(new_client
->id
, VGA_SWITCHEROO_ON
);
368 new_client
->active
= true;
372 static bool check_can_switch(void)
374 struct vga_switcheroo_client
*client
;
376 list_for_each_entry(client
, &vgasr_priv
.clients
, list
) {
377 if (!client
->ops
->can_switch(client
->pdev
)) {
378 printk(KERN_ERR
"vga_switcheroo: client %x refused switch\n", client
->id
);
386 vga_switcheroo_debugfs_write(struct file
*filp
, const char __user
*ubuf
,
387 size_t cnt
, loff_t
*ppos
)
391 bool delay
= false, can_switch
;
392 bool just_mux
= false;
394 struct vga_switcheroo_client
*client
= NULL
;
399 if (copy_from_user(usercmd
, ubuf
, cnt
))
402 mutex_lock(&vgasr_mutex
);
404 if (!vgasr_priv
.active
) {
409 /* pwr off the device not in use */
410 if (strncmp(usercmd
, "OFF", 3) == 0) {
411 list_for_each_entry(client
, &vgasr_priv
.clients
, list
) {
412 if (client
->active
|| client_is_audio(client
))
414 if (client
->driver_power_control
)
416 set_audio_state(client
->id
, VGA_SWITCHEROO_OFF
);
417 if (client
->pwr_state
== VGA_SWITCHEROO_ON
)
418 vga_switchoff(client
);
422 /* pwr on the device not in use */
423 if (strncmp(usercmd
, "ON", 2) == 0) {
424 list_for_each_entry(client
, &vgasr_priv
.clients
, list
) {
425 if (client
->active
|| client_is_audio(client
))
427 if (client
->driver_power_control
)
429 if (client
->pwr_state
== VGA_SWITCHEROO_OFF
)
430 vga_switchon(client
);
431 set_audio_state(client
->id
, VGA_SWITCHEROO_ON
);
436 /* request a delayed switch - test can we switch now */
437 if (strncmp(usercmd
, "DIGD", 4) == 0) {
438 client_id
= VGA_SWITCHEROO_IGD
;
442 if (strncmp(usercmd
, "DDIS", 4) == 0) {
443 client_id
= VGA_SWITCHEROO_DIS
;
447 if (strncmp(usercmd
, "IGD", 3) == 0)
448 client_id
= VGA_SWITCHEROO_IGD
;
450 if (strncmp(usercmd
, "DIS", 3) == 0)
451 client_id
= VGA_SWITCHEROO_DIS
;
453 if (strncmp(usercmd
, "MIGD", 4) == 0) {
455 client_id
= VGA_SWITCHEROO_IGD
;
457 if (strncmp(usercmd
, "MDIS", 4) == 0) {
459 client_id
= VGA_SWITCHEROO_DIS
;
464 client
= find_client_from_id(&vgasr_priv
.clients
, client_id
);
468 vgasr_priv
.delayed_switch_active
= false;
471 ret
= vgasr_priv
.handler
->switchto(client_id
);
478 /* okay we want a switch - test if devices are willing to switch */
479 can_switch
= check_can_switch();
481 if (can_switch
== false && delay
== false)
485 ret
= vga_switchto_stage1(client
);
487 printk(KERN_ERR
"vga_switcheroo: switching failed stage 1 %d\n", ret
);
489 ret
= vga_switchto_stage2(client
);
491 printk(KERN_ERR
"vga_switcheroo: switching failed stage 2 %d\n", ret
);
494 printk(KERN_INFO
"vga_switcheroo: setting delayed switch to client %d\n", client
->id
);
495 vgasr_priv
.delayed_switch_active
= true;
496 vgasr_priv
.delayed_client_id
= client_id
;
498 ret
= vga_switchto_stage1(client
);
500 printk(KERN_ERR
"vga_switcheroo: delayed switching stage 1 failed %d\n", ret
);
504 mutex_unlock(&vgasr_mutex
);
508 static const struct file_operations vga_switcheroo_debugfs_fops
= {
509 .owner
= THIS_MODULE
,
510 .open
= vga_switcheroo_debugfs_open
,
511 .write
= vga_switcheroo_debugfs_write
,
514 .release
= single_release
,
517 static void vga_switcheroo_debugfs_fini(struct vgasr_priv
*priv
)
519 if (priv
->switch_file
) {
520 debugfs_remove(priv
->switch_file
);
521 priv
->switch_file
= NULL
;
523 if (priv
->debugfs_root
) {
524 debugfs_remove(priv
->debugfs_root
);
525 priv
->debugfs_root
= NULL
;
529 static int vga_switcheroo_debugfs_init(struct vgasr_priv
*priv
)
531 /* already initialised */
532 if (priv
->debugfs_root
)
534 priv
->debugfs_root
= debugfs_create_dir("vgaswitcheroo", NULL
);
536 if (!priv
->debugfs_root
) {
537 printk(KERN_ERR
"vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
541 priv
->switch_file
= debugfs_create_file("switch", 0644,
542 priv
->debugfs_root
, NULL
, &vga_switcheroo_debugfs_fops
);
543 if (!priv
->switch_file
) {
544 printk(KERN_ERR
"vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
549 vga_switcheroo_debugfs_fini(priv
);
553 int vga_switcheroo_process_delayed_switch(void)
555 struct vga_switcheroo_client
*client
;
559 mutex_lock(&vgasr_mutex
);
560 if (!vgasr_priv
.delayed_switch_active
)
563 printk(KERN_INFO
"vga_switcheroo: processing delayed switch to %d\n", vgasr_priv
.delayed_client_id
);
565 client
= find_client_from_id(&vgasr_priv
.clients
,
566 vgasr_priv
.delayed_client_id
);
567 if (!client
|| !check_can_switch())
570 ret
= vga_switchto_stage2(client
);
572 printk(KERN_ERR
"vga_switcheroo: delayed switching failed stage 2 %d\n", ret
);
574 vgasr_priv
.delayed_switch_active
= false;
577 mutex_unlock(&vgasr_mutex
);
580 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch
);
582 static void vga_switcheroo_power_switch(struct pci_dev
*pdev
, enum vga_switcheroo_state state
)
584 struct vga_switcheroo_client
*client
;
586 if (!vgasr_priv
.handler
->power_state
)
589 client
= find_client_from_pci(&vgasr_priv
.clients
, pdev
);
593 if (!client
->driver_power_control
)
596 vgasr_priv
.handler
->power_state(client
->id
, state
);
599 /* force a PCI device to a certain state - mainly to turn off audio clients */
601 void vga_switcheroo_set_dynamic_switch(struct pci_dev
*pdev
, enum vga_switcheroo_state dynamic
)
603 struct vga_switcheroo_client
*client
;
605 client
= find_client_from_pci(&vgasr_priv
.clients
, pdev
);
609 if (!client
->driver_power_control
)
612 client
->pwr_state
= dynamic
;
613 set_audio_state(client
->id
, dynamic
);
615 EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch
);
617 /* switcheroo power domain */
618 static int vga_switcheroo_runtime_suspend(struct device
*dev
)
620 struct pci_dev
*pdev
= to_pci_dev(dev
);
623 ret
= dev
->bus
->pm
->runtime_suspend(dev
);
626 if (vgasr_priv
.handler
->switchto
)
627 vgasr_priv
.handler
->switchto(VGA_SWITCHEROO_IGD
);
628 vga_switcheroo_power_switch(pdev
, VGA_SWITCHEROO_OFF
);
632 static int vga_switcheroo_runtime_resume(struct device
*dev
)
634 struct pci_dev
*pdev
= to_pci_dev(dev
);
637 vga_switcheroo_power_switch(pdev
, VGA_SWITCHEROO_ON
);
638 ret
= dev
->bus
->pm
->runtime_resume(dev
);
645 /* this version is for the case where the power switch is separate
646 to the device being powered down. */
647 int vga_switcheroo_init_domain_pm_ops(struct device
*dev
, struct dev_pm_domain
*domain
)
649 /* copy over all the bus versions */
650 if (dev
->bus
&& dev
->bus
->pm
) {
651 domain
->ops
= *dev
->bus
->pm
;
652 domain
->ops
.runtime_suspend
= vga_switcheroo_runtime_suspend
;
653 domain
->ops
.runtime_resume
= vga_switcheroo_runtime_resume
;
655 dev
->pm_domain
= domain
;
658 dev
->pm_domain
= NULL
;
661 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops
);
663 void vga_switcheroo_fini_domain_pm_ops(struct device
*dev
)
665 dev
->pm_domain
= NULL
;
667 EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops
);
669 static int vga_switcheroo_runtime_resume_hdmi_audio(struct device
*dev
)
671 struct pci_dev
*pdev
= to_pci_dev(dev
);
673 struct vga_switcheroo_client
*client
, *found
= NULL
;
675 /* we need to check if we have to switch back on the video
676 device so the audio device can come back */
677 list_for_each_entry(client
, &vgasr_priv
.clients
, list
) {
678 if (PCI_SLOT(client
->pdev
->devfn
) == PCI_SLOT(pdev
->devfn
) && client_is_vga(client
)) {
680 ret
= pm_runtime_get_sync(&client
->pdev
->dev
);
688 ret
= dev
->bus
->pm
->runtime_resume(dev
);
690 /* put the reference for the gpu */
692 pm_runtime_mark_last_busy(&found
->pdev
->dev
);
693 pm_runtime_put_autosuspend(&found
->pdev
->dev
);
698 int vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device
*dev
, struct dev_pm_domain
*domain
)
700 /* copy over all the bus versions */
701 if (dev
->bus
&& dev
->bus
->pm
) {
702 domain
->ops
= *dev
->bus
->pm
;
703 domain
->ops
.runtime_resume
= vga_switcheroo_runtime_resume_hdmi_audio
;
705 dev
->pm_domain
= domain
;
708 dev
->pm_domain
= NULL
;
711 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio
);