vga_switcheroo.c 33 KB

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