xref: /linux/include/linux/vga_switcheroo.h (revision 2350f42c2f46b60bec79f2249f8bc5f3ff2807d4)
1 /*
2  * vga_switcheroo.h - 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 #ifndef _LINUX_VGA_SWITCHEROO_H_
32 #define _LINUX_VGA_SWITCHEROO_H_
33 
34 #include <linux/errno.h>
35 #include <linux/types.h>
36 
37 struct device;
38 struct dev_pm_domain;
39 struct fb_info;
40 struct pci_dev;
41 
42 /**
43  * enum vga_switcheroo_handler_flags_t - handler flags bitmask
44  * @VGA_SWITCHEROO_CAN_SWITCH_DDC: whether the handler is able to switch the
45  * 	DDC lines separately. This signals to clients that they should call
46  * 	drm_get_edid_switcheroo() to probe the EDID
47  * @VGA_SWITCHEROO_NEEDS_EDP_CONFIG: whether the handler is unable to switch
48  * 	the AUX channel separately. This signals to clients that the active
49  * 	GPU needs to train the link and communicate the link parameters to the
50  * 	inactive GPU (mediated by vga_switcheroo). The inactive GPU may then
51  * 	skip the AUX handshake and set up its output with these pre-calibrated
52  * 	values (DisplayPort specification v1.1a, section 2.5.3.3)
53  *
54  * Handler flags bitmask. Used by handlers to declare their capabilities upon
55  * registering with vga_switcheroo.
56  */
57 enum vga_switcheroo_handler_flags_t {
58 	VGA_SWITCHEROO_CAN_SWITCH_DDC	= (1 << 0),
59 	VGA_SWITCHEROO_NEEDS_EDP_CONFIG	= (1 << 1),
60 };
61 
62 /**
63  * enum vga_switcheroo_state - client power state
64  * @VGA_SWITCHEROO_OFF: off
65  * @VGA_SWITCHEROO_ON: on
66  * @VGA_SWITCHEROO_NOT_FOUND: client has not registered with vga_switcheroo.
67  * 	Only used in vga_switcheroo_get_client_state() which in turn is only
68  * 	called from hda_intel.c
69  *
70  * Client power state.
71  */
72 enum vga_switcheroo_state {
73 	VGA_SWITCHEROO_OFF,
74 	VGA_SWITCHEROO_ON,
75 	/* below are referred only from vga_switcheroo_get_client_state() */
76 	VGA_SWITCHEROO_NOT_FOUND,
77 };
78 
79 /**
80  * enum vga_switcheroo_client_id - client identifier
81  * @VGA_SWITCHEROO_UNKNOWN_ID: initial identifier assigned to vga clients.
82  * 	Determining the id requires the handler, so GPUs are given their
83  * 	true id in a delayed fashion in vga_switcheroo_enable()
84  * @VGA_SWITCHEROO_IGD: integrated graphics device
85  * @VGA_SWITCHEROO_DIS: discrete graphics device
86  * @VGA_SWITCHEROO_MAX_CLIENTS: currently no more than two GPUs are supported
87  *
88  * Client identifier. Audio clients use the same identifier & 0x100.
89  */
90 enum vga_switcheroo_client_id {
91 	VGA_SWITCHEROO_UNKNOWN_ID = 0x1000,
92 	VGA_SWITCHEROO_IGD = 0,
93 	VGA_SWITCHEROO_DIS,
94 	VGA_SWITCHEROO_MAX_CLIENTS,
95 };
96 
97 /**
98  * struct vga_switcheroo_handler - handler callbacks
99  * @init: initialize handler.
100  * 	Optional. This gets called when vga_switcheroo is enabled, i.e. when
101  * 	two vga clients have registered. It allows the handler to perform
102  * 	some delayed initialization that depends on the existence of the
103  * 	vga clients. Currently only the radeon and amdgpu drivers use this.
104  * 	The return value is ignored
105  * @switchto: switch outputs to given client.
106  * 	Mandatory. For muxless machines this should be a no-op. Returning 0
107  * 	denotes success, anything else failure (in which case the switch is
108  * 	aborted)
109  * @switch_ddc: switch DDC lines to given client.
110  * 	Optional. Should return the previous DDC owner on success or a
111  * 	negative int on failure
112  * @power_state: cut or reinstate power of given client.
113  * 	Optional. The return value is ignored
114  * @get_client_id: determine if given pci device is integrated or discrete GPU.
115  * 	Mandatory
116  *
117  * Handler callbacks. The multiplexer itself. The @switchto and @get_client_id
118  * methods are mandatory, all others may be set to NULL.
119  */
120 struct vga_switcheroo_handler {
121 	int (*init)(void);
122 	int (*switchto)(enum vga_switcheroo_client_id id);
123 	int (*switch_ddc)(enum vga_switcheroo_client_id id);
124 	int (*power_state)(enum vga_switcheroo_client_id id,
125 			   enum vga_switcheroo_state state);
126 	enum vga_switcheroo_client_id (*get_client_id)(struct pci_dev *pdev);
127 };
128 
129 /**
130  * struct vga_switcheroo_client_ops - client callbacks
131  * @set_gpu_state: do the equivalent of suspend/resume for the card.
132  * 	Mandatory. This should not cut power to the discrete GPU,
133  * 	which is the job of the handler
134  * @can_switch: check if the device is in a position to switch now.
135  * 	Mandatory. The client should return false if a user space process
136  * 	has one of its device files open
137  * @pre_switch: prepare switch
138  *	Optional. This gets called before switching the outputs to the
139  *	GPU. Allows drivers to prepare for the switch.
140  * @post_switch: completes switch
141  *	Optional. This gets called after waking the GPU and switching
142  *	the outputs to it. Allows drivers to poll the switched outputs.
143  * @gpu_bound: notify the client id to audio client when the GPU is bound.
144  *
145  * Client callbacks. A client can be either a GPU or an audio device on a GPU.
146  * The @set_gpu_state and @can_switch methods are mandatory, @pre_switch and
147  * @post_switch may be set to NULL. For audio clients, the @pre_switch and
148  * @post_switch members are bogus. OTOH, @gpu_bound is only for audio clients,
149  * and not used for GPU clients.
150  */
151 struct vga_switcheroo_client_ops {
152 	void (*set_gpu_state)(struct pci_dev *dev, enum vga_switcheroo_state);
153 	bool (*can_switch)(struct pci_dev *dev);
154 	void (*pre_switch)(struct pci_dev *dev);
155 	void (*post_switch)(struct pci_dev *dev);
156 	void (*gpu_bound)(struct pci_dev *dev, enum vga_switcheroo_client_id);
157 };
158 
159 #if defined(CONFIG_VGA_SWITCHEROO)
160 void vga_switcheroo_unregister_client(struct pci_dev *dev);
161 int vga_switcheroo_register_client(struct pci_dev *dev,
162 				   const struct vga_switcheroo_client_ops *ops,
163 				   bool driver_power_control);
164 int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
165 					 const struct vga_switcheroo_client_ops *ops,
166 					 struct pci_dev *vga_dev);
167 
168 int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
169 				    enum vga_switcheroo_handler_flags_t handler_flags);
170 void vga_switcheroo_unregister_handler(void);
171 enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void);
172 int vga_switcheroo_lock_ddc(struct pci_dev *pdev);
173 int vga_switcheroo_unlock_ddc(struct pci_dev *pdev);
174 
175 int vga_switcheroo_process_delayed_switch(void);
176 
177 bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev);
178 enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev);
179 
180 int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain);
181 void vga_switcheroo_fini_domain_pm_ops(struct device *dev);
182 #else
183 
184 static inline void vga_switcheroo_unregister_client(struct pci_dev *dev) {}
185 static inline int vga_switcheroo_register_client(struct pci_dev *dev,
186 		const struct vga_switcheroo_client_ops *ops, bool driver_power_control) { return 0; }
187 static inline int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
188 		enum vga_switcheroo_handler_flags_t handler_flags) { return 0; }
189 static inline int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
190 	const struct vga_switcheroo_client_ops *ops,
191 	struct pci_dev *vga_dev) { return 0; }
192 static inline void vga_switcheroo_unregister_handler(void) {}
193 static inline enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void) { return 0; }
194 static inline int vga_switcheroo_lock_ddc(struct pci_dev *pdev) { return -ENODEV; }
195 static inline int vga_switcheroo_unlock_ddc(struct pci_dev *pdev) { return -ENODEV; }
196 static inline int vga_switcheroo_process_delayed_switch(void) { return 0; }
197 static inline bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev) { return false; }
198 static inline enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev) { return VGA_SWITCHEROO_ON; }
199 
200 static inline int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain) { return -EINVAL; }
201 static inline void vga_switcheroo_fini_domain_pm_ops(struct device *dev) {}
202 
203 #endif
204 #endif /* _LINUX_VGA_SWITCHEROO_H_ */
205