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