blob: fa60add0ff63549d988215853f11af7cff9c6c43 [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>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100021#include <linux/seq_file.h>
22#include <linux/uaccess.h>
23#include <linux/fs.h>
24#include <linux/debugfs.h>
25#include <linux/fb.h>
26
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100027#include <linux/pci.h>
28#include <linux/vga_switcheroo.h>
29
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040030#include <linux/vgaarb.h>
31
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100032struct vga_switcheroo_client {
33 struct pci_dev *pdev;
34 struct fb_info *fb_info;
35 int pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +020036 const struct vga_switcheroo_client_ops *ops;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100037 int id;
38 bool active;
Takashi Iwai79721e02012-04-26 12:55:59 +020039 struct list_head list;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100040};
41
42static DEFINE_MUTEX(vgasr_mutex);
43
44struct vgasr_priv {
45
46 bool active;
47 bool delayed_switch_active;
48 enum vga_switcheroo_client_id delayed_client_id;
49
50 struct dentry *debugfs_root;
51 struct dentry *switch_file;
52
53 int registered_clients;
Takashi Iwai79721e02012-04-26 12:55:59 +020054 struct list_head clients;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100055
56 struct vga_switcheroo_handler *handler;
57};
58
Takashi Iwai3e9e63d2012-04-26 14:29:48 +020059#define ID_BIT_AUDIO 0x100
60#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
61#define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
62#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
63
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100064static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
65static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
66
67/* only one switcheroo per system */
Takashi Iwai79721e02012-04-26 12:55:59 +020068static struct vgasr_priv vgasr_priv = {
69 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
70};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100071
Seth Forshee36704c02012-08-17 11:17:03 -050072static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100073{
Seth Forshee36704c02012-08-17 11:17:03 -050074 /* we're ready if we get two clients + handler */
75 return !vgasr_priv.active &&
76 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100077}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100078
79static void vga_switcheroo_enable(void)
80{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100081 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +020082 struct vga_switcheroo_client *client;
83
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100084 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -050085 if (vgasr_priv.handler->init)
86 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100087
Takashi Iwai79721e02012-04-26 12:55:59 +020088 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +020089 if (client->id != -1)
90 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +020091 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100092 if (ret < 0)
93 return;
94
Takashi Iwai79721e02012-04-26 12:55:59 +020095 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100096 }
97 vga_switcheroo_debugfs_init(&vgasr_priv);
98 vgasr_priv.active = true;
99}
100
Seth Forshee36704c02012-08-17 11:17:03 -0500101int 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
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200132static int register_client(struct pci_dev *pdev,
133 const struct vga_switcheroo_client_ops *ops,
134 int id, bool active)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000135{
Takashi Iwai79721e02012-04-26 12:55:59 +0200136 struct vga_switcheroo_client *client;
137
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;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200144 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200145 client->id = id;
146 client->active = active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000147
148 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200149 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200150 if (client_is_vga(client))
151 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000152
Seth Forshee36704c02012-08-17 11:17:03 -0500153 if (vga_switcheroo_ready()) {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000154 printk(KERN_INFO "vga_switcheroo: enabled\n");
155 vga_switcheroo_enable();
156 }
157 mutex_unlock(&vgasr_mutex);
158 return 0;
159}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200160
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}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000167EXPORT_SYMBOL(vga_switcheroo_register_client);
168
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200169int 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
Takashi Iwai79721e02012-04-26 12:55:59 +0200177static 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)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200202 if (client->active && client_is_vga(client))
Takashi Iwai79721e02012-04-26 12:55:59 +0200203 return client;
204 return NULL;
205}
206
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200207int 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
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000220void vga_switcheroo_unregister_client(struct pci_dev *pdev)
221{
Takashi Iwai79721e02012-04-26 12:55:59 +0200222 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000223
224 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200225 client = find_client_from_pci(&vgasr_priv.clients, pdev);
226 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200227 if (client_is_vga(client))
228 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200229 list_del(&client->list);
230 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000231 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200232 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 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000237 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{
Takashi Iwai79721e02012-04-26 12:55:59 +0200244 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000245
246 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200247 client = find_client_from_pci(&vgasr_priv.clients, pdev);
248 if (client)
249 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000250 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{
Takashi Iwai79721e02012-04-26 12:55:59 +0200256 struct vga_switcheroo_client *client;
257 int i = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000258 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200259 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200260 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",
Takashi Iwai79721e02012-04-26 12:55:59 +0200263 client->active ? '+' : ' ',
264 client->pwr_state ? "Pwr" : "Off",
265 pci_name(client->pdev));
266 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000267 }
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{
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000279 if (vgasr_priv.handler->power_state)
280 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000281 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200282 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000283 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 */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200290 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000291 if (vgasr_priv.handler->power_state)
292 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000293 client->pwr_state = VGA_SWITCHEROO_OFF;
294 return 0;
295}
296
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200297static 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
Dave Airlie66b37c62010-12-07 14:24:25 +1000308/* stage one happens before delay */
309static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000310{
Takashi Iwai79721e02012-04-26 12:55:59 +0200311 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000312
Takashi Iwai79721e02012-04-26 12:55:59 +0200313 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000314 if (!active)
315 return 0;
316
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000317 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
318 vga_switchon(new_client);
319
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400320 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000321 return 0;
322}
323
324/* post delay */
325static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
326{
327 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200328 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000329
Takashi Iwai79721e02012-04-26 12:55:59 +0200330 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000331 if (!active)
332 return 0;
333
334 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000335
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200336 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
337
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000338 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
Takashi Iwai26ec6852012-05-11 07:51:17 +0200348 if (new_client->ops->reprobe)
349 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000350
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000351 if (active->pwr_state == VGA_SWITCHEROO_ON)
352 vga_switchoff(active);
353
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200354 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
355
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000356 new_client->active = true;
357 return 0;
358}
359
Takashi Iwai79721e02012-04-26 12:55:59 +0200360static bool check_can_switch(void)
361{
362 struct vga_switcheroo_client *client;
363
364 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200365 if (!client->ops->can_switch(client->pdev)) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200366 printk(KERN_ERR "vga_switcheroo: client %x refused switch\n", client->id);
367 return false;
368 }
369 }
370 return true;
371}
372
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000373static 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];
Takashi Iwai79721e02012-04-26 12:55:59 +0200378 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000379 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000380 bool just_mux = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000381 int client_id = -1;
382 struct vga_switcheroo_client *client = NULL;
383
384 if (cnt > 63)
385 cnt = 63;
386
387 if (copy_from_user(usercmd, ubuf, cnt))
388 return -EFAULT;
389
390 mutex_lock(&vgasr_mutex);
391
Jiri Slaby8c88e502010-04-27 14:11:03 -0700392 if (!vgasr_priv.active) {
393 cnt = -EINVAL;
394 goto out;
395 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000396
397 /* pwr off the device not in use */
398 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200399 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200400 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000401 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200402 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200403 if (client->pwr_state == VGA_SWITCHEROO_ON)
404 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000405 }
406 goto out;
407 }
408 /* pwr on the device not in use */
409 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200410 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200411 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000412 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200413 if (client->pwr_state == VGA_SWITCHEROO_OFF)
414 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200415 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000416 }
417 goto out;
418 }
419
420 /* request a delayed switch - test can we switch now */
421 if (strncmp(usercmd, "DIGD", 4) == 0) {
422 client_id = VGA_SWITCHEROO_IGD;
423 delay = true;
424 }
425
426 if (strncmp(usercmd, "DDIS", 4) == 0) {
427 client_id = VGA_SWITCHEROO_DIS;
428 delay = true;
429 }
430
431 if (strncmp(usercmd, "IGD", 3) == 0)
432 client_id = VGA_SWITCHEROO_IGD;
433
434 if (strncmp(usercmd, "DIS", 3) == 0)
435 client_id = VGA_SWITCHEROO_DIS;
436
Dan Carpenterfea6f332011-01-07 08:12:27 +0300437 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000438 just_mux = true;
439 client_id = VGA_SWITCHEROO_IGD;
440 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300441 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000442 just_mux = true;
443 client_id = VGA_SWITCHEROO_DIS;
444 }
445
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000446 if (client_id == -1)
447 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200448 client = find_client_from_id(&vgasr_priv.clients, client_id);
449 if (!client)
450 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000451
452 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000453
454 if (just_mux) {
455 ret = vgasr_priv.handler->switchto(client_id);
456 goto out;
457 }
458
Takashi Iwai79721e02012-04-26 12:55:59 +0200459 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200460 goto out;
461
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000462 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200463 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000464
465 if (can_switch == false && delay == false)
466 goto out;
467
Takashi Iwai79721e02012-04-26 12:55:59 +0200468 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000469 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000470 if (ret)
Dave Airlie66b37c62010-12-07 14:24:25 +1000471 printk(KERN_ERR "vga_switcheroo: switching failed stage 1 %d\n", ret);
472
473 ret = vga_switchto_stage2(client);
474 if (ret)
475 printk(KERN_ERR "vga_switcheroo: switching failed stage 2 %d\n", ret);
476
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000477 } else {
478 printk(KERN_INFO "vga_switcheroo: setting delayed switch to client %d\n", client->id);
479 vgasr_priv.delayed_switch_active = true;
480 vgasr_priv.delayed_client_id = client_id;
481
Dave Airlie66b37c62010-12-07 14:24:25 +1000482 ret = vga_switchto_stage1(client);
483 if (ret)
484 printk(KERN_ERR "vga_switcheroo: delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000485 }
486
487out:
488 mutex_unlock(&vgasr_mutex);
489 return cnt;
490}
491
492static const struct file_operations vga_switcheroo_debugfs_fops = {
493 .owner = THIS_MODULE,
494 .open = vga_switcheroo_debugfs_open,
495 .write = vga_switcheroo_debugfs_write,
496 .read = seq_read,
497 .llseek = seq_lseek,
498 .release = single_release,
499};
500
501static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
502{
503 if (priv->switch_file) {
504 debugfs_remove(priv->switch_file);
505 priv->switch_file = NULL;
506 }
507 if (priv->debugfs_root) {
508 debugfs_remove(priv->debugfs_root);
509 priv->debugfs_root = NULL;
510 }
511}
512
513static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
514{
515 /* already initialised */
516 if (priv->debugfs_root)
517 return 0;
518 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
519
520 if (!priv->debugfs_root) {
521 printk(KERN_ERR "vga_switcheroo: Cannot create /sys/kernel/debug/vgaswitcheroo\n");
522 goto fail;
523 }
524
525 priv->switch_file = debugfs_create_file("switch", 0644,
526 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
527 if (!priv->switch_file) {
528 printk(KERN_ERR "vga_switcheroo: cannot create /sys/kernel/debug/vgaswitcheroo/switch\n");
529 goto fail;
530 }
531 return 0;
532fail:
533 vga_switcheroo_debugfs_fini(priv);
534 return -1;
535}
536
537int vga_switcheroo_process_delayed_switch(void)
538{
Takashi Iwai79721e02012-04-26 12:55:59 +0200539 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000540 int ret;
541 int err = -EINVAL;
542
543 mutex_lock(&vgasr_mutex);
544 if (!vgasr_priv.delayed_switch_active)
545 goto err;
546
547 printk(KERN_INFO "vga_switcheroo: processing delayed switch to %d\n", vgasr_priv.delayed_client_id);
548
Takashi Iwai79721e02012-04-26 12:55:59 +0200549 client = find_client_from_id(&vgasr_priv.clients,
550 vgasr_priv.delayed_client_id);
551 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000552 goto err;
553
Dave Airlie66b37c62010-12-07 14:24:25 +1000554 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000555 if (ret)
Dave Airlie66b37c62010-12-07 14:24:25 +1000556 printk(KERN_ERR "vga_switcheroo: delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000557
558 vgasr_priv.delayed_switch_active = false;
559 err = 0;
560err:
561 mutex_unlock(&vgasr_mutex);
562 return err;
563}
564EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);