blob: faa57e546138aac8888b299e44750f1f997c7dec [file] [log] [blame]
Dave Airlie6a9ee8a2010-02-01 15:38:10 +10001/*
2 * Copyright (c) 2010 Red Hat Inc.
3 * Author : Dave Airlie <airlied@redhat.com>
4 *
5 *
6 * Licensed under GPLv2
7 *
8 * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
Thierry Reding71309272015-08-12 16:32:09 +02009 *
10 * Switcher interface - methods require for ATPX and DCM
11 * - switchto - this throws the output MUX switch
12 * - discrete_set_power - sets the power state for the discrete card
13 *
14 * GPU driver interface
15 * - set_gpu_state - this should do the equiv of s/r for the card
16 * - this should *not* set the discrete power state
17 * - switch_check - check if the device is in a position to switch now
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100018 */
19
Thierry Reding9b0be1e2015-08-12 16:32:10 +020020#define pr_fmt(fmt) "vga_switcheroo: " fmt
21
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100022#include <linux/module.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100023#include <linux/seq_file.h>
24#include <linux/uaccess.h>
25#include <linux/fs.h>
26#include <linux/debugfs.h>
27#include <linux/fb.h>
28
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100029#include <linux/pci.h>
Dave Airlie054430e2013-01-25 11:38:56 +100030#include <linux/console.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100031#include <linux/vga_switcheroo.h>
Dave Airlie0d697042012-09-10 12:28:36 +100032#include <linux/pm_runtime.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100033
Matthew Garrett2fbe8c72012-04-16 16:26:03 -040034#include <linux/vgaarb.h>
35
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100036struct vga_switcheroo_client {
37 struct pci_dev *pdev;
38 struct fb_info *fb_info;
39 int pwr_state;
Takashi Iwai26ec6852012-05-11 07:51:17 +020040 const struct vga_switcheroo_client_ops *ops;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100041 int id;
42 bool active;
Dave Airlie0d697042012-09-10 12:28:36 +100043 bool driver_power_control;
Takashi Iwai79721e02012-04-26 12:55:59 +020044 struct list_head list;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100045};
46
47static DEFINE_MUTEX(vgasr_mutex);
48
49struct vgasr_priv {
50
51 bool active;
52 bool delayed_switch_active;
53 enum vga_switcheroo_client_id delayed_client_id;
54
55 struct dentry *debugfs_root;
56 struct dentry *switch_file;
57
58 int registered_clients;
Takashi Iwai79721e02012-04-26 12:55:59 +020059 struct list_head clients;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100060
61 struct vga_switcheroo_handler *handler;
62};
63
Takashi Iwai3e9e63d2012-04-26 14:29:48 +020064#define ID_BIT_AUDIO 0x100
65#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
66#define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c))
67#define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
68
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100069static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
70static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
71
72/* only one switcheroo per system */
Takashi Iwai79721e02012-04-26 12:55:59 +020073static struct vgasr_priv vgasr_priv = {
74 .clients = LIST_HEAD_INIT(vgasr_priv.clients),
75};
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100076
Seth Forshee36704c02012-08-17 11:17:03 -050077static bool vga_switcheroo_ready(void)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100078{
Seth Forshee36704c02012-08-17 11:17:03 -050079 /* we're ready if we get two clients + handler */
80 return !vgasr_priv.active &&
81 vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100082}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100083
84static void vga_switcheroo_enable(void)
85{
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100086 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +020087 struct vga_switcheroo_client *client;
88
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100089 /* call the handler to init */
Seth Forsheee99eac52012-08-17 11:17:02 -050090 if (vgasr_priv.handler->init)
91 vgasr_priv.handler->init();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100092
Takashi Iwai79721e02012-04-26 12:55:59 +020093 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +020094 if (client->id != -1)
95 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +020096 ret = vgasr_priv.handler->get_client_id(client->pdev);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100097 if (ret < 0)
98 return;
99
Takashi Iwai79721e02012-04-26 12:55:59 +0200100 client->id = ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000101 }
102 vga_switcheroo_debugfs_init(&vgasr_priv);
103 vgasr_priv.active = true;
104}
105
Seth Forshee36704c02012-08-17 11:17:03 -0500106int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler)
107{
108 mutex_lock(&vgasr_mutex);
109 if (vgasr_priv.handler) {
110 mutex_unlock(&vgasr_mutex);
111 return -EINVAL;
112 }
113
114 vgasr_priv.handler = handler;
115 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200116 pr_info("enabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500117 vga_switcheroo_enable();
118 }
119 mutex_unlock(&vgasr_mutex);
120 return 0;
121}
122EXPORT_SYMBOL(vga_switcheroo_register_handler);
123
124void vga_switcheroo_unregister_handler(void)
125{
126 mutex_lock(&vgasr_mutex);
127 vgasr_priv.handler = NULL;
128 if (vgasr_priv.active) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200129 pr_info("disabled\n");
Seth Forshee36704c02012-08-17 11:17:03 -0500130 vga_switcheroo_debugfs_fini(&vgasr_priv);
131 vgasr_priv.active = false;
132 }
133 mutex_unlock(&vgasr_mutex);
134}
135EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
136
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200137static int register_client(struct pci_dev *pdev,
138 const struct vga_switcheroo_client_ops *ops,
Dave Airlie0d697042012-09-10 12:28:36 +1000139 int id, bool active, bool driver_power_control)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000140{
Takashi Iwai79721e02012-04-26 12:55:59 +0200141 struct vga_switcheroo_client *client;
142
143 client = kzalloc(sizeof(*client), GFP_KERNEL);
144 if (!client)
145 return -ENOMEM;
146
147 client->pwr_state = VGA_SWITCHEROO_ON;
148 client->pdev = pdev;
Takashi Iwai26ec6852012-05-11 07:51:17 +0200149 client->ops = ops;
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200150 client->id = id;
151 client->active = active;
Dave Airlie0d697042012-09-10 12:28:36 +1000152 client->driver_power_control = driver_power_control;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000153
154 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200155 list_add_tail(&client->list, &vgasr_priv.clients);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200156 if (client_is_vga(client))
157 vgasr_priv.registered_clients++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000158
Seth Forshee36704c02012-08-17 11:17:03 -0500159 if (vga_switcheroo_ready()) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200160 pr_info("enabled\n");
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000161 vga_switcheroo_enable();
162 }
163 mutex_unlock(&vgasr_mutex);
164 return 0;
165}
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200166
167int vga_switcheroo_register_client(struct pci_dev *pdev,
Dave Airlie0d697042012-09-10 12:28:36 +1000168 const struct vga_switcheroo_client_ops *ops,
169 bool driver_power_control)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200170{
171 return register_client(pdev, ops, -1,
Dave Airlie0d697042012-09-10 12:28:36 +1000172 pdev == vga_default_device(), driver_power_control);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200173}
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000174EXPORT_SYMBOL(vga_switcheroo_register_client);
175
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200176int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
177 const struct vga_switcheroo_client_ops *ops,
178 int id, bool active)
179{
Dave Airlie0d697042012-09-10 12:28:36 +1000180 return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false);
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200181}
182EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
183
Takashi Iwai79721e02012-04-26 12:55:59 +0200184static struct vga_switcheroo_client *
185find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
186{
187 struct vga_switcheroo_client *client;
188 list_for_each_entry(client, head, list)
189 if (client->pdev == pdev)
190 return client;
191 return NULL;
192}
193
194static struct vga_switcheroo_client *
195find_client_from_id(struct list_head *head, int client_id)
196{
197 struct vga_switcheroo_client *client;
198 list_for_each_entry(client, head, list)
199 if (client->id == client_id)
200 return client;
201 return NULL;
202}
203
204static struct vga_switcheroo_client *
205find_active_client(struct list_head *head)
206{
207 struct vga_switcheroo_client *client;
208 list_for_each_entry(client, head, list)
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200209 if (client->active && client_is_vga(client))
Takashi Iwai79721e02012-04-26 12:55:59 +0200210 return client;
211 return NULL;
212}
213
Takashi Iwaic8e9cf72012-06-07 12:15:15 +0200214int vga_switcheroo_get_client_state(struct pci_dev *pdev)
215{
216 struct vga_switcheroo_client *client;
217
218 client = find_client_from_pci(&vgasr_priv.clients, pdev);
219 if (!client)
220 return VGA_SWITCHEROO_NOT_FOUND;
221 if (!vgasr_priv.active)
222 return VGA_SWITCHEROO_INIT;
223 return client->pwr_state;
224}
225EXPORT_SYMBOL(vga_switcheroo_get_client_state);
226
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000227void vga_switcheroo_unregister_client(struct pci_dev *pdev)
228{
Takashi Iwai79721e02012-04-26 12:55:59 +0200229 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000230
231 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200232 client = find_client_from_pci(&vgasr_priv.clients, pdev);
233 if (client) {
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200234 if (client_is_vga(client))
235 vgasr_priv.registered_clients--;
Takashi Iwai79721e02012-04-26 12:55:59 +0200236 list_del(&client->list);
237 kfree(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000238 }
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200239 if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200240 pr_info("disabled\n");
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200241 vga_switcheroo_debugfs_fini(&vgasr_priv);
242 vgasr_priv.active = false;
243 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000244 mutex_unlock(&vgasr_mutex);
245}
246EXPORT_SYMBOL(vga_switcheroo_unregister_client);
247
248void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
249 struct fb_info *info)
250{
Takashi Iwai79721e02012-04-26 12:55:59 +0200251 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000252
253 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200254 client = find_client_from_pci(&vgasr_priv.clients, pdev);
255 if (client)
256 client->fb_info = info;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000257 mutex_unlock(&vgasr_mutex);
258}
259EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
260
261static int vga_switcheroo_show(struct seq_file *m, void *v)
262{
Takashi Iwai79721e02012-04-26 12:55:59 +0200263 struct vga_switcheroo_client *client;
264 int i = 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000265 mutex_lock(&vgasr_mutex);
Takashi Iwai79721e02012-04-26 12:55:59 +0200266 list_for_each_entry(client, &vgasr_priv.clients, list) {
Dave Airlie0d697042012-09-10 12:28:36 +1000267 seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200268 client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD",
269 client_is_vga(client) ? "" : "-Audio",
Takashi Iwai79721e02012-04-26 12:55:59 +0200270 client->active ? '+' : ' ',
Dave Airlie0d697042012-09-10 12:28:36 +1000271 client->driver_power_control ? "Dyn" : "",
Takashi Iwai79721e02012-04-26 12:55:59 +0200272 client->pwr_state ? "Pwr" : "Off",
273 pci_name(client->pdev));
274 i++;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000275 }
276 mutex_unlock(&vgasr_mutex);
277 return 0;
278}
279
280static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
281{
282 return single_open(file, vga_switcheroo_show, NULL);
283}
284
285static int vga_switchon(struct vga_switcheroo_client *client)
286{
Dave Airlie0d697042012-09-10 12:28:36 +1000287 if (client->driver_power_control)
288 return 0;
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000289 if (vgasr_priv.handler->power_state)
290 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000291 /* call the driver callback to turn on device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200292 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000293 client->pwr_state = VGA_SWITCHEROO_ON;
294 return 0;
295}
296
297static int vga_switchoff(struct vga_switcheroo_client *client)
298{
Dave Airlie0d697042012-09-10 12:28:36 +1000299 if (client->driver_power_control)
300 return 0;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000301 /* call the driver callback to turn off device */
Takashi Iwai26ec6852012-05-11 07:51:17 +0200302 client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
Dave Airlie5cfb3c32010-12-06 12:31:50 +1000303 if (vgasr_priv.handler->power_state)
304 vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000305 client->pwr_state = VGA_SWITCHEROO_OFF;
306 return 0;
307}
308
Takashi Iwai3e9e63d2012-04-26 14:29:48 +0200309static void set_audio_state(int id, int state)
310{
311 struct vga_switcheroo_client *client;
312
313 client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
314 if (client && client->pwr_state != state) {
315 client->ops->set_gpu_state(client->pdev, state);
316 client->pwr_state = state;
317 }
318}
319
Dave Airlie66b37c62010-12-07 14:24:25 +1000320/* stage one happens before delay */
321static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000322{
Takashi Iwai79721e02012-04-26 12:55:59 +0200323 struct vga_switcheroo_client *active;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000324
Takashi Iwai79721e02012-04-26 12:55:59 +0200325 active = find_active_client(&vgasr_priv.clients);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000326 if (!active)
327 return 0;
328
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000329 if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
330 vga_switchon(new_client);
331
Matthew Garrett2fbe8c72012-04-16 16:26:03 -0400332 vga_set_default_device(new_client->pdev);
Dave Airlie66b37c62010-12-07 14:24:25 +1000333 return 0;
334}
335
336/* post delay */
337static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
338{
339 int ret;
Takashi Iwai79721e02012-04-26 12:55:59 +0200340 struct vga_switcheroo_client *active;
Dave Airlie66b37c62010-12-07 14:24:25 +1000341
Takashi Iwai79721e02012-04-26 12:55:59 +0200342 active = find_active_client(&vgasr_priv.clients);
Dave Airlie66b37c62010-12-07 14:24:25 +1000343 if (!active)
344 return 0;
345
346 active->active = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000347
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200348 set_audio_state(active->id, VGA_SWITCHEROO_OFF);
349
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000350 if (new_client->fb_info) {
351 struct fb_event event;
Dave Airlie054430e2013-01-25 11:38:56 +1000352 console_lock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000353 event.info = new_client->fb_info;
354 fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
Dave Airlie054430e2013-01-25 11:38:56 +1000355 console_unlock();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000356 }
357
358 ret = vgasr_priv.handler->switchto(new_client->id);
359 if (ret)
360 return ret;
361
Takashi Iwai26ec6852012-05-11 07:51:17 +0200362 if (new_client->ops->reprobe)
363 new_client->ops->reprobe(new_client->pdev);
Dave Airlie8d608aa2010-12-07 08:57:57 +1000364
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000365 if (active->pwr_state == VGA_SWITCHEROO_ON)
366 vga_switchoff(active);
367
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200368 set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
369
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000370 new_client->active = true;
371 return 0;
372}
373
Takashi Iwai79721e02012-04-26 12:55:59 +0200374static bool check_can_switch(void)
375{
376 struct vga_switcheroo_client *client;
377
378 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwai26ec6852012-05-11 07:51:17 +0200379 if (!client->ops->can_switch(client->pdev)) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200380 pr_err("client %x refused switch\n", client->id);
Takashi Iwai79721e02012-04-26 12:55:59 +0200381 return false;
382 }
383 }
384 return true;
385}
386
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000387static ssize_t
388vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
389 size_t cnt, loff_t *ppos)
390{
391 char usercmd[64];
Takashi Iwai79721e02012-04-26 12:55:59 +0200392 int ret;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000393 bool delay = false, can_switch;
Dave Airlie851ab952010-12-06 12:35:52 +1000394 bool just_mux = false;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000395 int client_id = -1;
396 struct vga_switcheroo_client *client = NULL;
397
398 if (cnt > 63)
399 cnt = 63;
400
401 if (copy_from_user(usercmd, ubuf, cnt))
402 return -EFAULT;
403
404 mutex_lock(&vgasr_mutex);
405
Jiri Slaby8c88e502010-04-27 14:11:03 -0700406 if (!vgasr_priv.active) {
407 cnt = -EINVAL;
408 goto out;
409 }
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000410
411 /* pwr off the device not in use */
412 if (strncmp(usercmd, "OFF", 3) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200413 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200414 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000415 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000416 if (client->driver_power_control)
417 continue;
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200418 set_audio_state(client->id, VGA_SWITCHEROO_OFF);
Takashi Iwai79721e02012-04-26 12:55:59 +0200419 if (client->pwr_state == VGA_SWITCHEROO_ON)
420 vga_switchoff(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000421 }
422 goto out;
423 }
424 /* pwr on the device not in use */
425 if (strncmp(usercmd, "ON", 2) == 0) {
Takashi Iwai79721e02012-04-26 12:55:59 +0200426 list_for_each_entry(client, &vgasr_priv.clients, list) {
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200427 if (client->active || client_is_audio(client))
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000428 continue;
Dave Airlie0d697042012-09-10 12:28:36 +1000429 if (client->driver_power_control)
430 continue;
Takashi Iwai79721e02012-04-26 12:55:59 +0200431 if (client->pwr_state == VGA_SWITCHEROO_OFF)
432 vga_switchon(client);
Takashi Iwaic91c3fa2012-06-09 08:46:42 +0200433 set_audio_state(client->id, VGA_SWITCHEROO_ON);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000434 }
435 goto out;
436 }
437
438 /* request a delayed switch - test can we switch now */
439 if (strncmp(usercmd, "DIGD", 4) == 0) {
440 client_id = VGA_SWITCHEROO_IGD;
441 delay = true;
442 }
443
444 if (strncmp(usercmd, "DDIS", 4) == 0) {
445 client_id = VGA_SWITCHEROO_DIS;
446 delay = true;
447 }
448
449 if (strncmp(usercmd, "IGD", 3) == 0)
450 client_id = VGA_SWITCHEROO_IGD;
451
452 if (strncmp(usercmd, "DIS", 3) == 0)
453 client_id = VGA_SWITCHEROO_DIS;
454
Dan Carpenterfea6f332011-01-07 08:12:27 +0300455 if (strncmp(usercmd, "MIGD", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000456 just_mux = true;
457 client_id = VGA_SWITCHEROO_IGD;
458 }
Dan Carpenterfea6f332011-01-07 08:12:27 +0300459 if (strncmp(usercmd, "MDIS", 4) == 0) {
Dave Airlie851ab952010-12-06 12:35:52 +1000460 just_mux = true;
461 client_id = VGA_SWITCHEROO_DIS;
462 }
463
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000464 if (client_id == -1)
465 goto out;
Takashi Iwai79721e02012-04-26 12:55:59 +0200466 client = find_client_from_id(&vgasr_priv.clients, client_id);
467 if (!client)
468 goto out;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000469
470 vgasr_priv.delayed_switch_active = false;
Dave Airlie851ab952010-12-06 12:35:52 +1000471
472 if (just_mux) {
473 ret = vgasr_priv.handler->switchto(client_id);
474 goto out;
475 }
476
Takashi Iwai79721e02012-04-26 12:55:59 +0200477 if (client->active)
Florian Micklera67b8882011-05-15 16:32:50 +0200478 goto out;
479
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000480 /* okay we want a switch - test if devices are willing to switch */
Takashi Iwai79721e02012-04-26 12:55:59 +0200481 can_switch = check_can_switch();
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000482
483 if (can_switch == false && delay == false)
484 goto out;
485
Takashi Iwai79721e02012-04-26 12:55:59 +0200486 if (can_switch) {
Dave Airlie66b37c62010-12-07 14:24:25 +1000487 ret = vga_switchto_stage1(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000488 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200489 pr_err("switching failed stage 1 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000490
491 ret = vga_switchto_stage2(client);
492 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200493 pr_err("switching failed stage 2 %d\n", ret);
Dave Airlie66b37c62010-12-07 14:24:25 +1000494
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000495 } else {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200496 pr_info("setting delayed switch to client %d\n", client->id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000497 vgasr_priv.delayed_switch_active = true;
498 vgasr_priv.delayed_client_id = client_id;
499
Dave Airlie66b37c62010-12-07 14:24:25 +1000500 ret = vga_switchto_stage1(client);
501 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200502 pr_err("delayed switching stage 1 failed %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000503 }
504
505out:
506 mutex_unlock(&vgasr_mutex);
507 return cnt;
508}
509
510static const struct file_operations vga_switcheroo_debugfs_fops = {
511 .owner = THIS_MODULE,
512 .open = vga_switcheroo_debugfs_open,
513 .write = vga_switcheroo_debugfs_write,
514 .read = seq_read,
515 .llseek = seq_lseek,
516 .release = single_release,
517};
518
519static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
520{
521 if (priv->switch_file) {
522 debugfs_remove(priv->switch_file);
523 priv->switch_file = NULL;
524 }
525 if (priv->debugfs_root) {
526 debugfs_remove(priv->debugfs_root);
527 priv->debugfs_root = NULL;
528 }
529}
530
531static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
532{
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200533 static const char mp[] = "/sys/kernel/debug";
534
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000535 /* already initialised */
536 if (priv->debugfs_root)
537 return 0;
538 priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
539
540 if (!priv->debugfs_root) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200541 pr_err("Cannot create %s/vgaswitcheroo\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000542 goto fail;
543 }
544
545 priv->switch_file = debugfs_create_file("switch", 0644,
546 priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops);
547 if (!priv->switch_file) {
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200548 pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000549 goto fail;
550 }
551 return 0;
552fail:
553 vga_switcheroo_debugfs_fini(priv);
554 return -1;
555}
556
557int vga_switcheroo_process_delayed_switch(void)
558{
Takashi Iwai79721e02012-04-26 12:55:59 +0200559 struct vga_switcheroo_client *client;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000560 int ret;
561 int err = -EINVAL;
562
563 mutex_lock(&vgasr_mutex);
564 if (!vgasr_priv.delayed_switch_active)
565 goto err;
566
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200567 pr_info("processing delayed switch to %d\n",
568 vgasr_priv.delayed_client_id);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000569
Takashi Iwai79721e02012-04-26 12:55:59 +0200570 client = find_client_from_id(&vgasr_priv.clients,
571 vgasr_priv.delayed_client_id);
572 if (!client || !check_can_switch())
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000573 goto err;
574
Dave Airlie66b37c62010-12-07 14:24:25 +1000575 ret = vga_switchto_stage2(client);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000576 if (ret)
Thierry Reding9b0be1e2015-08-12 16:32:10 +0200577 pr_err("delayed switching failed stage 2 %d\n", ret);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000578
579 vgasr_priv.delayed_switch_active = false;
580 err = 0;
581err:
582 mutex_unlock(&vgasr_mutex);
583 return err;
584}
585EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
Dave Airlie0d697042012-09-10 12:28:36 +1000586
587static void vga_switcheroo_power_switch(struct pci_dev *pdev, enum vga_switcheroo_state state)
588{
589 struct vga_switcheroo_client *client;
590
591 if (!vgasr_priv.handler->power_state)
592 return;
593
594 client = find_client_from_pci(&vgasr_priv.clients, pdev);
595 if (!client)
596 return;
597
598 if (!client->driver_power_control)
599 return;
600
601 vgasr_priv.handler->power_state(client->id, state);
602}
603
604/* force a PCI device to a certain state - mainly to turn off audio clients */
605
606void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, enum vga_switcheroo_state dynamic)
607{
608 struct vga_switcheroo_client *client;
609
610 client = find_client_from_pci(&vgasr_priv.clients, pdev);
611 if (!client)
612 return;
613
614 if (!client->driver_power_control)
615 return;
616
617 client->pwr_state = dynamic;
618 set_audio_state(client->id, dynamic);
619}
620EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
621
622/* switcheroo power domain */
623static int vga_switcheroo_runtime_suspend(struct device *dev)
624{
625 struct pci_dev *pdev = to_pci_dev(dev);
626 int ret;
627
628 ret = dev->bus->pm->runtime_suspend(dev);
629 if (ret)
630 return ret;
Alex Deucherf2bc561612014-05-19 14:04:43 -0400631 if (vgasr_priv.handler->switchto)
632 vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
Dave Airlie0d697042012-09-10 12:28:36 +1000633 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
634 return 0;
635}
636
637static int vga_switcheroo_runtime_resume(struct device *dev)
638{
639 struct pci_dev *pdev = to_pci_dev(dev);
640 int ret;
641
642 vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
643 ret = dev->bus->pm->runtime_resume(dev);
644 if (ret)
645 return ret;
646
647 return 0;
648}
649
650/* this version is for the case where the power switch is separate
651 to the device being powered down. */
652int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain)
653{
654 /* copy over all the bus versions */
655 if (dev->bus && dev->bus->pm) {
656 domain->ops = *dev->bus->pm;
657 domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
658 domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
659
660 dev->pm_domain = domain;
661 return 0;
662 }
663 dev->pm_domain = NULL;
664 return -EINVAL;
665}
666EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
667
Alex Deucher766a53d2014-09-12 17:51:29 -0400668void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
669{
670 dev->pm_domain = NULL;
671}
672EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
673
Dave Airlie0d697042012-09-10 12:28:36 +1000674static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
675{
676 struct pci_dev *pdev = to_pci_dev(dev);
677 int ret;
678 struct vga_switcheroo_client *client, *found = NULL;
679
680 /* we need to check if we have to switch back on the video
681 device so the audio device can come back */
682 list_for_each_entry(client, &vgasr_priv.clients, list) {
683 if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) && client_is_vga(client)) {
684 found = client;
685 ret = pm_runtime_get_sync(&client->pdev->dev);
686 if (ret) {
687 if (ret != 1)
688 return ret;
689 }
690 break;
691 }
692 }
693 ret = dev->bus->pm->runtime_resume(dev);
694
695 /* put the reference for the gpu */
696 if (found) {
697 pm_runtime_mark_last_busy(&found->pdev->dev);
698 pm_runtime_put_autosuspend(&found->pdev->dev);
699 }
700 return ret;
701}
702
703int vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, struct dev_pm_domain *domain)
704{
705 /* copy over all the bus versions */
706 if (dev->bus && dev->bus->pm) {
707 domain->ops = *dev->bus->pm;
708 domain->ops.runtime_resume = vga_switcheroo_runtime_resume_hdmi_audio;
709
710 dev->pm_domain = domain;
711 return 0;
712 }
713 dev->pm_domain = NULL;
714 return -EINVAL;
715}
716EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);