| @@ -103,9 +103,11 @@ · 102 unmodified lines |
| 103 | 103 | * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs |
| 104 | 104 | * interface is a no-op so as not to interfere with runtime pm |
| 105 | 105 | * @list: client list |
| 106 | + * @vga_dev: pci device, indicate which GPU is bound to current audio client |
| 106 | 107 | * |
| 107 | 108 | * Registered client. A client can be either a GPU or an audio device on a GPU. |
| 108 | | − * For audio clients, the @fb_info and @active members are bogus. |
| 109 | + * For audio clients, the @fb_info and @active members are bogus. For GPU |
| 110 | + * clients, the @vga_dev is bogus. |
| 109 | 111 | */ |
| 110 | 112 | struct vga_switcheroo_client { |
| 111 | 113 | struct pci_dev *pdev; |
| @@ -116,6 +118,7 @@ struct vga_switcheroo_client { · 4 unmodified lines |
| 116 | 118 | bool active; |
| 117 | 119 | bool driver_power_control; |
| 118 | 120 | struct list_head list; |
| 121 | + struct pci_dev *vga_dev; |
| 119 | 122 | }; |
| 120 | 123 | |
| 121 | 124 | /* |
| @@ -161,9 +164,8 @@ struct vgasr_priv { · 39 unmodified lines |
| 161 | 164 | }; |
| 162 | 165 | |
| 163 | 166 | #define ID_BIT_AUDIO 0x100 |
| 164 | | −#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO) |
| 165 | | −#define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \ |
| 166 | | − !client_is_audio(c)) |
| 167 | +#define client_is_audio(c) ((c)->id & ID_BIT_AUDIO) |
| 168 | +#define client_is_vga(c) (!client_is_audio(c)) |
| 167 | 169 | #define client_id(c) ((c)->id & ~ID_BIT_AUDIO) |
| 168 | 170 | |
| 169 | 171 | static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv); |
| @@ -192,14 +194,29 @@ static void vga_switcheroo_enable(void) · 22 unmodified lines |
| 192 | 194 | vgasr_priv.handler->init(); |
| 193 | 195 | |
| 194 | 196 | list_for_each_entry(client, &vgasr_priv.clients, list) { |
| 195 | | − if (client->id != VGA_SWITCHEROO_UNKNOWN_ID) |
| 197 | + if (!client_is_vga(client) || |
| 198 | + client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID) |
| 196 | 199 | continue; |
| 200 | + |
| 197 | 201 | ret = vgasr_priv.handler->get_client_id(client->pdev); |
| 198 | 202 | if (ret < 0) |
| 199 | 203 | return; |
| 200 | 204 | |
| 201 | 205 | client->id = ret; |
| 202 | 206 | } |
| 207 | + |
| 208 | + list_for_each_entry(client, &vgasr_priv.clients, list) { |
| 209 | + if (!client_is_audio(client) || |
| 210 | + client_id(client) != VGA_SWITCHEROO_UNKNOWN_ID) |
| 211 | + continue; |
| 212 | + |
| 213 | + ret = vgasr_priv.handler->get_client_id(client->vga_dev); |
| 214 | + if (ret < 0) |
| 215 | + return; |
| 216 | + |
| 217 | + client->id = ret | ID_BIT_AUDIO; |
| 218 | + } |
| 219 | + |
| 203 | 220 | vga_switcheroo_debugfs_init(&vgasr_priv); |
| 204 | 221 | vgasr_priv.active = true; |
| 205 | 222 | } |
| @@ -272,7 +289,9 @@ EXPORT_SYMBOL(vga_switcheroo_handler_flags); · 66 unmodified lines |
| 272 | 289 | |
| 273 | 290 | static int register_client(struct pci_dev *pdev, |
| 274 | 291 | const struct vga_switcheroo_client_ops *ops, |
| 275 | | − enum vga_switcheroo_client_id id, bool active, |
| 292 | + enum vga_switcheroo_client_id id, |
| 293 | + struct pci_dev *vga_dev, |
| 294 | + bool active, |
| 276 | 295 | bool driver_power_control) |
| 277 | 296 | { |
| 278 | 297 | struct vga_switcheroo_client *client; |
| @@ -287,6 +306,7 @@ static int register_client(struct pci_dev *pdev, · 8 unmodified lines |
| 287 | 306 | client->id = id; |
| 288 | 307 | client->active = active; |
| 289 | 308 | client->driver_power_control = driver_power_control; |
| 309 | + client->vga_dev = vga_dev; |
| 290 | 310 | |
| 291 | 311 | mutex_lock(&vgasr_mutex); |
| 292 | 312 | list_add_tail(&client->list, &vgasr_priv.clients); |
| @@ -319,7 +339,7 @@ int vga_switcheroo_register_client(struct pci_dev *pdev, · 26 unmodified lines |
| 319 | 339 | const struct vga_switcheroo_client_ops *ops, |
| 320 | 340 | bool driver_power_control) |
| 321 | 341 | { |
| 322 | | − return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID, |
| 342 | + return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID, NULL, |
| 323 | 343 | pdev == vga_default_device(), |
| 324 | 344 | driver_power_control); |
| 325 | 345 | } |
| @@ -329,19 +349,40 @@ EXPORT_SYMBOL(vga_switcheroo_register_client); · 3 unmodified lines |
| 329 | 349 | * vga_switcheroo_register_audio_client - register audio client |
| 330 | 350 | * @pdev: client pci device |
| 331 | 351 | * @ops: client callbacks |
| 332 | | − * @id: client identifier |
| 352 | + * @vga_dev: pci device which is bound to current audio client |
| 333 | 353 | * |
| 334 | 354 | * Register audio client (audio device on a GPU). The client is assumed |
| 335 | 355 | * to use runtime PM. Beforehand, vga_switcheroo_client_probe_defer() |
| 336 | 356 | * shall be called to ensure that all prerequisites are met. |
| 337 | 357 | * |
| 338 | | − * Return: 0 on success, -ENOMEM on memory allocation error. |
| 358 | + * Return: 0 on success, -ENOMEM on memory allocation error, -EINVAL on getting |
| 359 | + * client id error. |
| 339 | 360 | */ |
| 340 | 361 | int vga_switcheroo_register_audio_client(struct pci_dev *pdev, |
| 341 | 362 | const struct vga_switcheroo_client_ops *ops, |
| 342 | | − enum vga_switcheroo_client_id id) |
| 363 | + struct pci_dev *vga_dev) |
| 343 | 364 | { |
| 344 | | − return register_client(pdev, ops, id | ID_BIT_AUDIO, false, true); |
| 365 | + enum vga_switcheroo_client_id id = VGA_SWITCHEROO_UNKNOWN_ID; |
| 366 | + |
| 367 | + /* |
| 368 | + * if vga_switcheroo has enabled, that mean two GPU clients and also |
| 369 | + * handler are registered. Get audio client id from bound GPU client |
| 370 | + * id directly, otherwise, set it as VGA_SWITCHEROO_UNKNOWN_ID, |
| 371 | + * it will set to correct id in later when vga_switcheroo_enable() |
| 372 | + * is called. |
| 373 | + */ |
| 374 | + mutex_lock(&vgasr_mutex); |
| 375 | + if (vgasr_priv.active) { |
| 376 | + id = vgasr_priv.handler->get_client_id(vga_dev); |
| 377 | + if (id < 0) { |
| 378 | + mutex_unlock(&vgasr_mutex); |
| 379 | + return -EINVAL; |
| 380 | + } |
| 381 | + } |
| 382 | + mutex_unlock(&vgasr_mutex); |
| 383 | + |
| 384 | + return register_client(pdev, ops, id | ID_BIT_AUDIO, vga_dev, |
| 385 | + false, true); |
| 345 | 386 | } |
| 346 | 387 | EXPORT_SYMBOL(vga_switcheroo_register_audio_client); |
| 347 | 388 | |