vga_switcheroo.c 32 KB

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