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