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/vga_switcheroo.h>
30 #include <linux/vgaarb.h>
32 struct vga_switcheroo_client {
34 struct fb_info *fb_info;
36 const struct vga_switcheroo_client_ops *ops;
39 struct list_head list;
42 static DEFINE_MUTEX(vgasr_mutex);
47 bool delayed_switch_active;
48 enum vga_switcheroo_client_id delayed_client_id;
50 struct dentry *debugfs_root;
51 struct dentry *switch_file;
53 int registered_clients;
54 struct list_head clients;
56 struct vga_switcheroo_handler *handler;
59 #define ID_BIT_AUDIO 0x100
60 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
61 #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
62 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
64 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
65 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
67 /* only one switcheroo per system */
68 static struct vgasr_priv vgasr_priv = {
69 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
72 static bool vga_switcheroo_ready(void)
74 /* we're ready if we get two clients + handler */
75 return !vgasr_priv.active &&
76 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
79 static void vga_switcheroo_enable(void)
82 struct vga_switcheroo_client *client;
84 /* call the handler to init */
85 if (vgasr_priv.handler->init)
86 vgasr_priv.handler->init();
88 list_for_each_entry(client, &vgasr_priv.clients, list) {
91 ret = vgasr_priv.handler->get_client_id(client->pdev);
97 vga_switcheroo_debugfs_init(&vgasr_priv);
98 vgasr_priv.active = true;
101 int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
103 mutex_lock(&vgasr_mutex);
104 if (vgasr_priv.handler) {
105 mutex_unlock(&vgasr_mutex);
109 vgasr_priv.handler = handler;
110 if (vga_switcheroo_ready()) {
111 printk(KERN_INFO "vga_switcheroo: enabled\n");
112 vga_switcheroo_enable();
114 mutex_unlock(&vgasr_mutex);
117 EXPORT_SYMBOL(vga_switcheroo_register_handler);
119 void vga_switcheroo_unregister_handler(void)
121 mutex_lock(&vgasr_mutex);
122 vgasr_priv.handler = NULL;
123 if (vgasr_priv.active) {
124 pr_info("vga_switcheroo: disabled\n");
125 vga_switcheroo_debugfs_fini(&vgasr_priv);
126 vgasr_priv.active = false;
128 mutex_unlock(&vgasr_mutex);
130 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
132 static int register_client(struct pci_dev *pdev,
133 const struct vga_switcheroo_client_ops *ops,
136 struct vga_switcheroo_client *client;
138 client = kzalloc(sizeof(*client), GFP_KERNEL);
142 client->pwr_state = VGA_SWITCHEROO_ON;
146 client->active = active;
148 mutex_lock(&vgasr_mutex);
149 list_add_tail(&client->list, &vgasr_priv.clients);
150 if (client_is_vga(client))
151 vgasr_priv.registered_clients++;
153 if (vga_switcheroo_ready()) {
154 printk(KERN_INFO "vga_switcheroo: enabled\n");
155 vga_switcheroo_enable();
157 mutex_unlock(&vgasr_mutex);
161 int vga_switcheroo_register_client(struct pci_dev *pdev,
162 const struct vga_switcheroo_client_ops *ops)
164 return register_client(pdev, ops, -1,
165 pdev == vga_default_device());
167 EXPORT_SYMBOL(vga_switcheroo_register_client);
169 int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
170 const struct vga_switcheroo_client_ops *ops,
173 return register_client(pdev, ops, id | ID_BIT_AUDIO, active);
175 EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
177 static struct vga_switcheroo_client *
178 find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
180 struct vga_switcheroo_client *client;
181 list_for_each_entry(client, head, list)
182 if (client->pdev == pdev)
187 static struct vga_switcheroo_client *
188 find_client_from_id(struct list_head *head, int client_id)
190 struct vga_switcheroo_client *client;
191 list_for_each_entry(client, head, list)
192 if (client->id == client_id)
197 static struct vga_switcheroo_client *
198 find_active_client(struct list_head *head)
200 struct vga_switcheroo_client *client;
201 list_for_each_entry(client, head, list)
202 if (client->active && client_is_vga(client))
207 int vga_switcheroo_get_client_state(struct pci_dev *pdev)
209 struct vga_switcheroo_client *client;
211 client = find_client_from_pci(&vgasr_priv.clients, pdev);
213 return VGA_SWITCHEROO_NOT_FOUND;
214 if (!vgasr_priv.active)
215 return VGA_SWITCHEROO_INIT;
216 return client->pwr_state;
218 EXPORT_SYMBOL(vga_switcheroo_get_client_state);
220 void vga_switcheroo_unregister_client(struct pci_dev *pdev)
222 struct vga_switcheroo_client *client;
224 mutex_lock(&vgasr_mutex);
225 client = find_client_from_pci(&vgasr_priv.clients, pdev);
227 if (client_is_vga(client))
228 vgasr_priv.registered_clients--;
229 list_del(&client->list);
232 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
233 printk(KERN_INFO "vga_switcheroo: disabled\n");
234 vga_switcheroo_debugfs_fini(&vgasr_priv);
235 vgasr_priv.active = false;
237 mutex_unlock(&vgasr_mutex);
239 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
241 void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
242 struct fb_info *info)
244 struct vga_switcheroo_client *client;
246 mutex_lock(&vgasr_mutex);
247 client = find_client_from_pci(&vgasr_priv.clients, pdev);
249 client->fb_info = info;
250 mutex_unlock(&vgasr_mutex);
252 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
254 static int vga_switcheroo_show(struct seq_file *m, void *v)
256 struct vga_switcheroo_client *client;
258 mutex_lock(&vgasr_mutex);
259 list_for_each_entry(client, &vgasr_priv.clients, list) {
260 seq_printf(m, "%d:%s%s:%c:%s:%s\n", i,
261 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
262 client_is_vga(client) ? "" : "-Audio",
263 client->active ? '+' : ' ',
264 client->pwr_state ? "Pwr" : "Off",
265 pci_name(client->pdev));
268 mutex_unlock(&vgasr_mutex);
272 static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
274 return single_open(file, vga_switcheroo_show, NULL);
277 static int vga_switchon(struct vga_switcheroo_client *client)
279 if (vgasr_priv.handler->power_state)
280 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
281 /* call the driver callback to turn on device */
282 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
283 client->pwr_state = VGA_SWITCHEROO_ON;
287 static int vga_switchoff(struct vga_switcheroo_client *client)
289 /* call the driver callback to turn off device */
290 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
291 if (vgasr_priv.handler->power_state)
292 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
293 client->pwr_state = VGA_SWITCHEROO_OFF;
297 static void set_audio_state(int id, int state)
299 struct vga_switcheroo_client *client;
301 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
302 if (client && client->pwr_state != state) {
303 client->ops->set_gpu_state(client->pdev, state);
304 client->pwr_state = state;
308 /* stage one happens before delay */
309 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
311 struct vga_switcheroo_client *active;
313 active = find_active_client(&vgasr_priv.clients);
317 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
318 vga_switchon(new_client);
320 vga_set_default_device(new_client->pdev);
325 static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
328 struct vga_switcheroo_client *active;
330 active = find_active_client(&vgasr_priv.clients);
334 active->active = false;
336 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
338 if (new_client->fb_info) {
339 struct fb_event event;
340 event.info = new_client->fb_info;
341 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
344 ret = vgasr_priv.handler->switchto(new_client->id);
348 if (new_client->ops->reprobe)
349 new_client->ops->reprobe(new_client->pdev);
351 if (active->pwr_state == VGA_SWITCHEROO_ON)
352 vga_switchoff(active);
354 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
356 new_client->active = true;
360 static bool check_can_switch(void)
362 struct vga_switcheroo_client *client;
364 list_for_each_entry(client, &vgasr_priv.clients, list) {
365 if (!client->ops->can_switch(client->pdev)) {
366 printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id);
374 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
375 size_t cnt, loff_t *ppos)
379 bool delay = false, can_switch;
380 bool just_mux = false;
382 struct vga_switcheroo_client *client = NULL;
387 if (copy_from_user(usercmd, ubuf, cnt))
390 mutex_lock(&vgasr_mutex);
392 if (!vgasr_priv.active) {
397 /* pwr off the device not in use */
398 if (strncmp(usercmd, "OFF", 3) == 0) {
399 list_for_each_entry(client, &vgasr_priv.clients, list) {
400 if (client->active || client_is_audio(client))
402 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
403 if (client->pwr_state == VGA_SWITCHEROO_ON)
404 vga_switchoff(client);
408 /* pwr on the device not in use */
409 if (strncmp(usercmd, "ON", 2) == 0) {
410 list_for_each_entry(client, &vgasr_priv.clients, list) {
411 if (client->active || client_is_audio(client))
413 if (client->pwr_state == VGA_SWITCHEROO_OFF)
414 vga_switchon(client);
415 set_audio_state(client->id, VGA_SWITCHEROO_ON);
420 /* request a delayed switch - test can we switch now */
421 if (strncmp(usercmd, "DIGD", 4) == 0) {
422 client_id = VGA_SWITCHEROO_IGD;
426 if (strncmp(usercmd, "DDIS", 4) == 0) {
427 client_id = VGA_SWITCHEROO_DIS;
431 if (strncmp(usercmd, "IGD", 3) == 0)
432 client_id = VGA_SWITCHEROO_IGD;
434 if (strncmp(usercmd, "DIS", 3) == 0)
435 client_id = VGA_SWITCHEROO_DIS;
437 if (strncmp(usercmd, "MIGD", 4) == 0) {
439 client_id = VGA_SWITCHEROO_IGD;
441 if (strncmp(usercmd, "MDIS", 4) == 0) {
443 client_id = VGA_SWITCHEROO_DIS;
448 client = find_client_from_id(&vgasr_priv.clients, client_id);
452 vgasr_priv.delayed_switch_active = false;
455 ret = vgasr_priv.handler->switchto(client_id);
462 /* okay we want a switch - test if devices are willing to switch */
463 can_switch = check_can_switch();
465 if (can_switch == false && delay == false)
469 ret = vga_switchto_stage1(client);
471 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
473 ret = vga_switchto_stage2(client);
475 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
478 printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
479 vgasr_priv.delayed_switch_active = true;
480 vgasr_priv.delayed_client_id = client_id;
482 ret = vga_switchto_stage1(client);
484 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
488 mutex_unlock(&vgasr_mutex);
492 static const struct file_operations vga_switcheroo_debugfs_fops = {
493 .owner = THIS_MODULE,
494 .open = vga_switcheroo_debugfs_open,
495 .write = vga_switcheroo_debugfs_write,
498 .release = single_release,
501 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
503 if (priv->switch_file) {
504 debugfs_remove(priv->switch_file);
505 priv->switch_file = NULL;
507 if (priv->debugfs_root) {
508 debugfs_remove(priv->debugfs_root);
509 priv->debugfs_root = NULL;
513 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
515 /* already initialised */
516 if (priv->debugfs_root)
518 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
520 if (!priv->debugfs_root) {
521 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
525 priv->switch_file = debugfs_create_file("switch", 0644,
526 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
527 if (!priv->switch_file) {
528 printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
533 vga_switcheroo_debugfs_fini(priv);
537 int vga_switcheroo_process_delayed_switch(void)
539 struct vga_switcheroo_client *client;
543 mutex_lock(&vgasr_mutex);
544 if (!vgasr_priv.delayed_switch_active)
547 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
549 client = find_client_from_id(&vgasr_priv.clients,
550 vgasr_priv.delayed_client_id);
551 if (!client || !check_can_switch())
554 ret = vga_switchto_stage2(client);
556 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
558 vgasr_priv.delayed_switch_active = false;
561 mutex_unlock(&vgasr_mutex);
564 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);