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/dmi.h>
22 #include <linux/seq_file.h>
23 #include <linux/uaccess.h>
25 #include <linux/debugfs.h>
28 #include <linux/pci.h>
29 #include <linux/vga_switcheroo.h>
31 #include <linux/vgaarb.h>
33 struct vga_switcheroo_client {
35 struct fb_info *fb_info;
37 const struct vga_switcheroo_client_ops *ops;
40 struct list_head list;
43 static DEFINE_MUTEX(vgasr_mutex);
48 bool delayed_switch_active;
49 enum vga_switcheroo_client_id delayed_client_id;
51 struct dentry *debugfs_root;
52 struct dentry *switch_file;
54 int registered_clients;
55 struct list_head clients;
57 struct vga_switcheroo_handler *handler;
60 #define ID_BIT_AUDIO 0x100
61 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
62 #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
63 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
65 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
66 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
68 /* only one switcheroo per system */
69 static struct vgasr_priv vgasr_priv = {
70 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
73 int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
75 mutex_lock(&vgasr_mutex);
76 if (vgasr_priv.handler) {
77 mutex_unlock(&vgasr_mutex);
81 vgasr_priv.handler = handler;
82 mutex_unlock(&vgasr_mutex);
85 EXPORT_SYMBOL(vga_switcheroo_register_handler);
87 void vga_switcheroo_unregister_handler(void)
89 mutex_lock(&vgasr_mutex);
90 vgasr_priv.handler = NULL;
91 mutex_unlock(&vgasr_mutex);
93 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
95 static void vga_switcheroo_enable(void)
98 struct vga_switcheroo_client *client;
100 /* call the handler to init */
101 vgasr_priv.handler->init();
103 list_for_each_entry(client, &vgasr_priv.clients, list) {
104 if (client->id != -1)
106 ret = vgasr_priv.handler->get_client_id(client->pdev);
112 vga_switcheroo_debugfs_init(&vgasr_priv);
113 vgasr_priv.active = true;
116 static int register_client(struct pci_dev *pdev,
117 const struct vga_switcheroo_client_ops *ops,
120 struct vga_switcheroo_client *client;
122 client = kzalloc(sizeof(*client), GFP_KERNEL);
126 client->pwr_state = VGA_SWITCHEROO_ON;
130 client->active = active;
132 mutex_lock(&vgasr_mutex);
133 list_add_tail(&client->list, &vgasr_priv.clients);
134 if (client_is_vga(client))
135 vgasr_priv.registered_clients++;
137 /* if we get two clients + handler */
138 if (!vgasr_priv.active &&
139 vgasr_priv.registered_clients == 2 && vgasr_priv.handler) {
140 printk(KERN_INFO "vga_switcheroo: enabled\n");
141 vga_switcheroo_enable();
143 mutex_unlock(&vgasr_mutex);
147 int vga_switcheroo_register_client(struct pci_dev *pdev,
148 const struct vga_switcheroo_client_ops *ops)
150 return register_client(pdev, ops, -1,
151 pdev == vga_default_device());
153 EXPORT_SYMBOL(vga_switcheroo_register_client);
155 int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
156 const struct vga_switcheroo_client_ops *ops,
159 return register_client(pdev, ops, id | ID_BIT_AUDIO, active);
161 EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
163 static struct vga_switcheroo_client *
164 find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
166 struct vga_switcheroo_client *client;
167 list_for_each_entry(client, head, list)
168 if (client->pdev == pdev)
173 static struct vga_switcheroo_client *
174 find_client_from_id(struct list_head *head, int client_id)
176 struct vga_switcheroo_client *client;
177 list_for_each_entry(client, head, list)
178 if (client->id == client_id)
183 static struct vga_switcheroo_client *
184 find_active_client(struct list_head *head)
186 struct vga_switcheroo_client *client;
187 list_for_each_entry(client, head, list)
188 if (client->active && client_is_vga(client))
193 void vga_switcheroo_unregister_client(struct pci_dev *pdev)
195 struct vga_switcheroo_client *client;
197 mutex_lock(&vgasr_mutex);
198 client = find_client_from_pci(&vgasr_priv.clients, pdev);
200 if (client_is_vga(client))
201 vgasr_priv.registered_clients--;
202 list_del(&client->list);
205 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
206 printk(KERN_INFO "vga_switcheroo: disabled\n");
207 vga_switcheroo_debugfs_fini(&vgasr_priv);
208 vgasr_priv.active = false;
210 mutex_unlock(&vgasr_mutex);
212 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
214 void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
215 struct fb_info *info)
217 struct vga_switcheroo_client *client;
219 mutex_lock(&vgasr_mutex);
220 client = find_client_from_pci(&vgasr_priv.clients, pdev);
222 client->fb_info = info;
223 mutex_unlock(&vgasr_mutex);
225 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
227 static int vga_switcheroo_show(struct seq_file *m, void *v)
229 struct vga_switcheroo_client *client;
231 mutex_lock(&vgasr_mutex);
232 list_for_each_entry(client, &vgasr_priv.clients, list) {
233 seq_printf(m, "%d:%s%s:%c:%s:%s\n", i,
234 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
235 client_is_vga(client) ? "" : "-Audio",
236 client->active ? '+' : ' ',
237 client->pwr_state ? "Pwr" : "Off",
238 pci_name(client->pdev));
241 mutex_unlock(&vgasr_mutex);
245 static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
247 return single_open(file, vga_switcheroo_show, NULL);
250 static int vga_switchon(struct vga_switcheroo_client *client)
252 if (vgasr_priv.handler->power_state)
253 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
254 /* call the driver callback to turn on device */
255 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
256 client->pwr_state = VGA_SWITCHEROO_ON;
260 static int vga_switchoff(struct vga_switcheroo_client *client)
262 /* call the driver callback to turn off device */
263 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
264 if (vgasr_priv.handler->power_state)
265 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
266 client->pwr_state = VGA_SWITCHEROO_OFF;
270 static void set_audio_state(int id, int state)
272 struct vga_switcheroo_client *client;
274 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
275 if (client && client->pwr_state != state) {
276 client->ops->set_gpu_state(client->pdev, state);
277 client->pwr_state = state;
281 /* stage one happens before delay */
282 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
284 struct vga_switcheroo_client *active;
286 active = find_active_client(&vgasr_priv.clients);
290 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
291 vga_switchon(new_client);
293 vga_set_default_device(new_client->pdev);
294 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
300 static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
303 struct vga_switcheroo_client *active;
305 active = find_active_client(&vgasr_priv.clients);
309 active->active = false;
311 if (new_client->fb_info) {
312 struct fb_event event;
313 event.info = new_client->fb_info;
314 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
317 ret = vgasr_priv.handler->switchto(new_client->id);
321 if (new_client->ops->reprobe)
322 new_client->ops->reprobe(new_client->pdev);
324 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
326 if (active->pwr_state == VGA_SWITCHEROO_ON)
327 vga_switchoff(active);
329 new_client->active = true;
333 static bool check_can_switch(void)
335 struct vga_switcheroo_client *client;
337 list_for_each_entry(client, &vgasr_priv.clients, list) {
338 if (!client->ops->can_switch(client->pdev)) {
339 printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id);
347 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
348 size_t cnt, loff_t *ppos)
351 const char *pdev_name;
353 bool delay = false, can_switch;
354 bool just_mux = false;
356 struct vga_switcheroo_client *client = NULL;
361 if (copy_from_user(usercmd, ubuf, cnt))
364 mutex_lock(&vgasr_mutex);
366 if (!vgasr_priv.active) {
371 /* pwr off the device not in use */
372 if (strncmp(usercmd, "OFF", 3) == 0) {
373 list_for_each_entry(client, &vgasr_priv.clients, list) {
376 if (client->pwr_state == VGA_SWITCHEROO_ON)
377 vga_switchoff(client);
381 /* pwr on the device not in use */
382 if (strncmp(usercmd, "ON", 2) == 0) {
383 list_for_each_entry(client, &vgasr_priv.clients, list) {
386 if (client->pwr_state == VGA_SWITCHEROO_OFF)
387 vga_switchon(client);
392 /* request a delayed switch - test can we switch now */
393 if (strncmp(usercmd, "DIGD", 4) == 0) {
394 client_id = VGA_SWITCHEROO_IGD;
398 if (strncmp(usercmd, "DDIS", 4) == 0) {
399 client_id = VGA_SWITCHEROO_DIS;
403 if (strncmp(usercmd, "IGD", 3) == 0)
404 client_id = VGA_SWITCHEROO_IGD;
406 if (strncmp(usercmd, "DIS", 3) == 0)
407 client_id = VGA_SWITCHEROO_DIS;
409 if (strncmp(usercmd, "MIGD", 4) == 0) {
411 client_id = VGA_SWITCHEROO_IGD;
413 if (strncmp(usercmd, "MDIS", 4) == 0) {
415 client_id = VGA_SWITCHEROO_DIS;
420 client = find_client_from_id(&vgasr_priv.clients, client_id);
424 vgasr_priv.delayed_switch_active = false;
427 ret = vgasr_priv.handler->switchto(client_id);
434 /* okay we want a switch - test if devices are willing to switch */
435 can_switch = check_can_switch();
437 if (can_switch == false && delay == false)
441 pdev_name = pci_name(client->pdev);
442 ret = vga_switchto_stage1(client);
444 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
446 ret = vga_switchto_stage2(client);
448 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
451 printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
452 vgasr_priv.delayed_switch_active = true;
453 vgasr_priv.delayed_client_id = client_id;
455 ret = vga_switchto_stage1(client);
457 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
461 mutex_unlock(&vgasr_mutex);
465 static const struct file_operations vga_switcheroo_debugfs_fops = {
466 .owner = THIS_MODULE,
467 .open = vga_switcheroo_debugfs_open,
468 .write = vga_switcheroo_debugfs_write,
471 .release = single_release,
474 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
476 if (priv->switch_file) {
477 debugfs_remove(priv->switch_file);
478 priv->switch_file = NULL;
480 if (priv->debugfs_root) {
481 debugfs_remove(priv->debugfs_root);
482 priv->debugfs_root = NULL;
486 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
488 /* already initialised */
489 if (priv->debugfs_root)
491 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
493 if (!priv->debugfs_root) {
494 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
498 priv->switch_file = debugfs_create_file("switch", 0644,
499 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
500 if (!priv->switch_file) {
501 printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
506 vga_switcheroo_debugfs_fini(priv);
510 int vga_switcheroo_process_delayed_switch(void)
512 struct vga_switcheroo_client *client;
513 const char *pdev_name;
517 mutex_lock(&vgasr_mutex);
518 if (!vgasr_priv.delayed_switch_active)
521 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
523 client = find_client_from_id(&vgasr_priv.clients,
524 vgasr_priv.delayed_client_id);
525 if (!client || !check_can_switch())
528 pdev_name = pci_name(client->pdev);
529 ret = vga_switchto_stage2(client);
531 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
533 vgasr_priv.delayed_switch_active = false;
536 mutex_unlock(&vgasr_mutex);
539 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);