Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Red Hat Inc. |
| 3 | * Author : Dave Airlie <airlied@redhat.com> |
| 4 | * |
| 5 | * |
| 6 | * Licensed under GPLv2 |
| 7 | * |
| 8 | * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs |
Thierry Reding | 7130927 | 2015-08-12 16:32:09 +0200 | [diff] [blame] | 9 | * |
| 10 | * Switcher interface - methods require for ATPX and DCM |
| 11 | * - switchto - this throws the output MUX switch |
| 12 | * - discrete_set_power - sets the power state for the discrete card |
| 13 | * |
| 14 | * GPU driver interface |
| 15 | * - set_gpu_state - this should do the equiv of s/r for the card |
| 16 | * - this should *not* set the discrete power state |
| 17 | * - switch_check - check if the device is in a position to switch now |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 18 | */ |
| 19 | |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 20 | #define pr_fmt(fmt) "vga_switcheroo: " fmt |
| 21 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 22 | #include <linux/module.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 23 | #include <linux/seq_file.h> |
| 24 | #include <linux/uaccess.h> |
| 25 | #include <linux/fs.h> |
| 26 | #include <linux/debugfs.h> |
| 27 | #include <linux/fb.h> |
| 28 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 29 | #include <linux/pci.h> |
Dave Airlie | 054430e | 2013-01-25 11:38:56 +1000 | [diff] [blame] | 30 | #include <linux/console.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 31 | #include <linux/vga_switcheroo.h> |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 32 | #include <linux/pm_runtime.h> |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 33 | |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 34 | #include <linux/vgaarb.h> |
| 35 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 36 | struct vga_switcheroo_client { |
| 37 | struct pci_dev *pdev; |
| 38 | struct fb_info *fb_info; |
| 39 | int pwr_state; |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 40 | const struct vga_switcheroo_client_ops *ops; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 41 | int id; |
| 42 | bool active; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 43 | bool driver_power_control; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 44 | struct list_head list; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static DEFINE_MUTEX(vgasr_mutex); |
| 48 | |
| 49 | struct vgasr_priv { |
| 50 | |
| 51 | bool active; |
| 52 | bool delayed_switch_active; |
| 53 | enum vga_switcheroo_client_id delayed_client_id; |
| 54 | |
| 55 | struct dentry *debugfs_root; |
| 56 | struct dentry *switch_file; |
| 57 | |
| 58 | int registered_clients; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 59 | struct list_head clients; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 60 | |
| 61 | struct vga_switcheroo_handler *handler; |
| 62 | }; |
| 63 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 64 | #define ID_BIT_AUDIO 0x100 |
| 65 | #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO) |
| 66 | #define client_is_vga(c) ((c)->id == -1 || !client_is_audio(c)) |
| 67 | #define client_id(c) ((c)->id & ~ID_BIT_AUDIO) |
| 68 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 69 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv); |
| 70 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv); |
| 71 | |
| 72 | /* only one switcheroo per system */ |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 73 | static struct vgasr_priv vgasr_priv = { |
| 74 | .clients = LIST_HEAD_INIT(vgasr_priv.clients), |
| 75 | }; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 76 | |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 77 | static bool vga_switcheroo_ready(void) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 78 | { |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 79 | /* we're ready if we get two clients + handler */ |
| 80 | return !vgasr_priv.active && |
| 81 | vgasr_priv.registered_clients == 2 && vgasr_priv.handler; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 82 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 83 | |
| 84 | static void vga_switcheroo_enable(void) |
| 85 | { |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 86 | int ret; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 87 | struct vga_switcheroo_client *client; |
| 88 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 89 | /* call the handler to init */ |
Seth Forshee | e99eac5 | 2012-08-17 11:17:02 -0500 | [diff] [blame] | 90 | if (vgasr_priv.handler->init) |
| 91 | vgasr_priv.handler->init(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 92 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 93 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 94 | if (client->id != -1) |
| 95 | continue; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 96 | ret = vgasr_priv.handler->get_client_id(client->pdev); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 97 | if (ret < 0) |
| 98 | return; |
| 99 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 100 | client->id = ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 101 | } |
| 102 | vga_switcheroo_debugfs_init(&vgasr_priv); |
| 103 | vgasr_priv.active = true; |
| 104 | } |
| 105 | |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 106 | int vga_switcheroo_register_handler(struct vga_switcheroo_handler *handler) |
| 107 | { |
| 108 | mutex_lock(&vgasr_mutex); |
| 109 | if (vgasr_priv.handler) { |
| 110 | mutex_unlock(&vgasr_mutex); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
| 114 | vgasr_priv.handler = handler; |
| 115 | if (vga_switcheroo_ready()) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 116 | pr_info("enabled\n"); |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 117 | vga_switcheroo_enable(); |
| 118 | } |
| 119 | mutex_unlock(&vgasr_mutex); |
| 120 | return 0; |
| 121 | } |
| 122 | EXPORT_SYMBOL(vga_switcheroo_register_handler); |
| 123 | |
| 124 | void vga_switcheroo_unregister_handler(void) |
| 125 | { |
| 126 | mutex_lock(&vgasr_mutex); |
| 127 | vgasr_priv.handler = NULL; |
| 128 | if (vgasr_priv.active) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 129 | pr_info("disabled\n"); |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 130 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
| 131 | vgasr_priv.active = false; |
| 132 | } |
| 133 | mutex_unlock(&vgasr_mutex); |
| 134 | } |
| 135 | EXPORT_SYMBOL(vga_switcheroo_unregister_handler); |
| 136 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 137 | static int register_client(struct pci_dev *pdev, |
| 138 | const struct vga_switcheroo_client_ops *ops, |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 139 | int id, bool active, bool driver_power_control) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 140 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 141 | struct vga_switcheroo_client *client; |
| 142 | |
| 143 | client = kzalloc(sizeof(*client), GFP_KERNEL); |
| 144 | if (!client) |
| 145 | return -ENOMEM; |
| 146 | |
| 147 | client->pwr_state = VGA_SWITCHEROO_ON; |
| 148 | client->pdev = pdev; |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 149 | client->ops = ops; |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 150 | client->id = id; |
| 151 | client->active = active; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 152 | client->driver_power_control = driver_power_control; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 153 | |
| 154 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 155 | list_add_tail(&client->list, &vgasr_priv.clients); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 156 | if (client_is_vga(client)) |
| 157 | vgasr_priv.registered_clients++; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 158 | |
Seth Forshee | 36704c0 | 2012-08-17 11:17:03 -0500 | [diff] [blame] | 159 | if (vga_switcheroo_ready()) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 160 | pr_info("enabled\n"); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 161 | vga_switcheroo_enable(); |
| 162 | } |
| 163 | mutex_unlock(&vgasr_mutex); |
| 164 | return 0; |
| 165 | } |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 166 | |
| 167 | int vga_switcheroo_register_client(struct pci_dev *pdev, |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 168 | const struct vga_switcheroo_client_ops *ops, |
| 169 | bool driver_power_control) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 170 | { |
| 171 | return register_client(pdev, ops, -1, |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 172 | pdev == vga_default_device(), |
| 173 | driver_power_control); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 174 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 175 | EXPORT_SYMBOL(vga_switcheroo_register_client); |
| 176 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 177 | int vga_switcheroo_register_audio_client(struct pci_dev *pdev, |
| 178 | const struct vga_switcheroo_client_ops *ops, |
| 179 | int id, bool active) |
| 180 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 181 | return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 182 | } |
| 183 | EXPORT_SYMBOL(vga_switcheroo_register_audio_client); |
| 184 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 185 | static struct vga_switcheroo_client * |
| 186 | find_client_from_pci(struct list_head *head, struct pci_dev *pdev) |
| 187 | { |
| 188 | struct vga_switcheroo_client *client; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 189 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 190 | list_for_each_entry(client, head, list) |
| 191 | if (client->pdev == pdev) |
| 192 | return client; |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static struct vga_switcheroo_client * |
| 197 | find_client_from_id(struct list_head *head, int client_id) |
| 198 | { |
| 199 | struct vga_switcheroo_client *client; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 200 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 201 | list_for_each_entry(client, head, list) |
| 202 | if (client->id == client_id) |
| 203 | return client; |
| 204 | return NULL; |
| 205 | } |
| 206 | |
| 207 | static struct vga_switcheroo_client * |
| 208 | find_active_client(struct list_head *head) |
| 209 | { |
| 210 | struct vga_switcheroo_client *client; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 211 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 212 | list_for_each_entry(client, head, list) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 213 | if (client->active && client_is_vga(client)) |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 214 | return client; |
| 215 | return NULL; |
| 216 | } |
| 217 | |
Takashi Iwai | c8e9cf7 | 2012-06-07 12:15:15 +0200 | [diff] [blame] | 218 | int vga_switcheroo_get_client_state(struct pci_dev *pdev) |
| 219 | { |
| 220 | struct vga_switcheroo_client *client; |
| 221 | |
| 222 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 223 | if (!client) |
| 224 | return VGA_SWITCHEROO_NOT_FOUND; |
| 225 | if (!vgasr_priv.active) |
| 226 | return VGA_SWITCHEROO_INIT; |
| 227 | return client->pwr_state; |
| 228 | } |
| 229 | EXPORT_SYMBOL(vga_switcheroo_get_client_state); |
| 230 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 231 | void vga_switcheroo_unregister_client(struct pci_dev *pdev) |
| 232 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 233 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 234 | |
| 235 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 236 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 237 | if (client) { |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 238 | if (client_is_vga(client)) |
| 239 | vgasr_priv.registered_clients--; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 240 | list_del(&client->list); |
| 241 | kfree(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 242 | } |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 243 | if (vgasr_priv.active && vgasr_priv.registered_clients < 2) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 244 | pr_info("disabled\n"); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 245 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
| 246 | vgasr_priv.active = false; |
| 247 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 248 | mutex_unlock(&vgasr_mutex); |
| 249 | } |
| 250 | EXPORT_SYMBOL(vga_switcheroo_unregister_client); |
| 251 | |
| 252 | void vga_switcheroo_client_fb_set(struct pci_dev *pdev, |
| 253 | struct fb_info *info) |
| 254 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 255 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 256 | |
| 257 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 258 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 259 | if (client) |
| 260 | client->fb_info = info; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 261 | mutex_unlock(&vgasr_mutex); |
| 262 | } |
| 263 | EXPORT_SYMBOL(vga_switcheroo_client_fb_set); |
| 264 | |
| 265 | static int vga_switcheroo_show(struct seq_file *m, void *v) |
| 266 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 267 | struct vga_switcheroo_client *client; |
| 268 | int i = 0; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 269 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 270 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 271 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 272 | seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i, |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 273 | client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : |
| 274 | "IGD", |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 275 | client_is_vga(client) ? "" : "-Audio", |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 276 | client->active ? '+' : ' ', |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 277 | client->driver_power_control ? "Dyn" : "", |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 278 | client->pwr_state ? "Pwr" : "Off", |
| 279 | pci_name(client->pdev)); |
| 280 | i++; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 281 | } |
| 282 | mutex_unlock(&vgasr_mutex); |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file) |
| 287 | { |
| 288 | return single_open(file, vga_switcheroo_show, NULL); |
| 289 | } |
| 290 | |
| 291 | static int vga_switchon(struct vga_switcheroo_client *client) |
| 292 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 293 | if (client->driver_power_control) |
| 294 | return 0; |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 295 | if (vgasr_priv.handler->power_state) |
| 296 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 297 | /* call the driver callback to turn on device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 298 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 299 | client->pwr_state = VGA_SWITCHEROO_ON; |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int vga_switchoff(struct vga_switcheroo_client *client) |
| 304 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 305 | if (client->driver_power_control) |
| 306 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 307 | /* call the driver callback to turn off device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 308 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF); |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 309 | if (vgasr_priv.handler->power_state) |
| 310 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 311 | client->pwr_state = VGA_SWITCHEROO_OFF; |
| 312 | return 0; |
| 313 | } |
| 314 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 315 | static void set_audio_state(int id, int state) |
| 316 | { |
| 317 | struct vga_switcheroo_client *client; |
| 318 | |
| 319 | client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO); |
| 320 | if (client && client->pwr_state != state) { |
| 321 | client->ops->set_gpu_state(client->pdev, state); |
| 322 | client->pwr_state = state; |
| 323 | } |
| 324 | } |
| 325 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 326 | /* stage one happens before delay */ |
| 327 | static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 328 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 329 | struct vga_switcheroo_client *active; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 330 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 331 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 332 | if (!active) |
| 333 | return 0; |
| 334 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 335 | if (new_client->pwr_state == VGA_SWITCHEROO_OFF) |
| 336 | vga_switchon(new_client); |
| 337 | |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 338 | vga_set_default_device(new_client->pdev); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* post delay */ |
| 343 | static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) |
| 344 | { |
| 345 | int ret; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 346 | struct vga_switcheroo_client *active; |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 347 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 348 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 349 | if (!active) |
| 350 | return 0; |
| 351 | |
| 352 | active->active = false; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 353 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 354 | set_audio_state(active->id, VGA_SWITCHEROO_OFF); |
| 355 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 356 | if (new_client->fb_info) { |
| 357 | struct fb_event event; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 358 | |
Dave Airlie | 054430e | 2013-01-25 11:38:56 +1000 | [diff] [blame] | 359 | console_lock(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 360 | event.info = new_client->fb_info; |
| 361 | fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event); |
Dave Airlie | 054430e | 2013-01-25 11:38:56 +1000 | [diff] [blame] | 362 | console_unlock(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | ret = vgasr_priv.handler->switchto(new_client->id); |
| 366 | if (ret) |
| 367 | return ret; |
| 368 | |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 369 | if (new_client->ops->reprobe) |
| 370 | new_client->ops->reprobe(new_client->pdev); |
Dave Airlie | 8d608aa | 2010-12-07 08:57:57 +1000 | [diff] [blame] | 371 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 372 | if (active->pwr_state == VGA_SWITCHEROO_ON) |
| 373 | vga_switchoff(active); |
| 374 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 375 | set_audio_state(new_client->id, VGA_SWITCHEROO_ON); |
| 376 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 377 | new_client->active = true; |
| 378 | return 0; |
| 379 | } |
| 380 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 381 | static bool check_can_switch(void) |
| 382 | { |
| 383 | struct vga_switcheroo_client *client; |
| 384 | |
| 385 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 386 | if (!client->ops->can_switch(client->pdev)) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 387 | pr_err("client %x refused switch\n", client->id); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 388 | return false; |
| 389 | } |
| 390 | } |
| 391 | return true; |
| 392 | } |
| 393 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 394 | static ssize_t |
| 395 | vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, |
| 396 | size_t cnt, loff_t *ppos) |
| 397 | { |
| 398 | char usercmd[64]; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 399 | int ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 400 | bool delay = false, can_switch; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 401 | bool just_mux = false; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 402 | int client_id = -1; |
| 403 | struct vga_switcheroo_client *client = NULL; |
| 404 | |
| 405 | if (cnt > 63) |
| 406 | cnt = 63; |
| 407 | |
| 408 | if (copy_from_user(usercmd, ubuf, cnt)) |
| 409 | return -EFAULT; |
| 410 | |
| 411 | mutex_lock(&vgasr_mutex); |
| 412 | |
Jiri Slaby | 8c88e50 | 2010-04-27 14:11:03 -0700 | [diff] [blame] | 413 | if (!vgasr_priv.active) { |
| 414 | cnt = -EINVAL; |
| 415 | goto out; |
| 416 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 417 | |
| 418 | /* pwr off the device not in use */ |
| 419 | if (strncmp(usercmd, "OFF", 3) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 420 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 421 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 422 | continue; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 423 | if (client->driver_power_control) |
| 424 | continue; |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 425 | set_audio_state(client->id, VGA_SWITCHEROO_OFF); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 426 | if (client->pwr_state == VGA_SWITCHEROO_ON) |
| 427 | vga_switchoff(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 428 | } |
| 429 | goto out; |
| 430 | } |
| 431 | /* pwr on the device not in use */ |
| 432 | if (strncmp(usercmd, "ON", 2) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 433 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 434 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 435 | continue; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 436 | if (client->driver_power_control) |
| 437 | continue; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 438 | if (client->pwr_state == VGA_SWITCHEROO_OFF) |
| 439 | vga_switchon(client); |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 440 | set_audio_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 441 | } |
| 442 | goto out; |
| 443 | } |
| 444 | |
| 445 | /* request a delayed switch - test can we switch now */ |
| 446 | if (strncmp(usercmd, "DIGD", 4) == 0) { |
| 447 | client_id = VGA_SWITCHEROO_IGD; |
| 448 | delay = true; |
| 449 | } |
| 450 | |
| 451 | if (strncmp(usercmd, "DDIS", 4) == 0) { |
| 452 | client_id = VGA_SWITCHEROO_DIS; |
| 453 | delay = true; |
| 454 | } |
| 455 | |
| 456 | if (strncmp(usercmd, "IGD", 3) == 0) |
| 457 | client_id = VGA_SWITCHEROO_IGD; |
| 458 | |
| 459 | if (strncmp(usercmd, "DIS", 3) == 0) |
| 460 | client_id = VGA_SWITCHEROO_DIS; |
| 461 | |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 462 | if (strncmp(usercmd, "MIGD", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 463 | just_mux = true; |
| 464 | client_id = VGA_SWITCHEROO_IGD; |
| 465 | } |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 466 | if (strncmp(usercmd, "MDIS", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 467 | just_mux = true; |
| 468 | client_id = VGA_SWITCHEROO_DIS; |
| 469 | } |
| 470 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 471 | if (client_id == -1) |
| 472 | goto out; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 473 | client = find_client_from_id(&vgasr_priv.clients, client_id); |
| 474 | if (!client) |
| 475 | goto out; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 476 | |
| 477 | vgasr_priv.delayed_switch_active = false; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 478 | |
| 479 | if (just_mux) { |
| 480 | ret = vgasr_priv.handler->switchto(client_id); |
| 481 | goto out; |
| 482 | } |
| 483 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 484 | if (client->active) |
Florian Mickler | a67b888 | 2011-05-15 16:32:50 +0200 | [diff] [blame] | 485 | goto out; |
| 486 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 487 | /* okay we want a switch - test if devices are willing to switch */ |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 488 | can_switch = check_can_switch(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 489 | |
| 490 | if (can_switch == false && delay == false) |
| 491 | goto out; |
| 492 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 493 | if (can_switch) { |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 494 | ret = vga_switchto_stage1(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 495 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 496 | pr_err("switching failed stage 1 %d\n", ret); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 497 | |
| 498 | ret = vga_switchto_stage2(client); |
| 499 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 500 | pr_err("switching failed stage 2 %d\n", ret); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 501 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 502 | } else { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 503 | pr_info("setting delayed switch to client %d\n", client->id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 504 | vgasr_priv.delayed_switch_active = true; |
| 505 | vgasr_priv.delayed_client_id = client_id; |
| 506 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 507 | ret = vga_switchto_stage1(client); |
| 508 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 509 | pr_err("delayed switching stage 1 failed %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | out: |
| 513 | mutex_unlock(&vgasr_mutex); |
| 514 | return cnt; |
| 515 | } |
| 516 | |
| 517 | static const struct file_operations vga_switcheroo_debugfs_fops = { |
| 518 | .owner = THIS_MODULE, |
| 519 | .open = vga_switcheroo_debugfs_open, |
| 520 | .write = vga_switcheroo_debugfs_write, |
| 521 | .read = seq_read, |
| 522 | .llseek = seq_lseek, |
| 523 | .release = single_release, |
| 524 | }; |
| 525 | |
| 526 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv) |
| 527 | { |
| 528 | if (priv->switch_file) { |
| 529 | debugfs_remove(priv->switch_file); |
| 530 | priv->switch_file = NULL; |
| 531 | } |
| 532 | if (priv->debugfs_root) { |
| 533 | debugfs_remove(priv->debugfs_root); |
| 534 | priv->debugfs_root = NULL; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv) |
| 539 | { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 540 | static const char mp[] = "/sys/kernel/debug"; |
| 541 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 542 | /* already initialised */ |
| 543 | if (priv->debugfs_root) |
| 544 | return 0; |
| 545 | priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL); |
| 546 | |
| 547 | if (!priv->debugfs_root) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 548 | pr_err("Cannot create %s/vgaswitcheroo\n", mp); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 549 | goto fail; |
| 550 | } |
| 551 | |
| 552 | priv->switch_file = debugfs_create_file("switch", 0644, |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 553 | priv->debugfs_root, NULL, |
| 554 | &vga_switcheroo_debugfs_fops); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 555 | if (!priv->switch_file) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 556 | pr_err("cannot create %s/vgaswitcheroo/switch\n", mp); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 557 | goto fail; |
| 558 | } |
| 559 | return 0; |
| 560 | fail: |
| 561 | vga_switcheroo_debugfs_fini(priv); |
| 562 | return -1; |
| 563 | } |
| 564 | |
| 565 | int vga_switcheroo_process_delayed_switch(void) |
| 566 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 567 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 568 | int ret; |
| 569 | int err = -EINVAL; |
| 570 | |
| 571 | mutex_lock(&vgasr_mutex); |
| 572 | if (!vgasr_priv.delayed_switch_active) |
| 573 | goto err; |
| 574 | |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 575 | pr_info("processing delayed switch to %d\n", |
| 576 | vgasr_priv.delayed_client_id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 577 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 578 | client = find_client_from_id(&vgasr_priv.clients, |
| 579 | vgasr_priv.delayed_client_id); |
| 580 | if (!client || !check_can_switch()) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 581 | goto err; |
| 582 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 583 | ret = vga_switchto_stage2(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 584 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame] | 585 | pr_err("delayed switching failed stage 2 %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 586 | |
| 587 | vgasr_priv.delayed_switch_active = false; |
| 588 | err = 0; |
| 589 | err: |
| 590 | mutex_unlock(&vgasr_mutex); |
| 591 | return err; |
| 592 | } |
| 593 | EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 594 | |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 595 | static void vga_switcheroo_power_switch(struct pci_dev *pdev, |
| 596 | enum vga_switcheroo_state state) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 597 | { |
| 598 | struct vga_switcheroo_client *client; |
| 599 | |
| 600 | if (!vgasr_priv.handler->power_state) |
| 601 | return; |
| 602 | |
| 603 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 604 | if (!client) |
| 605 | return; |
| 606 | |
| 607 | if (!client->driver_power_control) |
| 608 | return; |
| 609 | |
| 610 | vgasr_priv.handler->power_state(client->id, state); |
| 611 | } |
| 612 | |
| 613 | /* force a PCI device to a certain state - mainly to turn off audio clients */ |
| 614 | |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 615 | void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, |
| 616 | enum vga_switcheroo_state dynamic) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 617 | { |
| 618 | struct vga_switcheroo_client *client; |
| 619 | |
| 620 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 621 | if (!client) |
| 622 | return; |
| 623 | |
| 624 | if (!client->driver_power_control) |
| 625 | return; |
| 626 | |
| 627 | client->pwr_state = dynamic; |
| 628 | set_audio_state(client->id, dynamic); |
| 629 | } |
| 630 | EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch); |
| 631 | |
| 632 | /* switcheroo power domain */ |
| 633 | static int vga_switcheroo_runtime_suspend(struct device *dev) |
| 634 | { |
| 635 | struct pci_dev *pdev = to_pci_dev(dev); |
| 636 | int ret; |
| 637 | |
| 638 | ret = dev->bus->pm->runtime_suspend(dev); |
| 639 | if (ret) |
| 640 | return ret; |
Alex Deucher | f2bc56161 | 2014-05-19 14:04:43 -0400 | [diff] [blame] | 641 | if (vgasr_priv.handler->switchto) |
| 642 | vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 643 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int vga_switcheroo_runtime_resume(struct device *dev) |
| 648 | { |
| 649 | struct pci_dev *pdev = to_pci_dev(dev); |
| 650 | int ret; |
| 651 | |
| 652 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON); |
| 653 | ret = dev->bus->pm->runtime_resume(dev); |
| 654 | if (ret) |
| 655 | return ret; |
| 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | /* this version is for the case where the power switch is separate |
| 661 | to the device being powered down. */ |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 662 | int vga_switcheroo_init_domain_pm_ops(struct device *dev, |
| 663 | struct dev_pm_domain *domain) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 664 | { |
| 665 | /* copy over all the bus versions */ |
| 666 | if (dev->bus && dev->bus->pm) { |
| 667 | domain->ops = *dev->bus->pm; |
| 668 | domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend; |
| 669 | domain->ops.runtime_resume = vga_switcheroo_runtime_resume; |
| 670 | |
| 671 | dev->pm_domain = domain; |
| 672 | return 0; |
| 673 | } |
| 674 | dev->pm_domain = NULL; |
| 675 | return -EINVAL; |
| 676 | } |
| 677 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops); |
| 678 | |
Alex Deucher | 766a53d | 2014-09-12 17:51:29 -0400 | [diff] [blame] | 679 | void vga_switcheroo_fini_domain_pm_ops(struct device *dev) |
| 680 | { |
| 681 | dev->pm_domain = NULL; |
| 682 | } |
| 683 | EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops); |
| 684 | |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 685 | static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev) |
| 686 | { |
| 687 | struct pci_dev *pdev = to_pci_dev(dev); |
| 688 | int ret; |
| 689 | struct vga_switcheroo_client *client, *found = NULL; |
| 690 | |
| 691 | /* we need to check if we have to switch back on the video |
| 692 | device so the audio device can come back */ |
| 693 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 694 | if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) && |
| 695 | client_is_vga(client)) { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 696 | found = client; |
| 697 | ret = pm_runtime_get_sync(&client->pdev->dev); |
| 698 | if (ret) { |
| 699 | if (ret != 1) |
| 700 | return ret; |
| 701 | } |
| 702 | break; |
| 703 | } |
| 704 | } |
| 705 | ret = dev->bus->pm->runtime_resume(dev); |
| 706 | |
| 707 | /* put the reference for the gpu */ |
| 708 | if (found) { |
| 709 | pm_runtime_mark_last_busy(&found->pdev->dev); |
| 710 | pm_runtime_put_autosuspend(&found->pdev->dev); |
| 711 | } |
| 712 | return ret; |
| 713 | } |
| 714 | |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 715 | int |
| 716 | vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, |
| 717 | struct dev_pm_domain *domain) |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 718 | { |
| 719 | /* copy over all the bus versions */ |
| 720 | if (dev->bus && dev->bus->pm) { |
| 721 | domain->ops = *dev->bus->pm; |
Thierry Reding | 7491bfb | 2015-08-12 16:32:11 +0200 | [diff] [blame^] | 722 | domain->ops.runtime_resume = |
| 723 | vga_switcheroo_runtime_resume_hdmi_audio; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 724 | |
| 725 | dev->pm_domain = domain; |
| 726 | return 0; |
| 727 | } |
| 728 | dev->pm_domain = NULL; |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio); |