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