blob: d64d9058bce57ef6b424c994c02ff7f6cb36baa5 [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 *
Lukas Wunner3a848662016-01-02 18:47:17 +010066 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
Lukas Wunnera6456542015-08-23 15:18:55 +020067 * 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
Lukas Wunner21c5ba82015-08-28 13:30:32 +020087 * @id: client identifier. Determining the id requires the handler,
88 * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
89 * and later given their true id in vga_switcheroo_enable()
Lukas Wunnera6456542015-08-23 15:18:55 +020090 * @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;
Lukas Wunner203d0272015-08-28 11:56:26 +0200103 enum vga_switcheroo_state pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200104 const struct vga_switcheroo_client_ops *ops;
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200105 enum vga_switcheroo_client_id id;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000106 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
Lukas Wunner5d170132015-10-18 13:05:40 +0200143 const struct vga_switcheroo_handler *handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000144};
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)
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200148#define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
149 !client_is_audio(c))
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200150#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
151
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000152static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
153static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
154
155/* only one switcheroo per system */
Takashi Iwai79721e02012-04-26 12:55:59 +0200156static struct vgasr_priv vgasr_priv = {
157 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
158};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000159
Seth Forshee36704c02012-08-17 11:17:03 -0500160static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000161{
Seth Forshee36704c02012-08-17 11:17:03 -0500162 /* we're ready if we get two clients + handler */
163 return !vgasr_priv.active &&
164 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000165}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000166
167static void vga_switcheroo_enable(void)
168{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000169 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200170 struct vga_switcheroo_client *client;
171
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000172 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -0500173 if (vgasr_priv.handler->init)
174 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000175
Takashi Iwai79721e02012-04-26 12:55:59 +0200176 list_for_each_entry(client, &vgasr_priv.clients, list) {
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200177 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200178 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200179 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000180 if (ret < 0)
181 return;
182
Takashi Iwai79721e02012-04-26 12:55:59 +0200183 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000184 }
185 vga_switcheroo_debugfs_init(&vgasr_priv);
186 vgasr_priv.active = true;
187}
188
Lukas Wunnera6456542015-08-23 15:18:55 +0200189/**
190 * vga_switcheroo_register_handler() - register handler
191 * @handler: handler callbacks
192 *
193 * Register handler. Enable vga_switcheroo if two vga clients have already
194 * registered.
195 *
196 * Return: 0 on success, -EINVAL if a handler was already registered.
197 */
Lukas Wunner5d170132015-10-18 13:05:40 +0200198int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler)
Seth Forshee36704c02012-08-17 11:17:03 -0500199{
200 mutex_lock(&vgasr_mutex);
201 if (vgasr_priv.handler) {
202 mutex_unlock(&vgasr_mutex);
203 return -EINVAL;
204 }
205
206 vgasr_priv.handler = handler;
207 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200208 pr_info("enabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500209 vga_switcheroo_enable();
210 }
211 mutex_unlock(&vgasr_mutex);
212 return 0;
213}
214EXPORT_SYMBOL(vga_switcheroo_register_handler);
215
Lukas Wunnera6456542015-08-23 15:18:55 +0200216/**
217 * vga_switcheroo_unregister_handler() - unregister handler
218 *
219 * Unregister handler. Disable vga_switcheroo.
220 */
Seth Forshee36704c02012-08-17 11:17:03 -0500221void vga_switcheroo_unregister_handler(void)
222{
223 mutex_lock(&vgasr_mutex);
224 vgasr_priv.handler = NULL;
225 if (vgasr_priv.active) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200226 pr_info("disabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500227 vga_switcheroo_debugfs_fini(&vgasr_priv);
228 vgasr_priv.active = false;
229 }
230 mutex_unlock(&vgasr_mutex);
231}
232EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
233
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200234static int register_client(struct pci_dev *pdev,
235 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200236 enum vga_switcheroo_client_id id, bool active,
237 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{
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200282 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
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
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200292 * @id: client identifier
Lukas Wunnera6456542015-08-23 15:18:55 +0200293 *
294 * Register audio client (audio device on a GPU). The power state of the
295 * client is assumed to be ON.
296 *
297 * Return: 0 on success, -ENOMEM on memory allocation error.
298 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200299int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
300 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200301 enum vga_switcheroo_client_id id)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200302{
Lukas Wunner21b45672015-08-27 16:43:43 +0200303 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200304}
305EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
306
Takashi Iwai79721e02012-04-26 12:55:59 +0200307static struct vga_switcheroo_client *
308find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
309{
310 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200311
Takashi Iwai79721e02012-04-26 12:55:59 +0200312 list_for_each_entry(client, head, list)
313 if (client->pdev == pdev)
314 return client;
315 return NULL;
316}
317
318static struct vga_switcheroo_client *
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200319find_client_from_id(struct list_head *head,
320 enum vga_switcheroo_client_id client_id)
Takashi Iwai79721e02012-04-26 12:55:59 +0200321{
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)
Lukas Wunner21b45672015-08-27 16:43:43 +0200336 if (client->active)
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 */
Lukas Wunner203d0272015-08-28 11:56:26 +0200350enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200351{
352 struct vga_switcheroo_client *client;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200353 enum vga_switcheroo_state ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200354
Lukas Wunner8f12a312015-08-23 23:23:02 +0200355 mutex_lock(&vgasr_mutex);
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200356 client = find_client_from_pci(&vgasr_priv.clients, pdev);
357 if (!client)
Lukas Wunner8f12a312015-08-23 23:23:02 +0200358 ret = VGA_SWITCHEROO_NOT_FOUND;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200359 else
360 ret = client->pwr_state;
361 mutex_unlock(&vgasr_mutex);
362 return ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200363}
364EXPORT_SYMBOL(vga_switcheroo_get_client_state);
365
Lukas Wunnera6456542015-08-23 15:18:55 +0200366/**
367 * vga_switcheroo_unregister_client() - unregister client
368 * @pdev: client pci device
369 *
370 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
371 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000372void vga_switcheroo_unregister_client(struct pci_dev *pdev)
373{
Takashi Iwai79721e02012-04-26 12:55:59 +0200374 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000375
376 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200377 client = find_client_from_pci(&vgasr_priv.clients, pdev);
378 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200379 if (client_is_vga(client))
380 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200381 list_del(&client->list);
382 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000383 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200384 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200385 pr_info("disabled\n");
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200386 vga_switcheroo_debugfs_fini(&vgasr_priv);
387 vgasr_priv.active = false;
388 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000389 mutex_unlock(&vgasr_mutex);
390}
391EXPORT_SYMBOL(vga_switcheroo_unregister_client);
392
Lukas Wunnera6456542015-08-23 15:18:55 +0200393/**
394 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
395 * @pdev: client pci device
396 * @info: framebuffer
397 *
398 * Set framebuffer of a given client. The console will be remapped to this
399 * on switching.
400 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000401void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
402 struct fb_info *info)
403{
Takashi Iwai79721e02012-04-26 12:55:59 +0200404 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000405
406 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200407 client = find_client_from_pci(&vgasr_priv.clients, pdev);
408 if (client)
409 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000410 mutex_unlock(&vgasr_mutex);
411}
412EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
413
Lukas Wunnera6456542015-08-23 15:18:55 +0200414/**
415 * DOC: Manual switching and manual power control
416 *
417 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
418 * can be read to retrieve the current vga_switcheroo state and commands
419 * can be written to it to change the state. The file appears as soon as
420 * two GPU drivers and one handler have registered with vga_switcheroo.
421 * The following commands are understood:
422 *
423 * * OFF: Power off the device not in use.
424 * * ON: Power on the device not in use.
425 * * IGD: Switch to the integrated graphics device.
426 * Power on the integrated GPU if necessary, power off the discrete GPU.
427 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
428 * have opened device files of the GPUs or the audio client. If the
429 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
430 * and /dev/snd/controlC1 to identify processes blocking the switch.
431 * * DIS: Switch to the discrete graphics device.
432 * * DIGD: Delayed switch to the integrated graphics device.
433 * This will perform the switch once the last user space process has
434 * closed the device files of the GPUs and the audio client.
435 * * DDIS: Delayed switch to the discrete graphics device.
436 * * MIGD: Mux-only switch to the integrated graphics device.
437 * Does not remap console or change the power state of either gpu.
438 * If the integrated GPU is currently off, the screen will turn black.
439 * If it is on, the screen will show whatever happens to be in VRAM.
440 * Either way, the user has to blindly enter the command to switch back.
441 * * MDIS: Mux-only switch to the discrete graphics device.
442 *
443 * For GPUs whose power state is controlled by the driver's runtime pm,
444 * the ON and OFF commands are a no-op (see next section).
445 *
446 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
447 * should not be used.
448 */
449
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000450static int vga_switcheroo_show(struct seq_file *m, void *v)
451{
Takashi Iwai79721e02012-04-26 12:55:59 +0200452 struct vga_switcheroo_client *client;
453 int i = 0;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200454
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000455 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200456 list_for_each_entry(client, &vgasr_priv.clients, list) {
Dave Airlie0d697042012-09-10 12:28:36 +1000457 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200458 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
459 "IGD",
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200460 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200461 client->active ? '+' : ' ',
Dave Airlie0d697042012-09-10 12:28:36 +1000462 client->driver_power_control ? "Dyn" : "",
Takashi Iwai79721e02012-04-26 12:55:59 +0200463 client->pwr_state ? "Pwr" : "Off",
464 pci_name(client->pdev));
465 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000466 }
467 mutex_unlock(&vgasr_mutex);
468 return 0;
469}
470
471static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
472{
473 return single_open(file, vga_switcheroo_show, NULL);
474}
475
476static int vga_switchon(struct vga_switcheroo_client *client)
477{
Dave Airlie0d697042012-09-10 12:28:36 +1000478 if (client->driver_power_control)
479 return 0;
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000480 if (vgasr_priv.handler->power_state)
481 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000482 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200483 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000484 client->pwr_state = VGA_SWITCHEROO_ON;
485 return 0;
486}
487
488static int vga_switchoff(struct vga_switcheroo_client *client)
489{
Dave Airlie0d697042012-09-10 12:28:36 +1000490 if (client->driver_power_control)
491 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000492 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200493 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000494 if (vgasr_priv.handler->power_state)
495 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000496 client->pwr_state = VGA_SWITCHEROO_OFF;
497 return 0;
498}
499
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200500static void set_audio_state(enum vga_switcheroo_client_id id,
501 enum vga_switcheroo_state state)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200502{
503 struct vga_switcheroo_client *client;
504
505 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
506 if (client && client->pwr_state != state) {
507 client->ops->set_gpu_state(client->pdev, state);
508 client->pwr_state = state;
509 }
510}
511
Dave Airlie66b37c62010-12-07 14:24:25 +1000512/* stage one happens before delay */
513static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000514{
Takashi Iwai79721e02012-04-26 12:55:59 +0200515 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000516
Takashi Iwai79721e02012-04-26 12:55:59 +0200517 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000518 if (!active)
519 return 0;
520
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000521 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
522 vga_switchon(new_client);
523
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400524 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000525 return 0;
526}
527
528/* post delay */
529static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
530{
531 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200532 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000533
Takashi Iwai79721e02012-04-26 12:55:59 +0200534 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000535 if (!active)
536 return 0;
537
538 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000539
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200540 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
541
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000542 if (new_client->fb_info) {
543 struct fb_event event;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200544
Dave Airlie054430e2013-01-25 11:38:56 +1000545 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000546 event.info = new_client->fb_info;
547 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie054430e2013-01-25 11:38:56 +1000548 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000549 }
550
551 ret = vgasr_priv.handler->switchto(new_client->id);
552 if (ret)
553 return ret;
554
Takashi Iwai26ec6852012-05-11 07:51:17 +0200555 if (new_client->ops->reprobe)
556 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000557
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000558 if (active->pwr_state == VGA_SWITCHEROO_ON)
559 vga_switchoff(active);
560
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200561 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
562
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000563 new_client->active = true;
564 return 0;
565}
566
Takashi Iwai79721e02012-04-26 12:55:59 +0200567static bool check_can_switch(void)
568{
569 struct vga_switcheroo_client *client;
570
571 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200572 if (!client->ops->can_switch(client->pdev)) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200573 pr_err("client %x refused switch\n", client->id);
Takashi Iwai79721e02012-04-26 12:55:59 +0200574 return false;
575 }
576 }
577 return true;
578}
579
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000580static ssize_t
581vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
582 size_t cnt, loff_t *ppos)
583{
584 char usercmd[64];
Takashi Iwai79721e02012-04-26 12:55:59 +0200585 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000586 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000587 bool just_mux = false;
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200588 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000589 struct vga_switcheroo_client *client = NULL;
590
591 if (cnt > 63)
592 cnt = 63;
593
594 if (copy_from_user(usercmd, ubuf, cnt))
595 return -EFAULT;
596
597 mutex_lock(&vgasr_mutex);
598
Jiri Slaby8c88e502010-04-27 14:11:03 -0700599 if (!vgasr_priv.active) {
600 cnt = -EINVAL;
601 goto out;
602 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000603
604 /* pwr off the device not in use */
605 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200606 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200607 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000608 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000609 if (client->driver_power_control)
610 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200611 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200612 if (client->pwr_state == VGA_SWITCHEROO_ON)
613 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000614 }
615 goto out;
616 }
617 /* pwr on the device not in use */
618 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200619 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200620 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000621 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000622 if (client->driver_power_control)
623 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200624 if (client->pwr_state == VGA_SWITCHEROO_OFF)
625 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200626 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000627 }
628 goto out;
629 }
630
631 /* request a delayed switch - test can we switch now */
632 if (strncmp(usercmd, "DIGD", 4) == 0) {
633 client_id = VGA_SWITCHEROO_IGD;
634 delay = true;
635 }
636
637 if (strncmp(usercmd, "DDIS", 4) == 0) {
638 client_id = VGA_SWITCHEROO_DIS;
639 delay = true;
640 }
641
642 if (strncmp(usercmd, "IGD", 3) == 0)
643 client_id = VGA_SWITCHEROO_IGD;
644
645 if (strncmp(usercmd, "DIS", 3) == 0)
646 client_id = VGA_SWITCHEROO_DIS;
647
Dan Carpenterfea6f332011-01-07 08:12:27 +0300648 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000649 just_mux = true;
650 client_id = VGA_SWITCHEROO_IGD;
651 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300652 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000653 just_mux = true;
654 client_id = VGA_SWITCHEROO_DIS;
655 }
656
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200657 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000658 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200659 client = find_client_from_id(&vgasr_priv.clients, client_id);
660 if (!client)
661 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000662
663 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000664
665 if (just_mux) {
666 ret = vgasr_priv.handler->switchto(client_id);
667 goto out;
668 }
669
Takashi Iwai79721e02012-04-26 12:55:59 +0200670 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200671 goto out;
672
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000673 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200674 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000675
676 if (can_switch == false && delay == false)
677 goto out;
678
Takashi Iwai79721e02012-04-26 12:55:59 +0200679 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000680 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000681 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200682 pr_err("switching failed stage 1 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000683
684 ret = vga_switchto_stage2(client);
685 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200686 pr_err("switching failed stage 2 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000687
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000688 } else {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200689 pr_info("setting delayed switch to client %d\n", client->id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000690 vgasr_priv.delayed_switch_active = true;
691 vgasr_priv.delayed_client_id = client_id;
692
Dave Airlie66b37c62010-12-07 14:24:25 +1000693 ret = vga_switchto_stage1(client);
694 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200695 pr_err("delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000696 }
697
698out:
699 mutex_unlock(&vgasr_mutex);
700 return cnt;
701}
702
703static const struct file_operations vga_switcheroo_debugfs_fops = {
704 .owner = THIS_MODULE,
705 .open = vga_switcheroo_debugfs_open,
706 .write = vga_switcheroo_debugfs_write,
707 .read = seq_read,
708 .llseek = seq_lseek,
709 .release = single_release,
710};
711
712static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
713{
Thierry Reding798ae0f2015-08-12 16:32:12 +0200714 debugfs_remove(priv->switch_file);
715 priv->switch_file = NULL;
716
717 debugfs_remove(priv->debugfs_root);
718 priv->debugfs_root = NULL;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000719}
720
721static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
722{
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200723 static const char mp[] = "/sys/kernel/debug";
724
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000725 /* already initialised */
726 if (priv->debugfs_root)
727 return 0;
728 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
729
730 if (!priv->debugfs_root) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200731 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000732 goto fail;
733 }
734
735 priv->switch_file = debugfs_create_file("switch", 0644,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200736 priv->debugfs_root, NULL,
737 &vga_switcheroo_debugfs_fops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000738 if (!priv->switch_file) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200739 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000740 goto fail;
741 }
742 return 0;
743fail:
744 vga_switcheroo_debugfs_fini(priv);
745 return -1;
746}
747
Lukas Wunnera6456542015-08-23 15:18:55 +0200748/**
749 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
750 *
751 * Process a delayed switch if one is pending. DRM drivers should call this
752 * from their ->lastclose callback.
753 *
754 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
755 * has unregistered in the meantime or if there are other clients blocking the
756 * switch. If the actual switch fails, an error is reported and 0 is returned.
757 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000758int vga_switcheroo_process_delayed_switch(void)
759{
Takashi Iwai79721e02012-04-26 12:55:59 +0200760 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000761 int ret;
762 int err = -EINVAL;
763
764 mutex_lock(&vgasr_mutex);
765 if (!vgasr_priv.delayed_switch_active)
766 goto err;
767
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200768 pr_info("processing delayed switch to %d\n",
769 vgasr_priv.delayed_client_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000770
Takashi Iwai79721e02012-04-26 12:55:59 +0200771 client = find_client_from_id(&vgasr_priv.clients,
772 vgasr_priv.delayed_client_id);
773 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000774 goto err;
775
Dave Airlie66b37c62010-12-07 14:24:25 +1000776 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000777 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200778 pr_err("delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000779
780 vgasr_priv.delayed_switch_active = false;
781 err = 0;
782err:
783 mutex_unlock(&vgasr_mutex);
784 return err;
785}
786EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
Dave Airlie0d697042012-09-10 12:28:36 +1000787
Lukas Wunnera6456542015-08-23 15:18:55 +0200788/**
789 * DOC: Driver power control
790 *
791 * In this mode of use, the discrete GPU automatically powers up and down at
792 * the discretion of the driver's runtime pm. On muxed machines, the user may
793 * still influence the muxer state by way of the debugfs interface, however
794 * the ON and OFF commands become a no-op for the discrete GPU.
795 *
796 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
797 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
798 * command line disables it.
799 *
800 * When the driver decides to power up or down, it notifies vga_switcheroo
801 * thereof so that it can (a) power the audio device on the GPU up or down,
802 * and (b) update its internal power state representation for the device.
803 * This is achieved by vga_switcheroo_set_dynamic_switch().
804 *
805 * After the GPU has been suspended, the handler needs to be called to cut
806 * power to the GPU. Likewise it needs to reinstate power before the GPU
807 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
808 * which augments the GPU's suspend/resume functions by the requisite
809 * calls to the handler.
810 *
811 * When the audio device resumes, the GPU needs to be woken. This is achieved
812 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
813 * audio device's resume function.
814 *
815 * On muxed machines, if the mux is initially switched to the discrete GPU,
816 * the user ends up with a black screen when the GPU powers down after boot.
817 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
818 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
819 */
820
Thierry Reding7491bfb2015-08-12 16:32:11 +0200821static void vga_switcheroo_power_switch(struct pci_dev *pdev,
822 enum vga_switcheroo_state state)
Dave Airlie0d697042012-09-10 12:28:36 +1000823{
824 struct vga_switcheroo_client *client;
825
826 if (!vgasr_priv.handler->power_state)
827 return;
828
829 client = find_client_from_pci(&vgasr_priv.clients, pdev);
830 if (!client)
831 return;
832
833 if (!client->driver_power_control)
834 return;
835
836 vgasr_priv.handler->power_state(client->id, state);
837}
838
Lukas Wunnera6456542015-08-23 15:18:55 +0200839/**
840 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
841 * @pdev: client pci device
842 * @dynamic: new power state
843 *
844 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
845 * When the driver decides to power up or down, it notifies vga_switcheroo
846 * thereof using this helper so that it can (a) power the audio device on
847 * the GPU up or down, and (b) update its internal power state representation
848 * for the device.
849 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200850void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
851 enum vga_switcheroo_state dynamic)
Dave Airlie0d697042012-09-10 12:28:36 +1000852{
853 struct vga_switcheroo_client *client;
854
Lukas Wunner8f12a312015-08-23 23:23:02 +0200855 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000856 client = find_client_from_pci(&vgasr_priv.clients, pdev);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200857 if (!client || !client->driver_power_control) {
858 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000859 return;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200860 }
Dave Airlie0d697042012-09-10 12:28:36 +1000861
862 client->pwr_state = dynamic;
863 set_audio_state(client->id, dynamic);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200864 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000865}
866EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
867
868/* switcheroo power domain */
869static int vga_switcheroo_runtime_suspend(struct device *dev)
870{
871 struct pci_dev *pdev = to_pci_dev(dev);
872 int ret;
873
874 ret = dev->bus->pm->runtime_suspend(dev);
875 if (ret)
876 return ret;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200877 mutex_lock(&vgasr_mutex);
Alex Deucherf2bc561612014-05-19 14:04:43 -0400878 if (vgasr_priv.handler->switchto)
879 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
Dave Airlie0d697042012-09-10 12:28:36 +1000880 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200881 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000882 return 0;
883}
884
885static int vga_switcheroo_runtime_resume(struct device *dev)
886{
887 struct pci_dev *pdev = to_pci_dev(dev);
888 int ret;
889
Lukas Wunner8f12a312015-08-23 23:23:02 +0200890 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000891 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200892 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000893 ret = dev->bus->pm->runtime_resume(dev);
894 if (ret)
895 return ret;
896
897 return 0;
898}
899
Lukas Wunnera6456542015-08-23 15:18:55 +0200900/**
901 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
902 * @dev: vga client device
903 * @domain: power domain
904 *
905 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
906 * After the GPU has been suspended, the handler needs to be called to cut
907 * power to the GPU. Likewise it needs to reinstate power before the GPU
908 * can resume. To this end, this helper augments the suspend/resume functions
909 * by the requisite calls to the handler. It needs only be called on platforms
910 * where the power switch is separate to the device being powered down.
911 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200912int vga_switcheroo_init_domain_pm_ops(struct device *dev,
913 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000914{
915 /* copy over all the bus versions */
916 if (dev->bus && dev->bus->pm) {
917 domain->ops = *dev->bus->pm;
918 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
919 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
920
921 dev->pm_domain = domain;
922 return 0;
923 }
924 dev->pm_domain = NULL;
925 return -EINVAL;
926}
927EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
928
Alex Deucher766a53d2014-09-12 17:51:29 -0400929void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
930{
931 dev->pm_domain = NULL;
932}
933EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
934
Dave Airlie0d697042012-09-10 12:28:36 +1000935static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
936{
937 struct pci_dev *pdev = to_pci_dev(dev);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200938 struct vga_switcheroo_client *client;
939 struct device *video_dev = NULL;
Dave Airlie0d697042012-09-10 12:28:36 +1000940 int ret;
Dave Airlie0d697042012-09-10 12:28:36 +1000941
942 /* we need to check if we have to switch back on the video
943 device so the audio device can come back */
Lukas Wunner8f12a312015-08-23 23:23:02 +0200944 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000945 list_for_each_entry(client, &vgasr_priv.clients, list) {
Thierry Reding7491bfb2015-08-12 16:32:11 +0200946 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
947 client_is_vga(client)) {
Lukas Wunner8f12a312015-08-23 23:23:02 +0200948 video_dev = &client->pdev->dev;
Dave Airlie0d697042012-09-10 12:28:36 +1000949 break;
950 }
951 }
Lukas Wunner8f12a312015-08-23 23:23:02 +0200952 mutex_unlock(&vgasr_mutex);
953
954 if (video_dev) {
955 ret = pm_runtime_get_sync(video_dev);
956 if (ret && ret != 1)
957 return ret;
958 }
Dave Airlie0d697042012-09-10 12:28:36 +1000959 ret = dev->bus->pm->runtime_resume(dev);
960
961 /* put the reference for the gpu */
Lukas Wunner8f12a312015-08-23 23:23:02 +0200962 if (video_dev) {
963 pm_runtime_mark_last_busy(video_dev);
964 pm_runtime_put_autosuspend(video_dev);
Dave Airlie0d697042012-09-10 12:28:36 +1000965 }
966 return ret;
967}
968
Lukas Wunnera6456542015-08-23 15:18:55 +0200969/**
970 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
971 * power control
972 * @dev: audio client device
973 * @domain: power domain
974 *
975 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
976 * When the audio device resumes, the GPU needs to be woken. This helper
977 * augments the audio device's resume function to do that.
978 *
979 * Return: 0 on success, -EINVAL if no power management operations are
980 * defined for this device.
981 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200982int
983vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
984 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000985{
986 /* copy over all the bus versions */
987 if (dev->bus && dev->bus->pm) {
988 domain->ops = *dev->bus->pm;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200989 domain->ops.runtime_resume =
990 vga_switcheroo_runtime_resume_hdmi_audio;
Dave Airlie0d697042012-09-10 12:28:36 +1000991
992 dev->pm_domain = domain;
993 return 0;
994 }
995 dev->pm_domain = NULL;
996 return -EINVAL;
997}
998EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);