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