vgaarb: use kzalloc in vga_arbiter_add_pci_device()
[deliverable/linux.git] / drivers / gpu / vga / vga_switcheroo.c
CommitLineData
6a9ee8af 1/*
a645654b
LW
2 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
3 *
6a9ee8af
DA
4 * Copyright (c) 2010 Red Hat Inc.
5 * Author : Dave Airlie <airlied@redhat.com>
6 *
a645654b 7 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
6a9ee8af 8 *
a645654b
LW
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:
6a9ee8af 15 *
a645654b
LW
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.
71309278 19 *
a645654b
LW
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.
71309278 28 *
6a9ee8af
DA
29 */
30
9b0be1eb
TR
31#define pr_fmt(fmt) "vga_switcheroo: " fmt
32
4127838c 33#include <linux/console.h>
6a9ee8af
DA
34#include <linux/debugfs.h>
35#include <linux/fb.h>
4127838c
LW
36#include <linux/fs.h>
37#include <linux/module.h>
6a9ee8af 38#include <linux/pci.h>
0d69704a 39#include <linux/pm_runtime.h>
4127838c
LW
40#include <linux/seq_file.h>
41#include <linux/uaccess.h>
2fbe8c7c 42#include <linux/vgaarb.h>
4127838c 43#include <linux/vga_switcheroo.h>
2fbe8c7c 44
a645654b
LW
45/**
46 * DOC: Overview
47 *
48 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
49 * These come in two flavors:
50 *
51 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
52 * * muxless: Dual GPUs but only one of them is connected to outputs.
53 * The other one is merely used to offload rendering, its results
54 * are copied over PCIe into the framebuffer. On Linux this is
55 * supported with DRI PRIME.
56 *
57 * Hybrid graphics started to appear in the late Naughties and were initially
58 * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
59 * A notable exception is the MacBook Pro which continues to use a mux.
60 * Muxes come with varying capabilities: Some switch only the panel, others
61 * can also switch external displays. Some switch all display pins at once
62 * while others can switch just the DDC lines. (To allow EDID probing
63 * for the inactive GPU.) Also, muxes are often used to cut power to the
64 * discrete GPU while it is not used.
65 *
66 * DRM drivers register GPUs with vga_switcheroo, these are heretoforth called
67 * clients. The mux is called the handler. Muxless machines also register a
68 * handler to control the power state of the discrete GPU, its ->switchto
69 * callback is a no-op for obvious reasons. The discrete GPU is often equipped
70 * with an HDA controller for the HDMI/DP audio signal, this will also
71 * register as a client so that vga_switcheroo can take care of the correct
72 * suspend/resume order when changing the discrete GPU's power state. In total
73 * there can thus be up to three clients: Two vga clients (GPUs) and one audio
74 * client (on the discrete GPU). The code is mostly prepared to support
75 * machines with more than two GPUs should they become available.
76 * The GPU to which the outputs are currently switched is called the
77 * active client in vga_switcheroo parlance. The GPU not in use is the
78 * inactive client.
79 */
80
81/**
82 * struct vga_switcheroo_client - registered client
83 * @pdev: client pci device
84 * @fb_info: framebuffer to which console is remapped on switching
85 * @pwr_state: current power state
86 * @ops: client callbacks
87 * @id: client identifier, see enum vga_switcheroo_client_id.
88 * Determining the id requires the handler, so GPUs are initially
89 * assigned -1 and later given their true id in vga_switcheroo_enable()
90 * @active: whether the outputs are currently switched to this client
91 * @driver_power_control: whether power state is controlled by the driver's
92 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
93 * interface is a no-op so as not to interfere with runtime pm
94 * @list: client list
95 *
96 * Registered client. A client can be either a GPU or an audio device on a GPU.
97 * For audio clients, the @fb_info, @active and @driver_power_control members
98 * are bogus.
99 */
6a9ee8af
DA
100struct vga_switcheroo_client {
101 struct pci_dev *pdev;
102 struct fb_info *fb_info;
103 int pwr_state;
26ec685f 104 const struct vga_switcheroo_client_ops *ops;
6a9ee8af
DA
105 int id;
106 bool active;
0d69704a 107 bool driver_power_control;
79721e0a 108 struct list_head list;
6a9ee8af
DA
109};
110
a645654b
LW
111/*
112 * protects access to struct vgasr_priv
113 */
6a9ee8af
DA
114static DEFINE_MUTEX(vgasr_mutex);
115
a645654b
LW
116/**
117 * struct vgasr_priv - vga_switcheroo private data
118 * @active: whether vga_switcheroo is enabled.
119 * Prerequisite is the registration of two GPUs and a handler
120 * @delayed_switch_active: whether a delayed switch is pending
121 * @delayed_client_id: client to which a delayed switch is pending
122 * @debugfs_root: directory for vga_switcheroo debugfs interface
123 * @switch_file: file for vga_switcheroo debugfs interface
124 * @registered_clients: number of registered GPUs
125 * (counting only vga clients, not audio clients)
126 * @clients: list of registered clients
127 * @handler: registered handler
128 *
129 * vga_switcheroo private data. Currently only one vga_switcheroo instance
130 * per system is supported.
131 */
6a9ee8af 132struct vgasr_priv {
6a9ee8af
DA
133 bool active;
134 bool delayed_switch_active;
135 enum vga_switcheroo_client_id delayed_client_id;
136
137 struct dentry *debugfs_root;
138 struct dentry *switch_file;
139
140 int registered_clients;
79721e0a 141 struct list_head clients;
6a9ee8af
DA
142
143 struct vga_switcheroo_handler *handler;
144};
145
3e9e63db
TI
146#define ID_BIT_AUDIO 0x100
147#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
148#define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
149#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
150
6a9ee8af
DA
151static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
152static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
153
154/* only one switcheroo per system */
79721e0a
TI
155static struct vgasr_priv vgasr_priv = {
156 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
157};
6a9ee8af 158
36704c0c 159static bool vga_switcheroo_ready(void)
6a9ee8af 160{
36704c0c
SF
161 /* we're ready if we get two clients + handler */
162 return !vgasr_priv.active &&
163 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
6a9ee8af 164}
6a9ee8af
DA
165
166static void vga_switcheroo_enable(void)
167{
6a9ee8af 168 int ret;
79721e0a
TI
169 struct vga_switcheroo_client *client;
170
6a9ee8af 171 /* call the handler to init */
e99eac5e
SF
172 if (vgasr_priv.handler->init)
173 vgasr_priv.handler->init();
6a9ee8af 174
79721e0a 175 list_for_each_entry(client, &vgasr_priv.clients, list) {
3e9e63db
TI
176 if (client->id != -1)
177 continue;
79721e0a 178 ret = vgasr_priv.handler->get_client_id(client->pdev);
6a9ee8af
DA
179 if (ret < 0)
180 return;
181
79721e0a 182 client->id = ret;
6a9ee8af
DA
183 }
184 vga_switcheroo_debugfs_init(&vgasr_priv);
185 vgasr_priv.active = true;
186}
187
a645654b
LW
188/**
189 * vga_switcheroo_register_handler() - register handler
190 * @handler: handler callbacks
191 *
192 * Register handler. Enable vga_switcheroo if two vga clients have already
193 * registered.
194 *
195 * Return: 0 on success, -EINVAL if a handler was already registered.
196 */
36704c0c
SF
197int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
198{
199 mutex_lock(&vgasr_mutex);
200 if (vgasr_priv.handler) {
201 mutex_unlock(&vgasr_mutex);
202 return -EINVAL;
203 }
204
205 vgasr_priv.handler = handler;
206 if (vga_switcheroo_ready()) {
9b0be1eb 207 pr_info("enabled\n");
36704c0c
SF
208 vga_switcheroo_enable();
209 }
210 mutex_unlock(&vgasr_mutex);
211 return 0;
212}
213EXPORT_SYMBOL(vga_switcheroo_register_handler);
214
a645654b
LW
215/**
216 * vga_switcheroo_unregister_handler() - unregister handler
217 *
218 * Unregister handler. Disable vga_switcheroo.
219 */
36704c0c
SF
220void vga_switcheroo_unregister_handler(void)
221{
222 mutex_lock(&vgasr_mutex);
223 vgasr_priv.handler = NULL;
224 if (vgasr_priv.active) {
9b0be1eb 225 pr_info("disabled\n");
36704c0c
SF
226 vga_switcheroo_debugfs_fini(&vgasr_priv);
227 vgasr_priv.active = false;
228 }
229 mutex_unlock(&vgasr_mutex);
230}
231EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
232
3e9e63db
TI
233static int register_client(struct pci_dev *pdev,
234 const struct vga_switcheroo_client_ops *ops,
0d69704a 235 int id, bool active, bool driver_power_control)
6a9ee8af 236{
79721e0a 237 struct vga_switcheroo_client *client;
6a9ee8af 238
79721e0a
TI
239 client = kzalloc(sizeof(*client), GFP_KERNEL);
240 if (!client)
241 return -ENOMEM;
242
243 client->pwr_state = VGA_SWITCHEROO_ON;
244 client->pdev = pdev;
26ec685f 245 client->ops = ops;
3e9e63db
TI
246 client->id = id;
247 client->active = active;
0d69704a 248 client->driver_power_control = driver_power_control;
6a9ee8af 249
79721e0a
TI
250 mutex_lock(&vgasr_mutex);
251 list_add_tail(&client->list, &vgasr_priv.clients);
3e9e63db
TI
252 if (client_is_vga(client))
253 vgasr_priv.registered_clients++;
6a9ee8af 254
36704c0c 255 if (vga_switcheroo_ready()) {
9b0be1eb 256 pr_info("enabled\n");
6a9ee8af
DA
257 vga_switcheroo_enable();
258 }
259 mutex_unlock(&vgasr_mutex);
260 return 0;
261}
3e9e63db 262
a645654b
LW
263/**
264 * vga_switcheroo_register_client - register vga client
265 * @pdev: client pci device
266 * @ops: client callbacks
267 * @driver_power_control: whether power state is controlled by the driver's
268 * runtime pm
269 *
270 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
271 * handler have already registered. The power state of the client is assumed
272 * to be ON.
273 *
274 * Return: 0 on success, -ENOMEM on memory allocation error.
275 */
3e9e63db 276int vga_switcheroo_register_client(struct pci_dev *pdev,
0d69704a
DA
277 const struct vga_switcheroo_client_ops *ops,
278 bool driver_power_control)
3e9e63db
TI
279{
280 return register_client(pdev, ops, -1,
7491bfb4
TR
281 pdev == vga_default_device(),
282 driver_power_control);
3e9e63db 283}
6a9ee8af
DA
284EXPORT_SYMBOL(vga_switcheroo_register_client);
285
a645654b
LW
286/**
287 * vga_switcheroo_register_audio_client - register audio client
288 * @pdev: client pci device
289 * @ops: client callbacks
290 * @id: client identifier, see enum vga_switcheroo_client_id
a645654b
LW
291 *
292 * Register audio client (audio device on a GPU). The power state of the
293 * client is assumed to be ON.
294 *
295 * Return: 0 on success, -ENOMEM on memory allocation error.
296 */
3e9e63db
TI
297int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
298 const struct vga_switcheroo_client_ops *ops,
21b45676 299 int id)
3e9e63db 300{
21b45676 301 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
3e9e63db
TI
302}
303EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
304
79721e0a
TI
305static struct vga_switcheroo_client *
306find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
307{
308 struct vga_switcheroo_client *client;
7491bfb4 309
79721e0a
TI
310 list_for_each_entry(client, head, list)
311 if (client->pdev == pdev)
312 return client;
313 return NULL;
314}
315
316static struct vga_switcheroo_client *
317find_client_from_id(struct list_head *head, int client_id)
318{
319 struct vga_switcheroo_client *client;
7491bfb4 320
79721e0a
TI
321 list_for_each_entry(client, head, list)
322 if (client->id == client_id)
323 return client;
324 return NULL;
325}
326
327static struct vga_switcheroo_client *
328find_active_client(struct list_head *head)
329{
330 struct vga_switcheroo_client *client;
7491bfb4 331
79721e0a 332 list_for_each_entry(client, head, list)
21b45676 333 if (client->active)
79721e0a
TI
334 return client;
335 return NULL;
336}
337
a645654b
LW
338/**
339 * vga_switcheroo_get_client_state() - obtain power state of a given client
340 * @pdev: client pci device
341 *
342 * Obtain power state of a given client as seen from vga_switcheroo.
343 * The function is only called from hda_intel.c.
344 *
345 * Return: Power state.
346 */
c8e9cf7b
TI
347int vga_switcheroo_get_client_state(struct pci_dev *pdev)
348{
349 struct vga_switcheroo_client *client;
350
351 client = find_client_from_pci(&vgasr_priv.clients, pdev);
352 if (!client)
353 return VGA_SWITCHEROO_NOT_FOUND;
354 if (!vgasr_priv.active)
355 return VGA_SWITCHEROO_INIT;
356 return client->pwr_state;
357}
358EXPORT_SYMBOL(vga_switcheroo_get_client_state);
359
a645654b
LW
360/**
361 * vga_switcheroo_unregister_client() - unregister client
362 * @pdev: client pci device
363 *
364 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
365 */
6a9ee8af
DA
366void vga_switcheroo_unregister_client(struct pci_dev *pdev)
367{
79721e0a 368 struct vga_switcheroo_client *client;
6a9ee8af
DA
369
370 mutex_lock(&vgasr_mutex);
79721e0a
TI
371 client = find_client_from_pci(&vgasr_priv.clients, pdev);
372 if (client) {
3e9e63db
TI
373 if (client_is_vga(client))
374 vgasr_priv.registered_clients--;
79721e0a
TI
375 list_del(&client->list);
376 kfree(client);
6a9ee8af 377 }
3e9e63db 378 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
9b0be1eb 379 pr_info("disabled\n");
3e9e63db
TI
380 vga_switcheroo_debugfs_fini(&vgasr_priv);
381 vgasr_priv.active = false;
382 }
6a9ee8af
DA
383 mutex_unlock(&vgasr_mutex);
384}
385EXPORT_SYMBOL(vga_switcheroo_unregister_client);
386
a645654b
LW
387/**
388 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
389 * @pdev: client pci device
390 * @info: framebuffer
391 *
392 * Set framebuffer of a given client. The console will be remapped to this
393 * on switching.
394 */
6a9ee8af
DA
395void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
396 struct fb_info *info)
397{
79721e0a 398 struct vga_switcheroo_client *client;
6a9ee8af
DA
399
400 mutex_lock(&vgasr_mutex);
79721e0a
TI
401 client = find_client_from_pci(&vgasr_priv.clients, pdev);
402 if (client)
403 client->fb_info = info;
6a9ee8af
DA
404 mutex_unlock(&vgasr_mutex);
405}
406EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
407
a645654b
LW
408/**
409 * DOC: Manual switching and manual power control
410 *
411 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
412 * can be read to retrieve the current vga_switcheroo state and commands
413 * can be written to it to change the state. The file appears as soon as
414 * two GPU drivers and one handler have registered with vga_switcheroo.
415 * The following commands are understood:
416 *
417 * * OFF: Power off the device not in use.
418 * * ON: Power on the device not in use.
419 * * IGD: Switch to the integrated graphics device.
420 * Power on the integrated GPU if necessary, power off the discrete GPU.
421 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
422 * have opened device files of the GPUs or the audio client. If the
423 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
424 * and /dev/snd/controlC1 to identify processes blocking the switch.
425 * * DIS: Switch to the discrete graphics device.
426 * * DIGD: Delayed switch to the integrated graphics device.
427 * This will perform the switch once the last user space process has
428 * closed the device files of the GPUs and the audio client.
429 * * DDIS: Delayed switch to the discrete graphics device.
430 * * MIGD: Mux-only switch to the integrated graphics device.
431 * Does not remap console or change the power state of either gpu.
432 * If the integrated GPU is currently off, the screen will turn black.
433 * If it is on, the screen will show whatever happens to be in VRAM.
434 * Either way, the user has to blindly enter the command to switch back.
435 * * MDIS: Mux-only switch to the discrete graphics device.
436 *
437 * For GPUs whose power state is controlled by the driver's runtime pm,
438 * the ON and OFF commands are a no-op (see next section).
439 *
440 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
441 * should not be used.
442 */
443
6a9ee8af
DA
444static int vga_switcheroo_show(struct seq_file *m, void *v)
445{
79721e0a
TI
446 struct vga_switcheroo_client *client;
447 int i = 0;
7491bfb4 448
6a9ee8af 449 mutex_lock(&vgasr_mutex);
79721e0a 450 list_for_each_entry(client, &vgasr_priv.clients, list) {
0d69704a 451 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
7491bfb4
TR
452 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
453 "IGD",
3e9e63db 454 client_is_vga(client) ? "" : "-Audio",
79721e0a 455 client->active ? '+' : ' ',
0d69704a 456 client->driver_power_control ? "Dyn" : "",
79721e0a
TI
457 client->pwr_state ? "Pwr" : "Off",
458 pci_name(client->pdev));
459 i++;
6a9ee8af
DA
460 }
461 mutex_unlock(&vgasr_mutex);
462 return 0;
463}
464
465static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
466{
467 return single_open(file, vga_switcheroo_show, NULL);
468}
469
470static int vga_switchon(struct vga_switcheroo_client *client)
471{
0d69704a
DA
472 if (client->driver_power_control)
473 return 0;
5cfb3c3a
DA
474 if (vgasr_priv.handler->power_state)
475 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
6a9ee8af 476 /* call the driver callback to turn on device */
26ec685f 477 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
6a9ee8af
DA
478 client->pwr_state = VGA_SWITCHEROO_ON;
479 return 0;
480}
481
482static int vga_switchoff(struct vga_switcheroo_client *client)
483{
0d69704a
DA
484 if (client->driver_power_control)
485 return 0;
6a9ee8af 486 /* call the driver callback to turn off device */
26ec685f 487 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
5cfb3c3a
DA
488 if (vgasr_priv.handler->power_state)
489 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
6a9ee8af
DA
490 client->pwr_state = VGA_SWITCHEROO_OFF;
491 return 0;
492}
493
3e9e63db
TI
494static void set_audio_state(int id, int state)
495{
496 struct vga_switcheroo_client *client;
497
498 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
499 if (client && client->pwr_state != state) {
500 client->ops->set_gpu_state(client->pdev, state);
501 client->pwr_state = state;
502 }
503}
504
66b37c67
DA
505/* stage one happens before delay */
506static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
6a9ee8af 507{
79721e0a 508 struct vga_switcheroo_client *active;
6a9ee8af 509
79721e0a 510 active = find_active_client(&vgasr_priv.clients);
6a9ee8af
DA
511 if (!active)
512 return 0;
513
6a9ee8af
DA
514 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
515 vga_switchon(new_client);
516
2fbe8c7c 517 vga_set_default_device(new_client->pdev);
66b37c67
DA
518 return 0;
519}
520
521/* post delay */
522static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
523{
524 int ret;
79721e0a 525 struct vga_switcheroo_client *active;
66b37c67 526
79721e0a 527 active = find_active_client(&vgasr_priv.clients);
66b37c67
DA
528 if (!active)
529 return 0;
530
531 active->active = false;
6a9ee8af 532
c91c3fae
TI
533 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
534
6a9ee8af
DA
535 if (new_client->fb_info) {
536 struct fb_event event;
7491bfb4 537
054430e7 538 console_lock();
6a9ee8af
DA
539 event.info = new_client->fb_info;
540 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
054430e7 541 console_unlock();
6a9ee8af
DA
542 }
543
544 ret = vgasr_priv.handler->switchto(new_client->id);
545 if (ret)
546 return ret;
547
26ec685f
TI
548 if (new_client->ops->reprobe)
549 new_client->ops->reprobe(new_client->pdev);
8d608aa6 550
6a9ee8af
DA
551 if (active->pwr_state == VGA_SWITCHEROO_ON)
552 vga_switchoff(active);
553
c91c3fae
TI
554 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
555
6a9ee8af
DA
556 new_client->active = true;
557 return 0;
558}
559
79721e0a
TI
560static bool check_can_switch(void)
561{
562 struct vga_switcheroo_client *client;
563
564 list_for_each_entry(client, &vgasr_priv.clients, list) {
26ec685f 565 if (!client->ops->can_switch(client->pdev)) {
9b0be1eb 566 pr_err("client %x refused switch\n", client->id);
79721e0a
TI
567 return false;
568 }
569 }
570 return true;
571}
572
6a9ee8af
DA
573static ssize_t
574vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
575 size_t cnt, loff_t *ppos)
576{
577 char usercmd[64];
79721e0a 578 int ret;
6a9ee8af 579 bool delay = false, can_switch;
851ab954 580 bool just_mux = false;
6a9ee8af
DA
581 int client_id = -1;
582 struct vga_switcheroo_client *client = NULL;
583
584 if (cnt > 63)
585 cnt = 63;
586
587 if (copy_from_user(usercmd, ubuf, cnt))
588 return -EFAULT;
589
590 mutex_lock(&vgasr_mutex);
591
8c88e50b
JS
592 if (!vgasr_priv.active) {
593 cnt = -EINVAL;
594 goto out;
595 }
6a9ee8af
DA
596
597 /* pwr off the device not in use */
598 if (strncmp(usercmd, "OFF", 3) == 0) {
79721e0a 599 list_for_each_entry(client, &vgasr_priv.clients, list) {
c91c3fae 600 if (client->active || client_is_audio(client))
6a9ee8af 601 continue;
0d69704a
DA
602 if (client->driver_power_control)
603 continue;
c91c3fae 604 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
79721e0a
TI
605 if (client->pwr_state == VGA_SWITCHEROO_ON)
606 vga_switchoff(client);
6a9ee8af
DA
607 }
608 goto out;
609 }
610 /* pwr on the device not in use */
611 if (strncmp(usercmd, "ON", 2) == 0) {
79721e0a 612 list_for_each_entry(client, &vgasr_priv.clients, list) {
c91c3fae 613 if (client->active || client_is_audio(client))
6a9ee8af 614 continue;
0d69704a
DA
615 if (client->driver_power_control)
616 continue;
79721e0a
TI
617 if (client->pwr_state == VGA_SWITCHEROO_OFF)
618 vga_switchon(client);
c91c3fae 619 set_audio_state(client->id, VGA_SWITCHEROO_ON);
6a9ee8af
DA
620 }
621 goto out;
622 }
623
624 /* request a delayed switch - test can we switch now */
625 if (strncmp(usercmd, "DIGD", 4) == 0) {
626 client_id = VGA_SWITCHEROO_IGD;
627 delay = true;
628 }
629
630 if (strncmp(usercmd, "DDIS", 4) == 0) {
631 client_id = VGA_SWITCHEROO_DIS;
632 delay = true;
633 }
634
635 if (strncmp(usercmd, "IGD", 3) == 0)
636 client_id = VGA_SWITCHEROO_IGD;
637
638 if (strncmp(usercmd, "DIS", 3) == 0)
639 client_id = VGA_SWITCHEROO_DIS;
640
fea6f330 641 if (strncmp(usercmd, "MIGD", 4) == 0) {
851ab954
DA
642 just_mux = true;
643 client_id = VGA_SWITCHEROO_IGD;
644 }
fea6f330 645 if (strncmp(usercmd, "MDIS", 4) == 0) {
851ab954
DA
646 just_mux = true;
647 client_id = VGA_SWITCHEROO_DIS;
648 }
649
6a9ee8af
DA
650 if (client_id == -1)
651 goto out;
79721e0a
TI
652 client = find_client_from_id(&vgasr_priv.clients, client_id);
653 if (!client)
654 goto out;
6a9ee8af
DA
655
656 vgasr_priv.delayed_switch_active = false;
851ab954
DA
657
658 if (just_mux) {
659 ret = vgasr_priv.handler->switchto(client_id);
660 goto out;
661 }
662
79721e0a 663 if (client->active)
a67b8887
FM
664 goto out;
665
6a9ee8af 666 /* okay we want a switch - test if devices are willing to switch */
79721e0a 667 can_switch = check_can_switch();
6a9ee8af
DA
668
669 if (can_switch == false && delay == false)
670 goto out;
671
79721e0a 672 if (can_switch) {
66b37c67
DA
673 ret = vga_switchto_stage1(client);
674 if (ret)
9b0be1eb 675 pr_err("switching failed stage 1 %d\n", ret);
66b37c67
DA
676
677 ret = vga_switchto_stage2(client);
6a9ee8af 678 if (ret)
9b0be1eb 679 pr_err("switching failed stage 2 %d\n", ret);
66b37c67 680
6a9ee8af 681 } else {
9b0be1eb 682 pr_info("setting delayed switch to client %d\n", client->id);
6a9ee8af
DA
683 vgasr_priv.delayed_switch_active = true;
684 vgasr_priv.delayed_client_id = client_id;
685
66b37c67
DA
686 ret = vga_switchto_stage1(client);
687 if (ret)
9b0be1eb 688 pr_err("delayed switching stage 1 failed %d\n", ret);
6a9ee8af
DA
689 }
690
691out:
692 mutex_unlock(&vgasr_mutex);
693 return cnt;
694}
695
696static const struct file_operations vga_switcheroo_debugfs_fops = {
697 .owner = THIS_MODULE,
698 .open = vga_switcheroo_debugfs_open,
699 .write = vga_switcheroo_debugfs_write,
700 .read = seq_read,
701 .llseek = seq_lseek,
702 .release = single_release,
703};
704
705static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
706{
798ae0f6
TR
707 debugfs_remove(priv->switch_file);
708 priv->switch_file = NULL;
709
710 debugfs_remove(priv->debugfs_root);
711 priv->debugfs_root = NULL;
6a9ee8af
DA
712}
713
714static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
715{
9b0be1eb
TR
716 static const char mp[] = "/sys/kernel/debug";
717
6a9ee8af
DA
718 /* already initialised */
719 if (priv->debugfs_root)
720 return 0;
721 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
722
723 if (!priv->debugfs_root) {
9b0be1eb 724 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
6a9ee8af
DA
725 goto fail;
726 }
727
728 priv->switch_file = debugfs_create_file("switch", 0644,
7491bfb4
TR
729 priv->debugfs_root, NULL,
730 &vga_switcheroo_debugfs_fops);
6a9ee8af 731 if (!priv->switch_file) {
9b0be1eb 732 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
6a9ee8af
DA
733 goto fail;
734 }
735 return 0;
736fail:
737 vga_switcheroo_debugfs_fini(priv);
738 return -1;
739}
740
a645654b
LW
741/**
742 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
743 *
744 * Process a delayed switch if one is pending. DRM drivers should call this
745 * from their ->lastclose callback.
746 *
747 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
748 * has unregistered in the meantime or if there are other clients blocking the
749 * switch. If the actual switch fails, an error is reported and 0 is returned.
750 */
6a9ee8af
DA
751int vga_switcheroo_process_delayed_switch(void)
752{
79721e0a 753 struct vga_switcheroo_client *client;
6a9ee8af
DA
754 int ret;
755 int err = -EINVAL;
756
757 mutex_lock(&vgasr_mutex);
758 if (!vgasr_priv.delayed_switch_active)
759 goto err;
760
9b0be1eb
TR
761 pr_info("processing delayed switch to %d\n",
762 vgasr_priv.delayed_client_id);
6a9ee8af 763
79721e0a
TI
764 client = find_client_from_id(&vgasr_priv.clients,
765 vgasr_priv.delayed_client_id);
766 if (!client || !check_can_switch())
6a9ee8af
DA
767 goto err;
768
66b37c67 769 ret = vga_switchto_stage2(client);
6a9ee8af 770 if (ret)
9b0be1eb 771 pr_err("delayed switching failed stage 2 %d\n", ret);
6a9ee8af
DA
772
773 vgasr_priv.delayed_switch_active = false;
774 err = 0;
775err:
776 mutex_unlock(&vgasr_mutex);
777 return err;
778}
779EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
0d69704a 780
a645654b
LW
781/**
782 * DOC: Driver power control
783 *
784 * In this mode of use, the discrete GPU automatically powers up and down at
785 * the discretion of the driver's runtime pm. On muxed machines, the user may
786 * still influence the muxer state by way of the debugfs interface, however
787 * the ON and OFF commands become a no-op for the discrete GPU.
788 *
789 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
790 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
791 * command line disables it.
792 *
793 * When the driver decides to power up or down, it notifies vga_switcheroo
794 * thereof so that it can (a) power the audio device on the GPU up or down,
795 * and (b) update its internal power state representation for the device.
796 * This is achieved by vga_switcheroo_set_dynamic_switch().
797 *
798 * After the GPU has been suspended, the handler needs to be called to cut
799 * power to the GPU. Likewise it needs to reinstate power before the GPU
800 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
801 * which augments the GPU's suspend/resume functions by the requisite
802 * calls to the handler.
803 *
804 * When the audio device resumes, the GPU needs to be woken. This is achieved
805 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
806 * audio device's resume function.
807 *
808 * On muxed machines, if the mux is initially switched to the discrete GPU,
809 * the user ends up with a black screen when the GPU powers down after boot.
810 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
811 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
812 */
813
7491bfb4
TR
814static void vga_switcheroo_power_switch(struct pci_dev *pdev,
815 enum vga_switcheroo_state state)
0d69704a
DA
816{
817 struct vga_switcheroo_client *client;
818
819 if (!vgasr_priv.handler->power_state)
820 return;
821
822 client = find_client_from_pci(&vgasr_priv.clients, pdev);
823 if (!client)
824 return;
825
826 if (!client->driver_power_control)
827 return;
828
829 vgasr_priv.handler->power_state(client->id, state);
830}
831
a645654b
LW
832/**
833 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
834 * @pdev: client pci device
835 * @dynamic: new power state
836 *
837 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
838 * When the driver decides to power up or down, it notifies vga_switcheroo
839 * thereof using this helper so that it can (a) power the audio device on
840 * the GPU up or down, and (b) update its internal power state representation
841 * for the device.
842 */
7491bfb4
TR
843void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
844 enum vga_switcheroo_state dynamic)
0d69704a
DA
845{
846 struct vga_switcheroo_client *client;
847
848 client = find_client_from_pci(&vgasr_priv.clients, pdev);
849 if (!client)
850 return;
851
852 if (!client->driver_power_control)
853 return;
854
855 client->pwr_state = dynamic;
856 set_audio_state(client->id, dynamic);
857}
858EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
859
860/* switcheroo power domain */
861static int vga_switcheroo_runtime_suspend(struct device *dev)
862{
863 struct pci_dev *pdev = to_pci_dev(dev);
864 int ret;
865
866 ret = dev->bus->pm->runtime_suspend(dev);
867 if (ret)
868 return ret;
f2bc5616
AD
869 if (vgasr_priv.handler->switchto)
870 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
0d69704a
DA
871 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
872 return 0;
873}
874
875static int vga_switcheroo_runtime_resume(struct device *dev)
876{
877 struct pci_dev *pdev = to_pci_dev(dev);
878 int ret;
879
880 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
881 ret = dev->bus->pm->runtime_resume(dev);
882 if (ret)
883 return ret;
884
885 return 0;
886}
887
a645654b
LW
888/**
889 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
890 * @dev: vga client device
891 * @domain: power domain
892 *
893 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
894 * After the GPU has been suspended, the handler needs to be called to cut
895 * power to the GPU. Likewise it needs to reinstate power before the GPU
896 * can resume. To this end, this helper augments the suspend/resume functions
897 * by the requisite calls to the handler. It needs only be called on platforms
898 * where the power switch is separate to the device being powered down.
899 */
7491bfb4
TR
900int vga_switcheroo_init_domain_pm_ops(struct device *dev,
901 struct dev_pm_domain *domain)
0d69704a
DA
902{
903 /* copy over all the bus versions */
904 if (dev->bus && dev->bus->pm) {
905 domain->ops = *dev->bus->pm;
906 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
907 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
908
909 dev->pm_domain = domain;
910 return 0;
911 }
912 dev->pm_domain = NULL;
913 return -EINVAL;
914}
915EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
916
766a53d0
AD
917void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
918{
919 dev->pm_domain = NULL;
920}
921EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
922
0d69704a
DA
923static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
924{
925 struct pci_dev *pdev = to_pci_dev(dev);
926 int ret;
927 struct vga_switcheroo_client *client, *found = NULL;
928
929 /* we need to check if we have to switch back on the video
930 device so the audio device can come back */
931 list_for_each_entry(client, &vgasr_priv.clients, list) {
7491bfb4
TR
932 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
933 client_is_vga(client)) {
0d69704a
DA
934 found = client;
935 ret = pm_runtime_get_sync(&client->pdev->dev);
936 if (ret) {
937 if (ret != 1)
938 return ret;
939 }
940 break;
941 }
942 }
943 ret = dev->bus->pm->runtime_resume(dev);
944
945 /* put the reference for the gpu */
946 if (found) {
947 pm_runtime_mark_last_busy(&found->pdev->dev);
948 pm_runtime_put_autosuspend(&found->pdev->dev);
949 }
950 return ret;
951}
952
a645654b
LW
953/**
954 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
955 * power control
956 * @dev: audio client device
957 * @domain: power domain
958 *
959 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
960 * When the audio device resumes, the GPU needs to be woken. This helper
961 * augments the audio device's resume function to do that.
962 *
963 * Return: 0 on success, -EINVAL if no power management operations are
964 * defined for this device.
965 */
7491bfb4
TR
966int
967vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
968 struct dev_pm_domain *domain)
0d69704a
DA
969{
970 /* copy over all the bus versions */
971 if (dev->bus && dev->bus->pm) {
972 domain->ops = *dev->bus->pm;
7491bfb4
TR
973 domain->ops.runtime_resume =
974 vga_switcheroo_runtime_resume_hdmi_audio;
0d69704a
DA
975
976 dev->pm_domain = domain;
977 return 0;
978 }
979 dev->pm_domain = NULL;
980 return -EINVAL;
981}
982EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);
This page took 0.367565 seconds and 5 git commands to generate.