Merge tag 'drm-intel-next-2012-05-06-merged' of git://people.freedesktop.org/~danvet...
[deliverable/linux.git] / drivers / gpu / vga / vga_switcheroo.c
CommitLineData
6a9ee8af
DA
1/*
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
6a9ee8af
DA
28#include <linux/pci.h>
29#include <linux/vga_switcheroo.h>
30
2fbe8c7c
MG
31#include <linux/vgaarb.h>
32
6a9ee8af
DA
33struct vga_switcheroo_client {
34 struct pci_dev *pdev;
35 struct fb_info *fb_info;
36 int pwr_state;
37 void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state);
8d608aa6 38 void (*reprobe)(struct pci_dev *pdev);
6a9ee8af
DA
39 bool (*can_switch)(struct pci_dev *pdev);
40 int id;
41 bool active;
42};
43
44static DEFINE_MUTEX(vgasr_mutex);
45
46struct vgasr_priv {
47
48 bool active;
49 bool delayed_switch_active;
50 enum vga_switcheroo_client_id delayed_client_id;
51
52 struct dentry *debugfs_root;
53 struct dentry *switch_file;
54
55 int registered_clients;
56 struct vga_switcheroo_client clients[VGA_SWITCHEROO_MAX_CLIENTS];
57
58 struct vga_switcheroo_handler *handler;
59};
60
61static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
62static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
63
64/* only one switcheroo per system */
65static struct vgasr_priv vgasr_priv;
66
67int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
68{
69 mutex_lock(&vgasr_mutex);
70 if (vgasr_priv.handler) {
71 mutex_unlock(&vgasr_mutex);
72 return -EINVAL;
73 }
74
75 vgasr_priv.handler = handler;
76 mutex_unlock(&vgasr_mutex);
77 return 0;
78}
79EXPORT_SYMBOL(vga_switcheroo_register_handler);
80
81void vga_switcheroo_unregister_handler(void)
82{
83 mutex_lock(&vgasr_mutex);
84 vgasr_priv.handler = NULL;
85 mutex_unlock(&vgasr_mutex);
86}
87EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
88
89static void vga_switcheroo_enable(void)
90{
91 int i;
92 int ret;
93 /* call the handler to init */
94 vgasr_priv.handler->init();
95
96 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
97 ret = vgasr_priv.handler->get_client_id(vgasr_priv.clients[i].pdev);
98 if (ret < 0)
99 return;
100
101 vgasr_priv.clients[i].id = ret;
102 }
103 vga_switcheroo_debugfs_init(&vgasr_priv);
104 vgasr_priv.active = true;
105}
106
107int vga_switcheroo_register_client(struct pci_dev *pdev,
108 void (*set_gpu_state)(struct pci_dev *pdev, enum vga_switcheroo_state),
8d608aa6 109 void (*reprobe)(struct pci_dev *pdev),
6a9ee8af
DA
110 bool (*can_switch)(struct pci_dev *pdev))
111{
112 int index;
113
114 mutex_lock(&vgasr_mutex);
115 /* don't do IGD vs DIS here */
116 if (vgasr_priv.registered_clients & 1)
117 index = 1;
118 else
119 index = 0;
120
121 vgasr_priv.clients[index].pwr_state = VGA_SWITCHEROO_ON;
122 vgasr_priv.clients[index].pdev = pdev;
123 vgasr_priv.clients[index].set_gpu_state = set_gpu_state;
8d608aa6 124 vgasr_priv.clients[index].reprobe = reprobe;
6a9ee8af
DA
125 vgasr_priv.clients[index].can_switch = can_switch;
126 vgasr_priv.clients[index].id = -1;
2fbe8c7c 127 if (pdev == vga_default_device())
6a9ee8af
DA
128 vgasr_priv.clients[index].active = true;
129
130 vgasr_priv.registered_clients |= (1 << index);
131
132 /* if we get two clients + handler */
133 if (vgasr_priv.registered_clients == 0x3 && vgasr_priv.handler) {
134 printk(KERN_INFO "vga_switcheroo: enabled\n");
135 vga_switcheroo_enable();
136 }
137 mutex_unlock(&vgasr_mutex);
138 return 0;
139}
140EXPORT_SYMBOL(vga_switcheroo_register_client);
141
142void vga_switcheroo_unregister_client(struct pci_dev *pdev)
143{
144 int i;
145
146 mutex_lock(&vgasr_mutex);
147 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
148 if (vgasr_priv.clients[i].pdev == pdev) {
149 vgasr_priv.registered_clients &= ~(1 << i);
150 break;
151 }
152 }
153
154 printk(KERN_INFO "vga_switcheroo: disabled\n");
155 vga_switcheroo_debugfs_fini(&vgasr_priv);
156 vgasr_priv.active = false;
157 mutex_unlock(&vgasr_mutex);
158}
159EXPORT_SYMBOL(vga_switcheroo_unregister_client);
160
161void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
162 struct fb_info *info)
163{
164 int i;
165
166 mutex_lock(&vgasr_mutex);
167 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
168 if (vgasr_priv.clients[i].pdev == pdev) {
169 vgasr_priv.clients[i].fb_info = info;
170 break;
171 }
172 }
173 mutex_unlock(&vgasr_mutex);
174}
175EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
176
177static int vga_switcheroo_show(struct seq_file *m, void *v)
178{
179 int i;
180 mutex_lock(&vgasr_mutex);
181 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
6c2df40e
DA
182 seq_printf(m, "%d:%s:%c:%s:%s\n", i,
183 vgasr_priv.clients[i].id == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
6a9ee8af
DA
184 vgasr_priv.clients[i].active ? '+' : ' ',
185 vgasr_priv.clients[i].pwr_state ? "Pwr" : "Off",
186 pci_name(vgasr_priv.clients[i].pdev));
187 }
188 mutex_unlock(&vgasr_mutex);
189 return 0;
190}
191
192static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
193{
194 return single_open(file, vga_switcheroo_show, NULL);
195}
196
197static int vga_switchon(struct vga_switcheroo_client *client)
198{
5cfb3c3a
DA
199 if (vgasr_priv.handler->power_state)
200 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
6a9ee8af
DA
201 /* call the driver callback to turn on device */
202 client->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
203 client->pwr_state = VGA_SWITCHEROO_ON;
204 return 0;
205}
206
207static int vga_switchoff(struct vga_switcheroo_client *client)
208{
209 /* call the driver callback to turn off device */
210 client->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
5cfb3c3a
DA
211 if (vgasr_priv.handler->power_state)
212 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
6a9ee8af
DA
213 client->pwr_state = VGA_SWITCHEROO_OFF;
214 return 0;
215}
216
66b37c67
DA
217/* stage one happens before delay */
218static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
6a9ee8af 219{
6a9ee8af
DA
220 int i;
221 struct vga_switcheroo_client *active = NULL;
222
6a9ee8af
DA
223 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
224 if (vgasr_priv.clients[i].active == true) {
225 active = &vgasr_priv.clients[i];
226 break;
227 }
228 }
229 if (!active)
230 return 0;
231
6a9ee8af
DA
232 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
233 vga_switchon(new_client);
234
2fbe8c7c
MG
235 vga_set_default_device(new_client->pdev);
236
66b37c67
DA
237 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;
6a9ee8af
DA
257
258 if (new_client->fb_info) {
259 struct fb_event event;
260 event.info = new_client->fb_info;
261 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
262 }
263
264 ret = vgasr_priv.handler->switchto(new_client->id);
265 if (ret)
266 return ret;
267
8d608aa6
DA
268 if (new_client->reprobe)
269 new_client->reprobe(new_client->pdev);
270
6a9ee8af
DA
271 if (active->pwr_state == VGA_SWITCHEROO_ON)
272 vga_switchoff(active);
273
274 new_client->active = true;
275 return 0;
276}
277
278static ssize_t
279vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
280 size_t cnt, loff_t *ppos)
281{
282 char usercmd[64];
283 const char *pdev_name;
284 int i, ret;
285 bool delay = false, can_switch;
851ab954 286 bool just_mux = false;
6a9ee8af
DA
287 int client_id = -1;
288 struct vga_switcheroo_client *client = NULL;
289
290 if (cnt > 63)
291 cnt = 63;
292
293 if (copy_from_user(usercmd, ubuf, cnt))
294 return -EFAULT;
295
296 mutex_lock(&vgasr_mutex);
297
8c88e50b
JS
298 if (!vgasr_priv.active) {
299 cnt = -EINVAL;
300 goto out;
301 }
6a9ee8af
DA
302
303 /* pwr off the device not in use */
304 if (strncmp(usercmd, "OFF", 3) == 0) {
305 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
306 if (vgasr_priv.clients[i].active)
307 continue;
308 if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_ON)
309 vga_switchoff(&vgasr_priv.clients[i]);
310 }
311 goto out;
312 }
313 /* pwr on the device not in use */
314 if (strncmp(usercmd, "ON", 2) == 0) {
315 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
316 if (vgasr_priv.clients[i].active)
317 continue;
318 if (vgasr_priv.clients[i].pwr_state == VGA_SWITCHEROO_OFF)
319 vga_switchon(&vgasr_priv.clients[i]);
320 }
321 goto out;
322 }
323
324 /* request a delayed switch - test can we switch now */
325 if (strncmp(usercmd, "DIGD", 4) == 0) {
326 client_id = VGA_SWITCHEROO_IGD;
327 delay = true;
328 }
329
330 if (strncmp(usercmd, "DDIS", 4) == 0) {
331 client_id = VGA_SWITCHEROO_DIS;
332 delay = true;
333 }
334
335 if (strncmp(usercmd, "IGD", 3) == 0)
336 client_id = VGA_SWITCHEROO_IGD;
337
338 if (strncmp(usercmd, "DIS", 3) == 0)
339 client_id = VGA_SWITCHEROO_DIS;
340
fea6f330 341 if (strncmp(usercmd, "MIGD", 4) == 0) {
851ab954
DA
342 just_mux = true;
343 client_id = VGA_SWITCHEROO_IGD;
344 }
fea6f330 345 if (strncmp(usercmd, "MDIS", 4) == 0) {
851ab954
DA
346 just_mux = true;
347 client_id = VGA_SWITCHEROO_DIS;
348 }
349
6a9ee8af
DA
350 if (client_id == -1)
351 goto out;
352
353 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
354 if (vgasr_priv.clients[i].id == client_id) {
355 client = &vgasr_priv.clients[i];
356 break;
357 }
358 }
359
360 vgasr_priv.delayed_switch_active = false;
851ab954
DA
361
362 if (just_mux) {
363 ret = vgasr_priv.handler->switchto(client_id);
364 goto out;
365 }
366
a67b8887
FM
367 if (client->active == true)
368 goto out;
369
6a9ee8af
DA
370 /* okay we want a switch - test if devices are willing to switch */
371 can_switch = true;
372 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
373 can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
374 if (can_switch == false) {
375 printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
376 break;
377 }
378 }
379
380 if (can_switch == false && delay == false)
381 goto out;
382
383 if (can_switch == true) {
384 pdev_name = pci_name(client->pdev);
66b37c67
DA
385 ret = vga_switchto_stage1(client);
386 if (ret)
387 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
388
389 ret = vga_switchto_stage2(client);
6a9ee8af 390 if (ret)
66b37c67
DA
391 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
392
6a9ee8af
DA
393 } else {
394 printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
395 vgasr_priv.delayed_switch_active = true;
396 vgasr_priv.delayed_client_id = client_id;
397
66b37c67
DA
398 ret = vga_switchto_stage1(client);
399 if (ret)
400 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
6a9ee8af
DA
401 }
402
403out:
404 mutex_unlock(&vgasr_mutex);
405 return cnt;
406}
407
408static const struct file_operations vga_switcheroo_debugfs_fops = {
409 .owner = THIS_MODULE,
410 .open = vga_switcheroo_debugfs_open,
411 .write = vga_switcheroo_debugfs_write,
412 .read = seq_read,
413 .llseek = seq_lseek,
414 .release = single_release,
415};
416
417static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
418{
419 if (priv->switch_file) {
420 debugfs_remove(priv->switch_file);
421 priv->switch_file = NULL;
422 }
423 if (priv->debugfs_root) {
424 debugfs_remove(priv->debugfs_root);
425 priv->debugfs_root = NULL;
426 }
427}
428
429static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
430{
431 /* already initialised */
432 if (priv->debugfs_root)
433 return 0;
434 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
435
436 if (!priv->debugfs_root) {
437 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
438 goto fail;
439 }
440
441 priv->switch_file = debugfs_create_file("switch", 0644,
442 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
443 if (!priv->switch_file) {
444 printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
445 goto fail;
446 }
447 return 0;
448fail:
449 vga_switcheroo_debugfs_fini(priv);
450 return -1;
451}
452
453int vga_switcheroo_process_delayed_switch(void)
454{
455 struct vga_switcheroo_client *client = NULL;
456 const char *pdev_name;
457 bool can_switch = true;
458 int i;
459 int ret;
460 int err = -EINVAL;
461
462 mutex_lock(&vgasr_mutex);
463 if (!vgasr_priv.delayed_switch_active)
464 goto err;
465
466 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
467
468 for (i = 0; i < VGA_SWITCHEROO_MAX_CLIENTS; i++) {
469 if (vgasr_priv.clients[i].id == vgasr_priv.delayed_client_id)
470 client = &vgasr_priv.clients[i];
471 can_switch = vgasr_priv.clients[i].can_switch(vgasr_priv.clients[i].pdev);
472 if (can_switch == false) {
473 printk(KERN_ERR "vga_switcheroo: client %d refused switch\n", i);
474 break;
475 }
476 }
477
478 if (can_switch == false || client == NULL)
479 goto err;
480
481 pdev_name = pci_name(client->pdev);
66b37c67 482 ret = vga_switchto_stage2(client);
6a9ee8af 483 if (ret)
66b37c67 484 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
6a9ee8af
DA
485
486 vgasr_priv.delayed_switch_active = false;
487 err = 0;
488err:
489 mutex_unlock(&vgasr_mutex);
490 return err;
491}
492EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
493
This page took 0.170993 seconds and 5 git commands to generate.