blob: 67a57090175def77d3f4488a3a791cd999d679ef [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
Lukas Wunner41278382015-09-05 13:40:23 +020033#include <linux/console.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100034#include <linux/debugfs.h>
35#include <linux/fb.h>
Lukas Wunner41278382015-09-05 13:40:23 +020036#include <linux/fs.h>
37#include <linux/module.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100038#include <linux/pci.h>
Dave Airlie0d697042012-09-10 12:28:36 +100039#include <linux/pm_runtime.h>
Lukas Wunner41278382015-09-05 13:40:23 +020040#include <linux/seq_file.h>
41#include <linux/uaccess.h>
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040042#include <linux/vgaarb.h>
Lukas Wunner41278382015-09-05 13:40:23 +020043#include <linux/vga_switcheroo.h>
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040044
Lukas Wunnera6456542015-08-23 15:18:55 +020045/**
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 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000100struct vga_switcheroo_client {
101 struct pci_dev *pdev;
102 struct fb_info *fb_info;
103 int pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200104 const struct vga_switcheroo_client_ops *ops;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000105 int id;
106 bool active;
Dave Airlie0d697042012-09-10 12:28:36 +1000107 bool driver_power_control;
Takashi Iwai79721e02012-04-26 12:55:59 +0200108 struct list_head list;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000109};
110
Lukas Wunnera6456542015-08-23 15:18:55 +0200111/*
112 * protects access to struct vgasr_priv
113 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000114static DEFINE_MUTEX(vgasr_mutex);
115
Lukas Wunnera6456542015-08-23 15:18:55 +0200116/**
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 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000132struct vgasr_priv {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000133 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;
Takashi Iwai79721e02012-04-26 12:55:59 +0200141 struct list_head clients;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000142
143 struct vga_switcheroo_handler *handler;
144};
145
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200146#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
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000151static 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 */
Takashi Iwai79721e02012-04-26 12:55:59 +0200155static struct vgasr_priv vgasr_priv = {
156 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
157};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000158
Seth Forshee36704c02012-08-17 11:17:03 -0500159static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000160{
Seth Forshee36704c02012-08-17 11:17:03 -0500161 /* we're ready if we get two clients + handler */
162 return !vgasr_priv.active &&
163 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000164}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000165
166static void vga_switcheroo_enable(void)
167{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000168 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200169 struct vga_switcheroo_client *client;
170
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000171 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -0500172 if (vgasr_priv.handler->init)
173 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000174
Takashi Iwai79721e02012-04-26 12:55:59 +0200175 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200176 if (client->id != -1)
177 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200178 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000179 if (ret < 0)
180 return;
181
Takashi Iwai79721e02012-04-26 12:55:59 +0200182 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000183 }
184 vga_switcheroo_debugfs_init(&vgasr_priv);
185 vgasr_priv.active = true;
186}
187
Lukas Wunnera6456542015-08-23 15:18:55 +0200188/**
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 */
Seth Forshee36704c02012-08-17 11:17:03 -0500197int 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()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200207 pr_info("enabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500208 vga_switcheroo_enable();
209 }
210 mutex_unlock(&vgasr_mutex);
211 return 0;
212}
213EXPORT_SYMBOL(vga_switcheroo_register_handler);
214
Lukas Wunnera6456542015-08-23 15:18:55 +0200215/**
216 * vga_switcheroo_unregister_handler() - unregister handler
217 *
218 * Unregister handler. Disable vga_switcheroo.
219 */
Seth Forshee36704c02012-08-17 11:17:03 -0500220void vga_switcheroo_unregister_handler(void)
221{
222 mutex_lock(&vgasr_mutex);
223 vgasr_priv.handler = NULL;
224 if (vgasr_priv.active) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200225 pr_info("disabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500226 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
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200233static int register_client(struct pci_dev *pdev,
234 const struct vga_switcheroo_client_ops *ops,
Dave Airlie0d697042012-09-10 12:28:36 +1000235 int id, bool active, bool driver_power_control)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000236{
Takashi Iwai79721e02012-04-26 12:55:59 +0200237 struct vga_switcheroo_client *client;
238
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;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200245 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200246 client->id = id;
247 client->active = active;
Dave Airlie0d697042012-09-10 12:28:36 +1000248 client->driver_power_control = driver_power_control;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000249
250 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200251 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200252 if (client_is_vga(client))
253 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000254
Seth Forshee36704c02012-08-17 11:17:03 -0500255 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200256 pr_info("enabled\n");
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000257 vga_switcheroo_enable();
258 }
259 mutex_unlock(&vgasr_mutex);
260 return 0;
261}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200262
Lukas Wunnera6456542015-08-23 15:18:55 +0200263/**
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 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200276int vga_switcheroo_register_client(struct pci_dev *pdev,
Dave Airlie0d697042012-09-10 12:28:36 +1000277 const struct vga_switcheroo_client_ops *ops,
278 bool driver_power_control)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200279{
280 return register_client(pdev, ops, -1,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200281 pdev == vga_default_device(),
282 driver_power_control);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200283}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000284EXPORT_SYMBOL(vga_switcheroo_register_client);
285
Lukas Wunnera6456542015-08-23 15:18:55 +0200286/**
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
291 * @active: whether the audio device is fully initialized
292 *
293 * Register audio client (audio device on a GPU). The power state of the
294 * client is assumed to be ON.
295 *
296 * Return: 0 on success, -ENOMEM on memory allocation error.
297 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200298int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
299 const struct vga_switcheroo_client_ops *ops,
300 int id, bool active)
301{
Dave Airlie0d697042012-09-10 12:28:36 +1000302 return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200303}
304EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
305
Takashi Iwai79721e02012-04-26 12:55:59 +0200306static struct vga_switcheroo_client *
307find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
308{
309 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200310
Takashi Iwai79721e02012-04-26 12:55:59 +0200311 list_for_each_entry(client, head, list)
312 if (client->pdev == pdev)
313 return client;
314 return NULL;
315}
316
317static struct vga_switcheroo_client *
318find_client_from_id(struct list_head *head, int client_id)
319{
320 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200321
Takashi Iwai79721e02012-04-26 12:55:59 +0200322 list_for_each_entry(client, head, list)
323 if (client->id == client_id)
324 return client;
325 return NULL;
326}
327
328static struct vga_switcheroo_client *
329find_active_client(struct list_head *head)
330{
331 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200332
Takashi Iwai79721e02012-04-26 12:55:59 +0200333 list_for_each_entry(client, head, list)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200334 if (client->active && client_is_vga(client))
Takashi Iwai79721e02012-04-26 12:55:59 +0200335 return client;
336 return NULL;
337}
338
Lukas Wunnera6456542015-08-23 15:18:55 +0200339/**
340 * vga_switcheroo_get_client_state() - obtain power state of a given client
341 * @pdev: client pci device
342 *
343 * Obtain power state of a given client as seen from vga_switcheroo.
344 * The function is only called from hda_intel.c.
345 *
346 * Return: Power state.
347 */
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200348int vga_switcheroo_get_client_state(struct pci_dev *pdev)
349{
350 struct vga_switcheroo_client *client;
351
352 client = find_client_from_pci(&vgasr_priv.clients, pdev);
353 if (!client)
354 return VGA_SWITCHEROO_NOT_FOUND;
355 if (!vgasr_priv.active)
356 return VGA_SWITCHEROO_INIT;
357 return client->pwr_state;
358}
359EXPORT_SYMBOL(vga_switcheroo_get_client_state);
360
Lukas Wunnera6456542015-08-23 15:18:55 +0200361/**
362 * vga_switcheroo_unregister_client() - unregister client
363 * @pdev: client pci device
364 *
365 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
366 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000367void vga_switcheroo_unregister_client(struct pci_dev *pdev)
368{
Takashi Iwai79721e02012-04-26 12:55:59 +0200369 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000370
371 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200372 client = find_client_from_pci(&vgasr_priv.clients, pdev);
373 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200374 if (client_is_vga(client))
375 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200376 list_del(&client->list);
377 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000378 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200379 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200380 pr_info("disabled\n");
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200381 vga_switcheroo_debugfs_fini(&vgasr_priv);
382 vgasr_priv.active = false;
383 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000384 mutex_unlock(&vgasr_mutex);
385}
386EXPORT_SYMBOL(vga_switcheroo_unregister_client);
387
Lukas Wunnera6456542015-08-23 15:18:55 +0200388/**
389 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
390 * @pdev: client pci device
391 * @info: framebuffer
392 *
393 * Set framebuffer of a given client. The console will be remapped to this
394 * on switching.
395 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000396void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
397 struct fb_info *info)
398{
Takashi Iwai79721e02012-04-26 12:55:59 +0200399 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000400
401 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200402 client = find_client_from_pci(&vgasr_priv.clients, pdev);
403 if (client)
404 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000405 mutex_unlock(&vgasr_mutex);
406}
407EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
408
Lukas Wunnera6456542015-08-23 15:18:55 +0200409/**
410 * DOC: Manual switching and manual power control
411 *
412 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
413 * can be read to retrieve the current vga_switcheroo state and commands
414 * can be written to it to change the state. The file appears as soon as
415 * two GPU drivers and one handler have registered with vga_switcheroo.
416 * The following commands are understood:
417 *
418 * * OFF: Power off the device not in use.
419 * * ON: Power on the device not in use.
420 * * IGD: Switch to the integrated graphics device.
421 * Power on the integrated GPU if necessary, power off the discrete GPU.
422 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
423 * have opened device files of the GPUs or the audio client. If the
424 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
425 * and /dev/snd/controlC1 to identify processes blocking the switch.
426 * * DIS: Switch to the discrete graphics device.
427 * * DIGD: Delayed switch to the integrated graphics device.
428 * This will perform the switch once the last user space process has
429 * closed the device files of the GPUs and the audio client.
430 * * DDIS: Delayed switch to the discrete graphics device.
431 * * MIGD: Mux-only switch to the integrated graphics device.
432 * Does not remap console or change the power state of either gpu.
433 * If the integrated GPU is currently off, the screen will turn black.
434 * If it is on, the screen will show whatever happens to be in VRAM.
435 * Either way, the user has to blindly enter the command to switch back.
436 * * MDIS: Mux-only switch to the discrete graphics device.
437 *
438 * For GPUs whose power state is controlled by the driver's runtime pm,
439 * the ON and OFF commands are a no-op (see next section).
440 *
441 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
442 * should not be used.
443 */
444
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000445static int vga_switcheroo_show(struct seq_file *m, void *v)
446{
Takashi Iwai79721e02012-04-26 12:55:59 +0200447 struct vga_switcheroo_client *client;
448 int i = 0;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200449
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000450 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200451 list_for_each_entry(client, &vgasr_priv.clients, list) {
Dave Airlie0d697042012-09-10 12:28:36 +1000452 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200453 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
454 "IGD",
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200455 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200456 client->active ? '+' : ' ',
Dave Airlie0d697042012-09-10 12:28:36 +1000457 client->driver_power_control ? "Dyn" : "",
Takashi Iwai79721e02012-04-26 12:55:59 +0200458 client->pwr_state ? "Pwr" : "Off",
459 pci_name(client->pdev));
460 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000461 }
462 mutex_unlock(&vgasr_mutex);
463 return 0;
464}
465
466static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
467{
468 return single_open(file, vga_switcheroo_show, NULL);
469}
470
471static int vga_switchon(struct vga_switcheroo_client *client)
472{
Dave Airlie0d697042012-09-10 12:28:36 +1000473 if (client->driver_power_control)
474 return 0;
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000475 if (vgasr_priv.handler->power_state)
476 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000477 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200478 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000479 client->pwr_state = VGA_SWITCHEROO_ON;
480 return 0;
481}
482
483static int vga_switchoff(struct vga_switcheroo_client *client)
484{
Dave Airlie0d697042012-09-10 12:28:36 +1000485 if (client->driver_power_control)
486 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000487 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200488 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000489 if (vgasr_priv.handler->power_state)
490 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000491 client->pwr_state = VGA_SWITCHEROO_OFF;
492 return 0;
493}
494
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200495static void set_audio_state(int id, int state)
496{
497 struct vga_switcheroo_client *client;
498
499 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
500 if (client && client->pwr_state != state) {
501 client->ops->set_gpu_state(client->pdev, state);
502 client->pwr_state = state;
503 }
504}
505
Dave Airlie66b37c62010-12-07 14:24:25 +1000506/* stage one happens before delay */
507static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000508{
Takashi Iwai79721e02012-04-26 12:55:59 +0200509 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000510
Takashi Iwai79721e02012-04-26 12:55:59 +0200511 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000512 if (!active)
513 return 0;
514
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000515 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
516 vga_switchon(new_client);
517
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400518 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000519 return 0;
520}
521
522/* post delay */
523static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
524{
525 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200526 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000527
Takashi Iwai79721e02012-04-26 12:55:59 +0200528 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000529 if (!active)
530 return 0;
531
532 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000533
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200534 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
535
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000536 if (new_client->fb_info) {
537 struct fb_event event;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200538
Dave Airlie054430e2013-01-25 11:38:56 +1000539 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000540 event.info = new_client->fb_info;
541 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie054430e2013-01-25 11:38:56 +1000542 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000543 }
544
545 ret = vgasr_priv.handler->switchto(new_client->id);
546 if (ret)
547 return ret;
548
Takashi Iwai26ec6852012-05-11 07:51:17 +0200549 if (new_client->ops->reprobe)
550 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000551
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000552 if (active->pwr_state == VGA_SWITCHEROO_ON)
553 vga_switchoff(active);
554
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200555 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
556
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000557 new_client->active = true;
558 return 0;
559}
560
Takashi Iwai79721e02012-04-26 12:55:59 +0200561static bool check_can_switch(void)
562{
563 struct vga_switcheroo_client *client;
564
565 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200566 if (!client->ops->can_switch(client->pdev)) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200567 pr_err("client %x refused switch\n", client->id);
Takashi Iwai79721e02012-04-26 12:55:59 +0200568 return false;
569 }
570 }
571 return true;
572}
573
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000574static ssize_t
575vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
576 size_t cnt, loff_t *ppos)
577{
578 char usercmd[64];
Takashi Iwai79721e02012-04-26 12:55:59 +0200579 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000580 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000581 bool just_mux = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000582 int client_id = -1;
583 struct vga_switcheroo_client *client = NULL;
584
585 if (cnt > 63)
586 cnt = 63;
587
588 if (copy_from_user(usercmd, ubuf, cnt))
589 return -EFAULT;
590
591 mutex_lock(&vgasr_mutex);
592
Jiri Slaby8c88e502010-04-27 14:11:03 -0700593 if (!vgasr_priv.active) {
594 cnt = -EINVAL;
595 goto out;
596 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000597
598 /* pwr off the device not in use */
599 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200600 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200601 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000602 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000603 if (client->driver_power_control)
604 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200605 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200606 if (client->pwr_state == VGA_SWITCHEROO_ON)
607 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000608 }
609 goto out;
610 }
611 /* pwr on the device not in use */
612 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200613 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200614 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000615 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000616 if (client->driver_power_control)
617 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200618 if (client->pwr_state == VGA_SWITCHEROO_OFF)
619 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200620 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000621 }
622 goto out;
623 }
624
625 /* request a delayed switch - test can we switch now */
626 if (strncmp(usercmd, "DIGD", 4) == 0) {
627 client_id = VGA_SWITCHEROO_IGD;
628 delay = true;
629 }
630
631 if (strncmp(usercmd, "DDIS", 4) == 0) {
632 client_id = VGA_SWITCHEROO_DIS;
633 delay = true;
634 }
635
636 if (strncmp(usercmd, "IGD", 3) == 0)
637 client_id = VGA_SWITCHEROO_IGD;
638
639 if (strncmp(usercmd, "DIS", 3) == 0)
640 client_id = VGA_SWITCHEROO_DIS;
641
Dan Carpenterfea6f332011-01-07 08:12:27 +0300642 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000643 just_mux = true;
644 client_id = VGA_SWITCHEROO_IGD;
645 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300646 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000647 just_mux = true;
648 client_id = VGA_SWITCHEROO_DIS;
649 }
650
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000651 if (client_id == -1)
652 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200653 client = find_client_from_id(&vgasr_priv.clients, client_id);
654 if (!client)
655 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000656
657 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000658
659 if (just_mux) {
660 ret = vgasr_priv.handler->switchto(client_id);
661 goto out;
662 }
663
Takashi Iwai79721e02012-04-26 12:55:59 +0200664 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200665 goto out;
666
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000667 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200668 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000669
670 if (can_switch == false && delay == false)
671 goto out;
672
Takashi Iwai79721e02012-04-26 12:55:59 +0200673 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000674 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000675 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200676 pr_err("switching failed stage 1 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000677
678 ret = vga_switchto_stage2(client);
679 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200680 pr_err("switching failed stage 2 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000681
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000682 } else {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200683 pr_info("setting delayed switch to client %d\n", client->id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000684 vgasr_priv.delayed_switch_active = true;
685 vgasr_priv.delayed_client_id = client_id;
686
Dave Airlie66b37c62010-12-07 14:24:25 +1000687 ret = vga_switchto_stage1(client);
688 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200689 pr_err("delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000690 }
691
692out:
693 mutex_unlock(&vgasr_mutex);
694 return cnt;
695}
696
697static const struct file_operations vga_switcheroo_debugfs_fops = {
698 .owner = THIS_MODULE,
699 .open = vga_switcheroo_debugfs_open,
700 .write = vga_switcheroo_debugfs_write,
701 .read = seq_read,
702 .llseek = seq_lseek,
703 .release = single_release,
704};
705
706static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
707{
Thierry Reding798ae0f2015-08-12 16:32:12 +0200708 debugfs_remove(priv->switch_file);
709 priv->switch_file = NULL;
710
711 debugfs_remove(priv->debugfs_root);
712 priv->debugfs_root = NULL;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000713}
714
715static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
716{
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200717 static const char mp[] = "/sys/kernel/debug";
718
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000719 /* already initialised */
720 if (priv->debugfs_root)
721 return 0;
722 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
723
724 if (!priv->debugfs_root) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200725 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000726 goto fail;
727 }
728
729 priv->switch_file = debugfs_create_file("switch", 0644,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200730 priv->debugfs_root, NULL,
731 &vga_switcheroo_debugfs_fops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000732 if (!priv->switch_file) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200733 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000734 goto fail;
735 }
736 return 0;
737fail:
738 vga_switcheroo_debugfs_fini(priv);
739 return -1;
740}
741
Lukas Wunnera6456542015-08-23 15:18:55 +0200742/**
743 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
744 *
745 * Process a delayed switch if one is pending. DRM drivers should call this
746 * from their ->lastclose callback.
747 *
748 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
749 * has unregistered in the meantime or if there are other clients blocking the
750 * switch. If the actual switch fails, an error is reported and 0 is returned.
751 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000752int vga_switcheroo_process_delayed_switch(void)
753{
Takashi Iwai79721e02012-04-26 12:55:59 +0200754 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000755 int ret;
756 int err = -EINVAL;
757
758 mutex_lock(&vgasr_mutex);
759 if (!vgasr_priv.delayed_switch_active)
760 goto err;
761
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200762 pr_info("processing delayed switch to %d\n",
763 vgasr_priv.delayed_client_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000764
Takashi Iwai79721e02012-04-26 12:55:59 +0200765 client = find_client_from_id(&vgasr_priv.clients,
766 vgasr_priv.delayed_client_id);
767 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000768 goto err;
769
Dave Airlie66b37c62010-12-07 14:24:25 +1000770 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000771 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200772 pr_err("delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000773
774 vgasr_priv.delayed_switch_active = false;
775 err = 0;
776err:
777 mutex_unlock(&vgasr_mutex);
778 return err;
779}
780EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
Dave Airlie0d697042012-09-10 12:28:36 +1000781
Lukas Wunnera6456542015-08-23 15:18:55 +0200782/**
783 * DOC: Driver power control
784 *
785 * In this mode of use, the discrete GPU automatically powers up and down at
786 * the discretion of the driver's runtime pm. On muxed machines, the user may
787 * still influence the muxer state by way of the debugfs interface, however
788 * the ON and OFF commands become a no-op for the discrete GPU.
789 *
790 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
791 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
792 * command line disables it.
793 *
794 * When the driver decides to power up or down, it notifies vga_switcheroo
795 * thereof so that it can (a) power the audio device on the GPU up or down,
796 * and (b) update its internal power state representation for the device.
797 * This is achieved by vga_switcheroo_set_dynamic_switch().
798 *
799 * After the GPU has been suspended, the handler needs to be called to cut
800 * power to the GPU. Likewise it needs to reinstate power before the GPU
801 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
802 * which augments the GPU's suspend/resume functions by the requisite
803 * calls to the handler.
804 *
805 * When the audio device resumes, the GPU needs to be woken. This is achieved
806 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
807 * audio device's resume function.
808 *
809 * On muxed machines, if the mux is initially switched to the discrete GPU,
810 * the user ends up with a black screen when the GPU powers down after boot.
811 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
812 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
813 */
814
Thierry Reding7491bfb2015-08-12 16:32:11 +0200815static void vga_switcheroo_power_switch(struct pci_dev *pdev,
816 enum vga_switcheroo_state state)
Dave Airlie0d697042012-09-10 12:28:36 +1000817{
818 struct vga_switcheroo_client *client;
819
820 if (!vgasr_priv.handler->power_state)
821 return;
822
823 client = find_client_from_pci(&vgasr_priv.clients, pdev);
824 if (!client)
825 return;
826
827 if (!client->driver_power_control)
828 return;
829
830 vgasr_priv.handler->power_state(client->id, state);
831}
832
Lukas Wunnera6456542015-08-23 15:18:55 +0200833/**
834 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
835 * @pdev: client pci device
836 * @dynamic: new power state
837 *
838 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
839 * When the driver decides to power up or down, it notifies vga_switcheroo
840 * thereof using this helper so that it can (a) power the audio device on
841 * the GPU up or down, and (b) update its internal power state representation
842 * for the device.
843 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200844void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
845 enum vga_switcheroo_state dynamic)
Dave Airlie0d697042012-09-10 12:28:36 +1000846{
847 struct vga_switcheroo_client *client;
848
849 client = find_client_from_pci(&vgasr_priv.clients, pdev);
850 if (!client)
851 return;
852
853 if (!client->driver_power_control)
854 return;
855
856 client->pwr_state = dynamic;
857 set_audio_state(client->id, dynamic);
858}
859EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
860
861/* switcheroo power domain */
862static int vga_switcheroo_runtime_suspend(struct device *dev)
863{
864 struct pci_dev *pdev = to_pci_dev(dev);
865 int ret;
866
867 ret = dev->bus->pm->runtime_suspend(dev);
868 if (ret)
869 return ret;
Alex Deucherf2bc561612014-05-19 14:04:43 -0400870 if (vgasr_priv.handler->switchto)
871 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
Dave Airlie0d697042012-09-10 12:28:36 +1000872 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
873 return 0;
874}
875
876static int vga_switcheroo_runtime_resume(struct device *dev)
877{
878 struct pci_dev *pdev = to_pci_dev(dev);
879 int ret;
880
881 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
882 ret = dev->bus->pm->runtime_resume(dev);
883 if (ret)
884 return ret;
885
886 return 0;
887}
888
Lukas Wunnera6456542015-08-23 15:18:55 +0200889/**
890 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
891 * @dev: vga client device
892 * @domain: power domain
893 *
894 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
895 * After the GPU has been suspended, the handler needs to be called to cut
896 * power to the GPU. Likewise it needs to reinstate power before the GPU
897 * can resume. To this end, this helper augments the suspend/resume functions
898 * by the requisite calls to the handler. It needs only be called on platforms
899 * where the power switch is separate to the device being powered down.
900 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200901int vga_switcheroo_init_domain_pm_ops(struct device *dev,
902 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000903{
904 /* copy over all the bus versions */
905 if (dev->bus && dev->bus->pm) {
906 domain->ops = *dev->bus->pm;
907 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
908 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
909
910 dev->pm_domain = domain;
911 return 0;
912 }
913 dev->pm_domain = NULL;
914 return -EINVAL;
915}
916EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
917
Alex Deucher766a53d2014-09-12 17:51:29 -0400918void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
919{
920 dev->pm_domain = NULL;
921}
922EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
923
Dave Airlie0d697042012-09-10 12:28:36 +1000924static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
925{
926 struct pci_dev *pdev = to_pci_dev(dev);
927 int ret;
928 struct vga_switcheroo_client *client, *found = NULL;
929
930 /* we need to check if we have to switch back on the video
931 device so the audio device can come back */
932 list_for_each_entry(client, &vgasr_priv.clients, list) {
Thierry Reding7491bfb2015-08-12 16:32:11 +0200933 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
934 client_is_vga(client)) {
Dave Airlie0d697042012-09-10 12:28:36 +1000935 found = client;
936 ret = pm_runtime_get_sync(&client->pdev->dev);
937 if (ret) {
938 if (ret != 1)
939 return ret;
940 }
941 break;
942 }
943 }
944 ret = dev->bus->pm->runtime_resume(dev);
945
946 /* put the reference for the gpu */
947 if (found) {
948 pm_runtime_mark_last_busy(&found->pdev->dev);
949 pm_runtime_put_autosuspend(&found->pdev->dev);
950 }
951 return ret;
952}
953
Lukas Wunnera6456542015-08-23 15:18:55 +0200954/**
955 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
956 * power control
957 * @dev: audio client device
958 * @domain: power domain
959 *
960 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
961 * When the audio device resumes, the GPU needs to be woken. This helper
962 * augments the audio device's resume function to do that.
963 *
964 * Return: 0 on success, -EINVAL if no power management operations are
965 * defined for this device.
966 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200967int
968vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
969 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000970{
971 /* copy over all the bus versions */
972 if (dev->bus && dev->bus->pm) {
973 domain->ops = *dev->bus->pm;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200974 domain->ops.runtime_resume =
975 vga_switcheroo_runtime_resume_hdmi_audio;
Dave Airlie0d697042012-09-10 12:28:36 +1000976
977 dev->pm_domain = domain;
978 return 0;
979 }
980 dev->pm_domain = NULL;
981 return -EINVAL;
982}
983EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);