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, |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 172 | pdev == vga_default_device(), driver_power_control); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 173 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 174 | EXPORT_SYMBOL(vga_switcheroo_register_client); |
| 175 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 176 | int vga_switcheroo_register_audio_client(struct pci_dev *pdev, |
| 177 | const struct vga_switcheroo_client_ops *ops, |
| 178 | int id, bool active) |
| 179 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 180 | return register_client(pdev, ops, id | ID_BIT_AUDIO, active, false); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 181 | } |
| 182 | EXPORT_SYMBOL(vga_switcheroo_register_audio_client); |
| 183 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 184 | static struct vga_switcheroo_client * |
| 185 | find_client_from_pci(struct list_head *head, struct pci_dev *pdev) |
| 186 | { |
| 187 | struct vga_switcheroo_client *client; |
| 188 | list_for_each_entry(client, head, list) |
| 189 | if (client->pdev == pdev) |
| 190 | return client; |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | static struct vga_switcheroo_client * |
| 195 | find_client_from_id(struct list_head *head, int client_id) |
| 196 | { |
| 197 | struct vga_switcheroo_client *client; |
| 198 | list_for_each_entry(client, head, list) |
| 199 | if (client->id == client_id) |
| 200 | return client; |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | static struct vga_switcheroo_client * |
| 205 | find_active_client(struct list_head *head) |
| 206 | { |
| 207 | struct vga_switcheroo_client *client; |
| 208 | list_for_each_entry(client, head, list) |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 209 | if (client->active && client_is_vga(client)) |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 210 | return client; |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Takashi Iwai | c8e9cf7 | 2012-06-07 12:15:15 +0200 | [diff] [blame] | 214 | int vga_switcheroo_get_client_state(struct pci_dev *pdev) |
| 215 | { |
| 216 | struct vga_switcheroo_client *client; |
| 217 | |
| 218 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 219 | if (!client) |
| 220 | return VGA_SWITCHEROO_NOT_FOUND; |
| 221 | if (!vgasr_priv.active) |
| 222 | return VGA_SWITCHEROO_INIT; |
| 223 | return client->pwr_state; |
| 224 | } |
| 225 | EXPORT_SYMBOL(vga_switcheroo_get_client_state); |
| 226 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 227 | void vga_switcheroo_unregister_client(struct pci_dev *pdev) |
| 228 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 229 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 230 | |
| 231 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 232 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 233 | if (client) { |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 234 | if (client_is_vga(client)) |
| 235 | vgasr_priv.registered_clients--; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 236 | list_del(&client->list); |
| 237 | kfree(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 238 | } |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 239 | if (vgasr_priv.active && vgasr_priv.registered_clients < 2) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 240 | pr_info("disabled\n"); |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 241 | vga_switcheroo_debugfs_fini(&vgasr_priv); |
| 242 | vgasr_priv.active = false; |
| 243 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 244 | mutex_unlock(&vgasr_mutex); |
| 245 | } |
| 246 | EXPORT_SYMBOL(vga_switcheroo_unregister_client); |
| 247 | |
| 248 | void vga_switcheroo_client_fb_set(struct pci_dev *pdev, |
| 249 | struct fb_info *info) |
| 250 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 251 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 252 | |
| 253 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 254 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 255 | if (client) |
| 256 | client->fb_info = info; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 257 | mutex_unlock(&vgasr_mutex); |
| 258 | } |
| 259 | EXPORT_SYMBOL(vga_switcheroo_client_fb_set); |
| 260 | |
| 261 | static int vga_switcheroo_show(struct seq_file *m, void *v) |
| 262 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 263 | struct vga_switcheroo_client *client; |
| 264 | int i = 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 265 | mutex_lock(&vgasr_mutex); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 266 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 267 | seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i, |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 268 | client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" : "IGD", |
| 269 | client_is_vga(client) ? "" : "-Audio", |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 270 | client->active ? '+' : ' ', |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 271 | client->driver_power_control ? "Dyn" : "", |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 272 | client->pwr_state ? "Pwr" : "Off", |
| 273 | pci_name(client->pdev)); |
| 274 | i++; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 275 | } |
| 276 | mutex_unlock(&vgasr_mutex); |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file) |
| 281 | { |
| 282 | return single_open(file, vga_switcheroo_show, NULL); |
| 283 | } |
| 284 | |
| 285 | static int vga_switchon(struct vga_switcheroo_client *client) |
| 286 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 287 | if (client->driver_power_control) |
| 288 | return 0; |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 289 | if (vgasr_priv.handler->power_state) |
| 290 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 291 | /* call the driver callback to turn on device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 292 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 293 | client->pwr_state = VGA_SWITCHEROO_ON; |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int vga_switchoff(struct vga_switcheroo_client *client) |
| 298 | { |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 299 | if (client->driver_power_control) |
| 300 | return 0; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 301 | /* call the driver callback to turn off device */ |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 302 | client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF); |
Dave Airlie | 5cfb3c3 | 2010-12-06 12:31:50 +1000 | [diff] [blame] | 303 | if (vgasr_priv.handler->power_state) |
| 304 | vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 305 | client->pwr_state = VGA_SWITCHEROO_OFF; |
| 306 | return 0; |
| 307 | } |
| 308 | |
Takashi Iwai | 3e9e63d | 2012-04-26 14:29:48 +0200 | [diff] [blame] | 309 | static void set_audio_state(int id, int state) |
| 310 | { |
| 311 | struct vga_switcheroo_client *client; |
| 312 | |
| 313 | client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO); |
| 314 | if (client && client->pwr_state != state) { |
| 315 | client->ops->set_gpu_state(client->pdev, state); |
| 316 | client->pwr_state = state; |
| 317 | } |
| 318 | } |
| 319 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 320 | /* stage one happens before delay */ |
| 321 | static int vga_switchto_stage1(struct vga_switcheroo_client *new_client) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 322 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 323 | struct vga_switcheroo_client *active; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 324 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 325 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 326 | if (!active) |
| 327 | return 0; |
| 328 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 329 | if (new_client->pwr_state == VGA_SWITCHEROO_OFF) |
| 330 | vga_switchon(new_client); |
| 331 | |
Matthew Garrett | 2fbe8c7 | 2012-04-16 16:26:03 -0400 | [diff] [blame] | 332 | vga_set_default_device(new_client->pdev); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | /* post delay */ |
| 337 | static int vga_switchto_stage2(struct vga_switcheroo_client *new_client) |
| 338 | { |
| 339 | int ret; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 340 | struct vga_switcheroo_client *active; |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 341 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 342 | active = find_active_client(&vgasr_priv.clients); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 343 | if (!active) |
| 344 | return 0; |
| 345 | |
| 346 | active->active = false; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 347 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 348 | set_audio_state(active->id, VGA_SWITCHEROO_OFF); |
| 349 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 350 | if (new_client->fb_info) { |
| 351 | struct fb_event event; |
Dave Airlie | 054430e | 2013-01-25 11:38:56 +1000 | [diff] [blame] | 352 | console_lock(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 353 | event.info = new_client->fb_info; |
| 354 | fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event); |
Dave Airlie | 054430e | 2013-01-25 11:38:56 +1000 | [diff] [blame] | 355 | console_unlock(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | ret = vgasr_priv.handler->switchto(new_client->id); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 362 | if (new_client->ops->reprobe) |
| 363 | new_client->ops->reprobe(new_client->pdev); |
Dave Airlie | 8d608aa | 2010-12-07 08:57:57 +1000 | [diff] [blame] | 364 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 365 | if (active->pwr_state == VGA_SWITCHEROO_ON) |
| 366 | vga_switchoff(active); |
| 367 | |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 368 | set_audio_state(new_client->id, VGA_SWITCHEROO_ON); |
| 369 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 370 | new_client->active = true; |
| 371 | return 0; |
| 372 | } |
| 373 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 374 | static bool check_can_switch(void) |
| 375 | { |
| 376 | struct vga_switcheroo_client *client; |
| 377 | |
| 378 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | 26ec685 | 2012-05-11 07:51:17 +0200 | [diff] [blame] | 379 | if (!client->ops->can_switch(client->pdev)) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 380 | pr_err("client %x refused switch\n", client->id); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 381 | return false; |
| 382 | } |
| 383 | } |
| 384 | return true; |
| 385 | } |
| 386 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 387 | static ssize_t |
| 388 | vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf, |
| 389 | size_t cnt, loff_t *ppos) |
| 390 | { |
| 391 | char usercmd[64]; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 392 | int ret; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 393 | bool delay = false, can_switch; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 394 | bool just_mux = false; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 395 | int client_id = -1; |
| 396 | struct vga_switcheroo_client *client = NULL; |
| 397 | |
| 398 | if (cnt > 63) |
| 399 | cnt = 63; |
| 400 | |
| 401 | if (copy_from_user(usercmd, ubuf, cnt)) |
| 402 | return -EFAULT; |
| 403 | |
| 404 | mutex_lock(&vgasr_mutex); |
| 405 | |
Jiri Slaby | 8c88e50 | 2010-04-27 14:11:03 -0700 | [diff] [blame] | 406 | if (!vgasr_priv.active) { |
| 407 | cnt = -EINVAL; |
| 408 | goto out; |
| 409 | } |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 410 | |
| 411 | /* pwr off the device not in use */ |
| 412 | if (strncmp(usercmd, "OFF", 3) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 413 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 414 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 415 | continue; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 416 | if (client->driver_power_control) |
| 417 | continue; |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 418 | set_audio_state(client->id, VGA_SWITCHEROO_OFF); |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 419 | if (client->pwr_state == VGA_SWITCHEROO_ON) |
| 420 | vga_switchoff(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 421 | } |
| 422 | goto out; |
| 423 | } |
| 424 | /* pwr on the device not in use */ |
| 425 | if (strncmp(usercmd, "ON", 2) == 0) { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 426 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 427 | if (client->active || client_is_audio(client)) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 428 | continue; |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 429 | if (client->driver_power_control) |
| 430 | continue; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 431 | if (client->pwr_state == VGA_SWITCHEROO_OFF) |
| 432 | vga_switchon(client); |
Takashi Iwai | c91c3fa | 2012-06-09 08:46:42 +0200 | [diff] [blame] | 433 | set_audio_state(client->id, VGA_SWITCHEROO_ON); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 434 | } |
| 435 | goto out; |
| 436 | } |
| 437 | |
| 438 | /* request a delayed switch - test can we switch now */ |
| 439 | if (strncmp(usercmd, "DIGD", 4) == 0) { |
| 440 | client_id = VGA_SWITCHEROO_IGD; |
| 441 | delay = true; |
| 442 | } |
| 443 | |
| 444 | if (strncmp(usercmd, "DDIS", 4) == 0) { |
| 445 | client_id = VGA_SWITCHEROO_DIS; |
| 446 | delay = true; |
| 447 | } |
| 448 | |
| 449 | if (strncmp(usercmd, "IGD", 3) == 0) |
| 450 | client_id = VGA_SWITCHEROO_IGD; |
| 451 | |
| 452 | if (strncmp(usercmd, "DIS", 3) == 0) |
| 453 | client_id = VGA_SWITCHEROO_DIS; |
| 454 | |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 455 | if (strncmp(usercmd, "MIGD", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 456 | just_mux = true; |
| 457 | client_id = VGA_SWITCHEROO_IGD; |
| 458 | } |
Dan Carpenter | fea6f33 | 2011-01-07 08:12:27 +0300 | [diff] [blame] | 459 | if (strncmp(usercmd, "MDIS", 4) == 0) { |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 460 | just_mux = true; |
| 461 | client_id = VGA_SWITCHEROO_DIS; |
| 462 | } |
| 463 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 464 | if (client_id == -1) |
| 465 | goto out; |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 466 | client = find_client_from_id(&vgasr_priv.clients, client_id); |
| 467 | if (!client) |
| 468 | goto out; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 469 | |
| 470 | vgasr_priv.delayed_switch_active = false; |
Dave Airlie | 851ab95 | 2010-12-06 12:35:52 +1000 | [diff] [blame] | 471 | |
| 472 | if (just_mux) { |
| 473 | ret = vgasr_priv.handler->switchto(client_id); |
| 474 | goto out; |
| 475 | } |
| 476 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 477 | if (client->active) |
Florian Mickler | a67b888 | 2011-05-15 16:32:50 +0200 | [diff] [blame] | 478 | goto out; |
| 479 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 480 | /* okay we want a switch - test if devices are willing to switch */ |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 481 | can_switch = check_can_switch(); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 482 | |
| 483 | if (can_switch == false && delay == false) |
| 484 | goto out; |
| 485 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 486 | if (can_switch) { |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 487 | ret = vga_switchto_stage1(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 488 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 489 | pr_err("switching failed stage 1 %d\n", ret); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 490 | |
| 491 | ret = vga_switchto_stage2(client); |
| 492 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 493 | pr_err("switching failed stage 2 %d\n", ret); |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 494 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 495 | } else { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 496 | pr_info("setting delayed switch to client %d\n", client->id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 497 | vgasr_priv.delayed_switch_active = true; |
| 498 | vgasr_priv.delayed_client_id = client_id; |
| 499 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 500 | ret = vga_switchto_stage1(client); |
| 501 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 502 | pr_err("delayed switching stage 1 failed %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | out: |
| 506 | mutex_unlock(&vgasr_mutex); |
| 507 | return cnt; |
| 508 | } |
| 509 | |
| 510 | static const struct file_operations vga_switcheroo_debugfs_fops = { |
| 511 | .owner = THIS_MODULE, |
| 512 | .open = vga_switcheroo_debugfs_open, |
| 513 | .write = vga_switcheroo_debugfs_write, |
| 514 | .read = seq_read, |
| 515 | .llseek = seq_lseek, |
| 516 | .release = single_release, |
| 517 | }; |
| 518 | |
| 519 | static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv) |
| 520 | { |
| 521 | if (priv->switch_file) { |
| 522 | debugfs_remove(priv->switch_file); |
| 523 | priv->switch_file = NULL; |
| 524 | } |
| 525 | if (priv->debugfs_root) { |
| 526 | debugfs_remove(priv->debugfs_root); |
| 527 | priv->debugfs_root = NULL; |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv) |
| 532 | { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 533 | static const char mp[] = "/sys/kernel/debug"; |
| 534 | |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 535 | /* already initialised */ |
| 536 | if (priv->debugfs_root) |
| 537 | return 0; |
| 538 | priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL); |
| 539 | |
| 540 | if (!priv->debugfs_root) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 541 | pr_err("Cannot create %s/vgaswitcheroo\n", mp); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 542 | goto fail; |
| 543 | } |
| 544 | |
| 545 | priv->switch_file = debugfs_create_file("switch", 0644, |
| 546 | priv->debugfs_root, NULL, &vga_switcheroo_debugfs_fops); |
| 547 | if (!priv->switch_file) { |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 548 | pr_err("cannot create %s/vgaswitcheroo/switch\n", mp); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 549 | goto fail; |
| 550 | } |
| 551 | return 0; |
| 552 | fail: |
| 553 | vga_switcheroo_debugfs_fini(priv); |
| 554 | return -1; |
| 555 | } |
| 556 | |
| 557 | int vga_switcheroo_process_delayed_switch(void) |
| 558 | { |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 559 | struct vga_switcheroo_client *client; |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 560 | int ret; |
| 561 | int err = -EINVAL; |
| 562 | |
| 563 | mutex_lock(&vgasr_mutex); |
| 564 | if (!vgasr_priv.delayed_switch_active) |
| 565 | goto err; |
| 566 | |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 567 | pr_info("processing delayed switch to %d\n", |
| 568 | vgasr_priv.delayed_client_id); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 569 | |
Takashi Iwai | 79721e0 | 2012-04-26 12:55:59 +0200 | [diff] [blame] | 570 | client = find_client_from_id(&vgasr_priv.clients, |
| 571 | vgasr_priv.delayed_client_id); |
| 572 | if (!client || !check_can_switch()) |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 573 | goto err; |
| 574 | |
Dave Airlie | 66b37c6 | 2010-12-07 14:24:25 +1000 | [diff] [blame] | 575 | ret = vga_switchto_stage2(client); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 576 | if (ret) |
Thierry Reding | 9b0be1e | 2015-08-12 16:32:10 +0200 | [diff] [blame^] | 577 | pr_err("delayed switching failed stage 2 %d\n", ret); |
Dave Airlie | 6a9ee8a | 2010-02-01 15:38:10 +1000 | [diff] [blame] | 578 | |
| 579 | vgasr_priv.delayed_switch_active = false; |
| 580 | err = 0; |
| 581 | err: |
| 582 | mutex_unlock(&vgasr_mutex); |
| 583 | return err; |
| 584 | } |
| 585 | EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 586 | |
| 587 | static void vga_switcheroo_power_switch(struct pci_dev *pdev, enum vga_switcheroo_state state) |
| 588 | { |
| 589 | struct vga_switcheroo_client *client; |
| 590 | |
| 591 | if (!vgasr_priv.handler->power_state) |
| 592 | return; |
| 593 | |
| 594 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 595 | if (!client) |
| 596 | return; |
| 597 | |
| 598 | if (!client->driver_power_control) |
| 599 | return; |
| 600 | |
| 601 | vgasr_priv.handler->power_state(client->id, state); |
| 602 | } |
| 603 | |
| 604 | /* force a PCI device to a certain state - mainly to turn off audio clients */ |
| 605 | |
| 606 | void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev, enum vga_switcheroo_state dynamic) |
| 607 | { |
| 608 | struct vga_switcheroo_client *client; |
| 609 | |
| 610 | client = find_client_from_pci(&vgasr_priv.clients, pdev); |
| 611 | if (!client) |
| 612 | return; |
| 613 | |
| 614 | if (!client->driver_power_control) |
| 615 | return; |
| 616 | |
| 617 | client->pwr_state = dynamic; |
| 618 | set_audio_state(client->id, dynamic); |
| 619 | } |
| 620 | EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch); |
| 621 | |
| 622 | /* switcheroo power domain */ |
| 623 | static int vga_switcheroo_runtime_suspend(struct device *dev) |
| 624 | { |
| 625 | struct pci_dev *pdev = to_pci_dev(dev); |
| 626 | int ret; |
| 627 | |
| 628 | ret = dev->bus->pm->runtime_suspend(dev); |
| 629 | if (ret) |
| 630 | return ret; |
Alex Deucher | f2bc56161 | 2014-05-19 14:04:43 -0400 | [diff] [blame] | 631 | if (vgasr_priv.handler->switchto) |
| 632 | vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD); |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 633 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF); |
| 634 | return 0; |
| 635 | } |
| 636 | |
| 637 | static int vga_switcheroo_runtime_resume(struct device *dev) |
| 638 | { |
| 639 | struct pci_dev *pdev = to_pci_dev(dev); |
| 640 | int ret; |
| 641 | |
| 642 | vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON); |
| 643 | ret = dev->bus->pm->runtime_resume(dev); |
| 644 | if (ret) |
| 645 | return ret; |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /* this version is for the case where the power switch is separate |
| 651 | to the device being powered down. */ |
| 652 | int vga_switcheroo_init_domain_pm_ops(struct device *dev, struct dev_pm_domain *domain) |
| 653 | { |
| 654 | /* copy over all the bus versions */ |
| 655 | if (dev->bus && dev->bus->pm) { |
| 656 | domain->ops = *dev->bus->pm; |
| 657 | domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend; |
| 658 | domain->ops.runtime_resume = vga_switcheroo_runtime_resume; |
| 659 | |
| 660 | dev->pm_domain = domain; |
| 661 | return 0; |
| 662 | } |
| 663 | dev->pm_domain = NULL; |
| 664 | return -EINVAL; |
| 665 | } |
| 666 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops); |
| 667 | |
Alex Deucher | 766a53d | 2014-09-12 17:51:29 -0400 | [diff] [blame] | 668 | void vga_switcheroo_fini_domain_pm_ops(struct device *dev) |
| 669 | { |
| 670 | dev->pm_domain = NULL; |
| 671 | } |
| 672 | EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops); |
| 673 | |
Dave Airlie | 0d69704 | 2012-09-10 12:28:36 +1000 | [diff] [blame] | 674 | static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev) |
| 675 | { |
| 676 | struct pci_dev *pdev = to_pci_dev(dev); |
| 677 | int ret; |
| 678 | struct vga_switcheroo_client *client, *found = NULL; |
| 679 | |
| 680 | /* we need to check if we have to switch back on the video |
| 681 | device so the audio device can come back */ |
| 682 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
| 683 | if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) && client_is_vga(client)) { |
| 684 | found = client; |
| 685 | ret = pm_runtime_get_sync(&client->pdev->dev); |
| 686 | if (ret) { |
| 687 | if (ret != 1) |
| 688 | return ret; |
| 689 | } |
| 690 | break; |
| 691 | } |
| 692 | } |
| 693 | ret = dev->bus->pm->runtime_resume(dev); |
| 694 | |
| 695 | /* put the reference for the gpu */ |
| 696 | if (found) { |
| 697 | pm_runtime_mark_last_busy(&found->pdev->dev); |
| 698 | pm_runtime_put_autosuspend(&found->pdev->dev); |
| 699 | } |
| 700 | return ret; |
| 701 | } |
| 702 | |
| 703 | int vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev, struct dev_pm_domain *domain) |
| 704 | { |
| 705 | /* copy over all the bus versions */ |
| 706 | if (dev->bus && dev->bus->pm) { |
| 707 | domain->ops = *dev->bus->pm; |
| 708 | domain->ops.runtime_resume = vga_switcheroo_runtime_resume_hdmi_audio; |
| 709 | |
| 710 | dev->pm_domain = domain; |
| 711 | return 0; |
| 712 | } |
| 713 | dev->pm_domain = NULL; |
| 714 | return -EINVAL; |
| 715 | } |
| 716 | EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio); |