blob: 38f9534ac513bcf51b90fcfff56d6bd6ed84c988 [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>
29#include <linux/vga_switcheroo.h>
30
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040031#include <linux/vgaarb.h>
32
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100033struct vga_switcheroo_client {
34 struct pci_dev *pdev;
35 struct fb_info *fb_info;
36 int pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +020037 const struct vga_switcheroo_client_ops *ops;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100038 int id;
39 bool active;
Takashi Iwai79721e02012-04-26 12:55:59 +020040 struct list_head list;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100041};
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;
Takashi Iwai79721e02012-04-26 12:55:59 +020055 struct list_head clients;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100056
57 struct vga_switcheroo_handler *handler;
58};
59
Takashi Iwai3e9e63d2012-04-26 14:29:48 +020060#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
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100065static 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 */
Takashi Iwai79721e02012-04-26 12:55:59 +020069static struct vgasr_priv vgasr_priv = {
70 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
71};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100072
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{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100097 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +020098 struct vga_switcheroo_client *client;
99
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000100 /* call the handler to init */
101 vgasr_priv.handler->init();
102
Takashi Iwai79721e02012-04-26 12:55:59 +0200103 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200104 if (client->id != -1)
105 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200106 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000107 if (ret < 0)
108 return;
109
Takashi Iwai79721e02012-04-26 12:55:59 +0200110 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000111 }
112 vga_switcheroo_debugfs_init(&vgasr_priv);
113 vgasr_priv.active = true;
114}
115
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200116static int register_client(struct pci_dev *pdev,
117 const struct vga_switcheroo_client_ops *ops,
118 int id, bool active)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000119{
Takashi Iwai79721e02012-04-26 12:55:59 +0200120 struct vga_switcheroo_client *client;
121
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;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200128 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200129 client->id = id;
130 client->active = active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000131
132 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200133 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200134 if (client_is_vga(client))
135 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000136
137 /* if we get two clients + handler */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200138 if (!vgasr_priv.active &&
139 vgasr_priv.registered_clients == 2 && vgasr_priv.handler) {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000140 printk(KERN_INFO "vga_switcheroo: enabled\n");
141 vga_switcheroo_enable();
142 }
143 mutex_unlock(&vgasr_mutex);
144 return 0;
145}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200146
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}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000153EXPORT_SYMBOL(vga_switcheroo_register_client);
154
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200155int 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
Takashi Iwai79721e02012-04-26 12:55:59 +0200163static 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)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200188 if (client->active && client_is_vga(client))
Takashi Iwai79721e02012-04-26 12:55:59 +0200189 return client;
190 return NULL;
191}
192
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000193void vga_switcheroo_unregister_client(struct pci_dev *pdev)
194{
Takashi Iwai79721e02012-04-26 12:55:59 +0200195 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000196
197 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200198 client = find_client_from_pci(&vgasr_priv.clients, pdev);
199 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200200 if (client_is_vga(client))
201 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200202 list_del(&client->list);
203 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000204 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200205 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
206 printk(KERN_INFO "vga_switcheroo: disabled\n");
207 vga_switcheroo_debugfs_fini(&vgasr_priv);
208 vgasr_priv.active = false;
209 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000210 mutex_unlock(&vgasr_mutex);
211}
212EXPORT_SYMBOL(vga_switcheroo_unregister_client);
213
214void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
215 struct fb_info *info)
216{
Takashi Iwai79721e02012-04-26 12:55:59 +0200217 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000218
219 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200220 client = find_client_from_pci(&vgasr_priv.clients, pdev);
221 if (client)
222 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000223 mutex_unlock(&vgasr_mutex);
224}
225EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
226
227static int vga_switcheroo_show(struct seq_file *m, void *v)
228{
Takashi Iwai79721e02012-04-26 12:55:59 +0200229 struct vga_switcheroo_client *client;
230 int i = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000231 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200232 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200233 seq_printf(m, "%d:%s%s:%c:%s:%s\n", i,
234 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
235 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200236 client->active ? '+' : ' ',
237 client->pwr_state ? "Pwr" : "Off",
238 pci_name(client->pdev));
239 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000240 }
241 mutex_unlock(&vgasr_mutex);
242 return 0;
243}
244
245static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
246{
247 return single_open(file, vga_switcheroo_show, NULL);
248}
249
250static int vga_switchon(struct vga_switcheroo_client *client)
251{
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000252 if (vgasr_priv.handler->power_state)
253 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000254 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200255 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000256 client->pwr_state = VGA_SWITCHEROO_ON;
257 return 0;
258}
259
260static int vga_switchoff(struct vga_switcheroo_client *client)
261{
262 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200263 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000264 if (vgasr_priv.handler->power_state)
265 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000266 client->pwr_state = VGA_SWITCHEROO_OFF;
267 return 0;
268}
269
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200270static void set_audio_state(int id, int state)
271{
272 struct vga_switcheroo_client *client;
273
274 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
275 if (client && client->pwr_state != state) {
276 client->ops->set_gpu_state(client->pdev, state);
277 client->pwr_state = state;
278 }
279}
280
Dave Airlie66b37c62010-12-07 14:24:25 +1000281/* stage one happens before delay */
282static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000283{
Takashi Iwai79721e02012-04-26 12:55:59 +0200284 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000285
Takashi Iwai79721e02012-04-26 12:55:59 +0200286 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000287 if (!active)
288 return 0;
289
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000290 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
291 vga_switchon(new_client);
292
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400293 vga_set_default_device(new_client->pdev);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200294 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400295
Dave Airlie66b37c62010-12-07 14:24:25 +1000296 return 0;
297}
298
299/* post delay */
300static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
301{
302 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200303 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000304
Takashi Iwai79721e02012-04-26 12:55:59 +0200305 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000306 if (!active)
307 return 0;
308
309 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000310
311 if (new_client->fb_info) {
312 struct fb_event event;
313 event.info = new_client->fb_info;
314 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
315 }
316
317 ret = vgasr_priv.handler->switchto(new_client->id);
318 if (ret)
319 return ret;
320
Takashi Iwai26ec6852012-05-11 07:51:17 +0200321 if (new_client->ops->reprobe)
322 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000323
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200324 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
325
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000326 if (active->pwr_state == VGA_SWITCHEROO_ON)
327 vga_switchoff(active);
328
329 new_client->active = true;
330 return 0;
331}
332
Takashi Iwai79721e02012-04-26 12:55:59 +0200333static bool check_can_switch(void)
334{
335 struct vga_switcheroo_client *client;
336
337 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200338 if (!client->ops->can_switch(client->pdev)) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200339 printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id);
340 return false;
341 }
342 }
343 return true;
344}
345
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000346static ssize_t
347vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
348 size_t cnt, loff_t *ppos)
349{
350 char usercmd[64];
351 const char *pdev_name;
Takashi Iwai79721e02012-04-26 12:55:59 +0200352 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000353 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000354 bool just_mux = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000355 int client_id = -1;
356 struct vga_switcheroo_client *client = NULL;
357
358 if (cnt > 63)
359 cnt = 63;
360
361 if (copy_from_user(usercmd, ubuf, cnt))
362 return -EFAULT;
363
364 mutex_lock(&vgasr_mutex);
365
Jiri Slaby8c88e502010-04-27 14:11:03 -0700366 if (!vgasr_priv.active) {
367 cnt = -EINVAL;
368 goto out;
369 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000370
371 /* pwr off the device not in use */
372 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200373 list_for_each_entry(client, &vgasr_priv.clients, list) {
374 if (client->active)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000375 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200376 if (client->pwr_state == VGA_SWITCHEROO_ON)
377 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000378 }
379 goto out;
380 }
381 /* pwr on the device not in use */
382 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200383 list_for_each_entry(client, &vgasr_priv.clients, list) {
384 if (client->active)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000385 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200386 if (client->pwr_state == VGA_SWITCHEROO_OFF)
387 vga_switchon(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000388 }
389 goto out;
390 }
391
392 /* request a delayed switch - test can we switch now */
393 if (strncmp(usercmd, "DIGD", 4) == 0) {
394 client_id = VGA_SWITCHEROO_IGD;
395 delay = true;
396 }
397
398 if (strncmp(usercmd, "DDIS", 4) == 0) {
399 client_id = VGA_SWITCHEROO_DIS;
400 delay = true;
401 }
402
403 if (strncmp(usercmd, "IGD", 3) == 0)
404 client_id = VGA_SWITCHEROO_IGD;
405
406 if (strncmp(usercmd, "DIS", 3) == 0)
407 client_id = VGA_SWITCHEROO_DIS;
408
Dan Carpenterfea6f332011-01-07 08:12:27 +0300409 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000410 just_mux = true;
411 client_id = VGA_SWITCHEROO_IGD;
412 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300413 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000414 just_mux = true;
415 client_id = VGA_SWITCHEROO_DIS;
416 }
417
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000418 if (client_id == -1)
419 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200420 client = find_client_from_id(&vgasr_priv.clients, client_id);
421 if (!client)
422 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000423
424 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000425
426 if (just_mux) {
427 ret = vgasr_priv.handler->switchto(client_id);
428 goto out;
429 }
430
Takashi Iwai79721e02012-04-26 12:55:59 +0200431 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200432 goto out;
433
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000434 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200435 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000436
437 if (can_switch == false && delay == false)
438 goto out;
439
Takashi Iwai79721e02012-04-26 12:55:59 +0200440 if (can_switch) {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000441 pdev_name = pci_name(client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000442 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000443 if (ret)
Dave Airlie66b37c62010-12-07 14:24:25 +1000444 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
445
446 ret = vga_switchto_stage2(client);
447 if (ret)
448 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
449
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000450 } else {
451 printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
452 vgasr_priv.delayed_switch_active = true;
453 vgasr_priv.delayed_client_id = client_id;
454
Dave Airlie66b37c62010-12-07 14:24:25 +1000455 ret = vga_switchto_stage1(client);
456 if (ret)
457 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000458 }
459
460out:
461 mutex_unlock(&vgasr_mutex);
462 return cnt;
463}
464
465static const struct file_operations vga_switcheroo_debugfs_fops = {
466 .owner = THIS_MODULE,
467 .open = vga_switcheroo_debugfs_open,
468 .write = vga_switcheroo_debugfs_write,
469 .read = seq_read,
470 .llseek = seq_lseek,
471 .release = single_release,
472};
473
474static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
475{
476 if (priv->switch_file) {
477 debugfs_remove(priv->switch_file);
478 priv->switch_file = NULL;
479 }
480 if (priv->debugfs_root) {
481 debugfs_remove(priv->debugfs_root);
482 priv->debugfs_root = NULL;
483 }
484}
485
486static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
487{
488 /* already initialised */
489 if (priv->debugfs_root)
490 return 0;
491 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
492
493 if (!priv->debugfs_root) {
494 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
495 goto fail;
496 }
497
498 priv->switch_file = debugfs_create_file("switch", 0644,
499 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
500 if (!priv->switch_file) {
501 printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
502 goto fail;
503 }
504 return 0;
505fail:
506 vga_switcheroo_debugfs_fini(priv);
507 return -1;
508}
509
510int vga_switcheroo_process_delayed_switch(void)
511{
Takashi Iwai79721e02012-04-26 12:55:59 +0200512 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000513 const char *pdev_name;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000514 int ret;
515 int err = -EINVAL;
516
517 mutex_lock(&vgasr_mutex);
518 if (!vgasr_priv.delayed_switch_active)
519 goto err;
520
521 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
522
Takashi Iwai79721e02012-04-26 12:55:59 +0200523 client = find_client_from_id(&vgasr_priv.clients,
524 vgasr_priv.delayed_client_id);
525 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000526 goto err;
527
528 pdev_name = pci_name(client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000529 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000530 if (ret)
Dave Airlie66b37c62010-12-07 14:24:25 +1000531 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000532
533 vgasr_priv.delayed_switch_active = false;
534 err = 0;
535err:
536 mutex_unlock(&vgasr_mutex);
537 return err;
538}
539EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
540