vga_switcheroo.c 32 KB

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