blob: 37fe2463f9b0895612720710486c0142bacb8789 [file] [log] [blame]
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001/*
2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
4 *
5 *
6 * Licensed under GPLv2
7 *
8 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
9
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
13
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
18 */
19
20#include <linux/module.h>
21#include <linux/dmi.h>
22#include <linux/seq_file.h>
23#include <linux/uaccess.h>
24#include <linux/fs.h>
25#include <linux/debugfs.h>
26#include <linux/fb.h>
27
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100028#include <linux/pci.h>
Dave Airlie726cc912013-01-25 11:38:56 +100029#include <linux/console.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100030#include <linux/vga_switcheroo.h>
31
32struct vga_switcheroo_client {
33 struct pci_dev *pdev;
34 struct fb_info *fb_info;
35 int pwr_state;
36 void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state);
Dave Airlie8d608aa2010-12-07 08:57:57 +100037 void (*reprobe)(struct pci_dev *pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100038 bool (*can_switch)(struct pci_dev *pdev);
39 int id;
40 bool active;
41};
42
43static DEFINE_MUTEX(vgasr_mutex);
44
45struct vgasr_priv {
46
47 bool active;
48 bool delayed_switch_active;
49 enum vga_switcheroo_client_id delayed_client_id;
50
51 struct dentry *debugfs_root;
52 struct dentry *switch_file;
53
54 int registered_clients;
55 struct vga_switcheroo_client clients[VGA_SWITCHEROO_MAX_CLIENTS];
56
57 struct vga_switcheroo_handler *handler;
58};
59
60static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
61static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
62
63/* only one switcheroo per system */
64static struct vgasr_priv vgasr_priv;
65
66int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
67{
68 mutex_lock(&vgasr_mutex);
69 if (vgasr_priv.handler) {
70 mutex_unlock(&vgasr_mutex);
71 return -EINVAL;
72 }
73
74 vgasr_priv.handler = handler;
75 mutex_unlock(&vgasr_mutex);
76 return 0;
77}
78EXPORT_SYMBOL(vga_switcheroo_register_handler);
79
80void vga_switcheroo_unregister_handler(void)
81{
82 mutex_lock(&vgasr_mutex);
83 vgasr_priv.handler = NULL;
84 mutex_unlock(&vgasr_mutex);
85}
86EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
87
88static void vga_switcheroo_enable(void)
89{
90 int i;
91 int ret;
92 /* call the handler to init */
93 vgasr_priv.handler->init();
94
95 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
96 ret = vgasr_priv.handler->get_client_id(vgasr_priv.clients[i].pdev);
97 if (ret < 0)
98 return;
99
100 vgasr_priv.clients[i].id = ret;
101 }
102 vga_switcheroo_debugfs_init(&vgasr_priv);
103 vgasr_priv.active = true;
104}
105
106int vga_switcheroo_register_client(struct pci_dev *pdev,
107 void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
Dave Airlie8d608aa2010-12-07 08:57:57 +1000108 void (*reprobe)(struct pci_dev *pdev),
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000109 bool (*can_switch)(struct pci_dev *pdev))
110{
111 int index;
112
113 mutex_lock(&vgasr_mutex);
114 /* don't do IGD vs DIS here */
115 if (vgasr_priv.registered_clients & 1)
116 index = 1;
117 else
118 index = 0;
119
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;
Dave Airlie8d608aa2010-12-07 08:57:57 +1000123 vgasr_priv.clients[index].reprobe = reprobe;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000124 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;
128
129 vgasr_priv.registered_clients |= (1 << index);
130
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();
135 }
136 mutex_unlock(&vgasr_mutex);
137 return 0;
138}
139EXPORT_SYMBOL(vga_switcheroo_register_client);
140
141void vga_switcheroo_unregister_client(struct pci_dev *pdev)
142{
143 int i;
144
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);
149 break;
150 }
151 }
152
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);
157}
158EXPORT_SYMBOL(vga_switcheroo_unregister_client);
159
160void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
161 struct fb_info *info)
162{
163 int i;
164
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;
169 break;
170 }
171 }
172 mutex_unlock(&vgasr_mutex);
173}
174EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
175
176static int vga_switcheroo_show(struct seq_file *m, void *v)
177{
178 int i;
179 mutex_lock(&vgasr_mutex);
180 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
Dave Airlie6c2df402010-12-06 12:30:31 +1000181 seq_printf(m, "%d:%s:%c:%s:%s\n", i,
182 vgasr_priv.clients[i].id == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000183 vgasr_priv.clients[i].active ? '+' : ' ',
184 vgasr_priv.clients[i].pwr_state ? "Pwr" : "Off",
185 pci_name(vgasr_priv.clients[i].pdev));
186 }
187 mutex_unlock(&vgasr_mutex);
188 return 0;
189}
190
191static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
192{
193 return single_open(file, vga_switcheroo_show, NULL);
194}
195
196static int vga_switchon(struct vga_switcheroo_client *client)
197{
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000198 if (vgasr_priv.handler->power_state)
199 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000200 /* 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;
203 return 0;
204}
205
206static int vga_switchoff(struct vga_switcheroo_client *client)
207{
208 /* call the driver callback to turn off device */
209 client->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000210 if (vgasr_priv.handler->power_state)
211 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000212 client->pwr_state = VGA_SWITCHEROO_OFF;
213 return 0;
214}
215
Dave Airlie66b37c62010-12-07 14:24:25 +1000216/* stage one happens before delay */
217static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000218{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000219 int i;
220 struct vga_switcheroo_client *active = NULL;
221
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000222 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
223 if (vgasr_priv.clients[i].active == true) {
224 active = &vgasr_priv.clients[i];
225 break;
226 }
227 }
228 if (!active)
229 return 0;
230
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000231 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
232 vga_switchon(new_client);
233
234 /* swap shadow resource to denote boot VGA device has changed so X starts on new device */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000235 active->pdev->resource[PCI_ROM_RESOURCE].flags &= ~IORESOURCE_ROM_SHADOW;
236 new_client->pdev->resource[PCI_ROM_RESOURCE].flags |= IORESOURCE_ROM_SHADOW;
Dave Airlie66b37c62010-12-07 14:24:25 +1000237 return 0;
238}
239
240/* post delay */
241static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
242{
243 int ret;
244 int i;
245 struct vga_switcheroo_client *active = NULL;
246
247 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
248 if (vgasr_priv.clients[i].active == true) {
249 active = &vgasr_priv.clients[i];
250 break;
251 }
252 }
253 if (!active)
254 return 0;
255
256 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000257
258 if (new_client->fb_info) {
259 struct fb_event event;
Dave Airlie726cc912013-01-25 11:38:56 +1000260 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000261 event.info = new_client->fb_info;
262 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie726cc912013-01-25 11:38:56 +1000263 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000264 }
265
266 ret = vgasr_priv.handler->switchto(new_client->id);
267 if (ret)
268 return ret;
269
Dave Airlie8d608aa2010-12-07 08:57:57 +1000270 if (new_client->reprobe)
271 new_client->reprobe(new_client->pdev);
272
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000273 if (active->pwr_state == VGA_SWITCHEROO_ON)
274 vga_switchoff(active);
275
276 new_client->active = true;
277 return 0;
278}
279
280static ssize_t
281vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
282 size_t cnt, loff_t *ppos)
283{
284 char usercmd[64];
285 const char *pdev_name;
286 int i, ret;
287 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000288 bool just_mux = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000289 int client_id = -1;
290 struct vga_switcheroo_client *client = NULL;
291
292 if (cnt > 63)
293 cnt = 63;
294
295 if (copy_from_user(usercmd, ubuf, cnt))
296 return -EFAULT;
297
298 mutex_lock(&vgasr_mutex);
299
Jiri Slaby8c88e502010-04-27 14:11:03 -0700300 if (!vgasr_priv.active) {
301 cnt = -EINVAL;
302 goto out;
303 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000304
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)
309 continue;
310 if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_ON)
311 vga_switchoff(&vgasr_priv.clients[i]);
312 }
313 goto out;
314 }
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)
319 continue;
320 if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_OFF)
321 vga_switchon(&vgasr_priv.clients[i]);
322 }
323 goto out;
324 }
325
326 /* request a delayed switch - test can we switch now */
327 if (strncmp(usercmd, "DIGD", 4) == 0) {
328 client_id = VGA_SWITCHEROO_IGD;
329 delay = true;
330 }
331
332 if (strncmp(usercmd, "DDIS", 4) == 0) {
333 client_id = VGA_SWITCHEROO_DIS;
334 delay = true;
335 }
336
337 if (strncmp(usercmd, "IGD", 3) == 0)
338 client_id = VGA_SWITCHEROO_IGD;
339
340 if (strncmp(usercmd, "DIS", 3) == 0)
341 client_id = VGA_SWITCHEROO_DIS;
342
Dan Carpenterfea6f332011-01-07 08:12:27 +0300343 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000344 just_mux = true;
345 client_id = VGA_SWITCHEROO_IGD;
346 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300347 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000348 just_mux = true;
349 client_id = VGA_SWITCHEROO_DIS;
350 }
351
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000352 if (client_id == -1)
353 goto out;
354
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];
358 break;
359 }
360 }
361
362 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000363
364 if (just_mux) {
365 ret = vgasr_priv.handler->switchto(client_id);
366 goto out;
367 }
368
Florian Micklera67b8882011-05-15 16:32:50 +0200369 if (client->active == true)
370 goto out;
371
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000372 /* okay we want a switch - test if devices are willing to switch */
373 can_switch = true;
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);
378 break;
379 }
380 }
381
382 if (can_switch == false && delay == false)
383 goto out;
384
385 if (can_switch == true) {
386 pdev_name = pci_name(client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000387 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000388 if (ret)
Dave Airlie66b37c62010-12-07 14:24:25 +1000389 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
390
391 ret = vga_switchto_stage2(client);
392 if (ret)
393 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
394
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000395 } else {
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;
399
Dave Airlie66b37c62010-12-07 14:24:25 +1000400 ret = vga_switchto_stage1(client);
401 if (ret)
402 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000403 }
404
405out:
406 mutex_unlock(&vgasr_mutex);
407 return cnt;
408}
409
410static const struct file_operations vga_switcheroo_debugfs_fops = {
411 .owner = THIS_MODULE,
412 .open = vga_switcheroo_debugfs_open,
413 .write = vga_switcheroo_debugfs_write,
414 .read = seq_read,
415 .llseek = seq_lseek,
416 .release = single_release,
417};
418
419static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
420{
421 if (priv->switch_file) {
422 debugfs_remove(priv->switch_file);
423 priv->switch_file = NULL;
424 }
425 if (priv->debugfs_root) {
426 debugfs_remove(priv->debugfs_root);
427 priv->debugfs_root = NULL;
428 }
429}
430
431static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
432{
433 /* already initialised */
434 if (priv->debugfs_root)
435 return 0;
436 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
437
438 if (!priv->debugfs_root) {
439 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
440 goto fail;
441 }
442
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");
447 goto fail;
448 }
449 return 0;
450fail:
451 vga_switcheroo_debugfs_fini(priv);
452 return -1;
453}
454
455int vga_switcheroo_process_delayed_switch(void)
456{
457 struct vga_switcheroo_client *client = NULL;
458 const char *pdev_name;
459 bool can_switch = true;
460 int i;
461 int ret;
462 int err = -EINVAL;
463
464 mutex_lock(&vgasr_mutex);
465 if (!vgasr_priv.delayed_switch_active)
466 goto err;
467
468 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
469
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);
476 break;
477 }
478 }
479
480 if (can_switch == false || client == NULL)
481 goto err;
482
483 pdev_name = pci_name(client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000484 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000485 if (ret)
Dave Airlie66b37c62010-12-07 14:24:25 +1000486 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000487
488 vgasr_priv.delayed_switch_active = false;
489 err = 0;
490err:
491 mutex_unlock(&vgasr_mutex);
492 return err;
493}
494EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
495