Linux Audio

Check our new training course

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