Commit | Line | Data |
---|---|---|
6a9ee8af | 1 | /* |
a645654b LW |
2 | * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs |
3 | * | |
6a9ee8af DA |
4 | * Copyright (c) 2010 Red Hat Inc. |
5 | * Author : Dave Airlie <airlied@redhat.com> | |
6 | * | |
a645654b | 7 | * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de> |
6a9ee8af | 8 | * |
a645654b LW |
9 | * 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: | |
6a9ee8af | 15 | * |
a645654b LW |
16 | * 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. | |
71309278 | 19 | * |
a645654b LW |
20 | * 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. | |
71309278 | 28 | * |
6a9ee8af DA |
29 | */ |
30 | ||
9b0be1eb TR |
31 | #define pr_fmt(fmt) "vga_switcheroo: " fmt |
32 | ||
4127838c | 33 | #include <linux/console.h> |
6a9ee8af DA |
34 | #include <linux/debugfs.h> |
35 | #include <linux/fb.h> | |
4127838c LW |
36 | #include <linux/fs.h> |
37 | #include <linux/module.h> | |
6a9ee8af | 38 | #include <linux/pci.h> |
0d69704a | 39 | #include <linux/pm_runtime.h> |
4127838c LW |
40 | #include <linux/seq_file.h> |
41 | #include <linux/uaccess.h> | |
2fbe8c7c | 42 | #include <linux/vgaarb.h> |
4127838c | 43 | #include <linux/vga_switcheroo.h> |
2fbe8c7c | 44 | |
a645654b LW |
45 | /** |
46 | * DOC: Overview | |
47 | * | |
48 | * vga_switcheroo is the Linux subsystem for laptop hybrid graphics. | |
49 | * These come in two flavors: | |
50 | * | |
51 | * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs. | |
52 | * * muxless: Dual GPUs but only one of them is connected to outputs. | |
53 | * The other one is merely used to offload rendering, its results | |
54 | * are copied over PCIe into the framebuffer. On Linux this is | |
55 | * supported with DRI PRIME. | |
56 | * | |
57 | * Hybrid graphics started to appear in the late Naughties and were initially | |
58 | * all muxed. Newer laptops moved to a muxless architecture for cost reasons. | |
59 | * A notable exception is the MacBook Pro which continues to use a mux. | |
60 | * Muxes come with varying capabilities: Some switch only the panel, others | |
61 | * can also switch external displays. Some switch all display pins at once | |
62 | * while others can switch just the DDC lines. (To allow EDID probing | |
63 | * for the inactive GPU.) Also, muxes are often used to cut power to the | |
64 | * discrete GPU while it is not used. | |
65 | * | |
66 | * DRM drivers register GPUs with vga_switcheroo, these are heretoforth called | |
67 | * clients. The mux is called the handler. Muxless machines also register a | |
68 | * handler to control the power state of the discrete GPU, its ->switchto | |
69 | * callback is a no-op for obvious reasons. The discrete GPU is often equipped | |
70 | * with an HDA controller for the HDMI/DP audio signal, this will also | |
71 | * register as a client so that vga_switcheroo can take care of the correct | |
72 | * suspend/resume order when changing the discrete GPU's power state. In total | |
73 | * there can thus be up to three clients: Two vga clients (GPUs) and one audio | |
74 | * client (on the discrete GPU). The code is mostly prepared to support | |
75 | * machines with more than two GPUs should they become available. | |
76 | * The GPU to which the outputs are currently switched is called the | |
77 | * active client in vga_switcheroo parlance. The GPU not in use is the | |
78 | * inactive client. | |
79 | */ | |
80 | ||
81 | /** | |
82 | * struct vga_switcheroo_client - registered client | |
83 | * @pdev: client pci device | |
84 | * @fb_info: framebuffer to which console is remapped on switching | |
85 | * @pwr_state: current power state | |
86 | * @ops: client callbacks | |
87 | * @id: client identifier, see enum vga_switcheroo_client_id. | |
88 | * Determining the id requires the handler, so GPUs are initially | |
89 | * assigned -1 and later given their true id in vga_switcheroo_enable() | |
90 | * @active: whether the outputs are currently switched to this client | |
91 | * @driver_power_control: whether power state is controlled by the driver's | |
92 | * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs | |
93 | * interface is a no-op so as not to interfere with runtime pm | |
94 | * @list: client list | |
95 | * | |
96 | * Registered client. A client can be either a GPU or an audio device on a GPU. | |
97 | * For audio clients, the @fb_info, @active and @driver_power_control members | |
98 | * are bogus. | |
99 | */ | |
6a9ee8af DA |
100 | struct vga_switcheroo_client { |
101 | struct pci_dev *pdev; | |
102 | struct fb_info *fb_info; | |
103 | int pwr_state; | |
26ec685f | 104 | const struct vga_switcheroo_client_ops *ops; |
6a9ee8af DA |
105 | int id; |
106 | bool active; | |
0d69704a | 107 | bool driver_power_control; |
79721e0a | 108 | struct list_head list; |
6a9ee8af DA |
109 | }; |
110 | ||
a645654b LW |
111 | /* |
112 | * protects access to struct vgasr_priv | |
113 | */ | |
6a9ee8af DA |
114 | static DEFINE_MUTEX(vgasr_mutex); |
115 | ||
a645654b LW |
116 | /** |
117 | * struct vgasr_priv - vga_switcheroo private data | |
118 | * @active: whether vga_switcheroo is enabled. | |
119 | * Prerequisite is the registration of two GPUs and a handler | |
120 | * @delayed_switch_active: whether a delayed switch is pending | |
121 | * @delayed_client_id: client to which a delayed switch is pending | |
122 | * @debugfs_root: directory for vga_switcheroo debugfs interface | |
123 | * @switch_file: file for vga_switcheroo debugfs interface | |
124 | * @registered_clients: number of registered GPUs | |
125 | * (counting only vga clients, not audio clients) | |
126 | * @clients: list of registered clients | |
127 | * @handler: registered handler | |
128 | * | |
129 | * vga_switcheroo private data. Currently only one vga_switcheroo instance | |
130 | * per system is supported. | |
131 | */ | |
6a9ee8af | 132 | struct vgasr_priv { |
6a9ee8af DA |
133 | bool active; |
134 | bool delayed_switch_active; | |
135 | enum vga_switcheroo_client_id delayed_client_id; | |
136 | ||
137 | struct dentry *debugfs_root; | |
138 | struct dentry *switch_file; | |
139 | ||
140 | int registered_clients; | |
79721e0a | 141 | struct list_head clients; |
6a9ee8af DA |
142 | |
143 | struct vga_switcheroo_handler *handler; | |
144 | }; | |
145 | ||
3e9e63db TI |
146 | #define ID_BIT_AUDIO 0x100 |
147 | #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO) | |
148 | #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c)) | |
149 | #define client_id(c) ((c)->id & ~ID_BIT_AUDIO) | |
150 | ||
6a9ee8af DA |
151 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv); |
152 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv); | |
153 | ||
154 | /* only one switcheroo per system */ | |
79721e0a TI |
155 | static struct vgasr_priv vgasr_priv = { |
156 | .clients = LIST_HEAD_INIT(vgasr_priv.clients), | |
157 | }; | |
6a9ee8af | 158 | |
36704c0c | 159 | static bool vga_switcheroo_ready(void) |
6a9ee8af | 160 | { |
36704c0c SF |
161 | /* we're ready if we get two clients + handler */ |
162 | return !vgasr_priv.active && | |
163 | vgasr_priv.registered_clients == 2 && vgasr_priv.handler; | |
6a9ee8af | 164 | } |
6a9ee8af DA |
165 | |
166 | static void vga_switcheroo_enable(void) | |
167 | { | |
6a9ee8af | 168 | int ret; |
79721e0a TI |
169 | struct vga_switcheroo_client *client; |
170 | ||
6a9ee8af | 171 | /* call the handler to init */ |
e99eac5e SF |
172 | if (vgasr_priv.handler->init) |
173 | vgasr_priv.handler->init(); | |
6a9ee8af | 174 | |
79721e0a | 175 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
3e9e63db TI |
176 | if (client->id != -1) |
177 | continue; | |
79721e0a | 178 | ret = vgasr_priv.handler->get_client_id(client->pdev); |
6a9ee8af DA |
179 | if (ret < 0) |
180 | return; | |
181 | ||
79721e0a | 182 | client->id = ret; |
6a9ee8af DA |
183 | } |
184 | vga_switcheroo_debugfs_init(&vgasr_priv); | |
185 | vgasr_priv.active = true; | |
186 | } | |
187 | ||
a645654b LW |
188 | /** |
189 | * vga_switcheroo_register_handler() - register handler | |
190 | * @handler: handler callbacks | |
191 | * | |
192 | * Register handler. Enable vga_switcheroo if two vga clients have already | |
193 | * registered. | |
194 | * | |
195 | * Return: 0 on success, -EINVAL if a handler was already registered. | |
196 | */ | |
36704c0c SF |
197 | int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler) |
198 | { | |
199 | mutex_lock(&vgasr_mutex); | |
200 | if (vgasr_priv.handler) { | |
201 | mutex_unlock(&vgasr_mutex); | |
202 | return -EINVAL; | |
203 | } | |
204 | ||
205 | vgasr_priv.handler = handler; | |
206 | if (vga_switcheroo_ready()) { | |
9b0be1eb | 207 | pr_info("enabled\n"); |
36704c0c SF |
208 | vga_switcheroo_enable(); |
209 | } | |
210 | mutex_unlock(&vgasr_mutex); | |
211 | return 0; | |
212 | } | |
213 | EXPORT_SYMBOL(vga_switcheroo_register_handler); | |
214 | ||
a645654b LW |
215 | /** |
216 | * vga_switcheroo_unregister_handler() - unregister handler | |
217 | * | |
218 | * Unregister handler. Disable vga_switcheroo. | |
219 | */ | |
36704c0c SF |
220 | void vga_switcheroo_unregister_handler(void) |
221 | { | |
222 | mutex_lock(&vgasr_mutex); | |
223 | vgasr_priv.handler = NULL; | |
224 | if (vgasr_priv.active) { | |
9b0be1eb | 225 | pr_info("disabled\n"); |
36704c0c SF |
226 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
227 | vgasr_priv.active = false; | |
228 | } | |
229 | mutex_unlock(&vgasr_mutex); | |
230 | } | |
231 | EXPORT_SYMBOL(vga_switcheroo_unregister_handler); | |
232 | ||
3e9e63db TI |
233 | static int register_client(struct pci_dev *pdev, |
234 | const struct vga_switcheroo_client_ops *ops, | |
0d69704a | 235 | int id, bool active, bool driver_power_control) |
6a9ee8af | 236 | { |
79721e0a | 237 | struct vga_switcheroo_client *client; |
6a9ee8af | 238 | |
79721e0a TI |
239 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
240 | if (!client) | |
241 | return -ENOMEM; | |
242 | ||
243 | client->pwr_state = VGA_SWITCHEROO_ON; | |
244 | client->pdev = pdev; | |
26ec685f | 245 | client->ops = ops; |
3e9e63db TI |
246 | client->id = id; |
247 | client->active = active; | |
0d69704a | 248 | client->driver_power_control = driver_power_control; |
6a9ee8af | 249 | |
79721e0a TI |
250 | mutex_lock(&vgasr_mutex); |
251 | list_add_tail(&client->list, &vgasr_priv.clients); | |
3e9e63db TI |
252 | if (client_is_vga(client)) |
253 | vgasr_priv.registered_clients++; | |
6a9ee8af | 254 | |
36704c0c | 255 | if (vga_switcheroo_ready()) { |
9b0be1eb | 256 | pr_info("enabled\n"); |
6a9ee8af DA |
257 | vga_switcheroo_enable(); |
258 | } | |
259 | mutex_unlock(&vgasr_mutex); | |
260 | return 0; | |
261 | } | |
3e9e63db | 262 | |
a645654b LW |
263 | /** |
264 | * vga_switcheroo_register_client - register vga client | |
265 | * @pdev: client pci device | |
266 | * @ops: client callbacks | |
267 | * @driver_power_control: whether power state is controlled by the driver's | |
268 | * runtime pm | |
269 | * | |
270 | * Register vga client (GPU). Enable vga_switcheroo if another GPU and a | |
271 | * handler have already registered. The power state of the client is assumed | |
272 | * to be ON. | |
273 | * | |
274 | * Return: 0 on success, -ENOMEM on memory allocation error. | |
275 | */ | |
3e9e63db | 276 | int vga_switcheroo_register_client(struct pci_dev *pdev, |
0d69704a DA |
277 | const struct vga_switcheroo_client_ops *ops, |
278 | bool driver_power_control) | |
3e9e63db TI |
279 | { |
280 | return register_client(pdev, ops, -1, | |
7491bfb4 TR |
281 | pdev == vga_default_device(), |
282 | driver_power_control); | |
3e9e63db | 283 | } |
6a9ee8af DA |
284 | EXPORT_SYMBOL(vga_switcheroo_register_client); |
285 | ||
a645654b LW |
286 | /** |
287 | * vga_switcheroo_register_audio_client - register audio client | |
288 | * @pdev: client pci device | |
289 | * @ops: client callbacks | |
290 | * @id: client identifier, see enum vga_switcheroo_client_id | |
291 | * @active: whether the audio device is fully initialized | |
292 | * | |
293 | * Register audio client (audio device on a GPU). The power state of the | |
294 | * client is assumed to be ON. | |
295 | * | |
296 | * Return: 0 on success, -ENOMEM on memory allocation error. | |
297 | */ | |
3e9e63db TI |
298 | int vga_switcheroo_register_audio_client(struct pci_dev *pdev, |
299 | const struct vga_switcheroo_client_ops *ops, | |
300 | int id, bool active) | |
301 | { | |
0d69704a | 302 | return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false); |
3e9e63db TI |
303 | } |
304 | EXPORT_SYMBOL(vga_switcheroo_register_audio_client); | |
305 | ||
79721e0a TI |
306 | static struct vga_switcheroo_client * |
307 | find_client_from_pci(struct list_head *head, struct pci_dev *pdev) | |
308 | { | |
309 | struct vga_switcheroo_client *client; | |
7491bfb4 | 310 | |
79721e0a TI |
311 | list_for_each_entry(client, head, list) |
312 | if (client->pdev == pdev) | |
313 | return client; | |
314 | return NULL; | |
315 | } | |
316 | ||
317 | static struct vga_switcheroo_client * | |
318 | find_client_from_id(struct list_head *head, int client_id) | |
319 | { | |
320 | struct vga_switcheroo_client *client; | |
7491bfb4 | 321 | |
79721e0a TI |
322 | list_for_each_entry(client, head, list) |
323 | if (client->id == client_id) | |
324 | return client; | |
325 | return NULL; | |
326 | } | |
327 | ||
328 | static struct vga_switcheroo_client * | |
329 | find_active_client(struct list_head *head) | |
330 | { | |
331 | struct vga_switcheroo_client *client; | |
7491bfb4 | 332 | |
79721e0a | 333 | list_for_each_entry(client, head, list) |
3e9e63db | 334 | if (client->active && client_is_vga(client)) |
79721e0a TI |
335 | return client; |
336 | return NULL; | |
337 | } | |
338 | ||
a645654b LW |
339 | /** |
340 | * vga_switcheroo_get_client_state() - obtain power state of a given client | |
341 | * @pdev: client pci device | |
342 | * | |
343 | * Obtain power state of a given client as seen from vga_switcheroo. | |
344 | * The function is only called from hda_intel.c. | |
345 | * | |
346 | * Return: Power state. | |
347 | */ | |
c8e9cf7b TI |
348 | int vga_switcheroo_get_client_state(struct pci_dev *pdev) |
349 | { | |
350 | struct vga_switcheroo_client *client; | |
351 | ||
352 | client = find_client_from_pci(&vgasr_priv.clients, pdev); | |
353 | if (!client) | |
354 | return VGA_SWITCHEROO_NOT_FOUND; | |
355 | if (!vgasr_priv.active) | |
356 | return VGA_SWITCHEROO_INIT; | |
357 | return client->pwr_state; | |
358 | } | |
359 | EXPORT_SYMBOL(vga_switcheroo_get_client_state); | |
360 | ||
a645654b LW |
361 | /** |
362 | * vga_switcheroo_unregister_client() - unregister client | |
363 | * @pdev: client pci device | |
364 | * | |
365 | * Unregister client. Disable vga_switcheroo if this is a vga client (GPU). | |
366 | */ | |
6a9ee8af DA |
367 | void vga_switcheroo_unregister_client(struct pci_dev *pdev) |
368 | { | |
79721e0a | 369 | struct vga_switcheroo_client *client; |
6a9ee8af DA |
370 | |
371 | mutex_lock(&vgasr_mutex); | |
79721e0a TI |
372 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
373 | if (client) { | |
3e9e63db TI |
374 | if (client_is_vga(client)) |
375 | vgasr_priv.registered_clients--; | |
79721e0a TI |
376 | list_del(&client->list); |
377 | kfree(client); | |
6a9ee8af | 378 | } |
3e9e63db | 379 | if (vgasr_priv.active && vgasr_priv.registered_clients < 2) { |
9b0be1eb | 380 | pr_info("disabled\n"); |
3e9e63db TI |
381 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
382 | vgasr_priv.active = false; | |
383 | } | |
6a9ee8af DA |
384 | mutex_unlock(&vgasr_mutex); |
385 | } | |
386 | EXPORT_SYMBOL(vga_switcheroo_unregister_client); | |
387 | ||
a645654b LW |
388 | /** |
389 | * vga_switcheroo_client_fb_set() - set framebuffer of a given client | |
390 | * @pdev: client pci device | |
391 | * @info: framebuffer | |
392 | * | |
393 | * Set framebuffer of a given client. The console will be remapped to this | |
394 | * on switching. | |
395 | */ | |
6a9ee8af DA |
396 | void vga_switcheroo_client_fb_set(struct pci_dev *pdev, |
397 | struct fb_info *info) | |
398 | { | |
79721e0a | 399 | struct vga_switcheroo_client *client; |
6a9ee8af DA |
400 | |
401 | mutex_lock(&vgasr_mutex); | |
79721e0a TI |
402 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
403 | if (client) | |
404 | client->fb_info = info; | |
6a9ee8af DA |
405 | mutex_unlock(&vgasr_mutex); |
406 | } | |
407 | EXPORT_SYMBOL(vga_switcheroo_client_fb_set); | |
408 | ||
a645654b LW |
409 | /** |
410 | * DOC: Manual switching and manual power control | |
411 | * | |
412 | * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch | |
413 | * can be read to retrieve the current vga_switcheroo state and commands | |
414 | * can be written to it to change the state. The file appears as soon as | |
415 | * two GPU drivers and one handler have registered with vga_switcheroo. | |
416 | * The following commands are understood: | |
417 | * | |
418 | * * OFF: Power off the device not in use. | |
419 | * * ON: Power on the device not in use. | |
420 | * * IGD: Switch to the integrated graphics device. | |
421 | * Power on the integrated GPU if necessary, power off the discrete GPU. | |
422 | * Prerequisite is that no user space processes (e.g. Xorg, alsactl) | |
423 | * have opened device files of the GPUs or the audio client. If the | |
424 | * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/ | |
425 | * and /dev/snd/controlC1 to identify processes blocking the switch. | |
426 | * * DIS: Switch to the discrete graphics device. | |
427 | * * DIGD: Delayed switch to the integrated graphics device. | |
428 | * This will perform the switch once the last user space process has | |
429 | * closed the device files of the GPUs and the audio client. | |
430 | * * DDIS: Delayed switch to the discrete graphics device. | |
431 | * * MIGD: Mux-only switch to the integrated graphics device. | |
432 | * Does not remap console or change the power state of either gpu. | |
433 | * If the integrated GPU is currently off, the screen will turn black. | |
434 | * If it is on, the screen will show whatever happens to be in VRAM. | |
435 | * Either way, the user has to blindly enter the command to switch back. | |
436 | * * MDIS: Mux-only switch to the discrete graphics device. | |
437 | * | |
438 | * For GPUs whose power state is controlled by the driver's runtime pm, | |
439 | * the ON and OFF commands are a no-op (see next section). | |
440 | * | |
441 | * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands | |
442 | * should not be used. | |
443 | */ | |
444 | ||
6a9ee8af DA |
445 | static int vga_switcheroo_show(struct seq_file *m, void *v) |
446 | { | |
79721e0a TI |
447 | struct vga_switcheroo_client *client; |
448 | int i = 0; | |
7491bfb4 | 449 | |
6a9ee8af | 450 | mutex_lock(&vgasr_mutex); |
79721e0a | 451 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
0d69704a | 452 | seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i, |
7491bfb4 TR |
453 | client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : |
454 | "IGD", | |
3e9e63db | 455 | client_is_vga(client) ? "" : "-Audio", |
79721e0a | 456 | client->active ? '+' : ' ', |
0d69704a | 457 | client->driver_power_control ? "Dyn" : "", |
79721e0a TI |
458 | client->pwr_state ? "Pwr" : "Off", |
459 | pci_name(client->pdev)); | |
460 | i++; | |
6a9ee8af DA |
461 | } |
462 | mutex_unlock(&vgasr_mutex); | |
463 | return 0; | |
464 | } | |
465 | ||
466 | static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file) | |
467 | { | |
468 | return single_open(file, vga_switcheroo_show, NULL); | |
469 | } | |
470 | ||
471 | static int vga_switchon(struct vga_switcheroo_client *client) | |
472 | { | |
0d69704a DA |
473 | if (client->driver_power_control) |
474 | return 0; | |
5cfb3c3a DA |
475 | if (vgasr_priv.handler->power_state) |
476 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON); | |
6a9ee8af | 477 | /* call the driver callback to turn on device */ |
26ec685f | 478 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON); |
6a9ee8af DA |
479 | client->pwr_state = VGA_SWITCHEROO_ON; |
480 | return 0; | |
481 | } | |
482 | ||
483 | static int vga_switchoff(struct vga_switcheroo_client *client) | |
484 | { | |
0d69704a DA |
485 | if (client->driver_power_control) |
486 | return 0; | |
6a9ee8af | 487 | /* call the driver callback to turn off device */ |
26ec685f | 488 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF); |
5cfb3c3a DA |
489 | if (vgasr_priv.handler->power_state) |
490 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF); | |
6a9ee8af DA |
491 | client->pwr_state = VGA_SWITCHEROO_OFF; |
492 | return 0; | |
493 | } | |
494 | ||
3e9e63db TI |
495 | static void set_audio_state(int id, int state) |
496 | { | |
497 | struct vga_switcheroo_client *client; | |
498 | ||
499 | client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO); | |
500 | if (client && client->pwr_state != state) { | |
501 | client->ops->set_gpu_state(client->pdev, state); | |
502 | client->pwr_state = state; | |
503 | } | |
504 | } | |
505 | ||
66b37c67 DA |
506 | /* stage one happens before delay */ |
507 | static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) | |
6a9ee8af | 508 | { |
79721e0a | 509 | struct vga_switcheroo_client *active; |
6a9ee8af | 510 | |
79721e0a | 511 | active = find_active_client(&vgasr_priv.clients); |
6a9ee8af DA |
512 | if (!active) |
513 | return 0; | |
514 | ||
6a9ee8af DA |
515 | if (new_client->pwr_state == VGA_SWITCHEROO_OFF) |
516 | vga_switchon(new_client); | |
517 | ||
2fbe8c7c | 518 | vga_set_default_device(new_client->pdev); |
66b37c67 DA |
519 | return 0; |
520 | } | |
521 | ||
522 | /* post delay */ | |
523 | static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) | |
524 | { | |
525 | int ret; | |
79721e0a | 526 | struct vga_switcheroo_client *active; |
66b37c67 | 527 | |
79721e0a | 528 | active = find_active_client(&vgasr_priv.clients); |
66b37c67 DA |
529 | if (!active) |
530 | return 0; | |
531 | ||
532 | active->active = false; | |
6a9ee8af | 533 | |
c91c3fae TI |
534 | set_audio_state(active->id, VGA_SWITCHEROO_OFF); |
535 | ||
6a9ee8af DA |
536 | if (new_client->fb_info) { |
537 | struct fb_event event; | |
7491bfb4 | 538 | |
054430e7 | 539 | console_lock(); |
6a9ee8af DA |
540 | event.info = new_client->fb_info; |
541 | fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event); | |
054430e7 | 542 | console_unlock(); |
6a9ee8af DA |
543 | } |
544 | ||
545 | ret = vgasr_priv.handler->switchto(new_client->id); | |
546 | if (ret) | |
547 | return ret; | |
548 | ||
26ec685f TI |
549 | if (new_client->ops->reprobe) |
550 | new_client->ops->reprobe(new_client->pdev); | |
8d608aa6 | 551 | |
6a9ee8af DA |
552 | if (active->pwr_state == VGA_SWITCHEROO_ON) |
553 | vga_switchoff(active); | |
554 | ||
c91c3fae TI |
555 | set_audio_state(new_client->id, VGA_SWITCHEROO_ON); |
556 | ||
6a9ee8af DA |
557 | new_client->active = true; |
558 | return 0; | |
559 | } | |
560 | ||
79721e0a TI |
561 | static bool check_can_switch(void) |
562 | { | |
563 | struct vga_switcheroo_client *client; | |
564 | ||
565 | list_for_each_entry(client, &vgasr_priv.clients, list) { | |
26ec685f | 566 | if (!client->ops->can_switch(client->pdev)) { |
9b0be1eb | 567 | pr_err("client %x refused switch\n", client->id); |
79721e0a TI |
568 | return false; |
569 | } | |
570 | } | |
571 | return true; | |
572 | } | |
573 | ||
6a9ee8af DA |
574 | static ssize_t |
575 | vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, | |
576 | size_t cnt, loff_t *ppos) | |
577 | { | |
578 | char usercmd[64]; | |
79721e0a | 579 | int ret; |
6a9ee8af | 580 | bool delay = false, can_switch; |
851ab954 | 581 | bool just_mux = false; |
6a9ee8af DA |
582 | int client_id = -1; |
583 | struct vga_switcheroo_client *client = NULL; | |
584 | ||
585 | if (cnt > 63) | |
586 | cnt = 63; | |
587 | ||
588 | if (copy_from_user(usercmd, ubuf, cnt)) | |
589 | return -EFAULT; | |
590 | ||
591 | mutex_lock(&vgasr_mutex); | |
592 | ||
8c88e50b JS |
593 | if (!vgasr_priv.active) { |
594 | cnt = -EINVAL; | |
595 | goto out; | |
596 | } | |
6a9ee8af DA |
597 | |
598 | /* pwr off the device not in use */ | |
599 | if (strncmp(usercmd, "OFF", 3) == 0) { | |
79721e0a | 600 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
c91c3fae | 601 | if (client->active || client_is_audio(client)) |
6a9ee8af | 602 | continue; |
0d69704a DA |
603 | if (client->driver_power_control) |
604 | continue; | |
c91c3fae | 605 | set_audio_state(client->id, VGA_SWITCHEROO_OFF); |
79721e0a TI |
606 | if (client->pwr_state == VGA_SWITCHEROO_ON) |
607 | vga_switchoff(client); | |
6a9ee8af DA |
608 | } |
609 | goto out; | |
610 | } | |
611 | /* pwr on the device not in use */ | |
612 | if (strncmp(usercmd, "ON", 2) == 0) { | |
79721e0a | 613 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
c91c3fae | 614 | if (client->active || client_is_audio(client)) |
6a9ee8af | 615 | continue; |
0d69704a DA |
616 | if (client->driver_power_control) |
617 | continue; | |
79721e0a TI |
618 | if (client->pwr_state == VGA_SWITCHEROO_OFF) |
619 | vga_switchon(client); | |
c91c3fae | 620 | set_audio_state(client->id, VGA_SWITCHEROO_ON); |
6a9ee8af DA |
621 | } |
622 | goto out; | |
623 | } | |
624 | ||
625 | /* request a delayed switch - test can we switch now */ | |
626 | if (strncmp(usercmd, "DIGD", 4) == 0) { | |
627 | client_id = VGA_SWITCHEROO_IGD; | |
628 | delay = true; | |
629 | } | |
630 | ||
631 | if (strncmp(usercmd, "DDIS", 4) == 0) { | |
632 | client_id = VGA_SWITCHEROO_DIS; | |
633 | delay = true; | |
634 | } | |
635 | ||
636 | if (strncmp(usercmd, "IGD", 3) == 0) | |
637 | client_id = VGA_SWITCHEROO_IGD; | |
638 | ||
639 | if (strncmp(usercmd, "DIS", 3) == 0) | |
640 | client_id = VGA_SWITCHEROO_DIS; | |
641 | ||
fea6f330 | 642 | if (strncmp(usercmd, "MIGD", 4) == 0) { |
851ab954 DA |
643 | just_mux = true; |
644 | client_id = VGA_SWITCHEROO_IGD; | |
645 | } | |
fea6f330 | 646 | if (strncmp(usercmd, "MDIS", 4) == 0) { |
851ab954 DA |
647 | just_mux = true; |
648 | client_id = VGA_SWITCHEROO_DIS; | |
649 | } | |
650 | ||
6a9ee8af DA |
651 | if (client_id == -1) |
652 | goto out; | |
79721e0a TI |
653 | client = find_client_from_id(&vgasr_priv.clients, client_id); |
654 | if (!client) | |
655 | goto out; | |
6a9ee8af DA |
656 | |
657 | vgasr_priv.delayed_switch_active = false; | |
851ab954 DA |
658 | |
659 | if (just_mux) { | |
660 | ret = vgasr_priv.handler->switchto(client_id); | |
661 | goto out; | |
662 | } | |
663 | ||
79721e0a | 664 | if (client->active) |
a67b8887 FM |
665 | goto out; |
666 | ||
6a9ee8af | 667 | /* okay we want a switch - test if devices are willing to switch */ |
79721e0a | 668 | can_switch = check_can_switch(); |
6a9ee8af DA |
669 | |
670 | if (can_switch == false && delay == false) | |
671 | goto out; | |
672 | ||
79721e0a | 673 | if (can_switch) { |
66b37c67 DA |
674 | ret = vga_switchto_stage1(client); |
675 | if (ret) | |
9b0be1eb | 676 | pr_err("switching failed stage 1 %d\n", ret); |
66b37c67 DA |
677 | |
678 | ret = vga_switchto_stage2(client); | |
6a9ee8af | 679 | if (ret) |
9b0be1eb | 680 | pr_err("switching failed stage 2 %d\n", ret); |
66b37c67 | 681 | |
6a9ee8af | 682 | } else { |
9b0be1eb | 683 | pr_info("setting delayed switch to client %d\n", client->id); |
6a9ee8af DA |
684 | vgasr_priv.delayed_switch_active = true; |
685 | vgasr_priv.delayed_client_id = client_id; | |
686 | ||
66b37c67 DA |
687 | ret = vga_switchto_stage1(client); |
688 | if (ret) | |
9b0be1eb | 689 | pr_err("delayed switching stage 1 failed %d\n", ret); |
6a9ee8af DA |
690 | } |
691 | ||
692 | out: | |
693 | mutex_unlock(&vgasr_mutex); | |
694 | return cnt; | |
695 | } | |
696 | ||
697 | static const struct file_operations vga_switcheroo_debugfs_fops = { | |
698 | .owner = THIS_MODULE, | |
699 | .open = vga_switcheroo_debugfs_open, | |
700 | .write = vga_switcheroo_debugfs_write, | |
701 | .read = seq_read, | |
702 | .llseek = seq_lseek, | |
703 | .release = single_release, | |
704 | }; | |
705 | ||
706 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv) | |
707 | { | |
798ae0f6 TR |
708 | debugfs_remove(priv->switch_file); |
709 | priv->switch_file = NULL; | |
710 | ||
711 | debugfs_remove(priv->debugfs_root); | |
712 | priv->debugfs_root = NULL; | |
6a9ee8af DA |
713 | } |
714 | ||
715 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv) | |
716 | { | |
9b0be1eb TR |
717 | static const char mp[] = "/sys/kernel/debug"; |
718 | ||
6a9ee8af DA |
719 | /* already initialised */ |
720 | if (priv->debugfs_root) | |
721 | return 0; | |
722 | priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL); | |
723 | ||
724 | if (!priv->debugfs_root) { | |
9b0be1eb | 725 | pr_err("Cannot create %s/vgaswitcheroo\n", mp); |
6a9ee8af DA |
726 | goto fail; |
727 | } | |
728 | ||
729 | priv->switch_file = debugfs_create_file("switch", 0644, | |
7491bfb4 TR |
730 | priv->debugfs_root, NULL, |
731 | &vga_switcheroo_debugfs_fops); | |
6a9ee8af | 732 | if (!priv->switch_file) { |
9b0be1eb | 733 | pr_err("cannot create %s/vgaswitcheroo/switch\n", mp); |
6a9ee8af DA |
734 | goto fail; |
735 | } | |
736 | return 0; | |
737 | fail: | |
738 | vga_switcheroo_debugfs_fini(priv); | |
739 | return -1; | |
740 | } | |
741 | ||
a645654b LW |
742 | /** |
743 | * vga_switcheroo_process_delayed_switch() - helper for delayed switching | |
744 | * | |
745 | * Process a delayed switch if one is pending. DRM drivers should call this | |
746 | * from their ->lastclose callback. | |
747 | * | |
748 | * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client | |
749 | * has unregistered in the meantime or if there are other clients blocking the | |
750 | * switch. If the actual switch fails, an error is reported and 0 is returned. | |
751 | */ | |
6a9ee8af DA |
752 | int vga_switcheroo_process_delayed_switch(void) |
753 | { | |
79721e0a | 754 | struct vga_switcheroo_client *client; |
6a9ee8af DA |
755 | int ret; |
756 | int err = -EINVAL; | |
757 | ||
758 | mutex_lock(&vgasr_mutex); | |
759 | if (!vgasr_priv.delayed_switch_active) | |
760 | goto err; | |
761 | ||
9b0be1eb TR |
762 | pr_info("processing delayed switch to %d\n", |
763 | vgasr_priv.delayed_client_id); | |
6a9ee8af | 764 | |
79721e0a TI |
765 | client = find_client_from_id(&vgasr_priv.clients, |
766 | vgasr_priv.delayed_client_id); | |
767 | if (!client || !check_can_switch()) | |
6a9ee8af DA |
768 | goto err; |
769 | ||
66b37c67 | 770 | ret = vga_switchto_stage2(client); |
6a9ee8af | 771 | if (ret) |
9b0be1eb | 772 | pr_err("delayed switching failed stage 2 %d\n", ret); |
6a9ee8af DA |
773 | |
774 | vgasr_priv.delayed_switch_active = false; | |
775 | err = 0; | |
776 | err: | |
777 | mutex_unlock(&vgasr_mutex); | |
778 | return err; | |
779 | } | |
780 | EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch); | |
0d69704a | 781 | |
a645654b LW |
782 | /** |
783 | * DOC: Driver power control | |
784 | * | |
785 | * In this mode of use, the discrete GPU automatically powers up and down at | |
786 | * the discretion of the driver's runtime pm. On muxed machines, the user may | |
787 | * still influence the muxer state by way of the debugfs interface, however | |
788 | * the ON and OFF commands become a no-op for the discrete GPU. | |
789 | * | |
790 | * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress. | |
791 | * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel | |
792 | * command line disables it. | |
793 | * | |
794 | * When the driver decides to power up or down, it notifies vga_switcheroo | |
795 | * thereof so that it can (a) power the audio device on the GPU up or down, | |
796 | * and (b) update its internal power state representation for the device. | |
797 | * This is achieved by vga_switcheroo_set_dynamic_switch(). | |
798 | * | |
799 | * After the GPU has been suspended, the handler needs to be called to cut | |
800 | * power to the GPU. Likewise it needs to reinstate power before the GPU | |
801 | * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(), | |
802 | * which augments the GPU's suspend/resume functions by the requisite | |
803 | * calls to the handler. | |
804 | * | |
805 | * When the audio device resumes, the GPU needs to be woken. This is achieved | |
806 | * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the | |
807 | * audio device's resume function. | |
808 | * | |
809 | * On muxed machines, if the mux is initially switched to the discrete GPU, | |
810 | * the user ends up with a black screen when the GPU powers down after boot. | |
811 | * As a workaround, the mux is forced to the integrated GPU on runtime suspend, | |
812 | * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917 | |
813 | */ | |
814 | ||
7491bfb4 TR |
815 | static void vga_switcheroo_power_switch(struct pci_dev *pdev, |
816 | enum vga_switcheroo_state state) | |
0d69704a DA |
817 | { |
818 | struct vga_switcheroo_client *client; | |
819 | ||
820 | if (!vgasr_priv.handler->power_state) | |
821 | return; | |
822 | ||
823 | client = find_client_from_pci(&vgasr_priv.clients, pdev); | |
824 | if (!client) | |
825 | return; | |
826 | ||
827 | if (!client->driver_power_control) | |
828 | return; | |
829 | ||
830 | vgasr_priv.handler->power_state(client->id, state); | |
831 | } | |
832 | ||
a645654b LW |
833 | /** |
834 | * vga_switcheroo_set_dynamic_switch() - helper for driver power control | |
835 | * @pdev: client pci device | |
836 | * @dynamic: new power state | |
837 | * | |
838 | * Helper for GPUs whose power state is controlled by the driver's runtime pm. | |
839 | * When the driver decides to power up or down, it notifies vga_switcheroo | |
840 | * thereof using this helper so that it can (a) power the audio device on | |
841 | * the GPU up or down, and (b) update its internal power state representation | |
842 | * for the device. | |
843 | */ | |
7491bfb4 TR |
844 | void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, |
845 | enum vga_switcheroo_state dynamic) | |
0d69704a DA |
846 | { |
847 | struct vga_switcheroo_client *client; | |
848 | ||
849 | client = find_client_from_pci(&vgasr_priv.clients, pdev); | |
850 | if (!client) | |
851 | return; | |
852 | ||
853 | if (!client->driver_power_control) | |
854 | return; | |
855 | ||
856 | client->pwr_state = dynamic; | |
857 | set_audio_state(client->id, dynamic); | |
858 | } | |
859 | EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch); | |
860 | ||
861 | /* switcheroo power domain */ | |
862 | static int vga_switcheroo_runtime_suspend(struct device *dev) | |
863 | { | |
864 | struct pci_dev *pdev = to_pci_dev(dev); | |
865 | int ret; | |
866 | ||
867 | ret = dev->bus->pm->runtime_suspend(dev); | |
868 | if (ret) | |
869 | return ret; | |
f2bc5616 AD |
870 | if (vgasr_priv.handler->switchto) |
871 | vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD); | |
0d69704a DA |
872 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF); |
873 | return 0; | |
874 | } | |
875 | ||
876 | static int vga_switcheroo_runtime_resume(struct device *dev) | |
877 | { | |
878 | struct pci_dev *pdev = to_pci_dev(dev); | |
879 | int ret; | |
880 | ||
881 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON); | |
882 | ret = dev->bus->pm->runtime_resume(dev); | |
883 | if (ret) | |
884 | return ret; | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
a645654b LW |
889 | /** |
890 | * vga_switcheroo_init_domain_pm_ops() - helper for driver power control | |
891 | * @dev: vga client device | |
892 | * @domain: power domain | |
893 | * | |
894 | * Helper for GPUs whose power state is controlled by the driver's runtime pm. | |
895 | * After the GPU has been suspended, the handler needs to be called to cut | |
896 | * power to the GPU. Likewise it needs to reinstate power before the GPU | |
897 | * can resume. To this end, this helper augments the suspend/resume functions | |
898 | * by the requisite calls to the handler. It needs only be called on platforms | |
899 | * where the power switch is separate to the device being powered down. | |
900 | */ | |
7491bfb4 TR |
901 | int vga_switcheroo_init_domain_pm_ops(struct device *dev, |
902 | struct dev_pm_domain *domain) | |
0d69704a DA |
903 | { |
904 | /* copy over all the bus versions */ | |
905 | if (dev->bus && dev->bus->pm) { | |
906 | domain->ops = *dev->bus->pm; | |
907 | domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend; | |
908 | domain->ops.runtime_resume = vga_switcheroo_runtime_resume; | |
909 | ||
910 | dev->pm_domain = domain; | |
911 | return 0; | |
912 | } | |
913 | dev->pm_domain = NULL; | |
914 | return -EINVAL; | |
915 | } | |
916 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops); | |
917 | ||
766a53d0 AD |
918 | void vga_switcheroo_fini_domain_pm_ops(struct device *dev) |
919 | { | |
920 | dev->pm_domain = NULL; | |
921 | } | |
922 | EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops); | |
923 | ||
0d69704a DA |
924 | static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev) |
925 | { | |
926 | struct pci_dev *pdev = to_pci_dev(dev); | |
927 | int ret; | |
928 | struct vga_switcheroo_client *client, *found = NULL; | |
929 | ||
930 | /* we need to check if we have to switch back on the video | |
931 | device so the audio device can come back */ | |
932 | list_for_each_entry(client, &vgasr_priv.clients, list) { | |
7491bfb4 TR |
933 | if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) && |
934 | client_is_vga(client)) { | |
0d69704a DA |
935 | found = client; |
936 | ret = pm_runtime_get_sync(&client->pdev->dev); | |
937 | if (ret) { | |
938 | if (ret != 1) | |
939 | return ret; | |
940 | } | |
941 | break; | |
942 | } | |
943 | } | |
944 | ret = dev->bus->pm->runtime_resume(dev); | |
945 | ||
946 | /* put the reference for the gpu */ | |
947 | if (found) { | |
948 | pm_runtime_mark_last_busy(&found->pdev->dev); | |
949 | pm_runtime_put_autosuspend(&found->pdev->dev); | |
950 | } | |
951 | return ret; | |
952 | } | |
953 | ||
a645654b LW |
954 | /** |
955 | * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver | |
956 | * power control | |
957 | * @dev: audio client device | |
958 | * @domain: power domain | |
959 | * | |
960 | * Helper for GPUs whose power state is controlled by the driver's runtime pm. | |
961 | * When the audio device resumes, the GPU needs to be woken. This helper | |
962 | * augments the audio device's resume function to do that. | |
963 | * | |
964 | * Return: 0 on success, -EINVAL if no power management operations are | |
965 | * defined for this device. | |
966 | */ | |
7491bfb4 TR |
967 | int |
968 | vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, | |
969 | struct dev_pm_domain *domain) | |
0d69704a DA |
970 | { |
971 | /* copy over all the bus versions */ | |
972 | if (dev->bus && dev->bus->pm) { | |
973 | domain->ops = *dev->bus->pm; | |
7491bfb4 TR |
974 | domain->ops.runtime_resume = |
975 | vga_switcheroo_runtime_resume_hdmi_audio; | |
0d69704a DA |
976 | |
977 | dev->pm_domain = domain; | |
978 | return 0; | |
979 | } | |
980 | dev->pm_domain = NULL; | |
981 | return -EINVAL; | |
982 | } | |
983 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio); |