vga_switcheroo.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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
  92. * @ops: client callbacks
  93. * @id: client identifier. Determining the id requires the handler,
  94. * so gpus are initially assigned VGA_SWITCHEROO_UNKNOWN_ID
  95. * and later given their true id in vga_switcheroo_enable()
  96. * @active: whether the outputs are currently switched to this client
  97. * @driver_power_control: whether power state is controlled by the driver's
  98. * runtime pm. If true, writing ON and OFF to the vga_switcheroo debugfs
  99. * interface is a no-op so as not to interfere with runtime pm
  100. * @list: client list
  101. *
  102. * Registered client. A client can be either a GPU or an audio device on a GPU.
  103. * For audio clients, the @fb_info, @active and @driver_power_control members
  104. * 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 power state of the
  307. * client is assumed to be ON. 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, false);
  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. /**
  373. * vga_switcheroo_get_client_state() - obtain power state of a given client
  374. * @pdev: client pci device
  375. *
  376. * Obtain power state of a given client as seen from vga_switcheroo.
  377. * The function is only called from hda_intel.c.
  378. *
  379. * Return: Power state.
  380. */
  381. enum vga_switcheroo_state vga_switcheroo_get_client_state(struct pci_dev *pdev)
  382. {
  383. struct vga_switcheroo_client *client;
  384. enum vga_switcheroo_state ret;
  385. mutex_lock(&vgasr_mutex);
  386. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  387. if (!client)
  388. ret = VGA_SWITCHEROO_NOT_FOUND;
  389. else
  390. ret = client->pwr_state;
  391. mutex_unlock(&vgasr_mutex);
  392. return ret;
  393. }
  394. EXPORT_SYMBOL(vga_switcheroo_get_client_state);
  395. /**
  396. * vga_switcheroo_unregister_client() - unregister client
  397. * @pdev: client pci device
  398. *
  399. * Unregister client. Disable vga_switcheroo if this is a vga client (GPU).
  400. */
  401. void vga_switcheroo_unregister_client(struct pci_dev *pdev)
  402. {
  403. struct vga_switcheroo_client *client;
  404. mutex_lock(&vgasr_mutex);
  405. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  406. if (client) {
  407. if (client_is_vga(client))
  408. vgasr_priv.registered_clients--;
  409. list_del(&client->list);
  410. kfree(client);
  411. }
  412. if (vgasr_priv.active && vgasr_priv.registered_clients < 2) {
  413. pr_info("disabled\n");
  414. vga_switcheroo_debugfs_fini(&vgasr_priv);
  415. vgasr_priv.active = false;
  416. }
  417. mutex_unlock(&vgasr_mutex);
  418. }
  419. EXPORT_SYMBOL(vga_switcheroo_unregister_client);
  420. /**
  421. * vga_switcheroo_client_fb_set() - set framebuffer of a given client
  422. * @pdev: client pci device
  423. * @info: framebuffer
  424. *
  425. * Set framebuffer of a given client. The console will be remapped to this
  426. * on switching.
  427. */
  428. void vga_switcheroo_client_fb_set(struct pci_dev *pdev,
  429. struct fb_info *info)
  430. {
  431. struct vga_switcheroo_client *client;
  432. mutex_lock(&vgasr_mutex);
  433. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  434. if (client)
  435. client->fb_info = info;
  436. mutex_unlock(&vgasr_mutex);
  437. }
  438. EXPORT_SYMBOL(vga_switcheroo_client_fb_set);
  439. /**
  440. * vga_switcheroo_lock_ddc() - temporarily switch DDC lines to a given client
  441. * @pdev: client pci device
  442. *
  443. * Temporarily switch DDC lines to the client identified by @pdev
  444. * (but leave the outputs otherwise switched to where they are).
  445. * This allows the inactive client to probe EDID. The DDC lines must
  446. * afterwards be switched back by calling vga_switcheroo_unlock_ddc(),
  447. * even if this function returns an error.
  448. *
  449. * Return: Previous DDC owner on success or a negative int on error.
  450. * Specifically, %-ENODEV if no handler has registered or if the handler
  451. * does not support switching the DDC lines. Also, a negative value
  452. * returned by the handler is propagated back to the caller.
  453. * The return value has merely an informational purpose for any caller
  454. * which might be interested in it. It is acceptable to ignore the return
  455. * value and simply rely on the result of the subsequent EDID probe,
  456. * which will be %NULL if DDC switching failed.
  457. */
  458. int vga_switcheroo_lock_ddc(struct pci_dev *pdev)
  459. {
  460. enum vga_switcheroo_client_id id;
  461. mutex_lock(&vgasr_priv.mux_hw_lock);
  462. if (!vgasr_priv.handler || !vgasr_priv.handler->switch_ddc) {
  463. vgasr_priv.old_ddc_owner = -ENODEV;
  464. return -ENODEV;
  465. }
  466. id = vgasr_priv.handler->get_client_id(pdev);
  467. vgasr_priv.old_ddc_owner = vgasr_priv.handler->switch_ddc(id);
  468. return vgasr_priv.old_ddc_owner;
  469. }
  470. EXPORT_SYMBOL(vga_switcheroo_lock_ddc);
  471. /**
  472. * vga_switcheroo_unlock_ddc() - switch DDC lines back to previous owner
  473. * @pdev: client pci device
  474. *
  475. * Switch DDC lines back to the previous owner after calling
  476. * vga_switcheroo_lock_ddc(). This must be called even if
  477. * vga_switcheroo_lock_ddc() returned an error.
  478. *
  479. * Return: Previous DDC owner on success (i.e. the client identifier of @pdev)
  480. * or a negative int on error.
  481. * Specifically, %-ENODEV if no handler has registered or if the handler
  482. * does not support switching the DDC lines. Also, a negative value
  483. * returned by the handler is propagated back to the caller.
  484. * Finally, invoking this function without calling vga_switcheroo_lock_ddc()
  485. * first is not allowed and will result in %-EINVAL.
  486. */
  487. int vga_switcheroo_unlock_ddc(struct pci_dev *pdev)
  488. {
  489. enum vga_switcheroo_client_id id;
  490. int ret = vgasr_priv.old_ddc_owner;
  491. if (WARN_ON_ONCE(!mutex_is_locked(&vgasr_priv.mux_hw_lock)))
  492. return -EINVAL;
  493. if (vgasr_priv.old_ddc_owner >= 0) {
  494. id = vgasr_priv.handler->get_client_id(pdev);
  495. if (vgasr_priv.old_ddc_owner != id)
  496. ret = vgasr_priv.handler->switch_ddc(
  497. vgasr_priv.old_ddc_owner);
  498. }
  499. mutex_unlock(&vgasr_priv.mux_hw_lock);
  500. return ret;
  501. }
  502. EXPORT_SYMBOL(vga_switcheroo_unlock_ddc);
  503. /**
  504. * DOC: Manual switching and manual power control
  505. *
  506. * In this mode of use, the file /sys/kernel/debug/vgaswitcheroo/switch
  507. * can be read to retrieve the current vga_switcheroo state and commands
  508. * can be written to it to change the state. The file appears as soon as
  509. * two GPU drivers and one handler have registered with vga_switcheroo.
  510. * The following commands are understood:
  511. *
  512. * * OFF: Power off the device not in use.
  513. * * ON: Power on the device not in use.
  514. * * IGD: Switch to the integrated graphics device.
  515. * Power on the integrated GPU if necessary, power off the discrete GPU.
  516. * Prerequisite is that no user space processes (e.g. Xorg, alsactl)
  517. * have opened device files of the GPUs or the audio client. If the
  518. * switch fails, the user may invoke lsof(8) or fuser(1) on /dev/dri/
  519. * and /dev/snd/controlC1 to identify processes blocking the switch.
  520. * * DIS: Switch to the discrete graphics device.
  521. * * DIGD: Delayed switch to the integrated graphics device.
  522. * This will perform the switch once the last user space process has
  523. * closed the device files of the GPUs and the audio client.
  524. * * DDIS: Delayed switch to the discrete graphics device.
  525. * * MIGD: Mux-only switch to the integrated graphics device.
  526. * Does not remap console or change the power state of either gpu.
  527. * If the integrated GPU is currently off, the screen will turn black.
  528. * If it is on, the screen will show whatever happens to be in VRAM.
  529. * Either way, the user has to blindly enter the command to switch back.
  530. * * MDIS: Mux-only switch to the discrete graphics device.
  531. *
  532. * For GPUs whose power state is controlled by the driver's runtime pm,
  533. * the ON and OFF commands are a no-op (see next section).
  534. *
  535. * For muxless machines, the IGD/DIS, DIGD/DDIS and MIGD/MDIS commands
  536. * should not be used.
  537. */
  538. static int vga_switcheroo_show(struct seq_file *m, void *v)
  539. {
  540. struct vga_switcheroo_client *client;
  541. int i = 0;
  542. mutex_lock(&vgasr_mutex);
  543. list_for_each_entry(client, &vgasr_priv.clients, list) {
  544. seq_printf(m, "%d:%s%s:%c:%s%s:%s\n", i,
  545. client_id(client) == VGA_SWITCHEROO_DIS ? "DIS" :
  546. "IGD",
  547. client_is_vga(client) ? "" : "-Audio",
  548. client->active ? '+' : ' ',
  549. client->driver_power_control ? "Dyn" : "",
  550. client->pwr_state ? "Pwr" : "Off",
  551. pci_name(client->pdev));
  552. i++;
  553. }
  554. mutex_unlock(&vgasr_mutex);
  555. return 0;
  556. }
  557. static int vga_switcheroo_debugfs_open(struct inode *inode, struct file *file)
  558. {
  559. return single_open(file, vga_switcheroo_show, NULL);
  560. }
  561. static int vga_switchon(struct vga_switcheroo_client *client)
  562. {
  563. if (client->driver_power_control)
  564. return 0;
  565. if (vgasr_priv.handler->power_state)
  566. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_ON);
  567. /* call the driver callback to turn on device */
  568. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_ON);
  569. client->pwr_state = VGA_SWITCHEROO_ON;
  570. return 0;
  571. }
  572. static int vga_switchoff(struct vga_switcheroo_client *client)
  573. {
  574. if (client->driver_power_control)
  575. return 0;
  576. /* call the driver callback to turn off device */
  577. client->ops->set_gpu_state(client->pdev, VGA_SWITCHEROO_OFF);
  578. if (vgasr_priv.handler->power_state)
  579. vgasr_priv.handler->power_state(client->id, VGA_SWITCHEROO_OFF);
  580. client->pwr_state = VGA_SWITCHEROO_OFF;
  581. return 0;
  582. }
  583. static void set_audio_state(enum vga_switcheroo_client_id id,
  584. enum vga_switcheroo_state state)
  585. {
  586. struct vga_switcheroo_client *client;
  587. client = find_client_from_id(&vgasr_priv.clients, id | ID_BIT_AUDIO);
  588. if (client && client->pwr_state != state) {
  589. client->ops->set_gpu_state(client->pdev, state);
  590. client->pwr_state = state;
  591. }
  592. }
  593. /* stage one happens before delay */
  594. static int vga_switchto_stage1(struct vga_switcheroo_client *new_client)
  595. {
  596. struct vga_switcheroo_client *active;
  597. active = find_active_client(&vgasr_priv.clients);
  598. if (!active)
  599. return 0;
  600. if (new_client->pwr_state == VGA_SWITCHEROO_OFF)
  601. vga_switchon(new_client);
  602. vga_set_default_device(new_client->pdev);
  603. return 0;
  604. }
  605. /* post delay */
  606. static int vga_switchto_stage2(struct vga_switcheroo_client *new_client)
  607. {
  608. int ret;
  609. struct vga_switcheroo_client *active;
  610. active = find_active_client(&vgasr_priv.clients);
  611. if (!active)
  612. return 0;
  613. active->active = false;
  614. set_audio_state(active->id, VGA_SWITCHEROO_OFF);
  615. if (new_client->fb_info) {
  616. struct fb_event event;
  617. console_lock();
  618. event.info = new_client->fb_info;
  619. fb_notifier_call_chain(FB_EVENT_REMAP_ALL_CONSOLE, &event);
  620. console_unlock();
  621. }
  622. mutex_lock(&vgasr_priv.mux_hw_lock);
  623. ret = vgasr_priv.handler->switchto(new_client->id);
  624. mutex_unlock(&vgasr_priv.mux_hw_lock);
  625. if (ret)
  626. return ret;
  627. if (new_client->ops->reprobe)
  628. new_client->ops->reprobe(new_client->pdev);
  629. if (active->pwr_state == VGA_SWITCHEROO_ON)
  630. vga_switchoff(active);
  631. set_audio_state(new_client->id, VGA_SWITCHEROO_ON);
  632. new_client->active = true;
  633. return 0;
  634. }
  635. static bool check_can_switch(void)
  636. {
  637. struct vga_switcheroo_client *client;
  638. list_for_each_entry(client, &vgasr_priv.clients, list) {
  639. if (!client->ops->can_switch(client->pdev)) {
  640. pr_err("client %x refused switch\n", client->id);
  641. return false;
  642. }
  643. }
  644. return true;
  645. }
  646. static ssize_t
  647. vga_switcheroo_debugfs_write(struct file *filp, const char __user *ubuf,
  648. size_t cnt, loff_t *ppos)
  649. {
  650. char usercmd[64];
  651. int ret;
  652. bool delay = false, can_switch;
  653. bool just_mux = false;
  654. enum vga_switcheroo_client_id client_id = VGA_SWITCHEROO_UNKNOWN_ID;
  655. struct vga_switcheroo_client *client = NULL;
  656. if (cnt > 63)
  657. cnt = 63;
  658. if (copy_from_user(usercmd, ubuf, cnt))
  659. return -EFAULT;
  660. mutex_lock(&vgasr_mutex);
  661. if (!vgasr_priv.active) {
  662. cnt = -EINVAL;
  663. goto out;
  664. }
  665. /* pwr off the device not in use */
  666. if (strncmp(usercmd, "OFF", 3) == 0) {
  667. list_for_each_entry(client, &vgasr_priv.clients, list) {
  668. if (client->active || client_is_audio(client))
  669. continue;
  670. if (client->driver_power_control)
  671. continue;
  672. set_audio_state(client->id, VGA_SWITCHEROO_OFF);
  673. if (client->pwr_state == VGA_SWITCHEROO_ON)
  674. vga_switchoff(client);
  675. }
  676. goto out;
  677. }
  678. /* pwr on the device not in use */
  679. if (strncmp(usercmd, "ON", 2) == 0) {
  680. list_for_each_entry(client, &vgasr_priv.clients, list) {
  681. if (client->active || client_is_audio(client))
  682. continue;
  683. if (client->driver_power_control)
  684. continue;
  685. if (client->pwr_state == VGA_SWITCHEROO_OFF)
  686. vga_switchon(client);
  687. set_audio_state(client->id, VGA_SWITCHEROO_ON);
  688. }
  689. goto out;
  690. }
  691. /* request a delayed switch - test can we switch now */
  692. if (strncmp(usercmd, "DIGD", 4) == 0) {
  693. client_id = VGA_SWITCHEROO_IGD;
  694. delay = true;
  695. }
  696. if (strncmp(usercmd, "DDIS", 4) == 0) {
  697. client_id = VGA_SWITCHEROO_DIS;
  698. delay = true;
  699. }
  700. if (strncmp(usercmd, "IGD", 3) == 0)
  701. client_id = VGA_SWITCHEROO_IGD;
  702. if (strncmp(usercmd, "DIS", 3) == 0)
  703. client_id = VGA_SWITCHEROO_DIS;
  704. if (strncmp(usercmd, "MIGD", 4) == 0) {
  705. just_mux = true;
  706. client_id = VGA_SWITCHEROO_IGD;
  707. }
  708. if (strncmp(usercmd, "MDIS", 4) == 0) {
  709. just_mux = true;
  710. client_id = VGA_SWITCHEROO_DIS;
  711. }
  712. if (client_id == VGA_SWITCHEROO_UNKNOWN_ID)
  713. goto out;
  714. client = find_client_from_id(&vgasr_priv.clients, client_id);
  715. if (!client)
  716. goto out;
  717. vgasr_priv.delayed_switch_active = false;
  718. if (just_mux) {
  719. mutex_lock(&vgasr_priv.mux_hw_lock);
  720. ret = vgasr_priv.handler->switchto(client_id);
  721. mutex_unlock(&vgasr_priv.mux_hw_lock);
  722. goto out;
  723. }
  724. if (client->active)
  725. goto out;
  726. /* okay we want a switch - test if devices are willing to switch */
  727. can_switch = check_can_switch();
  728. if (can_switch == false && delay == false)
  729. goto out;
  730. if (can_switch) {
  731. ret = vga_switchto_stage1(client);
  732. if (ret)
  733. pr_err("switching failed stage 1 %d\n", ret);
  734. ret = vga_switchto_stage2(client);
  735. if (ret)
  736. pr_err("switching failed stage 2 %d\n", ret);
  737. } else {
  738. pr_info("setting delayed switch to client %d\n", client->id);
  739. vgasr_priv.delayed_switch_active = true;
  740. vgasr_priv.delayed_client_id = client_id;
  741. ret = vga_switchto_stage1(client);
  742. if (ret)
  743. pr_err("delayed switching stage 1 failed %d\n", ret);
  744. }
  745. out:
  746. mutex_unlock(&vgasr_mutex);
  747. return cnt;
  748. }
  749. static const struct file_operations vga_switcheroo_debugfs_fops = {
  750. .owner = THIS_MODULE,
  751. .open = vga_switcheroo_debugfs_open,
  752. .write = vga_switcheroo_debugfs_write,
  753. .read = seq_read,
  754. .llseek = seq_lseek,
  755. .release = single_release,
  756. };
  757. static void vga_switcheroo_debugfs_fini(struct vgasr_priv *priv)
  758. {
  759. debugfs_remove(priv->switch_file);
  760. priv->switch_file = NULL;
  761. debugfs_remove(priv->debugfs_root);
  762. priv->debugfs_root = NULL;
  763. }
  764. static int vga_switcheroo_debugfs_init(struct vgasr_priv *priv)
  765. {
  766. static const char mp[] = "/sys/kernel/debug";
  767. /* already initialised */
  768. if (priv->debugfs_root)
  769. return 0;
  770. priv->debugfs_root = debugfs_create_dir("vgaswitcheroo", NULL);
  771. if (!priv->debugfs_root) {
  772. pr_err("Cannot create %s/vgaswitcheroo\n", mp);
  773. goto fail;
  774. }
  775. priv->switch_file = debugfs_create_file("switch", 0644,
  776. priv->debugfs_root, NULL,
  777. &vga_switcheroo_debugfs_fops);
  778. if (!priv->switch_file) {
  779. pr_err("cannot create %s/vgaswitcheroo/switch\n", mp);
  780. goto fail;
  781. }
  782. return 0;
  783. fail:
  784. vga_switcheroo_debugfs_fini(priv);
  785. return -1;
  786. }
  787. /**
  788. * vga_switcheroo_process_delayed_switch() - helper for delayed switching
  789. *
  790. * Process a delayed switch if one is pending. DRM drivers should call this
  791. * from their ->lastclose callback.
  792. *
  793. * Return: 0 on success. -EINVAL if no delayed switch is pending, if the client
  794. * has unregistered in the meantime or if there are other clients blocking the
  795. * switch. If the actual switch fails, an error is reported and 0 is returned.
  796. */
  797. int vga_switcheroo_process_delayed_switch(void)
  798. {
  799. struct vga_switcheroo_client *client;
  800. int ret;
  801. int err = -EINVAL;
  802. mutex_lock(&vgasr_mutex);
  803. if (!vgasr_priv.delayed_switch_active)
  804. goto err;
  805. pr_info("processing delayed switch to %d\n",
  806. vgasr_priv.delayed_client_id);
  807. client = find_client_from_id(&vgasr_priv.clients,
  808. vgasr_priv.delayed_client_id);
  809. if (!client || !check_can_switch())
  810. goto err;
  811. ret = vga_switchto_stage2(client);
  812. if (ret)
  813. pr_err("delayed switching failed stage 2 %d\n", ret);
  814. vgasr_priv.delayed_switch_active = false;
  815. err = 0;
  816. err:
  817. mutex_unlock(&vgasr_mutex);
  818. return err;
  819. }
  820. EXPORT_SYMBOL(vga_switcheroo_process_delayed_switch);
  821. /**
  822. * DOC: Driver power control
  823. *
  824. * In this mode of use, the discrete GPU automatically powers up and down at
  825. * the discretion of the driver's runtime pm. On muxed machines, the user may
  826. * still influence the muxer state by way of the debugfs interface, however
  827. * the ON and OFF commands become a no-op for the discrete GPU.
  828. *
  829. * This mode is the default on Nvidia HybridPower/Optimus and ATI PowerXpress.
  830. * Specifying nouveau.runpm=0, radeon.runpm=0 or amdgpu.runpm=0 on the kernel
  831. * command line disables it.
  832. *
  833. * When the driver decides to power up or down, it notifies vga_switcheroo
  834. * thereof so that it can (a) power the audio device on the GPU up or down,
  835. * and (b) update its internal power state representation for the device.
  836. * This is achieved by vga_switcheroo_set_dynamic_switch().
  837. *
  838. * After the GPU has been suspended, the handler needs to be called to cut
  839. * power to the GPU. Likewise it needs to reinstate power before the GPU
  840. * can resume. This is achieved by vga_switcheroo_init_domain_pm_ops(),
  841. * which augments the GPU's suspend/resume functions by the requisite
  842. * calls to the handler.
  843. *
  844. * When the audio device resumes, the GPU needs to be woken. This is achieved
  845. * by vga_switcheroo_init_domain_pm_optimus_hdmi_audio(), which augments the
  846. * audio device's resume function.
  847. *
  848. * On muxed machines, if the mux is initially switched to the discrete GPU,
  849. * the user ends up with a black screen when the GPU powers down after boot.
  850. * As a workaround, the mux is forced to the integrated GPU on runtime suspend,
  851. * cf. https://bugs.freedesktop.org/show_bug.cgi?id=75917
  852. */
  853. static void vga_switcheroo_power_switch(struct pci_dev *pdev,
  854. enum vga_switcheroo_state state)
  855. {
  856. struct vga_switcheroo_client *client;
  857. if (!vgasr_priv.handler->power_state)
  858. return;
  859. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  860. if (!client)
  861. return;
  862. if (!client->driver_power_control)
  863. return;
  864. vgasr_priv.handler->power_state(client->id, state);
  865. }
  866. /**
  867. * vga_switcheroo_set_dynamic_switch() - helper for driver power control
  868. * @pdev: client pci device
  869. * @dynamic: new power state
  870. *
  871. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  872. * When the driver decides to power up or down, it notifies vga_switcheroo
  873. * thereof using this helper so that it can (a) power the audio device on
  874. * the GPU up or down, and (b) update its internal power state representation
  875. * for the device.
  876. */
  877. void vga_switcheroo_set_dynamic_switch(struct pci_dev *pdev,
  878. enum vga_switcheroo_state dynamic)
  879. {
  880. struct vga_switcheroo_client *client;
  881. mutex_lock(&vgasr_mutex);
  882. client = find_client_from_pci(&vgasr_priv.clients, pdev);
  883. if (!client || !client->driver_power_control) {
  884. mutex_unlock(&vgasr_mutex);
  885. return;
  886. }
  887. client->pwr_state = dynamic;
  888. set_audio_state(client->id, dynamic);
  889. mutex_unlock(&vgasr_mutex);
  890. }
  891. EXPORT_SYMBOL(vga_switcheroo_set_dynamic_switch);
  892. /* switcheroo power domain */
  893. static int vga_switcheroo_runtime_suspend(struct device *dev)
  894. {
  895. struct pci_dev *pdev = to_pci_dev(dev);
  896. int ret;
  897. ret = dev->bus->pm->runtime_suspend(dev);
  898. if (ret)
  899. return ret;
  900. mutex_lock(&vgasr_mutex);
  901. if (vgasr_priv.handler->switchto) {
  902. mutex_lock(&vgasr_priv.mux_hw_lock);
  903. vgasr_priv.handler->switchto(VGA_SWITCHEROO_IGD);
  904. mutex_unlock(&vgasr_priv.mux_hw_lock);
  905. }
  906. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_OFF);
  907. mutex_unlock(&vgasr_mutex);
  908. return 0;
  909. }
  910. static int vga_switcheroo_runtime_resume(struct device *dev)
  911. {
  912. struct pci_dev *pdev = to_pci_dev(dev);
  913. int ret;
  914. mutex_lock(&vgasr_mutex);
  915. vga_switcheroo_power_switch(pdev, VGA_SWITCHEROO_ON);
  916. mutex_unlock(&vgasr_mutex);
  917. ret = dev->bus->pm->runtime_resume(dev);
  918. if (ret)
  919. return ret;
  920. return 0;
  921. }
  922. /**
  923. * vga_switcheroo_init_domain_pm_ops() - helper for driver power control
  924. * @dev: vga client device
  925. * @domain: power domain
  926. *
  927. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  928. * After the GPU has been suspended, the handler needs to be called to cut
  929. * power to the GPU. Likewise it needs to reinstate power before the GPU
  930. * can resume. To this end, this helper augments the suspend/resume functions
  931. * by the requisite calls to the handler. It needs only be called on platforms
  932. * where the power switch is separate to the device being powered down.
  933. */
  934. int vga_switcheroo_init_domain_pm_ops(struct device *dev,
  935. struct dev_pm_domain *domain)
  936. {
  937. /* copy over all the bus versions */
  938. if (dev->bus && dev->bus->pm) {
  939. domain->ops = *dev->bus->pm;
  940. domain->ops.runtime_suspend = vga_switcheroo_runtime_suspend;
  941. domain->ops.runtime_resume = vga_switcheroo_runtime_resume;
  942. dev_pm_domain_set(dev, domain);
  943. return 0;
  944. }
  945. dev_pm_domain_set(dev, NULL);
  946. return -EINVAL;
  947. }
  948. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_ops);
  949. void vga_switcheroo_fini_domain_pm_ops(struct device *dev)
  950. {
  951. dev_pm_domain_set(dev, NULL);
  952. }
  953. EXPORT_SYMBOL(vga_switcheroo_fini_domain_pm_ops);
  954. static int vga_switcheroo_runtime_resume_hdmi_audio(struct device *dev)
  955. {
  956. struct pci_dev *pdev = to_pci_dev(dev);
  957. struct vga_switcheroo_client *client;
  958. struct device *video_dev = NULL;
  959. int ret;
  960. /* we need to check if we have to switch back on the video
  961. * device so the audio device can come back
  962. */
  963. mutex_lock(&vgasr_mutex);
  964. list_for_each_entry(client, &vgasr_priv.clients, list) {
  965. if (PCI_SLOT(client->pdev->devfn) == PCI_SLOT(pdev->devfn) &&
  966. client_is_vga(client)) {
  967. video_dev = &client->pdev->dev;
  968. break;
  969. }
  970. }
  971. mutex_unlock(&vgasr_mutex);
  972. if (video_dev) {
  973. ret = pm_runtime_get_sync(video_dev);
  974. if (ret && ret != 1)
  975. return ret;
  976. }
  977. ret = dev->bus->pm->runtime_resume(dev);
  978. /* put the reference for the gpu */
  979. if (video_dev) {
  980. pm_runtime_mark_last_busy(video_dev);
  981. pm_runtime_put_autosuspend(video_dev);
  982. }
  983. return ret;
  984. }
  985. /**
  986. * vga_switcheroo_init_domain_pm_optimus_hdmi_audio() - helper for driver
  987. * power control
  988. * @dev: audio client device
  989. * @domain: power domain
  990. *
  991. * Helper for GPUs whose power state is controlled by the driver's runtime pm.
  992. * When the audio device resumes, the GPU needs to be woken. This helper
  993. * augments the audio device's resume function to do that.
  994. *
  995. * Return: 0 on success, -EINVAL if no power management operations are
  996. * defined for this device.
  997. */
  998. int
  999. vga_switcheroo_init_domain_pm_optimus_hdmi_audio(struct device *dev,
  1000. struct dev_pm_domain *domain)
  1001. {
  1002. /* copy over all the bus versions */
  1003. if (dev->bus && dev->bus->pm) {
  1004. domain->ops = *dev->bus->pm;
  1005. domain->ops.runtime_resume =
  1006. vga_switcheroo_runtime_resume_hdmi_audio;
  1007. dev_pm_domain_set(dev, domain);
  1008. return 0;
  1009. }
  1010. dev_pm_domain_set(dev, NULL);
  1011. return -EINVAL;
  1012. }
  1013. EXPORT_SYMBOL(vga_switcheroo_init_domain_pm_optimus_hdmi_audio);