blob: e89f6ad5d83ede5374753b5991abc4bf347b299d [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>
Tomeu Vizoso989561d2016-01-07 16:46:13 +010039#include <linux/pm_domain.h>
Dave Airlie0d697042012-09-10 12:28:36 +100040#include <linux/pm_runtime.h>
Lukas Wunner41278382015-09-05 13:40:23 +020041#include <linux/seq_file.h>
42#include <linux/uaccess.h>
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040043#include <linux/vgaarb.h>
Lukas Wunner41278382015-09-05 13:40:23 +020044#include <linux/vga_switcheroo.h>
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040045
Lukas Wunnera6456542015-08-23 15:18:55 +020046/**
47 * DOC: Overview
48 *
49 * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
50 * These come in two flavors:
51 *
52 * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
53 * * muxless: Dual GPUs but only one of them is connected to outputs.
54 * The other one is merely used to offload rendering, its results
55 * are copied over PCIe into the framebuffer. On Linux this is
56 * supported with DRI PRIME.
57 *
58 * Hybrid graphics started to appear in the late Naughties and were initially
59 * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
60 * A notable exception is the MacBook Pro which continues to use a mux.
61 * Muxes come with varying capabilities: Some switch only the panel, others
62 * can also switch external displays. Some switch all display pins at once
63 * while others can switch just the DDC lines. (To allow EDID probing
64 * for the inactive GPU.) Also, muxes are often used to cut power to the
65 * discrete GPU while it is not used.
66 *
Lukas Wunner3a848662016-01-02 18:47:17 +010067 * DRM drivers register GPUs with vga_switcheroo, these are henceforth called
Lukas Wunnera6456542015-08-23 15:18:55 +020068 * clients. The mux is called the handler. Muxless machines also register a
69 * handler to control the power state of the discrete GPU, its ->switchto
70 * callback is a no-op for obvious reasons. The discrete GPU is often equipped
71 * with an HDA controller for the HDMI/DP audio signal, this will also
72 * register as a client so that vga_switcheroo can take care of the correct
73 * suspend/resume order when changing the discrete GPU's power state. In total
74 * there can thus be up to three clients: Two vga clients (GPUs) and one audio
75 * client (on the discrete GPU). The code is mostly prepared to support
76 * machines with more than two GPUs should they become available.
77 * The GPU to which the outputs are currently switched is called the
78 * active client in vga_switcheroo parlance. The GPU not in use is the
79 * inactive client.
80 */
81
82/**
83 * struct vga_switcheroo_client - registered client
84 * @pdev: client pci device
85 * @fb_info: framebuffer to which console is remapped on switching
86 * @pwr_state: current power state
87 * @ops: client callbacks
Lukas Wunner21c5ba82015-08-28 13:30:32 +020088 * @id: client identifier. Determining the id requires the handler,
89 * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
90 * and later given their true id in vga_switcheroo_enable()
Lukas Wunnera6456542015-08-23 15:18:55 +020091 * @active: whether the outputs are currently switched to this client
92 * @driver_power_control: whether power state is controlled by the driver's
93 * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
94 * interface is a no-op so as not to interfere with runtime pm
95 * @list: client list
96 *
97 * Registered client. A client can be either a GPU or an audio device on a GPU.
98 * For audio clients, the @fb_info, @active and @driver_power_control members
99 * are bogus.
100 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000101struct vga_switcheroo_client {
102 struct pci_dev *pdev;
103 struct fb_info *fb_info;
Lukas Wunner203d0272015-08-28 11:56:26 +0200104 enum vga_switcheroo_state pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200105 const struct vga_switcheroo_client_ops *ops;
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200106 enum vga_switcheroo_client_id id;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000107 bool active;
Dave Airlie0d697042012-09-10 12:28:36 +1000108 bool driver_power_control;
Takashi Iwai79721e02012-04-26 12:55:59 +0200109 struct list_head list;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000110};
111
Lukas Wunnera6456542015-08-23 15:18:55 +0200112/*
113 * protects access to struct vgasr_priv
114 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000115static DEFINE_MUTEX(vgasr_mutex);
116
Lukas Wunnera6456542015-08-23 15:18:55 +0200117/**
118 * struct vgasr_priv - vga_switcheroo private data
119 * @active: whether vga_switcheroo is enabled.
120 * Prerequisite is the registration of two GPUs and a handler
121 * @delayed_switch_active: whether a delayed switch is pending
122 * @delayed_client_id: client to which a delayed switch is pending
123 * @debugfs_root: directory for vga_switcheroo debugfs interface
124 * @switch_file: file for vga_switcheroo debugfs interface
125 * @registered_clients: number of registered GPUs
126 * (counting only vga clients, not audio clients)
127 * @clients: list of registered clients
128 * @handler: registered handler
Lukas Wunner156d7d42016-01-11 20:09:20 +0100129 * @handler_flags: flags of registered handler
Lukas Wunnera6456542015-08-23 15:18:55 +0200130 *
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
Lukas Wunner5d170132015-10-18 13:05:40 +0200145 const struct vga_switcheroo_handler *handler;
Lukas Wunner156d7d42016-01-11 20:09:20 +0100146 enum vga_switcheroo_handler_flags_t handler_flags;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000147};
148
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200149#define ID_BIT_AUDIO 0x100
150#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200151#define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
152 !client_is_audio(c))
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200153#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
154
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000155static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
156static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
157
158/* only one switcheroo per system */
Takashi Iwai79721e02012-04-26 12:55:59 +0200159static struct vgasr_priv vgasr_priv = {
160 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
161};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000162
Seth Forshee36704c02012-08-17 11:17:03 -0500163static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000164{
Seth Forshee36704c02012-08-17 11:17:03 -0500165 /* we're ready if we get two clients + handler */
166 return !vgasr_priv.active &&
167 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000168}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000169
170static void vga_switcheroo_enable(void)
171{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000172 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200173 struct vga_switcheroo_client *client;
174
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000175 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -0500176 if (vgasr_priv.handler->init)
177 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000178
Takashi Iwai79721e02012-04-26 12:55:59 +0200179 list_for_each_entry(client, &vgasr_priv.clients, list) {
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200180 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200181 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200182 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000183 if (ret < 0)
184 return;
185
Takashi Iwai79721e02012-04-26 12:55:59 +0200186 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000187 }
188 vga_switcheroo_debugfs_init(&vgasr_priv);
189 vgasr_priv.active = true;
190}
191
Lukas Wunnera6456542015-08-23 15:18:55 +0200192/**
193 * vga_switcheroo_register_handler() - register handler
194 * @handler: handler callbacks
Lukas Wunner156d7d42016-01-11 20:09:20 +0100195 * @handler_flags: handler flags
Lukas Wunnera6456542015-08-23 15:18:55 +0200196 *
197 * Register handler. Enable vga_switcheroo if two vga clients have already
198 * registered.
199 *
200 * Return: 0 on success, -EINVAL if a handler was already registered.
201 */
Lukas Wunner156d7d42016-01-11 20:09:20 +0100202int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
203 enum vga_switcheroo_handler_flags_t handler_flags)
Seth Forshee36704c02012-08-17 11:17:03 -0500204{
205 mutex_lock(&vgasr_mutex);
206 if (vgasr_priv.handler) {
207 mutex_unlock(&vgasr_mutex);
208 return -EINVAL;
209 }
210
211 vgasr_priv.handler = handler;
Lukas Wunner156d7d42016-01-11 20:09:20 +0100212 vgasr_priv.handler_flags = handler_flags;
Seth Forshee36704c02012-08-17 11:17:03 -0500213 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200214 pr_info("enabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500215 vga_switcheroo_enable();
216 }
217 mutex_unlock(&vgasr_mutex);
218 return 0;
219}
220EXPORT_SYMBOL(vga_switcheroo_register_handler);
221
Lukas Wunnera6456542015-08-23 15:18:55 +0200222/**
223 * vga_switcheroo_unregister_handler() - unregister handler
224 *
225 * Unregister handler. Disable vga_switcheroo.
226 */
Seth Forshee36704c02012-08-17 11:17:03 -0500227void vga_switcheroo_unregister_handler(void)
228{
229 mutex_lock(&vgasr_mutex);
Lukas Wunner156d7d42016-01-11 20:09:20 +0100230 vgasr_priv.handler_flags = 0;
Seth Forshee36704c02012-08-17 11:17:03 -0500231 vgasr_priv.handler = NULL;
232 if (vgasr_priv.active) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200233 pr_info("disabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500234 vga_switcheroo_debugfs_fini(&vgasr_priv);
235 vgasr_priv.active = false;
236 }
237 mutex_unlock(&vgasr_mutex);
238}
239EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
240
Lukas Wunner156d7d42016-01-11 20:09:20 +0100241/**
242 * vga_switcheroo_handler_flags() - obtain handler flags
243 *
244 * Helper for clients to obtain the handler flags bitmask.
245 *
246 * Return: Handler flags. A value of 0 means that no handler is registered
247 * or that the handler has no special capabilities.
248 */
249enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void)
250{
251 return vgasr_priv.handler_flags;
252}
253EXPORT_SYMBOL(vga_switcheroo_handler_flags);
254
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200255static int register_client(struct pci_dev *pdev,
256 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200257 enum vga_switcheroo_client_id id, bool active,
258 bool driver_power_control)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000259{
Takashi Iwai79721e02012-04-26 12:55:59 +0200260 struct vga_switcheroo_client *client;
261
262 client = kzalloc(sizeof(*client), GFP_KERNEL);
263 if (!client)
264 return -ENOMEM;
265
266 client->pwr_state = VGA_SWITCHEROO_ON;
267 client->pdev = pdev;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200268 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200269 client->id = id;
270 client->active = active;
Dave Airlie0d697042012-09-10 12:28:36 +1000271 client->driver_power_control = driver_power_control;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000272
273 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200274 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200275 if (client_is_vga(client))
276 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000277
Seth Forshee36704c02012-08-17 11:17:03 -0500278 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200279 pr_info("enabled\n");
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000280 vga_switcheroo_enable();
281 }
282 mutex_unlock(&vgasr_mutex);
283 return 0;
284}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200285
Lukas Wunnera6456542015-08-23 15:18:55 +0200286/**
287 * vga_switcheroo_register_client - register vga client
288 * @pdev: client pci device
289 * @ops: client callbacks
290 * @driver_power_control: whether power state is controlled by the driver's
291 * runtime pm
292 *
293 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
294 * handler have already registered. The power state of the client is assumed
295 * 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_client(struct pci_dev *pdev,
Dave Airlie0d697042012-09-10 12:28:36 +1000300 const struct vga_switcheroo_client_ops *ops,
301 bool driver_power_control)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200302{
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200303 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200304 pdev == vga_default_device(),
305 driver_power_control);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200306}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000307EXPORT_SYMBOL(vga_switcheroo_register_client);
308
Lukas Wunnera6456542015-08-23 15:18:55 +0200309/**
310 * vga_switcheroo_register_audio_client - register audio client
311 * @pdev: client pci device
312 * @ops: client callbacks
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200313 * @id: client identifier
Lukas Wunnera6456542015-08-23 15:18:55 +0200314 *
315 * Register audio client (audio device on a GPU). The power state of the
316 * client is assumed to be ON.
317 *
318 * Return: 0 on success, -ENOMEM on memory allocation error.
319 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200320int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
321 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200322 enum vga_switcheroo_client_id id)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200323{
Lukas Wunner21b45672015-08-27 16:43:43 +0200324 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200325}
326EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
327
Takashi Iwai79721e02012-04-26 12:55:59 +0200328static struct vga_switcheroo_client *
329find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
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)
334 if (client->pdev == pdev)
335 return client;
336 return NULL;
337}
338
339static struct vga_switcheroo_client *
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200340find_client_from_id(struct list_head *head,
341 enum vga_switcheroo_client_id client_id)
Takashi Iwai79721e02012-04-26 12:55:59 +0200342{
343 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200344
Takashi Iwai79721e02012-04-26 12:55:59 +0200345 list_for_each_entry(client, head, list)
346 if (client->id == client_id)
347 return client;
348 return NULL;
349}
350
351static struct vga_switcheroo_client *
352find_active_client(struct list_head *head)
353{
354 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200355
Takashi Iwai79721e02012-04-26 12:55:59 +0200356 list_for_each_entry(client, head, list)
Lukas Wunner21b45672015-08-27 16:43:43 +0200357 if (client->active)
Takashi Iwai79721e02012-04-26 12:55:59 +0200358 return client;
359 return NULL;
360}
361
Lukas Wunnera6456542015-08-23 15:18:55 +0200362/**
363 * vga_switcheroo_get_client_state() - obtain power state of a given client
364 * @pdev: client pci device
365 *
366 * Obtain power state of a given client as seen from vga_switcheroo.
367 * The function is only called from hda_intel.c.
368 *
369 * Return: Power state.
370 */
Lukas Wunner203d0272015-08-28 11:56:26 +0200371enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200372{
373 struct vga_switcheroo_client *client;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200374 enum vga_switcheroo_state ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200375
Lukas Wunner8f12a312015-08-23 23:23:02 +0200376 mutex_lock(&vgasr_mutex);
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200377 client = find_client_from_pci(&vgasr_priv.clients, pdev);
378 if (!client)
Lukas Wunner8f12a312015-08-23 23:23:02 +0200379 ret = VGA_SWITCHEROO_NOT_FOUND;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200380 else
381 ret = client->pwr_state;
382 mutex_unlock(&vgasr_mutex);
383 return ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200384}
385EXPORT_SYMBOL(vga_switcheroo_get_client_state);
386
Lukas Wunnera6456542015-08-23 15:18:55 +0200387/**
388 * vga_switcheroo_unregister_client() - unregister client
389 * @pdev: client pci device
390 *
391 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
392 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000393void vga_switcheroo_unregister_client(struct pci_dev *pdev)
394{
Takashi Iwai79721e02012-04-26 12:55:59 +0200395 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000396
397 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200398 client = find_client_from_pci(&vgasr_priv.clients, pdev);
399 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200400 if (client_is_vga(client))
401 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200402 list_del(&client->list);
403 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000404 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200405 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200406 pr_info("disabled\n");
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200407 vga_switcheroo_debugfs_fini(&vgasr_priv);
408 vgasr_priv.active = false;
409 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000410 mutex_unlock(&vgasr_mutex);
411}
412EXPORT_SYMBOL(vga_switcheroo_unregister_client);
413
Lukas Wunnera6456542015-08-23 15:18:55 +0200414/**
415 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
416 * @pdev: client pci device
417 * @info: framebuffer
418 *
419 * Set framebuffer of a given client. The console will be remapped to this
420 * on switching.
421 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000422void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
423 struct fb_info *info)
424{
Takashi Iwai79721e02012-04-26 12:55:59 +0200425 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000426
427 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200428 client = find_client_from_pci(&vgasr_priv.clients, pdev);
429 if (client)
430 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000431 mutex_unlock(&vgasr_mutex);
432}
433EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
434
Lukas Wunnera6456542015-08-23 15:18:55 +0200435/**
436 * DOC: Manual switching and manual power control
437 *
438 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
439 * can be read to retrieve the current vga_switcheroo state and commands
440 * can be written to it to change the state. The file appears as soon as
441 * two GPU drivers and one handler have registered with vga_switcheroo.
442 * The following commands are understood:
443 *
444 * * OFF: Power off the device not in use.
445 * * ON: Power on the device not in use.
446 * * IGD: Switch to the integrated graphics device.
447 * Power on the integrated GPU if necessary, power off the discrete GPU.
448 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
449 * have opened device files of the GPUs or the audio client. If the
450 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
451 * and /dev/snd/controlC1 to identify processes blocking the switch.
452 * * DIS: Switch to the discrete graphics device.
453 * * DIGD: Delayed switch to the integrated graphics device.
454 * This will perform the switch once the last user space process has
455 * closed the device files of the GPUs and the audio client.
456 * * DDIS: Delayed switch to the discrete graphics device.
457 * * MIGD: Mux-only switch to the integrated graphics device.
458 * Does not remap console or change the power state of either gpu.
459 * If the integrated GPU is currently off, the screen will turn black.
460 * If it is on, the screen will show whatever happens to be in VRAM.
461 * Either way, the user has to blindly enter the command to switch back.
462 * * MDIS: Mux-only switch to the discrete graphics device.
463 *
464 * For GPUs whose power state is controlled by the driver's runtime pm,
465 * the ON and OFF commands are a no-op (see next section).
466 *
467 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
468 * should not be used.
469 */
470
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000471static int vga_switcheroo_show(struct seq_file *m, void *v)
472{
Takashi Iwai79721e02012-04-26 12:55:59 +0200473 struct vga_switcheroo_client *client;
474 int i = 0;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200475
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000476 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200477 list_for_each_entry(client, &vgasr_priv.clients, list) {
Dave Airlie0d697042012-09-10 12:28:36 +1000478 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200479 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
480 "IGD",
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200481 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200482 client->active ? '+' : ' ',
Dave Airlie0d697042012-09-10 12:28:36 +1000483 client->driver_power_control ? "Dyn" : "",
Takashi Iwai79721e02012-04-26 12:55:59 +0200484 client->pwr_state ? "Pwr" : "Off",
485 pci_name(client->pdev));
486 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000487 }
488 mutex_unlock(&vgasr_mutex);
489 return 0;
490}
491
492static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
493{
494 return single_open(file, vga_switcheroo_show, NULL);
495}
496
497static int vga_switchon(struct vga_switcheroo_client *client)
498{
Dave Airlie0d697042012-09-10 12:28:36 +1000499 if (client->driver_power_control)
500 return 0;
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000501 if (vgasr_priv.handler->power_state)
502 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000503 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200504 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000505 client->pwr_state = VGA_SWITCHEROO_ON;
506 return 0;
507}
508
509static int vga_switchoff(struct vga_switcheroo_client *client)
510{
Dave Airlie0d697042012-09-10 12:28:36 +1000511 if (client->driver_power_control)
512 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000513 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200514 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000515 if (vgasr_priv.handler->power_state)
516 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000517 client->pwr_state = VGA_SWITCHEROO_OFF;
518 return 0;
519}
520
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200521static void set_audio_state(enum vga_switcheroo_client_id id,
522 enum vga_switcheroo_state state)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200523{
524 struct vga_switcheroo_client *client;
525
526 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
527 if (client && client->pwr_state != state) {
528 client->ops->set_gpu_state(client->pdev, state);
529 client->pwr_state = state;
530 }
531}
532
Dave Airlie66b37c62010-12-07 14:24:25 +1000533/* stage one happens before delay */
534static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000535{
Takashi Iwai79721e02012-04-26 12:55:59 +0200536 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000537
Takashi Iwai79721e02012-04-26 12:55:59 +0200538 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000539 if (!active)
540 return 0;
541
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000542 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
543 vga_switchon(new_client);
544
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400545 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000546 return 0;
547}
548
549/* post delay */
550static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
551{
552 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200553 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000554
Takashi Iwai79721e02012-04-26 12:55:59 +0200555 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000556 if (!active)
557 return 0;
558
559 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000560
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200561 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
562
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000563 if (new_client->fb_info) {
564 struct fb_event event;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200565
Dave Airlie054430e2013-01-25 11:38:56 +1000566 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000567 event.info = new_client->fb_info;
568 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie054430e2013-01-25 11:38:56 +1000569 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000570 }
571
572 ret = vgasr_priv.handler->switchto(new_client->id);
573 if (ret)
574 return ret;
575
Takashi Iwai26ec6852012-05-11 07:51:17 +0200576 if (new_client->ops->reprobe)
577 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000578
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000579 if (active->pwr_state == VGA_SWITCHEROO_ON)
580 vga_switchoff(active);
581
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200582 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
583
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000584 new_client->active = true;
585 return 0;
586}
587
Takashi Iwai79721e02012-04-26 12:55:59 +0200588static bool check_can_switch(void)
589{
590 struct vga_switcheroo_client *client;
591
592 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200593 if (!client->ops->can_switch(client->pdev)) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200594 pr_err("client %x refused switch\n", client->id);
Takashi Iwai79721e02012-04-26 12:55:59 +0200595 return false;
596 }
597 }
598 return true;
599}
600
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000601static ssize_t
602vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
603 size_t cnt, loff_t *ppos)
604{
605 char usercmd[64];
Takashi Iwai79721e02012-04-26 12:55:59 +0200606 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000607 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000608 bool just_mux = false;
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200609 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000610 struct vga_switcheroo_client *client = NULL;
611
612 if (cnt > 63)
613 cnt = 63;
614
615 if (copy_from_user(usercmd, ubuf, cnt))
616 return -EFAULT;
617
618 mutex_lock(&vgasr_mutex);
619
Jiri Slaby8c88e502010-04-27 14:11:03 -0700620 if (!vgasr_priv.active) {
621 cnt = -EINVAL;
622 goto out;
623 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000624
625 /* pwr off the device not in use */
626 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200627 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200628 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000629 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000630 if (client->driver_power_control)
631 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200632 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200633 if (client->pwr_state == VGA_SWITCHEROO_ON)
634 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000635 }
636 goto out;
637 }
638 /* pwr on the device not in use */
639 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200640 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200641 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000642 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000643 if (client->driver_power_control)
644 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200645 if (client->pwr_state == VGA_SWITCHEROO_OFF)
646 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200647 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000648 }
649 goto out;
650 }
651
652 /* request a delayed switch - test can we switch now */
653 if (strncmp(usercmd, "DIGD", 4) == 0) {
654 client_id = VGA_SWITCHEROO_IGD;
655 delay = true;
656 }
657
658 if (strncmp(usercmd, "DDIS", 4) == 0) {
659 client_id = VGA_SWITCHEROO_DIS;
660 delay = true;
661 }
662
663 if (strncmp(usercmd, "IGD", 3) == 0)
664 client_id = VGA_SWITCHEROO_IGD;
665
666 if (strncmp(usercmd, "DIS", 3) == 0)
667 client_id = VGA_SWITCHEROO_DIS;
668
Dan Carpenterfea6f332011-01-07 08:12:27 +0300669 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000670 just_mux = true;
671 client_id = VGA_SWITCHEROO_IGD;
672 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300673 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000674 just_mux = true;
675 client_id = VGA_SWITCHEROO_DIS;
676 }
677
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200678 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000679 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200680 client = find_client_from_id(&vgasr_priv.clients, client_id);
681 if (!client)
682 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000683
684 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000685
686 if (just_mux) {
687 ret = vgasr_priv.handler->switchto(client_id);
688 goto out;
689 }
690
Takashi Iwai79721e02012-04-26 12:55:59 +0200691 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200692 goto out;
693
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000694 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200695 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000696
697 if (can_switch == false && delay == false)
698 goto out;
699
Takashi Iwai79721e02012-04-26 12:55:59 +0200700 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000701 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000702 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200703 pr_err("switching failed stage 1 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000704
705 ret = vga_switchto_stage2(client);
706 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200707 pr_err("switching failed stage 2 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000708
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000709 } else {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200710 pr_info("setting delayed switch to client %d\n", client->id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000711 vgasr_priv.delayed_switch_active = true;
712 vgasr_priv.delayed_client_id = client_id;
713
Dave Airlie66b37c62010-12-07 14:24:25 +1000714 ret = vga_switchto_stage1(client);
715 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200716 pr_err("delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000717 }
718
719out:
720 mutex_unlock(&vgasr_mutex);
721 return cnt;
722}
723
724static const struct file_operations vga_switcheroo_debugfs_fops = {
725 .owner = THIS_MODULE,
726 .open = vga_switcheroo_debugfs_open,
727 .write = vga_switcheroo_debugfs_write,
728 .read = seq_read,
729 .llseek = seq_lseek,
730 .release = single_release,
731};
732
733static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
734{
Thierry Reding798ae0f2015-08-12 16:32:12 +0200735 debugfs_remove(priv->switch_file);
736 priv->switch_file = NULL;
737
738 debugfs_remove(priv->debugfs_root);
739 priv->debugfs_root = NULL;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000740}
741
742static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
743{
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200744 static const char mp[] = "/sys/kernel/debug";
745
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000746 /* already initialised */
747 if (priv->debugfs_root)
748 return 0;
749 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
750
751 if (!priv->debugfs_root) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200752 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000753 goto fail;
754 }
755
756 priv->switch_file = debugfs_create_file("switch", 0644,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200757 priv->debugfs_root, NULL,
758 &vga_switcheroo_debugfs_fops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000759 if (!priv->switch_file) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200760 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000761 goto fail;
762 }
763 return 0;
764fail:
765 vga_switcheroo_debugfs_fini(priv);
766 return -1;
767}
768
Lukas Wunnera6456542015-08-23 15:18:55 +0200769/**
770 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
771 *
772 * Process a delayed switch if one is pending. DRM drivers should call this
773 * from their ->lastclose callback.
774 *
775 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
776 * has unregistered in the meantime or if there are other clients blocking the
777 * switch. If the actual switch fails, an error is reported and 0 is returned.
778 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000779int vga_switcheroo_process_delayed_switch(void)
780{
Takashi Iwai79721e02012-04-26 12:55:59 +0200781 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000782 int ret;
783 int err = -EINVAL;
784
785 mutex_lock(&vgasr_mutex);
786 if (!vgasr_priv.delayed_switch_active)
787 goto err;
788
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200789 pr_info("processing delayed switch to %d\n",
790 vgasr_priv.delayed_client_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000791
Takashi Iwai79721e02012-04-26 12:55:59 +0200792 client = find_client_from_id(&vgasr_priv.clients,
793 vgasr_priv.delayed_client_id);
794 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000795 goto err;
796
Dave Airlie66b37c62010-12-07 14:24:25 +1000797 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000798 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200799 pr_err("delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000800
801 vgasr_priv.delayed_switch_active = false;
802 err = 0;
803err:
804 mutex_unlock(&vgasr_mutex);
805 return err;
806}
807EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
Dave Airlie0d697042012-09-10 12:28:36 +1000808
Lukas Wunnera6456542015-08-23 15:18:55 +0200809/**
810 * DOC: Driver power control
811 *
812 * In this mode of use, the discrete GPU automatically powers up and down at
813 * the discretion of the driver's runtime pm. On muxed machines, the user may
814 * still influence the muxer state by way of the debugfs interface, however
815 * the ON and OFF commands become a no-op for the discrete GPU.
816 *
817 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
818 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
819 * command line disables it.
820 *
821 * When the driver decides to power up or down, it notifies vga_switcheroo
822 * thereof so that it can (a) power the audio device on the GPU up or down,
823 * and (b) update its internal power state representation for the device.
824 * This is achieved by vga_switcheroo_set_dynamic_switch().
825 *
826 * After the GPU has been suspended, the handler needs to be called to cut
827 * power to the GPU. Likewise it needs to reinstate power before the GPU
828 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
829 * which augments the GPU's suspend/resume functions by the requisite
830 * calls to the handler.
831 *
832 * When the audio device resumes, the GPU needs to be woken. This is achieved
833 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
834 * audio device's resume function.
835 *
836 * On muxed machines, if the mux is initially switched to the discrete GPU,
837 * the user ends up with a black screen when the GPU powers down after boot.
838 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
839 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
840 */
841
Thierry Reding7491bfb2015-08-12 16:32:11 +0200842static void vga_switcheroo_power_switch(struct pci_dev *pdev,
843 enum vga_switcheroo_state state)
Dave Airlie0d697042012-09-10 12:28:36 +1000844{
845 struct vga_switcheroo_client *client;
846
847 if (!vgasr_priv.handler->power_state)
848 return;
849
850 client = find_client_from_pci(&vgasr_priv.clients, pdev);
851 if (!client)
852 return;
853
854 if (!client->driver_power_control)
855 return;
856
857 vgasr_priv.handler->power_state(client->id, state);
858}
859
Lukas Wunnera6456542015-08-23 15:18:55 +0200860/**
861 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
862 * @pdev: client pci device
863 * @dynamic: new power state
864 *
865 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
866 * When the driver decides to power up or down, it notifies vga_switcheroo
867 * thereof using this helper so that it can (a) power the audio device on
868 * the GPU up or down, and (b) update its internal power state representation
869 * for the device.
870 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200871void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
872 enum vga_switcheroo_state dynamic)
Dave Airlie0d697042012-09-10 12:28:36 +1000873{
874 struct vga_switcheroo_client *client;
875
Lukas Wunner8f12a312015-08-23 23:23:02 +0200876 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000877 client = find_client_from_pci(&vgasr_priv.clients, pdev);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200878 if (!client || !client->driver_power_control) {
879 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000880 return;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200881 }
Dave Airlie0d697042012-09-10 12:28:36 +1000882
883 client->pwr_state = dynamic;
884 set_audio_state(client->id, dynamic);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200885 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000886}
887EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
888
889/* switcheroo power domain */
890static int vga_switcheroo_runtime_suspend(struct device *dev)
891{
892 struct pci_dev *pdev = to_pci_dev(dev);
893 int ret;
894
895 ret = dev->bus->pm->runtime_suspend(dev);
896 if (ret)
897 return ret;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200898 mutex_lock(&vgasr_mutex);
Alex Deucherf2bc561612014-05-19 14:04:43 -0400899 if (vgasr_priv.handler->switchto)
900 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
Dave Airlie0d697042012-09-10 12:28:36 +1000901 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200902 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000903 return 0;
904}
905
906static int vga_switcheroo_runtime_resume(struct device *dev)
907{
908 struct pci_dev *pdev = to_pci_dev(dev);
909 int ret;
910
Lukas Wunner8f12a312015-08-23 23:23:02 +0200911 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000912 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200913 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000914 ret = dev->bus->pm->runtime_resume(dev);
915 if (ret)
916 return ret;
917
918 return 0;
919}
920
Lukas Wunnera6456542015-08-23 15:18:55 +0200921/**
922 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
923 * @dev: vga client device
924 * @domain: power domain
925 *
926 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
927 * After the GPU has been suspended, the handler needs to be called to cut
928 * power to the GPU. Likewise it needs to reinstate power before the GPU
929 * can resume. To this end, this helper augments the suspend/resume functions
930 * by the requisite calls to the handler. It needs only be called on platforms
931 * where the power switch is separate to the device being powered down.
932 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200933int vga_switcheroo_init_domain_pm_ops(struct device *dev,
934 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000935{
936 /* copy over all the bus versions */
937 if (dev->bus && dev->bus->pm) {
938 domain->ops = *dev->bus->pm;
939 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
940 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
941
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100942 dev_pm_domain_set(dev, domain);
Dave Airlie0d697042012-09-10 12:28:36 +1000943 return 0;
944 }
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100945 dev_pm_domain_set(dev, NULL);
Dave Airlie0d697042012-09-10 12:28:36 +1000946 return -EINVAL;
947}
948EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
949
Alex Deucher766a53d2014-09-12 17:51:29 -0400950void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
951{
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100952 dev_pm_domain_set(dev, NULL);
Alex Deucher766a53d2014-09-12 17:51:29 -0400953}
954EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
955
Dave Airlie0d697042012-09-10 12:28:36 +1000956static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
957{
958 struct pci_dev *pdev = to_pci_dev(dev);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200959 struct vga_switcheroo_client *client;
960 struct device *video_dev = NULL;
Dave Airlie0d697042012-09-10 12:28:36 +1000961 int ret;
Dave Airlie0d697042012-09-10 12:28:36 +1000962
963 /* we need to check if we have to switch back on the video
964 device so the audio device can come back */
Lukas Wunner8f12a312015-08-23 23:23:02 +0200965 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000966 list_for_each_entry(client, &vgasr_priv.clients, list) {
Thierry Reding7491bfb2015-08-12 16:32:11 +0200967 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
968 client_is_vga(client)) {
Lukas Wunner8f12a312015-08-23 23:23:02 +0200969 video_dev = &client->pdev->dev;
Dave Airlie0d697042012-09-10 12:28:36 +1000970 break;
971 }
972 }
Lukas Wunner8f12a312015-08-23 23:23:02 +0200973 mutex_unlock(&vgasr_mutex);
974
975 if (video_dev) {
976 ret = pm_runtime_get_sync(video_dev);
977 if (ret && ret != 1)
978 return ret;
979 }
Dave Airlie0d697042012-09-10 12:28:36 +1000980 ret = dev->bus->pm->runtime_resume(dev);
981
982 /* put the reference for the gpu */
Lukas Wunner8f12a312015-08-23 23:23:02 +0200983 if (video_dev) {
984 pm_runtime_mark_last_busy(video_dev);
985 pm_runtime_put_autosuspend(video_dev);
Dave Airlie0d697042012-09-10 12:28:36 +1000986 }
987 return ret;
988}
989
Lukas Wunnera6456542015-08-23 15:18:55 +0200990/**
991 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
992 * power control
993 * @dev: audio client device
994 * @domain: power domain
995 *
996 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
997 * When the audio device resumes, the GPU needs to be woken. This helper
998 * augments the audio device's resume function to do that.
999 *
1000 * Return: 0 on success, -EINVAL if no power management operations are
1001 * defined for this device.
1002 */
Thierry Reding7491bfb2015-08-12 16:32:11 +02001003int
1004vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
1005 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +10001006{
1007 /* copy over all the bus versions */
1008 if (dev->bus && dev->bus->pm) {
1009 domain->ops = *dev->bus->pm;
Thierry Reding7491bfb2015-08-12 16:32:11 +02001010 domain->ops.runtime_resume =
1011 vga_switcheroo_runtime_resume_hdmi_audio;
Dave Airlie0d697042012-09-10 12:28:36 +10001012
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001013 dev_pm_domain_set(dev, domain);
Dave Airlie0d697042012-09-10 12:28:36 +10001014 return 0;
1015 }
Tomeu Vizoso989561d2016-01-07 16:46:13 +01001016 dev_pm_domain_set(dev, NULL);
Dave Airlie0d697042012-09-10 12:28:36 +10001017 return -EINVAL;
1018}
1019EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);