vga_switcheroo.c 34 KB

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