1 /*
2 * vga_switcheroo.c -- vga_switcheroo driver for DragonFly
3 *
4 * Adapted from linux v4.8: linux-src/drivers/gpu/vga/vga_switcheroo.c
5 */
6
7 /*
8 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
9 *
10 * Copyright (c) 2010 Red Hat Inc.
11 * Author : Dave Airlie <airlied@redhat.com>
12 *
13 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice (including the next
23 * paragraph) shall be included in all copies or substantial portions of the
24 * Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 * DEALINGS
33 * IN THE SOFTWARE.
34 */
35
36 #include <sys/param.h>
37 #include <sys/kernel.h>
38 #include <sys/bio.h>
39 #include <sys/bus.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
42 #include <sys/proc.h>
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
45 #include <sys/systm.h>
46 #include <sys/device.h>
47 #include <sys/conf.h>
48 #include <sys/rman.h>
49 #include <sys/uio.h>
50
51 #define pr_fmt(fmt) "vga_switcheroo: " fmt
52
53 #include <linux/fb.h>
54 #include <linux/fs.h>
55 #include <linux/module.h>
56 #include <linux/pci.h>
57 #include <linux/pci_ids.h>
58 #include <linux/mutex.h>
59 #include <linux/list.h>
60 #include <linux/export.h>
61
62 #define VGA_SWITCHEROO
63 #include <linux/vga_switcheroo.h>
64
65 /**
66 * DOC: Overview
67 *
68 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
69 * These come in two flavors:
70 *
71 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
72 * * muxless: Dual GPUs but only one of them is connected to outputs.
73 * The other one is merely used to offload rendering, its results
74 * are copied over PCIe into the framebuffer. On Linux this is
75 * supported with DRI PRIME.
76 *
77 * Hybrid graphics started to appear in the late Naughties and were initially
78 * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
79 * A notable exception is the MacBook Pro which continues to use a mux.
80 * Muxes come with varying capabilities: Some switch only the panel, others
81 * can also switch external displays. Some switch all display pins at once
82 * while others can switch just the DDC lines. (To allow EDID probing
83 * for the inactive GPU.) Also, muxes are often used to cut power to the
84 * discrete GPU while it is not used.
85 *
86 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
87 * clients. The mux is called the handler. Muxless machines also register a
88 * handler to control the power state of the discrete GPU, its ->switchto
89 * callback is a no-op for obvious reasons. The discrete GPU is often equipped
90 * with an HDA controller for the HDMI/DP audio signal, this will also
91 * register as a client so that vga_switcheroo can take care of the correct
92 * suspend/resume order when changing the discrete GPU's power state. In total
93 * there can thus be up to three clients: Two vga clients (GPUs) and one audio
94 * client (on the discrete GPU). The code is mostly prepared to support
95 * machines with more than two GPUs should they become available.
96 *
97 * The GPU to which the outputs are currently switched is called the
98 * active client in vga_switcheroo parlance. The GPU not in use is the
99 * inactive client. When the inactive client's DRM driver is loaded,
100 * it will be unable to probe the panel's EDID and hence depends on
101 * VBIOS to provide its display modes. If the VBIOS modes are bogus or
102 * if there is no VBIOS at all (which is common on the MacBook Pro),
103 * a client may alternatively request that the DDC lines are temporarily
104 * switched to it, provided that the handler supports this. Switching
105 * only the DDC lines and not the entire output avoids unnecessary
106 * flickering.
107 */
108
109 /**
110 * struct vga_switcheroo_client - registered client
111 * @pdev: client pci device
112 * @fb_info: framebuffer to which console is remapped on switching
113 * @pwr_state: current power state
114 * @ops: client callbacks
115 * @id: client identifier. Determining the id requires the handler,
116 * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
117 * and later given their true id in vga_switcheroo_enable()
118 * @active: whether the outputs are currently switched to this client
119 * @driver_power_control: whether power state is controlled by the driver's
120 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
121 * interface is a no-op so as not to interfere with runtime pm
122 * @list: client list
123 *
124 * Registered client. A client can be either a GPU or an audio device on a GPU.
125 * For audio clients, the @fb_info, @active and @driver_power_control members
126 * are bogus.
127 */
128 struct vga_switcheroo_client {
129 struct pci_dev *pdev;
130 struct fb_info *fb_info;
131 enum vga_switcheroo_state pwr_state;
132 const struct vga_switcheroo_client_ops *ops;
133 enum vga_switcheroo_client_id id;
134 bool active;
135 bool driver_power_control;
136 struct list_head list;
137 };
138
139 /*
140 * protects access to struct vgasr_priv
141 */
142 static DEFINE_MUTEX(vgasr_mutex);
143
144 /**
145 * struct vgasr_priv - vga_switcheroo private data
146 * @active: whether vga_switcheroo is enabled.
147 * Prerequisite is the registration of two GPUs and a handler
148 * @delayed_switch_active: whether a delayed switch is pending
149 * @delayed_client_id: client to which a delayed switch is pending
150 * @debugfs_root: directory for vga_switcheroo debugfs interface
151 * @switch_file: file for vga_switcheroo debugfs interface
152 * @registered_clients: number of registered GPUs
153 * (counting only vga clients, not audio clients)
154 * @clients: list of registered clients
155 * @handler: registered handler
156 * @handler_flags: flags of registered handler
157 * @mux_hw_lock: protects mux state
158 * (in particular while DDC lines are temporarily switched)
159 * @old_ddc_owner: client to which DDC lines will be switched back on unlock
160 *
161 * vga_switcheroo private data. Currently only one vga_switcheroo instance
162 * per system is supported.
163 */
164 struct vgasr_priv {
165 bool active;
166 bool delayed_switch_active;
167 enum vga_switcheroo_client_id delayed_client_id;
168
169 struct dentry *debugfs_root;
170 struct dentry *switch_file;
171
172 int registered_clients;
173 struct list_head clients;
174
175 const struct vga_switcheroo_handler *handler;
176 enum vga_switcheroo_handler_flags_t handler_flags;
177 struct lock mux_hw_lk;
178 int old_ddc_owner;
179 };
180
181 #define ID_BIT_AUDIO 0x100
182 #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
183 #define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
184 !client_is_audio(c))
185 #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
186
187 #if 0
188 static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
189 static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
190 #endif
191
192 /* only one switcheroo per system */
193 static struct vgasr_priv *vgasr_priv;
194
195 static bool
vga_switcheroo_ready(void)196 vga_switcheroo_ready(void)
197 {
198 /* we're ready if we get two clients + handler */
199 return !vgasr_priv->active &&
200 vgasr_priv->registered_clients == 2 && vgasr_priv->handler;
201 }
202
203 static void
vga_switcheroo_enable(void)204 vga_switcheroo_enable(void)
205 {
206 int ret;
207 struct vga_switcheroo_client *client;
208
209 /* call the handler to init */
210 if (vgasr_priv->handler->init)
211 vgasr_priv->handler->init();
212
213 list_for_each_entry(client, &vgasr_priv->clients, list) {
214 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
215 continue;
216 ret = vgasr_priv->handler->get_client_id(client->pdev);
217 if (ret < 0)
218 return;
219
220 client->id = ret;
221 }
222 //XXX: TODO
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(const struct vga_switcheroo_handler * handler,enum vga_switcheroo_handler_flags_t handler_flags)238 vga_switcheroo_register_handler(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 vga_switcheroo_unregister_handler(void)
265 {
266 mutex_lock(&vgasr_mutex);
267 mutex_lock(&vgasr_priv->mux_hw_lk);
268 vgasr_priv->handler_flags = 0;
269 vgasr_priv->handler = NULL;
270 if (vgasr_priv->active) {
271 pr_info("disabled\n");
272 //XXX TODO:
273 //vga_switcheroo_debugfs_fini(&vgasr_priv);
274 vgasr_priv->active = false;
275 }
276 mutex_unlock(&vgasr_priv->mux_hw_lk);
277 mutex_unlock(&vgasr_mutex);
278 }
279 EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
280
281 /**
282 * vga_switcheroo_handler_flags() - obtain handler flags
283 *
284 * Helper for clients to obtain the handler flags bitmask.
285 *
286 * Return: Handler flags. A value of 0 means that no handler is registered
287 * or that the handler has no special capabilities.
288 */
289 enum vga_switcheroo_handler_flags_t
vga_switcheroo_handler_flags(void)290 vga_switcheroo_handler_flags(void)
291 {
292 return vgasr_priv->handler_flags;
293 }
294 EXPORT_SYMBOL(vga_switcheroo_handler_flags);
295
296 MALLOC_DECLARE(M_VGA_SWITCHEROO_CLIENT);
297 MALLOC_DEFINE(M_VGA_SWITCHEROO_CLIENT, "vga_switcheroo_client", "vga_switcheroo client data");
298
299 static int
register_client(struct pci_dev * pdev,const struct vga_switcheroo_client_ops * ops,enum vga_switcheroo_client_id id,bool active,bool driver_power_control)300 register_client(struct pci_dev *pdev,
301 const struct vga_switcheroo_client_ops *ops,
302 enum vga_switcheroo_client_id id, bool active,
303 bool driver_power_control)
304 {
305 struct vga_switcheroo_client *client;
306
307 client = kmalloc(sizeof(*client), M_VGA_SWITCHEROO_CLIENT, M_WAITOK | M_ZERO);
308 if (!client)
309 return -ENOMEM;
310
311 client->pwr_state = VGA_SWITCHEROO_ON;
312 client->pdev = pdev;
313 client->ops = ops;
314 client->id = id;
315 client->active = active;
316 client->driver_power_control = driver_power_control;
317
318 mutex_lock(&vgasr_mutex);
319 list_add_tail(&client->list, &vgasr_priv->clients);
320 if (client_is_vga(client))
321 vgasr_priv->registered_clients++;
322
323 if (vga_switcheroo_ready()) {
324 pr_info("enabled\n");
325 vga_switcheroo_enable();
326 }
327 mutex_unlock(&vgasr_mutex);
328 return 0;
329 }
330
331 /**
332 * vga_switcheroo_register_client - register vga client
333 * @pdev: client pci device
334 * @ops: client callbacks
335 * @driver_power_control: whether power state is controlled by the driver's
336 * runtime pm
337 *
338 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
339 * handler have already registered. The power state of the client is assumed
340 * to be ON.
341 *
342 * Return: 0 on success, -ENOMEM on memory allocation error.
343 */
344 int
vga_switcheroo_register_client(struct pci_dev * pdev,const struct vga_switcheroo_client_ops * ops,bool driver_power_control)345 vga_switcheroo_register_client(struct pci_dev *pdev,
346 const struct vga_switcheroo_client_ops *ops,
347 bool driver_power_control)
348 {
349 int error = 0;
350 int unit, default_vgapci_unit;
351 size_t len;
352
353 /*
354 * We need to find out if video device pci_dev is
355 * the active (default) video device: will determine
356 * if the pci_dev unit is the default_vgapci_unit
357 */
358 unit = device_get_unit(pdev->dev.bsddev);
359
360 len = sizeof(default_vgapci_unit);
361 error = kernel_sysctlbyname("hw.pci.default_vgapci_unit", &default_vgapci_unit, &len, NULL, 0, NULL);
362 if (error) {
363 pr_err("kernel_sysctlbyname: error %d\n", error);
364 return (error);
365 }
366
367 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
368 unit == default_vgapci_unit,
369 driver_power_control);
370 }
371 EXPORT_SYMBOL(vga_switcheroo_register_client);
372
373 /**
374 * vga_switcheroo_register_audio_client - register audio client
375 * @pdev: client pci device
376 * @ops: client callbacks
377 * @id: client identifier
378 *
379 * Register audio client (audio device on a GPU). The power state of the
380 * client is assumed to be ON.
381 *
382 * Return: 0 on success, -ENOMEM on memory allocation error.
383 */
384 int
vga_switcheroo_register_audio_client(struct pci_dev * pdev,const struct vga_switcheroo_client_ops * ops,enum vga_switcheroo_client_id id)385 vga_switcheroo_register_audio_client(struct pci_dev *pdev,
386 const struct vga_switcheroo_client_ops *ops,
387 enum vga_switcheroo_client_id id)
388 {
389 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
390 }
391 EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
392
393 static struct vga_switcheroo_client *
find_client_from_pci(struct list_head * head,struct pci_dev * pdev)394 find_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
404 static struct vga_switcheroo_client *
find_client_from_id(struct list_head * head,enum vga_switcheroo_client_id client_id)405 find_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
416 static struct vga_switcheroo_client *
find_active_client(struct list_head * head)417 find_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 */
438 #if 0
439 bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev)
440 {
441 if ((pdev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
442 /*
443 * apple-gmux is needed on pre-retina MacBook Pro
444 * to probe the panel if pdev is the inactive GPU.
445 */
446 if (apple_gmux_present() && pdev != vga_default_device() &&
447 !vgasr_priv.handler_flags)
448 return true;
449 }
450
451 return false;
452 }
453 EXPORT_SYMBOL(vga_switcheroo_client_probe_defer);
454 #endif
455
456 /**
457 * vga_switcheroo_get_client_state() - obtain power state of a given client
458 * @pdev: client pci device
459 *
460 * Obtain power state of a given client as seen from vga_switcheroo.
461 * The function is only called from hda_intel.c.
462 *
463 * Return: Power state.
464 */
465 enum vga_switcheroo_state
vga_switcheroo_get_client_state(struct pci_dev * pdev)466 vga_switcheroo_get_client_state(struct pci_dev *pdev)
467 {
468 struct vga_switcheroo_client *client;
469 enum vga_switcheroo_state ret;
470
471 mutex_lock(&vgasr_mutex);
472 client = find_client_from_pci(&vgasr_priv->clients, pdev);
473 if (!client)
474 ret = VGA_SWITCHEROO_NOT_FOUND;
475 else
476 ret = client->pwr_state;
477 mutex_unlock(&vgasr_mutex);
478 return ret;
479 }
480 EXPORT_SYMBOL(vga_switcheroo_get_client_state);
481
482 /**
483 * vga_switcheroo_unregister_client() - unregister client
484 * @pdev: client pci device
485 *
486 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
487 */
488 void
vga_switcheroo_unregister_client(struct pci_dev * pdev)489 vga_switcheroo_unregister_client(struct pci_dev *pdev)
490 {
491 struct vga_switcheroo_client *client;
492
493 mutex_lock(&vgasr_mutex);
494 client = find_client_from_pci(&vgasr_priv->clients, pdev);
495 if (client) {
496 if (client_is_vga(client))
497 vgasr_priv->registered_clients--;
498 list_del(&client->list);
499 kfree(client, M_VGA_SWITCHEROO_CLIENT);
500 }
501 if (vgasr_priv->active && vgasr_priv->registered_clients < 2) {
502 pr_info("disabled\n");
503 //XXX: TODO
504 //vga_switcheroo_debugfs_fini(&vgasr_priv);
505 vgasr_priv->active = false;
506 }
507 mutex_unlock(&vgasr_mutex);
508 }
509 EXPORT_SYMBOL(vga_switcheroo_unregister_client);
510
511 /**
512 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
513 * @pdev: client pci device
514 * @info: framebuffer
515 *
516 * Set framebuffer of a given client. The console will be remapped to this
517 * on switching.
518 */
519 void
vga_switcheroo_client_fb_set(struct pci_dev * pdev,struct fb_info * info)520 vga_switcheroo_client_fb_set(struct pci_dev *pdev,
521 struct fb_info *info)
522 {
523 struct vga_switcheroo_client *client;
524
525 mutex_lock(&vgasr_mutex);
526 client = find_client_from_pci(&vgasr_priv->clients, pdev);
527 if (client)
528 client->fb_info = info;
529 mutex_unlock(&vgasr_mutex);
530 }
531 EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
532
533 /**
534 * vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client
535 * @pdev: client pci device
536 *
537 * Temporarily switch DDC lines to the client identified by @pdev
538 * (but leave the outputs otherwise switched to where they are).
539 * This allows the inactive client to probe EDID. The DDC lines must
540 * afterwards be switched back by calling vga_switcheroo_unlock_ddc(),
541 * even if this function returns an error.
542 *
543 * Return: Previous DDC owner on success or a negative int on error.
544 * Specifically, %-ENODEV if no handler has registered or if the handler
545 * does not support switching the DDC lines. Also, a negative value
546 * returned by the handler is propagated back to the caller.
547 * The return value has merely an informational purpose for any caller
548 * which might be interested in it. It is acceptable to ignore the return
549 * value and simply rely on the result of the subsequent EDID probe,
550 * which will be %NULL if DDC switching failed.
551 */
552 int
vga_switcheroo_lock_ddc(struct pci_dev * pdev)553 vga_switcheroo_lock_ddc(struct pci_dev *pdev)
554 {
555 enum vga_switcheroo_client_id id;
556
557 mutex_lock(&vgasr_priv->mux_hw_lk);
558 if (!vgasr_priv->handler || !vgasr_priv->handler->switch_ddc) {
559 vgasr_priv->old_ddc_owner = -ENODEV;
560 return -ENODEV;
561 }
562
563 id = vgasr_priv->handler->get_client_id(pdev);
564 vgasr_priv->old_ddc_owner = vgasr_priv->handler->switch_ddc(id);
565 return vgasr_priv->old_ddc_owner;
566 }
567 EXPORT_SYMBOL(vga_switcheroo_lock_ddc);
568
569 /**
570 * vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner
571 * @pdev: client pci device
572 *
573 * Switch DDC lines back to the previous owner after calling
574 * vga_switcheroo_lock_ddc(). This must be called even if
575 * vga_switcheroo_lock_ddc() returned an error.
576 *
577 * Return: Previous DDC owner on success (i.e. the client identifier of @pdev)
578 * or a negative int on error.
579 * Specifically, %-ENODEV if no handler has registered or if the handler
580 * does not support switching the DDC lines. Also, a negative value
581 * returned by the handler is propagated back to the caller.
582 * Finally, invoking this function without calling vga_switcheroo_lock_ddc()
583 * first is not allowed and will result in %-EINVAL.
584 */
585 int
vga_switcheroo_unlock_ddc(struct pci_dev * pdev)586 vga_switcheroo_unlock_ddc(struct pci_dev *pdev)
587 {
588 enum vga_switcheroo_client_id id;
589 int ret = vgasr_priv->old_ddc_owner;
590
591 if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv->mux_hw_lk)))
592 return -EINVAL;
593
594 if (vgasr_priv->old_ddc_owner >= 0) {
595 id = vgasr_priv->handler->get_client_id(pdev);
596 if (vgasr_priv->old_ddc_owner != id)
597 ret = vgasr_priv->handler->switch_ddc(
598 vgasr_priv->old_ddc_owner);
599 }
600 mutex_unlock(&vgasr_priv->mux_hw_lk);
601 return ret;
602 }
603 EXPORT_SYMBOL(vga_switcheroo_unlock_ddc);
604
605 /**
606 * DOC: Manual switching and manual power control
607 *
608 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
609 * can be read to retrieve the current vga_switcheroo state and commands
610 * can be written to it to change the state. The file appears as soon as
611 * two GPU drivers and one handler have registered with vga_switcheroo.
612 * The following commands are understood:
613 *
614 * * OFF: Power off the device not in use.
615 * * ON: Power on the device not in use.
616 * * IGD: Switch to the integrated graphics device.
617 * Power on the integrated GPU if necessary, power off the discrete GPU.
618 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
619 * have opened device files of the GPUs or the audio client. If the
620 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
621 * and /dev/snd/controlC1 to identify processes blocking the switch.
622 * * DIS: Switch to the discrete graphics device.
623 * * DIGD: Delayed switch to the integrated graphics device.
624 * This will perform the switch once the last user space process has
625 * closed the device files of the GPUs and the audio client.
626 * * DDIS: Delayed switch to the discrete graphics device.
627 * * MIGD: Mux-only switch to the integrated graphics device.
628 * Does not remap console or change the power state of either gpu.
629 * If the integrated GPU is currently off, the screen will turn black.
630 * If it is on, the screen will show whatever happens to be in VRAM.
631 * Either way, the user has to blindly enter the command to switch back.
632 * * MDIS: Mux-only switch to the discrete graphics device.
633 *
634 * For GPUs whose power state is controlled by the driver's runtime pm,
635 * the ON and OFF commands are a no-op (see next section).
636 *
637 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
638 * should not be used.
639 */
640
641 #if 0 /* debugfs 1 */
642 static int
643 vga_switcheroo_show(struct seq_file *m, void *v)
644 {
645 struct vga_switcheroo_client *client;
646 int i = 0;
647
648 mutex_lock(&vgasr_mutex);
649 list_for_each_entry(client, &vgasr_priv.clients, list) {
650 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
651 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
652 "IGD",
653 client_is_vga(client) ? "" : "-Audio",
654 client->active ? '+' : ' ',
655 client->driver_power_control ? "Dyn" : "",
656 client->pwr_state ? "Pwr" : "Off",
657 pci_name(client->pdev));
658 i++;
659 }
660 mutex_unlock(&vgasr_mutex);
661 return 0;
662 }
663
664 static int
665 vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
666 {
667 return single_open(file, vga_switcheroo_show, NULL);
668 }
669 #endif /* debugfs 1 */
670
671 static int
vga_switchon(struct vga_switcheroo_client * client)672 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
684 static int
vga_switchoff(struct vga_switcheroo_client * client)685 vga_switchoff(struct vga_switcheroo_client *client)
686 {
687 if (client->driver_power_control)
688 return 0;
689 /* call the driver callback to turn off device */
690 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
691 if (vgasr_priv->handler->power_state)
692 vgasr_priv->handler->power_state(client->id, VGA_SWITCHEROO_OFF);
693 client->pwr_state = VGA_SWITCHEROO_OFF;
694 return 0;
695 }
696
697 static void
set_audio_state(enum vga_switcheroo_client_id id,enum vga_switcheroo_state state)698 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 && client->pwr_state != state) {
705 client->ops->set_gpu_state(client->pdev, state);
706 client->pwr_state = state;
707 }
708 }
709
710 /* stage one happens before delay */
711 static int
vga_switchto_stage1(struct vga_switcheroo_client * new_client)712 vga_switchto_stage1(struct vga_switcheroo_client *new_client)
713 {
714 struct vga_switcheroo_client *active;
715
716 active = find_active_client(&vgasr_priv->clients);
717 if (!active)
718 return 0;
719
720 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
721 vga_switchon(new_client);
722
723 //XXX CRITICAL TODO:
724 //vga_set_default_device(new_client->pdev);
725
726 return 0;
727 }
728
729 /* post delay */
730 static int
vga_switchto_stage2(struct vga_switcheroo_client * new_client)731 vga_switchto_stage2(struct vga_switcheroo_client *new_client)
732 {
733 int ret;
734 struct vga_switcheroo_client *active;
735
736 active = find_active_client(&vgasr_priv->clients);
737 if (!active)
738 return 0;
739
740 active->active = false;
741
742 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
743
744 /* XXX CRITICAL TODO: set new fb
745
746 if (new_client->fb_info) {
747 struct fb_event event;
748
749 console_lock();
750 event.info = new_client->fb_info;
751 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
752 console_unlock();
753 }
754 */
755
756 mutex_lock(&vgasr_priv->mux_hw_lk);
757 ret = vgasr_priv->handler->switchto(new_client->id);
758 mutex_unlock(&vgasr_priv->mux_hw_lk);
759 if (ret)
760 return ret;
761
762 if (new_client->ops->reprobe)
763 new_client->ops->reprobe(new_client->pdev);
764
765 if (active->pwr_state == VGA_SWITCHEROO_ON)
766 vga_switchoff(active);
767
768 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
769
770 new_client->active = true;
771 return 0;
772 }
773
774 static bool
check_can_switch(void)775 check_can_switch(void)
776 {
777 struct vga_switcheroo_client *client;
778
779 list_for_each_entry(client, &vgasr_priv->clients, list) {
780 if (!client->ops->can_switch(client->pdev)) {
781 pr_err("client %x refused switch\n", client->id);
782 return false;
783 }
784 }
785 return true;
786 }
787
788 #if 0 /* debugfs 2 */
789 static ssize_t
790 vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
791 size_t cnt, loff_t *ppos)
792 {
793 char usercmd[64];
794 int ret;
795 bool delay = false, can_switch;
796 bool just_mux = false;
797 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
798 struct vga_switcheroo_client *client = NULL;
799
800 if (cnt > 63)
801 cnt = 63;
802
803 if (copy_from_user(usercmd, ubuf, cnt))
804 return -EFAULT;
805
806 mutex_lock(&vgasr_mutex);
807
808 if (!vgasr_priv.active) {
809 cnt = -EINVAL;
810 goto out;
811 }
812
813 /* pwr off the device not in use */
814 if (strncmp(usercmd, "OFF", 3) == 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 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
821 if (client->pwr_state == VGA_SWITCHEROO_ON)
822 vga_switchoff(client);
823 }
824 goto out;
825 }
826 /* pwr on the device not in use */
827 if (strncmp(usercmd, "ON", 2) == 0) {
828 list_for_each_entry(client, &vgasr_priv.clients, list) {
829 if (client->active || client_is_audio(client))
830 continue;
831 if (client->driver_power_control)
832 continue;
833 if (client->pwr_state == VGA_SWITCHEROO_OFF)
834 vga_switchon(client);
835 set_audio_state(client->id, VGA_SWITCHEROO_ON);
836 }
837 goto out;
838 }
839
840 /* request a delayed switch - test can we switch now */
841 if (strncmp(usercmd, "DIGD", 4) == 0) {
842 client_id = VGA_SWITCHEROO_IGD;
843 delay = true;
844 }
845
846 if (strncmp(usercmd, "DDIS", 4) == 0) {
847 client_id = VGA_SWITCHEROO_DIS;
848 delay = true;
849 }
850
851 if (strncmp(usercmd, "IGD", 3) == 0)
852 client_id = VGA_SWITCHEROO_IGD;
853
854 if (strncmp(usercmd, "DIS", 3) == 0)
855 client_id = VGA_SWITCHEROO_DIS;
856
857 if (strncmp(usercmd, "MIGD", 4) == 0) {
858 just_mux = true;
859 client_id = VGA_SWITCHEROO_IGD;
860 }
861 if (strncmp(usercmd, "MDIS", 4) == 0) {
862 just_mux = true;
863 client_id = VGA_SWITCHEROO_DIS;
864 }
865
866 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
867 goto out;
868 client = find_client_from_id(&vgasr_priv.clients, client_id);
869 if (!client)
870 goto out;
871
872 vgasr_priv.delayed_switch_active = false;
873
874 if (just_mux) {
875 mutex_lock(&vgasr_priv.mux_hw_lock);
876 ret = vgasr_priv.handler->switchto(client_id);
877 mutex_unlock(&vgasr_priv.mux_hw_lock);
878 goto out;
879 }
880
881 if (client->active)
882 goto out;
883
884 /* okay we want a switch - test if devices are willing to switch */
885 can_switch = check_can_switch();
886
887 if (can_switch == false && delay == false)
888 goto out;
889
890 if (can_switch) {
891 ret = vga_switchto_stage1(client);
892 if (ret)
893 pr_err("switching failed stage 1 %d\n", ret);
894
895 ret = vga_switchto_stage2(client);
896 if (ret)
897 pr_err("switching failed stage 2 %d\n", ret);
898
899 } else {
900 pr_info("setting delayed switch to client %d\n", client->id);
901 vgasr_priv.delayed_switch_active = true;
902 vgasr_priv.delayed_client_id = client_id;
903
904 ret = vga_switchto_stage1(client);
905 if (ret)
906 pr_err("delayed switching stage 1 failed %d\n", ret);
907 }
908
909 out:
910 mutex_unlock(&vgasr_mutex);
911 return cnt;
912 }
913
914 #if 0 /* linux specific */
915 static const struct file_operations vga_switcheroo_debugfs_fops = {
916 .owner = THIS_MODULE,
917 .open = vga_switcheroo_debugfs_open,
918 .write = vga_switcheroo_debugfs_write,
919 .read = seq_read,
920 .llseek = seq_lseek,
921 .release = single_release,
922 };
923 #endif
924
925 static void
926 vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
927 {
928 debugfs_remove(priv->switch_file);
929 priv->switch_file = NULL;
930
931 debugfs_remove(priv->debugfs_root);
932 priv->debugfs_root = NULL;
933 }
934
935 static int
936 vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
937 {
938 static const char mp[] = "/sys/kernel/debug";
939
940 /* already initialised */
941 if (priv->debugfs_root)
942 return 0;
943 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
944
945 if (!priv->debugfs_root) {
946 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
947 goto fail;
948 }
949
950 priv->switch_file = debugfs_create_file("switch", 0644,
951 priv->debugfs_root, NULL,
952 &vga_switcheroo_debugfs_fops);
953 if (!priv->switch_file) {
954 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
955 goto fail;
956 }
957 return 0;
958 fail:
959 vga_switcheroo_debugfs_fini(priv);
960 return -1;
961 }
962 #endif /* debugfs 2 */
963
964 /*
965 * Device infrastructure
966 */
967 #define BUFFERSIZE 255
968
969 static d_open_t vga_switcheroo_open;
970 static d_close_t vga_switcheroo_close;
971 static d_read_t vga_switcheroo_read;
972 static d_write_t vga_switcheroo_write;
973
974 static struct dev_ops vga_switcheroo_ops = {
975 { "vga_switcheroo", 0, 0 },
976 .d_open = vga_switcheroo_open,
977 .d_close = vga_switcheroo_close,
978 .d_read = vga_switcheroo_read,
979 .d_write = vga_switcheroo_write,
980 };
981
982 struct s_vga_switcheroo {
983 char msg[BUFFERSIZE + 1];
984 int len;
985 };
986
987 static cdev_t vga_switcheroo_dev;
988 static struct s_vga_switcheroo *vga_switcheroo_buf;
989
990 MALLOC_DECLARE(M_VGA_SWITCHEROO_BUF);
991 MALLOC_DEFINE(M_VGA_SWITCHEROO_BUF, "vga_switcheroo_buf", "buffer for vga_switcheroo");
992 MALLOC_DECLARE(M_VGA_SWITCHEROO_VGASR_PRIV);
993 MALLOC_DEFINE(M_VGA_SWITCHEROO_VGASR_PRIV, "vga_switcheroo_vgasr_priv", "private data of vga_switcheroo");
994
995 static int
vga_switcheroo_handler(struct module * m __unused,int what,void * arg __unused)996 vga_switcheroo_handler(struct module *m __unused, int what, void *arg __unused)
997 {
998 int error = 0;
999
1000 switch (what) {
1001 case MOD_LOAD: /* kldload */
1002 vga_switcheroo_dev = make_dev(
1003 &vga_switcheroo_ops,
1004 0,
1005 UID_ROOT,
1006 GID_WHEEL,
1007 0600,
1008 "vga_switcheroo");
1009 reference_dev(vga_switcheroo_dev);
1010 if (error != 0)
1011 break;
1012
1013 vga_switcheroo_buf = kmalloc(sizeof(*vga_switcheroo_buf), M_VGA_SWITCHEROO_BUF, M_WAITOK | M_ZERO);
1014
1015 //init vgasr_priv
1016 vgasr_priv = kmalloc(sizeof(*vgasr_priv), M_VGA_SWITCHEROO_VGASR_PRIV, M_WAITOK | M_ZERO);
1017 vgasr_priv->clients = (struct list_head) { &(vgasr_priv->clients), &(vgasr_priv->clients) };
1018 lockinit(&vgasr_priv->mux_hw_lk, "mux_hw_lk", 0, LK_CANRECURSE);
1019 kprintf("vga_switcheroo device loaded\n");
1020 break;
1021
1022 case MOD_UNLOAD:
1023 case MOD_SHUTDOWN:
1024 destroy_dev(vga_switcheroo_dev);
1025 kfree(vga_switcheroo_buf, M_VGA_SWITCHEROO_BUF);
1026 kfree(vgasr_priv, M_VGA_SWITCHEROO_VGASR_PRIV);
1027 kprintf("vga_switcheroo device unloaded\n");
1028 break;
1029
1030 default:
1031 error = EOPNOTSUPP;
1032 break;
1033 }
1034
1035 return (error);
1036 }
1037
1038 static int
vga_switcheroo_open(struct dev_open_args * ap)1039 vga_switcheroo_open(struct dev_open_args *ap)
1040 {
1041 int error = 0;
1042
1043 return (error);
1044 }
1045
1046 static int
vga_switcheroo_close(struct dev_close_args * ap)1047 vga_switcheroo_close(struct dev_close_args *ap)
1048 {
1049 return (0);
1050 }
1051
1052 static int
vga_switcheroo_read(struct dev_read_args * ap)1053 vga_switcheroo_read(struct dev_read_args *ap)
1054 {
1055 struct vga_switcheroo_client *client;
1056 int i = 0;
1057
1058 mutex_lock(&vgasr_mutex);
1059 list_for_each_entry(client, &vgasr_priv->clients, list) {
1060 uprintf("%d:%s%s:%c:%s%s:%s\n", i,
1061 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
1062 client_is_vga(client) ? "" : "-Audio",
1063 client->active ? '+' : ' ',
1064 client->driver_power_control ? "Dyn" : "",
1065 client->pwr_state ? "Pwr" : "Off",
1066 device_get_nameunit(client->pdev->dev.bsddev));
1067 i++;
1068 }
1069 mutex_unlock(&vgasr_mutex);
1070 return 0;
1071 }
1072
1073 static int
vga_switcheroo_write(struct dev_write_args * ap)1074 vga_switcheroo_write(struct dev_write_args *ap)
1075 {
1076 struct uio *uio = ap->a_uio;
1077 size_t amt;
1078 int error = 0;
1079 int cnt;
1080 int ret;
1081 bool can_switch;
1082 bool delay = false;
1083 bool just_mux = false;
1084 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
1085 struct vga_switcheroo_client *client = NULL;
1086
1087 /*
1088 * We either write from the beginning or are appending -- do
1089 * not allow random access.
1090 */
1091 if (uio->uio_offset != 0 && (uio->uio_offset != vga_switcheroo_buf->len))
1092 return (EINVAL);
1093
1094 /* This is a new message, reset length */
1095 if (uio->uio_offset == 0)
1096 vga_switcheroo_buf->len = 0;
1097
1098 /* Copy the string in from user memory to kernel memory */
1099 amt = MIN(uio->uio_resid, (BUFFERSIZE - vga_switcheroo_buf->len));
1100 cnt = amt;
1101
1102 error = uiomove(vga_switcheroo_buf->msg + uio->uio_offset, amt, uio);
1103
1104 vga_switcheroo_buf->len = uio->uio_offset;
1105 vga_switcheroo_buf->msg[vga_switcheroo_buf->len] = 0;
1106
1107 if (error != 0)
1108 uprintf("Write failed: bad address!\n");
1109
1110 mutex_lock(&vgasr_mutex);
1111
1112 if (!vgasr_priv->active) {
1113 error = -EINVAL;
1114 goto out;
1115 }
1116
1117 /*
1118 * START PROCESSING COMMANDS
1119 */
1120
1121 /* pwr off the device not in use */
1122 if (strncmp(vga_switcheroo_buf->msg, "OFF", 3) == 0) {
1123 list_for_each_entry(client, &vgasr_priv->clients, list) {
1124 if (client->active || client_is_audio(client))
1125 continue;
1126 if (client->driver_power_control)
1127 continue;
1128 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
1129 if (client->pwr_state == VGA_SWITCHEROO_ON)
1130 vga_switchoff(client);
1131 }
1132 goto out;
1133 }
1134
1135 /* pwr on the device not in use */
1136 if (strncmp(vga_switcheroo_buf->msg, "ON", 2) == 0) {
1137 list_for_each_entry(client, &vgasr_priv->clients, list) {
1138 if (client->active || client_is_audio(client))
1139 continue;
1140 if (client->driver_power_control)
1141 continue;
1142 if (client->pwr_state == VGA_SWITCHEROO_OFF)
1143 vga_switchon(client);
1144 set_audio_state(client->id, VGA_SWITCHEROO_ON);
1145 }
1146 goto out;
1147 }
1148
1149 /* request a delayed switch - test can we switch now */
1150 if (strncmp(vga_switcheroo_buf->msg, "DIGD", 4) == 0) {
1151 client_id = VGA_SWITCHEROO_IGD;
1152 delay = true;
1153 }
1154
1155 if (strncmp(vga_switcheroo_buf->msg, "DDIS", 4) == 0) {
1156 client_id = VGA_SWITCHEROO_DIS;
1157 delay = true;
1158 }
1159
1160 if (strncmp(vga_switcheroo_buf->msg, "IGD", 3) == 0)
1161 client_id = VGA_SWITCHEROO_IGD;
1162
1163 if (strncmp(vga_switcheroo_buf->msg, "DIS", 3) == 0)
1164 client_id = VGA_SWITCHEROO_DIS;
1165
1166 if (strncmp(vga_switcheroo_buf->msg, "MIGD", 4) == 0) {
1167 just_mux = true;
1168 client_id = VGA_SWITCHEROO_IGD;
1169 }
1170
1171 if (strncmp(vga_switcheroo_buf->msg, "MDIS", 4) == 0) {
1172 just_mux = true;
1173 client_id = VGA_SWITCHEROO_DIS;
1174 }
1175
1176 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
1177 goto out;
1178 client = find_client_from_id(&vgasr_priv->clients, client_id);
1179 if (!client)
1180 goto out;
1181
1182 vgasr_priv->delayed_switch_active = false;
1183
1184 if (just_mux) {
1185 mutex_lock(&vgasr_priv->mux_hw_lk);
1186 ret = vgasr_priv->handler->switchto(client_id);
1187 mutex_unlock(&vgasr_priv->mux_hw_lk);
1188 goto out;
1189 }
1190
1191 if (client->active)
1192 goto out;
1193
1194 /* okay we want a switch - test if devices are willing to switch */
1195 can_switch = check_can_switch();
1196
1197 if (can_switch == false && delay == false)
1198 goto out;
1199
1200 if (can_switch) {
1201 ret = vga_switchto_stage1(client);
1202 if (ret)
1203 pr_err("switching failed stage 1 %d\n", ret);
1204
1205 ret = vga_switchto_stage2(client);
1206 if (ret)
1207 pr_err("switching failed stage 2 %d\n", ret);
1208
1209 } else {
1210 pr_info("setting delayed switch to client %d\n", client->id);
1211 vgasr_priv->delayed_switch_active = true;
1212 vgasr_priv->delayed_client_id = client_id;
1213
1214 ret = vga_switchto_stage1(client);
1215 if (ret)
1216 pr_err("delayed switching stage 1 failed %d\n", ret);
1217 }
1218
1219 out:
1220 mutex_unlock(&vgasr_mutex);
1221 return (error);
1222 }
1223
1224 #ifdef __DragonFly__
1225 int
vga_switcheroo_force_migd(void)1226 vga_switcheroo_force_migd(void)
1227 {
1228 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_IGD;
1229 int ret;
1230
1231 mutex_lock(&vgasr_priv->mux_hw_lk);
1232 ret = vgasr_priv->handler->switchto(client_id);
1233 mutex_unlock(&vgasr_priv->mux_hw_lk);
1234 if (ret)
1235 pr_err("failed to switch gmux to IGD\n");
1236
1237 return (ret);
1238 }
1239 #endif
1240
1241 MODULE_VERSION(vga_switcheroo, 1);
1242 DEV_MODULE(vga_switcheroo, vga_switcheroo_handler, NULL);
1243
1244 /**
1245 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
1246 *
1247 * Process a delayed switch if one is pending. DRM drivers should call this
1248 * from their ->lastclose callback.
1249 *
1250 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
1251 * has unregistered in the meantime or if there are other clients blocking the
1252 * switch. If the actual switch fails, an error is reported and 0 is returned.
1253 */
1254 int
vga_switcheroo_process_delayed_switch(void)1255 vga_switcheroo_process_delayed_switch(void)
1256 {
1257 struct vga_switcheroo_client *client;
1258 int ret;
1259 int err = -EINVAL;
1260
1261 mutex_lock(&vgasr_mutex);
1262 if (!vgasr_priv->delayed_switch_active)
1263 goto err;
1264
1265 pr_info("processing delayed switch to %d\n",
1266 vgasr_priv->delayed_client_id);
1267
1268 client = find_client_from_id(&vgasr_priv->clients,
1269 vgasr_priv->delayed_client_id);
1270 if (!client || !check_can_switch())
1271 goto err;
1272
1273 ret = vga_switchto_stage2(client);
1274 if (ret)
1275 pr_err("delayed switching failed stage 2 %d\n", ret);
1276
1277 vgasr_priv->delayed_switch_active = false;
1278 err = 0;
1279 err:
1280 mutex_unlock(&vgasr_mutex);
1281 return err;
1282 }
1283 EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
1284
1285 /**
1286 * DOC: Driver power control
1287 *
1288 * In this mode of use, the discrete GPU automatically powers up and down at
1289 * the discretion of the driver's runtime pm. On muxed machines, the user may
1290 * still influence the muxer state by way of the debugfs interface, however
1291 * the ON and OFF commands become a no-op for the discrete GPU.
1292 *
1293 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
1294 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
1295 * command line disables it.
1296 *
1297 * When the driver decides to power up or down, it notifies vga_switcheroo
1298 * thereof so that it can (a) power the audio device on the GPU up or down,
1299 * and (b) update its internal power state representation for the device.
1300 * This is achieved by vga_switcheroo_set_dynamic_switch().
1301 *
1302 * After the GPU has been suspended, the handler needs to be called to cut
1303 * power to the GPU. Likewise it needs to reinstate power before the GPU
1304 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
1305 * which augments the GPU's suspend/resume functions by the requisite
1306 * calls to the handler.
1307 *
1308 * When the audio device resumes, the GPU needs to be woken. This is achieved
1309 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
1310 * audio device's resume function.
1311 *
1312 * On muxed machines, if the mux is initially switched to the discrete GPU,
1313 * the user ends up with a black screen when the GPU powers down after boot.
1314 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
1315 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
1316 */
1317
1318 #if 0 /* used in: runtime_suspend/resume() */
1319 static void
1320 vga_switcheroo_power_switch(struct pci_dev *pdev,
1321 enum vga_switcheroo_state state)
1322 {
1323 struct vga_switcheroo_client *client;
1324
1325 if (!vgasr_priv->handler->power_state)
1326 return;
1327
1328 client = find_client_from_pci(&vgasr_priv->clients, pdev);
1329 if (!client)
1330 return;
1331
1332 if (!client->driver_power_control)
1333 return;
1334
1335 vgasr_priv->handler->power_state(client->id, state);
1336 }
1337 #endif /* used in: runtime_suspend/resume() */
1338
1339 /**
1340 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
1341 * @pdev: client pci device
1342 * @dynamic: new power state
1343 *
1344 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1345 * When the driver decides to power up or down, it notifies vga_switcheroo
1346 * thereof using this helper so that it can (a) power the audio device on
1347 * the GPU up or down, and (b) update its internal power state representation
1348 * for the device.
1349 */
1350 #if 0 /* TODO: no dynamic_switch yet */
1351 void
1352 vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
1353 enum vga_switcheroo_state dynamic)
1354 {
1355 struct vga_switcheroo_client *client;
1356
1357 mutex_lock(&vgasr_mutex);
1358 client = find_client_from_pci(&vgasr_priv.clients, pdev);
1359 if (!client || !client->driver_power_control) {
1360 mutex_unlock(&vgasr_mutex);
1361 return;
1362 }
1363
1364 client->pwr_state = dynamic;
1365 set_audio_state(client->id, dynamic);
1366 mutex_unlock(&vgasr_mutex);
1367 }
1368 EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
1369
1370 /* switcheroo power domain */
1371 static int
1372 vga_switcheroo_runtime_suspend(struct device *dev)
1373 {
1374 struct pci_dev *pdev = to_pci_dev(dev);
1375 int ret;
1376
1377 ret = dev->bus->pm->runtime_suspend(dev);
1378 if (ret)
1379 return ret;
1380 mutex_lock(&vgasr_mutex);
1381 if (vgasr_priv.handler->switchto) {
1382 mutex_lock(&vgasr_priv.mux_hw_lock);
1383 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
1384 mutex_unlock(&vgasr_priv.mux_hw_lock);
1385 }
1386 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
1387 mutex_unlock(&vgasr_mutex);
1388 return 0;
1389 }
1390
1391 static int
1392 vga_switcheroo_runtime_resume(struct device *dev)
1393 {
1394 struct pci_dev *pdev = to_pci_dev(dev);
1395 int ret;
1396
1397 mutex_lock(&vgasr_mutex);
1398 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
1399 mutex_unlock(&vgasr_mutex);
1400 ret = dev->bus->pm->runtime_resume(dev);
1401 if (ret)
1402 return ret;
1403
1404 return 0;
1405 }
1406 #endif /* TODO: no dynamic_switch yet */
1407
1408 /**
1409 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
1410 * @dev: vga client device
1411 * @domain: power domain
1412 *
1413 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1414 * After the GPU has been suspended, the handler needs to be called to cut
1415 * power to the GPU. Likewise it needs to reinstate power before the GPU
1416 * can resume. To this end, this helper augments the suspend/resume functions
1417 * by the requisite calls to the handler. It needs only be called on platforms
1418 * where the power switch is separate to the device being powered down.
1419 */
1420 #if 0 /* TODO: no pm_ops */
1421 int
1422 vga_switcheroo_init_domain_pm_ops(struct device *dev,
1423 struct dev_pm_domain *domain)
1424 {
1425 /* copy over all the bus versions */
1426 if (dev->bus && dev->bus->pm) {
1427 domain->ops = *dev->bus->pm;
1428 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
1429 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
1430
1431 dev_pm_domain_set(dev, domain);
1432 return 0;
1433 }
1434 dev_pm_domain_set(dev, NULL);
1435 return -EINVAL;
1436 }
1437 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
1438
1439 void
1440 vga_switcheroo_fini_domain_pm_ops(struct device *dev)
1441 {
1442 dev_pm_domain_set(dev, NULL);
1443 }
1444 EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
1445
1446 static int
1447 vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
1448 {
1449 struct pci_dev *pdev = to_pci_dev(dev);
1450 struct vga_switcheroo_client *client;
1451 struct device *video_dev = NULL;
1452 int ret;
1453
1454 /* we need to check if we have to switch back on the video
1455 device so the audio device can come back */
1456 mutex_lock(&vgasr_mutex);
1457 list_for_each_entry(client, &vgasr_priv.clients, list) {
1458 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
1459 client_is_vga(client)) {
1460 video_dev = &client->pdev->dev;
1461 break;
1462 }
1463 }
1464 mutex_unlock(&vgasr_mutex);
1465
1466 if (video_dev) {
1467 ret = pm_runtime_get_sync(video_dev);
1468 if (ret && ret != 1)
1469 return ret;
1470 }
1471 ret = dev->bus->pm->runtime_resume(dev);
1472
1473 /* put the reference for the gpu */
1474 if (video_dev) {
1475 pm_runtime_mark_last_busy(video_dev);
1476 pm_runtime_put_autosuspend(video_dev);
1477 }
1478 return ret;
1479 }
1480 #endif /* TODO: no pm_ops */
1481
1482 /**
1483 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
1484 * power control
1485 * @dev: audio client device
1486 * @domain: power domain
1487 *
1488 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
1489 * When the audio device resumes, the GPU needs to be woken. This helper
1490 * augments the audio device's resume function to do that.
1491 *
1492 * Return: 0 on success, -EINVAL if no power management operations are
1493 * defined for this device.
1494 */
1495 #if 0 /* TODO: no pm */
1496 int
1497 vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
1498 struct dev_pm_domain *domain)
1499 {
1500 /* copy over all the bus versions */
1501 if (dev->bus && dev->bus->pm) {
1502 domain->ops = *dev->bus->pm;
1503 domain->ops.runtime_resume =
1504 vga_switcheroo_runtime_resume_hdmi_audio;
1505
1506 dev_pm_domain_set(dev, domain);
1507 return 0;
1508 }
1509 dev_pm_domain_set(dev, NULL);
1510 return -EINVAL;
1511 }
1512 EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);
1513 #endif /* TODO: no pm */
1514