xref: /linux/include/linux/vga_switcheroo.h (revision 3eb66e91a25497065c5322b1268cbc3953642227)
16a9ee8afSDave Airlie /*
2a645654bSLukas Wunner  * vga_switcheroo.h - Support for laptop with dual GPU using one set of outputs
3a645654bSLukas Wunner  *
46a9ee8afSDave Airlie  * Copyright (c) 2010 Red Hat Inc.
56a9ee8afSDave Airlie  * Author : Dave Airlie <airlied@redhat.com>
66a9ee8afSDave Airlie  *
7a645654bSLukas Wunner  * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
86a9ee8afSDave Airlie  *
9a645654bSLukas Wunner  * Permission is hereby granted, free of charge, to any person obtaining a
10a645654bSLukas Wunner  * copy of this software and associated documentation files (the "Software"),
11a645654bSLukas Wunner  * to deal in the Software without restriction, including without limitation
12a645654bSLukas Wunner  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13a645654bSLukas Wunner  * and/or sell copies of the Software, and to permit persons to whom the
14a645654bSLukas Wunner  * Software is furnished to do so, subject to the following conditions:
15a645654bSLukas Wunner  *
16a645654bSLukas Wunner  * The above copyright notice and this permission notice (including the next
17a645654bSLukas Wunner  * paragraph) shall be included in all copies or substantial portions of the
18a645654bSLukas Wunner  * Software.
19a645654bSLukas Wunner  *
20a645654bSLukas Wunner  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21a645654bSLukas Wunner  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22a645654bSLukas Wunner  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23a645654bSLukas Wunner  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24a645654bSLukas Wunner  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25a645654bSLukas Wunner  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26a645654bSLukas Wunner  * DEALINGS
27a645654bSLukas Wunner  * IN THE SOFTWARE.
28a645654bSLukas Wunner  *
296a9ee8afSDave Airlie  */
306a9ee8afSDave Airlie 
31d3decf3aSOzan Çağlayan #ifndef _LINUX_VGA_SWITCHEROO_H_
32d3decf3aSOzan Çağlayan #define _LINUX_VGA_SWITCHEROO_H_
33d3decf3aSOzan Çağlayan 
346a9ee8afSDave Airlie #include <linux/fb.h>
356a9ee8afSDave Airlie 
36f8fee8f5SRandy Dunlap struct pci_dev;
37f8fee8f5SRandy Dunlap 
38a645654bSLukas Wunner /**
39156d7d41SLukas Wunner  * enum vga_switcheroo_handler_flags_t - handler flags bitmask
40156d7d41SLukas Wunner  * @VGA_SWITCHEROO_CAN_SWITCH_DDC: whether the handler is able to switch the
41156d7d41SLukas Wunner  * 	DDC lines separately. This signals to clients that they should call
42156d7d41SLukas Wunner  * 	drm_get_edid_switcheroo() to probe the EDID
43156d7d41SLukas Wunner  * @VGA_SWITCHEROO_NEEDS_EDP_CONFIG: whether the handler is unable to switch
44156d7d41SLukas Wunner  * 	the AUX channel separately. This signals to clients that the active
45156d7d41SLukas Wunner  * 	GPU needs to train the link and communicate the link parameters to the
46156d7d41SLukas Wunner  * 	inactive GPU (mediated by vga_switcheroo). The inactive GPU may then
47156d7d41SLukas Wunner  * 	skip the AUX handshake and set up its output with these pre-calibrated
48156d7d41SLukas Wunner  * 	values (DisplayPort specification v1.1a, section 2.5.3.3)
49156d7d41SLukas Wunner  *
50156d7d41SLukas Wunner  * Handler flags bitmask. Used by handlers to declare their capabilities upon
51156d7d41SLukas Wunner  * registering with vga_switcheroo.
52156d7d41SLukas Wunner  */
53156d7d41SLukas Wunner enum vga_switcheroo_handler_flags_t {
54156d7d41SLukas Wunner 	VGA_SWITCHEROO_CAN_SWITCH_DDC	= (1 << 0),
55156d7d41SLukas Wunner 	VGA_SWITCHEROO_NEEDS_EDP_CONFIG	= (1 << 1),
56156d7d41SLukas Wunner };
57156d7d41SLukas Wunner 
58156d7d41SLukas Wunner /**
59a645654bSLukas Wunner  * enum vga_switcheroo_state - client power state
60a645654bSLukas Wunner  * @VGA_SWITCHEROO_OFF: off
61a645654bSLukas Wunner  * @VGA_SWITCHEROO_ON: on
62a645654bSLukas Wunner  * @VGA_SWITCHEROO_NOT_FOUND: client has not registered with vga_switcheroo.
63a645654bSLukas Wunner  * 	Only used in vga_switcheroo_get_client_state() which in turn is only
64a645654bSLukas Wunner  * 	called from hda_intel.c
65a645654bSLukas Wunner  *
66a645654bSLukas Wunner  * Client power state.
67a645654bSLukas Wunner  */
686a9ee8afSDave Airlie enum vga_switcheroo_state {
696a9ee8afSDave Airlie 	VGA_SWITCHEROO_OFF,
706a9ee8afSDave Airlie 	VGA_SWITCHEROO_ON,
71c8e9cf7bSTakashi Iwai 	/* below are referred only from vga_switcheroo_get_client_state() */
72c8e9cf7bSTakashi Iwai 	VGA_SWITCHEROO_NOT_FOUND,
736a9ee8afSDave Airlie };
746a9ee8afSDave Airlie 
75a645654bSLukas Wunner /**
76a645654bSLukas Wunner  * enum vga_switcheroo_client_id - client identifier
7721c5ba8cSLukas Wunner  * @VGA_SWITCHEROO_UNKNOWN_ID: initial identifier assigned to vga clients.
7821c5ba8cSLukas Wunner  * 	Determining the id requires the handler, so GPUs are given their
7921c5ba8cSLukas Wunner  * 	true id in a delayed fashion in vga_switcheroo_enable()
80a645654bSLukas Wunner  * @VGA_SWITCHEROO_IGD: integrated graphics device
81a645654bSLukas Wunner  * @VGA_SWITCHEROO_DIS: discrete graphics device
82a645654bSLukas Wunner  * @VGA_SWITCHEROO_MAX_CLIENTS: currently no more than two GPUs are supported
83a645654bSLukas Wunner  *
84a645654bSLukas Wunner  * Client identifier. Audio clients use the same identifier & 0x100.
85a645654bSLukas Wunner  */
866a9ee8afSDave Airlie enum vga_switcheroo_client_id {
874aaf448fSJim Qu 	VGA_SWITCHEROO_UNKNOWN_ID = 0x1000,
884aaf448fSJim Qu 	VGA_SWITCHEROO_IGD = 0,
896a9ee8afSDave Airlie 	VGA_SWITCHEROO_DIS,
906a9ee8afSDave Airlie 	VGA_SWITCHEROO_MAX_CLIENTS,
916a9ee8afSDave Airlie };
926a9ee8afSDave Airlie 
93a645654bSLukas Wunner /**
94a645654bSLukas Wunner  * struct vga_switcheroo_handler - handler callbacks
95a645654bSLukas Wunner  * @init: initialize handler.
96a645654bSLukas Wunner  * 	Optional. This gets called when vga_switcheroo is enabled, i.e. when
97a645654bSLukas Wunner  * 	two vga clients have registered. It allows the handler to perform
98a645654bSLukas Wunner  * 	some delayed initialization that depends on the existence of the
99a645654bSLukas Wunner  * 	vga clients. Currently only the radeon and amdgpu drivers use this.
100a645654bSLukas Wunner  * 	The return value is ignored
101a645654bSLukas Wunner  * @switchto: switch outputs to given client.
102a645654bSLukas Wunner  * 	Mandatory. For muxless machines this should be a no-op. Returning 0
103a645654bSLukas Wunner  * 	denotes success, anything else failure (in which case the switch is
104a645654bSLukas Wunner  * 	aborted)
105e4cb81d7SLukas Wunner  * @switch_ddc: switch DDC lines to given client.
106e4cb81d7SLukas Wunner  * 	Optional. Should return the previous DDC owner on success or a
107e4cb81d7SLukas Wunner  * 	negative int on failure
108a645654bSLukas Wunner  * @power_state: cut or reinstate power of given client.
109a645654bSLukas Wunner  * 	Optional. The return value is ignored
110a645654bSLukas Wunner  * @get_client_id: determine if given pci device is integrated or discrete GPU.
111a645654bSLukas Wunner  * 	Mandatory
112a645654bSLukas Wunner  *
113a645654bSLukas Wunner  * Handler callbacks. The multiplexer itself. The @switchto and @get_client_id
114a645654bSLukas Wunner  * methods are mandatory, all others may be set to NULL.
115a645654bSLukas Wunner  */
1166a9ee8afSDave Airlie struct vga_switcheroo_handler {
117a645654bSLukas Wunner 	int (*init)(void);
1186a9ee8afSDave Airlie 	int (*switchto)(enum vga_switcheroo_client_id id);
119e4cb81d7SLukas Wunner 	int (*switch_ddc)(enum vga_switcheroo_client_id id);
1206a9ee8afSDave Airlie 	int (*power_state)(enum vga_switcheroo_client_id id,
1216a9ee8afSDave Airlie 			   enum vga_switcheroo_state state);
122fa3e967fSLukas Wunner 	enum vga_switcheroo_client_id (*get_client_id)(struct pci_dev *pdev);
1236a9ee8afSDave Airlie };
1246a9ee8afSDave Airlie 
125a645654bSLukas Wunner /**
126a645654bSLukas Wunner  * struct vga_switcheroo_client_ops - client callbacks
127a645654bSLukas Wunner  * @set_gpu_state: do the equivalent of suspend/resume for the card.
128a645654bSLukas Wunner  * 	Mandatory. This should not cut power to the discrete GPU,
129a645654bSLukas Wunner  * 	which is the job of the handler
130a645654bSLukas Wunner  * @reprobe: poll outputs.
131a645654bSLukas Wunner  * 	Optional. This gets called after waking the GPU and switching
132a645654bSLukas Wunner  * 	the outputs to it
133a645654bSLukas Wunner  * @can_switch: check if the device is in a position to switch now.
134a645654bSLukas Wunner  * 	Mandatory. The client should return false if a user space process
135a645654bSLukas Wunner  * 	has one of its device files open
136*37a3a98eSTakashi Iwai  * @gpu_bound: notify the client id to audio client when the GPU is bound.
137a645654bSLukas Wunner  *
138a645654bSLukas Wunner  * Client callbacks. A client can be either a GPU or an audio device on a GPU.
139a645654bSLukas Wunner  * The @set_gpu_state and @can_switch methods are mandatory, @reprobe may be
140a645654bSLukas Wunner  * set to NULL. For audio clients, the @reprobe member is bogus.
141*37a3a98eSTakashi Iwai  * OTOH, @gpu_bound is only for audio clients, and not used for GPU clients.
142a645654bSLukas Wunner  */
14326ec685fSTakashi Iwai struct vga_switcheroo_client_ops {
14426ec685fSTakashi Iwai 	void (*set_gpu_state)(struct pci_dev *dev, enum vga_switcheroo_state);
14526ec685fSTakashi Iwai 	void (*reprobe)(struct pci_dev *dev);
14626ec685fSTakashi Iwai 	bool (*can_switch)(struct pci_dev *dev);
147*37a3a98eSTakashi Iwai 	void (*gpu_bound)(struct pci_dev *dev, enum vga_switcheroo_client_id);
14826ec685fSTakashi Iwai };
1496a9ee8afSDave Airlie 
1506a9ee8afSDave Airlie #if defined(CONFIG_VGA_SWITCHEROO)
1516a9ee8afSDave Airlie void vga_switcheroo_unregister_client(struct pci_dev *dev);
1526a9ee8afSDave Airlie int vga_switcheroo_register_client(struct pci_dev *dev,
1530d69704aSDave Airlie 				   const struct vga_switcheroo_client_ops *ops,
1540d69704aSDave Airlie 				   bool driver_power_control);
1553e9e63dbSTakashi Iwai int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
1563e9e63dbSTakashi Iwai 					 const struct vga_switcheroo_client_ops *ops,
1574aaf448fSJim Qu 					 struct pci_dev *vga_dev);
1586a9ee8afSDave Airlie 
1596a9ee8afSDave Airlie void vga_switcheroo_client_fb_set(struct pci_dev *dev,
1606a9ee8afSDave Airlie 				  struct fb_info *info);
1616a9ee8afSDave Airlie 
162156d7d41SLukas Wunner int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
163156d7d41SLukas Wunner 				    enum vga_switcheroo_handler_flags_t handler_flags);
1646a9ee8afSDave Airlie void vga_switcheroo_unregister_handler(void);
165156d7d41SLukas Wunner enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void);
166e4cb81d7SLukas Wunner int vga_switcheroo_lock_ddc(struct pci_dev *pdev);
167e4cb81d7SLukas Wunner int vga_switcheroo_unlock_ddc(struct pci_dev *pdev);
1686a9ee8afSDave Airlie 
1696a9ee8afSDave Airlie int vga_switcheroo_process_delayed_switch(void);
1706a9ee8afSDave Airlie 
171b00e5334SLukas Wunner bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev);
172203d027dSLukas Wunner enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev);
173c8e9cf7bSTakashi Iwai 
1740d69704aSDave Airlie int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain);
175766a53d0SAlex Deucher void vga_switcheroo_fini_domain_pm_ops(struct device *dev);
1766a9ee8afSDave Airlie #else
1776a9ee8afSDave Airlie 
vga_switcheroo_unregister_client(struct pci_dev * dev)1786a9ee8afSDave Airlie static inline void vga_switcheroo_unregister_client(struct pci_dev *dev) {}
vga_switcheroo_register_client(struct pci_dev * dev,const struct vga_switcheroo_client_ops * ops,bool driver_power_control)1796a9ee8afSDave Airlie static inline int vga_switcheroo_register_client(struct pci_dev *dev,
1800d69704aSDave Airlie 		const struct vga_switcheroo_client_ops *ops, bool driver_power_control) { return 0; }
vga_switcheroo_client_fb_set(struct pci_dev * dev,struct fb_info * info)1816a9ee8afSDave Airlie static inline void vga_switcheroo_client_fb_set(struct pci_dev *dev, struct fb_info *info) {}
vga_switcheroo_register_handler(const struct vga_switcheroo_handler * handler,enum vga_switcheroo_handler_flags_t handler_flags)182156d7d41SLukas Wunner static inline int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler,
183156d7d41SLukas Wunner 		enum vga_switcheroo_handler_flags_t handler_flags) { return 0; }
vga_switcheroo_register_audio_client(struct pci_dev * pdev,const struct vga_switcheroo_client_ops * ops,struct pci_dev * vga_dev)1843e9e63dbSTakashi Iwai static inline int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
1853e9e63dbSTakashi Iwai 	const struct vga_switcheroo_client_ops *ops,
1864aaf448fSJim Qu 	struct pci_dev *vga_dev) { return 0; }
vga_switcheroo_unregister_handler(void)1876a9ee8afSDave Airlie static inline void vga_switcheroo_unregister_handler(void) {}
vga_switcheroo_handler_flags(void)188156d7d41SLukas Wunner static inline enum vga_switcheroo_handler_flags_t vga_switcheroo_handler_flags(void) { return 0; }
vga_switcheroo_lock_ddc(struct pci_dev * pdev)189e4cb81d7SLukas Wunner static inline int vga_switcheroo_lock_ddc(struct pci_dev *pdev) { return -ENODEV; }
vga_switcheroo_unlock_ddc(struct pci_dev * pdev)190e4cb81d7SLukas Wunner static inline int vga_switcheroo_unlock_ddc(struct pci_dev *pdev) { return -ENODEV; }
vga_switcheroo_process_delayed_switch(void)1916a9ee8afSDave Airlie static inline int vga_switcheroo_process_delayed_switch(void) { return 0; }
vga_switcheroo_client_probe_defer(struct pci_dev * pdev)192b00e5334SLukas Wunner static inline bool vga_switcheroo_client_probe_defer(struct pci_dev *pdev) { return false; }
vga_switcheroo_get_client_state(struct pci_dev * dev)193203d027dSLukas Wunner static inline enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *dev) { return VGA_SWITCHEROO_ON; }
194c8e9cf7bSTakashi Iwai 
vga_switcheroo_init_domain_pm_ops(struct device * dev,struct dev_pm_domain * domain)1950d69704aSDave Airlie static inline int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain) { return -EINVAL; }
vga_switcheroo_fini_domain_pm_ops(struct device * dev)196766a53d0SAlex Deucher static inline void vga_switcheroo_fini_domain_pm_ops(struct device *dev) {}
1976a9ee8afSDave Airlie 
1986a9ee8afSDave Airlie #endif
199d3decf3aSOzan Çağlayan #endif /* _LINUX_VGA_SWITCHEROO_H_ */
200