blob: b19a72f7ac7cfa4b36f0422233003e5aa5fe85a9 [file] [log] [blame]
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001/*
Lukas Wunnera6456542015-08-23 15:18:55 +02002 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
3 *
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10004 * Copyright (c) 2010 Red Hat Inc.
5 * Author : Dave Airlie <airlied@redhat.com>
6 *
Lukas Wunnera6456542015-08-23 15:18:55 +02007 * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10008 *
Lukas Wunnera6456542015-08-23 15:18:55 +02009 * 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:
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100015 *
Lukas Wunnera6456542015-08-23 15:18:55 +020016 * 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.
Thierry Reding71309272015-08-12 16:32:09 +020019 *
Lukas Wunnera6456542015-08-23 15:18:55 +020020 * 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.
Thierry Reding71309272015-08-12 16:32:09 +020028 *
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100029 */
30
Thierry Reding9b0be1e2015-08-12 16:32:10 +020031#define pr_fmt(fmt) "vga_switcheroo: " fmt
32
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100033#include <linux/module.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100034#include <linux/seq_file.h>
35#include <linux/uaccess.h>
36#include <linux/fs.h>
37#include <linux/debugfs.h>
38#include <linux/fb.h>
39
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100040#include <linux/pci.h>
Dave Airlie054430e2013-01-25 11:38:56 +100041#include <linux/console.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100042#include <linux/vga_switcheroo.h>
Dave Airlie0d697042012-09-10 12:28:36 +100043#include <linux/pm_runtime.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100044
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040045#include <linux/vgaarb.h>
46
Lukas Wunnera6456542015-08-23 15:18:55 +020047/**
48 * DOC: Overview
49 *
50 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
51 * These come in two flavors:
52 *
53 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
54 * * muxless: Dual GPUs but only one of them is connected to outputs.
55 * The other one is merely used to offload rendering, its results
56 * are copied over PCIe into the framebuffer. On Linux this is
57 * supported with DRI PRIME.
58 *
59 * Hybrid graphics started to appear in the late Naughties and were initially
60 * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
61 * A notable exception is the MacBook Pro which continues to use a mux.
62 * Muxes come with varying capabilities: Some switch only the panel, others
63 * can also switch external displays. Some switch all display pins at once
64 * while others can switch just the DDC lines. (To allow EDID probing
65 * for the inactive GPU.) Also, muxes are often used to cut power to the
66 * discrete GPU while it is not used.
67 *
68 * DRM drivers register GPUs with vga_switcheroo, these are heretoforth called
69 * clients. The mux is called the handler. Muxless machines also register a
70 * handler to control the power state of the discrete GPU, its ->switchto
71 * callback is a no-op for obvious reasons. The discrete GPU is often equipped
72 * with an HDA controller for the HDMI/DP audio signal, this will also
73 * register as a client so that vga_switcheroo can take care of the correct
74 * suspend/resume order when changing the discrete GPU's power state. In total
75 * there can thus be up to three clients: Two vga clients (GPUs) and one audio
76 * client (on the discrete GPU). The code is mostly prepared to support
77 * machines with more than two GPUs should they become available.
78 * The GPU to which the outputs are currently switched is called the
79 * active client in vga_switcheroo parlance. The GPU not in use is the
80 * inactive client.
81 */
82
83/**
84 * struct vga_switcheroo_client - registered client
85 * @pdev: client pci device
86 * @fb_info: framebuffer to which console is remapped on switching
87 * @pwr_state: current power state
88 * @ops: client callbacks
89 * @id: client identifier, see enum vga_switcheroo_client_id.
90 * Determining the id requires the handler, so GPUs are initially
91 * assigned -1 and later given their true id in vga_switcheroo_enable()
92 * @active: whether the outputs are currently switched to this client
93 * @driver_power_control: whether power state is controlled by the driver's
94 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
95 * interface is a no-op so as not to interfere with runtime pm
96 * @list: client list
97 *
98 * Registered client. A client can be either a GPU or an audio device on a GPU.
99 * For audio clients, the @fb_info, @active and @driver_power_control members
100 * are bogus.
101 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000102struct vga_switcheroo_client {
103 struct pci_dev *pdev;
104 struct fb_info *fb_info;
105 int pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200106 const struct vga_switcheroo_client_ops *ops;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000107 int id;
108 bool active;
Dave Airlie0d697042012-09-10 12:28:36 +1000109 bool driver_power_control;
Takashi Iwai79721e02012-04-26 12:55:59 +0200110 struct list_head list;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000111};
112
Lukas Wunnera6456542015-08-23 15:18:55 +0200113/*
114 * protects access to struct vgasr_priv
115 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000116static DEFINE_MUTEX(vgasr_mutex);
117
Lukas Wunnera6456542015-08-23 15:18:55 +0200118/**
119 * struct vgasr_priv - vga_switcheroo private data
120 * @active: whether vga_switcheroo is enabled.
121 * Prerequisite is the registration of two GPUs and a handler
122 * @delayed_switch_active: whether a delayed switch is pending
123 * @delayed_client_id: client to which a delayed switch is pending
124 * @debugfs_root: directory for vga_switcheroo debugfs interface
125 * @switch_file: file for vga_switcheroo debugfs interface
126 * @registered_clients: number of registered GPUs
127 * (counting only vga clients, not audio clients)
128 * @clients: list of registered clients
129 * @handler: registered handler
130 *
131 * vga_switcheroo private data. Currently only one vga_switcheroo instance
132 * per system is supported.
133 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000134struct vgasr_priv {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000135 bool active;
136 bool delayed_switch_active;
137 enum vga_switcheroo_client_id delayed_client_id;
138
139 struct dentry *debugfs_root;
140 struct dentry *switch_file;
141
142 int registered_clients;
Takashi Iwai79721e02012-04-26 12:55:59 +0200143 struct list_head clients;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000144
145 struct vga_switcheroo_handler *handler;
146};
147
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200148#define ID_BIT_AUDIO 0x100
149#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
150#define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
151#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
152
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000153static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
154static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
155
156/* only one switcheroo per system */
Takashi Iwai79721e02012-04-26 12:55:59 +0200157static struct vgasr_priv vgasr_priv = {
158 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
159};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000160
Seth Forshee36704c02012-08-17 11:17:03 -0500161static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000162{
Seth Forshee36704c02012-08-17 11:17:03 -0500163 /* we're ready if we get two clients + handler */
164 return !vgasr_priv.active &&
165 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000166}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000167
168static void vga_switcheroo_enable(void)
169{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000170 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200171 struct vga_switcheroo_client *client;
172
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000173 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -0500174 if (vgasr_priv.handler->init)
175 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000176
Takashi Iwai79721e02012-04-26 12:55:59 +0200177 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200178 if (client->id != -1)
179 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200180 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000181 if (ret < 0)
182 return;
183
Takashi Iwai79721e02012-04-26 12:55:59 +0200184 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000185 }
186 vga_switcheroo_debugfs_init(&vgasr_priv);
187 vgasr_priv.active = true;
188}
189
Lukas Wunnera6456542015-08-23 15:18:55 +0200190/**
191 * vga_switcheroo_register_handler() - register handler
192 * @handler: handler callbacks
193 *
194 * Register handler. Enable vga_switcheroo if two vga clients have already
195 * registered.
196 *
197 * Return: 0 on success, -EINVAL if a handler was already registered.
198 */
Seth Forshee36704c02012-08-17 11:17:03 -0500199int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
200{
201 mutex_lock(&vgasr_mutex);
202 if (vgasr_priv.handler) {
203 mutex_unlock(&vgasr_mutex);
204 return -EINVAL;
205 }
206
207 vgasr_priv.handler = handler;
208 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200209 pr_info("enabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500210 vga_switcheroo_enable();
211 }
212 mutex_unlock(&vgasr_mutex);
213 return 0;
214}
215EXPORT_SYMBOL(vga_switcheroo_register_handler);
216
Lukas Wunnera6456542015-08-23 15:18:55 +0200217/**
218 * vga_switcheroo_unregister_handler() - unregister handler
219 *
220 * Unregister handler. Disable vga_switcheroo.
221 */
Seth Forshee36704c02012-08-17 11:17:03 -0500222void vga_switcheroo_unregister_handler(void)
223{
224 mutex_lock(&vgasr_mutex);
225 vgasr_priv.handler = NULL;
226 if (vgasr_priv.active) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200227 pr_info("disabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500228 vga_switcheroo_debugfs_fini(&vgasr_priv);
229 vgasr_priv.active = false;
230 }
231 mutex_unlock(&vgasr_mutex);
232}
233EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
234
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200235static int register_client(struct pci_dev *pdev,
236 const struct vga_switcheroo_client_ops *ops,
Dave Airlie0d697042012-09-10 12:28:36 +1000237 int id, bool active, bool driver_power_control)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000238{
Takashi Iwai79721e02012-04-26 12:55:59 +0200239 struct vga_switcheroo_client *client;
240
241 client = kzalloc(sizeof(*client), GFP_KERNEL);
242 if (!client)
243 return -ENOMEM;
244
245 client->pwr_state = VGA_SWITCHEROO_ON;
246 client->pdev = pdev;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200247 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200248 client->id = id;
249 client->active = active;
Dave Airlie0d697042012-09-10 12:28:36 +1000250 client->driver_power_control = driver_power_control;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000251
252 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200253 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200254 if (client_is_vga(client))
255 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000256
Seth Forshee36704c02012-08-17 11:17:03 -0500257 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200258 pr_info("enabled\n");
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000259 vga_switcheroo_enable();
260 }
261 mutex_unlock(&vgasr_mutex);
262 return 0;
263}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200264
Lukas Wunnera6456542015-08-23 15:18:55 +0200265/**
266 * vga_switcheroo_register_client - register vga client
267 * @pdev: client pci device
268 * @ops: client callbacks
269 * @driver_power_control: whether power state is controlled by the driver's
270 * runtime pm
271 *
272 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
273 * handler have already registered. The power state of the client is assumed
274 * to be ON.
275 *
276 * Return: 0 on success, -ENOMEM on memory allocation error.
277 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200278int vga_switcheroo_register_client(struct pci_dev *pdev,
Dave Airlie0d697042012-09-10 12:28:36 +1000279 const struct vga_switcheroo_client_ops *ops,
280 bool driver_power_control)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200281{
282 return register_client(pdev, ops, -1,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200283 pdev == vga_default_device(),
284 driver_power_control);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200285}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000286EXPORT_SYMBOL(vga_switcheroo_register_client);
287
Lukas Wunnera6456542015-08-23 15:18:55 +0200288/**
289 * vga_switcheroo_register_audio_client - register audio client
290 * @pdev: client pci device
291 * @ops: client callbacks
292 * @id: client identifier, see enum vga_switcheroo_client_id
293 * @active: whether the audio device is fully initialized
294 *
295 * Register audio client (audio device on a GPU). The power state of the
296 * client is assumed to be ON.
297 *
298 * Return: 0 on success, -ENOMEM on memory allocation error.
299 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200300int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
301 const struct vga_switcheroo_client_ops *ops,
302 int id, bool active)
303{
Dave Airlie0d697042012-09-10 12:28:36 +1000304 return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200305}
306EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
307
Takashi Iwai79721e02012-04-26 12:55:59 +0200308static struct vga_switcheroo_client *
309find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
310{
311 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200312
Takashi Iwai79721e02012-04-26 12:55:59 +0200313 list_for_each_entry(client, head, list)
314 if (client->pdev == pdev)
315 return client;
316 return NULL;
317}
318
319static struct vga_switcheroo_client *
320find_client_from_id(struct list_head *head, int client_id)
321{
322 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200323
Takashi Iwai79721e02012-04-26 12:55:59 +0200324 list_for_each_entry(client, head, list)
325 if (client->id == client_id)
326 return client;
327 return NULL;
328}
329
330static struct vga_switcheroo_client *
331find_active_client(struct list_head *head)
332{
333 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200334
Takashi Iwai79721e02012-04-26 12:55:59 +0200335 list_for_each_entry(client, head, list)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200336 if (client->active && client_is_vga(client))
Takashi Iwai79721e02012-04-26 12:55:59 +0200337 return client;
338 return NULL;
339}
340
Lukas Wunnera6456542015-08-23 15:18:55 +0200341/**
342 * vga_switcheroo_get_client_state() - obtain power state of a given client
343 * @pdev: client pci device
344 *
345 * Obtain power state of a given client as seen from vga_switcheroo.
346 * The function is only called from hda_intel.c.
347 *
348 * Return: Power state.
349 */
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200350int vga_switcheroo_get_client_state(struct pci_dev *pdev)
351{
352 struct vga_switcheroo_client *client;
353
354 client = find_client_from_pci(&vgasr_priv.clients, pdev);
355 if (!client)
356 return VGA_SWITCHEROO_NOT_FOUND;
357 if (!vgasr_priv.active)
358 return VGA_SWITCHEROO_INIT;
359 return client->pwr_state;
360}
361EXPORT_SYMBOL(vga_switcheroo_get_client_state);
362
Lukas Wunnera6456542015-08-23 15:18:55 +0200363/**
364 * vga_switcheroo_unregister_client() - unregister client
365 * @pdev: client pci device
366 *
367 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
368 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000369void vga_switcheroo_unregister_client(struct pci_dev *pdev)
370{
Takashi Iwai79721e02012-04-26 12:55:59 +0200371 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000372
373 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200374 client = find_client_from_pci(&vgasr_priv.clients, pdev);
375 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200376 if (client_is_vga(client))
377 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200378 list_del(&client->list);
379 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000380 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200381 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200382 pr_info("disabled\n");
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200383 vga_switcheroo_debugfs_fini(&vgasr_priv);
384 vgasr_priv.active = false;
385 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000386 mutex_unlock(&vgasr_mutex);
387}
388EXPORT_SYMBOL(vga_switcheroo_unregister_client);
389
Lukas Wunnera6456542015-08-23 15:18:55 +0200390/**
391 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
392 * @pdev: client pci device
393 * @info: framebuffer
394 *
395 * Set framebuffer of a given client. The console will be remapped to this
396 * on switching.
397 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000398void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
399 struct fb_info *info)
400{
Takashi Iwai79721e02012-04-26 12:55:59 +0200401 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000402
403 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200404 client = find_client_from_pci(&vgasr_priv.clients, pdev);
405 if (client)
406 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000407 mutex_unlock(&vgasr_mutex);
408}
409EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
410
Lukas Wunnera6456542015-08-23 15:18:55 +0200411/**
412 * DOC: Manual switching and manual power control
413 *
414 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
415 * can be read to retrieve the current vga_switcheroo state and commands
416 * can be written to it to change the state. The file appears as soon as
417 * two GPU drivers and one handler have registered with vga_switcheroo.
418 * The following commands are understood:
419 *
420 * * OFF: Power off the device not in use.
421 * * ON: Power on the device not in use.
422 * * IGD: Switch to the integrated graphics device.
423 * Power on the integrated GPU if necessary, power off the discrete GPU.
424 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
425 * have opened device files of the GPUs or the audio client. If the
426 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
427 * and /dev/snd/controlC1 to identify processes blocking the switch.
428 * * DIS: Switch to the discrete graphics device.
429 * * DIGD: Delayed switch to the integrated graphics device.
430 * This will perform the switch once the last user space process has
431 * closed the device files of the GPUs and the audio client.
432 * * DDIS: Delayed switch to the discrete graphics device.
433 * * MIGD: Mux-only switch to the integrated graphics device.
434 * Does not remap console or change the power state of either gpu.
435 * If the integrated GPU is currently off, the screen will turn black.
436 * If it is on, the screen will show whatever happens to be in VRAM.
437 * Either way, the user has to blindly enter the command to switch back.
438 * * MDIS: Mux-only switch to the discrete graphics device.
439 *
440 * For GPUs whose power state is controlled by the driver's runtime pm,
441 * the ON and OFF commands are a no-op (see next section).
442 *
443 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
444 * should not be used.
445 */
446
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000447static int vga_switcheroo_show(struct seq_file *m, void *v)
448{
Takashi Iwai79721e02012-04-26 12:55:59 +0200449 struct vga_switcheroo_client *client;
450 int i = 0;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200451
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000452 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200453 list_for_each_entry(client, &vgasr_priv.clients, list) {
Dave Airlie0d697042012-09-10 12:28:36 +1000454 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200455 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
456 "IGD",
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200457 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200458 client->active ? '+' : ' ',
Dave Airlie0d697042012-09-10 12:28:36 +1000459 client->driver_power_control ? "Dyn" : "",
Takashi Iwai79721e02012-04-26 12:55:59 +0200460 client->pwr_state ? "Pwr" : "Off",
461 pci_name(client->pdev));
462 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000463 }
464 mutex_unlock(&vgasr_mutex);
465 return 0;
466}
467
468static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
469{
470 return single_open(file, vga_switcheroo_show, NULL);
471}
472
473static int vga_switchon(struct vga_switcheroo_client *client)
474{
Dave Airlie0d697042012-09-10 12:28:36 +1000475 if (client->driver_power_control)
476 return 0;
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000477 if (vgasr_priv.handler->power_state)
478 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000479 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200480 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000481 client->pwr_state = VGA_SWITCHEROO_ON;
482 return 0;
483}
484
485static int vga_switchoff(struct vga_switcheroo_client *client)
486{
Dave Airlie0d697042012-09-10 12:28:36 +1000487 if (client->driver_power_control)
488 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000489 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200490 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000491 if (vgasr_priv.handler->power_state)
492 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000493 client->pwr_state = VGA_SWITCHEROO_OFF;
494 return 0;
495}
496
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200497static void set_audio_state(int id, int state)
498{
499 struct vga_switcheroo_client *client;
500
501 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
502 if (client && client->pwr_state != state) {
503 client->ops->set_gpu_state(client->pdev, state);
504 client->pwr_state = state;
505 }
506}
507
Dave Airlie66b37c62010-12-07 14:24:25 +1000508/* stage one happens before delay */
509static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000510{
Takashi Iwai79721e02012-04-26 12:55:59 +0200511 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000512
Takashi Iwai79721e02012-04-26 12:55:59 +0200513 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000514 if (!active)
515 return 0;
516
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000517 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
518 vga_switchon(new_client);
519
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400520 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000521 return 0;
522}
523
524/* post delay */
525static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
526{
527 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200528 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000529
Takashi Iwai79721e02012-04-26 12:55:59 +0200530 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000531 if (!active)
532 return 0;
533
534 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000535
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200536 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
537
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000538 if (new_client->fb_info) {
539 struct fb_event event;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200540
Dave Airlie054430e2013-01-25 11:38:56 +1000541 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000542 event.info = new_client->fb_info;
543 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie054430e2013-01-25 11:38:56 +1000544 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000545 }
546
547 ret = vgasr_priv.handler->switchto(new_client->id);
548 if (ret)
549 return ret;
550
Takashi Iwai26ec6852012-05-11 07:51:17 +0200551 if (new_client->ops->reprobe)
552 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000553
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000554 if (active->pwr_state == VGA_SWITCHEROO_ON)
555 vga_switchoff(active);
556
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200557 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
558
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000559 new_client->active = true;
560 return 0;
561}
562
Takashi Iwai79721e02012-04-26 12:55:59 +0200563static bool check_can_switch(void)
564{
565 struct vga_switcheroo_client *client;
566
567 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200568 if (!client->ops->can_switch(client->pdev)) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200569 pr_err("client %x refused switch\n", client->id);
Takashi Iwai79721e02012-04-26 12:55:59 +0200570 return false;
571 }
572 }
573 return true;
574}
575
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000576static ssize_t
577vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
578 size_t cnt, loff_t *ppos)
579{
580 char usercmd[64];
Takashi Iwai79721e02012-04-26 12:55:59 +0200581 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000582 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000583 bool just_mux = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000584 int client_id = -1;
585 struct vga_switcheroo_client *client = NULL;
586
587 if (cnt > 63)
588 cnt = 63;
589
590 if (copy_from_user(usercmd, ubuf, cnt))
591 return -EFAULT;
592
593 mutex_lock(&vgasr_mutex);
594
Jiri Slaby8c88e502010-04-27 14:11:03 -0700595 if (!vgasr_priv.active) {
596 cnt = -EINVAL;
597 goto out;
598 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000599
600 /* pwr off the device not in use */
601 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200602 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200603 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000604 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000605 if (client->driver_power_control)
606 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200607 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200608 if (client->pwr_state == VGA_SWITCHEROO_ON)
609 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000610 }
611 goto out;
612 }
613 /* pwr on the device not in use */
614 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200615 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200616 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000617 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000618 if (client->driver_power_control)
619 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200620 if (client->pwr_state == VGA_SWITCHEROO_OFF)
621 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200622 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000623 }
624 goto out;
625 }
626
627 /* request a delayed switch - test can we switch now */
628 if (strncmp(usercmd, "DIGD", 4) == 0) {
629 client_id = VGA_SWITCHEROO_IGD;
630 delay = true;
631 }
632
633 if (strncmp(usercmd, "DDIS", 4) == 0) {
634 client_id = VGA_SWITCHEROO_DIS;
635 delay = true;
636 }
637
638 if (strncmp(usercmd, "IGD", 3) == 0)
639 client_id = VGA_SWITCHEROO_IGD;
640
641 if (strncmp(usercmd, "DIS", 3) == 0)
642 client_id = VGA_SWITCHEROO_DIS;
643
Dan Carpenterfea6f332011-01-07 08:12:27 +0300644 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000645 just_mux = true;
646 client_id = VGA_SWITCHEROO_IGD;
647 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300648 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000649 just_mux = true;
650 client_id = VGA_SWITCHEROO_DIS;
651 }
652
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000653 if (client_id == -1)
654 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200655 client = find_client_from_id(&vgasr_priv.clients, client_id);
656 if (!client)
657 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000658
659 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000660
661 if (just_mux) {
662 ret = vgasr_priv.handler->switchto(client_id);
663 goto out;
664 }
665
Takashi Iwai79721e02012-04-26 12:55:59 +0200666 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200667 goto out;
668
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000669 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200670 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000671
672 if (can_switch == false && delay == false)
673 goto out;
674
Takashi Iwai79721e02012-04-26 12:55:59 +0200675 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000676 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000677 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200678 pr_err("switching failed stage 1 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000679
680 ret = vga_switchto_stage2(client);
681 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200682 pr_err("switching failed stage 2 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000683
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000684 } else {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200685 pr_info("setting delayed switch to client %d\n", client->id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000686 vgasr_priv.delayed_switch_active = true;
687 vgasr_priv.delayed_client_id = client_id;
688
Dave Airlie66b37c62010-12-07 14:24:25 +1000689 ret = vga_switchto_stage1(client);
690 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200691 pr_err("delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000692 }
693
694out:
695 mutex_unlock(&vgasr_mutex);
696 return cnt;
697}
698
699static const struct file_operations vga_switcheroo_debugfs_fops = {
700 .owner = THIS_MODULE,
701 .open = vga_switcheroo_debugfs_open,
702 .write = vga_switcheroo_debugfs_write,
703 .read = seq_read,
704 .llseek = seq_lseek,
705 .release = single_release,
706};
707
708static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
709{
Thierry Reding798ae0f2015-08-12 16:32:12 +0200710 debugfs_remove(priv->switch_file);
711 priv->switch_file = NULL;
712
713 debugfs_remove(priv->debugfs_root);
714 priv->debugfs_root = NULL;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000715}
716
717static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
718{
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200719 static const char mp[] = "/sys/kernel/debug";
720
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000721 /* already initialised */
722 if (priv->debugfs_root)
723 return 0;
724 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
725
726 if (!priv->debugfs_root) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200727 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000728 goto fail;
729 }
730
731 priv->switch_file = debugfs_create_file("switch", 0644,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200732 priv->debugfs_root, NULL,
733 &vga_switcheroo_debugfs_fops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000734 if (!priv->switch_file) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200735 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000736 goto fail;
737 }
738 return 0;
739fail:
740 vga_switcheroo_debugfs_fini(priv);
741 return -1;
742}
743
Lukas Wunnera6456542015-08-23 15:18:55 +0200744/**
745 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
746 *
747 * Process a delayed switch if one is pending. DRM drivers should call this
748 * from their ->lastclose callback.
749 *
750 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
751 * has unregistered in the meantime or if there are other clients blocking the
752 * switch. If the actual switch fails, an error is reported and 0 is returned.
753 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000754int vga_switcheroo_process_delayed_switch(void)
755{
Takashi Iwai79721e02012-04-26 12:55:59 +0200756 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000757 int ret;
758 int err = -EINVAL;
759
760 mutex_lock(&vgasr_mutex);
761 if (!vgasr_priv.delayed_switch_active)
762 goto err;
763
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200764 pr_info("processing delayed switch to %d\n",
765 vgasr_priv.delayed_client_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000766
Takashi Iwai79721e02012-04-26 12:55:59 +0200767 client = find_client_from_id(&vgasr_priv.clients,
768 vgasr_priv.delayed_client_id);
769 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000770 goto err;
771
Dave Airlie66b37c62010-12-07 14:24:25 +1000772 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000773 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200774 pr_err("delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000775
776 vgasr_priv.delayed_switch_active = false;
777 err = 0;
778err:
779 mutex_unlock(&vgasr_mutex);
780 return err;
781}
782EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
Dave Airlie0d697042012-09-10 12:28:36 +1000783
Lukas Wunnera6456542015-08-23 15:18:55 +0200784/**
785 * DOC: Driver power control
786 *
787 * In this mode of use, the discrete GPU automatically powers up and down at
788 * the discretion of the driver's runtime pm. On muxed machines, the user may
789 * still influence the muxer state by way of the debugfs interface, however
790 * the ON and OFF commands become a no-op for the discrete GPU.
791 *
792 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
793 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
794 * command line disables it.
795 *
796 * When the driver decides to power up or down, it notifies vga_switcheroo
797 * thereof so that it can (a) power the audio device on the GPU up or down,
798 * and (b) update its internal power state representation for the device.
799 * This is achieved by vga_switcheroo_set_dynamic_switch().
800 *
801 * After the GPU has been suspended, the handler needs to be called to cut
802 * power to the GPU. Likewise it needs to reinstate power before the GPU
803 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
804 * which augments the GPU's suspend/resume functions by the requisite
805 * calls to the handler.
806 *
807 * When the audio device resumes, the GPU needs to be woken. This is achieved
808 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
809 * audio device's resume function.
810 *
811 * On muxed machines, if the mux is initially switched to the discrete GPU,
812 * the user ends up with a black screen when the GPU powers down after boot.
813 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
814 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
815 */
816
Thierry Reding7491bfb2015-08-12 16:32:11 +0200817static void vga_switcheroo_power_switch(struct pci_dev *pdev,
818 enum vga_switcheroo_state state)
Dave Airlie0d697042012-09-10 12:28:36 +1000819{
820 struct vga_switcheroo_client *client;
821
822 if (!vgasr_priv.handler->power_state)
823 return;
824
825 client = find_client_from_pci(&vgasr_priv.clients, pdev);
826 if (!client)
827 return;
828
829 if (!client->driver_power_control)
830 return;
831
832 vgasr_priv.handler->power_state(client->id, state);
833}
834
Lukas Wunnera6456542015-08-23 15:18:55 +0200835/**
836 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
837 * @pdev: client pci device
838 * @dynamic: new power state
839 *
840 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
841 * When the driver decides to power up or down, it notifies vga_switcheroo
842 * thereof using this helper so that it can (a) power the audio device on
843 * the GPU up or down, and (b) update its internal power state representation
844 * for the device.
845 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200846void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
847 enum vga_switcheroo_state dynamic)
Dave Airlie0d697042012-09-10 12:28:36 +1000848{
849 struct vga_switcheroo_client *client;
850
851 client = find_client_from_pci(&vgasr_priv.clients, pdev);
852 if (!client)
853 return;
854
855 if (!client->driver_power_control)
856 return;
857
858 client->pwr_state = dynamic;
859 set_audio_state(client->id, dynamic);
860}
861EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
862
863/* switcheroo power domain */
864static int vga_switcheroo_runtime_suspend(struct device *dev)
865{
866 struct pci_dev *pdev = to_pci_dev(dev);
867 int ret;
868
869 ret = dev->bus->pm->runtime_suspend(dev);
870 if (ret)
871 return ret;
Alex Deucherf2bc561612014-05-19 14:04:43 -0400872 if (vgasr_priv.handler->switchto)
873 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
Dave Airlie0d697042012-09-10 12:28:36 +1000874 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
875 return 0;
876}
877
878static int vga_switcheroo_runtime_resume(struct device *dev)
879{
880 struct pci_dev *pdev = to_pci_dev(dev);
881 int ret;
882
883 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
884 ret = dev->bus->pm->runtime_resume(dev);
885 if (ret)
886 return ret;
887
888 return 0;
889}
890
Lukas Wunnera6456542015-08-23 15:18:55 +0200891/**
892 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
893 * @dev: vga client device
894 * @domain: power domain
895 *
896 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
897 * After the GPU has been suspended, the handler needs to be called to cut
898 * power to the GPU. Likewise it needs to reinstate power before the GPU
899 * can resume. To this end, this helper augments the suspend/resume functions
900 * by the requisite calls to the handler. It needs only be called on platforms
901 * where the power switch is separate to the device being powered down.
902 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200903int vga_switcheroo_init_domain_pm_ops(struct device *dev,
904 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000905{
906 /* copy over all the bus versions */
907 if (dev->bus && dev->bus->pm) {
908 domain->ops = *dev->bus->pm;
909 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
910 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
911
912 dev->pm_domain = domain;
913 return 0;
914 }
915 dev->pm_domain = NULL;
916 return -EINVAL;
917}
918EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
919
Alex Deucher766a53d2014-09-12 17:51:29 -0400920void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
921{
922 dev->pm_domain = NULL;
923}
924EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
925
Dave Airlie0d697042012-09-10 12:28:36 +1000926static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
927{
928 struct pci_dev *pdev = to_pci_dev(dev);
929 int ret;
930 struct vga_switcheroo_client *client, *found = NULL;
931
932 /* we need to check if we have to switch back on the video
933 device so the audio device can come back */
934 list_for_each_entry(client, &vgasr_priv.clients, list) {
Thierry Reding7491bfb2015-08-12 16:32:11 +0200935 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
936 client_is_vga(client)) {
Dave Airlie0d697042012-09-10 12:28:36 +1000937 found = client;
938 ret = pm_runtime_get_sync(&client->pdev->dev);
939 if (ret) {
940 if (ret != 1)
941 return ret;
942 }
943 break;
944 }
945 }
946 ret = dev->bus->pm->runtime_resume(dev);
947
948 /* put the reference for the gpu */
949 if (found) {
950 pm_runtime_mark_last_busy(&found->pdev->dev);
951 pm_runtime_put_autosuspend(&found->pdev->dev);
952 }
953 return ret;
954}
955
Lukas Wunnera6456542015-08-23 15:18:55 +0200956/**
957 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
958 * power control
959 * @dev: audio client device
960 * @domain: power domain
961 *
962 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
963 * When the audio device resumes, the GPU needs to be woken. This helper
964 * augments the audio device's resume function to do that.
965 *
966 * Return: 0 on success, -EINVAL if no power management operations are
967 * defined for this device.
968 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200969int
970vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
971 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000972{
973 /* copy over all the bus versions */
974 if (dev->bus && dev->bus->pm) {
975 domain->ops = *dev->bus->pm;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200976 domain->ops.runtime_resume =
977 vga_switcheroo_runtime_resume_hdmi_audio;
Dave Airlie0d697042012-09-10 12:28:36 +1000978
979 dev->pm_domain = domain;
980 return 0;
981 }
982 dev->pm_domain = NULL;
983 return -EINVAL;
984}
985EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);