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