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