blob: 665ab9fd0e011233b2105035ce20cdb9e4ee7c60 [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
129 *
130 * vga_switcheroo private data. Currently only one vga_switcheroo instance
131 * per system is supported.
132 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000133struct vgasr_priv {
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000134 bool active;
135 bool delayed_switch_active;
136 enum vga_switcheroo_client_id delayed_client_id;
137
138 struct dentry *debugfs_root;
139 struct dentry *switch_file;
140
141 int registered_clients;
Takashi Iwai79721e02012-04-26 12:55:59 +0200142 struct list_head clients;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000143
Lukas Wunner5d170132015-10-18 13:05:40 +0200144 const struct vga_switcheroo_handler *handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000145};
146
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200147#define ID_BIT_AUDIO 0x100
148#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200149#define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
150 !client_is_audio(c))
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200151#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
152
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000153static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
154static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
155
156/* only one switcheroo per system */
Takashi Iwai79721e02012-04-26 12:55:59 +0200157static struct vgasr_priv vgasr_priv = {
158 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
159};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000160
Seth Forshee36704c02012-08-17 11:17:03 -0500161static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000162{
Seth Forshee36704c02012-08-17 11:17:03 -0500163 /* we're ready if we get two clients + handler */
164 return !vgasr_priv.active &&
165 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000166}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000167
168static void vga_switcheroo_enable(void)
169{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000170 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200171 struct vga_switcheroo_client *client;
172
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000173 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -0500174 if (vgasr_priv.handler->init)
175 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000176
Takashi Iwai79721e02012-04-26 12:55:59 +0200177 list_for_each_entry(client, &vgasr_priv.clients, list) {
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200178 if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200179 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200180 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000181 if (ret < 0)
182 return;
183
Takashi Iwai79721e02012-04-26 12:55:59 +0200184 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000185 }
186 vga_switcheroo_debugfs_init(&vgasr_priv);
187 vgasr_priv.active = true;
188}
189
Lukas Wunnera6456542015-08-23 15:18:55 +0200190/**
191 * vga_switcheroo_register_handler() - register handler
192 * @handler: handler callbacks
193 *
194 * Register handler. Enable vga_switcheroo if two vga clients have already
195 * registered.
196 *
197 * Return: 0 on success, -EINVAL if a handler was already registered.
198 */
Lukas Wunner5d170132015-10-18 13:05:40 +0200199int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler)
Seth Forshee36704c02012-08-17 11:17:03 -0500200{
201 mutex_lock(&vgasr_mutex);
202 if (vgasr_priv.handler) {
203 mutex_unlock(&vgasr_mutex);
204 return -EINVAL;
205 }
206
207 vgasr_priv.handler = handler;
208 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200209 pr_info("enabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500210 vga_switcheroo_enable();
211 }
212 mutex_unlock(&vgasr_mutex);
213 return 0;
214}
215EXPORT_SYMBOL(vga_switcheroo_register_handler);
216
Lukas Wunnera6456542015-08-23 15:18:55 +0200217/**
218 * vga_switcheroo_unregister_handler() - unregister handler
219 *
220 * Unregister handler. Disable vga_switcheroo.
221 */
Seth Forshee36704c02012-08-17 11:17:03 -0500222void vga_switcheroo_unregister_handler(void)
223{
224 mutex_lock(&vgasr_mutex);
225 vgasr_priv.handler = NULL;
226 if (vgasr_priv.active) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200227 pr_info("disabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500228 vga_switcheroo_debugfs_fini(&vgasr_priv);
229 vgasr_priv.active = false;
230 }
231 mutex_unlock(&vgasr_mutex);
232}
233EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
234
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200235static int register_client(struct pci_dev *pdev,
236 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200237 enum vga_switcheroo_client_id id, bool active,
238 bool driver_power_control)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000239{
Takashi Iwai79721e02012-04-26 12:55:59 +0200240 struct vga_switcheroo_client *client;
241
242 client = kzalloc(sizeof(*client), GFP_KERNEL);
243 if (!client)
244 return -ENOMEM;
245
246 client->pwr_state = VGA_SWITCHEROO_ON;
247 client->pdev = pdev;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200248 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200249 client->id = id;
250 client->active = active;
Dave Airlie0d697042012-09-10 12:28:36 +1000251 client->driver_power_control = driver_power_control;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000252
253 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200254 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200255 if (client_is_vga(client))
256 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000257
Seth Forshee36704c02012-08-17 11:17:03 -0500258 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200259 pr_info("enabled\n");
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000260 vga_switcheroo_enable();
261 }
262 mutex_unlock(&vgasr_mutex);
263 return 0;
264}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200265
Lukas Wunnera6456542015-08-23 15:18:55 +0200266/**
267 * vga_switcheroo_register_client - register vga client
268 * @pdev: client pci device
269 * @ops: client callbacks
270 * @driver_power_control: whether power state is controlled by the driver's
271 * runtime pm
272 *
273 * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
274 * handler have already registered. The power state of the client is assumed
275 * to be ON.
276 *
277 * Return: 0 on success, -ENOMEM on memory allocation error.
278 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200279int vga_switcheroo_register_client(struct pci_dev *pdev,
Dave Airlie0d697042012-09-10 12:28:36 +1000280 const struct vga_switcheroo_client_ops *ops,
281 bool driver_power_control)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200282{
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200283 return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200284 pdev == vga_default_device(),
285 driver_power_control);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200286}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000287EXPORT_SYMBOL(vga_switcheroo_register_client);
288
Lukas Wunnera6456542015-08-23 15:18:55 +0200289/**
290 * vga_switcheroo_register_audio_client - register audio client
291 * @pdev: client pci device
292 * @ops: client callbacks
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200293 * @id: client identifier
Lukas Wunnera6456542015-08-23 15:18:55 +0200294 *
295 * Register audio client (audio device on a GPU). The power state of the
296 * client is assumed to be ON.
297 *
298 * Return: 0 on success, -ENOMEM on memory allocation error.
299 */
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200300int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
301 const struct vga_switcheroo_client_ops *ops,
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200302 enum vga_switcheroo_client_id id)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200303{
Lukas Wunner21b45672015-08-27 16:43:43 +0200304 return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200305}
306EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
307
Takashi Iwai79721e02012-04-26 12:55:59 +0200308static struct vga_switcheroo_client *
309find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
310{
311 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200312
Takashi Iwai79721e02012-04-26 12:55:59 +0200313 list_for_each_entry(client, head, list)
314 if (client->pdev == pdev)
315 return client;
316 return NULL;
317}
318
319static struct vga_switcheroo_client *
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200320find_client_from_id(struct list_head *head,
321 enum vga_switcheroo_client_id client_id)
Takashi Iwai79721e02012-04-26 12:55:59 +0200322{
323 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200324
Takashi Iwai79721e02012-04-26 12:55:59 +0200325 list_for_each_entry(client, head, list)
326 if (client->id == client_id)
327 return client;
328 return NULL;
329}
330
331static struct vga_switcheroo_client *
332find_active_client(struct list_head *head)
333{
334 struct vga_switcheroo_client *client;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200335
Takashi Iwai79721e02012-04-26 12:55:59 +0200336 list_for_each_entry(client, head, list)
Lukas Wunner21b45672015-08-27 16:43:43 +0200337 if (client->active)
Takashi Iwai79721e02012-04-26 12:55:59 +0200338 return client;
339 return NULL;
340}
341
Lukas Wunnera6456542015-08-23 15:18:55 +0200342/**
343 * vga_switcheroo_get_client_state() - obtain power state of a given client
344 * @pdev: client pci device
345 *
346 * Obtain power state of a given client as seen from vga_switcheroo.
347 * The function is only called from hda_intel.c.
348 *
349 * Return: Power state.
350 */
Lukas Wunner203d0272015-08-28 11:56:26 +0200351enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200352{
353 struct vga_switcheroo_client *client;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200354 enum vga_switcheroo_state ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200355
Lukas Wunner8f12a312015-08-23 23:23:02 +0200356 mutex_lock(&vgasr_mutex);
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200357 client = find_client_from_pci(&vgasr_priv.clients, pdev);
358 if (!client)
Lukas Wunner8f12a312015-08-23 23:23:02 +0200359 ret = VGA_SWITCHEROO_NOT_FOUND;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200360 else
361 ret = client->pwr_state;
362 mutex_unlock(&vgasr_mutex);
363 return ret;
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200364}
365EXPORT_SYMBOL(vga_switcheroo_get_client_state);
366
Lukas Wunnera6456542015-08-23 15:18:55 +0200367/**
368 * vga_switcheroo_unregister_client() - unregister client
369 * @pdev: client pci device
370 *
371 * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
372 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000373void vga_switcheroo_unregister_client(struct pci_dev *pdev)
374{
Takashi Iwai79721e02012-04-26 12:55:59 +0200375 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000376
377 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200378 client = find_client_from_pci(&vgasr_priv.clients, pdev);
379 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200380 if (client_is_vga(client))
381 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200382 list_del(&client->list);
383 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000384 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200385 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200386 pr_info("disabled\n");
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200387 vga_switcheroo_debugfs_fini(&vgasr_priv);
388 vgasr_priv.active = false;
389 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000390 mutex_unlock(&vgasr_mutex);
391}
392EXPORT_SYMBOL(vga_switcheroo_unregister_client);
393
Lukas Wunnera6456542015-08-23 15:18:55 +0200394/**
395 * vga_switcheroo_client_fb_set() - set framebuffer of a given client
396 * @pdev: client pci device
397 * @info: framebuffer
398 *
399 * Set framebuffer of a given client. The console will be remapped to this
400 * on switching.
401 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000402void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
403 struct fb_info *info)
404{
Takashi Iwai79721e02012-04-26 12:55:59 +0200405 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000406
407 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200408 client = find_client_from_pci(&vgasr_priv.clients, pdev);
409 if (client)
410 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000411 mutex_unlock(&vgasr_mutex);
412}
413EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
414
Lukas Wunnera6456542015-08-23 15:18:55 +0200415/**
416 * DOC: Manual switching and manual power control
417 *
418 * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
419 * can be read to retrieve the current vga_switcheroo state and commands
420 * can be written to it to change the state. The file appears as soon as
421 * two GPU drivers and one handler have registered with vga_switcheroo.
422 * The following commands are understood:
423 *
424 * * OFF: Power off the device not in use.
425 * * ON: Power on the device not in use.
426 * * IGD: Switch to the integrated graphics device.
427 * Power on the integrated GPU if necessary, power off the discrete GPU.
428 * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
429 * have opened device files of the GPUs or the audio client. If the
430 * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
431 * and /dev/snd/controlC1 to identify processes blocking the switch.
432 * * DIS: Switch to the discrete graphics device.
433 * * DIGD: Delayed switch to the integrated graphics device.
434 * This will perform the switch once the last user space process has
435 * closed the device files of the GPUs and the audio client.
436 * * DDIS: Delayed switch to the discrete graphics device.
437 * * MIGD: Mux-only switch to the integrated graphics device.
438 * Does not remap console or change the power state of either gpu.
439 * If the integrated GPU is currently off, the screen will turn black.
440 * If it is on, the screen will show whatever happens to be in VRAM.
441 * Either way, the user has to blindly enter the command to switch back.
442 * * MDIS: Mux-only switch to the discrete graphics device.
443 *
444 * For GPUs whose power state is controlled by the driver's runtime pm,
445 * the ON and OFF commands are a no-op (see next section).
446 *
447 * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
448 * should not be used.
449 */
450
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000451static int vga_switcheroo_show(struct seq_file *m, void *v)
452{
Takashi Iwai79721e02012-04-26 12:55:59 +0200453 struct vga_switcheroo_client *client;
454 int i = 0;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200455
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000456 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200457 list_for_each_entry(client, &vgasr_priv.clients, list) {
Dave Airlie0d697042012-09-10 12:28:36 +1000458 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200459 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
460 "IGD",
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200461 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200462 client->active ? '+' : ' ',
Dave Airlie0d697042012-09-10 12:28:36 +1000463 client->driver_power_control ? "Dyn" : "",
Takashi Iwai79721e02012-04-26 12:55:59 +0200464 client->pwr_state ? "Pwr" : "Off",
465 pci_name(client->pdev));
466 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000467 }
468 mutex_unlock(&vgasr_mutex);
469 return 0;
470}
471
472static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
473{
474 return single_open(file, vga_switcheroo_show, NULL);
475}
476
477static int vga_switchon(struct vga_switcheroo_client *client)
478{
Dave Airlie0d697042012-09-10 12:28:36 +1000479 if (client->driver_power_control)
480 return 0;
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000481 if (vgasr_priv.handler->power_state)
482 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000483 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200484 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000485 client->pwr_state = VGA_SWITCHEROO_ON;
486 return 0;
487}
488
489static int vga_switchoff(struct vga_switcheroo_client *client)
490{
Dave Airlie0d697042012-09-10 12:28:36 +1000491 if (client->driver_power_control)
492 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000493 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200494 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000495 if (vgasr_priv.handler->power_state)
496 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000497 client->pwr_state = VGA_SWITCHEROO_OFF;
498 return 0;
499}
500
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200501static void set_audio_state(enum vga_switcheroo_client_id id,
502 enum vga_switcheroo_state state)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200503{
504 struct vga_switcheroo_client *client;
505
506 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
507 if (client && client->pwr_state != state) {
508 client->ops->set_gpu_state(client->pdev, state);
509 client->pwr_state = state;
510 }
511}
512
Dave Airlie66b37c62010-12-07 14:24:25 +1000513/* stage one happens before delay */
514static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000515{
Takashi Iwai79721e02012-04-26 12:55:59 +0200516 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000517
Takashi Iwai79721e02012-04-26 12:55:59 +0200518 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000519 if (!active)
520 return 0;
521
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000522 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
523 vga_switchon(new_client);
524
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400525 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000526 return 0;
527}
528
529/* post delay */
530static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
531{
532 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200533 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000534
Takashi Iwai79721e02012-04-26 12:55:59 +0200535 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000536 if (!active)
537 return 0;
538
539 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000540
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200541 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
542
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000543 if (new_client->fb_info) {
544 struct fb_event event;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200545
Dave Airlie054430e2013-01-25 11:38:56 +1000546 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000547 event.info = new_client->fb_info;
548 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie054430e2013-01-25 11:38:56 +1000549 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000550 }
551
552 ret = vgasr_priv.handler->switchto(new_client->id);
553 if (ret)
554 return ret;
555
Takashi Iwai26ec6852012-05-11 07:51:17 +0200556 if (new_client->ops->reprobe)
557 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000558
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000559 if (active->pwr_state == VGA_SWITCHEROO_ON)
560 vga_switchoff(active);
561
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200562 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
563
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000564 new_client->active = true;
565 return 0;
566}
567
Takashi Iwai79721e02012-04-26 12:55:59 +0200568static bool check_can_switch(void)
569{
570 struct vga_switcheroo_client *client;
571
572 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200573 if (!client->ops->can_switch(client->pdev)) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200574 pr_err("client %x refused switch\n", client->id);
Takashi Iwai79721e02012-04-26 12:55:59 +0200575 return false;
576 }
577 }
578 return true;
579}
580
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000581static ssize_t
582vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
583 size_t cnt, loff_t *ppos)
584{
585 char usercmd[64];
Takashi Iwai79721e02012-04-26 12:55:59 +0200586 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000587 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000588 bool just_mux = false;
Lukas Wunnerfa3e9672015-08-28 12:54:07 +0200589 enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000590 struct vga_switcheroo_client *client = NULL;
591
592 if (cnt > 63)
593 cnt = 63;
594
595 if (copy_from_user(usercmd, ubuf, cnt))
596 return -EFAULT;
597
598 mutex_lock(&vgasr_mutex);
599
Jiri Slaby8c88e502010-04-27 14:11:03 -0700600 if (!vgasr_priv.active) {
601 cnt = -EINVAL;
602 goto out;
603 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000604
605 /* pwr off the device not in use */
606 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200607 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200608 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000609 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000610 if (client->driver_power_control)
611 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200612 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200613 if (client->pwr_state == VGA_SWITCHEROO_ON)
614 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000615 }
616 goto out;
617 }
618 /* pwr on the device not in use */
619 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200620 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200621 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000622 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000623 if (client->driver_power_control)
624 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200625 if (client->pwr_state == VGA_SWITCHEROO_OFF)
626 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200627 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000628 }
629 goto out;
630 }
631
632 /* request a delayed switch - test can we switch now */
633 if (strncmp(usercmd, "DIGD", 4) == 0) {
634 client_id = VGA_SWITCHEROO_IGD;
635 delay = true;
636 }
637
638 if (strncmp(usercmd, "DDIS", 4) == 0) {
639 client_id = VGA_SWITCHEROO_DIS;
640 delay = true;
641 }
642
643 if (strncmp(usercmd, "IGD", 3) == 0)
644 client_id = VGA_SWITCHEROO_IGD;
645
646 if (strncmp(usercmd, "DIS", 3) == 0)
647 client_id = VGA_SWITCHEROO_DIS;
648
Dan Carpenterfea6f332011-01-07 08:12:27 +0300649 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000650 just_mux = true;
651 client_id = VGA_SWITCHEROO_IGD;
652 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300653 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000654 just_mux = true;
655 client_id = VGA_SWITCHEROO_DIS;
656 }
657
Lukas Wunner21c5ba82015-08-28 13:30:32 +0200658 if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000659 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200660 client = find_client_from_id(&vgasr_priv.clients, client_id);
661 if (!client)
662 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000663
664 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000665
666 if (just_mux) {
667 ret = vgasr_priv.handler->switchto(client_id);
668 goto out;
669 }
670
Takashi Iwai79721e02012-04-26 12:55:59 +0200671 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200672 goto out;
673
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000674 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200675 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000676
677 if (can_switch == false && delay == false)
678 goto out;
679
Takashi Iwai79721e02012-04-26 12:55:59 +0200680 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000681 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000682 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200683 pr_err("switching failed stage 1 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000684
685 ret = vga_switchto_stage2(client);
686 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200687 pr_err("switching failed stage 2 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000688
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000689 } else {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200690 pr_info("setting delayed switch to client %d\n", client->id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000691 vgasr_priv.delayed_switch_active = true;
692 vgasr_priv.delayed_client_id = client_id;
693
Dave Airlie66b37c62010-12-07 14:24:25 +1000694 ret = vga_switchto_stage1(client);
695 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200696 pr_err("delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000697 }
698
699out:
700 mutex_unlock(&vgasr_mutex);
701 return cnt;
702}
703
704static const struct file_operations vga_switcheroo_debugfs_fops = {
705 .owner = THIS_MODULE,
706 .open = vga_switcheroo_debugfs_open,
707 .write = vga_switcheroo_debugfs_write,
708 .read = seq_read,
709 .llseek = seq_lseek,
710 .release = single_release,
711};
712
713static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
714{
Thierry Reding798ae0f2015-08-12 16:32:12 +0200715 debugfs_remove(priv->switch_file);
716 priv->switch_file = NULL;
717
718 debugfs_remove(priv->debugfs_root);
719 priv->debugfs_root = NULL;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000720}
721
722static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
723{
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200724 static const char mp[] = "/sys/kernel/debug";
725
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000726 /* already initialised */
727 if (priv->debugfs_root)
728 return 0;
729 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
730
731 if (!priv->debugfs_root) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200732 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000733 goto fail;
734 }
735
736 priv->switch_file = debugfs_create_file("switch", 0644,
Thierry Reding7491bfb2015-08-12 16:32:11 +0200737 priv->debugfs_root, NULL,
738 &vga_switcheroo_debugfs_fops);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000739 if (!priv->switch_file) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200740 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000741 goto fail;
742 }
743 return 0;
744fail:
745 vga_switcheroo_debugfs_fini(priv);
746 return -1;
747}
748
Lukas Wunnera6456542015-08-23 15:18:55 +0200749/**
750 * vga_switcheroo_process_delayed_switch() - helper for delayed switching
751 *
752 * Process a delayed switch if one is pending. DRM drivers should call this
753 * from their ->lastclose callback.
754 *
755 * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
756 * has unregistered in the meantime or if there are other clients blocking the
757 * switch. If the actual switch fails, an error is reported and 0 is returned.
758 */
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000759int vga_switcheroo_process_delayed_switch(void)
760{
Takashi Iwai79721e02012-04-26 12:55:59 +0200761 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000762 int ret;
763 int err = -EINVAL;
764
765 mutex_lock(&vgasr_mutex);
766 if (!vgasr_priv.delayed_switch_active)
767 goto err;
768
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200769 pr_info("processing delayed switch to %d\n",
770 vgasr_priv.delayed_client_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000771
Takashi Iwai79721e02012-04-26 12:55:59 +0200772 client = find_client_from_id(&vgasr_priv.clients,
773 vgasr_priv.delayed_client_id);
774 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000775 goto err;
776
Dave Airlie66b37c62010-12-07 14:24:25 +1000777 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000778 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200779 pr_err("delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000780
781 vgasr_priv.delayed_switch_active = false;
782 err = 0;
783err:
784 mutex_unlock(&vgasr_mutex);
785 return err;
786}
787EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
Dave Airlie0d697042012-09-10 12:28:36 +1000788
Lukas Wunnera6456542015-08-23 15:18:55 +0200789/**
790 * DOC: Driver power control
791 *
792 * In this mode of use, the discrete GPU automatically powers up and down at
793 * the discretion of the driver's runtime pm. On muxed machines, the user may
794 * still influence the muxer state by way of the debugfs interface, however
795 * the ON and OFF commands become a no-op for the discrete GPU.
796 *
797 * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
798 * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
799 * command line disables it.
800 *
801 * When the driver decides to power up or down, it notifies vga_switcheroo
802 * thereof so that it can (a) power the audio device on the GPU up or down,
803 * and (b) update its internal power state representation for the device.
804 * This is achieved by vga_switcheroo_set_dynamic_switch().
805 *
806 * After the GPU has been suspended, the handler needs to be called to cut
807 * power to the GPU. Likewise it needs to reinstate power before the GPU
808 * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
809 * which augments the GPU's suspend/resume functions by the requisite
810 * calls to the handler.
811 *
812 * When the audio device resumes, the GPU needs to be woken. This is achieved
813 * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
814 * audio device's resume function.
815 *
816 * On muxed machines, if the mux is initially switched to the discrete GPU,
817 * the user ends up with a black screen when the GPU powers down after boot.
818 * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
819 * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
820 */
821
Thierry Reding7491bfb2015-08-12 16:32:11 +0200822static void vga_switcheroo_power_switch(struct pci_dev *pdev,
823 enum vga_switcheroo_state state)
Dave Airlie0d697042012-09-10 12:28:36 +1000824{
825 struct vga_switcheroo_client *client;
826
827 if (!vgasr_priv.handler->power_state)
828 return;
829
830 client = find_client_from_pci(&vgasr_priv.clients, pdev);
831 if (!client)
832 return;
833
834 if (!client->driver_power_control)
835 return;
836
837 vgasr_priv.handler->power_state(client->id, state);
838}
839
Lukas Wunnera6456542015-08-23 15:18:55 +0200840/**
841 * vga_switcheroo_set_dynamic_switch() - helper for driver power control
842 * @pdev: client pci device
843 * @dynamic: new power state
844 *
845 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
846 * When the driver decides to power up or down, it notifies vga_switcheroo
847 * thereof using this helper so that it can (a) power the audio device on
848 * the GPU up or down, and (b) update its internal power state representation
849 * for the device.
850 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200851void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
852 enum vga_switcheroo_state dynamic)
Dave Airlie0d697042012-09-10 12:28:36 +1000853{
854 struct vga_switcheroo_client *client;
855
Lukas Wunner8f12a312015-08-23 23:23:02 +0200856 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000857 client = find_client_from_pci(&vgasr_priv.clients, pdev);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200858 if (!client || !client->driver_power_control) {
859 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000860 return;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200861 }
Dave Airlie0d697042012-09-10 12:28:36 +1000862
863 client->pwr_state = dynamic;
864 set_audio_state(client->id, dynamic);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200865 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000866}
867EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
868
869/* switcheroo power domain */
870static int vga_switcheroo_runtime_suspend(struct device *dev)
871{
872 struct pci_dev *pdev = to_pci_dev(dev);
873 int ret;
874
875 ret = dev->bus->pm->runtime_suspend(dev);
876 if (ret)
877 return ret;
Lukas Wunner8f12a312015-08-23 23:23:02 +0200878 mutex_lock(&vgasr_mutex);
Alex Deucherf2bc561612014-05-19 14:04:43 -0400879 if (vgasr_priv.handler->switchto)
880 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
Dave Airlie0d697042012-09-10 12:28:36 +1000881 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200882 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000883 return 0;
884}
885
886static int vga_switcheroo_runtime_resume(struct device *dev)
887{
888 struct pci_dev *pdev = to_pci_dev(dev);
889 int ret;
890
Lukas Wunner8f12a312015-08-23 23:23:02 +0200891 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000892 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200893 mutex_unlock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000894 ret = dev->bus->pm->runtime_resume(dev);
895 if (ret)
896 return ret;
897
898 return 0;
899}
900
Lukas Wunnera6456542015-08-23 15:18:55 +0200901/**
902 * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
903 * @dev: vga client device
904 * @domain: power domain
905 *
906 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
907 * After the GPU has been suspended, the handler needs to be called to cut
908 * power to the GPU. Likewise it needs to reinstate power before the GPU
909 * can resume. To this end, this helper augments the suspend/resume functions
910 * by the requisite calls to the handler. It needs only be called on platforms
911 * where the power switch is separate to the device being powered down.
912 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200913int vga_switcheroo_init_domain_pm_ops(struct device *dev,
914 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000915{
916 /* copy over all the bus versions */
917 if (dev->bus && dev->bus->pm) {
918 domain->ops = *dev->bus->pm;
919 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
920 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
921
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100922 dev_pm_domain_set(dev, domain);
Dave Airlie0d697042012-09-10 12:28:36 +1000923 return 0;
924 }
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100925 dev_pm_domain_set(dev, NULL);
Dave Airlie0d697042012-09-10 12:28:36 +1000926 return -EINVAL;
927}
928EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
929
Alex Deucher766a53d2014-09-12 17:51:29 -0400930void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
931{
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100932 dev_pm_domain_set(dev, NULL);
Alex Deucher766a53d2014-09-12 17:51:29 -0400933}
934EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
935
Dave Airlie0d697042012-09-10 12:28:36 +1000936static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
937{
938 struct pci_dev *pdev = to_pci_dev(dev);
Lukas Wunner8f12a312015-08-23 23:23:02 +0200939 struct vga_switcheroo_client *client;
940 struct device *video_dev = NULL;
Dave Airlie0d697042012-09-10 12:28:36 +1000941 int ret;
Dave Airlie0d697042012-09-10 12:28:36 +1000942
943 /* we need to check if we have to switch back on the video
944 device so the audio device can come back */
Lukas Wunner8f12a312015-08-23 23:23:02 +0200945 mutex_lock(&vgasr_mutex);
Dave Airlie0d697042012-09-10 12:28:36 +1000946 list_for_each_entry(client, &vgasr_priv.clients, list) {
Thierry Reding7491bfb2015-08-12 16:32:11 +0200947 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
948 client_is_vga(client)) {
Lukas Wunner8f12a312015-08-23 23:23:02 +0200949 video_dev = &client->pdev->dev;
Dave Airlie0d697042012-09-10 12:28:36 +1000950 break;
951 }
952 }
Lukas Wunner8f12a312015-08-23 23:23:02 +0200953 mutex_unlock(&vgasr_mutex);
954
955 if (video_dev) {
956 ret = pm_runtime_get_sync(video_dev);
957 if (ret && ret != 1)
958 return ret;
959 }
Dave Airlie0d697042012-09-10 12:28:36 +1000960 ret = dev->bus->pm->runtime_resume(dev);
961
962 /* put the reference for the gpu */
Lukas Wunner8f12a312015-08-23 23:23:02 +0200963 if (video_dev) {
964 pm_runtime_mark_last_busy(video_dev);
965 pm_runtime_put_autosuspend(video_dev);
Dave Airlie0d697042012-09-10 12:28:36 +1000966 }
967 return ret;
968}
969
Lukas Wunnera6456542015-08-23 15:18:55 +0200970/**
971 * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
972 * power control
973 * @dev: audio client device
974 * @domain: power domain
975 *
976 * Helper for GPUs whose power state is controlled by the driver's runtime pm.
977 * When the audio device resumes, the GPU needs to be woken. This helper
978 * augments the audio device's resume function to do that.
979 *
980 * Return: 0 on success, -EINVAL if no power management operations are
981 * defined for this device.
982 */
Thierry Reding7491bfb2015-08-12 16:32:11 +0200983int
984vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
985 struct dev_pm_domain *domain)
Dave Airlie0d697042012-09-10 12:28:36 +1000986{
987 /* copy over all the bus versions */
988 if (dev->bus && dev->bus->pm) {
989 domain->ops = *dev->bus->pm;
Thierry Reding7491bfb2015-08-12 16:32:11 +0200990 domain->ops.runtime_resume =
991 vga_switcheroo_runtime_resume_hdmi_audio;
Dave Airlie0d697042012-09-10 12:28:36 +1000992
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100993 dev_pm_domain_set(dev, domain);
Dave Airlie0d697042012-09-10 12:28:36 +1000994 return 0;
995 }
Tomeu Vizoso989561d2016-01-07 16:46:13 +0100996 dev_pm_domain_set(dev, NULL);
Dave Airlie0d697042012-09-10 12:28:36 +1000997 return -EINVAL;
998}
999EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);