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