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/console.h>
30 #include <linux/vga_switcheroo.h>
32 struct vga_switcheroo_client {
34 struct fb_info *fb_info;
36 void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state);
37 void (*reprobe)(struct pci_dev *pdev);
38 bool (*can_switch)(struct pci_dev *pdev);
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 vga_switcheroo_client clients[VGA_SWITCHEROO_MAX_CLIENTS];
57 struct vga_switcheroo_handler *handler;
60 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
61 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
63 /* only one switcheroo per system */
64 static struct vgasr_priv vgasr_priv;
66 int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
68 mutex_lock(&vgasr_mutex);
69 if (vgasr_priv.handler) {
70 mutex_unlock(&vgasr_mutex);
74 vgasr_priv.handler = handler;
75 mutex_unlock(&vgasr_mutex);
78 EXPORT_SYMBOL(vga_switcheroo_register_handler);
80 void vga_switcheroo_unregister_handler(void)
82 mutex_lock(&vgasr_mutex);
83 vgasr_priv.handler = NULL;
84 mutex_unlock(&vgasr_mutex);
86 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
88 static void vga_switcheroo_enable(void)
92 /* call the handler to init */
93 vgasr_priv.handler->init();
95 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
96 ret = vgasr_priv.handler->get_client_id(vgasr_priv.clients[i].pdev);
100 vgasr_priv.clients[i].id = ret;
102 vga_switcheroo_debugfs_init(&vgasr_priv);
103 vgasr_priv.active = true;
106 int vga_switcheroo_register_client(struct pci_dev *pdev,
107 void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
108 void (*reprobe)(struct pci_dev *pdev),
109 bool (*can_switch)(struct pci_dev *pdev))
113 mutex_lock(&vgasr_mutex);
114 /* don't do IGD vs DIS here */
115 if (vgasr_priv.registered_clients & 1)
120 vgasr_priv.clients[index].pwr_state = VGA_SWITCHEROO_ON;
121 vgasr_priv.clients[index].pdev = pdev;
122 vgasr_priv.clients[index].set_gpu_state = set_gpu_state;
123 vgasr_priv.clients[index].reprobe = reprobe;
124 vgasr_priv.clients[index].can_switch = can_switch;
125 vgasr_priv.clients[index].id = -1;
126 if (pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW)
127 vgasr_priv.clients[index].active = true;
129 vgasr_priv.registered_clients |= (1 << index);
131 /* if we get two clients + handler */
132 if (vgasr_priv.registered_clients == 0x3 && vgasr_priv.handler) {
133 printk(KERN_INFO "vga_switcheroo: enabled\n");
134 vga_switcheroo_enable();
136 mutex_unlock(&vgasr_mutex);
139 EXPORT_SYMBOL(vga_switcheroo_register_client);
141 void vga_switcheroo_unregister_client(struct pci_dev *pdev)
145 mutex_lock(&vgasr_mutex);
146 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
147 if (vgasr_priv.clients[i].pdev == pdev) {
148 vgasr_priv.registered_clients &= ~(1 << i);
153 printk(KERN_INFO "vga_switcheroo: disabled\n");
154 vga_switcheroo_debugfs_fini(&vgasr_priv);
155 vgasr_priv.active = false;
156 mutex_unlock(&vgasr_mutex);
158 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
160 void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
161 struct fb_info *info)
165 mutex_lock(&vgasr_mutex);
166 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
167 if (vgasr_priv.clients[i].pdev == pdev) {
168 vgasr_priv.clients[i].fb_info = info;
172 mutex_unlock(&vgasr_mutex);
174 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
176 static int vga_switcheroo_show(struct seq_file *m, void *v)
179 mutex_lock(&vgasr_mutex);
180 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
181 seq_printf(m, "%d:%s:%c:%s:%s\n", i,
182 vgasr_priv.clients[i].id == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
183 vgasr_priv.clients[i].active ? '+' : ' ',
184 vgasr_priv.clients[i].pwr_state ? "Pwr" : "Off",
185 pci_name(vgasr_priv.clients[i].pdev));
187 mutex_unlock(&vgasr_mutex);
191 static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
193 return single_open(file, vga_switcheroo_show, NULL);
196 static int vga_switchon(struct vga_switcheroo_client *client)
198 if (vgasr_priv.handler->power_state)
199 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
200 /* call the driver callback to turn on device */
201 client->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
202 client->pwr_state = VGA_SWITCHEROO_ON;
206 static int vga_switchoff(struct vga_switcheroo_client *client)
208 /* call the driver callback to turn off device */
209 client->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
210 if (vgasr_priv.handler->power_state)
211 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
212 client->pwr_state = VGA_SWITCHEROO_OFF;
216 /* stage one happens before delay */
217 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
220 struct vga_switcheroo_client *active = NULL;
222 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
223 if (vgasr_priv.clients[i].active == true) {
224 active = &vgasr_priv.clients[i];
231 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
232 vga_switchon(new_client);
234 /* swap shadow resource to denote boot VGA device has changed so X starts on new device */
235 active->pdev->resource[PCI_ROM_RESOURCE].flags &= ~IORESOURCE_ROM_SHADOW;
236 new_client->pdev->resource[PCI_ROM_RESOURCE].flags |= IORESOURCE_ROM_SHADOW;
241 static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
245 struct vga_switcheroo_client *active = NULL;
247 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
248 if (vgasr_priv.clients[i].active == true) {
249 active = &vgasr_priv.clients[i];
256 active->active = false;
258 if (new_client->fb_info) {
259 struct fb_event event;
261 event.info = new_client->fb_info;
262 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
266 ret = vgasr_priv.handler->switchto(new_client->id);
270 if (new_client->reprobe)
271 new_client->reprobe(new_client->pdev);
273 if (active->pwr_state == VGA_SWITCHEROO_ON)
274 vga_switchoff(active);
276 new_client->active = true;
281 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
282 size_t cnt, loff_t *ppos)
285 const char *pdev_name;
287 bool delay = false, can_switch;
288 bool just_mux = false;
290 struct vga_switcheroo_client *client = NULL;
295 if (copy_from_user(usercmd, ubuf, cnt))
298 mutex_lock(&vgasr_mutex);
300 if (!vgasr_priv.active) {
305 /* pwr off the device not in use */
306 if (strncmp(usercmd, "OFF", 3) == 0) {
307 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
308 if (vgasr_priv.clients[i].active)
310 if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_ON)
311 vga_switchoff(&vgasr_priv.clients[i]);
315 /* pwr on the device not in use */
316 if (strncmp(usercmd, "ON", 2) == 0) {
317 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
318 if (vgasr_priv.clients[i].active)
320 if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_OFF)
321 vga_switchon(&vgasr_priv.clients[i]);
326 /* request a delayed switch - test can we switch now */
327 if (strncmp(usercmd, "DIGD", 4) == 0) {
328 client_id = VGA_SWITCHEROO_IGD;
332 if (strncmp(usercmd, "DDIS", 4) == 0) {
333 client_id = VGA_SWITCHEROO_DIS;
337 if (strncmp(usercmd, "IGD", 3) == 0)
338 client_id = VGA_SWITCHEROO_IGD;
340 if (strncmp(usercmd, "DIS", 3) == 0)
341 client_id = VGA_SWITCHEROO_DIS;
343 if (strncmp(usercmd, "MIGD", 4) == 0) {
345 client_id = VGA_SWITCHEROO_IGD;
347 if (strncmp(usercmd, "MDIS", 4) == 0) {
349 client_id = VGA_SWITCHEROO_DIS;
355 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
356 if (vgasr_priv.clients[i].id == client_id) {
357 client = &vgasr_priv.clients[i];
362 vgasr_priv.delayed_switch_active = false;
365 ret = vgasr_priv.handler->switchto(client_id);
369 if (client->active == true)
372 /* okay we want a switch - test if devices are willing to switch */
374 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
375 can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
376 if (can_switch == false) {
377 printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
382 if (can_switch == false && delay == false)
385 if (can_switch == true) {
386 pdev_name = pci_name(client->pdev);
387 ret = vga_switchto_stage1(client);
389 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
391 ret = vga_switchto_stage2(client);
393 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
396 printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
397 vgasr_priv.delayed_switch_active = true;
398 vgasr_priv.delayed_client_id = client_id;
400 ret = vga_switchto_stage1(client);
402 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
406 mutex_unlock(&vgasr_mutex);
410 static const struct file_operations vga_switcheroo_debugfs_fops = {
411 .owner = THIS_MODULE,
412 .open = vga_switcheroo_debugfs_open,
413 .write = vga_switcheroo_debugfs_write,
416 .release = single_release,
419 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
421 if (priv->switch_file) {
422 debugfs_remove(priv->switch_file);
423 priv->switch_file = NULL;
425 if (priv->debugfs_root) {
426 debugfs_remove(priv->debugfs_root);
427 priv->debugfs_root = NULL;
431 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
433 /* already initialised */
434 if (priv->debugfs_root)
436 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
438 if (!priv->debugfs_root) {
439 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
443 priv->switch_file = debugfs_create_file("switch", 0644,
444 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
445 if (!priv->switch_file) {
446 printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
451 vga_switcheroo_debugfs_fini(priv);
455 int vga_switcheroo_process_delayed_switch(void)
457 struct vga_switcheroo_client *client = NULL;
458 const char *pdev_name;
459 bool can_switch = true;
464 mutex_lock(&vgasr_mutex);
465 if (!vgasr_priv.delayed_switch_active)
468 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
470 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
471 if (vgasr_priv.clients[i].id == vgasr_priv.delayed_client_id)
472 client = &vgasr_priv.clients[i];
473 can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
474 if (can_switch == false) {
475 printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
480 if (can_switch == false || client == NULL)
483 pdev_name = pci_name(client->pdev);
484 ret = vga_switchto_stage2(client);
486 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
488 vgasr_priv.delayed_switch_active = false;
491 mutex_unlock(&vgasr_mutex);
494 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);