vga_switcheroo.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * vga_switcheroo.c - Support for laptop with dual GPU using one set of outputs
  3. *
  4. * Copyright (c) 2010 Red Hat Inc.
  5. * Author : Dave Airlie <airlied@redhat.com>
  6. *
  7. * Copyright (c) 2015 Lukas Wunner <lukas@wunner.de>
  8. *
  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:
  15. *
  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.
  19. *
  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.
  28. *
  29. */
  30. #define pr_fmt(fmt) "vga_switcheroo: " fmt
  31. #include <linux/console.h>
  32. #include <linux/debugfs.h>
  33. #include <linux/fb.h>
  34. #include <linux/fs.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/uaccess.h>
  40. #include <linux/vgaarb.h>
  41. #include <linux/vga_switcheroo.h>
  42. /**
  43. * DOC: Overview
  44. *
  45. * vga_switcheroo is the Linux subsystem for laptop hybrid graphics.
  46. * These come in two flavors:
  47. *
  48. * * muxed: Dual GPUs with a multiplexer chip to switch outputs between GPUs.
  49. * * muxless: Dual GPUs but only one of them is connected to outputs.
  50. * The other one is merely used to offload rendering, its results
  51. * are copied over PCIe into the framebuffer. On Linux this is
  52. * supported with DRI PRIME.
  53. *
  54. * Hybrid graphics started to appear in the late Naughties and were initially
  55. * all muxed. Newer laptops moved to a muxless architecture for cost reasons.
  56. * A notable exception is the MacBook Pro which continues to use a mux.
  57. * Muxes come with varying capabilities: Some switch only the panel, others
  58. * can also switch external displays. Some switch all display pins at once
  59. * while others can switch just the DDC lines. (To allow EDID probing
  60. * for the inactive GPU.) Also, muxes are often used to cut power to the
  61. * discrete GPU while it is not used.
  62. *
  63. * DRM drivers register GPUs with vga_switcheroo, these are heretoforth called
  64. * clients. The mux is called the handler. Muxless machines also register a
  65. * handler to control the power state of the discrete GPU, its ->switchto
  66. * callback is a no-op for obvious reasons. The discrete GPU is often equipped
  67. * with an HDA controller for the HDMI/DP audio signal, this will also
  68. * register as a client so that vga_switcheroo can take care of the correct
  69. * suspend/resume order when changing the discrete GPU's power state. In total
  70. * there can thus be up to three clients: Two vga clients (GPUs) and one audio
  71. * client (on the discrete GPU). The code is mostly prepared to support
  72. * machines with more than two GPUs should they become available.
  73. * The GPU to which the outputs are currently switched is called the
  74. * active client in vga_switcheroo parlance. The GPU not in use is the
  75. * inactive client.
  76. */
  77. /**
  78. * struct vga_switcheroo_client - registered client
  79. * @pdev: client pci device
  80. * @fb_info: framebuffer to which console is remapped on switching
  81. * @pwr_state: current power state
  82. * @ops: client callbacks
  83. * @id: client identifier. Determining the id requires the handler,
  84. * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
  85. * and later given their true id in vga_switcheroo_enable()
  86. * @active: whether the outputs are currently switched to this client
  87. * @driver_power_control: whether power state is controlled by the driver's
  88. * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
  89. * interface is a no-op so as not to interfere with runtime pm
  90. * @list: client list
  91. *
  92. * Registered client. A client can be either a GPU or an audio device on a GPU.
  93. * For audio clients, the @fb_info, @active and @driver_power_control members
  94. * are bogus.
  95. */
  96. struct vga_switcheroo_client {
  97. struct pci_dev *pdev;
  98. struct fb_info *fb_info;
  99. enum vga_switcheroo_state pwr_state;
  100. const struct vga_switcheroo_client_ops *ops;
  101. enum vga_switcheroo_client_id id;
  102. bool active;
  103. bool driver_power_control;
  104. struct list_head list;
  105. };
  106. /*
  107. * protects access to struct vgasr_priv
  108. */
  109. static DEFINE_MUTEX(vgasr_mutex);
  110. /**
  111. * struct vgasr_priv - vga_switcheroo private data
  112. * @active: whether vga_switcheroo is enabled.
  113. * Prerequisite is the registration of two GPUs and a handler
  114. * @delayed_switch_active: whether a delayed switch is pending
  115. * @delayed_client_id: client to which a delayed switch is pending
  116. * @debugfs_root: directory for vga_switcheroo debugfs interface
  117. * @switch_file: file for vga_switcheroo debugfs interface
  118. * @registered_clients: number of registered GPUs
  119. * (counting only vga clients, not audio clients)
  120. * @clients: list of registered clients
  121. * @handler: registered handler
  122. *
  123. * vga_switcheroo private data. Currently only one vga_switcheroo instance
  124. * per system is supported.
  125. */
  126. struct vgasr_priv {
  127. bool active;
  128. bool delayed_switch_active;
  129. enum vga_switcheroo_client_id delayed_client_id;
  130. struct dentry *debugfs_root;
  131. struct dentry *switch_file;
  132. int registered_clients;
  133. struct list_head clients;
  134. const struct vga_switcheroo_handler *handler;
  135. };
  136. #define ID_BIT_AUDIO 0x100
  137. #define client_is_audio(c) ((c)->id & ID_BIT_AUDIO)
  138. #define client_is_vga(c) ((c)->id == VGA_SWITCHEROO_UNKNOWN_ID || \
  139. !client_is_audio(c))
  140. #define client_id(c) ((c)->id & ~ID_BIT_AUDIO)
  141. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv);
  142. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv);
  143. /* only one switcheroo per system */
  144. static struct vgasr_priv vgasr_priv = {
  145. .clients = LIST_HEAD_INIT(vgasr_priv.clients),
  146. };
  147. static bool vga_switcheroo_ready(void)
  148. {
  149. /* we're ready if we get two clients + handler */
  150. return !vgasr_priv.active &&
  151. vgasr_priv.registered_clients == 2 && vgasr_priv.handler;
  152. }
  153. static void vga_switcheroo_enable(void)
  154. {
  155. int ret;
  156. struct vga_switcheroo_client *client;
  157. /* call the handler to init */
  158. if (vgasr_priv.handler->init)
  159. vgasr_priv.handler->init();
  160. list_for_each_entry(client, &vgasr_priv.clients, list) {
  161. if (client->id != VGA_SWITCHEROO_UNKNOWN_ID)
  162. continue;
  163. ret = vgasr_priv.handler->get_client_id(client->pdev);
  164. if (ret < 0)
  165. return;
  166. client->id = ret;
  167. }
  168. vga_switcheroo_debugfs_init(&vgasr_priv);
  169. vgasr_priv.active = true;
  170. }
  171. /**
  172. * vga_switcheroo_register_handler() - register handler
  173. * @handler: handler callbacks
  174. *
  175. * Register handler. Enable vga_switcheroo if two vga clients have already
  176. * registered.
  177. *
  178. * Return: 0 on success, -EINVAL if a handler was already registered.
  179. */
  180. int vga_switcheroo_register_handler(const struct vga_switcheroo_handler *handler)
  181. {
  182. mutex_lock(&vgasr_mutex);
  183. if (vgasr_priv.handler) {
  184. mutex_unlock(&vgasr_mutex);
  185. return -EINVAL;
  186. }
  187. vgasr_priv.handler = handler;
  188. if (vga_switcheroo_ready()) {
  189. pr_info("enabled\n");
  190. vga_switcheroo_enable();
  191. }
  192. mutex_unlock(&vgasr_mutex);
  193. return 0;
  194. }
  195. EXPORT_SYMBOL(vga_switcheroo_register_handler);
  196. /**
  197. * vga_switcheroo_unregister_handler() - unregister handler
  198. *
  199. * Unregister handler. Disable vga_switcheroo.
  200. */
  201. void vga_switcheroo_unregister_handler(void)
  202. {
  203. mutex_lock(&vgasr_mutex);
  204. vgasr_priv.handler = NULL;
  205. if (vgasr_priv.active) {
  206. pr_info("disabled\n");
  207. vga_switcheroo_debugfs_fini(&vgasr_priv);
  208. vgasr_priv.active = false;
  209. }
  210. mutex_unlock(&vgasr_mutex);
  211. }
  212. EXPORT_SYMBOL(vga_switcheroo_unregister_handler);
  213. static int register_client(struct pci_dev *pdev,
  214. const struct vga_switcheroo_client_ops *ops,
  215. enum vga_switcheroo_client_id id, bool active,
  216. bool driver_power_control)
  217. {
  218. struct vga_switcheroo_client *client;
  219. client = kzalloc(sizeof(*client), GFP_KERNEL);
  220. if (!client)
  221. return -ENOMEM;
  222. client->pwr_state = VGA_SWITCHEROO_ON;
  223. client->pdev = pdev;
  224. client->ops = ops;
  225. client->id = id;
  226. client->active = active;
  227. client->driver_power_control = driver_power_control;
  228. mutex_lock(&vgasr_mutex);
  229. list_add_tail(&client->list, &vgasr_priv.clients);
  230. if (client_is_vga(client))
  231. vgasr_priv.registered_clients++;
  232. if (vga_switcheroo_ready()) {
  233. pr_info("enabled\n");
  234. vga_switcheroo_enable();
  235. }
  236. mutex_unlock(&vgasr_mutex);
  237. return 0;
  238. }
  239. /**
  240. * vga_switcheroo_register_client - register vga client
  241. * @pdev: client pci device
  242. * @ops: client callbacks
  243. * @driver_power_control: whether power state is controlled by the driver's
  244. * runtime pm
  245. *
  246. * Register vga client (GPU). Enable vga_switcheroo if another GPU and a
  247. * handler have already registered. The power state of the client is assumed
  248. * to be ON.
  249. *
  250. * Return: 0 on success, -ENOMEM on memory allocation error.
  251. */
  252. int vga_switcheroo_register_client(struct pci_dev *pdev,
  253. const struct vga_switcheroo_client_ops *ops,
  254. bool driver_power_control)
  255. {
  256. return register_client(pdev, ops, VGA_SWITCHEROO_UNKNOWN_ID,
  257. pdev == vga_default_device(),
  258. driver_power_control);
  259. }
  260. EXPORT_SYMBOL(vga_switcheroo_register_client);
  261. /**
  262. * vga_switcheroo_register_audio_client - register audio client
  263. * @pdev: client pci device
  264. * @ops: client callbacks
  265. * @id: client identifier
  266. *
  267. * Register audio client (audio device on a GPU). The power state of the
  268. * client is assumed to be ON.
  269. *
  270. * Return: 0 on success, -ENOMEM on memory allocation error.
  271. */
  272. int vga_switcheroo_register_audio_client(struct pci_dev *pdev,
  273. const struct vga_switcheroo_client_ops *ops,
  274. enum vga_switcheroo_client_id id)
  275. {
  276. return register_client(pdev, ops, id | ID_BIT_AUDIO, false, false);
  277. }
  278. EXPORT_SYMBOL(vga_switcheroo_register_audio_client);
  279. static struct vga_switcheroo_client *
  280. find_client_from_pci(struct list_head *head, struct pci_dev *pdev)
  281. {
  282. struct vga_switcheroo_client *client;
  283. list_for_each_entry(client, head, list)
  284. if (client->pdev == pdev)
  285. return client;
  286. return NULL;
  287. }
  288. static struct vga_switcheroo_client *
  289. find_client_from_id(struct list_head *head,
  290. enum vga_switcheroo_client_id client_id)
  291. {
  292. struct vga_switcheroo_client *client;
  293. list_for_each_entry(client, head, list)
  294. if (client->id == client_id)
  295. return client;
  296. return NULL;
  297. }
  298. static struct vga_switcheroo_client *
  299. find_active_client(struct list_head *head)
  300. {
  301. struct vga_switcheroo_client *client;
  302. list_for_each_entry(client, head, list)
  303. if (client->active)
  304. return client;
  305. return NULL;
  306. }
  307. /**
  308. * vga_switcheroo_get_client_state() - obtain power state of a given client
  309. * @pdev: client pci device
  310. *
  311. * Obtain power state of a given client as seen from vga_switcheroo.
  312. * The function is only called from hda_intel.c.
  313. *
  314. * Return: Power state.
  315. */
  316. enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
  317. {
  318. struct vga_switcheroo_client *client;
  319. enum vga_switcheroo_state ret;
  320. mutex_lock(&vgasr_mutex);
  321. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  322. if (!client)
  323. ret = VGA_SWITCHEROO_NOT_FOUND;
  324. else if (!vgasr_priv.active)
  325. ret = VGA_SWITCHEROO_INIT;
  326. else
  327. ret = client->pwr_state;
  328. mutex_unlock(&vgasr_mutex);
  329. return ret;
  330. }
  331. EXPORT_SYMBOL(vga_switcheroo_get_client_state);
  332. /**
  333. * vga_switcheroo_unregister_client() - unregister client
  334. * @pdev: client pci device
  335. *
  336. * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
  337. */
  338. void vga_switcheroo_unregister_client(struct pci_dev *pdev)
  339. {
  340. struct vga_switcheroo_client *client;
  341. mutex_lock(&vgasr_mutex);
  342. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  343. if (client) {
  344. if (client_is_vga(client))
  345. vgasr_priv.registered_clients--;
  346. list_del(&client->list);
  347. kfree(client);
  348. }
  349. if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
  350. pr_info("disabled\n");
  351. vga_switcheroo_debugfs_fini(&vgasr_priv);
  352. vgasr_priv.active = false;
  353. }
  354. mutex_unlock(&vgasr_mutex);
  355. }
  356. EXPORT_SYMBOL(vga_switcheroo_unregister_client);
  357. /**
  358. * vga_switcheroo_client_fb_set() - set framebuffer of a given client
  359. * @pdev: client pci device
  360. * @info: framebuffer
  361. *
  362. * Set framebuffer of a given client. The console will be remapped to this
  363. * on switching.
  364. */
  365. void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
  366. struct fb_info *info)
  367. {
  368. struct vga_switcheroo_client *client;
  369. mutex_lock(&vgasr_mutex);
  370. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  371. if (client)
  372. client->fb_info = info;
  373. mutex_unlock(&vgasr_mutex);
  374. }
  375. EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
  376. /**
  377. * DOC: Manual switching and manual power control
  378. *
  379. * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
  380. * can be read to retrieve the current vga_switcheroo state and commands
  381. * can be written to it to change the state. The file appears as soon as
  382. * two GPU drivers and one handler have registered with vga_switcheroo.
  383. * The following commands are understood:
  384. *
  385. * * OFF: Power off the device not in use.
  386. * * ON: Power on the device not in use.
  387. * * IGD: Switch to the integrated graphics device.
  388. * Power on the integrated GPU if necessary, power off the discrete GPU.
  389. * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
  390. * have opened device files of the GPUs or the audio client. If the
  391. * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
  392. * and /dev/snd/controlC1 to identify processes blocking the switch.
  393. * * DIS: Switch to the discrete graphics device.
  394. * * DIGD: Delayed switch to the integrated graphics device.
  395. * This will perform the switch once the last user space process has
  396. * closed the device files of the GPUs and the audio client.
  397. * * DDIS: Delayed switch to the discrete graphics device.
  398. * * MIGD: Mux-only switch to the integrated graphics device.
  399. * Does not remap console or change the power state of either gpu.
  400. * If the integrated GPU is currently off, the screen will turn black.
  401. * If it is on, the screen will show whatever happens to be in VRAM.
  402. * Either way, the user has to blindly enter the command to switch back.
  403. * * MDIS: Mux-only switch to the discrete graphics device.
  404. *
  405. * For GPUs whose power state is controlled by the driver's runtime pm,
  406. * the ON and OFF commands are a no-op (see next section).
  407. *
  408. * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
  409. * should not be used.
  410. */
  411. static int vga_switcheroo_show(struct seq_file *m, void *v)
  412. {
  413. struct vga_switcheroo_client *client;
  414. int i = 0;
  415. mutex_lock(&vgasr_mutex);
  416. list_for_each_entry(client, &vgasr_priv.clients, list) {
  417. seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
  418. client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
  419. "IGD",
  420. client_is_vga(client) ? "" : "-Audio",
  421. client->active ? '+' : ' ',
  422. client->driver_power_control ? "Dyn" : "",
  423. client->pwr_state ? "Pwr" : "Off",
  424. pci_name(client->pdev));
  425. i++;
  426. }
  427. mutex_unlock(&vgasr_mutex);
  428. return 0;
  429. }
  430. static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
  431. {
  432. return single_open(file, vga_switcheroo_show, NULL);
  433. }
  434. static int vga_switchon(struct vga_switcheroo_client *client)
  435. {
  436. if (client->driver_power_control)
  437. return 0;
  438. if (vgasr_priv.handler->power_state)
  439. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
  440. /* call the driver callback to turn on device */
  441. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
  442. client->pwr_state = VGA_SWITCHEROO_ON;
  443. return 0;
  444. }
  445. static int vga_switchoff(struct vga_switcheroo_client *client)
  446. {
  447. if (client->driver_power_control)
  448. return 0;
  449. /* call the driver callback to turn off device */
  450. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
  451. if (vgasr_priv.handler->power_state)
  452. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
  453. client->pwr_state = VGA_SWITCHEROO_OFF;
  454. return 0;
  455. }
  456. static void set_audio_state(enum vga_switcheroo_client_id id,
  457. enum vga_switcheroo_state state)
  458. {
  459. struct vga_switcheroo_client *client;
  460. client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
  461. if (client && client->pwr_state != state) {
  462. client->ops->set_gpu_state(client->pdev, state);
  463. client->pwr_state = state;
  464. }
  465. }
  466. /* stage one happens before delay */
  467. static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
  468. {
  469. struct vga_switcheroo_client *active;
  470. active = find_active_client(&vgasr_priv.clients);
  471. if (!active)
  472. return 0;
  473. if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
  474. vga_switchon(new_client);
  475. vga_set_default_device(new_client->pdev);
  476. return 0;
  477. }
  478. /* post delay */
  479. static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
  480. {
  481. int ret;
  482. struct vga_switcheroo_client *active;
  483. active = find_active_client(&vgasr_priv.clients);
  484. if (!active)
  485. return 0;
  486. active->active = false;
  487. set_audio_state(active->id, VGA_SWITCHEROO_OFF);
  488. if (new_client->fb_info) {
  489. struct fb_event event;
  490. console_lock();
  491. event.info = new_client->fb_info;
  492. fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
  493. console_unlock();
  494. }
  495. ret = vgasr_priv.handler->switchto(new_client->id);
  496. if (ret)
  497. return ret;
  498. if (new_client->ops->reprobe)
  499. new_client->ops->reprobe(new_client->pdev);
  500. if (active->pwr_state == VGA_SWITCHEROO_ON)
  501. vga_switchoff(active);
  502. set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
  503. new_client->active = true;
  504. return 0;
  505. }
  506. static bool check_can_switch(void)
  507. {
  508. struct vga_switcheroo_client *client;
  509. list_for_each_entry(client, &vgasr_priv.clients, list) {
  510. if (!client->ops->can_switch(client->pdev)) {
  511. pr_err("client %x refused switch\n", client->id);
  512. return false;
  513. }
  514. }
  515. return true;
  516. }
  517. static ssize_t
  518. vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
  519. size_t cnt, loff_t *ppos)
  520. {
  521. char usercmd[64];
  522. int ret;
  523. bool delay = false, can_switch;
  524. bool just_mux = false;
  525. enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
  526. struct vga_switcheroo_client *client = NULL;
  527. if (cnt > 63)
  528. cnt = 63;
  529. if (copy_from_user(usercmd, ubuf, cnt))
  530. return -EFAULT;
  531. mutex_lock(&vgasr_mutex);
  532. if (!vgasr_priv.active) {
  533. cnt = -EINVAL;
  534. goto out;
  535. }
  536. /* pwr off the device not in use */
  537. if (strncmp(usercmd, "OFF", 3) == 0) {
  538. list_for_each_entry(client, &vgasr_priv.clients, list) {
  539. if (client->active || client_is_audio(client))
  540. continue;
  541. if (client->driver_power_control)
  542. continue;
  543. set_audio_state(client->id, VGA_SWITCHEROO_OFF);
  544. if (client->pwr_state == VGA_SWITCHEROO_ON)
  545. vga_switchoff(client);
  546. }
  547. goto out;
  548. }
  549. /* pwr on the device not in use */
  550. if (strncmp(usercmd, "ON", 2) == 0) {
  551. list_for_each_entry(client, &vgasr_priv.clients, list) {
  552. if (client->active || client_is_audio(client))
  553. continue;
  554. if (client->driver_power_control)
  555. continue;
  556. if (client->pwr_state == VGA_SWITCHEROO_OFF)
  557. vga_switchon(client);
  558. set_audio_state(client->id, VGA_SWITCHEROO_ON);
  559. }
  560. goto out;
  561. }
  562. /* request a delayed switch - test can we switch now */
  563. if (strncmp(usercmd, "DIGD", 4) == 0) {
  564. client_id = VGA_SWITCHEROO_IGD;
  565. delay = true;
  566. }
  567. if (strncmp(usercmd, "DDIS", 4) == 0) {
  568. client_id = VGA_SWITCHEROO_DIS;
  569. delay = true;
  570. }
  571. if (strncmp(usercmd, "IGD", 3) == 0)
  572. client_id = VGA_SWITCHEROO_IGD;
  573. if (strncmp(usercmd, "DIS", 3) == 0)
  574. client_id = VGA_SWITCHEROO_DIS;
  575. if (strncmp(usercmd, "MIGD", 4) == 0) {
  576. just_mux = true;
  577. client_id = VGA_SWITCHEROO_IGD;
  578. }
  579. if (strncmp(usercmd, "MDIS", 4) == 0) {
  580. just_mux = true;
  581. client_id = VGA_SWITCHEROO_DIS;
  582. }
  583. if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
  584. goto out;
  585. client = find_client_from_id(&vgasr_priv.clients, client_id);
  586. if (!client)
  587. goto out;
  588. vgasr_priv.delayed_switch_active = false;
  589. if (just_mux) {
  590. ret = vgasr_priv.handler->switchto(client_id);
  591. goto out;
  592. }
  593. if (client->active)
  594. goto out;
  595. /* okay we want a switch - test if devices are willing to switch */
  596. can_switch = check_can_switch();
  597. if (can_switch == false && delay == false)
  598. goto out;
  599. if (can_switch) {
  600. ret = vga_switchto_stage1(client);
  601. if (ret)
  602. pr_err("switching failed stage 1 %d\n", ret);
  603. ret = vga_switchto_stage2(client);
  604. if (ret)
  605. pr_err("switching failed stage 2 %d\n", ret);
  606. } else {
  607. pr_info("setting delayed switch to client %d\n", client->id);
  608. vgasr_priv.delayed_switch_active = true;
  609. vgasr_priv.delayed_client_id = client_id;
  610. ret = vga_switchto_stage1(client);
  611. if (ret)
  612. pr_err("delayed switching stage 1 failed %d\n", ret);
  613. }
  614. out:
  615. mutex_unlock(&vgasr_mutex);
  616. return cnt;
  617. }
  618. static const struct file_operations vga_switcheroo_debugfs_fops = {
  619. .owner = THIS_MODULE,
  620. .open = vga_switcheroo_debugfs_open,
  621. .write = vga_switcheroo_debugfs_write,
  622. .read = seq_read,
  623. .llseek = seq_lseek,
  624. .release = single_release,
  625. };
  626. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
  627. {
  628. debugfs_remove(priv->switch_file);
  629. priv->switch_file = NULL;
  630. debugfs_remove(priv->debugfs_root);
  631. priv->debugfs_root = NULL;
  632. }
  633. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
  634. {
  635. static const char mp[] = "/sys/kernel/debug";
  636. /* already initialised */
  637. if (priv->debugfs_root)
  638. return 0;
  639. priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
  640. if (!priv->debugfs_root) {
  641. pr_err("Cannot create %s/vgaswitcheroo\n", mp);
  642. goto fail;
  643. }
  644. priv->switch_file = debugfs_create_file("switch", 0644,
  645. priv->debugfs_root, NULL,
  646. &vga_switcheroo_debugfs_fops);
  647. if (!priv->switch_file) {
  648. pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
  649. goto fail;
  650. }
  651. return 0;
  652. fail:
  653. vga_switcheroo_debugfs_fini(priv);
  654. return -1;
  655. }
  656. /**
  657. * vga_switcheroo_process_delayed_switch() - helper for delayed switching
  658. *
  659. * Process a delayed switch if one is pending. DRM drivers should call this
  660. * from their ->lastclose callback.
  661. *
  662. * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
  663. * has unregistered in the meantime or if there are other clients blocking the
  664. * switch. If the actual switch fails, an error is reported and 0 is returned.
  665. */
  666. int vga_switcheroo_process_delayed_switch(void)
  667. {
  668. struct vga_switcheroo_client *client;
  669. int ret;
  670. int err = -EINVAL;
  671. mutex_lock(&vgasr_mutex);
  672. if (!vgasr_priv.delayed_switch_active)
  673. goto err;
  674. pr_info("processing delayed switch to %d\n",
  675. vgasr_priv.delayed_client_id);
  676. client = find_client_from_id(&vgasr_priv.clients,
  677. vgasr_priv.delayed_client_id);
  678. if (!client || !check_can_switch())
  679. goto err;
  680. ret = vga_switchto_stage2(client);
  681. if (ret)
  682. pr_err("delayed switching failed stage 2 %d\n", ret);
  683. vgasr_priv.delayed_switch_active = false;
  684. err = 0;
  685. err:
  686. mutex_unlock(&vgasr_mutex);
  687. return err;
  688. }
  689. EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
  690. /**
  691. * DOC: Driver power control
  692. *
  693. * In this mode of use, the discrete GPU automatically powers up and down at
  694. * the discretion of the driver's runtime pm. On muxed machines, the user may
  695. * still influence the muxer state by way of the debugfs interface, however
  696. * the ON and OFF commands become a no-op for the discrete GPU.
  697. *
  698. * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
  699. * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
  700. * command line disables it.
  701. *
  702. * When the driver decides to power up or down, it notifies vga_switcheroo
  703. * thereof so that it can (a) power the audio device on the GPU up or down,
  704. * and (b) update its internal power state representation for the device.
  705. * This is achieved by vga_switcheroo_set_dynamic_switch().
  706. *
  707. * After the GPU has been suspended, the handler needs to be called to cut
  708. * power to the GPU. Likewise it needs to reinstate power before the GPU
  709. * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
  710. * which augments the GPU's suspend/resume functions by the requisite
  711. * calls to the handler.
  712. *
  713. * When the audio device resumes, the GPU needs to be woken. This is achieved
  714. * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
  715. * audio device's resume function.
  716. *
  717. * On muxed machines, if the mux is initially switched to the discrete GPU,
  718. * the user ends up with a black screen when the GPU powers down after boot.
  719. * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
  720. * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
  721. */
  722. static void vga_switcheroo_power_switch(struct pci_dev *pdev,
  723. enum vga_switcheroo_state state)
  724. {
  725. struct vga_switcheroo_client *client;
  726. if (!vgasr_priv.handler->power_state)
  727. return;
  728. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  729. if (!client)
  730. return;
  731. if (!client->driver_power_control)
  732. return;
  733. vgasr_priv.handler->power_state(client->id, state);
  734. }
  735. /**
  736. * vga_switcheroo_set_dynamic_switch() - helper for driver power control
  737. * @pdev: client pci device
  738. * @dynamic: new power state
  739. *
  740. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  741. * When the driver decides to power up or down, it notifies vga_switcheroo
  742. * thereof using this helper so that it can (a) power the audio device on
  743. * the GPU up or down, and (b) update its internal power state representation
  744. * for the device.
  745. */
  746. void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
  747. enum vga_switcheroo_state dynamic)
  748. {
  749. struct vga_switcheroo_client *client;
  750. mutex_lock(&vgasr_mutex);
  751. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  752. if (!client || !client->driver_power_control) {
  753. mutex_unlock(&vgasr_mutex);
  754. return;
  755. }
  756. client->pwr_state = dynamic;
  757. set_audio_state(client->id, dynamic);
  758. mutex_unlock(&vgasr_mutex);
  759. }
  760. EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
  761. /* switcheroo power domain */
  762. static int vga_switcheroo_runtime_suspend(struct device *dev)
  763. {
  764. struct pci_dev *pdev = to_pci_dev(dev);
  765. int ret;
  766. ret = dev->bus->pm->runtime_suspend(dev);
  767. if (ret)
  768. return ret;
  769. mutex_lock(&vgasr_mutex);
  770. if (vgasr_priv.handler->switchto)
  771. vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
  772. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
  773. mutex_unlock(&vgasr_mutex);
  774. return 0;
  775. }
  776. static int vga_switcheroo_runtime_resume(struct device *dev)
  777. {
  778. struct pci_dev *pdev = to_pci_dev(dev);
  779. int ret;
  780. mutex_lock(&vgasr_mutex);
  781. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
  782. mutex_unlock(&vgasr_mutex);
  783. ret = dev->bus->pm->runtime_resume(dev);
  784. if (ret)
  785. return ret;
  786. return 0;
  787. }
  788. /**
  789. * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
  790. * @dev: vga client device
  791. * @domain: power domain
  792. *
  793. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  794. * After the GPU has been suspended, the handler needs to be called to cut
  795. * power to the GPU. Likewise it needs to reinstate power before the GPU
  796. * can resume. To this end, this helper augments the suspend/resume functions
  797. * by the requisite calls to the handler. It needs only be called on platforms
  798. * where the power switch is separate to the device being powered down.
  799. */
  800. int vga_switcheroo_init_domain_pm_ops(struct device *dev,
  801. struct dev_pm_domain *domain)
  802. {
  803. /* copy over all the bus versions */
  804. if (dev->bus && dev->bus->pm) {
  805. domain->ops = *dev->bus->pm;
  806. domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
  807. domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
  808. dev->pm_domain = domain;
  809. return 0;
  810. }
  811. dev->pm_domain = NULL;
  812. return -EINVAL;
  813. }
  814. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
  815. void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
  816. {
  817. dev->pm_domain = NULL;
  818. }
  819. EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
  820. static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
  821. {
  822. struct pci_dev *pdev = to_pci_dev(dev);
  823. struct vga_switcheroo_client *client;
  824. struct device *video_dev = NULL;
  825. int ret;
  826. /* we need to check if we have to switch back on the video
  827. device so the audio device can come back */
  828. mutex_lock(&vgasr_mutex);
  829. list_for_each_entry(client, &vgasr_priv.clients, list) {
  830. if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
  831. client_is_vga(client)) {
  832. video_dev = &client->pdev->dev;
  833. break;
  834. }
  835. }
  836. mutex_unlock(&vgasr_mutex);
  837. if (video_dev) {
  838. ret = pm_runtime_get_sync(video_dev);
  839. if (ret && ret != 1)
  840. return ret;
  841. }
  842. ret = dev->bus->pm->runtime_resume(dev);
  843. /* put the reference for the gpu */
  844. if (video_dev) {
  845. pm_runtime_mark_last_busy(video_dev);
  846. pm_runtime_put_autosuspend(video_dev);
  847. }
  848. return ret;
  849. }
  850. /**
  851. * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
  852. * power control
  853. * @dev: audio client device
  854. * @domain: power domain
  855. *
  856. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  857. * When the audio device resumes, the GPU needs to be woken. This helper
  858. * augments the audio device's resume function to do that.
  859. *
  860. * Return: 0 on success, -EINVAL if no power management operations are
  861. * defined for this device.
  862. */
  863. int
  864. vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
  865. struct dev_pm_domain *domain)
  866. {
  867. /* copy over all the bus versions */
  868. if (dev->bus && dev->bus->pm) {
  869. domain->ops = *dev->bus->pm;
  870. domain->ops.runtime_resume =
  871. vga_switcheroo_runtime_resume_hdmi_audio;
  872. dev->pm_domain = domain;
  873. return 0;
  874. }
  875. dev->pm_domain = NULL;
  876. return -EINVAL;
  877. }
  878. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);