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