vga_switcheroo.c 33 KB

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