ALSA: hda - HDMI Audio init all connectors when VGA-switcheroo is off
[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;
26ec685f 37 const struct vga_switcheroo_client_ops *ops;
6a9ee8af
DA
38 int id;
39 bool active;
79721e0a 40 struct list_head list;
6a9ee8af
DA
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;
79721e0a 55 struct list_head clients;
6a9ee8af
DA
56
57 struct vga_switcheroo_handler *handler;
58};
59
3e9e63db
TI
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)
64
6a9ee8af
DA
65static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
66static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
67
68/* only one switcheroo per system */
79721e0a
TI
69static struct vgasr_priv vgasr_priv = {
70 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
71};
6a9ee8af
DA
72
73int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
74{
75 mutex_lock(&vgasr_mutex);
76 if (vgasr_priv.handler) {
77 mutex_unlock(&vgasr_mutex);
78 return -EINVAL;
79 }
80
81 vgasr_priv.handler = handler;
82 mutex_unlock(&vgasr_mutex);
83 return 0;
84}
85EXPORT_SYMBOL(vga_switcheroo_register_handler);
86
87void vga_switcheroo_unregister_handler(void)
88{
89 mutex_lock(&vgasr_mutex);
90 vgasr_priv.handler = NULL;
91 mutex_unlock(&vgasr_mutex);
92}
93EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
94
95static void vga_switcheroo_enable(void)
96{
6a9ee8af 97 int ret;
79721e0a
TI
98 struct vga_switcheroo_client *client;
99
6a9ee8af
DA
100 /* call the handler to init */
101 vgasr_priv.handler->init();
102
79721e0a 103 list_for_each_entry(client, &vgasr_priv.clients, list) {
3e9e63db
TI
104 if (client->id != -1)
105 continue;
79721e0a 106 ret = vgasr_priv.handler->get_client_id(client->pdev);
6a9ee8af
DA
107 if (ret < 0)
108 return;
109
79721e0a 110 client->id = ret;
6a9ee8af
DA
111 }
112 vga_switcheroo_debugfs_init(&vgasr_priv);
113 vgasr_priv.active = true;
114}
115
3e9e63db
TI
116static int register_client(struct pci_dev *pdev,
117 const struct vga_switcheroo_client_ops *ops,
118 int id, bool active)
6a9ee8af 119{
79721e0a 120 struct vga_switcheroo_client *client;
6a9ee8af 121
79721e0a
TI
122 client = kzalloc(sizeof(*client), GFP_KERNEL);
123 if (!client)
124 return -ENOMEM;
125
126 client->pwr_state = VGA_SWITCHEROO_ON;
127 client->pdev = pdev;
26ec685f 128 client->ops = ops;
3e9e63db
TI
129 client->id = id;
130 client->active = active;
6a9ee8af 131
79721e0a
TI
132 mutex_lock(&vgasr_mutex);
133 list_add_tail(&client->list, &vgasr_priv.clients);
3e9e63db
TI
134 if (client_is_vga(client))
135 vgasr_priv.registered_clients++;
6a9ee8af
DA
136
137 /* if we get two clients + handler */
3e9e63db
TI
138 if (!vgasr_priv.active &&
139 vgasr_priv.registered_clients == 2 && vgasr_priv.handler) {
6a9ee8af
DA
140 printk(KERN_INFO "vga_switcheroo: enabled\n");
141 vga_switcheroo_enable();
142 }
143 mutex_unlock(&vgasr_mutex);
144 return 0;
145}
3e9e63db
TI
146
147int vga_switcheroo_register_client(struct pci_dev *pdev,
148 const struct vga_switcheroo_client_ops *ops)
149{
150 return register_client(pdev, ops, -1,
151 pdev == vga_default_device());
152}
6a9ee8af
DA
153EXPORT_SYMBOL(vga_switcheroo_register_client);
154
3e9e63db
TI
155int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
156 const struct vga_switcheroo_client_ops *ops,
157 int id, bool active)
158{
159 return register_client(pdev, ops, id | ID_BIT_AUDIO, active);
160}
161EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
162
79721e0a
TI
163static struct vga_switcheroo_client *
164find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
165{
166 struct vga_switcheroo_client *client;
167 list_for_each_entry(client, head, list)
168 if (client->pdev == pdev)
169 return client;
170 return NULL;
171}
172
173static struct vga_switcheroo_client *
174find_client_from_id(struct list_head *head, int client_id)
175{
176 struct vga_switcheroo_client *client;
177 list_for_each_entry(client, head, list)
178 if (client->id == client_id)
179 return client;
180 return NULL;
181}
182
183static struct vga_switcheroo_client *
184find_active_client(struct list_head *head)
185{
186 struct vga_switcheroo_client *client;
187 list_for_each_entry(client, head, list)
3e9e63db 188 if (client->active && client_is_vga(client))
79721e0a
TI
189 return client;
190 return NULL;
191}
192
c8e9cf7b
TI
193int vga_switcheroo_get_client_state(struct pci_dev *pdev)
194{
195 struct vga_switcheroo_client *client;
196
197 client = find_client_from_pci(&vgasr_priv.clients, pdev);
198 if (!client)
199 return VGA_SWITCHEROO_NOT_FOUND;
200 if (!vgasr_priv.active)
201 return VGA_SWITCHEROO_INIT;
202 return client->pwr_state;
203}
204EXPORT_SYMBOL(vga_switcheroo_get_client_state);
205
6a9ee8af
DA
206void vga_switcheroo_unregister_client(struct pci_dev *pdev)
207{
79721e0a 208 struct vga_switcheroo_client *client;
6a9ee8af
DA
209
210 mutex_lock(&vgasr_mutex);
79721e0a
TI
211 client = find_client_from_pci(&vgasr_priv.clients, pdev);
212 if (client) {
3e9e63db
TI
213 if (client_is_vga(client))
214 vgasr_priv.registered_clients--;
79721e0a
TI
215 list_del(&client->list);
216 kfree(client);
6a9ee8af 217 }
3e9e63db
TI
218 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
219 printk(KERN_INFO "vga_switcheroo: disabled\n");
220 vga_switcheroo_debugfs_fini(&vgasr_priv);
221 vgasr_priv.active = false;
222 }
6a9ee8af
DA
223 mutex_unlock(&vgasr_mutex);
224}
225EXPORT_SYMBOL(vga_switcheroo_unregister_client);
226
227void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
228 struct fb_info *info)
229{
79721e0a 230 struct vga_switcheroo_client *client;
6a9ee8af
DA
231
232 mutex_lock(&vgasr_mutex);
79721e0a
TI
233 client = find_client_from_pci(&vgasr_priv.clients, pdev);
234 if (client)
235 client->fb_info = info;
6a9ee8af
DA
236 mutex_unlock(&vgasr_mutex);
237}
238EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
239
240static int vga_switcheroo_show(struct seq_file *m, void *v)
241{
79721e0a
TI
242 struct vga_switcheroo_client *client;
243 int i = 0;
6a9ee8af 244 mutex_lock(&vgasr_mutex);
79721e0a 245 list_for_each_entry(client, &vgasr_priv.clients, list) {
3e9e63db
TI
246 seq_printf(m, "%d:%s%s:%c:%s:%s\n", i,
247 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
248 client_is_vga(client) ? "" : "-Audio",
79721e0a
TI
249 client->active ? '+' : ' ',
250 client->pwr_state ? "Pwr" : "Off",
251 pci_name(client->pdev));
252 i++;
6a9ee8af
DA
253 }
254 mutex_unlock(&vgasr_mutex);
255 return 0;
256}
257
258static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
259{
260 return single_open(file, vga_switcheroo_show, NULL);
261}
262
263static int vga_switchon(struct vga_switcheroo_client *client)
264{
5cfb3c3a
DA
265 if (vgasr_priv.handler->power_state)
266 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
6a9ee8af 267 /* call the driver callback to turn on device */
26ec685f 268 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
6a9ee8af
DA
269 client->pwr_state = VGA_SWITCHEROO_ON;
270 return 0;
271}
272
273static int vga_switchoff(struct vga_switcheroo_client *client)
274{
275 /* call the driver callback to turn off device */
26ec685f 276 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
5cfb3c3a
DA
277 if (vgasr_priv.handler->power_state)
278 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
6a9ee8af
DA
279 client->pwr_state = VGA_SWITCHEROO_OFF;
280 return 0;
281}
282
3e9e63db
TI
283static void set_audio_state(int id, int state)
284{
285 struct vga_switcheroo_client *client;
286
287 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
288 if (client && client->pwr_state != state) {
289 client->ops->set_gpu_state(client->pdev, state);
290 client->pwr_state = state;
291 }
292}
293
66b37c67
DA
294/* stage one happens before delay */
295static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
6a9ee8af 296{
79721e0a 297 struct vga_switcheroo_client *active;
6a9ee8af 298
79721e0a 299 active = find_active_client(&vgasr_priv.clients);
6a9ee8af
DA
300 if (!active)
301 return 0;
302
6a9ee8af
DA
303 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
304 vga_switchon(new_client);
305
2fbe8c7c 306 vga_set_default_device(new_client->pdev);
3e9e63db 307 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
2fbe8c7c 308
66b37c67
DA
309 return 0;
310}
311
312/* post delay */
313static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
314{
315 int ret;
79721e0a 316 struct vga_switcheroo_client *active;
66b37c67 317
79721e0a 318 active = find_active_client(&vgasr_priv.clients);
66b37c67
DA
319 if (!active)
320 return 0;
321
322 active->active = false;
6a9ee8af
DA
323
324 if (new_client->fb_info) {
325 struct fb_event event;
326 event.info = new_client->fb_info;
327 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
328 }
329
330 ret = vgasr_priv.handler->switchto(new_client->id);
331 if (ret)
332 return ret;
333
26ec685f
TI
334 if (new_client->ops->reprobe)
335 new_client->ops->reprobe(new_client->pdev);
8d608aa6 336
3e9e63db
TI
337 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
338
6a9ee8af
DA
339 if (active->pwr_state == VGA_SWITCHEROO_ON)
340 vga_switchoff(active);
341
342 new_client->active = true;
343 return 0;
344}
345
79721e0a
TI
346static bool check_can_switch(void)
347{
348 struct vga_switcheroo_client *client;
349
350 list_for_each_entry(client, &vgasr_priv.clients, list) {
26ec685f 351 if (!client->ops->can_switch(client->pdev)) {
79721e0a
TI
352 printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id);
353 return false;
354 }
355 }
356 return true;
357}
358
6a9ee8af
DA
359static ssize_t
360vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
361 size_t cnt, loff_t *ppos)
362{
363 char usercmd[64];
364 const char *pdev_name;
79721e0a 365 int ret;
6a9ee8af 366 bool delay = false, can_switch;
851ab954 367 bool just_mux = false;
6a9ee8af
DA
368 int client_id = -1;
369 struct vga_switcheroo_client *client = NULL;
370
371 if (cnt > 63)
372 cnt = 63;
373
374 if (copy_from_user(usercmd, ubuf, cnt))
375 return -EFAULT;
376
377 mutex_lock(&vgasr_mutex);
378
8c88e50b
JS
379 if (!vgasr_priv.active) {
380 cnt = -EINVAL;
381 goto out;
382 }
6a9ee8af
DA
383
384 /* pwr off the device not in use */
385 if (strncmp(usercmd, "OFF", 3) == 0) {
79721e0a
TI
386 list_for_each_entry(client, &vgasr_priv.clients, list) {
387 if (client->active)
6a9ee8af 388 continue;
79721e0a
TI
389 if (client->pwr_state == VGA_SWITCHEROO_ON)
390 vga_switchoff(client);
6a9ee8af
DA
391 }
392 goto out;
393 }
394 /* pwr on the device not in use */
395 if (strncmp(usercmd, "ON", 2) == 0) {
79721e0a
TI
396 list_for_each_entry(client, &vgasr_priv.clients, list) {
397 if (client->active)
6a9ee8af 398 continue;
79721e0a
TI
399 if (client->pwr_state == VGA_SWITCHEROO_OFF)
400 vga_switchon(client);
6a9ee8af
DA
401 }
402 goto out;
403 }
404
405 /* request a delayed switch - test can we switch now */
406 if (strncmp(usercmd, "DIGD", 4) == 0) {
407 client_id = VGA_SWITCHEROO_IGD;
408 delay = true;
409 }
410
411 if (strncmp(usercmd, "DDIS", 4) == 0) {
412 client_id = VGA_SWITCHEROO_DIS;
413 delay = true;
414 }
415
416 if (strncmp(usercmd, "IGD", 3) == 0)
417 client_id = VGA_SWITCHEROO_IGD;
418
419 if (strncmp(usercmd, "DIS", 3) == 0)
420 client_id = VGA_SWITCHEROO_DIS;
421
fea6f330 422 if (strncmp(usercmd, "MIGD", 4) == 0) {
851ab954
DA
423 just_mux = true;
424 client_id = VGA_SWITCHEROO_IGD;
425 }
fea6f330 426 if (strncmp(usercmd, "MDIS", 4) == 0) {
851ab954
DA
427 just_mux = true;
428 client_id = VGA_SWITCHEROO_DIS;
429 }
430
6a9ee8af
DA
431 if (client_id == -1)
432 goto out;
79721e0a
TI
433 client = find_client_from_id(&vgasr_priv.clients, client_id);
434 if (!client)
435 goto out;
6a9ee8af
DA
436
437 vgasr_priv.delayed_switch_active = false;
851ab954
DA
438
439 if (just_mux) {
440 ret = vgasr_priv.handler->switchto(client_id);
441 goto out;
442 }
443
79721e0a 444 if (client->active)
a67b8887
FM
445 goto out;
446
6a9ee8af 447 /* okay we want a switch - test if devices are willing to switch */
79721e0a 448 can_switch = check_can_switch();
6a9ee8af
DA
449
450 if (can_switch == false && delay == false)
451 goto out;
452
79721e0a 453 if (can_switch) {
6a9ee8af 454 pdev_name = pci_name(client->pdev);
66b37c67
DA
455 ret = vga_switchto_stage1(client);
456 if (ret)
457 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
458
459 ret = vga_switchto_stage2(client);
6a9ee8af 460 if (ret)
66b37c67
DA
461 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
462
6a9ee8af
DA
463 } else {
464 printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
465 vgasr_priv.delayed_switch_active = true;
466 vgasr_priv.delayed_client_id = client_id;
467
66b37c67
DA
468 ret = vga_switchto_stage1(client);
469 if (ret)
470 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
6a9ee8af
DA
471 }
472
473out:
474 mutex_unlock(&vgasr_mutex);
475 return cnt;
476}
477
478static const struct file_operations vga_switcheroo_debugfs_fops = {
479 .owner = THIS_MODULE,
480 .open = vga_switcheroo_debugfs_open,
481 .write = vga_switcheroo_debugfs_write,
482 .read = seq_read,
483 .llseek = seq_lseek,
484 .release = single_release,
485};
486
487static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
488{
489 if (priv->switch_file) {
490 debugfs_remove(priv->switch_file);
491 priv->switch_file = NULL;
492 }
493 if (priv->debugfs_root) {
494 debugfs_remove(priv->debugfs_root);
495 priv->debugfs_root = NULL;
496 }
497}
498
499static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
500{
501 /* already initialised */
502 if (priv->debugfs_root)
503 return 0;
504 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
505
506 if (!priv->debugfs_root) {
507 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
508 goto fail;
509 }
510
511 priv->switch_file = debugfs_create_file("switch", 0644,
512 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
513 if (!priv->switch_file) {
514 printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
515 goto fail;
516 }
517 return 0;
518fail:
519 vga_switcheroo_debugfs_fini(priv);
520 return -1;
521}
522
523int vga_switcheroo_process_delayed_switch(void)
524{
79721e0a 525 struct vga_switcheroo_client *client;
6a9ee8af 526 const char *pdev_name;
6a9ee8af
DA
527 int ret;
528 int err = -EINVAL;
529
530 mutex_lock(&vgasr_mutex);
531 if (!vgasr_priv.delayed_switch_active)
532 goto err;
533
534 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
535
79721e0a
TI
536 client = find_client_from_id(&vgasr_priv.clients,
537 vgasr_priv.delayed_client_id);
538 if (!client || !check_can_switch())
6a9ee8af
DA
539 goto err;
540
541 pdev_name = pci_name(client->pdev);
66b37c67 542 ret = vga_switchto_stage2(client);
6a9ee8af 543 if (ret)
66b37c67 544 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
6a9ee8af
DA
545
546 vgasr_priv.delayed_switch_active = false;
547 err = 0;
548err:
549 mutex_unlock(&vgasr_mutex);
550 return err;
551}
552EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
553
This page took 0.212404 seconds and 5 git commands to generate.