1 /* 2 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs 3 * 4 * Copyright (c) 2010 Red Hat Inc. 5 * Author : Dave Airlie <airlied@redhat.com> 6 * 7 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de> 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a 10 * copy of this software and associated documentation files (the "Software"), 11 * to deal in the Software without restriction, including without limitation 12 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 13 * and/or sell copies of the Software, and to permit persons to whom the 14 * Software is furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice (including the next 17 * paragraph) shall be included in all copies or substantial portions of the 18 * Software. 19 * 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 * DEALINGS 27 * IN THE SOFTWARE. 28 * 29 */ 30 31 #define pr_fmt(fmt) "vga_switcheroo: " fmt 32 33 #include <linux/apple-gmux.h> 34 #include <linux/console.h> 35 #include <linux/debugfs.h> 36 #include <linux/fb.h> 37 #include <linux/fs.h> 38 #include <linux/fbcon.h> 39 #include <linux/module.h> 40 #include <linux/pci.h> 41 #include <linux/pm_domain.h> 42 #include <linux/pm_runtime.h> 43 #include <linux/seq_file.h> 44 #include <linux/uaccess.h> 45 #include <linux/vgaarb.h> 46 #include <linux/vga_switcheroo.h> 47 48 /** 49 * DOC: Overview 50 * 51 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics. 52 * These come in two flavors: 53 * 54 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs. 55 * * muxless: Dual GPUs but only one of them is connected to outputs. 56 * The other one is merely used to offload rendering, its results 57 * are copied over PCIe into the framebuffer. On Linux this is 58 * supported with DRI PRIME. 59 * 60 * Hybrid graphics started to appear in the late Naughties and were initially 61 * all muxed. Newer laptops moved to a muxless architecture for cost reasons. 62 * A notable exception is the MacBook Pro which continues to use a mux. 63 * Muxes come with varying capabilities: Some switch only the panel, others 64 * can also switch external displays. Some switch all display pins at once 65 * while others can switch just the DDC lines. (To allow EDID probing 66 * for the inactive GPU.) Also, muxes are often used to cut power to the 67 * discrete GPU while it is not used. 68 * 69 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called 70 * clients. The mux is called the handler. Muxless machines also register a 71 * handler to control the power state of the discrete GPU, its ->switchto 72 * callback is a no-op for obvious reasons. The discrete GPU is often equipped 73 * with an HDA controller for the HDMI/DP audio signal, this will also 74 * register as a client so that vga_switcheroo can take care of the correct 75 * suspend/resume order when changing the discrete GPU's power state. In total 76 * there can thus be up to three clients: Two vga clients (GPUs) and one audio 77 * client (on the discrete GPU). The code is mostly prepared to support 78 * machines with more than two GPUs should they become available. 79 * 80 * The GPU to which the outputs are currently switched is called the 81 * active client in vga_switcheroo parlance. The GPU not in use is the 82 * inactive client. When the inactive client's DRM driver is loaded, 83 * it will be unable to probe the panel's EDID and hence depends on 84 * VBIOS to provide its display modes. If the VBIOS modes are bogus or 85 * if there is no VBIOS at all (which is common on the MacBook Pro), 86 * a client may alternatively request that the DDC lines are temporarily 87 * switched to it, provided that the handler supports this. Switching 88 * only the DDC lines and not the entire output avoids unnecessary 89 * flickering. 90 */ 91 92 /** 93 * struct vga_switcheroo_client - registered client 94 * @pdev: client pci device 95 * @fb_info: framebuffer to which console is remapped on switching 96 * @pwr_state: current power state if manual power control is used. 97 * For driver power control, call vga_switcheroo_pwr_state(). 98 * @ops: client callbacks 99 * @id: client identifier. Determining the id requires the handler, 100 * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID 101 * and later given their true id in vga_switcheroo_enable() 102 * @active: whether the outputs are currently switched to this client 103 * @driver_power_control: whether power state is controlled by the driver's 104 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs 105 * interface is a no-op so as not to interfere with runtime pm 106 * @list: client list 107 * @vga_dev: pci device, indicate which GPU is bound to current audio client 108 * 109 * Registered client. A client can be either a GPU or an audio device on a GPU. 110 * For audio clients, the @fb_info and @active members are bogus. For GPU 111 * clients, the @vga_dev is bogus. 112 */ 113 struct vga_switcheroo_client { 114 struct pci_dev *pdev; 115 struct fb_info *fb_info; 116 enum vga_switcheroo_state pwr_state; 117 const struct vga_switcheroo_client_ops *ops; 118 enum vga_switcheroo_client_id id; 119 bool active; 120 bool driver_power_control; 121 struct list_head list; 122 struct pci_dev *vga_dev; 123 }; 124 125 /* 126 * protects access to struct vgasr_priv 127 */ 128 static DEFINE_MUTEX(vgasr_mutex); 129 130 /** 131 * struct vgasr_priv - vga_switcheroo private data 132 * @active: whether vga_switcheroo is enabled. 133 * Prerequisite is the registration of two GPUs and a handler 134 * @delayed_switch_active: whether a delayed switch is pending 135 * @delayed_client_id: client to which a delayed switch is pending 136 * @debugfs_root: directory for vga_switcheroo debugfs interface 137 * @switch_file: file for vga_switcheroo debugfs interface 138 * @registered_clients: number of registered GPUs 139 * (counting only vga clients, not audio clients) 140 * @clients: list of registered clients 141 * @handler: registered handler 142 * @handler_flags: flags of registered handler 143 * @mux_hw_lock: protects mux state 144 * (in particular while DDC lines are temporarily switched) 145 * @old_ddc_owner: client to which DDC lines will be switched back on unlock 146 * 147 * vga_switcheroo private data. Currently only one vga_switcheroo instance 148 * per system is supported. 149 */ 150 struct vgasr_priv { 151 bool active; 152 bool delayed_switch_active; 153 enum vga_switcheroo_client_id delayed_client_id; 154 155 struct dentry *debugfs_root; 156 struct dentry *switch_file; 157 158 int registered_clients; 159 struct list_head clients; 160 161 const struct vga_switcheroo_handler *handler; 162 enum vga_switcheroo_handler_flags_t handler_flags; 163 struct mutex mux_hw_lock; 164 int old_ddc_owner; 165 }; 166 167 #define ID_BIT_AUDIO 0x100 168 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO) 169 #define client_is_vga(c) (!client_is_audio(c)) 170 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO) 171 172 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv); 173 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv); 174 175 /* only one switcheroo per system */ 176 static struct vgasr_priv vgasr_priv = { 177 .clients = LIST_HEAD_INIT(vgasr_priv.clients), 178 .mux_hw_lock = __MUTEX_INITIALIZER(vgasr_priv.mux_hw_lock), 179 }; 180 181 static bool vga_switcheroo_ready(void) 182 { 183 /* we're ready if we get two clients + handler */ 184 return !vgasr_priv.active && 185 vgasr_priv.registered_clients == 2 && vgasr_priv.handler; 186 } 187 188 static void vga_switcheroo_enable(void) 189 { 190 int ret; 191 struct vga_switcheroo_client *client; 192 193 /* call the handler to init */ 194 if (vgasr_priv.handler->init) 195 vgasr_priv.handler->init(); 196 197 list_for_each_entry(client, &vgasr_priv.clients, list) { 198 if (!client_is_vga(client) || 199 client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID) 200 continue; 201 202 ret = vgasr_priv.handler->get_client_id(client->pdev); 203 if (ret < 0) 204 return; 205 206 client->id = ret; 207 } 208 209 list_for_each_entry(client, &vgasr_priv.clients, list) { 210 if (!client_is_audio(client) || 211 client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID) 212 continue; 213 214 ret = vgasr_priv.handler->get_client_id(client->vga_dev); 215 if (ret < 0) 216 return; 217 218 client->id = ret | ID_BIT_AUDIO; 219 if (client->ops->gpu_bound) 220 client->ops->gpu_bound(client->pdev, ret); 221 } 222 223 vga_switcheroo_debugfs_init(&vgasr_priv); 224 vgasr_priv.active = true; 225 } 226 227 /** 228 * vga_switcheroo_register_handler() - register handler 229 * @handler: handler callbacks 230 * @handler_flags: handler flags 231 * 232 * Register handler. Enable vga_switcheroo if two vga clients have already 233 * registered. 234 * 235 * Return: 0 on success, -EINVAL if a handler was already registered. 236 */ 237 int vga_switcheroo_register_handler( 238 const struct vga_switcheroo_handler *handler, 239 enum vga_switcheroo_handler_flags_t handler_flags) 240 { 241 mutex_lock(&vgasr_mutex); 242 if (vgasr_priv.handler) { 243 mutex_unlock(&vgasr_mutex); 244 return -EINVAL; 245 } 246 247 vgasr_priv.handler = handler; 248 vgasr_priv.handler_flags = handler_flags; 249 if (vga_switcheroo_ready()) { 250 pr_info("enabled\n"); 251 vga_switcheroo_enable(); 252 } 253 mutex_unlock(&vgasr_mutex); 254 return 0; 255 } 256 EXPORT_SYMBOL(vga_switcheroo_register_handler); 257 258 /** 259 * vga_switcheroo_unregister_handler() - unregister handler 260 * 261 * Unregister handler. Disable vga_switcheroo. 262 */ 263 void vga_switcheroo_unregister_handler(void) 264 { 265 mutex_lock(&vgasr_mutex); 266 mutex_lock(&vgasr_priv.mux_hw_lock); 267 vgasr_priv.handler_flags = 0; 268 vgasr_priv.handler = NULL; 269 if (vgasr_priv.active) { 270 pr_info("disabled\n"); 271 vga_switcheroo_debugfs_fini(&vgasr_priv); 272 vgasr_priv.active = false; 273 } 274 mutex_unlock(&vgasr_priv.mux_hw_lock); 275 mutex_unlock(&vgasr_mutex); 276 } 277 EXPORT_SYMBOL(vga_switcheroo_unregister_handler); 278 279 /** 280 * vga_switcheroo_handler_flags() - obtain handler flags 281 * 282 * Helper for clients to obtain the handler flags bitmask. 283 * 284 * Return: Handler flags. A value of 0 means that no handler is registered 285 * or that the handler has no special capabilities. 286 */ 287 enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void) 288 { 289 return vgasr_priv.handler_flags; 290 } 291 EXPORT_SYMBOL(vga_switcheroo_handler_flags); 292 293 static int register_client(struct pci_dev *pdev, 294 const struct vga_switcheroo_client_ops *ops, 295 enum vga_switcheroo_client_id id, 296 struct pci_dev *vga_dev, 297 bool active, 298 bool driver_power_control) 299 { 300 struct vga_switcheroo_client *client; 301 302 client = kzalloc(sizeof(*client), GFP_KERNEL); 303 if (!client) 304 return -ENOMEM; 305 306 client->pwr_state = VGA_SWITCHEROO_ON; 307 client->pdev = pdev; 308 client->ops = ops; 309 client->id = id; 310 client->active = active; 311 client->driver_power_control = driver_power_control; 312 client->vga_dev = vga_dev; 313 314 mutex_lock(&vgasr_mutex); 315 list_add_tail(&client->list, &vgasr_priv.clients); 316 if (client_is_vga(client)) 317 vgasr_priv.registered_clients++; 318 319 if (vga_switcheroo_ready()) { 320 pr_info("enabled\n"); 321 vga_switcheroo_enable(); 322 } 323 mutex_unlock(&vgasr_mutex); 324 return 0; 325 } 326 327 /** 328 * vga_switcheroo_register_client - register vga client 329 * @pdev: client pci device 330 * @ops: client callbacks 331 * @driver_power_control: whether power state is controlled by the driver's 332 * runtime pm 333 * 334 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a 335 * handler have already registered. The power state of the client is assumed 336 * to be ON. Beforehand, vga_switcheroo_client_probe_defer() shall be called 337 * to ensure that all prerequisites are met. 338 * 339 * Return: 0 on success, -ENOMEM on memory allocation error. 340 */ 341 int vga_switcheroo_register_client(struct pci_dev *pdev, 342 const struct vga_switcheroo_client_ops *ops, 343 bool driver_power_control) 344 { 345 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID, NULL, 346 pdev == vga_default_device(), 347 driver_power_control); 348 } 349 EXPORT_SYMBOL(vga_switcheroo_register_client); 350 351 /** 352 * vga_switcheroo_register_audio_client - register audio client 353 * @pdev: client pci device 354 * @ops: client callbacks 355 * @vga_dev: pci device which is bound to current audio client 356 * 357 * Register audio client (audio device on a GPU). The client is assumed 358 * to use runtime PM. Beforehand, vga_switcheroo_client_probe_defer() 359 * shall be called to ensure that all prerequisites are met. 360 * 361 * Return: 0 on success, -ENOMEM on memory allocation error, -EINVAL on getting 362 * client id error. 363 */ 364 int vga_switcheroo_register_audio_client(struct pci_dev *pdev, 365 const struct vga_switcheroo_client_ops *ops, 366 struct pci_dev *vga_dev) 367 { 368 enum vga_switcheroo_client_id id = VGA_SWITCHEROO_UNKNOWN_ID; 369 370 /* 371 * if vga_switcheroo has enabled, that mean two GPU clients and also 372 * handler are registered. Get audio client id from bound GPU client 373 * id directly, otherwise, set it as VGA_SWITCHEROO_UNKNOWN_ID, 374 * it will set to correct id in later when vga_switcheroo_enable() 375 * is called. 376 */ 377 mutex_lock(&vgasr_mutex); 378 if (vgasr_priv.active) { 379 id = vgasr_priv.handler->get_client_id(vga_dev); 380 if (id < 0) { 381 mutex_unlock(&vgasr_mutex); 382 return -EINVAL; 383 } 384 /* notify if GPU has been already bound */ 385 if (ops->gpu_bound) 386 ops->gpu_bound(pdev, id); 387 } 388 mutex_unlock(&vgasr_mutex); 389 390 return register_client(pdev, ops, id | ID_BIT_AUDIO, vga_dev, 391 false, true); 392 } 393 EXPORT_SYMBOL(vga_switcheroo_register_audio_client); 394 395 static struct vga_switcheroo_client * 396 find_client_from_pci(struct list_head *head, struct pci_dev *pdev) 397 { 398 struct vga_switcheroo_client *client; 399 400 list_for_each_entry(client, head, list) 401 if (client->pdev == pdev) 402 return client; 403 return NULL; 404 } 405 406 static struct vga_switcheroo_client * 407 find_client_from_id(struct list_head *head, 408 enum vga_switcheroo_client_id client_id) 409 { 410 struct vga_switcheroo_client *client; 411 412 list_for_each_entry(client, head, list) 413 if (client->id == client_id) 414 return client; 415 return NULL; 416 } 417 418 static struct vga_switcheroo_client * 419 find_active_client(struct list_head *head) 420 { 421 struct vga_switcheroo_client *client; 422 423 list_for_each_entry(client, head, list) 424 if (client->active) 425 return client; 426 return NULL; 427 } 428 429 /** 430 * vga_switcheroo_client_probe_defer() - whether to defer probing a given client 431 * @pdev: client pci device 432 * 433 * Determine whether any prerequisites are not fulfilled to probe a given 434 * client. Drivers shall invoke this early on in their ->probe callback 435 * and return %-EPROBE_DEFER if it evaluates to %true. Thou shalt not 436 * register the client ere thou hast called this. 437 * 438 * Return: %true if probing should be deferred, otherwise %false. 439 */ 440 bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev) 441 { 442 if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) { 443 /* 444 * apple-gmux is needed on pre-retina MacBook Pro 445 * to probe the panel if pdev is the inactive GPU. 446 */ 447 if (apple_gmux_present() && pdev != vga_default_device() && 448 !vgasr_priv.handler_flags) 449 return true; 450 } 451 452 return false; 453 } 454 EXPORT_SYMBOL(vga_switcheroo_client_probe_defer); 455 456 static enum vga_switcheroo_state 457 vga_switcheroo_pwr_state(struct vga_switcheroo_client *client) 458 { 459 if (client->driver_power_control) 460 if (pm_runtime_enabled(&client->pdev->dev) && 461 pm_runtime_active(&client->pdev->dev)) 462 return VGA_SWITCHEROO_ON; 463 else 464 return VGA_SWITCHEROO_OFF; 465 else 466 return client->pwr_state; 467 } 468 469 /** 470 * vga_switcheroo_get_client_state() - obtain power state of a given client 471 * @pdev: client pci device 472 * 473 * Obtain power state of a given client as seen from vga_switcheroo. 474 * The function is only called from hda_intel.c. 475 * 476 * Return: Power state. 477 */ 478 enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev) 479 { 480 struct vga_switcheroo_client *client; 481 enum vga_switcheroo_state ret; 482 483 mutex_lock(&vgasr_mutex); 484 client = find_client_from_pci(&vgasr_priv.clients, pdev); 485 if (!client) 486 ret = VGA_SWITCHEROO_NOT_FOUND; 487 else 488 ret = vga_switcheroo_pwr_state(client); 489 mutex_unlock(&vgasr_mutex); 490 return ret; 491 } 492 EXPORT_SYMBOL(vga_switcheroo_get_client_state); 493 494 /** 495 * vga_switcheroo_unregister_client() - unregister client 496 * @pdev: client pci device 497 * 498 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU). 499 */ 500 void vga_switcheroo_unregister_client(struct pci_dev *pdev) 501 { 502 struct vga_switcheroo_client *client; 503 504 mutex_lock(&vgasr_mutex); 505 client = find_client_from_pci(&vgasr_priv.clients, pdev); 506 if (client) { 507 if (client_is_vga(client)) 508 vgasr_priv.registered_clients--; 509 list_del(&client->list); 510 kfree(client); 511 } 512 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) { 513 pr_info("disabled\n"); 514 vga_switcheroo_debugfs_fini(&vgasr_priv); 515 vgasr_priv.active = false; 516 } 517 mutex_unlock(&vgasr_mutex); 518 } 519 EXPORT_SYMBOL(vga_switcheroo_unregister_client); 520 521 /** 522 * vga_switcheroo_client_fb_set() - set framebuffer of a given client 523 * @pdev: client pci device 524 * @info: framebuffer 525 * 526 * Set framebuffer of a given client. The console will be remapped to this 527 * on switching. 528 */ 529 void vga_switcheroo_client_fb_set(struct pci_dev *pdev, 530 struct fb_info *info) 531 { 532 struct vga_switcheroo_client *client; 533 534 mutex_lock(&vgasr_mutex); 535 client = find_client_from_pci(&vgasr_priv.clients, pdev); 536 if (client) 537 client->fb_info = info; 538 mutex_unlock(&vgasr_mutex); 539 } 540 EXPORT_SYMBOL(vga_switcheroo_client_fb_set); 541 542 /** 543 * vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client 544 * @pdev: client pci device 545 * 546 * Temporarily switch DDC lines to the client identified by @pdev 547 * (but leave the outputs otherwise switched to where they are). 548 * This allows the inactive client to probe EDID. The DDC lines must 549 * afterwards be switched back by calling vga_switcheroo_unlock_ddc(), 550 * even if this function returns an error. 551 * 552 * Return: Previous DDC owner on success or a negative int on error. 553 * Specifically, %-ENODEV if no handler has registered or if the handler 554 * does not support switching the DDC lines. Also, a negative value 555 * returned by the handler is propagated back to the caller. 556 * The return value has merely an informational purpose for any caller 557 * which might be interested in it. It is acceptable to ignore the return 558 * value and simply rely on the result of the subsequent EDID probe, 559 * which will be %NULL if DDC switching failed. 560 */ 561 int vga_switcheroo_lock_ddc(struct pci_dev *pdev) 562 { 563 enum vga_switcheroo_client_id id; 564 565 mutex_lock(&vgasr_priv.mux_hw_lock); 566 if (!vgasr_priv.handler || !vgasr_priv.handler->switch_ddc) { 567 vgasr_priv.old_ddc_owner = -ENODEV; 568 return -ENODEV; 569 } 570 571 id = vgasr_priv.handler->get_client_id(pdev); 572 vgasr_priv.old_ddc_owner = vgasr_priv.handler->switch_ddc(id); 573 return vgasr_priv.old_ddc_owner; 574 } 575 EXPORT_SYMBOL(vga_switcheroo_lock_ddc); 576 577 /** 578 * vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner 579 * @pdev: client pci device 580 * 581 * Switch DDC lines back to the previous owner after calling 582 * vga_switcheroo_lock_ddc(). This must be called even if 583 * vga_switcheroo_lock_ddc() returned an error. 584 * 585 * Return: Previous DDC owner on success (i.e. the client identifier of @pdev) 586 * or a negative int on error. 587 * Specifically, %-ENODEV if no handler has registered or if the handler 588 * does not support switching the DDC lines. Also, a negative value 589 * returned by the handler is propagated back to the caller. 590 * Finally, invoking this function without calling vga_switcheroo_lock_ddc() 591 * first is not allowed and will result in %-EINVAL. 592 */ 593 int vga_switcheroo_unlock_ddc(struct pci_dev *pdev) 594 { 595 enum vga_switcheroo_client_id id; 596 int ret = vgasr_priv.old_ddc_owner; 597 598 if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv.mux_hw_lock))) 599 return -EINVAL; 600 601 if (vgasr_priv.old_ddc_owner >= 0) { 602 id = vgasr_priv.handler->get_client_id(pdev); 603 if (vgasr_priv.old_ddc_owner != id) 604 ret = vgasr_priv.handler->switch_ddc( 605 vgasr_priv.old_ddc_owner); 606 } 607 mutex_unlock(&vgasr_priv.mux_hw_lock); 608 return ret; 609 } 610 EXPORT_SYMBOL(vga_switcheroo_unlock_ddc); 611 612 /** 613 * DOC: Manual switching and manual power control 614 * 615 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch 616 * can be read to retrieve the current vga_switcheroo state and commands 617 * can be written to it to change the state. The file appears as soon as 618 * two GPU drivers and one handler have registered with vga_switcheroo. 619 * The following commands are understood: 620 * 621 * * OFF: Power off the device not in use. 622 * * ON: Power on the device not in use. 623 * * IGD: Switch to the integrated graphics device. 624 * Power on the integrated GPU if necessary, power off the discrete GPU. 625 * Prerequisite is that no user space processes (e.g. Xorg, alsactl) 626 * have opened device files of the GPUs or the audio client. If the 627 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/ 628 * and /dev/snd/controlC1 to identify processes blocking the switch. 629 * * DIS: Switch to the discrete graphics device. 630 * * DIGD: Delayed switch to the integrated graphics device. 631 * This will perform the switch once the last user space process has 632 * closed the device files of the GPUs and the audio client. 633 * * DDIS: Delayed switch to the discrete graphics device. 634 * * MIGD: Mux-only switch to the integrated graphics device. 635 * Does not remap console or change the power state of either gpu. 636 * If the integrated GPU is currently off, the screen will turn black. 637 * If it is on, the screen will show whatever happens to be in VRAM. 638 * Either way, the user has to blindly enter the command to switch back. 639 * * MDIS: Mux-only switch to the discrete graphics device. 640 * 641 * For GPUs whose power state is controlled by the driver's runtime pm, 642 * the ON and OFF commands are a no-op (see next section). 643 * 644 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands 645 * should not be used. 646 */ 647 648 static int vga_switcheroo_show(struct seq_file *m, void *v) 649 { 650 struct vga_switcheroo_client *client; 651 int i = 0; 652 653 mutex_lock(&vgasr_mutex); 654 list_for_each_entry(client, &vgasr_priv.clients, list) { 655 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i, 656 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : 657 "IGD", 658 client_is_vga(client) ? "" : "-Audio", 659 client->active ? '+' : ' ', 660 client->driver_power_control ? "Dyn" : "", 661 vga_switcheroo_pwr_state(client) ? "Pwr" : "Off", 662 pci_name(client->pdev)); 663 i++; 664 } 665 mutex_unlock(&vgasr_mutex); 666 return 0; 667 } 668 669 static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file) 670 { 671 return single_open(file, vga_switcheroo_show, NULL); 672 } 673 674 static int vga_switchon(struct vga_switcheroo_client *client) 675 { 676 if (client->driver_power_control) 677 return 0; 678 if (vgasr_priv.handler->power_state) 679 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON); 680 /* call the driver callback to turn on device */ 681 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON); 682 client->pwr_state = VGA_SWITCHEROO_ON; 683 return 0; 684 } 685 686 static int vga_switchoff(struct vga_switcheroo_client *client) 687 { 688 if (client->driver_power_control) 689 return 0; 690 /* call the driver callback to turn off device */ 691 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF); 692 if (vgasr_priv.handler->power_state) 693 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF); 694 client->pwr_state = VGA_SWITCHEROO_OFF; 695 return 0; 696 } 697 698 static void set_audio_state(enum vga_switcheroo_client_id id, 699 enum vga_switcheroo_state state) 700 { 701 struct vga_switcheroo_client *client; 702 703 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO); 704 if (client) 705 client->ops->set_gpu_state(client->pdev, state); 706 } 707 708 /* stage one happens before delay */ 709 static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) 710 { 711 struct vga_switcheroo_client *active; 712 713 active = find_active_client(&vgasr_priv.clients); 714 if (!active) 715 return 0; 716 717 if (vga_switcheroo_pwr_state(new_client) == VGA_SWITCHEROO_OFF) 718 vga_switchon(new_client); 719 720 vga_set_default_device(new_client->pdev); 721 return 0; 722 } 723 724 /* post delay */ 725 static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) 726 { 727 int ret; 728 struct vga_switcheroo_client *active; 729 730 active = find_active_client(&vgasr_priv.clients); 731 if (!active) 732 return 0; 733 734 active->active = false; 735 736 /* let HDA controller autosuspend if GPU uses driver power control */ 737 if (!active->driver_power_control) 738 set_audio_state(active->id, VGA_SWITCHEROO_OFF); 739 740 if (new_client->fb_info) 741 fbcon_remap_all(new_client->fb_info); 742 743 mutex_lock(&vgasr_priv.mux_hw_lock); 744 ret = vgasr_priv.handler->switchto(new_client->id); 745 mutex_unlock(&vgasr_priv.mux_hw_lock); 746 if (ret) 747 return ret; 748 749 if (new_client->ops->reprobe) 750 new_client->ops->reprobe(new_client->pdev); 751 752 if (vga_switcheroo_pwr_state(active) == VGA_SWITCHEROO_ON) 753 vga_switchoff(active); 754 755 /* let HDA controller autoresume if GPU uses driver power control */ 756 if (!new_client->driver_power_control) 757 set_audio_state(new_client->id, VGA_SWITCHEROO_ON); 758 759 new_client->active = true; 760 return 0; 761 } 762 763 static bool check_can_switch(void) 764 { 765 struct vga_switcheroo_client *client; 766 767 list_for_each_entry(client, &vgasr_priv.clients, list) { 768 if (!client->ops->can_switch(client->pdev)) { 769 pr_err("client %x refused switch\n", client->id); 770 return false; 771 } 772 } 773 return true; 774 } 775 776 static ssize_t 777 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, 778 size_t cnt, loff_t *ppos) 779 { 780 char usercmd[64]; 781 int ret; 782 bool delay = false, can_switch; 783 bool just_mux = false; 784 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID; 785 struct vga_switcheroo_client *client = NULL; 786 787 if (cnt > 63) 788 cnt = 63; 789 790 if (copy_from_user(usercmd, ubuf, cnt)) 791 return -EFAULT; 792 793 mutex_lock(&vgasr_mutex); 794 795 if (!vgasr_priv.active) { 796 cnt = -EINVAL; 797 goto out; 798 } 799 800 /* pwr off the device not in use */ 801 if (strncmp(usercmd, "OFF", 3) == 0) { 802 list_for_each_entry(client, &vgasr_priv.clients, list) { 803 if (client->active || client_is_audio(client)) 804 continue; 805 if (client->driver_power_control) 806 continue; 807 set_audio_state(client->id, VGA_SWITCHEROO_OFF); 808 if (client->pwr_state == VGA_SWITCHEROO_ON) 809 vga_switchoff(client); 810 } 811 goto out; 812 } 813 /* pwr on the device not in use */ 814 if (strncmp(usercmd, "ON", 2) == 0) { 815 list_for_each_entry(client, &vgasr_priv.clients, list) { 816 if (client->active || client_is_audio(client)) 817 continue; 818 if (client->driver_power_control) 819 continue; 820 if (client->pwr_state == VGA_SWITCHEROO_OFF) 821 vga_switchon(client); 822 set_audio_state(client->id, VGA_SWITCHEROO_ON); 823 } 824 goto out; 825 } 826 827 /* request a delayed switch - test can we switch now */ 828 if (strncmp(usercmd, "DIGD", 4) == 0) { 829 client_id = VGA_SWITCHEROO_IGD; 830 delay = true; 831 } 832 833 if (strncmp(usercmd, "DDIS", 4) == 0) { 834 client_id = VGA_SWITCHEROO_DIS; 835 delay = true; 836 } 837 838 if (strncmp(usercmd, "IGD", 3) == 0) 839 client_id = VGA_SWITCHEROO_IGD; 840 841 if (strncmp(usercmd, "DIS", 3) == 0) 842 client_id = VGA_SWITCHEROO_DIS; 843 844 if (strncmp(usercmd, "MIGD", 4) == 0) { 845 just_mux = true; 846 client_id = VGA_SWITCHEROO_IGD; 847 } 848 if (strncmp(usercmd, "MDIS", 4) == 0) { 849 just_mux = true; 850 client_id = VGA_SWITCHEROO_DIS; 851 } 852 853 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID) 854 goto out; 855 client = find_client_from_id(&vgasr_priv.clients, client_id); 856 if (!client) 857 goto out; 858 859 vgasr_priv.delayed_switch_active = false; 860 861 if (just_mux) { 862 mutex_lock(&vgasr_priv.mux_hw_lock); 863 ret = vgasr_priv.handler->switchto(client_id); 864 mutex_unlock(&vgasr_priv.mux_hw_lock); 865 goto out; 866 } 867 868 if (client->active) 869 goto out; 870 871 /* okay we want a switch - test if devices are willing to switch */ 872 can_switch = check_can_switch(); 873 874 if (can_switch == false && delay == false) 875 goto out; 876 877 if (can_switch) { 878 ret = vga_switchto_stage1(client); 879 if (ret) 880 pr_err("switching failed stage 1 %d\n", ret); 881 882 ret = vga_switchto_stage2(client); 883 if (ret) 884 pr_err("switching failed stage 2 %d\n", ret); 885 886 } else { 887 pr_info("setting delayed switch to client %d\n", client->id); 888 vgasr_priv.delayed_switch_active = true; 889 vgasr_priv.delayed_client_id = client_id; 890 891 ret = vga_switchto_stage1(client); 892 if (ret) 893 pr_err("delayed switching stage 1 failed %d\n", ret); 894 } 895 896 out: 897 mutex_unlock(&vgasr_mutex); 898 return cnt; 899 } 900 901 static const struct file_operations vga_switcheroo_debugfs_fops = { 902 .owner = THIS_MODULE, 903 .open = vga_switcheroo_debugfs_open, 904 .write = vga_switcheroo_debugfs_write, 905 .read = seq_read, 906 .llseek = seq_lseek, 907 .release = single_release, 908 }; 909 910 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv) 911 { 912 debugfs_remove(priv->switch_file); 913 priv->switch_file = NULL; 914 915 debugfs_remove(priv->debugfs_root); 916 priv->debugfs_root = NULL; 917 } 918 919 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv) 920 { 921 static const char mp[] = "/sys/kernel/debug"; 922 923 /* already initialised */ 924 if (priv->debugfs_root) 925 return 0; 926 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL); 927 928 if (!priv->debugfs_root) { 929 pr_err("Cannot create %s/vgaswitcheroo\n", mp); 930 goto fail; 931 } 932 933 priv->switch_file = debugfs_create_file("switch", 0644, 934 priv->debugfs_root, NULL, 935 &vga_switcheroo_debugfs_fops); 936 if (!priv->switch_file) { 937 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp); 938 goto fail; 939 } 940 return 0; 941 fail: 942 vga_switcheroo_debugfs_fini(priv); 943 return -1; 944 } 945 946 /** 947 * vga_switcheroo_process_delayed_switch() - helper for delayed switching 948 * 949 * Process a delayed switch if one is pending. DRM drivers should call this 950 * from their ->lastclose callback. 951 * 952 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client 953 * has unregistered in the meantime or if there are other clients blocking the 954 * switch. If the actual switch fails, an error is reported and 0 is returned. 955 */ 956 int vga_switcheroo_process_delayed_switch(void) 957 { 958 struct vga_switcheroo_client *client; 959 int ret; 960 int err = -EINVAL; 961 962 mutex_lock(&vgasr_mutex); 963 if (!vgasr_priv.delayed_switch_active) 964 goto err; 965 966 pr_info("processing delayed switch to %d\n", 967 vgasr_priv.delayed_client_id); 968 969 client = find_client_from_id(&vgasr_priv.clients, 970 vgasr_priv.delayed_client_id); 971 if (!client || !check_can_switch()) 972 goto err; 973 974 ret = vga_switchto_stage2(client); 975 if (ret) 976 pr_err("delayed switching failed stage 2 %d\n", ret); 977 978 vgasr_priv.delayed_switch_active = false; 979 err = 0; 980 err: 981 mutex_unlock(&vgasr_mutex); 982 return err; 983 } 984 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch); 985 986 /** 987 * DOC: Driver power control 988 * 989 * In this mode of use, the discrete GPU automatically powers up and down at 990 * the discretion of the driver's runtime pm. On muxed machines, the user may 991 * still influence the muxer state by way of the debugfs interface, however 992 * the ON and OFF commands become a no-op for the discrete GPU. 993 * 994 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress. 995 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel 996 * command line disables it. 997 * 998 * After the GPU has been suspended, the handler needs to be called to cut 999 * power to the GPU. Likewise it needs to reinstate power before the GPU 1000 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(), 1001 * which augments the GPU's suspend/resume functions by the requisite 1002 * calls to the handler. 1003 * 1004 * When the audio device resumes, the GPU needs to be woken. This is achieved 1005 * by a PCI quirk which calls device_link_add() to declare a dependency on the 1006 * GPU. That way, the GPU is kept awake whenever and as long as the audio 1007 * device is in use. 1008 * 1009 * On muxed machines, if the mux is initially switched to the discrete GPU, 1010 * the user ends up with a black screen when the GPU powers down after boot. 1011 * As a workaround, the mux is forced to the integrated GPU on runtime suspend, 1012 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917 1013 */ 1014 1015 static void vga_switcheroo_power_switch(struct pci_dev *pdev, 1016 enum vga_switcheroo_state state) 1017 { 1018 struct vga_switcheroo_client *client; 1019 1020 if (!vgasr_priv.handler->power_state) 1021 return; 1022 1023 client = find_client_from_pci(&vgasr_priv.clients, pdev); 1024 if (!client) 1025 return; 1026 1027 if (!client->driver_power_control) 1028 return; 1029 1030 vgasr_priv.handler->power_state(client->id, state); 1031 } 1032 1033 /* switcheroo power domain */ 1034 static int vga_switcheroo_runtime_suspend(struct device *dev) 1035 { 1036 struct pci_dev *pdev = to_pci_dev(dev); 1037 int ret; 1038 1039 ret = dev->bus->pm->runtime_suspend(dev); 1040 if (ret) 1041 return ret; 1042 mutex_lock(&vgasr_mutex); 1043 if (vgasr_priv.handler->switchto) { 1044 mutex_lock(&vgasr_priv.mux_hw_lock); 1045 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD); 1046 mutex_unlock(&vgasr_priv.mux_hw_lock); 1047 } 1048 pci_bus_set_current_state(pdev->bus, PCI_D3cold); 1049 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF); 1050 mutex_unlock(&vgasr_mutex); 1051 return 0; 1052 } 1053 1054 static int vga_switcheroo_runtime_resume(struct device *dev) 1055 { 1056 struct pci_dev *pdev = to_pci_dev(dev); 1057 int ret; 1058 1059 mutex_lock(&vgasr_mutex); 1060 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON); 1061 mutex_unlock(&vgasr_mutex); 1062 pci_wakeup_bus(pdev->bus); 1063 ret = dev->bus->pm->runtime_resume(dev); 1064 if (ret) 1065 return ret; 1066 1067 return 0; 1068 } 1069 1070 /** 1071 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control 1072 * @dev: vga client device 1073 * @domain: power domain 1074 * 1075 * Helper for GPUs whose power state is controlled by the driver's runtime pm. 1076 * After the GPU has been suspended, the handler needs to be called to cut 1077 * power to the GPU. Likewise it needs to reinstate power before the GPU 1078 * can resume. To this end, this helper augments the suspend/resume functions 1079 * by the requisite calls to the handler. It needs only be called on platforms 1080 * where the power switch is separate to the device being powered down. 1081 */ 1082 int vga_switcheroo_init_domain_pm_ops(struct device *dev, 1083 struct dev_pm_domain *domain) 1084 { 1085 /* copy over all the bus versions */ 1086 if (dev->bus && dev->bus->pm) { 1087 domain->ops = *dev->bus->pm; 1088 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend; 1089 domain->ops.runtime_resume = vga_switcheroo_runtime_resume; 1090 1091 dev_pm_domain_set(dev, domain); 1092 return 0; 1093 } 1094 dev_pm_domain_set(dev, NULL); 1095 return -EINVAL; 1096 } 1097 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops); 1098 1099 void vga_switcheroo_fini_domain_pm_ops(struct device *dev) 1100 { 1101 dev_pm_domain_set(dev, NULL); 1102 } 1103 EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops); 1104