vga_switcheroo.c 35 KB

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